blob: 855f7246cfffb4a4d597a11bd5ffc654a32e348a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/smp.h>
15#include <linux/smp_lock.h>
16#include <linux/errno.h>
17#include <linux/ptrace.h>
18#include <linux/user.h>
19#include <linux/slab.h>
20#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070021#include <linux/signal.h>
Stuart Menefy9432f962007-02-23 13:22:17 +090022#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <asm/pgtable.h>
25#include <asm/system.h>
26#include <asm/processor.h>
27#include <asm/mmu_context.h>
28
29/*
30 * does not yet catch signals sent when the child dies.
31 * in exit.c or in signal.c.
32 */
33
34/*
35 * This routine will get a word off of the process kernel stack.
36 */
37static inline int get_stack_long(struct task_struct *task, int offset)
38{
39 unsigned char *stack;
40
Al Viro3cf0f4e2006-01-12 01:05:44 -080041 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 stack += offset;
43 return (*((int *)stack));
44}
45
46/*
47 * This routine will put a word on the process kernel stack.
48 */
49static inline int put_stack_long(struct task_struct *task, int offset,
50 unsigned long data)
51{
52 unsigned char *stack;
53
Al Viro3cf0f4e2006-01-12 01:05:44 -080054 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 stack += offset;
56 *(unsigned long *) stack = data;
57 return 0;
58}
59
Stuart Menefy9432f962007-02-23 13:22:17 +090060static void ptrace_disable_singlestep(struct task_struct *child)
61{
62 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
63
64 /*
65 * Ensure the UBC is not programmed at the next context switch.
66 *
67 * Normally this is not needed but there are sequences such as
68 * singlestep, signal delivery, and continue that leave the
69 * ubc_pc non-zero leading to spurious SIGTRAPs.
70 */
71 if (child->thread.ubc_pc != 0) {
72 ubc_usercnt -= 1;
73 child->thread.ubc_pc = 0;
74 }
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * Called by kernel/ptrace.c when detaching..
79 *
80 * Make sure single step bits etc are not set.
81 */
82void ptrace_disable(struct task_struct *child)
83{
Stuart Menefy9432f962007-02-23 13:22:17 +090084 ptrace_disable_singlestep(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Christoph Hellwig481bed42005-11-07 00:59:47 -080087long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct user * dummy = NULL;
90 int ret;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 switch (request) {
93 /* when I and D space are separate, these will need to be fixed. */
Stuart Menefy9432f962007-02-23 13:22:17 +090094 case PTRACE_PEEKTEXT: /* read word at location addr. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 case PTRACE_PEEKDATA: {
96 unsigned long tmp;
97 int copied;
98
99 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
100 ret = -EIO;
101 if (copied != sizeof(tmp))
102 break;
103 ret = put_user(tmp,(unsigned long *) data);
104 break;
105 }
106
107 /* read the word at location addr in the USER area. */
108 case PTRACE_PEEKUSR: {
109 unsigned long tmp;
110
111 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900112 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 addr > sizeof(struct user) - 3)
114 break;
115
116 if (addr < sizeof(struct pt_regs))
117 tmp = get_stack_long(child, addr);
118 else if (addr >= (long) &dummy->fpu &&
119 addr < (long) &dummy->u_fpvalid) {
120 if (!tsk_used_math(child)) {
121 if (addr == (long)&dummy->fpu.fpscr)
122 tmp = FPSCR_INIT;
123 else
124 tmp = 0;
125 } else
126 tmp = ((long *)&child->thread.fpu)
127 [(addr - (long)&dummy->fpu) >> 2];
128 } else if (addr == (long) &dummy->u_fpvalid)
129 tmp = !!tsk_used_math(child);
130 else
131 tmp = 0;
132 ret = put_user(tmp, (unsigned long *)data);
133 break;
134 }
135
136 /* when I and D space are separate, this will have to be fixed. */
137 case PTRACE_POKETEXT: /* write the word at location addr. */
138 case PTRACE_POKEDATA:
139 ret = 0;
140 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
141 break;
142 ret = -EIO;
143 break;
144
145 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
146 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900147 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 addr > sizeof(struct user) - 3)
149 break;
150
151 if (addr < sizeof(struct pt_regs))
152 ret = put_stack_long(child, addr, data);
153 else if (addr >= (long) &dummy->fpu &&
154 addr < (long) &dummy->u_fpvalid) {
155 set_stopped_child_used_math(child);
156 ((long *)&child->thread.fpu)
157 [(addr - (long)&dummy->fpu) >> 2] = data;
158 ret = 0;
159 } else if (addr == (long) &dummy->u_fpvalid) {
160 conditional_stopped_child_used_math(data, child);
161 ret = 0;
162 }
163 break;
164
165 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
166 case PTRACE_CONT: { /* restart after signal. */
167 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700168 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 if (request == PTRACE_SYSCALL)
171 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
172 else
173 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Stuart Menefy9432f962007-02-23 13:22:17 +0900174
175 ptrace_disable_singlestep(child);
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 child->exit_code = data;
178 wake_up_process(child);
179 ret = 0;
180 break;
181 }
182
183/*
Stuart Menefy9432f962007-02-23 13:22:17 +0900184 * make the child exit. Best I can do is send it a sigkill.
185 * perhaps it should be put in the status that it wants to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * exit.
187 */
188 case PTRACE_KILL: {
189 ret = 0;
190 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
191 break;
Stuart Menefy9432f962007-02-23 13:22:17 +0900192 ptrace_disable_singlestep(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 child->exit_code = SIGKILL;
194 wake_up_process(child);
195 break;
196 }
197
198 case PTRACE_SINGLESTEP: { /* set the trap flag. */
199 long pc;
200 struct pt_regs *dummy = NULL;
201
202 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700203 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
206 if ((child->ptrace & PT_DTRACE) == 0) {
207 /* Spurious delayed TF traps may occur */
208 child->ptrace |= PT_DTRACE;
209 }
210
211 pc = get_stack_long(child, (long)&dummy->pc);
212
213 /* Next scheduling will set up UBC */
214 if (child->thread.ubc_pc == 0)
215 ubc_usercnt += 1;
216 child->thread.ubc_pc = pc;
217
Stuart Menefy9432f962007-02-23 13:22:17 +0900218 set_tsk_thread_flag(child, TIF_SINGLESTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 child->exit_code = data;
220 /* give it a chance to run. */
221 wake_up_process(child);
222 ret = 0;
223 break;
224 }
225
226 case PTRACE_DETACH: /* detach a process that was attached. */
227 ret = ptrace_detach(child, data);
228 break;
229
230#ifdef CONFIG_SH_DSP
231 case PTRACE_GETDSPREGS: {
232 unsigned long dp;
233
234 ret = -EIO;
235 dp = ((unsigned long) child) + THREAD_SIZE -
236 sizeof(struct pt_dspregs);
237 if (*((int *) (dp - 4)) == SR_FD) {
238 copy_to_user(addr, (void *) dp,
239 sizeof(struct pt_dspregs));
240 ret = 0;
241 }
242 break;
243 }
244
245 case PTRACE_SETDSPREGS: {
246 unsigned long dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 ret = -EIO;
249 dp = ((unsigned long) child) + THREAD_SIZE -
250 sizeof(struct pt_dspregs);
251 if (*((int *) (dp - 4)) == SR_FD) {
252 copy_from_user((void *) dp, addr,
253 sizeof(struct pt_dspregs));
254 ret = 0;
255 }
256 break;
257 }
258#endif
259 default:
260 ret = ptrace_request(child, request, addr, data);
261 break;
262 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return ret;
265}
266
267asmlinkage void do_syscall_trace(void)
268{
269 struct task_struct *tsk = current;
270
Stuart Menefy9432f962007-02-23 13:22:17 +0900271 if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
272 !test_thread_flag(TIF_SINGLESTEP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return;
274 if (!(tsk->ptrace & PT_PTRACED))
275 return;
276 /* the 0x80 provides a way for the tracing parent to distinguish
277 between a syscall stop and SIGTRAP delivery */
Stuart Menefy9432f962007-02-23 13:22:17 +0900278 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
279 !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /*
282 * this isn't the same as continuing with a signal, but it will do
283 * for normal use. strace only continues with a signal if the
284 * stopping signal is not SIGTRAP. -brl
285 */
286 if (tsk->exit_code) {
287 send_sig(tsk->exit_code, tsk, 1);
288 tsk->exit_code = 0;
289 }
290}