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 / boot / efi_model.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 | /* efi_model.c -- read the yardstick model from the boot disk before |
| 18 | * ExitBootServices, through the firmware's own Block I/O driver (the one that |
| 19 | * just loaded BOOTAA64.EFI), and hand it to the kernel in CHANDOF2 |
| 20 | * (aienos#34 lane 6: the first model ingest path that exists on hardware). |
| 21 | * |
| 22 | * Why the firmware and not our NVMe driver: our C NVMe driver and its SMMU |
| 23 | * confinement have never run on the DGX Spark (GB10_NATIVE_STATUS: NVMe |
| 24 | * unconfirmed), the inference image is the core-only image without the NVMe |
| 25 | * stage, and one attended boot must not gamble. UEFI Block I/O is the path |
| 26 | * the firmware already proved by loading this image. |
| 27 | * |
| 28 | * Flow (UEFI 2.10; edk2 LoadedImage.h, SimpleFileSystem.h, BlockIo.h): |
| 29 | * 1. HandleProtocol(image, LoadedImage) -> DeviceHandle = the boot volume; |
| 30 | * HandleProtocol(DeviceHandle, SimpleFileSystem) -> OpenVolume -> Open |
| 31 | * \EFI\AIENOS\MODEL.MAP read-only. Absent map: "model_map: absent", |
| 32 | * nothing recorded, boot continues (the kernel then has no disk model). |
| 33 | * 2. ck_model_map_check (native/boot/model_map.h): magic, version, sector |
| 34 | * size, extents, exact coverage. Refused -> "model_map: refused (...)". |
| 35 | * 3. LocateHandleBuffer(ByProtocol, BlockIo): among whole disks |
| 36 | * (LogicalPartition == 0, MediaPresent, BlockSize == map sector size) |
| 37 | * read LBA 1 and match "EFI PART" + disk GUID bytes 56..71 with the map. |
| 38 | * 4. AllocatePages(AllocateAnyPages, EfiLoaderData): the kernel never hands |
| 39 | * LoaderData out (mm/mmu.c) and reserves this range explicitly. |
| 40 | * 5. ReadBlocks per extent in <= 8 MiB pieces, BufferSize a multiple of |
| 41 | * BlockSize, buffers page aligned (>= IoAlign for every driver seen). |
| 42 | * 6. Record base, length, declared SHA-256, flags, timing in the handoff. |
| 43 | * The stub does NOT hash: the kernel recomputes SHA-256 over the bytes it |
| 44 | * received (core/infer.c) and refuses to decode on a mismatch, so a wrong LBA, |
| 45 | * a short read or a stale file can never pass as the model. */ |
| 46 | #include "efi.h" |
| 47 | #include "model_map.h" |
| 48 | #include "handoff.h" |
| 49 | #include "../kernel/arch/arch.h" |
| 50 | #include "../kernel/core/ck_internal.h" |
| 51 | |
| 52 | #define READ_CHUNK (8ull << 20) |
| 53 | #define PAGE 4096ull |
| 54 | |
| 55 | static struct ck_model_map map __attribute__((aligned(4096))); |
| 56 | static uint8_t lba1[4096] __attribute__((aligned(4096))); |
| 57 | |
| 58 | |
| 59 | static void hex(char *out, const uint8_t *b, unsigned n) |
| 60 | { |
| 61 | static const char d[] = "0123456789abcdef"; |
| 62 | for (unsigned i = 0; i < n; i++) { |
| 63 | out[2 * i] = d[b[i] >> 4]; |
| 64 | out[2 * i + 1] = d[b[i] & 15]; |
| 65 | } |
| 66 | out[2 * n] = 0; |
| 67 | } |
| 68 | |
| 69 | static uint64_t ticks_to_us(uint64_t ticks, uint64_t hz) |
| 70 | { |
| 71 | return hz ? (ticks / hz) * 1000000ull + ((ticks % hz) * 1000000ull) / hz : 0; |
| 72 | } |
| 73 | |
| 74 | static void clear_model(struct ck_handoff *h) |
| 75 | { |
| 76 | h->model_base = h->model_len = 0; |
| 77 | memset(h->model_sha256, 0, sizeof h->model_sha256); |
| 78 | h->model_flags = h->model_extents = 0; |
| 79 | h->model_read_us = h->model_disk_last_block = 0; |
| 80 | h->model_block_size = 0; |
| 81 | } |
| 82 | |
| 83 | /* Read MODEL.MAP from the volume this image was loaded from. 1 = absent, |
| 84 | * -1 = present but unreadable/malformed (printed), 0 = in `map`. */ |
| 85 | static int read_map(EFI_HANDLE image, EFI_BOOT_SERVICES *bs) |
| 86 | { |
| 87 | static const EFI_GUID li_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID_INIT; |
| 88 | static const EFI_GUID fs_guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID_INIT; |
| 89 | static CHAR16 path[] = {'\\', 'E', 'F', 'I', '\\', 'A', 'I', 'E', 'N', 'O', 'S', '\\', |
| 90 | 'M', 'O', 'D', 'E', 'L', '.', 'M', 'A', 'P', 0}; |
| 91 | EFI_LOADED_IMAGE_PROTOCOL *li = 0; |
| 92 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs = 0; |
| 93 | EFI_FILE_PROTOCOL *root = 0, *f = 0; |
| 94 | EFI_STATUS s = bs->HandleProtocol(image, &li_guid, (void **)&li); |
| 95 | if (s != EFI_SUCCESS || !li || !li->DeviceHandle) { |
| 96 | ck_printf("model_map: absent (no LoadedImage device, status=0x%llx)\n", (unsigned long long)s); |
| 97 | return 1; |
| 98 | } |
| 99 | s = bs->HandleProtocol(li->DeviceHandle, &fs_guid, (void **)&fs); |
| 100 | if (s != EFI_SUCCESS || !fs) { |
| 101 | ck_printf("model_map: absent (boot volume has no file system, status=0x%llx)\n", |
| 102 | (unsigned long long)s); |
| 103 | return 1; |
| 104 | } |
| 105 | s = fs->OpenVolume(fs, &root); |
| 106 | if (s != EFI_SUCCESS || !root) { |
| 107 | ck_printf("model_map: absent (OpenVolume status=0x%llx)\n", (unsigned long long)s); |
| 108 | return 1; |
| 109 | } |
| 110 | s = root->Open(root, &f, path, EFI_FILE_MODE_READ, 0); |
| 111 | if (s != EFI_SUCCESS || !f) { |
| 112 | ck_puts("model_map: absent (no " CK_MODEL_MAP_PATH " on the boot volume)\n"); |
| 113 | root->Close(root); |
| 114 | return 1; |
| 115 | } |
| 116 | uint64_t n = sizeof map; |
| 117 | memset(&map, 0, sizeof map); |
| 118 | s = f->Read(f, &n, &map); |
| 119 | f->Close(f); |
| 120 | root->Close(root); |
| 121 | if (s != EFI_SUCCESS || n != sizeof map) { |
| 122 | ck_printf("model_map: refused (read status=0x%llx bytes=%llu, want %u)\n", |
| 123 | (unsigned long long)s, (unsigned long long)n, (unsigned)sizeof map); |
| 124 | return -1; |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /* The whole disk whose GPT disk GUID matches the map; 0 if none. */ |
| 130 | static EFI_BLOCK_IO_PROTOCOL *find_disk(EFI_BOOT_SERVICES *bs, uint64_t *seen) |
| 131 | { |
| 132 | static const EFI_GUID bio_guid = EFI_BLOCK_IO_PROTOCOL_GUID_INIT; |
| 133 | EFI_HANDLE *handles = 0; |
| 134 | uint64_t n = 0; |
| 135 | EFI_BLOCK_IO_PROTOCOL *found = 0; |
| 136 | *seen = 0; |
| 137 | EFI_STATUS s = bs->LocateHandleBuffer(ByProtocol, &bio_guid, 0, &n, &handles); |
| 138 | if (s != EFI_SUCCESS || !handles) |
| 139 | return 0; |
| 140 | *seen = n; |
| 141 | for (uint64_t i = 0; i < n && !found; i++) { |
| 142 | EFI_BLOCK_IO_PROTOCOL *bio = 0; |
| 143 | if (bs->HandleProtocol(handles[i], &bio_guid, (void **)&bio) != EFI_SUCCESS || !bio || !bio->Media) |
| 144 | continue; |
| 145 | const EFI_BLOCK_IO_MEDIA *m = bio->Media; |
| 146 | if (!m->MediaPresent || m->LogicalPartition || m->BlockSize != map.sector_size || |
| 147 | m->BlockSize > sizeof lba1 || m->LastBlock < 2) |
| 148 | continue; |
| 149 | if (bio->ReadBlocks(bio, m->MediaId, 1, m->BlockSize, lba1) != EFI_SUCCESS) |
| 150 | continue; |
| 151 | if (memcmp(lba1, "EFI PART", 8) == 0 && memcmp(lba1 + 56, map.disk_guid, 16) == 0) |
| 152 | found = bio; |
| 153 | } |
| 154 | bs->FreePool(handles); |
| 155 | return found; |
| 156 | } |
| 157 | |
| 158 | /* 0: model loaded and recorded in h; 1: no MODEL.MAP (not an error); |
| 159 | * -1: map present but refused or the read failed (printed, nothing recorded). */ |
| 160 | int ck_boot_model_load(EFI_HANDLE image, EFI_SYSTEM_TABLE *st, struct ck_handoff *h) |
| 161 | { |
| 162 | EFI_BOOT_SERVICES *bs = st->BootServices; |
| 163 | char gh[33], sh[65]; |
| 164 | clear_model(h); |
| 165 | int rc = read_map(image, bs); |
| 166 | if (rc) |
| 167 | return rc; |
| 168 | const char *why = 0; |
| 169 | if (ck_model_map_check(&map, 0, 0, &why)) { |
| 170 | ck_printf("model_map: refused (%s)\n", why); |
| 171 | return -1; |
| 172 | } |
| 173 | hex(gh, map.disk_guid, 16); |
| 174 | hex(sh, map.sha256, 32); |
| 175 | ck_printf("model_map: ok len=%llu extents=%u sector=%u disk=%s\n", (unsigned long long)map.model_len, |
| 176 | map.n_extents, map.sector_size, gh); |
| 177 | |
| 178 | uint64_t seen = 0; |
| 179 | EFI_BLOCK_IO_PROTOCOL *bio = find_disk(bs, &seen); |
| 180 | if (!bio) { |
| 181 | ck_printf("model_map: refused (disk %s not found among %llu block devices)\n", gh, |
| 182 | (unsigned long long)seen); |
| 183 | return -1; |
| 184 | } |
| 185 | const EFI_BLOCK_IO_MEDIA *m = bio->Media; |
| 186 | if (ck_model_map_check(&map, m->BlockSize, m->LastBlock, &why)) { |
| 187 | ck_printf("model_map: refused (%s)\n", why); |
| 188 | return -1; |
| 189 | } |
| 190 | if (m->IoAlign > PAGE) { |
| 191 | ck_printf("model_map: refused (IoAlign %u > page)\n", m->IoAlign); |
| 192 | return -1; |
| 193 | } |
| 194 | |
| 195 | uint64_t bs_bytes = m->BlockSize; |
| 196 | uint64_t sectors = (map.model_len + bs_bytes - 1) / bs_bytes; |
| 197 | uint64_t pages = (sectors * bs_bytes + PAGE - 1) / PAGE; |
| 198 | uint64_t base = 0; |
| 199 | EFI_STATUS s = bs->AllocatePages(AllocateAnyPages, EfiLoaderData, pages, &base); |
| 200 | if (s != EFI_SUCCESS || !base) { |
| 201 | ck_printf("model_map: refused (AllocatePages %llu pages status=0x%llx)\n", |
| 202 | (unsigned long long)pages, (unsigned long long)s); |
| 203 | return -1; |
| 204 | } |
| 205 | ck_printf("model: reading %llu bytes in %u extents, block_size=%u last_block=%llu -> 0x%llx\n", |
| 206 | (unsigned long long)map.model_len, map.n_extents, m->BlockSize, |
| 207 | (unsigned long long)m->LastBlock, (unsigned long long)base); |
| 208 | |
| 209 | uint64_t t0 = ck_rd(cntpct_el0), off = 0; |
| 210 | for (uint32_t i = 0; i < map.n_extents; i++) { |
| 211 | uint64_t lba = map.ext[i].lba, left = map.ext[i].count; |
| 212 | while (left) { |
| 213 | uint64_t n = left * bs_bytes > READ_CHUNK ? READ_CHUNK / bs_bytes : left; |
| 214 | s = bio->ReadBlocks(bio, m->MediaId, lba, n * bs_bytes, (void *)(uintptr_t)(base + off)); |
| 215 | if (s != EFI_SUCCESS) { |
| 216 | ck_printf("model: ReadBlocks status=0x%llx at lba=%llu count=%llu (extent %u)\n", |
| 217 | (unsigned long long)s, (unsigned long long)lba, (unsigned long long)n, i); |
| 218 | bs->FreePages(base, pages); |
| 219 | return -1; |
| 220 | } |
| 221 | lba += n; |
| 222 | left -= n; |
| 223 | off += n * bs_bytes; |
| 224 | } |
| 225 | } |
| 226 | uint64_t us = ticks_to_us(ck_rd(cntpct_el0) - t0, h->counter_freq_hz); |
| 227 | |
| 228 | h->model_base = base; |
| 229 | h->model_len = map.model_len; |
| 230 | memcpy(h->model_sha256, map.sha256, 32); |
| 231 | h->model_flags = CK_HANDOFF_MODEL_PRESENT | CK_HANDOFF_MODEL_BLOCKIO; |
| 232 | h->model_extents = map.n_extents; |
| 233 | h->model_read_us = us; |
| 234 | h->model_disk_last_block = m->LastBlock; |
| 235 | h->model_block_size = m->BlockSize; |
| 236 | ck_printf("model: base=0x%llx len=%llu extents=%u read_us=%llu declared_sha256=%s\n", |
| 237 | (unsigned long long)base, (unsigned long long)map.model_len, map.n_extents, |
| 238 | (unsigned long long)us, sh); |
| 239 | return 0; |
| 240 | } |