blob: 61ce6273a8952ed6b6986b2dbadf651c0c9c5d47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/cris/kernel/signal.c
3 *
4 * Based on arch/i386/kernel/signal.c by
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson *
7 *
8 * Ideas also taken from arch/arm.
9 *
Jesper Nilssona4858d42008-01-14 00:55:22 -080010 * Copyright (C) 2000-2007 Axis Communications AB
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Authors: Bjorn Wesen (bjornw@axis.com)
13 *
14 */
15
16#include <linux/sched.h>
17#include <linux/mm.h>
18#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
20#include <linux/signal.h>
21#include <linux/errno.h>
22#include <linux/wait.h>
23#include <linux/ptrace.h>
24#include <linux/unistd.h>
25#include <linux/stddef.h>
26
27#include <asm/processor.h>
28#include <asm/ucontext.h>
29#include <asm/uaccess.h>
David Howellsb1a154d2012-03-28 18:30:02 +010030#include <arch/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define DEBUG_SIG 0
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
35/* manipulate regs so that upon return, it will be re-executed */
36
37/* We rely on that pc points to the instruction after "break 13", so the
38 * library must never do strange things like putting it in a delay slot.
39 */
40#define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
41
Jesper Nilssona4858d42008-01-14 00:55:22 -080042void do_signal(int canrestart, struct pt_regs *regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Do a signal return; undo the signal stack.
46 */
47
48struct sigframe {
49 struct sigcontext sc;
50 unsigned long extramask[_NSIG_WORDS-1];
51 unsigned char retcode[8]; /* trampoline code */
52};
53
54struct rt_sigframe {
55 struct siginfo *pinfo;
56 void *puc;
57 struct siginfo info;
58 struct ucontext uc;
59 unsigned char retcode[8]; /* trampoline code */
60};
61
62
63static int
64restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
65{
66 unsigned int err = 0;
67 unsigned long old_usp;
68
69 /* Always make any pending restarted system calls return -EINTR */
70 current_thread_info()->restart_block.fn = do_no_restart_syscall;
71
72 /* restore the regs from &sc->regs (same as sc, since regs is first)
73 * (sc is already checked for VERIFY_READ since the sigframe was
74 * checked in sys_sigreturn previously)
75 */
76
77 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
78 goto badframe;
79
80 /* make sure the U-flag is set so user-mode cannot fool us */
81
82 regs->dccr |= 1 << 8;
83
84 /* restore the old USP as it was before we stacked the sc etc.
85 * (we cannot just pop the sigcontext since we aligned the sp and
86 * stuff after pushing it)
87 */
88
89 err |= __get_user(old_usp, &sc->usp);
90
91 wrusp(old_usp);
92
93 /* TODO: the other ports use regs->orig_XX to disable syscall checks
94 * after this completes, but we don't use that mechanism. maybe we can
Jesper Nilssona4858d42008-01-14 00:55:22 -080095 * use it now ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97
98 return err;
99
100badframe:
101 return 1;
102}
103
Al Viroe6a6d212012-12-26 22:11:53 -0500104asmlinkage int sys_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Al Viroe6a6d212012-12-26 22:11:53 -0500106 struct pt_regs *regs = current_pt_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct sigframe __user *frame = (struct sigframe *)rdusp();
108 sigset_t set;
109
110 /*
111 * Since we stacked the signal on a dword boundary,
112 * then frame should be dword aligned here. If it's
113 * not, then the user is trying to mess with us.
114 */
115 if (((long)frame) & 3)
116 goto badframe;
117
118 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
119 goto badframe;
120 if (__get_user(set.sig[0], &frame->sc.oldmask)
121 || (_NSIG_WORDS > 1
122 && __copy_from_user(&set.sig[1], frame->extramask,
123 sizeof(frame->extramask))))
124 goto badframe;
125
Matt Flemingf3b5e822012-05-11 10:58:00 +1000126 set_current_blocked(&set);
Jesper Nilssona4858d42008-01-14 00:55:22 -0800127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (restore_sigcontext(regs, &frame->sc))
129 goto badframe;
130
131 /* TODO: SIGTRAP when single-stepping as in arm ? */
132
133 return regs->r10;
134
135badframe:
136 force_sig(SIGSEGV, current);
137 return 0;
Jesper Nilssona4858d42008-01-14 00:55:22 -0800138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Al Viroe6a6d212012-12-26 22:11:53 -0500140asmlinkage int sys_rt_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Al Viroe6a6d212012-12-26 22:11:53 -0500142 struct pt_regs *regs = current_pt_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
144 sigset_t set;
145
146 /*
147 * Since we stacked the signal on a dword boundary,
148 * then frame should be dword aligned here. If it's
149 * not, then the user is trying to mess with us.
150 */
151 if (((long)frame) & 3)
152 goto badframe;
153
154 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
155 goto badframe;
156 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
157 goto badframe;
158
Matt Flemingf3b5e822012-05-11 10:58:00 +1000159 set_current_blocked(&set);
Jesper Nilssona4858d42008-01-14 00:55:22 -0800160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
162 goto badframe;
163
Al Virod970e422012-12-23 02:07:30 -0500164 if (restore_altstack(&frame->uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 goto badframe;
166
167 return regs->r10;
168
169badframe:
170 force_sig(SIGSEGV, current);
171 return 0;
Jesper Nilssona4858d42008-01-14 00:55:22 -0800172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/*
175 * Set up a signal frame.
176 */
177
Jesper Nilssona4858d42008-01-14 00:55:22 -0800178static int setup_sigcontext(struct sigcontext __user *sc,
179 struct pt_regs *regs, unsigned long mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int err = 0;
182 unsigned long usp = rdusp();
183
184 /* copy the regs. they are first in sc so we can use sc directly */
185
186 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
187
188 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
189 the signal handler. The frametype will be restored to its previous
190 value in restore_sigcontext. */
191 regs->frametype = CRIS_FRAME_NORMAL;
192
193 /* then some other stuff */
194
195 err |= __put_user(mask, &sc->oldmask);
196
197 err |= __put_user(usp, &sc->usp);
198
199 return err;
200}
201
Jesper Nilssona4858d42008-01-14 00:55:22 -0800202/* Figure out where we want to put the new signal frame
203 * - usually on the stack. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205static inline void __user *
Jesper Nilssona4858d42008-01-14 00:55:22 -0800206get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 unsigned long sp = rdusp();
209
210 /* This is the X/Open sanctioned signal stack switching. */
211 if (ka->sa.sa_flags & SA_ONSTACK) {
212 if (! on_sig_stack(sp))
213 sp = current->sas_ss_sp + current->sas_ss_size;
214 }
215
216 /* make sure the frame is dword-aligned */
217
218 sp &= ~3;
219
220 return (void __user*)(sp - frame_size);
221}
222
223/* grab and setup a signal frame.
Jesper Nilssona4858d42008-01-14 00:55:22 -0800224 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * basically we stack a lot of state info, and arrange for the
226 * user-mode program to return to the kernel using either a
227 * trampoline which performs the syscall sigreturn, or a provided
228 * user-mode trampoline.
229 */
230
Jesper Nilssona4858d42008-01-14 00:55:22 -0800231static int setup_frame(int sig, struct k_sigaction *ka,
232 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct sigframe __user *frame;
235 unsigned long return_ip;
236 int err = 0;
237
238 frame = get_sigframe(ka, regs, sizeof(*frame));
239
240 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
241 goto give_sigsegv;
242
243 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
244 if (err)
245 goto give_sigsegv;
246
247 if (_NSIG_WORDS > 1) {
248 err |= __copy_to_user(frame->extramask, &set->sig[1],
249 sizeof(frame->extramask));
250 }
251 if (err)
252 goto give_sigsegv;
253
254 /* Set up to return from userspace. If provided, use a stub
255 already in userspace. */
256 if (ka->sa.sa_flags & SA_RESTORER) {
257 return_ip = (unsigned long)ka->sa.sa_restorer;
258 } else {
259 /* trampoline - the desired return ip is the retcode itself */
260 return_ip = (unsigned long)&frame->retcode;
261 /* This is movu.w __NR_sigreturn, r9; break 13; */
262 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
263 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
264 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
265 }
266
267 if (err)
268 goto give_sigsegv;
269
270 /* Set up registers for signal handler */
271
272 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
273 regs->srp = return_ip; /* what we enter LATER */
274 regs->r10 = sig; /* first argument is signo */
275
276 /* actually move the usp to reflect the stacked frame */
277
278 wrusp((unsigned long)frame);
279
Jesper Nilssona4858d42008-01-14 00:55:22 -0800280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282give_sigsegv:
283 force_sigsegv(sig, current);
Jesper Nilssona4858d42008-01-14 00:55:22 -0800284 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Jesper Nilssona4858d42008-01-14 00:55:22 -0800287static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
288 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct rt_sigframe __user *frame;
291 unsigned long return_ip;
292 int err = 0;
293
294 frame = get_sigframe(ka, regs, sizeof(*frame));
295
296 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
297 goto give_sigsegv;
298
299 err |= __put_user(&frame->info, &frame->pinfo);
300 err |= __put_user(&frame->uc, &frame->puc);
301 err |= copy_siginfo_to_user(&frame->info, info);
302 if (err)
303 goto give_sigsegv;
304
305 /* Clear all the bits of the ucontext we don't use. */
306 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
307
308 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
309
310 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
311
Al Viro9df794d2012-12-23 02:11:49 -0500312 err |= __save_altstack(&frame->uc.uc_stack, rdusp());
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (err)
315 goto give_sigsegv;
316
317 /* Set up to return from userspace. If provided, use a stub
318 already in userspace. */
319 if (ka->sa.sa_flags & SA_RESTORER) {
320 return_ip = (unsigned long)ka->sa.sa_restorer;
321 } else {
322 /* trampoline - the desired return ip is the retcode itself */
323 return_ip = (unsigned long)&frame->retcode;
324 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
Jesper Nilssona4858d42008-01-14 00:55:22 -0800325 err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
326 err |= __put_user(__NR_rt_sigreturn,
327 (short __user *)(frame->retcode+2));
328 err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
331 if (err)
332 goto give_sigsegv;
333
334 /* TODO what is the current->exec_domain stuff and invmap ? */
335
336 /* Set up registers for signal handler */
337
Jesper Nilssona4858d42008-01-14 00:55:22 -0800338 /* What we enter NOW */
339 regs->irp = (unsigned long) ka->sa.sa_handler;
340 /* What we enter LATER */
341 regs->srp = return_ip;
342 /* First argument is signo */
343 regs->r10 = sig;
344 /* Second argument is (siginfo_t *) */
345 regs->r11 = (unsigned long)&frame->info;
346 /* Third argument is unused */
347 regs->r12 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jesper Nilssona4858d42008-01-14 00:55:22 -0800349 /* Actually move the usp to reflect the stacked frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 wrusp((unsigned long)frame);
351
Jesper Nilssona4858d42008-01-14 00:55:22 -0800352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354give_sigsegv:
355 force_sigsegv(sig, current);
Jesper Nilssona4858d42008-01-14 00:55:22 -0800356 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359/*
360 * OK, we're invoking a handler
Jesper Nilssona4858d42008-01-14 00:55:22 -0800361 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Al Viroa610d6e2012-05-21 23:42:15 -0400363static inline void handle_signal(int canrestart, unsigned long sig,
Jesper Nilssona4858d42008-01-14 00:55:22 -0800364 siginfo_t *info, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400365 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Al Virob7f9a112012-05-02 09:59:21 -0400367 sigset_t *oldset = sigmask_to_save();
Jesper Nilssona4858d42008-01-14 00:55:22 -0800368 int ret;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Are we from a system call? */
371 if (canrestart) {
372 /* If so, check system call restarting.. */
373 switch (regs->r10) {
Jesper Nilssona4858d42008-01-14 00:55:22 -0800374 case -ERESTART_RESTARTBLOCK:
375 case -ERESTARTNOHAND:
376 /* ERESTARTNOHAND means that the syscall should
377 * only be restarted if there was no handler for
378 * the signal, and since we only get here if there
379 * is a handler, we don't restart */
380 regs->r10 = -EINTR;
381 break;
382 case -ERESTARTSYS:
383 /* ERESTARTSYS means to restart the syscall if
384 * there is no handler or the handler was
385 * registered with SA_RESTART */
386 if (!(ka->sa.sa_flags & SA_RESTART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 regs->r10 = -EINTR;
388 break;
Jesper Nilssona4858d42008-01-14 00:55:22 -0800389 }
390 /* fallthrough */
391 case -ERESTARTNOINTR:
392 /* ERESTARTNOINTR means that the syscall should
393 * be called again after the signal handler returns. */
394 RESTART_CRIS_SYS(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 }
397
398 /* Set up the stack frame */
399 if (ka->sa.sa_flags & SA_SIGINFO)
Jesper Nilssona4858d42008-01-14 00:55:22 -0800400 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 else
Jesper Nilssona4858d42008-01-14 00:55:22 -0800402 ret = setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Matt Flemingf3b5e822012-05-11 10:58:00 +1000404 if (ret == 0)
Al Viroefee9842012-04-28 02:04:15 -0400405 signal_delivered(sig, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408/*
409 * Note that 'init' is a special process: it doesn't get signals it doesn't
410 * want to handle. Thus you cannot kill init even with a SIGKILL even by
411 * mistake.
412 *
413 * Also note that the regs structure given here as an argument, is the latest
414 * pushed pt_regs. It may or may not be the same as the first pushed registers
415 * when the initial usermode->kernelmode transition took place. Therefore
416 * we can use user_mode(regs) to see if we came directly from kernel or user
417 * mode below.
418 */
419
Jesper Nilssona4858d42008-01-14 00:55:22 -0800420void do_signal(int canrestart, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 siginfo_t info;
423 int signr;
424 struct k_sigaction ka;
425
426 /*
427 * We want the common case to go fast, which
428 * is why we may in certain cases get here from
429 * kernel mode. Just return without doing anything
430 * if so.
431 */
432 if (!user_mode(regs))
Jesper Nilssona4858d42008-01-14 00:55:22 -0800433 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
436 if (signr > 0) {
437 /* Whee! Actually deliver the signal. */
Al Viroa610d6e2012-05-21 23:42:15 -0400438 handle_signal(canrestart, signr, &info, &ka, regs);
Jesper Nilssona4858d42008-01-14 00:55:22 -0800439 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 /* Did we come from a system call? */
443 if (canrestart) {
444 /* Restart the system call - no handlers present */
445 if (regs->r10 == -ERESTARTNOHAND ||
446 regs->r10 == -ERESTARTSYS ||
447 regs->r10 == -ERESTARTNOINTR) {
448 RESTART_CRIS_SYS(regs);
449 }
Jesper Nilssona4858d42008-01-14 00:55:22 -0800450 if (regs->r10 == -ERESTART_RESTARTBLOCK) {
Jesper Nilsson33dc0ad2011-03-21 16:07:05 +0100451 regs->r9 = __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 regs->irp -= 2;
453 }
454 }
Jesper Nilssona4858d42008-01-14 00:55:22 -0800455
456 /* if there's no signal to deliver, we just put the saved sigmask
457 * back */
Al Viro51a7b442012-05-21 23:33:55 -0400458 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}