1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Signal Handling for ARC 4 * 5 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 6 * 7 * vineetg: Jan 2010 (Restarting of timer related syscalls) 8 * 9 * vineetg: Nov 2009 (Everything needed for TIF_RESTORE_SIGMASK) 10 * -do_signal() supports TIF_RESTORE_SIGMASK 11 * -do_signal() no loner needs oldset, required by OLD sys_sigsuspend 12 * -sys_rt_sigsuspend() now comes from generic code, so discard arch implemen 13 * -sys_sigsuspend() no longer needs to fudge ptregs, hence that arg removed 14 * -sys_sigsuspend() no longer loops for do_signal(), sets TIF_xxx and leaves 15 * the job to do_signal() 16 * 17 * vineetg: July 2009 18 * -Modified Code to support the uClibc provided userland sigreturn stub 19 * to avoid kernel synthesing it on user stack at runtime, costing TLB 20 * probes and Cache line flushes. 21 * 22 * vineetg: July 2009 23 * -In stash_usr_regs( ) and restore_usr_regs( ), save/restore of user regs 24 * in done in block copy rather than one word at a time. 25 * This saves around 2K of code and improves LMBench lat_sig <catch> 26 * 27 * rajeshwarr: Feb 2009 28 * - Support for Realtime Signals 29 * 30 * vineetg: Aug 11th 2008: Bug #94183 31 * -ViXS were still seeing crashes when using insmod to load drivers. 32 * It turned out that the code to change Execute permssions for TLB entries 33 * of user was not guarded for interrupts (mod_tlb_permission) 34 * This was causing TLB entries to be overwritten on unrelated indexes 35 * 36 * Vineetg: July 15th 2008: Bug #94183 37 * -Exception happens in Delay slot of a JMP, and before user space resumes, 38 * Signal is delivered (Ctrl + C) = >SIGINT. 39 * setup_frame( ) sets up PC,SP,BLINK to enable user space signal handler 40 * to run, but doesn't clear the Delay slot bit from status32. As a result, 41 * on resuming user mode, signal handler branches off to BTA of orig JMP 42 * -FIX: clear the DE bit from status32 in setup_frame( ) 43 * 44 * Rahul Trivedi, Kanika Nema: Codito Technologies 2004 45 */ 46 47 #include <linux/signal.h> 48 #include <linux/ptrace.h> 49 #include <linux/personality.h> 50 #include <linux/uaccess.h> 51 #include <linux/syscalls.h> 52 #include <linux/resume_user_mode.h> 53 #include <linux/sched/task_stack.h> 54 55 #include <asm/ucontext.h> 56 #include <asm/entry.h> 57 58 struct rt_sigframe { 59 struct siginfo info; 60 struct ucontext uc; 61 #define MAGIC_SIGALTSTK 0x07302004 62 unsigned int sigret_magic; 63 }; 64 65 static int save_arcv2_regs(struct sigcontext *mctx, struct pt_regs *regs) 66 { 67 int err = 0; 68 #ifndef CONFIG_ISA_ARCOMPACT 69 struct user_regs_arcv2 v2abi; 70 71 v2abi.r30 = regs->r30; 72 #ifdef CONFIG_ARC_HAS_ACCL_REGS 73 v2abi.r58 = regs->r58; 74 v2abi.r59 = regs->r59; 75 #else 76 v2abi.r58 = v2abi.r59 = 0; 77 #endif 78 err = __copy_to_user(&mctx->v2abi, &v2abi, sizeof(v2abi)); 79 #endif 80 return err; 81 } 82 83 static int restore_arcv2_regs(struct sigcontext *mctx, struct pt_regs *regs) 84 { 85 int err = 0; 86 #ifndef CONFIG_ISA_ARCOMPACT 87 struct user_regs_arcv2 v2abi; 88 89 err = __copy_from_user(&v2abi, &mctx->v2abi, sizeof(v2abi)); 90 91 regs->r30 = v2abi.r30; 92 #ifdef CONFIG_ARC_HAS_ACCL_REGS 93 regs->r58 = v2abi.r58; 94 regs->r59 = v2abi.r59; 95 #endif 96 #endif 97 return err; 98 } 99 100 static int 101 stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs, 102 sigset_t *set) 103 { 104 int err; 105 struct user_regs_struct uregs; 106 107 uregs.scratch.bta = regs->bta; 108 uregs.scratch.lp_start = regs->lp_start; 109 uregs.scratch.lp_end = regs->lp_end; 110 uregs.scratch.lp_count = regs->lp_count; 111 uregs.scratch.status32 = regs->status32; 112 uregs.scratch.ret = regs->ret; 113 uregs.scratch.blink = regs->blink; 114 uregs.scratch.fp = regs->fp; 115 uregs.scratch.gp = regs->r26; 116 uregs.scratch.r12 = regs->r12; 117 uregs.scratch.r11 = regs->r11; 118 uregs.scratch.r10 = regs->r10; 119 uregs.scratch.r9 = regs->r9; 120 uregs.scratch.r8 = regs->r8; 121 uregs.scratch.r7 = regs->r7; 122 uregs.scratch.r6 = regs->r6; 123 uregs.scratch.r5 = regs->r5; 124 uregs.scratch.r4 = regs->r4; 125 uregs.scratch.r3 = regs->r3; 126 uregs.scratch.r2 = regs->r2; 127 uregs.scratch.r1 = regs->r1; 128 uregs.scratch.r0 = regs->r0; 129 uregs.scratch.sp = regs->sp; 130 131 err = __copy_to_user(&(sf->uc.uc_mcontext.regs.scratch), &uregs.scratch, 132 sizeof(sf->uc.uc_mcontext.regs.scratch)); 133 134 if (is_isa_arcv2()) 135 err |= save_arcv2_regs(&(sf->uc.uc_mcontext), regs); 136 137 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(sigset_t)); 138 139 return err ? -EFAULT : 0; 140 } 141 142 static int restore_usr_regs(struct pt_regs *regs, struct rt_sigframe __user *sf) 143 { 144 sigset_t set; 145 int err; 146 struct user_regs_struct uregs; 147 148 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set)); 149 err |= __copy_from_user(&uregs.scratch, 150 &(sf->uc.uc_mcontext.regs.scratch), 151 sizeof(sf->uc.uc_mcontext.regs.scratch)); 152 153 if (is_isa_arcv2()) 154 err |= restore_arcv2_regs(&(sf->uc.uc_mcontext), regs); 155 156 if (err) 157 return -EFAULT; 158 159 set_current_blocked(&set); 160 regs->bta = uregs.scratch.bta; 161 regs->lp_start = uregs.scratch.lp_start; 162 regs->lp_end = uregs.scratch.lp_end; 163 regs->lp_count = uregs.scratch.lp_count; 164 regs->status32 = uregs.scratch.status32; 165 regs->ret = uregs.scratch.ret; 166 regs->blink = uregs.scratch.blink; 167 regs->fp = uregs.scratch.fp; 168 regs->r26 = uregs.scratch.gp; 169 regs->r12 = uregs.scratch.r12; 170 regs->r11 = uregs.scratch.r11; 171 regs->r10 = uregs.scratch.r10; 172 regs->r9 = uregs.scratch.r9; 173 regs->r8 = uregs.scratch.r8; 174 regs->r7 = uregs.scratch.r7; 175 regs->r6 = uregs.scratch.r6; 176 regs->r5 = uregs.scratch.r5; 177 regs->r4 = uregs.scratch.r4; 178 regs->r3 = uregs.scratch.r3; 179 regs->r2 = uregs.scratch.r2; 180 regs->r1 = uregs.scratch.r1; 181 regs->r0 = uregs.scratch.r0; 182 regs->sp = uregs.scratch.sp; 183 184 return 0; 185 } 186 187 static inline int is_do_ss_needed(unsigned int magic) 188 { 189 if (MAGIC_SIGALTSTK == magic) 190 return 1; 191 else 192 return 0; 193 } 194 195 SYSCALL_DEFINE0(rt_sigreturn) 196 { 197 struct rt_sigframe __user *sf; 198 unsigned int magic; 199 struct pt_regs *regs = current_pt_regs(); 200 201 /* Always make any pending restarted system calls return -EINTR */ 202 current->restart_block.fn = do_no_restart_syscall; 203 204 /* Since we stacked the signal on a word boundary, 205 * then 'sp' should be word aligned here. If it's 206 * not, then the user is trying to mess with us. 207 */ 208 if (regs->sp & 3) 209 goto badframe; 210 211 sf = (struct rt_sigframe __force __user *)(regs->sp); 212 213 if (!access_ok(sf, sizeof(*sf))) 214 goto badframe; 215 216 if (__get_user(magic, &sf->sigret_magic)) 217 goto badframe; 218 219 if (unlikely(is_do_ss_needed(magic))) 220 if (restore_altstack(&sf->uc.uc_stack)) 221 goto badframe; 222 223 if (restore_usr_regs(regs, sf)) 224 goto badframe; 225 226 /* Don't restart from sigreturn */ 227 syscall_wont_restart(regs); 228 229 /* 230 * Ensure that sigreturn always returns to user mode (in case the 231 * regs saved on user stack got fudged between save and sigreturn) 232 * Otherwise it is easy to panic the kernel with a custom 233 * signal handler and/or restorer which clobberes the status32/ret 234 * to return to a bogus location in kernel mode. 235 */ 236 regs->status32 |= STATUS_U_MASK; 237 238 return regs->r0; 239 240 badframe: 241 force_sig(SIGSEGV); 242 return 0; 243 } 244 245 /* 246 * Determine which stack to use.. 247 */ 248 static inline void __user *get_sigframe(struct ksignal *ksig, 249 struct pt_regs *regs, 250 unsigned long framesize) 251 { 252 unsigned long sp = sigsp(regs->sp, ksig); 253 void __user *frame; 254 255 /* No matter what happens, 'sp' must be word 256 * aligned otherwise nasty things could happen 257 */ 258 259 /* ATPCS B01 mandates 8-byte alignment */ 260 frame = (void __user *)((sp - framesize) & ~7); 261 262 /* Check that we can actually write to the signal frame */ 263 if (!access_ok(frame, framesize)) 264 frame = NULL; 265 266 return frame; 267 } 268 269 static int 270 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs) 271 { 272 struct rt_sigframe __user *sf; 273 unsigned int magic = 0; 274 int err = 0; 275 276 sf = get_sigframe(ksig, regs, sizeof(struct rt_sigframe)); 277 if (!sf) 278 return 1; 279 280 /* 281 * w/o SA_SIGINFO, struct ucontext is partially populated (only 282 * uc_mcontext/uc_sigmask) for kernel's normal user state preservation 283 * during signal handler execution. This works for SA_SIGINFO as well 284 * although the semantics are now overloaded (the same reg state can be 285 * inspected by userland: but are they allowed to fiddle with it ? 286 */ 287 err |= stash_usr_regs(sf, regs, set); 288 289 /* 290 * SA_SIGINFO requires 3 args to signal handler: 291 * #1: sig-no (common to any handler) 292 * #2: struct siginfo 293 * #3: struct ucontext (completely populated) 294 */ 295 if (unlikely(ksig->ka.sa.sa_flags & SA_SIGINFO)) { 296 err |= copy_siginfo_to_user(&sf->info, &ksig->info); 297 err |= __put_user(0, &sf->uc.uc_flags); 298 err |= __put_user(NULL, &sf->uc.uc_link); 299 err |= __save_altstack(&sf->uc.uc_stack, regs->sp); 300 301 /* setup args 2 and 3 for user mode handler */ 302 regs->r1 = (unsigned long)&sf->info; 303 regs->r2 = (unsigned long)&sf->uc; 304 305 /* 306 * small optim to avoid unconditionally calling do_sigaltstack 307 * in sigreturn path, now that we only have rt_sigreturn 308 */ 309 magic = MAGIC_SIGALTSTK; 310 } 311 312 err |= __put_user(magic, &sf->sigret_magic); 313 if (err) 314 return err; 315 316 /* #1 arg to the user Signal handler */ 317 regs->r0 = ksig->sig; 318 319 /* setup PC of user space signal handler */ 320 regs->ret = (unsigned long)ksig->ka.sa.sa_handler; 321 322 /* 323 * handler returns using sigreturn stub provided already by userspace 324 * If not, nuke the process right away 325 */ 326 if(!(ksig->ka.sa.sa_flags & SA_RESTORER)) 327 return 1; 328 329 regs->blink = (unsigned long)ksig->ka.sa.sa_restorer; 330 331 /* User Stack for signal handler will be above the frame just carved */ 332 regs->sp = (unsigned long)sf; 333 334 /* 335 * Bug 94183, Clear the DE bit, so that when signal handler 336 * starts to run, it doesn't use BTA 337 */ 338 regs->status32 &= ~STATUS_DE_MASK; 339 regs->status32 |= STATUS_L_MASK; 340 341 return err; 342 } 343 344 static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs) 345 { 346 switch (regs->r0) { 347 case -ERESTART_RESTARTBLOCK: 348 case -ERESTARTNOHAND: 349 /* 350 * ERESTARTNOHAND means that the syscall should 351 * only be restarted if there was no handler for 352 * the signal, and since we only get here if there 353 * is a handler, we don't restart 354 */ 355 regs->r0 = -EINTR; /* ERESTART_xxx is internal */ 356 break; 357 358 case -ERESTARTSYS: 359 /* 360 * ERESTARTSYS means to restart the syscall if 361 * there is no handler or the handler was 362 * registered with SA_RESTART 363 */ 364 if (!(ka->sa.sa_flags & SA_RESTART)) { 365 regs->r0 = -EINTR; 366 break; 367 } 368 fallthrough; 369 370 case -ERESTARTNOINTR: 371 /* 372 * ERESTARTNOINTR means that the syscall should 373 * be called again after the signal handler returns. 374 * Setup reg state just as it was before doing the trap 375 * r0 has been clobbered with sys call ret code thus it 376 * needs to be reloaded with orig first arg to syscall 377 * in orig_r0. Rest of relevant reg-file: 378 * r8 (syscall num) and (r1 - r7) will be reset to 379 * their orig user space value when we ret from kernel 380 */ 381 regs->r0 = regs->orig_r0; 382 regs->ret -= is_isa_arcv2() ? 2 : 4; 383 break; 384 } 385 } 386 387 /* 388 * OK, we're invoking a handler 389 */ 390 static void 391 handle_signal(struct ksignal *ksig, struct pt_regs *regs) 392 { 393 sigset_t *oldset = sigmask_to_save(); 394 int failed; 395 396 /* Set up the stack frame */ 397 failed = setup_rt_frame(ksig, oldset, regs); 398 399 signal_setup_done(failed, ksig, 0); 400 } 401 402 void do_signal(struct pt_regs *regs) 403 { 404 struct ksignal ksig; 405 int restart_scall; 406 407 restart_scall = in_syscall(regs) && syscall_restartable(regs); 408 409 if (test_thread_flag(TIF_SIGPENDING) && get_signal(&ksig)) { 410 if (restart_scall) { 411 arc_restart_syscall(&ksig.ka, regs); 412 syscall_wont_restart(regs); /* No more restarts */ 413 } 414 handle_signal(&ksig, regs); 415 return; 416 } 417 418 if (restart_scall) { 419 /* No handler for syscall: restart it */ 420 if (regs->r0 == -ERESTARTNOHAND || 421 regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) { 422 regs->r0 = regs->orig_r0; 423 regs->ret -= is_isa_arcv2() ? 2 : 4; 424 } else if (regs->r0 == -ERESTART_RESTARTBLOCK) { 425 regs->r8 = __NR_restart_syscall; 426 regs->ret -= is_isa_arcv2() ? 2 : 4; 427 } 428 syscall_wont_restart(regs); /* No more restarts */ 429 } 430 431 /* If there's no signal to deliver, restore the saved sigmask back */ 432 restore_saved_sigmask(); 433 } 434 435 void do_notify_resume(struct pt_regs *regs) 436 { 437 /* 438 * ASM glue guarantees that this is only called when returning to 439 * user mode 440 */ 441 if (test_thread_flag(TIF_NOTIFY_RESUME)) 442 resume_user_mode_work(regs); 443 } 444