signal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. stack_top &= -8UL;
  194. frame = (struct sigframe __user *) stack_top - 1;
  195. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  196. return 1;
  197. restorer = frame->retcode;
  198. if(ka->sa.sa_flags & SA_RESTORER)
  199. restorer = ka->sa.sa_restorer;
  200. /* Update SP now because the page fault handler refuses to extend
  201. * the stack if the faulting address is too far below the current
  202. * SP, which frame now certainly is. If there's an error, the original
  203. * value is restored on the way out.
  204. * When writing the sigcontext to the stack, we have to write the
  205. * original value, so that's passed to copy_sc_to_user, which does
  206. * the right thing with it.
  207. */
  208. PT_REGS_SP(regs) = (unsigned long) frame;
  209. err |= __put_user(restorer, &frame->pretcode);
  210. err |= __put_user(sig, &frame->sig);
  211. err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
  212. err |= __put_user(mask->sig[0], &frame->sc.oldmask);
  213. if (_NSIG_WORDS > 1)
  214. err |= __copy_to_user(&frame->extramask, &mask->sig[1],
  215. sizeof(frame->extramask));
  216. /*
  217. * This is popl %eax ; movl $,%eax ; int $0x80
  218. *
  219. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  220. * reasons and because gdb uses it as a signature to notice
  221. * signal handler stack frames.
  222. */
  223. err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
  224. err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
  225. err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
  226. if(err)
  227. goto err;
  228. PT_REGS_SP(regs) = (unsigned long) frame;
  229. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  230. PT_REGS_EAX(regs) = (unsigned long) sig;
  231. PT_REGS_EDX(regs) = (unsigned long) 0;
  232. PT_REGS_ECX(regs) = (unsigned long) 0;
  233. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  234. ptrace_notify(SIGTRAP);
  235. return 0;
  236. err:
  237. PT_REGS_SP(regs) = save_sp;
  238. return err;
  239. }
  240. int setup_signal_stack_si(unsigned long stack_top, int sig,
  241. struct k_sigaction *ka, struct pt_regs *regs,
  242. siginfo_t *info, sigset_t *mask)
  243. {
  244. struct rt_sigframe __user *frame;
  245. void __user *restorer;
  246. unsigned long save_sp = PT_REGS_SP(regs);
  247. int err = 0;
  248. stack_top &= -8UL;
  249. frame = (struct rt_sigframe __user *) stack_top - 1;
  250. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  251. return 1;
  252. restorer = frame->retcode;
  253. if(ka->sa.sa_flags & SA_RESTORER)
  254. restorer = ka->sa.sa_restorer;
  255. /* See comment above about why this is here */
  256. PT_REGS_SP(regs) = (unsigned long) frame;
  257. err |= __put_user(restorer, &frame->pretcode);
  258. err |= __put_user(sig, &frame->sig);
  259. err |= __put_user(&frame->info, &frame->pinfo);
  260. err |= __put_user(&frame->uc, &frame->puc);
  261. err |= copy_siginfo_to_user(&frame->info, info);
  262. err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
  263. save_sp);
  264. /*
  265. * This is movl $,%eax ; int $0x80
  266. *
  267. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  268. * reasons and because gdb uses it as a signature to notice
  269. * signal handler stack frames.
  270. */
  271. err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
  272. err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
  273. err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
  274. if(err)
  275. goto err;
  276. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  277. PT_REGS_EAX(regs) = (unsigned long) sig;
  278. PT_REGS_EDX(regs) = (unsigned long) &frame->info;
  279. PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
  280. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  281. ptrace_notify(SIGTRAP);
  282. return 0;
  283. err:
  284. PT_REGS_SP(regs) = save_sp;
  285. return err;
  286. }
  287. long sys_sigreturn(struct pt_regs regs)
  288. {
  289. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  290. struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
  291. sigset_t set;
  292. struct sigcontext __user *sc = &frame->sc;
  293. unsigned long __user *oldmask = &sc->oldmask;
  294. unsigned long __user *extramask = frame->extramask;
  295. int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
  296. if(copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
  297. copy_from_user(&set.sig[1], extramask, sig_size))
  298. goto segfault;
  299. sigdelsetmask(&set, ~_BLOCKABLE);
  300. spin_lock_irq(&current->sighand->siglock);
  301. current->blocked = set;
  302. recalc_sigpending();
  303. spin_unlock_irq(&current->sighand->siglock);
  304. if(copy_sc_from_user(&current->thread.regs, sc))
  305. goto segfault;
  306. /* Avoid ERESTART handling */
  307. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  308. return(PT_REGS_SYSCALL_RET(&current->thread.regs));
  309. segfault:
  310. force_sig(SIGSEGV, current);
  311. return 0;
  312. }
  313. long sys_rt_sigreturn(struct pt_regs regs)
  314. {
  315. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  316. struct rt_sigframe __user *frame = (struct rt_sigframe __user *) (sp - 4);
  317. sigset_t set;
  318. struct ucontext __user *uc = &frame->uc;
  319. int sig_size = _NSIG_WORDS * sizeof(unsigned long);
  320. if(copy_from_user(&set, &uc->uc_sigmask, sig_size))
  321. goto segfault;
  322. sigdelsetmask(&set, ~_BLOCKABLE);
  323. spin_lock_irq(&current->sighand->siglock);
  324. current->blocked = set;
  325. recalc_sigpending();
  326. spin_unlock_irq(&current->sighand->siglock);
  327. if(copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
  328. goto segfault;
  329. /* Avoid ERESTART handling */
  330. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  331. return(PT_REGS_SYSCALL_RET(&current->thread.regs));
  332. segfault:
  333. force_sig(SIGSEGV, current);
  334. return 0;
  335. }
  336. /*
  337. * Overrides for Emacs so that we follow Linus's tabbing style.
  338. * Emacs will notice this stuff at the end of the file and automatically
  339. * adjust the settings for this buffer only. This must remain at the end
  340. * of the file.
  341. * ---------------------------------------------------------------------------
  342. * Local variables:
  343. * c-file-style: "linux"
  344. * End:
  345. */