Genode Packages collection https://git.sr.ht/~ehmry/genodepkgs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hardware.nix 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 { type = types.enum [ "ipxe" "virtio" ]; };
  10. stack = mkOption {
  11. type = with types; nullOr (enum [ "lwip" "lxip" ]);
  12. default = "lwip";
  13. };
  14. };
  15. }));
  16. };
  17. hardware.genode.platform.policies = lib.mkOption {
  18. type = with types; listOf path;
  19. default = [ ];
  20. description = ''
  21. List of policies to append to the Genode platform driver.
  22. Type is Init.Config.Policy.Type.
  23. '';
  24. };
  25. hardware.usb.genode.enable = lib.mkEnableOption "USB driver";
  26. hardware.usb.genode.storage.enable =
  27. lib.mkEnableOption "USB mass storage driver";
  28. };
  29. config = {
  30. assertions = with builtins;
  31. let
  32. addrCheck = name: interface: {
  33. assertion = lessThan (length interface.ipv4.addresses) 2;
  34. message = "Genode interfaces do not support multihoming.";
  35. };
  36. routeCheck = name: interface: {
  37. assertion = lessThan (length interface.ipv4.routes) 2;
  38. message = "Genode interfaces do not support multiple routes.";
  39. };
  40. in lib.mapAttrsToList addrCheck config.networking.interfaces
  41. ++ lib.mapAttrsToList routeCheck config.networking.interfaces;
  42. hardware.usb.genode.storage.enable = config.genode.boot.storeBackend
  43. == "usb";
  44. hardware.usb.genode.enable = config.hardware.usb.genode.storage.enable;
  45. hardware.genode.platform.policies = lib.lists.imap0 (i: name:
  46. builtins.toFile (name + ".platform-policy.dhall") ''
  47. let Genode = env:DHALL_GENODE
  48. in Genode.Init.Config.Policy::{
  49. , service = "Platform"
  50. , label = Genode.Init.LabelSelector.prefix "nixos -> ${name}.driver"
  51. , content =
  52. [ Genode.Prelude.XML.leaf
  53. { name = "pci"
  54. , attributes = toMap {
  55. , class = "ETHERNET"
  56. , index = "${toString i}"
  57. }
  58. }
  59. ]
  60. }
  61. '') (builtins.attrNames config.networking.interfaces)
  62. ++ lib.optional config.hardware.usb.genode.enable
  63. (builtins.toFile ("usb.platform-policy.dhall") ''
  64. let Genode = env:DHALL_GENODE
  65. in Genode.Init.Config.Policy::{
  66. , service = "Platform"
  67. , label = Genode.Init.LabelSelector.prefix "usb_drv"
  68. , content =
  69. [ Genode.Prelude.XML.leaf
  70. { name = "pci", attributes = toMap { class = "USB" } }
  71. ]
  72. }
  73. '');
  74. genode.core.basePackages = with pkgs.genodePackages;
  75. [ acpi_drv platform_drv ]
  76. ++ lib.optional config.hardware.usb.genode.enable
  77. pkgs.genodePackages.usb_drv;
  78. genode.init.children = let
  79. nics = mapAttrs' (name: interface:
  80. let name' = name + ".driver";
  81. in {
  82. name = name';
  83. value = let
  84. binary = with pkgs.genodePackages;
  85. {
  86. ipxe = ipxe_nic_drv;
  87. virtio = virtio_nic_drv;
  88. }.${interface.genode.driver};
  89. in {
  90. inputs = [ binary ];
  91. configFile = let
  92. policies = if interface.genode.stack == null then
  93. "[] : List Init.Config.Policy.Type"
  94. else ''
  95. [ Init.Config.Policy::{
  96. , service = "Nic"
  97. , label = Init.LabelSelector.prefix "${name}.sockets"
  98. }
  99. ]
  100. '';
  101. in pkgs.writeText "${name'}.dhall" ''
  102. let Genode = env:DHALL_GENODE
  103. let Init = Genode.Init
  104. in Init.Child.flat
  105. Init.Child.Attributes::{
  106. , binary = "${binary.pname}"
  107. , provides = [ "Nic" ]
  108. , resources = Init.Resources::{
  109. , caps = 128
  110. , ram = Genode.units.MiB 4
  111. }
  112. , routes = [
  113. , Init.ServiceRoute.parent "IO_MEM"
  114. , Init.ServiceRoute.parent "Platform"
  115. ]
  116. , config = Init.Config::{
  117. , attributes = toMap { verbose = "true" }
  118. , policies = ${policies}
  119. }
  120. }
  121. '';
  122. };
  123. }) config.networking.interfaces;
  124. sockets = mapAttrs' (name: interface:
  125. let name' = name + ".sockets";
  126. in {
  127. name = name';
  128. value = if interface.genode.stack == null then
  129. null
  130. else {
  131. inputs = with pkgs.genodePackages;
  132. {
  133. lwip = [ vfs_lwip ];
  134. lxip = [ vfs_lxip ];
  135. }.${interface.genode.stack};
  136. configFile = let
  137. binary = "${pkgs.genodePackages.vfs}/bin/vfs";
  138. ram = {
  139. lwip = 16;
  140. lxip = 32;
  141. }.${interface.genode.stack};
  142. settings = with builtins;
  143. lib.optionals (interface.ipv4.addresses != [ ])
  144. (let addr = head interface.ipv4.addresses;
  145. in [
  146. {
  147. name = "ip_addr";
  148. value = addr.address;
  149. }
  150. {
  151. name = "netmask";
  152. value = if addr.prefixLength == 24 then
  153. "255.255.255.0"
  154. else
  155. throw "missing prefix to netmask conversion";
  156. }
  157. ]) ++ lib.optional (interface.ipv4.routes != [ ])
  158. (let route = head interface.ipv4.routes;
  159. in {
  160. name = "gateway";
  161. value = route.address;
  162. }) ++ lib.optional (interface.useDHCP != null) {
  163. name = "dhcp";
  164. value = if interface.useDHCP then "true" else "false";
  165. };
  166. settingsMap = builtins.concatStringsSep ", " (map
  167. ({ name, value }:
  168. ''{ mapKey = "${name}", mapValue = "${value}" }'') settings);
  169. in pkgs.writeText "${name'}.dhall" ''
  170. let Genode = env:DHALL_GENODE
  171. let Init = Genode.Init
  172. in Init.Child.flat
  173. Init.Child.Attributes::{
  174. , binary = "${binary}"
  175. , provides = [ "File_system" ]
  176. , resources = Init.Resources::{
  177. , caps = 128
  178. , ram = Genode.units.MiB ${toString ram}
  179. }
  180. , config = Init.Config::{
  181. , policies =
  182. [ Init.Config.Policy::{
  183. , service = "File_system"
  184. , label = Init.LabelSelector.suffix "${name'}"
  185. , attributes = toMap { root = "/", writeable="yes" }
  186. }
  187. ]
  188. , content =
  189. let VFS = Genode.VFS
  190. in [ VFS.vfs
  191. [ VFS.leafAttrs
  192. "${interface.genode.stack}"
  193. ([ ${settingsMap} ] : Genode.Prelude.Map.Type Text Text)
  194. ]
  195. ]
  196. }
  197. }
  198. '';
  199. };
  200. }) config.networking.interfaces;
  201. in lib.filterAttrs (n: v: v != null) (nics // sockets);
  202. genode.core.children = {
  203. acpi_drv = {
  204. coreROMs = [ "acpi_drv" ];
  205. configFile = pkgs.writeText "acpi_drv.dhall" ''
  206. let Genode = env:DHALL_GENODE
  207. let Init = Genode.Init
  208. let label = λ(_ : Text) → { local = _, route = _ }
  209. in Init.Child.flat
  210. Init.Child.Attributes::{
  211. , binary = "acpi_drv"
  212. , resources = Init.Resources::{
  213. , caps = 400
  214. , ram = Genode.units.MiB 4
  215. , constrainPhys = True
  216. }
  217. , romReports = [ label "acpi", label "smbios_table" ]
  218. , routes =
  219. [ Init.ServiceRoute.parent "IRQ"
  220. , Init.ServiceRoute.parent "IO_MEM"
  221. , Init.ServiceRoute.parent "IO_PORT"
  222. ]
  223. }
  224. '';
  225. };
  226. platform_drv = {
  227. coreROMs = [ "platform_drv" ];
  228. configFile = let
  229. policies = map (policy: ", ${policy}")
  230. config.hardware.genode.platform.policies;
  231. in pkgs.writeText "platform_drv.dhall" ''
  232. let Genode = env:DHALL_GENODE
  233. let Init = Genode.Init
  234. in Init.Child.flat
  235. Init.Child.Attributes::{
  236. , binary = "platform_drv"
  237. , resources = Init.Resources::{
  238. , caps = 800
  239. , ram = Genode.units.MiB 4
  240. , constrainPhys = True
  241. }
  242. , reportRoms = let label = "acpi" in [ { local = label, route = label } ]
  243. , provides = [ "Platform" ]
  244. , routes =
  245. [ Init.ServiceRoute.parent "IRQ"
  246. , Init.ServiceRoute.parent "IO_MEM"
  247. , Init.ServiceRoute.parent "IO_PORT"
  248. ]
  249. , config = Init.Config::{
  250. , policies = [ ${toString policies} ]
  251. }
  252. }
  253. '';
  254. };
  255. } // (if config.hardware.usb.genode.enable then {
  256. usb_drv = {
  257. coreROMs = [ "usb_drv" ];
  258. configFile = builtins.toFile "usb_drv.dhall" ''
  259. let Genode = env:DHALL_GENODE
  260. let XML = Genode.Prelude.XML
  261. let Init = Genode.Init
  262. let storageEnable = ${
  263. if config.hardware.usb.genode.storage.enable then
  264. "True"
  265. else
  266. "False"
  267. }
  268. in Init.Child.flat
  269. Init.Child.Attributes::{
  270. , binary = "usb_drv"
  271. , provides = [ "Block", "Usb" ]
  272. , resources = Init.Resources::{ caps = 256, ram = Genode.units.MiB 12 }
  273. , routes = [ Init.ServiceRoute.parent "IO_MEM" ]
  274. , config = Init.Config::{
  275. , attributes = toMap { uhci = "yes", ehci = "yes", xhci = "yes" }
  276. , content =
  277. if storageEnable
  278. then [ XML.leaf
  279. { name = "storage", attributes = XML.emptyAttributes }
  280. ]
  281. else [] : List XML.Type
  282. , policies =
  283. if storageEnable
  284. then [ Init.Config.Policy::{
  285. , service = "Block"
  286. , label = Init.LabelSelector.prefix "store_fs"
  287. }
  288. ]
  289. else [] : List Init.Config.Policy.Type
  290. }
  291. }
  292. '';
  293. };
  294. } else
  295. { });
  296. };
  297. }