21个版本
0.10.0 | 2024年7月12日 |
---|---|
0.9.0 | 2022年2月27日 |
0.8.0 | 2021年7月26日 |
0.7.0 | 2021年2月13日 |
0.1.4 | 2020年3月24日 |
#55 在 嵌入式开发
5,821 每月下载量
用于 ostd
66KB
2K SLoC
TrapFrame-rs
在多个指令集架构上处理内核和用户空间之间的陷阱帧。
支持的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),
}
}
更多示例
内部
x86_64上的控制流
依赖项
~1MB
~19K SLoC