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 / diag_x86.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 | /* diag_x86.c -- AIENOS-IMAC-BOOT-0: the x86_64 part of the UEFI diagnostic |
| 18 | * (diag_main.c). Read-only inventory of what a port needs to know: |
| 19 | * |
| 20 | * cpu_* CPUID vendor, brand, family/model/stepping, topology, features, |
| 21 | * and the mode the firmware left the CPU in (CPL, paging, EFER.LMA) |
| 22 | * smbios_* SMBIOS type 0/1/2 identity (BIOS version, Mac model, board id). |
| 23 | * Serial numbers (type 1/2 offset 7) are never read. |
| 24 | * pci: every PCI function, read through configuration mechanism #1 |
| 25 | * (ports 0xCF8/0xCFC, PCI Local Bus 3.0 section 3.2.2.3.2); reads only |
| 26 | * gop: every Graphics Output Protocol mode the firmware set |
| 27 | * block: every whole-disk Block I/O device (no partitions) |
| 28 | * |
| 29 | * Built with -mabi=ms (all functions use the UEFI x86_64 calling convention), |
| 30 | * so the firmware function pointers in efi.h need no annotations. */ |
| 31 | #include <stdarg.h> |
| 32 | #include <string.h> |
| 33 | #include "efi.h" |
| 34 | #include "diag.h" |
| 35 | |
| 36 | #define say diag_say |
| 37 | |
| 38 | static uint16_t rd16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); } |
| 39 | static uint32_t rd32(const uint8_t *p) { return (uint32_t)rd16(p) | (uint32_t)rd16(p + 2) << 16; } |
| 40 | static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | (uint64_t)rd32(p + 4) << 32; } |
| 41 | |
| 42 | /* ---- console: Apple firmware starts in graphics mode, where ConOut text is |
| 43 | * invisible until EFI_CONSOLE_CONTROL_PROTOCOL (Intel EFI 1.x, kept by Apple) |
| 44 | * selects text mode. Absent on edk2/OVMF: nothing happens. ---- */ |
| 45 | #define CONSOLE_CONTROL_GUID_INIT \ |
| 46 | { 0xf42f7782, 0x012e, 0x4c12, { 0x99, 0x56, 0x49, 0xf9, 0x43, 0x04, 0xf7, 0x21 } } |
| 47 | typedef struct CONSOLE_CONTROL { |
| 48 | void *GetMode; |
| 49 | EFI_STATUS (*SetMode)(struct CONSOLE_CONTROL *This, uint32_t Mode); /* 0: text */ |
| 50 | void *LockStdIn; |
| 51 | } CONSOLE_CONTROL; |
| 52 | |
| 53 | void diag_x86_console(EFI_SYSTEM_TABLE *st) |
| 54 | { |
| 55 | static const EFI_GUID guid = CONSOLE_CONTROL_GUID_INIT; |
| 56 | EFI_BOOT_SERVICES *bs = st->BootServices; |
| 57 | EFI_HANDLE *h = 0; |
| 58 | uint64_t n = 0; |
| 59 | if (bs->LocateHandleBuffer(ByProtocol, &guid, 0, &n, &h) != EFI_SUCCESS) |
| 60 | return; |
| 61 | for (uint64_t i = 0; i < n; i++) { |
| 62 | CONSOLE_CONTROL *cc = 0; |
| 63 | if (bs->HandleProtocol(h[i], &guid, (void **)&cc) == EFI_SUCCESS && cc && cc->SetMode) |
| 64 | cc->SetMode(cc, 0); |
| 65 | } |
| 66 | bs->FreePool(h); |
| 67 | } |
| 68 | |
| 69 | /* ---- CPU (Intel SDM vol. 2A, CPUID) ---- */ |
| 70 | static void cpuid(uint32_t leaf, uint32_t sub, uint32_t r[4]) |
| 71 | { |
| 72 | __asm__ volatile("cpuid" : "=a"(r[0]), "=b"(r[1]), "=c"(r[2]), "=d"(r[3]) : "a"(leaf), "c"(sub)); |
| 73 | } |
| 74 | |
| 75 | struct feat { |
| 76 | uint8_t reg, bit; /* reg: 0 leaf1.ecx 1 leaf1.edx 2 leaf7.ebx 3 leaf7.ecx 4 ext1.edx 5 ext7.edx */ |
| 77 | const char *name; |
| 78 | }; |
| 79 | static const struct feat feats[] = { |
| 80 | {0, 0, "sse3"}, {0, 9, "ssse3"}, {0, 12, "fma"}, {0, 19, "sse4.1"}, {0, 20, "sse4.2"}, |
| 81 | {0, 21, "x2apic"}, {0, 23, "popcnt"}, {0, 25, "aes"}, {0, 26, "xsave"}, {0, 28, "avx"}, |
| 82 | {0, 29, "f16c"}, {0, 30, "rdrand"}, {0, 31, "hypervisor"}, {1, 4, "tsc"}, {1, 9, "apic"}, |
| 83 | {1, 26, "sse2"}, {2, 3, "bmi1"}, {2, 5, "avx2"}, {2, 8, "bmi2"}, {2, 9, "erms"}, |
| 84 | {2, 10, "invpcid"}, {2, 16, "avx512f"}, {2, 18, "rdseed"}, {2, 19, "adx"}, {2, 20, "smap"}, |
| 85 | {2, 7, "smep"}, {2, 29, "sha"}, {3, 2, "umip"}, {3, 11, "avx512vnni"}, {4, 20, "nx"}, |
| 86 | {4, 26, "pdpe1gb"}, {4, 27, "rdtscp"}, {4, 29, "lm"}, {5, 8, "invariant_tsc"}, |
| 87 | }; |
| 88 | |
| 89 | void diag_x86_cpu(void) |
| 90 | { |
| 91 | uint32_t r[4], max, ext, reg[6] = {0}; |
| 92 | char vendor[13], brand[49]; |
| 93 | cpuid(0, 0, r); |
| 94 | max = r[0]; |
| 95 | memcpy(vendor, &r[1], 4); |
| 96 | memcpy(vendor + 4, &r[3], 4); |
| 97 | memcpy(vendor + 8, &r[2], 4); |
| 98 | vendor[12] = 0; |
| 99 | cpuid(0x80000000u, 0, r); |
| 100 | ext = r[0]; |
| 101 | brand[0] = 0; |
| 102 | if (ext >= 0x80000004u) { |
| 103 | for (uint32_t i = 0; i < 3; i++) { |
| 104 | cpuid(0x80000002u + i, 0, r); |
| 105 | memcpy(brand + 16 * i, r, 16); |
| 106 | } |
| 107 | brand[48] = 0; |
| 108 | } |
| 109 | const char *b = brand; |
| 110 | while (*b == ' ') |
| 111 | b++; |
| 112 | say("cpu_vendor: %s\n", vendor); |
| 113 | say("cpu_brand: %s\n", *b ? b : "(none)"); |
| 114 | |
| 115 | cpuid(1, 0, r); |
| 116 | uint32_t fam = (r[0] >> 8) & 0xf, model = (r[0] >> 4) & 0xf; |
| 117 | if (fam == 0xf) |
| 118 | fam += (r[0] >> 20) & 0xff; |
| 119 | if (fam == 6 || fam >= 0xf) |
| 120 | model |= ((r[0] >> 16) & 0xf) << 4; |
| 121 | say("cpu_signature: family=0x%x model=0x%x stepping=%u cpuid_max=0x%x ext_max=0x%x\n", fam, model, |
| 122 | r[0] & 0xf, max, ext); |
| 123 | reg[0] = r[2]; |
| 124 | reg[1] = r[3]; |
| 125 | if (max >= 7) { |
| 126 | cpuid(7, 0, r); |
| 127 | reg[2] = r[1]; |
| 128 | reg[3] = r[2]; |
| 129 | } |
| 130 | if (ext >= 0x80000001u) { |
| 131 | cpuid(0x80000001u, 0, r); |
| 132 | reg[4] = r[3]; |
| 133 | } |
| 134 | if (ext >= 0x80000007u) { |
| 135 | cpuid(0x80000007u, 0, r); |
| 136 | reg[5] = r[3]; |
| 137 | } |
| 138 | if (max >= 0xb) { |
| 139 | uint32_t smt = 0, pkg = 0; |
| 140 | for (uint32_t lvl = 0; lvl < 4; lvl++) { |
| 141 | cpuid(0xb, lvl, r); |
| 142 | uint32_t type = (r[2] >> 8) & 0xff; |
| 143 | if (!type) |
| 144 | break; |
| 145 | if (type == 1) |
| 146 | smt = r[1] & 0xffff; |
| 147 | if (type == 2) |
| 148 | pkg = r[1] & 0xffff; |
| 149 | } |
| 150 | say("cpu_topology: threads_per_core=%u threads_per_package=%u\n", smt, pkg); |
| 151 | } |
| 152 | if (ext >= 0x80000008u) { |
| 153 | cpuid(0x80000008u, 0, r); |
| 154 | say("cpu_address_bits: physical=%u linear=%u\n", r[0] & 0xff, (r[0] >> 8) & 0xff); |
| 155 | } |
| 156 | char line[512]; |
| 157 | size_t k = 0; |
| 158 | for (size_t i = 0; i < sizeof feats / sizeof feats[0]; i++) { |
| 159 | if (!(reg[feats[i].reg] >> feats[i].bit & 1)) |
| 160 | continue; |
| 161 | for (const char *p = feats[i].name; *p && k < sizeof line - 2; p++) |
| 162 | line[k++] = *p; |
| 163 | line[k++] = ' '; |
| 164 | } |
| 165 | if (k) |
| 166 | k--; |
| 167 | line[k] = 0; |
| 168 | say("cpu_features: %s\n", line); |
| 169 | |
| 170 | uint64_t cs, cr0, cr4; |
| 171 | uint32_t lo, hi; |
| 172 | __asm__ volatile("mov %%cs, %0" : "=r"(cs)); |
| 173 | __asm__ volatile("mov %%cr0, %0" : "=r"(cr0)); |
| 174 | __asm__ volatile("mov %%cr4, %0" : "=r"(cr4)); |
| 175 | __asm__ volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(0xc0000080u)); /* IA32_EFER */ |
| 176 | say("cpu_mode: cpl=%u paging=%u long_mode=%u cr0=0x%llx cr4=0x%llx efer=0x%llx\n", (unsigned)(cs & 3), |
| 177 | (unsigned)(cr0 >> 31 & 1), (unsigned)(lo >> 10 & 1), (unsigned long long)cr0, (unsigned long long)cr4, |
| 178 | (unsigned long long)((uint64_t)hi << 32 | lo)); |
| 179 | } |
| 180 | |
| 181 | /* ---- SMBIOS (DMTF DSP0134 3.x: 5.2 entry points, 6.1 structures) ---- */ |
| 182 | static const char *sm_str(const uint8_t *s, const uint8_t *end, uint8_t idx, char *out, size_t cap) |
| 183 | { |
| 184 | const uint8_t *p = s + s[1]; |
| 185 | size_t k = 0; |
| 186 | out[0] = 0; |
| 187 | if (!idx) |
| 188 | return "(none)"; |
| 189 | for (uint8_t i = 1; p < end && *p; i++) { |
| 190 | if (i == idx) { |
| 191 | for (; p < end && *p && k < cap - 1; p++) |
| 192 | out[k++] = (*p >= 0x20 && *p < 0x7f) ? (char)*p : '?'; |
| 193 | out[k] = 0; |
| 194 | return out; |
| 195 | } |
| 196 | while (p < end && *p) |
| 197 | p++; |
| 198 | p++; |
| 199 | } |
| 200 | return "(missing)"; |
| 201 | } |
| 202 | |
| 203 | static void smbios(EFI_SYSTEM_TABLE *st) |
| 204 | { |
| 205 | static const EFI_GUID g2 = {0xeb9d2d31, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}; |
| 206 | static const EFI_GUID g3 = {0xf2fd1544, 0x9794, 0x4a2c, {0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94}}; |
| 207 | const uint8_t *e2 = 0, *e3 = 0, *t = 0; |
| 208 | uint64_t len = 0; |
| 209 | for (uint64_t i = 0; i < st->NumberOfTableEntries; i++) { |
| 210 | if (memcmp(&st->ConfigurationTable[i].VendorGuid, &g3, sizeof g3) == 0) |
| 211 | e3 = st->ConfigurationTable[i].VendorTable; |
| 212 | if (memcmp(&st->ConfigurationTable[i].VendorGuid, &g2, sizeof g2) == 0) |
| 213 | e2 = st->ConfigurationTable[i].VendorTable; |
| 214 | } |
| 215 | if (e3 && memcmp(e3, "_SM3_", 5) == 0) { |
| 216 | t = (const uint8_t *)(uintptr_t)rd64(e3 + 0x10); |
| 217 | len = rd32(e3 + 0x0c); |
| 218 | say("smbios: entry=3 version=%u.%u table=0x%llx max_len=%llu\n", e3[7], e3[8], |
| 219 | (unsigned long long)(uintptr_t)t, (unsigned long long)len); |
| 220 | } else if (e2 && memcmp(e2, "_SM_", 4) == 0) { |
| 221 | t = (const uint8_t *)(uintptr_t)rd32(e2 + 0x18); |
| 222 | len = rd16(e2 + 0x16); |
| 223 | say("smbios: entry=2 version=%u.%u table=0x%llx len=%llu structures=%u\n", e2[6], e2[7], |
| 224 | (unsigned long long)(uintptr_t)t, (unsigned long long)len, rd16(e2 + 0x1c)); |
| 225 | } else { |
| 226 | say("smbios: none\n"); |
| 227 | return; |
| 228 | } |
| 229 | if (!t || !len || len > (1u << 20)) { |
| 230 | say("smbios: table pointer or length invalid\n"); |
| 231 | return; |
| 232 | } |
| 233 | const uint8_t *p = t, *end = t + len; |
| 234 | char a[65], b[65], c[65]; |
| 235 | while (p + 4 <= end && p[1] >= 4) { |
| 236 | const uint8_t *q = p + p[1]; |
| 237 | while (q + 1 < end && !(q[0] == 0 && q[1] == 0)) |
| 238 | q++; |
| 239 | uint8_t type = p[0], n = p[1]; |
| 240 | if (type == 0 && n >= 9) |
| 241 | say("smbios_bios: vendor=%s version=%s date=%s\n", sm_str(p, end, p[4], a, sizeof a), |
| 242 | sm_str(p, end, p[5], b, sizeof b), sm_str(p, end, p[8], c, sizeof c)); |
| 243 | if (type == 1 && n >= 7) |
| 244 | say("smbios_system: manufacturer=%s product=%s version=%s\n", sm_str(p, end, p[4], a, sizeof a), |
| 245 | sm_str(p, end, p[5], b, sizeof b), sm_str(p, end, p[6], c, sizeof c)); |
| 246 | if (type == 2 && n >= 7) |
| 247 | say("smbios_board: manufacturer=%s product=%s version=%s\n", sm_str(p, end, p[4], a, sizeof a), |
| 248 | sm_str(p, end, p[5], b, sizeof b), sm_str(p, end, p[6], c, sizeof c)); |
| 249 | if (type == 127) |
| 250 | break; |
| 251 | p = q + 2; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* ---- PCI: configuration mechanism #1, reads only ---- */ |
| 256 | static uint32_t pci_rd(uint32_t bus, uint32_t dev, uint32_t fn, uint32_t off) |
| 257 | { |
| 258 | uint32_t v, a = 0x80000000u | bus << 16 | dev << 11 | fn << 8 | (off & 0xfc); |
| 259 | __asm__ volatile("outl %0, %w1" ::"a"(a), "Nd"((uint16_t)0xcf8)); |
| 260 | __asm__ volatile("inl %w1, %0" : "=a"(v) : "Nd"((uint16_t)0xcfc)); |
| 261 | return v; |
| 262 | } |
| 263 | |
| 264 | static const char *pci_kind(uint32_t cls) |
| 265 | { |
| 266 | switch (cls >> 8) { |
| 267 | case 0x0101: return "storage-ide"; |
| 268 | case 0x0104: return "storage-raid"; |
| 269 | case 0x0106: return "storage-sata"; |
| 270 | case 0x0108: return "storage-nvme"; |
| 271 | case 0x0200: return "net-ethernet"; |
| 272 | case 0x0280: return "net-other"; |
| 273 | case 0x0300: return "display-vga"; |
| 274 | case 0x0380: return "display-other"; |
| 275 | case 0x0403: return "audio"; |
| 276 | case 0x0600: return "host-bridge"; |
| 277 | case 0x0601: return "isa-bridge"; |
| 278 | case 0x0604: return "pci-bridge"; |
| 279 | case 0x0880: return "system-other"; |
| 280 | case 0x0c03: return (cls & 0xff) == 0x30 ? "usb-xhci" : "usb"; |
| 281 | case 0x0c05: return "smbus"; |
| 282 | case 0x0c80: return "serial-bus-other"; |
| 283 | case 0x0d11: return "bluetooth"; |
| 284 | case 0x1180: return "signal-processing"; |
| 285 | } |
| 286 | return "other"; |
| 287 | } |
| 288 | |
| 289 | static void pci_scan(void) |
| 290 | { |
| 291 | unsigned found = 0; |
| 292 | for (uint32_t bus = 0; bus < 256; bus++) |
| 293 | for (uint32_t dev = 0; dev < 32; dev++) { |
| 294 | if ((pci_rd(bus, dev, 0, 0) & 0xffff) == 0xffff) |
| 295 | continue; |
| 296 | uint32_t nfn = (pci_rd(bus, dev, 0, 0x0c) >> 16 & 0x80) ? 8 : 1; |
| 297 | for (uint32_t fn = 0; fn < nfn; fn++) { |
| 298 | uint32_t id = pci_rd(bus, dev, fn, 0); |
| 299 | if ((id & 0xffff) == 0xffff) |
| 300 | continue; |
| 301 | uint32_t cr = pci_rd(bus, dev, fn, 0x08), hdr = pci_rd(bus, dev, fn, 0x0c) >> 16 & 0x7f; |
| 302 | uint32_t cls = cr >> 8, sub = hdr == 0 ? pci_rd(bus, dev, fn, 0x2c) : 0; |
| 303 | found++; |
| 304 | if (hdr == 1) { |
| 305 | uint32_t bn = pci_rd(bus, dev, fn, 0x18); |
| 306 | say("pci: %02x:%02x.%u %04x:%04x rev=%02x class=%06x hdr=1 secondary=%02x subordinate=%02x %s\n", |
| 307 | bus, dev, fn, id & 0xffff, id >> 16, cr & 0xff, cls, bn >> 8 & 0xff, bn >> 16 & 0xff, |
| 308 | pci_kind(cls)); |
| 309 | } else { |
| 310 | say("pci: %02x:%02x.%u %04x:%04x rev=%02x class=%06x hdr=%u sub=%04x:%04x %s\n", bus, dev, fn, |
| 311 | id & 0xffff, id >> 16, cr & 0xff, cls, hdr, sub & 0xffff, sub >> 16, pci_kind(cls)); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | say("pci_functions: %u\n", found); |
| 316 | } |
| 317 | |
| 318 | /* ---- GOP modes and whole-disk Block I/O devices ---- */ |
| 319 | static void gop_list(EFI_BOOT_SERVICES *bs) |
| 320 | { |
| 321 | static const EFI_GUID guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID_INIT; |
| 322 | EFI_HANDLE *h = 0; |
| 323 | uint64_t n = 0; |
| 324 | if (bs->LocateHandleBuffer(ByProtocol, &guid, 0, &n, &h) != EFI_SUCCESS) { |
| 325 | say("gop: none\n"); |
| 326 | return; |
| 327 | } |
| 328 | for (uint64_t i = 0; i < n; i++) { |
| 329 | EFI_GRAPHICS_OUTPUT_PROTOCOL *g = 0; |
| 330 | if (bs->HandleProtocol(h[i], &guid, (void **)&g) != EFI_SUCCESS || !g || !g->Mode || !g->Mode->Info) |
| 331 | continue; |
| 332 | const EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *m = g->Mode; |
| 333 | say("gop %llu: mode=%u/%u %ux%u format=%u stride=%u fb=0x%llx fb_bytes=%llu\n", (unsigned long long)i, |
| 334 | m->Mode, m->MaxMode, m->Info->HorizontalResolution, m->Info->VerticalResolution, m->Info->PixelFormat, |
| 335 | m->Info->PixelsPerScanLine, (unsigned long long)m->FrameBufferBase, |
| 336 | (unsigned long long)m->FrameBufferSize); |
| 337 | } |
| 338 | bs->FreePool(h); |
| 339 | } |
| 340 | |
| 341 | static void block_list(EFI_BOOT_SERVICES *bs) |
| 342 | { |
| 343 | static const EFI_GUID guid = EFI_BLOCK_IO_PROTOCOL_GUID_INIT; |
| 344 | EFI_HANDLE *h = 0; |
| 345 | uint64_t n = 0, disks = 0, parts = 0; |
| 346 | if (bs->LocateHandleBuffer(ByProtocol, &guid, 0, &n, &h) != EFI_SUCCESS) { |
| 347 | say("block: none\n"); |
| 348 | return; |
| 349 | } |
| 350 | for (uint64_t i = 0; i < n; i++) { |
| 351 | EFI_BLOCK_IO_PROTOCOL *b = 0; |
| 352 | if (bs->HandleProtocol(h[i], &guid, (void **)&b) != EFI_SUCCESS || !b || !b->Media) |
| 353 | continue; |
| 354 | const EFI_BLOCK_IO_MEDIA *m = b->Media; |
| 355 | if (m->LogicalPartition) { |
| 356 | parts++; |
| 357 | continue; |
| 358 | } |
| 359 | uint64_t blocks = m->MediaPresent ? m->LastBlock + 1 : 0; |
| 360 | say("block %llu: disk removable=%u present=%u read_only=%u block_bytes=%u blocks=%llu mib=%llu\n", |
| 361 | (unsigned long long)disks++, m->RemovableMedia, m->MediaPresent, m->ReadOnly, m->BlockSize, |
| 362 | (unsigned long long)blocks, (unsigned long long)(blocks * m->BlockSize >> 20)); |
| 363 | } |
| 364 | say("block_devices: disks=%llu partitions=%llu\n", (unsigned long long)disks, (unsigned long long)parts); |
| 365 | bs->FreePool(h); |
| 366 | } |
| 367 | |
| 368 | void diag_x86_devices(EFI_SYSTEM_TABLE *st) |
| 369 | { |
| 370 | smbios(st); |
| 371 | pci_scan(); |
| 372 | gop_list(st->BootServices); |
| 373 | block_list(st->BootServices); |
| 374 | } |