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/user.h>
28 #include <linux/security.h>
29 #include <linux/tracehook.h>
30 #include <linux/audit.h>
31 #include <linux/seccomp.h>
32 #include <linux/ftrace.h>
34 #include <asm/byteorder.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, __s64 __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], data + i);
78 __put_user((long)regs->lo, data + EF_LO - EF_R0);
79 __put_user((long)regs->hi, data + EF_HI - EF_R0);
80 __put_user((long)regs->cp0_epc, data + EF_CP0_EPC - EF_R0);
81 __put_user((long)regs->cp0_badvaddr, data + EF_CP0_BADVADDR - EF_R0);
82 __put_user((long)regs->cp0_status, data + EF_CP0_STATUS - EF_R0);
83 __put_user((long)regs->cp0_cause, data + EF_CP0_CAUSE - EF_R0);
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, __s64 __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], data + i);
105 __get_user(regs->lo, data + EF_LO - EF_R0);
106 __get_user(regs->hi, data + EF_HI - EF_R0);
107 __get_user(regs->cp0_epc, data + EF_CP0_EPC - EF_R0);
109 /* badvaddr, status, and cause may not be written. */
114 int ptrace_getfpregs(struct task_struct *child, __u32 __user *data)
119 if (!access_ok(VERIFY_WRITE, data, 33 * 8))
122 if (tsk_used_math(child)) {
123 union fpureg *fregs = get_fpu_regs(child);
124 for (i = 0; i < 32; i++)
125 __put_user(get_fpr64(&fregs[i], 0),
126 i + (__u64 __user *)data);
128 for (i = 0; i < 32; i++)
129 __put_user((__u64) -1, i + (__u64 __user *) data);
132 __put_user(child->thread.fpu.fcr31, data + 64);
138 if (cpu_has_mipsmt) {
139 unsigned int vpflags = dvpe();
140 flags = read_c0_status();
141 __enable_fpu(FPU_AS_IS);
142 __asm__ __volatile__("cfc1\t%0,$0" : "=r" (tmp));
143 write_c0_status(flags);
146 flags = read_c0_status();
147 __enable_fpu(FPU_AS_IS);
148 __asm__ __volatile__("cfc1\t%0,$0" : "=r" (tmp));
149 write_c0_status(flags);
155 __put_user(tmp, data + 65);
160 int ptrace_setfpregs(struct task_struct *child, __u32 __user *data)
166 if (!access_ok(VERIFY_READ, data, 33 * 8))
169 fregs = get_fpu_regs(child);
171 for (i = 0; i < 32; i++) {
172 __get_user(fpr_val, i + (__u64 __user *)data);
173 set_fpr64(&fregs[i], 0, fpr_val);
176 __get_user(child->thread.fpu.fcr31, data + 64);
178 /* FIR may not be written. */
183 int ptrace_get_watch_regs(struct task_struct *child,
184 struct pt_watch_regs __user *addr)
186 enum pt_watch_style style;
189 if (!cpu_has_watch || current_cpu_data.watch_reg_use_cnt == 0)
191 if (!access_ok(VERIFY_WRITE, addr, sizeof(struct pt_watch_regs)))
195 style = pt_watch_style_mips32;
196 #define WATCH_STYLE mips32
198 style = pt_watch_style_mips64;
199 #define WATCH_STYLE mips64
202 __put_user(style, &addr->style);
203 __put_user(current_cpu_data.watch_reg_use_cnt,
204 &addr->WATCH_STYLE.num_valid);
205 for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
206 __put_user(child->thread.watch.mips3264.watchlo[i],
207 &addr->WATCH_STYLE.watchlo[i]);
208 __put_user(child->thread.watch.mips3264.watchhi[i] & 0xfff,
209 &addr->WATCH_STYLE.watchhi[i]);
210 __put_user(current_cpu_data.watch_reg_masks[i],
211 &addr->WATCH_STYLE.watch_masks[i]);
214 __put_user(0, &addr->WATCH_STYLE.watchlo[i]);
215 __put_user(0, &addr->WATCH_STYLE.watchhi[i]);
216 __put_user(0, &addr->WATCH_STYLE.watch_masks[i]);
222 int ptrace_set_watch_regs(struct task_struct *child,
223 struct pt_watch_regs __user *addr)
226 int watch_active = 0;
227 unsigned long lt[NUM_WATCH_REGS];
228 u16 ht[NUM_WATCH_REGS];
230 if (!cpu_has_watch || current_cpu_data.watch_reg_use_cnt == 0)
232 if (!access_ok(VERIFY_READ, addr, sizeof(struct pt_watch_regs)))
234 /* Check the values. */
235 for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
236 __get_user(lt[i], &addr->WATCH_STYLE.watchlo[i]);
238 if (lt[i] & __UA_LIMIT)
241 if (test_tsk_thread_flag(child, TIF_32BIT_ADDR)) {
242 if (lt[i] & 0xffffffff80000000UL)
245 if (lt[i] & __UA_LIMIT)
249 __get_user(ht[i], &addr->WATCH_STYLE.watchhi[i]);
254 for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
257 child->thread.watch.mips3264.watchlo[i] = lt[i];
259 child->thread.watch.mips3264.watchhi[i] = ht[i];
263 set_tsk_thread_flag(child, TIF_LOAD_WATCH);
265 clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
270 /* regset get/set implementations */
272 static int gpr_get(struct task_struct *target,
273 const struct user_regset *regset,
274 unsigned int pos, unsigned int count,
275 void *kbuf, void __user *ubuf)
277 struct pt_regs *regs = task_pt_regs(target);
279 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
280 regs, 0, sizeof(*regs));
283 static int gpr_set(struct task_struct *target,
284 const struct user_regset *regset,
285 unsigned int pos, unsigned int count,
286 const void *kbuf, const void __user *ubuf)
288 struct pt_regs newregs;
291 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
297 *task_pt_regs(target) = newregs;
302 static int fpr_get(struct task_struct *target,
303 const struct user_regset *regset,
304 unsigned int pos, unsigned int count,
305 void *kbuf, void __user *ubuf)
313 if (sizeof(target->thread.fpu.fpr[i]) == sizeof(elf_fpreg_t))
314 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
316 0, sizeof(elf_fpregset_t));
318 for (i = 0; i < NUM_FPU_REGS; i++) {
319 fpr_val = get_fpr64(&target->thread.fpu.fpr[i], 0);
320 err = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
321 &fpr_val, i * sizeof(elf_fpreg_t),
322 (i + 1) * sizeof(elf_fpreg_t));
330 static int fpr_set(struct task_struct *target,
331 const struct user_regset *regset,
332 unsigned int pos, unsigned int count,
333 const void *kbuf, const void __user *ubuf)
341 if (sizeof(target->thread.fpu.fpr[i]) == sizeof(elf_fpreg_t))
342 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
344 0, sizeof(elf_fpregset_t));
346 for (i = 0; i < NUM_FPU_REGS; i++) {
347 err = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
348 &fpr_val, i * sizeof(elf_fpreg_t),
349 (i + 1) * sizeof(elf_fpreg_t));
352 set_fpr64(&target->thread.fpu.fpr[i], 0, fpr_val);
363 static const struct user_regset mips_regsets[] = {
365 .core_note_type = NT_PRSTATUS,
367 .size = sizeof(unsigned int),
368 .align = sizeof(unsigned int),
373 .core_note_type = NT_PRFPREG,
375 .size = sizeof(elf_fpreg_t),
376 .align = sizeof(elf_fpreg_t),
382 static const struct user_regset_view user_mips_view = {
384 .e_machine = ELF_ARCH,
385 .ei_osabi = ELF_OSABI,
386 .regsets = mips_regsets,
387 .n = ARRAY_SIZE(mips_regsets),
390 static const struct user_regset mips64_regsets[] = {
392 .core_note_type = NT_PRSTATUS,
394 .size = sizeof(unsigned long),
395 .align = sizeof(unsigned long),
400 .core_note_type = NT_PRFPREG,
402 .size = sizeof(elf_fpreg_t),
403 .align = sizeof(elf_fpreg_t),
409 static const struct user_regset_view user_mips64_view = {
411 .e_machine = ELF_ARCH,
412 .ei_osabi = ELF_OSABI,
413 .regsets = mips64_regsets,
414 .n = ARRAY_SIZE(mips_regsets),
417 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
420 return &user_mips_view;
423 #ifdef CONFIG_MIPS32_O32
424 if (test_thread_flag(TIF_32BIT_REGS))
425 return &user_mips_view;
428 return &user_mips64_view;
431 long arch_ptrace(struct task_struct *child, long request,
432 unsigned long addr, unsigned long data)
435 void __user *addrp = (void __user *) addr;
436 void __user *datavp = (void __user *) data;
437 unsigned long __user *datalp = (void __user *) data;
440 /* when I and D space are separate, these will need to be fixed. */
441 case PTRACE_PEEKTEXT: /* read word at location addr. */
442 case PTRACE_PEEKDATA:
443 ret = generic_ptrace_peekdata(child, addr, data);
446 /* Read the word at location addr in the USER area. */
447 case PTRACE_PEEKUSR: {
448 struct pt_regs *regs;
450 unsigned long tmp = 0;
452 regs = task_pt_regs(child);
453 ret = 0; /* Default return value. */
457 tmp = regs->regs[addr];
459 case FPR_BASE ... FPR_BASE + 31:
460 if (!tsk_used_math(child)) {
461 /* FP not yet used */
465 fregs = get_fpu_regs(child);
468 if (test_thread_flag(TIF_32BIT_FPREGS)) {
470 * The odd registers are actually the high
471 * order bits of the values stored in the even
472 * registers - unless we're using r2k_switch.S.
474 tmp = get_fpr32(&fregs[(addr & ~1) - FPR_BASE],
479 tmp = get_fpr32(&fregs[addr - FPR_BASE], 0);
485 tmp = regs->cp0_cause;
488 tmp = regs->cp0_badvaddr;
496 #ifdef CONFIG_CPU_HAS_SMARTMIPS
502 tmp = child->thread.fpu.fcr31;
505 /* implementation / version register */
506 tmp = current_cpu_data.fpu_id;
508 case DSP_BASE ... DSP_BASE + 5: {
516 dregs = __get_dsp_regs(child);
517 tmp = (unsigned long) (dregs[addr - DSP_BASE]);
526 tmp = child->thread.dsp.dspcontrol;
533 ret = put_user(tmp, datalp);
537 /* when I and D space are separate, this will have to be fixed. */
538 case PTRACE_POKETEXT: /* write the word at location addr. */
539 case PTRACE_POKEDATA:
540 ret = generic_ptrace_pokedata(child, addr, data);
543 case PTRACE_POKEUSR: {
544 struct pt_regs *regs;
546 regs = task_pt_regs(child);
550 regs->regs[addr] = data;
552 case FPR_BASE ... FPR_BASE + 31: {
553 union fpureg *fregs = get_fpu_regs(child);
555 if (!tsk_used_math(child)) {
556 /* FP not yet used */
557 memset(&child->thread.fpu, ~0,
558 sizeof(child->thread.fpu));
559 child->thread.fpu.fcr31 = 0;
562 if (test_thread_flag(TIF_32BIT_FPREGS)) {
564 * The odd registers are actually the high
565 * order bits of the values stored in the even
566 * registers - unless we're using r2k_switch.S.
568 set_fpr32(&fregs[(addr & ~1) - FPR_BASE],
573 set_fpr64(&fregs[addr - FPR_BASE], 0, data);
577 regs->cp0_epc = data;
585 #ifdef CONFIG_CPU_HAS_SMARTMIPS
591 child->thread.fpu.fcr31 = data;
593 case DSP_BASE ... DSP_BASE + 5: {
601 dregs = __get_dsp_regs(child);
602 dregs[addr - DSP_BASE] = data;
610 child->thread.dsp.dspcontrol = data;
613 /* The rest are not allowed. */
621 ret = ptrace_getregs(child, datavp);
625 ret = ptrace_setregs(child, datavp);
628 case PTRACE_GETFPREGS:
629 ret = ptrace_getfpregs(child, datavp);
632 case PTRACE_SETFPREGS:
633 ret = ptrace_setfpregs(child, datavp);
636 case PTRACE_GET_THREAD_AREA:
637 ret = put_user(task_thread_info(child)->tp_value, datalp);
640 case PTRACE_GET_WATCH_REGS:
641 ret = ptrace_get_watch_regs(child, addrp);
644 case PTRACE_SET_WATCH_REGS:
645 ret = ptrace_set_watch_regs(child, addrp);
649 ret = ptrace_request(child, request, addr, data);
657 * Notification of system call entry/exit
658 * - triggered by current->work.syscall_trace
660 asmlinkage long syscall_trace_enter(struct pt_regs *regs, long syscall)
665 if (secure_computing(syscall) == -1)
668 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
669 tracehook_report_syscall_entry(regs))
672 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
673 trace_sys_enter(regs, regs->regs[2]);
675 audit_syscall_entry(syscall_get_arch(current, regs),
677 regs->regs[4], regs->regs[5],
678 regs->regs[6], regs->regs[7]);
683 * Notification of system call entry/exit
684 * - triggered by current->work.syscall_trace
686 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
689 * We may come here right after calling schedule_user()
690 * or do_notify_resume(), in which case we can be in RCU
695 audit_syscall_exit(regs);
697 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
698 trace_sys_exit(regs, regs->regs[2]);
700 if (test_thread_flag(TIF_SYSCALL_TRACE))
701 tracehook_report_syscall_exit(regs, 0);