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/patches/fe9ef466f7af419a # 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 ## GitHub aien-dev/aienux#424 (code only) closed derived `fe9ef466f7af419a` #424 Imported from GitHub aien-dev/aienux#424 (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 `3be8290b317427791b12ae1416f055e5e696712d` · Head `c4bb81f199c19ff25ccd44e7a6731956ac651759` drakestapleton reviewed (COMMENTED) on 2026-10-30 11:18 UTC Triage: stale (draft; stacked on #421 `fix/imac-kbd-replug`; no activity since 2026-10-18) - The `AIEN Verification` check fails on head `c3cc26f8`. - This stack appears to be consolidated into #473; please confirm and close one or the other. - Hardware claim (screen sleep, cores resting): needs an iMac serial log plus power or idle evidence. To be merge-ready: land #421 first, retarget to `main`, get AIEN Verification green, attach hardware evidence, and leave draft. ### Fetch it A git bundle of the pull request's head, 197513 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/aienux/patches/fe9ef466f7af419a.bundle -o fe9ef466f7af419a.bundle git fetch fe9ef466f7af419a.bundle refs/pull/424/head ``` Raw bundle · 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 424 c4bb81f199c19ff25ccd44e7a6731956ac651759 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aienux","number":424,"url":"https://github.com/aien-dev/aienux/pull/424","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:25Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index b1dc59cd..baa4bed5 100644 --- a/.crumb +++ b/.crumb @@ -22,44 +22,44 @@ "children": [ { "digest": "b7d58d76e7dacaa887051e8c2d64e2eaef683c9ba82c03e073002f6ddd920239", - "last_commit": "801b248", + "last_commit": "7db773a8", "name": "crates", "purpose": "Directory crates. Purpose not yet described by a human." }, { "digest": "74bb22d6e439ad7405487d6846114229171cd42746cd2c3e7b2c2d41276008c4", - "last_commit": "4c25c6c", + "last_commit": "4bd49ca1", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "0660522029d1706b495ea85da3194a9bcad5d7938241850f23920cd513f1b46d", - "last_commit": "3d6824f", + "digest": "2d40ed5d0918a2f80175deba5cb887f67e3c95efe01613910621673b195611b1", + "last_commit": "74424e63", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "29e4b821b3b6cbd6ee01c8af06694de4a5118a2cf5f96ac6c7acca68f658d26c", - "last_commit": "edac0f1", + "digest": "4d8879c95261a7a40343e11a473ddf4869a88f54b26c0a9a19b46f5541690123", + "last_commit": "74424e63", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." }, { "digest": "e891c078bec120c3fdb14e13b7dd25d69054f608c2ea632adf2fd2b6e45d8fa1", - "last_commit": "29e884d", + "last_commit": "", "name": "tools", "purpose": "First-party host artifact preparation tools" } ], - "children_root": "sha256:a0cb9848caff8506cb044ae23751d1009b2359a03cec2882dcabe488cba98b6e", - "compiled_at": "2026-11-01T21:25:40Z", + "children_root": "sha256:30706995c5fa458f6c780e0ba3dea9d39af251304a4e140d1ec4f02a553138a6", + "compiled_at": "2026-11-04T21:00:23Z", "compiler": "crumb-compile/1", - "digest": "sha256:7d34f6dbe83d58ef034a8da5104ce731993b8afeb7abfbc243d3dc929e8336ce", + "digest": "sha256:54517b45cd71882b642b0ae687a4afbe7ea60bbf89dd77e04b5d726ea0094c38", "evidence_files": 274, "evidence_root": "sha256:af014a4ed8f6fe759fc5809807fb47578d3410811b522d9f65d749382b029802", "files": 3730, - "generated_at_commit": "1d1be93b23086e99e510a3e283956d1df0fcb84a", - "source_tree": "sha256:fffb73d551a730e6172e0091f5b4482606c81d84590bb78a783bba05f83c8af1" + "generated_at_commit": "c3cc26f8ac198f64a6ae8fa3c10b35d03c015924", + "source_tree": "sha256:d38f1f37212b1b41f3816155263878a284acc39ec559c4cd180a55a367a0ace2" }, "owner_lane": "", "provenance": { diff --git a/native/.crumb b/native/.crumb index 075626a4..3f72c3ef 100644 --- a/native/.crumb +++ b/native/.crumb @@ -50,80 +50,80 @@ "children": [ { "digest": "72ee4e94f3dc99d79ea4eb3bf619ad645c0b76d7009d1883e59adfbfce9c8ac2", - "last_commit": "2c9c8b5", + "last_commit": "09e59a82", "name": "argus", "purpose": "Directory native/argus. Purpose not yet described by a human." }, { "digest": "55582525fd80851150b4b8620558bd3d44f516aac70cf20566f33ec98a914c33", - "last_commit": "e4bc7aa", + "last_commit": "2eef482f", "name": "boot", "purpose": "Directory native/boot. Purpose not yet described by a human." }, { "digest": "1b5414c184e37d4d943e7979db3d82f1441d6004bb9b5c379a4e4e39381fe7fd", - "last_commit": "f45d00a", + "last_commit": "7db773a8", "name": "capability", "purpose": "Directory native/capability. Purpose not yet described by a human." }, { "digest": "ec6a4295568339a6dea0df513d23cf7a28a90d07bee2cd8f93a7417b48ff7871", - "last_commit": "2c9c8b5", + "last_commit": "7db773a8", "name": "crypto", "purpose": "Directory native/crypto. Purpose not yet described by a human." }, { "digest": "43e1cf9e246a9cdfca6b1147381ad071e1cffaf408bb58d3e09b7001e7b0eb5c", - "last_commit": "b5e7268", + "last_commit": "7db773a8", "name": "disk", "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "61b2904194490fb8fa5b1dba19bb861308efc31be1ad5cc909d8f226d1e45d05", - "last_commit": "b4c351e", + "digest": "2b97ed47e85a1f5329faf2a5e136d4dceaab613a2c62b1900376638566ea128e", + "last_commit": "74424e63", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, { "digest": "b9588f7026aaa3fbf7777f5c10d90a1c915cc301be98be5081d3d66106cf77c8", - "last_commit": "2c9c8b5", + "last_commit": "7db773a8", "name": "m5", "purpose": "Directory native/m5. Purpose not yet described by a human." }, { "digest": "b0c2410bb450a0bf6156e064185b024b622fd844e87d7acd476df10c7af63cf6", - "last_commit": "dc9a996", + "last_commit": "7db773a8", "name": "net", "purpose": "Directory native/net. Purpose not yet described by a human." }, { "digest": "f3c7ee4f18f768fa9964c123e07f076c544c58c0966a8a5e9571b0397bf57b06", - "last_commit": "c93a5f5", + "last_commit": "", "name": "omega", "purpose": "Seeded Omega native modules" }, { "digest": "6f0bf10c76df492d27ae642792476c1138bc73f0d897b3ddca04605ac07b4268", - "last_commit": "2c9c8b5", + "last_commit": "7db773a8", "name": "sig", "purpose": "Directory native/sig. Purpose not yet described by a human." }, { "digest": "e125172a18437b662edf303f4b9615bb791f5e7d2ed94192033823185e7d40e1", - "last_commit": "2c9c8b5", + "last_commit": "e5e8f080", "name": "store", "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:984d253047fdba696dc25901a6d5a1035256acd1f0062acc1f7b9b8635a68cc5", - "compiled_at": "2026-10-31T03:56:20Z", + "children_root": "sha256:a08bf9e7a1d37c8a896039107d86fdef258340bf685954d1905a8e545da1dbbc", + "compiled_at": "2026-11-04T21:00:23Z", "compiler": "crumb-compile/1", - "digest": "sha256:0660522029d1706b495ea85da3194a9bcad5d7938241850f23920cd513f1b46d", + "digest": "sha256:2d40ed5d0918a2f80175deba5cb887f67e3c95efe01613910621673b195611b1", "evidence_files": 24, "evidence_root": "sha256:ecc7ccf2e924c221a5bac1d91e23b13d8f3c8d78c2a893a882ed7ca5e88d855d", "files": 770, - "generated_at_commit": "b4c351e7bc9c01e9384c19e9d6fc2ab4d2472e85", - "source_tree": "sha256:5c4657f8d59935d52f7097f40cad08394b97854d7c91f7fe6bd9012a0576cedd" + "generated_at_commit": "c3cc26f8ac198f64a6ae8fa3c10b35d03c015924", + "source_tree": "sha256:23899ae6ed49bcfd46b737c0070e155d0f62f4320488d7f62ea0a22f56830c73" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index ee8038b4..755b7824 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -46,68 +46,68 @@ "children": [ { "digest": "aa16ee3dab6c71bf912686eef4e763743d26883addadc7ca4371eb8545b513aa", - "last_commit": "9e48cf8", + "last_commit": "2529bc07", "name": "arch", "purpose": "Directory native/kernel/arch. Purpose not yet described by a human." }, { "digest": "de009659b554bfb75b093c0b62567ed58238731e3ce4c457008fcc2be1488b89", - "last_commit": "f45d00a", + "last_commit": "6ad91908", "name": "artifact", "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { "digest": "ed58fce3de8581101263e4241f336cc14ea58cd50263fd4bbb5e9ca9bb6639eb", - "last_commit": "792181f", + "last_commit": "9329896a", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { "digest": "eb03419b33745642bf04e2ba6a0a9451910c3d7b7e15c5cb516863fbe41121f3", - "last_commit": "e4bc7aa", + "last_commit": "1c91bb2d", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { "digest": "54cee35b405dc1c90f62d4d227ed172142abfd4917e720708ba0b83b94b9376e", - "last_commit": "f45d00a", + "last_commit": "07355436", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, { "digest": "c7dc6acbe30f21ef0d3983d31b212f89ff23ae6e56862a2718d98fd28c76649a", - "last_commit": "f45d00a", + "last_commit": "2bc7146e", "name": "mm", "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { "digest": "b8e137e0cb61e080d217fe51961287a0803aa7a3f53424c67dbb1d153d32085d", - "last_commit": "b4c351e", + "last_commit": "ca77aa8b", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { "digest": "2ca38317e04e90ee53c0a97be152b07b62e80b4b32db8bbe6b8aa3cb60aa3900", - "last_commit": "792181f", + "last_commit": "0175c6c8", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, { "digest": "1dd8ed2150cfa3709d98eed188288b25edc8d94711ea36b4b846fe66b9ab224b", - "last_commit": "792181f", + "last_commit": "2fd82cef", "name": "tools", "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], "children_root": "sha256:bfaaca1717891ea213270f2368dec6024439319706b4be0bc841942f5c408441", - "compiled_at": "2026-10-31T03:56:20Z", + "compiled_at": "2026-11-04T21:00:23Z", "compiler": "crumb-compile/1", - "digest": "sha256:61b2904194490fb8fa5b1dba19bb861308efc31be1ad5cc909d8f226d1e45d05", + "digest": "sha256:2b97ed47e85a1f5329faf2a5e136d4dceaab613a2c62b1900376638566ea128e", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 515, - "generated_at_commit": "b4c351e7bc9c01e9384c19e9d6fc2ab4d2472e85", - "source_tree": "sha256:98198dfdfcf95267c4f2dd7c63f6f520d3a0281546c1f83519a359408bdc1fc6" + "generated_at_commit": "c3cc26f8ac198f64a6ae8fa3c10b35d03c015924", + "source_tree": "sha256:fd52d443b270465840806201c795bae5bad755382b70e4d68428a3f0615ac04d" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 82da5a29..31a191a5 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -2706,6 +2706,11 @@ IMACKERN_WX_FLAGS := -DCK_X86_WX_MUTANT_TEXT_RW=1 else $(error IMACKERN_WX_MUTANT must be empty, no_nx or text_writable) endif +# CK_X86_IDLE_S: seconds without a key before Allen's screen sleeps (kmain_x86.c default 600, 0 never). +# Set only for test builds (scripts/qemu_ck_imac_keyboard_test.sh uses 25), into their own OUT directory. +ifdef CK_X86_IDLE_S +IMACKERN_CFLAGS += -DCK_X86_IDLE_S=$(CK_X86_IDLE_S)u +endif $(IMACKERN_DIR)/kmain_x86.o: IMACKERN_CFLAGS += $(IMACKERN_WX_FLAGS) $(IMACKERN_DIR)/kmain_x86.o: $(IMACKERN_DIR)/wx_mutant.stamp $(IMACKERN_DIR)/wx_mutant.stamp: FORCE diff --git a/native/kernel/README.md b/native/kernel/README.md index 18584762..475b0aa9 100644 --- a/native/kernel/README.md +++ b/native/kernel/README.md @@ -318,8 +318,16 @@ when a note is saved or handed back. Each Enter gets a local answer from talk, your name for the session, facts about the machine, how long it has been awake and sums said in words ("what is 12 times 4"); anything else gives "I don't know that yet." Answers print as `allen: ` on the console. The -window has no idle countdown: it stays up until "goodbye" (then ACPI S5, or a -restart in the lab image) or the power button. Unplugging the keyboard and plugging it +window has no countdown to power-off. "goodbye", a tap of the power button, or +10 minutes without a key (`CK_X86_IDLE_S`, default 600) puts Allen to sleep: the +screen goes dark and nothing moves, the three drawing cores rest in MWAIT (the +deepest of C6, C3 and C1 that CPUID lists) until he wakes, and the boot core +halts between keyboard polls every 20 ms on a one-shot LAPIC timer. Any key (not +typed) or another tap wakes him; holding the button 4 s is the chipset's hard +off. On the iMac the processor's own energy counter (RAPL) gives +`power: processor X.Y W on average over N s awake|asleep` at each sleep and wake +(never under a hypervisor). The display's backlight stays lit while he sleeps: +the kernel has no driver for the iMac panel's backlight yet. Unplugging the keyboard and plugging it (or another) back in works: the driver watches the keyboard's root port, prints `keyboard: unplugged`, releases the old device and runs discovery again, then `keyboard: ready` once more (the QEMU test does exactly this from outside the diff --git a/native/kernel/x86/kmain_x86.c b/native/kernel/x86/kmain_x86.c index bebb2551..14a52fa9 100644 --- a/native/kernel/x86/kmain_x86.c +++ b/native/kernel/x86/kmain_x86.c @@ -62,6 +62,9 @@ #ifndef CK_X86_HOLD_S #define CK_X86_HOLD_S 60u #endif +#ifndef CK_X86_IDLE_S +#define CK_X86_IDLE_S 600u /* Allen's window: the screen sleeps after this long without a key (0: never) */ +#endif #define SEL_CODE 0x08 #define SEL_DATA 0x10 @@ -289,12 +292,14 @@ static void acpi_reset(void) * (svc/allen_reply.c). Every Enter gets an answer on the console * ("allen: ..."); when a keyboard is ready and the RAM for drawing is there, * the screen switches from the boot report to Allen's window, which stays - * up (no idle countdown); "goodbye" or a tap of the power button puts Allen to - * sleep (dark screen), a key or another tap wakes him; holding the button 4 s + * up (no countdown to power-off); "goodbye", a tap of the power button or + * CK_X86_IDLE_S without a key puts Allen to sleep (dark screen, the cores + * resting), a key or another tap wakes him; holding the button 4 s * is the hardware power-off. Without either, the text screen and its timed hold stay as they were. */ static int allen_on; static uint64_t allen_bye; /* TSC when Allen said goodbye; 0 = still talking */ static uint64_t allen_woke; /* TSC when Allen's window took the screen */ +static uint64_t allen_key; /* TSC of the last key, or of waking; the idle timer counts from it */ static int allen_asleep; /* the screen is dark; a key or the power button wakes him */ static uint16_t pm1_sts; /* PM1a status port (FADT), 0 = no power-button watch */ static uint64_t pwr_last; /* TSC of the last power-button tap taken */ @@ -345,15 +350,125 @@ static int power_button_tapped(void) pwr_last = now; return 1; } +/* Resting while Allen sleeps. Nothing on the screen moves then, so the + * drawing cores wait in MWAIT on park.gen until a write to it (Allen waking, + * or draw_stop) instead of spinning; the boot core, which still polls the + * keyboard and the power button, halts between polls until a one-shot LAPIC + * timer interrupt REST_MS later. The MWAIT hint is the deepest of C6, C3 and + * C1 that CPUID leaf 5 lists (an MWAIT C-state the firmware limits is entered + * as a shallower one). Without MONITOR/MWAIT the drawing cores spin as before + * and the boot core uses HLT. */ +#define REST_MS 20u +static struct { + volatile uint32_t gen; + uint8_t pad[60]; /* alone on its 64-byte line: only a wake writes it */ +} park __attribute__((aligned(64))); +static int rest_mwait; +static uint32_t rest_hint, lapic_per_ms; + +static void rest_probe(uint32_t lapic_hz) +{ + uint32_t r[4]; + lapic_per_ms = lapic_hz / 1000u; + ck_cpuid(0, 0, r); + uint32_t max = r[0]; + ck_cpuid(1, 0, r); + if (max < 5 || !(r[2] & 1u << 3)) + return; + ck_cpuid(5, 0, r); /* EDX: 4 bits of sub-states per MWAIT C-state, C0 first; hint (n - 1) << 4 for Cn */ + rest_mwait = 1; + rest_hint = (r[3] >> 12 & 0xfu) ? 0x20u : (r[3] >> 8 & 0xfu) ? 0x10u : 0x00u; +} + +/* A drawing core while the screen is dark: waits until park.gen is no longer g. */ +static void rest_wait(uint32_t g) +{ + if (!rest_mwait) { + ck_pause(); + return; + } + __asm__ volatile("sfence" ::: "memory"); /* the last strips leave the write-combining buffers first */ + __asm__ volatile("monitor" ::"a"(&park.gen), "c"(0), "d"(0)); + if (__atomic_load_n(&park.gen, __ATOMIC_ACQUIRE) == g) + __asm__ volatile("mwait" ::"a"(rest_hint), "c"(0) : "memory"); +} +static void rest_wake(void) { __atomic_fetch_add(&park.gen, 1, __ATOMIC_RELEASE); } +static void rest_boot(void); /* after the LAPIC registers, below */ + +/* The processor's own energy counter (RAPL MSR_PKG_ENERGY_STATUS 0x611, units + * in MSR_RAPL_POWER_UNIT 0x606 bits 12:8): read only on Intel client cores from + * Skylake on (the iMac's i5-7500 is model 0x9e) and never under a hypervisor, + * where the MSR may not exist and reading it would fault. 32 bits that wrap in + * about an hour at 65 W, so sampled each second into microjoules. */ +static struct { + int on; + uint32_t esu, last; + uint64_t uj, t0, next; +} meter; + +static const char *meter_start(void) +{ + static const uint8_t models[] = {0x4e, 0x5e, 0x8e, 0x9e, 0xa5, 0xa6}; + uint32_t r[4]; + ck_cpuid(1, 0, r); + if (r[2] >> 31 & 1) + return "none (under a hypervisor)"; + uint32_t sig = r[0]; + ck_cpuid(0, 0, r); + if (r[1] != 0x756e6547u) /* "Genu" */ + return "none (not an Intel processor)"; + r[0] = sig; + uint32_t family = r[0] >> 8 & 0xfu, model = (r[0] >> 4 & 0xfu) | (r[0] >> 12 & 0xf0u); + int known = 0; + for (unsigned i = 0; i < sizeof models; i++) + known |= family == 6 && model == models[i]; + if (!known) + return "none (not an Intel client core from Skylake on)"; + meter.esu = (uint32_t)(ck_rdmsr(0x606) >> 8 & 0x1fu); + meter.last = (uint32_t)ck_rdmsr(0x611); + meter.t0 = meter.next = ck_rdtsc(); + meter.on = 1; + return "rapl package"; +} +static void meter_step(uint64_t now) +{ + if (!meter.on || now < meter.next) + return; + uint32_t e = (uint32_t)ck_rdmsr(0x611); + meter.uj += ((uint64_t)(uint32_t)(e - meter.last) * 1000000u) >> meter.esu; + meter.last = e; + meter.next = now + tsc_hz; +} +/* The processor's average since the last line, then the count starts again. */ +static void meter_line(const char *when) +{ + if (!meter.on) + return; + uint64_t now = ck_rdtsc(); + meter.next = 0; + meter_step(now); + uint64_t ms = (now - meter.t0) * 1000u / tsc_hz; + if (ms >= 1000) { + uint64_t mw = meter.uj / ms; + ck_printf("power: processor %llu.%llu W on average over %llu s %s\n", (unsigned long long)(mw / 1000), + (unsigned long long)(mw / 100 % 10), (unsigned long long)(ms / 1000), when); + } + meter.uj = 0; + meter.t0 = now; +} + static void allen_sleep(int on) { if (allen_asleep == on) return; - allen_asleep = on; + meter_line(on ? "awake" : "asleep"); + __atomic_store_n(&allen_asleep, on, __ATOMIC_RELEASE); ck_allen_ui_sleep(on, ck_rdtsc()); if (on) ck_puts("power: Allen asleep (any key or a tap of the power button wakes him)\n"); else { + rest_wake(); /* after the UI has the news: a woken core draws the sky again */ + allen_key = ck_rdtsc(); ck_x86_kbd_discard(); ck_puts("power: Allen awake\n"); } @@ -378,6 +493,7 @@ static ck_allen_notes allen_notes; /* loaded from disk at the window, or empty * static void allen_edit(const char *buf, unsigned len) { + allen_key = ck_rdtsc(); if (allen_asleep) { /* the key only wakes him */ allen_sleep(0); return; @@ -415,6 +531,7 @@ static void allen_map(void) static void allen_line(const char *text) { + allen_key = ck_rdtsc(); if (allen_asleep) { allen_sleep(0); return; @@ -481,16 +598,23 @@ static void night_tick(void) } /* The secondary cores' work while the night is up; each returns to its halt - * once draw_stop clears draw_run. */ + * once draw_stop clears draw_run. While Allen sleeps they rest (rest_wait): + * the frame core once its frame has put the dark screen up, the helpers at + * once (the frame core draws any strip they leave). park.gen is read before + * the state it guards, so a wake between the two is never missed. */ static void draw_loop(void) { __atomic_store_n(&draw_on, 1, __ATOMIC_RELEASE); while (__atomic_load_n(&draw_run, __ATOMIC_ACQUIRE)) { + uint32_t g = __atomic_load_n(&park.gen, __ATOMIC_ACQUIRE); while (__atomic_test_and_set(&draw_lock, __ATOMIC_ACQUIRE)) ck_pause(); int drew = ck_allen_ui_tick(ck_rdtsc()); + int dark = ck_allen_ui_asleep(); __atomic_clear(&draw_lock, __ATOMIC_RELEASE); - if (!drew) + if (dark) + rest_wait(g); + else if (!drew) ck_pause(); } __atomic_store_n(&draw_on, 0, __ATOMIC_RELEASE); @@ -498,9 +622,15 @@ static void draw_loop(void) static void help_loop(void) { __atomic_fetch_add(&draw_helpers, 1, __ATOMIC_ACQ_REL); - while (__atomic_load_n(&draw_run, __ATOMIC_ACQUIRE)) - if (!ck_allen_ui_help()) + while (__atomic_load_n(&draw_run, __ATOMIC_ACQUIRE)) { + uint32_t g = __atomic_load_n(&park.gen, __ATOMIC_ACQUIRE); + if (ck_allen_ui_help()) + continue; + if (__atomic_load_n(&allen_asleep, __ATOMIC_ACQUIRE)) + rest_wait(g); + else ck_pause(); + } __atomic_fetch_sub(&draw_helpers, 1, __ATOMIC_ACQ_REL); } @@ -510,6 +640,7 @@ static void help_loop(void) static void draw_stop(void) { __atomic_store_n(&draw_run, 0, __ATOMIC_RELEASE); + rest_wake(); /* resting cores see draw_run cleared */ if (!tsc_hz) return; uint64_t t0 = ck_rdtsc(); @@ -609,8 +740,19 @@ static void allen_window(void) ck_allen_ui_wake(footer, ck_rdtsc()); ck_x86_kbd_sink(allen_edit, allen_line); allen_on = 1; - allen_woke = ck_rdtsc(); + allen_woke = allen_key = ck_rdtsc(); power_button_watch(); + const char *meter_how = meter_start(); + if (CK_X86_IDLE_S) + ck_printf("power: the screen sleeps after %u s without a key\n", (unsigned)CK_X86_IDLE_S); + else + ck_puts("power: the screen does not sleep by itself\n"); + if (rest_mwait) + ck_printf("power: asleep, the drawing cores rest in mwait (hint 0x%x) and the boot core in mwait between " + "keyboard polls every %u ms; meter: %s\n", rest_hint, REST_MS, meter_how); + else + ck_printf("power: asleep, the drawing cores spin (no MONITOR/MWAIT) and the boot core halts between " + "keyboard polls every %u ms; meter: %s\n", REST_MS, meter_how); boot_times(); night_stats("before Allen woke"); allen_map(); /* the notes loaded from disk, before the first word */ @@ -636,15 +778,23 @@ void ck_x86_hold_step(uint64_t *end) told = 1; night_stats("by 20 s after Allen woke"); } - /* No countdown while Allen's window is up. After goodbye, 4 s for the answer - * to appear, then Allen sleeps; a power-button tap sleeps or wakes him. */ + /* No countdown to power-off while Allen's window is up. After goodbye, 4 s + * for the answer to appear, then Allen sleeps; CK_X86_IDLE_S without a key + * also sleeps him; a power-button tap sleeps or wakes him. */ if (allen_bye && ck_rdtsc() - allen_bye > 4 * tsc_hz) { allen_bye = 0; allen_sleep(1); } + if (CK_X86_IDLE_S && !allen_asleep && ck_rdtsc() - allen_key > (uint64_t)CK_X86_IDLE_S * tsc_hz) { + ck_printf("power: no key for %u s; the screen sleeps\n", (unsigned)CK_X86_IDLE_S); + allen_sleep(1); + } if (power_button_tapped()) allen_sleep(!allen_asleep); + meter_step(ck_rdtsc()); *end = ~0ull; + if (allen_asleep) + rest_boot(); } static __attribute__((noreturn)) void finish(void) @@ -752,6 +902,26 @@ static uint32_t apic_rd(unsigned reg) #define APIC_TIMER_CUR 0x390 #define APIC_TIMER_DIV 0x3e0 +/* The boot core between keyboard polls while Allen sleeps: a one-shot LAPIC + * timer REST_MS out, then STI;MWAIT (or STI;HLT) until its interrupt, which + * STI's one-instruction shadow cannot let slip in before the wait begins. The + * timer was proven at boot (apic_start: 20 ticks or a panic), every other + * interrupt line is masked, and a stray vector is only counted. */ +static void rest_boot(void) +{ + if (!lapic_per_ms) + return; + apic_wr(APIC_LVT_TIMER, VEC_TIMER); /* one shot, unmasked; divide 16 as measured */ + apic_wr(APIC_TIMER_INIT, lapic_per_ms * REST_MS); + if (rest_mwait) { + __asm__ volatile("monitor" ::"a"(&park.gen), "c"(0), "d"(0)); + __asm__ volatile("sti; mwait; cli" ::"a"(rest_hint), "c"(0) : "memory"); + } else + __asm__ volatile("sti; hlt; cli" ::: "memory"); + apic_wr(APIC_TIMER_INIT, 0); + apic_wr(APIC_LVT_TIMER, 1u << 16 | VEC_TIMER); +} + void ck_x86_trap(struct ck_x86_frame *f) { if (f->vector == 14 && probe.armed) { @@ -1544,6 +1714,7 @@ static __attribute__((noreturn)) void kernel_main(void *arg) (unsigned long long)ar.max_us, (unsigned long long)stray_irqs); if (ar.ticks < 20) ck_panic("timer: only %llu ticks", (unsigned long long)ar.ticks); + rest_probe(ar.lapic_hz); struct smp_report sr = {0}; smp_start(&ar, &sr); diff --git a/scripts/.crumb b/scripts/.crumb index c0105d7e..bc94977a 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-31T11:56:25Z", + "compiled_at": "2026-11-04T21:00:23Z", "compiler": "crumb-compile/1", - "digest": "sha256:29e4b821b3b6cbd6ee01c8af06694de4a5118a2cf5f96ac6c7acca68f658d26c", + "digest": "sha256:4d8879c95261a7a40343e11a473ddf4869a88f54b26c0a9a19b46f5541690123", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 133, - "generated_at_commit": "44f1fd15f64091e7c6ede3711b81aff3650711ff", - "source_tree": "sha256:638a38daf9f1fff1b7934a8f0d08aa94f914c93550fe54763f6e128396b78bc9" + "generated_at_commit": "c3cc26f8ac198f64a6ae8fa3c10b35d03c015924", + "source_tree": "sha256:7332fea465273bcab4f4b5d29e58ecd9b0e86c0caf9913cdef39a16e24a480d3" }, "owner_lane": "", "provenance": { diff --git a/scripts/qemu_ck_imac_keyboard_test.sh b/scripts/qemu_ck_imac_keyboard_test.sh index 9874449a..11b96940 100755 --- a/scripts/qemu_ck_imac_keyboard_test.sh +++ b/scripts/qemu_ck_imac_keyboard_test.sh @@ -4,7 +4,8 @@ # about a 2017 iMac, its USB controller or any real keyboard. # # Builds IMACKERN.EFI (the default image) and boots it three times at once in QEMU q35 -# with OVMF and a qemu-xhci controller: +# with OVMF and a qemu-xhci controller, plus a fourth machine on the same kernel built +# with a 25 s idle time (CK_X86_IDLE_S=25, own object directory): # hub a usb-kbd behind a usb-hub (the shape of Apple's wired keyboards, which # carry their own hub); keys are typed from OUTSIDE the guest through the # QEMU monitor ("sendkey"): "Hello Allen" Enter, "xy" Backspace "z?!," Enter, then @@ -16,11 +17,18 @@ # ("allen: Hello Humanity.", "allen: I don't know that yet.", plus the # content-free "screen: Allen answered (11 characters in, 15 out)"), switch the # screen to Allen's window (a QEMU screendump of it is kept as -# $out/allen-qemu.png), keep it up with no idle countdown (still on 70 s -# after the last key), sleep on "goodbye", wake on the power button or a key; +# $out/allen-qemu.png), keep it up with no countdown to power-off (still on and +# awake 70 s after the last key, the default idle time being 600 s), sleep on +# "goodbye", wake on the power button or a key; # direct a usb-kbd on a root port: found on that port; # none the controller with nothing on it: "keyboard: none (no USB keyboard -# found)" and the normal boot otherwise. +# found)" and the normal boot otherwise; +# idle a usb-kbd on a root port and no key typed: 25 s after Allen's window +# opens the screen sleeps by itself ("power: no key for 25 s; the screen +# sleeps"), the cores rest (the startup line says how; QEMU's MWAIT does not +# really wait with more than one CPU, so nothing is measured here, and the +# processor meter stays off under a hypervisor), a key wakes him without +# being typed, a line typed after that is taken. # The kernel's lines reach 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_KEYBOARD: PASS|FAIL|NOT_RUN [(...)] exit 0 / 1 / 2 (tools) / 3 (NOT_RUN) @@ -39,6 +47,7 @@ done out="${IMACKERN_OUT:-${repo_root}/target/native-kernel-imac-keyboard}" commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" imac-kernel >/dev/null +make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}/idle" AIENOS_COMMIT="${commit}" CK_X86_IDLE_S=25 imac-kernel >/dev/null . "${repo_root}/scripts/lib_gate_hold.sh" if ! gh_quiet_check || ! gh_lock_take qemu_ck_imac_keyboard_test "${AIENOS_GATE_MINUTES:-30}"; then @@ -61,6 +70,11 @@ truncate -s 64M "${img}" mformat -i "${img}" -F :: mmd -i "${img}" ::/EFI ::/EFI/BOOT mcopy -i "${img}" "${out}/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI +idle_img="${work}/idle-esp.img" +truncate -s 64M "${idle_img}" +mformat -i "${idle_img}" -F :: +mmd -i "${idle_img}" ::/EFI ::/EFI/BOOT +mcopy -i "${idle_img}" "${out}/idle/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI fails=0 chk() { # description, file, pattern @@ -78,11 +92,12 @@ wait_for() { return 1 } -# start name usb-devices...: one QEMU in the background, monitor on a pipe. +# start name usb-devices...: one QEMU in the background, monitor on a pipe +# (START_IMG: another ESP image than the default kernel's). start() { local name="$1"; shift cp "${vars_fd}" "${work}/${name}.vars" - cp "${img}" "${work}/${name}.esp" + cp "${START_IMG:-${img}}" "${work}/${name}.esp" mkfifo "${work}/${name}.mon.in" "${work}/${name}.mon.out" : >"${work}/${name}.log" qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 2 -m 1024 \ @@ -103,6 +118,7 @@ stop() { local v="pid_$1"; kill "${!v}" 2>/dev/null || true; } start hub -device usb-hub,bus=xhci.0,port=1 -device usb-kbd,bus=xhci.0,port=1.1 start direct -device usb-kbd,bus=xhci.0,port=1,id=kbd0 start none +START_IMG="${idle_img}" start idle -device usb-kbd,bus=xhci.0,port=1 # Keys go in through the QEMU monitor (outside the guest), 0.15 s apart. typed_ok=0 @@ -143,15 +159,32 @@ wait_for direct 300 '^power: Allen.s window stays on' || true wait_for none 300 '^power: holding the screen' || true stop direct stop none -# Allen's window has no idle countdown: 70 s after the last key (the text -# screen's hold is 60 s) the hub run must still be up. Then "goodbye" puts +# idle: nobody types; the screen must sleep by itself 25 s after the window +# opens, then a key wakes him (not typed) and a line typed after it is taken. +idle_slept=0; idle_woke=0 +if wait_for idle 300 '^keyboard: type a line and press Enter' && wait_for idle 120 '^power: Allen asleep'; then + idle_slept=1 + exec 5>"${work}/idle.mon.in" + sleep 2 + echo "sendkey x" >&5 + if wait_for idle 30 '^power: Allen awake'; then + idle_woke=1 + sleep 2 + for k in o k ret; do echo "sendkey ${k}" >&5; sleep 0.15; done + wait_for idle 60 '^keyboard_line: ok$' || true + fi +fi +stop idle +# Allen's window has no countdown to power-off, and the default idle time is +# 600 s: 70 s after the last key (the text screen's hold is 60 s) the hub run +# must still be up and awake. Then "goodbye" puts # Allen to sleep; QEMU's power button (monitor system_powerdown) wakes him, # taps him back to sleep, and a key wakes him without being typed. stayed=0 slept=0; tap_woke=0; tap_slept=0; key_woke=0 if [[ ${typed_ok} == 1 ]] && wait_for hub 60 "^allen: I don't know that yet\."; then sleep 70 - running hub && ! has hub '^power: off' && stayed=1 + running hub && ! has hub '^power: off' && ! has hub '^power: (no key|Allen asleep)' && stayed=1 for k in g o o d b y e ret; do echo "sendkey ${k}" >&3; sleep 0.15; done if wait_for hub 60 '^power: Allen asleep'; then slept=1 @@ -181,7 +214,7 @@ if [[ ${typed_ok} == 1 ]] && wait_for hub 60 "^allen: I don't know that yet\."; fi stop hub -for r in hub direct none; do txt "${r}" >"${work}/${r}.txt"; done +for r in hub direct none idle; do txt "${r}" >"${work}/${r}.txt"; done echo "== hub: usb-kbd behind usb-hub ==" chk "hub: controller running" "${work}/hub.txt" '^xhci: ctrl=0 running version=[0-9a-f]+ ports=[1-9][0-9]* slots=[1-8] context_bytes=(32|64) scratchpads=[0-9]+$' @@ -190,6 +223,7 @@ chk "hub: keyboard found behind the hub" "${work}/hub.txt" '^keyboard: ready \(c chk "hub: typing prompt" "${work}/hub.txt" '^keyboard: type a line and press Enter; goodbye puts Allen to sleep, any key wakes him, holding the power button 4 s turns the computer off$' chk "hub: no countdown while Allen's window is up" "${work}/hub.txt" '^power: Allen.s window stays on; goodbye or a power-button tap puts him to sleep, hold the button 4 s to turn off$' chk "hub: power button watched" "${work}/hub.txt" '^power: button watched at PM1 status port 0x[0-9a-f]+ ' +chk "hub: default idle time 600 s" "${work}/hub.txt" '^power: the screen sleeps after 600 s without a key$' [[ ${typed_ok} == 1 ]] && echo "PASS hub: keys sent from outside the guest" || { echo "FAIL hub: keys not sent"; fails=$((fails + 1)); } chk "hub: Allen's window took the screen" "${work}/hub.txt" '^screen: Allen.s window \([0-9]+x[0-9]+, [0-9]+ MiB drawing memory\)$' @@ -200,7 +234,7 @@ chk "hub: the answer reported by lengths only (11 characters in, 15 out)" "${wor [[ -s "${out}/allen-qemu.png" ]] && echo "PASS hub: screenshot of Allen's window kept (${out}/allen-qemu.png)" || { echo "FAIL hub: no screenshot"; fails=$((fails + 1)); } chk "hub: backspace edits the line; shifted and plain symbols typed" "${work}/hub.txt" '^keyboard_line: xz\?!,$' chk "hub: Allen says what it does not know" "${work}/hub.txt" "^allen: I don't know that yet\." -[[ ${stayed} == 1 ]] && echo "PASS hub: still on 70 s after the last key (no idle countdown)" || { echo "FAIL hub: Allen's window did not stay up"; fails=$((fails + 1)); } +[[ ${stayed} == 1 ]] && echo "PASS hub: still on and awake 70 s after the last key (idle time 600 s)" || { echo "FAIL hub: Allen's window did not stay up and awake"; fails=$((fails + 1)); } chk "hub: Allen says goodbye" "${work}/hub.txt" '^allen: Goodbye\. Until next time\.$' chk "hub: kernel alive" "${work}/hub.txt" '^kernel: alive$' # Goodbye sleeps instead of powering off; a 4 s hold is the chipset's own hard off and QEMU cannot hold the button. @@ -236,14 +270,32 @@ else echo "PASS none: text screen kept without a keyboard" fi -for r in hub direct none; do +echo "== idle: nobody types, idle time 25 s ==" +chk "idle: keyboard found" "${work}/idle.txt" '^keyboard: ready \(controller 0, port [0-9]+, (full|high) speed, slot 1, endpoint 0x8[1-9a-f]\)$' +chk "idle: idle time 25 s" "${work}/idle.txt" '^power: the screen sleeps after 25 s without a key$' +# Either way of resting is accepted: whether QEMU's -cpu max lists MONITOR/MWAIT is QEMU's choice. +chk "idle: how the cores rest, meter off under a hypervisor" "${work}/idle.txt" '^power: asleep, the drawing cores (rest in mwait \(hint 0x[0-9a-f]+\) and the boot core in mwait|spin \(no MONITOR/MWAIT\) and the boot core halts) between keyboard polls every 20 ms; meter: none \(under a hypervisor\)$' +chk "idle: the screen slept by itself" "${work}/idle.txt" '^power: no key for 25 s; the screen sleeps$' +[[ ${idle_slept} == 1 ]] && echo "PASS idle: Allen asleep with no key typed" || { echo "FAIL idle: Allen did not fall asleep by himself"; fails=$((fails + 1)); } +[[ ${idle_woke} == 1 ]] && echo "PASS idle: a key woke Allen" || { echo "FAIL idle: key did not wake Allen"; fails=$((fails + 1)); } +n_sl=$(grep -acE '^power: Allen asleep' "${work}/idle.txt" || true); n_aw=$(grep -acE '^power: Allen awake' "${work}/idle.txt" || true) +if [[ ${n_sl} == 1 && ${n_aw} == 1 ]]; then echo "PASS idle: one sleep, one wake"; else + echo "FAIL idle: ${n_sl} sleep and ${n_aw} wake lines (one each expected)"; fails=$((fails + 1)) + echo "---- idle serial (all) ----"; cat "${work}/idle.txt"; echo "----" +fi +if grep -aqE '^keyboard_line: xok$' "${work}/idle.txt"; then echo "FAIL idle: the waking key was typed"; fails=$((fails + 1)); else echo "PASS idle: the waking key was not typed"; fi +chk "idle: typing works again after waking" "${work}/idle.txt" '^keyboard_line: ok$' +if grep -aqE '^power: processor ' "${work}/idle.txt"; then echo "FAIL idle: processor meter read under a hypervisor"; fails=$((fails + 1)); else echo "PASS idle: no processor meter under a hypervisor"; fi +chk "idle: kernel alive" "${work}/idle.txt" '^kernel: alive$' + +for r in hub direct none idle; do if grep -aqE '^(panic|power: still running|power: halted)' "${work}/${r}.txt"; then echo "FAIL ${r}: panic or failed power off"; fails=$((fails + 1)) else echo "PASS ${r}: no panic" fi done -for r in hub direct none; do echo "---- ${r} serial (keyboard lines) ----"; grep -aE '^(pci: xhci|xhci|usb|keyboard)' "${work}/${r}.txt" || true; done +for r in hub direct none idle; do echo "---- ${r} serial (keyboard and power lines) ----"; grep -aE '^(pci: xhci|xhci|usb|keyboard|power)' "${work}/${r}.txt" || true; done if [[ ${fails} == 0 ]]; then echo "${tag}: PASS" exit 0 ```