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.c

254 lines · 9919 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.c -- RSDP/XSDT walk, checksum and length checks, MADT, SPCR and
18 * FADT parsing on crafted tables shaped like QEMU virt's. */
19#include <stdint.h>
20#include <string.h>
21#include "ck_test.h"
22#include "acpi.h"
23#include "frames.h"
24
25static void w32(uint8_t *p, uint32_t v) { memcpy(p, &v, 4); }
26static void w64(uint8_t *p, uint64_t v) { memcpy(p, &v, 8); }
27
28static void sum_fix(uint8_t *t, size_t len, size_t at)
29{
30 t[at] = 0;
31 uint8_t s = 0;
32 for (size_t i = 0; i < len; i++)
33 s = (uint8_t)(s + t[i]);
34 t[at] = (uint8_t)-s;
35}
36
37static void sdt(uint8_t *t, const char *sig, uint32_t len)
38{
39 memcpy(t, sig, 4);
40 w32(t + 4, len);
41 t[8] = 2;
42 memcpy(t + 10, "AIENOS", 6);
43}
44
45static uint8_t rsdp[36], xsdt[36 + 8 * 5], madt[256], spcr[80], fadt[276], badspcr[80], other[40];
46
47/* Frame reservation of every ACPI table: one page-aligned arena holding an
48 * RSDP that straddles a page boundary, the XSDT, a 5000-byte table spanning
49 * three pages and a header-only table. */
50static uint8_t arena[16 * 4096] __attribute__((aligned(4096)));
51static void reserve_cb(uint64_t lo, uint64_t hi, void *ctx)
52{
53 ck_frames_reserve(ctx, lo, hi);
54}
55
56static void test_spans(void)
57{
58 uint8_t *rs = arena + 1 * 4096 + 4080, *xs = arena + 4 * 4096 + 100;
59 uint8_t *big = arena + 7 * 4096 + 4000, *hdr = arena + 12 * 4096;
60 sdt(big, "OEM2", 5000);
61 sum_fix(big, 5000, 9);
62 sdt(hdr, "OEM3", 36);
63 sum_fix(hdr, 36, 9);
64 sdt(xs, "XSDT", 36 + 16);
65 w64(xs + 36, (uint64_t)(uintptr_t)big);
66 w64(xs + 44, (uint64_t)(uintptr_t)hdr);
67 sum_fix(xs, 36 + 16, 9);
68 memcpy(rs, "RSD PTR ", 8);
69 rs[15] = 2;
70 w32(rs + 20, 36);
71 w64(rs + 24, (uint64_t)(uintptr_t)xs);
72 sum_fix(rs, 20, 8);
73 sum_fix(rs, 36, 32);
74
75 uint64_t base = (uint64_t)(uintptr_t)arena;
76 struct ck_frames f;
77 ck_frames_init(&f);
78 CHECK(ck_frames_add(&f, base, base + sizeof arena) == 0);
79 CHECK(ck_acpi_spans((uint64_t)(uintptr_t)rs, reserve_cb, &f) == 4);
80 /* Exhaust the allocator: exactly the pages holding no ACPI byte remain. */
81 int got[16] = { 0 }, n = 0;
82 uint64_t a;
83 while (ck_frames_alloc(&f, 1, 4096, &a) == 0 && n < 32) {
84 CHECK(a >= base && a < base + sizeof arena);
85 if (a >= base && a < base + sizeof arena)
86 got[(a - base) / 4096]++;
87 n++;
88 }
89 static const int want[16] = { 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1 };
90 for (int i = 0; i < 16; i++)
91 CHECK(got[i] == want[i]);
92 CHECK(n == 9);
93 /* An invalid RSDP reports nothing. */
94 rs[8] ^= 1;
95 ck_frames_init(&f);
96 CHECK(ck_frames_add(&f, base, base + sizeof arena) == 0);
97 CHECK(ck_acpi_spans((uint64_t)(uintptr_t)rs, reserve_cb, &f) == -1);
98 CHECK(ck_frames_free_bytes(&f) == sizeof arena);
99}
100
101static int seen;
102static void each_cb(uint64_t t, uint32_t len, void *ctx)
103{
104 (void)t;
105 (void)len;
106 (*(int *)ctx)++;
107 seen++;
108}
109
110int main(void)
111{
112 /* MADT: header + 8-byte MADT fields, 2 GICC (one disabled), GICD v3,
113 * GICR range. */
114 uint32_t at = 44;
115 for (int i = 0; i < 2; i++) {
116 uint8_t *e = madt + at;
117 e[0] = 0x0b;
118 e[1] = 80;
119 w32(e + 12, i == 0 ? 1 : 0);
120 w64(e + 32, 0x08010000 + i * 0x1000);
121 w64(e + 60, 0x080a0000 + i * 0x20000);
122 w64(e + 68, (uint64_t)i);
123 at += 80;
124 }
125 madt[at] = 0x0c; madt[at + 1] = 24; w64(madt + at + 8, 0x08000000); madt[at + 20] = 3; at += 24;
126 madt[at] = 0x0e; madt[at + 1] = 16; w64(madt + at + 4, 0x080a0000); w32(madt + at + 12, 0xf60000); at += 16;
127 sdt(madt, "APIC", at);
128 sum_fix(madt, at, 9);
129
130 sdt(spcr, "SPCR", 80);
131 spcr[36] = 0x03; spcr[40] = 0; spcr[41] = 32; spcr[43] = 3; w64(spcr + 44, 0x09000000);
132 sum_fix(spcr, 80, 9);
133 memcpy(badspcr, spcr, 80);
134 badspcr[50] ^= 0x55; /* checksum now wrong */
135
136 sdt(fadt, "FACP", 276);
137 fadt[129] = 3; /* PSCI compliant, use HVC */
138 sum_fix(fadt, 276, 9);
139 sdt(other, "OEM1", 40);
140 sum_fix(other, 40, 9);
141
142 uint8_t *tabs[5] = { badspcr, madt, spcr, fadt, other };
143 sdt(xsdt, "XSDT", sizeof xsdt);
144 for (int i = 0; i < 5; i++)
145 w64(xsdt + 36 + 8 * i, (uint64_t)(uintptr_t)tabs[i]);
146 sum_fix(xsdt, sizeof xsdt, 9);
147
148 memcpy(rsdp, "RSD PTR ", 8);
149 memcpy(rsdp + 9, "AIENOS", 6);
150 rsdp[15] = 2;
151 w32(rsdp + 16, 0x1234);
152 w32(rsdp + 20, 36);
153 w64(rsdp + 24, (uint64_t)(uintptr_t)xsdt);
154 sum_fix(rsdp, 20, 8);
155 sum_fix(rsdp, 36, 32);
156 uint64_t r = (uint64_t)(uintptr_t)rsdp;
157
158 CHECK(ck_acpi_sum_ok(rsdp, 20) && ck_acpi_sum_ok(rsdp, 36));
159 int x = -1;
160 CHECK(ck_acpi_root(r, &x) == (uint64_t)(uintptr_t)xsdt && x == 1);
161 CHECK(ck_acpi_lookup(r, "APIC") == madt);
162 CHECK(ck_acpi_lookup(r, "SPCR") == spcr); /* bad-checksum copy skipped */
163 CHECK(ck_acpi_lookup(r, "FACP") == fadt);
164 CHECK(ck_acpi_lookup(r, "MCFG") == NULL);
165 int n = 0;
166 CHECK(ck_acpi_each(r, each_cb, &n) == 5 && n == 5 && seen == 5);
167
168 struct ck_madt_gic g;
169 CHECK(ck_madt_parse(madt, &g) == 0);
170 CHECK(g.gicd == 0x08000000 && g.gic_version == 3 && g.gicr == 0x080a0000 && g.gicr_len == 0xf60000);
171 CHECK(g.gicc_count == 1 && g.gicc_gicr == 0x080a0000);
172 CHECK(g.gicc_base == 0x08010000 && ck_madt_gic_kind(&g) == 3);
173 { /* GICv2 MADT shapes (Pi 5 GIC-400, QEMU gic-version=2) */
174 struct ck_madt_gic v = { 0 };
175 v.gicd = 0x107fff9000ull; v.gicc_base = 0x107fffa000ull; v.gic_version = 2;
176 CHECK(ck_madt_gic_kind(&v) == 2);
177 v.gic_version = 0; /* discover: no redistributor anywhere, GICC base given */
178 CHECK(ck_madt_gic_kind(&v) == 2);
179 v.gicc_gicr = 0x080a0000; /* a redistributor named: GICv3 */
180 CHECK(ck_madt_gic_kind(&v) == 3);
181 v.gicc_gicr = 0; v.gicc_base = 0; v.gic_version = 2; /* v2 without a CPU interface */
182 CHECK(ck_madt_gic_kind(&v) == 0);
183 v.gic_version = 1; /* GICv1: unsupported */
184 CHECK(ck_madt_gic_kind(&v) == 0);
185 }
186
187 struct ck_spcr s;
188 CHECK(ck_spcr_parse(spcr, &s) == 0 && s.interface_type == 3 && s.space == 0 &&
189 s.bit_width == 32 && s.access_size == 3 && s.base == 0x09000000);
190 uint16_t fl = 0;
191 CHECK(ck_fadt_arm_boot_arch(fadt, &fl) == 0 && fl == 3);
192
193 /* Malformed inputs. */
194 CHECK(ck_spcr_parse(madt, &s) == -1);
195 CHECK(ck_fadt_arm_boot_arch(spcr, &fl) == -1);
196 uint8_t m2[256];
197 memcpy(m2, madt, sizeof m2);
198 m2[44 + 1] = 0; /* zero-length entry would loop forever */
199 CHECK(ck_madt_parse(m2, &g) == -1);
200 memcpy(m2, madt, sizeof m2);
201 w32(m2 + 4, 44 + 80 + 10); /* table ends inside an entry */
202 CHECK(ck_madt_parse(m2, &g) == -1);
203 rsdp[8] ^= 1; /* RSDP checksum broken */
204 CHECK(ck_acpi_root(r, &x) == 0 && ck_acpi_lookup(r, "APIC") == NULL && ck_acpi_each(r, each_cb, &n) == -1);
205 rsdp[8] ^= 1;
206 xsdt[40] ^= 1; /* XSDT checksum broken */
207 CHECK(ck_acpi_lookup(r, "APIC") == NULL);
208 xsdt[40] ^= 1;
209 /* Revision 0 RSDP: RSDT address. */
210 rsdp[15] = 0;
211 sum_fix(rsdp, 20, 8);
212 CHECK(ck_acpi_root(r, &x) == 0x1234 && x == 0);
213 CHECK(ck_acpi_root(0, &x) == 0);
214 /* CPU topology (acpi.rs madt_cpu_topology) and placement (thread.rs). */
215 struct ck_cpu_topology tp;
216 uint8_t pc;
217 uint32_t pn;
218 CHECK(ck_madt_cpu_topology(madt, &tp) == 0);
219 CHECK(tp.cores == 1 && tp.distinct_classes == 1 && tp.class_id[0] == 0 &&
220 tp.class_count[0] == 1 && tp.unknown_class == 0 && tp.has_first_mpidr &&
221 tp.first_mpidr == 0);
222 CHECK(ck_place_task(&tp, 1, &pc, &pn) == 0 && pc == 0 && pn == 0);
223 memcpy(m2, madt, sizeof m2);
224 w32(m2 + 44 + 80 + 12, 8); /* second GICC online-capable, class 1 */
225 m2[44 + 80 + 76] = 1;
226 m2[44 + 76] = 2; /* first GICC class 2: sorted after class 1 */
227 CHECK(ck_madt_cpu_topology(m2, &tp) == 0);
228 CHECK(tp.cores == 2 && tp.distinct_classes == 2 && tp.class_id[0] == 1 && tp.class_id[1] == 2);
229 m2[44 + 1] = 0;
230 CHECK(ck_madt_cpu_topology(m2, &tp) == -1);
231 memset(&tp, 0, sizeof tp);
232 CHECK(ck_place_task(&tp, 0, &pc, &pn) == -1); /* empty topology is unplaced */
233 tp.distinct_classes = 2;
234 tp.class_id[0] = 0; tp.class_count[0] = 2;
235 tp.class_id[1] = 1; tp.class_count[1] = 2;
236 static const uint8_t wc[8] = { 0, 0, 1, 1, 0, 0, 1, 1 };
237 static const uint32_t wn[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
238 for (uint32_t i = 0; i < 8; i++)
239 CHECK(ck_place_task(&tp, i, &pc, &pn) == 0 && pc == wc[i] && pn == wn[i]);
240 /* MPIDR list for the SMP bring-up (core/smp.c). */
241 uint64_t mp[4];
242 unsigned mn = 99;
243 CHECK(ck_madt_mpidrs(madt, mp, 4, &mn) == 0 && mn == 1 && mp[0] == 0); /* disabled GICC skipped */
244 memcpy(m2, madt, sizeof m2);
245 w32(m2 + 44 + 80 + 12, 8); /* second GICC online-capable */
246 w64(m2 + 44 + 80 + 68, 0xffffffffff000102ull); /* RES0 bits masked off */
247 CHECK(ck_madt_mpidrs(m2, mp, 4, &mn) == 0 && mn == 2 && mp[0] == 0 && mp[1] == 0xff00000102ull);
248 CHECK(ck_madt_mpidrs(m2, mp, 1, &mn) == -2 && mn == 2); /* more cores than slots */
249 m2[44 + 80 + 1] = 72; /* GICC too short to carry the MPIDR */
250 CHECK(ck_madt_mpidrs(m2, mp, 4, &mn) == -1);
251 CHECK(ck_madt_mpidrs(fadt, mp, 4, &mn) == -1); /* not a MADT */
252 test_spans();
253 return ck_t_verdict("CK_ACPI");
254}