xref: /linux/arch/hexagon/kernel/signal.c (revision 0d456bad36d42d16022be045c8a53ddbb59ee478)
1 /*
2  * Signal support for Hexagon processor
3  *
4  * Copyright (c) 2010-2011, 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 <asm/registers.h>
25 #include <asm/thread_info.h>
26 #include <asm/unistd.h>
27 #include <asm/uaccess.h>
28 #include <asm/ucontext.h>
29 #include <asm/cacheflush.h>
30 #include <asm/signal.h>
31 #include <asm/vdso.h>
32 
33 struct rt_sigframe {
34 	unsigned long tramp[2];
35 	struct siginfo info;
36 	struct ucontext uc;
37 };
38 
39 static void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
40 			  size_t frame_size)
41 {
42 	unsigned long sp = regs->r29;
43 
44 	/* Switch to signal stack if appropriate */
45 	if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
46 		sp = current->sas_ss_sp + current->sas_ss_size;
47 
48 	return (void __user *)((sp - frame_size) & ~(sizeof(long long) - 1));
49 }
50 
51 static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
52 {
53 	unsigned long tmp;
54 	int err = 0;
55 
56 	err |= copy_to_user(&sc->sc_regs.r0, &regs->r00,
57 			    32*sizeof(unsigned long));
58 
59 	err |= __put_user(regs->sa0, &sc->sc_regs.sa0);
60 	err |= __put_user(regs->lc0, &sc->sc_regs.lc0);
61 	err |= __put_user(regs->sa1, &sc->sc_regs.sa1);
62 	err |= __put_user(regs->lc1, &sc->sc_regs.lc1);
63 	err |= __put_user(regs->m0, &sc->sc_regs.m0);
64 	err |= __put_user(regs->m1, &sc->sc_regs.m1);
65 	err |= __put_user(regs->usr, &sc->sc_regs.usr);
66 	err |= __put_user(regs->preds, &sc->sc_regs.p3_0);
67 	err |= __put_user(regs->gp, &sc->sc_regs.gp);
68 	err |= __put_user(regs->ugp, &sc->sc_regs.ugp);
69 
70 	tmp = pt_elr(regs); err |= __put_user(tmp, &sc->sc_regs.pc);
71 	tmp = pt_cause(regs); err |= __put_user(tmp, &sc->sc_regs.cause);
72 	tmp = pt_badva(regs); err |= __put_user(tmp, &sc->sc_regs.badva);
73 
74 	return err;
75 }
76 
77 static int restore_sigcontext(struct pt_regs *regs,
78 			      struct sigcontext __user *sc)
79 {
80 	unsigned long tmp;
81 	int err = 0;
82 
83 	err |= copy_from_user(&regs->r00, &sc->sc_regs.r0,
84 			      32 * sizeof(unsigned long));
85 
86 	err |= __get_user(regs->sa0, &sc->sc_regs.sa0);
87 	err |= __get_user(regs->lc0, &sc->sc_regs.lc0);
88 	err |= __get_user(regs->sa1, &sc->sc_regs.sa1);
89 	err |= __get_user(regs->lc1, &sc->sc_regs.lc1);
90 	err |= __get_user(regs->m0, &sc->sc_regs.m0);
91 	err |= __get_user(regs->m1, &sc->sc_regs.m1);
92 	err |= __get_user(regs->usr, &sc->sc_regs.usr);
93 	err |= __get_user(regs->preds, &sc->sc_regs.p3_0);
94 	err |= __get_user(regs->gp, &sc->sc_regs.gp);
95 	err |= __get_user(regs->ugp, &sc->sc_regs.ugp);
96 
97 	err |= __get_user(tmp, &sc->sc_regs.pc); pt_set_elr(regs, tmp);
98 
99 	return err;
100 }
101 
102 /*
103  * Setup signal stack frame with siginfo structure
104  */
105 static int setup_rt_frame(int signr, struct k_sigaction *ka, siginfo_t *info,
106 			  sigset_t *set,  struct pt_regs *regs)
107 {
108 	int err = 0;
109 	struct rt_sigframe __user *frame;
110 	struct hexagon_vdso *vdso = current->mm->context.vdso;
111 
112 	frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe));
113 
114 	if (!access_ok(VERIFY_WRITE, frame, sizeof(struct rt_sigframe)))
115 		goto	sigsegv;
116 
117 	if (copy_siginfo_to_user(&frame->info, info))
118 		goto	sigsegv;
119 
120 	/* The on-stack signal trampoline is no longer executed;
121 	 * however, the libgcc signal frame unwinding code checks for
122 	 * the presence of these two numeric magic values.
123 	 */
124 	err |= __put_user(0x7800d166, &frame->tramp[0]);
125 	err |= __put_user(0x5400c004, &frame->tramp[1]);
126 	err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
127 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
128 	if (err)
129 		goto sigsegv;
130 
131 	/* Load r0/r1 pair with signumber/siginfo pointer... */
132 	regs->r0100 = ((unsigned long long)((unsigned long)&frame->info) << 32)
133 		| (unsigned long long)signr;
134 	regs->r02 = (unsigned long) &frame->uc;
135 	regs->r31 = (unsigned long) vdso->rt_signal_trampoline;
136 	pt_psp(regs) = (unsigned long) frame;
137 	pt_set_elr(regs, (unsigned long)ka->sa.sa_handler);
138 
139 	return 0;
140 
141 sigsegv:
142 	force_sigsegv(signr, current);
143 	return -EFAULT;
144 }
145 
146 /*
147  * Setup invocation of signal handler
148  */
149 static void handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
150 			 struct pt_regs *regs)
151 {
152 	/*
153 	 * If we're handling a signal that aborted a system call,
154 	 * set up the error return value before adding the signal
155 	 * frame to the stack.
156 	 */
157 
158 	if (regs->syscall_nr >= 0) {
159 		switch (regs->r00) {
160 		case -ERESTART_RESTARTBLOCK:
161 		case -ERESTARTNOHAND:
162 			regs->r00 = -EINTR;
163 			break;
164 		case -ERESTARTSYS:
165 			if (!(ka->sa.sa_flags & SA_RESTART)) {
166 				regs->r00 = -EINTR;
167 				break;
168 			}
169 			/* Fall through */
170 		case -ERESTARTNOINTR:
171 			regs->r06 = regs->syscall_nr;
172 			pt_set_elr(regs, pt_elr(regs) - 4);
173 			regs->r00 = regs->restart_r0;
174 			break;
175 		default:
176 			break;
177 		}
178 	}
179 
180 	/*
181 	 * Set up the stack frame; not doing the SA_SIGINFO thing.  We
182 	 * only set up the rt_frame flavor.
183 	 */
184 	/* If there was an error on setup, no signal was delivered. */
185 	if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0)
186 		return;
187 
188 	signal_delivered(sig, info, ka, regs,
189 			test_thread_flag(TIF_SINGLESTEP));
190 }
191 
192 /*
193  * Called from return-from-event code.
194  */
195 static void do_signal(struct pt_regs *regs)
196 {
197 	struct k_sigaction sigact;
198 	siginfo_t info;
199 	int signo;
200 
201 	if (!user_mode(regs))
202 		return;
203 
204 	signo = get_signal_to_deliver(&info, &sigact, regs, NULL);
205 
206 	if (signo > 0) {
207 		handle_signal(signo, &info, &sigact, regs);
208 		return;
209 	}
210 
211 	/*
212 	 * If we came from a system call, handle the restart.
213 	 */
214 	if (regs->syscall_nr >= 0) {
215 		switch (regs->r00) {
216 		case -ERESTARTNOHAND:
217 		case -ERESTARTSYS:
218 		case -ERESTARTNOINTR:
219 			regs->r06 = regs->syscall_nr;
220 			break;
221 		case -ERESTART_RESTARTBLOCK:
222 			regs->r06 = __NR_restart_syscall;
223 			break;
224 		default:
225 			goto no_restart;
226 		}
227 		pt_set_elr(regs, pt_elr(regs) - 4);
228 		regs->r00 = regs->restart_r0;
229 	}
230 
231 no_restart:
232 	/* If there's no signal to deliver, put the saved sigmask back */
233 	restore_saved_sigmask();
234 }
235 
236 void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
237 {
238 	if (thread_info_flags & _TIF_SIGPENDING)
239 		do_signal(regs);
240 
241 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
242 		clear_thread_flag(TIF_NOTIFY_RESUME);
243 		tracehook_notify_resume(regs);
244 	}
245 }
246 
247 /*
248  * Architecture-specific wrappers for signal-related system calls
249  */
250 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
251 {
252 	struct pt_regs *regs = current_pt_regs();
253 
254 	return do_sigaltstack(uss, uoss, regs->r29);
255 }
256 
257 asmlinkage int sys_rt_sigreturn(void)
258 {
259 	struct pt_regs *regs = current_pt_regs();
260 	struct rt_sigframe __user *frame;
261 	sigset_t blocked;
262 
263 	/* Always make any pending restarted system calls return -EINTR */
264 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
265 
266 	frame = (struct rt_sigframe __user *)pt_psp(regs);
267 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
268 		goto badframe;
269 	if (__copy_from_user(&blocked, &frame->uc.uc_sigmask, sizeof(blocked)))
270 		goto badframe;
271 
272 	set_current_blocked(&blocked);
273 
274 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
275 		goto badframe;
276 
277 	/* Restore the user's stack as well */
278 	pt_psp(regs) = regs->r29;
279 
280 	/*
281 	 * Leave a trace in the stack frame that this was a sigreturn.
282 	 * If the system call is to replay, we've already restored the
283 	 * number in the GPR slot and it will be regenerated on the
284 	 * new system call trap entry. Note that if restore_sigcontext()
285 	 * did something other than a bulk copy of the pt_regs struct,
286 	 * we could avoid this assignment by simply not overwriting
287 	 * regs->syscall_nr.
288 	 */
289 	regs->syscall_nr = __NR_rt_sigreturn;
290 
291 	/*
292 	 * If we were meticulous, we'd only call this if we knew that
293 	 * we were actually going to use an alternate stack, and we'd
294 	 * consider any error to be fatal.  What we do here, in common
295 	 * with many other architectures, is call it blindly and only
296 	 * consider the -EFAULT return case to be proof of a problem.
297 	 */
298 	if (do_sigaltstack(&frame->uc.uc_stack, NULL, pt_psp(regs)) == -EFAULT)
299 		goto badframe;
300 
301 	return 0;
302 
303 badframe:
304 	force_sig(SIGSEGV, current);
305 	return 0;
306 }
307