kerf-multikernel 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kerf/dtc/extractor.py ADDED
@@ -0,0 +1,362 @@
1
+ # Copyright 2025 Multikernel Technologies, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ DTB generation from device tree models.
17
+ """
18
+
19
+ import libfdt
20
+ from ..models import GlobalDeviceTree, Instance
21
+ from .cells import pack_cpu_ids
22
+
23
+
24
+ class InstanceExtractor:
25
+ """Generates device tree blobs (DTB) from device tree models."""
26
+
27
+ def __init__(self):
28
+ self.fdt = None
29
+
30
+ def _create_minimal_fdt(self) -> bytes:
31
+ """Create a minimal valid FDT structure."""
32
+ # Create a minimal FDT with proper structure
33
+ import struct
34
+
35
+ # Calculate sizes
36
+ header_size = 40 # FDT header is 40 bytes
37
+ mem_rsv_size = 16 # Two 8-byte entries (terminator)
38
+ struct_size = 16 # FDT_BEGIN_NODE + FDT_END_NODE + FDT_END
39
+ strings_size = 0 # No strings for minimal FDT
40
+
41
+ # Calculate offsets
42
+ off_mem_rsvmap = header_size
43
+ off_dt_struct = off_mem_rsvmap + mem_rsv_size
44
+ off_dt_strings = off_dt_struct + struct_size
45
+ totalsize = off_dt_strings + strings_size
46
+
47
+ # Create FDT data
48
+ fdt_data = bytearray(totalsize)
49
+
50
+ # FDT header (40 bytes)
51
+ header = struct.pack(
52
+ ">IIIIIIIIII",
53
+ 0xD00DFEED, # magic
54
+ totalsize, # totalsize
55
+ off_dt_struct, # off_dt_struct
56
+ off_dt_strings, # off_dt_strings
57
+ off_mem_rsvmap, # off_mem_rsvmap
58
+ 17, # version
59
+ 16, # last_comp_version
60
+ 0, # boot_cpuid_phys
61
+ strings_size, # size_dt_strings
62
+ struct_size,
63
+ ) # size_dt_struct
64
+
65
+ fdt_data[: len(header)] = header
66
+
67
+ # Memory reservation block (16 bytes - two 8-byte entries of zeros)
68
+ fdt_data[off_mem_rsvmap : off_mem_rsvmap + 16] = b"\x00" * 16
69
+
70
+ # Structure block
71
+ struct_data = b"\x00\x00\x00\x01" # FDT_BEGIN_NODE
72
+ struct_data += b"\x00" # Root node name (empty)
73
+ struct_data += b"\x00\x00\x00\x02" # FDT_END_NODE
74
+ struct_data += b"\x00\x00\x00\x09" # FDT_END
75
+
76
+ fdt_data[off_dt_struct : off_dt_struct + len(struct_data)] = struct_data
77
+
78
+ return bytes(fdt_data)
79
+
80
+ def generate_global_dtb(self, tree: GlobalDeviceTree) -> bytes:
81
+ """Generate global DTB from tree model."""
82
+ # For production use, we'll create a comprehensive DTB that includes all the parsed data
83
+ # This creates a proper device tree blob with hardware inventory, instances, and device references
84
+
85
+ # Create a larger FDT structure to accommodate all the data
86
+ fdt_data = self._create_comprehensive_fdt(tree)
87
+
88
+ return fdt_data
89
+
90
+ def _create_comprehensive_fdt(self, tree: GlobalDeviceTree) -> bytes:
91
+ """Create a comprehensive FDT with all parsed data using libfdt FdtSw."""
92
+ # Use libfdt's FdtSw (FDT source writer) to properly build the DTB
93
+ # This ensures correct structure, size calculations, and string handling
94
+ # FdtSw automatically resizes the buffer as needed
95
+
96
+ fdt_sw = libfdt.FdtSw()
97
+ fdt_sw.finish_reservemap()
98
+
99
+ fdt_sw.begin_node("")
100
+ fdt_sw.property_string("compatible", "linux,multikernel-host")
101
+
102
+ fdt_sw.begin_node("resources")
103
+ self._add_cpu_properties_sw(fdt_sw, tree.hardware.cpus)
104
+ self._add_memory_properties_sw(fdt_sw, tree.hardware.memory)
105
+
106
+ if tree.hardware.devices:
107
+ self._add_devices_section_sw(fdt_sw, tree.hardware.devices)
108
+
109
+ fdt_sw.end_node() # End resources
110
+
111
+ if tree.instances:
112
+ self._add_instances_section_sw(fdt_sw, tree.instances)
113
+
114
+ if tree.device_references:
115
+ self._add_device_references_sw(fdt_sw, tree.device_references)
116
+
117
+ fdt_sw.end_node()
118
+
119
+ dtb = fdt_sw.as_fdt()
120
+ dtb.pack()
121
+ return dtb.as_bytearray()
122
+
123
+ def _add_cpu_properties_sw(self, fdt_sw, cpus):
124
+ """Add CPU properties directly to resources node."""
125
+ fdt_sw.property("cpus", pack_cpu_ids(cpus.available))
126
+
127
+ def _add_memory_properties_sw(self, fdt_sw, memory):
128
+ """Add memory properties directly to resources node."""
129
+ fdt_sw.property_u64("memory-base", memory.memory_pool_base)
130
+ fdt_sw.property_u64("memory-bytes", memory.memory_pool_bytes)
131
+
132
+ def _add_devices_section_sw(self, fdt_sw, devices):
133
+ """Add devices section using FdtSw."""
134
+ fdt_sw.begin_node("devices")
135
+
136
+ for name, device_info in devices.items():
137
+ fdt_sw.begin_node(name)
138
+
139
+ if device_info.device_type:
140
+ fdt_sw.property_string("device-type", device_info.device_type)
141
+
142
+ if device_info.compatible:
143
+ fdt_sw.property_string("compatible", device_info.compatible)
144
+
145
+ if device_info.device_name:
146
+ fdt_sw.property_string("device-name", device_info.device_name)
147
+
148
+ if device_info.pci_id:
149
+ fdt_sw.property_string("pci-id", device_info.pci_id)
150
+
151
+ if device_info.vendor_id is not None:
152
+ fdt_sw.property_u32("vendor-id", device_info.vendor_id)
153
+
154
+ if device_info.device_id is not None:
155
+ fdt_sw.property_u32("device-id", device_info.device_id)
156
+
157
+ if device_info.sriov_vfs is not None:
158
+ fdt_sw.property_u32("sriov-vfs", device_info.sriov_vfs)
159
+
160
+ if device_info.host_reserved_vf is not None:
161
+ fdt_sw.property_u32("host-reserved-vf", device_info.host_reserved_vf)
162
+
163
+ if device_info.available_vfs:
164
+ import struct
165
+
166
+ vfs_data = struct.pack(
167
+ ">" + "I" * len(device_info.available_vfs), *device_info.available_vfs
168
+ )
169
+ fdt_sw.property("available-vfs", vfs_data)
170
+
171
+ if device_info.namespaces is not None:
172
+ fdt_sw.property_u32("namespaces", device_info.namespaces)
173
+
174
+ if device_info.host_reserved_ns is not None:
175
+ fdt_sw.property_u32("host-reserved-ns", device_info.host_reserved_ns)
176
+
177
+ if device_info.available_ns:
178
+ import struct
179
+
180
+ ns_data = struct.pack(
181
+ ">" + "I" * len(device_info.available_ns), *device_info.available_ns
182
+ )
183
+ fdt_sw.property("available-ns", ns_data)
184
+
185
+ fdt_sw.end_node()
186
+
187
+ fdt_sw.end_node()
188
+
189
+ def _add_instances_section_sw(self, fdt_sw, instances):
190
+ """Add instances section using FdtSw."""
191
+ fdt_sw.begin_node("instances")
192
+
193
+ for name, instance in instances.items():
194
+ fdt_sw.begin_node(name)
195
+ if instance.id is None:
196
+ raise ValueError(f"Instance '{name}' missing ID in baseline (should not happen)")
197
+ fdt_sw.property_u32("id", instance.id)
198
+
199
+ fdt_sw.begin_node("resources")
200
+
201
+ fdt_sw.property("cpus", pack_cpu_ids(instance.resources.cpus))
202
+
203
+ fdt_sw.property_u64("memory-base", instance.resources.memory_base)
204
+ fdt_sw.property_u64("memory-bytes", instance.resources.memory_bytes)
205
+
206
+ if instance.resources.devices:
207
+ stringlist_data = b'\0'.join(d.encode('utf-8') for d in instance.resources.devices) + b'\0'
208
+ fdt_sw.property("device-names", stringlist_data)
209
+
210
+ if instance.resources.uring:
211
+ fdt_sw.begin_node("uring")
212
+ if instance.resources.uring_sq_entries:
213
+ fdt_sw.property_u32("sq-entries", instance.resources.uring_sq_entries)
214
+ if instance.resources.uring_cq_entries:
215
+ fdt_sw.property_u32("cq-entries", instance.resources.uring_cq_entries)
216
+ if instance.resources.uring_shim_pages:
217
+ fdt_sw.property_u32("shim-data-pages", instance.resources.uring_shim_pages)
218
+ fdt_sw.end_node()
219
+
220
+ fdt_sw.end_node() # End resources
221
+ fdt_sw.end_node() # End instance
222
+
223
+ fdt_sw.end_node() # End instances
224
+
225
+ def _add_device_references_sw(self, fdt_sw, device_references):
226
+ """Add device references using FdtSw."""
227
+ for name, device_ref in device_references.items():
228
+ fdt_sw.begin_node(name)
229
+
230
+ if isinstance(device_ref, dict):
231
+ if "parent" in device_ref and device_ref["parent"]:
232
+ fdt_sw.property_string("parent", device_ref["parent"])
233
+ if "vf_id" in device_ref and device_ref["vf_id"] is not None:
234
+ fdt_sw.property_u32("vf-id", device_ref["vf_id"])
235
+ if "namespace_id" in device_ref and device_ref["namespace_id"] is not None:
236
+ fdt_sw.property_u32("namespace-id", device_ref["namespace_id"])
237
+ else:
238
+ if hasattr(device_ref, "parent") and device_ref.parent:
239
+ fdt_sw.property_string("parent", device_ref.parent)
240
+ if hasattr(device_ref, "vf_id") and device_ref.vf_id is not None:
241
+ fdt_sw.property_u32("vf-id", device_ref.vf_id)
242
+ if hasattr(device_ref, "namespace_id") and device_ref.namespace_id is not None:
243
+ fdt_sw.property_u32("namespace-id", device_ref.namespace_id)
244
+
245
+ fdt_sw.end_node()
246
+
247
+ def _add_resources_section(self, parent_offset: int, tree: GlobalDeviceTree):
248
+ """Add resources section to DTB."""
249
+ resources_offset = self.fdt.add_subnode(parent_offset, "resources")
250
+
251
+ # Add CPU information
252
+ self._add_cpu_section(resources_offset, tree.hardware.cpus)
253
+
254
+ # Add memory information
255
+ self._add_memory_section(resources_offset, tree.hardware.memory)
256
+
257
+ # Add device information
258
+ self._add_devices_section(resources_offset, tree.hardware.devices)
259
+
260
+ def _add_cpu_section(self, parent_offset: int, cpus):
261
+ """Add CPU section to DTB."""
262
+ cpus_offset = self.fdt.add_subnode(parent_offset, "cpus")
263
+ self.fdt.setprop_u32(cpus_offset, "total", cpus.total)
264
+
265
+ self.fdt.setprop(cpus_offset, "host-reserved", pack_cpu_ids(cpus.host_reserved))
266
+ self.fdt.setprop(cpus_offset, "available", pack_cpu_ids(cpus.available))
267
+
268
+ def _add_memory_section(self, parent_offset: int, memory):
269
+ """Add memory section to DTB."""
270
+ memory_offset = self.fdt.add_subnode(parent_offset, "memory")
271
+ self.fdt.setprop_u64(memory_offset, "total-bytes", memory.total_bytes)
272
+ self.fdt.setprop_u64(memory_offset, "host-reserved-bytes", memory.host_reserved_bytes)
273
+ self.fdt.setprop_u64(memory_offset, "memory-pool-base", memory.memory_pool_base)
274
+ self.fdt.setprop_u64(memory_offset, "memory-pool-bytes", memory.memory_pool_bytes)
275
+
276
+ def _add_devices_section(self, parent_offset: int, devices):
277
+ """Add devices section to DTB."""
278
+ devices_offset = self.fdt.add_subnode(parent_offset, "devices")
279
+
280
+ for name, device_info in devices.items():
281
+ device_offset = self.fdt.add_subnode(devices_offset, name)
282
+ self.fdt.setprop_str(device_offset, "compatible", device_info.compatible)
283
+
284
+ if device_info.pci_id:
285
+ self.fdt.setprop_str(device_offset, "pci-id", device_info.pci_id)
286
+
287
+ if device_info.sriov_vfs is not None:
288
+ self.fdt.setprop_u32(device_offset, "sriov-vfs", device_info.sriov_vfs)
289
+
290
+ if device_info.host_reserved_vf is not None:
291
+ self.fdt.setprop_u32(
292
+ device_offset, "host-reserved-vf", device_info.host_reserved_vf
293
+ )
294
+
295
+ if device_info.available_vfs:
296
+ import struct
297
+
298
+ vfs_data = struct.pack(
299
+ ">" + "I" * len(device_info.available_vfs), *device_info.available_vfs
300
+ )
301
+ self.fdt.setprop(device_offset, "available-vfs", vfs_data)
302
+
303
+ if device_info.namespaces is not None:
304
+ self.fdt.setprop_u32(device_offset, "namespaces", device_info.namespaces)
305
+
306
+ if device_info.host_reserved_ns is not None:
307
+ self.fdt.setprop_u32(
308
+ device_offset, "host-reserved-ns", device_info.host_reserved_ns
309
+ )
310
+
311
+ if device_info.available_ns:
312
+ import struct
313
+
314
+ ns_data = struct.pack(
315
+ ">" + "I" * len(device_info.available_ns), *device_info.available_ns
316
+ )
317
+ self.fdt.setprop(device_offset, "available-ns", ns_data)
318
+
319
+ def _add_instances_section(self, parent_offset: int, tree: GlobalDeviceTree):
320
+ """Add instances section to DTB."""
321
+ instances_offset = self.fdt.add_subnode(parent_offset, "instances")
322
+
323
+ for name, instance in tree.instances.items():
324
+ instance_offset = self.fdt.add_subnode(instances_offset, name)
325
+ # Instance ID should always be present in baseline (instances are already created)
326
+ if instance.id is None:
327
+ raise ValueError(f"Instance '{name}' missing ID in baseline (should not happen)")
328
+ self.fdt.setprop_u32(instance_offset, "id", instance.id)
329
+
330
+ # Add resources section
331
+ self._add_instance_resources(instance_offset, instance)
332
+
333
+ def _add_instance_resources(self, parent_offset: int, instance: Instance):
334
+ """Add instance resources section."""
335
+ resources_offset = self.fdt.add_subnode(parent_offset, "resources")
336
+
337
+ self.fdt.setprop(resources_offset, "cpus", pack_cpu_ids(instance.resources.cpus))
338
+
339
+ self.fdt.setprop_u64(resources_offset, "memory-base", instance.resources.memory_base)
340
+ self.fdt.setprop_u64(resources_offset, "memory-bytes", instance.resources.memory_bytes)
341
+
342
+ if instance.resources.devices:
343
+ stringlist_data = b'\0'.join(d.encode('utf-8') for d in instance.resources.devices) + b'\0'
344
+ self.fdt.setprop(resources_offset, "device-names", stringlist_data)
345
+
346
+ def _add_device_references(self, parent_offset: int, tree: GlobalDeviceTree):
347
+ """Add device reference nodes."""
348
+
349
+ for name, device_ref in tree.device_references.items():
350
+ device_ref_offset = self.fdt.add_subnode(parent_offset, name)
351
+
352
+ # Add parent phandle reference
353
+ if hasattr(device_ref, "parent") and device_ref.parent:
354
+ # This would need proper phandle handling in a full implementation
355
+ self.fdt.setprop_str(device_ref_offset, "parent", device_ref.parent)
356
+
357
+ # Add device-specific properties
358
+ if hasattr(device_ref, "vf_id") and device_ref.vf_id is not None:
359
+ self.fdt.setprop_u32(device_ref_offset, "vf-id", device_ref.vf_id)
360
+
361
+ if hasattr(device_ref, "namespace_id") and device_ref.namespace_id is not None:
362
+ self.fdt.setprop_u32(device_ref_offset, "namespace-id", device_ref.namespace_id)