Genode Packages collection https://git.sr.ht/~ehmry/genodepkgs/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

hardware.nix 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. { config, pkgs, lib, ... }:
  2. with lib;
  3. {
  4. options = {
  5. networking.interfaces = lib.mkOption {
  6. type = with types;
  7. attrsOf (submodule ({ ... }: {
  8. options.genode = {
  9. driver = mkOption {
  10. type = types.enum [ "ipxe" "virtio" ];
  11. description = "Nic driver to use for this interface.";
  12. };
  13. stack = mkOption {
  14. type = with types; nullOr (enum [ "lwip" "lxip" ]);
  15. default = "lwip";
  16. description = "IP stack to use for this interface.";
  17. };
  18. };
  19. }));
  20. };
  21. hardware.genode.platform.policies = lib.mkOption {
  22. type = with types; listOf path;
  23. default = [ ];
  24. description = ''
  25. List of policies to append to the Genode platform driver.
  26. Type is Init.Config.Policy.Type.
  27. '';
  28. };
  29. hardware.genode = {
  30. ahci.enable = lib.mkEnableOption "AHCI (SATA) block driver";
  31. framebuffer = {
  32. enable = lib.mkEnableOption "framebuffer driver";
  33. driver = mkOption {
  34. type = types.enum [ "boot" "vesa" ];
  35. default = "vesa";
  36. };
  37. };
  38. usb = {
  39. enable = lib.mkEnableOption "USB driver";
  40. storage.enable = lib.mkEnableOption "USB mass storage driver";
  41. ehciSupport = lib.mkEnableOption "EHCI support" // { default = true; };
  42. ohciSupport = lib.mkEnableOption "OHCI support" // { default = true; };
  43. uhciSupport = lib.mkEnableOption "UHCI support" // { default = false; };
  44. xhciSupport = lib.mkEnableOption "XHCI support" // { default = true; };
  45. };
  46. };
  47. };
  48. config = {
  49. assertions = with builtins;
  50. let
  51. addrCheck = name: interface: {
  52. assertion = lessThan (length interface.ipv4.addresses) 2;
  53. message = "Genode interfaces do not support multihoming.";
  54. };
  55. routeCheck = name: interface: {
  56. assertion = lessThan (length interface.ipv4.routes) 2;
  57. message = "Genode interfaces do not support multiple routes.";
  58. };
  59. policyCheck = name: interface:
  60. let
  61. clientList = filter (x x != null) (lib.mapAttrsToList
  62. (childName: value:
  63. if any (nic: nic == name) value.routeToNics then
  64. childName
  65. else
  66. null) config.genode.init.children);
  67. in {
  68. assertion =
  69. trace clientList (clientList == [ ] || length clientList == 1);
  70. message = "Multiple routes to Nic ${name}, ${clientList}";
  71. };
  72. in lib.lists.concatMap
  73. (f: lib.mapAttrsToList f config.networking.interfaces) [
  74. addrCheck
  75. routeCheck
  76. policyCheck
  77. ];
  78. hardware.genode.ahci.enable = config.genode.boot.storeBackend == "ahci";
  79. hardware.genode.usb.storage.enable = config.genode.boot.storeBackend
  80. == "usb";
  81. hardware.genode.usb.enable = config.hardware.genode.usb.storage.enable;
  82. hardware.genode.platform.policies = lib.lists.imap0 (i: name:
  83. builtins.toFile (name + ".platform-policy.dhall") ''
  84. let Genode = env:DHALL_GENODE
  85. in Genode.Init.Config.Policy::{
  86. , service = "Platform"
  87. , label = Genode.Init.LabelSelector.prefix "nixos -> ${name}.driver"
  88. , content =
  89. [ Genode.Prelude.XML.leaf
  90. { name = "pci"
  91. , attributes = toMap {
  92. , class = "ETHERNET"
  93. , index = "${toString i}"
  94. }
  95. }
  96. ]
  97. }
  98. '') (builtins.attrNames config.networking.interfaces)
  99. ++ (lib.optional config.hardware.genode.ahci.enable
  100. (builtins.toFile ("ahci.platform-policy.dhall") ''
  101. let Genode = env:DHALL_GENODE
  102. in Genode.Init.Config.Policy::{
  103. , service = "Platform"
  104. , label = Genode.Init.LabelSelector.prefix "ahci_drv"
  105. , content =
  106. [ Genode.Prelude.XML.leaf
  107. { name = "pci", attributes = toMap { class = "AHCI" } }
  108. ]
  109. }
  110. '')) ++ (lib.optional config.hardware.genode.framebuffer.enable
  111. (builtins.toFile ("framebuffer.platform-policy.dhall") ''
  112. let Genode = env:DHALL_GENODE
  113. in Genode.Init.Config.Policy::{
  114. , service = "Platform"
  115. , label = Genode.Init.LabelSelector.prefix "fb_drv"
  116. , content =
  117. [ Genode.Prelude.XML.leaf
  118. { name = "pci", attributes = toMap { class = "VGA" } }
  119. ]
  120. }
  121. '')) ++ (lib.optional config.hardware.genode.usb.enable
  122. (builtins.toFile ("usb.platform-policy.dhall") ''
  123. let Genode = env:DHALL_GENODE
  124. in Genode.Init.Config.Policy::{
  125. , service = "Platform"
  126. , label = Genode.Init.LabelSelector.prefix "usb_drv"
  127. , content =
  128. [ Genode.Prelude.XML.leaf
  129. { name = "pci", attributes = toMap { class = "USB" } }
  130. ]
  131. }
  132. ''));
  133. genode.init.children = let
  134. nics = mapAttrs' (name: interface:
  135. let name' = name + ".driver";
  136. in {
  137. name = name';
  138. value = let
  139. binary = with pkgs.genodePackages;
  140. {
  141. ipxe = ipxe_nic_drv;
  142. virtio = virtio_nic_drv;
  143. }.${interface.genode.driver};
  144. in {
  145. inputs = [ binary ];
  146. configFile = let
  147. policy = lib.mapAttrsToList (childName: value:
  148. if any (nic: nic == name) value.routeToNics then ''
  149. Init.Config.Policy::{
  150. , service = "Nic"
  151. , label = Init.LabelSelector.prefix "${childName}"
  152. }
  153. '' else
  154. "") config.genode.init.children;
  155. in pkgs.writeText "${name'}.dhall" ''
  156. let Genode = env:DHALL_GENODE
  157. let Init = Genode.Init
  158. in Init.Child.flat
  159. Init.Child.Attributes::{
  160. , binary = "${binary}/bin/${binary.pname}"
  161. , resources = Init.Resources::{
  162. , caps = 128
  163. , ram = Genode.units.MiB 4
  164. }
  165. , routes = [
  166. , Init.ServiceRoute.parent "IO_MEM"
  167. , Init.ServiceRoute.parent "Platform"
  168. ]
  169. , config = Init.Config::{
  170. , attributes = toMap { verbose = "true" }
  171. , policies =
  172. [ ${toString policy} ]
  173. : List Init.Config.Policy.Type
  174. }
  175. }
  176. '';
  177. };
  178. }) config.networking.interfaces;
  179. sockets = mapAttrs' (name: interface:
  180. let name' = name + ".sockets";
  181. in {
  182. name = name';
  183. value = if interface.genode.stack == null then
  184. null
  185. else {
  186. inputs = with pkgs.genodePackages;
  187. {
  188. lwip = [ vfs_lwip ];
  189. lxip = [ vfs_lxip ];
  190. }.${interface.genode.stack};
  191. routeToNics = [ name ];
  192. configFile = let
  193. binary = "${pkgs.genodePackages.vfs}/bin/vfs";
  194. ram = {
  195. lwip = 16;
  196. lxip = 32;
  197. }.${interface.genode.stack};
  198. settings = with builtins;
  199. lib.optionals (interface.ipv4.addresses != [ ])
  200. (let addr = head interface.ipv4.addresses;
  201. in [
  202. {
  203. name = "ip_addr";
  204. value = addr.address;
  205. }
  206. {
  207. name = "netmask";
  208. value = if addr.prefixLength == 24 then
  209. "255.255.255.0"
  210. else
  211. throw "missing prefix to netmask conversion";
  212. }
  213. ]) ++ lib.optional (interface.ipv4.routes != [ ])
  214. (let route = head interface.ipv4.routes;
  215. in {
  216. name = "gateway";
  217. value = route.address;
  218. }) ++ lib.optional (interface.useDHCP != null) {
  219. name = "dhcp";
  220. value = if interface.useDHCP then "true" else "false";
  221. };
  222. settingsMap = builtins.concatStringsSep ", " (map
  223. ({ name, value }:
  224. ''{ mapKey = "${name}", mapValue = "${value}" }'') settings);
  225. in pkgs.writeText "${name'}.dhall" ''
  226. let Genode = env:DHALL_GENODE
  227. let Init = Genode.Init
  228. in Init.Child.flat
  229. Init.Child.Attributes::{
  230. , binary = "${binary}"
  231. , provides = [ "File_system" ]
  232. , resources = Init.Resources::{
  233. , caps = 128
  234. , ram = Genode.units.MiB ${toString ram}
  235. }
  236. , config = Init.Config::{
  237. , policies =
  238. [ Init.Config.Policy::{
  239. , service = "File_system"
  240. , label = Init.LabelSelector.suffix "${name'}"
  241. , attributes = toMap { root = "/", writeable="yes" }
  242. }
  243. ]
  244. , content =
  245. let VFS = Genode.VFS
  246. in [ VFS.vfs
  247. [ VFS.leafAttrs
  248. "${interface.genode.stack}"
  249. ([ ${settingsMap} ] : Genode.Prelude.Map.Type Text Text)
  250. ]
  251. ]
  252. }
  253. }
  254. '';
  255. };
  256. }) config.networking.interfaces;
  257. in lib.filterAttrs (n: v: v != null) (nics // sockets);
  258. genode.core.children.ahci_drv = {
  259. inputs = [ pkgs.genodePackages.ahci_drv ];
  260. configFile = pkgs.writeText "ahci_drv.dhall" ''
  261. let Genode = env:DHALL_GENODE
  262. let Init = Genode.Init
  263. in Init.Child.flat
  264. Init.Child.Attributes::{
  265. , binary = "ahci_drv"
  266. , resources = Init.Resources::{
  267. , caps = 400
  268. , ram = Genode.units.MiB 10
  269. , constrainPhys = True
  270. }
  271. , romReports = [ { local = "ports", route = "ahci_ports" } ]
  272. , routes =
  273. [ Init.ServiceRoute.parent "IRQ"
  274. , Init.ServiceRoute.parent "IO_MEM"
  275. , Init.ServiceRoute.parent "IO_PORT"
  276. ]
  277. , config = Init.Config::{
  278. , policies =
  279. [ Init.Config.Policy::{
  280. , service = "Block"
  281. , label = Init.LabelSelector.prefix "part_block"
  282. , attributes = toMap { device = "0", writeable = "yes" }
  283. }
  284. ]
  285. }
  286. }
  287. '';
  288. };
  289. genode.core.children.acpi_drv = {
  290. inputs = [ pkgs.genodePackages.acpi_drv ];
  291. configFile = pkgs.writeText "acpi_drv.dhall" ''
  292. let Genode = env:DHALL_GENODE
  293. let Init = Genode.Init
  294. let label = λ(_ : Text) → { local = _, route = _ }
  295. in Init.Child.flat
  296. Init.Child.Attributes::{
  297. , binary = "acpi_drv"
  298. , resources = Init.Resources::{
  299. , caps = 400
  300. , ram = Genode.units.MiB 4
  301. , constrainPhys = True
  302. }
  303. , romReports = [ label "acpi", label "smbios_table" ]
  304. , routes =
  305. [ Init.ServiceRoute.parent "IRQ"
  306. , Init.ServiceRoute.parent "IO_MEM"
  307. , Init.ServiceRoute.parent "IO_PORT"
  308. ]
  309. }
  310. '';
  311. };
  312. genode.core.children.platform_drv = {
  313. inputs = [ pkgs.genodePackages.platform_drv ];
  314. configFile = let
  315. policies =
  316. map (policy: ", ${policy}") config.hardware.genode.platform.policies;
  317. in pkgs.writeText "platform_drv.dhall" ''
  318. let Genode = env:DHALL_GENODE
  319. let Init = Genode.Init
  320. in Init.Child.flat
  321. Init.Child.Attributes::{
  322. , binary = "platform_drv"
  323. , resources = Init.Resources::{
  324. , caps = 800
  325. , ram = Genode.units.MiB 4
  326. , constrainPhys = True
  327. }
  328. , reportRoms = let label = "acpi" in [ { local = label, route = label } ]
  329. , routes =
  330. [ Init.ServiceRoute.parent "IRQ"
  331. , Init.ServiceRoute.parent "IO_MEM"
  332. , Init.ServiceRoute.parent "IO_PORT"
  333. ]
  334. , config = Init.Config::{
  335. , policies = [ ${
  336. toString policies
  337. } ] : List Init.Config.Policy.Type
  338. }
  339. }
  340. '';
  341. };
  342. genode.core.children.usb_drv = let
  343. cfg = config.hardware.genode.usb;
  344. toYesNo = b: if b then "yes" else "no";
  345. in mkIf cfg.enable {
  346. inputs = [ pkgs.genodePackages.usb_drv ];
  347. configFile = builtins.toFile "usb_drv.dhall" ''
  348. let Genode = env:DHALL_GENODE
  349. let XML = Genode.Prelude.XML
  350. let Init = Genode.Init
  351. in Init.Child.flat
  352. Init.Child.Attributes::{
  353. , binary = "usb_drv"
  354. , resources = Init.Resources::{ caps = 256, ram = Genode.units.MiB 12 }
  355. , romReports = let local = "devices" in [ { local, route = local } ]
  356. , routes = [ Init.ServiceRoute.parent "IO_MEM" ]
  357. , config =
  358. let storagePolicy =
  359. Init.Config.Policy::{
  360. , service = "Usb"
  361. , label = Init.LabelSelector.prefix "usb_block_drv"
  362. , attributes = toMap { class = "8" }
  363. , diag = Some True
  364. }
  365. in Init.Config::{
  366. , attributes = toMap
  367. { ehci = "${toYesNo cfg.ehciSupport}"
  368. , ohci = "${toYesNo cfg.ohciSupport}"
  369. , uhci = "${toYesNo cfg.uhciSupport}"
  370. , xhci = "${toYesNo cfg.xhciSupport}"
  371. }
  372. , content =
  373. [ XML.element
  374. { name = "raw"
  375. , attributes = XML.emptyAttributes
  376. , content =
  377. [ XML.leaf
  378. { name = "report"
  379. , attributes = toMap { devices = "yes" }
  380. }
  381. , Init.Config.Policy.toXML storagePolicy
  382. ]
  383. }
  384. ]
  385. , policies = [ storagePolicy ] : List Init.Config.Policy.Type
  386. }
  387. }
  388. '';
  389. };
  390. genode.core.children.usb_block_drv =
  391. mkIf config.hardware.genode.usb.storage.enable {
  392. inputs = [ pkgs.genodePackages.usb_block_drv ];
  393. configFile = builtins.toFile "usb_block_drv.dhall" ''
  394. let Genode = env:DHALL_GENODE
  395. let XML = Genode.Prelude.XML
  396. let Init = Genode.Init
  397. in Init.Child.flat
  398. Init.Child.Attributes::{
  399. , binary = "usb_block_drv"
  400. , resources = Init.Resources::{ caps = 256, ram = Genode.units.MiB 4 }
  401. , config = Init.Config::{
  402. , attributes = toMap { writeable = "yes" }
  403. , policies =
  404. [ Init.Config.Policy::{
  405. , service = "Block"
  406. , label = Init.LabelSelector.prefix "part_block"
  407. }
  408. ]
  409. }
  410. }
  411. '';
  412. };
  413. genode.core.children.fb_drv =
  414. mkIf config.hardware.genode.framebuffer.enable {
  415. inputs = with pkgs.genodePackages;
  416. {
  417. "boot" = [ boot_fb_drv ];
  418. "vesa" = [ vesa_drv ];
  419. }.${config.hardware.genode.framebuffer.driver};
  420. configFile = let
  421. binary = with pkgs.genodePackages;
  422. {
  423. "boot" = "boot_fb_drv";
  424. "vesa" = "vesa_fb_drv";
  425. }.${config.hardware.genode.framebuffer.driver};
  426. in builtins.toFile "fb_drv.dhall" ''
  427. let Genode = env:DHALL_GENODE
  428. let XML = Genode.Prelude.XML
  429. let Init = Genode.Init
  430. in Init.Child.flat
  431. Init.Child.Attributes::{
  432. , binary = "${binary}"
  433. , resources = Init.Resources::{ caps = 256, ram = Genode.units.MiB 32 }
  434. , routes =
  435. [ Init.ServiceRoute.parent "IO_MEM"
  436. , Init.ServiceRoute.parent "IO_PORT"
  437. ]
  438. }
  439. '';
  440. };
  441. virtualisation.useBootLoader = config.genode.boot.storeBackend != "tarball";
  442. virtualisation.qemu.options = lib.optional config.hardware.genode.usb.enable
  443. (lib.optional (pkgs.stdenv.isi686 || pkgs.stdenv.isx86_64) "-usb"
  444. ++ lib.optional (pkgs.stdenv.isAarch32 || pkgs.stdenv.isAarch64)
  445. "-device usb-ehci,id=usb0");
  446. };
  447. }