blob: 236d8bef9ccd0947d196ee5f8ddb4a9274f137fc [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>
Adrian Bunk50387b32008-04-13 21:15:38 +030038#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* This mask defines the bits of the SR which the user is not allowed to
41 change, which are everything except S, Q, M, PR, SZ, FR. */
42#define SR_MASK (0xffff8cfd)
43
44/*
45 * does not yet catch signals sent when the child dies.
46 * in exit.c or in signal.c.
47 */
48
49/*
50 * This routine will get a word from the user area in the process kernel stack.
51 */
52static inline int get_stack_long(struct task_struct *task, int offset)
53{
54 unsigned char *stack;
55
56 stack = (unsigned char *)(task->thread.uregs);
57 stack += offset;
58 return (*((int *)stack));
59}
60
61static inline unsigned long
62get_fpu_long(struct task_struct *task, unsigned long addr)
63{
64 unsigned long tmp;
65 struct pt_regs *regs;
66 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
67
68 if (!tsk_used_math(task)) {
69 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
70 tmp = FPSCR_INIT;
71 } else {
72 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
73 }
74 return tmp;
75 }
76
77 if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +090078 enable_fpu();
Paul Mundt332fd572007-11-22 17:30:50 +090079 save_fpu(task, regs);
Paul Mundt256b22c2007-11-10 20:27:03 +090080 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 last_task_used_math = 0;
82 regs->sr |= SR_FD;
83 }
84
85 tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
86 return tmp;
87}
88
89/*
90 * This routine will put a word into the user area in the process kernel stack.
91 */
92static inline int put_stack_long(struct task_struct *task, int offset,
93 unsigned long data)
94{
95 unsigned char *stack;
96
97 stack = (unsigned char *)(task->thread.uregs);
98 stack += offset;
99 *(unsigned long *) stack = data;
100 return 0;
101}
102
103static inline int
104put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
105{
106 struct pt_regs *regs;
107
108 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
109
110 if (!tsk_used_math(task)) {
111 fpinit(&task->thread.fpu.hard);
112 set_stopped_child_used_math(task);
113 } else if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +0900114 enable_fpu();
Paul Mundt332fd572007-11-22 17:30:50 +0900115 save_fpu(task, regs);
Paul Mundt256b22c2007-11-10 20:27:03 +0900116 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 last_task_used_math = 0;
118 regs->sr |= SR_FD;
119 }
120
121 ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
122 return 0;
123}
124
Paul Mundtc459dbf2008-07-30 19:09:31 +0900125void user_enable_single_step(struct task_struct *child)
126{
127 struct pt_regs *regs = child->thread.uregs;
128
129 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
130}
131
132void user_disable_single_step(struct task_struct *child)
133{
134 regs->sr &= ~SR_SSTEP;
135}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800136
137long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int ret;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* read the word at location addr in the USER area. */
143 case PTRACE_PEEKUSR: {
144 unsigned long tmp;
145
146 ret = -EIO;
147 if ((addr & 3) || addr < 0)
148 break;
149
150 if (addr < sizeof(struct pt_regs))
151 tmp = get_stack_long(child, addr);
152 else if ((addr >= offsetof(struct user, fpu)) &&
153 (addr < offsetof(struct user, u_fpvalid))) {
154 tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
155 } else if (addr == offsetof(struct user, u_fpvalid)) {
156 tmp = !!tsk_used_math(child);
157 } else {
158 break;
159 }
160 ret = put_user(tmp, (unsigned long *)data);
161 break;
162 }
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 case PTRACE_POKEUSR:
165 /* write the word at location addr in the USER area. We must
166 disallow any changes to certain SR bits or u_fpvalid, since
167 this could crash the kernel or result in a security
168 loophole. */
169 ret = -EIO;
170 if ((addr & 3) || addr < 0)
171 break;
172
173 if (addr < sizeof(struct pt_regs)) {
174 /* Ignore change of top 32 bits of SR */
175 if (addr == offsetof (struct pt_regs, sr)+4)
176 {
177 ret = 0;
178 break;
179 }
180 /* If lower 32 bits of SR, ignore non-user bits */
181 if (addr == offsetof (struct pt_regs, sr))
182 {
183 long cursr = get_stack_long(child, addr);
184 data &= ~(SR_MASK);
185 data |= (cursr & SR_MASK);
186 }
187 ret = put_stack_long(child, addr, data);
188 }
189 else if ((addr >= offsetof(struct user, fpu)) &&
190 (addr < offsetof(struct user, u_fpvalid))) {
191 ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
192 }
193 break;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 default:
196 ret = ptrace_request(child, request, addr, data);
197 break;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return ret;
200}
201
Christoph Hellwig481bed42005-11-07 00:59:47 -0800202asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
203{
Christoph Hellwig481bed42005-11-07 00:59:47 -0800204#define WPC_DBRMODE 0x0d104008
205 static int first_call = 1;
206
207 lock_kernel();
208 if (first_call) {
209 /* Set WPC.DBRMODE to 0. This makes all debug events get
210 * delivered through RESVEC, i.e. into the handlers in entry.S.
211 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
212 * would normally be left set to 1, which makes debug events get
213 * delivered through DBRVEC, i.e. into the remote gdb's
214 * handlers. This prevents ptrace getting them, and confuses
215 * the remote gdb.) */
216 printk("DBRMODE set to 0 to permit native debugging\n");
217 poke_real_address_q(WPC_DBRMODE, 0);
218 first_call = 0;
219 }
220 unlock_kernel();
221
222 return sys_ptrace(request, pid, addr, data);
223}
224
Paul Mundtab99c732008-07-30 19:55:30 +0900225asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Paul Mundtab99c732008-07-30 19:55:30 +0900227 long long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Paul Mundtc4637d42008-07-30 15:30:52 +0900229 secure_computing(regs->regs[9]);
230
Paul Mundtab99c732008-07-30 19:55:30 +0900231 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
232 tracehook_report_syscall_entry(regs))
233 /*
234 * Tracing decided this syscall should not happen.
235 * We'll return a bogus call number to get an ENOSYS
236 * error, but leave the original number in regs->regs[0].
237 */
238 ret = -1LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Paul Mundtab99c732008-07-30 19:55:30 +0900240 if (unlikely(current->audit_context))
Paul Mundt4b27c472007-11-28 19:58:11 +0900241 audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[1],
242 regs->regs[2], regs->regs[3],
243 regs->regs[4], regs->regs[5]);
Paul Mundtab99c732008-07-30 19:55:30 +0900244
245 return ret ?: regs->regs[9];
246}
247
248asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
249{
250 if (unlikely(current->audit_context))
251 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
252 regs->regs[9]);
253
254 if (test_thread_flag(TIF_SYSCALL_TRACE))
255 tracehook_report_syscall_exit(regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/* Called with interrupts disabled */
259asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
260{
261 /* This is called after a single step exception (DEBUGSS).
262 There is no need to change the PC, as it is a post-execution
263 exception, as entry.S does not do anything to the PC for DEBUGSS.
264 We need to clear the Single Step setting in SR to avoid
265 continually stepping. */
266 local_irq_enable();
267 regs->sr &= ~SR_SSTEP;
268 force_sig(SIGTRAP, current);
269}
270
271/* Called with interrupts disabled */
272asmlinkage void do_software_break_point(unsigned long long vec,
273 struct pt_regs *regs)
274{
275 /* We need to forward step the PC, to counteract the backstep done
276 in signal.c. */
277 local_irq_enable();
278 force_sig(SIGTRAP, current);
279 regs->pc += 4;
280}
281
282/*
283 * Called by kernel/ptrace.c when detaching..
284 *
285 * Make sure single step bits etc are not set.
286 */
287void ptrace_disable(struct task_struct *child)
288{
Paul Mundtc459dbf2008-07-30 19:09:31 +0900289 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}