Compare commits
177 Commits
ch7-signal
...
main
Author | SHA1 | Date |
---|---|---|
|
21bae8b034 | 3 years ago |
|
a773a977e7 | 3 years ago |
|
d52256baa9 | 3 years ago |
|
7c83dd49e7 | 3 years ago |
|
9a95c7dcb4 | 3 years ago |
|
cc10cd10eb | 3 years ago |
|
b732da242d | 3 years ago |
|
ca997784ab | 3 years ago |
|
a85cb98743 | 3 years ago |
|
31a2873a61 | 3 years ago |
|
1371aab236 | 3 years ago |
|
e615ae4483 | 3 years ago |
|
18d4a89ab7 | 3 years ago |
|
c234d1417b | 3 years ago |
|
cf00c3ae5d | 3 years ago |
|
87cd106b67 | 3 years ago |
|
3d5e9ed9e6 | 3 years ago |
|
2bd092dd64 | 3 years ago |
|
07675fcd00 | 3 years ago |
|
c9a288c110 | 3 years ago |
|
8e73480c99 | 3 years ago |
|
8657b656e5 | 3 years ago |
|
1afa429e93 | 3 years ago |
|
964445e59c | 3 years ago |
|
df36cbe657 | 3 years ago |
|
9d2690f8d9 | 3 years ago |
|
a8e4c13e99 | 3 years ago |
|
a70a983497 | 3 years ago |
|
ca1d7a07b8 | 3 years ago |
|
3b7c4b1cdc | 3 years ago |
|
91c29d0b70 | 3 years ago |
|
a61d34e2b1 | 3 years ago |
|
858334bc02 | 3 years ago |
|
b6978bf6ec | 3 years ago |
|
aec0a6ebe5 | 3 years ago |
|
34e1a3def8 | 3 years ago |
|
d6a3de2510 | 3 years ago |
|
3a44decf58 | 3 years ago |
|
923638023d | 3 years ago |
|
c009012d85 | 3 years ago |
|
21d57c1396 | 3 years ago |
|
334d868a5c | 3 years ago |
|
fbe8e39b38 | 3 years ago |
|
53034d7c33 | 3 years ago |
|
d3bd19867c | 3 years ago |
|
6ef566faac | 3 years ago |
|
f0f7f6fcaa | 3 years ago |
|
89b9d7c161 | 3 years ago |
|
e6b6251979 | 3 years ago |
|
61152471b7 | 3 years ago |
|
be2ed8fa37 | 3 years ago |
|
6f09af2c0f | 3 years ago |
|
48bdefe6b8 | 3 years ago |
|
29d6d26644 | 3 years ago |
|
1c55663478 | 3 years ago |
|
740730e7f7 | 3 years ago |
|
f65451bc72 | 3 years ago |
|
e8686526bb | 3 years ago |
|
514c110a28 | 3 years ago |
|
40f37501af | 3 years ago |
|
24e530935f | 3 years ago |
|
c358424fae | 4 years ago |
|
2ac621972c | 4 years ago |
|
aa104ecd54 | 4 years ago |
|
d81560a492 | 4 years ago |
|
cba8d9e6d8 | 4 years ago |
|
72d6b38fca | 4 years ago |
|
cf6d905ac4 | 4 years ago |
|
abd9d361e4 | 4 years ago |
|
045c47e4ef | 4 years ago |
|
97fdd4f2a2 | 4 years ago |
|
22db3123d8 | 4 years ago |
|
f6b210adbe | 4 years ago |
|
760615095e | 4 years ago |
|
06243fac76 | 4 years ago |
|
713e78ea91 | 4 years ago |
|
1c0bbf4404 | 4 years ago |
|
90796450fe | 4 years ago |
|
4ad64e83f6 | 4 years ago |
|
3dc6d9c97c | 4 years ago |
|
b77b108a28 | 4 years ago |
|
1baf177f9e | 4 years ago |
|
a3f9b5fea9 | 4 years ago |
|
11a389592a | 4 years ago |
|
7caf43bbbf | 4 years ago |
|
bb5dca2158 | 4 years ago |
|
3d2909e990 | 4 years ago |
|
2041a7c0d4 | 4 years ago |
|
cd6754a7df | 4 years ago |
|
5753a09366 | 4 years ago |
|
aedd7f5612 | 4 years ago |
|
b851f8d743 | 4 years ago |
|
8adfc90db9 | 4 years ago |
|
7225254d8a | 4 years ago |
|
b0fad5aca3 | 4 years ago |
|
43c6b7cf01 | 4 years ago |
|
136e26ae6c | 4 years ago |
|
24b3c82b8a | 4 years ago |
|
1493ec9459 | 4 years ago |
|
c599a31dd0 | 4 years ago |
|
6693de9611 | 4 years ago |
|
26bbec6320 | 4 years ago |
|
91a758d657 | 4 years ago |
|
03ea339e58 | 4 years ago |
|
940073e9f3 | 4 years ago |
|
03151ac124 | 4 years ago |
|
46900cc9af | 4 years ago |
|
cf9218113f | 4 years ago |
|
1590923666 | 4 years ago |
|
aaed6006aa | 4 years ago |
|
95431917ed | 4 years ago |
|
b5f7fb6c45 | 4 years ago |
|
f04110e6e3 | 4 years ago |
|
a7b981b14d | 4 years ago |
|
3a6e4e38f2 | 4 years ago |
|
81559f70b9 | 4 years ago |
|
0a8bd2c3fd | 4 years ago |
|
e7f120bab3 | 4 years ago |
|
7a36cdb77f | 4 years ago |
|
f7ed29756c | 4 years ago |
|
bfa6a80732 | 4 years ago |
|
a57d470edc | 4 years ago |
|
91d4d6d40c | 4 years ago |
|
60477da9be | 4 years ago |
|
a1cda4aa59 | 4 years ago |
|
d302a0d616 | 4 years ago |
|
3554e20dc6 | 4 years ago |
|
818363f2ca | 4 years ago |
|
613f77c5a4 | 4 years ago |
|
6298f57a87 | 4 years ago |
|
1e2e83e886 | 4 years ago |
|
a43dbc4e34 | 4 years ago |
|
dd2be93ef0 | 4 years ago |
|
e55c5200c5 | 4 years ago |
|
67372ac84d | 4 years ago |
|
cfa3819bee | 4 years ago |
|
4668911483 | 4 years ago |
|
9196963e44 | 4 years ago |
|
3f5308f46c | 4 years ago |
|
e0a3933b1c | 4 years ago |
|
35cc3d6e2f | 4 years ago |
|
1346fb1a1f | 4 years ago |
|
12c6c53af5 | 4 years ago |
|
e8a0682cf8 | 4 years ago |
|
8b27976d23 | 4 years ago |
|
84a55c17c4 | 4 years ago |
|
c6a262d215 | 4 years ago |
|
00084f5165 | 4 years ago |
|
87743bac4d | 4 years ago |
|
a5c4f3228e | 4 years ago |
|
1b6f2c4c1e | 4 years ago |
|
caac1beb0a | 4 years ago |
|
d97b0a20ab | 4 years ago |
|
ea4222bed0 | 4 years ago |
|
b28d94b226 | 4 years ago |
|
0d9024b5bd | 4 years ago |
|
b659f10d22 | 4 years ago |
|
29d02e7442 | 4 years ago |
|
4b6bd7deaa | 4 years ago |
|
dddd04b683 | 4 years ago |
|
c01b3289c6 | 4 years ago |
|
eb5ef8e956 | 4 years ago |
|
5afed009c0 | 4 years ago |
|
07467287da | 4 years ago |
|
654f6eb959 | 4 years ago |
|
18da8a3879 | 4 years ago |
|
6267c5d922 | 4 years ago |
|
9772373743 | 5 years ago |
|
c14392ec60 | 5 years ago |
|
c763a3be96 | 5 years ago |
|
c65ce846ce | 5 years ago |
|
e9453d7834 | 5 years ago |
|
05d34106ce | 5 years ago |
|
1a7261d86d | 5 years ago |
|
12747d71b4 | 5 years ago |
|
b674fd5a77 | 5 years ago |
|
fdab87d2ed | 5 years ago |
@ -1,25 +0,0 @@
|
||||
name: Build Rust Doc
|
||||
|
||||
on: [push]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
build-doc:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Build doc
|
||||
run: |
|
||||
rustup target add riscv64gc-unknown-none-elf
|
||||
rustup component add llvm-tools-preview
|
||||
rustup component add rust-src
|
||||
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 }}
|
@ -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
|
@ -1,18 +1,13 @@
|
||||
.idea
|
||||
Cargo.lock
|
||||
target
|
||||
.*/*
|
||||
!.github/*
|
||||
!.vscode/settings.json
|
||||
|
||||
**/target/
|
||||
**/Cargo.lock
|
||||
|
||||
os/src/link_app.S
|
||||
os/src/linker.ld
|
||||
os/last-*
|
||||
os/Cargo.lock
|
||||
os/.gdb_history
|
||||
user/build
|
||||
user/target/*
|
||||
user/.idea/*
|
||||
user/Cargo.lock
|
||||
easy-fs/Cargo.lock
|
||||
easy-fs/target/*
|
||||
easy-fs-fuse/Cargo.lock
|
||||
easy-fs-fuse/target/*
|
||||
tools/
|
||||
pushall.sh
|
||||
|
@ -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
|
||||
}
|
Binary file not shown.
@ -1,3 +0,0 @@
|
||||
.idea/
|
||||
target/
|
||||
Cargo.lock
|
@ -1,6 +1,126 @@
|
||||
pub const CLOCK_FREQ: usize = 12500000;
|
||||
|
||||
pub const MMIO: &[(usize, usize)] = &[(0x10001000, 0x1000)];
|
||||
pub const MMIO: &[(usize, usize)] = &[
|
||||
(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;
|
||||
pub type CharDeviceImpl = crate::drivers::chardev::NS16550a<VIRT_UART>;
|
||||
|
||||
pub const VIRT_PLIC: usize = 0xC00_0000;
|
||||
pub const VIRT_UART: usize = 0x1000_0000;
|
||||
|
||||
use crate::drivers::block::BLOCK_DEVICE;
|
||||
use crate::drivers::chardev::{CharDevice, UART};
|
||||
use crate::drivers::plic::{IntrTargetPriority, PLIC};
|
||||
|
||||
pub fn device_init() {
|
||||
use riscv::register::sie;
|
||||
let mut plic = unsafe { PLIC::new(VIRT_PLIC) };
|
||||
let hart_id: usize = 0;
|
||||
let supervisor = IntrTargetPriority::Supervisor;
|
||||
let machine = IntrTargetPriority::Machine;
|
||||
plic.set_threshold(hart_id, supervisor, 0);
|
||||
plic.set_threshold(hart_id, machine, 1);
|
||||
for intr_src_id in [1usize, 10] {
|
||||
plic.enable(hart_id, supervisor, intr_src_id);
|
||||
plic.set_priority(intr_src_id, 1);
|
||||
}
|
||||
unsafe {
|
||||
sie::set_sext();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn irq_handler() {
|
||||
let mut plic = unsafe { PLIC::new(VIRT_PLIC) };
|
||||
let intr_src_id = plic.claim(0, IntrTargetPriority::Supervisor);
|
||||
match intr_src_id {
|
||||
1 => BLOCK_DEVICE.handle_irq(),
|
||||
10 => UART.handle_irq(),
|
||||
_ => panic!("unsupported IRQ {}", 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);
|
||||
|
@ -0,0 +1,17 @@
|
||||
mod ns16550a;
|
||||
|
||||
pub use ns16550a::NS16550a;
|
||||
|
||||
use crate::board::CharDeviceImpl;
|
||||
use alloc::sync::Arc;
|
||||
use lazy_static::*;
|
||||
|
||||
pub trait CharDevice {
|
||||
fn read(&self) -> u8;
|
||||
fn write(&self, ch: u8);
|
||||
fn handle_irq(&self);
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref UART: Arc<CharDeviceImpl> = Arc::new(CharDeviceImpl::new());
|
||||
}
|
@ -0,0 +1,175 @@
|
||||
///! 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;
|
||||
use alloc::collections::VecDeque;
|
||||
use bitflags::*;
|
||||
use volatile::{ReadOnly, Volatile, WriteOnly};
|
||||
|
||||
bitflags! {
|
||||
/// InterruptEnableRegister
|
||||
pub struct IER: u8 {
|
||||
const RX_AVAILABLE = 1 << 0;
|
||||
const TX_EMPTY = 1 << 1;
|
||||
}
|
||||
|
||||
/// LineStatusRegister
|
||||
pub struct LSR: u8 {
|
||||
const DATA_AVAILABLE = 1 << 0;
|
||||
const THR_EMPTY = 1 << 5;
|
||||
}
|
||||
|
||||
/// Model Control Register
|
||||
pub struct MCR: u8 {
|
||||
const DATA_TERMINAL_READY = 1 << 0;
|
||||
const REQUEST_TO_SEND = 1 << 1;
|
||||
const AUX_OUTPUT1 = 1 << 2;
|
||||
const AUX_OUTPUT2 = 1 << 3;
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[allow(dead_code)]
|
||||
struct ReadWithoutDLAB {
|
||||
/// receiver buffer register
|
||||
pub rbr: ReadOnly<u8>,
|
||||
/// interrupt enable register
|
||||
pub ier: Volatile<IER>,
|
||||
/// interrupt identification register
|
||||
pub iir: ReadOnly<u8>,
|
||||
/// line control register
|
||||
pub lcr: Volatile<u8>,
|
||||
/// model control register
|
||||
pub mcr: Volatile<MCR>,
|
||||
/// line status register
|
||||
pub lsr: ReadOnly<LSR>,
|
||||
/// ignore MSR
|
||||
_padding1: ReadOnly<u8>,
|
||||
/// ignore SCR
|
||||
_padding2: ReadOnly<u8>,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[allow(dead_code)]
|
||||
struct WriteWithoutDLAB {
|
||||
/// transmitter holding register
|
||||
pub thr: WriteOnly<u8>,
|
||||
/// interrupt enable register
|
||||
pub ier: Volatile<IER>,
|
||||
/// ignore FCR
|
||||
_padding0: ReadOnly<u8>,
|
||||
/// line control register
|
||||
pub lcr: Volatile<u8>,
|
||||
/// modem control register
|
||||
pub mcr: Volatile<MCR>,
|
||||
/// line status register
|
||||
pub lsr: ReadOnly<LSR>,
|
||||
/// ignore other registers
|
||||
_padding1: ReadOnly<u16>,
|
||||
}
|
||||
|
||||
pub struct NS16550aRaw {
|
||||
base_addr: usize,
|
||||
}
|
||||
|
||||
impl NS16550aRaw {
|
||||
fn read_end(&mut self) -> &mut ReadWithoutDLAB {
|
||||
unsafe { &mut *(self.base_addr as *mut ReadWithoutDLAB) }
|
||||
}
|
||||
|
||||
fn write_end(&mut self) -> &mut WriteWithoutDLAB {
|
||||
unsafe { &mut *(self.base_addr as *mut WriteWithoutDLAB) }
|
||||
}
|
||||
|
||||
pub fn new(base_addr: usize) -> Self {
|
||||
Self { base_addr }
|
||||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
let read_end = self.read_end();
|
||||
let mut mcr = MCR::empty();
|
||||
mcr |= MCR::DATA_TERMINAL_READY;
|
||||
mcr |= MCR::REQUEST_TO_SEND;
|
||||
mcr |= MCR::AUX_OUTPUT2;
|
||||
read_end.mcr.write(mcr);
|
||||
let ier = IER::RX_AVAILABLE;
|
||||
read_end.ier.write(ier);
|
||||
}
|
||||
|
||||
pub fn read(&mut self) -> Option<u8> {
|
||||
let read_end = self.read_end();
|
||||
let lsr = read_end.lsr.read();
|
||||
if lsr.contains(LSR::DATA_AVAILABLE) {
|
||||
Some(read_end.rbr.read())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(&mut self, ch: u8) {
|
||||
let write_end = self.write_end();
|
||||
loop {
|
||||
if write_end.lsr.read().contains(LSR::THR_EMPTY) {
|
||||
write_end.thr.write(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NS16550aInner {
|
||||
ns16550a: NS16550aRaw,
|
||||
read_buffer: VecDeque<u8>,
|
||||
}
|
||||
|
||||
pub struct NS16550a<const BASE_ADDR: usize> {
|
||||
inner: UPIntrFreeCell<NS16550aInner>,
|
||||
condvar: Condvar,
|
||||
}
|
||||
|
||||
impl<const BASE_ADDR: usize> NS16550a<BASE_ADDR> {
|
||||
pub fn new() -> Self {
|
||||
let mut inner = NS16550aInner {
|
||||
ns16550a: NS16550aRaw::new(BASE_ADDR),
|
||||
read_buffer: VecDeque::new(),
|
||||
};
|
||||
inner.ns16550a.init();
|
||||
Self {
|
||||
inner: unsafe { UPIntrFreeCell::new(inner) },
|
||||
condvar: Condvar::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const BASE_ADDR: usize> CharDevice for NS16550a<BASE_ADDR> {
|
||||
fn read(&self) -> u8 {
|
||||
loop {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
if let Some(ch) = inner.read_buffer.pop_front() {
|
||||
return ch;
|
||||
} else {
|
||||
let task_cx_ptr = self.condvar.wait_no_sched();
|
||||
drop(inner);
|
||||
schedule(task_cx_ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn write(&self, ch: u8) {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
inner.ns16550a.write(ch);
|
||||
}
|
||||
fn handle_irq(&self) {
|
||||
let mut count = 0;
|
||||
self.inner.exclusive_session(|inner| {
|
||||
while let Some(ch) = inner.ns16550a.read() {
|
||||
count += 1;
|
||||
inner.read_buffer.push_back(ch);
|
||||
}
|
||||
});
|
||||
if count > 0 {
|
||||
self.condvar.signal();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
pub mod block;
|
||||
pub mod chardev;
|
||||
pub mod plic;
|
||||
|
||||
pub use block::BLOCK_DEVICE;
|
||||
pub use chardev::UART;
|
||||
|
@ -0,0 +1,124 @@
|
||||
#[allow(clippy::upper_case_acronyms)]
|
||||
pub struct PLIC {
|
||||
base_addr: usize,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum IntrTargetPriority {
|
||||
Machine = 0,
|
||||
Supervisor = 1,
|
||||
}
|
||||
|
||||
impl IntrTargetPriority {
|
||||
pub fn supported_number() -> usize {
|
||||
2
|
||||
}
|
||||
}
|
||||
|
||||
impl PLIC {
|
||||
fn priority_ptr(&self, intr_source_id: usize) -> *mut u32 {
|
||||
assert!(intr_source_id > 0 && intr_source_id <= 132);
|
||||
(self.base_addr + intr_source_id * 4) as *mut u32
|
||||
}
|
||||
fn hart_id_with_priority(hart_id: usize, target_priority: IntrTargetPriority) -> usize {
|
||||
let priority_num = IntrTargetPriority::supported_number();
|
||||
hart_id * priority_num + target_priority as usize
|
||||
}
|
||||
fn enable_ptr(
|
||||
&self,
|
||||
hart_id: usize,
|
||||
target_priority: IntrTargetPriority,
|
||||
intr_source_id: usize,
|
||||
) -> (*mut u32, usize) {
|
||||
let id = Self::hart_id_with_priority(hart_id, target_priority);
|
||||
let (reg_id, reg_shift) = (intr_source_id / 32, intr_source_id % 32);
|
||||
(
|
||||
(self.base_addr + 0x2000 + 0x80 * id + 0x4 * reg_id) as *mut u32,
|
||||
reg_shift,
|
||||
)
|
||||
}
|
||||
fn threshold_ptr_of_hart_with_priority(
|
||||
&self,
|
||||
hart_id: usize,
|
||||
target_priority: IntrTargetPriority,
|
||||
) -> *mut u32 {
|
||||
let id = Self::hart_id_with_priority(hart_id, target_priority);
|
||||
(self.base_addr + 0x20_0000 + 0x1000 * id) as *mut u32
|
||||
}
|
||||
fn claim_comp_ptr_of_hart_with_priority(
|
||||
&self,
|
||||
hart_id: usize,
|
||||
target_priority: IntrTargetPriority,
|
||||
) -> *mut u32 {
|
||||
let id = Self::hart_id_with_priority(hart_id, target_priority);
|
||||
(self.base_addr + 0x20_0004 + 0x1000 * id) as *mut u32
|
||||
}
|
||||
pub unsafe fn new(base_addr: usize) -> Self {
|
||||
Self { base_addr }
|
||||
}
|
||||
pub fn set_priority(&mut self, intr_source_id: usize, priority: u32) {
|
||||
assert!(priority < 8);
|
||||
unsafe {
|
||||
self.priority_ptr(intr_source_id).write_volatile(priority);
|
||||
}
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn get_priority(&mut self, intr_source_id: usize) -> u32 {
|
||||
unsafe { self.priority_ptr(intr_source_id).read_volatile() & 7 }
|
||||
}
|
||||
pub fn enable(
|
||||
&mut self,
|
||||
hart_id: usize,
|
||||
target_priority: IntrTargetPriority,
|
||||
intr_source_id: usize,
|
||||
) {
|
||||
let (reg_ptr, shift) = self.enable_ptr(hart_id, target_priority, intr_source_id);
|
||||
unsafe {
|
||||
reg_ptr.write_volatile(reg_ptr.read_volatile() | 1 << shift);
|
||||
}
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn disable(
|
||||
&mut self,
|
||||
hart_id: usize,
|
||||
target_priority: IntrTargetPriority,
|
||||
intr_source_id: usize,
|
||||
) {
|
||||
let (reg_ptr, shift) = self.enable_ptr(hart_id, target_priority, intr_source_id);
|
||||
unsafe {
|
||||
reg_ptr.write_volatile(reg_ptr.read_volatile() & (!(1u32 << shift)));
|
||||
}
|
||||
}
|
||||
pub fn set_threshold(
|
||||
&mut self,
|
||||
hart_id: usize,
|
||||
target_priority: IntrTargetPriority,
|
||||
threshold: u32,
|
||||
) {
|
||||
assert!(threshold < 8);
|
||||
let threshold_ptr = self.threshold_ptr_of_hart_with_priority(hart_id, target_priority);
|
||||
unsafe {
|
||||
threshold_ptr.write_volatile(threshold);
|
||||
}
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn get_threshold(&mut self, hart_id: usize, target_priority: IntrTargetPriority) -> u32 {
|
||||
let threshold_ptr = self.threshold_ptr_of_hart_with_priority(hart_id, target_priority);
|
||||
unsafe { threshold_ptr.read_volatile() & 7 }
|
||||
}
|
||||
pub fn claim(&mut self, hart_id: usize, target_priority: IntrTargetPriority) -> u32 {
|
||||
let claim_comp_ptr = self.claim_comp_ptr_of_hart_with_priority(hart_id, target_priority);
|
||||
unsafe { claim_comp_ptr.read_volatile() }
|
||||
}
|
||||
pub fn complete(
|
||||
&mut self,
|
||||
hart_id: usize,
|
||||
target_priority: IntrTargetPriority,
|
||||
completion: u32,
|
||||
) {
|
||||
let claim_comp_ptr = self.claim_comp_ptr_of_hart_with_priority(hart_id, target_priority);
|
||||
unsafe {
|
||||
claim_comp_ptr.write_volatile(completion);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
use crate::sync::{Mutex, UPIntrFreeCell};
|
||||
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 {
|
||||
pub inner: UPIntrFreeCell<CondvarInner>,
|
||||
}
|
||||
|
||||
pub struct CondvarInner {
|
||||
pub wait_queue: VecDeque<Arc<TaskControlBlock>>,
|
||||
}
|
||||
|
||||
impl Condvar {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: unsafe {
|
||||
UPIntrFreeCell::new(CondvarInner {
|
||||
wait_queue: VecDeque::new(),
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn signal(&self) {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
if let Some(task) = inner.wait_queue.pop_front() {
|
||||
add_task(task);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn wait(&self) {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
inner.wait_queue.push_back(current_task().unwrap());
|
||||
drop(inner);
|
||||
block_current_and_run_next();
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn wait_no_sched(&self) -> *mut TaskContext {
|
||||
self.inner.exclusive_session(|inner| {
|
||||
inner.wait_queue.push_back(current_task().unwrap());
|
||||
});
|
||||
block_current_task()
|
||||
}
|
||||
|
||||
pub fn wait_with_mutex(&self, mutex: Arc<dyn Mutex>) {
|
||||
mutex.unlock();
|
||||
self.inner.exclusive_session(|inner| {
|
||||
inner.wait_queue.push_back(current_task().unwrap());
|
||||
});
|
||||
block_current_and_run_next();
|
||||
mutex.lock();
|
||||
}
|
||||
}
|
@ -1,3 +1,9 @@
|
||||
mod condvar;
|
||||
mod mutex;
|
||||
mod semaphore;
|
||||
mod up;
|
||||
|
||||
pub use up::UPSafeCell;
|
||||
pub use condvar::Condvar;
|
||||
pub use mutex::{Mutex, MutexBlocking, MutexSpin};
|
||||
pub use semaphore::Semaphore;
|
||||
pub use up::{UPIntrFreeCell, UPIntrRefMut};
|
||||
|
@ -0,0 +1,88 @@
|
||||
use super::UPIntrFreeCell;
|
||||
use crate::task::TaskControlBlock;
|
||||
use crate::task::{add_task, current_task};
|
||||
use crate::task::{block_current_and_run_next, suspend_current_and_run_next};
|
||||
use alloc::{collections::VecDeque, sync::Arc};
|
||||
|
||||
pub trait Mutex: Sync + Send {
|
||||
fn lock(&self);
|
||||
fn unlock(&self);
|
||||
}
|
||||
|
||||
pub struct MutexSpin {
|
||||
locked: UPIntrFreeCell<bool>,
|
||||
}
|
||||
|
||||
impl MutexSpin {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
locked: unsafe { UPIntrFreeCell::new(false) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mutex for MutexSpin {
|
||||
fn lock(&self) {
|
||||
loop {
|
||||
let mut locked = self.locked.exclusive_access();
|
||||
if *locked {
|
||||
drop(locked);
|
||||
suspend_current_and_run_next();
|
||||
continue;
|
||||
} else {
|
||||
*locked = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn unlock(&self) {
|
||||
let mut locked = self.locked.exclusive_access();
|
||||
*locked = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MutexBlocking {
|
||||
inner: UPIntrFreeCell<MutexBlockingInner>,
|
||||
}
|
||||
|
||||
pub struct MutexBlockingInner {
|
||||
locked: bool,
|
||||
wait_queue: VecDeque<Arc<TaskControlBlock>>,
|
||||
}
|
||||
|
||||
impl MutexBlocking {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: unsafe {
|
||||
UPIntrFreeCell::new(MutexBlockingInner {
|
||||
locked: false,
|
||||
wait_queue: VecDeque::new(),
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mutex for MutexBlocking {
|
||||
fn lock(&self) {
|
||||
let mut mutex_inner = self.inner.exclusive_access();
|
||||
if mutex_inner.locked {
|
||||
mutex_inner.wait_queue.push_back(current_task().unwrap());
|
||||
drop(mutex_inner);
|
||||
block_current_and_run_next();
|
||||
} else {
|
||||
mutex_inner.locked = true;
|
||||
}
|
||||
}
|
||||
|
||||
fn unlock(&self) {
|
||||
let mut mutex_inner = self.inner.exclusive_access();
|
||||
assert!(mutex_inner.locked);
|
||||
if let Some(waking_task) = mutex_inner.wait_queue.pop_front() {
|
||||
add_task(waking_task);
|
||||
} else {
|
||||
mutex_inner.locked = false;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
use crate::sync::UPIntrFreeCell;
|
||||
use crate::task::{add_task, block_current_and_run_next, current_task, TaskControlBlock};
|
||||
use alloc::{collections::VecDeque, sync::Arc};
|
||||
|
||||
pub struct Semaphore {
|
||||
pub inner: UPIntrFreeCell<SemaphoreInner>,
|
||||
}
|
||||
|
||||
pub struct SemaphoreInner {
|
||||
pub count: isize,
|
||||
pub wait_queue: VecDeque<Arc<TaskControlBlock>>,
|
||||
}
|
||||
|
||||
impl Semaphore {
|
||||
pub fn new(res_count: usize) -> Self {
|
||||
Self {
|
||||
inner: unsafe {
|
||||
UPIntrFreeCell::new(SemaphoreInner {
|
||||
count: res_count as isize,
|
||||
wait_queue: VecDeque::new(),
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn up(&self) {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
inner.count += 1;
|
||||
if inner.count <= 0 {
|
||||
if let Some(task) = inner.wait_queue.pop_front() {
|
||||
add_task(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn down(&self) {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
inner.count -= 1;
|
||||
if inner.count < 0 {
|
||||
inner.wait_queue.push_back(current_task().unwrap());
|
||||
drop(inner);
|
||||
block_current_and_run_next();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
use crate::sync::{Condvar, Mutex, MutexBlocking, MutexSpin, Semaphore};
|
||||
use crate::task::{block_current_and_run_next, current_process, current_task};
|
||||
use crate::timer::{add_timer, get_time_ms};
|
||||
use alloc::sync::Arc;
|
||||
|
||||
pub fn sys_sleep(ms: usize) -> isize {
|
||||
let expire_ms = get_time_ms() + ms;
|
||||
let task = current_task().unwrap();
|
||||
add_timer(expire_ms, task);
|
||||
block_current_and_run_next();
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_mutex_create(blocking: bool) -> isize {
|
||||
let process = current_process();
|
||||
let mutex: Option<Arc<dyn Mutex>> = if !blocking {
|
||||
Some(Arc::new(MutexSpin::new()))
|
||||
} else {
|
||||
Some(Arc::new(MutexBlocking::new()))
|
||||
};
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
if let Some(id) = process_inner
|
||||
.mutex_list
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, item)| item.is_none())
|
||||
.map(|(id, _)| id)
|
||||
{
|
||||
process_inner.mutex_list[id] = mutex;
|
||||
id as isize
|
||||
} else {
|
||||
process_inner.mutex_list.push(mutex);
|
||||
process_inner.mutex_list.len() as isize - 1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sys_mutex_lock(mutex_id: usize) -> isize {
|
||||
let process = current_process();
|
||||
let process_inner = process.inner_exclusive_access();
|
||||
let mutex = Arc::clone(process_inner.mutex_list[mutex_id].as_ref().unwrap());
|
||||
drop(process_inner);
|
||||
drop(process);
|
||||
mutex.lock();
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_mutex_unlock(mutex_id: usize) -> isize {
|
||||
let process = current_process();
|
||||
let process_inner = process.inner_exclusive_access();
|
||||
let mutex = Arc::clone(process_inner.mutex_list[mutex_id].as_ref().unwrap());
|
||||
drop(process_inner);
|
||||
drop(process);
|
||||
mutex.unlock();
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_semaphore_create(res_count: usize) -> isize {
|
||||
let process = current_process();
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
let id = if let Some(id) = process_inner
|
||||
.semaphore_list
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, item)| item.is_none())
|
||||
.map(|(id, _)| id)
|
||||
{
|
||||
process_inner.semaphore_list[id] = Some(Arc::new(Semaphore::new(res_count)));
|
||||
id
|
||||
} else {
|
||||
process_inner
|
||||
.semaphore_list
|
||||
.push(Some(Arc::new(Semaphore::new(res_count))));
|
||||
process_inner.semaphore_list.len() - 1
|
||||
};
|
||||
id as isize
|
||||
}
|
||||
|
||||
pub fn sys_semaphore_up(sem_id: usize) -> isize {
|
||||
let process = current_process();
|
||||
let process_inner = process.inner_exclusive_access();
|
||||
let sem = Arc::clone(process_inner.semaphore_list[sem_id].as_ref().unwrap());
|
||||
drop(process_inner);
|
||||
sem.up();
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_semaphore_down(sem_id: usize) -> isize {
|
||||
let process = current_process();
|
||||
let process_inner = process.inner_exclusive_access();
|
||||
let sem = Arc::clone(process_inner.semaphore_list[sem_id].as_ref().unwrap());
|
||||
drop(process_inner);
|
||||
sem.down();
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_condvar_create(_arg: usize) -> isize {
|
||||
let process = current_process();
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
let id = if let Some(id) = process_inner
|
||||
.condvar_list
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, item)| item.is_none())
|
||||
.map(|(id, _)| id)
|
||||
{
|
||||
process_inner.condvar_list[id] = Some(Arc::new(Condvar::new()));
|
||||
id
|
||||
} else {
|
||||
process_inner
|
||||
.condvar_list
|
||||
.push(Some(Arc::new(Condvar::new())));
|
||||
process_inner.condvar_list.len() - 1
|
||||
};
|
||||
id as isize
|
||||
}
|
||||
|
||||
pub fn sys_condvar_signal(condvar_id: usize) -> isize {
|
||||
let process = current_process();
|
||||
let process_inner = process.inner_exclusive_access();
|
||||
let condvar = Arc::clone(process_inner.condvar_list[condvar_id].as_ref().unwrap());
|
||||
drop(process_inner);
|
||||
condvar.signal();
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_condvar_wait(condvar_id: usize, mutex_id: usize) -> isize {
|
||||
let process = current_process();
|
||||
let process_inner = process.inner_exclusive_access();
|
||||
let condvar = Arc::clone(process_inner.condvar_list[condvar_id].as_ref().unwrap());
|
||||
let mutex = Arc::clone(process_inner.mutex_list[mutex_id].as_ref().unwrap());
|
||||
drop(process_inner);
|
||||
condvar.wait_with_mutex(mutex);
|
||||
0
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
use crate::{
|
||||
mm::kernel_token,
|
||||
task::{add_task, current_task, TaskControlBlock},
|
||||
trap::{trap_handler, TrapContext},
|
||||
};
|
||||
use alloc::sync::Arc;
|
||||
|
||||
pub fn sys_thread_create(entry: usize, arg: usize) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
let process = task.process.upgrade().unwrap();
|
||||
// create a new thread
|
||||
let new_task = Arc::new(TaskControlBlock::new(
|
||||
Arc::clone(&process),
|
||||
task.inner_exclusive_access()
|
||||
.res
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.ustack_base,
|
||||
true,
|
||||
));
|
||||
// add new task to scheduler
|
||||
add_task(Arc::clone(&new_task));
|
||||
let new_task_inner = new_task.inner_exclusive_access();
|
||||
let new_task_res = new_task_inner.res.as_ref().unwrap();
|
||||
let new_task_tid = new_task_res.tid;
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
// add new thread to current process
|
||||
let tasks = &mut process_inner.tasks;
|
||||
while tasks.len() < new_task_tid + 1 {
|
||||
tasks.push(None);
|
||||
}
|
||||
tasks[new_task_tid] = Some(Arc::clone(&new_task));
|
||||
let new_task_trap_cx = new_task_inner.get_trap_cx();
|
||||
*new_task_trap_cx = TrapContext::app_init_context(
|
||||
entry,
|
||||
new_task_res.ustack_top(),
|
||||
kernel_token(),
|
||||
new_task.kstack.get_top(),
|
||||
trap_handler as usize,
|
||||
);
|
||||
(*new_task_trap_cx).x[10] = arg;
|
||||
new_task_tid as isize
|
||||
}
|
||||
|
||||
pub fn sys_gettid() -> isize {
|
||||
current_task()
|
||||
.unwrap()
|
||||
.inner_exclusive_access()
|
||||
.res
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.tid as isize
|
||||
}
|
||||
|
||||
/// thread does not exist, return -1
|
||||
/// thread has not exited yet, return -2
|
||||
/// otherwise, return thread's exit code
|
||||
pub fn sys_waittid(tid: usize) -> i32 {
|
||||
let task = current_task().unwrap();
|
||||
let process = task.process.upgrade().unwrap();
|
||||
let task_inner = task.inner_exclusive_access();
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
// a thread cannot wait for itself
|
||||
if task_inner.res.as_ref().unwrap().tid == tid {
|
||||
return -1;
|
||||
}
|
||||
let mut exit_code: Option<i32> = None;
|
||||
let waited_task = process_inner.tasks[tid].as_ref();
|
||||
if let Some(waited_task) = waited_task {
|
||||
if let Some(waited_exit_code) = waited_task.inner_exclusive_access().exit_code {
|
||||
exit_code = Some(waited_exit_code);
|
||||
}
|
||||
} else {
|
||||
// waited thread does not exist
|
||||
return -1;
|
||||
}
|
||||
if let Some(exit_code) = exit_code {
|
||||
// dealloc the exited thread
|
||||
process_inner.tasks[tid] = None;
|
||||
exit_code
|
||||
} else {
|
||||
// waited thread has not exited
|
||||
-2
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
use crate::task::{SignalFlags, MAX_SIG};
|
||||
|
||||
|
||||
/// Action for a signal
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct SignalAction {
|
||||
pub handler: usize,
|
||||
pub mask: SignalFlags
|
||||
}
|
||||
|
||||
impl Default for SignalAction {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
handler: 0,
|
||||
mask: SignalFlags::from_bits(40).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SignalActions {
|
||||
pub table: [SignalAction; MAX_SIG + 1],
|
||||
}
|
||||
|
||||
impl Default for SignalActions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
table: [SignalAction::default(); MAX_SIG + 1],
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,225 @@
|
||||
use super::ProcessControlBlock;
|
||||
use crate::config::{KERNEL_STACK_SIZE, PAGE_SIZE, TRAMPOLINE, TRAP_CONTEXT_BASE, USER_STACK_SIZE};
|
||||
use crate::mm::{MapPermission, PhysPageNum, VirtAddr, KERNEL_SPACE};
|
||||
use crate::sync::UPIntrFreeCell;
|
||||
use alloc::{
|
||||
sync::{Arc, Weak},
|
||||
vec::Vec,
|
||||
};
|
||||
use lazy_static::*;
|
||||
|
||||
pub struct RecycleAllocator {
|
||||
current: usize,
|
||||
recycled: Vec<usize>,
|
||||
}
|
||||
|
||||
impl RecycleAllocator {
|
||||
pub fn new() -> Self {
|
||||
RecycleAllocator {
|
||||
current: 0,
|
||||
recycled: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn alloc(&mut self) -> usize {
|
||||
if let Some(id) = self.recycled.pop() {
|
||||
id
|
||||
} else {
|
||||
self.current += 1;
|
||||
self.current - 1
|
||||
}
|
||||
}
|
||||
pub fn dealloc(&mut self, id: usize) {
|
||||
assert!(id < self.current);
|
||||
assert!(
|
||||
!self.recycled.iter().any(|i| *i == id),
|
||||
"id {} has been deallocated!",
|
||||
id
|
||||
);
|
||||
self.recycled.push(id);
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref PID_ALLOCATOR: UPIntrFreeCell<RecycleAllocator> =
|
||||
unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) };
|
||||
static ref KSTACK_ALLOCATOR: UPIntrFreeCell<RecycleAllocator> =
|
||||
unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) };
|
||||
}
|
||||
|
||||
pub const IDLE_PID: usize = 0;
|
||||
|
||||
pub struct PidHandle(pub usize);
|
||||
|
||||
pub fn pid_alloc() -> PidHandle {
|
||||
PidHandle(PID_ALLOCATOR.exclusive_access().alloc())
|
||||
}
|
||||
|
||||
impl Drop for PidHandle {
|
||||
fn drop(&mut self) {
|
||||
PID_ALLOCATOR.exclusive_access().dealloc(self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Return (bottom, top) of a kernel stack in kernel space.
|
||||
pub fn kernel_stack_position(kstack_id: usize) -> (usize, usize) {
|
||||
let top = TRAMPOLINE - kstack_id * (KERNEL_STACK_SIZE + PAGE_SIZE);
|
||||
let bottom = top - KERNEL_STACK_SIZE;
|
||||
(bottom, top)
|
||||
}
|
||||
|
||||
pub struct KernelStack(pub usize);
|
||||
|
||||
pub fn kstack_alloc() -> KernelStack {
|
||||
let kstack_id = KSTACK_ALLOCATOR.exclusive_access().alloc();
|
||||
let (kstack_bottom, kstack_top) = kernel_stack_position(kstack_id);
|
||||
KERNEL_SPACE.exclusive_access().insert_framed_area(
|
||||
kstack_bottom.into(),
|
||||
kstack_top.into(),
|
||||
MapPermission::R | MapPermission::W,
|
||||
);
|
||||
KernelStack(kstack_id)
|
||||
}
|
||||
|
||||
impl Drop for KernelStack {
|
||||
fn drop(&mut self) {
|
||||
let (kernel_stack_bottom, _) = kernel_stack_position(self.0);
|
||||
let kernel_stack_bottom_va: VirtAddr = kernel_stack_bottom.into();
|
||||
KERNEL_SPACE
|
||||
.exclusive_access()
|
||||
.remove_area_with_start_vpn(kernel_stack_bottom_va.into());
|
||||
}
|
||||
}
|
||||
|
||||
impl KernelStack {
|
||||
#[allow(unused)]
|
||||
pub fn push_on_top<T>(&self, value: T) -> *mut T
|
||||
where
|
||||
T: Sized,
|
||||
{
|
||||
let kernel_stack_top = self.get_top();
|
||||
let ptr_mut = (kernel_stack_top - core::mem::size_of::<T>()) as *mut T;
|
||||
unsafe {
|
||||
*ptr_mut = value;
|
||||
}
|
||||
ptr_mut
|
||||
}
|
||||
pub fn get_top(&self) -> usize {
|
||||
let (_, kernel_stack_top) = kernel_stack_position(self.0);
|
||||
kernel_stack_top
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TaskUserRes {
|
||||
pub tid: usize,
|
||||
pub ustack_base: usize,
|
||||
pub process: Weak<ProcessControlBlock>,
|
||||
}
|
||||
|
||||
fn trap_cx_bottom_from_tid(tid: usize) -> usize {
|
||||
TRAP_CONTEXT_BASE - tid * PAGE_SIZE
|
||||
}
|
||||
|
||||
fn ustack_bottom_from_tid(ustack_base: usize, tid: usize) -> usize {
|
||||
ustack_base + tid * (PAGE_SIZE + USER_STACK_SIZE)
|
||||
}
|
||||
|
||||
impl TaskUserRes {
|
||||
pub fn new(
|
||||
process: Arc<ProcessControlBlock>,
|
||||
ustack_base: usize,
|
||||
alloc_user_res: bool,
|
||||
) -> Self {
|
||||
let tid = process.inner_exclusive_access().alloc_tid();
|
||||
let task_user_res = Self {
|
||||
tid,
|
||||
ustack_base,
|
||||
process: Arc::downgrade(&process),
|
||||
};
|
||||
if alloc_user_res {
|
||||
task_user_res.alloc_user_res();
|
||||
}
|
||||
task_user_res
|
||||
}
|
||||
|
||||
pub fn alloc_user_res(&self) {
|
||||
let process = self.process.upgrade().unwrap();
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
// alloc user stack
|
||||
let ustack_bottom = ustack_bottom_from_tid(self.ustack_base, self.tid);
|
||||
let ustack_top = ustack_bottom + USER_STACK_SIZE;
|
||||
process_inner.memory_set.insert_framed_area(
|
||||
ustack_bottom.into(),
|
||||
ustack_top.into(),
|
||||
MapPermission::R | MapPermission::W | MapPermission::U,
|
||||
);
|
||||
// alloc trap_cx
|
||||
let trap_cx_bottom = trap_cx_bottom_from_tid(self.tid);
|
||||
let trap_cx_top = trap_cx_bottom + PAGE_SIZE;
|
||||
process_inner.memory_set.insert_framed_area(
|
||||
trap_cx_bottom.into(),
|
||||
trap_cx_top.into(),
|
||||
MapPermission::R | MapPermission::W,
|
||||
);
|
||||
}
|
||||
|
||||
fn dealloc_user_res(&self) {
|
||||
// dealloc tid
|
||||
let process = self.process.upgrade().unwrap();
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
// dealloc ustack manually
|
||||
let ustack_bottom_va: VirtAddr = ustack_bottom_from_tid(self.ustack_base, self.tid).into();
|
||||
process_inner
|
||||
.memory_set
|
||||
.remove_area_with_start_vpn(ustack_bottom_va.into());
|
||||
// dealloc trap_cx manually
|
||||
let trap_cx_bottom_va: VirtAddr = trap_cx_bottom_from_tid(self.tid).into();
|
||||
process_inner
|
||||
.memory_set
|
||||
.remove_area_with_start_vpn(trap_cx_bottom_va.into());
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn alloc_tid(&mut self) {
|
||||
self.tid = self
|
||||
.process
|
||||
.upgrade()
|
||||
.unwrap()
|
||||
.inner_exclusive_access()
|
||||
.alloc_tid();
|
||||
}
|
||||
|
||||
pub fn dealloc_tid(&self) {
|
||||
let process = self.process.upgrade().unwrap();
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
process_inner.dealloc_tid(self.tid);
|
||||
}
|
||||
|
||||
pub fn trap_cx_user_va(&self) -> usize {
|
||||
trap_cx_bottom_from_tid(self.tid)
|
||||
}
|
||||
|
||||
pub fn trap_cx_ppn(&self) -> PhysPageNum {
|
||||
let process = self.process.upgrade().unwrap();
|
||||
let process_inner = process.inner_exclusive_access();
|
||||
let trap_cx_bottom_va: VirtAddr = trap_cx_bottom_from_tid(self.tid).into();
|
||||
process_inner
|
||||
.memory_set
|
||||
.translate(trap_cx_bottom_va.into())
|
||||
.unwrap()
|
||||
.ppn()
|
||||
}
|
||||
|
||||
pub fn ustack_base(&self) -> usize {
|
||||
self.ustack_base
|
||||
}
|
||||
pub fn ustack_top(&self) -> usize {
|
||||
ustack_bottom_from_tid(self.ustack_base, self.tid) + USER_STACK_SIZE
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TaskUserRes {
|
||||
fn drop(&mut self) {
|
||||
self.dealloc_tid();
|
||||
self.dealloc_user_res();
|
||||
}
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
use crate::config::{KERNEL_STACK_SIZE, PAGE_SIZE, TRAMPOLINE};
|
||||
use crate::mm::{MapPermission, VirtAddr, KERNEL_SPACE};
|
||||
use crate::sync::UPSafeCell;
|
||||
use alloc::vec::Vec;
|
||||
use lazy_static::*;
|
||||
|
||||
struct PidAllocator {
|
||||
current: usize,
|
||||
recycled: Vec<usize>,
|
||||
}
|
||||
|
||||
impl PidAllocator {
|
||||
pub fn new() -> Self {
|
||||
PidAllocator {
|
||||
current: 0,
|
||||
recycled: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn alloc(&mut self) -> PidHandle {
|
||||
if let Some(pid) = self.recycled.pop() {
|
||||
PidHandle(pid)
|
||||
} else {
|
||||
self.current += 1;
|
||||
PidHandle(self.current - 1)
|
||||
}
|
||||
}
|
||||
pub fn dealloc(&mut self, pid: usize) {
|
||||
assert!(pid < self.current);
|
||||
assert!(
|
||||
!self.recycled.iter().any(|ppid| *ppid == pid),
|
||||
"pid {} has been deallocated!",
|
||||
pid
|
||||
);
|
||||
self.recycled.push(pid);
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref PID_ALLOCATOR: UPSafeCell<PidAllocator> =
|
||||
unsafe { UPSafeCell::new(PidAllocator::new()) };
|
||||
}
|
||||
|
||||
pub struct PidHandle(pub usize);
|
||||
|
||||
impl Drop for PidHandle {
|
||||
fn drop(&mut self) {
|
||||
//println!("drop pid {}", self.0);
|
||||
PID_ALLOCATOR.exclusive_access().dealloc(self.0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pid_alloc() -> PidHandle {
|
||||
PID_ALLOCATOR.exclusive_access().alloc()
|
||||
}
|
||||
|
||||
/// Return (bottom, top) of a kernel stack in kernel space.
|
||||
pub fn kernel_stack_position(app_id: usize) -> (usize, usize) {
|
||||
let top = TRAMPOLINE - app_id * (KERNEL_STACK_SIZE + PAGE_SIZE);
|
||||
let bottom = top - KERNEL_STACK_SIZE;
|
||||
(bottom, top)
|
||||
}
|
||||
|
||||
pub struct KernelStack {
|
||||
pid: usize,
|
||||
}
|
||||
|
||||
impl KernelStack {
|
||||
pub fn new(pid_handle: &PidHandle) -> Self {
|
||||
let pid = pid_handle.0;
|
||||
let (kernel_stack_bottom, kernel_stack_top) = kernel_stack_position(pid);
|
||||
KERNEL_SPACE.exclusive_access().insert_framed_area(
|
||||
kernel_stack_bottom.into(),
|
||||
kernel_stack_top.into(),
|
||||
MapPermission::R | MapPermission::W,
|
||||
);
|
||||
KernelStack { pid: pid_handle.0 }
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn push_on_top<T>(&self, value: T) -> *mut T
|
||||
where
|
||||
T: Sized,
|
||||
{
|
||||
let kernel_stack_top = self.get_top();
|
||||
let ptr_mut = (kernel_stack_top - core::mem::size_of::<T>()) as *mut T;
|
||||
unsafe {
|
||||
*ptr_mut = value;
|
||||
}
|
||||
ptr_mut
|
||||
}
|
||||
pub fn get_top(&self) -> usize {
|
||||
let (_, kernel_stack_top) = kernel_stack_position(self.pid);
|
||||
kernel_stack_top
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for KernelStack {
|
||||
fn drop(&mut self) {
|
||||
let (kernel_stack_bottom, _) = kernel_stack_position(self.pid);
|
||||
let kernel_stack_bottom_va: VirtAddr = kernel_stack_bottom.into();
|
||||
KERNEL_SPACE
|
||||
.exclusive_access()
|
||||
.remove_area_with_start_vpn(kernel_stack_bottom_va.into());
|
||||
}
|
||||
}
|
@ -0,0 +1,258 @@
|
||||
use super::id::RecycleAllocator;
|
||||
use super::manager::insert_into_pid2process;
|
||||
use super::TaskControlBlock;
|
||||
use super::{add_task, SignalFlags};
|
||||
use super::{pid_alloc, PidHandle};
|
||||
use crate::fs::{File, Stdin, Stdout};
|
||||
use crate::mm::{translated_refmut, MemorySet, KERNEL_SPACE};
|
||||
use crate::sync::{Condvar, Mutex, Semaphore, UPIntrFreeCell, UPIntrRefMut};
|
||||
use crate::trap::{trap_handler, TrapContext};
|
||||
use alloc::string::String;
|
||||
use alloc::sync::{Arc, Weak};
|
||||
use alloc::vec;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
pub struct ProcessControlBlock {
|
||||
// immutable
|
||||
pub pid: PidHandle,
|
||||
// mutable
|
||||
inner: UPIntrFreeCell<ProcessControlBlockInner>,
|
||||
}
|
||||
|
||||
pub struct ProcessControlBlockInner {
|
||||
pub is_zombie: bool,
|
||||
pub memory_set: MemorySet,
|
||||
pub parent: Option<Weak<ProcessControlBlock>>,
|
||||
pub children: Vec<Arc<ProcessControlBlock>>,
|
||||
pub exit_code: i32,
|
||||
pub fd_table: Vec<Option<Arc<dyn File + Send + Sync>>>,
|
||||
pub signals: SignalFlags,
|
||||
pub tasks: Vec<Option<Arc<TaskControlBlock>>>,
|
||||
pub task_res_allocator: RecycleAllocator,
|
||||
pub mutex_list: Vec<Option<Arc<dyn Mutex>>>,
|
||||
pub semaphore_list: Vec<Option<Arc<Semaphore>>>,
|
||||
pub condvar_list: Vec<Option<Arc<Condvar>>>,
|
||||
}
|
||||
|
||||
impl ProcessControlBlockInner {
|
||||
#[allow(unused)]
|
||||
pub fn get_user_token(&self) -> usize {
|
||||
self.memory_set.token()
|
||||
}
|
||||
|
||||
pub fn alloc_fd(&mut self) -> usize {
|
||||
if let Some(fd) = (0..self.fd_table.len()).find(|fd| self.fd_table[*fd].is_none()) {
|
||||
fd
|
||||
} else {
|
||||
self.fd_table.push(None);
|
||||
self.fd_table.len() - 1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn alloc_tid(&mut self) -> usize {
|
||||
self.task_res_allocator.alloc()
|
||||
}
|
||||
|
||||
pub fn dealloc_tid(&mut self, tid: usize) {
|
||||
self.task_res_allocator.dealloc(tid)
|
||||
}
|
||||
|
||||
pub fn thread_count(&self) -> usize {
|
||||
self.tasks.len()
|
||||
}
|
||||
|
||||
pub fn get_task(&self, tid: usize) -> Arc<TaskControlBlock> {
|
||||
self.tasks[tid].as_ref().unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl ProcessControlBlock {
|
||||
pub fn inner_exclusive_access(&self) -> UPIntrRefMut<'_, ProcessControlBlockInner> {
|
||||
self.inner.exclusive_access()
|
||||
}
|
||||
|
||||
pub fn new(elf_data: &[u8]) -> Arc<Self> {
|
||||
// memory_set with elf program headers/trampoline/trap context/user stack
|
||||
let (memory_set, ustack_base, entry_point) = MemorySet::from_elf(elf_data);
|
||||
// allocate a pid
|
||||
let pid_handle = pid_alloc();
|
||||
let process = Arc::new(Self {
|
||||
pid: pid_handle,
|
||||
inner: unsafe {
|
||||
UPIntrFreeCell::new(ProcessControlBlockInner {
|
||||
is_zombie: false,
|
||||
memory_set,
|
||||
parent: None,
|
||||
children: Vec::new(),
|
||||
exit_code: 0,
|
||||
fd_table: vec![
|
||||
// 0 -> stdin
|
||||
Some(Arc::new(Stdin)),
|
||||
// 1 -> stdout
|
||||
Some(Arc::new(Stdout)),
|
||||
// 2 -> stderr
|
||||
Some(Arc::new(Stdout)),
|
||||
],
|
||||
signals: SignalFlags::empty(),
|
||||
tasks: Vec::new(),
|
||||
task_res_allocator: RecycleAllocator::new(),
|
||||
mutex_list: Vec::new(),
|
||||
semaphore_list: Vec::new(),
|
||||
condvar_list: Vec::new(),
|
||||
})
|
||||
},
|
||||
});
|
||||
// create a main thread, we should allocate ustack and trap_cx here
|
||||
let task = Arc::new(TaskControlBlock::new(
|
||||
Arc::clone(&process),
|
||||
ustack_base,
|
||||
true,
|
||||
));
|
||||
// prepare trap_cx of main thread
|
||||
let task_inner = task.inner_exclusive_access();
|
||||
let trap_cx = task_inner.get_trap_cx();
|
||||
let ustack_top = task_inner.res.as_ref().unwrap().ustack_top();
|
||||
let kstack_top = task.kstack.get_top();
|
||||
drop(task_inner);
|
||||
*trap_cx = TrapContext::app_init_context(
|
||||
entry_point,
|
||||
ustack_top,
|
||||
KERNEL_SPACE.exclusive_access().token(),
|
||||
kstack_top,
|
||||
trap_handler as usize,
|
||||
);
|
||||
// add main thread to the process
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
process_inner.tasks.push(Some(Arc::clone(&task)));
|
||||
drop(process_inner);
|
||||
insert_into_pid2process(process.getpid(), Arc::clone(&process));
|
||||
// add main thread to scheduler
|
||||
add_task(task);
|
||||
process
|
||||
}
|
||||
|
||||
/// Only support processes with a single thread.
|
||||
pub fn exec(self: &Arc<Self>, elf_data: &[u8], args: Vec<String>) {
|
||||
assert_eq!(self.inner_exclusive_access().thread_count(), 1);
|
||||
// memory_set with elf program headers/trampoline/trap context/user stack
|
||||
let (memory_set, ustack_base, entry_point) = MemorySet::from_elf(elf_data);
|
||||
let new_token = memory_set.token();
|
||||
// substitute memory_set
|
||||
self.inner_exclusive_access().memory_set = memory_set;
|
||||
// then we alloc user resource for main thread again
|
||||
// since memory_set has been changed
|
||||
let task = self.inner_exclusive_access().get_task(0);
|
||||
let mut task_inner = task.inner_exclusive_access();
|
||||
task_inner.res.as_mut().unwrap().ustack_base = ustack_base;
|
||||
task_inner.res.as_mut().unwrap().alloc_user_res();
|
||||
task_inner.trap_cx_ppn = task_inner.res.as_mut().unwrap().trap_cx_ppn();
|
||||
// push arguments on user stack
|
||||
let mut user_sp = task_inner.res.as_mut().unwrap().ustack_top();
|
||||
user_sp -= (args.len() + 1) * core::mem::size_of::<usize>();
|
||||
let argv_base = user_sp;
|
||||
let mut argv: Vec<_> = (0..=args.len())
|
||||
.map(|arg| {
|
||||
translated_refmut(
|
||||
new_token,
|
||||
(argv_base + arg * core::mem::size_of::<usize>()) as *mut usize,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
*argv[args.len()] = 0;
|
||||
for i in 0..args.len() {
|
||||
user_sp -= args[i].len() + 1;
|
||||
*argv[i] = user_sp;
|
||||
let mut p = user_sp;
|
||||
for c in args[i].as_bytes() {
|
||||
*translated_refmut(new_token, p as *mut u8) = *c;
|
||||
p += 1;
|
||||
}
|
||||
*translated_refmut(new_token, p as *mut u8) = 0;
|
||||
}
|
||||
// make the user_sp aligned to 8B for k210 platform
|
||||
user_sp -= user_sp % core::mem::size_of::<usize>();
|
||||
// initialize trap_cx
|
||||
let mut trap_cx = TrapContext::app_init_context(
|
||||
entry_point,
|
||||
user_sp,
|
||||
KERNEL_SPACE.exclusive_access().token(),
|
||||
task.kstack.get_top(),
|
||||
trap_handler as usize,
|
||||
);
|
||||
trap_cx.x[10] = args.len();
|
||||
trap_cx.x[11] = argv_base;
|
||||
*task_inner.get_trap_cx() = trap_cx;
|
||||
}
|
||||
|
||||
/// Only support processes with a single thread.
|
||||
pub fn fork(self: &Arc<Self>) -> Arc<Self> {
|
||||
let mut parent = self.inner_exclusive_access();
|
||||
assert_eq!(parent.thread_count(), 1);
|
||||
// clone parent's memory_set completely including trampoline/ustacks/trap_cxs
|
||||
let memory_set = MemorySet::from_existed_user(&parent.memory_set);
|
||||
// alloc a pid
|
||||
let pid = pid_alloc();
|
||||
// copy fd table
|
||||
let mut new_fd_table: Vec<Option<Arc<dyn File + Send + Sync>>> = Vec::new();
|
||||
for fd in parent.fd_table.iter() {
|
||||
if let Some(file) = fd {
|
||||
new_fd_table.push(Some(file.clone()));
|
||||
} else {
|
||||
new_fd_table.push(None);
|
||||
}
|
||||
}
|
||||
// create child process pcb
|
||||
let child = Arc::new(Self {
|
||||
pid,
|
||||
inner: unsafe {
|
||||
UPIntrFreeCell::new(ProcessControlBlockInner {
|
||||
is_zombie: false,
|
||||
memory_set,
|
||||
parent: Some(Arc::downgrade(self)),
|
||||
children: Vec::new(),
|
||||
exit_code: 0,
|
||||
fd_table: new_fd_table,
|
||||
signals: SignalFlags::empty(),
|
||||
tasks: Vec::new(),
|
||||
task_res_allocator: RecycleAllocator::new(),
|
||||
mutex_list: Vec::new(),
|
||||
semaphore_list: Vec::new(),
|
||||
condvar_list: Vec::new(),
|
||||
})
|
||||
},
|
||||
});
|
||||
// add child
|
||||
parent.children.push(Arc::clone(&child));
|
||||
// create main thread of child process
|
||||
let task = Arc::new(TaskControlBlock::new(
|
||||
Arc::clone(&child),
|
||||
parent
|
||||
.get_task(0)
|
||||
.inner_exclusive_access()
|
||||
.res
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.ustack_base(),
|
||||
// here we do not allocate trap_cx or ustack again
|
||||
// but mention that we allocate a new kstack here
|
||||
false,
|
||||
));
|
||||
// attach task to child process
|
||||
let mut child_inner = child.inner_exclusive_access();
|
||||
child_inner.tasks.push(Some(Arc::clone(&task)));
|
||||
drop(child_inner);
|
||||
// modify kstack_top in trap_cx of this thread
|
||||
let task_inner = task.inner_exclusive_access();
|
||||
let trap_cx = task_inner.get_trap_cx();
|
||||
trap_cx.kernel_sp = task.kstack.get_top();
|
||||
drop(task_inner);
|
||||
insert_into_pid2process(child.getpid(), Arc::clone(&child));
|
||||
// add this thread to scheduler
|
||||
add_task(task);
|
||||
child
|
||||
}
|
||||
|
||||
pub fn getpid(&self) -> usize {
|
||||
self.pid.0
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
export PATH=$(rustc --print sysroot)/bin:$PATH
|
||||
export RUST_SRC_PATH=$(rustc --print sysroot)/lib/rustlib/src/rust/library/
|
@ -0,0 +1,138 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(core_intrinsics)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
extern crate core;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use user_lib::{exit, sleep, thread_create, waittid};
|
||||
|
||||
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
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
use user_lib::console::getchar;
|
||||
|
||||
const N: usize = 10;
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
println!("I will receive {} letters.", N);
|
||||
let mut line = [0u8; N];
|
||||
for idx in 0..N {
|
||||
let c = getchar();
|
||||
line[idx] = c;
|
||||
}
|
||||
println!("{} letters entered", N);
|
||||
for idx in 0..N {
|
||||
print!("{}", line[idx] as char);
|
||||
}
|
||||
println!("");
|
||||
println!("You got it!");
|
||||
0
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
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};
|
||||
|
||||
fn worker(size_kib: usize) {
|
||||
let mut buffer = [0u8; 1024]; // 1KiB
|
||||
for (i, ch) in buffer.iter_mut().enumerate() {
|
||||
*ch = i as u8;
|
||||
}
|
||||
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!");
|
||||
}
|
||||
let f = f as usize;
|
||||
for _ in 0..size_kib {
|
||||
write(f, &buffer);
|
||||
}
|
||||
close(f);
|
||||
exit(0)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
assert_eq!(argc, 2, "wrong argument");
|
||||
let size_mb = 1usize;
|
||||
let size_kb = size_mb << 10;
|
||||
let workers = argv[1].parse::<usize>().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));
|
||||
}
|
||||
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!(
|
||||
"{}MiB written by {} threads, time cost = {}ms, write speed = {}KiB/s",
|
||||
size_mb, workers, time_ms, speed_kbs
|
||||
);
|
||||
0
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![allow(clippy::println_empty_string)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::exit;
|
||||
use user_lib::{semaphore_create, semaphore_down, semaphore_up};
|
||||
use user_lib::{thread_create, waittid};
|
||||
|
||||
const SEM_MUTEX: usize = 0;
|
||||
const SEM_EMPTY: usize = 1;
|
||||
const SEM_EXISTED: usize = 2;
|
||||
const BUFFER_SIZE: usize = 8;
|
||||
static mut BUFFER: [usize; BUFFER_SIZE] = [0; BUFFER_SIZE];
|
||||
static mut FRONT: usize = 0;
|
||||
static mut TAIL: usize = 0;
|
||||
const PRODUCER_COUNT: usize = 4;
|
||||
const NUMBER_PER_PRODUCER: usize = 100;
|
||||
|
||||
unsafe fn producer(id: *const usize) -> ! {
|
||||
let id = *id;
|
||||
for _ in 0..NUMBER_PER_PRODUCER {
|
||||
semaphore_down(SEM_EMPTY);
|
||||
semaphore_down(SEM_MUTEX);
|
||||
BUFFER[FRONT] = id;
|
||||
FRONT = (FRONT + 1) % BUFFER_SIZE;
|
||||
semaphore_up(SEM_MUTEX);
|
||||
semaphore_up(SEM_EXISTED);
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
|
||||
unsafe fn consumer() -> ! {
|
||||
for _ in 0..PRODUCER_COUNT * NUMBER_PER_PRODUCER {
|
||||
semaphore_down(SEM_EXISTED);
|
||||
semaphore_down(SEM_MUTEX);
|
||||
print!("{} ", BUFFER[TAIL]);
|
||||
TAIL = (TAIL + 1) % BUFFER_SIZE;
|
||||
semaphore_up(SEM_MUTEX);
|
||||
semaphore_up(SEM_EMPTY);
|
||||
}
|
||||
println!("");
|
||||
exit(0)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
// create semaphores
|
||||
assert_eq!(semaphore_create(1) as usize, SEM_MUTEX);
|
||||
assert_eq!(semaphore_create(BUFFER_SIZE) as usize, SEM_EMPTY);
|
||||
assert_eq!(semaphore_create(0) as usize, SEM_EXISTED);
|
||||
// create threads
|
||||
let ids: Vec<_> = (0..PRODUCER_COUNT).collect();
|
||||
let mut threads = Vec::new();
|
||||
for i in 0..PRODUCER_COUNT {
|
||||
threads.push(thread_create(
|
||||
producer as usize,
|
||||
&ids.as_slice()[i] as *const _ as usize,
|
||||
));
|
||||
}
|
||||
threads.push(thread_create(consumer as usize, 0));
|
||||
// wait for all threads to complete
|
||||
for thread in threads.iter() {
|
||||
waittid(*thread as usize);
|
||||
}
|
||||
println!("mpsc_sem passed!");
|
||||
0
|
||||
}
|
@ -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 alloc::vec::Vec;
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use user_lib::{exit, sleep, thread_create, waittid};
|
||||
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
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![allow(clippy::println_empty_string)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, get_time, sleep};
|
||||
use user_lib::{mutex_blocking_create, mutex_lock, mutex_unlock};
|
||||
use user_lib::{thread_create, waittid};
|
||||
|
||||
const N: usize = 5;
|
||||
const ROUND: usize = 4;
|
||||
// A round: think -> wait for forks -> eat
|
||||
const GRAPH_SCALE: usize = 100;
|
||||
|
||||
fn get_time_u() -> usize {
|
||||
get_time() as usize
|
||||
}
|
||||
|
||||
// Time unit: ms
|
||||
const ARR: [[usize; ROUND * 2]; N] = [
|
||||
[700, 800, 1000, 400, 500, 600, 200, 400],
|
||||
[300, 600, 200, 700, 1000, 100, 300, 600],
|
||||
[500, 200, 900, 200, 400, 600, 1200, 400],
|
||||
[500, 1000, 600, 500, 800, 600, 200, 900],
|
||||
[600, 100, 600, 600, 200, 500, 600, 200],
|
||||
];
|
||||
static mut THINK: [[usize; ROUND * 2]; N] = [[0; ROUND * 2]; N];
|
||||
static mut EAT: [[usize; ROUND * 2]; N] = [[0; ROUND * 2]; N];
|
||||
|
||||
fn philosopher_dining_problem(id: *const usize) {
|
||||
let id = unsafe { *id };
|
||||
let left = id;
|
||||
let right = if id == N - 1 { 0 } else { id + 1 };
|
||||
let min = if left < right { left } else { right };
|
||||
let max = left + right - min;
|
||||
for round in 0..ROUND {
|
||||
// thinking
|
||||
unsafe {
|
||||
THINK[id][2 * round] = get_time_u();
|
||||
}
|
||||
sleep(ARR[id][2 * round]);
|
||||
unsafe {
|
||||
THINK[id][2 * round + 1] = get_time_u();
|
||||
}
|
||||
// wait for forks
|
||||
mutex_lock(min);
|
||||
mutex_lock(max);
|
||||
// eating
|
||||
unsafe {
|
||||
EAT[id][2 * round] = get_time_u();
|
||||
}
|
||||
sleep(ARR[id][2 * round + 1]);
|
||||
unsafe {
|
||||
EAT[id][2 * round + 1] = get_time_u();
|
||||
}
|
||||
mutex_unlock(max);
|
||||
mutex_unlock(min);
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let mut v = Vec::new();
|
||||
let ids: Vec<_> = (0..N).collect();
|
||||
let start = get_time_u();
|
||||
for i in 0..N {
|
||||
assert_eq!(mutex_blocking_create(), i as isize);
|
||||
v.push(thread_create(
|
||||
philosopher_dining_problem as usize,
|
||||
&ids.as_slice()[i] as *const _ as usize,
|
||||
));
|
||||
}
|
||||
for tid in v.iter() {
|
||||
waittid(*tid as usize);
|
||||
}
|
||||
let time_cost = get_time_u() - start;
|
||||
println!("time cost = {}", time_cost);
|
||||
println!("'-' -> THINKING; 'x' -> EATING; ' ' -> WAITING ");
|
||||
for id in (0..N).into_iter().chain(0..=0) {
|
||||
print!("#{}:", id);
|
||||
for j in 0..time_cost / GRAPH_SCALE {
|
||||
let current_time = j * GRAPH_SCALE + start;
|
||||
if (0..ROUND).any(|round| unsafe {
|
||||
let start_thinking = THINK[id][2 * round];
|
||||
let end_thinking = THINK[id][2 * round + 1];
|
||||
start_thinking <= current_time && current_time <= end_thinking
|
||||
}) {
|
||||
print!("-");
|
||||
} else if (0..ROUND).any(|round| unsafe {
|
||||
let start_eating = EAT[id][2 * round];
|
||||
let end_eating = EAT[id][2 * round + 1];
|
||||
start_eating <= current_time && current_time <= end_eating
|
||||
}) {
|
||||
print!("x");
|
||||
} else {
|
||||
print!(" ");
|
||||
};
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
0
|
||||
}
|
@ -1,201 +0,0 @@
|
||||
// https://www.joshmcguigan.com/blog/build-your-own-shell-rust/
|
||||
// https://github.com/JoshMcguigan/bubble-shell
|
||||
// https://github.com/psinghal20/rush
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
const LF: u8 = 0x0au8;
|
||||
const CR: u8 = 0x0du8;
|
||||
const DL: u8 = 0x7fu8;
|
||||
const BS: u8 = 0x08u8;
|
||||
|
||||
use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::console::{getchar};
|
||||
use user_lib::{close, dup, exec, fork, open, pipe, waitpid, OpenFlags};
|
||||
|
||||
const STDIN: usize = 0;
|
||||
const STDOUT: usize = 1;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum IOType {
|
||||
File(String),
|
||||
// [read end, write end]
|
||||
Pipe([usize; 2]),
|
||||
Inherit,
|
||||
}
|
||||
|
||||
impl IOType {
|
||||
pub fn new_pipe() -> Self {
|
||||
let mut pipe_fd: [usize; 2] = [0; 2];
|
||||
pipe(&mut pipe_fd);
|
||||
Self::Pipe(pipe_fd)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Process {
|
||||
pub pid: isize,
|
||||
pub output: IOType,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
println!("Rust user shell (with pipe)");
|
||||
let mut line: String = String::new();
|
||||
print!(">) ");
|
||||
//flush();
|
||||
loop {
|
||||
let c = getchar();
|
||||
match c {
|
||||
LF | CR => {
|
||||
println!("");
|
||||
if !line.is_empty() {
|
||||
let mut commands = line.trim().split(" | ").peekable();
|
||||
|
||||
let mut previous_process: Option<Process> = None;
|
||||
while let Some(command) = commands.next() {
|
||||
let args: Vec<_> = command.trim().split(' ').collect();
|
||||
let mut args_copy: Vec<String> = args
|
||||
.iter()
|
||||
.map(|&arg| {
|
||||
let mut string = String::new();
|
||||
string.push_str(arg);
|
||||
string.push('\0');
|
||||
string
|
||||
})
|
||||
.collect();
|
||||
|
||||
// redirect input
|
||||
let input = if previous_process.is_some() {
|
||||
previous_process.clone().unwrap().output
|
||||
} else if let Some((idx, _)) = args_copy
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, arg)| arg.as_str() == "<\0")
|
||||
{
|
||||
let tmp = IOType::File(args_copy[idx + 1].clone());
|
||||
args_copy.drain(idx..=idx + 1);
|
||||
tmp
|
||||
} else {
|
||||
IOType::Inherit
|
||||
};
|
||||
|
||||
// redirect output
|
||||
let output = if commands.peek().is_some() {
|
||||
IOType::new_pipe()
|
||||
} else if let Some((idx, _)) = args_copy
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, arg)| arg.as_str() == ">\0")
|
||||
{
|
||||
let tmp = IOType::File(args_copy[idx + 1].clone());
|
||||
args_copy.drain(idx..=idx + 1);
|
||||
tmp
|
||||
} else {
|
||||
IOType::Inherit
|
||||
};
|
||||
|
||||
let mut args_addr: Vec<*const u8> =
|
||||
args_copy.iter().map(|arg| arg.as_ptr()).collect();
|
||||
args_addr.push(0 as *const u8);
|
||||
let pid = fork();
|
||||
if pid == 0 {
|
||||
// input redirection
|
||||
match input.clone() {
|
||||
IOType::File(filename) => {
|
||||
let input_fd = open(filename.as_str(), OpenFlags::RDONLY);
|
||||
if input_fd == -1 {
|
||||
println!("Error when opening file {}", filename);
|
||||
return -4;
|
||||
}
|
||||
let input_fd = input_fd as usize;
|
||||
close(STDIN);
|
||||
assert_eq!(dup(input_fd) as usize, STDIN);
|
||||
close(input_fd);
|
||||
}
|
||||
IOType::Pipe(pipes) => {
|
||||
close(pipes[1]); // close write end
|
||||
close(STDIN);
|
||||
assert_eq!(dup(pipes[0]) as usize, STDIN);
|
||||
close(pipes[0]);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
// output redirection
|
||||
match output.clone() {
|
||||
IOType::File(filename) => {
|
||||
let output_fd = open(
|
||||
filename.as_str(),
|
||||
OpenFlags::CREATE | OpenFlags::WRONLY,
|
||||
);
|
||||
if output_fd == -1 {
|
||||
println!("Error when opening file {}", filename);
|
||||
return -4;
|
||||
}
|
||||
let output_fd = output_fd as usize;
|
||||
close(STDOUT);
|
||||
assert_eq!(dup(output_fd) as usize, STDOUT);
|
||||
close(output_fd);
|
||||
}
|
||||
IOType::Pipe(pipes) => {
|
||||
close(pipes[0]); // close read end
|
||||
close(STDOUT);
|
||||
assert_eq!(dup(pipes[1]) as usize, STDOUT);
|
||||
close(pipes[1]);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
// run child process
|
||||
if exec(args_copy[0].as_str(), args_addr.as_slice()) == -1 {
|
||||
println!("Error when executing!");
|
||||
return -4;
|
||||
}
|
||||
unreachable!("Should not happen!");
|
||||
}
|
||||
// old pipe was done, close it.
|
||||
if let Some(p) = previous_process {
|
||||
match p.output {
|
||||
IOType::Pipe(pipes) => {
|
||||
close(pipes[0]);
|
||||
close(pipes[1]);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
previous_process = Some(Process { pid, output });
|
||||
}
|
||||
line.clear();
|
||||
if let Some(process) = previous_process {
|
||||
let mut exit_code: i32 = 0;
|
||||
let pid = process.pid;
|
||||
let exit_pid = waitpid(pid as usize, &mut exit_code);
|
||||
assert_eq!(pid, exit_pid);
|
||||
println!("Shell: Process {} exited with code {}", pid, exit_code);
|
||||
}
|
||||
}
|
||||
print!(">) ");
|
||||
//flush();
|
||||
}
|
||||
BS | DL => {
|
||||
if !line.is_empty() {
|
||||
print!("{}", BS as char);
|
||||
print!(" ");
|
||||
print!("{}", BS as char);
|
||||
//flush();
|
||||
line.pop();
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
print!("{}", c as char);
|
||||
//flush();
|
||||
line.push(c as char);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, get_time, thread_create, waittid};
|
||||
|
||||
static mut A: usize = 0;
|
||||
const PER_THREAD: usize = 1000;
|
||||
const THREAD_COUNT: usize = 16;
|
||||
|
||||
unsafe fn f() -> ! {
|
||||
let mut t = 2usize;
|
||||
for _ in 0..PER_THREAD {
|
||||
let a = &mut A as *mut usize;
|
||||
let cur = a.read_volatile();
|
||||
for _ in 0..500 {
|
||||
t = t * t % 10007;
|
||||
}
|
||||
a.write_volatile(cur + 1);
|
||||
}
|
||||
exit(t as i32)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
time_cost.push(waittid(*tid));
|
||||
}
|
||||
println!("time cost is {}ms", get_time() - start);
|
||||
assert_eq!(unsafe { A }, PER_THREAD * THREAD_COUNT);
|
||||
0
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use crate::alloc::string::ToString;
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, get_time, thread_create, waittid};
|
||||
|
||||
static mut A: usize = 0;
|
||||
const PER_THREAD: usize = 1000;
|
||||
const THREAD_COUNT: usize = 16;
|
||||
|
||||
unsafe fn f(count: usize) -> ! {
|
||||
let mut t = 2usize;
|
||||
for _ in 0..PER_THREAD {
|
||||
let a = &mut A as *mut usize;
|
||||
let cur = a.read_volatile();
|
||||
for _ in 0..count {
|
||||
t = t * t % 10007;
|
||||
}
|
||||
a.write_volatile(cur + 1);
|
||||
}
|
||||
exit(t as i32)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
let count: usize;
|
||||
if argc == 1 {
|
||||
count = THREAD_COUNT;
|
||||
} else if argc == 2 {
|
||||
count = argv[1].to_string().parse::<usize>().unwrap();
|
||||
} else {
|
||||
println!(
|
||||
"ERROR in argv, argc is {}, argv[0] {} , argv[1] {} , argv[2] {}",
|
||||
argc, argv[0], argv[1], argv[2]
|
||||
);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize, count) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
time_cost.push(waittid(*tid));
|
||||
}
|
||||
println!("time cost is {}ms", get_time() - start);
|
||||
assert_eq!(unsafe { A }, PER_THREAD * THREAD_COUNT);
|
||||
0
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use user_lib::{exit, get_time, thread_create, waittid, yield_};
|
||||
|
||||
static mut A: usize = 0;
|
||||
static OCCUPIED: AtomicBool = AtomicBool::new(false);
|
||||
const PER_THREAD: usize = 1000;
|
||||
const THREAD_COUNT: usize = 16;
|
||||
|
||||
unsafe fn f() -> ! {
|
||||
let mut t = 2usize;
|
||||
for _ in 0..PER_THREAD {
|
||||
while OCCUPIED
|
||||
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
|
||||
.is_err()
|
||||
{
|
||||
yield_();
|
||||
}
|
||||
let a = &mut A as *mut usize;
|
||||
let cur = a.read_volatile();
|
||||
for _ in 0..500 {
|
||||
t = t * t % 10007;
|
||||
}
|
||||
a.write_volatile(cur + 1);
|
||||
OCCUPIED.store(false, Ordering::Relaxed);
|
||||
}
|
||||
exit(t as i32)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
time_cost.push(waittid(*tid));
|
||||
}
|
||||
println!("time cost is {}ms", get_time() - start);
|
||||
assert_eq!(unsafe { A }, PER_THREAD * THREAD_COUNT);
|
||||
0
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, get_time, thread_create, waittid, yield_};
|
||||
|
||||
static mut A: usize = 0;
|
||||
static mut OCCUPIED: bool = false;
|
||||
const PER_THREAD: usize = 1000;
|
||||
const THREAD_COUNT: usize = 16;
|
||||
|
||||
unsafe fn f() -> ! {
|
||||
let mut t = 2usize;
|
||||
for _ in 0..PER_THREAD {
|
||||
while OCCUPIED {
|
||||
yield_();
|
||||
}
|
||||
OCCUPIED = true;
|
||||
// enter critical section
|
||||
let a = &mut A as *mut usize;
|
||||
let cur = a.read_volatile();
|
||||
for _ in 0..500 {
|
||||
t = t * t % 10007;
|
||||
}
|
||||
a.write_volatile(cur + 1);
|
||||
// exit critical section
|
||||
OCCUPIED = false;
|
||||
}
|
||||
|
||||
exit(t as i32)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
time_cost.push(waittid(*tid));
|
||||
}
|
||||
println!("time cost is {}ms", get_time() - start);
|
||||
assert_eq!(unsafe { A }, PER_THREAD * THREAD_COUNT);
|
||||
0
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, get_time, thread_create, waittid};
|
||||
use user_lib::{mutex_blocking_create, mutex_lock, mutex_unlock};
|
||||
|
||||
static mut A: usize = 0;
|
||||
const PER_THREAD: usize = 1000;
|
||||
const THREAD_COUNT: usize = 16;
|
||||
|
||||
unsafe fn f() -> ! {
|
||||
let mut t = 2usize;
|
||||
for _ in 0..PER_THREAD {
|
||||
mutex_lock(0);
|
||||
let a = &mut A as *mut usize;
|
||||
let cur = a.read_volatile();
|
||||
for _ in 0..500 {
|
||||
t = t * t % 10007;
|
||||
}
|
||||
a.write_volatile(cur + 1);
|
||||
mutex_unlock(0);
|
||||
}
|
||||
exit(t as i32)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let start = get_time();
|
||||
assert_eq!(mutex_blocking_create(), 0);
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
time_cost.push(waittid(*tid));
|
||||
}
|
||||
println!("time cost is {}ms", get_time() - start);
|
||||
assert_eq!(unsafe { A }, PER_THREAD * THREAD_COUNT);
|
||||
0
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, get_time, thread_create, waittid};
|
||||
use user_lib::{mutex_create, mutex_lock, mutex_unlock};
|
||||
|
||||
static mut A: usize = 0;
|
||||
const PER_THREAD: usize = 1000;
|
||||
const THREAD_COUNT: usize = 16;
|
||||
|
||||
unsafe fn f() -> ! {
|
||||
let mut t = 2usize;
|
||||
for _ in 0..PER_THREAD {
|
||||
mutex_lock(0);
|
||||
let a = &mut A as *mut usize;
|
||||
let cur = a.read_volatile();
|
||||
for _ in 0..500 {
|
||||
t = t * t % 10007;
|
||||
}
|
||||
a.write_volatile(cur + 1);
|
||||
mutex_unlock(0);
|
||||
}
|
||||
exit(t as i32)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let start = get_time();
|
||||
assert_eq!(mutex_create(), 0);
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
time_cost.push(waittid(*tid));
|
||||
}
|
||||
println!("time cost is {}ms", get_time() - start);
|
||||
assert_eq!(unsafe { A }, PER_THREAD * THREAD_COUNT);
|
||||
0
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
// use user_lib::{sigaction, sigprocmask, SignalAction, SignalFlags, fork, exit, wait, kill, getpid, sleep, sigreturn};
|
||||
use user_lib::*;
|
||||
|
||||
fn func() {
|
||||
println!("user_sig_test succsess");
|
||||
sigreturn();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let mut new = SignalAction::default();
|
||||
let old = SignalAction::default();
|
||||
new.handler = func as usize;
|
||||
|
||||
println!("signal_simple: sigaction");
|
||||
if sigaction(SIGUSR1, &new, &old) < 0 {
|
||||
panic!("Sigaction failed!");
|
||||
}
|
||||
println!("signal_simple: kill");
|
||||
if kill(getpid() as usize, SIGUSR1) < 0 {
|
||||
println!("Kill failed!");
|
||||
exit(1);
|
||||
}
|
||||
println!("signal_simple: Done");
|
||||
0
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
use user_lib::{sigaction, sigprocmask, SignalAction, SignalFlags, fork, exit, waitpid, kill, getpid, sleep, sigreturn};
|
||||
|
||||
fn func() {
|
||||
println!("user_sig_test succsess");
|
||||
sigreturn();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let pid = fork();
|
||||
if pid==0{
|
||||
let mut new = SignalAction::default();
|
||||
let old = SignalAction::default();
|
||||
new.handler = func as usize;
|
||||
|
||||
println!("signal_simple2: child sigaction");
|
||||
if sigaction(10, &new, &old) < 0 {
|
||||
panic!("Sigaction failed!");
|
||||
}
|
||||
sleep(1000);
|
||||
println!("signal_simple2: child done");
|
||||
exit(0);
|
||||
} else if pid >0 {
|
||||
println!("signal_simple2: parent kill child");
|
||||
sleep(500);
|
||||
if kill(pid as usize, 1<<10) < 0 {
|
||||
println!("Kill failed!");
|
||||
exit(1);
|
||||
}
|
||||
println!("signal_simple2: parent wait child");
|
||||
let mut exit_code = 0;
|
||||
waitpid(pid as usize, &mut exit_code);
|
||||
println!("signal_simple2: parent Done");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
0
|
||||
}
|
@ -1,204 +0,0 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
// use user_lib::{sigaction, sigprocmask, SignalAction, SignalFlags, fork, exit, wait, kill, getpid, sleep, sigreturn};
|
||||
use user_lib::*;
|
||||
|
||||
fn func() {
|
||||
println!("user_sig_test succsess");
|
||||
sigreturn();
|
||||
}
|
||||
|
||||
fn func2() {
|
||||
loop {
|
||||
print!("");
|
||||
}
|
||||
}
|
||||
|
||||
fn func3() {
|
||||
println!("interrupt");
|
||||
sigreturn();
|
||||
}
|
||||
|
||||
fn user_sig_test_failsignum() {
|
||||
let mut new = SignalAction::default();
|
||||
let old = SignalAction::default();
|
||||
new.handler = func as usize;
|
||||
if sigaction(50, &new, &old) >= 0 {
|
||||
panic!("Wrong sigaction but success!");
|
||||
}
|
||||
}
|
||||
|
||||
fn user_sig_test_kill() {
|
||||
let mut new = SignalAction::default();
|
||||
let old = SignalAction::default();
|
||||
new.handler = func as usize;
|
||||
|
||||
if sigaction(SIGUSR1, &new, &old) < 0 {
|
||||
panic!("Sigaction failed!");
|
||||
}
|
||||
if kill(getpid() as usize, SIGUSR1) < 0 {
|
||||
println!("Kill failed!");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn user_sig_test_multiprocsignals() {
|
||||
let pid= fork();
|
||||
if pid == 0{
|
||||
let mut new = SignalAction::default();
|
||||
let old = SignalAction::default();
|
||||
new.handler = func as usize;
|
||||
if sigaction(SIGUSR1, &new, &old) < 0 {
|
||||
panic!("Sigaction failed!");
|
||||
}
|
||||
} else {
|
||||
if kill(pid as usize, SIGUSR1) < 0 {
|
||||
println!("Kill failed!");
|
||||
exit(1);
|
||||
}
|
||||
let mut exit_code = 0;
|
||||
wait(&mut exit_code);
|
||||
}
|
||||
}
|
||||
|
||||
fn user_sig_test_restore() {
|
||||
let mut new = SignalAction::default();
|
||||
let old = SignalAction::default();
|
||||
let old2 = SignalAction::default();
|
||||
new.handler = func as usize;
|
||||
|
||||
if sigaction(SIGUSR1, &new, &old) < 0 {
|
||||
panic!("Sigaction failed!");
|
||||
}
|
||||
|
||||
if sigaction(SIGUSR1, &old, &old2) < 0 {
|
||||
panic!("Sigaction failed!");
|
||||
}
|
||||
|
||||
if old2.handler != new.handler {
|
||||
println!("Restore failed!");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
fn kernel_sig_test_ignore() {
|
||||
sigprocmask(SignalFlags::SIGSTOP.bits() as u32);
|
||||
if kill(getpid() as usize, SignalFlags::SIGSTOP.bits()) < 0{
|
||||
println!("kill faild\n");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
fn kernel_sig_test_stop_cont() {
|
||||
let pid= fork();
|
||||
if pid == 0 {
|
||||
kill(getpid() as usize, SignalFlags::SIGSTOP.bits());
|
||||
sleep(1000);
|
||||
exit(-1);
|
||||
} else {
|
||||
sleep(5000);
|
||||
kill(pid as usize, SignalFlags::SIGCONT.bits());
|
||||
let mut exit_code = 0;
|
||||
wait(&mut exit_code);
|
||||
}
|
||||
}
|
||||
|
||||
fn kernel_sig_test_failignorekill() {
|
||||
let mut new = SignalAction::default();
|
||||
let old = SignalAction::default();
|
||||
new.handler = func as usize;
|
||||
|
||||
if sigaction(9, &new, &old) >= 0 {
|
||||
panic!("Should not set sigaction to kill!");
|
||||
}
|
||||
|
||||
if sigaction(9, &new, 0 as *const SignalAction) >= 0 {
|
||||
panic!("Should not set sigaction to kill!");
|
||||
}
|
||||
|
||||
if sigaction(9, 0 as *const SignalAction, &old) >= 0 {
|
||||
panic!("Should not set sigaction to kill!");
|
||||
}
|
||||
}
|
||||
|
||||
fn final_sig_test() {
|
||||
let mut new = SignalAction::default();
|
||||
let old = SignalAction::default();
|
||||
new.handler = func2 as usize;
|
||||
|
||||
let mut new2 = SignalAction::default();
|
||||
let old2 = SignalAction::default();
|
||||
new2.handler = func3 as usize;
|
||||
|
||||
let pid= fork();
|
||||
if pid == 0{
|
||||
if sigaction(SIGUSR1, &new, &old) < 0 {
|
||||
panic!("Sigaction failed!");
|
||||
}
|
||||
if sigaction(14, &new2, &old2) < 0 {
|
||||
panic!("Sigaction failed!");
|
||||
}
|
||||
if kill(getpid() as usize, SIGUSR1) < 0 {
|
||||
println!("Kill failed!");
|
||||
exit(-1);
|
||||
}
|
||||
} else {
|
||||
sleep(1000);
|
||||
if kill(pid as usize, 1 << 14) < 0 {
|
||||
println!("Kill failed!");
|
||||
exit(-1);
|
||||
}
|
||||
sleep(1000);
|
||||
kill(pid as usize, SignalFlags::SIGKILL.bits());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn run(f: fn()) -> bool {
|
||||
let pid = fork();
|
||||
if pid == 0 {
|
||||
f();
|
||||
exit(0);
|
||||
} else {
|
||||
let mut exit_code: i32 = 0;
|
||||
wait(&mut exit_code);
|
||||
if exit_code != 0 {
|
||||
println!("FAILED!");
|
||||
} else {
|
||||
println!("OK!");
|
||||
}
|
||||
exit_code == 0
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let tests: [(fn(), &str); 8] = [
|
||||
(user_sig_test_failsignum, "user_sig_test_failsignum"),
|
||||
(user_sig_test_kill, "user_sig_test_kill"),
|
||||
(user_sig_test_multiprocsignals, "user_sig_test_multiprocsignals"),
|
||||
(user_sig_test_restore, "user_sig_test_restore"),
|
||||
(kernel_sig_test_ignore, "kernel_sig_test_ignore"),
|
||||
(kernel_sig_test_stop_cont, "kernel_sig_test_stop_cont"),
|
||||
(kernel_sig_test_failignorekill, "kernel_sig_test_failignorekill"),
|
||||
(final_sig_test, "final_sig_test")
|
||||
];
|
||||
let mut fail_num = 0;
|
||||
for test in tests {
|
||||
println!("Testing {}", test.1);
|
||||
if !run(test.0) {
|
||||
fail_num += 1;
|
||||
}
|
||||
}
|
||||
if fail_num == 0 {
|
||||
println!("ALL TESTS PASSED");
|
||||
0
|
||||
} else {
|
||||
println!("SOME TESTS FAILED");
|
||||
-1
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec;
|
||||
use user_lib::exit;
|
||||
use user_lib::{semaphore_create, semaphore_down, semaphore_up};
|
||||
use user_lib::{sleep, thread_create, waittid};
|
||||
|
||||
const SEM_SYNC: usize = 0;
|
||||
|
||||
unsafe fn first() -> ! {
|
||||
sleep(10);
|
||||
println!("First work and wakeup Second");
|
||||
semaphore_up(SEM_SYNC);
|
||||
exit(0)
|
||||
}
|
||||
|
||||
unsafe fn second() -> ! {
|
||||
println!("Second want to continue,but need to wait first");
|
||||
semaphore_down(SEM_SYNC);
|
||||
println!("Second can work now");
|
||||
exit(0)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
// create semaphores
|
||||
assert_eq!(semaphore_create(0) as usize, SEM_SYNC);
|
||||
// create threads
|
||||
let threads = vec![
|
||||
thread_create(first as usize, 0),
|
||||
thread_create(second as usize, 0),
|
||||
];
|
||||
// wait for all threads to complete
|
||||
for thread in threads.iter() {
|
||||
waittid(*thread as usize);
|
||||
}
|
||||
println!("sync_sem passed!");
|
||||
0
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec;
|
||||
use user_lib::exit;
|
||||
use user_lib::{
|
||||
condvar_create, condvar_signal, condvar_wait, mutex_blocking_create, mutex_lock, mutex_unlock,
|
||||
};
|
||||
use user_lib::{sleep, thread_create, waittid};
|
||||
|
||||
static mut A: usize = 0;
|
||||
|
||||
const CONDVAR_ID: usize = 0;
|
||||
const MUTEX_ID: usize = 0;
|
||||
|
||||
unsafe fn first() -> ! {
|
||||
sleep(10);
|
||||
println!("First work, Change A --> 1 and wakeup Second");
|
||||
mutex_lock(MUTEX_ID);
|
||||
A = 1;
|
||||
condvar_signal(CONDVAR_ID);
|
||||
mutex_unlock(MUTEX_ID);
|
||||
exit(0)
|
||||
}
|
||||
|
||||
unsafe fn second() -> ! {
|
||||
println!("Second want to continue,but need to wait A=1");
|
||||
mutex_lock(MUTEX_ID);
|
||||
while A == 0 {
|
||||
println!("Second: A is {}", A);
|
||||
condvar_wait(CONDVAR_ID, MUTEX_ID);
|
||||
}
|
||||
mutex_unlock(MUTEX_ID);
|
||||
println!("A is {}, Second can work now", A);
|
||||
exit(0)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
// create condvar & mutex
|
||||
assert_eq!(condvar_create() as usize, CONDVAR_ID);
|
||||
assert_eq!(mutex_blocking_create() as usize, MUTEX_ID);
|
||||
// create threads
|
||||
let threads = vec![
|
||||
thread_create(first as usize, 0),
|
||||
thread_create(second as usize, 0),
|
||||
];
|
||||
// wait for all threads to complete
|
||||
for thread in threads.iter() {
|
||||
waittid(*thread as usize);
|
||||
}
|
||||
println!("test_condvar passed!");
|
||||
0
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec;
|
||||
use user_lib::{exit, thread_create, waittid};
|
||||
|
||||
pub fn thread_a() -> ! {
|
||||
for _ in 0..1000 {
|
||||
print!("a");
|
||||
}
|
||||
exit(1)
|
||||
}
|
||||
|
||||
pub fn thread_b() -> ! {
|
||||
for _ in 0..1000 {
|
||||
print!("b");
|
||||
}
|
||||
exit(2)
|
||||
}
|
||||
|
||||
pub fn thread_c() -> ! {
|
||||
for _ in 0..1000 {
|
||||
print!("c");
|
||||
}
|
||||
exit(3)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let v = vec![
|
||||
thread_create(thread_a as usize, 0),
|
||||
thread_create(thread_b as usize, 0),
|
||||
thread_create(thread_c as usize, 0),
|
||||
];
|
||||
for tid in v.iter() {
|
||||
let exit_code = waittid(*tid as usize);
|
||||
println!("thread#{} exited with code {}", tid, exit_code);
|
||||
}
|
||||
println!("main thread exited.");
|
||||
0
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, thread_create, waittid};
|
||||
|
||||
struct Argument {
|
||||
pub ch: char,
|
||||
pub rc: i32,
|
||||
}
|
||||
|
||||
fn thread_print(arg: *const Argument) -> ! {
|
||||
let arg = unsafe { &*arg };
|
||||
for _ in 0..1000 {
|
||||
print!("{}", arg.ch);
|
||||
}
|
||||
exit(arg.rc)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let mut v = Vec::new();
|
||||
let args = [
|
||||
Argument { ch: 'a', rc: 1 },
|
||||
Argument { ch: 'b', rc: 2 },
|
||||
Argument { ch: 'c', rc: 3 },
|
||||
];
|
||||
for arg in args.iter() {
|
||||
v.push(thread_create(
|
||||
thread_print as usize,
|
||||
arg as *const _ as usize,
|
||||
));
|
||||
}
|
||||
for tid in v.iter() {
|
||||
let exit_code = waittid(*tid as usize);
|
||||
println!("thread#{} exited with code {}", tid, exit_code);
|
||||
}
|
||||
println!("main thread exited.");
|
||||
0
|
||||
}
|
Loading…
Reference in new issue