xref: /DragonOS/kernel/src/arch/x86_64/process/kthread.rs (revision 7c28051e8c601312d3d0fd7bcb71bc71450d10c0)
1 use alloc::sync::Arc;
2 use system_error::SystemError;
3 
4 use crate::{
5     arch::{
6         interrupt::TrapFrame,
7         process::table::{KERNEL_CS, KERNEL_DS},
8     },
9     process::{
10         fork::CloneFlags,
11         kthread::{kernel_thread_bootstrap_stage2, KernelThreadCreateInfo, KernelThreadMechanism},
12         Pid, ProcessManager,
13     },
14 };
15 
16 impl KernelThreadMechanism {
17     /// 伪造trapframe,创建内核线程
18     ///
19     /// ## 返回值
20     ///
21     /// 返回创建的内核线程的pid
__inner_create( info: &Arc<KernelThreadCreateInfo>, clone_flags: CloneFlags, ) -> Result<Pid, SystemError>22     pub fn __inner_create(
23         info: &Arc<KernelThreadCreateInfo>,
24         clone_flags: CloneFlags,
25     ) -> Result<Pid, SystemError> {
26         // WARNING: If create failed, we must drop the info manually or it will cause memory leak. (refcount will not decrease when create failed)
27         let create_info: *const KernelThreadCreateInfo =
28             KernelThreadCreateInfo::generate_unsafe_arc_ptr(info.clone());
29 
30         let mut frame = TrapFrame::new();
31         frame.rbx = create_info as usize as u64;
32         frame.ds = KERNEL_DS.bits() as u64;
33         frame.es = KERNEL_DS.bits() as u64;
34         frame.cs = KERNEL_CS.bits() as u64;
35         frame.ss = KERNEL_DS.bits() as u64;
36 
37         // 使能中断
38         frame.rflags |= 1 << 9;
39 
40         frame.rip = kernel_thread_bootstrap_stage1 as usize as u64;
41 
42         // fork失败的话,子线程不会执行。否则将导致内存安全问题。
43         let pid = ProcessManager::fork(&frame, clone_flags).inspect_err(|_e| {
44             unsafe { KernelThreadCreateInfo::parse_unsafe_arc_ptr(create_info) };
45         })?;
46 
47         ProcessManager::find(pid)
48             .unwrap()
49             .set_name(info.name().clone());
50 
51         return Ok(pid);
52     }
53 }
54 
55 /// 内核线程引导函数的第一阶段
56 ///
57 /// 当内核线程开始执行时,会先执行这个函数,这个函数会将伪造的trapframe中的数据弹出,然后跳转到第二阶段
58 ///
59 /// 跳转之后,指向Box<KernelThreadClosure>的指针将传入到stage2的函数
60 #[naked]
kernel_thread_bootstrap_stage1()61 pub(super) unsafe extern "sysv64" fn kernel_thread_bootstrap_stage1() {
62     core::arch::naked_asm!(
63         concat!(
64             "
65 
66             pop r15
67             pop r14
68             pop r13
69             pop r12
70             pop r11
71             pop r10
72             pop r9
73             pop r8
74             pop rbx
75             pop rcx
76             pop rdx
77             pop rsi
78             pop rdi
79             pop rbp
80             pop rax
81             mov ds, ax
82             pop rax
83             mov es, ax
84             pop rax
85             add rsp, 0x20
86             popfq
87             add rsp, 0x10
88             mov rdi, rbx
89             jmp {stage2_func}
90             "
91         ),
92         stage2_func = sym kernel_thread_bootstrap_stage2,
93     )
94 }
95