Merge branch 'master' into feat-network-rebuild

Signed-off-by: longjin <longjin@DragonOS.org>
This commit is contained in:
longjin
2024-11-27 14:34:01 +00:00
153 changed files with 2578 additions and 1847 deletions

View File

@ -14,11 +14,9 @@ CFLAGS_UNWIND =
LDFLAGS_UNWIND =
RUSTFLAGS_UNWIND =
ifeq ($(UNWIND_ENABLE), yes)
CFLAGS_UNWIND = -funwind-tables
ifeq ($(ARCH), x86_64)
LDFLAGS_UNWIND = --eh-frame-hdr
RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld
endif
CFLAGS_UNWIND = -funwind-tables
LDFLAGS_UNWIND = --eh-frame-hdr
RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld -Cpanic=unwind
endif
RUSTFLAGS += $(RUSTFLAGS_UNWIND)
@ -58,7 +56,6 @@ ECHO:
@echo "$@"
$(kernel_subdirs): ECHO
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" kernel_root_path="$(shell pwd)"
kernel: $(kernel_subdirs) kernel_rust
@ -66,7 +63,26 @@ kernel: $(kernel_subdirs) kernel_rust
__link_riscv64_kernel:
@echo "Linking kernel..."
$(LD) -b elf64-littleriscv -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/riscv64gc-unknown-none-elf/release/libdragonos_kernel.a -T arch/riscv64/link.ld --no-relax
# 生成kallsyms
current_dir=$(pwd)
@dbg='debug';for x in $$dbg; do \
cd $$x;\
$(MAKE) generate_kallsyms kernel_root_path="$(shell pwd)"||exit 1;\
cd ..;\
done
# 重新链接
@echo "Re-Linking kernel..."
@echo $(shell find . -name "*.o")
$(LD) -b elf64-littleriscv -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/riscv64gc-unknown-none-elf/release/libdragonos_kernel.a ./debug/kallsyms.o -T arch/riscv64/link.ld --no-relax
@echo "Generating kernel ELF file..."
ifeq ($(UNWIND_ENABLE), yes)
$(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv kernel ../../bin/kernel/kernel.elf
else
$(OBJCOPY) -I elf64-littleriscv -O elf64-littleriscv -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
endif
@rm kernel
$(MAKE) __dragon_stub PAYLOAD_ELF="$(shell pwd)/../../bin/kernel/kernel.elf"
@ -111,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)

View File

@ -28,6 +28,7 @@ SECTIONS
. = ALIGN(4096);
text_start_pa = .;
__executable_start = .;
.text (text_start_pa): AT(text_start_pa - KERNEL_VMA)
{
_text = .;
@ -39,6 +40,7 @@ SECTIONS
*(.text.*)
_etext = .;
__etext = .;
}
. = ALIGN(32768);
data_start_pa = .;
@ -60,6 +62,7 @@ SECTIONS
_rodata = .;
*(.rodata)
*(.rodata.*)
*(.gcc_except_table .gcc_except_table.*)
_erodata = .;
}

View File

@ -0,0 +1,28 @@
{
"arch": "riscv64",
"code-model": "medium",
"cpu": "generic-rv64",
"crt-objects-fallback": "false",
"data-layout": "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128",
"eh-frame-header": false,
"emit-debug-gdb-scripts": false,
"features": "+m,+a,+f,+d,+c",
"linker": "rust-lld",
"linker-flavor": "gnu-lld",
"llvm-abiname": "lp64d",
"llvm-target": "riscv64",
"max-atomic-width": 64,
"metadata": {
"description": "Bare RISC-V (RV64IMAFDC ISA)",
"host_tools": false,
"std": false,
"tier": 2
},
"panic-strategy": "abort",
"relocation-model": "static",
"supported-sanitizers": [
"shadow-call-stack",
"kernel-address"
],
"target-pointer-width": "64"
}

View File

@ -35,9 +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::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);
}

View File

@ -26,6 +26,7 @@ SECTIONS
. = ALIGN(32768);
. += KERNEL_VMA;
text_start_pa = .;
__executable_start = .;
.text (text_start_pa): AT(text_start_pa - KERNEL_VMA)
{
_text = .;
@ -37,6 +38,7 @@ SECTIONS
*(.text.*)
_etext = .;
__etext = .;
}
. = ALIGN(32768);
data_start_pa = .;
@ -59,6 +61,7 @@ SECTIONS
*(.rodata.*)
*(.note.gnu.*)
*(.fixup)
*(.gcc_except_table .gcc_except_table.*)
_erodata = .;
}

View File

@ -151,7 +151,19 @@ pub extern "sysv64" fn syscall_handler(frame: &mut TrapFrame) {
}
_ => {}
}
normal_syscall_return!(Syscall::handle(syscall_num, &args, frame), frame, show);
let mut syscall_handle = || -> u64 {
#[cfg(feature = "backtrace")]
{
Syscall::catch_handle(syscall_num, &args, frame)
.unwrap_or_else(|e| e.to_posix_errno() as usize) as u64
}
#[cfg(not(feature = "backtrace"))]
{
Syscall::handle(syscall_num, &args, frame)
.unwrap_or_else(|e| e.to_posix_errno() as usize) as u64
}
};
syscall_return!(syscall_handle(), frame, show);
}
/// 系统调用初始化

View File

@ -0,0 +1,28 @@
#[cfg(feature = "static_keys_test")]
mod tests {
use static_keys::{define_static_key_false, static_branch_unlikely};
define_static_key_false!(MY_STATIC_KEY);
#[inline(always)]
fn foo() {
println!("Entering foo function");
if static_branch_unlikely!(MY_STATIC_KEY) {
println!("A branch");
} else {
println!("B branch");
}
}
pub(super) fn static_keys_test() {
foo();
unsafe {
MY_STATIC_KEY.enable();
}
foo();
}
}
pub fn static_keys_init() {
static_keys::global_init();
#[cfg(feature = "static_keys_test")]
tests::static_keys_test();
}

View File

@ -1,2 +1,4 @@
pub mod jump_label;
pub mod klog;
pub mod kprobe;
pub mod panic;

View File

@ -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
}
}

View File

@ -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::<hook::Tracer>(
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);
}

View File

@ -57,6 +57,7 @@ fn do_start_kernel() {
unsafe { mm_init() };
// crate::debug::jump_label::static_keys_init();
if scm_reinit().is_ok() {
if let Err(e) = textui_init() {
warn!("Failed to init textui: {:?}", e);
@ -90,6 +91,7 @@ fn do_start_kernel() {
clocksource_boot_finish();
Futex::init();
crate::bpf::init_bpf_system();
crate::debug::jump_label::static_keys_init();
#[cfg(all(target_arch = "x86_64", feature = "kvm"))]
crate::virt::kvm::kvm_init();
}

View File

@ -17,6 +17,7 @@
#![feature(sync_unsafe_cell)]
#![feature(vec_into_raw_parts)]
#![feature(c_variadic)]
#![feature(asm_goto)]
#![cfg_attr(target_os = "none", no_std)]
#![allow(static_mut_refs, non_local_definitions, internal_features)]
// clippy的配置
@ -36,8 +37,6 @@
#[macro_use]
extern crate std;
use core::panic::PanicInfo;
/// 导出x86_64架构相关的代码命名为arch模块
#[macro_use]
mod arch;
@ -93,56 +92,6 @@ extern crate wait_queue_macros;
use crate::mm::allocator::kernel_allocator::KernelAllocator;
#[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
extern crate mini_backtrace;
extern "C" {
fn lookup_kallsyms(addr: u64, level: i32) -> i32;
}
// 声明全局的分配器
#[cfg_attr(not(test), global_allocator)]
pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
/// 全局的panic处理函数
#[cfg(target_os = "none")]
#[panic_handler]
#[no_mangle]
pub fn panic(info: &PanicInfo) -> ! {
use log::error;
use process::ProcessManager;
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(all(feature = "backtrace", target_arch = "x86_64"))]
{
unsafe {
let bt = mini_backtrace::Backtrace::<16>::capture();
println!("Rust Panic Backtrace:");
let mut level = 0;
for frame in bt.frames {
lookup_kallsyms(frame as u64, level);
level += 1;
}
};
}
println!("Current PCB:\n\t{:?}", (ProcessManager::current_pcb()));
ProcessManager::exit(usize::MAX);
}

View File

@ -74,6 +74,20 @@ impl Syscall {
return r;
}
/// 系统调用分发器,用于分发系统调用。
///
/// 与[handle]不同这个函数会捕获系统调用处理函数的panic返回错误码。
#[cfg(feature = "backtrace")]
pub fn catch_handle(
syscall_num: usize,
args: &[usize],
frame: &mut TrapFrame,
) -> Result<usize, SystemError> {
let res = unwinding::panic::catch_unwind(|| Self::handle(syscall_num, args, frame));
res.unwrap_or_else(|_| loop {
core::hint::spin_loop();
})
}
/// @brief 系统调用分发器,用于分发系统调用。
///
/// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。