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