mirror of
https://github.com/LearningOS/rust-based-os-comp2022.git
synced 2026-05-09 23:31:29 +08:00
add os[1-8]-ref for os refereces, add guide, add README
This commit is contained in:
16
os6-ref/src/config.rs
Normal file
16
os6-ref/src/config.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
//! 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 = 0x88000000;
|
||||
pub const PAGE_SIZE: usize = 0x1000;
|
||||
pub const PAGE_SIZE_BITS: usize = 0xc;
|
||||
pub const MAX_SYSCALL_NUM: usize = 500;
|
||||
|
||||
pub const TRAMPOLINE: usize = usize::MAX - PAGE_SIZE + 1;
|
||||
pub const TRAP_CONTEXT: usize = TRAMPOLINE - PAGE_SIZE;
|
||||
pub const CLOCK_FREQ: usize = 12500000;
|
||||
pub const MMIO: &[(usize, usize)] = &[
|
||||
(0x10001000, 0x1000),
|
||||
];
|
||||
130
os6-ref/src/console.rs
Normal file
130
os6-ref/src/console.rs
Normal file
@@ -0,0 +1,130 @@
|
||||
//! SBI console driver, for text output
|
||||
|
||||
use crate::sbi::console_putchar;
|
||||
use core::fmt::{self, Write};
|
||||
|
||||
struct Stdout;
|
||||
|
||||
impl Write for Stdout {
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
for c in s.chars() {
|
||||
console_putchar(c as usize);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print(args: fmt::Arguments) {
|
||||
Stdout.write_fmt(args).unwrap();
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
/// print string macro
|
||||
macro_rules! print {
|
||||
($fmt: literal $(, $($arg: tt)+)?) => {
|
||||
$crate::console::print(format_args!($fmt $(, $($arg)+)?));
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
/// println string macro
|
||||
macro_rules! println {
|
||||
($fmt: literal $(, $($arg: tt)+)?) => {
|
||||
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
以下代码提供了与颜色相关的 ANSI 转义字符,以及彩色输出可以使用的函数与宏。
|
||||
|
||||
可以使用它们,甚至扩展它们,来提升开发体验和显示效果。
|
||||
*/
|
||||
|
||||
// 使用 ANSI 转义字符来加上颜色
|
||||
#[macro_export]
|
||||
macro_rules! colorize {
|
||||
($content: ident, $foreground_color: ident) => {
|
||||
format_args!("\u{1B}[{}m{}\u{1B}[0m", $foreground_color as u8, $content)
|
||||
};
|
||||
($content: ident, $foreground_color: ident, $background_color: ident) => {
|
||||
format_args!(
|
||||
"\u{1B}[{}m\u{1B}[{}m{}\u{1B}[0m",
|
||||
$foreground_color.into(),
|
||||
$background_color.into(),
|
||||
$content
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
pub fn print_colorized(
|
||||
args: fmt::Arguments,
|
||||
foreground_color: impl Into<u8>,
|
||||
background_color: impl Into<u8>,
|
||||
) {
|
||||
Stdout
|
||||
.write_fmt(colorize!(args, foreground_color, background_color))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! print_colorized {
|
||||
($fmt: literal, $foreground_color: expr, $background_color: expr $(, $($arg: tt)+)?) => {
|
||||
$crate::console::print_colorized(format_args!($fmt $(, $($arg)+)?), $foreground_color as u8, $background_color as u8);
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! println_colorized {
|
||||
($fmt: literal, $foreground_color: expr, $background_color: expr $(, $($arg: tt)+)?) => {
|
||||
$crate::console::print_colorized(format_args!(concat!($fmt, "\n") $(, $($arg)+)?), $foreground_color as u8, $background_color as u8);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub enum ANSICON {
|
||||
Reset = 0,
|
||||
Bold = 1,
|
||||
Underline = 4,
|
||||
Blink = 5,
|
||||
Reverse = 7,
|
||||
FgBlack = 30,
|
||||
FgRed = 31,
|
||||
FgGreen = 32,
|
||||
FgYellow = 33,
|
||||
FgBlue = 34,
|
||||
FgMagenta = 35,
|
||||
FgCyan = 36,
|
||||
FgWhite = 37,
|
||||
FgDefault = 39,
|
||||
FgLightGray = 90,
|
||||
FgLightRed = 91,
|
||||
FgLightGreen = 92,
|
||||
FgLightYellow = 93,
|
||||
FgLightBlue = 94,
|
||||
FgLightMagenta = 95,
|
||||
FgLightCyan = 96,
|
||||
FgLightWhite = 97,
|
||||
BgBlack = 40,
|
||||
BgRed = 41,
|
||||
BgGreen = 42,
|
||||
BgYellow = 43,
|
||||
BgBlue = 44,
|
||||
BgMagenta = 45,
|
||||
BgCyan = 46,
|
||||
BgWhite = 47,
|
||||
BgDefault = 49,
|
||||
BgLightGray = 100,
|
||||
BgLightRed = 101,
|
||||
BgLightGreen = 102,
|
||||
BgLightYellow = 103,
|
||||
BgLightBlue = 104,
|
||||
BgLightMagenta = 105,
|
||||
BgLightCyan = 106,
|
||||
BgLightWhite = 107,
|
||||
}
|
||||
|
||||
impl From<ANSICON> for u8 {
|
||||
fn from(con: ANSICON) -> Self {
|
||||
con as Self
|
||||
}
|
||||
}
|
||||
24
os6-ref/src/drivers/block/mod.rs
Normal file
24
os6-ref/src/drivers/block/mod.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
mod virtio_blk;
|
||||
|
||||
use lazy_static::*;
|
||||
use alloc::sync::Arc;
|
||||
use easy_fs::BlockDevice;
|
||||
type BlockDeviceImpl = virtio_blk::VirtIOBlock;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref BLOCK_DEVICE: Arc<dyn BlockDevice> = Arc::new(BlockDeviceImpl::new());
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn block_device_test() {
|
||||
let block_device = BLOCK_DEVICE.clone();
|
||||
let mut write_buffer = [0u8; 512];
|
||||
let mut read_buffer = [0u8; 512];
|
||||
for i in 0..512 {
|
||||
for byte in write_buffer.iter_mut() { *byte = i as u8; }
|
||||
block_device.write_block(i as usize, &write_buffer);
|
||||
block_device.read_block(i as usize, &mut read_buffer);
|
||||
assert_eq!(write_buffer, read_buffer);
|
||||
}
|
||||
println!("block device test passed!");
|
||||
}
|
||||
84
os6-ref/src/drivers/block/virtio_blk.rs
Normal file
84
os6-ref/src/drivers/block/virtio_blk.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
|
||||
use virtio_drivers::{VirtIOBlk, VirtIOHeader};
|
||||
use crate::mm::{
|
||||
PhysAddr,
|
||||
VirtAddr,
|
||||
frame_alloc,
|
||||
frame_dealloc,
|
||||
PhysPageNum,
|
||||
FrameTracker,
|
||||
PageTable,
|
||||
StepByOne,
|
||||
kernel_token,
|
||||
};
|
||||
use super::BlockDevice;
|
||||
use crate::sync::UPSafeCell;
|
||||
use alloc::vec::Vec;
|
||||
use lazy_static::*;
|
||||
|
||||
#[allow(unused)]
|
||||
const VIRTIO0: usize = 0x10001000;
|
||||
|
||||
pub struct VirtIOBlock(UPSafeCell<VirtIOBlk<'static>>);
|
||||
|
||||
lazy_static! {
|
||||
static ref QUEUE_FRAMES: UPSafeCell<Vec<FrameTracker>> = unsafe {
|
||||
UPSafeCell::new(Vec::new())
|
||||
};
|
||||
}
|
||||
|
||||
impl BlockDevice for VirtIOBlock {
|
||||
fn read_block(&self, block_id: usize, buf: &mut [u8]) {
|
||||
self.0.exclusive_access()
|
||||
.read_block(block_id, buf)
|
||||
.expect("Error when reading VirtIOBlk");
|
||||
}
|
||||
fn write_block(&self, block_id: usize, buf: &[u8]) {
|
||||
self.0.exclusive_access()
|
||||
.write_block(block_id, buf)
|
||||
.expect("Error when writing VirtIOBlk");
|
||||
}
|
||||
}
|
||||
|
||||
impl VirtIOBlock {
|
||||
#[allow(unused)]
|
||||
pub fn new() -> Self {
|
||||
unsafe {
|
||||
Self(UPSafeCell::new(VirtIOBlk::new(
|
||||
&mut *(VIRTIO0 as *mut VirtIOHeader)
|
||||
).unwrap()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn virtio_dma_alloc(pages: usize) -> PhysAddr {
|
||||
let mut ppn_base = PhysPageNum(0);
|
||||
for i in 0..pages {
|
||||
let frame = frame_alloc().unwrap();
|
||||
if i == 0 { ppn_base = frame.ppn; }
|
||||
assert_eq!(frame.ppn.0, ppn_base.0 + i);
|
||||
QUEUE_FRAMES.exclusive_access().push(frame);
|
||||
}
|
||||
ppn_base.into()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn virtio_dma_dealloc(pa: PhysAddr, pages: usize) -> i32 {
|
||||
let mut ppn_base: PhysPageNum = pa.into();
|
||||
for _ in 0..pages {
|
||||
frame_dealloc(ppn_base);
|
||||
ppn_base.step();
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn virtio_phys_to_virt(paddr: PhysAddr) -> VirtAddr {
|
||||
VirtAddr(paddr.0)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn virtio_virt_to_phys(vaddr: VirtAddr) -> PhysAddr {
|
||||
PageTable::from_token(kernel_token()).translate_va(vaddr).unwrap()
|
||||
}
|
||||
3
os6-ref/src/drivers/mod.rs
Normal file
3
os6-ref/src/drivers/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod block;
|
||||
|
||||
pub use block::BLOCK_DEVICE;
|
||||
12
os6-ref/src/entry.asm
Normal file
12
os6-ref/src/entry.asm
Normal file
@@ -0,0 +1,12 @@
|
||||
.section .text.entry
|
||||
.globl _start
|
||||
_start:
|
||||
la sp, boot_stack_top
|
||||
call rust_main
|
||||
|
||||
.section .bss.stack
|
||||
.globl boot_stack
|
||||
boot_stack:
|
||||
.space 4096 * 16
|
||||
.globl boot_stack_top
|
||||
boot_stack_top:
|
||||
169
os6-ref/src/fs/inode.rs
Normal file
169
os6-ref/src/fs/inode.rs
Normal file
@@ -0,0 +1,169 @@
|
||||
use easy_fs::{
|
||||
EasyFileSystem,
|
||||
Inode,
|
||||
};
|
||||
use crate::drivers::BLOCK_DEVICE;
|
||||
use crate::sync::UPSafeCell;
|
||||
use alloc::sync::Arc;
|
||||
use lazy_static::*;
|
||||
use bitflags::*;
|
||||
use alloc::vec::Vec;
|
||||
use super::File;
|
||||
use crate::mm::UserBuffer;
|
||||
|
||||
/// A wrapper around a filesystem inode
|
||||
/// to implement File trait atop
|
||||
pub struct OSInode {
|
||||
readable: bool,
|
||||
writable: bool,
|
||||
inner: UPSafeCell<OSInodeInner>,
|
||||
}
|
||||
|
||||
/// The OS inode inner in 'UPSafeCell'
|
||||
pub struct OSInodeInner {
|
||||
offset: usize,
|
||||
inode: Arc<Inode>,
|
||||
}
|
||||
|
||||
impl OSInode {
|
||||
/// Construct an OS inode from a inode
|
||||
pub fn new(
|
||||
readable: bool,
|
||||
writable: bool,
|
||||
inode: Arc<Inode>,
|
||||
) -> Self {
|
||||
Self {
|
||||
readable,
|
||||
writable,
|
||||
inner: unsafe { UPSafeCell::new(OSInodeInner {
|
||||
offset: 0,
|
||||
inode,
|
||||
})},
|
||||
}
|
||||
}
|
||||
/// Read all data inside a inode into vector
|
||||
pub fn read_all(&self) -> Vec<u8> {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
let mut buffer = [0u8; 512];
|
||||
let mut v: Vec<u8> = Vec::new();
|
||||
loop {
|
||||
let len = inner.inode.read_at(inner.offset, &mut buffer);
|
||||
if len == 0 {
|
||||
break;
|
||||
}
|
||||
inner.offset += len;
|
||||
v.extend_from_slice(&buffer[..len]);
|
||||
}
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// The root of all inodes, or '/' in short
|
||||
pub static ref ROOT_INODE: Arc<Inode> = {
|
||||
let efs = EasyFileSystem::open(BLOCK_DEVICE.clone());
|
||||
Arc::new(EasyFileSystem::root_inode(&efs))
|
||||
};
|
||||
}
|
||||
|
||||
/// List all files in the filesystems
|
||||
pub fn list_apps() {
|
||||
println!("/**** APPS ****");
|
||||
for app in ROOT_INODE.ls() {
|
||||
println!("{}", app);
|
||||
}
|
||||
println!("**************/");
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
/// Flags for opening files
|
||||
pub struct OpenFlags: u32 {
|
||||
const RDONLY = 0;
|
||||
const WRONLY = 1 << 0;
|
||||
const RDWR = 1 << 1;
|
||||
const CREATE = 1 << 9;
|
||||
const TRUNC = 1 << 10;
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenFlags {
|
||||
/// Get the current read write permission on an inode
|
||||
/// does not check validity for simplicity
|
||||
/// returns (readable, writable)
|
||||
pub fn read_write(&self) -> (bool, bool) {
|
||||
if self.is_empty() {
|
||||
(true, false)
|
||||
} else if self.contains(Self::WRONLY) {
|
||||
(false, true)
|
||||
} else {
|
||||
(true, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Open a file by path
|
||||
pub fn open_file(name: &str, flags: OpenFlags) -> Option<Arc<OSInode>> {
|
||||
let (readable, writable) = flags.read_write();
|
||||
if flags.contains(OpenFlags::CREATE) {
|
||||
if let Some(inode) = ROOT_INODE.find(name) {
|
||||
// clear size
|
||||
inode.clear();
|
||||
Some(Arc::new(OSInode::new(
|
||||
readable,
|
||||
writable,
|
||||
inode,
|
||||
)))
|
||||
} else {
|
||||
// create file
|
||||
ROOT_INODE.create(name)
|
||||
.map(|inode| {
|
||||
Arc::new(OSInode::new(
|
||||
readable,
|
||||
writable,
|
||||
inode,
|
||||
))
|
||||
})
|
||||
}
|
||||
} else {
|
||||
ROOT_INODE.find(name)
|
||||
.map(|inode| {
|
||||
if flags.contains(OpenFlags::TRUNC) {
|
||||
inode.clear();
|
||||
}
|
||||
Arc::new(OSInode::new(
|
||||
readable,
|
||||
writable,
|
||||
inode
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl File for OSInode {
|
||||
fn readable(&self) -> bool { self.readable }
|
||||
fn writable(&self) -> bool { self.writable }
|
||||
fn read(&self, mut buf: UserBuffer) -> usize {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
let mut total_read_size = 0usize;
|
||||
for slice in buf.buffers.iter_mut() {
|
||||
let read_size = inner.inode.read_at(inner.offset, *slice);
|
||||
if read_size == 0 {
|
||||
break;
|
||||
}
|
||||
inner.offset += read_size;
|
||||
total_read_size += read_size;
|
||||
}
|
||||
total_read_size
|
||||
}
|
||||
fn write(&self, buf: UserBuffer) -> usize {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
let mut total_write_size = 0usize;
|
||||
for slice in buf.buffers.iter() {
|
||||
let write_size = inner.inode.write_at(inner.offset, *slice);
|
||||
assert_eq!(write_size, slice.len());
|
||||
inner.offset += write_size;
|
||||
total_write_size += write_size;
|
||||
}
|
||||
total_write_size
|
||||
}
|
||||
}
|
||||
43
os6-ref/src/fs/mod.rs
Normal file
43
os6-ref/src/fs/mod.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
mod stdio;
|
||||
mod inode;
|
||||
|
||||
use crate::mm::UserBuffer;
|
||||
|
||||
/// The common abstraction of all IO resources
|
||||
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;
|
||||
}
|
||||
|
||||
/// The stat of a inode
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct Stat {
|
||||
/// ID of device containing file
|
||||
pub dev: u64,
|
||||
/// inode number
|
||||
pub ino: u64,
|
||||
/// file type and mode
|
||||
pub mode: StatMode,
|
||||
/// number of hard links
|
||||
pub nlink: u32,
|
||||
/// unused pad
|
||||
pad: [u64; 7],
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
/// The mode of a inode
|
||||
/// whether a directory or a file
|
||||
pub struct StatMode: u32 {
|
||||
const NULL = 0;
|
||||
/// directory
|
||||
const DIR = 0o040000;
|
||||
/// ordinary regular file
|
||||
const FILE = 0o100000;
|
||||
}
|
||||
}
|
||||
|
||||
pub use stdio::{Stdin, Stdout};
|
||||
pub use inode::{OSInode, open_file, OpenFlags, list_apps};
|
||||
48
os6-ref/src/fs/stdio.rs
Normal file
48
os6-ref/src/fs/stdio.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use super::File;
|
||||
use crate::mm::{UserBuffer};
|
||||
use crate::sbi::console_getchar;
|
||||
use crate::task::suspend_current_and_run_next;
|
||||
|
||||
/// The standard input
|
||||
pub struct Stdin;
|
||||
/// The standard output
|
||||
pub struct Stdout;
|
||||
|
||||
impl File for Stdin {
|
||||
fn readable(&self) -> bool { true }
|
||||
fn writable(&self) -> bool { false }
|
||||
fn read(&self, mut user_buf: UserBuffer) -> usize {
|
||||
assert_eq!(user_buf.len(), 1);
|
||||
// busy loop
|
||||
let mut c: usize;
|
||||
loop {
|
||||
c = console_getchar();
|
||||
if c == 0 {
|
||||
suspend_current_and_run_next();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let ch = c as u8;
|
||||
unsafe { user_buf.buffers[0].as_mut_ptr().write_volatile(ch); }
|
||||
1
|
||||
}
|
||||
fn write(&self, _user_buf: UserBuffer) -> usize {
|
||||
panic!("Cannot write to stdin!");
|
||||
}
|
||||
}
|
||||
|
||||
impl File for Stdout {
|
||||
fn readable(&self) -> bool { false }
|
||||
fn writable(&self) -> bool { true }
|
||||
fn read(&self, _user_buf: UserBuffer) -> usize{
|
||||
panic!("Cannot read from stdout!");
|
||||
}
|
||||
fn write(&self, user_buf: UserBuffer) -> usize {
|
||||
for buffer in user_buf.buffers.iter() {
|
||||
print!("{}", core::str::from_utf8(*buffer).unwrap());
|
||||
}
|
||||
user_buf.len()
|
||||
}
|
||||
}
|
||||
29
os6-ref/src/lang_items.rs
Normal file
29
os6-ref/src/lang_items.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
//! The panic handler
|
||||
|
||||
use crate::console::ANSICON;
|
||||
use crate::sbi::shutdown;
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[panic_handler]
|
||||
/// panic handler
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
if let Some(location) = info.location() {
|
||||
println_colorized!(
|
||||
"[kernel] Panicked at {}:{} {}",
|
||||
ANSICON::FgRed,
|
||||
ANSICON::BgDefault,
|
||||
location.file(),
|
||||
location.line(),
|
||||
info.message().unwrap()
|
||||
);
|
||||
} else {
|
||||
println_colorized!(
|
||||
"[kernel] Panicked: {}",
|
||||
ANSICON::FgRed,
|
||||
ANSICON::BgDefault,
|
||||
info.message().unwrap()
|
||||
);
|
||||
}
|
||||
shutdown()
|
||||
}
|
||||
53
os6-ref/src/linker.ld
Normal file
53
os6-ref/src/linker.ld
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
45
os6-ref/src/logging.rs
Normal file
45
os6-ref/src/logging.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
//! Global logger
|
||||
|
||||
use log::{self, Level, LevelFilter, Log, Metadata, Record};
|
||||
|
||||
/// a simple logger
|
||||
struct SimpleLogger;
|
||||
|
||||
impl Log for SimpleLogger {
|
||||
fn enabled(&self, _metadata: &Metadata) -> bool {
|
||||
true
|
||||
}
|
||||
fn log(&self, record: &Record) {
|
||||
if !self.enabled(record.metadata()) {
|
||||
return;
|
||||
}
|
||||
let color = match record.level() {
|
||||
Level::Error => 31, // Red
|
||||
Level::Warn => 93, // BrightYellow
|
||||
Level::Info => 34, // Blue
|
||||
Level::Debug => 32, // Green
|
||||
Level::Trace => 90, // BrightBlack
|
||||
};
|
||||
println!(
|
||||
"\u{1B}[{}m[{:>5}] {}\u{1B}[0m",
|
||||
color,
|
||||
record.level(),
|
||||
record.args(),
|
||||
);
|
||||
}
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
/// initiate logger
|
||||
pub fn init() {
|
||||
static LOGGER: SimpleLogger = SimpleLogger;
|
||||
log::set_logger(&LOGGER).unwrap();
|
||||
log::set_max_level(match option_env!("LOG") {
|
||||
Some("ERROR") => LevelFilter::Error,
|
||||
Some("WARN") => LevelFilter::Warn,
|
||||
Some("INFO") => LevelFilter::Info,
|
||||
Some("DEBUG") => LevelFilter::Debug,
|
||||
Some("TRACE") => LevelFilter::Trace,
|
||||
_ => LevelFilter::Off,
|
||||
});
|
||||
}
|
||||
74
os6-ref/src/main.rs
Normal file
74
os6-ref/src/main.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
//! The main module and entrypoint
|
||||
//!
|
||||
//! Various facilities of the kernels are implemented as submodules. The most
|
||||
//! important ones are:
|
||||
//!
|
||||
//! - [`trap`]: Handles all cases of switching from userspace to the kernel
|
||||
//! - [`task`]: Task management
|
||||
//! - [`syscall`]: System call handling and implementation
|
||||
//!
|
||||
//! The operating system also starts in this module. Kernel code starts
|
||||
//! executing from `entry.asm`, after which [`rust_main()`] is called to
|
||||
//! initialize various pieces of functionality. (See its source code for
|
||||
//! details.)
|
||||
//!
|
||||
//! We then call [`task::run_first_task()`] and for the first time go to
|
||||
//! userspace.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(panic_info_message)]
|
||||
#![feature(alloc_error_handler)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate bitflags;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
#[macro_use]
|
||||
mod console;
|
||||
mod config;
|
||||
mod lang_items;
|
||||
mod logging;
|
||||
mod mm;
|
||||
mod sbi;
|
||||
mod sync;
|
||||
mod syscall;
|
||||
mod task;
|
||||
mod timer;
|
||||
mod trap;
|
||||
mod drivers;
|
||||
mod fs;
|
||||
|
||||
core::arch::global_asm!(include_str!("entry.asm"));
|
||||
|
||||
/// clear BSS segment
|
||||
fn clear_bss() {
|
||||
extern "C" {
|
||||
fn sbss();
|
||||
fn ebss();
|
||||
}
|
||||
unsafe {
|
||||
core::slice::from_raw_parts_mut(sbss as usize as *mut u8, ebss as usize - sbss as usize)
|
||||
.fill(0);
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// the rust entry-point of os
|
||||
pub fn rust_main() -> ! {
|
||||
clear_bss();
|
||||
logging::init();
|
||||
println!("[kernel] Hello, world!");
|
||||
mm::init();
|
||||
mm::remap_test();
|
||||
trap::init();
|
||||
trap::enable_timer_interrupt();
|
||||
timer::set_next_trigger();
|
||||
fs::list_apps();
|
||||
task::add_initproc();
|
||||
task::run_tasks();
|
||||
panic!("Unreachable in rust_main!");
|
||||
}
|
||||
259
os6-ref/src/mm/address.rs
Normal file
259
os6-ref/src/mm/address.rs
Normal file
@@ -0,0 +1,259 @@
|
||||
//! Implementation of physical and virtual address and page number.
|
||||
use super::PageTableEntry;
|
||||
use crate::config::{PAGE_SIZE, PAGE_SIZE_BITS};
|
||||
use core::fmt::{self, Debug, Formatter};
|
||||
|
||||
/// Definitions
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct PhysAddr(pub usize);
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct VirtAddr(pub usize);
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct PhysPageNum(pub usize);
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct VirtPageNum(pub usize);
|
||||
|
||||
/// Debugging
|
||||
|
||||
impl Debug for VirtAddr {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("VA:{:#x}", self.0))
|
||||
}
|
||||
}
|
||||
impl Debug for VirtPageNum {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("VPN:{:#x}", self.0))
|
||||
}
|
||||
}
|
||||
impl Debug for PhysAddr {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("PA:{:#x}", self.0))
|
||||
}
|
||||
}
|
||||
impl Debug for PhysPageNum {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("PPN:{:#x}", self.0))
|
||||
}
|
||||
}
|
||||
|
||||
/// T: {PhysAddr, VirtAddr, PhysPageNum, VirtPageNum}
|
||||
/// T -> usize: T.0
|
||||
/// usize -> T: usize.into()
|
||||
|
||||
impl From<usize> for PhysAddr {
|
||||
fn from(v: usize) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
impl From<usize> for PhysPageNum {
|
||||
fn from(v: usize) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
impl From<usize> for VirtAddr {
|
||||
fn from(v: usize) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
impl From<usize> for VirtPageNum {
|
||||
fn from(v: usize) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
impl From<PhysAddr> for usize {
|
||||
fn from(v: PhysAddr) -> Self {
|
||||
v.0
|
||||
}
|
||||
}
|
||||
impl From<PhysPageNum> for usize {
|
||||
fn from(v: PhysPageNum) -> Self {
|
||||
v.0
|
||||
}
|
||||
}
|
||||
impl From<VirtAddr> for usize {
|
||||
fn from(v: VirtAddr) -> Self {
|
||||
v.0
|
||||
}
|
||||
}
|
||||
impl From<VirtPageNum> for usize {
|
||||
fn from(v: VirtPageNum) -> Self {
|
||||
v.0
|
||||
}
|
||||
}
|
||||
|
||||
impl VirtAddr {
|
||||
pub fn floor(&self) -> VirtPageNum {
|
||||
VirtPageNum(self.0 / PAGE_SIZE)
|
||||
}
|
||||
pub fn ceil(&self) -> VirtPageNum {
|
||||
VirtPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE)
|
||||
}
|
||||
pub fn page_offset(&self) -> usize {
|
||||
self.0 & (PAGE_SIZE - 1)
|
||||
}
|
||||
pub fn aligned(&self) -> bool {
|
||||
self.page_offset() == 0
|
||||
}
|
||||
}
|
||||
impl From<VirtAddr> for VirtPageNum {
|
||||
fn from(v: VirtAddr) -> Self {
|
||||
assert_eq!(v.page_offset(), 0);
|
||||
v.floor()
|
||||
}
|
||||
}
|
||||
impl From<VirtPageNum> for VirtAddr {
|
||||
fn from(v: VirtPageNum) -> Self {
|
||||
Self(v.0 << PAGE_SIZE_BITS)
|
||||
}
|
||||
}
|
||||
impl PhysAddr {
|
||||
pub fn floor(&self) -> PhysPageNum {
|
||||
PhysPageNum(self.0 / PAGE_SIZE)
|
||||
}
|
||||
pub fn ceil(&self) -> PhysPageNum {
|
||||
PhysPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE)
|
||||
}
|
||||
pub fn page_offset(&self) -> usize {
|
||||
self.0 & (PAGE_SIZE - 1)
|
||||
}
|
||||
pub fn aligned(&self) -> bool {
|
||||
self.page_offset() == 0
|
||||
}
|
||||
}
|
||||
impl From<PhysAddr> for PhysPageNum {
|
||||
fn from(v: PhysAddr) -> Self {
|
||||
assert_eq!(v.page_offset(), 0);
|
||||
v.floor()
|
||||
}
|
||||
}
|
||||
impl From<PhysPageNum> for PhysAddr {
|
||||
fn from(v: PhysPageNum) -> Self {
|
||||
Self(v.0 << PAGE_SIZE_BITS)
|
||||
}
|
||||
}
|
||||
|
||||
impl VirtPageNum {
|
||||
pub fn indexes(&self) -> [usize; 3] {
|
||||
let mut vpn = self.0;
|
||||
let mut idx = [0usize; 3];
|
||||
for i in (0..3).rev() {
|
||||
idx[i] = vpn & 511;
|
||||
vpn >>= 9;
|
||||
}
|
||||
idx
|
||||
}
|
||||
}
|
||||
|
||||
impl PhysAddr {
|
||||
pub fn get_ref<T>(&self) -> &'static T {
|
||||
unsafe { (self.0 as *const T).as_ref().unwrap() }
|
||||
}
|
||||
pub fn get_mut<T>(&self) -> &'static mut T {
|
||||
unsafe { (self.0 as *mut T).as_mut().unwrap() }
|
||||
}
|
||||
}
|
||||
impl PhysPageNum {
|
||||
pub fn get_pte_array(&self) -> &'static mut [PageTableEntry] {
|
||||
let pa: PhysAddr = (*self).into();
|
||||
unsafe { core::slice::from_raw_parts_mut(pa.0 as *mut PageTableEntry, 512) }
|
||||
}
|
||||
pub fn get_bytes_array(&self) -> &'static mut [u8] {
|
||||
let pa: PhysAddr = (*self).into();
|
||||
unsafe { core::slice::from_raw_parts_mut(pa.0 as *mut u8, 4096) }
|
||||
}
|
||||
pub fn get_mut<T>(&self) -> &'static mut T {
|
||||
let pa: PhysAddr = (*self).into();
|
||||
pa.get_mut()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait StepByOne {
|
||||
fn step(&mut self);
|
||||
}
|
||||
impl StepByOne for VirtPageNum {
|
||||
fn step(&mut self) {
|
||||
self.0 += 1;
|
||||
}
|
||||
}
|
||||
|
||||
impl StepByOne for PhysPageNum {
|
||||
fn step(&mut self) {
|
||||
self.0 += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
/// a simple range structure for type T
|
||||
pub struct SimpleRange<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
l: T,
|
||||
r: T,
|
||||
}
|
||||
impl<T> SimpleRange<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
pub fn new(start: T, end: T) -> Self {
|
||||
assert!(start <= end, "start {:?} > end {:?}!", start, end);
|
||||
Self { l: start, r: end }
|
||||
}
|
||||
pub fn get_start(&self) -> T {
|
||||
self.l
|
||||
}
|
||||
pub fn get_end(&self) -> T {
|
||||
self.r
|
||||
}
|
||||
}
|
||||
impl<T> IntoIterator for SimpleRange<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
type Item = T;
|
||||
type IntoIter = SimpleRangeIterator<T>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
SimpleRangeIterator::new(self.l, self.r)
|
||||
}
|
||||
}
|
||||
/// iterator for the simple range structure
|
||||
pub struct SimpleRangeIterator<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
current: T,
|
||||
end: T,
|
||||
}
|
||||
impl<T> SimpleRangeIterator<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
pub fn new(l: T, r: T) -> Self {
|
||||
Self { current: l, end: r }
|
||||
}
|
||||
}
|
||||
impl<T> Iterator for SimpleRangeIterator<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
type Item = T;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current == self.end {
|
||||
None
|
||||
} else {
|
||||
let t = self.current;
|
||||
self.current.step();
|
||||
Some(t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// a simple range structure for virtual page number
|
||||
pub type VPNRange = SimpleRange<VirtPageNum>;
|
||||
136
os6-ref/src/mm/frame_allocator.rs
Normal file
136
os6-ref/src/mm/frame_allocator.rs
Normal file
@@ -0,0 +1,136 @@
|
||||
//! Implementation of [`FrameAllocator`] which
|
||||
//! controls all the frames in the operating system.
|
||||
|
||||
use super::{PhysAddr, PhysPageNum};
|
||||
use crate::config::MEMORY_END;
|
||||
use crate::sync::UPSafeCell;
|
||||
use alloc::vec::Vec;
|
||||
use core::fmt::{self, Debug, Formatter};
|
||||
use lazy_static::*;
|
||||
|
||||
/// manage a frame which has the same lifecycle as the tracker
|
||||
pub struct FrameTracker {
|
||||
pub ppn: PhysPageNum,
|
||||
}
|
||||
|
||||
impl FrameTracker {
|
||||
pub fn new(ppn: PhysPageNum) -> Self {
|
||||
// page cleaning
|
||||
let bytes_array = ppn.get_bytes_array();
|
||||
for i in bytes_array {
|
||||
*i = 0;
|
||||
}
|
||||
Self { ppn }
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for FrameTracker {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("FrameTracker:PPN={:#x}", self.ppn.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for FrameTracker {
|
||||
fn drop(&mut self) {
|
||||
frame_dealloc(self.ppn);
|
||||
}
|
||||
}
|
||||
|
||||
trait FrameAllocator {
|
||||
fn new() -> Self;
|
||||
fn alloc(&mut self) -> Option<PhysPageNum>;
|
||||
fn dealloc(&mut self, ppn: PhysPageNum);
|
||||
}
|
||||
|
||||
/// an implementation for frame allocator
|
||||
pub struct StackFrameAllocator {
|
||||
current: usize,
|
||||
end: usize,
|
||||
recycled: Vec<usize>,
|
||||
}
|
||||
|
||||
impl StackFrameAllocator {
|
||||
pub fn init(&mut self, l: PhysPageNum, r: PhysPageNum) {
|
||||
self.current = l.0;
|
||||
self.end = r.0;
|
||||
info!("last {} Physical Frames.", self.end - self.current);
|
||||
}
|
||||
}
|
||||
impl FrameAllocator for StackFrameAllocator {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
current: 0,
|
||||
end: 0,
|
||||
recycled: Vec::new(),
|
||||
}
|
||||
}
|
||||
fn alloc(&mut self) -> Option<PhysPageNum> {
|
||||
if let Some(ppn) = self.recycled.pop() {
|
||||
Some(ppn.into())
|
||||
} else if self.current == self.end {
|
||||
None
|
||||
} else {
|
||||
self.current += 1;
|
||||
Some((self.current - 1).into())
|
||||
}
|
||||
}
|
||||
fn dealloc(&mut self, ppn: PhysPageNum) {
|
||||
let ppn = ppn.0;
|
||||
// validity check
|
||||
if ppn >= self.current || self.recycled.iter().any(|v| *v == ppn) {
|
||||
panic!("Frame ppn={:#x} has not been allocated!", ppn);
|
||||
}
|
||||
// recycle
|
||||
self.recycled.push(ppn);
|
||||
}
|
||||
}
|
||||
|
||||
type FrameAllocatorImpl = StackFrameAllocator;
|
||||
|
||||
lazy_static! {
|
||||
/// frame allocator instance through lazy_static!
|
||||
pub static ref FRAME_ALLOCATOR: UPSafeCell<FrameAllocatorImpl> =
|
||||
unsafe { UPSafeCell::new(FrameAllocatorImpl::new()) };
|
||||
}
|
||||
|
||||
pub fn init_frame_allocator() {
|
||||
extern "C" {
|
||||
fn ekernel();
|
||||
}
|
||||
FRAME_ALLOCATOR.exclusive_access().init(
|
||||
PhysAddr::from(ekernel as usize).ceil(),
|
||||
PhysAddr::from(MEMORY_END).floor(),
|
||||
);
|
||||
}
|
||||
|
||||
/// initiate the frame allocator using `ekernel` and `MEMORY_END`
|
||||
pub fn frame_alloc() -> Option<FrameTracker> {
|
||||
FRAME_ALLOCATOR
|
||||
.exclusive_access()
|
||||
.alloc()
|
||||
.map(FrameTracker::new)
|
||||
}
|
||||
|
||||
/// deallocate a frame
|
||||
pub fn frame_dealloc(ppn: PhysPageNum) {
|
||||
FRAME_ALLOCATOR.exclusive_access().dealloc(ppn);
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
/// a simple test for frame allocator
|
||||
pub fn frame_allocator_test() {
|
||||
let mut v: Vec<FrameTracker> = Vec::new();
|
||||
for i in 0..5 {
|
||||
let frame = frame_alloc().unwrap();
|
||||
info!("{:?}", frame);
|
||||
v.push(frame);
|
||||
}
|
||||
v.clear();
|
||||
for i in 0..5 {
|
||||
let frame = frame_alloc().unwrap();
|
||||
info!("{:?}", frame);
|
||||
v.push(frame);
|
||||
}
|
||||
drop(v);
|
||||
info!("frame_allocator_test passed!");
|
||||
}
|
||||
51
os6-ref/src/mm/heap_allocator.rs
Normal file
51
os6-ref/src/mm/heap_allocator.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
//! The global allocator
|
||||
|
||||
use crate::config::KERNEL_HEAP_SIZE;
|
||||
use buddy_system_allocator::LockedHeap;
|
||||
|
||||
#[global_allocator]
|
||||
/// heap allocator instance
|
||||
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
|
||||
|
||||
#[alloc_error_handler]
|
||||
/// panic when heap allocation error occurs
|
||||
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
|
||||
panic!("Heap allocation error, layout = {:?}", layout);
|
||||
}
|
||||
|
||||
/// heap space ([u8; KERNEL_HEAP_SIZE])
|
||||
static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];
|
||||
|
||||
/// initiate heap allocator
|
||||
pub fn init_heap() {
|
||||
unsafe {
|
||||
HEAP_ALLOCATOR
|
||||
.lock()
|
||||
.init(HEAP_SPACE.as_ptr() as usize, KERNEL_HEAP_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn heap_test() {
|
||||
use alloc::boxed::Box;
|
||||
use alloc::vec::Vec;
|
||||
extern "C" {
|
||||
fn sbss();
|
||||
fn ebss();
|
||||
}
|
||||
let bss_range = sbss as usize..ebss as usize;
|
||||
let a = Box::new(5);
|
||||
assert_eq!(*a, 5);
|
||||
assert!(bss_range.contains(&(a.as_ref() as *const _ as usize)));
|
||||
drop(a);
|
||||
let mut v: Vec<usize> = Vec::new();
|
||||
for i in 0..500 {
|
||||
v.push(i);
|
||||
}
|
||||
for (i, vi) in v.iter().enumerate().take(500) {
|
||||
assert_eq!(*vi, i);
|
||||
}
|
||||
assert!(bss_range.contains(&(v.as_ptr() as usize)));
|
||||
drop(v);
|
||||
info!("heap_test passed!");
|
||||
}
|
||||
404
os6-ref/src/mm/memory_set.rs
Normal file
404
os6-ref/src/mm/memory_set.rs
Normal file
@@ -0,0 +1,404 @@
|
||||
//! Implementation of [`MapArea`] and [`MemorySet`].
|
||||
|
||||
use super::{frame_alloc, FrameTracker};
|
||||
use super::{PTEFlags, PageTable, PageTableEntry};
|
||||
use super::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum};
|
||||
use super::{StepByOne, VPNRange};
|
||||
use crate::config::{MEMORY_END, PAGE_SIZE, TRAMPOLINE, TRAP_CONTEXT, USER_STACK_SIZE, MMIO};
|
||||
use crate::sync::UPSafeCell;
|
||||
use alloc::collections::BTreeMap;
|
||||
use alloc::sync::Arc;
|
||||
use alloc::vec::Vec;
|
||||
use lazy_static::*;
|
||||
use riscv::register::satp;
|
||||
|
||||
extern "C" {
|
||||
fn stext();
|
||||
fn etext();
|
||||
fn srodata();
|
||||
fn erodata();
|
||||
fn sdata();
|
||||
fn edata();
|
||||
fn sbss_with_stack();
|
||||
fn ebss();
|
||||
fn ekernel();
|
||||
fn strampoline();
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// a memory set instance through lazy_static! managing kernel space
|
||||
pub static ref KERNEL_SPACE: Arc<UPSafeCell<MemorySet>> =
|
||||
Arc::new(unsafe { UPSafeCell::new(MemorySet::new_kernel()) });
|
||||
}
|
||||
|
||||
/// Get the token of the kernel memory space
|
||||
pub fn kernel_token() -> usize {
|
||||
KERNEL_SPACE.exclusive_access().token()
|
||||
}
|
||||
|
||||
/// memory set structure, controls virtual-memory space
|
||||
pub struct MemorySet {
|
||||
page_table: PageTable,
|
||||
areas: Vec<MapArea>,
|
||||
}
|
||||
|
||||
impl MemorySet {
|
||||
pub fn new_bare() -> Self {
|
||||
Self {
|
||||
page_table: PageTable::new(),
|
||||
areas: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn token(&self) -> usize {
|
||||
self.page_table.token()
|
||||
}
|
||||
/// Assume that no conflicts.
|
||||
pub fn insert_framed_area(
|
||||
&mut self,
|
||||
start_va: VirtAddr,
|
||||
end_va: VirtAddr,
|
||||
permission: MapPermission,
|
||||
) {
|
||||
self.push(
|
||||
MapArea::new(start_va, end_va, MapType::Framed, permission),
|
||||
None,
|
||||
);
|
||||
}
|
||||
pub fn remove_area_with_start_vpn(&mut self, start_vpn: VirtPageNum) {
|
||||
if let Some((idx, area)) = self
|
||||
.areas
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.find(|(_, area)| area.vpn_range.get_start() == start_vpn)
|
||||
{
|
||||
area.unmap(&mut self.page_table);
|
||||
self.areas.remove(idx);
|
||||
}
|
||||
}
|
||||
fn push(&mut self, mut map_area: MapArea, data: Option<&[u8]>) {
|
||||
map_area.map(&mut self.page_table);
|
||||
if let Some(data) = data {
|
||||
map_area.copy_data(&mut self.page_table, data);
|
||||
}
|
||||
self.areas.push(map_area);
|
||||
}
|
||||
/// Mention that trampoline is not collected by areas.
|
||||
fn map_trampoline(&mut self) {
|
||||
self.page_table.map(
|
||||
VirtAddr::from(TRAMPOLINE).into(),
|
||||
PhysAddr::from(strampoline as usize).into(),
|
||||
PTEFlags::R | PTEFlags::X,
|
||||
);
|
||||
}
|
||||
/// Without kernel stacks.
|
||||
pub fn new_kernel() -> Self {
|
||||
let mut memory_set = Self::new_bare();
|
||||
// map trampoline
|
||||
memory_set.map_trampoline();
|
||||
// map kernel sections
|
||||
info!(".text [{:#x}, {:#x})", stext as usize, etext as usize);
|
||||
info!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize);
|
||||
info!(".data [{:#x}, {:#x})", sdata as usize, edata as usize);
|
||||
info!(
|
||||
".bss [{:#x}, {:#x})",
|
||||
sbss_with_stack as usize, ebss as usize
|
||||
);
|
||||
info!("mapping .text section");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(stext as usize).into(),
|
||||
(etext as usize).into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R | MapPermission::X,
|
||||
),
|
||||
None,
|
||||
);
|
||||
info!("mapping .rodata section");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(srodata as usize).into(),
|
||||
(erodata as usize).into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R,
|
||||
),
|
||||
None,
|
||||
);
|
||||
info!("mapping .data section");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(sdata as usize).into(),
|
||||
(edata as usize).into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R | MapPermission::W,
|
||||
),
|
||||
None,
|
||||
);
|
||||
info!("mapping .bss section");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(sbss_with_stack as usize).into(),
|
||||
(ebss as usize).into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R | MapPermission::W,
|
||||
),
|
||||
None,
|
||||
);
|
||||
info!("mapping physical memory");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(ekernel as usize).into(),
|
||||
MEMORY_END.into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R | MapPermission::W,
|
||||
),
|
||||
None,
|
||||
);
|
||||
info!("mapping memory-mapped registers");
|
||||
for pair in MMIO {
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(*pair).0.into(),
|
||||
((*pair).0 + (*pair).1).into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R | MapPermission::W,
|
||||
),
|
||||
None);
|
||||
}
|
||||
memory_set
|
||||
}
|
||||
/// Include sections in elf and trampoline and TrapContext and user stack,
|
||||
/// also returns user_sp and entry point.
|
||||
pub fn from_elf(elf_data: &[u8]) -> (Self, usize, usize) {
|
||||
let mut memory_set = Self::new_bare();
|
||||
// map trampoline
|
||||
memory_set.map_trampoline();
|
||||
// map program headers of elf, with U flag
|
||||
let elf = xmas_elf::ElfFile::new(elf_data).unwrap();
|
||||
let elf_header = elf.header;
|
||||
let magic = elf_header.pt1.magic;
|
||||
assert_eq!(magic, [0x7f, 0x45, 0x4c, 0x46], "invalid elf!");
|
||||
let ph_count = elf_header.pt2.ph_count();
|
||||
let mut max_end_vpn = VirtPageNum(0);
|
||||
for i in 0..ph_count {
|
||||
let ph = elf.program_header(i).unwrap();
|
||||
if ph.get_type().unwrap() == xmas_elf::program::Type::Load {
|
||||
let start_va: VirtAddr = (ph.virtual_addr() as usize).into();
|
||||
let end_va: VirtAddr = ((ph.virtual_addr() + ph.mem_size()) as usize).into();
|
||||
let mut map_perm = MapPermission::U;
|
||||
let ph_flags = ph.flags();
|
||||
if ph_flags.is_read() {
|
||||
map_perm |= MapPermission::R;
|
||||
}
|
||||
if ph_flags.is_write() {
|
||||
map_perm |= MapPermission::W;
|
||||
}
|
||||
if ph_flags.is_execute() {
|
||||
map_perm |= MapPermission::X;
|
||||
}
|
||||
let map_area = MapArea::new(start_va, end_va, MapType::Framed, map_perm);
|
||||
max_end_vpn = map_area.vpn_range.get_end();
|
||||
memory_set.push(
|
||||
map_area,
|
||||
Some(&elf.input[ph.offset() as usize..(ph.offset() + ph.file_size()) as usize]),
|
||||
);
|
||||
}
|
||||
}
|
||||
// map user stack with U flags
|
||||
let max_end_va: VirtAddr = max_end_vpn.into();
|
||||
let mut user_stack_bottom: usize = max_end_va.into();
|
||||
// guard page
|
||||
user_stack_bottom += PAGE_SIZE;
|
||||
let user_stack_top = user_stack_bottom + USER_STACK_SIZE;
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
user_stack_bottom.into(),
|
||||
user_stack_top.into(),
|
||||
MapType::Framed,
|
||||
MapPermission::R | MapPermission::W | MapPermission::U,
|
||||
),
|
||||
None,
|
||||
);
|
||||
// map TrapContext
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
TRAP_CONTEXT.into(),
|
||||
TRAMPOLINE.into(),
|
||||
MapType::Framed,
|
||||
MapPermission::R | MapPermission::W,
|
||||
),
|
||||
None,
|
||||
);
|
||||
(
|
||||
memory_set,
|
||||
user_stack_top,
|
||||
elf.header.pt2.entry_point() as usize,
|
||||
)
|
||||
}
|
||||
/// Copy an identical user_space
|
||||
pub fn from_existed_user(user_space: &MemorySet) -> MemorySet {
|
||||
let mut memory_set = Self::new_bare();
|
||||
// map trampoline
|
||||
memory_set.map_trampoline();
|
||||
// copy data sections/trap_context/user_stack
|
||||
for area in user_space.areas.iter() {
|
||||
let new_area = MapArea::from_another(area);
|
||||
memory_set.push(new_area, None);
|
||||
// copy data from another space
|
||||
for vpn in area.vpn_range {
|
||||
let src_ppn = user_space.translate(vpn).unwrap().ppn();
|
||||
let dst_ppn = memory_set.translate(vpn).unwrap().ppn();
|
||||
dst_ppn
|
||||
.get_bytes_array()
|
||||
.copy_from_slice(src_ppn.get_bytes_array());
|
||||
}
|
||||
}
|
||||
memory_set
|
||||
}
|
||||
pub fn activate(&self) {
|
||||
let satp = self.page_table.token();
|
||||
unsafe {
|
||||
satp::write(satp);
|
||||
core::arch::asm!("sfence.vma");
|
||||
}
|
||||
}
|
||||
pub fn translate(&self, vpn: VirtPageNum) -> Option<PageTableEntry> {
|
||||
self.page_table.translate(vpn)
|
||||
}
|
||||
pub fn recycle_data_pages(&mut self) {
|
||||
//*self = Self::new_bare();
|
||||
self.areas.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// map area structure, controls a contiguous piece of virtual memory
|
||||
pub struct MapArea {
|
||||
vpn_range: VPNRange,
|
||||
data_frames: BTreeMap<VirtPageNum, FrameTracker>,
|
||||
map_type: MapType,
|
||||
map_perm: MapPermission,
|
||||
}
|
||||
|
||||
impl MapArea {
|
||||
pub fn new(
|
||||
start_va: VirtAddr,
|
||||
end_va: VirtAddr,
|
||||
map_type: MapType,
|
||||
map_perm: MapPermission,
|
||||
) -> Self {
|
||||
let start_vpn: VirtPageNum = start_va.floor();
|
||||
let end_vpn: VirtPageNum = end_va.ceil();
|
||||
Self {
|
||||
vpn_range: VPNRange::new(start_vpn, end_vpn),
|
||||
data_frames: BTreeMap::new(),
|
||||
map_type,
|
||||
map_perm,
|
||||
}
|
||||
}
|
||||
pub fn from_another(another: &MapArea) -> Self {
|
||||
Self {
|
||||
vpn_range: VPNRange::new(another.vpn_range.get_start(), another.vpn_range.get_end()),
|
||||
data_frames: BTreeMap::new(),
|
||||
map_type: another.map_type,
|
||||
map_perm: another.map_perm,
|
||||
}
|
||||
}
|
||||
pub fn map_one(&mut self, page_table: &mut PageTable, vpn: VirtPageNum) {
|
||||
let ppn: PhysPageNum;
|
||||
match self.map_type {
|
||||
MapType::Identical => {
|
||||
ppn = PhysPageNum(vpn.0);
|
||||
}
|
||||
MapType::Framed => {
|
||||
let frame = frame_alloc().unwrap();
|
||||
ppn = frame.ppn;
|
||||
self.data_frames.insert(vpn, frame);
|
||||
}
|
||||
}
|
||||
let pte_flags = PTEFlags::from_bits(self.map_perm.bits).unwrap();
|
||||
page_table.map(vpn, ppn, pte_flags);
|
||||
}
|
||||
|
||||
pub fn unmap_one(&mut self, page_table: &mut PageTable, vpn: VirtPageNum) {
|
||||
#[allow(clippy::single_match)]
|
||||
match self.map_type {
|
||||
MapType::Framed => {
|
||||
self.data_frames.remove(&vpn);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
page_table.unmap(vpn);
|
||||
}
|
||||
pub fn map(&mut self, page_table: &mut PageTable) {
|
||||
for vpn in self.vpn_range {
|
||||
self.map_one(page_table, vpn);
|
||||
}
|
||||
}
|
||||
pub fn unmap(&mut self, page_table: &mut PageTable) {
|
||||
for vpn in self.vpn_range {
|
||||
self.unmap_one(page_table, vpn);
|
||||
}
|
||||
}
|
||||
/// data: start-aligned but maybe with shorter length
|
||||
/// assume that all frames were cleared before
|
||||
pub fn copy_data(&mut self, page_table: &mut PageTable, data: &[u8]) {
|
||||
assert_eq!(self.map_type, MapType::Framed);
|
||||
let mut start: usize = 0;
|
||||
let mut current_vpn = self.vpn_range.get_start();
|
||||
let len = data.len();
|
||||
loop {
|
||||
let src = &data[start..len.min(start + PAGE_SIZE)];
|
||||
let dst = &mut page_table
|
||||
.translate(current_vpn)
|
||||
.unwrap()
|
||||
.ppn()
|
||||
.get_bytes_array()[..src.len()];
|
||||
dst.copy_from_slice(src);
|
||||
start += PAGE_SIZE;
|
||||
if start >= len {
|
||||
break;
|
||||
}
|
||||
current_vpn.step();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
/// map type for memory set: identical or framed
|
||||
pub enum MapType {
|
||||
Identical,
|
||||
Framed,
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
/// map permission corresponding to that in pte: `R W X U`
|
||||
pub struct MapPermission: u8 {
|
||||
const R = 1 << 1;
|
||||
const W = 1 << 2;
|
||||
const X = 1 << 3;
|
||||
const U = 1 << 4;
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn remap_test() {
|
||||
let mut kernel_space = KERNEL_SPACE.exclusive_access();
|
||||
let mid_text: VirtAddr = ((stext as usize + etext as usize) / 2).into();
|
||||
let mid_rodata: VirtAddr = ((srodata as usize + erodata as usize) / 2).into();
|
||||
let mid_data: VirtAddr = ((sdata as usize + edata as usize) / 2).into();
|
||||
assert!(!kernel_space
|
||||
.page_table
|
||||
.translate(mid_text.floor())
|
||||
.unwrap()
|
||||
.writable());
|
||||
assert!(!kernel_space
|
||||
.page_table
|
||||
.translate(mid_rodata.floor())
|
||||
.unwrap()
|
||||
.writable());
|
||||
assert!(!kernel_space
|
||||
.page_table
|
||||
.translate(mid_data.floor())
|
||||
.unwrap()
|
||||
.executable());
|
||||
info!("remap_test passed!");
|
||||
}
|
||||
29
os6-ref/src/mm/mod.rs
Normal file
29
os6-ref/src/mm/mod.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
//! 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 heap_allocator;
|
||||
mod memory_set;
|
||||
mod page_table;
|
||||
|
||||
pub use address::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum};
|
||||
pub use address::{StepByOne, VPNRange};
|
||||
pub use frame_allocator::{frame_alloc, frame_dealloc, FrameTracker};
|
||||
pub use memory_set::{remap_test, kernel_token};
|
||||
pub use memory_set::{MapPermission, MemorySet, KERNEL_SPACE};
|
||||
pub use page_table::{translated_byte_buffer, translated_refmut, translated_ref, translated_str, PageTableEntry};
|
||||
pub use page_table::{PTEFlags, PageTable, UserBuffer};
|
||||
|
||||
/// initiate heap allocator, frame allocator and kernel space
|
||||
pub fn init() {
|
||||
heap_allocator::init_heap();
|
||||
frame_allocator::init_frame_allocator();
|
||||
KERNEL_SPACE.exclusive_access().activate();
|
||||
}
|
||||
260
os6-ref/src/mm/page_table.rs
Normal file
260
os6-ref/src/mm/page_table.rs
Normal file
@@ -0,0 +1,260 @@
|
||||
//! Implementation of [`PageTableEntry`] and [`PageTable`].
|
||||
|
||||
use super::{frame_alloc, FrameTracker, PhysAddr, PhysPageNum, StepByOne, VirtAddr, VirtPageNum};
|
||||
use alloc::string::String;
|
||||
use alloc::vec;
|
||||
use alloc::vec::Vec;
|
||||
use bitflags::*;
|
||||
|
||||
bitflags! {
|
||||
/// page table entry flags
|
||||
pub struct PTEFlags: u8 {
|
||||
const V = 1 << 0;
|
||||
const R = 1 << 1;
|
||||
const W = 1 << 2;
|
||||
const X = 1 << 3;
|
||||
const U = 1 << 4;
|
||||
const G = 1 << 5;
|
||||
const A = 1 << 6;
|
||||
const D = 1 << 7;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(C)]
|
||||
/// page table entry structure
|
||||
pub struct PageTableEntry {
|
||||
pub bits: usize,
|
||||
}
|
||||
|
||||
impl PageTableEntry {
|
||||
pub fn new(ppn: PhysPageNum, flags: PTEFlags) -> Self {
|
||||
PageTableEntry {
|
||||
bits: ppn.0 << 10 | flags.bits as usize,
|
||||
}
|
||||
}
|
||||
pub fn empty() -> Self {
|
||||
PageTableEntry { bits: 0 }
|
||||
}
|
||||
pub fn ppn(&self) -> PhysPageNum {
|
||||
(self.bits >> 10 & ((1usize << 44) - 1)).into()
|
||||
}
|
||||
pub fn flags(&self) -> PTEFlags {
|
||||
PTEFlags::from_bits(self.bits as u8).unwrap()
|
||||
}
|
||||
pub fn is_valid(&self) -> bool {
|
||||
(self.flags() & PTEFlags::V) != PTEFlags::empty()
|
||||
}
|
||||
pub fn readable(&self) -> bool {
|
||||
(self.flags() & PTEFlags::R) != PTEFlags::empty()
|
||||
}
|
||||
pub fn writable(&self) -> bool {
|
||||
(self.flags() & PTEFlags::W) != PTEFlags::empty()
|
||||
}
|
||||
pub fn executable(&self) -> bool {
|
||||
(self.flags() & PTEFlags::X) != PTEFlags::empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// page table structure
|
||||
pub struct PageTable {
|
||||
root_ppn: PhysPageNum,
|
||||
frames: Vec<FrameTracker>,
|
||||
}
|
||||
|
||||
/// Assume that it won't oom when creating/mapping.
|
||||
impl PageTable {
|
||||
pub fn new() -> Self {
|
||||
let frame = frame_alloc().unwrap();
|
||||
PageTable {
|
||||
root_ppn: frame.ppn,
|
||||
frames: vec![frame],
|
||||
}
|
||||
}
|
||||
/// Temporarily used to get arguments from user space.
|
||||
pub fn from_token(satp: usize) -> Self {
|
||||
Self {
|
||||
root_ppn: PhysPageNum::from(satp & ((1usize << 44) - 1)),
|
||||
frames: Vec::new(),
|
||||
}
|
||||
}
|
||||
fn find_pte_create(&mut self, vpn: VirtPageNum) -> Option<&mut PageTableEntry> {
|
||||
let mut idxs = vpn.indexes();
|
||||
let mut ppn = self.root_ppn;
|
||||
let mut result: Option<&mut PageTableEntry> = None;
|
||||
for (i, idx) in idxs.iter_mut().enumerate() {
|
||||
let pte = &mut ppn.get_pte_array()[*idx];
|
||||
if i == 2 {
|
||||
result = Some(pte);
|
||||
break;
|
||||
}
|
||||
if !pte.is_valid() {
|
||||
let frame = frame_alloc().unwrap();
|
||||
*pte = PageTableEntry::new(frame.ppn, PTEFlags::V);
|
||||
self.frames.push(frame);
|
||||
}
|
||||
ppn = pte.ppn();
|
||||
}
|
||||
result
|
||||
}
|
||||
fn find_pte(&self, vpn: VirtPageNum) -> Option<&PageTableEntry> {
|
||||
let idxs = vpn.indexes();
|
||||
let mut ppn = self.root_ppn;
|
||||
let mut result: Option<&PageTableEntry> = None;
|
||||
for (i, idx) in idxs.iter().enumerate() {
|
||||
let pte = &ppn.get_pte_array()[*idx];
|
||||
if i == 2 {
|
||||
result = Some(pte);
|
||||
break;
|
||||
}
|
||||
if !pte.is_valid() {
|
||||
return None;
|
||||
}
|
||||
ppn = pte.ppn();
|
||||
}
|
||||
result
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn map(&mut self, vpn: VirtPageNum, ppn: PhysPageNum, flags: PTEFlags) {
|
||||
let pte = self.find_pte_create(vpn).unwrap();
|
||||
assert!(!pte.is_valid(), "vpn {:?} is mapped before mapping", vpn);
|
||||
*pte = PageTableEntry::new(ppn, flags | PTEFlags::V);
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn unmap(&mut self, vpn: VirtPageNum) {
|
||||
let pte = self.find_pte_create(vpn).unwrap();
|
||||
assert!(pte.is_valid(), "vpn {:?} is invalid before unmapping", vpn);
|
||||
*pte = PageTableEntry::empty();
|
||||
}
|
||||
pub fn translate(&self, vpn: VirtPageNum) -> Option<PageTableEntry> {
|
||||
self.find_pte(vpn).copied()
|
||||
}
|
||||
pub fn translate_va(&self, va: VirtAddr) -> Option<PhysAddr> {
|
||||
self.find_pte(va.clone().floor()).map(|pte| {
|
||||
//println!("translate_va:va = {:?}", va);
|
||||
let aligned_pa: PhysAddr = pte.ppn().into();
|
||||
//println!("translate_va:pa_align = {:?}", aligned_pa);
|
||||
let offset = va.page_offset();
|
||||
let aligned_pa_usize: usize = aligned_pa.into();
|
||||
(aligned_pa_usize + offset).into()
|
||||
})
|
||||
}
|
||||
pub fn token(&self) -> usize {
|
||||
8usize << 60 | self.root_ppn.0
|
||||
}
|
||||
}
|
||||
|
||||
/// translate a pointer to a mutable u8 Vec through page table
|
||||
pub fn translated_byte_buffer(token: usize, ptr: *const u8, len: usize) -> Vec<&'static mut [u8]> {
|
||||
let page_table = PageTable::from_token(token);
|
||||
let mut start = ptr as usize;
|
||||
let end = start + len;
|
||||
let mut v = Vec::new();
|
||||
while start < end {
|
||||
let start_va = VirtAddr::from(start);
|
||||
let mut vpn = start_va.floor();
|
||||
let ppn = page_table.translate(vpn).unwrap().ppn();
|
||||
vpn.step();
|
||||
let mut end_va: VirtAddr = vpn.into();
|
||||
end_va = end_va.min(VirtAddr::from(end));
|
||||
if end_va.page_offset() == 0 {
|
||||
v.push(&mut ppn.get_bytes_array()[start_va.page_offset()..]);
|
||||
} else {
|
||||
v.push(&mut ppn.get_bytes_array()[start_va.page_offset()..end_va.page_offset()]);
|
||||
}
|
||||
start = end_va.into();
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
pub fn translated_str(token: usize, ptr: *const u8) -> String {
|
||||
let page_table = PageTable::from_token(token);
|
||||
let mut string = String::new();
|
||||
let mut va = ptr as usize;
|
||||
loop {
|
||||
let ch: u8 = *(page_table
|
||||
.translate_va(VirtAddr::from(va))
|
||||
.unwrap()
|
||||
.get_mut());
|
||||
if ch == 0 {
|
||||
break;
|
||||
} else {
|
||||
string.push(ch as char);
|
||||
va += 1;
|
||||
}
|
||||
}
|
||||
string
|
||||
}
|
||||
|
||||
pub fn translated_ref<T>(token: usize, ptr: *const T) -> &'static T {
|
||||
let page_table = PageTable::from_token(token);
|
||||
page_table.translate_va(VirtAddr::from(ptr as usize)).unwrap().get_mut()
|
||||
}
|
||||
|
||||
pub fn translated_refmut<T>(token: usize, ptr: *mut T) -> &'static mut T {
|
||||
//println!("into translated_refmut!");
|
||||
let page_table = PageTable::from_token(token);
|
||||
let va = ptr as usize;
|
||||
//println!("translated_refmut: before translate_va");
|
||||
page_table
|
||||
.translate_va(VirtAddr::from(va))
|
||||
.unwrap()
|
||||
.get_mut()
|
||||
}
|
||||
|
||||
/// An abstraction over a buffer passed from user space to kernel space
|
||||
pub struct UserBuffer {
|
||||
pub buffers: Vec<&'static mut [u8]>,
|
||||
}
|
||||
|
||||
impl UserBuffer {
|
||||
/// Constuct a UserBuffer
|
||||
pub fn new(buffers: Vec<&'static mut [u8]>) -> Self {
|
||||
Self { buffers }
|
||||
}
|
||||
/// Get the length of a UserBuffer
|
||||
pub fn len(&self) -> usize {
|
||||
let mut total: usize = 0;
|
||||
for b in self.buffers.iter() {
|
||||
total += b.len();
|
||||
}
|
||||
total
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for UserBuffer {
|
||||
type Item = *mut u8;
|
||||
type IntoIter = UserBufferIterator;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
UserBufferIterator {
|
||||
buffers: self.buffers,
|
||||
current_buffer: 0,
|
||||
current_idx: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An iterator over a UserBuffer
|
||||
pub struct UserBufferIterator {
|
||||
buffers: Vec<&'static mut [u8]>,
|
||||
current_buffer: usize,
|
||||
current_idx: usize,
|
||||
}
|
||||
|
||||
impl Iterator for UserBufferIterator {
|
||||
type Item = *mut u8;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current_buffer >= self.buffers.len() {
|
||||
None
|
||||
} else {
|
||||
let r = &mut self.buffers[self.current_buffer][self.current_idx] as *mut _;
|
||||
if self.current_idx + 1 == self.buffers[self.current_buffer].len() {
|
||||
self.current_idx = 0;
|
||||
self.current_buffer += 1;
|
||||
} else {
|
||||
self.current_idx += 1;
|
||||
}
|
||||
Some(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
45
os6-ref/src/sbi.rs
Normal file
45
os6-ref/src/sbi.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
//! SBI call wrappers
|
||||
|
||||
#![allow(unused)]
|
||||
|
||||
const SBI_SET_TIMER: usize = 0;
|
||||
const SBI_CONSOLE_PUTCHAR: usize = 1;
|
||||
const SBI_CONSOLE_GETCHAR: usize = 2;
|
||||
const SBI_SHUTDOWN: usize = 8;
|
||||
|
||||
#[inline(always)]
|
||||
/// general sbi call
|
||||
fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
|
||||
let mut ret;
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"ecall",
|
||||
inlateout("x10") arg0 => ret,
|
||||
in("x11") arg1,
|
||||
in("x12") arg2,
|
||||
in("x17") which,
|
||||
);
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
/// use sbi call to set timer
|
||||
pub fn set_timer(timer: usize) {
|
||||
sbi_call(SBI_SET_TIMER, timer, 0, 0);
|
||||
}
|
||||
|
||||
/// use sbi call to putchar in console (qemu uart handler)
|
||||
pub fn console_putchar(c: usize) {
|
||||
sbi_call(SBI_CONSOLE_PUTCHAR, c, 0, 0);
|
||||
}
|
||||
|
||||
/// use sbi call to getchar from console (qemu uart handler)
|
||||
pub fn console_getchar() -> usize {
|
||||
sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0)
|
||||
}
|
||||
|
||||
/// use sbi call to shutdown the kernel
|
||||
pub fn shutdown() -> ! {
|
||||
sbi_call(SBI_SHUTDOWN, 0, 0, 0);
|
||||
panic!("It should shutdown!");
|
||||
}
|
||||
5
os6-ref/src/sync/mod.rs
Normal file
5
os6-ref/src/sync/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
//! Synchronization and interior mutability primitives
|
||||
|
||||
mod up;
|
||||
|
||||
pub use up::UPSafeCell;
|
||||
31
os6-ref/src/sync/up.rs
Normal file
31
os6-ref/src/sync/up.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
//! 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),
|
||||
}
|
||||
}
|
||||
/// Panic if the data has been borrowed.
|
||||
pub fn exclusive_access(&self) -> RefMut<'_, T> {
|
||||
self.inner.borrow_mut()
|
||||
}
|
||||
}
|
||||
93
os6-ref/src/syscall/fs.rs
Normal file
93
os6-ref/src/syscall/fs.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
//! File and filesystem-related syscalls
|
||||
|
||||
use crate::mm::translated_byte_buffer;
|
||||
use crate::mm::translated_str;
|
||||
use crate::mm::translated_refmut;
|
||||
use crate::task::current_user_token;
|
||||
use crate::task::current_task;
|
||||
use crate::fs::open_file;
|
||||
use crate::fs::OpenFlags;
|
||||
use crate::fs::Stat;
|
||||
use crate::mm::UserBuffer;
|
||||
use alloc::sync::Arc;
|
||||
|
||||
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
||||
let token = current_user_token();
|
||||
let task = current_task().unwrap();
|
||||
let inner = task.inner_exclusive_access();
|
||||
if fd >= inner.fd_table.len() {
|
||||
return -1;
|
||||
}
|
||||
if let Some(file) = &inner.fd_table[fd] {
|
||||
let file = file.clone();
|
||||
// release current task TCB manually to avoid multi-borrow
|
||||
drop(inner);
|
||||
file.write(
|
||||
UserBuffer::new(translated_byte_buffer(token, buf, len))
|
||||
) as isize
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
|
||||
let token = current_user_token();
|
||||
let task = current_task().unwrap();
|
||||
let inner = task.inner_exclusive_access();
|
||||
if fd >= inner.fd_table.len() {
|
||||
return -1;
|
||||
}
|
||||
if let Some(file) = &inner.fd_table[fd] {
|
||||
let file = file.clone();
|
||||
// release current task TCB manually to avoid multi-borrow
|
||||
drop(inner);
|
||||
file.read(
|
||||
UserBuffer::new(translated_byte_buffer(token, buf, len))
|
||||
) as isize
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sys_open(path: *const u8, flags: u32) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
let token = current_user_token();
|
||||
let path = translated_str(token, path);
|
||||
if let Some(inode) = open_file(
|
||||
path.as_str(),
|
||||
OpenFlags::from_bits(flags).unwrap()
|
||||
) {
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
let fd = inner.alloc_fd();
|
||||
inner.fd_table[fd] = Some(inode);
|
||||
fd as isize
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sys_close(fd: usize) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
if fd >= inner.fd_table.len() {
|
||||
return -1;
|
||||
}
|
||||
if inner.fd_table[fd].is_none() {
|
||||
return -1;
|
||||
}
|
||||
inner.fd_table[fd].take();
|
||||
0
|
||||
}
|
||||
|
||||
// YOUR JOB: 扩展 easy-fs 和内核以实现以下三个 syscall
|
||||
pub fn sys_fstat(_fd: usize, _st: *mut Stat) -> isize {
|
||||
-1
|
||||
}
|
||||
|
||||
pub fn sys_linkat(_old_name: *const u8, _new_name: *const u8) -> isize {
|
||||
-1
|
||||
}
|
||||
|
||||
pub fn sys_unlinkat(_name: *const u8) -> isize {
|
||||
-1
|
||||
}
|
||||
64
os6-ref/src/syscall/mod.rs
Normal file
64
os6-ref/src/syscall/mod.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
//! Implementation of syscalls
|
||||
//!
|
||||
//! The single entry point to all system calls, [`syscall()`], is called
|
||||
//! whenever userspace wishes to perform a system call using the `ecall`
|
||||
//! instruction. In this case, the processor raises an 'Environment call from
|
||||
//! U-mode' exception, which is handled as one of the cases in
|
||||
//! [`crate::trap::trap_handler`].
|
||||
//!
|
||||
//! For clarity, each single syscall is implemented as its own function, named
|
||||
//! `sys_` then the name of the syscall. You can find functions like this in
|
||||
//! submodules, and you should also implement syscalls this way.
|
||||
|
||||
const SYSCALL_UNLINKAT: usize = 35;
|
||||
const SYSCALL_LINKAT: usize = 37;
|
||||
const SYSCALL_OPEN: usize = 56;
|
||||
const SYSCALL_CLOSE: usize = 57;
|
||||
const SYSCALL_READ: usize = 63;
|
||||
const SYSCALL_WRITE: usize = 64;
|
||||
const SYSCALL_FSTAT: usize = 80;
|
||||
const SYSCALL_EXIT: usize = 93;
|
||||
const SYSCALL_YIELD: usize = 124;
|
||||
const SYSCALL_GET_TIME: usize = 169;
|
||||
const SYSCALL_GETPID: usize = 172;
|
||||
const SYSCALL_FORK: usize = 220;
|
||||
const SYSCALL_EXEC: usize = 221;
|
||||
const SYSCALL_WAITPID: usize = 260;
|
||||
const SYSCALL_SPAWN: usize = 400;
|
||||
const SYSCALL_MUNMAP: usize = 215;
|
||||
const SYSCALL_MMAP: usize = 222;
|
||||
const SYSCALL_SET_PRIORITY: usize = 140;
|
||||
const SYSCALL_TASK_INFO: usize = 410;
|
||||
|
||||
mod fs;
|
||||
pub mod process;
|
||||
|
||||
use fs::*;
|
||||
use process::*;
|
||||
use crate::fs::Stat;
|
||||
|
||||
/// handle syscall exception with `syscall_id` and other arguments
|
||||
pub fn syscall(syscall_id: usize, args: [usize; 4]) -> isize {
|
||||
match syscall_id {
|
||||
SYSCALL_LINKAT => sys_linkat(args[1] as *const u8, args[3] as *const u8),
|
||||
SYSCALL_UNLINKAT => sys_unlinkat(args[1] as *const u8),
|
||||
SYSCALL_OPEN => sys_open(args[1] as *const u8, args[2] as u32),
|
||||
SYSCALL_CLOSE => sys_close(args[0]),
|
||||
SYSCALL_READ => sys_read(args[0], args[1] as *const u8, args[2]),
|
||||
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
|
||||
SYSCALL_FSTAT => sys_fstat(args[0], args[1] as *mut Stat),
|
||||
SYSCALL_EXIT => sys_exit(args[0] as i32),
|
||||
SYSCALL_YIELD => sys_yield(),
|
||||
SYSCALL_GETPID => sys_getpid(),
|
||||
SYSCALL_FORK => sys_fork(),
|
||||
SYSCALL_EXEC => sys_exec(args[0] as *const u8),
|
||||
SYSCALL_WAITPID => sys_waitpid(args[0] as isize, args[1] as *mut i32),
|
||||
SYSCALL_GET_TIME => sys_get_time(args[0] as *mut TimeVal, args[1]),
|
||||
SYSCALL_MMAP => sys_mmap(args[0], args[1], args[2]),
|
||||
SYSCALL_MUNMAP => sys_munmap(args[0], args[1]),
|
||||
SYSCALL_SET_PRIORITY => sys_set_priority(args[0] as isize),
|
||||
SYSCALL_TASK_INFO => sys_task_info(args[0] as *mut TaskInfo),
|
||||
SYSCALL_SPAWN => sys_spawn(args[0] as *const u8),
|
||||
_ => panic!("Unsupported syscall_id: {}", syscall_id),
|
||||
}
|
||||
}
|
||||
148
os6-ref/src/syscall/process.rs
Normal file
148
os6-ref/src/syscall/process.rs
Normal file
@@ -0,0 +1,148 @@
|
||||
//! Process management syscalls
|
||||
|
||||
use crate::mm::{translated_refmut, translated_ref, translated_str};
|
||||
use crate::task::{
|
||||
add_task, current_task, current_user_token, exit_current_and_run_next,
|
||||
suspend_current_and_run_next, TaskStatus,
|
||||
};
|
||||
use crate::fs::{open_file, OpenFlags};
|
||||
use crate::timer::get_time_us;
|
||||
use alloc::sync::Arc;
|
||||
use alloc::vec::Vec;
|
||||
use crate::config::MAX_SYSCALL_NUM;
|
||||
use alloc::string::String;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct TimeVal {
|
||||
pub sec: usize,
|
||||
pub usec: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct TaskInfo {
|
||||
pub status: TaskStatus,
|
||||
pub syscall_times: [u32; MAX_SYSCALL_NUM],
|
||||
pub time: usize,
|
||||
}
|
||||
|
||||
pub fn sys_exit(exit_code: i32) -> ! {
|
||||
debug!("[kernel] Application exited with code {}", exit_code);
|
||||
exit_current_and_run_next(exit_code);
|
||||
panic!("Unreachable in sys_exit!");
|
||||
}
|
||||
|
||||
/// current task gives up resources for other tasks
|
||||
pub fn sys_yield() -> isize {
|
||||
suspend_current_and_run_next();
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_getpid() -> isize {
|
||||
current_task().unwrap().pid.0 as isize
|
||||
}
|
||||
|
||||
/// Syscall Fork which returns 0 for child process and child_pid for parent process
|
||||
pub fn sys_fork() -> isize {
|
||||
let current_task = current_task().unwrap();
|
||||
let new_task = current_task.fork();
|
||||
let new_pid = new_task.pid.0;
|
||||
// modify trap context of new_task, because it returns immediately after switching
|
||||
let trap_cx = new_task.inner_exclusive_access().get_trap_cx();
|
||||
// we do not have to move to next instruction since we have done it before
|
||||
// for child process, fork returns 0
|
||||
trap_cx.x[10] = 0;
|
||||
// add new task to scheduler
|
||||
add_task(new_task);
|
||||
new_pid as isize
|
||||
}
|
||||
|
||||
/// Syscall Exec which accepts the elf path
|
||||
pub fn sys_exec(path: *const u8) -> isize {
|
||||
let token = current_user_token();
|
||||
let path = translated_str(token, path);
|
||||
if let Some(app_inode) = open_file(path.as_str(), OpenFlags::RDONLY) {
|
||||
let all_data = app_inode.read_all();
|
||||
let task = current_task().unwrap();
|
||||
task.exec(all_data.as_slice());
|
||||
0
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// If there is not a child process whose pid is same as given, return -1.
|
||||
/// Else if there is a child process but it is still running, return -2.
|
||||
pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
// find a child process
|
||||
|
||||
// ---- access current TCB exclusively
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
if !inner
|
||||
.children
|
||||
.iter()
|
||||
.any(|p| pid == -1 || pid as usize == p.getpid())
|
||||
{
|
||||
return -1;
|
||||
// ---- release current PCB
|
||||
}
|
||||
let pair = inner.children.iter().enumerate().find(|(_, p)| {
|
||||
// ++++ temporarily access child PCB lock exclusively
|
||||
p.inner_exclusive_access().is_zombie() && (pid == -1 || pid as usize == p.getpid())
|
||||
// ++++ release child PCB
|
||||
});
|
||||
if let Some((idx, _)) = pair {
|
||||
let child = inner.children.remove(idx);
|
||||
// confirm that child will be deallocated after removing from children list
|
||||
assert_eq!(Arc::strong_count(&child), 1);
|
||||
let found_pid = child.getpid();
|
||||
// ++++ temporarily access child TCB exclusively
|
||||
let exit_code = child.inner_exclusive_access().exit_code;
|
||||
// ++++ release child PCB
|
||||
*translated_refmut(inner.memory_set.token(), exit_code_ptr) = exit_code;
|
||||
found_pid as isize
|
||||
} else {
|
||||
-2
|
||||
}
|
||||
// ---- release current PCB lock automatically
|
||||
}
|
||||
|
||||
// YOUR JOB: 引入虚地址后重写 sys_get_time
|
||||
pub fn sys_get_time(_ts: *mut TimeVal, _tz: usize) -> isize {
|
||||
let _us = get_time_us();
|
||||
// unsafe {
|
||||
// *ts = TimeVal {
|
||||
// sec: us / 1_000_000,
|
||||
// usec: us % 1_000_000,
|
||||
// };
|
||||
// }
|
||||
0
|
||||
}
|
||||
|
||||
// YOUR JOB: 引入虚地址后重写 sys_task_info
|
||||
pub fn sys_task_info(ti: *mut TaskInfo) -> isize {
|
||||
-1
|
||||
}
|
||||
|
||||
// YOUR JOB: 实现sys_set_priority,为任务添加优先级
|
||||
pub fn sys_set_priority(_prio: isize) -> isize {
|
||||
-1
|
||||
}
|
||||
|
||||
// YOUR JOB: 扩展内核以实现 sys_mmap 和 sys_munmap
|
||||
pub fn sys_mmap(_start: usize, _len: usize, _port: usize) -> isize {
|
||||
-1
|
||||
}
|
||||
|
||||
pub fn sys_munmap(_start: usize, _len: usize) -> isize {
|
||||
-1
|
||||
}
|
||||
|
||||
//
|
||||
// YOUR JOB: 实现 sys_spawn 系统调用
|
||||
// ALERT: 注意在实现 SPAWN 时不需要复制父进程地址空间,SPAWN != FORK + EXEC
|
||||
pub fn sys_spawn(_path: *const u8) -> isize {
|
||||
-1
|
||||
}
|
||||
32
os6-ref/src/task/context.rs
Normal file
32
os6-ref/src/task/context.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
//! Implementation of [`TaskContext`]
|
||||
|
||||
use crate::trap::trap_return;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(C)]
|
||||
/// task context structure containing some registers
|
||||
pub struct TaskContext {
|
||||
/// Ret position after task switching
|
||||
ra: usize,
|
||||
/// Stack pointer
|
||||
sp: usize,
|
||||
/// s0-11 register, callee saved
|
||||
s: [usize; 12],
|
||||
}
|
||||
|
||||
impl TaskContext {
|
||||
pub fn zero_init() -> Self {
|
||||
Self {
|
||||
ra: 0,
|
||||
sp: 0,
|
||||
s: [0; 12],
|
||||
}
|
||||
}
|
||||
pub fn goto_trap_return(kstack_ptr: usize) -> Self {
|
||||
Self {
|
||||
ra: trap_return as usize,
|
||||
sp: kstack_ptr,
|
||||
s: [0; 12],
|
||||
}
|
||||
}
|
||||
}
|
||||
47
os6-ref/src/task/manager.rs
Normal file
47
os6-ref/src/task/manager.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
//! Implementation of [`TaskManager`]
|
||||
//!
|
||||
//! It is only used to manage processes and schedule process based on ready queue.
|
||||
//! Other CPU process monitoring functions are in Processor.
|
||||
|
||||
|
||||
use super::TaskControlBlock;
|
||||
use crate::sync::UPSafeCell;
|
||||
use alloc::collections::VecDeque;
|
||||
use alloc::sync::Arc;
|
||||
use lazy_static::*;
|
||||
|
||||
pub struct TaskManager {
|
||||
ready_queue: VecDeque<Arc<TaskControlBlock>>,
|
||||
}
|
||||
|
||||
// YOUR JOB: FIFO->Stride
|
||||
/// A simple FIFO scheduler.
|
||||
impl TaskManager {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
ready_queue: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
/// Add process back to ready queue
|
||||
pub fn add(&mut self, task: Arc<TaskControlBlock>) {
|
||||
self.ready_queue.push_back(task);
|
||||
}
|
||||
/// Take a process out of the ready queue
|
||||
pub fn fetch(&mut self) -> Option<Arc<TaskControlBlock>> {
|
||||
self.ready_queue.pop_front()
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// TASK_MANAGER instance through lazy_static!
|
||||
pub static ref TASK_MANAGER: UPSafeCell<TaskManager> =
|
||||
unsafe { UPSafeCell::new(TaskManager::new()) };
|
||||
}
|
||||
|
||||
pub fn add_task(task: Arc<TaskControlBlock>) {
|
||||
TASK_MANAGER.exclusive_access().add(task);
|
||||
}
|
||||
|
||||
pub fn fetch_task() -> Option<Arc<TaskControlBlock>> {
|
||||
TASK_MANAGER.exclusive_access().fetch()
|
||||
}
|
||||
106
os6-ref/src/task/mod.rs
Normal file
106
os6-ref/src/task/mod.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
//! Implementation of process management mechanism
|
||||
//!
|
||||
//! Here is the entry for process scheduling required by other modules
|
||||
//! (such as syscall or clock interrupt).
|
||||
//! By suspending or exiting the current process, you can
|
||||
//! modify the process state, manage the process queue through TASK_MANAGER,
|
||||
//! and switch the control flow through PROCESSOR.
|
||||
//!
|
||||
//! Be careful when you see [`__switch`]. Control flow around this function
|
||||
//! might not be what you expect.
|
||||
|
||||
mod context;
|
||||
mod manager;
|
||||
mod pid;
|
||||
mod processor;
|
||||
mod switch;
|
||||
#[allow(clippy::module_inception)]
|
||||
mod task;
|
||||
|
||||
use alloc::sync::Arc;
|
||||
use lazy_static::*;
|
||||
use manager::fetch_task;
|
||||
use switch::__switch;
|
||||
use crate::mm::VirtAddr;
|
||||
use crate::mm::MapPermission;
|
||||
use crate::config::PAGE_SIZE;
|
||||
use crate::timer::get_time_us;
|
||||
pub use crate::syscall::process::TaskInfo;
|
||||
use crate::fs::{open_file, OpenFlags};
|
||||
pub use task::{TaskControlBlock, TaskStatus};
|
||||
|
||||
pub use context::TaskContext;
|
||||
pub use manager::add_task;
|
||||
pub use pid::{pid_alloc, KernelStack, PidHandle};
|
||||
pub use processor::{
|
||||
current_task, current_trap_cx, current_user_token, run_tasks, schedule, take_current_task,
|
||||
};
|
||||
|
||||
/// Make current task suspended and switch to the next task
|
||||
pub fn suspend_current_and_run_next() {
|
||||
// There must be an application running.
|
||||
let task = take_current_task().unwrap();
|
||||
|
||||
// ---- 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_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 current task, recycle process resources and switch to the next task
|
||||
pub fn exit_current_and_run_next(exit_code: i32) {
|
||||
// take from Processor
|
||||
let task = take_current_task().unwrap();
|
||||
// **** 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
|
||||
|
||||
// ++++++ access initproc TCB exclusively
|
||||
{
|
||||
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
|
||||
|
||||
inner.children.clear();
|
||||
// deallocate user space
|
||||
inner.memory_set.recycle_data_pages();
|
||||
drop(inner);
|
||||
// **** release current PCB
|
||||
// drop task manually to maintain rc correctly
|
||||
drop(task);
|
||||
// we do not have to save task context
|
||||
let mut _unused = TaskContext::zero_init();
|
||||
schedule(&mut _unused as *mut _);
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// Creation of initial process
|
||||
///
|
||||
/// the name "initproc" may be changed to any other app name like "usertests",
|
||||
/// but we have user_shell, so we don't need to change it.
|
||||
pub static ref INITPROC: Arc<TaskControlBlock> = Arc::new({
|
||||
let inode = open_file("ch6b_initproc", OpenFlags::RDONLY).unwrap();
|
||||
let v = inode.read_all();
|
||||
TaskControlBlock::new(v.as_slice())
|
||||
});
|
||||
}
|
||||
|
||||
pub fn add_initproc() {
|
||||
add_task(INITPROC.clone());
|
||||
}
|
||||
116
os6-ref/src/task/pid.rs
Normal file
116
os6-ref/src/task/pid.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
//! Task pid implementation.
|
||||
//!
|
||||
//! Assign PID to the process here. At the same time, the position of the application KernelStack
|
||||
//! is determined according to the PID.
|
||||
|
||||
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::*;
|
||||
|
||||
/// Process identifier allocator using stack allocation
|
||||
struct PidAllocator {
|
||||
/// A new PID to be assigned
|
||||
current: usize,
|
||||
/// Recycled PID sequence
|
||||
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! {
|
||||
/// Pid allocator instance through lazy_static!
|
||||
static ref PID_ALLOCATOR: UPSafeCell<PidAllocator> =
|
||||
unsafe { UPSafeCell::new(PidAllocator::new()) };
|
||||
}
|
||||
|
||||
/// Abstract structure of PID
|
||||
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)
|
||||
}
|
||||
|
||||
/// KernelStack corresponding to PID
|
||||
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)]
|
||||
/// Push a variable of type T into the top of the KernelStack and return its raw pointer
|
||||
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());
|
||||
}
|
||||
}
|
||||
105
os6-ref/src/task/processor.rs
Normal file
105
os6-ref/src/task/processor.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
//! Implementation of [`Processor`] and Intersection of control flow
|
||||
//!
|
||||
//! Here, the continuous operation of user apps in CPU is maintained,
|
||||
//! the current running state of CPU is recorded,
|
||||
//! and the replacement and transfer of control flow of different applications are executed.
|
||||
|
||||
|
||||
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 {
|
||||
/// The task currently executing on the current processor
|
||||
current: Option<Arc<TaskControlBlock>>,
|
||||
/// The basic control flow of each core, helping to select and switch process
|
||||
idle_task_cx: TaskContext,
|
||||
}
|
||||
|
||||
impl Processor {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
current: None,
|
||||
idle_task_cx: TaskContext::zero_init(),
|
||||
}
|
||||
}
|
||||
fn get_idle_task_cx_ptr(&mut self) -> *mut TaskContext {
|
||||
&mut self.idle_task_cx as *mut _
|
||||
}
|
||||
pub fn take_current(&mut self) -> Option<Arc<TaskControlBlock>> {
|
||||
self.current.take()
|
||||
}
|
||||
pub fn current(&self) -> Option<Arc<TaskControlBlock>> {
|
||||
self.current.as_ref().map(|task| Arc::clone(task))
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// PROCESSOR instance through lazy_static!
|
||||
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() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current task through take, leaving a None in its place
|
||||
pub fn take_current_task() -> Option<Arc<TaskControlBlock>> {
|
||||
PROCESSOR.exclusive_access().take_current()
|
||||
}
|
||||
|
||||
/// Get a copy of the current task
|
||||
pub fn current_task() -> Option<Arc<TaskControlBlock>> {
|
||||
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.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()
|
||||
.inner_exclusive_access()
|
||||
.get_trap_cx()
|
||||
}
|
||||
|
||||
/// 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_ptr, idle_task_cx_ptr);
|
||||
}
|
||||
}
|
||||
34
os6-ref/src/task/switch.S
Normal file
34
os6-ref/src/task/switch.S
Normal file
@@ -0,0 +1,34 @@
|
||||
.altmacro
|
||||
.macro SAVE_SN n
|
||||
sd s\n, (\n+2)*8(a0)
|
||||
.endm
|
||||
.macro LOAD_SN n
|
||||
ld s\n, (\n+2)*8(a1)
|
||||
.endm
|
||||
.section .text
|
||||
.globl __switch
|
||||
__switch:
|
||||
# __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
|
||||
# restore ra & s0~s11 of next execution
|
||||
ld ra, 0(a1)
|
||||
.set n, 0
|
||||
.rept 12
|
||||
LOAD_SN %n
|
||||
.set n, n + 1
|
||||
.endr
|
||||
# restore kernel stack of next task
|
||||
ld sp, 8(a1)
|
||||
ret
|
||||
|
||||
16
os6-ref/src/task/switch.rs
Normal file
16
os6-ref/src/task/switch.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
//! Rust wrapper around `__switch`.
|
||||
//!
|
||||
//! Switching to a different task's context happens here. The actual
|
||||
//! implementation must not be in Rust and (essentially) has to be in assembly
|
||||
//! language (Do you know why?), so this module really is just a wrapper around
|
||||
//! `switch.S`.
|
||||
|
||||
core::arch::global_asm!(include_str!("switch.S"));
|
||||
|
||||
use super::TaskContext;
|
||||
|
||||
extern "C" {
|
||||
/// Switch to the context of `next_task_cx_ptr`, saving the current context
|
||||
/// in `current_task_cx_ptr`.
|
||||
pub fn __switch(current_task_cx_ptr: *mut TaskContext, next_task_cx_ptr: *const TaskContext);
|
||||
}
|
||||
229
os6-ref/src/task/task.rs
Normal file
229
os6-ref/src/task/task.rs
Normal file
@@ -0,0 +1,229 @@
|
||||
//! Types related to task management & Functions for completely changing TCB
|
||||
|
||||
use super::TaskContext;
|
||||
use super::{pid_alloc, KernelStack, PidHandle};
|
||||
use crate::config::TRAP_CONTEXT;
|
||||
use crate::mm::{MemorySet, PhysPageNum, VirtAddr, KERNEL_SPACE};
|
||||
use crate::sync::UPSafeCell;
|
||||
use crate::trap::{trap_handler, TrapContext};
|
||||
use alloc::sync::{Arc, Weak};
|
||||
use alloc::vec::Vec;
|
||||
use core::cell::RefMut;
|
||||
use crate::fs::{File, Stdin, Stdout};
|
||||
use alloc::string::String;
|
||||
use crate::mm::translated_refmut;
|
||||
|
||||
/// Task control block structure
|
||||
///
|
||||
/// Directly save the contents that will not change during running
|
||||
pub struct TaskControlBlock {
|
||||
// immutable
|
||||
/// Process identifier
|
||||
pub pid: PidHandle,
|
||||
/// Kernel stack corresponding to PID
|
||||
pub kernel_stack: KernelStack,
|
||||
// mutable
|
||||
inner: UPSafeCell<TaskControlBlockInner>,
|
||||
}
|
||||
|
||||
/// Structure containing more process content
|
||||
///
|
||||
/// Store the contents that will change during operation
|
||||
/// and are wrapped by UPSafeCell to provide mutual exclusion
|
||||
pub struct TaskControlBlockInner {
|
||||
/// The physical page number of the frame where the trap context is placed
|
||||
pub trap_cx_ppn: PhysPageNum,
|
||||
/// Application data can only appear in areas
|
||||
/// where the application address space is lower than base_size
|
||||
pub base_size: usize,
|
||||
/// Save task context
|
||||
pub task_cx: TaskContext,
|
||||
/// Maintain the execution status of the current process
|
||||
pub task_status: TaskStatus,
|
||||
/// Application address space
|
||||
pub memory_set: MemorySet,
|
||||
/// Parent process of the current process.
|
||||
/// Weak will not affect the reference count of the parent
|
||||
pub parent: Option<Weak<TaskControlBlock>>,
|
||||
/// A vector containing TCBs of all child processes of the current process
|
||||
pub children: Vec<Arc<TaskControlBlock>>,
|
||||
/// It is set when active exit or execution error occurs
|
||||
pub exit_code: i32,
|
||||
pub fd_table: Vec<Option<Arc<dyn File + Send + Sync>>>,
|
||||
}
|
||||
|
||||
/// Simple access to its internal fields
|
||||
impl TaskControlBlockInner {
|
||||
/*
|
||||
pub fn get_task_cx_ptr2(&self) -> *const usize {
|
||||
&self.task_cx_ptr as *const usize
|
||||
}
|
||||
*/
|
||||
pub fn get_trap_cx(&self) -> &'static mut TrapContext {
|
||||
self.trap_cx_ppn.get_mut()
|
||||
}
|
||||
pub fn get_user_token(&self) -> usize {
|
||||
self.memory_set.token()
|
||||
}
|
||||
fn get_status(&self) -> TaskStatus {
|
||||
self.task_status
|
||||
}
|
||||
pub fn is_zombie(&self) -> bool {
|
||||
self.get_status() == TaskStatus::Zombie
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TaskControlBlock {
|
||||
/// Get the mutex to get the RefMut TaskControlBlockInner
|
||||
pub fn inner_exclusive_access(&self) -> RefMut<'_, TaskControlBlockInner> {
|
||||
self.inner.exclusive_access()
|
||||
}
|
||||
|
||||
/// Create a new process
|
||||
///
|
||||
/// At present, it is only used for the creation of initproc
|
||||
pub fn new(elf_data: &[u8]) -> Self {
|
||||
// memory_set with elf program headers/trampoline/trap context/user stack
|
||||
let (memory_set, user_sp, entry_point) = MemorySet::from_elf(elf_data);
|
||||
let trap_cx_ppn = memory_set
|
||||
.translate(VirtAddr::from(TRAP_CONTEXT).into())
|
||||
.unwrap()
|
||||
.ppn();
|
||||
// alloc a pid and a kernel stack in kernel space
|
||||
let pid_handle = pid_alloc();
|
||||
let kernel_stack = KernelStack::new(&pid_handle);
|
||||
let kernel_stack_top = kernel_stack.get_top();
|
||||
// push a task context which goes to trap_return to the top of kernel stack
|
||||
let task_control_block = Self {
|
||||
pid: pid_handle,
|
||||
kernel_stack,
|
||||
inner: unsafe {
|
||||
UPSafeCell::new(TaskControlBlockInner {
|
||||
trap_cx_ppn,
|
||||
base_size: user_sp,
|
||||
task_cx: TaskContext::goto_trap_return(kernel_stack_top),
|
||||
task_status: TaskStatus::Ready,
|
||||
memory_set,
|
||||
parent: None,
|
||||
children: Vec::new(),
|
||||
exit_code: 0,
|
||||
fd_table: alloc::vec![
|
||||
// 0 -> stdin
|
||||
Some(Arc::new(Stdin)),
|
||||
// 1 -> stdout
|
||||
Some(Arc::new(Stdout)),
|
||||
// 2 -> stderr
|
||||
Some(Arc::new(Stdout)),
|
||||
],
|
||||
})
|
||||
},
|
||||
};
|
||||
// prepare TrapContext in user space
|
||||
let trap_cx = task_control_block.inner_exclusive_access().get_trap_cx();
|
||||
*trap_cx = TrapContext::app_init_context(
|
||||
entry_point,
|
||||
user_sp,
|
||||
KERNEL_SPACE.exclusive_access().token(),
|
||||
kernel_stack_top,
|
||||
trap_handler as usize,
|
||||
);
|
||||
task_control_block
|
||||
}
|
||||
/// Load a new elf to replace the original application address space and start execution
|
||||
pub fn exec(&self, elf_data: &[u8]) {
|
||||
// memory_set with elf program headers/trampoline/trap context/user stack
|
||||
let (memory_set, mut user_sp, entry_point) = MemorySet::from_elf(elf_data);
|
||||
let trap_cx_ppn = memory_set
|
||||
.translate(VirtAddr::from(TRAP_CONTEXT).into())
|
||||
.unwrap()
|
||||
.ppn();
|
||||
// **** access inner exclusively
|
||||
let mut inner = self.inner_exclusive_access();
|
||||
// substitute memory_set
|
||||
inner.memory_set = memory_set;
|
||||
// update trap_cx ppn
|
||||
inner.trap_cx_ppn = trap_cx_ppn;
|
||||
// initialize trap_cx
|
||||
let trap_cx = inner.get_trap_cx();
|
||||
*trap_cx = TrapContext::app_init_context(
|
||||
entry_point,
|
||||
user_sp,
|
||||
KERNEL_SPACE.exclusive_access().token(),
|
||||
self.kernel_stack.get_top(),
|
||||
trap_handler as usize,
|
||||
);
|
||||
// **** release inner automatically
|
||||
}
|
||||
/// Fork from parent to child
|
||||
pub fn fork(self: &Arc<TaskControlBlock>) -> Arc<TaskControlBlock> {
|
||||
// ---- access parent PCB exclusively
|
||||
let mut parent_inner = self.inner_exclusive_access();
|
||||
// copy user space(include trap context)
|
||||
let memory_set = MemorySet::from_existed_user(&parent_inner.memory_set);
|
||||
let trap_cx_ppn = memory_set
|
||||
.translate(VirtAddr::from(TRAP_CONTEXT).into())
|
||||
.unwrap()
|
||||
.ppn();
|
||||
// alloc a pid and a kernel stack in kernel space
|
||||
let pid_handle = pid_alloc();
|
||||
let kernel_stack = KernelStack::new(&pid_handle);
|
||||
let kernel_stack_top = kernel_stack.get_top();
|
||||
let mut new_fd_table: Vec<Option<Arc<dyn File + Send + Sync>>> = Vec::new();
|
||||
// clone all fds from parent to child
|
||||
for fd in parent_inner.fd_table.iter() {
|
||||
if let Some(file) = fd {
|
||||
new_fd_table.push(Some(file.clone()));
|
||||
} else {
|
||||
new_fd_table.push(None);
|
||||
}
|
||||
}
|
||||
let task_control_block = Arc::new(TaskControlBlock {
|
||||
pid: pid_handle,
|
||||
kernel_stack,
|
||||
inner: unsafe {
|
||||
UPSafeCell::new(TaskControlBlockInner {
|
||||
trap_cx_ppn,
|
||||
base_size: parent_inner.base_size,
|
||||
task_cx: TaskContext::goto_trap_return(kernel_stack_top),
|
||||
task_status: TaskStatus::Ready,
|
||||
memory_set,
|
||||
parent: Some(Arc::downgrade(self)),
|
||||
children: Vec::new(),
|
||||
exit_code: 0,
|
||||
fd_table: new_fd_table,
|
||||
})
|
||||
},
|
||||
});
|
||||
// add child
|
||||
parent_inner.children.push(task_control_block.clone());
|
||||
// modify kernel_sp in trap_cx
|
||||
// **** access children PCB exclusively
|
||||
let trap_cx = task_control_block.inner_exclusive_access().get_trap_cx();
|
||||
trap_cx.kernel_sp = kernel_stack_top;
|
||||
// return
|
||||
task_control_block
|
||||
// ---- release parent PCB automatically
|
||||
// **** release children PCB automatically
|
||||
}
|
||||
pub fn getpid(&self) -> usize {
|
||||
self.pid.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
/// task status: UnInit, Ready, Running, Exited
|
||||
pub enum TaskStatus {
|
||||
UnInit,
|
||||
Ready,
|
||||
Running,
|
||||
Zombie,
|
||||
}
|
||||
23
os6-ref/src/timer.rs
Normal file
23
os6-ref/src/timer.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
//! RISC-V timer-related functionality
|
||||
|
||||
use crate::config::CLOCK_FREQ;
|
||||
use crate::sbi::set_timer;
|
||||
use riscv::register::time;
|
||||
|
||||
const TICKS_PER_SEC: usize = 100;
|
||||
const MICRO_PER_SEC: usize = 1_000_000;
|
||||
|
||||
/// read the `mtime` register
|
||||
pub fn get_time() -> usize {
|
||||
time::read()
|
||||
}
|
||||
|
||||
/// get current time in microseconds
|
||||
pub fn get_time_us() -> usize {
|
||||
time::read() / (CLOCK_FREQ / MICRO_PER_SEC)
|
||||
}
|
||||
|
||||
/// set the next timer interrupt
|
||||
pub fn set_next_trigger() {
|
||||
set_timer(get_time() + CLOCK_FREQ / TICKS_PER_SEC);
|
||||
}
|
||||
47
os6-ref/src/trap/context.rs
Normal file
47
os6-ref/src/trap/context.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
//! Implementation of [`TrapContext`]
|
||||
|
||||
use riscv::register::sstatus::{self, Sstatus, SPP};
|
||||
|
||||
#[repr(C)]
|
||||
/// trap context structure containing sstatus, sepc and registers
|
||||
pub struct TrapContext {
|
||||
/// General-Purpose Register x0-31
|
||||
pub x: [usize; 32],
|
||||
/// sstatus
|
||||
pub sstatus: Sstatus,
|
||||
/// sepc
|
||||
pub sepc: usize,
|
||||
/// Token of kernel address space
|
||||
pub kernel_satp: usize,
|
||||
/// Kernel stack pointer of the current application
|
||||
pub kernel_sp: usize,
|
||||
/// Virtual address of trap handler entry point in kernel
|
||||
pub trap_handler: usize,
|
||||
}
|
||||
|
||||
impl TrapContext {
|
||||
pub fn set_sp(&mut self, sp: usize) {
|
||||
self.x[2] = sp;
|
||||
}
|
||||
pub fn app_init_context(
|
||||
entry: usize,
|
||||
sp: usize,
|
||||
kernel_satp: usize,
|
||||
kernel_sp: usize,
|
||||
trap_handler: usize,
|
||||
) -> Self {
|
||||
let mut sstatus = sstatus::read();
|
||||
// set CPU privilege to User after trapping back
|
||||
sstatus.set_spp(SPP::User);
|
||||
let mut cx = Self {
|
||||
x: [0; 32],
|
||||
sstatus,
|
||||
sepc: entry,
|
||||
kernel_satp,
|
||||
kernel_sp,
|
||||
trap_handler,
|
||||
};
|
||||
cx.set_sp(sp);
|
||||
cx
|
||||
}
|
||||
}
|
||||
131
os6-ref/src/trap/mod.rs
Normal file
131
os6-ref/src/trap/mod.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
//! Trap handling functionality
|
||||
//!
|
||||
//! For rCore, we have a single trap entry point, namely `__alltraps`. At
|
||||
//! initialization in [`init()`], we set the `stvec` CSR to point to it.
|
||||
//!
|
||||
//! All traps go through `__alltraps`, which is defined in `trap.S`. The
|
||||
//! assembly language code does just enough work restore the kernel space
|
||||
//! context, ensuring that Rust code safely runs, and transfers control to
|
||||
//! [`trap_handler()`].
|
||||
//!
|
||||
//! It then calls different functionality based on what exactly the exception
|
||||
//! was. For example, timer interrupts trigger task preemption, and syscalls go
|
||||
//! to [`syscall()`].
|
||||
|
||||
mod context;
|
||||
|
||||
use crate::config::{TRAMPOLINE, TRAP_CONTEXT};
|
||||
use crate::syscall::syscall;
|
||||
use crate::task::{
|
||||
current_trap_cx, current_user_token, exit_current_and_run_next, suspend_current_and_run_next,
|
||||
};
|
||||
use crate::timer::set_next_trigger;
|
||||
use riscv::register::{
|
||||
mtvec::TrapMode,
|
||||
scause::{self, Exception, Interrupt, Trap},
|
||||
sie, stval, stvec,
|
||||
};
|
||||
|
||||
core::arch::global_asm!(include_str!("trap.S"));
|
||||
|
||||
pub fn init() {
|
||||
set_kernel_trap_entry();
|
||||
}
|
||||
|
||||
fn set_kernel_trap_entry() {
|
||||
unsafe {
|
||||
stvec::write(trap_from_kernel as usize, TrapMode::Direct);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_user_trap_entry() {
|
||||
unsafe {
|
||||
stvec::write(TRAMPOLINE as usize, TrapMode::Direct);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enable_timer_interrupt() {
|
||||
unsafe {
|
||||
sie::set_stimer();
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn trap_handler() -> ! {
|
||||
set_kernel_trap_entry();
|
||||
let scause = scause::read();
|
||||
let stval = stval::read();
|
||||
match scause.cause() {
|
||||
Trap::Exception(Exception::UserEnvCall) => {
|
||||
// jump to next instruction anyway
|
||||
let mut cx = current_trap_cx();
|
||||
cx.sepc += 4;
|
||||
// get system call return value
|
||||
let result = syscall(cx.x[17], [cx.x[10], cx.x[11], cx.x[12], cx.x[13]]);
|
||||
// cx is changed during sys_exec, so we have to call it again
|
||||
cx = current_trap_cx();
|
||||
cx.x[10] = result as usize;
|
||||
}
|
||||
Trap::Exception(Exception::StoreFault)
|
||||
| Trap::Exception(Exception::StorePageFault)
|
||||
| Trap::Exception(Exception::InstructionFault)
|
||||
| Trap::Exception(Exception::InstructionPageFault)
|
||||
| Trap::Exception(Exception::LoadFault)
|
||||
| Trap::Exception(Exception::LoadPageFault) => {
|
||||
println!(
|
||||
"[kernel] {:?} in application, bad addr = {:#x}, bad instruction = {:#x}, core dumped.",
|
||||
scause.cause(),
|
||||
stval,
|
||||
current_trap_cx().sepc,
|
||||
);
|
||||
// page fault exit code
|
||||
exit_current_and_run_next(-2);
|
||||
}
|
||||
Trap::Exception(Exception::IllegalInstruction) => {
|
||||
println!("[kernel] IllegalInstruction in application, core dumped.");
|
||||
// illegal instruction exit code
|
||||
exit_current_and_run_next(-3);
|
||||
}
|
||||
Trap::Interrupt(Interrupt::SupervisorTimer) => {
|
||||
set_next_trigger();
|
||||
suspend_current_and_run_next();
|
||||
}
|
||||
_ => {
|
||||
panic!(
|
||||
"Unsupported trap {:?}, stval = {:#x}!",
|
||||
scause.cause(),
|
||||
stval
|
||||
);
|
||||
}
|
||||
}
|
||||
trap_return();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn trap_return() -> ! {
|
||||
set_user_trap_entry();
|
||||
let trap_cx_ptr = TRAP_CONTEXT;
|
||||
let user_satp = current_user_token();
|
||||
extern "C" {
|
||||
fn __alltraps();
|
||||
fn __restore();
|
||||
}
|
||||
let restore_va = __restore as usize - __alltraps as usize + TRAMPOLINE;
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"fence.i",
|
||||
"jr {restore_va}",
|
||||
restore_va = in(reg) restore_va,
|
||||
in("a0") trap_cx_ptr,
|
||||
in("a1") user_satp,
|
||||
options(noreturn)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn trap_from_kernel() -> ! {
|
||||
panic!("a trap {:?} from kernel!", scause::read().cause());
|
||||
}
|
||||
|
||||
pub use context::TrapContext;
|
||||
69
os6-ref/src/trap/trap.S
Normal file
69
os6-ref/src/trap/trap.S
Normal file
@@ -0,0 +1,69 @@
|
||||
.altmacro
|
||||
.macro SAVE_GP n
|
||||
sd x\n, \n*8(sp)
|
||||
.endm
|
||||
.macro LOAD_GP n
|
||||
ld x\n, \n*8(sp)
|
||||
.endm
|
||||
.section .text.trampoline
|
||||
.globl __alltraps
|
||||
.globl __restore
|
||||
.align 2
|
||||
__alltraps:
|
||||
csrrw sp, sscratch, sp
|
||||
# now sp->*TrapContext in user space, sscratch->user stack
|
||||
# save other general purpose registers
|
||||
sd x1, 1*8(sp)
|
||||
# skip sp(x2), we will save it later
|
||||
sd x3, 3*8(sp)
|
||||
# skip tp(x4), application does not use it
|
||||
# save x5~x31
|
||||
.set n, 5
|
||||
.rept 27
|
||||
SAVE_GP %n
|
||||
.set n, n+1
|
||||
.endr
|
||||
# we can use t0/t1/t2 freely, because they have been saved in TrapContext
|
||||
csrr t0, sstatus
|
||||
csrr t1, sepc
|
||||
sd t0, 32*8(sp)
|
||||
sd t1, 33*8(sp)
|
||||
# read user stack from sscratch and save it in TrapContext
|
||||
csrr t2, sscratch
|
||||
sd t2, 2*8(sp)
|
||||
# load kernel_satp into t0
|
||||
ld t0, 34*8(sp)
|
||||
# load trap_handler into t1
|
||||
ld t1, 36*8(sp)
|
||||
# move to kernel_sp
|
||||
ld sp, 35*8(sp)
|
||||
# switch to kernel space
|
||||
csrw satp, t0
|
||||
sfence.vma
|
||||
# jump to trap_handler
|
||||
jr t1
|
||||
|
||||
__restore:
|
||||
# a0: *TrapContext in user space(Constant); a1: user space token
|
||||
# switch to user space
|
||||
csrw satp, a1
|
||||
sfence.vma
|
||||
csrw sscratch, a0
|
||||
mv sp, a0
|
||||
# now sp points to TrapContext in user space, start restoring based on it
|
||||
# restore sstatus/sepc
|
||||
ld t0, 32*8(sp)
|
||||
ld t1, 33*8(sp)
|
||||
csrw sstatus, t0
|
||||
csrw sepc, t1
|
||||
# restore general purpose registers except x0/sp/tp
|
||||
ld x1, 1*8(sp)
|
||||
ld x3, 3*8(sp)
|
||||
.set n, 5
|
||||
.rept 27
|
||||
LOAD_GP %n
|
||||
.set n, n+1
|
||||
.endr
|
||||
# back to user stack
|
||||
ld sp, 2*8(sp)
|
||||
sret
|
||||
Reference in New Issue
Block a user