blob: bbc3a4a9a0fa521df54024994734bd0112a225dc [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. */
Alexey Dobriyan76647322007-07-17 04:03:43 -070055 case PTRACE_PEEKDATA:
56 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 /* read the word at location addr in the USER area. */
Bodo Stroesser82c1c112005-05-06 21:30:46 -070060 case PTRACE_PEEKUSR:
61 ret = peek_user(child, addr, data);
62 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* when I and D space are separate, this will have to be fixed. */
65 case PTRACE_POKETEXT: /* write the word at location addr. */
66 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -070067 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 break;
69
70 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
Bodo Stroesser82c1c112005-05-06 21:30:46 -070071 ret = poke_user(child, addr, data);
72 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
75 case PTRACE_CONT: { /* restart after signal. */
76 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -070077 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79
Bodo Stroesser82c1c112005-05-06 21:30:46 -070080 set_singlestepping(child, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (request == PTRACE_SYSCALL) {
82 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
83 }
84 else {
85 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
86 }
87 child->exit_code = data;
88 wake_up_process(child);
89 ret = 0;
90 break;
91 }
92
93/*
94 * make the child exit. Best I can do is send it a sigkill.
95 * perhaps it should be put in the status that it wants to
96 * exit.
97 */
98 case PTRACE_KILL: {
99 ret = 0;
100 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
101 break;
102
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700103 set_singlestepping(child, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 child->exit_code = SIGKILL;
105 wake_up_process(child);
106 break;
107 }
108
109 case PTRACE_SINGLESTEP: { /* set the trap flag. */
110 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700111 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 break;
113 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700114 set_singlestepping(child, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 child->exit_code = data;
116 /* give it a chance to run. */
117 wake_up_process(child);
118 ret = 0;
119 break;
120 }
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#ifdef PTRACE_GETREGS
123 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Al Viro4d338e12006-03-31 02:30:15 -0800124 if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 ret = -EIO;
126 break;
127 }
128 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -0800129 __put_user(getreg(child, i), p);
130 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132 ret = 0;
133 break;
134 }
135#endif
136#ifdef PTRACE_SETREGS
137 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
138 unsigned long tmp = 0;
Al Viro4d338e12006-03-31 02:30:15 -0800139 if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 ret = -EIO;
141 break;
142 }
143 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -0800144 __get_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 putreg(child, i, tmp);
Al Viro4d338e12006-03-31 02:30:15 -0800146 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148 ret = 0;
149 break;
150 }
151#endif
152#ifdef PTRACE_GETFPREGS
153 case PTRACE_GETFPREGS: /* Get the child FPU state. */
154 ret = get_fpregs(data, child);
155 break;
156#endif
157#ifdef PTRACE_SETFPREGS
158 case PTRACE_SETFPREGS: /* Set the child FPU state. */
159 ret = set_fpregs(data, child);
160 break;
161#endif
162#ifdef PTRACE_GETFPXREGS
163 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
164 ret = get_fpxregs(data, child);
165 break;
166#endif
167#ifdef PTRACE_SETFPXREGS
168 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
169 ret = set_fpxregs(data, child);
170 break;
171#endif
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800172 case PTRACE_GET_THREAD_AREA:
173 ret = ptrace_get_thread_area(child, addr,
174 (struct user_desc __user *) data);
175 break;
176
177 case PTRACE_SET_THREAD_AREA:
178 ret = ptrace_set_thread_area(child, addr,
179 (struct user_desc __user *) data);
180 break;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 case PTRACE_FAULTINFO: {
Al Viro4d338e12006-03-31 02:30:15 -0800183 /* Take the info from thread->arch->faultinfo,
184 * but transfer max. sizeof(struct ptrace_faultinfo).
185 * On i386, ptrace_faultinfo is smaller!
186 */
187 ret = copy_to_user(p, &child->thread.arch.faultinfo,
188 sizeof(struct ptrace_faultinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if(ret)
190 break;
191 break;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Bodo Stroesserc5784552005-05-05 16:15:31 -0700194#ifdef PTRACE_LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 case PTRACE_LDT: {
196 struct ptrace_ldt ldt;
197
Al Viro4d338e12006-03-31 02:30:15 -0800198 if(copy_from_user(&ldt, p, sizeof(ldt))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 ret = -EIO;
200 break;
201 }
202
203 /* This one is confusing, so just punt and return -EIO for
204 * now
205 */
206 ret = -EIO;
207 break;
208 }
Bodo Stroesserc5784552005-05-05 16:15:31 -0700209#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#ifdef CONFIG_PROC_MM
211 case PTRACE_SWITCH_MM: {
212 struct mm_struct *old = child->mm;
213 struct mm_struct *new = proc_mm_get_mm(data);
214
215 if(IS_ERR(new)){
216 ret = PTR_ERR(new);
217 break;
218 }
219
220 atomic_inc(&new->mm_users);
221 child->mm = new;
222 child->active_mm = new;
223 mmput(old);
224 ret = 0;
225 break;
226 }
227#endif
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800228#ifdef PTRACE_ARCH_PRCTL
229 case PTRACE_ARCH_PRCTL:
230 /* XXX Calls ptrace on the host - needs some SMP thinking */
Jeff Dike77bf4402007-10-16 01:26:58 -0700231 ret = arch_prctl(child, data, (void *) addr);
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800232 break;
233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 default:
235 ret = ptrace_request(child, request, addr, data);
236 break;
237 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return ret;
240}
241
Jeff Dike77bf4402007-10-16 01:26:58 -0700242void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int error_code)
244{
245 struct siginfo info;
246
247 memset(&info, 0, sizeof(info));
248 info.si_signo = SIGTRAP;
249 info.si_code = TRAP_BRKPT;
250
251 /* User-mode eip? */
252 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
253
254 /* Send us the fakey SIGTRAP */
255 force_sig_info(SIGTRAP, &info, tsk);
256}
257
258/* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
259 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
260 */
Jeff Dike77bf4402007-10-16 01:26:58 -0700261void syscall_trace(struct uml_pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
264 int tracesysgood;
265
266 if (unlikely(current->audit_context)) {
267 if (!entryexit)
Al Viro5411be52006-03-29 20:23:36 -0500268 audit_syscall_entry(HOST_AUDIT_ARCH,
Jeff Dike79d20b12005-05-03 07:54:51 +0100269 UPT_SYSCALL_NR(regs),
270 UPT_SYSCALL_ARG1(regs),
271 UPT_SYSCALL_ARG2(regs),
272 UPT_SYSCALL_ARG3(regs),
273 UPT_SYSCALL_ARG4(regs));
Al Viro5411be52006-03-29 20:23:36 -0500274 else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
Jeff Dikeb9e0d062005-05-28 15:51:53 -0700275 UPT_SYSCALL_RET(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
278 /* Fake a debug trap */
279 if (is_singlestep)
280 send_sigtrap(current, regs, 0);
281
282 if (!test_thread_flag(TIF_SYSCALL_TRACE))
283 return;
284
285 if (!(current->ptrace & PT_PTRACED))
286 return;
287
288 /* the 0x80 provides a way for the tracing parent to distinguish
289 between a syscall stop and SIGTRAP delivery */
290 tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
291 ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
292
293 if (entryexit) /* force do_signal() --> is_syscall() */
294 set_thread_flag(TIF_SIGPENDING);
295
296 /* this isn't the same as continuing with a signal, but it will do
297 * for normal use. strace only continues with a signal if the
298 * stopping signal is not SIGTRAP. -brl
299 */
300 if (current->exit_code) {
301 send_sig(current->exit_code, current, 1);
302 current->exit_code = 0;
303 }
304}