blob: be9b5dcb4021a42dadb9437fdf0f4a851d1eaa13 [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
Paul Mundta74f7e02009-09-16 14:30:34 +090037#define CREATE_TRACE_POINTS
38#include <trace/events/syscalls.h>
Matt Flemingc652d782009-07-06 20:16:33 +090039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * This routine will get a word off of the process kernel stack.
42 */
43static inline int get_stack_long(struct task_struct *task, int offset)
44{
45 unsigned char *stack;
46
Al Viro3cf0f4e2006-01-12 01:05:44 -080047 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 stack += offset;
49 return (*((int *)stack));
50}
51
52/*
53 * This routine will put a word on the process kernel stack.
54 */
55static inline int put_stack_long(struct task_struct *task, int offset,
56 unsigned long data)
57{
58 unsigned char *stack;
59
Al Viro3cf0f4e2006-01-12 01:05:44 -080060 stack = (unsigned char *)task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 stack += offset;
62 *(unsigned long *) stack = data;
63 return 0;
64}
65
Paul Mundtc459dbf2008-07-30 19:09:31 +090066void user_enable_single_step(struct task_struct *child)
67{
Paul Mundtc459dbf2008-07-30 19:09:31 +090068 /* Next scheduling will set up UBC */
69 if (child->thread.ubc_pc == 0)
70 ubc_usercnt += 1;
71
Paul Mundt934135c2008-09-12 19:52:36 +090072 child->thread.ubc_pc = get_stack_long(child,
73 offsetof(struct pt_regs, pc));
Paul Mundtc459dbf2008-07-30 19:09:31 +090074
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
Paul Mundt934135c2008-09-12 19:52:36 +0900105static int genregs_get(struct task_struct *target,
106 const struct user_regset *regset,
107 unsigned int pos, unsigned int count,
108 void *kbuf, void __user *ubuf)
109{
110 const struct pt_regs *regs = task_pt_regs(target);
111 int ret;
112
113 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
114 regs->regs,
115 0, 16 * sizeof(unsigned long));
116 if (!ret)
117 /* PC, PR, SR, GBR, MACH, MACL, TRA */
118 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
119 &regs->pc,
120 offsetof(struct pt_regs, pc),
121 sizeof(struct pt_regs));
122 if (!ret)
123 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
124 sizeof(struct pt_regs), -1);
125
126 return ret;
127}
128
129static int genregs_set(struct task_struct *target,
130 const struct user_regset *regset,
131 unsigned int pos, unsigned int count,
132 const void *kbuf, const void __user *ubuf)
133{
134 struct pt_regs *regs = task_pt_regs(target);
135 int ret;
136
137 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
138 regs->regs,
139 0, 16 * sizeof(unsigned long));
140 if (!ret && count > 0)
141 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
142 &regs->pc,
143 offsetof(struct pt_regs, pc),
144 sizeof(struct pt_regs));
145 if (!ret)
146 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
147 sizeof(struct pt_regs), -1);
148
149 return ret;
150}
151
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900152#ifdef CONFIG_SH_FPU
153int fpregs_get(struct task_struct *target,
154 const struct user_regset *regset,
155 unsigned int pos, unsigned int count,
156 void *kbuf, void __user *ubuf)
157{
158 int ret;
159
160 ret = init_fpu(target);
161 if (ret)
162 return ret;
163
164 if ((boot_cpu_data.flags & CPU_HAS_FPU))
165 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Paul Mundt0ea820c2010-01-13 12:51:40 +0900166 &target->thread.xstate->hardfpu, 0, -1);
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900167
168 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Paul Mundt0ea820c2010-01-13 12:51:40 +0900169 &target->thread.xstate->softfpu, 0, -1);
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900170}
171
172static int fpregs_set(struct task_struct *target,
173 const struct user_regset *regset,
174 unsigned int pos, unsigned int count,
175 const void *kbuf, const void __user *ubuf)
176{
177 int ret;
178
179 ret = init_fpu(target);
180 if (ret)
181 return ret;
182
183 set_stopped_child_used_math(target);
184
185 if ((boot_cpu_data.flags & CPU_HAS_FPU))
186 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Paul Mundt0ea820c2010-01-13 12:51:40 +0900187 &target->thread.xstate->hardfpu, 0, -1);
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900188
189 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Paul Mundt0ea820c2010-01-13 12:51:40 +0900190 &target->thread.xstate->softfpu, 0, -1);
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900191}
192
193static int fpregs_active(struct task_struct *target,
194 const struct user_regset *regset)
195{
196 return tsk_used_math(target) ? regset->n : 0;
197}
198#endif
199
Paul Mundt5dadb342008-09-12 22:42:10 +0900200#ifdef CONFIG_SH_DSP
201static int dspregs_get(struct task_struct *target,
202 const struct user_regset *regset,
203 unsigned int pos, unsigned int count,
204 void *kbuf, void __user *ubuf)
205{
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000206 const struct pt_dspregs *regs =
207 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
Paul Mundt5dadb342008-09-12 22:42:10 +0900208 int ret;
209
210 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
211 0, sizeof(struct pt_dspregs));
212 if (!ret)
213 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
214 sizeof(struct pt_dspregs), -1);
215
216 return ret;
217}
218
219static int dspregs_set(struct task_struct *target,
220 const struct user_regset *regset,
221 unsigned int pos, unsigned int count,
222 const void *kbuf, const void __user *ubuf)
223{
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000224 struct pt_dspregs *regs =
225 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
Paul Mundt5dadb342008-09-12 22:42:10 +0900226 int ret;
227
228 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
229 0, sizeof(struct pt_dspregs));
230 if (!ret)
231 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
232 sizeof(struct pt_dspregs), -1);
233
234 return ret;
235}
Paul Mundt72461992008-09-12 22:56:35 +0900236
237static int dspregs_active(struct task_struct *target,
238 const struct user_regset *regset)
239{
240 struct pt_regs *regs = task_pt_regs(target);
241
242 return regs->sr & SR_DSP ? regset->n : 0;
243}
Paul Mundt5dadb342008-09-12 22:42:10 +0900244#endif
245
Paul Mundt934135c2008-09-12 19:52:36 +0900246/*
247 * These are our native regset flavours.
248 */
249enum sh_regset {
250 REGSET_GENERAL,
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900251#ifdef CONFIG_SH_FPU
252 REGSET_FPU,
253#endif
Paul Mundt5dadb342008-09-12 22:42:10 +0900254#ifdef CONFIG_SH_DSP
255 REGSET_DSP,
256#endif
Paul Mundt934135c2008-09-12 19:52:36 +0900257};
258
259static const struct user_regset sh_regsets[] = {
260 /*
261 * Format is:
262 * R0 --> R15
263 * PC, PR, SR, GBR, MACH, MACL, TRA
264 */
265 [REGSET_GENERAL] = {
266 .core_note_type = NT_PRSTATUS,
267 .n = ELF_NGREG,
268 .size = sizeof(long),
269 .align = sizeof(long),
270 .get = genregs_get,
271 .set = genregs_set,
272 },
Paul Mundt5dadb342008-09-12 22:42:10 +0900273
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900274#ifdef CONFIG_SH_FPU
275 [REGSET_FPU] = {
276 .core_note_type = NT_PRFPREG,
277 .n = sizeof(struct user_fpu_struct) / sizeof(long),
278 .size = sizeof(long),
279 .align = sizeof(long),
280 .get = fpregs_get,
281 .set = fpregs_set,
282 .active = fpregs_active,
283 },
284#endif
285
Paul Mundt5dadb342008-09-12 22:42:10 +0900286#ifdef CONFIG_SH_DSP
287 [REGSET_DSP] = {
288 .n = sizeof(struct pt_dspregs) / sizeof(long),
289 .size = sizeof(long),
290 .align = sizeof(long),
291 .get = dspregs_get,
292 .set = dspregs_set,
Paul Mundt72461992008-09-12 22:56:35 +0900293 .active = dspregs_active,
Paul Mundt5dadb342008-09-12 22:42:10 +0900294 },
295#endif
Paul Mundt934135c2008-09-12 19:52:36 +0900296};
297
298static const struct user_regset_view user_sh_native_view = {
299 .name = "sh",
300 .e_machine = EM_SH,
301 .regsets = sh_regsets,
302 .n = ARRAY_SIZE(sh_regsets),
303};
304
Paul Mundtf9540ec2008-09-12 22:42:43 +0900305const struct user_regset_view *task_user_regset_view(struct task_struct *task)
306{
307 return &user_sh_native_view;
308}
309
Christoph Hellwig481bed42005-11-07 00:59:47 -0800310long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct user * dummy = NULL;
Paul Mundtfa439722008-09-04 18:53:58 +0900313 unsigned long __user *datap = (unsigned long __user *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int ret;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* read the word at location addr in the USER area. */
318 case PTRACE_PEEKUSR: {
319 unsigned long tmp;
320
321 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900322 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 addr > sizeof(struct user) - 3)
324 break;
325
326 if (addr < sizeof(struct pt_regs))
327 tmp = get_stack_long(child, addr);
328 else if (addr >= (long) &dummy->fpu &&
329 addr < (long) &dummy->u_fpvalid) {
330 if (!tsk_used_math(child)) {
331 if (addr == (long)&dummy->fpu.fpscr)
332 tmp = FPSCR_INIT;
333 else
334 tmp = 0;
335 } else
Paul Mundt0ea820c2010-01-13 12:51:40 +0900336 tmp = ((long *)child->thread.xstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 [(addr - (long)&dummy->fpu) >> 2];
338 } else if (addr == (long) &dummy->u_fpvalid)
339 tmp = !!tsk_used_math(child);
Peter Griffinba0d4742009-05-08 15:50:54 +0100340 else if (addr == PT_TEXT_ADDR)
341 tmp = child->mm->start_code;
342 else if (addr == PT_DATA_ADDR)
343 tmp = child->mm->start_data;
344 else if (addr == PT_TEXT_END_ADDR)
345 tmp = child->mm->end_code;
346 else if (addr == PT_TEXT_LEN)
347 tmp = child->mm->end_code - child->mm->start_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 else
349 tmp = 0;
Paul Mundtfa439722008-09-04 18:53:58 +0900350 ret = put_user(tmp, datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
352 }
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
355 ret = -EIO;
Stuart Menefy9432f962007-02-23 13:22:17 +0900356 if ((addr & 3) || addr < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 addr > sizeof(struct user) - 3)
358 break;
359
360 if (addr < sizeof(struct pt_regs))
361 ret = put_stack_long(child, addr, data);
362 else if (addr >= (long) &dummy->fpu &&
363 addr < (long) &dummy->u_fpvalid) {
364 set_stopped_child_used_math(child);
Paul Mundt0ea820c2010-01-13 12:51:40 +0900365 ((long *)child->thread.xstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 [(addr - (long)&dummy->fpu) >> 2] = data;
367 ret = 0;
368 } else if (addr == (long) &dummy->u_fpvalid) {
369 conditional_stopped_child_used_math(data, child);
370 ret = 0;
371 }
372 break;
373
Paul Mundt934135c2008-09-12 19:52:36 +0900374 case PTRACE_GETREGS:
375 return copy_regset_to_user(child, &user_sh_native_view,
376 REGSET_GENERAL,
377 0, sizeof(struct pt_regs),
378 (void __user *)data);
379 case PTRACE_SETREGS:
380 return copy_regset_from_user(child, &user_sh_native_view,
381 REGSET_GENERAL,
382 0, sizeof(struct pt_regs),
383 (const void __user *)data);
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900384#ifdef CONFIG_SH_FPU
385 case PTRACE_GETFPREGS:
386 return copy_regset_to_user(child, &user_sh_native_view,
387 REGSET_FPU,
388 0, sizeof(struct user_fpu_struct),
389 (void __user *)data);
390 case PTRACE_SETFPREGS:
391 return copy_regset_from_user(child, &user_sh_native_view,
392 REGSET_FPU,
393 0, sizeof(struct user_fpu_struct),
394 (const void __user *)data);
395#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#ifdef CONFIG_SH_DSP
Paul Mundt5dadb342008-09-12 22:42:10 +0900397 case PTRACE_GETDSPREGS:
398 return copy_regset_to_user(child, &user_sh_native_view,
399 REGSET_DSP,
400 0, sizeof(struct pt_dspregs),
401 (void __user *)data);
402 case PTRACE_SETDSPREGS:
403 return copy_regset_from_user(child, &user_sh_native_view,
404 REGSET_DSP,
405 0, sizeof(struct pt_dspregs),
406 (const void __user *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#endif
Paul Mundt3bc24a12008-05-19 13:40:12 +0900408#ifdef CONFIG_BINFMT_ELF_FDPIC
409 case PTRACE_GETFDPIC: {
410 unsigned long tmp = 0;
411
412 switch (addr) {
413 case PTRACE_GETFDPIC_EXEC:
414 tmp = child->mm->context.exec_fdpic_loadmap;
415 break;
416 case PTRACE_GETFDPIC_INTERP:
417 tmp = child->mm->context.interp_fdpic_loadmap;
418 break;
419 default:
420 break;
421 }
422
423 ret = 0;
Paul Mundtfa439722008-09-04 18:53:58 +0900424 if (put_user(tmp, datap)) {
Paul Mundt3bc24a12008-05-19 13:40:12 +0900425 ret = -EFAULT;
426 break;
427 }
428 break;
429 }
430#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 default:
432 ret = ptrace_request(child, request, addr, data);
433 break;
434 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return ret;
437}
438
Paul Mundt9e5e2112008-07-30 20:05:35 +0900439static inline int audit_arch(void)
440{
441 int arch = EM_SH;
442
443#ifdef CONFIG_CPU_LITTLE_ENDIAN
444 arch |= __AUDIT_ARCH_LE;
445#endif
446
447 return arch;
448}
449
Paul Mundtab99c732008-07-30 19:55:30 +0900450asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Paul Mundtab99c732008-07-30 19:55:30 +0900452 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Paul Mundtc4637d42008-07-30 15:30:52 +0900454 secure_computing(regs->regs[0]);
455
Paul Mundtab99c732008-07-30 19:55:30 +0900456 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
457 tracehook_report_syscall_entry(regs))
458 /*
459 * Tracing decided this syscall should not happen.
460 * We'll return a bogus call number to get an ENOSYS
461 * error, but leave the original number in regs->regs[0].
462 */
463 ret = -1L;
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900464
Paul Mundta74f7e02009-09-16 14:30:34 +0900465 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
466 trace_sys_enter(regs, regs->regs[0]);
Matt Flemingc652d782009-07-06 20:16:33 +0900467
Paul Mundtab99c732008-07-30 19:55:30 +0900468 if (unlikely(current->audit_context))
Paul Mundt9e5e2112008-07-30 20:05:35 +0900469 audit_syscall_entry(audit_arch(), regs->regs[3],
Yuichi Nakamura1322b9d2007-11-10 19:21:34 +0900470 regs->regs[4], regs->regs[5],
471 regs->regs[6], regs->regs[7]);
472
Paul Mundtab99c732008-07-30 19:55:30 +0900473 return ret ?: regs->regs[0];
474}
475
476asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
477{
478 int step;
479
480 if (unlikely(current->audit_context))
481 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
482 regs->regs[0]);
483
Paul Mundta74f7e02009-09-16 14:30:34 +0900484 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
485 trace_sys_exit(regs, regs->regs[0]);
Matt Flemingc652d782009-07-06 20:16:33 +0900486
Paul Mundtab99c732008-07-30 19:55:30 +0900487 step = test_thread_flag(TIF_SINGLESTEP);
488 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
489 tracehook_report_syscall_exit(regs, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}