blob: e15b099c1f069f8856e43caa7db2187ab129cb7e [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>
Paul Mundtc4637d42008-07-30 15:30:52 +090030#include <linux/seccomp.h>
Paul Mundtab99c732008-07-30 19:55:30 +090031#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/io.h>
33#include <asm/uaccess.h>
34#include <asm/pgtable.h>
35#include <asm/system.h>
36#include <asm/processor.h>
37#include <asm/mmu_context.h>
Paul Mundtfa439722008-09-04 18:53:58 +090038#include <asm/syscalls.h>
Adrian Bunk50387b32008-04-13 21:15:38 +030039#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* This mask defines the bits of the SR which the user is not allowed to
42 change, which are everything except S, Q, M, PR, SZ, FR. */
43#define SR_MASK (0xffff8cfd)
44
45/*
46 * does not yet catch signals sent when the child dies.
47 * in exit.c or in signal.c.
48 */
49
50/*
51 * This routine will get a word from the user area in the process kernel stack.
52 */
53static inline int get_stack_long(struct task_struct *task, int offset)
54{
55 unsigned char *stack;
56
57 stack = (unsigned char *)(task->thread.uregs);
58 stack += offset;
59 return (*((int *)stack));
60}
61
62static inline unsigned long
63get_fpu_long(struct task_struct *task, unsigned long addr)
64{
65 unsigned long tmp;
66 struct pt_regs *regs;
67 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
68
69 if (!tsk_used_math(task)) {
70 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
71 tmp = FPSCR_INIT;
72 } else {
73 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
74 }
75 return tmp;
76 }
77
78 if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +090079 enable_fpu();
Paul Mundt332fd572007-11-22 17:30:50 +090080 save_fpu(task, regs);
Paul Mundt256b22c2007-11-10 20:27:03 +090081 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 last_task_used_math = 0;
83 regs->sr |= SR_FD;
84 }
85
86 tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
87 return tmp;
88}
89
90/*
91 * This routine will put a word into the user area in the process kernel stack.
92 */
93static inline int put_stack_long(struct task_struct *task, int offset,
94 unsigned long data)
95{
96 unsigned char *stack;
97
98 stack = (unsigned char *)(task->thread.uregs);
99 stack += offset;
100 *(unsigned long *) stack = data;
101 return 0;
102}
103
104static inline int
105put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
106{
107 struct pt_regs *regs;
108
109 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
110
111 if (!tsk_used_math(task)) {
112 fpinit(&task->thread.fpu.hard);
113 set_stopped_child_used_math(task);
114 } else if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +0900115 enable_fpu();
Paul Mundt332fd572007-11-22 17:30:50 +0900116 save_fpu(task, regs);
Paul Mundt256b22c2007-11-10 20:27:03 +0900117 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 last_task_used_math = 0;
119 regs->sr |= SR_FD;
120 }
121
122 ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
123 return 0;
124}
125
Paul Mundtc459dbf2008-07-30 19:09:31 +0900126void user_enable_single_step(struct task_struct *child)
127{
128 struct pt_regs *regs = child->thread.uregs;
129
130 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
131}
132
133void user_disable_single_step(struct task_struct *child)
134{
Adrian Bunke311be52008-08-22 18:20:36 +0300135 struct pt_regs *regs = child->thread.uregs;
136
Paul Mundtc459dbf2008-07-30 19:09:31 +0900137 regs->sr &= ~SR_SSTEP;
138}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800139
140long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int ret;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* read the word at location addr in the USER area. */
146 case PTRACE_PEEKUSR: {
147 unsigned long tmp;
148
149 ret = -EIO;
150 if ((addr & 3) || addr < 0)
151 break;
152
153 if (addr < sizeof(struct pt_regs))
154 tmp = get_stack_long(child, addr);
155 else if ((addr >= offsetof(struct user, fpu)) &&
156 (addr < offsetof(struct user, u_fpvalid))) {
157 tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
158 } else if (addr == offsetof(struct user, u_fpvalid)) {
159 tmp = !!tsk_used_math(child);
160 } else {
161 break;
162 }
163 ret = put_user(tmp, (unsigned long *)data);
164 break;
165 }
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 case PTRACE_POKEUSR:
168 /* write the word at location addr in the USER area. We must
169 disallow any changes to certain SR bits or u_fpvalid, since
170 this could crash the kernel or result in a security
171 loophole. */
172 ret = -EIO;
173 if ((addr & 3) || addr < 0)
174 break;
175
176 if (addr < sizeof(struct pt_regs)) {
177 /* Ignore change of top 32 bits of SR */
178 if (addr == offsetof (struct pt_regs, sr)+4)
179 {
180 ret = 0;
181 break;
182 }
183 /* If lower 32 bits of SR, ignore non-user bits */
184 if (addr == offsetof (struct pt_regs, sr))
185 {
186 long cursr = get_stack_long(child, addr);
187 data &= ~(SR_MASK);
188 data |= (cursr & SR_MASK);
189 }
190 ret = put_stack_long(child, addr, data);
191 }
192 else if ((addr >= offsetof(struct user, fpu)) &&
193 (addr < offsetof(struct user, u_fpvalid))) {
194 ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
195 }
196 break;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 default:
199 ret = ptrace_request(child, request, addr, data);
200 break;
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return ret;
203}
204
Christoph Hellwig481bed42005-11-07 00:59:47 -0800205asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
206{
Christoph Hellwig481bed42005-11-07 00:59:47 -0800207#define WPC_DBRMODE 0x0d104008
208 static int first_call = 1;
209
210 lock_kernel();
211 if (first_call) {
212 /* Set WPC.DBRMODE to 0. This makes all debug events get
213 * delivered through RESVEC, i.e. into the handlers in entry.S.
214 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
215 * would normally be left set to 1, which makes debug events get
216 * delivered through DBRVEC, i.e. into the remote gdb's
217 * handlers. This prevents ptrace getting them, and confuses
218 * the remote gdb.) */
219 printk("DBRMODE set to 0 to permit native debugging\n");
220 poke_real_address_q(WPC_DBRMODE, 0);
221 first_call = 0;
222 }
223 unlock_kernel();
224
225 return sys_ptrace(request, pid, addr, data);
226}
227
Paul Mundt9e5e2112008-07-30 20:05:35 +0900228static inline int audit_arch(void)
229{
230 int arch = EM_SH;
231
232#ifdef CONFIG_64BIT
233 arch |= __AUDIT_ARCH_64BIT;
234#endif
235#ifdef CONFIG_CPU_LITTLE_ENDIAN
236 arch |= __AUDIT_ARCH_LE;
237#endif
238
239 return arch;
240}
241
Paul Mundtab99c732008-07-30 19:55:30 +0900242asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Paul Mundtab99c732008-07-30 19:55:30 +0900244 long long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Paul Mundtc4637d42008-07-30 15:30:52 +0900246 secure_computing(regs->regs[9]);
247
Paul Mundtab99c732008-07-30 19:55:30 +0900248 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
249 tracehook_report_syscall_entry(regs))
250 /*
251 * Tracing decided this syscall should not happen.
252 * We'll return a bogus call number to get an ENOSYS
253 * error, but leave the original number in regs->regs[0].
254 */
255 ret = -1LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Paul Mundtab99c732008-07-30 19:55:30 +0900257 if (unlikely(current->audit_context))
Paul Mundt9e5e2112008-07-30 20:05:35 +0900258 audit_syscall_entry(audit_arch(), regs->regs[1],
Paul Mundt4b27c472007-11-28 19:58:11 +0900259 regs->regs[2], regs->regs[3],
260 regs->regs[4], regs->regs[5]);
Paul Mundtab99c732008-07-30 19:55:30 +0900261
262 return ret ?: regs->regs[9];
263}
264
265asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
266{
267 if (unlikely(current->audit_context))
268 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
269 regs->regs[9]);
270
271 if (test_thread_flag(TIF_SYSCALL_TRACE))
272 tracehook_report_syscall_exit(regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/* Called with interrupts disabled */
276asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
277{
278 /* This is called after a single step exception (DEBUGSS).
279 There is no need to change the PC, as it is a post-execution
280 exception, as entry.S does not do anything to the PC for DEBUGSS.
281 We need to clear the Single Step setting in SR to avoid
282 continually stepping. */
283 local_irq_enable();
284 regs->sr &= ~SR_SSTEP;
285 force_sig(SIGTRAP, current);
286}
287
288/* Called with interrupts disabled */
289asmlinkage void do_software_break_point(unsigned long long vec,
290 struct pt_regs *regs)
291{
292 /* We need to forward step the PC, to counteract the backstep done
293 in signal.c. */
294 local_irq_enable();
295 force_sig(SIGTRAP, current);
296 regs->pc += 4;
297}
298
299/*
300 * Called by kernel/ptrace.c when detaching..
301 *
302 * Make sure single step bits etc are not set.
303 */
304void ptrace_disable(struct task_struct *child)
305{
Paul Mundtc459dbf2008-07-30 19:09:31 +0900306 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}