mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-24 10:26:25 +04:00
cargo clippy & fmt
This commit is contained in:
parent
5d518349e9
commit
ec25d32cf9
27
os/build.rs
27
os/build.rs
@ -1,5 +1,5 @@
|
|||||||
|
use std::fs::{read_dir, File};
|
||||||
use std::io::{Result, Write};
|
use std::io::{Result, Write};
|
||||||
use std::fs::{File, read_dir};
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("cargo:rerun-if-changed=../user/src/");
|
println!("cargo:rerun-if-changed=../user/src/");
|
||||||
@ -22,35 +22,46 @@ fn insert_app_data() -> Result<()> {
|
|||||||
.collect();
|
.collect();
|
||||||
apps.sort();
|
apps.sort();
|
||||||
|
|
||||||
writeln!(f, r#"
|
writeln!(
|
||||||
|
f,
|
||||||
|
r#"
|
||||||
.align 3
|
.align 3
|
||||||
.section .data
|
.section .data
|
||||||
.global _num_app
|
.global _num_app
|
||||||
_num_app:
|
_num_app:
|
||||||
.quad {}"#, apps.len())?;
|
.quad {}"#,
|
||||||
|
apps.len()
|
||||||
|
)?;
|
||||||
|
|
||||||
for i in 0..apps.len() {
|
for i in 0..apps.len() {
|
||||||
writeln!(f, r#" .quad app_{}_start"#, i)?;
|
writeln!(f, r#" .quad app_{}_start"#, i)?;
|
||||||
}
|
}
|
||||||
writeln!(f, r#" .quad app_{}_end"#, apps.len() - 1)?;
|
writeln!(f, r#" .quad app_{}_end"#, apps.len() - 1)?;
|
||||||
|
|
||||||
writeln!(f, r#"
|
writeln!(
|
||||||
|
f,
|
||||||
|
r#"
|
||||||
.global _app_names
|
.global _app_names
|
||||||
_app_names:"#)?;
|
_app_names:"#
|
||||||
|
)?;
|
||||||
for app in apps.iter() {
|
for app in apps.iter() {
|
||||||
writeln!(f, r#" .string "{}""#, app)?;
|
writeln!(f, r#" .string "{}""#, app)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (idx, app) in apps.iter().enumerate() {
|
for (idx, app) in apps.iter().enumerate() {
|
||||||
println!("app_{}: {}", idx, app);
|
println!("app_{}: {}", idx, app);
|
||||||
writeln!(f, r#"
|
writeln!(
|
||||||
|
f,
|
||||||
|
r#"
|
||||||
.section .data
|
.section .data
|
||||||
.global app_{0}_start
|
.global app_{0}_start
|
||||||
.global app_{0}_end
|
.global app_{0}_end
|
||||||
.align 3
|
.align 3
|
||||||
app_{0}_start:
|
app_{0}_start:
|
||||||
.incbin "{2}{1}"
|
.incbin "{2}{1}"
|
||||||
app_{0}_end:"#, idx, app, TARGET_PATH)?;
|
app_{0}_end:"#,
|
||||||
|
idx, app, TARGET_PATH
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use core::fmt::{self, Write};
|
|
||||||
use crate::sbi::console_putchar;
|
use crate::sbi::console_putchar;
|
||||||
|
use core::fmt::{self, Write};
|
||||||
|
|
||||||
struct Stdout;
|
struct Stdout;
|
||||||
|
|
||||||
@ -29,5 +29,3 @@ macro_rules! println {
|
|||||||
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
|
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
use core::panic::PanicInfo;
|
|
||||||
use crate::sbi::shutdown;
|
use crate::sbi::shutdown;
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(info: &PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
if let Some(location) = info.location() {
|
if let Some(location) = info.location() {
|
||||||
println!("[kernel] Panicked at {}:{} {}", location.file(), location.line(), info.message().unwrap());
|
println!(
|
||||||
|
"[kernel] Panicked at {}:{} {}",
|
||||||
|
location.file(),
|
||||||
|
location.line(),
|
||||||
|
info.message().unwrap()
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
println!("[kernel] Panicked: {}", info.message().unwrap());
|
println!("[kernel] Panicked: {}", info.message().unwrap());
|
||||||
}
|
}
|
||||||
|
@ -2,22 +2,24 @@ use alloc::vec::Vec;
|
|||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
|
|
||||||
pub fn get_num_app() -> usize {
|
pub fn get_num_app() -> usize {
|
||||||
extern "C" { fn _num_app(); }
|
extern "C" {
|
||||||
|
fn _num_app();
|
||||||
|
}
|
||||||
unsafe { (_num_app as usize as *const usize).read_volatile() }
|
unsafe { (_num_app as usize as *const usize).read_volatile() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_app_data(app_id: usize) -> &'static [u8] {
|
pub fn get_app_data(app_id: usize) -> &'static [u8] {
|
||||||
extern "C" { fn _num_app(); }
|
extern "C" {
|
||||||
|
fn _num_app();
|
||||||
|
}
|
||||||
let num_app_ptr = _num_app as usize as *const usize;
|
let num_app_ptr = _num_app as usize as *const usize;
|
||||||
let num_app = get_num_app();
|
let num_app = get_num_app();
|
||||||
let app_start = unsafe {
|
let app_start = unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) };
|
||||||
core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1)
|
|
||||||
};
|
|
||||||
assert!(app_id < num_app);
|
assert!(app_id < num_app);
|
||||||
unsafe {
|
unsafe {
|
||||||
core::slice::from_raw_parts(
|
core::slice::from_raw_parts(
|
||||||
app_start[app_id] as *const u8,
|
app_start[app_id] as *const u8,
|
||||||
app_start[app_id + 1] - app_start[app_id]
|
app_start[app_id + 1] - app_start[app_id],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -25,7 +27,9 @@ pub fn get_app_data(app_id: usize) -> &'static [u8] {
|
|||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref APP_NAMES: Vec<&'static str> = {
|
static ref APP_NAMES: Vec<&'static str> = {
|
||||||
let num_app = get_num_app();
|
let num_app = get_num_app();
|
||||||
extern "C" { fn _app_names(); }
|
extern "C" {
|
||||||
|
fn _app_names();
|
||||||
|
}
|
||||||
let mut start = _app_names as usize as *const u8;
|
let mut start = _app_names as usize as *const u8;
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -44,7 +48,6 @@ lazy_static! {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub fn get_app_data_by_name(name: &str) -> Option<&'static [u8]> {
|
pub fn get_app_data_by_name(name: &str) -> Option<&'static [u8]> {
|
||||||
let num_app = get_num_app();
|
let num_app = get_num_app();
|
||||||
@ -59,4 +62,4 @@ pub fn list_apps() {
|
|||||||
println!("{}", app);
|
println!("{}", app);
|
||||||
}
|
}
|
||||||
println!("**************/");
|
println!("**************/");
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,16 @@ extern crate bitflags;
|
|||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod console;
|
mod console;
|
||||||
mod lang_items;
|
|
||||||
mod sbi;
|
|
||||||
mod syscall;
|
|
||||||
mod trap;
|
|
||||||
mod loader;
|
|
||||||
mod config;
|
mod config;
|
||||||
|
mod lang_items;
|
||||||
|
mod loader;
|
||||||
|
mod mm;
|
||||||
|
mod sbi;
|
||||||
|
mod sync;
|
||||||
|
mod syscall;
|
||||||
mod task;
|
mod task;
|
||||||
mod timer;
|
mod timer;
|
||||||
mod sync;
|
mod trap;
|
||||||
mod mm;
|
|
||||||
|
|
||||||
use core::arch::global_asm;
|
use core::arch::global_asm;
|
||||||
|
|
||||||
@ -32,10 +32,8 @@ fn clear_bss() {
|
|||||||
fn ebss();
|
fn ebss();
|
||||||
}
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
core::slice::from_raw_parts_mut(
|
core::slice::from_raw_parts_mut(sbss as usize as *mut u8, ebss as usize - sbss as usize)
|
||||||
sbss as usize as *mut u8,
|
.fill(0);
|
||||||
ebss as usize - sbss as usize,
|
|
||||||
).fill(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,4 +51,4 @@ pub fn rust_main() -> ! {
|
|||||||
loader::list_apps();
|
loader::list_apps();
|
||||||
task::run_tasks();
|
task::run_tasks();
|
||||||
panic!("Unreachable in rust_main!");
|
panic!("Unreachable in rust_main!");
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::config::{PAGE_SIZE, PAGE_SIZE_BITS};
|
|
||||||
use super::PageTableEntry;
|
use super::PageTableEntry;
|
||||||
|
use crate::config::{PAGE_SIZE, PAGE_SIZE_BITS};
|
||||||
use core::fmt::{self, Debug, Formatter};
|
use core::fmt::{self, Debug, Formatter};
|
||||||
|
|
||||||
const PA_WIDTH_SV39: usize = 56;
|
const PA_WIDTH_SV39: usize = 56;
|
||||||
@ -48,35 +48,59 @@ impl Debug for PhysPageNum {
|
|||||||
/// usize -> T: usize.into()
|
/// usize -> T: usize.into()
|
||||||
|
|
||||||
impl From<usize> for PhysAddr {
|
impl From<usize> for PhysAddr {
|
||||||
fn from(v: usize) -> Self { Self(v & ( (1 << PA_WIDTH_SV39) - 1 )) }
|
fn from(v: usize) -> Self {
|
||||||
|
Self(v & ((1 << PA_WIDTH_SV39) - 1))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl From<usize> for PhysPageNum {
|
impl From<usize> for PhysPageNum {
|
||||||
fn from(v: usize) -> Self { Self(v & ( (1 << PPN_WIDTH_SV39) - 1 )) }
|
fn from(v: usize) -> Self {
|
||||||
|
Self(v & ((1 << PPN_WIDTH_SV39) - 1))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl From<usize> for VirtAddr {
|
impl From<usize> for VirtAddr {
|
||||||
fn from(v: usize) -> Self { Self(v & ( (1 << VA_WIDTH_SV39) - 1 )) }
|
fn from(v: usize) -> Self {
|
||||||
|
Self(v & ((1 << VA_WIDTH_SV39) - 1))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl From<usize> for VirtPageNum {
|
impl From<usize> for VirtPageNum {
|
||||||
fn from(v: usize) -> Self { Self(v & ( (1 << VPN_WIDTH_SV39) - 1 )) }
|
fn from(v: usize) -> Self {
|
||||||
|
Self(v & ((1 << VPN_WIDTH_SV39) - 1))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl From<PhysAddr> for usize {
|
impl From<PhysAddr> for usize {
|
||||||
fn from(v: PhysAddr) -> Self { v.0 }
|
fn from(v: PhysAddr) -> Self {
|
||||||
|
v.0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl From<PhysPageNum> for usize {
|
impl From<PhysPageNum> for usize {
|
||||||
fn from(v: PhysPageNum) -> Self { v.0 }
|
fn from(v: PhysPageNum) -> Self {
|
||||||
|
v.0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl From<VirtAddr> for usize {
|
impl From<VirtAddr> for usize {
|
||||||
fn from(v: VirtAddr) -> Self { v.0 }
|
fn from(v: VirtAddr) -> Self {
|
||||||
|
v.0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl From<VirtPageNum> for usize {
|
impl From<VirtPageNum> for usize {
|
||||||
fn from(v: VirtPageNum) -> Self { v.0 }
|
fn from(v: VirtPageNum) -> Self {
|
||||||
|
v.0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VirtAddr {
|
impl VirtAddr {
|
||||||
pub fn floor(&self) -> VirtPageNum { VirtPageNum(self.0 / PAGE_SIZE) }
|
pub fn floor(&self) -> VirtPageNum {
|
||||||
pub fn ceil(&self) -> VirtPageNum { VirtPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE) }
|
VirtPageNum(self.0 / PAGE_SIZE)
|
||||||
pub fn page_offset(&self) -> usize { self.0 & (PAGE_SIZE - 1) }
|
}
|
||||||
pub fn aligned(&self) -> bool { self.page_offset() == 0 }
|
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 {
|
impl From<VirtAddr> for VirtPageNum {
|
||||||
fn from(v: VirtAddr) -> Self {
|
fn from(v: VirtAddr) -> Self {
|
||||||
@ -85,13 +109,23 @@ impl From<VirtAddr> for VirtPageNum {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<VirtPageNum> for VirtAddr {
|
impl From<VirtPageNum> for VirtAddr {
|
||||||
fn from(v: VirtPageNum) -> Self { Self(v.0 << PAGE_SIZE_BITS) }
|
fn from(v: VirtPageNum) -> Self {
|
||||||
|
Self(v.0 << PAGE_SIZE_BITS)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl PhysAddr {
|
impl PhysAddr {
|
||||||
pub fn floor(&self) -> PhysPageNum { PhysPageNum(self.0 / PAGE_SIZE) }
|
pub fn floor(&self) -> PhysPageNum {
|
||||||
pub fn ceil(&self) -> PhysPageNum { PhysPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE) }
|
PhysPageNum(self.0 / PAGE_SIZE)
|
||||||
pub fn page_offset(&self) -> usize { self.0 & (PAGE_SIZE - 1) }
|
}
|
||||||
pub fn aligned(&self) -> bool { self.page_offset() == 0 }
|
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 {
|
impl From<PhysAddr> for PhysPageNum {
|
||||||
fn from(v: PhysAddr) -> Self {
|
fn from(v: PhysAddr) -> Self {
|
||||||
@ -100,7 +134,9 @@ impl From<PhysAddr> for PhysPageNum {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<PhysPageNum> for PhysAddr {
|
impl From<PhysPageNum> for PhysAddr {
|
||||||
fn from(v: PhysPageNum) -> Self { Self(v.0 << PAGE_SIZE_BITS) }
|
fn from(v: PhysPageNum) -> Self {
|
||||||
|
Self(v.0 << PAGE_SIZE_BITS)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VirtPageNum {
|
impl VirtPageNum {
|
||||||
@ -117,23 +153,17 @@ impl VirtPageNum {
|
|||||||
|
|
||||||
impl PhysAddr {
|
impl PhysAddr {
|
||||||
pub fn get_mut<T>(&self) -> &'static mut T {
|
pub fn get_mut<T>(&self) -> &'static mut T {
|
||||||
unsafe {
|
unsafe { (self.0 as *mut T).as_mut().unwrap() }
|
||||||
(self.0 as *mut T).as_mut().unwrap()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl PhysPageNum {
|
impl PhysPageNum {
|
||||||
pub fn get_pte_array(&self) -> &'static mut [PageTableEntry] {
|
pub fn get_pte_array(&self) -> &'static mut [PageTableEntry] {
|
||||||
let pa: PhysAddr = self.clone().into();
|
let pa: PhysAddr = self.clone().into();
|
||||||
unsafe {
|
unsafe { core::slice::from_raw_parts_mut(pa.0 as *mut PageTableEntry, 512) }
|
||||||
core::slice::from_raw_parts_mut(pa.0 as *mut PageTableEntry, 512)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn get_bytes_array(&self) -> &'static mut [u8] {
|
pub fn get_bytes_array(&self) -> &'static mut [u8] {
|
||||||
let pa: PhysAddr = self.clone().into();
|
let pa: PhysAddr = self.clone().into();
|
||||||
unsafe {
|
unsafe { core::slice::from_raw_parts_mut(pa.0 as *mut u8, 4096) }
|
||||||
core::slice::from_raw_parts_mut(pa.0 as *mut u8, 4096)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn get_mut<T>(&self) -> &'static mut T {
|
pub fn get_mut<T>(&self) -> &'static mut T {
|
||||||
let pa: PhysAddr = self.clone().into();
|
let pa: PhysAddr = self.clone().into();
|
||||||
@ -151,41 +181,57 @@ impl StepByOne for VirtPageNum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct SimpleRange<T> where
|
pub struct SimpleRange<T>
|
||||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug, {
|
where
|
||||||
|
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||||
|
{
|
||||||
l: T,
|
l: T,
|
||||||
r: T,
|
r: T,
|
||||||
}
|
}
|
||||||
impl<T> SimpleRange<T> where
|
impl<T> SimpleRange<T>
|
||||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug, {
|
where
|
||||||
|
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||||
|
{
|
||||||
pub fn new(start: T, end: T) -> Self {
|
pub fn new(start: T, end: T) -> Self {
|
||||||
assert!(start <= end, "start {:?} > end {:?}!", start, end);
|
assert!(start <= end, "start {:?} > end {:?}!", start, end);
|
||||||
Self { l: start, r: end }
|
Self { l: start, r: end }
|
||||||
}
|
}
|
||||||
pub fn get_start(&self) -> T { self.l }
|
pub fn get_start(&self) -> T {
|
||||||
pub fn get_end(&self) -> T { self.r }
|
self.l
|
||||||
|
}
|
||||||
|
pub fn get_end(&self) -> T {
|
||||||
|
self.r
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl<T> IntoIterator for SimpleRange<T> where
|
impl<T> IntoIterator for SimpleRange<T>
|
||||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug, {
|
where
|
||||||
|
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||||
|
{
|
||||||
type Item = T;
|
type Item = T;
|
||||||
type IntoIter = SimpleRangeIterator<T>;
|
type IntoIter = SimpleRangeIterator<T>;
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
SimpleRangeIterator::new(self.l, self.r)
|
SimpleRangeIterator::new(self.l, self.r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub struct SimpleRangeIterator<T> where
|
pub struct SimpleRangeIterator<T>
|
||||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug, {
|
where
|
||||||
|
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||||
|
{
|
||||||
current: T,
|
current: T,
|
||||||
end: T,
|
end: T,
|
||||||
}
|
}
|
||||||
impl<T> SimpleRangeIterator<T> where
|
impl<T> SimpleRangeIterator<T>
|
||||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug, {
|
where
|
||||||
|
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||||
|
{
|
||||||
pub fn new(l: T, r: T) -> Self {
|
pub fn new(l: T, r: T) -> Self {
|
||||||
Self { current: l, end: r, }
|
Self { current: l, end: r }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T> Iterator for SimpleRangeIterator<T> where
|
impl<T> Iterator for SimpleRangeIterator<T>
|
||||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug, {
|
where
|
||||||
|
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||||
|
{
|
||||||
type Item = T;
|
type Item = T;
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
if self.current == self.end {
|
if self.current == self.end {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use super::{PhysAddr, PhysPageNum};
|
use super::{PhysAddr, PhysPageNum};
|
||||||
use alloc::vec::Vec;
|
|
||||||
use crate::sync::UPSafeCell;
|
|
||||||
use crate::config::MEMORY_END;
|
use crate::config::MEMORY_END;
|
||||||
use lazy_static::*;
|
use crate::sync::UPSafeCell;
|
||||||
|
use alloc::vec::Vec;
|
||||||
use core::fmt::{self, Debug, Formatter};
|
use core::fmt::{self, Debug, Formatter};
|
||||||
|
use lazy_static::*;
|
||||||
|
|
||||||
pub struct FrameTracker {
|
pub struct FrameTracker {
|
||||||
pub ppn: PhysPageNum,
|
pub ppn: PhysPageNum,
|
||||||
@ -62,22 +62,17 @@ impl FrameAllocator for StackFrameAllocator {
|
|||||||
fn alloc(&mut self) -> Option<PhysPageNum> {
|
fn alloc(&mut self) -> Option<PhysPageNum> {
|
||||||
if let Some(ppn) = self.recycled.pop() {
|
if let Some(ppn) = self.recycled.pop() {
|
||||||
Some(ppn.into())
|
Some(ppn.into())
|
||||||
|
} else if self.current == self.end {
|
||||||
|
None
|
||||||
} else {
|
} else {
|
||||||
if self.current == self.end {
|
self.current += 1;
|
||||||
None
|
Some((self.current - 1).into())
|
||||||
} else {
|
|
||||||
self.current += 1;
|
|
||||||
Some((self.current - 1).into())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn dealloc(&mut self, ppn: PhysPageNum) {
|
fn dealloc(&mut self, ppn: PhysPageNum) {
|
||||||
let ppn = ppn.0;
|
let ppn = ppn.0;
|
||||||
// validity check
|
// validity check
|
||||||
if ppn >= self.current || self.recycled
|
if ppn >= self.current || self.recycled.iter().find(|&v| *v == ppn).is_some() {
|
||||||
.iter()
|
|
||||||
.find(|&v| {*v == ppn})
|
|
||||||
.is_some() {
|
|
||||||
panic!("Frame ppn={:#x} has not been allocated!", ppn);
|
panic!("Frame ppn={:#x} has not been allocated!", ppn);
|
||||||
}
|
}
|
||||||
// recycle
|
// recycle
|
||||||
@ -88,18 +83,18 @@ impl FrameAllocator for StackFrameAllocator {
|
|||||||
type FrameAllocatorImpl = StackFrameAllocator;
|
type FrameAllocatorImpl = StackFrameAllocator;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref FRAME_ALLOCATOR: UPSafeCell<FrameAllocatorImpl> = unsafe {
|
pub static ref FRAME_ALLOCATOR: UPSafeCell<FrameAllocatorImpl> =
|
||||||
UPSafeCell::new(FrameAllocatorImpl::new())
|
unsafe { UPSafeCell::new(FrameAllocatorImpl::new()) };
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_frame_allocator() {
|
pub fn init_frame_allocator() {
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn ekernel();
|
fn ekernel();
|
||||||
}
|
}
|
||||||
FRAME_ALLOCATOR
|
FRAME_ALLOCATOR.exclusive_access().init(
|
||||||
.exclusive_access()
|
PhysAddr::from(ekernel as usize).ceil(),
|
||||||
.init(PhysAddr::from(ekernel as usize).ceil(), PhysAddr::from(MEMORY_END).floor());
|
PhysAddr::from(MEMORY_END).floor(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn frame_alloc() -> Option<FrameTracker> {
|
pub fn frame_alloc() -> Option<FrameTracker> {
|
||||||
@ -110,9 +105,7 @@ pub fn frame_alloc() -> Option<FrameTracker> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn frame_dealloc(ppn: PhysPageNum) {
|
fn frame_dealloc(ppn: PhysPageNum) {
|
||||||
FRAME_ALLOCATOR
|
FRAME_ALLOCATOR.exclusive_access().dealloc(ppn);
|
||||||
.exclusive_access()
|
|
||||||
.dealloc(ppn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
@ -131,4 +124,4 @@ pub fn frame_allocator_test() {
|
|||||||
}
|
}
|
||||||
drop(v);
|
drop(v);
|
||||||
println!("frame_allocator_test passed!");
|
println!("frame_allocator_test passed!");
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use buddy_system_allocator::LockedHeap;
|
|
||||||
use crate::config::KERNEL_HEAP_SIZE;
|
use crate::config::KERNEL_HEAP_SIZE;
|
||||||
|
use buddy_system_allocator::LockedHeap;
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
|
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
|
||||||
|
@ -1,21 +1,15 @@
|
|||||||
use super::{PageTable, PageTableEntry, PTEFlags};
|
use super::{frame_alloc, FrameTracker};
|
||||||
use super::{VirtPageNum, VirtAddr, PhysPageNum, PhysAddr};
|
use super::{PTEFlags, PageTable, PageTableEntry};
|
||||||
use super::{FrameTracker, frame_alloc};
|
use super::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum};
|
||||||
use super::{VPNRange, StepByOne};
|
use super::{StepByOne, VPNRange};
|
||||||
use alloc::collections::BTreeMap;
|
use crate::config::{MEMORY_END, PAGE_SIZE, TRAMPOLINE, TRAP_CONTEXT, USER_STACK_SIZE};
|
||||||
use alloc::vec::Vec;
|
|
||||||
use riscv::register::satp;
|
|
||||||
use alloc::sync::Arc;
|
|
||||||
use lazy_static::*;
|
|
||||||
use crate::sync::UPSafeCell;
|
use crate::sync::UPSafeCell;
|
||||||
use crate::config::{
|
use alloc::collections::BTreeMap;
|
||||||
MEMORY_END,
|
use alloc::sync::Arc;
|
||||||
PAGE_SIZE,
|
use alloc::vec::Vec;
|
||||||
TRAMPOLINE,
|
|
||||||
TRAP_CONTEXT,
|
|
||||||
USER_STACK_SIZE
|
|
||||||
};
|
|
||||||
use core::arch::asm;
|
use core::arch::asm;
|
||||||
|
use lazy_static::*;
|
||||||
|
use riscv::register::satp;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn stext();
|
fn stext();
|
||||||
@ -31,9 +25,8 @@ extern "C" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref KERNEL_SPACE: Arc<UPSafeCell<MemorySet>> = Arc::new(unsafe {
|
pub static ref KERNEL_SPACE: Arc<UPSafeCell<MemorySet>> =
|
||||||
UPSafeCell::new(MemorySet::new_kernel())
|
Arc::new(unsafe { UPSafeCell::new(MemorySet::new_kernel()) });
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MemorySet {
|
pub struct MemorySet {
|
||||||
@ -52,17 +45,24 @@ impl MemorySet {
|
|||||||
self.page_table.token()
|
self.page_table.token()
|
||||||
}
|
}
|
||||||
/// Assume that no conflicts.
|
/// Assume that no conflicts.
|
||||||
pub fn insert_framed_area(&mut self, start_va: VirtAddr, end_va: VirtAddr, permission: MapPermission) {
|
pub fn insert_framed_area(
|
||||||
self.push(MapArea::new(
|
&mut self,
|
||||||
start_va,
|
start_va: VirtAddr,
|
||||||
end_va,
|
end_va: VirtAddr,
|
||||||
MapType::Framed,
|
permission: MapPermission,
|
||||||
permission,
|
) {
|
||||||
), None);
|
self.push(
|
||||||
|
MapArea::new(start_va, end_va, MapType::Framed, permission),
|
||||||
|
None,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
pub fn remove_area_with_start_vpn(&mut self, start_vpn: VirtPageNum) {
|
pub fn remove_area_with_start_vpn(&mut self, start_vpn: VirtPageNum) {
|
||||||
if let Some((idx, area)) = self.areas.iter_mut().enumerate()
|
if let Some((idx, area)) = self
|
||||||
.find(|(_, area)| area.vpn_range.get_start() == start_vpn) {
|
.areas
|
||||||
|
.iter_mut()
|
||||||
|
.enumerate()
|
||||||
|
.find(|(_, area)| area.vpn_range.get_start() == start_vpn)
|
||||||
|
{
|
||||||
area.unmap(&mut self.page_table);
|
area.unmap(&mut self.page_table);
|
||||||
self.areas.remove(idx);
|
self.areas.remove(idx);
|
||||||
}
|
}
|
||||||
@ -91,42 +91,60 @@ impl MemorySet {
|
|||||||
println!(".text [{:#x}, {:#x})", stext as usize, etext as usize);
|
println!(".text [{:#x}, {:#x})", stext as usize, etext as usize);
|
||||||
println!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize);
|
println!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize);
|
||||||
println!(".data [{:#x}, {:#x})", sdata as usize, edata as usize);
|
println!(".data [{:#x}, {:#x})", sdata as usize, edata as usize);
|
||||||
println!(".bss [{:#x}, {:#x})", sbss_with_stack as usize, ebss as usize);
|
println!(
|
||||||
|
".bss [{:#x}, {:#x})",
|
||||||
|
sbss_with_stack as usize, ebss as usize
|
||||||
|
);
|
||||||
println!("mapping .text section");
|
println!("mapping .text section");
|
||||||
memory_set.push(MapArea::new(
|
memory_set.push(
|
||||||
(stext as usize).into(),
|
MapArea::new(
|
||||||
(etext as usize).into(),
|
(stext as usize).into(),
|
||||||
MapType::Identical,
|
(etext as usize).into(),
|
||||||
MapPermission::R | MapPermission::X,
|
MapType::Identical,
|
||||||
), None);
|
MapPermission::R | MapPermission::X,
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
);
|
||||||
println!("mapping .rodata section");
|
println!("mapping .rodata section");
|
||||||
memory_set.push(MapArea::new(
|
memory_set.push(
|
||||||
(srodata as usize).into(),
|
MapArea::new(
|
||||||
(erodata as usize).into(),
|
(srodata as usize).into(),
|
||||||
MapType::Identical,
|
(erodata as usize).into(),
|
||||||
MapPermission::R,
|
MapType::Identical,
|
||||||
), None);
|
MapPermission::R,
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
);
|
||||||
println!("mapping .data section");
|
println!("mapping .data section");
|
||||||
memory_set.push(MapArea::new(
|
memory_set.push(
|
||||||
(sdata as usize).into(),
|
MapArea::new(
|
||||||
(edata as usize).into(),
|
(sdata as usize).into(),
|
||||||
MapType::Identical,
|
(edata as usize).into(),
|
||||||
MapPermission::R | MapPermission::W,
|
MapType::Identical,
|
||||||
), None);
|
MapPermission::R | MapPermission::W,
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
);
|
||||||
println!("mapping .bss section");
|
println!("mapping .bss section");
|
||||||
memory_set.push(MapArea::new(
|
memory_set.push(
|
||||||
(sbss_with_stack as usize).into(),
|
MapArea::new(
|
||||||
(ebss as usize).into(),
|
(sbss_with_stack as usize).into(),
|
||||||
MapType::Identical,
|
(ebss as usize).into(),
|
||||||
MapPermission::R | MapPermission::W,
|
MapType::Identical,
|
||||||
), None);
|
MapPermission::R | MapPermission::W,
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
);
|
||||||
println!("mapping physical memory");
|
println!("mapping physical memory");
|
||||||
memory_set.push(MapArea::new(
|
memory_set.push(
|
||||||
(ekernel as usize).into(),
|
MapArea::new(
|
||||||
MEMORY_END.into(),
|
(ekernel as usize).into(),
|
||||||
MapType::Identical,
|
MEMORY_END.into(),
|
||||||
MapPermission::R | MapPermission::W,
|
MapType::Identical,
|
||||||
), None);
|
MapPermission::R | MapPermission::W,
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
);
|
||||||
memory_set
|
memory_set
|
||||||
}
|
}
|
||||||
/// Include sections in elf and trampoline and TrapContext and user stack,
|
/// Include sections in elf and trampoline and TrapContext and user stack,
|
||||||
@ -149,19 +167,20 @@ impl MemorySet {
|
|||||||
let end_va: VirtAddr = ((ph.virtual_addr() + ph.mem_size()) as usize).into();
|
let end_va: VirtAddr = ((ph.virtual_addr() + ph.mem_size()) as usize).into();
|
||||||
let mut map_perm = MapPermission::U;
|
let mut map_perm = MapPermission::U;
|
||||||
let ph_flags = ph.flags();
|
let ph_flags = ph.flags();
|
||||||
if ph_flags.is_read() { map_perm |= MapPermission::R; }
|
if ph_flags.is_read() {
|
||||||
if ph_flags.is_write() { map_perm |= MapPermission::W; }
|
map_perm |= MapPermission::R;
|
||||||
if ph_flags.is_execute() { map_perm |= MapPermission::X; }
|
}
|
||||||
let map_area = MapArea::new(
|
if ph_flags.is_write() {
|
||||||
start_va,
|
map_perm |= MapPermission::W;
|
||||||
end_va,
|
}
|
||||||
MapType::Framed,
|
if ph_flags.is_execute() {
|
||||||
map_perm,
|
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();
|
max_end_vpn = map_area.vpn_range.get_end();
|
||||||
memory_set.push(
|
memory_set.push(
|
||||||
map_area,
|
map_area,
|
||||||
Some(&elf.input[ph.offset() as usize..(ph.offset() + ph.file_size()) as usize])
|
Some(&elf.input[ph.offset() as usize..(ph.offset() + ph.file_size()) as usize]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -171,20 +190,30 @@ impl MemorySet {
|
|||||||
// guard page
|
// guard page
|
||||||
user_stack_bottom += PAGE_SIZE;
|
user_stack_bottom += PAGE_SIZE;
|
||||||
let user_stack_top = user_stack_bottom + USER_STACK_SIZE;
|
let user_stack_top = user_stack_bottom + USER_STACK_SIZE;
|
||||||
memory_set.push(MapArea::new(
|
memory_set.push(
|
||||||
user_stack_bottom.into(),
|
MapArea::new(
|
||||||
user_stack_top.into(),
|
user_stack_bottom.into(),
|
||||||
MapType::Framed,
|
user_stack_top.into(),
|
||||||
MapPermission::R | MapPermission::W | MapPermission::U,
|
MapType::Framed,
|
||||||
), None);
|
MapPermission::R | MapPermission::W | MapPermission::U,
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
);
|
||||||
// map TrapContext
|
// map TrapContext
|
||||||
memory_set.push(MapArea::new(
|
memory_set.push(
|
||||||
TRAP_CONTEXT.into(),
|
MapArea::new(
|
||||||
TRAMPOLINE.into(),
|
TRAP_CONTEXT.into(),
|
||||||
MapType::Framed,
|
TRAMPOLINE.into(),
|
||||||
MapPermission::R | MapPermission::W,
|
MapType::Framed,
|
||||||
), None);
|
MapPermission::R | MapPermission::W,
|
||||||
(memory_set, user_stack_top, elf.header.pt2.entry_point() as usize)
|
),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
(
|
||||||
|
memory_set,
|
||||||
|
user_stack_top,
|
||||||
|
elf.header.pt2.entry_point() as usize,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
pub fn from_existed_user(user_space: &MemorySet) -> MemorySet {
|
pub fn from_existed_user(user_space: &MemorySet) -> MemorySet {
|
||||||
let mut memory_set = Self::new_bare();
|
let mut memory_set = Self::new_bare();
|
||||||
@ -198,7 +227,9 @@ impl MemorySet {
|
|||||||
for vpn in area.vpn_range {
|
for vpn in area.vpn_range {
|
||||||
let src_ppn = user_space.translate(vpn).unwrap().ppn();
|
let src_ppn = user_space.translate(vpn).unwrap().ppn();
|
||||||
let dst_ppn = memory_set.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());
|
dst_ppn
|
||||||
|
.get_bytes_array()
|
||||||
|
.copy_from_slice(src_ppn.get_bytes_array());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
memory_set
|
memory_set
|
||||||
@ -231,7 +262,7 @@ impl MapArea {
|
|||||||
start_va: VirtAddr,
|
start_va: VirtAddr,
|
||||||
end_va: VirtAddr,
|
end_va: VirtAddr,
|
||||||
map_type: MapType,
|
map_type: MapType,
|
||||||
map_perm: MapPermission
|
map_perm: MapPermission,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let start_vpn: VirtPageNum = start_va.floor();
|
let start_vpn: VirtPageNum = start_va.floor();
|
||||||
let end_vpn: VirtPageNum = end_va.ceil();
|
let end_vpn: VirtPageNum = end_va.ceil();
|
||||||
@ -330,15 +361,27 @@ pub fn remap_test() {
|
|||||||
let mid_rodata: VirtAddr = ((srodata as usize + erodata 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();
|
let mid_data: VirtAddr = ((sdata as usize + edata as usize) / 2).into();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
kernel_space.page_table.translate(mid_text.floor()).unwrap().writable(),
|
kernel_space
|
||||||
|
.page_table
|
||||||
|
.translate(mid_text.floor())
|
||||||
|
.unwrap()
|
||||||
|
.writable(),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
kernel_space.page_table.translate(mid_rodata.floor()).unwrap().writable(),
|
kernel_space
|
||||||
|
.page_table
|
||||||
|
.translate(mid_rodata.floor())
|
||||||
|
.unwrap()
|
||||||
|
.writable(),
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
kernel_space.page_table.translate(mid_data.floor()).unwrap().executable(),
|
kernel_space
|
||||||
|
.page_table
|
||||||
|
.translate(mid_data.floor())
|
||||||
|
.unwrap()
|
||||||
|
.executable(),
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
println!("remap_test passed!");
|
println!("remap_test passed!");
|
||||||
|
@ -1,21 +1,16 @@
|
|||||||
mod heap_allocator;
|
|
||||||
mod address;
|
mod address;
|
||||||
mod frame_allocator;
|
mod frame_allocator;
|
||||||
mod page_table;
|
mod heap_allocator;
|
||||||
mod memory_set;
|
mod memory_set;
|
||||||
|
mod page_table;
|
||||||
|
|
||||||
use page_table::{PageTable, PTEFlags};
|
pub use address::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum};
|
||||||
use address::{VPNRange, StepByOne};
|
use address::{StepByOne, VPNRange};
|
||||||
pub use address::{PhysAddr, VirtAddr, PhysPageNum, VirtPageNum};
|
pub use frame_allocator::{frame_alloc, FrameTracker};
|
||||||
pub use frame_allocator::{FrameTracker, frame_alloc};
|
|
||||||
pub use page_table::{
|
|
||||||
PageTableEntry,
|
|
||||||
translated_byte_buffer,
|
|
||||||
translated_str,
|
|
||||||
translated_refmut,
|
|
||||||
};
|
|
||||||
pub use memory_set::{MemorySet, KERNEL_SPACE, MapPermission};
|
|
||||||
pub use memory_set::remap_test;
|
pub use memory_set::remap_test;
|
||||||
|
pub use memory_set::{MapPermission, MemorySet, KERNEL_SPACE};
|
||||||
|
pub use page_table::{translated_byte_buffer, translated_refmut, translated_str, PageTableEntry};
|
||||||
|
use page_table::{PTEFlags, PageTable};
|
||||||
|
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
heap_allocator::init_heap();
|
heap_allocator::init_heap();
|
||||||
|
@ -1,15 +1,7 @@
|
|||||||
use super::{
|
use super::{frame_alloc, FrameTracker, PhysAddr, PhysPageNum, StepByOne, VirtAddr, VirtPageNum};
|
||||||
frame_alloc,
|
|
||||||
PhysPageNum,
|
|
||||||
FrameTracker,
|
|
||||||
VirtPageNum,
|
|
||||||
VirtAddr,
|
|
||||||
PhysAddr,
|
|
||||||
StepByOne
|
|
||||||
};
|
|
||||||
use alloc::vec::Vec;
|
|
||||||
use alloc::vec;
|
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
|
use alloc::vec;
|
||||||
|
use alloc::vec::Vec;
|
||||||
use bitflags::*;
|
use bitflags::*;
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
@ -38,9 +30,7 @@ impl PageTableEntry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn empty() -> Self {
|
pub fn empty() -> Self {
|
||||||
PageTableEntry {
|
PageTableEntry { bits: 0 }
|
||||||
bits: 0,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn ppn(&self) -> PhysPageNum {
|
pub fn ppn(&self) -> PhysPageNum {
|
||||||
(self.bits >> 10 & ((1usize << 44) - 1)).into()
|
(self.bits >> 10 & ((1usize << 44) - 1)).into()
|
||||||
@ -132,19 +122,17 @@ impl PageTable {
|
|||||||
*pte = PageTableEntry::empty();
|
*pte = PageTableEntry::empty();
|
||||||
}
|
}
|
||||||
pub fn translate(&self, vpn: VirtPageNum) -> Option<PageTableEntry> {
|
pub fn translate(&self, vpn: VirtPageNum) -> Option<PageTableEntry> {
|
||||||
self.find_pte(vpn)
|
self.find_pte(vpn).map(|pte| pte.clone())
|
||||||
.map(|pte| {pte.clone()})
|
|
||||||
}
|
}
|
||||||
pub fn translate_va(&self, va: VirtAddr) -> Option<PhysAddr> {
|
pub fn translate_va(&self, va: VirtAddr) -> Option<PhysAddr> {
|
||||||
self.find_pte(va.clone().floor())
|
self.find_pte(va.clone().floor()).map(|pte| {
|
||||||
.map(|pte| {
|
//println!("translate_va:va = {:?}", va);
|
||||||
//println!("translate_va:va = {:?}", va);
|
let aligned_pa: PhysAddr = pte.ppn().into();
|
||||||
let aligned_pa: PhysAddr = pte.ppn().into();
|
//println!("translate_va:pa_align = {:?}", aligned_pa);
|
||||||
//println!("translate_va:pa_align = {:?}", aligned_pa);
|
let offset = va.page_offset();
|
||||||
let offset = va.page_offset();
|
let aligned_pa_usize: usize = aligned_pa.into();
|
||||||
let aligned_pa_usize: usize = aligned_pa.into();
|
(aligned_pa_usize + offset).into()
|
||||||
(aligned_pa_usize + offset).into()
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
pub fn token(&self) -> usize {
|
pub fn token(&self) -> usize {
|
||||||
8usize << 60 | self.root_ppn.0
|
8usize << 60 | self.root_ppn.0
|
||||||
@ -159,10 +147,7 @@ pub fn translated_byte_buffer(token: usize, ptr: *const u8, len: usize) -> Vec<&
|
|||||||
while start < end {
|
while start < end {
|
||||||
let start_va = VirtAddr::from(start);
|
let start_va = VirtAddr::from(start);
|
||||||
let mut vpn = start_va.floor();
|
let mut vpn = start_va.floor();
|
||||||
let ppn = page_table
|
let ppn = page_table.translate(vpn).unwrap().ppn();
|
||||||
.translate(vpn)
|
|
||||||
.unwrap()
|
|
||||||
.ppn();
|
|
||||||
vpn.step();
|
vpn.step();
|
||||||
let mut end_va: VirtAddr = vpn.into();
|
let mut end_va: VirtAddr = vpn.into();
|
||||||
end_va = end_va.min(VirtAddr::from(end));
|
end_va = end_va.min(VirtAddr::from(end));
|
||||||
@ -181,7 +166,10 @@ pub fn translated_str(token: usize, ptr: *const u8) -> String {
|
|||||||
let mut string = String::new();
|
let mut string = String::new();
|
||||||
let mut va = ptr as usize;
|
let mut va = ptr as usize;
|
||||||
loop {
|
loop {
|
||||||
let ch: u8 = *(page_table.translate_va(VirtAddr::from(va)).unwrap().get_mut());
|
let ch: u8 = *(page_table
|
||||||
|
.translate_va(VirtAddr::from(va))
|
||||||
|
.unwrap()
|
||||||
|
.get_mut());
|
||||||
if ch == 0 {
|
if ch == 0 {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
@ -197,6 +185,8 @@ pub fn translated_refmut<T>(token: usize, ptr: *mut T) -> &'static mut T {
|
|||||||
let page_table = PageTable::from_token(token);
|
let page_table = PageTable::from_token(token);
|
||||||
let va = ptr as usize;
|
let va = ptr as usize;
|
||||||
//println!("translated_refmut: before translate_va");
|
//println!("translated_refmut: before translate_va");
|
||||||
page_table.translate_va(VirtAddr::from(va)).unwrap().get_mut()
|
page_table
|
||||||
|
.translate_va(VirtAddr::from(va))
|
||||||
|
.unwrap()
|
||||||
|
.get_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,4 +43,3 @@ pub fn shutdown() -> ! {
|
|||||||
sbi_call(SBI_SHUTDOWN, 0, 0, 0);
|
sbi_call(SBI_SHUTDOWN, 0, 0, 0);
|
||||||
panic!("It should shutdown!");
|
panic!("It should shutdown!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
mod up;
|
mod up;
|
||||||
|
|
||||||
pub use up::UPSafeCell;
|
pub use up::UPSafeCell;
|
||||||
|
@ -18,10 +18,12 @@ impl<T> UPSafeCell<T> {
|
|||||||
/// User is responsible to guarantee that inner struct is only used in
|
/// User is responsible to guarantee that inner struct is only used in
|
||||||
/// uniprocessor.
|
/// uniprocessor.
|
||||||
pub unsafe fn new(value: T) -> Self {
|
pub unsafe fn new(value: T) -> Self {
|
||||||
Self { inner: RefCell::new(value) }
|
Self {
|
||||||
|
inner: RefCell::new(value),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/// Panic if the data has been borrowed.
|
/// Panic if the data has been borrowed.
|
||||||
pub fn exclusive_access(&self) -> RefMut<'_, T> {
|
pub fn exclusive_access(&self) -> RefMut<'_, T> {
|
||||||
self.inner.borrow_mut()
|
self.inner.borrow_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::mm::translated_byte_buffer;
|
use crate::mm::translated_byte_buffer;
|
||||||
use crate::task::{current_user_token, suspend_current_and_run_next};
|
|
||||||
use crate::sbi::console_getchar;
|
use crate::sbi::console_getchar;
|
||||||
|
use crate::task::{current_user_token, suspend_current_and_run_next};
|
||||||
|
|
||||||
const FD_STDIN: usize = 0;
|
const FD_STDIN: usize = 0;
|
||||||
const FD_STDOUT: usize = 1;
|
const FD_STDOUT: usize = 1;
|
||||||
@ -13,7 +13,7 @@ pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
|||||||
print!("{}", core::str::from_utf8(buffer).unwrap());
|
print!("{}", core::str::from_utf8(buffer).unwrap());
|
||||||
}
|
}
|
||||||
len as isize
|
len as isize
|
||||||
},
|
}
|
||||||
_ => {
|
_ => {
|
||||||
panic!("Unsupported fd in sys_write!");
|
panic!("Unsupported fd in sys_write!");
|
||||||
}
|
}
|
||||||
@ -36,11 +36,13 @@ pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
|
|||||||
}
|
}
|
||||||
let ch = c as u8;
|
let ch = c as u8;
|
||||||
let mut buffers = translated_byte_buffer(current_user_token(), buf, len);
|
let mut buffers = translated_byte_buffer(current_user_token(), buf, len);
|
||||||
unsafe { buffers[0].as_mut_ptr().write_volatile(ch); }
|
unsafe {
|
||||||
|
buffers[0].as_mut_ptr().write_volatile(ch);
|
||||||
|
}
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
panic!("Unsupported fd in sys_read!");
|
panic!("Unsupported fd in sys_read!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,4 +28,3 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
|
|||||||
_ => panic!("Unsupported syscall_id: {}", syscall_id),
|
_ => panic!("Unsupported syscall_id: {}", syscall_id),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +1,10 @@
|
|||||||
|
use crate::loader::get_app_data_by_name;
|
||||||
|
use crate::mm::{translated_refmut, translated_str};
|
||||||
use crate::task::{
|
use crate::task::{
|
||||||
|
add_task, current_task, current_user_token, exit_current_and_run_next,
|
||||||
suspend_current_and_run_next,
|
suspend_current_and_run_next,
|
||||||
exit_current_and_run_next,
|
|
||||||
current_task,
|
|
||||||
current_user_token,
|
|
||||||
add_task,
|
|
||||||
};
|
};
|
||||||
use crate::timer::get_time_ms;
|
use crate::timer::get_time_ms;
|
||||||
use crate::mm::{
|
|
||||||
translated_str,
|
|
||||||
translated_refmut,
|
|
||||||
};
|
|
||||||
use crate::loader::get_app_data_by_name;
|
|
||||||
use alloc::sync::Arc;
|
use alloc::sync::Arc;
|
||||||
|
|
||||||
pub fn sys_exit(exit_code: i32) -> ! {
|
pub fn sys_exit(exit_code: i32) -> ! {
|
||||||
@ -65,21 +59,20 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
|
|||||||
|
|
||||||
// ---- access current TCB exclusively
|
// ---- access current TCB exclusively
|
||||||
let mut inner = task.inner_exclusive_access();
|
let mut inner = task.inner_exclusive_access();
|
||||||
if inner.children
|
if inner
|
||||||
|
.children
|
||||||
.iter()
|
.iter()
|
||||||
.find(|p| {pid == -1 || pid as usize == p.getpid()})
|
.find(|p| pid == -1 || pid as usize == p.getpid())
|
||||||
.is_none() {
|
.is_none()
|
||||||
|
{
|
||||||
return -1;
|
return -1;
|
||||||
// ---- release current PCB
|
// ---- release current PCB
|
||||||
}
|
}
|
||||||
let pair = inner.children
|
let pair = inner.children.iter().enumerate().find(|(_, p)| {
|
||||||
.iter()
|
// ++++ temporarily access child PCB lock exclusively
|
||||||
.enumerate()
|
p.inner_exclusive_access().is_zombie() && (pid == -1 || pid as usize == p.getpid())
|
||||||
.find(|(_, p)| {
|
// ++++ release child PCB
|
||||||
// ++++ 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 {
|
if let Some((idx, _)) = pair {
|
||||||
let child = inner.children.remove(idx);
|
let child = inner.children.remove(idx);
|
||||||
// confirm that child will be deallocated after removing from children list
|
// confirm that child will be deallocated after removing from children list
|
||||||
@ -94,4 +87,4 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
|
|||||||
-2
|
-2
|
||||||
}
|
}
|
||||||
// ---- release current PCB lock automatically
|
// ---- release current PCB lock automatically
|
||||||
}
|
}
|
||||||
|
@ -23,4 +23,3 @@ impl TaskContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::sync::UPSafeCell;
|
|
||||||
use super::TaskControlBlock;
|
use super::TaskControlBlock;
|
||||||
|
use crate::sync::UPSafeCell;
|
||||||
use alloc::collections::VecDeque;
|
use alloc::collections::VecDeque;
|
||||||
use alloc::sync::Arc;
|
use alloc::sync::Arc;
|
||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
@ -11,7 +11,9 @@ pub struct TaskManager {
|
|||||||
/// A simple FIFO scheduler.
|
/// A simple FIFO scheduler.
|
||||||
impl TaskManager {
|
impl TaskManager {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { ready_queue: VecDeque::new(), }
|
Self {
|
||||||
|
ready_queue: VecDeque::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn add(&mut self, task: Arc<TaskControlBlock>) {
|
pub fn add(&mut self, task: Arc<TaskControlBlock>) {
|
||||||
self.ready_queue.push_back(task);
|
self.ready_queue.push_back(task);
|
||||||
@ -22,9 +24,8 @@ impl TaskManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref TASK_MANAGER: UPSafeCell<TaskManager> = unsafe {
|
pub static ref TASK_MANAGER: UPSafeCell<TaskManager> =
|
||||||
UPSafeCell::new(TaskManager::new())
|
unsafe { UPSafeCell::new(TaskManager::new()) };
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_task(task: Arc<TaskControlBlock>) {
|
pub fn add_task(task: Arc<TaskControlBlock>) {
|
||||||
@ -33,4 +34,4 @@ pub fn add_task(task: Arc<TaskControlBlock>) {
|
|||||||
|
|
||||||
pub fn fetch_task() -> Option<Arc<TaskControlBlock>> {
|
pub fn fetch_task() -> Option<Arc<TaskControlBlock>> {
|
||||||
TASK_MANAGER.exclusive_access().fetch()
|
TASK_MANAGER.exclusive_access().fetch()
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,23 @@
|
|||||||
mod context;
|
mod context;
|
||||||
|
mod manager;
|
||||||
|
mod pid;
|
||||||
|
mod processor;
|
||||||
mod switch;
|
mod switch;
|
||||||
mod task;
|
mod task;
|
||||||
mod manager;
|
|
||||||
mod processor;
|
|
||||||
mod pid;
|
|
||||||
|
|
||||||
use crate::loader::get_app_data_by_name;
|
use crate::loader::get_app_data_by_name;
|
||||||
|
use alloc::sync::Arc;
|
||||||
|
use lazy_static::*;
|
||||||
|
use manager::fetch_task;
|
||||||
use switch::__switch;
|
use switch::__switch;
|
||||||
use task::{TaskControlBlock, TaskStatus};
|
use task::{TaskControlBlock, TaskStatus};
|
||||||
use alloc::sync::Arc;
|
|
||||||
use manager::fetch_task;
|
|
||||||
use lazy_static::*;
|
|
||||||
|
|
||||||
pub use context::TaskContext;
|
pub use context::TaskContext;
|
||||||
pub use processor::{
|
|
||||||
run_tasks,
|
|
||||||
current_task,
|
|
||||||
current_user_token,
|
|
||||||
current_trap_cx,
|
|
||||||
take_current_task,
|
|
||||||
schedule,
|
|
||||||
};
|
|
||||||
pub use manager::add_task;
|
pub use manager::add_task;
|
||||||
pub use pid::{PidHandle, pid_alloc, KernelStack};
|
pub use pid::{pid_alloc, KernelStack, PidHandle};
|
||||||
|
pub use processor::{
|
||||||
|
current_task, current_trap_cx, current_user_token, run_tasks, schedule, take_current_task,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn suspend_current_and_run_next() {
|
pub fn suspend_current_and_run_next() {
|
||||||
// There must be an application running.
|
// There must be an application running.
|
||||||
@ -76,9 +71,9 @@ pub fn exit_current_and_run_next(exit_code: i32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref INITPROC: Arc<TaskControlBlock> = Arc::new(
|
pub static ref INITPROC: Arc<TaskControlBlock> = Arc::new(TaskControlBlock::new(
|
||||||
TaskControlBlock::new(get_app_data_by_name("initproc").unwrap())
|
get_app_data_by_name("initproc").unwrap()
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_initproc() {
|
pub fn add_initproc() {
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
|
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 alloc::vec::Vec;
|
||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
use crate::sync::UPSafeCell;
|
|
||||||
use crate::mm::{KERNEL_SPACE, MapPermission, VirtAddr};
|
|
||||||
use crate::config::{
|
|
||||||
PAGE_SIZE,
|
|
||||||
TRAMPOLINE,
|
|
||||||
KERNEL_STACK_SIZE,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PidAllocator {
|
struct PidAllocator {
|
||||||
current: usize,
|
current: usize,
|
||||||
@ -32,16 +28,16 @@ impl PidAllocator {
|
|||||||
assert!(pid < self.current);
|
assert!(pid < self.current);
|
||||||
assert!(
|
assert!(
|
||||||
self.recycled.iter().find(|ppid| **ppid == pid).is_none(),
|
self.recycled.iter().find(|ppid| **ppid == pid).is_none(),
|
||||||
"pid {} has been deallocated!", pid
|
"pid {} has been deallocated!",
|
||||||
|
pid
|
||||||
);
|
);
|
||||||
self.recycled.push(pid);
|
self.recycled.push(pid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref PID_ALLOCATOR : UPSafeCell<PidAllocator> = unsafe {
|
static ref PID_ALLOCATOR: UPSafeCell<PidAllocator> =
|
||||||
UPSafeCell::new(PidAllocator::new())
|
unsafe { UPSafeCell::new(PidAllocator::new()) };
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PidHandle(pub usize);
|
pub struct PidHandle(pub usize);
|
||||||
@ -72,23 +68,23 @@ impl KernelStack {
|
|||||||
pub fn new(pid_handle: &PidHandle) -> Self {
|
pub fn new(pid_handle: &PidHandle) -> Self {
|
||||||
let pid = pid_handle.0;
|
let pid = pid_handle.0;
|
||||||
let (kernel_stack_bottom, kernel_stack_top) = kernel_stack_position(pid);
|
let (kernel_stack_bottom, kernel_stack_top) = kernel_stack_position(pid);
|
||||||
KERNEL_SPACE
|
KERNEL_SPACE.exclusive_access().insert_framed_area(
|
||||||
.exclusive_access()
|
kernel_stack_bottom.into(),
|
||||||
.insert_framed_area(
|
kernel_stack_top.into(),
|
||||||
kernel_stack_bottom.into(),
|
MapPermission::R | MapPermission::W,
|
||||||
kernel_stack_top.into(),
|
);
|
||||||
MapPermission::R | MapPermission::W,
|
KernelStack { pid: pid_handle.0 }
|
||||||
);
|
|
||||||
KernelStack {
|
|
||||||
pid: pid_handle.0,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub fn push_on_top<T>(&self, value: T) -> *mut T where
|
pub fn push_on_top<T>(&self, value: T) -> *mut T
|
||||||
T: Sized, {
|
where
|
||||||
|
T: Sized,
|
||||||
|
{
|
||||||
let kernel_stack_top = self.get_top();
|
let kernel_stack_top = self.get_top();
|
||||||
let ptr_mut = (kernel_stack_top - core::mem::size_of::<T>()) as *mut T;
|
let ptr_mut = (kernel_stack_top - core::mem::size_of::<T>()) as *mut T;
|
||||||
unsafe { *ptr_mut = value; }
|
unsafe {
|
||||||
|
*ptr_mut = value;
|
||||||
|
}
|
||||||
ptr_mut
|
ptr_mut
|
||||||
}
|
}
|
||||||
pub fn get_top(&self) -> usize {
|
pub fn get_top(&self) -> usize {
|
||||||
@ -105,4 +101,4 @@ impl Drop for KernelStack {
|
|||||||
.exclusive_access()
|
.exclusive_access()
|
||||||
.remove_area_with_start_vpn(kernel_stack_bottom_va.into());
|
.remove_area_with_start_vpn(kernel_stack_bottom_va.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
|
use super::__switch;
|
||||||
|
use super::{fetch_task, TaskStatus};
|
||||||
use super::{TaskContext, TaskControlBlock};
|
use super::{TaskContext, TaskControlBlock};
|
||||||
|
use crate::sync::UPSafeCell;
|
||||||
|
use crate::trap::TrapContext;
|
||||||
use alloc::sync::Arc;
|
use alloc::sync::Arc;
|
||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
use super::{fetch_task, TaskStatus};
|
|
||||||
use super::__switch;
|
|
||||||
use crate::trap::TrapContext;
|
|
||||||
use crate::sync::UPSafeCell;
|
|
||||||
|
|
||||||
pub struct Processor {
|
pub struct Processor {
|
||||||
current: Option<Arc<TaskControlBlock>>,
|
current: Option<Arc<TaskControlBlock>>,
|
||||||
@ -30,9 +30,7 @@ impl Processor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref PROCESSOR: UPSafeCell<Processor> = unsafe {
|
pub static ref PROCESSOR: UPSafeCell<Processor> = unsafe { UPSafeCell::new(Processor::new()) };
|
||||||
UPSafeCell::new(Processor::new())
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_tasks() {
|
pub fn run_tasks() {
|
||||||
@ -50,10 +48,7 @@ pub fn run_tasks() {
|
|||||||
// release processor manually
|
// release processor manually
|
||||||
drop(processor);
|
drop(processor);
|
||||||
unsafe {
|
unsafe {
|
||||||
__switch(
|
__switch(idle_task_cx_ptr, next_task_cx_ptr);
|
||||||
idle_task_cx_ptr,
|
|
||||||
next_task_cx_ptr,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,7 +69,10 @@ pub fn current_user_token() -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_trap_cx() -> &'static mut TrapContext {
|
pub fn current_trap_cx() -> &'static mut TrapContext {
|
||||||
current_task().unwrap().inner_exclusive_access().get_trap_cx()
|
current_task()
|
||||||
|
.unwrap()
|
||||||
|
.inner_exclusive_access()
|
||||||
|
.get_trap_cx()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn schedule(switched_task_cx_ptr: *mut TaskContext) {
|
pub fn schedule(switched_task_cx_ptr: *mut TaskContext) {
|
||||||
@ -82,9 +80,6 @@ pub fn schedule(switched_task_cx_ptr: *mut TaskContext) {
|
|||||||
let idle_task_cx_ptr = processor.get_idle_task_cx_ptr();
|
let idle_task_cx_ptr = processor.get_idle_task_cx_ptr();
|
||||||
drop(processor);
|
drop(processor);
|
||||||
unsafe {
|
unsafe {
|
||||||
__switch(
|
__switch(switched_task_cx_ptr, idle_task_cx_ptr);
|
||||||
switched_task_cx_ptr,
|
|
||||||
idle_task_cx_ptr,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,5 @@ use core::arch::global_asm;
|
|||||||
global_asm!(include_str!("switch.S"));
|
global_asm!(include_str!("switch.S"));
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
pub fn __switch(
|
pub fn __switch(current_task_cx_ptr: *mut TaskContext, next_task_cx_ptr: *const TaskContext);
|
||||||
current_task_cx_ptr: *mut TaskContext,
|
|
||||||
next_task_cx_ptr: *const TaskContext
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
use crate::mm::{MemorySet, PhysPageNum, KERNEL_SPACE, VirtAddr};
|
|
||||||
use crate::trap::{TrapContext, trap_handler};
|
|
||||||
use crate::config::TRAP_CONTEXT;
|
|
||||||
use crate::sync::UPSafeCell;
|
|
||||||
use core::cell::RefMut;
|
|
||||||
use super::TaskContext;
|
use super::TaskContext;
|
||||||
use super::{PidHandle, pid_alloc, KernelStack};
|
use super::{pid_alloc, KernelStack, PidHandle};
|
||||||
use alloc::sync::{Weak, Arc};
|
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 alloc::vec::Vec;
|
||||||
|
use core::cell::RefMut;
|
||||||
|
|
||||||
pub struct TaskControlBlock {
|
pub struct TaskControlBlock {
|
||||||
// immutable
|
// immutable
|
||||||
@ -66,16 +66,18 @@ impl TaskControlBlock {
|
|||||||
let task_control_block = Self {
|
let task_control_block = Self {
|
||||||
pid: pid_handle,
|
pid: pid_handle,
|
||||||
kernel_stack,
|
kernel_stack,
|
||||||
inner: unsafe { UPSafeCell::new(TaskControlBlockInner {
|
inner: unsafe {
|
||||||
trap_cx_ppn,
|
UPSafeCell::new(TaskControlBlockInner {
|
||||||
base_size: user_sp,
|
trap_cx_ppn,
|
||||||
task_cx: TaskContext::goto_trap_return(kernel_stack_top),
|
base_size: user_sp,
|
||||||
task_status: TaskStatus::Ready,
|
task_cx: TaskContext::goto_trap_return(kernel_stack_top),
|
||||||
memory_set,
|
task_status: TaskStatus::Ready,
|
||||||
parent: None,
|
memory_set,
|
||||||
children: Vec::new(),
|
parent: None,
|
||||||
exit_code: 0,
|
children: Vec::new(),
|
||||||
})},
|
exit_code: 0,
|
||||||
|
})
|
||||||
|
},
|
||||||
};
|
};
|
||||||
// prepare TrapContext in user space
|
// prepare TrapContext in user space
|
||||||
let trap_cx = task_control_block.inner_exclusive_access().get_trap_cx();
|
let trap_cx = task_control_block.inner_exclusive_access().get_trap_cx();
|
||||||
@ -117,9 +119,7 @@ impl TaskControlBlock {
|
|||||||
// ---- access parent PCB exclusively
|
// ---- access parent PCB exclusively
|
||||||
let mut parent_inner = self.inner_exclusive_access();
|
let mut parent_inner = self.inner_exclusive_access();
|
||||||
// copy user space(include trap context)
|
// copy user space(include trap context)
|
||||||
let memory_set = MemorySet::from_existed_user(
|
let memory_set = MemorySet::from_existed_user(&parent_inner.memory_set);
|
||||||
&parent_inner.memory_set
|
|
||||||
);
|
|
||||||
let trap_cx_ppn = memory_set
|
let trap_cx_ppn = memory_set
|
||||||
.translate(VirtAddr::from(TRAP_CONTEXT).into())
|
.translate(VirtAddr::from(TRAP_CONTEXT).into())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -131,16 +131,18 @@ impl TaskControlBlock {
|
|||||||
let task_control_block = Arc::new(TaskControlBlock {
|
let task_control_block = Arc::new(TaskControlBlock {
|
||||||
pid: pid_handle,
|
pid: pid_handle,
|
||||||
kernel_stack,
|
kernel_stack,
|
||||||
inner: unsafe { UPSafeCell::new(TaskControlBlockInner {
|
inner: unsafe {
|
||||||
trap_cx_ppn,
|
UPSafeCell::new(TaskControlBlockInner {
|
||||||
base_size: parent_inner.base_size,
|
trap_cx_ppn,
|
||||||
task_cx: TaskContext::goto_trap_return(kernel_stack_top),
|
base_size: parent_inner.base_size,
|
||||||
task_status: TaskStatus::Ready,
|
task_cx: TaskContext::goto_trap_return(kernel_stack_top),
|
||||||
memory_set,
|
task_status: TaskStatus::Ready,
|
||||||
parent: Some(Arc::downgrade(self)),
|
memory_set,
|
||||||
children: Vec::new(),
|
parent: Some(Arc::downgrade(self)),
|
||||||
exit_code: 0,
|
children: Vec::new(),
|
||||||
})},
|
exit_code: 0,
|
||||||
|
})
|
||||||
|
},
|
||||||
});
|
});
|
||||||
// add child
|
// add child
|
||||||
parent_inner.children.push(task_control_block.clone());
|
parent_inner.children.push(task_control_block.clone());
|
||||||
@ -163,4 +165,4 @@ pub enum TaskStatus {
|
|||||||
Ready,
|
Ready,
|
||||||
Running,
|
Running,
|
||||||
Zombie,
|
Zombie,
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use riscv::register::time;
|
|
||||||
use crate::sbi::set_timer;
|
|
||||||
use crate::config::CLOCK_FREQ;
|
use crate::config::CLOCK_FREQ;
|
||||||
|
use crate::sbi::set_timer;
|
||||||
|
use riscv::register::time;
|
||||||
|
|
||||||
const TICKS_PER_SEC: usize = 100;
|
const TICKS_PER_SEC: usize = 100;
|
||||||
const MSEC_PER_SEC: usize = 1000;
|
const MSEC_PER_SEC: usize = 1000;
|
||||||
@ -15,4 +15,4 @@ pub fn get_time_ms() -> usize {
|
|||||||
|
|
||||||
pub fn set_next_trigger() {
|
pub fn set_next_trigger() {
|
||||||
set_timer(get_time() + CLOCK_FREQ / TICKS_PER_SEC);
|
set_timer(get_time() + CLOCK_FREQ / TICKS_PER_SEC);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use riscv::register::sstatus::{Sstatus, self, SPP};
|
use riscv::register::sstatus::{self, Sstatus, SPP};
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct TrapContext {
|
pub struct TrapContext {
|
||||||
@ -11,7 +11,9 @@ pub struct TrapContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TrapContext {
|
impl TrapContext {
|
||||||
pub fn set_sp(&mut self, sp: usize) { self.x[2] = sp; }
|
pub fn set_sp(&mut self, sp: usize) {
|
||||||
|
self.x[2] = sp;
|
||||||
|
}
|
||||||
pub fn app_init_context(
|
pub fn app_init_context(
|
||||||
entry: usize,
|
entry: usize,
|
||||||
sp: usize,
|
sp: usize,
|
||||||
|
@ -1,27 +1,17 @@
|
|||||||
mod context;
|
mod context;
|
||||||
|
|
||||||
use riscv::register::{
|
use crate::config::{TRAMPOLINE, TRAP_CONTEXT};
|
||||||
mtvec::TrapMode,
|
|
||||||
stvec,
|
|
||||||
scause::{
|
|
||||||
self,
|
|
||||||
Trap,
|
|
||||||
Exception,
|
|
||||||
Interrupt,
|
|
||||||
},
|
|
||||||
stval,
|
|
||||||
sie,
|
|
||||||
};
|
|
||||||
use crate::syscall::syscall;
|
use crate::syscall::syscall;
|
||||||
use crate::task::{
|
use crate::task::{
|
||||||
exit_current_and_run_next,
|
current_trap_cx, current_user_token, exit_current_and_run_next, suspend_current_and_run_next,
|
||||||
suspend_current_and_run_next,
|
|
||||||
current_user_token,
|
|
||||||
current_trap_cx,
|
|
||||||
};
|
};
|
||||||
use crate::timer::set_next_trigger;
|
use crate::timer::set_next_trigger;
|
||||||
use crate::config::{TRAP_CONTEXT, TRAMPOLINE};
|
use core::arch::{asm, global_asm};
|
||||||
use core::arch::{global_asm, asm};
|
use riscv::register::{
|
||||||
|
mtvec::TrapMode,
|
||||||
|
scause::{self, Exception, Interrupt, Trap},
|
||||||
|
sie, stval, stvec,
|
||||||
|
};
|
||||||
|
|
||||||
global_asm!(include_str!("trap.S"));
|
global_asm!(include_str!("trap.S"));
|
||||||
|
|
||||||
@ -42,7 +32,9 @@ fn set_user_trap_entry() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn enable_timer_interrupt() {
|
pub fn enable_timer_interrupt() {
|
||||||
unsafe { sie::set_stimer(); }
|
unsafe {
|
||||||
|
sie::set_stimer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -61,12 +53,12 @@ pub fn trap_handler() -> ! {
|
|||||||
cx = current_trap_cx();
|
cx = current_trap_cx();
|
||||||
cx.x[10] = result as usize;
|
cx.x[10] = result as usize;
|
||||||
}
|
}
|
||||||
Trap::Exception(Exception::StoreFault) |
|
Trap::Exception(Exception::StoreFault)
|
||||||
Trap::Exception(Exception::StorePageFault) |
|
| Trap::Exception(Exception::StorePageFault)
|
||||||
Trap::Exception(Exception::InstructionFault) |
|
| Trap::Exception(Exception::InstructionFault)
|
||||||
Trap::Exception(Exception::InstructionPageFault) |
|
| Trap::Exception(Exception::InstructionPageFault)
|
||||||
Trap::Exception(Exception::LoadFault) |
|
| Trap::Exception(Exception::LoadFault)
|
||||||
Trap::Exception(Exception::LoadPageFault) => {
|
| Trap::Exception(Exception::LoadPageFault) => {
|
||||||
println!(
|
println!(
|
||||||
"[kernel] {:?} in application, bad addr = {:#x}, bad instruction = {:#x}, kernel killed it.",
|
"[kernel] {:?} in application, bad addr = {:#x}, bad instruction = {:#x}, kernel killed it.",
|
||||||
scause.cause(),
|
scause.cause(),
|
||||||
@ -86,7 +78,11 @@ pub fn trap_handler() -> ! {
|
|||||||
suspend_current_and_run_next();
|
suspend_current_and_run_next();
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
panic!("Unsupported trap {:?}, stval = {:#x}!", scause.cause(), stval);
|
panic!(
|
||||||
|
"Unsupported trap {:?}, stval = {:#x}!",
|
||||||
|
scause.cause(),
|
||||||
|
stval
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
trap_return();
|
trap_return();
|
||||||
@ -119,4 +115,4 @@ pub fn trap_from_kernel() -> ! {
|
|||||||
panic!("a trap {:?} from kernel!", scause::read().cause());
|
panic!("a trap {:?} from kernel!", scause::read().cause());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub use context::{TrapContext};
|
pub use context::TrapContext;
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
use user_lib::{fork, yield_, waitpid, exit, wait};
|
use user_lib::{exit, fork, wait, waitpid, yield_};
|
||||||
|
|
||||||
const MAGIC: i32 = -0x10384;
|
const MAGIC: i32 = -0x10384;
|
||||||
|
|
||||||
@ -13,7 +13,9 @@ pub fn main() -> i32 {
|
|||||||
let pid = fork();
|
let pid = fork();
|
||||||
if pid == 0 {
|
if pid == 0 {
|
||||||
println!("I am the child.");
|
println!("I am the child.");
|
||||||
for _ in 0..7 { yield_(); }
|
for _ in 0..7 {
|
||||||
|
yield_();
|
||||||
|
}
|
||||||
exit(MAGIC);
|
exit(MAGIC);
|
||||||
} else {
|
} else {
|
||||||
println!("I am parent, fork a child pid {}", pid);
|
println!("I am parent, fork a child pid {}", pid);
|
||||||
@ -26,4 +28,3 @@ pub fn main() -> i32 {
|
|||||||
println!("exit pass.");
|
println!("exit pass.");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,4 +41,4 @@ pub fn main() -> i32 {
|
|||||||
println!("{}", color_text!(text, i));
|
println!("{}", color_text!(text, i));
|
||||||
}
|
}
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
|
|
||||||
use user_lib::{fork, wait, exit};
|
use user_lib::{exit, fork, wait};
|
||||||
|
|
||||||
const MAX_CHILD: usize = 30;
|
const MAX_CHILD: usize = 30;
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
|
|
||||||
use user_lib::{fork, wait, getpid, exit, sleep, get_time};
|
use user_lib::{exit, fork, get_time, getpid, sleep, wait};
|
||||||
|
|
||||||
static NUM: usize = 30;
|
static NUM: usize = 30;
|
||||||
|
|
||||||
@ -14,7 +14,8 @@ pub fn main() -> i32 {
|
|||||||
let pid = fork();
|
let pid = fork();
|
||||||
if pid == 0 {
|
if pid == 0 {
|
||||||
let current_time = get_time();
|
let current_time = get_time();
|
||||||
let sleep_length = (current_time as i32 as isize) * (current_time as i32 as isize) % 1000 + 1000;
|
let sleep_length =
|
||||||
|
(current_time as i32 as isize) * (current_time as i32 as isize) % 1000 + 1000;
|
||||||
println!("pid {} sleep for {} ms", getpid(), sleep_length);
|
println!("pid {} sleep for {} ms", getpid(), sleep_length);
|
||||||
sleep(sleep_length as usize);
|
sleep(sleep_length as usize);
|
||||||
println!("pid {} OK!", getpid());
|
println!("pid {} OK!", getpid());
|
||||||
@ -30,4 +31,4 @@ pub fn main() -> i32 {
|
|||||||
assert!(wait(&mut exit_code) < 0);
|
assert!(wait(&mut exit_code) < 0);
|
||||||
println!("forktest2 test passed!");
|
println!("forktest2 test passed!");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -25,4 +25,4 @@ pub fn main() -> i32 {
|
|||||||
println!("child process pid = {}, exit code = {}", pid, exit_code);
|
println!("child process pid = {}, exit code = {}", pid, exit_code);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
|
|
||||||
use user_lib::{sleep, getpid, fork, exit, yield_};
|
use user_lib::{exit, fork, getpid, sleep, yield_};
|
||||||
|
|
||||||
const DEPTH: usize = 4;
|
const DEPTH: usize = 4;
|
||||||
|
|
||||||
|
@ -8,4 +8,4 @@ extern crate user_lib;
|
|||||||
pub fn main() -> i32 {
|
pub fn main() -> i32 {
|
||||||
println!("Hello world from user mode program!");
|
println!("Hello world from user mode program!");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,7 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
|
|
||||||
use user_lib::{
|
use user_lib::{exec, fork, wait, yield_};
|
||||||
fork,
|
|
||||||
wait,
|
|
||||||
exec,
|
|
||||||
yield_,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
fn main() -> i32 {
|
fn main() -> i32 {
|
||||||
@ -25,10 +20,9 @@ fn main() -> i32 {
|
|||||||
}
|
}
|
||||||
println!(
|
println!(
|
||||||
"[initproc] Released a zombie process, pid={}, exit_code={}",
|
"[initproc] Released a zombie process, pid={}, exit_code={}",
|
||||||
pid,
|
pid, exit_code,
|
||||||
exit_code,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
#![allow(clippy::needless_range_loop)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
|
|
||||||
use user_lib::{fork, wait, yield_, exit, getpid, get_time};
|
use user_lib::{exit, fork, get_time, getpid, wait, yield_};
|
||||||
|
|
||||||
static NUM: usize = 30;
|
static NUM: usize = 30;
|
||||||
const N: usize = 10;
|
const N: usize = 10;
|
||||||
@ -27,6 +28,7 @@ fn work(times: isize) {
|
|||||||
for i in 0..N {
|
for i in 0..N {
|
||||||
for j in 0..N {
|
for j in 0..N {
|
||||||
c[i][j] = 0;
|
c[i][j] = 0;
|
||||||
|
#[allow(clippy::needless_range_loop)]
|
||||||
for k in 0..N {
|
for k in 0..N {
|
||||||
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % P;
|
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % P;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
|
|
||||||
use user_lib::{sleep, exit, get_time, fork, waitpid};
|
use user_lib::{exit, fork, get_time, sleep, waitpid};
|
||||||
|
|
||||||
fn sleepy() {
|
fn sleepy() {
|
||||||
let time: usize = 100;
|
let time: usize = 100;
|
||||||
@ -27,4 +27,4 @@ pub fn main() -> i32 {
|
|||||||
println!("use {} msecs.", get_time() - current_time);
|
println!("use {} msecs.", get_time() - current_time);
|
||||||
println!("sleep pass.");
|
println!("sleep pass.");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,11 @@ pub fn main() -> i32 {
|
|||||||
println!("current time_msec = {}", start);
|
println!("current time_msec = {}", start);
|
||||||
sleep(100);
|
sleep(100);
|
||||||
let end = get_time();
|
let end = get_time();
|
||||||
println!("time_msec = {} after sleeping 100 ticks, delta = {}ms!", end, end - start);
|
println!(
|
||||||
|
"time_msec = {} after sleeping 100 ticks, delta = {}ms!",
|
||||||
|
end,
|
||||||
|
end - start
|
||||||
|
);
|
||||||
println!("r_sleep passed!");
|
println!("r_sleep passed!");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
extern crate user_lib;
|
extern crate user_lib;
|
||||||
|
|
||||||
fn f(d: usize) {
|
fn f(d: usize) {
|
||||||
println!("d = {}",d);
|
println!("d = {}", d);
|
||||||
f(d + 1);
|
f(d + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,4 +14,4 @@ pub fn main() -> i32 {
|
|||||||
println!("It should trigger segmentation fault!");
|
println!("It should trigger segmentation fault!");
|
||||||
f(0);
|
f(0);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
#![allow(clippy::println_empty_string)]
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
@ -12,8 +13,8 @@ const DL: u8 = 0x7fu8;
|
|||||||
const BS: u8 = 0x08u8;
|
const BS: u8 = 0x08u8;
|
||||||
|
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
use user_lib::{fork, exec, waitpid};
|
|
||||||
use user_lib::console::getchar;
|
use user_lib::console::getchar;
|
||||||
|
use user_lib::{exec, fork, waitpid};
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn main() -> i32 {
|
pub fn main() -> i32 {
|
||||||
@ -59,4 +60,4 @@ pub fn main() -> i32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,9 +32,12 @@ pub fn main() -> i32 {
|
|||||||
let mut exit_code: i32 = Default::default();
|
let mut exit_code: i32 = Default::default();
|
||||||
let wait_pid = waitpid(pid as usize, &mut exit_code);
|
let wait_pid = waitpid(pid as usize, &mut exit_code);
|
||||||
assert_eq!(pid, wait_pid);
|
assert_eq!(pid, wait_pid);
|
||||||
println!("\x1b[32mUsertests: Test {} in Process {} exited with code {}\x1b[0m", test, pid, exit_code);
|
println!(
|
||||||
|
"\x1b[32mUsertests: Test {} in Process {} exited with code {}\x1b[0m",
|
||||||
|
test, pid, exit_code
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("Usertests passed!");
|
println!("Usertests passed!");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -14,4 +14,4 @@ pub fn main() -> i32 {
|
|||||||
}
|
}
|
||||||
println!("yield pass.");
|
println!("yield pass.");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,14 @@
|
|||||||
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
|
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
|
||||||
let err = panic_info.message().unwrap();
|
let err = panic_info.message().unwrap();
|
||||||
if let Some(location) = panic_info.location() {
|
if let Some(location) = panic_info.location() {
|
||||||
println!("Panicked at {}:{}, {}", location.file(), location.line(), err);
|
println!(
|
||||||
|
"Panicked at {}:{}, {}",
|
||||||
|
location.file(),
|
||||||
|
location.line(),
|
||||||
|
err
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
println!("Panicked: {}", err);
|
println!("Panicked: {}", err);
|
||||||
}
|
}
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod console;
|
pub mod console;
|
||||||
mod syscall;
|
|
||||||
mod lang_items;
|
mod lang_items;
|
||||||
|
mod syscall;
|
||||||
|
|
||||||
use syscall::*;
|
|
||||||
use buddy_system_allocator::LockedHeap;
|
use buddy_system_allocator::LockedHeap;
|
||||||
|
use syscall::*;
|
||||||
|
|
||||||
const USER_HEAP_SIZE: usize = 16384;
|
const USER_HEAP_SIZE: usize = 16384;
|
||||||
|
|
||||||
@ -39,18 +39,36 @@ fn main() -> i32 {
|
|||||||
panic!("Cannot find main!");
|
panic!("Cannot find main!");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(fd: usize, buf: &mut [u8]) -> isize { sys_read(fd, buf) }
|
pub fn read(fd: usize, buf: &mut [u8]) -> isize {
|
||||||
pub fn write(fd: usize, buf: &[u8]) -> isize { sys_write(fd, buf) }
|
sys_read(fd, buf)
|
||||||
pub fn exit(exit_code: i32) -> ! { sys_exit(exit_code); }
|
}
|
||||||
pub fn yield_() -> isize { sys_yield() }
|
pub fn write(fd: usize, buf: &[u8]) -> isize {
|
||||||
pub fn get_time() -> isize { sys_get_time() }
|
sys_write(fd, buf)
|
||||||
pub fn getpid() -> isize { sys_getpid() }
|
}
|
||||||
pub fn fork() -> isize { sys_fork() }
|
pub fn exit(exit_code: i32) -> ! {
|
||||||
pub fn exec(path: &str) -> isize { sys_exec(path) }
|
sys_exit(exit_code);
|
||||||
|
}
|
||||||
|
pub fn yield_() -> isize {
|
||||||
|
sys_yield()
|
||||||
|
}
|
||||||
|
pub fn get_time() -> isize {
|
||||||
|
sys_get_time()
|
||||||
|
}
|
||||||
|
pub fn getpid() -> isize {
|
||||||
|
sys_getpid()
|
||||||
|
}
|
||||||
|
pub fn fork() -> isize {
|
||||||
|
sys_fork()
|
||||||
|
}
|
||||||
|
pub fn exec(path: &str) -> isize {
|
||||||
|
sys_exec(path)
|
||||||
|
}
|
||||||
pub fn wait(exit_code: &mut i32) -> isize {
|
pub fn wait(exit_code: &mut i32) -> isize {
|
||||||
loop {
|
loop {
|
||||||
match sys_waitpid(-1, exit_code as *mut _) {
|
match sys_waitpid(-1, exit_code as *mut _) {
|
||||||
-2 => { yield_(); }
|
-2 => {
|
||||||
|
yield_();
|
||||||
|
}
|
||||||
// -1 or a real pid
|
// -1 or a real pid
|
||||||
exit_pid => return exit_pid,
|
exit_pid => return exit_pid,
|
||||||
}
|
}
|
||||||
@ -60,7 +78,9 @@ pub fn wait(exit_code: &mut i32) -> isize {
|
|||||||
pub fn waitpid(pid: usize, exit_code: &mut i32) -> isize {
|
pub fn waitpid(pid: usize, exit_code: &mut i32) -> isize {
|
||||||
loop {
|
loop {
|
||||||
match sys_waitpid(pid as isize, exit_code as *mut _) {
|
match sys_waitpid(pid as isize, exit_code as *mut _) {
|
||||||
-2 => { yield_(); }
|
-2 => {
|
||||||
|
yield_();
|
||||||
|
}
|
||||||
// -1 or a real pid
|
// -1 or a real pid
|
||||||
exit_pid => return exit_pid,
|
exit_pid => return exit_pid,
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,10 @@ fn syscall(id: usize, args: [usize; 3]) -> isize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn sys_read(fd: usize, buffer: &mut [u8]) -> isize {
|
pub fn sys_read(fd: usize, buffer: &mut [u8]) -> isize {
|
||||||
syscall(SYSCALL_READ, [fd, buffer.as_mut_ptr() as usize, buffer.len()])
|
syscall(
|
||||||
|
SYSCALL_READ,
|
||||||
|
[fd, buffer.as_mut_ptr() as usize, buffer.len()],
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
|
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
|
||||||
|
Loading…
Reference in New Issue
Block a user