signal.c 11 KB

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