• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* MN10300 Signal handling
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11 
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/tty.h>
23 #include <linux/personality.h>
24 #include <linux/suspend.h>
25 #include <linux/tracehook.h>
26 #include <asm/cacheflush.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/fpu.h>
30 #include "sigframe.h"
31 
32 #define DEBUG_SIG 0
33 
34 /*
35  * do a signal return; undo the signal stack.
36  */
restore_sigcontext(struct pt_regs * regs,struct sigcontext __user * sc,long * _d0)37 static int restore_sigcontext(struct pt_regs *regs,
38 			      struct sigcontext __user *sc, long *_d0)
39 {
40 	unsigned int err = 0;
41 
42 	/* Always make any pending restarted system calls return -EINTR */
43 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
44 
45 	if (is_using_fpu(current))
46 		fpu_kill_state(current);
47 
48 #define COPY(x) err |= __get_user(regs->x, &sc->x)
49 	COPY(d1); COPY(d2); COPY(d3);
50 	COPY(a0); COPY(a1); COPY(a2); COPY(a3);
51 	COPY(e0); COPY(e1); COPY(e2); COPY(e3);
52 	COPY(e4); COPY(e5); COPY(e6); COPY(e7);
53 	COPY(lar); COPY(lir);
54 	COPY(mdr); COPY(mdrq);
55 	COPY(mcvf); COPY(mcrl); COPY(mcrh);
56 	COPY(sp); COPY(pc);
57 #undef COPY
58 
59 	{
60 		unsigned int tmpflags;
61 #ifndef CONFIG_MN10300_USING_JTAG
62 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
63 		   EPSW_T | EPSW_nAR)
64 #else
65 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
66 		   EPSW_nAR)
67 #endif
68 		err |= __get_user(tmpflags, &sc->epsw);
69 		regs->epsw = (regs->epsw & ~USER_EPSW) |
70 		  (tmpflags & USER_EPSW);
71 		regs->orig_d0 = -1;		/* disable syscall checks */
72 	}
73 
74 	{
75 		struct fpucontext *buf;
76 		err |= __get_user(buf, &sc->fpucontext);
77 		if (buf) {
78 			if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
79 				goto badframe;
80 			err |= fpu_restore_sigcontext(buf);
81 		}
82 	}
83 
84 	err |= __get_user(*_d0, &sc->d0);
85 	return err;
86 
87 badframe:
88 	return 1;
89 }
90 
91 /*
92  * standard signal return syscall
93  */
sys_sigreturn(void)94 asmlinkage long sys_sigreturn(void)
95 {
96 	struct sigframe __user *frame;
97 	sigset_t set;
98 	long d0;
99 
100 	frame = (struct sigframe __user *) current_frame()->sp;
101 	if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
102 		goto badframe;
103 	if (__get_user(set.sig[0], &frame->sc.oldmask))
104 		goto badframe;
105 
106 	if (_NSIG_WORDS > 1 &&
107 	    __copy_from_user(&set.sig[1], &frame->extramask,
108 			     sizeof(frame->extramask)))
109 		goto badframe;
110 
111 	set_current_blocked(&set);
112 
113 	if (restore_sigcontext(current_frame(), &frame->sc, &d0))
114 		goto badframe;
115 
116 	return d0;
117 
118 badframe:
119 	force_sig(SIGSEGV, current);
120 	return 0;
121 }
122 
123 /*
124  * realtime signal return syscall
125  */
sys_rt_sigreturn(void)126 asmlinkage long sys_rt_sigreturn(void)
127 {
128 	struct rt_sigframe __user *frame;
129 	sigset_t set;
130 	long d0;
131 
132 	frame = (struct rt_sigframe __user *) current_frame()->sp;
133 	if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
134 		goto badframe;
135 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
136 		goto badframe;
137 
138 	set_current_blocked(&set);
139 
140 	if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0))
141 		goto badframe;
142 
143 	if (restore_altstack(&frame->uc.uc_stack))
144 		goto badframe;
145 
146 	return d0;
147 
148 badframe:
149 	force_sig(SIGSEGV, current);
150 	return 0;
151 }
152 
153 /*
154  * store the userspace context into a signal frame
155  */
setup_sigcontext(struct sigcontext __user * sc,struct fpucontext * fpuctx,struct pt_regs * regs,unsigned long mask)156 static int setup_sigcontext(struct sigcontext __user *sc,
157 			    struct fpucontext *fpuctx,
158 			    struct pt_regs *regs,
159 			    unsigned long mask)
160 {
161 	int tmp, err = 0;
162 
163 #define COPY(x) err |= __put_user(regs->x, &sc->x)
164 	COPY(d0); COPY(d1); COPY(d2); COPY(d3);
165 	COPY(a0); COPY(a1); COPY(a2); COPY(a3);
166 	COPY(e0); COPY(e1); COPY(e2); COPY(e3);
167 	COPY(e4); COPY(e5); COPY(e6); COPY(e7);
168 	COPY(lar); COPY(lir);
169 	COPY(mdr); COPY(mdrq);
170 	COPY(mcvf); COPY(mcrl); COPY(mcrh);
171 	COPY(sp); COPY(epsw); COPY(pc);
172 #undef COPY
173 
174 	tmp = fpu_setup_sigcontext(fpuctx);
175 	if (tmp < 0)
176 		err = 1;
177 	else
178 		err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
179 
180 	/* non-iBCS2 extensions.. */
181 	err |= __put_user(mask, &sc->oldmask);
182 
183 	return err;
184 }
185 
186 /*
187  * determine which stack to use..
188  */
get_sigframe(struct k_sigaction * ka,struct pt_regs * regs,size_t frame_size)189 static inline void __user *get_sigframe(struct k_sigaction *ka,
190 					struct pt_regs *regs,
191 					size_t frame_size)
192 {
193 	unsigned long sp;
194 
195 	/* default to using normal stack */
196 	sp = regs->sp;
197 
198 	/* this is the X/Open sanctioned signal stack switching.  */
199 	if (ka->sa.sa_flags & SA_ONSTACK) {
200 		if (sas_ss_flags(sp) == 0)
201 			sp = current->sas_ss_sp + current->sas_ss_size;
202 	}
203 
204 	return (void __user *) ((sp - frame_size) & ~7UL);
205 }
206 
207 /*
208  * set up a normal signal frame
209  */
setup_frame(int sig,struct k_sigaction * ka,sigset_t * set,struct pt_regs * regs)210 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
211 		       struct pt_regs *regs)
212 {
213 	struct sigframe __user *frame;
214 	int rsig;
215 
216 	frame = get_sigframe(ka, regs, sizeof(*frame));
217 
218 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
219 		goto give_sigsegv;
220 
221 	rsig = sig;
222 	if (sig < 32 &&
223 	    current_thread_info()->exec_domain &&
224 	    current_thread_info()->exec_domain->signal_invmap)
225 		rsig = current_thread_info()->exec_domain->signal_invmap[sig];
226 
227 	if (__put_user(rsig, &frame->sig) < 0 ||
228 	    __put_user(&frame->sc, &frame->psc) < 0)
229 		goto give_sigsegv;
230 
231 	if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
232 		goto give_sigsegv;
233 
234 	if (_NSIG_WORDS > 1) {
235 		if (__copy_to_user(frame->extramask, &set->sig[1],
236 				   sizeof(frame->extramask)))
237 			goto give_sigsegv;
238 	}
239 
240 	/* set up to return from userspace.  If provided, use a stub already in
241 	 * userspace */
242 	if (ka->sa.sa_flags & SA_RESTORER) {
243 		if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
244 			goto give_sigsegv;
245 	} else {
246 		if (__put_user((void (*)(void))frame->retcode,
247 			       &frame->pretcode))
248 			goto give_sigsegv;
249 		/* this is mov $,d0; syscall 0 */
250 		if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
251 		    __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
252 		    __put_user(0x00, (char *)(frame->retcode + 2)) ||
253 		    __put_user(0xf0, (char *)(frame->retcode + 3)) ||
254 		    __put_user(0xe0, (char *)(frame->retcode + 4)))
255 			goto give_sigsegv;
256 		flush_icache_range((unsigned long) frame->retcode,
257 				   (unsigned long) frame->retcode + 5);
258 	}
259 
260 	/* set up registers for signal handler */
261 	regs->sp = (unsigned long) frame;
262 	regs->pc = (unsigned long) ka->sa.sa_handler;
263 	regs->d0 = sig;
264 	regs->d1 = (unsigned long) &frame->sc;
265 
266 #if DEBUG_SIG
267 	printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
268 	       sig, current->comm, current->pid, frame, regs->pc,
269 	       frame->pretcode);
270 #endif
271 
272 	return 0;
273 
274 give_sigsegv:
275 	force_sigsegv(sig, current);
276 	return -EFAULT;
277 }
278 
279 /*
280  * set up a realtime signal frame
281  */
setup_rt_frame(int sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * set,struct pt_regs * regs)282 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
283 			  sigset_t *set, struct pt_regs *regs)
284 {
285 	struct rt_sigframe __user *frame;
286 	int rsig;
287 
288 	frame = get_sigframe(ka, regs, sizeof(*frame));
289 
290 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
291 		goto give_sigsegv;
292 
293 	rsig = sig;
294 	if (sig < 32 &&
295 	    current_thread_info()->exec_domain &&
296 	    current_thread_info()->exec_domain->signal_invmap)
297 		rsig = current_thread_info()->exec_domain->signal_invmap[sig];
298 
299 	if (__put_user(rsig, &frame->sig) ||
300 	    __put_user(&frame->info, &frame->pinfo) ||
301 	    __put_user(&frame->uc, &frame->puc) ||
302 	    copy_siginfo_to_user(&frame->info, info))
303 		goto give_sigsegv;
304 
305 	/* create the ucontext.  */
306 	if (__put_user(0, &frame->uc.uc_flags) ||
307 	    __put_user(0, &frame->uc.uc_link) ||
308 	    __save_altstack(&frame->uc.uc_stack, regs->sp) ||
309 	    setup_sigcontext(&frame->uc.uc_mcontext,
310 			     &frame->fpuctx, regs, set->sig[0]) ||
311 	    __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
312 		goto give_sigsegv;
313 
314 	/* set up to return from userspace.  If provided, use a stub already in
315 	 * userspace */
316 	if (ka->sa.sa_flags & SA_RESTORER) {
317 		if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
318 			goto give_sigsegv;
319 	} else {
320 		if (__put_user((void(*)(void))frame->retcode,
321 			       &frame->pretcode) ||
322 		    /* This is mov $,d0; syscall 0 */
323 		    __put_user(0x2c, (char *)(frame->retcode + 0)) ||
324 		    __put_user(__NR_rt_sigreturn,
325 			       (char *)(frame->retcode + 1)) ||
326 		    __put_user(0x00, (char *)(frame->retcode + 2)) ||
327 		    __put_user(0xf0, (char *)(frame->retcode + 3)) ||
328 		    __put_user(0xe0, (char *)(frame->retcode + 4)))
329 			goto give_sigsegv;
330 
331 		flush_icache_range((u_long) frame->retcode,
332 				   (u_long) frame->retcode + 5);
333 	}
334 
335 	/* Set up registers for signal handler */
336 	regs->sp = (unsigned long) frame;
337 	regs->pc = (unsigned long) ka->sa.sa_handler;
338 	regs->d0 = sig;
339 	regs->d1 = (long) &frame->info;
340 
341 #if DEBUG_SIG
342 	printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
343 	       sig, current->comm, current->pid, frame, regs->pc,
344 	       frame->pretcode);
345 #endif
346 
347 	return 0;
348 
349 give_sigsegv:
350 	force_sigsegv(sig, current);
351 	return -EFAULT;
352 }
353 
stepback(struct pt_regs * regs)354 static inline void stepback(struct pt_regs *regs)
355 {
356 	regs->pc -= 2;
357 	regs->orig_d0 = -1;
358 }
359 
360 /*
361  * handle the actual delivery of a signal to userspace
362  */
handle_signal(int sig,siginfo_t * info,struct k_sigaction * ka,struct pt_regs * regs)363 static int handle_signal(int sig,
364 			 siginfo_t *info, struct k_sigaction *ka,
365 			 struct pt_regs *regs)
366 {
367 	sigset_t *oldset = sigmask_to_save();
368 	int ret;
369 
370 	/* Are we from a system call? */
371 	if (regs->orig_d0 >= 0) {
372 		/* If so, check system call restarting.. */
373 		switch (regs->d0) {
374 		case -ERESTART_RESTARTBLOCK:
375 		case -ERESTARTNOHAND:
376 			regs->d0 = -EINTR;
377 			break;
378 
379 		case -ERESTARTSYS:
380 			if (!(ka->sa.sa_flags & SA_RESTART)) {
381 				regs->d0 = -EINTR;
382 				break;
383 			}
384 
385 			/* fallthrough */
386 		case -ERESTARTNOINTR:
387 			regs->d0 = regs->orig_d0;
388 			stepback(regs);
389 		}
390 	}
391 
392 	/* Set up the stack frame */
393 	if (ka->sa.sa_flags & SA_SIGINFO)
394 		ret = setup_rt_frame(sig, ka, info, oldset, regs);
395 	else
396 		ret = setup_frame(sig, ka, oldset, regs);
397 	if (ret)
398 		return ret;
399 
400 	signal_delivered(sig, info, ka, regs,
401 			 test_thread_flag(TIF_SINGLESTEP));
402 	return 0;
403 }
404 
405 /*
406  * handle a potential signal
407  */
do_signal(struct pt_regs * regs)408 static void do_signal(struct pt_regs *regs)
409 {
410 	struct k_sigaction ka;
411 	siginfo_t info;
412 	int signr;
413 
414 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
415 	if (signr > 0) {
416 		if (handle_signal(signr, &info, &ka, regs) == 0) {
417 		}
418 
419 		return;
420 	}
421 
422 	/* did we come from a system call? */
423 	if (regs->orig_d0 >= 0) {
424 		/* restart the system call - no handlers present */
425 		switch (regs->d0) {
426 		case -ERESTARTNOHAND:
427 		case -ERESTARTSYS:
428 		case -ERESTARTNOINTR:
429 			regs->d0 = regs->orig_d0;
430 			stepback(regs);
431 			break;
432 
433 		case -ERESTART_RESTARTBLOCK:
434 			regs->d0 = __NR_restart_syscall;
435 			stepback(regs);
436 			break;
437 		}
438 	}
439 
440 	/* if there's no signal to deliver, we just put the saved sigmask
441 	 * back */
442 	restore_saved_sigmask();
443 }
444 
445 /*
446  * notification of userspace execution resumption
447  * - triggered by current->work.notify_resume
448  */
do_notify_resume(struct pt_regs * regs,u32 thread_info_flags)449 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
450 {
451 	/* Pending single-step? */
452 	if (thread_info_flags & _TIF_SINGLESTEP) {
453 #ifndef CONFIG_MN10300_USING_JTAG
454 		regs->epsw |= EPSW_T;
455 		clear_thread_flag(TIF_SINGLESTEP);
456 #else
457 		BUG(); /* no h/w single-step if using JTAG unit */
458 #endif
459 	}
460 
461 	/* deal with pending signal delivery */
462 	if (thread_info_flags & _TIF_SIGPENDING)
463 		do_signal(regs);
464 
465 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
466 		clear_thread_flag(TIF_NOTIFY_RESUME);
467 		tracehook_notify_resume(current_frame());
468 	}
469 }
470