KVM: Prevent kvm_init from corrupting debugfs structures
[linux-2.6/verdex.git] / arch / microblaze / kernel / signal.c
blob1c80e4fc40cef1fc0513951c449452ff23139c96
1 /*
2 * Signal handling
4 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2008-2009 PetaLogix
6 * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au>
7 * Copyright (C) 2001 NEC Corporation
8 * Copyright (C) 2001 Miles Bader <miles@gnu.org>
9 * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima
10 * Copyright (C) 1991,1992 Linus Torvalds
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 * This file was was derived from the sh version, arch/sh/kernel/signal.c
16 * This file is subject to the terms and conditions of the GNU General
17 * Public License. See the file COPYING in the main directory of this
18 * archive for more details.
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/kernel.h>
25 #include <linux/signal.h>
26 #include <linux/errno.h>
27 #include <linux/wait.h>
28 #include <linux/ptrace.h>
29 #include <linux/unistd.h>
30 #include <linux/stddef.h>
31 #include <linux/personality.h>
32 #include <linux/percpu.h>
33 #include <linux/linkage.h>
34 #include <asm/entry.h>
35 #include <asm/ucontext.h>
36 #include <linux/uaccess.h>
37 #include <asm/pgtable.h>
38 #include <asm/pgalloc.h>
39 #include <linux/syscalls.h>
40 #include <asm/cacheflush.h>
41 #include <asm/syscalls.h>
43 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
45 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_sycall);
48 asmlinkage long
49 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
50 struct pt_regs *regs)
52 return do_sigaltstack(uss, uoss, regs->r1);
56 * Do a signal return; undo the signal stack.
58 struct sigframe {
59 struct sigcontext sc;
60 unsigned long extramask[_NSIG_WORDS-1];
61 unsigned long tramp[2]; /* signal trampoline */
64 struct rt_sigframe {
65 struct siginfo info;
66 struct ucontext uc;
67 unsigned long tramp[2]; /* signal trampoline */
70 static int restore_sigcontext(struct pt_regs *regs,
71 struct sigcontext __user *sc, int *rval_p)
73 unsigned int err = 0;
75 #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
76 COPY(r0);
77 COPY(r1);
78 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
79 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
80 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
81 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
82 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
83 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
84 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
85 COPY(r30); COPY(r31);
86 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
87 #undef COPY
89 *rval_p = regs->r3;
91 return err;
94 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
96 struct rt_sigframe __user *frame =
97 (struct rt_sigframe __user *)(regs->r1 + STATE_SAVE_ARG_SPACE);
99 sigset_t set;
100 int rval;
102 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
103 goto badframe;
105 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
106 goto badframe;
108 sigdelsetmask(&set, ~_BLOCKABLE);
109 spin_lock_irq(&current->sighand->siglock);
110 current->blocked = set;
111 recalc_sigpending();
112 spin_unlock_irq(&current->sighand->siglock);
114 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
115 goto badframe;
117 /* It is more difficult to avoid calling this function than to
118 call it and ignore errors. */
119 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->r1))
120 goto badframe;
122 return rval;
124 badframe:
125 force_sig(SIGSEGV, current);
126 return 0;
130 * Set up a signal frame.
133 static int
134 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
135 unsigned long mask)
137 int err = 0;
139 #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
140 COPY(r0);
141 COPY(r1);
142 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
143 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
144 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
145 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
146 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
147 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
148 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
149 COPY(r30); COPY(r31);
150 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
151 #undef COPY
153 err |= __put_user(mask, &sc->oldmask);
155 return err;
159 * Determine which stack to use..
161 static inline void __user *
162 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
164 /* Default to using normal stack */
165 unsigned long sp = regs->r1;
167 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
168 sp = current->sas_ss_sp + current->sas_ss_size;
170 return (void __user *)((sp - frame_size) & -8UL);
173 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
174 sigset_t *set, struct pt_regs *regs)
176 struct rt_sigframe __user *frame;
177 int err = 0;
178 int signal;
180 frame = get_sigframe(ka, regs, sizeof(*frame));
182 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
183 goto give_sigsegv;
185 signal = current_thread_info()->exec_domain
186 && current_thread_info()->exec_domain->signal_invmap
187 && sig < 32
188 ? current_thread_info()->exec_domain->signal_invmap[sig]
189 : sig;
191 if (info)
192 err |= copy_siginfo_to_user(&frame->info, info);
194 /* Create the ucontext. */
195 err |= __put_user(0, &frame->uc.uc_flags);
196 err |= __put_user(0, &frame->uc.uc_link);
197 err |= __put_user((void *)current->sas_ss_sp,
198 &frame->uc.uc_stack.ss_sp);
199 err |= __put_user(sas_ss_flags(regs->r1),
200 &frame->uc.uc_stack.ss_flags);
201 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
202 err |= setup_sigcontext(&frame->uc.uc_mcontext,
203 regs, set->sig[0]);
204 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
206 /* Set up to return from userspace. If provided, use a stub
207 already in userspace. */
208 /* minus 8 is offset to cater for "rtsd r15,8" */
209 /* addi r12, r0, __NR_sigreturn */
210 err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
211 frame->tramp + 0);
212 /* brki r14, 0x8 */
213 err |= __put_user(0xb9cc0008, frame->tramp + 1);
215 /* Return from sighandler will jump to the tramp.
216 Negative 8 offset because return is rtsd r15, 8 */
217 regs->r15 = ((unsigned long)frame->tramp)-8;
219 __invalidate_cache_sigtramp((unsigned long)frame->tramp);
221 if (err)
222 goto give_sigsegv;
224 /* Set up registers for signal handler */
225 regs->r1 = (unsigned long) frame - STATE_SAVE_ARG_SPACE;
227 /* Signal handler args: */
228 regs->r5 = signal; /* arg 0: signum */
229 regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
230 regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
231 /* Offset to handle microblaze rtid r14, 0 */
232 regs->pc = (unsigned long)ka->sa.sa_handler;
234 set_fs(USER_DS);
236 #ifdef DEBUG_SIG
237 printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
238 current->comm, current->pid, frame, regs->pc);
239 #endif
241 return;
243 give_sigsegv:
244 if (sig == SIGSEGV)
245 ka->sa.sa_handler = SIG_DFL;
246 force_sig(SIGSEGV, current);
249 /* Handle restarting system calls */
250 static inline void
251 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
253 switch (regs->r3) {
254 case -ERESTART_RESTARTBLOCK:
255 case -ERESTARTNOHAND:
256 if (!has_handler)
257 goto do_restart;
258 regs->r3 = -EINTR;
259 break;
260 case -ERESTARTSYS:
261 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
262 regs->r3 = -EINTR;
263 break;
265 /* fallthrough */
266 case -ERESTARTNOINTR:
267 do_restart:
268 /* offset of 4 bytes to re-execute trap (brki) instruction */
269 #ifndef CONFIG_MMU
270 regs->pc -= 4;
271 #else
272 /* offset of 8 bytes required = 4 for rtbd
273 offset, plus 4 for size of
274 "brki r14,8"
275 instruction. */
276 regs->pc -= 8;
277 #endif
278 break;
283 * OK, we're invoking a handler
286 static int
287 handle_signal(unsigned long sig, struct k_sigaction *ka,
288 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
290 /* Set up the stack frame */
291 if (ka->sa.sa_flags & SA_SIGINFO)
292 setup_rt_frame(sig, ka, info, oldset, regs);
293 else
294 setup_rt_frame(sig, ka, NULL, oldset, regs);
296 if (ka->sa.sa_flags & SA_ONESHOT)
297 ka->sa.sa_handler = SIG_DFL;
299 if (!(ka->sa.sa_flags & SA_NODEFER)) {
300 spin_lock_irq(&current->sighand->siglock);
301 sigorsets(&current->blocked,
302 &current->blocked, &ka->sa.sa_mask);
303 sigaddset(&current->blocked, sig);
304 recalc_sigpending();
305 spin_unlock_irq(&current->sighand->siglock);
307 return 1;
311 * Note that 'init' is a special process: it doesn't get signals it doesn't
312 * want to handle. Thus you cannot kill init even with a SIGKILL even by
313 * mistake.
315 * Note that we go through the signals twice: once to check the signals that
316 * the kernel can handle, and then we build all the user-level signal handling
317 * stack-frames in one go after that.
319 int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_syscall)
321 siginfo_t info;
322 int signr;
323 struct k_sigaction ka;
324 #ifdef DEBUG_SIG
325 printk(KERN_INFO "do signal: %p %p %d\n", regs, oldset, in_syscall);
326 printk(KERN_INFO "do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
327 regs->r12, current_thread_info()->flags);
328 #endif
330 * We want the common case to go fast, which
331 * is why we may in certain cases get here from
332 * kernel mode. Just return without doing anything
333 * if so.
335 if (kernel_mode(regs))
336 return 1;
338 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
339 oldset = &current->saved_sigmask;
340 else
341 oldset = &current->blocked;
343 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
344 if (signr > 0) {
345 /* Whee! Actually deliver the signal. */
346 if (in_syscall)
347 handle_restart(regs, &ka, 1);
348 if (handle_signal(signr, &ka, &info, oldset, regs)) {
350 * A signal was successfully delivered; the saved
351 * sigmask will have been stored in the signal frame,
352 * and will be restored by sigreturn, so we can simply
353 * clear the TS_RESTORE_SIGMASK flag.
355 current_thread_info()->status &=
356 ~TS_RESTORE_SIGMASK;
358 return 1;
361 if (in_syscall)
362 handle_restart(regs, NULL, 0);
365 * If there's no signal to deliver, we just put the saved sigmask
366 * back.
368 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
369 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
370 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
373 /* Did we come from a system call? */
374 return 0;