[PATCH] Avoid console spam with ext3 aborted journal.
[linux-2.6/verdex.git] / arch / cris / arch-v10 / kernel / ptrace.c
blob581ecabaae5397edbdbdb9923888bdfec3cebf86
1 /*
2 * Copyright (C) 2000-2003, Axis Communications AB.
3 */
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>
13 #include <linux/signal.h>
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>
21 /*
22 * Determines which bits in DCCR the user has access to.
23 * 1 = access, 0 = no access.
25 #define DCCR_MASK 0x0000001f /* XNZVC */
28 * Get contents of register REGNO in task TASK.
30 inline long get_reg(struct task_struct *task, unsigned int regno)
32 /* USP is a special case, it's not in the pt_regs struct but
33 * in the tasks thread struct
36 if (regno == PT_USP)
37 return task->thread.usp;
38 else if (regno < PT_MAX)
39 return ((unsigned long *)user_regs(task->thread_info))[regno];
40 else
41 return 0;
45 * Write contents of register REGNO in task TASK.
47 inline int put_reg(struct task_struct *task, unsigned int regno,
48 unsigned long data)
50 if (regno == PT_USP)
51 task->thread.usp = data;
52 else if (regno < PT_MAX)
53 ((unsigned long *)user_regs(task->thread_info))[regno] = data;
54 else
55 return -1;
56 return 0;
60 * Called by kernel/ptrace.c when detaching.
62 * Make sure the single step bit is not set.
64 void
65 ptrace_disable(struct task_struct *child)
67 /* Todo - pending singlesteps? */
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).
78 asmlinkage int
79 sys_ptrace(long request, long pid, long addr, long data)
81 struct task_struct *child;
82 int ret;
83 unsigned long __user *datap = (unsigned long __user *)data;
85 lock_kernel();
86 ret = -EPERM;
88 if (request == PTRACE_TRACEME) {
89 if (current->ptrace & PT_PTRACED)
90 goto out;
92 current->ptrace |= PT_PTRACED;
93 ret = 0;
94 goto out;
97 ret = -ESRCH;
98 read_lock(&tasklist_lock);
99 child = find_task_by_pid(pid);
101 if (child)
102 get_task_struct(child);
104 read_unlock(&tasklist_lock);
106 if (!child)
107 goto out;
109 ret = -EPERM;
111 if (pid == 1) /* Leave the init process alone! */
112 goto out_tsk;
114 if (request == PTRACE_ATTACH) {
115 ret = ptrace_attach(child);
116 goto out_tsk;
119 ret = ptrace_check_attach(child, request == PTRACE_KILL);
120 if (ret < 0)
121 goto out_tsk;
123 switch (request) {
124 /* Read word at location address. */
125 case PTRACE_PEEKTEXT:
126 case PTRACE_PEEKDATA: {
127 unsigned long tmp;
128 int copied;
130 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
131 ret = -EIO;
133 if (copied != sizeof(tmp))
134 break;
136 ret = put_user(tmp,datap);
137 break;
140 /* Read the word at location address in the USER area. */
141 case PTRACE_PEEKUSR: {
142 unsigned long tmp;
144 ret = -EIO;
145 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
146 break;
148 tmp = get_reg(child, addr >> 2);
149 ret = put_user(tmp, datap);
150 break;
153 /* Write the word at location address. */
154 case PTRACE_POKETEXT:
155 case PTRACE_POKEDATA:
156 ret = 0;
158 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
159 break;
161 ret = -EIO;
162 break;
164 /* Write the word at location address in the USER area. */
165 case PTRACE_POKEUSR:
166 ret = -EIO;
167 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
168 break;
170 addr >>= 2;
172 if (addr == PT_DCCR) {
173 /* don't allow the tracing process to change stuff like
174 * interrupt enable, kernel/user bit, dma enables etc.
176 data &= DCCR_MASK;
177 data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
179 if (put_reg(child, addr, data))
180 break;
181 ret = 0;
182 break;
184 case PTRACE_SYSCALL:
185 case PTRACE_CONT:
186 ret = -EIO;
188 if (!valid_signal(data))
189 break;
191 if (request == PTRACE_SYSCALL) {
192 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
194 else {
195 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
198 child->exit_code = data;
200 /* TODO: make sure any pending breakpoint is killed */
201 wake_up_process(child);
202 ret = 0;
204 break;
206 /* Make the child exit by sending it a sigkill. */
207 case PTRACE_KILL:
208 ret = 0;
210 if (child->state == TASK_ZOMBIE)
211 break;
213 child->exit_code = SIGKILL;
215 /* TODO: make sure any pending breakpoint is killed */
216 wake_up_process(child);
217 break;
219 /* Set the trap flag. */
220 case PTRACE_SINGLESTEP:
221 ret = -EIO;
223 if (!valid_signal(data))
224 break;
226 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
228 /* TODO: set some clever breakpoint mechanism... */
230 child->exit_code = data;
231 wake_up_process(child);
232 ret = 0;
233 break;
235 case PTRACE_DETACH:
236 ret = ptrace_detach(child, data);
237 break;
239 /* Get all GP registers from the child. */
240 case PTRACE_GETREGS: {
241 int i;
242 unsigned long tmp;
244 for (i = 0; i <= PT_MAX; i++) {
245 tmp = get_reg(child, i);
247 if (put_user(tmp, datap)) {
248 ret = -EFAULT;
249 goto out_tsk;
252 data += sizeof(long);
255 ret = 0;
256 break;
259 /* Set all GP registers in the child. */
260 case PTRACE_SETREGS: {
261 int i;
262 unsigned long tmp;
264 for (i = 0; i <= PT_MAX; i++) {
265 if (get_user(tmp, datap)) {
266 ret = -EFAULT;
267 goto out_tsk;
270 if (i == PT_DCCR) {
271 tmp &= DCCR_MASK;
272 tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
275 put_reg(child, i, tmp);
276 data += sizeof(long);
279 ret = 0;
280 break;
283 default:
284 ret = ptrace_request(child, request, addr, data);
285 break;
287 out_tsk:
288 put_task_struct(child);
289 out:
290 unlock_kernel();
291 return ret;
294 void do_syscall_trace(void)
296 if (!test_thread_flag(TIF_SYSCALL_TRACE))
297 return;
299 if (!(current->ptrace & PT_PTRACED))
300 return;
302 /* the 0x80 provides a way for the tracing parent to distinguish
303 between a syscall stop and SIGTRAP delivery */
304 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
305 ? 0x80 : 0));
308 * This isn't the same as continuing with a signal, but it will do for
309 * normal use.
311 if (current->exit_code) {
312 send_sig(current->exit_code, current, 1);
313 current->exit_code = 0;