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