signal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 "mode.h"
  15. #ifdef CONFIG_MODE_SKAS
  16. #include "skas.h"
  17. void copy_sc(union uml_pt_regs *regs, void *from)
  18. {
  19. struct sigcontext *sc = from;
  20. REGS_GS(regs->skas.regs) = sc->gs;
  21. REGS_FS(regs->skas.regs) = sc->fs;
  22. REGS_ES(regs->skas.regs) = sc->es;
  23. REGS_DS(regs->skas.regs) = sc->ds;
  24. REGS_EDI(regs->skas.regs) = sc->edi;
  25. REGS_ESI(regs->skas.regs) = sc->esi;
  26. REGS_EBP(regs->skas.regs) = sc->ebp;
  27. REGS_SP(regs->skas.regs) = sc->esp;
  28. REGS_EBX(regs->skas.regs) = sc->ebx;
  29. REGS_EDX(regs->skas.regs) = sc->edx;
  30. REGS_ECX(regs->skas.regs) = sc->ecx;
  31. REGS_EAX(regs->skas.regs) = sc->eax;
  32. REGS_IP(regs->skas.regs) = sc->eip;
  33. REGS_CS(regs->skas.regs) = sc->cs;
  34. REGS_EFLAGS(regs->skas.regs) = sc->eflags;
  35. REGS_SS(regs->skas.regs) = sc->ss;
  36. }
  37. static int copy_sc_from_user_skas(struct pt_regs *regs,
  38. struct sigcontext __user *from)
  39. {
  40. struct sigcontext sc;
  41. unsigned long fpregs[HOST_FP_SIZE];
  42. int err;
  43. err = copy_from_user(&sc, from, sizeof(sc));
  44. err |= copy_from_user(fpregs, sc.fpstate, sizeof(fpregs));
  45. if(err)
  46. return err;
  47. copy_sc(&regs->regs, &sc);
  48. err = restore_fp_registers(userspace_pid[0], fpregs);
  49. if(err < 0) {
  50. printk("copy_sc_from_user_skas - PTRACE_SETFPREGS failed, "
  51. "errno = %d\n", -err);
  52. return err;
  53. }
  54. return 0;
  55. }
  56. int copy_sc_to_user_skas(struct sigcontext __user *to, struct _fpstate __user *to_fp,
  57. struct pt_regs *regs, unsigned long sp)
  58. {
  59. struct sigcontext sc;
  60. unsigned long fpregs[HOST_FP_SIZE];
  61. struct faultinfo * fi = &current->thread.arch.faultinfo;
  62. int err;
  63. sc.gs = REGS_GS(regs->regs.skas.regs);
  64. sc.fs = REGS_FS(regs->regs.skas.regs);
  65. sc.es = REGS_ES(regs->regs.skas.regs);
  66. sc.ds = REGS_DS(regs->regs.skas.regs);
  67. sc.edi = REGS_EDI(regs->regs.skas.regs);
  68. sc.esi = REGS_ESI(regs->regs.skas.regs);
  69. sc.ebp = REGS_EBP(regs->regs.skas.regs);
  70. sc.esp = sp;
  71. sc.ebx = REGS_EBX(regs->regs.skas.regs);
  72. sc.edx = REGS_EDX(regs->regs.skas.regs);
  73. sc.ecx = REGS_ECX(regs->regs.skas.regs);
  74. sc.eax = REGS_EAX(regs->regs.skas.regs);
  75. sc.eip = REGS_IP(regs->regs.skas.regs);
  76. sc.cs = REGS_CS(regs->regs.skas.regs);
  77. sc.eflags = REGS_EFLAGS(regs->regs.skas.regs);
  78. sc.esp_at_signal = regs->regs.skas.regs[UESP];
  79. sc.ss = regs->regs.skas.regs[SS];
  80. sc.cr2 = fi->cr2;
  81. sc.err = fi->error_code;
  82. sc.trapno = fi->trap_no;
  83. err = save_fp_registers(userspace_pid[0], fpregs);
  84. if(err < 0){
  85. printk("copy_sc_to_user_skas - PTRACE_GETFPREGS failed, "
  86. "errno = %d\n", err);
  87. return 1;
  88. }
  89. to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1));
  90. sc.fpstate = to_fp;
  91. if(err)
  92. return err;
  93. return copy_to_user(to, &sc, sizeof(sc)) ||
  94. copy_to_user(to_fp, fpregs, sizeof(fpregs));
  95. }
  96. #endif
  97. #ifdef CONFIG_MODE_TT
  98. /* These copy a sigcontext to/from userspace. They copy the fpstate pointer,
  99. * blowing away the old, good one. So, that value is saved, and then restored
  100. * after the sigcontext copy. In copy_from, the variable holding the saved
  101. * fpstate pointer, and the sigcontext that it should be restored to are both
  102. * in the kernel, so we can just restore using an assignment. In copy_to, the
  103. * saved pointer is in the kernel, but the sigcontext is in userspace, so we
  104. * copy_to_user it.
  105. */
  106. int copy_sc_from_user_tt(struct sigcontext *to, struct sigcontext __user *from,
  107. int fpsize)
  108. {
  109. struct _fpstate *to_fp;
  110. struct _fpstate __user *from_fp;
  111. unsigned long sigs;
  112. int err;
  113. to_fp = to->fpstate;
  114. sigs = to->oldmask;
  115. err = copy_from_user(to, from, sizeof(*to));
  116. from_fp = to->fpstate;
  117. to->oldmask = sigs;
  118. to->fpstate = to_fp;
  119. if(to_fp != NULL)
  120. err |= copy_from_user(to_fp, from_fp, fpsize);
  121. return err;
  122. }
  123. int copy_sc_to_user_tt(struct sigcontext __user *to, struct _fpstate __user *fp,
  124. struct sigcontext *from, int fpsize, unsigned long sp)
  125. {
  126. struct _fpstate __user *to_fp;
  127. struct _fpstate *from_fp;
  128. int err;
  129. to_fp = (fp ? fp : (struct _fpstate __user *) (to + 1));
  130. from_fp = from->fpstate;
  131. err = copy_to_user(to, from, sizeof(*to));
  132. /* The SP in the sigcontext is the updated one for the signal
  133. * delivery. The sp passed in is the original, and this needs
  134. * to be restored, so we stick it in separately.
  135. */
  136. err |= copy_to_user(&SC_SP(to), &sp, sizeof(sp));
  137. if(from_fp != NULL){
  138. err |= copy_to_user(&to->fpstate, &to_fp, sizeof(to->fpstate));
  139. err |= copy_to_user(to_fp, from_fp, fpsize);
  140. }
  141. return err;
  142. }
  143. #endif
  144. static int copy_sc_from_user(struct pt_regs *to, void __user *from)
  145. {
  146. int ret;
  147. ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from,
  148. sizeof(struct _fpstate)),
  149. copy_sc_from_user_skas(to, from));
  150. return ret;
  151. }
  152. static int copy_sc_to_user(struct sigcontext __user *to, struct _fpstate __user *fp,
  153. struct pt_regs *from, unsigned long sp)
  154. {
  155. return CHOOSE_MODE(copy_sc_to_user_tt(to, fp, UPT_SC(&from->regs),
  156. sizeof(*fp), sp),
  157. copy_sc_to_user_skas(to, fp, from, sp));
  158. }
  159. static int copy_ucontext_to_user(struct ucontext __user *uc, struct _fpstate __user *fp,
  160. sigset_t *set, unsigned long sp)
  161. {
  162. int err = 0;
  163. err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
  164. err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
  165. err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
  166. err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, sp);
  167. err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
  168. return err;
  169. }
  170. struct sigframe
  171. {
  172. char __user *pretcode;
  173. int sig;
  174. struct sigcontext sc;
  175. struct _fpstate fpstate;
  176. unsigned long extramask[_NSIG_WORDS-1];
  177. char retcode[8];
  178. };
  179. struct rt_sigframe
  180. {
  181. char __user *pretcode;
  182. int sig;
  183. struct siginfo __user *pinfo;
  184. void __user *puc;
  185. struct siginfo info;
  186. struct ucontext uc;
  187. struct _fpstate fpstate;
  188. char retcode[8];
  189. };
  190. int setup_signal_stack_sc(unsigned long stack_top, int sig,
  191. struct k_sigaction *ka, struct pt_regs *regs,
  192. sigset_t *mask)
  193. {
  194. struct sigframe __user *frame;
  195. void __user *restorer;
  196. unsigned long save_sp = PT_REGS_SP(regs);
  197. int err = 0;
  198. /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
  199. stack_top = ((stack_top + 4) & -16UL) - 4;
  200. frame = (struct sigframe __user *) stack_top - 1;
  201. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  202. return 1;
  203. restorer = frame->retcode;
  204. if(ka->sa.sa_flags & SA_RESTORER)
  205. restorer = ka->sa.sa_restorer;
  206. /* Update SP now because the page fault handler refuses to extend
  207. * the stack if the faulting address is too far below the current
  208. * SP, which frame now certainly is. If there's an error, the original
  209. * value is restored on the way out.
  210. * When writing the sigcontext to the stack, we have to write the
  211. * original value, so that's passed to copy_sc_to_user, which does
  212. * the right thing with it.
  213. */
  214. PT_REGS_SP(regs) = (unsigned long) frame;
  215. err |= __put_user(restorer, &frame->pretcode);
  216. err |= __put_user(sig, &frame->sig);
  217. err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
  218. err |= __put_user(mask->sig[0], &frame->sc.oldmask);
  219. if (_NSIG_WORDS > 1)
  220. err |= __copy_to_user(&frame->extramask, &mask->sig[1],
  221. sizeof(frame->extramask));
  222. /*
  223. * This is popl %eax ; movl $,%eax ; int $0x80
  224. *
  225. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  226. * reasons and because gdb uses it as a signature to notice
  227. * signal handler stack frames.
  228. */
  229. err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
  230. err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
  231. err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
  232. if(err)
  233. goto err;
  234. PT_REGS_SP(regs) = (unsigned long) frame;
  235. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  236. PT_REGS_EAX(regs) = (unsigned long) sig;
  237. PT_REGS_EDX(regs) = (unsigned long) 0;
  238. PT_REGS_ECX(regs) = (unsigned long) 0;
  239. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  240. ptrace_notify(SIGTRAP);
  241. return 0;
  242. err:
  243. PT_REGS_SP(regs) = save_sp;
  244. return err;
  245. }
  246. int setup_signal_stack_si(unsigned long stack_top, int sig,
  247. struct k_sigaction *ka, struct pt_regs *regs,
  248. siginfo_t *info, sigset_t *mask)
  249. {
  250. struct rt_sigframe __user *frame;
  251. void __user *restorer;
  252. unsigned long save_sp = PT_REGS_SP(regs);
  253. int err = 0;
  254. stack_top &= -8UL;
  255. frame = (struct rt_sigframe __user *) stack_top - 1;
  256. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  257. return 1;
  258. restorer = frame->retcode;
  259. if(ka->sa.sa_flags & SA_RESTORER)
  260. restorer = ka->sa.sa_restorer;
  261. /* See comment above about why this is here */
  262. PT_REGS_SP(regs) = (unsigned long) frame;
  263. err |= __put_user(restorer, &frame->pretcode);
  264. err |= __put_user(sig, &frame->sig);
  265. err |= __put_user(&frame->info, &frame->pinfo);
  266. err |= __put_user(&frame->uc, &frame->puc);
  267. err |= copy_siginfo_to_user(&frame->info, info);
  268. err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
  269. save_sp);
  270. /*
  271. * This is movl $,%eax ; int $0x80
  272. *
  273. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  274. * reasons and because gdb uses it as a signature to notice
  275. * signal handler stack frames.
  276. */
  277. err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
  278. err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
  279. err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
  280. if(err)
  281. goto err;
  282. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  283. PT_REGS_EAX(regs) = (unsigned long) sig;
  284. PT_REGS_EDX(regs) = (unsigned long) &frame->info;
  285. PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
  286. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  287. ptrace_notify(SIGTRAP);
  288. return 0;
  289. err:
  290. PT_REGS_SP(regs) = save_sp;
  291. return err;
  292. }
  293. long sys_sigreturn(struct pt_regs regs)
  294. {
  295. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  296. struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
  297. sigset_t set;
  298. struct sigcontext __user *sc = &frame->sc;
  299. unsigned long __user *oldmask = &sc->oldmask;
  300. unsigned long __user *extramask = frame->extramask;
  301. int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
  302. if(copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
  303. copy_from_user(&set.sig[1], extramask, sig_size))
  304. goto segfault;
  305. sigdelsetmask(&set, ~_BLOCKABLE);
  306. spin_lock_irq(&current->sighand->siglock);
  307. current->blocked = set;
  308. recalc_sigpending();
  309. spin_unlock_irq(&current->sighand->siglock);
  310. if(copy_sc_from_user(&current->thread.regs, sc))
  311. goto segfault;
  312. /* Avoid ERESTART handling */
  313. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  314. return PT_REGS_SYSCALL_RET(&current->thread.regs);
  315. segfault:
  316. force_sig(SIGSEGV, current);
  317. return 0;
  318. }
  319. long sys_rt_sigreturn(struct pt_regs regs)
  320. {
  321. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  322. struct rt_sigframe __user *frame = (struct rt_sigframe __user *) (sp - 4);
  323. sigset_t set;
  324. struct ucontext __user *uc = &frame->uc;
  325. int sig_size = _NSIG_WORDS * sizeof(unsigned long);
  326. if(copy_from_user(&set, &uc->uc_sigmask, sig_size))
  327. goto segfault;
  328. sigdelsetmask(&set, ~_BLOCKABLE);
  329. spin_lock_irq(&current->sighand->siglock);
  330. current->blocked = set;
  331. recalc_sigpending();
  332. spin_unlock_irq(&current->sighand->siglock);
  333. if(copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
  334. goto segfault;
  335. /* Avoid ERESTART handling */
  336. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  337. return PT_REGS_SYSCALL_RET(&current->thread.regs);
  338. segfault:
  339. force_sig(SIGSEGV, current);
  340. return 0;
  341. }