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>
33 #include <asm/byteorder.h>
37 #include <asm/mipsregs.h>
38 #include <asm/mipsmtregs.h>
39 #include <asm/pgtable.h>
41 #include <asm/syscall.h>
42 #include <asm/uaccess.h>
43 #include <asm/bootinfo.h>
47 * Called by kernel/ptrace.c when detaching..
49 * Make sure single step bits etc are not set.
51 void ptrace_disable(struct task_struct *child)
53 /* Don't load the watchpoint registers for the ex-child. */
54 clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
58 * Read a general register set. We always use the 64-bit format, even
59 * for 32-bit kernels and for 32-bit processes on a 64-bit kernel.
60 * Registers are sign extended to fill the available space.
62 int ptrace_getregs(struct task_struct *child, __s64 __user *data)
67 if (!access_ok(VERIFY_WRITE, data, 38 * 8))
70 regs = task_pt_regs(child);
72 for (i = 0; i < 32; i++)
73 __put_user((long)regs->regs[i], data + i);
74 __put_user((long)regs->lo, data + EF_LO - EF_R0);
75 __put_user((long)regs->hi, data + EF_HI - EF_R0);
76 __put_user((long)regs->cp0_epc, data + EF_CP0_EPC - EF_R0);
77 __put_user((long)regs->cp0_badvaddr, data + EF_CP0_BADVADDR - EF_R0);
78 __put_user((long)regs->cp0_status, data + EF_CP0_STATUS - EF_R0);
79 __put_user((long)regs->cp0_cause, data + EF_CP0_CAUSE - EF_R0);
85 * Write a general register set. As for PTRACE_GETREGS, we always use
86 * the 64-bit format. On a 32-bit kernel only the lower order half
87 * (according to endianness) will be used.
89 int ptrace_setregs(struct task_struct *child, __s64 __user *data)
94 if (!access_ok(VERIFY_READ, data, 38 * 8))
97 regs = task_pt_regs(child);
99 for (i = 0; i < 32; i++)
100 __get_user(regs->regs[i], data + i);
101 __get_user(regs->lo, data + EF_LO - EF_R0);
102 __get_user(regs->hi, data + EF_HI - EF_R0);
103 __get_user(regs->cp0_epc, data + EF_CP0_EPC - EF_R0);
105 /* badvaddr, status, and cause may not be written. */
110 int ptrace_getfpregs(struct task_struct *child, __u32 __user *data)
115 if (!access_ok(VERIFY_WRITE, data, 33 * 8))
118 if (tsk_used_math(child)) {
119 fpureg_t *fregs = get_fpu_regs(child);
120 for (i = 0; i < 32; i++)
121 __put_user(fregs[i], i + (__u64 __user *) data);
123 for (i = 0; i < 32; i++)
124 __put_user((__u64) -1, i + (__u64 __user *) data);
127 __put_user(child->thread.fpu.fcr31, data + 64);
133 if (cpu_has_mipsmt) {
134 unsigned int vpflags = dvpe();
135 flags = read_c0_status();
137 __asm__ __volatile__("cfc1\t%0,$0" : "=r" (tmp));
138 write_c0_status(flags);
141 flags = read_c0_status();
143 __asm__ __volatile__("cfc1\t%0,$0" : "=r" (tmp));
144 write_c0_status(flags);
150 __put_user(tmp, data + 65);
155 int ptrace_setfpregs(struct task_struct *child, __u32 __user *data)
160 if (!access_ok(VERIFY_READ, data, 33 * 8))
163 fregs = get_fpu_regs(child);
165 for (i = 0; i < 32; i++)
166 __get_user(fregs[i], i + (__u64 __user *) data);
168 __get_user(child->thread.fpu.fcr31, data + 64);
170 /* FIR may not be written. */
175 int ptrace_get_watch_regs(struct task_struct *child,
176 struct pt_watch_regs __user *addr)
178 enum pt_watch_style style;
181 if (!cpu_has_watch || current_cpu_data.watch_reg_use_cnt == 0)
183 if (!access_ok(VERIFY_WRITE, addr, sizeof(struct pt_watch_regs)))
187 style = pt_watch_style_mips32;
188 #define WATCH_STYLE mips32
190 style = pt_watch_style_mips64;
191 #define WATCH_STYLE mips64
194 __put_user(style, &addr->style);
195 __put_user(current_cpu_data.watch_reg_use_cnt,
196 &addr->WATCH_STYLE.num_valid);
197 for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
198 __put_user(child->thread.watch.mips3264.watchlo[i],
199 &addr->WATCH_STYLE.watchlo[i]);
200 __put_user(child->thread.watch.mips3264.watchhi[i] & 0xfff,
201 &addr->WATCH_STYLE.watchhi[i]);
202 __put_user(current_cpu_data.watch_reg_masks[i],
203 &addr->WATCH_STYLE.watch_masks[i]);
206 __put_user(0, &addr->WATCH_STYLE.watchlo[i]);
207 __put_user(0, &addr->WATCH_STYLE.watchhi[i]);
208 __put_user(0, &addr->WATCH_STYLE.watch_masks[i]);
214 int ptrace_set_watch_regs(struct task_struct *child,
215 struct pt_watch_regs __user *addr)
218 int watch_active = 0;
219 unsigned long lt[NUM_WATCH_REGS];
220 u16 ht[NUM_WATCH_REGS];
222 if (!cpu_has_watch || current_cpu_data.watch_reg_use_cnt == 0)
224 if (!access_ok(VERIFY_READ, addr, sizeof(struct pt_watch_regs)))
226 /* Check the values. */
227 for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
228 __get_user(lt[i], &addr->WATCH_STYLE.watchlo[i]);
230 if (lt[i] & __UA_LIMIT)
233 if (test_tsk_thread_flag(child, TIF_32BIT_ADDR)) {
234 if (lt[i] & 0xffffffff80000000UL)
237 if (lt[i] & __UA_LIMIT)
241 __get_user(ht[i], &addr->WATCH_STYLE.watchhi[i]);
246 for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
249 child->thread.watch.mips3264.watchlo[i] = lt[i];
251 child->thread.watch.mips3264.watchhi[i] = ht[i];
255 set_tsk_thread_flag(child, TIF_LOAD_WATCH);
257 clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
262 /* regset get/set implementations */
264 static int gpr_get(struct task_struct *target,
265 const struct user_regset *regset,
266 unsigned int pos, unsigned int count,
267 void *kbuf, void __user *ubuf)
269 struct pt_regs *regs = task_pt_regs(target);
271 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
272 regs, 0, sizeof(*regs));
275 static int gpr_set(struct task_struct *target,
276 const struct user_regset *regset,
277 unsigned int pos, unsigned int count,
278 const void *kbuf, const void __user *ubuf)
280 struct pt_regs newregs;
283 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
289 *task_pt_regs(target) = newregs;
294 static int fpr_get(struct task_struct *target,
295 const struct user_regset *regset,
296 unsigned int pos, unsigned int count,
297 void *kbuf, void __user *ubuf)
299 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
301 0, sizeof(elf_fpregset_t));
305 static int fpr_set(struct task_struct *target,
306 const struct user_regset *regset,
307 unsigned int pos, unsigned int count,
308 const void *kbuf, const void __user *ubuf)
310 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
312 0, sizeof(elf_fpregset_t));
321 static const struct user_regset mips_regsets[] = {
323 .core_note_type = NT_PRSTATUS,
325 .size = sizeof(unsigned int),
326 .align = sizeof(unsigned int),
331 .core_note_type = NT_PRFPREG,
333 .size = sizeof(elf_fpreg_t),
334 .align = sizeof(elf_fpreg_t),
340 static const struct user_regset_view user_mips_view = {
342 .e_machine = ELF_ARCH,
343 .ei_osabi = ELF_OSABI,
344 .regsets = mips_regsets,
345 .n = ARRAY_SIZE(mips_regsets),
348 static const struct user_regset mips64_regsets[] = {
350 .core_note_type = NT_PRSTATUS,
352 .size = sizeof(unsigned long),
353 .align = sizeof(unsigned long),
358 .core_note_type = NT_PRFPREG,
360 .size = sizeof(elf_fpreg_t),
361 .align = sizeof(elf_fpreg_t),
367 static const struct user_regset_view user_mips64_view = {
369 .e_machine = ELF_ARCH,
370 .ei_osabi = ELF_OSABI,
371 .regsets = mips64_regsets,
372 .n = ARRAY_SIZE(mips_regsets),
375 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
378 return &user_mips_view;
381 #ifdef CONFIG_MIPS32_O32
382 if (test_thread_flag(TIF_32BIT_REGS))
383 return &user_mips_view;
386 return &user_mips64_view;
389 long arch_ptrace(struct task_struct *child, long request,
390 unsigned long addr, unsigned long data)
393 void __user *addrp = (void __user *) addr;
394 void __user *datavp = (void __user *) data;
395 unsigned long __user *datalp = (void __user *) data;
398 /* when I and D space are separate, these will need to be fixed. */
399 case PTRACE_PEEKTEXT: /* read word at location addr. */
400 case PTRACE_PEEKDATA:
401 ret = generic_ptrace_peekdata(child, addr, data);
404 /* Read the word at location addr in the USER area. */
405 case PTRACE_PEEKUSR: {
406 struct pt_regs *regs;
407 unsigned long tmp = 0;
409 regs = task_pt_regs(child);
410 ret = 0; /* Default return value. */
414 tmp = regs->regs[addr];
416 case FPR_BASE ... FPR_BASE + 31:
417 if (tsk_used_math(child)) {
418 fpureg_t *fregs = get_fpu_regs(child);
422 * The odd registers are actually the high
423 * order bits of the values stored in the even
424 * registers - unless we're using r2k_switch.S.
427 tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
429 tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
432 tmp = fregs[addr - FPR_BASE];
435 tmp = -1; /* FP not yet used */
442 tmp = regs->cp0_cause;
445 tmp = regs->cp0_badvaddr;
453 #ifdef CONFIG_CPU_HAS_SMARTMIPS
459 tmp = child->thread.fpu.fcr31;
461 case FPC_EIR: { /* implementation / version register */
463 #ifdef CONFIG_MIPS_MT_SMTC
464 unsigned long irqflags;
465 unsigned int mtflags;
466 #endif /* CONFIG_MIPS_MT_SMTC */
474 #ifdef CONFIG_MIPS_MT_SMTC
475 /* Read-modify-write of Status must be atomic */
476 local_irq_save(irqflags);
478 #endif /* CONFIG_MIPS_MT_SMTC */
479 if (cpu_has_mipsmt) {
480 unsigned int vpflags = dvpe();
481 flags = read_c0_status();
483 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
484 write_c0_status(flags);
487 flags = read_c0_status();
489 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
490 write_c0_status(flags);
492 #ifdef CONFIG_MIPS_MT_SMTC
494 local_irq_restore(irqflags);
495 #endif /* CONFIG_MIPS_MT_SMTC */
499 case DSP_BASE ... DSP_BASE + 5: {
507 dregs = __get_dsp_regs(child);
508 tmp = (unsigned long) (dregs[addr - DSP_BASE]);
517 tmp = child->thread.dsp.dspcontrol;
524 ret = put_user(tmp, datalp);
528 /* when I and D space are separate, this will have to be fixed. */
529 case PTRACE_POKETEXT: /* write the word at location addr. */
530 case PTRACE_POKEDATA:
531 ret = generic_ptrace_pokedata(child, addr, data);
534 case PTRACE_POKEUSR: {
535 struct pt_regs *regs;
537 regs = task_pt_regs(child);
541 regs->regs[addr] = data;
543 case FPR_BASE ... FPR_BASE + 31: {
544 fpureg_t *fregs = get_fpu_regs(child);
546 if (!tsk_used_math(child)) {
547 /* FP not yet used */
548 memset(&child->thread.fpu, ~0,
549 sizeof(child->thread.fpu));
550 child->thread.fpu.fcr31 = 0;
554 * The odd registers are actually the high order bits
555 * of the values stored in the even registers - unless
556 * we're using r2k_switch.S.
559 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
560 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
562 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
563 fregs[addr - FPR_BASE] |= data;
567 fregs[addr - FPR_BASE] = data;
572 regs->cp0_epc = data;
580 #ifdef CONFIG_CPU_HAS_SMARTMIPS
586 child->thread.fpu.fcr31 = data;
588 case DSP_BASE ... DSP_BASE + 5: {
596 dregs = __get_dsp_regs(child);
597 dregs[addr - DSP_BASE] = data;
605 child->thread.dsp.dspcontrol = data;
608 /* The rest are not allowed. */
616 ret = ptrace_getregs(child, datavp);
620 ret = ptrace_setregs(child, datavp);
623 case PTRACE_GETFPREGS:
624 ret = ptrace_getfpregs(child, datavp);
627 case PTRACE_SETFPREGS:
628 ret = ptrace_setfpregs(child, datavp);
631 case PTRACE_GET_THREAD_AREA:
632 ret = put_user(task_thread_info(child)->tp_value, datalp);
635 case PTRACE_GET_WATCH_REGS:
636 ret = ptrace_get_watch_regs(child, addrp);
639 case PTRACE_SET_WATCH_REGS:
640 ret = ptrace_set_watch_regs(child, addrp);
644 ret = ptrace_request(child, request, addr, data);
652 * Notification of system call entry/exit
653 * - triggered by current->work.syscall_trace
655 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
660 /* do the secure computing check first */
661 secure_computing_strict(regs->regs[2]);
663 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
664 tracehook_report_syscall_entry(regs))
667 audit_syscall_entry(__syscall_get_arch(),
669 regs->regs[4], regs->regs[5],
670 regs->regs[6], regs->regs[7]);
674 * Notification of system call entry/exit
675 * - triggered by current->work.syscall_trace
677 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
680 * We may come here right after calling schedule_user()
681 * or do_notify_resume(), in which case we can be in RCU
686 audit_syscall_exit(regs);
688 if (test_thread_flag(TIF_SYSCALL_TRACE))
689 tracehook_report_syscall_exit(regs, 0);