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/x86/x86.h # 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 / x86 / x86.h` 324 lines ยท 17951 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 /* x86.h -- x86_64 machine access for the AIENOS-KERNEL-0 kernel 18 * (native/kernel/x86): port and MSR I/O, CPUID, TSC, control registers, 19 * the trap frame trap.S builds, and the entry points between the files. 20 * Everything is compiled -mabi=ms (UEFI calling convention, like the stub), 21 * so trap.S passes the first argument in rcx. */ 22 #ifndef AIENOS_CK_X86_H 23 #define AIENOS_CK_X86_H 24 #include 25 #include 26 #include "disk.h" 27 #include "../svc/allen_reply.h" 28 29 /* Read-only firmware inventory; never establishes DMA protection. */ 30 void ck_x86_dmar_inventory(uint64_t rsdp); 31 32 static inline void ck_outb(uint16_t p, uint8_t v) { __asm__ volatile("outb %0, %1" ::"a"(v), "Nd"(p)); } 33 static inline void ck_outw(uint16_t p, uint16_t v) { __asm__ volatile("outw %0, %1" ::"a"(v), "Nd"(p)); } 34 static inline uint8_t ck_inb(uint16_t p) 35 { 36 uint8_t v; 37 __asm__ volatile("inb %1, %0" : "=a"(v) : "Nd"(p)); 38 return v; 39 } 40 static inline uint16_t ck_inw(uint16_t p) 41 { 42 uint16_t v; 43 __asm__ volatile("inw %1, %0" : "=a"(v) : "Nd"(p)); 44 return v; 45 } 46 static inline uint64_t ck_rdmsr(uint32_t m) 47 { 48 uint32_t lo, hi; 49 __asm__ volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(m)); 50 return (uint64_t)hi << 32 | lo; 51 } 52 static inline void ck_wrmsr(uint32_t m, uint64_t v) 53 { 54 __asm__ volatile("wrmsr" ::"c"(m), "a"((uint32_t)v), "d"((uint32_t)(v >> 32)) : "memory"); 55 } 56 static inline void ck_cpuid(uint32_t leaf, uint32_t sub, uint32_t r[4]) 57 { 58 __asm__ volatile("cpuid" : "=a"(r[0]), "=b"(r[1]), "=c"(r[2]), "=d"(r[3]) : "a"(leaf), "c"(sub)); 59 } 60 static inline uint64_t ck_rdtsc(void) 61 { 62 uint32_t lo, hi; 63 __asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi)); 64 return (uint64_t)hi << 32 | lo; 65 } 66 #define CK_RD_CR(n) \ 67 static inline uint64_t ck_rd_cr##n(void) \ 68 { \ 69 uint64_t v; \ 70 __asm__ volatile("mov %%cr" #n ", %0" : "=r"(v)); \ 71 return v; \ 72 } 73 CK_RD_CR(0) 74 CK_RD_CR(2) 75 CK_RD_CR(3) 76 CK_RD_CR(4) 77 #undef CK_RD_CR 78 static inline void ck_wr_cr3(uint64_t v) { __asm__ volatile("mov %0, %%cr3" ::"r"(v) : "memory"); } 79 static inline void ck_cli(void) { __asm__ volatile("cli" ::: "memory"); } 80 static inline void ck_sti(void) { __asm__ volatile("sti" ::: "memory"); } 81 static inline void ck_pause(void) { __asm__ volatile("pause"); } 82 83 /* What trap.S pushes, lowest address first: the general registers, the 84 * vector and error code (0 for vectors without one), then the CPU frame. */ 85 struct ck_x86_frame { 86 uint64_t r15, r14, r13, r12, r11, r10, r9, r8, rbp, rdi, rsi, rdx, rcx, rbx, rax; 87 uint64_t vector, error; 88 uint64_t rip, cs, rflags, rsp, ss; 89 }; 90 _Static_assert(sizeof(struct ck_x86_frame) == 22 * 8, "ck_x86_frame size"); 91 92 /* trap.S */ 93 extern char ck_x86_isr_stubs[]; /* 256 stubs, 16 bytes apart */ 94 #define CK_X86_ISR_STRIDE 16u 95 void ck_x86_load_gdt(const void *gdtr, uint16_t code_sel, uint16_t data_sel, uint16_t tss_sel); 96 __attribute__((noreturn)) void ck_x86_call_on_stack(void (*fn)(void *), void *arg, uint64_t sp); 97 void ck_x86_trap(struct ck_x86_frame *f); /* kmain.c, called by every stub */ 98 99 /* console_x86.c: COM1 if a 16550 answers there, the GOP framebuffer once 100 * the kernel turned it on, and a RAM copy of everything written so far that 101 * is replayed onto the screen when it comes on. */ 102 int ck_x86_com1_probe(void); 103 /* Allen's courier on the second serial port (x86/courier_x86.c): NULL when no 16550 answers at COM2. */ 104 struct ck_courier; 105 const struct ck_courier *ck_x86_courier_init(void); 106 int ck_x86_console_fb_on(volatile void *base, uint32_t width, uint32_t height, uint32_t pitch, 107 uint32_t format, uint32_t *scale, uint32_t *cols, uint32_t *rows); 108 109 /* console_x86.c: the RAM copy of everything written so far (the first 110 * 64 KiB); in the lab image, the input the network report filters. The 111 * second call counts the bytes written after that copy filled. */ 112 const char *ck_x86_console_log(size_t *len); 113 size_t ck_x86_console_log_dropped(void); 114 /* console_x86.c: the screen for another drawer (Allen's window). get: its 115 * framebuffer (pitch in pixels, format 0 RGBX / 1 BGRX), or -1 when there is 116 * none; off: console text stops drawing on it and goes on to the RAM copy and 117 * COM1 only. */ 118 int ck_x86_console_fb_get(volatile uint32_t **px, uint32_t *width, uint32_t *height, uint32_t *pitch, 119 uint32_t *format); 120 void ck_x86_console_fb_off(void); 121 /* console_x86.c: the startup picture. hold, before fb_on: the screen is kept 122 * as it is and text stays off it; tick is called after each console write 123 * (never from inside itself) so the picture can move on. show: the text 124 * comes back (screen cleared, everything so far replayed), for anything 125 * that is not Allen's window: a panic, a fault, a machine without a 126 * keyboard. Once Allen's window has the screen (off), show does nothing. */ 127 void ck_x86_console_fb_hold(void (*tick)(void)); 128 void ck_x86_console_fb_show(void); 129 130 /* pci_x86.c: PCI walk over the MCFG ECAM window; every AHCI and NVMe disk 131 * read-only (identify, read, GPT check), a look at the Ethernet chip. */ 132 struct ck_frames; 133 struct ck_x86_storage_result { 134 unsigned pci_functions, ahci_ctrls, ahci_disks, nvme_ctrls, nvme_disks, gpt_valid; 135 unsigned net_found, net_link; /* net_link: 0 not read, 1 up, 2 down, 3 not up yet (driver running) */ 136 unsigned net_driver; /* 0 not tried, 1 running, 2 failed */ 137 }; 138 const struct ck_x86_storage_result *ck_x86_storage(uint64_t rsdp, struct ck_frames *fr, uint64_t tsc_hz, 139 uint64_t map_top); 140 /* The PCI functions that walk found, in order (at most 64): 0, or -1 past the last. 141 * cls is class, subclass and interface (0x030000: a VGA display controller). */ 142 int ck_x86_pci_fn(unsigned i, uint16_t *vendor, uint16_t *device, uint32_t *cls); 143 /* kmain_x86.c: maps [pa, pa + len) uncached when it lies above the identity map. */ 144 int ck_x86_map_mmio(uint64_t pa, uint64_t len); 145 /* ahci_x86.c: one AHCI controller at abar, polled; read-only until a write 146 * range is opened (only x86/memory_x86.c does). */ 147 void ck_x86_ahci_start(unsigned ctrl, uint64_t abar, struct ck_frames *fr, uint64_t tsc_hz); 148 unsigned ck_x86_ahci_disks(void); 149 /* The disk_dev of AHCI disk index (NULL past the last) and its IDENTIFY model. */ 150 const disk_dev *ck_x86_ahci_dev(unsigned index, const char **model); 151 /* Opens one more write range [first, last] (never LBA 0; at most 3): 0 or -1. */ 152 int ck_x86_ahci_allow(unsigned index, uint64_t first, uint64_t last); 153 /* Closes every write range of the disk. */ 154 void ck_x86_ahci_allow_none(unsigned index); 155 #ifdef CK_X86_SWAP_TEST_DISK 156 int ck_x86_ahci_read_lba(unsigned ctrl, unsigned disk, uint64_t lba, unsigned count, uint8_t *buf); 157 #endif 158 159 /* efi_x86.c: what the network loader handed this kernel (boot/netboot_proto.h), 160 * or 0 when it was not started by the loader (the firmware started it from a 161 * disk, or another loader). *object_dropped: the loader named an object that 162 * was not in its own memory, so it was ignored. */ 163 struct nb_pass; 164 const struct nb_pass *ck_x86_netldr_pass(int *object_dropped); 165 166 /* kmain_x86.c: marks the end of a start-up step (TSC now) for boot_time's 167 * breakdown; name is a short static string. */ 168 void ck_x86_step(const char *name); 169 170 /* memory_x86.c: Allen's memory and notes on a hard disk (svc/allen_memory.h). 171 * open: finds the disk holding Allen's section (or adds one on an approved 172 * disk, in unused space), lists its partitions, installs the network loader's 173 * verified kernel on the boot section beside it (fat_boot.h), loads the newest copy into 174 * *mem and the notes it counts into *notes. 1 when saves will reach the disk, 175 * 0 when Allen remembers only until power off. save, notes_write (note index, 176 * written before the save that counts it): 0 or -1 (then nothing more is 177 * written this boot). */ 178 int ck_x86_memory_open(ck_allen_memory *mem, ck_allen_notes *notes, uint64_t tsc_hz); 179 int ck_x86_memory_save(const ck_allen_memory *mem); 180 /* The AIENOS partition, for the setup wizard's records (svc/setup_store.h) at 181 * units CK_SETUP_FIRST and +1, inside the open write range; NULL when Allen 182 * keeps nothing this start or the section has no room for them. */ 183 const disk_dev *ck_x86_memory_part(void); 184 int ck_x86_notes_write(const ck_allen_notes *notes, uint32_t index); 185 186 /* pci_x86.c: offers every xHCI controller the walk found to xhci_x86.c until 187 * one has a USB boot keyboard; prints "keyboard: ready (...)" or why none. 188 * Call after ck_x86_storage. */ 189 void ck_x86_usb(struct ck_frames *fr, uint64_t tsc_hz); 190 /* pci_x86.c: hands every High Definition Audio controller the walk found to 191 * hda_x86.c (Intel PCI settings first); the first with a speaker path plays 192 * the startup chirp from the moment the night (begun at TSC night_t0; 0 for 193 * no night) has reached. Call after ck_x86_storage. hda_x86.c start: stops 194 * any DMA the firmware left running, resets the controller, maps each codec 195 * onto the report, opens every output path; with play set, also runs the 196 * chirp. bar = its registers (mapped), memory decode and bus master on. 1 when 197 * playing, 0 when ready but not playing, -1 when not ready. step: stops the 198 * stream once the chirp is over; cheap, call it often. */ 199 void ck_x86_sound(struct ck_frames *fr, uint64_t tsc_hz, uint64_t night_t0); 200 int ck_x86_hda_start(unsigned ctrl, volatile uint8_t *bar, struct ck_frames *fr, uint64_t tsc_hz, uint64_t night_t0, 201 int play); 202 void ck_x86_hda_step(void); 203 /* pci_x86.c: hands the display chip the walk found (the one whose VRAM holds 204 * the framebuffer at fb_pa, fb_bytes long) to panel_x86.c; with none, says so. 205 * Call after ck_x86_storage. */ 206 void ck_x86_display(uint64_t fb_pa, uint64_t fb_bytes, struct ck_frames *fr, uint64_t tsc_hz); 207 208 /* gpu/gpu_x86.c: AMD Polaris GPU driver entry point (CK_X86_GPU=1). 209 * Called from pci_x86.c when a Polaris display chip is found. 210 * bar0_pa = VRAM aperture PA; bar5_pa = register aperture PA (already mapped 211 * by panel_x86.c). fb_pa, fb_w, fb_h, fb_pitch describe the GOP framebuffer. 212 * Stubs out to a log line when CK_X86_GPU=0. */ 213 void ck_x86_gpu_start(volatile uint8_t *cfg, unsigned bus, unsigned dev, unsigned fn, 214 uint16_t vendor, uint16_t device, 215 uint64_t bar0_pa, uint64_t bar5_pa, 216 uint64_t fb_pa, uint32_t fb_w, uint32_t fb_h, uint32_t fb_pitch, 217 struct ck_frames *fr, uint64_t tsc_hz); 218 /* panel_x86.c: the built-in display panel, switched off for real while Allen 219 * rests and on again when he wakes (built in with CK_X86_PANEL_OFF=1; otherwise, 220 * or on a machine without the AMD chip it knows, every call says so once and 221 * does nothing). start: the display chip's config space (NULL: none found) and 222 * identity, plus the framebuffer; prints what it found and whether the panel 223 * can be switched. ready: 1 when it can. off / on: 0 when every step took, 224 * -1 when the panel was left alone (each step is logged). */ 225 void ck_x86_panel_start(volatile uint8_t *cfg, unsigned bus, unsigned dev, unsigned fn, uint16_t vendor, 226 uint16_t device, uint32_t cls, uint64_t fb_pa, uint64_t fb_bytes, uint64_t tsc_hz); 227 int ck_x86_panel_ready(void); 228 int ck_x86_panel_off(void); 229 int ck_x86_panel_on(void); 230 /* xhci_x86.c: the USB keyboard, polled. start: bar0 = the controller's 231 * registers (mapped), memory decode and bus master on; 0 when a keyboard is 232 * running. hold_poll: handles waiting keys (echo, line editing, a 233 * "keyboard_line: ..." line on Enter) and, while keys arrive, moves *end (a 234 * TSC deadline) out to idle_s after the last key, capped. */ 235 int ck_x86_xhci_start(unsigned ctrl, volatile uint8_t *bar0, struct ck_frames *fr, uint64_t tsc_hz); 236 int ck_x86_kbd_ready(void); 237 void ck_x86_kbd_hold_poll(uint64_t *end); 238 unsigned ck_x86_kbd_idle_s(void); 239 /* Ends the typing session at the end of the hold: "keyboard: done (lines=N)" on 240 * its own line (once; nothing when no keyboard ran). */ 241 void ck_x86_kbd_done(void); 242 /* Where typed text goes besides the console: edit(buf, len) after every key 243 * that changed the line, line(text) after each Enter (after its 244 * "keyboard_line:" line). Either may be NULL. */ 245 void ck_x86_kbd_sink(void (*edit)(const char *buf, unsigned len), void (*line)(const char *text)); 246 void ck_x86_kbd_discard(void); /* drop the line being typed (the key that woke Allen) */ 247 /* Every key, before the line editor (enum ck_key_kind, c for a character): a 248 * nonzero return takes it whole, so it is never echoed or logged (the setup 249 * wizard's key entry). NULL: keys go to the line as before. */ 250 void ck_x86_kbd_keys(int (*key)(int kind, char c)); 251 /* fwcfg_x86.c, launcher image only (CK_X86_HOSTDESC=1): reads the QEMU fw_cfg file "name" 252 * (read-only; only when the DSDT names the device): 253 * 0 whole, 1 cut at cap, -1 none. */ 254 int ck_x86_fwcfg_file(uint64_t rsdp, const char *name, char *buf, unsigned cap, unsigned *len); 255 /* setup_x86.c: Allen's setup wizard in his window. open: reads the saved record 256 * and starts the wizard unless an earlier start finished it (again: start over 257 * whatever is saved); 1 when it is up. key: every key while it is up (1: 258 * taken). line: a courier line, 1 when it was the key test's answer. step: 259 * the clock, in the hold loop. */ 260 struct ck_courier; 261 struct ck_x86_setup_facts { 262 uint64_t rsdp, ram_bytes; 263 unsigned cores; 264 uint32_t screen_w, screen_h; 265 uint32_t starts; /* Allen's starts with this memory, this one counted (1: the first) */ 266 }; 267 int ck_x86_setup_open(const struct ck_x86_setup_facts *f, const struct ck_courier *c, uint64_t tsc_hz, int again); 268 int ck_x86_setup_on(void); 269 int ck_x86_setup_key(int kind, char c); 270 int ck_x86_setup_line(const char *line); 271 void ck_x86_setup_step(void); 272 /* kmain_x86.c: one step of the screen hold: waiting keys (which may move *end 273 * out) and the window's animation. Called in every hold loop. */ 274 void ck_x86_hold_step(uint64_t *end); 275 276 /* tg3_x86.c: the Broadcom BCM57766 Ethernet chip, transmit only, polled. 277 * start: regs = BAR0 (mapped), cfg = its ECAM config space; 0 when the chip 278 * is up (the link may still be negotiating). The rest report on or use it. */ 279 int ck_x86_tg3_start(volatile uint8_t *regs, volatile uint8_t *cfg, struct ck_frames *fr, uint64_t tsc_hz); 280 int ck_x86_tg3_ready(void); 281 int ck_x86_tg3_link(void); 282 void ck_x86_tg3_mac(uint8_t mac[6]); 283 #if defined(CK_X86_REPORT) && CK_X86_REPORT 284 /* Lab image only (make imac-kernel CK_X86_REPORT=1); absent from the default 285 * and any resident or service build. */ 286 int ck_x86_tg3_send(const uint8_t *frame, size_t len); 287 int ck_x86_tg3_drain(unsigned ms); 288 /* The next received frame (lab image only): bytes copied to out, 0 when none waits. */ 289 size_t ck_x86_tg3_recv(uint8_t *out, size_t cap); 290 /* kmain_x86.c (lab image only): Allen's courier over Ethernet (courier_udp.h) 291 * bound to the chip; NULL when the chip is not up. */ 292 const struct ck_courier *ck_x86_courier_udp(uint64_t tsc_hz); 293 /* report_x86.c (lab only): holds `seconds`, sending the allowlisted console 294 * lines as unauthenticated UDP broadcasts every 2 s while the link is up; 295 * returns the rounds sent. */ 296 unsigned ck_x86_report_hold(uint64_t tsc_hz, unsigned seconds); 297 /* The GPU bench's receive (lab only): 0 with *len the UDP payload of a datagram for port 47035 (headers 298 * stripped) and src_mac its sender, 0 with *len 0 when none waits, -1 when the chip is not up. Frames for 299 * the other reader (ck_x86_tg3_recv) are kept for it, and the other way round. */ 300 int ck_x86_tg3_recv_bench(uint8_t *pkt, size_t cap, size_t *len, uint8_t src_mac[6]); 301 /* UDP broadcast send helper. */ 302 void ck_x86_udp_send(const uint8_t dst_mac[6], const uint8_t *pkt, size_t len, 303 uint16_t src_port, uint16_t dst_port); 304 /* Live swap entry: the trampoline (swap_tramp.S) calls the new image's PE entry, _start, with this 305 * value where UEFI puts the ImageHandle (RCX) and the handoff where it puts the SystemTable (RDX). 306 * It is not a canonical address, so no firmware handle can equal it; efi_main then skips the 307 * firmware and enters the kernel (efi_x86.c). */ 308 #define CK_X86_SWAP_ENTRY_MAGIC 0x5357415041454E49ull /* "INEAPAWS" */ 309 /* Set by efi_main when this kernel was entered by a live swap, not by the firmware. */ 310 extern int ck_x86_swapped_in; 311 /* Swap helpers (swap_x86.c, exposed to kmain_x86.c). */ 312 void ck_x86_swap_stop_aps(void); 313 uint64_t ck_x86_tsc_hz(void); 314 void ck_x86_xhci_stop_all(void); 315 void ck_x86_tg3_stop(void); 316 void ck_x86_hda_stop(void); 317 #endif 318 319 /* smp_x86.S: the secondary-core trampoline, copied below 1 MiB and patched. */ 320 extern char ck_x86_tramp_start[], ck_x86_tramp_end[], ck_x86_tramp_gdt[], ck_x86_tramp_pm32[], 321 ck_x86_tramp_lm64[], ck_x86_tramp_gdtr[], ck_x86_tramp_far32[], ck_x86_tramp_far64[], ck_x86_tramp_cr3[], 322 ck_x86_tramp_stack[], ck_x86_tramp_entry[], ck_x86_tramp_arg[]; 323 324 #endif