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 / efi_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 | /* efi_x86.c -- UEFI stub of AIENOS-KERNEL-0 (x86_64): the same job as |
| 18 | * native/boot/efi_main.c does for the AArch64 kernel, on the same CHANDOF3 |
| 19 | * record (handoff.h), so core/handoff_check.c checks it unchanged. |
| 20 | * |
| 21 | * It finds the ACPI 2.0 RSDP and the GOP framebuffer (boot/efi_gop.c), takes |
| 22 | * the final memory map, leaves boot services (retrying when the map key goes |
| 23 | * stale) and jumps to ck_x86_kernel_entry. It never calls a runtime service. |
| 24 | * When the network loader started it, it keeps the loader's struct nb_pass |
| 25 | * (boot/netboot_proto.h, from LoadOptions) and drops its object unless that |
| 26 | * lies wholly in EfiLoaderData of the final memory map. |
| 27 | * x86 meaning of the AArch64-named fields: firmware_el = CPL (0), |
| 28 | * firmware_ttbr0 = CR3, firmware_sctlr = CR0, uefi_entry_ticks = TSC, |
| 29 | * counter_freq_hz = 0 (the kernel measures the TSC itself). */ |
| 30 | #include "../../boot/efi.h" |
| 31 | #include "../../boot/netboot_proto.h" |
| 32 | #include "../core/ck_internal.h" |
| 33 | #include "x86.h" |
| 34 | |
| 35 | #define MAP_BYTES (64u << 10) |
| 36 | #define EXIT_TRIES 8 |
| 37 | |
| 38 | extern char __image_base[], __image_end[]; |
| 39 | __attribute__((noreturn)) void ck_x86_kernel_entry(struct ck_handoff *h); |
| 40 | void ck_boot_gop_find(EFI_SYSTEM_TABLE *st, struct ck_handoff *h); |
| 41 | |
| 42 | static uint64_t map_buf[MAP_BYTES / 8]; |
| 43 | static struct ck_handoff handoff; |
| 44 | int ck_x86_swapped_in; |
| 45 | static struct nb_pass pass; /* from the network loader, when it started this kernel */ |
| 46 | static int pass_state; /* 0 none, 1 taken, 2 taken but its object was not in loader memory (dropped) */ |
| 47 | |
| 48 | const struct nb_pass *ck_x86_netldr_pass(int *object_dropped) |
| 49 | { |
| 50 | if (object_dropped) |
| 51 | *object_dropped = pass_state == 2; |
| 52 | return pass_state ? &pass : 0; |
| 53 | } |
| 54 | |
| 55 | /* The loader's object must lie wholly inside one EfiLoaderData range of the |
| 56 | * final memory map (pages nothing in this kernel allocates from). */ |
| 57 | static int in_loader_data(const struct ck_handoff *h, uint64_t a, uint64_t n) |
| 58 | { |
| 59 | for (uint64_t off = 0; off + h->desc_size <= h->map_size; off += h->desc_size) { |
| 60 | const EFI_MEMORY_DESCRIPTOR *m = (const void *)((const uint8_t *)map_buf + off); |
| 61 | uint64_t bytes = m->NumberOfPages * 4096u; |
| 62 | if (m->Type == 2 && a >= m->PhysicalStart && n <= bytes && a - m->PhysicalStart <= bytes - n) |
| 63 | return 1; |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int guid_eq(const EFI_GUID *a, const EFI_GUID *b) |
| 69 | { |
| 70 | return memcmp(a, b, sizeof *a) == 0; |
| 71 | } |
| 72 | |
| 73 | /* The night's empty sky (ui/night.c, no stars yet: about 2, 2, 6) over the |
| 74 | * whole screen, before ExitBootServices: the firmware's own shutdown takes |
| 75 | * about 1.2 s on the real machine, and the firmware's picture should not be |
| 76 | * what is seen meanwhile. Eight bytes at a time, two pixels per store. */ |
| 77 | static void night_ground(const struct ck_handoff *h) |
| 78 | { |
| 79 | if (h->fb_status != CK_HANDOFF_FB_PRESENT || !h->fb_base || h->fb_pitch < h->fb_width || |
| 80 | (uint64_t)h->fb_pitch * h->fb_height * 4u > h->fb_size) |
| 81 | return; |
| 82 | uint64_t px = h->fb_format == CK_HANDOFF_FB_BGRX ? 0x00020206u : 0x00060202u; |
| 83 | px |= px << 32; |
| 84 | for (uint32_t y = 0; y < h->fb_height; y++) { |
| 85 | volatile uint64_t *row = (volatile uint64_t *)(uintptr_t)(h->fb_base + (uint64_t)y * h->fb_pitch * 4u); |
| 86 | for (uint32_t x = 0; x + 1u < h->fb_width; x += 2u) |
| 87 | row[x / 2u] = px; |
| 88 | if (h->fb_width & 1u) |
| 89 | ((volatile uint32_t *)row)[h->fb_width - 1u] = (uint32_t)px; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) |
| 94 | { |
| 95 | #if defined(CK_X86_SWAP) && CK_X86_SWAP |
| 96 | /* A live swap, not the firmware: the running kernel already left UEFI and hands over its |
| 97 | * CHANDOF3 record (swap_x86.c). The console is this image's own state, so probe it again; |
| 98 | * ck_x86_kernel_entry checks the record. The record sits in the old kernel's trampoline page |
| 99 | * below 1 MiB: copied into this image, that page is free again for the secondary-core |
| 100 | * trampoline (kmain_x86.c mm_build takes the lowest free page). */ |
| 101 | if ((uint64_t)(uintptr_t)image == CK_X86_SWAP_ENTRY_MAGIC) { |
| 102 | memcpy(&handoff, (const void *)st, sizeof handoff); |
| 103 | ck_x86_swapped_in = 1; |
| 104 | ck_x86_com1_probe(); |
| 105 | ck_x86_kernel_entry(&handoff); |
| 106 | } |
| 107 | #endif |
| 108 | struct ck_handoff *h = &handoff; |
| 109 | uint16_t cs; |
| 110 | __asm__ volatile("mov %%cs, %0" : "=r"(cs)); |
| 111 | h->magic = CK_HANDOFF_MAGIC; |
| 112 | h->firmware_el = cs & 3u; |
| 113 | h->uefi_entry_ticks = ck_rdtsc(); |
| 114 | h->firmware_ttbr0 = ck_rd_cr3(); |
| 115 | h->firmware_sctlr = ck_rd_cr0(); |
| 116 | h->image_base = (uint64_t)(uintptr_t)__image_base; |
| 117 | h->image_end = (uint64_t)(uintptr_t)__image_end; |
| 118 | h->commit = ck_commit(); |
| 119 | h->memory_map = (uint64_t)(uintptr_t)map_buf; |
| 120 | |
| 121 | ck_x86_com1_probe(); |
| 122 | if (st->BootServices->SetWatchdogTimer) |
| 123 | st->BootServices->SetWatchdogTimer(0, 0, 0, 0); |
| 124 | static const EFI_GUID li_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID_INIT; |
| 125 | EFI_LOADED_IMAGE_PROTOCOL *li = 0; |
| 126 | if (st->BootServices->HandleProtocol(image, &li_guid, (void **)&li) == EFI_SUCCESS && li && li->LoadOptions && |
| 127 | li->LoadOptionsSize == sizeof pass && !memcmp(li->LoadOptions, NB_PASS_MAGIC, 8)) { |
| 128 | memcpy(&pass, li->LoadOptions, sizeof pass); |
| 129 | pass_state = 1; |
| 130 | } |
| 131 | static const EFI_GUID acpi20 = EFI_ACPI_20_TABLE_GUID_INIT; |
| 132 | for (uint64_t i = 0; i < st->NumberOfTableEntries; i++) |
| 133 | if (guid_eq(&st->ConfigurationTable[i].VendorGuid, &acpi20)) |
| 134 | h->rsdp = (uint64_t)(uintptr_t)st->ConfigurationTable[i].VendorTable; |
| 135 | ck_x86_step("start"); |
| 136 | |
| 137 | ck_puts("AIENOS-KERNEL-0\n"); |
| 138 | ck_printf("report_kind: pre_exit\naienos_commit: %s\n", ck_commit()); |
| 139 | ck_puts("kernel_impl: c (native/kernel/x86, x86_64)\n"); |
| 140 | char v[80]; |
| 141 | size_t n = 0; |
| 142 | for (; st->FirmwareVendor && n < sizeof v - 1 && st->FirmwareVendor[n]; n++) |
| 143 | v[n] = st->FirmwareVendor[n] < 0x80 ? (char)st->FirmwareVendor[n] : '?'; |
| 144 | v[n] = 0; |
| 145 | ck_printf("firmware_vendor: %s\nfirmware_revision: 0x%x\n", v, st->FirmwareRevision); |
| 146 | ck_printf("firmware_cpl: %u\n", h->firmware_el); |
| 147 | ck_boot_gop_find(st, h); |
| 148 | ck_x86_step("gop"); |
| 149 | night_ground(h); |
| 150 | ck_x86_step("paint"); |
| 151 | ck_printf("acpi_rsdp: 0x%llx\n", (unsigned long long)h->rsdp); |
| 152 | ck_printf("image: 0x%llx-0x%llx\n", (unsigned long long)h->image_base, (unsigned long long)h->image_end); |
| 153 | ck_puts("exiting firmware boot services\n"); |
| 154 | |
| 155 | EFI_BOOT_SERVICES *bs = st->BootServices; |
| 156 | EFI_STATUS s = EFI_INVALID_PARAMETER; |
| 157 | for (unsigned tries = 1; tries <= EXIT_TRIES; tries++) { |
| 158 | uint64_t size = sizeof map_buf, key = 0, dsize = 0; |
| 159 | uint32_t dver = 0; |
| 160 | s = bs->GetMemoryMap(&size, (EFI_MEMORY_DESCRIPTOR *)map_buf, &key, &dsize, &dver); |
| 161 | if (s != EFI_SUCCESS) { |
| 162 | ck_printf("exit_boot_services: GetMemoryMap status=0x%llx size=%llu\n", (unsigned long long)s, |
| 163 | (unsigned long long)size); |
| 164 | return s; |
| 165 | } |
| 166 | h->map_size = size; |
| 167 | h->desc_size = dsize; |
| 168 | h->desc_version = dver; |
| 169 | h->exit_attempts = tries; |
| 170 | s = bs->ExitBootServices(image, key); |
| 171 | if (s == EFI_SUCCESS) |
| 172 | break; |
| 173 | } |
| 174 | if (s != EFI_SUCCESS) { |
| 175 | ck_printf("exit_boot_services: failed status=0x%llx after %u tries\n", (unsigned long long)s, |
| 176 | EXIT_TRIES); |
| 177 | return s; |
| 178 | } |
| 179 | ck_x86_step("exit"); |
| 180 | if (pass_state && pass.object && !in_loader_data(h, pass.object, pass.object_len)) { |
| 181 | pass.object = pass.object_len = 0; |
| 182 | pass_state = 2; |
| 183 | } |
| 184 | ck_x86_kernel_entry(h); |
| 185 | } |