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 / svc / continuity_boot.c
| 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 | /* continuity_boot.c -- see continuity_boot.h. TEST-ONLY; empty unless |
| 18 | * CK_TEST_CONTINUITY=1 (make full CK_TEST_CONTINUITY=1). |
| 19 | * |
| 20 | * Marker text is produced by cr_marker_* / rc_marker_* (contract section 3); |
| 21 | * this file only chooses what to run and prints. Oracle: the Rust |
| 22 | * continuity_phase and recovery_phase in crates/aienos-boot/src/nvme_read.rs |
| 23 | * :1100-1400. */ |
| 24 | #include "continuity_boot.h" |
| 25 | |
| 26 | #ifdef CK_TEST_CONTINUITY |
| 27 | #ifdef CK_HARDWARE_STAGING |
| 28 | #error "CK_TEST_CONTINUITY (TEST-ONLY continuity mode selection) cannot be combined with CK_HARDWARE_STAGING" |
| 29 | #endif |
| 30 | #ifdef CK_QEMU_UNSAFE_DMA |
| 31 | #error "CK_TEST_CONTINUITY cannot be combined with CK_QEMU_UNSAFE_DMA" |
| 32 | #endif |
| 33 | #ifdef CK_TEST_STORE_CRASH |
| 34 | #error "CK_TEST_CONTINUITY cannot be combined with CK_TEST_STORE_CRASH" |
| 35 | #endif |
| 36 | #include <stddef.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include "ck.h" |
| 40 | #include "continuity_recovery.h" |
| 41 | #include "continuity_subject_provision.h" |
| 42 | #include "disk_layout.h" |
| 43 | #include "entropy.h" |
| 44 | #include "fmt.h" |
| 45 | #include "m5.h" |
| 46 | #include "sha256.h" |
| 47 | #include "store_boot.h" |
| 48 | |
| 49 | #define PLAN_MAGIC "AIENCONT v1 " |
| 50 | |
| 51 | /* TEST operator key of the Rust qualification (store_qual.rs:59-63). */ |
| 52 | static const uint8_t k_test_operator_key[32] = { |
| 53 | 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, |
| 54 | 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f}; |
| 55 | |
| 56 | struct plan { |
| 57 | int mode; |
| 58 | int cp; /* -1 none */ |
| 59 | int have_response; |
| 60 | uint8_t response[32]; |
| 61 | int have_lineage; /* mode 5: provision WITH the ALLEN subject (OS-0018) */ |
| 62 | uint8_t lineage[32]; |
| 63 | int have_intent; /* mode 5 with lineage: one standing intent after genesis */ |
| 64 | uint64_t intent[2]; |
| 65 | uint8_t request[32]; /* SHA-256 of the plan line: the provisioning request */ |
| 66 | }; |
| 67 | |
| 68 | static const int k_cps[] = {ST_CP_BEFORE_FIRST_WRITE, ST_CP_AFTER_PAYLOAD_OBJECTS, ST_CP_AFTER_CATALOG, |
| 69 | ST_CP_AFTER_COMMIT_RECORD, ST_CP_AFTER_FIRST_FLUSH, ST_CP_AFTER_INACTIVE_SUPERBLOCK, |
| 70 | ST_CP_AFTER_FINAL_FLUSH, SS_CP_BEFORE_ANCHOR, SS_CP_AFTER_ANCHOR}; |
| 71 | |
| 72 | static const char *cp_name(int cp) |
| 73 | { |
| 74 | if (cp == SS_CP_BEFORE_ANCHOR) return "before_anchor"; |
| 75 | if (cp == SS_CP_AFTER_ANCHOR) return "after_anchor"; |
| 76 | return st_checkpoint_name(cp); |
| 77 | } |
| 78 | |
| 79 | static size_t str_len(const char *s) |
| 80 | { |
| 81 | size_t n = 0; |
| 82 | while (s[n]) n++; |
| 83 | return n; |
| 84 | } |
| 85 | |
| 86 | static int hexv(char c) |
| 87 | { |
| 88 | if (c >= '0' && c <= '9') return c - '0'; |
| 89 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | /* Parses "AIENCONT v1 mode=N[ cp=NAME][ response=64hex]\n" (nothing but zeros |
| 94 | * after the newline). 0 ok, -1 malformed. */ |
| 95 | static int parse_plan(const char *p, size_t len, struct plan *out) |
| 96 | { |
| 97 | size_t i = sizeof PLAN_MAGIC - 1; |
| 98 | memset(out, 0, sizeof *out); |
| 99 | out->cp = -1; |
| 100 | int have_mode = 0; |
| 101 | while (i < len && p[i] != '\n') { |
| 102 | size_t s = i; |
| 103 | while (i < len && p[i] != ' ' && p[i] != '\n') i++; |
| 104 | size_t n = i - s; |
| 105 | const char *w = p + s; |
| 106 | if (n > 5 && !memcmp(w, "mode=", 5)) { |
| 107 | int v = 0; |
| 108 | for (size_t k = 5; k < n; k++) { |
| 109 | if (w[k] < '0' || w[k] > '9' || v > 100) return -1; |
| 110 | v = v * 10 + (w[k] - '0'); |
| 111 | } |
| 112 | out->mode = v; |
| 113 | have_mode = 1; |
| 114 | } else if (n > 3 && !memcmp(w, "cp=", 3)) { |
| 115 | for (size_t k = 0; k < sizeof k_cps / sizeof k_cps[0]; k++) { |
| 116 | const char *nm = cp_name(k_cps[k]); |
| 117 | if (str_len(nm) == n - 3 && !memcmp(w + 3, nm, n - 3)) out->cp = k_cps[k]; |
| 118 | } |
| 119 | if (out->cp < 0) return -1; |
| 120 | } else if (n == 9 + 64 && !memcmp(w, "response=", 9)) { |
| 121 | for (size_t k = 0; k < 32; k++) { |
| 122 | int hi = hexv(w[9 + 2 * k]), lo = hexv(w[10 + 2 * k]); |
| 123 | if (hi < 0 || lo < 0) return -1; |
| 124 | out->response[k] = (uint8_t)(hi << 4 | lo); |
| 125 | } |
| 126 | out->have_response = 1; |
| 127 | } else if (n == 8 + 64 && !memcmp(w, "lineage=", 8)) { |
| 128 | for (size_t k = 0; k < 32; k++) { |
| 129 | int hi = hexv(w[8 + 2 * k]), lo = hexv(w[9 + 2 * k]); |
| 130 | if (hi < 0 || lo < 0) return -1; |
| 131 | out->lineage[k] = (uint8_t)(hi << 4 | lo); |
| 132 | } |
| 133 | out->have_lineage = 1; |
| 134 | } else if (n > 7 && !memcmp(w, "intent=", 7)) { |
| 135 | /* intent=<regime>,<target ns>: one CS_INTENT_GOAL_LATENCY intent. */ |
| 136 | size_t k = 7; |
| 137 | for (int f = 0; f < 2; f++) { |
| 138 | uint64_t v = 0; |
| 139 | size_t d = 0; |
| 140 | while (k < n && w[k] >= '0' && w[k] <= '9' && d < 19) { |
| 141 | v = v * 10 + (uint64_t)(w[k] - '0'); |
| 142 | k++; |
| 143 | d++; |
| 144 | } |
| 145 | if (!d) return -1; |
| 146 | out->intent[f] = v; |
| 147 | if (f == 0) { |
| 148 | if (k >= n || w[k] != ',') return -1; |
| 149 | k++; |
| 150 | } |
| 151 | } |
| 152 | if (k != n) return -1; |
| 153 | out->have_intent = 1; |
| 154 | } else { |
| 155 | return -1; |
| 156 | } |
| 157 | if (i < len && p[i] == ' ') i++; |
| 158 | } |
| 159 | if (!have_mode || i >= len) return -1; |
| 160 | { |
| 161 | sha256_ctx h; /* the provisioning request: the plan line up to its newline */ |
| 162 | sha256_init(&h); |
| 163 | sha256_update(&h, (const uint8_t *)p, i); |
| 164 | sha256_final(&h, out->request); |
| 165 | } |
| 166 | for (i = i + 1; i < len; i++) |
| 167 | if (p[i]) return -1; |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* ---- the entropy source: the kernel's RNDR or a refusal, no fallback ---- */ |
| 172 | static int ent_present(void *ctx) |
| 173 | { |
| 174 | (void)ctx; |
| 175 | return ck_entropy_status() == CK_RNG_OK; |
| 176 | } |
| 177 | static int ent_read(void *ctx, uint64_t *out) |
| 178 | { |
| 179 | (void)ctx; |
| 180 | uint64_t w = 0; |
| 181 | if (ck_entropy_fill(&w, sizeof w) != 0) return 0; |
| 182 | *out = w; |
| 183 | return 1; |
| 184 | } |
| 185 | static const struct ck_rng_ops k_ent_ops = {ent_present, ent_read, 0}; |
| 186 | |
| 187 | /* ---- workspace (heap, once) ---- */ |
| 188 | static struct { |
| 189 | ss_workspace *ws; |
| 190 | ss_store *s; |
| 191 | struct cr_work *w; |
| 192 | struct cr_txwork *tw; |
| 193 | struct cr_view *v, *v2; |
| 194 | struct cc_state *st; |
| 195 | struct cs_subject *subj, *subj2; /* ALLEN (OS-0018) */ |
| 196 | st_disk sd; |
| 197 | st_dev sdev; |
| 198 | ts_device tdev; |
| 199 | ss_keys keys; |
| 200 | uint64_t store_base_lba, store_units; |
| 201 | uint32_t bpu; |
| 202 | } g; |
| 203 | |
| 204 | static int alloc_all(void) |
| 205 | { |
| 206 | g.ws = ck_alloc(sizeof *g.ws); |
| 207 | g.s = ck_alloc(sizeof *g.s); |
| 208 | g.w = ck_alloc(sizeof *g.w); |
| 209 | g.tw = ck_alloc(sizeof *g.tw); |
| 210 | g.v = ck_alloc(sizeof *g.v); |
| 211 | g.v2 = ck_alloc(sizeof *g.v2); |
| 212 | g.st = ck_alloc(sizeof *g.st); |
| 213 | g.subj = ck_alloc(sizeof *g.subj); |
| 214 | g.subj2 = ck_alloc(sizeof *g.subj2); |
| 215 | return g.ws && g.s && g.w && g.tw && g.v && g.v2 && g.st && g.subj && g.subj2 ? 0 : -1; |
| 216 | } |
| 217 | |
| 218 | static void line(const char *s) |
| 219 | { |
| 220 | ck_puts(s); |
| 221 | ck_puts("\n"); |
| 222 | } |
| 223 | |
| 224 | static void print_view(const char *word, const struct cr_view *v) |
| 225 | { |
| 226 | char b[256]; |
| 227 | if (cr_marker_view(b, sizeof b, word, v)) line(b); |
| 228 | } |
| 229 | static void print_outcome(int outcome, const char *why, int store_rc) |
| 230 | { |
| 231 | char b[256]; |
| 232 | if (cr_marker_outcome(b, sizeof b, outcome, why, store_rc)) line(b); |
| 233 | } |
| 234 | |
| 235 | static void hex_into(char *o, const uint8_t *b, size_t n) |
| 236 | { |
| 237 | static const char hx[] = "0123456789abcdef"; |
| 238 | for (size_t i = 0; i < n; i++) { |
| 239 | o[2 * i] = hx[b[i] >> 4]; |
| 240 | o[2 * i + 1] = hx[b[i] & 15]; |
| 241 | } |
| 242 | o[2 * n] = 0; |
| 243 | } |
| 244 | |
| 245 | /* Observation hook for mode 8: print every checkpoint; halt at the planned one |
| 246 | * (the host kills QEMU on seeing the marker; the halt only keeps the guest from |
| 247 | * going on). Nothing is flushed or written here. */ |
| 248 | static void cont_hook(void *arg, int cp) |
| 249 | { |
| 250 | const struct plan *p = arg; |
| 251 | ck_printf("CHECKPOINT: %s\n", cp_name(cp)); |
| 252 | if (p->cp == cp) { |
| 253 | ck_printf("continuity: HALT at %s (the host kills QEMU now)\n", cp_name(cp)); |
| 254 | for (;;) __asm__ volatile("wfi"); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static int is_blank(const disk_dev *d, uint8_t *buf) |
| 259 | { |
| 260 | for (uint64_t u = 0; u < CK_LAYOUT_ANCHOR_UNITS + 2u; u++) { |
| 261 | if (disk_read(d, u * g.bpu, g.bpu, buf)) return 0; |
| 262 | for (uint32_t i = 0; i < CK_LAYOUT_UNIT; i++) |
| 263 | if (buf[i]) return 0; |
| 264 | } |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | static void fact(struct cc_record *r, const char *s) |
| 269 | { |
| 270 | memset(r, 0, sizeof *r); |
| 271 | r->status = CC_EPI_DIRECT_OBSERVATION; |
| 272 | r->len = (uint16_t)str_len(s); |
| 273 | r->statement = (const uint8_t *)s; |
| 274 | } |
| 275 | |
| 276 | static void recovery_record_line(const struct rc_record *r) |
| 277 | { |
| 278 | char b[400]; |
| 279 | size_t n = 0; |
| 280 | n += ck_snprintf(b + n, sizeof b - n, "RECOVERY_RECORD:"); |
| 281 | for (int i = 0; i < 2; i++) { |
| 282 | switch (r->slot[i]) { |
| 283 | case RC_SLOT_ZERO: n += ck_snprintf(b + n, sizeof b - n, " slot%d=zero", i); break; |
| 284 | case RC_SLOT_SUPERBLOCK: |
| 285 | n += ck_snprintf(b + n, sizeof b - n, " slot%d=gen%llu", i, (unsigned long long)r->slot_generation[i]); |
| 286 | break; |
| 287 | case RC_SLOT_UNDECODABLE: n += ck_snprintf(b + n, sizeof b - n, " slot%d=undecodable", i); break; |
| 288 | default: n += ck_snprintf(b + n, sizeof b - n, " slot%d=read-error", i); break; |
| 289 | } |
| 290 | } |
| 291 | if (r->have_mount) |
| 292 | n += ck_snprintf(b + n, sizeof b - n, " mount=%s peer=%s generation=%llu", |
| 293 | r->mount_state == ST_VALID ? "Valid" : "DegradedRecovery", rc_peer_name(r->peer), |
| 294 | (unsigned long long)r->generation); |
| 295 | if (r->have_catalog) |
| 296 | n += ck_snprintf(b + n, sizeof b - n, " roots=%u manifests=%u other=%u", r->n_roots, r->n_manifests, r->n_other); |
| 297 | n += ck_snprintf(b + n, sizeof b - n, " agent="); |
| 298 | if (r->have_identity) |
| 299 | hex_into(b + n, r->agent_id, 32); |
| 300 | else |
| 301 | ck_snprintf(b + n, sizeof b - n, "none"); |
| 302 | line(b); |
| 303 | } |
| 304 | |
| 305 | static void recovery_phase(const disk_dev *d, const struct plan *p) |
| 306 | { |
| 307 | (void)d; |
| 308 | line("RECOVERY_OPERATOR_KEY: TEST-ONLY"); |
| 309 | struct rc_env e; |
| 310 | memset(&e, 0, sizeof e); |
| 311 | e.dev = &g.sdev; |
| 312 | e.anchor = &g.tdev; |
| 313 | e.anchor_lba = 0; |
| 314 | e.keys = &g.keys; |
| 315 | e.ws = g.ws; |
| 316 | e.store = g.s; |
| 317 | e.w = g.w; |
| 318 | e.tw = g.tw; |
| 319 | e.view = g.v2; |
| 320 | static struct rc_record rec, after; |
| 321 | if (rc_inspect(&e, &rec) != 0) { |
| 322 | line("RECOVERY_REFUSED (Io)"); |
| 323 | return; |
| 324 | } |
| 325 | recovery_record_line(&rec); |
| 326 | char b[256]; |
| 327 | if (rc_marker_entry(b, sizeof b, &rec)) line(b); |
| 328 | int action = rc_applicable(&rec); |
| 329 | if (action && rc_marker_challenge(b, sizeof b, &rec, (uint8_t)action)) line(b); |
| 330 | if (p->mode == 9) return; |
| 331 | |
| 332 | int rc, cro = 0, srr = 0; |
| 333 | const char *why = 0; |
| 334 | uint8_t agent[32]; |
| 335 | int have_agent = 0; |
| 336 | if (p->mode == 10) { |
| 337 | rc = rc_repair_degraded_peer(&e, k_test_operator_key, p->response, &after); |
| 338 | } else { |
| 339 | struct ck_rng rng; |
| 340 | memset(&rng, 0, sizeof rng); |
| 341 | if (ck_rng_probe(&rng, &k_ent_ops) != CK_RNG_OK) { |
| 342 | line("RECOVERY_REFUSED (NoEntropy)"); |
| 343 | return; |
| 344 | } |
| 345 | rc = rc_provision_identity(&e, k_test_operator_key, p->response, &rng, g.v, &cro, &why, &srr); |
| 346 | if (rc == RC_OK) { |
| 347 | memcpy(agent, g.v->root.agent_id, 32); |
| 348 | have_agent = 1; |
| 349 | } |
| 350 | } |
| 351 | if (rc == RC_OK) { |
| 352 | uint8_t a = p->mode == 10 ? CR_ACTION_REPAIR_DEGRADED_PEER : CR_ACTION_PROVISION_IDENTITY; |
| 353 | if (rc_marker_done(b, sizeof b, a, have_agent ? agent : 0)) line(b); |
| 354 | } else if (rc_marker_refused(b, sizeof b, rc, cro)) { |
| 355 | line(b); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* ---- ALLEN (ARCH-0035 / OS-0018, PROPOSED) ---- |
| 360 | * Provision (mode 5 with lineage=): identity and the genesis subject in ONE |
| 361 | * Store transaction (cs_provision), then, if intent= is given, one standing |
| 362 | * intent as subject sequence 2 through cs_commit (its own transaction). |
| 363 | * Restore (modes 6..8): the subject is resolved read-only BEFORE anything is |
| 364 | * written. RESOLVED is reported; ABSENT is reported and never minted; |
| 365 | * anything else (corrupt, forked, foreign, unreadable) stops the boot with |
| 366 | * nothing written. Markers: |
| 367 | * ALLEN: GENESIS|INTENDED|RESTORED subject=<64> sequence=<n> agent=<64> lineage=<64> intents=<n> active=<n> |
| 368 | * ALLEN: INTENT id=<64> kind=<k> regime=<r> target_ns=<t> since=<s> (each ACTIVE intent) |
| 369 | * ALLEN: ABSENT (...) ALLEN: CORRUPT (<why>) ALLEN: STOP (<outcome>) */ |
| 370 | static void allen_lines(const char *word, const struct cs_subject *s, const uint8_t id[32]) |
| 371 | { |
| 372 | char b[400], sub[65], ag[65], ln[65]; |
| 373 | uint32_t active = 0; |
| 374 | for (uint32_t i = 0; i < s->n_intents; i++) active += s->in[i].state == CS_ACTIVE; |
| 375 | hex_into(sub, id, 32); |
| 376 | hex_into(ag, s->agent, 32); |
| 377 | hex_into(ln, s->cortex, 32); |
| 378 | ck_snprintf(b, sizeof b, "ALLEN: %s subject=%s sequence=%llu agent=%s lineage=%s intents=%u active=%u", word, sub, |
| 379 | (unsigned long long)s->sequence, ag, ln, (unsigned)s->n_intents, (unsigned)active); |
| 380 | line(b); |
| 381 | for (uint32_t i = 0; i < s->n_intents; i++) { |
| 382 | const struct cs_intent *a = &s->in[i]; |
| 383 | if (a->state != CS_ACTIVE) continue; |
| 384 | hex_into(sub, a->id, 32); |
| 385 | ck_snprintf(b, sizeof b, "ALLEN: INTENT id=%s kind=%u regime=%llu target_ns=%llu since=%llu", sub, |
| 386 | (unsigned)a->kind, (unsigned long long)a->payload[0], (unsigned long long)a->payload[1], |
| 387 | (unsigned long long)a->since); |
| 388 | line(b); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static void allen_stop(int o, const char *why) |
| 393 | { |
| 394 | char b[256]; |
| 395 | if (o == CR_CORRUPT) ck_snprintf(b, sizeof b, "ALLEN: CORRUPT (%s)", why ? why : "-"); |
| 396 | else ck_snprintf(b, sizeof b, "ALLEN: STOP (%s%s%s)", cr_outcome_name(o), why ? ": " : "", why ? why : ""); |
| 397 | line(b); |
| 398 | } |
| 399 | |
| 400 | static void allen_provision(const struct cr_source *src, const struct cr_sink *snk, struct cr_sealed_sink *sk, |
| 401 | const struct plan *p, struct ck_rng *rng) |
| 402 | { |
| 403 | static struct cs_genesis gen; |
| 404 | uint8_t sid[32], sid2[32], iid[32]; |
| 405 | const char *why = 0; |
| 406 | int store_rc = 0; |
| 407 | memcpy(gen.provenance, p->request, 32); |
| 408 | gen.origin = CS_ORIGIN_OPERATOR; |
| 409 | memcpy(gen.cortex, p->lineage, 32); |
| 410 | if (p->cp >= 0) { |
| 411 | sk->hook = cont_hook; |
| 412 | sk->hook_arg = (void *)p; |
| 413 | } |
| 414 | int o = cs_provision(src, snk, g.w, g.tw, rng, ck_store_test_uuid, CC_SOURCE_QUALIFICATION, &gen, g.v, g.subj, |
| 415 | sid, &why, &store_rc); |
| 416 | sk->hook = 0; |
| 417 | if (o != CR_RESOLVED) { |
| 418 | print_outcome(o, why, store_rc); |
| 419 | allen_stop(o, why); |
| 420 | return; |
| 421 | } |
| 422 | print_view("PROVISIONED", g.v); |
| 423 | allen_lines("GENESIS", g.subj, sid); |
| 424 | if (!p->have_intent) return; |
| 425 | memcpy(g.subj2, g.subj, sizeof *g.subj2); |
| 426 | cs_subject_advance(g.subj2, sid, p->request, CS_ORIGIN_OPERATOR); |
| 427 | if (cs_subject_intend(g.subj2, CS_INTENT_GOAL_LATENCY, p->intent, iid, &why) != CC_OK) { |
| 428 | allen_stop(CR_E_ARG, why); |
| 429 | return; |
| 430 | } |
| 431 | o = cs_commit(src, snk, g.w, g.v, g.subj2, sid2, &why, &store_rc); |
| 432 | if (o != CS_RESOLVED) { |
| 433 | allen_stop(o, why); |
| 434 | return; |
| 435 | } |
| 436 | allen_lines("INTENDED", g.subj2, sid2); |
| 437 | } |
| 438 | |
| 439 | /* 1 = go on with the resume, 0 = stop (nothing written). */ |
| 440 | static int allen_restore(const struct cr_source *src, const struct cr_sink *snk, const struct plan *p) |
| 441 | { |
| 442 | uint8_t sid[32]; |
| 443 | const char *why = 0; |
| 444 | int store_rc = 0; |
| 445 | (void)snk; |
| 446 | (void)p; |
| 447 | if (cr_resolve(src, g.w, g.v2, &why, &store_rc) != CR_RESOLVED) return 1; /* cr_resume reports it */ |
| 448 | int s = cs_resolve(src, g.w, g.v2, g.subj, sid, &why, &store_rc); |
| 449 | #ifdef CS_MUTANT_RESTORE_MINTS |
| 450 | if (s == CS_ABSENT) { /* MUTANT: restore mints a fresh subject */ |
| 451 | cs_subject_genesis(g.subj, g.v2, p->request, CS_ORIGIN_OPERATOR); |
| 452 | if (cs_commit(src, snk, g.w, g.v2, g.subj, sid, &why, &store_rc) == CS_RESOLVED) |
| 453 | s = cs_resolve(src, g.w, g.v2, g.subj, sid, &why, &store_rc); |
| 454 | } |
| 455 | #endif |
| 456 | if (s == CS_RESOLVED) { |
| 457 | allen_lines("RESTORED", g.subj, sid); |
| 458 | return 1; |
| 459 | } |
| 460 | if (s == CS_ABSENT) { |
| 461 | line("ALLEN: ABSENT (provisioned without a subject; restore never mints one)"); |
| 462 | return 1; |
| 463 | } |
| 464 | allen_stop(s, why); |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | static void continuity_phase(const disk_dev *d, const struct plan *p) |
| 469 | { |
| 470 | struct cr_source src; |
| 471 | struct cr_sink snk; |
| 472 | struct cr_sealed_sink sk = {g.s, 0, 0}; |
| 473 | cr_bind_sealed(&src, g.s); |
| 474 | cr_bind_sealed_sink(&snk, &sk); |
| 475 | (void)d; |
| 476 | const char *why = 0; |
| 477 | int store_rc = 0; |
| 478 | struct ck_rng rng; |
| 479 | |
| 480 | if (p->mode == 5) { |
| 481 | memset(&rng, 0, sizeof rng); |
| 482 | if (ck_rng_probe(&rng, &k_ent_ops) != CK_RNG_OK) { |
| 483 | line("CONTINUITY: NO_ENTROPY"); |
| 484 | return; |
| 485 | } |
| 486 | if (p->have_lineage) { |
| 487 | allen_provision(&src, &snk, &sk, p, &rng); |
| 488 | return; |
| 489 | } |
| 490 | int o = cr_provision(&src, &snk, g.w, g.tw, &rng, ck_store_test_uuid, CC_SOURCE_QUALIFICATION, g.v, &why, |
| 491 | &store_rc); |
| 492 | if (o == CR_RESOLVED) print_view("PROVISIONED", g.v); |
| 493 | else print_outcome(o, why, store_rc); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | if (!allen_restore(&src, &snk, p)) return; |
| 498 | int committed = 0; |
| 499 | int o = cr_resume(&src, &snk, g.w, g.tw, g.v, &committed, &why, &store_rc); |
| 500 | if (o != CR_RESOLVED) { |
| 501 | print_outcome(o, why, store_rc); |
| 502 | return; |
| 503 | } |
| 504 | if (!committed) { |
| 505 | print_view("RESUMED_READONLY", g.v); |
| 506 | return; |
| 507 | } |
| 508 | print_view("RESUMED", g.v); |
| 509 | |
| 510 | struct cc_record rec; |
| 511 | if (p->mode == 7) { |
| 512 | memcpy(g.st, &g.v->state, sizeof *g.st); |
| 513 | uint8_t child[32]; |
| 514 | if (cc_state_fork(g.st, g.v->root.root_branch, child, 0) != CC_OK) { |
| 515 | line("CONTINUITY: STOP (fork)"); |
| 516 | return; |
| 517 | } |
| 518 | fact(&rec, "qemu: the operator asked AIEN to remember this"); |
| 519 | struct cr_update up = {g.st, &rec, 1}; |
| 520 | o = cr_commit(&src, &snk, g.w, g.tw, g.v, &up, 0, g.v2, &why, &store_rc); |
| 521 | if (o == CR_RESOLVED) print_view("REMEMBERED", g.v2); |
| 522 | else print_outcome(o, why, store_rc); |
| 523 | } else if (p->mode == 8) { |
| 524 | sk.hook = cont_hook; |
| 525 | sk.hook_arg = (void *)p; |
| 526 | fact(&rec, "qemu: committed across a crash"); |
| 527 | struct cr_update up = {0, &rec, 1}; |
| 528 | o = cr_commit(&src, &snk, g.w, g.tw, g.v, &up, 0, g.v2, &why, &store_rc); |
| 529 | if (o == CR_RESOLVED) print_view("COMMITTED", g.v2); |
| 530 | else print_outcome(o, why, store_rc); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | int ck_cont_boot_stage(const disk_dev *d, int *rc) |
| 535 | { |
| 536 | *rc = 0; |
| 537 | ck_puts("continuity: TEST-ONLY continuity mode selection image (CK_TEST_CONTINUITY=1); " |
| 538 | "never counts toward a default boot\n"); |
| 539 | #ifdef CM_MUTANT_RESUME_NO_COMMIT |
| 540 | ck_puts("continuity: TEST-ONLY MUTANT resume_no_commit: resume skips the incarnation commit\n"); |
| 541 | #endif |
| 542 | #ifdef CM_MUTANT_RESUME_PROVISIONS |
| 543 | ck_puts("continuity: TEST-ONLY MUTANT resume_provisions: resume mints an identity on an unprovisioned store\n"); |
| 544 | #endif |
| 545 | #ifdef RC_MUTANT_DEGRADED_IS_UNPROVISIONED |
| 546 | ck_puts("continuity: TEST-ONLY MUTANT degraded_is_unprovisioned: a degraded mount reads as unprovisioned\n"); |
| 547 | #endif |
| 548 | #ifdef CR_MUTANT_BARE_SHA256 |
| 549 | ck_puts("continuity: TEST-ONLY MUTANT bare_sha256: operator response is a bare SHA-256\n"); |
| 550 | #endif |
| 551 | #ifdef CS_MUTANT_RESTORE_MINTS |
| 552 | ck_puts("continuity: TEST-ONLY MUTANT subject_restore_mints: restore mints an ALLEN subject when it finds none\n"); |
| 553 | #endif |
| 554 | #ifdef CS_MUTANT_ACCEPT_FOREIGN |
| 555 | ck_puts("continuity: TEST-ONLY MUTANT subject_accept_foreign: restore accepts a subject of another agent\n"); |
| 556 | #endif |
| 557 | if (!d || (d->block_size != 512 && d->block_size != 4096)) return 0; |
| 558 | uint32_t bpu = CK_LAYOUT_UNIT / d->block_size; |
| 559 | uint64_t units = d->block_count / bpu; |
| 560 | uint8_t *buf = ck_alloc(CK_LAYOUT_UNIT); |
| 561 | if (!units || !buf) { |
| 562 | ck_puts("continuity: plan REFUSED (no plan buffer or no unit); Store stage runs as default\n"); |
| 563 | return 0; |
| 564 | } |
| 565 | if (disk_read(d, (units - 1u) * bpu, bpu, buf)) { |
| 566 | ck_puts("continuity: plan REFUSED (read error); Store stage runs as default\n"); |
| 567 | return 0; |
| 568 | } |
| 569 | if (memcmp(buf, PLAN_MAGIC, sizeof PLAN_MAGIC - 1)) { |
| 570 | ck_puts("continuity: no plan; Store stage runs as default\n"); |
| 571 | return 0; |
| 572 | } |
| 573 | static struct plan p; |
| 574 | if (parse_plan((const char *)buf, CK_LAYOUT_UNIT, &p) || !(p.mode == 1 || (p.mode >= 5 && p.mode <= 11)) || |
| 575 | (p.mode >= 10 && !p.have_response)) { |
| 576 | ck_puts("continuity: plan REFUSED (malformed); Store stage runs as default\n"); |
| 577 | return 0; |
| 578 | } |
| 579 | if (p.mode == 1) { |
| 580 | ck_puts("continuity: plan mode=1 (ordinary Store boot)\n"); |
| 581 | return 0; |
| 582 | } |
| 583 | ck_printf("continuity: plan mode=%d\n", p.mode); |
| 584 | |
| 585 | g.bpu = bpu; |
| 586 | g.store_base_lba = (uint64_t)CK_LAYOUT_ANCHOR_UNITS * bpu; |
| 587 | g.store_units = units - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS; |
| 588 | if (alloc_all()) { |
| 589 | ck_puts("continuity: REFUSED (no workspace)\n"); |
| 590 | *rc = -1; |
| 591 | return 1; |
| 592 | } |
| 593 | int r = st_disk_bind(&g.sd, d, g.store_base_lba, g.store_units, &g.sdev); |
| 594 | if (r) { |
| 595 | ck_printf("continuity: REFUSED (st_disk_bind rc=%d)\n", r); |
| 596 | *rc = r; |
| 597 | return 1; |
| 598 | } |
| 599 | ss_ts_device(d, &g.tdev); |
| 600 | ck_store_test_keys(&g.keys); |
| 601 | |
| 602 | int blank = is_blank(d, g.ws->rd); |
| 603 | if (p.mode == 5 && blank) { |
| 604 | r = ss_format(&g.sdev, &g.tdev, 0, ck_store_test_uuid, &g.keys, g.ws); |
| 605 | if (r) { |
| 606 | line("CONTINUITY: STOP (genesis write)"); |
| 607 | return 1; |
| 608 | } |
| 609 | blank = 0; |
| 610 | } |
| 611 | if (blank && p.mode >= 5 && p.mode <= 8) { |
| 612 | line("CONTINUITY: STOP (store Unformatted)"); |
| 613 | return 1; |
| 614 | } |
| 615 | if (p.mode <= 8) { |
| 616 | memset(g.s, 0, sizeof *g.s); |
| 617 | r = ss_open(g.s, &g.sdev, &g.tdev, 0, &g.keys, g.ws); |
| 618 | if (r) { |
| 619 | ck_printf("CONTINUITY: STOP (store Refused(%d))\n", r); |
| 620 | return 1; |
| 621 | } |
| 622 | continuity_phase(d, &p); |
| 623 | } else { |
| 624 | recovery_phase(d, &p); |
| 625 | } |
| 626 | memset(&g.keys, 0, sizeof g.keys); |
| 627 | return 1; |
| 628 | } |
| 629 | #else |
| 630 | typedef int ck_continuity_boot_not_built; /* ISO C: no empty translation unit */ |
| 631 | #endif |