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