signal.rs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. use log::error;
  2. use crate::{
  3. arch::{interrupt::TrapFrame, sched::sched, CurrentIrqArch},
  4. exception::InterruptArch,
  5. ipc::signal_types::SignalArch,
  6. process::ProcessManager,
  7. };
  8. /// 信号最大值
  9. pub const MAX_SIG_NUM: usize = 64;
  10. #[allow(dead_code)]
  11. #[derive(Eq)]
  12. #[repr(usize)]
  13. #[allow(non_camel_case_types)]
  14. #[atomic_enum]
  15. pub enum Signal {
  16. INVALID = 0,
  17. SIGHUP = 1,
  18. SIGINT,
  19. SIGQUIT,
  20. SIGILL,
  21. SIGTRAP,
  22. /// SIGABRT和SIGIOT共用这个号码
  23. SIGABRT_OR_IOT,
  24. SIGBUS,
  25. SIGFPE,
  26. SIGKILL,
  27. SIGUSR1,
  28. SIGSEGV = 11,
  29. SIGUSR2,
  30. SIGPIPE,
  31. SIGALRM,
  32. SIGTERM,
  33. SIGSTKFLT,
  34. SIGCHLD,
  35. SIGCONT,
  36. SIGSTOP,
  37. SIGTSTP,
  38. SIGTTIN = 21,
  39. SIGTTOU,
  40. SIGURG,
  41. SIGXCPU,
  42. SIGXFSZ,
  43. SIGVTALRM,
  44. SIGPROF,
  45. SIGWINCH,
  46. /// SIGIO和SIGPOLL共用这个号码
  47. SIGIO_OR_POLL,
  48. SIGPWR,
  49. SIGSYS = 31,
  50. SIGRTMIN = 32,
  51. SIGRTMAX = 64,
  52. }
  53. /// 为Signal实现判断相等的trait
  54. impl PartialEq for Signal {
  55. fn eq(&self, other: &Signal) -> bool {
  56. *self as usize == *other as usize
  57. }
  58. }
  59. impl From<usize> for Signal {
  60. fn from(value: usize) -> Self {
  61. if value <= MAX_SIG_NUM {
  62. let ret: Signal = unsafe { core::mem::transmute(value) };
  63. return ret;
  64. } else {
  65. error!("Try to convert an invalid number to Signal");
  66. return Signal::INVALID;
  67. }
  68. }
  69. }
  70. impl Into<usize> for Signal {
  71. fn into(self) -> usize {
  72. self as usize
  73. }
  74. }
  75. impl From<i32> for Signal {
  76. fn from(value: i32) -> Self {
  77. if value < 0 {
  78. error!("Try to convert an invalid number to Signal");
  79. return Signal::INVALID;
  80. } else {
  81. return Self::from(value as usize);
  82. }
  83. }
  84. }
  85. impl Into<SigSet> for Signal {
  86. fn into(self) -> SigSet {
  87. self.into_sigset()
  88. }
  89. }
  90. impl Signal {
  91. /// 判断一个数字是否为可用的信号
  92. #[inline]
  93. pub fn is_valid(&self) -> bool {
  94. return (*self) as usize <= MAX_SIG_NUM;
  95. }
  96. /// const convertor between `Signal` and `SigSet`
  97. pub const fn into_sigset(self) -> SigSet {
  98. SigSet {
  99. bits: (1 << (self as usize - 1) as u64),
  100. }
  101. }
  102. /// 判断一个信号是不是实时信号
  103. ///
  104. /// ## 返回值
  105. ///
  106. /// - `true` 这个信号是实时信号
  107. /// - `false` 这个信号不是实时信号
  108. #[inline]
  109. pub fn is_rt_signal(&self) -> bool {
  110. return (*self) as usize >= Signal::SIGRTMIN.into();
  111. }
  112. /// 调用信号的默认处理函数
  113. pub fn handle_default(&self) {
  114. match self {
  115. Signal::INVALID => {
  116. error!("attempting to handler an Invalid");
  117. }
  118. Signal::SIGHUP => sig_terminate(self.clone()),
  119. Signal::SIGINT => sig_terminate(self.clone()),
  120. Signal::SIGQUIT => sig_terminate_dump(self.clone()),
  121. Signal::SIGILL => sig_terminate_dump(self.clone()),
  122. Signal::SIGTRAP => sig_terminate_dump(self.clone()),
  123. Signal::SIGABRT_OR_IOT => sig_terminate_dump(self.clone()),
  124. Signal::SIGBUS => sig_terminate_dump(self.clone()),
  125. Signal::SIGFPE => sig_terminate_dump(self.clone()),
  126. Signal::SIGKILL => sig_terminate(self.clone()),
  127. Signal::SIGUSR1 => sig_terminate(self.clone()),
  128. Signal::SIGSEGV => sig_terminate_dump(self.clone()),
  129. Signal::SIGUSR2 => sig_terminate(self.clone()),
  130. Signal::SIGPIPE => sig_terminate(self.clone()),
  131. Signal::SIGALRM => sig_terminate(self.clone()),
  132. Signal::SIGTERM => sig_terminate(self.clone()),
  133. Signal::SIGSTKFLT => sig_terminate(self.clone()),
  134. Signal::SIGCHLD => sig_ignore(self.clone()),
  135. Signal::SIGCONT => sig_continue(self.clone()),
  136. Signal::SIGSTOP => sig_stop(self.clone()),
  137. Signal::SIGTSTP => sig_stop(self.clone()),
  138. Signal::SIGTTIN => sig_stop(self.clone()),
  139. Signal::SIGTTOU => sig_stop(self.clone()),
  140. Signal::SIGURG => sig_ignore(self.clone()),
  141. Signal::SIGXCPU => sig_terminate_dump(self.clone()),
  142. Signal::SIGXFSZ => sig_terminate_dump(self.clone()),
  143. Signal::SIGVTALRM => sig_terminate(self.clone()),
  144. Signal::SIGPROF => sig_terminate(self.clone()),
  145. Signal::SIGWINCH => sig_ignore(self.clone()),
  146. Signal::SIGIO_OR_POLL => sig_terminate(self.clone()),
  147. Signal::SIGPWR => sig_terminate(self.clone()),
  148. Signal::SIGSYS => sig_terminate(self.clone()),
  149. Signal::SIGRTMIN => sig_terminate(self.clone()),
  150. Signal::SIGRTMAX => sig_terminate(self.clone()),
  151. }
  152. }
  153. }
  154. /// siginfo中的si_code的可选值
  155. /// 请注意,当这个值小于0时,表示siginfo来自用户态,否则来自内核态
  156. #[derive(Copy, Debug, Clone)]
  157. #[repr(i32)]
  158. pub enum SigCode {
  159. /// sent by kill, sigsend, raise
  160. User = 0,
  161. /// sent by kernel from somewhere
  162. Kernel = 0x80,
  163. /// 通过sigqueue发送
  164. Queue = -1,
  165. /// 定时器过期时发送
  166. Timer = -2,
  167. /// 当实时消息队列的状态发生改变时发送
  168. Mesgq = -3,
  169. /// 当异步IO完成时发送
  170. AsyncIO = -4,
  171. /// sent by queued SIGIO
  172. SigIO = -5,
  173. }
  174. impl SigCode {
  175. /// 为SigCode这个枚举类型实现从i32转换到枚举类型的转换函数
  176. #[allow(dead_code)]
  177. pub fn from_i32(x: i32) -> SigCode {
  178. match x {
  179. 0 => Self::User,
  180. 0x80 => Self::Kernel,
  181. -1 => Self::Queue,
  182. -2 => Self::Timer,
  183. -3 => Self::Mesgq,
  184. -4 => Self::AsyncIO,
  185. -5 => Self::SigIO,
  186. _ => panic!("signal code not valid"),
  187. }
  188. }
  189. }
  190. bitflags! {
  191. #[repr(C,align(8))]
  192. #[derive(Default)]
  193. pub struct SigFlags:u32{
  194. const SA_NOCLDSTOP = 1;
  195. const SA_NOCLDWAIT = 2;
  196. const SA_SIGINFO = 4;
  197. const SA_ONSTACK = 0x08000000;
  198. const SA_RESTART = 0x10000000;
  199. const SA_NODEFER = 0x40000000;
  200. const SA_RESETHAND = 0x80000000;
  201. const SA_RESTORER =0x04000000;
  202. const SA_ALL = Self::SA_NOCLDSTOP.bits()|Self::SA_NOCLDWAIT.bits()|Self::SA_NODEFER.bits()|Self::SA_ONSTACK.bits()|Self::SA_RESETHAND.bits()|Self::SA_RESTART.bits()|Self::SA_SIGINFO.bits()|Self::SA_RESTORER.bits();
  203. }
  204. /// 请注意,sigset 这个bitmap, 第0位表示sig=1的信号。也就是说,Signal-1才是sigset_t中对应的位
  205. #[derive(Default)]
  206. pub struct SigSet:u64{
  207. const SIGHUP = 1<<0;
  208. const SIGINT = 1<<1;
  209. const SIGQUIT = 1<<2;
  210. const SIGILL = 1<<3;
  211. const SIGTRAP = 1<<4;
  212. /// SIGABRT和SIGIOT共用这个号码
  213. const SIGABRT_OR_IOT = 1<<5;
  214. const SIGBUS = 1<<6;
  215. const SIGFPE = 1<<7;
  216. const SIGKILL = 1<<8;
  217. const SIGUSR = 1<<9;
  218. const SIGSEGV = 1<<10;
  219. const SIGUSR2 = 1<<11;
  220. const SIGPIPE = 1<<12;
  221. const SIGALRM = 1<<13;
  222. const SIGTERM = 1<<14;
  223. const SIGSTKFLT= 1<<15;
  224. const SIGCHLD = 1<<16;
  225. const SIGCONT = 1<<17;
  226. const SIGSTOP = 1<<18;
  227. const SIGTSTP = 1<<19;
  228. const SIGTTIN = 1<<20;
  229. const SIGTTOU = 1<<21;
  230. const SIGURG = 1<<22;
  231. const SIGXCPU = 1<<23;
  232. const SIGXFSZ = 1<<24;
  233. const SIGVTALRM= 1<<25;
  234. const SIGPROF = 1<<26;
  235. const SIGWINCH = 1<<27;
  236. /// SIGIO和SIGPOLL共用这个号码
  237. const SIGIO_OR_POLL = 1<<28;
  238. const SIGPWR = 1<<29;
  239. const SIGSYS = 1<<30;
  240. const SIGRTMIN = 1<<31;
  241. // TODO 写上实时信号
  242. const SIGRTMAX = 1<<MAX_SIG_NUM-1;
  243. }
  244. }
  245. /// SIGCHLD si_codes
  246. #[derive(Debug, Clone, Copy, PartialEq, Eq, ToPrimitive)]
  247. #[allow(dead_code)]
  248. pub enum SigChildCode {
  249. /// child has exited
  250. ///
  251. /// CLD_EXITED
  252. Exited = 1,
  253. /// child was killed
  254. ///
  255. /// CLD_KILLED
  256. Killed = 2,
  257. /// child terminated abnormally
  258. ///
  259. /// CLD_DUMPED
  260. Dumped = 3,
  261. /// traced child has trapped
  262. ///
  263. /// CLD_TRAPPED
  264. Trapped = 4,
  265. /// child has stopped
  266. ///
  267. /// CLD_STOPPED
  268. Stopped = 5,
  269. /// stopped child has continued
  270. ///
  271. /// CLD_CONTINUED
  272. Continued = 6,
  273. }
  274. impl Into<i32> for SigChildCode {
  275. fn into(self) -> i32 {
  276. self as i32
  277. }
  278. }
  279. /// 信号默认处理函数——终止进程
  280. fn sig_terminate(sig: Signal) {
  281. ProcessManager::exit(sig as usize);
  282. }
  283. /// 信号默认处理函数——终止进程并生成 core dump
  284. fn sig_terminate_dump(sig: Signal) {
  285. ProcessManager::exit(sig as usize);
  286. // TODO 生成 coredump 文件
  287. }
  288. /// 信号默认处理函数——暂停进程
  289. fn sig_stop(sig: Signal) {
  290. let guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
  291. ProcessManager::mark_stop().unwrap_or_else(|e| {
  292. error!(
  293. "sleep error :{:?},failed to sleep process :{:?}, with signal :{:?}",
  294. e,
  295. ProcessManager::current_pcb(),
  296. sig
  297. );
  298. });
  299. drop(guard);
  300. sched();
  301. // TODO 暂停进程
  302. }
  303. /// 信号默认处理函数——继续进程
  304. fn sig_continue(sig: Signal) {
  305. ProcessManager::wakeup_stop(&ProcessManager::current_pcb()).unwrap_or_else(|_| {
  306. error!(
  307. "Failed to wake up process pid = {:?} with signal :{:?}",
  308. ProcessManager::current_pcb().pid(),
  309. sig
  310. );
  311. });
  312. }
  313. /// 信号默认处理函数——忽略
  314. fn sig_ignore(_sig: Signal) {
  315. return;
  316. }
  317. pub struct RiscV64SignalArch;
  318. impl SignalArch for RiscV64SignalArch {
  319. // TODO: 为RISCV64实现信号处理
  320. // 注意,rv64现在在中断/系统调用返回用户态时,没有进入 irqentry_exit() 函数,
  321. // 到时候实现信号处理时,需要修改中断/系统调用返回用户态的代码,进入 irqentry_exit() 函数
  322. unsafe fn do_signal_or_restart(_frame: &mut TrapFrame) {
  323. todo!()
  324. }
  325. fn sys_rt_sigreturn(_trap_frame: &mut TrapFrame) -> u64 {
  326. todo!()
  327. }
  328. }