ee856d9f6f64e70c89e3eb1bef2d591e2b00c211
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / kernel / ptrace.c
1 /*
2  * Based on arch/arm/kernel/ptrace.c
3  *
4  * By Ross Biro 1/23/92
5  * edited by Linus Torvalds
6  * ARM modifications Copyright (C) 2000 Russell King
7  * Copyright (C) 2012 ARM Ltd.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <linux/audit.h>
23 #include <linux/compat.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/mm.h>
27 #include <linux/smp.h>
28 #include <linux/ptrace.h>
29 #include <linux/user.h>
30 #include <linux/security.h>
31 #include <linux/init.h>
32 #include <linux/signal.h>
33 #include <linux/uaccess.h>
34 #include <linux/perf_event.h>
35 #include <linux/hw_breakpoint.h>
36 #include <linux/regset.h>
37 #include <linux/tracehook.h>
38 #include <linux/elf.h>
39
40 #include <asm/compat.h>
41 #include <asm/debug-monitors.h>
42 #include <asm/pgtable.h>
43 #include <asm/syscall.h>
44 #include <asm/traps.h>
45 #include <asm/system_misc.h>
46
47 /*
48  * TODO: does not yet catch signals sent when the child dies.
49  * in exit.c or in signal.c.
50  */
51
52 /*
53  * Called by kernel/ptrace.c when detaching..
54  */
55 void ptrace_disable(struct task_struct *child)
56 {
57 }
58
59 #ifdef CONFIG_HAVE_HW_BREAKPOINT
60 /*
61  * Handle hitting a HW-breakpoint.
62  */
63 static void ptrace_hbptriggered(struct perf_event *bp,
64                                 struct perf_sample_data *data,
65                                 struct pt_regs *regs)
66 {
67         struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
68         siginfo_t info = {
69                 .si_signo       = SIGTRAP,
70                 .si_errno       = 0,
71                 .si_code        = TRAP_HWBKPT,
72                 .si_addr        = (void __user *)(bkpt->trigger),
73         };
74
75 #ifdef CONFIG_COMPAT
76         int i;
77
78         if (!is_compat_task())
79                 goto send_sig;
80
81         for (i = 0; i < ARM_MAX_BRP; ++i) {
82                 if (current->thread.debug.hbp_break[i] == bp) {
83                         info.si_errno = (i << 1) + 1;
84                         break;
85                 }
86         }
87         for (i = ARM_MAX_BRP; i < ARM_MAX_HBP_SLOTS && !bp; ++i) {
88                 if (current->thread.debug.hbp_watch[i] == bp) {
89                         info.si_errno = -((i << 1) + 1);
90                         break;
91                 }
92         }
93
94 send_sig:
95 #endif
96         force_sig_info(SIGTRAP, &info, current);
97 }
98
99 /*
100  * Unregister breakpoints from this task and reset the pointers in
101  * the thread_struct.
102  */
103 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
104 {
105         int i;
106         struct thread_struct *t = &tsk->thread;
107
108         for (i = 0; i < ARM_MAX_BRP; i++) {
109                 if (t->debug.hbp_break[i]) {
110                         unregister_hw_breakpoint(t->debug.hbp_break[i]);
111                         t->debug.hbp_break[i] = NULL;
112                 }
113         }
114
115         for (i = 0; i < ARM_MAX_WRP; i++) {
116                 if (t->debug.hbp_watch[i]) {
117                         unregister_hw_breakpoint(t->debug.hbp_watch[i]);
118                         t->debug.hbp_watch[i] = NULL;
119                 }
120         }
121 }
122
123 void ptrace_hw_copy_thread(struct task_struct *tsk)
124 {
125         memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
126 }
127
128 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
129                                                struct task_struct *tsk,
130                                                unsigned long idx)
131 {
132         struct perf_event *bp = ERR_PTR(-EINVAL);
133
134         switch (note_type) {
135         case NT_ARM_HW_BREAK:
136                 if (idx < ARM_MAX_BRP)
137                         bp = tsk->thread.debug.hbp_break[idx];
138                 break;
139         case NT_ARM_HW_WATCH:
140                 if (idx < ARM_MAX_WRP)
141                         bp = tsk->thread.debug.hbp_watch[idx];
142                 break;
143         }
144
145         return bp;
146 }
147
148 static int ptrace_hbp_set_event(unsigned int note_type,
149                                 struct task_struct *tsk,
150                                 unsigned long idx,
151                                 struct perf_event *bp)
152 {
153         int err = -EINVAL;
154
155         switch (note_type) {
156         case NT_ARM_HW_BREAK:
157                 if (idx < ARM_MAX_BRP) {
158                         tsk->thread.debug.hbp_break[idx] = bp;
159                         err = 0;
160                 }
161                 break;
162         case NT_ARM_HW_WATCH:
163                 if (idx < ARM_MAX_WRP) {
164                         tsk->thread.debug.hbp_watch[idx] = bp;
165                         err = 0;
166                 }
167                 break;
168         }
169
170         return err;
171 }
172
173 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
174                                             struct task_struct *tsk,
175                                             unsigned long idx)
176 {
177         struct perf_event *bp;
178         struct perf_event_attr attr;
179         int err, type;
180
181         switch (note_type) {
182         case NT_ARM_HW_BREAK:
183                 type = HW_BREAKPOINT_X;
184                 break;
185         case NT_ARM_HW_WATCH:
186                 type = HW_BREAKPOINT_RW;
187                 break;
188         default:
189                 return ERR_PTR(-EINVAL);
190         }
191
192         ptrace_breakpoint_init(&attr);
193
194         /*
195          * Initialise fields to sane defaults
196          * (i.e. values that will pass validation).
197          */
198         attr.bp_addr    = 0;
199         attr.bp_len     = HW_BREAKPOINT_LEN_4;
200         attr.bp_type    = type;
201         attr.disabled   = 1;
202
203         bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
204         if (IS_ERR(bp))
205                 return bp;
206
207         err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
208         if (err)
209                 return ERR_PTR(err);
210
211         return bp;
212 }
213
214 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
215                                      struct arch_hw_breakpoint_ctrl ctrl,
216                                      struct perf_event_attr *attr)
217 {
218         int err, len, type, disabled = !ctrl.enabled;
219
220         if (disabled) {
221                 len = 0;
222                 type = HW_BREAKPOINT_EMPTY;
223         } else {
224                 err = arch_bp_generic_fields(ctrl, &len, &type);
225                 if (err)
226                         return err;
227
228                 switch (note_type) {
229                 case NT_ARM_HW_BREAK:
230                         if ((type & HW_BREAKPOINT_X) != type)
231                                 return -EINVAL;
232                         break;
233                 case NT_ARM_HW_WATCH:
234                         if ((type & HW_BREAKPOINT_RW) != type)
235                                 return -EINVAL;
236                         break;
237                 default:
238                         return -EINVAL;
239                 }
240         }
241
242         attr->bp_len    = len;
243         attr->bp_type   = type;
244         attr->disabled  = disabled;
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         fpsimd_flush_task_state(target);
526         return ret;
527 }
528
529 static int tls_get(struct task_struct *target, const struct user_regset *regset,
530                    unsigned int pos, unsigned int count,
531                    void *kbuf, void __user *ubuf)
532 {
533         unsigned long *tls = &target->thread.tp_value;
534         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
535 }
536
537 static int tls_set(struct task_struct *target, const struct user_regset *regset,
538                    unsigned int pos, unsigned int count,
539                    const void *kbuf, const void __user *ubuf)
540 {
541         int ret;
542         unsigned long tls;
543
544         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
545         if (ret)
546                 return ret;
547
548         target->thread.tp_value = tls;
549         return ret;
550 }
551
552 enum aarch64_regset {
553         REGSET_GPR,
554         REGSET_FPR,
555         REGSET_TLS,
556 #ifdef CONFIG_HAVE_HW_BREAKPOINT
557         REGSET_HW_BREAK,
558         REGSET_HW_WATCH,
559 #endif
560 };
561
562 static const struct user_regset aarch64_regsets[] = {
563         [REGSET_GPR] = {
564                 .core_note_type = NT_PRSTATUS,
565                 .n = sizeof(struct user_pt_regs) / sizeof(u64),
566                 .size = sizeof(u64),
567                 .align = sizeof(u64),
568                 .get = gpr_get,
569                 .set = gpr_set
570         },
571         [REGSET_FPR] = {
572                 .core_note_type = NT_PRFPREG,
573                 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
574                 /*
575                  * We pretend we have 32-bit registers because the fpsr and
576                  * fpcr are 32-bits wide.
577                  */
578                 .size = sizeof(u32),
579                 .align = sizeof(u32),
580                 .get = fpr_get,
581                 .set = fpr_set
582         },
583         [REGSET_TLS] = {
584                 .core_note_type = NT_ARM_TLS,
585                 .n = 1,
586                 .size = sizeof(void *),
587                 .align = sizeof(void *),
588                 .get = tls_get,
589                 .set = tls_set,
590         },
591 #ifdef CONFIG_HAVE_HW_BREAKPOINT
592         [REGSET_HW_BREAK] = {
593                 .core_note_type = NT_ARM_HW_BREAK,
594                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
595                 .size = sizeof(u32),
596                 .align = sizeof(u32),
597                 .get = hw_break_get,
598                 .set = hw_break_set,
599         },
600         [REGSET_HW_WATCH] = {
601                 .core_note_type = NT_ARM_HW_WATCH,
602                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
603                 .size = sizeof(u32),
604                 .align = sizeof(u32),
605                 .get = hw_break_get,
606                 .set = hw_break_set,
607         },
608 #endif
609 };
610
611 static const struct user_regset_view user_aarch64_view = {
612         .name = "aarch64", .e_machine = EM_AARCH64,
613         .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
614 };
615
616 #ifdef CONFIG_COMPAT
617 #include <linux/compat.h>
618
619 enum compat_regset {
620         REGSET_COMPAT_GPR,
621         REGSET_COMPAT_VFP,
622 };
623
624 static int compat_gpr_get(struct task_struct *target,
625                           const struct user_regset *regset,
626                           unsigned int pos, unsigned int count,
627                           void *kbuf, void __user *ubuf)
628 {
629         int ret = 0;
630         unsigned int i, start, num_regs;
631
632         /* Calculate the number of AArch32 registers contained in count */
633         num_regs = count / regset->size;
634
635         /* Convert pos into an register number */
636         start = pos / regset->size;
637
638         if (start + num_regs > regset->n)
639                 return -EIO;
640
641         for (i = 0; i < num_regs; ++i) {
642                 unsigned int idx = start + i;
643                 void *reg;
644
645                 switch (idx) {
646                 case 15:
647                         reg = (void *)&task_pt_regs(target)->pc;
648                         break;
649                 case 16:
650                         reg = (void *)&task_pt_regs(target)->pstate;
651                         break;
652                 case 17:
653                         reg = (void *)&task_pt_regs(target)->orig_x0;
654                         break;
655                 default:
656                         reg = (void *)&task_pt_regs(target)->regs[idx];
657                 }
658
659                 ret = copy_to_user(ubuf, reg, sizeof(compat_ulong_t));
660
661                 if (ret)
662                         break;
663                 else
664                         ubuf += sizeof(compat_ulong_t);
665         }
666
667         return ret;
668 }
669
670 static int compat_gpr_set(struct task_struct *target,
671                           const struct user_regset *regset,
672                           unsigned int pos, unsigned int count,
673                           const void *kbuf, const void __user *ubuf)
674 {
675         struct pt_regs newregs;
676         int ret = 0;
677         unsigned int i, start, num_regs;
678
679         /* Calculate the number of AArch32 registers contained in count */
680         num_regs = count / regset->size;
681
682         /* Convert pos into an register number */
683         start = pos / regset->size;
684
685         if (start + num_regs > regset->n)
686                 return -EIO;
687
688         newregs = *task_pt_regs(target);
689
690         for (i = 0; i < num_regs; ++i) {
691                 unsigned int idx = start + i;
692                 void *reg;
693
694                 switch (idx) {
695                 case 15:
696                         reg = (void *)&newregs.pc;
697                         break;
698                 case 16:
699                         reg = (void *)&newregs.pstate;
700                         break;
701                 case 17:
702                         reg = (void *)&newregs.orig_x0;
703                         break;
704                 default:
705                         reg = (void *)&newregs.regs[idx];
706                 }
707
708                 ret = copy_from_user(reg, ubuf, sizeof(compat_ulong_t));
709
710                 if (ret)
711                         goto out;
712                 else
713                         ubuf += sizeof(compat_ulong_t);
714         }
715
716         if (valid_user_regs(&newregs.user_regs))
717                 *task_pt_regs(target) = newregs;
718         else
719                 ret = -EINVAL;
720
721 out:
722         return ret;
723 }
724
725 static int compat_vfp_get(struct task_struct *target,
726                           const struct user_regset *regset,
727                           unsigned int pos, unsigned int count,
728                           void *kbuf, void __user *ubuf)
729 {
730         struct user_fpsimd_state *uregs;
731         compat_ulong_t fpscr;
732         int ret;
733
734         uregs = &target->thread.fpsimd_state.user_fpsimd;
735
736         /*
737          * The VFP registers are packed into the fpsimd_state, so they all sit
738          * nicely together for us. We just need to create the fpscr separately.
739          */
740         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
741                                   VFP_STATE_SIZE - sizeof(compat_ulong_t));
742
743         if (count && !ret) {
744                 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
745                         (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
746                 ret = put_user(fpscr, (compat_ulong_t *)ubuf);
747         }
748
749         return ret;
750 }
751
752 static int compat_vfp_set(struct task_struct *target,
753                           const struct user_regset *regset,
754                           unsigned int pos, unsigned int count,
755                           const void *kbuf, const void __user *ubuf)
756 {
757         struct user_fpsimd_state *uregs;
758         compat_ulong_t fpscr;
759         int ret;
760
761         if (pos + count > VFP_STATE_SIZE)
762                 return -EIO;
763
764         uregs = &target->thread.fpsimd_state.user_fpsimd;
765
766         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
767                                  VFP_STATE_SIZE - sizeof(compat_ulong_t));
768
769         if (count && !ret) {
770                 ret = get_user(fpscr, (compat_ulong_t *)ubuf);
771                 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
772                 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
773         }
774
775         fpsimd_flush_task_state(target);
776         return ret;
777 }
778
779 static const struct user_regset aarch32_regsets[] = {
780         [REGSET_COMPAT_GPR] = {
781                 .core_note_type = NT_PRSTATUS,
782                 .n = COMPAT_ELF_NGREG,
783                 .size = sizeof(compat_elf_greg_t),
784                 .align = sizeof(compat_elf_greg_t),
785                 .get = compat_gpr_get,
786                 .set = compat_gpr_set
787         },
788         [REGSET_COMPAT_VFP] = {
789                 .core_note_type = NT_ARM_VFP,
790                 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
791                 .size = sizeof(compat_ulong_t),
792                 .align = sizeof(compat_ulong_t),
793                 .get = compat_vfp_get,
794                 .set = compat_vfp_set
795         },
796 };
797
798 static const struct user_regset_view user_aarch32_view = {
799         .name = "aarch32", .e_machine = EM_ARM,
800         .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
801 };
802
803 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
804                                    compat_ulong_t __user *ret)
805 {
806         compat_ulong_t tmp;
807
808         if (off & 3)
809                 return -EIO;
810
811         if (off == COMPAT_PT_TEXT_ADDR)
812                 tmp = tsk->mm->start_code;
813         else if (off == COMPAT_PT_DATA_ADDR)
814                 tmp = tsk->mm->start_data;
815         else if (off == COMPAT_PT_TEXT_END_ADDR)
816                 tmp = tsk->mm->end_code;
817         else if (off < sizeof(compat_elf_gregset_t))
818                 return copy_regset_to_user(tsk, &user_aarch32_view,
819                                            REGSET_COMPAT_GPR, off,
820                                            sizeof(compat_ulong_t), ret);
821         else if (off >= COMPAT_USER_SZ)
822                 return -EIO;
823         else
824                 tmp = 0;
825
826         return put_user(tmp, ret);
827 }
828
829 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
830                                     compat_ulong_t val)
831 {
832         int ret;
833
834         if (off & 3 || off >= COMPAT_USER_SZ)
835                 return -EIO;
836
837         if (off >= sizeof(compat_elf_gregset_t))
838                 return 0;
839
840         ret = copy_regset_from_user(tsk, &user_aarch32_view,
841                                     REGSET_COMPAT_GPR, off,
842                                     sizeof(compat_ulong_t),
843                                     &val);
844         return ret;
845 }
846
847 #ifdef CONFIG_HAVE_HW_BREAKPOINT
848
849 /*
850  * Convert a virtual register number into an index for a thread_info
851  * breakpoint array. Breakpoints are identified using positive numbers
852  * whilst watchpoints are negative. The registers are laid out as pairs
853  * of (address, control), each pair mapping to a unique hw_breakpoint struct.
854  * Register 0 is reserved for describing resource information.
855  */
856 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
857 {
858         return (abs(num) - 1) >> 1;
859 }
860
861 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
862 {
863         u8 num_brps, num_wrps, debug_arch, wp_len;
864         u32 reg = 0;
865
866         num_brps        = hw_breakpoint_slots(TYPE_INST);
867         num_wrps        = hw_breakpoint_slots(TYPE_DATA);
868
869         debug_arch      = debug_monitors_arch();
870         wp_len          = 8;
871         reg             |= debug_arch;
872         reg             <<= 8;
873         reg             |= wp_len;
874         reg             <<= 8;
875         reg             |= num_wrps;
876         reg             <<= 8;
877         reg             |= num_brps;
878
879         *kdata = reg;
880         return 0;
881 }
882
883 static int compat_ptrace_hbp_get(unsigned int note_type,
884                                  struct task_struct *tsk,
885                                  compat_long_t num,
886                                  u32 *kdata)
887 {
888         u64 addr = 0;
889         u32 ctrl = 0;
890
891         int err, idx = compat_ptrace_hbp_num_to_idx(num);;
892
893         if (num & 1) {
894                 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
895                 *kdata = (u32)addr;
896         } else {
897                 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
898                 *kdata = ctrl;
899         }
900
901         return err;
902 }
903
904 static int compat_ptrace_hbp_set(unsigned int note_type,
905                                  struct task_struct *tsk,
906                                  compat_long_t num,
907                                  u32 *kdata)
908 {
909         u64 addr;
910         u32 ctrl;
911
912         int err, idx = compat_ptrace_hbp_num_to_idx(num);
913
914         if (num & 1) {
915                 addr = *kdata;
916                 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
917         } else {
918                 ctrl = *kdata;
919                 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
920         }
921
922         return err;
923 }
924
925 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
926                                     compat_ulong_t __user *data)
927 {
928         int ret;
929         u32 kdata;
930         mm_segment_t old_fs = get_fs();
931
932         set_fs(KERNEL_DS);
933         /* Watchpoint */
934         if (num < 0) {
935                 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
936         /* Resource info */
937         } else if (num == 0) {
938                 ret = compat_ptrace_hbp_get_resource_info(&kdata);
939         /* Breakpoint */
940         } else {
941                 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
942         }
943         set_fs(old_fs);
944
945         if (!ret)
946                 ret = put_user(kdata, data);
947
948         return ret;
949 }
950
951 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
952                                     compat_ulong_t __user *data)
953 {
954         int ret;
955         u32 kdata = 0;
956         mm_segment_t old_fs = get_fs();
957
958         if (num == 0)
959                 return 0;
960
961         ret = get_user(kdata, data);
962         if (ret)
963                 return ret;
964
965         set_fs(KERNEL_DS);
966         if (num < 0)
967                 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
968         else
969                 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
970         set_fs(old_fs);
971
972         return ret;
973 }
974 #endif  /* CONFIG_HAVE_HW_BREAKPOINT */
975
976 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
977                         compat_ulong_t caddr, compat_ulong_t cdata)
978 {
979         unsigned long addr = caddr;
980         unsigned long data = cdata;
981         void __user *datap = compat_ptr(data);
982         int ret;
983
984         switch (request) {
985                 case PTRACE_PEEKUSR:
986                         ret = compat_ptrace_read_user(child, addr, datap);
987                         break;
988
989                 case PTRACE_POKEUSR:
990                         ret = compat_ptrace_write_user(child, addr, data);
991                         break;
992
993                 case COMPAT_PTRACE_GETREGS:
994                         ret = copy_regset_to_user(child,
995                                                   &user_aarch32_view,
996                                                   REGSET_COMPAT_GPR,
997                                                   0, sizeof(compat_elf_gregset_t),
998                                                   datap);
999                         break;
1000
1001                 case COMPAT_PTRACE_SETREGS:
1002                         ret = copy_regset_from_user(child,
1003                                                     &user_aarch32_view,
1004                                                     REGSET_COMPAT_GPR,
1005                                                     0, sizeof(compat_elf_gregset_t),
1006                                                     datap);
1007                         break;
1008
1009                 case COMPAT_PTRACE_GET_THREAD_AREA:
1010                         ret = put_user((compat_ulong_t)child->thread.tp_value,
1011                                        (compat_ulong_t __user *)datap);
1012                         break;
1013
1014                 case COMPAT_PTRACE_SET_SYSCALL:
1015                         task_pt_regs(child)->syscallno = data;
1016                         ret = 0;
1017                         break;
1018
1019                 case COMPAT_PTRACE_GETVFPREGS:
1020                         ret = copy_regset_to_user(child,
1021                                                   &user_aarch32_view,
1022                                                   REGSET_COMPAT_VFP,
1023                                                   0, VFP_STATE_SIZE,
1024                                                   datap);
1025                         break;
1026
1027                 case COMPAT_PTRACE_SETVFPREGS:
1028                         ret = copy_regset_from_user(child,
1029                                                     &user_aarch32_view,
1030                                                     REGSET_COMPAT_VFP,
1031                                                     0, VFP_STATE_SIZE,
1032                                                     datap);
1033                         break;
1034
1035 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1036                 case COMPAT_PTRACE_GETHBPREGS:
1037                         ret = compat_ptrace_gethbpregs(child, addr, datap);
1038                         break;
1039
1040                 case COMPAT_PTRACE_SETHBPREGS:
1041                         ret = compat_ptrace_sethbpregs(child, addr, datap);
1042                         break;
1043 #endif
1044
1045                 default:
1046                         ret = compat_ptrace_request(child, request, addr,
1047                                                     data);
1048                         break;
1049         }
1050
1051         return ret;
1052 }
1053 #endif /* CONFIG_COMPAT */
1054
1055 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1056 {
1057 #ifdef CONFIG_COMPAT
1058         if (is_compat_thread(task_thread_info(task)))
1059                 return &user_aarch32_view;
1060 #endif
1061         return &user_aarch64_view;
1062 }
1063
1064 long arch_ptrace(struct task_struct *child, long request,
1065                  unsigned long addr, unsigned long data)
1066 {
1067         return ptrace_request(child, request, addr, data);
1068 }
1069
1070 enum ptrace_syscall_dir {
1071         PTRACE_SYSCALL_ENTER = 0,
1072         PTRACE_SYSCALL_EXIT,
1073 };
1074
1075 static void tracehook_report_syscall(struct pt_regs *regs,
1076                                      enum ptrace_syscall_dir dir)
1077 {
1078         int regno;
1079         unsigned long saved_reg;
1080
1081         /*
1082          * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1083          * used to denote syscall entry/exit:
1084          */
1085         regno = (is_compat_task() ? 12 : 7);
1086         saved_reg = regs->regs[regno];
1087         regs->regs[regno] = dir;
1088
1089         if (dir == PTRACE_SYSCALL_EXIT)
1090                 tracehook_report_syscall_exit(regs, 0);
1091         else if (tracehook_report_syscall_entry(regs))
1092                 regs->syscallno = ~0UL;
1093
1094         regs->regs[regno] = saved_reg;
1095 }
1096
1097 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1098 {
1099         if (test_thread_flag(TIF_SYSCALL_TRACE))
1100                 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1101
1102         audit_syscall_entry(syscall_get_arch(), regs->syscallno,
1103                 regs->orig_x0, regs->regs[1], regs->regs[2], regs->regs[3]);
1104
1105         return regs->syscallno;
1106 }
1107
1108 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1109 {
1110         audit_syscall_exit(regs);
1111
1112         if (test_thread_flag(TIF_SYSCALL_TRACE))
1113                 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1114 }