blob: 108f3962e39a36e9b6c48e00933fcf26cba7bd45 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
32#include <asm/uaccess.h>
33#include <asm/pgtable.h>
34#include <asm/system.h>
35#include <asm/processor.h>
36#include <asm/mmu_context.h>
Adrian Bunk50387b32008-04-13 21:15:38 +030037#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* This mask defines the bits of the SR which the user is not allowed to
40 change, which are everything except S, Q, M, PR, SZ, FR. */
41#define SR_MASK (0xffff8cfd)
42
43/*
44 * does not yet catch signals sent when the child dies.
45 * in exit.c or in signal.c.
46 */
47
48/*
49 * This routine will get a word from the user area in the process kernel stack.
50 */
51static inline int get_stack_long(struct task_struct *task, int offset)
52{
53 unsigned char *stack;
54
55 stack = (unsigned char *)(task->thread.uregs);
56 stack += offset;
57 return (*((int *)stack));
58}
59
60static inline unsigned long
61get_fpu_long(struct task_struct *task, unsigned long addr)
62{
63 unsigned long tmp;
64 struct pt_regs *regs;
65 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
66
67 if (!tsk_used_math(task)) {
68 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
69 tmp = FPSCR_INIT;
70 } else {
71 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
72 }
73 return tmp;
74 }
75
76 if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +090077 enable_fpu();
Paul Mundt332fd572007-11-22 17:30:50 +090078 save_fpu(task, regs);
Paul Mundt256b22c2007-11-10 20:27:03 +090079 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 last_task_used_math = 0;
81 regs->sr |= SR_FD;
82 }
83
84 tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
85 return tmp;
86}
87
88/*
89 * This routine will put a word into the user area in the process kernel stack.
90 */
91static inline int put_stack_long(struct task_struct *task, int offset,
92 unsigned long data)
93{
94 unsigned char *stack;
95
96 stack = (unsigned char *)(task->thread.uregs);
97 stack += offset;
98 *(unsigned long *) stack = data;
99 return 0;
100}
101
102static inline int
103put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
104{
105 struct pt_regs *regs;
106
107 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
108
109 if (!tsk_used_math(task)) {
110 fpinit(&task->thread.fpu.hard);
111 set_stopped_child_used_math(task);
112 } else if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +0900113 enable_fpu();
Paul Mundt332fd572007-11-22 17:30:50 +0900114 save_fpu(task, regs);
Paul Mundt256b22c2007-11-10 20:27:03 +0900115 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 last_task_used_math = 0;
117 regs->sr |= SR_FD;
118 }
119
120 ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
121 return 0;
122}
123
Paul Mundtc459dbf2008-07-30 19:09:31 +0900124void user_enable_single_step(struct task_struct *child)
125{
126 struct pt_regs *regs = child->thread.uregs;
127
128 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
129}
130
131void user_disable_single_step(struct task_struct *child)
132{
133 regs->sr &= ~SR_SSTEP;
134}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800135
136long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int ret;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* read the word at location addr in the USER area. */
142 case PTRACE_PEEKUSR: {
143 unsigned long tmp;
144
145 ret = -EIO;
146 if ((addr & 3) || addr < 0)
147 break;
148
149 if (addr < sizeof(struct pt_regs))
150 tmp = get_stack_long(child, addr);
151 else if ((addr >= offsetof(struct user, fpu)) &&
152 (addr < offsetof(struct user, u_fpvalid))) {
153 tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
154 } else if (addr == offsetof(struct user, u_fpvalid)) {
155 tmp = !!tsk_used_math(child);
156 } else {
157 break;
158 }
159 ret = put_user(tmp, (unsigned long *)data);
160 break;
161 }
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 case PTRACE_POKEUSR:
164 /* write the word at location addr in the USER area. We must
165 disallow any changes to certain SR bits or u_fpvalid, since
166 this could crash the kernel or result in a security
167 loophole. */
168 ret = -EIO;
169 if ((addr & 3) || addr < 0)
170 break;
171
172 if (addr < sizeof(struct pt_regs)) {
173 /* Ignore change of top 32 bits of SR */
174 if (addr == offsetof (struct pt_regs, sr)+4)
175 {
176 ret = 0;
177 break;
178 }
179 /* If lower 32 bits of SR, ignore non-user bits */
180 if (addr == offsetof (struct pt_regs, sr))
181 {
182 long cursr = get_stack_long(child, addr);
183 data &= ~(SR_MASK);
184 data |= (cursr & SR_MASK);
185 }
186 ret = put_stack_long(child, addr, data);
187 }
188 else if ((addr >= offsetof(struct user, fpu)) &&
189 (addr < offsetof(struct user, u_fpvalid))) {
190 ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
191 }
192 break;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 default:
195 ret = ptrace_request(child, request, addr, data);
196 break;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return ret;
199}
200
Christoph Hellwig481bed42005-11-07 00:59:47 -0800201asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
202{
Christoph Hellwig481bed42005-11-07 00:59:47 -0800203#define WPC_DBRMODE 0x0d104008
204 static int first_call = 1;
205
206 lock_kernel();
207 if (first_call) {
208 /* Set WPC.DBRMODE to 0. This makes all debug events get
209 * delivered through RESVEC, i.e. into the handlers in entry.S.
210 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
211 * would normally be left set to 1, which makes debug events get
212 * delivered through DBRVEC, i.e. into the remote gdb's
213 * handlers. This prevents ptrace getting them, and confuses
214 * the remote gdb.) */
215 printk("DBRMODE set to 0 to permit native debugging\n");
216 poke_real_address_q(WPC_DBRMODE, 0);
217 first_call = 0;
218 }
219 unlock_kernel();
220
221 return sys_ptrace(request, pid, addr, data);
222}
223
Paul Mundt4b27c472007-11-28 19:58:11 +0900224asmlinkage void syscall_trace(struct pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct task_struct *tsk = current;
227
Paul Mundtc4637d42008-07-30 15:30:52 +0900228 secure_computing(regs->regs[9]);
229
Paul Mundt4b27c472007-11-28 19:58:11 +0900230 if (unlikely(current->audit_context) && entryexit)
231 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
232 regs->regs[9]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Paul Mundt4b27c472007-11-28 19:58:11 +0900234 if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
235 !test_thread_flag(TIF_SINGLESTEP))
236 goto out;
237 if (!(tsk->ptrace & PT_PTRACED))
238 goto out;
239
240 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
241 !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /*
244 * this isn't the same as continuing with a signal, but it will do
245 * for normal use. strace only continues with a signal if the
246 * stopping signal is not SIGTRAP. -brl
247 */
248 if (tsk->exit_code) {
249 send_sig(tsk->exit_code, tsk, 1);
250 tsk->exit_code = 0;
251 }
Paul Mundt4b27c472007-11-28 19:58:11 +0900252
253out:
254 if (unlikely(current->audit_context) && !entryexit)
255 audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[1],
256 regs->regs[2], regs->regs[3],
257 regs->regs[4], regs->regs[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/* Called with interrupts disabled */
261asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
262{
263 /* This is called after a single step exception (DEBUGSS).
264 There is no need to change the PC, as it is a post-execution
265 exception, as entry.S does not do anything to the PC for DEBUGSS.
266 We need to clear the Single Step setting in SR to avoid
267 continually stepping. */
268 local_irq_enable();
269 regs->sr &= ~SR_SSTEP;
270 force_sig(SIGTRAP, current);
271}
272
273/* Called with interrupts disabled */
274asmlinkage void do_software_break_point(unsigned long long vec,
275 struct pt_regs *regs)
276{
277 /* We need to forward step the PC, to counteract the backstep done
278 in signal.c. */
279 local_irq_enable();
280 force_sig(SIGTRAP, current);
281 regs->pc += 4;
282}
283
284/*
285 * Called by kernel/ptrace.c when detaching..
286 *
287 * Make sure single step bits etc are not set.
288 */
289void ptrace_disable(struct task_struct *child)
290{
Paul Mundtc459dbf2008-07-30 19:09:31 +0900291 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}