blob: ff66f97c564d90b24fd79bca950a7a71366b5636 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
25#include <asm/pgtable.h>
26#include <asm/system.h>
27#include <asm/processor.h>
28#include <asm/mmu_context.h>
29
30/*
31 * does not yet catch signals sent when the child dies.
32 * in exit.c or in signal.c.
33 */
34
35/*
36 * This routine will get a word off of the process kernel stack.
37 */
38static inline int get_stack_long(struct task_struct *task, int offset)
39{
40 unsigned char *stack;
41
Al Viro3cf0f4e2006-01-12 01:05:44 -080042 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 stack += offset;
44 return (*((int *)stack));
45}
46
47/*
48 * This routine will put a word on the process kernel stack.
49 */
50static inline int put_stack_long(struct task_struct *task, int offset,
51 unsigned long data)
52{
53 unsigned char *stack;
54
Al Viro3cf0f4e2006-01-12 01:05:44 -080055 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 stack += offset;
57 *(unsigned long *) stack = data;
58 return 0;
59}
60
Paul Mundtc459dbf2008-07-30 19:09:31 +090061void user_enable_single_step(struct task_struct *child)
62{
63 struct pt_regs *regs = task_pt_regs(child);
64 long pc;
65
66 pc = get_stack_long(child, (long)&regs->pc);
67
68 /* Next scheduling will set up UBC */
69 if (child->thread.ubc_pc == 0)
70 ubc_usercnt += 1;
71
72 child->thread.ubc_pc = pc;
73
74 set_tsk_thread_flag(child, TIF_SINGLESTEP);
75}
76
77void user_disable_single_step(struct task_struct *child)
Stuart Menefy9432f962007-02-23 13:22:17 +090078{
79 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
80
81 /*
82 * Ensure the UBC is not programmed at the next context switch.
83 *
84 * Normally this is not needed but there are sequences such as
85 * singlestep, signal delivery, and continue that leave the
86 * ubc_pc non-zero leading to spurious SIGTRAPs.
87 */
88 if (child->thread.ubc_pc != 0) {
89 ubc_usercnt -= 1;
90 child->thread.ubc_pc = 0;
91 }
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
95 * Called by kernel/ptrace.c when detaching..
96 *
97 * Make sure single step bits etc are not set.
98 */
99void ptrace_disable(struct task_struct *child)
100{
Paul Mundtc459dbf2008-07-30 19:09:31 +0900101 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Christoph Hellwig481bed42005-11-07 00:59:47 -0800104long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct user * dummy = NULL;
107 int ret;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* read the word at location addr in the USER area. */
111 case PTRACE_PEEKUSR: {
112 unsigned long tmp;
113
114 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900115 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 addr > sizeof(struct user) - 3)
117 break;
118
119 if (addr < sizeof(struct pt_regs))
120 tmp = get_stack_long(child, addr);
121 else if (addr >= (long) &dummy->fpu &&
122 addr < (long) &dummy->u_fpvalid) {
123 if (!tsk_used_math(child)) {
124 if (addr == (long)&dummy->fpu.fpscr)
125 tmp = FPSCR_INIT;
126 else
127 tmp = 0;
128 } else
129 tmp = ((long *)&child->thread.fpu)
130 [(addr - (long)&dummy->fpu) >> 2];
131 } else if (addr == (long) &dummy->u_fpvalid)
132 tmp = !!tsk_used_math(child);
133 else
134 tmp = 0;
Paul Mundte08f4572007-05-14 12:52:56 +0900135 ret = put_user(tmp, (unsigned long __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 break;
137 }
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
140 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900141 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 addr > sizeof(struct user) - 3)
143 break;
144
145 if (addr < sizeof(struct pt_regs))
146 ret = put_stack_long(child, addr, data);
147 else if (addr >= (long) &dummy->fpu &&
148 addr < (long) &dummy->u_fpvalid) {
149 set_stopped_child_used_math(child);
150 ((long *)&child->thread.fpu)
151 [(addr - (long)&dummy->fpu) >> 2] = data;
152 ret = 0;
153 } else if (addr == (long) &dummy->u_fpvalid) {
154 conditional_stopped_child_used_math(data, child);
155 ret = 0;
156 }
157 break;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#ifdef CONFIG_SH_DSP
160 case PTRACE_GETDSPREGS: {
161 unsigned long dp;
162
163 ret = -EIO;
164 dp = ((unsigned long) child) + THREAD_SIZE -
165 sizeof(struct pt_dspregs);
166 if (*((int *) (dp - 4)) == SR_FD) {
Magnus Damm09061852008-02-08 17:26:54 +0900167 copy_to_user((void *)addr, (void *) dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 sizeof(struct pt_dspregs));
169 ret = 0;
170 }
171 break;
172 }
173
174 case PTRACE_SETDSPREGS: {
175 unsigned long dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 ret = -EIO;
178 dp = ((unsigned long) child) + THREAD_SIZE -
179 sizeof(struct pt_dspregs);
180 if (*((int *) (dp - 4)) == SR_FD) {
Magnus Damm09061852008-02-08 17:26:54 +0900181 copy_from_user((void *) dp, (void *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 sizeof(struct pt_dspregs));
183 ret = 0;
184 }
185 break;
186 }
187#endif
Paul Mundt3bc24a12008-05-19 13:40:12 +0900188#ifdef CONFIG_BINFMT_ELF_FDPIC
189 case PTRACE_GETFDPIC: {
190 unsigned long tmp = 0;
191
192 switch (addr) {
193 case PTRACE_GETFDPIC_EXEC:
194 tmp = child->mm->context.exec_fdpic_loadmap;
195 break;
196 case PTRACE_GETFDPIC_INTERP:
197 tmp = child->mm->context.interp_fdpic_loadmap;
198 break;
199 default:
200 break;
201 }
202
203 ret = 0;
204 if (put_user(tmp, (unsigned long *) data)) {
205 ret = -EFAULT;
206 break;
207 }
208 break;
209 }
210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 default:
212 ret = ptrace_request(child, request, addr, data);
213 break;
214 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return ret;
217}
218
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900219asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct task_struct *tsk = current;
222
Paul Mundtc4637d42008-07-30 15:30:52 +0900223 secure_computing(regs->regs[0]);
224
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900225 if (unlikely(current->audit_context) && entryexit)
226 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
227 regs->regs[0]);
228
Stuart Menefy9432f962007-02-23 13:22:17 +0900229 if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
230 !test_thread_flag(TIF_SINGLESTEP))
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900231 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!(tsk->ptrace & PT_PTRACED))
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900233 goto out;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* the 0x80 provides a way for the tracing parent to distinguish
236 between a syscall stop and SIGTRAP delivery */
Stuart Menefy9432f962007-02-23 13:22:17 +0900237 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
238 !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /*
241 * this isn't the same as continuing with a signal, but it will do
242 * for normal use. strace only continues with a signal if the
243 * stopping signal is not SIGTRAP. -brl
244 */
245 if (tsk->exit_code) {
246 send_sig(tsk->exit_code, tsk, 1);
247 tsk->exit_code = 0;
248 }
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900249
250out:
251 if (unlikely(current->audit_context) && !entryexit)
252 audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[3],
253 regs->regs[4], regs->regs[5],
254 regs->regs[6], regs->regs[7]);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}