Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

CPU Primitives

This document describes the x86-64 CPU primitives used by the Kyronix kernel. It is the child of x86-64 Architecture.

Source

kernel/arch/x86_64/cpu.h

Compiler Attributes

AttributeDefinitionPurpose
NORETURN__attribute__((noreturn))Function never returns
PACKED__attribute__((packed))No struct padding
ALIGNED(n)__attribute__((aligned(n)))Alignment requirement
INLINEstatic inline __attribute__((always_inline))Force inlining
UNUSED__attribute__((unused))Suppress unused warnings

I/O Port Functions

FunctionDescription
outb(port, val)Write byte to I/O port
outw(port, val)Write 16-bit word to I/O port
outl(port, val)Write 32-bit dword to I/O port
inb(port)Read byte from I/O port
inw(port)Read 16-bit word from I/O port
inl(port)Read 32-bit dword from I/O port
io_wait()Write 0 to port 0x80 (delay)

Interrupt Control

FunctionDescription
cli()Disable interrupts
sti()Enable interrupts
hlt()Halt CPU until next interrupt
cpu_relax()pause instruction (spin-loop hint)
cpu_halt()Disable interrupts, halt forever (NORETURN)

MSR Access

FunctionDescription
rdmsr(msr)Read 64-bit MSR
wrmsr(msr, val)Write 64-bit MSR

Control Register Access

FunctionDescription
read_cr0() / write_cr0(val)CR0 (cache control, write protect)
read_cr2()CR2 (page fault linear address)
read_cr3() / write_cr3(val)CR3 (page table base)
read_cr4() / write_cr4(val)CR4 (SMEP, PGE, etc.)

IRQ Save/Restore

FunctionDescription
irq_save()Save RFLAGS, disable interrupts, return saved flags
irq_restore(flags)Restore RFLAGS to saved value

Data Structures

cpu_state_t (Interrupt/Syscall Frame)

184-byte packed structure pushed by ISR stubs:

OffsetFieldDescription
0x00-0x38r15-r8General purpose registers
0x40-0x78rbp, rdi, rsi, rdx, rcx, rbx, raxMore GPRs
0x80int_noInterrupt vector number
0x88error_codeCPU error code (or 0)
0x90ripReturn instruction pointer
0x98csCode segment
0xA0rflagsFlags register
0xA8rspStack pointer
0xB0ssStack segment

gdt_entry_t (8 bytes, packed)

FieldSizeDescription
limit_lowu16Segment limit bits 0-15
base_lowu16Base address bits 0-15
base_midu8Base address bits 16-23
accessu8Access byte
granularityu8Granularity + flags + limit bits 16-19
base_highu8Base address bits 24-31

idt_entry_t (16 bytes, packed)

FieldSizeDescription
offset_lowu16Handler offset bits 0-15
selectoru16Code segment selector
istu8Interrupt Stack Table index
type_attru8Gate type + DPL + present bit
offset_midu16Handler offset bits 16-31
offset_highu32Handler offset bits 32-63
zerou32Reserved (must be zero)

Last reviewed: 2026-07-22