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/slot_service.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 / slot_service.c` 334 lines ยท 12770 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 /* slot_service.c -- kernel-side promotion and abandon of AIENOS image slots 18 * (P11 cut 2b). Contract in slot_service.h. Freestanding. */ 19 #include "ck.h" 20 #include "handoff.h" 21 #include "devices.h" 22 #include "disk.h" 23 #include "disk_part.h" 24 #include "store_sealed.h" 25 #include "slot_service.h" 26 #include "../../boot/slot_control.h" 27 #include "../../boot/slot_update.h" 28 #include "ck_internal.h" 29 30 #ifndef CK_SERVICE 31 #error "slot_service.c is only part of a CK_SERVICE=1 image" 32 #endif 33 34 /* Mirrors mm/frames.h struct ck_efi_desc (UEFI EFI_MEMORY_DESCRIPTOR). */ 35 struct slot_efi_desc { 36 uint32_t type; 37 uint32_t pad; 38 uint64_t phys; 39 uint64_t virt; 40 uint64_t pages; 41 uint64_t attr; 42 }; 43 44 static struct ck_slot_boot_record g_rec; 45 static int g_rec_state; /* 0: none found, 1: valid, -1: refused */ 46 static const char *g_rec_why; 47 static int g_wait; /* 1: candidate waits for health */ 48 static int g_done; /* 1: decided for this boot (written or refused) */ 49 static uint8_t g_unit[CK_SLOT_UNIT] __attribute__((aligned(4096))); 50 /* 4 KiB each: static, never on the kernel stack. */ 51 static struct ck_slot_control ctl, out; 52 static struct ck_slot_manifest man; 53 54 /* Called from ck_kernel_entry after ck_handoff_check, while the firmware's 55 * identity map is still live: the record page is not a RAM type the kernel 56 * maps (mm/mmu.c ram_type), so it is read here or never. */ 57 void ck_slot_boot_capture(const struct ck_handoff *h) 58 { 59 unsigned found = 0; 60 const struct slot_efi_desc *hit = 0; 61 if (!h || !h->memory_map || h->desc_size < sizeof(struct slot_efi_desc)) return; 62 for (uint64_t off = 0; off + h->desc_size <= h->map_size; off += h->desc_size) { 63 const struct slot_efi_desc *d = (const struct slot_efi_desc *)(uintptr_t)(h->memory_map + off); 64 if (d->type == CK_SLOT_BOOT_MEMTYPE) { 65 found++; 66 hit = d; 67 } 68 } 69 if (found == 0) return; 70 if (found != 1 || !hit->pages || !hit->phys || (hit->phys & 4095u)) { 71 g_rec_state = -1; 72 g_rec_why = found != 1 ? "more than one boot record page" : "boot record page geometry"; 73 return; 74 } 75 const uint8_t *src = (const uint8_t *)(uintptr_t)hit->phys; 76 uint8_t *dst = (uint8_t *)&g_rec; 77 for (unsigned i = 0; i < sizeof g_rec; i++) dst[i] = src[i]; 78 g_rec_state = ck_slot_boot_check(&g_rec, &g_rec_why) == 0 ? 1 : -1; 79 } 80 81 static void say_refuse(const char *why) 82 { 83 ck_printf("slot: REFUSED %s: control record not written\n", why); 84 } 85 86 /* One evaluation: read the control record and the started slot's manifest 87 * from the slots partition, decide, commit PROMOTE / ABANDON. */ 88 static void evaluate(int healthy) 89 { 90 int prc = 0; 91 const disk_dev *d = ck_dev_slots_disk(&prc); 92 if (!d) { 93 g_done = 1; 94 say_refuse(prc == CK_DEV_NO_BOOT_DISK ? "no bound boot disk" : ck_gpt_strerror(prc)); 95 return; 96 } 97 if (d->block_size != 512 && d->block_size != 4096) { 98 g_done = 1; 99 say_refuse("slots partition block size"); 100 return; 101 } 102 uint32_t bpu = CK_SLOT_UNIT / d->block_size; 103 uint64_t units = d->block_count / bpu, cap = ck_slot_cap_units(units); 104 if (cap < CK_SLOT_MIN_CAP_UNITS) { 105 g_done = 1; 106 say_refuse("slots partition too small"); 107 return; 108 } 109 ts_device tdev; 110 ts_region r; 111 ts_slot_state tss[2]; 112 uint64_t seq = 0; 113 ss_ts_device(d, &tdev); 114 int trc = ts_open(&r, &tdev, 0); 115 if (trc == TS_OK) trc = ts_recover(&r, (uint8_t *)&ctl, &seq, tss); 116 int have_ctl = trc == TS_OK; 117 if (have_ctl && seq != g_rec.control_seq) { 118 g_done = 1; 119 ck_printf("slot: REFUSED control record moved since the loader read it (seq %llu on disk, %llu in the boot " 120 "record): control record not written\n", 121 (unsigned long long)seq, (unsigned long long)g_rec.control_seq); 122 return; 123 } 124 uint64_t mlba = ck_slot_manifest_unit(g_rec.boot_slot, cap) * bpu; 125 int have_man = disk_read(d, mlba, bpu, g_unit) == DISK_OK; 126 if (have_man) { 127 uint8_t *m = (uint8_t *)&man; 128 for (unsigned i = 0; i < sizeof man; i++) m[i] = g_unit[i]; 129 } 130 const char *why = 0; 131 enum ck_slot_action a = 132 ck_slot_kernel_action(&g_rec, have_ctl ? &ctl : 0, have_man ? &man : 0, healthy, &out, &why); 133 g_wait = a == CK_SLOT_A_WAIT; 134 if (a == CK_SLOT_A_WAIT) { 135 ck_printf("slot: candidate %c waiting for health (tries left %u): not promoted yet\n", 136 'A' + g_rec.boot_slot, (unsigned)ctl.tries_remaining); 137 return; 138 } 139 g_done = 1; 140 if (a == CK_SLOT_A_REFUSE) { 141 say_refuse(why ? why : "refused"); 142 return; 143 } 144 if (a == CK_SLOT_A_NONE) { 145 ck_printf("slot: nothing to write (%s) active=%c\n", why ? why : "steady", 'A' + ctl.active_slot); 146 return; 147 } 148 uint64_t nseq = 0; 149 int crc = ck_slot_control_commit(&r, &out, &nseq); 150 if (crc != TS_OK) { 151 ck_printf("slot: %s FAILED control commit rc=%d (the previous record stays authoritative)\n", 152 ck_slot_action_text(a), crc); 153 return; 154 } 155 if (a == CK_SLOT_A_PROMOTE) 156 ck_printf("slot: promoted slot %c to active generation=%llu floor=%llu seq=%llu (health: store open, " 157 "continuity committed, lifecycle READY)\n", 158 'A' + out.active_slot, (unsigned long long)out.last_good_generation, 159 (unsigned long long)out.generation_floor, (unsigned long long)nseq); 160 else 161 ck_printf("slot: candidate abandoned (marked failed) active=%c stays seq=%llu\n", 'A' + out.active_slot, 162 (unsigned long long)nseq); 163 } 164 165 void ck_slot_service_start(int healthy) 166 { 167 if (g_rec_state == 0) { 168 g_done = 1; 169 ck_puts("slot: no boot record (not started by SLOTLDR.EFI): slot service idle, nothing written\n"); 170 return; 171 } 172 if (g_rec_state < 0) { 173 g_done = 1; 174 say_refuse(g_rec_why ? g_rec_why : "boot record invalid"); 175 return; 176 } 177 ck_printf("slot: booted slot %c reason=%s generation=%llu tries=%u control_seq=%llu\n", 'A' + g_rec.boot_slot, 178 ck_slot_reason_text((enum ck_slot_reason)g_rec.reason), (unsigned long long)g_rec.generation, 179 (unsigned)g_rec.tries, (unsigned long long)g_rec.control_seq); 180 evaluate(healthy); 181 } 182 183 void ck_slot_service_health(int healthy) 184 { 185 if (g_done || !g_wait || !healthy) return; 186 evaluate(healthy); 187 } 188 189 void ck_slot_service_stop(void) 190 { 191 if (g_wait && !g_done) 192 ck_printf("slot: candidate %c NOT promoted this boot (health milestone not reached); the loader counts " 193 "its tries down\n", 194 'A' + g_rec.boot_slot); 195 } 196 197 /* ---- P11 cut 3: the inactive-slot updater (operator verb `update`) ------ */ 198 /* Trusted manifest signers: the same key table SLOTLDR.EFI is built with, 199 * generated by ck_slot_tool keys-c (native/kernel/Makefile): RFC 8032 TEST 1 200 * in the TEST-keys service image, the Owner Root (CK_OWNER_PUBKEYS) under 201 * CK_HARDWARE_STAGING. The plain service image trusts no key and refuses. */ 202 #ifdef CK_SLOT_UPDATE_KEYS 203 extern const uint8_t ck_slot_loader_keys[][32]; 204 extern const unsigned ck_slot_loader_nkeys; 205 #define UPD_KEYS ck_slot_loader_keys 206 #define UPD_NKEYS ck_slot_loader_nkeys 207 #else 208 static const uint8_t g_no_keys[1][32]; 209 #define UPD_KEYS g_no_keys 210 #define UPD_NKEYS 0u 211 #endif 212 #define UPDATE_MAX_UNITS ((64u << 20) / CK_SLOT_UNIT) /* image buffer bound: 64 MiB */ 213 214 #ifdef CK_SERVICE_TEST_KEYS 215 /* TEST-ONLY interruption point (scripts/qemu_ck_slot_loader_test.sh): with 216 * "update halt-after=K" the updater parks the CPU once K or more image units 217 * are written, so the gate can kill QEMU mid-write. Never in another image. */ 218 static uint64_t g_halt_after; 219 static int update_progress(void *ctx, const char *step, uint64_t done, uint64_t total) 220 { 221 (void)ctx; 222 if (g_halt_after && step[0] == 'i' && done >= g_halt_after) { 223 ck_printf("slot: TEST-ONLY update halted after %llu of %llu image units (interrupted on purpose)\n", 224 (unsigned long long)done, (unsigned long long)total); 225 for (;;) __asm__ volatile("wfi"); 226 } 227 return 0; 228 } 229 #endif 230 231 static void update_refuse(const char *why, int wrote, unsigned target) 232 { 233 if (wrote) 234 ck_printf("slot: update REFUSED %s: slot %c left invalid, active slot and control record untouched\n", why, 235 'A' + target); 236 else 237 ck_printf("slot: update REFUSED %s: nothing written\n", why); 238 } 239 240 void ck_slot_service_update(const char *arg, size_t len) 241 { 242 #ifdef CK_SERVICE_TEST_KEYS 243 g_halt_after = 0; 244 if (len) { 245 static const char k[] = "halt-after="; 246 size_t i = 0; 247 uint64_t v = 0; 248 for (; i < sizeof k - 1; i++) 249 if (i >= len || arg[i] != k[i]) break; 250 int ok = i == sizeof k - 1 && len > i && len - i <= 6; 251 for (; ok && i < len; i++) { 252 if (arg[i] < '0' || arg[i] > '9') ok = 0; 253 else v = v * 10 + (uint64_t)(arg[i] - '0'); 254 } 255 if (!ok || v == 0) { 256 update_refuse("argument (TEST image: none, or halt-after=)", 0, 0); 257 return; 258 } 259 g_halt_after = v; 260 } 261 #else 262 (void)arg; 263 if (len) { 264 update_refuse("update takes no argument", 0, 0); 265 return; 266 } 267 #endif 268 if (g_rec_state != 1) { 269 update_refuse(g_rec_state == 0 ? "not started by SLOTLDR.EFI: the booted slot is unknown" 270 : "the boot record was refused", 271 0, 0); 272 return; 273 } 274 int prc = 0, irc = 0; 275 const disk_dev *d = ck_dev_slots_disk(&prc); 276 const disk_dev *in = ck_dev_inbox_disk(&irc); 277 if (!d || !in) { 278 int rc = d ? irc : prc; 279 update_refuse(rc == CK_DEV_NO_BOOT_DISK ? "no bound boot disk" 280 : !d ? "no slots partition" 281 : "no update inbox partition", 282 0, 0); 283 return; 284 } 285 if (d->block_size != 512 && d->block_size != 4096) { 286 update_refuse("slots partition block size", 0, 0); 287 return; 288 } 289 ts_device tdev; 290 ts_region r; 291 ts_slot_state tss[2]; 292 uint64_t seq = 0; 293 ss_ts_device(d, &tdev); 294 int trc = ts_open(&r, &tdev, 0); 295 if (trc == TS_OK) trc = ts_recover(&r, (uint8_t *)&ctl, &seq, tss); 296 uint64_t cap = ck_slot_cap_units(d->block_count / (CK_SLOT_UNIT / d->block_size)); 297 uint64_t inu = in->block_size ? in->block_count / (CK_SLOT_UNIT / in->block_size) : 0; 298 uint64_t pages = cap > 1 ? cap - 1 : 1; 299 if (inu > 1 && inu - 1 < pages) pages = inu - 1; 300 if (pages > UPDATE_MAX_UNITS) pages = UPDATE_MAX_UNITS; 301 uint64_t pa = 0; 302 if (ck_mm_frames_alloc(pages, &pa) != 0 || !ck_mm_mapped(pa, pages * CK_SLOT_UNIT)) { 303 update_refuse("no memory for the image buffer", 0, 0); 304 return; 305 } 306 struct ck_slot_update_io io = { 307 .slots = d, 308 .inbox = in, 309 .control = &r, 310 .ctl = trc == TS_OK ? &ctl : 0, 311 .booted_slot = g_rec.boot_slot, 312 .keys = UPD_KEYS, 313 .nkeys = UPD_NKEYS, 314 .buf = (uint8_t *)(uintptr_t)pa, 315 .buf_cap = pages * CK_SLOT_UNIT, 316 #ifdef CK_SERVICE_TEST_KEYS 317 .progress = update_progress, 318 #endif 319 }; 320 struct ck_slot_update_result res; 321 const char *why = 0; 322 ck_printf("slot: update from the inbox into the inactive slot (booted %c, keys=%u)\n", 'A' + g_rec.boot_slot, 323 UPD_NKEYS); 324 int rc = ck_slot_update(&io, &res, &why); 325 (void)ck_mm_frames_free(pa, pages); 326 if (rc != 0) { 327 update_refuse(why ? why : "refused", res.wrote, res.target); 328 return; 329 } 330 ck_printf("slot: update staged slot %c as candidate generation=%llu bytes=%llu units=%llu seq=%llu tries=%u: " 331 "reboot to try it\n", 332 'A' + res.target, (unsigned long long)res.generation, (unsigned long long)res.image_len, 333 (unsigned long long)res.units_written, (unsigned long long)res.seq, CK_SLOT_MAX_TRIES); 334 }