signal.c 12 KB

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