PTRACE_PEEKDATA consolidation
[linux-2.6/openmoko-kernel.git] / arch / xtensa / kernel / ptrace.c
blobaf182d3a700086def4a8e2180c19c92d1a05f969
1 // TODO some minor issues
2 /*
3 * This file is subject to the terms and conditions of the GNU General Public
4 * License. See the file "COPYING" in the main directory of this archive
5 * for more details.
7 * Copyright (C) 2001 - 2005 Tensilica Inc.
9 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
10 * Chris Zankel <chris@zankel.net>
11 * Scott Foehner<sfoehner@yahoo.com>,
12 * Kevin Chea
13 * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/mm.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/smp.h>
22 #include <linux/security.h>
23 #include <linux/signal.h>
25 #include <asm/pgtable.h>
26 #include <asm/page.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/ptrace.h>
30 #include <asm/elf.h>
32 #define TEST_KERNEL // verify kernel operations FIXME: remove
36 * Called by kernel/ptrace.c when detaching..
38 * Make sure single step bits etc are not set.
41 void ptrace_disable(struct task_struct *child)
43 /* Nothing to do.. */
46 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
48 int ret = -EPERM;
50 switch (request) {
51 case PTRACE_PEEKTEXT: /* read word at location addr. */
52 case PTRACE_PEEKDATA:
53 ret = generic_ptrace_peekdata(child, addr, data);
54 goto out;
56 /* Read the word at location addr in the USER area. */
58 case PTRACE_PEEKUSR:
60 struct pt_regs *regs;
61 unsigned long tmp;
63 regs = task_pt_regs(child);
64 tmp = 0; /* Default return value. */
66 switch(addr) {
68 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
70 int ar = addr - REG_AR_BASE - regs->windowbase * 4;
71 ar &= (XCHAL_NUM_AREGS - 1);
72 if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
73 tmp = regs->areg[ar];
74 else
75 ret = -EIO;
76 break;
78 case REG_A_BASE ... REG_A_BASE + 15:
79 tmp = regs->areg[addr - REG_A_BASE];
80 break;
81 case REG_PC:
82 tmp = regs->pc;
83 break;
84 case REG_PS:
85 /* Note: PS.EXCM is not set while user task is running;
86 * its being set in regs is for exception handling
87 * convenience. */
88 tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
89 break;
90 case REG_WB:
91 tmp = regs->windowbase;
92 break;
93 case REG_WS:
94 tmp = regs->windowstart;
95 break;
96 case REG_LBEG:
97 tmp = regs->lbeg;
98 break;
99 case REG_LEND:
100 tmp = regs->lend;
101 break;
102 case REG_LCOUNT:
103 tmp = regs->lcount;
104 break;
105 case REG_SAR:
106 tmp = regs->sar;
107 break;
108 case REG_DEPC:
109 tmp = regs->depc;
110 break;
111 case REG_EXCCAUSE:
112 tmp = regs->exccause;
113 break;
114 case REG_EXCVADDR:
115 tmp = regs->excvaddr;
116 break;
117 case SYSCALL_NR:
118 tmp = regs->syscall;
119 break;
120 default:
121 tmp = 0;
122 ret = -EIO;
123 goto out;
125 ret = put_user(tmp, (unsigned long *) data);
126 goto out;
129 case PTRACE_POKETEXT: /* write the word at location addr. */
130 case PTRACE_POKEDATA:
131 if (access_process_vm(child, addr, &data, sizeof(data), 1)
132 == sizeof(data))
133 break;
134 ret = -EIO;
135 goto out;
137 case PTRACE_POKEUSR:
139 struct pt_regs *regs;
140 regs = task_pt_regs(child);
142 switch (addr) {
143 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
145 int ar = addr - REG_AR_BASE - regs->windowbase * 4;
146 if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
147 regs->areg[ar & (XCHAL_NUM_AREGS - 1)] = data;
148 else
149 ret = -EIO;
150 break;
152 case REG_A_BASE ... REG_A_BASE + 15:
153 regs->areg[addr - REG_A_BASE] = data;
154 break;
155 case REG_PC:
156 regs->pc = data;
157 break;
158 case SYSCALL_NR:
159 regs->syscall = data;
160 break;
161 #ifdef TEST_KERNEL
162 case REG_WB:
163 regs->windowbase = data;
164 break;
165 case REG_WS:
166 regs->windowstart = data;
167 break;
168 #endif
170 default:
171 /* The rest are not allowed. */
172 ret = -EIO;
173 break;
175 break;
178 /* continue and stop at next (return from) syscall */
179 case PTRACE_SYSCALL:
180 case PTRACE_CONT: /* restart after signal. */
182 ret = -EIO;
183 if (!valid_signal(data))
184 break;
185 if (request == PTRACE_SYSCALL)
186 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
187 else
188 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
189 child->exit_code = data;
190 /* Make sure the single step bit is not set. */
191 child->ptrace &= ~PT_SINGLESTEP;
192 wake_up_process(child);
193 ret = 0;
194 break;
198 * make the child exit. Best I can do is send it a sigkill.
199 * perhaps it should be put in the status that it wants to
200 * exit.
202 case PTRACE_KILL:
203 ret = 0;
204 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
205 break;
206 child->exit_code = SIGKILL;
207 child->ptrace &= ~PT_SINGLESTEP;
208 wake_up_process(child);
209 break;
211 case PTRACE_SINGLESTEP:
212 ret = -EIO;
213 if (!valid_signal(data))
214 break;
215 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
216 child->ptrace |= PT_SINGLESTEP;
217 child->exit_code = data;
218 wake_up_process(child);
219 ret = 0;
220 break;
222 case PTRACE_GETREGS:
224 /* 'data' points to user memory in which to write.
225 * Mainly due to the non-live register values, we
226 * reformat the register values into something more
227 * standard. For convenience, we use the handy
228 * elf_gregset_t format. */
230 xtensa_gregset_t format;
231 struct pt_regs *regs = task_pt_regs(child);
233 do_copy_regs (&format, regs, child);
235 /* Now, copy to user space nice and easy... */
236 ret = 0;
237 if (copy_to_user((void *)data, &format, sizeof(elf_gregset_t)))
238 ret = -EFAULT;
239 break;
242 case PTRACE_SETREGS:
244 /* 'data' points to user memory that contains the new
245 * values in the elf_gregset_t format. */
247 xtensa_gregset_t format;
248 struct pt_regs *regs = task_pt_regs(child);
250 if (copy_from_user(&format,(void *)data,sizeof(elf_gregset_t))){
251 ret = -EFAULT;
252 break;
255 /* FIXME: Perhaps we want some sanity checks on
256 * these user-space values? See ARM version. Are
257 * debuggers a security concern? */
259 do_restore_regs (&format, regs, child);
261 ret = 0;
262 break;
265 case PTRACE_GETFPREGS:
267 /* 'data' points to user memory in which to write.
268 * For convenience, we use the handy
269 * elf_fpregset_t format. */
271 elf_fpregset_t fpregs;
272 struct pt_regs *regs = task_pt_regs(child);
274 do_save_fpregs (&fpregs, regs, child);
276 /* Now, copy to user space nice and easy... */
277 ret = 0;
278 if (copy_to_user((void *)data, &fpregs, sizeof(elf_fpregset_t)))
279 ret = -EFAULT;
281 break;
284 case PTRACE_SETFPREGS:
286 /* 'data' points to user memory that contains the new
287 * values in the elf_fpregset_t format.
289 elf_fpregset_t fpregs;
290 struct pt_regs *regs = task_pt_regs(child);
292 ret = 0;
293 if (copy_from_user(&fpregs, (void *)data, sizeof(elf_fpregset_t))) {
294 ret = -EFAULT;
295 break;
298 if (do_restore_fpregs (&fpregs, regs, child))
299 ret = -EIO;
300 break;
303 case PTRACE_GETFPREGSIZE:
304 /* 'data' points to 'unsigned long' set to the size
305 * of elf_fpregset_t
307 ret = put_user(sizeof(elf_fpregset_t), (unsigned long *) data);
308 break;
310 case PTRACE_DETACH: /* detach a process that was attached. */
311 ret = ptrace_detach(child, data);
312 break;
314 default:
315 ret = ptrace_request(child, request, addr, data);
316 goto out;
318 out:
319 return ret;
322 void do_syscall_trace(void)
325 * The 0x80 provides a way for the tracing parent to distinguish
326 * between a syscall stop and SIGTRAP delivery
328 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
331 * this isn't the same as continuing with a signal, but it will do
332 * for normal use. strace only continues with a signal if the
333 * stopping signal is not SIGTRAP. -brl
335 if (current->exit_code) {
336 send_sig(current->exit_code, current, 1);
337 current->exit_code = 0;
341 void do_syscall_trace_enter(struct pt_regs *regs)
343 if (test_thread_flag(TIF_SYSCALL_TRACE)
344 && (current->ptrace & PT_PTRACED))
345 do_syscall_trace();
347 #if 0
348 if (unlikely(current->audit_context))
349 audit_syscall_entry(current, AUDIT_ARCH_XTENSA..);
350 #endif
353 void do_syscall_trace_leave(struct pt_regs *regs)
355 if ((test_thread_flag(TIF_SYSCALL_TRACE))
356 && (current->ptrace & PT_PTRACED))
357 do_syscall_trace();