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