signal.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. use super::{ProcessList, Thread, WaitObject, WaitType};
  2. use crate::{
  3. io::BufferFill,
  4. kernel::{
  5. constants::{SA_RESTORER, SA_SIGINFO},
  6. user::{dataflow::UserBuffer, UserPointer},
  7. },
  8. prelude::*,
  9. };
  10. use alloc::collections::{binary_heap::BinaryHeap, btree_map::BTreeMap};
  11. use arch::{ExtendedContext, InterruptContext};
  12. use bindings::{EFAULT, EINVAL};
  13. use core::{cmp::Reverse, task::Waker};
  14. use eonix_runtime::task::Task;
  15. use eonix_sync::AsProof as _;
  16. use intrusive_collections::UnsafeRef;
  17. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
  18. pub struct Signal(u32);
  19. #[allow(dead_code)]
  20. impl Signal {
  21. pub const SIGHUP: Signal = Signal(1);
  22. pub const SIGINT: Signal = Signal(2);
  23. pub const SIGQUIT: Signal = Signal(3);
  24. pub const SIGILL: Signal = Signal(4);
  25. pub const SIGTRAP: Signal = Signal(5);
  26. pub const SIGABRT: Signal = Signal(6);
  27. pub const SIGIOT: Signal = Signal(6);
  28. pub const SIGBUS: Signal = Signal(7);
  29. pub const SIGFPE: Signal = Signal(8);
  30. pub const SIGKILL: Signal = Signal(9);
  31. pub const SIGUSR1: Signal = Signal(10);
  32. pub const SIGSEGV: Signal = Signal(11);
  33. pub const SIGUSR2: Signal = Signal(12);
  34. pub const SIGPIPE: Signal = Signal(13);
  35. pub const SIGALRM: Signal = Signal(14);
  36. pub const SIGTERM: Signal = Signal(15);
  37. pub const SIGSTKFLT: Signal = Signal(16);
  38. pub const SIGCHLD: Signal = Signal(17);
  39. pub const SIGCONT: Signal = Signal(18);
  40. pub const SIGSTOP: Signal = Signal(19);
  41. pub const SIGTSTP: Signal = Signal(20);
  42. pub const SIGTTIN: Signal = Signal(21);
  43. pub const SIGTTOU: Signal = Signal(22);
  44. pub const SIGURG: Signal = Signal(23);
  45. pub const SIGXCPU: Signal = Signal(24);
  46. pub const SIGXFSZ: Signal = Signal(25);
  47. pub const SIGVTALRM: Signal = Signal(26);
  48. pub const SIGPROF: Signal = Signal(27);
  49. pub const SIGWINCH: Signal = Signal(28);
  50. pub const SIGIO: Signal = Signal(29);
  51. pub const SIGPOLL: Signal = Signal(29);
  52. pub const SIGPWR: Signal = Signal(30);
  53. pub const SIGSYS: Signal = Signal(31);
  54. }
  55. #[derive(Debug, Clone, Copy)]
  56. pub struct SignalAction {
  57. pub sa_handler: usize,
  58. pub sa_flags: usize,
  59. pub sa_restorer: usize,
  60. pub sa_mask: usize,
  61. }
  62. struct SignalListInner {
  63. mask: u64,
  64. pending: BinaryHeap<Reverse<Signal>>,
  65. signal_waker: Option<UnsafeRef<dyn Fn() + Send + Sync>>,
  66. stop_waker: Option<Waker>,
  67. // TODO!!!!!: Signal disposition should be per-process.
  68. handlers: BTreeMap<Signal, SignalAction>,
  69. }
  70. pub struct SignalList {
  71. inner: Spin<SignalListInner>,
  72. }
  73. impl Clone for SignalList {
  74. fn clone(&self) -> Self {
  75. let inner = self.inner.lock();
  76. debug_assert!(
  77. inner.stop_waker.is_none(),
  78. "We should not have a stop waker here"
  79. );
  80. Self {
  81. inner: Spin::new(SignalListInner {
  82. mask: inner.mask,
  83. pending: BinaryHeap::new(),
  84. signal_waker: None,
  85. stop_waker: None,
  86. handlers: inner.handlers.clone(),
  87. }),
  88. }
  89. }
  90. }
  91. #[derive(Debug, Clone, Copy)]
  92. pub enum RaiseResult {
  93. Finished,
  94. Masked,
  95. }
  96. impl Signal {
  97. const fn is_ignore(&self) -> bool {
  98. match *self {
  99. Self::SIGCHLD | Self::SIGURG | Self::SIGWINCH => true,
  100. _ => false,
  101. }
  102. }
  103. pub const fn is_now(&self) -> bool {
  104. match *self {
  105. Self::SIGKILL | Self::SIGSTOP => true,
  106. _ => false,
  107. }
  108. }
  109. pub const fn is_coredump(&self) -> bool {
  110. match *self {
  111. Self::SIGQUIT
  112. | Self::SIGILL
  113. | Self::SIGABRT
  114. | Self::SIGFPE
  115. | Self::SIGSEGV
  116. | Self::SIGBUS
  117. | Self::SIGTRAP
  118. | Self::SIGSYS
  119. | Self::SIGXCPU
  120. | Self::SIGXFSZ => true,
  121. _ => false,
  122. }
  123. }
  124. fn to_mask(&self) -> u64 {
  125. 1 << (self.0 - 1)
  126. }
  127. }
  128. impl TryFrom<u32> for Signal {
  129. type Error = u32;
  130. fn try_from(signum: u32) -> Result<Self, Self::Error> {
  131. if signum > 0 && signum <= 64 {
  132. Ok(Self(signum))
  133. } else {
  134. Err(EINVAL)
  135. }
  136. }
  137. }
  138. impl From<Signal> for u32 {
  139. fn from(signal: Signal) -> Self {
  140. let Signal(signum) = signal;
  141. signum
  142. }
  143. }
  144. impl SignalAction {
  145. fn default_action() -> Self {
  146. Self {
  147. sa_handler: 0,
  148. sa_flags: 0,
  149. sa_restorer: 0,
  150. sa_mask: 0,
  151. }
  152. }
  153. fn is_ignore(&self) -> bool {
  154. const SIG_IGN: usize = 1;
  155. self.sa_handler == SIG_IGN
  156. }
  157. fn is_default(&self) -> bool {
  158. const SIG_DFL: usize = 0;
  159. self.sa_handler == SIG_DFL
  160. }
  161. /// # Might Sleep
  162. fn handle(
  163. &self,
  164. signal: Signal,
  165. old_mask: u64,
  166. int_stack: &mut InterruptContext,
  167. ext_ctx: &mut ExtendedContext,
  168. ) -> KResult<()> {
  169. if self.sa_flags & SA_RESTORER as usize == 0 {
  170. return Err(EINVAL);
  171. }
  172. const CONTEXT_SIZE: usize = size_of::<InterruptContext>()
  173. + size_of::<ExtendedContext>()
  174. + size_of::<usize>() // old_mask
  175. + size_of::<u32>(); // `sa_handler` argument: `signum`
  176. // Save current interrupt context to 128 bytes above current user stack
  177. // and align to 16 bytes. Then we push the return address of the restorer.
  178. // TODO!!!: Determine the size of the return address
  179. let sp = ((int_stack.rsp as usize - 128 - CONTEXT_SIZE) & !0xf) - size_of::<u32>();
  180. let restorer_address: u32 = self.sa_restorer as u32;
  181. let mut stack = UserBuffer::new(sp as *mut u8, CONTEXT_SIZE + size_of::<u32>())?;
  182. stack.copy(&restorer_address)?.ok_or(EFAULT)?; // Restorer address
  183. stack.copy(&u32::from(signal))?.ok_or(EFAULT)?; // Restorer address
  184. stack.copy(&old_mask)?.ok_or(EFAULT)?; // Original signal mask
  185. stack.copy(ext_ctx)?.ok_or(EFAULT)?; // MMX registers
  186. stack.copy(int_stack)?.ok_or(EFAULT)?; // Interrupt stack
  187. int_stack.rip = self.sa_handler as u64;
  188. int_stack.rsp = sp as u64;
  189. Ok(())
  190. }
  191. }
  192. impl SignalListInner {
  193. fn get_mask(&self) -> u64 {
  194. self.mask
  195. }
  196. fn set_mask(&mut self, mask: u64) {
  197. self.mask = mask;
  198. }
  199. fn mask(&mut self, mask: u64) {
  200. self.set_mask(self.mask | mask)
  201. }
  202. fn unmask(&mut self, mask: u64) {
  203. self.set_mask(self.mask & !mask)
  204. }
  205. fn is_masked(&self, signal: Signal) -> bool {
  206. self.mask & signal.to_mask() != 0
  207. }
  208. fn pop(&mut self) -> Option<Signal> {
  209. self.pending.pop().map(|Reverse(signal)| signal)
  210. }
  211. fn raise(&mut self, signal: Signal) -> RaiseResult {
  212. if self.is_masked(signal) {
  213. return RaiseResult::Masked;
  214. }
  215. match self.handlers.get(&signal) {
  216. // Ignore action
  217. Some(handler) if handler.is_ignore() => return RaiseResult::Finished,
  218. // Default action
  219. None if signal.is_ignore() => return RaiseResult::Finished,
  220. _ => {}
  221. }
  222. self.mask(signal.to_mask());
  223. self.pending.push(Reverse(signal));
  224. match signal {
  225. Signal::SIGCONT => {
  226. self.stop_waker.take().map(|waker| waker.wake());
  227. }
  228. _ => {
  229. // If we don't have a waker here, we are not permitted to be woken up.
  230. // We would run in the end anyway.
  231. if let Some(waker) = self.signal_waker.take() {
  232. waker();
  233. }
  234. }
  235. }
  236. return RaiseResult::Finished;
  237. }
  238. }
  239. impl SignalList {
  240. pub fn new() -> Self {
  241. Self {
  242. inner: Spin::new(SignalListInner {
  243. mask: 0,
  244. pending: BinaryHeap::new(),
  245. signal_waker: None,
  246. stop_waker: None,
  247. handlers: BTreeMap::new(),
  248. }),
  249. }
  250. }
  251. pub fn get_mask(&self) -> u64 {
  252. self.inner.lock().get_mask()
  253. }
  254. pub fn set_mask(&self, mask: u64) {
  255. self.inner.lock().set_mask(mask)
  256. }
  257. pub fn mask(&self, mask: u64) {
  258. self.inner.lock().set_mask(mask)
  259. }
  260. pub fn unmask(&self, mask: u64) {
  261. self.inner.lock().unmask(mask)
  262. }
  263. pub fn set_handler(&self, signal: Signal, action: &SignalAction) -> KResult<()> {
  264. if signal.is_now() || action.sa_flags & SA_SIGINFO as usize != 0 {
  265. return Err(EINVAL);
  266. }
  267. let mut inner = self.inner.lock();
  268. if action.is_default() {
  269. inner.handlers.remove(&signal);
  270. } else {
  271. inner.handlers.insert(signal, action.clone());
  272. }
  273. Ok(())
  274. }
  275. pub fn get_handler(&self, signal: Signal) -> SignalAction {
  276. self.inner
  277. .lock()
  278. .handlers
  279. .get(&signal)
  280. .cloned()
  281. .unwrap_or_else(SignalAction::default_action)
  282. }
  283. pub fn set_signal_waker(&self, waker: Option<UnsafeRef<dyn Fn() + Send + Sync>>) {
  284. let mut inner = self.inner.lock();
  285. inner.signal_waker = waker;
  286. }
  287. /// Clear all signals except for `SIG_IGN`.
  288. /// This is used when `execve` is called.
  289. pub fn clear_non_ignore(&self) {
  290. self.inner
  291. .lock()
  292. .handlers
  293. .retain(|_, action| action.is_ignore());
  294. }
  295. /// Clear all pending signals.
  296. /// This is used when `fork` is called.
  297. pub fn clear_pending(&self) {
  298. self.inner.lock().pending.clear()
  299. }
  300. pub fn has_pending_signal(&self) -> bool {
  301. !self.inner.lock().pending.is_empty()
  302. }
  303. /// Do not use this, use `Thread::raise` instead.
  304. pub(super) fn raise(&self, signal: Signal) -> RaiseResult {
  305. self.inner.lock().raise(signal)
  306. }
  307. /// Handle signals in the context of `Thread::current()`.
  308. ///
  309. /// # Safety
  310. /// This function might never return. Caller must make sure that local variables
  311. /// that own resources are dropped before calling this function.
  312. pub fn handle(&self, int_stack: &mut InterruptContext, ext_ctx: &mut ExtendedContext) {
  313. loop {
  314. let signal = {
  315. let signal = match self.inner.lock().pop() {
  316. Some(signal) => signal,
  317. None => return,
  318. };
  319. let handler = self.inner.lock().handlers.get(&signal).cloned();
  320. if let Some(handler) = handler {
  321. if !signal.is_now() {
  322. let old_mask = {
  323. let mut inner = self.inner.lock();
  324. let old_mask = inner.mask;
  325. inner.mask(handler.sa_mask as u64);
  326. old_mask
  327. };
  328. let result = handler.handle(signal, old_mask, int_stack, ext_ctx);
  329. if result.is_err() {
  330. self.inner.lock().set_mask(old_mask);
  331. }
  332. match result {
  333. Err(EFAULT) => self.inner.lock().raise(Signal::SIGSEGV),
  334. Err(_) => self.inner.lock().raise(Signal::SIGSYS),
  335. Ok(()) => return,
  336. };
  337. continue;
  338. }
  339. }
  340. // TODO: The default signal handling process should be atomic.
  341. // Default actions include stopping the thread, continuing the thread and
  342. // terminating the process. All these actions will block the thread or return
  343. // to the thread immediately. So we can unmask these signals now.
  344. self.inner.lock().unmask(signal.to_mask());
  345. signal
  346. };
  347. // Default actions.
  348. match signal {
  349. Signal::SIGSTOP | Signal::SIGTSTP | Signal::SIGTTIN | Signal::SIGTTOU => {
  350. let thread = Thread::current();
  351. if let Some(parent) = thread.process.parent.load() {
  352. parent.notify(
  353. WaitObject {
  354. pid: thread.process.pid,
  355. code: WaitType::Stopped(signal),
  356. },
  357. Task::block_on(ProcessList::get().read()).prove(),
  358. );
  359. }
  360. eonix_preempt::disable();
  361. // `SIGSTOP` can only be waken up by `SIGCONT` or `SIGKILL`.
  362. // SAFETY: Preempt disabled above.
  363. {
  364. let mut inner = self.inner.lock();
  365. let waker = Waker::from(Task::current().clone());
  366. let old_waker = inner.stop_waker.replace(waker);
  367. assert!(old_waker.is_none(), "We should not have a waker here");
  368. }
  369. Task::park_preempt_disabled();
  370. if let Some(parent) = thread.process.parent.load() {
  371. parent.notify(
  372. WaitObject {
  373. pid: thread.process.pid,
  374. code: WaitType::Continued,
  375. },
  376. Task::block_on(ProcessList::get().read()).prove(),
  377. );
  378. }
  379. }
  380. Signal::SIGCONT => {}
  381. Signal::SIGKILL => ProcessList::kill_current(signal),
  382. // Ignored
  383. Signal::SIGCHLD | Signal::SIGURG | Signal::SIGWINCH => {}
  384. // TODO!!!!!!: Check exit status format.
  385. s if s.is_coredump() => ProcessList::kill_current(signal),
  386. signal => ProcessList::kill_current(signal),
  387. }
  388. }
  389. }
  390. /// Load the signal mask, MMX registers and interrupt stack from the user stack.
  391. /// We must be here because `sigreturn` is called. Se we return the value of the register
  392. /// used to store the syscall return value to prevent the original value being clobbered.
  393. pub fn restore(
  394. &self,
  395. int_stack: &mut InterruptContext,
  396. ext_ctx: &mut ExtendedContext,
  397. ) -> KResult<usize> {
  398. let old_mask_vaddr = int_stack.rsp as usize;
  399. let old_mmxregs_vaddr = old_mask_vaddr + size_of::<usize>();
  400. let old_int_stack_vaddr = old_mmxregs_vaddr + size_of::<ExtendedContext>();
  401. let old_mask = UserPointer::<u64>::new_vaddr(old_mask_vaddr)?.read()?;
  402. *ext_ctx = UserPointer::<ExtendedContext>::new_vaddr(old_mmxregs_vaddr)?.read()?;
  403. *int_stack = UserPointer::<InterruptContext>::new_vaddr(old_int_stack_vaddr)?.read()?;
  404. self.inner.lock().set_mask(old_mask);
  405. Ok(int_stack.rax as usize)
  406. }
  407. }