blob: 84bf3420597c77c861a1af55721e9c349b12e23b [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>
Paul Mundtfa439722008-09-04 18:53:58 +090030#include <asm/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * does not yet catch signals sent when the child dies.
34 * in exit.c or in signal.c.
35 */
36
37/*
38 * This routine will get a word off of the process kernel stack.
39 */
40static inline int get_stack_long(struct task_struct *task, int offset)
41{
42 unsigned char *stack;
43
Al Viro3cf0f4e2006-01-12 01:05:44 -080044 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 stack += offset;
46 return (*((int *)stack));
47}
48
49/*
50 * This routine will put a word on the process kernel stack.
51 */
52static inline int put_stack_long(struct task_struct *task, int offset,
53 unsigned long data)
54{
55 unsigned char *stack;
56
Al Viro3cf0f4e2006-01-12 01:05:44 -080057 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 stack += offset;
59 *(unsigned long *) stack = data;
60 return 0;
61}
62
Paul Mundtc459dbf2008-07-30 19:09:31 +090063void user_enable_single_step(struct task_struct *child)
64{
65 struct pt_regs *regs = task_pt_regs(child);
66 long pc;
67
68 pc = get_stack_long(child, (long)&regs->pc);
69
70 /* Next scheduling will set up UBC */
71 if (child->thread.ubc_pc == 0)
72 ubc_usercnt += 1;
73
74 child->thread.ubc_pc = pc;
75
76 set_tsk_thread_flag(child, TIF_SINGLESTEP);
77}
78
79void user_disable_single_step(struct task_struct *child)
Stuart Menefy9432f962007-02-23 13:22:17 +090080{
81 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
82
83 /*
84 * Ensure the UBC is not programmed at the next context switch.
85 *
86 * Normally this is not needed but there are sequences such as
87 * singlestep, signal delivery, and continue that leave the
88 * ubc_pc non-zero leading to spurious SIGTRAPs.
89 */
90 if (child->thread.ubc_pc != 0) {
91 ubc_usercnt -= 1;
92 child->thread.ubc_pc = 0;
93 }
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * Called by kernel/ptrace.c when detaching..
98 *
99 * Make sure single step bits etc are not set.
100 */
101void ptrace_disable(struct task_struct *child)
102{
Paul Mundtc459dbf2008-07-30 19:09:31 +0900103 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Christoph Hellwig481bed42005-11-07 00:59:47 -0800106long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 struct user * dummy = NULL;
Paul Mundtfa439722008-09-04 18:53:58 +0900109 unsigned long __user *datap = (unsigned long __user *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int ret;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* read the word at location addr in the USER area. */
114 case PTRACE_PEEKUSR: {
115 unsigned long tmp;
116
117 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900118 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 addr > sizeof(struct user) - 3)
120 break;
121
122 if (addr < sizeof(struct pt_regs))
123 tmp = get_stack_long(child, addr);
124 else if (addr >= (long) &dummy->fpu &&
125 addr < (long) &dummy->u_fpvalid) {
126 if (!tsk_used_math(child)) {
127 if (addr == (long)&dummy->fpu.fpscr)
128 tmp = FPSCR_INIT;
129 else
130 tmp = 0;
131 } else
132 tmp = ((long *)&child->thread.fpu)
133 [(addr - (long)&dummy->fpu) >> 2];
134 } else if (addr == (long) &dummy->u_fpvalid)
135 tmp = !!tsk_used_math(child);
136 else
137 tmp = 0;
Paul Mundtfa439722008-09-04 18:53:58 +0900138 ret = put_user(tmp, datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 break;
140 }
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
143 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900144 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 addr > sizeof(struct user) - 3)
146 break;
147
148 if (addr < sizeof(struct pt_regs))
149 ret = put_stack_long(child, addr, data);
150 else if (addr >= (long) &dummy->fpu &&
151 addr < (long) &dummy->u_fpvalid) {
152 set_stopped_child_used_math(child);
153 ((long *)&child->thread.fpu)
154 [(addr - (long)&dummy->fpu) >> 2] = data;
155 ret = 0;
156 } else if (addr == (long) &dummy->u_fpvalid) {
157 conditional_stopped_child_used_math(data, child);
158 ret = 0;
159 }
160 break;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#ifdef CONFIG_SH_DSP
163 case PTRACE_GETDSPREGS: {
164 unsigned long dp;
165
166 ret = -EIO;
167 dp = ((unsigned long) child) + THREAD_SIZE -
168 sizeof(struct pt_dspregs);
169 if (*((int *) (dp - 4)) == SR_FD) {
Magnus Damm09061852008-02-08 17:26:54 +0900170 copy_to_user((void *)addr, (void *) dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 sizeof(struct pt_dspregs));
172 ret = 0;
173 }
174 break;
175 }
176
177 case PTRACE_SETDSPREGS: {
178 unsigned long dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 ret = -EIO;
181 dp = ((unsigned long) child) + THREAD_SIZE -
182 sizeof(struct pt_dspregs);
183 if (*((int *) (dp - 4)) == SR_FD) {
Magnus Damm09061852008-02-08 17:26:54 +0900184 copy_from_user((void *) dp, (void *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 sizeof(struct pt_dspregs));
186 ret = 0;
187 }
188 break;
189 }
190#endif
Paul Mundt3bc24a12008-05-19 13:40:12 +0900191#ifdef CONFIG_BINFMT_ELF_FDPIC
192 case PTRACE_GETFDPIC: {
193 unsigned long tmp = 0;
194
195 switch (addr) {
196 case PTRACE_GETFDPIC_EXEC:
197 tmp = child->mm->context.exec_fdpic_loadmap;
198 break;
199 case PTRACE_GETFDPIC_INTERP:
200 tmp = child->mm->context.interp_fdpic_loadmap;
201 break;
202 default:
203 break;
204 }
205
206 ret = 0;
Paul Mundtfa439722008-09-04 18:53:58 +0900207 if (put_user(tmp, datap)) {
Paul Mundt3bc24a12008-05-19 13:40:12 +0900208 ret = -EFAULT;
209 break;
210 }
211 break;
212 }
213#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 default:
215 ret = ptrace_request(child, request, addr, data);
216 break;
217 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return ret;
220}
221
Paul Mundt9e5e2112008-07-30 20:05:35 +0900222static inline int audit_arch(void)
223{
224 int arch = EM_SH;
225
226#ifdef CONFIG_CPU_LITTLE_ENDIAN
227 arch |= __AUDIT_ARCH_LE;
228#endif
229
230 return arch;
231}
232
Paul Mundtab99c732008-07-30 19:55:30 +0900233asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Paul Mundtab99c732008-07-30 19:55:30 +0900235 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Paul Mundtc4637d42008-07-30 15:30:52 +0900237 secure_computing(regs->regs[0]);
238
Paul Mundtab99c732008-07-30 19:55:30 +0900239 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
240 tracehook_report_syscall_entry(regs))
241 /*
242 * Tracing decided this syscall should not happen.
243 * We'll return a bogus call number to get an ENOSYS
244 * error, but leave the original number in regs->regs[0].
245 */
246 ret = -1L;
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900247
Paul Mundtab99c732008-07-30 19:55:30 +0900248 if (unlikely(current->audit_context))
Paul Mundt9e5e2112008-07-30 20:05:35 +0900249 audit_syscall_entry(audit_arch(), regs->regs[3],
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900250 regs->regs[4], regs->regs[5],
251 regs->regs[6], regs->regs[7]);
252
Paul Mundtab99c732008-07-30 19:55:30 +0900253 return ret ?: regs->regs[0];
254}
255
256asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
257{
258 int step;
259
260 if (unlikely(current->audit_context))
261 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
262 regs->regs[0]);
263
264 step = test_thread_flag(TIF_SINGLESTEP);
265 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
266 tracehook_report_syscall_exit(regs, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}