blob: 6e3c26a1607cdb6b505e4b25cab682a08ed04605 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/m32r/kernel/signal.c
3 *
4 * Copyright (c) 2003 Hitoshi Yamamoto
5 *
6 * Taken from i386 version.
7 * Copyright (C) 1991, 1992 Linus Torvalds
8 *
9 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
10 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
14#include <linux/mm.h>
15#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/signal.h>
18#include <linux/errno.h>
19#include <linux/wait.h>
20#include <linux/unistd.h>
21#include <linux/stddef.h>
22#include <linux/personality.h>
David Howells733e5e42009-09-09 08:30:21 +010023#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/cacheflush.h>
25#include <asm/ucontext.h>
26#include <asm/uaccess.h>
27
28#define DEBUG_SIG 0
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030asmlinkage int
31sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
32 unsigned long r2, unsigned long r3, unsigned long r4,
Hirokazu Takata6ced13c2006-02-24 13:03:51 -080033 unsigned long r5, unsigned long r6, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Hirokazu Takata6ced13c2006-02-24 13:03:51 -080035 return do_sigaltstack(uss, uoss, regs->spu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38
39/*
40 * Do a signal return; undo the signal stack.
41 */
42
43struct rt_sigframe
44{
45 int sig;
Al Viro870e75a2006-10-11 17:24:45 +010046 struct siginfo __user *pinfo;
47 void __user *puc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct siginfo info;
49 struct ucontext uc;
50// struct _fpstate fpstate;
51};
52
53static int
54restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
55 int *r0_p)
56{
57 unsigned int err = 0;
58
59 /* Always make any pending restarted system calls return -EINTR */
60 current_thread_info()->restart_block.fn = do_no_restart_syscall;
61
62#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
63 COPY(r4);
64 COPY(r5);
65 COPY(r6);
66 COPY(pt_regs);
67 /* COPY(r0); Skip r0 */
68 COPY(r1);
69 COPY(r2);
70 COPY(r3);
71 COPY(r7);
72 COPY(r8);
73 COPY(r9);
74 COPY(r10);
75 COPY(r11);
76 COPY(r12);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 COPY(acc0h);
78 COPY(acc0l);
Hirokazu Takata9674dcf2007-02-10 01:43:35 -080079 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
80 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 COPY(psw);
82 COPY(bpc);
83 COPY(bbpsw);
84 COPY(bbpc);
85 COPY(spu);
86 COPY(fp);
87 COPY(lr);
88 COPY(spi);
89#undef COPY
90
91 regs->syscall_nr = -1; /* disable syscall checks */
92 err |= __get_user(*r0_p, &sc->sc_r0);
93
94 return err;
95}
96
97asmlinkage int
98sys_rt_sigreturn(unsigned long r0, unsigned long r1,
99 unsigned long r2, unsigned long r3, unsigned long r4,
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800100 unsigned long r5, unsigned long r6, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800102 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int result;
105
106 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
107 goto badframe;
108 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
109 goto badframe;
110
Matt Fleming3883e302012-05-11 10:57:59 +1000111 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800113 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 goto badframe;
115
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800116 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->spu) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 return result;
120
121badframe:
122 force_sig(SIGSEGV, current);
123 return 0;
124}
125
126/*
127 * Set up a signal frame.
128 */
129
130static int
131setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
132 unsigned long mask)
133{
134 int err = 0;
135
136#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
137 COPY(r4);
138 COPY(r5);
139 COPY(r6);
140 COPY(pt_regs);
141 COPY(r0);
142 COPY(r1);
143 COPY(r2);
144 COPY(r3);
145 COPY(r7);
146 COPY(r8);
147 COPY(r9);
148 COPY(r10);
149 COPY(r11);
150 COPY(r12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 COPY(acc0h);
152 COPY(acc0l);
Hirokazu Takata9674dcf2007-02-10 01:43:35 -0800153 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
154 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 COPY(psw);
156 COPY(bpc);
157 COPY(bbpsw);
158 COPY(bbpc);
159 COPY(spu);
160 COPY(fp);
161 COPY(lr);
162 COPY(spi);
163#undef COPY
164
165 err |= __put_user(mask, &sc->oldmask);
166
167 return err;
168}
169
170/*
171 * Determine which stack to use..
172 */
173static inline void __user *
174get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
175{
176 /* This is the X/Open sanctioned signal stack switching. */
177 if (ka->sa.sa_flags & SA_ONSTACK) {
178 if (sas_ss_flags(sp) == 0)
179 sp = current->sas_ss_sp + current->sas_ss_size;
180 }
181
182 return (void __user *)((sp - frame_size) & -8ul);
183}
184
Al Viroa05c4e12010-09-24 06:23:57 +0100185static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 sigset_t *set, struct pt_regs *regs)
187{
188 struct rt_sigframe __user *frame;
189 int err = 0;
190 int signal;
191
192 frame = get_sigframe(ka, regs->spu, sizeof(*frame));
193
194 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
195 goto give_sigsegv;
196
197 signal = current_thread_info()->exec_domain
198 && current_thread_info()->exec_domain->signal_invmap
199 && sig < 32
200 ? current_thread_info()->exec_domain->signal_invmap[sig]
201 : sig;
202
203 err |= __put_user(signal, &frame->sig);
204 if (err)
205 goto give_sigsegv;
206
207 err |= __put_user(&frame->info, &frame->pinfo);
208 err |= __put_user(&frame->uc, &frame->puc);
209 err |= copy_siginfo_to_user(&frame->info, info);
210 if (err)
211 goto give_sigsegv;
212
213 /* Create the ucontext. */
214 err |= __put_user(0, &frame->uc.uc_flags);
215 err |= __put_user(0, &frame->uc.uc_link);
216 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
217 err |= __put_user(sas_ss_flags(regs->spu),
218 &frame->uc.uc_stack.ss_flags);
219 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
220 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
221 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
222 if (err)
223 goto give_sigsegv;
224
225 /* Set up to return from userspace. */
226 regs->lr = (unsigned long)ka->sa.sa_restorer;
227
228 /* Set up registers for signal handler */
229 regs->spu = (unsigned long)frame;
230 regs->r0 = signal; /* Arg for signal handler */
231 regs->r1 = (unsigned long)&frame->info;
232 regs->r2 = (unsigned long)&frame->uc;
233 regs->bpc = (unsigned long)ka->sa.sa_handler;
234
235 set_fs(USER_DS);
236
237#if DEBUG_SIG
238 printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
239 current->comm, current->pid, frame, regs->pc);
240#endif
241
Al Viroa05c4e12010-09-24 06:23:57 +0100242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244give_sigsegv:
245 force_sigsegv(sig, current);
Al Viroa05c4e12010-09-24 06:23:57 +0100246 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Al Virobb9c8612010-09-24 06:24:53 +0100249static int prev_insn(struct pt_regs *regs)
250{
251 u16 inst;
Kyle McMartin388d1482010-10-15 21:17:09 -0400252 if (get_user(inst, (u16 __user *)(regs->bpc - 2)))
Al Virobb9c8612010-09-24 06:24:53 +0100253 return -EFAULT;
254 if ((inst & 0xfff0) == 0x10f0) /* trap ? */
255 regs->bpc -= 2;
256 else
257 regs->bpc -= 4;
258 regs->syscall_nr = -1;
259 return 0;
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/*
263 * OK, we're invoking a handler
264 */
265
Al Viroa610d6e2012-05-21 23:42:15 -0400266static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
Al Virob7f9a112012-05-02 09:59:21 -0400268 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* Are we from a system call? */
271 if (regs->syscall_nr >= 0) {
272 /* If so, check system call restarting.. */
273 switch (regs->r0) {
274 case -ERESTART_RESTARTBLOCK:
275 case -ERESTARTNOHAND:
276 regs->r0 = -EINTR;
277 break;
278
279 case -ERESTARTSYS:
280 if (!(ka->sa.sa_flags & SA_RESTART)) {
281 regs->r0 = -EINTR;
282 break;
283 }
284 /* fallthrough */
285 case -ERESTARTNOINTR:
286 regs->r0 = regs->orig_r0;
Al Virobb9c8612010-09-24 06:24:53 +0100287 if (prev_insn(regs) < 0)
Geert Uytterhoevenf9717f32012-07-17 15:47:59 -0700288 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 }
291
292 /* Set up the stack frame */
Al Virob7f9a112012-05-02 09:59:21 -0400293 if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs))
Al Viroa610d6e2012-05-21 23:42:15 -0400294 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Al Viroefee9842012-04-28 02:04:15 -0400296 signal_delivered(sig, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299/*
300 * Note that 'init' is a special process: it doesn't get signals it doesn't
301 * want to handle. Thus you cannot kill init even with a SIGKILL even by
302 * mistake.
303 */
Al Viroa05c4e12010-09-24 06:23:57 +0100304static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 siginfo_t info;
307 int signr;
308 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /*
311 * We want the common case to go fast, which
312 * is why we may in certain cases get here from
313 * kernel mode. Just return without doing anything
314 * if so.
315 */
316 if (!user_mode(regs))
Al Viroa05c4e12010-09-24 06:23:57 +0100317 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
320 if (signr > 0) {
Simon Arlott5aa8b6c2007-10-20 01:14:39 +0200321 /* Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * signal to user space. The processor register will
323 * have been cleared if the watchpoint triggered
324 * inside the kernel.
325 */
326
327 /* Whee! Actually deliver the signal. */
Al Viroa610d6e2012-05-21 23:42:15 -0400328 handle_signal(signr, &ka, &info, regs);
Al Viroa05c4e12010-09-24 06:23:57 +0100329
330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Did we come from a system call? */
334 if (regs->syscall_nr >= 0) {
335 /* Restart the system call - no handlers present */
336 if (regs->r0 == -ERESTARTNOHAND ||
337 regs->r0 == -ERESTARTSYS ||
338 regs->r0 == -ERESTARTNOINTR) {
339 regs->r0 = regs->orig_r0;
Al Virobb9c8612010-09-24 06:24:53 +0100340 prev_insn(regs);
Al Viroa7481022010-09-24 06:22:30 +0100341 } else if (regs->r0 == -ERESTART_RESTARTBLOCK){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 regs->r0 = regs->orig_r0;
343 regs->r7 = __NR_restart_syscall;
Al Virobb9c8612010-09-24 06:24:53 +0100344 prev_insn(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 }
Al Viro51a7b442012-05-21 23:33:55 -0400347 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/*
351 * notification of userspace execution resumption
352 * - triggered by current->work.notify_resume
353 */
Al Viroa7f83882010-09-24 06:20:35 +0100354void do_notify_resume(struct pt_regs *regs, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 /* Pending single-step? */
357 if (thread_info_flags & _TIF_SINGLESTEP)
358 clear_thread_flag(TIF_SINGLESTEP);
359
360 /* deal with pending signal delivery */
361 if (thread_info_flags & _TIF_SIGPENDING)
Al Viroa7f83882010-09-24 06:20:35 +0100362 do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
David Howellsd0420c82009-09-02 09:14:16 +0100364 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
365 clear_thread_flag(TIF_NOTIFY_RESUME);
366 tracehook_notify_resume(regs);
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}