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

GitHub aien-dev/aienux#225 (code only)

closed derived df4da515cef78e82 #225

Imported from GitHub aien-dev/aienux#225 (code only)

GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed).

Base 4a116e50581e36e8b90c9f4a3f04f7863798492b · Head 818697eb6c59003c5b44fa82e0d5c7cc55f7bd4a

Apply it

git checkout -b patch-df4da515cef78e82 4a116e50581e36e8b90c9f4a3f04f7863798492b
curl -fsSL https://interplane.io/drakestapleton/aienux/patches/df4da515cef78e82.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/aienux
225
818697eb6c59003c5b44fa82e0d5c7cc55f7bd4a
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":225,"url":"https://github.com/aien-dev/aienux/pull/225","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:00Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/native/boot/handoff.h b/native/boot/handoff.h
index 8a93e68b..ad08e381 100644
--- a/native/boot/handoff.h
+++ b/native/boot/handoff.h
@@ -5,6 +5,7 @@
 #define AIENOS_CK_HANDOFF_H
 
 #include <stdint.h>
+#include <stddef.h>
 
 #define CK_HANDOFF_MAGIC 0x31464f444e414843ull /* "CHANDOF1" */
 
@@ -27,6 +28,26 @@ struct ck_handoff {
     const char *commit;
 };
 
+/* Layout pins for the frozen CHANDOF1 v1 record (docs/BOOT_HANDOFF_CONTRACT.md
+ * section 3, LP64): a reorder, resize or new field fails the build. */
+_Static_assert(sizeof(struct ck_handoff) == 112, "ck_handoff size");
+_Static_assert(offsetof(struct ck_handoff, magic) == 0, "ck_handoff.magic offset");
+_Static_assert(offsetof(struct ck_handoff, firmware_el) == 8, "ck_handoff.firmware_el offset");
+_Static_assert(offsetof(struct ck_handoff, desc_version) == 12, "ck_handoff.desc_version offset");
+_Static_assert(offsetof(struct ck_handoff, firmware_ttbr0) == 16, "ck_handoff.firmware_ttbr0 offset");
+_Static_assert(offsetof(struct ck_handoff, firmware_sctlr) == 24, "ck_handoff.firmware_sctlr offset");
+_Static_assert(offsetof(struct ck_handoff, rsdp) == 32, "ck_handoff.rsdp offset");
+_Static_assert(offsetof(struct ck_handoff, memory_map) == 40, "ck_handoff.memory_map offset");
+_Static_assert(offsetof(struct ck_handoff, map_size) == 48, "ck_handoff.map_size offset");
+_Static_assert(offsetof(struct ck_handoff, desc_size) == 56, "ck_handoff.desc_size offset");
+_Static_assert(offsetof(struct ck_handoff, image_base) == 64, "ck_handoff.image_base offset");
+_Static_assert(offsetof(struct ck_handoff, image_end) == 72, "ck_handoff.image_end offset");
+_Static_assert(offsetof(struct ck_handoff, counter_freq_hz) == 80, "ck_handoff.counter_freq_hz offset");
+_Static_assert(offsetof(struct ck_handoff, uefi_entry_ticks) == 88, "ck_handoff.uefi_entry_ticks offset");
+_Static_assert(offsetof(struct ck_handoff, exit_attempts) == 96, "ck_handoff.exit_attempts offset");
+_Static_assert(offsetof(struct ck_handoff, reserved0) == 100, "ck_handoff.reserved0 offset");
+_Static_assert(offsetof(struct ck_handoff, commit) == 104, "ck_handoff.commit offset");
+
 /* Kernel core entry, still at the firmware EL with firmware translation. */
 __attribute__((noreturn)) void ck_kernel_entry(struct ck_handoff *h);
 
diff --git a/native/kernel/Makefile b/native/kernel/Makefile
index f0cf3eb1..cc787a10 100644
--- a/native/kernel/Makefile
+++ b/native/kernel/Makefile
@@ -27,6 +27,8 @@
 #   make store-image     host tool $(OUT)/host/ck_store_image: signed artifacts
 #                        into the sealed Store on a raw NVMe disk image
 #   make test            host unit tests            -> CK_CORE_HOST: PASS
+#                        (also builds the handoff_check reserved0 mutant
+#                        and requires test_handoff to FAIL against it)
 #   make sanitize        same under ASan + UBSan    -> CK_CORE_HOST: PASS
 #   make stage-test | stage-sanitize | stage-free   stage host tests (stage.mk)
 #   make print-kcflags   flags stage code must be built with
@@ -157,7 +159,7 @@ KLDFLAGS := -nostdlib -pie --no-dynamic-linker -z norelro -z noexecstack -z max-
 CORE_C := $(BOOT)/efi_main.c \
 	$(KDIR)/arch/exception.c $(KDIR)/arch/gic.c $(KDIR)/arch/timer.c \
 	$(KDIR)/core/acpi.c $(KDIR)/core/console.c $(KDIR)/core/fmt.c \
-	$(KDIR)/core/kmain.c $(KDIR)/core/libc_min.c $(KDIR)/core/report.c \
+	$(KDIR)/core/kmain.c $(KDIR)/core/handoff_check.c $(KDIR)/core/libc_min.c $(KDIR)/core/report.c \
 	$(KDIR)/core/smmu.c $(KDIR)/core/smmu_svc.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c \
 	$(KDIR)/core/m3.c $(KDIR)/core/entropy.c $(KDIR)/arch/rndr.c \
 	$(KDIR)/mm/frames.c $(KDIR)/mm/heap.c $(KDIR)/mm/mmu.c $(KDIR)/mm/pt.c
@@ -317,9 +319,10 @@ $(FULL_EFI): $(FULL_ELF)
 
 # ---- host unit tests ----
 HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/core/smmu.c $(KDIR)/mm/frames.c \
-	$(KDIR)/mm/pt.c $(KDIR)/mm/heap.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c $(KDIR)/core/entropy.c
-TESTS := test_fmt test_frames test_pt test_acpi test_heap test_smmu test_sched test_ipc test_artifact test_entropy
-HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests
+	$(KDIR)/mm/pt.c $(KDIR)/mm/heap.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c $(KDIR)/core/entropy.c \
+	$(KDIR)/core/handoff_check.c
+TESTS := test_fmt test_frames test_pt test_acpi test_heap test_smmu test_sched test_ipc test_artifact test_entropy test_handoff
+HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests -I$(BOOT)
 SANFLAGS := -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer
 
 $(OUT)/host/%: $(KDIR)/tests/%.c $(HOST_SRCS) $(KDIR)/tests/ck_test.h $(HDRS)
@@ -330,8 +333,20 @@ $(OUT)/host-san/%: $(KDIR)/tests/%.c $(HOST_SRCS) $(KDIR)/tests/ck_test.h $(HDRS
 	@mkdir -p $(dir $@)
 	$(HOSTCC) $(HCFLAGS) $(SANFLAGS) -o $@ $< $(HOST_SRCS)
 
-test: $(addprefix $(OUT)/host/,$(TESTS))
-	@fail=0; for t in $^; do $$t || fail=1; done; \
+# TEST-ONLY mutant of core/handoff_check.c (reserved0 check removed): the
+# handoff test must FAIL against it, so "make test" fails if the test stops
+# catching the mutation. Host only (handoff_check.c refuses it freestanding
+# and with CK_HARDWARE_STAGING); never part of an image.
+$(OUT)/host/test_handoff_mutant: $(KDIR)/tests/test_handoff.c $(HOST_SRCS) $(KDIR)/tests/ck_test.h $(HDRS)
+	@mkdir -p $(dir $@)
+	$(HOSTCC) $(HCFLAGS) -DCK_HANDOFF_MUTANT_SKIP_RESERVED -o $@ $< $(HOST_SRCS)
+
+handoff-mutant: $(OUT)/host/test_handoff_mutant
+	@if $< > $<.log 2>&1; then cat $<.log; echo "test_handoff mutant (reserved0 check removed): PASSED, test does not catch it"; exit 1; \
+	else grep -F "test_handoff: FAIL" $<.log && echo "test_handoff mutant (reserved0 check removed): FAILS as required"; fi
+
+test: $(addprefix $(OUT)/host/,$(TESTS)) handoff-mutant
+	@fail=0; for t in $(filter-out handoff-mutant,$^); do $$t || fail=1; done; \
 	if [ $$fail = 0 ]; then echo "CK_CORE_HOST: PASS"; else echo "CK_CORE_HOST: FAIL"; exit 1; fi
 
 sanitize: $(addprefix $(OUT)/host-san/,$(TESTS))
@@ -393,4 +408,4 @@ clean:
 	rm -rf $(OUT)
 
 FORCE:
-.PHONY: all full test sanitize clean print-kcflags udp-echo tool store-image owner-gen FORCE
+.PHONY: all full test sanitize handoff-mutant clean print-kcflags udp-echo tool store-image owner-gen FORCE
diff --git a/native/kernel/core/handoff_check.c b/native/kernel/core/handoff_check.c
new file mode 100644
index 00000000..dddd2b5c
--- /dev/null
+++ b/native/kernel/core/handoff_check.c
@@ -0,0 +1,54 @@
+/* handoff_check.c -- refuse a malformed loader -> kernel handoff record
+ * before the kernel trusts any field of it (docs/BOOT_HANDOFF_CONTRACT.md
+ * section 6). Checked here: NULL record, magic, reserved0, descriptor
+ * stride and version, memory map size. The other PROPOSED refusals of the
+ * contract (image range, overlaps, rsdp, firmware_el, CHANDOF<n> version
+ * line) are not part of this cut. */
+#include "handoff_check.h"
+#include "fmt.h"
+
+/* TEST-ONLY mutant (tests: make test builds it and requires it to FAIL).
+ * Never in a kernel image or a hardware staging build. */
+#ifdef CK_HANDOFF_MUTANT_SKIP_RESERVED
+#if !__STDC_HOSTED__
+#error "CK_HANDOFF_MUTANT_SKIP_RESERVED is a host test mutant only"
+#endif
+#ifdef CK_HARDWARE_STAGING
+#error "CK_HANDOFF_MUTANT_SKIP_RESERVED cannot be combined with CK_HARDWARE_STAGING"
+#endif
+#endif
+
+static char why_buf[64];
+
+static int refuse(const char **why, const char *what, unsigned long long n)
+{
+    ck_snprintf(why_buf, sizeof why_buf, "%s %llu", what, n);
+    *why = why_buf;
+    return -1;
+}
+
+int ck_handoff_check(const struct ck_handoff *h, const char **why)
+{
+    if (!h) {
+        *why = "null record";
+        return -1;
+    }
+    if (h->magic != CK_HANDOFF_MAGIC) {
+        *why = "bad magic";
+        return -1;
+    }
+#ifndef CK_HANDOFF_MUTANT_SKIP_RESERVED
+    if (h->reserved0 != 0) {
+        *why = "reserved field nonzero";
+        return -1;
+    }
+#endif
+    if (h->desc_size < CK_HANDOFF_DESC_MIN || h->desc_size % 8 != 0)
+        return refuse(why, "bad descriptor size", (unsigned long long)h->desc_size);
+    if (h->desc_version != CK_HANDOFF_DESC_VERSION)
+        return refuse(why, "bad descriptor version", (unsigned long long)h->desc_version);
+    if (h->map_size == 0 || h->map_size > CK_HANDOFF_MAP_MAX || h->map_size % h->desc_size != 0)
+        return refuse(why, "bad map size", (unsigned long long)h->map_size);
+    *why = 0;
+    return 0;
+}
diff --git a/native/kernel/core/handoff_check.h b/native/kernel/core/handoff_check.h
new file mode 100644
index 00000000..4c6ddccd
--- /dev/null
+++ b/native/kernel/core/handoff_check.h
@@ -0,0 +1,23 @@
+/* handoff_check.h -- pure validator of the loader -> kernel handoff record
+ * (docs/BOOT_HANDOFF_CONTRACT.md section 6, CHANDOF1 v1). No I/O, no
+ * globals the caller can see; host tested by tests/test_handoff.c. */
+#ifndef AIENOS_CK_HANDOFF_CHECK_H
+#define AIENOS_CK_HANDOFF_CHECK_H
+
+#include "handoff.h"
+
+/* Largest memory map copy a loader may hand over (efi_main.c MAP_BYTES). */
+#define CK_HANDOFF_MAP_MAX (64u << 10)
+/* Smallest UEFI descriptor stride the kernel parses (struct ck_efi_desc,
+ * mm/frames.h). */
+#define CK_HANDOFF_DESC_MIN 40u
+/* UEFI EFI_MEMORY_DESCRIPTOR_VERSION. */
+#define CK_HANDOFF_DESC_VERSION 1u
+
+/* Returns 0 when h is an acceptable CHANDOF1 record. Otherwise returns -1
+ * and sets *why to the refusal reason without the "handoff: " prefix
+ * (for example "bad magic"); the kernel panics with "handoff: <why>".
+ * *why may point at a static buffer overwritten by the next call. */
+int ck_handoff_check(const struct ck_handoff *h, const char **why);
+
+#endif
diff --git a/native/kernel/core/kmain.c b/native/kernel/core/kmain.c
index 005387a2..156cf7c7 100644
--- a/native/kernel/core/kmain.c
+++ b/native/kernel/core/kmain.c
@@ -10,6 +10,7 @@
 #include "arch.h"
 #include "ck_internal.h"
 #include "entropy.h"
+#include "handoff_check.h"
 
 #define TTBR_BADDR_MASK 0x0000fffffffffffeull
 
@@ -171,8 +172,9 @@ void ck_kernel_entry(struct ck_handoff *h)
     if (el == 2)
         __asm__ volatile("brk #0x2");
 #endif
-    if (h->magic != CK_HANDOFF_MAGIC)
-        ck_panic("handoff: bad magic");
+    const char *why;
+    if (ck_handoff_check(h, &why) != 0)
+        ck_panic("handoff: %s", why);
     ck_psci_configure(ck_acpi_find("FACP"));
     ck_set_stage("mm_build");
     uint64_t sp = ck_mm_build(h);
diff --git a/native/kernel/tests/test_handoff.c b/native/kernel/tests/test_handoff.c
new file mode 100755
index 00000000..b3c271c5
--- /dev/null
+++ b/native/kernel/tests/test_handoff.c
@@ -0,0 +1,143 @@
+/* test_handoff.c -- host tests of core/handoff_check.c against the handoff
+ * contract (docs/BOOT_HANDOFF_CONTRACT.md sections 6 and 9).
+ *
+ * Positive control: a record filled the way native/boot/efi_main.c fills it
+ * (efi_main.c:60-76 and 126-129, static so reserved0 is zero) with a QEMU
+ * AAVMF style map (48-byte descriptors, version 1; UNVERIFIED stand-in
+ * values, the real boot is proven by scripts/qemu_ck_boot_test.sh).
+ * One negative case per refusal reason, each checked for the exact
+ * "handoff: <reason>" line the kernel panics with.
+ *
+ * Mutation: built with -DCK_HANDOFF_MUTANT_SKIP_RESERVED the validator
+ * skips the reserved0 check; this test then FAILS (reserved0 = 1 is
+ * accepted). "make test" builds that mutant and requires it to fail. */
+#include <string.h>
+#include "ck_test.h"
+#include "handoff_check.h"
+
+static uint64_t map_buf[(64u << 10) / 8];
+
+static struct ck_handoff good(void)
+{
+    static const char commit[] = "test";
+    struct ck_handoff h;
+    memset(&h, 0, sizeof h); /* efi_main.c: static handoff, BSS zeroed */
+    h.magic = CK_HANDOFF_MAGIC;
+    h.firmware_el = 2;
+    h.uefi_entry_ticks = 123456;
+    h.counter_freq_hz = 62500000;
+    h.firmware_ttbr0 = 0x13ffff000ull;
+    h.firmware_sctlr = 0x30c5183dull;
+    h.image_base = 0x13c000000ull;
+    h.image_end = 0x13c080000ull;
+    h.commit = commit;
+    h.memory_map = (uint64_t)(uintptr_t)map_buf;
+    h.rsdp = 0x13f5f0018ull;
+    h.desc_size = 48;
+    h.desc_version = 1;
+    h.map_size = 48 * 120;
+    h.exit_attempts = 1;
+    return h;
+}
+
+/* Runs the validator and returns the panic line the kernel would print
+ * ("" on acceptance). */
+static const char *line(const struct ck_handoff *h)
+{
+    static char buf[96];
+    const char *why = (const char *)1;
+    if (ck_handoff_check(h, &why) == 0)
+        return why == 0 ? "" : "accepted but why set";
+    snprintf(buf, sizeof buf, "handoff: %s", why ? why : "(null why)");
+    return buf;
+}
+
+static void expect(const struct ck_handoff *h, const char *want)
+{
+    const char *got = line(h);
+    int ok = strcmp(got, want) == 0;
+    CHECK(ok);
+    if (!ok)
+        printf("    want \"%s\" got \"%s\"\n", want, got);
+}
+
+int main(void)
+{
+    struct ck_handoff h;
+
+    /* 13. positive control; the full 64 KiB map buffer is also accepted. */
+    h = good();
+    expect(&h, "");
+    h.map_size = 48 * 1365; /* 65520, largest multiple of 48 <= 64 KiB */
+    expect(&h, "");
+    h = good();
+    h.firmware_el = 1;
+    expect(&h, "");
+
+    /* 1. NULL record */
+    expect(0, "handoff: null record");
+
+    /* 2. bad magic: one bit off; CHANDOF2 (refused as bad magic in this cut) */
+    h = good();
+    h.magic ^= 1;
+    expect(&h, "handoff: bad magic");
+    h = good();
+    h.magic = 0x32464f444e414843ull;
+    expect(&h, "handoff: bad magic");
+    h = good();
+    h.magic = 0;
+    expect(&h, "handoff: bad magic");
+
+    /* 3. reserved0 nonzero (the mutant skips this; must FAIL then) */
+    h = good();
+    h.reserved0 = 1;
+    expect(&h, "handoff: reserved field nonzero");
+    h = good();
+    h.reserved0 = 0x80000000u;
+    expect(&h, "handoff: reserved field nonzero");
+
+    /* 4. descriptor size 0, 39, 41 (not a multiple of 8), 44 */
+    h = good();
+    h.desc_size = 0;
+    expect(&h, "handoff: bad descriptor size 0");
+    h.desc_size = 39;
+    expect(&h, "handoff: bad descriptor size 39");
+    h.desc_size = 41;
+    expect(&h, "handoff: bad descriptor size 41");
+    h.desc_size = 44;
+    expect(&h, "handoff: bad descriptor size 44");
+    h.desc_size = 40; /* smallest accepted stride; 48*120 is a multiple of 40 */
+    expect(&h, "");
+
+    /* 5. descriptor version 0 and 2 */
+    h = good();
+    h.desc_version = 0;
+    expect(&h, "handoff: bad descriptor version 0");
+    h.desc_version = 2;
+    expect(&h, "handoff: bad descriptor version 2");
+
+    /* 6. map size 0, not a multiple of desc_size, 65544 (> 64 KiB) */
+    h = good();
+    h.map_size = 0;
+    expect(&h, "handoff: bad map size 0");
+    h.map_size = 48 * 120 + 8;
+    expect(&h, "handoff: bad map size 5768");
+    h.desc_size = 21848; /* 65544 = 3 * 21848: refused only for > 64 KiB */
+    h.map_size = 65544;
+    expect(&h, "handoff: bad map size 65544");
+    h.desc_size = 64; /* boundary: exactly 64 KiB is accepted */
+    h.map_size = 65536;
+    expect(&h, "");
+    h.desc_size = 65544; /* stride larger than 64 KiB: one descriptor cannot fit */
+    h.map_size = 65544;
+    expect(&h, "handoff: bad map size 65544");
+
+    /* Check order: the first failing field is reported. */
+    h = good();
+    h.magic = 0;
+    h.reserved0 = 1;
+    h.map_size = 0;
+    expect(&h, "handoff: bad magic");
+
+    return ck_t_verdict("test_handoff");
+}