1 /* 2 * Copyright IBM Corp. 2000, 2006 3 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com) 4 * Gerhard Tonn (ton@de.ibm.com) 5 * 6 * Copyright (C) 1991, 1992 Linus Torvalds 7 * 8 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 9 */ 10 11 #include <linux/compat.h> 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 <asm/ucontext.h> 26 #include <asm/uaccess.h> 27 #include <asm/lowcore.h> 28 #include <asm/switch_to.h> 29 #include "compat_linux.h" 30 #include "compat_ptrace.h" 31 #include "entry.h" 32 33 typedef struct 34 { 35 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32]; 36 struct sigcontext32 sc; 37 _sigregs32 sregs; 38 int signo; 39 __u32 gprs_high[NUM_GPRS]; 40 __u8 retcode[S390_SYSCALL_SIZE]; 41 } sigframe32; 42 43 typedef struct 44 { 45 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32]; 46 __u8 retcode[S390_SYSCALL_SIZE]; 47 compat_siginfo_t info; 48 struct ucontext32 uc; 49 __u32 gprs_high[NUM_GPRS]; 50 } rt_sigframe32; 51 52 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from) 53 { 54 int err; 55 56 /* If you change siginfo_t structure, please be sure 57 this code is fixed accordingly. 58 It should never copy any pad contained in the structure 59 to avoid security leaks, but must copy the generic 60 3 ints plus the relevant union member. 61 This routine must convert siginfo from 64bit to 32bit as well 62 at the same time. */ 63 err = __put_user(from->si_signo, &to->si_signo); 64 err |= __put_user(from->si_errno, &to->si_errno); 65 err |= __put_user((short)from->si_code, &to->si_code); 66 if (from->si_code < 0) 67 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE); 68 else { 69 switch (from->si_code >> 16) { 70 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */ 71 case __SI_MESGQ >> 16: 72 err |= __put_user(from->si_int, &to->si_int); 73 /* fallthrough */ 74 case __SI_KILL >> 16: 75 err |= __put_user(from->si_pid, &to->si_pid); 76 err |= __put_user(from->si_uid, &to->si_uid); 77 break; 78 case __SI_CHLD >> 16: 79 err |= __put_user(from->si_pid, &to->si_pid); 80 err |= __put_user(from->si_uid, &to->si_uid); 81 err |= __put_user(from->si_utime, &to->si_utime); 82 err |= __put_user(from->si_stime, &to->si_stime); 83 err |= __put_user(from->si_status, &to->si_status); 84 break; 85 case __SI_FAULT >> 16: 86 err |= __put_user((unsigned long) from->si_addr, 87 &to->si_addr); 88 break; 89 case __SI_POLL >> 16: 90 err |= __put_user(from->si_band, &to->si_band); 91 err |= __put_user(from->si_fd, &to->si_fd); 92 break; 93 case __SI_TIMER >> 16: 94 err |= __put_user(from->si_tid, &to->si_tid); 95 err |= __put_user(from->si_overrun, &to->si_overrun); 96 err |= __put_user(from->si_int, &to->si_int); 97 break; 98 default: 99 break; 100 } 101 } 102 return err ? -EFAULT : 0; 103 } 104 105 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from) 106 { 107 int err; 108 u32 tmp; 109 110 err = __get_user(to->si_signo, &from->si_signo); 111 err |= __get_user(to->si_errno, &from->si_errno); 112 err |= __get_user(to->si_code, &from->si_code); 113 114 if (to->si_code < 0) 115 err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE); 116 else { 117 switch (to->si_code >> 16) { 118 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */ 119 case __SI_MESGQ >> 16: 120 err |= __get_user(to->si_int, &from->si_int); 121 /* fallthrough */ 122 case __SI_KILL >> 16: 123 err |= __get_user(to->si_pid, &from->si_pid); 124 err |= __get_user(to->si_uid, &from->si_uid); 125 break; 126 case __SI_CHLD >> 16: 127 err |= __get_user(to->si_pid, &from->si_pid); 128 err |= __get_user(to->si_uid, &from->si_uid); 129 err |= __get_user(to->si_utime, &from->si_utime); 130 err |= __get_user(to->si_stime, &from->si_stime); 131 err |= __get_user(to->si_status, &from->si_status); 132 break; 133 case __SI_FAULT >> 16: 134 err |= __get_user(tmp, &from->si_addr); 135 to->si_addr = (void __force __user *) 136 (u64) (tmp & PSW32_ADDR_INSN); 137 break; 138 case __SI_POLL >> 16: 139 err |= __get_user(to->si_band, &from->si_band); 140 err |= __get_user(to->si_fd, &from->si_fd); 141 break; 142 case __SI_TIMER >> 16: 143 err |= __get_user(to->si_tid, &from->si_tid); 144 err |= __get_user(to->si_overrun, &from->si_overrun); 145 err |= __get_user(to->si_int, &from->si_int); 146 break; 147 default: 148 break; 149 } 150 } 151 return err ? -EFAULT : 0; 152 } 153 154 static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs) 155 { 156 _s390_regs_common32 regs32; 157 int err, i; 158 159 regs32.psw.mask = psw32_user_bits | 160 ((__u32)(regs->psw.mask >> 32) & PSW32_MASK_USER); 161 regs32.psw.addr = (__u32) regs->psw.addr | 162 (__u32)(regs->psw.mask & PSW_MASK_BA); 163 for (i = 0; i < NUM_GPRS; i++) 164 regs32.gprs[i] = (__u32) regs->gprs[i]; 165 save_access_regs(current->thread.acrs); 166 memcpy(regs32.acrs, current->thread.acrs, sizeof(regs32.acrs)); 167 err = __copy_to_user(&sregs->regs, ®s32, sizeof(regs32)); 168 if (err) 169 return err; 170 save_fp_regs(¤t->thread.fp_regs); 171 /* s390_fp_regs and _s390_fp_regs32 are the same ! */ 172 return __copy_to_user(&sregs->fpregs, ¤t->thread.fp_regs, 173 sizeof(_s390_fp_regs32)); 174 } 175 176 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs) 177 { 178 _s390_regs_common32 regs32; 179 int err, i; 180 181 /* Alwys make any pending restarted system call return -EINTR */ 182 current_thread_info()->restart_block.fn = do_no_restart_syscall; 183 184 err = __copy_from_user(®s32, &sregs->regs, sizeof(regs32)); 185 if (err) 186 return err; 187 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) | 188 (__u64)(regs32.psw.mask & PSW32_MASK_USER) << 32 | 189 (__u64)(regs32.psw.addr & PSW32_ADDR_AMODE); 190 /* Check for invalid user address space control. */ 191 if ((regs->psw.mask & PSW_MASK_ASC) >= (psw_kernel_bits & PSW_MASK_ASC)) 192 regs->psw.mask = (psw_user_bits & PSW_MASK_ASC) | 193 (regs->psw.mask & ~PSW_MASK_ASC); 194 regs->psw.addr = (__u64)(regs32.psw.addr & PSW32_ADDR_INSN); 195 for (i = 0; i < NUM_GPRS; i++) 196 regs->gprs[i] = (__u64) regs32.gprs[i]; 197 memcpy(current->thread.acrs, regs32.acrs, sizeof(current->thread.acrs)); 198 restore_access_regs(current->thread.acrs); 199 200 err = __copy_from_user(¤t->thread.fp_regs, &sregs->fpregs, 201 sizeof(_s390_fp_regs32)); 202 current->thread.fp_regs.fpc &= FPC_VALID_MASK; 203 if (err) 204 return err; 205 206 restore_fp_regs(¤t->thread.fp_regs); 207 clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */ 208 return 0; 209 } 210 211 static int save_sigregs_gprs_high(struct pt_regs *regs, __u32 __user *uregs) 212 { 213 __u32 gprs_high[NUM_GPRS]; 214 int i; 215 216 for (i = 0; i < NUM_GPRS; i++) 217 gprs_high[i] = regs->gprs[i] >> 32; 218 219 return __copy_to_user(uregs, &gprs_high, sizeof(gprs_high)); 220 } 221 222 static int restore_sigregs_gprs_high(struct pt_regs *regs, __u32 __user *uregs) 223 { 224 __u32 gprs_high[NUM_GPRS]; 225 int err, i; 226 227 err = __copy_from_user(&gprs_high, uregs, sizeof(gprs_high)); 228 if (err) 229 return err; 230 for (i = 0; i < NUM_GPRS; i++) 231 *(__u32 *)®s->gprs[i] = gprs_high[i]; 232 return 0; 233 } 234 235 asmlinkage long sys32_sigreturn(void) 236 { 237 struct pt_regs *regs = task_pt_regs(current); 238 sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15]; 239 sigset_t set; 240 241 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE32)) 242 goto badframe; 243 set_current_blocked(&set); 244 if (restore_sigregs32(regs, &frame->sregs)) 245 goto badframe; 246 if (restore_sigregs_gprs_high(regs, frame->gprs_high)) 247 goto badframe; 248 return regs->gprs[2]; 249 badframe: 250 force_sig(SIGSEGV, current); 251 return 0; 252 } 253 254 asmlinkage long sys32_rt_sigreturn(void) 255 { 256 struct pt_regs *regs = task_pt_regs(current); 257 rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15]; 258 sigset_t set; 259 260 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 261 goto badframe; 262 set_current_blocked(&set); 263 if (restore_sigregs32(regs, &frame->uc.uc_mcontext)) 264 goto badframe; 265 if (restore_sigregs_gprs_high(regs, frame->gprs_high)) 266 goto badframe; 267 if (compat_restore_altstack(&frame->uc.uc_stack)) 268 goto badframe; 269 return regs->gprs[2]; 270 badframe: 271 force_sig(SIGSEGV, current); 272 return 0; 273 } 274 275 /* 276 * Set up a signal frame. 277 */ 278 279 280 /* 281 * Determine which stack to use.. 282 */ 283 static inline void __user * 284 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) 285 { 286 unsigned long sp; 287 288 /* Default to using normal stack */ 289 sp = (unsigned long) A(regs->gprs[15]); 290 291 /* Overflow on alternate signal stack gives SIGSEGV. */ 292 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL)) 293 return (void __user *) -1UL; 294 295 /* This is the X/Open sanctioned signal stack switching. */ 296 if (ka->sa.sa_flags & SA_ONSTACK) { 297 if (! sas_ss_flags(sp)) 298 sp = current->sas_ss_sp + current->sas_ss_size; 299 } 300 301 return (void __user *)((sp - frame_size) & -8ul); 302 } 303 304 static inline int map_signal(int sig) 305 { 306 if (current_thread_info()->exec_domain 307 && current_thread_info()->exec_domain->signal_invmap 308 && sig < 32) 309 return current_thread_info()->exec_domain->signal_invmap[sig]; 310 else 311 return sig; 312 } 313 314 static int setup_frame32(int sig, struct k_sigaction *ka, 315 sigset_t *set, struct pt_regs * regs) 316 { 317 sigframe32 __user *frame = get_sigframe(ka, regs, sizeof(sigframe32)); 318 319 if (frame == (void __user *) -1UL) 320 goto give_sigsegv; 321 322 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE32)) 323 goto give_sigsegv; 324 325 if (save_sigregs32(regs, &frame->sregs)) 326 goto give_sigsegv; 327 if (save_sigregs_gprs_high(regs, frame->gprs_high)) 328 goto give_sigsegv; 329 if (__put_user((unsigned long) &frame->sregs, &frame->sc.sregs)) 330 goto give_sigsegv; 331 332 /* Set up to return from userspace. If provided, use a stub 333 already in userspace. */ 334 if (ka->sa.sa_flags & SA_RESTORER) { 335 regs->gprs[14] = (__u64 __force) ka->sa.sa_restorer | PSW32_ADDR_AMODE; 336 } else { 337 regs->gprs[14] = (__u64 __force) frame->retcode | PSW32_ADDR_AMODE; 338 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, 339 (u16 __force __user *)(frame->retcode))) 340 goto give_sigsegv; 341 } 342 343 /* Set up backchain. */ 344 if (__put_user(regs->gprs[15], (unsigned int __user *) frame)) 345 goto give_sigsegv; 346 347 /* Set up registers for signal handler */ 348 regs->gprs[15] = (__force __u64) frame; 349 /* Force 31 bit amode and default user address space control. */ 350 regs->psw.mask = PSW_MASK_BA | 351 (psw_user_bits & PSW_MASK_ASC) | 352 (regs->psw.mask & ~PSW_MASK_ASC); 353 regs->psw.addr = (__force __u64) ka->sa.sa_handler; 354 355 regs->gprs[2] = map_signal(sig); 356 regs->gprs[3] = (__force __u64) &frame->sc; 357 358 /* We forgot to include these in the sigcontext. 359 To avoid breaking binary compatibility, they are passed as args. */ 360 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 361 sig == SIGTRAP || sig == SIGFPE) { 362 /* set extra registers only for synchronous signals */ 363 regs->gprs[4] = regs->int_code & 127; 364 regs->gprs[5] = regs->int_parm_long; 365 regs->gprs[6] = task_thread_info(current)->last_break; 366 } 367 368 /* Place signal number on stack to allow backtrace from handler. */ 369 if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo)) 370 goto give_sigsegv; 371 return 0; 372 373 give_sigsegv: 374 force_sigsegv(sig, current); 375 return -EFAULT; 376 } 377 378 static int setup_rt_frame32(int sig, struct k_sigaction *ka, siginfo_t *info, 379 sigset_t *set, struct pt_regs * regs) 380 { 381 int err = 0; 382 rt_sigframe32 __user *frame = get_sigframe(ka, regs, sizeof(rt_sigframe32)); 383 384 if (frame == (void __user *) -1UL) 385 goto give_sigsegv; 386 387 if (copy_siginfo_to_user32(&frame->info, info)) 388 goto give_sigsegv; 389 390 /* Create the ucontext. */ 391 err |= __put_user(UC_EXTENDED, &frame->uc.uc_flags); 392 err |= __put_user(0, &frame->uc.uc_link); 393 err |= __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]); 394 err |= save_sigregs32(regs, &frame->uc.uc_mcontext); 395 err |= save_sigregs_gprs_high(regs, frame->gprs_high); 396 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 397 if (err) 398 goto give_sigsegv; 399 400 /* Set up to return from userspace. If provided, use a stub 401 already in userspace. */ 402 if (ka->sa.sa_flags & SA_RESTORER) { 403 regs->gprs[14] = (__u64 __force) ka->sa.sa_restorer | PSW32_ADDR_AMODE; 404 } else { 405 regs->gprs[14] = (__u64 __force) frame->retcode | PSW32_ADDR_AMODE; 406 err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, 407 (u16 __force __user *)(frame->retcode)); 408 } 409 410 /* Set up backchain. */ 411 if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame)) 412 goto give_sigsegv; 413 414 /* Set up registers for signal handler */ 415 regs->gprs[15] = (__force __u64) frame; 416 /* Force 31 bit amode and default user address space control. */ 417 regs->psw.mask = PSW_MASK_BA | 418 (psw_user_bits & PSW_MASK_ASC) | 419 (regs->psw.mask & ~PSW_MASK_ASC); 420 regs->psw.addr = (__u64 __force) ka->sa.sa_handler; 421 422 regs->gprs[2] = map_signal(sig); 423 regs->gprs[3] = (__force __u64) &frame->info; 424 regs->gprs[4] = (__force __u64) &frame->uc; 425 regs->gprs[5] = task_thread_info(current)->last_break; 426 return 0; 427 428 give_sigsegv: 429 force_sigsegv(sig, current); 430 return -EFAULT; 431 } 432 433 /* 434 * OK, we're invoking a handler 435 */ 436 437 void handle_signal32(unsigned long sig, struct k_sigaction *ka, 438 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs) 439 { 440 int ret; 441 442 /* Set up the stack frame */ 443 if (ka->sa.sa_flags & SA_SIGINFO) 444 ret = setup_rt_frame32(sig, ka, info, oldset, regs); 445 else 446 ret = setup_frame32(sig, ka, oldset, regs); 447 if (ret) 448 return; 449 signal_delivered(sig, info, ka, regs, 450 test_thread_flag(TIF_SINGLE_STEP)); 451 } 452 453