2 * Copyright 2004-2009 Analog Devices Inc.
4 * Licensed under the GPL-2 or later
7 #include <linux/signal.h>
8 #include <linux/syscalls.h>
9 #include <linux/ptrace.h>
10 #include <linux/tty.h>
11 #include <linux/personality.h>
12 #include <linux/binfmts.h>
13 #include <linux/freezer.h>
14 #include <linux/uaccess.h>
15 #include <linux/tracehook.h>
17 #include <asm/cacheflush.h>
18 #include <asm/ucontext.h>
19 #include <asm/fixed_code.h>
21 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
23 /* Location of the trace bit in SYSCFG. */
24 #define TRACE_BITS 0x0001
26 struct fdpic_func_descriptor
{
33 struct siginfo
*pinfo
;
35 /* This is no longer needed by the kernel, but unfortunately userspace
36 * code expects it to be there. */
42 asmlinkage
int sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
)
44 return do_sigaltstack(uss
, uoss
, rdusp());
48 rt_restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
, int *pr0
)
50 unsigned long usp
= 0;
53 #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
55 /* restore passed registers */
56 RESTORE(r0
); RESTORE(r1
); RESTORE(r2
); RESTORE(r3
);
57 RESTORE(r4
); RESTORE(r5
); RESTORE(r6
); RESTORE(r7
);
58 RESTORE(p0
); RESTORE(p1
); RESTORE(p2
); RESTORE(p3
);
59 RESTORE(p4
); RESTORE(p5
);
60 err
|= __get_user(usp
, &sc
->sc_usp
);
62 RESTORE(a0w
); RESTORE(a1w
);
63 RESTORE(a0x
); RESTORE(a1x
);
69 RESTORE(i0
); RESTORE(i1
); RESTORE(i2
); RESTORE(i3
);
70 RESTORE(m0
); RESTORE(m1
); RESTORE(m2
); RESTORE(m3
);
71 RESTORE(l0
); RESTORE(l1
); RESTORE(l2
); RESTORE(l3
);
72 RESTORE(b0
); RESTORE(b1
); RESTORE(b2
); RESTORE(b3
);
73 RESTORE(lc0
); RESTORE(lc1
);
74 RESTORE(lt0
); RESTORE(lt1
);
75 RESTORE(lb0
); RESTORE(lb1
);
78 regs
->orig_p0
= -1; /* disable syscall checks */
84 asmlinkage
int do_rt_sigreturn(unsigned long __unused
)
86 struct pt_regs
*regs
= (struct pt_regs
*)__unused
;
87 unsigned long usp
= rdusp();
88 struct rt_sigframe
*frame
= (struct rt_sigframe
*)(usp
);
92 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
94 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
97 sigdelsetmask(&set
, ~_BLOCKABLE
);
98 spin_lock_irq(¤t
->sighand
->siglock
);
99 current
->blocked
= set
;
101 spin_unlock_irq(¤t
->sighand
->siglock
);
103 if (rt_restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &r0
))
106 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, regs
->usp
) == -EFAULT
)
112 force_sig(SIGSEGV
, current
);
116 static inline int rt_setup_sigcontext(struct sigcontext
*sc
, struct pt_regs
*regs
)
120 #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
122 SETUP(r0
); SETUP(r1
); SETUP(r2
); SETUP(r3
);
123 SETUP(r4
); SETUP(r5
); SETUP(r6
); SETUP(r7
);
124 SETUP(p0
); SETUP(p1
); SETUP(p2
); SETUP(p3
);
125 SETUP(p4
); SETUP(p5
);
126 err
|= __put_user(rdusp(), &sc
->sc_usp
);
127 SETUP(a0w
); SETUP(a1w
);
128 SETUP(a0x
); SETUP(a1x
);
134 SETUP(i0
); SETUP(i1
); SETUP(i2
); SETUP(i3
);
135 SETUP(m0
); SETUP(m1
); SETUP(m2
); SETUP(m3
);
136 SETUP(l0
); SETUP(l1
); SETUP(l2
); SETUP(l3
);
137 SETUP(b0
); SETUP(b1
); SETUP(b2
); SETUP(b3
);
138 SETUP(lc0
); SETUP(lc1
);
139 SETUP(lt0
); SETUP(lt1
);
140 SETUP(lb0
); SETUP(lb1
);
146 static inline void *get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
,
151 /* Default to using normal stack. */
154 /* This is the X/Open sanctioned signal stack switching. */
155 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
156 if (!on_sig_stack(usp
))
157 usp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
159 return (void *)((usp
- frame_size
) & -8UL);
163 setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
* info
,
164 sigset_t
* set
, struct pt_regs
*regs
)
166 struct rt_sigframe
*frame
;
169 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
171 err
|= __put_user((current_thread_info()->exec_domain
172 && current_thread_info()->exec_domain
->signal_invmap
174 ? current_thread_info()->exec_domain
->
175 signal_invmap
[sig
] : sig
), &frame
->sig
);
177 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
178 err
|= __put_user(&frame
->uc
, &frame
->puc
);
179 err
|= copy_siginfo_to_user(&frame
->info
, info
);
181 /* Create the ucontext. */
182 err
|= __put_user(0, &frame
->uc
.uc_flags
);
183 err
|= __put_user(0, &frame
->uc
.uc_link
);
185 __put_user((void *)current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
186 err
|= __put_user(sas_ss_flags(rdusp()), &frame
->uc
.uc_stack
.ss_flags
);
187 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
188 err
|= rt_setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
);
189 err
|= copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
194 /* Set up registers for signal handler */
195 wrusp((unsigned long)frame
);
196 if (current
->personality
& FDPIC_FUNCPTRS
) {
197 struct fdpic_func_descriptor __user
*funcptr
=
198 (struct fdpic_func_descriptor
*) ka
->sa
.sa_handler
;
199 __get_user(regs
->pc
, &funcptr
->text
);
200 __get_user(regs
->p3
, &funcptr
->GOT
);
202 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
203 regs
->rets
= SIGRETURN_STUB
;
205 regs
->r0
= frame
->sig
;
206 regs
->r1
= (unsigned long)(&frame
->info
);
207 regs
->r2
= (unsigned long)(&frame
->uc
);
210 * Clear the trace flag when entering the signal handler, but
211 * notify any tracer that was single-stepping it. The tracer
212 * may want to single-step inside the handler too.
214 if (regs
->syscfg
& TRACE_BITS
) {
215 regs
->syscfg
&= ~TRACE_BITS
;
216 ptrace_notify(SIGTRAP
);
223 ka
->sa
.sa_handler
= SIG_DFL
;
224 force_sig(SIGSEGV
, current
);
229 handle_restart(struct pt_regs
*regs
, struct k_sigaction
*ka
, int has_handler
)
232 case -ERESTARTNOHAND
:
239 if (has_handler
&& !(ka
->sa
.sa_flags
& SA_RESTART
)) {
244 case -ERESTARTNOINTR
:
246 regs
->p0
= regs
->orig_p0
;
247 regs
->r0
= regs
->orig_r0
;
254 * OK, we're invoking a handler
257 handle_signal(int sig
, siginfo_t
*info
, struct k_sigaction
*ka
,
258 sigset_t
*oldset
, struct pt_regs
*regs
)
262 /* are we from a system call? to see pt_regs->orig_p0 */
263 if (regs
->orig_p0
>= 0)
264 /* If so, check system call restarting.. */
265 handle_restart(regs
, ka
, 1);
267 /* set up the stack frame */
268 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
271 spin_lock_irq(¤t
->sighand
->siglock
);
272 sigorsets(¤t
->blocked
, ¤t
->blocked
,
274 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
275 sigaddset(¤t
->blocked
, sig
);
277 spin_unlock_irq(¤t
->sighand
->siglock
);
283 * Note that 'init' is a special process: it doesn't get signals it doesn't
284 * want to handle. Thus you cannot kill init even with a SIGKILL even by
287 * Note that we go through the signals twice: once to check the signals
288 * that the kernel can handle, and then we build all the user-level signal
289 * handling stack-frames in one go after that.
291 asmlinkage
void do_signal(struct pt_regs
*regs
)
295 struct k_sigaction ka
;
298 current
->thread
.esp0
= (unsigned long)regs
;
303 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
304 oldset
= ¤t
->saved_sigmask
;
306 oldset
= ¤t
->blocked
;
308 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
310 /* Whee! Actually deliver the signal. */
311 if (handle_signal(signr
, &info
, &ka
, oldset
, regs
) == 0) {
312 /* a signal was successfully delivered; the saved
313 * sigmask will have been stored in the signal frame,
314 * and will be restored by sigreturn, so we can simply
315 * clear the TIF_RESTORE_SIGMASK flag */
316 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
317 clear_thread_flag(TIF_RESTORE_SIGMASK
);
324 /* Did we come from a system call? */
325 if (regs
->orig_p0
>= 0)
326 /* Restart the system call - no handlers present */
327 handle_restart(regs
, NULL
, 0);
329 /* if there's no signal to deliver, we just put the saved sigmask
331 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
332 clear_thread_flag(TIF_RESTORE_SIGMASK
);
333 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
338 * notification of userspace execution resumption
340 asmlinkage
void do_notify_resume(struct pt_regs
*regs
)
342 if (test_thread_flag(TIF_SIGPENDING
) || test_thread_flag(TIF_RESTORE_SIGMASK
))
345 if (test_thread_flag(TIF_NOTIFY_RESUME
)) {
346 clear_thread_flag(TIF_NOTIFY_RESUME
);
347 tracehook_notify_resume(regs
);
348 if (current
->replacement_session_keyring
)
349 key_replace_session_keyring();