From 8bafab99b83a5ded041d6bd7640a4cd1dbef27f5 Mon Sep 17 00:00:00 2001 From: buhe Date: Sat, 9 Oct 2021 20:07:13 +0800 Subject: [PATCH] format code --- os/build.rs | 20 ++++++++++++++------ os/src/main.rs | 5 +++-- os/src/scall_sbi/fs.rs | 4 ++-- os/src/task/mod.rs | 27 ++++++++++++++++----------- os/src/trap/mod.rs | 9 ++++++--- os/src/trap/trap_ctx.rs | 14 +++++++------- 6 files changed, 48 insertions(+), 31 deletions(-) diff --git a/os/build.rs b/os/build.rs index 07a25c9..905e21e 100644 --- a/os/build.rs +++ b/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,14 +40,18 @@ _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 .align 3 app_{0}_start: .incbin "{2}{1}" -app_{0}_end:"#, idx, app, TARGET_PATH)?; +app_{0}_end:"#, + idx, app, TARGET_PATH + )?; } Ok(()) -} \ No newline at end of file +} diff --git a/os/src/main.rs b/os/src/main.rs index 25a9246..655463b 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -50,11 +50,12 @@ extern "C" fn rust_main(hartid: usize, device_tree_paddr: usize) -> ! { const DEVICE_TREE_MAGIC: u32 = 0xd00dfeed; assert_eq!(magic, DEVICE_TREE_MAGIC); let size = u32::from_be(header.be_size); - let _dtb_data = unsafe { core::slice::from_raw_parts(device_tree_paddr as *const u8, size as usize) }; + let _dtb_data = + unsafe { core::slice::from_raw_parts(device_tree_paddr as *const u8, size as usize) }; // let dt = DeviceTree::load(dtb_data).expect("failed to parse device tree"); // DeviceTree::load is not adpator k210 println!("dt size is {:#?}", size); - + clear_bss(); heap::init(); mmu::init(); diff --git a/os/src/scall_sbi/fs.rs b/os/src/scall_sbi/fs.rs index c033336..105b2e9 100644 --- a/os/src/scall_sbi/fs.rs +++ b/os/src/scall_sbi/fs.rs @@ -3,7 +3,7 @@ use crate::{mmu::translated_byte_buffer, task::current_user_token}; const FD_STDOUT: usize = 1; pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize { - match fd { + match fd { FD_STDOUT => { // 改为使用虚拟内存 let buffers = translated_byte_buffer(current_user_token(), buf, len); @@ -11,7 +11,7 @@ pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize { print!("{}", core::str::from_utf8(buffer).unwrap()); } len as isize - }, + } _ => { panic!("Unsupported fd in sys_write!"); } diff --git a/os/src/task/mod.rs b/os/src/task/mod.rs index 63f714d..83aaac5 100644 --- a/os/src/task/mod.rs +++ b/os/src/task/mod.rs @@ -1,4 +1,8 @@ -use crate::{config::{TRAP_CONTEXT, kernel_stack_position}, mmu::{KERNEL_SPACE, MemorySet, PhysPageNum, VirtAddr, MapPermission}, trap::{TrapContext, trap_handler, trap_return}}; +use crate::{ + config::{kernel_stack_position, TRAP_CONTEXT}, + mmu::{MapPermission, MemorySet, PhysPageNum, VirtAddr, KERNEL_SPACE}, + trap::{trap_handler, trap_return, TrapContext}, +}; use core::{cell::RefCell, usize}; use lazy_static::*; @@ -38,13 +42,11 @@ impl AppManagerInner { // 内核栈单纯用于内核的函数调用 // map a kernel-stack in kernel space let (kernel_stack_bottom, kernel_stack_top) = kernel_stack_position(0); - KERNEL_SPACE - .lock() - .insert_framed_area( - kernel_stack_bottom.into(), - kernel_stack_top.into(), - MapPermission::R | MapPermission::W, - ); + KERNEL_SPACE.lock().insert_framed_area( + kernel_stack_bottom.into(), + kernel_stack_top.into(), + MapPermission::R | MapPermission::W, + ); // 获取 trap context 的指针并赋值 let trap_cx = self.trap_cx_ppn.get_mut(); *trap_cx = TrapContext::app_init_context( @@ -69,7 +71,11 @@ lazy_static! { let app_start_raw: &[usize] = unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) }; app_start[..=num_app].copy_from_slice(app_start_raw); - AppManagerInner { app_start,token: 0, trap_cx_ppn: PhysPageNum(0) } + AppManagerInner { + app_start, + token: 0, + trap_cx_ppn: PhysPageNum(0), + } }), }; } @@ -91,11 +97,10 @@ pub fn run() -> ! { trap_return(); } - pub fn current_user_token() -> usize { APP_MANAGER.inner.borrow().token } pub fn current_trap_cx() -> &'static mut TrapContext { APP_MANAGER.inner.borrow().trap_cx_ppn.get_mut() -} \ No newline at end of file +} diff --git a/os/src/trap/mod.rs b/os/src/trap/mod.rs index 4c3845b..cc9d8b4 100644 --- a/os/src/trap/mod.rs +++ b/os/src/trap/mod.rs @@ -5,12 +5,15 @@ use riscv::register::{ }; pub use trap_ctx::TrapContext; -use crate::{config::{TRAMPOLINE, TRAP_CONTEXT}, scall_sbi::syscall, task::{current_trap_cx, current_user_token}}; +use crate::{ + config::{TRAMPOLINE, TRAP_CONTEXT}, + scall_sbi::syscall, + task::{current_trap_cx, current_user_token}, +}; mod trap_ctx; global_asm!(include_str!("trap.asm")); - pub fn init() { set_kernel_trap_entry(); } @@ -37,7 +40,7 @@ pub fn trap_return() -> ! { fn __restore(); } let restore_va = __restore as usize - __alltraps as usize + TRAMPOLINE; - unsafe { + unsafe { asm!( "fence.i", "jr {restore_va}", diff --git a/os/src/trap/trap_ctx.rs b/os/src/trap/trap_ctx.rs index 4c5e82b..896e160 100644 --- a/os/src/trap/trap_ctx.rs +++ b/os/src/trap/trap_ctx.rs @@ -5,7 +5,7 @@ pub struct TrapContext { pub x: [usize; 32], pub sstatus: Sstatus, pub sepc: usize, - pub kernel_satp: usize, + pub kernel_satp: usize, pub kernel_sp: usize, pub trap_handler: usize, } @@ -14,7 +14,7 @@ impl TrapContext { pub fn set_sp(&mut self, sp: usize) { self.x[2] = sp; } - pub fn app_init_context( + pub fn app_init_context( entry: usize, sp: usize, kernel_satp: usize, @@ -25,11 +25,11 @@ impl TrapContext { sstatus.set_spp(SPP::User); let mut cx = Self { x: [0; 32], - sstatus,//33 - sepc: entry,//34 - kernel_satp,//35 - kernel_sp,//36 - trap_handler,//37 + sstatus, //33 + sepc: entry, //34 + kernel_satp, //35 + kernel_sp, //36 + trap_handler, //37 }; cx.set_sp(sp); cx