11496ba7bSLoGin use core::{ 21496ba7bSLoGin arch::asm, 31496ba7bSLoGin intrinsics::unlikely, 41496ba7bSLoGin mem::ManuallyDrop, 51496ba7bSLoGin sync::atomic::{compiler_fence, Ordering}, 61496ba7bSLoGin }; 71496ba7bSLoGin 81496ba7bSLoGin use alloc::{string::String, sync::Arc, vec::Vec}; 91496ba7bSLoGin 101496ba7bSLoGin use memoffset::offset_of; 111496ba7bSLoGin use x86::{controlregs::Cr4, segmentation::SegmentSelector}; 121496ba7bSLoGin 131496ba7bSLoGin use crate::{ 141496ba7bSLoGin arch::process::table::TSSManager, 151496ba7bSLoGin exception::InterruptArch, 161496ba7bSLoGin libs::spinlock::SpinLockGuard, 171496ba7bSLoGin mm::{ 181496ba7bSLoGin percpu::{PerCpu, PerCpuVar}, 191496ba7bSLoGin VirtAddr, 201496ba7bSLoGin }, 211496ba7bSLoGin process::{ 221496ba7bSLoGin fork::CloneFlags, KernelStack, ProcessControlBlock, ProcessFlags, ProcessManager, 231496ba7bSLoGin SwitchResult, SWITCH_RESULT, 241496ba7bSLoGin }, 251496ba7bSLoGin syscall::{Syscall, SystemError}, 261496ba7bSLoGin }; 271496ba7bSLoGin 281496ba7bSLoGin use self::{ 291496ba7bSLoGin kthread::kernel_thread_bootstrap_stage1, 301496ba7bSLoGin table::{switch_fs_and_gs, KERNEL_DS, USER_DS}, 311496ba7bSLoGin }; 321496ba7bSLoGin 331496ba7bSLoGin use super::{fpu::FpState, interrupt::TrapFrame, CurrentIrqArch}; 341496ba7bSLoGin 351496ba7bSLoGin mod c_adapter; 361496ba7bSLoGin pub mod kthread; 371496ba7bSLoGin pub mod syscall; 381496ba7bSLoGin pub mod table; 391496ba7bSLoGin 401496ba7bSLoGin extern "C" { 411496ba7bSLoGin /// 从中断返回 421496ba7bSLoGin fn ret_from_intr(); 431496ba7bSLoGin } 441496ba7bSLoGin 45*de71ec25SLoGin #[allow(dead_code)] 46*de71ec25SLoGin #[repr(align(32768))] 47*de71ec25SLoGin union InitProcUnion { 48*de71ec25SLoGin /// 用于存放idle进程的内核栈 49*de71ec25SLoGin idle_stack: [u8; 32768], 50*de71ec25SLoGin } 51*de71ec25SLoGin 52*de71ec25SLoGin #[link_section = ".data.init_proc_union"] 53*de71ec25SLoGin #[no_mangle] 54*de71ec25SLoGin static BSP_IDLE_STACK_SPACE: InitProcUnion = InitProcUnion { 55*de71ec25SLoGin idle_stack: [0; 32768], 56*de71ec25SLoGin }; 57*de71ec25SLoGin 581496ba7bSLoGin /// PCB中与架构相关的信息 591496ba7bSLoGin #[derive(Debug, Clone)] 601496ba7bSLoGin #[allow(dead_code)] 611496ba7bSLoGin pub struct ArchPCBInfo { 621496ba7bSLoGin rflags: usize, 631496ba7bSLoGin rbx: usize, 641496ba7bSLoGin r12: usize, 651496ba7bSLoGin r13: usize, 661496ba7bSLoGin r14: usize, 671496ba7bSLoGin r15: usize, 681496ba7bSLoGin rbp: usize, 691496ba7bSLoGin rsp: usize, 701496ba7bSLoGin rip: usize, 711496ba7bSLoGin cr2: usize, 721496ba7bSLoGin fsbase: usize, 731496ba7bSLoGin gsbase: usize, 741496ba7bSLoGin fs: u16, 751496ba7bSLoGin gs: u16, 761496ba7bSLoGin 771496ba7bSLoGin /// 浮点寄存器的状态 781496ba7bSLoGin fp_state: Option<FpState>, 791496ba7bSLoGin } 801496ba7bSLoGin 811496ba7bSLoGin #[allow(dead_code)] 821496ba7bSLoGin impl ArchPCBInfo { 831496ba7bSLoGin /// 创建一个新的ArchPCBInfo 841496ba7bSLoGin /// 851496ba7bSLoGin /// ## 参数 861496ba7bSLoGin /// 871496ba7bSLoGin /// - `kstack`:内核栈的引用,如果为None,则不会设置rsp和rbp。如果为Some,则会设置rsp和rbp为内核栈的最高地址。 881496ba7bSLoGin /// 891496ba7bSLoGin /// ## 返回值 901496ba7bSLoGin /// 911496ba7bSLoGin /// 返回一个新的ArchPCBInfo 921496ba7bSLoGin pub fn new(kstack: Option<&KernelStack>) -> Self { 931496ba7bSLoGin let mut r = Self { 941496ba7bSLoGin rflags: 0, 951496ba7bSLoGin rbx: 0, 961496ba7bSLoGin r12: 0, 971496ba7bSLoGin r13: 0, 981496ba7bSLoGin r14: 0, 991496ba7bSLoGin r15: 0, 1001496ba7bSLoGin rbp: 0, 1011496ba7bSLoGin rsp: 0, 1021496ba7bSLoGin rip: 0, 1031496ba7bSLoGin cr2: 0, 1041496ba7bSLoGin fsbase: 0, 1051496ba7bSLoGin gsbase: 0, 1061496ba7bSLoGin fs: KERNEL_DS.bits(), 1071496ba7bSLoGin gs: KERNEL_DS.bits(), 1081496ba7bSLoGin fp_state: None, 1091496ba7bSLoGin }; 1101496ba7bSLoGin 1111496ba7bSLoGin if kstack.is_some() { 1121496ba7bSLoGin let kstack = kstack.unwrap(); 1131496ba7bSLoGin r.rsp = kstack.stack_max_address().data(); 1141496ba7bSLoGin r.rbp = kstack.stack_max_address().data(); 1151496ba7bSLoGin } 1161496ba7bSLoGin 1171496ba7bSLoGin return r; 1181496ba7bSLoGin } 1191496ba7bSLoGin 1201496ba7bSLoGin pub fn set_stack(&mut self, stack: VirtAddr) { 1211496ba7bSLoGin self.rsp = stack.data(); 1221496ba7bSLoGin } 1231496ba7bSLoGin 1241496ba7bSLoGin pub fn set_stack_base(&mut self, stack_base: VirtAddr) { 1251496ba7bSLoGin self.rbp = stack_base.data(); 1261496ba7bSLoGin } 1271496ba7bSLoGin 1281496ba7bSLoGin pub fn rbp(&self) -> usize { 1291496ba7bSLoGin self.rbp 1301496ba7bSLoGin } 1311496ba7bSLoGin 1321496ba7bSLoGin pub unsafe fn push_to_stack(&mut self, value: usize) { 1331496ba7bSLoGin self.rsp -= core::mem::size_of::<usize>(); 1341496ba7bSLoGin *(self.rsp as *mut usize) = value; 1351496ba7bSLoGin } 1361496ba7bSLoGin 1371496ba7bSLoGin pub unsafe fn pop_from_stack(&mut self) -> usize { 1381496ba7bSLoGin let value = *(self.rsp as *const usize); 1391496ba7bSLoGin self.rsp += core::mem::size_of::<usize>(); 1401496ba7bSLoGin value 1411496ba7bSLoGin } 1421496ba7bSLoGin 1431496ba7bSLoGin pub fn save_fp_state(&mut self) { 1441496ba7bSLoGin if self.fp_state.is_none() { 1451496ba7bSLoGin self.fp_state = Some(FpState::new()); 1461496ba7bSLoGin } 1471496ba7bSLoGin 1481496ba7bSLoGin self.fp_state.as_mut().unwrap().save(); 1491496ba7bSLoGin } 1501496ba7bSLoGin 1511496ba7bSLoGin pub fn restore_fp_state(&mut self) { 1521496ba7bSLoGin if unlikely(self.fp_state.is_none()) { 1531496ba7bSLoGin return; 1541496ba7bSLoGin } 1551496ba7bSLoGin 1561496ba7bSLoGin self.fp_state.as_mut().unwrap().restore(); 1571496ba7bSLoGin } 1581496ba7bSLoGin 1591496ba7bSLoGin pub unsafe fn save_fsbase(&mut self) { 1601496ba7bSLoGin if x86::controlregs::cr4().contains(Cr4::CR4_ENABLE_FSGSBASE) { 1611496ba7bSLoGin self.fsbase = x86::current::segmentation::rdfsbase() as usize; 1621496ba7bSLoGin } else { 1631496ba7bSLoGin self.fsbase = 0; 1641496ba7bSLoGin } 1651496ba7bSLoGin } 1661496ba7bSLoGin 1671496ba7bSLoGin pub unsafe fn save_gsbase(&mut self) { 1681496ba7bSLoGin if x86::controlregs::cr4().contains(Cr4::CR4_ENABLE_FSGSBASE) { 1691496ba7bSLoGin self.gsbase = x86::current::segmentation::rdgsbase() as usize; 1701496ba7bSLoGin } else { 1711496ba7bSLoGin self.gsbase = 0; 1721496ba7bSLoGin } 1731496ba7bSLoGin } 1741496ba7bSLoGin 1751496ba7bSLoGin pub unsafe fn restore_fsbase(&mut self) { 1761496ba7bSLoGin if x86::controlregs::cr4().contains(Cr4::CR4_ENABLE_FSGSBASE) { 1771496ba7bSLoGin x86::current::segmentation::wrfsbase(self.fsbase as u64); 1781496ba7bSLoGin } 1791496ba7bSLoGin } 1801496ba7bSLoGin 1811496ba7bSLoGin pub unsafe fn restore_gsbase(&mut self) { 1821496ba7bSLoGin if x86::controlregs::cr4().contains(Cr4::CR4_ENABLE_FSGSBASE) { 1831496ba7bSLoGin x86::current::segmentation::wrgsbase(self.gsbase as u64); 1841496ba7bSLoGin } 1851496ba7bSLoGin } 1861496ba7bSLoGin 1871496ba7bSLoGin pub fn fsbase(&self) -> usize { 1881496ba7bSLoGin self.fsbase 1891496ba7bSLoGin } 1901496ba7bSLoGin 1911496ba7bSLoGin pub fn gsbase(&self) -> usize { 1921496ba7bSLoGin self.gsbase 1931496ba7bSLoGin } 1941496ba7bSLoGin } 1951496ba7bSLoGin 1961496ba7bSLoGin impl ProcessControlBlock { 1971496ba7bSLoGin /// 获取当前进程的pcb 1981496ba7bSLoGin pub fn arch_current_pcb() -> Arc<Self> { 1991496ba7bSLoGin // 获取栈指针 2001496ba7bSLoGin let ptr = VirtAddr::new(x86::current::registers::rsp() as usize); 2011496ba7bSLoGin let stack_base = VirtAddr::new(ptr.data() & (!(KernelStack::ALIGN - 1))); 2021496ba7bSLoGin // 从内核栈的最低地址处取出pcb的地址 2031496ba7bSLoGin let p = stack_base.data() as *const *const ProcessControlBlock; 2041496ba7bSLoGin if unlikely((unsafe { *p }).is_null()) { 2051496ba7bSLoGin panic!("current_pcb is null"); 2061496ba7bSLoGin } 2071496ba7bSLoGin unsafe { 2081496ba7bSLoGin // 为了防止内核栈的pcb指针被释放,这里需要将其包装一下,使得Arc的drop不会被调用 2091496ba7bSLoGin let arc_wrapper: ManuallyDrop<Arc<ProcessControlBlock>> = 2101496ba7bSLoGin ManuallyDrop::new(Arc::from_raw(*p)); 2111496ba7bSLoGin 2121496ba7bSLoGin let new_arc: Arc<ProcessControlBlock> = Arc::clone(&arc_wrapper); 2131496ba7bSLoGin return new_arc; 2141496ba7bSLoGin } 2151496ba7bSLoGin } 2161496ba7bSLoGin } 2171496ba7bSLoGin 2181496ba7bSLoGin impl ProcessManager { 2191496ba7bSLoGin pub fn arch_init() { 2201496ba7bSLoGin { 2211496ba7bSLoGin // 初始化进程切换结果 per cpu变量 2221496ba7bSLoGin let mut switch_res_vec: Vec<SwitchResult> = Vec::new(); 2231496ba7bSLoGin for _ in 0..PerCpu::MAX_CPU_NUM { 2241496ba7bSLoGin switch_res_vec.push(SwitchResult::new()); 2251496ba7bSLoGin } 2261496ba7bSLoGin unsafe { 2271496ba7bSLoGin SWITCH_RESULT = Some(PerCpuVar::new(switch_res_vec).unwrap()); 2281496ba7bSLoGin } 2291496ba7bSLoGin } 2301496ba7bSLoGin } 2311496ba7bSLoGin /// fork的过程中复制线程 2321496ba7bSLoGin /// 2331496ba7bSLoGin /// 由于这个过程与具体的架构相关,所以放在这里 2341496ba7bSLoGin pub fn copy_thread( 2351496ba7bSLoGin _clone_flags: &CloneFlags, 2361496ba7bSLoGin current_pcb: &Arc<ProcessControlBlock>, 2371496ba7bSLoGin new_pcb: &Arc<ProcessControlBlock>, 2381496ba7bSLoGin current_trapframe: &TrapFrame, 2391496ba7bSLoGin ) -> Result<(), SystemError> { 2401496ba7bSLoGin let mut child_trapframe = current_trapframe.clone(); 2411496ba7bSLoGin 2421496ba7bSLoGin // 子进程的返回值为0 2431496ba7bSLoGin child_trapframe.set_return_value(0); 2441496ba7bSLoGin 2451496ba7bSLoGin // 设置子进程的栈基址(开始执行中断返回流程时的栈基址) 2461496ba7bSLoGin let mut new_arch_guard = new_pcb.arch_info(); 2471496ba7bSLoGin let kernel_stack_guard = new_pcb.kernel_stack(); 2481496ba7bSLoGin 2491496ba7bSLoGin // 设置子进程在内核态开始执行时的rsp、rbp 2501496ba7bSLoGin new_arch_guard.set_stack_base(kernel_stack_guard.stack_max_address()); 2511496ba7bSLoGin 2521496ba7bSLoGin let trap_frame_vaddr: VirtAddr = 2531496ba7bSLoGin kernel_stack_guard.stack_max_address() - core::mem::size_of::<TrapFrame>(); 2541496ba7bSLoGin new_arch_guard.set_stack(trap_frame_vaddr); 2551496ba7bSLoGin 2561496ba7bSLoGin // 拷贝栈帧 2571496ba7bSLoGin unsafe { 2581496ba7bSLoGin let trap_frame_ptr = trap_frame_vaddr.data() as *mut TrapFrame; 2591496ba7bSLoGin *trap_frame_ptr = child_trapframe; 2601496ba7bSLoGin } 2611496ba7bSLoGin 2621496ba7bSLoGin let current_arch_guard = current_pcb.arch_info_irqsave(); 2631496ba7bSLoGin new_arch_guard.fsbase = current_arch_guard.fsbase; 2641496ba7bSLoGin new_arch_guard.gsbase = current_arch_guard.gsbase; 2651496ba7bSLoGin new_arch_guard.fs = current_arch_guard.fs; 2661496ba7bSLoGin new_arch_guard.gs = current_arch_guard.gs; 2671496ba7bSLoGin new_arch_guard.fp_state = current_arch_guard.fp_state.clone(); 2681496ba7bSLoGin 2691496ba7bSLoGin // 拷贝浮点寄存器的状态 2701496ba7bSLoGin if let Some(fp_state) = current_arch_guard.fp_state.as_ref() { 2711496ba7bSLoGin new_arch_guard.fp_state = Some(*fp_state); 2721496ba7bSLoGin } 2731496ba7bSLoGin drop(current_arch_guard); 2741496ba7bSLoGin 2751496ba7bSLoGin // 设置返回地址(子进程开始执行的指令地址) 2761496ba7bSLoGin 2771496ba7bSLoGin if new_pcb.flags().contains(ProcessFlags::KTHREAD) { 2781496ba7bSLoGin let kthread_bootstrap_stage1_func_addr = kernel_thread_bootstrap_stage1 as usize; 2791496ba7bSLoGin 2801496ba7bSLoGin new_arch_guard.rip = kthread_bootstrap_stage1_func_addr; 2811496ba7bSLoGin } else { 2821496ba7bSLoGin new_arch_guard.rip = ret_from_intr as usize; 2831496ba7bSLoGin } 2841496ba7bSLoGin 2851496ba7bSLoGin return Ok(()); 2861496ba7bSLoGin } 2871496ba7bSLoGin 2881496ba7bSLoGin /// 切换进程 2891496ba7bSLoGin /// 2901496ba7bSLoGin /// ## 参数 2911496ba7bSLoGin /// 2921496ba7bSLoGin /// - `prev`:上一个进程的pcb 2931496ba7bSLoGin /// - `next`:下一个进程的pcb 2941496ba7bSLoGin pub unsafe fn switch_process(prev: Arc<ProcessControlBlock>, next: Arc<ProcessControlBlock>) { 2951496ba7bSLoGin assert!(CurrentIrqArch::is_irq_enabled() == false); 2961496ba7bSLoGin 2971496ba7bSLoGin // 保存浮点寄存器 2981496ba7bSLoGin prev.arch_info().save_fp_state(); 2991496ba7bSLoGin // 切换浮点寄存器 3001496ba7bSLoGin next.arch_info().restore_fp_state(); 3011496ba7bSLoGin 3021496ba7bSLoGin // 切换fsbase 3031496ba7bSLoGin prev.arch_info().save_fsbase(); 3041496ba7bSLoGin next.arch_info().restore_fsbase(); 3051496ba7bSLoGin 3061496ba7bSLoGin // 切换gsbase 3071496ba7bSLoGin prev.arch_info().save_gsbase(); 3081496ba7bSLoGin next.arch_info().restore_gsbase(); 3091496ba7bSLoGin 3101496ba7bSLoGin // 切换地址空间 3111496ba7bSLoGin let next_addr_space = next.basic().user_vm().as_ref().unwrap().clone(); 3121496ba7bSLoGin compiler_fence(Ordering::SeqCst); 3131496ba7bSLoGin 3141496ba7bSLoGin next_addr_space.read().user_mapper.utable.make_current(); 3151496ba7bSLoGin compiler_fence(Ordering::SeqCst); 3161496ba7bSLoGin // 切换内核栈 3171496ba7bSLoGin 3181496ba7bSLoGin // 获取arch info的锁,并强制泄露其守卫(切换上下文后,在switch_finish_hook中会释放锁) 3191496ba7bSLoGin let next_arch = SpinLockGuard::leak(next.arch_info()); 3201496ba7bSLoGin let prev_arch = SpinLockGuard::leak(prev.arch_info()); 3211496ba7bSLoGin 3221496ba7bSLoGin prev_arch.rip = switch_back as usize; 3231496ba7bSLoGin 3241496ba7bSLoGin // 恢复当前的 preempt count*2 3251496ba7bSLoGin ProcessManager::current_pcb().preempt_enable(); 3261496ba7bSLoGin ProcessManager::current_pcb().preempt_enable(); 3271496ba7bSLoGin SWITCH_RESULT.as_mut().unwrap().get_mut().prev_pcb = Some(prev.clone()); 3281496ba7bSLoGin SWITCH_RESULT.as_mut().unwrap().get_mut().next_pcb = Some(next.clone()); 3291496ba7bSLoGin 3301496ba7bSLoGin // 切换tss 3311496ba7bSLoGin TSSManager::current_tss().set_rsp( 3321496ba7bSLoGin x86::Ring::Ring0, 3331496ba7bSLoGin next.kernel_stack().stack_max_address().data() as u64, 3341496ba7bSLoGin ); 3351496ba7bSLoGin // kdebug!("switch tss ok"); 3361496ba7bSLoGin 337*de71ec25SLoGin compiler_fence(Ordering::SeqCst); 3381496ba7bSLoGin // 正式切换上下文 3391496ba7bSLoGin switch_to_inner(prev_arch, next_arch); 3401496ba7bSLoGin } 3411496ba7bSLoGin } 3421496ba7bSLoGin 3431496ba7bSLoGin /// 保存上下文,然后切换进程,接着jmp到`switch_finish_hook`钩子函数 3441496ba7bSLoGin #[naked] 3451496ba7bSLoGin unsafe extern "sysv64" fn switch_to_inner(prev: &mut ArchPCBInfo, next: &mut ArchPCBInfo) { 3461496ba7bSLoGin asm!( 3471496ba7bSLoGin // As a quick reminder for those who are unfamiliar with the System V ABI (extern "C"): 3481496ba7bSLoGin // 3491496ba7bSLoGin // - the current parameters are passed in the registers `rdi`, `rsi`, 3501496ba7bSLoGin // - we can modify scratch registers, e.g. rax 3511496ba7bSLoGin // - we cannot change callee-preserved registers arbitrarily, e.g. rbx, which is why we 3521496ba7bSLoGin // store them here in the first place. 3531496ba7bSLoGin concat!(" 3541496ba7bSLoGin // Save old registers, and load new ones 3551496ba7bSLoGin mov [rdi + {off_rbx}], rbx 3561496ba7bSLoGin mov rbx, [rsi + {off_rbx}] 3571496ba7bSLoGin 3581496ba7bSLoGin mov [rdi + {off_r12}], r12 3591496ba7bSLoGin mov r12, [rsi + {off_r12}] 3601496ba7bSLoGin 3611496ba7bSLoGin mov [rdi + {off_r13}], r13 3621496ba7bSLoGin mov r13, [rsi + {off_r13}] 3631496ba7bSLoGin 3641496ba7bSLoGin mov [rdi + {off_r14}], r14 3651496ba7bSLoGin mov r14, [rsi + {off_r14}] 3661496ba7bSLoGin 3671496ba7bSLoGin mov [rdi + {off_r15}], r15 3681496ba7bSLoGin mov r15, [rsi + {off_r15}] 3691496ba7bSLoGin 3701496ba7bSLoGin // switch segment registers (这些寄存器只能通过接下来的switch_hook的return来切换) 3711496ba7bSLoGin mov [rdi + {off_fs}], fs 3721496ba7bSLoGin mov [rdi + {off_gs}], gs 3731496ba7bSLoGin 3741496ba7bSLoGin push rbp 3751496ba7bSLoGin push rax 3761496ba7bSLoGin 3771496ba7bSLoGin mov [rdi + {off_rbp}], rbp 3781496ba7bSLoGin mov rbp, [rsi + {off_rbp}] 3791496ba7bSLoGin 3801496ba7bSLoGin mov [rdi + {off_rsp}], rsp 3811496ba7bSLoGin mov rsp, [rsi + {off_rsp}] 3821496ba7bSLoGin 3831496ba7bSLoGin // // push RFLAGS (can only be modified via stack) 3841496ba7bSLoGin pushfq 3851496ba7bSLoGin // // pop RFLAGS into `self.rflags` 3861496ba7bSLoGin pop QWORD PTR [rdi + {off_rflags}] 3871496ba7bSLoGin 3881496ba7bSLoGin // // push `next.rflags` 3891496ba7bSLoGin push QWORD PTR [rsi + {off_rflags}] 3901496ba7bSLoGin // // pop into RFLAGS 3911496ba7bSLoGin popfq 3921496ba7bSLoGin 3931496ba7bSLoGin // push next rip to stack 3941496ba7bSLoGin push QWORD PTR [rsi + {off_rip}] 3951496ba7bSLoGin 3961496ba7bSLoGin 3971496ba7bSLoGin // When we return, we cannot even guarantee that the return address on the stack, points to 3981496ba7bSLoGin // the calling function. Thus, we have to execute this Rust hook by 3991496ba7bSLoGin // ourselves, which will unlock the contexts before the later switch. 4001496ba7bSLoGin 4011496ba7bSLoGin // Note that switch_finish_hook will be responsible for executing `ret`. 4021496ba7bSLoGin jmp {switch_hook} 4031496ba7bSLoGin "), 4041496ba7bSLoGin 4051496ba7bSLoGin off_rflags = const(offset_of!(ArchPCBInfo, rflags)), 4061496ba7bSLoGin 4071496ba7bSLoGin off_rbx = const(offset_of!(ArchPCBInfo, rbx)), 4081496ba7bSLoGin off_r12 = const(offset_of!(ArchPCBInfo, r12)), 4091496ba7bSLoGin off_r13 = const(offset_of!(ArchPCBInfo, r13)), 4101496ba7bSLoGin off_r14 = const(offset_of!(ArchPCBInfo, r14)), 4111496ba7bSLoGin off_rbp = const(offset_of!(ArchPCBInfo, rbp)), 4121496ba7bSLoGin off_rsp = const(offset_of!(ArchPCBInfo, rsp)), 4131496ba7bSLoGin off_r15 = const(offset_of!(ArchPCBInfo, r15)), 4141496ba7bSLoGin off_rip = const(offset_of!(ArchPCBInfo, rip)), 4151496ba7bSLoGin off_fs = const(offset_of!(ArchPCBInfo, fs)), 4161496ba7bSLoGin off_gs = const(offset_of!(ArchPCBInfo, gs)), 4171496ba7bSLoGin 4181496ba7bSLoGin switch_hook = sym crate::process::switch_finish_hook, 4191496ba7bSLoGin options(noreturn), 4201496ba7bSLoGin ); 4211496ba7bSLoGin } 4221496ba7bSLoGin 4231496ba7bSLoGin /// 从`switch_to_inner`返回后,执行这个函数 4241496ba7bSLoGin /// 4251496ba7bSLoGin /// 也就是说,当进程再次被调度时,会从这里开始执行 4261496ba7bSLoGin #[inline(never)] 4271496ba7bSLoGin unsafe extern "sysv64" fn switch_back() { 4281496ba7bSLoGin asm!(concat!( 4291496ba7bSLoGin " 4301496ba7bSLoGin pop rax 4311496ba7bSLoGin pop rbp 4321496ba7bSLoGin " 4331496ba7bSLoGin )) 4341496ba7bSLoGin } 4351496ba7bSLoGin 4361496ba7bSLoGin pub unsafe fn arch_switch_to_user(path: String, argv: Vec<String>, envp: Vec<String>) -> ! { 4371496ba7bSLoGin // 以下代码不能发生中断 4381496ba7bSLoGin CurrentIrqArch::interrupt_disable(); 4391496ba7bSLoGin 4401496ba7bSLoGin let current_pcb = ProcessManager::current_pcb(); 4411496ba7bSLoGin let trap_frame_vaddr = VirtAddr::new( 4421496ba7bSLoGin current_pcb.kernel_stack().stack_max_address().data() - core::mem::size_of::<TrapFrame>(), 4431496ba7bSLoGin ); 4441496ba7bSLoGin // kdebug!("trap_frame_vaddr: {:?}", trap_frame_vaddr); 4451496ba7bSLoGin let new_rip = VirtAddr::new(ret_from_intr as usize); 4461496ba7bSLoGin 4471496ba7bSLoGin assert!( 4481496ba7bSLoGin (x86::current::registers::rsp() as usize) < trap_frame_vaddr.data(), 4491496ba7bSLoGin "arch_switch_to_user(): current_rsp >= fake trap 4501496ba7bSLoGin frame vaddr, this may cause some illegal access to memory! 4511496ba7bSLoGin rsp: {:#x}, trap_frame_vaddr: {:#x}", 4521496ba7bSLoGin x86::current::registers::rsp() as usize, 4531496ba7bSLoGin trap_frame_vaddr.data() 4541496ba7bSLoGin ); 4551496ba7bSLoGin 4561496ba7bSLoGin let mut arch_guard = current_pcb.arch_info_irqsave(); 4571496ba7bSLoGin arch_guard.rsp = trap_frame_vaddr.data(); 4581496ba7bSLoGin 4591496ba7bSLoGin arch_guard.fs = USER_DS.bits(); 4601496ba7bSLoGin arch_guard.gs = USER_DS.bits(); 4611496ba7bSLoGin 4621496ba7bSLoGin switch_fs_and_gs( 4631496ba7bSLoGin SegmentSelector::from_bits_truncate(arch_guard.fs), 4641496ba7bSLoGin SegmentSelector::from_bits_truncate(arch_guard.gs), 4651496ba7bSLoGin ); 4661496ba7bSLoGin arch_guard.rip = new_rip.data(); 4671496ba7bSLoGin 4681496ba7bSLoGin drop(arch_guard); 4691496ba7bSLoGin 4701496ba7bSLoGin // 删除kthread的标志 4711496ba7bSLoGin current_pcb.flags().remove(ProcessFlags::KTHREAD); 4721496ba7bSLoGin current_pcb.worker_private().take(); 4731496ba7bSLoGin 4741496ba7bSLoGin let mut trap_frame = TrapFrame::new(); 4751496ba7bSLoGin 4761496ba7bSLoGin compiler_fence(Ordering::SeqCst); 4771496ba7bSLoGin Syscall::do_execve(path, argv, envp, &mut trap_frame).unwrap_or_else(|e| { 4781496ba7bSLoGin panic!( 4791496ba7bSLoGin "arch_switch_to_user(): pid: {pid:?}, Failed to execve: , error: {e:?}", 4801496ba7bSLoGin pid = current_pcb.pid(), 4811496ba7bSLoGin e = e 4821496ba7bSLoGin ); 4831496ba7bSLoGin }); 4841496ba7bSLoGin compiler_fence(Ordering::SeqCst); 4851496ba7bSLoGin 4861496ba7bSLoGin // 重要!在这里之后,一定要保证上面的引用计数变量、动态申请的变量、锁的守卫都被drop了,否则可能导致内存安全问题! 4871496ba7bSLoGin 4881496ba7bSLoGin drop(current_pcb); 4891496ba7bSLoGin 4901496ba7bSLoGin compiler_fence(Ordering::SeqCst); 4911496ba7bSLoGin ready_to_switch_to_user(trap_frame, trap_frame_vaddr.data(), new_rip.data()); 4921496ba7bSLoGin } 4931496ba7bSLoGin 4941496ba7bSLoGin /// 由于需要依赖ret来切换到用户态,所以不能inline 4951496ba7bSLoGin #[inline(never)] 4961496ba7bSLoGin unsafe extern "sysv64" fn ready_to_switch_to_user( 4971496ba7bSLoGin trap_frame: TrapFrame, 4981496ba7bSLoGin trapframe_vaddr: usize, 4991496ba7bSLoGin new_rip: usize, 5001496ba7bSLoGin ) -> ! { 5011496ba7bSLoGin *(trapframe_vaddr as *mut TrapFrame) = trap_frame; 5021496ba7bSLoGin asm!( 5031496ba7bSLoGin "mov rsp, {trapframe_vaddr}", 5041496ba7bSLoGin "push {new_rip}", 5051496ba7bSLoGin "ret", 5061496ba7bSLoGin trapframe_vaddr = in(reg) trapframe_vaddr, 5071496ba7bSLoGin new_rip = in(reg) new_rip 5081496ba7bSLoGin ); 5091496ba7bSLoGin unreachable!() 5101496ba7bSLoGin } 511