#interrupt #bare-metal #riscv #no_std #x86_64

nightly no-std trapframe

Handle Trap Frame across kernel and user space on multiple ISAs

5 releases

✓ Uses Rust 2018 edition

new 0.1.4 Mar 24, 2020
0.1.3 Mar 4, 2020
0.1.2 Feb 27, 2020
0.1.1 Feb 11, 2020
0.1.0 Feb 5, 2020

#68 in No standard library

Download history 43/week @ 2020-02-03 29/week @ 2020-02-10 16/week @ 2020-02-17 78/week @ 2020-02-24 118/week @ 2020-03-02 16/week @ 2020-03-09 16/week @ 2020-03-16

115 downloads per month

MIT license

29KB
739 lines

TrapFrame-rs

Crate Docs Actions Status

Handle Trap Frame across kernel and user space on multiple ISAs.

Supported ISA:

  • ✅ x86_64
  • ✅ RISC-V 32/64
  • 🚧 MIPS32
  • 🚧 AArch64

Example

Go to user space

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),
        ...
    }
}

Handle kernel trap

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),
    }
}

More examples

Internal

Control flow on x86_64:

x86_64

Dependencies

~215KB