2 * Signal support for Hexagon processor
4 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <linux/linkage.h>
22 #include <linux/syscalls.h>
23 #include <linux/freezer.h>
24 #include <linux/tracehook.h>
25 #include <asm/registers.h>
26 #include <asm/thread_info.h>
27 #include <asm/unistd.h>
28 #include <asm/uaccess.h>
29 #include <asm/ucontext.h>
30 #include <asm/cacheflush.h>
31 #include <asm/signal.h>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 unsigned long tramp
[2];
42 static void __user
*get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
,
45 unsigned long sp
= regs
->r29
;
47 /* Switch to signal stack if appropriate */
48 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && (sas_ss_flags(sp
) == 0))
49 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
51 return (void __user
*)((sp
- frame_size
) & ~(sizeof(long long) - 1));
54 static int setup_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
59 err
|= copy_to_user(&sc
->sc_regs
.r0
, ®s
->r00
,
60 32*sizeof(unsigned long));
62 err
|= __put_user(regs
->sa0
, &sc
->sc_regs
.sa0
);
63 err
|= __put_user(regs
->lc0
, &sc
->sc_regs
.lc0
);
64 err
|= __put_user(regs
->sa1
, &sc
->sc_regs
.sa1
);
65 err
|= __put_user(regs
->lc1
, &sc
->sc_regs
.lc1
);
66 err
|= __put_user(regs
->m0
, &sc
->sc_regs
.m0
);
67 err
|= __put_user(regs
->m1
, &sc
->sc_regs
.m1
);
68 err
|= __put_user(regs
->usr
, &sc
->sc_regs
.usr
);
69 err
|= __put_user(regs
->preds
, &sc
->sc_regs
.p3_0
);
70 err
|= __put_user(regs
->gp
, &sc
->sc_regs
.gp
);
71 err
|= __put_user(regs
->ugp
, &sc
->sc_regs
.ugp
);
73 tmp
= pt_elr(regs
); err
|= __put_user(tmp
, &sc
->sc_regs
.pc
);
74 tmp
= pt_cause(regs
); err
|= __put_user(tmp
, &sc
->sc_regs
.cause
);
75 tmp
= pt_badva(regs
); err
|= __put_user(tmp
, &sc
->sc_regs
.badva
);
80 static int restore_sigcontext(struct pt_regs
*regs
,
81 struct sigcontext __user
*sc
)
86 err
|= copy_from_user(®s
->r00
, &sc
->sc_regs
.r0
,
87 32 * sizeof(unsigned long));
89 err
|= __get_user(regs
->sa0
, &sc
->sc_regs
.sa0
);
90 err
|= __get_user(regs
->lc0
, &sc
->sc_regs
.lc0
);
91 err
|= __get_user(regs
->sa1
, &sc
->sc_regs
.sa1
);
92 err
|= __get_user(regs
->lc1
, &sc
->sc_regs
.lc1
);
93 err
|= __get_user(regs
->m0
, &sc
->sc_regs
.m0
);
94 err
|= __get_user(regs
->m1
, &sc
->sc_regs
.m1
);
95 err
|= __get_user(regs
->usr
, &sc
->sc_regs
.usr
);
96 err
|= __get_user(regs
->preds
, &sc
->sc_regs
.p3_0
);
97 err
|= __get_user(regs
->gp
, &sc
->sc_regs
.gp
);
98 err
|= __get_user(regs
->ugp
, &sc
->sc_regs
.ugp
);
100 err
|= __get_user(tmp
, &sc
->sc_regs
.pc
); pt_set_elr(regs
, tmp
);
106 * Setup signal stack frame with siginfo structure
108 static int setup_rt_frame(int signr
, struct k_sigaction
*ka
, siginfo_t
*info
,
109 sigset_t
*set
, struct pt_regs
*regs
)
112 struct rt_sigframe __user
*frame
;
113 struct hexagon_vdso
*vdso
= current
->mm
->context
.vdso
;
115 frame
= get_sigframe(ka
, regs
, sizeof(struct rt_sigframe
));
117 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(struct rt_sigframe
)))
120 if (copy_siginfo_to_user(&frame
->info
, info
))
123 /* The on-stack signal trampoline is no longer executed;
124 * however, the libgcc signal frame unwinding code checks for
125 * the presence of these two numeric magic values.
127 err
|= __put_user(0x7800d166, &frame
->tramp
[0]);
128 err
|= __put_user(0x5400c004, &frame
->tramp
[1]);
129 err
|= setup_sigcontext(regs
, &frame
->uc
.uc_mcontext
);
130 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
134 /* Load r0/r1 pair with signumber/siginfo pointer... */
135 regs
->r0100
= ((unsigned long long)((unsigned long)&frame
->info
) << 32)
136 | (unsigned long long)signr
;
137 regs
->r02
= (unsigned long) &frame
->uc
;
138 regs
->r31
= (unsigned long) vdso
->rt_signal_trampoline
;
139 pt_psp(regs
) = (unsigned long) frame
;
140 pt_set_elr(regs
, (unsigned long)ka
->sa
.sa_handler
);
145 force_sigsegv(signr
, current
);
150 * Setup invocation of signal handler
152 static int handle_signal(int sig
, siginfo_t
*info
, struct k_sigaction
*ka
,
153 sigset_t
*oldset
, struct pt_regs
*regs
)
158 * If we're handling a signal that aborted a system call,
159 * set up the error return value before adding the signal
160 * frame to the stack.
163 if (regs
->syscall_nr
>= 0) {
165 case -ERESTART_RESTARTBLOCK
:
166 case -ERESTARTNOHAND
:
170 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
175 case -ERESTARTNOINTR
:
176 regs
->r06
= regs
->syscall_nr
;
177 pt_set_elr(regs
, pt_elr(regs
) - 4);
178 regs
->r00
= regs
->restart_r0
;
186 * Set up the stack frame; not doing the SA_SIGINFO thing. We
187 * only set up the rt_frame flavor.
189 rc
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
191 /* If there was an error on setup, no signal was delivered. */
195 spin_lock_irq(¤t
->sighand
->siglock
);
196 sigorsets(¤t
->blocked
, ¤t
->blocked
, &ka
->sa
.sa_mask
);
197 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
198 sigaddset(¤t
->blocked
, sig
);
200 spin_unlock_irq(¤t
->sighand
->siglock
);
206 * Called from return-from-event code.
208 static void do_signal(struct pt_regs
*regs
)
210 struct k_sigaction sigact
;
214 if (!user_mode(regs
))
220 signo
= get_signal_to_deliver(&info
, &sigact
, regs
, NULL
);
225 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
226 oldset
= ¤t
->saved_sigmask
;
228 oldset
= ¤t
->blocked
;
230 if (handle_signal(signo
, &info
, &sigact
, oldset
, regs
) == 0) {
232 * Successful delivery case. The saved sigmask is
233 * stored in the signal frame, and will be restored
234 * by sigreturn. We can clear the TIF flag.
236 clear_thread_flag(TIF_RESTORE_SIGMASK
);
238 tracehook_signal_handler(signo
, &info
, &sigact
, regs
,
239 test_thread_flag(TIF_SINGLESTEP
));
246 * If we came from a system call, handle the restart.
248 if (regs
->syscall_nr
>= 0) {
250 case -ERESTARTNOHAND
:
252 case -ERESTARTNOINTR
:
253 regs
->r06
= regs
->syscall_nr
;
255 case -ERESTART_RESTARTBLOCK
:
256 regs
->r06
= __NR_restart_syscall
;
261 pt_set_elr(regs
, pt_elr(regs
) - 4);
262 regs
->r00
= regs
->restart_r0
;
266 /* If there's no signal to deliver, put the saved sigmask back */
267 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
268 clear_thread_flag(TIF_RESTORE_SIGMASK
);
269 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
273 void do_notify_resume(struct pt_regs
*regs
, unsigned long thread_info_flags
)
275 if (thread_info_flags
& _TIF_SIGPENDING
)
278 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
279 clear_thread_flag(TIF_NOTIFY_RESUME
);
280 if (current
->replacement_session_keyring
)
281 key_replace_session_keyring();
286 * Architecture-specific wrappers for signal-related system calls
288 asmlinkage
int sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
)
290 struct pt_regs
*regs
= current_thread_info()->regs
;
292 return do_sigaltstack(uss
, uoss
, regs
->r29
);
295 asmlinkage
int sys_rt_sigreturn(void)
297 struct pt_regs
*regs
= current_thread_info()->regs
;
298 struct rt_sigframe __user
*frame
;
301 frame
= (struct rt_sigframe __user
*)pt_psp(regs
);
302 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
304 if (__copy_from_user(&blocked
, &frame
->uc
.uc_sigmask
, sizeof(blocked
)))
307 sigdelsetmask(&blocked
, ~_BLOCKABLE
);
308 spin_lock_irq(¤t
->sighand
->siglock
);
309 current
->blocked
= blocked
;
311 spin_unlock_irq(¤t
->sighand
->siglock
);
313 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
316 /* Restore the user's stack as well */
317 pt_psp(regs
) = regs
->r29
;
320 * Leave a trace in the stack frame that this was a sigreturn.
321 * If the system call is to replay, we've already restored the
322 * number in the GPR slot and it will be regenerated on the
323 * new system call trap entry. Note that if restore_sigcontext()
324 * did something other than a bulk copy of the pt_regs struct,
325 * we could avoid this assignment by simply not overwriting
328 regs
->syscall_nr
= __NR_rt_sigreturn
;
331 * If we were meticulous, we'd only call this if we knew that
332 * we were actually going to use an alternate stack, and we'd
333 * consider any error to be fatal. What we do here, in common
334 * with many other architectures, is call it blindly and only
335 * consider the -EFAULT return case to be proof of a problem.
337 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, pt_psp(regs
)) == -EFAULT
)
343 force_sig(SIGSEGV
, current
);