1
2
3
4
5
6
7
8
9
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
36struct fdpic_func_descriptor {
37 unsigned long text;
38 unsigned long GOT;
39};
40
41
42
43
44
45
46
47
48
49#define UNWINDGUARD 64
50
51
52
53
54
55#define MOVW(n) (0x9300|((n)-2))
56#if defined(CONFIG_CPU_SH2)
57#define TRAP_NOARG 0xc320
58#else
59#define TRAP_NOARG 0xc310
60#endif
61#define OR_R0_R0 0x200b
62
63struct sigframe
64{
65 struct sigcontext sc;
66 unsigned long extramask[_NSIG_WORDS-1];
67 u16 retcode[8];
68};
69
70struct rt_sigframe
71{
72 struct siginfo info;
73 struct ucontext uc;
74 u16 retcode[8];
75};
76
77#ifdef CONFIG_SH_FPU
78static 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
90static 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
105
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
114
115static int
116restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
117{
118 unsigned int err = 0;
119
120#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
121 COPY(regs[1]);
122 COPY(regs[2]); COPY(regs[3]);
123 COPY(regs[4]); COPY(regs[5]);
124 COPY(regs[6]); COPY(regs[7]);
125 COPY(regs[8]); COPY(regs[9]);
126 COPY(regs[10]); COPY(regs[11]);
127 COPY(regs[12]); COPY(regs[13]);
128 COPY(regs[14]); COPY(regs[15]);
129 COPY(gbr); COPY(mach);
130 COPY(macl); COPY(pr);
131 COPY(sr); COPY(pc);
132#undef COPY
133
134#ifdef CONFIG_SH_FPU
135 if (boot_cpu_data.flags & CPU_HAS_FPU) {
136 int owned_fp;
137 struct task_struct *tsk = current;
138
139 regs->sr |= SR_FD;
140 clear_fpu(tsk, regs);
141 clear_used_math();
142 err |= __get_user (owned_fp, &sc->sc_ownedfp);
143 if (owned_fp)
144 err |= restore_sigcontext_fpu(sc);
145 }
146#endif
147
148 regs->tra = -1;
149 err |= __get_user(*r0_p, &sc->sc_regs[0]);
150 return err;
151}
152
153asmlinkage int sys_sigreturn(void)
154{
155 struct pt_regs *regs = current_pt_regs();
156 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
157 sigset_t set;
158 int r0;
159
160
161 current->restart_block.fn = do_no_restart_syscall;
162
163 if (!access_ok(frame, sizeof(*frame)))
164 goto badframe;
165
166 if (__get_user(set.sig[0], &frame->sc.oldmask)
167 || (_NSIG_WORDS > 1
168 && __copy_from_user(&set.sig[1], &frame->extramask,
169 sizeof(frame->extramask))))
170 goto badframe;
171
172 set_current_blocked(&set);
173
174 if (restore_sigcontext(regs, &frame->sc, &r0))
175 goto badframe;
176 return r0;
177
178badframe:
179 force_sig(SIGSEGV, current);
180 return 0;
181}
182
183asmlinkage int sys_rt_sigreturn(void)
184{
185 struct pt_regs *regs = current_pt_regs();
186 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
187 sigset_t set;
188 int r0;
189
190
191 current->restart_block.fn = do_no_restart_syscall;
192
193 if (!access_ok(frame, sizeof(*frame)))
194 goto badframe;
195
196 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
197 goto badframe;
198
199 set_current_blocked(&set);
200
201 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
202 goto badframe;
203
204 if (restore_altstack(&frame->uc.uc_stack))
205 goto badframe;
206
207 return r0;
208
209badframe:
210 force_sig(SIGSEGV, current);
211 return 0;
212}
213
214
215
216
217
218static int
219setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
220 unsigned long mask)
221{
222 int err = 0;
223
224#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
225 COPY(regs[0]); COPY(regs[1]);
226 COPY(regs[2]); COPY(regs[3]);
227 COPY(regs[4]); COPY(regs[5]);
228 COPY(regs[6]); COPY(regs[7]);
229 COPY(regs[8]); COPY(regs[9]);
230 COPY(regs[10]); COPY(regs[11]);
231 COPY(regs[12]); COPY(regs[13]);
232 COPY(regs[14]); COPY(regs[15]);
233 COPY(gbr); COPY(mach);
234 COPY(macl); COPY(pr);
235 COPY(sr); COPY(pc);
236#undef COPY
237
238#ifdef CONFIG_SH_FPU
239 err |= save_sigcontext_fpu(sc, regs);
240#endif
241
242
243 err |= __put_user(mask, &sc->oldmask);
244
245 return err;
246}
247
248
249
250
251static inline void __user *
252get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
253{
254 if (ka->sa.sa_flags & SA_ONSTACK) {
255 if (sas_ss_flags(sp) == 0)
256 sp = current->sas_ss_sp + current->sas_ss_size;
257 }
258
259 return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
260}
261
262
263
264extern void __kernel_sigreturn(void);
265extern void __kernel_rt_sigreturn(void);
266
267static int setup_frame(struct ksignal *ksig, sigset_t *set,
268 struct pt_regs *regs)
269{
270 struct sigframe __user *frame;
271 int err = 0, sig = ksig->sig;
272
273 frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
274
275 if (!access_ok(frame, sizeof(*frame)))
276 return -EFAULT;
277
278 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
279
280 if (_NSIG_WORDS > 1)
281 err |= __copy_to_user(frame->extramask, &set->sig[1],
282 sizeof(frame->extramask));
283
284
285
286 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
287 regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
288#ifdef CONFIG_VSYSCALL
289 } else if (likely(current->mm->context.vdso)) {
290 regs->pr = VDSO_SYM(&__kernel_sigreturn);
291#endif
292 } else {
293
294 err |= __put_user(MOVW(7), &frame->retcode[0]);
295 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
296 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
297 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
298 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
299 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
300 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
301 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
302 regs->pr = (unsigned long) frame->retcode;
303 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
304 }
305
306 if (err)
307 return -EFAULT;
308
309
310 regs->regs[15] = (unsigned long) frame;
311 regs->regs[4] = sig;
312 regs->regs[5] = 0;
313 regs->regs[6] = (unsigned long) &frame->sc;
314
315 if (current->personality & FDPIC_FUNCPTRS) {
316 struct fdpic_func_descriptor __user *funcptr =
317 (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
318
319 err |= __get_user(regs->pc, &funcptr->text);
320 err |= __get_user(regs->regs[12], &funcptr->GOT);
321 } else
322 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
323
324 if (err)
325 return -EFAULT;
326
327 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
328 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
329
330 return 0;
331}
332
333static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
334 struct pt_regs *regs)
335{
336 struct rt_sigframe __user *frame;
337 int err = 0, sig = ksig->sig;
338
339 frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
340
341 if (!access_ok(frame, sizeof(*frame)))
342 return -EFAULT;
343
344 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
345
346
347 err |= __put_user(0, &frame->uc.uc_flags);
348 err |= __put_user(NULL, &frame->uc.uc_link);
349 err |= __save_altstack(&frame->uc.uc_stack, regs->regs[15]);
350 err |= setup_sigcontext(&frame->uc.uc_mcontext,
351 regs, set->sig[0]);
352 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
353
354
355
356 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
357 regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
358#ifdef CONFIG_VSYSCALL
359 } else if (likely(current->mm->context.vdso)) {
360 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
361#endif
362 } else {
363
364 err |= __put_user(MOVW(7), &frame->retcode[0]);
365 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
366 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
367 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
368 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
369 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
370 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
371 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
372 regs->pr = (unsigned long) frame->retcode;
373 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
374 }
375
376 if (err)
377 return -EFAULT;
378
379
380 regs->regs[15] = (unsigned long) frame;
381 regs->regs[4] = sig;
382 regs->regs[5] = (unsigned long) &frame->info;
383 regs->regs[6] = (unsigned long) &frame->uc;
384
385 if (current->personality & FDPIC_FUNCPTRS) {
386 struct fdpic_func_descriptor __user *funcptr =
387 (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
388
389 err |= __get_user(regs->pc, &funcptr->text);
390 err |= __get_user(regs->regs[12], &funcptr->GOT);
391 } else
392 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
393
394 if (err)
395 return -EFAULT;
396
397 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
398 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
399
400 return 0;
401}
402
403static inline void
404handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
405 struct sigaction *sa)
406{
407
408 if (regs->tra < 0)
409 return;
410
411
412 switch (regs->regs[0]) {
413 case -ERESTART_RESTARTBLOCK:
414 case -ERESTARTNOHAND:
415 no_system_call_restart:
416 regs->regs[0] = -EINTR;
417 break;
418
419 case -ERESTARTSYS:
420 if (!(sa->sa_flags & SA_RESTART))
421 goto no_system_call_restart;
422
423 case -ERESTARTNOINTR:
424 regs->regs[0] = save_r0;
425 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
426 break;
427 }
428}
429
430
431
432
433static void
434handle_signal(struct ksignal *ksig, struct pt_regs *regs, unsigned int save_r0)
435{
436 sigset_t *oldset = sigmask_to_save();
437 int ret;
438
439
440 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
441 ret = setup_rt_frame(ksig, oldset, regs);
442 else
443 ret = setup_frame(ksig, oldset, regs);
444
445 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
446}
447
448
449
450
451
452
453
454
455
456
457static void do_signal(struct pt_regs *regs, unsigned int save_r0)
458{
459 struct ksignal ksig;
460
461
462
463
464
465
466
467 if (!user_mode(regs))
468 return;
469
470 if (get_signal(&ksig)) {
471 handle_syscall_restart(save_r0, regs, &ksig.ka.sa);
472
473
474 handle_signal(&ksig, regs, save_r0);
475 return;
476 }
477
478
479 if (regs->tra >= 0) {
480
481 if (regs->regs[0] == -ERESTARTNOHAND ||
482 regs->regs[0] == -ERESTARTSYS ||
483 regs->regs[0] == -ERESTARTNOINTR) {
484 regs->regs[0] = save_r0;
485 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
486 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
487 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
488 regs->regs[3] = __NR_restart_syscall;
489 }
490 }
491
492
493
494
495
496 restore_saved_sigmask();
497}
498
499asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
500 unsigned long thread_info_flags)
501{
502
503 if (thread_info_flags & _TIF_SIGPENDING)
504 do_signal(regs, save_r0);
505
506 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
507 clear_thread_flag(TIF_NOTIFY_RESUME);
508 tracehook_notify_resume(regs);
509 }
510}
511