git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/exceptions.rs
//! The exception vectors: every exception saves the interrupted context as a
//! [`Frame`] on the current stack, and resumes whichever frame its handler
//! returns. That is also the whole context switch: the scheduler answers with
//! another task's frame.
use crate::println;
use core::mem::size_of;
/// The registers of an interrupted context, as the vectors save them.
#[repr(C)]
pub struct Frame {
pub x: [u64; 31],
pub sp_el0: u64,
pub elr: u64,
pub spsr: u64,
/// TPIDR_EL0, the thread pointer a program keeps its thread-locals by
/// (Rust's std does): each thread's own, so saved with the rest.
pub tpidr: u64,
_pad: u64,
}
/// SPSR for a kernel task: EL1 on its own stack (EL1h), interrupts unmasked.
const SPSR_EL1H: u64 = 0b0101;
/// SPSR for a program: EL0, interrupts unmasked.
const SPSR_EL0: u64 = 0;
/// What the scheduler and the system calls ask of a frame, the same on
/// every architecture (`x86_64/interrupts.rs` has the PC's).
impl Frame {
const fn new(x: [u64; 31], sp_el0: u64, elr: u64, spsr: u64) -> Frame {
Frame {
x,
sp_el0,
elr,
spsr,
tpidr: 0,
_pad: 0,
}
}
/// A kernel task's first frame: `entry` on the task's own stack, which
/// returns into `ret`.
pub fn kernel_task(entry: u64, ret: u64) -> Frame {
let mut x = [0; 31];
x[30] = ret;
Frame::new(x, 0, entry, SPSR_EL1H)
}
/// A program thread's first frame: `entry` with `stack` and
/// `argument` (its first argument register).
pub fn program(entry: u64, stack: u64, argument: u64) -> Frame {
let mut x = [0; 31];
x[0] = argument;
Frame::new(x, stack, entry, SPSR_EL0)
}
/// Told where the scheduler put the frame, at the top of the task's
/// stack. Nothing to do here: an exception return takes no stack.
pub fn placed_at(&mut self, _at: u64) {}
/// A system call's number.
pub fn call(&self) -> u64 {
self.x[8]
}
/// A system call's seven arguments.
pub fn args(&self) -> [u64; 7] {
[
self.x[0], self.x[1], self.x[2], self.x[3], self.x[4], self.x[5], self.x[6],
]
}
/// A system call's three results.
pub fn set_result(&mut self, (a, b, c): (u64, u64, u64)) {
(self.x[0], self.x[1], self.x[2]) = (a, b, c);
}
/// A system call's first result alone.
pub fn set_first_result(&mut self, a: u64) {
self.x[0] = a;
}
/// Rewinds to the system call, to make it again when the task resumes.
pub fn repeat_call(&mut self) {
self.elr -= 4;
}
/// Whether the frame is the kernel's (EL1h), not a program's.
pub fn is_kernel(&self) -> bool {
self.spsr & 0xf == SPSR_EL1H
}
/// Where it resumes, and its saved status (for a list of tasks).
pub fn pc(&self) -> u64 {
self.elr
}
pub fn status(&self) -> u64 {
self.spsr
}
}
const FRAME: usize = size_of::<Frame>();
const _: () = assert!(FRAME.is_multiple_of(16), "the stack stays 16-byte aligned");
/// Which of the sixteen vectors was taken: the source (current EL on SP0, on
/// SPx, a lower EL in AArch64 or AArch32) times four, plus the type.
mod vector {
pub const CURRENT_SPX_SYNC: u64 = 4;
pub const CURRENT_SPX_IRQ: u64 = 5;
pub const LOWER_SYNC: u64 = 8;
pub const LOWER_IRQ: u64 = 9;
}
// Each vector stores x0 and x1, puts its number in x0 and joins the rest of
// the save; `exception_return` restores whatever frame the handler returned.
core::arch::global_asm!(
".section .text.vectors, \"ax\"",
".balign 0x800",
".global exception_vectors",
"exception_vectors:",
".irp n, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15",
".balign 0x80",
"sub sp, sp, #{frame}",
"stp x0, x1, [sp]",
"mov x0, #\\n",
"b exception_save",
".endr",
"exception_save:",
"stp x2, x3, [sp, #16]",
"stp x4, x5, [sp, #32]",
"stp x6, x7, [sp, #48]",
"stp x8, x9, [sp, #64]",
"stp x10, x11, [sp, #80]",
"stp x12, x13, [sp, #96]",
"stp x14, x15, [sp, #112]",
"stp x16, x17, [sp, #128]",
"stp x18, x19, [sp, #144]",
"stp x20, x21, [sp, #160]",
"stp x22, x23, [sp, #176]",
"stp x24, x25, [sp, #192]",
"stp x26, x27, [sp, #208]",
"stp x28, x29, [sp, #224]",
"mrs x1, sp_el0",
"stp x30, x1, [sp, #240]",
"mrs x1, elr_el1",
"mrs x2, spsr_el1",
"stp x1, x2, [sp, #256]",
"mrs x1, tpidr_el0",
"str x1, [sp, #272]",
"mov x1, sp",
"bl {handle}",
"mov sp, x0",
".global exception_return",
"exception_return:",
"ldr x1, [sp, #272]",
"msr tpidr_el0, x1",
"ldp x1, x2, [sp, #256]",
"msr elr_el1, x1",
"msr spsr_el1, x2",
"ldp x30, x1, [sp, #240]",
"msr sp_el0, x1",
"ldp x0, x1, [sp]",
"ldp x2, x3, [sp, #16]",
"ldp x4, x5, [sp, #32]",
"ldp x6, x7, [sp, #48]",
"ldp x8, x9, [sp, #64]",
"ldp x10, x11, [sp, #80]",
"ldp x12, x13, [sp, #96]",
"ldp x14, x15, [sp, #112]",
"ldp x16, x17, [sp, #128]",
"ldp x18, x19, [sp, #144]",
"ldp x20, x21, [sp, #160]",
"ldp x22, x23, [sp, #176]",
"ldp x24, x25, [sp, #192]",
"ldp x26, x27, [sp, #208]",
"ldp x28, x29, [sp, #224]",
"add sp, sp, #{frame}",
"eret",
frame = const FRAME,
handle = sym handle,
);
unsafe extern "C" {
static exception_vectors: u8;
}
/// Points VBAR_EL1 at the vectors.
pub fn init() {
// SAFETY: the vectors are 2 KiB aligned and handle every entry.
unsafe {
core::arch::asm!(
"msr vbar_el1, {}",
"isb",
in(reg) &raw const exception_vectors,
options(nostack),
);
}
}
/// Exception class (ESR_EL1.EC) of an SVC from AArch64.
const EC_SVC64: u64 = 0x15;
/// Exception class of an FP/SIMD access CPACR_EL1 trapped.
const EC_FP_TRAPPED: u64 = 0x07;
/// Exception class of a data abort from a program (EL0).
const EC_DATA_ABORT_LOWER: u64 = 0x24;
extern "C" fn handle(vector: u64, frame: *mut Frame) -> *mut Frame {
let class = crate::arch::esr() >> 26;
match vector {
vector::CURRENT_SPX_IRQ | vector::LOWER_IRQ => crate::irq::handle(frame),
vector::CURRENT_SPX_SYNC | vector::LOWER_SYNC if class == EC_SVC64 => crate::syscall::handle(frame),
// A program's fault ends the program, not the kernel.
// FP/SIMD from a program whose registers are not loaded.
vector::LOWER_SYNC if class == EC_FP_TRAPPED => crate::sched::fp_trap(frame),
// Its main stack growing: the access runs again once mapped.
vector::LOWER_SYNC
if class == EC_DATA_ABORT_LOWER
&& crate::sched::with_process(|p| {
crate::process::grow_stack(&mut p.space, crate::arch::far() as usize)
}) == Some(true) =>
{
frame
}
vector::LOWER_SYNC => {
println!(
"task {} killed: exception class {class:#x} at {:#x}",
crate::sched::current(),
crate::arch::far()
);
crate::sched::exit_process(frame, pinnacle_abi::KILLED + class)
}
_ => {
// SAFETY: the vector stub just wrote this frame.
let elr = unsafe { (*frame).elr };
panic!(
"exception {vector}: ESR {:#x}, ELR {elr:#x}, FAR {:#x}",
crate::arch::esr(),
crate::arch::far(),
)
}
}
}