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