blob: 627742d894347c7ca6eb51f989b1e4c259de0909 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include "linux/sched.h"
7#include "linux/mm.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"
13#ifdef CONFIG_PROC_MM
14#include "linux/proc_mm.h"
15#endif
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"
Jeff Dike6e6d74c2007-02-10 01:44:30 -080021#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Bodo Stroesser82c1c112005-05-06 21:30:46 -070023static inline void set_singlestepping(struct task_struct *child, int on)
24{
25 if (on)
26 child->ptrace |= PT_DTRACE;
27 else
28 child->ptrace &= ~PT_DTRACE;
29 child->thread.singlestep_syscall = 0;
30
31#ifdef SUBARCH_SET_SINGLESTEPPING
Jeff Dikeba9950c2005-05-20 13:59:07 -070032 SUBARCH_SET_SINGLESTEPPING(child, on);
Bodo Stroesser82c1c112005-05-06 21:30:46 -070033#endif
Jeff Dikeba9950c2005-05-20 13:59:07 -070034}
Bodo Stroesser82c1c112005-05-06 21:30:46 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * Called by kernel/ptrace.c when detaching..
38 */
39void ptrace_disable(struct task_struct *child)
40{
Bodo Stroesser82c1c112005-05-06 21:30:46 -070041 set_singlestepping(child,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Bodo Stroesser82c1c112005-05-06 21:30:46 -070044extern int peek_user(struct task_struct * child, long addr, long data);
45extern int poke_user(struct task_struct * child, long addr, long data);
46
Christoph Hellwig481bed42005-11-07 00:59:47 -080047long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int i, ret;
Al Viro4d338e12006-03-31 02:30:15 -080050 unsigned long __user *p = (void __user *)(unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (request) {
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: {
56 unsigned long tmp;
57 int copied;
58
59 ret = -EIO;
60 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
61 if (copied != sizeof(tmp))
62 break;
Al Viro4d338e12006-03-31 02:30:15 -080063 ret = put_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 break;
65 }
66
67 /* read the word at location addr in the USER area. */
Bodo Stroesser82c1c112005-05-06 21:30:46 -070068 case PTRACE_PEEKUSR:
69 ret = peek_user(child, addr, data);
70 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /* when I and D space are separate, this will have to be fixed. */
73 case PTRACE_POKETEXT: /* write the word at location addr. */
74 case PTRACE_POKEDATA:
75 ret = -EIO;
76 if (access_process_vm(child, addr, &data, sizeof(data),
77 1) != sizeof(data))
78 break;
79 ret = 0;
80 break;
81
82 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
Bodo Stroesser82c1c112005-05-06 21:30:46 -070083 ret = poke_user(child, addr, data);
84 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
87 case PTRACE_CONT: { /* restart after signal. */
88 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -070089 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 break;
91
Bodo Stroesser82c1c112005-05-06 21:30:46 -070092 set_singlestepping(child, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (request == PTRACE_SYSCALL) {
94 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
95 }
96 else {
97 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
98 }
99 child->exit_code = data;
100 wake_up_process(child);
101 ret = 0;
102 break;
103 }
104
105/*
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
108 * exit.
109 */
110 case PTRACE_KILL: {
111 ret = 0;
112 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
113 break;
114
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700115 set_singlestepping(child, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 child->exit_code = SIGKILL;
117 wake_up_process(child);
118 break;
119 }
120
121 case PTRACE_SINGLESTEP: { /* set the trap flag. */
122 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700123 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 break;
125 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700126 set_singlestepping(child, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 child->exit_code = data;
128 /* give it a chance to run. */
129 wake_up_process(child);
130 ret = 0;
131 break;
132 }
133
134 case PTRACE_DETACH:
135 /* detach a process that was attached. */
136 ret = ptrace_detach(child, data);
137 break;
138
139#ifdef PTRACE_GETREGS
140 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Al Viro4d338e12006-03-31 02:30:15 -0800141 if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 ret = -EIO;
143 break;
144 }
145 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -0800146 __put_user(getreg(child, i), p);
147 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149 ret = 0;
150 break;
151 }
152#endif
153#ifdef PTRACE_SETREGS
154 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
155 unsigned long tmp = 0;
Al Viro4d338e12006-03-31 02:30:15 -0800156 if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 ret = -EIO;
158 break;
159 }
160 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -0800161 __get_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 putreg(child, i, tmp);
Al Viro4d338e12006-03-31 02:30:15 -0800163 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 ret = 0;
166 break;
167 }
168#endif
169#ifdef PTRACE_GETFPREGS
170 case PTRACE_GETFPREGS: /* Get the child FPU state. */
171 ret = get_fpregs(data, child);
172 break;
173#endif
174#ifdef PTRACE_SETFPREGS
175 case PTRACE_SETFPREGS: /* Set the child FPU state. */
176 ret = set_fpregs(data, child);
177 break;
178#endif
179#ifdef PTRACE_GETFPXREGS
180 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
181 ret = get_fpxregs(data, child);
182 break;
183#endif
184#ifdef PTRACE_SETFPXREGS
185 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
186 ret = set_fpxregs(data, child);
187 break;
188#endif
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800189 case PTRACE_GET_THREAD_AREA:
190 ret = ptrace_get_thread_area(child, addr,
191 (struct user_desc __user *) data);
192 break;
193
194 case PTRACE_SET_THREAD_AREA:
195 ret = ptrace_set_thread_area(child, addr,
196 (struct user_desc __user *) data);
197 break;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 case PTRACE_FAULTINFO: {
Al Viro4d338e12006-03-31 02:30:15 -0800200 /* Take the info from thread->arch->faultinfo,
201 * but transfer max. sizeof(struct ptrace_faultinfo).
202 * On i386, ptrace_faultinfo is smaller!
203 */
204 ret = copy_to_user(p, &child->thread.arch.faultinfo,
205 sizeof(struct ptrace_faultinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if(ret)
207 break;
208 break;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Bodo Stroesserc5784552005-05-05 16:15:31 -0700211#ifdef PTRACE_LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 case PTRACE_LDT: {
213 struct ptrace_ldt ldt;
214
Al Viro4d338e12006-03-31 02:30:15 -0800215 if(copy_from_user(&ldt, p, sizeof(ldt))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 ret = -EIO;
217 break;
218 }
219
220 /* This one is confusing, so just punt and return -EIO for
221 * now
222 */
223 ret = -EIO;
224 break;
225 }
Bodo Stroesserc5784552005-05-05 16:15:31 -0700226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#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);
231
232 if(IS_ERR(new)){
233 ret = PTR_ERR(new);
234 break;
235 }
236
237 atomic_inc(&new->mm_users);
238 child->mm = new;
239 child->active_mm = new;
240 mmput(old);
241 ret = 0;
242 break;
243 }
244#endif
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800245#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);
249 break;
250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 default:
252 ret = ptrace_request(child, request, addr, data);
253 break;
254 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return ret;
257}
258
259void send_sigtrap(struct task_struct *tsk, union uml_pt_regs *regs,
260 int error_code)
261{
262 struct siginfo info;
263
264 memset(&info, 0, sizeof(info));
265 info.si_signo = SIGTRAP;
266 info.si_code = TRAP_BRKPT;
267
268 /* User-mode eip? */
269 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
270
271 /* Send us the fakey SIGTRAP */
272 force_sig_info(SIGTRAP, &info, tsk);
273}
274
275/* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
276 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
277 */
278void syscall_trace(union uml_pt_regs *regs, int entryexit)
279{
280 int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
281 int tracesysgood;
282
283 if (unlikely(current->audit_context)) {
284 if (!entryexit)
Al Viro5411be52006-03-29 20:23:36 -0500285 audit_syscall_entry(HOST_AUDIT_ARCH,
Jeff Dike79d20b12005-05-03 07:54:51 +0100286 UPT_SYSCALL_NR(regs),
287 UPT_SYSCALL_ARG1(regs),
288 UPT_SYSCALL_ARG2(regs),
289 UPT_SYSCALL_ARG3(regs),
290 UPT_SYSCALL_ARG4(regs));
Al Viro5411be52006-03-29 20:23:36 -0500291 else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
Jeff Dikeb9e0d062005-05-28 15:51:53 -0700292 UPT_SYSCALL_RET(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
295 /* Fake a debug trap */
296 if (is_singlestep)
297 send_sigtrap(current, regs, 0);
298
299 if (!test_thread_flag(TIF_SYSCALL_TRACE))
300 return;
301
302 if (!(current->ptrace & PT_PTRACED))
303 return;
304
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));
309
310 if (entryexit) /* force do_signal() --> is_syscall() */
311 set_thread_flag(TIF_SIGPENDING);
312
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
316 */
317 if (current->exit_code) {
318 send_sig(current->exit_code, current, 1);
319 current->exit_code = 0;
320 }
321}