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