Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/svc/pi5_fact.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / svc / pi5_fact.c` 155 lines ยท 6499 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* pi5_fact.c -- see pi5_fact.h. */ 18 #include "pi5_fact.h" 19 #include 20 #include "ck.h" 21 #include "cortex_journal.h" 22 23 #define FACT_HEAP_BYTES (512u * 1024u) /* replay of a journal of a few facts peaks well below this */ 24 static uint8_t g_heap[FACT_HEAP_BYTES] __attribute__((aligned(16))); 25 static ck_cj g_cj; 26 static CxStore g_cx; 27 28 static uint32_t journal_chunks(const ss_store *s) 29 { 30 uint32_t k, n = 0; 31 for (k = 0; k < s->nclaims; k++) 32 if (s->ws->claims[k].c.obj.object_kind == CK_CJ_KIND) n++; 33 return n; 34 } 35 36 static void show(char *out, const uint8_t *b, size_t n) 37 { 38 for (size_t i = 0; i < n; i++) out[i] = (b[i] >= 0x20 && b[i] <= 0x7e) ? (char)b[i] : '?'; 39 out[n] = 0; 40 } 41 42 /* Same record as core/lifecycle.c remember(): payload = text packed into little-endian 64-bit words, 43 * tag = byte length, t = record count + 1, generation = the Store generation this append commits. */ 44 static int remember(ss_store *s, const struct ck_fact_req_boot *req) 45 { 46 uint64_t pay[(CK_FACT_REQ_TEXT_MAX + 7) / 8]; 47 uint32_t nw = (req->len + 7) / 8, i; 48 CxHeader h; 49 uint64_t id = 0; 50 for (i = 0; i < nw; i++) pay[i] = 0; 51 for (i = 0; i < req->len; i++) pay[i / 8] |= (uint64_t)req->text[i] << (8 * (i % 8)); 52 for (i = 0; i < sizeof h; i++) ((uint8_t *)&h)[i] = 0; 53 h.cls = CX_CLAIM; 54 h.kind = CX_K_CLAIM; 55 h.subject = CK_CX_FACT_SUBJECT; 56 h.t = g_cx.n + 1; 57 h.generation = ss_generation(s) + 1; 58 h.tag = req->len; 59 #ifdef CK_FACT_MUTANT_NO_APPEND 60 /* TEST-ONLY mutant: skip the commit but still report success. */ 61 int rc = CX_OK; 62 id = g_cx.n + 1; 63 #else 64 int rc = cx_append(&g_cx, &h, pay, nw, &id); 65 #endif 66 if (rc != CX_OK) { 67 ck_printf("fact_stage: REFUSED remember rc=%d reason=\"cortex append failed, nothing written\"\n", rc); 68 return -1; 69 } 70 { 71 char shown[CK_FACT_REQ_TEXT_MAX + 1]; 72 show(shown, req->text, req->len); 73 ck_printf("fact_stage: remembered id=%llu t=%llu store_gen=%llu bytes=%u text=\"%s\"\n", 74 (unsigned long long)id, (unsigned long long)h.t, (unsigned long long)ss_generation(s), 75 (unsigned)req->len, shown); 76 } 77 return 0; 78 } 79 80 /* Every fact, oldest first (label "readback" right after a remember in the same boot, "recalled" on a later boot), each read back through cx_recall_id (digest re-checked); then the chain. */ 81 static int recall(ss_store *s, struct ck_fact_result *out, const char *label) 82 { 83 static CxRecord r; 84 uint64_t id; 85 (void)s; 86 for (id = 1; id <= g_cx.n; id++) { 87 uint8_t text[CK_FACT_REQ_TEXT_MAX]; 88 char shown[CK_FACT_REQ_TEXT_MAX + 1]; 89 size_t len, i; 90 if (cx_recall_id(&g_cx, id, &r) != CX_OK || !r.verified) { 91 ck_printf("fact_stage: fact id=%llu DIGEST_FAIL\n", (unsigned long long)id); 92 continue; 93 } 94 if (r.hdr.cls != CX_CLAIM || r.hdr.kind != CX_K_CLAIM || r.hdr.subject != CK_CX_FACT_SUBJECT) continue; 95 len = (size_t)r.hdr.tag; 96 if (len > sizeof text || len > (size_t)r.hdr.n * 8) len = 0; 97 for (i = 0; i < len; i++) text[i] = (uint8_t)(r.payload[i / 8] >> (8 * (i % 8))); 98 show(shown, text, len); 99 ck_printf("fact_stage: %s id=%llu t=%llu store_gen=%llu text=\"%s\"\n", label, (unsigned long long)id, 100 (unsigned long long)r.hdr.t, (unsigned long long)r.hdr.generation, shown); 101 out->shown++; 102 out->last_id = id; 103 out->last_gen = r.hdr.generation; 104 } 105 return 0; 106 } 107 108 int ck_pi5_fact_run(ss_store *s, const struct ck_fact_req_boot *req, struct ck_fact_result *out) 109 { 110 struct ck_fact_result z = {0}; 111 int want_write = req && req->state == CK_FACT_REQ_FOUND; 112 int rc; 113 *out = z; 114 if (!s) { 115 out->mode = CK_FACT_MODE_NOSTORE; 116 ck_puts("fact_stage: NOTHING RECALLED: the Store is not open (recovery secret missing or rejected, or the disk " 117 "was refused); no Cortex was read, nothing was written\n"); 118 return 0; 119 } 120 out->mode = want_write ? CK_FACT_MODE_REMEMBER : CK_FACT_MODE_RECALL; 121 #ifdef CK_FACT_MUTANT_RECALL_BLIND 122 /* TEST-ONLY mutant: recall never looks at the journal. */ 123 if (!want_write) { 124 #else 125 if (!want_write && journal_chunks(s) == 0) { 126 #endif 127 ck_puts("fact_stage: recall none cortex=none records=0 (no journal on this disk; nothing created)\n"); 128 out->chain_ok = 1; 129 return 0; 130 } 131 ck_cx_heap_init(g_heap, sizeof g_heap); 132 ck_cj_init(&g_cj, s); 133 rc = cx_open_with(&g_cx, &ck_cx_journal_store, &g_cj, "cortex", CK_CX_SUBJECTS, 0); 134 if (rc != CX_OK) { 135 ck_printf("fact_stage: REFUSED cortex open rc=%d chunks=%u\n", rc, journal_chunks(s)); 136 return -1; 137 } 138 ck_printf("fact_stage: cortex open records=%llu chunks=%u journal_bytes=%u store_gen=%llu chain=%s\n", 139 (unsigned long long)g_cx.n, g_cj.nchunks, (unsigned)g_cj.len, (unsigned long long)ss_generation(s), 140 cx_verify_chain(&g_cx) == CX_OK ? "ok" : "FAIL"); 141 rc = want_write ? remember(s, req) : 0; 142 if (rc == 0) { 143 /* Read everything back the way a cold boot would: from the journal replayed at open (remember 144 * appended to the open store, which is the same state a replay rebuilds). */ 145 rc = recall(s, out, want_write ? "readback" : "recalled"); 146 } 147 out->records = g_cx.n; 148 out->chain_ok = cx_verify_chain(&g_cx) == CX_OK; 149 ck_printf("fact_stage: %s records=%llu shown=%llu newest_id=%llu newest_store_gen=%llu journal_bytes=%u chain=%s\n", 150 want_write ? "remember" : "recall", (unsigned long long)out->records, (unsigned long long)out->shown, 151 (unsigned long long)out->last_id, (unsigned long long)out->last_gen, (unsigned)g_cj.len, 152 out->chain_ok ? "ok" : "FAIL"); 153 cx_close(&g_cx); 154 return rc; 155 }