使用 Rust 实现的 RISC-V 内核
rust-kernel-riscvrust-kernel-riscv is an open-source project that implements an operating system kernel on RISC-V architecture with Rust programming language. The project draws inspiration from several open-source implementations, such as xv6-riscv and zCore.
The kernel leverages Rust's asynchronous programming model to schedule threads in both the kernel and user space, which makes context switching more efficient and eliminates the need of allocating a separate kernel stack for each user process.
The kernel implements the kernel page-table isolation, which prevents the kernel space and the user space to share the same page table and mitigates potential Meltdown attacks.
riscv64gc-unknown-none-elf target and related components:rustup install nightly
rustup target add riscv64gc-unknown-none-elf
rustup component add llvm-tools-preview
rustup component add rust-src
cargo install cargo-binutilsbrew install qemumake qemuThe kernel executor handles the management and execution of tasks, which can be either user threads or kernel threads. As of the current implementation, the TaskQueue is a wrapper around the VecDeque type, which store and execute tasks in a FIFO order. The run_until_complete function blocks the calling thread and runs all the tasks in the TaskQueue.
lazy_static! {
static ref TASK_QUEUE: Mutex = Mutex::new(TaskQueue::new());
}
pub fn run_until_complete() {
loop {
let task = TASK_QUEUE.lock().pop_front();
if let Some(task) = task {
task.run();
} else {
break;
}
}
}The trampoline is a dedicated page that acts as a bridge for transferring control between supervisor and user modes, which is located at the same address (0xFFFFFFFFFFFFF000) in both kernel and user thread page tables. Identical mapping is required because the program counter must point to a valid location after switching the page table.
The trampoline contains a pair of naked functions, _enter_kernel_space and _enter_user_space:
_enter_user_space stores callee-saved registers on the kernel stack, switches to the page table of the user thread, and restores the context (registers, sstatuc, sepc) of the user thread from a TrapContext. Following these steps, it uses a sret instruction to return to user mode.
_enter_kernel_space stores the context (registers, sstatuc, sepc) of the user thread to a TrapContext, switch to the page table of the kernel, and restores the callee-saved registers from the kernel stack. Following these steps, it uses a ret instruction to jump to the thread_loop, which will handle the exception or interrupt.
Each user thread is represented with the executor::future::thread_loop future. The executor runs a future with its poll method, and the thread_loop invokes _enter_user_space function to enter the user mode. The _enter_user_space returns when an exception or interrupt occurs, and the thread_loop handles them and decide whether to continue, yield, or terminate the thread. The spawn_thread function is used to add a new user thread to the executor.
…thread_loop future and incorporates it into the executor.thread_loop.thread_loop calls _enter_user_space._enter_kernel_space specified in the stvec register is triggered, returning control to the thread_loop.thread_loop manages the trap and determines whether to continue, yield, or terminate the thread. If it chooses to continue, the thread_loop moves on to the next iteration.暂无开放 Issues,或尚未同步最近议题。