Crates.io | trapframe |
lib.rs | trapframe |
version | 0.10.0 |
source | src |
created_at | 2020-02-05 14:42:30.20707 |
updated_at | 2024-07-12 06:38:31.834116 |
description | Handle Trap Frame across kernel and user space on multiple ISAs. |
homepage | https://github.com/rcore-os/trapframe-rs |
repository | https://github.com/rcore-os/trapframe-rs |
max_upload_size | |
id | 205168 |
size | 73,338 |
Handle Trap Frame across kernel and user space on multiple ISAs.
Supported ISA: x86_64, aarch64, riscv32, riscv64, mipsel
use trapframe::{UserContext, GeneralRegs};
fn kernel_thread() {
// initialize trap handling
unsafe {
trapframe::init();
}
// construct a user space context, set registers
let mut context = UserContext {
general: GeneralRegs {
rip: 0x1000,
rsp: 0x10000,
..Default::default()
},
..Default::default()
};
// go to user space with the context
context.run();
// come back from user space, maybe syscall or trap
println!("back from user: {:#x?}", context);
// check the context and handle the trap
match context.trap_num {
0x3 => println!("breakpoint"),
0xd => println!("general protection fault"),
0x100 => println!("syscall: id={}", context.general.rax),
...
}
}
use trapframe::TrapFrame;
#[no_mangle] // export a function 'trap_handler'
extern "sysv64" fn trap_handler(tf: &mut TrapFrame) {
match tf.trap_num {
0x3 => {
println!("TRAP: Breakpoint");
tf.rip += 1;
}
_ => panic!("TRAP: {:#x?}", tf),
}
}
Control flow on x86_64: