1 /*
2 * Signal support for Hexagon processor
3 *
4 * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
5 *
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.
9 *
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.
14 *
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
18 * 02110-1301, USA.
19 */
20
21 #include <linux/linkage.h>
22 #include <linux/syscalls.h>
23 #include <linux/tracehook.h>
24 #include <linux/sched/task_stack.h>
25
26 #include <asm/registers.h>
27 #include <asm/thread_info.h>
28 #include <asm/unistd.h>
29 #include <linux/uaccess.h>
30 #include <asm/ucontext.h>
31 #include <asm/cacheflush.h>
32 #include <asm/signal.h>
33 #include <asm/vdso.h>
34
35 struct rt_sigframe {
36 unsigned long tramp[2];
37 struct siginfo info;
38 struct ucontext uc;
39 };
40
get_sigframe(struct ksignal * ksig,struct pt_regs * regs,size_t frame_size)41 static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
42 size_t frame_size)
43 {
44 unsigned long sp = sigsp(regs->r29, ksig);
45
46 return (void __user *)((sp - frame_size) & ~(sizeof(long long) - 1));
47 }
48
setup_sigcontext(struct pt_regs * regs,struct sigcontext __user * sc)49 static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
50 {
51 unsigned long tmp;
52 int err = 0;
53
54 err |= copy_to_user(&sc->sc_regs.r0, ®s->r00,
55 32*sizeof(unsigned long));
56
57 err |= __put_user(regs->sa0, &sc->sc_regs.sa0);
58 err |= __put_user(regs->lc0, &sc->sc_regs.lc0);
59 err |= __put_user(regs->sa1, &sc->sc_regs.sa1);
60 err |= __put_user(regs->lc1, &sc->sc_regs.lc1);
61 err |= __put_user(regs->m0, &sc->sc_regs.m0);
62 err |= __put_user(regs->m1, &sc->sc_regs.m1);
63 err |= __put_user(regs->usr, &sc->sc_regs.usr);
64 err |= __put_user(regs->preds, &sc->sc_regs.p3_0);
65 err |= __put_user(regs->gp, &sc->sc_regs.gp);
66 err |= __put_user(regs->ugp, &sc->sc_regs.ugp);
67 #if CONFIG_HEXAGON_ARCH_VERSION >= 4
68 err |= __put_user(regs->cs0, &sc->sc_regs.cs0);
69 err |= __put_user(regs->cs1, &sc->sc_regs.cs1);
70 #endif
71 tmp = pt_elr(regs); err |= __put_user(tmp, &sc->sc_regs.pc);
72 tmp = pt_cause(regs); err |= __put_user(tmp, &sc->sc_regs.cause);
73 tmp = pt_badva(regs); err |= __put_user(tmp, &sc->sc_regs.badva);
74
75 return err;
76 }
77
restore_sigcontext(struct pt_regs * regs,struct sigcontext __user * sc)78 static int restore_sigcontext(struct pt_regs *regs,
79 struct sigcontext __user *sc)
80 {
81 unsigned long tmp;
82 int err = 0;
83
84 err |= copy_from_user(®s->r00, &sc->sc_regs.r0,
85 32 * sizeof(unsigned long));
86
87 err |= __get_user(regs->sa0, &sc->sc_regs.sa0);
88 err |= __get_user(regs->lc0, &sc->sc_regs.lc0);
89 err |= __get_user(regs->sa1, &sc->sc_regs.sa1);
90 err |= __get_user(regs->lc1, &sc->sc_regs.lc1);
91 err |= __get_user(regs->m0, &sc->sc_regs.m0);
92 err |= __get_user(regs->m1, &sc->sc_regs.m1);
93 err |= __get_user(regs->usr, &sc->sc_regs.usr);
94 err |= __get_user(regs->preds, &sc->sc_regs.p3_0);
95 err |= __get_user(regs->gp, &sc->sc_regs.gp);
96 err |= __get_user(regs->ugp, &sc->sc_regs.ugp);
97 #if CONFIG_HEXAGON_ARCH_VERSION >= 4
98 err |= __get_user(regs->cs0, &sc->sc_regs.cs0);
99 err |= __get_user(regs->cs1, &sc->sc_regs.cs1);
100 #endif
101 err |= __get_user(tmp, &sc->sc_regs.pc); pt_set_elr(regs, tmp);
102
103 return err;
104 }
105
106 /*
107 * Setup signal stack frame with siginfo structure
108 */
setup_rt_frame(struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)109 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
110 struct pt_regs *regs)
111 {
112 int err = 0;
113 struct rt_sigframe __user *frame;
114 struct hexagon_vdso *vdso = current->mm->context.vdso;
115
116 frame = get_sigframe(ksig, regs, sizeof(struct rt_sigframe));
117
118 if (!access_ok(VERIFY_WRITE, frame, sizeof(struct rt_sigframe)))
119 return -EFAULT;
120
121 if (copy_siginfo_to_user(&frame->info, &ksig->info))
122 return -EFAULT;
123
124 /* The on-stack signal trampoline is no longer executed;
125 * however, the libgcc signal frame unwinding code checks for
126 * the presence of these two numeric magic values.
127 */
128 err |= __put_user(0x7800d166, &frame->tramp[0]);
129 err |= __put_user(0x5400c004, &frame->tramp[1]);
130 err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
131 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
132 err |= __save_altstack(&frame->uc.uc_stack, user_stack_pointer(regs));
133 if (err)
134 return -EFAULT;
135
136 /* Load r0/r1 pair with signumber/siginfo pointer... */
137 regs->r0100 = ((unsigned long long)((unsigned long)&frame->info) << 32)
138 | (unsigned long long)ksig->sig;
139 regs->r02 = (unsigned long) &frame->uc;
140 regs->r31 = (unsigned long) vdso->rt_signal_trampoline;
141 pt_psp(regs) = (unsigned long) frame;
142 pt_set_elr(regs, (unsigned long)ksig->ka.sa.sa_handler);
143
144 return 0;
145 }
146
147 /*
148 * Setup invocation of signal handler
149 */
handle_signal(struct ksignal * ksig,struct pt_regs * regs)150 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
151 {
152 int ret;
153
154 /*
155 * If we're handling a signal that aborted a system call,
156 * set up the error return value before adding the signal
157 * frame to the stack.
158 */
159
160 if (regs->syscall_nr >= 0) {
161 switch (regs->r00) {
162 case -ERESTART_RESTARTBLOCK:
163 case -ERESTARTNOHAND:
164 regs->r00 = -EINTR;
165 break;
166 case -ERESTARTSYS:
167 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
168 regs->r00 = -EINTR;
169 break;
170 }
171 /* Fall through */
172 case -ERESTARTNOINTR:
173 regs->r06 = regs->syscall_nr;
174 pt_set_elr(regs, pt_elr(regs) - 4);
175 regs->r00 = regs->restart_r0;
176 break;
177 default:
178 break;
179 }
180 }
181
182 /*
183 * Set up the stack frame; not doing the SA_SIGINFO thing. We
184 * only set up the rt_frame flavor.
185 */
186 /* If there was an error on setup, no signal was delivered. */
187 ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
188
189 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
190 }
191
192 /*
193 * Called from return-from-event code.
194 */
do_signal(struct pt_regs * regs)195 void do_signal(struct pt_regs *regs)
196 {
197 struct ksignal ksig;
198
199 if (!user_mode(regs))
200 return;
201
202 if (get_signal(&ksig)) {
203 handle_signal(&ksig, regs);
204 return;
205 }
206
207 /*
208 * No (more) signals; if we came from a system call, handle the restart.
209 */
210
211 if (regs->syscall_nr >= 0) {
212 switch (regs->r00) {
213 case -ERESTARTNOHAND:
214 case -ERESTARTSYS:
215 case -ERESTARTNOINTR:
216 regs->r06 = regs->syscall_nr;
217 break;
218 case -ERESTART_RESTARTBLOCK:
219 regs->r06 = __NR_restart_syscall;
220 break;
221 default:
222 goto no_restart;
223 }
224 pt_set_elr(regs, pt_elr(regs) - 4);
225 regs->r00 = regs->restart_r0;
226 }
227
228 no_restart:
229 /* If there's no signal to deliver, put the saved sigmask back */
230 restore_saved_sigmask();
231 }
232
233 /*
234 * Architecture-specific wrappers for signal-related system calls
235 */
236
sys_rt_sigreturn(void)237 asmlinkage int sys_rt_sigreturn(void)
238 {
239 struct pt_regs *regs = current_pt_regs();
240 struct rt_sigframe __user *frame;
241 sigset_t blocked;
242
243 /* Always make any pending restarted system calls return -EINTR */
244 current->restart_block.fn = do_no_restart_syscall;
245
246 frame = (struct rt_sigframe __user *)pt_psp(regs);
247 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
248 goto badframe;
249 if (__copy_from_user(&blocked, &frame->uc.uc_sigmask, sizeof(blocked)))
250 goto badframe;
251
252 set_current_blocked(&blocked);
253
254 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
255 goto badframe;
256
257 /* Restore the user's stack as well */
258 pt_psp(regs) = regs->r29;
259
260 regs->syscall_nr = -1;
261
262 if (restore_altstack(&frame->uc.uc_stack))
263 goto badframe;
264
265 return regs->r00;
266
267 badframe:
268 force_sig(SIGSEGV, current);
269 return 0;
270 }
271