Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/sh/kernel/signal.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson |
| 7 | * |
| 8 | * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima |
| 9 | * |
| 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/sched.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/signal.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/wait.h> |
| 18 | #include <linux/ptrace.h> |
| 19 | #include <linux/unistd.h> |
| 20 | #include <linux/stddef.h> |
| 21 | #include <linux/tty.h> |
Paul Mundt | 19f9a34 | 2006-09-27 18:33:49 +0900 | [diff] [blame] | 22 | #include <linux/elf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/personality.h> |
| 24 | #include <linux/binfmts.h> |
Paul Mundt | b66d51c | 2007-06-01 17:26:13 +0900 | [diff] [blame] | 25 | #include <linux/io.h> |
Paul Mundt | ab99c73 | 2008-07-30 19:55:30 +0900 | [diff] [blame] | 26 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/ucontext.h> |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <asm/pgtable.h> |
| 30 | #include <asm/cacheflush.h> |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 31 | #include <asm/syscalls.h> |
Paul Mundt | 9bbafce | 2008-03-26 19:02:47 +0900 | [diff] [blame] | 32 | #include <asm/fpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Paul Mundt | 3bc24a1 | 2008-05-19 13:40:12 +0900 | [diff] [blame] | 34 | struct fdpic_func_descriptor { |
| 35 | unsigned long text; |
| 36 | unsigned long GOT; |
| 37 | }; |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* |
Carl Shaw | 2fc742f | 2009-08-24 15:07:08 +0900 | [diff] [blame] | 40 | * The following define adds a 64 byte gap between the signal |
| 41 | * stack frame and previous contents of the stack. This allows |
| 42 | * frame unwinding in a function epilogue but only if a frame |
| 43 | * pointer is used in the function. This is necessary because |
| 44 | * current gcc compilers (<4.3) do not generate unwind info on |
| 45 | * SH for function epilogues. |
| 46 | */ |
| 47 | #define UNWINDGUARD 64 |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* |
| 50 | * Do a signal return; undo the signal stack. |
| 51 | */ |
| 52 | |
| 53 | #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */ |
Yoshinori Sato | 11cbb70 | 2006-12-07 18:07:27 +0900 | [diff] [blame] | 54 | #if defined(CONFIG_CPU_SH2) |
Yoshinori Sato | 9d4436a | 2006-11-05 15:40:13 +0900 | [diff] [blame] | 55 | #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */ |
| 56 | #else |
| 57 | #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */ |
| 58 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */ |
| 60 | |
| 61 | struct sigframe |
| 62 | { |
| 63 | struct sigcontext sc; |
| 64 | unsigned long extramask[_NSIG_WORDS-1]; |
| 65 | u16 retcode[8]; |
| 66 | }; |
| 67 | |
| 68 | struct rt_sigframe |
| 69 | { |
| 70 | struct siginfo info; |
| 71 | struct ucontext uc; |
| 72 | u16 retcode[8]; |
| 73 | }; |
| 74 | |
| 75 | #ifdef CONFIG_SH_FPU |
| 76 | static inline int restore_sigcontext_fpu(struct sigcontext __user *sc) |
| 77 | { |
| 78 | struct task_struct *tsk = current; |
| 79 | |
Paul Mundt | eaa4770 | 2009-10-14 14:12:41 +0900 | [diff] [blame] | 80 | if (!(boot_cpu_data.flags & CPU_HAS_FPU)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | return 0; |
| 82 | |
| 83 | set_used_math(); |
Paul Mundt | 0ea820c | 2010-01-13 12:51:40 +0900 | [diff] [blame] | 84 | return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | sizeof(long)*(16*2+2)); |
| 86 | } |
| 87 | |
| 88 | static inline int save_sigcontext_fpu(struct sigcontext __user *sc, |
| 89 | struct pt_regs *regs) |
| 90 | { |
| 91 | struct task_struct *tsk = current; |
| 92 | |
Paul Mundt | eaa4770 | 2009-10-14 14:12:41 +0900 | [diff] [blame] | 93 | if (!(boot_cpu_data.flags & CPU_HAS_FPU)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return 0; |
| 95 | |
Al Viro | a46808e | 2012-04-22 16:59:56 -0400 | [diff] [blame] | 96 | if (!used_math()) |
| 97 | return __put_user(0, &sc->sc_ownedfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Al Viro | a46808e | 2012-04-22 16:59:56 -0400 | [diff] [blame] | 99 | if (__put_user(1, &sc->sc_ownedfp)) |
| 100 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | /* This will cause a "finit" to be triggered by the next |
| 103 | attempted FPU operation by the 'current' process. |
| 104 | */ |
| 105 | clear_used_math(); |
| 106 | |
| 107 | unlazy_fpu(tsk, regs); |
Paul Mundt | 0ea820c | 2010-01-13 12:51:40 +0900 | [diff] [blame] | 108 | return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | sizeof(long)*(16*2+2)); |
| 110 | } |
| 111 | #endif /* CONFIG_SH_FPU */ |
| 112 | |
| 113 | static int |
| 114 | restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p) |
| 115 | { |
| 116 | unsigned int err = 0; |
| 117 | |
| 118 | #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x) |
| 119 | COPY(regs[1]); |
| 120 | COPY(regs[2]); COPY(regs[3]); |
| 121 | COPY(regs[4]); COPY(regs[5]); |
| 122 | COPY(regs[6]); COPY(regs[7]); |
| 123 | COPY(regs[8]); COPY(regs[9]); |
| 124 | COPY(regs[10]); COPY(regs[11]); |
| 125 | COPY(regs[12]); COPY(regs[13]); |
| 126 | COPY(regs[14]); COPY(regs[15]); |
| 127 | COPY(gbr); COPY(mach); |
| 128 | COPY(macl); COPY(pr); |
| 129 | COPY(sr); COPY(pc); |
| 130 | #undef COPY |
| 131 | |
| 132 | #ifdef CONFIG_SH_FPU |
Paul Mundt | eaa4770 | 2009-10-14 14:12:41 +0900 | [diff] [blame] | 133 | if (boot_cpu_data.flags & CPU_HAS_FPU) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | int owned_fp; |
| 135 | struct task_struct *tsk = current; |
| 136 | |
| 137 | regs->sr |= SR_FD; /* Release FPU */ |
| 138 | clear_fpu(tsk, regs); |
| 139 | clear_used_math(); |
Al Viro | a46808e | 2012-04-22 16:59:56 -0400 | [diff] [blame] | 140 | err |= __get_user (owned_fp, &sc->sc_ownedfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | if (owned_fp) |
| 142 | err |= restore_sigcontext_fpu(sc); |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | regs->tra = -1; /* disable syscall checks */ |
| 147 | err |= __get_user(*r0_p, &sc->sc_regs[0]); |
| 148 | return err; |
| 149 | } |
| 150 | |
Bobby Bingham | abafe5d | 2014-04-03 14:46:39 -0700 | [diff] [blame] | 151 | asmlinkage int sys_sigreturn(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
Bobby Bingham | abafe5d | 2014-04-03 14:46:39 -0700 | [diff] [blame] | 153 | struct pt_regs *regs = current_pt_regs(); |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 154 | struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | sigset_t set; |
| 156 | int r0; |
| 157 | |
Paul Mundt | 1bec157 | 2008-09-24 14:37:35 +0900 | [diff] [blame] | 158 | /* Always make any pending restarted system calls return -EINTR */ |
| 159 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 162 | goto badframe; |
| 163 | |
| 164 | if (__get_user(set.sig[0], &frame->sc.oldmask) |
| 165 | || (_NSIG_WORDS > 1 |
| 166 | && __copy_from_user(&set.sig[1], &frame->extramask, |
| 167 | sizeof(frame->extramask)))) |
| 168 | goto badframe; |
| 169 | |
Matt Fleming | 5e047fa | 2012-03-28 15:16:32 -0700 | [diff] [blame] | 170 | set_current_blocked(&set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 172 | if (restore_sigcontext(regs, &frame->sc, &r0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | goto badframe; |
| 174 | return r0; |
| 175 | |
| 176 | badframe: |
| 177 | force_sig(SIGSEGV, current); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Bobby Bingham | abafe5d | 2014-04-03 14:46:39 -0700 | [diff] [blame] | 181 | asmlinkage int sys_rt_sigreturn(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Bobby Bingham | abafe5d | 2014-04-03 14:46:39 -0700 | [diff] [blame] | 183 | struct pt_regs *regs = current_pt_regs(); |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 184 | struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | sigset_t set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | int r0; |
| 187 | |
Paul Mundt | 1bec157 | 2008-09-24 14:37:35 +0900 | [diff] [blame] | 188 | /* Always make any pending restarted system calls return -EINTR */ |
| 189 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 192 | goto badframe; |
| 193 | |
| 194 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
| 195 | goto badframe; |
| 196 | |
Matt Fleming | 5e047fa | 2012-03-28 15:16:32 -0700 | [diff] [blame] | 197 | set_current_blocked(&set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 199 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | goto badframe; |
| 201 | |
Al Viro | 7a879a9 | 2012-12-23 03:46:05 -0500 | [diff] [blame] | 202 | if (restore_altstack(&frame->uc.uc_stack)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | goto badframe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | return r0; |
| 206 | |
| 207 | badframe: |
| 208 | force_sig(SIGSEGV, current); |
| 209 | return 0; |
Paul Mundt | 3aeb884 | 2007-06-19 12:33:21 +0900 | [diff] [blame] | 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * Set up a signal frame. |
| 214 | */ |
| 215 | |
| 216 | static int |
| 217 | setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, |
| 218 | unsigned long mask) |
| 219 | { |
| 220 | int err = 0; |
| 221 | |
| 222 | #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x) |
| 223 | COPY(regs[0]); COPY(regs[1]); |
| 224 | COPY(regs[2]); COPY(regs[3]); |
| 225 | COPY(regs[4]); COPY(regs[5]); |
| 226 | COPY(regs[6]); COPY(regs[7]); |
| 227 | COPY(regs[8]); COPY(regs[9]); |
| 228 | COPY(regs[10]); COPY(regs[11]); |
| 229 | COPY(regs[12]); COPY(regs[13]); |
| 230 | COPY(regs[14]); COPY(regs[15]); |
| 231 | COPY(gbr); COPY(mach); |
| 232 | COPY(macl); COPY(pr); |
| 233 | COPY(sr); COPY(pc); |
| 234 | #undef COPY |
| 235 | |
| 236 | #ifdef CONFIG_SH_FPU |
| 237 | err |= save_sigcontext_fpu(sc, regs); |
| 238 | #endif |
| 239 | |
| 240 | /* non-iBCS2 extensions.. */ |
| 241 | err |= __put_user(mask, &sc->oldmask); |
| 242 | |
| 243 | return err; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Determine which stack to use.. |
| 248 | */ |
| 249 | static inline void __user * |
| 250 | get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size) |
| 251 | { |
| 252 | if (ka->sa.sa_flags & SA_ONSTACK) { |
| 253 | if (sas_ss_flags(sp) == 0) |
| 254 | sp = current->sas_ss_sp + current->sas_ss_size; |
| 255 | } |
| 256 | |
Carl Shaw | 2fc742f | 2009-08-24 15:07:08 +0900 | [diff] [blame] | 257 | return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Paul Mundt | 19f9a34 | 2006-09-27 18:33:49 +0900 | [diff] [blame] | 260 | /* These symbols are defined with the addresses in the vsyscall page. |
| 261 | See vsyscall-trapa.S. */ |
Matt Fleming | 9445571 | 2009-06-18 10:03:33 +0100 | [diff] [blame] | 262 | extern void __kernel_sigreturn(void); |
| 263 | extern void __kernel_rt_sigreturn(void); |
Paul Mundt | 19f9a34 | 2006-09-27 18:33:49 +0900 | [diff] [blame] | 264 | |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 265 | static int setup_frame(struct ksignal *ksig, sigset_t *set, |
| 266 | struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
| 268 | struct sigframe __user *frame; |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 269 | int err = 0, sig = ksig->sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | int signal; |
| 271 | |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 272 | frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 275 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | signal = current_thread_info()->exec_domain |
| 278 | && current_thread_info()->exec_domain->signal_invmap |
| 279 | && sig < 32 |
| 280 | ? current_thread_info()->exec_domain->signal_invmap[sig] |
| 281 | : sig; |
| 282 | |
| 283 | err |= setup_sigcontext(&frame->sc, regs, set->sig[0]); |
| 284 | |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 285 | if (_NSIG_WORDS > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | err |= __copy_to_user(frame->extramask, &set->sig[1], |
| 287 | sizeof(frame->extramask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | /* Set up to return from userspace. If provided, use a stub |
| 290 | already in userspace. */ |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 291 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
| 292 | regs->pr = (unsigned long) ksig->ka.sa.sa_restorer; |
Paul Mundt | 19f9a34 | 2006-09-27 18:33:49 +0900 | [diff] [blame] | 293 | #ifdef CONFIG_VSYSCALL |
| 294 | } else if (likely(current->mm->context.vdso)) { |
| 295 | regs->pr = VDSO_SYM(&__kernel_sigreturn); |
| 296 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } else { |
| 298 | /* Generate return code (system call to sigreturn) */ |
| 299 | err |= __put_user(MOVW(7), &frame->retcode[0]); |
Yoshinori Sato | 9d4436a | 2006-11-05 15:40:13 +0900 | [diff] [blame] | 300 | err |= __put_user(TRAP_NOARG, &frame->retcode[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | err |= __put_user(OR_R0_R0, &frame->retcode[2]); |
| 302 | err |= __put_user(OR_R0_R0, &frame->retcode[3]); |
| 303 | err |= __put_user(OR_R0_R0, &frame->retcode[4]); |
| 304 | err |= __put_user(OR_R0_R0, &frame->retcode[5]); |
| 305 | err |= __put_user(OR_R0_R0, &frame->retcode[6]); |
| 306 | err |= __put_user((__NR_sigreturn), &frame->retcode[7]); |
| 307 | regs->pr = (unsigned long) frame->retcode; |
Stuart Menefy | f2fb4e4 | 2008-07-02 17:51:23 +0900 | [diff] [blame] | 308 | flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if (err) |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 312 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | /* Set up registers for signal handler */ |
| 315 | regs->regs[15] = (unsigned long) frame; |
| 316 | regs->regs[4] = signal; /* Arg for signal handler */ |
| 317 | regs->regs[5] = 0; |
| 318 | regs->regs[6] = (unsigned long) &frame->sc; |
Paul Mundt | 3bc24a1 | 2008-05-19 13:40:12 +0900 | [diff] [blame] | 319 | |
| 320 | if (current->personality & FDPIC_FUNCPTRS) { |
| 321 | struct fdpic_func_descriptor __user *funcptr = |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 322 | (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler; |
Paul Mundt | 3bc24a1 | 2008-05-19 13:40:12 +0900 | [diff] [blame] | 323 | |
Al Viro | a46808e | 2012-04-22 16:59:56 -0400 | [diff] [blame] | 324 | err |= __get_user(regs->pc, &funcptr->text); |
| 325 | err |= __get_user(regs->regs[12], &funcptr->GOT); |
Paul Mundt | 3bc24a1 | 2008-05-19 13:40:12 +0900 | [diff] [blame] | 326 | } else |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 327 | regs->pc = (unsigned long)ksig->ka.sa.sa_handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Al Viro | a46808e | 2012-04-22 16:59:56 -0400 | [diff] [blame] | 329 | if (err) |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 330 | return -EFAULT; |
Al Viro | a46808e | 2012-04-22 16:59:56 -0400 | [diff] [blame] | 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | set_fs(USER_DS); |
| 333 | |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 334 | pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n", |
Alexey Dobriyan | 19c5870 | 2007-10-18 23:40:41 -0700 | [diff] [blame] | 335 | current->comm, task_pid_nr(current), frame, regs->pc, regs->pr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 337 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 340 | static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, |
| 341 | struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | { |
| 343 | struct rt_sigframe __user *frame; |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 344 | int err = 0, sig = ksig->sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | int signal; |
| 346 | |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 347 | frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 350 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | signal = current_thread_info()->exec_domain |
| 353 | && current_thread_info()->exec_domain->signal_invmap |
| 354 | && sig < 32 |
| 355 | ? current_thread_info()->exec_domain->signal_invmap[sig] |
| 356 | : sig; |
| 357 | |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 358 | err |= copy_siginfo_to_user(&frame->info, &ksig->info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | /* Create the ucontext. */ |
| 361 | err |= __put_user(0, &frame->uc.uc_flags); |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 362 | err |= __put_user(NULL, &frame->uc.uc_link); |
Al Viro | 7a879a9 | 2012-12-23 03:46:05 -0500 | [diff] [blame] | 363 | err |= __save_altstack(&frame->uc.uc_stack, regs->regs[15]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | err |= setup_sigcontext(&frame->uc.uc_mcontext, |
| 365 | regs, set->sig[0]); |
| 366 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 367 | |
| 368 | /* Set up to return from userspace. If provided, use a stub |
| 369 | already in userspace. */ |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 370 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
| 371 | regs->pr = (unsigned long) ksig->ka.sa.sa_restorer; |
Paul Mundt | 19f9a34 | 2006-09-27 18:33:49 +0900 | [diff] [blame] | 372 | #ifdef CONFIG_VSYSCALL |
| 373 | } else if (likely(current->mm->context.vdso)) { |
| 374 | regs->pr = VDSO_SYM(&__kernel_rt_sigreturn); |
| 375 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } else { |
| 377 | /* Generate return code (system call to rt_sigreturn) */ |
| 378 | err |= __put_user(MOVW(7), &frame->retcode[0]); |
Yoshinori Sato | 9d4436a | 2006-11-05 15:40:13 +0900 | [diff] [blame] | 379 | err |= __put_user(TRAP_NOARG, &frame->retcode[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | err |= __put_user(OR_R0_R0, &frame->retcode[2]); |
| 381 | err |= __put_user(OR_R0_R0, &frame->retcode[3]); |
| 382 | err |= __put_user(OR_R0_R0, &frame->retcode[4]); |
| 383 | err |= __put_user(OR_R0_R0, &frame->retcode[5]); |
| 384 | err |= __put_user(OR_R0_R0, &frame->retcode[6]); |
| 385 | err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]); |
| 386 | regs->pr = (unsigned long) frame->retcode; |
Paul Mundt | 4d2947f | 2009-10-14 14:21:40 +0900 | [diff] [blame] | 387 | flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | if (err) |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 391 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | /* Set up registers for signal handler */ |
| 394 | regs->regs[15] = (unsigned long) frame; |
| 395 | regs->regs[4] = signal; /* Arg for signal handler */ |
| 396 | regs->regs[5] = (unsigned long) &frame->info; |
| 397 | regs->regs[6] = (unsigned long) &frame->uc; |
Paul Mundt | 3bc24a1 | 2008-05-19 13:40:12 +0900 | [diff] [blame] | 398 | |
| 399 | if (current->personality & FDPIC_FUNCPTRS) { |
| 400 | struct fdpic_func_descriptor __user *funcptr = |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 401 | (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler; |
Paul Mundt | 3bc24a1 | 2008-05-19 13:40:12 +0900 | [diff] [blame] | 402 | |
Al Viro | a46808e | 2012-04-22 16:59:56 -0400 | [diff] [blame] | 403 | err |= __get_user(regs->pc, &funcptr->text); |
| 404 | err |= __get_user(regs->regs[12], &funcptr->GOT); |
Paul Mundt | 3bc24a1 | 2008-05-19 13:40:12 +0900 | [diff] [blame] | 405 | } else |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 406 | regs->pc = (unsigned long)ksig->ka.sa.sa_handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Al Viro | a46808e | 2012-04-22 16:59:56 -0400 | [diff] [blame] | 408 | if (err) |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 409 | return -EFAULT; |
Al Viro | a46808e | 2012-04-22 16:59:56 -0400 | [diff] [blame] | 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | set_fs(USER_DS); |
| 412 | |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 413 | pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n", |
Alexey Dobriyan | 19c5870 | 2007-10-18 23:40:41 -0700 | [diff] [blame] | 414 | current->comm, task_pid_nr(current), frame, regs->pc, regs->pr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 416 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Paul Mundt | f8b890a | 2008-09-12 22:08:20 +0900 | [diff] [blame] | 419 | static inline void |
| 420 | handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs, |
| 421 | struct sigaction *sa) |
| 422 | { |
| 423 | /* If we're not from a syscall, bail out */ |
| 424 | if (regs->tra < 0) |
| 425 | return; |
| 426 | |
| 427 | /* check for system call restart.. */ |
| 428 | switch (regs->regs[0]) { |
| 429 | case -ERESTART_RESTARTBLOCK: |
| 430 | case -ERESTARTNOHAND: |
| 431 | no_system_call_restart: |
| 432 | regs->regs[0] = -EINTR; |
Paul Mundt | f8b890a | 2008-09-12 22:08:20 +0900 | [diff] [blame] | 433 | break; |
| 434 | |
| 435 | case -ERESTARTSYS: |
| 436 | if (!(sa->sa_flags & SA_RESTART)) |
| 437 | goto no_system_call_restart; |
| 438 | /* fallthrough */ |
| 439 | case -ERESTARTNOINTR: |
| 440 | regs->regs[0] = save_r0; |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 441 | regs->pc -= instruction_size(__raw_readw(regs->pc - 4)); |
Paul Mundt | f8b890a | 2008-09-12 22:08:20 +0900 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | } |
| 445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | /* |
| 447 | * OK, we're invoking a handler |
| 448 | */ |
Al Viro | a610d6e | 2012-05-21 23:42:15 -0400 | [diff] [blame] | 449 | static void |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 450 | handle_signal(struct ksignal *ksig, struct pt_regs *regs, unsigned int save_r0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Al Viro | b7f9a11 | 2012-05-02 09:59:21 -0400 | [diff] [blame] | 452 | sigset_t *oldset = sigmask_to_save(); |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 453 | int ret; |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* Set up the stack frame */ |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 456 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) |
| 457 | ret = setup_rt_frame(ksig, oldset, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | else |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 459 | ret = setup_frame(ksig, oldset, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 461 | signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Note that 'init' is a special process: it doesn't get signals it doesn't |
| 466 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 467 | * mistake. |
| 468 | * |
| 469 | * Note that we go through the signals twice: once to check the signals that |
| 470 | * the kernel can handle, and then we build all the user-level signal handling |
| 471 | * stack-frames in one go after that. |
| 472 | */ |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 473 | static void do_signal(struct pt_regs *regs, unsigned int save_r0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 475 | struct ksignal ksig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * We want the common case to go fast, which |
| 479 | * is why we may in certain cases get here from |
| 480 | * kernel mode. Just return without doing anything |
| 481 | * if so. |
| 482 | */ |
| 483 | if (!user_mode(regs)) |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 484 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 486 | if (get_signal(&ksig)) { |
| 487 | handle_syscall_restart(save_r0, regs, &ksig.ka.sa); |
Paul Mundt | f8b890a | 2008-09-12 22:08:20 +0900 | [diff] [blame] | 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | /* Whee! Actually deliver the signal. */ |
Richard Weinberger | b46e848 | 2013-10-07 14:51:14 +0200 | [diff] [blame^] | 490 | handle_signal(&ksig, regs, save_r0); |
Ryusuke Sakato | c8bfa1f | 2007-02-23 13:22:56 +0900 | [diff] [blame] | 491 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | /* Did we come from a system call? */ |
| 495 | if (regs->tra >= 0) { |
| 496 | /* Restart the system call - no handlers present */ |
| 497 | if (regs->regs[0] == -ERESTARTNOHAND || |
| 498 | regs->regs[0] == -ERESTARTSYS || |
Paul Mundt | 0b89293 | 2006-09-27 17:22:49 +0900 | [diff] [blame] | 499 | regs->regs[0] == -ERESTARTNOINTR) { |
Ryusuke Sakato | c8bfa1f | 2007-02-23 13:22:56 +0900 | [diff] [blame] | 500 | regs->regs[0] = save_r0; |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 501 | regs->pc -= instruction_size(__raw_readw(regs->pc - 4)); |
Paul Mundt | 0b89293 | 2006-09-27 17:22:49 +0900 | [diff] [blame] | 502 | } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) { |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 503 | regs->pc -= instruction_size(__raw_readw(regs->pc - 4)); |
Paul Mundt | 0b89293 | 2006-09-27 17:22:49 +0900 | [diff] [blame] | 504 | regs->regs[3] = __NR_restart_syscall; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 507 | |
Paul Mundt | 56bfc42 | 2009-10-14 16:05:42 +0900 | [diff] [blame] | 508 | /* |
| 509 | * If there's no signal to deliver, we just put the saved sigmask |
| 510 | * back. |
| 511 | */ |
Al Viro | 51a7b44 | 2012-05-21 23:33:55 -0400 | [diff] [blame] | 512 | restore_saved_sigmask(); |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0, |
Paul Mundt | ab99c73 | 2008-07-30 19:55:30 +0900 | [diff] [blame] | 516 | unsigned long thread_info_flags) |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 517 | { |
| 518 | /* deal with pending signal delivery */ |
Paul Mundt | ab99c73 | 2008-07-30 19:55:30 +0900 | [diff] [blame] | 519 | if (thread_info_flags & _TIF_SIGPENDING) |
Paul Mundt | 9f23e7e | 2006-09-27 17:27:00 +0900 | [diff] [blame] | 520 | do_signal(regs, save_r0); |
Paul Mundt | ab99c73 | 2008-07-30 19:55:30 +0900 | [diff] [blame] | 521 | |
| 522 | if (thread_info_flags & _TIF_NOTIFY_RESUME) { |
| 523 | clear_thread_flag(TIF_NOTIFY_RESUME); |
| 524 | tracehook_notify_resume(regs); |
| 525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |