~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/arm64/kernel/ptrace.c

Version: ~ [ linux-5.12-rc7 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp