xv6-riscv-rust icon indicating copy to clipboard operation
xv6-riscv-rust copied to clipboard

xv6-riscv-rust

This is a project intending to port xv6-riscv using Rust.
It is now runnable.

Usage

  1. Follow 6.S081 2021/6.S081 2020 to install tools.

    We may need to build qemu from source depending on the machine.

  2. Build fs:

make fs.img
  1. Run:
cargo run

Misc Options/Features

Objdump:

cargo objdump --bin xv6-riscv-rust -- -d > kernel.asm

// recommended, some instructions is unknown when using rust-objdump/llvm-objdump
// in target/riscv64gc-unknown-none-elf/debug
riscv64-unknown-elf-objdump -S xv6-riscv-rust > kernel.asm

trace system call:

cargo run --features "trace_syscall"

Verbose init info:

cargo run --features "verbose_init_info"

Unit Test(deprecated):

cargo run --features "unit_test"

target spec:

rustc -Z unstable-options --print target-spec-json --target riscv64gc-unknown-none-elf

Path

  • [x] porting console and uart to support printf, p.s., smp = 1
  • [x] add register abstraction to support start using mret to return to rust_main
  • [x] cpu abstraction and spinlock, add unit_test feature as temp solution
  • [x] us spin e lock to synchronize con print sole's ln, and refactor PRINT
  • [x] add kernel frame allocator(kalloc), fix writing bug in timerinit
  • [x] use Unique in self-implemented Box to provide ownership, see this for example
  • [x] add Addr and PageTable
  • [x] add kvm for kernel, i.e., kernel paging
  • [x] cpu and proc basic abstraction(hard time playing around lock and borrow checker)
  • [x] add kernel trap handler(panic at fork_ret)
  • [x] add user trap returner and way to user space
  • [x] add user code space(initcode) and ecall handing in user_trap
  • [x] add virtio disk driver, plic, buffer cache, inode
  • [x] refactor Proc into several parts, one need lock to protect, the other is private
  • [x] separate Buf into two parts, one guarded by bcache's lock, the guarded by its own sleeplock
  • [x] update bio and virtio disk
  • [x] replace linked list allocator with buddy system, remove self-implemented Box
  • [x] add log layer in fs
  • [x] add inode layer in fs
  • [x] complete sys_exec and add elf loader
  • [x] add console, refactor uart and print
  • [x] add file layer and sys_open, sys_dup, sys_write
  • [x] add several sys_* func
  • [x] add pipe in fs and also sys_unlink, sys_chdir, sys_pipe
  • [x] port user library
  • [x] add several sys_* func and handle some OOM cases
  • [x] enable all harts

TODO

  • [ ] recycle pgt for uvm(no need to recycle pgt for kvm now)
  • [ ] remove ConstAddr and PhysAddr?
  • [ ] stack size need to be 8192 bytes?
  • [ ] meta data portion of buddy system is too high
  • [ ] may be too much UB
  • [ ] one-time init, like Once
  • [ ] some assertions can switch to debug_assert, compile time assert
  • [ ] remove VirtAddr and PhysAddr
  • [ ] refactor superblock
  • [ ] refactor begin_op and end_op
  • [ ] compare raw pointer's get_mut method with null-unchecked version &mut *
  • [ ] new_uninit
  • [ ] OOM
  • [ ] unexpected external interrupt irq=0

Useful Reference

Why implementing Send trait for Mutex?
Explicitly drop
fixed-size linked list allocator
take ownership from nothing
Unique issue
out of memory
integrate Mutex and MutexGuard
lld linker script
Rust Memory layout
rustc codegen options
Consider deprecation of UB-happy static mut
non-reentrant function
Cpp's Relaxed ordering
Rust build profile