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 / core / acpi_dev.c

392 lines · 14025 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/* acpi_dev.c -- bounded static scan of DSDT/SSDT Device objects (see
18 * acpi_dev.h). Freestanding; host tested in tests/test_acpi_dev.c. */
19#include "acpi_dev.h"
20
21/* TEST-ONLY code mutants (host test tests/test_acpi_dev.c must FAIL against
22 * each; Makefile target acpi-dev-mutant): 1 = signature check removed,
23 * 2 = checksum check removed, 3 = length-versus-buffer check removed. Never
24 * in a kernel image or a hardware staging build. */
25#ifdef CK_AML_MUTANT
26#if !__STDC_HOSTED__
27#error "CK_AML_MUTANT is a host test mutant only"
28#endif
29#ifdef CK_HARDWARE_STAGING
30#error "CK_AML_MUTANT cannot be combined with CK_HARDWARE_STAGING"
31#endif
32#else
33#define CK_AML_MUTANT 0
34#endif
35
36#define AML_HDR 36u
37#define OP_NAME 0x08u
38#define OP_ZERO 0x00u
39#define OP_ONE 0x01u
40#define OP_BYTE 0x0au
41#define OP_WORD 0x0bu
42#define OP_DWORD 0x0cu
43#define OP_STRING 0x0du
44#define OP_QWORD 0x0eu
45#define OP_BUFFER 0x11u
46#define OP_METHOD 0x14u
47#define OP_EXT 0x5bu
48#define OP_EXT_DEVICE 0x82u
49#define PFX_ROOT 0x5cu /* '\\' */
50#define PFX_PARENT 0x5eu /* '^' */
51#define PFX_DUAL 0x2eu
52#define PFX_MULTI 0x2fu
53
54static uint32_t rd16(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8; }
55static uint32_t rd32(const uint8_t *p) { return rd16(p) | rd16(p + 2) << 16; }
56static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | (uint64_t)rd32(p + 4) << 32; }
57
58static int str_eq(const char *a, const char *b)
59{
60 while (*a && *a == *b) a++, b++;
61 return *a == *b;
62}
63
64int ck_aml_table_ok(const void *table, size_t avail)
65{
66 const uint8_t *t = table;
67 if (!t || avail < AML_HDR) return CK_AML_E_LEN;
68 int dsdt = t[0] == 'D' && t[1] == 'S' && t[2] == 'D' && t[3] == 'T';
69 int ssdt = t[0] == 'S' && t[1] == 'S' && t[2] == 'D' && t[3] == 'T';
70 if (!dsdt && !ssdt && CK_AML_MUTANT != 1) return CK_AML_E_SIG;
71 uint32_t len = rd32(t + 4);
72 if (len < AML_HDR) return CK_AML_E_LEN;
73 if (len > avail && CK_AML_MUTANT != 3) return CK_AML_E_LEN;
74 if (CK_AML_MUTANT == 3 && len > avail) len = (uint32_t)avail; /* mutant: trusts a truncated table */
75 uint8_t sum = 0;
76 for (uint32_t i = 0; i < len; i++) sum = (uint8_t)(sum + t[i]);
77 return sum && CK_AML_MUTANT != 2 ? CK_AML_E_SUM : CK_AML_OK;
78}
79
80/* PkgLength at p (ACPI 6.5 20.2.4): *value is the package length (it counts
81 * its own encoding bytes), *nbytes the encoding size. -1 if past end. */
82static int pkg_length(const uint8_t *p, const uint8_t *end, uint32_t *value, uint32_t *nbytes)
83{
84 if (p >= end) return -1;
85 uint32_t follow = p[0] >> 6;
86 if ((uint32_t)(end - p) < follow + 1u) return -1;
87 if (follow == 0) {
88 *value = p[0] & 0x3fu;
89 } else {
90 if (p[0] & 0x30u) return -1; /* bits 5:4 must be zero in the multi-byte form */
91 uint32_t v = p[0] & 0x0fu;
92 for (uint32_t i = 0; i < follow; i++) v |= (uint32_t)p[1 + i] << (4u + 8u * i);
93 *value = v;
94 }
95 *nbytes = follow + 1u;
96 return 0;
97}
98
99static int seg_char(uint8_t c, int first)
100{
101 if (c == '_' || (c >= 'A' && c <= 'Z')) return 1;
102 return !first && c >= '0' && c <= '9';
103}
104static int name_seg_ok(const uint8_t *s)
105{
106 return seg_char(s[0], 1) && seg_char(s[1], 0) && seg_char(s[2], 0) && seg_char(s[3], 0);
107}
108
109/* NameString at p into out ("\\", "^" prefixes kept, segments joined by
110 * '.'). Returns the encoded size, or 0 if malformed / past end / too long. */
111static uint32_t name_string(const uint8_t *p, const uint8_t *end, char *out, uint32_t cap)
112{
113 const uint8_t *q = p;
114 uint32_t o = 0;
115 if (q < end && *q == PFX_ROOT) {
116 if (o + 1 >= cap) return 0;
117 out[o++] = '\\';
118 q++;
119 } else {
120 while (q < end && *q == PFX_PARENT) {
121 if (o + 1 >= cap) return 0;
122 out[o++] = '^';
123 q++;
124 }
125 }
126 if (q >= end) return 0;
127 uint32_t segs;
128 if (*q == PFX_DUAL) {
129 segs = 2;
130 q++;
131 } else if (*q == PFX_MULTI) {
132 if (end - q < 2) return 0;
133 segs = q[1];
134 q += 2;
135 if (segs == 0) return 0;
136 } else if (*q == 0x00) {
137 segs = 0; /* NullName */
138 q++;
139 } else {
140 segs = 1;
141 }
142 for (uint32_t s = 0; s < segs; s++) {
143 if (end - q < 4 || !name_seg_ok(q)) return 0;
144 if (o + (s ? 5u : 4u) >= cap) return 0;
145 if (s) out[o++] = '.';
146 for (int i = 0; i < 4; i++) out[o++] = (char)q[i];
147 q += 4;
148 }
149 out[o] = 0;
150 return (uint32_t)(q - p);
151}
152
153void ck_aml_eisaid(uint32_t v, char out[8])
154{
155 /* Stored little-endian; the id is the big-endian reading of the DWORD. */
156 uint32_t b = ((v & 0xffu) << 24) | ((v & 0xff00u) << 8) | ((v >> 8) & 0xff00u) | (v >> 24);
157 static const char hex[] = "0123456789ABCDEF";
158 out[0] = (char)('@' + ((b >> 26) & 0x1fu));
159 out[1] = (char)('@' + ((b >> 21) & 0x1fu));
160 out[2] = (char)('@' + ((b >> 16) & 0x1fu));
161 out[3] = hex[(b >> 12) & 0xfu];
162 out[4] = hex[(b >> 8) & 0xfu];
163 out[5] = hex[(b >> 4) & 0xfu];
164 out[6] = hex[b & 0xfu];
165 out[7] = 0;
166}
167
168/* The value of Name(XXXX, ...) for _HID / _CID: a String or an EisaId
169 * DWORD. 0 ok, -1 other forms (packages, integers other than DWORD). */
170static int id_value(const uint8_t *p, const uint8_t *end, char out[CK_AML_ID_MAX])
171{
172 if (p >= end) return -1;
173 if (p[0] == OP_STRING) {
174 uint32_t i = 0;
175 for (const uint8_t *s = p + 1; s < end; s++, i++) {
176 if (*s == 0) {
177 out[i] = 0;
178 return i ? 0 : -1;
179 }
180 if (i + 1 >= CK_AML_ID_MAX || *s < 0x20 || *s > 0x7e) return -1;
181 out[i] = (char)*s;
182 }
183 return -1;
184 }
185 if (p[0] == OP_DWORD && end - p >= 5) {
186 ck_aml_eisaid(rd32(p + 1), out);
187 return 0;
188 }
189 return -1;
190}
191
192/* First memory range of a resource template [p, end) (ACPI 6.5 6.4):
193 * Memory32Fixed, Memory32, DWord / QWord address space of type memory.
194 * 0 found, -1 none or malformed before one was found. */
195static int first_memory(const uint8_t *p, const uint8_t *end, uint64_t *base, uint64_t *len)
196{
197 while (p < end) {
198 uint8_t tag = p[0];
199 if (!(tag & 0x80u)) { /* small item */
200 uint32_t n = tag & 0x7u;
201 if ((tag >> 3) == 0x0fu) return -1; /* End Tag: no memory range */
202 if ((uint32_t)(end - p) < 1u + n) return -1;
203 p += 1u + n;
204 continue;
205 }
206 if (end - p < 3) return -1;
207 uint32_t n = rd16(p + 1);
208 if ((uint32_t)(end - p) < 3u + n) return -1;
209 const uint8_t *d = p + 3;
210 switch (tag) {
211 case 0x86: /* Memory32Fixed: info, base, length */
212 if (n >= 9) {
213 *base = rd32(d + 1);
214 *len = rd32(d + 5);
215 if (*len) return 0;
216 }
217 break;
218 case 0x85: /* Memory32: info, min, max, align, length */
219 if (n >= 17 && rd32(d + 1) == rd32(d + 5)) {
220 *base = rd32(d + 1);
221 *len = rd32(d + 13);
222 if (*len) return 0;
223 }
224 break;
225 case 0x87: /* DWord address space: type, gflags, tflags, gran, min, max, tra, len */
226 if (n >= 23 && d[0] == 0) {
227 *base = rd32(d + 7);
228 *len = rd32(d + 19);
229 if (*len) return 0;
230 }
231 break;
232 case 0x8a: /* QWord address space */
233 if (n >= 43 && d[0] == 0) {
234 *base = rd64(d + 11);
235 *len = rd64(d + 35);
236 if (*len) return 0;
237 }
238 break;
239 default:
240 break;
241 }
242 p += 3u + n;
243 }
244 return -1;
245}
246
247/* Buffer object at p: [data, data_end). 0 ok, -1 malformed. */
248static int buffer_at(const uint8_t *p, const uint8_t *end, const uint8_t **data, const uint8_t **data_end)
249{
250 if (p >= end || p[0] != OP_BUFFER) return -1;
251 uint32_t plen, pn;
252 if (pkg_length(p + 1, end, &plen, &pn)) return -1;
253 const uint8_t *pend = p + 1 + plen;
254 if (plen < pn || pend > end) return -1;
255 const uint8_t *q = p + 1 + pn;
256 uint64_t size;
257 if (q >= pend) return -1;
258 switch (q[0]) {
259 case OP_ZERO: size = 0; q += 1; break;
260 case OP_ONE: size = 1; q += 1; break;
261 case OP_BYTE: if (pend - q < 2) return -1; size = q[1]; q += 2; break;
262 case OP_WORD: if (pend - q < 3) return -1; size = rd16(q + 1); q += 3; break;
263 case OP_DWORD: if (pend - q < 5) return -1; size = rd32(q + 1); q += 5; break;
264 default: return -1;
265 }
266 if (size > (uint64_t)(pend - q)) size = (uint64_t)(pend - q); /* initializer shorter than the size: rest is zero */
267 *data = q;
268 *data_end = q + size;
269 return 0;
270}
271
272/* Valid DeviceOp at p: *body / *dend span the device's term list, name in
273 * path. 0 ok, -1 not a device. */
274static int device_at(const uint8_t *p, const uint8_t *end, const uint8_t **body, const uint8_t **dend, char *path)
275{
276 if (end - p < 3 || p[0] != OP_EXT || p[1] != OP_EXT_DEVICE) return -1;
277 uint32_t plen, pn;
278 if (pkg_length(p + 2, end, &plen, &pn)) return -1;
279 const uint8_t *pend = p + 2 + plen;
280 if (plen < pn || pend > end) return -1;
281 uint32_t nn = name_string(p + 2 + pn, pend, path, CK_AML_PATH_MAX);
282 if (!nn) return -1;
283 *body = p + 2 + pn + nn;
284 *dend = pend;
285 return 0;
286}
287
288static int seg_is(const uint8_t *p, const uint8_t *end, const char *s)
289{
290 return end - p >= 4 && p[0] == (uint8_t)s[0] && p[1] == (uint8_t)s[1] && p[2] == (uint8_t)s[2] && p[3] == (uint8_t)s[3];
291}
292
293/* Reads _HID, _CID and _CRS of the device body [b, e), stopping at the first
294 * nested device (its objects belong to that device). */
295static void device_fields(const uint8_t *b, const uint8_t *e, struct ck_acpi_device *d)
296{
297 for (const uint8_t *p = b; p < e; p++) {
298 const uint8_t *nb, *ne;
299 char tmp[CK_AML_PATH_MAX];
300 if (p[0] == OP_EXT && device_at(p, e, &nb, &ne, tmp) == 0) break;
301 if (p[0] == OP_NAME && seg_is(p + 1, e, "_HID") && !d->hid[0]) {
302 if (id_value(p + 5, e, d->hid)) d->hid[0] = 0;
303 } else if (p[0] == OP_NAME && seg_is(p + 1, e, "_CID") && !d->cid[0]) {
304 if (id_value(p + 5, e, d->cid)) d->cid[0] = 0;
305 } else if (p[0] == OP_NAME && seg_is(p + 1, e, "_CRS") && !d->mmio_len) {
306 const uint8_t *db, *de;
307 if (buffer_at(p + 5, e, &db, &de) == 0 && first_memory(db, de, &d->mmio_base, &d->mmio_len))
308 d->mmio_base = d->mmio_len = 0;
309 } else if (p[0] == OP_METHOD && !d->mmio_len) {
310 uint32_t plen, pn;
311 if (pkg_length(p + 1, e, &plen, &pn) || plen < pn) continue;
312 const uint8_t *mend = p + 1 + plen;
313 const uint8_t *mname = p + 1 + pn;
314 if (mend > e || !seg_is(mname, mend, "_CRS")) continue;
315 /* first Buffer inside the method body (after name and flags) */
316 for (const uint8_t *q = mname + 5; q < mend; q++) {
317 const uint8_t *db, *de;
318 if (q[0] != OP_BUFFER || buffer_at(q, mend, &db, &de)) continue;
319 if (first_memory(db, de, &d->mmio_base, &d->mmio_len)) d->mmio_base = d->mmio_len = 0;
320 break;
321 }
322 }
323 }
324}
325
326static int id_match(const struct ck_acpi_device *d, const char *const *ids, unsigned nids)
327{
328 if (!nids) return 1;
329 for (unsigned i = 0; i < nids; i++)
330 if ((d->hid[0] && str_eq(d->hid, ids[i])) || (d->cid[0] && str_eq(d->cid, ids[i]))) return 1;
331 return 0;
332}
333
334int ck_aml_scan(const void *table, size_t avail, const char *const *ids, unsigned nids,
335 struct ck_acpi_device *out, unsigned max, unsigned *seen)
336{
337 int rc = ck_aml_table_ok(table, avail);
338 if (seen) *seen = 0;
339 if (rc) return rc;
340 const uint8_t *t = table;
341 const uint8_t *end = t + rd32(t + 4);
342 unsigned n = 0, parsed = 0;
343 for (const uint8_t *p = t + AML_HDR; p + 3 <= end; p++) {
344 const uint8_t *b, *e;
345 struct ck_acpi_device d;
346 for (unsigned i = 0; i < sizeof d; i++) ((uint8_t *)&d)[i] = 0;
347 if (p[0] != OP_EXT || device_at(p, end, &b, &e, d.path)) continue;
348 parsed++;
349 d.offset = (uint32_t)(p - t);
350 device_fields(b, e, &d);
351 if (!id_match(&d, ids, nids)) continue;
352 if (n < max && out) out[n] = d;
353 n++;
354 }
355 if (seen) *seen = parsed;
356 return (int)n;
357}
358
359/* Copies one segment (up to 4 chars, padded with '_'); NULL if longer. */
360static const char *seg4(const char *p, char seg[4])
361{
362 int i = 0;
363 for (; *p && *p != '.' && i < 4; i++, p++) seg[i] = *p;
364 if (*p && *p != '.') return 0;
365 for (; i < 4; i++) seg[i] = '_';
366 return p;
367}
368
369int ck_aml_path_eq(const char *a, const char *b)
370{
371 for (;;) {
372 while (*a == '\\' || *a == '.') a++;
373 while (*b == '\\' || *b == '.') b++;
374 if (!*a || !*b) return !*a && !*b;
375 char sa[4], sb[4];
376 a = seg4(a, sa);
377 b = seg4(b, sb);
378 if (!a || !b) return 0;
379 for (int i = 0; i < 4; i++)
380 if (sa[i] != sb[i]) return 0;
381 }
382}
383
384int ck_aml_last_seg_eq(const char *a, const char *b)
385{
386 const char *la = a, *lb = b;
387 for (const char *p = a; *p; p++)
388 if (*p == '.' || *p == '\\' || *p == '^') la = p + 1;
389 for (const char *p = b; *p; p++)
390 if (*p == '.' || *p == '\\' || *p == '^') lb = p + 1;
391 return *la && *lb && ck_aml_path_eq(la, lb);
392}