[PATCH] VFS: Permit filesystem to perform statfs with a known root dentry
[deliverable/linux.git] / arch / mips / kernel / signal.c
CommitLineData
1da177e4
LT
1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1994 - 2000 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 */
10#include <linux/config.h>
02416dcf 11#include <linux/cache.h>
1da177e4
LT
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/personality.h>
15#include <linux/smp.h>
16#include <linux/smp_lock.h>
17#include <linux/kernel.h>
18#include <linux/signal.h>
19#include <linux/errno.h>
20#include <linux/wait.h>
21#include <linux/ptrace.h>
22#include <linux/unistd.h>
23#include <linux/compiler.h>
24
e50c0a8f 25#include <asm/abi.h>
1da177e4
LT
26#include <asm/asm.h>
27#include <linux/bitops.h>
28#include <asm/cacheflush.h>
29#include <asm/fpu.h>
30#include <asm/sim.h>
31#include <asm/uaccess.h>
32#include <asm/ucontext.h>
33#include <asm/cpu-features.h>
02416dcf 34#include <asm/war.h>
1da177e4
LT
35
36#include "signal-common.h"
37
38#define DEBUG_SIG 0
39
40#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41
1da177e4
LT
42/*
43 * Atomically swap in the new signal mask, and wait for a signal.
44 */
45
46#ifdef CONFIG_TRAD_SIGNALS
47save_static_function(sys_sigsuspend);
48__attribute_used__ noinline static int
49_sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
50{
7b3e2fc8 51 sigset_t newset;
fe00f943 52 sigset_t __user *uset;
1da177e4 53
fe00f943 54 uset = (sigset_t __user *) regs.regs[4];
1da177e4
LT
55 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
56 return -EFAULT;
57 sigdelsetmask(&newset, ~_BLOCKABLE);
58
59 spin_lock_irq(&current->sighand->siglock);
7b3e2fc8 60 current->saved_sigmask = current->blocked;
1da177e4
LT
61 current->blocked = newset;
62 recalc_sigpending();
63 spin_unlock_irq(&current->sighand->siglock);
64
7b3e2fc8
RB
65 current->state = TASK_INTERRUPTIBLE;
66 schedule();
67 set_thread_flag(TIF_RESTORE_SIGMASK);
68 return -ERESTARTNOHAND;
1da177e4
LT
69}
70#endif
71
72save_static_function(sys_rt_sigsuspend);
73__attribute_used__ noinline static int
74_sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
75{
7b3e2fc8 76 sigset_t newset;
fe00f943 77 sigset_t __user *unewset;
1da177e4
LT
78 size_t sigsetsize;
79
80 /* XXX Don't preclude handling different sized sigset_t's. */
81 sigsetsize = regs.regs[5];
82 if (sigsetsize != sizeof(sigset_t))
83 return -EINVAL;
84
fe00f943 85 unewset = (sigset_t __user *) regs.regs[4];
1da177e4
LT
86 if (copy_from_user(&newset, unewset, sizeof(newset)))
87 return -EFAULT;
88 sigdelsetmask(&newset, ~_BLOCKABLE);
89
90 spin_lock_irq(&current->sighand->siglock);
7b3e2fc8 91 current->saved_sigmask = current->blocked;
1da177e4
LT
92 current->blocked = newset;
93 recalc_sigpending();
94 spin_unlock_irq(&current->sighand->siglock);
95
7b3e2fc8
RB
96 current->state = TASK_INTERRUPTIBLE;
97 schedule();
98 set_thread_flag(TIF_RESTORE_SIGMASK);
99 return -ERESTARTNOHAND;
1da177e4
LT
100}
101
102#ifdef CONFIG_TRAD_SIGNALS
9c6031cc
AN
103asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
104 struct sigaction __user *oact)
1da177e4
LT
105{
106 struct k_sigaction new_ka, old_ka;
107 int ret;
108 int err = 0;
109
110 if (act) {
111 old_sigset_t mask;
112
113 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
114 return -EFAULT;
115 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
116 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
117 err |= __get_user(mask, &act->sa_mask.sig[0]);
118 if (err)
119 return -EFAULT;
120
121 siginitset(&new_ka.sa.sa_mask, mask);
122 }
123
124 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
125
126 if (!ret && oact) {
127 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
128 return -EFAULT;
129 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
130 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
131 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
132 err |= __put_user(0, &oact->sa_mask.sig[1]);
133 err |= __put_user(0, &oact->sa_mask.sig[2]);
134 err |= __put_user(0, &oact->sa_mask.sig[3]);
135 if (err)
136 return -EFAULT;
137 }
138
139 return ret;
140}
141#endif
142
143asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
144{
fe00f943
RB
145 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
146 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
1da177e4
LT
147 unsigned long usp = regs.regs[29];
148
149 return do_sigaltstack(uss, uoss, usp);
150}
151
02416dcf
RB
152/*
153 * Horribly complicated - with the bloody RM9000 workarounds enabled
154 * the signal trampolines is moving to the end of the structure so we can
155 * increase the alignment without breaking software compatibility.
156 */
1da177e4
LT
157#ifdef CONFIG_TRAD_SIGNALS
158struct sigframe {
159 u32 sf_ass[4]; /* argument save space for o32 */
02416dcf
RB
160#if ICACHE_REFILLS_WORKAROUND_WAR
161 u32 sf_pad[2];
162#else
163 u32 sf_code[2]; /* signal trampoline */
164#endif
165 struct sigcontext sf_sc;
1da177e4 166 sigset_t sf_mask;
02416dcf
RB
167#if ICACHE_REFILLS_WORKAROUND_WAR
168 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
169#endif
1da177e4
LT
170};
171#endif
172
173struct rt_sigframe {
174 u32 rs_ass[4]; /* argument save space for o32 */
02416dcf
RB
175#if ICACHE_REFILLS_WORKAROUND_WAR
176 u32 rs_pad[2];
177#else
178 u32 rs_code[2]; /* signal trampoline */
179#endif
180 struct siginfo rs_info;
1da177e4 181 struct ucontext rs_uc;
02416dcf
RB
182#if ICACHE_REFILLS_WORKAROUND_WAR
183 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
184#endif
1da177e4
LT
185};
186
187#ifdef CONFIG_TRAD_SIGNALS
188save_static_function(sys_sigreturn);
189__attribute_used__ noinline static void
190_sys_sigreturn(nabi_no_regargs struct pt_regs regs)
191{
9bbf28a3 192 struct sigframe __user *frame;
1da177e4
LT
193 sigset_t blocked;
194
9bbf28a3 195 frame = (struct sigframe __user *) regs.regs[29];
1da177e4
LT
196 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
197 goto badframe;
198 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
199 goto badframe;
200
201 sigdelsetmask(&blocked, ~_BLOCKABLE);
202 spin_lock_irq(&current->sighand->siglock);
203 current->blocked = blocked;
204 recalc_sigpending();
205 spin_unlock_irq(&current->sighand->siglock);
206
207 if (restore_sigcontext(&regs, &frame->sf_sc))
208 goto badframe;
209
210 /*
211 * Don't let your children do this ...
212 */
1da177e4
LT
213 __asm__ __volatile__(
214 "move\t$29, %0\n\t"
215 "j\tsyscall_exit"
216 :/* no outputs */
217 :"r" (&regs));
218 /* Unreached */
219
220badframe:
221 force_sig(SIGSEGV, current);
222}
e50c0a8f 223#endif /* CONFIG_TRAD_SIGNALS */
1da177e4
LT
224
225save_static_function(sys_rt_sigreturn);
226__attribute_used__ noinline static void
227_sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
228{
9bbf28a3 229 struct rt_sigframe __user *frame;
1da177e4
LT
230 sigset_t set;
231 stack_t st;
232
9bbf28a3 233 frame = (struct rt_sigframe __user *) regs.regs[29];
1da177e4
LT
234 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
235 goto badframe;
236 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
237 goto badframe;
238
239 sigdelsetmask(&set, ~_BLOCKABLE);
240 spin_lock_irq(&current->sighand->siglock);
241 current->blocked = set;
242 recalc_sigpending();
243 spin_unlock_irq(&current->sighand->siglock);
244
245 if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
246 goto badframe;
247
248 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
249 goto badframe;
250 /* It is more difficult to avoid calling this function than to
251 call it and ignore errors. */
9bbf28a3 252 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
1da177e4
LT
253
254 /*
255 * Don't let your children do this ...
256 */
257 __asm__ __volatile__(
258 "move\t$29, %0\n\t"
259 "j\tsyscall_exit"
260 :/* no outputs */
261 :"r" (&regs));
262 /* Unreached */
263
264badframe:
265 force_sig(SIGSEGV, current);
266}
267
268#ifdef CONFIG_TRAD_SIGNALS
129bc8f7 269int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
1da177e4
LT
270 int signr, sigset_t *set)
271{
9bbf28a3 272 struct sigframe __user *frame;
1da177e4
LT
273 int err = 0;
274
275 frame = get_sigframe(ka, regs, sizeof(*frame));
276 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
277 goto give_sigsegv;
278
02416dcf 279 install_sigtramp(frame->sf_code, __NR_sigreturn);
1da177e4
LT
280
281 err |= setup_sigcontext(regs, &frame->sf_sc);
282 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
283 if (err)
284 goto give_sigsegv;
285
286 /*
287 * Arguments to signal handler:
288 *
289 * a0 = signal number
290 * a1 = 0 (should be cause)
291 * a2 = pointer to struct sigcontext
292 *
293 * $25 and c0_epc point to the signal handler, $29 points to the
294 * struct sigframe.
295 */
296 regs->regs[ 4] = signr;
297 regs->regs[ 5] = 0;
298 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
299 regs->regs[29] = (unsigned long) frame;
300 regs->regs[31] = (unsigned long) frame->sf_code;
301 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
302
303#if DEBUG_SIG
304 printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
305 current->comm, current->pid,
306 frame, regs->cp0_epc, frame->regs[31]);
307#endif
7b3e2fc8 308 return 0;
1da177e4
LT
309
310give_sigsegv:
311 force_sigsegv(signr, current);
7b3e2fc8 312 return -EFAULT;
1da177e4
LT
313}
314#endif
315
129bc8f7 316int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
1da177e4
LT
317 int signr, sigset_t *set, siginfo_t *info)
318{
9bbf28a3 319 struct rt_sigframe __user *frame;
1da177e4
LT
320 int err = 0;
321
322 frame = get_sigframe(ka, regs, sizeof(*frame));
323 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
324 goto give_sigsegv;
325
02416dcf 326 install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
1da177e4
LT
327
328 /* Create siginfo. */
329 err |= copy_siginfo_to_user(&frame->rs_info, info);
330
331 /* Create the ucontext. */
332 err |= __put_user(0, &frame->rs_uc.uc_flags);
5665a0ac 333 err |= __put_user(NULL, &frame->rs_uc.uc_link);
9c6031cc 334 err |= __put_user((void __user *)current->sas_ss_sp,
1da177e4
LT
335 &frame->rs_uc.uc_stack.ss_sp);
336 err |= __put_user(sas_ss_flags(regs->regs[29]),
337 &frame->rs_uc.uc_stack.ss_flags);
338 err |= __put_user(current->sas_ss_size,
339 &frame->rs_uc.uc_stack.ss_size);
340 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
341 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
342
343 if (err)
344 goto give_sigsegv;
345
346 /*
347 * Arguments to signal handler:
348 *
349 * a0 = signal number
350 * a1 = 0 (should be cause)
351 * a2 = pointer to ucontext
352 *
353 * $25 and c0_epc point to the signal handler, $29 points to
354 * the struct rt_sigframe.
355 */
356 regs->regs[ 4] = signr;
357 regs->regs[ 5] = (unsigned long) &frame->rs_info;
358 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
359 regs->regs[29] = (unsigned long) frame;
360 regs->regs[31] = (unsigned long) frame->rs_code;
361 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
362
363#if DEBUG_SIG
364 printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
365 current->comm, current->pid,
366 frame, regs->cp0_epc, regs->regs[31]);
367#endif
7b3e2fc8 368 return 0;
1da177e4
LT
369
370give_sigsegv:
371 force_sigsegv(signr, current);
7b3e2fc8 372 return -EFAULT;
1da177e4
LT
373}
374
129bc8f7 375static inline int handle_signal(unsigned long sig, siginfo_t *info,
1da177e4
LT
376 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
377{
129bc8f7
RB
378 int ret;
379
1da177e4
LT
380 switch(regs->regs[0]) {
381 case ERESTART_RESTARTBLOCK:
382 case ERESTARTNOHAND:
383 regs->regs[2] = EINTR;
384 break;
385 case ERESTARTSYS:
7b3e2fc8 386 if (!(ka->sa.sa_flags & SA_RESTART)) {
1da177e4
LT
387 regs->regs[2] = EINTR;
388 break;
389 }
390 /* fallthrough */
391 case ERESTARTNOINTR: /* Userland will reload $v0. */
392 regs->regs[7] = regs->regs[26];
393 regs->cp0_epc -= 8;
394 }
395
396 regs->regs[0] = 0; /* Don't deal with this again. */
397
e50c0a8f 398 if (sig_uses_siginfo(ka))
129bc8f7 399 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
1da177e4 400 else
129bc8f7 401 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
1da177e4 402
69be8f18
SR
403 spin_lock_irq(&current->sighand->siglock);
404 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
405 if (!(ka->sa.sa_flags & SA_NODEFER))
1da177e4 406 sigaddset(&current->blocked,sig);
69be8f18
SR
407 recalc_sigpending();
408 spin_unlock_irq(&current->sighand->siglock);
129bc8f7
RB
409
410 return ret;
1da177e4
LT
411}
412
40ac5d47 413void do_signal(struct pt_regs *regs)
1da177e4
LT
414{
415 struct k_sigaction ka;
7b3e2fc8 416 sigset_t *oldset;
1da177e4
LT
417 siginfo_t info;
418 int signr;
419
1da177e4
LT
420 /*
421 * We want the common case to go fast, which is why we may in certain
422 * cases get here from kernel mode. Just return without doing anything
423 * if so.
424 */
425 if (!user_mode(regs))
40ac5d47 426 return;
1da177e4 427
d4b3a80e 428 if (try_to_freeze())
1da177e4
LT
429 goto no_signal;
430
7b3e2fc8
RB
431 if (test_thread_flag(TIF_RESTORE_SIGMASK))
432 oldset = &current->saved_sigmask;
433 else
1da177e4
LT
434 oldset = &current->blocked;
435
7b3e2fc8 436
1da177e4 437 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
7b3e2fc8
RB
438 if (signr > 0) {
439 /* Whee! Actually deliver the signal. */
440 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
441 /*
442 * A signal was successfully delivered; the saved
443 * sigmask will have been stored in the signal frame,
444 * and will be restored by sigreturn, so we can simply
445 * clear the TIF_RESTORE_SIGMASK flag.
446 */
447 if (test_thread_flag(TIF_RESTORE_SIGMASK))
448 clear_thread_flag(TIF_RESTORE_SIGMASK);
449 }
450 }
1da177e4
LT
451
452no_signal:
453 /*
454 * Who's code doesn't conform to the restartable syscall convention
455 * dies here!!! The li instruction, a single machine instruction,
456 * must directly be followed by the syscall instruction.
457 */
458 if (regs->regs[0]) {
459 if (regs->regs[2] == ERESTARTNOHAND ||
460 regs->regs[2] == ERESTARTSYS ||
461 regs->regs[2] == ERESTARTNOINTR) {
462 regs->regs[7] = regs->regs[26];
463 regs->cp0_epc -= 8;
464 }
465 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
466 regs->regs[2] = __NR_restart_syscall;
467 regs->regs[7] = regs->regs[26];
468 regs->cp0_epc -= 4;
469 }
470 }
7b3e2fc8
RB
471
472 /*
473 * If there's no signal to deliver, we just put the saved sigmask
474 * back
475 */
476 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
477 clear_thread_flag(TIF_RESTORE_SIGMASK);
478 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
479 }
1da177e4
LT
480}
481
482/*
483 * notification of userspace execution resumption
7b3e2fc8 484 * - triggered by the TIF_WORK_MASK flags
1da177e4 485 */
7b3e2fc8 486asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
1da177e4
LT
487 __u32 thread_info_flags)
488{
489 /* deal with pending signal delivery */
7b3e2fc8
RB
490 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
491 current->thread.abi->do_signal(regs);
1da177e4 492}
This page took 0.186811 seconds and 5 git commands to generate.