2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/sched.h"
8 #include "linux/errno.h"
9 #include "linux/smp_lock.h"
10 #include "linux/security.h"
11 #include "linux/ptrace.h"
12 #include "linux/audit.h"
14 #include "linux/proc_mm.h"
16 #include "asm/ptrace.h"
17 #include "asm/uaccess.h"
18 #include "kern_util.h"
19 #include "skas_ptrace.h"
20 #include "sysdep/ptrace.h"
23 static inline void set_singlestepping(struct task_struct
*child
, int on
)
26 child
->ptrace
|= PT_DTRACE
;
28 child
->ptrace
&= ~PT_DTRACE
;
29 child
->thread
.singlestep_syscall
= 0;
31 #ifdef SUBARCH_SET_SINGLESTEPPING
32 SUBARCH_SET_SINGLESTEPPING(child
, on
);
37 * Called by kernel/ptrace.c when detaching..
39 void ptrace_disable(struct task_struct
*child
)
41 set_singlestepping(child
,0);
44 extern int peek_user(struct task_struct
* child
, long addr
, long data
);
45 extern int poke_user(struct task_struct
* child
, long addr
, long data
);
47 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
50 unsigned long __user
*p
= (void __user
*)(unsigned long)data
;
53 /* when I and D space are separate, these will need to be fixed. */
54 case PTRACE_PEEKTEXT
: /* read word at location addr. */
55 case PTRACE_PEEKDATA
: {
60 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
61 if (copied
!= sizeof(tmp
))
63 ret
= put_user(tmp
, p
);
67 /* read the word at location addr in the USER area. */
69 ret
= peek_user(child
, addr
, data
);
72 /* when I and D space are separate, this will have to be fixed. */
73 case PTRACE_POKETEXT
: /* write the word at location addr. */
76 if (access_process_vm(child
, addr
, &data
, sizeof(data
),
82 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
83 ret
= poke_user(child
, addr
, data
);
86 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
87 case PTRACE_CONT
: { /* restart after signal. */
89 if (!valid_signal(data
))
92 set_singlestepping(child
, 0);
93 if (request
== PTRACE_SYSCALL
) {
94 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
97 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
99 child
->exit_code
= data
;
100 wake_up_process(child
);
106 * make the child exit. Best I can do is send it a sigkill.
107 * perhaps it should be put in the status that it wants to
112 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
115 set_singlestepping(child
, 0);
116 child
->exit_code
= SIGKILL
;
117 wake_up_process(child
);
121 case PTRACE_SINGLESTEP
: { /* set the trap flag. */
123 if (!valid_signal(data
))
125 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
126 set_singlestepping(child
, 1);
127 child
->exit_code
= data
;
128 /* give it a chance to run. */
129 wake_up_process(child
);
135 /* detach a process that was attached. */
136 ret
= ptrace_detach(child
, data
);
139 #ifdef PTRACE_GETREGS
140 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
141 if (!access_ok(VERIFY_WRITE
, p
, MAX_REG_OFFSET
)) {
145 for ( i
= 0; i
< MAX_REG_OFFSET
; i
+= sizeof(long) ) {
146 __put_user(getreg(child
, i
), p
);
153 #ifdef PTRACE_SETREGS
154 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
155 unsigned long tmp
= 0;
156 if (!access_ok(VERIFY_READ
, p
, MAX_REG_OFFSET
)) {
160 for ( i
= 0; i
< MAX_REG_OFFSET
; i
+= sizeof(long) ) {
162 putreg(child
, i
, tmp
);
169 #ifdef PTRACE_GETFPREGS
170 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
171 ret
= get_fpregs(data
, child
);
174 #ifdef PTRACE_SETFPREGS
175 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
176 ret
= set_fpregs(data
, child
);
179 #ifdef PTRACE_GETFPXREGS
180 case PTRACE_GETFPXREGS
: /* Get the child FPU state. */
181 ret
= get_fpxregs(data
, child
);
184 #ifdef PTRACE_SETFPXREGS
185 case PTRACE_SETFPXREGS
: /* Set the child FPU state. */
186 ret
= set_fpxregs(data
, child
);
189 case PTRACE_GET_THREAD_AREA
:
190 ret
= ptrace_get_thread_area(child
, addr
,
191 (struct user_desc __user
*) data
);
194 case PTRACE_SET_THREAD_AREA
:
195 ret
= ptrace_set_thread_area(child
, addr
,
196 (struct user_desc __user
*) data
);
199 case PTRACE_FAULTINFO
: {
200 /* Take the info from thread->arch->faultinfo,
201 * but transfer max. sizeof(struct ptrace_faultinfo).
202 * On i386, ptrace_faultinfo is smaller!
204 ret
= copy_to_user(p
, &child
->thread
.arch
.faultinfo
,
205 sizeof(struct ptrace_faultinfo
));
213 struct ptrace_ldt ldt
;
215 if(copy_from_user(&ldt
, p
, sizeof(ldt
))){
220 /* This one is confusing, so just punt and return -EIO for
227 #ifdef CONFIG_PROC_MM
228 case PTRACE_SWITCH_MM
: {
229 struct mm_struct
*old
= child
->mm
;
230 struct mm_struct
*new = proc_mm_get_mm(data
);
237 atomic_inc(&new->mm_users
);
239 child
->active_mm
= new;
245 #ifdef PTRACE_ARCH_PRCTL
246 case PTRACE_ARCH_PRCTL
:
247 /* XXX Calls ptrace on the host - needs some SMP thinking */
248 ret
= arch_prctl_skas(child
, data
, (void *) addr
);
252 ret
= ptrace_request(child
, request
, addr
, data
);
259 void send_sigtrap(struct task_struct
*tsk
, union uml_pt_regs
*regs
,
264 memset(&info
, 0, sizeof(info
));
265 info
.si_signo
= SIGTRAP
;
266 info
.si_code
= TRAP_BRKPT
;
269 info
.si_addr
= UPT_IS_USER(regs
) ? (void __user
*) UPT_IP(regs
) : NULL
;
271 /* Send us the fakey SIGTRAP */
272 force_sig_info(SIGTRAP
, &info
, tsk
);
275 /* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
276 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
278 void syscall_trace(union uml_pt_regs
*regs
, int entryexit
)
280 int is_singlestep
= (current
->ptrace
& PT_DTRACE
) && entryexit
;
283 if (unlikely(current
->audit_context
)) {
285 audit_syscall_entry(HOST_AUDIT_ARCH
,
286 UPT_SYSCALL_NR(regs
),
287 UPT_SYSCALL_ARG1(regs
),
288 UPT_SYSCALL_ARG2(regs
),
289 UPT_SYSCALL_ARG3(regs
),
290 UPT_SYSCALL_ARG4(regs
));
291 else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs
)),
292 UPT_SYSCALL_RET(regs
));
295 /* Fake a debug trap */
297 send_sigtrap(current
, regs
, 0);
299 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
302 if (!(current
->ptrace
& PT_PTRACED
))
305 /* the 0x80 provides a way for the tracing parent to distinguish
306 between a syscall stop and SIGTRAP delivery */
307 tracesysgood
= (current
->ptrace
& PT_TRACESYSGOOD
);
308 ptrace_notify(SIGTRAP
| (tracesysgood
? 0x80 : 0));
310 if (entryexit
) /* force do_signal() --> is_syscall() */
311 set_thread_flag(TIF_SIGPENDING
);
313 /* this isn't the same as continuing with a signal, but it will do
314 * for normal use. strace only continues with a signal if the
315 * stopping signal is not SIGTRAP. -brl
317 if (current
->exit_code
) {
318 send_sig(current
->exit_code
, current
, 1);
319 current
->exit_code
= 0;