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