~ehmry/genodepkgs

ref: staging genodepkgs/nixos-modules/hardware.nix -rw-r--r-- 8.6 KiB
7f414b17Emery Hemingway WiP! lighttpd 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
{ config, pkgs, lib, ... }:

with lib;

{
  options = {

    networking.interfaces = lib.mkOption {
      type = with types;
        attrsOf (submodule ({ ... }: {
          options.genode = {

            driver = mkOption { type = types.enum [ "ipxe" "virtio" ]; };

            stack = mkOption {
              type = with types; nullOr (enum [ "lwip" "lxip" ]);
              default = "lwip";
            };

          };
        }));
    };

    hardware.genode.platform.policies = lib.mkOption {
      type = with types; listOf path;
      default = [ ];
      description = ''
        List of policies to append to the Genode platform driver.
        Type is Init.Config.Policy.Type.
      '';
    };

  };

  config = {

    assertions = with builtins;
      let
        addrCheck = name: interface: {
          assertion = lessThan (length interface.ipv4.addresses) 2;
          message = "Genode interfaces do not support multihoming.";
        };
        routeCheck = name: interface: {
          assertion = lessThan (length interface.ipv4.routes) 2;
          message = "Genode interfaces do not support multiple routes.";
        };
      in lib.mapAttrsToList addrCheck config.networking.interfaces
      ++ lib.mapAttrsToList routeCheck config.networking.interfaces;

    hardware.genode.platform.policies = lib.lists.imap0 (i: name:
      builtins.toFile (name + ".platform-policy.dhall") ''
        let Genode = env:DHALL_GENODE

        in  Genode.Init.Config.Policy::{
            , service = "Platform"
            , label = Genode.Init.LabelSelector.prefix "${name}.driver"
            , content =
              [ Genode.Prelude.XML.leaf
                  { name = "pci"
                  , attributes = toMap {
                    , class = "ETHERNET"
                    , index = "${toString i}"
                    }
                  }
              ]
            }
      '') (builtins.attrNames config.networking.interfaces);

    genode.core.basePackages = with pkgs.genodePackages; [
      acpi_drv
      platform_drv
    ];

    genode.init.children = let

      nics = mapAttrs' (name: interface:
        let name' = name + ".driver";
        in {
          name = name';
          value = let
            binary = with pkgs.genodePackages;
              {
                ipxe = ipxe_nic_drv;
                virtio = virtio_nic_drv;
              }.${interface.genode.driver};
          in {
            inputs = [ binary ];
            configFile = let
              policies = if interface.genode.stack == null then
                "[] : List Init.Config.Policy.Type"
              else ''
                [ Init.Config.Policy::{
                  , service = "Nic"
                  , label = Init.LabelSelector.prefix "${name}.sockets"
                  }
                ]
              '';
            in pkgs.writeText "${name'}.dhall" ''
              let Genode = env:DHALL_GENODE

              let Init = Genode.Init

              in  Init.Child.flat
                    Init.Child.Attributes::{
                    , binary = "${binary.pname}"
                    , provides = [ "Nic" ]
                    , resources = Init.Resources::{
                      , caps = 128
                      , ram = Genode.units.MiB 4
                      }
                    , routes = [ Init.ServiceRoute.parent "IO_MEM" ]
                    , config = Init.Config::{
                      , attributes = toMap { verbose = "true" }
                      , policies = ${policies}
                      }
                    }
            '';
          };
        }) config.networking.interfaces;

      sockets = mapAttrs' (name: interface:
        let name' = name + ".sockets";
        in {
          name = name';
          value = if interface.genode.stack == null then
            null
          else {
            inputs = with pkgs.genodePackages;
              {
                lwip = [ vfs_lwip ];
                lxip = [ vfs_lxip ];
              }.${interface.genode.stack};
            configFile = let
              binary = "${pkgs.genodePackages.vfs}/bin/vfs";
              ram = {
                lwip = 16;
                lxip = 32;
              }.${interface.genode.stack};
              settings = with builtins;
                lib.optionals (interface.ipv4.addresses != [ ])
                (let addr = head interface.ipv4.addresses;
                in [
                  {
                    name = "ip_addr";
                    value = addr.address;
                  }
                  {
                    name = "netmask";
                    value = if addr.prefixLength == 24 then
                      "255.255.255.0"
                    else
                      throw "missing prefix to netmask conversion";
                  }
                ]) ++ lib.optional (interface.ipv4.routes != [ ])
                (let route = head interface.ipv4.routes;
                in {
                  name = "gateway";
                  value = route.address;
                }) ++ lib.optional (interface.useDHCP != null) {
                  name = "dhcp";
                  value = if interface.useDHCP then "true" else "false";
                };
              settingsMap = builtins.concatStringsSep ", " (map
                ({ name, value }:
                  ''{ mapKey = "${name}", mapValue = "${value}" }'') settings);
            in pkgs.writeText "${name'}.dhall" ''
              let Genode = env:DHALL_GENODE

              let Init = Genode.Init

              in  Init.Child.flat
                    Init.Child.Attributes::{
                    , binary = "${binary}"
                    , provides = [ "File_system" ]
                    , resources = Init.Resources::{
                      , caps = 128
                      , ram = Genode.units.MiB ${toString ram}
                      }
                    , config = Init.Config::{
                      , policies =
                        [ Init.Config.Policy::{
                          , service = "File_system"
                          , label = Init.LabelSelector.suffix "${name'}"
                          , attributes = toMap { root = "/", writeable="yes" }
                          }
                        ]
                      , content =
                          let VFS = Genode.VFS

                          in  [ VFS.vfs
                                  [ VFS.leafAttrs
                                      "${interface.genode.stack}"
                                      ([ ${settingsMap} ] : Genode.Prelude.Map.Type Text Text)
                                  ]
                              ]
                      }
                    }
            '';
          };
        }) config.networking.interfaces;

    in nics // (lib.filterAttrs (n: v: v != null) sockets) // {

      acpi_drv = {
        coreROMs = [ "acpi_drv" ];
        configFile = pkgs.writeText "acpi_drv.dhall" ''
          let Genode = env:DHALL_GENODE

          let Init = Genode.Init

          let label = λ(_ : Text) → { local = _, route = _ }

          in  Init.Child.flat
                Init.Child.Attributes::{
                , binary = "acpi_drv"
                , resources = Init.Resources::{
                  , caps = 400
                  , ram = Genode.units.MiB 4
                  , constrainPhys = True
                  }
                , romReports = [ label "acpi", label "smbios_table" ]
                , routes =
                  [ Init.ServiceRoute.parent "IRQ"
                  , Init.ServiceRoute.parent "IO_MEM"
                  , Init.ServiceRoute.parent "IO_PORT"
                  ]
                }
        '';
      };

      platform_drv = {
        coreROMs = [ "platform_drv" ];
        configFile = let
          policies = map (policy: ", ${policy}")
            config.hardware.genode.platform.policies;
        in pkgs.writeText "platform_drv.dhall" ''
          let Genode = env:DHALL_GENODE

          let Init = Genode.Init

          in  Init.Child.flat
                Init.Child.Attributes::{
                , binary = "platform_drv"
                , resources = Init.Resources::{
                  , caps = 800
                  , ram = Genode.units.MiB 4
                  , constrainPhys = True
                  }
                , reportRoms = let label = "acpi" in [ { local = label, route = label } ]
                , provides = [ "Platform" ]
                , routes =
                  [ Init.ServiceRoute.parent "IRQ"
                  , Init.ServiceRoute.parent "IO_MEM"
                  , Init.ServiceRoute.parent "IO_PORT"
                  ]
                , config = Init.Config::{
                  , policies = [ ${toString policies} ]
                  }
                }
        '';
      };

    };

  };

}