Linux 3.17-rc2
[linux/fpc-iii.git] / arch / m32r / kernel / signal.c
blob95408b8f130af6264ecf5d949de470a5557ef585
1 /*
2 * linux/arch/m32r/kernel/signal.c
4 * Copyright (c) 2003 Hitoshi Yamamoto
6 * Taken from i386 version.
7 * Copyright (C) 1991, 1992 Linus Torvalds
9 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
10 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #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>
23 #include <linux/tracehook.h>
24 #include <asm/cacheflush.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
28 #define DEBUG_SIG 0
31 * Do a signal return; undo the signal stack.
34 struct rt_sigframe
36 int sig;
37 struct siginfo __user *pinfo;
38 void __user *puc;
39 struct siginfo info;
40 struct ucontext uc;
41 // struct _fpstate fpstate;
44 static int
45 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
46 int *r0_p)
48 unsigned int err = 0;
50 /* Always make any pending restarted system calls return -EINTR */
51 current_thread_info()->restart_block.fn = do_no_restart_syscall;
53 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
54 COPY(r4);
55 COPY(r5);
56 COPY(r6);
57 COPY(pt_regs);
58 /* COPY(r0); Skip r0 */
59 COPY(r1);
60 COPY(r2);
61 COPY(r3);
62 COPY(r7);
63 COPY(r8);
64 COPY(r9);
65 COPY(r10);
66 COPY(r11);
67 COPY(r12);
68 COPY(acc0h);
69 COPY(acc0l);
70 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
71 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
72 COPY(psw);
73 COPY(bpc);
74 COPY(bbpsw);
75 COPY(bbpc);
76 COPY(spu);
77 COPY(fp);
78 COPY(lr);
79 COPY(spi);
80 #undef COPY
82 regs->syscall_nr = -1; /* disable syscall checks */
83 err |= __get_user(*r0_p, &sc->sc_r0);
85 return err;
88 asmlinkage int
89 sys_rt_sigreturn(unsigned long r0, unsigned long r1,
90 unsigned long r2, unsigned long r3, unsigned long r4,
91 unsigned long r5, unsigned long r6, struct pt_regs *regs)
93 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
94 sigset_t set;
95 int result;
97 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
98 goto badframe;
99 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
100 goto badframe;
102 set_current_blocked(&set);
104 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
105 goto badframe;
107 if (restore_altstack(&frame->uc.uc_stack))
108 goto badframe;
110 return result;
112 badframe:
113 force_sig(SIGSEGV, current);
114 return 0;
118 * Set up a signal frame.
121 static int
122 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
123 unsigned long mask)
125 int err = 0;
127 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
128 COPY(r4);
129 COPY(r5);
130 COPY(r6);
131 COPY(pt_regs);
132 COPY(r0);
133 COPY(r1);
134 COPY(r2);
135 COPY(r3);
136 COPY(r7);
137 COPY(r8);
138 COPY(r9);
139 COPY(r10);
140 COPY(r11);
141 COPY(r12);
142 COPY(acc0h);
143 COPY(acc0l);
144 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
145 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
146 COPY(psw);
147 COPY(bpc);
148 COPY(bbpsw);
149 COPY(bbpc);
150 COPY(spu);
151 COPY(fp);
152 COPY(lr);
153 COPY(spi);
154 #undef COPY
156 err |= __put_user(mask, &sc->oldmask);
158 return err;
162 * Determine which stack to use..
164 static inline void __user *
165 get_sigframe(struct ksignal *ksig, unsigned long sp, size_t frame_size)
167 return (void __user *)((sigsp(sp, ksig) - frame_size) & -8ul);
170 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
171 struct pt_regs *regs)
173 struct rt_sigframe __user *frame;
174 int err = 0;
175 int signal, sig = ksig->sig;
177 frame = get_sigframe(ksig, regs->spu, sizeof(*frame));
179 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
180 return -EFAULT;
182 signal = current_thread_info()->exec_domain
183 && current_thread_info()->exec_domain->signal_invmap
184 && sig < 32
185 ? current_thread_info()->exec_domain->signal_invmap[sig]
186 : sig;
188 err |= __put_user(signal, &frame->sig);
189 if (err)
190 return -EFAULT;
192 err |= __put_user(&frame->info, &frame->pinfo);
193 err |= __put_user(&frame->uc, &frame->puc);
194 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
195 if (err)
196 return -EFAULT;
198 /* Create the ucontext. */
199 err |= __put_user(0, &frame->uc.uc_flags);
200 err |= __put_user(0, &frame->uc.uc_link);
201 err |= __save_altstack(&frame->uc.uc_stack, regs->spu);
202 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
203 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
204 if (err)
205 return -EFAULT;
207 /* Set up to return from userspace. */
208 regs->lr = (unsigned long)ksig->ka.sa.sa_restorer;
210 /* Set up registers for signal handler */
211 regs->spu = (unsigned long)frame;
212 regs->r0 = signal; /* Arg for signal handler */
213 regs->r1 = (unsigned long)&frame->info;
214 regs->r2 = (unsigned long)&frame->uc;
215 regs->bpc = (unsigned long)ksig->ka.sa.sa_handler;
217 set_fs(USER_DS);
219 #if DEBUG_SIG
220 printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
221 current->comm, current->pid, frame, regs->pc);
222 #endif
224 return 0;
227 static int prev_insn(struct pt_regs *regs)
229 u16 inst;
230 if (get_user(inst, (u16 __user *)(regs->bpc - 2)))
231 return -EFAULT;
232 if ((inst & 0xfff0) == 0x10f0) /* trap ? */
233 regs->bpc -= 2;
234 else
235 regs->bpc -= 4;
236 regs->syscall_nr = -1;
237 return 0;
241 * OK, we're invoking a handler
244 static void
245 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
247 int ret;
249 /* Are we from a system call? */
250 if (regs->syscall_nr >= 0) {
251 /* If so, check system call restarting.. */
252 switch (regs->r0) {
253 case -ERESTART_RESTARTBLOCK:
254 case -ERESTARTNOHAND:
255 regs->r0 = -EINTR;
256 break;
258 case -ERESTARTSYS:
259 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
260 regs->r0 = -EINTR;
261 break;
263 /* fallthrough */
264 case -ERESTARTNOINTR:
265 regs->r0 = regs->orig_r0;
266 if (prev_insn(regs) < 0)
267 return;
271 /* Set up the stack frame */
272 ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
274 signal_setup_done(ret, ksig, 0);
278 * Note that 'init' is a special process: it doesn't get signals it doesn't
279 * want to handle. Thus you cannot kill init even with a SIGKILL even by
280 * mistake.
282 static void do_signal(struct pt_regs *regs)
284 struct ksignal ksig;
287 * We want the common case to go fast, which
288 * is why we may in certain cases get here from
289 * kernel mode. Just return without doing anything
290 * if so.
292 if (!user_mode(regs))
293 return;
295 if (get_signal(&ksig)) {
296 /* Re-enable any watchpoints before delivering the
297 * signal to user space. The processor register will
298 * have been cleared if the watchpoint triggered
299 * inside the kernel.
302 /* Whee! Actually deliver the signal. */
303 handle_signal(&ksig, regs);
305 return;
308 /* Did we come from a system call? */
309 if (regs->syscall_nr >= 0) {
310 /* Restart the system call - no handlers present */
311 if (regs->r0 == -ERESTARTNOHAND ||
312 regs->r0 == -ERESTARTSYS ||
313 regs->r0 == -ERESTARTNOINTR) {
314 regs->r0 = regs->orig_r0;
315 prev_insn(regs);
316 } else if (regs->r0 == -ERESTART_RESTARTBLOCK){
317 regs->r0 = regs->orig_r0;
318 regs->r7 = __NR_restart_syscall;
319 prev_insn(regs);
322 restore_saved_sigmask();
326 * notification of userspace execution resumption
327 * - triggered by current->work.notify_resume
329 void do_notify_resume(struct pt_regs *regs, __u32 thread_info_flags)
331 /* Pending single-step? */
332 if (thread_info_flags & _TIF_SINGLESTEP)
333 clear_thread_flag(TIF_SINGLESTEP);
335 /* deal with pending signal delivery */
336 if (thread_info_flags & _TIF_SIGPENDING)
337 do_signal(regs);
339 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
340 clear_thread_flag(TIF_NOTIFY_RESUME);
341 tracehook_notify_resume(regs);