Staging: Panel: panel: Fixed checkpatch line length warnings
[linux/fpc-iii.git] / arch / frv / kernel / signal.c
blobd822700d4f15dacf49d96c612bc1ea6ac65c3c4b
1 /* signal.c: FRV specific bits of signal handling
3 * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from arch/m68k/kernel/signal.c
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
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/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/personality.h>
23 #include <linux/tracehook.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
26 #include <asm/cacheflush.h>
28 #define DEBUG_SIG 0
30 struct fdpic_func_descriptor {
31 unsigned long text;
32 unsigned long GOT;
36 * Do a signal return; undo the signal stack.
39 struct sigframe
41 __sigrestore_t pretcode;
42 int sig;
43 struct sigcontext sc;
44 unsigned long extramask[_NSIG_WORDS-1];
45 uint32_t retcode[2];
48 struct rt_sigframe
50 __sigrestore_t pretcode;
51 int sig;
52 struct siginfo __user *pinfo;
53 void __user *puc;
54 struct siginfo info;
55 struct ucontext uc;
56 uint32_t retcode[2];
59 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
61 struct user_context *user = current->thread.user;
62 unsigned long tbr, psr;
64 /* Always make any pending restarted system calls return -EINTR */
65 current_thread_info()->restart_block.fn = do_no_restart_syscall;
67 tbr = user->i.tbr;
68 psr = user->i.psr;
69 if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
70 goto badframe;
71 user->i.tbr = tbr;
72 user->i.psr = psr;
74 restore_user_regs(user);
76 user->i.syscallno = -1; /* disable syscall checks */
78 *_gr8 = user->i.gr[8];
79 return 0;
81 badframe:
82 return 1;
85 asmlinkage int sys_sigreturn(void)
87 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
88 sigset_t set;
89 int gr8;
91 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
92 goto badframe;
93 if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
94 goto badframe;
96 if (_NSIG_WORDS > 1 &&
97 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
98 goto badframe;
100 set_current_blocked(&set);
102 if (restore_sigcontext(&frame->sc, &gr8))
103 goto badframe;
104 return gr8;
106 badframe:
107 force_sig(SIGSEGV, current);
108 return 0;
111 asmlinkage int sys_rt_sigreturn(void)
113 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
114 sigset_t set;
115 int gr8;
117 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
118 goto badframe;
119 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
120 goto badframe;
122 set_current_blocked(&set);
124 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
125 goto badframe;
127 if (restore_altstack(&frame->uc.uc_stack))
128 goto badframe;
130 return gr8;
132 badframe:
133 force_sig(SIGSEGV, current);
134 return 0;
138 * Set up a signal frame
140 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
142 save_user_regs(current->thread.user);
144 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
145 goto badframe;
147 /* non-iBCS2 extensions.. */
148 if (__put_user(mask, &sc->sc_oldmask) < 0)
149 goto badframe;
151 return 0;
153 badframe:
154 return 1;
157 /*****************************************************************************/
159 * Determine which stack to use..
161 static inline void __user *get_sigframe(struct k_sigaction *ka,
162 size_t frame_size)
164 unsigned long sp;
166 /* Default to using normal stack */
167 sp = __frame->sp;
169 /* This is the X/Open sanctioned signal stack switching. */
170 if (ka->sa.sa_flags & SA_ONSTACK) {
171 if (! sas_ss_flags(sp))
172 sp = current->sas_ss_sp + current->sas_ss_size;
175 return (void __user *) ((sp - frame_size) & ~7UL);
177 } /* end get_sigframe() */
179 /*****************************************************************************/
183 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
185 struct sigframe __user *frame;
186 int rsig;
188 set_fs(USER_DS);
190 frame = get_sigframe(ka, sizeof(*frame));
192 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
193 goto give_sigsegv;
195 rsig = sig;
196 if (sig < 32 &&
197 __current_thread_info->exec_domain &&
198 __current_thread_info->exec_domain->signal_invmap)
199 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
201 if (__put_user(rsig, &frame->sig) < 0)
202 goto give_sigsegv;
204 if (setup_sigcontext(&frame->sc, set->sig[0]))
205 goto give_sigsegv;
207 if (_NSIG_WORDS > 1) {
208 if (__copy_to_user(frame->extramask, &set->sig[1],
209 sizeof(frame->extramask)))
210 goto give_sigsegv;
213 /* Set up to return from userspace. If provided, use a stub
214 * already in userspace. */
215 if (ka->sa.sa_flags & SA_RESTORER) {
216 if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
217 goto give_sigsegv;
219 else {
220 /* Set up the following code on the stack:
221 * setlos #__NR_sigreturn,gr7
222 * tira gr0,0
224 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
225 __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
226 __put_user(0xc0700000, &frame->retcode[1]))
227 goto give_sigsegv;
229 flush_icache_range((unsigned long) frame->retcode,
230 (unsigned long) (frame->retcode + 2));
233 /* Set up registers for the signal handler */
234 if (current->personality & FDPIC_FUNCPTRS) {
235 struct fdpic_func_descriptor __user *funcptr =
236 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
237 struct fdpic_func_descriptor desc;
238 if (copy_from_user(&desc, funcptr, sizeof(desc)))
239 goto give_sigsegv;
240 __frame->pc = desc.text;
241 __frame->gr15 = desc.GOT;
242 } else {
243 __frame->pc = (unsigned long) ka->sa.sa_handler;
244 __frame->gr15 = 0;
247 __frame->sp = (unsigned long) frame;
248 __frame->lr = (unsigned long) &frame->retcode;
249 __frame->gr8 = sig;
251 #if DEBUG_SIG
252 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
253 sig, current->comm, current->pid, frame, __frame->pc,
254 frame->pretcode);
255 #endif
257 return 0;
259 give_sigsegv:
260 force_sigsegv(sig, current);
261 return -EFAULT;
263 } /* end setup_frame() */
265 /*****************************************************************************/
269 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
270 sigset_t *set)
272 struct rt_sigframe __user *frame;
273 int rsig;
275 set_fs(USER_DS);
277 frame = get_sigframe(ka, sizeof(*frame));
279 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
280 goto give_sigsegv;
282 rsig = sig;
283 if (sig < 32 &&
284 __current_thread_info->exec_domain &&
285 __current_thread_info->exec_domain->signal_invmap)
286 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
288 if (__put_user(rsig, &frame->sig) ||
289 __put_user(&frame->info, &frame->pinfo) ||
290 __put_user(&frame->uc, &frame->puc))
291 goto give_sigsegv;
293 if (copy_siginfo_to_user(&frame->info, info))
294 goto give_sigsegv;
296 /* Create the ucontext. */
297 if (__put_user(0, &frame->uc.uc_flags) ||
298 __put_user(NULL, &frame->uc.uc_link) ||
299 __save_altstack(&frame->uc.uc_stack, __frame->sp))
300 goto give_sigsegv;
302 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
303 goto give_sigsegv;
305 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
306 goto give_sigsegv;
308 /* Set up to return from userspace. If provided, use a stub
309 * already in userspace. */
310 if (ka->sa.sa_flags & SA_RESTORER) {
311 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
312 goto give_sigsegv;
314 else {
315 /* Set up the following code on the stack:
316 * setlos #__NR_sigreturn,gr7
317 * tira gr0,0
319 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
320 __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
321 __put_user(0xc0700000, &frame->retcode[1]))
322 goto give_sigsegv;
324 flush_icache_range((unsigned long) frame->retcode,
325 (unsigned long) (frame->retcode + 2));
328 /* Set up registers for signal handler */
329 if (current->personality & FDPIC_FUNCPTRS) {
330 struct fdpic_func_descriptor __user *funcptr =
331 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
332 struct fdpic_func_descriptor desc;
333 if (copy_from_user(&desc, funcptr, sizeof(desc)))
334 goto give_sigsegv;
335 __frame->pc = desc.text;
336 __frame->gr15 = desc.GOT;
337 } else {
338 __frame->pc = (unsigned long) ka->sa.sa_handler;
339 __frame->gr15 = 0;
342 __frame->sp = (unsigned long) frame;
343 __frame->lr = (unsigned long) &frame->retcode;
344 __frame->gr8 = sig;
345 __frame->gr9 = (unsigned long) &frame->info;
347 #if DEBUG_SIG
348 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
349 sig, current->comm, current->pid, frame, __frame->pc,
350 frame->pretcode);
351 #endif
353 return 0;
355 give_sigsegv:
356 force_sigsegv(sig, current);
357 return -EFAULT;
359 } /* end setup_rt_frame() */
361 /*****************************************************************************/
363 * OK, we're invoking a handler
365 static void handle_signal(unsigned long sig, siginfo_t *info,
366 struct k_sigaction *ka)
368 sigset_t *oldset = sigmask_to_save();
369 int ret;
371 /* Are we from a system call? */
372 if (__frame->syscallno != -1) {
373 /* If so, check system call restarting.. */
374 switch (__frame->gr8) {
375 case -ERESTART_RESTARTBLOCK:
376 case -ERESTARTNOHAND:
377 __frame->gr8 = -EINTR;
378 break;
380 case -ERESTARTSYS:
381 if (!(ka->sa.sa_flags & SA_RESTART)) {
382 __frame->gr8 = -EINTR;
383 break;
386 /* fallthrough */
387 case -ERESTARTNOINTR:
388 __frame->gr8 = __frame->orig_gr8;
389 __frame->pc -= 4;
391 __frame->syscallno = -1;
394 /* Set up the stack frame */
395 if (ka->sa.sa_flags & SA_SIGINFO)
396 ret = setup_rt_frame(sig, ka, info, oldset);
397 else
398 ret = setup_frame(sig, ka, oldset);
400 if (ret)
401 return;
403 signal_delivered(sig, info, ka, __frame,
404 test_thread_flag(TIF_SINGLESTEP));
405 } /* end handle_signal() */
407 /*****************************************************************************/
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.
413 static void do_signal(void)
415 struct k_sigaction ka;
416 siginfo_t info;
417 int signr;
419 signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
420 if (signr > 0) {
421 handle_signal(signr, &info, &ka);
422 return;
425 /* Did we come from a system call? */
426 if (__frame->syscallno != -1) {
427 /* Restart the system call - no handlers present */
428 switch (__frame->gr8) {
429 case -ERESTARTNOHAND:
430 case -ERESTARTSYS:
431 case -ERESTARTNOINTR:
432 __frame->gr8 = __frame->orig_gr8;
433 __frame->pc -= 4;
434 break;
436 case -ERESTART_RESTARTBLOCK:
437 __frame->gr7 = __NR_restart_syscall;
438 __frame->pc -= 4;
439 break;
441 __frame->syscallno = -1;
444 /* if there's no signal to deliver, we just put the saved sigmask
445 * back */
446 restore_saved_sigmask();
447 } /* end do_signal() */
449 /*****************************************************************************/
451 * notification of userspace execution resumption
452 * - triggered by the TIF_WORK_MASK flags
454 asmlinkage void do_notify_resume(__u32 thread_info_flags)
456 /* pending single-step? */
457 if (thread_info_flags & _TIF_SINGLESTEP)
458 clear_thread_flag(TIF_SINGLESTEP);
460 /* deal with pending signal delivery */
461 if (thread_info_flags & _TIF_SIGPENDING)
462 do_signal();
464 /* deal with notification on about to resume userspace execution */
465 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
466 clear_thread_flag(TIF_NOTIFY_RESUME);
467 tracehook_notify_resume(__frame);
470 } /* end do_notify_resume() */