#中断 #裸机 #用户空间 #x86_64 #risc-v #无std

nightly 无std trapframe

在多个指令集架构上处理内核和用户空间之间的陷阱帧

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嵌入式开发

Download history 313/week @ 2024-05-03 554/week @ 2024-05-10 429/week @ 2024-05-17 538/week @ 2024-05-24 621/week @ 2024-05-31 689/week @ 2024-06-07 768/week @ 2024-06-14 407/week @ 2024-06-21 496/week @ 2024-06-28 416/week @ 2024-07-05 883/week @ 2024-07-12 1664/week @ 2024-07-19 1564/week @ 2024-07-26 1237/week @ 2024-08-02 1300/week @ 2024-08-09 1454/week @ 2024-08-16

5,821 每月下载量
用于 ostd

MIT 许可证

66KB
2K SLoC

Rust 1K SLoC // 0.1% comments GNU Style Assembly 762 SLoC // 0.0% comments

TrapFrame-rs

Crate Docs Actions Status

在多个指令集架构上处理内核和用户空间之间的陷阱帧。

支持的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上的控制流

x86_64

依赖项

~1MB
~19K SLoC