signal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/ptrace.h>
  6. #include <asm/unistd.h>
  7. #include <asm/uaccess.h>
  8. #include <asm/ucontext.h>
  9. #include "frame_kern.h"
  10. #include "skas.h"
  11. void copy_sc(struct uml_pt_regs *regs, void *from)
  12. {
  13. struct sigcontext *sc = from;
  14. REGS_GS(regs->gp) = sc->gs;
  15. REGS_FS(regs->gp) = sc->fs;
  16. REGS_ES(regs->gp) = sc->es;
  17. REGS_DS(regs->gp) = sc->ds;
  18. REGS_EDI(regs->gp) = sc->di;
  19. REGS_ESI(regs->gp) = sc->si;
  20. REGS_EBP(regs->gp) = sc->bp;
  21. REGS_SP(regs->gp) = sc->sp;
  22. REGS_EBX(regs->gp) = sc->bx;
  23. REGS_EDX(regs->gp) = sc->dx;
  24. REGS_ECX(regs->gp) = sc->cx;
  25. REGS_EAX(regs->gp) = sc->ax;
  26. REGS_IP(regs->gp) = sc->ip;
  27. REGS_CS(regs->gp) = sc->cs;
  28. REGS_EFLAGS(regs->gp) = sc->flags;
  29. REGS_SS(regs->gp) = sc->ss;
  30. }
  31. /*
  32. * FPU tag word conversions.
  33. */
  34. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  35. {
  36. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  37. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  38. tmp = ~twd;
  39. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  40. /* and move the valid bits to the lower byte. */
  41. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  42. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  43. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  44. return tmp;
  45. }
  46. static inline unsigned long twd_fxsr_to_i387(struct user_fxsr_struct *fxsave)
  47. {
  48. struct _fpxreg *st = NULL;
  49. unsigned long twd = (unsigned long) fxsave->twd;
  50. unsigned long tag;
  51. unsigned long ret = 0xffff0000;
  52. int i;
  53. #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
  54. for (i = 0; i < 8; i++) {
  55. if (twd & 0x1) {
  56. st = (struct _fpxreg *) FPREG_ADDR(fxsave, i);
  57. switch (st->exponent & 0x7fff) {
  58. case 0x7fff:
  59. tag = 2; /* Special */
  60. break;
  61. case 0x0000:
  62. if ( !st->significand[0] &&
  63. !st->significand[1] &&
  64. !st->significand[2] &&
  65. !st->significand[3] ) {
  66. tag = 1; /* Zero */
  67. } else {
  68. tag = 2; /* Special */
  69. }
  70. break;
  71. default:
  72. if (st->significand[3] & 0x8000) {
  73. tag = 0; /* Valid */
  74. } else {
  75. tag = 2; /* Special */
  76. }
  77. break;
  78. }
  79. } else {
  80. tag = 3; /* Empty */
  81. }
  82. ret |= (tag << (2 * i));
  83. twd = twd >> 1;
  84. }
  85. return ret;
  86. }
  87. static int convert_fxsr_to_user(struct _fpstate __user *buf,
  88. struct user_fxsr_struct *fxsave)
  89. {
  90. unsigned long env[7];
  91. struct _fpreg __user *to;
  92. struct _fpxreg *from;
  93. int i;
  94. env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
  95. env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
  96. env[2] = twd_fxsr_to_i387(fxsave);
  97. env[3] = fxsave->fip;
  98. env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
  99. env[5] = fxsave->foo;
  100. env[6] = fxsave->fos;
  101. if (__copy_to_user(buf, env, 7 * sizeof(unsigned long)))
  102. return 1;
  103. to = &buf->_st[0];
  104. from = (struct _fpxreg *) &fxsave->st_space[0];
  105. for (i = 0; i < 8; i++, to++, from++) {
  106. unsigned long __user *t = (unsigned long __user *)to;
  107. unsigned long *f = (unsigned long *)from;
  108. if (__put_user(*f, t) ||
  109. __put_user(*(f + 1), t + 1) ||
  110. __put_user(from->exponent, &to->exponent))
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. static int convert_fxsr_from_user(struct user_fxsr_struct *fxsave,
  116. struct _fpstate __user *buf)
  117. {
  118. unsigned long env[7];
  119. struct _fpxreg *to;
  120. struct _fpreg __user *from;
  121. int i;
  122. if (copy_from_user( env, buf, 7 * sizeof(long)))
  123. return 1;
  124. fxsave->cwd = (unsigned short)(env[0] & 0xffff);
  125. fxsave->swd = (unsigned short)(env[1] & 0xffff);
  126. fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
  127. fxsave->fip = env[3];
  128. fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
  129. fxsave->fcs = (env[4] & 0xffff);
  130. fxsave->foo = env[5];
  131. fxsave->fos = env[6];
  132. to = (struct _fpxreg *) &fxsave->st_space[0];
  133. from = &buf->_st[0];
  134. for (i = 0; i < 8; i++, to++, from++) {
  135. unsigned long *t = (unsigned long *)to;
  136. unsigned long __user *f = (unsigned long __user *)from;
  137. if (__get_user(*t, f) ||
  138. __get_user(*(t + 1), f + 1) ||
  139. __get_user(to->exponent, &from->exponent))
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. extern int have_fpx_regs;
  145. static int copy_sc_from_user(struct pt_regs *regs,
  146. struct sigcontext __user *from)
  147. {
  148. struct sigcontext sc;
  149. int err, pid;
  150. err = copy_from_user(&sc, from, sizeof(sc));
  151. if (err)
  152. return err;
  153. pid = userspace_pid[current_thread_info()->cpu];
  154. copy_sc(&regs->regs, &sc);
  155. if (have_fpx_regs) {
  156. struct user_fxsr_struct fpx;
  157. err = copy_from_user(&fpx,
  158. &((struct _fpstate __user *)sc.fpstate)->_fxsr_env[0],
  159. sizeof(struct user_fxsr_struct));
  160. if (err)
  161. return 1;
  162. err = convert_fxsr_from_user(&fpx, sc.fpstate);
  163. if (err)
  164. return 1;
  165. err = restore_fpx_registers(pid, (unsigned long *) &fpx);
  166. if (err < 0) {
  167. printk(KERN_ERR "copy_sc_from_user - "
  168. "restore_fpx_registers failed, errno = %d\n",
  169. -err);
  170. return 1;
  171. }
  172. }
  173. else {
  174. struct user_i387_struct fp;
  175. err = copy_from_user(&fp, sc.fpstate,
  176. sizeof(struct user_i387_struct));
  177. if (err)
  178. return 1;
  179. err = restore_fp_registers(pid, (unsigned long *) &fp);
  180. if (err < 0) {
  181. printk(KERN_ERR "copy_sc_from_user - "
  182. "restore_fp_registers failed, errno = %d\n",
  183. -err);
  184. return 1;
  185. }
  186. }
  187. return 0;
  188. }
  189. static int copy_sc_to_user(struct sigcontext __user *to,
  190. struct _fpstate __user *to_fp, struct pt_regs *regs,
  191. unsigned long sp)
  192. {
  193. struct sigcontext sc;
  194. struct faultinfo * fi = &current->thread.arch.faultinfo;
  195. int err, pid;
  196. sc.gs = REGS_GS(regs->regs.gp);
  197. sc.fs = REGS_FS(regs->regs.gp);
  198. sc.es = REGS_ES(regs->regs.gp);
  199. sc.ds = REGS_DS(regs->regs.gp);
  200. sc.di = REGS_EDI(regs->regs.gp);
  201. sc.si = REGS_ESI(regs->regs.gp);
  202. sc.bp = REGS_EBP(regs->regs.gp);
  203. sc.sp = sp;
  204. sc.bx = REGS_EBX(regs->regs.gp);
  205. sc.dx = REGS_EDX(regs->regs.gp);
  206. sc.cx = REGS_ECX(regs->regs.gp);
  207. sc.ax = REGS_EAX(regs->regs.gp);
  208. sc.ip = REGS_IP(regs->regs.gp);
  209. sc.cs = REGS_CS(regs->regs.gp);
  210. sc.flags = REGS_EFLAGS(regs->regs.gp);
  211. sc.sp_at_signal = regs->regs.gp[UESP];
  212. sc.ss = regs->regs.gp[SS];
  213. sc.cr2 = fi->cr2;
  214. sc.err = fi->error_code;
  215. sc.trapno = fi->trap_no;
  216. to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1));
  217. sc.fpstate = to_fp;
  218. pid = userspace_pid[current_thread_info()->cpu];
  219. if (have_fpx_regs) {
  220. struct user_fxsr_struct fpx;
  221. err = save_fpx_registers(pid, (unsigned long *) &fpx);
  222. if (err < 0){
  223. printk(KERN_ERR "copy_sc_to_user - save_fpx_registers "
  224. "failed, errno = %d\n", err);
  225. return 1;
  226. }
  227. err = convert_fxsr_to_user(to_fp, &fpx);
  228. if (err)
  229. return 1;
  230. err |= __put_user(fpx.swd, &to_fp->status);
  231. err |= __put_user(X86_FXSR_MAGIC, &to_fp->magic);
  232. if (err)
  233. return 1;
  234. if (copy_to_user(&to_fp->_fxsr_env[0], &fpx,
  235. sizeof(struct user_fxsr_struct)))
  236. return 1;
  237. }
  238. else {
  239. struct user_i387_struct fp;
  240. err = save_fp_registers(pid, (unsigned long *) &fp);
  241. if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct)))
  242. return 1;
  243. }
  244. return copy_to_user(to, &sc, sizeof(sc));
  245. }
  246. static int copy_ucontext_to_user(struct ucontext __user *uc,
  247. struct _fpstate __user *fp, sigset_t *set,
  248. unsigned long sp)
  249. {
  250. int err = 0;
  251. err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
  252. err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
  253. err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
  254. err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, sp);
  255. err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
  256. return err;
  257. }
  258. struct sigframe
  259. {
  260. char __user *pretcode;
  261. int sig;
  262. struct sigcontext sc;
  263. struct _fpstate fpstate;
  264. unsigned long extramask[_NSIG_WORDS-1];
  265. char retcode[8];
  266. };
  267. struct rt_sigframe
  268. {
  269. char __user *pretcode;
  270. int sig;
  271. struct siginfo __user *pinfo;
  272. void __user *puc;
  273. struct siginfo info;
  274. struct ucontext uc;
  275. struct _fpstate fpstate;
  276. char retcode[8];
  277. };
  278. int setup_signal_stack_sc(unsigned long stack_top, int sig,
  279. struct k_sigaction *ka, struct pt_regs *regs,
  280. sigset_t *mask)
  281. {
  282. struct sigframe __user *frame;
  283. void __user *restorer;
  284. unsigned long save_sp = PT_REGS_SP(regs);
  285. int err = 0;
  286. /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
  287. stack_top = ((stack_top + 4) & -16UL) - 4;
  288. frame = (struct sigframe __user *) stack_top - 1;
  289. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  290. return 1;
  291. restorer = frame->retcode;
  292. if (ka->sa.sa_flags & SA_RESTORER)
  293. restorer = ka->sa.sa_restorer;
  294. /* Update SP now because the page fault handler refuses to extend
  295. * the stack if the faulting address is too far below the current
  296. * SP, which frame now certainly is. If there's an error, the original
  297. * value is restored on the way out.
  298. * When writing the sigcontext to the stack, we have to write the
  299. * original value, so that's passed to copy_sc_to_user, which does
  300. * the right thing with it.
  301. */
  302. PT_REGS_SP(regs) = (unsigned long) frame;
  303. err |= __put_user(restorer, &frame->pretcode);
  304. err |= __put_user(sig, &frame->sig);
  305. err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
  306. err |= __put_user(mask->sig[0], &frame->sc.oldmask);
  307. if (_NSIG_WORDS > 1)
  308. err |= __copy_to_user(&frame->extramask, &mask->sig[1],
  309. sizeof(frame->extramask));
  310. /*
  311. * This is popl %eax ; movl $,%eax ; int $0x80
  312. *
  313. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  314. * reasons and because gdb uses it as a signature to notice
  315. * signal handler stack frames.
  316. */
  317. err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
  318. err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
  319. err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
  320. if (err)
  321. goto err;
  322. PT_REGS_SP(regs) = (unsigned long) frame;
  323. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  324. PT_REGS_EAX(regs) = (unsigned long) sig;
  325. PT_REGS_EDX(regs) = (unsigned long) 0;
  326. PT_REGS_ECX(regs) = (unsigned long) 0;
  327. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  328. ptrace_notify(SIGTRAP);
  329. return 0;
  330. err:
  331. PT_REGS_SP(regs) = save_sp;
  332. return err;
  333. }
  334. int setup_signal_stack_si(unsigned long stack_top, int sig,
  335. struct k_sigaction *ka, struct pt_regs *regs,
  336. siginfo_t *info, sigset_t *mask)
  337. {
  338. struct rt_sigframe __user *frame;
  339. void __user *restorer;
  340. unsigned long save_sp = PT_REGS_SP(regs);
  341. int err = 0;
  342. stack_top &= -8UL;
  343. frame = (struct rt_sigframe __user *) stack_top - 1;
  344. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  345. return 1;
  346. restorer = frame->retcode;
  347. if (ka->sa.sa_flags & SA_RESTORER)
  348. restorer = ka->sa.sa_restorer;
  349. /* See comment above about why this is here */
  350. PT_REGS_SP(regs) = (unsigned long) frame;
  351. err |= __put_user(restorer, &frame->pretcode);
  352. err |= __put_user(sig, &frame->sig);
  353. err |= __put_user(&frame->info, &frame->pinfo);
  354. err |= __put_user(&frame->uc, &frame->puc);
  355. err |= copy_siginfo_to_user(&frame->info, info);
  356. err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
  357. save_sp);
  358. /*
  359. * This is movl $,%eax ; int $0x80
  360. *
  361. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  362. * reasons and because gdb uses it as a signature to notice
  363. * signal handler stack frames.
  364. */
  365. err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
  366. err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
  367. err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
  368. if (err)
  369. goto err;
  370. PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
  371. PT_REGS_EAX(regs) = (unsigned long) sig;
  372. PT_REGS_EDX(regs) = (unsigned long) &frame->info;
  373. PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
  374. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  375. ptrace_notify(SIGTRAP);
  376. return 0;
  377. err:
  378. PT_REGS_SP(regs) = save_sp;
  379. return err;
  380. }
  381. long sys_sigreturn(struct pt_regs regs)
  382. {
  383. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  384. struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
  385. sigset_t set;
  386. struct sigcontext __user *sc = &frame->sc;
  387. unsigned long __user *oldmask = &sc->oldmask;
  388. unsigned long __user *extramask = frame->extramask;
  389. int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
  390. if (copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
  391. copy_from_user(&set.sig[1], extramask, sig_size))
  392. goto segfault;
  393. sigdelsetmask(&set, ~_BLOCKABLE);
  394. spin_lock_irq(&current->sighand->siglock);
  395. current->blocked = set;
  396. recalc_sigpending();
  397. spin_unlock_irq(&current->sighand->siglock);
  398. if (copy_sc_from_user(&current->thread.regs, sc))
  399. goto segfault;
  400. /* Avoid ERESTART handling */
  401. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  402. return PT_REGS_SYSCALL_RET(&current->thread.regs);
  403. segfault:
  404. force_sig(SIGSEGV, current);
  405. return 0;
  406. }
  407. long sys_rt_sigreturn(struct pt_regs regs)
  408. {
  409. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  410. struct rt_sigframe __user *frame =
  411. (struct rt_sigframe __user *) (sp - 4);
  412. sigset_t set;
  413. struct ucontext __user *uc = &frame->uc;
  414. int sig_size = _NSIG_WORDS * sizeof(unsigned long);
  415. if (copy_from_user(&set, &uc->uc_sigmask, sig_size))
  416. goto segfault;
  417. sigdelsetmask(&set, ~_BLOCKABLE);
  418. spin_lock_irq(&current->sighand->siglock);
  419. current->blocked = set;
  420. recalc_sigpending();
  421. spin_unlock_irq(&current->sighand->siglock);
  422. if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
  423. goto segfault;
  424. /* Avoid ERESTART handling */
  425. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  426. return PT_REGS_SYSCALL_RET(&current->thread.regs);
  427. segfault:
  428. force_sig(SIGSEGV, current);
  429. return 0;
  430. }