//! Each core's segments: the kernel's and the programs' (long mode ignores //! their bases and limits; they carry the privilege level), and a task //! state segment, whose only use here is `rsp0`: the stack an interrupt //! from a program switches to, which is the running task's own kernel //! stack (`set_kernel_stack`, at every switch). use crate::smp::MAX_CORES; pub const KERNEL_CODE: u16 = 0x08; pub const KERNEL_DATA: u16 = 0x10; /// Ring 3: the selector's low bits ask for it. pub const USER_DATA: u16 = 0x18 | 3; pub const USER_CODE: u16 = 0x20 | 3; const TSS: u16 = 0x28; #[repr(C, packed)] struct TaskState { reserved0: u32, rsp0: u64, rsp1: u64, rsp2: u64, reserved1: u64, ist: [u64; 7], reserved2: u64, reserved3: u16, /// Past the segment's end: no I/O permission bitmap. io_map: u16, } #[repr(C, align(16))] struct Table([u64; 7]); static mut TABLES: [Table; MAX_CORES] = [const { Table([ 0, 0x00af_9a00_0000_ffff, // kernel code, 64-bit 0x00cf_9200_0000_ffff, // kernel data 0x00cf_f200_0000_ffff, // user data, ring 3 0x00af_fa00_0000_ffff, // user code, 64-bit, ring 3 0, // the task state segment, filled per core 0, ]) }; MAX_CORES]; static mut STATES: [TaskState; MAX_CORES] = [const { TaskState { reserved0: 0, rsp0: 0, rsp1: 0, rsp2: 0, reserved1: 0, ist: [0; 7], reserved2: 0, reserved3: 0, io_map: size_of::() as u16, } }; MAX_CORES]; #[repr(C, packed)] struct Pointer { limit: u16, base: u64, } /// Loads core `core`'s table and task state, and reloads the segment /// registers from it (GS's base, the core's index, is left alone). pub fn load(core: usize) { // SAFETY: only the address is taken. let state = unsafe { &raw const STATES[core] } as u64; let limit = size_of::() as u64 - 1; // An available 64-bit TSS (type 9), present, in two entries. let low = (limit & 0xffff) | (state & 0xff_ffff) << 16 | 0x89 << 40 | ((limit >> 16) & 0xf) << 48 | ((state >> 24) & 0xff) << 56; // SAFETY: only the address is taken. let table = unsafe { &raw mut TABLES[core] }; // SAFETY: this core's own entries, written before it loads them. unsafe { (*table).0[5] = low; (*table).0[6] = state >> 32; } let pointer = Pointer { limit: (size_of::() - 1) as u16, base: table as u64, }; // SAFETY: a complete table; CS is reloaded by a far return to the next // instruction, the data segments by moves, then the task register. unsafe { core::arch::asm!( "lgdt [{pointer}]", "push {code}", "lea {t}, [rip + 2f]", "push {t}", "retfq", "2:", "mov {t:e}, {data}", "mov ds, {t:e}", "mov es, {t:e}", "mov ss, {t:e}", "mov {t:e}, {tss}", "ltr {t:x}", pointer = in(reg) &pointer, code = const KERNEL_CODE as u64, data = const KERNEL_DATA as u32, tss = const TSS as u32, t = out(reg) _, ); } } /// The stack an interrupt from a program lands on, on this core: the top /// of the task's own kernel stack, where its frame then lies. pub fn set_kernel_stack(top: u64) { let core = crate::arch::core_id(); // SAFETY: this core's own state, read by the CPU only on an interrupt // from ring 3, which cannot come while the kernel runs here. unsafe { let state = &raw mut STATES[core]; (*state).rsp0 = top; } }