Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sh / kernel / ptrace.c
blob1b0dfb4d8ea472d550fea292d87a428fc4fa1e40
1 /*
2 * linux/arch/sh/kernel/ptrace.c
4 * Original x86 implementation:
5 * By Ross Biro 1/23/92
6 * edited by Linus Torvalds
8 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/slab.h>
22 #include <linux/security.h>
24 #include <asm/io.h>
25 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
29 #include <asm/mmu_context.h>
32 * does not yet catch signals sent when the child dies.
33 * in exit.c or in signal.c.
37 * This routine will get a word off of the process kernel stack.
39 static inline int get_stack_long(struct task_struct *task, int offset)
41 unsigned char *stack;
43 stack = (unsigned char *)
44 task->thread_info + THREAD_SIZE - sizeof(struct pt_regs)
45 #ifdef CONFIG_SH_DSP
46 - sizeof(struct pt_dspregs)
47 #endif
48 - sizeof(unsigned long);
49 stack += offset;
50 return (*((int *)stack));
54 * This routine will put a word on the process kernel stack.
56 static inline int put_stack_long(struct task_struct *task, int offset,
57 unsigned long data)
59 unsigned char *stack;
61 stack = (unsigned char *)
62 task->thread_info + THREAD_SIZE - sizeof(struct pt_regs)
63 #ifdef CONFIG_SH_DSP
64 - sizeof(struct pt_dspregs)
65 #endif
66 - sizeof(unsigned long);
67 stack += offset;
68 *(unsigned long *) stack = data;
69 return 0;
73 * Called by kernel/ptrace.c when detaching..
75 * Make sure single step bits etc are not set.
77 void ptrace_disable(struct task_struct *child)
79 /* nothing to do.. */
82 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
84 struct task_struct *child;
85 struct user * dummy = NULL;
86 int ret;
88 lock_kernel();
89 ret = -EPERM;
90 if (request == PTRACE_TRACEME) {
91 /* are we already being traced? */
92 if (current->ptrace & PT_PTRACED)
93 goto out;
94 ret = security_ptrace(current->parent, current);
95 if (ret)
96 goto out;
97 /* set the ptrace bit in the process flags. */
98 current->ptrace |= PT_PTRACED;
99 ret = 0;
100 goto out;
102 ret = -ESRCH;
103 read_lock(&tasklist_lock);
104 child = find_task_by_pid(pid);
105 if (child)
106 get_task_struct(child);
107 read_unlock(&tasklist_lock);
108 if (!child)
109 goto out;
111 ret = -EPERM;
112 if (pid == 1) /* you may not mess with init */
113 goto out_tsk;
115 if (request == PTRACE_ATTACH) {
116 ret = ptrace_attach(child);
117 goto out_tsk;
120 ret = ptrace_check_attach(child, request == PTRACE_KILL);
121 if (ret < 0)
122 goto out_tsk;
124 switch (request) {
125 /* when I and D space are separate, these will need to be fixed. */
126 case PTRACE_PEEKTEXT: /* read word at location addr. */
127 case PTRACE_PEEKDATA: {
128 unsigned long tmp;
129 int copied;
131 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
132 ret = -EIO;
133 if (copied != sizeof(tmp))
134 break;
135 ret = put_user(tmp,(unsigned long *) data);
136 break;
139 /* read the word at location addr in the USER area. */
140 case PTRACE_PEEKUSR: {
141 unsigned long tmp;
143 ret = -EIO;
144 if ((addr & 3) || addr < 0 ||
145 addr > sizeof(struct user) - 3)
146 break;
148 if (addr < sizeof(struct pt_regs))
149 tmp = get_stack_long(child, addr);
150 else if (addr >= (long) &dummy->fpu &&
151 addr < (long) &dummy->u_fpvalid) {
152 if (!tsk_used_math(child)) {
153 if (addr == (long)&dummy->fpu.fpscr)
154 tmp = FPSCR_INIT;
155 else
156 tmp = 0;
157 } else
158 tmp = ((long *)&child->thread.fpu)
159 [(addr - (long)&dummy->fpu) >> 2];
160 } else if (addr == (long) &dummy->u_fpvalid)
161 tmp = !!tsk_used_math(child);
162 else
163 tmp = 0;
164 ret = put_user(tmp, (unsigned long *)data);
165 break;
168 /* when I and D space are separate, this will have to be fixed. */
169 case PTRACE_POKETEXT: /* write the word at location addr. */
170 case PTRACE_POKEDATA:
171 ret = 0;
172 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
173 break;
174 ret = -EIO;
175 break;
177 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
178 ret = -EIO;
179 if ((addr & 3) || addr < 0 ||
180 addr > sizeof(struct user) - 3)
181 break;
183 if (addr < sizeof(struct pt_regs))
184 ret = put_stack_long(child, addr, data);
185 else if (addr >= (long) &dummy->fpu &&
186 addr < (long) &dummy->u_fpvalid) {
187 set_stopped_child_used_math(child);
188 ((long *)&child->thread.fpu)
189 [(addr - (long)&dummy->fpu) >> 2] = data;
190 ret = 0;
191 } else if (addr == (long) &dummy->u_fpvalid) {
192 conditional_stopped_child_used_math(data, child);
193 ret = 0;
195 break;
197 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
198 case PTRACE_CONT: { /* restart after signal. */
199 ret = -EIO;
200 if ((unsigned long) data > _NSIG)
201 break;
202 if (request == PTRACE_SYSCALL)
203 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
204 else
205 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
206 child->exit_code = data;
207 wake_up_process(child);
208 ret = 0;
209 break;
213 * make the child exit. Best I can do is send it a sigkill.
214 * perhaps it should be put in the status that it wants to
215 * exit.
217 case PTRACE_KILL: {
218 ret = 0;
219 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
220 break;
221 child->exit_code = SIGKILL;
222 wake_up_process(child);
223 break;
226 case PTRACE_SINGLESTEP: { /* set the trap flag. */
227 long pc;
228 struct pt_regs *dummy = NULL;
230 ret = -EIO;
231 if ((unsigned long) data > _NSIG)
232 break;
233 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
234 if ((child->ptrace & PT_DTRACE) == 0) {
235 /* Spurious delayed TF traps may occur */
236 child->ptrace |= PT_DTRACE;
239 pc = get_stack_long(child, (long)&dummy->pc);
241 /* Next scheduling will set up UBC */
242 if (child->thread.ubc_pc == 0)
243 ubc_usercnt += 1;
244 child->thread.ubc_pc = pc;
246 child->exit_code = data;
247 /* give it a chance to run. */
248 wake_up_process(child);
249 ret = 0;
250 break;
253 case PTRACE_DETACH: /* detach a process that was attached. */
254 ret = ptrace_detach(child, data);
255 break;
257 #ifdef CONFIG_SH_DSP
258 case PTRACE_GETDSPREGS: {
259 unsigned long dp;
261 ret = -EIO;
262 dp = ((unsigned long) child) + THREAD_SIZE -
263 sizeof(struct pt_dspregs);
264 if (*((int *) (dp - 4)) == SR_FD) {
265 copy_to_user(addr, (void *) dp,
266 sizeof(struct pt_dspregs));
267 ret = 0;
269 break;
272 case PTRACE_SETDSPREGS: {
273 unsigned long dp;
274 int i;
276 ret = -EIO;
277 dp = ((unsigned long) child) + THREAD_SIZE -
278 sizeof(struct pt_dspregs);
279 if (*((int *) (dp - 4)) == SR_FD) {
280 copy_from_user((void *) dp, addr,
281 sizeof(struct pt_dspregs));
282 ret = 0;
284 break;
286 #endif
287 default:
288 ret = ptrace_request(child, request, addr, data);
289 break;
291 out_tsk:
292 put_task_struct(child);
293 out:
294 unlock_kernel();
295 return ret;
298 asmlinkage void do_syscall_trace(void)
300 struct task_struct *tsk = current;
302 if (!test_thread_flag(TIF_SYSCALL_TRACE))
303 return;
304 if (!(tsk->ptrace & PT_PTRACED))
305 return;
306 /* the 0x80 provides a way for the tracing parent to distinguish
307 between a syscall stop and SIGTRAP delivery */
308 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
309 ? 0x80 : 0));
312 * this isn't the same as continuing with a signal, but it will do
313 * for normal use. strace only continues with a signal if the
314 * stopping signal is not SIGTRAP. -brl
316 if (tsk->exit_code) {
317 send_sig(tsk->exit_code, tsk, 1);
318 tsk->exit_code = 0;