diff --git a/.crumb b/.crumb index 75e6a96a..a4df6536 100644 --- a/.crumb +++ b/.crumb @@ -33,14 +33,14 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "cca8235751bf317e623ecc5e86d3fb818977b53027dc2b70ac2c5b7f4bd0392e", - "last_commit": "14b4a8b6", + "digest": "dea2985c38978d62713f4db77afd6dadc3e9c3130606ed90f015a5bf6d30b280", + "last_commit": "003c74c5", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "e8aeac2d60e55081ae3c39f89c4946cd8f9f6994f97883281f41fcf1a3555c68", - "last_commit": "cf6eb73c", + "digest": "c7893c9928c69881c17c653c9b31c3b699166a29fa827d48a0f9a5c5d68d101f", + "last_commit": "003c74c5", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." }, @@ -51,15 +51,15 @@ "purpose": "First-party host artifact preparation tools" } ], - "children_root": "sha256:1cbeab4c388d5065d6de1e065e38f6fe1d851936eca85b86525abd9910b433b3", - "compiled_at": "2026-11-06T02:27:56Z", + "children_root": "sha256:f6b14b653363d1bec119ebde3ff071ac94e0df5d26704cfa07c869881a02f1ad", + "compiled_at": "2026-11-06T02:51:44Z", "compiler": "crumb-compile/1", - "digest": "sha256:974e836b8a4b525a3605edffc85920561815105925f42d6beda773cc79a4c2cf", + "digest": "sha256:fce9194675e270cd8ddf31444c1bfa57775f613abb1b750bc317d49b002722e8", "evidence_files": 274, "evidence_root": "sha256:af014a4ed8f6fe759fc5809807fb47578d3410811b522d9f65d749382b029802", "files": 3779, - "generated_at_commit": "14b4a8b63ec35e37fff7074c118a8a06b50bfaf3", - "source_tree": "sha256:91d14a3ce7a684a65121ba20c0d920788c94000b69228ea8fdb3733bea137458" + "generated_at_commit": "003c74c5f8604a67429b6e3b9dd8fee47ea9ea53", + "source_tree": "sha256:c5bb9e2a056b151b3d74d630c3259915a47df003ab780ceed65ec4b41d72d408" }, "owner_lane": "", "provenance": { diff --git a/native/.crumb b/native/.crumb index 8a929f0f..8976ff24 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "ddf9545df0499aa0cb899809f9eca3bf532fbbdde2ed6c79608c3a331de158a0", - "last_commit": "14b4a8b6", + "digest": "fa756452a9307b2526dfec40b0deedb69b4b2eb7f11ec411b4b6afd3f01ceb73", + "last_commit": "003c74c5", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -115,15 +115,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:7295118aaf78e62ce48a2d1158aa0222a0e0c12e2f50c6db42420548e65dde2f", - "compiled_at": "2026-11-06T02:27:56Z", + "children_root": "sha256:dff843524703b8ca03095ca9b3b2c1b7824dc6de73a2c445a70cbb01321fb662", + "compiled_at": "2026-11-06T02:51:44Z", "compiler": "crumb-compile/1", - "digest": "sha256:cca8235751bf317e623ecc5e86d3fb818977b53027dc2b70ac2c5b7f4bd0392e", + "digest": "sha256:dea2985c38978d62713f4db77afd6dadc3e9c3130606ed90f015a5bf6d30b280", "evidence_files": 24, "evidence_root": "sha256:ecc7ccf2e924c221a5bac1d91e23b13d8f3c8d78c2a893a882ed7ca5e88d855d", "files": 779, - "generated_at_commit": "14b4a8b63ec35e37fff7074c118a8a06b50bfaf3", - "source_tree": "sha256:45751e531133bee2084b11fd439448d333537df096b313aaceae69ede6d8fb1e" + "generated_at_commit": "003c74c5f8604a67429b6e3b9dd8fee47ea9ea53", + "source_tree": "sha256:905e2b6c823a1df97a568e5f8dec73e624745f7dc357b6c3c517001d333e2fdf" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index bd8623a6..295f2925 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -58,7 +58,7 @@ }, { "digest": "bcc16a4b2b8667677b3d5ca23e27dece3e62ae4f2b7aa5e3059eb699fe73ecdf", - "last_commit": "14b4a8b6", + "last_commit": "803dc5da", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, @@ -88,7 +88,7 @@ }, { "digest": "08af7f33ca615c6516eb2e2d800c9e6cff204aeb90ca47e934e5a24060ab2865", - "last_commit": "14b4a8b6", + "last_commit": "803dc5da", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, @@ -100,14 +100,14 @@ } ], "children_root": "sha256:8ca5e34f0cd76e78a1531d7764f8c2e2f3d7179d2e24a537c6427604ee39a69a", - "compiled_at": "2026-11-06T02:27:56Z", + "compiled_at": "2026-11-06T02:51:44Z", "compiler": "crumb-compile/1", - "digest": "sha256:ddf9545df0499aa0cb899809f9eca3bf532fbbdde2ed6c79608c3a331de158a0", + "digest": "sha256:fa756452a9307b2526dfec40b0deedb69b4b2eb7f11ec411b4b6afd3f01ceb73", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 522, - "generated_at_commit": "14b4a8b63ec35e37fff7074c118a8a06b50bfaf3", - "source_tree": "sha256:397b9288bf678b8dd2c9e213cf52508a5d510c2d99e699da137b172eea9af15c" + "generated_at_commit": "003c74c5f8604a67429b6e3b9dd8fee47ea9ea53", + "source_tree": "sha256:92795f28620cee9ccf035c93c64ceff10d545eb5a50b6cdca1262476cb58b6ea" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 386fb547..77b74809 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -2778,9 +2778,10 @@ $(IMACKERN_EFI): $(IMACKERN_ELF) @[ "$$(od -An -tx1 -j68 -N2 $@ | tr -d ' ')" = "6486" ] || { echo "PE Machine is not x86_64 (0x8664)"; rm -f $@; exit 1; } @echo "built $@ (commit $(AIENOS_COMMIT))" imac-kernel: $(IMACKERN_EFI) -# W^X negative controls (#409, #484), TEST-ONLY, QEMU only, never deployed: IMACKERN_WX_MUTANT=no_nx drops -# every NX bit, =text_writable leaves the kernel's code writable, =ap_no_wp starts the secondary cores -# without CR0.WP. The kernel's W^X probes (or, for ap_no_wp, the wx_audit core count) must then panic +# W^X and TLB negative controls (#409, #484, #485), TEST-ONLY, QEMU only, never deployed: IMACKERN_WX_MUTANT=no_nx +# drops every NX bit, =text_writable leaves the kernel's code writable, =ap_no_wp starts the secondary cores +# without CR0.WP, =no_shootdown flushes a changed page from the boot core's TLB only. The kernel's W^X and TLB +# probes (or, for ap_no_wp, the wx_audit core count) must then panic # (scripts/qemu_ck_imac_kernel_test.sh builds each into its own OUT). The stamp rebuilds kmain_x86.o and # smp_x86.o whenever the setting changes, so a mutant object never lingers in a default build. IMACKERN_WX_MUTANT ?= @@ -2792,8 +2793,10 @@ else ifeq ($(strip $(IMACKERN_WX_MUTANT)),text_writable) IMACKERN_WX_FLAGS := -DCK_X86_WX_MUTANT_TEXT_RW=1 else ifeq ($(strip $(IMACKERN_WX_MUTANT)),ap_no_wp) IMACKERN_WX_FLAGS := -DCK_X86_WX_MUTANT_AP_NO_WP=1 +else ifeq ($(strip $(IMACKERN_WX_MUTANT)),no_shootdown) +IMACKERN_WX_FLAGS := -DCK_X86_WX_MUTANT_NO_SHOOTDOWN=1 else -$(error IMACKERN_WX_MUTANT must be empty, no_nx, text_writable or ap_no_wp) +$(error IMACKERN_WX_MUTANT must be empty, no_nx, text_writable, ap_no_wp or no_shootdown) endif $(IMACKERN_DIR)/kmain_x86.o $(IMACKERN_DIR)/smp_x86.o: IMACKERN_CFLAGS += $(IMACKERN_WX_FLAGS) $(IMACKERN_DIR)/kmain_x86.o $(IMACKERN_DIR)/smp_x86.o: $(IMACKERN_DIR)/wx_mutant.stamp diff --git a/native/kernel/README.md b/native/kernel/README.md index c84e306e..0bfd75cb 100644 --- a/native/kernel/README.md +++ b/native/kernel/README.md @@ -181,7 +181,7 @@ a 4 KiB guard page under the stack), proves the guard page faults and the fault is contained, measures the TSC against the HPET, runs the local APIC timer (xAPIC) for 20 interrupts at 100 Hz, starts every other core the MADT lists (INIT-SIPI-SIPI into the `x86/smp_x86.S` trampoline below 1 MiB; each core loads -the kernel's tables, checks in and halts), then walks PCI (`x86/pci_x86.c`, MCFG +the kernel's tables, turns interrupts on, checks in and draws or halts), then walks PCI (`x86/pci_x86.c`, MCFG ECAM window; device registers above the identity map are mapped on demand) and reads every disk without writing. SATA through `x86/ahci_x86.c` (polled, no controller or port reset, only IDENTIFY DEVICE and READ DMA EXT exist and the @@ -375,13 +375,24 @@ PAT cache types and the stack guard; the 2 MiB pages around the image and the trampoline are split into 4 KiB pages at the boundaries from `../boot/image_x86_64.ld`. The trampoline page is never writable and executable at once (writable while it is filled, executable while a core runs it, -read-only no-execute once the cores are parked). At boot the kernel proves it: +read-only no-execute once the cores are started). TLB shootdown (#485): +`invlpg` flushes only the core that runs it, and the started cores keep +running, so every page table change that takes rights away or unmaps +(`tlb_flush_page`, the trampoline's included) also sends an IPI (vector 0xf0) +to every secondary core that has checked in and waits until each has flushed +the page and acknowledged; a core that does not answer within a second is a +panic. Mappings are only ever added beside that (device registers, not +present before), and no core caches a not-present entry. At boot the kernel proves it: writes to code and to read-only data and execution from heap and stack must -each fault with the right error code (`wx: ok ...`), and after the cores and +each fault with the right error code (`wx: ok ...`); each secondary core in +turn caches a writable translation of a page, the boot core makes the page +read-only and then unmaps it, and the core's next write and next read must +each fault (`tlb_shootdown: ok cores=3 ...`); and after the cores and disks are up an audit of every page table leaf must find nothing writable and executable, only the code executable, and NXE and WP on every core -(`wx_audit: ok ...`). The QEMU test also builds two TEST-ONLY mutants -(`make imac-kernel IMACKERN_WX_MUTANT=no_nx` or `=text_writable`) and fails +(`wx_audit: ok ...`). The QEMU test also builds four TEST-ONLY mutants +(`make imac-kernel IMACKERN_WX_MUTANT=no_nx`, `=text_writable`, `=ap_no_wp` or +`=no_shootdown`, the last two run with four cores) and fails unless their probes panic. None of this holds from the first instruction: W^X, NX and WP are enforced only once `mm_build` (`x86/kmain_x86.c`) sets EFER.NXE, loads the kernel's own tables into CR3 and sets CR0.WP. Until that CR3 load the diff --git a/native/kernel/x86/kmain_x86.c b/native/kernel/x86/kmain_x86.c index 26e05e5b..93295520 100644 --- a/native/kernel/x86/kmain_x86.c +++ b/native/kernel/x86/kmain_x86.c @@ -67,6 +67,8 @@ #define SEL_DATA 0x10 #define SEL_TSS 0x18 #define VEC_TIMER 0x40 +#define VEC_TLB_PROBE 0x50 /* the shootdown self-test's probe on a secondary core */ +#define VEC_TLB 0xf0 /* TLB shootdown: flush tlb_va on this core and acknowledge */ #define VEC_SPURIOUS 0xff #define STACK_BYTES (64u << 10) #define IST_BYTES (16u << 10) @@ -85,8 +87,9 @@ /* W^X negative controls (make IMACKERN_WX_MUTANT=..., QEMU only, never * deployed): no_nx drops every NX bit, text_writable leaves the code - * writable, ap_no_wp starts the secondary cores without CR0.WP (smp_x86.S). - * The W^X probes or the wx_audit core count below must then fail. */ + * writable, ap_no_wp starts the secondary cores without CR0.WP (smp_x86.S), + * no_shootdown leaves the other cores' TLBs stale (invlpg on the boot core + * only). The W^X and TLB probes or the wx_audit core count below must then fail. */ #define PTE_XD (1ull << 63) /* the hardware execute-disable bit */ #ifdef CK_X86_WX_MUTANT_NO_NX #define PTE_NX 0ull @@ -726,6 +729,11 @@ static struct { } probe; static volatile uint64_t ticks, tick_tsc[32]; static volatile uint64_t stray_irqs; +/* TLB shootdown (#485, tlb_flush_page): the page every secondary core is to + * flush and how many have done so. */ +static volatile uint64_t tlb_va; +static volatile uint32_t tlb_acks; +static void tlb_probe_run(void); static volatile uint32_t *xapic; static const char *const exc_name[32] = { @@ -763,6 +771,17 @@ void ck_x86_trap(struct ck_x86_frame *f) f->rip = probe.resume; return; } + if (f->vector == VEC_TLB) { + __asm__ volatile("invlpg (%0)" ::"r"(tlb_va) : "memory"); + apic_wr(APIC_EOI, 0); + __atomic_fetch_add(&tlb_acks, 1, __ATOMIC_RELEASE); + return; + } + if (f->vector == VEC_TLB_PROBE) { + apic_wr(APIC_EOI, 0); /* first, so VEC_TLB can arrive while the probe waits */ + tlb_probe_run(); + return; + } if (f->vector == VEC_TIMER) { uint64_t n = ticks; if (n < 32) @@ -991,6 +1010,8 @@ int ck_x86_map_mmio(uint64_t pa, uint64_t len) *e = a | PTE_P | PTE_W | PTE_PS | PTE_PCD | PTE_PWT | PTE_NX; mm.pages_uc++; } + /* only not-present entries become present here, and no core caches a + * not-present translation, so the other cores need no shootdown */ __asm__ volatile("invlpg (%0)" ::"r"(a) : "memory"); } return 0; @@ -1138,8 +1159,9 @@ static void apic_start(struct apic_report *ar) * Each enabled local APIC the MADT lists (other than this core's) gets INIT, * SIPI, SIPI pointing at the trampoline (smp_x86.S). A started core loads the * kernel's GDT (no task register) and IDT, sets the same PAT, enables its - * local APIC, records its APIC ID, checks in, and halts with interrupts off. - * Nothing is scheduled on it yet. */ + * local APIC, records its APIC ID, turns interrupts on (only IPIs reach it: + * its LVT entries are masked, the I/O APIC lines too), checks in, and runs + * the night's drawing or halts. */ #define SMP_MAX 16u #define AP_STACK_PAGES 4u #define APIC_ICR_LO 0x300 @@ -1177,13 +1199,14 @@ static void ap_entry(void *arg) ap_ids[idx] = apic_rd(APIC_ID) >> 24; if ((ck_rdmsr(MSR_EFER) & EFER_NXE) && (ck_rd_cr0() & CR0_WP)) __atomic_fetch_add(&ap_nx_wp, 1, __ATOMIC_SEQ_CST); + __asm__ volatile("sti" ::: "memory"); /* before checking in: a shootdown can come any time after */ __atomic_fetch_add(&ap_up, 1, __ATOMIC_SEQ_CST); if (idx == 0) /* the first one draws the night's frames, the next two help */ draw_loop(); else if (idx < 3) help_loop(); for (;;) - __asm__ volatile("cli; hlt"); + __asm__ volatile("hlt"); } static int icr_send(uint32_t id, uint32_t lo) @@ -1213,14 +1236,50 @@ static uint32_t tramp_at(const char *sym) return (uint32_t)(tramp_pa + (uint64_t)(uintptr_t)(sym - ck_x86_tramp_start)); } -/* The trampoline page is never writable and executable at once: RW+NX while - * it is filled, RX while a core runs it, R+NX once the cores are parked (a - * parked core runs nothing, so no TLB shootdown). */ +/* TLB shootdown (#485). invlpg drops a translation from this core's TLB only, + * so a change that takes rights from a page (or unmaps it) is not done until + * every core that may hold the old translation has dropped it too: the page + * is flushed here, VEC_TLB goes to every secondary core that has checked in, + * and this returns once each has flushed it and acknowledged. A core that + * does not answer within a second is a panic, never a page left stale. Only + * the boot core changes page tables, so one request is in flight at a time. */ +static uint32_t tlb_shootdowns, tlb_acked; +static void tlb_flush_page(uint64_t va) +{ + __asm__ volatile("invlpg (%0)" ::"r"(va) : "memory"); +#ifndef CK_X86_WX_MUTANT_NO_SHOOTDOWN + uint32_t n = __atomic_load_n(&ap_up, __ATOMIC_ACQUIRE); + if (n > SMP_MAX) + n = SMP_MAX; + if (!n) + return; + tlb_va = va; + __atomic_store_n(&tlb_acks, 0, __ATOMIC_SEQ_CST); + for (uint32_t k = 0; k < n; k++) + if (icr_send(ap_ids[k], 0x4000 | VEC_TLB)) /* fixed, assert */ + ck_panic("tlb: shootdown IPI to core %u not delivered", ap_ids[k]); + uint64_t t0 = ck_rdtsc(); + uint32_t got; + while ((got = __atomic_load_n(&tlb_acks, __ATOMIC_ACQUIRE)) < n) { + if (ck_rdtsc() - t0 > tsc_hz) + ck_panic("tlb: shootdown of 0x%llx acknowledged by %u of %u secondary cores", (unsigned long long)va, + got, n); + ck_pause(); + } + tlb_shootdowns++; + tlb_acked += got; +#endif +} + +/* The trampoline page is never writable and executable at once, on any core: + * RW+NX while it is filled, RX while a core runs it, R+NX once the cores are + * started. The cores already started are running and still hold its RX + * translation from their own start, so every change is a shootdown. */ static void tramp_prot(uint64_t bits) { uint64_t *e = pte_4k(tramp_pa); *e = (*e & (PTE_ADDR | PTE_PCD | PTE_PWT)) | PTE_P | bits; - __asm__ volatile("invlpg (%0)" ::"r"(tramp_pa) : "memory"); + tlb_flush_page(tramp_pa); } static void smp_start(const struct apic_report *ar, struct smp_report *sr) @@ -1322,6 +1381,93 @@ static int probe_exec(uint8_t *buf) return probe.hit && probe.cr2 == a && (probe.err & 0x1b) == 0x11; } +static int probe_read(uint64_t a) +{ + probe.hit = 0; + probe.armed = 1; + __asm__ volatile("lea 1f(%%rip), %%rax\n\t" + "mov %%rax, %0\n\t" + "movb (%1), %%al\n" + "1:" + : "=m"(probe.resume) + : "r"(a) + : "rax", "memory"); + probe.armed = 0; + return probe.hit && probe.cr2 == a && !(probe.err & 1); +} + +/* ---- TLB shootdown self-test (#485) ---- + * On each secondary core in turn: the core writes tlb_page (so it caches a + * writable translation), the boot core makes the page read-only and shoots + * it down, and the core's next write must fault; the core reads the page + * (a fresh read-only translation), the boot core unmaps it and shoots it + * down, and the core's next read must fault. Between its accesses the core + * waits inside the VEC_TLB_PROBE handler with interrupts on and touches + * nothing else, so a translation the shootdown missed is still in its TLB + * and the access goes through. */ +static uint8_t tlb_page[CK_PAGE] __attribute__((aligned(CK_PAGE))); +static volatile uint32_t tlb_step, tlb_faults; /* tlb_faults: bit 0 the write faulted, bit 1 the read */ + +static void tlb_probe_run(void) +{ + uint64_t a = (uint64_t)(uintptr_t)tlb_page; + __asm__ volatile("sti" ::: "memory"); + *(volatile uint8_t *)tlb_page = 1; + __atomic_store_n(&tlb_step, 1, __ATOMIC_RELEASE); + while (__atomic_load_n(&tlb_step, __ATOMIC_ACQUIRE) != 2) + ck_pause(); + uint32_t got = (uint32_t)probe_write(a); + (void)*(volatile uint8_t *)tlb_page; + __atomic_store_n(&tlb_step, 3, __ATOMIC_RELEASE); + while (__atomic_load_n(&tlb_step, __ATOMIC_ACQUIRE) != 4) + ck_pause(); + got |= (uint32_t)probe_read(a) << 1; + __asm__ volatile("cli" ::: "memory"); + tlb_faults = got; + __atomic_store_n(&tlb_step, 5, __ATOMIC_RELEASE); +} + +static void tlb_wait(uint32_t s, uint32_t id) +{ + uint64_t t0 = ck_rdtsc(); + while (__atomic_load_n(&tlb_step, __ATOMIC_ACQUIRE) != s) { + if (ck_rdtsc() - t0 > tsc_hz) + ck_panic("tlb_shootdown: core %u stopped before step %u", id, s); + ck_pause(); + } +} + +static void tlb_selftest(void) +{ + uint32_t n = __atomic_load_n(&ap_up, __ATOMIC_ACQUIRE), stale_w = 0, stale_r = 0; + if (n > SMP_MAX) + n = SMP_MAX; + uint64_t a = (uint64_t)(uintptr_t)tlb_page, *e = pte_4k(a), rw = *e; + for (uint32_t k = 0; k < n; k++) { + uint32_t id = ap_ids[k]; + __atomic_store_n(&tlb_step, 0, __ATOMIC_SEQ_CST); + if (icr_send(id, 0x4000 | VEC_TLB_PROBE)) /* fixed, assert */ + ck_panic("tlb_shootdown: probe IPI to core %u not delivered", id); + tlb_wait(1, id); + *e = rw & ~PTE_W; + tlb_flush_page(a); + __atomic_store_n(&tlb_step, 2, __ATOMIC_RELEASE); + tlb_wait(3, id); + *e = 0; + tlb_flush_page(a); + __atomic_store_n(&tlb_step, 4, __ATOMIC_RELEASE); + tlb_wait(5, id); + *e = rw; + tlb_flush_page(a); + stale_w += !(tlb_faults & 1); + stale_r += !(tlb_faults & 2); + } + if (stale_w || stale_r) + ck_panic("tlb_shootdown: FAIL cores=%u stale_write_allowed=%u stale_read_allowed=%u", n, stale_w, stale_r); + ck_printf("tlb_shootdown: ok cores=%u stale_write_allowed=0 stale_read_allowed=0 shootdowns=%u acks=%u\n", n, + tlb_shootdowns, tlb_acked); +} + static void wx_probes(void) { uint64_t efer = ck_rdmsr(MSR_EFER), cr0 = ck_rd_cr0(); @@ -1509,17 +1655,7 @@ static __attribute__((noreturn)) void kernel_main(void *arg) /* The guard page faults, and the fault is contained. */ uint64_t g = (uint64_t)(uintptr_t)kstack; - probe.hit = 0; - probe.armed = 1; - __asm__ volatile("lea 1f(%%rip), %%rax\n\t" - "mov %%rax, %0\n\t" - "movb (%1), %%al\n" - "1:" - : "=m"(probe.resume) - : "r"(g) - : "rax", "memory"); - probe.armed = 0; - if (probe.hit && probe.cr2 == g && !(probe.err & 1)) + if (probe_read(g)) ck_printf("guard_page: ok fault=contained (#PF cr2=0x%llx error=0x%llx, not present)\n", (unsigned long long)probe.cr2, (unsigned long long)probe.err); else @@ -1561,6 +1697,7 @@ static __attribute__((noreturn)) void kernel_main(void *arg) sr.listed, sr.started, sr.failed, ar.id, sr.started ? ids : "none", (unsigned long long)tramp_pa, draw_on ? "drawing" : "halted", draw_helpers); } + tlb_selftest(); const struct ck_x86_storage_result *st = ck_x86_storage(h->rsdp, &frames, tsc_hz, mm.top); ck_x86_sound(&frames, tsc_hz, night_t0); /* before the keyboard: the chirp is already under way */ @@ -1591,7 +1728,8 @@ void ck_x86_kernel_entry(struct ck_handoff *h) hand = h; tables_load(); ck_puts("\nreport_kind: kernel\n"); -#if defined(CK_X86_WX_MUTANT_NO_NX) || defined(CK_X86_WX_MUTANT_TEXT_RW) || defined(CK_X86_WX_MUTANT_AP_NO_WP) +#if defined(CK_X86_WX_MUTANT_NO_NX) || defined(CK_X86_WX_MUTANT_TEXT_RW) || defined(CK_X86_WX_MUTANT_AP_NO_WP) || \ + defined(CK_X86_WX_MUTANT_NO_SHOOTDOWN) ck_puts("wx: TEST-ONLY W^X mutant build, page permissions deliberately broken, never deploy\n"); #endif ck_printf("exit_boot_services: ok attempts=%u\n", h->exit_attempts); diff --git a/scripts/.crumb b/scripts/.crumb index aefc6da8..0a4db8e5 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-11-06T01:37:45Z", + "compiled_at": "2026-11-06T02:51:44Z", "compiler": "crumb-compile/1", - "digest": "sha256:e8aeac2d60e55081ae3c39f89c4946cd8f9f6994f97883281f41fcf1a3555c68", + "digest": "sha256:c7893c9928c69881c17c653c9b31c3b699166a29fa827d48a0f9a5c5d68d101f", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 140, - "generated_at_commit": "b079990e34b1ac0ce69894b065d83e23bae56676", - "source_tree": "sha256:41348c109b6e0a62e5ee4f9b85adb2f4e5df6f5e89af2c80a89486858ced2916" + "generated_at_commit": "003c74c5f8604a67429b6e3b9dd8fee47ea9ea53", + "source_tree": "sha256:7b1a0e4740d6a42ffe7340c7d0ace28b809b853913020be7a0faf4d753505bc0" }, "owner_lane": "", "provenance": { diff --git a/scripts/qemu_ck_imac_kernel_test.sh b/scripts/qemu_ck_imac_kernel_test.sh index 2cb5e4d8..a04a5c0f 100755 --- a/scripts/qemu_ck_imac_kernel_test.sh +++ b/scripts/qemu_ck_imac_kernel_test.sh @@ -34,6 +34,10 @@ # writable and executable, NXE and WP on every core). Three TEST-ONLY mutant builds # (IMACKERN_WX_MUTANT=no_nx, =text_writable at -smp 1; =ap_no_wp, secondary cores # without CR0.WP, at -smp 4) must make those checks panic. +# TLB shootdown (#485): each run must show every secondary core's stale writable +# and stale present translation of a page gone after the boot core made it +# read-only and then unmapped it; the TEST-ONLY mutant no_shootdown (invlpg on the +# boot core only, -smp 4) must make that probe panic. # The kernel's report reaches the serial log through COM1, which QEMU has and the # iMac does not. Needs qemu-system-x86_64, OVMF, mtools and an x86_64 gcc. Final line: # AIENOS_IMAC_KERNEL: PASS|FAIL|NOT_RUN [(...)] exit 0 / 1 / 2 (tools) / 3 (NOT_RUN) @@ -135,6 +139,7 @@ run() { # name, -cpu model, expected APIC mode chk "${name}: data disk identified" "${txt}" "^ahci_disk: ctrl=0 disk=1 port=1 link=gen[0-9] model=\"QEMU HARDDISK\" blocks=131072 block=512 mib=64 access=read-only\$" chk "${name}: data disk AIENOS partition where written" "${txt}" "^ahci_gpt: ctrl=0 disk=1 gpt=valid used=3 aienos first_lba=${a_first} last_lba=${a_last}\$" chk "${name}: three secondary cores started, drawing the night" "${txt}" "^smp: cpus=4 started=3 failed=0 bsp_id=0 ap_ids=[0-9]+,[0-9]+,[0-9]+ trampoline=0x[0-9a-f]+ state=drawing helpers=[0-2]\$" + chk "${name}: TLB shootdown reaches every secondary core" "${txt}" '^tlb_shootdown: ok cores=3 stale_write_allowed=0 stale_read_allowed=0 shootdowns=14 acks=36$' chk "${name}: NVMe controller found" "${txt}" "^pci: nvme [0-9a-f:.]+ id=1b36:0010 " chk "${name}: NVMe disk identified" "${txt}" "^nvme_disk: ctrl=0 disk=0 version=1\\.[0-9]+ nsid=1 blocks=65536 block=512 mib=32 access=read-only\$" chk "${name}: NVMe AIENOS partition where written" "${txt}" "^nvme_gpt: ctrl=0 disk=0 gpt=valid used=3 aienos first_lba=${n_first} last_lba=${n_last}\$" @@ -175,8 +180,8 @@ run() { # name, -cpu model, expected APIC mode } run max max xapic run qemu64 qemu64 xapic -# W^X negative controls: the probes run before the clock is measured, so a mutant's -# panic halts instead of powering off; wait for the panic line, then stop QEMU. +# W^X and TLB negative controls: the W^X probes run before the clock is measured, so +# a mutant's panic may halt instead of powering off; wait for the panic line, then stop QEMU. mutant() { # IMACKERN_WX_MUTANT value, expected panic pattern, QEMU core count (default 1) local m="$1" mo="${out}-wx-$1" mi="${work}/wx-$1.img" log="${work}/wx-$1.log" hit=0 smp="${3:-1}" make -s -C native/kernel X86_CROSS="${cross}" OUT="${mo}" AIENOS_COMMIT="${commit}" IMACKERN_WX_MUTANT="${m}" imac-kernel >/dev/null @@ -201,7 +206,7 @@ mutant() { # IMACKERN_WX_MUTANT value, expected panic pattern, QEMU core count ( qpid="" if [[ ${hit} == 1 ]] && tr -d '\r' <"${log}" | grep -aq '^wx: TEST-ONLY W^X mutant build'; then echo "PASS mutant ${m}: W^X probes catch it" - tr -d '\r' <"${log}" | grep -a '^panic: wx' + tr -d '\r' <"${log}" | grep -aE -- "$2" else echo "FAIL mutant ${m}: no match for: $2"; fails=$((fails + 1)) tr -d '\r' <"${log}" | tail -20 @@ -209,8 +214,11 @@ mutant() { # IMACKERN_WX_MUTANT value, expected panic pattern, QEMU core count ( } mutant no_nx '^panic: wx: FAIL nx=on wp=on text_write=fault ro_write=fault heap_exec=ALLOWED stack_exec=ALLOWED$' mutant text_writable '^panic: wx: FAIL nx=on wp=on text_write=ALLOWED ro_write=fault heap_exec=fault stack_exec=fault$' -# The secondary cores start without CR0.WP (#484): the boot core's probes pass, so the failure must be the -# wx_audit core count, 1 of 4 cores with NXE and WP, after all three cores started. -mutant ap_no_wp '^panic: wx_audit: FAIL wx_pages=0 exec_pages=([1-9][0-9]*) text_pages=\1 exec_outside_text=0 ro_writable=0 cores_nx_wp=1/4$' 4 +mutant no_shootdown '^panic: tlb_shootdown: FAIL cores=3 stale_write_allowed=3 stale_read_allowed=3$' 4 +# The secondary cores start without CR0.WP (#484): the boot core's probes pass. The TLB shootdown probe (#485) +# runs on the secondary cores before wx_audit, and without WP their writes to the page made read-only succeed +# while the unmapped page still faults: write side fails on all three cores, read side holds. (Without the +# shootdown probe, the wx_audit core count, 1 of 4 cores with NXE and WP, catches the same mutant.) +mutant ap_no_wp '^panic: tlb_shootdown: FAIL cores=3 stale_write_allowed=3 stale_read_allowed=0$' 4 if [[ ${fails} == 0 ]]; then echo "${tag}: PASS"; exit 0; fi echo "${tag}: FAIL (${fails} checks)"; exit 1