blob: f6fbdfa6876d0a7463c748b31a8fb591b926c4a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mundt4b27c472007-11-28 19:58:11 +09002 * arch/sh/kernel/ptrace_64.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000, 2001 Paolo Alberelli
Paul Mundt4b27c472007-11-28 19:58:11 +09005 * Copyright (C) 2003 - 2007 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Started from SH3/4 version:
8 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
9 *
10 * Original x86 implementation:
11 * By Ross Biro 1/23/92
12 * edited by Linus Torvalds
13 *
Paul Mundt4b27c472007-11-28 19:58:11 +090014 * This file is subject to the terms and conditions of the GNU General Public
15 * License. See the file "COPYING" in the main directory of this archive
16 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/rwsem.h>
20#include <linux/sched.h>
21#include <linux/mm.h>
22#include <linux/smp.h>
23#include <linux/smp_lock.h>
24#include <linux/errno.h>
25#include <linux/ptrace.h>
26#include <linux/user.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070027#include <linux/signal.h>
Christoph Hellwig481bed42005-11-07 00:59:47 -080028#include <linux/syscalls.h>
Paul Mundt4b27c472007-11-28 19:58:11 +090029#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
31#include <asm/uaccess.h>
32#include <asm/pgtable.h>
33#include <asm/system.h>
34#include <asm/processor.h>
35#include <asm/mmu_context.h>
36
37/* This mask defines the bits of the SR which the user is not allowed to
38 change, which are everything except S, Q, M, PR, SZ, FR. */
39#define SR_MASK (0xffff8cfd)
40
41/*
42 * does not yet catch signals sent when the child dies.
43 * in exit.c or in signal.c.
44 */
45
46/*
47 * This routine will get a word from the user area in the process kernel stack.
48 */
49static inline int get_stack_long(struct task_struct *task, int offset)
50{
51 unsigned char *stack;
52
53 stack = (unsigned char *)(task->thread.uregs);
54 stack += offset;
55 return (*((int *)stack));
56}
57
58static inline unsigned long
59get_fpu_long(struct task_struct *task, unsigned long addr)
60{
61 unsigned long tmp;
62 struct pt_regs *regs;
63 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
64
65 if (!tsk_used_math(task)) {
66 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
67 tmp = FPSCR_INIT;
68 } else {
69 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
70 }
71 return tmp;
72 }
73
74 if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +090075 enable_fpu();
Paul Mundt332fd572007-11-22 17:30:50 +090076 save_fpu(task, regs);
Paul Mundt256b22c2007-11-10 20:27:03 +090077 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 last_task_used_math = 0;
79 regs->sr |= SR_FD;
80 }
81
82 tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
83 return tmp;
84}
85
86/*
87 * This routine will put a word into the user area in the process kernel stack.
88 */
89static inline int put_stack_long(struct task_struct *task, int offset,
90 unsigned long data)
91{
92 unsigned char *stack;
93
94 stack = (unsigned char *)(task->thread.uregs);
95 stack += offset;
96 *(unsigned long *) stack = data;
97 return 0;
98}
99
100static inline int
101put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
102{
103 struct pt_regs *regs;
104
105 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
106
107 if (!tsk_used_math(task)) {
108 fpinit(&task->thread.fpu.hard);
109 set_stopped_child_used_math(task);
110 } else if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +0900111 enable_fpu();
Paul Mundt332fd572007-11-22 17:30:50 +0900112 save_fpu(task, regs);
Paul Mundt256b22c2007-11-10 20:27:03 +0900113 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 last_task_used_math = 0;
115 regs->sr |= SR_FD;
116 }
117
118 ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
119 return 0;
120}
121
Christoph Hellwig481bed42005-11-07 00:59:47 -0800122
123long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int ret;
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 switch (request) {
128 /* when I and D space are separate, these will need to be fixed. */
129 case PTRACE_PEEKTEXT: /* read word at location addr. */
Alexey Dobriyan76647322007-07-17 04:03:43 -0700130 case PTRACE_PEEKDATA:
131 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /* read the word at location addr in the USER area. */
135 case PTRACE_PEEKUSR: {
136 unsigned long tmp;
137
138 ret = -EIO;
139 if ((addr & 3) || addr < 0)
140 break;
141
142 if (addr < sizeof(struct pt_regs))
143 tmp = get_stack_long(child, addr);
144 else if ((addr >= offsetof(struct user, fpu)) &&
145 (addr < offsetof(struct user, u_fpvalid))) {
146 tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
147 } else if (addr == offsetof(struct user, u_fpvalid)) {
148 tmp = !!tsk_used_math(child);
149 } else {
150 break;
151 }
152 ret = put_user(tmp, (unsigned long *)data);
153 break;
154 }
155
156 /* when I and D space are separate, this will have to be fixed. */
157 case PTRACE_POKETEXT: /* write the word at location addr. */
158 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700159 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
161
162 case PTRACE_POKEUSR:
163 /* write the word at location addr in the USER area. We must
164 disallow any changes to certain SR bits or u_fpvalid, since
165 this could crash the kernel or result in a security
166 loophole. */
167 ret = -EIO;
168 if ((addr & 3) || addr < 0)
169 break;
170
171 if (addr < sizeof(struct pt_regs)) {
172 /* Ignore change of top 32 bits of SR */
173 if (addr == offsetof (struct pt_regs, sr)+4)
174 {
175 ret = 0;
176 break;
177 }
178 /* If lower 32 bits of SR, ignore non-user bits */
179 if (addr == offsetof (struct pt_regs, sr))
180 {
181 long cursr = get_stack_long(child, addr);
182 data &= ~(SR_MASK);
183 data |= (cursr & SR_MASK);
184 }
185 ret = put_stack_long(child, addr, data);
186 }
187 else if ((addr >= offsetof(struct user, fpu)) &&
188 (addr < offsetof(struct user, u_fpvalid))) {
189 ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
190 }
191 break;
192
193 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
194 case PTRACE_CONT: { /* restart after signal. */
195 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700196 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198 if (request == PTRACE_SYSCALL)
199 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
200 else
201 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
202 child->exit_code = data;
203 wake_up_process(child);
204 ret = 0;
205 break;
206 }
207
208/*
209 * make the child exit. Best I can do is send it a sigkill.
210 * perhaps it should be put in the status that it wants to
211 * exit.
212 */
213 case PTRACE_KILL: {
214 ret = 0;
215 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
216 break;
217 child->exit_code = SIGKILL;
218 wake_up_process(child);
219 break;
220 }
221
222 case PTRACE_SINGLESTEP: { /* set the trap flag. */
223 struct pt_regs *regs;
224
225 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700226 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
228 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
229 if ((child->ptrace & PT_DTRACE) == 0) {
230 /* Spurious delayed TF traps may occur */
231 child->ptrace |= PT_DTRACE;
232 }
233
234 regs = child->thread.uregs;
235
236 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
237
238 child->exit_code = data;
239 /* give it a chance to run. */
240 wake_up_process(child);
241 ret = 0;
242 break;
243 }
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 default:
246 ret = ptrace_request(child, request, addr, data);
247 break;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return ret;
250}
251
Christoph Hellwig481bed42005-11-07 00:59:47 -0800252asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
253{
Christoph Hellwig481bed42005-11-07 00:59:47 -0800254#define WPC_DBRMODE 0x0d104008
255 static int first_call = 1;
256
257 lock_kernel();
258 if (first_call) {
259 /* Set WPC.DBRMODE to 0. This makes all debug events get
260 * delivered through RESVEC, i.e. into the handlers in entry.S.
261 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
262 * would normally be left set to 1, which makes debug events get
263 * delivered through DBRVEC, i.e. into the remote gdb's
264 * handlers. This prevents ptrace getting them, and confuses
265 * the remote gdb.) */
266 printk("DBRMODE set to 0 to permit native debugging\n");
267 poke_real_address_q(WPC_DBRMODE, 0);
268 first_call = 0;
269 }
270 unlock_kernel();
271
272 return sys_ptrace(request, pid, addr, data);
273}
274
Paul Mundt4b27c472007-11-28 19:58:11 +0900275asmlinkage void syscall_trace(struct pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct task_struct *tsk = current;
278
Paul Mundt4b27c472007-11-28 19:58:11 +0900279 if (unlikely(current->audit_context) && entryexit)
280 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
281 regs->regs[9]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Paul Mundt4b27c472007-11-28 19:58:11 +0900283 if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
284 !test_thread_flag(TIF_SINGLESTEP))
285 goto out;
286 if (!(tsk->ptrace & PT_PTRACED))
287 goto out;
288
289 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
290 !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /*
293 * this isn't the same as continuing with a signal, but it will do
294 * for normal use. strace only continues with a signal if the
295 * stopping signal is not SIGTRAP. -brl
296 */
297 if (tsk->exit_code) {
298 send_sig(tsk->exit_code, tsk, 1);
299 tsk->exit_code = 0;
300 }
Paul Mundt4b27c472007-11-28 19:58:11 +0900301
302out:
303 if (unlikely(current->audit_context) && !entryexit)
304 audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[1],
305 regs->regs[2], regs->regs[3],
306 regs->regs[4], regs->regs[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/* Called with interrupts disabled */
310asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
311{
312 /* This is called after a single step exception (DEBUGSS).
313 There is no need to change the PC, as it is a post-execution
314 exception, as entry.S does not do anything to the PC for DEBUGSS.
315 We need to clear the Single Step setting in SR to avoid
316 continually stepping. */
317 local_irq_enable();
318 regs->sr &= ~SR_SSTEP;
319 force_sig(SIGTRAP, current);
320}
321
322/* Called with interrupts disabled */
323asmlinkage void do_software_break_point(unsigned long long vec,
324 struct pt_regs *regs)
325{
326 /* We need to forward step the PC, to counteract the backstep done
327 in signal.c. */
328 local_irq_enable();
329 force_sig(SIGTRAP, current);
330 regs->pc += 4;
331}
332
333/*
334 * Called by kernel/ptrace.c when detaching..
335 *
336 * Make sure single step bits etc are not set.
337 */
338void ptrace_disable(struct task_struct *child)
339{
340 /* nothing to do.. */
341}