Remove 'type' argument from access_ok() function
[linux-2.6-block.git] / arch / sh / kernel / signal_64.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * arch/sh/kernel/signal_64.c
4  *
5  * Copyright (C) 2000, 2001  Paolo Alberelli
6  * Copyright (C) 2003 - 2008  Paul Mundt
7  * Copyright (C) 2004  Richard Curnow
8  */
9 #include <linux/rwsem.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/kernel.h>
14 #include <linux/signal.h>
15 #include <linux/errno.h>
16 #include <linux/wait.h>
17 #include <linux/personality.h>
18 #include <linux/ptrace.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/tracehook.h>
22 #include <asm/ucontext.h>
23 #include <linux/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/cacheflush.h>
26 #include <asm/fpu.h>
27
28 #define REG_RET 9
29 #define REG_ARG1 2
30 #define REG_ARG2 3
31 #define REG_ARG3 4
32 #define REG_SP 15
33 #define REG_PR 18
34 #define REF_REG_RET regs->regs[REG_RET]
35 #define REF_REG_SP regs->regs[REG_SP]
36 #define DEREF_REG_PR regs->regs[REG_PR]
37
38 #define DEBUG_SIG 0
39
40 static void
41 handle_signal(struct ksignal *ksig, struct pt_regs *regs);
42
43 static inline void
44 handle_syscall_restart(struct pt_regs *regs, struct sigaction *sa)
45 {
46         /* If we're not from a syscall, bail out */
47         if (regs->syscall_nr < 0)
48                 return;
49
50         /* check for system call restart.. */
51         switch (regs->regs[REG_RET]) {
52                 case -ERESTART_RESTARTBLOCK:
53                 case -ERESTARTNOHAND:
54                 no_system_call_restart:
55                         regs->regs[REG_RET] = -EINTR;
56                         break;
57
58                 case -ERESTARTSYS:
59                         if (!(sa->sa_flags & SA_RESTART))
60                                 goto no_system_call_restart;
61                 /* fallthrough */
62                 case -ERESTARTNOINTR:
63                         /* Decode syscall # */
64                         regs->regs[REG_RET] = regs->syscall_nr;
65                         regs->pc -= 4;
66                         break;
67         }
68 }
69
70 /*
71  * Note that 'init' is a special process: it doesn't get signals it doesn't
72  * want to handle. Thus you cannot kill init even with a SIGKILL even by
73  * mistake.
74  *
75  * Note that we go through the signals twice: once to check the signals that
76  * the kernel can handle, and then we build all the user-level signal handling
77  * stack-frames in one go after that.
78  */
79 static void do_signal(struct pt_regs *regs)
80 {
81         struct ksignal ksig;
82
83         /*
84          * We want the common case to go fast, which
85          * is why we may in certain cases get here from
86          * kernel mode. Just return without doing anything
87          * if so.
88          */
89         if (!user_mode(regs))
90                 return;
91
92         if (get_signal(&ksig)) {
93                 handle_syscall_restart(regs, &ksig.ka.sa);
94
95                 /* Whee!  Actually deliver the signal.  */
96                 handle_signal(&ksig, regs);
97                 return;
98         }
99
100         /* Did we come from a system call? */
101         if (regs->syscall_nr >= 0) {
102                 /* Restart the system call - no handlers present */
103                 switch (regs->regs[REG_RET]) {
104                 case -ERESTARTNOHAND:
105                 case -ERESTARTSYS:
106                 case -ERESTARTNOINTR:
107                         /* Decode Syscall # */
108                         regs->regs[REG_RET] = regs->syscall_nr;
109                         regs->pc -= 4;
110                         break;
111
112                 case -ERESTART_RESTARTBLOCK:
113                         regs->regs[REG_RET] = __NR_restart_syscall;
114                         regs->pc -= 4;
115                         break;
116                 }
117         }
118
119         /* No signal to deliver -- put the saved sigmask back */
120         restore_saved_sigmask();
121 }
122
123 /*
124  * Do a signal return; undo the signal stack.
125  */
126 struct sigframe {
127         struct sigcontext sc;
128         unsigned long extramask[_NSIG_WORDS-1];
129         long long retcode[2];
130 };
131
132 struct rt_sigframe {
133         struct siginfo __user *pinfo;
134         void *puc;
135         struct siginfo info;
136         struct ucontext uc;
137         long long retcode[2];
138 };
139
140 #ifdef CONFIG_SH_FPU
141 static inline int
142 restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
143 {
144         int err = 0;
145         int fpvalid;
146
147         err |= __get_user (fpvalid, &sc->sc_fpvalid);
148         conditional_used_math(fpvalid);
149         if (! fpvalid)
150                 return err;
151
152         if (current == last_task_used_math) {
153                 last_task_used_math = NULL;
154                 regs->sr |= SR_FD;
155         }
156
157         err |= __copy_from_user(&current->thread.xstate->hardfpu, &sc->sc_fpregs[0],
158                                 (sizeof(long long) * 32) + (sizeof(int) * 1));
159
160         return err;
161 }
162
163 static inline int
164 setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
165 {
166         int err = 0;
167         int fpvalid;
168
169         fpvalid = !!used_math();
170         err |= __put_user(fpvalid, &sc->sc_fpvalid);
171         if (! fpvalid)
172                 return err;
173
174         if (current == last_task_used_math) {
175                 enable_fpu();
176                 save_fpu(current);
177                 disable_fpu();
178                 last_task_used_math = NULL;
179                 regs->sr |= SR_FD;
180         }
181
182         err |= __copy_to_user(&sc->sc_fpregs[0], &current->thread.xstate->hardfpu,
183                               (sizeof(long long) * 32) + (sizeof(int) * 1));
184         clear_used_math();
185
186         return err;
187 }
188 #else
189 static inline int
190 restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
191 {
192         return 0;
193 }
194 static inline int
195 setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
196 {
197         return 0;
198 }
199 #endif
200
201 static int
202 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, long long *r2_p)
203 {
204         unsigned int err = 0;
205         unsigned long long current_sr, new_sr;
206 #define SR_MASK 0xffff8cfd
207
208 #define COPY(x)         err |= __get_user(regs->x, &sc->sc_##x)
209
210         COPY(regs[0]);  COPY(regs[1]);  COPY(regs[2]);  COPY(regs[3]);
211         COPY(regs[4]);  COPY(regs[5]);  COPY(regs[6]);  COPY(regs[7]);
212         COPY(regs[8]);  COPY(regs[9]);  COPY(regs[10]); COPY(regs[11]);
213         COPY(regs[12]); COPY(regs[13]); COPY(regs[14]); COPY(regs[15]);
214         COPY(regs[16]); COPY(regs[17]); COPY(regs[18]); COPY(regs[19]);
215         COPY(regs[20]); COPY(regs[21]); COPY(regs[22]); COPY(regs[23]);
216         COPY(regs[24]); COPY(regs[25]); COPY(regs[26]); COPY(regs[27]);
217         COPY(regs[28]); COPY(regs[29]); COPY(regs[30]); COPY(regs[31]);
218         COPY(regs[32]); COPY(regs[33]); COPY(regs[34]); COPY(regs[35]);
219         COPY(regs[36]); COPY(regs[37]); COPY(regs[38]); COPY(regs[39]);
220         COPY(regs[40]); COPY(regs[41]); COPY(regs[42]); COPY(regs[43]);
221         COPY(regs[44]); COPY(regs[45]); COPY(regs[46]); COPY(regs[47]);
222         COPY(regs[48]); COPY(regs[49]); COPY(regs[50]); COPY(regs[51]);
223         COPY(regs[52]); COPY(regs[53]); COPY(regs[54]); COPY(regs[55]);
224         COPY(regs[56]); COPY(regs[57]); COPY(regs[58]); COPY(regs[59]);
225         COPY(regs[60]); COPY(regs[61]); COPY(regs[62]);
226         COPY(tregs[0]); COPY(tregs[1]); COPY(tregs[2]); COPY(tregs[3]);
227         COPY(tregs[4]); COPY(tregs[5]); COPY(tregs[6]); COPY(tregs[7]);
228
229         /* Prevent the signal handler manipulating SR in a way that can
230            crash the kernel. i.e. only allow S, Q, M, PR, SZ, FR to be
231            modified */
232         current_sr = regs->sr;
233         err |= __get_user(new_sr, &sc->sc_sr);
234         regs->sr &= SR_MASK;
235         regs->sr |= (new_sr & ~SR_MASK);
236
237         COPY(pc);
238
239 #undef COPY
240
241         /* Must do this last in case it sets regs->sr.fd (i.e. after rest of sr
242          * has been restored above.) */
243         err |= restore_sigcontext_fpu(regs, sc);
244
245         regs->syscall_nr = -1;          /* disable syscall checks */
246         err |= __get_user(*r2_p, &sc->sc_regs[REG_RET]);
247         return err;
248 }
249
250 asmlinkage int sys_sigreturn(unsigned long r2, unsigned long r3,
251                                    unsigned long r4, unsigned long r5,
252                                    unsigned long r6, unsigned long r7,
253                                    struct pt_regs * regs)
254 {
255         struct sigframe __user *frame = (struct sigframe __user *) (long) REF_REG_SP;
256         sigset_t set;
257         long long ret;
258
259         /* Always make any pending restarted system calls return -EINTR */
260         current->restart_block.fn = do_no_restart_syscall;
261
262         if (!access_ok(frame, sizeof(*frame)))
263                 goto badframe;
264
265         if (__get_user(set.sig[0], &frame->sc.oldmask)
266             || (_NSIG_WORDS > 1
267                 && __copy_from_user(&set.sig[1], &frame->extramask,
268                                     sizeof(frame->extramask))))
269                 goto badframe;
270
271         set_current_blocked(&set);
272
273         if (restore_sigcontext(regs, &frame->sc, &ret))
274                 goto badframe;
275         regs->pc -= 4;
276
277         return (int) ret;
278
279 badframe:
280         force_sig(SIGSEGV, current);
281         return 0;
282 }
283
284 asmlinkage int sys_rt_sigreturn(unsigned long r2, unsigned long r3,
285                                 unsigned long r4, unsigned long r5,
286                                 unsigned long r6, unsigned long r7,
287                                 struct pt_regs * regs)
288 {
289         struct rt_sigframe __user *frame = (struct rt_sigframe __user *) (long) REF_REG_SP;
290         sigset_t set;
291         long long ret;
292
293         /* Always make any pending restarted system calls return -EINTR */
294         current->restart_block.fn = do_no_restart_syscall;
295
296         if (!access_ok(frame, sizeof(*frame)))
297                 goto badframe;
298
299         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
300                 goto badframe;
301
302         set_current_blocked(&set);
303
304         if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ret))
305                 goto badframe;
306         regs->pc -= 4;
307
308         if (restore_altstack(&frame->uc.uc_stack))
309                 goto badframe;
310
311         return (int) ret;
312
313 badframe:
314         force_sig(SIGSEGV, current);
315         return 0;
316 }
317
318 /*
319  * Set up a signal frame.
320  */
321 static int
322 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
323                  unsigned long mask)
324 {
325         int err = 0;
326
327         /* Do this first, otherwise is this sets sr->fd, that value isn't preserved. */
328         err |= setup_sigcontext_fpu(regs, sc);
329
330 #define COPY(x)         err |= __put_user(regs->x, &sc->sc_##x)
331
332         COPY(regs[0]);  COPY(regs[1]);  COPY(regs[2]);  COPY(regs[3]);
333         COPY(regs[4]);  COPY(regs[5]);  COPY(regs[6]);  COPY(regs[7]);
334         COPY(regs[8]);  COPY(regs[9]);  COPY(regs[10]); COPY(regs[11]);
335         COPY(regs[12]); COPY(regs[13]); COPY(regs[14]); COPY(regs[15]);
336         COPY(regs[16]); COPY(regs[17]); COPY(regs[18]); COPY(regs[19]);
337         COPY(regs[20]); COPY(regs[21]); COPY(regs[22]); COPY(regs[23]);
338         COPY(regs[24]); COPY(regs[25]); COPY(regs[26]); COPY(regs[27]);
339         COPY(regs[28]); COPY(regs[29]); COPY(regs[30]); COPY(regs[31]);
340         COPY(regs[32]); COPY(regs[33]); COPY(regs[34]); COPY(regs[35]);
341         COPY(regs[36]); COPY(regs[37]); COPY(regs[38]); COPY(regs[39]);
342         COPY(regs[40]); COPY(regs[41]); COPY(regs[42]); COPY(regs[43]);
343         COPY(regs[44]); COPY(regs[45]); COPY(regs[46]); COPY(regs[47]);
344         COPY(regs[48]); COPY(regs[49]); COPY(regs[50]); COPY(regs[51]);
345         COPY(regs[52]); COPY(regs[53]); COPY(regs[54]); COPY(regs[55]);
346         COPY(regs[56]); COPY(regs[57]); COPY(regs[58]); COPY(regs[59]);
347         COPY(regs[60]); COPY(regs[61]); COPY(regs[62]);
348         COPY(tregs[0]); COPY(tregs[1]); COPY(tregs[2]); COPY(tregs[3]);
349         COPY(tregs[4]); COPY(tregs[5]); COPY(tregs[6]); COPY(tregs[7]);
350         COPY(sr);       COPY(pc);
351
352 #undef COPY
353
354         err |= __put_user(mask, &sc->oldmask);
355
356         return err;
357 }
358
359 /*
360  * Determine which stack to use..
361  */
362 static inline void __user *
363 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
364 {
365         if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! sas_ss_flags(sp))
366                 sp = current->sas_ss_sp + current->sas_ss_size;
367
368         return (void __user *)((sp - frame_size) & -8ul);
369 }
370
371 void sa_default_restorer(void);         /* See comments below */
372 void sa_default_rt_restorer(void);      /* See comments below */
373
374 static int setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
375 {
376         struct sigframe __user *frame;
377         int err = 0, sig = ksig->sig;
378         int signal;
379
380         frame = get_sigframe(&ksig->ka, regs->regs[REG_SP], sizeof(*frame));
381
382         if (!access_ok(frame, sizeof(*frame)))
383                 return -EFAULT;
384
385         err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
386
387         /* Give up earlier as i386, in case */
388         if (err)
389                 return -EFAULT;
390
391         if (_NSIG_WORDS > 1) {
392                 err |= __copy_to_user(frame->extramask, &set->sig[1],
393                                       sizeof(frame->extramask)); }
394
395         /* Give up earlier as i386, in case */
396         if (err)
397                 return -EFAULT;
398
399         /* Set up to return from userspace.  If provided, use a stub
400            already in userspace.  */
401         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
402                 /*
403                  * On SH5 all edited pointers are subject to NEFF
404                  */
405                 DEREF_REG_PR = neff_sign_extend((unsigned long)
406                         ksig->ka->sa.sa_restorer | 0x1);
407         } else {
408                 /*
409                  * Different approach on SH5.
410                  * . Endianness independent asm code gets placed in entry.S .
411                  *   This is limited to four ASM instructions corresponding
412                  *   to two long longs in size.
413                  * . err checking is done on the else branch only
414                  * . flush_icache_range() is called upon __put_user() only
415                  * . all edited pointers are subject to NEFF
416                  * . being code, linker turns ShMedia bit on, always
417                  *   dereference index -1.
418                  */
419                 DEREF_REG_PR = neff_sign_extend((unsigned long)
420                         frame->retcode | 0x01);
421
422                 if (__copy_to_user(frame->retcode,
423                         (void *)((unsigned long)sa_default_restorer & (~1)), 16) != 0)
424                         return -EFAULT;
425
426                 /* Cohere the trampoline with the I-cache. */
427                 flush_cache_sigtramp(DEREF_REG_PR-1);
428         }
429
430         /*
431          * Set up registers for signal handler.
432          * All edited pointers are subject to NEFF.
433          */
434         regs->regs[REG_SP] = neff_sign_extend((unsigned long)frame);
435         regs->regs[REG_ARG1] = sig; /* Arg for signal handler */
436
437         /* FIXME:
438            The glibc profiling support for SH-5 needs to be passed a sigcontext
439            so it can retrieve the PC.  At some point during 2003 the glibc
440            support was changed to receive the sigcontext through the 2nd
441            argument, but there are still versions of libc.so in use that use
442            the 3rd argument.  Until libc.so is stabilised, pass the sigcontext
443            through both 2nd and 3rd arguments.
444         */
445
446         regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->sc;
447         regs->regs[REG_ARG3] = (unsigned long long)(unsigned long)(signed long)&frame->sc;
448
449         regs->pc = neff_sign_extend((unsigned long)ksig->ka.sa.sa_handler);
450
451         /* Broken %016Lx */
452         pr_debug("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
453                  sig, current->comm, current->pid, frame,
454                  regs->pc >> 32, regs->pc & 0xffffffff,
455                  DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
456
457         return 0;
458 }
459
460 static int setup_rt_frame(struct ksignal *kig, sigset_t *set,
461                           struct pt_regs *regs)
462 {
463         struct rt_sigframe __user *frame;
464         int err = 0, sig = ksig->sig;
465
466         frame = get_sigframe(&ksig->ka, regs->regs[REG_SP], sizeof(*frame));
467
468         if (!access_ok(frame, sizeof(*frame)))
469                 return -EFAULT;
470
471         err |= __put_user(&frame->info, &frame->pinfo);
472         err |= __put_user(&frame->uc, &frame->puc);
473         err |= copy_siginfo_to_user(&frame->info, &ksig->info);
474
475         /* Give up earlier as i386, in case */
476         if (err)
477                 return -EFAULT;
478
479         /* Create the ucontext.  */
480         err |= __put_user(0, &frame->uc.uc_flags);
481         err |= __put_user(0, &frame->uc.uc_link);
482         err |= __save_altstack(&frame->uc.uc_stack, regs->regs[REG_SP]);
483         err |= setup_sigcontext(&frame->uc.uc_mcontext,
484                                 regs, set->sig[0]);
485         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
486
487         /* Give up earlier as i386, in case */
488         if (err)
489                 return -EFAULT;
490
491         /* Set up to return from userspace.  If provided, use a stub
492            already in userspace.  */
493         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
494                 /*
495                  * On SH5 all edited pointers are subject to NEFF
496                  */
497                 DEREF_REG_PR = neff_sign_extend((unsigned long)
498                         ksig->ka.sa.sa_restorer | 0x1);
499         } else {
500                 /*
501                  * Different approach on SH5.
502                  * . Endianness independent asm code gets placed in entry.S .
503                  *   This is limited to four ASM instructions corresponding
504                  *   to two long longs in size.
505                  * . err checking is done on the else branch only
506                  * . flush_icache_range() is called upon __put_user() only
507                  * . all edited pointers are subject to NEFF
508                  * . being code, linker turns ShMedia bit on, always
509                  *   dereference index -1.
510                  */
511                 DEREF_REG_PR = neff_sign_extend((unsigned long)
512                         frame->retcode | 0x01);
513
514                 if (__copy_to_user(frame->retcode,
515                         (void *)((unsigned long)sa_default_rt_restorer & (~1)), 16) != 0)
516                         return -EFAULT;
517
518                 /* Cohere the trampoline with the I-cache. */
519                 flush_icache_range(DEREF_REG_PR-1, DEREF_REG_PR-1+15);
520         }
521
522         /*
523          * Set up registers for signal handler.
524          * All edited pointers are subject to NEFF.
525          */
526         regs->regs[REG_SP] = neff_sign_extend((unsigned long)frame);
527         regs->regs[REG_ARG1] = sig; /* Arg for signal handler */
528         regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->info;
529         regs->regs[REG_ARG3] = (unsigned long long)(unsigned long)(signed long)&frame->uc.uc_mcontext;
530         regs->pc = neff_sign_extend((unsigned long)ksig->ka.sa.sa_handler);
531
532         pr_debug("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
533                  sig, current->comm, current->pid, frame,
534                  regs->pc >> 32, regs->pc & 0xffffffff,
535                  DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
536
537         return 0;
538 }
539
540 /*
541  * OK, we're invoking a handler
542  */
543 static void
544 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
545 {
546         sigset_t *oldset = sigmask_to_save();
547         int ret;
548
549         /* Set up the stack frame */
550         if (ksig->ka.sa.sa_flags & SA_SIGINFO)
551                 ret = setup_rt_frame(ksig, oldset, regs);
552         else
553                 ret = setup_frame(ksig, oldset, regs);
554
555         signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
556 }
557
558 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
559 {
560         if (thread_info_flags & _TIF_SIGPENDING)
561                 do_signal(regs);
562
563         if (thread_info_flags & _TIF_NOTIFY_RESUME) {
564                 clear_thread_flag(TIF_NOTIFY_RESUME);
565                 tracehook_notify_resume(regs);
566         }
567 }