blob: 6cbd34a27b906cdce1805b710eedca1cd6cfa566 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000-2003, Axis Communications AB.
3 */
4
5#include <linux/kernel.h>
6#include <linux/sched.h>
7#include <linux/mm.h>
8#include <linux/smp.h>
9#include <linux/smp_lock.h>
10#include <linux/errno.h>
11#include <linux/ptrace.h>
12#include <linux/user.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070013#include <linux/signal.h>
Mikael Starvik5d01e6c2005-07-27 11:44:43 -070014#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/uaccess.h>
17#include <asm/page.h>
18#include <asm/pgtable.h>
19#include <asm/system.h>
20#include <asm/processor.h>
21
22/*
23 * Determines which bits in DCCR the user has access to.
24 * 1 = access, 0 = no access.
25 */
26#define DCCR_MASK 0x0000001f /* XNZVC */
27
28/*
29 * Get contents of register REGNO in task TASK.
30 */
31inline long get_reg(struct task_struct *task, unsigned int regno)
32{
33 /* USP is a special case, it's not in the pt_regs struct but
34 * in the tasks thread struct
35 */
36
37 if (regno == PT_USP)
38 return task->thread.usp;
39 else if (regno < PT_MAX)
40 return ((unsigned long *)user_regs(task->thread_info))[regno];
41 else
42 return 0;
43}
44
45/*
46 * Write contents of register REGNO in task TASK.
47 */
48inline int put_reg(struct task_struct *task, unsigned int regno,
49 unsigned long data)
50{
51 if (regno == PT_USP)
52 task->thread.usp = data;
53 else if (regno < PT_MAX)
54 ((unsigned long *)user_regs(task->thread_info))[regno] = data;
55 else
56 return -1;
57 return 0;
58}
59
60/*
61 * Called by kernel/ptrace.c when detaching.
62 *
63 * Make sure the single step bit is not set.
64 */
65void
66ptrace_disable(struct task_struct *child)
67{
68 /* Todo - pending singlesteps? */
69}
70
71/*
72 * Note that this implementation of ptrace behaves differently from vanilla
73 * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
74 * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
75 * ignored. Instead, the data variable is expected to point at a location
76 * (in user space) where the result of the ptrace call is written (instead of
77 * being returned).
78 */
Christoph Hellwig481bed42005-11-07 00:59:47 -080079long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int ret;
82 unsigned long __user *datap = (unsigned long __user *)data;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 switch (request) {
85 /* Read word at location address. */
86 case PTRACE_PEEKTEXT:
87 case PTRACE_PEEKDATA: {
88 unsigned long tmp;
89 int copied;
90
91 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
92 ret = -EIO;
93
94 if (copied != sizeof(tmp))
95 break;
96
97 ret = put_user(tmp,datap);
98 break;
99 }
100
101 /* Read the word at location address in the USER area. */
102 case PTRACE_PEEKUSR: {
103 unsigned long tmp;
104
105 ret = -EIO;
106 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
107 break;
108
109 tmp = get_reg(child, addr >> 2);
110 ret = put_user(tmp, datap);
111 break;
112 }
113
114 /* Write the word at location address. */
115 case PTRACE_POKETEXT:
116 case PTRACE_POKEDATA:
117 ret = 0;
118
119 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
120 break;
121
122 ret = -EIO;
123 break;
124
125 /* Write the word at location address in the USER area. */
126 case PTRACE_POKEUSR:
127 ret = -EIO;
128 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
129 break;
130
131 addr >>= 2;
132
133 if (addr == PT_DCCR) {
134 /* don't allow the tracing process to change stuff like
135 * interrupt enable, kernel/user bit, dma enables etc.
136 */
137 data &= DCCR_MASK;
138 data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
139 }
140 if (put_reg(child, addr, data))
141 break;
142 ret = 0;
143 break;
144
145 case PTRACE_SYSCALL:
146 case PTRACE_CONT:
147 ret = -EIO;
148
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700149 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 break;
151
152 if (request == PTRACE_SYSCALL) {
153 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
154 }
155 else {
156 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
157 }
158
159 child->exit_code = data;
160
161 /* TODO: make sure any pending breakpoint is killed */
162 wake_up_process(child);
163 ret = 0;
164
165 break;
166
167 /* Make the child exit by sending it a sigkill. */
168 case PTRACE_KILL:
169 ret = 0;
170
Mikael Starvik5d01e6c2005-07-27 11:44:43 -0700171 if (child->exit_state == EXIT_ZOMBIE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 break;
173
174 child->exit_code = SIGKILL;
175
176 /* TODO: make sure any pending breakpoint is killed */
177 wake_up_process(child);
178 break;
179
180 /* Set the trap flag. */
181 case PTRACE_SINGLESTEP:
182 ret = -EIO;
183
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700184 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 break;
186
187 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
188
189 /* TODO: set some clever breakpoint mechanism... */
190
191 child->exit_code = data;
192 wake_up_process(child);
193 ret = 0;
194 break;
195
196 case PTRACE_DETACH:
197 ret = ptrace_detach(child, data);
198 break;
199
200 /* Get all GP registers from the child. */
201 case PTRACE_GETREGS: {
202 int i;
203 unsigned long tmp;
204
205 for (i = 0; i <= PT_MAX; i++) {
206 tmp = get_reg(child, i);
207
208 if (put_user(tmp, datap)) {
209 ret = -EFAULT;
210 goto out_tsk;
211 }
212
213 data += sizeof(long);
214 }
215
216 ret = 0;
217 break;
218 }
219
220 /* Set all GP registers in the child. */
221 case PTRACE_SETREGS: {
222 int i;
223 unsigned long tmp;
224
225 for (i = 0; i <= PT_MAX; i++) {
226 if (get_user(tmp, datap)) {
227 ret = -EFAULT;
228 goto out_tsk;
229 }
230
231 if (i == PT_DCCR) {
232 tmp &= DCCR_MASK;
233 tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
234 }
235
236 put_reg(child, i, tmp);
237 data += sizeof(long);
238 }
239
240 ret = 0;
241 break;
242 }
243
244 default:
245 ret = ptrace_request(child, request, addr, data);
246 break;
247 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return ret;
250}
251
252void do_syscall_trace(void)
253{
254 if (!test_thread_flag(TIF_SYSCALL_TRACE))
255 return;
256
257 if (!(current->ptrace & PT_PTRACED))
258 return;
259
260 /* the 0x80 provides a way for the tracing parent to distinguish
261 between a syscall stop and SIGTRAP delivery */
262 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
263 ? 0x80 : 0));
264
265 /*
266 * This isn't the same as continuing with a signal, but it will do for
267 * normal use.
268 */
269 if (current->exit_code) {
270 send_sig(current->exit_code, current, 1);
271 current->exit_code = 0;
272 }
273}