diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 47ab4e9f4..414df828b 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -15,7 +15,7 @@ members = [ ] [features] -default = ["kvm", "fatfs", "fatfs-secure", "static_keys_test"] +default = ["backtrace", "kvm", "fatfs", "fatfs-secure", "static_keys_test"] # 内核栈回溯 backtrace = ["dep:unwinding"] # kvm diff --git a/kernel/Makefile b/kernel/Makefile index 4429fb1d9..f61030b21 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -33,13 +33,7 @@ endif .PHONY: check check: ECHO -# @echo "Checking kernel... ARCH=$(ARCH)" -# @exit 1 -ifeq ($(ARCH), x86_64) - RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2024-11-05 check --workspace $(CARGO_ZBUILD) --message-format=json --target ./src/$(TARGET_JSON) -else ifeq ($(ARCH), riscv64) - RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2024-11-05 check --workspace $(CARGO_ZBUILD) --message-format=json --target ./src/$(TARGET_JSON) -endif + $(MAKE) -C src check ARCH=$(ARCH) test: # 测试内核库 diff --git a/kernel/crates/unified-init/Cargo.toml b/kernel/crates/unified-init/Cargo.toml index d52fce584..607896f77 100644 --- a/kernel/crates/unified-init/Cargo.toml +++ b/kernel/crates/unified-init/Cargo.toml @@ -11,4 +11,4 @@ path = "src/main.rs" [dependencies] unified-init-macros = { path = "macros" } linkme = "=0.3.27" -system_error = { path = "../system_error" } \ No newline at end of file +system_error = { path = "../system_error" } diff --git a/kernel/src/Makefile b/kernel/src/Makefile index acd1a095e..b5ba69105 100644 --- a/kernel/src/Makefile +++ b/kernel/src/Makefile @@ -6,7 +6,7 @@ GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS))) DIR_LIB=libs lib_patterns := *.a LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns))) -EXT_FEATURES = + # unwind/backtrace related UNWIND_ENABLE ?= yes @@ -16,10 +16,7 @@ RUSTFLAGS_UNWIND = ifeq ($(UNWIND_ENABLE), yes) CFLAGS_UNWIND = -funwind-tables LDFLAGS_UNWIND = --eh-frame-hdr - RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld - EXT_FEATURES += backtrace -else - RUSTFLAGS_UNWIND = -Cpanic=abort + RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld -Cpanic=abort endif RUSTFLAGS += $(RUSTFLAGS_UNWIND) @@ -41,10 +38,7 @@ kernel_subdirs := debug kernel_rust: - RUSTFLAGS="$(RUSTFLAGS)" \ - cargo +nightly-2024-11-05 $(CARGO_ZBUILD) build --release \ - --target $(TARGET_JSON) \ - --features "$(EXT_FEATURES)" + RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2024-11-05 $(CARGO_ZBUILD) build --release --target $(TARGET_JSON) all: kernel @@ -133,3 +127,7 @@ clean: cd $$subdir && $(MAKE) clean;\ cd .. ;\ done + +.PHONY: check +check: + RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2024-11-05 $(CARGO_ZBUILD) check --workspace --message-format=json --target $(TARGET_JSON) \ No newline at end of file diff --git a/kernel/src/arch/riscv64/riscv64gc-unknown-none-elf.json b/kernel/src/arch/riscv64/riscv64gc-unknown-none-elf.json index b474a6fb3..f89675297 100644 --- a/kernel/src/arch/riscv64/riscv64gc-unknown-none-elf.json +++ b/kernel/src/arch/riscv64/riscv64gc-unknown-none-elf.json @@ -18,7 +18,7 @@ "std": false, "tier": 2 }, - "panic-strategy": "unwind", + "panic-strategy": "abort", "relocation-model": "static", "supported-sanitizers": [ "shadow-call-stack", diff --git a/kernel/src/arch/riscv64/syscall/mod.rs b/kernel/src/arch/riscv64/syscall/mod.rs index 450c0e85d..6f80d2446 100644 --- a/kernel/src/arch/riscv64/syscall/mod.rs +++ b/kernel/src/arch/riscv64/syscall/mod.rs @@ -35,10 +35,17 @@ pub(super) fn syscall_handler(syscall_num: usize, frame: &mut TrapFrame) -> () { } let args = [frame.a0, frame.a1, frame.a2, frame.a3, frame.a4, frame.a5]; - syscall_return!( - Syscall::catch_handle(syscall_num, &args, frame) - .unwrap_or_else(|e| e.to_posix_errno() as usize), - frame, - false - ); + let mut syscall_handle = || -> usize { + #[cfg(feature = "backtrace")] + { + Syscall::catch_handle(syscall_num, &args, frame) + .unwrap_or_else(|e| e.to_posix_errno() as usize) + } + #[cfg(not(feature = "backtrace"))] + { + Syscall::handle(syscall_num, &args, frame) + .unwrap_or_else(|e| e.to_posix_errno() as usize) + } + }; + syscall_return!(syscall_handle(), frame, false); } diff --git a/kernel/src/arch/x86_64/x86_64-unknown-none.json b/kernel/src/arch/x86_64/x86_64-unknown-none.json index c676f3773..fbc13a4a2 100644 --- a/kernel/src/arch/x86_64/x86_64-unknown-none.json +++ b/kernel/src/arch/x86_64/x86_64-unknown-none.json @@ -11,5 +11,5 @@ "executables": true, "features": "-mmx,-sse,+soft-float", "disable-redzone": true, - "panic-strategy": "unwind" + "panic-strategy": "abort" } diff --git a/kernel/src/debug/mod.rs b/kernel/src/debug/mod.rs index 085d70517..dcd4c86ac 100644 --- a/kernel/src/debug/mod.rs +++ b/kernel/src/debug/mod.rs @@ -1,3 +1,4 @@ pub mod jump_label; pub mod klog; pub mod kprobe; +pub mod panic; diff --git a/kernel/src/debug/panic/hook.rs b/kernel/src/debug/panic/hook.rs new file mode 100644 index 000000000..c339a1961 --- /dev/null +++ b/kernel/src/debug/panic/hook.rs @@ -0,0 +1,27 @@ +use unwinding::abi::{UnwindContext, UnwindReasonCode, _Unwind_GetIP}; +use unwinding::panic::UserUnwindTrace; + +extern "C" { + fn lookup_kallsyms(addr: u64, level: i32) -> i32; +} + +/// User hook for unwinding +/// +/// During stack backtrace, the user can print the function location of the current stack frame. +pub struct Tracer; +pub struct CallbackData { + pub counter: usize, +} +impl UserUnwindTrace for Tracer { + type Arg = CallbackData; + + fn trace(ctx: &UnwindContext<'_>, arg: *mut Self::Arg) -> UnwindReasonCode { + let data = unsafe { &mut *(arg) }; + data.counter += 1; + let pc = _Unwind_GetIP(ctx); + unsafe { + lookup_kallsyms(pc as u64, data.counter as i32); + } + UnwindReasonCode::NO_REASON + } +} diff --git a/kernel/src/debug/panic/mod.rs b/kernel/src/debug/panic/mod.rs new file mode 100644 index 000000000..245058fa8 --- /dev/null +++ b/kernel/src/debug/panic/mod.rs @@ -0,0 +1,46 @@ +#[cfg(feature = "backtrace")] +mod hook; +use core::panic::PanicInfo; + +/// 全局的panic处理函数 +/// +#[cfg(target_os = "none")] +#[panic_handler] +#[no_mangle] +pub fn panic(info: &PanicInfo) -> ! { + use log::error; + + use crate::process; + + error!("Kernel Panic Occurred."); + + match info.location() { + Some(loc) => { + println!( + "Location:\n\tFile: {}\n\tLine: {}, Column: {}", + loc.file(), + loc.line(), + loc.column() + ); + } + None => { + println!("No location info"); + } + } + println!("Message:\n\t{}", info.message()); + #[cfg(feature = "backtrace")] + { + let mut data = hook::CallbackData { counter: 0 }; + println!("Rust Panic Backtrace:"); + let _res = unwinding::panic::begin_panic_with_hook::( + alloc::boxed::Box::new(()), + &mut data, + ); + // log::error!("panic unreachable: {:?}", res.0); + } + println!( + "Current PCB:\n\t{:?}", + process::ProcessManager::current_pcb() + ); + process::ProcessManager::exit(usize::MAX); +} diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 637f3d994..c6aa5b499 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -37,8 +37,6 @@ #[macro_use] extern crate std; -use core::panic::PanicInfo; - /// 导出x86_64架构相关的代码,命名为arch模块 #[macro_use] mod arch; @@ -94,103 +92,6 @@ extern crate wait_queue_macros; use crate::mm::allocator::kernel_allocator::KernelAllocator; -extern "C" { - fn lookup_kallsyms(addr: u64, level: i32) -> i32; -} - // 声明全局的分配器 #[cfg_attr(not(test), global_allocator)] pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator; - -/// 全局的panic处理函数 -/// -/// How to use unwinding lib: -/// -/// ``` -/// pub fn test_unwind() { -/// struct UnwindTest; -/// impl Drop for UnwindTest { -/// fn drop(&mut self) { -/// println!("Drop UnwindTest"); -/// } -/// } -/// let res1 = unwinding::panic::catch_unwind(|| { -/// let _unwind_test = UnwindTest; -/// println!("Test panic..."); -/// panic!("Test panic"); -/// }); -/// assert_eq!(res1.is_err(), true); -/// let res2 = unwinding::panic::catch_unwind(|| { -/// let _unwind_test = UnwindTest; -/// println!("Test no panic..."); -/// 0 -/// }); -/// assert_eq!(res2.is_ok(), true); -/// } -/// ``` -/// -#[cfg(target_os = "none")] -#[panic_handler] -#[no_mangle] -pub fn panic(info: &PanicInfo) -> ! { - use log::error; - - error!("Kernel Panic Occurred."); - - match info.location() { - Some(loc) => { - println!( - "Location:\n\tFile: {}\n\tLine: {}, Column: {}", - loc.file(), - loc.line(), - loc.column() - ); - } - None => { - println!("No location info"); - } - } - println!("Message:\n\t{}", info.message()); - #[cfg(feature = "backtrace")] - { - let mut data = hook::CallbackData { counter: 0 }; - println!("Rust Panic Backtrace:"); - let res = unwinding::panic::begin_panic_with_hook::( - alloc::boxed::Box::new(()), - &mut data, - ); - log::error!("panic unreachable: {:?}", res.0); - } - println!( - "Current PCB:\n\t{:?}", - process::ProcessManager::current_pcb() - ); - process::ProcessManager::exit(usize::MAX); -} -#[cfg(feature = "backtrace")] -mod hook { - use crate::lookup_kallsyms; - use unwinding::abi::{UnwindContext, UnwindReasonCode, _Unwind_GetIP}; - use unwinding::panic::UserUnwindTrace; - - /// User hook for unwinding - /// - /// During stack backtrace, the user can print the function location of the current stack frame. - pub struct Tracer; - pub struct CallbackData { - pub counter: usize, - } - impl UserUnwindTrace for Tracer { - type Arg = CallbackData; - - fn trace(ctx: &UnwindContext<'_>, arg: *mut Self::Arg) -> UnwindReasonCode { - let data = unsafe { &mut *(arg) }; - data.counter += 1; - let pc = _Unwind_GetIP(ctx); - unsafe { - lookup_kallsyms(pc as u64, data.counter as i32); - } - UnwindReasonCode::NO_REASON - } - } -}