commit
18d4a89ab7
@ -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,16 +1,13 @@
|
||||
.idea/*
|
||||
os/target/*
|
||||
os/.idea/*
|
||||
.*/*
|
||||
!.github/*
|
||||
!.vscode/settings.json
|
||||
|
||||
**/target/
|
||||
**/Cargo.lock
|
||||
|
||||
os/src/link_app.S
|
||||
os/src/linker.ld
|
||||
os/last-*
|
||||
os/Cargo.lock
|
||||
os/last-*
|
||||
user/target/*
|
||||
user/.idea/*
|
||||
user/Cargo.lock
|
||||
easy-fs/Cargo.lock
|
||||
easy-fs/target/*
|
||||
easy-fs-fuse/Cargo.lock
|
||||
easy-fs-fuse/target/*
|
||||
os/.gdb_history
|
||||
tools/
|
||||
pushall.sh
|
||||
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,6 +1,7 @@
|
||||
use core::any::Any;
|
||||
|
||||
pub trait BlockDevice : Send + Sync + Any {
|
||||
pub trait BlockDevice: Send + Sync + Any {
|
||||
fn read_block(&self, block_id: usize, buf: &mut [u8]);
|
||||
fn write_block(&self, block_id: usize, buf: &[u8]);
|
||||
fn handle_irq(&self);
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
pub const CLOCK_FREQ: usize = 403000000 / 62;
|
||||
|
||||
pub const MMIO: &[(usize, usize)] = &[
|
||||
// we don't need clint in S priv when running
|
||||
// we only need claim/complete for target0 after initializing
|
||||
(0x0C00_0000, 0x3000), /* PLIC */
|
||||
(0x0C20_0000, 0x1000), /* PLIC */
|
||||
(0x3800_0000, 0x1000), /* UARTHS */
|
||||
(0x3800_1000, 0x1000), /* GPIOHS */
|
||||
(0x5020_0000, 0x1000), /* GPIO */
|
||||
(0x5024_0000, 0x1000), /* SPI_SLAVE */
|
||||
(0x502B_0000, 0x1000), /* FPIOA */
|
||||
(0x502D_0000, 0x1000), /* TIMER0 */
|
||||
(0x502E_0000, 0x1000), /* TIMER1 */
|
||||
(0x502F_0000, 0x1000), /* TIMER2 */
|
||||
(0x5044_0000, 0x1000), /* SYSCTL */
|
||||
(0x5200_0000, 0x1000), /* SPI0 */
|
||||
(0x5300_0000, 0x1000), /* SPI1 */
|
||||
(0x5400_0000, 0x1000), /* SPI2 */
|
||||
];
|
||||
|
||||
pub type BlockDeviceImpl = crate::drivers::block::SDCardWrapper;
|
||||
|
||||
pub fn device_init() {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
pub fn irq_handler() {
|
||||
unimplemented!();
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
pub const CLOCK_FREQ: usize = 12500000;
|
||||
|
||||
pub const MMIO: &[(usize, usize)] = &[
|
||||
(0x1000_0000, 0x1000),
|
||||
(0x1000_1000, 0x1000),
|
||||
(0xC00_0000, 0x40_0000),
|
||||
];
|
||||
|
||||
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);
|
||||
}
|
@ -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 @@
|
||||
mod block;
|
||||
pub mod block;
|
||||
pub mod chardev;
|
||||
pub mod plic;
|
||||
|
||||
pub use block::BLOCK_DEVICE;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +1,16 @@
|
||||
mod inode;
|
||||
mod pipe;
|
||||
mod stdio;
|
||||
mod inode;
|
||||
|
||||
use crate::mm::UserBuffer;
|
||||
|
||||
pub trait File : Send + Sync {
|
||||
pub trait File: Send + Sync {
|
||||
fn readable(&self) -> bool;
|
||||
fn writable(&self) -> bool;
|
||||
fn read(&self, buf: UserBuffer) -> usize;
|
||||
fn write(&self, buf: UserBuffer) -> usize;
|
||||
}
|
||||
|
||||
pub use pipe::{Pipe, make_pipe};
|
||||
pub use inode::{list_apps, open_file, OSInode, OpenFlags};
|
||||
pub use pipe::{make_pipe, Pipe};
|
||||
pub use stdio::{Stdin, Stdout};
|
||||
pub use inode::{OSInode, open_file, OpenFlags, list_apps};
|
@ -1,62 +0,0 @@
|
||||
use alloc::vec::Vec;
|
||||
use lazy_static::*;
|
||||
|
||||
pub fn get_num_app() -> usize {
|
||||
extern "C" { fn _num_app(); }
|
||||
unsafe { (_num_app as usize as *const usize).read_volatile() }
|
||||
}
|
||||
|
||||
pub fn get_app_data(app_id: usize) -> &'static [u8] {
|
||||
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)
|
||||
};
|
||||
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]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
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() != '\0' 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);
|
||||
start = end.add(1);
|
||||
}
|
||||
}
|
||||
v
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn get_app_data_by_name(name: &str) -> Option<&'static [u8]> {
|
||||
let num_app = get_num_app();
|
||||
(0..num_app)
|
||||
.find(|&i| APP_NAMES[i] == name)
|
||||
.map(|i| get_app_data(i))
|
||||
}
|
||||
|
||||
pub fn list_apps() {
|
||||
println!("/**** APPS ****");
|
||||
for app in APP_NAMES.iter() {
|
||||
println!("{}", app);
|
||||
}
|
||||
println!("**************/");
|
||||
}
|
@ -1,28 +1,22 @@
|
||||
mod heap_allocator;
|
||||
mod address;
|
||||
mod frame_allocator;
|
||||
mod page_table;
|
||||
mod heap_allocator;
|
||||
mod memory_set;
|
||||
mod page_table;
|
||||
|
||||
use page_table::PTEFlags;
|
||||
use address::VPNRange;
|
||||
pub use address::{PhysAddr, VirtAddr, PhysPageNum, VirtPageNum, StepByOne};
|
||||
pub use frame_allocator::{FrameTracker, frame_alloc, frame_dealloc,};
|
||||
pub use address::{PhysAddr, PhysPageNum, StepByOne, VirtAddr, VirtPageNum};
|
||||
pub use frame_allocator::{frame_alloc, frame_dealloc, FrameTracker};
|
||||
pub use memory_set::remap_test;
|
||||
pub use memory_set::{kernel_token, MapPermission, MemorySet, KERNEL_SPACE};
|
||||
use page_table::PTEFlags;
|
||||
pub use page_table::{
|
||||
PageTable,
|
||||
PageTableEntry,
|
||||
translated_byte_buffer,
|
||||
translated_str,
|
||||
translated_ref,
|
||||
translated_refmut,
|
||||
UserBuffer,
|
||||
UserBufferIterator,
|
||||
translated_byte_buffer, translated_ref, translated_refmut, translated_str, PageTable,
|
||||
PageTableEntry, UserBuffer, UserBufferIterator,
|
||||
};
|
||||
pub use memory_set::{MemorySet, KERNEL_SPACE, MapPermission, kernel_token};
|
||||
pub use memory_set::remap_test;
|
||||
|
||||
pub fn init() {
|
||||
heap_allocator::init_heap();
|
||||
frame_allocator::init_frame_allocator();
|
||||
KERNEL_SPACE.exclusive_access().activate();
|
||||
}
|
||||
}
|
||||
|
@ -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,7 +1,9 @@
|
||||
mod up;
|
||||
mod condvar;
|
||||
mod mutex;
|
||||
mod semaphore;
|
||||
mod up;
|
||||
|
||||
pub use up::UPSafeCell;
|
||||
pub use mutex::{Mutex, MutexSpin, MutexBlocking};
|
||||
pub use condvar::Condvar;
|
||||
pub use mutex::{Mutex, MutexBlocking, MutexSpin};
|
||||
pub use semaphore::Semaphore;
|
||||
pub use up::{UPIntrFreeCell, UPIntrRefMut};
|
||||
|
@ -0,0 +1,29 @@
|
||||
use bitflags::*;
|
||||
|
||||
bitflags! {
|
||||
pub struct SignalFlags: u32 {
|
||||
const SIGINT = 1 << 2;
|
||||
const SIGILL = 1 << 4;
|
||||
const SIGABRT = 1 << 6;
|
||||
const SIGFPE = 1 << 8;
|
||||
const SIGSEGV = 1 << 11;
|
||||
}
|
||||
}
|
||||
|
||||
impl SignalFlags {
|
||||
pub fn check_error(&self) -> Option<(i32, &'static str)> {
|
||||
if self.contains(Self::SIGINT) {
|
||||
Some((-2, "Killed, SIGINT=2"))
|
||||
} else if self.contains(Self::SIGILL) {
|
||||
Some((-4, "Illegal Instruction, SIGILL=4"))
|
||||
} else if self.contains(Self::SIGABRT) {
|
||||
Some((-6, "Aborted, SIGABRT=6"))
|
||||
} else if self.contains(Self::SIGFPE) {
|
||||
Some((-8, "Erroneous Arithmetic Operation, SIGFPE=8"))
|
||||
} else if self.contains(Self::SIGSEGV) {
|
||||
Some((-11, "Segmentation Fault, SIGSEGV=11"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,8 @@
|
||||
global_asm!(include_str!("switch.S"));
|
||||
|
||||
use super::TaskContext;
|
||||
use core::arch::global_asm;
|
||||
|
||||
global_asm!(include_str!("switch.S"));
|
||||
|
||||
extern "C" {
|
||||
pub fn __switch(
|
||||
current_task_cx_ptr: *mut TaskContext,
|
||||
next_task_cx_ptr: *const TaskContext
|
||||
);
|
||||
pub fn __switch(current_task_cx_ptr: *mut TaskContext, next_task_cx_ptr: *const TaskContext);
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
nightly-2021-10-15
|
||||
nightly-2022-04-11
|
||||
|
@ -0,0 +1,30 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
use user_lib::read;
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main(_argc: usize, _argv: &[&str]) -> i32 {
|
||||
let mut buf = [0u8; 256];
|
||||
let mut lines = 0usize;
|
||||
let mut total_size = 0usize;
|
||||
loop {
|
||||
let len = read(0, &mut buf) as usize;
|
||||
if len == 0 {
|
||||
break;
|
||||
}
|
||||
total_size += len;
|
||||
let string = core::str::from_utf8(&buf[..len]).unwrap();
|
||||
lines += string
|
||||
.chars()
|
||||
.fold(0, |acc, c| acc + if c == '\n' { 1 } else { 0 });
|
||||
}
|
||||
if total_size > 0 {
|
||||
lines += 1;
|
||||
}
|
||||
println!("{}", lines);
|
||||
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,10 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![allow(clippy::empty_loop)]
|
||||
|
||||
extern crate user_lib;
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main(_argc: usize, _argv: &[&str]) -> ! {
|
||||
loop {}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
use riscv::register::sstatus::{self, SPP};
|
||||
|
||||
#[no_mangle]
|
||||
fn main() -> i32 {
|
||||
println!("Try to access privileged CSR in U Mode");
|
||||
println!("Kernel should kill this application!");
|
||||
unsafe {
|
||||
sstatus::set_spp(SPP::User);
|
||||
}
|
||||
0
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
use core::arch::asm;
|
||||
|
||||
#[no_mangle]
|
||||
fn main() -> i32 {
|
||||
println!("Try to execute privileged instruction in U Mode");
|
||||
println!("Kernel should kill this application!");
|
||||
unsafe {
|
||||
asm!("sret");
|
||||
}
|
||||
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,15 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
#[no_mangle]
|
||||
fn main() -> i32 {
|
||||
println!("Into Test store_fault, we will insert an invalid store operation...");
|
||||
println!("Kernel should kill this application!");
|
||||
unsafe {
|
||||
core::ptr::null_mut::<u8>().write_volatile(0);
|
||||
}
|
||||
0
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue