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>
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>
46 #define CREATE_TRACE_POINTS
47 #include <trace/events/syscalls.h>
50 * Called by kernel/ptrace.c when detaching..
52 * Make sure single step bits etc are not set.
54 void ptrace_disable(struct task_struct
*child
)
56 /* Don't load the watchpoint registers for the ex-child. */
57 clear_tsk_thread_flag(child
, TIF_LOAD_WATCH
);
61 * Read a general register set. We always use the 64-bit format, even
62 * for 32-bit kernels and for 32-bit processes on a 64-bit kernel.
63 * Registers are sign extended to fill the available space.
65 int ptrace_getregs(struct task_struct
*child
, struct user_pt_regs __user
*data
)
70 if (!access_ok(VERIFY_WRITE
, data
, 38 * 8))
73 regs
= task_pt_regs(child
);
75 for (i
= 0; i
< 32; i
++)
76 __put_user((long)regs
->regs
[i
], (__s64 __user
*)&data
->regs
[i
]);
77 __put_user((long)regs
->lo
, (__s64 __user
*)&data
->lo
);
78 __put_user((long)regs
->hi
, (__s64 __user
*)&data
->hi
);
79 __put_user((long)regs
->cp0_epc
, (__s64 __user
*)&data
->cp0_epc
);
80 __put_user((long)regs
->cp0_badvaddr
, (__s64 __user
*)&data
->cp0_badvaddr
);
81 __put_user((long)regs
->cp0_status
, (__s64 __user
*)&data
->cp0_status
);
82 __put_user((long)regs
->cp0_cause
, (__s64 __user
*)&data
->cp0_cause
);
88 * Write a general register set. As for PTRACE_GETREGS, we always use
89 * the 64-bit format. On a 32-bit kernel only the lower order half
90 * (according to endianness) will be used.
92 int ptrace_setregs(struct task_struct
*child
, struct user_pt_regs __user
*data
)
97 if (!access_ok(VERIFY_READ
, data
, 38 * 8))
100 regs
= task_pt_regs(child
);
102 for (i
= 0; i
< 32; i
++)
103 __get_user(regs
->regs
[i
], (__s64 __user
*)&data
->regs
[i
]);
104 __get_user(regs
->lo
, (__s64 __user
*)&data
->lo
);
105 __get_user(regs
->hi
, (__s64 __user
*)&data
->hi
);
106 __get_user(regs
->cp0_epc
, (__s64 __user
*)&data
->cp0_epc
);
108 /* badvaddr, status, and cause may not be written. */
113 int ptrace_getfpregs(struct task_struct
*child
, __u32 __user
*data
)
117 if (!access_ok(VERIFY_WRITE
, data
, 33 * 8))
120 if (tsk_used_math(child
)) {
121 union fpureg
*fregs
= get_fpu_regs(child
);
122 for (i
= 0; i
< 32; i
++)
123 __put_user(get_fpr64(&fregs
[i
], 0),
124 i
+ (__u64 __user
*)data
);
126 for (i
= 0; i
< 32; i
++)
127 __put_user((__u64
) -1, i
+ (__u64 __user
*) data
);
130 __put_user(child
->thread
.fpu
.fcr31
, data
+ 64);
131 __put_user(boot_cpu_data
.fpu_id
, data
+ 65);
136 int ptrace_setfpregs(struct task_struct
*child
, __u32 __user
*data
)
142 if (!access_ok(VERIFY_READ
, data
, 33 * 8))
145 fregs
= get_fpu_regs(child
);
147 for (i
= 0; i
< 32; i
++) {
148 __get_user(fpr_val
, i
+ (__u64 __user
*)data
);
149 set_fpr64(&fregs
[i
], 0, fpr_val
);
152 __get_user(child
->thread
.fpu
.fcr31
, data
+ 64);
153 child
->thread
.fpu
.fcr31
&= ~FPU_CSR_ALL_X
;
155 /* FIR may not be written. */
160 int ptrace_get_watch_regs(struct task_struct
*child
,
161 struct pt_watch_regs __user
*addr
)
163 enum pt_watch_style style
;
166 if (!cpu_has_watch
|| boot_cpu_data
.watch_reg_use_cnt
== 0)
168 if (!access_ok(VERIFY_WRITE
, addr
, sizeof(struct pt_watch_regs
)))
172 style
= pt_watch_style_mips32
;
173 #define WATCH_STYLE mips32
175 style
= pt_watch_style_mips64
;
176 #define WATCH_STYLE mips64
179 __put_user(style
, &addr
->style
);
180 __put_user(boot_cpu_data
.watch_reg_use_cnt
,
181 &addr
->WATCH_STYLE
.num_valid
);
182 for (i
= 0; i
< boot_cpu_data
.watch_reg_use_cnt
; i
++) {
183 __put_user(child
->thread
.watch
.mips3264
.watchlo
[i
],
184 &addr
->WATCH_STYLE
.watchlo
[i
]);
185 __put_user(child
->thread
.watch
.mips3264
.watchhi
[i
] & 0xfff,
186 &addr
->WATCH_STYLE
.watchhi
[i
]);
187 __put_user(boot_cpu_data
.watch_reg_masks
[i
],
188 &addr
->WATCH_STYLE
.watch_masks
[i
]);
191 __put_user(0, &addr
->WATCH_STYLE
.watchlo
[i
]);
192 __put_user(0, &addr
->WATCH_STYLE
.watchhi
[i
]);
193 __put_user(0, &addr
->WATCH_STYLE
.watch_masks
[i
]);
199 int ptrace_set_watch_regs(struct task_struct
*child
,
200 struct pt_watch_regs __user
*addr
)
203 int watch_active
= 0;
204 unsigned long lt
[NUM_WATCH_REGS
];
205 u16 ht
[NUM_WATCH_REGS
];
207 if (!cpu_has_watch
|| boot_cpu_data
.watch_reg_use_cnt
== 0)
209 if (!access_ok(VERIFY_READ
, addr
, sizeof(struct pt_watch_regs
)))
211 /* Check the values. */
212 for (i
= 0; i
< boot_cpu_data
.watch_reg_use_cnt
; i
++) {
213 __get_user(lt
[i
], &addr
->WATCH_STYLE
.watchlo
[i
]);
215 if (lt
[i
] & __UA_LIMIT
)
218 if (test_tsk_thread_flag(child
, TIF_32BIT_ADDR
)) {
219 if (lt
[i
] & 0xffffffff80000000UL
)
222 if (lt
[i
] & __UA_LIMIT
)
226 __get_user(ht
[i
], &addr
->WATCH_STYLE
.watchhi
[i
]);
231 for (i
= 0; i
< boot_cpu_data
.watch_reg_use_cnt
; i
++) {
234 child
->thread
.watch
.mips3264
.watchlo
[i
] = lt
[i
];
236 child
->thread
.watch
.mips3264
.watchhi
[i
] = ht
[i
];
240 set_tsk_thread_flag(child
, TIF_LOAD_WATCH
);
242 clear_tsk_thread_flag(child
, TIF_LOAD_WATCH
);
247 /* regset get/set implementations */
249 #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
251 static int gpr32_get(struct task_struct
*target
,
252 const struct user_regset
*regset
,
253 unsigned int pos
, unsigned int count
,
254 void *kbuf
, void __user
*ubuf
)
256 struct pt_regs
*regs
= task_pt_regs(target
);
257 u32 uregs
[ELF_NGREG
] = {};
260 for (i
= MIPS32_EF_R1
; i
<= MIPS32_EF_R31
; i
++) {
261 /* k0/k1 are copied as zero. */
262 if (i
== MIPS32_EF_R26
|| i
== MIPS32_EF_R27
)
265 uregs
[i
] = regs
->regs
[i
- MIPS32_EF_R0
];
268 uregs
[MIPS32_EF_LO
] = regs
->lo
;
269 uregs
[MIPS32_EF_HI
] = regs
->hi
;
270 uregs
[MIPS32_EF_CP0_EPC
] = regs
->cp0_epc
;
271 uregs
[MIPS32_EF_CP0_BADVADDR
] = regs
->cp0_badvaddr
;
272 uregs
[MIPS32_EF_CP0_STATUS
] = regs
->cp0_status
;
273 uregs
[MIPS32_EF_CP0_CAUSE
] = regs
->cp0_cause
;
275 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
, uregs
, 0,
279 static int gpr32_set(struct task_struct
*target
,
280 const struct user_regset
*regset
,
281 unsigned int pos
, unsigned int count
,
282 const void *kbuf
, const void __user
*ubuf
)
284 struct pt_regs
*regs
= task_pt_regs(target
);
285 u32 uregs
[ELF_NGREG
];
286 unsigned start
, num_regs
, i
;
289 start
= pos
/ sizeof(u32
);
290 num_regs
= count
/ sizeof(u32
);
292 if (start
+ num_regs
> ELF_NGREG
)
295 err
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, uregs
, 0,
300 for (i
= start
; i
< num_regs
; i
++) {
302 * Cast all values to signed here so that if this is a 64-bit
303 * kernel, the supplied 32-bit values will be sign extended.
306 case MIPS32_EF_R1
... MIPS32_EF_R25
:
307 /* k0/k1 are ignored. */
308 case MIPS32_EF_R28
... MIPS32_EF_R31
:
309 regs
->regs
[i
- MIPS32_EF_R0
] = (s32
)uregs
[i
];
312 regs
->lo
= (s32
)uregs
[i
];
315 regs
->hi
= (s32
)uregs
[i
];
317 case MIPS32_EF_CP0_EPC
:
318 regs
->cp0_epc
= (s32
)uregs
[i
];
326 #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
330 static int gpr64_get(struct task_struct
*target
,
331 const struct user_regset
*regset
,
332 unsigned int pos
, unsigned int count
,
333 void *kbuf
, void __user
*ubuf
)
335 struct pt_regs
*regs
= task_pt_regs(target
);
336 u64 uregs
[ELF_NGREG
] = {};
339 for (i
= MIPS64_EF_R1
; i
<= MIPS64_EF_R31
; i
++) {
340 /* k0/k1 are copied as zero. */
341 if (i
== MIPS64_EF_R26
|| i
== MIPS64_EF_R27
)
344 uregs
[i
] = regs
->regs
[i
- MIPS64_EF_R0
];
347 uregs
[MIPS64_EF_LO
] = regs
->lo
;
348 uregs
[MIPS64_EF_HI
] = regs
->hi
;
349 uregs
[MIPS64_EF_CP0_EPC
] = regs
->cp0_epc
;
350 uregs
[MIPS64_EF_CP0_BADVADDR
] = regs
->cp0_badvaddr
;
351 uregs
[MIPS64_EF_CP0_STATUS
] = regs
->cp0_status
;
352 uregs
[MIPS64_EF_CP0_CAUSE
] = regs
->cp0_cause
;
354 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
, uregs
, 0,
358 static int gpr64_set(struct task_struct
*target
,
359 const struct user_regset
*regset
,
360 unsigned int pos
, unsigned int count
,
361 const void *kbuf
, const void __user
*ubuf
)
363 struct pt_regs
*regs
= task_pt_regs(target
);
364 u64 uregs
[ELF_NGREG
];
365 unsigned start
, num_regs
, i
;
368 start
= pos
/ sizeof(u64
);
369 num_regs
= count
/ sizeof(u64
);
371 if (start
+ num_regs
> ELF_NGREG
)
374 err
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, uregs
, 0,
379 for (i
= start
; i
< num_regs
; i
++) {
381 case MIPS64_EF_R1
... MIPS64_EF_R25
:
382 /* k0/k1 are ignored. */
383 case MIPS64_EF_R28
... MIPS64_EF_R31
:
384 regs
->regs
[i
- MIPS64_EF_R0
] = uregs
[i
];
392 case MIPS64_EF_CP0_EPC
:
393 regs
->cp0_epc
= uregs
[i
];
401 #endif /* CONFIG_64BIT */
403 static int fpr_get(struct task_struct
*target
,
404 const struct user_regset
*regset
,
405 unsigned int pos
, unsigned int count
,
406 void *kbuf
, void __user
*ubuf
)
414 if (sizeof(target
->thread
.fpu
.fpr
[i
]) == sizeof(elf_fpreg_t
))
415 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
417 0, sizeof(elf_fpregset_t
));
419 for (i
= 0; i
< NUM_FPU_REGS
; i
++) {
420 fpr_val
= get_fpr64(&target
->thread
.fpu
.fpr
[i
], 0);
421 err
= user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
422 &fpr_val
, i
* sizeof(elf_fpreg_t
),
423 (i
+ 1) * sizeof(elf_fpreg_t
));
431 static int fpr_set(struct task_struct
*target
,
432 const struct user_regset
*regset
,
433 unsigned int pos
, unsigned int count
,
434 const void *kbuf
, const void __user
*ubuf
)
442 if (sizeof(target
->thread
.fpu
.fpr
[i
]) == sizeof(elf_fpreg_t
))
443 return user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
445 0, sizeof(elf_fpregset_t
));
447 for (i
= 0; i
< NUM_FPU_REGS
; i
++) {
448 err
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
449 &fpr_val
, i
* sizeof(elf_fpreg_t
),
450 (i
+ 1) * sizeof(elf_fpreg_t
));
453 set_fpr64(&target
->thread
.fpu
.fpr
[i
], 0, fpr_val
);
464 #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
466 static const struct user_regset mips_regsets
[] = {
468 .core_note_type
= NT_PRSTATUS
,
470 .size
= sizeof(unsigned int),
471 .align
= sizeof(unsigned int),
476 .core_note_type
= NT_PRFPREG
,
478 .size
= sizeof(elf_fpreg_t
),
479 .align
= sizeof(elf_fpreg_t
),
485 static const struct user_regset_view user_mips_view
= {
487 .e_machine
= ELF_ARCH
,
488 .ei_osabi
= ELF_OSABI
,
489 .regsets
= mips_regsets
,
490 .n
= ARRAY_SIZE(mips_regsets
),
493 #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
497 static const struct user_regset mips64_regsets
[] = {
499 .core_note_type
= NT_PRSTATUS
,
501 .size
= sizeof(unsigned long),
502 .align
= sizeof(unsigned long),
507 .core_note_type
= NT_PRFPREG
,
509 .size
= sizeof(elf_fpreg_t
),
510 .align
= sizeof(elf_fpreg_t
),
516 static const struct user_regset_view user_mips64_view
= {
518 .e_machine
= ELF_ARCH
,
519 .ei_osabi
= ELF_OSABI
,
520 .regsets
= mips64_regsets
,
521 .n
= ARRAY_SIZE(mips64_regsets
),
524 #endif /* CONFIG_64BIT */
526 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
529 return &user_mips_view
;
531 #ifdef CONFIG_MIPS32_O32
532 if (test_tsk_thread_flag(task
, TIF_32BIT_REGS
))
533 return &user_mips_view
;
535 return &user_mips64_view
;
539 long arch_ptrace(struct task_struct
*child
, long request
,
540 unsigned long addr
, unsigned long data
)
543 void __user
*addrp
= (void __user
*) addr
;
544 void __user
*datavp
= (void __user
*) data
;
545 unsigned long __user
*datalp
= (void __user
*) data
;
548 /* when I and D space are separate, these will need to be fixed. */
549 case PTRACE_PEEKTEXT
: /* read word at location addr. */
550 case PTRACE_PEEKDATA
:
551 ret
= generic_ptrace_peekdata(child
, addr
, data
);
554 /* Read the word at location addr in the USER area. */
555 case PTRACE_PEEKUSR
: {
556 struct pt_regs
*regs
;
558 unsigned long tmp
= 0;
560 regs
= task_pt_regs(child
);
561 ret
= 0; /* Default return value. */
565 tmp
= regs
->regs
[addr
];
567 case FPR_BASE
... FPR_BASE
+ 31:
568 if (!tsk_used_math(child
)) {
569 /* FP not yet used */
573 fregs
= get_fpu_regs(child
);
576 if (test_thread_flag(TIF_32BIT_FPREGS
)) {
578 * The odd registers are actually the high
579 * order bits of the values stored in the even
580 * registers - unless we're using r2k_switch.S.
582 tmp
= get_fpr32(&fregs
[(addr
& ~1) - FPR_BASE
],
587 tmp
= get_fpr32(&fregs
[addr
- FPR_BASE
], 0);
593 tmp
= regs
->cp0_cause
;
596 tmp
= regs
->cp0_badvaddr
;
604 #ifdef CONFIG_CPU_HAS_SMARTMIPS
610 tmp
= child
->thread
.fpu
.fcr31
;
613 /* implementation / version register */
614 tmp
= boot_cpu_data
.fpu_id
;
616 case DSP_BASE
... DSP_BASE
+ 5: {
624 dregs
= __get_dsp_regs(child
);
625 tmp
= (unsigned long) (dregs
[addr
- DSP_BASE
]);
634 tmp
= child
->thread
.dsp
.dspcontrol
;
641 ret
= put_user(tmp
, datalp
);
645 /* when I and D space are separate, this will have to be fixed. */
646 case PTRACE_POKETEXT
: /* write the word at location addr. */
647 case PTRACE_POKEDATA
:
648 ret
= generic_ptrace_pokedata(child
, addr
, data
);
651 case PTRACE_POKEUSR
: {
652 struct pt_regs
*regs
;
654 regs
= task_pt_regs(child
);
658 regs
->regs
[addr
] = data
;
660 case FPR_BASE
... FPR_BASE
+ 31: {
661 union fpureg
*fregs
= get_fpu_regs(child
);
663 if (!tsk_used_math(child
)) {
664 /* FP not yet used */
665 memset(&child
->thread
.fpu
, ~0,
666 sizeof(child
->thread
.fpu
));
667 child
->thread
.fpu
.fcr31
= 0;
670 if (test_thread_flag(TIF_32BIT_FPREGS
)) {
672 * The odd registers are actually the high
673 * order bits of the values stored in the even
674 * registers - unless we're using r2k_switch.S.
676 set_fpr32(&fregs
[(addr
& ~1) - FPR_BASE
],
681 set_fpr64(&fregs
[addr
- FPR_BASE
], 0, data
);
685 regs
->cp0_epc
= data
;
693 #ifdef CONFIG_CPU_HAS_SMARTMIPS
699 child
->thread
.fpu
.fcr31
= data
& ~FPU_CSR_ALL_X
;
701 case DSP_BASE
... DSP_BASE
+ 5: {
709 dregs
= __get_dsp_regs(child
);
710 dregs
[addr
- DSP_BASE
] = data
;
718 child
->thread
.dsp
.dspcontrol
= data
;
721 /* The rest are not allowed. */
729 ret
= ptrace_getregs(child
, datavp
);
733 ret
= ptrace_setregs(child
, datavp
);
736 case PTRACE_GETFPREGS
:
737 ret
= ptrace_getfpregs(child
, datavp
);
740 case PTRACE_SETFPREGS
:
741 ret
= ptrace_setfpregs(child
, datavp
);
744 case PTRACE_GET_THREAD_AREA
:
745 ret
= put_user(task_thread_info(child
)->tp_value
, datalp
);
748 case PTRACE_GET_WATCH_REGS
:
749 ret
= ptrace_get_watch_regs(child
, addrp
);
752 case PTRACE_SET_WATCH_REGS
:
753 ret
= ptrace_set_watch_regs(child
, addrp
);
757 ret
= ptrace_request(child
, request
, addr
, data
);
765 * Notification of system call entry/exit
766 * - triggered by current->work.syscall_trace
768 asmlinkage
long syscall_trace_enter(struct pt_regs
*regs
, long syscall
)
773 if (secure_computing(syscall
) == -1)
776 if (test_thread_flag(TIF_SYSCALL_TRACE
) &&
777 tracehook_report_syscall_entry(regs
))
780 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
781 trace_sys_enter(regs
, regs
->regs
[2]);
783 audit_syscall_entry(syscall_get_arch(),
785 regs
->regs
[4], regs
->regs
[5],
786 regs
->regs
[6], regs
->regs
[7]);
791 * Notification of system call entry/exit
792 * - triggered by current->work.syscall_trace
794 asmlinkage
void syscall_trace_leave(struct pt_regs
*regs
)
797 * We may come here right after calling schedule_user()
798 * or do_notify_resume(), in which case we can be in RCU
803 audit_syscall_exit(regs
);
805 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
806 trace_sys_exit(regs
, regs
->regs
[2]);
808 if (test_thread_flag(TIF_SYSCALL_TRACE
))
809 tracehook_report_syscall_exit(regs
, 0);