Merge branch 'for_3.8-rc1' into v4l_for_linus
[deliverable/linux.git] / arch / microblaze / kernel / signal.c
CommitLineData
2148daa9
MS
1/*
2 * Signal handling
3 *
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
11 *
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
13 *
14 * This file was was derived from the sh version, arch/sh/kernel/signal.c
15 *
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.
19 */
20
21#include <linux/sched.h>
22#include <linux/mm.h>
23#include <linux/smp.h>
2148daa9
MS
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>
969a9616 34#include <linux/tracehook.h>
2148daa9
MS
35#include <asm/entry.h>
36#include <asm/ucontext.h>
37#include <linux/uaccess.h>
38#include <asm/pgtable.h>
39#include <asm/pgalloc.h>
2148daa9
MS
40#include <linux/syscalls.h>
41#include <asm/cacheflush.h>
42#include <asm/syscalls.h>
43
e513588f 44asmlinkage long
2148daa9
MS
45sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
46 struct pt_regs *regs)
47{
48 return do_sigaltstack(uss, uoss, regs->r1);
49}
50
51/*
52 * Do a signal return; undo the signal stack.
53 */
2148daa9
MS
54struct sigframe {
55 struct sigcontext sc;
56 unsigned long extramask[_NSIG_WORDS-1];
57 unsigned long tramp[2]; /* signal trampoline */
58};
59
60struct rt_sigframe {
61 struct siginfo info;
62 struct ucontext uc;
63 unsigned long tramp[2]; /* signal trampoline */
64};
65
353b431b
AB
66static int restore_sigcontext(struct pt_regs *regs,
67 struct sigcontext __user *sc, int *rval_p)
2148daa9
MS
68{
69 unsigned int err = 0;
70
71#define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
72 COPY(r0);
73 COPY(r1);
74 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
75 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
76 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
77 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
78 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
79 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
80 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
81 COPY(r30); COPY(r31);
82 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
83#undef COPY
84
85 *rval_p = regs->r3;
86
87 return err;
88}
89
3183e068 90asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
2148daa9 91{
353b431b 92 struct rt_sigframe __user *frame =
6e83557c 93 (struct rt_sigframe __user *)(regs->r1);
2921e2bd 94
2148daa9 95 sigset_t set;
2148daa9
MS
96 int rval;
97
58e4257b
AV
98 /* Always make any pending restarted system calls return -EINTR */
99 current_thread_info()->restart_block.fn = do_no_restart_syscall;
100
2148daa9
MS
101 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
102 goto badframe;
103
104 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
105 goto badframe;
106
60c59751 107 set_current_blocked(&set);
2148daa9
MS
108
109 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
110 goto badframe;
111
2148daa9
MS
112 /* It is more difficult to avoid calling this function than to
113 call it and ignore errors. */
02232f8d 114 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->r1) == -EFAULT)
353b431b 115 goto badframe;
2148daa9
MS
116
117 return rval;
118
119badframe:
120 force_sig(SIGSEGV, current);
121 return 0;
122}
123
124/*
125 * Set up a signal frame.
126 */
127
128static int
353b431b 129setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
2148daa9
MS
130 unsigned long mask)
131{
132 int err = 0;
133
134#define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
135 COPY(r0);
136 COPY(r1);
137 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
138 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
139 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
140 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
141 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
142 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
143 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
144 COPY(r30); COPY(r31);
145 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
146#undef COPY
147
148 err |= __put_user(mask, &sc->oldmask);
149
150 return err;
151}
152
153/*
154 * Determine which stack to use..
155 */
353b431b 156static inline void __user *
2148daa9
MS
157get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
158{
159 /* Default to using normal stack */
160 unsigned long sp = regs->r1;
161
162 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
163 sp = current->sas_ss_sp + current->sas_ss_size;
164
353b431b 165 return (void __user *)((sp - frame_size) & -8UL);
2148daa9
MS
166}
167
bcb8c8d0 168static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
2148daa9
MS
169 sigset_t *set, struct pt_regs *regs)
170{
353b431b 171 struct rt_sigframe __user *frame;
2148daa9
MS
172 int err = 0;
173 int signal;
2ee2ff87
MS
174 unsigned long address = 0;
175#ifdef CONFIG_MMU
176 pmd_t *pmdp;
177 pte_t *ptep;
178#endif
2148daa9
MS
179
180 frame = get_sigframe(ka, regs, sizeof(*frame));
181
182 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
183 goto give_sigsegv;
184
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;
190
a6029d1c
MS
191 if (info)
192 err |= copy_siginfo_to_user(&frame->info, info);
2148daa9
MS
193
194 /* Create the ucontext. */
195 err |= __put_user(0, &frame->uc.uc_flags);
2cfedb97
MS
196 err |= __put_user(NULL, &frame->uc.uc_link);
197 err |= __put_user((void __user *)current->sas_ss_sp,
2148daa9
MS
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));
205
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" */
3183e068
AB
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);
214
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;
218
2ee2ff87
MS
219 address = ((unsigned long)frame->tramp);
220#ifdef CONFIG_MMU
221 pmdp = pmd_offset(pud_offset(
222 pgd_offset(current->mm, address),
223 address), address);
224
225 preempt_disable();
226 ptep = pte_offset_map(pmdp, address);
227 if (pte_present(*ptep)) {
228 address = (unsigned long) page_address(pte_page(*ptep));
229 /* MS: I need add offset in page */
230 address += ((unsigned long)frame->tramp) & ~PAGE_MASK;
231 /* MS address is virtual */
232 address = virt_to_phys(address);
233 invalidate_icache_range(address, address + 8);
234 flush_dcache_range(address, address + 8);
235 }
236 pte_unmap(ptep);
237 preempt_enable();
238#else
239 flush_icache_range(address, address + 8);
240 flush_dcache_range(address, address + 8);
241#endif
2148daa9
MS
242 if (err)
243 goto give_sigsegv;
244
245 /* Set up registers for signal handler */
6e83557c 246 regs->r1 = (unsigned long) frame;
2921e2bd 247
2148daa9
MS
248 /* Signal handler args: */
249 regs->r5 = signal; /* arg 0: signum */
250 regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
251 regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
252 /* Offset to handle microblaze rtid r14, 0 */
253 regs->pc = (unsigned long)ka->sa.sa_handler;
254
255 set_fs(USER_DS);
256
257#ifdef DEBUG_SIG
258 printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
259 current->comm, current->pid, frame, regs->pc);
260#endif
261
bcb8c8d0 262 return 0;
2148daa9
MS
263
264give_sigsegv:
de8f2ddd 265 force_sigsegv(sig, current);
bcb8c8d0 266 return -EFAULT;
2148daa9
MS
267}
268
269/* Handle restarting system calls */
270static inline void
271handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
272{
273 switch (regs->r3) {
274 case -ERESTART_RESTARTBLOCK:
275 case -ERESTARTNOHAND:
276 if (!has_handler)
277 goto do_restart;
278 regs->r3 = -EINTR;
279 break;
280 case -ERESTARTSYS:
281 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
282 regs->r3 = -EINTR;
283 break;
284 }
285 /* fallthrough */
286 case -ERESTARTNOINTR:
287do_restart:
288 /* offset of 4 bytes to re-execute trap (brki) instruction */
289 regs->pc -= 4;
290 break;
291 }
292}
293
294/*
295 * OK, we're invoking a handler
296 */
297
a610d6e6 298static void
2148daa9 299handle_signal(unsigned long sig, struct k_sigaction *ka,
b7f9a11a 300 siginfo_t *info, struct pt_regs *regs)
2148daa9 301{
b7f9a11a 302 sigset_t *oldset = sigmask_to_save();
bcb8c8d0
MF
303 int ret;
304
2148daa9
MS
305 /* Set up the stack frame */
306 if (ka->sa.sa_flags & SA_SIGINFO)
bcb8c8d0 307 ret = setup_rt_frame(sig, ka, info, oldset, regs);
2148daa9 308 else
bcb8c8d0
MF
309 ret = setup_rt_frame(sig, ka, NULL, oldset, regs);
310
311 if (ret)
a610d6e6 312 return;
2148daa9 313
7f1c6168
AV
314 signal_delivered(sig, info, ka, regs,
315 test_thread_flag(TIF_SINGLESTEP));
2148daa9
MS
316}
317
318/*
319 * Note that 'init' is a special process: it doesn't get signals it doesn't
320 * want to handle. Thus you cannot kill init even with a SIGKILL even by
321 * mistake.
322 *
323 * Note that we go through the signals twice: once to check the signals that
324 * the kernel can handle, and then we build all the user-level signal handling
325 * stack-frames in one go after that.
326 */
83140191 327static void do_signal(struct pt_regs *regs, int in_syscall)
2148daa9
MS
328{
329 siginfo_t info;
330 int signr;
331 struct k_sigaction ka;
332#ifdef DEBUG_SIG
83140191 333 printk(KERN_INFO "do signal: %p %d\n", regs, in_syscall);
2148daa9
MS
334 printk(KERN_INFO "do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
335 regs->r12, current_thread_info()->flags);
336#endif
2148daa9 337
2148daa9
MS
338 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
339 if (signr > 0) {
340 /* Whee! Actually deliver the signal. */
341 if (in_syscall)
342 handle_restart(regs, &ka, 1);
a610d6e6 343 handle_signal(signr, &ka, &info, regs);
83140191 344 return;
2148daa9
MS
345 }
346
347 if (in_syscall)
348 handle_restart(regs, NULL, 0);
349
3183e068
AB
350 /*
351 * If there's no signal to deliver, we just put the saved sigmask
352 * back.
353 */
51a7b448 354 restore_saved_sigmask();
2148daa9 355}
969a9616 356
83140191 357void do_notify_resume(struct pt_regs *regs, int in_syscall)
969a9616
AV
358{
359 /*
360 * We want the common case to go fast, which
361 * is why we may in certain cases get here from
362 * kernel mode. Just return without doing anything
363 * if so.
364 */
365 if (kernel_mode(regs))
366 return;
367
368 if (test_thread_flag(TIF_SIGPENDING))
83140191 369 do_signal(regs, in_syscall);
969a9616 370
a42c6ded 371 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
969a9616 372 tracehook_notify_resume(regs);
969a9616 373}
This page took 0.329175 seconds and 5 git commands to generate.