fix: format code
This commit is contained in:
		| @@ -3,6 +3,7 @@ use core::arch::asm; | ||||
| use lazy_static::lazy_static; | ||||
|  | ||||
| use crate::log_information; | ||||
| use crate::sbi::shutdown; | ||||
| use crate::sync::single_cell::SingleCell; | ||||
| use crate::trap::context::TrapContext; | ||||
|  | ||||
| @@ -26,14 +27,13 @@ lazy_static! { | ||||
|             let num_app_ptr = _num_app as usize as *const usize; | ||||
|             let num_app = num_app_ptr.read_volatile(); | ||||
|             let mut start_addresses: [usize; MAX_APP_NUMBER + 1] = [0; MAX_APP_NUMBER + 1]; | ||||
|             let start_addresses_raw: &[usize] = core::slice::from_raw_parts( | ||||
|                 num_app_ptr.add(1), num_app + 1 | ||||
|             ); | ||||
|             let start_addresses_raw: &[usize] = | ||||
|                 core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1); | ||||
|             start_addresses[..=num_app].copy_from_slice(start_addresses_raw); | ||||
|             AppManager { | ||||
|                 app_count: num_app, | ||||
|                 current: 0, | ||||
|                 start_addresses | ||||
|                 start_addresses, | ||||
|             } | ||||
|         }) | ||||
|     }; | ||||
| @@ -44,10 +44,12 @@ impl AppManager { | ||||
|         log_information!("[kernel] Application count is {}.", self.app_count); | ||||
|  | ||||
|         for i in 0..self.app_count { | ||||
|             log_information!("[kernel] Application_{} ({:#x} -> {:#x})", | ||||
|             log_information!( | ||||
|                 "[kernel] Application_{} ({:#x} -> {:#x})", | ||||
|                 i, | ||||
|                 self.start_addresses[i], | ||||
|                      self.start_addresses[i + 1]); | ||||
|                 self.start_addresses[i + 1] | ||||
|             ); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -66,19 +68,14 @@ impl AppManager { | ||||
|  | ||||
|         log_information!("[kernel] Loading application_{}...", app_id); | ||||
|  | ||||
|         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_source = core::slice::from_raw_parts( | ||||
|             self.start_addresses[app_id] as *const u8, | ||||
|             self.start_addresses[app_id + 1] - self.start_addresses[app_id] | ||||
|         ); | ||||
|         let app_destination = core::slice::from_raw_parts_mut( | ||||
|             APP_BASE_ADDRESS as *mut u8, | ||||
|             app_source.len() | ||||
|             self.start_addresses[app_id + 1] - self.start_addresses[app_id], | ||||
|         ); | ||||
|         let app_destination = | ||||
|             core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, app_source.len()); | ||||
|         app_destination.copy_from_slice(app_source); | ||||
|  | ||||
|         // 保证指令缓存的更新 | ||||
| @@ -92,7 +89,7 @@ const KERNEL_STACK_SIZE: usize = 4096 * 2; | ||||
|  | ||||
| #[repr(align(4096))] | ||||
| struct UserStack { | ||||
|     data: [u8; USER_STACK_SIZE] | ||||
|     data: [u8; USER_STACK_SIZE], | ||||
| } | ||||
|  | ||||
| impl UserStack { | ||||
| @@ -103,7 +100,7 @@ impl UserStack { | ||||
|  | ||||
| #[repr(align(4096))] | ||||
| struct KernelStack { | ||||
|     data: [u8; KERNEL_STACK_SIZE] | ||||
|     data: [u8; KERNEL_STACK_SIZE], | ||||
| } | ||||
|  | ||||
| impl KernelStack { | ||||
| @@ -120,8 +117,12 @@ impl KernelStack { | ||||
|     } | ||||
| } | ||||
|  | ||||
| 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], | ||||
| }; | ||||
|  | ||||
| pub fn print_app_information() { | ||||
|     let app_manager = APP_MANAGER.exclusive_borrow(); | ||||
| @@ -131,6 +132,11 @@ pub fn print_app_information() { | ||||
| pub fn run_next_application() -> ! { | ||||
|     let mut app_manager = APP_MANAGER.exclusive_borrow(); | ||||
|     let current_app = app_manager.get_current_app(); | ||||
|     if current_app >= app_manager.app_count { | ||||
|         log_information!("Run out of applications, the os will stop!"); | ||||
|         shutdown(true); | ||||
|     } | ||||
|  | ||||
|     unsafe { | ||||
|         app_manager.load_app(current_app); | ||||
|     } | ||||
| @@ -147,12 +153,10 @@ pub fn run_next_application() -> ! { | ||||
|     unsafe { | ||||
|         let context_address = KERNEL_STACK.push_context(TrapContext::init_application_context( | ||||
|             APP_BASE_ADDRESS, | ||||
|             USER_STACK.get_sp() | ||||
|             USER_STACK.get_sp(), | ||||
|         )) as *const _ as usize; | ||||
|         __restore(context_address); | ||||
|  | ||||
|         unreachable!() | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| use crate::sbi::console_print; | ||||
| use core::fmt; | ||||
| use core::fmt::Write; | ||||
| use crate::sbi::console_print; | ||||
|  | ||||
| struct Stdout; | ||||
|  | ||||
|   | ||||
| @@ -1,21 +1,20 @@ | ||||
| #![no_std] | ||||
| #![no_main] | ||||
|  | ||||
| use core::arch::global_asm; | ||||
| use crate::utils::clear_bss; | ||||
| use core::arch::global_asm; | ||||
|  | ||||
| mod utils; | ||||
| mod sbi; | ||||
| mod console; | ||||
| mod batch; | ||||
| mod console; | ||||
| mod sbi; | ||||
| mod sync; | ||||
| mod trap; | ||||
| mod syscall; | ||||
| mod trap; | ||||
| mod utils; | ||||
|  | ||||
| global_asm!(include_str!("entry.asm")); | ||||
| global_asm!(include_str!("link_app.asm")); | ||||
|  | ||||
|  | ||||
| #[no_mangle] | ||||
| fn rust_main() -> ! { | ||||
|     clear_bss(); | ||||
| @@ -24,4 +23,3 @@ fn rust_main() -> ! { | ||||
|     batch::print_app_information(); | ||||
|     batch::run_next_application(); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -4,7 +4,7 @@ pub fn console_print(c: usize) { | ||||
| } | ||||
|  | ||||
| pub fn shutdown(failure: bool) -> ! { | ||||
|     use sbi_rt::{system_reset, Shutdown, NoReason, SystemFailure}; | ||||
|     use sbi_rt::{system_reset, NoReason, Shutdown, SystemFailure}; | ||||
|  | ||||
|     if failure { | ||||
|         system_reset(Shutdown, SystemFailure); | ||||
|   | ||||
| @@ -2,17 +2,15 @@ use core::cell::{RefCell, RefMut}; | ||||
|  | ||||
| /// 只能在单核场景下保持安全的屑RefCell | ||||
| pub struct SingleCell<T> { | ||||
|     inner: RefCell<T> | ||||
|     inner: RefCell<T>, | ||||
| } | ||||
|  | ||||
| unsafe impl<T> Sync for SingleCell<T> { | ||||
|  | ||||
| } | ||||
| unsafe impl<T> Sync for SingleCell<T> {} | ||||
|  | ||||
| impl<T> SingleCell<T> { | ||||
|     pub unsafe fn new(value: T) -> SingleCell<T> { | ||||
|         SingleCell { | ||||
|             inner: RefCell::new(value) | ||||
|             inner: RefCell::new(value), | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -38,8 +38,6 @@ __alltraps: | ||||
|     mv a0, sp | ||||
|     call trap_handler | ||||
|  | ||||
|  | ||||
|  | ||||
| __restore: | ||||
|     # case1: start running app by __restore | ||||
|     # case2: back to U after handling trap | ||||
|   | ||||
| @@ -1,20 +1,22 @@ | ||||
| use core::arch::global_asm; | ||||
| use riscv::register::{ | ||||
|     mtvec::TrapMode, | ||||
|     scause::{self, Exception, Trap}, | ||||
|     stval, stvec | ||||
| }; | ||||
| use crate::batch::run_next_application; | ||||
| use crate::println; | ||||
| use crate::syscall::syscall; | ||||
| use crate::trap::context::TrapContext; | ||||
| use core::arch::global_asm; | ||||
| use riscv::register::{ | ||||
|     mtvec::TrapMode, | ||||
|     scause::{self, Exception, Trap}, | ||||
|     stval, stvec, | ||||
| }; | ||||
|  | ||||
| pub mod context; | ||||
|  | ||||
| global_asm!(include_str!("trap.asm")); | ||||
|  | ||||
| pub fn init() { | ||||
|     extern "C" { fn __alltraps(); } | ||||
|     extern "C" { | ||||
|         fn __alltraps(); | ||||
|     } | ||||
|     unsafe { | ||||
|         stvec::write(__alltraps as usize, TrapMode::Direct); | ||||
|     } | ||||
| @@ -29,17 +31,21 @@ pub fn trap_handler(context: &mut TrapContext) -> &mut TrapContext { | ||||
|         Trap::Exception(Exception::UserEnvCall) => { | ||||
|             context.sepc += 4; | ||||
|             context.x[10] = syscall(context.x[17], [context.x[10], context.x[11], context.x[12]]); | ||||
|         }, | ||||
|         } | ||||
|         Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => { | ||||
|             println!("[kernel] PageFault in application, kernel will kill it."); | ||||
|             run_next_application(); | ||||
|         }, | ||||
|         } | ||||
|         Trap::Exception(Exception::IllegalInstruction) => { | ||||
|             println!("[kernel] Illegal instruction in application, kernel will kill it."); | ||||
|             run_next_application(); | ||||
|         }, | ||||
|         } | ||||
|         _ => { | ||||
|             panic!("[Kernel] Unsupported trap: {:?}, stval = {:#x}!", scause.cause(), stval); | ||||
|             panic!( | ||||
|                 "[Kernel] Unsupported trap: {:?}, stval = {:#x}!", | ||||
|                 scause.cause(), | ||||
|                 stval | ||||
|             ); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -4,7 +4,7 @@ use riscv::register::sstatus::{self, Sstatus, SPP}; | ||||
| pub struct TrapContext { | ||||
|     pub x: [usize; 32], | ||||
|     pub s_status: Sstatus, | ||||
|     pub sepc: usize | ||||
|     pub sepc: usize, | ||||
| } | ||||
|  | ||||
| impl TrapContext { | ||||
| @@ -14,7 +14,7 @@ impl TrapContext { | ||||
|         let mut context = Self { | ||||
|             x: [0; 32], | ||||
|             s_status: sstatus, | ||||
|             sepc: entry | ||||
|             sepc: entry, | ||||
|         }; | ||||
|  | ||||
|         context.set_sp(sp); | ||||
| @@ -26,4 +26,3 @@ impl TrapContext { | ||||
|         self.x[2] = sp; | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -1,17 +1,19 @@ | ||||
| use core::panic::PanicInfo; | ||||
| use crate::println; | ||||
|  | ||||
| use crate::log_error; | ||||
| use crate::sbi::shutdown; | ||||
|  | ||||
| #[panic_handler] | ||||
| fn panic(info: &PanicInfo) -> ! { | ||||
|     if let Some(location) = info.location() { | ||||
|         println!("Panicked at {}:{} {}.", | ||||
|         log_error!( | ||||
|             "Panicked at {}:{} {}.", | ||||
|             location.file(), | ||||
|             location.line(), | ||||
|             info.message() | ||||
|         ); | ||||
|     } else { | ||||
|         println!("Panicked: {}.", info.message()); | ||||
|         log_error!("Panicked: {}.", info.message()); | ||||
|     } | ||||
|  | ||||
|     shutdown(true) | ||||
| @@ -24,8 +26,6 @@ pub fn clear_bss() { | ||||
|     } | ||||
|  | ||||
|     for m in sbss as usize..ebss as usize { | ||||
|         unsafe { | ||||
|             (m as *mut u8).write_volatile(0) | ||||
|         } | ||||
|         unsafe { (m as *mut u8).write_volatile(0) } | ||||
|     } | ||||
| } | ||||
| @@ -1,5 +1,5 @@ | ||||
| use core::fmt::Write; | ||||
| use crate::syscall::sys_write; | ||||
| use core::fmt::Write; | ||||
|  | ||||
| struct Stdout; | ||||
|  | ||||
|   | ||||
| @@ -3,8 +3,8 @@ | ||||
|  | ||||
| use crate::syscall::{sys_exit, sys_write}; | ||||
|  | ||||
| mod utils; | ||||
| mod syscall; | ||||
| mod utils; | ||||
| #[macro_use] | ||||
| pub mod console; | ||||
|  | ||||
| @@ -28,4 +28,3 @@ pub fn write(fd: usize, buf: &[u8]) -> isize { | ||||
| pub fn exit(exit_code: i32) -> isize { | ||||
|     sys_exit(exit_code) | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -1,5 +1,5 @@ | ||||
| use core::panic::PanicInfo; | ||||
| use crate::println; | ||||
| use core::panic::PanicInfo; | ||||
|  | ||||
| pub fn clear_bss() { | ||||
|     extern "C" { | ||||
| @@ -9,9 +9,7 @@ pub fn clear_bss() { | ||||
|     } | ||||
|  | ||||
|     for i in start_bss as usize..end_bss as usize { | ||||
|         unsafe { | ||||
|             (i as *mut u8).write_volatile(0) | ||||
|         } | ||||
|         unsafe { (i as *mut u8).write_volatile(0) } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -20,7 +18,12 @@ fn panic_handler(info: &PanicInfo) -> ! { | ||||
|     let message = info.message(); | ||||
|  | ||||
|     if let Some(location) = info.location() { | ||||
|         println!("Panicked at {}:{} {}", location.file(), location.line(), message); | ||||
|         println!( | ||||
|             "Panicked at {}:{} {}", | ||||
|             location.file(), | ||||
|             location.line(), | ||||
|             message | ||||
|         ); | ||||
|     } else { | ||||
|         println!("Panicked: {}", message); | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user