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