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