arm64: Rework valid_user_regs
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / kernel / ptrace.c
1 /*
2  * Based on arch/arm/kernel/ptrace.c
3  *
4  * By Ross Biro 1/23/92
5  * edited by Linus Torvalds
6  * ARM modifications Copyright (C) 2000 Russell King
7  * Copyright (C) 2012 ARM Ltd.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <linux/audit.h>
23 #include <linux/compat.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/mm.h>
27 #include <linux/smp.h>
28 #include <linux/ptrace.h>
29 #include <linux/user.h>
30 #include <linux/seccomp.h>
31 #include <linux/security.h>
32 #include <linux/init.h>
33 #include <linux/signal.h>
34 #include <linux/uaccess.h>
35 #include <linux/perf_event.h>
36 #include <linux/hw_breakpoint.h>
37 #include <linux/regset.h>
38 #include <linux/tracehook.h>
39 #include <linux/elf.h>
40
41 #include <asm/compat.h>
42 #include <asm/cpufeature.h>
43 #include <asm/debug-monitors.h>
44 #include <asm/pgtable.h>
45 #include <asm/syscall.h>
46 #include <asm/traps.h>
47 #include <asm/system_misc.h>
48
49 #define CREATE_TRACE_POINTS
50 #include <trace/events/syscalls.h>
51
52 /*
53  * TODO: does not yet catch signals sent when the child dies.
54  * in exit.c or in signal.c.
55  */
56
57 /*
58  * Called by kernel/ptrace.c when detaching..
59  */
60 void ptrace_disable(struct task_struct *child)
61 {
62         /*
63          * This would be better off in core code, but PTRACE_DETACH has
64          * grown its fair share of arch-specific worts and changing it
65          * is likely to cause regressions on obscure architectures.
66          */
67         user_disable_single_step(child);
68 }
69
70 #ifdef CONFIG_HAVE_HW_BREAKPOINT
71 /*
72  * Handle hitting a HW-breakpoint.
73  */
74 static void ptrace_hbptriggered(struct perf_event *bp,
75                                 struct perf_sample_data *data,
76                                 struct pt_regs *regs)
77 {
78         struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
79         siginfo_t info = {
80                 .si_signo       = SIGTRAP,
81                 .si_errno       = 0,
82                 .si_code        = TRAP_HWBKPT,
83                 .si_addr        = (void __user *)(bkpt->trigger),
84         };
85
86 #ifdef CONFIG_COMPAT
87         int i;
88
89         if (!is_compat_task())
90                 goto send_sig;
91
92         for (i = 0; i < ARM_MAX_BRP; ++i) {
93                 if (current->thread.debug.hbp_break[i] == bp) {
94                         info.si_errno = (i << 1) + 1;
95                         break;
96                 }
97         }
98
99         for (i = 0; i < ARM_MAX_WRP; ++i) {
100                 if (current->thread.debug.hbp_watch[i] == bp) {
101                         info.si_errno = -((i << 1) + 1);
102                         break;
103                 }
104         }
105
106 send_sig:
107 #endif
108         force_sig_info(SIGTRAP, &info, current);
109 }
110
111 /*
112  * Unregister breakpoints from this task and reset the pointers in
113  * the thread_struct.
114  */
115 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
116 {
117         int i;
118         struct thread_struct *t = &tsk->thread;
119
120         for (i = 0; i < ARM_MAX_BRP; i++) {
121                 if (t->debug.hbp_break[i]) {
122                         unregister_hw_breakpoint(t->debug.hbp_break[i]);
123                         t->debug.hbp_break[i] = NULL;
124                 }
125         }
126
127         for (i = 0; i < ARM_MAX_WRP; i++) {
128                 if (t->debug.hbp_watch[i]) {
129                         unregister_hw_breakpoint(t->debug.hbp_watch[i]);
130                         t->debug.hbp_watch[i] = NULL;
131                 }
132         }
133 }
134
135 void ptrace_hw_copy_thread(struct task_struct *tsk)
136 {
137         memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
138 }
139
140 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
141                                                struct task_struct *tsk,
142                                                unsigned long idx)
143 {
144         struct perf_event *bp = ERR_PTR(-EINVAL);
145
146         switch (note_type) {
147         case NT_ARM_HW_BREAK:
148                 if (idx < ARM_MAX_BRP)
149                         bp = tsk->thread.debug.hbp_break[idx];
150                 break;
151         case NT_ARM_HW_WATCH:
152                 if (idx < ARM_MAX_WRP)
153                         bp = tsk->thread.debug.hbp_watch[idx];
154                 break;
155         }
156
157         return bp;
158 }
159
160 static int ptrace_hbp_set_event(unsigned int note_type,
161                                 struct task_struct *tsk,
162                                 unsigned long idx,
163                                 struct perf_event *bp)
164 {
165         int err = -EINVAL;
166
167         switch (note_type) {
168         case NT_ARM_HW_BREAK:
169                 if (idx < ARM_MAX_BRP) {
170                         tsk->thread.debug.hbp_break[idx] = bp;
171                         err = 0;
172                 }
173                 break;
174         case NT_ARM_HW_WATCH:
175                 if (idx < ARM_MAX_WRP) {
176                         tsk->thread.debug.hbp_watch[idx] = bp;
177                         err = 0;
178                 }
179                 break;
180         }
181
182         return err;
183 }
184
185 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
186                                             struct task_struct *tsk,
187                                             unsigned long idx)
188 {
189         struct perf_event *bp;
190         struct perf_event_attr attr;
191         int err, type;
192
193         switch (note_type) {
194         case NT_ARM_HW_BREAK:
195                 type = HW_BREAKPOINT_X;
196                 break;
197         case NT_ARM_HW_WATCH:
198                 type = HW_BREAKPOINT_RW;
199                 break;
200         default:
201                 return ERR_PTR(-EINVAL);
202         }
203
204         ptrace_breakpoint_init(&attr);
205
206         /*
207          * Initialise fields to sane defaults
208          * (i.e. values that will pass validation).
209          */
210         attr.bp_addr    = 0;
211         attr.bp_len     = HW_BREAKPOINT_LEN_4;
212         attr.bp_type    = type;
213         attr.disabled   = 1;
214
215         bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
216         if (IS_ERR(bp))
217                 return bp;
218
219         err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
220         if (err)
221                 return ERR_PTR(err);
222
223         return bp;
224 }
225
226 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
227                                      struct arch_hw_breakpoint_ctrl ctrl,
228                                      struct perf_event_attr *attr)
229 {
230         int err, len, type, disabled = !ctrl.enabled;
231
232         attr->disabled = disabled;
233         if (disabled)
234                 return 0;
235
236         err = arch_bp_generic_fields(ctrl, &len, &type);
237         if (err)
238                 return err;
239
240         switch (note_type) {
241         case NT_ARM_HW_BREAK:
242                 if ((type & HW_BREAKPOINT_X) != type)
243                         return -EINVAL;
244                 break;
245         case NT_ARM_HW_WATCH:
246                 if ((type & HW_BREAKPOINT_RW) != type)
247                         return -EINVAL;
248                 break;
249         default:
250                 return -EINVAL;
251         }
252
253         attr->bp_len    = len;
254         attr->bp_type   = type;
255
256         return 0;
257 }
258
259 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
260 {
261         u8 num;
262         u32 reg = 0;
263
264         switch (note_type) {
265         case NT_ARM_HW_BREAK:
266                 num = hw_breakpoint_slots(TYPE_INST);
267                 break;
268         case NT_ARM_HW_WATCH:
269                 num = hw_breakpoint_slots(TYPE_DATA);
270                 break;
271         default:
272                 return -EINVAL;
273         }
274
275         reg |= debug_monitors_arch();
276         reg <<= 8;
277         reg |= num;
278
279         *info = reg;
280         return 0;
281 }
282
283 static int ptrace_hbp_get_ctrl(unsigned int note_type,
284                                struct task_struct *tsk,
285                                unsigned long idx,
286                                u32 *ctrl)
287 {
288         struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
289
290         if (IS_ERR(bp))
291                 return PTR_ERR(bp);
292
293         *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
294         return 0;
295 }
296
297 static int ptrace_hbp_get_addr(unsigned int note_type,
298                                struct task_struct *tsk,
299                                unsigned long idx,
300                                u64 *addr)
301 {
302         struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
303
304         if (IS_ERR(bp))
305                 return PTR_ERR(bp);
306
307         *addr = bp ? bp->attr.bp_addr : 0;
308         return 0;
309 }
310
311 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
312                                                         struct task_struct *tsk,
313                                                         unsigned long idx)
314 {
315         struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
316
317         if (!bp)
318                 bp = ptrace_hbp_create(note_type, tsk, idx);
319
320         return bp;
321 }
322
323 static int ptrace_hbp_set_ctrl(unsigned int note_type,
324                                struct task_struct *tsk,
325                                unsigned long idx,
326                                u32 uctrl)
327 {
328         int err;
329         struct perf_event *bp;
330         struct perf_event_attr attr;
331         struct arch_hw_breakpoint_ctrl ctrl;
332
333         bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
334         if (IS_ERR(bp)) {
335                 err = PTR_ERR(bp);
336                 return err;
337         }
338
339         attr = bp->attr;
340         decode_ctrl_reg(uctrl, &ctrl);
341         err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
342         if (err)
343                 return err;
344
345         return modify_user_hw_breakpoint(bp, &attr);
346 }
347
348 static int ptrace_hbp_set_addr(unsigned int note_type,
349                                struct task_struct *tsk,
350                                unsigned long idx,
351                                u64 addr)
352 {
353         int err;
354         struct perf_event *bp;
355         struct perf_event_attr attr;
356
357         bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
358         if (IS_ERR(bp)) {
359                 err = PTR_ERR(bp);
360                 return err;
361         }
362
363         attr = bp->attr;
364         attr.bp_addr = addr;
365         err = modify_user_hw_breakpoint(bp, &attr);
366         return err;
367 }
368
369 #define PTRACE_HBP_ADDR_SZ      sizeof(u64)
370 #define PTRACE_HBP_CTRL_SZ      sizeof(u32)
371 #define PTRACE_HBP_PAD_SZ       sizeof(u32)
372
373 static int hw_break_get(struct task_struct *target,
374                         const struct user_regset *regset,
375                         unsigned int pos, unsigned int count,
376                         void *kbuf, void __user *ubuf)
377 {
378         unsigned int note_type = regset->core_note_type;
379         int ret, idx = 0, offset, limit;
380         u32 info, ctrl;
381         u64 addr;
382
383         /* Resource info */
384         ret = ptrace_hbp_get_resource_info(note_type, &info);
385         if (ret)
386                 return ret;
387
388         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
389                                   sizeof(info));
390         if (ret)
391                 return ret;
392
393         /* Pad */
394         offset = offsetof(struct user_hwdebug_state, pad);
395         ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
396                                        offset + PTRACE_HBP_PAD_SZ);
397         if (ret)
398                 return ret;
399
400         /* (address, ctrl) registers */
401         offset = offsetof(struct user_hwdebug_state, dbg_regs);
402         limit = regset->n * regset->size;
403         while (count && offset < limit) {
404                 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
405                 if (ret)
406                         return ret;
407                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
408                                           offset, offset + PTRACE_HBP_ADDR_SZ);
409                 if (ret)
410                         return ret;
411                 offset += PTRACE_HBP_ADDR_SZ;
412
413                 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
414                 if (ret)
415                         return ret;
416                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
417                                           offset, offset + PTRACE_HBP_CTRL_SZ);
418                 if (ret)
419                         return ret;
420                 offset += PTRACE_HBP_CTRL_SZ;
421
422                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
423                                                offset,
424                                                offset + PTRACE_HBP_PAD_SZ);
425                 if (ret)
426                         return ret;
427                 offset += PTRACE_HBP_PAD_SZ;
428                 idx++;
429         }
430
431         return 0;
432 }
433
434 static int hw_break_set(struct task_struct *target,
435                         const struct user_regset *regset,
436                         unsigned int pos, unsigned int count,
437                         const void *kbuf, const void __user *ubuf)
438 {
439         unsigned int note_type = regset->core_note_type;
440         int ret, idx = 0, offset, limit;
441         u32 ctrl;
442         u64 addr;
443
444         /* Resource info and pad */
445         offset = offsetof(struct user_hwdebug_state, dbg_regs);
446         ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
447         if (ret)
448                 return ret;
449
450         /* (address, ctrl) registers */
451         limit = regset->n * regset->size;
452         while (count && offset < limit) {
453                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
454                                          offset, offset + PTRACE_HBP_ADDR_SZ);
455                 if (ret)
456                         return ret;
457                 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
458                 if (ret)
459                         return ret;
460                 offset += PTRACE_HBP_ADDR_SZ;
461
462                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
463                                          offset, offset + PTRACE_HBP_CTRL_SZ);
464                 if (ret)
465                         return ret;
466                 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
467                 if (ret)
468                         return ret;
469                 offset += PTRACE_HBP_CTRL_SZ;
470
471                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
472                                                 offset,
473                                                 offset + PTRACE_HBP_PAD_SZ);
474                 if (ret)
475                         return ret;
476                 offset += PTRACE_HBP_PAD_SZ;
477                 idx++;
478         }
479
480         return 0;
481 }
482 #endif  /* CONFIG_HAVE_HW_BREAKPOINT */
483
484 static int gpr_get(struct task_struct *target,
485                    const struct user_regset *regset,
486                    unsigned int pos, unsigned int count,
487                    void *kbuf, void __user *ubuf)
488 {
489         struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
490         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
491 }
492
493 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
494                    unsigned int pos, unsigned int count,
495                    const void *kbuf, const void __user *ubuf)
496 {
497         int ret;
498         struct user_pt_regs newregs;
499
500         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
501         if (ret)
502                 return ret;
503
504         if (!valid_user_regs(&newregs, target))
505                 return -EINVAL;
506
507         task_pt_regs(target)->user_regs = newregs;
508         return 0;
509 }
510
511 /*
512  * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
513  */
514 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
515                    unsigned int pos, unsigned int count,
516                    void *kbuf, void __user *ubuf)
517 {
518         struct user_fpsimd_state *uregs;
519         uregs = &target->thread.fpsimd_state.user_fpsimd;
520         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
521 }
522
523 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
524                    unsigned int pos, unsigned int count,
525                    const void *kbuf, const void __user *ubuf)
526 {
527         int ret;
528         struct user_fpsimd_state newstate;
529
530         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
531         if (ret)
532                 return ret;
533
534         target->thread.fpsimd_state.user_fpsimd = newstate;
535         fpsimd_flush_task_state(target);
536         return ret;
537 }
538
539 static int tls_get(struct task_struct *target, const struct user_regset *regset,
540                    unsigned int pos, unsigned int count,
541                    void *kbuf, void __user *ubuf)
542 {
543         unsigned long *tls = &target->thread.tp_value;
544         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
545 }
546
547 static int tls_set(struct task_struct *target, const struct user_regset *regset,
548                    unsigned int pos, unsigned int count,
549                    const void *kbuf, const void __user *ubuf)
550 {
551         int ret;
552         unsigned long tls;
553
554         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
555         if (ret)
556                 return ret;
557
558         target->thread.tp_value = tls;
559         return ret;
560 }
561
562 static int system_call_get(struct task_struct *target,
563                            const struct user_regset *regset,
564                            unsigned int pos, unsigned int count,
565                            void *kbuf, void __user *ubuf)
566 {
567         int syscallno = task_pt_regs(target)->syscallno;
568
569         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
570                                    &syscallno, 0, -1);
571 }
572
573 static int system_call_set(struct task_struct *target,
574                            const struct user_regset *regset,
575                            unsigned int pos, unsigned int count,
576                            const void *kbuf, const void __user *ubuf)
577 {
578         int syscallno, ret;
579
580         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
581         if (ret)
582                 return ret;
583
584         task_pt_regs(target)->syscallno = syscallno;
585         return ret;
586 }
587
588 enum aarch64_regset {
589         REGSET_GPR,
590         REGSET_FPR,
591         REGSET_TLS,
592 #ifdef CONFIG_HAVE_HW_BREAKPOINT
593         REGSET_HW_BREAK,
594         REGSET_HW_WATCH,
595 #endif
596         REGSET_SYSTEM_CALL,
597 };
598
599 static const struct user_regset aarch64_regsets[] = {
600         [REGSET_GPR] = {
601                 .core_note_type = NT_PRSTATUS,
602                 .n = sizeof(struct user_pt_regs) / sizeof(u64),
603                 .size = sizeof(u64),
604                 .align = sizeof(u64),
605                 .get = gpr_get,
606                 .set = gpr_set
607         },
608         [REGSET_FPR] = {
609                 .core_note_type = NT_PRFPREG,
610                 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
611                 /*
612                  * We pretend we have 32-bit registers because the fpsr and
613                  * fpcr are 32-bits wide.
614                  */
615                 .size = sizeof(u32),
616                 .align = sizeof(u32),
617                 .get = fpr_get,
618                 .set = fpr_set
619         },
620         [REGSET_TLS] = {
621                 .core_note_type = NT_ARM_TLS,
622                 .n = 1,
623                 .size = sizeof(void *),
624                 .align = sizeof(void *),
625                 .get = tls_get,
626                 .set = tls_set,
627         },
628 #ifdef CONFIG_HAVE_HW_BREAKPOINT
629         [REGSET_HW_BREAK] = {
630                 .core_note_type = NT_ARM_HW_BREAK,
631                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
632                 .size = sizeof(u32),
633                 .align = sizeof(u32),
634                 .get = hw_break_get,
635                 .set = hw_break_set,
636         },
637         [REGSET_HW_WATCH] = {
638                 .core_note_type = NT_ARM_HW_WATCH,
639                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
640                 .size = sizeof(u32),
641                 .align = sizeof(u32),
642                 .get = hw_break_get,
643                 .set = hw_break_set,
644         },
645 #endif
646         [REGSET_SYSTEM_CALL] = {
647                 .core_note_type = NT_ARM_SYSTEM_CALL,
648                 .n = 1,
649                 .size = sizeof(int),
650                 .align = sizeof(int),
651                 .get = system_call_get,
652                 .set = system_call_set,
653         },
654 };
655
656 static const struct user_regset_view user_aarch64_view = {
657         .name = "aarch64", .e_machine = EM_AARCH64,
658         .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
659 };
660
661 #ifdef CONFIG_COMPAT
662 #include <linux/compat.h>
663
664 enum compat_regset {
665         REGSET_COMPAT_GPR,
666         REGSET_COMPAT_VFP,
667 };
668
669 static int compat_gpr_get(struct task_struct *target,
670                           const struct user_regset *regset,
671                           unsigned int pos, unsigned int count,
672                           void *kbuf, void __user *ubuf)
673 {
674         int ret = 0;
675         unsigned int i, start, num_regs;
676
677         /* Calculate the number of AArch32 registers contained in count */
678         num_regs = count / regset->size;
679
680         /* Convert pos into an register number */
681         start = pos / regset->size;
682
683         if (start + num_regs > regset->n)
684                 return -EIO;
685
686         for (i = 0; i < num_regs; ++i) {
687                 unsigned int idx = start + i;
688                 compat_ulong_t reg;
689
690                 switch (idx) {
691                 case 15:
692                         reg = task_pt_regs(target)->pc;
693                         break;
694                 case 16:
695                         reg = task_pt_regs(target)->pstate;
696                         break;
697                 case 17:
698                         reg = task_pt_regs(target)->orig_x0;
699                         break;
700                 default:
701                         reg = task_pt_regs(target)->regs[idx];
702                 }
703
704                 if (kbuf) {
705                         memcpy(kbuf, &reg, sizeof(reg));
706                         kbuf += sizeof(reg);
707                 } else {
708                         ret = copy_to_user(ubuf, &reg, sizeof(reg));
709                         if (ret) {
710                                 ret = -EFAULT;
711                                 break;
712                         }
713
714                         ubuf += sizeof(reg);
715                 }
716         }
717
718         return ret;
719 }
720
721 static int compat_gpr_set(struct task_struct *target,
722                           const struct user_regset *regset,
723                           unsigned int pos, unsigned int count,
724                           const void *kbuf, const void __user *ubuf)
725 {
726         struct pt_regs newregs;
727         int ret = 0;
728         unsigned int i, start, num_regs;
729
730         /* Calculate the number of AArch32 registers contained in count */
731         num_regs = count / regset->size;
732
733         /* Convert pos into an register number */
734         start = pos / regset->size;
735
736         if (start + num_regs > regset->n)
737                 return -EIO;
738
739         newregs = *task_pt_regs(target);
740
741         for (i = 0; i < num_regs; ++i) {
742                 unsigned int idx = start + i;
743                 compat_ulong_t reg;
744
745                 if (kbuf) {
746                         memcpy(&reg, kbuf, sizeof(reg));
747                         kbuf += sizeof(reg);
748                 } else {
749                         ret = copy_from_user(&reg, ubuf, sizeof(reg));
750                         if (ret) {
751                                 ret = -EFAULT;
752                                 break;
753                         }
754
755                         ubuf += sizeof(reg);
756                 }
757
758                 switch (idx) {
759                 case 15:
760                         newregs.pc = reg;
761                         break;
762                 case 16:
763                         newregs.pstate = reg;
764                         break;
765                 case 17:
766                         newregs.orig_x0 = reg;
767                         break;
768                 default:
769                         newregs.regs[idx] = reg;
770                 }
771
772         }
773
774         if (valid_user_regs(&newregs.user_regs, target))
775                 *task_pt_regs(target) = newregs;
776         else
777                 ret = -EINVAL;
778
779         return ret;
780 }
781
782 static int compat_vfp_get(struct task_struct *target,
783                           const struct user_regset *regset,
784                           unsigned int pos, unsigned int count,
785                           void *kbuf, void __user *ubuf)
786 {
787         struct user_fpsimd_state *uregs;
788         compat_ulong_t fpscr;
789         int ret;
790
791         uregs = &target->thread.fpsimd_state.user_fpsimd;
792
793         /*
794          * The VFP registers are packed into the fpsimd_state, so they all sit
795          * nicely together for us. We just need to create the fpscr separately.
796          */
797         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
798                                   VFP_STATE_SIZE - sizeof(compat_ulong_t));
799
800         if (count && !ret) {
801                 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
802                         (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
803                 ret = put_user(fpscr, (compat_ulong_t *)ubuf);
804         }
805
806         return ret;
807 }
808
809 static int compat_vfp_set(struct task_struct *target,
810                           const struct user_regset *regset,
811                           unsigned int pos, unsigned int count,
812                           const void *kbuf, const void __user *ubuf)
813 {
814         struct user_fpsimd_state *uregs;
815         compat_ulong_t fpscr;
816         int ret;
817
818         if (pos + count > VFP_STATE_SIZE)
819                 return -EIO;
820
821         uregs = &target->thread.fpsimd_state.user_fpsimd;
822
823         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
824                                  VFP_STATE_SIZE - sizeof(compat_ulong_t));
825
826         if (count && !ret) {
827                 ret = get_user(fpscr, (compat_ulong_t *)ubuf);
828                 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
829                 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
830         }
831
832         fpsimd_flush_task_state(target);
833         return ret;
834 }
835
836 static int compat_tls_get(struct task_struct *target,
837                           const struct user_regset *regset, unsigned int pos,
838                           unsigned int count, void *kbuf, void __user *ubuf)
839 {
840         compat_ulong_t tls = (compat_ulong_t)target->thread.tp_value;
841         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
842 }
843
844 static int compat_tls_set(struct task_struct *target,
845                           const struct user_regset *regset, unsigned int pos,
846                           unsigned int count, const void *kbuf,
847                           const void __user *ubuf)
848 {
849         int ret;
850         compat_ulong_t tls;
851
852         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
853         if (ret)
854                 return ret;
855
856         target->thread.tp_value = tls;
857         return ret;
858 }
859
860 static const struct user_regset aarch32_regsets[] = {
861         [REGSET_COMPAT_GPR] = {
862                 .core_note_type = NT_PRSTATUS,
863                 .n = COMPAT_ELF_NGREG,
864                 .size = sizeof(compat_elf_greg_t),
865                 .align = sizeof(compat_elf_greg_t),
866                 .get = compat_gpr_get,
867                 .set = compat_gpr_set
868         },
869         [REGSET_COMPAT_VFP] = {
870                 .core_note_type = NT_ARM_VFP,
871                 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
872                 .size = sizeof(compat_ulong_t),
873                 .align = sizeof(compat_ulong_t),
874                 .get = compat_vfp_get,
875                 .set = compat_vfp_set
876         },
877 };
878
879 static const struct user_regset_view user_aarch32_view = {
880         .name = "aarch32", .e_machine = EM_ARM,
881         .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
882 };
883
884 static const struct user_regset aarch32_ptrace_regsets[] = {
885         [REGSET_GPR] = {
886                 .core_note_type = NT_PRSTATUS,
887                 .n = COMPAT_ELF_NGREG,
888                 .size = sizeof(compat_elf_greg_t),
889                 .align = sizeof(compat_elf_greg_t),
890                 .get = compat_gpr_get,
891                 .set = compat_gpr_set
892         },
893         [REGSET_FPR] = {
894                 .core_note_type = NT_ARM_VFP,
895                 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
896                 .size = sizeof(compat_ulong_t),
897                 .align = sizeof(compat_ulong_t),
898                 .get = compat_vfp_get,
899                 .set = compat_vfp_set
900         },
901         [REGSET_TLS] = {
902                 .core_note_type = NT_ARM_TLS,
903                 .n = 1,
904                 .size = sizeof(compat_ulong_t),
905                 .align = sizeof(compat_ulong_t),
906                 .get = compat_tls_get,
907                 .set = compat_tls_set,
908         },
909 #ifdef CONFIG_HAVE_HW_BREAKPOINT
910         [REGSET_HW_BREAK] = {
911                 .core_note_type = NT_ARM_HW_BREAK,
912                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
913                 .size = sizeof(u32),
914                 .align = sizeof(u32),
915                 .get = hw_break_get,
916                 .set = hw_break_set,
917         },
918         [REGSET_HW_WATCH] = {
919                 .core_note_type = NT_ARM_HW_WATCH,
920                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
921                 .size = sizeof(u32),
922                 .align = sizeof(u32),
923                 .get = hw_break_get,
924                 .set = hw_break_set,
925         },
926 #endif
927         [REGSET_SYSTEM_CALL] = {
928                 .core_note_type = NT_ARM_SYSTEM_CALL,
929                 .n = 1,
930                 .size = sizeof(int),
931                 .align = sizeof(int),
932                 .get = system_call_get,
933                 .set = system_call_set,
934         },
935 };
936
937 static const struct user_regset_view user_aarch32_ptrace_view = {
938         .name = "aarch32", .e_machine = EM_ARM,
939         .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
940 };
941
942 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
943                                    compat_ulong_t __user *ret)
944 {
945         compat_ulong_t tmp;
946
947         if (off & 3)
948                 return -EIO;
949
950         if (off == COMPAT_PT_TEXT_ADDR)
951                 tmp = tsk->mm->start_code;
952         else if (off == COMPAT_PT_DATA_ADDR)
953                 tmp = tsk->mm->start_data;
954         else if (off == COMPAT_PT_TEXT_END_ADDR)
955                 tmp = tsk->mm->end_code;
956         else if (off < sizeof(compat_elf_gregset_t))
957                 return copy_regset_to_user(tsk, &user_aarch32_view,
958                                            REGSET_COMPAT_GPR, off,
959                                            sizeof(compat_ulong_t), ret);
960         else if (off >= COMPAT_USER_SZ)
961                 return -EIO;
962         else
963                 tmp = 0;
964
965         return put_user(tmp, ret);
966 }
967
968 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
969                                     compat_ulong_t val)
970 {
971         int ret;
972         mm_segment_t old_fs = get_fs();
973
974         if (off & 3 || off >= COMPAT_USER_SZ)
975                 return -EIO;
976
977         if (off >= sizeof(compat_elf_gregset_t))
978                 return 0;
979
980         set_fs(KERNEL_DS);
981         ret = copy_regset_from_user(tsk, &user_aarch32_view,
982                                     REGSET_COMPAT_GPR, off,
983                                     sizeof(compat_ulong_t),
984                                     &val);
985         set_fs(old_fs);
986
987         return ret;
988 }
989
990 #ifdef CONFIG_HAVE_HW_BREAKPOINT
991
992 /*
993  * Convert a virtual register number into an index for a thread_info
994  * breakpoint array. Breakpoints are identified using positive numbers
995  * whilst watchpoints are negative. The registers are laid out as pairs
996  * of (address, control), each pair mapping to a unique hw_breakpoint struct.
997  * Register 0 is reserved for describing resource information.
998  */
999 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1000 {
1001         return (abs(num) - 1) >> 1;
1002 }
1003
1004 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1005 {
1006         u8 num_brps, num_wrps, debug_arch, wp_len;
1007         u32 reg = 0;
1008
1009         num_brps        = hw_breakpoint_slots(TYPE_INST);
1010         num_wrps        = hw_breakpoint_slots(TYPE_DATA);
1011
1012         debug_arch      = debug_monitors_arch();
1013         wp_len          = 8;
1014         reg             |= debug_arch;
1015         reg             <<= 8;
1016         reg             |= wp_len;
1017         reg             <<= 8;
1018         reg             |= num_wrps;
1019         reg             <<= 8;
1020         reg             |= num_brps;
1021
1022         *kdata = reg;
1023         return 0;
1024 }
1025
1026 static int compat_ptrace_hbp_get(unsigned int note_type,
1027                                  struct task_struct *tsk,
1028                                  compat_long_t num,
1029                                  u32 *kdata)
1030 {
1031         u64 addr = 0;
1032         u32 ctrl = 0;
1033
1034         int err, idx = compat_ptrace_hbp_num_to_idx(num);;
1035
1036         if (num & 1) {
1037                 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1038                 *kdata = (u32)addr;
1039         } else {
1040                 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1041                 *kdata = ctrl;
1042         }
1043
1044         return err;
1045 }
1046
1047 static int compat_ptrace_hbp_set(unsigned int note_type,
1048                                  struct task_struct *tsk,
1049                                  compat_long_t num,
1050                                  u32 *kdata)
1051 {
1052         u64 addr;
1053         u32 ctrl;
1054
1055         int err, idx = compat_ptrace_hbp_num_to_idx(num);
1056
1057         if (num & 1) {
1058                 addr = *kdata;
1059                 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1060         } else {
1061                 ctrl = *kdata;
1062                 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1063         }
1064
1065         return err;
1066 }
1067
1068 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1069                                     compat_ulong_t __user *data)
1070 {
1071         int ret;
1072         u32 kdata;
1073         mm_segment_t old_fs = get_fs();
1074
1075         set_fs(KERNEL_DS);
1076         /* Watchpoint */
1077         if (num < 0) {
1078                 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1079         /* Resource info */
1080         } else if (num == 0) {
1081                 ret = compat_ptrace_hbp_get_resource_info(&kdata);
1082         /* Breakpoint */
1083         } else {
1084                 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1085         }
1086         set_fs(old_fs);
1087
1088         if (!ret)
1089                 ret = put_user(kdata, data);
1090
1091         return ret;
1092 }
1093
1094 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1095                                     compat_ulong_t __user *data)
1096 {
1097         int ret;
1098         u32 kdata = 0;
1099         mm_segment_t old_fs = get_fs();
1100
1101         if (num == 0)
1102                 return 0;
1103
1104         ret = get_user(kdata, data);
1105         if (ret)
1106                 return ret;
1107
1108         set_fs(KERNEL_DS);
1109         if (num < 0)
1110                 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1111         else
1112                 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1113         set_fs(old_fs);
1114
1115         return ret;
1116 }
1117 #endif  /* CONFIG_HAVE_HW_BREAKPOINT */
1118
1119 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1120                         compat_ulong_t caddr, compat_ulong_t cdata)
1121 {
1122         unsigned long addr = caddr;
1123         unsigned long data = cdata;
1124         void __user *datap = compat_ptr(data);
1125         int ret;
1126
1127         switch (request) {
1128                 case PTRACE_PEEKUSR:
1129                         ret = compat_ptrace_read_user(child, addr, datap);
1130                         break;
1131
1132                 case PTRACE_POKEUSR:
1133                         ret = compat_ptrace_write_user(child, addr, data);
1134                         break;
1135
1136                 case COMPAT_PTRACE_GETREGS:
1137                         ret = copy_regset_to_user(child,
1138                                                   &user_aarch32_view,
1139                                                   REGSET_COMPAT_GPR,
1140                                                   0, sizeof(compat_elf_gregset_t),
1141                                                   datap);
1142                         break;
1143
1144                 case COMPAT_PTRACE_SETREGS:
1145                         ret = copy_regset_from_user(child,
1146                                                     &user_aarch32_view,
1147                                                     REGSET_COMPAT_GPR,
1148                                                     0, sizeof(compat_elf_gregset_t),
1149                                                     datap);
1150                         break;
1151
1152                 case COMPAT_PTRACE_GET_THREAD_AREA:
1153                         ret = put_user((compat_ulong_t)child->thread.tp_value,
1154                                        (compat_ulong_t __user *)datap);
1155                         break;
1156
1157                 case COMPAT_PTRACE_SET_SYSCALL:
1158                         task_pt_regs(child)->syscallno = data;
1159                         ret = 0;
1160                         break;
1161
1162                 case COMPAT_PTRACE_GETVFPREGS:
1163                         ret = copy_regset_to_user(child,
1164                                                   &user_aarch32_view,
1165                                                   REGSET_COMPAT_VFP,
1166                                                   0, VFP_STATE_SIZE,
1167                                                   datap);
1168                         break;
1169
1170                 case COMPAT_PTRACE_SETVFPREGS:
1171                         ret = copy_regset_from_user(child,
1172                                                     &user_aarch32_view,
1173                                                     REGSET_COMPAT_VFP,
1174                                                     0, VFP_STATE_SIZE,
1175                                                     datap);
1176                         break;
1177
1178 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1179                 case COMPAT_PTRACE_GETHBPREGS:
1180                         ret = compat_ptrace_gethbpregs(child, addr, datap);
1181                         break;
1182
1183                 case COMPAT_PTRACE_SETHBPREGS:
1184                         ret = compat_ptrace_sethbpregs(child, addr, datap);
1185                         break;
1186 #endif
1187
1188                 default:
1189                         ret = compat_ptrace_request(child, request, addr,
1190                                                     data);
1191                         break;
1192         }
1193
1194         return ret;
1195 }
1196 #endif /* CONFIG_COMPAT */
1197
1198 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1199 {
1200 #ifdef CONFIG_COMPAT
1201         /*
1202          * Core dumping of 32-bit tasks or compat ptrace requests must use the
1203          * user_aarch32_view compatible with arm32. Native ptrace requests on
1204          * 32-bit children use an extended user_aarch32_ptrace_view to allow
1205          * access to the TLS register.
1206          */
1207         if (is_compat_task())
1208                 return &user_aarch32_view;
1209         else if (is_compat_thread(task_thread_info(task)))
1210                 return &user_aarch32_ptrace_view;
1211 #endif
1212         return &user_aarch64_view;
1213 }
1214
1215 long arch_ptrace(struct task_struct *child, long request,
1216                  unsigned long addr, unsigned long data)
1217 {
1218         return ptrace_request(child, request, addr, data);
1219 }
1220
1221 enum ptrace_syscall_dir {
1222         PTRACE_SYSCALL_ENTER = 0,
1223         PTRACE_SYSCALL_EXIT,
1224 };
1225
1226 static void tracehook_report_syscall(struct pt_regs *regs,
1227                                      enum ptrace_syscall_dir dir)
1228 {
1229         int regno;
1230         unsigned long saved_reg;
1231
1232         /*
1233          * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1234          * used to denote syscall entry/exit:
1235          */
1236         regno = (is_compat_task() ? 12 : 7);
1237         saved_reg = regs->regs[regno];
1238         regs->regs[regno] = dir;
1239
1240         if (dir == PTRACE_SYSCALL_EXIT)
1241                 tracehook_report_syscall_exit(regs, 0);
1242         else if (tracehook_report_syscall_entry(regs))
1243                 regs->syscallno = ~0UL;
1244
1245         regs->regs[regno] = saved_reg;
1246 }
1247
1248 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1249 {
1250         /* Do the secure computing check first; failures should be fast. */
1251         if (secure_computing() == -1)
1252                 return -1;
1253
1254         if (test_thread_flag(TIF_SYSCALL_TRACE))
1255                 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1256
1257         if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1258                 trace_sys_enter(regs, regs->syscallno);
1259
1260         audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1261                             regs->regs[2], regs->regs[3]);
1262
1263         return regs->syscallno;
1264 }
1265
1266 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1267 {
1268         audit_syscall_exit(regs);
1269
1270         if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1271                 trace_sys_exit(regs, regs_return_value(regs));
1272
1273         if (test_thread_flag(TIF_SYSCALL_TRACE))
1274                 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1275 }
1276
1277 /*
1278  * Bits which are always architecturally RES0 per ARM DDI 0487A.h
1279  * Userspace cannot use these until they have an architectural meaning.
1280  * We also reserve IL for the kernel; SS is handled dynamically.
1281  */
1282 #define SPSR_EL1_AARCH64_RES0_BITS \
1283         (GENMASK_ULL(63,32) | GENMASK_ULL(27, 22) | GENMASK_ULL(20, 10) | \
1284          GENMASK_ULL(5, 5))
1285 #define SPSR_EL1_AARCH32_RES0_BITS \
1286         (GENMASK_ULL(63,32) | GENMASK_ULL(24, 22) | GENMASK_ULL(20,20))
1287
1288 static int valid_compat_regs(struct user_pt_regs *regs)
1289 {
1290         regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
1291
1292         if (!system_supports_mixed_endian_el0()) {
1293                 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1294                         regs->pstate |= COMPAT_PSR_E_BIT;
1295                 else
1296                         regs->pstate &= ~COMPAT_PSR_E_BIT;
1297         }
1298
1299         if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
1300             (regs->pstate & COMPAT_PSR_A_BIT) == 0 &&
1301             (regs->pstate & COMPAT_PSR_I_BIT) == 0 &&
1302             (regs->pstate & COMPAT_PSR_F_BIT) == 0) {
1303                 return 1;
1304         }
1305
1306         /*
1307          * Force PSR to a valid 32-bit EL0t, preserving the same bits as
1308          * arch/arm.
1309          */
1310         regs->pstate &= COMPAT_PSR_N_BIT | COMPAT_PSR_Z_BIT |
1311                         COMPAT_PSR_C_BIT | COMPAT_PSR_V_BIT |
1312                         COMPAT_PSR_Q_BIT | COMPAT_PSR_IT_MASK |
1313                         COMPAT_PSR_GE_MASK | COMPAT_PSR_E_BIT |
1314                         COMPAT_PSR_T_BIT;
1315         regs->pstate |= PSR_MODE32_BIT;
1316
1317         return 0;
1318 }
1319
1320 static int valid_native_regs(struct user_pt_regs *regs)
1321 {
1322         regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
1323
1324         if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
1325             (regs->pstate & PSR_D_BIT) == 0 &&
1326             (regs->pstate & PSR_A_BIT) == 0 &&
1327             (regs->pstate & PSR_I_BIT) == 0 &&
1328             (regs->pstate & PSR_F_BIT) == 0) {
1329                 return 1;
1330         }
1331
1332         /* Force PSR to a valid 64-bit EL0t */
1333         regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
1334
1335         return 0;
1336 }
1337
1338 /*
1339  * Are the current registers suitable for user mode? (used to maintain
1340  * security in signal handlers)
1341  */
1342 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
1343 {
1344         if (!test_tsk_thread_flag(task, TIF_SINGLESTEP))
1345                 regs->pstate &= ~DBG_SPSR_SS;
1346
1347         if (is_compat_thread(task_thread_info(task)))
1348                 return valid_compat_regs(regs);
1349         else
1350                 return valid_native_regs(regs);
1351 }