1/*
2 * arch/sh/kernel/ptrace_64.c
3 *
4 * Copyright (C) 2000, 2001  Paolo Alberelli
5 * Copyright (C) 2003 - 2008  Paul Mundt
6 *
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 *
14 * 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.
17 */
18#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/bitops.h>
24#include <linux/errno.h>
25#include <linux/ptrace.h>
26#include <linux/user.h>
27#include <linux/signal.h>
28#include <linux/syscalls.h>
29#include <linux/audit.h>
30#include <linux/seccomp.h>
31#include <linux/tracehook.h>
32#include <linux/elf.h>
33#include <linux/regset.h>
34#include <asm/io.h>
35#include <asm/uaccess.h>
36#include <asm/pgtable.h>
37#include <asm/processor.h>
38#include <asm/mmu_context.h>
39#include <asm/syscalls.h>
40#include <asm/fpu.h>
41#include <asm/traps.h>
42
43#define CREATE_TRACE_POINTS
44#include <trace/events/syscalls.h>
45
46/* 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) {
84		enable_fpu();
85		save_fpu(task);
86		disable_fpu();
87		last_task_used_math = 0;
88		regs->sr |= SR_FD;
89	}
90
91	tmp = ((long *)task->thread.xstate)[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		init_fpu(task);
118	} else if (last_task_used_math == task) {
119		enable_fpu();
120		save_fpu(task);
121		disable_fpu();
122		last_task_used_math = 0;
123		regs->sr |= SR_FD;
124	}
125
126	((long *)task->thread.xstate)[addr / sizeof(unsigned long)] = data;
127	return 0;
128}
129
130void user_enable_single_step(struct task_struct *child)
131{
132	struct pt_regs *regs = child->thread.uregs;
133
134	regs->sr |= SR_SSTEP;	/* auto-resetting upon exception */
135
136	set_tsk_thread_flag(child, TIF_SINGLESTEP);
137}
138
139void user_disable_single_step(struct task_struct *child)
140{
141	struct pt_regs *regs = child->thread.uregs;
142
143	regs->sr &= ~SR_SSTEP;
144
145	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
146}
147
148static int genregs_get(struct task_struct *target,
149		       const struct user_regset *regset,
150		       unsigned int pos, unsigned int count,
151		       void *kbuf, void __user *ubuf)
152{
153	const struct pt_regs *regs = task_pt_regs(target);
154	int ret;
155
156	/* PC, SR, SYSCALL */
157	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
158				  &regs->pc,
159				  0, 3 * sizeof(unsigned long long));
160
161	/* R1 -> R63 */
162	if (!ret)
163		ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
164					  regs->regs,
165					  offsetof(struct pt_regs, regs[0]),
166					  63 * sizeof(unsigned long long));
167	/* TR0 -> TR7 */
168	if (!ret)
169		ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
170					  regs->tregs,
171					  offsetof(struct pt_regs, tregs[0]),
172					  8 * sizeof(unsigned long long));
173
174	if (!ret)
175		ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
176					       sizeof(struct pt_regs), -1);
177
178	return ret;
179}
180
181static int genregs_set(struct task_struct *target,
182		       const struct user_regset *regset,
183		       unsigned int pos, unsigned int count,
184		       const void *kbuf, const void __user *ubuf)
185{
186	struct pt_regs *regs = task_pt_regs(target);
187	int ret;
188
189	/* PC, SR, SYSCALL */
190	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
191				 &regs->pc,
192				 0, 3 * sizeof(unsigned long long));
193
194	/* R1 -> R63 */
195	if (!ret && count > 0)
196		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
197					 regs->regs,
198					 offsetof(struct pt_regs, regs[0]),
199					 63 * sizeof(unsigned long long));
200
201	/* TR0 -> TR7 */
202	if (!ret && count > 0)
203		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
204					 regs->tregs,
205					 offsetof(struct pt_regs, tregs[0]),
206					 8 * sizeof(unsigned long long));
207
208	if (!ret)
209		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
210						sizeof(struct pt_regs), -1);
211
212	return ret;
213}
214
215#ifdef CONFIG_SH_FPU
216int fpregs_get(struct task_struct *target,
217	       const struct user_regset *regset,
218	       unsigned int pos, unsigned int count,
219	       void *kbuf, void __user *ubuf)
220{
221	int ret;
222
223	ret = init_fpu(target);
224	if (ret)
225		return ret;
226
227	return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
228				   &target->thread.xstate->hardfpu, 0, -1);
229}
230
231static int fpregs_set(struct task_struct *target,
232		       const struct user_regset *regset,
233		       unsigned int pos, unsigned int count,
234		       const void *kbuf, const void __user *ubuf)
235{
236	int ret;
237
238	ret = init_fpu(target);
239	if (ret)
240		return ret;
241
242	set_stopped_child_used_math(target);
243
244	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
245				  &target->thread.xstate->hardfpu, 0, -1);
246}
247
248static int fpregs_active(struct task_struct *target,
249			 const struct user_regset *regset)
250{
251	return tsk_used_math(target) ? regset->n : 0;
252}
253#endif
254
255const struct pt_regs_offset regoffset_table[] = {
256	REG_OFFSET_NAME(pc),
257	REG_OFFSET_NAME(sr),
258	REG_OFFSET_NAME(syscall_nr),
259	REGS_OFFSET_NAME(0),
260	REGS_OFFSET_NAME(1),
261	REGS_OFFSET_NAME(2),
262	REGS_OFFSET_NAME(3),
263	REGS_OFFSET_NAME(4),
264	REGS_OFFSET_NAME(5),
265	REGS_OFFSET_NAME(6),
266	REGS_OFFSET_NAME(7),
267	REGS_OFFSET_NAME(8),
268	REGS_OFFSET_NAME(9),
269	REGS_OFFSET_NAME(10),
270	REGS_OFFSET_NAME(11),
271	REGS_OFFSET_NAME(12),
272	REGS_OFFSET_NAME(13),
273	REGS_OFFSET_NAME(14),
274	REGS_OFFSET_NAME(15),
275	REGS_OFFSET_NAME(16),
276	REGS_OFFSET_NAME(17),
277	REGS_OFFSET_NAME(18),
278	REGS_OFFSET_NAME(19),
279	REGS_OFFSET_NAME(20),
280	REGS_OFFSET_NAME(21),
281	REGS_OFFSET_NAME(22),
282	REGS_OFFSET_NAME(23),
283	REGS_OFFSET_NAME(24),
284	REGS_OFFSET_NAME(25),
285	REGS_OFFSET_NAME(26),
286	REGS_OFFSET_NAME(27),
287	REGS_OFFSET_NAME(28),
288	REGS_OFFSET_NAME(29),
289	REGS_OFFSET_NAME(30),
290	REGS_OFFSET_NAME(31),
291	REGS_OFFSET_NAME(32),
292	REGS_OFFSET_NAME(33),
293	REGS_OFFSET_NAME(34),
294	REGS_OFFSET_NAME(35),
295	REGS_OFFSET_NAME(36),
296	REGS_OFFSET_NAME(37),
297	REGS_OFFSET_NAME(38),
298	REGS_OFFSET_NAME(39),
299	REGS_OFFSET_NAME(40),
300	REGS_OFFSET_NAME(41),
301	REGS_OFFSET_NAME(42),
302	REGS_OFFSET_NAME(43),
303	REGS_OFFSET_NAME(44),
304	REGS_OFFSET_NAME(45),
305	REGS_OFFSET_NAME(46),
306	REGS_OFFSET_NAME(47),
307	REGS_OFFSET_NAME(48),
308	REGS_OFFSET_NAME(49),
309	REGS_OFFSET_NAME(50),
310	REGS_OFFSET_NAME(51),
311	REGS_OFFSET_NAME(52),
312	REGS_OFFSET_NAME(53),
313	REGS_OFFSET_NAME(54),
314	REGS_OFFSET_NAME(55),
315	REGS_OFFSET_NAME(56),
316	REGS_OFFSET_NAME(57),
317	REGS_OFFSET_NAME(58),
318	REGS_OFFSET_NAME(59),
319	REGS_OFFSET_NAME(60),
320	REGS_OFFSET_NAME(61),
321	REGS_OFFSET_NAME(62),
322	REGS_OFFSET_NAME(63),
323	TREGS_OFFSET_NAME(0),
324	TREGS_OFFSET_NAME(1),
325	TREGS_OFFSET_NAME(2),
326	TREGS_OFFSET_NAME(3),
327	TREGS_OFFSET_NAME(4),
328	TREGS_OFFSET_NAME(5),
329	TREGS_OFFSET_NAME(6),
330	TREGS_OFFSET_NAME(7),
331	REG_OFFSET_END,
332};
333
334/*
335 * These are our native regset flavours.
336 */
337enum sh_regset {
338	REGSET_GENERAL,
339#ifdef CONFIG_SH_FPU
340	REGSET_FPU,
341#endif
342};
343
344static const struct user_regset sh_regsets[] = {
345	/*
346	 * Format is:
347	 *	PC, SR, SYSCALL,
348	 *	R1 --> R63,
349	 *	TR0 --> TR7,
350	 */
351	[REGSET_GENERAL] = {
352		.core_note_type	= NT_PRSTATUS,
353		.n		= ELF_NGREG,
354		.size		= sizeof(long long),
355		.align		= sizeof(long long),
356		.get		= genregs_get,
357		.set		= genregs_set,
358	},
359
360#ifdef CONFIG_SH_FPU
361	[REGSET_FPU] = {
362		.core_note_type	= NT_PRFPREG,
363		.n		= sizeof(struct user_fpu_struct) /
364				  sizeof(long long),
365		.size		= sizeof(long long),
366		.align		= sizeof(long long),
367		.get		= fpregs_get,
368		.set		= fpregs_set,
369		.active		= fpregs_active,
370	},
371#endif
372};
373
374static const struct user_regset_view user_sh64_native_view = {
375	.name		= "sh64",
376	.e_machine	= EM_SH,
377	.regsets	= sh_regsets,
378	.n		= ARRAY_SIZE(sh_regsets),
379};
380
381const struct user_regset_view *task_user_regset_view(struct task_struct *task)
382{
383	return &user_sh64_native_view;
384}
385
386long arch_ptrace(struct task_struct *child, long request,
387		 unsigned long addr, unsigned long data)
388{
389	int ret;
390	unsigned long __user *datap = (unsigned long __user *) data;
391
392	switch (request) {
393	/* read the word at location addr in the USER area. */
394	case PTRACE_PEEKUSR: {
395		unsigned long tmp;
396
397		ret = -EIO;
398		if ((addr & 3) || addr < 0)
399			break;
400
401		if (addr < sizeof(struct pt_regs))
402			tmp = get_stack_long(child, addr);
403		else if ((addr >= offsetof(struct user, fpu)) &&
404			 (addr <  offsetof(struct user, u_fpvalid))) {
405			unsigned long index;
406			ret = init_fpu(child);
407			if (ret)
408				break;
409			index = addr - offsetof(struct user, fpu);
410			tmp = get_fpu_long(child, index);
411		} else if (addr == offsetof(struct user, u_fpvalid)) {
412			tmp = !!tsk_used_math(child);
413		} else {
414			break;
415		}
416		ret = put_user(tmp, datap);
417		break;
418	}
419
420	case PTRACE_POKEUSR:
421                /* write the word at location addr in the USER area. We must
422                   disallow any changes to certain SR bits or u_fpvalid, since
423                   this could crash the kernel or result in a security
424                   loophole. */
425		ret = -EIO;
426		if ((addr & 3) || addr < 0)
427			break;
428
429		if (addr < sizeof(struct pt_regs)) {
430			/* Ignore change of top 32 bits of SR */
431			if (addr == offsetof (struct pt_regs, sr)+4)
432			{
433				ret = 0;
434				break;
435			}
436			/* If lower 32 bits of SR, ignore non-user bits */
437			if (addr == offsetof (struct pt_regs, sr))
438			{
439				long cursr = get_stack_long(child, addr);
440				data &= ~(SR_MASK);
441				data |= (cursr & SR_MASK);
442			}
443			ret = put_stack_long(child, addr, data);
444		}
445		else if ((addr >= offsetof(struct user, fpu)) &&
446			 (addr <  offsetof(struct user, u_fpvalid))) {
447			unsigned long index;
448			ret = init_fpu(child);
449			if (ret)
450				break;
451			index = addr - offsetof(struct user, fpu);
452			ret = put_fpu_long(child, index, data);
453		}
454		break;
455
456	case PTRACE_GETREGS:
457		return copy_regset_to_user(child, &user_sh64_native_view,
458					   REGSET_GENERAL,
459					   0, sizeof(struct pt_regs),
460					   datap);
461	case PTRACE_SETREGS:
462		return copy_regset_from_user(child, &user_sh64_native_view,
463					     REGSET_GENERAL,
464					     0, sizeof(struct pt_regs),
465					     datap);
466#ifdef CONFIG_SH_FPU
467	case PTRACE_GETFPREGS:
468		return copy_regset_to_user(child, &user_sh64_native_view,
469					   REGSET_FPU,
470					   0, sizeof(struct user_fpu_struct),
471					   datap);
472	case PTRACE_SETFPREGS:
473		return copy_regset_from_user(child, &user_sh64_native_view,
474					     REGSET_FPU,
475					     0, sizeof(struct user_fpu_struct),
476					     datap);
477#endif
478	default:
479		ret = ptrace_request(child, request, addr, data);
480		break;
481	}
482
483	return ret;
484}
485
486asmlinkage int sh64_ptrace(long request, long pid,
487			   unsigned long addr, unsigned long data)
488{
489#define WPC_DBRMODE 0x0d104008
490	static unsigned long first_call;
491
492	if (!test_and_set_bit(0, &first_call)) {
493		/* Set WPC.DBRMODE to 0.  This makes all debug events get
494		 * delivered through RESVEC, i.e. into the handlers in entry.S.
495		 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
496		 * would normally be left set to 1, which makes debug events get
497		 * delivered through DBRVEC, i.e. into the remote gdb's
498		 * handlers.  This prevents ptrace getting them, and confuses
499		 * the remote gdb.) */
500		printk("DBRMODE set to 0 to permit native debugging\n");
501		poke_real_address_q(WPC_DBRMODE, 0);
502	}
503
504	return sys_ptrace(request, pid, addr, data);
505}
506
507asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
508{
509	long long ret = 0;
510
511	secure_computing_strict(regs->regs[9]);
512
513	if (test_thread_flag(TIF_SYSCALL_TRACE) &&
514	    tracehook_report_syscall_entry(regs))
515		/*
516		 * Tracing decided this syscall should not happen.
517		 * We'll return a bogus call number to get an ENOSYS
518		 * error, but leave the original number in regs->regs[0].
519		 */
520		ret = -1LL;
521
522	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
523		trace_sys_enter(regs, regs->regs[9]);
524
525	audit_syscall_entry(regs->regs[1], regs->regs[2], regs->regs[3],
526			    regs->regs[4], regs->regs[5]);
527
528	return ret ?: regs->regs[9];
529}
530
531asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
532{
533	int step;
534
535	audit_syscall_exit(regs);
536
537	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
538		trace_sys_exit(regs, regs->regs[9]);
539
540	step = test_thread_flag(TIF_SINGLESTEP);
541	if (step || test_thread_flag(TIF_SYSCALL_TRACE))
542		tracehook_report_syscall_exit(regs, step);
543}
544
545/* Called with interrupts disabled */
546asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
547{
548	/* This is called after a single step exception (DEBUGSS).
549	   There is no need to change the PC, as it is a post-execution
550	   exception, as entry.S does not do anything to the PC for DEBUGSS.
551	   We need to clear the Single Step setting in SR to avoid
552	   continually stepping. */
553	local_irq_enable();
554	regs->sr &= ~SR_SSTEP;
555	force_sig(SIGTRAP, current);
556}
557
558/* Called with interrupts disabled */
559BUILD_TRAP_HANDLER(breakpoint)
560{
561	TRAP_HANDLER_DECL;
562
563	/* We need to forward step the PC, to counteract the backstep done
564	   in signal.c. */
565	local_irq_enable();
566	force_sig(SIGTRAP, current);
567	regs->pc += 4;
568}
569
570/*
571 * Called by kernel/ptrace.c when detaching..
572 *
573 * Make sure single step bits etc are not set.
574 */
575void ptrace_disable(struct task_struct *child)
576{
577	user_disable_single_step(child);
578}
579