process_list.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. use super::{Process, ProcessGroup, Session, Signal, Thread, WaitObject, WaitType};
  2. use crate::{prelude::*, rcu::rcu_sync};
  3. use alloc::{
  4. collections::btree_map::BTreeMap,
  5. sync::{Arc, Weak},
  6. };
  7. use bindings::KERNEL_PML4;
  8. use eonix_sync::{AsProof as _, AsProofMut as _};
  9. use lazy_static::lazy_static;
  10. pub struct ProcessList {
  11. /// The init process.
  12. init: Option<Arc<Process>>,
  13. /// All threads.
  14. threads: BTreeMap<u32, Arc<Thread>>,
  15. /// All processes.
  16. processes: BTreeMap<u32, Weak<Process>>,
  17. /// All process groups.
  18. pgroups: BTreeMap<u32, Weak<ProcessGroup>>,
  19. /// All sessions.
  20. sessions: BTreeMap<u32, Weak<Session>>,
  21. }
  22. lazy_static! {
  23. static ref GLOBAL_PROC_LIST: RwLock<ProcessList> = {
  24. RwLock::new(ProcessList {
  25. init: None,
  26. threads: BTreeMap::new(),
  27. processes: BTreeMap::new(),
  28. pgroups: BTreeMap::new(),
  29. sessions: BTreeMap::new(),
  30. })
  31. };
  32. }
  33. impl ProcessList {
  34. pub fn get() -> &'static RwLock<Self> {
  35. &GLOBAL_PROC_LIST
  36. }
  37. pub fn add_session(&mut self, session: &Arc<Session>) {
  38. self.sessions.insert(session.sid, Arc::downgrade(session));
  39. }
  40. pub fn add_pgroup(&mut self, pgroup: &Arc<ProcessGroup>) {
  41. self.pgroups.insert(pgroup.pgid, Arc::downgrade(pgroup));
  42. }
  43. pub fn add_process(&mut self, process: &Arc<Process>) {
  44. self.processes.insert(process.pid, Arc::downgrade(process));
  45. }
  46. pub fn add_thread(&mut self, thread: &Arc<Thread>) {
  47. self.threads.insert(thread.tid, thread.clone());
  48. }
  49. pub fn kill_current(signal: Signal) -> ! {
  50. unsafe {
  51. let mut process_list = ProcessList::get().lock();
  52. eonix_preempt::disable();
  53. // SAFETY: Preemption disabled.
  54. process_list.do_kill_process(&Thread::current().process, WaitType::Signaled(signal));
  55. }
  56. unsafe {
  57. // SAFETY: Preempt count == 1.
  58. Thread::exit();
  59. }
  60. }
  61. pub fn remove_process(&mut self, pid: u32) {
  62. // Thread group leader has the same tid as the pid.
  63. if let Some(thread) = self.threads.remove(&pid) {
  64. self.processes.remove(&pid);
  65. // SAFETY: We wait until all references are dropped below with `rcu_sync()`.
  66. let session = unsafe { thread.process.session.swap(None) }.unwrap();
  67. let pgroup = unsafe { thread.process.pgroup.swap(None) }.unwrap();
  68. let _parent = unsafe { thread.process.parent.swap(None) }.unwrap();
  69. pgroup.remove_member(pid, self.prove_mut());
  70. rcu_sync();
  71. if Arc::strong_count(&pgroup) == 1 {
  72. self.pgroups.remove(&pgroup.pgid);
  73. }
  74. if Arc::strong_count(&session) == 1 {
  75. self.sessions.remove(&session.sid);
  76. }
  77. } else {
  78. panic!("Process {} not found", pid);
  79. }
  80. }
  81. pub fn set_init_process(&mut self, init: Arc<Process>) {
  82. let old_init = self.init.replace(init);
  83. assert!(old_init.is_none(), "Init process already set");
  84. }
  85. pub fn init_process(&self) -> &Arc<Process> {
  86. self.init.as_ref().unwrap()
  87. }
  88. pub fn try_find_thread(&self, tid: u32) -> Option<&Arc<Thread>> {
  89. self.threads.get(&tid)
  90. }
  91. pub fn try_find_process(&self, pid: u32) -> Option<Arc<Process>> {
  92. self.processes.get(&pid).and_then(Weak::upgrade)
  93. }
  94. pub fn try_find_pgroup(&self, pgid: u32) -> Option<Arc<ProcessGroup>> {
  95. self.pgroups.get(&pgid).and_then(Weak::upgrade)
  96. }
  97. pub fn try_find_session(&self, sid: u32) -> Option<Arc<Session>> {
  98. self.sessions.get(&sid).and_then(Weak::upgrade)
  99. }
  100. /// Make the process a zombie and notify the parent.
  101. /// # Safety
  102. /// This function needs to be called with preemption disabled.
  103. pub unsafe fn do_kill_process(&mut self, process: &Arc<Process>, status: WaitType) {
  104. if process.pid == 1 {
  105. panic!("init exited");
  106. }
  107. let inner = process.inner.access_mut(self.prove_mut());
  108. // TODO!!!!!!: When we are killing multiple threads, we need to wait until all
  109. // the threads are stopped then proceed.
  110. for thread in inner.threads.values().map(|t| t.upgrade().unwrap()) {
  111. assert!(thread.tid == Thread::current().tid);
  112. // TODO: Send SIGKILL to all threads.
  113. thread.files.close_all();
  114. }
  115. // If we are the session leader, we should drop the control terminal.
  116. if process.session(self.prove()).sid == process.pid {
  117. if let Some(terminal) = process.session(self.prove()).drop_control_terminal() {
  118. terminal.drop_session();
  119. }
  120. }
  121. // Release the MMList as well as the page table.
  122. // Before we release the page table, we need to switch to the kernel page table.
  123. arch::set_root_page_table(KERNEL_PML4 as usize);
  124. unsafe {
  125. process.mm_list.release();
  126. }
  127. // Make children orphans (adopted by init)
  128. {
  129. let init = self.init_process();
  130. inner.children.retain(|_, child| {
  131. let child = child.upgrade().unwrap();
  132. // SAFETY: `child.parent` must be ourself. So we don't need to free it.
  133. unsafe { child.parent.swap(Some(init.clone())) };
  134. init.add_child(&child, self.prove_mut());
  135. false
  136. });
  137. }
  138. let mut init_notify = self.init_process().notify_batch();
  139. process
  140. .wait_list
  141. .drain_exited()
  142. .into_iter()
  143. .for_each(|item| init_notify.notify(item));
  144. init_notify.finish(self.prove());
  145. process.parent(self.prove()).notify(
  146. WaitObject {
  147. pid: process.pid,
  148. code: status,
  149. },
  150. self.prove(),
  151. );
  152. }
  153. }