arm64/ptrace: Preserve previous registers for short regset write - 3
[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/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                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
454                                          offset, offset + PTRACE_HBP_ADDR_SZ);
455                 if (ret)
456                         return ret;
457                 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
458                 if (ret)
459                         return ret;
460                 offset += PTRACE_HBP_ADDR_SZ;
461
462                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
463                                          offset, offset + PTRACE_HBP_CTRL_SZ);
464                 if (ret)
465                         return ret;
466                 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
467                 if (ret)
468                         return ret;
469                 offset += PTRACE_HBP_CTRL_SZ;
470
471                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
472                                                 offset,
473                                                 offset + PTRACE_HBP_PAD_SZ);
474                 if (ret)
475                         return ret;
476                 offset += PTRACE_HBP_PAD_SZ;
477                 idx++;
478         }
479
480         return 0;
481 }
482 #endif  /* CONFIG_HAVE_HW_BREAKPOINT */
483
484 static int gpr_get(struct task_struct *target,
485                    const struct user_regset *regset,
486                    unsigned int pos, unsigned int count,
487                    void *kbuf, void __user *ubuf)
488 {
489         struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
490         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
491 }
492
493 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
494                    unsigned int pos, unsigned int count,
495                    const void *kbuf, const void __user *ubuf)
496 {
497         int ret;
498         struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
499
500         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
501         if (ret)
502                 return ret;
503
504         if (!valid_user_regs(&newregs, target))
505                 return -EINVAL;
506
507         task_pt_regs(target)->user_regs = newregs;
508         return 0;
509 }
510
511 /*
512  * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
513  */
514 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
515                    unsigned int pos, unsigned int count,
516                    void *kbuf, void __user *ubuf)
517 {
518         struct user_fpsimd_state *uregs;
519         uregs = &target->thread.fpsimd_state.user_fpsimd;
520         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
521 }
522
523 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
524                    unsigned int pos, unsigned int count,
525                    const void *kbuf, const void __user *ubuf)
526 {
527         int ret;
528         struct user_fpsimd_state newstate =
529                 target->thread.fpsimd_state.user_fpsimd;
530
531         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
532         if (ret)
533                 return ret;
534
535         target->thread.fpsimd_state.user_fpsimd = newstate;
536         fpsimd_flush_task_state(target);
537         return ret;
538 }
539
540 static int tls_get(struct task_struct *target, const struct user_regset *regset,
541                    unsigned int pos, unsigned int count,
542                    void *kbuf, void __user *ubuf)
543 {
544         unsigned long *tls = &target->thread.tp_value;
545         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
546 }
547
548 static int tls_set(struct task_struct *target, const struct user_regset *regset,
549                    unsigned int pos, unsigned int count,
550                    const void *kbuf, const void __user *ubuf)
551 {
552         int ret;
553         unsigned long tls = target->thread.tp_value;
554
555         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
556         if (ret)
557                 return ret;
558
559         target->thread.tp_value = tls;
560         return ret;
561 }
562
563 static int system_call_get(struct task_struct *target,
564                            const struct user_regset *regset,
565                            unsigned int pos, unsigned int count,
566                            void *kbuf, void __user *ubuf)
567 {
568         int syscallno = task_pt_regs(target)->syscallno;
569
570         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
571                                    &syscallno, 0, -1);
572 }
573
574 static int system_call_set(struct task_struct *target,
575                            const struct user_regset *regset,
576                            unsigned int pos, unsigned int count,
577                            const void *kbuf, const void __user *ubuf)
578 {
579         int syscallno = task_pt_regs(target)->syscallno;
580         int ret;
581
582         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
583         if (ret)
584                 return ret;
585
586         task_pt_regs(target)->syscallno = syscallno;
587         return ret;
588 }
589
590 enum aarch64_regset {
591         REGSET_GPR,
592         REGSET_FPR,
593         REGSET_TLS,
594 #ifdef CONFIG_HAVE_HW_BREAKPOINT
595         REGSET_HW_BREAK,
596         REGSET_HW_WATCH,
597 #endif
598         REGSET_SYSTEM_CALL,
599 };
600
601 static const struct user_regset aarch64_regsets[] = {
602         [REGSET_GPR] = {
603                 .core_note_type = NT_PRSTATUS,
604                 .n = sizeof(struct user_pt_regs) / sizeof(u64),
605                 .size = sizeof(u64),
606                 .align = sizeof(u64),
607                 .get = gpr_get,
608                 .set = gpr_set
609         },
610         [REGSET_FPR] = {
611                 .core_note_type = NT_PRFPREG,
612                 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
613                 /*
614                  * We pretend we have 32-bit registers because the fpsr and
615                  * fpcr are 32-bits wide.
616                  */
617                 .size = sizeof(u32),
618                 .align = sizeof(u32),
619                 .get = fpr_get,
620                 .set = fpr_set
621         },
622         [REGSET_TLS] = {
623                 .core_note_type = NT_ARM_TLS,
624                 .n = 1,
625                 .size = sizeof(void *),
626                 .align = sizeof(void *),
627                 .get = tls_get,
628                 .set = tls_set,
629         },
630 #ifdef CONFIG_HAVE_HW_BREAKPOINT
631         [REGSET_HW_BREAK] = {
632                 .core_note_type = NT_ARM_HW_BREAK,
633                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
634                 .size = sizeof(u32),
635                 .align = sizeof(u32),
636                 .get = hw_break_get,
637                 .set = hw_break_set,
638         },
639         [REGSET_HW_WATCH] = {
640                 .core_note_type = NT_ARM_HW_WATCH,
641                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
642                 .size = sizeof(u32),
643                 .align = sizeof(u32),
644                 .get = hw_break_get,
645                 .set = hw_break_set,
646         },
647 #endif
648         [REGSET_SYSTEM_CALL] = {
649                 .core_note_type = NT_ARM_SYSTEM_CALL,
650                 .n = 1,
651                 .size = sizeof(int),
652                 .align = sizeof(int),
653                 .get = system_call_get,
654                 .set = system_call_set,
655         },
656 };
657
658 static const struct user_regset_view user_aarch64_view = {
659         .name = "aarch64", .e_machine = EM_AARCH64,
660         .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
661 };
662
663 #ifdef CONFIG_COMPAT
664 #include <linux/compat.h>
665
666 enum compat_regset {
667         REGSET_COMPAT_GPR,
668         REGSET_COMPAT_VFP,
669 };
670
671 static int compat_gpr_get(struct task_struct *target,
672                           const struct user_regset *regset,
673                           unsigned int pos, unsigned int count,
674                           void *kbuf, void __user *ubuf)
675 {
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         for (i = 0; i < num_regs; ++i) {
689                 unsigned int idx = start + i;
690                 compat_ulong_t reg;
691
692                 switch (idx) {
693                 case 15:
694                         reg = task_pt_regs(target)->pc;
695                         break;
696                 case 16:
697                         reg = task_pt_regs(target)->pstate;
698                         break;
699                 case 17:
700                         reg = task_pt_regs(target)->orig_x0;
701                         break;
702                 default:
703                         reg = task_pt_regs(target)->regs[idx];
704                 }
705
706                 if (kbuf) {
707                         memcpy(kbuf, &reg, sizeof(reg));
708                         kbuf += sizeof(reg);
709                 } else {
710                         ret = copy_to_user(ubuf, &reg, sizeof(reg));
711                         if (ret) {
712                                 ret = -EFAULT;
713                                 break;
714                         }
715
716                         ubuf += sizeof(reg);
717                 }
718         }
719
720         return ret;
721 }
722
723 static int compat_gpr_set(struct task_struct *target,
724                           const struct user_regset *regset,
725                           unsigned int pos, unsigned int count,
726                           const void *kbuf, const void __user *ubuf)
727 {
728         struct pt_regs newregs;
729         int ret = 0;
730         unsigned int i, start, num_regs;
731
732         /* Calculate the number of AArch32 registers contained in count */
733         num_regs = count / regset->size;
734
735         /* Convert pos into an register number */
736         start = pos / regset->size;
737
738         if (start + num_regs > regset->n)
739                 return -EIO;
740
741         newregs = *task_pt_regs(target);
742
743         for (i = 0; i < num_regs; ++i) {
744                 unsigned int idx = start + i;
745                 compat_ulong_t reg;
746
747                 if (kbuf) {
748                         memcpy(&reg, kbuf, sizeof(reg));
749                         kbuf += sizeof(reg);
750                 } else {
751                         ret = copy_from_user(&reg, ubuf, sizeof(reg));
752                         if (ret) {
753                                 ret = -EFAULT;
754                                 break;
755                         }
756
757                         ubuf += sizeof(reg);
758                 }
759
760                 switch (idx) {
761                 case 15:
762                         newregs.pc = reg;
763                         break;
764                 case 16:
765                         newregs.pstate = reg;
766                         break;
767                 case 17:
768                         newregs.orig_x0 = reg;
769                         break;
770                 default:
771                         newregs.regs[idx] = reg;
772                 }
773
774         }
775
776         if (valid_user_regs(&newregs.user_regs, target))
777                 *task_pt_regs(target) = newregs;
778         else
779                 ret = -EINVAL;
780
781         return ret;
782 }
783
784 static int compat_vfp_get(struct task_struct *target,
785                           const struct user_regset *regset,
786                           unsigned int pos, unsigned int count,
787                           void *kbuf, void __user *ubuf)
788 {
789         struct user_fpsimd_state *uregs;
790         compat_ulong_t fpscr;
791         int ret;
792
793         uregs = &target->thread.fpsimd_state.user_fpsimd;
794
795         /*
796          * The VFP registers are packed into the fpsimd_state, so they all sit
797          * nicely together for us. We just need to create the fpscr separately.
798          */
799         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
800                                   VFP_STATE_SIZE - sizeof(compat_ulong_t));
801
802         if (count && !ret) {
803                 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
804                         (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
805                 ret = put_user(fpscr, (compat_ulong_t *)ubuf);
806         }
807
808         return ret;
809 }
810
811 static int compat_vfp_set(struct task_struct *target,
812                           const struct user_regset *regset,
813                           unsigned int pos, unsigned int count,
814                           const void *kbuf, const void __user *ubuf)
815 {
816         struct user_fpsimd_state *uregs;
817         compat_ulong_t fpscr;
818         int ret;
819
820         if (pos + count > VFP_STATE_SIZE)
821                 return -EIO;
822
823         uregs = &target->thread.fpsimd_state.user_fpsimd;
824
825         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
826                                  VFP_STATE_SIZE - sizeof(compat_ulong_t));
827
828         if (count && !ret) {
829                 ret = get_user(fpscr, (compat_ulong_t *)ubuf);
830                 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
831                 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
832         }
833
834         fpsimd_flush_task_state(target);
835         return ret;
836 }
837
838 static int compat_tls_get(struct task_struct *target,
839                           const struct user_regset *regset, unsigned int pos,
840                           unsigned int count, void *kbuf, void __user *ubuf)
841 {
842         compat_ulong_t tls = (compat_ulong_t)target->thread.tp_value;
843         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
844 }
845
846 static int compat_tls_set(struct task_struct *target,
847                           const struct user_regset *regset, unsigned int pos,
848                           unsigned int count, const void *kbuf,
849                           const void __user *ubuf)
850 {
851         int ret;
852         compat_ulong_t tls = target->thread.tp_value;
853
854         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
855         if (ret)
856                 return ret;
857
858         target->thread.tp_value = tls;
859         return ret;
860 }
861
862 static const struct user_regset aarch32_regsets[] = {
863         [REGSET_COMPAT_GPR] = {
864                 .core_note_type = NT_PRSTATUS,
865                 .n = COMPAT_ELF_NGREG,
866                 .size = sizeof(compat_elf_greg_t),
867                 .align = sizeof(compat_elf_greg_t),
868                 .get = compat_gpr_get,
869                 .set = compat_gpr_set
870         },
871         [REGSET_COMPAT_VFP] = {
872                 .core_note_type = NT_ARM_VFP,
873                 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
874                 .size = sizeof(compat_ulong_t),
875                 .align = sizeof(compat_ulong_t),
876                 .get = compat_vfp_get,
877                 .set = compat_vfp_set
878         },
879 };
880
881 static const struct user_regset_view user_aarch32_view = {
882         .name = "aarch32", .e_machine = EM_ARM,
883         .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
884 };
885
886 static const struct user_regset aarch32_ptrace_regsets[] = {
887         [REGSET_GPR] = {
888                 .core_note_type = NT_PRSTATUS,
889                 .n = COMPAT_ELF_NGREG,
890                 .size = sizeof(compat_elf_greg_t),
891                 .align = sizeof(compat_elf_greg_t),
892                 .get = compat_gpr_get,
893                 .set = compat_gpr_set
894         },
895         [REGSET_FPR] = {
896                 .core_note_type = NT_ARM_VFP,
897                 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
898                 .size = sizeof(compat_ulong_t),
899                 .align = sizeof(compat_ulong_t),
900                 .get = compat_vfp_get,
901                 .set = compat_vfp_set
902         },
903         [REGSET_TLS] = {
904                 .core_note_type = NT_ARM_TLS,
905                 .n = 1,
906                 .size = sizeof(compat_ulong_t),
907                 .align = sizeof(compat_ulong_t),
908                 .get = compat_tls_get,
909                 .set = compat_tls_set,
910         },
911 #ifdef CONFIG_HAVE_HW_BREAKPOINT
912         [REGSET_HW_BREAK] = {
913                 .core_note_type = NT_ARM_HW_BREAK,
914                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
915                 .size = sizeof(u32),
916                 .align = sizeof(u32),
917                 .get = hw_break_get,
918                 .set = hw_break_set,
919         },
920         [REGSET_HW_WATCH] = {
921                 .core_note_type = NT_ARM_HW_WATCH,
922                 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
923                 .size = sizeof(u32),
924                 .align = sizeof(u32),
925                 .get = hw_break_get,
926                 .set = hw_break_set,
927         },
928 #endif
929         [REGSET_SYSTEM_CALL] = {
930                 .core_note_type = NT_ARM_SYSTEM_CALL,
931                 .n = 1,
932                 .size = sizeof(int),
933                 .align = sizeof(int),
934                 .get = system_call_get,
935                 .set = system_call_set,
936         },
937 };
938
939 static const struct user_regset_view user_aarch32_ptrace_view = {
940         .name = "aarch32", .e_machine = EM_ARM,
941         .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
942 };
943
944 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
945                                    compat_ulong_t __user *ret)
946 {
947         compat_ulong_t tmp;
948
949         if (off & 3)
950                 return -EIO;
951
952         if (off == COMPAT_PT_TEXT_ADDR)
953                 tmp = tsk->mm->start_code;
954         else if (off == COMPAT_PT_DATA_ADDR)
955                 tmp = tsk->mm->start_data;
956         else if (off == COMPAT_PT_TEXT_END_ADDR)
957                 tmp = tsk->mm->end_code;
958         else if (off < sizeof(compat_elf_gregset_t))
959                 return copy_regset_to_user(tsk, &user_aarch32_view,
960                                            REGSET_COMPAT_GPR, off,
961                                            sizeof(compat_ulong_t), ret);
962         else if (off >= COMPAT_USER_SZ)
963                 return -EIO;
964         else
965                 tmp = 0;
966
967         return put_user(tmp, ret);
968 }
969
970 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
971                                     compat_ulong_t val)
972 {
973         int ret;
974         mm_segment_t old_fs = get_fs();
975
976         if (off & 3 || off >= COMPAT_USER_SZ)
977                 return -EIO;
978
979         if (off >= sizeof(compat_elf_gregset_t))
980                 return 0;
981
982         set_fs(KERNEL_DS);
983         ret = copy_regset_from_user(tsk, &user_aarch32_view,
984                                     REGSET_COMPAT_GPR, off,
985                                     sizeof(compat_ulong_t),
986                                     &val);
987         set_fs(old_fs);
988
989         return ret;
990 }
991
992 #ifdef CONFIG_HAVE_HW_BREAKPOINT
993
994 /*
995  * Convert a virtual register number into an index for a thread_info
996  * breakpoint array. Breakpoints are identified using positive numbers
997  * whilst watchpoints are negative. The registers are laid out as pairs
998  * of (address, control), each pair mapping to a unique hw_breakpoint struct.
999  * Register 0 is reserved for describing resource information.
1000  */
1001 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1002 {
1003         return (abs(num) - 1) >> 1;
1004 }
1005
1006 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1007 {
1008         u8 num_brps, num_wrps, debug_arch, wp_len;
1009         u32 reg = 0;
1010
1011         num_brps        = hw_breakpoint_slots(TYPE_INST);
1012         num_wrps        = hw_breakpoint_slots(TYPE_DATA);
1013
1014         debug_arch      = debug_monitors_arch();
1015         wp_len          = 8;
1016         reg             |= debug_arch;
1017         reg             <<= 8;
1018         reg             |= wp_len;
1019         reg             <<= 8;
1020         reg             |= num_wrps;
1021         reg             <<= 8;
1022         reg             |= num_brps;
1023
1024         *kdata = reg;
1025         return 0;
1026 }
1027
1028 static int compat_ptrace_hbp_get(unsigned int note_type,
1029                                  struct task_struct *tsk,
1030                                  compat_long_t num,
1031                                  u32 *kdata)
1032 {
1033         u64 addr = 0;
1034         u32 ctrl = 0;
1035
1036         int err, idx = compat_ptrace_hbp_num_to_idx(num);;
1037
1038         if (num & 1) {
1039                 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1040                 *kdata = (u32)addr;
1041         } else {
1042                 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1043                 *kdata = ctrl;
1044         }
1045
1046         return err;
1047 }
1048
1049 static int compat_ptrace_hbp_set(unsigned int note_type,
1050                                  struct task_struct *tsk,
1051                                  compat_long_t num,
1052                                  u32 *kdata)
1053 {
1054         u64 addr;
1055         u32 ctrl;
1056
1057         int err, idx = compat_ptrace_hbp_num_to_idx(num);
1058
1059         if (num & 1) {
1060                 addr = *kdata;
1061                 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1062         } else {
1063                 ctrl = *kdata;
1064                 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1065         }
1066
1067         return err;
1068 }
1069
1070 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1071                                     compat_ulong_t __user *data)
1072 {
1073         int ret;
1074         u32 kdata;
1075         mm_segment_t old_fs = get_fs();
1076
1077         set_fs(KERNEL_DS);
1078         /* Watchpoint */
1079         if (num < 0) {
1080                 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1081         /* Resource info */
1082         } else if (num == 0) {
1083                 ret = compat_ptrace_hbp_get_resource_info(&kdata);
1084         /* Breakpoint */
1085         } else {
1086                 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1087         }
1088         set_fs(old_fs);
1089
1090         if (!ret)
1091                 ret = put_user(kdata, data);
1092
1093         return ret;
1094 }
1095
1096 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1097                                     compat_ulong_t __user *data)
1098 {
1099         int ret;
1100         u32 kdata = 0;
1101         mm_segment_t old_fs = get_fs();
1102
1103         if (num == 0)
1104                 return 0;
1105
1106         ret = get_user(kdata, data);
1107         if (ret)
1108                 return ret;
1109
1110         set_fs(KERNEL_DS);
1111         if (num < 0)
1112                 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1113         else
1114                 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1115         set_fs(old_fs);
1116
1117         return ret;
1118 }
1119 #endif  /* CONFIG_HAVE_HW_BREAKPOINT */
1120
1121 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1122                         compat_ulong_t caddr, compat_ulong_t cdata)
1123 {
1124         unsigned long addr = caddr;
1125         unsigned long data = cdata;
1126         void __user *datap = compat_ptr(data);
1127         int ret;
1128
1129         switch (request) {
1130                 case PTRACE_PEEKUSR:
1131                         ret = compat_ptrace_read_user(child, addr, datap);
1132                         break;
1133
1134                 case PTRACE_POKEUSR:
1135                         ret = compat_ptrace_write_user(child, addr, data);
1136                         break;
1137
1138                 case COMPAT_PTRACE_GETREGS:
1139                         ret = copy_regset_to_user(child,
1140                                                   &user_aarch32_view,
1141                                                   REGSET_COMPAT_GPR,
1142                                                   0, sizeof(compat_elf_gregset_t),
1143                                                   datap);
1144                         break;
1145
1146                 case COMPAT_PTRACE_SETREGS:
1147                         ret = copy_regset_from_user(child,
1148                                                     &user_aarch32_view,
1149                                                     REGSET_COMPAT_GPR,
1150                                                     0, sizeof(compat_elf_gregset_t),
1151                                                     datap);
1152                         break;
1153
1154                 case COMPAT_PTRACE_GET_THREAD_AREA:
1155                         ret = put_user((compat_ulong_t)child->thread.tp_value,
1156                                        (compat_ulong_t __user *)datap);
1157                         break;
1158
1159                 case COMPAT_PTRACE_SET_SYSCALL:
1160                         task_pt_regs(child)->syscallno = data;
1161                         ret = 0;
1162                         break;
1163
1164                 case COMPAT_PTRACE_GETVFPREGS:
1165                         ret = copy_regset_to_user(child,
1166                                                   &user_aarch32_view,
1167                                                   REGSET_COMPAT_VFP,
1168                                                   0, VFP_STATE_SIZE,
1169                                                   datap);
1170                         break;
1171
1172                 case COMPAT_PTRACE_SETVFPREGS:
1173                         ret = copy_regset_from_user(child,
1174                                                     &user_aarch32_view,
1175                                                     REGSET_COMPAT_VFP,
1176                                                     0, VFP_STATE_SIZE,
1177                                                     datap);
1178                         break;
1179
1180 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1181                 case COMPAT_PTRACE_GETHBPREGS:
1182                         ret = compat_ptrace_gethbpregs(child, addr, datap);
1183                         break;
1184
1185                 case COMPAT_PTRACE_SETHBPREGS:
1186                         ret = compat_ptrace_sethbpregs(child, addr, datap);
1187                         break;
1188 #endif
1189
1190                 default:
1191                         ret = compat_ptrace_request(child, request, addr,
1192                                                     data);
1193                         break;
1194         }
1195
1196         return ret;
1197 }
1198 #endif /* CONFIG_COMPAT */
1199
1200 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1201 {
1202 #ifdef CONFIG_COMPAT
1203         /*
1204          * Core dumping of 32-bit tasks or compat ptrace requests must use the
1205          * user_aarch32_view compatible with arm32. Native ptrace requests on
1206          * 32-bit children use an extended user_aarch32_ptrace_view to allow
1207          * access to the TLS register.
1208          */
1209         if (is_compat_task())
1210                 return &user_aarch32_view;
1211         else if (is_compat_thread(task_thread_info(task)))
1212                 return &user_aarch32_ptrace_view;
1213 #endif
1214         return &user_aarch64_view;
1215 }
1216
1217 long arch_ptrace(struct task_struct *child, long request,
1218                  unsigned long addr, unsigned long data)
1219 {
1220         return ptrace_request(child, request, addr, data);
1221 }
1222
1223 enum ptrace_syscall_dir {
1224         PTRACE_SYSCALL_ENTER = 0,
1225         PTRACE_SYSCALL_EXIT,
1226 };
1227
1228 static void tracehook_report_syscall(struct pt_regs *regs,
1229                                      enum ptrace_syscall_dir dir)
1230 {
1231         int regno;
1232         unsigned long saved_reg;
1233
1234         /*
1235          * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1236          * used to denote syscall entry/exit:
1237          */
1238         regno = (is_compat_task() ? 12 : 7);
1239         saved_reg = regs->regs[regno];
1240         regs->regs[regno] = dir;
1241
1242         if (dir == PTRACE_SYSCALL_EXIT)
1243                 tracehook_report_syscall_exit(regs, 0);
1244         else if (tracehook_report_syscall_entry(regs))
1245                 regs->syscallno = ~0UL;
1246
1247         regs->regs[regno] = saved_reg;
1248 }
1249
1250 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1251 {
1252         /* Do the secure computing check first; failures should be fast. */
1253         if (secure_computing() == -1)
1254                 return -1;
1255
1256         if (test_thread_flag(TIF_SYSCALL_TRACE))
1257                 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1258
1259         if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1260                 trace_sys_enter(regs, regs->syscallno);
1261
1262         audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1263                             regs->regs[2], regs->regs[3]);
1264
1265         return regs->syscallno;
1266 }
1267
1268 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1269 {
1270         audit_syscall_exit(regs);
1271
1272         if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1273                 trace_sys_exit(regs, regs_return_value(regs));
1274
1275         if (test_thread_flag(TIF_SYSCALL_TRACE))
1276                 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1277 }
1278
1279 /*
1280  * Bits which are always architecturally RES0 per ARM DDI 0487A.h
1281  * Userspace cannot use these until they have an architectural meaning.
1282  * We also reserve IL for the kernel; SS is handled dynamically.
1283  */
1284 #define SPSR_EL1_AARCH64_RES0_BITS \
1285         (GENMASK_ULL(63,32) | GENMASK_ULL(27, 22) | GENMASK_ULL(20, 10) | \
1286          GENMASK_ULL(5, 5))
1287 #define SPSR_EL1_AARCH32_RES0_BITS \
1288         (GENMASK_ULL(63,32) | GENMASK_ULL(24, 22) | GENMASK_ULL(20,20))
1289
1290 static int valid_compat_regs(struct user_pt_regs *regs)
1291 {
1292         regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
1293
1294         if (!system_supports_mixed_endian_el0()) {
1295                 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1296                         regs->pstate |= COMPAT_PSR_E_BIT;
1297                 else
1298                         regs->pstate &= ~COMPAT_PSR_E_BIT;
1299         }
1300
1301         if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
1302             (regs->pstate & COMPAT_PSR_A_BIT) == 0 &&
1303             (regs->pstate & COMPAT_PSR_I_BIT) == 0 &&
1304             (regs->pstate & COMPAT_PSR_F_BIT) == 0) {
1305                 return 1;
1306         }
1307
1308         /*
1309          * Force PSR to a valid 32-bit EL0t, preserving the same bits as
1310          * arch/arm.
1311          */
1312         regs->pstate &= COMPAT_PSR_N_BIT | COMPAT_PSR_Z_BIT |
1313                         COMPAT_PSR_C_BIT | COMPAT_PSR_V_BIT |
1314                         COMPAT_PSR_Q_BIT | COMPAT_PSR_IT_MASK |
1315                         COMPAT_PSR_GE_MASK | COMPAT_PSR_E_BIT |
1316                         COMPAT_PSR_T_BIT;
1317         regs->pstate |= PSR_MODE32_BIT;
1318
1319         return 0;
1320 }
1321
1322 static int valid_native_regs(struct user_pt_regs *regs)
1323 {
1324         regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
1325
1326         if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
1327             (regs->pstate & PSR_D_BIT) == 0 &&
1328             (regs->pstate & PSR_A_BIT) == 0 &&
1329             (regs->pstate & PSR_I_BIT) == 0 &&
1330             (regs->pstate & PSR_F_BIT) == 0) {
1331                 return 1;
1332         }
1333
1334         /* Force PSR to a valid 64-bit EL0t */
1335         regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
1336
1337         return 0;
1338 }
1339
1340 /*
1341  * Are the current registers suitable for user mode? (used to maintain
1342  * security in signal handlers)
1343  */
1344 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
1345 {
1346         if (!test_tsk_thread_flag(task, TIF_SINGLESTEP))
1347                 regs->pstate &= ~DBG_SPSR_SS;
1348
1349         if (is_compat_thread(task_thread_info(task)))
1350                 return valid_compat_regs(regs);
1351         else
1352                 return valid_native_regs(regs);
1353 }