signal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Copyright (C) 2003 PathScale, Inc.
  3. * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Licensed under the GPL
  5. */
  6. #include <linux/personality.h>
  7. #include <linux/ptrace.h>
  8. #include <linux/kernel.h>
  9. #include <asm/unistd.h>
  10. #include <asm/uaccess.h>
  11. #include <asm/ucontext.h>
  12. #include <frame_kern.h>
  13. #include <skas.h>
  14. #ifdef CONFIG_X86_32
  15. /*
  16. * FPU tag word conversions.
  17. */
  18. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  19. {
  20. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  21. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  22. tmp = ~twd;
  23. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  24. /* and move the valid bits to the lower byte. */
  25. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  26. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  27. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  28. return tmp;
  29. }
  30. static inline unsigned long twd_fxsr_to_i387(struct user_fxsr_struct *fxsave)
  31. {
  32. struct _fpxreg *st = NULL;
  33. unsigned long twd = (unsigned long) fxsave->twd;
  34. unsigned long tag;
  35. unsigned long ret = 0xffff0000;
  36. int i;
  37. #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16)
  38. for (i = 0; i < 8; i++) {
  39. if (twd & 0x1) {
  40. st = (struct _fpxreg *) FPREG_ADDR(fxsave, i);
  41. switch (st->exponent & 0x7fff) {
  42. case 0x7fff:
  43. tag = 2; /* Special */
  44. break;
  45. case 0x0000:
  46. if ( !st->significand[0] &&
  47. !st->significand[1] &&
  48. !st->significand[2] &&
  49. !st->significand[3] ) {
  50. tag = 1; /* Zero */
  51. } else {
  52. tag = 2; /* Special */
  53. }
  54. break;
  55. default:
  56. if (st->significand[3] & 0x8000) {
  57. tag = 0; /* Valid */
  58. } else {
  59. tag = 2; /* Special */
  60. }
  61. break;
  62. }
  63. } else {
  64. tag = 3; /* Empty */
  65. }
  66. ret |= (tag << (2 * i));
  67. twd = twd >> 1;
  68. }
  69. return ret;
  70. }
  71. static int convert_fxsr_to_user(struct _fpstate __user *buf,
  72. struct user_fxsr_struct *fxsave)
  73. {
  74. unsigned long env[7];
  75. struct _fpreg __user *to;
  76. struct _fpxreg *from;
  77. int i;
  78. env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
  79. env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
  80. env[2] = twd_fxsr_to_i387(fxsave);
  81. env[3] = fxsave->fip;
  82. env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
  83. env[5] = fxsave->foo;
  84. env[6] = fxsave->fos;
  85. if (__copy_to_user(buf, env, 7 * sizeof(unsigned long)))
  86. return 1;
  87. to = &buf->_st[0];
  88. from = (struct _fpxreg *) &fxsave->st_space[0];
  89. for (i = 0; i < 8; i++, to++, from++) {
  90. unsigned long __user *t = (unsigned long __user *)to;
  91. unsigned long *f = (unsigned long *)from;
  92. if (__put_user(*f, t) ||
  93. __put_user(*(f + 1), t + 1) ||
  94. __put_user(from->exponent, &to->exponent))
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. static int convert_fxsr_from_user(struct user_fxsr_struct *fxsave,
  100. struct _fpstate __user *buf)
  101. {
  102. unsigned long env[7];
  103. struct _fpxreg *to;
  104. struct _fpreg __user *from;
  105. int i;
  106. if (copy_from_user( env, buf, 7 * sizeof(long)))
  107. return 1;
  108. fxsave->cwd = (unsigned short)(env[0] & 0xffff);
  109. fxsave->swd = (unsigned short)(env[1] & 0xffff);
  110. fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
  111. fxsave->fip = env[3];
  112. fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
  113. fxsave->fcs = (env[4] & 0xffff);
  114. fxsave->foo = env[5];
  115. fxsave->fos = env[6];
  116. to = (struct _fpxreg *) &fxsave->st_space[0];
  117. from = &buf->_st[0];
  118. for (i = 0; i < 8; i++, to++, from++) {
  119. unsigned long *t = (unsigned long *)to;
  120. unsigned long __user *f = (unsigned long __user *)from;
  121. if (__get_user(*t, f) ||
  122. __get_user(*(t + 1), f + 1) ||
  123. __get_user(to->exponent, &from->exponent))
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. extern int have_fpx_regs;
  129. #endif
  130. static int copy_sc_from_user(struct pt_regs *regs,
  131. struct sigcontext __user *from)
  132. {
  133. struct sigcontext sc;
  134. int err, pid;
  135. /* Always make any pending restarted system calls return -EINTR */
  136. current->restart_block.fn = do_no_restart_syscall;
  137. err = copy_from_user(&sc, from, sizeof(sc));
  138. if (err)
  139. return err;
  140. #define GETREG(regno, regname) regs->regs.gp[HOST_##regno] = sc.regname
  141. #ifdef CONFIG_X86_32
  142. GETREG(GS, gs);
  143. GETREG(FS, fs);
  144. GETREG(ES, es);
  145. GETREG(DS, ds);
  146. #endif
  147. GETREG(DI, di);
  148. GETREG(SI, si);
  149. GETREG(BP, bp);
  150. GETREG(SP, sp);
  151. GETREG(BX, bx);
  152. GETREG(DX, dx);
  153. GETREG(CX, cx);
  154. GETREG(AX, ax);
  155. GETREG(IP, ip);
  156. #ifdef CONFIG_X86_64
  157. GETREG(R8, r8);
  158. GETREG(R9, r9);
  159. GETREG(R10, r10);
  160. GETREG(R11, r11);
  161. GETREG(R12, r12);
  162. GETREG(R13, r13);
  163. GETREG(R14, r14);
  164. GETREG(R15, r15);
  165. #endif
  166. GETREG(CS, cs);
  167. GETREG(EFLAGS, flags);
  168. #ifdef CONFIG_X86_32
  169. GETREG(SS, ss);
  170. #endif
  171. #undef GETREG
  172. pid = userspace_pid[current_thread_info()->cpu];
  173. #ifdef CONFIG_X86_32
  174. if (have_fpx_regs) {
  175. struct user_fxsr_struct fpx;
  176. err = copy_from_user(&fpx,
  177. &((struct _fpstate __user *)sc.fpstate)->_fxsr_env[0],
  178. sizeof(struct user_fxsr_struct));
  179. if (err)
  180. return 1;
  181. err = convert_fxsr_from_user(&fpx, (void *)sc.fpstate);
  182. if (err)
  183. return 1;
  184. err = restore_fpx_registers(pid, (unsigned long *) &fpx);
  185. if (err < 0) {
  186. printk(KERN_ERR "copy_sc_from_user - "
  187. "restore_fpx_registers failed, errno = %d\n",
  188. -err);
  189. return 1;
  190. }
  191. } else
  192. #endif
  193. {
  194. struct user_i387_struct fp;
  195. err = copy_from_user(&fp, (void *)sc.fpstate,
  196. sizeof(struct user_i387_struct));
  197. if (err)
  198. return 1;
  199. err = restore_fp_registers(pid, (unsigned long *) &fp);
  200. if (err < 0) {
  201. printk(KERN_ERR "copy_sc_from_user - "
  202. "restore_fp_registers failed, errno = %d\n",
  203. -err);
  204. return 1;
  205. }
  206. }
  207. return 0;
  208. }
  209. static int copy_sc_to_user(struct sigcontext __user *to,
  210. struct _fpstate __user *to_fp, struct pt_regs *regs,
  211. unsigned long mask)
  212. {
  213. struct sigcontext sc;
  214. struct faultinfo * fi = &current->thread.arch.faultinfo;
  215. int err, pid;
  216. memset(&sc, 0, sizeof(struct sigcontext));
  217. #define PUTREG(regno, regname) sc.regname = regs->regs.gp[HOST_##regno]
  218. #ifdef CONFIG_X86_32
  219. PUTREG(GS, gs);
  220. PUTREG(FS, fs);
  221. PUTREG(ES, es);
  222. PUTREG(DS, ds);
  223. #endif
  224. PUTREG(DI, di);
  225. PUTREG(SI, si);
  226. PUTREG(BP, bp);
  227. PUTREG(SP, sp);
  228. PUTREG(BX, bx);
  229. PUTREG(DX, dx);
  230. PUTREG(CX, cx);
  231. PUTREG(AX, ax);
  232. #ifdef CONFIG_X86_64
  233. PUTREG(R8, r8);
  234. PUTREG(R9, r9);
  235. PUTREG(R10, r10);
  236. PUTREG(R11, r11);
  237. PUTREG(R12, r12);
  238. PUTREG(R13, r13);
  239. PUTREG(R14, r14);
  240. PUTREG(R15, r15);
  241. #endif
  242. sc.cr2 = fi->cr2;
  243. sc.err = fi->error_code;
  244. sc.trapno = fi->trap_no;
  245. PUTREG(IP, ip);
  246. PUTREG(CS, cs);
  247. PUTREG(EFLAGS, flags);
  248. #ifdef CONFIG_X86_32
  249. PUTREG(SP, sp_at_signal);
  250. PUTREG(SS, ss);
  251. #endif
  252. #undef PUTREG
  253. sc.oldmask = mask;
  254. sc.fpstate = (unsigned long)to_fp;
  255. err = copy_to_user(to, &sc, sizeof(struct sigcontext));
  256. if (err)
  257. return 1;
  258. pid = userspace_pid[current_thread_info()->cpu];
  259. #ifdef CONFIG_X86_32
  260. if (have_fpx_regs) {
  261. struct user_fxsr_struct fpx;
  262. err = save_fpx_registers(pid, (unsigned long *) &fpx);
  263. if (err < 0){
  264. printk(KERN_ERR "copy_sc_to_user - save_fpx_registers "
  265. "failed, errno = %d\n", err);
  266. return 1;
  267. }
  268. err = convert_fxsr_to_user(to_fp, &fpx);
  269. if (err)
  270. return 1;
  271. err |= __put_user(fpx.swd, &to_fp->status);
  272. err |= __put_user(X86_FXSR_MAGIC, &to_fp->magic);
  273. if (err)
  274. return 1;
  275. if (copy_to_user(&to_fp->_fxsr_env[0], &fpx,
  276. sizeof(struct user_fxsr_struct)))
  277. return 1;
  278. } else
  279. #endif
  280. {
  281. struct user_i387_struct fp;
  282. err = save_fp_registers(pid, (unsigned long *) &fp);
  283. if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct)))
  284. return 1;
  285. }
  286. return 0;
  287. }
  288. #ifdef CONFIG_X86_32
  289. static int copy_ucontext_to_user(struct ucontext __user *uc,
  290. struct _fpstate __user *fp, sigset_t *set,
  291. unsigned long sp)
  292. {
  293. int err = 0;
  294. err |= __save_altstack(&uc->uc_stack, sp);
  295. err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, 0);
  296. err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
  297. return err;
  298. }
  299. struct sigframe
  300. {
  301. char __user *pretcode;
  302. int sig;
  303. struct sigcontext sc;
  304. struct _fpstate fpstate;
  305. unsigned long extramask[_NSIG_WORDS-1];
  306. char retcode[8];
  307. };
  308. struct rt_sigframe
  309. {
  310. char __user *pretcode;
  311. int sig;
  312. struct siginfo __user *pinfo;
  313. void __user *puc;
  314. struct siginfo info;
  315. struct ucontext uc;
  316. struct _fpstate fpstate;
  317. char retcode[8];
  318. };
  319. int setup_signal_stack_sc(unsigned long stack_top, struct ksignal *ksig,
  320. struct pt_regs *regs, sigset_t *mask)
  321. {
  322. struct sigframe __user *frame;
  323. void __user *restorer;
  324. int err = 0, sig = ksig->sig;
  325. /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
  326. stack_top = ((stack_top + 4) & -16UL) - 4;
  327. frame = (struct sigframe __user *) stack_top - 1;
  328. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  329. return 1;
  330. restorer = frame->retcode;
  331. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  332. restorer = ksig->ka.sa.sa_restorer;
  333. err |= __put_user(restorer, &frame->pretcode);
  334. err |= __put_user(sig, &frame->sig);
  335. err |= copy_sc_to_user(&frame->sc, &frame->fpstate, regs, mask->sig[0]);
  336. if (_NSIG_WORDS > 1)
  337. err |= __copy_to_user(&frame->extramask, &mask->sig[1],
  338. sizeof(frame->extramask));
  339. /*
  340. * This is popl %eax ; movl $,%eax ; int $0x80
  341. *
  342. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  343. * reasons and because gdb uses it as a signature to notice
  344. * signal handler stack frames.
  345. */
  346. err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
  347. err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
  348. err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
  349. if (err)
  350. return err;
  351. PT_REGS_SP(regs) = (unsigned long) frame;
  352. PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
  353. PT_REGS_AX(regs) = (unsigned long) sig;
  354. PT_REGS_DX(regs) = (unsigned long) 0;
  355. PT_REGS_CX(regs) = (unsigned long) 0;
  356. return 0;
  357. }
  358. int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig,
  359. struct pt_regs *regs, sigset_t *mask)
  360. {
  361. struct rt_sigframe __user *frame;
  362. void __user *restorer;
  363. int err = 0, sig = ksig->sig;
  364. stack_top &= -8UL;
  365. frame = (struct rt_sigframe __user *) stack_top - 1;
  366. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  367. return 1;
  368. restorer = frame->retcode;
  369. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  370. restorer = ksig->ka.sa.sa_restorer;
  371. err |= __put_user(restorer, &frame->pretcode);
  372. err |= __put_user(sig, &frame->sig);
  373. err |= __put_user(&frame->info, &frame->pinfo);
  374. err |= __put_user(&frame->uc, &frame->puc);
  375. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  376. err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
  377. PT_REGS_SP(regs));
  378. /*
  379. * This is movl $,%eax ; int $0x80
  380. *
  381. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  382. * reasons and because gdb uses it as a signature to notice
  383. * signal handler stack frames.
  384. */
  385. err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
  386. err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
  387. err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
  388. if (err)
  389. return err;
  390. PT_REGS_SP(regs) = (unsigned long) frame;
  391. PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
  392. PT_REGS_AX(regs) = (unsigned long) sig;
  393. PT_REGS_DX(regs) = (unsigned long) &frame->info;
  394. PT_REGS_CX(regs) = (unsigned long) &frame->uc;
  395. return 0;
  396. }
  397. long sys_sigreturn(void)
  398. {
  399. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  400. struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
  401. sigset_t set;
  402. struct sigcontext __user *sc = &frame->sc;
  403. int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
  404. if (copy_from_user(&set.sig[0], &sc->oldmask, sizeof(set.sig[0])) ||
  405. copy_from_user(&set.sig[1], frame->extramask, sig_size))
  406. goto segfault;
  407. set_current_blocked(&set);
  408. if (copy_sc_from_user(&current->thread.regs, sc))
  409. goto segfault;
  410. /* Avoid ERESTART handling */
  411. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  412. return PT_REGS_SYSCALL_RET(&current->thread.regs);
  413. segfault:
  414. force_sig(SIGSEGV, current);
  415. return 0;
  416. }
  417. #else
  418. struct rt_sigframe
  419. {
  420. char __user *pretcode;
  421. struct ucontext uc;
  422. struct siginfo info;
  423. struct _fpstate fpstate;
  424. };
  425. int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig,
  426. struct pt_regs *regs, sigset_t *set)
  427. {
  428. struct rt_sigframe __user *frame;
  429. int err = 0, sig = ksig->sig;
  430. unsigned long fp_to;
  431. frame = (struct rt_sigframe __user *)
  432. round_down(stack_top - sizeof(struct rt_sigframe), 16);
  433. /* Subtract 128 for a red zone and 8 for proper alignment */
  434. frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
  435. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  436. goto out;
  437. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  438. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  439. if (err)
  440. goto out;
  441. }
  442. /* Create the ucontext. */
  443. err |= __put_user(0, &frame->uc.uc_flags);
  444. err |= __put_user(0, &frame->uc.uc_link);
  445. err |= __save_altstack(&frame->uc.uc_stack, PT_REGS_SP(regs));
  446. err |= copy_sc_to_user(&frame->uc.uc_mcontext, &frame->fpstate, regs,
  447. set->sig[0]);
  448. fp_to = (unsigned long)&frame->fpstate;
  449. err |= __put_user(fp_to, &frame->uc.uc_mcontext.fpstate);
  450. if (sizeof(*set) == 16) {
  451. err |= __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
  452. err |= __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
  453. }
  454. else
  455. err |= __copy_to_user(&frame->uc.uc_sigmask, set,
  456. sizeof(*set));
  457. /*
  458. * Set up to return from userspace. If provided, use a stub
  459. * already in userspace.
  460. */
  461. /* x86-64 should always use SA_RESTORER. */
  462. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  463. err |= __put_user((void *)ksig->ka.sa.sa_restorer,
  464. &frame->pretcode);
  465. else
  466. /* could use a vstub here */
  467. return err;
  468. if (err)
  469. return err;
  470. PT_REGS_SP(regs) = (unsigned long) frame;
  471. PT_REGS_DI(regs) = sig;
  472. /* In case the signal handler was declared without prototypes */
  473. PT_REGS_AX(regs) = 0;
  474. /*
  475. * This also works for non SA_SIGINFO handlers because they expect the
  476. * next argument after the signal number on the stack.
  477. */
  478. PT_REGS_SI(regs) = (unsigned long) &frame->info;
  479. PT_REGS_DX(regs) = (unsigned long) &frame->uc;
  480. PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
  481. out:
  482. return err;
  483. }
  484. #endif
  485. long sys_rt_sigreturn(void)
  486. {
  487. unsigned long sp = PT_REGS_SP(&current->thread.regs);
  488. struct rt_sigframe __user *frame =
  489. (struct rt_sigframe __user *)(sp - sizeof(long));
  490. struct ucontext __user *uc = &frame->uc;
  491. sigset_t set;
  492. if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
  493. goto segfault;
  494. set_current_blocked(&set);
  495. if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
  496. goto segfault;
  497. /* Avoid ERESTART handling */
  498. PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
  499. return PT_REGS_SYSCALL_RET(&current->thread.regs);
  500. segfault:
  501. force_sig(SIGSEGV, current);
  502. return 0;
  503. }