Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
Mike Frysinger | e8f263d | 2010-01-26 07:33:53 +0000 | [diff] [blame] | 2 | * Copyright 2004-2010 Analog Devices Inc. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 3 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 4 | * Licensed under the GPL-2 or later |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/signal.h> |
| 8 | #include <linux/syscalls.h> |
| 9 | #include <linux/ptrace.h> |
| 10 | #include <linux/tty.h> |
| 11 | #include <linux/personality.h> |
| 12 | #include <linux/binfmts.h> |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Barry Song | d1be2e4 | 2009-12-08 09:55:37 +0000 | [diff] [blame] | 14 | #include <linux/tracehook.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 15 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 16 | #include <asm/cacheflush.h> |
| 17 | #include <asm/ucontext.h> |
Bernd Schmidt | 697a9d6 | 2008-04-24 02:51:36 +0800 | [diff] [blame] | 18 | #include <asm/fixed_code.h> |
Mike Frysinger | ddaebca | 2010-01-29 01:33:54 +0000 | [diff] [blame] | 19 | #include <asm/syscall.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 20 | |
Bernd Schmidt | 8513c42e | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 21 | /* Location of the trace bit in SYSCFG. */ |
| 22 | #define TRACE_BITS 0x0001 |
| 23 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 24 | struct fdpic_func_descriptor { |
| 25 | unsigned long text; |
| 26 | unsigned long GOT; |
| 27 | }; |
| 28 | |
| 29 | struct rt_sigframe { |
| 30 | int sig; |
| 31 | struct siginfo *pinfo; |
| 32 | void *puc; |
Bernd Schmidt | 697a9d6 | 2008-04-24 02:51:36 +0800 | [diff] [blame] | 33 | /* This is no longer needed by the kernel, but unfortunately userspace |
| 34 | * code expects it to be there. */ |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 35 | char retcode[8]; |
| 36 | struct siginfo info; |
| 37 | struct ucontext uc; |
| 38 | }; |
| 39 | |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 40 | asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 41 | { |
| 42 | return do_sigaltstack(uss, uoss, rdusp()); |
| 43 | } |
| 44 | |
| 45 | static inline int |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 46 | rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 47 | { |
| 48 | unsigned long usp = 0; |
| 49 | int err = 0; |
| 50 | |
Mike Frysinger | ddaebca | 2010-01-29 01:33:54 +0000 | [diff] [blame] | 51 | /* Always make any pending restarted system calls return -EINTR */ |
| 52 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 53 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 54 | #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x) |
| 55 | |
| 56 | /* restore passed registers */ |
| 57 | RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3); |
| 58 | RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7); |
| 59 | RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3); |
| 60 | RESTORE(p4); RESTORE(p5); |
| 61 | err |= __get_user(usp, &sc->sc_usp); |
| 62 | wrusp(usp); |
| 63 | RESTORE(a0w); RESTORE(a1w); |
| 64 | RESTORE(a0x); RESTORE(a1x); |
| 65 | RESTORE(astat); |
| 66 | RESTORE(rets); |
| 67 | RESTORE(pc); |
| 68 | RESTORE(retx); |
| 69 | RESTORE(fp); |
| 70 | RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3); |
| 71 | RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3); |
| 72 | RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3); |
| 73 | RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3); |
| 74 | RESTORE(lc0); RESTORE(lc1); |
| 75 | RESTORE(lt0); RESTORE(lt1); |
| 76 | RESTORE(lb0); RESTORE(lb1); |
| 77 | RESTORE(seqstat); |
| 78 | |
| 79 | regs->orig_p0 = -1; /* disable syscall checks */ |
| 80 | |
| 81 | *pr0 = regs->r0; |
| 82 | return err; |
| 83 | } |
| 84 | |
Al Viro | 135c37b | 2012-11-13 23:47:37 -0500 | [diff] [blame] | 85 | asmlinkage int sys_rt_sigreturn(void) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 86 | { |
Al Viro | 135c37b | 2012-11-13 23:47:37 -0500 | [diff] [blame] | 87 | struct pt_regs *regs = current_pt_regs(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 88 | unsigned long usp = rdusp(); |
| 89 | struct rt_sigframe *frame = (struct rt_sigframe *)(usp); |
| 90 | sigset_t set; |
| 91 | int r0; |
| 92 | |
| 93 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 94 | goto badframe; |
| 95 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
| 96 | goto badframe; |
| 97 | |
Matt Fleming | 8e3f9f6 | 2012-05-11 10:58:11 +1000 | [diff] [blame] | 98 | set_current_blocked(&set); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 99 | |
| 100 | if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0)) |
| 101 | goto badframe; |
| 102 | |
| 103 | if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT) |
| 104 | goto badframe; |
| 105 | |
| 106 | return r0; |
| 107 | |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 108 | badframe: |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 109 | force_sig(SIGSEGV, current); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs) |
| 114 | { |
| 115 | int err = 0; |
| 116 | |
| 117 | #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x) |
| 118 | |
| 119 | SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3); |
| 120 | SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7); |
| 121 | SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3); |
| 122 | SETUP(p4); SETUP(p5); |
| 123 | err |= __put_user(rdusp(), &sc->sc_usp); |
| 124 | SETUP(a0w); SETUP(a1w); |
| 125 | SETUP(a0x); SETUP(a1x); |
| 126 | SETUP(astat); |
| 127 | SETUP(rets); |
| 128 | SETUP(pc); |
| 129 | SETUP(retx); |
| 130 | SETUP(fp); |
| 131 | SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3); |
| 132 | SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3); |
| 133 | SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3); |
| 134 | SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3); |
| 135 | SETUP(lc0); SETUP(lc1); |
| 136 | SETUP(lt0); SETUP(lt1); |
| 137 | SETUP(lb0); SETUP(lb1); |
| 138 | SETUP(seqstat); |
| 139 | |
| 140 | return err; |
| 141 | } |
| 142 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 143 | static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, |
| 144 | size_t frame_size) |
| 145 | { |
| 146 | unsigned long usp; |
| 147 | |
| 148 | /* Default to using normal stack. */ |
| 149 | usp = rdusp(); |
| 150 | |
| 151 | /* This is the X/Open sanctioned signal stack switching. */ |
| 152 | if (ka->sa.sa_flags & SA_ONSTACK) { |
| 153 | if (!on_sig_stack(usp)) |
| 154 | usp = current->sas_ss_sp + current->sas_ss_size; |
| 155 | } |
| 156 | return (void *)((usp - frame_size) & -8UL); |
| 157 | } |
| 158 | |
| 159 | static int |
| 160 | setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info, |
| 161 | sigset_t * set, struct pt_regs *regs) |
| 162 | { |
| 163 | struct rt_sigframe *frame; |
| 164 | int err = 0; |
| 165 | |
| 166 | frame = get_sigframe(ka, regs, sizeof(*frame)); |
| 167 | |
| 168 | err |= __put_user((current_thread_info()->exec_domain |
| 169 | && current_thread_info()->exec_domain->signal_invmap |
| 170 | && sig < 32 |
| 171 | ? current_thread_info()->exec_domain-> |
| 172 | signal_invmap[sig] : sig), &frame->sig); |
| 173 | |
| 174 | err |= __put_user(&frame->info, &frame->pinfo); |
| 175 | err |= __put_user(&frame->uc, &frame->puc); |
| 176 | err |= copy_siginfo_to_user(&frame->info, info); |
| 177 | |
| 178 | /* Create the ucontext. */ |
| 179 | err |= __put_user(0, &frame->uc.uc_flags); |
| 180 | err |= __put_user(0, &frame->uc.uc_link); |
| 181 | err |= |
| 182 | __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); |
| 183 | err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags); |
| 184 | err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); |
| 185 | err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs); |
| 186 | err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 187 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 188 | if (err) |
Al Viro | 29bf5dd | 2012-05-02 21:14:30 -0400 | [diff] [blame] | 189 | return -EFAULT; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 190 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 191 | /* Set up registers for signal handler */ |
WANG Cong | ecd0fa9 | 2008-04-29 00:59:15 -0700 | [diff] [blame] | 192 | if (current->personality & FDPIC_FUNCPTRS) { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 193 | struct fdpic_func_descriptor __user *funcptr = |
| 194 | (struct fdpic_func_descriptor *) ka->sa.sa_handler; |
Al Viro | 29bf5dd | 2012-05-02 21:14:30 -0400 | [diff] [blame] | 195 | u32 pc, p3; |
| 196 | err |= __get_user(pc, &funcptr->text); |
| 197 | err |= __get_user(p3, &funcptr->GOT); |
| 198 | if (err) |
| 199 | return -EFAULT; |
| 200 | regs->pc = pc; |
| 201 | regs->p3 = p3; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 202 | } else |
| 203 | regs->pc = (unsigned long)ka->sa.sa_handler; |
Al Viro | 29bf5dd | 2012-05-02 21:14:30 -0400 | [diff] [blame] | 204 | wrusp((unsigned long)frame); |
Bernd Schmidt | 697a9d6 | 2008-04-24 02:51:36 +0800 | [diff] [blame] | 205 | regs->rets = SIGRETURN_STUB; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 206 | |
| 207 | regs->r0 = frame->sig; |
| 208 | regs->r1 = (unsigned long)(&frame->info); |
| 209 | regs->r2 = (unsigned long)(&frame->uc); |
| 210 | |
| 211 | return 0; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static inline void |
| 215 | handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler) |
| 216 | { |
| 217 | switch (regs->r0) { |
| 218 | case -ERESTARTNOHAND: |
| 219 | if (!has_handler) |
| 220 | goto do_restart; |
| 221 | regs->r0 = -EINTR; |
| 222 | break; |
| 223 | |
| 224 | case -ERESTARTSYS: |
| 225 | if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) { |
| 226 | regs->r0 = -EINTR; |
| 227 | break; |
| 228 | } |
| 229 | /* fallthrough */ |
| 230 | case -ERESTARTNOINTR: |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 231 | do_restart: |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 232 | regs->p0 = regs->orig_p0; |
| 233 | regs->r0 = regs->orig_r0; |
| 234 | regs->pc -= 2; |
| 235 | break; |
Mike Frysinger | ddaebca | 2010-01-29 01:33:54 +0000 | [diff] [blame] | 236 | |
| 237 | case -ERESTART_RESTARTBLOCK: |
| 238 | regs->p0 = __NR_restart_syscall; |
| 239 | regs->pc -= 2; |
| 240 | break; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * OK, we're invoking a handler |
| 246 | */ |
Al Viro | a610d6e | 2012-05-21 23:42:15 -0400 | [diff] [blame] | 247 | static void |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 248 | handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka, |
Al Viro | b7f9a11 | 2012-05-02 09:59:21 -0400 | [diff] [blame] | 249 | struct pt_regs *regs) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 250 | { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 251 | /* are we from a system call? to see pt_regs->orig_p0 */ |
| 252 | if (regs->orig_p0 >= 0) |
| 253 | /* If so, check system call restarting.. */ |
| 254 | handle_restart(regs, ka, 1); |
| 255 | |
| 256 | /* set up the stack frame */ |
Al Viro | 29bf5dd | 2012-05-02 21:14:30 -0400 | [diff] [blame] | 257 | if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0) |
| 258 | force_sigsegv(sig, current); |
| 259 | else |
| 260 | signal_delivered(sig, info, ka, regs, |
| 261 | test_thread_flag(TIF_SINGLESTEP)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Note that 'init' is a special process: it doesn't get signals it doesn't |
| 266 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 267 | * mistake. |
| 268 | * |
| 269 | * Note that we go through the signals twice: once to check the signals |
| 270 | * that the kernel can handle, and then we build all the user-level signal |
| 271 | * handling stack-frames in one go after that. |
| 272 | */ |
| 273 | asmlinkage void do_signal(struct pt_regs *regs) |
| 274 | { |
| 275 | siginfo_t info; |
| 276 | int signr; |
| 277 | struct k_sigaction ka; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 278 | |
| 279 | current->thread.esp0 = (unsigned long)regs; |
| 280 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 281 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
| 282 | if (signr > 0) { |
| 283 | /* Whee! Actually deliver the signal. */ |
Al Viro | a610d6e | 2012-05-21 23:42:15 -0400 | [diff] [blame] | 284 | handle_signal(signr, &info, &ka, regs); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 288 | /* Did we come from a system call? */ |
| 289 | if (regs->orig_p0 >= 0) |
| 290 | /* Restart the system call - no handlers present */ |
| 291 | handle_restart(regs, NULL, 0); |
| 292 | |
| 293 | /* if there's no signal to deliver, we just put the saved sigmask |
| 294 | * back */ |
Al Viro | 51a7b44 | 2012-05-21 23:33:55 -0400 | [diff] [blame] | 295 | restore_saved_sigmask(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 296 | } |
Barry Song | d1be2e4 | 2009-12-08 09:55:37 +0000 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * notification of userspace execution resumption |
| 300 | */ |
| 301 | asmlinkage void do_notify_resume(struct pt_regs *regs) |
| 302 | { |
Al Viro | 6fd84c0 | 2012-05-23 15:28:58 -0400 | [diff] [blame] | 303 | if (test_thread_flag(TIF_SIGPENDING)) |
Barry Song | d1be2e4 | 2009-12-08 09:55:37 +0000 | [diff] [blame] | 304 | do_signal(regs); |
| 305 | |
| 306 | if (test_thread_flag(TIF_NOTIFY_RESUME)) { |
| 307 | clear_thread_flag(TIF_NOTIFY_RESUME); |
| 308 | tracehook_notify_resume(regs); |
Barry Song | d1be2e4 | 2009-12-08 09:55:37 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |