blob: 035cb300d3dcf245b894e86ac39931cbdeee696f [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
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +09009 * Audit support: Yuichi Nakamura <ynakam@hitachisoft.jp>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
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>
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +090022#include <linux/audit.h>
Paul Mundtc4637d42008-07-30 15:30:52 +090023#include <linux/seccomp.h>
Paul Mundtab99c732008-07-30 19:55:30 +090024#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
26#include <asm/pgtable.h>
27#include <asm/system.h>
28#include <asm/processor.h>
29#include <asm/mmu_context.h>
30
31/*
32 * does not yet catch signals sent when the child dies.
33 * in exit.c or in signal.c.
34 */
35
36/*
37 * This routine will get a word off of the process kernel stack.
38 */
39static inline int get_stack_long(struct task_struct *task, int offset)
40{
41 unsigned char *stack;
42
Al Viro3cf0f4e2006-01-12 01:05:44 -080043 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 stack += offset;
45 return (*((int *)stack));
46}
47
48/*
49 * This routine will put a word on the process kernel stack.
50 */
51static inline int put_stack_long(struct task_struct *task, int offset,
52 unsigned long data)
53{
54 unsigned char *stack;
55
Al Viro3cf0f4e2006-01-12 01:05:44 -080056 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 stack += offset;
58 *(unsigned long *) stack = data;
59 return 0;
60}
61
Paul Mundtc459dbf2008-07-30 19:09:31 +090062void user_enable_single_step(struct task_struct *child)
63{
64 struct pt_regs *regs = task_pt_regs(child);
65 long pc;
66
67 pc = get_stack_long(child, (long)&regs->pc);
68
69 /* Next scheduling will set up UBC */
70 if (child->thread.ubc_pc == 0)
71 ubc_usercnt += 1;
72
73 child->thread.ubc_pc = pc;
74
75 set_tsk_thread_flag(child, TIF_SINGLESTEP);
76}
77
78void user_disable_single_step(struct task_struct *child)
Stuart Menefy9432f962007-02-23 13:22:17 +090079{
80 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
81
82 /*
83 * Ensure the UBC is not programmed at the next context switch.
84 *
85 * Normally this is not needed but there are sequences such as
86 * singlestep, signal delivery, and continue that leave the
87 * ubc_pc non-zero leading to spurious SIGTRAPs.
88 */
89 if (child->thread.ubc_pc != 0) {
90 ubc_usercnt -= 1;
91 child->thread.ubc_pc = 0;
92 }
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * Called by kernel/ptrace.c when detaching..
97 *
98 * Make sure single step bits etc are not set.
99 */
100void ptrace_disable(struct task_struct *child)
101{
Paul Mundtc459dbf2008-07-30 19:09:31 +0900102 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Christoph Hellwig481bed42005-11-07 00:59:47 -0800105long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct user * dummy = NULL;
108 int ret;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* read the word at location addr in the USER area. */
112 case PTRACE_PEEKUSR: {
113 unsigned long tmp;
114
115 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900116 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 addr > sizeof(struct user) - 3)
118 break;
119
120 if (addr < sizeof(struct pt_regs))
121 tmp = get_stack_long(child, addr);
122 else if (addr >= (long) &dummy->fpu &&
123 addr < (long) &dummy->u_fpvalid) {
124 if (!tsk_used_math(child)) {
125 if (addr == (long)&dummy->fpu.fpscr)
126 tmp = FPSCR_INIT;
127 else
128 tmp = 0;
129 } else
130 tmp = ((long *)&child->thread.fpu)
131 [(addr - (long)&dummy->fpu) >> 2];
132 } else if (addr == (long) &dummy->u_fpvalid)
133 tmp = !!tsk_used_math(child);
134 else
135 tmp = 0;
Paul Mundte08f4572007-05-14 12:52:56 +0900136 ret = put_user(tmp, (unsigned long __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 break;
138 }
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
141 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900142 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 addr > sizeof(struct user) - 3)
144 break;
145
146 if (addr < sizeof(struct pt_regs))
147 ret = put_stack_long(child, addr, data);
148 else if (addr >= (long) &dummy->fpu &&
149 addr < (long) &dummy->u_fpvalid) {
150 set_stopped_child_used_math(child);
151 ((long *)&child->thread.fpu)
152 [(addr - (long)&dummy->fpu) >> 2] = data;
153 ret = 0;
154 } else if (addr == (long) &dummy->u_fpvalid) {
155 conditional_stopped_child_used_math(data, child);
156 ret = 0;
157 }
158 break;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#ifdef CONFIG_SH_DSP
161 case PTRACE_GETDSPREGS: {
162 unsigned long dp;
163
164 ret = -EIO;
165 dp = ((unsigned long) child) + THREAD_SIZE -
166 sizeof(struct pt_dspregs);
167 if (*((int *) (dp - 4)) == SR_FD) {
Magnus Damm09061852008-02-08 17:26:54 +0900168 copy_to_user((void *)addr, (void *) dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 sizeof(struct pt_dspregs));
170 ret = 0;
171 }
172 break;
173 }
174
175 case PTRACE_SETDSPREGS: {
176 unsigned long dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 ret = -EIO;
179 dp = ((unsigned long) child) + THREAD_SIZE -
180 sizeof(struct pt_dspregs);
181 if (*((int *) (dp - 4)) == SR_FD) {
Magnus Damm09061852008-02-08 17:26:54 +0900182 copy_from_user((void *) dp, (void *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 sizeof(struct pt_dspregs));
184 ret = 0;
185 }
186 break;
187 }
188#endif
Paul Mundt3bc24a12008-05-19 13:40:12 +0900189#ifdef CONFIG_BINFMT_ELF_FDPIC
190 case PTRACE_GETFDPIC: {
191 unsigned long tmp = 0;
192
193 switch (addr) {
194 case PTRACE_GETFDPIC_EXEC:
195 tmp = child->mm->context.exec_fdpic_loadmap;
196 break;
197 case PTRACE_GETFDPIC_INTERP:
198 tmp = child->mm->context.interp_fdpic_loadmap;
199 break;
200 default:
201 break;
202 }
203
204 ret = 0;
205 if (put_user(tmp, (unsigned long *) data)) {
206 ret = -EFAULT;
207 break;
208 }
209 break;
210 }
211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 default:
213 ret = ptrace_request(child, request, addr, data);
214 break;
215 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return ret;
218}
219
Paul Mundt9e5e2112008-07-30 20:05:35 +0900220static inline int audit_arch(void)
221{
222 int arch = EM_SH;
223
224#ifdef CONFIG_CPU_LITTLE_ENDIAN
225 arch |= __AUDIT_ARCH_LE;
226#endif
227
228 return arch;
229}
230
Paul Mundtab99c732008-07-30 19:55:30 +0900231asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Paul Mundtab99c732008-07-30 19:55:30 +0900233 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Paul Mundtc4637d42008-07-30 15:30:52 +0900235 secure_computing(regs->regs[0]);
236
Paul Mundtab99c732008-07-30 19:55:30 +0900237 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
238 tracehook_report_syscall_entry(regs))
239 /*
240 * Tracing decided this syscall should not happen.
241 * We'll return a bogus call number to get an ENOSYS
242 * error, but leave the original number in regs->regs[0].
243 */
244 ret = -1L;
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900245
Paul Mundtab99c732008-07-30 19:55:30 +0900246 if (unlikely(current->audit_context))
Paul Mundt9e5e2112008-07-30 20:05:35 +0900247 audit_syscall_entry(audit_arch(), regs->regs[3],
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900248 regs->regs[4], regs->regs[5],
249 regs->regs[6], regs->regs[7]);
250
Paul Mundtab99c732008-07-30 19:55:30 +0900251 return ret ?: regs->regs[0];
252}
253
254asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
255{
256 int step;
257
258 if (unlikely(current->audit_context))
259 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
260 regs->regs[0]);
261
262 step = test_thread_flag(TIF_SINGLESTEP);
263 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
264 tracehook_report_syscall_exit(regs, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}