From e9df799333ba66b704f693e972324001487bb592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=B8=B0=E6=BA=90?= Date: Tue, 4 Feb 2020 17:22:35 +0800 Subject: [PATCH] run cargo fmt --- os/build.rs | 8 ++- os/src/context.rs | 60 +++++++------------ os/src/fs/device.rs | 15 ++--- os/src/fs/mod.rs | 5 +- os/src/fs/stdio.rs | 12 ++-- os/src/init.rs | 12 ++-- os/src/interrupt.rs | 65 +++++++++------------ os/src/io.rs | 7 +-- os/src/lib.rs | 14 ++--- os/src/main.rs | 2 +- os/src/memory/frame_allocator.rs | 22 +++++-- os/src/memory/memory_set/area.rs | 55 +++++++++--------- os/src/memory/memory_set/attr.rs | 20 +++---- os/src/memory/memory_set/handler.rs | 79 ++++++++++++------------- os/src/memory/memory_set/mod.rs | 50 ++++++++-------- os/src/memory/mod.rs | 25 +++----- os/src/memory/paging.rs | 89 ++++++++++++++++------------- os/src/process/mod.rs | 18 +++--- os/src/process/processor.rs | 65 +++++++++------------ os/src/process/scheduler.rs | 27 ++++----- os/src/process/structs.rs | 61 ++++++++------------ os/src/process/thread_pool.rs | 27 ++++----- os/src/sync/condvar.rs | 10 ++-- os/src/syscall.rs | 20 +++---- os/src/timer.rs | 5 +- usr/rust/src/bin/notebook.rs | 2 +- usr/rust/src/bin/user_shell.rs | 45 +++++++-------- usr/rust/src/io.rs | 2 +- usr/rust/src/lang_items.rs | 8 ++- usr/rust/src/lib.rs | 2 +- usr/rust/src/syscall.rs | 12 +--- 31 files changed, 383 insertions(+), 461 deletions(-) diff --git a/os/build.rs b/os/build.rs index 42cacd5..67c4c82 100644 --- a/os/build.rs +++ b/os/build.rs @@ -15,13 +15,17 @@ fn gen_link_user_asm() -> Result<()> { let user_img = std::env::var("USER_IMG").unwrap(); writeln!(f, "# generated by build.rs - do not edit")?; - writeln!(f, r#" + writeln!( + f, + r#" .section .data .global _user_img_start .global _user_img_end _user_img_start: .incbin "{}" _user_img_end: -"#, user_img)?; +"#, + user_img + )?; Ok(()) } diff --git a/os/src/context.rs b/os/src/context.rs index a8c8020..5ea0e44 100644 --- a/os/src/context.rs +++ b/os/src/context.rs @@ -1,17 +1,14 @@ -use riscv::register::{ - sstatus::Sstatus, - scause::Scause, -}; -use riscv::register::sstatus; use core::mem::zeroed; +use riscv::register::sstatus; +use riscv::register::{scause::Scause, sstatus::Sstatus}; #[repr(C)] pub struct TrapFrame { - pub x: [usize; 32], // General registers + pub x: [usize; 32], // General registers pub sstatus: Sstatus, // Supervisor Status Register - pub sepc: usize, // Supervisor exception program counter - pub stval: usize, // Supervisor trap value - pub scause: Scause, // Scause register: record the cause of exception/interrupt/trap + pub sepc: usize, // Supervisor exception program counter + pub stval: usize, // Supervisor trap value + pub scause: Scause, // Scause register: record the cause of exception/interrupt/trap } #[repr(C)] @@ -19,7 +16,6 @@ pub struct Context { pub content_addr: usize, } - impl Context { #[naked] #[inline(never)] @@ -27,16 +23,11 @@ impl Context { asm!(include_str!("process/switch.asm") :::: "volatile"); } - pub fn null() -> Context { - Context { content_addr: 0, } + pub fn null() -> Context { + Context { content_addr: 0 } } - pub unsafe fn new_kernel_thread( - entry: usize, - kstack_top: usize, - satp: usize - ) -> Context { - + pub unsafe fn new_kernel_thread(entry: usize, kstack_top: usize, satp: usize) -> Context { ContextContent::new_kernel_thread(entry, kstack_top, satp).push_at(kstack_top) } @@ -46,12 +37,12 @@ impl Context { contextContent.tf.x[11] = args[1]; contextContent.tf.x[12] = args[2]; } - - pub unsafe fn new_user_thread( + + pub unsafe fn new_user_thread( entry: usize, ustack_top: usize, kstack_top: usize, - satp: usize + satp: usize, ) -> Self { ContextContent::new_user_thread(entry, ustack_top, satp).push_at(kstack_top) } @@ -66,16 +57,11 @@ pub struct ContextContent { } extern "C" { - fn __trapret(); + fn __trapret(); } impl ContextContent { - fn new_kernel_thread( - entry: usize, - kstack_top: usize, - satp: usize, - ) -> ContextContent { - + fn new_kernel_thread(entry: usize, kstack_top: usize, satp: usize) -> ContextContent { let mut content = ContextContent { ra: __trapret as usize, satp, @@ -89,16 +75,12 @@ impl ContextContent { tf.sstatus.set_spie(true); tf.sstatus.set_sie(false); tf - } + }, }; content } - fn new_user_thread( - entry: usize, - ustack_top: usize, - satp: usize - ) -> Self { + fn new_user_thread(entry: usize, ustack_top: usize, satp: usize) -> Self { ContextContent { ra: __trapret as usize, satp, @@ -112,15 +94,15 @@ impl ContextContent { tf.sstatus.set_sie(false); tf.sstatus.set_spp(sstatus::SPP::User); tf - } + }, } } - unsafe fn push_at(self, stack_top: usize) -> Context { + unsafe fn push_at(self, stack_top: usize) -> Context { let ptr = (stack_top as *mut ContextContent).sub(1); *ptr = self; - Context { content_addr: ptr as usize } + Context { + content_addr: ptr as usize, + } } } - - diff --git a/os/src/fs/device.rs b/os/src/fs/device.rs index 03fcc65..ab2c42b 100644 --- a/os/src/fs/device.rs +++ b/os/src/fs/device.rs @@ -1,23 +1,18 @@ -use spin::RwLock; use rcore_fs::dev::*; +use spin::RwLock; pub struct MemBuf(RwLock<&'static mut [u8]>); impl MemBuf { pub unsafe fn new(begin: usize, end: usize) -> Self { use core::slice; - MemBuf( - RwLock::new( - slice::from_raw_parts_mut( - begin as *mut u8, - end - begin - ) - ) - ) + MemBuf(RwLock::new(slice::from_raw_parts_mut( + begin as *mut u8, + end - begin, + ))) } } - impl Device for MemBuf { fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result { let slice = self.0.read(); diff --git a/os/src/fs/mod.rs b/os/src/fs/mod.rs index 4adc82f..abfc57b 100644 --- a/os/src/fs/mod.rs +++ b/os/src/fs/mod.rs @@ -1,10 +1,10 @@ mod device; pub mod stdio; +use alloc::{sync::Arc, vec::Vec}; use lazy_static::*; use rcore_fs::vfs::*; use rcore_fs_sfs::SimpleFileSystem; -use alloc::{ sync::Arc, vec::Vec }; lazy_static! { pub static ref ROOT_INODE: Arc = { @@ -15,11 +15,10 @@ lazy_static! { }; let start = _user_img_start as usize; let end = _user_img_end as usize; - Arc::new(unsafe { device::MemBuf::new(start, end) }) + Arc::new(unsafe { device::MemBuf::new(start, end) }) }; let sfs = SimpleFileSystem::open(device).expect("failed to open SFS"); sfs.root_inode() - }; } diff --git a/os/src/fs/stdio.rs b/os/src/fs/stdio.rs index 03b4368..eee42b6 100644 --- a/os/src/fs/stdio.rs +++ b/os/src/fs/stdio.rs @@ -1,8 +1,8 @@ -use alloc::{ collections::VecDeque, sync::Arc }; -use spin::Mutex; use crate::process; use crate::sync::condvar::*; +use alloc::{collections::VecDeque, sync::Arc}; use lazy_static::*; +use spin::Mutex; pub struct Stdin { buf: Mutex>, @@ -18,23 +18,19 @@ impl Stdin { } pub fn push(&self, ch: char) { - self.buf - .lock() - .push_back(ch); + self.buf.lock().push_back(ch); self.pushed.notify(); } - pub fn pop(&self) -> char { loop { let ret = self.buf.lock().pop_front(); match ret { Some(ch) => { return ch; - }, + } None => { self.pushed.wait(); - } } } diff --git a/os/src/init.rs b/os/src/init.rs index 7eb3fea..d94231d 100644 --- a/os/src/init.rs +++ b/os/src/init.rs @@ -2,10 +2,7 @@ global_asm!(include_str!("boot/entry64.asm")); global_asm!(include_str!("link_user.S")); use crate::consts::*; -use crate::memory::{ - alloc_frame, - dealloc_frame -}; +use crate::memory::{alloc_frame, dealloc_frame}; #[no_mangle] pub extern "C" fn rust_main() -> ! { @@ -14,13 +11,12 @@ pub extern "C" fn rust_main() -> ! { } crate::memory::init( ((end as usize - KERNEL_BEGIN_VADDR + KERNEL_BEGIN_PADDR) >> 12) + 1, - PHYSICAL_MEMORY_END >> 12 + PHYSICAL_MEMORY_END >> 12, ); - crate::interrupt::init(); - crate::fs::init(); + crate::interrupt::init(); + crate::fs::init(); crate::process::init(); crate::timer::init(); crate::process::run(); loop {} } - diff --git a/os/src/interrupt.rs b/os/src/interrupt.rs index f6186f8..1edeb16 100644 --- a/os/src/interrupt.rs +++ b/os/src/interrupt.rs @@ -1,23 +1,12 @@ -use riscv::register::{ - scause::{ - self, - Trap, - Exception, - Interrupt - }, - sepc, - stvec, - sscratch, - sstatus -}; -use crate::timer::{ - TICKS, - clock_set_next_event -}; use crate::context::TrapFrame; -use crate::process::tick; use crate::memory::access_pa_via_va; +use crate::process::tick; +use crate::timer::{clock_set_next_event, TICKS}; use riscv::register::sie; +use riscv::register::{ + scause::{self, Exception, Interrupt, Trap}, + sepc, sscratch, sstatus, stvec, +}; global_asm!(include_str!("trap/trap.asm")); @@ -25,19 +14,19 @@ pub fn init() { unsafe { extern "C" { fn __alltraps(); - } + } sscratch::write(0); stvec::write(__alltraps as usize, stvec::TrapMode::Direct); - sstatus::set_sie(); + sstatus::set_sie(); - // enable external interrupt - sie::set_sext(); + // enable external interrupt + sie::set_sext(); - // closed by OpenSBI, so we open them manually - // see https://github.com/rcore-os/rCore/blob/54fddfbe1d402ac1fafd9d58a0bd4f6a8dd99ece/kernel/src/arch/riscv32/board/virt/mod.rs#L4 - init_external_interrupt(); - enable_serial_interrupt(); + // closed by OpenSBI, so we open them manually + // see https://github.com/rcore-os/rCore/blob/54fddfbe1d402ac1fafd9d58a0bd4f6a8dd99ece/kernel/src/arch/riscv32/board/virt/mod.rs#L4 + init_external_interrupt(); + enable_serial_interrupt(); } println!("++++ setup interrupt! ++++"); } @@ -62,9 +51,9 @@ pub fn rust_trap(tf: &mut TrapFrame) { Trap::Exception(Exception::InstructionPageFault) => page_fault(tf), Trap::Exception(Exception::LoadPageFault) => page_fault(tf), Trap::Exception(Exception::StorePageFault) => page_fault(tf), - Trap::Exception(Exception::UserEnvCall) => syscall(tf), - Trap::Interrupt(Interrupt::SupervisorExternal) => external(), - _ => panic!("undefined trap!") + Trap::Exception(Exception::UserEnvCall) => syscall(tf), + Trap::Interrupt(Interrupt::SupervisorExternal) => external(), + _ => panic!("undefined trap!"), } } @@ -78,17 +67,18 @@ fn super_timer() { tick(); } fn page_fault(tf: &mut TrapFrame) { - println!("{:?} va = {:#x} instruction = {:#x}", tf.scause.cause(), tf.stval, tf.sepc); + println!( + "{:?} va = {:#x} instruction = {:#x}", + tf.scause.cause(), + tf.stval, + tf.sepc + ); panic!("page fault!"); } fn syscall(tf: &mut TrapFrame) { tf.sepc += 4; - let ret = crate::syscall::syscall( - tf.x[17], - [tf.x[10], tf.x[11], tf.x[12]], - tf - ); + let ret = crate::syscall::syscall(tf.x[17], [tf.x[10], tf.x[11], tf.x[12]], tf); tf.x[10] = ret as usize; } @@ -101,13 +91,12 @@ fn try_serial() -> bool { Some(ch) => { if (ch == '\r') { crate::fs::stdio::STDIN.push('\n'); - } - else { + } else { crate::fs::stdio::STDIN.push(ch); } true - }, - None => false + } + None => false, } } diff --git a/os/src/io.rs b/os/src/io.rs index 0b794fe..55412c3 100644 --- a/os/src/io.rs +++ b/os/src/io.rs @@ -1,5 +1,5 @@ use crate::sbi; -use core::fmt::{ self, Write }; +use core::fmt::{self, Write}; pub fn putchar(ch: char) { sbi::console_putchar(ch as u8 as usize); @@ -42,7 +42,7 @@ pub fn getchar() -> char { match c { 255 => '\0', - c => c as char + c => c as char, } } // 调用 OpenSBI 接口 @@ -50,7 +50,6 @@ pub fn getchar_option() -> Option { let c = sbi::console_getchar() as isize; match c { -1 => None, - c => Some(c as u8 as char) + c => Some(c as u8 as char), } } - diff --git a/os/src/lib.rs b/os/src/lib.rs index 318a70c..ad61357 100644 --- a/os/src/lib.rs +++ b/os/src/lib.rs @@ -9,15 +9,15 @@ extern crate alloc; #[macro_use] mod io; +mod consts; +mod context; +mod fs; mod init; -mod lang_items; -mod sbi; mod interrupt; -mod context; -mod timer; -mod consts; +mod lang_items; mod memory; mod process; -mod syscall; -mod fs; +mod sbi; mod sync; +mod syscall; +mod timer; diff --git a/os/src/main.rs b/os/src/main.rs index b0f1e15..94d7352 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -2,4 +2,4 @@ #![no_main] #[allow(unused_imports)] -use os; \ No newline at end of file +use os; diff --git a/os/src/memory/frame_allocator.rs b/os/src/memory/frame_allocator.rs index 68f143d..e078af3 100644 --- a/os/src/memory/frame_allocator.rs +++ b/os/src/memory/frame_allocator.rs @@ -5,7 +5,7 @@ pub struct SegmentTreeAllocator { a: [u8; MAX_PHYSICAL_PAGES << 1], m: usize, n: usize, - offset: usize + offset: usize, } impl SegmentTreeAllocator { @@ -16,9 +16,15 @@ impl SegmentTreeAllocator { while self.m < self.n + 2 { self.m = self.m << 1; } - for i in (1..(self.m << 1)) { self.a[i] = 1; } - for i in (1..self.n) { self.a[self.m + i] = 0; } - for i in (1..self.m).rev() { self.a[i] = self.a[i << 1] & self.a[(i << 1) | 1]; } + for i in (1..(self.m << 1)) { + self.a[i] = 1; + } + for i in (1..self.n) { + self.a[self.m + i] = 0; + } + for i in (1..self.m).rev() { + self.a[i] = self.a[i << 1] & self.a[(i << 1) | 1]; + } } pub fn alloc(&mut self) -> usize { @@ -28,7 +34,11 @@ impl SegmentTreeAllocator { } let mut p = 1; while p < self.m { - if self.a[p << 1] == 0 { p = p << 1; } else { p = (p << 1) | 1; } + if self.a[p << 1] == 0 { + p = p << 1; + } else { + p = (p << 1) | 1; + } } let result = p + self.offset - self.m; self.a[p] = 1; @@ -56,5 +66,5 @@ pub static SEGMENT_TREE_ALLOCATOR: Mutex = Mutex::new(Segm a: [0; MAX_PHYSICAL_PAGES << 1], m: 0, n: 0, - offset: 0 + offset: 0, }); diff --git a/os/src/memory/memory_set/area.rs b/os/src/memory/memory_set/area.rs index 5b50940..b5e0e02 100644 --- a/os/src/memory/memory_set/area.rs +++ b/os/src/memory/memory_set/area.rs @@ -1,57 +1,60 @@ -use alloc::boxed::Box; -use crate::memory::paging::{PageTableImpl, PageRange,}; -use super::{attr::MemoryAttr, handler::MemoryHandler, }; +use super::{attr::MemoryAttr, handler::MemoryHandler}; use crate::consts::PAGE_SIZE; +use crate::memory::paging::{PageRange, PageTableImpl}; +use alloc::boxed::Box; -#[derive(Debug,Clone)] +#[derive(Debug, Clone)] pub struct MemoryArea { - start : usize, - end : usize, - handler : Box, - attr : MemoryAttr, + start: usize, + end: usize, + handler: Box, + attr: MemoryAttr, } impl MemoryArea { - pub fn map(&self, pt : &mut PageTableImpl) { + pub fn map(&self, pt: &mut PageTableImpl) { for page in PageRange::new(self.start, self.end) { self.handler.map(pt, page, &self.attr); } } - fn unmap(&self, pt : &mut PageTableImpl) { + fn unmap(&self, pt: &mut PageTableImpl) { for page in PageRange::new(self.start, self.end) { self.handler.unmap(pt, page); } } - pub fn is_overlap_with(&self, start_addr : usize, end_addr : usize) -> bool { + pub fn is_overlap_with(&self, start_addr: usize, end_addr: usize) -> bool { let p1 = self.start / PAGE_SIZE; let p2 = (self.end - 1) / PAGE_SIZE + 1; let p3 = start_addr / PAGE_SIZE; let p4 = (end_addr - 1) / PAGE_SIZE + 1; !((p1 >= p4) || (p2 <= p3)) } - - pub fn new(start_addr : usize, end_addr : usize, handler : Box, attr : MemoryAttr) -> Self { - MemoryArea{ - start : start_addr, - end : end_addr, - handler : handler, - attr : attr, + + pub fn new( + start_addr: usize, + end_addr: usize, + handler: Box, + attr: MemoryAttr, + ) -> Self { + MemoryArea { + start: start_addr, + end: end_addr, + handler: handler, + attr: attr, } } - pub fn page_copy(&self, pt: &mut PageTableImpl, src: usize, length: usize) { + pub fn page_copy(&self, pt: &mut PageTableImpl, src: usize, length: usize) { let mut l = length; let mut s = src; for page in PageRange::new(self.start, self.end) { - self.handler.page_copy( - pt, - page, - s, - if l < PAGE_SIZE { l } else { PAGE_SIZE }, - ); + self.handler + .page_copy(pt, page, s, if l < PAGE_SIZE { l } else { PAGE_SIZE }); s += PAGE_SIZE; - if l >= PAGE_SIZE { l -= PAGE_SIZE; } + if l >= PAGE_SIZE { + l -= PAGE_SIZE; + } } } } diff --git a/os/src/memory/memory_set/attr.rs b/os/src/memory/memory_set/attr.rs index 00b85bc..922ae26 100644 --- a/os/src/memory/memory_set/attr.rs +++ b/os/src/memory/memory_set/attr.rs @@ -1,22 +1,21 @@ use crate::memory::paging::PageEntry; -#[derive(Clone,Debug)] +#[derive(Clone, Debug)] pub struct MemoryAttr { - user : bool, - readonly : bool, - execute : bool, + user: bool, + readonly: bool, + execute: bool, } impl MemoryAttr { - pub fn new() -> Self{ + pub fn new() -> Self { MemoryAttr { - user : false, - readonly : false, - execute : false, + user: false, + readonly: false, + execute: false, } } - pub fn set_user(mut self) -> Self { self.user = true; self @@ -30,8 +29,7 @@ impl MemoryAttr { self } - - pub fn apply(&self, entry : &mut PageEntry) { + pub fn apply(&self, entry: &mut PageEntry) { entry.set_present(true); entry.set_user(self.user); entry.set_writable(!self.readonly); diff --git a/os/src/memory/memory_set/handler.rs b/os/src/memory/memory_set/handler.rs index 42a8c00..3750f24 100644 --- a/os/src/memory/memory_set/handler.rs +++ b/os/src/memory/memory_set/handler.rs @@ -1,60 +1,59 @@ -use crate::memory::paging::PageTableImpl; use super::attr::MemoryAttr; -use crate::memory::alloc_frame; -use core::fmt::Debug; -use alloc::boxed::Box; -use crate::memory::access_pa_via_va; use crate::consts::PAGE_SIZE; +use crate::memory::access_pa_via_va; +use crate::memory::alloc_frame; +use crate::memory::paging::PageTableImpl; +use alloc::boxed::Box; +use core::fmt::Debug; pub trait MemoryHandler: Debug + 'static { fn box_clone(&self) -> Box; fn map(&self, pt: &mut PageTableImpl, va: usize, attr: &MemoryAttr); fn unmap(&self, pt: &mut PageTableImpl, va: usize); - fn page_copy(&self, pt: &mut PageTableImpl, va: usize, src: usize, length: usize); + fn page_copy(&self, pt: &mut PageTableImpl, va: usize, src: usize, length: usize); } impl Clone for Box { - fn clone(&self) -> Box { self.box_clone() } + fn clone(&self) -> Box { + self.box_clone() + } } - #[derive(Debug, Clone)] -pub struct Linear { offset: usize } +pub struct Linear { + offset: usize, +} impl Linear { pub fn new(off: usize) -> Self { - Linear { offset: off, } + Linear { offset: off } } } impl MemoryHandler for Linear { - fn box_clone(&self) -> Box { Box::new(self.clone()) } + fn box_clone(&self) -> Box { + Box::new(self.clone()) + } fn map(&self, pt: &mut PageTableImpl, va: usize, attr: &MemoryAttr) { attr.apply(pt.map(va, va - self.offset)); } fn unmap(&self, pt: &mut PageTableImpl, va: usize) { pt.unmap(va); } - fn page_copy(&self, pt: &mut PageTableImpl, va: usize, src: usize, length: usize) { - let pa = pt.get_entry(va) - .expect("get pa error!") - .0 - .addr() - .as_usize(); + fn page_copy(&self, pt: &mut PageTableImpl, va: usize, src: usize, length: usize) { + let pa = pt.get_entry(va).expect("get pa error!").0.addr().as_usize(); assert!(va == access_pa_via_va(pa)); assert!(va == pa + self.offset); unsafe { - let dst = core::slice::from_raw_parts_mut( - va as *mut u8, - PAGE_SIZE, - ); + let dst = core::slice::from_raw_parts_mut(va as *mut u8, PAGE_SIZE); if length > 0 { - let src = core::slice::from_raw_parts( - src as *const u8, - PAGE_SIZE, - ); - for i in 0..length { dst[i] = src[i]; } + let src = core::slice::from_raw_parts(src as *const u8, PAGE_SIZE); + for i in 0..length { + dst[i] = src[i]; + } + } + for i in length..PAGE_SIZE { + dst[i] = 0; } - for i in length..PAGE_SIZE { dst[i] = 0; } } } } @@ -80,25 +79,19 @@ impl MemoryHandler for ByFrame { fn unmap(&self, pt: &mut PageTableImpl, va: usize) { pt.unmap(va); } - fn page_copy(&self, pt: &mut PageTableImpl, va: usize, src: usize, length: usize) { - let pa = pt.get_entry(va) - .expect("get pa error!") - .0 - .addr() - .as_usize(); + fn page_copy(&self, pt: &mut PageTableImpl, va: usize, src: usize, length: usize) { + let pa = pt.get_entry(va).expect("get pa error!").0.addr().as_usize(); unsafe { - let dst = core::slice::from_raw_parts_mut( - access_pa_via_va(pa) as *mut u8, - PAGE_SIZE, - ); + let dst = core::slice::from_raw_parts_mut(access_pa_via_va(pa) as *mut u8, PAGE_SIZE); if length > 0 { - let src = core::slice::from_raw_parts( - src as *const u8, - PAGE_SIZE, - ); - for i in 0..length { dst[i] = src[i]; } + let src = core::slice::from_raw_parts(src as *const u8, PAGE_SIZE); + for i in 0..length { + dst[i] = src[i]; + } + } + for i in length..PAGE_SIZE { + dst[i] = 0; } - for i in length..PAGE_SIZE { dst[i] = 0; } } } } diff --git a/os/src/memory/memory_set/mod.rs b/os/src/memory/memory_set/mod.rs index 4d59db1..4117256 100644 --- a/os/src/memory/memory_set/mod.rs +++ b/os/src/memory/memory_set/mod.rs @@ -1,20 +1,14 @@ +pub mod area; pub mod attr; pub mod handler; -pub mod area; -use area::MemoryArea; -use attr::MemoryAttr; -use crate::memory::paging::PageTableImpl; use crate::consts::*; -use handler::{ - MemoryHandler, - Linear -}; -use alloc::{ - boxed::Box, - vec::Vec -}; use crate::memory::access_pa_via_va; +use crate::memory::paging::PageTableImpl; +use alloc::{boxed::Box, vec::Vec}; +use area::MemoryArea; +use attr::MemoryAttr; +use handler::{Linear, MemoryHandler}; pub struct MemorySet { areas: Vec, @@ -22,7 +16,14 @@ pub struct MemorySet { } impl MemorySet { - pub fn push(&mut self, start: usize, end: usize, attr: MemoryAttr, handler: impl MemoryHandler, data: Option<(usize, usize)>) { + pub fn push( + &mut self, + start: usize, + end: usize, + attr: MemoryAttr, + handler: impl MemoryHandler, + data: Option<(usize, usize)>, + ) { assert!(start <= end, "invalid memory area!"); assert!(self.test_free_area(start, end), "memory area overlap!"); let area = MemoryArea::new(start, end, Box::new(handler), attr); @@ -31,8 +32,7 @@ impl MemorySet { area.page_copy(&mut self.page_table, src, length); } self.areas.push(area); - - } + } fn test_free_area(&self, start: usize, end: usize) -> bool { self.areas .iter() @@ -42,7 +42,7 @@ impl MemorySet { pub unsafe fn activate(&self) { self.page_table.activate(); } - pub fn new() -> Self { + pub fn new() -> Self { let mut memory_set = MemorySet { areas: Vec::new(), page_table: PageTableImpl::new_bare(), @@ -70,7 +70,7 @@ impl MemorySet { etext as usize, MemoryAttr::new().set_readonly().set_execute(), Linear::new(offset), - None, + None, ); // .rodata R self.push( @@ -78,7 +78,7 @@ impl MemorySet { erodata as usize, MemoryAttr::new().set_readonly(), Linear::new(offset), - None, + None, ); // .data R|W self.push( @@ -86,7 +86,7 @@ impl MemorySet { edata as usize, MemoryAttr::new(), Linear::new(offset), - None, + None, ); // .bss R|W self.push( @@ -94,18 +94,18 @@ impl MemorySet { ebss as usize, MemoryAttr::new(), Linear::new(offset), - None, + None, ); // 物理内存 R|W self.push( - (end as usize / PAGE_SIZE + 1) * PAGE_SIZE, + (end as usize / PAGE_SIZE + 1) * PAGE_SIZE, access_pa_via_va(PHYSICAL_MEMORY_END), MemoryAttr::new(), Linear::new(offset), - None, + None, ); } - pub fn token(&self) -> usize { - self.page_table.token() - } + pub fn token(&self) -> usize { + self.page_table.token() + } } diff --git a/os/src/memory/mod.rs b/os/src/memory/mod.rs index 912db86..c32246b 100644 --- a/os/src/memory/mod.rs +++ b/os/src/memory/mod.rs @@ -1,21 +1,12 @@ mod frame_allocator; -pub mod paging; pub mod memory_set; +pub mod paging; +use crate::consts::*; use buddy_system_allocator::LockedHeap; use frame_allocator::SEGMENT_TREE_ALLOCATOR as FRAME_ALLOCATOR; -use riscv::addr::{ - VirtAddr, - PhysAddr, - Page, - Frame -}; -use crate::consts::*; -use memory_set::{ - MemorySet, - attr::MemoryAttr, - handler::Linear -}; +use memory_set::{attr::MemoryAttr, handler::Linear, MemorySet}; +use riscv::addr::{Frame, Page, PhysAddr, VirtAddr}; use riscv::register::sstatus; pub fn init(l: usize, r: usize) { @@ -61,21 +52,21 @@ pub fn kernel_remap() { bootstacktop as usize, MemoryAttr::new(), Linear::new(PHYSICAL_MEMORY_OFFSET), - None, + None, ); - memory_set.push( + memory_set.push( access_pa_via_va(0x0c00_2000), access_pa_via_va(0x0c00_3000), MemoryAttr::new(), Linear::new(PHYSICAL_MEMORY_OFFSET), - None + None, ); memory_set.push( access_pa_via_va(0x1000_0000), access_pa_via_va(0x1000_1000), MemoryAttr::new(), Linear::new(PHYSICAL_MEMORY_OFFSET), - None + None, ); unsafe { diff --git a/os/src/memory/paging.rs b/os/src/memory/paging.rs index 47f1c79..9ab62e8 100644 --- a/os/src/memory/paging.rs +++ b/os/src/memory/paging.rs @@ -1,25 +1,12 @@ use crate::consts::*; +use crate::memory::{access_pa_via_va, alloc_frame, dealloc_frame}; use riscv::addr::*; +use riscv::asm::{sfence_vma, sfence_vma_all}; use riscv::paging::{ - PageTableEntry, - Mapper, - Rv39PageTable, - PageTable as PageTableEntryArray, - PageTableFlags as EF, - FrameAllocator, - FrameDeallocator - -}; -use riscv::asm::{ - sfence_vma, - sfence_vma_all, + FrameAllocator, FrameDeallocator, Mapper, PageTable as PageTableEntryArray, PageTableEntry, + PageTableFlags as EF, Rv39PageTable, }; use riscv::register::satp; -use crate::memory::{ - alloc_frame, - dealloc_frame, - access_pa_via_va -}; pub struct PageEntry(pub &'static mut PageTableEntry, Page); @@ -29,27 +16,45 @@ impl PageEntry { sfence_vma(0, self.1.start_address().as_usize()); } } - - pub fn accessed(&self) -> bool { self.0.flags().contains(EF::ACCESSED) } - pub fn clear_accessed(&mut self) { self.0.flags_mut().remove(EF::ACCESSED); } - pub fn dirty(&self) -> bool { self.0.flags().contains(EF::DIRTY) } - pub fn clear_dirty(&mut self) { self.0.flags_mut().remove(EF::DIRTY); } + pub fn accessed(&self) -> bool { + self.0.flags().contains(EF::ACCESSED) + } + pub fn clear_accessed(&mut self) { + self.0.flags_mut().remove(EF::ACCESSED); + } + + pub fn dirty(&self) -> bool { + self.0.flags().contains(EF::DIRTY) + } + pub fn clear_dirty(&mut self) { + self.0.flags_mut().remove(EF::DIRTY); + } - pub fn writable(&self) -> bool { self.0.flags().contains(EF::WRITABLE) } + pub fn writable(&self) -> bool { + self.0.flags().contains(EF::WRITABLE) + } pub fn set_writable(&mut self, value: bool) { - self.0.flags_mut().set(EF::WRITABLE, value); + self.0.flags_mut().set(EF::WRITABLE, value); } - pub fn present(&self) -> bool { self.0.flags().contains(EF::VALID | EF::READABLE) } + pub fn present(&self) -> bool { + self.0.flags().contains(EF::VALID | EF::READABLE) + } pub fn set_present(&mut self, value: bool) { self.0.flags_mut().set(EF::VALID | EF::READABLE, value); } - pub fn user(&self) -> bool { self.0.flags().contains(EF::USER) } - pub fn set_user(&mut self, value: bool) { self.0.flags_mut().set(EF::USER, value); } + pub fn user(&self) -> bool { + self.0.flags().contains(EF::USER) + } + pub fn set_user(&mut self, value: bool) { + self.0.flags_mut().set(EF::USER, value); + } - pub fn execute(&self) -> bool { self.0.flags().contains(EF::EXECUTABLE) } + pub fn execute(&self) -> bool { + self.0.flags().contains(EF::EXECUTABLE) + } pub fn set_execute(&mut self, value: bool) { self.0.flags_mut().set(EF::EXECUTABLE, value); } @@ -94,11 +99,11 @@ impl PageTableImpl { PageTableImpl { page_table: Rv39PageTable::new(table, PHYSICAL_MEMORY_OFFSET), root_frame: frame, - entry: None + entry: None, } } - pub fn map(&mut self, va: usize, pa: usize) -> &mut PageEntry { + pub fn map(&mut self, va: usize, pa: usize) -> &mut PageEntry { let flags = EF::VALID | EF::READABLE | EF::WRITABLE; let page = Page::of_addr(VirtAddr::new(va)); let frame = Frame::of_addr(PhysAddr::new(pa)); @@ -121,20 +126,27 @@ impl PageTableImpl { let e = unsafe { &mut *(e as *mut PageTableEntry) }; self.entry = Some(PageEntry(e, page)); Some(self.entry.as_mut().unwrap()) - } - else { + } else { None } } - pub fn token(&self) -> usize { self.root_frame.number() | (8 << 60) } + pub fn token(&self) -> usize { + self.root_frame.number() | (8 << 60) + } unsafe fn set_token(token: usize) { asm!("csrw satp, $0" :: "r"(token) :: "volatile"); } - fn active_token() -> usize { satp::read().bits() } + fn active_token() -> usize { + satp::read().bits() + } - fn flush_tlb() { unsafe { sfence_vma_all(); } } + fn flush_tlb() { + unsafe { + sfence_vma_all(); + } + } pub unsafe fn activate(&self) { let old_token = Self::active_token(); @@ -151,7 +163,7 @@ impl PageTableImpl { #[repr(C)] pub struct PageRange { start: usize, - end: usize + end: usize, } // 为 PageRange 实现 Iterator trait 成为可被遍历的迭代器 @@ -163,8 +175,7 @@ impl Iterator for PageRange { let page = self.start << 12; self.start += 1; Some(page) - } - else { + } else { None } } @@ -174,7 +185,7 @@ impl PageRange { pub fn new(start_addr: usize, end_addr: usize) -> Self { PageRange { start: start_addr / PAGE_SIZE, - end: (end_addr - 1) / PAGE_SIZE + 1 + end: (end_addr - 1) / PAGE_SIZE + 1, } } } diff --git a/os/src/process/mod.rs b/os/src/process/mod.rs index 9b5d2d5..a97ba07 100644 --- a/os/src/process/mod.rs +++ b/os/src/process/mod.rs @@ -1,22 +1,18 @@ -pub mod structs; +pub mod processor; pub mod scheduler; +pub mod structs; pub mod thread_pool; -pub mod processor; -use structs::Thread; +use crate::fs::{INodeExt, ROOT_INODE}; +use alloc::boxed::Box; use processor::Processor; use scheduler::RRScheduler; +use structs::Thread; use thread_pool::ThreadPool; -use alloc::boxed::Box; -use crate::fs::{ - ROOT_INODE, - INodeExt -}; pub type Tid = usize; pub type ExitCode = usize; - static CPU: Processor = Processor::new(); pub fn init() { @@ -26,7 +22,7 @@ pub fn init() { idle.append_initial_arguments([&CPU as *const Processor as usize, 0, 0]); CPU.init(idle, Box::new(thread_pool)); - execute("rust/user_shell", None); + execute("rust/user_shell", None); println!("++++ setup process! ++++"); } @@ -39,7 +35,7 @@ pub fn execute(path: &str, host_tid: Option) -> bool { let user_thread = unsafe { Thread::new_user(data.as_slice(), host_tid) }; CPU.add_thread(user_thread); true - }, + } Err(_) => { println!("command not found!"); false diff --git a/os/src/process/processor.rs b/os/src/process/processor.rs index 412374f..10b76a8 100644 --- a/os/src/process/processor.rs +++ b/os/src/process/processor.rs @@ -1,10 +1,10 @@ -use core::cell::UnsafeCell; -use alloc::boxed::Box; -use crate::process::Tid; +use crate::context::ContextContent; +use crate::interrupt::*; use crate::process::structs::*; use crate::process::thread_pool::ThreadPool; -use crate::interrupt::*; -use crate::context::ContextContent; +use crate::process::Tid; +use alloc::boxed::Box; +use core::cell::UnsafeCell; pub struct ProcessorInner { pool: Box, @@ -27,14 +27,11 @@ impl Processor { pub fn init(&self, idle: Box, pool: Box) { unsafe { - *self.inner.get() = Some( - ProcessorInner { - pool, - idle, - current: None, - } - ); - + *self.inner.get() = Some(ProcessorInner { + pool, + idle, + current: None, + }); } } @@ -48,7 +45,7 @@ impl Processor { self.inner().pool.add(thread); } - pub fn idle_main(&self) -> ! { + pub fn idle_main(&self) -> ! { let inner = self.inner(); disable_and_store(); @@ -56,39 +53,34 @@ impl Processor { if let Some(thread) = inner.pool.acquire() { inner.current = Some(thread); // println!("\n>>>> will switch_to thread {} in idle_main!", inner.current.as_mut().unwrap().0); - inner.idle.switch_to( - &mut *inner.current.as_mut().unwrap().1 - ); + inner + .idle + .switch_to(&mut *inner.current.as_mut().unwrap().1); // println!("\n<<<< switch_back to idle in idle_main!"); let (tid, thread) = inner.current.take().unwrap(); inner.pool.retrieve(tid, thread); - } - else { + } else { enable_and_wfi(); disable_and_store(); } } } - pub fn tick(&self) { + pub fn tick(&self) { let inner = self.inner(); if !inner.current.is_none() { if inner.pool.tick() { let flags = disable_and_store(); - inner.current - .as_mut() - .unwrap() - .1 - .switch_to(&mut inner.idle); + inner.current.as_mut().unwrap().1.switch_to(&mut inner.idle); restore(flags); } } } - pub fn exit(&self, code: usize) -> ! { + pub fn exit(&self, code: usize) -> ! { disable_and_store(); let inner = self.inner(); let tid = inner.current.as_ref().unwrap().0; @@ -96,33 +88,32 @@ impl Processor { inner.pool.exit(tid); println!("thread {} exited, exit code = {}", tid, code); - if let Some(wait) = inner.current.as_ref().unwrap().1.wait { + if let Some(wait) = inner.current.as_ref().unwrap().1.wait { inner.pool.wakeup(wait); } - inner.current - .as_mut() - .unwrap() - .1 - .switch_to(&mut inner.idle); + inner.current.as_mut().unwrap().1.switch_to(&mut inner.idle); loop {} } - pub fn run(&self) { + pub fn run(&self) { Thread::get_boot_thread().switch_to(&mut self.inner().idle); } - pub fn yield_now(&self) { + pub fn yield_now(&self) { let inner = self.inner(); if !inner.current.is_none() { unsafe { let flags = disable_and_store(); let tid = inner.current.as_mut().unwrap().0; - let thread_info = inner.pool.threads[tid].as_mut().expect("thread not existed when yielding"); - //let thread_info = inner.pool.get_thread_info(tid); + let thread_info = inner.pool.threads[tid] + .as_mut() + .expect("thread not existed when yielding"); + //let thread_info = inner.pool.get_thread_info(tid); thread_info.status = Status::Sleeping; - inner.current + inner + .current .as_mut() .unwrap() .1 diff --git a/os/src/process/scheduler.rs b/os/src/process/scheduler.rs index 634dd92..f8c25ef 100644 --- a/os/src/process/scheduler.rs +++ b/os/src/process/scheduler.rs @@ -8,7 +8,6 @@ pub trait Scheduler { fn exit(&mut self, tid: Tid); } - #[derive(Default)] struct RRInfo { valid: bool, @@ -30,19 +29,17 @@ impl RRScheduler { max_time: max_time_slice, current: 0, }; - rr.threads.push( - RRInfo { - valid: false, - time: 0, - prev: 0, - next: 0, - } - ); + rr.threads.push(RRInfo { + valid: false, + time: 0, + prev: 0, + next: 0, + }); rr } } impl Scheduler for RRScheduler { - fn push(&mut self, tid : Tid) { + fn push(&mut self, tid: Tid) { let tid = tid + 1; if tid + 1 > self.threads.len() { self.threads.resize_with(tid + 1, Default::default); @@ -71,27 +68,27 @@ impl Scheduler for RRScheduler { self.threads[ret].next = 0; self.threads[ret].valid = false; self.current = ret; - Some(ret-1) - }else{ + Some(ret - 1) + } else { None } } // 当前线程的可用时间片 -= 1 - fn tick(&mut self) -> bool{ + fn tick(&mut self) -> bool { let tid = self.current; if tid != 0 { self.threads[tid].time -= 1; if self.threads[tid].time == 0 { return true; - }else{ + } else { return false; } } return true; } - fn exit(&mut self, tid : Tid) { + fn exit(&mut self, tid: Tid) { let tid = tid + 1; if self.current == tid { self.current = 0; diff --git a/os/src/process/structs.rs b/os/src/process/structs.rs index 716a675..fbb25aa 100644 --- a/os/src/process/structs.rs +++ b/os/src/process/structs.rs @@ -1,24 +1,16 @@ -use crate::context::Context; -use crate::alloc::alloc::{ - alloc, - dealloc, - Layout, -}; +use super::{ExitCode, Tid}; +use crate::alloc::alloc::{alloc, dealloc, Layout}; use crate::consts::*; -use riscv::register::satp; +use crate::context::Context; +use crate::memory::memory_set::{attr::MemoryAttr, handler::ByFrame, MemorySet}; use alloc::boxed::Box; -use super::{ Tid, ExitCode }; +use core::str; +use riscv::register::satp; use xmas_elf::{ header, - program::{ Flags, SegmentData, Type }, + program::{Flags, SegmentData, Type}, ElfFile, }; -use crate::memory::memory_set::{ - MemorySet, - handler::ByFrame, - attr::MemoryAttr, -}; -use core::str; #[derive(Clone)] pub enum Status { @@ -31,7 +23,7 @@ pub enum Status { pub struct Thread { pub context: Context, pub kstack: KernelStack, - pub wait: Option, + pub wait: Option, } impl Thread { @@ -41,41 +33,41 @@ impl Thread { } } - pub fn new_kernel(entry: usize) -> Box { + pub fn new_kernel(entry: usize) -> Box { unsafe { let kstack_ = KernelStack::new(); Box::new(Thread { context: Context::new_kernel_thread(entry, kstack_.top(), satp::read().bits()), kstack: kstack_, - wait: None + wait: None, }) } } - - pub fn get_boot_thread() -> Box { + + pub fn get_boot_thread() -> Box { Box::new(Thread { context: Context::null(), kstack: KernelStack::new_empty(), - wait: None + wait: None, }) } pub fn append_initial_arguments(&self, args: [usize; 3]) { unsafe { self.context.append_initial_arguments(args); - } + } } - - pub unsafe fn new_user(data: &[u8], wait_thread: Option) -> Box { + + pub unsafe fn new_user(data: &[u8], wait_thread: Option) -> Box { let elf = ElfFile::new(data).expect("failed to analyse elf!"); match elf.header.pt2.type_().as_type() { header::Type::Executable => { // println!("it really a executable!"); - }, + } header::Type::SharedObject => { panic!("shared object is not supported!"); - }, + } _ => { panic!("unsupported elf type!"); } @@ -84,7 +76,8 @@ impl Thread { let mut vm = elf.make_memory_set(); let mut ustack_top = { - let (ustack_bottom, ustack_top) = (USER_STACK_OFFSET, USER_STACK_OFFSET + USER_STACK_SIZE); + let (ustack_bottom, ustack_top) = + (USER_STACK_OFFSET, USER_STACK_OFFSET + USER_STACK_SIZE); vm.push( ustack_bottom, ustack_top, @@ -97,13 +90,11 @@ impl Thread { let kstack = KernelStack::new(); - Box::new( - Thread { - context: Context::new_user_thread(entry_addr, ustack_top, kstack.top(), vm.token()), - kstack: kstack, - wait: wait_thread - } - ) + Box::new(Thread { + context: Context::new_user_thread(entry_addr, ustack_top, kstack.top(), vm.token()), + kstack: kstack, + wait: wait_thread, + }) } } @@ -132,9 +123,7 @@ impl Drop for KernelStack { Layout::from_size_align(KERNEL_STACK_SIZE, KERNEL_STACK_SIZE).unwrap(), ); } - } - } } diff --git a/os/src/process/thread_pool.rs b/os/src/process/thread_pool.rs index 4a674ad..ade6652 100644 --- a/os/src/process/thread_pool.rs +++ b/os/src/process/thread_pool.rs @@ -1,9 +1,6 @@ +use crate::alloc::{boxed::Box, vec::Vec}; use crate::process::scheduler::Scheduler; use crate::process::structs::*; -use crate::alloc::{ - vec::Vec, - boxed::Box, -}; use crate::process::Tid; pub struct ThreadInfo { @@ -38,12 +35,10 @@ impl ThreadPool { pub fn add(&mut self, _thread: Box) { let tid = self.alloc_tid(); - self.threads[tid] = Some( - ThreadInfo { - status: Status::Ready, - thread: Some(_thread), - } - ); + self.threads[tid] = Some(ThreadInfo { + status: Status::Ready, + thread: Some(_thread), + }); self.scheduler.push(tid); } @@ -52,8 +47,7 @@ impl ThreadPool { let mut thread_info = self.threads[tid].as_mut().expect("thread not exist!"); thread_info.status = Status::Running(tid); return Some((tid, thread_info.thread.take().expect("thread not exist!"))); - } - else { + } else { return None; } } @@ -62,7 +56,7 @@ impl ThreadPool { if self.threads[tid].is_none() { return; } - let mut thread_info = self.threads[tid].as_mut().expect("thread not exist!"); + let mut thread_info = self.threads[tid].as_mut().expect("thread not exist!"); thread_info.thread = Some(thread); if let Status::Running(_) = thread_info.status { thread_info.status = Status::Ready; @@ -80,10 +74,11 @@ impl ThreadPool { self.scheduler.exit(tid); } - pub fn wakeup(&mut self, tid: Tid) { - let proc = self.threads[tid].as_mut().expect("thread not exist when waking up"); + pub fn wakeup(&mut self, tid: Tid) { + let proc = self.threads[tid] + .as_mut() + .expect("thread not exist when waking up"); proc.status = Status::Ready; self.scheduler.push(tid); } - } diff --git a/os/src/sync/condvar.rs b/os/src/sync/condvar.rs index 7eb7a2f..dc25b8a 100644 --- a/os/src/sync/condvar.rs +++ b/os/src/sync/condvar.rs @@ -1,6 +1,6 @@ -use spin::Mutex; +use crate::process::{current_tid, wake_up, yield_now, Tid}; use alloc::collections::VecDeque; -use crate::process::{ Tid, current_tid, yield_now, wake_up }; +use spin::Mutex; #[derive(Default)] pub struct Condvar { @@ -13,9 +13,7 @@ impl Condvar { } pub fn wait(&self) { - self.wait_queue - .lock() - .push_back(current_tid()); + self.wait_queue.lock().push_back(current_tid()); yield_now(); } @@ -24,6 +22,6 @@ impl Condvar { if let Some(tid) = tid { wake_up(tid); } - /* yield_now(); */ + /* yield_now(); */ } } diff --git a/os/src/syscall.rs b/os/src/syscall.rs index 7f9d3da..97a8ac0 100644 --- a/os/src/syscall.rs +++ b/os/src/syscall.rs @@ -11,20 +11,16 @@ pub fn syscall(id: usize, args: [usize; 3], tf: &mut TrapFrame) -> isize { SYS_WRITE => { print!("{}", args[0] as u8 as char); 0 - }, + } SYS_EXIT => { sys_exit(args[0]); 0 - }, - SYS_READ => { - sys_read(args[0], args[1] as *mut u8, args[2]) - }, - SYS_EXEC => { - sys_exec(args[0] as *const u8) - }, + } + SYS_READ => sys_read(args[0], args[1] as *mut u8, args[2]), + SYS_EXEC => sys_exec(args[0] as *const u8), _ => { panic!("unknown syscall id {}", id); - }, + } } } @@ -40,13 +36,15 @@ fn sys_read(fd: usize, base: *mut u8, len: usize) -> isize { } pub unsafe fn from_cstr(s: *const u8) -> &'static str { - use core::{ slice, str }; + use core::{slice, str}; let len = (0usize..).find(|&i| *s.add(i) == 0).unwrap(); str::from_utf8(slice::from_raw_parts(s, len)).unwrap() } fn sys_exec(path: *const u8) -> isize { let valid = process::execute(unsafe { from_cstr(path) }, Some(process::current_tid())); - if valid { process::yield_now(); } + if valid { + process::yield_now(); + } return 0; } diff --git a/os/src/timer.rs b/os/src/timer.rs index 748d52f..8a0859d 100644 --- a/os/src/timer.rs +++ b/os/src/timer.rs @@ -1,8 +1,5 @@ use crate::sbi::set_timer; -use riscv::register::{ - time, - sie -}; +use riscv::register::{sie, time}; pub static mut TICKS: usize = 0; diff --git a/usr/rust/src/bin/notebook.rs b/usr/rust/src/bin/notebook.rs index 694e8d4..7ff4f80 100644 --- a/usr/rust/src/bin/notebook.rs +++ b/usr/rust/src/bin/notebook.rs @@ -19,7 +19,7 @@ pub fn main() { print!("{}", LF as char); print!("{}", CR as char) } - _ => print!("{}", c as char) + _ => print!("{}", c as char), } } } diff --git a/usr/rust/src/bin/user_shell.rs b/usr/rust/src/bin/user_shell.rs index 34c70c2..7c565d7 100644 --- a/usr/rust/src/bin/user_shell.rs +++ b/usr/rust/src/bin/user_shell.rs @@ -9,32 +9,31 @@ extern crate user; const LF: u8 = 0x0au8; const CR: u8 = 0x0du8; +use alloc::string::String; use user::io::getc; use user::syscall::sys_exec; -use alloc::string::String; #[no_mangle] pub fn main() { - println!("Rust user shell"); - let mut line: String = String::new(); - print!(">> "); - loop { - let c = getc(); - match c { - LF | CR => { - println!(""); - if !line.is_empty() { - println!("searching for program {}", line); - sys_exec(line.as_ptr()); - line.clear(); - } - print!(">> "); - }, - _ => { - - print!("{}", c as char); - line.push(c as char); - } - } - } + println!("Rust user shell"); + let mut line: String = String::new(); + print!(">> "); + loop { + let c = getc(); + match c { + LF | CR => { + println!(""); + if !line.is_empty() { + println!("searching for program {}", line); + sys_exec(line.as_ptr()); + line.clear(); + } + print!(">> "); + } + _ => { + print!("{}", c as char); + line.push(c as char); + } + } + } } diff --git a/usr/rust/src/io.rs b/usr/rust/src/io.rs index d561a6a..f709a62 100644 --- a/usr/rust/src/io.rs +++ b/usr/rust/src/io.rs @@ -1,6 +1,6 @@ +use crate::syscall::sys_read; use crate::syscall::sys_write; use core::fmt::{self, Write}; -use crate::syscall::sys_read; pub fn putchar(ch: char) { sys_write(ch as u8); diff --git a/usr/rust/src/lang_items.rs b/usr/rust/src/lang_items.rs index c329d94..49e941f 100644 --- a/usr/rust/src/lang_items.rs +++ b/usr/rust/src/lang_items.rs @@ -1,6 +1,6 @@ -use core::panic::PanicInfo; use crate::syscall::sys_exit; use core::alloc::Layout; +use core::panic::PanicInfo; #[linkage = "weak"] #[no_mangle] @@ -14,7 +14,9 @@ fn init_heap() { const HEAP_SIZE: usize = 0x1000; static mut HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE]; unsafe { - DYNAMIC_ALLOCATOR.lock().init(HEAP.as_ptr() as usize, HEAP_SIZE); + DYNAMIC_ALLOCATOR + .lock() + .init(HEAP.as_ptr() as usize, HEAP_SIZE); } } @@ -38,7 +40,7 @@ pub extern "C" fn _start(_args: isize, _argv: *const u8) -> ! { } #[no_mangle] -pub extern fn abort() { +pub extern "C" fn abort() { panic!("abort"); } diff --git a/usr/rust/src/lib.rs b/usr/rust/src/lib.rs index 5cd40a0..603199b 100644 --- a/usr/rust/src/lib.rs +++ b/usr/rust/src/lib.rs @@ -9,8 +9,8 @@ extern crate alloc; #[macro_use] pub mod io; -pub mod syscall; pub mod lang_items; +pub mod syscall; use buddy_system_allocator::LockedHeap; diff --git a/usr/rust/src/syscall.rs b/usr/rust/src/syscall.rs index 51762ea..afadc6d 100644 --- a/usr/rust/src/syscall.rs +++ b/usr/rust/src/syscall.rs @@ -1,18 +1,12 @@ enum SyscallId { - Read = 63, + Read = 63, Write = 64, Exit = 93, - Exec = 221, + Exec = 221, } #[inline(always)] -fn sys_call( - syscall_id: SyscallId, - arg0: usize, - arg1: usize, - arg2: usize, - arg3: usize, -) -> i64 { +fn sys_call(syscall_id: SyscallId, arg0: usize, arg1: usize, arg2: usize, arg3: usize) -> i64 { let id = syscall_id as usize; let mut ret: i64; unsafe {