This client driver allows you to use a GPIO pin as a source for PPS
[linux-2.6/next.git] / arch / microblaze / kernel / signal.c
blob99b970866b29a1de57583e85e18a050c1dc73ea3
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);
47 asmlinkage long
48 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
49 struct pt_regs *regs)
51 return do_sigaltstack(uss, uoss, regs->r1);
55 * Do a signal return; undo the signal stack.
57 struct sigframe {
58 struct sigcontext sc;
59 unsigned long extramask[_NSIG_WORDS-1];
60 unsigned long tramp[2]; /* signal trampoline */
63 struct rt_sigframe {
64 struct siginfo info;
65 struct ucontext uc;
66 unsigned long tramp[2]; /* signal trampoline */
69 static int restore_sigcontext(struct pt_regs *regs,
70 struct sigcontext __user *sc, int *rval_p)
72 unsigned int err = 0;
74 #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
75 COPY(r0);
76 COPY(r1);
77 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
78 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
79 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
80 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
81 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
82 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
83 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
84 COPY(r30); COPY(r31);
85 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
86 #undef COPY
88 *rval_p = regs->r3;
90 return err;
93 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
95 struct rt_sigframe __user *frame =
96 (struct rt_sigframe __user *)(regs->r1);
98 sigset_t set;
99 int rval;
101 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
102 goto badframe;
104 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
105 goto badframe;
107 sigdelsetmask(&set, ~_BLOCKABLE);
108 set_current_blocked(&set);
110 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
111 goto badframe;
113 /* It is more difficult to avoid calling this function than to
114 call it and ignore errors. */
115 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->r1))
116 goto badframe;
118 return rval;
120 badframe:
121 force_sig(SIGSEGV, current);
122 return 0;
126 * Set up a signal frame.
129 static int
130 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
131 unsigned long mask)
133 int err = 0;
135 #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
136 COPY(r0);
137 COPY(r1);
138 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
139 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
140 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
141 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
142 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
143 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
144 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
145 COPY(r30); COPY(r31);
146 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
147 #undef COPY
149 err |= __put_user(mask, &sc->oldmask);
151 return err;
155 * Determine which stack to use..
157 static inline void __user *
158 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
160 /* Default to using normal stack */
161 unsigned long sp = regs->r1;
163 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
164 sp = current->sas_ss_sp + current->sas_ss_size;
166 return (void __user *)((sp - frame_size) & -8UL);
169 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
170 sigset_t *set, struct pt_regs *regs)
172 struct rt_sigframe __user *frame;
173 int err = 0;
174 int signal;
175 unsigned long address = 0;
176 #ifdef CONFIG_MMU
177 pmd_t *pmdp;
178 pte_t *ptep;
179 #endif
181 frame = get_sigframe(ka, regs, sizeof(*frame));
183 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
184 goto give_sigsegv;
186 signal = current_thread_info()->exec_domain
187 && current_thread_info()->exec_domain->signal_invmap
188 && sig < 32
189 ? current_thread_info()->exec_domain->signal_invmap[sig]
190 : sig;
192 if (info)
193 err |= copy_siginfo_to_user(&frame->info, info);
195 /* Create the ucontext. */
196 err |= __put_user(0, &frame->uc.uc_flags);
197 err |= __put_user(NULL, &frame->uc.uc_link);
198 err |= __put_user((void __user *)current->sas_ss_sp,
199 &frame->uc.uc_stack.ss_sp);
200 err |= __put_user(sas_ss_flags(regs->r1),
201 &frame->uc.uc_stack.ss_flags);
202 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
203 err |= setup_sigcontext(&frame->uc.uc_mcontext,
204 regs, set->sig[0]);
205 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
207 /* Set up to return from userspace. If provided, use a stub
208 already in userspace. */
209 /* minus 8 is offset to cater for "rtsd r15,8" */
210 /* addi r12, r0, __NR_sigreturn */
211 err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
212 frame->tramp + 0);
213 /* brki r14, 0x8 */
214 err |= __put_user(0xb9cc0008, frame->tramp + 1);
216 /* Return from sighandler will jump to the tramp.
217 Negative 8 offset because return is rtsd r15, 8 */
218 regs->r15 = ((unsigned long)frame->tramp)-8;
220 address = ((unsigned long)frame->tramp);
221 #ifdef CONFIG_MMU
222 pmdp = pmd_offset(pud_offset(
223 pgd_offset(current->mm, address),
224 address), address);
226 preempt_disable();
227 ptep = pte_offset_map(pmdp, address);
228 if (pte_present(*ptep)) {
229 address = (unsigned long) page_address(pte_page(*ptep));
230 /* MS: I need add offset in page */
231 address += ((unsigned long)frame->tramp) & ~PAGE_MASK;
232 /* MS address is virtual */
233 address = virt_to_phys(address);
234 invalidate_icache_range(address, address + 8);
235 flush_dcache_range(address, address + 8);
237 pte_unmap(ptep);
238 preempt_enable();
239 #else
240 flush_icache_range(address, address + 8);
241 flush_dcache_range(address, address + 8);
242 #endif
243 if (err)
244 goto give_sigsegv;
246 /* Set up registers for signal handler */
247 regs->r1 = (unsigned long) frame;
249 /* Signal handler args: */
250 regs->r5 = signal; /* arg 0: signum */
251 regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
252 regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
253 /* Offset to handle microblaze rtid r14, 0 */
254 regs->pc = (unsigned long)ka->sa.sa_handler;
256 set_fs(USER_DS);
258 /* the tracer may want to single-step inside the handler */
259 if (test_thread_flag(TIF_SINGLESTEP))
260 ptrace_notify(SIGTRAP);
262 #ifdef DEBUG_SIG
263 printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
264 current->comm, current->pid, frame, regs->pc);
265 #endif
267 return 0;
269 give_sigsegv:
270 force_sigsegv(sig, current);
271 return -EFAULT;
274 /* Handle restarting system calls */
275 static inline void
276 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
278 switch (regs->r3) {
279 case -ERESTART_RESTARTBLOCK:
280 case -ERESTARTNOHAND:
281 if (!has_handler)
282 goto do_restart;
283 regs->r3 = -EINTR;
284 break;
285 case -ERESTARTSYS:
286 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
287 regs->r3 = -EINTR;
288 break;
290 /* fallthrough */
291 case -ERESTARTNOINTR:
292 do_restart:
293 /* offset of 4 bytes to re-execute trap (brki) instruction */
294 #ifndef CONFIG_MMU
295 regs->pc -= 4;
296 #else
297 /* offset of 8 bytes required = 4 for rtbd
298 offset, plus 4 for size of
299 "brki r14,8"
300 instruction. */
301 regs->pc -= 8;
302 #endif
303 break;
308 * OK, we're invoking a handler
311 static int
312 handle_signal(unsigned long sig, struct k_sigaction *ka,
313 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
315 int ret;
317 /* Set up the stack frame */
318 if (ka->sa.sa_flags & SA_SIGINFO)
319 ret = setup_rt_frame(sig, ka, info, oldset, regs);
320 else
321 ret = setup_rt_frame(sig, ka, NULL, oldset, regs);
323 if (ret)
324 return ret;
326 block_sigmask(ka, sig);
328 return 0;
332 * Note that 'init' is a special process: it doesn't get signals it doesn't
333 * want to handle. Thus you cannot kill init even with a SIGKILL even by
334 * mistake.
336 * Note that we go through the signals twice: once to check the signals that
337 * the kernel can handle, and then we build all the user-level signal handling
338 * stack-frames in one go after that.
340 int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_syscall)
342 siginfo_t info;
343 int signr;
344 struct k_sigaction ka;
345 #ifdef DEBUG_SIG
346 printk(KERN_INFO "do signal: %p %p %d\n", regs, oldset, in_syscall);
347 printk(KERN_INFO "do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
348 regs->r12, current_thread_info()->flags);
349 #endif
351 * We want the common case to go fast, which
352 * is why we may in certain cases get here from
353 * kernel mode. Just return without doing anything
354 * if so.
356 if (kernel_mode(regs))
357 return 1;
359 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
360 oldset = &current->saved_sigmask;
361 else
362 oldset = &current->blocked;
364 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
365 if (signr > 0) {
366 /* Whee! Actually deliver the signal. */
367 if (in_syscall)
368 handle_restart(regs, &ka, 1);
369 if (!handle_signal(signr, &ka, &info, oldset, regs)) {
371 * A signal was successfully delivered; the saved
372 * sigmask will have been stored in the signal frame,
373 * and will be restored by sigreturn, so we can simply
374 * clear the TS_RESTORE_SIGMASK flag.
376 current_thread_info()->status &=
377 ~TS_RESTORE_SIGMASK;
379 return 1;
382 if (in_syscall)
383 handle_restart(regs, NULL, 0);
386 * If there's no signal to deliver, we just put the saved sigmask
387 * back.
389 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
390 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
391 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
394 /* Did we come from a system call? */
395 return 0;