Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/core/kmain.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / core / kmain.c` 402 lines ยท 17336 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 /* kmain.c -- C kernel core bring-up after the UEFI stub. 18 * 19 * ck_kernel_entry runs at the firmware EL on the firmware's translation: 20 * vectors, PSCI conduit, own address space, then EL2 -> EL1h (ADR 0009) with 21 * the MMU on our tables. ck_el1_main then reports the MMU switch from live 22 * registers, proves the guard pages, brings up GICv3 and the EL1 physical 23 * timer, starts and parks the secondary cores (core/smp.c), runs the M3 24 * isolation checks (core/m3.c) and the linked boot stages, 25 * and ends with the final report and a PSCI reset. QEMU runs of this path 26 * qualify nothing physical. */ 27 #include "arch.h" 28 #include "ck_internal.h" 29 #include "entropy.h" 30 #include "cpufeat.h" 31 #include "handoff_check.h" 32 #include "frames.h" 33 #if CK_BOOT_SHOW 34 #include "boot_screen.h" 35 #endif 36 #ifdef CK_PI5_NET 37 #include "../dev/pi5_net.h" 38 #endif 39 #ifdef CK_PI5_PCIE 40 #include "../dev/pi5_pcie.h" 41 #endif 42 43 #define TTBR_BADDR_MASK 0x0000fffffffffffeull 44 45 static struct ck_handoff *hand; 46 const struct ck_handoff *ck_handoff_get(void) { return hand; } 47 48 /* ck.h operator shell facts. The conventional-memory total is summed once at 49 * kernel entry, while the firmware memory-map copy is still reachable. */ 50 static uint64_t conventional_kb; 51 unsigned ck_exception_level(void) { return ck_current_el(); } 52 uint64_t ck_conventional_memory_kb(void) { return conventional_kb; } 53 static void sum_conventional(const struct ck_handoff *h) 54 { 55 uint64_t pages = 0; 56 if (!h->memory_map || h->desc_size < sizeof(struct ck_efi_desc)) return; 57 for (uint64_t off = 0; off + h->desc_size <= h->map_size; off += h->desc_size) { 58 const struct ck_efi_desc *d = (const struct ck_efi_desc *)(uintptr_t)(h->memory_map + off); 59 if (d->type == CK_EFI_CONVENTIONAL && d->pages <= (1ull << 36)) pages += d->pages; 60 } 61 conventional_kb = pages * 4u; 62 } 63 64 const void *ck_acpi_find(const char sig[4]) 65 { 66 return hand && hand->rsdp ? ck_acpi_lookup(hand->rsdp, sig) : 0; 67 } 68 69 static void run_stage(const char *name, int (*fn)(void)) 70 { 71 if (!fn) { 72 ck_printf("stage %s: not linked\n", name); 73 return; 74 } 75 ck_set_stage(name); 76 int rc = fn(); 77 ck_irq_cpu_enable(0); 78 if (rc == 0) 79 ck_printf("stage %s: ok\n", name); 80 else 81 ck_printf("stage %s: FAIL rc=%d\n", name, rc); 82 } 83 84 /* CHANDOF3: put the console on the GOP framebuffer (mapped by ck_mm_build) 85 * or say on the UART why there is no screen. The first line drawn on the 86 * screen is the "screen: gop" line (scripts/qemu_ck_screen_test.sh renders 87 * the serial log from that line on and compares it with a QEMU screendump). */ 88 static void screen_start(const struct ck_handoff *h, const struct ck_mm_report *mm) 89 { 90 if (h->fb_status == CK_HANDOFF_FB_NONE) { 91 ck_puts("screen: none (no UEFI Graphics Output Protocol); report on UART only\n"); 92 return; 93 } 94 if (h->fb_status == CK_HANDOFF_FB_NO_LINEAR) { 95 ck_printf("screen: none (GOP handles=%u, none with a linear 32-bit framebuffer); report on UART only\n", 96 h->fb_gop_handles); 97 return; 98 } 99 if (mm->fb_map != CK_MM_FB_MAPPED) { 100 ck_printf("screen: refused (framebuffer 0x%llx+0x%llx overlaps RAM mapped write-back); report on UART " 101 "only\n", 102 (unsigned long long)h->fb_base, (unsigned long long)mm->fb_map_bytes); 103 return; 104 } 105 if (ck_console_set_fb((volatile void *)(uintptr_t)h->fb_base, h->fb_width, h->fb_height, h->fb_pitch, 106 h->fb_format) != 0) { 107 ck_printf("screen: refused (geometry %ux%u pitch=%u format=%u unusable); report on UART only\n", 108 h->fb_width, h->fb_height, h->fb_pitch, h->fb_format); 109 return; 110 } 111 uint32_t scale = 0, cols = 0, rows = 0; 112 ck_console_fb_info(&scale, &cols, &rows); 113 ck_printf("screen: gop %ux%u pitch=%u format=%s base=0x%llx bytes=0x%llx scale=%u grid=%ux%u font=8x8 " 114 "mode=half-scroll\n", 115 h->fb_width, h->fb_height, h->fb_pitch, h->fb_format == CK_HANDOFF_FB_RGBX ? "rgbx" : "bgrx", 116 (unsigned long long)h->fb_base, (unsigned long long)mm->fb_map_bytes, scale, cols, rows); 117 } 118 119 /* TEST-ONLY rollback candidates (CK gate M0_ROLLBACK, Makefile 120 * CK_TEST_ROLLBACK=cpu-fault|hang, scripts/qemu_ck_rollback_test.sh). They 121 * end the candidate boot the two ways a real kernel can fail after 122 * "kernel: alive": a CPU exception (BRK at EL1 -> ck_fault_report -> reset) 123 * or a hang with interrupts masked (no reset; only an outside reset ends it, 124 * docs/BOOT_HANDOFF_CONTRACT.md 7 step 3). Never in a hardware staging image. */ 125 #if defined(CK_TEST_ROLLBACK_CPU_FAULT) || defined(CK_TEST_ROLLBACK_HANG) 126 #if defined(CK_HARDWARE_STAGING) 127 #error "CK_TEST_ROLLBACK is TEST-only and cannot be combined with CK_HARDWARE_STAGING" 128 #endif 129 static __attribute__((noreturn)) void rollback_test_end(void) 130 { 131 #ifdef CK_TEST_ROLLBACK_CPU_FAULT 132 ck_puts("rollback_test: TEST-ONLY rollback candidate (cpu-fault): brk at EL1 now; never counts toward a PASS\n"); 133 __asm__ volatile("brk #0x7b" ::: "memory"); 134 ck_panic("rollback_test: brk returned"); 135 #else 136 ck_puts("rollback_test: TEST-ONLY rollback candidate (hang): spinning with interrupts masked, no reset follows; never counts toward a PASS\n"); 137 __asm__ volatile("msr daifset, #0xf" ::: "memory"); 138 for (;;) 139 __asm__ volatile("wfe" ::: "memory"); 140 #endif 141 } 142 #endif 143 144 #ifdef CK_PIPE_TEST 145 void ck_pipe_selftest(void); 146 #endif 147 148 static __attribute__((noreturn)) void ck_el1_main(void *arg) 149 { 150 struct ck_handoff *h = arg; 151 ck_set_stage("kernel_el1"); 152 ck_mm_el1_ready(); 153 const struct ck_mm_report *mm = ck_mm_report(); 154 screen_start(h, mm); 155 if (ck_pi5_start) 156 ck_pi5_start(); 157 158 ck_puts("\n"); 159 ck_report_header("kernel"); 160 unsigned el = ck_current_el(); 161 uint64_t sctlr = ck_rd(sctlr_el1); 162 int mmu_on = (sctlr & 1) != 0; 163 ck_printf("mmu: %s\n", mmu_on ? "enabled" : "disabled"); 164 ck_printf("pt_frames_used: %llu\n", (unsigned long long)mm->pt_tables); 165 uint64_t el1_base = ck_rd(ttbr0_el1) & TTBR_BADDR_MASK; 166 uint64_t fw_base = h->firmware_ttbr0 & TTBR_BADDR_MASK; 167 int switched = el == 1 && mmu_on && el1_base == mm->root && el1_base != fw_base; 168 ck_printf("mmu_switch: firmware=EL%u firmware_mmu=%s firmware_ttbr0=0x%llx aienos_root=0x%llx " 169 "ttbr0_el1=0x%llx switched=%s\n", 170 h->firmware_el, (h->firmware_sctlr & 1) ? "on" : "off", (unsigned long long)fw_base, 171 (unsigned long long)mm->root, (unsigned long long)el1_base, switched ? "yes" : "no"); 172 ck_printf("mmu_regs: mair=0x%llx tcr=0x%llx sctlr=0x%llx\n", (unsigned long long)ck_rd(mair_el1), 173 (unsigned long long)ck_rd(tcr_el1), (unsigned long long)sctlr); 174 ck_printf("memory: ram_ranges=%llu free_bytes=%llu stack=0x%llx-0x%llx heap=0x%llx-0x%llx " 175 "dma=0x%llx-0x%llx exit_attempts=%u\n", 176 (unsigned long long)mm->ram_ranges, (unsigned long long)mm->free_bytes, 177 (unsigned long long)mm->stack_lo, (unsigned long long)mm->stack_hi, 178 (unsigned long long)mm->heap_lo, (unsigned long long)mm->heap_hi, 179 (unsigned long long)mm->dma_lo, (unsigned long long)mm->dma_hi, h->exit_attempts); 180 181 /* Heap smoke test. */ 182 void *a = ck_alloc(100), *b = ck_alloc(5000); 183 if (!a || !b) 184 ck_panic("heap: allocation failed"); 185 ck_free(a); 186 ck_free(b); 187 188 /* Addition over the Rust kernel: the guard pages really fault, and the 189 * fault is contained (handled and resumed, no reset). */ 190 char detail[160]; 191 ck_set_stage("guard_page"); 192 if (ck_mm_guard_selftest(detail, sizeof detail) == 0) 193 ck_printf("guard_page: ok fault=contained (%s) [C kernel addition, not in the Rust kernel]\n", 194 detail); 195 else 196 ck_printf("guard_page: FAIL %s\n", detail); 197 198 /* GICv3 or GICv2 (arch/gic.c) from the MADT, then the EL1 physical timer window. */ 199 ck_set_stage("gic"); 200 const void *madt = ck_acpi_find("APIC"); 201 struct ck_madt_gic mg; 202 if (!madt || ck_madt_parse(madt, &mg)) 203 ck_panic("gic: no usable ACPI MADT"); 204 struct ck_gic_report gr; 205 int rc = ck_gic_init(&mg, &gr); 206 if (rc) 207 ck_panic("gic: init failed rc=%d (madt gicd=0x%llx gicr=0x%llx)", rc, 208 (unsigned long long)mg.gicd, (unsigned long long)mg.gicr); 209 ck_set_stage("timer"); 210 struct ck_timer_window tw; 211 if (ck_timer_window(200, &tw)) 212 ck_panic("timer: counter frequency is zero"); 213 uint64_t f = tw.freq_hz; 214 ck_printf("gic: v%u\n", gr.arch_rev); 215 ck_printf("timer_irq: %llu ticks in %llu ms\n", (unsigned long long)tw.ticks, 216 (unsigned long long)tw.ms); 217 ck_printf("gic_madt: gicd=0x%llx gicr=0x%llx arch_rev=%u icc_sre=%llu\n", 218 (unsigned long long)gr.gicd, (unsigned long long)gr.gicr, gr.arch_rev, 219 (unsigned long long)gr.icc_sre); 220 if (gr.kind == 2) 221 ck_printf("gic_cpu_interface: mmio gicc=0x%llx\n", (unsigned long long)gr.gicc); 222 else 223 ck_printf("gic_redistributor: frame=0x%llx\n", (unsigned long long)gr.rd_frame); 224 ck_printf("timer_stats: intid=30 freq_hz=%llu period_us=%llu min_us=%llu avg_us=%llu max_us=%llu\n", 225 (unsigned long long)f, (unsigned long long)ck_counter_to_us(f / 100, f), 226 (unsigned long long)ck_counter_to_us(tw.min_interval, f), 227 (unsigned long long)(tw.ticks ? ck_counter_to_us(tw.span / tw.ticks, f) : 0), 228 (unsigned long long)ck_counter_to_us(tw.max_interval, f)); 229 230 uint64_t spsel = ck_rd(spsel); 231 ck_printf("exception_level: EL%u\n", el); 232 ck_puts("kernel: alive\n"); 233 ck_printf("kernel_el: EL%u%s\n", el, (spsel & 1) ? "h" : "t"); 234 235 #if defined(CK_TEST_ROLLBACK_CPU_FAULT) || defined(CK_TEST_ROLLBACK_HANG) 236 rollback_test_end(); 237 #endif 238 #ifdef CK_PIPE_TEST 239 /* TEST-ONLY pipe self-test (core/pipe_test.c, scripts/qemu_ck_pipe_test.sh); never in a production image. */ 240 ck_pipe_selftest(); 241 #endif 242 243 { 244 struct ck_cpu_features cf; 245 char cfl[96]; 246 ck_cpufeat_decode(ck_rd(id_aa64isar0_el1), ck_rd(id_aa64isar1_el1), ck_rd(id_aa64pfr0_el1), &cf); 247 ck_cpufeat_set_boot(&cf); 248 ck_cpufeat_format(&cf, cfl, sizeof cfl); 249 ck_printf("%s\n", cfl); /* boot core only; see core/cpufeat.h */ 250 } 251 252 /* Kernel entropy (arch/rndr.c): RNDR or a latched refusal, no fallback. 253 * Probed once here, before the artifact loader and the stages. */ 254 ck_set_stage("entropy"); 255 int ent = ck_entropy_init(); 256 if (ent == CK_RNG_OK) 257 #ifdef CK_PI5 258 ck_printf("entropy: source=%s probe_words=2 retries=%llu stuck_test=ok\n", ck_entropy_source(), 259 (unsigned long long)ck_entropy_retries()); 260 #else 261 ck_printf("entropy: rndr feat_rng=yes probe_words=2 retries=%llu stuck_test=ok\n", 262 (unsigned long long)ck_entropy_retries()); 263 #endif 264 else 265 ck_printf("entropy: unavailable reason=%s (%s); security consumers refuse, fail closed\n", 266 ck_entropy_reason(), 267 #ifdef CK_PI5 268 ent == CK_RNG_ABSENT ? "source=rng200 not present: block unmapped or not enabled" 269 #else 270 ent == CK_RNG_ABSENT ? "ID_AA64ISAR0_EL1.RNDR=0, no FEAT_RNG" 271 #endif 272 : ent == CK_RNG_FAILED ? "RNDR returned no number within the retry bound" 273 : ent == CK_RNG_STUCK ? "RNDR repeated a 64-bit word" 274 : "probe error"); 275 276 #ifdef CK_PI5_PCIE 277 /* Pi 5 PCIe2 root complex / RP1 enumerator (dev/pi5_pcie.c): read-only boot report, CK_PI5_PCIE image only. */ 278 ck_set_stage("pi5_pcie"); 279 ck_pi5pcie_report(); 280 #endif 281 #ifdef CK_PI5_NET 282 /* Pi 5 network cut 2 (dev/pi5_net.c): narrow the PCIe inbound DMA window, bring RP1 Ethernet up to the PHY. CK_PI5_NET image only. */ 283 ck_set_stage("pi5_net"); 284 ck_pi5net_run(); 285 #endif 286 287 #ifdef CK_B7B_PROBE 288 /* B7b preparation probe (core/smmu_svc.c), probe build only. */ 289 ck_b7b_probe(); 290 #endif 291 292 /* Secondary cores (core/smp.c): PSCI CPU_ON, check in, park in WFE with 293 * interrupts masked; the boot core goes on alone. Lines judged by 294 * scripts/qemu_ck_smp_test.sh (CK gate SMP). */ 295 ck_smp_run(); 296 297 /* One FP/SIMD-enabled Rust unit (core/fpu.c), probe build only. */ 298 ck_fpu_run(); 299 300 /* Ingest the GGUF over fw_cfg and call the Rust inference unit (core/infer.c), probe build only. 301 * A CK_INFER_RESIDENT service image opens the session after the store 302 * stage instead (below), so the session's frame accounting (free frames 303 * at open == free frames at close) brackets only its own allocations and 304 * not the NVMe queues the store stage binds for the service lifetime. */ 305 #ifndef CK_INFER_RESIDENT 306 ck_infer_run(); 307 #endif 308 309 /* M3 isolation checks (core/m3.c), before any stage registers an IRQ. */ 310 ck_m3_run(); 311 312 run_stage("devices", ck_stage_devices); 313 #if CK_BOOT_SHOW 314 /* The boot sequence (core/boot_screen.c): the night and its chirp on the screen, once. */ 315 ck_boot_screen_run(); 316 #endif 317 run_stage("security", ck_stage_security); 318 run_stage("store", ck_stage_store); 319 320 /* P2 sealed-artifact loader (core/artifact_loader.c). Runs after the store 321 * stage: its default candidate source is the boot disk Store, read by that 322 * stage before the NVMe DMA revoke. No stage registers an IRQ. */ 323 ck_artifact_run(); 324 #if defined(CK_INFER_RESIDENT) && !defined(CK_PI5_AGENT) 325 /* Resident inference session for the lifecycle (core/infer.c): model 326 * ingest and session open after the store stage, before the lifecycle. 327 * Not in the Pi agent image: its stage (svc/pi5_agent.c) opens the session itself, inside the 328 * store stage and only after the Store opened, and closes it before committing. */ 329 ck_infer_run(); 330 #endif 331 /* PI5-8 cut K ALLEN exam stage (core/allen.c): pi5-allen image only, else a no-op. Before mm_usage so the 332 * stack high-water mark below includes the Rust decision core. */ 333 ck_allen_run(); 334 struct ck_mm_usage mu; 335 ck_mm_usage(&mu); 336 ck_printf("mm_usage: stack_bytes=%llu stack_used=%llu heap_bytes=%llu heap_free=%llu heap_min_free=%llu\n", 337 (unsigned long long)mu.stack_bytes, (unsigned long long)mu.stack_used, 338 (unsigned long long)mu.heap_bytes, (unsigned long long)mu.heap_free, 339 (unsigned long long)mu.heap_min_free); 340 341 /* Optional final boot stage: the operator console session (TEST-ONLY image 342 * built with CK_CONSOLE_SESSION=1; weak, absent and silent otherwise). */ 343 if (ck_stage_console_session) { 344 ck_set_stage("console"); 345 int crc = ck_stage_console_session(); 346 ck_irq_cpu_enable(0); 347 ck_printf("stage console: %s rc=%d\n", crc == 0 ? "ok" : "FAIL", crc); 348 } 349 350 /* Resident production lifecycle (core/lifecycle.c; only a CK_SERVICE=1 351 * image links it, weak and absent otherwise). The product boundary: the 352 * service serves bounded operator requests here until its stop order, 353 * then falls through to the final report and the reset below. */ 354 if (ck_lifecycle_run) { 355 ck_set_stage("lifecycle"); 356 int lrc = ck_lifecycle_run(); 357 ck_irq_cpu_enable(0); 358 ck_printf("stage lifecycle: %s rc=%d\n", lrc == 0 ? "ok" : "FAIL", lrc); 359 } 360 361 #ifdef CK_B7B_EVENT_READOUT 362 ck_b7b_event_readout(); 363 #endif 364 ck_set_stage("final"); 365 ck_puts("\n"); 366 ck_report_header("final"); 367 ck_artifact_final(); 368 ck_puts("note: QEMU qualifies nothing physical\n"); 369 ck_reset(); 370 } 371 372 void ck_kernel_entry(struct ck_handoff *h) 373 { 374 hand = h; 375 unsigned el = ck_current_el(); 376 if (el == 2) 377 ck_wr(vbar_el2, (uint64_t)(uintptr_t)ck_vectors_el2); 378 else 379 ck_wr(vbar_el1, (uint64_t)(uintptr_t)ck_vectors); 380 ck_isb(); 381 #ifdef CK_SELFTEST_EL2_FAULT 382 /* One-off check of the EL2 fatal path (never in a normal build). */ 383 if (el == 2) 384 __asm__ volatile("brk #0x2"); 385 #endif 386 const char *why; 387 if (ck_handoff_check(h, &why) != 0) 388 ck_panic("handoff: %s", why); 389 sum_conventional(h); 390 if (ck_slot_boot_capture) ck_slot_boot_capture(h); 391 ck_psci_configure(ck_acpi_find("FACP")); 392 ck_set_stage("mm_build"); 393 uint64_t sp = ck_mm_build(h); 394 ck_set_stage("el1_switch"); 395 if (el == 2) 396 ck_enter_el1(ck_mm_report()->root, ck_mm_mair(), ck_mm_tcr(), ck_mm_sctlr(), sp, 397 ck_el1_main, h); 398 if (el == 1) 399 ck_switch_el1(ck_mm_report()->root, ck_mm_mair(), ck_mm_tcr(), ck_mm_sctlr(), sp, 400 ck_el1_main, h); 401 ck_panic("kernel entry at unsupported EL%u", el); 402 }