signal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* $Id: signal.c,v 1.7 2000/09/05 21:44:54 davem Exp $
  2. * signal.c: Signal emulation for Solaris
  3. *
  4. * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  5. */
  6. #include <linux/types.h>
  7. #include <linux/smp_lock.h>
  8. #include <linux/errno.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/svr4.h>
  11. #include <asm/string.h>
  12. #include "conv.h"
  13. #include "signal.h"
  14. #define _S(nr) (1L<<((nr)-1))
  15. #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
  16. long linux_to_solaris_signals[] = {
  17. 0,
  18. SOLARIS_SIGHUP, SOLARIS_SIGINT,
  19. SOLARIS_SIGQUIT, SOLARIS_SIGILL,
  20. SOLARIS_SIGTRAP, SOLARIS_SIGIOT,
  21. SOLARIS_SIGEMT, SOLARIS_SIGFPE,
  22. SOLARIS_SIGKILL, SOLARIS_SIGBUS,
  23. SOLARIS_SIGSEGV, SOLARIS_SIGSYS,
  24. SOLARIS_SIGPIPE, SOLARIS_SIGALRM,
  25. SOLARIS_SIGTERM, SOLARIS_SIGURG,
  26. SOLARIS_SIGSTOP, SOLARIS_SIGTSTP,
  27. SOLARIS_SIGCONT, SOLARIS_SIGCLD,
  28. SOLARIS_SIGTTIN, SOLARIS_SIGTTOU,
  29. SOLARIS_SIGPOLL, SOLARIS_SIGXCPU,
  30. SOLARIS_SIGXFSZ, SOLARIS_SIGVTALRM,
  31. SOLARIS_SIGPROF, SOLARIS_SIGWINCH,
  32. SOLARIS_SIGUSR1, SOLARIS_SIGUSR1,
  33. SOLARIS_SIGUSR2, -1,
  34. };
  35. long solaris_to_linux_signals[] = {
  36. 0,
  37. SIGHUP, SIGINT, SIGQUIT, SIGILL,
  38. SIGTRAP, SIGIOT, SIGEMT, SIGFPE,
  39. SIGKILL, SIGBUS, SIGSEGV, SIGSYS,
  40. SIGPIPE, SIGALRM, SIGTERM, SIGUSR1,
  41. SIGUSR2, SIGCHLD, -1, SIGWINCH,
  42. SIGURG, SIGPOLL, SIGSTOP, SIGTSTP,
  43. SIGCONT, SIGTTIN, SIGTTOU, SIGVTALRM,
  44. SIGPROF, SIGXCPU, SIGXFSZ, -1,
  45. -1, -1, -1, -1,
  46. -1, -1, -1, -1,
  47. -1, -1, -1, -1,
  48. };
  49. static inline long mapsig(long sig)
  50. {
  51. if ((unsigned long)sig > SOLARIS_NSIGNALS)
  52. return -EINVAL;
  53. return solaris_to_linux_signals[sig];
  54. }
  55. asmlinkage int solaris_kill(int pid, int sig)
  56. {
  57. int (*sys_kill)(int,int) =
  58. (int (*)(int,int))SYS(kill);
  59. int s = mapsig(sig);
  60. if (s < 0) return s;
  61. return sys_kill(pid, s);
  62. }
  63. static long sig_handler(int sig, u32 arg, int one_shot)
  64. {
  65. struct sigaction sa, old;
  66. int ret;
  67. mm_segment_t old_fs = get_fs();
  68. int (*sys_sigaction)(int,struct sigaction __user *,struct sigaction __user *) =
  69. (int (*)(int,struct sigaction __user *,struct sigaction __user *))SYS(sigaction);
  70. sigemptyset(&sa.sa_mask);
  71. sa.sa_restorer = NULL;
  72. sa.sa_handler = (__sighandler_t)A(arg);
  73. sa.sa_flags = 0;
  74. if (one_shot) sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  75. set_fs (KERNEL_DS);
  76. ret = sys_sigaction(sig, (void __user *)&sa, (void __user *)&old);
  77. set_fs (old_fs);
  78. if (ret < 0) return ret;
  79. return (u32)(unsigned long)old.sa_handler;
  80. }
  81. static inline long solaris_signal(int sig, u32 arg)
  82. {
  83. return sig_handler (sig, arg, 1);
  84. }
  85. static long solaris_sigset(int sig, u32 arg)
  86. {
  87. if (arg != 2) /* HOLD */ {
  88. spin_lock_irq(&current->sighand->siglock);
  89. sigdelsetmask(&current->blocked, _S(sig));
  90. recalc_sigpending();
  91. spin_unlock_irq(&current->sighand->siglock);
  92. return sig_handler (sig, arg, 0);
  93. } else {
  94. spin_lock_irq(&current->sighand->siglock);
  95. sigaddsetmask(&current->blocked, (_S(sig) & ~_BLOCKABLE));
  96. recalc_sigpending();
  97. spin_unlock_irq(&current->sighand->siglock);
  98. return 0;
  99. }
  100. }
  101. static inline long solaris_sighold(int sig)
  102. {
  103. return solaris_sigset(sig, 2);
  104. }
  105. static inline long solaris_sigrelse(int sig)
  106. {
  107. spin_lock_irq(&current->sighand->siglock);
  108. sigdelsetmask(&current->blocked, _S(sig));
  109. recalc_sigpending();
  110. spin_unlock_irq(&current->sighand->siglock);
  111. return 0;
  112. }
  113. static inline long solaris_sigignore(int sig)
  114. {
  115. return sig_handler(sig, (u32)(unsigned long)SIG_IGN, 0);
  116. }
  117. static inline long solaris_sigpause(int sig)
  118. {
  119. printk ("Need to support solaris sigpause\n");
  120. return -ENOSYS;
  121. }
  122. asmlinkage long solaris_sigfunc(int sig, u32 arg)
  123. {
  124. int func = sig & ~0xff;
  125. sig = mapsig(sig & 0xff);
  126. if (sig < 0) return sig;
  127. switch (func) {
  128. case 0: return solaris_signal(sig, arg);
  129. case 0x100: return solaris_sigset(sig, arg);
  130. case 0x200: return solaris_sighold(sig);
  131. case 0x400: return solaris_sigrelse(sig);
  132. case 0x800: return solaris_sigignore(sig);
  133. case 0x1000: return solaris_sigpause(sig);
  134. }
  135. return -EINVAL;
  136. }
  137. typedef struct {
  138. u32 __sigbits[4];
  139. } sol_sigset_t;
  140. static inline int mapin(u32 *p, sigset_t *q)
  141. {
  142. int i;
  143. u32 x;
  144. int sig;
  145. sigemptyset(q);
  146. x = p[0];
  147. for (i = 1; i <= SOLARIS_NSIGNALS; i++) {
  148. if (x & 1) {
  149. sig = solaris_to_linux_signals[i];
  150. if (sig == -1)
  151. return -EINVAL;
  152. sigaddsetmask(q, (1L << (sig - 1)));
  153. }
  154. x >>= 1;
  155. if (i == 32)
  156. x = p[1];
  157. }
  158. return 0;
  159. }
  160. static inline int mapout(sigset_t *q, u32 *p)
  161. {
  162. int i;
  163. int sig;
  164. p[0] = 0;
  165. p[1] = 0;
  166. for (i = 1; i <= 32; i++) {
  167. if (sigismember(q, sigmask(i))) {
  168. sig = linux_to_solaris_signals[i];
  169. if (sig == -1)
  170. return -EINVAL;
  171. if (sig > 32)
  172. p[1] |= 1L << (sig - 33);
  173. else
  174. p[0] |= 1L << (sig - 1);
  175. }
  176. }
  177. return 0;
  178. }
  179. asmlinkage int solaris_sigprocmask(int how, u32 in, u32 out)
  180. {
  181. sigset_t in_s, *ins, out_s, *outs;
  182. mm_segment_t old_fs = get_fs();
  183. int ret;
  184. int (*sys_sigprocmask)(int,sigset_t __user *,sigset_t __user *) =
  185. (int (*)(int,sigset_t __user *,sigset_t __user *))SYS(sigprocmask);
  186. ins = NULL; outs = NULL;
  187. if (in) {
  188. u32 tmp[2];
  189. if (copy_from_user (tmp, (void __user *)A(in), 2*sizeof(u32)))
  190. return -EFAULT;
  191. ins = &in_s;
  192. if (mapin (tmp, ins)) return -EINVAL;
  193. }
  194. if (out) outs = &out_s;
  195. set_fs (KERNEL_DS);
  196. ret = sys_sigprocmask((how == 3) ? SIG_SETMASK : how,
  197. (void __user *)ins, (void __user *)outs);
  198. set_fs (old_fs);
  199. if (ret) return ret;
  200. if (out) {
  201. u32 tmp[4];
  202. tmp[2] = 0; tmp[3] = 0;
  203. if (mapout (outs, tmp)) return -EINVAL;
  204. if (copy_to_user((void __user *)A(out), tmp, 4*sizeof(u32)))
  205. return -EFAULT;
  206. }
  207. return 0;
  208. }
  209. asmlinkage long do_sol_sigsuspend(u32 mask)
  210. {
  211. sigset_t s;
  212. u32 tmp[2];
  213. if (copy_from_user (tmp, (sol_sigset_t __user *)A(mask), 2*sizeof(u32)))
  214. return -EFAULT;
  215. if (mapin (tmp, &s)) return -EINVAL;
  216. return (long)s.sig[0];
  217. }
  218. struct sol_sigaction {
  219. int sa_flags;
  220. u32 sa_handler;
  221. u32 sa_mask[4];
  222. int sa_resv[2];
  223. };
  224. asmlinkage int solaris_sigaction(int sig, u32 act, u32 old)
  225. {
  226. u32 tmp, tmp2[4];
  227. struct sigaction s, s2;
  228. int ret;
  229. mm_segment_t old_fs = get_fs();
  230. struct sol_sigaction __user *p = (void __user *)A(old);
  231. int (*sys_sigaction)(int,struct sigaction __user *,struct sigaction __user *) =
  232. (int (*)(int,struct sigaction __user *,struct sigaction __user *))SYS(sigaction);
  233. sig = mapsig(sig);
  234. if (sig < 0) {
  235. /* We cheat a little bit for Solaris only signals */
  236. if (old && clear_user(p, sizeof(struct sol_sigaction)))
  237. return -EFAULT;
  238. return 0;
  239. }
  240. if (act) {
  241. if (get_user (tmp, &p->sa_flags))
  242. return -EFAULT;
  243. s.sa_flags = 0;
  244. if (tmp & SOLARIS_SA_ONSTACK) s.sa_flags |= SA_STACK;
  245. if (tmp & SOLARIS_SA_RESTART) s.sa_flags |= SA_RESTART;
  246. if (tmp & SOLARIS_SA_NODEFER) s.sa_flags |= SA_NOMASK;
  247. if (tmp & SOLARIS_SA_RESETHAND) s.sa_flags |= SA_ONESHOT;
  248. if (tmp & SOLARIS_SA_NOCLDSTOP) s.sa_flags |= SA_NOCLDSTOP;
  249. if (get_user (tmp, &p->sa_handler) ||
  250. copy_from_user (tmp2, &p->sa_mask, 2*sizeof(u32)))
  251. return -EFAULT;
  252. s.sa_handler = (__sighandler_t)A(tmp);
  253. if (mapin (tmp2, &s.sa_mask)) return -EINVAL;
  254. s.sa_restorer = NULL;
  255. }
  256. set_fs(KERNEL_DS);
  257. ret = sys_sigaction(sig, act ? (void __user *)&s : NULL,
  258. old ? (void __user *)&s2 : NULL);
  259. set_fs(old_fs);
  260. if (ret) return ret;
  261. if (old) {
  262. if (mapout (&s2.sa_mask, tmp2)) return -EINVAL;
  263. tmp = 0; tmp2[2] = 0; tmp2[3] = 0;
  264. if (s2.sa_flags & SA_STACK) tmp |= SOLARIS_SA_ONSTACK;
  265. if (s2.sa_flags & SA_RESTART) tmp |= SOLARIS_SA_RESTART;
  266. if (s2.sa_flags & SA_NOMASK) tmp |= SOLARIS_SA_NODEFER;
  267. if (s2.sa_flags & SA_ONESHOT) tmp |= SOLARIS_SA_RESETHAND;
  268. if (s2.sa_flags & SA_NOCLDSTOP) tmp |= SOLARIS_SA_NOCLDSTOP;
  269. if (put_user (tmp, &p->sa_flags) ||
  270. __put_user ((u32)(unsigned long)s2.sa_handler, &p->sa_handler) ||
  271. copy_to_user (&p->sa_mask, tmp2, 4*sizeof(u32)))
  272. return -EFAULT;
  273. }
  274. return 0;
  275. }
  276. asmlinkage int solaris_sigpending(int which, u32 set)
  277. {
  278. sigset_t s;
  279. u32 tmp[4];
  280. switch (which) {
  281. case 1: /* sigpending */
  282. spin_lock_irq(&current->sighand->siglock);
  283. sigandsets(&s, &current->blocked, &current->pending.signal);
  284. recalc_sigpending();
  285. spin_unlock_irq(&current->sighand->siglock);
  286. break;
  287. case 2: /* sigfillset - I just set signals which have linux equivalents */
  288. sigfillset(&s);
  289. break;
  290. default: return -EINVAL;
  291. }
  292. if (mapout (&s, tmp)) return -EINVAL;
  293. tmp[2] = 0; tmp[3] = 0;
  294. if (copy_to_user ((u32 __user *)A(set), tmp, sizeof(tmp)))
  295. return -EFAULT;
  296. return 0;
  297. }
  298. asmlinkage int solaris_wait(u32 stat_loc)
  299. {
  300. unsigned __user *p = (unsigned __user *)A(stat_loc);
  301. int (*sys_wait4)(pid_t,unsigned __user *, int, struct rusage __user *) =
  302. (int (*)(pid_t,unsigned __user *, int, struct rusage __user *))SYS(wait4);
  303. int ret, status;
  304. ret = sys_wait4(-1, p, WUNTRACED, NULL);
  305. if (ret >= 0 && stat_loc) {
  306. if (get_user (status, p))
  307. return -EFAULT;
  308. if (((status - 1) & 0xffff) < 0xff)
  309. status = linux_to_solaris_signals[status & 0x7f] & 0x7f;
  310. else if ((status & 0xff) == 0x7f)
  311. status = (linux_to_solaris_signals[(status >> 8) & 0xff] << 8) | 0x7f;
  312. if (__put_user (status, p))
  313. return -EFAULT;
  314. }
  315. return ret;
  316. }
  317. asmlinkage int solaris_waitid(int idtype, s32 pid, u32 info, int options)
  318. {
  319. int (*sys_wait4)(pid_t,unsigned __user *, int, struct rusage __user *) =
  320. (int (*)(pid_t,unsigned __user *, int, struct rusage __user *))SYS(wait4);
  321. int opts, status, ret;
  322. switch (idtype) {
  323. case 0: /* P_PID */ break;
  324. case 1: /* P_PGID */ pid = -pid; break;
  325. case 7: /* P_ALL */ pid = -1; break;
  326. default: return -EINVAL;
  327. }
  328. opts = 0;
  329. if (options & SOLARIS_WUNTRACED) opts |= WUNTRACED;
  330. if (options & SOLARIS_WNOHANG) opts |= WNOHANG;
  331. current->state = TASK_RUNNING;
  332. ret = sys_wait4(pid, (unsigned int __user *)A(info), opts, NULL);
  333. if (ret < 0) return ret;
  334. if (info) {
  335. struct sol_siginfo __user *s = (void __user *)A(info);
  336. if (get_user (status, (unsigned int __user *)A(info)))
  337. return -EFAULT;
  338. if (__put_user (SOLARIS_SIGCLD, &s->si_signo) ||
  339. __put_user (ret, &s->_data._proc._pid))
  340. return -EFAULT;
  341. switch (status & 0xff) {
  342. case 0: ret = SOLARIS_CLD_EXITED;
  343. status = (status >> 8) & 0xff;
  344. break;
  345. case 0x7f:
  346. status = (status >> 8) & 0xff;
  347. switch (status) {
  348. case SIGSTOP:
  349. case SIGTSTP: ret = SOLARIS_CLD_STOPPED;
  350. default: ret = SOLARIS_CLD_EXITED;
  351. }
  352. status = linux_to_solaris_signals[status];
  353. break;
  354. default:
  355. if (status & 0x80) ret = SOLARIS_CLD_DUMPED;
  356. else ret = SOLARIS_CLD_KILLED;
  357. status = linux_to_solaris_signals[status & 0x7f];
  358. break;
  359. }
  360. if (__put_user (ret, &s->si_code) ||
  361. __put_user (status, &s->_data._proc._pdata._cld._status))
  362. return -EFAULT;
  363. }
  364. return 0;
  365. }
  366. extern int svr4_setcontext(svr4_ucontext_t *c, struct pt_regs *regs);
  367. extern int svr4_getcontext(svr4_ucontext_t *c, struct pt_regs *regs);
  368. asmlinkage int solaris_context(struct pt_regs *regs)
  369. {
  370. switch ((unsigned)regs->u_regs[UREG_I0]) {
  371. case 0: /* getcontext */
  372. return svr4_getcontext((svr4_ucontext_t *)(long)(u32)regs->u_regs[UREG_I1], regs);
  373. case 1: /* setcontext */
  374. return svr4_setcontext((svr4_ucontext_t *)(long)(u32)regs->u_regs[UREG_I1], regs);
  375. default:
  376. return -EINVAL;
  377. }
  378. }
  379. asmlinkage int solaris_sigaltstack(u32 ss, u32 oss)
  380. {
  381. /* XXX Implement this soon */
  382. return 0;
  383. }