obkrnl/trap/
x86_64.rs

1use crate::context::current_thread;
2use config::BootEnv;
3use core::sync::atomic::Ordering;
4use krt::boot_env;
5
6/// Main entry point for interrupt.
7///
8/// This will be called by an inline assembly.
9///
10/// See `trap` function on the PS4 for a reference.
11pub extern "C" fn interrupt_handler(frame: &mut TrapFrame) {
12    let td = current_thread();
13
14    unsafe { td.active_interrupts().fetch_add(1, Ordering::Relaxed) };
15
16    match frame.num {
17        TrapNo::Breakpoint => match boot_env() {
18            BootEnv::Vm(vm) => super::vm::interrupt_handler(vm, frame),
19        },
20    }
21
22    unsafe { td.active_interrupts().fetch_sub(1, Ordering::Relaxed) };
23}
24
25/// Main entry point for `syscall` instruction.
26///
27/// This will be called by an inline assembly.
28///
29/// See `amd64_syscall` function on the PS4 for a reference.
30pub extern "C" fn syscall_handler() {
31    // TODO: Implement pc_cnt.v_syscall increment.
32    let td = current_thread();
33    let p = td.proc();
34
35    td.set_profiling_ticks(0);
36
37    // We merge sv_fetch_syscall_args and the code to invoke each syscall handler together.
38    p.abi().syscall_handler();
39
40    todo!()
41}
42
43/// Predefined interrupt vector number.
44#[allow(dead_code)] // Used by inline assembly.
45#[repr(u32)]
46#[derive(Clone, Copy, PartialEq, Eq)]
47pub enum TrapNo {
48    Breakpoint = 3, // T_BPTFLT
49}
50
51/// Contains states of the interupted program.
52#[repr(C)]
53pub struct TrapFrame {
54    pub rdi: usize,  // tf_rdi
55    pub rsi: usize,  // tf_rsi
56    pub rdx: usize,  // tf_rdx
57    pub rcx: usize,  // tf_rcx
58    pub r8: usize,   // tf_r8
59    pub r9: usize,   // tf_r9
60    pub rax: usize,  // tf_rax
61    pub rbx: usize,  // tf_rbx
62    pub rbp: usize,  // tf_rbp
63    pub r10: usize,  // tf_r10
64    pub r11: usize,  // tf_r11
65    pub r12: usize,  // tf_r12
66    pub r13: usize,  // tf_r13
67    pub r14: usize,  // tf_r14
68    pub r15: usize,  // tf_r15
69    pub num: TrapNo, // tf_trapno
70    pub fs: u16,     // tf_fs
71    pub gs: u16,     // tf_gs
72}