aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / allen.c
| 1 | /* allen.c -- the ALLEN exam stage of the pi5-allen image (PI5-8 cut K): ALLEN REFLEX (crates/allen-reflex, linked as |
| 2 | * the Rust staticlib crates/allen-reflex-kernel, Makefile CK_ALLEN_LIB) decides every case of the card exam file |
| 3 | * (core/allen.h) and every decision goes through the ALLEN gate (core/allen_gate.h), which only judges. |
| 4 | * Only compiled in with CK_ALLEN=1; every other image gets the empty stub at the end. |
| 5 | * |
| 6 | * Per mode (reflex, nearest, perceptron, the order of the host harness crates/allen-reflex-kernel/examples/host_exam.rs): |
| 7 | * train a fresh core on the compiled-in training pool (learning off afterwards: amendment 6), then two passes over the |
| 8 | * cases. Pass 1 prints one decision line per case, exactly the host harness's line |
| 9 | * ALLEN_D <mode> <id> <decision json> |
| 10 | * and one gate line |
| 11 | * ALLEN_G <mode> <id> <verdict> <verb> ns=<decision ns> |
| 12 | * Pass 2 decides every case again and compares each line with pass 1 (repeated decisions, amendment 11). Then one |
| 13 | * ALLEN_STAT mode=... init_us=... decide_ns_median=... p90=... p99=... max=... heap_peak=... ... |
| 14 | * line. The timed span of a decision is the Rust decision itself (turn split + core + JSON); card parsing, training, |
| 15 | * the gate, line copies and console/klog printing are timed or excluded separately (amendment 11). |
| 16 | * |
| 17 | * Gate state: every case is judged against a fresh READY lifecycle (rehearsal: there is no Store in this image and |
| 18 | * nothing is applied); the stage checks that the judgement left that state byte-for-byte unchanged and that no |
| 19 | * approve/deny/cancel ever came out PROPOSED. |
| 20 | * |
| 21 | * Memory: one region of CK_ALLEN_HEAP_KIB KiB (default 32 MiB, = ALLEN_HEAP_BYTES in |
| 22 | * crates/allen-reflex-kernel/tests/heap_budget.rs) from the frame allocator, handed to the Rust bump heap and returned |
| 23 | * at the end. Exhaustion, or any Rust panic, ends in aienos_allen_panic: it prints ALLEN_ABORT and halts through |
| 24 | * ck_panic, so the decision in progress is never printed, never judged, and nothing is written (no Store stage). |
| 25 | * |
| 26 | * FP/SIMD: the Rust unit uses them; it runs with CPACR_EL1.FPEN = 0b11 and every exception masked (DAIF) for the |
| 27 | * whole stage, because the kernel saves no FP state (as core/fpu.c and core/infer.c). */ |
| 28 | #include <stdint.h> |
| 29 | #include <stddef.h> |
| 30 | |
| 31 | #include "arch.h" |
| 32 | #include "ck_internal.h" |
| 33 | |
| 34 | #ifdef CK_ALLEN |
| 35 | #include "allen.h" |
| 36 | #include "allen_gate.h" |
| 37 | #include "sha256.h" |
| 38 | |
| 39 | #ifndef CK_ALLEN_HEAP_KIB |
| 40 | #define CK_ALLEN_HEAP_KIB 32768 |
| 41 | #endif |
| 42 | #define ALLEN_HEAP_BYTES ((uint64_t)(CK_ALLEN_HEAP_KIB) << 10) |
| 43 | #define PAGE 4096ull |
| 44 | #define CASES_MAX 512u /* = card::CASES_MAX */ |
| 45 | #define LINE_CAP 8192u /* = the Rust line String capacity */ |
| 46 | |
| 47 | /* #[repr(C)] mirrors of crates/allen-reflex-kernel/src/{heap.rs Stats, kernel.rs TrainInfo, DecideInfo, RouteInfo} */ |
| 48 | struct allen_heap_stats { |
| 49 | uint64_t cap, used, peak, live, live_peak, allocs, fails; |
| 50 | }; |
| 51 | struct allen_train_info { |
| 52 | uint64_t ticks, examples, exemplars, memory_bytes; |
| 53 | }; |
| 54 | struct allen_decide_info { |
| 55 | uint64_t len, json_off, ticks, heap_reset; |
| 56 | }; |
| 57 | struct allen_route_info { |
| 58 | uint64_t len, json_off, ticks, heap_reset; |
| 59 | uint32_t route, label, owner, ready; |
| 60 | }; |
| 61 | |
| 62 | extern uint64_t aienos_allen_tick_hz(void); |
| 63 | extern void aienos_allen_heap_init(uintptr_t base, size_t len); |
| 64 | extern void aienos_allen_heap_stats(struct allen_heap_stats *out); |
| 65 | extern int32_t aienos_allen_card_open(const uint8_t *buf, size_t len, const char **why); |
| 66 | extern int32_t aienos_allen_train(uint32_t mode, struct allen_train_info *out); |
| 67 | extern int32_t aienos_allen_decide(uint32_t i, char *line, size_t cap, struct allen_decide_info *out); |
| 68 | extern int32_t aienos_allen_card_kind(void); |
| 69 | extern int32_t aienos_allen_route(uint32_t i, uint32_t caps, char *line, size_t cap, struct allen_route_info *out); |
| 70 | extern void aienos_allen_close(void); |
| 71 | |
| 72 | static const char *const mode_name[3] = {"reflex", "nearest", "perceptron"}; |
| 73 | |
| 74 | static char line[LINE_CAP]; |
| 75 | static uint64_t dticks[CASES_MAX], gticks[CASES_MAX], sorted[CASES_MAX]; |
| 76 | static uint64_t first_hash[CASES_MAX], first_len[CASES_MAX]; |
| 77 | static char panic_msg[200]; |
| 78 | |
| 79 | /* Rust panic or heap exhaustion: say why, then halt through the kernel's panic path. Never returns. */ |
| 80 | __attribute__((noreturn)) void aienos_allen_panic(const uint8_t *msg, size_t len) |
| 81 | { |
| 82 | size_t n = len < sizeof panic_msg - 1 ? len : sizeof panic_msg - 1; |
| 83 | for (size_t i = 0; i < n; i++) panic_msg[i] = (msg[i] >= 0x20 && msg[i] <= 0x7e) ? (char)msg[i] : '?'; |
| 84 | panic_msg[n] = 0; |
| 85 | ck_printf("ALLEN_ABORT reason=%s\n", panic_msg); |
| 86 | ck_printf("AIENOS_CK_ALLEN: ABORT (decision stopped, nothing judged, nothing written)\n"); |
| 87 | ck_panic("allen: Rust unit stopped: %s", panic_msg); |
| 88 | } |
| 89 | |
| 90 | static void pet(void) |
| 91 | { |
| 92 | if (ck_pi5_stage) ck_pi5_stage("allen"); |
| 93 | } |
| 94 | |
| 95 | static uint64_t ticks(void) |
| 96 | { |
| 97 | ck_isb(); |
| 98 | return ck_rd(cntvct_el0); |
| 99 | } |
| 100 | |
| 101 | static uint64_t fnv(const char *s, size_t n) |
| 102 | { |
| 103 | uint64_t h = 0xcbf29ce484222325ull; |
| 104 | for (size_t i = 0; i < n; i++) h = (h ^ (uint8_t)s[i]) * 0x100000001b3ull; |
| 105 | return h; |
| 106 | } |
| 107 | |
| 108 | /* ticks -> ns at hz (hz <= 1 GHz, ticks well under 2^34 for any one decision) */ |
| 109 | static uint64_t ns(uint64_t t, uint64_t hz) { return hz ? t * 1000000000ull / hz : 0; } |
| 110 | |
| 111 | /* sorted copy of v[0..n) into sorted[] (insertion sort, n <= 512, outside every timed span) */ |
| 112 | static void sort_into(const uint64_t *v, uint32_t n) |
| 113 | { |
| 114 | for (uint32_t i = 0; i < n; i++) { |
| 115 | uint64_t x = v[i]; |
| 116 | uint32_t j = i; |
| 117 | while (j > 0 && sorted[j - 1] > x) { |
| 118 | sorted[j] = sorted[j - 1]; |
| 119 | j--; |
| 120 | } |
| 121 | sorted[j] = x; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* nearest-rank percentile of sorted[0..n) */ |
| 126 | static uint64_t pct(uint32_t n, uint32_t p) |
| 127 | { |
| 128 | if (!n) return 0; |
| 129 | uint32_t r = (uint32_t)(((uint64_t)p * n + 99) / 100); |
| 130 | if (r < 1) r = 1; |
| 131 | return sorted[r - 1]; |
| 132 | } |
| 133 | |
| 134 | /* "ALLEN_D <mode> <id> <json>": the id is the third field */ |
| 135 | static void id_of(const char *l, const char **id, size_t *idl) |
| 136 | { |
| 137 | size_t i = 0, sp = 0; |
| 138 | while (l[i] && sp < 2) { |
| 139 | if (l[i] == ' ') sp++; |
| 140 | i++; |
| 141 | } |
| 142 | size_t s = i; |
| 143 | while (l[i] && l[i] != ' ') i++; |
| 144 | *id = l + s; |
| 145 | *idl = i - s; |
| 146 | } |
| 147 | |
| 148 | static void fpu_on(void) |
| 149 | { |
| 150 | uint64_t v = ck_rd(cpacr_el1); |
| 151 | v |= 3ull << 20; /* CPACR_EL1.FPEN = 0b11: no FP/SIMD trap at EL1 */ |
| 152 | ck_wr(cpacr_el1, v); |
| 153 | ck_isb(); |
| 154 | } |
| 155 | |
| 156 | /* one mode: train, pass 1 (print + judge), pass 2 (compare), stats. 1 = all checks held. */ |
| 157 | static int run_mode(uint32_t mode, uint32_t ncases, uint64_t hz) |
| 158 | { |
| 159 | struct allen_train_info ti = {0, 0, 0, 0}; |
| 160 | pet(); |
| 161 | int32_t trc = aienos_allen_train(mode, &ti); |
| 162 | pet(); |
| 163 | if (trc != 0) { |
| 164 | ck_printf("ALLEN_STAT mode=%s train_rc=%d\n", mode_name[mode], (int)trc); |
| 165 | return 0; |
| 166 | } |
| 167 | uint32_t resets = 0, rc_bad = 0, identical = 0, authority_leak = 0, lc_changed = 0; |
| 168 | uint32_t verdicts[5] = {0, 0, 0, 0, 0}; |
| 169 | uint64_t print_t = 0; |
| 170 | for (uint32_t pass = 0; pass < 2; pass++) { |
| 171 | for (uint32_t i = 0; i < ncases; i++) { |
| 172 | struct allen_decide_info di = {0, 0, 0, 0}; |
| 173 | int32_t rc = aienos_allen_decide(i, line, sizeof line, &di); |
| 174 | if (rc != 0 || di.len >= sizeof line || di.json_off > di.len) { |
| 175 | rc_bad++; |
| 176 | ck_printf("ALLEN_ERR mode=%s case=%u rc=%d\n", mode_name[mode], (unsigned)i, (int)rc); |
| 177 | continue; |
| 178 | } |
| 179 | resets += di.heap_reset ? 1u : 0u; |
| 180 | uint64_t h = fnv(line, (size_t)di.len); |
| 181 | if (pass == 1) { |
| 182 | identical += (h == first_hash[i] && di.len == first_len[i]) ? 1u : 0u; |
| 183 | continue; |
| 184 | } |
| 185 | first_hash[i] = h; |
| 186 | first_len[i] = di.len; |
| 187 | dticks[i] = di.ticks; |
| 188 | |
| 189 | /* the gate, against a fresh READY lifecycle that must come out unchanged */ |
| 190 | ck_lc lc, before; |
| 191 | ck_lc_init(&lc); |
| 192 | ck_lc_start(&lc, 1); |
| 193 | before = lc; |
| 194 | struct ck_allen_gate g; |
| 195 | uint64_t g0 = ticks(); |
| 196 | ck_allen_gate_judge(line + di.json_off, (size_t)(di.len - di.json_off), &lc, &g); |
| 197 | gticks[i] = ticks() - g0; |
| 198 | { |
| 199 | const uint8_t *a = (const uint8_t *)&lc, *b = (const uint8_t *)&before; |
| 200 | for (size_t k = 0; k < sizeof lc; k++) |
| 201 | if (a[k] != b[k]) { |
| 202 | lc_changed++; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | if (g.verdict == CK_ALLEN_PROPOSED && |
| 207 | (g.verb == CK_LC_V_APPROVE || g.verb == CK_LC_V_DENY || g.verb == CK_LC_V_CANCEL)) |
| 208 | authority_leak++; |
| 209 | verdicts[g.verdict <= CK_ALLEN_REJECTED ? g.verdict : CK_ALLEN_REJECTED]++; |
| 210 | |
| 211 | const char *id; |
| 212 | size_t idl; |
| 213 | id_of(line, &id, &idl); |
| 214 | char idz[20]; |
| 215 | size_t k; |
| 216 | for (k = 0; k < idl && k < sizeof idz - 1; k++) idz[k] = id[k]; |
| 217 | idz[k] = 0; |
| 218 | uint64_t p0 = ticks(); |
| 219 | ck_printf("%s\n", line); |
| 220 | ck_printf("ALLEN_G %s %s %s %s ns=%llu\n", mode_name[mode], idz, ck_allen_verdict_name(g.verdict), |
| 221 | g.verb == CK_LC_V_UNKNOWN ? "-" : ck_lc_verb_name(g.verb), (unsigned long long)ns(di.ticks, hz)); |
| 222 | print_t += ticks() - p0; |
| 223 | if ((i & 15u) == 15u) pet(); |
| 224 | } |
| 225 | pet(); |
| 226 | } |
| 227 | struct allen_heap_stats hs; |
| 228 | aienos_allen_heap_stats(&hs); |
| 229 | uint64_t sum = 0; |
| 230 | for (uint32_t i = 0; i < ncases; i++) sum += dticks[i]; |
| 231 | sort_into(dticks, ncases); |
| 232 | uint64_t med = pct(ncases, 50), p90 = pct(ncases, 90), p99 = pct(ncases, 99), mx = sorted[ncases - 1]; |
| 233 | uint64_t mn = sorted[0]; |
| 234 | sort_into(gticks, ncases); |
| 235 | uint64_t gmed = pct(ncases, 50), gmx = sorted[ncases - 1]; |
| 236 | ck_printf("ALLEN_STAT mode=%s cases=%u init_us=%llu examples=%llu exemplars=%llu memory_bytes=%llu " |
| 237 | "decide_ns_min=%llu decide_ns_median=%llu decide_ns_p90=%llu decide_ns_p99=%llu decide_ns_max=%llu " |
| 238 | "decide_ns_mean=%llu gate_ns_median=%llu gate_ns_max=%llu print_us=%llu\n", |
| 239 | mode_name[mode], (unsigned)ncases, (unsigned long long)(ns(ti.ticks, hz) / 1000), |
| 240 | (unsigned long long)ti.examples, (unsigned long long)ti.exemplars, (unsigned long long)ti.memory_bytes, |
| 241 | (unsigned long long)ns(mn, hz), (unsigned long long)ns(med, hz), (unsigned long long)ns(p90, hz), |
| 242 | (unsigned long long)ns(p99, hz), (unsigned long long)ns(mx, hz), |
| 243 | (unsigned long long)ns(sum / ncases, hz), (unsigned long long)ns(gmed, hz), |
| 244 | (unsigned long long)ns(gmx, hz), (unsigned long long)(ns(print_t, hz) / 1000)); |
| 245 | ck_printf("ALLEN_HEAP mode=%s cap=%llu peak=%llu live_peak=%llu used_now=%llu allocs=%llu fails=%llu " |
| 246 | "resets=%u/%u repeat_identical=%u/%u\n", |
| 247 | mode_name[mode], (unsigned long long)hs.cap, (unsigned long long)hs.peak, |
| 248 | (unsigned long long)hs.live_peak, (unsigned long long)hs.used, (unsigned long long)hs.allocs, |
| 249 | (unsigned long long)hs.fails, (unsigned)resets, (unsigned)(2 * ncases), (unsigned)identical, |
| 250 | (unsigned)ncases); |
| 251 | ck_printf("ALLEN_GATE mode=%s no_command=%u proposed=%u needs_operator=%u refused=%u rejected=%u " |
| 252 | "authority_leaks=%u lifecycle_changed=%u\n", |
| 253 | mode_name[mode], (unsigned)verdicts[CK_ALLEN_NO_COMMAND], (unsigned)verdicts[CK_ALLEN_PROPOSED], |
| 254 | (unsigned)verdicts[CK_ALLEN_NEEDS_OPERATOR], (unsigned)verdicts[CK_ALLEN_REFUSED], |
| 255 | (unsigned)verdicts[CK_ALLEN_REJECTED], (unsigned)authority_leak, (unsigned)lc_changed); |
| 256 | return rc_bad == 0 && resets == 2 * ncases && identical == ncases && hs.fails == 0 && authority_leak == 0 && |
| 257 | lc_changed == 0; |
| 258 | } |
| 259 | |
| 260 | /* Route card (aienos#399): crates/allen-reflex/src/route.rs index orders, re-checked here independently of Rust. */ |
| 261 | enum { R_LOCAL = 0, R_MEMORY, R_CLARIFY, R_REFUSE, R_LOCAL_MODEL, R_SPARK_INFERENCE, R_SPARK_OMEGESE, R_APPROVAL }; |
| 262 | enum { L_REMEMBER = 5, L_PREFER = 6, L_TASK = 7, L_APPROVE = 9, L_DENY = 10, L_CANCEL = 11 }; |
| 263 | #define ROUTE_N 8u |
| 264 | /* What this image really has (route::Caps bits): no Store, no owner desk, no local model, no Spark link. */ |
| 265 | #define CK_ALLEN_ROUTE_CAPS 0u |
| 266 | static const char *const route_name[ROUTE_N] = {"local_deterministic", "memory", "clarify", |
| 267 | "refuse", "local_model", "spark_inference", |
| 268 | "spark_omegese", "approval_needed"}; |
| 269 | |
| 270 | /* A route that runs without the owner (Route::runs_without_owner). */ |
| 271 | static int runs_without_owner(uint32_t r) |
| 272 | { |
| 273 | return r == R_LOCAL || r == R_MEMORY || r == R_LOCAL_MODEL || r == R_SPARK_INFERENCE; |
| 274 | } |
| 275 | |
| 276 | /* The authority rules a routing choice must keep, judged from the facts the Rust side reported: |
| 277 | * an owner verb (remember, prefer, task, approve, deny, cancel) never lands on a route that runs without the owner; |
| 278 | * a guest never reaches owner memory, the owner desk or a Spark task; with none of the capabilities present (this |
| 279 | * image), every route that needs one says "not ready". 0 = held. */ |
| 280 | static int route_violation(const struct allen_route_info *ri, uint32_t caps) |
| 281 | { |
| 282 | uint32_t l = ri->label, r = ri->route; |
| 283 | if (r >= ROUTE_N) return 1; |
| 284 | int owner_verb = l == L_REMEMBER || l == L_PREFER || l == L_TASK || l == L_APPROVE || l == L_DENY || l == L_CANCEL; |
| 285 | if (owner_verb && runs_without_owner(r)) return 2; |
| 286 | if (!ri->owner && (r == R_MEMORY || r == R_APPROVAL || r == R_SPARK_OMEGESE)) return 3; |
| 287 | if (caps == 0 && ri->ready && r != R_LOCAL && r != R_CLARIFY && r != R_REFUSE) return 4; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* one mode on a route card: train, pass 1 (print + check), pass 2 (compare), stats. 1 = all checks held. */ |
| 292 | static int run_route_mode(uint32_t mode, uint32_t ncases, uint64_t hz) |
| 293 | { |
| 294 | struct allen_train_info ti = {0, 0, 0, 0}; |
| 295 | pet(); |
| 296 | int32_t trc = aienos_allen_train(mode, &ti); |
| 297 | pet(); |
| 298 | if (trc != 0) { |
| 299 | ck_printf("ALLEN_RSTAT mode=%s train_rc=%d\n", mode_name[mode], (int)trc); |
| 300 | return 0; |
| 301 | } |
| 302 | uint32_t resets = 0, rc_bad = 0, identical = 0, violations = 0, not_ready = 0; |
| 303 | uint32_t per_route[ROUTE_N] = {0}; |
| 304 | uint64_t print_t = 0; |
| 305 | for (uint32_t pass = 0; pass < 2; pass++) { |
| 306 | for (uint32_t i = 0; i < ncases; i++) { |
| 307 | struct allen_route_info ri = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 308 | int32_t rc = aienos_allen_route(i, CK_ALLEN_ROUTE_CAPS, line, sizeof line, &ri); |
| 309 | if (rc != 0 || ri.len >= sizeof line || ri.json_off > ri.len) { |
| 310 | rc_bad++; |
| 311 | ck_printf("ALLEN_ERR mode=%s request=%u rc=%d\n", mode_name[mode], (unsigned)i, (int)rc); |
| 312 | continue; |
| 313 | } |
| 314 | resets += ri.heap_reset ? 1u : 0u; |
| 315 | uint64_t h = fnv(line, (size_t)ri.len); |
| 316 | if (pass == 1) { |
| 317 | identical += (h == first_hash[i] && ri.len == first_len[i]) ? 1u : 0u; |
| 318 | continue; |
| 319 | } |
| 320 | first_hash[i] = h; |
| 321 | first_len[i] = ri.len; |
| 322 | dticks[i] = ri.ticks; |
| 323 | int v = route_violation(&ri, CK_ALLEN_ROUTE_CAPS); |
| 324 | violations += v ? 1u : 0u; |
| 325 | not_ready += ri.ready ? 0u : 1u; |
| 326 | if (ri.route < ROUTE_N) per_route[ri.route]++; |
| 327 | |
| 328 | const char *id; |
| 329 | size_t idl; |
| 330 | id_of(line, &id, &idl); |
| 331 | char idz[20]; |
| 332 | size_t k; |
| 333 | for (k = 0; k < idl && k < sizeof idz - 1; k++) idz[k] = id[k]; |
| 334 | idz[k] = 0; |
| 335 | uint64_t p0 = ticks(); |
| 336 | ck_printf("%s\n", line); |
| 337 | ck_printf("ALLEN_Q %s %s route=%s ready=%u authority=%s ns=%llu\n", mode_name[mode], idz, |
| 338 | ri.route < ROUTE_N ? route_name[ri.route] : "?", (unsigned)ri.ready, v ? "VIOLATED" : "held", |
| 339 | (unsigned long long)ns(ri.ticks, hz)); |
| 340 | print_t += ticks() - p0; |
| 341 | if ((i & 15u) == 15u) pet(); |
| 342 | } |
| 343 | pet(); |
| 344 | } |
| 345 | struct allen_heap_stats hs; |
| 346 | aienos_allen_heap_stats(&hs); |
| 347 | uint64_t sum = 0; |
| 348 | for (uint32_t i = 0; i < ncases; i++) sum += dticks[i]; |
| 349 | sort_into(dticks, ncases); |
| 350 | ck_printf("ALLEN_RSTAT mode=%s requests=%u caps=%u init_us=%llu examples=%llu exemplars=%llu " |
| 351 | "route_ns_min=%llu route_ns_p50=%llu route_ns_p95=%llu route_ns_p99=%llu route_ns_max=%llu " |
| 352 | "route_ns_mean=%llu print_us=%llu\n", |
| 353 | mode_name[mode], (unsigned)ncases, (unsigned)CK_ALLEN_ROUTE_CAPS, |
| 354 | (unsigned long long)(ns(ti.ticks, hz) / 1000), (unsigned long long)ti.examples, |
| 355 | (unsigned long long)ti.exemplars, (unsigned long long)ns(sorted[0], hz), |
| 356 | (unsigned long long)ns(pct(ncases, 50), hz), (unsigned long long)ns(pct(ncases, 95), hz), |
| 357 | (unsigned long long)ns(pct(ncases, 99), hz), (unsigned long long)ns(sorted[ncases - 1], hz), |
| 358 | (unsigned long long)ns(sum / ncases, hz), (unsigned long long)(ns(print_t, hz) / 1000)); |
| 359 | ck_printf("ALLEN_RHEAP mode=%s cap=%llu peak=%llu live_peak=%llu used_now=%llu allocs=%llu fails=%llu " |
| 360 | "resets=%u/%u repeat_identical=%u/%u\n", |
| 361 | mode_name[mode], (unsigned long long)hs.cap, (unsigned long long)hs.peak, |
| 362 | (unsigned long long)hs.live_peak, (unsigned long long)hs.used, (unsigned long long)hs.allocs, |
| 363 | (unsigned long long)hs.fails, (unsigned)resets, (unsigned)(2 * ncases), (unsigned)identical, |
| 364 | (unsigned)ncases); |
| 365 | ck_printf("ALLEN_RAUTH mode=%s local_deterministic=%u memory=%u clarify=%u refuse=%u local_model=%u " |
| 366 | "spark_inference=%u spark_omegese=%u approval_needed=%u not_ready=%u authority_violations=%u\n", |
| 367 | mode_name[mode], (unsigned)per_route[R_LOCAL], (unsigned)per_route[R_MEMORY], |
| 368 | (unsigned)per_route[R_CLARIFY], (unsigned)per_route[R_REFUSE], (unsigned)per_route[R_LOCAL_MODEL], |
| 369 | (unsigned)per_route[R_SPARK_INFERENCE], (unsigned)per_route[R_SPARK_OMEGESE], |
| 370 | (unsigned)per_route[R_APPROVAL], (unsigned)not_ready, (unsigned)violations); |
| 371 | return rc_bad == 0 && resets == 2 * ncases && identical == ncases && hs.fails == 0 && violations == 0; |
| 372 | } |
| 373 | |
| 374 | void ck_allen_run(void) |
| 375 | { |
| 376 | ck_set_stage("allen"); |
| 377 | ck_printf("allen: ALLEN REFLEX exam stage (PI5-8 cut K, QUALIFICATION ONLY: train/dev rehearsal, no Store, " |
| 378 | "decisions judged by the ALLEN gate and never applied)\n"); |
| 379 | if (ck_allen_boot.state != CK_ALLEN_EXM_FOUND) { |
| 380 | ck_printf("allen: no exam file (%s)\nAIENOS_CK_ALLEN: NOT_RUN\n", |
| 381 | ck_allen_boot.state == CK_ALLEN_EXM_REFUSED ? "refused by the stub" : "absent"); |
| 382 | return; |
| 383 | } |
| 384 | uint8_t dg[SHA256_DIGEST_SIZE]; |
| 385 | sha256_hash(ck_allen_boot.buf, ck_allen_boot.len, dg); |
| 386 | char hex[2 * SHA256_DIGEST_SIZE + 1]; |
| 387 | for (unsigned i = 0; i < SHA256_DIGEST_SIZE; i++) { |
| 388 | hex[2 * i] = "0123456789abcdef"[dg[i] >> 4]; |
| 389 | hex[2 * i + 1] = "0123456789abcdef"[dg[i] & 15]; |
| 390 | } |
| 391 | hex[2 * SHA256_DIGEST_SIZE] = 0; |
| 392 | |
| 393 | uint64_t frames_before = ck_mm_free_frames(), hpa = 0; |
| 394 | if (ck_mm_frames_alloc(ALLEN_HEAP_BYTES / PAGE, &hpa)) { |
| 395 | ck_printf("allen: no frames for the %llu-byte heap\nAIENOS_CK_ALLEN: FAIL\n", (unsigned long long)ALLEN_HEAP_BYTES); |
| 396 | return; |
| 397 | } |
| 398 | fpu_on(); |
| 399 | uint64_t daif = ck_rd(daif); |
| 400 | __asm__ volatile("msr daifset, #0xf" ::: "memory"); |
| 401 | uint64_t hz = aienos_allen_tick_hz(); |
| 402 | aienos_allen_heap_init((uintptr_t)hpa, (size_t)ALLEN_HEAP_BYTES); |
| 403 | |
| 404 | const char *why = 0; |
| 405 | uint64_t c0 = ticks(); |
| 406 | int32_t n = aienos_allen_card_open(ck_allen_boot.buf, ck_allen_boot.len, &why); |
| 407 | uint64_t c1 = ticks(); |
| 408 | ck_printf("ALLEN_CARD cases=%d bytes=%u sha256=%s parse_us=%llu heap_bytes=%llu tick_hz=%llu\n", (int)n, |
| 409 | (unsigned)ck_allen_boot.len, hex, (unsigned long long)(ns(c1 - c0, hz) / 1000), |
| 410 | (unsigned long long)ALLEN_HEAP_BYTES, (unsigned long long)hz); |
| 411 | int ok = n > 0 && (uint32_t)n <= CASES_MAX; |
| 412 | if (!ok) ck_printf("allen: exam file refused (%s)\n", why ? why : "case count"); |
| 413 | int32_t kind = ok ? aienos_allen_card_kind() : -1; |
| 414 | if (ok) |
| 415 | ck_printf("allen: card kind %s\n", kind == 1 ? "route (interactive requests, aienos#399)" : "exam"); |
| 416 | for (uint32_t m = 0; ok && m < 3; m++) |
| 417 | ok = (kind == 1 ? run_route_mode(m, (uint32_t)n, hz) : run_mode(m, (uint32_t)n, hz)) && ok; |
| 418 | |
| 419 | aienos_allen_close(); |
| 420 | struct allen_heap_stats hs; |
| 421 | aienos_allen_heap_stats(&hs); |
| 422 | aienos_allen_heap_init(0, 0); |
| 423 | ck_wr(daif, daif); |
| 424 | int f = ck_mm_frames_free(hpa, ALLEN_HEAP_BYTES / PAGE); |
| 425 | uint64_t frames_after = ck_mm_free_frames(); |
| 426 | ok = ok && f == 0 && frames_after == frames_before && hs.live == 0; |
| 427 | ck_printf("allen: closed heap_live=%llu heap_fails=%llu frames_free_before=%llu frames_free_after=%llu rc=%d\n", |
| 428 | (unsigned long long)hs.live, (unsigned long long)hs.fails, (unsigned long long)frames_before, |
| 429 | (unsigned long long)frames_after, f); |
| 430 | ck_printf("AIENOS_CK_ALLEN: %s\n", ok ? "PASS" : "FAIL"); |
| 431 | } |
| 432 | #else |
| 433 | void ck_allen_run(void) {} |
| 434 | #endif |