blob: c198eceaee94dbef536ee9e0db32a2a002abfea6 [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>
Paul Mundte7ab3cd2008-09-21 19:04:55 +090035#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Matt Flemingc652d782009-07-06 20:16:33 +090037#include <trace/syscall.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * This routine will get a word off of the process kernel stack.
41 */
42static inline int get_stack_long(struct task_struct *task, int offset)
43{
44 unsigned char *stack;
45
Al Viro3cf0f4e2006-01-12 01:05:44 -080046 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 stack += offset;
48 return (*((int *)stack));
49}
50
51/*
52 * This routine will put a word on the process kernel stack.
53 */
54static inline int put_stack_long(struct task_struct *task, int offset,
55 unsigned long data)
56{
57 unsigned char *stack;
58
Al Viro3cf0f4e2006-01-12 01:05:44 -080059 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 stack += offset;
61 *(unsigned long *) stack = data;
62 return 0;
63}
64
Paul Mundtc459dbf2008-07-30 19:09:31 +090065void user_enable_single_step(struct task_struct *child)
66{
Paul Mundtc459dbf2008-07-30 19:09:31 +090067 /* Next scheduling will set up UBC */
68 if (child->thread.ubc_pc == 0)
69 ubc_usercnt += 1;
70
Paul Mundt934135c2008-09-12 19:52:36 +090071 child->thread.ubc_pc = get_stack_long(child,
72 offsetof(struct pt_regs, pc));
Paul Mundtc459dbf2008-07-30 19:09:31 +090073
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
Paul Mundt934135c2008-09-12 19:52:36 +0900104static int genregs_get(struct task_struct *target,
105 const struct user_regset *regset,
106 unsigned int pos, unsigned int count,
107 void *kbuf, void __user *ubuf)
108{
109 const struct pt_regs *regs = task_pt_regs(target);
110 int ret;
111
112 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
113 regs->regs,
114 0, 16 * sizeof(unsigned long));
115 if (!ret)
116 /* PC, PR, SR, GBR, MACH, MACL, TRA */
117 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
118 &regs->pc,
119 offsetof(struct pt_regs, pc),
120 sizeof(struct pt_regs));
121 if (!ret)
122 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
123 sizeof(struct pt_regs), -1);
124
125 return ret;
126}
127
128static int genregs_set(struct task_struct *target,
129 const struct user_regset *regset,
130 unsigned int pos, unsigned int count,
131 const void *kbuf, const void __user *ubuf)
132{
133 struct pt_regs *regs = task_pt_regs(target);
134 int ret;
135
136 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
137 regs->regs,
138 0, 16 * sizeof(unsigned long));
139 if (!ret && count > 0)
140 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
141 &regs->pc,
142 offsetof(struct pt_regs, pc),
143 sizeof(struct pt_regs));
144 if (!ret)
145 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
146 sizeof(struct pt_regs), -1);
147
148 return ret;
149}
150
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900151#ifdef CONFIG_SH_FPU
152int fpregs_get(struct task_struct *target,
153 const struct user_regset *regset,
154 unsigned int pos, unsigned int count,
155 void *kbuf, void __user *ubuf)
156{
157 int ret;
158
159 ret = init_fpu(target);
160 if (ret)
161 return ret;
162
163 if ((boot_cpu_data.flags & CPU_HAS_FPU))
164 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
165 &target->thread.fpu.hard, 0, -1);
166
167 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
168 &target->thread.fpu.soft, 0, -1);
169}
170
171static int fpregs_set(struct task_struct *target,
172 const struct user_regset *regset,
173 unsigned int pos, unsigned int count,
174 const void *kbuf, const void __user *ubuf)
175{
176 int ret;
177
178 ret = init_fpu(target);
179 if (ret)
180 return ret;
181
182 set_stopped_child_used_math(target);
183
184 if ((boot_cpu_data.flags & CPU_HAS_FPU))
185 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
186 &target->thread.fpu.hard, 0, -1);
187
188 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
189 &target->thread.fpu.soft, 0, -1);
190}
191
192static int fpregs_active(struct task_struct *target,
193 const struct user_regset *regset)
194{
195 return tsk_used_math(target) ? regset->n : 0;
196}
197#endif
198
Paul Mundt5dadb342008-09-12 22:42:10 +0900199#ifdef CONFIG_SH_DSP
200static int dspregs_get(struct task_struct *target,
201 const struct user_regset *regset,
202 unsigned int pos, unsigned int count,
203 void *kbuf, void __user *ubuf)
204{
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000205 const struct pt_dspregs *regs =
206 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
Paul Mundt5dadb342008-09-12 22:42:10 +0900207 int ret;
208
209 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
210 0, sizeof(struct pt_dspregs));
211 if (!ret)
212 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
213 sizeof(struct pt_dspregs), -1);
214
215 return ret;
216}
217
218static int dspregs_set(struct task_struct *target,
219 const struct user_regset *regset,
220 unsigned int pos, unsigned int count,
221 const void *kbuf, const void __user *ubuf)
222{
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000223 struct pt_dspregs *regs =
224 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
Paul Mundt5dadb342008-09-12 22:42:10 +0900225 int ret;
226
227 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
228 0, sizeof(struct pt_dspregs));
229 if (!ret)
230 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
231 sizeof(struct pt_dspregs), -1);
232
233 return ret;
234}
Paul Mundt72461992008-09-12 22:56:35 +0900235
236static int dspregs_active(struct task_struct *target,
237 const struct user_regset *regset)
238{
239 struct pt_regs *regs = task_pt_regs(target);
240
241 return regs->sr & SR_DSP ? regset->n : 0;
242}
Paul Mundt5dadb342008-09-12 22:42:10 +0900243#endif
244
Paul Mundt934135c2008-09-12 19:52:36 +0900245/*
246 * These are our native regset flavours.
247 */
248enum sh_regset {
249 REGSET_GENERAL,
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900250#ifdef CONFIG_SH_FPU
251 REGSET_FPU,
252#endif
Paul Mundt5dadb342008-09-12 22:42:10 +0900253#ifdef CONFIG_SH_DSP
254 REGSET_DSP,
255#endif
Paul Mundt934135c2008-09-12 19:52:36 +0900256};
257
258static const struct user_regset sh_regsets[] = {
259 /*
260 * Format is:
261 * R0 --> R15
262 * PC, PR, SR, GBR, MACH, MACL, TRA
263 */
264 [REGSET_GENERAL] = {
265 .core_note_type = NT_PRSTATUS,
266 .n = ELF_NGREG,
267 .size = sizeof(long),
268 .align = sizeof(long),
269 .get = genregs_get,
270 .set = genregs_set,
271 },
Paul Mundt5dadb342008-09-12 22:42:10 +0900272
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900273#ifdef CONFIG_SH_FPU
274 [REGSET_FPU] = {
275 .core_note_type = NT_PRFPREG,
276 .n = sizeof(struct user_fpu_struct) / sizeof(long),
277 .size = sizeof(long),
278 .align = sizeof(long),
279 .get = fpregs_get,
280 .set = fpregs_set,
281 .active = fpregs_active,
282 },
283#endif
284
Paul Mundt5dadb342008-09-12 22:42:10 +0900285#ifdef CONFIG_SH_DSP
286 [REGSET_DSP] = {
287 .n = sizeof(struct pt_dspregs) / sizeof(long),
288 .size = sizeof(long),
289 .align = sizeof(long),
290 .get = dspregs_get,
291 .set = dspregs_set,
Paul Mundt72461992008-09-12 22:56:35 +0900292 .active = dspregs_active,
Paul Mundt5dadb342008-09-12 22:42:10 +0900293 },
294#endif
Paul Mundt934135c2008-09-12 19:52:36 +0900295};
296
297static const struct user_regset_view user_sh_native_view = {
298 .name = "sh",
299 .e_machine = EM_SH,
300 .regsets = sh_regsets,
301 .n = ARRAY_SIZE(sh_regsets),
302};
303
Paul Mundtf9540ec2008-09-12 22:42:43 +0900304const struct user_regset_view *task_user_regset_view(struct task_struct *task)
305{
306 return &user_sh_native_view;
307}
308
Christoph Hellwig481bed42005-11-07 00:59:47 -0800309long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct user * dummy = NULL;
Paul Mundtfa439722008-09-04 18:53:58 +0900312 unsigned long __user *datap = (unsigned long __user *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int ret;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* read the word at location addr in the USER area. */
317 case PTRACE_PEEKUSR: {
318 unsigned long tmp;
319
320 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900321 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 addr > sizeof(struct user) - 3)
323 break;
324
325 if (addr < sizeof(struct pt_regs))
326 tmp = get_stack_long(child, addr);
327 else if (addr >= (long) &dummy->fpu &&
328 addr < (long) &dummy->u_fpvalid) {
329 if (!tsk_used_math(child)) {
330 if (addr == (long)&dummy->fpu.fpscr)
331 tmp = FPSCR_INIT;
332 else
333 tmp = 0;
334 } else
335 tmp = ((long *)&child->thread.fpu)
336 [(addr - (long)&dummy->fpu) >> 2];
337 } else if (addr == (long) &dummy->u_fpvalid)
338 tmp = !!tsk_used_math(child);
Peter Griffinba0d4742009-05-08 15:50:54 +0100339 else if (addr == PT_TEXT_ADDR)
340 tmp = child->mm->start_code;
341 else if (addr == PT_DATA_ADDR)
342 tmp = child->mm->start_data;
343 else if (addr == PT_TEXT_END_ADDR)
344 tmp = child->mm->end_code;
345 else if (addr == PT_TEXT_LEN)
346 tmp = child->mm->end_code - child->mm->start_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 else
348 tmp = 0;
Paul Mundtfa439722008-09-04 18:53:58 +0900349 ret = put_user(tmp, datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351 }
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
354 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900355 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 addr > sizeof(struct user) - 3)
357 break;
358
359 if (addr < sizeof(struct pt_regs))
360 ret = put_stack_long(child, addr, data);
361 else if (addr >= (long) &dummy->fpu &&
362 addr < (long) &dummy->u_fpvalid) {
363 set_stopped_child_used_math(child);
364 ((long *)&child->thread.fpu)
365 [(addr - (long)&dummy->fpu) >> 2] = data;
366 ret = 0;
367 } else if (addr == (long) &dummy->u_fpvalid) {
368 conditional_stopped_child_used_math(data, child);
369 ret = 0;
370 }
371 break;
372
Paul Mundt934135c2008-09-12 19:52:36 +0900373 case PTRACE_GETREGS:
374 return copy_regset_to_user(child, &user_sh_native_view,
375 REGSET_GENERAL,
376 0, sizeof(struct pt_regs),
377 (void __user *)data);
378 case PTRACE_SETREGS:
379 return copy_regset_from_user(child, &user_sh_native_view,
380 REGSET_GENERAL,
381 0, sizeof(struct pt_regs),
382 (const void __user *)data);
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900383#ifdef CONFIG_SH_FPU
384 case PTRACE_GETFPREGS:
385 return copy_regset_to_user(child, &user_sh_native_view,
386 REGSET_FPU,
387 0, sizeof(struct user_fpu_struct),
388 (void __user *)data);
389 case PTRACE_SETFPREGS:
390 return copy_regset_from_user(child, &user_sh_native_view,
391 REGSET_FPU,
392 0, sizeof(struct user_fpu_struct),
393 (const void __user *)data);
394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#ifdef CONFIG_SH_DSP
Paul Mundt5dadb342008-09-12 22:42:10 +0900396 case PTRACE_GETDSPREGS:
397 return copy_regset_to_user(child, &user_sh_native_view,
398 REGSET_DSP,
399 0, sizeof(struct pt_dspregs),
400 (void __user *)data);
401 case PTRACE_SETDSPREGS:
402 return copy_regset_from_user(child, &user_sh_native_view,
403 REGSET_DSP,
404 0, sizeof(struct pt_dspregs),
405 (const void __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#endif
Paul Mundt3bc24a12008-05-19 13:40:12 +0900407#ifdef CONFIG_BINFMT_ELF_FDPIC
408 case PTRACE_GETFDPIC: {
409 unsigned long tmp = 0;
410
411 switch (addr) {
412 case PTRACE_GETFDPIC_EXEC:
413 tmp = child->mm->context.exec_fdpic_loadmap;
414 break;
415 case PTRACE_GETFDPIC_INTERP:
416 tmp = child->mm->context.interp_fdpic_loadmap;
417 break;
418 default:
419 break;
420 }
421
422 ret = 0;
Paul Mundtfa439722008-09-04 18:53:58 +0900423 if (put_user(tmp, datap)) {
Paul Mundt3bc24a12008-05-19 13:40:12 +0900424 ret = -EFAULT;
425 break;
426 }
427 break;
428 }
429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 default:
431 ret = ptrace_request(child, request, addr, data);
432 break;
433 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return ret;
436}
437
Paul Mundt9e5e2112008-07-30 20:05:35 +0900438static inline int audit_arch(void)
439{
440 int arch = EM_SH;
441
442#ifdef CONFIG_CPU_LITTLE_ENDIAN
443 arch |= __AUDIT_ARCH_LE;
444#endif
445
446 return arch;
447}
448
Paul Mundtab99c732008-07-30 19:55:30 +0900449asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Paul Mundtab99c732008-07-30 19:55:30 +0900451 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Paul Mundtc4637d42008-07-30 15:30:52 +0900453 secure_computing(regs->regs[0]);
454
Paul Mundtab99c732008-07-30 19:55:30 +0900455 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
456 tracehook_report_syscall_entry(regs))
457 /*
458 * Tracing decided this syscall should not happen.
459 * We'll return a bogus call number to get an ENOSYS
460 * error, but leave the original number in regs->regs[0].
461 */
462 ret = -1L;
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900463
Matt Flemingc652d782009-07-06 20:16:33 +0900464 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
465 ftrace_syscall_enter(regs);
466
Paul Mundtab99c732008-07-30 19:55:30 +0900467 if (unlikely(current->audit_context))
Paul Mundt9e5e2112008-07-30 20:05:35 +0900468 audit_syscall_entry(audit_arch(), regs->regs[3],
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900469 regs->regs[4], regs->regs[5],
470 regs->regs[6], regs->regs[7]);
471
Paul Mundtab99c732008-07-30 19:55:30 +0900472 return ret ?: regs->regs[0];
473}
474
475asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
476{
477 int step;
478
479 if (unlikely(current->audit_context))
480 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
481 regs->regs[0]);
482
Matt Flemingc652d782009-07-06 20:16:33 +0900483 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
484 ftrace_syscall_exit(regs);
485
Paul Mundtab99c732008-07-30 19:55:30 +0900486 step = test_thread_flag(TIF_SINGLESTEP);
487 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
488 tracehook_report_syscall_exit(regs, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}