blob: f64a2d2416d4a364e0330ae5cab5e64666a65e54 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
16#include <linux/ptrace.h>
17#include <linux/user.h>
18#include <linux/slab.h>
19#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070020#include <linux/signal.h>
Stuart Menefy9432f962007-02-23 13:22:17 +090021#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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
Al Viro3cf0f4e2006-01-12 01:05:44 -080040 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 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
Al Viro3cf0f4e2006-01-12 01:05:44 -080053 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 stack += offset;
55 *(unsigned long *) stack = data;
56 return 0;
57}
58
Stuart Menefy9432f962007-02-23 13:22:17 +090059static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
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{
Stuart Menefy9432f962007-02-23 13:22:17 +090083 ptrace_disable_singlestep(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Christoph Hellwig481bed42005-11-07 00:59:47 -080086long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct user * dummy = NULL;
89 int ret;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 switch (request) {
92 /* when I and D space are separate, these will need to be fixed. */
Stuart Menefy9432f962007-02-23 13:22:17 +090093 case PTRACE_PEEKTEXT: /* read word at location addr. */
Alexey Dobriyan76647322007-07-17 04:03:43 -070094 case PTRACE_PEEKDATA:
95 ret = generic_ptrace_peekdata(child, addr, data);
Paul Mundt662ae212007-08-04 13:39:21 +090096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* read the word at location addr in the USER area. */
99 case PTRACE_PEEKUSR: {
100 unsigned long tmp;
101
102 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900103 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 addr > sizeof(struct user) - 3)
105 break;
106
107 if (addr < sizeof(struct pt_regs))
108 tmp = get_stack_long(child, addr);
109 else if (addr >= (long) &dummy->fpu &&
110 addr < (long) &dummy->u_fpvalid) {
111 if (!tsk_used_math(child)) {
112 if (addr == (long)&dummy->fpu.fpscr)
113 tmp = FPSCR_INIT;
114 else
115 tmp = 0;
116 } else
117 tmp = ((long *)&child->thread.fpu)
118 [(addr - (long)&dummy->fpu) >> 2];
119 } else if (addr == (long) &dummy->u_fpvalid)
120 tmp = !!tsk_used_math(child);
121 else
122 tmp = 0;
Paul Mundte08f4572007-05-14 12:52:56 +0900123 ret = put_user(tmp, (unsigned long __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 break;
125 }
126
127 /* when I and D space are separate, this will have to be fixed. */
128 case PTRACE_POKETEXT: /* write the word at location addr. */
129 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700130 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 break;
132
133 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
134 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900135 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 addr > sizeof(struct user) - 3)
137 break;
138
139 if (addr < sizeof(struct pt_regs))
140 ret = put_stack_long(child, addr, data);
141 else if (addr >= (long) &dummy->fpu &&
142 addr < (long) &dummy->u_fpvalid) {
143 set_stopped_child_used_math(child);
144 ((long *)&child->thread.fpu)
145 [(addr - (long)&dummy->fpu) >> 2] = data;
146 ret = 0;
147 } else if (addr == (long) &dummy->u_fpvalid) {
148 conditional_stopped_child_used_math(data, child);
149 ret = 0;
150 }
151 break;
152
153 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
154 case PTRACE_CONT: { /* restart after signal. */
155 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700156 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 break;
158 if (request == PTRACE_SYSCALL)
159 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
160 else
161 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Stuart Menefy9432f962007-02-23 13:22:17 +0900162
163 ptrace_disable_singlestep(child);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 child->exit_code = data;
166 wake_up_process(child);
167 ret = 0;
168 break;
169 }
170
171/*
Stuart Menefy9432f962007-02-23 13:22:17 +0900172 * make the child exit. Best I can do is send it a sigkill.
173 * perhaps it should be put in the status that it wants to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * exit.
175 */
176 case PTRACE_KILL: {
177 ret = 0;
178 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
179 break;
Stuart Menefy9432f962007-02-23 13:22:17 +0900180 ptrace_disable_singlestep(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 child->exit_code = SIGKILL;
182 wake_up_process(child);
183 break;
184 }
185
186 case PTRACE_SINGLESTEP: { /* set the trap flag. */
187 long pc;
Paul Mundte08f4572007-05-14 12:52:56 +0900188 struct pt_regs *regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700191 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
194 if ((child->ptrace & PT_DTRACE) == 0) {
195 /* Spurious delayed TF traps may occur */
196 child->ptrace |= PT_DTRACE;
197 }
198
Paul Mundte08f4572007-05-14 12:52:56 +0900199 pc = get_stack_long(child, (long)&regs->pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* Next scheduling will set up UBC */
202 if (child->thread.ubc_pc == 0)
203 ubc_usercnt += 1;
204 child->thread.ubc_pc = pc;
205
Stuart Menefy9432f962007-02-23 13:22:17 +0900206 set_tsk_thread_flag(child, TIF_SINGLESTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 child->exit_code = data;
208 /* give it a chance to run. */
209 wake_up_process(child);
210 ret = 0;
211 break;
212 }
213
214 case PTRACE_DETACH: /* detach a process that was attached. */
215 ret = ptrace_detach(child, data);
216 break;
217
218#ifdef CONFIG_SH_DSP
219 case PTRACE_GETDSPREGS: {
220 unsigned long dp;
221
222 ret = -EIO;
223 dp = ((unsigned long) child) + THREAD_SIZE -
224 sizeof(struct pt_dspregs);
225 if (*((int *) (dp - 4)) == SR_FD) {
226 copy_to_user(addr, (void *) dp,
227 sizeof(struct pt_dspregs));
228 ret = 0;
229 }
230 break;
231 }
232
233 case PTRACE_SETDSPREGS: {
234 unsigned long dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 ret = -EIO;
237 dp = ((unsigned long) child) + THREAD_SIZE -
238 sizeof(struct pt_dspregs);
239 if (*((int *) (dp - 4)) == SR_FD) {
240 copy_from_user((void *) dp, addr,
241 sizeof(struct pt_dspregs));
242 ret = 0;
243 }
244 break;
245 }
246#endif
247 default:
248 ret = ptrace_request(child, request, addr, data);
249 break;
250 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return ret;
253}
254
255asmlinkage void do_syscall_trace(void)
256{
257 struct task_struct *tsk = current;
258
Stuart Menefy9432f962007-02-23 13:22:17 +0900259 if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
260 !test_thread_flag(TIF_SINGLESTEP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return;
262 if (!(tsk->ptrace & PT_PTRACED))
263 return;
264 /* the 0x80 provides a way for the tracing parent to distinguish
265 between a syscall stop and SIGTRAP delivery */
Stuart Menefy9432f962007-02-23 13:22:17 +0900266 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
267 !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /*
270 * this isn't the same as continuing with a signal, but it will do
271 * for normal use. strace only continues with a signal if the
272 * stopping signal is not SIGTRAP. -brl
273 */
274 if (tsk->exit_code) {
275 send_sig(tsk->exit_code, tsk, 1);
276 tsk->exit_code = 0;
277 }
278}