2 * Copyright 2004-2010 Analog Devices Inc.
4 * Licensed under the GPL-2 or later
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>
13 #include <linux/uaccess.h>
14 #include <linux/tracehook.h>
16 #include <asm/cacheflush.h>
17 #include <asm/ucontext.h>
18 #include <asm/fixed_code.h>
19 #include <asm/syscall.h>
21 /* Location of the trace bit in SYSCFG. */
22 #define TRACE_BITS 0x0001
24 struct fdpic_func_descriptor {
31 struct siginfo *pinfo;
33 /* This is no longer needed by the kernel, but unfortunately userspace
34 * code expects it to be there. */
40 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
42 return do_sigaltstack(uss, uoss, rdusp());
46 rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
48 unsigned long usp = 0;
51 /* Always make any pending restarted system calls return -EINTR */
52 current_thread_info()->restart_block.fn = do_no_restart_syscall;
54 #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
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);
63 RESTORE(a0w); RESTORE(a1w);
64 RESTORE(a0x); RESTORE(a1x);
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);
79 regs->orig_p0 = -1; /* disable syscall checks */
85 asmlinkage int do_rt_sigreturn(unsigned long __unused)
87 struct pt_regs *regs = (struct pt_regs *)__unused;
88 unsigned long usp = rdusp();
89 struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
93 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
95 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
98 set_current_blocked(&set);
100 if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
103 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
109 force_sig(SIGSEGV, current);
113 static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
117 #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
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);
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);
143 static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
148 /* Default to using normal stack. */
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;
156 return (void *)((usp - frame_size) & -8UL);
160 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
161 sigset_t * set, struct pt_regs *regs)
163 struct rt_sigframe *frame;
166 frame = get_sigframe(ka, regs, sizeof(*frame));
168 err |= __put_user((current_thread_info()->exec_domain
169 && current_thread_info()->exec_domain->signal_invmap
171 ? current_thread_info()->exec_domain->
172 signal_invmap[sig] : sig), &frame->sig);
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);
178 /* Create the ucontext. */
179 err |= __put_user(0, &frame->uc.uc_flags);
180 err |= __put_user(0, &frame->uc.uc_link);
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));
191 /* Set up registers for signal handler */
192 if (current->personality & FDPIC_FUNCPTRS) {
193 struct fdpic_func_descriptor __user *funcptr =
194 (struct fdpic_func_descriptor *) ka->sa.sa_handler;
196 err |= __get_user(pc, &funcptr->text);
197 err |= __get_user(p3, &funcptr->GOT);
203 regs->pc = (unsigned long)ka->sa.sa_handler;
204 wrusp((unsigned long)frame);
205 regs->rets = SIGRETURN_STUB;
207 regs->r0 = frame->sig;
208 regs->r1 = (unsigned long)(&frame->info);
209 regs->r2 = (unsigned long)(&frame->uc);
215 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
218 case -ERESTARTNOHAND:
225 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
230 case -ERESTARTNOINTR:
232 regs->p0 = regs->orig_p0;
233 regs->r0 = regs->orig_r0;
237 case -ERESTART_RESTARTBLOCK:
238 regs->p0 = __NR_restart_syscall;
245 * OK, we're invoking a handler
248 handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
249 struct pt_regs *regs)
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);
256 /* set up the stack frame */
257 if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0)
258 force_sigsegv(sig, current);
260 signal_delivered(sig, info, ka, regs,
261 test_thread_flag(TIF_SINGLESTEP));
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
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.
273 asmlinkage void do_signal(struct pt_regs *regs)
277 struct k_sigaction ka;
279 current->thread.esp0 = (unsigned long)regs;
281 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
283 /* Whee! Actually deliver the signal. */
284 handle_signal(signr, &info, &ka, regs);
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);
293 /* if there's no signal to deliver, we just put the saved sigmask
295 restore_saved_sigmask();
299 * notification of userspace execution resumption
301 asmlinkage void do_notify_resume(struct pt_regs *regs)
303 if (test_thread_flag(TIF_SIGPENDING))
306 if (test_thread_flag(TIF_NOTIFY_RESUME)) {
307 clear_thread_flag(TIF_NOTIFY_RESUME);
308 tracehook_notify_resume(regs);