signal.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (C) 2003 PathScale, Inc.
  3. * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Licensed under the GPL
  5. */
  6. #include <linux/personality.h>
  7. #include <linux/ptrace.h>
  8. #include <linux/kernel.h>
  9. #include <asm/unistd.h>
  10. #include <asm/uaccess.h>
  11. #include <asm/ucontext.h>
  12. #include "frame_kern.h"
  13. #include "skas.h"
  14. void copy_sc(struct uml_pt_regs *regs, void *from)
  15. {
  16. struct sigcontext *sc = from;
  17. #define GETREG(regs, regno, sc, regname) \
  18. (regs)->gp[(regno) / sizeof(unsigned long)] = (sc)->regname
  19. GETREG(regs, R8, sc, r8);
  20. GETREG(regs, R9, sc, r9);
  21. GETREG(regs, R10, sc, r10);
  22. GETREG(regs, R11, sc, r11);
  23. GETREG(regs, R12, sc, r12);
  24. GETREG(regs, R13, sc, r13);
  25. GETREG(regs, R14, sc, r14);
  26. GETREG(regs, R15, sc, r15);
  27. GETREG(regs, RDI, sc, di);
  28. GETREG(regs, RSI, sc, si);
  29. GETREG(regs, RBP, sc, bp);
  30. GETREG(regs, RBX, sc, bx);
  31. GETREG(regs, RDX, sc, dx);
  32. GETREG(regs, RAX, sc, ax);
  33. GETREG(regs, RCX, sc, cx);
  34. GETREG(regs, RSP, sc, sp);
  35. GETREG(regs, RIP, sc, ip);
  36. GETREG(regs, EFLAGS, sc, flags);
  37. GETREG(regs, CS, sc, cs);
  38. #undef GETREG
  39. }
  40. static int copy_sc_from_user(struct pt_regs *regs,
  41. struct sigcontext __user *from,
  42. struct _fpstate __user *fpp)
  43. {
  44. struct user_i387_struct fp;
  45. int err = 0;
  46. #define GETREG(regs, regno, sc, regname) \
  47. __get_user((regs)->regs.gp[(regno) / sizeof(unsigned long)], \
  48. &(sc)->regname)
  49. err |= GETREG(regs, R8, from, r8);
  50. err |= GETREG(regs, R9, from, r9);
  51. err |= GETREG(regs, R10, from, r10);
  52. err |= GETREG(regs, R11, from, r11);
  53. err |= GETREG(regs, R12, from, r12);
  54. err |= GETREG(regs, R13, from, r13);
  55. err |= GETREG(regs, R14, from, r14);
  56. err |= GETREG(regs, R15, from, r15);
  57. err |= GETREG(regs, RDI, from, di);
  58. err |= GETREG(regs, RSI, from, si);
  59. err |= GETREG(regs, RBP, from, bp);
  60. err |= GETREG(regs, RBX, from, bx);
  61. err |= GETREG(regs, RDX, from, dx);
  62. err |= GETREG(regs, RAX, from, ax);
  63. err |= GETREG(regs, RCX, from, cx);
  64. err |= GETREG(regs, RSP, from, sp);
  65. err |= GETREG(regs, RIP, from, ip);
  66. err |= GETREG(regs, EFLAGS, from, flags);
  67. err |= GETREG(regs, CS, from, cs);
  68. if (err)
  69. return 1;
  70. #undef GETREG
  71. err = copy_from_user(&fp, fpp, sizeof(struct user_i387_struct));
  72. if (err)
  73. return 1;
  74. err = restore_fp_registers(userspace_pid[current_thread_info()->cpu],
  75. (unsigned long *) &fp);
  76. if (err < 0) {
  77. printk(KERN_ERR "copy_sc_from_user - "
  78. "restore_fp_registers failed, errno = %d\n",
  79. -err);
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. static int copy_sc_to_user(struct sigcontext __user *to,
  85. struct _fpstate __user *to_fp, struct pt_regs *regs,
  86. unsigned long mask, unsigned long sp)
  87. {
  88. struct faultinfo * fi = &current->thread.arch.faultinfo;
  89. struct user_i387_struct fp;
  90. int err = 0;
  91. err |= __put_user(0, &to->gs);
  92. err |= __put_user(0, &to->fs);
  93. #define PUTREG(regs, regno, sc, regname) \
  94. __put_user((regs)->regs.gp[(regno) / sizeof(unsigned long)], \
  95. &(sc)->regname)
  96. err |= PUTREG(regs, RDI, to, di);
  97. err |= PUTREG(regs, RSI, to, si);
  98. err |= PUTREG(regs, RBP, to, bp);
  99. /*
  100. * Must use original RSP, which is passed in, rather than what's in
  101. * the pt_regs, because that's already been updated to point at the
  102. * signal frame.
  103. */
  104. err |= __put_user(sp, &to->sp);
  105. err |= PUTREG(regs, RBX, to, bx);
  106. err |= PUTREG(regs, RDX, to, dx);
  107. err |= PUTREG(regs, RCX, to, cx);
  108. err |= PUTREG(regs, RAX, to, ax);
  109. err |= PUTREG(regs, R8, to, r8);
  110. err |= PUTREG(regs, R9, to, r9);
  111. err |= PUTREG(regs, R10, to, r10);
  112. err |= PUTREG(regs, R11, to, r11);
  113. err |= PUTREG(regs, R12, to, r12);
  114. err |= PUTREG(regs, R13, to, r13);
  115. err |= PUTREG(regs, R14, to, r14);
  116. err |= PUTREG(regs, R15, to, r15);
  117. err |= PUTREG(regs, CS, to, cs); /* XXX x86_64 doesn't do this */
  118. err |= __put_user(fi->cr2, &to->cr2);
  119. err |= __put_user(fi->error_code, &to->err);
  120. err |= __put_user(fi->trap_no, &to->trapno);
  121. err |= PUTREG(regs, RIP, to, ip);
  122. err |= PUTREG(regs, EFLAGS, to, flags);
  123. #undef PUTREG
  124. err |= __put_user(mask, &to->oldmask);
  125. if (err)
  126. return 1;
  127. err = save_fp_registers(userspace_pid[current_thread_info()->cpu],
  128. (unsigned long *) &fp);
  129. if (err < 0) {
  130. printk(KERN_ERR "copy_sc_from_user - restore_fp_registers "
  131. "failed, errno = %d\n", -err);
  132. return 1;
  133. }
  134. if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct)))
  135. return 1;
  136. return err;
  137. }
  138. struct rt_sigframe
  139. {
  140. char __user *pretcode;
  141. struct ucontext uc;
  142. struct siginfo info;
  143. struct _fpstate fpstate;
  144. };
  145. int setup_signal_stack_si(unsigned long stack_top, int sig,
  146. struct k_sigaction *ka, struct pt_regs * regs,
  147. siginfo_t *info, sigset_t *set)
  148. {
  149. struct rt_sigframe __user *frame;
  150. unsigned long save_sp = PT_REGS_RSP(regs);
  151. int err = 0;
  152. struct task_struct *me = current;
  153. frame = (struct rt_sigframe __user *)
  154. round_down(stack_top - sizeof(struct rt_sigframe), 16);
  155. /* Subtract 128 for a red zone and 8 for proper alignment */
  156. frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
  157. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  158. goto out;
  159. if (ka->sa.sa_flags & SA_SIGINFO) {
  160. err |= copy_siginfo_to_user(&frame->info, info);
  161. if (err)
  162. goto out;
  163. }
  164. /*
  165. * Update SP now because the page fault handler refuses to extend
  166. * the stack if the faulting address is too far below the current
  167. * SP, which frame now certainly is. If there's an error, the original
  168. * value is restored on the way out.
  169. * When writing the sigcontext to the stack, we have to write the
  170. * original value, so that's passed to copy_sc_to_user, which does
  171. * the right thing with it.
  172. */
  173. PT_REGS_RSP(regs) = (unsigned long) frame;
  174. /* Create the ucontext. */
  175. err |= __put_user(0, &frame->uc.uc_flags);
  176. err |= __put_user(0, &frame->uc.uc_link);
  177. err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  178. err |= __put_user(sas_ss_flags(save_sp),
  179. &frame->uc.uc_stack.ss_flags);
  180. err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
  181. err |= copy_sc_to_user(&frame->uc.uc_mcontext, &frame->fpstate, regs,
  182. set->sig[0], save_sp);
  183. err |= __put_user(&frame->fpstate, &frame->uc.uc_mcontext.fpstate);
  184. if (sizeof(*set) == 16) {
  185. __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
  186. __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
  187. }
  188. else
  189. err |= __copy_to_user(&frame->uc.uc_sigmask, set,
  190. sizeof(*set));
  191. /*
  192. * Set up to return from userspace. If provided, use a stub
  193. * already in userspace.
  194. */
  195. /* x86-64 should always use SA_RESTORER. */
  196. if (ka->sa.sa_flags & SA_RESTORER)
  197. err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
  198. else
  199. /* could use a vstub here */
  200. goto restore_sp;
  201. if (err)
  202. goto restore_sp;
  203. /* Set up registers for signal handler */
  204. {
  205. struct exec_domain *ed = current_thread_info()->exec_domain;
  206. if (unlikely(ed && ed->signal_invmap && sig < 32))
  207. sig = ed->signal_invmap[sig];
  208. }
  209. PT_REGS_RDI(regs) = sig;
  210. /* In case the signal handler was declared without prototypes */
  211. PT_REGS_RAX(regs) = 0;
  212. /*
  213. * This also works for non SA_SIGINFO handlers because they expect the
  214. * next argument after the signal number on the stack.
  215. */
  216. PT_REGS_RSI(regs) = (unsigned long) &frame->info;
  217. PT_REGS_RDX(regs) = (unsigned long) &frame->uc;
  218. PT_REGS_RIP(regs) = (unsigned long) ka->sa.sa_handler;
  219. out:
  220. return err;
  221. restore_sp:
  222. PT_REGS_RSP(regs) = save_sp;
  223. return err;
  224. }
  225. long sys_rt_sigreturn(struct pt_regs *regs)
  226. {
  227. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  228. struct rt_sigframe __user *frame =
  229. (struct rt_sigframe __user *)(sp - 8);
  230. struct ucontext __user *uc = &frame->uc;
  231. sigset_t set;
  232. if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
  233. goto segfault;
  234. sigdelsetmask(&set, ~_BLOCKABLE);
  235. spin_lock_irq(&current->sighand->siglock);
  236. current->blocked = set;
  237. recalc_sigpending();
  238. spin_unlock_irq(&current->sighand->siglock);
  239. if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext,
  240. &frame->fpstate))
  241. goto segfault;
  242. /* Avoid ERESTART handling */
  243. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  244. return PT_REGS_SYSCALL_RET(&current->thread.regs);
  245. segfault:
  246. force_sig(SIGSEGV, current);
  247. return 0;
  248. }