xref: /DragonOS/kernel/src/process/kthread.rs (revision 86ee1395de7c614865236ee15071c3603b794e44)
1 use core::{
2     hint::spin_loop,
3     sync::atomic::{compiler_fence, AtomicBool, Ordering},
4 };
5 
6 use alloc::{
7     boxed::Box,
8     collections::LinkedList,
9     string::{String, ToString},
10     sync::{Arc, Weak},
11 };
12 use atomic_enum::atomic_enum;
13 use log::info;
14 use system_error::SystemError;
15 
16 use crate::{
17     arch::CurrentIrqArch,
18     exception::{irqdesc::IrqAction, InterruptArch},
19     init::initial_kthread::initial_kernel_thread,
20     libs::{once::Once, spinlock::SpinLock},
21     process::{ProcessManager, ProcessState},
22     sched::{schedule, SchedMode},
23 };
24 
25 use super::{fork::CloneFlags, Pid, ProcessControlBlock, ProcessFlags};
26 
27 /// 内核线程的创建任务列表
28 static KTHREAD_CREATE_LIST: SpinLock<LinkedList<Arc<KernelThreadCreateInfo>>> =
29     SpinLock::new(LinkedList::new());
30 
31 static mut KTHREAD_DAEMON_PCB: Option<Arc<ProcessControlBlock>> = None;
32 
33 #[derive(Debug)]
34 pub enum WorkerPrivate {
35     KernelThread(KernelThreadPcbPrivate),
36 }
37 
38 #[allow(dead_code)]
39 impl WorkerPrivate {
40     pub fn kernel_thread(&self) -> Option<&KernelThreadPcbPrivate> {
41         match self {
42             Self::KernelThread(x) => Some(x),
43         }
44     }
45 
46     pub fn kernel_thread_mut(&mut self) -> Option<&mut KernelThreadPcbPrivate> {
47         match self {
48             Self::KernelThread(x) => Some(x),
49         }
50     }
51 }
52 
53 bitflags! {
54     pub struct KernelThreadFlags: u32 {
55         const IS_PER_CPU = 1 << 0;
56         const SHOULD_STOP = 1 << 1;
57         const SHOULD_PARK = 1 << 2;
58     }
59 }
60 
61 #[derive(Debug)]
62 pub struct KernelThreadPcbPrivate {
63     flags: KernelThreadFlags,
64 }
65 
66 #[allow(dead_code)]
67 impl KernelThreadPcbPrivate {
68     pub fn new() -> Self {
69         Self {
70             flags: KernelThreadFlags::empty(),
71         }
72     }
73 
74     pub fn flags(&self) -> &KernelThreadFlags {
75         &self.flags
76     }
77 
78     pub fn flags_mut(&mut self) -> &mut KernelThreadFlags {
79         &mut self.flags
80     }
81 }
82 
83 /// 内核线程的闭包,参数必须与闭包的参数一致,返回值必须是i32
84 ///
85 /// 元组的第一个元素是闭包,第二个元素是闭包的参数对象
86 ///
87 /// 对于非原始类型的参数,需要使用Box包装
88 #[allow(dead_code)]
89 #[allow(clippy::type_complexity)]
90 pub enum KernelThreadClosure {
91     UsizeClosure((Box<dyn Fn(usize) -> i32 + Send + Sync>, usize)),
92     StaticUsizeClosure((&'static fn(usize) -> i32, usize)),
93     EmptyClosure((Box<dyn Fn() -> i32 + Send + Sync>, ())),
94     StaticEmptyClosure((&'static fn() -> i32, ())),
95     IrqThread(
96         (
97             &'static dyn Fn(Arc<IrqAction>) -> Result<(), SystemError>,
98             Arc<IrqAction>,
99         ),
100     ),
101     // 添加其他类型入参的闭包,返回值必须是i32
102 }
103 
104 unsafe impl Send for KernelThreadClosure {}
105 unsafe impl Sync for KernelThreadClosure {}
106 
107 impl KernelThreadClosure {
108     pub fn run(self) -> i32 {
109         match self {
110             Self::UsizeClosure((func, arg)) => func(arg),
111             Self::EmptyClosure((func, _arg)) => func(),
112             Self::StaticUsizeClosure((func, arg)) => func(arg),
113             Self::StaticEmptyClosure((func, _arg)) => func(),
114             Self::IrqThread((func, arg)) => {
115                 func(arg).map(|_| 0).unwrap_or_else(|e| e.to_posix_errno())
116             }
117         }
118     }
119 }
120 
121 pub struct KernelThreadCreateInfo {
122     /// 内核线程的入口函数、传入参数
123     closure: SpinLock<Option<Box<KernelThreadClosure>>>,
124     /// 内核线程的名字
125     name: String,
126     /// 是否已经完成创建 todo:使用comletion机制优化这里
127     created: AtomicKernelThreadCreateStatus,
128     result_pcb: SpinLock<Option<Arc<ProcessControlBlock>>>,
129     /// 不安全的Arc引用计数,当内核线程创建失败时,需要减少这个计数
130     has_unsafe_arc_instance: AtomicBool,
131     self_ref: Weak<Self>,
132     /// 如果该值为true在进入bootstrap stage2之后,就会进入睡眠状态
133     to_mark_sleep: AtomicBool,
134 }
135 
136 #[atomic_enum]
137 #[derive(PartialEq)]
138 pub enum KernelThreadCreateStatus {
139     Created,
140     NotCreated,
141     ErrorOccured,
142 }
143 
144 #[allow(dead_code)]
145 impl KernelThreadCreateInfo {
146     pub fn new(func: KernelThreadClosure, name: String) -> Arc<Self> {
147         let result = Arc::new(Self {
148             closure: SpinLock::new(Some(Box::new(func))),
149             name,
150             created: AtomicKernelThreadCreateStatus::new(KernelThreadCreateStatus::NotCreated),
151             result_pcb: SpinLock::new(None),
152             has_unsafe_arc_instance: AtomicBool::new(false),
153             self_ref: Weak::new(),
154             to_mark_sleep: AtomicBool::new(true),
155         });
156         let tmp = result.clone();
157         unsafe {
158             let tmp = Arc::into_raw(tmp) as *mut Self;
159             (*tmp).self_ref = Arc::downgrade(&result);
160             Arc::from_raw(tmp);
161         }
162 
163         return result;
164     }
165 
166     /// 创建者调用这函数,等待创建完成后,获取创建结果
167     ///
168     /// ## 返回值
169     ///
170     /// - Some(Arc<ProcessControlBlock>) 创建成功,返回新创建的内核线程的PCB
171     /// - None 创建失败
172     pub fn poll_result(&self) -> Option<Arc<ProcessControlBlock>> {
173         loop {
174             match self.created.load(Ordering::SeqCst) {
175                 KernelThreadCreateStatus::Created => {
176                     return self.result_pcb.lock().take();
177                 }
178                 KernelThreadCreateStatus::NotCreated => {
179                     spin_loop();
180                 }
181                 KernelThreadCreateStatus::ErrorOccured => {
182                     // 创建失败,减少不安全的Arc引用计数
183                     let to_delete = self.has_unsafe_arc_instance.swap(false, Ordering::SeqCst);
184                     if to_delete {
185                         let self_ref = self.self_ref.upgrade().unwrap();
186                         unsafe { Arc::decrement_strong_count(&self_ref) };
187                     }
188                     return None;
189                 }
190             }
191         }
192     }
193 
194     pub fn take_closure(&self) -> Option<Box<KernelThreadClosure>> {
195         return self.closure.lock().take();
196     }
197 
198     pub fn name(&self) -> &String {
199         &self.name
200     }
201 
202     pub unsafe fn set_create_ok(&self, pcb: Arc<ProcessControlBlock>) {
203         // todo: 使用completion机制优化这里
204         self.result_pcb.lock().replace(pcb);
205         self.created
206             .store(KernelThreadCreateStatus::Created, Ordering::SeqCst);
207     }
208 
209     /// 生成一个不安全的Arc指针(用于创建内核线程时传递参数)
210     pub fn generate_unsafe_arc_ptr(self: Arc<Self>) -> *const Self {
211         assert!(
212             self.has_unsafe_arc_instance
213                 .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
214                 .is_ok(),
215             "Cannot generate unsafe arc ptr when there is already one."
216         );
217         let ptr = Arc::into_raw(self);
218         return ptr;
219     }
220 
221     pub unsafe fn parse_unsafe_arc_ptr(ptr: *const Self) -> Arc<Self> {
222         let arc = Arc::from_raw(ptr);
223         assert!(
224             arc.has_unsafe_arc_instance
225                 .compare_exchange(true, false, Ordering::SeqCst, Ordering::SeqCst)
226                 .is_ok(),
227             "Cannot parse unsafe arc ptr when there is no one."
228         );
229         assert!(Arc::strong_count(&arc) > 0);
230         return arc;
231     }
232 
233     /// 设置是否在进入bootstrap stage2之后,就进入睡眠状态
234     ///
235     /// ## 参数
236     ///
237     /// - to_mark_sleep: 是否在进入bootstrap stage2之后,就进入睡眠状态
238     ///
239     /// ## 返回值
240     /// 如果已经创建完成,返回EINVAL
241     pub fn set_to_mark_sleep(&self, to_mark_sleep: bool) -> Result<(), SystemError> {
242         let result_guard = self.result_pcb.lock();
243         if result_guard.is_some() {
244             // 已经创建完成,不需要设置
245             return Err(SystemError::EINVAL);
246         }
247         self.to_mark_sleep.store(to_mark_sleep, Ordering::SeqCst);
248         return Ok(());
249     }
250 
251     pub fn to_mark_sleep(&self) -> bool {
252         self.to_mark_sleep.load(Ordering::SeqCst)
253     }
254 }
255 
256 pub struct KernelThreadMechanism;
257 
258 impl KernelThreadMechanism {
259     pub fn init_stage1() {
260         assert!(ProcessManager::current_pcb().pid() == Pid::new(0));
261         info!("Initializing kernel thread mechanism stage1...");
262 
263         // 初始化第一个内核线程
264 
265         let create_info = KernelThreadCreateInfo::new(
266             KernelThreadClosure::EmptyClosure((Box::new(initial_kernel_thread), ())),
267             "init".to_string(),
268         );
269 
270         let irq_guard: crate::exception::IrqFlagsGuard =
271             unsafe { CurrentIrqArch::save_and_disable_irq() };
272         // 由于当前是pid=0的idle进程,而__inner_create要求当前是kthread,所以先临时设置为kthread
273         ProcessManager::current_pcb()
274             .flags
275             .get_mut()
276             .insert(ProcessFlags::KTHREAD);
277         create_info
278             .set_to_mark_sleep(false)
279             .expect("Failed to set to_mark_sleep");
280 
281         KernelThreadMechanism::__inner_create(
282             &create_info,
283             CloneFlags::CLONE_VM | CloneFlags::CLONE_SIGNAL,
284         )
285         .unwrap_or_else(|e| panic!("Failed to create initial kernel thread, error: {:?}", e));
286 
287         ProcessManager::current_pcb()
288             .flags
289             .get_mut()
290             .remove(ProcessFlags::KTHREAD);
291 
292         drop(irq_guard);
293         info!("Initializing kernel thread mechanism stage1 complete");
294     }
295 
296     pub fn init_stage2() {
297         assert!(ProcessManager::current_pcb()
298             .flags()
299             .contains(ProcessFlags::KTHREAD));
300         static INIT: Once = Once::new();
301         INIT.call_once(|| {
302             info!("Initializing kernel thread mechanism stage2...");
303             // 初始化kthreadd
304             let closure = KernelThreadClosure::EmptyClosure((Box::new(Self::kthread_daemon), ()));
305             let info = KernelThreadCreateInfo::new(closure, "kthreadd".to_string());
306             info.set_to_mark_sleep(false)
307                 .expect("kthreadadd should be run first");
308             let kthreadd_pid: Pid = Self::__inner_create(
309                 &info,
310                 CloneFlags::CLONE_VM | CloneFlags::CLONE_FS | CloneFlags::CLONE_SIGNAL,
311             )
312             .expect("Failed to create kthread daemon");
313             let pcb = ProcessManager::find(kthreadd_pid).unwrap();
314             ProcessManager::wakeup(&pcb).expect("Failed to wakeup kthread daemon");
315             unsafe {
316                 KTHREAD_DAEMON_PCB.replace(pcb);
317             }
318             info!("Initialize kernel thread mechanism stage2 complete");
319         });
320     }
321 
322     /// 创建一个新的内核线程
323     ///
324     /// ## 参数
325     ///
326     /// - func: 内核线程的入口函数、传入参数
327     /// - name: 内核线程的名字
328     ///
329     /// ## 返回值
330     ///
331     /// - Some(Arc<ProcessControlBlock>) 创建成功,返回新创建的内核线程的PCB
332     #[allow(dead_code)]
333     pub fn create(func: KernelThreadClosure, name: String) -> Option<Arc<ProcessControlBlock>> {
334         let info = KernelThreadCreateInfo::new(func, name);
335         while unsafe { KTHREAD_DAEMON_PCB.is_none() } {
336             // 等待kthreadd启动
337             spin_loop()
338         }
339         KTHREAD_CREATE_LIST.lock().push_back(info.clone());
340         compiler_fence(Ordering::SeqCst);
341         ProcessManager::wakeup(unsafe { KTHREAD_DAEMON_PCB.as_ref().unwrap() })
342             .expect("Failed to wakeup kthread daemon");
343         return info.poll_result();
344     }
345 
346     /// 创建并运行一个新的内核线程
347     ///
348     /// ## 参数
349     ///
350     /// - func: 内核线程的入口函数、传入参数
351     /// - name: 内核线程的名字
352     ///
353     /// ## 返回值
354     ///
355     /// - Some(Arc<ProcessControlBlock>) 创建成功,返回新创建的内核线程的PCB
356     #[allow(dead_code)]
357     pub fn create_and_run(
358         func: KernelThreadClosure,
359         name: String,
360     ) -> Option<Arc<ProcessControlBlock>> {
361         let pcb = Self::create(func, name)?;
362         ProcessManager::wakeup(&pcb)
363             .unwrap_or_else(|_| panic!("Failed to wakeup kthread: {:?}", pcb.pid()));
364         return Some(pcb);
365     }
366 
367     /// 停止一个内核线程
368     ///
369     /// 如果目标内核线程的数据检查失败,会panic
370     ///
371     /// ## 返回值
372     ///
373     /// - Ok(i32) 目标内核线程的退出码
374     #[allow(dead_code)]
375     pub fn stop(pcb: &Arc<ProcessControlBlock>) -> Result<usize, SystemError> {
376         if !pcb.flags().contains(ProcessFlags::KTHREAD) {
377             panic!("Cannt stop a non-kthread process");
378         }
379 
380         let mut worker_private = pcb.worker_private();
381         assert!(
382             worker_private.is_some(),
383             "kthread stop: worker_private is none, pid: {:?}",
384             pcb.pid()
385         );
386         worker_private
387             .as_mut()
388             .unwrap()
389             .kernel_thread_mut()
390             .expect("Error type of worker private")
391             .flags
392             .insert(KernelThreadFlags::SHOULD_STOP);
393 
394         drop(worker_private);
395 
396         ProcessManager::wakeup(pcb).ok();
397 
398         // 忙等目标内核线程退出
399         // todo: 使用completion机制优化这里
400         loop {
401             if let ProcessState::Exited(code) = pcb.sched_info().inner_lock_read_irqsave().state() {
402                 return Ok(code);
403             }
404             spin_loop();
405         }
406     }
407 
408     /// 判断一个内核线程是否应当停止
409     ///
410     /// ## 参数
411     ///
412     /// - pcb: 目标内核线程的PCB
413     ///
414     /// ## 返回值
415     ///
416     /// - bool 是否应当停止. true表示应当停止,false表示不应当停止. 如果目标进程不是内核线程,返回false
417     ///
418     /// ## Panic
419     ///
420     /// 如果目标内核线程的数据检查失败,会panic
421     #[allow(dead_code)]
422     pub fn should_stop(pcb: &Arc<ProcessControlBlock>) -> bool {
423         if !pcb.flags().contains(ProcessFlags::KTHREAD) {
424             return false;
425         }
426 
427         let worker_private = pcb.worker_private();
428         assert!(
429             worker_private.is_some(),
430             "kthread should_stop: worker_private is none, pid: {:?}",
431             pcb.pid()
432         );
433         return worker_private
434             .as_ref()
435             .unwrap()
436             .kernel_thread()
437             .expect("Error type of worker private")
438             .flags
439             .contains(KernelThreadFlags::SHOULD_STOP);
440     }
441 
442     /// A daemon thread which creates other kernel threads
443     #[inline(never)]
444     fn kthread_daemon() -> i32 {
445         let current_pcb = ProcessManager::current_pcb();
446         {
447             // 初始化worker_private
448             let mut worker_private_guard = current_pcb.worker_private();
449             let worker_private = WorkerPrivate::KernelThread(KernelThreadPcbPrivate::new());
450             *worker_private_guard = Some(worker_private);
451         }
452         // 设置为kthread
453         current_pcb.flags().insert(ProcessFlags::KTHREAD);
454         drop(current_pcb);
455 
456         loop {
457             let mut list = KTHREAD_CREATE_LIST.lock();
458             while let Some(info) = list.pop_front() {
459                 drop(list);
460                 // create a new kernel thread
461                 let result: Result<Pid, SystemError> = Self::__inner_create(
462                     &info,
463                     CloneFlags::CLONE_VM | CloneFlags::CLONE_FS | CloneFlags::CLONE_SIGNAL,
464                 );
465                 if result.is_err() {
466                     // 创建失败
467                     info.created
468                         .store(KernelThreadCreateStatus::ErrorOccured, Ordering::SeqCst);
469                 };
470                 list = KTHREAD_CREATE_LIST.lock();
471             }
472             drop(list);
473 
474             let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
475             ProcessManager::mark_sleep(true).ok();
476             drop(irq_guard);
477             schedule(SchedMode::SM_NONE);
478         }
479     }
480 }
481 
482 /// 内核线程启动的第二阶段
483 ///
484 /// 该函数只能被`kernel_thread_bootstrap_stage1`调用(jmp到该函数)
485 ///
486 /// ## 参数
487 ///
488 /// - ptr: 传入的参数,是一个指向`Arc<KernelThreadCreateInfo>`的指针
489 pub unsafe extern "C" fn kernel_thread_bootstrap_stage2(ptr: *const KernelThreadCreateInfo) -> ! {
490     let info = KernelThreadCreateInfo::parse_unsafe_arc_ptr(ptr);
491 
492     let closure: Box<KernelThreadClosure> = info.take_closure().unwrap();
493     info.set_create_ok(ProcessManager::current_pcb());
494     let to_mark_sleep = info.to_mark_sleep();
495     drop(info);
496 
497     if to_mark_sleep {
498         // 进入睡眠状态
499         let irq_guard = CurrentIrqArch::save_and_disable_irq();
500         ProcessManager::mark_sleep(true).expect("Failed to mark sleep");
501         drop(irq_guard);
502         schedule(SchedMode::SM_NONE);
503     }
504 
505     let mut retval = SystemError::EINTR.to_posix_errno();
506 
507     if !KernelThreadMechanism::should_stop(&ProcessManager::current_pcb()) {
508         retval = closure.run();
509     }
510 
511     ProcessManager::exit(retval as usize);
512 }
513 
514 /// 初始化内核线程机制
515 #[inline(never)]
516 pub fn kthread_init() {
517     static INIT: Once = Once::new();
518     INIT.call_once(|| {
519         KernelThreadMechanism::init_stage1();
520     });
521 }
522