Linux 3.10.49
[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/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/smp.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
28 #include <linux/security.h>
29 #include <linux/init.h>
30 #include <linux/signal.h>
31 #include <linux/uaccess.h>
32 #include <linux/perf_event.h>
33 #include <linux/hw_breakpoint.h>
34 #include <linux/regset.h>
35 #include <linux/tracehook.h>
36 #include <linux/elf.h>
37
38 #include <asm/compat.h>
39 #include <asm/debug-monitors.h>
40 #include <asm/pgtable.h>
41 #include <asm/traps.h>
42 #include <asm/system_misc.h>
43
44 /*
45  * TODO: does not yet catch signals sent when the child dies.
46  * in exit.c or in signal.c.
47  */
48
49 /*
50  * Called by kernel/ptrace.c when detaching..
51  */
52 void ptrace_disable(struct task_struct *child)
53 {
54 }
55
56 /*
57  * Handle hitting a breakpoint.
58  */
59 static int ptrace_break(struct pt_regs *regs)
60 {
61         siginfo_t info = {
62                 .si_signo = SIGTRAP,
63                 .si_errno = 0,
64                 .si_code  = TRAP_BRKPT,
65                 .si_addr  = (void __user *)instruction_pointer(regs),
66         };
67
68         force_sig_info(SIGTRAP, &info, current);
69         return 0;
70 }
71
72 static int arm64_break_trap(unsigned long addr, unsigned int esr,
73                             struct pt_regs *regs)
74 {
75         return ptrace_break(regs);
76 }
77
78 #ifdef CONFIG_HAVE_HW_BREAKPOINT
79 /*
80  * Handle hitting a HW-breakpoint.
81  */
82 static void ptrace_hbptriggered(struct perf_event *bp,
83                                 struct perf_sample_data *data,
84                                 struct pt_regs *regs)
85 {
86         struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
87         siginfo_t info = {
88                 .si_signo       = SIGTRAP,
89                 .si_errno       = 0,
90                 .si_code        = TRAP_HWBKPT,
91                 .si_addr        = (void __user *)(bkpt->trigger),
92         };
93
94 #ifdef CONFIG_COMPAT
95         int i;
96
97         if (!is_compat_task())
98                 goto send_sig;
99
100         for (i = 0; i < ARM_MAX_BRP; ++i) {
101                 if (current->thread.debug.hbp_break[i] == bp) {
102                         info.si_errno = (i << 1) + 1;
103                         break;
104                 }
105         }
106         for (i = ARM_MAX_BRP; i < ARM_MAX_HBP_SLOTS && !bp; ++i) {
107                 if (current->thread.debug.hbp_watch[i] == bp) {
108                         info.si_errno = -((i << 1) + 1);
109                         break;
110                 }
111         }
112
113 send_sig:
114 #endif
115         force_sig_info(SIGTRAP, &info, current);
116 }
117
118 /*
119  * Unregister breakpoints from this task and reset the pointers in
120  * the thread_struct.
121  */
122 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
123 {
124         int i;
125         struct thread_struct *t = &tsk->thread;
126
127         for (i = 0; i < ARM_MAX_BRP; i++) {
128                 if (t->debug.hbp_break[i]) {
129                         unregister_hw_breakpoint(t->debug.hbp_break[i]);
130                         t->debug.hbp_break[i] = NULL;
131                 }
132         }
133
134         for (i = 0; i < ARM_MAX_WRP; i++) {
135                 if (t->debug.hbp_watch[i]) {
136                         unregister_hw_breakpoint(t->debug.hbp_watch[i]);
137                         t->debug.hbp_watch[i] = NULL;
138                 }
139         }
140 }
141
142 void ptrace_hw_copy_thread(struct task_struct *tsk)
143 {
144         memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
145 }
146
147 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
148                                                struct task_struct *tsk,
149                                                unsigned long idx)
150 {
151         struct perf_event *bp = ERR_PTR(-EINVAL);
152
153         switch (note_type) {
154         case NT_ARM_HW_BREAK:
155                 if (idx < ARM_MAX_BRP)
156                         bp = tsk->thread.debug.hbp_break[idx];
157                 break;
158         case NT_ARM_HW_WATCH:
159                 if (idx < ARM_MAX_WRP)
160                         bp = tsk->thread.debug.hbp_watch[idx];
161                 break;
162         }
163
164         return bp;
165 }
166
167 static int ptrace_hbp_set_event(unsigned int note_type,
168                                 struct task_struct *tsk,
169                                 unsigned long idx,
170                                 struct perf_event *bp)
171 {
172         int err = -EINVAL;
173
174         switch (note_type) {
175         case NT_ARM_HW_BREAK:
176                 if (idx < ARM_MAX_BRP) {
177                         tsk->thread.debug.hbp_break[idx] = bp;
178                         err = 0;
179                 }
180                 break;
181         case NT_ARM_HW_WATCH:
182                 if (idx < ARM_MAX_WRP) {
183                         tsk->thread.debug.hbp_watch[idx] = bp;
184                         err = 0;
185                 }
186                 break;
187         }
188
189         return err;
190 }
191
192 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
193                                             struct task_struct *tsk,
194                                             unsigned long idx)
195 {
196         struct perf_event *bp;
197         struct perf_event_attr attr;
198         int err, type;
199
200         switch (note_type) {
201         case NT_ARM_HW_BREAK:
202                 type = HW_BREAKPOINT_X;
203                 break;
204         case NT_ARM_HW_WATCH:
205                 type = HW_BREAKPOINT_RW;
206                 break;
207         default:
208                 return ERR_PTR(-EINVAL);
209         }
210
211         ptrace_breakpoint_init(&attr);
212
213         /*
214          * Initialise fields to sane defaults
215          * (i.e. values that will pass validation).
216          */
217         attr.bp_addr    = 0;
218         attr.bp_len     = HW_BREAKPOINT_LEN_4;
219         attr.bp_type    = type;
220         attr.disabled   = 1;
221
222         bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
223         if (IS_ERR(bp))
224                 return bp;
225
226         err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
227         if (err)
228                 return ERR_PTR(err);
229
230         return bp;
231 }
232
233 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
234                                      struct arch_hw_breakpoint_ctrl ctrl,
235                                      struct perf_event_attr *attr)
236 {
237         int err, len, type, disabled = !ctrl.enabled;
238
239         attr->disabled = disabled;
240         if (disabled)
241                 return 0;
242
243         err = arch_bp_generic_fields(ctrl, &len, &type);
244         if (err)
245                 return err;
246
247         switch (note_type) {
248         case NT_ARM_HW_BREAK:
249                 if ((type & HW_BREAKPOINT_X) != type)
250                         return -EINVAL;
251                 break;
252         case NT_ARM_HW_WATCH:
253                 if ((type & HW_BREAKPOINT_RW) != type)
254                         return -EINVAL;
255                 break;
256         default:
257                 return -EINVAL;
258         }
259
260         attr->bp_len    = len;
261         attr->bp_type   = type;
262
263         return 0;
264 }
265
266 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
267 {
268         u8 num;
269         u32 reg = 0;
270
271         switch (note_type) {
272         case NT_ARM_HW_BREAK:
273                 num = hw_breakpoint_slots(TYPE_INST);
274                 break;
275         case NT_ARM_HW_WATCH:
276                 num = hw_breakpoint_slots(TYPE_DATA);
277                 break;
278         default:
279                 return -EINVAL;
280         }
281
282         reg |= debug_monitors_arch();
283         reg <<= 8;
284         reg |= num;
285
286         *info = reg;
287         return 0;
288 }
289
290 static int ptrace_hbp_get_ctrl(unsigned int note_type,
291                                struct task_struct *tsk,
292                                unsigned long idx,
293                                u32 *ctrl)
294 {
295         struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
296
297         if (IS_ERR(bp))
298                 return PTR_ERR(bp);
299
300         *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
301         return 0;
302 }
303
304 static int ptrace_hbp_get_addr(unsigned int note_type,
305                                struct task_struct *tsk,
306                                unsigned long idx,
307                                u64 *addr)
308 {
309         struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
310
311         if (IS_ERR(bp))
312                 return PTR_ERR(bp);
313
314         *addr = bp ? bp->attr.bp_addr : 0;
315         return 0;
316 }
317
318 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
319                                                         struct task_struct *tsk,
320                                                         unsigned long idx)
321 {
322         struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
323
324         if (!bp)
325                 bp = ptrace_hbp_create(note_type, tsk, idx);
326
327         return bp;
328 }
329
330 static int ptrace_hbp_set_ctrl(unsigned int note_type,
331                                struct task_struct *tsk,
332                                unsigned long idx,
333                                u32 uctrl)
334 {
335         int err;
336         struct perf_event *bp;
337         struct perf_event_attr attr;
338         struct arch_hw_breakpoint_ctrl ctrl;
339
340         bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
341         if (IS_ERR(bp)) {
342                 err = PTR_ERR(bp);
343                 return err;
344         }
345
346         attr = bp->attr;
347         decode_ctrl_reg(uctrl, &ctrl);
348         err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
349         if (err)
350                 return err;
351
352         return modify_user_hw_breakpoint(bp, &attr);
353 }
354
355 static int ptrace_hbp_set_addr(unsigned int note_type,
356                                struct task_struct *tsk,
357                                unsigned long idx,
358                                u64 addr)
359 {
360         int err;
361         struct perf_event *bp;
362         struct perf_event_attr attr;
363
364         bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
365         if (IS_ERR(bp)) {
366                 err = PTR_ERR(bp);
367                 return err;
368         }
369
370         attr = bp->attr;
371         attr.bp_addr = addr;
372         err = modify_user_hw_breakpoint(bp, &attr);
373         return err;
374 }
375
376 #define PTRACE_HBP_ADDR_SZ      sizeof(u64)
377 #define PTRACE_HBP_CTRL_SZ      sizeof(u32)
378 #define PTRACE_HBP_PAD_SZ       sizeof(u32)
379
380 static int hw_break_get(struct task_struct *target,
381                         const struct user_regset *regset,
382                         unsigned int pos, unsigned int count,
383                         void *kbuf, void __user *ubuf)
384 {
385         unsigned int note_type = regset->core_note_type;
386         int ret, idx = 0, offset, limit;
387         u32 info, ctrl;
388         u64 addr;
389
390         /* Resource info */
391         ret = ptrace_hbp_get_resource_info(note_type, &info);
392         if (ret)
393                 return ret;
394
395         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
396                                   sizeof(info));
397         if (ret)
398                 return ret;
399
400         /* Pad */
401         offset = offsetof(struct user_hwdebug_state, pad);
402         ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
403                                        offset + PTRACE_HBP_PAD_SZ);
404         if (ret)
405                 return ret;
406
407         /* (address, ctrl) registers */
408         offset = offsetof(struct user_hwdebug_state, dbg_regs);
409         limit = regset->n * regset->size;
410         while (count && offset < limit) {
411                 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
412                 if (ret)
413                         return ret;
414                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
415                                           offset, offset + PTRACE_HBP_ADDR_SZ);
416                 if (ret)
417                         return ret;
418                 offset += PTRACE_HBP_ADDR_SZ;
419
420                 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
421                 if (ret)
422                         return ret;
423                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
424                                           offset, offset + PTRACE_HBP_CTRL_SZ);
425                 if (ret)
426                         return ret;
427                 offset += PTRACE_HBP_CTRL_SZ;
428
429                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
430                                                offset,
431                                                offset + PTRACE_HBP_PAD_SZ);
432                 if (ret)
433                         return ret;
434                 offset += PTRACE_HBP_PAD_SZ;
435                 idx++;
436         }
437
438         return 0;
439 }
440
441 static int hw_break_set(struct task_struct *target,
442                         const struct user_regset *regset,
443                         unsigned int pos, unsigned int count,
444                         const void *kbuf, const void __user *ubuf)
445 {
446         unsigned int note_type = regset->core_note_type;
447         int ret, idx = 0, offset, limit;
448         u32 ctrl;
449         u64 addr;
450
451         /* Resource info and pad */
452         offset = offsetof(struct user_hwdebug_state, dbg_regs);
453         ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
454         if (ret)
455                 return ret;
456
457         /* (address, ctrl) registers */
458         limit = regset->n * regset->size;
459         while (count && offset < limit) {
460                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
461                                          offset, offset + PTRACE_HBP_ADDR_SZ);
462                 if (ret)
463                         return ret;
464                 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
465                 if (ret)
466                         return ret;
467                 offset += PTRACE_HBP_ADDR_SZ;
468
469                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
470                                          offset, offset + PTRACE_HBP_CTRL_SZ);
471                 if (ret)
472                         return ret;
473                 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
474                 if (ret)
475                         return ret;
476                 offset += PTRACE_HBP_CTRL_SZ;
477
478                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
479                                                 offset,
480                                                 offset + PTRACE_HBP_PAD_SZ);
481                 if (ret)
482                         return ret;
483                 offset += PTRACE_HBP_PAD_SZ;
484                 idx++;
485         }
486
487         return 0;
488 }
489 #endif  /* CONFIG_HAVE_HW_BREAKPOINT */
490
491 static int gpr_get(struct task_struct *target,
492                    const struct user_regset *regset,
493                    unsigned int pos, unsigned int count,
494                    void *kbuf, void __user *ubuf)
495 {
496         struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
497         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
498 }
499
500 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
501                    unsigned int pos, unsigned int count,
502                    const void *kbuf, const void __user *ubuf)
503 {
504         int ret;
505         struct user_pt_regs newregs;
506
507         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
508         if (ret)
509                 return ret;
510
511         if (!valid_user_regs(&newregs))
512                 return -EINVAL;
513
514         task_pt_regs(target)->user_regs = newregs;
515         return 0;
516 }
517
518 /*
519  * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
520  */
521 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
522                    unsigned int pos, unsigned int count,
523                    void *kbuf, void __user *ubuf)
524 {
525         struct user_fpsimd_state *uregs;
526         uregs = &target->thread.fpsimd_state.user_fpsimd;
527         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
528 }
529
530 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
531                    unsigned int pos, unsigned int count,
532                    const void *kbuf, const void __user *ubuf)
533 {
534         int ret;
535         struct user_fpsimd_state newstate;
536
537         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
538         if (ret)
539                 return ret;
540
541         target->thread.fpsimd_state.user_fpsimd = newstate;
542         return ret;
543 }
544
545 static int tls_get(struct task_struct *target, const struct user_regset *regset,
546                    unsigned int pos, unsigned int count,
547                    void *kbuf, void __user *ubuf)
548 {
549         unsigned long *tls = &target->thread.tp_value;
550         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
551 }
552
553 static int tls_set(struct task_struct *target, const struct user_regset *regset,
554                    unsigned int pos, unsigned int count,
555                    const void *kbuf, const void __user *ubuf)
556 {
557         int ret;
558         unsigned long tls;
559
560         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
561         if (ret)
562                 return ret;
563
564         target->thread.tp_value = tls;
565         return ret;
566 }
567
568 enum aarch64_regset {
569         REGSET_GPR,
570         REGSET_FPR,
571         REGSET_TLS,
572 #ifdef CONFIG_HAVE_HW_BREAKPOINT
573         REGSET_HW_BREAK,
574         REGSET_HW_WATCH,
575 #endif
576 };
577
578 static const struct user_regset aarch64_regsets[] = {
579         [REGSET_GPR] = {
580                 .core_note_type = NT_PRSTATUS,
581                 .n = sizeof(struct user_pt_regs) / sizeof(u64),
582                 .size = sizeof(u64),
583                 .align = sizeof(u64),
584                 .get = gpr_get,
585                 .set = gpr_set
586         },
587         [REGSET_FPR] = {
588                 .core_note_type = NT_PRFPREG,
589                 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
590                 /*
591                  * We pretend we have 32-bit registers because the fpsr and
592                  * fpcr are 32-bits wide.
593                  */
594                 .size = sizeof(u32),
595                 .align = sizeof(u32),
596                 .get = fpr_get,
597                 .set = fpr_set
598         },
599         [REGSET_TLS] = {
600                 .core_note_type = NT_ARM_TLS,
601                 .n = 1,
602                 .size = sizeof(void *),
603                 .align = sizeof(void *),
604                 .get = tls_get,
605                 .set = tls_set,
606         },
607 #ifdef CONFIG_HAVE_HW_BREAKPOINT
608         [REGSET_HW_BREAK] = {
609                 .core_note_type = NT_ARM_HW_BREAK,
610                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
611                 .size = sizeof(u32),
612                 .align = sizeof(u32),
613                 .get = hw_break_get,
614                 .set = hw_break_set,
615         },
616         [REGSET_HW_WATCH] = {
617                 .core_note_type = NT_ARM_HW_WATCH,
618                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
619                 .size = sizeof(u32),
620                 .align = sizeof(u32),
621                 .get = hw_break_get,
622                 .set = hw_break_set,
623         },
624 #endif
625 };
626
627 static const struct user_regset_view user_aarch64_view = {
628         .name = "aarch64", .e_machine = EM_AARCH64,
629         .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
630 };
631
632 #ifdef CONFIG_COMPAT
633 #include <linux/compat.h>
634
635 enum compat_regset {
636         REGSET_COMPAT_GPR,
637         REGSET_COMPAT_VFP,
638 };
639
640 static int compat_gpr_get(struct task_struct *target,
641                           const struct user_regset *regset,
642                           unsigned int pos, unsigned int count,
643                           void *kbuf, void __user *ubuf)
644 {
645         int ret = 0;
646         unsigned int i, start, num_regs;
647
648         /* Calculate the number of AArch32 registers contained in count */
649         num_regs = count / regset->size;
650
651         /* Convert pos into an register number */
652         start = pos / regset->size;
653
654         if (start + num_regs > regset->n)
655                 return -EIO;
656
657         for (i = 0; i < num_regs; ++i) {
658                 unsigned int idx = start + i;
659                 void *reg;
660
661                 switch (idx) {
662                 case 15:
663                         reg = (void *)&task_pt_regs(target)->pc;
664                         break;
665                 case 16:
666                         reg = (void *)&task_pt_regs(target)->pstate;
667                         break;
668                 case 17:
669                         reg = (void *)&task_pt_regs(target)->orig_x0;
670                         break;
671                 default:
672                         reg = (void *)&task_pt_regs(target)->regs[idx];
673                 }
674
675                 ret = copy_to_user(ubuf, reg, sizeof(compat_ulong_t));
676
677                 if (ret)
678                         break;
679                 else
680                         ubuf += sizeof(compat_ulong_t);
681         }
682
683         return ret;
684 }
685
686 static int compat_gpr_set(struct task_struct *target,
687                           const struct user_regset *regset,
688                           unsigned int pos, unsigned int count,
689                           const void *kbuf, const void __user *ubuf)
690 {
691         struct pt_regs newregs;
692         int ret = 0;
693         unsigned int i, start, num_regs;
694
695         /* Calculate the number of AArch32 registers contained in count */
696         num_regs = count / regset->size;
697
698         /* Convert pos into an register number */
699         start = pos / regset->size;
700
701         if (start + num_regs > regset->n)
702                 return -EIO;
703
704         newregs = *task_pt_regs(target);
705
706         for (i = 0; i < num_regs; ++i) {
707                 unsigned int idx = start + i;
708                 void *reg;
709
710                 switch (idx) {
711                 case 15:
712                         reg = (void *)&newregs.pc;
713                         break;
714                 case 16:
715                         reg = (void *)&newregs.pstate;
716                         break;
717                 case 17:
718                         reg = (void *)&newregs.orig_x0;
719                         break;
720                 default:
721                         reg = (void *)&newregs.regs[idx];
722                 }
723
724                 ret = copy_from_user(reg, ubuf, sizeof(compat_ulong_t));
725
726                 if (ret)
727                         goto out;
728                 else
729                         ubuf += sizeof(compat_ulong_t);
730         }
731
732         if (valid_user_regs(&newregs.user_regs))
733                 *task_pt_regs(target) = newregs;
734         else
735                 ret = -EINVAL;
736
737 out:
738         return ret;
739 }
740
741 static int compat_vfp_get(struct task_struct *target,
742                           const struct user_regset *regset,
743                           unsigned int pos, unsigned int count,
744                           void *kbuf, void __user *ubuf)
745 {
746         struct user_fpsimd_state *uregs;
747         compat_ulong_t fpscr;
748         int ret;
749
750         uregs = &target->thread.fpsimd_state.user_fpsimd;
751
752         /*
753          * The VFP registers are packed into the fpsimd_state, so they all sit
754          * nicely together for us. We just need to create the fpscr separately.
755          */
756         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
757                                   VFP_STATE_SIZE - sizeof(compat_ulong_t));
758
759         if (count && !ret) {
760                 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
761                         (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
762                 ret = put_user(fpscr, (compat_ulong_t *)ubuf);
763         }
764
765         return ret;
766 }
767
768 static int compat_vfp_set(struct task_struct *target,
769                           const struct user_regset *regset,
770                           unsigned int pos, unsigned int count,
771                           const void *kbuf, const void __user *ubuf)
772 {
773         struct user_fpsimd_state *uregs;
774         compat_ulong_t fpscr;
775         int ret;
776
777         if (pos + count > VFP_STATE_SIZE)
778                 return -EIO;
779
780         uregs = &target->thread.fpsimd_state.user_fpsimd;
781
782         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
783                                  VFP_STATE_SIZE - sizeof(compat_ulong_t));
784
785         if (count && !ret) {
786                 ret = get_user(fpscr, (compat_ulong_t *)ubuf);
787                 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
788                 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
789         }
790
791         return ret;
792 }
793
794 static const struct user_regset aarch32_regsets[] = {
795         [REGSET_COMPAT_GPR] = {
796                 .core_note_type = NT_PRSTATUS,
797                 .n = COMPAT_ELF_NGREG,
798                 .size = sizeof(compat_elf_greg_t),
799                 .align = sizeof(compat_elf_greg_t),
800                 .get = compat_gpr_get,
801                 .set = compat_gpr_set
802         },
803         [REGSET_COMPAT_VFP] = {
804                 .core_note_type = NT_ARM_VFP,
805                 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
806                 .size = sizeof(compat_ulong_t),
807                 .align = sizeof(compat_ulong_t),
808                 .get = compat_vfp_get,
809                 .set = compat_vfp_set
810         },
811 };
812
813 static const struct user_regset_view user_aarch32_view = {
814         .name = "aarch32", .e_machine = EM_ARM,
815         .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
816 };
817
818 int aarch32_break_trap(struct pt_regs *regs)
819 {
820         unsigned int instr;
821         bool bp = false;
822         void __user *pc = (void __user *)instruction_pointer(regs);
823
824         if (compat_thumb_mode(regs)) {
825                 /* get 16-bit Thumb instruction */
826                 get_user(instr, (u16 __user *)pc);
827                 if (instr == AARCH32_BREAK_THUMB2_LO) {
828                         /* get second half of 32-bit Thumb-2 instruction */
829                         get_user(instr, (u16 __user *)(pc + 2));
830                         bp = instr == AARCH32_BREAK_THUMB2_HI;
831                 } else {
832                         bp = instr == AARCH32_BREAK_THUMB;
833                 }
834         } else {
835                 /* 32-bit ARM instruction */
836                 get_user(instr, (u32 __user *)pc);
837                 bp = (instr & ~0xf0000000) == AARCH32_BREAK_ARM;
838         }
839
840         if (bp)
841                 return ptrace_break(regs);
842         return 1;
843 }
844
845 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
846                                    compat_ulong_t __user *ret)
847 {
848         compat_ulong_t tmp;
849
850         if (off & 3)
851                 return -EIO;
852
853         if (off == COMPAT_PT_TEXT_ADDR)
854                 tmp = tsk->mm->start_code;
855         else if (off == COMPAT_PT_DATA_ADDR)
856                 tmp = tsk->mm->start_data;
857         else if (off == COMPAT_PT_TEXT_END_ADDR)
858                 tmp = tsk->mm->end_code;
859         else if (off < sizeof(compat_elf_gregset_t))
860                 return copy_regset_to_user(tsk, &user_aarch32_view,
861                                            REGSET_COMPAT_GPR, off,
862                                            sizeof(compat_ulong_t), ret);
863         else if (off >= COMPAT_USER_SZ)
864                 return -EIO;
865         else
866                 tmp = 0;
867
868         return put_user(tmp, ret);
869 }
870
871 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
872                                     compat_ulong_t val)
873 {
874         int ret;
875         mm_segment_t old_fs = get_fs();
876
877         if (off & 3 || off >= COMPAT_USER_SZ)
878                 return -EIO;
879
880         if (off >= sizeof(compat_elf_gregset_t))
881                 return 0;
882
883         set_fs(KERNEL_DS);
884         ret = copy_regset_from_user(tsk, &user_aarch32_view,
885                                     REGSET_COMPAT_GPR, off,
886                                     sizeof(compat_ulong_t),
887                                     &val);
888         set_fs(old_fs);
889
890         return ret;
891 }
892
893 #ifdef CONFIG_HAVE_HW_BREAKPOINT
894
895 /*
896  * Convert a virtual register number into an index for a thread_info
897  * breakpoint array. Breakpoints are identified using positive numbers
898  * whilst watchpoints are negative. The registers are laid out as pairs
899  * of (address, control), each pair mapping to a unique hw_breakpoint struct.
900  * Register 0 is reserved for describing resource information.
901  */
902 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
903 {
904         return (abs(num) - 1) >> 1;
905 }
906
907 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
908 {
909         u8 num_brps, num_wrps, debug_arch, wp_len;
910         u32 reg = 0;
911
912         num_brps        = hw_breakpoint_slots(TYPE_INST);
913         num_wrps        = hw_breakpoint_slots(TYPE_DATA);
914
915         debug_arch      = debug_monitors_arch();
916         wp_len          = 8;
917         reg             |= debug_arch;
918         reg             <<= 8;
919         reg             |= wp_len;
920         reg             <<= 8;
921         reg             |= num_wrps;
922         reg             <<= 8;
923         reg             |= num_brps;
924
925         *kdata = reg;
926         return 0;
927 }
928
929 static int compat_ptrace_hbp_get(unsigned int note_type,
930                                  struct task_struct *tsk,
931                                  compat_long_t num,
932                                  u32 *kdata)
933 {
934         u64 addr = 0;
935         u32 ctrl = 0;
936
937         int err, idx = compat_ptrace_hbp_num_to_idx(num);;
938
939         if (num & 1) {
940                 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
941                 *kdata = (u32)addr;
942         } else {
943                 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
944                 *kdata = ctrl;
945         }
946
947         return err;
948 }
949
950 static int compat_ptrace_hbp_set(unsigned int note_type,
951                                  struct task_struct *tsk,
952                                  compat_long_t num,
953                                  u32 *kdata)
954 {
955         u64 addr;
956         u32 ctrl;
957
958         int err, idx = compat_ptrace_hbp_num_to_idx(num);
959
960         if (num & 1) {
961                 addr = *kdata;
962                 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
963         } else {
964                 ctrl = *kdata;
965                 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
966         }
967
968         return err;
969 }
970
971 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
972                                     compat_ulong_t __user *data)
973 {
974         int ret;
975         u32 kdata;
976         mm_segment_t old_fs = get_fs();
977
978         set_fs(KERNEL_DS);
979         /* Watchpoint */
980         if (num < 0) {
981                 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
982         /* Resource info */
983         } else if (num == 0) {
984                 ret = compat_ptrace_hbp_get_resource_info(&kdata);
985         /* Breakpoint */
986         } else {
987                 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
988         }
989         set_fs(old_fs);
990
991         if (!ret)
992                 ret = put_user(kdata, data);
993
994         return ret;
995 }
996
997 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
998                                     compat_ulong_t __user *data)
999 {
1000         int ret;
1001         u32 kdata = 0;
1002         mm_segment_t old_fs = get_fs();
1003
1004         if (num == 0)
1005                 return 0;
1006
1007         ret = get_user(kdata, data);
1008         if (ret)
1009                 return ret;
1010
1011         set_fs(KERNEL_DS);
1012         if (num < 0)
1013                 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1014         else
1015                 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1016         set_fs(old_fs);
1017
1018         return ret;
1019 }
1020 #endif  /* CONFIG_HAVE_HW_BREAKPOINT */
1021
1022 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1023                         compat_ulong_t caddr, compat_ulong_t cdata)
1024 {
1025         unsigned long addr = caddr;
1026         unsigned long data = cdata;
1027         void __user *datap = compat_ptr(data);
1028         int ret;
1029
1030         switch (request) {
1031                 case PTRACE_PEEKUSR:
1032                         ret = compat_ptrace_read_user(child, addr, datap);
1033                         break;
1034
1035                 case PTRACE_POKEUSR:
1036                         ret = compat_ptrace_write_user(child, addr, data);
1037                         break;
1038
1039                 case COMPAT_PTRACE_GETREGS:
1040                         ret = copy_regset_to_user(child,
1041                                                   &user_aarch32_view,
1042                                                   REGSET_COMPAT_GPR,
1043                                                   0, sizeof(compat_elf_gregset_t),
1044                                                   datap);
1045                         break;
1046
1047                 case COMPAT_PTRACE_SETREGS:
1048                         ret = copy_regset_from_user(child,
1049                                                     &user_aarch32_view,
1050                                                     REGSET_COMPAT_GPR,
1051                                                     0, sizeof(compat_elf_gregset_t),
1052                                                     datap);
1053                         break;
1054
1055                 case COMPAT_PTRACE_GET_THREAD_AREA:
1056                         ret = put_user((compat_ulong_t)child->thread.tp_value,
1057                                        (compat_ulong_t __user *)datap);
1058                         break;
1059
1060                 case COMPAT_PTRACE_SET_SYSCALL:
1061                         task_pt_regs(child)->syscallno = data;
1062                         ret = 0;
1063                         break;
1064
1065                 case COMPAT_PTRACE_GETVFPREGS:
1066                         ret = copy_regset_to_user(child,
1067                                                   &user_aarch32_view,
1068                                                   REGSET_COMPAT_VFP,
1069                                                   0, VFP_STATE_SIZE,
1070                                                   datap);
1071                         break;
1072
1073                 case COMPAT_PTRACE_SETVFPREGS:
1074                         ret = copy_regset_from_user(child,
1075                                                     &user_aarch32_view,
1076                                                     REGSET_COMPAT_VFP,
1077                                                     0, VFP_STATE_SIZE,
1078                                                     datap);
1079                         break;
1080
1081 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1082                 case COMPAT_PTRACE_GETHBPREGS:
1083                         ret = compat_ptrace_gethbpregs(child, addr, datap);
1084                         break;
1085
1086                 case COMPAT_PTRACE_SETHBPREGS:
1087                         ret = compat_ptrace_sethbpregs(child, addr, datap);
1088                         break;
1089 #endif
1090
1091                 default:
1092                         ret = compat_ptrace_request(child, request, addr,
1093                                                     data);
1094                         break;
1095         }
1096
1097         return ret;
1098 }
1099 #endif /* CONFIG_COMPAT */
1100
1101 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1102 {
1103 #ifdef CONFIG_COMPAT
1104         if (is_compat_thread(task_thread_info(task)))
1105                 return &user_aarch32_view;
1106 #endif
1107         return &user_aarch64_view;
1108 }
1109
1110 long arch_ptrace(struct task_struct *child, long request,
1111                  unsigned long addr, unsigned long data)
1112 {
1113         return ptrace_request(child, request, addr, data);
1114 }
1115
1116
1117 static int __init ptrace_break_init(void)
1118 {
1119         hook_debug_fault_code(DBG_ESR_EVT_BRK, arm64_break_trap, SIGTRAP,
1120                               TRAP_BRKPT, "ptrace BRK handler");
1121         return 0;
1122 }
1123 core_initcall(ptrace_break_init);
1124
1125
1126 asmlinkage int syscall_trace(int dir, struct pt_regs *regs)
1127 {
1128         unsigned long saved_reg;
1129
1130         if (!test_thread_flag(TIF_SYSCALL_TRACE))
1131                 return regs->syscallno;
1132
1133         if (is_compat_task()) {
1134                 /* AArch32 uses ip (r12) for scratch */
1135                 saved_reg = regs->regs[12];
1136                 regs->regs[12] = dir;
1137         } else {
1138                 /*
1139                  * Save X7. X7 is used to denote syscall entry/exit:
1140                  *   X7 = 0 -> entry, = 1 -> exit
1141                  */
1142                 saved_reg = regs->regs[7];
1143                 regs->regs[7] = dir;
1144         }
1145
1146         if (dir)
1147                 tracehook_report_syscall_exit(regs, 0);
1148         else if (tracehook_report_syscall_entry(regs))
1149                 regs->syscallno = ~0UL;
1150
1151         if (is_compat_task())
1152                 regs->regs[12] = saved_reg;
1153         else
1154                 regs->regs[7] = saved_reg;
1155
1156         return regs->syscallno;
1157 }