mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-22 01:16:26 +04:00
merge from main
This commit is contained in:
commit
d58ec14719
66
.github/workflows/doc-and-test.yml
vendored
Normal file
66
.github/workflows/doc-and-test.yml
vendored
Normal file
@ -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
|
10
.gitignore
vendored
10
.gitignore
vendored
@ -1,6 +1,10 @@
|
|||||||
.idea/*
|
.*/*
|
||||||
os/target/*
|
!.github/*
|
||||||
os/.idea/*
|
!.vscode/settings.json
|
||||||
|
|
||||||
|
**/target/
|
||||||
|
**/Cargo.lock
|
||||||
|
|
||||||
os/src/link_app.S
|
os/src/link_app.S
|
||||||
os/src/linker.ld
|
os/src/linker.ld
|
||||||
os/last-*
|
os/last-*
|
||||||
|
10
.vscode/settings.json
vendored
Normal file
10
.vscode/settings.json
vendored
Normal file
@ -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
|
||||||
|
}
|
40
README.md
40
README.md
@ -188,6 +188,46 @@ $ make run BOARD=k210
|
|||||||
|
|
||||||
Type `Ctrl+]` to disconnect from 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
|
## Rustdoc
|
||||||
|
|
||||||
Currently it can only help you view the code since only a tiny part of the code has been documented.
|
Currently it can only help you view the code since only a tiny part of the code has been documented.
|
||||||
|
Binary file not shown.
@ -37,6 +37,9 @@ OBJCOPY := rust-objcopy --binary-architecture=riscv64
|
|||||||
# Disassembly
|
# Disassembly
|
||||||
DISASM ?= -x
|
DISASM ?= -x
|
||||||
|
|
||||||
|
# Run usertests or usershell
|
||||||
|
TEST ?=
|
||||||
|
|
||||||
build: env switch-check $(KERNEL_BIN) fs-img
|
build: env switch-check $(KERNEL_BIN) fs-img
|
||||||
|
|
||||||
switch-check:
|
switch-check:
|
||||||
@ -61,7 +64,7 @@ $(KERNEL_BIN): kernel
|
|||||||
@$(OBJCOPY) $(KERNEL_ELF) --strip-all -O binary $@
|
@$(OBJCOPY) $(KERNEL_ELF) --strip-all -O binary $@
|
||||||
|
|
||||||
fs-img: $(APPS)
|
fs-img: $(APPS)
|
||||||
@cd ../user && make build
|
@cd ../user && make build TEST=$(TEST)
|
||||||
@rm -f $(FS_IMG)
|
@rm -f $(FS_IMG)
|
||||||
@cd ../easy-fs-fuse && cargo run --release -- -s ../user/src/bin/ -t ../user/target/riscv64gc-unknown-none-elf/release/
|
@cd ../easy-fs-fuse && cargo run --release -- -s ../user/src/bin/ -t ../user/target/riscv64gc-unknown-none-elf/release/
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
pub const CLOCK_FREQ: usize = 12500000;
|
pub const CLOCK_FREQ: usize = 12500000;
|
||||||
|
|
||||||
pub const MMIO: &[(usize, usize)] = &[
|
pub const MMIO: &[(usize, usize)] = &[
|
||||||
(0x1000_0000, 0x1000),
|
(0x1000_0000, 0x1000), // VIRT_UART0 in virt machine
|
||||||
(0x1000_1000, 0x1000),
|
(0x1000_1000, 0x1000), // VIRT_VIRTIO in virt machine
|
||||||
(0xC00_0000, 0x40_0000),
|
(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;
|
pub type BlockDeviceImpl = crate::drivers::block::VirtIOBlock;
|
||||||
@ -43,3 +44,84 @@ pub fn irq_handler() {
|
|||||||
}
|
}
|
||||||
plic.complete(0, IntrTargetPriority::Supervisor, intr_src_id);
|
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);
|
||||||
|
@ -321,14 +321,14 @@ impl</*'a,*/ X: SPI> SDCard</*'a,*/ X> {
|
|||||||
* Get SD card data response.
|
* Get SD card data response.
|
||||||
* @param None
|
* @param None
|
||||||
* @retval The SD status: Read data response xxx0<status>1
|
* @retval The SD status: Read data response xxx0<status>1
|
||||||
* - status 010: Data accecpted
|
* - status 010: Data accepted
|
||||||
* - status 101: Data rejected due to a crc error
|
* - status 101: Data rejected due to a crc error
|
||||||
* - status 110: Data rejected due to a Write error.
|
* - status 110: Data rejected due to a Write error.
|
||||||
* - status 111: Data rejected due to other error.
|
* - status 111: Data rejected due to other error.
|
||||||
*/
|
*/
|
||||||
fn get_dataresponse(&self) -> u8 {
|
fn get_dataresponse(&self) -> u8 {
|
||||||
let response = &mut [0u8];
|
let response = &mut [0u8];
|
||||||
/* Read resonse */
|
/* Read response */
|
||||||
self.read_data(response);
|
self.read_data(response);
|
||||||
/* Mask unused bits */
|
/* Mask unused bits */
|
||||||
response[0] &= 0x1F;
|
response[0] &= 0x1F;
|
||||||
@ -423,7 +423,7 @@ impl</*'a,*/ X: SPI> SDCard</*'a,*/ X> {
|
|||||||
/* Byte 15 */
|
/* Byte 15 */
|
||||||
CSD_CRC: (csd_tab[15] & 0xFE) >> 1,
|
CSD_CRC: (csd_tab[15] & 0xFE) >> 1,
|
||||||
Reserved4: 1,
|
Reserved4: 1,
|
||||||
/* Return the reponse */
|
/* Return the response */
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,8 @@ pub struct VirtIOBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref QUEUE_FRAMES: UPIntrFreeCell<Vec<FrameTracker>> = unsafe { UPIntrFreeCell::new(Vec::new()) };
|
static ref QUEUE_FRAMES: UPIntrFreeCell<Vec<FrameTracker>> =
|
||||||
|
unsafe { UPIntrFreeCell::new(Vec::new()) };
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlockDevice for VirtIOBlock {
|
impl BlockDevice for VirtIOBlock {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
///! Ref: https://www.lammertbies.nl/comm/info/serial-uart
|
///! Ref: https://www.lammertbies.nl/comm/info/serial-uart
|
||||||
///! Ref: ns16550a datasheet: https://datasheetspdf.com/pdf-file/605590/NationalSemiconductor/NS16550A/1
|
///! Ref: ns16550a datasheet: https://datasheetspdf.com/pdf-file/605590/NationalSemiconductor/NS16550A/1
|
||||||
///! Ref: ns16450 datasheet: https://datasheetspdf.com/pdf-file/1311818/NationalSemiconductor/NS16450/1
|
///! Ref: ns16450 datasheet: https://datasheetspdf.com/pdf-file/1311818/NationalSemiconductor/NS16450/1
|
||||||
|
|
||||||
use super::CharDevice;
|
use super::CharDevice;
|
||||||
use crate::sync::{Condvar, UPIntrFreeCell};
|
use crate::sync::{Condvar, UPIntrFreeCell};
|
||||||
use crate::task::schedule;
|
use crate::task::schedule;
|
||||||
@ -12,7 +11,7 @@ use volatile::{ReadOnly, Volatile, WriteOnly};
|
|||||||
bitflags! {
|
bitflags! {
|
||||||
/// InterruptEnableRegister
|
/// InterruptEnableRegister
|
||||||
pub struct IER: u8 {
|
pub struct IER: u8 {
|
||||||
const RX_AVALIABLE = 1 << 0;
|
const RX_AVAILABLE = 1 << 0;
|
||||||
const TX_EMPTY = 1 << 1;
|
const TX_EMPTY = 1 << 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +94,7 @@ impl NS16550aRaw {
|
|||||||
mcr |= MCR::REQUEST_TO_SEND;
|
mcr |= MCR::REQUEST_TO_SEND;
|
||||||
mcr |= MCR::AUX_OUTPUT2;
|
mcr |= MCR::AUX_OUTPUT2;
|
||||||
read_end.mcr.write(mcr);
|
read_end.mcr.write(mcr);
|
||||||
let ier = IER::RX_AVALIABLE;
|
let ier = IER::RX_AVAILABLE;
|
||||||
read_end.ier.write(ier);
|
read_end.ier.write(ier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ fn panic(info: &PanicInfo) -> ! {
|
|||||||
unsafe {
|
unsafe {
|
||||||
backtrace();
|
backtrace();
|
||||||
}
|
}
|
||||||
shutdown()
|
shutdown(255)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn backtrace() {
|
unsafe fn backtrace() {
|
||||||
|
@ -46,7 +46,8 @@ use lazy_static::*;
|
|||||||
use sync::UPIntrFreeCell;
|
use sync::UPIntrFreeCell;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref DEV_NON_BLOCKING_ACCESS: UPIntrFreeCell<bool> = unsafe { UPIntrFreeCell::new(false) };
|
pub static ref DEV_NON_BLOCKING_ACCESS: UPIntrFreeCell<bool> =
|
||||||
|
unsafe { UPIntrFreeCell::new(false) };
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -16,7 +16,7 @@ const SBI_SHUTDOWN: usize = 8;
|
|||||||
fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
|
fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
|
||||||
let mut ret;
|
let mut ret;
|
||||||
unsafe {
|
unsafe {
|
||||||
asm!(
|
core::arch::asm!(
|
||||||
"ecall",
|
"ecall",
|
||||||
inlateout("x10") arg0 => ret,
|
inlateout("x10") arg0 => ret,
|
||||||
in("x11") arg1,
|
in("x11") arg1,
|
||||||
@ -39,7 +39,9 @@ pub fn console_getchar() -> usize {
|
|||||||
sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0)
|
sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shutdown() -> ! {
|
use crate::board::QEMUExit;
|
||||||
sbi_call(SBI_SHUTDOWN, 0, 0, 0);
|
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!");
|
panic!("It should shutdown!");
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
use crate::sync::{Mutex, UPIntrFreeCell};
|
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};
|
use alloc::{collections::VecDeque, sync::Arc};
|
||||||
|
|
||||||
pub struct Condvar {
|
pub struct Condvar {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use core::cell::{RefCell, RefMut, UnsafeCell};
|
use core::cell::{RefCell, RefMut, UnsafeCell};
|
||||||
use core::ops::{Deref, DerefMut};
|
use core::ops::{Deref, DerefMut};
|
||||||
use riscv::register::sstatus;
|
|
||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
|
use riscv::register::sstatus;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
/// Wrap a static data structure inside it so that we are
|
/// Wrap a static data structure inside it so that we are
|
||||||
@ -56,9 +56,8 @@ pub struct IntrMaskingInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref INTR_MASKING_INFO: UPSafeCellRaw<IntrMaskingInfo> = unsafe {
|
static ref INTR_MASKING_INFO: UPSafeCellRaw<IntrMaskingInfo> =
|
||||||
UPSafeCellRaw::new(IntrMaskingInfo::new())
|
unsafe { UPSafeCellRaw::new(IntrMaskingInfo::new()) };
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntrMaskingInfo {
|
impl IntrMaskingInfo {
|
||||||
@ -71,7 +70,9 @@ impl IntrMaskingInfo {
|
|||||||
|
|
||||||
pub fn enter(&mut self) {
|
pub fn enter(&mut self) {
|
||||||
let sie = sstatus::read().sie();
|
let sie = sstatus::read().sie();
|
||||||
unsafe { sstatus::clear_sie(); }
|
unsafe {
|
||||||
|
sstatus::clear_sie();
|
||||||
|
}
|
||||||
if self.nested_level == 0 {
|
if self.nested_level == 0 {
|
||||||
self.sie_before_masking = sie;
|
self.sie_before_masking = sie;
|
||||||
}
|
}
|
||||||
@ -81,7 +82,9 @@ impl IntrMaskingInfo {
|
|||||||
pub fn exit(&mut self) {
|
pub fn exit(&mut self) {
|
||||||
self.nested_level -= 1;
|
self.nested_level -= 1;
|
||||||
if self.nested_level == 0 && self.sie_before_masking {
|
if self.nested_level == 0 && self.sie_before_masking {
|
||||||
unsafe { sstatus::set_sie(); }
|
unsafe {
|
||||||
|
sstatus::set_sie();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,13 +104,17 @@ impl<T> UPIntrFreeCell<T> {
|
|||||||
inner: RefCell::new(value),
|
inner: RefCell::new(value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Panic if the data has been borrowed.
|
/// Panic if the data has been borrowed.
|
||||||
pub fn exclusive_access(&self) -> UPIntrRefMut<'_, T> {
|
pub fn exclusive_access(&self) -> UPIntrRefMut<'_, T> {
|
||||||
INTR_MASKING_INFO.get_mut().enter();
|
INTR_MASKING_INFO.get_mut().enter();
|
||||||
UPIntrRefMut(Some(self.inner.borrow_mut()))
|
UPIntrRefMut(Some(self.inner.borrow_mut()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exclusive_session<F, V>(&self, f: F) -> V where F: FnOnce(&mut T) -> V {
|
pub fn exclusive_session<F, V>(&self, f: F) -> V
|
||||||
|
where
|
||||||
|
F: FnOnce(&mut T) -> V,
|
||||||
|
{
|
||||||
let mut inner = self.exclusive_access();
|
let mut inner = self.exclusive_access();
|
||||||
f(inner.deref_mut())
|
f(inner.deref_mut())
|
||||||
}
|
}
|
||||||
@ -131,4 +138,3 @@ impl<'a, T> DerefMut for UPIntrRefMut<'a, T> {
|
|||||||
self.0.as_mut().unwrap().deref_mut()
|
self.0.as_mut().unwrap().deref_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +46,8 @@ lazy_static! {
|
|||||||
unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) };
|
unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const IDLE_PID: usize = 0;
|
||||||
|
|
||||||
pub struct PidHandle(pub usize);
|
pub struct PidHandle(pub usize);
|
||||||
|
|
||||||
pub fn pid_alloc() -> PidHandle {
|
pub fn pid_alloc() -> PidHandle {
|
||||||
|
@ -17,7 +17,7 @@ use process::ProcessControlBlock;
|
|||||||
use switch::__switch;
|
use switch::__switch;
|
||||||
|
|
||||||
pub use context::TaskContext;
|
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 manager::{add_task, pid2process, remove_from_pid2process};
|
||||||
pub use processor::{
|
pub use processor::{
|
||||||
current_kstack_top, current_process, current_task, current_trap_cx, current_trap_cx_user_va,
|
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() {
|
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);
|
schedule(task_cx_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use crate::board::QEMUExit;
|
||||||
|
|
||||||
pub fn exit_current_and_run_next(exit_code: i32) {
|
pub fn exit_current_and_run_next(exit_code: i32) {
|
||||||
let task = take_current_task().unwrap();
|
let task = take_current_task().unwrap();
|
||||||
let mut task_inner = task.inner_exclusive_access();
|
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
|
// however, if this is the main thread of current process
|
||||||
// the process should terminate at once
|
// the process should terminate at once
|
||||||
if tid == 0 {
|
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();
|
let mut process_inner = process.inner_exclusive_access();
|
||||||
// mark this process as a zombie process
|
// mark this process as a zombie process
|
||||||
process_inner.is_zombie = true;
|
process_inner.is_zombie = true;
|
||||||
|
@ -30,7 +30,8 @@ impl Processor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref PROCESSOR: UPIntrFreeCell<Processor> = unsafe { UPIntrFreeCell::new(Processor::new()) };
|
pub static ref PROCESSOR: UPIntrFreeCell<Processor> =
|
||||||
|
unsafe { UPIntrFreeCell::new(Processor::new()) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_tasks() {
|
pub fn run_tasks() {
|
||||||
@ -94,9 +95,8 @@ pub fn current_kstack_top() -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn schedule(switched_task_cx_ptr: *mut TaskContext) {
|
pub fn schedule(switched_task_cx_ptr: *mut TaskContext) {
|
||||||
let idle_task_cx_ptr = PROCESSOR.exclusive_session(|processor| {
|
let idle_task_cx_ptr =
|
||||||
processor.get_idle_task_cx_ptr()
|
PROCESSOR.exclusive_session(|processor| processor.get_idle_task_cx_ptr());
|
||||||
});
|
|
||||||
unsafe {
|
unsafe {
|
||||||
__switch(switched_task_cx_ptr, idle_task_cx_ptr);
|
__switch(switched_task_cx_ptr, idle_task_cx_ptr);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
use super::id::TaskUserRes;
|
use super::id::TaskUserRes;
|
||||||
use super::{kstack_alloc, KernelStack, ProcessControlBlock, TaskContext};
|
use super::{kstack_alloc, KernelStack, ProcessControlBlock, TaskContext};
|
||||||
use crate::trap::TrapContext;
|
use crate::trap::TrapContext;
|
||||||
use crate::{mm::PhysPageNum, sync::{UPIntrFreeCell, UPIntrRefMut}};
|
use crate::{
|
||||||
|
mm::PhysPageNum,
|
||||||
|
sync::{UPIntrFreeCell, UPIntrRefMut},
|
||||||
|
};
|
||||||
use alloc::sync::{Arc, Weak};
|
use alloc::sync::{Arc, Weak};
|
||||||
|
|
||||||
pub struct TaskControlBlock {
|
pub struct TaskControlBlock {
|
||||||
|
@ -11,7 +11,7 @@ use core::arch::{asm, global_asm};
|
|||||||
use riscv::register::{
|
use riscv::register::{
|
||||||
mtvec::TrapMode,
|
mtvec::TrapMode,
|
||||||
scause::{self, Exception, Interrupt, Trap},
|
scause::{self, Exception, Interrupt, Trap},
|
||||||
sie, stval, stvec, sstatus, sscratch,
|
sie, sscratch, sstatus, stval, stvec,
|
||||||
};
|
};
|
||||||
|
|
||||||
global_asm!(include_str!("trap.S"));
|
global_asm!(include_str!("trap.S"));
|
||||||
@ -23,7 +23,7 @@ pub fn init() {
|
|||||||
fn set_kernel_trap_entry() {
|
fn set_kernel_trap_entry() {
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn __alltraps();
|
fn __alltraps();
|
||||||
fn __alltraps_k();
|
fn __alltraps_k();
|
||||||
}
|
}
|
||||||
let __alltraps_k_va = __alltraps_k as usize - __alltraps as usize + TRAMPOLINE;
|
let __alltraps_k_va = __alltraps_k as usize - __alltraps as usize + TRAMPOLINE;
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -52,7 +52,7 @@ fn enable_supervisor_interrupt() {
|
|||||||
|
|
||||||
fn disable_supervisor_interrupt() {
|
fn disable_supervisor_interrupt() {
|
||||||
unsafe {
|
unsafe {
|
||||||
sstatus::clear_sie();
|
sstatus::clear_sie();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ pub fn trap_handler() -> ! {
|
|||||||
// jump to next instruction anyway
|
// jump to next instruction anyway
|
||||||
let mut cx = current_trap_cx();
|
let mut cx = current_trap_cx();
|
||||||
cx.sepc += 4;
|
cx.sepc += 4;
|
||||||
|
|
||||||
enable_supervisor_interrupt();
|
enable_supervisor_interrupt();
|
||||||
|
|
||||||
// get system call return value
|
// get system call return value
|
||||||
@ -150,19 +150,19 @@ pub fn trap_from_kernel(_trap_cx: &TrapContext) {
|
|||||||
match scause.cause() {
|
match scause.cause() {
|
||||||
Trap::Interrupt(Interrupt::SupervisorExternal) => {
|
Trap::Interrupt(Interrupt::SupervisorExternal) => {
|
||||||
crate::board::irq_handler();
|
crate::board::irq_handler();
|
||||||
},
|
}
|
||||||
Trap::Interrupt(Interrupt::SupervisorTimer) => {
|
Trap::Interrupt(Interrupt::SupervisorTimer) => {
|
||||||
set_next_trigger();
|
set_next_trigger();
|
||||||
check_timer();
|
check_timer();
|
||||||
// do not schedule now
|
// do not schedule now
|
||||||
},
|
}
|
||||||
_ => {
|
_ => {
|
||||||
panic!(
|
panic!(
|
||||||
"Unsupported trap from kernel: {:?}, stval = {:#x}!",
|
"Unsupported trap from kernel: {:?}, stval = {:#x}!",
|
||||||
scause.cause(),
|
scause.cause(),
|
||||||
stval
|
stval
|
||||||
);
|
);
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
setenv.sh
Normal file
2
setenv.sh
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export PATH=$(rustc --print sysroot)/bin:$PATH
|
||||||
|
export RUST_SRC_PATH=$(rustc --print sysroot)/lib/rustlib/src/rust/library/
|
@ -8,9 +8,15 @@ BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS))
|
|||||||
|
|
||||||
OBJDUMP := rust-objdump --arch-name=riscv64
|
OBJDUMP := rust-objdump --arch-name=riscv64
|
||||||
OBJCOPY := rust-objcopy --binary-architecture=riscv64
|
OBJCOPY := rust-objcopy --binary-architecture=riscv64
|
||||||
|
CP := cp
|
||||||
|
|
||||||
|
TEST ?=
|
||||||
|
|
||||||
elf: $(APPS)
|
elf: $(APPS)
|
||||||
@cargo build --release
|
@cargo build --release
|
||||||
|
ifeq ($(TEST), 1)
|
||||||
|
@$(CP) $(TARGET_DIR)/usertests $(TARGET_DIR)/initproc
|
||||||
|
endif
|
||||||
|
|
||||||
binary: elf
|
binary: elf
|
||||||
$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));)
|
$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));)
|
||||||
|
@ -9,10 +9,14 @@ use user_lib::{close, open, read, OpenFlags};
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
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);
|
assert!(argc == 2);
|
||||||
let fd = open(argv[1], OpenFlags::RDONLY);
|
let fd = open(argv[1], OpenFlags::RDONLY);
|
||||||
if fd == -1 {
|
if fd == -1 {
|
||||||
panic!("Error occured when opening file");
|
panic!("Error occurred when opening file");
|
||||||
}
|
}
|
||||||
let fd = fd as usize;
|
let fd = fd as usize;
|
||||||
let mut buf = [0u8; 256];
|
let mut buf = [0u8; 256];
|
||||||
|
133
user/src/bin/eisenberg.rs
Normal file
133
user/src/bin/eisenberg.rs
Normal file
@ -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<usize> = 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
|
||||||
|
}
|
@ -21,6 +21,7 @@ fn fork_child(cur: &str, branch: char) {
|
|||||||
yield_();
|
yield_();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fork_tree(cur: &str) {
|
fn fork_tree(cur: &str) {
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
extern crate alloc;
|
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::{exit, thread_create, waittid};
|
||||||
use user_lib::{close, get_time, open, write, OpenFlags, gettid};
|
|
||||||
|
|
||||||
fn worker(size_kib: usize) {
|
fn worker(size_kib: usize) {
|
||||||
let mut buffer = [0u8; 1024]; // 1KiB
|
let mut buffer = [0u8; 1024]; // 1KiB
|
||||||
@ -17,11 +17,11 @@ fn worker(size_kib: usize) {
|
|||||||
let filename = format(format_args!("testf{}\0", gettid()));
|
let filename = format(format_args!("testf{}\0", gettid()));
|
||||||
let f = open(filename.as_str(), OpenFlags::CREATE | OpenFlags::WRONLY);
|
let f = open(filename.as_str(), OpenFlags::CREATE | OpenFlags::WRONLY);
|
||||||
if f < 0 {
|
if f < 0 {
|
||||||
panic!("Open test file failed!");
|
panic!("Open test file failed!");
|
||||||
}
|
}
|
||||||
let f = f as usize;
|
let f = f as usize;
|
||||||
for _ in 0..size_kib {
|
for _ in 0..size_kib {
|
||||||
write(f, &buffer);
|
write(f, &buffer);
|
||||||
}
|
}
|
||||||
close(f);
|
close(f);
|
||||||
exit(0)
|
exit(0)
|
||||||
@ -34,18 +34,18 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
|||||||
let size_kb = size_mb << 10;
|
let size_kb = size_mb << 10;
|
||||||
let workers = argv[1].parse::<usize>().expect("wrong argument");
|
let workers = argv[1].parse::<usize>().expect("wrong argument");
|
||||||
assert!(workers >= 1 && size_kb % workers == 0, "wrong argument");
|
assert!(workers >= 1 && size_kb % workers == 0, "wrong argument");
|
||||||
|
|
||||||
let start = get_time();
|
let start = get_time();
|
||||||
|
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
let size_mb = 1usize;
|
let size_mb = 1usize;
|
||||||
for _ in 0..workers {
|
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() {
|
for tid in v.iter() {
|
||||||
assert_eq!(0, waittid(*tid as usize));
|
assert_eq!(0, waittid(*tid as usize));
|
||||||
}
|
}
|
||||||
|
|
||||||
let time_ms = (get_time() - start) as usize;
|
let time_ms = (get_time() - start) as usize;
|
||||||
let speed_kbs = size_kb * 1000 / time_ms;
|
let speed_kbs = size_kb * 1000 / time_ms;
|
||||||
println!(
|
println!(
|
||||||
|
78
user/src/bin/peterson.rs
Normal file
78
user/src/bin/peterson.rs
Normal file
@ -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
|
||||||
|
}
|
@ -34,7 +34,10 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
|||||||
} else if argc == 2 {
|
} else if argc == 2 {
|
||||||
count = argv[1].to_string().parse::<usize>().unwrap();
|
count = argv[1].to_string().parse::<usize>().unwrap();
|
||||||
} else {
|
} 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);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ use user_lib::{exec, fork, wait};
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn main() -> i32 {
|
pub fn main() -> i32 {
|
||||||
for i in 0..1000 {
|
for i in 0..50 {
|
||||||
if fork() == 0 {
|
if fork() == 0 {
|
||||||
exec("pipe_large_test\0", &[core::ptr::null::<u8>()]);
|
exec("pipe_large_test\0", &[core::ptr::null::<u8>()]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -4,40 +4,134 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
|
|
||||||
static TESTS: &[&str] = &[
|
// not in SUCC_TESTS & FAIL_TESTS
|
||||||
"exit\0",
|
// count_lines, infloop, user_shell, usertests
|
||||||
"fantastic_text\0",
|
|
||||||
"forktest\0",
|
// item of TESTS : app_name(argv_0), argv_1, argv_2, argv_3, exit_code
|
||||||
"forktest2\0",
|
static SUCC_TESTS: &[(&str, &str, &str, &str, i32)] = &[
|
||||||
"forktest_simple\0",
|
("filetest_simple\0", "\0", "\0", "\0", 0),
|
||||||
"hello_world\0",
|
("cat\0", "filea\0", "\0", "\0", 0),
|
||||||
"matrix\0",
|
("cmdline_args\0", "1\0", "2\0", "3\0", 0),
|
||||||
"sleep\0",
|
("eisenberg\0", "\0", "\0", "\0", 0),
|
||||||
"sleep_simple\0",
|
("exit\0", "\0", "\0", "\0", 0),
|
||||||
"stack_overflow\0",
|
("fantastic_text\0", "\0", "\0", "\0", 0),
|
||||||
"yield\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};
|
use user_lib::{exec, fork, waitpid};
|
||||||
|
|
||||||
#[no_mangle]
|
fn run_tests(tests: &[(&str, &str, &str, &str, i32)]) -> i32 {
|
||||||
pub fn main() -> i32 {
|
let mut pass_num = 0;
|
||||||
for test in TESTS {
|
let mut arr: [*const u8; 4] = [
|
||||||
println!("Usertests: Running {}", test);
|
core::ptr::null::<u8>(),
|
||||||
|
core::ptr::null::<u8>(),
|
||||||
|
core::ptr::null::<u8>(),
|
||||||
|
core::ptr::null::<u8>(),
|
||||||
|
];
|
||||||
|
|
||||||
|
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::<u8>();
|
||||||
|
arr[3] = core::ptr::null::<u8>();
|
||||||
|
if test.2 != "\0" {
|
||||||
|
arr[2] = test.2.as_ptr();
|
||||||
|
arr[3] = core::ptr::null::<u8>();
|
||||||
|
if test.3 != "\0" {
|
||||||
|
arr[3] = test.3.as_ptr();
|
||||||
|
} else {
|
||||||
|
arr[3] = core::ptr::null::<u8>();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
arr[2] = core::ptr::null::<u8>();
|
||||||
|
arr[3] = core::ptr::null::<u8>();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
arr[1] = core::ptr::null::<u8>();
|
||||||
|
arr[2] = core::ptr::null::<u8>();
|
||||||
|
arr[3] = core::ptr::null::<u8>();
|
||||||
|
}
|
||||||
|
|
||||||
let pid = fork();
|
let pid = fork();
|
||||||
if pid == 0 {
|
if pid == 0 {
|
||||||
exec(*test, &[core::ptr::null::<u8>()]);
|
exec(test.0, &arr[..]);
|
||||||
panic!("unreachable!");
|
panic!("unreachable!");
|
||||||
} else {
|
} else {
|
||||||
let mut exit_code: i32 = Default::default();
|
let mut exit_code: i32 = Default::default();
|
||||||
let wait_pid = waitpid(pid as usize, &mut exit_code);
|
let wait_pid = waitpid(pid as usize, &mut exit_code);
|
||||||
assert_eq!(pid, wait_pid);
|
assert_eq!(pid, wait_pid);
|
||||||
|
if exit_code == test.4 {
|
||||||
|
// summary apps with exit_code
|
||||||
|
pass_num = pass_num + 1;
|
||||||
|
}
|
||||||
println!(
|
println!(
|
||||||
"\x1b[32mUsertests: Test {} in Process {} exited with code {}\x1b[0m",
|
"\x1b[32mUsertests: Test {} in Process {} exited with code {}\x1b[0m",
|
||||||
test, pid, exit_code
|
test.0, pid, exit_code
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("Usertests passed!");
|
pass_num
|
||||||
0
|
}
|
||||||
|
|
||||||
|
#[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;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#![feature(linkage)]
|
#![feature(linkage)]
|
||||||
#![feature(panic_info_message)]
|
#![feature(panic_info_message)]
|
||||||
#![feature(alloc_error_handler)]
|
#![feature(alloc_error_handler)]
|
||||||
|
#![feature(core_intrinsics)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod console;
|
pub mod console;
|
||||||
@ -197,3 +198,24 @@ pub fn condvar_signal(condvar_id: usize) {
|
|||||||
pub fn condvar_wait(condvar_id: usize, mutex_id: usize) {
|
pub fn condvar_wait(condvar_id: usize, mutex_id: usize) {
|
||||||
sys_condvar_wait(condvar_id, mutex_id);
|
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)
|
||||||
|
};
|
||||||
|
}
|
@ -1,5 +1,3 @@
|
|||||||
use core::arch::asm;
|
|
||||||
|
|
||||||
const SYSCALL_DUP: usize = 24;
|
const SYSCALL_DUP: usize = 24;
|
||||||
const SYSCALL_OPEN: usize = 56;
|
const SYSCALL_OPEN: usize = 56;
|
||||||
const SYSCALL_CLOSE: usize = 57;
|
const SYSCALL_CLOSE: usize = 57;
|
||||||
@ -31,7 +29,7 @@ const SYSCALL_CONDVAR_WAIT: usize = 1032;
|
|||||||
fn syscall(id: usize, args: [usize; 3]) -> isize {
|
fn syscall(id: usize, args: [usize; 3]) -> isize {
|
||||||
let mut ret: isize;
|
let mut ret: isize;
|
||||||
unsafe {
|
unsafe {
|
||||||
asm!(
|
core::arch::asm!(
|
||||||
"ecall",
|
"ecall",
|
||||||
inlateout("x10") args[0] => ret,
|
inlateout("x10") args[0] => ret,
|
||||||
in("x11") args[1],
|
in("x11") args[1],
|
||||||
|
Loading…
Reference in New Issue
Block a user