blob: 74b1b4dc822527a5f901d899233bc5f187735fe9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/errno.h>
10#include <linux/ptrace.h>
11#include <linux/user.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070012#include <linux/signal.h>
Mikael Starvik5d01e6c2005-07-27 11:44:43 -070013#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/uaccess.h>
16#include <asm/page.h>
17#include <asm/pgtable.h>
18#include <asm/system.h>
19#include <asm/processor.h>
20
21/*
22 * Determines which bits in DCCR the user has access to.
23 * 1 = access, 0 = no access.
24 */
25#define DCCR_MASK 0x0000001f /* XNZVC */
26
27/*
28 * Get contents of register REGNO in task TASK.
29 */
30inline long get_reg(struct task_struct *task, unsigned int regno)
31{
32 /* USP is a special case, it's not in the pt_regs struct but
33 * in the tasks thread struct
34 */
35
36 if (regno == PT_USP)
37 return task->thread.usp;
38 else if (regno < PT_MAX)
Al Viro95ca0dc2006-01-12 01:06:03 -080039 return ((unsigned long *)task_pt_regs(task))[regno];
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 else
41 return 0;
42}
43
44/*
45 * Write contents of register REGNO in task TASK.
46 */
47inline int put_reg(struct task_struct *task, unsigned int regno,
48 unsigned long data)
49{
50 if (regno == PT_USP)
51 task->thread.usp = data;
52 else if (regno < PT_MAX)
Al Viro95ca0dc2006-01-12 01:06:03 -080053 ((unsigned long *)task_pt_regs(task))[regno] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 else
55 return -1;
56 return 0;
57}
58
59/*
60 * Called by kernel/ptrace.c when detaching.
61 *
62 * Make sure the single step bit is not set.
63 */
64void
65ptrace_disable(struct task_struct *child)
66{
67 /* Todo - pending singlesteps? */
68}
69
70/*
71 * Note that this implementation of ptrace behaves differently from vanilla
72 * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
73 * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
74 * ignored. Instead, the data variable is expected to point at a location
75 * (in user space) where the result of the ptrace call is written (instead of
76 * being returned).
77 */
Christoph Hellwig481bed42005-11-07 00:59:47 -080078long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int ret;
81 unsigned long __user *datap = (unsigned long __user *)data;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 switch (request) {
84 /* Read word at location address. */
85 case PTRACE_PEEKTEXT:
Alexey Dobriyan76647322007-07-17 04:03:43 -070086 case PTRACE_PEEKDATA:
87 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 /* Read the word at location address in the USER area. */
91 case PTRACE_PEEKUSR: {
92 unsigned long tmp;
93
94 ret = -EIO;
95 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
96 break;
97
98 tmp = get_reg(child, addr >> 2);
99 ret = put_user(tmp, datap);
100 break;
101 }
102
103 /* Write the word at location address. */
104 case PTRACE_POKETEXT:
105 case PTRACE_POKEDATA:
106 ret = 0;
107
108 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
109 break;
110
111 ret = -EIO;
112 break;
113
114 /* Write the word at location address in the USER area. */
115 case PTRACE_POKEUSR:
116 ret = -EIO;
117 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
118 break;
119
120 addr >>= 2;
121
122 if (addr == PT_DCCR) {
123 /* don't allow the tracing process to change stuff like
124 * interrupt enable, kernel/user bit, dma enables etc.
125 */
126 data &= DCCR_MASK;
127 data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
128 }
129 if (put_reg(child, addr, data))
130 break;
131 ret = 0;
132 break;
133
134 case PTRACE_SYSCALL:
135 case PTRACE_CONT:
136 ret = -EIO;
137
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700138 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 break;
140
141 if (request == PTRACE_SYSCALL) {
142 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
143 }
144 else {
145 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
146 }
147
148 child->exit_code = data;
149
150 /* TODO: make sure any pending breakpoint is killed */
151 wake_up_process(child);
152 ret = 0;
153
154 break;
155
156 /* Make the child exit by sending it a sigkill. */
157 case PTRACE_KILL:
158 ret = 0;
159
Mikael Starvik5d01e6c2005-07-27 11:44:43 -0700160 if (child->exit_state == EXIT_ZOMBIE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 break;
162
163 child->exit_code = SIGKILL;
164
165 /* TODO: make sure any pending breakpoint is killed */
166 wake_up_process(child);
167 break;
168
169 /* Set the trap flag. */
170 case PTRACE_SINGLESTEP:
171 ret = -EIO;
172
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700173 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175
176 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
177
178 /* TODO: set some clever breakpoint mechanism... */
179
180 child->exit_code = data;
181 wake_up_process(child);
182 ret = 0;
183 break;
184
185 case PTRACE_DETACH:
186 ret = ptrace_detach(child, data);
187 break;
188
189 /* Get all GP registers from the child. */
190 case PTRACE_GETREGS: {
191 int i;
192 unsigned long tmp;
193
Al Viroc3508852006-01-28 22:17:11 -0500194 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 for (i = 0; i <= PT_MAX; i++) {
196 tmp = get_reg(child, i);
197
198 if (put_user(tmp, datap)) {
199 ret = -EFAULT;
Al Viroc3508852006-01-28 22:17:11 -0500200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
203 data += sizeof(long);
204 }
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
207 }
208
209 /* Set all GP registers in the child. */
210 case PTRACE_SETREGS: {
211 int i;
212 unsigned long tmp;
213
Al Viroc3508852006-01-28 22:17:11 -0500214 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 for (i = 0; i <= PT_MAX; i++) {
216 if (get_user(tmp, datap)) {
217 ret = -EFAULT;
Al Viroc3508852006-01-28 22:17:11 -0500218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
221 if (i == PT_DCCR) {
222 tmp &= DCCR_MASK;
223 tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
224 }
225
226 put_reg(child, i, tmp);
227 data += sizeof(long);
228 }
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
231 }
232
233 default:
234 ret = ptrace_request(child, request, addr, data);
235 break;
236 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return ret;
239}
240
241void do_syscall_trace(void)
242{
243 if (!test_thread_flag(TIF_SYSCALL_TRACE))
244 return;
245
246 if (!(current->ptrace & PT_PTRACED))
247 return;
248
249 /* the 0x80 provides a way for the tracing parent to distinguish
250 between a syscall stop and SIGTRAP delivery */
251 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
252 ? 0x80 : 0));
253
254 /*
255 * This isn't the same as continuing with a signal, but it will do for
256 * normal use.
257 */
258 if (current->exit_code) {
259 send_sig(current->exit_code, current, 1);
260 current->exit_code = 0;
261 }
262}