signal.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/ptrace.h"
  6. #include "asm/unistd.h"
  7. #include "asm/uaccess.h"
  8. #include "asm/ucontext.h"
  9. #include "frame_kern.h"
  10. #include "skas.h"
  11. void copy_sc(struct uml_pt_regs *regs, void *from)
  12. {
  13. struct sigcontext *sc = from;
  14. REGS_GS(regs->regs) = sc->gs;
  15. REGS_FS(regs->regs) = sc->fs;
  16. REGS_ES(regs->regs) = sc->es;
  17. REGS_DS(regs->regs) = sc->ds;
  18. REGS_EDI(regs->regs) = sc->edi;
  19. REGS_ESI(regs->regs) = sc->esi;
  20. REGS_EBP(regs->regs) = sc->ebp;
  21. REGS_SP(regs->regs) = sc->esp;
  22. REGS_EBX(regs->regs) = sc->ebx;
  23. REGS_EDX(regs->regs) = sc->edx;
  24. REGS_ECX(regs->regs) = sc->ecx;
  25. REGS_EAX(regs->regs) = sc->eax;
  26. REGS_IP(regs->regs) = sc->eip;
  27. REGS_CS(regs->regs) = sc->cs;
  28. REGS_EFLAGS(regs->regs) = sc->eflags;
  29. REGS_SS(regs->regs) = sc->ss;
  30. }
  31. static int copy_sc_from_user(struct pt_regs *regs,
  32. struct sigcontext __user *from)
  33. {
  34. struct sigcontext sc;
  35. unsigned long fpregs[HOST_FP_SIZE];
  36. int err;
  37. err = copy_from_user(&sc, from, sizeof(sc));
  38. err |= copy_from_user(fpregs, sc.fpstate, sizeof(fpregs));
  39. if (err)
  40. return err;
  41. copy_sc(&regs->regs, &sc);
  42. err = restore_fp_registers(userspace_pid[0], fpregs);
  43. if (err < 0) {
  44. printk(KERN_ERR "copy_sc_from_user_skas - PTRACE_SETFPREGS "
  45. "failed, errno = %d\n", -err);
  46. return err;
  47. }
  48. return 0;
  49. }
  50. static int copy_sc_to_user(struct sigcontext __user *to,
  51. struct _fpstate __user *to_fp, struct pt_regs *regs,
  52. unsigned long sp)
  53. {
  54. struct sigcontext sc;
  55. unsigned long fpregs[HOST_FP_SIZE];
  56. struct faultinfo * fi = &current->thread.arch.faultinfo;
  57. int err;
  58. sc.gs = REGS_GS(regs->regs.regs);
  59. sc.fs = REGS_FS(regs->regs.regs);
  60. sc.es = REGS_ES(regs->regs.regs);
  61. sc.ds = REGS_DS(regs->regs.regs);
  62. sc.edi = REGS_EDI(regs->regs.regs);
  63. sc.esi = REGS_ESI(regs->regs.regs);
  64. sc.ebp = REGS_EBP(regs->regs.regs);
  65. sc.esp = sp;
  66. sc.ebx = REGS_EBX(regs->regs.regs);
  67. sc.edx = REGS_EDX(regs->regs.regs);
  68. sc.ecx = REGS_ECX(regs->regs.regs);
  69. sc.eax = REGS_EAX(regs->regs.regs);
  70. sc.eip = REGS_IP(regs->regs.regs);
  71. sc.cs = REGS_CS(regs->regs.regs);
  72. sc.eflags = REGS_EFLAGS(regs->regs.regs);
  73. sc.esp_at_signal = regs->regs.regs[UESP];
  74. sc.ss = regs->regs.regs[SS];
  75. sc.cr2 = fi->cr2;
  76. sc.err = fi->error_code;
  77. sc.trapno = fi->trap_no;
  78. err = save_fp_registers(userspace_pid[0], fpregs);
  79. if (err < 0) {
  80. printk(KERN_ERR "copy_sc_to_user_skas - PTRACE_GETFPREGS "
  81. "failed, errno = %d\n", err);
  82. return 1;
  83. }
  84. to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1));
  85. sc.fpstate = to_fp;
  86. if (err)
  87. return err;
  88. return copy_to_user(to, &sc, sizeof(sc)) ||
  89. copy_to_user(to_fp, fpregs, sizeof(fpregs));
  90. }
  91. static int copy_ucontext_to_user(struct ucontext __user *uc,
  92. struct _fpstate __user *fp, sigset_t *set,
  93. unsigned long sp)
  94. {
  95. int err = 0;
  96. err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
  97. err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
  98. err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
  99. err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, sp);
  100. err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
  101. return err;
  102. }
  103. struct sigframe
  104. {
  105. char __user *pretcode;
  106. int sig;
  107. struct sigcontext sc;
  108. struct _fpstate fpstate;
  109. unsigned long extramask[_NSIG_WORDS-1];
  110. char retcode[8];
  111. };
  112. struct rt_sigframe
  113. {
  114. char __user *pretcode;
  115. int sig;
  116. struct siginfo __user *pinfo;
  117. void __user *puc;
  118. struct siginfo info;
  119. struct ucontext uc;
  120. struct _fpstate fpstate;
  121. char retcode[8];
  122. };
  123. int setup_signal_stack_sc(unsigned long stack_top, int sig,
  124. struct k_sigaction *ka, struct pt_regs *regs,
  125. sigset_t *mask)
  126. {
  127. struct sigframe __user *frame;
  128. void __user *restorer;
  129. unsigned long save_sp = PT_REGS_SP(regs);
  130. int err = 0;
  131. /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
  132. stack_top = ((stack_top + 4) & -16UL) - 4;
  133. frame = (struct sigframe __user *) stack_top - 1;
  134. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  135. return 1;
  136. restorer = frame->retcode;
  137. if (ka->sa.sa_flags & SA_RESTORER)
  138. restorer = ka->sa.sa_restorer;
  139. /* Update SP now because the page fault handler refuses to extend
  140. * the stack if the faulting address is too far below the current
  141. * SP, which frame now certainly is. If there's an error, the original
  142. * value is restored on the way out.
  143. * When writing the sigcontext to the stack, we have to write the
  144. * original value, so that's passed to copy_sc_to_user, which does
  145. * the right thing with it.
  146. */
  147. PT_REGS_SP(regs) = (unsigned long) frame;
  148. err |= __put_user(restorer, &frame->pretcode);
  149. err |= __put_user(sig, &frame->sig);
  150. err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
  151. err |= __put_user(mask->sig[0], &frame->sc.oldmask);
  152. if (_NSIG_WORDS > 1)
  153. err |= __copy_to_user(&frame->extramask, &mask->sig[1],
  154. sizeof(frame->extramask));
  155. /*
  156. * This is popl %eax ; movl $,%eax ; int $0x80
  157. *
  158. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  159. * reasons and because gdb uses it as a signature to notice
  160. * signal handler stack frames.
  161. */
  162. err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
  163. err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
  164. err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
  165. if (err)
  166. goto err;
  167. PT_REGS_SP(regs) = (unsigned long) frame;
  168. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  169. PT_REGS_EAX(regs) = (unsigned long) sig;
  170. PT_REGS_EDX(regs) = (unsigned long) 0;
  171. PT_REGS_ECX(regs) = (unsigned long) 0;
  172. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  173. ptrace_notify(SIGTRAP);
  174. return 0;
  175. err:
  176. PT_REGS_SP(regs) = save_sp;
  177. return err;
  178. }
  179. int setup_signal_stack_si(unsigned long stack_top, int sig,
  180. struct k_sigaction *ka, struct pt_regs *regs,
  181. siginfo_t *info, sigset_t *mask)
  182. {
  183. struct rt_sigframe __user *frame;
  184. void __user *restorer;
  185. unsigned long save_sp = PT_REGS_SP(regs);
  186. int err = 0;
  187. stack_top &= -8UL;
  188. frame = (struct rt_sigframe __user *) stack_top - 1;
  189. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  190. return 1;
  191. restorer = frame->retcode;
  192. if (ka->sa.sa_flags & SA_RESTORER)
  193. restorer = ka->sa.sa_restorer;
  194. /* See comment above about why this is here */
  195. PT_REGS_SP(regs) = (unsigned long) frame;
  196. err |= __put_user(restorer, &frame->pretcode);
  197. err |= __put_user(sig, &frame->sig);
  198. err |= __put_user(&frame->info, &frame->pinfo);
  199. err |= __put_user(&frame->uc, &frame->puc);
  200. err |= copy_siginfo_to_user(&frame->info, info);
  201. err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
  202. save_sp);
  203. /*
  204. * This is movl $,%eax ; int $0x80
  205. *
  206. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  207. * reasons and because gdb uses it as a signature to notice
  208. * signal handler stack frames.
  209. */
  210. err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
  211. err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
  212. err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
  213. if (err)
  214. goto err;
  215. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  216. PT_REGS_EAX(regs) = (unsigned long) sig;
  217. PT_REGS_EDX(regs) = (unsigned long) &frame->info;
  218. PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
  219. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  220. ptrace_notify(SIGTRAP);
  221. return 0;
  222. err:
  223. PT_REGS_SP(regs) = save_sp;
  224. return err;
  225. }
  226. long sys_sigreturn(struct pt_regs regs)
  227. {
  228. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  229. struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
  230. sigset_t set;
  231. struct sigcontext __user *sc = &frame->sc;
  232. unsigned long __user *oldmask = &sc->oldmask;
  233. unsigned long __user *extramask = frame->extramask;
  234. int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
  235. if (copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
  236. copy_from_user(&set.sig[1], extramask, sig_size))
  237. goto segfault;
  238. sigdelsetmask(&set, ~_BLOCKABLE);
  239. spin_lock_irq(&current->sighand->siglock);
  240. current->blocked = set;
  241. recalc_sigpending();
  242. spin_unlock_irq(&current->sighand->siglock);
  243. if (copy_sc_from_user(&current->thread.regs, sc))
  244. goto segfault;
  245. /* Avoid ERESTART handling */
  246. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  247. return PT_REGS_SYSCALL_RET(&current->thread.regs);
  248. segfault:
  249. force_sig(SIGSEGV, current);
  250. return 0;
  251. }
  252. long sys_rt_sigreturn(struct pt_regs regs)
  253. {
  254. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  255. struct rt_sigframe __user *frame =
  256. (struct rt_sigframe __user *) (sp - 4);
  257. sigset_t set;
  258. struct ucontext __user *uc = &frame->uc;
  259. int sig_size = _NSIG_WORDS * sizeof(unsigned long);
  260. if (copy_from_user(&set, &uc->uc_sigmask, sig_size))
  261. goto segfault;
  262. sigdelsetmask(&set, ~_BLOCKABLE);
  263. spin_lock_irq(&current->sighand->siglock);
  264. current->blocked = set;
  265. recalc_sigpending();
  266. spin_unlock_irq(&current->sighand->siglock);
  267. if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
  268. goto segfault;
  269. /* Avoid ERESTART handling */
  270. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  271. return PT_REGS_SYSCALL_RET(&current->thread.regs);
  272. segfault:
  273. force_sig(SIGSEGV, current);
  274. return 0;
  275. }