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