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#288 (code only)

closed derived 3d822191c7d5f0db #288

Imported from GitHub aien-dev/aienux#288 (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 bc16c1414f439713f85019fa5684bdd09d884fe6 · Head 2afc6d54ce69b376a3d5bb3bc3a23a1b25293390

Apply it

git checkout -b patch-3d822191c7d5f0db bc16c1414f439713f85019fa5684bdd09d884fe6
curl -fsSL https://interplane.io/drakestapleton/aienux/patches/3d822191c7d5f0db.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
288
2afc6d54ce69b376a3d5bb3bc3a23a1b25293390
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":288,"url":"https://github.com/aien-dev/aienux/pull/288","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:08Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/.crumb b/.crumb
index 626f2370..182f944e 100644
--- a/.crumb
+++ b/.crumb
@@ -28,32 +28,32 @@
         },
         {
           "digest": "11efd71d613be5ca1fb6405abb1688d6d3f72fd1ac1e5dc8e091734d0f2688a0",
-          "last_commit": "0f70efd",
+          "last_commit": "bc16c14",
           "name": "docs",
           "purpose": "Directory docs. Purpose not yet described by a human."
         },
         {
-          "digest": "fcfb41da00d8ff2d8162483a8bc323cdf31e6e30dddc05b90f481535e53ef61f",
-          "last_commit": "c250fd2",
+          "digest": "cda106fcac6686d3f71dbc0782d7ed769f43853aaa246b68b6690c9f0dc45623",
+          "last_commit": "422738f",
           "name": "native",
           "purpose": "Directory native. Purpose not yet described by a human."
         },
         {
           "digest": "c53533f3712e26103f053602b917b580a6ac839b71f7ecab13fae29e49c0aa50",
-          "last_commit": "fa209e4",
+          "last_commit": "e342b8e",
           "name": "scripts",
           "purpose": "Directory scripts. Purpose not yet described by a human."
         }
       ],
-      "children_root": "sha256:b18f53d6f3866ed04e705e68e737b98e0bf8b95297ac63bec2c64873c3d144e2",
-      "compiled_at": "2026-10-08T15:58:17Z",
+      "children_root": "sha256:50d08d232b7670d7ce158a757fbb4c0d9ce398b858df3da24daf806c3763b907",
+      "compiled_at": "2026-10-08T16:44:45Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:a45ee1b6377453711868dd83c5dab79daea2bc2e8edf9d57dc50e851c2b0490f",
+      "digest": "sha256:88789e3f30fe7824920988fc099cd756a5746d3ed39bc9d2fde1b5ff491e1e97",
       "evidence_files": 153,
       "evidence_root": "sha256:29aa8775feb12d9e0e64b41fc1fc48b97e32dc16bf9176562cbe9fe1ea934f00",
       "files": 3024,
-      "generated_at_commit": "2835db1cbb0117521468f3ab12e5396a2ad4f3f3",
-      "source_tree": "sha256:0cfe70787e4c9405fd19d9785ca373ae07bd4cafe715e4e53ee431bf30c1cad6"
+      "generated_at_commit": "422738fd6f052883b886b2cf1c7c3db6608ccfb7",
+      "source_tree": "sha256:22b664cc85efa599d2e9b8b2b4e1374144a948dc6dc9e7927ab4348c6041989b"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/native/.crumb b/native/.crumb
index 9e6f89d5..68739888 100644
--- a/native/.crumb
+++ b/native/.crumb
@@ -79,8 +79,8 @@
           "purpose": "Directory native/disk. Purpose not yet described by a human."
         },
         {
-          "digest": "c28edd6ebdf3c8e7285402d71efea9c5576429e7fb024dd79152f01f054866b0",
-          "last_commit": "c250fd2",
+          "digest": "e65b2d175f1b762831dd6410f79943676b5818b70941ffd301f31e9108e9f706",
+          "last_commit": "422738f",
           "name": "kernel",
           "purpose": "Directory native/kernel. Purpose not yet described by a human."
         },
@@ -109,15 +109,15 @@
           "purpose": "Directory native/store. Purpose not yet described by a human."
         }
       ],
-      "children_root": "sha256:08488a453b23e5b8836cffd54ceae1beb91d8307612299ff5c19354ebff14934",
-      "compiled_at": "2026-10-08T15:58:17Z",
+      "children_root": "sha256:e20d501a3d52d7a1f73600393e36f14f75af472e37cf49b25d0486dec2d6dc52",
+      "compiled_at": "2026-10-08T16:44:45Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:fcfb41da00d8ff2d8162483a8bc323cdf31e6e30dddc05b90f481535e53ef61f",
+      "digest": "sha256:cda106fcac6686d3f71dbc0782d7ed769f43853aaa246b68b6690c9f0dc45623",
       "evidence_files": 4,
       "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335",
       "files": 451,
-      "generated_at_commit": "2835db1cbb0117521468f3ab12e5396a2ad4f3f3",
-      "source_tree": "sha256:c3e7e36669876b9d4f2699ded3a2593f547ef6ca44b75e86411245ca5668791d"
+      "generated_at_commit": "422738fd6f052883b886b2cf1c7c3db6608ccfb7",
+      "source_tree": "sha256:20b7c1fdc08dfc4ec90904e9df49941753563f540b179abf6d023a670a451a90"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/native/kernel/.crumb b/native/kernel/.crumb
index 50c2bac8..2fc6946a 100644
--- a/native/kernel/.crumb
+++ b/native/kernel/.crumb
@@ -58,13 +58,13 @@
         },
         {
           "digest": "ef1dd930dc3a359952e25dfc3c6d4c7a3a9ef3113bc2bd61c82efb8018f4dec3",
-          "last_commit": "827e20f",
+          "last_commit": "3b26ea9",
           "name": "core",
           "purpose": "Directory native/kernel/core. Purpose not yet described by a human."
         },
         {
-          "digest": "21bbcdddd7bb19bdc26b94e33495527555ce09589e355dbe233a39edc2bb3448",
-          "last_commit": "c250fd2",
+          "digest": "aaa36df49a59761f649b0b950dddd15aea369a9e13022a2cd5c7920944309836",
+          "last_commit": "422738f",
           "name": "dev",
           "purpose": "Directory native/kernel/dev. Purpose not yet described by a human."
         },
@@ -81,14 +81,14 @@
           "purpose": "Directory native/kernel/mm. Purpose not yet described by a human."
         },
         {
-          "digest": "f2a5c970c1c337189c6c1251edb501cbb77da5139043d3b4f3187d03bf37e725",
-          "last_commit": "c250fd2",
+          "digest": "bdd303a1a11459ede12f8a52d5db34b92a13ad968dc42d2329bf80e43bff7b0b",
+          "last_commit": "422738f",
           "name": "svc",
           "purpose": "Directory native/kernel/svc. Purpose not yet described by a human."
         },
         {
           "digest": "a2824603eee1429e7fe251a0051d43ec3d3822ba7f3796d2d554c359f0588ed0",
-          "last_commit": "827e20f",
+          "last_commit": "3b26ea9",
           "name": "tests",
           "purpose": "Directory native/kernel/tests. Purpose not yet described by a human."
         },
@@ -99,15 +99,15 @@
           "purpose": "Directory native/kernel/tools. Purpose not yet described by a human."
         }
       ],
-      "children_root": "sha256:c8f64fe5280d1c55049b5260e4bb9306d96d558847eea11c42c2db653164453b",
-      "compiled_at": "2026-10-08T15:58:17Z",
+      "children_root": "sha256:43615fa7069aa84a3e8a65019c501ddbc881f77357fc68cd303905cee041d992",
+      "compiled_at": "2026-10-08T16:44:45Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:c28edd6ebdf3c8e7285402d71efea9c5576429e7fb024dd79152f01f054866b0",
+      "digest": "sha256:e65b2d175f1b762831dd6410f79943676b5818b70941ffd301f31e9108e9f706",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
       "files": 287,
-      "generated_at_commit": "2835db1cbb0117521468f3ab12e5396a2ad4f3f3",
-      "source_tree": "sha256:038bf075bdc8ca7e4783a786a3db03c4f20eb3430fd3802f69e7bb4214eda8b3"
+      "generated_at_commit": "422738fd6f052883b886b2cf1c7c3db6608ccfb7",
+      "source_tree": "sha256:38f184af5c41de10f9e6d1cfd56ffa0bc0f510aa202851d64e508457e0eba7dc"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/native/kernel/dev/.crumb b/native/kernel/dev/.crumb
index ce558072..6d843efe 100644
--- a/native/kernel/dev/.crumb
+++ b/native/kernel/dev/.crumb
@@ -7,14 +7,14 @@
     "generated": {
       "children": [],
       "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "compiled_at": "2026-10-08T15:14:34Z",
+      "compiled_at": "2026-10-08T16:44:45Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:21bbcdddd7bb19bdc26b94e33495527555ce09589e355dbe233a39edc2bb3448",
+      "digest": "sha256:aaa36df49a59761f649b0b950dddd15aea369a9e13022a2cd5c7920944309836",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
       "files": 25,
-      "generated_at_commit": "bf475a089d2824e0c923c42e73f948cc65c87c32",
-      "source_tree": "sha256:19bbff893ccd5342120c5ff800f7141dd8e126733e66be51c1c8958ef24fef8b"
+      "generated_at_commit": "422738fd6f052883b886b2cf1c7c3db6608ccfb7",
+      "source_tree": "sha256:8887d9f55b9524689746f9ad49dd36eefc90096268cf0830736cf3ff7d918697"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/native/kernel/dev/devices.c b/native/kernel/dev/devices.c
index 87800f3e..5479d86c 100644
--- a/native/kernel/dev/devices.c
+++ b/native/kernel/dev/devices.c
@@ -45,6 +45,9 @@ int ck_stage_devices(void)
 {
     int rc = pci_stage_probe(&g_pci);
     if (rc) return rc;
+    /* Report-only multi-segment discovery for the boot log (aienos#286); the
+     * result is ignored, nothing below depends on it. */
+    (void)pci_stage_discover_report();
     /* Post-exit bus-master sweep (Rust dma_gate::sweep_bus_master): before any
      * device is given DMA, clear BME on every endpoint firmware left with it
      * set, so each device starts with DMA off. */
diff --git a/native/kernel/dev/pci.c b/native/kernel/dev/pci.c
index 07f61ad3..b0fc7aef 100644
--- a/native/kernel/dev/pci.c
+++ b/native/kernel/dev/pci.c
@@ -491,3 +491,63 @@ int pci_discover(const pci_bus_access *a, uint16_t segment, pci_found *out, uint
     if (bridges_followed) *bridges_followed = d.bridges;
     return rc;
 }
+
+/* ---- Boot discovery report: every MCFG segment, read only. ---- */
+
+#define PCI_MAX_SEGMENTS 32u
+
+static pci_ecam g_disc_ecam[PCI_MAX_SEGMENTS];
+static pci_found g_disc_found[PCI_MAX_FUNCS];
+
+/* Report-only: prints what firmware declared and what answers, changes no
+ * state. Each segment maps only its declared bus range through the
+ * fail-closed ck_mmio_try_map (a refused map is reported, never retried
+ * another way). pci_discover reads config space and nothing else: no BAR
+ * sizing, no config write. Segment 0 was already mapped by pci_stage_probe;
+ * mapping the same pages again is a no-op. The caller ignores the return
+ * value (it is for the log and tests); binding, sweep and assignment do not
+ * depend on it. */
+int pci_stage_discover_report(void)
+{
+    const uint8_t *mcfg = (const uint8_t *)ck_acpi_find("MCFG");
+    if (!mcfg) {
+        ck_printf("pci_disc: no ACPI MCFG table\n");
+        return PCI_E_NO_MCFG;
+    }
+    uint32_t len = le32(mcfg + 4);
+    uint32_t nseg = 0;
+    int rc = pci_mcfg_parse_all(mcfg, len, g_disc_ecam, PCI_MAX_SEGMENTS, &nseg);
+    if (rc) {
+        ck_printf("pci_disc: MCFG malformed or too large (len=%u rc=%d)\n", len, rc);
+        return rc;
+    }
+    uint32_t scanned = 0, total = 0;
+    for (uint32_t i = 0; i < nseg; i++) {
+        const pci_ecam *e = &g_disc_ecam[i];
+        uint64_t first = e->base + ((uint64_t)e->start_bus << 20);
+        uint64_t bytes = (uint64_t)((uint32_t)e->end_bus - e->start_bus + 1u) << 20;
+        volatile uint8_t *m = (volatile uint8_t *)ck_mmio_try_map(first, (size_t)bytes);
+        if (!m) {
+            ck_printf("pci_disc: seg %04x bus %02x-%02x REFUSED (ecam map refused, base=0x%llx)\n", e->segment,
+                      e->start_bus, e->end_bus, (unsigned long long)e->base);
+            continue;
+        }
+        pci_bus_access a;
+        a.ecam = m;
+        a.start_bus = e->start_bus;
+        a.end_bus = e->end_bus;
+        uint32_t n = 0, br = 0;
+        int drc = pci_discover(&a, e->segment, g_disc_found, PCI_MAX_FUNCS, &n, &br);
+        ck_printf("pci_disc: seg %04x bus %02x-%02x functions=%u bridges=%u rc=%d\n", e->segment, e->start_bus,
+                  e->end_bus, n, br, drc);
+        for (uint32_t j = 0; j < n; j++) {
+            const pci_found *f = &g_disc_found[j];
+            ck_printf("pci_disc: %04x:%02x:%02x.%u %04x:%04x class=%06x rev=%02x\n", f->segment, f->bus, f->dev,
+                      f->fn, f->vendor, f->device, f->class_code, f->revision);
+        }
+        scanned++;
+        total += n;
+    }
+    ck_printf("pci_disc: segments=%u functions=%u (read-only, no config writes)\n", scanned, total);
+    return PCI_OK;
+}
diff --git a/native/kernel/dev/pci.h b/native/kernel/dev/pci.h
index 6a5733d3..5e128fc8 100644
--- a/native/kernel/dev/pci.h
+++ b/native/kernel/dev/pci.h
@@ -182,4 +182,7 @@ const char *pci_class_name(uint32_t class_code);
  * known QEMU virt base (0x4010000000 or 0x3f000000). */
 int pci_stage_probe(pci_system *s);
 
+/* Report-only, read-only multi-segment discovery for the boot log (see pci.c). */
+int pci_stage_discover_report(void);
+
 #endif
diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb
index 2e63fa6c..a93a00b7 100644
--- a/native/kernel/svc/.crumb
+++ b/native/kernel/svc/.crumb
@@ -23,21 +23,21 @@
           "purpose": "Directory native/kernel/svc/compat. Purpose not yet described by a human."
         },
         {
-          "digest": "e20962c560a6c4f4370da0e582830c691a81ef01cf376b8176bfa3bb2ee6e557",
-          "last_commit": "8641977",
+          "digest": "c5124fdba32f7908635c7b7162d6269c623128d16795100a3943418907cc8481",
+          "last_commit": "422738f",
           "name": "tests",
           "purpose": "Directory native/kernel/svc/tests. Purpose not yet described by a human."
         }
       ],
-      "children_root": "sha256:433085fba7ead748420255532396e7ffa382b57cfe8fd4dd217772dc5829d778",
-      "compiled_at": "2026-10-08T15:14:34Z",
+      "children_root": "sha256:a0aabfef5bc931dfbcfee0f925ded07dd1211b8c42616741b692bacbe637d9a4",
+      "compiled_at": "2026-10-08T16:44:45Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:f2a5c970c1c337189c6c1251edb501cbb77da5139043d3b4f3187d03bf37e725",
+      "digest": "sha256:bdd303a1a11459ede12f8a52d5db34b92a13ad968dc42d2329bf80e43bff7b0b",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
       "files": 35,
-      "generated_at_commit": "bf475a089d2824e0c923c42e73f948cc65c87c32",
-      "source_tree": "sha256:4ba0c2b6cbceb7b4be08a248a493bca2d22be438c5ff964204f729dfb181ca94"
+      "generated_at_commit": "422738fd6f052883b886b2cf1c7c3db6608ccfb7",
+      "source_tree": "sha256:d9f525ae43f6a55d65318297b87825e787bcd6f70542c4530f9eaa09bf8d1c12"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/native/kernel/svc/tests/.crumb b/native/kernel/svc/tests/.crumb
index 7555255e..71c9f7ee 100644
--- a/native/kernel/svc/tests/.crumb
+++ b/native/kernel/svc/tests/.crumb
@@ -7,14 +7,14 @@
     "generated": {
       "children": [],
       "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "compiled_at": "2026-10-08T15:14:34Z",
+      "compiled_at": "2026-10-08T16:44:45Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:e20962c560a6c4f4370da0e582830c691a81ef01cf376b8176bfa3bb2ee6e557",
+      "digest": "sha256:c5124fdba32f7908635c7b7162d6269c623128d16795100a3943418907cc8481",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
       "files": 3,
-      "generated_at_commit": "bf475a089d2824e0c923c42e73f948cc65c87c32",
-      "source_tree": "sha256:3fe5e78fba4704feb11507270a818f7a70695056fecc32c73b2b3fe6e544aaed"
+      "generated_at_commit": "422738fd6f052883b886b2cf1c7c3db6608ccfb7",
+      "source_tree": "sha256:5a9a16d8041d363d32bca898729fb55ad189d11e202fd5a4533d8dd93925443b"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/native/kernel/svc/tests/ck_host.c b/native/kernel/svc/tests/ck_host.c
index faa847c3..e499a03a 100644
--- a/native/kernel/svc/tests/ck_host.c
+++ b/native/kernel/svc/tests/ck_host.c
@@ -14,13 +14,40 @@
 
 const void *ck_host_mcfg;
 int ck_host_quiet;
+int ck_host_try_map_ok;
+uint64_t ck_host_try_map_fail;
+
+/* Optional text capture so a test can check what a stage printed. */
+static char cap_buf[32768];
+static size_t cap_len;
+static int cap_on;
+void ck_host_capture_start(void) { cap_len = 0; cap_buf[0] = 0; cap_on = 1; }
+const char *ck_host_capture_text(void) { return cap_buf; }
+void ck_host_capture_stop(void) { cap_on = 0; }
+static void cap_add(const char *s, size_t n)
+{
+    if (!cap_on) return;
+    if (n > sizeof cap_buf - 1 - cap_len) n = sizeof cap_buf - 1 - cap_len;
+    memcpy(cap_buf + cap_len, s, n);
+    cap_len += n;
+    cap_buf[cap_len] = 0;
+}
 
 void ck_puts(const char *s)
 {
+    cap_add(s, strlen(s));
     if (!ck_host_quiet) fputs(s, stdout);
 }
 void ck_vprintf(const char *fmt, va_list ap)
 {
+    if (cap_on) {
+        char tmp[512];
+        va_list c;
+        va_copy(c, ap);
+        int n = vsnprintf(tmp, sizeof tmp, fmt, c);
+        va_end(c);
+        if (n > 0) cap_add(tmp, (size_t)n < sizeof tmp ? (size_t)n : sizeof tmp - 1);
+    }
     if (!ck_host_quiet) vprintf(fmt, ap);
 }
 void ck_printf(const char *fmt, ...)
@@ -57,11 +84,14 @@ volatile void *ck_mmio_map(uint64_t phys, size_t len)
     (void)len;
     return (volatile void *)(uintptr_t)phys;
 }
-/* Host: no platform MMIO (the ACPI platform xHCI list is always empty). */
+/* Host: no platform MMIO by default (the ACPI platform xHCI list is always
+ * empty). A test may turn on identity mapping (ck_host_try_map_ok) and make
+ * one exact address fail (ck_host_try_map_fail). */
 volatile void *ck_mmio_try_map(uint64_t phys, size_t len)
 {
-    (void)phys; (void)len;
-    return NULL;
+    (void)len;
+    if (!ck_host_try_map_ok || (ck_host_try_map_fail && phys == ck_host_try_map_fail)) return NULL;
+    return (volatile void *)(uintptr_t)phys;
 }
 /* Host: no DSDT reachable (no FADT in ck_acpi_find). */
 int ck_acpi_platform_devices(const char *const *ids, unsigned nids, struct ck_platform_dev *out, unsigned max,
diff --git a/native/kernel/svc/tests/ck_host.h b/native/kernel/svc/tests/ck_host.h
index 5bb76ff2..e50f6fb4 100644
--- a/native/kernel/svc/tests/ck_host.h
+++ b/native/kernel/svc/tests/ck_host.h
@@ -1,6 +1,12 @@
 /* ck_host.h -- knobs of the host ck.h shim (tests only). */
 #ifndef AIENOS_CK_HOST_H
 #define AIENOS_CK_HOST_H
+#include <stdint.h>
 extern const void *ck_host_mcfg; /* table returned for ck_acpi_find("MCFG") */
 extern int ck_host_quiet;        /* 1: drop ck_printf output */
+extern int ck_host_try_map_ok;            /* 1: ck_mmio_try_map is identity (default 0: always NULL) */
+extern uint64_t ck_host_try_map_fail;     /* with try_map_ok: this exact phys still fails (0: none) */
+void ck_host_capture_start(void);         /* record ck_printf/ck_puts text (even when quiet) */
+const char *ck_host_capture_text(void);   /* NUL-terminated text captured since start */
+void ck_host_capture_stop(void);
 #endif
diff --git a/native/kernel/svc/tests/stage_test.c b/native/kernel/svc/tests/stage_test.c
index 67dbb13b..b9f26912 100644
--- a/native/kernel/svc/tests/stage_test.c
+++ b/native/kernel/svc/tests/stage_test.c
@@ -401,6 +401,162 @@ static void test_discover_edges(void)
     munmap(ecam, 4u << 20);
 }
 
+/* ---------------- Boot discovery report (pci_stage_discover_report) ---------------- */
+
+/* 1 MiB aligned anonymous map (the fake ECAM base must be the address itself
+ * and ECAM bases are 1 MiB aligned). */
+static uint8_t *map_aligned(size_t bytes)
+{
+    size_t slack = 1u << 20;
+    uint8_t *raw = mmap(NULL, bytes + slack, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+    if (raw == MAP_FAILED) return NULL;
+    uintptr_t a = ((uintptr_t)raw + slack - 1) & ~(uintptr_t)(slack - 1);
+    return (uint8_t *)a;
+}
+
+/* Fake segment 0 (SYNTHETIC ids, QEMU-style): host bridge 00:00.0 and a
+ * virtio NIC 00:01.0, two buses. */
+static uint8_t *mk_fake_seg0(void)
+{
+    uint8_t *ecam = map_aligned(FAKE_BYTES);
+    if (!ecam) return NULL;
+    memset(ecam, 0xff, FAKE_BYTES);
+    uint8_t *c;
+    memset(c = cfgp(ecam, 0, 0, 0), 0, 4096); mkfn(c, 0x1b36, 0x0008, 0x060000, 0);
+    memset(c = cfgp(ecam, 0, 1, 0), 0, 4096); mkfn(c, 0x1af4, 0x1041, 0x020000, 0);
+    return ecam;
+}
+
+/* Segment-15 fake, copied to a 1 MiB aligned map (same content as mk_fake_seg15). */
+static uint8_t *mk_fake_seg15_aligned(void)
+{
+    uint8_t *src = mk_fake_seg15();
+    uint8_t *dst = map_aligned(FAKE_BYTES);
+    if (src && dst) memcpy(dst, src, FAKE_BYTES);
+    if (src) munmap(src, FAKE_BYTES);
+    return dst;
+}
+
+static size_t mk_two_seg_mcfg(uint8_t *t, uint8_t *e0, uint8_t *e15)
+{
+    uint64_t b[2] = {(uint64_t)(uintptr_t)e0, (uint64_t)(uintptr_t)e15};
+    uint16_t s[2] = {0, FAKE_SEG};
+    uint8_t sb[2] = {0, 0}, eb[2] = {1, 1};
+    return mk_mcfg(t, 2, b, s, sb, eb);
+}
+
+static int has(const char *text, const char *needle) { return strstr(text, needle) != NULL; }
+
+static void test_discover_report(void)
+{
+    uint8_t *e0 = mk_fake_seg0(), *e15 = mk_fake_seg15_aligned();
+    CHECK(e0 && e15);
+    if (!e0 || !e15) return;
+    static uint8_t c0[FAKE_BYTES], c15[FAKE_BYTES];
+    memcpy(c0, e0, FAKE_BYTES);
+    memcpy(c15, e15, FAKE_BYTES);
+    CHECK(mprotect(e0, FAKE_BYTES, PROT_READ) == 0);
+    CHECK(mprotect(e15, FAKE_BYTES, PROT_READ) == 0);
+    uint8_t t[44 + 32];
+    size_t len = mk_two_seg_mcfg(t, e0, e15);
+    ck_host_mcfg = t;
+    ck_host_try_map_ok = 1;
+    ck_host_try_map_fail = 0;
+
+    /* Both segments mapped read-only: a config write would kill the process. */
+    ck_host_capture_start();
+    CHECK(pci_stage_discover_report() == PCI_OK);
+    ck_host_capture_stop();
+    const char *x = ck_host_capture_text();
+    CHECK(has(x, "pci_disc: seg 0000 bus 00-01 functions=2 bridges=0 rc=0\n"));
+    CHECK(has(x, "pci_disc: seg 000f bus 00-01 functions=2 bridges=1 rc=0\n"));
+    CHECK(has(x, "pci_disc: 000f:01:00.0 10de:2e12 class=030000 rev=a1\n"));
+    CHECK(has(x, "pci_disc: 0000:00:01.0 1af4:1041 class=020000 rev=01\n"));
+    CHECK(has(x, "pci_disc: segments=2 functions=4 (read-only, no config writes)\n"));
+    CHECK(memcmp(e0, c0, FAKE_BYTES) == 0 && memcmp(e15, c15, FAKE_BYTES) == 0);
+
+    /* Segment 15 map refused: that segment is reported as refused, segment 0 still scanned, no fallback. */
+    ck_host_try_map_fail = (uint64_t)(uintptr_t)e15;
+    ck_host_capture_start();
+    CHECK(pci_stage_discover_report() == PCI_OK);
+    ck_host_capture_stop();
+    x = ck_host_capture_text();
+    CHECK(has(x, "pci_disc: seg 000f bus 00-01 REFUSED"));
+    CHECK(!has(x, "10de:2e12"));
+    CHECK(has(x, "pci_disc: seg 0000 bus 00-01 functions=2"));
+    CHECK(has(x, "pci_disc: segments=1 functions=2 (read-only, no config writes)\n"));
+
+    /* Mapping unavailable for every segment (the host default): all refused. */
+    ck_host_try_map_ok = 0;
+    ck_host_try_map_fail = 0;
+    ck_host_capture_start();
+    CHECK(pci_stage_discover_report() == PCI_OK);
+    ck_host_capture_stop();
+    x = ck_host_capture_text();
+    CHECK(has(x, "pci_disc: segments=0 functions=0 (read-only, no config writes)\n"));
+
+    /* Malformed MCFG: error returned, one line. */
+    ck_host_try_map_ok = 1;
+    t[4] = (uint8_t)(len - 1);
+    ck_host_capture_start();
+    CHECK(pci_stage_discover_report() == PCI_E_MCFG);
+    ck_host_capture_stop();
+    CHECK(has(ck_host_capture_text(), "pci_disc: MCFG malformed"));
+    ck_host_mcfg = NULL;
+    ck_host_capture_start();
+    CHECK(pci_stage_discover_report() == PCI_E_NO_MCFG);
+    ck_host_capture_stop();
+    CHECK(has(ck_host_capture_text(), "pci_disc: no ACPI MCFG table\n"));
+    ck_host_try_map_ok = 0;
+    munmap(e0, FAKE_BYTES);
+    munmap(e15, FAKE_BYTES);
+}
+
+/* The devices stage runs the report after pci: probe, ignores its result, and
+ * leaves the report's segments untouched (segment 15 is read-only here). */
+static void test_discover_in_stage(void)
+{
+    uint8_t *e0 = mk_fake_seg0(), *e15 = mk_fake_seg15_aligned();
+    CHECK(e0 && e15);
+    if (!e0 || !e15) return;
+    static uint8_t c15[FAKE_BYTES];
+    memcpy(c15, e15, FAKE_BYTES);
+    CHECK(mprotect(e15, FAKE_BYTES, PROT_READ) == 0);
+    uint8_t t[44 + 32];
+    size_t len = mk_two_seg_mcfg(t, e0, e15);
+    ck_host_mcfg = t;
+    ck_host_try_map_ok = 1;
+    ck_host_quiet = 1;
+    ck_host_capture_start();
+    CHECK(ck_stage_devices() == 0);
+    ck_host_capture_stop();
+    const char *x = ck_host_capture_text();
+    const char *p = strstr(x, "pci: ecam base="), *d = strstr(x, "pci_disc: seg 0000"), *v = strstr(x, "devices: pci=ok");
+    CHECK(p && d && v && p < d && d < v);                           /* probe, then report, then the rest */
+    CHECK(has(x, "pci_disc: 000f:01:00.0 10de:2e12 class=030000 rev=a1\n"));
+    CHECK(memcmp(e15, c15, FAKE_BYTES) == 0);
+
+    /* Report fails (segment 0 allocated twice: pci_mcfg_parse takes the first,
+     * pci_mcfg_parse_all refuses the table): the stage still completes. */
+    uint64_t b[2] = {(uint64_t)(uintptr_t)e0, (uint64_t)(uintptr_t)e0};
+    uint16_t s[2] = {0, 0};
+    uint8_t sb[2] = {0, 0}, eb[2] = {1, 1};
+    (void)len;
+    mk_mcfg(t, 2, b, s, sb, eb);
+    ck_host_capture_start();
+    CHECK(ck_stage_devices() == 0);
+    ck_host_capture_stop();
+    x = ck_host_capture_text();
+    CHECK(has(x, "pci_disc: MCFG malformed") && has(x, "devices: pci=ok"));
+    ck_host_capture_stop();
+    ck_host_quiet = 0;
+    ck_host_try_map_ok = 0;
+    ck_host_mcfg = NULL;
+    munmap(e0, FAKE_BYTES);
+    munmap(e15, FAKE_BYTES);
+}
+
+
 /* ---------------- Store boot ---------------- */
 
 #define T_UNITS 128u
@@ -1266,6 +1422,8 @@ int main(void)
     test_discover_seg15();
     test_discover_trap();
     test_discover_edges();
+    test_discover_report();
+    test_discover_in_stage();
     test_store();
     test_artifact_store();
     test_security();