License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / arch / cris / arch-v10 / kernel / signal.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/arch/cris/kernel/signal.c
4 *
5 * Based on arch/i386/kernel/signal.c by
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson *
8 *
9 * Ideas also taken from arch/arm.
10 *
a4858d4d 11 * Copyright (C) 2000-2007 Axis Communications AB
1da177e4
LT
12 *
13 * Authors: Bjorn Wesen (bjornw@axis.com)
14 *
15 */
16
17#include <linux/sched.h>
68db0cf1 18#include <linux/sched/task_stack.h>
1da177e4
LT
19#include <linux/mm.h>
20#include <linux/smp.h>
1da177e4
LT
21#include <linux/kernel.h>
22#include <linux/signal.h>
23#include <linux/errno.h>
24#include <linux/wait.h>
25#include <linux/ptrace.h>
26#include <linux/unistd.h>
27#include <linux/stddef.h>
28
29#include <asm/processor.h>
30#include <asm/ucontext.h>
7c0f6ba6 31#include <linux/uaccess.h>
b1a154db 32#include <arch/system.h>
1da177e4
LT
33
34#define DEBUG_SIG 0
35
1da177e4
LT
36/* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
37/* manipulate regs so that upon return, it will be re-executed */
38
39/* We rely on that pc points to the instruction after "break 13", so the
40 * library must never do strange things like putting it in a delay slot.
41 */
42#define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
43
a4858d4d 44void do_signal(int canrestart, struct pt_regs *regs);
1da177e4 45
1da177e4
LT
46/*
47 * Do a signal return; undo the signal stack.
48 */
49
50struct sigframe {
51 struct sigcontext sc;
52 unsigned long extramask[_NSIG_WORDS-1];
53 unsigned char retcode[8]; /* trampoline code */
54};
55
56struct rt_sigframe {
57 struct siginfo *pinfo;
58 void *puc;
59 struct siginfo info;
60 struct ucontext uc;
61 unsigned char retcode[8]; /* trampoline code */
62};
63
64
65static int
66restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
67{
68 unsigned int err = 0;
69 unsigned long old_usp;
70
71 /* Always make any pending restarted system calls return -EINTR */
f56141e3 72 current->restart_block.fn = do_no_restart_syscall;
1da177e4
LT
73
74 /* restore the regs from &sc->regs (same as sc, since regs is first)
75 * (sc is already checked for VERIFY_READ since the sigframe was
76 * checked in sys_sigreturn previously)
77 */
78
79 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
80 goto badframe;
81
82 /* make sure the U-flag is set so user-mode cannot fool us */
83
84 regs->dccr |= 1 << 8;
85
86 /* restore the old USP as it was before we stacked the sc etc.
87 * (we cannot just pop the sigcontext since we aligned the sp and
88 * stuff after pushing it)
89 */
90
91 err |= __get_user(old_usp, &sc->usp);
92
93 wrusp(old_usp);
94
95 /* TODO: the other ports use regs->orig_XX to disable syscall checks
96 * after this completes, but we don't use that mechanism. maybe we can
a4858d4d 97 * use it now ?
1da177e4
LT
98 */
99
100 return err;
101
102badframe:
103 return 1;
104}
105
e6a6d210 106asmlinkage int sys_sigreturn(void)
1da177e4 107{
e6a6d210 108 struct pt_regs *regs = current_pt_regs();
1da177e4
LT
109 struct sigframe __user *frame = (struct sigframe *)rdusp();
110 sigset_t set;
111
112 /*
113 * Since we stacked the signal on a dword boundary,
114 * then frame should be dword aligned here. If it's
115 * not, then the user is trying to mess with us.
116 */
117 if (((long)frame) & 3)
118 goto badframe;
119
120 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
121 goto badframe;
122 if (__get_user(set.sig[0], &frame->sc.oldmask)
123 || (_NSIG_WORDS > 1
124 && __copy_from_user(&set.sig[1], frame->extramask,
125 sizeof(frame->extramask))))
126 goto badframe;
127
f3b5e822 128 set_current_blocked(&set);
a4858d4d 129
1da177e4
LT
130 if (restore_sigcontext(regs, &frame->sc))
131 goto badframe;
132
133 /* TODO: SIGTRAP when single-stepping as in arm ? */
134
135 return regs->r10;
136
137badframe:
138 force_sig(SIGSEGV, current);
139 return 0;
a4858d4d 140}
1da177e4 141
e6a6d210 142asmlinkage int sys_rt_sigreturn(void)
1da177e4 143{
e6a6d210 144 struct pt_regs *regs = current_pt_regs();
1da177e4
LT
145 struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
146 sigset_t set;
147
148 /*
149 * Since we stacked the signal on a dword boundary,
150 * then frame should be dword aligned here. If it's
151 * not, then the user is trying to mess with us.
152 */
153 if (((long)frame) & 3)
154 goto badframe;
155
156 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
157 goto badframe;
158 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
159 goto badframe;
160
f3b5e822 161 set_current_blocked(&set);
a4858d4d 162
1da177e4
LT
163 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
164 goto badframe;
165
d970e428 166 if (restore_altstack(&frame->uc.uc_stack))
1da177e4
LT
167 goto badframe;
168
169 return regs->r10;
170
171badframe:
172 force_sig(SIGSEGV, current);
173 return 0;
a4858d4d 174}
1da177e4
LT
175
176/*
177 * Set up a signal frame.
178 */
179
a4858d4d
JN
180static int setup_sigcontext(struct sigcontext __user *sc,
181 struct pt_regs *regs, unsigned long mask)
1da177e4
LT
182{
183 int err = 0;
184 unsigned long usp = rdusp();
185
186 /* copy the regs. they are first in sc so we can use sc directly */
187
188 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
189
190 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
191 the signal handler. The frametype will be restored to its previous
192 value in restore_sigcontext. */
193 regs->frametype = CRIS_FRAME_NORMAL;
194
195 /* then some other stuff */
196
197 err |= __put_user(mask, &sc->oldmask);
198
199 err |= __put_user(usp, &sc->usp);
200
201 return err;
202}
203
a4858d4d
JN
204/* Figure out where we want to put the new signal frame
205 * - usually on the stack. */
1da177e4
LT
206
207static inline void __user *
8215ade8 208get_sigframe(struct ksignal *ksig, size_t frame_size)
1da177e4 209{
8215ade8 210 unsigned long sp = sigsp(rdusp(), ksig);
1da177e4
LT
211
212 /* make sure the frame is dword-aligned */
213
214 sp &= ~3;
215
216 return (void __user*)(sp - frame_size);
217}
218
219/* grab and setup a signal frame.
a4858d4d 220 *
1da177e4
LT
221 * basically we stack a lot of state info, and arrange for the
222 * user-mode program to return to the kernel using either a
223 * trampoline which performs the syscall sigreturn, or a provided
224 * user-mode trampoline.
225 */
226
fa019772
RW
227static int setup_frame(struct ksignal *ksig, sigset_t *set,
228 struct pt_regs *regs)
1da177e4
LT
229{
230 struct sigframe __user *frame;
231 unsigned long return_ip;
232 int err = 0;
233
8215ade8 234 frame = get_sigframe(ksig, sizeof(*frame));
1da177e4
LT
235
236 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
fa019772 237 return -EFAULT;
1da177e4
LT
238
239 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
240 if (err)
fa019772 241 return -EFAULT;
1da177e4
LT
242
243 if (_NSIG_WORDS > 1) {
244 err |= __copy_to_user(frame->extramask, &set->sig[1],
245 sizeof(frame->extramask));
246 }
247 if (err)
fa019772 248 return -EFAULT;
1da177e4
LT
249
250 /* Set up to return from userspace. If provided, use a stub
251 already in userspace. */
fa019772
RW
252 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
253 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
1da177e4
LT
254 } else {
255 /* trampoline - the desired return ip is the retcode itself */
256 return_ip = (unsigned long)&frame->retcode;
257 /* This is movu.w __NR_sigreturn, r9; break 13; */
258 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
259 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
260 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
261 }
262
263 if (err)
fa019772 264 return -EFAULT;
1da177e4
LT
265
266 /* Set up registers for signal handler */
267
fa019772 268 regs->irp = (unsigned long) ksig->ka.sa.sa_handler; /* what we enter NOW */
1da177e4 269 regs->srp = return_ip; /* what we enter LATER */
fa019772 270 regs->r10 = ksig->sig; /* first argument is signo */
1da177e4
LT
271
272 /* actually move the usp to reflect the stacked frame */
273
274 wrusp((unsigned long)frame);
275
a4858d4d 276 return 0;
1da177e4
LT
277}
278
fa019772
RW
279static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
280 struct pt_regs *regs)
1da177e4
LT
281{
282 struct rt_sigframe __user *frame;
283 unsigned long return_ip;
284 int err = 0;
285
8215ade8 286 frame = get_sigframe(ksig, sizeof(*frame));
1da177e4
LT
287
288 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
fa019772 289 return -EFAULT;
1da177e4
LT
290
291 err |= __put_user(&frame->info, &frame->pinfo);
292 err |= __put_user(&frame->uc, &frame->puc);
fa019772 293 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
1da177e4 294 if (err)
fa019772 295 return -EFAULT;
1da177e4
LT
296
297 /* Clear all the bits of the ucontext we don't use. */
298 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
299
300 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
301
302 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
303
9df794d9
AV
304 err |= __save_altstack(&frame->uc.uc_stack, rdusp());
305
1da177e4 306 if (err)
fa019772 307 return -EFAULT;
1da177e4
LT
308
309 /* Set up to return from userspace. If provided, use a stub
310 already in userspace. */
fa019772
RW
311 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
312 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
1da177e4
LT
313 } else {
314 /* trampoline - the desired return ip is the retcode itself */
315 return_ip = (unsigned long)&frame->retcode;
316 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
a4858d4d
JN
317 err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
318 err |= __put_user(__NR_rt_sigreturn,
319 (short __user *)(frame->retcode+2));
320 err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
1da177e4
LT
321 }
322
323 if (err)
fa019772 324 return -EFAULT;
1da177e4 325
1da177e4
LT
326 /* Set up registers for signal handler */
327
a4858d4d 328 /* What we enter NOW */
fa019772 329 regs->irp = (unsigned long) ksig->ka.sa.sa_handler;
a4858d4d
JN
330 /* What we enter LATER */
331 regs->srp = return_ip;
332 /* First argument is signo */
fa019772 333 regs->r10 = ksig->sig;
a4858d4d
JN
334 /* Second argument is (siginfo_t *) */
335 regs->r11 = (unsigned long)&frame->info;
336 /* Third argument is unused */
337 regs->r12 = 0;
338
339 /* Actually move the usp to reflect the stacked frame */
1da177e4
LT
340 wrusp((unsigned long)frame);
341
a4858d4d 342 return 0;
1da177e4
LT
343}
344
345/*
346 * OK, we're invoking a handler
a4858d4d 347 */
1da177e4 348
fa019772
RW
349static inline void handle_signal(int canrestart, struct ksignal *ksig,
350 struct pt_regs *regs)
1da177e4 351{
b7f9a11a 352 sigset_t *oldset = sigmask_to_save();
a4858d4d
JN
353 int ret;
354
1da177e4
LT
355 /* Are we from a system call? */
356 if (canrestart) {
357 /* If so, check system call restarting.. */
358 switch (regs->r10) {
a4858d4d
JN
359 case -ERESTART_RESTARTBLOCK:
360 case -ERESTARTNOHAND:
361 /* ERESTARTNOHAND means that the syscall should
362 * only be restarted if there was no handler for
363 * the signal, and since we only get here if there
364 * is a handler, we don't restart */
365 regs->r10 = -EINTR;
366 break;
367 case -ERESTARTSYS:
368 /* ERESTARTSYS means to restart the syscall if
369 * there is no handler or the handler was
370 * registered with SA_RESTART */
fa019772 371 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
1da177e4
LT
372 regs->r10 = -EINTR;
373 break;
a4858d4d
JN
374 }
375 /* fallthrough */
376 case -ERESTARTNOINTR:
377 /* ERESTARTNOINTR means that the syscall should
378 * be called again after the signal handler returns. */
379 RESTART_CRIS_SYS(regs);
1da177e4
LT
380 }
381 }
382
383 /* Set up the stack frame */
fa019772
RW
384 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
385 ret = setup_rt_frame(ksig, oldset, regs);
1da177e4 386 else
fa019772 387 ret = setup_frame(ksig, oldset, regs);
a4858d4d 388
fa019772 389 signal_setup_done(ret, ksig, 0);
1da177e4
LT
390}
391
392/*
393 * Note that 'init' is a special process: it doesn't get signals it doesn't
394 * want to handle. Thus you cannot kill init even with a SIGKILL even by
395 * mistake.
396 *
397 * Also note that the regs structure given here as an argument, is the latest
398 * pushed pt_regs. It may or may not be the same as the first pushed registers
399 * when the initial usermode->kernelmode transition took place. Therefore
400 * we can use user_mode(regs) to see if we came directly from kernel or user
401 * mode below.
402 */
403
a4858d4d 404void do_signal(int canrestart, struct pt_regs *regs)
1da177e4 405{
fa019772 406 struct ksignal ksig;
1da177e4
LT
407
408 /*
409 * We want the common case to go fast, which
410 * is why we may in certain cases get here from
411 * kernel mode. Just return without doing anything
412 * if so.
413 */
414 if (!user_mode(regs))
a4858d4d 415 return;
1da177e4 416
fa019772 417 if (get_signal(&ksig)) {
1da177e4 418 /* Whee! Actually deliver the signal. */
fa019772 419 handle_signal(canrestart, &ksig, regs);
a4858d4d 420 return;
1da177e4
LT
421 }
422
423 /* Did we come from a system call? */
424 if (canrestart) {
425 /* Restart the system call - no handlers present */
426 if (regs->r10 == -ERESTARTNOHAND ||
427 regs->r10 == -ERESTARTSYS ||
428 regs->r10 == -ERESTARTNOINTR) {
429 RESTART_CRIS_SYS(regs);
430 }
a4858d4d 431 if (regs->r10 == -ERESTART_RESTARTBLOCK) {
33dc0ad7 432 regs->r9 = __NR_restart_syscall;
1da177e4
LT
433 regs->irp -= 2;
434 }
435 }
a4858d4d
JN
436
437 /* if there's no signal to deliver, we just put the saved sigmask
438 * back */
51a7b448 439 restore_saved_sigmask();
1da177e4 440}