Full support for Ginger Console
[linux-ginger.git] / arch / sh / kernel / ptrace_32.c
blob9be35f34809356f40b006c91fea36ba77deea458
1 /*
2 * SuperH process tracing
4 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
5 * Copyright (C) 2002 - 2008 Paul Mundt
7 * Audit support by Yuichi Nakamura <ynakam@hitachisoft.jp>
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.
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/errno.h>
18 #include <linux/ptrace.h>
19 #include <linux/user.h>
20 #include <linux/slab.h>
21 #include <linux/security.h>
22 #include <linux/signal.h>
23 #include <linux/io.h>
24 #include <linux/audit.h>
25 #include <linux/seccomp.h>
26 #include <linux/tracehook.h>
27 #include <linux/elf.h>
28 #include <linux/regset.h>
29 #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>
34 #include <asm/syscalls.h>
35 #include <asm/fpu.h>
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/syscalls.h>
41 * This routine will get a word off of the process kernel stack.
43 static inline int get_stack_long(struct task_struct *task, int offset)
45 unsigned char *stack;
47 stack = (unsigned char *)task_pt_regs(task);
48 stack += offset;
49 return (*((int *)stack));
53 * This routine will put a word on the process kernel stack.
55 static inline int put_stack_long(struct task_struct *task, int offset,
56 unsigned long data)
58 unsigned char *stack;
60 stack = (unsigned char *)task_pt_regs(task);
61 stack += offset;
62 *(unsigned long *) stack = data;
63 return 0;
66 void user_enable_single_step(struct task_struct *child)
68 /* Next scheduling will set up UBC */
69 if (child->thread.ubc_pc == 0)
70 ubc_usercnt += 1;
72 child->thread.ubc_pc = get_stack_long(child,
73 offsetof(struct pt_regs, pc));
75 set_tsk_thread_flag(child, TIF_SINGLESTEP);
78 void user_disable_single_step(struct task_struct *child)
80 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
83 * Ensure the UBC is not programmed at the next context switch.
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.
89 if (child->thread.ubc_pc != 0) {
90 ubc_usercnt -= 1;
91 child->thread.ubc_pc = 0;
96 * Called by kernel/ptrace.c when detaching..
98 * Make sure single step bits etc are not set.
100 void ptrace_disable(struct task_struct *child)
102 user_disable_single_step(child);
105 static 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)
110 const struct pt_regs *regs = task_pt_regs(target);
111 int ret;
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);
126 return ret;
129 static 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)
134 struct pt_regs *regs = task_pt_regs(target);
135 int ret;
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);
149 return ret;
152 #ifdef CONFIG_SH_FPU
153 int 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)
158 int ret;
160 ret = init_fpu(target);
161 if (ret)
162 return ret;
164 if ((boot_cpu_data.flags & CPU_HAS_FPU))
165 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
166 &target->thread.fpu.hard, 0, -1);
168 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
169 &target->thread.fpu.soft, 0, -1);
172 static 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)
177 int ret;
179 ret = init_fpu(target);
180 if (ret)
181 return ret;
183 set_stopped_child_used_math(target);
185 if ((boot_cpu_data.flags & CPU_HAS_FPU))
186 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
187 &target->thread.fpu.hard, 0, -1);
189 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
190 &target->thread.fpu.soft, 0, -1);
193 static int fpregs_active(struct task_struct *target,
194 const struct user_regset *regset)
196 return tsk_used_math(target) ? regset->n : 0;
198 #endif
200 #ifdef CONFIG_SH_DSP
201 static 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)
206 const struct pt_dspregs *regs =
207 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
208 int ret;
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);
216 return ret;
219 static 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)
224 struct pt_dspregs *regs =
225 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
226 int ret;
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);
234 return ret;
237 static int dspregs_active(struct task_struct *target,
238 const struct user_regset *regset)
240 struct pt_regs *regs = task_pt_regs(target);
242 return regs->sr & SR_DSP ? regset->n : 0;
244 #endif
247 * These are our native regset flavours.
249 enum sh_regset {
250 REGSET_GENERAL,
251 #ifdef CONFIG_SH_FPU
252 REGSET_FPU,
253 #endif
254 #ifdef CONFIG_SH_DSP
255 REGSET_DSP,
256 #endif
259 static const struct user_regset sh_regsets[] = {
261 * Format is:
262 * R0 --> R15
263 * PC, PR, SR, GBR, MACH, MACL, TRA
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,
274 #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,
284 #endif
286 #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,
293 .active = dspregs_active,
295 #endif
298 static 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),
305 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
307 return &user_sh_native_view;
310 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
312 struct user * dummy = NULL;
313 unsigned long __user *datap = (unsigned long __user *)data;
314 int ret;
316 switch (request) {
317 /* read the word at location addr in the USER area. */
318 case PTRACE_PEEKUSR: {
319 unsigned long tmp;
321 ret = -EIO;
322 if ((addr & 3) || addr < 0 ||
323 addr > sizeof(struct user) - 3)
324 break;
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
336 tmp = ((long *)&child->thread.fpu)
337 [(addr - (long)&dummy->fpu) >> 2];
338 } else if (addr == (long) &dummy->u_fpvalid)
339 tmp = !!tsk_used_math(child);
340 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;
348 else
349 tmp = 0;
350 ret = put_user(tmp, datap);
351 break;
354 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
355 ret = -EIO;
356 if ((addr & 3) || addr < 0 ||
357 addr > sizeof(struct user) - 3)
358 break;
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);
365 ((long *)&child->thread.fpu)
366 [(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;
372 break;
374 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);
384 #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
396 #ifdef CONFIG_SH_DSP
397 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);
407 #endif
408 #ifdef CONFIG_BINFMT_ELF_FDPIC
409 case PTRACE_GETFDPIC: {
410 unsigned long tmp = 0;
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;
423 ret = 0;
424 if (put_user(tmp, datap)) {
425 ret = -EFAULT;
426 break;
428 break;
430 #endif
431 default:
432 ret = ptrace_request(child, request, addr, data);
433 break;
436 return ret;
439 static inline int audit_arch(void)
441 int arch = EM_SH;
443 #ifdef CONFIG_CPU_LITTLE_ENDIAN
444 arch |= __AUDIT_ARCH_LE;
445 #endif
447 return arch;
450 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
452 long ret = 0;
454 secure_computing(regs->regs[0]);
456 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
457 tracehook_report_syscall_entry(regs))
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].
463 ret = -1L;
465 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
466 trace_sys_enter(regs, regs->regs[0]);
468 if (unlikely(current->audit_context))
469 audit_syscall_entry(audit_arch(), regs->regs[3],
470 regs->regs[4], regs->regs[5],
471 regs->regs[6], regs->regs[7]);
473 return ret ?: regs->regs[0];
476 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
478 int step;
480 if (unlikely(current->audit_context))
481 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
482 regs->regs[0]);
484 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
485 trace_sys_exit(regs, regs->regs[0]);
487 step = test_thread_flag(TIF_SINGLESTEP);
488 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
489 tracehook_report_syscall_exit(regs, step);