Linux 3.12.28
[linux/fpc-iii.git] / arch / blackfin / kernel / signal.c
blobb022af6c48f85ad91bb69ed66dbaa283cb0b6db7
1 /*
2 * Copyright 2004-2010 Analog Devices Inc.
4 * Licensed under the GPL-2 or later
5 */
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 {
25 unsigned long text;
26 unsigned long GOT;
29 struct rt_sigframe {
30 int sig;
31 struct siginfo *pinfo;
32 void *puc;
33 /* This is no longer needed by the kernel, but unfortunately userspace
34 * code expects it to be there. */
35 char retcode[8];
36 struct siginfo info;
37 struct ucontext uc;
40 static inline int
41 rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
43 unsigned long usp = 0;
44 int err = 0;
46 /* Always make any pending restarted system calls return -EINTR */
47 current_thread_info()->restart_block.fn = do_no_restart_syscall;
49 #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
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);
74 regs->orig_p0 = -1; /* disable syscall checks */
76 *pr0 = regs->r0;
77 return err;
80 asmlinkage int sys_rt_sigreturn(void)
82 struct pt_regs *regs = current_pt_regs();
83 unsigned long usp = rdusp();
84 struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
85 sigset_t set;
86 int r0;
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;
93 set_current_blocked(&set);
95 if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
96 goto badframe;
98 if (restore_altstack(&frame->uc.uc_stack))
99 goto badframe;
101 return r0;
103 badframe:
104 force_sig(SIGSEGV, current);
105 return 0;
108 static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
110 int err = 0;
112 #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
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);
135 return err;
138 static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
139 size_t frame_size)
141 unsigned long usp;
143 /* Default to using normal stack. */
144 usp = rdusp();
146 /* This is the X/Open sanctioned signal stack switching. */
147 if (ka->sa.sa_flags & SA_ONSTACK) {
148 if (!on_sig_stack(usp))
149 usp = current->sas_ss_sp + current->sas_ss_size;
151 return (void *)((usp - frame_size) & -8UL);
154 static int
155 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
156 sigset_t * set, struct pt_regs *regs)
158 struct rt_sigframe *frame;
159 int err = 0;
161 frame = get_sigframe(ka, regs, sizeof(*frame));
163 err |= __put_user((current_thread_info()->exec_domain
164 && current_thread_info()->exec_domain->signal_invmap
165 && sig < 32
166 ? current_thread_info()->exec_domain->
167 signal_invmap[sig] : sig), &frame->sig);
169 err |= __put_user(&frame->info, &frame->pinfo);
170 err |= __put_user(&frame->uc, &frame->puc);
171 err |= copy_siginfo_to_user(&frame->info, info);
173 /* Create the ucontext. */
174 err |= __put_user(0, &frame->uc.uc_flags);
175 err |= __put_user(0, &frame->uc.uc_link);
176 err |= __save_altstack(&frame->uc.uc_stack, rdusp());
177 err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
178 err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
180 if (err)
181 return -EFAULT;
183 /* Set up registers for signal handler */
184 if (current->personality & FDPIC_FUNCPTRS) {
185 struct fdpic_func_descriptor __user *funcptr =
186 (struct fdpic_func_descriptor *) ka->sa.sa_handler;
187 u32 pc, p3;
188 err |= __get_user(pc, &funcptr->text);
189 err |= __get_user(p3, &funcptr->GOT);
190 if (err)
191 return -EFAULT;
192 regs->pc = pc;
193 regs->p3 = p3;
194 } else
195 regs->pc = (unsigned long)ka->sa.sa_handler;
196 wrusp((unsigned long)frame);
197 regs->rets = SIGRETURN_STUB;
199 regs->r0 = frame->sig;
200 regs->r1 = (unsigned long)(&frame->info);
201 regs->r2 = (unsigned long)(&frame->uc);
203 return 0;
206 static inline void
207 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
209 switch (regs->r0) {
210 case -ERESTARTNOHAND:
211 if (!has_handler)
212 goto do_restart;
213 regs->r0 = -EINTR;
214 break;
216 case -ERESTARTSYS:
217 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
218 regs->r0 = -EINTR;
219 break;
221 /* fallthrough */
222 case -ERESTARTNOINTR:
223 do_restart:
224 regs->p0 = regs->orig_p0;
225 regs->r0 = regs->orig_r0;
226 regs->pc -= 2;
227 break;
229 case -ERESTART_RESTARTBLOCK:
230 regs->p0 = __NR_restart_syscall;
231 regs->pc -= 2;
232 break;
237 * OK, we're invoking a handler
239 static void
240 handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
241 struct pt_regs *regs)
243 /* are we from a system call? to see pt_regs->orig_p0 */
244 if (regs->orig_p0 >= 0)
245 /* If so, check system call restarting.. */
246 handle_restart(regs, ka, 1);
248 /* set up the stack frame */
249 if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0)
250 force_sigsegv(sig, current);
251 else
252 signal_delivered(sig, info, ka, regs,
253 test_thread_flag(TIF_SINGLESTEP));
257 * Note that 'init' is a special process: it doesn't get signals it doesn't
258 * want to handle. Thus you cannot kill init even with a SIGKILL even by
259 * mistake.
261 * Note that we go through the signals twice: once to check the signals
262 * that the kernel can handle, and then we build all the user-level signal
263 * handling stack-frames in one go after that.
265 asmlinkage void do_signal(struct pt_regs *regs)
267 siginfo_t info;
268 int signr;
269 struct k_sigaction ka;
271 current->thread.esp0 = (unsigned long)regs;
273 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
274 if (signr > 0) {
275 /* Whee! Actually deliver the signal. */
276 handle_signal(signr, &info, &ka, regs);
277 return;
280 /* Did we come from a system call? */
281 if (regs->orig_p0 >= 0)
282 /* Restart the system call - no handlers present */
283 handle_restart(regs, NULL, 0);
285 /* if there's no signal to deliver, we just put the saved sigmask
286 * back */
287 restore_saved_sigmask();
291 * notification of userspace execution resumption
293 asmlinkage void do_notify_resume(struct pt_regs *regs)
295 if (test_thread_flag(TIF_SIGPENDING))
296 do_signal(regs);
298 if (test_thread_flag(TIF_NOTIFY_RESUME)) {
299 clear_thread_flag(TIF_NOTIFY_RESUME);
300 tracehook_notify_resume(regs);