signal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 *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 *to, struct _fpstate *to_fp,
  52. struct pt_regs *regs)
  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 = REGS_SP(regs->regs.skas.regs);
  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 *) (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 *from,
  102. int fpsize)
  103. {
  104. struct _fpstate *to_fp, *from_fp;
  105. unsigned long sigs;
  106. int err;
  107. to_fp = to->fpstate;
  108. sigs = to->oldmask;
  109. err = copy_from_user(to, from, sizeof(*to));
  110. from_fp = to->fpstate;
  111. to->oldmask = sigs;
  112. to->fpstate = to_fp;
  113. if(to_fp != NULL)
  114. err |= copy_from_user(to_fp, from_fp, fpsize);
  115. return(err);
  116. }
  117. int copy_sc_to_user_tt(struct sigcontext *to, struct _fpstate *fp,
  118. struct sigcontext *from, int fpsize)
  119. {
  120. struct _fpstate *to_fp, *from_fp;
  121. int err;
  122. to_fp = (fp ? fp : (struct _fpstate *) (to + 1));
  123. from_fp = from->fpstate;
  124. err = copy_to_user(to, from, sizeof(*to));
  125. if(from_fp != NULL){
  126. err |= copy_to_user(&to->fpstate, &to_fp, sizeof(to->fpstate));
  127. err |= copy_to_user(to_fp, from_fp, fpsize);
  128. }
  129. return(err);
  130. }
  131. #endif
  132. static int copy_sc_from_user(struct pt_regs *to, void __user *from)
  133. {
  134. int ret;
  135. ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from,
  136. sizeof(struct _fpstate)),
  137. copy_sc_from_user_skas(to, from));
  138. return(ret);
  139. }
  140. static int copy_sc_to_user(struct sigcontext *to, struct _fpstate *fp,
  141. struct pt_regs *from)
  142. {
  143. return(CHOOSE_MODE(copy_sc_to_user_tt(to, fp, UPT_SC(&from->regs),
  144. sizeof(*fp)),
  145. copy_sc_to_user_skas(to, fp, from)));
  146. }
  147. static int copy_ucontext_to_user(struct ucontext *uc, struct _fpstate *fp,
  148. sigset_t *set, unsigned long sp)
  149. {
  150. int err = 0;
  151. err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
  152. err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
  153. err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
  154. err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs);
  155. err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
  156. return(err);
  157. }
  158. struct sigframe
  159. {
  160. char *pretcode;
  161. int sig;
  162. struct sigcontext sc;
  163. struct _fpstate fpstate;
  164. unsigned long extramask[_NSIG_WORDS-1];
  165. char retcode[8];
  166. };
  167. struct rt_sigframe
  168. {
  169. char *pretcode;
  170. int sig;
  171. struct siginfo *pinfo;
  172. void *puc;
  173. struct siginfo info;
  174. struct ucontext uc;
  175. struct _fpstate fpstate;
  176. char retcode[8];
  177. };
  178. int setup_signal_stack_sc(unsigned long stack_top, int sig,
  179. struct k_sigaction *ka, struct pt_regs *regs,
  180. sigset_t *mask)
  181. {
  182. struct sigframe __user *frame;
  183. void *restorer;
  184. int err = 0;
  185. stack_top &= -8UL;
  186. frame = (struct sigframe *) stack_top - 1;
  187. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  188. return 1;
  189. restorer = (void *) frame->retcode;
  190. if(ka->sa.sa_flags & SA_RESTORER)
  191. restorer = ka->sa.sa_restorer;
  192. err |= __put_user(restorer, &frame->pretcode);
  193. err |= __put_user(sig, &frame->sig);
  194. err |= copy_sc_to_user(&frame->sc, NULL, regs);
  195. err |= __put_user(mask->sig[0], &frame->sc.oldmask);
  196. if (_NSIG_WORDS > 1)
  197. err |= __copy_to_user(&frame->extramask, &mask->sig[1],
  198. sizeof(frame->extramask));
  199. /*
  200. * This is popl %eax ; movl $,%eax ; int $0x80
  201. *
  202. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  203. * reasons and because gdb uses it as a signature to notice
  204. * signal handler stack frames.
  205. */
  206. err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
  207. err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
  208. err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
  209. if(err)
  210. return(err);
  211. PT_REGS_SP(regs) = (unsigned long) frame;
  212. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  213. PT_REGS_EAX(regs) = (unsigned long) sig;
  214. PT_REGS_EDX(regs) = (unsigned long) 0;
  215. PT_REGS_ECX(regs) = (unsigned long) 0;
  216. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  217. ptrace_notify(SIGTRAP);
  218. return(0);
  219. }
  220. int setup_signal_stack_si(unsigned long stack_top, int sig,
  221. struct k_sigaction *ka, struct pt_regs *regs,
  222. siginfo_t *info, sigset_t *mask)
  223. {
  224. struct rt_sigframe __user *frame;
  225. void *restorer;
  226. int err = 0;
  227. stack_top &= -8UL;
  228. frame = (struct rt_sigframe *) stack_top - 1;
  229. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  230. return 1;
  231. restorer = (void *) frame->retcode;
  232. if(ka->sa.sa_flags & SA_RESTORER)
  233. restorer = ka->sa.sa_restorer;
  234. err |= __put_user(restorer, &frame->pretcode);
  235. err |= __put_user(sig, &frame->sig);
  236. err |= __put_user(&frame->info, &frame->pinfo);
  237. err |= __put_user(&frame->uc, &frame->puc);
  238. err |= copy_siginfo_to_user(&frame->info, info);
  239. err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
  240. PT_REGS_SP(regs));
  241. /*
  242. * This is movl $,%eax ; int $0x80
  243. *
  244. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  245. * reasons and because gdb uses it as a signature to notice
  246. * signal handler stack frames.
  247. */
  248. err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
  249. err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
  250. err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
  251. if(err)
  252. return(err);
  253. PT_REGS_SP(regs) = (unsigned long) frame;
  254. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  255. PT_REGS_EAX(regs) = (unsigned long) sig;
  256. PT_REGS_EDX(regs) = (unsigned long) &frame->info;
  257. PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
  258. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  259. ptrace_notify(SIGTRAP);
  260. return(0);
  261. }
  262. long sys_sigreturn(struct pt_regs regs)
  263. {
  264. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  265. struct sigframe __user *frame = (struct sigframe *)(sp - 8);
  266. sigset_t set;
  267. struct sigcontext __user *sc = &frame->sc;
  268. unsigned long __user *oldmask = &sc->oldmask;
  269. unsigned long __user *extramask = frame->extramask;
  270. int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
  271. if(copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
  272. copy_from_user(&set.sig[1], extramask, sig_size))
  273. goto segfault;
  274. sigdelsetmask(&set, ~_BLOCKABLE);
  275. spin_lock_irq(&current->sighand->siglock);
  276. current->blocked = set;
  277. recalc_sigpending();
  278. spin_unlock_irq(&current->sighand->siglock);
  279. if(copy_sc_from_user(&current->thread.regs, sc))
  280. goto segfault;
  281. /* Avoid ERESTART handling */
  282. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  283. return(PT_REGS_SYSCALL_RET(&current->thread.regs));
  284. segfault:
  285. force_sig(SIGSEGV, current);
  286. return 0;
  287. }
  288. long sys_rt_sigreturn(struct pt_regs regs)
  289. {
  290. unsigned long __user sp = PT_REGS_SP(&current->thread.regs);
  291. struct rt_sigframe __user *frame = (struct rt_sigframe *) (sp - 4);
  292. sigset_t set;
  293. struct ucontext __user *uc = &frame->uc;
  294. int sig_size = _NSIG_WORDS * sizeof(unsigned long);
  295. if(copy_from_user(&set, &uc->uc_sigmask, sig_size))
  296. goto segfault;
  297. sigdelsetmask(&set, ~_BLOCKABLE);
  298. spin_lock_irq(&current->sighand->siglock);
  299. current->blocked = set;
  300. recalc_sigpending();
  301. spin_unlock_irq(&current->sighand->siglock);
  302. if(copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
  303. goto segfault;
  304. /* Avoid ERESTART handling */
  305. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  306. return(PT_REGS_SYSCALL_RET(&current->thread.regs));
  307. segfault:
  308. force_sig(SIGSEGV, current);
  309. return 0;
  310. }
  311. /*
  312. * Overrides for Emacs so that we follow Linus's tabbing style.
  313. * Emacs will notice this stuff at the end of the file and automatically
  314. * adjust the settings for this buffer only. This must remain at the end
  315. * of the file.
  316. * ---------------------------------------------------------------------------
  317. * Local variables:
  318. * c-file-style: "linux"
  319. * End:
  320. */