blob: 20b103f6c33ba73e885c18e7794d89be2f276c7f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mundt934135c2008-09-12 19:52:36 +09002 * SuperH process tracing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Paul Mundt934135c2008-09-12 19:52:36 +09004 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
5 * Copyright (C) 2002 - 2008 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Paul Mundt934135c2008-09-12 19:52:36 +09007 * Audit support by Yuichi Nakamura <ynakam@hitachisoft.jp>
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
11 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ptrace.h>
19#include <linux/user.h>
20#include <linux/slab.h>
21#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070022#include <linux/signal.h>
Stuart Menefy9432f962007-02-23 13:22:17 +090023#include <linux/io.h>
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +090024#include <linux/audit.h>
Paul Mundtc4637d42008-07-30 15:30:52 +090025#include <linux/seccomp.h>
Paul Mundtab99c732008-07-30 19:55:30 +090026#include <linux/tracehook.h>
Paul Mundt934135c2008-09-12 19:52:36 +090027#include <linux/elf.h>
28#include <linux/regset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <asm/pgtable.h>
31#include <asm/system.h>
32#include <asm/processor.h>
33#include <asm/mmu_context.h>
Paul Mundtfa439722008-09-04 18:53:58 +090034#include <asm/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * 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{
Paul Mundtc459dbf2008-07-30 19:09:31 +090064 /* Next scheduling will set up UBC */
65 if (child->thread.ubc_pc == 0)
66 ubc_usercnt += 1;
67
Paul Mundt934135c2008-09-12 19:52:36 +090068 child->thread.ubc_pc = get_stack_long(child,
69 offsetof(struct pt_regs, pc));
Paul Mundtc459dbf2008-07-30 19:09:31 +090070
71 set_tsk_thread_flag(child, TIF_SINGLESTEP);
72}
73
74void user_disable_single_step(struct task_struct *child)
Stuart Menefy9432f962007-02-23 13:22:17 +090075{
76 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
77
78 /*
79 * Ensure the UBC is not programmed at the next context switch.
80 *
81 * Normally this is not needed but there are sequences such as
82 * singlestep, signal delivery, and continue that leave the
83 * ubc_pc non-zero leading to spurious SIGTRAPs.
84 */
85 if (child->thread.ubc_pc != 0) {
86 ubc_usercnt -= 1;
87 child->thread.ubc_pc = 0;
88 }
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * Called by kernel/ptrace.c when detaching..
93 *
94 * Make sure single step bits etc are not set.
95 */
96void ptrace_disable(struct task_struct *child)
97{
Paul Mundtc459dbf2008-07-30 19:09:31 +090098 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Paul Mundt934135c2008-09-12 19:52:36 +0900101static int genregs_get(struct task_struct *target,
102 const struct user_regset *regset,
103 unsigned int pos, unsigned int count,
104 void *kbuf, void __user *ubuf)
105{
106 const struct pt_regs *regs = task_pt_regs(target);
107 int ret;
108
109 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
110 regs->regs,
111 0, 16 * sizeof(unsigned long));
112 if (!ret)
113 /* PC, PR, SR, GBR, MACH, MACL, TRA */
114 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
115 &regs->pc,
116 offsetof(struct pt_regs, pc),
117 sizeof(struct pt_regs));
118 if (!ret)
119 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
120 sizeof(struct pt_regs), -1);
121
122 return ret;
123}
124
125static int genregs_set(struct task_struct *target,
126 const struct user_regset *regset,
127 unsigned int pos, unsigned int count,
128 const void *kbuf, const void __user *ubuf)
129{
130 struct pt_regs *regs = task_pt_regs(target);
131 int ret;
132
133 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
134 regs->regs,
135 0, 16 * sizeof(unsigned long));
136 if (!ret && count > 0)
137 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
138 &regs->pc,
139 offsetof(struct pt_regs, pc),
140 sizeof(struct pt_regs));
141 if (!ret)
142 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
143 sizeof(struct pt_regs), -1);
144
145 return ret;
146}
147
Paul Mundt5dadb342008-09-12 22:42:10 +0900148#ifdef CONFIG_SH_DSP
149static int dspregs_get(struct task_struct *target,
150 const struct user_regset *regset,
151 unsigned int pos, unsigned int count,
152 void *kbuf, void __user *ubuf)
153{
154 const struct pt_dspregs *regs = task_pt_dspregs(target);
155 int ret;
156
157 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
158 0, sizeof(struct pt_dspregs));
159 if (!ret)
160 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
161 sizeof(struct pt_dspregs), -1);
162
163 return ret;
164}
165
166static int dspregs_set(struct task_struct *target,
167 const struct user_regset *regset,
168 unsigned int pos, unsigned int count,
169 const void *kbuf, const void __user *ubuf)
170{
171 struct pt_dspregs *regs = task_pt_dspregs(target);
172 int ret;
173
174 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
175 0, sizeof(struct pt_dspregs));
176 if (!ret)
177 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
178 sizeof(struct pt_dspregs), -1);
179
180 return ret;
181}
182#endif
183
Paul Mundt934135c2008-09-12 19:52:36 +0900184/*
185 * These are our native regset flavours.
186 */
187enum sh_regset {
188 REGSET_GENERAL,
Paul Mundt5dadb342008-09-12 22:42:10 +0900189#ifdef CONFIG_SH_DSP
190 REGSET_DSP,
191#endif
Paul Mundt934135c2008-09-12 19:52:36 +0900192};
193
194static const struct user_regset sh_regsets[] = {
195 /*
196 * Format is:
197 * R0 --> R15
198 * PC, PR, SR, GBR, MACH, MACL, TRA
199 */
200 [REGSET_GENERAL] = {
201 .core_note_type = NT_PRSTATUS,
202 .n = ELF_NGREG,
203 .size = sizeof(long),
204 .align = sizeof(long),
205 .get = genregs_get,
206 .set = genregs_set,
207 },
Paul Mundt5dadb342008-09-12 22:42:10 +0900208
209#ifdef CONFIG_SH_DSP
210 [REGSET_DSP] = {
211 .n = sizeof(struct pt_dspregs) / sizeof(long),
212 .size = sizeof(long),
213 .align = sizeof(long),
214 .get = dspregs_get,
215 .set = dspregs_set,
216 },
217#endif
Paul Mundt934135c2008-09-12 19:52:36 +0900218};
219
220static const struct user_regset_view user_sh_native_view = {
221 .name = "sh",
222 .e_machine = EM_SH,
223 .regsets = sh_regsets,
224 .n = ARRAY_SIZE(sh_regsets),
225};
226
Christoph Hellwig481bed42005-11-07 00:59:47 -0800227long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct user * dummy = NULL;
Paul Mundtfa439722008-09-04 18:53:58 +0900230 unsigned long __user *datap = (unsigned long __user *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int ret;
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* read the word at location addr in the USER area. */
235 case PTRACE_PEEKUSR: {
236 unsigned long tmp;
237
238 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900239 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 addr > sizeof(struct user) - 3)
241 break;
242
243 if (addr < sizeof(struct pt_regs))
244 tmp = get_stack_long(child, addr);
245 else if (addr >= (long) &dummy->fpu &&
246 addr < (long) &dummy->u_fpvalid) {
247 if (!tsk_used_math(child)) {
248 if (addr == (long)&dummy->fpu.fpscr)
249 tmp = FPSCR_INIT;
250 else
251 tmp = 0;
252 } else
253 tmp = ((long *)&child->thread.fpu)
254 [(addr - (long)&dummy->fpu) >> 2];
255 } else if (addr == (long) &dummy->u_fpvalid)
256 tmp = !!tsk_used_math(child);
257 else
258 tmp = 0;
Paul Mundtfa439722008-09-04 18:53:58 +0900259 ret = put_user(tmp, datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 break;
261 }
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
264 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900265 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 addr > sizeof(struct user) - 3)
267 break;
268
269 if (addr < sizeof(struct pt_regs))
270 ret = put_stack_long(child, addr, data);
271 else if (addr >= (long) &dummy->fpu &&
272 addr < (long) &dummy->u_fpvalid) {
273 set_stopped_child_used_math(child);
274 ((long *)&child->thread.fpu)
275 [(addr - (long)&dummy->fpu) >> 2] = data;
276 ret = 0;
277 } else if (addr == (long) &dummy->u_fpvalid) {
278 conditional_stopped_child_used_math(data, child);
279 ret = 0;
280 }
281 break;
282
Paul Mundt934135c2008-09-12 19:52:36 +0900283 case PTRACE_GETREGS:
284 return copy_regset_to_user(child, &user_sh_native_view,
285 REGSET_GENERAL,
286 0, sizeof(struct pt_regs),
287 (void __user *)data);
288 case PTRACE_SETREGS:
289 return copy_regset_from_user(child, &user_sh_native_view,
290 REGSET_GENERAL,
291 0, sizeof(struct pt_regs),
292 (const void __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#ifdef CONFIG_SH_DSP
Paul Mundt5dadb342008-09-12 22:42:10 +0900294 case PTRACE_GETDSPREGS:
295 return copy_regset_to_user(child, &user_sh_native_view,
296 REGSET_DSP,
297 0, sizeof(struct pt_dspregs),
298 (void __user *)data);
299 case PTRACE_SETDSPREGS:
300 return copy_regset_from_user(child, &user_sh_native_view,
301 REGSET_DSP,
302 0, sizeof(struct pt_dspregs),
303 (const void __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#endif
Paul Mundt3bc24a12008-05-19 13:40:12 +0900305#ifdef CONFIG_BINFMT_ELF_FDPIC
306 case PTRACE_GETFDPIC: {
307 unsigned long tmp = 0;
308
309 switch (addr) {
310 case PTRACE_GETFDPIC_EXEC:
311 tmp = child->mm->context.exec_fdpic_loadmap;
312 break;
313 case PTRACE_GETFDPIC_INTERP:
314 tmp = child->mm->context.interp_fdpic_loadmap;
315 break;
316 default:
317 break;
318 }
319
320 ret = 0;
Paul Mundtfa439722008-09-04 18:53:58 +0900321 if (put_user(tmp, datap)) {
Paul Mundt3bc24a12008-05-19 13:40:12 +0900322 ret = -EFAULT;
323 break;
324 }
325 break;
326 }
327#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 default:
329 ret = ptrace_request(child, request, addr, data);
330 break;
331 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return ret;
334}
335
Paul Mundt9e5e2112008-07-30 20:05:35 +0900336static inline int audit_arch(void)
337{
338 int arch = EM_SH;
339
340#ifdef CONFIG_CPU_LITTLE_ENDIAN
341 arch |= __AUDIT_ARCH_LE;
342#endif
343
344 return arch;
345}
346
Paul Mundtab99c732008-07-30 19:55:30 +0900347asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Paul Mundtab99c732008-07-30 19:55:30 +0900349 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Paul Mundtc4637d42008-07-30 15:30:52 +0900351 secure_computing(regs->regs[0]);
352
Paul Mundtab99c732008-07-30 19:55:30 +0900353 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
354 tracehook_report_syscall_entry(regs))
355 /*
356 * Tracing decided this syscall should not happen.
357 * We'll return a bogus call number to get an ENOSYS
358 * error, but leave the original number in regs->regs[0].
359 */
360 ret = -1L;
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900361
Paul Mundtab99c732008-07-30 19:55:30 +0900362 if (unlikely(current->audit_context))
Paul Mundt9e5e2112008-07-30 20:05:35 +0900363 audit_syscall_entry(audit_arch(), regs->regs[3],
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900364 regs->regs[4], regs->regs[5],
365 regs->regs[6], regs->regs[7]);
366
Paul Mundtab99c732008-07-30 19:55:30 +0900367 return ret ?: regs->regs[0];
368}
369
370asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
371{
372 int step;
373
374 if (unlikely(current->audit_context))
375 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
376 regs->regs[0]);
377
378 step = test_thread_flag(TIF_SINGLESTEP);
379 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
380 tracehook_report_syscall_exit(regs, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}