signal.rs 11 KB

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