blob: b063eb8b18e30061088abe291a5eb6973eff09a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mundt4b27c472007-11-28 19:58:11 +09002 * arch/sh/kernel/ptrace_64.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000, 2001 Paolo Alberelli
Paul Mundtdd762792008-12-10 20:14:15 +09005 * Copyright (C) 2003 - 2008 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Started from SH3/4 version:
8 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
9 *
10 * Original x86 implementation:
11 * By Ross Biro 1/23/92
12 * edited by Linus Torvalds
13 *
Paul Mundt4b27c472007-11-28 19:58:11 +090014 * This file is subject to the terms and conditions of the GNU General Public
15 * License. See the file "COPYING" in the main directory of this archive
16 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/rwsem.h>
20#include <linux/sched.h>
21#include <linux/mm.h>
22#include <linux/smp.h>
23#include <linux/smp_lock.h>
24#include <linux/errno.h>
25#include <linux/ptrace.h>
26#include <linux/user.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070027#include <linux/signal.h>
Christoph Hellwig481bed42005-11-07 00:59:47 -080028#include <linux/syscalls.h>
Paul Mundt4b27c472007-11-28 19:58:11 +090029#include <linux/audit.h>
Paul Mundtc4637d42008-07-30 15:30:52 +090030#include <linux/seccomp.h>
Paul Mundtab99c732008-07-30 19:55:30 +090031#include <linux/tracehook.h>
Paul Mundtdd762792008-12-10 20:14:15 +090032#include <linux/elf.h>
33#include <linux/regset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/io.h>
35#include <asm/uaccess.h>
36#include <asm/pgtable.h>
37#include <asm/system.h>
38#include <asm/processor.h>
39#include <asm/mmu_context.h>
Paul Mundtfa439722008-09-04 18:53:58 +090040#include <asm/syscalls.h>
Adrian Bunk50387b32008-04-13 21:15:38 +030041#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Paul Mundta74f7e02009-09-16 14:30:34 +090043#define CREATE_TRACE_POINTS
44#include <trace/events/syscalls.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* This mask defines the bits of the SR which the user is not allowed to
47 change, which are everything except S, Q, M, PR, SZ, FR. */
48#define SR_MASK (0xffff8cfd)
49
50/*
51 * does not yet catch signals sent when the child dies.
52 * in exit.c or in signal.c.
53 */
54
55/*
56 * This routine will get a word from the user area in the process kernel stack.
57 */
58static inline int get_stack_long(struct task_struct *task, int offset)
59{
60 unsigned char *stack;
61
62 stack = (unsigned char *)(task->thread.uregs);
63 stack += offset;
64 return (*((int *)stack));
65}
66
67static inline unsigned long
68get_fpu_long(struct task_struct *task, unsigned long addr)
69{
70 unsigned long tmp;
71 struct pt_regs *regs;
72 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
73
74 if (!tsk_used_math(task)) {
75 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
76 tmp = FPSCR_INIT;
77 } else {
78 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
79 }
80 return tmp;
81 }
82
83 if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +090084 enable_fpu();
Matt Fleming61cc7b02009-12-14 20:12:04 +000085 save_fpu(task);
Paul Mundt256b22c2007-11-10 20:27:03 +090086 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 last_task_used_math = 0;
88 regs->sr |= SR_FD;
89 }
90
91 tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
92 return tmp;
93}
94
95/*
96 * This routine will put a word into the user area in the process kernel stack.
97 */
98static inline int put_stack_long(struct task_struct *task, int offset,
99 unsigned long data)
100{
101 unsigned char *stack;
102
103 stack = (unsigned char *)(task->thread.uregs);
104 stack += offset;
105 *(unsigned long *) stack = data;
106 return 0;
107}
108
109static inline int
110put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
111{
112 struct pt_regs *regs;
113
114 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
115
116 if (!tsk_used_math(task)) {
117 fpinit(&task->thread.fpu.hard);
118 set_stopped_child_used_math(task);
119 } else if (last_task_used_math == task) {
Paul Mundt256b22c2007-11-10 20:27:03 +0900120 enable_fpu();
Matt Fleming61cc7b02009-12-14 20:12:04 +0000121 save_fpu(task);
Paul Mundt256b22c2007-11-10 20:27:03 +0900122 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 last_task_used_math = 0;
124 regs->sr |= SR_FD;
125 }
126
127 ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
128 return 0;
129}
130
Paul Mundtc459dbf2008-07-30 19:09:31 +0900131void user_enable_single_step(struct task_struct *child)
132{
133 struct pt_regs *regs = child->thread.uregs;
134
135 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
Paul Mundt4b505db2010-02-15 14:17:45 +0900136
137 set_tsk_thread_flag(child, TIF_SINGLESTEP);
Paul Mundtc459dbf2008-07-30 19:09:31 +0900138}
139
140void user_disable_single_step(struct task_struct *child)
141{
Adrian Bunke311be52008-08-22 18:20:36 +0300142 struct pt_regs *regs = child->thread.uregs;
143
Paul Mundtc459dbf2008-07-30 19:09:31 +0900144 regs->sr &= ~SR_SSTEP;
Paul Mundt4b505db2010-02-15 14:17:45 +0900145
146 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
Paul Mundtc459dbf2008-07-30 19:09:31 +0900147}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800148
Paul Mundtdd762792008-12-10 20:14:15 +0900149static int genregs_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_regs *regs = task_pt_regs(target);
155 int ret;
156
157 /* PC, SR, SYSCALL */
158 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
159 &regs->pc,
160 0, 3 * sizeof(unsigned long long));
161
162 /* R1 -> R63 */
163 if (!ret)
164 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
165 regs->regs,
166 offsetof(struct pt_regs, regs[0]),
167 63 * sizeof(unsigned long long));
168 /* TR0 -> TR7 */
169 if (!ret)
170 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
171 regs->tregs,
172 offsetof(struct pt_regs, tregs[0]),
173 8 * sizeof(unsigned long long));
174
175 if (!ret)
176 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
177 sizeof(struct pt_regs), -1);
178
179 return ret;
180}
181
182static int genregs_set(struct task_struct *target,
183 const struct user_regset *regset,
184 unsigned int pos, unsigned int count,
185 const void *kbuf, const void __user *ubuf)
186{
187 struct pt_regs *regs = task_pt_regs(target);
188 int ret;
189
190 /* PC, SR, SYSCALL */
191 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
192 &regs->pc,
193 0, 3 * sizeof(unsigned long long));
194
195 /* R1 -> R63 */
196 if (!ret && count > 0)
197 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
198 regs->regs,
199 offsetof(struct pt_regs, regs[0]),
200 63 * sizeof(unsigned long long));
201
202 /* TR0 -> TR7 */
203 if (!ret && count > 0)
204 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
205 regs->tregs,
206 offsetof(struct pt_regs, tregs[0]),
207 8 * sizeof(unsigned long long));
208
209 if (!ret)
210 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
211 sizeof(struct pt_regs), -1);
212
213 return ret;
214}
215
216#ifdef CONFIG_SH_FPU
217int fpregs_get(struct task_struct *target,
218 const struct user_regset *regset,
219 unsigned int pos, unsigned int count,
220 void *kbuf, void __user *ubuf)
221{
222 int ret;
223
224 ret = init_fpu(target);
225 if (ret)
226 return ret;
227
228 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
229 &target->thread.fpu.hard, 0, -1);
230}
231
232static int fpregs_set(struct task_struct *target,
233 const struct user_regset *regset,
234 unsigned int pos, unsigned int count,
235 const void *kbuf, const void __user *ubuf)
236{
237 int ret;
238
239 ret = init_fpu(target);
240 if (ret)
241 return ret;
242
243 set_stopped_child_used_math(target);
244
245 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
246 &target->thread.fpu.hard, 0, -1);
247}
248
249static int fpregs_active(struct task_struct *target,
250 const struct user_regset *regset)
251{
252 return tsk_used_math(target) ? regset->n : 0;
253}
254#endif
255
256/*
257 * These are our native regset flavours.
258 */
259enum sh_regset {
260 REGSET_GENERAL,
261#ifdef CONFIG_SH_FPU
262 REGSET_FPU,
263#endif
264};
265
266static const struct user_regset sh_regsets[] = {
267 /*
268 * Format is:
269 * PC, SR, SYSCALL,
270 * R1 --> R63,
271 * TR0 --> TR7,
272 */
273 [REGSET_GENERAL] = {
274 .core_note_type = NT_PRSTATUS,
275 .n = ELF_NGREG,
276 .size = sizeof(long long),
277 .align = sizeof(long long),
278 .get = genregs_get,
279 .set = genregs_set,
280 },
281
282#ifdef CONFIG_SH_FPU
283 [REGSET_FPU] = {
284 .core_note_type = NT_PRFPREG,
285 .n = sizeof(struct user_fpu_struct) /
286 sizeof(long long),
287 .size = sizeof(long long),
288 .align = sizeof(long long),
289 .get = fpregs_get,
290 .set = fpregs_set,
291 .active = fpregs_active,
292 },
293#endif
294};
295
296static const struct user_regset_view user_sh64_native_view = {
297 .name = "sh64",
298 .e_machine = EM_SH,
299 .regsets = sh_regsets,
300 .n = ARRAY_SIZE(sh_regsets),
301};
302
303const struct user_regset_view *task_user_regset_view(struct task_struct *task)
304{
305 return &user_sh64_native_view;
306}
307
Christoph Hellwig481bed42005-11-07 00:59:47 -0800308long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int ret;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* read the word at location addr in the USER area. */
314 case PTRACE_PEEKUSR: {
315 unsigned long tmp;
316
317 ret = -EIO;
318 if ((addr & 3) || addr < 0)
319 break;
320
321 if (addr < sizeof(struct pt_regs))
322 tmp = get_stack_long(child, addr);
323 else if ((addr >= offsetof(struct user, fpu)) &&
324 (addr < offsetof(struct user, u_fpvalid))) {
325 tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
326 } else if (addr == offsetof(struct user, u_fpvalid)) {
327 tmp = !!tsk_used_math(child);
328 } else {
329 break;
330 }
331 ret = put_user(tmp, (unsigned long *)data);
332 break;
333 }
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 case PTRACE_POKEUSR:
336 /* write the word at location addr in the USER area. We must
337 disallow any changes to certain SR bits or u_fpvalid, since
338 this could crash the kernel or result in a security
339 loophole. */
340 ret = -EIO;
341 if ((addr & 3) || addr < 0)
342 break;
343
344 if (addr < sizeof(struct pt_regs)) {
345 /* Ignore change of top 32 bits of SR */
346 if (addr == offsetof (struct pt_regs, sr)+4)
347 {
348 ret = 0;
349 break;
350 }
351 /* If lower 32 bits of SR, ignore non-user bits */
352 if (addr == offsetof (struct pt_regs, sr))
353 {
354 long cursr = get_stack_long(child, addr);
355 data &= ~(SR_MASK);
356 data |= (cursr & SR_MASK);
357 }
358 ret = put_stack_long(child, addr, data);
359 }
360 else if ((addr >= offsetof(struct user, fpu)) &&
361 (addr < offsetof(struct user, u_fpvalid))) {
362 ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
363 }
364 break;
365
Paul Mundtdd762792008-12-10 20:14:15 +0900366 case PTRACE_GETREGS:
367 return copy_regset_to_user(child, &user_sh64_native_view,
368 REGSET_GENERAL,
369 0, sizeof(struct pt_regs),
370 (void __user *)data);
371 case PTRACE_SETREGS:
372 return copy_regset_from_user(child, &user_sh64_native_view,
373 REGSET_GENERAL,
374 0, sizeof(struct pt_regs),
375 (const void __user *)data);
376#ifdef CONFIG_SH_FPU
377 case PTRACE_GETFPREGS:
378 return copy_regset_to_user(child, &user_sh64_native_view,
379 REGSET_FPU,
380 0, sizeof(struct user_fpu_struct),
381 (void __user *)data);
382 case PTRACE_SETFPREGS:
383 return copy_regset_from_user(child, &user_sh64_native_view,
384 REGSET_FPU,
385 0, sizeof(struct user_fpu_struct),
386 (const void __user *)data);
387#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 default:
389 ret = ptrace_request(child, request, addr, data);
390 break;
391 }
Paul Mundtdd762792008-12-10 20:14:15 +0900392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return ret;
394}
395
Christoph Hellwig481bed42005-11-07 00:59:47 -0800396asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
397{
Christoph Hellwig481bed42005-11-07 00:59:47 -0800398#define WPC_DBRMODE 0x0d104008
399 static int first_call = 1;
400
401 lock_kernel();
402 if (first_call) {
403 /* Set WPC.DBRMODE to 0. This makes all debug events get
404 * delivered through RESVEC, i.e. into the handlers in entry.S.
405 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
406 * would normally be left set to 1, which makes debug events get
407 * delivered through DBRVEC, i.e. into the remote gdb's
408 * handlers. This prevents ptrace getting them, and confuses
409 * the remote gdb.) */
410 printk("DBRMODE set to 0 to permit native debugging\n");
411 poke_real_address_q(WPC_DBRMODE, 0);
412 first_call = 0;
413 }
414 unlock_kernel();
415
416 return sys_ptrace(request, pid, addr, data);
417}
418
Paul Mundt9e5e2112008-07-30 20:05:35 +0900419static inline int audit_arch(void)
420{
421 int arch = EM_SH;
422
423#ifdef CONFIG_64BIT
424 arch |= __AUDIT_ARCH_64BIT;
425#endif
426#ifdef CONFIG_CPU_LITTLE_ENDIAN
427 arch |= __AUDIT_ARCH_LE;
428#endif
429
430 return arch;
431}
432
Paul Mundtab99c732008-07-30 19:55:30 +0900433asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Paul Mundtab99c732008-07-30 19:55:30 +0900435 long long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Paul Mundtc4637d42008-07-30 15:30:52 +0900437 secure_computing(regs->regs[9]);
438
Paul Mundtab99c732008-07-30 19:55:30 +0900439 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
440 tracehook_report_syscall_entry(regs))
441 /*
442 * Tracing decided this syscall should not happen.
443 * We'll return a bogus call number to get an ENOSYS
444 * error, but leave the original number in regs->regs[0].
445 */
446 ret = -1LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Paul Mundta74f7e02009-09-16 14:30:34 +0900448 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
449 trace_sys_enter(regs, regs->regs[9]);
450
Paul Mundtab99c732008-07-30 19:55:30 +0900451 if (unlikely(current->audit_context))
Paul Mundt9e5e2112008-07-30 20:05:35 +0900452 audit_syscall_entry(audit_arch(), regs->regs[1],
Paul Mundt4b27c472007-11-28 19:58:11 +0900453 regs->regs[2], regs->regs[3],
454 regs->regs[4], regs->regs[5]);
Paul Mundtab99c732008-07-30 19:55:30 +0900455
456 return ret ?: regs->regs[9];
457}
458
459asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
460{
Paul Mundt4b505db2010-02-15 14:17:45 +0900461 int step;
462
Paul Mundtab99c732008-07-30 19:55:30 +0900463 if (unlikely(current->audit_context))
464 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
465 regs->regs[9]);
466
Paul Mundta74f7e02009-09-16 14:30:34 +0900467 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
468 trace_sys_exit(regs, regs->regs[9]);
469
Paul Mundt4b505db2010-02-15 14:17:45 +0900470 step = test_thread_flag(TIF_SINGLESTEP);
471 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
472 tracehook_report_syscall_exit(regs, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475/* Called with interrupts disabled */
476asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
477{
478 /* This is called after a single step exception (DEBUGSS).
479 There is no need to change the PC, as it is a post-execution
480 exception, as entry.S does not do anything to the PC for DEBUGSS.
481 We need to clear the Single Step setting in SR to avoid
482 continually stepping. */
483 local_irq_enable();
484 regs->sr &= ~SR_SSTEP;
485 force_sig(SIGTRAP, current);
486}
487
488/* Called with interrupts disabled */
489asmlinkage void do_software_break_point(unsigned long long vec,
490 struct pt_regs *regs)
491{
492 /* We need to forward step the PC, to counteract the backstep done
493 in signal.c. */
494 local_irq_enable();
495 force_sig(SIGTRAP, current);
496 regs->pc += 4;
497}
498
499/*
500 * Called by kernel/ptrace.c when detaching..
501 *
502 * Make sure single step bits etc are not set.
503 */
504void ptrace_disable(struct task_struct *child)
505{
Paul Mundtc459dbf2008-07-30 19:09:31 +0900506 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}