signal.c 9.0 KB

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