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