• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/arch/sh/kernel/signal.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  *
7  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
8  *
9  *  SuperH version:  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
10  *
11  */
12 #include <linux/sched.h>
13 #include <linux/sched/task_stack.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/tty.h>
24 #include <linux/elf.h>
25 #include <linux/personality.h>
26 #include <linux/binfmts.h>
27 #include <linux/io.h>
28 #include <linux/tracehook.h>
29 #include <asm/ucontext.h>
30 #include <linux/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/cacheflush.h>
33 #include <asm/syscalls.h>
34 #include <asm/fpu.h>
35 
36 struct fdpic_func_descriptor {
37 	unsigned long	text;
38 	unsigned long	GOT;
39 };
40 
41 /*
42  * The following define adds a 64 byte gap between the signal
43  * stack frame and previous contents of the stack.  This allows
44  * frame unwinding in a function epilogue but only if a frame
45  * pointer is used in the function.  This is necessary because
46  * current gcc compilers (<4.3) do not generate unwind info on
47  * SH for function epilogues.
48  */
49 #define UNWINDGUARD 64
50 
51 /*
52  * Do a signal return; undo the signal stack.
53  */
54 
55 #define MOVW(n)	 (0x9300|((n)-2))	/* Move mem word at PC+n to R3 */
56 #if defined(CONFIG_CPU_SH2)
57 #define TRAP_NOARG 0xc320		/* Syscall w/no args (NR in R3) */
58 #else
59 #define TRAP_NOARG 0xc310		/* Syscall w/no args (NR in R3) */
60 #endif
61 #define OR_R0_R0 0x200b			/* or r0,r0 (insert to avoid hardware bug) */
62 
63 struct sigframe
64 {
65 	struct sigcontext sc;
66 	unsigned long extramask[_NSIG_WORDS-1];
67 	u16 retcode[8];
68 };
69 
70 struct rt_sigframe
71 {
72 	struct siginfo info;
73 	struct ucontext uc;
74 	u16 retcode[8];
75 };
76 
77 #ifdef CONFIG_SH_FPU
restore_sigcontext_fpu(struct sigcontext __user * sc)78 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
79 {
80 	struct task_struct *tsk = current;
81 
82 	if (!(boot_cpu_data.flags & CPU_HAS_FPU))
83 		return 0;
84 
85 	set_used_math();
86 	return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
87 				sizeof(long)*(16*2+2));
88 }
89 
save_sigcontext_fpu(struct sigcontext __user * sc,struct pt_regs * regs)90 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
91 				      struct pt_regs *regs)
92 {
93 	struct task_struct *tsk = current;
94 
95 	if (!(boot_cpu_data.flags & CPU_HAS_FPU))
96 		return 0;
97 
98 	if (!used_math())
99 		return __put_user(0, &sc->sc_ownedfp);
100 
101 	if (__put_user(1, &sc->sc_ownedfp))
102 		return -EFAULT;
103 
104 	/* This will cause a "finit" to be triggered by the next
105 	   attempted FPU operation by the 'current' process.
106 	   */
107 	clear_used_math();
108 
109 	unlazy_fpu(tsk, regs);
110 	return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
111 			      sizeof(long)*(16*2+2));
112 }
113 #endif /* CONFIG_SH_FPU */
114 
115 static int
restore_sigcontext(struct pt_regs * regs,struct sigcontext __user * sc,int * r0_p)116 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
117 {
118 	unsigned int err = 0;
119 	unsigned int sr = regs->sr & ~SR_USER_MASK;
120 
121 #define COPY(x)		err |= __get_user(regs->x, &sc->sc_##x)
122 			COPY(regs[1]);
123 	COPY(regs[2]);	COPY(regs[3]);
124 	COPY(regs[4]);	COPY(regs[5]);
125 	COPY(regs[6]);	COPY(regs[7]);
126 	COPY(regs[8]);	COPY(regs[9]);
127 	COPY(regs[10]);	COPY(regs[11]);
128 	COPY(regs[12]);	COPY(regs[13]);
129 	COPY(regs[14]);	COPY(regs[15]);
130 	COPY(gbr);	COPY(mach);
131 	COPY(macl);	COPY(pr);
132 	COPY(sr);	COPY(pc);
133 #undef COPY
134 
135 	regs->sr = (regs->sr & SR_USER_MASK) | sr;
136 
137 #ifdef CONFIG_SH_FPU
138 	if (boot_cpu_data.flags & CPU_HAS_FPU) {
139 		int owned_fp;
140 		struct task_struct *tsk = current;
141 
142 		regs->sr |= SR_FD; /* Release FPU */
143 		clear_fpu(tsk, regs);
144 		clear_used_math();
145 		err |= __get_user (owned_fp, &sc->sc_ownedfp);
146 		if (owned_fp)
147 			err |= restore_sigcontext_fpu(sc);
148 	}
149 #endif
150 
151 	regs->tra = -1;		/* disable syscall checks */
152 	err |= __get_user(*r0_p, &sc->sc_regs[0]);
153 	return err;
154 }
155 
sys_sigreturn(void)156 asmlinkage int sys_sigreturn(void)
157 {
158 	struct pt_regs *regs = current_pt_regs();
159 	struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
160 	sigset_t set;
161 	int r0;
162 
163         /* Always make any pending restarted system calls return -EINTR */
164 	current->restart_block.fn = do_no_restart_syscall;
165 
166 	if (!access_ok(frame, sizeof(*frame)))
167 		goto badframe;
168 
169 	if (__get_user(set.sig[0], &frame->sc.oldmask)
170 	    || (_NSIG_WORDS > 1
171 		&& __copy_from_user(&set.sig[1], &frame->extramask,
172 				    sizeof(frame->extramask))))
173 		goto badframe;
174 
175 	set_current_blocked(&set);
176 
177 	if (restore_sigcontext(regs, &frame->sc, &r0))
178 		goto badframe;
179 	return r0;
180 
181 badframe:
182 	force_sig(SIGSEGV);
183 	return 0;
184 }
185 
sys_rt_sigreturn(void)186 asmlinkage int sys_rt_sigreturn(void)
187 {
188 	struct pt_regs *regs = current_pt_regs();
189 	struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
190 	sigset_t set;
191 	int r0;
192 
193 	/* Always make any pending restarted system calls return -EINTR */
194 	current->restart_block.fn = do_no_restart_syscall;
195 
196 	if (!access_ok(frame, sizeof(*frame)))
197 		goto badframe;
198 
199 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
200 		goto badframe;
201 
202 	set_current_blocked(&set);
203 
204 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
205 		goto badframe;
206 
207 	if (restore_altstack(&frame->uc.uc_stack))
208 		goto badframe;
209 
210 	return r0;
211 
212 badframe:
213 	force_sig(SIGSEGV);
214 	return 0;
215 }
216 
217 /*
218  * Set up a signal frame.
219  */
220 
221 static int
setup_sigcontext(struct sigcontext __user * sc,struct pt_regs * regs,unsigned long mask)222 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
223 		 unsigned long mask)
224 {
225 	int err = 0;
226 
227 #define COPY(x)		err |= __put_user(regs->x, &sc->sc_##x)
228 	COPY(regs[0]);	COPY(regs[1]);
229 	COPY(regs[2]);	COPY(regs[3]);
230 	COPY(regs[4]);	COPY(regs[5]);
231 	COPY(regs[6]);	COPY(regs[7]);
232 	COPY(regs[8]);	COPY(regs[9]);
233 	COPY(regs[10]);	COPY(regs[11]);
234 	COPY(regs[12]);	COPY(regs[13]);
235 	COPY(regs[14]);	COPY(regs[15]);
236 	COPY(gbr);	COPY(mach);
237 	COPY(macl);	COPY(pr);
238 	COPY(sr);	COPY(pc);
239 #undef COPY
240 
241 #ifdef CONFIG_SH_FPU
242 	err |= save_sigcontext_fpu(sc, regs);
243 #endif
244 
245 	/* non-iBCS2 extensions.. */
246 	err |= __put_user(mask, &sc->oldmask);
247 
248 	return err;
249 }
250 
251 /*
252  * Determine which stack to use..
253  */
254 static inline void __user *
get_sigframe(struct k_sigaction * ka,unsigned long sp,size_t frame_size)255 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
256 {
257 	if (ka->sa.sa_flags & SA_ONSTACK) {
258 		if (sas_ss_flags(sp) == 0)
259 			sp = current->sas_ss_sp + current->sas_ss_size;
260 	}
261 
262 	return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
263 }
264 
265 /* These symbols are defined with the addresses in the vsyscall page.
266    See vsyscall-trapa.S.  */
267 extern void __kernel_sigreturn(void);
268 extern void __kernel_rt_sigreturn(void);
269 
setup_frame(struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)270 static int setup_frame(struct ksignal *ksig, sigset_t *set,
271 		       struct pt_regs *regs)
272 {
273 	struct sigframe __user *frame;
274 	int err = 0, sig = ksig->sig;
275 
276 	frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
277 
278 	if (!access_ok(frame, sizeof(*frame)))
279 		return -EFAULT;
280 
281 	err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
282 
283 	if (_NSIG_WORDS > 1)
284 		err |= __copy_to_user(frame->extramask, &set->sig[1],
285 				      sizeof(frame->extramask));
286 
287 	/* Set up to return from userspace.  If provided, use a stub
288 	   already in userspace.  */
289 	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
290 		regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
291 #ifdef CONFIG_VSYSCALL
292 	} else if (likely(current->mm->context.vdso)) {
293 		regs->pr = VDSO_SYM(&__kernel_sigreturn);
294 #endif
295 	} else {
296 		/* Generate return code (system call to sigreturn) */
297 		err |= __put_user(MOVW(7), &frame->retcode[0]);
298 		err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
299 		err |= __put_user(OR_R0_R0, &frame->retcode[2]);
300 		err |= __put_user(OR_R0_R0, &frame->retcode[3]);
301 		err |= __put_user(OR_R0_R0, &frame->retcode[4]);
302 		err |= __put_user(OR_R0_R0, &frame->retcode[5]);
303 		err |= __put_user(OR_R0_R0, &frame->retcode[6]);
304 		err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
305 		regs->pr = (unsigned long) frame->retcode;
306 		flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
307 	}
308 
309 	if (err)
310 		return -EFAULT;
311 
312 	/* Set up registers for signal handler */
313 	regs->regs[15] = (unsigned long) frame;
314 	regs->regs[4] = sig; /* Arg for signal handler */
315 	regs->regs[5] = 0;
316 	regs->regs[6] = (unsigned long) &frame->sc;
317 
318 	if (current->personality & FDPIC_FUNCPTRS) {
319 		struct fdpic_func_descriptor __user *funcptr =
320 			(struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
321 
322 		err |= __get_user(regs->pc, &funcptr->text);
323 		err |= __get_user(regs->regs[12], &funcptr->GOT);
324 	} else
325 		regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
326 
327 	if (err)
328 		return -EFAULT;
329 
330 	pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
331 		 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
332 
333 	return 0;
334 }
335 
setup_rt_frame(struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)336 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
337 			  struct pt_regs *regs)
338 {
339 	struct rt_sigframe __user *frame;
340 	int err = 0, sig = ksig->sig;
341 
342 	frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
343 
344 	if (!access_ok(frame, sizeof(*frame)))
345 		return -EFAULT;
346 
347 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
348 
349 	/* Create the ucontext.  */
350 	err |= __put_user(0, &frame->uc.uc_flags);
351 	err |= __put_user(NULL, &frame->uc.uc_link);
352 	err |= __save_altstack(&frame->uc.uc_stack, regs->regs[15]);
353 	err |= setup_sigcontext(&frame->uc.uc_mcontext,
354 			        regs, set->sig[0]);
355 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
356 
357 	/* Set up to return from userspace.  If provided, use a stub
358 	   already in userspace.  */
359 	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
360 		regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
361 #ifdef CONFIG_VSYSCALL
362 	} else if (likely(current->mm->context.vdso)) {
363 		regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
364 #endif
365 	} else {
366 		/* Generate return code (system call to rt_sigreturn) */
367 		err |= __put_user(MOVW(7), &frame->retcode[0]);
368 		err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
369 		err |= __put_user(OR_R0_R0, &frame->retcode[2]);
370 		err |= __put_user(OR_R0_R0, &frame->retcode[3]);
371 		err |= __put_user(OR_R0_R0, &frame->retcode[4]);
372 		err |= __put_user(OR_R0_R0, &frame->retcode[5]);
373 		err |= __put_user(OR_R0_R0, &frame->retcode[6]);
374 		err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
375 		regs->pr = (unsigned long) frame->retcode;
376 		flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
377 	}
378 
379 	if (err)
380 		return -EFAULT;
381 
382 	/* Set up registers for signal handler */
383 	regs->regs[15] = (unsigned long) frame;
384 	regs->regs[4] = sig; /* Arg for signal handler */
385 	regs->regs[5] = (unsigned long) &frame->info;
386 	regs->regs[6] = (unsigned long) &frame->uc;
387 
388 	if (current->personality & FDPIC_FUNCPTRS) {
389 		struct fdpic_func_descriptor __user *funcptr =
390 			(struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
391 
392 		err |= __get_user(regs->pc, &funcptr->text);
393 		err |= __get_user(regs->regs[12], &funcptr->GOT);
394 	} else
395 		regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
396 
397 	if (err)
398 		return -EFAULT;
399 
400 	pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
401 		 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
402 
403 	return 0;
404 }
405 
406 static inline void
handle_syscall_restart(unsigned long save_r0,struct pt_regs * regs,struct sigaction * sa)407 handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
408 		       struct sigaction *sa)
409 {
410 	/* If we're not from a syscall, bail out */
411 	if (regs->tra < 0)
412 		return;
413 
414 	/* check for system call restart.. */
415 	switch (regs->regs[0]) {
416 		case -ERESTART_RESTARTBLOCK:
417 		case -ERESTARTNOHAND:
418 		no_system_call_restart:
419 			regs->regs[0] = -EINTR;
420 			break;
421 
422 		case -ERESTARTSYS:
423 			if (!(sa->sa_flags & SA_RESTART))
424 				goto no_system_call_restart;
425 		/* fallthrough */
426 		case -ERESTARTNOINTR:
427 			regs->regs[0] = save_r0;
428 			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
429 			break;
430 	}
431 }
432 
433 /*
434  * OK, we're invoking a handler
435  */
436 static void
handle_signal(struct ksignal * ksig,struct pt_regs * regs,unsigned int save_r0)437 handle_signal(struct ksignal *ksig, struct pt_regs *regs, unsigned int save_r0)
438 {
439 	sigset_t *oldset = sigmask_to_save();
440 	int ret;
441 
442 	/* Set up the stack frame */
443 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
444 		ret = setup_rt_frame(ksig, oldset, regs);
445 	else
446 		ret = setup_frame(ksig, oldset, regs);
447 
448 	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
449 }
450 
451 /*
452  * Note that 'init' is a special process: it doesn't get signals it doesn't
453  * want to handle. Thus you cannot kill init even with a SIGKILL even by
454  * mistake.
455  *
456  * Note that we go through the signals twice: once to check the signals that
457  * the kernel can handle, and then we build all the user-level signal handling
458  * stack-frames in one go after that.
459  */
do_signal(struct pt_regs * regs,unsigned int save_r0)460 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
461 {
462 	struct ksignal ksig;
463 
464 	/*
465 	 * We want the common case to go fast, which
466 	 * is why we may in certain cases get here from
467 	 * kernel mode. Just return without doing anything
468 	 * if so.
469 	 */
470 	if (!user_mode(regs))
471 		return;
472 
473 	if (get_signal(&ksig)) {
474 		handle_syscall_restart(save_r0, regs, &ksig.ka.sa);
475 
476 		/* Whee!  Actually deliver the signal.  */
477 		handle_signal(&ksig, regs, save_r0);
478 		return;
479 	}
480 
481 	/* Did we come from a system call? */
482 	if (regs->tra >= 0) {
483 		/* Restart the system call - no handlers present */
484 		if (regs->regs[0] == -ERESTARTNOHAND ||
485 		    regs->regs[0] == -ERESTARTSYS ||
486 		    regs->regs[0] == -ERESTARTNOINTR) {
487 			regs->regs[0] = save_r0;
488 			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
489 		} else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
490 			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
491 			regs->regs[3] = __NR_restart_syscall;
492 		}
493 	}
494 
495 	/*
496 	 * If there's no signal to deliver, we just put the saved sigmask
497 	 * back.
498 	 */
499 	restore_saved_sigmask();
500 }
501 
do_notify_resume(struct pt_regs * regs,unsigned int save_r0,unsigned long thread_info_flags)502 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
503 				 unsigned long thread_info_flags)
504 {
505 	/* deal with pending signal delivery */
506 	if (thread_info_flags & _TIF_SIGPENDING)
507 		do_signal(regs, save_r0);
508 
509 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
510 		clear_thread_flag(TIF_NOTIFY_RESUME);
511 		tracehook_notify_resume(regs);
512 	}
513 }
514