]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/blackfin/kernel/signal.c
sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[mirror_ubuntu-bionic-kernel.git] / arch / blackfin / kernel / signal.c
CommitLineData
1394f032 1/*
e8f263df 2 * Copyright 2004-2010 Analog Devices Inc.
1394f032 3 *
96f1050d 4 * Licensed under the GPL-2 or later
1394f032
BW
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>
1f83b8f1 13#include <linux/uaccess.h>
d1be2e48 14#include <linux/tracehook.h>
68db0cf1 15#include <linux/sched/task_stack.h>
1394f032 16
1394f032
BW
17#include <asm/cacheflush.h>
18#include <asm/ucontext.h>
697a9d65 19#include <asm/fixed_code.h>
ddaebcab 20#include <asm/syscall.h>
1394f032 21
8513c42e
BS
22/* Location of the trace bit in SYSCFG. */
23#define TRACE_BITS 0x0001
24
1394f032
BW
25struct fdpic_func_descriptor {
26 unsigned long text;
27 unsigned long GOT;
28};
29
30struct rt_sigframe {
31 int sig;
32 struct siginfo *pinfo;
33 void *puc;
697a9d65
BS
34 /* This is no longer needed by the kernel, but unfortunately userspace
35 * code expects it to be there. */
1394f032
BW
36 char retcode[8];
37 struct siginfo info;
38 struct ucontext uc;
39};
40
1394f032 41static inline int
0ddeeca2 42rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
1394f032
BW
43{
44 unsigned long usp = 0;
45 int err = 0;
46
ddaebcab 47 /* Always make any pending restarted system calls return -EINTR */
f56141e3 48 current->restart_block.fn = do_no_restart_syscall;
ddaebcab 49
1394f032
BW
50#define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
51
52 /* restore passed registers */
53 RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
54 RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
55 RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
56 RESTORE(p4); RESTORE(p5);
57 err |= __get_user(usp, &sc->sc_usp);
58 wrusp(usp);
59 RESTORE(a0w); RESTORE(a1w);
60 RESTORE(a0x); RESTORE(a1x);
61 RESTORE(astat);
62 RESTORE(rets);
63 RESTORE(pc);
64 RESTORE(retx);
65 RESTORE(fp);
66 RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
67 RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
68 RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
69 RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
70 RESTORE(lc0); RESTORE(lc1);
71 RESTORE(lt0); RESTORE(lt1);
72 RESTORE(lb0); RESTORE(lb1);
73 RESTORE(seqstat);
74
75 regs->orig_p0 = -1; /* disable syscall checks */
76
77 *pr0 = regs->r0;
78 return err;
79}
80
135c37b8 81asmlinkage int sys_rt_sigreturn(void)
1394f032 82{
135c37b8 83 struct pt_regs *regs = current_pt_regs();
1394f032
BW
84 unsigned long usp = rdusp();
85 struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
86 sigset_t set;
87 int r0;
88
89 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
90 goto badframe;
91 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
92 goto badframe;
93
8e3f9f65 94 set_current_blocked(&set);
1394f032
BW
95
96 if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
97 goto badframe;
98
79d43210 99 if (restore_altstack(&frame->uc.uc_stack))
1394f032
BW
100 goto badframe;
101
102 return r0;
103
1f83b8f1 104 badframe:
1394f032
BW
105 force_sig(SIGSEGV, current);
106 return 0;
107}
108
109static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
110{
111 int err = 0;
112
113#define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
114
115 SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
116 SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
117 SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
118 SETUP(p4); SETUP(p5);
119 err |= __put_user(rdusp(), &sc->sc_usp);
120 SETUP(a0w); SETUP(a1w);
121 SETUP(a0x); SETUP(a1x);
122 SETUP(astat);
123 SETUP(rets);
124 SETUP(pc);
125 SETUP(retx);
126 SETUP(fp);
127 SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
128 SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
129 SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
130 SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
131 SETUP(lc0); SETUP(lc1);
132 SETUP(lt0); SETUP(lt1);
133 SETUP(lb0); SETUP(lb1);
134 SETUP(seqstat);
135
136 return err;
137}
138
e90670a9 139static inline void *get_sigframe(struct ksignal *ksig,
1394f032
BW
140 size_t frame_size)
141{
e90670a9 142 unsigned long usp = sigsp(rdusp(), ksig);
1394f032 143
1394f032
BW
144 return (void *)((usp - frame_size) & -8UL);
145}
146
147static int
1270cff1 148setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
1394f032
BW
149{
150 struct rt_sigframe *frame;
151 int err = 0;
152
e90670a9 153 frame = get_sigframe(ksig, sizeof(*frame));
1394f032 154
7bd83010 155 err |= __put_user(ksig->sig, &frame->sig);
1394f032
BW
156
157 err |= __put_user(&frame->info, &frame->pinfo);
158 err |= __put_user(&frame->uc, &frame->puc);
1270cff1 159 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
1394f032
BW
160
161 /* Create the ucontext. */
162 err |= __put_user(0, &frame->uc.uc_flags);
163 err |= __put_user(0, &frame->uc.uc_link);
79d43210 164 err |= __save_altstack(&frame->uc.uc_stack, rdusp());
1394f032
BW
165 err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
166 err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
167
1394f032 168 if (err)
29bf5dd8 169 return -EFAULT;
1394f032 170
1394f032 171 /* Set up registers for signal handler */
ecd0fa98 172 if (current->personality & FDPIC_FUNCPTRS) {
1394f032 173 struct fdpic_func_descriptor __user *funcptr =
1270cff1 174 (struct fdpic_func_descriptor *) ksig->ka.sa.sa_handler;
29bf5dd8
AV
175 u32 pc, p3;
176 err |= __get_user(pc, &funcptr->text);
177 err |= __get_user(p3, &funcptr->GOT);
178 if (err)
179 return -EFAULT;
180 regs->pc = pc;
181 regs->p3 = p3;
1394f032 182 } else
1270cff1 183 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
29bf5dd8 184 wrusp((unsigned long)frame);
697a9d65 185 regs->rets = SIGRETURN_STUB;
1394f032
BW
186
187 regs->r0 = frame->sig;
188 regs->r1 = (unsigned long)(&frame->info);
189 regs->r2 = (unsigned long)(&frame->uc);
190
191 return 0;
1394f032
BW
192}
193
194static inline void
195handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
196{
197 switch (regs->r0) {
198 case -ERESTARTNOHAND:
199 if (!has_handler)
200 goto do_restart;
201 regs->r0 = -EINTR;
202 break;
203
204 case -ERESTARTSYS:
205 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
206 regs->r0 = -EINTR;
207 break;
208 }
209 /* fallthrough */
210 case -ERESTARTNOINTR:
1f83b8f1 211 do_restart:
1394f032
BW
212 regs->p0 = regs->orig_p0;
213 regs->r0 = regs->orig_r0;
214 regs->pc -= 2;
215 break;
ddaebcab
MF
216
217 case -ERESTART_RESTARTBLOCK:
218 regs->p0 = __NR_restart_syscall;
219 regs->pc -= 2;
220 break;
1394f032
BW
221 }
222}
223
224/*
225 * OK, we're invoking a handler
226 */
a610d6e6 227static void
1270cff1 228handle_signal(struct ksignal *ksig, struct pt_regs *regs)
1394f032 229{
1270cff1
RW
230 int ret;
231
1394f032
BW
232 /* are we from a system call? to see pt_regs->orig_p0 */
233 if (regs->orig_p0 >= 0)
234 /* If so, check system call restarting.. */
1270cff1 235 handle_restart(regs, &ksig->ka, 1);
1394f032
BW
236
237 /* set up the stack frame */
1270cff1
RW
238 ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
239
240 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
1394f032
BW
241}
242
243/*
244 * Note that 'init' is a special process: it doesn't get signals it doesn't
245 * want to handle. Thus you cannot kill init even with a SIGKILL even by
246 * mistake.
247 *
248 * Note that we go through the signals twice: once to check the signals
249 * that the kernel can handle, and then we build all the user-level signal
250 * handling stack-frames in one go after that.
251 */
252asmlinkage void do_signal(struct pt_regs *regs)
253{
1270cff1 254 struct ksignal ksig;
1394f032
BW
255
256 current->thread.esp0 = (unsigned long)regs;
257
1270cff1 258 if (get_signal(&ksig)) {
1394f032 259 /* Whee! Actually deliver the signal. */
1270cff1 260 handle_signal(&ksig, regs);
1394f032
BW
261 return;
262 }
263
1394f032
BW
264 /* Did we come from a system call? */
265 if (regs->orig_p0 >= 0)
266 /* Restart the system call - no handlers present */
267 handle_restart(regs, NULL, 0);
268
269 /* if there's no signal to deliver, we just put the saved sigmask
270 * back */
51a7b448 271 restore_saved_sigmask();
1394f032 272}
d1be2e48
BS
273
274/*
275 * notification of userspace execution resumption
276 */
277asmlinkage void do_notify_resume(struct pt_regs *regs)
278{
6fd84c08 279 if (test_thread_flag(TIF_SIGPENDING))
d1be2e48
BS
280 do_signal(regs);
281
282 if (test_thread_flag(TIF_NOTIFY_RESUME)) {
283 clear_thread_flag(TIF_NOTIFY_RESUME);
284 tracehook_notify_resume(regs);
d1be2e48
BS
285 }
286}
287