2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1992 Ross Biro
7 * Copyright (C) Linus Torvalds
8 * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
9 * Copyright (C) 1996 David S. Miller
10 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
11 * Copyright (C) 1999 MIPS Technologies, Inc.
12 * Copyright (C) 2000 Ulf Carlsson
14 * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
17 #include <linux/compiler.h>
18 #include <linux/context_tracking.h>
19 #include <linux/elf.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
23 #include <linux/errno.h>
24 #include <linux/ptrace.h>
25 #include <linux/regset.h>
26 #include <linux/smp.h>
27 #include <linux/security.h>
28 #include <linux/tracehook.h>
29 #include <linux/audit.h>
30 #include <linux/seccomp.h>
31 #include <linux/ftrace.h>
33 #include <asm/byteorder.h>
35 #include <asm/cpu-info.h>
38 #include <asm/mipsregs.h>
39 #include <asm/mipsmtregs.h>
40 #include <asm/pgtable.h>
42 #include <asm/syscall.h>
43 #include <asm/uaccess.h>
44 #include <asm/bootinfo.h>
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/syscalls.h>
51 * Called by kernel/ptrace.c when detaching..
53 * Make sure single step bits etc are not set.
55 void ptrace_disable(struct task_struct *child)
57 /* Don't load the watchpoint registers for the ex-child. */
58 clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
62 * Read a general register set. We always use the 64-bit format, even
63 * for 32-bit kernels and for 32-bit processes on a 64-bit kernel.
64 * Registers are sign extended to fill the available space.
66 int ptrace_getregs(struct task_struct *child, struct user_pt_regs __user *data)
71 if (!access_ok(VERIFY_WRITE, data, 38 * 8))
74 regs = task_pt_regs(child);
76 for (i = 0; i < 32; i++)
77 __put_user((long)regs->regs[i], (__s64 __user *)&data->regs[i]);
78 __put_user((long)regs->lo, (__s64 __user *)&data->lo);
79 __put_user((long)regs->hi, (__s64 __user *)&data->hi);
80 __put_user((long)regs->cp0_epc, (__s64 __user *)&data->cp0_epc);
81 __put_user((long)regs->cp0_badvaddr, (__s64 __user *)&data->cp0_badvaddr);
82 __put_user((long)regs->cp0_status, (__s64 __user *)&data->cp0_status);
83 __put_user((long)regs->cp0_cause, (__s64 __user *)&data->cp0_cause);
89 * Write a general register set. As for PTRACE_GETREGS, we always use
90 * the 64-bit format. On a 32-bit kernel only the lower order half
91 * (according to endianness) will be used.
93 int ptrace_setregs(struct task_struct *child, struct user_pt_regs __user *data)
98 if (!access_ok(VERIFY_READ, data, 38 * 8))
101 regs = task_pt_regs(child);
103 for (i = 0; i < 32; i++)
104 __get_user(regs->regs[i], (__s64 __user *)&data->regs[i]);
105 __get_user(regs->lo, (__s64 __user *)&data->lo);
106 __get_user(regs->hi, (__s64 __user *)&data->hi);
107 __get_user(regs->cp0_epc, (__s64 __user *)&data->cp0_epc);
109 /* badvaddr, status, and cause may not be written. */
114 int ptrace_getfpregs(struct task_struct *child, __u32 __user *data)
118 if (!access_ok(VERIFY_WRITE, data, 33 * 8))
121 if (tsk_used_math(child)) {
122 union fpureg *fregs = get_fpu_regs(child);
123 for (i = 0; i < 32; i++)
124 __put_user(get_fpr64(&fregs[i], 0),
125 i + (__u64 __user *)data);
127 for (i = 0; i < 32; i++)
128 __put_user((__u64) -1, i + (__u64 __user *) data);
131 __put_user(child->thread.fpu.fcr31, data + 64);
132 __put_user(boot_cpu_data.fpu_id, data + 65);
137 int ptrace_setfpregs(struct task_struct *child, __u32 __user *data)
146 if (!access_ok(VERIFY_READ, data, 33 * 8))
149 fregs = get_fpu_regs(child);
151 for (i = 0; i < 32; i++) {
152 __get_user(fpr_val, i + (__u64 __user *)data);
153 set_fpr64(&fregs[i], 0, fpr_val);
156 __get_user(value, data + 64);
157 fcr31 = child->thread.fpu.fcr31;
158 mask = current_cpu_data.fpu_msk31;
159 child->thread.fpu.fcr31 = (value & ~mask) | (fcr31 & mask);
161 /* FIR may not be written. */
166 int ptrace_get_watch_regs(struct task_struct *child,
167 struct pt_watch_regs __user *addr)
169 enum pt_watch_style style;
172 if (!cpu_has_watch || boot_cpu_data.watch_reg_use_cnt == 0)
174 if (!access_ok(VERIFY_WRITE, addr, sizeof(struct pt_watch_regs)))
178 style = pt_watch_style_mips32;
179 #define WATCH_STYLE mips32
181 style = pt_watch_style_mips64;
182 #define WATCH_STYLE mips64
185 __put_user(style, &addr->style);
186 __put_user(boot_cpu_data.watch_reg_use_cnt,
187 &addr->WATCH_STYLE.num_valid);
188 for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
189 __put_user(child->thread.watch.mips3264.watchlo[i],
190 &addr->WATCH_STYLE.watchlo[i]);
191 __put_user(child->thread.watch.mips3264.watchhi[i] & 0xfff,
192 &addr->WATCH_STYLE.watchhi[i]);
193 __put_user(boot_cpu_data.watch_reg_masks[i],
194 &addr->WATCH_STYLE.watch_masks[i]);
197 __put_user(0, &addr->WATCH_STYLE.watchlo[i]);
198 __put_user(0, &addr->WATCH_STYLE.watchhi[i]);
199 __put_user(0, &addr->WATCH_STYLE.watch_masks[i]);
205 int ptrace_set_watch_regs(struct task_struct *child,
206 struct pt_watch_regs __user *addr)
209 int watch_active = 0;
210 unsigned long lt[NUM_WATCH_REGS];
211 u16 ht[NUM_WATCH_REGS];
213 if (!cpu_has_watch || boot_cpu_data.watch_reg_use_cnt == 0)
215 if (!access_ok(VERIFY_READ, addr, sizeof(struct pt_watch_regs)))
217 /* Check the values. */
218 for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
219 __get_user(lt[i], &addr->WATCH_STYLE.watchlo[i]);
221 if (lt[i] & __UA_LIMIT)
224 if (test_tsk_thread_flag(child, TIF_32BIT_ADDR)) {
225 if (lt[i] & 0xffffffff80000000UL)
228 if (lt[i] & __UA_LIMIT)
232 __get_user(ht[i], &addr->WATCH_STYLE.watchhi[i]);
237 for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
240 child->thread.watch.mips3264.watchlo[i] = lt[i];
242 child->thread.watch.mips3264.watchhi[i] = ht[i];
246 set_tsk_thread_flag(child, TIF_LOAD_WATCH);
248 clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
253 /* regset get/set implementations */
255 #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
257 static int gpr32_get(struct task_struct *target,
258 const struct user_regset *regset,
259 unsigned int pos, unsigned int count,
260 void *kbuf, void __user *ubuf)
262 struct pt_regs *regs = task_pt_regs(target);
263 u32 uregs[ELF_NGREG] = {};
266 for (i = MIPS32_EF_R1; i <= MIPS32_EF_R31; i++) {
267 /* k0/k1 are copied as zero. */
268 if (i == MIPS32_EF_R26 || i == MIPS32_EF_R27)
271 uregs[i] = regs->regs[i - MIPS32_EF_R0];
274 uregs[MIPS32_EF_LO] = regs->lo;
275 uregs[MIPS32_EF_HI] = regs->hi;
276 uregs[MIPS32_EF_CP0_EPC] = regs->cp0_epc;
277 uregs[MIPS32_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
278 uregs[MIPS32_EF_CP0_STATUS] = regs->cp0_status;
279 uregs[MIPS32_EF_CP0_CAUSE] = regs->cp0_cause;
281 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
285 static int gpr32_set(struct task_struct *target,
286 const struct user_regset *regset,
287 unsigned int pos, unsigned int count,
288 const void *kbuf, const void __user *ubuf)
290 struct pt_regs *regs = task_pt_regs(target);
291 u32 uregs[ELF_NGREG];
292 unsigned start, num_regs, i;
295 start = pos / sizeof(u32);
296 num_regs = count / sizeof(u32);
298 if (start + num_regs > ELF_NGREG)
301 err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
306 for (i = start; i < num_regs; i++) {
308 * Cast all values to signed here so that if this is a 64-bit
309 * kernel, the supplied 32-bit values will be sign extended.
312 case MIPS32_EF_R1 ... MIPS32_EF_R25:
313 /* k0/k1 are ignored. */
314 case MIPS32_EF_R28 ... MIPS32_EF_R31:
315 regs->regs[i - MIPS32_EF_R0] = (s32)uregs[i];
318 regs->lo = (s32)uregs[i];
321 regs->hi = (s32)uregs[i];
323 case MIPS32_EF_CP0_EPC:
324 regs->cp0_epc = (s32)uregs[i];
332 #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
336 static int gpr64_get(struct task_struct *target,
337 const struct user_regset *regset,
338 unsigned int pos, unsigned int count,
339 void *kbuf, void __user *ubuf)
341 struct pt_regs *regs = task_pt_regs(target);
342 u64 uregs[ELF_NGREG] = {};
345 for (i = MIPS64_EF_R1; i <= MIPS64_EF_R31; i++) {
346 /* k0/k1 are copied as zero. */
347 if (i == MIPS64_EF_R26 || i == MIPS64_EF_R27)
350 uregs[i] = regs->regs[i - MIPS64_EF_R0];
353 uregs[MIPS64_EF_LO] = regs->lo;
354 uregs[MIPS64_EF_HI] = regs->hi;
355 uregs[MIPS64_EF_CP0_EPC] = regs->cp0_epc;
356 uregs[MIPS64_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
357 uregs[MIPS64_EF_CP0_STATUS] = regs->cp0_status;
358 uregs[MIPS64_EF_CP0_CAUSE] = regs->cp0_cause;
360 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
364 static int gpr64_set(struct task_struct *target,
365 const struct user_regset *regset,
366 unsigned int pos, unsigned int count,
367 const void *kbuf, const void __user *ubuf)
369 struct pt_regs *regs = task_pt_regs(target);
370 u64 uregs[ELF_NGREG];
371 unsigned start, num_regs, i;
374 start = pos / sizeof(u64);
375 num_regs = count / sizeof(u64);
377 if (start + num_regs > ELF_NGREG)
380 err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
385 for (i = start; i < num_regs; i++) {
387 case MIPS64_EF_R1 ... MIPS64_EF_R25:
388 /* k0/k1 are ignored. */
389 case MIPS64_EF_R28 ... MIPS64_EF_R31:
390 regs->regs[i - MIPS64_EF_R0] = uregs[i];
398 case MIPS64_EF_CP0_EPC:
399 regs->cp0_epc = uregs[i];
407 #endif /* CONFIG_64BIT */
409 static int fpr_get(struct task_struct *target,
410 const struct user_regset *regset,
411 unsigned int pos, unsigned int count,
412 void *kbuf, void __user *ubuf)
420 if (sizeof(target->thread.fpu.fpr[i]) == sizeof(elf_fpreg_t))
421 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
423 0, sizeof(elf_fpregset_t));
425 for (i = 0; i < NUM_FPU_REGS; i++) {
426 fpr_val = get_fpr64(&target->thread.fpu.fpr[i], 0);
427 err = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
428 &fpr_val, i * sizeof(elf_fpreg_t),
429 (i + 1) * sizeof(elf_fpreg_t));
437 static int fpr_set(struct task_struct *target,
438 const struct user_regset *regset,
439 unsigned int pos, unsigned int count,
440 const void *kbuf, const void __user *ubuf)
448 if (sizeof(target->thread.fpu.fpr[i]) == sizeof(elf_fpreg_t))
449 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
451 0, sizeof(elf_fpregset_t));
453 for (i = 0; i < NUM_FPU_REGS; i++) {
454 err = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
455 &fpr_val, i * sizeof(elf_fpreg_t),
456 (i + 1) * sizeof(elf_fpreg_t));
459 set_fpr64(&target->thread.fpu.fpr[i], 0, fpr_val);
470 #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
472 static const struct user_regset mips_regsets[] = {
474 .core_note_type = NT_PRSTATUS,
476 .size = sizeof(unsigned int),
477 .align = sizeof(unsigned int),
482 .core_note_type = NT_PRFPREG,
484 .size = sizeof(elf_fpreg_t),
485 .align = sizeof(elf_fpreg_t),
491 static const struct user_regset_view user_mips_view = {
493 .e_machine = ELF_ARCH,
494 .ei_osabi = ELF_OSABI,
495 .regsets = mips_regsets,
496 .n = ARRAY_SIZE(mips_regsets),
499 #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
503 static const struct user_regset mips64_regsets[] = {
505 .core_note_type = NT_PRSTATUS,
507 .size = sizeof(unsigned long),
508 .align = sizeof(unsigned long),
513 .core_note_type = NT_PRFPREG,
515 .size = sizeof(elf_fpreg_t),
516 .align = sizeof(elf_fpreg_t),
522 static const struct user_regset_view user_mips64_view = {
524 .e_machine = ELF_ARCH,
525 .ei_osabi = ELF_OSABI,
526 .regsets = mips64_regsets,
527 .n = ARRAY_SIZE(mips64_regsets),
530 #endif /* CONFIG_64BIT */
532 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
535 return &user_mips_view;
537 #ifdef CONFIG_MIPS32_O32
538 if (test_tsk_thread_flag(task, TIF_32BIT_REGS))
539 return &user_mips_view;
541 return &user_mips64_view;
545 long arch_ptrace(struct task_struct *child, long request,
546 unsigned long addr, unsigned long data)
549 void __user *addrp = (void __user *) addr;
550 void __user *datavp = (void __user *) data;
551 unsigned long __user *datalp = (void __user *) data;
554 /* when I and D space are separate, these will need to be fixed. */
555 case PTRACE_PEEKTEXT: /* read word at location addr. */
556 case PTRACE_PEEKDATA:
557 ret = generic_ptrace_peekdata(child, addr, data);
560 /* Read the word at location addr in the USER area. */
561 case PTRACE_PEEKUSR: {
562 struct pt_regs *regs;
564 unsigned long tmp = 0;
566 regs = task_pt_regs(child);
567 ret = 0; /* Default return value. */
571 tmp = regs->regs[addr];
573 case FPR_BASE ... FPR_BASE + 31:
574 if (!tsk_used_math(child)) {
575 /* FP not yet used */
579 fregs = get_fpu_regs(child);
582 if (test_thread_flag(TIF_32BIT_FPREGS)) {
584 * The odd registers are actually the high
585 * order bits of the values stored in the even
586 * registers - unless we're using r2k_switch.S.
588 tmp = get_fpr32(&fregs[(addr & ~1) - FPR_BASE],
593 tmp = get_fpr32(&fregs[addr - FPR_BASE], 0);
599 tmp = regs->cp0_cause;
602 tmp = regs->cp0_badvaddr;
610 #ifdef CONFIG_CPU_HAS_SMARTMIPS
616 tmp = child->thread.fpu.fcr31;
619 /* implementation / version register */
620 tmp = boot_cpu_data.fpu_id;
622 case DSP_BASE ... DSP_BASE + 5: {
630 dregs = __get_dsp_regs(child);
631 tmp = (unsigned long) (dregs[addr - DSP_BASE]);
640 tmp = child->thread.dsp.dspcontrol;
647 ret = put_user(tmp, datalp);
651 /* when I and D space are separate, this will have to be fixed. */
652 case PTRACE_POKETEXT: /* write the word at location addr. */
653 case PTRACE_POKEDATA:
654 ret = generic_ptrace_pokedata(child, addr, data);
657 case PTRACE_POKEUSR: {
658 struct pt_regs *regs;
660 regs = task_pt_regs(child);
664 regs->regs[addr] = data;
666 case FPR_BASE ... FPR_BASE + 31: {
667 union fpureg *fregs = get_fpu_regs(child);
669 if (!tsk_used_math(child)) {
670 /* FP not yet used */
671 memset(&child->thread.fpu, ~0,
672 sizeof(child->thread.fpu));
673 child->thread.fpu.fcr31 = 0;
676 if (test_thread_flag(TIF_32BIT_FPREGS)) {
678 * The odd registers are actually the high
679 * order bits of the values stored in the even
680 * registers - unless we're using r2k_switch.S.
682 set_fpr32(&fregs[(addr & ~1) - FPR_BASE],
687 set_fpr64(&fregs[addr - FPR_BASE], 0, data);
691 regs->cp0_epc = data;
699 #ifdef CONFIG_CPU_HAS_SMARTMIPS
705 child->thread.fpu.fcr31 = data & ~FPU_CSR_ALL_X;
707 case DSP_BASE ... DSP_BASE + 5: {
715 dregs = __get_dsp_regs(child);
716 dregs[addr - DSP_BASE] = data;
724 child->thread.dsp.dspcontrol = data;
727 /* The rest are not allowed. */
735 ret = ptrace_getregs(child, datavp);
739 ret = ptrace_setregs(child, datavp);
742 case PTRACE_GETFPREGS:
743 ret = ptrace_getfpregs(child, datavp);
746 case PTRACE_SETFPREGS:
747 ret = ptrace_setfpregs(child, datavp);
750 case PTRACE_GET_THREAD_AREA:
751 ret = put_user(task_thread_info(child)->tp_value, datalp);
754 case PTRACE_GET_WATCH_REGS:
755 ret = ptrace_get_watch_regs(child, addrp);
758 case PTRACE_SET_WATCH_REGS:
759 ret = ptrace_set_watch_regs(child, addrp);
763 ret = ptrace_request(child, request, addr, data);
771 * Notification of system call entry/exit
772 * - triggered by current->work.syscall_trace
774 asmlinkage long syscall_trace_enter(struct pt_regs *regs, long syscall)
779 current_thread_info()->syscall = syscall;
781 if (secure_computing() == -1)
784 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
785 tracehook_report_syscall_entry(regs))
788 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
789 trace_sys_enter(regs, regs->regs[2]);
791 audit_syscall_entry(syscall, regs->regs[4], regs->regs[5],
792 regs->regs[6], regs->regs[7]);
797 * Notification of system call entry/exit
798 * - triggered by current->work.syscall_trace
800 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
803 * We may come here right after calling schedule_user()
804 * or do_notify_resume(), in which case we can be in RCU
809 audit_syscall_exit(regs);
811 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
812 trace_sys_exit(regs, regs->regs[2]);
814 if (test_thread_flag(TIF_SYSCALL_TRACE))
815 tracehook_report_syscall_exit(regs, 0);