mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-22 01:16:26 +04:00
cargo clippy & fmt
This commit is contained in:
parent
581adc9083
commit
c4a5b7fca5
20
os/build.rs
20
os/build.rs
@ -1,5 +1,5 @@
|
||||
use std::fs::{read_dir, File};
|
||||
use std::io::{Result, Write};
|
||||
use std::fs::{File, read_dir};
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=../user/src/");
|
||||
@ -22,12 +22,16 @@ fn insert_app_data() -> Result<()> {
|
||||
.collect();
|
||||
apps.sort();
|
||||
|
||||
writeln!(f, r#"
|
||||
writeln!(
|
||||
f,
|
||||
r#"
|
||||
.align 3
|
||||
.section .data
|
||||
.global _num_app
|
||||
_num_app:
|
||||
.quad {}"#, apps.len())?;
|
||||
.quad {}"#,
|
||||
apps.len()
|
||||
)?;
|
||||
|
||||
for i in 0..apps.len() {
|
||||
writeln!(f, r#" .quad app_{}_start"#, i)?;
|
||||
@ -36,13 +40,17 @@ _num_app:
|
||||
|
||||
for (idx, app) in apps.iter().enumerate() {
|
||||
println!("app_{}: {}", idx, app);
|
||||
writeln!(f, r#"
|
||||
writeln!(
|
||||
f,
|
||||
r#"
|
||||
.section .data
|
||||
.global app_{0}_start
|
||||
.global app_{0}_end
|
||||
app_{0}_start:
|
||||
.incbin "{2}{1}.bin"
|
||||
app_{0}_end:"#, idx, app, TARGET_PATH)?;
|
||||
app_{0}_end:"#,
|
||||
idx, app, TARGET_PATH
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use lazy_static::*;
|
||||
use crate::trap::TrapContext;
|
||||
use crate::sync::UPSafeCell;
|
||||
use crate::trap::TrapContext;
|
||||
use core::arch::asm;
|
||||
use lazy_static::*;
|
||||
|
||||
const USER_STACK_SIZE: usize = 4096 * 2;
|
||||
const KERNEL_STACK_SIZE: usize = 4096 * 2;
|
||||
@ -19,8 +19,12 @@ struct UserStack {
|
||||
data: [u8; USER_STACK_SIZE],
|
||||
}
|
||||
|
||||
static KERNEL_STACK: KernelStack = KernelStack { data: [0; KERNEL_STACK_SIZE] };
|
||||
static USER_STACK: UserStack = UserStack { data: [0; USER_STACK_SIZE] };
|
||||
static KERNEL_STACK: KernelStack = KernelStack {
|
||||
data: [0; KERNEL_STACK_SIZE],
|
||||
};
|
||||
static USER_STACK: UserStack = UserStack {
|
||||
data: [0; USER_STACK_SIZE],
|
||||
};
|
||||
|
||||
impl KernelStack {
|
||||
fn get_sp(&self) -> usize {
|
||||
@ -28,7 +32,9 @@ impl KernelStack {
|
||||
}
|
||||
pub fn push_context(&self, cx: TrapContext) -> &'static mut TrapContext {
|
||||
let cx_ptr = (self.get_sp() - core::mem::size_of::<TrapContext>()) as *mut TrapContext;
|
||||
unsafe { *cx_ptr = cx; }
|
||||
unsafe {
|
||||
*cx_ptr = cx;
|
||||
}
|
||||
unsafe { cx_ptr.as_mut().unwrap() }
|
||||
}
|
||||
}
|
||||
@ -49,7 +55,12 @@ impl AppManager {
|
||||
pub fn print_app_info(&self) {
|
||||
println!("[kernel] num_app = {}", self.num_app);
|
||||
for i in 0..self.num_app {
|
||||
println!("[kernel] app_{} [{:#x}, {:#x})", i, self.app_start[i], self.app_start[i + 1]);
|
||||
println!(
|
||||
"[kernel] app_{} [{:#x}, {:#x})",
|
||||
i,
|
||||
self.app_start[i],
|
||||
self.app_start[i + 1]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,22 +72,18 @@ impl AppManager {
|
||||
// clear icache
|
||||
asm!("fence.i");
|
||||
// clear app area
|
||||
core::slice::from_raw_parts_mut(
|
||||
APP_BASE_ADDRESS as *mut u8,
|
||||
APP_SIZE_LIMIT
|
||||
).fill(0);
|
||||
core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, APP_SIZE_LIMIT).fill(0);
|
||||
let app_src = core::slice::from_raw_parts(
|
||||
self.app_start[app_id] as *const u8,
|
||||
self.app_start[app_id + 1] - self.app_start[app_id]
|
||||
);
|
||||
let app_dst = core::slice::from_raw_parts_mut(
|
||||
APP_BASE_ADDRESS as *mut u8,
|
||||
app_src.len()
|
||||
self.app_start[app_id + 1] - self.app_start[app_id],
|
||||
);
|
||||
let app_dst = core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, app_src.len());
|
||||
app_dst.copy_from_slice(app_src);
|
||||
}
|
||||
|
||||
pub fn get_current_app(&self) -> usize { self.current_app }
|
||||
pub fn get_current_app(&self) -> usize {
|
||||
self.current_app
|
||||
}
|
||||
|
||||
pub fn move_to_next_app(&mut self) {
|
||||
self.current_app += 1;
|
||||
@ -84,21 +91,24 @@ impl AppManager {
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref APP_MANAGER: UPSafeCell<AppManager> = unsafe { UPSafeCell::new({
|
||||
extern "C" { fn _num_app(); }
|
||||
let num_app_ptr = _num_app as usize as *const usize;
|
||||
let num_app = num_app_ptr.read_volatile();
|
||||
let mut app_start: [usize; MAX_APP_NUM + 1] = [0; MAX_APP_NUM + 1];
|
||||
let app_start_raw: &[usize] = core::slice::from_raw_parts(
|
||||
num_app_ptr.add(1), num_app + 1
|
||||
);
|
||||
app_start[..=num_app].copy_from_slice(app_start_raw);
|
||||
AppManager {
|
||||
num_app,
|
||||
current_app: 0,
|
||||
app_start,
|
||||
}
|
||||
})};
|
||||
static ref APP_MANAGER: UPSafeCell<AppManager> = unsafe {
|
||||
UPSafeCell::new({
|
||||
extern "C" {
|
||||
fn _num_app();
|
||||
}
|
||||
let num_app_ptr = _num_app as usize as *const usize;
|
||||
let num_app = num_app_ptr.read_volatile();
|
||||
let mut app_start: [usize; MAX_APP_NUM + 1] = [0; MAX_APP_NUM + 1];
|
||||
let app_start_raw: &[usize] =
|
||||
core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1);
|
||||
app_start[..=num_app].copy_from_slice(app_start_raw);
|
||||
AppManager {
|
||||
num_app,
|
||||
current_app: 0,
|
||||
app_start,
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
@ -119,11 +129,14 @@ pub fn run_next_app() -> ! {
|
||||
drop(app_manager);
|
||||
// before this we have to drop local variables related to resources manually
|
||||
// and release the resources
|
||||
extern "C" { fn __restore(cx_addr: usize); }
|
||||
extern "C" {
|
||||
fn __restore(cx_addr: usize);
|
||||
}
|
||||
unsafe {
|
||||
__restore(KERNEL_STACK.push_context(
|
||||
TrapContext::app_init_context(APP_BASE_ADDRESS, USER_STACK.get_sp())
|
||||
) as *const _ as usize);
|
||||
__restore(KERNEL_STACK.push_context(TrapContext::app_init_context(
|
||||
APP_BASE_ADDRESS,
|
||||
USER_STACK.get_sp(),
|
||||
)) as *const _ as usize);
|
||||
}
|
||||
panic!("Unreachable in batch::run_current_app!");
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use core::fmt::{self, Write};
|
||||
use crate::sbi::console_putchar;
|
||||
use core::fmt::{self, Write};
|
||||
|
||||
struct Stdout;
|
||||
|
||||
@ -29,5 +29,3 @@ macro_rules! println {
|
||||
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,10 +1,15 @@
|
||||
use core::panic::PanicInfo;
|
||||
use crate::sbi::shutdown;
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
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 {
|
||||
println!("[kernel] Panicked: {}", info.message().unwrap());
|
||||
}
|
||||
|
@ -6,12 +6,12 @@ use core::arch::global_asm;
|
||||
|
||||
#[macro_use]
|
||||
mod console;
|
||||
mod batch;
|
||||
mod lang_items;
|
||||
mod sbi;
|
||||
mod sync;
|
||||
mod syscall;
|
||||
mod trap;
|
||||
mod batch;
|
||||
mod sync;
|
||||
|
||||
global_asm!(include_str!("entry.asm"));
|
||||
global_asm!(include_str!("link_app.S"));
|
||||
@ -22,10 +22,8 @@ fn clear_bss() {
|
||||
fn ebss();
|
||||
}
|
||||
unsafe {
|
||||
core::slice::from_raw_parts_mut(
|
||||
sbss as usize as *mut u8,
|
||||
ebss as usize - sbss as usize,
|
||||
).fill(0);
|
||||
core::slice::from_raw_parts_mut(sbss as usize as *mut u8, ebss as usize - sbss as usize)
|
||||
.fill(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,4 +34,4 @@ pub fn rust_main() -> ! {
|
||||
trap::init();
|
||||
batch::init();
|
||||
batch::run_next_app();
|
||||
}
|
||||
}
|
||||
|
@ -39,4 +39,3 @@ pub fn shutdown() -> ! {
|
||||
sbi_call(SBI_SHUTDOWN, 0, 0, 0);
|
||||
panic!("It should shutdown!");
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,3 @@
|
||||
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
|
||||
/// uniprocessor.
|
||||
pub unsafe fn new(value: T) -> Self {
|
||||
Self { inner: RefCell::new(value) }
|
||||
Self {
|
||||
inner: RefCell::new(value),
|
||||
}
|
||||
}
|
||||
/// Panic if the data has been borrowed.
|
||||
pub fn exclusive_access(&self) -> RefMut<'_, T> {
|
||||
self.inner.borrow_mut()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
||||
let str = core::str::from_utf8(slice).unwrap();
|
||||
print!("{}", str);
|
||||
len as isize
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
panic!("Unsupported fd in sys_write!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,4 +14,3 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
|
||||
_ => panic!("Unsupported syscall_id: {}", syscall_id),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,4 +3,4 @@ use crate::batch::run_next_app;
|
||||
pub fn sys_exit(exit_code: i32) -> ! {
|
||||
println!("[kernel] Application exited with code {}", exit_code);
|
||||
run_next_app()
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use riscv::register::sstatus::{Sstatus, self, SPP};
|
||||
use riscv::register::sstatus::{self, Sstatus, SPP};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct TrapContext {
|
||||
@ -8,7 +8,9 @@ pub struct 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(entry: usize, sp: usize) -> Self {
|
||||
let mut sstatus = sstatus::read();
|
||||
sstatus.set_spp(SPP::User);
|
||||
|
@ -1,23 +1,20 @@
|
||||
mod context;
|
||||
|
||||
use crate::batch::run_next_app;
|
||||
use crate::syscall::syscall;
|
||||
use core::arch::global_asm;
|
||||
use riscv::register::{
|
||||
mtvec::TrapMode,
|
||||
stvec,
|
||||
scause::{
|
||||
self,
|
||||
Trap,
|
||||
Exception,
|
||||
},
|
||||
stval,
|
||||
scause::{self, Exception, Trap},
|
||||
stval, stvec,
|
||||
};
|
||||
use crate::syscall::syscall;
|
||||
use crate::batch::run_next_app;
|
||||
use core::arch::global_asm;
|
||||
|
||||
global_asm!(include_str!("trap.S"));
|
||||
|
||||
pub fn init() {
|
||||
extern "C" { fn __alltraps(); }
|
||||
extern "C" {
|
||||
fn __alltraps();
|
||||
}
|
||||
unsafe {
|
||||
stvec::write(__alltraps as usize, TrapMode::Direct);
|
||||
}
|
||||
@ -32,8 +29,7 @@ pub fn trap_handler(cx: &mut TrapContext) -> &mut TrapContext {
|
||||
cx.sepc += 4;
|
||||
cx.x[10] = syscall(cx.x[17], [cx.x[10], cx.x[11], cx.x[12]]) as usize;
|
||||
}
|
||||
Trap::Exception(Exception::StoreFault) |
|
||||
Trap::Exception(Exception::StorePageFault) => {
|
||||
Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => {
|
||||
println!("[kernel] PageFault in application, kernel killed it.");
|
||||
run_next_app();
|
||||
}
|
||||
@ -42,7 +38,11 @@ pub fn trap_handler(cx: &mut TrapContext) -> &mut TrapContext {
|
||||
run_next_app();
|
||||
}
|
||||
_ => {
|
||||
panic!("Unsupported trap {:?}, stval = {:#x}!", scause.cause(), stval);
|
||||
panic!(
|
||||
"Unsupported trap {:?}, stval = {:#x}!",
|
||||
scause.cause(),
|
||||
stval
|
||||
);
|
||||
}
|
||||
}
|
||||
cx
|
||||
|
@ -8,6 +8,8 @@ extern crate user_lib;
|
||||
fn main() -> i32 {
|
||||
println!("Into Test store_fault, we will insert an invalid store operation...");
|
||||
println!("Kernel should kill this application!");
|
||||
unsafe { (0x0 as *mut u8).write_volatile(0); }
|
||||
unsafe {
|
||||
core::ptr::null_mut::<u8>().write_volatile(0);
|
||||
}
|
||||
0
|
||||
}
|
||||
}
|
||||
|
@ -24,4 +24,4 @@ fn main() -> i32 {
|
||||
}
|
||||
println!("Test power OK!");
|
||||
0
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use core::fmt::{self, Write};
|
||||
use super::write;
|
||||
use core::fmt::{self, Write};
|
||||
|
||||
struct Stdout;
|
||||
|
||||
@ -28,4 +28,4 @@ macro_rules! println {
|
||||
($fmt: literal $(, $($arg: tt)+)?) => {
|
||||
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,14 @@
|
||||
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
|
||||
let err = panic_info.message().unwrap();
|
||||
if let Some(location) = panic_info.location() {
|
||||
println!("Panicked at {}:{}, {}", location.file(), location.line(), err);
|
||||
println!(
|
||||
"Panicked at {}:{}, {}",
|
||||
location.file(),
|
||||
location.line(),
|
||||
err
|
||||
);
|
||||
} else {
|
||||
println!("Panicked: {}", err);
|
||||
}
|
||||
loop {}
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,8 @@
|
||||
|
||||
#[macro_use]
|
||||
pub mod console;
|
||||
mod syscall;
|
||||
mod lang_items;
|
||||
mod syscall;
|
||||
|
||||
#[no_mangle]
|
||||
#[link_section = ".text.entry"]
|
||||
@ -26,12 +26,16 @@ fn clear_bss() {
|
||||
fn start_bss();
|
||||
fn end_bss();
|
||||
}
|
||||
(start_bss as usize..end_bss as usize).for_each(|addr| {
|
||||
unsafe { (addr as *mut u8).write_volatile(0); }
|
||||
(start_bss as usize..end_bss as usize).for_each(|addr| unsafe {
|
||||
(addr as *mut u8).write_volatile(0);
|
||||
});
|
||||
}
|
||||
|
||||
use syscall::*;
|
||||
|
||||
pub fn write(fd: usize, buf: &[u8]) -> isize { sys_write(fd, buf) }
|
||||
pub fn exit(exit_code: i32) -> isize { sys_exit(exit_code) }
|
||||
pub fn write(fd: usize, buf: &[u8]) -> isize {
|
||||
sys_write(fd, buf)
|
||||
}
|
||||
pub fn exit(exit_code: i32) -> isize {
|
||||
sys_exit(exit_code)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user