2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
7 * 2000-2002 x86-64 support by Andi Kleen
10 #include <linux/sched.h>
12 #include <linux/smp.h>
13 #include <linux/kernel.h>
14 #include <linux/signal.h>
15 #include <linux/errno.h>
16 #include <linux/wait.h>
17 #include <linux/ptrace.h>
18 #include <linux/tracehook.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/personality.h>
22 #include <linux/uaccess.h>
24 #include <asm/processor.h>
25 #include <asm/ucontext.h>
30 #include <asm/proto.h>
31 #include <asm/ia32_unistd.h>
33 #endif /* CONFIG_X86_64 */
35 #include <asm/syscall.h>
36 #include <asm/syscalls.h>
40 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
42 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
43 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
44 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
48 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
50 # define FIX_EFLAGS __FIX_EFLAGS
54 asmlinkage
int sys_sigaltstack(unsigned long bx
)
57 * This is needed to make gcc realize it doesn't own the
60 struct pt_regs
*regs
= (struct pt_regs
*)&bx
;
61 const stack_t __user
*uss
= (const stack_t __user
*)bx
;
62 stack_t __user
*uoss
= (stack_t __user
*)regs
->cx
;
64 return do_sigaltstack(uss
, uoss
, regs
->sp
);
66 #else /* !CONFIG_X86_32 */
68 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
,
71 return do_sigaltstack(uss
, uoss
, regs
->sp
);
73 #endif /* CONFIG_X86_32 */
76 err |= __get_user(regs->x, &sc->x); \
79 #define COPY_SEG_CPL3(seg) { \
81 err |= __get_user(tmp, &sc->seg); \
82 regs->seg = tmp | 3; \
86 * Do a signal return; undo the signal stack.
89 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
,
93 unsigned int tmpflags
;
96 /* Always make any pending restarted system calls return -EINTR */
97 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
104 #endif /* CONFIG_X86_32 */
106 COPY(di
); COPY(si
); COPY(bp
); COPY(sp
); COPY(bx
);
107 COPY(dx
); COPY(cx
); COPY(ip
);
118 #endif /* CONFIG_X86_64 */
123 #else /* !CONFIG_X86_32 */
124 /* Kernel saves and restores only the CS segment register on signals,
125 * which is the bare minimum needed to allow mixed 32/64-bit code.
126 * App's signal handler can save/restore other segments if needed. */
128 #endif /* CONFIG_X86_32 */
130 err
|= __get_user(tmpflags
, &sc
->flags
);
131 regs
->flags
= (regs
->flags
& ~FIX_EFLAGS
) | (tmpflags
& FIX_EFLAGS
);
132 regs
->orig_ax
= -1; /* disable syscall checks */
134 err
|= __get_user(buf
, &sc
->fpstate
);
135 err
|= restore_i387_xstate(buf
);
137 err
|= __get_user(*pax
, &sc
->ax
);
141 static long do_rt_sigreturn(struct pt_regs
*regs
)
143 struct rt_sigframe __user
*frame
;
147 frame
= (struct rt_sigframe __user
*)(regs
->sp
- sizeof(long));
148 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
150 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
153 sigdelsetmask(&set
, ~_BLOCKABLE
);
154 spin_lock_irq(¤t
->sighand
->siglock
);
155 current
->blocked
= set
;
157 spin_unlock_irq(¤t
->sighand
->siglock
);
159 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &ax
))
162 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, regs
->sp
) == -EFAULT
)
168 signal_fault(regs
, frame
, "rt_sigreturn");
172 asmlinkage
long sys_rt_sigreturn(struct pt_regs
*regs
)
174 return do_rt_sigreturn(regs
);
178 * Set up a signal frame.
181 setup_sigcontext(struct sigcontext __user
*sc
, void __user
*fpstate
,
182 struct pt_regs
*regs
, unsigned long mask
)
190 savesegment(gs
, tmp
);
191 err
|= __put_user(tmp
, (unsigned int __user
*)&sc
->gs
);
193 err
|= __put_user(regs
->fs
, (unsigned int __user
*)&sc
->fs
);
194 err
|= __put_user(regs
->es
, (unsigned int __user
*)&sc
->es
);
195 err
|= __put_user(regs
->ds
, (unsigned int __user
*)&sc
->ds
);
196 #endif /* CONFIG_X86_32 */
198 err
|= __put_user(regs
->di
, &sc
->di
);
199 err
|= __put_user(regs
->si
, &sc
->si
);
200 err
|= __put_user(regs
->bp
, &sc
->bp
);
201 err
|= __put_user(regs
->sp
, &sc
->sp
);
202 err
|= __put_user(regs
->bx
, &sc
->bx
);
203 err
|= __put_user(regs
->dx
, &sc
->dx
);
204 err
|= __put_user(regs
->cx
, &sc
->cx
);
205 err
|= __put_user(regs
->ax
, &sc
->ax
);
207 err
|= __put_user(regs
->r8
, &sc
->r8
);
208 err
|= __put_user(regs
->r9
, &sc
->r9
);
209 err
|= __put_user(regs
->r10
, &sc
->r10
);
210 err
|= __put_user(regs
->r11
, &sc
->r11
);
211 err
|= __put_user(regs
->r12
, &sc
->r12
);
212 err
|= __put_user(regs
->r13
, &sc
->r13
);
213 err
|= __put_user(regs
->r14
, &sc
->r14
);
214 err
|= __put_user(regs
->r15
, &sc
->r15
);
215 #endif /* CONFIG_X86_64 */
217 err
|= __put_user(current
->thread
.trap_no
, &sc
->trapno
);
218 err
|= __put_user(current
->thread
.error_code
, &sc
->err
);
219 err
|= __put_user(regs
->ip
, &sc
->ip
);
221 err
|= __put_user(regs
->cs
, (unsigned int __user
*)&sc
->cs
);
222 err
|= __put_user(regs
->flags
, &sc
->flags
);
223 err
|= __put_user(regs
->sp
, &sc
->sp_at_signal
);
224 err
|= __put_user(regs
->ss
, (unsigned int __user
*)&sc
->ss
);
225 #else /* !CONFIG_X86_32 */
226 err
|= __put_user(regs
->flags
, &sc
->flags
);
227 err
|= __put_user(regs
->cs
, &sc
->cs
);
228 err
|= __put_user(0, &sc
->gs
);
229 err
|= __put_user(0, &sc
->fs
);
230 #endif /* CONFIG_X86_32 */
232 err
|= __put_user(fpstate
, &sc
->fpstate
);
234 /* non-iBCS2 extensions.. */
235 err
|= __put_user(mask
, &sc
->oldmask
);
236 err
|= __put_user(current
->thread
.cr2
, &sc
->cr2
);
242 * Determine which stack to use..
246 get_stack(struct k_sigaction
*ka
, unsigned long sp
, unsigned long size
)
248 /* Default to using normal stack - redzone*/
251 /* This is the X/Open sanctioned signal stack switching. */
252 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
253 if (sas_ss_flags(sp
) == 0)
254 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
257 return (void __user
*)round_down(sp
- size
, 64);
260 static int __setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
261 sigset_t
*set
, struct pt_regs
*regs
)
263 struct rt_sigframe __user
*frame
;
264 void __user
*fp
= NULL
;
266 struct task_struct
*me
= current
;
269 fp
= get_stack(ka
, regs
->sp
, sig_xstate_size
);
270 frame
= (void __user
*)round_down(
271 (unsigned long)fp
- sizeof(struct rt_sigframe
), 16) - 8;
273 if (save_i387_xstate(fp
) < 0)
276 frame
= get_stack(ka
, regs
->sp
, sizeof(struct rt_sigframe
)) - 8;
278 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
281 if (ka
->sa
.sa_flags
& SA_SIGINFO
) {
282 if (copy_siginfo_to_user(&frame
->info
, info
))
286 /* Create the ucontext. */
288 err
|= __put_user(UC_FP_XSTATE
, &frame
->uc
.uc_flags
);
290 err
|= __put_user(0, &frame
->uc
.uc_flags
);
291 err
|= __put_user(0, &frame
->uc
.uc_link
);
292 err
|= __put_user(me
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
293 err
|= __put_user(sas_ss_flags(regs
->sp
),
294 &frame
->uc
.uc_stack
.ss_flags
);
295 err
|= __put_user(me
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
296 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, fp
, regs
, set
->sig
[0]);
297 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
299 /* Set up to return from userspace. If provided, use a stub
300 already in userspace. */
301 /* x86-64 should always use SA_RESTORER. */
302 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
303 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
305 /* could use a vstub here */
312 /* Set up registers for signal handler */
314 /* In case the signal handler was declared without prototypes */
317 /* This also works for non SA_SIGINFO handlers because they expect the
318 next argument after the signal number on the stack. */
319 regs
->si
= (unsigned long)&frame
->info
;
320 regs
->dx
= (unsigned long)&frame
->uc
;
321 regs
->ip
= (unsigned long) ka
->sa
.sa_handler
;
323 regs
->sp
= (unsigned long)frame
;
325 /* Set up the CS register to run signal handlers in 64-bit mode,
326 even if the handler happens to be interrupting 32-bit code. */
327 regs
->cs
= __USER_CS
;
333 * OK, we're invoking a handler
335 static int signr_convert(int sig
)
338 struct thread_info
*info
= current_thread_info();
340 if (info
->exec_domain
&& info
->exec_domain
->signal_invmap
&& sig
< 32)
341 return info
->exec_domain
->signal_invmap
[sig
];
342 #endif /* CONFIG_X86_32 */
349 #define ia32_setup_frame __setup_frame
350 #define ia32_setup_rt_frame __setup_rt_frame
352 #else /* !CONFIG_X86_32 */
354 #ifdef CONFIG_IA32_EMULATION
355 #define is_ia32 test_thread_flag(TIF_IA32)
356 #else /* !CONFIG_IA32_EMULATION */
358 #endif /* CONFIG_IA32_EMULATION */
360 #endif /* CONFIG_X86_32 */
363 setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
364 sigset_t
*set
, struct pt_regs
*regs
)
366 int usig
= signr_convert(sig
);
369 /* Set up the stack frame */
371 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
372 ret
= ia32_setup_rt_frame(usig
, ka
, info
, set
, regs
);
374 ret
= ia32_setup_frame(usig
, ka
, set
, regs
);
376 ret
= __setup_rt_frame(sig
, ka
, info
, set
, regs
);
379 force_sigsegv(sig
, current
);
387 handle_signal(unsigned long sig
, siginfo_t
*info
, struct k_sigaction
*ka
,
388 sigset_t
*oldset
, struct pt_regs
*regs
)
392 /* Are we from a system call? */
393 if (syscall_get_nr(current
, regs
) >= 0) {
394 /* If so, check system call restarting.. */
395 switch (syscall_get_error(current
, regs
)) {
396 case -ERESTART_RESTARTBLOCK
:
397 case -ERESTARTNOHAND
:
402 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
407 case -ERESTARTNOINTR
:
408 regs
->ax
= regs
->orig_ax
;
415 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
416 * flag so that register information in the sigcontext is correct.
418 if (unlikely(regs
->flags
& X86_EFLAGS_TF
) &&
419 likely(test_and_clear_thread_flag(TIF_FORCED_TF
)))
420 regs
->flags
&= ~X86_EFLAGS_TF
;
422 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
429 * This has nothing to do with segment registers,
430 * despite the name. This magic affects uaccess.h
431 * macros' behavior. Reset it to the normal setting.
437 * Clear the direction flag as per the ABI for function entry.
439 regs
->flags
&= ~X86_EFLAGS_DF
;
442 * Clear TF when entering the signal handler, but
443 * notify any tracer that was single-stepping it.
444 * The tracer may want to single-step inside the
447 regs
->flags
&= ~X86_EFLAGS_TF
;
449 spin_lock_irq(¤t
->sighand
->siglock
);
450 sigorsets(¤t
->blocked
, ¤t
->blocked
, &ka
->sa
.sa_mask
);
451 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
452 sigaddset(¤t
->blocked
, sig
);
454 spin_unlock_irq(¤t
->sighand
->siglock
);
456 tracehook_signal_handler(sig
, info
, ka
, regs
,
457 test_thread_flag(TIF_SINGLESTEP
));
463 #define NR_restart_syscall __NR_restart_syscall
464 #else /* !CONFIG_X86_32 */
465 #define NR_restart_syscall \
466 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
467 #endif /* CONFIG_X86_32 */
470 * Note that 'init' is a special process: it doesn't get signals it doesn't
471 * want to handle. Thus you cannot kill init even with a SIGKILL even by
474 static void do_signal(struct pt_regs
*regs
)
476 struct k_sigaction ka
;
482 * We want the common case to go fast, which is why we may in certain
483 * cases get here from kernel mode. Just return without doing anything
485 * X86_32: vm86 regs switched out by assembly code before reaching
486 * here, so testing against kernel CS suffices.
488 if (!user_mode(regs
))
491 if (current_thread_info()->status
& TS_RESTORE_SIGMASK
)
492 oldset
= ¤t
->saved_sigmask
;
494 oldset
= ¤t
->blocked
;
496 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
499 * Re-enable any watchpoints before delivering the
500 * signal to user space. The processor register will
501 * have been cleared if the watchpoint triggered
504 if (current
->thread
.debugreg7
)
505 set_debugreg(current
->thread
.debugreg7
, 7);
507 /* Whee! Actually deliver the signal. */
508 if (handle_signal(signr
, &info
, &ka
, oldset
, regs
) == 0) {
510 * A signal was successfully delivered; the saved
511 * sigmask will have been stored in the signal frame,
512 * and will be restored by sigreturn, so we can simply
513 * clear the TS_RESTORE_SIGMASK flag.
515 current_thread_info()->status
&= ~TS_RESTORE_SIGMASK
;
520 /* Did we come from a system call? */
521 if (syscall_get_nr(current
, regs
) >= 0) {
522 /* Restart the system call - no handlers present */
523 switch (syscall_get_error(current
, regs
)) {
524 case -ERESTARTNOHAND
:
526 case -ERESTARTNOINTR
:
527 regs
->ax
= regs
->orig_ax
;
531 case -ERESTART_RESTARTBLOCK
:
532 regs
->ax
= NR_restart_syscall
;
539 * If there's no signal to deliver, we just put the saved sigmask
542 if (current_thread_info()->status
& TS_RESTORE_SIGMASK
) {
543 current_thread_info()->status
&= ~TS_RESTORE_SIGMASK
;
544 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
549 * notification of userspace execution resumption
550 * - triggered by the TIF_WORK_MASK flags
553 do_notify_resume(struct pt_regs
*regs
, void *unused
, __u32 thread_info_flags
)
555 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
556 /* notify userspace of pending MCEs */
557 if (thread_info_flags
& _TIF_MCE_NOTIFY
)
559 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
561 /* deal with pending signal delivery */
562 if (thread_info_flags
& _TIF_SIGPENDING
)
565 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
566 clear_thread_flag(TIF_NOTIFY_RESUME
);
567 tracehook_notify_resume(regs
);
571 clear_thread_flag(TIF_IRET
);
572 #endif /* CONFIG_X86_32 */
575 void signal_fault(struct pt_regs
*regs
, void __user
*frame
, char *where
)
577 struct task_struct
*me
= current
;
579 if (show_unhandled_signals
&& printk_ratelimit()) {
581 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
582 me
->comm
, me
->pid
, where
, frame
,
583 regs
->ip
, regs
->sp
, regs
->orig_ax
);
584 print_vma_addr(" in ", regs
->ip
);
585 printk(KERN_CONT
"\n");
588 force_sig(SIGSEGV
, me
);