From 090796d3f0e91b8442dc8a52f85da64f46b32531 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Tue, 8 Jan 2019 12:05:29 +0800 Subject: [PATCH] Implement backtrace support for RISCV32 --- kernel/src/backtrace.rs | 16 ++++++++-------- kernel/targets/riscv32.json | 3 ++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/kernel/src/backtrace.rs b/kernel/src/backtrace.rs index 798327d..1c6f865 100644 --- a/kernel/src/backtrace.rs +++ b/kernel/src/backtrace.rs @@ -5,14 +5,14 @@ extern "C" { /// Returns the current frame pointer. #[inline(always)] -#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] +#[cfg(any(target_arch = "aarch64", target_arch = "riscv32", target_arch = "riscv64"))] pub fn fp() -> usize { let ptr: usize; #[cfg(target_arch = "aarch64")] unsafe { asm!("mov $0, x29" : "=r"(ptr)); } - #[cfg(target_arch = "riscv64")] + #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] unsafe { asm!("mv $0, s0" : "=r"(ptr)); } @@ -22,14 +22,14 @@ pub fn fp() -> usize { /// Returns the current link register. #[inline(always)] -#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] +#[cfg(any(target_arch = "aarch64", target_arch = "riscv32", target_arch = "riscv64"))] pub fn lr() -> usize { let ptr: usize; #[cfg(target_arch = "aarch64")] unsafe { asm!("mov $0, x30" : "=r"(ptr)); } - #[cfg(target_arch = "riscv64")] + #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] unsafe { asm!("mv $0, ra" : "=r"(ptr)); } @@ -39,17 +39,17 @@ pub fn lr() -> usize { // Print the backtrace starting from the caller pub fn backtrace() { - #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] + #[cfg(any(target_arch = "aarch64", target_arch = "riscv32", target_arch = "riscv64"))] unsafe { let mut current_pc = lr(); let mut current_fp = fp(); let mut stack_num = 0; while current_pc >= stext as usize && current_pc <= etext as usize && current_fp as usize != 0 { - println!("#{} {:#018X} fp {:#18X}", stack_num, current_pc - 4, current_fp); + println!("#{} {:#018X} fp {:#018X}", stack_num, current_pc - 4, current_fp); stack_num = stack_num + 1; - #[cfg(target_arch = "riscv64")] + #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] { - current_fp = *((current_fp - 16) as *const usize); + current_fp = *(current_fp as *const usize).offset(-2); current_pc = *(current_fp as *const usize).offset(-1); } #[cfg(target_arch = "aarch64")] diff --git a/kernel/targets/riscv32.json b/kernel/targets/riscv32.json index ca35687..9430baf 100644 --- a/kernel/targets/riscv32.json +++ b/kernel/targets/riscv32.json @@ -31,5 +31,6 @@ "ptx-kernel", "msp430-interrupt", "x86-interrupt" - ] + ], + "eliminate-frame-pointer": false }