interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tests / test_acpi_dev.c

415 lines · 15151 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* test_acpi_dev.c -- core/acpi_dev.c (static DSDT/SSDT device scan) and the
18 * IORT named-component lookup in core/acpi.c, on synthetic tables built
19 * here in the encodings the real firmware uses:
20 * - DGX Spark form (measured from the Spark DSDT under Ubuntu, 2026-10-05):
21 * Scope(\_SB) { Device(USB0) { Name(_HID, "NVDA8000") Name(_CID,
22 * "PNP0D15") ... Method(_CRS) { Name(RBUF, Buffer() {Memory32Fixed ...})
23 * Return(RBUF) } } }
24 * - QEMU virt form: Device(COM0) { Name(_HID, "ARMH0011") Name(_CRS,
25 * ResourceTemplate() { Memory32Fixed(..., 0x09000000, 0x1000) ... }) }
26 * - EisaId _HID/_CID, QWord memory, nested devices.
27 * Negative: bad signature, bad checksum, truncated table, short header,
28 * device package past the end, descriptor past the buffer, bad names. The
29 * Makefile target acpi-dev-mutant builds this test against code mutants
30 * (signature / checksum / length check removed) and requires it to FAIL. */
31#include <stdint.h>
32#include <string.h>
33
34#include "acpi.h"
35#include "acpi_dev.h"
36#include "ck_test.h"
37
38static uint8_t buf[8192];
39static uint32_t at;
40
41static void b1(uint8_t v) { buf[at++] = v; }
42static void b4(uint32_t v) { for (int i = 0; i < 4; i++) b1((uint8_t)(v >> (8 * i))); }
43static void b8(uint64_t v) { for (int i = 0; i < 8; i++) b1((uint8_t)(v >> (8 * i))); }
44static void bs(const char *s) { while (*s) b1((uint8_t)*s++); }
45
46/* Package length covering `content` bytes after the encoding. Writes the
47 * encoding at buf[pos] (space reserved by pk_open). */
48static uint32_t pk_open(void) { uint32_t p = at; at += 2; return p; } /* always 2-byte form */
49static void pk_close(uint32_t p)
50{
51 uint32_t total = at - p; /* includes the 2 encoding bytes */
52 buf[p] = (uint8_t)(0x40u | (total & 0x0fu));
53 buf[p + 1] = (uint8_t)(total >> 4);
54}
55
56static uint32_t hdr(const char *sig)
57{
58 uint32_t start = at;
59 bs(sig);
60 b4(0); /* length, fixed by finish */
61 b1(2); b1(0); bs("AIENOS"); bs("CKTEST01"); b4(1); bs("CKT "); b4(1);
62 return start;
63}
64static uint32_t finish(uint32_t start)
65{
66 uint32_t len = at - start;
67 buf[start + 4] = (uint8_t)len;
68 buf[start + 5] = (uint8_t)(len >> 8);
69 buf[start + 6] = (uint8_t)(len >> 16);
70 buf[start + 7] = (uint8_t)(len >> 24);
71 buf[start + 9] = 0;
72 uint8_t s = 0;
73 for (uint32_t i = 0; i < len; i++) s = (uint8_t)(s + buf[start + i]);
74 buf[start + 9] = (uint8_t)(0u - s);
75 return len;
76}
77
78static void name_str(const char *seg, const char *val) { b1(0x08); bs(seg); b1(0x0d); bs(val); b1(0); }
79static void name_dword(const char *seg, uint32_t v) { b1(0x08); bs(seg); b1(0x0c); b4(v); }
80static void mem32fixed(uint32_t base, uint32_t len) { b1(0x86); b1(9); b1(0); b1(1); b4(base); b4(len); }
81static void irq(uint32_t intid) { b1(0x89); b1(6); b1(0); b1(0x01); b1(1); b4(intid); }
82static void end_tag(void) { b1(0x79); b1(0); }
83
84/* Buffer(size) { bytes } where the bytes are emitted by fn. */
85static void buffer_of(void (*fn)(void))
86{
87 b1(0x11);
88 uint32_t p = pk_open();
89 b1(0x0b); /* WordPrefix size, fixed below */
90 uint32_t szpos = at;
91 at += 2;
92 uint32_t d0 = at;
93 fn();
94 uint32_t n = at - d0;
95 buf[szpos] = (uint8_t)n;
96 buf[szpos + 1] = (uint8_t)(n >> 8);
97 pk_close(p);
98}
99
100static void spark_rbuf(void)
101{
102 mem32fixed(0x1db60000u, 0x7800u);
103 mem32fixed(0x1db68000u, 0x100u);
104 mem32fixed(0x1db70000u, 0x4000u);
105 irq(0xaf);
106 end_tag();
107}
108static void qemu_uart(void)
109{
110 mem32fixed(0x09000000u, 0x1000u);
111 irq(0x21);
112 end_tag();
113}
114static void qword_mem(void)
115{
116 b1(0x8a); b1(43); b1(0);
117 b1(0); b1(0x0c); b1(0x01); /* memory, flags */
118 b8(0); b8(0x4000000000ull); b8(0x400000ffffull); b8(0); b8(0x10000);
119 end_tag();
120}
121static void desc_past_end(void)
122{
123 b1(0x86); b1(40); b1(0); b1(1); b4(0x12340000u); /* claims 40 bytes, has 5 */
124}
125
126/* Device(NAME) { fn() } */
127static void device(const char *seg, void (*fn)(void))
128{
129 b1(0x5b); b1(0x82);
130 uint32_t p = pk_open();
131 bs(seg);
132 fn();
133 pk_close(p);
134}
135
136static void usb0_body(void)
137{
138 name_str("_HID", "NVDA8000");
139 name_str("_CID", "PNP0D15");
140 b1(0x08); bs("_UID"); b1(0x00);
141 /* Method(_STA) { If (UP0E == 0) { Return (0) } Return (0x0F) } (shape only) */
142 b1(0x14); { uint32_t p = pk_open(); bs("_STA"); b1(0x00); b1(0xa4); b1(0x0a); b1(0x0f); pk_close(p); }
143 /* Method(_CRS, 0, Serialized) { Name(RBUF, Buffer(){...}) Return(RBUF) } */
144 b1(0x14);
145 uint32_t p = pk_open();
146 bs("_CRS");
147 b1(0x08);
148 b1(0x08); bs("RBUF");
149 buffer_of(spark_rbuf);
150 b1(0xa4); bs("RBUF");
151 pk_close(p);
152}
153static void usb4_body(void)
154{
155 name_str("_HID", "NVDA8001");
156 name_str("_CID", "PNP0D15");
157 b1(0x14);
158 uint32_t p = pk_open();
159 bs("_CRS");
160 b1(0x08);
161 b1(0x08); bs("RBUF");
162 buffer_of(spark_rbuf);
163 b1(0xa4); bs("RBUF");
164 pk_close(p);
165}
166static void com0_body(void)
167{
168 name_str("_HID", "ARMH0011");
169 b1(0x08); bs("_UID"); b1(0x00);
170 b1(0x08); bs("_CRS");
171 buffer_of(qemu_uart);
172}
173static void xhc_eisa_body(void)
174{
175 name_dword("_HID", 0x100dd041u); /* EisaId("PNP0D10") */
176 b1(0x08); bs("_CRS");
177 buffer_of(qword_mem);
178}
179static void pci0_body(void)
180{
181 name_dword("_HID", 0x080ad041u); /* EisaId("PNP0A08") */
182 name_dword("_CID", 0x030ad041u); /* EisaId("PNP0A03") */
183}
184static void child_body(void) { name_str("_HID", "NVDA8001"); }
185static void parent_body(void)
186{
187 device("CHL0", child_body);
188 name_str("_HID", "PNP0C02"); /* after the nested device: not read for PAR0 */
189}
190static void bad_crs_body(void)
191{
192 name_str("_HID", "PNP0D15");
193 b1(0x08); bs("_CRS");
194 buffer_of(desc_past_end);
195}
196static void sb_scope(void)
197{
198 device("USB0", usb0_body);
199 device("USB4", usb4_body);
200 device("COM0", com0_body);
201 device("XHC1", xhc_eisa_body);
202 device("PCI0", pci0_body);
203 device("PAR0", parent_body);
204 device("BADC", bad_crs_body);
205}
206
207static uint32_t build_dsdt(const char *sig)
208{
209 at = 0;
210 uint32_t s = hdr(sig);
211 b1(0x10); /* Scope(\_SB_) */
212 uint32_t p = pk_open();
213 b1(0x5c); bs("_SB_");
214 sb_scope();
215 pk_close(p);
216 return finish(s);
217}
218
219static const struct ck_acpi_device *by_name(const struct ck_acpi_device *d, int n, const char *name)
220{
221 for (int i = 0; i < n; i++)
222 if (!strcmp(d[i].path, name)) return &d[i];
223 return 0;
224}
225
226static void test_scan(void)
227{
228 uint32_t len = build_dsdt("DSDT");
229 CHECK(ck_aml_table_ok(buf, len) == CK_AML_OK);
230 struct ck_acpi_device d[16];
231 unsigned seen = 0;
232 int n = ck_aml_scan(buf, len, 0, 0, d, 16, &seen);
233 CHECK(n == 8 && seen == 8); /* USB0 USB4 COM0 XHC1 PCI0 PAR0 CHL0 BADC */
234
235 const struct ck_acpi_device *u = by_name(d, n, "USB0");
236 CHECK(u && !strcmp(u->hid, "NVDA8000") && !strcmp(u->cid, "PNP0D15"));
237 CHECK(u && u->mmio_base == 0x1db60000u && u->mmio_len == 0x7800u); /* first range only */
238 const struct ck_acpi_device *c = by_name(d, n, "COM0");
239 CHECK(c && !strcmp(c->hid, "ARMH0011") && c->cid[0] == 0 && c->mmio_base == 0x09000000u && c->mmio_len == 0x1000u);
240 const struct ck_acpi_device *x = by_name(d, n, "XHC1");
241 CHECK(x && !strcmp(x->hid, "PNP0D10") && x->mmio_base == 0x4000000000ull && x->mmio_len == 0x10000u);
242 const struct ck_acpi_device *p = by_name(d, n, "PCI0");
243 CHECK(p && !strcmp(p->hid, "PNP0A08") && !strcmp(p->cid, "PNP0A03") && p->mmio_len == 0);
244 const struct ck_acpi_device *par = by_name(d, n, "PAR0");
245 const struct ck_acpi_device *chl = by_name(d, n, "CHL0");
246 CHECK(par && par->hid[0] == 0); /* the child's _HID is not the parent's */
247 CHECK(chl && !strcmp(chl->hid, "NVDA8001"));
248 const struct ck_acpi_device *bc = by_name(d, n, "BADC");
249 CHECK(bc && bc->mmio_base == 0 && bc->mmio_len == 0); /* descriptor past its buffer: no range */
250
251 /* The xHCI id filter (the fence's list). */
252 static const char *const ids[] = { "NVDA8000", "NVDA8001", "PNP0D10", "PNP0D15" };
253 n = ck_aml_scan(buf, len, ids, 4, d, 16, &seen);
254 CHECK(n == 5); /* USB0 USB4 XHC1 CHL0 BADC */
255 CHECK(by_name(d, n, "USB0") && by_name(d, n, "USB4") && by_name(d, n, "XHC1") && by_name(d, n, "CHL0") &&
256 by_name(d, n, "BADC") && !by_name(d, n, "COM0"));
257 /* max smaller than the matches: count returned, only max stored. */
258 memset(d, 0x5a, sizeof d);
259 CHECK(ck_aml_scan(buf, len, ids, 4, d, 2, 0) == 5);
260 CHECK(d[2].path[0] == 0x5a);
261 static const char *const ctl[] = { "ARMH0011" };
262 n = ck_aml_scan(buf, len, ctl, 1, d, 16, 0);
263 CHECK(n == 1 && !strcmp(d[0].path, "COM0"));
264 static const char *const none[] = { "QEMU0002" };
265 CHECK(ck_aml_scan(buf, len, none, 1, d, 16, 0) == 0);
266
267 /* SSDT is accepted too. */
268 len = build_dsdt("SSDT");
269 CHECK(ck_aml_table_ok(buf, len) == CK_AML_OK && ck_aml_scan(buf, len, ids, 4, d, 16, 0) == 5);
270}
271
272static void test_refusals(void)
273{
274 struct ck_acpi_device d[16];
275 unsigned seen = 7;
276 /* bad signature */
277 uint32_t len = build_dsdt("XSDT");
278 CHECK(ck_aml_table_ok(buf, len) == CK_AML_E_SIG);
279 CHECK(ck_aml_scan(buf, len, 0, 0, d, 16, &seen) == CK_AML_E_SIG && seen == 0);
280 /* bad checksum (one byte flipped after the checksum was fixed) */
281 len = build_dsdt("DSDT");
282 buf[len - 1] ^= 0x01;
283 CHECK(ck_aml_table_ok(buf, len) == CK_AML_E_SUM);
284 CHECK(ck_aml_scan(buf, len, 0, 0, d, 16, 0) == CK_AML_E_SUM);
285 /* truncated: the header claims more bytes than are available */
286 len = build_dsdt("DSDT");
287 CHECK(ck_aml_table_ok(buf, len - 1) == CK_AML_E_LEN);
288 CHECK(ck_aml_scan(buf, len - 40, 0, 0, d, 16, 0) == CK_AML_E_LEN);
289 CHECK(ck_aml_table_ok(buf, 35) == CK_AML_E_LEN);
290 /* header length below 36 */
291 len = build_dsdt("DSDT");
292 buf[4] = 20, buf[5] = buf[6] = buf[7] = 0;
293 CHECK(ck_aml_table_ok(buf, len) == CK_AML_E_LEN);
294 CHECK(ck_aml_table_ok(0, 100) == CK_AML_E_LEN);
295
296 /* A device whose package runs past the table end is skipped, not followed. */
297 at = 0;
298 uint32_t s = hdr("SSDT");
299 device("GOOD", child_body);
300 b1(0x5b); b1(0x82); b1(0x4f); b1(0x40); bs("LONG"); /* 2-byte PkgLength 0x40f, far past the end */
301 name_str("_HID", "NVDA8000");
302 len = finish(s);
303 CHECK(ck_aml_table_ok(buf, len) == CK_AML_OK);
304 int n = ck_aml_scan(buf, len, 0, 0, d, 16, &seen);
305 CHECK(n == 1 && seen == 1 && !strcmp(d[0].path, "GOOD"));
306 /* A bad name segment (lower case) is not a device. */
307 at = 0;
308 s = hdr("SSDT");
309 b1(0x5b); b1(0x82); b1(0x0d); bs("usb0"); name_str("_HID", "NVDA");
310 len = finish(s);
311 CHECK(ck_aml_scan(buf, len, 0, 0, d, 16, &seen) == 0 && seen == 0);
312}
313
314static void test_helpers(void)
315{
316 char id[8];
317 ck_aml_eisaid(0x150dd041u, id);
318 CHECK(!strcmp(id, "PNP0D15"));
319 ck_aml_eisaid(0x080ad041u, id);
320 CHECK(!strcmp(id, "PNP0A08"));
321 CHECK(ck_aml_path_eq("\\_SB.USB0", "_SB_.USB0"));
322 CHECK(ck_aml_path_eq("\\_SB_.USB0", "\\_SB_.USB0"));
323 CHECK(!ck_aml_path_eq("\\_SB_.USB0", "\\_SB_.USB1"));
324 CHECK(!ck_aml_path_eq("\\_SB_.USB0", "\\_SB_"));
325 CHECK(!ck_aml_path_eq("\\_SB_.USB00", "\\_SB_.USB0"));
326 CHECK(ck_aml_last_seg_eq("USB0", "\\_SB_.USB0"));
327 CHECK(ck_aml_last_seg_eq("\\_SB_.PCI0.XHC", "XHC_"));
328 CHECK(!ck_aml_last_seg_eq("USB0", "\\_SB_.USB1"));
329 CHECK(!ck_aml_last_seg_eq("", "USB0"));
330}
331
332/* ---- IORT named components ---- */
333static uint32_t iort_smmu(uint64_t base)
334{
335 uint32_t off = at;
336 b1(4); b1(68); b1(0); b1(4); b4(0); b4(0); b4(0); /* type, len, rev, id, no mappings */
337 b8(base);
338 while (at < off + 68) b1(0);
339 return off;
340}
341static void iort_named(const char *name, uint32_t sid, uint32_t target, int single, int with_map)
342{
343 uint32_t off = at;
344 uint32_t nlen = 29 + 32; /* fixed fields + name area */
345 uint32_t total = nlen + (with_map ? 20u : 0u);
346 b1(1); b1((uint8_t)total); b1((uint8_t)(total >> 8)); b1(4); b4(0);
347 b4(with_map ? 1u : 0u); b4(with_map ? nlen : 0u);
348 b4(0); b8(0x0000000000000001ull); b1(48);
349 uint32_t nm = at;
350 bs(name);
351 while (at < nm + 32) b1(0);
352 if (with_map) {
353 b4(0); b4(0); b4(sid); b4(target); b4(single ? 1u : 0u);
354 }
355 (void)off;
356}
357static uint32_t build_iort(int dup)
358{
359 at = 0;
360 uint32_t s = at;
361 bs("IORT"); b4(0); b1(3); b1(0); bs("AIENOS"); bs("CKTEST01"); b4(1); bs("CKT "); b4(1);
362 uint32_t count_pos = at;
363 b4(0); b4(48); b4(0);
364 uint32_t a = iort_smmu(0x13800000ull); /* the first SMMUv3: the one the kernel drives */
365 uint32_t b = iort_smmu(0x14900000ull);
366 iort_named("\\_SB_.USB0", 0x30, a, 1, 1);
367 iort_named("\\_SB_.USB1", 0x31, b, 1, 1);
368 iort_named("\\_SB_.USB2", 0, 0, 0, 0);
369 iort_named("\\_SB_.USB3", 0x33, a, 0, 1); /* not flagged single, but one id (count 0) */
370 uint32_t nodes = 6;
371 if (dup) {
372 iort_named("\\_SB_.PCI1.USB0", 0x40, a, 1, 1);
373 nodes++;
374 }
375 buf[count_pos] = (uint8_t)nodes;
376 return finish(s);
377}
378
379static void test_iort_named(void)
380{
381 build_iort(0);
382 struct ck_iort_named r;
383 struct ck_iort_smmu first;
384 CHECK(ck_iort_parse(buf, &first) == 1 && first.base == 0x13800000ull);
385 CHECK(ck_iort_named(buf, "USB0", &r) == 1);
386 CHECK(r.stream_id == 0x30 && r.target_off == first.node_off && r.target_base == 0x13800000ull);
387 CHECK(!strcmp(r.name, "\\_SB_.USB0"));
388 CHECK(ck_iort_named(buf, "\\_SB.USB1", &r) == 1);
389 CHECK(r.stream_id == 0x31 && r.target_off != first.node_off && r.target_base == 0x14900000ull);
390 CHECK(ck_iort_named(buf, "USB2", &r) == 1 && r.target_off == 0); /* no mapping: no stream */
391 CHECK(ck_iort_named(buf, "USB3", &r) == 1 && r.stream_id == 0x33 && r.target_off == first.node_off);
392 CHECK(ck_iort_named(buf, "USB9", &r) == 0);
393 CHECK(ck_iort_named(buf, "TOOLONG5", &r) == -1);
394 build_iort(1);
395 CHECK(ck_iort_named(buf, "USB0", &r) == -2); /* two named components end in USB0 */
396 /* malformed: a mapping that references outside the table */
397 uint32_t len = build_iort(0);
398 for (uint32_t i = 48; i + 20 <= len; i++) /* find USB0's mapping (sid 0x30) */
399 if (buf[i] == 0x30 && buf[i + 1] == 0 && buf[i + 4] != 0 && buf[i - 4] == 0) {
400 buf[i + 4] = 0xff, buf[i + 5] = 0xff, buf[i + 6] = 0, buf[i + 7] = 0;
401 break;
402 }
403 CHECK(ck_iort_named(buf, "USB0", &r) == -1);
404 memcpy(buf, "XXXX", 4);
405 CHECK(ck_iort_named(buf, "USB0", &r) == -1);
406}
407
408int main(void)
409{
410 test_scan();
411 test_refusals();
412 test_helpers();
413 test_iort_named();
414 return ck_t_verdict("test_acpi_dev");
415}