e3cf09626245aea9440aafbbbe5d84ad2d0ea5a6
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / kernel / signal.c
1 /*
2  * Based on arch/arm/kernel/signal.c
3  *
4  * Copyright (C) 1995-2009 Russell King
5  * Copyright (C) 2012 ARM Ltd.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/compat.h>
21 #include <linux/errno.h>
22 #include <linux/signal.h>
23 #include <linux/personality.h>
24 #include <linux/freezer.h>
25 #include <linux/uaccess.h>
26 #include <linux/tracehook.h>
27 #include <linux/ratelimit.h>
28
29 #include <asm/debug-monitors.h>
30 #include <asm/elf.h>
31 #include <asm/cacheflush.h>
32 #include <asm/ucontext.h>
33 #include <asm/unistd.h>
34 #include <asm/fpsimd.h>
35 #include <asm/signal32.h>
36 #include <asm/vdso.h>
37
38 /*
39  * Do a signal return; undo the signal stack. These are aligned to 128-bit.
40  */
41 struct rt_sigframe {
42         struct siginfo info;
43         struct ucontext uc;
44         u64 fp;
45         u64 lr;
46 };
47
48 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
49 {
50         struct fpsimd_state *fpsimd = &current->thread.fpsimd_state;
51         int err;
52
53         /* dump the hardware registers to the fpsimd_state structure */
54         fpsimd_save_state(fpsimd);
55
56         /* copy the FP and status/control registers */
57         err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
58         __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
59         __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
60
61         /* copy the magic/size information */
62         __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
63         __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
64
65         return err ? -EFAULT : 0;
66 }
67
68 static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
69 {
70         struct fpsimd_state fpsimd;
71         __u32 magic, size;
72         int err = 0;
73
74         /* check the magic/size information */
75         __get_user_error(magic, &ctx->head.magic, err);
76         __get_user_error(size, &ctx->head.size, err);
77         if (err)
78                 return -EFAULT;
79         if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
80                 return -EINVAL;
81
82         /* copy the FP and status/control registers */
83         err = __copy_from_user(fpsimd.vregs, ctx->vregs,
84                                sizeof(fpsimd.vregs));
85         __get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
86         __get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
87
88         /* load the hardware registers from the fpsimd_state structure */
89         if (!err) {
90                 preempt_disable();
91                 fpsimd_load_state(&fpsimd);
92                 preempt_enable();
93         }
94
95         return err ? -EFAULT : 0;
96 }
97
98 static int restore_sigframe(struct pt_regs *regs,
99                             struct rt_sigframe __user *sf)
100 {
101         sigset_t set;
102         int i, err;
103         void *aux = sf->uc.uc_mcontext.__reserved;
104
105         err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
106         if (err == 0)
107                 set_current_blocked(&set);
108
109         for (i = 0; i < 31; i++)
110                 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
111                                  err);
112         __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
113         __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
114         __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
115
116         /*
117          * Avoid sys_rt_sigreturn() restarting.
118          */
119         regs->syscallno = ~0UL;
120
121         err |= !valid_user_regs(&regs->user_regs);
122
123         if (err == 0) {
124                 struct fpsimd_context *fpsimd_ctx =
125                         container_of(aux, struct fpsimd_context, head);
126                 err |= restore_fpsimd_context(fpsimd_ctx);
127         }
128
129         return err;
130 }
131
132 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
133 {
134         struct rt_sigframe __user *frame;
135
136         /* Always make any pending restarted system calls return -EINTR */
137         current_thread_info()->restart_block.fn = do_no_restart_syscall;
138
139         /*
140          * Since we stacked the signal on a 128-bit boundary, then 'sp' should
141          * be word aligned here.
142          */
143         if (regs->sp & 15)
144                 goto badframe;
145
146         frame = (struct rt_sigframe __user *)regs->sp;
147
148         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
149                 goto badframe;
150
151         if (restore_sigframe(regs, frame))
152                 goto badframe;
153
154         if (restore_altstack(&frame->uc.uc_stack))
155                 goto badframe;
156
157         return regs->regs[0];
158
159 badframe:
160         if (show_unhandled_signals)
161                 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
162                                     current->comm, task_pid_nr(current), __func__,
163                                     regs->pc, regs->sp);
164         force_sig(SIGSEGV, current);
165         return 0;
166 }
167
168 static int setup_sigframe(struct rt_sigframe __user *sf,
169                           struct pt_regs *regs, sigset_t *set)
170 {
171         int i, err = 0;
172         void *aux = sf->uc.uc_mcontext.__reserved;
173         struct _aarch64_ctx *end;
174
175         /* set up the stack frame for unwinding */
176         __put_user_error(regs->regs[29], &sf->fp, err);
177         __put_user_error(regs->regs[30], &sf->lr, err);
178
179         for (i = 0; i < 31; i++)
180                 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
181                                  err);
182         __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
183         __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
184         __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
185
186         __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
187
188         err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
189
190         if (err == 0) {
191                 struct fpsimd_context *fpsimd_ctx =
192                         container_of(aux, struct fpsimd_context, head);
193                 err |= preserve_fpsimd_context(fpsimd_ctx);
194                 aux += sizeof(*fpsimd_ctx);
195         }
196
197         /* set the "end" magic */
198         end = aux;
199         __put_user_error(0, &end->magic, err);
200         __put_user_error(0, &end->size, err);
201
202         return err;
203 }
204
205 static struct rt_sigframe __user *get_sigframe(struct k_sigaction *ka,
206                                                struct pt_regs *regs)
207 {
208         unsigned long sp, sp_top;
209         struct rt_sigframe __user *frame;
210
211         sp = sp_top = regs->sp;
212
213         /*
214          * This is the X/Open sanctioned signal stack switching.
215          */
216         if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
217                 sp = sp_top = current->sas_ss_sp + current->sas_ss_size;
218
219         sp = (sp - sizeof(struct rt_sigframe)) & ~15;
220         frame = (struct rt_sigframe __user *)sp;
221
222         /*
223          * Check that we can actually write to the signal frame.
224          */
225         if (!access_ok(VERIFY_WRITE, frame, sp_top - sp))
226                 frame = NULL;
227
228         return frame;
229 }
230
231 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
232                          void __user *frame, int usig)
233 {
234         __sigrestore_t sigtramp;
235
236         regs->regs[0] = usig;
237         regs->sp = (unsigned long)frame;
238         regs->regs[29] = regs->sp + offsetof(struct rt_sigframe, fp);
239         regs->pc = (unsigned long)ka->sa.sa_handler;
240
241         if (ka->sa.sa_flags & SA_RESTORER)
242                 sigtramp = ka->sa.sa_restorer;
243         else
244                 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
245
246         regs->regs[30] = (unsigned long)sigtramp;
247 }
248
249 static int setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
250                           sigset_t *set, struct pt_regs *regs)
251 {
252         struct rt_sigframe __user *frame;
253         int err = 0;
254
255         frame = get_sigframe(ka, regs);
256         if (!frame)
257                 return 1;
258
259         __put_user_error(0, &frame->uc.uc_flags, err);
260         __put_user_error(NULL, &frame->uc.uc_link, err);
261
262         err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
263         err |= setup_sigframe(frame, regs, set);
264         if (err == 0) {
265                 setup_return(regs, ka, frame, usig);
266                 if (ka->sa.sa_flags & SA_SIGINFO) {
267                         err |= copy_siginfo_to_user(&frame->info, info);
268                         regs->regs[1] = (unsigned long)&frame->info;
269                         regs->regs[2] = (unsigned long)&frame->uc;
270                 }
271         }
272
273         return err;
274 }
275
276 static void setup_restart_syscall(struct pt_regs *regs)
277 {
278         if (is_compat_task())
279                 compat_setup_restart_syscall(regs);
280         else
281                 regs->regs[8] = __NR_restart_syscall;
282 }
283
284 /*
285  * OK, we're invoking a handler
286  */
287 static void handle_signal(unsigned long sig, struct k_sigaction *ka,
288                           siginfo_t *info, struct pt_regs *regs)
289 {
290         struct thread_info *thread = current_thread_info();
291         struct task_struct *tsk = current;
292         sigset_t *oldset = sigmask_to_save();
293         int usig = sig;
294         int ret;
295
296         /*
297          * translate the signal
298          */
299         if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
300                 usig = thread->exec_domain->signal_invmap[usig];
301
302         /*
303          * Set up the stack frame
304          */
305         if (is_compat_task()) {
306                 if (ka->sa.sa_flags & SA_SIGINFO)
307                         ret = compat_setup_rt_frame(usig, ka, info, oldset,
308                                                     regs);
309                 else
310                         ret = compat_setup_frame(usig, ka, oldset, regs);
311         } else {
312                 ret = setup_rt_frame(usig, ka, info, oldset, regs);
313         }
314
315         /*
316          * Check that the resulting registers are actually sane.
317          */
318         ret |= !valid_user_regs(&regs->user_regs);
319
320         if (ret != 0) {
321                 force_sigsegv(sig, tsk);
322                 return;
323         }
324
325         /*
326          * Fast forward the stepping logic so we step into the signal
327          * handler.
328          */
329         user_fastforward_single_step(tsk);
330
331         signal_delivered(sig, info, ka, regs, 0);
332 }
333
334 /*
335  * Note that 'init' is a special process: it doesn't get signals it doesn't
336  * want to handle. Thus you cannot kill init even with a SIGKILL even by
337  * mistake.
338  *
339  * Note that we go through the signals twice: once to check the signals that
340  * the kernel can handle, and then we build all the user-level signal handling
341  * stack-frames in one go after that.
342  */
343 static void do_signal(struct pt_regs *regs)
344 {
345         unsigned long continue_addr = 0, restart_addr = 0;
346         struct k_sigaction ka;
347         siginfo_t info;
348         int signr, retval = 0;
349         int syscall = (int)regs->syscallno;
350
351         /*
352          * If we were from a system call, check for system call restarting...
353          */
354         if (syscall >= 0) {
355                 continue_addr = regs->pc;
356                 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
357                 retval = regs->regs[0];
358
359                 /*
360                  * Avoid additional syscall restarting via ret_to_user.
361                  */
362                 regs->syscallno = ~0UL;
363
364                 /*
365                  * Prepare for system call restart. We do this here so that a
366                  * debugger will see the already changed PC.
367                  */
368                 switch (retval) {
369                 case -ERESTARTNOHAND:
370                 case -ERESTARTSYS:
371                 case -ERESTARTNOINTR:
372                 case -ERESTART_RESTARTBLOCK:
373                         regs->regs[0] = regs->orig_x0;
374                         regs->pc = restart_addr;
375                         break;
376                 }
377         }
378
379         /*
380          * Get the signal to deliver. When running under ptrace, at this point
381          * the debugger may change all of our registers.
382          */
383         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
384         if (signr > 0) {
385                 /*
386                  * Depending on the signal settings, we may need to revert the
387                  * decision to restart the system call, but skip this if a
388                  * debugger has chosen to restart at a different PC.
389                  */
390                 if (regs->pc == restart_addr &&
391                     (retval == -ERESTARTNOHAND ||
392                      retval == -ERESTART_RESTARTBLOCK ||
393                      (retval == -ERESTARTSYS &&
394                       !(ka.sa.sa_flags & SA_RESTART)))) {
395                         regs->regs[0] = -EINTR;
396                         regs->pc = continue_addr;
397                 }
398
399                 handle_signal(signr, &ka, &info, regs);
400                 return;
401         }
402
403         /*
404          * Handle restarting a different system call. As above, if a debugger
405          * has chosen to restart at a different PC, ignore the restart.
406          */
407         if (syscall >= 0 && regs->pc == restart_addr) {
408                 if (retval == -ERESTART_RESTARTBLOCK)
409                         setup_restart_syscall(regs);
410                 user_rewind_single_step(current);
411         }
412
413         restore_saved_sigmask();
414 }
415
416 asmlinkage void do_notify_resume(struct pt_regs *regs,
417                                  unsigned int thread_flags)
418 {
419         if (thread_flags & _TIF_SIGPENDING)
420                 do_signal(regs);
421
422         if (thread_flags & _TIF_NOTIFY_RESUME) {
423                 clear_thread_flag(TIF_NOTIFY_RESUME);
424                 tracehook_notify_resume(regs);
425         }
426 }