signal.c 11 KB

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