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