diff --git a/.github/workflows/doc-and-test.yml b/.github/workflows/doc-and-test.yml new file mode 100644 index 00000000..e065b69b --- /dev/null +++ b/.github/workflows/doc-and-test.yml @@ -0,0 +1,66 @@ +name: Build Rust Doc And Run tests + +on: [push] + +env: + CARGO_TERM_COLOR: always + +jobs: + build-doc: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: nightly-2022-04-11 + components: rust-src, llvm-tools-preview + target: riscv64gc-unknown-none-elf + - name: Build doc + run: cd os && cargo doc --no-deps --verbose + - name: Deploy to Github Pages + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./os/target/riscv64gc-unknown-none-elf/doc + destination_dir: ${{ github.ref_name }} + + run-tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: nightly-2022-04-11 + components: rust-src, llvm-tools-preview + target: riscv64gc-unknown-none-elf + - uses: actions-rs/install@v0.1 + with: + crate: cargo-binutils + version: latest + use-tool-cache: true + - name: Cache QEMU + uses: actions/cache@v3 + with: + path: qemu-7.0.0 + key: qemu-7.0.0-x86_64-riscv64 + - name: Install QEMU + run: | + sudo apt-get update + sudo apt-get install ninja-build -y + if [ ! -d qemu-7.0.0 ]; then + wget https://download.qemu.org/qemu-7.0.0.tar.xz + tar -xf qemu-7.0.0.tar.xz + cd qemu-7.0.0 + ./configure --target-list=riscv64-softmmu + make -j + else + cd qemu-7.0.0 + fi + sudo make install + qemu-system-riscv64 --version + + - name: Run usertests + run: cd os && make run TEST=1 + timeout-minutes: 10 diff --git a/.gitignore b/.gitignore index 676c060d..88033e23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ -.idea/* -os/target/* -os/.idea/* +.*/* +!.github/* +!.vscode/settings.json + +**/target/ +**/Cargo.lock + os/src/link_app.S os/src/linker.ld os/last-* diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..11de1111 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + // Prevent "can't find crate for `test`" error on no_std + // Ref: https://github.com/rust-lang/vscode-rust/issues/729 + // For vscode-rust plugin users: + "rust.target": "riscv64gc-unknown-none-elf", + "rust.all_targets": false, + // For Rust Analyzer plugin users: + "rust-analyzer.cargo.target": "riscv64gc-unknown-none-elf", + "rust-analyzer.checkOnSave.allTargets": false +} diff --git a/README.md b/README.md index 5f27d55e..e6b84dc2 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,46 @@ $ make run BOARD=k210 Type `Ctrl+]` to disconnect from K210. + +## Show runtime debug info of OS kernel version +The branch of ch9-log contains a lot of debug info. You could try to run rcore tutorial +for understand the internal behavior of os kernel. + +```sh +$ git clone https://github.com/rcore-os/rCore-Tutorial-v3.git +$ cd rCore-Tutorial-v3/os +$ git checkout ch9-log +$ make run +...... +[rustsbi] RustSBI version 0.2.0-alpha.10, adapting to RISC-V SBI v0.3 +.______ __ __ _______.___________. _______..______ __ +| _ \ | | | | / | | / || _ \ | | +| |_) | | | | | | (----`---| |----`| (----`| |_) || | +| / | | | | \ \ | | \ \ | _ < | | +| |\ \----.| `--' |.----) | | | .----) | | |_) || | +| _| `._____| \______/ |_______/ |__| |_______/ |______/ |__| + +[rustsbi] Implementation: RustSBI-QEMU Version 0.0.2 +[rustsbi-dtb] Hart count: cluster0 with 1 cores +[rustsbi] misa: RV64ACDFIMSU +[rustsbi] mideleg: ssoft, stimer, sext (0x222) +[rustsbi] medeleg: ima, ia, bkpt, la, sa, uecall, ipage, lpage, spage (0xb1ab) +[rustsbi] pmp0: 0x10000000 ..= 0x10001fff (rw-) +[rustsbi] pmp1: 0x2000000 ..= 0x200ffff (rw-) +[rustsbi] pmp2: 0xc000000 ..= 0xc3fffff (rw-) +[rustsbi] pmp3: 0x80000000 ..= 0x8fffffff (rwx) +[rustsbi] enter supervisor 0x80200000 +[KERN] rust_main() begin +[KERN] clear_bss() begin +[KERN] clear_bss() end +[KERN] mm::init() begin +[KERN] mm::init_heap() begin +[KERN] mm::init_heap() end +[KERN] mm::init_frame_allocator() begin +[KERN] mm::frame_allocator::lazy_static!FRAME_ALLOCATOR begin +...... +``` + ## Rustdoc Currently it can only help you view the code since only a tiny part of the code has been documented. diff --git a/bootloader/rustsbi-qemu.bin b/bootloader/rustsbi-qemu.bin index 5f319c3d..d74249e8 100755 Binary files a/bootloader/rustsbi-qemu.bin and b/bootloader/rustsbi-qemu.bin differ diff --git a/os/Makefile b/os/Makefile index 741c81db..0601e393 100644 --- a/os/Makefile +++ b/os/Makefile @@ -37,6 +37,9 @@ OBJCOPY := rust-objcopy --binary-architecture=riscv64 # Disassembly DISASM ?= -x +# Run usertests or usershell +TEST ?= + build: env switch-check $(KERNEL_BIN) fs-img switch-check: @@ -61,7 +64,7 @@ $(KERNEL_BIN): kernel @$(OBJCOPY) $(KERNEL_ELF) --strip-all -O binary $@ fs-img: $(APPS) - @cd ../user && make build + @cd ../user && make build TEST=$(TEST) @rm -f $(FS_IMG) @cd ../easy-fs-fuse && cargo run --release -- -s ../user/src/bin/ -t ../user/target/riscv64gc-unknown-none-elf/release/ diff --git a/os/src/boards/qemu.rs b/os/src/boards/qemu.rs index 0c3ba66d..32b9588a 100644 --- a/os/src/boards/qemu.rs +++ b/os/src/boards/qemu.rs @@ -1,9 +1,10 @@ pub const CLOCK_FREQ: usize = 12500000; pub const MMIO: &[(usize, usize)] = &[ - (0x1000_0000, 0x1000), - (0x1000_1000, 0x1000), - (0xC00_0000, 0x40_0000), + (0x1000_0000, 0x1000), // VIRT_UART0 in virt machine + (0x1000_1000, 0x1000), // VIRT_VIRTIO in virt machine + (0x0C00_0000, 0x40_0000), // VIRT_PLIC in virt machine + (0x0010_0000, 0x00_2000), // VIRT_TEST/RTC in virt machine ]; pub type BlockDeviceImpl = crate::drivers::block::VirtIOBlock; @@ -43,3 +44,84 @@ pub fn irq_handler() { } plic.complete(0, IntrTargetPriority::Supervisor, intr_src_id); } + +//ref:: https://github.com/andre-richter/qemu-exit +use core::arch::asm; + +const EXIT_SUCCESS: u32 = 0x5555; // Equals `exit(0)`. qemu successful exit + +const EXIT_FAILURE_FLAG: u32 = 0x3333; +const EXIT_FAILURE: u32 = exit_code_encode(1); // Equals `exit(1)`. qemu failed exit +const EXIT_RESET: u32 = 0x7777; // qemu reset + +pub trait QEMUExit { + /// Exit with specified return code. + /// + /// Note: For `X86`, code is binary-OR'ed with `0x1` inside QEMU. + fn exit(&self, code: u32) -> !; + + /// Exit QEMU using `EXIT_SUCCESS`, aka `0`, if possible. + /// + /// Note: Not possible for `X86`. + fn exit_success(&self) -> !; + + /// Exit QEMU using `EXIT_FAILURE`, aka `1`. + fn exit_failure(&self) -> !; +} + + +/// RISCV64 configuration +pub struct RISCV64 { + /// Address of the sifive_test mapped device. + addr: u64, +} + +/// Encode the exit code using EXIT_FAILURE_FLAG. +const fn exit_code_encode(code: u32) -> u32 { + (code << 16) | EXIT_FAILURE_FLAG +} + +impl RISCV64 { + /// Create an instance. + pub const fn new(addr: u64) -> Self { + RISCV64 { addr } + } +} + +impl QEMUExit for RISCV64 { + /// Exit qemu with specified exit code. + fn exit(&self, code: u32) -> ! { + // If code is not a special value, we need to encode it with EXIT_FAILURE_FLAG. + let code_new = match code { + EXIT_SUCCESS | EXIT_FAILURE | EXIT_RESET => code, + _ => exit_code_encode(code), + }; + + unsafe { + asm!( + "sw {0}, 0({1})", + in(reg)code_new, in(reg)self.addr + ); + + // For the case that the QEMU exit attempt did not work, transition into an infinite + // loop. Calling `panic!()` here is unfeasible, since there is a good chance + // this function here is the last expression in the `panic!()` handler + // itself. This prevents a possible infinite loop. + loop { + asm!("wfi", options(nomem, nostack)); + } + } + } + + fn exit_success(&self) -> ! { + self.exit(EXIT_SUCCESS); + } + + fn exit_failure(&self) -> ! { + self.exit(EXIT_FAILURE); + } +} + +const VIRT_TEST: u64 =0x100000; + +pub const QEMU_EXIT_HANDLE: RISCV64 = RISCV64::new(VIRT_TEST); diff --git a/os/src/drivers/block/sdcard.rs b/os/src/drivers/block/sdcard.rs index 9b12ffd2..756e9a00 100644 --- a/os/src/drivers/block/sdcard.rs +++ b/os/src/drivers/block/sdcard.rs @@ -321,14 +321,14 @@ impl SDCard { * Get SD card data response. * @param None * @retval The SD status: Read data response xxx01 - * - status 010: Data accecpted + * - status 010: Data accepted * - status 101: Data rejected due to a crc error * - status 110: Data rejected due to a Write error. * - status 111: Data rejected due to other error. */ fn get_dataresponse(&self) -> u8 { let response = &mut [0u8]; - /* Read resonse */ + /* Read response */ self.read_data(response); /* Mask unused bits */ response[0] &= 0x1F; @@ -423,7 +423,7 @@ impl SDCard { /* Byte 15 */ CSD_CRC: (csd_tab[15] & 0xFE) >> 1, Reserved4: 1, - /* Return the reponse */ + /* Return the response */ }) } diff --git a/os/src/drivers/block/virtio_blk.rs b/os/src/drivers/block/virtio_blk.rs index a30e3bf8..75e69c17 100644 --- a/os/src/drivers/block/virtio_blk.rs +++ b/os/src/drivers/block/virtio_blk.rs @@ -20,7 +20,8 @@ pub struct VirtIOBlock { } lazy_static! { - static ref QUEUE_FRAMES: UPIntrFreeCell> = unsafe { UPIntrFreeCell::new(Vec::new()) }; + static ref QUEUE_FRAMES: UPIntrFreeCell> = + unsafe { UPIntrFreeCell::new(Vec::new()) }; } impl BlockDevice for VirtIOBlock { diff --git a/os/src/drivers/chardev/ns16550a.rs b/os/src/drivers/chardev/ns16550a.rs index 667a6604..da290633 100644 --- a/os/src/drivers/chardev/ns16550a.rs +++ b/os/src/drivers/chardev/ns16550a.rs @@ -1,7 +1,6 @@ ///! Ref: https://www.lammertbies.nl/comm/info/serial-uart ///! Ref: ns16550a datasheet: https://datasheetspdf.com/pdf-file/605590/NationalSemiconductor/NS16550A/1 ///! Ref: ns16450 datasheet: https://datasheetspdf.com/pdf-file/1311818/NationalSemiconductor/NS16450/1 - use super::CharDevice; use crate::sync::{Condvar, UPIntrFreeCell}; use crate::task::schedule; @@ -12,7 +11,7 @@ use volatile::{ReadOnly, Volatile, WriteOnly}; bitflags! { /// InterruptEnableRegister pub struct IER: u8 { - const RX_AVALIABLE = 1 << 0; + const RX_AVAILABLE = 1 << 0; const TX_EMPTY = 1 << 1; } @@ -95,7 +94,7 @@ impl NS16550aRaw { mcr |= MCR::REQUEST_TO_SEND; mcr |= MCR::AUX_OUTPUT2; read_end.mcr.write(mcr); - let ier = IER::RX_AVALIABLE; + let ier = IER::RX_AVAILABLE; read_end.ier.write(ier); } diff --git a/os/src/lang_items.rs b/os/src/lang_items.rs index 6788148a..a33943a1 100644 --- a/os/src/lang_items.rs +++ b/os/src/lang_items.rs @@ -18,7 +18,7 @@ fn panic(info: &PanicInfo) -> ! { unsafe { backtrace(); } - shutdown() + shutdown(255) } unsafe fn backtrace() { diff --git a/os/src/main.rs b/os/src/main.rs index a9323be1..3c23069b 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -46,7 +46,8 @@ use lazy_static::*; use sync::UPIntrFreeCell; lazy_static! { - pub static ref DEV_NON_BLOCKING_ACCESS: UPIntrFreeCell = unsafe { UPIntrFreeCell::new(false) }; + pub static ref DEV_NON_BLOCKING_ACCESS: UPIntrFreeCell = + unsafe { UPIntrFreeCell::new(false) }; } #[no_mangle] diff --git a/os/src/sbi.rs b/os/src/sbi.rs index 2e2804b2..8e0c8567 100644 --- a/os/src/sbi.rs +++ b/os/src/sbi.rs @@ -16,7 +16,7 @@ const SBI_SHUTDOWN: usize = 8; fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize { let mut ret; unsafe { - asm!( + core::arch::asm!( "ecall", inlateout("x10") arg0 => ret, in("x11") arg1, @@ -39,7 +39,9 @@ pub fn console_getchar() -> usize { sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0) } -pub fn shutdown() -> ! { - sbi_call(SBI_SHUTDOWN, 0, 0, 0); +use crate::board::QEMUExit; +pub fn shutdown(exit_code: usize) -> ! { + //sbi_call(SBI_SHUTDOWN, exit_code, 0, 0); + crate::board::QEMU_EXIT_HANDLE.exit_failure(); panic!("It should shutdown!"); } diff --git a/os/src/sync/condvar.rs b/os/src/sync/condvar.rs index c50a53e2..714782c2 100644 --- a/os/src/sync/condvar.rs +++ b/os/src/sync/condvar.rs @@ -1,5 +1,8 @@ use crate::sync::{Mutex, UPIntrFreeCell}; -use crate::task::{add_task, block_current_task, block_current_and_run_next, current_task, TaskControlBlock, TaskContext}; +use crate::task::{ + add_task, block_current_and_run_next, block_current_task, current_task, TaskContext, + TaskControlBlock, +}; use alloc::{collections::VecDeque, sync::Arc}; pub struct Condvar { diff --git a/os/src/sync/up.rs b/os/src/sync/up.rs index 55c8d40a..6c7a0f4a 100644 --- a/os/src/sync/up.rs +++ b/os/src/sync/up.rs @@ -1,7 +1,7 @@ use core::cell::{RefCell, RefMut, UnsafeCell}; use core::ops::{Deref, DerefMut}; -use riscv::register::sstatus; use lazy_static::*; +use riscv::register::sstatus; /* /// Wrap a static data structure inside it so that we are @@ -56,9 +56,8 @@ pub struct IntrMaskingInfo { } lazy_static! { - static ref INTR_MASKING_INFO: UPSafeCellRaw = unsafe { - UPSafeCellRaw::new(IntrMaskingInfo::new()) - }; + static ref INTR_MASKING_INFO: UPSafeCellRaw = + unsafe { UPSafeCellRaw::new(IntrMaskingInfo::new()) }; } impl IntrMaskingInfo { @@ -71,7 +70,9 @@ impl IntrMaskingInfo { pub fn enter(&mut self) { let sie = sstatus::read().sie(); - unsafe { sstatus::clear_sie(); } + unsafe { + sstatus::clear_sie(); + } if self.nested_level == 0 { self.sie_before_masking = sie; } @@ -81,7 +82,9 @@ impl IntrMaskingInfo { pub fn exit(&mut self) { self.nested_level -= 1; if self.nested_level == 0 && self.sie_before_masking { - unsafe { sstatus::set_sie(); } + unsafe { + sstatus::set_sie(); + } } } } @@ -101,13 +104,17 @@ impl UPIntrFreeCell { inner: RefCell::new(value), } } + /// Panic if the data has been borrowed. pub fn exclusive_access(&self) -> UPIntrRefMut<'_, T> { INTR_MASKING_INFO.get_mut().enter(); UPIntrRefMut(Some(self.inner.borrow_mut())) } - pub fn exclusive_session(&self, f: F) -> V where F: FnOnce(&mut T) -> V { + pub fn exclusive_session(&self, f: F) -> V + where + F: FnOnce(&mut T) -> V, + { let mut inner = self.exclusive_access(); f(inner.deref_mut()) } @@ -131,4 +138,3 @@ impl<'a, T> DerefMut for UPIntrRefMut<'a, T> { self.0.as_mut().unwrap().deref_mut() } } - diff --git a/os/src/task/id.rs b/os/src/task/id.rs index e336c500..c1771813 100644 --- a/os/src/task/id.rs +++ b/os/src/task/id.rs @@ -46,6 +46,8 @@ lazy_static! { unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) }; } +pub const IDLE_PID: usize = 0; + pub struct PidHandle(pub usize); pub fn pid_alloc() -> PidHandle { diff --git a/os/src/task/mod.rs b/os/src/task/mod.rs index c3053f06..3c762b77 100644 --- a/os/src/task/mod.rs +++ b/os/src/task/mod.rs @@ -17,7 +17,7 @@ use process::ProcessControlBlock; use switch::__switch; pub use context::TaskContext; -pub use id::{kstack_alloc, pid_alloc, KernelStack, PidHandle}; +pub use id::{kstack_alloc, pid_alloc, KernelStack, PidHandle, IDLE_PID}; pub use manager::{add_task, pid2process, remove_from_pid2process}; pub use processor::{ current_kstack_top, current_process, current_task, current_trap_cx, current_trap_cx_user_va, @@ -53,10 +53,12 @@ pub fn block_current_task() -> *mut TaskContext { } pub fn block_current_and_run_next() { - let task_cx_ptr = block_current_task(); + let task_cx_ptr = block_current_task(); schedule(task_cx_ptr); } +use crate::board::QEMUExit; + pub fn exit_current_and_run_next(exit_code: i32) { let task = take_current_task().unwrap(); let mut task_inner = task.inner_exclusive_access(); @@ -72,7 +74,21 @@ pub fn exit_current_and_run_next(exit_code: i32) { // however, if this is the main thread of current process // the process should terminate at once if tid == 0 { - remove_from_pid2process(process.getpid()); + let pid = process.getpid(); + if pid == IDLE_PID { + println!( + "[kernel] Idle process exit with exit_code {} ...", + exit_code + ); + if exit_code != 0 { + //crate::sbi::shutdown(255); //255 == -1 for err hint + crate::board::QEMU_EXIT_HANDLE.exit_failure(); + } else { + //crate::sbi::shutdown(0); //0 for success hint + crate::board::QEMU_EXIT_HANDLE.exit_success(); + } + } + remove_from_pid2process(pid); let mut process_inner = process.inner_exclusive_access(); // mark this process as a zombie process process_inner.is_zombie = true; diff --git a/os/src/task/processor.rs b/os/src/task/processor.rs index f196f681..dfa7d4c5 100644 --- a/os/src/task/processor.rs +++ b/os/src/task/processor.rs @@ -30,7 +30,8 @@ impl Processor { } lazy_static! { - pub static ref PROCESSOR: UPIntrFreeCell = unsafe { UPIntrFreeCell::new(Processor::new()) }; + pub static ref PROCESSOR: UPIntrFreeCell = + unsafe { UPIntrFreeCell::new(Processor::new()) }; } pub fn run_tasks() { @@ -94,9 +95,8 @@ pub fn current_kstack_top() -> usize { } pub fn schedule(switched_task_cx_ptr: *mut TaskContext) { - let idle_task_cx_ptr = PROCESSOR.exclusive_session(|processor| { - processor.get_idle_task_cx_ptr() - }); + let idle_task_cx_ptr = + PROCESSOR.exclusive_session(|processor| processor.get_idle_task_cx_ptr()); unsafe { __switch(switched_task_cx_ptr, idle_task_cx_ptr); } diff --git a/os/src/task/task.rs b/os/src/task/task.rs index 85d949f8..c620890e 100644 --- a/os/src/task/task.rs +++ b/os/src/task/task.rs @@ -1,7 +1,10 @@ use super::id::TaskUserRes; use super::{kstack_alloc, KernelStack, ProcessControlBlock, TaskContext}; use crate::trap::TrapContext; -use crate::{mm::PhysPageNum, sync::{UPIntrFreeCell, UPIntrRefMut}}; +use crate::{ + mm::PhysPageNum, + sync::{UPIntrFreeCell, UPIntrRefMut}, +}; use alloc::sync::{Arc, Weak}; pub struct TaskControlBlock { diff --git a/os/src/trap/mod.rs b/os/src/trap/mod.rs index 5eb417dc..ce2aae8e 100644 --- a/os/src/trap/mod.rs +++ b/os/src/trap/mod.rs @@ -11,7 +11,7 @@ use core::arch::{asm, global_asm}; use riscv::register::{ mtvec::TrapMode, scause::{self, Exception, Interrupt, Trap}, - sie, stval, stvec, sstatus, sscratch, + sie, sscratch, sstatus, stval, stvec, }; global_asm!(include_str!("trap.S")); @@ -23,7 +23,7 @@ pub fn init() { fn set_kernel_trap_entry() { extern "C" { fn __alltraps(); - fn __alltraps_k(); + fn __alltraps_k(); } let __alltraps_k_va = __alltraps_k as usize - __alltraps as usize + TRAMPOLINE; unsafe { @@ -52,7 +52,7 @@ fn enable_supervisor_interrupt() { fn disable_supervisor_interrupt() { unsafe { - sstatus::clear_sie(); + sstatus::clear_sie(); } } @@ -67,7 +67,7 @@ pub fn trap_handler() -> ! { // jump to next instruction anyway let mut cx = current_trap_cx(); cx.sepc += 4; - + enable_supervisor_interrupt(); // get system call return value @@ -150,19 +150,19 @@ pub fn trap_from_kernel(_trap_cx: &TrapContext) { match scause.cause() { Trap::Interrupt(Interrupt::SupervisorExternal) => { crate::board::irq_handler(); - }, + } Trap::Interrupt(Interrupt::SupervisorTimer) => { set_next_trigger(); check_timer(); // do not schedule now - }, + } _ => { panic!( "Unsupported trap from kernel: {:?}, stval = {:#x}!", scause.cause(), stval ); - }, + } } } diff --git a/setenv.sh b/setenv.sh new file mode 100644 index 00000000..e3842d51 --- /dev/null +++ b/setenv.sh @@ -0,0 +1,2 @@ +export PATH=$(rustc --print sysroot)/bin:$PATH +export RUST_SRC_PATH=$(rustc --print sysroot)/lib/rustlib/src/rust/library/ diff --git a/user/Makefile b/user/Makefile index e2eaf994..c09b5e96 100644 --- a/user/Makefile +++ b/user/Makefile @@ -8,9 +8,15 @@ BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS)) OBJDUMP := rust-objdump --arch-name=riscv64 OBJCOPY := rust-objcopy --binary-architecture=riscv64 +CP := cp + +TEST ?= elf: $(APPS) @cargo build --release +ifeq ($(TEST), 1) + @$(CP) $(TARGET_DIR)/usertests $(TARGET_DIR)/initproc +endif binary: elf $(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));) diff --git a/user/src/bin/cat.rs b/user/src/bin/cat.rs index 9cbed233..fea5a240 100644 --- a/user/src/bin/cat.rs +++ b/user/src/bin/cat.rs @@ -9,10 +9,14 @@ use user_lib::{close, open, read, OpenFlags}; #[no_mangle] pub fn main(argc: usize, argv: &[&str]) -> i32 { + println!("argc = {}", argc); + for (i, arg) in argv.iter().enumerate() { + println!("argv[{}] = {}", i, arg); + } assert!(argc == 2); let fd = open(argv[1], OpenFlags::RDONLY); if fd == -1 { - panic!("Error occured when opening file"); + panic!("Error occurred when opening file"); } let fd = fd as usize; let mut buf = [0u8; 256]; diff --git a/user/src/bin/eisenberg.rs b/user/src/bin/eisenberg.rs new file mode 100644 index 00000000..f01ea1a2 --- /dev/null +++ b/user/src/bin/eisenberg.rs @@ -0,0 +1,133 @@ +#![no_std] +#![no_main] +#![feature(core_intrinsics)] + +#[macro_use] +extern crate user_lib; +extern crate alloc; +extern crate core; + +use user_lib::{thread_create, waittid, exit, sleep}; +use alloc::vec::Vec; +use core::sync::atomic::{AtomicUsize, Ordering}; + +const N: usize = 2; +const THREAD_NUM: usize = 10; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum FlagState { + Out, Want, In, +} + +static mut TURN: usize = 0; +static mut FLAG: [FlagState; THREAD_NUM] = [FlagState::Out; THREAD_NUM]; + +static GUARD: AtomicUsize = AtomicUsize::new(0); + +fn critical_test_enter() { + assert_eq!(GUARD.fetch_add(1, Ordering::SeqCst), 0); +} + +fn critical_test_claim() { + assert_eq!(GUARD.load(Ordering::SeqCst), 1); +} + +fn critical_test_exit() { + assert_eq!(GUARD.fetch_sub(1, Ordering::SeqCst), 1); +} + +fn eisenberg_enter_critical(id: usize) { + /* announce that we want to enter */ + loop { + println!("Thread[{}] try enter", id); + vstore!(&FLAG[id], FlagState::Want); + loop { + /* check if any with higher priority is `Want` or `In` */ + let mut prior_thread:Option = None; + let turn = vload!(&TURN); + let ring_id = if id < turn { id + THREAD_NUM } else { id }; + // FLAG.iter() may lead to some errors, use for-loop instead + for i in turn..ring_id { + if vload!(&FLAG[i % THREAD_NUM]) != FlagState::Out { + prior_thread = Some(i % THREAD_NUM); + break; + } + } + if prior_thread.is_none() { + break; + } + println!("Thread[{}]: prior thread {} exist, sleep and retry", + id, prior_thread.unwrap()); + sleep(1); + } + /* now tentatively claim the resource */ + vstore!(&FLAG[id], FlagState::In); + /* enforce the order of `claim` and `conflict check`*/ + memory_fence!(); + /* check if anthor thread is also `In`, which imply a conflict*/ + let mut conflict = false; + for i in 0..THREAD_NUM { + if i != id && vload!(&FLAG[i]) == FlagState::In { + conflict = true; + } + } + if !conflict { + break; + } + println!("Thread[{}]: CONFLECT!", id); + /* no need to sleep */ + } + /* clain the trun */ + vstore!(&TURN, id); + println!("Thread[{}] enter", id); +} + +fn eisenberg_exit_critical(id: usize) { + /* find next one who wants to enter and give the turn to it*/ + let mut next = id; + let ring_id = id + THREAD_NUM; + for i in (id+1)..ring_id { + let idx = i % THREAD_NUM; + if vload!(&FLAG[idx]) == FlagState::Want { + next = idx; + break; + } + } + vstore!(&TURN, next); + /* All done */ + vstore!(&FLAG[id], FlagState::Out); + println!("Thread[{}] exit, give turn to {}", id, next); +} + +pub fn thread_fn(id: usize) -> ! { + println!("Thread[{}] init.", id); + for _ in 0..N { + eisenberg_enter_critical(id); + critical_test_enter(); + for _ in 0..3 { + critical_test_claim(); + sleep(2); + } + critical_test_exit(); + eisenberg_exit_critical(id); + } + exit(0) +} + +#[no_mangle] +pub fn main() -> i32 { + let mut v = Vec::new(); + // TODO: really shuffle + assert_eq!(THREAD_NUM, 10); + let shuffle:[usize; 10] = [0, 7, 4, 6, 2, 9, 8, 1, 3, 5]; + for i in 0..THREAD_NUM { + v.push(thread_create(thread_fn as usize, shuffle[i])); + } + for tid in v.iter() { + let exit_code = waittid(*tid as usize); + assert_eq!(exit_code, 0, "thread conflict happened!"); + println!("thread#{} exited with code {}", tid, exit_code); + } + println!("main thread exited."); + 0 +} \ No newline at end of file diff --git a/user/src/bin/forktree.rs b/user/src/bin/forktree.rs index bfcfc4ca..2d225c13 100644 --- a/user/src/bin/forktree.rs +++ b/user/src/bin/forktree.rs @@ -21,6 +21,7 @@ fn fork_child(cur: &str, branch: char) { yield_(); exit(0); } + exit(0); } fn fork_tree(cur: &str) { diff --git a/user/src/bin/huge_write_mt.rs b/user/src/bin/huge_write_mt.rs index 63146a58..8ca75783 100644 --- a/user/src/bin/huge_write_mt.rs +++ b/user/src/bin/huge_write_mt.rs @@ -5,9 +5,9 @@ extern crate user_lib; extern crate alloc; -use alloc::{vec::Vec, string::String, fmt::format}; +use alloc::{fmt::format, string::String, vec::Vec}; +use user_lib::{close, get_time, gettid, open, write, OpenFlags}; use user_lib::{exit, thread_create, waittid}; -use user_lib::{close, get_time, open, write, OpenFlags, gettid}; fn worker(size_kib: usize) { let mut buffer = [0u8; 1024]; // 1KiB @@ -17,11 +17,11 @@ fn worker(size_kib: usize) { let filename = format(format_args!("testf{}\0", gettid())); let f = open(filename.as_str(), OpenFlags::CREATE | OpenFlags::WRONLY); if f < 0 { - panic!("Open test file failed!"); + panic!("Open test file failed!"); } let f = f as usize; for _ in 0..size_kib { - write(f, &buffer); + write(f, &buffer); } close(f); exit(0) @@ -34,18 +34,18 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 { let size_kb = size_mb << 10; let workers = argv[1].parse::().expect("wrong argument"); assert!(workers >= 1 && size_kb % workers == 0, "wrong argument"); - + let start = get_time(); - + let mut v = Vec::new(); let size_mb = 1usize; for _ in 0..workers { - v.push(thread_create(worker as usize, size_kb / workers)); + v.push(thread_create(worker as usize, size_kb / workers)); } for tid in v.iter() { assert_eq!(0, waittid(*tid as usize)); } - + let time_ms = (get_time() - start) as usize; let speed_kbs = size_kb * 1000 / time_ms; println!( diff --git a/user/src/bin/peterson.rs b/user/src/bin/peterson.rs new file mode 100644 index 00000000..21865e24 --- /dev/null +++ b/user/src/bin/peterson.rs @@ -0,0 +1,78 @@ +#![no_std] +#![no_main] +#![feature(core_intrinsics)] +#![feature(asm)] + +#[macro_use] +extern crate user_lib; +extern crate alloc; +extern crate core; + +use user_lib::{thread_create, waittid, exit, sleep}; +use core::sync::atomic::{AtomicUsize, Ordering}; +use alloc::vec::Vec; +const N: usize = 3; + +static mut TURN: usize = 0; +static mut FLAG: [bool; 2] = [false; 2]; +static GUARD: AtomicUsize = AtomicUsize::new(0); + +fn critical_test_enter() { + assert_eq!(GUARD.fetch_add(1, Ordering::SeqCst), 0); +} + +fn critical_test_claim() { + assert_eq!(GUARD.load(Ordering::SeqCst), 1); +} + +fn critical_test_exit() { + assert_eq!(GUARD.fetch_sub(1, Ordering::SeqCst), 1); +} + +fn peterson_enter_critical(id: usize, peer_id: usize) { + println!("Thread[{}] try enter", id); + vstore!(&FLAG[id], true); + vstore!(&TURN, peer_id); + memory_fence!(); + while vload!(&FLAG[peer_id]) && vload!(&TURN) == peer_id { + println!("Thread[{}] enter fail", id); + sleep(1); + println!("Thread[{}] retry enter", id); + } + println!("Thread[{}] enter", id); +} + +fn peterson_exit_critical(id: usize) { + vstore!(&FLAG[id], false); + println!("Thread[{}] exit", id); +} + +pub fn thread_fn(id: usize) -> ! { + println!("Thread[{}] init.", id); + let peer_id: usize = id ^ 1; + for _ in 0..N { + peterson_enter_critical(id, peer_id); + critical_test_enter(); + for _ in 0..3 { + critical_test_claim(); + sleep(2); + } + critical_test_exit(); + peterson_exit_critical(id); + } + exit(0) +} + +#[no_mangle] +pub fn main() -> i32 { + let mut v = Vec::new(); + v.push(thread_create(thread_fn as usize, 0)); + // v.push(thread_create(thread_fn as usize, 1)); + for tid in v.iter() { + let exit_code = waittid(*tid as usize); + assert_eq!(exit_code, 0, "thread conflict happened!"); + println!("thread#{} exited with code {}", tid, exit_code); + } + println!("main thread exited."); + 0 +} \ No newline at end of file diff --git a/user/src/bin/race_adder_arg.rs b/user/src/bin/race_adder_arg.rs index 7c8b7074..ba99b62f 100644 --- a/user/src/bin/race_adder_arg.rs +++ b/user/src/bin/race_adder_arg.rs @@ -34,7 +34,10 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 { } else if argc == 2 { count = argv[1].to_string().parse::().unwrap(); } else { - println!("ERROR in argv"); + println!( + "ERROR in argv, argc is {}, argv[0] {} , argv[1] {} , argv[2] {}", + argc, argv[0], argv[1], argv[2] + ); exit(-1); } diff --git a/user/src/bin/run_pipe_test.rs b/user/src/bin/run_pipe_test.rs index ea99b6a7..5f50b0d6 100644 --- a/user/src/bin/run_pipe_test.rs +++ b/user/src/bin/run_pipe_test.rs @@ -8,7 +8,7 @@ use user_lib::{exec, fork, wait}; #[no_mangle] pub fn main() -> i32 { - for i in 0..1000 { + for i in 0..50 { if fork() == 0 { exec("pipe_large_test\0", &[core::ptr::null::()]); } else { diff --git a/user/src/bin/usertests.rs b/user/src/bin/usertests.rs index 4fd49a4f..112f9969 100644 --- a/user/src/bin/usertests.rs +++ b/user/src/bin/usertests.rs @@ -4,40 +4,134 @@ #[macro_use] extern crate user_lib; -static TESTS: &[&str] = &[ - "exit\0", - "fantastic_text\0", - "forktest\0", - "forktest2\0", - "forktest_simple\0", - "hello_world\0", - "matrix\0", - "sleep\0", - "sleep_simple\0", - "stack_overflow\0", - "yield\0", +// not in SUCC_TESTS & FAIL_TESTS +// count_lines, infloop, user_shell, usertests + +// item of TESTS : app_name(argv_0), argv_1, argv_2, argv_3, exit_code +static SUCC_TESTS: &[(&str, &str, &str, &str, i32)] = &[ + ("filetest_simple\0", "\0", "\0", "\0", 0), + ("cat\0", "filea\0", "\0", "\0", 0), + ("cmdline_args\0", "1\0", "2\0", "3\0", 0), + ("eisenberg\0", "\0", "\0", "\0", 0), + ("exit\0", "\0", "\0", "\0", 0), + ("fantastic_text\0", "\0", "\0", "\0", 0), + ("forktest_simple\0", "\0", "\0", "\0", 0), + ("forktest\0", "\0", "\0", "\0", 0), + ("forktest2\0", "\0", "\0", "\0", 0), + ("forktree\0", "\0", "\0", "\0", 0), + ("hello_world\0", "\0", "\0", "\0", 0), + ("huge_write\0", "\0", "\0", "\0", 0), + ("matrix\0", "\0", "\0", "\0", 0), + ("mpsc_sem\0", "\0", "\0", "\0", 0), + ("peterson\0", "\0", "\0", "\0", 0), + ("phil_din_mutex\0", "\0", "\0", "\0", 0), + ("pipe_large_test\0", "\0", "\0", "\0", 0), + ("pipetest\0", "\0", "\0", "\0", 0), + ("race_adder_arg\0", "3\0", "\0", "\0", 0), + ("race_adder_atomic\0", "\0", "\0", "\0", 0), + ("race_adder_mutex_blocking\0", "\0", "\0", "\0", 0), + ("race_adder_mutex_spin\0", "\0", "\0", "\0", 0), + ("run_pipe_test\0", "\0", "\0", "\0", 0), + ("sleep_simple\0", "\0", "\0", "\0", 0), + ("sleep\0", "\0", "\0", "\0", 0), + ("sleep_simple\0", "\0", "\0", "\0", 0), + ("sync_sem\0", "\0", "\0", "\0", 0), + ("test_condvar\0", "\0", "\0", "\0", 0), + ("threads_arg\0", "\0", "\0", "\0", 0), + ("threads\0", "\0", "\0", "\0", 0), + ("yield\0", "\0", "\0", "\0", 0), +]; + +static FAIL_TESTS: &[(&str, &str, &str, &str, i32)] = &[ + ("stack_overflow\0", "\0", "\0", "\0", -11), + ("race_adder_loop\0", "\0", "\0", "\0", -6), + ("priv_csr\0", "\0", "\0", "\0", -4), + ("priv_inst\0", "\0", "\0", "\0", -4), + ("store_fault\0", "\0", "\0", "\0", -11), + ("until_timeout\0", "\0", "\0", "\0", -6), + ("race_adder\0", "\0", "\0", "\0", -6), + ("huge_write_mt\0", "\0", "\0", "\0", -6), ]; use user_lib::{exec, fork, waitpid}; -#[no_mangle] -pub fn main() -> i32 { - for test in TESTS { - println!("Usertests: Running {}", test); +fn run_tests(tests: &[(&str, &str, &str, &str, i32)]) -> i32 { + let mut pass_num = 0; + let mut arr: [*const u8; 4] = [ + core::ptr::null::(), + core::ptr::null::(), + core::ptr::null::(), + core::ptr::null::(), + ]; + + for test in tests { + println!("Usertests: Running {}", test.0); + arr[0] = test.0.as_ptr(); + if test.1 != "\0" { + arr[1] = test.1.as_ptr(); + arr[2] = core::ptr::null::(); + arr[3] = core::ptr::null::(); + if test.2 != "\0" { + arr[2] = test.2.as_ptr(); + arr[3] = core::ptr::null::(); + if test.3 != "\0" { + arr[3] = test.3.as_ptr(); + } else { + arr[3] = core::ptr::null::(); + } + } else { + arr[2] = core::ptr::null::(); + arr[3] = core::ptr::null::(); + } + } else { + arr[1] = core::ptr::null::(); + arr[2] = core::ptr::null::(); + arr[3] = core::ptr::null::(); + } + let pid = fork(); if pid == 0 { - exec(*test, &[core::ptr::null::()]); + exec(test.0, &arr[..]); panic!("unreachable!"); } else { let mut exit_code: i32 = Default::default(); let wait_pid = waitpid(pid as usize, &mut exit_code); assert_eq!(pid, wait_pid); + if exit_code == test.4 { + // summary apps with exit_code + pass_num = pass_num + 1; + } println!( "\x1b[32mUsertests: Test {} in Process {} exited with code {}\x1b[0m", - test, pid, exit_code + test.0, pid, exit_code ); } } - println!("Usertests passed!"); - 0 + pass_num +} + +#[no_mangle] +pub fn main() -> i32 { + let succ_num = run_tests(SUCC_TESTS); + let err_num = run_tests(FAIL_TESTS); + if succ_num == SUCC_TESTS.len() as i32 && err_num == FAIL_TESTS.len() as i32 { + println!("{} of sueecssed apps, {} of failed apps run correctly. \nUsertests passed!", SUCC_TESTS.len(), FAIL_TESTS.len() ); + return 0; + } + if succ_num != SUCC_TESTS.len() as i32 { + println!( + "all successed app_num is {} , but only passed {}", + SUCC_TESTS.len(), + succ_num + ); + } + if err_num != FAIL_TESTS.len() as i32 { + println!( + "all failed app_num is {} , but only passed {}", + FAIL_TESTS.len(), + err_num + ); + } + println!(" Usertests failed!"); + return -1; } diff --git a/user/src/lib.rs b/user/src/lib.rs index 60f39ffb..dd32e44f 100644 --- a/user/src/lib.rs +++ b/user/src/lib.rs @@ -2,6 +2,7 @@ #![feature(linkage)] #![feature(panic_info_message)] #![feature(alloc_error_handler)] +#![feature(core_intrinsics)] #[macro_use] pub mod console; @@ -197,3 +198,24 @@ pub fn condvar_signal(condvar_id: usize) { pub fn condvar_wait(condvar_id: usize, mutex_id: usize) { sys_condvar_wait(condvar_id, mutex_id); } + +#[macro_export] +macro_rules! vstore { + ($var_ref: expr, $value: expr) => { + unsafe { core::intrinsics::volatile_store($var_ref as *const _ as _, $value) } + }; +} + +#[macro_export] +macro_rules! vload { + ($var_ref: expr) => { + unsafe { core::intrinsics::volatile_load($var_ref as *const _ as _) } + }; +} + +#[macro_export] +macro_rules! memory_fence { + () => { + core::sync::atomic::fence(core::sync::atomic::Ordering::SeqCst) + }; +} \ No newline at end of file diff --git a/user/src/syscall.rs b/user/src/syscall.rs index 4cd714ef..b4bb67a0 100644 --- a/user/src/syscall.rs +++ b/user/src/syscall.rs @@ -1,5 +1,3 @@ -use core::arch::asm; - const SYSCALL_DUP: usize = 24; const SYSCALL_OPEN: usize = 56; const SYSCALL_CLOSE: usize = 57; @@ -31,7 +29,7 @@ const SYSCALL_CONDVAR_WAIT: usize = 1032; fn syscall(id: usize, args: [usize; 3]) -> isize { let mut ret: isize; unsafe { - asm!( + core::arch::asm!( "ecall", inlateout("x10") args[0] => ret, in("x11") args[1],