David S. Miller | 5526b7e | 2008-04-27 02:26:36 -0700 | [diff] [blame] | 1 | /* linux/arch/sparc/kernel/signal.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 4 | * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) |
| 5 | * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx) |
| 6 | * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be) |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/sched.h> |
| 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/tty.h> |
| 18 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/binfmts.h> /* do_coredum */ |
| 20 | #include <linux/bitops.h> |
David S. Miller | 5a157d5 | 2008-07-27 03:38:53 -0700 | [diff] [blame] | 21 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/uaccess.h> |
| 24 | #include <asm/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/pgalloc.h> |
| 26 | #include <asm/pgtable.h> |
| 27 | #include <asm/cacheflush.h> /* flush_sig_insns */ |
David Howells | d550bbd | 2012-03-28 18:30:03 +0100 | [diff] [blame] | 28 | #include <asm/switch_to.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 30 | #include "sigutil.h" |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | extern void fpsave(unsigned long *fpregs, unsigned long *fsr, |
| 33 | void *fpqueue, unsigned long *fpqdepth); |
| 34 | extern void fpload(unsigned long *fpregs, unsigned long *fsr); |
| 35 | |
David S. Miller | 5526b7e | 2008-04-27 02:26:36 -0700 | [diff] [blame] | 36 | struct signal_frame { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | struct sparc_stackf ss; |
Sam Ravnborg | 4d7b92a | 2009-01-02 19:32:59 -0800 | [diff] [blame] | 38 | __siginfo32_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | __siginfo_fpu_t __user *fpu_save; |
| 40 | unsigned long insns[2] __attribute__ ((aligned (8))); |
| 41 | unsigned int extramask[_NSIG_WORDS - 1]; |
| 42 | unsigned int extra_size; /* Should be 0 */ |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 43 | __siginfo_rwin_t __user *rwin_save; |
| 44 | } __attribute__((aligned(8))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | struct rt_signal_frame { |
| 47 | struct sparc_stackf ss; |
| 48 | siginfo_t info; |
| 49 | struct pt_regs regs; |
| 50 | sigset_t mask; |
| 51 | __siginfo_fpu_t __user *fpu_save; |
| 52 | unsigned int insns[2]; |
| 53 | stack_t stack; |
| 54 | unsigned int extra_size; /* Should be 0 */ |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 55 | __siginfo_rwin_t __user *rwin_save; |
| 56 | } __attribute__((aligned(8))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* Align macros */ |
David S. Miller | 5526b7e | 2008-04-27 02:26:36 -0700 | [diff] [blame] | 59 | #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7))) |
| 61 | |
David S. Miller | 2d7d5f0 | 2006-01-19 02:42:49 -0800 | [diff] [blame] | 62 | static int _sigpause_common(old_sigset_t set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Matt Fleming | faddf59 | 2011-08-11 14:57:02 +0100 | [diff] [blame] | 64 | sigset_t blocked; |
Matt Fleming | faddf59 | 2011-08-11 14:57:02 +0100 | [diff] [blame] | 65 | siginitset(&blocked, set); |
Al Viro | 68f3f16 | 2012-05-21 21:42:32 -0400 | [diff] [blame] | 66 | return sigsuspend(&blocked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
David S. Miller | 2d7d5f0 | 2006-01-19 02:42:49 -0800 | [diff] [blame] | 69 | asmlinkage int sys_sigsuspend(old_sigset_t set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
David S. Miller | 2d7d5f0 | 2006-01-19 02:42:49 -0800 | [diff] [blame] | 71 | return _sigpause_common(set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
David S. Miller | 5526b7e | 2008-04-27 02:26:36 -0700 | [diff] [blame] | 74 | asmlinkage void do_sigreturn(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
David S. Miller | 5526b7e | 2008-04-27 02:26:36 -0700 | [diff] [blame] | 76 | struct signal_frame __user *sf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | unsigned long up_psr, pc, npc; |
| 78 | sigset_t set; |
| 79 | __siginfo_fpu_t __user *fpu_save; |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 80 | __siginfo_rwin_t __user *rwin_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | int err; |
| 82 | |
David S. Miller | 5526b7e | 2008-04-27 02:26:36 -0700 | [diff] [blame] | 83 | /* Always make any pending restarted system calls return -EINTR */ |
| 84 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 85 | |
| 86 | synchronize_user_stack(); |
| 87 | |
| 88 | sf = (struct signal_frame __user *) regs->u_regs[UREG_FP]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* 1. Make sure we are not getting garbage from the user */ |
| 91 | if (!access_ok(VERIFY_READ, sf, sizeof(*sf))) |
| 92 | goto segv_and_exit; |
| 93 | |
| 94 | if (((unsigned long) sf) & 3) |
| 95 | goto segv_and_exit; |
| 96 | |
| 97 | err = __get_user(pc, &sf->info.si_regs.pc); |
| 98 | err |= __get_user(npc, &sf->info.si_regs.npc); |
| 99 | |
| 100 | if ((pc | npc) & 3) |
| 101 | goto segv_and_exit; |
| 102 | |
| 103 | /* 2. Restore the state */ |
| 104 | up_psr = regs->psr; |
| 105 | err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs)); |
| 106 | |
| 107 | /* User can only change condition codes and FPU enabling in %psr. */ |
| 108 | regs->psr = (up_psr & ~(PSR_ICC | PSR_EF)) |
| 109 | | (regs->psr & (PSR_ICC | PSR_EF)); |
| 110 | |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 111 | /* Prevent syscall restart. */ |
| 112 | pt_regs_clear_syscall(regs); |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | err |= __get_user(fpu_save, &sf->fpu_save); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (fpu_save) |
| 116 | err |= restore_fpu_state(regs, fpu_save); |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 117 | err |= __get_user(rwin_save, &sf->rwin_save); |
| 118 | if (rwin_save) |
| 119 | err |= restore_rwin_state(rwin_save); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | /* This is pretty much atomic, no amount locking would prevent |
| 122 | * the races which exist anyways. |
| 123 | */ |
| 124 | err |= __get_user(set.sig[0], &sf->info.si_mask); |
| 125 | err |= __copy_from_user(&set.sig[1], &sf->extramask, |
| 126 | (_NSIG_WORDS-1) * sizeof(unsigned int)); |
| 127 | |
| 128 | if (err) |
| 129 | goto segv_and_exit; |
| 130 | |
Matt Fleming | faddf59 | 2011-08-11 14:57:02 +0100 | [diff] [blame] | 131 | set_current_blocked(&set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | return; |
| 133 | |
| 134 | segv_and_exit: |
| 135 | force_sig(SIGSEGV, current); |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | asmlinkage void do_rt_sigreturn(struct pt_regs *regs) |
| 139 | { |
| 140 | struct rt_signal_frame __user *sf; |
| 141 | unsigned int psr, pc, npc; |
| 142 | __siginfo_fpu_t __user *fpu_save; |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 143 | __siginfo_rwin_t __user *rwin_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | sigset_t set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | int err; |
| 146 | |
| 147 | synchronize_user_stack(); |
| 148 | sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP]; |
| 149 | if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) || |
| 150 | (((unsigned long) sf) & 0x03)) |
| 151 | goto segv; |
| 152 | |
| 153 | err = __get_user(pc, &sf->regs.pc); |
| 154 | err |= __get_user(npc, &sf->regs.npc); |
| 155 | err |= ((pc | npc) & 0x03); |
| 156 | |
| 157 | err |= __get_user(regs->y, &sf->regs.y); |
| 158 | err |= __get_user(psr, &sf->regs.psr); |
| 159 | |
| 160 | err |= __copy_from_user(®s->u_regs[UREG_G1], |
| 161 | &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32)); |
| 162 | |
| 163 | regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC); |
| 164 | |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 165 | /* Prevent syscall restart. */ |
| 166 | pt_regs_clear_syscall(regs); |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | err |= __get_user(fpu_save, &sf->fpu_save); |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 169 | if (!err && fpu_save) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | err |= restore_fpu_state(regs, fpu_save); |
| 171 | err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t)); |
Al Viro | 99b06fe | 2012-12-23 03:41:17 -0500 | [diff] [blame^] | 172 | err |= restore_altstack(&sf->stack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | if (err) |
| 175 | goto segv; |
| 176 | |
| 177 | regs->pc = pc; |
| 178 | regs->npc = npc; |
| 179 | |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 180 | err |= __get_user(rwin_save, &sf->rwin_save); |
| 181 | if (!err && rwin_save) { |
| 182 | if (restore_rwin_state(rwin_save)) |
| 183 | goto segv; |
| 184 | } |
| 185 | |
Matt Fleming | faddf59 | 2011-08-11 14:57:02 +0100 | [diff] [blame] | 186 | set_current_blocked(&set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return; |
| 188 | segv: |
| 189 | force_sig(SIGSEGV, current); |
| 190 | } |
| 191 | |
| 192 | /* Checks if the fp is valid */ |
| 193 | static inline int invalid_frame_pointer(void __user *fp, int fplen) |
| 194 | { |
Sam Ravnborg | 3c23a76 | 2012-05-11 11:35:14 +0000 | [diff] [blame] | 195 | if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | return 1; |
Sam Ravnborg | 3c23a76 | 2012-05-11 11:35:14 +0000 | [diff] [blame] | 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize) |
| 202 | { |
David S. Miller | dc5dc7e | 2008-05-07 18:54:05 -0700 | [diff] [blame] | 203 | unsigned long sp = regs->u_regs[UREG_FP]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
David S. Miller | dc5dc7e | 2008-05-07 18:54:05 -0700 | [diff] [blame] | 205 | /* |
| 206 | * If we are on the alternate signal stack and would overflow it, don't. |
| 207 | * Return an always-bogus address instead so we will die with SIGSEGV. |
| 208 | */ |
| 209 | if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) |
| 210 | return (void __user *) -1L; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | /* This is the X/Open sanctioned signal stack switching. */ |
| 213 | if (sa->sa_flags & SA_ONSTACK) { |
David S. Miller | dc5dc7e | 2008-05-07 18:54:05 -0700 | [diff] [blame] | 214 | if (sas_ss_flags(sp) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | sp = current->sas_ss_sp + current->sas_ss_size; |
| 216 | } |
David S. Miller | dc5dc7e | 2008-05-07 18:54:05 -0700 | [diff] [blame] | 217 | |
David S. Miller | f036d9f | 2010-02-09 16:18:40 -0800 | [diff] [blame] | 218 | sp -= framesize; |
| 219 | |
David S. Miller | dc5dc7e | 2008-05-07 18:54:05 -0700 | [diff] [blame] | 220 | /* Always align the stack frame. This handles two cases. First, |
| 221 | * sigaltstack need not be mindful of platform specific stack |
| 222 | * alignment. Second, if we took this signal because the stack |
| 223 | * is not aligned properly, we'd like to take the signal cleanly |
| 224 | * and report that. |
| 225 | */ |
David S. Miller | f036d9f | 2010-02-09 16:18:40 -0800 | [diff] [blame] | 226 | sp &= ~15UL; |
David S. Miller | dc5dc7e | 2008-05-07 18:54:05 -0700 | [diff] [blame] | 227 | |
David S. Miller | f036d9f | 2010-02-09 16:18:40 -0800 | [diff] [blame] | 228 | return (void __user *) sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 231 | static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs, |
| 232 | int signo, sigset_t *oldset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
David S. Miller | 5526b7e | 2008-04-27 02:26:36 -0700 | [diff] [blame] | 234 | struct signal_frame __user *sf; |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 235 | int sigframe_size, err, wsaved; |
| 236 | void __user *tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
| 238 | /* 1. Make sure everything is clean */ |
| 239 | synchronize_user_stack(); |
| 240 | |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 241 | wsaved = current_thread_info()->w_saved; |
| 242 | |
| 243 | sigframe_size = sizeof(*sf); |
| 244 | if (used_math()) |
| 245 | sigframe_size += sizeof(__siginfo_fpu_t); |
| 246 | if (wsaved) |
| 247 | sigframe_size += sizeof(__siginfo_rwin_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
David S. Miller | 5526b7e | 2008-04-27 02:26:36 -0700 | [diff] [blame] | 249 | sf = (struct signal_frame __user *) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | get_sigframe(&ka->sa, regs, sigframe_size); |
| 251 | |
| 252 | if (invalid_frame_pointer(sf, sigframe_size)) |
| 253 | goto sigill_and_return; |
| 254 | |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 255 | tail = sf + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | /* 2. Save the current process state */ |
| 258 | err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs)); |
| 259 | |
| 260 | err |= __put_user(0, &sf->extra_size); |
| 261 | |
| 262 | if (used_math()) { |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 263 | __siginfo_fpu_t __user *fp = tail; |
| 264 | tail += sizeof(*fp); |
| 265 | err |= save_fpu_state(regs, fp); |
| 266 | err |= __put_user(fp, &sf->fpu_save); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } else { |
| 268 | err |= __put_user(0, &sf->fpu_save); |
| 269 | } |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 270 | if (wsaved) { |
| 271 | __siginfo_rwin_t __user *rwp = tail; |
| 272 | tail += sizeof(*rwp); |
| 273 | err |= save_rwin_state(wsaved, rwp); |
| 274 | err |= __put_user(rwp, &sf->rwin_save); |
| 275 | } else { |
| 276 | err |= __put_user(0, &sf->rwin_save); |
| 277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | err |= __put_user(oldset->sig[0], &sf->info.si_mask); |
| 280 | err |= __copy_to_user(sf->extramask, &oldset->sig[1], |
| 281 | (_NSIG_WORDS - 1) * sizeof(unsigned int)); |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 282 | if (!wsaved) { |
| 283 | err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP], |
| 284 | sizeof(struct reg_window32)); |
| 285 | } else { |
| 286 | struct reg_window32 *rp; |
| 287 | |
| 288 | rp = ¤t_thread_info()->reg_window[wsaved - 1]; |
| 289 | err |= __copy_to_user(sf, rp, sizeof(struct reg_window32)); |
| 290 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | if (err) |
| 292 | goto sigsegv; |
| 293 | |
| 294 | /* 3. signal handler back-trampoline and parameters */ |
| 295 | regs->u_regs[UREG_FP] = (unsigned long) sf; |
| 296 | regs->u_regs[UREG_I0] = signo; |
| 297 | regs->u_regs[UREG_I1] = (unsigned long) &sf->info; |
| 298 | regs->u_regs[UREG_I2] = (unsigned long) &sf->info; |
| 299 | |
| 300 | /* 4. signal handler */ |
| 301 | regs->pc = (unsigned long) ka->sa.sa_handler; |
| 302 | regs->npc = (regs->pc + 4); |
| 303 | |
| 304 | /* 5. return to kernel instructions */ |
| 305 | if (ka->ka_restorer) |
| 306 | regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer; |
| 307 | else { |
| 308 | regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2); |
| 309 | |
| 310 | /* mov __NR_sigreturn, %g1 */ |
| 311 | err |= __put_user(0x821020d8, &sf->insns[0]); |
| 312 | |
| 313 | /* t 0x10 */ |
| 314 | err |= __put_user(0x91d02010, &sf->insns[1]); |
| 315 | if (err) |
| 316 | goto sigsegv; |
| 317 | |
| 318 | /* Flush instruction space. */ |
| 319 | flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0])); |
| 320 | } |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 321 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | sigill_and_return: |
| 324 | do_exit(SIGILL); |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 325 | return -EINVAL; |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | sigsegv: |
| 328 | force_sigsegv(signo, current); |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 329 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 332 | static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs, |
| 333 | int signo, sigset_t *oldset, siginfo_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | struct rt_signal_frame __user *sf; |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 336 | int sigframe_size, wsaved; |
| 337 | void __user *tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | unsigned int psr; |
| 339 | int err; |
| 340 | |
| 341 | synchronize_user_stack(); |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 342 | wsaved = current_thread_info()->w_saved; |
| 343 | sigframe_size = sizeof(*sf); |
| 344 | if (used_math()) |
| 345 | sigframe_size += sizeof(__siginfo_fpu_t); |
| 346 | if (wsaved) |
| 347 | sigframe_size += sizeof(__siginfo_rwin_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | sf = (struct rt_signal_frame __user *) |
| 349 | get_sigframe(&ka->sa, regs, sigframe_size); |
| 350 | if (invalid_frame_pointer(sf, sigframe_size)) |
| 351 | goto sigill; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 353 | tail = sf + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | err = __put_user(regs->pc, &sf->regs.pc); |
| 355 | err |= __put_user(regs->npc, &sf->regs.npc); |
| 356 | err |= __put_user(regs->y, &sf->regs.y); |
| 357 | psr = regs->psr; |
| 358 | if (used_math()) |
| 359 | psr |= PSR_EF; |
| 360 | err |= __put_user(psr, &sf->regs.psr); |
| 361 | err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs)); |
| 362 | err |= __put_user(0, &sf->extra_size); |
| 363 | |
| 364 | if (psr & PSR_EF) { |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 365 | __siginfo_fpu_t *fp = tail; |
| 366 | tail += sizeof(*fp); |
| 367 | err |= save_fpu_state(regs, fp); |
| 368 | err |= __put_user(fp, &sf->fpu_save); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } else { |
| 370 | err |= __put_user(0, &sf->fpu_save); |
| 371 | } |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 372 | if (wsaved) { |
| 373 | __siginfo_rwin_t *rwp = tail; |
| 374 | tail += sizeof(*rwp); |
| 375 | err |= save_rwin_state(wsaved, rwp); |
| 376 | err |= __put_user(rwp, &sf->rwin_save); |
| 377 | } else { |
| 378 | err |= __put_user(0, &sf->rwin_save); |
| 379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t)); |
| 381 | |
| 382 | /* Setup sigaltstack */ |
Al Viro | 99b06fe | 2012-12-23 03:41:17 -0500 | [diff] [blame^] | 383 | err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
David S. Miller | 5598473 | 2011-08-20 17:14:54 -0700 | [diff] [blame] | 385 | if (!wsaved) { |
| 386 | err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP], |
| 387 | sizeof(struct reg_window32)); |
| 388 | } else { |
| 389 | struct reg_window32 *rp; |
| 390 | |
| 391 | rp = ¤t_thread_info()->reg_window[wsaved - 1]; |
| 392 | err |= __copy_to_user(sf, rp, sizeof(struct reg_window32)); |
| 393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
| 395 | err |= copy_siginfo_to_user(&sf->info, info); |
| 396 | |
| 397 | if (err) |
| 398 | goto sigsegv; |
| 399 | |
| 400 | regs->u_regs[UREG_FP] = (unsigned long) sf; |
| 401 | regs->u_regs[UREG_I0] = signo; |
| 402 | regs->u_regs[UREG_I1] = (unsigned long) &sf->info; |
| 403 | regs->u_regs[UREG_I2] = (unsigned long) &sf->regs; |
| 404 | |
| 405 | regs->pc = (unsigned long) ka->sa.sa_handler; |
| 406 | regs->npc = (regs->pc + 4); |
| 407 | |
| 408 | if (ka->ka_restorer) |
| 409 | regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer; |
| 410 | else { |
| 411 | regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2); |
| 412 | |
| 413 | /* mov __NR_sigreturn, %g1 */ |
| 414 | err |= __put_user(0x821020d8, &sf->insns[0]); |
| 415 | |
| 416 | /* t 0x10 */ |
| 417 | err |= __put_user(0x91d02010, &sf->insns[1]); |
| 418 | if (err) |
| 419 | goto sigsegv; |
| 420 | |
| 421 | /* Flush instruction space. */ |
| 422 | flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0])); |
| 423 | } |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 424 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | sigill: |
| 427 | do_exit(SIGILL); |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 428 | return -EINVAL; |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | sigsegv: |
| 431 | force_sigsegv(signo, current); |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 432 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Al Viro | a610d6e | 2012-05-21 23:42:15 -0400 | [diff] [blame] | 435 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | handle_signal(unsigned long signr, struct k_sigaction *ka, |
Al Viro | b7f9a11 | 2012-05-02 09:59:21 -0400 | [diff] [blame] | 437 | siginfo_t *info, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Al Viro | b7f9a11 | 2012-05-02 09:59:21 -0400 | [diff] [blame] | 439 | sigset_t *oldset = sigmask_to_save(); |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 440 | int err; |
| 441 | |
David S. Miller | ec98c6b | 2008-04-20 02:14:23 -0700 | [diff] [blame] | 442 | if (ka->sa.sa_flags & SA_SIGINFO) |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 443 | err = setup_rt_frame(ka, regs, signr, oldset, info); |
David S. Miller | ec98c6b | 2008-04-20 02:14:23 -0700 | [diff] [blame] | 444 | else |
David S. Miller | 392c218 | 2010-09-21 21:41:12 -0700 | [diff] [blame] | 445 | err = setup_frame(ka, regs, signr, oldset); |
| 446 | |
| 447 | if (err) |
Al Viro | a610d6e | 2012-05-21 23:42:15 -0400 | [diff] [blame] | 448 | return; |
David S. Miller | ec98c6b | 2008-04-20 02:14:23 -0700 | [diff] [blame] | 449 | |
Al Viro | efee984 | 2012-04-28 02:04:15 -0400 | [diff] [blame] | 450 | signal_delivered(signr, info, ka, regs, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs, |
| 454 | struct sigaction *sa) |
| 455 | { |
| 456 | switch(regs->u_regs[UREG_I0]) { |
| 457 | case ERESTART_RESTARTBLOCK: |
| 458 | case ERESTARTNOHAND: |
| 459 | no_system_call_restart: |
| 460 | regs->u_regs[UREG_I0] = EINTR; |
| 461 | regs->psr |= PSR_C; |
| 462 | break; |
| 463 | case ERESTARTSYS: |
| 464 | if (!(sa->sa_flags & SA_RESTART)) |
| 465 | goto no_system_call_restart; |
| 466 | /* fallthrough */ |
| 467 | case ERESTARTNOINTR: |
| 468 | regs->u_regs[UREG_I0] = orig_i0; |
| 469 | regs->pc -= 4; |
| 470 | regs->npc -= 4; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* Note that 'init' is a special process: it doesn't get signals it doesn't |
| 475 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 476 | * mistake. |
| 477 | */ |
David S. Miller | 5a157d5 | 2008-07-27 03:38:53 -0700 | [diff] [blame] | 478 | static void do_signal(struct pt_regs *regs, unsigned long orig_i0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | struct k_sigaction ka; |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 481 | int restart_syscall; |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 482 | siginfo_t info; |
| 483 | int signr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
David S. Miller | 1d299bc | 2011-11-14 20:32:16 -0800 | [diff] [blame] | 485 | /* It's a lot of work and synchronization to add a new ptrace |
| 486 | * register for GDB to save and restore in order to get |
| 487 | * orig_i0 correct for syscall restarts when debugging. |
| 488 | * |
David S. Miller | e88d246 | 2011-11-15 12:57:00 -0800 | [diff] [blame] | 489 | * Although it should be the case that most of the global |
| 490 | * registers are volatile across a system call, glibc already |
| 491 | * depends upon that fact that we preserve them. So we can't |
| 492 | * just use any global register to save away the orig_i0 value. |
| 493 | * |
| 494 | * In particular %g2, %g3, %g4, and %g5 are all assumed to be |
| 495 | * preserved across a system call trap by various pieces of |
| 496 | * code in glibc. |
| 497 | * |
| 498 | * %g7 is used as the "thread register". %g6 is not used in |
| 499 | * any fixed manner. %g6 is used as a scratch register and |
| 500 | * a compiler temporary, but it's value is never used across |
| 501 | * a system call. Therefore %g6 is usable for orig_i0 storage. |
David S. Miller | 1d299bc | 2011-11-14 20:32:16 -0800 | [diff] [blame] | 502 | */ |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 503 | if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) |
David S. Miller | e88d246 | 2011-11-15 12:57:00 -0800 | [diff] [blame] | 504 | regs->u_regs[UREG_G6] = orig_i0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 506 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
| 507 | |
| 508 | /* If the debugger messes with the program counter, it clears |
| 509 | * the software "in syscall" bit, directing us to not perform |
| 510 | * a syscall restart. |
| 511 | */ |
David S. Miller | 1d299bc | 2011-11-14 20:32:16 -0800 | [diff] [blame] | 512 | restart_syscall = 0; |
| 513 | if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) { |
| 514 | restart_syscall = 1; |
David S. Miller | e88d246 | 2011-11-15 12:57:00 -0800 | [diff] [blame] | 515 | orig_i0 = regs->u_regs[UREG_G6]; |
David S. Miller | 1d299bc | 2011-11-14 20:32:16 -0800 | [diff] [blame] | 516 | } |
| 517 | |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | if (signr > 0) { |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 520 | if (restart_syscall) |
| 521 | syscall_restart(orig_i0, regs, &ka.sa); |
Al Viro | a610d6e | 2012-05-21 23:42:15 -0400 | [diff] [blame] | 522 | handle_signal(signr, &ka, &info, regs); |
David S. Miller | 2d7d5f0 | 2006-01-19 02:42:49 -0800 | [diff] [blame] | 523 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 525 | if (restart_syscall && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | (regs->u_regs[UREG_I0] == ERESTARTNOHAND || |
| 527 | regs->u_regs[UREG_I0] == ERESTARTSYS || |
| 528 | regs->u_regs[UREG_I0] == ERESTARTNOINTR)) { |
| 529 | /* replay the system call when we are done */ |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 530 | regs->u_regs[UREG_I0] = orig_i0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | regs->pc -= 4; |
| 532 | regs->npc -= 4; |
David S. Miller | c278525 | 2010-09-21 22:30:13 -0700 | [diff] [blame] | 533 | pt_regs_clear_syscall(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
David S. Miller | 28e6103 | 2008-05-11 02:07:19 -0700 | [diff] [blame] | 535 | if (restart_syscall && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) { |
| 537 | regs->u_regs[UREG_G1] = __NR_restart_syscall; |
| 538 | regs->pc -= 4; |
| 539 | regs->npc -= 4; |
David S. Miller | c278525 | 2010-09-21 22:30:13 -0700 | [diff] [blame] | 540 | pt_regs_clear_syscall(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
David S. Miller | 2d7d5f0 | 2006-01-19 02:42:49 -0800 | [diff] [blame] | 542 | |
| 543 | /* if there's no signal to deliver, we just put the saved sigmask |
| 544 | * back |
| 545 | */ |
Al Viro | 51a7b44 | 2012-05-21 23:33:55 -0400 | [diff] [blame] | 546 | restore_saved_sigmask(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
| 548 | |
David S. Miller | 5a157d5 | 2008-07-27 03:38:53 -0700 | [diff] [blame] | 549 | void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, |
| 550 | unsigned long thread_info_flags) |
| 551 | { |
Al Viro | 6fd84c0 | 2012-05-23 15:28:58 -0400 | [diff] [blame] | 552 | if (thread_info_flags & _TIF_SIGPENDING) |
David S. Miller | 5a157d5 | 2008-07-27 03:38:53 -0700 | [diff] [blame] | 553 | do_signal(regs, orig_i0); |
| 554 | if (thread_info_flags & _TIF_NOTIFY_RESUME) { |
| 555 | clear_thread_flag(TIF_NOTIFY_RESUME); |
| 556 | tracehook_notify_resume(regs); |
| 557 | } |
| 558 | } |
| 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | asmlinkage int |
| 561 | do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr, |
| 562 | unsigned long sp) |
| 563 | { |
| 564 | int ret = -EFAULT; |
| 565 | |
| 566 | /* First see if old state is wanted. */ |
| 567 | if (ossptr) { |
| 568 | if (put_user(current->sas_ss_sp + current->sas_ss_size, |
| 569 | &ossptr->the_stack) || |
| 570 | __put_user(on_sig_stack(sp), &ossptr->cur_status)) |
| 571 | goto out; |
| 572 | } |
| 573 | |
| 574 | /* Now see if we want to update the new state. */ |
| 575 | if (ssptr) { |
| 576 | char *ss_sp; |
| 577 | |
| 578 | if (get_user(ss_sp, &ssptr->the_stack)) |
| 579 | goto out; |
| 580 | /* If the current stack was set with sigaltstack, don't |
| 581 | swap stacks while we are on it. */ |
| 582 | ret = -EPERM; |
| 583 | if (current->sas_ss_sp && on_sig_stack(sp)) |
| 584 | goto out; |
| 585 | |
| 586 | /* Since we don't know the extent of the stack, and we don't |
| 587 | track onstack-ness, but rather calculate it, we must |
| 588 | presume a size. Ho hum this interface is lossy. */ |
| 589 | current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ; |
| 590 | current->sas_ss_size = SIGSTKSZ; |
| 591 | } |
| 592 | ret = 0; |
| 593 | out: |
| 594 | return ret; |
| 595 | } |