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

286 lines · 9783 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_handoff.c -- host tests of core/handoff_check.c against the handoff
18 * contract (docs/BOOT_HANDOFF_CONTRACT.md sections 6 and 9).
19 *
20 * Positive control: a record filled the way native/boot/efi_main.c fills it
21 * (efi_main.c:60-76 and 126-129, static so reserved0 is zero) with a QEMU
22 * AAVMF style map (48-byte descriptors, version 1; UNVERIFIED stand-in
23 * values, the real boot is proven by scripts/qemu_ck_boot_test.sh).
24 * One negative case per refusal reason, each checked for the exact
25 * "handoff: <reason>" line the kernel panics with.
26 *
27 * Mutation: built with -DCK_HANDOFF_MUTANT_SKIP_RESERVED the validator
28 * skips the reserved0 check; this test then FAILS (reserved0 = 1 is
29 * accepted). "make test" builds that mutant and requires it to fail. */
30#include <string.h>
31#include "ck_test.h"
32#include "handoff_check.h"
33
34static uint64_t map_buf[(64u << 10) / 8];
35
36static struct ck_handoff good(void)
37{
38 static const char commit[] = "test";
39 struct ck_handoff h;
40 memset(&h, 0, sizeof h); /* efi_main.c: static handoff, BSS zeroed */
41 h.magic = CK_HANDOFF_MAGIC;
42 h.firmware_el = 2;
43 h.uefi_entry_ticks = 123456;
44 h.counter_freq_hz = 62500000;
45 h.firmware_ttbr0 = 0x13ffff000ull;
46 h.firmware_sctlr = 0x30c5183dull;
47 h.image_base = 0x13c000000ull;
48 h.image_end = 0x13c080000ull;
49 h.commit = commit;
50 h.memory_map = (uint64_t)(uintptr_t)map_buf;
51 h.rsdp = 0x13f5f0018ull;
52 h.desc_size = 48;
53 h.desc_version = 1;
54 h.map_size = 48 * 120;
55 h.exit_attempts = 1;
56 return h;
57}
58
59/* Runs the validator and returns the panic line the kernel would print
60 * ("" on acceptance). */
61static const char *line(const struct ck_handoff *h)
62{
63 static char buf[96];
64 const char *why = (const char *)1;
65 if (ck_handoff_check(h, &why) == 0)
66 return why == 0 ? "" : "accepted but why set";
67 snprintf(buf, sizeof buf, "handoff: %s", why ? why : "(null why)");
68 return buf;
69}
70
71static void expect(const struct ck_handoff *h, const char *want)
72{
73 const char *got = line(h);
74 int ok = strcmp(got, want) == 0;
75 CHECK(ok);
76 if (!ok)
77 printf(" want \"%s\" got \"%s\"\n", want, got);
78}
79
80int main(void)
81{
82 struct ck_handoff h;
83
84 /* 13. positive control; the full 64 KiB map buffer is also accepted. */
85 h = good();
86 expect(&h, "");
87 h.map_size = 48 * 1365; /* 65520, largest multiple of 48 <= 64 KiB */
88 expect(&h, "");
89 h = good();
90 h.firmware_el = 1;
91 expect(&h, "");
92
93 /* 1. NULL record */
94 expect(0, "handoff: null record");
95
96 /* 2. bad magic: one bit off and 0; the previous layouts CHANDOF1 and
97 * CHANDOF2 and a future CHANDOF4 get the "unsupported version" line
98 * (contract section 6), a non-digit version byte is plain bad magic. */
99 h = good();
100 h.magic ^= 1ull << 8;
101 expect(&h, "handoff: bad magic");
102 h = good();
103 h.magic = CK_HANDOFF_MAGIC_V1;
104 expect(&h, "handoff: unsupported version 1 (kernel reads CHANDOF3 only)");
105 h = good();
106 h.magic = CK_HANDOFF_MAGIC_V2;
107 expect(&h, "handoff: unsupported version 2 (kernel reads CHANDOF3 only)");
108 h = good();
109 h.magic = CK_HANDOFF_MAGIC_STEM | (uint64_t)'4' << 56;
110 expect(&h, "handoff: unsupported version 4 (kernel reads CHANDOF3 only)");
111 h = good();
112 h.magic = CK_HANDOFF_MAGIC_STEM | (uint64_t)'X' << 56;
113 expect(&h, "handoff: bad magic");
114 h = good();
115 h.magic = 0;
116 expect(&h, "handoff: bad magic");
117
118 /* 3. reserved0 nonzero (the mutant skips this; must FAIL then) */
119 h = good();
120 h.reserved0 = 1;
121 expect(&h, "handoff: reserved field nonzero");
122 h = good();
123 h.reserved0 = 0x80000000u;
124 expect(&h, "handoff: reserved field nonzero");
125
126 /* 4. descriptor size 0, 39, 41 (not a multiple of 8), 44 */
127 h = good();
128 h.desc_size = 0;
129 expect(&h, "handoff: bad descriptor size 0");
130 h.desc_size = 39;
131 expect(&h, "handoff: bad descriptor size 39");
132 h.desc_size = 41;
133 expect(&h, "handoff: bad descriptor size 41");
134 h.desc_size = 44;
135 expect(&h, "handoff: bad descriptor size 44");
136 h.desc_size = 40; /* smallest accepted stride; 48*120 is a multiple of 40 */
137 expect(&h, "");
138
139 /* 5. descriptor version 0 and 2 */
140 h = good();
141 h.desc_version = 0;
142 expect(&h, "handoff: bad descriptor version 0");
143 h.desc_version = 2;
144 expect(&h, "handoff: bad descriptor version 2");
145
146 /* 6. map size 0, not a multiple of desc_size, 65544 (> 64 KiB) */
147 h = good();
148 h.map_size = 0;
149 expect(&h, "handoff: bad map size 0");
150 h.map_size = 48 * 120 + 8;
151 expect(&h, "handoff: bad map size 5768");
152 h.desc_size = 21848; /* 65544 = 3 * 21848: refused only for > 64 KiB */
153 h.map_size = 65544;
154 expect(&h, "handoff: bad map size 65544");
155 h.desc_size = 64; /* boundary: exactly 64 KiB is accepted */
156 h.map_size = 65536;
157 expect(&h, "");
158 h.desc_size = 65544; /* stride larger than 64 KiB: one descriptor cannot fit */
159 h.map_size = 65544;
160 expect(&h, "handoff: bad map size 65544");
161
162 /* 7. CHANDOF2 model fields (docs/BOOT_HANDOFF_CONTRACT.md 3.2): reserved1,
163 * unknown flags, fields set without the present flag, and the range/
164 * geometry checks on a well-formed disk model (the Spark numbers). */
165 h = good();
166 h.reserved1 = 1;
167 expect(&h, "handoff: reserved1 field nonzero");
168 h = good();
169 h.model_flags = 4;
170 expect(&h, "handoff: bad model flags 4");
171 h = good();
172 h.model_len = 100;
173 expect(&h, "handoff: model fields without model");
174 h = good();
175 h.model_sha256[31] = 1;
176 expect(&h, "handoff: model fields without model");
177 h = good();
178 h.model_flags = CK_HANDOFF_MODEL_BLOCKIO;
179 expect(&h, "handoff: model fields without model");
180 struct ck_handoff m = good();
181 m.model_flags = CK_HANDOFF_MODEL_PRESENT | CK_HANDOFF_MODEL_BLOCKIO;
182 m.model_base = 0x140000000ull;
183 m.model_len = 807694368ull;
184 m.model_extents = 24;
185 m.model_block_size = 512;
186 m.model_disk_last_block = 8001573551ull;
187 m.model_read_us = 1234567;
188 m.model_sha256[0] = 0x3f;
189 expect(&m, "");
190 h = m;
191 h.model_base = 0;
192 expect(&h, "handoff: bad model range 807694368");
193 h = m;
194 h.model_len = 0;
195 expect(&h, "handoff: bad model range 0");
196 h = m;
197 h.model_base = 0x140000800ull;
198 expect(&h, "handoff: model base unaligned 5368711168");
199 h = m;
200 h.model_len = ~0ull;
201 expect(&h, "handoff: model range overflow 18446744073709551615");
202 h = m;
203 h.model_block_size = 1024;
204 expect(&h, "handoff: bad model block size 1024");
205 h = m;
206 h.model_extents = 0;
207 expect(&h, "handoff: bad model extents 0");
208 h = m;
209 h.model_flags = CK_HANDOFF_MODEL_PRESENT;
210 expect(&h, "");
211
212 /* 8. CHANDOF3 framebuffer fields (contract section 3): absent GOP must be
213 * all zero; a GOP without a linear framebuffer carries no range; a
214 * present framebuffer (the Spark's Linux view: 1920x1080, 7680-byte
215 * stride, 32 bpp; QEMU ramfb) passes the geometry checks. */
216 h = good();
217 h.fb_width = 800;
218 expect(&h, "handoff: fb fields without gop");
219 h = good();
220 h.fb_gop_handles = 1;
221 expect(&h, "handoff: fb fields without gop");
222 h = good();
223 h.fb_status = 3;
224 expect(&h, "handoff: bad fb status 3");
225 h = good();
226 h.fb_status = CK_HANDOFF_FB_NO_LINEAR;
227 h.fb_gop_handles = 1;
228 h.fb_format = 3; /* PixelBltOnly, recorded for the report */
229 expect(&h, "");
230 h.fb_base = 0x80000000ull;
231 expect(&h, "handoff: fb range without linear framebuffer 0");
232 h.fb_base = 0;
233 h.fb_gop_handles = 0;
234 expect(&h, "handoff: bad fb gop handles 0");
235 struct ck_handoff f = good();
236 f.fb_status = CK_HANDOFF_FB_PRESENT;
237 f.fb_base = 0x1f0000000ull;
238 f.fb_size = 1920ull * 1080 * 4;
239 f.fb_width = 1920;
240 f.fb_height = 1080;
241 f.fb_pitch = 1920;
242 f.fb_format = CK_HANDOFF_FB_BGRX;
243 f.fb_gop_handles = 1;
244 expect(&f, "");
245 h = f;
246 h.fb_format = CK_HANDOFF_FB_RGBX;
247 expect(&h, "");
248 h = f;
249 h.fb_format = 2; /* PixelBitMask is never PRESENT */
250 expect(&h, "handoff: bad fb format 2");
251 h = f;
252 h.fb_width = 0;
253 expect(&h, "handoff: bad fb width 0");
254 h = f;
255 h.fb_height = 16385;
256 expect(&h, "handoff: bad fb height 16385");
257 h = f;
258 h.fb_pitch = 1919;
259 expect(&h, "handoff: bad fb pitch 1919");
260 h = f;
261 h.fb_pitch = 2048; /* padded scan lines: size must cover pitch, not width */
262 expect(&h, "handoff: fb size too small 8294400");
263 h.fb_size = 2048ull * 1080 * 4;
264 expect(&h, "");
265 h = f;
266 h.fb_base = 0;
267 expect(&h, "handoff: bad fb base 0");
268 h = f;
269 h.fb_base = 0x1f0000002ull;
270 expect(&h, "handoff: bad fb base 8321499138");
271 h = f;
272 h.fb_base = ~0ull - 4095;
273 expect(&h, "handoff: fb range overflow 8294400");
274 h = f;
275 h.fb_gop_handles = 0;
276 expect(&h, "handoff: bad fb gop handles 0");
277
278 /* Check order: the first failing field is reported. */
279 h = good();
280 h.magic = 0;
281 h.reserved0 = 1;
282 h.map_size = 0;
283 expect(&h, "handoff: bad magic");
284
285 return ck_t_verdict("test_handoff");
286}