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 /* Returns non-zero on fault. */ 52 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs) 53 { 54 _sigregs user_sregs; 55 56 save_access_regs(current->thread.acrs); 57 58 /* Copy a 'clean' PSW mask to the user to avoid leaking 59 information about whether PER is currently on. */ 60 user_sregs.regs.psw.mask = PSW_USER_BITS | 61 (regs->psw.mask & PSW_MASK_USER); 62 user_sregs.regs.psw.addr = regs->psw.addr; 63 memcpy(&user_sregs.regs.gprs, ®s->gprs, sizeof(sregs->regs.gprs)); 64 memcpy(&user_sregs.regs.acrs, current->thread.acrs, 65 sizeof(user_sregs.regs.acrs)); 66 /* 67 * We have to store the fp registers to current->thread.fp_regs 68 * to merge them with the emulated registers. 69 */ 70 save_fp_ctl(¤t->thread.fp_regs.fpc); 71 save_fp_regs(current->thread.fp_regs.fprs); 72 memcpy(&user_sregs.fpregs, ¤t->thread.fp_regs, 73 sizeof(user_sregs.fpregs)); 74 if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs))) 75 return -EFAULT; 76 return 0; 77 } 78 79 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs) 80 { 81 _sigregs user_sregs; 82 83 /* Alwys make any pending restarted system call return -EINTR */ 84 current_thread_info()->restart_block.fn = do_no_restart_syscall; 85 86 if (__copy_from_user(&user_sregs, sregs, sizeof(user_sregs))) 87 return -EFAULT; 88 89 /* Loading the floating-point-control word can fail. Do that first. */ 90 if (restore_fp_ctl(&user_sregs.fpregs.fpc)) 91 return -EINVAL; 92 93 /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */ 94 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) | 95 (user_sregs.regs.psw.mask & PSW_MASK_USER); 96 /* Check for invalid user address space control. */ 97 if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME) 98 regs->psw.mask = PSW_ASC_PRIMARY | 99 (regs->psw.mask & ~PSW_MASK_ASC); 100 /* Check for invalid amode */ 101 if (regs->psw.mask & PSW_MASK_EA) 102 regs->psw.mask |= PSW_MASK_BA; 103 regs->psw.addr = user_sregs.regs.psw.addr; 104 memcpy(®s->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs)); 105 memcpy(¤t->thread.acrs, &user_sregs.regs.acrs, 106 sizeof(current->thread.acrs)); 107 restore_access_regs(current->thread.acrs); 108 109 memcpy(¤t->thread.fp_regs, &user_sregs.fpregs, 110 sizeof(current->thread.fp_regs)); 111 112 restore_fp_regs(current->thread.fp_regs.fprs); 113 clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */ 114 return 0; 115 } 116 117 SYSCALL_DEFINE0(sigreturn) 118 { 119 struct pt_regs *regs = task_pt_regs(current); 120 sigframe __user *frame = (sigframe __user *)regs->gprs[15]; 121 sigset_t set; 122 123 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE)) 124 goto badframe; 125 set_current_blocked(&set); 126 if (restore_sigregs(regs, &frame->sregs)) 127 goto badframe; 128 return regs->gprs[2]; 129 badframe: 130 force_sig(SIGSEGV, current); 131 return 0; 132 } 133 134 SYSCALL_DEFINE0(rt_sigreturn) 135 { 136 struct pt_regs *regs = task_pt_regs(current); 137 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15]; 138 sigset_t set; 139 140 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set))) 141 goto badframe; 142 set_current_blocked(&set); 143 if (restore_sigregs(regs, &frame->uc.uc_mcontext)) 144 goto badframe; 145 if (restore_altstack(&frame->uc.uc_stack)) 146 goto badframe; 147 return regs->gprs[2]; 148 badframe: 149 force_sig(SIGSEGV, current); 150 return 0; 151 } 152 153 /* 154 * Set up a signal frame. 155 */ 156 157 158 /* 159 * Determine which stack to use.. 160 */ 161 static inline void __user * 162 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) 163 { 164 unsigned long sp; 165 166 /* Default to using normal stack */ 167 sp = regs->gprs[15]; 168 169 /* Overflow on alternate signal stack gives SIGSEGV. */ 170 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL)) 171 return (void __user *) -1UL; 172 173 /* This is the X/Open sanctioned signal stack switching. */ 174 if (ka->sa.sa_flags & SA_ONSTACK) { 175 if (! sas_ss_flags(sp)) 176 sp = current->sas_ss_sp + current->sas_ss_size; 177 } 178 179 return (void __user *)((sp - frame_size) & -8ul); 180 } 181 182 static inline int map_signal(int sig) 183 { 184 if (current_thread_info()->exec_domain 185 && current_thread_info()->exec_domain->signal_invmap 186 && sig < 32) 187 return current_thread_info()->exec_domain->signal_invmap[sig]; 188 else 189 return sig; 190 } 191 192 static int setup_frame(int sig, struct k_sigaction *ka, 193 sigset_t *set, struct pt_regs * regs) 194 { 195 sigframe __user *frame; 196 197 frame = get_sigframe(ka, regs, sizeof(sigframe)); 198 199 if (frame == (void __user *) -1UL) 200 goto give_sigsegv; 201 202 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE)) 203 goto give_sigsegv; 204 205 if (save_sigregs(regs, &frame->sregs)) 206 goto give_sigsegv; 207 if (__put_user(&frame->sregs, &frame->sc.sregs)) 208 goto give_sigsegv; 209 210 /* Set up to return from userspace. If provided, use a stub 211 already in userspace. */ 212 if (ka->sa.sa_flags & SA_RESTORER) { 213 regs->gprs[14] = (unsigned long) 214 ka->sa.sa_restorer | PSW_ADDR_AMODE; 215 } else { 216 regs->gprs[14] = (unsigned long) 217 frame->retcode | PSW_ADDR_AMODE; 218 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, 219 (u16 __user *)(frame->retcode))) 220 goto give_sigsegv; 221 } 222 223 /* Set up backchain. */ 224 if (__put_user(regs->gprs[15], (addr_t __user *) frame)) 225 goto give_sigsegv; 226 227 /* Set up registers for signal handler */ 228 regs->gprs[15] = (unsigned long) frame; 229 /* Force default amode and default user address space control. */ 230 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA | 231 (PSW_USER_BITS & PSW_MASK_ASC) | 232 (regs->psw.mask & ~PSW_MASK_ASC); 233 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE; 234 235 regs->gprs[2] = map_signal(sig); 236 regs->gprs[3] = (unsigned long) &frame->sc; 237 238 /* We forgot to include these in the sigcontext. 239 To avoid breaking binary compatibility, they are passed as args. */ 240 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 241 sig == SIGTRAP || sig == SIGFPE) { 242 /* set extra registers only for synchronous signals */ 243 regs->gprs[4] = regs->int_code & 127; 244 regs->gprs[5] = regs->int_parm_long; 245 regs->gprs[6] = task_thread_info(current)->last_break; 246 } 247 248 /* Place signal number on stack to allow backtrace from handler. */ 249 if (__put_user(regs->gprs[2], (int __user *) &frame->signo)) 250 goto give_sigsegv; 251 return 0; 252 253 give_sigsegv: 254 force_sigsegv(sig, current); 255 return -EFAULT; 256 } 257 258 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 259 sigset_t *set, struct pt_regs * regs) 260 { 261 int err = 0; 262 rt_sigframe __user *frame; 263 264 frame = get_sigframe(ka, regs, sizeof(rt_sigframe)); 265 266 if (frame == (void __user *) -1UL) 267 goto give_sigsegv; 268 269 if (copy_siginfo_to_user(&frame->info, info)) 270 goto give_sigsegv; 271 272 /* Create the ucontext. */ 273 err |= __put_user(0, &frame->uc.uc_flags); 274 err |= __put_user(NULL, &frame->uc.uc_link); 275 err |= __save_altstack(&frame->uc.uc_stack, regs->gprs[15]); 276 err |= save_sigregs(regs, &frame->uc.uc_mcontext); 277 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 278 if (err) 279 goto give_sigsegv; 280 281 /* Set up to return from userspace. If provided, use a stub 282 already in userspace. */ 283 if (ka->sa.sa_flags & SA_RESTORER) { 284 regs->gprs[14] = (unsigned long) 285 ka->sa.sa_restorer | PSW_ADDR_AMODE; 286 } else { 287 regs->gprs[14] = (unsigned long) 288 frame->retcode | PSW_ADDR_AMODE; 289 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, 290 (u16 __user *)(frame->retcode))) 291 goto give_sigsegv; 292 } 293 294 /* Set up backchain. */ 295 if (__put_user(regs->gprs[15], (addr_t __user *) frame)) 296 goto give_sigsegv; 297 298 /* Set up registers for signal handler */ 299 regs->gprs[15] = (unsigned long) frame; 300 /* Force default amode and default user address space control. */ 301 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA | 302 (PSW_USER_BITS & PSW_MASK_ASC) | 303 (regs->psw.mask & ~PSW_MASK_ASC); 304 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE; 305 306 regs->gprs[2] = map_signal(sig); 307 regs->gprs[3] = (unsigned long) &frame->info; 308 regs->gprs[4] = (unsigned long) &frame->uc; 309 regs->gprs[5] = task_thread_info(current)->last_break; 310 return 0; 311 312 give_sigsegv: 313 force_sigsegv(sig, current); 314 return -EFAULT; 315 } 316 317 static void handle_signal(unsigned long sig, struct k_sigaction *ka, 318 siginfo_t *info, sigset_t *oldset, 319 struct pt_regs *regs) 320 { 321 int ret; 322 323 /* Set up the stack frame */ 324 if (ka->sa.sa_flags & SA_SIGINFO) 325 ret = setup_rt_frame(sig, ka, info, oldset, regs); 326 else 327 ret = setup_frame(sig, ka, oldset, regs); 328 if (ret) 329 return; 330 signal_delivered(sig, info, ka, regs, 331 test_thread_flag(TIF_SINGLE_STEP)); 332 } 333 334 /* 335 * Note that 'init' is a special process: it doesn't get signals it doesn't 336 * want to handle. Thus you cannot kill init even with a SIGKILL even by 337 * mistake. 338 * 339 * Note that we go through the signals twice: once to check the signals that 340 * the kernel can handle, and then we build all the user-level signal handling 341 * stack-frames in one go after that. 342 */ 343 void do_signal(struct pt_regs *regs) 344 { 345 siginfo_t info; 346 int signr; 347 struct k_sigaction ka; 348 sigset_t *oldset = sigmask_to_save(); 349 350 /* 351 * Get signal to deliver. When running under ptrace, at this point 352 * the debugger may change all our registers, including the system 353 * call information. 354 */ 355 current_thread_info()->system_call = 356 test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0; 357 signr = get_signal_to_deliver(&info, &ka, regs, NULL); 358 359 if (signr > 0) { 360 /* Whee! Actually deliver the signal. */ 361 if (current_thread_info()->system_call) { 362 regs->int_code = current_thread_info()->system_call; 363 /* Check for system call restarting. */ 364 switch (regs->gprs[2]) { 365 case -ERESTART_RESTARTBLOCK: 366 case -ERESTARTNOHAND: 367 regs->gprs[2] = -EINTR; 368 break; 369 case -ERESTARTSYS: 370 if (!(ka.sa.sa_flags & SA_RESTART)) { 371 regs->gprs[2] = -EINTR; 372 break; 373 } 374 /* fallthrough */ 375 case -ERESTARTNOINTR: 376 regs->gprs[2] = regs->orig_gpr2; 377 regs->psw.addr = 378 __rewind_psw(regs->psw, 379 regs->int_code >> 16); 380 break; 381 } 382 } 383 /* No longer in a system call */ 384 clear_thread_flag(TIF_SYSCALL); 385 386 if (is_compat_task()) 387 handle_signal32(signr, &ka, &info, oldset, regs); 388 else 389 handle_signal(signr, &ka, &info, oldset, regs); 390 return; 391 } 392 393 /* No handlers present - check for system call restart */ 394 clear_thread_flag(TIF_SYSCALL); 395 if (current_thread_info()->system_call) { 396 regs->int_code = current_thread_info()->system_call; 397 switch (regs->gprs[2]) { 398 case -ERESTART_RESTARTBLOCK: 399 /* Restart with sys_restart_syscall */ 400 regs->int_code = __NR_restart_syscall; 401 /* fallthrough */ 402 case -ERESTARTNOHAND: 403 case -ERESTARTSYS: 404 case -ERESTARTNOINTR: 405 /* Restart system call with magic TIF bit. */ 406 regs->gprs[2] = regs->orig_gpr2; 407 set_thread_flag(TIF_SYSCALL); 408 if (test_thread_flag(TIF_SINGLE_STEP)) 409 set_thread_flag(TIF_PER_TRAP); 410 break; 411 } 412 } 413 414 /* 415 * If there's no signal to deliver, we just put the saved sigmask back. 416 */ 417 restore_saved_sigmask(); 418 } 419 420 void do_notify_resume(struct pt_regs *regs) 421 { 422 clear_thread_flag(TIF_NOTIFY_RESUME); 423 tracehook_notify_resume(regs); 424 } 425