Skip to content

Commit

Permalink
rustsbi: 打印加载阶段的陷入错误
Browse files Browse the repository at this point in the history
  • Loading branch information
luojia65 committed Nov 30, 2021
1 parent be680e4 commit d5211f8
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[alias]
xtask = "run --package xtask --"
make = "xtask make"
asm = "xtask asm"
gdb = "xtask gdb"
image = "xtask image"
110 changes: 110 additions & 0 deletions rustsbi-hifive-unmatched/src/early_trap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use riscv::register::{
mstatus::Mstatus,
mtvec::{self, TrapMode},
mscratch,
mcause,
mtval
};

#[inline]
pub fn init(hartid: usize) {
let stack_base = unsafe { &super::SBI_STACK } as *const _ as usize;
let stack = stack_base + (hartid + 1) * super::PER_HART_STACK_SIZE;
mscratch::write(stack);
let mut addr = early_trap_fail as usize;
if addr & 0x2 != 0 {
addr += 0x2; // 中断入口地址必须对齐到4个字节
}
unsafe { mtvec::write(addr, TrapMode::Direct) };
}

extern "C" fn rust_fail(ctx: &SupervisorContext) -> ! {
crate::println!("rustsbi: early init stage fail, context: {:x?}, mcause: {:?}, mtval: {:x}", ctx, mcause::read(), mtval::read());
loop {}
}

#[derive(Debug)]
#[repr(C)]
pub struct SupervisorContext {
pub ra: usize, // 0
pub sp: usize,
pub gp: usize,
pub tp: usize,
pub t0: usize,
pub t1: usize,
pub t2: usize,
pub s0: usize,
pub s1: usize,
pub a0: usize,
pub a1: usize,
pub a2: usize,
pub a3: usize,
pub a4: usize,
pub a5: usize,
pub a6: usize,
pub a7: usize,
pub s2: usize,
pub s3: usize,
pub s4: usize,
pub s5: usize,
pub s6: usize,
pub s7: usize,
pub s8: usize,
pub s9: usize,
pub s10: usize,
pub s11: usize,
pub t3: usize,
pub t4: usize,
pub t5: usize,
pub t6: usize, // 30
pub mstatus: Mstatus, // 31
pub mepc: usize, // 32
}

#[naked]
#[link_section = ".text"]
pub unsafe extern "C" fn early_trap_fail() -> ! {
asm!( // sp:特权级栈,mscratch:特权级上下文
".p2align 2",
"csrrw sp, mscratch, sp", // 新mscratch:特权级栈, 新sp:特权级上下文
"addi sp, sp, -33*8",
"sd ra, 0*8(sp)
sd gp, 2*8(sp)
sd tp, 3*8(sp)
sd t0, 4*8(sp)
sd t1, 5*8(sp)
sd t2, 6*8(sp)
sd s0, 7*8(sp)
sd s1, 8*8(sp)
sd a0, 9*8(sp)
sd a1, 10*8(sp)
sd a2, 11*8(sp)
sd a3, 12*8(sp)
sd a4, 13*8(sp)
sd a5, 14*8(sp)
sd a6, 15*8(sp)
sd a7, 16*8(sp)
sd s2, 17*8(sp)
sd s3, 18*8(sp)
sd s4, 19*8(sp)
sd s5, 20*8(sp)
sd s6, 21*8(sp)
sd s7, 22*8(sp)
sd s8, 23*8(sp)
sd s9, 24*8(sp)
sd s10, 25*8(sp)
sd s11, 26*8(sp)
sd t3, 27*8(sp)
sd t4, 28*8(sp)
sd t5, 29*8(sp)
sd t6, 30*8(sp)",
"csrr t0, mstatus
sd t0, 31*8(sp)",
"csrr t1, mepc
sd t1, 32*8(sp)",
"mv a0, sp",
"j {fail}",
fail = sym rust_fail,
options(noreturn)
)
}
6 changes: 4 additions & 2 deletions rustsbi-hifive-unmatched/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

mod runtime;
mod peripheral;
mod early_trap;

use core::panic::PanicInfo;

Expand All @@ -27,18 +28,19 @@ fn rust_main(hartid: usize, opaque: usize) -> ! {
if hartid == 0 {
init_bss();
}
runtime::init();
if hartid == 0 {
let uart = unsafe { peripheral::Uart::prev_bootloading_step() };
init_stdout(uart);
early_trap::init(hartid);
println!("rustsbi: hello world (1)!");
print_misa();
init_heap(); // 必须先加载堆内存,才能使用rustsbi框架
init_stdio();
println!("rustsbi: hello world!");
println!("rustsbi: hello world! {:x} {:x}", hartid, opaque);
}
todo!()
runtime::init();
loop {}
}

fn init_bss() {
Expand Down
20 changes: 20 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ fn main() {
(about: "Build project")
(@arg release: --release "Build artifacts in release mode, with optimizations")
)
(@subcommand asm =>
(about: "View asm code for project")
(@arg release: --release "Build artifacts in release mode, with optimizations")
)
(@subcommand image =>
(about: "Build SD card partition image")
(@arg release: --release "Build artifacts in release mode, with optimizations")
Expand All @@ -42,6 +46,12 @@ fn main() {
}
xtask_build_sbi(&xtask_env);
xtask_binary_sbi(&xtask_env);
} else if let Some(matches) = matches.subcommand_matches("asm") {
if matches.is_present("release") {
xtask_env.compile_mode = CompileMode::Release;
}
xtask_build_sbi(&xtask_env);
xtask_asm_sbi(&xtask_env);
} else if let Some(matches) = matches.subcommand_matches("image") {
if matches.is_present("release") {
xtask_env.compile_mode = CompileMode::Release;
Expand Down Expand Up @@ -93,6 +103,16 @@ fn xtask_binary_sbi(xtask_env: &XtaskEnv) {
}
}

fn xtask_asm_sbi(xtask_env: &XtaskEnv) {
// @{{objdump}} -D {{test-kernel-elf}} | less
Command::new("riscv-none-embed-objdump")
.current_dir(dist_dir(xtask_env))
.arg("-d")
.arg("rustsbi-hifive-unmatched")
.status()
.unwrap();
}

fn xtask_unmatched_gdb(xtask_env: &XtaskEnv) {
let mut command = Command::new("riscv-none-embed-gdb");
command.current_dir(dist_dir(xtask_env));
Expand Down

0 comments on commit d5211f8

Please sign in to comment.