Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[linux-2.6-block.git] / arch / sh / kernel / ptrace.c
CommitLineData
1da177e4
LT
1/*
2 * linux/arch/sh/kernel/ptrace.c
3 *
4 * Original x86 implementation:
5 * By Ross Biro 1/23/92
6 * edited by Linus Torvalds
7 *
8 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
9 *
10 */
1da177e4
LT
11#include <linux/kernel.h>
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/smp.h>
1da177e4
LT
15#include <linux/errno.h>
16#include <linux/ptrace.h>
17#include <linux/user.h>
18#include <linux/slab.h>
19#include <linux/security.h>
7ed20e1a 20#include <linux/signal.h>
9432f968 21#include <linux/io.h>
1da177e4
LT
22#include <asm/uaccess.h>
23#include <asm/pgtable.h>
24#include <asm/system.h>
25#include <asm/processor.h>
26#include <asm/mmu_context.h>
27
28/*
29 * does not yet catch signals sent when the child dies.
30 * in exit.c or in signal.c.
31 */
32
33/*
34 * This routine will get a word off of the process kernel stack.
35 */
36static inline int get_stack_long(struct task_struct *task, int offset)
37{
38 unsigned char *stack;
39
3cf0f4ec 40 stack = (unsigned char *)task_pt_regs(task);
1da177e4
LT
41 stack += offset;
42 return (*((int *)stack));
43}
44
45/*
46 * This routine will put a word on the process kernel stack.
47 */
48static inline int put_stack_long(struct task_struct *task, int offset,
49 unsigned long data)
50{
51 unsigned char *stack;
52
3cf0f4ec 53 stack = (unsigned char *)task_pt_regs(task);
1da177e4
LT
54 stack += offset;
55 *(unsigned long *) stack = data;
56 return 0;
57}
58
9432f968
SM
59static void ptrace_disable_singlestep(struct task_struct *child)
60{
61 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
62
63 /*
64 * Ensure the UBC is not programmed at the next context switch.
65 *
66 * Normally this is not needed but there are sequences such as
67 * singlestep, signal delivery, and continue that leave the
68 * ubc_pc non-zero leading to spurious SIGTRAPs.
69 */
70 if (child->thread.ubc_pc != 0) {
71 ubc_usercnt -= 1;
72 child->thread.ubc_pc = 0;
73 }
74}
75
1da177e4
LT
76/*
77 * Called by kernel/ptrace.c when detaching..
78 *
79 * Make sure single step bits etc are not set.
80 */
81void ptrace_disable(struct task_struct *child)
82{
9432f968 83 ptrace_disable_singlestep(child);
1da177e4
LT
84}
85
481bed45 86long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1da177e4 87{
1da177e4
LT
88 struct user * dummy = NULL;
89 int ret;
90
1da177e4
LT
91 switch (request) {
92 /* when I and D space are separate, these will need to be fixed. */
9432f968 93 case PTRACE_PEEKTEXT: /* read word at location addr. */
76647323
AD
94 case PTRACE_PEEKDATA:
95 ret = generic_ptrace_peekdata(child, addr, data);
1da177e4
LT
96
97 /* read the word at location addr in the USER area. */
98 case PTRACE_PEEKUSR: {
99 unsigned long tmp;
100
101 ret = -EIO;
9432f968 102 if ((addr & 3) || addr < 0 ||
1da177e4
LT
103 addr > sizeof(struct user) - 3)
104 break;
105
106 if (addr < sizeof(struct pt_regs))
107 tmp = get_stack_long(child, addr);
108 else if (addr >= (long) &dummy->fpu &&
109 addr < (long) &dummy->u_fpvalid) {
110 if (!tsk_used_math(child)) {
111 if (addr == (long)&dummy->fpu.fpscr)
112 tmp = FPSCR_INIT;
113 else
114 tmp = 0;
115 } else
116 tmp = ((long *)&child->thread.fpu)
117 [(addr - (long)&dummy->fpu) >> 2];
118 } else if (addr == (long) &dummy->u_fpvalid)
119 tmp = !!tsk_used_math(child);
120 else
121 tmp = 0;
e08f457c 122 ret = put_user(tmp, (unsigned long __user *)data);
1da177e4
LT
123 break;
124 }
125
126 /* when I and D space are separate, this will have to be fixed. */
127 case PTRACE_POKETEXT: /* write the word at location addr. */
128 case PTRACE_POKEDATA:
f284ce72 129 ret = generic_ptrace_pokedata(child, addr, data);
1da177e4
LT
130 break;
131
132 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
133 ret = -EIO;
9432f968 134 if ((addr & 3) || addr < 0 ||
1da177e4
LT
135 addr > sizeof(struct user) - 3)
136 break;
137
138 if (addr < sizeof(struct pt_regs))
139 ret = put_stack_long(child, addr, data);
140 else if (addr >= (long) &dummy->fpu &&
141 addr < (long) &dummy->u_fpvalid) {
142 set_stopped_child_used_math(child);
143 ((long *)&child->thread.fpu)
144 [(addr - (long)&dummy->fpu) >> 2] = data;
145 ret = 0;
146 } else if (addr == (long) &dummy->u_fpvalid) {
147 conditional_stopped_child_used_math(data, child);
148 ret = 0;
149 }
150 break;
151
152 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
153 case PTRACE_CONT: { /* restart after signal. */
154 ret = -EIO;
7ed20e1a 155 if (!valid_signal(data))
1da177e4
LT
156 break;
157 if (request == PTRACE_SYSCALL)
158 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
159 else
160 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
9432f968
SM
161
162 ptrace_disable_singlestep(child);
163
1da177e4
LT
164 child->exit_code = data;
165 wake_up_process(child);
166 ret = 0;
167 break;
168 }
169
170/*
9432f968
SM
171 * make the child exit. Best I can do is send it a sigkill.
172 * perhaps it should be put in the status that it wants to
1da177e4
LT
173 * exit.
174 */
175 case PTRACE_KILL: {
176 ret = 0;
177 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
178 break;
9432f968 179 ptrace_disable_singlestep(child);
1da177e4
LT
180 child->exit_code = SIGKILL;
181 wake_up_process(child);
182 break;
183 }
184
185 case PTRACE_SINGLESTEP: { /* set the trap flag. */
186 long pc;
e08f457c 187 struct pt_regs *regs = NULL;
1da177e4
LT
188
189 ret = -EIO;
7ed20e1a 190 if (!valid_signal(data))
1da177e4
LT
191 break;
192 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
193 if ((child->ptrace & PT_DTRACE) == 0) {
194 /* Spurious delayed TF traps may occur */
195 child->ptrace |= PT_DTRACE;
196 }
197
e08f457c 198 pc = get_stack_long(child, (long)&regs->pc);
1da177e4
LT
199
200 /* Next scheduling will set up UBC */
201 if (child->thread.ubc_pc == 0)
202 ubc_usercnt += 1;
203 child->thread.ubc_pc = pc;
204
9432f968 205 set_tsk_thread_flag(child, TIF_SINGLESTEP);
1da177e4
LT
206 child->exit_code = data;
207 /* give it a chance to run. */
208 wake_up_process(child);
209 ret = 0;
210 break;
211 }
212
213 case PTRACE_DETACH: /* detach a process that was attached. */
214 ret = ptrace_detach(child, data);
215 break;
216
217#ifdef CONFIG_SH_DSP
218 case PTRACE_GETDSPREGS: {
219 unsigned long dp;
220
221 ret = -EIO;
222 dp = ((unsigned long) child) + THREAD_SIZE -
223 sizeof(struct pt_dspregs);
224 if (*((int *) (dp - 4)) == SR_FD) {
225 copy_to_user(addr, (void *) dp,
226 sizeof(struct pt_dspregs));
227 ret = 0;
228 }
229 break;
230 }
231
232 case PTRACE_SETDSPREGS: {
233 unsigned long dp;
1da177e4
LT
234
235 ret = -EIO;
236 dp = ((unsigned long) child) + THREAD_SIZE -
237 sizeof(struct pt_dspregs);
238 if (*((int *) (dp - 4)) == SR_FD) {
239 copy_from_user((void *) dp, addr,
240 sizeof(struct pt_dspregs));
241 ret = 0;
242 }
243 break;
244 }
245#endif
246 default:
247 ret = ptrace_request(child, request, addr, data);
248 break;
249 }
481bed45 250
1da177e4
LT
251 return ret;
252}
253
254asmlinkage void do_syscall_trace(void)
255{
256 struct task_struct *tsk = current;
257
9432f968
SM
258 if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
259 !test_thread_flag(TIF_SINGLESTEP))
1da177e4
LT
260 return;
261 if (!(tsk->ptrace & PT_PTRACED))
262 return;
263 /* the 0x80 provides a way for the tracing parent to distinguish
264 between a syscall stop and SIGTRAP delivery */
9432f968
SM
265 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
266 !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
1da177e4
LT
267
268 /*
269 * this isn't the same as continuing with a signal, but it will do
270 * for normal use. strace only continues with a signal if the
271 * stopping signal is not SIGTRAP. -brl
272 */
273 if (tsk->exit_code) {
274 send_sig(tsk->exit_code, tsk, 1);
275 tsk->exit_code = 0;
276 }
277}