xref: /DragonOS/kernel/src/arch/x86_64/process/mod.rs (revision c75089286e9d49cef8d039446bf570c1bd4d2550)
1 use core::{
2     arch::asm,
3     intrinsics::unlikely,
4     mem::ManuallyDrop,
5     sync::atomic::{compiler_fence, Ordering},
6 };
7 
8 use alloc::{
9     string::String,
10     sync::{Arc, Weak},
11     vec::Vec,
12 };
13 
14 use kdepends::memoffset::offset_of;
15 use x86::{controlregs::Cr4, segmentation::SegmentSelector};
16 
17 use crate::{
18     arch::process::table::TSSManager,
19     exception::InterruptArch,
20     kwarn,
21     libs::spinlock::SpinLockGuard,
22     mm::{
23         percpu::{PerCpu, PerCpuVar},
24         VirtAddr,
25     },
26     process::{
27         fork::{CloneFlags, KernelCloneArgs},
28         KernelStack, ProcessControlBlock, ProcessFlags, ProcessManager, SwitchResult,
29         SWITCH_RESULT,
30     },
31     syscall::{Syscall, SystemError},
32 };
33 
34 use self::{
35     kthread::kernel_thread_bootstrap_stage1,
36     syscall::ARCH_SET_FS,
37     table::{switch_fs_and_gs, KERNEL_DS, USER_DS},
38 };
39 
40 use super::{fpu::FpState, interrupt::TrapFrame, syscall::X86_64GSData, CurrentIrqArch};
41 
42 mod c_adapter;
43 pub mod kthread;
44 pub mod syscall;
45 pub mod table;
46 
47 extern "C" {
48     /// 从中断返回
49     fn ret_from_intr();
50 }
51 
52 #[allow(dead_code)]
53 #[repr(align(32768))]
54 union InitProcUnion {
55     /// 用于存放idle进程的内核栈
56     idle_stack: [u8; 32768],
57 }
58 
59 #[link_section = ".data.init_proc_union"]
60 #[no_mangle]
61 static BSP_IDLE_STACK_SPACE: InitProcUnion = InitProcUnion {
62     idle_stack: [0; 32768],
63 };
64 
65 /// PCB中与架构相关的信息
66 #[derive(Debug)]
67 #[allow(dead_code)]
68 pub struct ArchPCBInfo {
69     rflags: usize,
70     rbx: usize,
71     r12: usize,
72     r13: usize,
73     r14: usize,
74     r15: usize,
75     rbp: usize,
76     rsp: usize,
77     rip: usize,
78     cr2: usize,
79     fsbase: usize,
80     gsbase: usize,
81     fs: SegmentSelector,
82     gs: SegmentSelector,
83     /// 存储PCB系统调用栈以及在syscall过程中暂存用户态rsp的结构体
84     gsdata: X86_64GSData,
85     /// 浮点寄存器的状态
86     fp_state: Option<FpState>,
87 }
88 
89 #[allow(dead_code)]
90 impl ArchPCBInfo {
91     /// 创建一个新的ArchPCBInfo
92     ///
93     /// ## 参数
94     ///
95     /// - `kstack`:内核栈的引用,如果为None,则不会设置rsp和rbp。如果为Some,则会设置rsp和rbp为内核栈的最高地址。
96     ///
97     /// ## 返回值
98     ///
99     /// 返回一个新的ArchPCBInfo
100     pub fn new(kstack: &KernelStack) -> Self {
101         let mut r = Self {
102             rflags: 0,
103             rbx: 0,
104             r12: 0,
105             r13: 0,
106             r14: 0,
107             r15: 0,
108             rbp: 0,
109             rsp: 0,
110             rip: 0,
111             cr2: 0,
112             fsbase: 0,
113             gsbase: 0,
114             gsdata: X86_64GSData {
115                 kaddr: VirtAddr::new(0),
116                 uaddr: VirtAddr::new(0),
117             },
118             fs: KERNEL_DS,
119             gs: KERNEL_DS,
120             fp_state: None,
121         };
122 
123         r.rsp = kstack.stack_max_address().data() - 8;
124         r.rbp = kstack.stack_max_address().data();
125 
126         return r;
127     }
128 
129     pub fn set_stack(&mut self, stack: VirtAddr) {
130         self.rsp = stack.data();
131     }
132 
133     pub fn set_stack_base(&mut self, stack_base: VirtAddr) {
134         self.rbp = stack_base.data();
135     }
136 
137     pub fn rbp(&self) -> usize {
138         self.rbp
139     }
140 
141     pub unsafe fn push_to_stack(&mut self, value: usize) {
142         self.rsp -= core::mem::size_of::<usize>();
143         *(self.rsp as *mut usize) = value;
144     }
145 
146     pub unsafe fn pop_from_stack(&mut self) -> usize {
147         let value = *(self.rsp as *const usize);
148         self.rsp += core::mem::size_of::<usize>();
149         value
150     }
151 
152     pub fn save_fp_state(&mut self) {
153         if self.fp_state.is_none() {
154             self.fp_state = Some(FpState::new());
155         }
156 
157         self.fp_state.as_mut().unwrap().save();
158     }
159 
160     pub fn restore_fp_state(&mut self) {
161         if unlikely(self.fp_state.is_none()) {
162             return;
163         }
164 
165         self.fp_state.as_mut().unwrap().restore();
166     }
167 
168     /// 返回浮点寄存器结构体的副本
169     pub fn fp_state(&self) -> &Option<FpState> {
170         &self.fp_state
171     }
172 
173     // 清空浮点寄存器
174     pub fn clear_fp_state(&mut self) {
175         if unlikely(self.fp_state.is_none()) {
176             kwarn!("fp_state is none");
177             return;
178         }
179 
180         self.fp_state.as_mut().unwrap().clear();
181     }
182     pub unsafe fn save_fsbase(&mut self) {
183         if x86::controlregs::cr4().contains(Cr4::CR4_ENABLE_FSGSBASE) {
184             self.fsbase = x86::current::segmentation::rdfsbase() as usize;
185         } else {
186             self.fsbase = x86::msr::rdmsr(x86::msr::IA32_FS_BASE) as usize;
187         }
188     }
189 
190     pub unsafe fn save_gsbase(&mut self) {
191         if x86::controlregs::cr4().contains(Cr4::CR4_ENABLE_FSGSBASE) {
192             self.gsbase = x86::current::segmentation::rdgsbase() as usize;
193         } else {
194             self.gsbase = x86::msr::rdmsr(x86::msr::IA32_GS_BASE) as usize;
195         }
196     }
197 
198     pub unsafe fn restore_fsbase(&mut self) {
199         if x86::controlregs::cr4().contains(Cr4::CR4_ENABLE_FSGSBASE) {
200             x86::current::segmentation::wrfsbase(self.fsbase as u64);
201         } else {
202             x86::msr::wrmsr(x86::msr::IA32_FS_BASE, self.fsbase as u64);
203         }
204     }
205 
206     pub unsafe fn restore_gsbase(&mut self) {
207         if x86::controlregs::cr4().contains(Cr4::CR4_ENABLE_FSGSBASE) {
208             x86::current::segmentation::wrgsbase(self.gsbase as u64);
209         } else {
210             x86::msr::wrmsr(x86::msr::IA32_GS_BASE, self.gsbase as u64);
211         }
212     }
213 
214     /// 将gsdata写入KernelGsbase寄存器
215     pub unsafe fn store_kernel_gsbase(&self) {
216         x86::msr::wrmsr(
217             x86::msr::IA32_KERNEL_GSBASE,
218             &self.gsdata as *const X86_64GSData as u64,
219         );
220     }
221 
222     /// ### 初始化系统调用栈,不得与PCB内核栈冲突(即传入的应该是一个新的栈,避免栈损坏)
223     pub fn init_syscall_stack(&mut self, stack: &KernelStack) {
224         self.gsdata.set_kstack(stack.stack_max_address() - 8);
225     }
226 
227     pub fn fsbase(&self) -> usize {
228         self.fsbase
229     }
230 
231     pub fn gsbase(&self) -> usize {
232         self.gsbase
233     }
234 
235     pub fn cr2_mut(&mut self) -> &mut usize {
236         &mut self.cr2
237     }
238 
239     pub fn fp_state_mut(&mut self) -> &mut Option<FpState> {
240         &mut self.fp_state
241     }
242 
243     /// ### 克隆ArchPCBInfo,需要注意gsdata也是对应clone的
244     pub fn clone_all(&self) -> Self {
245         Self {
246             rflags: self.rflags,
247             rbx: self.rbx,
248             r12: self.r12,
249             r13: self.r13,
250             r14: self.r14,
251             r15: self.r15,
252             rbp: self.rbp,
253             rsp: self.rsp,
254             rip: self.rip,
255             cr2: self.cr2,
256             fsbase: self.fsbase,
257             gsbase: self.gsbase,
258             fs: self.fs.clone(),
259             gs: self.gs.clone(),
260             gsdata: self.gsdata.clone(),
261             fp_state: self.fp_state,
262         }
263     }
264 
265     // ### 从另一个ArchPCBInfo处clone,gsdata会被保留
266     pub fn clone_from(&mut self, from: &Self) {
267         let gsdata = self.gsdata.clone();
268         *self = from.clone_all();
269         self.gsdata = gsdata;
270     }
271 }
272 
273 impl ProcessControlBlock {
274     /// 获取当前进程的pcb
275     pub fn arch_current_pcb() -> Arc<Self> {
276         // 获取栈指针
277         let ptr = VirtAddr::new(x86::current::registers::rsp() as usize);
278         let stack_base = VirtAddr::new(ptr.data() & (!(KernelStack::ALIGN - 1)));
279         // 从内核栈的最低地址处取出pcb的地址
280         let p = stack_base.data() as *const *const ProcessControlBlock;
281         if unlikely((unsafe { *p }).is_null()) {
282             panic!("current_pcb is null");
283         }
284         unsafe {
285             // 为了防止内核栈的pcb weak 指针被释放,这里需要将其包装一下
286             let weak_wrapper: ManuallyDrop<Weak<ProcessControlBlock>> =
287                 ManuallyDrop::new(Weak::from_raw(*p));
288 
289             let new_arc: Arc<ProcessControlBlock> = weak_wrapper.upgrade().unwrap();
290             return new_arc;
291         }
292     }
293 }
294 
295 impl ProcessManager {
296     pub fn arch_init() {
297         {
298             // 初始化进程切换结果 per cpu变量
299             let mut switch_res_vec: Vec<SwitchResult> = Vec::new();
300             for _ in 0..PerCpu::MAX_CPU_NUM {
301                 switch_res_vec.push(SwitchResult::new());
302             }
303             unsafe {
304                 SWITCH_RESULT = Some(PerCpuVar::new(switch_res_vec).unwrap());
305             }
306         }
307     }
308     /// fork的过程中复制线程
309     ///
310     /// 由于这个过程与具体的架构相关,所以放在这里
311     pub fn copy_thread(
312         current_pcb: &Arc<ProcessControlBlock>,
313         new_pcb: &Arc<ProcessControlBlock>,
314         clone_args: KernelCloneArgs,
315         current_trapframe: &TrapFrame,
316     ) -> Result<(), SystemError> {
317         let clone_flags = clone_args.flags;
318         let mut child_trapframe = current_trapframe.clone();
319 
320         // 子进程的返回值为0
321         child_trapframe.set_return_value(0);
322 
323         // 设置子进程的栈基址(开始执行中断返回流程时的栈基址)
324         let mut new_arch_guard = new_pcb.arch_info();
325         let kernel_stack_guard = new_pcb.kernel_stack();
326 
327         // 设置子进程在内核态开始执行时的rsp、rbp
328         new_arch_guard.set_stack_base(kernel_stack_guard.stack_max_address());
329 
330         let trap_frame_vaddr: VirtAddr =
331             kernel_stack_guard.stack_max_address() - core::mem::size_of::<TrapFrame>();
332         new_arch_guard.set_stack(trap_frame_vaddr);
333 
334         // 拷贝栈帧
335         unsafe {
336             let usp = clone_args.stack;
337             if usp != 0 {
338                 child_trapframe.rsp = usp as u64;
339             }
340             let trap_frame_ptr = trap_frame_vaddr.data() as *mut TrapFrame;
341             *trap_frame_ptr = child_trapframe;
342         }
343 
344         let current_arch_guard = current_pcb.arch_info_irqsave();
345         new_arch_guard.fsbase = current_arch_guard.fsbase;
346         new_arch_guard.gsbase = current_arch_guard.gsbase;
347         new_arch_guard.fs = current_arch_guard.fs;
348         new_arch_guard.gs = current_arch_guard.gs;
349         new_arch_guard.fp_state = current_arch_guard.fp_state.clone();
350 
351         // 拷贝浮点寄存器的状态
352         if let Some(fp_state) = current_arch_guard.fp_state.as_ref() {
353             new_arch_guard.fp_state = Some(*fp_state);
354         }
355         drop(current_arch_guard);
356 
357         // 设置返回地址(子进程开始执行的指令地址)
358         if new_pcb.flags().contains(ProcessFlags::KTHREAD) {
359             let kthread_bootstrap_stage1_func_addr = kernel_thread_bootstrap_stage1 as usize;
360             new_arch_guard.rip = kthread_bootstrap_stage1_func_addr;
361         } else {
362             new_arch_guard.rip = ret_from_intr as usize;
363         }
364 
365         // 设置tls
366         if clone_flags.contains(CloneFlags::CLONE_SETTLS) {
367             drop(new_arch_guard);
368             Syscall::do_arch_prctl_64(new_pcb, ARCH_SET_FS, clone_args.tls, true)?;
369         }
370 
371         return Ok(());
372     }
373 
374     /// 切换进程
375     ///
376     /// ## 参数
377     ///
378     /// - `prev`:上一个进程的pcb
379     /// - `next`:下一个进程的pcb
380     pub unsafe fn switch_process(prev: Arc<ProcessControlBlock>, next: Arc<ProcessControlBlock>) {
381         assert!(CurrentIrqArch::is_irq_enabled() == false);
382 
383         // 保存浮点寄存器
384         prev.arch_info().save_fp_state();
385         // 切换浮点寄存器
386         next.arch_info().restore_fp_state();
387 
388         // 切换fsbase
389         prev.arch_info().save_fsbase();
390         next.arch_info().restore_fsbase();
391 
392         // 切换gsbase
393         Self::switch_gsbase(&prev, &next);
394 
395         // 切换地址空间
396         let next_addr_space = next.basic().user_vm().as_ref().unwrap().clone();
397         compiler_fence(Ordering::SeqCst);
398 
399         next_addr_space.read().user_mapper.utable.make_current();
400         drop(next_addr_space);
401         compiler_fence(Ordering::SeqCst);
402         // 切换内核栈
403 
404         // 获取arch info的锁,并强制泄露其守卫(切换上下文后,在switch_finish_hook中会释放锁)
405         let next_arch = SpinLockGuard::leak(next.arch_info()) as *mut ArchPCBInfo;
406         let prev_arch = SpinLockGuard::leak(prev.arch_info()) as *mut ArchPCBInfo;
407 
408         (*prev_arch).rip = switch_back as usize;
409 
410         // 恢复当前的 preempt count*2
411         ProcessManager::current_pcb().preempt_enable();
412         ProcessManager::current_pcb().preempt_enable();
413 
414         // 切换tss
415         TSSManager::current_tss().set_rsp(
416             x86::Ring::Ring0,
417             next.kernel_stack().stack_max_address().data() as u64,
418         );
419         SWITCH_RESULT.as_mut().unwrap().get_mut().prev_pcb = Some(prev);
420         SWITCH_RESULT.as_mut().unwrap().get_mut().next_pcb = Some(next);
421         // kdebug!("switch tss ok");
422         compiler_fence(Ordering::SeqCst);
423         // 正式切换上下文
424         switch_to_inner(prev_arch, next_arch);
425     }
426 
427     unsafe fn switch_gsbase(prev: &Arc<ProcessControlBlock>, next: &Arc<ProcessControlBlock>) {
428         asm!("swapgs", options(nostack, preserves_flags));
429         prev.arch_info().save_gsbase();
430         next.arch_info().restore_gsbase();
431         // 将下一个进程的kstack写入kernel_gsbase
432         next.arch_info().store_kernel_gsbase();
433         asm!("swapgs", options(nostack, preserves_flags));
434     }
435 }
436 
437 /// 保存上下文,然后切换进程,接着jmp到`switch_finish_hook`钩子函数
438 #[naked]
439 unsafe extern "sysv64" fn switch_to_inner(prev: *mut ArchPCBInfo, next: *mut ArchPCBInfo) {
440     asm!(
441         // As a quick reminder for those who are unfamiliar with the System V ABI (extern "C"):
442         //
443         // - the current parameters are passed in the registers `rdi`, `rsi`,
444         // - we can modify scratch registers, e.g. rax
445         // - we cannot change callee-preserved registers arbitrarily, e.g. rbx, which is why we
446         //   store them here in the first place.
447         concat!("
448         // Save old registers, and load new ones
449         mov [rdi + {off_rbx}], rbx
450         mov rbx, [rsi + {off_rbx}]
451 
452         mov [rdi + {off_r12}], r12
453         mov r12, [rsi + {off_r12}]
454 
455         mov [rdi + {off_r13}], r13
456         mov r13, [rsi + {off_r13}]
457 
458         mov [rdi + {off_r14}], r14
459         mov r14, [rsi + {off_r14}]
460 
461         mov [rdi + {off_r15}], r15
462         mov r15, [rsi + {off_r15}]
463 
464         // switch segment registers (这些寄存器只能通过接下来的switch_hook的return来切换)
465         mov [rdi + {off_fs}], fs
466         mov [rdi + {off_gs}], gs
467 
468         // mov fs, [rsi + {off_fs}]
469         // mov gs, [rsi + {off_gs}]
470 
471         push rbp
472         push rax
473 
474         mov [rdi + {off_rbp}], rbp
475         mov rbp, [rsi + {off_rbp}]
476 
477         mov [rdi + {off_rsp}], rsp
478         mov rsp, [rsi + {off_rsp}]
479 
480         // // push RFLAGS (can only be modified via stack)
481         pushfq
482         // // pop RFLAGS into `self.rflags`
483         pop QWORD PTR [rdi + {off_rflags}]
484 
485         // // push `next.rflags`
486         push QWORD PTR [rsi + {off_rflags}]
487         // // pop into RFLAGS
488         popfq
489 
490         // push next rip to stack
491         push QWORD PTR [rsi + {off_rip}]
492 
493 
494         // When we return, we cannot even guarantee that the return address on the stack, points to
495         // the calling function. Thus, we have to execute this Rust hook by
496         // ourselves, which will unlock the contexts before the later switch.
497 
498         // Note that switch_finish_hook will be responsible for executing `ret`.
499         jmp {switch_hook}
500         "),
501 
502         off_rflags = const(offset_of!(ArchPCBInfo, rflags)),
503 
504         off_rbx = const(offset_of!(ArchPCBInfo, rbx)),
505         off_r12 = const(offset_of!(ArchPCBInfo, r12)),
506         off_r13 = const(offset_of!(ArchPCBInfo, r13)),
507         off_r14 = const(offset_of!(ArchPCBInfo, r14)),
508         off_rbp = const(offset_of!(ArchPCBInfo, rbp)),
509         off_rsp = const(offset_of!(ArchPCBInfo, rsp)),
510         off_r15 = const(offset_of!(ArchPCBInfo, r15)),
511         off_rip = const(offset_of!(ArchPCBInfo, rip)),
512         off_fs = const(offset_of!(ArchPCBInfo, fs)),
513         off_gs = const(offset_of!(ArchPCBInfo, gs)),
514 
515         switch_hook = sym crate::process::switch_finish_hook,
516         options(noreturn),
517     );
518 }
519 
520 /// 从`switch_to_inner`返回后,执行这个函数
521 ///
522 /// 也就是说,当进程再次被调度时,会从这里开始执行
523 #[inline(never)]
524 unsafe extern "sysv64" fn switch_back() {
525     asm!(concat!(
526         "
527         pop rax
528         pop rbp
529         "
530     ))
531 }
532 
533 pub unsafe fn arch_switch_to_user(path: String, argv: Vec<String>, envp: Vec<String>) -> ! {
534     // 以下代码不能发生中断
535     CurrentIrqArch::interrupt_disable();
536 
537     let current_pcb = ProcessManager::current_pcb();
538     let trap_frame_vaddr = VirtAddr::new(
539         current_pcb.kernel_stack().stack_max_address().data() - core::mem::size_of::<TrapFrame>(),
540     );
541     // kdebug!("trap_frame_vaddr: {:?}", trap_frame_vaddr);
542     let new_rip = VirtAddr::new(ret_from_intr as usize);
543 
544     assert!(
545         (x86::current::registers::rsp() as usize) < trap_frame_vaddr.data(),
546         "arch_switch_to_user(): current_rsp >= fake trap
547         frame vaddr, this may cause some illegal access to memory!
548         rsp: {:#x}, trap_frame_vaddr: {:#x}",
549         x86::current::registers::rsp() as usize,
550         trap_frame_vaddr.data()
551     );
552 
553     let mut arch_guard = current_pcb.arch_info_irqsave();
554     arch_guard.rsp = trap_frame_vaddr.data();
555 
556     arch_guard.fs = USER_DS;
557     arch_guard.gs = USER_DS;
558 
559     // 将内核gs数据压进cpu
560     arch_guard.store_kernel_gsbase();
561 
562     switch_fs_and_gs(
563         SegmentSelector::from_bits_truncate(arch_guard.fs.bits()),
564         SegmentSelector::from_bits_truncate(arch_guard.gs.bits()),
565     );
566     arch_guard.rip = new_rip.data();
567 
568     drop(arch_guard);
569 
570     // 删除kthread的标志
571     current_pcb.flags().remove(ProcessFlags::KTHREAD);
572     current_pcb.worker_private().take();
573 
574     let mut trap_frame = TrapFrame::new();
575 
576     compiler_fence(Ordering::SeqCst);
577     Syscall::do_execve(path, argv, envp, &mut trap_frame).unwrap_or_else(|e| {
578         panic!(
579             "arch_switch_to_user(): pid: {pid:?}, Failed to execve: , error: {e:?}",
580             pid = current_pcb.pid(),
581             e = e
582         );
583     });
584     compiler_fence(Ordering::SeqCst);
585 
586     // 重要!在这里之后,一定要保证上面的引用计数变量、动态申请的变量、锁的守卫都被drop了,否则可能导致内存安全问题!
587 
588     drop(current_pcb);
589 
590     compiler_fence(Ordering::SeqCst);
591     ready_to_switch_to_user(trap_frame, trap_frame_vaddr.data(), new_rip.data());
592 }
593 
594 /// 由于需要依赖ret来切换到用户态,所以不能inline
595 #[inline(never)]
596 unsafe extern "sysv64" fn ready_to_switch_to_user(
597     trap_frame: TrapFrame,
598     trapframe_vaddr: usize,
599     new_rip: usize,
600 ) -> ! {
601     *(trapframe_vaddr as *mut TrapFrame) = trap_frame;
602     asm!(
603         "swapgs",
604         "mov rsp, {trapframe_vaddr}",
605         "push {new_rip}",
606         "ret",
607         trapframe_vaddr = in(reg) trapframe_vaddr,
608         new_rip = in(reg) new_rip
609     );
610     unreachable!()
611 }
612