signal.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. mod signal_action;
  2. use super::{ProcessList, Thread, WaitObject, WaitType};
  3. use crate::kernel::constants::{EFAULT, EINVAL};
  4. use crate::{kernel::user::UserPointer, prelude::*};
  5. use alloc::collections::binary_heap::BinaryHeap;
  6. use alloc::sync::Arc;
  7. use core::{cmp::Reverse, task::Waker};
  8. use eonix_hal::fpu::FpuState;
  9. use eonix_hal::traits::trap::RawTrapContext;
  10. use eonix_hal::trap::TrapContext;
  11. use eonix_runtime::scheduler::Runtime;
  12. use eonix_sync::AsProof as _;
  13. use intrusive_collections::UnsafeRef;
  14. use posix_types::signal::{SigSet, Signal};
  15. use posix_types::{SIGNAL_IGNORE, SIGNAL_NOW, SIGNAL_STOP};
  16. use signal_action::SignalActionList;
  17. pub use signal_action::SignalAction;
  18. pub(self) const SAVED_DATA_SIZE: usize =
  19. size_of::<TrapContext>() + size_of::<FpuState>() + size_of::<SigSet>();
  20. struct SignalListInner {
  21. mask: SigSet,
  22. pending: BinaryHeap<Reverse<Signal>>,
  23. signal_waker: Option<UnsafeRef<dyn Fn() + Send + Sync>>,
  24. stop_waker: Option<Waker>,
  25. // TODO!!!!!: Signal disposition should be per-process.
  26. actions: Arc<SignalActionList>,
  27. }
  28. pub struct SignalList {
  29. inner: Spin<SignalListInner>,
  30. }
  31. impl Clone for SignalList {
  32. fn clone(&self) -> Self {
  33. let inner = self.inner.lock();
  34. debug_assert!(
  35. inner.stop_waker.is_none(),
  36. "We should not have a stop waker here"
  37. );
  38. Self {
  39. inner: Spin::new(SignalListInner {
  40. mask: inner.mask,
  41. pending: BinaryHeap::new(),
  42. signal_waker: None,
  43. stop_waker: None,
  44. actions: inner.actions.clone(),
  45. }),
  46. }
  47. }
  48. }
  49. #[derive(Debug, Clone, Copy)]
  50. pub enum RaiseResult {
  51. Finished,
  52. Masked,
  53. }
  54. impl SignalListInner {
  55. fn pop(&mut self) -> Option<Signal> {
  56. self.pending.pop().map(|Reverse(signal)| signal)
  57. }
  58. fn raise(&mut self, signal: Signal) -> RaiseResult {
  59. if self.mask.include(signal) {
  60. return RaiseResult::Masked;
  61. }
  62. match (signal, self.actions.get(signal)) {
  63. (_, SignalAction::Ignore) => {}
  64. (SIGNAL_IGNORE!(), SignalAction::Default) => {}
  65. _ => {
  66. self.mask.mask(SigSet::from(signal));
  67. self.pending.push(Reverse(signal));
  68. if matches!(signal, Signal::SIGCONT) {
  69. self.stop_waker.take().map(|waker| waker.wake());
  70. } else {
  71. // If we don't have a waker here, we are not permitted to be woken up.
  72. // We would run in the end anyway.
  73. if let Some(waker) = self.signal_waker.take() {
  74. waker();
  75. }
  76. }
  77. }
  78. }
  79. RaiseResult::Finished
  80. }
  81. }
  82. impl SignalList {
  83. pub fn new() -> Self {
  84. Self {
  85. inner: Spin::new(SignalListInner {
  86. mask: SigSet::empty(),
  87. pending: BinaryHeap::new(),
  88. signal_waker: None,
  89. stop_waker: None,
  90. actions: Arc::new(SignalActionList::new()),
  91. }),
  92. }
  93. }
  94. pub fn get_mask(&self) -> SigSet {
  95. self.inner.lock().mask
  96. }
  97. pub fn set_mask(&self, mask: SigSet) {
  98. self.inner.lock().mask = mask;
  99. }
  100. pub fn mask(&self, mask: SigSet) {
  101. self.inner.lock().mask.mask(mask)
  102. }
  103. pub fn unmask(&self, mask: SigSet) {
  104. self.inner.lock().mask.unmask(mask)
  105. }
  106. pub fn set_action(&self, signal: Signal, action: SignalAction) -> KResult<()> {
  107. if matches!(signal, SIGNAL_NOW!()) {
  108. return Err(EINVAL);
  109. }
  110. self.inner.lock().actions.set(signal, action);
  111. Ok(())
  112. }
  113. pub fn get_action(&self, signal: Signal) -> SignalAction {
  114. self.inner.lock().actions.get(signal)
  115. }
  116. pub fn set_signal_waker(&self, waker: Option<UnsafeRef<dyn Fn() + Send + Sync>>) {
  117. let mut inner = self.inner.lock();
  118. inner.signal_waker = waker;
  119. }
  120. /// Clear all signals except for `SIG_IGN`.
  121. /// This is used when `execve` is called.
  122. pub fn clear_non_ignore(&self) {
  123. self.inner.lock().actions.remove_non_ignore();
  124. }
  125. /// Clear all pending signals.
  126. /// This is used when `fork` is called.
  127. pub fn clear_pending(&self) {
  128. self.inner.lock().pending.clear()
  129. }
  130. pub fn has_pending_signal(&self) -> bool {
  131. !self.inner.lock().pending.is_empty()
  132. }
  133. /// Do not use this, use `Thread::raise` instead.
  134. pub(super) fn raise(&self, signal: Signal) -> RaiseResult {
  135. self.inner.lock().raise(signal)
  136. }
  137. /// Handle signals in the context of `Thread::current()`.
  138. pub async fn handle(&self, trap_ctx: &mut TrapContext, fpu_state: &mut FpuState) {
  139. loop {
  140. let signal = {
  141. let signal = match self.inner.lock().pop() {
  142. Some(signal) => signal,
  143. None => return,
  144. };
  145. let handler = self.inner.lock().actions.get(signal);
  146. if let SignalAction::SimpleHandler { mask, .. } = &handler {
  147. let old_mask = {
  148. let mut inner = self.inner.lock();
  149. let old_mask = inner.mask;
  150. inner.mask.mask(*mask);
  151. old_mask
  152. };
  153. let result = handler.handle(signal, old_mask, trap_ctx, fpu_state);
  154. if result.is_err() {
  155. self.inner.lock().mask = old_mask;
  156. }
  157. match result {
  158. Err(EFAULT) => self.inner.lock().raise(Signal::SIGSEGV),
  159. Err(_) => self.inner.lock().raise(Signal::SIGSYS),
  160. Ok(()) => return,
  161. };
  162. continue;
  163. }
  164. // TODO: The default signal handling process should be atomic.
  165. // Default actions include stopping the thread, continuing the thread and
  166. // terminating the process. All these actions will block the thread or return
  167. // to the thread immediately. So we can unmask these signals now.
  168. self.inner.lock().mask.unmask(SigSet::from(signal));
  169. signal
  170. };
  171. // Default actions.
  172. match signal {
  173. SIGNAL_IGNORE!() => {}
  174. Signal::SIGCONT => {
  175. // SIGCONT wakeup is done in `raise()`. So no further action needed here.
  176. }
  177. SIGNAL_STOP!() => {
  178. let thread = Thread::current();
  179. if let Some(parent) = thread.process.parent.load() {
  180. parent.notify(
  181. Some(Signal::SIGCHLD),
  182. WaitObject {
  183. pid: thread.process.pid,
  184. code: WaitType::Stopped(signal),
  185. },
  186. ProcessList::get().read().await.prove(),
  187. );
  188. }
  189. eonix_preempt::disable();
  190. // `SIGSTOP` can only be waken up by `SIGCONT` or `SIGKILL`.
  191. // SAFETY: Preempt disabled above.
  192. Runtime::block_till_woken(|waker| {
  193. let mut inner = self.inner.lock();
  194. let old_waker = inner.stop_waker.replace(waker.clone());
  195. assert!(old_waker.is_none(), "We should not have a waker here");
  196. })
  197. .await;
  198. if let Some(parent) = thread.process.parent.load() {
  199. parent.notify(
  200. Some(Signal::SIGCHLD),
  201. WaitObject {
  202. pid: thread.process.pid,
  203. code: WaitType::Continued,
  204. },
  205. ProcessList::get().read().await.prove(),
  206. );
  207. }
  208. }
  209. signal => {
  210. // Default to terminate the thread.
  211. Thread::current().force_kill(signal).await;
  212. return;
  213. }
  214. }
  215. }
  216. }
  217. /// Load the signal mask, fpu state and trap context from the user stack.
  218. pub fn restore(
  219. &self,
  220. trap_ctx: &mut TrapContext,
  221. fpu_state: &mut FpuState,
  222. old_sigreturn: bool,
  223. ) -> KResult<()> {
  224. #[cfg(not(any(
  225. target_arch = "x86_64",
  226. target_arch = "riscv64",
  227. target_arch = "loongarch64"
  228. )))]
  229. compile_error!("`restore` is not implemented for this architecture");
  230. #[cfg(target_arch = "x86_64")]
  231. let old_trap_ctx_vaddr = {
  232. let mut old_trap_ctx_vaddr = trap_ctx.get_stack_pointer() + 16;
  233. if old_sigreturn {
  234. // Old sigreturn will pop 4 bytes off the stack. We sub them back.
  235. use posix_types::ctypes::Long;
  236. old_trap_ctx_vaddr -= size_of::<Long>();
  237. }
  238. old_trap_ctx_vaddr
  239. };
  240. #[cfg(any(target_arch = "riscv64", target_arch = "loongarch64"))]
  241. let old_trap_ctx_vaddr = {
  242. debug_assert!(
  243. !old_sigreturn,
  244. "Old sigreturn is not supported on RISC-V and LoongArch64"
  245. );
  246. trap_ctx.get_stack_pointer()
  247. };
  248. let old_fpu_state_vaddr = old_trap_ctx_vaddr + size_of::<TrapContext>();
  249. let old_mask_vaddr = old_fpu_state_vaddr + size_of::<FpuState>();
  250. *trap_ctx = UserPointer::<TrapContext>::with_addr(old_trap_ctx_vaddr)?.read()?;
  251. // Make sure that at least we won't crash the kernel.
  252. if !trap_ctx.is_user_mode() || !trap_ctx.is_interrupt_enabled() {
  253. return Err(EFAULT)?;
  254. }
  255. *fpu_state = UserPointer::<FpuState>::with_addr(old_fpu_state_vaddr)?.read()?;
  256. self.inner.lock().mask = UserPointer::<SigSet>::with_addr(old_mask_vaddr)?.read()?;
  257. Ok(())
  258. }
  259. }
  260. impl SignalList {
  261. pub fn new_cloned(other: &Self) -> Self {
  262. let inner = other.inner.lock();
  263. debug_assert!(
  264. inner.stop_waker.is_none(),
  265. "We should not have a stop waker here"
  266. );
  267. Self {
  268. inner: Spin::new(SignalListInner {
  269. mask: inner.mask,
  270. pending: BinaryHeap::new(),
  271. signal_waker: None,
  272. stop_waker: None,
  273. actions: SignalActionList::new_cloned(&inner.actions),
  274. }),
  275. }
  276. }
  277. // shared only signal actions
  278. pub fn new_shared(other: &Self) -> Self {
  279. let inner = other.inner.lock();
  280. debug_assert!(
  281. inner.stop_waker.is_none(),
  282. "We should not have a stop waker here"
  283. );
  284. Self {
  285. inner: Spin::new(SignalListInner {
  286. mask: inner.mask,
  287. pending: BinaryHeap::new(),
  288. signal_waker: None,
  289. stop_waker: None,
  290. actions: SignalActionList::new_shared(&inner.actions),
  291. }),
  292. }
  293. }
  294. }