Skip to content

Commit

Permalink
run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
刘丰源 committed Feb 4, 2020
1 parent 8a7f4ae commit e9df799
Show file tree
Hide file tree
Showing 31 changed files with 383 additions and 461 deletions.
8 changes: 6 additions & 2 deletions os/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
60 changes: 21 additions & 39 deletions os/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
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)]
pub struct Context {
pub content_addr: usize,
}


impl Context {
#[naked]
#[inline(never)]
pub unsafe extern "C" fn switch(&mut self, target: &mut 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)
}

Expand All @@ -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)
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
}
}
}


15 changes: 5 additions & 10 deletions os/src/fs/device.rs
Original file line number Diff line number Diff line change
@@ -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<usize> {
let slice = self.0.read();
Expand Down
5 changes: 2 additions & 3 deletions os/src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -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<dyn INode> = {
Expand All @@ -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()

};
}

Expand Down
12 changes: 4 additions & 8 deletions os/src/fs/stdio.rs
Original file line number Diff line number Diff line change
@@ -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<VecDeque<char>>,
Expand All @@ -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();

}
}
}
Expand Down
12 changes: 4 additions & 8 deletions os/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() -> ! {
Expand All @@ -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 {}
}

65 changes: 27 additions & 38 deletions os/src/interrupt.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
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"));

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! ++++");
}
Expand All @@ -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!"),
}
}

Expand All @@ -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;
}

Expand All @@ -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,
}
}

Expand Down
Loading

0 comments on commit e9df799

Please sign in to comment.