Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/arch/alpha/kernel/signal.c |
| 4 | * |
| 5 | * Copyright (C) 1995 Linus Torvalds |
| 6 | * |
| 7 | * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson |
| 8 | */ |
| 9 | |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 10 | #include <linux/sched/signal.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 11 | #include <linux/sched/task_stack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/signal.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/wait.h> |
| 16 | #include <linux/ptrace.h> |
| 17 | #include <linux/unistd.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/stddef.h> |
| 21 | #include <linux/tty.h> |
| 22 | #include <linux/binfmts.h> |
| 23 | #include <linux/bitops.h> |
Ivan Kokshaysky | e5d9a90 | 2009-01-29 14:25:18 -0800 | [diff] [blame] | 24 | #include <linux/syscalls.h> |
David Howells | 733e5e4 | 2009-09-09 08:30:21 +0100 | [diff] [blame] | 25 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/sigcontext.h> |
| 29 | #include <asm/ucontext.h> |
| 30 | |
| 31 | #include "proto.h" |
| 32 | |
| 33 | |
| 34 | #define DEBUG_SIG 0 |
| 35 | |
| 36 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) |
| 37 | |
| 38 | asmlinkage void ret_from_sys_call(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * The OSF/1 sigprocmask calling sequence is different from the |
| 42 | * C sigprocmask() sequence.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | */ |
Al Viro | c52c2dd | 2010-09-26 19:28:12 +0100 | [diff] [blame] | 44 | SYSCALL_DEFINE2(osf_sigprocmask, int, how, unsigned long, newmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Al Viro | c52c2dd | 2010-09-26 19:28:12 +0100 | [diff] [blame] | 46 | sigset_t oldmask; |
| 47 | sigset_t mask; |
| 48 | unsigned long res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Linus Torvalds | 87400e5 | 2010-09-30 08:37:38 -0700 | [diff] [blame] | 50 | siginitset(&mask, newmask & _BLOCKABLE); |
Linus Torvalds | 0f44fbd | 2010-09-28 13:26:57 -0700 | [diff] [blame] | 51 | res = sigprocmask(how, &mask, &oldmask); |
Al Viro | c52c2dd | 2010-09-26 19:28:12 +0100 | [diff] [blame] | 52 | if (!res) { |
| 53 | force_successful_syscall_return(); |
Linus Torvalds | 0f44fbd | 2010-09-28 13:26:57 -0700 | [diff] [blame] | 54 | res = oldmask.sig[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
Al Viro | c52c2dd | 2010-09-26 19:28:12 +0100 | [diff] [blame] | 56 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Ivan Kokshaysky | e5d9a90 | 2009-01-29 14:25:18 -0800 | [diff] [blame] | 59 | SYSCALL_DEFINE3(osf_sigaction, int, sig, |
| 60 | const struct osf_sigaction __user *, act, |
| 61 | struct osf_sigaction __user *, oact) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
| 63 | struct k_sigaction new_ka, old_ka; |
| 64 | int ret; |
| 65 | |
| 66 | if (act) { |
| 67 | old_sigset_t mask; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 68 | if (!access_ok(act, sizeof(*act)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | __get_user(new_ka.sa.sa_handler, &act->sa_handler) || |
Al Viro | 18e6bfa | 2010-09-26 19:28:22 +0100 | [diff] [blame] | 70 | __get_user(new_ka.sa.sa_flags, &act->sa_flags) || |
| 71 | __get_user(mask, &act->sa_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | siginitset(&new_ka.sa.sa_mask, mask); |
| 74 | new_ka.ka_restorer = NULL; |
| 75 | } |
| 76 | |
| 77 | ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); |
| 78 | |
| 79 | if (!ret && oact) { |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 80 | if (!access_ok(oact, sizeof(*oact)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || |
Al Viro | 18e6bfa | 2010-09-26 19:28:22 +0100 | [diff] [blame] | 82 | __put_user(old_ka.sa.sa_flags, &oact->sa_flags) || |
| 83 | __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
Ivan Kokshaysky | e5d9a90 | 2009-01-29 14:25:18 -0800 | [diff] [blame] | 90 | SYSCALL_DEFINE5(rt_sigaction, int, sig, const struct sigaction __user *, act, |
| 91 | struct sigaction __user *, oact, |
| 92 | size_t, sigsetsize, void __user *, restorer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | struct k_sigaction new_ka, old_ka; |
| 95 | int ret; |
| 96 | |
| 97 | /* XXX: Don't preclude handling different sized sigset_t's. */ |
| 98 | if (sigsetsize != sizeof(sigset_t)) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | if (act) { |
| 102 | new_ka.ka_restorer = restorer; |
| 103 | if (copy_from_user(&new_ka.sa, act, sizeof(*act))) |
| 104 | return -EFAULT; |
| 105 | } |
| 106 | |
| 107 | ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); |
| 108 | |
| 109 | if (!ret && oact) { |
| 110 | if (copy_to_user(oact, &old_ka.sa, sizeof(*oact))) |
| 111 | return -EFAULT; |
| 112 | } |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | * Do a signal return; undo the signal stack. |
| 119 | */ |
| 120 | |
| 121 | #if _NSIG_WORDS > 1 |
| 122 | # error "Non SA_SIGINFO frame needs rearranging" |
| 123 | #endif |
| 124 | |
| 125 | struct sigframe |
| 126 | { |
| 127 | struct sigcontext sc; |
| 128 | unsigned int retcode[3]; |
| 129 | }; |
| 130 | |
| 131 | struct rt_sigframe |
| 132 | { |
| 133 | struct siginfo info; |
| 134 | struct ucontext uc; |
| 135 | unsigned int retcode[3]; |
| 136 | }; |
| 137 | |
| 138 | /* If this changes, userland unwinders that Know Things about our signal |
| 139 | frame will break. Do not undertake lightly. It also implies an ABI |
| 140 | change wrt the size of siginfo_t, which may cause some pain. */ |
| 141 | extern char compile_time_assert |
| 142 | [offsetof(struct rt_sigframe, uc.uc_mcontext) == 176 ? 1 : -1]; |
| 143 | |
| 144 | #define INSN_MOV_R30_R16 0x47fe0410 |
| 145 | #define INSN_LDI_R0 0x201f0000 |
| 146 | #define INSN_CALLSYS 0x00000083 |
| 147 | |
| 148 | static long |
Al Viro | b960f30 | 2012-10-18 01:43:27 -0400 | [diff] [blame] | 149 | restore_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | unsigned long usp; |
Al Viro | b960f30 | 2012-10-18 01:43:27 -0400 | [diff] [blame] | 152 | struct switch_stack *sw = (struct switch_stack *)regs - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | long i, err = __get_user(regs->pc, &sc->sc_pc); |
| 154 | |
Andy Lutomirski | f56141e | 2015-02-12 15:01:14 -0800 | [diff] [blame] | 155 | current->restart_block.fn = do_no_restart_syscall; |
Al Viro | 2deba1b | 2010-09-18 08:38:47 -0400 | [diff] [blame] | 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | sw->r26 = (unsigned long) ret_from_sys_call; |
| 158 | |
| 159 | err |= __get_user(regs->r0, sc->sc_regs+0); |
| 160 | err |= __get_user(regs->r1, sc->sc_regs+1); |
| 161 | err |= __get_user(regs->r2, sc->sc_regs+2); |
| 162 | err |= __get_user(regs->r3, sc->sc_regs+3); |
| 163 | err |= __get_user(regs->r4, sc->sc_regs+4); |
| 164 | err |= __get_user(regs->r5, sc->sc_regs+5); |
| 165 | err |= __get_user(regs->r6, sc->sc_regs+6); |
| 166 | err |= __get_user(regs->r7, sc->sc_regs+7); |
| 167 | err |= __get_user(regs->r8, sc->sc_regs+8); |
| 168 | err |= __get_user(sw->r9, sc->sc_regs+9); |
| 169 | err |= __get_user(sw->r10, sc->sc_regs+10); |
| 170 | err |= __get_user(sw->r11, sc->sc_regs+11); |
| 171 | err |= __get_user(sw->r12, sc->sc_regs+12); |
| 172 | err |= __get_user(sw->r13, sc->sc_regs+13); |
| 173 | err |= __get_user(sw->r14, sc->sc_regs+14); |
| 174 | err |= __get_user(sw->r15, sc->sc_regs+15); |
| 175 | err |= __get_user(regs->r16, sc->sc_regs+16); |
| 176 | err |= __get_user(regs->r17, sc->sc_regs+17); |
| 177 | err |= __get_user(regs->r18, sc->sc_regs+18); |
| 178 | err |= __get_user(regs->r19, sc->sc_regs+19); |
| 179 | err |= __get_user(regs->r20, sc->sc_regs+20); |
| 180 | err |= __get_user(regs->r21, sc->sc_regs+21); |
| 181 | err |= __get_user(regs->r22, sc->sc_regs+22); |
| 182 | err |= __get_user(regs->r23, sc->sc_regs+23); |
| 183 | err |= __get_user(regs->r24, sc->sc_regs+24); |
| 184 | err |= __get_user(regs->r25, sc->sc_regs+25); |
| 185 | err |= __get_user(regs->r26, sc->sc_regs+26); |
| 186 | err |= __get_user(regs->r27, sc->sc_regs+27); |
| 187 | err |= __get_user(regs->r28, sc->sc_regs+28); |
| 188 | err |= __get_user(regs->gp, sc->sc_regs+29); |
| 189 | err |= __get_user(usp, sc->sc_regs+30); |
| 190 | wrusp(usp); |
| 191 | |
| 192 | for (i = 0; i < 31; i++) |
| 193 | err |= __get_user(sw->fp[i], sc->sc_fpregs+i); |
| 194 | err |= __get_user(sw->fp[31], &sc->sc_fpcr); |
| 195 | |
| 196 | return err; |
| 197 | } |
| 198 | |
| 199 | /* Note that this syscall is also used by setcontext(3) to install |
| 200 | a given sigcontext. This because it's impossible to set *all* |
| 201 | registers and transfer control from userland. */ |
| 202 | |
| 203 | asmlinkage void |
Al Viro | b960f30 | 2012-10-18 01:43:27 -0400 | [diff] [blame] | 204 | do_sigreturn(struct sigcontext __user *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Al Viro | b960f30 | 2012-10-18 01:43:27 -0400 | [diff] [blame] | 206 | struct pt_regs *regs = current_pt_regs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | sigset_t set; |
| 208 | |
| 209 | /* Verify that it's a good sigcontext before using it */ |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 210 | if (!access_ok(sc, sizeof(*sc))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | goto give_sigsegv; |
| 212 | if (__get_user(set.sig[0], &sc->sc_mask)) |
| 213 | goto give_sigsegv; |
| 214 | |
Matt Fleming | 2561b06 | 2012-04-05 14:25:12 -0700 | [diff] [blame] | 215 | set_current_blocked(&set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Al Viro | b960f30 | 2012-10-18 01:43:27 -0400 | [diff] [blame] | 217 | if (restore_sigcontext(sc, regs)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | goto give_sigsegv; |
| 219 | |
| 220 | /* Send SIGTRAP if we're single-stepping: */ |
| 221 | if (ptrace_cancel_bpt (current)) { |
Eric W. Biederman | 5f50245 | 2018-04-17 17:37:49 -0500 | [diff] [blame] | 222 | send_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *) regs->pc, 0, |
| 223 | current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | return; |
| 226 | |
| 227 | give_sigsegv: |
Eric W. Biederman | 3cf5d07 | 2019-05-23 10:17:27 -0500 | [diff] [blame] | 228 | force_sig(SIGSEGV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | asmlinkage void |
Al Viro | b960f30 | 2012-10-18 01:43:27 -0400 | [diff] [blame] | 232 | do_rt_sigreturn(struct rt_sigframe __user *frame) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Al Viro | b960f30 | 2012-10-18 01:43:27 -0400 | [diff] [blame] | 234 | struct pt_regs *regs = current_pt_regs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | sigset_t set; |
| 236 | |
| 237 | /* Verify that it's a good ucontext_t before using it */ |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 238 | if (!access_ok(&frame->uc, sizeof(frame->uc))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | goto give_sigsegv; |
| 240 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
| 241 | goto give_sigsegv; |
| 242 | |
Matt Fleming | 2561b06 | 2012-04-05 14:25:12 -0700 | [diff] [blame] | 243 | set_current_blocked(&set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Al Viro | b960f30 | 2012-10-18 01:43:27 -0400 | [diff] [blame] | 245 | if (restore_sigcontext(&frame->uc.uc_mcontext, regs)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | goto give_sigsegv; |
| 247 | |
| 248 | /* Send SIGTRAP if we're single-stepping: */ |
| 249 | if (ptrace_cancel_bpt (current)) { |
Eric W. Biederman | 5f50245 | 2018-04-17 17:37:49 -0500 | [diff] [blame] | 250 | send_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *) regs->pc, 0, |
| 251 | current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | return; |
| 254 | |
| 255 | give_sigsegv: |
Eric W. Biederman | 3cf5d07 | 2019-05-23 10:17:27 -0500 | [diff] [blame] | 256 | force_sig(SIGSEGV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| 260 | /* |
| 261 | * Set up a signal frame. |
| 262 | */ |
| 263 | |
| 264 | static inline void __user * |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 265 | get_sigframe(struct ksignal *ksig, unsigned long sp, size_t frame_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 267 | return (void __user *)((sigsp(sp, ksig) - frame_size) & -32ul); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static long |
| 271 | setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, |
Al Viro | d9d0738 | 2012-09-05 18:53:18 -0400 | [diff] [blame] | 272 | unsigned long mask, unsigned long sp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
Al Viro | d9d0738 | 2012-09-05 18:53:18 -0400 | [diff] [blame] | 274 | struct switch_stack *sw = (struct switch_stack *)regs - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | long i, err = 0; |
| 276 | |
| 277 | err |= __put_user(on_sig_stack((unsigned long)sc), &sc->sc_onstack); |
| 278 | err |= __put_user(mask, &sc->sc_mask); |
| 279 | err |= __put_user(regs->pc, &sc->sc_pc); |
| 280 | err |= __put_user(8, &sc->sc_ps); |
| 281 | |
| 282 | err |= __put_user(regs->r0 , sc->sc_regs+0); |
| 283 | err |= __put_user(regs->r1 , sc->sc_regs+1); |
| 284 | err |= __put_user(regs->r2 , sc->sc_regs+2); |
| 285 | err |= __put_user(regs->r3 , sc->sc_regs+3); |
| 286 | err |= __put_user(regs->r4 , sc->sc_regs+4); |
| 287 | err |= __put_user(regs->r5 , sc->sc_regs+5); |
| 288 | err |= __put_user(regs->r6 , sc->sc_regs+6); |
| 289 | err |= __put_user(regs->r7 , sc->sc_regs+7); |
| 290 | err |= __put_user(regs->r8 , sc->sc_regs+8); |
| 291 | err |= __put_user(sw->r9 , sc->sc_regs+9); |
| 292 | err |= __put_user(sw->r10 , sc->sc_regs+10); |
| 293 | err |= __put_user(sw->r11 , sc->sc_regs+11); |
| 294 | err |= __put_user(sw->r12 , sc->sc_regs+12); |
| 295 | err |= __put_user(sw->r13 , sc->sc_regs+13); |
| 296 | err |= __put_user(sw->r14 , sc->sc_regs+14); |
| 297 | err |= __put_user(sw->r15 , sc->sc_regs+15); |
| 298 | err |= __put_user(regs->r16, sc->sc_regs+16); |
| 299 | err |= __put_user(regs->r17, sc->sc_regs+17); |
| 300 | err |= __put_user(regs->r18, sc->sc_regs+18); |
| 301 | err |= __put_user(regs->r19, sc->sc_regs+19); |
| 302 | err |= __put_user(regs->r20, sc->sc_regs+20); |
| 303 | err |= __put_user(regs->r21, sc->sc_regs+21); |
| 304 | err |= __put_user(regs->r22, sc->sc_regs+22); |
| 305 | err |= __put_user(regs->r23, sc->sc_regs+23); |
| 306 | err |= __put_user(regs->r24, sc->sc_regs+24); |
| 307 | err |= __put_user(regs->r25, sc->sc_regs+25); |
| 308 | err |= __put_user(regs->r26, sc->sc_regs+26); |
| 309 | err |= __put_user(regs->r27, sc->sc_regs+27); |
| 310 | err |= __put_user(regs->r28, sc->sc_regs+28); |
| 311 | err |= __put_user(regs->gp , sc->sc_regs+29); |
| 312 | err |= __put_user(sp, sc->sc_regs+30); |
| 313 | err |= __put_user(0, sc->sc_regs+31); |
| 314 | |
| 315 | for (i = 0; i < 31; i++) |
| 316 | err |= __put_user(sw->fp[i], sc->sc_fpregs+i); |
| 317 | err |= __put_user(0, sc->sc_fpregs+31); |
| 318 | err |= __put_user(sw->fp[31], &sc->sc_fpcr); |
| 319 | |
| 320 | err |= __put_user(regs->trap_a0, &sc->sc_traparg_a0); |
| 321 | err |= __put_user(regs->trap_a1, &sc->sc_traparg_a1); |
| 322 | err |= __put_user(regs->trap_a2, &sc->sc_traparg_a2); |
| 323 | |
| 324 | return err; |
| 325 | } |
| 326 | |
Richard Henderson | b927b3e | 2007-05-29 16:03:28 -0700 | [diff] [blame] | 327 | static int |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 328 | setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | unsigned long oldsp, r26, err = 0; |
| 331 | struct sigframe __user *frame; |
| 332 | |
| 333 | oldsp = rdusp(); |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 334 | frame = get_sigframe(ksig, oldsp, sizeof(*frame)); |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 335 | if (!access_ok(frame, sizeof(*frame))) |
Al Viro | cbdfb9f | 2012-04-22 02:34:42 -0400 | [diff] [blame] | 336 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Al Viro | d9d0738 | 2012-09-05 18:53:18 -0400 | [diff] [blame] | 338 | err |= setup_sigcontext(&frame->sc, regs, set->sig[0], oldsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | if (err) |
Al Viro | cbdfb9f | 2012-04-22 02:34:42 -0400 | [diff] [blame] | 340 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
| 342 | /* Set up to return from userspace. If provided, use a stub |
| 343 | already in userspace. */ |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 344 | r26 = (unsigned long) ksig->ka.ka_restorer; |
| 345 | if (!r26) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0); |
| 347 | err |= __put_user(INSN_LDI_R0+__NR_sigreturn, frame->retcode+1); |
| 348 | err |= __put_user(INSN_CALLSYS, frame->retcode+2); |
| 349 | imb(); |
| 350 | r26 = (unsigned long) frame->retcode; |
| 351 | } |
| 352 | |
| 353 | /* Check that everything was written properly. */ |
| 354 | if (err) |
Al Viro | cbdfb9f | 2012-04-22 02:34:42 -0400 | [diff] [blame] | 355 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| 357 | /* "Return" to the handler */ |
| 358 | regs->r26 = r26; |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 359 | regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler; |
| 360 | regs->r16 = ksig->sig; /* a0: signal number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | regs->r17 = 0; /* a1: exception code */ |
| 362 | regs->r18 = (unsigned long) &frame->sc; /* a2: sigcontext pointer */ |
| 363 | wrusp((unsigned long) frame); |
| 364 | |
| 365 | #if DEBUG_SIG |
| 366 | printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n", |
| 367 | current->comm, current->pid, frame, regs->pc, regs->r26); |
| 368 | #endif |
Richard Henderson | b927b3e | 2007-05-29 16:03:28 -0700 | [diff] [blame] | 369 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Richard Henderson | b927b3e | 2007-05-29 16:03:28 -0700 | [diff] [blame] | 372 | static int |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 373 | setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
| 375 | unsigned long oldsp, r26, err = 0; |
| 376 | struct rt_sigframe __user *frame; |
| 377 | |
| 378 | oldsp = rdusp(); |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 379 | frame = get_sigframe(ksig, oldsp, sizeof(*frame)); |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 380 | if (!access_ok(frame, sizeof(*frame))) |
Al Viro | cbdfb9f | 2012-04-22 02:34:42 -0400 | [diff] [blame] | 381 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 383 | err |= copy_siginfo_to_user(&frame->info, &ksig->info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | /* Create the ucontext. */ |
| 386 | err |= __put_user(0, &frame->uc.uc_flags); |
| 387 | err |= __put_user(0, &frame->uc.uc_link); |
| 388 | err |= __put_user(set->sig[0], &frame->uc.uc_osf_sigmask); |
Al Viro | 50ececc | 2012-12-14 15:25:43 -0500 | [diff] [blame] | 389 | err |= __save_altstack(&frame->uc.uc_stack, oldsp); |
Al Viro | d9d0738 | 2012-09-05 18:53:18 -0400 | [diff] [blame] | 390 | err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | set->sig[0], oldsp); |
| 392 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 393 | if (err) |
Al Viro | cbdfb9f | 2012-04-22 02:34:42 -0400 | [diff] [blame] | 394 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | /* Set up to return from userspace. If provided, use a stub |
| 397 | already in userspace. */ |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 398 | r26 = (unsigned long) ksig->ka.ka_restorer; |
| 399 | if (!r26) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0); |
| 401 | err |= __put_user(INSN_LDI_R0+__NR_rt_sigreturn, |
| 402 | frame->retcode+1); |
| 403 | err |= __put_user(INSN_CALLSYS, frame->retcode+2); |
| 404 | imb(); |
| 405 | r26 = (unsigned long) frame->retcode; |
| 406 | } |
| 407 | |
| 408 | if (err) |
Al Viro | cbdfb9f | 2012-04-22 02:34:42 -0400 | [diff] [blame] | 409 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | /* "Return" to the handler */ |
| 412 | regs->r26 = r26; |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 413 | regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler; |
| 414 | regs->r16 = ksig->sig; /* a0: signal number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | regs->r17 = (unsigned long) &frame->info; /* a1: siginfo pointer */ |
| 416 | regs->r18 = (unsigned long) &frame->uc; /* a2: ucontext pointer */ |
| 417 | wrusp((unsigned long) frame); |
| 418 | |
| 419 | #if DEBUG_SIG |
| 420 | printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n", |
| 421 | current->comm, current->pid, frame, regs->pc, regs->r26); |
| 422 | #endif |
| 423 | |
Richard Henderson | b927b3e | 2007-05-29 16:03:28 -0700 | [diff] [blame] | 424 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | |
| 428 | /* |
| 429 | * OK, we're invoking a handler. |
| 430 | */ |
Al Viro | cbdfb9f | 2012-04-22 02:34:42 -0400 | [diff] [blame] | 431 | static inline void |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 432 | handle_signal(struct ksignal *ksig, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
Al Viro | b7f9a11 | 2012-05-02 09:59:21 -0400 | [diff] [blame] | 434 | sigset_t *oldset = sigmask_to_save(); |
Richard Henderson | b927b3e | 2007-05-29 16:03:28 -0700 | [diff] [blame] | 435 | int ret; |
| 436 | |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 437 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) |
| 438 | ret = setup_rt_frame(ksig, oldset, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | else |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 440 | ret = setup_frame(ksig, oldset, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 442 | signal_setup_done(ret, ksig, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static inline void |
| 446 | syscall_restart(unsigned long r0, unsigned long r19, |
| 447 | struct pt_regs *regs, struct k_sigaction *ka) |
| 448 | { |
| 449 | switch (regs->r0) { |
| 450 | case ERESTARTSYS: |
| 451 | if (!(ka->sa.sa_flags & SA_RESTART)) { |
| 452 | case ERESTARTNOHAND: |
| 453 | regs->r0 = EINTR; |
| 454 | break; |
| 455 | } |
| 456 | /* fallthrough */ |
| 457 | case ERESTARTNOINTR: |
| 458 | regs->r0 = r0; /* reset v0 and a3 and replay syscall */ |
| 459 | regs->r19 = r19; |
| 460 | regs->pc -= 4; |
| 461 | break; |
| 462 | case ERESTART_RESTARTBLOCK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | regs->r0 = EINTR; |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /* |
| 470 | * Note that 'init' is a special process: it doesn't get signals it doesn't |
| 471 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 472 | * mistake. |
| 473 | * |
| 474 | * Note that we go through the signals twice: once to check the signals that |
| 475 | * the kernel can handle, and then we build all the user-level signal handling |
| 476 | * stack-frames in one go after that. |
| 477 | * |
| 478 | * "r0" and "r19" are the registers we need to restore for system call |
| 479 | * restart. "r0" is also used as an indicator whether we can restart at |
| 480 | * all (if we get here from anything but a syscall return, it will be 0) |
| 481 | */ |
Richard Henderson | b927b3e | 2007-05-29 16:03:28 -0700 | [diff] [blame] | 482 | static void |
Al Viro | d9d0738 | 2012-09-05 18:53:18 -0400 | [diff] [blame] | 483 | do_signal(struct pt_regs *regs, unsigned long r0, unsigned long r19) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | unsigned long single_stepping = ptrace_cancel_bpt(current); |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 486 | struct ksignal ksig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | /* This lets the debugger run, ... */ |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 489 | if (get_signal(&ksig)) { |
| 490 | /* ... so re-check the single stepping. */ |
| 491 | single_stepping |= ptrace_cancel_bpt(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /* Whee! Actually deliver the signal. */ |
Richard Henderson | b927b3e | 2007-05-29 16:03:28 -0700 | [diff] [blame] | 493 | if (r0) |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 494 | syscall_restart(r0, r19, regs, &ksig.ka); |
| 495 | handle_signal(&ksig, regs); |
| 496 | } else { |
| 497 | single_stepping |= ptrace_cancel_bpt(current); |
| 498 | if (r0) { |
| 499 | switch (regs->r0) { |
| 500 | case ERESTARTNOHAND: |
| 501 | case ERESTARTSYS: |
| 502 | case ERESTARTNOINTR: |
| 503 | /* Reset v0 and a3 and replay syscall. */ |
| 504 | regs->r0 = r0; |
| 505 | regs->r19 = r19; |
| 506 | regs->pc -= 4; |
| 507 | break; |
| 508 | case ERESTART_RESTARTBLOCK: |
| 509 | /* Set v0 to the restart_syscall and replay */ |
| 510 | regs->r0 = __NR_restart_syscall; |
| 511 | regs->pc -= 4; |
| 512 | break; |
| 513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
Al Viro | cfd60c0 | 2012-11-07 17:38:51 -0500 | [diff] [blame] | 515 | restore_saved_sigmask(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | if (single_stepping) |
| 518 | ptrace_set_bpt(current); /* re-set breakpoint */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void |
Al Viro | cb45076 | 2012-10-10 23:50:59 -0400 | [diff] [blame] | 522 | do_work_pending(struct pt_regs *regs, unsigned long thread_flags, |
Richard Henderson | b927b3e | 2007-05-29 16:03:28 -0700 | [diff] [blame] | 523 | unsigned long r0, unsigned long r19) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
Al Viro | 6972d6f | 2012-09-05 18:30:34 -0400 | [diff] [blame] | 525 | do { |
| 526 | if (thread_flags & _TIF_NEED_RESCHED) { |
| 527 | schedule(); |
| 528 | } else { |
| 529 | local_irq_enable(); |
| 530 | if (thread_flags & _TIF_SIGPENDING) { |
Al Viro | d9d0738 | 2012-09-05 18:53:18 -0400 | [diff] [blame] | 531 | do_signal(regs, r0, r19); |
Al Viro | 6972d6f | 2012-09-05 18:30:34 -0400 | [diff] [blame] | 532 | r0 = 0; |
| 533 | } else { |
| 534 | clear_thread_flag(TIF_NOTIFY_RESUME); |
| 535 | tracehook_notify_resume(regs); |
| 536 | } |
| 537 | } |
| 538 | local_irq_disable(); |
| 539 | thread_flags = current_thread_info()->flags; |
| 540 | } while (thread_flags & _TIF_WORK_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |