1 /* 2 * Copyright IBM Corp. 1999, 2006 3 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com) 4 * 5 * Based on Intel version 6 * 7 * Copyright (C) 1991, 1992 Linus Torvalds 8 * 9 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 10 */ 11 12 #include <linux/sched.h> 13 #include <linux/mm.h> 14 #include <linux/smp.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/unistd.h> 21 #include <linux/stddef.h> 22 #include <linux/tty.h> 23 #include <linux/personality.h> 24 #include <linux/binfmts.h> 25 #include <linux/tracehook.h> 26 #include <linux/syscalls.h> 27 #include <linux/compat.h> 28 #include <asm/ucontext.h> 29 #include <asm/uaccess.h> 30 #include <asm/lowcore.h> 31 #include <asm/switch_to.h> 32 #include "entry.h" 33 34 typedef struct 35 { 36 __u8 callee_used_stack[__SIGNAL_FRAMESIZE]; 37 struct sigcontext sc; 38 _sigregs sregs; 39 int signo; 40 __u8 retcode[S390_SYSCALL_SIZE]; 41 } sigframe; 42 43 typedef struct 44 { 45 __u8 callee_used_stack[__SIGNAL_FRAMESIZE]; 46 __u8 retcode[S390_SYSCALL_SIZE]; 47 struct siginfo info; 48 struct ucontext uc; 49 } rt_sigframe; 50 51 /* 52 * Atomically swap in the new signal mask, and wait for a signal. 53 */ 54 SYSCALL_DEFINE3(sigsuspend, int, history0, int, history1, old_sigset_t, mask) 55 { 56 sigset_t blocked; 57 siginitset(&blocked, mask); 58 return sigsuspend(&blocked); 59 } 60 61 SYSCALL_DEFINE3(sigaction, int, sig, const struct old_sigaction __user *, act, 62 struct old_sigaction __user *, oact) 63 { 64 struct k_sigaction new_ka, old_ka; 65 int ret; 66 67 if (act) { 68 old_sigset_t mask; 69 if (!access_ok(VERIFY_READ, act, sizeof(*act)) || 70 __get_user(new_ka.sa.sa_handler, &act->sa_handler) || 71 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) || 72 __get_user(new_ka.sa.sa_flags, &act->sa_flags) || 73 __get_user(mask, &act->sa_mask)) 74 return -EFAULT; 75 siginitset(&new_ka.sa.sa_mask, mask); 76 } 77 78 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); 79 80 if (!ret && oact) { 81 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) || 82 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || 83 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) || 84 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) || 85 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask)) 86 return -EFAULT; 87 } 88 89 return ret; 90 } 91 92 SYSCALL_DEFINE2(sigaltstack, const stack_t __user *, uss, 93 stack_t __user *, uoss) 94 { 95 struct pt_regs *regs = task_pt_regs(current); 96 return do_sigaltstack(uss, uoss, regs->gprs[15]); 97 } 98 99 /* Returns non-zero on fault. */ 100 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs) 101 { 102 _sigregs user_sregs; 103 104 save_access_regs(current->thread.acrs); 105 106 /* Copy a 'clean' PSW mask to the user to avoid leaking 107 information about whether PER is currently on. */ 108 user_sregs.regs.psw.mask = psw_user_bits | 109 (regs->psw.mask & PSW_MASK_USER); 110 user_sregs.regs.psw.addr = regs->psw.addr; 111 memcpy(&user_sregs.regs.gprs, ®s->gprs, sizeof(sregs->regs.gprs)); 112 memcpy(&user_sregs.regs.acrs, current->thread.acrs, 113 sizeof(sregs->regs.acrs)); 114 /* 115 * We have to store the fp registers to current->thread.fp_regs 116 * to merge them with the emulated registers. 117 */ 118 save_fp_regs(¤t->thread.fp_regs); 119 memcpy(&user_sregs.fpregs, ¤t->thread.fp_regs, 120 sizeof(s390_fp_regs)); 121 return __copy_to_user(sregs, &user_sregs, sizeof(_sigregs)); 122 } 123 124 /* Returns positive number on error */ 125 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs) 126 { 127 int err; 128 _sigregs user_sregs; 129 130 /* Alwys make any pending restarted system call return -EINTR */ 131 current_thread_info()->restart_block.fn = do_no_restart_syscall; 132 133 err = __copy_from_user(&user_sregs, sregs, sizeof(_sigregs)); 134 if (err) 135 return err; 136 /* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */ 137 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) | 138 (user_sregs.regs.psw.mask & PSW_MASK_USER); 139 /* Check for invalid user address space control. */ 140 if ((regs->psw.mask & PSW_MASK_ASC) >= (psw_kernel_bits & PSW_MASK_ASC)) 141 regs->psw.mask = (psw_user_bits & PSW_MASK_ASC) | 142 (regs->psw.mask & ~PSW_MASK_ASC); 143 /* Check for invalid amode */ 144 if (regs->psw.mask & PSW_MASK_EA) 145 regs->psw.mask |= PSW_MASK_BA; 146 regs->psw.addr = user_sregs.regs.psw.addr; 147 memcpy(®s->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs)); 148 memcpy(¤t->thread.acrs, &user_sregs.regs.acrs, 149 sizeof(sregs->regs.acrs)); 150 restore_access_regs(current->thread.acrs); 151 152 memcpy(¤t->thread.fp_regs, &user_sregs.fpregs, 153 sizeof(s390_fp_regs)); 154 current->thread.fp_regs.fpc &= FPC_VALID_MASK; 155 156 restore_fp_regs(¤t->thread.fp_regs); 157 clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */ 158 return 0; 159 } 160 161 SYSCALL_DEFINE0(sigreturn) 162 { 163 struct pt_regs *regs = task_pt_regs(current); 164 sigframe __user *frame = (sigframe __user *)regs->gprs[15]; 165 sigset_t set; 166 167 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 168 goto badframe; 169 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE)) 170 goto badframe; 171 set_current_blocked(&set); 172 if (restore_sigregs(regs, &frame->sregs)) 173 goto badframe; 174 return regs->gprs[2]; 175 badframe: 176 force_sig(SIGSEGV, current); 177 return 0; 178 } 179 180 SYSCALL_DEFINE0(rt_sigreturn) 181 { 182 struct pt_regs *regs = task_pt_regs(current); 183 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15]; 184 sigset_t set; 185 186 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 187 goto badframe; 188 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set))) 189 goto badframe; 190 set_current_blocked(&set); 191 if (restore_sigregs(regs, &frame->uc.uc_mcontext)) 192 goto badframe; 193 if (do_sigaltstack(&frame->uc.uc_stack, NULL, 194 regs->gprs[15]) == -EFAULT) 195 goto badframe; 196 return regs->gprs[2]; 197 badframe: 198 force_sig(SIGSEGV, current); 199 return 0; 200 } 201 202 /* 203 * Set up a signal frame. 204 */ 205 206 207 /* 208 * Determine which stack to use.. 209 */ 210 static inline void __user * 211 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) 212 { 213 unsigned long sp; 214 215 /* Default to using normal stack */ 216 sp = regs->gprs[15]; 217 218 /* Overflow on alternate signal stack gives SIGSEGV. */ 219 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL)) 220 return (void __user *) -1UL; 221 222 /* This is the X/Open sanctioned signal stack switching. */ 223 if (ka->sa.sa_flags & SA_ONSTACK) { 224 if (! sas_ss_flags(sp)) 225 sp = current->sas_ss_sp + current->sas_ss_size; 226 } 227 228 return (void __user *)((sp - frame_size) & -8ul); 229 } 230 231 static inline int map_signal(int sig) 232 { 233 if (current_thread_info()->exec_domain 234 && current_thread_info()->exec_domain->signal_invmap 235 && sig < 32) 236 return current_thread_info()->exec_domain->signal_invmap[sig]; 237 else 238 return sig; 239 } 240 241 static int setup_frame(int sig, struct k_sigaction *ka, 242 sigset_t *set, struct pt_regs * regs) 243 { 244 sigframe __user *frame; 245 246 frame = get_sigframe(ka, regs, sizeof(sigframe)); 247 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe))) 248 goto give_sigsegv; 249 250 if (frame == (void __user *) -1UL) 251 goto give_sigsegv; 252 253 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE)) 254 goto give_sigsegv; 255 256 if (save_sigregs(regs, &frame->sregs)) 257 goto give_sigsegv; 258 if (__put_user(&frame->sregs, &frame->sc.sregs)) 259 goto give_sigsegv; 260 261 /* Set up to return from userspace. If provided, use a stub 262 already in userspace. */ 263 if (ka->sa.sa_flags & SA_RESTORER) { 264 regs->gprs[14] = (unsigned long) 265 ka->sa.sa_restorer | PSW_ADDR_AMODE; 266 } else { 267 regs->gprs[14] = (unsigned long) 268 frame->retcode | PSW_ADDR_AMODE; 269 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, 270 (u16 __user *)(frame->retcode))) 271 goto give_sigsegv; 272 } 273 274 /* Set up backchain. */ 275 if (__put_user(regs->gprs[15], (addr_t __user *) frame)) 276 goto give_sigsegv; 277 278 /* Set up registers for signal handler */ 279 regs->gprs[15] = (unsigned long) frame; 280 /* Force default amode and default user address space control. */ 281 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA | 282 (psw_user_bits & PSW_MASK_ASC) | 283 (regs->psw.mask & ~PSW_MASK_ASC); 284 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE; 285 286 regs->gprs[2] = map_signal(sig); 287 regs->gprs[3] = (unsigned long) &frame->sc; 288 289 /* We forgot to include these in the sigcontext. 290 To avoid breaking binary compatibility, they are passed as args. */ 291 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 292 sig == SIGTRAP || sig == SIGFPE) { 293 /* set extra registers only for synchronous signals */ 294 regs->gprs[4] = regs->int_code & 127; 295 regs->gprs[5] = regs->int_parm_long; 296 regs->gprs[6] = task_thread_info(current)->last_break; 297 } 298 299 /* Place signal number on stack to allow backtrace from handler. */ 300 if (__put_user(regs->gprs[2], (int __user *) &frame->signo)) 301 goto give_sigsegv; 302 return 0; 303 304 give_sigsegv: 305 force_sigsegv(sig, current); 306 return -EFAULT; 307 } 308 309 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 310 sigset_t *set, struct pt_regs * regs) 311 { 312 int err = 0; 313 rt_sigframe __user *frame; 314 315 frame = get_sigframe(ka, regs, sizeof(rt_sigframe)); 316 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe))) 317 goto give_sigsegv; 318 319 if (frame == (void __user *) -1UL) 320 goto give_sigsegv; 321 322 if (copy_siginfo_to_user(&frame->info, info)) 323 goto give_sigsegv; 324 325 /* Create the ucontext. */ 326 err |= __put_user(0, &frame->uc.uc_flags); 327 err |= __put_user(NULL, &frame->uc.uc_link); 328 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); 329 err |= __put_user(sas_ss_flags(regs->gprs[15]), 330 &frame->uc.uc_stack.ss_flags); 331 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); 332 err |= save_sigregs(regs, &frame->uc.uc_mcontext); 333 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 334 if (err) 335 goto give_sigsegv; 336 337 /* Set up to return from userspace. If provided, use a stub 338 already in userspace. */ 339 if (ka->sa.sa_flags & SA_RESTORER) { 340 regs->gprs[14] = (unsigned long) 341 ka->sa.sa_restorer | PSW_ADDR_AMODE; 342 } else { 343 regs->gprs[14] = (unsigned long) 344 frame->retcode | PSW_ADDR_AMODE; 345 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, 346 (u16 __user *)(frame->retcode))) 347 goto give_sigsegv; 348 } 349 350 /* Set up backchain. */ 351 if (__put_user(regs->gprs[15], (addr_t __user *) frame)) 352 goto give_sigsegv; 353 354 /* Set up registers for signal handler */ 355 regs->gprs[15] = (unsigned long) frame; 356 /* Force default amode and default user address space control. */ 357 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA | 358 (psw_user_bits & PSW_MASK_ASC) | 359 (regs->psw.mask & ~PSW_MASK_ASC); 360 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE; 361 362 regs->gprs[2] = map_signal(sig); 363 regs->gprs[3] = (unsigned long) &frame->info; 364 regs->gprs[4] = (unsigned long) &frame->uc; 365 regs->gprs[5] = task_thread_info(current)->last_break; 366 return 0; 367 368 give_sigsegv: 369 force_sigsegv(sig, current); 370 return -EFAULT; 371 } 372 373 static void handle_signal(unsigned long sig, struct k_sigaction *ka, 374 siginfo_t *info, sigset_t *oldset, 375 struct pt_regs *regs) 376 { 377 int ret; 378 379 /* Set up the stack frame */ 380 if (ka->sa.sa_flags & SA_SIGINFO) 381 ret = setup_rt_frame(sig, ka, info, oldset, regs); 382 else 383 ret = setup_frame(sig, ka, oldset, regs); 384 if (ret) 385 return; 386 signal_delivered(sig, info, ka, regs, 387 test_thread_flag(TIF_SINGLE_STEP)); 388 } 389 390 /* 391 * Note that 'init' is a special process: it doesn't get signals it doesn't 392 * want to handle. Thus you cannot kill init even with a SIGKILL even by 393 * mistake. 394 * 395 * Note that we go through the signals twice: once to check the signals that 396 * the kernel can handle, and then we build all the user-level signal handling 397 * stack-frames in one go after that. 398 */ 399 void do_signal(struct pt_regs *regs) 400 { 401 siginfo_t info; 402 int signr; 403 struct k_sigaction ka; 404 sigset_t *oldset = sigmask_to_save(); 405 406 /* 407 * Get signal to deliver. When running under ptrace, at this point 408 * the debugger may change all our registers, including the system 409 * call information. 410 */ 411 current_thread_info()->system_call = 412 test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0; 413 signr = get_signal_to_deliver(&info, &ka, regs, NULL); 414 415 if (signr > 0) { 416 /* Whee! Actually deliver the signal. */ 417 if (current_thread_info()->system_call) { 418 regs->int_code = current_thread_info()->system_call; 419 /* Check for system call restarting. */ 420 switch (regs->gprs[2]) { 421 case -ERESTART_RESTARTBLOCK: 422 case -ERESTARTNOHAND: 423 regs->gprs[2] = -EINTR; 424 break; 425 case -ERESTARTSYS: 426 if (!(ka.sa.sa_flags & SA_RESTART)) { 427 regs->gprs[2] = -EINTR; 428 break; 429 } 430 /* fallthrough */ 431 case -ERESTARTNOINTR: 432 regs->gprs[2] = regs->orig_gpr2; 433 regs->psw.addr = 434 __rewind_psw(regs->psw, 435 regs->int_code >> 16); 436 break; 437 } 438 } 439 /* No longer in a system call */ 440 clear_thread_flag(TIF_SYSCALL); 441 442 if (is_compat_task()) 443 handle_signal32(signr, &ka, &info, oldset, regs); 444 else 445 handle_signal(signr, &ka, &info, oldset, regs); 446 return; 447 } 448 449 /* No handlers present - check for system call restart */ 450 clear_thread_flag(TIF_SYSCALL); 451 if (current_thread_info()->system_call) { 452 regs->int_code = current_thread_info()->system_call; 453 switch (regs->gprs[2]) { 454 case -ERESTART_RESTARTBLOCK: 455 /* Restart with sys_restart_syscall */ 456 regs->int_code = __NR_restart_syscall; 457 /* fallthrough */ 458 case -ERESTARTNOHAND: 459 case -ERESTARTSYS: 460 case -ERESTARTNOINTR: 461 /* Restart system call with magic TIF bit. */ 462 regs->gprs[2] = regs->orig_gpr2; 463 set_thread_flag(TIF_SYSCALL); 464 if (test_thread_flag(TIF_SINGLE_STEP)) 465 set_thread_flag(TIF_PER_TRAP); 466 break; 467 } 468 } 469 470 /* 471 * If there's no signal to deliver, we just put the saved sigmask back. 472 */ 473 restore_saved_sigmask(); 474 } 475 476 void do_notify_resume(struct pt_regs *regs) 477 { 478 clear_thread_flag(TIF_NOTIFY_RESUME); 479 tracehook_notify_resume(regs); 480 } 481