Compare commits
54 Commits
Author | SHA1 | Date |
---|---|---|
|
21c3c492ac | 3 years ago |
|
3fd8b8e94d | 3 years ago |
|
8c92e66f6a | 3 years ago |
|
4387d128d8 | 3 years ago |
|
5aee0ddfd3 | 3 years ago |
|
1205a81f35 | 3 years ago |
|
3bc09a651e | 3 years ago |
|
07bdb0ce7e | 3 years ago |
|
96a15bc80d | 3 years ago |
|
cd03de03e4 | 3 years ago |
|
ec25d32cf9 | 3 years ago |
|
5d518349e9 | 3 years ago |
|
041adc8b9c | 3 years ago |
|
0aba21b556 | 3 years ago |
|
9ff45a2e7a | 4 years ago |
|
2793aa60e5 | 4 years ago |
|
6c79752cff | 4 years ago |
|
bfa23a257b | 4 years ago |
|
e15436368f | 4 years ago |
|
c86f31295c | 4 years ago |
|
e468c5d951 | 4 years ago |
|
1d307c6582 | 4 years ago |
|
a5f972b77e | 4 years ago |
|
68545145a3 | 4 years ago |
|
9f770750b7 | 4 years ago |
|
9d3d3a9147 | 4 years ago |
|
1537c57d12 | 4 years ago |
|
0bce7242d8 | 4 years ago |
|
3ea60ee964 | 4 years ago |
|
902e0a2f69 | 4 years ago |
|
ad16ae21a5 | 4 years ago |
|
c8ebc26ded | 4 years ago |
|
c25b7a9eb6 | 4 years ago |
|
b6d38e5a0d | 4 years ago |
|
9d736068b9 | 4 years ago |
|
30d6c88438 | 4 years ago |
|
f2905a003f | 4 years ago |
|
b096dcee78 | 4 years ago |
|
becf14defa | 4 years ago |
|
00b3e5ff14 | 4 years ago |
|
4ada6bde3c | 4 years ago |
|
8447285789 | 4 years ago |
|
07ec90ddf7 | 4 years ago |
|
a54c26720c | 4 years ago |
|
6705d7a1e2 | 4 years ago |
|
24151b9211 | 4 years ago |
|
6346c4c943 | 4 years ago |
|
306c3a127d | 4 years ago |
|
40345eb3c2 | 5 years ago |
|
726c19276f | 5 years ago |
|
5ddf7ed1de | 5 years ago |
|
afc7a9c4e9 | 5 years ago |
|
86f4e77066 | 5 years ago |
|
b938944882 | 5 years ago |
@ -0,0 +1 @@
|
||||
*/*
|
@ -0,0 +1,25 @@
|
||||
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 }}
|
@ -1,8 +1,18 @@
|
||||
.idea/*
|
||||
os/target/*
|
||||
os/.idea/*
|
||||
.idea
|
||||
Cargo.lock
|
||||
target
|
||||
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,40 @@
|
||||
FROM ubuntu:18.04
|
||||
LABEL maintainer="dinghao188" \
|
||||
version="1.1" \
|
||||
description="ubuntu 18.04 with tools for tsinghua's rCore-Tutorial-V3"
|
||||
|
||||
#install some deps
|
||||
RUN set -x \
|
||||
&& apt-get update \
|
||||
&& apt-get install -y curl wget autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev \
|
||||
gawk build-essential bison flex texinfo gperf libtool patchutils bc xz-utils \
|
||||
zlib1g-dev libexpat-dev pkg-config libglib2.0-dev libpixman-1-dev git tmux python3
|
||||
|
||||
#install rust and qemu
|
||||
RUN set -x; \
|
||||
RUSTUP='/root/rustup.sh' \
|
||||
&& cd $HOME \
|
||||
#install rust
|
||||
&& curl https://sh.rustup.rs -sSf > $RUSTUP && chmod +x $RUSTUP \
|
||||
&& $RUSTUP -y --default-toolchain nightly --profile minimal \
|
||||
|
||||
#compile qemu
|
||||
&& wget https://ftp.osuosl.org/pub/blfs/conglomeration/qemu/qemu-5.0.0.tar.xz \
|
||||
&& tar xvJf qemu-5.0.0.tar.xz \
|
||||
&& cd qemu-5.0.0 \
|
||||
&& ./configure --target-list=riscv64-softmmu,riscv64-linux-user \
|
||||
&& make -j$(nproc) install \
|
||||
&& cd $HOME && rm -rf qemu-5.0.0 qemu-5.0.0.tar.xz
|
||||
|
||||
#for chinese network
|
||||
RUN set -x; \
|
||||
APT_CONF='/etc/apt/sources.list'; \
|
||||
CARGO_CONF='/root/.cargo/config'; \
|
||||
BASHRC='/root/.bashrc' \
|
||||
&& echo 'export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static' >> $BASHRC \
|
||||
&& echo 'export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup' >> $BASHRC \
|
||||
&& touch $CARGO_CONF \
|
||||
&& echo '[source.crates-io]' > $CARGO_CONF \
|
||||
&& echo "replace-with = 'ustc'" >> $CARGO_CONF \
|
||||
&& echo '[source.ustc]' >> $CARGO_CONF \
|
||||
&& echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' >> $CARGO_CONF
|
@ -0,0 +1,10 @@
|
||||
DOCKER_NAME ?= dinghao188/rcore-tutorial
|
||||
.PHONY: docker build_docker
|
||||
|
||||
docker:
|
||||
docker run --rm -it --mount type=bind,source=$(shell pwd),destination=/mnt ${DOCKER_NAME}
|
||||
|
||||
build_docker:
|
||||
docker build -t ${DOCKER_NAME} .
|
||||
fmt:
|
||||
cd os ; cargo fmt; cd ../user; cargo fmt; cd ..
|
@ -1,2 +1,260 @@
|
||||
# rCore-Tutorial-v3
|
||||
rCore-Tutorial version 3.
|
||||
rCore-Tutorial version 3.5. See the [Documentation in Chinese](https://rcore-os.github.io/rCore-Tutorial-Book-v3/).
|
||||
|
||||
rCore-Tutorial API Docs. See the [API Docs of Ten OSes ](#OS-API-DOCS)
|
||||
|
||||
Official QQ group number: 735045051
|
||||
|
||||
## news
|
||||
- 25/01/2022: Version 3.6.0 is on the way! Now we directly update the code on chX branches, please periodically check if there are any updates.
|
||||
|
||||
## Overview
|
||||
|
||||
This project aims to show how to write an **Unix-like OS** running on **RISC-V** platforms **from scratch** in **[Rust](https://www.rust-lang.org/)** for **beginners** without any background knowledge about **computer architectures, assembly languages or operating systems**.
|
||||
|
||||
## Features
|
||||
|
||||
* Platform supported: `qemu-system-riscv64` simulator or dev boards based on [Kendryte K210 SoC](https://canaan.io/product/kendryteai) such as [Maix Dock](https://www.seeedstudio.com/Sipeed-MAIX-Dock-p-4815.html)
|
||||
* OS
|
||||
* concurrency of multiple processes each of which contains mutiple native threads
|
||||
* preemptive scheduling(Round-Robin algorithm)
|
||||
* dynamic memory management in kernel
|
||||
* virtual memory
|
||||
* a simple file system with a block cache
|
||||
* an interactive shell in the userspace
|
||||
* **only 4K+ LoC**
|
||||
* [A detailed documentation in Chinese](https://rcore-os.github.io/rCore-Tutorial-Book-v3/) in spite of the lack of comments in the code(English version is not available at present)
|
||||
|
||||
## Prerequisites
|
||||
|
||||
### Install Rust
|
||||
|
||||
See [official guide](https://www.rust-lang.org/tools/install).
|
||||
|
||||
Install some tools:
|
||||
|
||||
```sh
|
||||
$ rustup target add riscv64gc-unknown-none-elf
|
||||
$ cargo install cargo-binutils --vers =0.3.3
|
||||
$ rustup component add llvm-tools-preview
|
||||
$ rustup component add rust-src
|
||||
```
|
||||
|
||||
### Install Qemu
|
||||
|
||||
Here we manually compile and install Qemu 5.0.0. For example, on Ubuntu 18.04:
|
||||
|
||||
```sh
|
||||
# install dependency packages
|
||||
$ sudo apt install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev \
|
||||
gawk build-essential bison flex texinfo gperf libtool patchutils bc \
|
||||
zlib1g-dev libexpat-dev pkg-config libglib2.0-dev libpixman-1-dev git tmux python3 python3-pip
|
||||
# download Qemu source code
|
||||
$ wget https://download.qemu.org/qemu-5.0.0.tar.xz
|
||||
# extract to qemu-5.0.0/
|
||||
$ tar xvJf qemu-5.0.0.tar.xz
|
||||
$ cd qemu-5.0.0
|
||||
# build
|
||||
$ ./configure --target-list=riscv64-softmmu,riscv64-linux-user
|
||||
$ make -j$(nproc)
|
||||
```
|
||||
|
||||
Then, add following contents to `~/.bashrc`(please adjust these paths according to your environment):
|
||||
|
||||
```
|
||||
export PATH=$PATH:/home/shinbokuow/Downloads/built/qemu-5.0.0
|
||||
export PATH=$PATH:/home/shinbokuow/Downloads/built/qemu-5.0.0/riscv64-softmmu
|
||||
export PATH=$PATH:/home/shinbokuow/Downloads/built/qemu-5.0.0/riscv64-linux-user
|
||||
```
|
||||
|
||||
Finally, update the current shell:
|
||||
|
||||
```sh
|
||||
$ source ~/.bashrc
|
||||
```
|
||||
|
||||
Now we can check the version of Qemu:
|
||||
|
||||
```sh
|
||||
$ qemu-system-riscv64 --version
|
||||
QEMU emulator version 5.0.0
|
||||
Copyright (c) 2003-2020 Fabrice Bellard and the QEMU Project developers
|
||||
```
|
||||
|
||||
### Install RISC-V GNU Embedded Toolchain(including GDB)
|
||||
|
||||
Download the compressed file according to your platform From [Sifive website](https://www.sifive.com/software)(Ctrl+F 'toolchain').
|
||||
|
||||
Extract it and append the location of the 'bin' directory under its root directory to `$PATH`.
|
||||
|
||||
For example, we can check the version of GDB:
|
||||
|
||||
```sh
|
||||
$ riscv64-unknown-elf-gdb --version
|
||||
GNU gdb (SiFive GDB-Metal 10.1.0-2020.12.7) 10.1
|
||||
Copyright (C) 2020 Free Software Foundation, Inc.
|
||||
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
|
||||
This is free software: you are free to change and redistribute it.
|
||||
There is NO WARRANTY, to the extent permitted by law.
|
||||
```
|
||||
|
||||
### Install serial tools(Optional, if you want to run on K210)
|
||||
|
||||
```sh
|
||||
$ pip3 install pyserial
|
||||
$ sudo apt install python3-serial
|
||||
```
|
||||
|
||||
## Run our project
|
||||
|
||||
### Qemu
|
||||
|
||||
```sh
|
||||
$ git clone https://github.com/rcore-os/rCore-Tutorial-v3.git
|
||||
$ cd rCore-Tutorial-v3/os
|
||||
$ make run
|
||||
```
|
||||
|
||||
After outputing some debug messages, the kernel lists all the applications available and enter the user shell:
|
||||
|
||||
```
|
||||
/**** APPS ****
|
||||
mpsc_sem
|
||||
usertests
|
||||
pipetest
|
||||
forktest2
|
||||
cat
|
||||
initproc
|
||||
race_adder_loop
|
||||
threads_arg
|
||||
race_adder_mutex_spin
|
||||
race_adder_mutex_blocking
|
||||
forktree
|
||||
user_shell
|
||||
huge_write
|
||||
race_adder
|
||||
race_adder_atomic
|
||||
threads
|
||||
stack_overflow
|
||||
filetest_simple
|
||||
forktest_simple
|
||||
cmdline_args
|
||||
run_pipe_test
|
||||
forktest
|
||||
matrix
|
||||
exit
|
||||
fantastic_text
|
||||
sleep_simple
|
||||
yield
|
||||
hello_world
|
||||
pipe_large_test
|
||||
sleep
|
||||
phil_din_mutex
|
||||
**************/
|
||||
Rust user shell
|
||||
>>
|
||||
```
|
||||
|
||||
You can run any application except for `initproc` and `user_shell` itself. To run an application, just input its filename and hit enter. `usertests` can run a bunch of applications, thus it is recommended.
|
||||
|
||||
Type `Ctrl+a` then `x` to exit Qemu.
|
||||
|
||||
### K210
|
||||
|
||||
Before chapter 6, you do not need a SD card:
|
||||
|
||||
```sh
|
||||
$ git clone https://github.com/rcore-os/rCore-Tutorial-v3.git
|
||||
$ cd rCore-Tutorial-v3/os
|
||||
$ make run BOARD=k210
|
||||
```
|
||||
|
||||
From chapter 6, before running the kernel, we should insert a SD card into PC and manually write the filesystem image to it:
|
||||
|
||||
```sh
|
||||
$ cd rCore-Tutorial-v3/os
|
||||
$ make sdcard
|
||||
```
|
||||
|
||||
By default it will overwrite the device `/dev/sdb` which is the SD card, but you can provide another location. For example, `make sdcard SDCARD=/dev/sdc`.
|
||||
|
||||
After that, remove the SD card from PC and insert it to the slot of K210. Connect the K210 to PC and then:
|
||||
|
||||
```sh
|
||||
$ git clone https://github.com/rcore-os/rCore-Tutorial-v3.git
|
||||
$ cd rCore-Tutorial-v3/os
|
||||
$ make run BOARD=k210
|
||||
```
|
||||
|
||||
Type `Ctrl+]` to disconnect from K210.
|
||||
|
||||
## Rustdoc
|
||||
|
||||
Currently it can only help you view the code since only a tiny part of the code has been documented.
|
||||
|
||||
You can open a doc html of `os` using `cargo doc --no-deps --open` under `os` directory.
|
||||
|
||||
### OS-API-DOCS
|
||||
The API Docs for Ten OS
|
||||
1. [Lib-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch1/os/index.html)
|
||||
1. [Batch-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch2/os/index.html)
|
||||
1. [MultiProg-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch3-coop/os/index.html)
|
||||
1. [TimeSharing-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch3/os/index.html)
|
||||
1. [AddrSpace-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch4/os/index.html)
|
||||
1. [Process-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch5/os/index.html)
|
||||
1. [FileSystem-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch6/os/index.html)
|
||||
1. [IPC-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch7/os/index.html)
|
||||
1. [SyncMutex-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch8/os/index.html)
|
||||
1. [IODevice-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch9/os/index.html)
|
||||
|
||||
## Working in progress
|
||||
|
||||
Our first release 3.5.0 (chapter 1-7) has been published.
|
||||
|
||||
There will be 9 chapters in our next release 3.6.0, where 2 new chapters will be added:
|
||||
* chapter 8: synchronization on a uniprocessor
|
||||
* chapter 9: I/O devices
|
||||
|
||||
Current version is 3.6.0-alpha.1 and we are still working on it.
|
||||
|
||||
Here are the updates since 3.5.0:
|
||||
|
||||
### Completed
|
||||
|
||||
* [x] automatically clean up and rebuild before running our project on a different platform
|
||||
* [x] fix `power` series application in early chapters, now you can find modulus in the output
|
||||
* [x] use `UPSafeCell` instead of `RefCell` or `spin::Mutex` in order to access static data structures and adjust its API so that it cannot be borrowed twice at a time(mention `& .exclusive_access().task[0]` in `run_first_task`)
|
||||
* [x] move `TaskContext` into `TaskControlBlock` instead of restoring it in place on kernel stack(since ch3), eliminating annoying `task_cx_ptr2`
|
||||
* [x] replace `llvm_asm!` with `asm!`
|
||||
* [x] expand the fs image size generated by `rcore-fs-fuse` to 128MiB
|
||||
* [x] add a new test named `huge_write` which evaluates the fs performance(qemu\~500KiB/s k210\~50KiB/s)
|
||||
* [x] flush all block cache to disk after a fs transaction which involves write operation
|
||||
* [x] replace `spin::Mutex` with `UPSafeCell` before SMP chapter
|
||||
* [x] add codes for a new chapter about synchronization & mutual exclusion(uniprocessor only)
|
||||
* [x] bug fix: we should call `find_pte` rather than `find_pte_create` in `PageTable::unmap`
|
||||
* [x] clarify: "check validity of level-3 pte in `find_pte` instead of checking it outside this function" should not be a bug
|
||||
* [x] code of chapter 8: synchronization on a uniprocessor
|
||||
* [x] switch the code of chapter 6 and chapter 7
|
||||
* [x] support signal mechanism in chapter 7/8(only works for apps with a single thread)
|
||||
* [x] Add boards/ directory and support rustdoc, for example you can use `cargo doc --no-deps --open` to view the documentation of a crate
|
||||
|
||||
### Todo(High priority)
|
||||
|
||||
* [ ] review documentation, current progress: 5/9
|
||||
* [ ] support user-level sync primitives in chapter 8
|
||||
* [ ] code of chapter 9: device drivers based on interrupts, including UART and block devices
|
||||
* [ ] use old fs image optionally, do not always rebuild the image
|
||||
* [ ] add new system calls: getdents64/fstat
|
||||
* [ ] shell functionality improvement(to be continued...)
|
||||
* [ ] give every non-zero process exit code an unique and clear error type
|
||||
* [ ] effective error handling of mm module
|
||||
|
||||
### Todo(Low priority)
|
||||
|
||||
* [ ] rewrite practice doc and remove some inproper questions
|
||||
* [ ] provide smooth debug experience at a Rust source code level
|
||||
* [ ] format the code using official tools
|
||||
|
||||
### Crates
|
||||
|
||||
We will add them later.
|
||||
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,18 @@
|
||||
# rCore-Tutorial-v3
|
||||
rCore-Tutorial version 3.x
|
||||
|
||||
## Dependency
|
||||
|
||||
### Binaries
|
||||
|
||||
* rustc: 1.57.0-nightly (e1e9319d9 2021-10-14)
|
||||
|
||||
* cargo-binutils: 0.3.3
|
||||
|
||||
* qemu: 5.0.0
|
||||
|
||||
* rustsbi-lib: 0.2.0-alpha.4
|
||||
|
||||
rustsbi-qemu: d4968dd2
|
||||
|
||||
rustsbi-k210: b689314e
|
@ -0,0 +1,2 @@
|
||||
pub const CLOCK_FREQ: usize = 403000000 / 62;
|
||||
pub const MEMORY_END: usize = 0x80600000;
|
@ -0,0 +1,2 @@
|
||||
pub const CLOCK_FREQ: usize = 12500000;
|
||||
pub const MEMORY_END: usize = 0x80800000;
|
@ -1,15 +1,12 @@
|
||||
//! Constants used in rCore
|
||||
pub const USER_STACK_SIZE: usize = 4096 * 2;
|
||||
pub const KERNEL_STACK_SIZE: usize = 4096 * 2;
|
||||
pub const KERNEL_HEAP_SIZE: usize = 0x20_0000;
|
||||
pub const MEMORY_END: usize = 0x80600000;
|
||||
|
||||
pub const PAGE_SIZE: usize = 0x1000;
|
||||
pub const PAGE_SIZE_BITS: usize = 0xc;
|
||||
|
||||
pub const TRAMPOLINE: usize = usize::MAX - PAGE_SIZE + 1;
|
||||
pub const TRAP_CONTEXT: usize = TRAMPOLINE - PAGE_SIZE;
|
||||
|
||||
#[cfg(feature = "board_k210")]
|
||||
pub const CPU_FREQ: usize = 10000000;
|
||||
|
||||
#[cfg(feature = "board_qemu")]
|
||||
pub const CPU_FREQ: usize = 12500000;
|
||||
pub use crate::board::{CLOCK_FREQ, MEMORY_END};
|
||||
|
@ -0,0 +1,53 @@
|
||||
OUTPUT_ARCH(riscv)
|
||||
ENTRY(_start)
|
||||
BASE_ADDRESS = 0x80200000;
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = BASE_ADDRESS;
|
||||
skernel = .;
|
||||
|
||||
stext = .;
|
||||
.text : {
|
||||
*(.text.entry)
|
||||
. = ALIGN(4K);
|
||||
strampoline = .;
|
||||
*(.text.trampoline);
|
||||
. = ALIGN(4K);
|
||||
*(.text .text.*)
|
||||
}
|
||||
|
||||
. = ALIGN(4K);
|
||||
etext = .;
|
||||
srodata = .;
|
||||
.rodata : {
|
||||
*(.rodata .rodata.*)
|
||||
*(.srodata .srodata.*)
|
||||
}
|
||||
|
||||
. = ALIGN(4K);
|
||||
erodata = .;
|
||||
sdata = .;
|
||||
.data : {
|
||||
*(.data .data.*)
|
||||
*(.sdata .sdata.*)
|
||||
}
|
||||
|
||||
. = ALIGN(4K);
|
||||
edata = .;
|
||||
sbss_with_stack = .;
|
||||
.bss : {
|
||||
*(.bss.stack)
|
||||
sbss = .;
|
||||
*(.bss .bss.*)
|
||||
*(.sbss .sbss.*)
|
||||
}
|
||||
|
||||
. = ALIGN(4K);
|
||||
ebss = .;
|
||||
ekernel = .;
|
||||
|
||||
/DISCARD/ : {
|
||||
*(.eh_frame)
|
||||
}
|
||||
}
|
@ -1,62 +1,70 @@
|
||||
use alloc::vec::Vec;
|
||||
//! Loading user applications into memory
|
||||
|
||||
/// Get the total number of applications.
|
||||
use alloc::vec::Vec;
|
||||
use lazy_static::*;
|
||||
///get app number
|
||||
pub fn get_num_app() -> usize {
|
||||
extern "C" { fn _num_app(); }
|
||||
extern "C" {
|
||||
fn _num_app();
|
||||
}
|
||||
unsafe { (_num_app as usize as *const usize).read_volatile() }
|
||||
}
|
||||
|
||||
/// get applications data
|
||||
pub fn get_app_data(app_id: usize) -> &'static [u8] {
|
||||
extern "C" { fn _num_app(); }
|
||||
extern "C" {
|
||||
fn _num_app();
|
||||
}
|
||||
let num_app_ptr = _num_app as usize as *const usize;
|
||||
let num_app = get_num_app();
|
||||
let app_start = unsafe {
|
||||
core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1)
|
||||
};
|
||||
let app_start = unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) };
|
||||
assert!(app_id < num_app);
|
||||
unsafe {
|
||||
core::slice::from_raw_parts(
|
||||
app_start[app_id] as *const u8,
|
||||
app_start[app_id + 1] - app_start[app_id]
|
||||
app_start[app_id + 1] - app_start[app_id],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn get_app_data_by_name(name: &str) -> Option<&'static [u8]> {
|
||||
let num_app = get_num_app();
|
||||
let app_names = app_names();
|
||||
(0..num_app)
|
||||
.find(|&i| app_names[i] == name)
|
||||
.map(|i| get_app_data(i))
|
||||
lazy_static! {
|
||||
///All of app's name
|
||||
static ref APP_NAMES: Vec<&'static str> = {
|
||||
let num_app = get_num_app();
|
||||
extern "C" {
|
||||
fn _app_names();
|
||||
}
|
||||
let mut start = _app_names as usize as *const u8;
|
||||
let mut v = Vec::new();
|
||||
unsafe {
|
||||
for _ in 0..num_app {
|
||||
let mut end = start;
|
||||
while end.read_volatile() != b'\0' {
|
||||
end = end.add(1);
|
||||
}
|
||||
let slice = core::slice::from_raw_parts(start, end as usize - start as usize);
|
||||
let str = core::str::from_utf8(slice).unwrap();
|
||||
v.push(str);
|
||||
start = end.add(1);
|
||||
}
|
||||
}
|
||||
v
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn app_names() -> Vec<&'static str> {
|
||||
///get app data from name
|
||||
pub fn get_app_data_by_name(name: &str) -> Option<&'static [u8]> {
|
||||
let num_app = get_num_app();
|
||||
extern "C" { fn _app_names(); }
|
||||
let mut start = _app_names as usize as *const u8;
|
||||
let mut v = Vec::new();
|
||||
unsafe {
|
||||
for _ in 0..num_app {
|
||||
let mut end = start;
|
||||
while end.read_volatile() != '\n' as u8 {
|
||||
end = end.add(1);
|
||||
}
|
||||
let slice = core::slice::from_raw_parts(start, end as usize - start as usize);
|
||||
let str = core::str::from_utf8(slice).unwrap();
|
||||
v.push(str);
|
||||
// Mention that there is a extra char between names
|
||||
start = end.add(2);
|
||||
}
|
||||
}
|
||||
v
|
||||
(0..num_app)
|
||||
.find(|&i| APP_NAMES[i] == name)
|
||||
.map(get_app_data)
|
||||
}
|
||||
|
||||
///list all apps
|
||||
pub fn list_apps() {
|
||||
let apps = app_names();
|
||||
println!("/**** APPS ****");
|
||||
for app in apps {
|
||||
for app in APP_NAMES.iter() {
|
||||
println!("{}", app);
|
||||
}
|
||||
println!("**************/")
|
||||
}
|
||||
println!("**************/");
|
||||
}
|
||||
|
@ -1,24 +1,26 @@
|
||||
mod heap_allocator;
|
||||
//! Memory management implementation
|
||||
//!
|
||||
//! SV39 page-based virtual-memory architecture for RV64 systems, and
|
||||
//! everything about memory management, like frame allocator, page table,
|
||||
//! map area and memory set, is implemented here.
|
||||
//!
|
||||
//! Every task or process has a memory_set to control its virtual memory.
|
||||
mod address;
|
||||
mod frame_allocator;
|
||||
mod page_table;
|
||||
mod heap_allocator;
|
||||
mod memory_set;
|
||||
mod page_table;
|
||||
|
||||
use page_table::{PageTable, PTEFlags};
|
||||
use address::{VPNRange, StepByOne};
|
||||
pub use address::{PhysAddr, VirtAddr, PhysPageNum, VirtPageNum};
|
||||
pub use frame_allocator::{FrameTracker, frame_alloc};
|
||||
pub use page_table::{
|
||||
PageTableEntry,
|
||||
translated_byte_buffer,
|
||||
translated_str,
|
||||
translated_refmut,
|
||||
};
|
||||
pub use memory_set::{MemorySet, KERNEL_SPACE, MapPermission};
|
||||
pub use address::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum};
|
||||
use address::{StepByOne, VPNRange};
|
||||
pub use frame_allocator::{frame_alloc, FrameTracker};
|
||||
pub use memory_set::remap_test;
|
||||
|
||||
pub use memory_set::{MapPermission, MemorySet, KERNEL_SPACE};
|
||||
pub use page_table::{translated_byte_buffer, translated_refmut, translated_str, PageTableEntry};
|
||||
use page_table::{PTEFlags, PageTable};
|
||||
/// initiate heap allocator, frame allocator and kernel space
|
||||
pub fn init() {
|
||||
heap_allocator::init_heap();
|
||||
frame_allocator::init_frame_allocator();
|
||||
KERNEL_SPACE.clone().lock().activate();
|
||||
KERNEL_SPACE.exclusive_access().activate();
|
||||
}
|
||||
|
@ -0,0 +1,4 @@
|
||||
//! Synchronization and interior mutability primitives
|
||||
mod up;
|
||||
|
||||
pub use up::UPSafeCell;
|
@ -0,0 +1,30 @@
|
||||
//! Uniprocessor interior mutability primitives
|
||||
use core::cell::{RefCell, RefMut};
|
||||
|
||||
/// Wrap a static data structure inside it so that we are
|
||||
/// able to access it without any `unsafe`.
|
||||
///
|
||||
/// We should only use it in uniprocessor.
|
||||
///
|
||||
/// In order to get mutable reference of inner data, call
|
||||
/// `exclusive_access`.
|
||||
pub struct UPSafeCell<T> {
|
||||
/// inner data
|
||||
inner: RefCell<T>,
|
||||
}
|
||||
|
||||
unsafe impl<T> Sync for UPSafeCell<T> {}
|
||||
|
||||
impl<T> UPSafeCell<T> {
|
||||
/// User is responsible to guarantee that inner struct is only used in
|
||||
/// uniprocessor.
|
||||
pub unsafe fn new(value: T) -> Self {
|
||||
Self {
|
||||
inner: RefCell::new(value),
|
||||
}
|
||||
}
|
||||
/// Exclusive access inner data in UPSafeCell. Panic if the data has been borrowed.
|
||||
pub fn exclusive_access(&self) -> RefMut<'_, T> {
|
||||
self.inner.borrow_mut()
|
||||
}
|
||||
}
|
@ -1,34 +1,41 @@
|
||||
//!Implementation of [`TaskManager`]
|
||||
use super::TaskControlBlock;
|
||||
use crate::sync::UPSafeCell;
|
||||
use alloc::collections::VecDeque;
|
||||
use alloc::sync::Arc;
|
||||
use spin::Mutex;
|
||||
use lazy_static::*;
|
||||
|
||||
///A array of `TaskControlBlock` that is thread-safe
|
||||
pub struct TaskManager {
|
||||
ready_queue: VecDeque<Arc<TaskControlBlock>>,
|
||||
}
|
||||
|
||||
/// A simple FIFO scheduler.
|
||||
impl TaskManager {
|
||||
///Creat an empty TaskManager
|
||||
pub fn new() -> Self {
|
||||
Self { ready_queue: VecDeque::new(), }
|
||||
Self {
|
||||
ready_queue: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
///Add a task to `TaskManager`
|
||||
pub fn add(&mut self, task: Arc<TaskControlBlock>) {
|
||||
self.ready_queue.push_back(task);
|
||||
}
|
||||
///Remove the first task and return it,or `None` if `TaskManager` is empty
|
||||
pub fn fetch(&mut self) -> Option<Arc<TaskControlBlock>> {
|
||||
self.ready_queue.pop_front()
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref TASK_MANAGER: Mutex<TaskManager> = Mutex::new(TaskManager::new());
|
||||
pub static ref TASK_MANAGER: UPSafeCell<TaskManager> =
|
||||
unsafe { UPSafeCell::new(TaskManager::new()) };
|
||||
}
|
||||
|
||||
///Interface offered to add task
|
||||
pub fn add_task(task: Arc<TaskControlBlock>) {
|
||||
TASK_MANAGER.lock().add(task);
|
||||
TASK_MANAGER.exclusive_access().add(task);
|
||||
}
|
||||
|
||||
///Interface offered to pop the first task
|
||||
pub fn fetch_task() -> Option<Arc<TaskControlBlock>> {
|
||||
TASK_MANAGER.lock().fetch()
|
||||
}
|
||||
TASK_MANAGER.exclusive_access().fetch()
|
||||
}
|
||||
|
@ -1,86 +1,100 @@
|
||||
//! Task management implementation
|
||||
//!
|
||||
//! Everything about task management, like starting and switching tasks is
|
||||
//! implemented here.
|
||||
//!
|
||||
//! A single global instance of [`TaskManager`] called `TASK_MANAGER` controls
|
||||
//! all the tasks in the whole operating system.
|
||||
//!
|
||||
//! A single global instance of [`Processor`] called `PROCESSOR` monitors running
|
||||
//! task(s) for each core.
|
||||
//!
|
||||
//! A single global instance of [`PidAllocator`] called `PID_ALLOCATOR` allocates
|
||||
//! pid for user apps.
|
||||
//!
|
||||
//! Be careful when you see `__switch` ASM function in `switch.S`. Control flow around this function
|
||||
//! might not be what you expect.
|
||||
mod context;
|
||||
mod switch;
|
||||
mod task;
|
||||
mod manager;
|
||||
mod processor;
|
||||
mod pid;
|
||||
mod processor;
|
||||
mod switch;
|
||||
#[allow(clippy::module_inception)]
|
||||
mod task;
|
||||
|
||||
use crate::loader::{get_app_data_by_name};
|
||||
use switch::__switch;
|
||||
use task::{TaskControlBlock, TaskStatus};
|
||||
use crate::loader::get_app_data_by_name;
|
||||
use alloc::sync::Arc;
|
||||
use manager::fetch_task;
|
||||
use lazy_static::*;
|
||||
pub use manager::{fetch_task, TaskManager};
|
||||
use switch::__switch;
|
||||
use task::{TaskControlBlock, TaskStatus};
|
||||
|
||||
pub use context::TaskContext;
|
||||
pub use manager::add_task;
|
||||
pub use pid::{pid_alloc, KernelStack, PidAllocator, PidHandle};
|
||||
pub use processor::{
|
||||
run_tasks,
|
||||
current_task,
|
||||
current_user_token,
|
||||
current_trap_cx,
|
||||
take_current_task,
|
||||
schedule,
|
||||
current_task, current_trap_cx, current_user_token, run_tasks, schedule, take_current_task,
|
||||
Processor,
|
||||
};
|
||||
pub use manager::add_task;
|
||||
pub use pid::{PidHandle, pid_alloc, KernelStack};
|
||||
|
||||
/// Suspend the current 'Running' task and run the next task in task list.
|
||||
pub fn suspend_current_and_run_next() {
|
||||
// There must be an application running.
|
||||
let task = take_current_task().unwrap();
|
||||
|
||||
// ---- temporarily hold current PCB lock
|
||||
let task_cx_ptr = task.acquire_inner_lock().get_task_cx_ptr2();
|
||||
// ---- release current PCB lock
|
||||
|
||||
// ++++ temporarily hold current PCB lock
|
||||
// ---- access current TCB exclusively
|
||||
let mut task_inner = task.inner_exclusive_access();
|
||||
let task_cx_ptr = &mut task_inner.task_cx as *mut TaskContext;
|
||||
// Change status to Ready
|
||||
task.acquire_inner_lock().task_status = TaskStatus::Ready;
|
||||
// ++++ release current PCB lock
|
||||
task_inner.task_status = TaskStatus::Ready;
|
||||
drop(task_inner);
|
||||
// ---- release current PCB
|
||||
|
||||
// push back to ready queue.
|
||||
add_task(task);
|
||||
// jump to scheduling cycle
|
||||
schedule(task_cx_ptr);
|
||||
}
|
||||
|
||||
/// Exit the current 'Running' task and run the next task in task list.
|
||||
pub fn exit_current_and_run_next(exit_code: i32) {
|
||||
// take from Processor
|
||||
let task = take_current_task().unwrap();
|
||||
// **** hold current PCB lock
|
||||
let mut inner = task.acquire_inner_lock();
|
||||
// **** access current TCB exclusively
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
// Change status to Zombie
|
||||
inner.task_status = TaskStatus::Zombie;
|
||||
// Record exit code
|
||||
inner.exit_code = exit_code;
|
||||
// do not move to its parent but under initproc
|
||||
|
||||
// ++++++ hold initproc PCB lock here
|
||||
// ++++++ access initproc TCB exclusively
|
||||
{
|
||||
let mut initproc_inner = INITPROC.acquire_inner_lock();
|
||||
let mut initproc_inner = INITPROC.inner_exclusive_access();
|
||||
for child in inner.children.iter() {
|
||||
child.inner_exclusive_access().parent = Some(Arc::downgrade(&INITPROC));
|
||||
initproc_inner.children.push(child.clone());
|
||||
}
|
||||
}
|
||||
// ++++++ release parent PCB lock here
|
||||
// ++++++ release parent PCB
|
||||
|
||||
inner.children.clear();
|
||||
// deallocate user space
|
||||
inner.memory_set.recycle_data_pages();
|
||||
drop(inner);
|
||||
// **** release current PCB lock
|
||||
// **** release current PCB
|
||||
// drop task manually to maintain rc correctly
|
||||
drop(task);
|
||||
// we do not have to save task context
|
||||
let _unused: usize = 0;
|
||||
schedule(&_unused as *const _);
|
||||
let mut _unused = TaskContext::zero_init();
|
||||
schedule(&mut _unused as *mut _);
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref INITPROC: Arc<TaskControlBlock> = Arc::new(
|
||||
TaskControlBlock::new(get_app_data_by_name("initproc").unwrap())
|
||||
);
|
||||
///Globle process that init user shell
|
||||
pub static ref INITPROC: Arc<TaskControlBlock> = Arc::new(TaskControlBlock::new(
|
||||
get_app_data_by_name("initproc").unwrap()
|
||||
));
|
||||
}
|
||||
|
||||
///Add init process to the manager
|
||||
pub fn add_initproc() {
|
||||
add_task(INITPROC.clone());
|
||||
}
|
||||
|
@ -1,93 +1,93 @@
|
||||
use super::TaskControlBlock;
|
||||
use alloc::sync::Arc;
|
||||
use spin::Mutex;
|
||||
use lazy_static::*;
|
||||
use super::{fetch_task, TaskStatus};
|
||||
//!Implementation of [`Processor`] and Intersection of control flow
|
||||
use super::__switch;
|
||||
use super::{fetch_task, TaskStatus};
|
||||
use super::{TaskContext, TaskControlBlock};
|
||||
use crate::sync::UPSafeCell;
|
||||
use crate::trap::TrapContext;
|
||||
|
||||
use alloc::sync::Arc;
|
||||
use lazy_static::*;
|
||||
///Processor management structure
|
||||
pub struct Processor {
|
||||
inner: Mutex<ProcessorInner>,
|
||||
}
|
||||
|
||||
unsafe impl Sync for Processor {}
|
||||
|
||||
struct ProcessorInner {
|
||||
///The task currently executing on the current processor
|
||||
current: Option<Arc<TaskControlBlock>>,
|
||||
idle_task_cx_ptr: usize,
|
||||
///The basic control flow of each core, helping to select and switch process
|
||||
idle_task_cx: TaskContext,
|
||||
}
|
||||
|
||||
impl Processor {
|
||||
///Create an empty Processor
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: Mutex::new(ProcessorInner {
|
||||
current: None,
|
||||
idle_task_cx_ptr: 0,
|
||||
}),
|
||||
current: None,
|
||||
idle_task_cx: TaskContext::zero_init(),
|
||||
}
|
||||
}
|
||||
fn get_idle_task_cx_ptr2(&self) -> *const usize {
|
||||
let inner = self.inner.lock();
|
||||
&inner.idle_task_cx_ptr as *const usize
|
||||
}
|
||||
pub fn run(&self) {
|
||||
loop {
|
||||
if let Some(task) = fetch_task() {
|
||||
let idle_task_cx_ptr = self.get_idle_task_cx_ptr2();
|
||||
// acquire
|
||||
let next_task_cx_ptr = task.acquire_inner_lock().get_task_cx_ptr2();
|
||||
task.acquire_inner_lock().task_status = TaskStatus::Running;
|
||||
// release
|
||||
self.inner.lock().current = Some(task);
|
||||
unsafe {
|
||||
__switch(
|
||||
idle_task_cx_ptr,
|
||||
next_task_cx_ptr,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
///Get mutable reference to `idle_task_cx`
|
||||
fn get_idle_task_cx_ptr(&mut self) -> *mut TaskContext {
|
||||
&mut self.idle_task_cx as *mut _
|
||||
}
|
||||
pub fn take_current(&self) -> Option<Arc<TaskControlBlock>> {
|
||||
self.inner.lock().current.take()
|
||||
///Get current task in moving semanteme
|
||||
pub fn take_current(&mut self) -> Option<Arc<TaskControlBlock>> {
|
||||
self.current.take()
|
||||
}
|
||||
///Get current task in cloning semanteme
|
||||
pub fn current(&self) -> Option<Arc<TaskControlBlock>> {
|
||||
self.inner.lock().current.as_ref().map(|task| task.clone())
|
||||
self.current.as_ref().map(Arc::clone)
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref PROCESSOR: Processor = Processor::new();
|
||||
pub static ref PROCESSOR: UPSafeCell<Processor> = unsafe { UPSafeCell::new(Processor::new()) };
|
||||
}
|
||||
|
||||
///The main part of process execution and scheduling
|
||||
///Loop `fetch_task` to get the process that needs to run, and switch the process through `__switch`
|
||||
pub fn run_tasks() {
|
||||
PROCESSOR.run();
|
||||
loop {
|
||||
let mut processor = PROCESSOR.exclusive_access();
|
||||
if let Some(task) = fetch_task() {
|
||||
let idle_task_cx_ptr = processor.get_idle_task_cx_ptr();
|
||||
// access coming task TCB exclusively
|
||||
let mut task_inner = task.inner_exclusive_access();
|
||||
let next_task_cx_ptr = &task_inner.task_cx as *const TaskContext;
|
||||
task_inner.task_status = TaskStatus::Running;
|
||||
drop(task_inner);
|
||||
// release coming task TCB manually
|
||||
processor.current = Some(task);
|
||||
// release processor manually
|
||||
drop(processor);
|
||||
unsafe {
|
||||
__switch(idle_task_cx_ptr, next_task_cx_ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///Take the current task,leaving a None in its place
|
||||
pub fn take_current_task() -> Option<Arc<TaskControlBlock>> {
|
||||
PROCESSOR.take_current()
|
||||
PROCESSOR.exclusive_access().take_current()
|
||||
}
|
||||
|
||||
///Get running task
|
||||
pub fn current_task() -> Option<Arc<TaskControlBlock>> {
|
||||
PROCESSOR.current()
|
||||
PROCESSOR.exclusive_access().current()
|
||||
}
|
||||
|
||||
///Get token of the address space of current task
|
||||
pub fn current_user_token() -> usize {
|
||||
let task = current_task().unwrap();
|
||||
let token = task.acquire_inner_lock().get_user_token();
|
||||
let token = task.inner_exclusive_access().get_user_token();
|
||||
token
|
||||
}
|
||||
|
||||
///Get the mutable reference to trap context of current task
|
||||
pub fn current_trap_cx() -> &'static mut TrapContext {
|
||||
current_task().unwrap().acquire_inner_lock().get_trap_cx()
|
||||
current_task()
|
||||
.unwrap()
|
||||
.inner_exclusive_access()
|
||||
.get_trap_cx()
|
||||
}
|
||||
|
||||
pub fn schedule(switched_task_cx_ptr2: *const usize) {
|
||||
let idle_task_cx_ptr2 = PROCESSOR.get_idle_task_cx_ptr2();
|
||||
///Return to idle control flow for new scheduling
|
||||
pub fn schedule(switched_task_cx_ptr: *mut TaskContext) {
|
||||
let mut processor = PROCESSOR.exclusive_access();
|
||||
let idle_task_cx_ptr = processor.get_idle_task_cx_ptr();
|
||||
drop(processor);
|
||||
unsafe {
|
||||
__switch(
|
||||
switched_task_cx_ptr2,
|
||||
idle_task_cx_ptr2,
|
||||
);
|
||||
__switch(switched_task_cx_ptr, idle_task_cx_ptr);
|
||||
}
|
||||
}
|
||||
|
@ -1,34 +1,34 @@
|
||||
.altmacro
|
||||
.macro SAVE_SN n
|
||||
sd s\n, (\n+1)*8(sp)
|
||||
sd s\n, (\n+2)*8(a0)
|
||||
.endm
|
||||
.macro LOAD_SN n
|
||||
ld s\n, (\n+1)*8(sp)
|
||||
ld s\n, (\n+2)*8(a1)
|
||||
.endm
|
||||
.section .text
|
||||
.globl __switch
|
||||
__switch:
|
||||
# __switch(current_task_cx: &*const TaskContext, next_task_cx: &*const TaskContext)
|
||||
# push TaskContext to current sp and save its address to where a0 points to
|
||||
addi sp, sp, -13*8
|
||||
sd sp, 0(a0)
|
||||
# fill TaskContext with ra & s0-s11
|
||||
sd ra, 0(sp)
|
||||
# __switch(
|
||||
# current_task_cx_ptr: *mut TaskContext,
|
||||
# next_task_cx_ptr: *const TaskContext
|
||||
# )
|
||||
# save kernel stack of current task
|
||||
sd sp, 8(a0)
|
||||
# save ra & s0~s11 of current execution
|
||||
sd ra, 0(a0)
|
||||
.set n, 0
|
||||
.rept 12
|
||||
SAVE_SN %n
|
||||
.set n, n + 1
|
||||
.endr
|
||||
# ready for loading TaskContext a1 points to
|
||||
ld sp, 0(a1)
|
||||
# load registers in the TaskContext
|
||||
ld ra, 0(sp)
|
||||
# restore ra & s0~s11 of next execution
|
||||
ld ra, 0(a1)
|
||||
.set n, 0
|
||||
.rept 12
|
||||
LOAD_SN %n
|
||||
.set n, n + 1
|
||||
.endr
|
||||
# pop TaskContext
|
||||
addi sp, sp, 13*8
|
||||
# restore kernel stack of next task
|
||||
ld sp, 8(a1)
|
||||
ret
|
||||
|
||||
|
@ -1,5 +1,9 @@
|
||||
//!Wrap `switch.S` as a function
|
||||
use super::TaskContext;
|
||||
use core::arch::global_asm;
|
||||
|
||||
global_asm!(include_str!("switch.S"));
|
||||
|
||||
extern "C" {
|
||||
pub fn __switch(current_task_cx: *const usize, next_task_cx: *const usize);
|
||||
pub fn __switch(current_task_cx_ptr: *mut TaskContext, next_task_cx_ptr: *const TaskContext);
|
||||
}
|
||||
|
@ -1,18 +1,20 @@
|
||||
use riscv::register::time;
|
||||
//! RISC-V timer-related functionality
|
||||
|
||||
use crate::config::CLOCK_FREQ;
|
||||
use crate::sbi::set_timer;
|
||||
use crate::config::CPU_FREQ;
|
||||
use riscv::register::time;
|
||||
|
||||
const TICKS_PER_SEC: usize = 100;
|
||||
const MSEC_PER_SEC: usize = 1000;
|
||||
|
||||
///get current time
|
||||
pub fn get_time() -> usize {
|
||||
time::read()
|
||||
}
|
||||
|
||||
/// get current time in microseconds
|
||||
pub fn get_time_ms() -> usize {
|
||||
time::read() / (CPU_FREQ / MSEC_PER_SEC)
|
||||
time::read() / (CLOCK_FREQ / MSEC_PER_SEC)
|
||||
}
|
||||
|
||||
/// set the next timer interrupt
|
||||
pub fn set_next_trigger() {
|
||||
set_timer(get_time() + CPU_FREQ / TICKS_PER_SEC);
|
||||
}
|
||||
set_timer(get_time() + CLOCK_FREQ / TICKS_PER_SEC);
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
nightly-2020-11-01
|
||||
nightly-2022-04-11
|
||||
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,35 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
use user_lib::{exec, fork, getpid, wait};
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
println!("pid {}: parent start forking ...", getpid());
|
||||
let pid = fork();
|
||||
if pid == 0 {
|
||||
// child process
|
||||
println!(
|
||||
"pid {}: forked child start execing hello_world app ... ",
|
||||
getpid()
|
||||
);
|
||||
exec("hello_world");
|
||||
100
|
||||
} else {
|
||||
// parent process
|
||||
let mut exit_code: i32 = 0;
|
||||
println!("pid {}: ready waiting child ...", getpid());
|
||||
assert_eq!(pid, wait(&mut exit_code));
|
||||
assert_eq!(exit_code, 0);
|
||||
println!(
|
||||
"pid {}: got child info:: pid {}, exit code: {}",
|
||||
getpid(),
|
||||
pid,
|
||||
exit_code
|
||||
);
|
||||
0
|
||||
}
|
||||
}
|
Loading…
Reference in new issue