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