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 / x86 / swap_x86.c
| 1 | /* swap_x86.c -- AIENOS-KERNEL-0 live kernel swap (LAB BUILD ONLY: CK_X86_REPORT=1 |
| 2 | * compiles the receiver in; a default image gets the stubs at the bottom). |
| 3 | * |
| 4 | * A swap takes a signed slot object (one 4096-byte manifest unit + the raw kernel |
| 5 | * PE, the format NETLDR.EFI and the slot loader use) and, if it verifies, quiesces |
| 6 | * the devices and jumps into the new kernel through the trampoline (swap_tramp.S). |
| 7 | * |
| 8 | * 1. VERIFY: ck_slot_verify (boot/slot_select.c) -- the same function NETLDR.EFI |
| 9 | * calls (boot/net_loader.c:578) -- against the same owner key table |
| 10 | * (ck_slot_loader_keys, generated by ck_slot_tool keys-c from CK_SLOT_KEYS or |
| 11 | * CK_TEST_SLOT_KEY=1). Checks: manifest structure, signer_id names a trusted |
| 12 | * key, the Ed25519 signature over the 448 signed bytes, the image SHA-512. |
| 13 | * This file keeps no copy of that logic. |
| 14 | * 2. PLACE: the firmware loaded this kernel at hand->image_base (a relocated PE); |
| 15 | * the new kernel is linked at ImageBase 0. Its base relocations (DIR64) are |
| 16 | * applied to the staged copy for the running base, and its sections are laid |
| 17 | * out from that base. A new image whose extent is larger than the running |
| 18 | * image is refused: it would overwrite memory past the kernel. |
| 19 | * 3. QUIESCE: APs to their HLT loop, xHCI, tg3, HDA stopped, panel off. Every |
| 20 | * refusal (the image, the memory map, the tramp page, the trampoline's size) |
| 21 | * happens before this step, so a refused swap leaves the machine running as it was. |
| 22 | * 4. JUMP: the trampoline page is opened writable and executable (lab only, after |
| 23 | * the verify), the trampoline copies the new sections into place and calls the |
| 24 | * new entry with a rebuilt CHANDOF3 handoff. |
| 25 | * |
| 26 | * IMAGE SOURCES, as built in this branch: |
| 27 | * - CK_X86_SWAP_TEST_DISK (QEMU test only): the slot object is read raw from |
| 28 | * LBA 2048 of SATA disk index 2 (the test's third disk). |
| 29 | * - UDP port 47034: NOT BUILT. There is no receiver, so a running iMac cannot |
| 30 | * take a kernel over the network yet. |
| 31 | * |
| 32 | * Honest limits: if tramp_pa is zero (no free page below 1 MiB) the swap is |
| 33 | * refused. Only DIR64 base relocations are handled; any other fix-up type refuses |
| 34 | * the image. QEMU runs exercise the verify, place and jump path only; nothing here |
| 35 | * is tested on the iMac. */ |
| 36 | #include "../core/ck_internal.h" |
| 37 | #include "../mm/frames.h" |
| 38 | #include "../../boot/slot_format.h" |
| 39 | #include "../../boot/netboot_proto.h" |
| 40 | #include "x86.h" |
| 41 | #include "swap_proto.h" |
| 42 | #include "swap_disk_read.h" |
| 43 | #include "report_x86.h" |
| 44 | |
| 45 | #ifndef CK_X86_SWAP |
| 46 | #define CK_X86_SWAP 0 |
| 47 | #endif |
| 48 | |
| 49 | #if CK_X86_SWAP |
| 50 | |
| 51 | /* The owner key table NETLDR.EFI verifies with (generated; see Makefile |
| 52 | * IMACKERN_SLOT_KEYS_C). ck_slot_verify takes it directly. */ |
| 53 | extern const uint8_t ck_slot_loader_keys[][32]; |
| 54 | extern const unsigned ck_slot_loader_nkeys; |
| 55 | |
| 56 | /* Swap buffer: pre-allocated from the frame allocator in ck_x86_swap_init. |
| 57 | * Holds the incoming signed slot object: CK_SLOT_UNIT + image bytes. */ |
| 58 | #define SWAP_BUF_PAGES 1024u /* 4 MiB max kernel image + manifest */ |
| 59 | #define SWAP_BUF_BYTES (SWAP_BUF_PAGES * CK_PAGE) |
| 60 | |
| 61 | static uint64_t swap_buf_pa; /* physical address of the swap buffer */ |
| 62 | static uint64_t swap_stack_top; /* top of the stack the new kernel starts on */ |
| 63 | #define SWAP_STACK_PAGES 16u /* 64 KiB */ |
| 64 | /* The UEFI memory map the new kernel builds its frame pool from. The handoff's copy lives in the |
| 65 | * running image (efi_x86.c map_buf), which the trampoline overwrites: it is copied here first. */ |
| 66 | #define SWAP_MAP_PAGES 16u /* 64 KiB, efi_x86.c MAP_BYTES */ |
| 67 | static uint64_t swap_map_pa; |
| 68 | static uint32_t swap_received; /* bytes received so far */ |
| 69 | static uint32_t swap_total; /* total bytes expected */ |
| 70 | static int swap_ready; /* 1: init succeeded */ |
| 71 | |
| 72 | /* ---- Trampoline page layout (at tramp_pa, already allocated) ---- */ |
| 73 | #define TRAMP_SECT_OFF 0u /* section table: 8 × 32 bytes = 256 bytes */ |
| 74 | #define TRAMP_ENTRY_OFF 256u /* entry_point u64 */ |
| 75 | #define TRAMP_STACK_OFF 264u /* stack top u64 (rsp for the new kernel; the old stack may be overwritten) */ |
| 76 | #define TRAMP_MAGIC_OFF 272u /* CK_X86_SWAP_ENTRY_MAGIC u64: the RCX the new image's _start sees (x86.h) */ |
| 77 | #define TRAMP_HAND_OFF 280u /* handoff struct copy (232 bytes) */ |
| 78 | #define TRAMP_CODE_OFF 512u /* position-independent trampoline code */ |
| 79 | |
| 80 | /* Section table entry (32 bytes). */ |
| 81 | struct swap_sect { |
| 82 | uint64_t src_pa; /* physical address of source section data */ |
| 83 | uint64_t dst_pa; /* destination physical address (running base + RVA) */ |
| 84 | uint64_t len; /* bytes */ |
| 85 | uint32_t flags; /* 0=copy, 1=zero (BSS) */ |
| 86 | uint32_t _pad; |
| 87 | }; |
| 88 | #define SWAP_SECT_MAX 8u |
| 89 | #define SWAP_SECT_ZERO 1u |
| 90 | |
| 91 | /* ---- PE parser (PE32+ only; offsets from the PE spec) ---- */ |
| 92 | #define PE_OFF_MAGIC 0x3c |
| 93 | #define PE_DD_BASEREL 176u /* DataDirectory[5] (BASERELOC), from the PE signature: 24 + 112 (PE32+ data dirs) + 5*8 */ |
| 94 | static int pe_read32(const uint8_t *img, size_t img_len, uint32_t off, uint32_t *out) |
| 95 | { |
| 96 | if ((uint64_t)off + 4 > img_len) return -1; |
| 97 | uint32_t v; memcpy(&v, img + off, 4); *out = v; return 0; |
| 98 | } |
| 99 | static int pe_read16(const uint8_t *img, size_t img_len, uint32_t off, uint16_t *out) |
| 100 | { |
| 101 | if ((uint64_t)off + 2 > img_len) return -1; |
| 102 | uint16_t v; memcpy(&v, img + off, 2); *out = v; return 0; |
| 103 | } |
| 104 | static int pe_read64(const uint8_t *img, size_t img_len, uint32_t off, uint64_t *out) |
| 105 | { |
| 106 | if ((uint64_t)off + 8 > img_len) return -1; |
| 107 | uint64_t v; memcpy(&v, img + off, 8); *out = v; return 0; |
| 108 | } |
| 109 | |
| 110 | struct pe_info { |
| 111 | uint32_t pe_off; /* offset of the PE signature */ |
| 112 | uint32_t sect_off; /* offset of the section table */ |
| 113 | uint16_t nsections; |
| 114 | uint64_t image_base; /* ImageBase as linked */ |
| 115 | uint32_t entry_rva; |
| 116 | }; |
| 117 | |
| 118 | static int pe_headers(const uint8_t *img, size_t img_len, struct pe_info *pi) |
| 119 | { |
| 120 | if (img_len < 0x40 || img[0] != 'M' || img[1] != 'Z') return -1; |
| 121 | uint32_t pe_off; |
| 122 | if (pe_read32(img, img_len, PE_OFF_MAGIC, &pe_off)) return -1; |
| 123 | if ((uint64_t)pe_off + 24 > img_len) return -1; |
| 124 | uint32_t sig; |
| 125 | if (pe_read32(img, img_len, pe_off, &sig) || sig != 0x00004550u) return -1; |
| 126 | uint16_t nsections, optsize, magic; |
| 127 | if (pe_read16(img, img_len, pe_off + 6, &nsections) || /* COFF NumberOfSections */ |
| 128 | pe_read16(img, img_len, pe_off + 20, &optsize) || /* COFF SizeOfOptionalHeader */ |
| 129 | pe_read16(img, img_len, pe_off + 24, &magic)) /* optional header magic */ |
| 130 | return -1; |
| 131 | if (magic != 0x020bu) return -1; /* PE32+ only */ |
| 132 | if (pe_read32(img, img_len, pe_off + 40, &pi->entry_rva) || |
| 133 | pe_read64(img, img_len, pe_off + 48, &pi->image_base)) |
| 134 | return -1; |
| 135 | pi->pe_off = pe_off; |
| 136 | pi->sect_off = pe_off + 24 + (uint32_t)optsize; |
| 137 | pi->nsections = nsections; |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* Section header i (40 bytes): VirtualSize @8, VirtualAddress @12, SizeOfRawData @16, |
| 142 | * PointerToRawData @20. */ |
| 143 | static int pe_section(const uint8_t *img, size_t img_len, const struct pe_info *pi, unsigned i, |
| 144 | uint32_t *rva, uint32_t *vsize, uint32_t *raw_size, uint32_t *raw_off) |
| 145 | { |
| 146 | uint64_t s = (uint64_t)pi->sect_off + (uint64_t)i * 40u; |
| 147 | if (s + 40 > img_len) return -1; |
| 148 | uint32_t so = (uint32_t)s; |
| 149 | if (pe_read32(img, img_len, so + 8, vsize) || pe_read32(img, img_len, so + 12, rva) || |
| 150 | pe_read32(img, img_len, so + 16, raw_size) || pe_read32(img, img_len, so + 20, raw_off)) |
| 151 | return -1; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* File offset of the byte at image RVA rva (bytes that exist in the file only). */ |
| 156 | static int pe_rva_off(const uint8_t *img, size_t img_len, const struct pe_info *pi, |
| 157 | uint32_t rva, uint32_t *off) |
| 158 | { |
| 159 | for (unsigned i = 0; i < pi->nsections; i++) { |
| 160 | uint32_t s_rva, s_vs, s_raw, s_off; |
| 161 | if (pe_section(img, img_len, pi, i, &s_rva, &s_vs, &s_raw, &s_off)) return -1; |
| 162 | if (rva < s_rva) continue; |
| 163 | uint32_t d = rva - s_rva; |
| 164 | if (d >= s_raw || d >= s_vs) continue; |
| 165 | if ((uint64_t)s_off + d + 8 > img_len) return -1; |
| 166 | *off = s_off + d; |
| 167 | return 0; |
| 168 | } |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | /* Applies the base relocations (IMAGE_REL_BASED_DIR64 only) of the staged image, |
| 173 | * moving every absolute address by delta. A PE with no relocation table cannot be |
| 174 | * placed anywhere else, so it is refused; so is any other fix-up type. */ |
| 175 | static int pe_relocate(uint8_t *img, size_t img_len, const struct pe_info *pi, uint64_t delta) |
| 176 | { |
| 177 | uint32_t dir_rva, dir_size; |
| 178 | if (pe_read32(img, img_len, pi->pe_off + PE_DD_BASEREL, &dir_rva) || |
| 179 | pe_read32(img, img_len, pi->pe_off + PE_DD_BASEREL + 4, &dir_size)) |
| 180 | return -1; |
| 181 | if (!dir_rva || !dir_size) return -1; |
| 182 | uint32_t p = 0; |
| 183 | while (p + 8 <= dir_size) { |
| 184 | uint32_t blk; |
| 185 | if (pe_rva_off(img, img_len, pi, dir_rva + p, &blk)) return -1; |
| 186 | uint32_t page_rva, blk_size; |
| 187 | if (pe_read32(img, img_len, blk, &page_rva) || pe_read32(img, img_len, blk + 4, &blk_size)) |
| 188 | return -1; |
| 189 | if (blk_size < 8 || p + blk_size > dir_size) return -1; |
| 190 | for (uint32_t e = 8; e + 2 <= blk_size; e += 2) { |
| 191 | uint16_t ent; |
| 192 | if (pe_read16(img, img_len, blk + e, &ent)) return -1; |
| 193 | unsigned type = ent >> 12; |
| 194 | if (type == 0) continue; /* ABSOLUTE: padding */ |
| 195 | if (type != 10) return -1; /* only DIR64 is handled */ |
| 196 | uint32_t tgt; |
| 197 | if (pe_rva_off(img, img_len, pi, page_rva + (ent & 0xfffu), &tgt)) return -1; |
| 198 | uint64_t v; |
| 199 | memcpy(&v, img + tgt, 8); |
| 200 | v += delta; |
| 201 | memcpy(img + tgt, &v, 8); |
| 202 | } |
| 203 | p += blk_size; |
| 204 | } |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /* Lays the sections out from load_base. extent = max(RVA + VirtualSize). */ |
| 209 | static int pe_sections(const uint8_t *img, size_t img_len, const struct pe_info *pi, |
| 210 | uint64_t load_base, struct swap_sect *sect, unsigned *nsect, uint64_t *extent) |
| 211 | { |
| 212 | *nsect = 0; |
| 213 | *extent = 0; |
| 214 | for (unsigned i = 0; i < pi->nsections; i++) { |
| 215 | uint32_t rva, vsize, raw_size, raw_off; |
| 216 | if (pe_section(img, img_len, pi, i, &rva, &vsize, &raw_size, &raw_off)) return -1; |
| 217 | if ((uint64_t)rva + vsize > *extent) *extent = (uint64_t)rva + vsize; |
| 218 | if (!vsize) continue; |
| 219 | uint64_t dst = load_base + rva; |
| 220 | uint32_t copy = raw_size < vsize ? raw_size : vsize; |
| 221 | if (copy) { |
| 222 | if ((uint64_t)raw_off + copy > img_len) return -1; |
| 223 | if (*nsect >= SWAP_SECT_MAX) return -1; |
| 224 | sect[*nsect].src_pa = (uint64_t)(uintptr_t)(img + raw_off); |
| 225 | sect[*nsect].dst_pa = dst; |
| 226 | sect[*nsect].len = copy; |
| 227 | sect[*nsect].flags = 0; |
| 228 | (*nsect)++; |
| 229 | } |
| 230 | if (vsize > copy) { |
| 231 | if (*nsect >= SWAP_SECT_MAX) return -1; |
| 232 | sect[*nsect].src_pa = 0; |
| 233 | sect[*nsect].dst_pa = dst + copy; |
| 234 | sect[*nsect].len = vsize - copy; |
| 235 | sect[*nsect].flags = SWAP_SECT_ZERO; |
| 236 | (*nsect)++; |
| 237 | } |
| 238 | } |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | /* ---- Verification: the netboot loader's own function and key table ---- */ |
| 243 | /* obj = slot object (manifest unit + image). Returns 0 and the image pointer when |
| 244 | * ck_slot_verify says valid; -1 with the reason printed otherwise. */ |
| 245 | static int slot_verify_swap(const uint8_t *obj, size_t obj_len, |
| 246 | const uint8_t **img_out, uint32_t *img_len_out) |
| 247 | { |
| 248 | if (obj_len < CK_SLOT_UNIT) { |
| 249 | ck_puts("swap: payload too short for a manifest\n"); |
| 250 | return -1; |
| 251 | } |
| 252 | const struct ck_slot_manifest *m = (const struct ck_slot_manifest *)(const void *)obj; |
| 253 | struct ck_slot_eval ev; |
| 254 | ck_slot_verify(m, obj + CK_SLOT_UNIT, obj_len - CK_SLOT_UNIT, |
| 255 | ck_slot_loader_keys, ck_slot_loader_nkeys, &ev); |
| 256 | if (ev.valid && m->image_len != obj_len - CK_SLOT_UNIT) { |
| 257 | ev.valid = 0; |
| 258 | ev.why = "image_len differs from the bytes received"; |
| 259 | } |
| 260 | if (!ev.valid) { |
| 261 | ck_printf("swap: REJECTED (%s)\n", ev.why ? ev.why : "invalid"); |
| 262 | return -1; |
| 263 | } |
| 264 | *img_out = obj + CK_SLOT_UNIT; |
| 265 | *img_len_out = (uint32_t)m->image_len; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* ---- Quiesce helpers ---- */ |
| 270 | static void quiesce_aps(void) |
| 271 | { |
| 272 | ck_x86_panel_off(); |
| 273 | /* INIT puts every other core in wait-for-SIPI: none runs this image while the copy |
| 274 | * replaces it (kmain_x86.c). */ |
| 275 | ck_x86_swap_stop_aps(); |
| 276 | } |
| 277 | |
| 278 | /* Defined in kmain_x86.c: reopens the trampoline page for the lab swap jump. */ |
| 279 | void ck_x86_swap_tramp_open(void); |
| 280 | |
| 281 | /* ---- Trampoline code (position-independent, copied to tramp_pa+512) ---- */ |
| 282 | /* Defined in swap_tramp.S */ |
| 283 | extern char ck_x86_swap_tramp[]; |
| 284 | extern char ck_x86_swap_tramp_end[]; |
| 285 | |
| 286 | /* ---- Disk-staged swap (QEMU test only: CK_X86_SWAP_TEST_DISK) ---- */ |
| 287 | #ifdef CK_X86_SWAP_TEST_DISK |
| 288 | #define SWAP_TEST_DISK_INDEX 2u /* the test's third SATA disk (ahci_x86.c index) */ |
| 289 | #define SWAP_TEST_LBA 2048u |
| 290 | extern int ck_x86_ahci_read_lba(unsigned ctrl, unsigned disk_idx, uint64_t lba, |
| 291 | unsigned count, uint8_t *buf); |
| 292 | static int disk_stage_triggered; |
| 293 | |
| 294 | static int swap_test_disk_sectors(uint64_t lba, unsigned count, uint8_t *buf) |
| 295 | { |
| 296 | return ck_x86_ahci_read_lba(0, SWAP_TEST_DISK_INDEX, lba, count, buf); |
| 297 | } |
| 298 | |
| 299 | /* Reads the raw slot object at SWAP_TEST_LBA. Returns 0 and the length on success. */ |
| 300 | static int swap_read_test_disk(uint8_t *buf, size_t cap, size_t *len_out) |
| 301 | { |
| 302 | return ck_swap_disk_load(swap_test_disk_sectors, SWAP_TEST_LBA, buf, cap, len_out); |
| 303 | } |
| 304 | |
| 305 | static void try_disk_stage(void) |
| 306 | { |
| 307 | if (disk_stage_triggered || !swap_ready || !swap_buf_pa) |
| 308 | return; |
| 309 | disk_stage_triggered = 1; |
| 310 | /* The test disk is read only and staged for the first kernel: a kernel that came in by the |
| 311 | * swap would find the same object and swap into itself again, and again. */ |
| 312 | if (ck_x86_swapped_in) { |
| 313 | ck_puts("swap: entered by a swap, the test disk inbox is left alone\n"); |
| 314 | return; |
| 315 | } |
| 316 | size_t len = 0; |
| 317 | if (swap_read_test_disk((uint8_t *)(uintptr_t)swap_buf_pa, SWAP_BUF_BYTES, &len)) |
| 318 | return; |
| 319 | swap_total = (uint32_t)len; |
| 320 | swap_received = (uint32_t)len; |
| 321 | } |
| 322 | #endif /* CK_X86_SWAP_TEST_DISK */ |
| 323 | |
| 324 | /* ---- Public API ---- */ |
| 325 | |
| 326 | void ck_x86_swap_init(struct ck_frames *fr) |
| 327 | { |
| 328 | uint64_t pa; |
| 329 | if (ck_frames_alloc(fr, SWAP_BUF_PAGES, CK_PAGE, &pa)) { |
| 330 | ck_puts("swap: no memory for swap buffer (swap disabled)\n"); |
| 331 | return; |
| 332 | } |
| 333 | uint64_t stk; |
| 334 | if (ck_frames_alloc(fr, SWAP_STACK_PAGES, CK_PAGE, &stk)) { |
| 335 | ck_puts("swap: no memory for the swap stack (swap disabled)\n"); |
| 336 | return; |
| 337 | } |
| 338 | if (ck_frames_alloc(fr, SWAP_MAP_PAGES, CK_PAGE, &swap_map_pa)) { |
| 339 | ck_puts("swap: no memory for the memory map copy (swap disabled)\n"); |
| 340 | return; |
| 341 | } |
| 342 | swap_stack_top = stk + (uint64_t)SWAP_STACK_PAGES * CK_PAGE - 16; |
| 343 | swap_buf_pa = pa; |
| 344 | swap_ready = 1; |
| 345 | ck_printf("swap: ready buf=0x%llx cap=%u KiB port=%u receiver=%s\n", |
| 346 | (unsigned long long)pa, (unsigned)(SWAP_BUF_BYTES >> 10), |
| 347 | (unsigned)CK_X86_SWAP_PORT, |
| 348 | #ifdef CK_X86_SWAP_TEST_DISK |
| 349 | "disk-test" |
| 350 | #else |
| 351 | "udp-not-built" |
| 352 | #endif |
| 353 | ); |
| 354 | } |
| 355 | |
| 356 | /* Drops the staged payload and says why. Always returns 0 (the kernel keeps running). */ |
| 357 | static int swap_refuse(const char *why) |
| 358 | { |
| 359 | ck_printf("swap: refused (%s), resuming normal operation\n", why); |
| 360 | swap_received = swap_total = 0; |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /* Called from the hold loop. Returns 1 if a swap was performed (never returns |
| 365 | * normally after a successful swap since we jump to the new kernel). */ |
| 366 | int ck_x86_swap_step(const struct ck_handoff *hand, uint64_t tramp_pa) |
| 367 | { |
| 368 | if (!swap_ready) return 0; |
| 369 | |
| 370 | #ifdef CK_X86_SWAP_TEST_DISK |
| 371 | try_disk_stage(); |
| 372 | #endif |
| 373 | /* No UDP receiver is built (port 47034 is design only, see the header): |
| 374 | * a payload exists only when the test path staged one. */ |
| 375 | if (swap_total == 0 || swap_received < swap_total) |
| 376 | return 0; |
| 377 | |
| 378 | /* We have a complete payload in swap_buf_pa..swap_buf_pa+swap_total. */ |
| 379 | ck_puts("swap: payload complete, verifying\n"); |
| 380 | const uint8_t *img; |
| 381 | uint32_t img_len; |
| 382 | if (slot_verify_swap((const uint8_t *)(uintptr_t)swap_buf_pa, swap_total, &img, &img_len)) { |
| 383 | ck_puts("swap: resuming normal operation\n"); |
| 384 | swap_received = swap_total = 0; |
| 385 | return 0; |
| 386 | } |
| 387 | ck_puts("swap: verified ok, placing the new image\n"); |
| 388 | |
| 389 | /* Place the image at the running kernel's base (see the header, step 2). */ |
| 390 | struct pe_info pi; |
| 391 | if (pe_headers(img, img_len, &pi)) |
| 392 | return swap_refuse("not a PE32+ image"); |
| 393 | uint64_t base = hand->image_base; |
| 394 | if (!base || hand->image_end <= base) |
| 395 | return swap_refuse("the handoff gives no running image range"); |
| 396 | if (pe_relocate((uint8_t *)(uintptr_t)img, img_len, &pi, base - pi.image_base)) |
| 397 | return swap_refuse("PE base relocations missing or not DIR64"); |
| 398 | struct swap_sect sects[SWAP_SECT_MAX]; |
| 399 | unsigned nsect = 0; |
| 400 | uint64_t extent = 0; |
| 401 | if (pe_sections(img, img_len, &pi, base, sects, &nsect, &extent) || !nsect) |
| 402 | return swap_refuse("PE sections unreadable or too many"); |
| 403 | if (extent > hand->image_end - base) |
| 404 | return swap_refuse("the new image is larger than the running one"); |
| 405 | if (!hand->memory_map || hand->map_size > (uint64_t)SWAP_MAP_PAGES * CK_PAGE) |
| 406 | return swap_refuse("the memory map does not fit the swap's copy"); |
| 407 | uint64_t entry = base + pi.entry_rva; |
| 408 | ck_printf("swap: PE ok base=0x%llx entry=0x%llx sections=%u extent=0x%llx\n", |
| 409 | (unsigned long long)base, (unsigned long long)entry, nsect, |
| 410 | (unsigned long long)extent); |
| 411 | |
| 412 | if (!tramp_pa) { |
| 413 | ck_puts("swap: no tramp page (no free page below 1 MiB was available at boot), swap refused\n"); |
| 414 | swap_received = swap_total = 0; |
| 415 | return 0; |
| 416 | } |
| 417 | /* The trampoline code must fit its page after TRAMP_CODE_OFF. Checked here, before any |
| 418 | * core or device is stopped and before any page is made writable and executable. */ |
| 419 | size_t tramp_code_bytes = (size_t)(ck_x86_swap_tramp_end - ck_x86_swap_tramp); |
| 420 | if (tramp_code_bytes > CK_PAGE - TRAMP_CODE_OFF) |
| 421 | return swap_refuse("the trampoline does not fit its page"); |
| 422 | |
| 423 | /* Quiesce devices. */ |
| 424 | quiesce_aps(); |
| 425 | ck_x86_xhci_stop_all(); |
| 426 | ck_x86_tg3_stop(); |
| 427 | ck_x86_hda_stop(); |
| 428 | |
| 429 | /* The trampoline page (read-only no-execute after smp_start) and the running image |
| 430 | * (W^X) become writable and executable here, after the verify, for this one jump. */ |
| 431 | ck_x86_swap_tramp_open(); |
| 432 | |
| 433 | /* Build rebuilt handoff in the tramp page. */ |
| 434 | uint8_t *tp = (uint8_t *)(uintptr_t)tramp_pa; |
| 435 | /* Section table. */ |
| 436 | struct swap_sect *st = (struct swap_sect *)(tp + TRAMP_SECT_OFF); |
| 437 | for (unsigned i = 0; i < nsect; i++) |
| 438 | st[i] = sects[i]; |
| 439 | /* Sentinel at end. */ |
| 440 | if (nsect < SWAP_SECT_MAX) { |
| 441 | st[nsect].src_pa = 0; |
| 442 | st[nsect].dst_pa = 0; |
| 443 | st[nsect].len = 0; |
| 444 | st[nsect].flags = 0xFFu; |
| 445 | st[nsect]._pad = 0; |
| 446 | } |
| 447 | /* Entry point. */ |
| 448 | uint64_t *ep = (uint64_t *)(tp + TRAMP_ENTRY_OFF); |
| 449 | *ep = entry; |
| 450 | /* Handoff: the new kernel runs at the same base, with the same range. */ |
| 451 | struct ck_handoff *new_hand = (struct ck_handoff *)(tp + TRAMP_HAND_OFF); |
| 452 | memcpy(new_hand, hand, sizeof *hand); |
| 453 | new_hand->exit_attempts = 1; |
| 454 | new_hand->image_base = base; |
| 455 | new_hand->image_end = base + extent; |
| 456 | /* The memory map, out of the image the trampoline overwrites (size checked before the quiesce). */ |
| 457 | memcpy((void *)(uintptr_t)swap_map_pa, (const void *)(uintptr_t)hand->memory_map, hand->map_size); |
| 458 | new_hand->memory_map = swap_map_pa; |
| 459 | /* uefi_entry_ticks: preserve original (the clock since power-on). */ |
| 460 | |
| 461 | /* Stack for the new kernel: a page the image copy cannot reach. */ |
| 462 | *(uint64_t *)(tp + TRAMP_STACK_OFF) = swap_stack_top; |
| 463 | *(uint64_t *)(tp + TRAMP_MAGIC_OFF) = CK_X86_SWAP_ENTRY_MAGIC; |
| 464 | /* Copy trampoline code to tramp_pa + TRAMP_CODE_OFF (its size checked before the quiesce). */ |
| 465 | memcpy(tp + TRAMP_CODE_OFF, ck_x86_swap_tramp, tramp_code_bytes); |
| 466 | __asm__ volatile("wbinvd" ::: "memory"); |
| 467 | |
| 468 | ck_puts("swap: jumping to new kernel\n"); |
| 469 | /* Jump to the trampoline at tramp_pa + TRAMP_CODE_OFF. */ |
| 470 | typedef void (*tramp_fn)(uint64_t tramp_pa); |
| 471 | tramp_fn fn = (tramp_fn)(uintptr_t)(tramp_pa + TRAMP_CODE_OFF); |
| 472 | fn(tramp_pa); |
| 473 | __builtin_unreachable(); |
| 474 | } |
| 475 | |
| 476 | #else /* !CK_X86_SWAP */ |
| 477 | void ck_x86_swap_init(struct ck_frames *fr) { (void)fr; } |
| 478 | int ck_x86_swap_step(const struct ck_handoff *h, uint64_t tramp_pa) { (void)h; (void)tramp_pa; return 0; } |
| 479 | #endif /* CK_X86_SWAP */ |