arm64: fpsimd: Split cpu field out from struct fpsimd_state
[linux-2.6-microblaze.git] / arch / arm64 / kernel / signal32.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  * Modified by Will Deacon <will.deacon@arm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <linux/compat.h>
22 #include <linux/signal.h>
23 #include <linux/syscalls.h>
24 #include <linux/ratelimit.h>
25
26 #include <asm/esr.h>
27 #include <asm/fpsimd.h>
28 #include <asm/signal32.h>
29 #include <asm/traps.h>
30 #include <linux/uaccess.h>
31 #include <asm/unistd.h>
32
33 struct compat_sigcontext {
34         /* We always set these two fields to 0 */
35         compat_ulong_t                  trap_no;
36         compat_ulong_t                  error_code;
37
38         compat_ulong_t                  oldmask;
39         compat_ulong_t                  arm_r0;
40         compat_ulong_t                  arm_r1;
41         compat_ulong_t                  arm_r2;
42         compat_ulong_t                  arm_r3;
43         compat_ulong_t                  arm_r4;
44         compat_ulong_t                  arm_r5;
45         compat_ulong_t                  arm_r6;
46         compat_ulong_t                  arm_r7;
47         compat_ulong_t                  arm_r8;
48         compat_ulong_t                  arm_r9;
49         compat_ulong_t                  arm_r10;
50         compat_ulong_t                  arm_fp;
51         compat_ulong_t                  arm_ip;
52         compat_ulong_t                  arm_sp;
53         compat_ulong_t                  arm_lr;
54         compat_ulong_t                  arm_pc;
55         compat_ulong_t                  arm_cpsr;
56         compat_ulong_t                  fault_address;
57 };
58
59 struct compat_ucontext {
60         compat_ulong_t                  uc_flags;
61         compat_uptr_t                   uc_link;
62         compat_stack_t                  uc_stack;
63         struct compat_sigcontext        uc_mcontext;
64         compat_sigset_t                 uc_sigmask;
65         int             __unused[32 - (sizeof (compat_sigset_t) / sizeof (int))];
66         compat_ulong_t  uc_regspace[128] __attribute__((__aligned__(8)));
67 };
68
69 struct compat_vfp_sigframe {
70         compat_ulong_t  magic;
71         compat_ulong_t  size;
72         struct compat_user_vfp {
73                 compat_u64      fpregs[32];
74                 compat_ulong_t  fpscr;
75         } ufp;
76         struct compat_user_vfp_exc {
77                 compat_ulong_t  fpexc;
78                 compat_ulong_t  fpinst;
79                 compat_ulong_t  fpinst2;
80         } ufp_exc;
81 } __attribute__((__aligned__(8)));
82
83 #define VFP_MAGIC               0x56465001
84 #define VFP_STORAGE_SIZE        sizeof(struct compat_vfp_sigframe)
85
86 #define FSR_WRITE_SHIFT         (11)
87
88 struct compat_aux_sigframe {
89         struct compat_vfp_sigframe      vfp;
90
91         /* Something that isn't a valid magic number for any coprocessor.  */
92         unsigned long                   end_magic;
93 } __attribute__((__aligned__(8)));
94
95 struct compat_sigframe {
96         struct compat_ucontext  uc;
97         compat_ulong_t          retcode[2];
98 };
99
100 struct compat_rt_sigframe {
101         struct compat_siginfo info;
102         struct compat_sigframe sig;
103 };
104
105 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
106
107 static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
108 {
109         compat_sigset_t cset;
110
111         cset.sig[0] = set->sig[0] & 0xffffffffull;
112         cset.sig[1] = set->sig[0] >> 32;
113
114         return copy_to_user(uset, &cset, sizeof(*uset));
115 }
116
117 static inline int get_sigset_t(sigset_t *set,
118                                const compat_sigset_t __user *uset)
119 {
120         compat_sigset_t s32;
121
122         if (copy_from_user(&s32, uset, sizeof(*uset)))
123                 return -EFAULT;
124
125         set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
126         return 0;
127 }
128
129 /*
130  * VFP save/restore code.
131  *
132  * We have to be careful with endianness, since the fpsimd context-switch
133  * code operates on 128-bit (Q) register values whereas the compat ABI
134  * uses an array of 64-bit (D) registers. Consequently, we need to swap
135  * the two halves of each Q register when running on a big-endian CPU.
136  */
137 union __fpsimd_vreg {
138         __uint128_t     raw;
139         struct {
140 #ifdef __AARCH64EB__
141                 u64     hi;
142                 u64     lo;
143 #else
144                 u64     lo;
145                 u64     hi;
146 #endif
147         };
148 };
149
150 static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame)
151 {
152         struct user_fpsimd_state const *fpsimd = &current->thread.fpsimd_state;
153         compat_ulong_t magic = VFP_MAGIC;
154         compat_ulong_t size = VFP_STORAGE_SIZE;
155         compat_ulong_t fpscr, fpexc;
156         int i, err = 0;
157
158         /*
159          * Save the hardware registers to the fpsimd_state structure.
160          * Note that this also saves V16-31, which aren't visible
161          * in AArch32.
162          */
163         fpsimd_signal_preserve_current_state();
164
165         /* Place structure header on the stack */
166         __put_user_error(magic, &frame->magic, err);
167         __put_user_error(size, &frame->size, err);
168
169         /*
170          * Now copy the FP registers. Since the registers are packed,
171          * we can copy the prefix we want (V0-V15) as it is.
172          */
173         for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
174                 union __fpsimd_vreg vreg = {
175                         .raw = fpsimd->vregs[i >> 1],
176                 };
177
178                 __put_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
179                 __put_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
180         }
181
182         /* Create an AArch32 fpscr from the fpsr and the fpcr. */
183         fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) |
184                 (fpsimd->fpcr & VFP_FPSCR_CTRL_MASK);
185         __put_user_error(fpscr, &frame->ufp.fpscr, err);
186
187         /*
188          * The exception register aren't available so we fake up a
189          * basic FPEXC and zero everything else.
190          */
191         fpexc = (1 << 30);
192         __put_user_error(fpexc, &frame->ufp_exc.fpexc, err);
193         __put_user_error(0, &frame->ufp_exc.fpinst, err);
194         __put_user_error(0, &frame->ufp_exc.fpinst2, err);
195
196         return err ? -EFAULT : 0;
197 }
198
199 static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame)
200 {
201         struct user_fpsimd_state fpsimd;
202         compat_ulong_t magic = VFP_MAGIC;
203         compat_ulong_t size = VFP_STORAGE_SIZE;
204         compat_ulong_t fpscr;
205         int i, err = 0;
206
207         __get_user_error(magic, &frame->magic, err);
208         __get_user_error(size, &frame->size, err);
209
210         if (err)
211                 return -EFAULT;
212         if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
213                 return -EINVAL;
214
215         /* Copy the FP registers into the start of the fpsimd_state. */
216         for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
217                 union __fpsimd_vreg vreg;
218
219                 __get_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
220                 __get_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
221                 fpsimd.vregs[i >> 1] = vreg.raw;
222         }
223
224         /* Extract the fpsr and the fpcr from the fpscr */
225         __get_user_error(fpscr, &frame->ufp.fpscr, err);
226         fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK;
227         fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
228
229         /*
230          * We don't need to touch the exception register, so
231          * reload the hardware state.
232          */
233         if (!err)
234                 fpsimd_update_current_state(&fpsimd);
235
236         return err ? -EFAULT : 0;
237 }
238
239 static int compat_restore_sigframe(struct pt_regs *regs,
240                                    struct compat_sigframe __user *sf)
241 {
242         int err;
243         sigset_t set;
244         struct compat_aux_sigframe __user *aux;
245
246         err = get_sigset_t(&set, &sf->uc.uc_sigmask);
247         if (err == 0) {
248                 sigdelsetmask(&set, ~_BLOCKABLE);
249                 set_current_blocked(&set);
250         }
251
252         __get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
253         __get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
254         __get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
255         __get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
256         __get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
257         __get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
258         __get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
259         __get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
260         __get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
261         __get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
262         __get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
263         __get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
264         __get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
265         __get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
266         __get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
267         __get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
268         __get_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
269
270         /*
271          * Avoid compat_sys_sigreturn() restarting.
272          */
273         forget_syscall(regs);
274
275         err |= !valid_user_regs(&regs->user_regs, current);
276
277         aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
278         if (err == 0)
279                 err |= compat_restore_vfp_context(&aux->vfp);
280
281         return err;
282 }
283
284 asmlinkage int compat_sys_sigreturn(struct pt_regs *regs)
285 {
286         struct compat_sigframe __user *frame;
287
288         /* Always make any pending restarted system calls return -EINTR */
289         current->restart_block.fn = do_no_restart_syscall;
290
291         /*
292          * Since we stacked the signal on a 64-bit boundary,
293          * then 'sp' should be word aligned here.  If it's
294          * not, then the user is trying to mess with us.
295          */
296         if (regs->compat_sp & 7)
297                 goto badframe;
298
299         frame = (struct compat_sigframe __user *)regs->compat_sp;
300
301         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
302                 goto badframe;
303
304         if (compat_restore_sigframe(regs, frame))
305                 goto badframe;
306
307         return regs->regs[0];
308
309 badframe:
310         arm64_notify_segfault(regs->compat_sp);
311         return 0;
312 }
313
314 asmlinkage int compat_sys_rt_sigreturn(struct pt_regs *regs)
315 {
316         struct compat_rt_sigframe __user *frame;
317
318         /* Always make any pending restarted system calls return -EINTR */
319         current->restart_block.fn = do_no_restart_syscall;
320
321         /*
322          * Since we stacked the signal on a 64-bit boundary,
323          * then 'sp' should be word aligned here.  If it's
324          * not, then the user is trying to mess with us.
325          */
326         if (regs->compat_sp & 7)
327                 goto badframe;
328
329         frame = (struct compat_rt_sigframe __user *)regs->compat_sp;
330
331         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
332                 goto badframe;
333
334         if (compat_restore_sigframe(regs, &frame->sig))
335                 goto badframe;
336
337         if (compat_restore_altstack(&frame->sig.uc.uc_stack))
338                 goto badframe;
339
340         return regs->regs[0];
341
342 badframe:
343         arm64_notify_segfault(regs->compat_sp);
344         return 0;
345 }
346
347 static void __user *compat_get_sigframe(struct ksignal *ksig,
348                                         struct pt_regs *regs,
349                                         int framesize)
350 {
351         compat_ulong_t sp = sigsp(regs->compat_sp, ksig);
352         void __user *frame;
353
354         /*
355          * ATPCS B01 mandates 8-byte alignment
356          */
357         frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7));
358
359         /*
360          * Check that we can actually write to the signal frame.
361          */
362         if (!access_ok(VERIFY_WRITE, frame, framesize))
363                 frame = NULL;
364
365         return frame;
366 }
367
368 static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
369                                 compat_ulong_t __user *rc, void __user *frame,
370                                 int usig)
371 {
372         compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler);
373         compat_ulong_t retcode;
374         compat_ulong_t spsr = regs->pstate & ~(PSR_f | COMPAT_PSR_E_BIT);
375         int thumb;
376
377         /* Check if the handler is written for ARM or Thumb */
378         thumb = handler & 1;
379
380         if (thumb)
381                 spsr |= COMPAT_PSR_T_BIT;
382         else
383                 spsr &= ~COMPAT_PSR_T_BIT;
384
385         /* The IT state must be cleared for both ARM and Thumb-2 */
386         spsr &= ~COMPAT_PSR_IT_MASK;
387
388         /* Restore the original endianness */
389         spsr |= COMPAT_PSR_ENDSTATE;
390
391         if (ka->sa.sa_flags & SA_RESTORER) {
392                 retcode = ptr_to_compat(ka->sa.sa_restorer);
393         } else {
394                 /* Set up sigreturn pointer */
395                 unsigned int idx = thumb << 1;
396
397                 if (ka->sa.sa_flags & SA_SIGINFO)
398                         idx += 3;
399
400                 retcode = AARCH32_VECTORS_BASE +
401                           AARCH32_KERN_SIGRET_CODE_OFFSET +
402                           (idx << 2) + thumb;
403         }
404
405         regs->regs[0]   = usig;
406         regs->compat_sp = ptr_to_compat(frame);
407         regs->compat_lr = retcode;
408         regs->pc        = handler;
409         regs->pstate    = spsr;
410 }
411
412 static int compat_setup_sigframe(struct compat_sigframe __user *sf,
413                                  struct pt_regs *regs, sigset_t *set)
414 {
415         struct compat_aux_sigframe __user *aux;
416         int err = 0;
417
418         __put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
419         __put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
420         __put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
421         __put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
422         __put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
423         __put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
424         __put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
425         __put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
426         __put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
427         __put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
428         __put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
429         __put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
430         __put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
431         __put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
432         __put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
433         __put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
434         __put_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
435
436         __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err);
437         /* set the compat FSR WnR */
438         __put_user_error(!!(current->thread.fault_code & ESR_ELx_WNR) <<
439                          FSR_WRITE_SHIFT, &sf->uc.uc_mcontext.error_code, err);
440         __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
441         __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
442
443         err |= put_sigset_t(&sf->uc.uc_sigmask, set);
444
445         aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
446
447         if (err == 0)
448                 err |= compat_preserve_vfp_context(&aux->vfp);
449         __put_user_error(0, &aux->end_magic, err);
450
451         return err;
452 }
453
454 /*
455  * 32-bit signal handling routines called from signal.c
456  */
457 int compat_setup_rt_frame(int usig, struct ksignal *ksig,
458                           sigset_t *set, struct pt_regs *regs)
459 {
460         struct compat_rt_sigframe __user *frame;
461         int err = 0;
462
463         frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
464
465         if (!frame)
466                 return 1;
467
468         err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
469
470         __put_user_error(0, &frame->sig.uc.uc_flags, err);
471         __put_user_error(0, &frame->sig.uc.uc_link, err);
472
473         err |= __compat_save_altstack(&frame->sig.uc.uc_stack, regs->compat_sp);
474
475         err |= compat_setup_sigframe(&frame->sig, regs, set);
476
477         if (err == 0) {
478                 compat_setup_return(regs, &ksig->ka, frame->sig.retcode, frame, usig);
479                 regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
480                 regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
481         }
482
483         return err;
484 }
485
486 int compat_setup_frame(int usig, struct ksignal *ksig, sigset_t *set,
487                        struct pt_regs *regs)
488 {
489         struct compat_sigframe __user *frame;
490         int err = 0;
491
492         frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
493
494         if (!frame)
495                 return 1;
496
497         __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
498
499         err |= compat_setup_sigframe(frame, regs, set);
500         if (err == 0)
501                 compat_setup_return(regs, &ksig->ka, frame->retcode, frame, usig);
502
503         return err;
504 }
505
506 void compat_setup_restart_syscall(struct pt_regs *regs)
507 {
508        regs->regs[7] = __NR_compat_restart_syscall;
509 }