1 /* 2 * arch/sparc64/kernel/signal.c 3 * 4 * Copyright (C) 1991, 1992 Linus Torvalds 5 * Copyright (C) 1995, 2008 David S. Miller (davem@davemloft.net) 6 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx) 7 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be) 8 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz) 9 */ 10 11 #ifdef CONFIG_COMPAT 12 #include <linux/compat.h> /* for compat_old_sigset_t */ 13 #endif 14 #include <linux/sched.h> 15 #include <linux/kernel.h> 16 #include <linux/signal.h> 17 #include <linux/errno.h> 18 #include <linux/wait.h> 19 #include <linux/ptrace.h> 20 #include <linux/tracehook.h> 21 #include <linux/unistd.h> 22 #include <linux/mm.h> 23 #include <linux/tty.h> 24 #include <linux/binfmts.h> 25 #include <linux/bitops.h> 26 #include <linux/context_tracking.h> 27 28 #include <asm/uaccess.h> 29 #include <asm/ptrace.h> 30 #include <asm/pgtable.h> 31 #include <asm/fpumacro.h> 32 #include <asm/uctx.h> 33 #include <asm/siginfo.h> 34 #include <asm/visasm.h> 35 #include <asm/switch_to.h> 36 #include <asm/cacheflush.h> 37 38 #include "entry.h" 39 #include "systbls.h" 40 #include "sigutil.h" 41 42 /* {set, get}context() needed for 64-bit SparcLinux userland. */ 43 asmlinkage void sparc64_set_context(struct pt_regs *regs) 44 { 45 struct ucontext __user *ucp = (struct ucontext __user *) 46 regs->u_regs[UREG_I0]; 47 enum ctx_state prev_state = exception_enter(); 48 mc_gregset_t __user *grp; 49 unsigned long pc, npc, tstate; 50 unsigned long fp, i7; 51 unsigned char fenab; 52 int err; 53 54 flush_user_windows(); 55 if (get_thread_wsaved() || 56 (((unsigned long)ucp) & (sizeof(unsigned long)-1)) || 57 (!__access_ok(ucp, sizeof(*ucp)))) 58 goto do_sigsegv; 59 grp = &ucp->uc_mcontext.mc_gregs; 60 err = __get_user(pc, &((*grp)[MC_PC])); 61 err |= __get_user(npc, &((*grp)[MC_NPC])); 62 if (err || ((pc | npc) & 3)) 63 goto do_sigsegv; 64 if (regs->u_regs[UREG_I1]) { 65 sigset_t set; 66 67 if (_NSIG_WORDS == 1) { 68 if (__get_user(set.sig[0], &ucp->uc_sigmask.sig[0])) 69 goto do_sigsegv; 70 } else { 71 if (__copy_from_user(&set, &ucp->uc_sigmask, sizeof(sigset_t))) 72 goto do_sigsegv; 73 } 74 set_current_blocked(&set); 75 } 76 if (test_thread_flag(TIF_32BIT)) { 77 pc &= 0xffffffff; 78 npc &= 0xffffffff; 79 } 80 regs->tpc = pc; 81 regs->tnpc = npc; 82 err |= __get_user(regs->y, &((*grp)[MC_Y])); 83 err |= __get_user(tstate, &((*grp)[MC_TSTATE])); 84 regs->tstate &= ~(TSTATE_ASI | TSTATE_ICC | TSTATE_XCC); 85 regs->tstate |= (tstate & (TSTATE_ASI | TSTATE_ICC | TSTATE_XCC)); 86 err |= __get_user(regs->u_regs[UREG_G1], (&(*grp)[MC_G1])); 87 err |= __get_user(regs->u_regs[UREG_G2], (&(*grp)[MC_G2])); 88 err |= __get_user(regs->u_regs[UREG_G3], (&(*grp)[MC_G3])); 89 err |= __get_user(regs->u_regs[UREG_G4], (&(*grp)[MC_G4])); 90 err |= __get_user(regs->u_regs[UREG_G5], (&(*grp)[MC_G5])); 91 err |= __get_user(regs->u_regs[UREG_G6], (&(*grp)[MC_G6])); 92 93 /* Skip %g7 as that's the thread register in userspace. */ 94 95 err |= __get_user(regs->u_regs[UREG_I0], (&(*grp)[MC_O0])); 96 err |= __get_user(regs->u_regs[UREG_I1], (&(*grp)[MC_O1])); 97 err |= __get_user(regs->u_regs[UREG_I2], (&(*grp)[MC_O2])); 98 err |= __get_user(regs->u_regs[UREG_I3], (&(*grp)[MC_O3])); 99 err |= __get_user(regs->u_regs[UREG_I4], (&(*grp)[MC_O4])); 100 err |= __get_user(regs->u_regs[UREG_I5], (&(*grp)[MC_O5])); 101 err |= __get_user(regs->u_regs[UREG_I6], (&(*grp)[MC_O6])); 102 err |= __get_user(regs->u_regs[UREG_I7], (&(*grp)[MC_O7])); 103 104 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp)); 105 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7)); 106 err |= __put_user(fp, 107 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[6]))); 108 err |= __put_user(i7, 109 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[7]))); 110 111 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab)); 112 if (fenab) { 113 unsigned long *fpregs = current_thread_info()->fpregs; 114 unsigned long fprs; 115 116 fprs_write(0); 117 err |= __get_user(fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs)); 118 if (fprs & FPRS_DL) 119 err |= copy_from_user(fpregs, 120 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs), 121 (sizeof(unsigned int) * 32)); 122 if (fprs & FPRS_DU) 123 err |= copy_from_user(fpregs+16, 124 ((unsigned long __user *)&(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs))+16, 125 (sizeof(unsigned int) * 32)); 126 err |= __get_user(current_thread_info()->xfsr[0], 127 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr)); 128 err |= __get_user(current_thread_info()->gsr[0], 129 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr)); 130 regs->tstate &= ~TSTATE_PEF; 131 } 132 if (err) 133 goto do_sigsegv; 134 out: 135 exception_exit(prev_state); 136 return; 137 do_sigsegv: 138 force_sig(SIGSEGV, current); 139 goto out; 140 } 141 142 asmlinkage void sparc64_get_context(struct pt_regs *regs) 143 { 144 struct ucontext __user *ucp = (struct ucontext __user *) 145 regs->u_regs[UREG_I0]; 146 enum ctx_state prev_state = exception_enter(); 147 mc_gregset_t __user *grp; 148 mcontext_t __user *mcp; 149 unsigned long fp, i7; 150 unsigned char fenab; 151 int err; 152 153 synchronize_user_stack(); 154 if (get_thread_wsaved() || clear_user(ucp, sizeof(*ucp))) 155 goto do_sigsegv; 156 157 #if 1 158 fenab = 0; /* IMO get_context is like any other system call, thus modifies FPU state -jj */ 159 #else 160 fenab = (current_thread_info()->fpsaved[0] & FPRS_FEF); 161 #endif 162 163 mcp = &ucp->uc_mcontext; 164 grp = &mcp->mc_gregs; 165 166 /* Skip over the trap instruction, first. */ 167 if (test_thread_flag(TIF_32BIT)) { 168 regs->tpc = (regs->tnpc & 0xffffffff); 169 regs->tnpc = (regs->tnpc + 4) & 0xffffffff; 170 } else { 171 regs->tpc = regs->tnpc; 172 regs->tnpc += 4; 173 } 174 err = 0; 175 if (_NSIG_WORDS == 1) 176 err |= __put_user(current->blocked.sig[0], 177 (unsigned long __user *)&ucp->uc_sigmask); 178 else 179 err |= __copy_to_user(&ucp->uc_sigmask, ¤t->blocked, 180 sizeof(sigset_t)); 181 182 err |= __put_user(regs->tstate, &((*grp)[MC_TSTATE])); 183 err |= __put_user(regs->tpc, &((*grp)[MC_PC])); 184 err |= __put_user(regs->tnpc, &((*grp)[MC_NPC])); 185 err |= __put_user(regs->y, &((*grp)[MC_Y])); 186 err |= __put_user(regs->u_regs[UREG_G1], &((*grp)[MC_G1])); 187 err |= __put_user(regs->u_regs[UREG_G2], &((*grp)[MC_G2])); 188 err |= __put_user(regs->u_regs[UREG_G3], &((*grp)[MC_G3])); 189 err |= __put_user(regs->u_regs[UREG_G4], &((*grp)[MC_G4])); 190 err |= __put_user(regs->u_regs[UREG_G5], &((*grp)[MC_G5])); 191 err |= __put_user(regs->u_regs[UREG_G6], &((*grp)[MC_G6])); 192 err |= __put_user(regs->u_regs[UREG_G7], &((*grp)[MC_G7])); 193 err |= __put_user(regs->u_regs[UREG_I0], &((*grp)[MC_O0])); 194 err |= __put_user(regs->u_regs[UREG_I1], &((*grp)[MC_O1])); 195 err |= __put_user(regs->u_regs[UREG_I2], &((*grp)[MC_O2])); 196 err |= __put_user(regs->u_regs[UREG_I3], &((*grp)[MC_O3])); 197 err |= __put_user(regs->u_regs[UREG_I4], &((*grp)[MC_O4])); 198 err |= __put_user(regs->u_regs[UREG_I5], &((*grp)[MC_O5])); 199 err |= __put_user(regs->u_regs[UREG_I6], &((*grp)[MC_O6])); 200 err |= __put_user(regs->u_regs[UREG_I7], &((*grp)[MC_O7])); 201 202 err |= __get_user(fp, 203 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[6]))); 204 err |= __get_user(i7, 205 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[7]))); 206 err |= __put_user(fp, &(mcp->mc_fp)); 207 err |= __put_user(i7, &(mcp->mc_i7)); 208 209 err |= __put_user(fenab, &(mcp->mc_fpregs.mcfpu_enab)); 210 if (fenab) { 211 unsigned long *fpregs = current_thread_info()->fpregs; 212 unsigned long fprs; 213 214 fprs = current_thread_info()->fpsaved[0]; 215 if (fprs & FPRS_DL) 216 err |= copy_to_user(&(mcp->mc_fpregs.mcfpu_fregs), fpregs, 217 (sizeof(unsigned int) * 32)); 218 if (fprs & FPRS_DU) 219 err |= copy_to_user( 220 ((unsigned long __user *)&(mcp->mc_fpregs.mcfpu_fregs))+16, fpregs+16, 221 (sizeof(unsigned int) * 32)); 222 err |= __put_user(current_thread_info()->xfsr[0], &(mcp->mc_fpregs.mcfpu_fsr)); 223 err |= __put_user(current_thread_info()->gsr[0], &(mcp->mc_fpregs.mcfpu_gsr)); 224 err |= __put_user(fprs, &(mcp->mc_fpregs.mcfpu_fprs)); 225 } 226 if (err) 227 goto do_sigsegv; 228 out: 229 exception_exit(prev_state); 230 return; 231 do_sigsegv: 232 force_sig(SIGSEGV, current); 233 goto out; 234 } 235 236 struct rt_signal_frame { 237 struct sparc_stackf ss; 238 siginfo_t info; 239 struct pt_regs regs; 240 __siginfo_fpu_t __user *fpu_save; 241 stack_t stack; 242 sigset_t mask; 243 __siginfo_rwin_t *rwin_save; 244 }; 245 246 void do_rt_sigreturn(struct pt_regs *regs) 247 { 248 struct rt_signal_frame __user *sf; 249 unsigned long tpc, tnpc, tstate; 250 __siginfo_fpu_t __user *fpu_save; 251 __siginfo_rwin_t __user *rwin_save; 252 sigset_t set; 253 int err; 254 255 /* Always make any pending restarted system calls return -EINTR */ 256 current_thread_info()->restart_block.fn = do_no_restart_syscall; 257 258 synchronize_user_stack (); 259 sf = (struct rt_signal_frame __user *) 260 (regs->u_regs [UREG_FP] + STACK_BIAS); 261 262 /* 1. Make sure we are not getting garbage from the user */ 263 if (((unsigned long) sf) & 3) 264 goto segv; 265 266 err = get_user(tpc, &sf->regs.tpc); 267 err |= __get_user(tnpc, &sf->regs.tnpc); 268 if (test_thread_flag(TIF_32BIT)) { 269 tpc &= 0xffffffff; 270 tnpc &= 0xffffffff; 271 } 272 err |= ((tpc | tnpc) & 3); 273 274 /* 2. Restore the state */ 275 err |= __get_user(regs->y, &sf->regs.y); 276 err |= __get_user(tstate, &sf->regs.tstate); 277 err |= copy_from_user(regs->u_regs, sf->regs.u_regs, sizeof(regs->u_regs)); 278 279 /* User can only change condition codes and %asi in %tstate. */ 280 regs->tstate &= ~(TSTATE_ASI | TSTATE_ICC | TSTATE_XCC); 281 regs->tstate |= (tstate & (TSTATE_ASI | TSTATE_ICC | TSTATE_XCC)); 282 283 err |= __get_user(fpu_save, &sf->fpu_save); 284 if (!err && fpu_save) 285 err |= restore_fpu_state(regs, fpu_save); 286 287 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t)); 288 err |= restore_altstack(&sf->stack); 289 if (err) 290 goto segv; 291 292 err |= __get_user(rwin_save, &sf->rwin_save); 293 if (!err && rwin_save) { 294 if (restore_rwin_state(rwin_save)) 295 goto segv; 296 } 297 298 regs->tpc = tpc; 299 regs->tnpc = tnpc; 300 301 /* Prevent syscall restart. */ 302 pt_regs_clear_syscall(regs); 303 304 set_current_blocked(&set); 305 return; 306 segv: 307 force_sig(SIGSEGV, current); 308 } 309 310 /* Checks if the fp is valid */ 311 static int invalid_frame_pointer(void __user *fp) 312 { 313 if (((unsigned long) fp) & 15) 314 return 1; 315 return 0; 316 } 317 318 static inline void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize) 319 { 320 unsigned long sp = regs->u_regs[UREG_FP] + STACK_BIAS; 321 322 /* 323 * If we are on the alternate signal stack and would overflow it, don't. 324 * Return an always-bogus address instead so we will die with SIGSEGV. 325 */ 326 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) 327 return (void __user *) -1L; 328 329 /* This is the X/Open sanctioned signal stack switching. */ 330 sp = sigsp(sp, ksig) - framesize; 331 332 /* Always align the stack frame. This handles two cases. First, 333 * sigaltstack need not be mindful of platform specific stack 334 * alignment. Second, if we took this signal because the stack 335 * is not aligned properly, we'd like to take the signal cleanly 336 * and report that. 337 */ 338 sp &= ~15UL; 339 340 return (void __user *) sp; 341 } 342 343 static inline int 344 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs) 345 { 346 struct rt_signal_frame __user *sf; 347 int wsaved, err, sf_size; 348 void __user *tail; 349 350 /* 1. Make sure everything is clean */ 351 synchronize_user_stack(); 352 save_and_clear_fpu(); 353 354 wsaved = get_thread_wsaved(); 355 356 sf_size = sizeof(struct rt_signal_frame); 357 if (current_thread_info()->fpsaved[0] & FPRS_FEF) 358 sf_size += sizeof(__siginfo_fpu_t); 359 if (wsaved) 360 sf_size += sizeof(__siginfo_rwin_t); 361 sf = (struct rt_signal_frame __user *) 362 get_sigframe(ksig, regs, sf_size); 363 364 if (invalid_frame_pointer (sf)) { 365 do_exit(SIGILL); /* won't return, actually */ 366 return -EINVAL; 367 } 368 369 tail = (sf + 1); 370 371 /* 2. Save the current process state */ 372 err = copy_to_user(&sf->regs, regs, sizeof (*regs)); 373 374 if (current_thread_info()->fpsaved[0] & FPRS_FEF) { 375 __siginfo_fpu_t __user *fpu_save = tail; 376 tail += sizeof(__siginfo_fpu_t); 377 err |= save_fpu_state(regs, fpu_save); 378 err |= __put_user((u64)fpu_save, &sf->fpu_save); 379 } else { 380 err |= __put_user(0, &sf->fpu_save); 381 } 382 if (wsaved) { 383 __siginfo_rwin_t __user *rwin_save = tail; 384 tail += sizeof(__siginfo_rwin_t); 385 err |= save_rwin_state(wsaved, rwin_save); 386 err |= __put_user((u64)rwin_save, &sf->rwin_save); 387 set_thread_wsaved(0); 388 } else { 389 err |= __put_user(0, &sf->rwin_save); 390 } 391 392 /* Setup sigaltstack */ 393 err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]); 394 395 err |= copy_to_user(&sf->mask, sigmask_to_save(), sizeof(sigset_t)); 396 397 if (!wsaved) { 398 err |= copy_in_user((u64 __user *)sf, 399 (u64 __user *)(regs->u_regs[UREG_FP] + 400 STACK_BIAS), 401 sizeof(struct reg_window)); 402 } else { 403 struct reg_window *rp; 404 405 rp = ¤t_thread_info()->reg_window[wsaved - 1]; 406 err |= copy_to_user(sf, rp, sizeof(struct reg_window)); 407 } 408 if (ksig->ka.sa.sa_flags & SA_SIGINFO) 409 err |= copy_siginfo_to_user(&sf->info, &ksig->info); 410 else { 411 err |= __put_user(ksig->sig, &sf->info.si_signo); 412 err |= __put_user(SI_NOINFO, &sf->info.si_code); 413 } 414 if (err) 415 return err; 416 417 /* 3. signal handler back-trampoline and parameters */ 418 regs->u_regs[UREG_FP] = ((unsigned long) sf) - STACK_BIAS; 419 regs->u_regs[UREG_I0] = ksig->sig; 420 regs->u_regs[UREG_I1] = (unsigned long) &sf->info; 421 422 /* The sigcontext is passed in this way because of how it 423 * is defined in GLIBC's /usr/include/bits/sigcontext.h 424 * for sparc64. It includes the 128 bytes of siginfo_t. 425 */ 426 regs->u_regs[UREG_I2] = (unsigned long) &sf->info; 427 428 /* 5. signal handler */ 429 regs->tpc = (unsigned long) ksig->ka.sa.sa_handler; 430 regs->tnpc = (regs->tpc + 4); 431 if (test_thread_flag(TIF_32BIT)) { 432 regs->tpc &= 0xffffffff; 433 regs->tnpc &= 0xffffffff; 434 } 435 /* 4. return to kernel instructions */ 436 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer; 437 return 0; 438 } 439 440 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs, 441 struct sigaction *sa) 442 { 443 switch (regs->u_regs[UREG_I0]) { 444 case ERESTART_RESTARTBLOCK: 445 case ERESTARTNOHAND: 446 no_system_call_restart: 447 regs->u_regs[UREG_I0] = EINTR; 448 regs->tstate |= (TSTATE_ICARRY|TSTATE_XCARRY); 449 break; 450 case ERESTARTSYS: 451 if (!(sa->sa_flags & SA_RESTART)) 452 goto no_system_call_restart; 453 /* fallthrough */ 454 case ERESTARTNOINTR: 455 regs->u_regs[UREG_I0] = orig_i0; 456 regs->tpc -= 4; 457 regs->tnpc -= 4; 458 } 459 } 460 461 /* Note that 'init' is a special process: it doesn't get signals it doesn't 462 * want to handle. Thus you cannot kill init even with a SIGKILL even by 463 * mistake. 464 */ 465 static void do_signal(struct pt_regs *regs, unsigned long orig_i0) 466 { 467 struct ksignal ksig; 468 int restart_syscall; 469 bool has_handler; 470 471 /* It's a lot of work and synchronization to add a new ptrace 472 * register for GDB to save and restore in order to get 473 * orig_i0 correct for syscall restarts when debugging. 474 * 475 * Although it should be the case that most of the global 476 * registers are volatile across a system call, glibc already 477 * depends upon that fact that we preserve them. So we can't 478 * just use any global register to save away the orig_i0 value. 479 * 480 * In particular %g2, %g3, %g4, and %g5 are all assumed to be 481 * preserved across a system call trap by various pieces of 482 * code in glibc. 483 * 484 * %g7 is used as the "thread register". %g6 is not used in 485 * any fixed manner. %g6 is used as a scratch register and 486 * a compiler temporary, but it's value is never used across 487 * a system call. Therefore %g6 is usable for orig_i0 storage. 488 */ 489 if (pt_regs_is_syscall(regs) && 490 (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) 491 regs->u_regs[UREG_G6] = orig_i0; 492 493 #ifdef CONFIG_COMPAT 494 if (test_thread_flag(TIF_32BIT)) { 495 extern void do_signal32(struct pt_regs *); 496 do_signal32(regs); 497 return; 498 } 499 #endif 500 501 has_handler = get_signal(&ksig); 502 503 restart_syscall = 0; 504 if (pt_regs_is_syscall(regs) && 505 (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) { 506 restart_syscall = 1; 507 orig_i0 = regs->u_regs[UREG_G6]; 508 } 509 510 if (has_handler) { 511 if (restart_syscall) 512 syscall_restart(orig_i0, regs, &ksig.ka.sa); 513 signal_setup_done(setup_rt_frame(&ksig, regs), &ksig, 0); 514 } else { 515 if (restart_syscall) { 516 switch (regs->u_regs[UREG_I0]) { 517 case ERESTARTNOHAND: 518 case ERESTARTSYS: 519 case ERESTARTNOINTR: 520 /* replay the system call when we are done */ 521 regs->u_regs[UREG_I0] = orig_i0; 522 regs->tpc -= 4; 523 regs->tnpc -= 4; 524 pt_regs_clear_syscall(regs); 525 case ERESTART_RESTARTBLOCK: 526 regs->u_regs[UREG_G1] = __NR_restart_syscall; 527 regs->tpc -= 4; 528 regs->tnpc -= 4; 529 pt_regs_clear_syscall(regs); 530 } 531 } 532 restore_saved_sigmask(); 533 } 534 } 535 536 void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, unsigned long thread_info_flags) 537 { 538 user_exit(); 539 if (thread_info_flags & _TIF_SIGPENDING) 540 do_signal(regs, orig_i0); 541 if (thread_info_flags & _TIF_NOTIFY_RESUME) { 542 clear_thread_flag(TIF_NOTIFY_RESUME); 543 tracehook_notify_resume(regs); 544 } 545 user_enter(); 546 } 547 548