|
| 1 | +//! batch subsystem |
| 2 | +
|
| 3 | +use crate::sync::UPSafeCell; |
| 4 | +use crate::trap::TrapContext; |
| 5 | +use core::arch::asm; |
| 6 | +use lazy_static::*; |
| 7 | + |
| 8 | +const USER_STACK_SIZE: usize = 4096 * 2; |
| 9 | +const KERNEL_STACK_SIZE: usize = 4096 * 2; |
| 10 | +const MAX_APP_NUM: usize = 16; |
| 11 | +const APP_BASE_ADDRESS: usize = 0x80400000; |
| 12 | +const APP_SIZE_LIMIT: usize = 0x20000; |
| 13 | + |
| 14 | +#[repr(align(4096))] |
| 15 | +struct KernelStack { |
| 16 | + data: [u8; KERNEL_STACK_SIZE], |
| 17 | +} |
| 18 | + |
| 19 | +#[repr(align(4096))] |
| 20 | +struct UserStack { |
| 21 | + data: [u8; USER_STACK_SIZE], |
| 22 | +} |
| 23 | + |
| 24 | +static KERNEL_STACK: KernelStack = KernelStack { |
| 25 | + data: [0; KERNEL_STACK_SIZE], |
| 26 | +}; |
| 27 | +static USER_STACK: UserStack = UserStack { |
| 28 | + data: [0; USER_STACK_SIZE], |
| 29 | +}; |
| 30 | + |
| 31 | +impl KernelStack { |
| 32 | + fn get_sp(&self) -> usize { |
| 33 | + self.data.as_ptr() as usize + KERNEL_STACK_SIZE |
| 34 | + } |
| 35 | + pub fn push_context(&self, cx: TrapContext) -> &'static mut TrapContext { |
| 36 | + let cx_ptr = (self.get_sp() - core::mem::size_of::<TrapContext>()) as *mut TrapContext; |
| 37 | + unsafe { |
| 38 | + *cx_ptr = cx; |
| 39 | + } |
| 40 | + unsafe { cx_ptr.as_mut().unwrap() } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl UserStack { |
| 45 | + fn get_sp(&self) -> usize { |
| 46 | + self.data.as_ptr() as usize + USER_STACK_SIZE |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +struct AppManager { |
| 51 | + num_app: usize, |
| 52 | + current_app: usize, |
| 53 | + app_start: [usize; MAX_APP_NUM + 1], |
| 54 | +} |
| 55 | + |
| 56 | +impl AppManager { |
| 57 | + pub fn print_app_info(&self) { |
| 58 | + println!("[kernel] num_app = {}", self.num_app); |
| 59 | + for i in 0..self.num_app { |
| 60 | + println!( |
| 61 | + "[kernel] app_{} [{:#x}, {:#x})", |
| 62 | + i, |
| 63 | + self.app_start[i], |
| 64 | + self.app_start[i + 1] |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + unsafe fn load_app(&self, app_id: usize) { |
| 70 | + if app_id >= self.num_app { |
| 71 | + println!("All applications completed!"); |
| 72 | + use crate::board::QEMUExit; |
| 73 | + crate::board::QEMU_EXIT_HANDLE.exit_success(); |
| 74 | + } |
| 75 | + println!("[kernel] Loading app_{}", app_id); |
| 76 | + // clear app area |
| 77 | + core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, APP_SIZE_LIMIT).fill(0); |
| 78 | + let app_src = core::slice::from_raw_parts( |
| 79 | + self.app_start[app_id] as *const u8, |
| 80 | + self.app_start[app_id + 1] - self.app_start[app_id], |
| 81 | + ); |
| 82 | + let app_dst = core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, app_src.len()); |
| 83 | + app_dst.copy_from_slice(app_src); |
| 84 | + // Memory fence about fetching the instruction memory |
| 85 | + // It is guaranteed that a subsequent instruction fetch must |
| 86 | + // observes all previous writes to the instruction memory. |
| 87 | + // Therefore, fence.i must be executed after we have loaded |
| 88 | + // the code of the next app into the instruction memory. |
| 89 | + // See also: riscv non-priv spec chapter 3, 'Zifencei' extension. |
| 90 | + asm!("fence.i"); |
| 91 | + } |
| 92 | + |
| 93 | + pub fn get_current_app(&self) -> usize { |
| 94 | + self.current_app |
| 95 | + } |
| 96 | + |
| 97 | + pub fn move_to_next_app(&mut self) { |
| 98 | + self.current_app += 1; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +lazy_static! { |
| 103 | + static ref APP_MANAGER: UPSafeCell<AppManager> = unsafe { |
| 104 | + UPSafeCell::new({ |
| 105 | + extern "C" { |
| 106 | + fn _num_app(); |
| 107 | + } |
| 108 | + let num_app_ptr = _num_app as usize as *const usize; |
| 109 | + let num_app = num_app_ptr.read_volatile(); |
| 110 | + let mut app_start: [usize; MAX_APP_NUM + 1] = [0; MAX_APP_NUM + 1]; |
| 111 | + let app_start_raw: &[usize] = |
| 112 | + core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1); |
| 113 | + app_start[..=num_app].copy_from_slice(app_start_raw); |
| 114 | + AppManager { |
| 115 | + num_app, |
| 116 | + current_app: 0, |
| 117 | + app_start, |
| 118 | + } |
| 119 | + }) |
| 120 | + }; |
| 121 | +} |
| 122 | + |
| 123 | +/// init batch subsystem |
| 124 | +pub fn init() { |
| 125 | + print_app_info(); |
| 126 | +} |
| 127 | + |
| 128 | +/// print apps info |
| 129 | +pub fn print_app_info() { |
| 130 | + APP_MANAGER.exclusive_access().print_app_info(); |
| 131 | +} |
| 132 | + |
| 133 | +/// run next app |
| 134 | +pub fn run_next_app() -> ! { |
| 135 | + let mut app_manager = APP_MANAGER.exclusive_access(); |
| 136 | + let current_app = app_manager.get_current_app(); |
| 137 | + unsafe { |
| 138 | + app_manager.load_app(current_app); |
| 139 | + } |
| 140 | + app_manager.move_to_next_app(); |
| 141 | + drop(app_manager); |
| 142 | + // before this we have to drop local variables related to resources manually |
| 143 | + // and release the resources |
| 144 | + extern "C" { |
| 145 | + fn __restore(cx_addr: usize); |
| 146 | + } |
| 147 | + unsafe { |
| 148 | + __restore(KERNEL_STACK.push_context(TrapContext::app_init_context( |
| 149 | + APP_BASE_ADDRESS, |
| 150 | + USER_STACK.get_sp(), |
| 151 | + )) as *const _ as usize); |
| 152 | + } |
| 153 | + panic!("Unreachable in batch::run_current_app!"); |
| 154 | +} |
0 commit comments