xref: /openbmc/linux/arch/m68k/kernel/signal.c (revision b845b574)
1 /*
2  *  linux/arch/m68k/kernel/signal.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file COPYING in the main directory of this archive
8  * for more details.
9  */
10 
11 /*
12  * Linux/m68k support by Hamish Macdonald
13  *
14  * 68060 fixes by Jesper Skov
15  *
16  * 1997-12-01  Modified for POSIX.1b signals by Andreas Schwab
17  *
18  * mathemu support by Roman Zippel
19  *  (Note: fpstate in the signal context is completely ignored for the emulator
20  *         and the internal floating point format is put on stack)
21  */
22 
23 /*
24  * ++roman (07/09/96): implemented signal stacks (specially for tosemu on
25  * Atari :-) Current limitation: Only one sigstack can be active at one time.
26  * If a second signal with SA_ONSTACK set arrives while working on a sigstack,
27  * SA_ONSTACK is ignored. This behaviour avoids lots of trouble with nested
28  * signal handlers!
29  */
30 
31 #include <linux/sched.h>
32 #include <linux/mm.h>
33 #include <linux/kernel.h>
34 #include <linux/signal.h>
35 #include <linux/syscalls.h>
36 #include <linux/errno.h>
37 #include <linux/wait.h>
38 #include <linux/ptrace.h>
39 #include <linux/unistd.h>
40 #include <linux/stddef.h>
41 #include <linux/highuid.h>
42 #include <linux/personality.h>
43 #include <linux/tty.h>
44 #include <linux/binfmts.h>
45 #include <linux/extable.h>
46 #include <linux/resume_user_mode.h>
47 
48 #include <asm/setup.h>
49 #include <linux/uaccess.h>
50 #include <asm/traps.h>
51 #include <asm/ucontext.h>
52 #include <asm/cacheflush.h>
53 
54 #ifdef CONFIG_MMU
55 
56 /*
57  * Handle the slight differences in classic 68k and ColdFire trap frames.
58  */
59 #ifdef CONFIG_COLDFIRE
60 #define	FORMAT		4
61 #define	FMT4SIZE	0
62 #else
63 #define	FORMAT		0
64 #define	FMT4SIZE	sizeof_field(struct frame, un.fmt4)
65 #endif
66 
67 static const int frame_size_change[16] = {
68   [1]	= -1, /* sizeof_field(struct frame, un.fmt1), */
69   [2]	= sizeof_field(struct frame, un.fmt2),
70   [3]	= sizeof_field(struct frame, un.fmt3),
71   [4]	= FMT4SIZE,
72   [5]	= -1, /* sizeof_field(struct frame, un.fmt5), */
73   [6]	= -1, /* sizeof_field(struct frame, un.fmt6), */
74   [7]	= sizeof_field(struct frame, un.fmt7),
75   [8]	= -1, /* sizeof_field(struct frame, un.fmt8), */
76   [9]	= sizeof_field(struct frame, un.fmt9),
77   [10]	= sizeof_field(struct frame, un.fmta),
78   [11]	= sizeof_field(struct frame, un.fmtb),
79   [12]	= -1, /* sizeof_field(struct frame, un.fmtc), */
80   [13]	= -1, /* sizeof_field(struct frame, un.fmtd), */
81   [14]	= -1, /* sizeof_field(struct frame, un.fmte), */
82   [15]	= -1, /* sizeof_field(struct frame, un.fmtf), */
83 };
84 
frame_extra_sizes(int f)85 static inline int frame_extra_sizes(int f)
86 {
87 	return frame_size_change[f];
88 }
89 
fixup_exception(struct pt_regs * regs)90 int fixup_exception(struct pt_regs *regs)
91 {
92 	const struct exception_table_entry *fixup;
93 	struct pt_regs *tregs;
94 
95 	/* Are we prepared to handle this kernel fault? */
96 	fixup = search_exception_tables(regs->pc);
97 	if (!fixup)
98 		return 0;
99 
100 	/* Create a new four word stack frame, discarding the old one. */
101 	regs->stkadj = frame_extra_sizes(regs->format);
102 	tregs =	(struct pt_regs *)((long)regs + regs->stkadj);
103 	tregs->vector = regs->vector;
104 	tregs->format = FORMAT;
105 	tregs->pc = fixup->fixup;
106 	tregs->sr = regs->sr;
107 
108 	return 1;
109 }
110 
push_cache(unsigned long vaddr)111 static inline void push_cache (unsigned long vaddr)
112 {
113 	/*
114 	 * Using the old cache_push_v() was really a big waste.
115 	 *
116 	 * What we are trying to do is to flush 8 bytes to ram.
117 	 * Flushing 2 cache lines of 16 bytes is much cheaper than
118 	 * flushing 1 or 2 pages, as previously done in
119 	 * cache_push_v().
120 	 *                                                     Jes
121 	 */
122 	if (CPU_IS_040) {
123 		unsigned long temp;
124 
125 		__asm__ __volatile__ (".chip 68040\n\t"
126 				      "nop\n\t"
127 				      "ptestr (%1)\n\t"
128 				      "movec %%mmusr,%0\n\t"
129 				      ".chip 68k"
130 				      : "=r" (temp)
131 				      : "a" (vaddr));
132 
133 		temp &= PAGE_MASK;
134 		temp |= vaddr & ~PAGE_MASK;
135 
136 		__asm__ __volatile__ (".chip 68040\n\t"
137 				      "nop\n\t"
138 				      "cpushl %%bc,(%0)\n\t"
139 				      ".chip 68k"
140 				      : : "a" (temp));
141 	}
142 	else if (CPU_IS_060) {
143 		unsigned long temp;
144 		__asm__ __volatile__ (".chip 68060\n\t"
145 				      "plpar (%0)\n\t"
146 				      ".chip 68k"
147 				      : "=a" (temp)
148 				      : "0" (vaddr));
149 		__asm__ __volatile__ (".chip 68060\n\t"
150 				      "cpushl %%bc,(%0)\n\t"
151 				      ".chip 68k"
152 				      : : "a" (temp));
153 	} else if (!CPU_IS_COLDFIRE) {
154 		/*
155 		 * 68030/68020 have no writeback cache;
156 		 * still need to clear icache.
157 		 * Note that vaddr is guaranteed to be long word aligned.
158 		 */
159 		unsigned long temp;
160 		asm volatile ("movec %%cacr,%0" : "=r" (temp));
161 		temp += 4;
162 		asm volatile ("movec %0,%%caar\n\t"
163 			      "movec %1,%%cacr"
164 			      : : "r" (vaddr), "r" (temp));
165 		asm volatile ("movec %0,%%caar\n\t"
166 			      "movec %1,%%cacr"
167 			      : : "r" (vaddr + 4), "r" (temp));
168 	} else {
169 		/* CPU_IS_COLDFIRE */
170 #if defined(CONFIG_CACHE_COPYBACK)
171 		flush_cf_dcache(0, DCACHE_MAX_ADDR);
172 #endif
173 		/* Invalidate instruction cache for the pushed bytes */
174 		clear_cf_icache(vaddr, vaddr + 8);
175 	}
176 }
177 
adjustformat(struct pt_regs * regs)178 static inline void adjustformat(struct pt_regs *regs)
179 {
180 }
181 
save_a5_state(struct sigcontext * sc,struct pt_regs * regs)182 static inline void save_a5_state(struct sigcontext *sc, struct pt_regs *regs)
183 {
184 }
185 
186 #else /* CONFIG_MMU */
187 
188 void ret_from_user_signal(void);
189 void ret_from_user_rt_signal(void);
190 
frame_extra_sizes(int f)191 static inline int frame_extra_sizes(int f)
192 {
193 	/* No frame size adjustments required on non-MMU CPUs */
194 	return 0;
195 }
196 
adjustformat(struct pt_regs * regs)197 static inline void adjustformat(struct pt_regs *regs)
198 {
199 	/*
200 	 * set format byte to make stack appear modulo 4, which it will
201 	 * be when doing the rte
202 	 */
203 	regs->format = 0x4;
204 }
205 
save_a5_state(struct sigcontext * sc,struct pt_regs * regs)206 static inline void save_a5_state(struct sigcontext *sc, struct pt_regs *regs)
207 {
208 	sc->sc_a5 = ((struct switch_stack *)regs - 1)->a5;
209 }
210 
push_cache(unsigned long vaddr)211 static inline void push_cache(unsigned long vaddr)
212 {
213 }
214 
215 #endif /* CONFIG_MMU */
216 
217 /*
218  * Do a signal return; undo the signal stack.
219  *
220  * Keep the return code on the stack quadword aligned!
221  * That makes the cache flush below easier.
222  */
223 
224 struct sigframe
225 {
226 	char __user *pretcode;
227 	int sig;
228 	int code;
229 	struct sigcontext __user *psc;
230 	char retcode[8];
231 	unsigned long extramask[_NSIG_WORDS-1];
232 	struct sigcontext sc;
233 };
234 
235 struct rt_sigframe
236 {
237 	char __user *pretcode;
238 	int sig;
239 	struct siginfo __user *pinfo;
240 	void __user *puc;
241 	char retcode[8];
242 	struct siginfo info;
243 	struct ucontext uc;
244 };
245 
246 #define FPCONTEXT_SIZE	216
247 #define uc_fpstate	uc_filler[0]
248 #define uc_formatvec	uc_filler[FPCONTEXT_SIZE/4]
249 #define uc_extra	uc_filler[FPCONTEXT_SIZE/4+1]
250 
251 #ifdef CONFIG_FPU
252 
253 static unsigned char fpu_version;	/* version number of fpu, set by setup_frame */
254 
restore_fpu_state(struct sigcontext * sc)255 static inline int restore_fpu_state(struct sigcontext *sc)
256 {
257 	int err = 1;
258 
259 	if (FPU_IS_EMU) {
260 	    /* restore registers */
261 	    memcpy(current->thread.fpcntl, sc->sc_fpcntl, 12);
262 	    memcpy(current->thread.fp, sc->sc_fpregs, 24);
263 	    return 0;
264 	}
265 
266 	if (CPU_IS_060 ? sc->sc_fpstate[2] : sc->sc_fpstate[0]) {
267 	    /* Verify the frame format.  */
268 	    if (!(CPU_IS_060 || CPU_IS_COLDFIRE) &&
269 		 (sc->sc_fpstate[0] != fpu_version))
270 		goto out;
271 	    if (CPU_IS_020_OR_030) {
272 		if (m68k_fputype & FPU_68881 &&
273 		    !(sc->sc_fpstate[1] == 0x18 || sc->sc_fpstate[1] == 0xb4))
274 		    goto out;
275 		if (m68k_fputype & FPU_68882 &&
276 		    !(sc->sc_fpstate[1] == 0x38 || sc->sc_fpstate[1] == 0xd4))
277 		    goto out;
278 	    } else if (CPU_IS_040) {
279 		if (!(sc->sc_fpstate[1] == 0x00 ||
280                       sc->sc_fpstate[1] == 0x28 ||
281                       sc->sc_fpstate[1] == 0x60))
282 		    goto out;
283 	    } else if (CPU_IS_060) {
284 		if (!(sc->sc_fpstate[3] == 0x00 ||
285                       sc->sc_fpstate[3] == 0x60 ||
286 		      sc->sc_fpstate[3] == 0xe0))
287 		    goto out;
288 	    } else if (CPU_IS_COLDFIRE) {
289 		if (!(sc->sc_fpstate[0] == 0x00 ||
290 		      sc->sc_fpstate[0] == 0x05 ||
291 		      sc->sc_fpstate[0] == 0xe5))
292 		    goto out;
293 	    } else
294 		goto out;
295 
296 	    if (CPU_IS_COLDFIRE) {
297 		__asm__ volatile ("fmovemd %0,%%fp0-%%fp1\n\t"
298 				  "fmovel %1,%%fpcr\n\t"
299 				  "fmovel %2,%%fpsr\n\t"
300 				  "fmovel %3,%%fpiar"
301 				  : /* no outputs */
302 				  : "m" (sc->sc_fpregs[0]),
303 				    "m" (sc->sc_fpcntl[0]),
304 				    "m" (sc->sc_fpcntl[1]),
305 				    "m" (sc->sc_fpcntl[2]));
306 	    } else {
307 		__asm__ volatile (".chip 68k/68881\n\t"
308 				  "fmovemx %0,%%fp0-%%fp1\n\t"
309 				  "fmoveml %1,%%fpcr/%%fpsr/%%fpiar\n\t"
310 				  ".chip 68k"
311 				  : /* no outputs */
312 				  : "m" (*sc->sc_fpregs),
313 				    "m" (*sc->sc_fpcntl));
314 	    }
315 	}
316 
317 	if (CPU_IS_COLDFIRE) {
318 		__asm__ volatile ("frestore %0" : : "m" (*sc->sc_fpstate));
319 	} else {
320 		__asm__ volatile (".chip 68k/68881\n\t"
321 				  "frestore %0\n\t"
322 				  ".chip 68k"
323 				  : : "m" (*sc->sc_fpstate));
324 	}
325 	err = 0;
326 
327 out:
328 	return err;
329 }
330 
rt_restore_fpu_state(struct ucontext __user * uc)331 static inline int rt_restore_fpu_state(struct ucontext __user *uc)
332 {
333 	unsigned char fpstate[FPCONTEXT_SIZE];
334 	int context_size = CPU_IS_060 ? 8 : (CPU_IS_COLDFIRE ? 12 : 0);
335 	fpregset_t fpregs;
336 	int err = 1;
337 
338 	if (FPU_IS_EMU) {
339 		/* restore fpu control register */
340 		if (__copy_from_user(current->thread.fpcntl,
341 				uc->uc_mcontext.fpregs.f_fpcntl, 12))
342 			goto out;
343 		/* restore all other fpu register */
344 		if (__copy_from_user(current->thread.fp,
345 				uc->uc_mcontext.fpregs.f_fpregs, 96))
346 			goto out;
347 		return 0;
348 	}
349 
350 	if (__get_user(*(long *)fpstate, (long __user *)&uc->uc_fpstate))
351 		goto out;
352 	if (CPU_IS_060 ? fpstate[2] : fpstate[0]) {
353 		if (!(CPU_IS_060 || CPU_IS_COLDFIRE))
354 			context_size = fpstate[1];
355 		/* Verify the frame format.  */
356 		if (!(CPU_IS_060 || CPU_IS_COLDFIRE) &&
357 		     (fpstate[0] != fpu_version))
358 			goto out;
359 		if (CPU_IS_020_OR_030) {
360 			if (m68k_fputype & FPU_68881 &&
361 			    !(context_size == 0x18 || context_size == 0xb4))
362 				goto out;
363 			if (m68k_fputype & FPU_68882 &&
364 			    !(context_size == 0x38 || context_size == 0xd4))
365 				goto out;
366 		} else if (CPU_IS_040) {
367 			if (!(context_size == 0x00 ||
368 			      context_size == 0x28 ||
369 			      context_size == 0x60))
370 				goto out;
371 		} else if (CPU_IS_060) {
372 			if (!(fpstate[3] == 0x00 ||
373 			      fpstate[3] == 0x60 ||
374 			      fpstate[3] == 0xe0))
375 				goto out;
376 		} else if (CPU_IS_COLDFIRE) {
377 			if (!(fpstate[3] == 0x00 ||
378 			      fpstate[3] == 0x05 ||
379 			      fpstate[3] == 0xe5))
380 				goto out;
381 		} else
382 			goto out;
383 		if (__copy_from_user(&fpregs, &uc->uc_mcontext.fpregs,
384 				     sizeof(fpregs)))
385 			goto out;
386 
387 		if (CPU_IS_COLDFIRE) {
388 			__asm__ volatile ("fmovemd %0,%%fp0-%%fp7\n\t"
389 					  "fmovel %1,%%fpcr\n\t"
390 					  "fmovel %2,%%fpsr\n\t"
391 					  "fmovel %3,%%fpiar"
392 					  : /* no outputs */
393 					  : "m" (fpregs.f_fpregs[0]),
394 					    "m" (fpregs.f_fpcntl[0]),
395 					    "m" (fpregs.f_fpcntl[1]),
396 					    "m" (fpregs.f_fpcntl[2]));
397 		} else {
398 			__asm__ volatile (".chip 68k/68881\n\t"
399 					  "fmovemx %0,%%fp0-%%fp7\n\t"
400 					  "fmoveml %1,%%fpcr/%%fpsr/%%fpiar\n\t"
401 					  ".chip 68k"
402 					  : /* no outputs */
403 					  : "m" (*fpregs.f_fpregs),
404 					    "m" (*fpregs.f_fpcntl));
405 		}
406 	}
407 	if (context_size &&
408 	    __copy_from_user(fpstate + 4, (long __user *)&uc->uc_fpstate + 1,
409 			     context_size))
410 		goto out;
411 
412 	if (CPU_IS_COLDFIRE) {
413 		__asm__ volatile ("frestore %0" : : "m" (*fpstate));
414 	} else {
415 		__asm__ volatile (".chip 68k/68881\n\t"
416 				  "frestore %0\n\t"
417 				  ".chip 68k"
418 				  : : "m" (*fpstate));
419 	}
420 	err = 0;
421 
422 out:
423 	return err;
424 }
425 
426 /*
427  * Set up a signal frame.
428  */
save_fpu_state(struct sigcontext * sc,struct pt_regs * regs)429 static inline void save_fpu_state(struct sigcontext *sc, struct pt_regs *regs)
430 {
431 	if (FPU_IS_EMU) {
432 		/* save registers */
433 		memcpy(sc->sc_fpcntl, current->thread.fpcntl, 12);
434 		memcpy(sc->sc_fpregs, current->thread.fp, 24);
435 		return;
436 	}
437 
438 	if (CPU_IS_COLDFIRE) {
439 		__asm__ volatile ("fsave %0"
440 				  : : "m" (*sc->sc_fpstate) : "memory");
441 	} else {
442 		__asm__ volatile (".chip 68k/68881\n\t"
443 				  "fsave %0\n\t"
444 				  ".chip 68k"
445 				  : : "m" (*sc->sc_fpstate) : "memory");
446 	}
447 
448 	if (CPU_IS_060 ? sc->sc_fpstate[2] : sc->sc_fpstate[0]) {
449 		fpu_version = sc->sc_fpstate[0];
450 		if (CPU_IS_020_OR_030 && !regs->stkadj &&
451 		    regs->vector >= (VEC_FPBRUC * 4) &&
452 		    regs->vector <= (VEC_FPNAN * 4)) {
453 			/* Clear pending exception in 68882 idle frame */
454 			if (*(unsigned short *) sc->sc_fpstate == 0x1f38)
455 				sc->sc_fpstate[0x38] |= 1 << 3;
456 		}
457 
458 		if (CPU_IS_COLDFIRE) {
459 			__asm__ volatile ("fmovemd %%fp0-%%fp1,%0\n\t"
460 					  "fmovel %%fpcr,%1\n\t"
461 					  "fmovel %%fpsr,%2\n\t"
462 					  "fmovel %%fpiar,%3"
463 					  : "=m" (sc->sc_fpregs[0]),
464 					    "=m" (sc->sc_fpcntl[0]),
465 					    "=m" (sc->sc_fpcntl[1]),
466 					    "=m" (sc->sc_fpcntl[2])
467 					  : /* no inputs */
468 					  : "memory");
469 		} else {
470 			__asm__ volatile (".chip 68k/68881\n\t"
471 					  "fmovemx %%fp0-%%fp1,%0\n\t"
472 					  "fmoveml %%fpcr/%%fpsr/%%fpiar,%1\n\t"
473 					  ".chip 68k"
474 					  : "=m" (*sc->sc_fpregs),
475 					    "=m" (*sc->sc_fpcntl)
476 					  : /* no inputs */
477 					  : "memory");
478 		}
479 	}
480 }
481 
rt_save_fpu_state(struct ucontext __user * uc,struct pt_regs * regs)482 static inline int rt_save_fpu_state(struct ucontext __user *uc, struct pt_regs *regs)
483 {
484 	unsigned char fpstate[FPCONTEXT_SIZE];
485 	int context_size = CPU_IS_060 ? 8 : (CPU_IS_COLDFIRE ? 12 : 0);
486 	int err = 0;
487 
488 	if (FPU_IS_EMU) {
489 		/* save fpu control register */
490 		err |= copy_to_user(uc->uc_mcontext.fpregs.f_fpcntl,
491 				current->thread.fpcntl, 12);
492 		/* save all other fpu register */
493 		err |= copy_to_user(uc->uc_mcontext.fpregs.f_fpregs,
494 				current->thread.fp, 96);
495 		return err;
496 	}
497 
498 	if (CPU_IS_COLDFIRE) {
499 		__asm__ volatile ("fsave %0" : : "m" (*fpstate) : "memory");
500 	} else {
501 		__asm__ volatile (".chip 68k/68881\n\t"
502 				  "fsave %0\n\t"
503 				  ".chip 68k"
504 				  : : "m" (*fpstate) : "memory");
505 	}
506 
507 	err |= __put_user(*(long *)fpstate, (long __user *)&uc->uc_fpstate);
508 	if (CPU_IS_060 ? fpstate[2] : fpstate[0]) {
509 		fpregset_t fpregs;
510 		if (!(CPU_IS_060 || CPU_IS_COLDFIRE))
511 			context_size = fpstate[1];
512 		fpu_version = fpstate[0];
513 		if (CPU_IS_020_OR_030 && !regs->stkadj &&
514 		    regs->vector >= (VEC_FPBRUC * 4) &&
515 		    regs->vector <= (VEC_FPNAN * 4)) {
516 			/* Clear pending exception in 68882 idle frame */
517 			if (*(unsigned short *) fpstate == 0x1f38)
518 				fpstate[0x38] |= 1 << 3;
519 		}
520 		if (CPU_IS_COLDFIRE) {
521 			__asm__ volatile ("fmovemd %%fp0-%%fp7,%0\n\t"
522 					  "fmovel %%fpcr,%1\n\t"
523 					  "fmovel %%fpsr,%2\n\t"
524 					  "fmovel %%fpiar,%3"
525 					  : "=m" (fpregs.f_fpregs[0]),
526 					    "=m" (fpregs.f_fpcntl[0]),
527 					    "=m" (fpregs.f_fpcntl[1]),
528 					    "=m" (fpregs.f_fpcntl[2])
529 					  : /* no inputs */
530 					  : "memory");
531 		} else {
532 			__asm__ volatile (".chip 68k/68881\n\t"
533 					  "fmovemx %%fp0-%%fp7,%0\n\t"
534 					  "fmoveml %%fpcr/%%fpsr/%%fpiar,%1\n\t"
535 					  ".chip 68k"
536 					  : "=m" (*fpregs.f_fpregs),
537 					    "=m" (*fpregs.f_fpcntl)
538 					  : /* no inputs */
539 					  : "memory");
540 		}
541 		err |= copy_to_user(&uc->uc_mcontext.fpregs, &fpregs,
542 				    sizeof(fpregs));
543 	}
544 	if (context_size)
545 		err |= copy_to_user((long __user *)&uc->uc_fpstate + 1, fpstate + 4,
546 				    context_size);
547 	return err;
548 }
549 
550 #else /* CONFIG_FPU */
551 
552 /*
553  * For the case with no FPU configured these all do nothing.
554  */
restore_fpu_state(struct sigcontext * sc)555 static inline int restore_fpu_state(struct sigcontext *sc)
556 {
557 	return 0;
558 }
559 
rt_restore_fpu_state(struct ucontext __user * uc)560 static inline int rt_restore_fpu_state(struct ucontext __user *uc)
561 {
562 	return 0;
563 }
564 
save_fpu_state(struct sigcontext * sc,struct pt_regs * regs)565 static inline void save_fpu_state(struct sigcontext *sc, struct pt_regs *regs)
566 {
567 }
568 
rt_save_fpu_state(struct ucontext __user * uc,struct pt_regs * regs)569 static inline int rt_save_fpu_state(struct ucontext __user *uc, struct pt_regs *regs)
570 {
571 	return 0;
572 }
573 
574 #endif /* CONFIG_FPU */
575 
siginfo_build_tests(void)576 static inline void siginfo_build_tests(void)
577 {
578 	/*
579 	 * This needs to be tested on m68k as it has a lesser
580 	 * alignment requirement than x86 and that can cause surprises.
581 	 */
582 
583 	/* This is part of the ABI and can never change in size: */
584 	BUILD_BUG_ON(sizeof(siginfo_t) != 128);
585 
586 	/* Ensure the known fields never change in location */
587 	BUILD_BUG_ON(offsetof(siginfo_t, si_signo) != 0);
588 	BUILD_BUG_ON(offsetof(siginfo_t, si_errno) != 4);
589 	BUILD_BUG_ON(offsetof(siginfo_t, si_code)  != 8);
590 
591 	/* _kill */
592 	BUILD_BUG_ON(offsetof(siginfo_t, si_pid) != 0x0c);
593 	BUILD_BUG_ON(offsetof(siginfo_t, si_uid) != 0x10);
594 
595 	/* _timer */
596 	BUILD_BUG_ON(offsetof(siginfo_t, si_tid)     != 0x0c);
597 	BUILD_BUG_ON(offsetof(siginfo_t, si_overrun) != 0x10);
598 	BUILD_BUG_ON(offsetof(siginfo_t, si_value)   != 0x14);
599 
600 	/* _rt */
601 	BUILD_BUG_ON(offsetof(siginfo_t, si_pid)   != 0x0c);
602 	BUILD_BUG_ON(offsetof(siginfo_t, si_uid)   != 0x10);
603 	BUILD_BUG_ON(offsetof(siginfo_t, si_value) != 0x14);
604 
605 	/* _sigchld */
606 	BUILD_BUG_ON(offsetof(siginfo_t, si_pid)    != 0x0c);
607 	BUILD_BUG_ON(offsetof(siginfo_t, si_uid)    != 0x10);
608 	BUILD_BUG_ON(offsetof(siginfo_t, si_status) != 0x14);
609 	BUILD_BUG_ON(offsetof(siginfo_t, si_utime)  != 0x18);
610 	BUILD_BUG_ON(offsetof(siginfo_t, si_stime)  != 0x1c);
611 
612 	/* _sigfault */
613 	BUILD_BUG_ON(offsetof(siginfo_t, si_addr) != 0x0c);
614 
615 	/* _sigfault._mcerr */
616 	BUILD_BUG_ON(offsetof(siginfo_t, si_addr_lsb) != 0x10);
617 
618 	/* _sigfault._addr_bnd */
619 	BUILD_BUG_ON(offsetof(siginfo_t, si_lower) != 0x12);
620 	BUILD_BUG_ON(offsetof(siginfo_t, si_upper) != 0x16);
621 
622 	/* _sigfault._addr_pkey */
623 	BUILD_BUG_ON(offsetof(siginfo_t, si_pkey) != 0x12);
624 
625 	/* _sigfault._perf */
626 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_data) != 0x10);
627 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_type) != 0x14);
628 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_flags) != 0x18);
629 
630 	/* _sigpoll */
631 	BUILD_BUG_ON(offsetof(siginfo_t, si_band)   != 0x0c);
632 	BUILD_BUG_ON(offsetof(siginfo_t, si_fd)     != 0x10);
633 
634 	/* _sigsys */
635 	BUILD_BUG_ON(offsetof(siginfo_t, si_call_addr) != 0x0c);
636 	BUILD_BUG_ON(offsetof(siginfo_t, si_syscall)   != 0x10);
637 	BUILD_BUG_ON(offsetof(siginfo_t, si_arch)      != 0x14);
638 
639 	/* any new si_fields should be added here */
640 }
641 
mangle_kernel_stack(struct pt_regs * regs,int formatvec,void __user * fp)642 static int mangle_kernel_stack(struct pt_regs *regs, int formatvec,
643 			       void __user *fp)
644 {
645 	int extra = frame_extra_sizes(formatvec >> 12);
646 	char buf[sizeof_field(struct frame, un)];
647 
648 	if (extra < 0) {
649 		/*
650 		 * user process trying to return with weird frame format
651 		 */
652 		pr_debug("user process returning with weird frame format\n");
653 		return -1;
654 	}
655 	if (extra && copy_from_user(buf, fp, extra))
656 		return -1;
657 	regs->format = formatvec >> 12;
658 	regs->vector = formatvec & 0xfff;
659 	if (extra) {
660 		void *p = (struct switch_stack *)regs - 1;
661 		struct frame *new = (void *)regs - extra;
662 		int size = sizeof(struct pt_regs)+sizeof(struct switch_stack);
663 
664 		memmove(p - extra, p, size);
665 		memcpy(p - extra + size, buf, extra);
666 		current->thread.esp0 = (unsigned long)&new->ptregs;
667 #ifdef CONFIG_M68040
668 		/* on 68040 complete pending writebacks if any */
669 		if (new->ptregs.format == 7) // bus error frame
670 			berr_040cleanup(new);
671 #endif
672 	}
673 	return extra;
674 }
675 
676 static inline int
restore_sigcontext(struct pt_regs * regs,struct sigcontext __user * usc,void __user * fp)677 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *usc, void __user *fp)
678 {
679 	int formatvec;
680 	struct sigcontext context;
681 
682 	siginfo_build_tests();
683 
684 	/* Always make any pending restarted system calls return -EINTR */
685 	current->restart_block.fn = do_no_restart_syscall;
686 
687 	/* get previous context */
688 	if (copy_from_user(&context, usc, sizeof(context)))
689 		return -1;
690 
691 	/* restore passed registers */
692 	regs->d0 = context.sc_d0;
693 	regs->d1 = context.sc_d1;
694 	regs->a0 = context.sc_a0;
695 	regs->a1 = context.sc_a1;
696 	regs->sr = (regs->sr & 0xff00) | (context.sc_sr & 0xff);
697 	regs->pc = context.sc_pc;
698 	regs->orig_d0 = -1;		/* disable syscall checks */
699 	wrusp(context.sc_usp);
700 	formatvec = context.sc_formatvec;
701 
702 	if (restore_fpu_state(&context))
703 		return -1;
704 
705 	return mangle_kernel_stack(regs, formatvec, fp);
706 }
707 
708 static inline int
rt_restore_ucontext(struct pt_regs * regs,struct switch_stack * sw,struct ucontext __user * uc)709 rt_restore_ucontext(struct pt_regs *regs, struct switch_stack *sw,
710 		    struct ucontext __user *uc)
711 {
712 	int temp;
713 	greg_t __user *gregs = uc->uc_mcontext.gregs;
714 	unsigned long usp;
715 	int err;
716 
717 	/* Always make any pending restarted system calls return -EINTR */
718 	current->restart_block.fn = do_no_restart_syscall;
719 
720 	err = __get_user(temp, &uc->uc_mcontext.version);
721 	if (temp != MCONTEXT_VERSION)
722 		return -1;
723 	/* restore passed registers */
724 	err |= __get_user(regs->d0, &gregs[0]);
725 	err |= __get_user(regs->d1, &gregs[1]);
726 	err |= __get_user(regs->d2, &gregs[2]);
727 	err |= __get_user(regs->d3, &gregs[3]);
728 	err |= __get_user(regs->d4, &gregs[4]);
729 	err |= __get_user(regs->d5, &gregs[5]);
730 	err |= __get_user(sw->d6, &gregs[6]);
731 	err |= __get_user(sw->d7, &gregs[7]);
732 	err |= __get_user(regs->a0, &gregs[8]);
733 	err |= __get_user(regs->a1, &gregs[9]);
734 	err |= __get_user(regs->a2, &gregs[10]);
735 	err |= __get_user(sw->a3, &gregs[11]);
736 	err |= __get_user(sw->a4, &gregs[12]);
737 	err |= __get_user(sw->a5, &gregs[13]);
738 	err |= __get_user(sw->a6, &gregs[14]);
739 	err |= __get_user(usp, &gregs[15]);
740 	wrusp(usp);
741 	err |= __get_user(regs->pc, &gregs[16]);
742 	err |= __get_user(temp, &gregs[17]);
743 	regs->sr = (regs->sr & 0xff00) | (temp & 0xff);
744 	regs->orig_d0 = -1;		/* disable syscall checks */
745 	err |= __get_user(temp, &uc->uc_formatvec);
746 
747 	err |= rt_restore_fpu_state(uc);
748 	err |= restore_altstack(&uc->uc_stack);
749 
750 	if (err)
751 		return -1;
752 
753 	return mangle_kernel_stack(regs, temp, &uc->uc_extra);
754 }
755 
do_sigreturn(struct pt_regs * regs,struct switch_stack * sw)756 asmlinkage void *do_sigreturn(struct pt_regs *regs, struct switch_stack *sw)
757 {
758 	unsigned long usp = rdusp();
759 	struct sigframe __user *frame = (struct sigframe __user *)(usp - 4);
760 	sigset_t set;
761 	int size;
762 
763 	if (!access_ok(frame, sizeof(*frame)))
764 		goto badframe;
765 	if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
766 	    (_NSIG_WORDS > 1 &&
767 	     __copy_from_user(&set.sig[1], &frame->extramask,
768 			      sizeof(frame->extramask))))
769 		goto badframe;
770 
771 	set_current_blocked(&set);
772 
773 	size = restore_sigcontext(regs, &frame->sc, frame + 1);
774 	if (size < 0)
775 		goto badframe;
776 	return (void *)sw - size;
777 
778 badframe:
779 	force_sig(SIGSEGV);
780 	return sw;
781 }
782 
do_rt_sigreturn(struct pt_regs * regs,struct switch_stack * sw)783 asmlinkage void *do_rt_sigreturn(struct pt_regs *regs, struct switch_stack *sw)
784 {
785 	unsigned long usp = rdusp();
786 	struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(usp - 4);
787 	sigset_t set;
788 	int size;
789 
790 	if (!access_ok(frame, sizeof(*frame)))
791 		goto badframe;
792 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
793 		goto badframe;
794 
795 	set_current_blocked(&set);
796 
797 	size = rt_restore_ucontext(regs, sw, &frame->uc);
798 	if (size < 0)
799 		goto badframe;
800 	return (void *)sw - size;
801 
802 badframe:
803 	force_sig(SIGSEGV);
804 	return sw;
805 }
806 
rte_regs(struct pt_regs * regs)807 static inline struct pt_regs *rte_regs(struct pt_regs *regs)
808 {
809 	return (void *)regs + regs->stkadj;
810 }
811 
setup_sigcontext(struct sigcontext * sc,struct pt_regs * regs,unsigned long mask)812 static void setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
813 			     unsigned long mask)
814 {
815 	struct pt_regs *tregs = rte_regs(regs);
816 	sc->sc_mask = mask;
817 	sc->sc_usp = rdusp();
818 	sc->sc_d0 = regs->d0;
819 	sc->sc_d1 = regs->d1;
820 	sc->sc_a0 = regs->a0;
821 	sc->sc_a1 = regs->a1;
822 	sc->sc_sr = tregs->sr;
823 	sc->sc_pc = tregs->pc;
824 	sc->sc_formatvec = tregs->format << 12 | tregs->vector;
825 	save_a5_state(sc, regs);
826 	save_fpu_state(sc, regs);
827 }
828 
rt_setup_ucontext(struct ucontext __user * uc,struct pt_regs * regs)829 static inline int rt_setup_ucontext(struct ucontext __user *uc, struct pt_regs *regs)
830 {
831 	struct switch_stack *sw = (struct switch_stack *)regs - 1;
832 	struct pt_regs *tregs = rte_regs(regs);
833 	greg_t __user *gregs = uc->uc_mcontext.gregs;
834 	int err = 0;
835 
836 	err |= __put_user(MCONTEXT_VERSION, &uc->uc_mcontext.version);
837 	err |= __put_user(regs->d0, &gregs[0]);
838 	err |= __put_user(regs->d1, &gregs[1]);
839 	err |= __put_user(regs->d2, &gregs[2]);
840 	err |= __put_user(regs->d3, &gregs[3]);
841 	err |= __put_user(regs->d4, &gregs[4]);
842 	err |= __put_user(regs->d5, &gregs[5]);
843 	err |= __put_user(sw->d6, &gregs[6]);
844 	err |= __put_user(sw->d7, &gregs[7]);
845 	err |= __put_user(regs->a0, &gregs[8]);
846 	err |= __put_user(regs->a1, &gregs[9]);
847 	err |= __put_user(regs->a2, &gregs[10]);
848 	err |= __put_user(sw->a3, &gregs[11]);
849 	err |= __put_user(sw->a4, &gregs[12]);
850 	err |= __put_user(sw->a5, &gregs[13]);
851 	err |= __put_user(sw->a6, &gregs[14]);
852 	err |= __put_user(rdusp(), &gregs[15]);
853 	err |= __put_user(tregs->pc, &gregs[16]);
854 	err |= __put_user(tregs->sr, &gregs[17]);
855 	err |= __put_user((tregs->format << 12) | tregs->vector, &uc->uc_formatvec);
856 	err |= rt_save_fpu_state(uc, regs);
857 	return err;
858 }
859 
860 static inline void __user *
get_sigframe(struct ksignal * ksig,struct pt_regs * tregs,size_t frame_size)861 get_sigframe(struct ksignal *ksig, struct pt_regs *tregs, size_t frame_size)
862 {
863 	unsigned long usp = sigsp(rdusp(), ksig);
864 	unsigned long gap = 0;
865 
866 	if (CPU_IS_020_OR_030 && tregs->format == 0xb) {
867 		/* USP is unreliable so use worst-case value */
868 		gap = 256;
869 	}
870 
871 	return (void __user *)((usp - gap - frame_size) & -8UL);
872 }
873 
setup_frame(struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)874 static int setup_frame(struct ksignal *ksig, sigset_t *set,
875 			struct pt_regs *regs)
876 {
877 	struct sigframe __user *frame;
878 	struct pt_regs *tregs = rte_regs(regs);
879 	int fsize = frame_extra_sizes(tregs->format);
880 	struct sigcontext context;
881 	int err = 0, sig = ksig->sig;
882 
883 	if (fsize < 0) {
884 		pr_debug("setup_frame: Unknown frame format %#x\n",
885 			 tregs->format);
886 		return -EFAULT;
887 	}
888 
889 	frame = get_sigframe(ksig, tregs, sizeof(*frame) + fsize);
890 
891 	if (fsize)
892 		err |= copy_to_user (frame + 1, regs + 1, fsize);
893 
894 	err |= __put_user(sig, &frame->sig);
895 
896 	err |= __put_user(tregs->vector, &frame->code);
897 	err |= __put_user(&frame->sc, &frame->psc);
898 
899 	if (_NSIG_WORDS > 1)
900 		err |= copy_to_user(frame->extramask, &set->sig[1],
901 				    sizeof(frame->extramask));
902 
903 	setup_sigcontext(&context, regs, set->sig[0]);
904 	err |= copy_to_user (&frame->sc, &context, sizeof(context));
905 
906 	/* Set up to return from userspace.  */
907 #ifdef CONFIG_MMU
908 	err |= __put_user(frame->retcode, &frame->pretcode);
909 	/* moveq #,d0; trap #0 */
910 	err |= __put_user(0x70004e40 + (__NR_sigreturn << 16),
911 			  (long __user *)(frame->retcode));
912 #else
913 	err |= __put_user((long) ret_from_user_signal,
914 			  (long __user *) &frame->pretcode);
915 #endif
916 
917 	if (err)
918 		return -EFAULT;
919 
920 	push_cache ((unsigned long) &frame->retcode);
921 
922 	/*
923 	 * This is subtle; if we build more than one sigframe, all but the
924 	 * first one will see frame format 0 and have fsize == 0, so we won't
925 	 * screw stkadj.
926 	 */
927 	if (fsize) {
928 		regs->stkadj = fsize;
929 		tregs = rte_regs(regs);
930 		pr_debug("Performing stackadjust=%04lx\n", regs->stkadj);
931 		tregs->vector = 0;
932 		tregs->format = 0;
933 		tregs->sr = regs->sr;
934 	}
935 
936 	/*
937 	 * Set up registers for signal handler.  All the state we are about
938 	 * to destroy is successfully copied to sigframe.
939 	 */
940 	wrusp ((unsigned long) frame);
941 	tregs->pc = (unsigned long) ksig->ka.sa.sa_handler;
942 	adjustformat(regs);
943 
944 	return 0;
945 }
946 
setup_rt_frame(struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)947 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
948 			   struct pt_regs *regs)
949 {
950 	struct rt_sigframe __user *frame;
951 	struct pt_regs *tregs = rte_regs(regs);
952 	int fsize = frame_extra_sizes(tregs->format);
953 	int err = 0, sig = ksig->sig;
954 
955 	if (fsize < 0) {
956 		pr_debug("setup_frame: Unknown frame format %#x\n",
957 			 regs->format);
958 		return -EFAULT;
959 	}
960 
961 	frame = get_sigframe(ksig, tregs, sizeof(*frame));
962 
963 	if (fsize)
964 		err |= copy_to_user (&frame->uc.uc_extra, regs + 1, fsize);
965 
966 	err |= __put_user(sig, &frame->sig);
967 	err |= __put_user(&frame->info, &frame->pinfo);
968 	err |= __put_user(&frame->uc, &frame->puc);
969 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
970 
971 	/* Create the ucontext.  */
972 	err |= __put_user(0, &frame->uc.uc_flags);
973 	err |= __put_user(NULL, &frame->uc.uc_link);
974 	err |= __save_altstack(&frame->uc.uc_stack, rdusp());
975 	err |= rt_setup_ucontext(&frame->uc, regs);
976 	err |= copy_to_user (&frame->uc.uc_sigmask, set, sizeof(*set));
977 
978 	/* Set up to return from userspace.  */
979 #ifdef CONFIG_MMU
980 	err |= __put_user(frame->retcode, &frame->pretcode);
981 #ifdef __mcoldfire__
982 	/* movel #__NR_rt_sigreturn,d0; trap #0 */
983 	err |= __put_user(0x203c0000, (long __user *)(frame->retcode + 0));
984 	err |= __put_user(0x00004e40 + (__NR_rt_sigreturn << 16),
985 			  (long __user *)(frame->retcode + 4));
986 #else
987 	/* moveq #,d0; notb d0; trap #0 */
988 	err |= __put_user(0x70004600 + ((__NR_rt_sigreturn ^ 0xff) << 16),
989 			  (long __user *)(frame->retcode + 0));
990 	err |= __put_user(0x4e40, (short __user *)(frame->retcode + 4));
991 #endif
992 #else
993 	err |= __put_user((long) ret_from_user_rt_signal,
994 			  (long __user *) &frame->pretcode);
995 #endif /* CONFIG_MMU */
996 
997 	if (err)
998 		return -EFAULT;
999 
1000 	push_cache ((unsigned long) &frame->retcode);
1001 
1002 	/*
1003 	 * This is subtle; if we build more than one sigframe, all but the
1004 	 * first one will see frame format 0 and have fsize == 0, so we won't
1005 	 * screw stkadj.
1006 	 */
1007 	if (fsize) {
1008 		regs->stkadj = fsize;
1009 		tregs = rte_regs(regs);
1010 		pr_debug("Performing stackadjust=%04lx\n", regs->stkadj);
1011 		tregs->vector = 0;
1012 		tregs->format = 0;
1013 		tregs->sr = regs->sr;
1014 	}
1015 
1016 	/*
1017 	 * Set up registers for signal handler.  All the state we are about
1018 	 * to destroy is successfully copied to sigframe.
1019 	 */
1020 	wrusp ((unsigned long) frame);
1021 	tregs->pc = (unsigned long) ksig->ka.sa.sa_handler;
1022 	adjustformat(regs);
1023 	return 0;
1024 }
1025 
1026 static inline void
handle_restart(struct pt_regs * regs,struct k_sigaction * ka,int has_handler)1027 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
1028 {
1029 	switch (regs->d0) {
1030 	case -ERESTARTNOHAND:
1031 		if (!has_handler)
1032 			goto do_restart;
1033 		regs->d0 = -EINTR;
1034 		break;
1035 
1036 	case -ERESTART_RESTARTBLOCK:
1037 		if (!has_handler) {
1038 			regs->d0 = __NR_restart_syscall;
1039 			regs->pc -= 2;
1040 			break;
1041 		}
1042 		regs->d0 = -EINTR;
1043 		break;
1044 
1045 	case -ERESTARTSYS:
1046 		if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
1047 			regs->d0 = -EINTR;
1048 			break;
1049 		}
1050 		fallthrough;
1051 	case -ERESTARTNOINTR:
1052 	do_restart:
1053 		regs->d0 = regs->orig_d0;
1054 		regs->pc -= 2;
1055 		break;
1056 	}
1057 }
1058 
1059 /*
1060  * OK, we're invoking a handler
1061  */
1062 static void
handle_signal(struct ksignal * ksig,struct pt_regs * regs)1063 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
1064 {
1065 	sigset_t *oldset = sigmask_to_save();
1066 	int err;
1067 	/* are we from a system call? */
1068 	if (regs->orig_d0 >= 0)
1069 		/* If so, check system call restarting.. */
1070 		handle_restart(regs, &ksig->ka, 1);
1071 
1072 	/* set up the stack frame */
1073 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
1074 		err = setup_rt_frame(ksig, oldset, regs);
1075 	else
1076 		err = setup_frame(ksig, oldset, regs);
1077 
1078 	signal_setup_done(err, ksig, 0);
1079 
1080 	if (test_thread_flag(TIF_DELAYED_TRACE)) {
1081 		regs->sr &= ~0x8000;
1082 		send_sig(SIGTRAP, current, 1);
1083 	}
1084 }
1085 
1086 /*
1087  * Note that 'init' is a special process: it doesn't get signals it doesn't
1088  * want to handle. Thus you cannot kill init even with a SIGKILL even by
1089  * mistake.
1090  */
do_signal(struct pt_regs * regs)1091 static void do_signal(struct pt_regs *regs)
1092 {
1093 	struct ksignal ksig;
1094 
1095 	current->thread.esp0 = (unsigned long) regs;
1096 
1097 	if (get_signal(&ksig)) {
1098 		/* Whee!  Actually deliver the signal.  */
1099 		handle_signal(&ksig, regs);
1100 		return;
1101 	}
1102 
1103 	/* Did we come from a system call? */
1104 	if (regs->orig_d0 >= 0)
1105 		/* Restart the system call - no handlers present */
1106 		handle_restart(regs, NULL, 0);
1107 
1108 	/* If there's no signal to deliver, we just restore the saved mask.  */
1109 	restore_saved_sigmask();
1110 }
1111 
do_notify_resume(struct pt_regs * regs)1112 void do_notify_resume(struct pt_regs *regs)
1113 {
1114 	if (test_thread_flag(TIF_NOTIFY_SIGNAL) ||
1115 	    test_thread_flag(TIF_SIGPENDING))
1116 		do_signal(regs);
1117 
1118 	if (test_thread_flag(TIF_NOTIFY_RESUME))
1119 		resume_user_mode_work(regs);
1120 }
1121