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 * Copyright (C) 2000 Ulf Carlsson
12 * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/user.h>
24 #include <linux/security.h>
28 #include <asm/mipsregs.h>
29 #include <asm/pgtable.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
33 #include <asm/bootinfo.h>
36 * Called by kernel/ptrace.c when detaching..
38 * Make sure single step bits etc are not set.
40 void ptrace_disable(struct task_struct *child)
46 * Tracing a 32-bit process with a 64-bit strace and vice versa will not
47 * work. I don't know how to fix this.
49 asmlinkage int sys32_ptrace(int request, int pid, int addr, int data)
51 struct task_struct *child;
56 if (request == PTRACE_TRACEME) {
57 /* are we already being traced? */
58 if (current->ptrace & PT_PTRACED)
60 if ((ret = security_ptrace(current->parent, current)))
62 /* set the ptrace bit in the process flags. */
63 current->ptrace |= PT_PTRACED;
68 read_lock(&tasklist_lock);
69 child = find_task_by_pid(pid);
71 get_task_struct(child);
72 read_unlock(&tasklist_lock);
77 if (pid == 1) /* you may not mess with init */
80 if (request == PTRACE_ATTACH) {
81 ret = ptrace_attach(child);
85 ret = ptrace_check_attach(child, request == PTRACE_KILL);
90 /* when I and D space are separate, these will need to be fixed. */
91 case PTRACE_PEEKTEXT: /* read word at location addr. */
92 case PTRACE_PEEKDATA: {
96 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
98 if (copied != sizeof(tmp))
100 ret = put_user(tmp, (unsigned int *) (unsigned long) data);
104 /* read the word at location addr in the USER area. */
105 case PTRACE_PEEKUSR: {
106 struct pt_regs *regs;
109 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
110 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
115 tmp = regs->regs[addr];
117 case FPR_BASE ... FPR_BASE + 31:
118 if (child->used_math) {
119 unsigned long long *fregs;
120 fregs = (unsigned long long *)get_fpu_regs(child);
122 * The odd registers are actually the high
123 * order bits of the values stored in the even
124 * registers - unless we're using r2k_switch.S.
127 tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
129 tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
131 tmp = -1; /* FP not yet used */
138 tmp = regs->cp0_cause;
141 tmp = regs->cp0_badvaddr;
151 tmp = child->thread.fpu.hard.control;
153 tmp = child->thread.fpu.soft.sr;
155 case FPC_EIR: { /* implementation / version register */
157 flags = read_c0_status();
159 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
160 write_c0_status(flags);
168 ret = put_user(tmp, (unsigned *) (unsigned long) data);
171 /* when I and D space are separate, this will have to be fixed. */
172 case PTRACE_POKETEXT: /* write the word at location addr. */
173 case PTRACE_POKEDATA:
175 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
180 case PTRACE_POKEUSR: {
181 struct pt_regs *regs;
183 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
184 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
188 regs->regs[addr] = data;
190 case FPR_BASE ... FPR_BASE + 31: {
191 unsigned long long *fregs;
192 fregs = (unsigned long long *)get_fpu_regs(child);
193 if (!child->used_math) {
194 /* FP not yet used */
195 memset(&child->thread.fpu.hard, ~0,
196 sizeof(child->thread.fpu.hard));
197 child->thread.fpu.hard.control = 0;
200 * The odd registers are actually the high order bits
201 * of the values stored in the even registers - unless
202 * we're using r2k_switch.S.
205 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
206 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
208 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
209 /* Must cast, lest sign extension fill upper
211 fregs[addr - FPR_BASE] |= (unsigned int)data;
216 regs->cp0_epc = data;
226 child->thread.fpu.hard.control = data;
228 child->thread.fpu.soft.sr = data;
231 /* The rest are not allowed. */
237 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
238 case PTRACE_CONT: { /* restart after signal. */
240 if ((unsigned int) data > _NSIG)
242 if (request == PTRACE_SYSCALL) {
243 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
246 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
248 child->exit_code = data;
249 wake_up_process(child);
255 * make the child exit. Best I can do is send it a sigkill.
256 * perhaps it should be put in the status that it wants to
260 if (child->state == TASK_ZOMBIE) /* already dead */
262 child->exit_code = SIGKILL;
263 wake_up_process(child);
267 case PTRACE_DETACH: /* detach a process that was attached. */
268 ret = ptrace_detach(child, data);
272 ret = ptrace_request(child, request, addr, data);
277 put_task_struct(child);
283 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
285 struct task_struct *child;
290 printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
291 (int) request, (int) pid, (unsigned long) addr,
292 (unsigned long) data);
295 if (request == PTRACE_TRACEME) {
296 /* are we already being traced? */
297 if (current->ptrace & PT_PTRACED)
299 if ((ret = security_ptrace(current->parent, current)))
301 /* set the ptrace bit in the process flags. */
302 current->ptrace |= PT_PTRACED;
307 read_lock(&tasklist_lock);
308 child = find_task_by_pid(pid);
310 get_task_struct(child);
311 read_unlock(&tasklist_lock);
316 if (pid == 1) /* you may not mess with init */
319 if (request == PTRACE_ATTACH) {
320 ret = ptrace_attach(child);
324 ret = ptrace_check_attach(child, request == PTRACE_KILL);
329 /* when I and D space are separate, these will need to be fixed. */
330 case PTRACE_PEEKTEXT: /* read word at location addr. */
331 case PTRACE_PEEKDATA: {
335 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
337 if (copied != sizeof(tmp))
339 ret = put_user(tmp,(unsigned long *) data);
343 /* read the word at location addr in the USER area. */
344 case PTRACE_PEEKUSR: {
345 struct pt_regs *regs;
348 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
349 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
354 tmp = regs->regs[addr];
356 case FPR_BASE ... FPR_BASE + 31:
357 if (child->used_math) {
358 unsigned long *fregs = get_fpu_regs(child);
359 tmp = fregs[addr - FPR_BASE];
368 tmp = regs->cp0_cause;
371 tmp = regs->cp0_badvaddr;
381 tmp = child->thread.fpu.hard.control;
383 tmp = child->thread.fpu.soft.sr;
385 case FPC_EIR: { /* implementation / version register */
387 flags = read_c0_status();
389 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
390 write_c0_status(flags);
398 ret = put_user(tmp, (unsigned long *) data);
401 /* when I and D space are separate, this will have to be fixed. */
402 case PTRACE_POKETEXT: /* write the word at location addr. */
403 case PTRACE_POKEDATA:
405 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
410 case PTRACE_POKEUSR: {
411 struct pt_regs *regs;
413 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
414 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
418 regs->regs[addr] = data;
420 case FPR_BASE ... FPR_BASE + 31: {
421 unsigned long *fregs = get_fpu_regs(child);
422 if (!child->used_math) {
423 /* FP not yet used */
424 memset(&child->thread.fpu.hard, ~0,
425 sizeof(child->thread.fpu.hard));
426 child->thread.fpu.hard.control = 0;
428 fregs[addr - FPR_BASE] = data;
432 regs->cp0_epc = data;
442 child->thread.fpu.hard.control = data;
444 child->thread.fpu.soft.sr = data;
447 /* The rest are not allowed. */
453 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
454 case PTRACE_CONT: { /* restart after signal. */
456 if ((unsigned long) data > _NSIG)
458 if (request == PTRACE_SYSCALL) {
459 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
462 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
464 child->exit_code = data;
465 wake_up_process(child);
471 * make the child exit. Best I can do is send it a sigkill.
472 * perhaps it should be put in the status that it wants to
476 if (child->state == TASK_ZOMBIE) /* already dead */
478 child->exit_code = SIGKILL;
479 wake_up_process(child);
483 case PTRACE_DETACH: /* detach a process that was attached. */
484 ret = ptrace_detach(child, data);
488 ret = ptrace_request(child, request, addr, data);
493 put_task_struct(child);
499 struct task_work_bf {
501 signed need_resched :8;
502 unsigned syscall_trace; /* count of syscall interceptors */
504 unsigned notify_resume; /* request for notification on
505 userspace execution resumption */
508 unsigned notify_resume : 8; /* request for notification on
511 unsigned sigpending : 8;
512 unsigned syscall_trace : 8; /* count of syscall
514 signed need_resched : 8;
518 asmlinkage void do_syscall_trace(void)
520 if (!test_thread_flag(TIF_SYSCALL_TRACE))
522 if (!(current->ptrace & PT_PTRACED))
525 /* The 0x80 provides a way for the tracing parent to distinguish
526 between a syscall stop and SIGTRAP delivery */
527 current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
530 current->state = TASK_STOPPED;
531 notify_parent(current, SIGCHLD);
535 * this isn't the same as continuing with a signal, but it will do
536 * for normal use. strace only continues with a signal if the
537 * stopping signal is not SIGTRAP. -brl
539 if (current->exit_code) {
540 send_sig(current->exit_code, current, 1);
541 current->exit_code = 0;