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
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1994 - 2000, 2006 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
10 #include <linux/cache.h>
11 #include <linux/compat.h>
12 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/syscalls.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/suspend.h>
22 #include <linux/compiler.h>
23 #include <linux/uaccess.h>
27 #include <asm/compat-signal.h>
28 #include <linux/bitops.h>
29 #include <asm/cacheflush.h>
31 #include <asm/ucontext.h>
37 #include "signal-common.h"
39 static int (*save_fp_context32
)(struct sigcontext32 __user
*sc
);
40 static int (*restore_fp_context32
)(struct sigcontext32 __user
*sc
);
42 extern asmlinkage
int _save_fp_context32(struct sigcontext32 __user
*sc
);
43 extern asmlinkage
int _restore_fp_context32(struct sigcontext32 __user
*sc
);
46 * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
48 #define __NR_O32_restart_syscall 4253
50 /* 32-bit compatibility types */
52 typedef unsigned int __sighandler32_t
;
53 typedef void (*vfptr_t
)(void);
58 compat_stack_t uc_stack
;
59 struct sigcontext32 uc_mcontext
;
60 compat_sigset_t uc_sigmask
; /* mask last for extensibility */
64 u32 sf_ass
[4]; /* argument save space for o32 */
65 u32 sf_pad
[2]; /* Was: signal trampoline */
66 struct sigcontext32 sf_sc
;
67 compat_sigset_t sf_mask
;
70 struct rt_sigframe32
{
71 u32 rs_ass
[4]; /* argument save space for o32 */
72 u32 rs_pad
[2]; /* Was: signal trampoline */
73 compat_siginfo_t rs_info
;
74 struct ucontext32 rs_uc
;
78 * Thread saved context copy to/from a signal context presumed to be on the
79 * user stack, and therefore accessed with appropriate macros from uaccess.h.
81 static int copy_fp_to_sigcontext32(struct sigcontext32 __user
*sc
)
85 int inc
= test_thread_flag(TIF_32BIT_FPREGS
) ? 2 : 1;
87 for (i
= 0; i
< NUM_FPU_REGS
; i
+= inc
) {
89 __put_user(get_fpr64(¤t
->thread
.fpu
.fpr
[i
], 0),
92 err
|= __put_user(current
->thread
.fpu
.fcr31
, &sc
->sc_fpc_csr
);
97 static int copy_fp_from_sigcontext32(struct sigcontext32 __user
*sc
)
101 int inc
= test_thread_flag(TIF_32BIT_FPREGS
) ? 2 : 1;
104 for (i
= 0; i
< NUM_FPU_REGS
; i
+= inc
) {
105 err
|= __get_user(fpr_val
, &sc
->sc_fpregs
[i
]);
106 set_fpr64(¤t
->thread
.fpu
.fpr
[i
], 0, fpr_val
);
108 err
|= __get_user(current
->thread
.fpu
.fcr31
, &sc
->sc_fpc_csr
);
114 * sigcontext handlers
116 static int protected_save_fp_context32(struct sigcontext32 __user
*sc
)
121 if (is_fpu_owner()) {
122 err
= save_fp_context32(sc
);
126 err
= copy_fp_to_sigcontext32(sc
);
130 /* touch the sigcontext and try again */
131 err
= __put_user(0, &sc
->sc_fpregs
[0]) |
132 __put_user(0, &sc
->sc_fpregs
[31]) |
133 __put_user(0, &sc
->sc_fpc_csr
);
135 break; /* really bad sigcontext */
140 static int protected_restore_fp_context32(struct sigcontext32 __user
*sc
)
142 int err
, tmp __maybe_unused
;
145 if (is_fpu_owner()) {
146 err
= restore_fp_context32(sc
);
150 err
= copy_fp_from_sigcontext32(sc
);
154 /* touch the sigcontext and try again */
155 err
= __get_user(tmp
, &sc
->sc_fpregs
[0]) |
156 __get_user(tmp
, &sc
->sc_fpregs
[31]) |
157 __get_user(tmp
, &sc
->sc_fpc_csr
);
159 break; /* really bad sigcontext */
164 static int setup_sigcontext32(struct pt_regs
*regs
,
165 struct sigcontext32 __user
*sc
)
171 err
|= __put_user(regs
->cp0_epc
, &sc
->sc_pc
);
173 err
|= __put_user(0, &sc
->sc_regs
[0]);
174 for (i
= 1; i
< 32; i
++)
175 err
|= __put_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
177 err
|= __put_user(regs
->hi
, &sc
->sc_mdhi
);
178 err
|= __put_user(regs
->lo
, &sc
->sc_mdlo
);
180 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
181 err
|= __put_user(mfhi1(), &sc
->sc_hi1
);
182 err
|= __put_user(mflo1(), &sc
->sc_lo1
);
183 err
|= __put_user(mfhi2(), &sc
->sc_hi2
);
184 err
|= __put_user(mflo2(), &sc
->sc_lo2
);
185 err
|= __put_user(mfhi3(), &sc
->sc_hi3
);
186 err
|= __put_user(mflo3(), &sc
->sc_lo3
);
189 used_math
= !!used_math();
190 err
|= __put_user(used_math
, &sc
->sc_used_math
);
194 * Save FPU state to signal context. Signal handler
195 * will "inherit" current FPU state.
197 err
|= protected_save_fp_context32(sc
);
203 check_and_restore_fp_context32(struct sigcontext32 __user
*sc
)
207 err
= sig
= fpcsr_pending(&sc
->sc_fpc_csr
);
210 err
|= protected_restore_fp_context32(sc
);
214 static int restore_sigcontext32(struct pt_regs
*regs
,
215 struct sigcontext32 __user
*sc
)
222 /* Always make any pending restarted system calls return -EINTR */
223 current
->restart_block
.fn
= do_no_restart_syscall
;
225 err
|= __get_user(regs
->cp0_epc
, &sc
->sc_pc
);
226 err
|= __get_user(regs
->hi
, &sc
->sc_mdhi
);
227 err
|= __get_user(regs
->lo
, &sc
->sc_mdlo
);
229 err
|= __get_user(treg
, &sc
->sc_hi1
); mthi1(treg
);
230 err
|= __get_user(treg
, &sc
->sc_lo1
); mtlo1(treg
);
231 err
|= __get_user(treg
, &sc
->sc_hi2
); mthi2(treg
);
232 err
|= __get_user(treg
, &sc
->sc_lo2
); mtlo2(treg
);
233 err
|= __get_user(treg
, &sc
->sc_hi3
); mthi3(treg
);
234 err
|= __get_user(treg
, &sc
->sc_lo3
); mtlo3(treg
);
235 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
238 for (i
= 1; i
< 32; i
++)
239 err
|= __get_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
241 err
|= __get_user(used_math
, &sc
->sc_used_math
);
242 conditional_used_math(used_math
);
245 /* restore fpu context if we have used it before */
247 err
= check_and_restore_fp_context32(sc
);
249 /* signal handler may have used FPU. Give it up. */
259 extern void __put_sigset_unknown_nsig(void);
260 extern void __get_sigset_unknown_nsig(void);
262 static inline int put_sigset(const sigset_t
*kbuf
, compat_sigset_t __user
*ubuf
)
266 if (!access_ok(VERIFY_WRITE
, ubuf
, sizeof(*ubuf
)))
269 switch (_NSIG_WORDS
) {
271 __put_sigset_unknown_nsig();
273 err
|= __put_user(kbuf
->sig
[1] >> 32, &ubuf
->sig
[3]);
274 err
|= __put_user(kbuf
->sig
[1] & 0xffffffff, &ubuf
->sig
[2]);
276 err
|= __put_user(kbuf
->sig
[0] >> 32, &ubuf
->sig
[1]);
277 err
|= __put_user(kbuf
->sig
[0] & 0xffffffff, &ubuf
->sig
[0]);
283 static inline int get_sigset(sigset_t
*kbuf
, const compat_sigset_t __user
*ubuf
)
286 unsigned long sig
[4];
288 if (!access_ok(VERIFY_READ
, ubuf
, sizeof(*ubuf
)))
291 switch (_NSIG_WORDS
) {
293 __get_sigset_unknown_nsig();
295 err
|= __get_user(sig
[3], &ubuf
->sig
[3]);
296 err
|= __get_user(sig
[2], &ubuf
->sig
[2]);
297 kbuf
->sig
[1] = sig
[2] | (sig
[3] << 32);
299 err
|= __get_user(sig
[1], &ubuf
->sig
[1]);
300 err
|= __get_user(sig
[0], &ubuf
->sig
[0]);
301 kbuf
->sig
[0] = sig
[0] | (sig
[1] << 32);
308 * Atomically swap in the new signal mask, and wait for a signal.
311 asmlinkage
int sys32_sigsuspend(compat_sigset_t __user
*uset
)
313 return compat_sys_rt_sigsuspend(uset
, sizeof(compat_sigset_t
));
316 SYSCALL_DEFINE3(32_sigaction
, long, sig
, const struct compat_sigaction __user
*, act
,
317 struct compat_sigaction __user
*, oact
)
319 struct k_sigaction new_ka
, old_ka
;
327 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)))
329 err
|= __get_user(handler
, &act
->sa_handler
);
330 new_ka
.sa
.sa_handler
= (void __user
*)(s64
)handler
;
331 err
|= __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
332 err
|= __get_user(mask
, &act
->sa_mask
.sig
[0]);
336 siginitset(&new_ka
.sa
.sa_mask
, mask
);
339 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
342 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)))
344 err
|= __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
345 err
|= __put_user((u32
)(u64
)old_ka
.sa
.sa_handler
,
347 err
|= __put_user(old_ka
.sa
.sa_mask
.sig
[0], oact
->sa_mask
.sig
);
348 err
|= __put_user(0, &oact
->sa_mask
.sig
[1]);
349 err
|= __put_user(0, &oact
->sa_mask
.sig
[2]);
350 err
|= __put_user(0, &oact
->sa_mask
.sig
[3]);
358 int copy_siginfo_to_user32(compat_siginfo_t __user
*to
, const siginfo_t
*from
)
362 if (!access_ok (VERIFY_WRITE
, to
, sizeof(compat_siginfo_t
)))
365 /* If you change siginfo_t structure, please be sure
366 this code is fixed accordingly.
367 It should never copy any pad contained in the structure
368 to avoid security leaks, but must copy the generic
369 3 ints plus the relevant union member.
370 This routine must convert siginfo from 64bit to 32bit as well
372 err
= __put_user(from
->si_signo
, &to
->si_signo
);
373 err
|= __put_user(from
->si_errno
, &to
->si_errno
);
374 err
|= __put_user((short)from
->si_code
, &to
->si_code
);
375 if (from
->si_code
< 0)
376 err
|= __copy_to_user(&to
->_sifields
._pad
, &from
->_sifields
._pad
, SI_PAD_SIZE
);
378 switch (from
->si_code
>> 16) {
379 case __SI_TIMER
>> 16:
380 err
|= __put_user(from
->si_tid
, &to
->si_tid
);
381 err
|= __put_user(from
->si_overrun
, &to
->si_overrun
);
382 err
|= __put_user(from
->si_int
, &to
->si_int
);
384 case __SI_CHLD
>> 16:
385 err
|= __put_user(from
->si_utime
, &to
->si_utime
);
386 err
|= __put_user(from
->si_stime
, &to
->si_stime
);
387 err
|= __put_user(from
->si_status
, &to
->si_status
);
389 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
390 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
392 case __SI_FAULT
>> 16:
393 err
|= __put_user((unsigned long)from
->si_addr
, &to
->si_addr
);
395 case __SI_POLL
>> 16:
396 err
|= __put_user(from
->si_band
, &to
->si_band
);
397 err
|= __put_user(from
->si_fd
, &to
->si_fd
);
399 case __SI_RT
>> 16: /* This is not generated by the kernel as of now. */
400 case __SI_MESGQ
>> 16:
401 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
402 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
403 err
|= __put_user(from
->si_int
, &to
->si_int
);
410 int copy_siginfo_from_user32(siginfo_t
*to
, compat_siginfo_t __user
*from
)
412 if (copy_from_user(to
, from
, 3*sizeof(int)) ||
413 copy_from_user(to
->_sifields
._pad
,
414 from
->_sifields
._pad
, SI_PAD_SIZE32
))
420 asmlinkage
void sys32_sigreturn(nabi_no_regargs
struct pt_regs regs
)
422 struct sigframe32 __user
*frame
;
426 frame
= (struct sigframe32 __user
*) regs
.regs
[29];
427 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
429 if (__copy_conv_sigset_from_user(&blocked
, &frame
->sf_mask
))
432 set_current_blocked(&blocked
);
434 sig
= restore_sigcontext32(®s
, &frame
->sf_sc
);
438 force_sig(sig
, current
);
441 * Don't let your children do this ...
443 __asm__
__volatile__(
451 force_sig(SIGSEGV
, current
);
454 asmlinkage
void sys32_rt_sigreturn(nabi_no_regargs
struct pt_regs regs
)
456 struct rt_sigframe32 __user
*frame
;
460 frame
= (struct rt_sigframe32 __user
*) regs
.regs
[29];
461 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
463 if (__copy_conv_sigset_from_user(&set
, &frame
->rs_uc
.uc_sigmask
))
466 set_current_blocked(&set
);
468 sig
= restore_sigcontext32(®s
, &frame
->rs_uc
.uc_mcontext
);
472 force_sig(sig
, current
);
474 if (compat_restore_altstack(&frame
->rs_uc
.uc_stack
))
478 * Don't let your children do this ...
480 __asm__
__volatile__(
488 force_sig(SIGSEGV
, current
);
491 static int setup_frame_32(void *sig_return
, struct ksignal
*ksig
,
492 struct pt_regs
*regs
, sigset_t
*set
)
494 struct sigframe32 __user
*frame
;
497 frame
= get_sigframe(ksig
, regs
, sizeof(*frame
));
498 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
501 err
|= setup_sigcontext32(regs
, &frame
->sf_sc
);
502 err
|= __copy_conv_sigset_to_user(&frame
->sf_mask
, set
);
508 * Arguments to signal handler:
511 * a1 = 0 (should be cause)
512 * a2 = pointer to struct sigcontext
514 * $25 and c0_epc point to the signal handler, $29 points to the
517 regs
->regs
[ 4] = ksig
->sig
;
519 regs
->regs
[ 6] = (unsigned long) &frame
->sf_sc
;
520 regs
->regs
[29] = (unsigned long) frame
;
521 regs
->regs
[31] = (unsigned long) sig_return
;
522 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ksig
->ka
.sa
.sa_handler
;
524 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
525 current
->comm
, current
->pid
,
526 frame
, regs
->cp0_epc
, regs
->regs
[31]);
531 static int setup_rt_frame_32(void *sig_return
, struct ksignal
*ksig
,
532 struct pt_regs
*regs
, sigset_t
*set
)
534 struct rt_sigframe32 __user
*frame
;
537 frame
= get_sigframe(ksig
, regs
, sizeof(*frame
));
538 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
541 /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
542 err
|= copy_siginfo_to_user32(&frame
->rs_info
, &ksig
->info
);
544 /* Create the ucontext. */
545 err
|= __put_user(0, &frame
->rs_uc
.uc_flags
);
546 err
|= __put_user(0, &frame
->rs_uc
.uc_link
);
547 err
|= __compat_save_altstack(&frame
->rs_uc
.uc_stack
, regs
->regs
[29]);
548 err
|= setup_sigcontext32(regs
, &frame
->rs_uc
.uc_mcontext
);
549 err
|= __copy_conv_sigset_to_user(&frame
->rs_uc
.uc_sigmask
, set
);
555 * Arguments to signal handler:
558 * a1 = 0 (should be cause)
559 * a2 = pointer to ucontext
561 * $25 and c0_epc point to the signal handler, $29 points to
562 * the struct rt_sigframe32.
564 regs
->regs
[ 4] = ksig
->sig
;
565 regs
->regs
[ 5] = (unsigned long) &frame
->rs_info
;
566 regs
->regs
[ 6] = (unsigned long) &frame
->rs_uc
;
567 regs
->regs
[29] = (unsigned long) frame
;
568 regs
->regs
[31] = (unsigned long) sig_return
;
569 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ksig
->ka
.sa
.sa_handler
;
571 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
572 current
->comm
, current
->pid
,
573 frame
, regs
->cp0_epc
, regs
->regs
[31]);
579 * o32 compatibility on 64-bit kernels, without DSP ASE
581 struct mips_abi mips_abi_32
= {
582 .setup_frame
= setup_frame_32
,
583 .signal_return_offset
=
584 offsetof(struct mips_vdso
, o32_signal_trampoline
),
585 .setup_rt_frame
= setup_rt_frame_32
,
586 .rt_signal_return_offset
=
587 offsetof(struct mips_vdso
, o32_rt_signal_trampoline
),
588 .restart
= __NR_O32_restart_syscall
591 static int signal32_init(void)
594 save_fp_context32
= _save_fp_context32
;
595 restore_fp_context32
= _restore_fp_context32
;
597 save_fp_context32
= copy_fp_to_sigcontext32
;
598 restore_fp_context32
= copy_fp_from_sigcontext32
;
604 arch_initcall(signal32_init
);