xref: /linux/arch/sh/kernel/ptrace_32.c (revision 4dc0da86967d5463708631d02a70cfed5b104884)
148b22cf9SPaul Mundt /*
2934135c1SPaul Mundt  * SuperH process tracing
348b22cf9SPaul Mundt  *
4934135c1SPaul Mundt  * Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
534d0b5afSPaul Mundt  * Copyright (C) 2002 - 2009  Paul Mundt
648b22cf9SPaul Mundt  *
7934135c1SPaul Mundt  * Audit support by Yuichi Nakamura <ynakam@hitachisoft.jp>
8934135c1SPaul Mundt  *
9934135c1SPaul Mundt  * This file is subject to the terms and conditions of the GNU General Public
10934135c1SPaul Mundt  * License.  See the file "COPYING" in the main directory of this archive
11934135c1SPaul Mundt  * for more details.
1248b22cf9SPaul Mundt  */
1348b22cf9SPaul Mundt #include <linux/kernel.h>
1448b22cf9SPaul Mundt #include <linux/sched.h>
1548b22cf9SPaul Mundt #include <linux/mm.h>
1648b22cf9SPaul Mundt #include <linux/smp.h>
1748b22cf9SPaul Mundt #include <linux/errno.h>
1848b22cf9SPaul Mundt #include <linux/ptrace.h>
1948b22cf9SPaul Mundt #include <linux/user.h>
2048b22cf9SPaul Mundt #include <linux/security.h>
2148b22cf9SPaul Mundt #include <linux/signal.h>
2248b22cf9SPaul Mundt #include <linux/io.h>
231322b9deSYuichi Nakamura #include <linux/audit.h>
24c4637d47SPaul Mundt #include <linux/seccomp.h>
25ab99c733SPaul Mundt #include <linux/tracehook.h>
26934135c1SPaul Mundt #include <linux/elf.h>
27934135c1SPaul Mundt #include <linux/regset.h>
2834d0b5afSPaul Mundt #include <linux/hw_breakpoint.h>
2948b22cf9SPaul Mundt #include <asm/uaccess.h>
3048b22cf9SPaul Mundt #include <asm/pgtable.h>
3148b22cf9SPaul Mundt #include <asm/system.h>
3248b22cf9SPaul Mundt #include <asm/processor.h>
3348b22cf9SPaul Mundt #include <asm/mmu_context.h>
34fa43972fSPaul Mundt #include <asm/syscalls.h>
35e7ab3cd2SPaul Mundt #include <asm/fpu.h>
3648b22cf9SPaul Mundt 
37a74f7e04SPaul Mundt #define CREATE_TRACE_POINTS
38a74f7e04SPaul Mundt #include <trace/events/syscalls.h>
39c652d780SMatt Fleming 
4048b22cf9SPaul Mundt /*
4148b22cf9SPaul Mundt  * This routine will get a word off of the process kernel stack.
4248b22cf9SPaul Mundt  */
4348b22cf9SPaul Mundt static inline int get_stack_long(struct task_struct *task, int offset)
4448b22cf9SPaul Mundt {
4548b22cf9SPaul Mundt 	unsigned char *stack;
4648b22cf9SPaul Mundt 
4748b22cf9SPaul Mundt 	stack = (unsigned char *)task_pt_regs(task);
4848b22cf9SPaul Mundt 	stack += offset;
4948b22cf9SPaul Mundt 	return (*((int *)stack));
5048b22cf9SPaul Mundt }
5148b22cf9SPaul Mundt 
5248b22cf9SPaul Mundt /*
5348b22cf9SPaul Mundt  * This routine will put a word on the process kernel stack.
5448b22cf9SPaul Mundt  */
5548b22cf9SPaul Mundt static inline int put_stack_long(struct task_struct *task, int offset,
5648b22cf9SPaul Mundt 				 unsigned long data)
5748b22cf9SPaul Mundt {
5848b22cf9SPaul Mundt 	unsigned char *stack;
5948b22cf9SPaul Mundt 
6048b22cf9SPaul Mundt 	stack = (unsigned char *)task_pt_regs(task);
6148b22cf9SPaul Mundt 	stack += offset;
6248b22cf9SPaul Mundt 	*(unsigned long *) stack = data;
6348b22cf9SPaul Mundt 	return 0;
6448b22cf9SPaul Mundt }
6548b22cf9SPaul Mundt 
66a8b0ca17SPeter Zijlstra void ptrace_triggered(struct perf_event *bp,
6734d0b5afSPaul Mundt 		      struct perf_sample_data *data, struct pt_regs *regs)
6834d0b5afSPaul Mundt {
6934d0b5afSPaul Mundt 	struct perf_event_attr attr;
7034d0b5afSPaul Mundt 
7134d0b5afSPaul Mundt 	/*
7234d0b5afSPaul Mundt 	 * Disable the breakpoint request here since ptrace has defined a
7334d0b5afSPaul Mundt 	 * one-shot behaviour for breakpoint exceptions.
7434d0b5afSPaul Mundt 	 */
7534d0b5afSPaul Mundt 	attr = bp->attr;
7634d0b5afSPaul Mundt 	attr.disabled = true;
7734d0b5afSPaul Mundt 	modify_user_hw_breakpoint(bp, &attr);
7834d0b5afSPaul Mundt }
7934d0b5afSPaul Mundt 
8034d0b5afSPaul Mundt static int set_single_step(struct task_struct *tsk, unsigned long addr)
8134d0b5afSPaul Mundt {
8234d0b5afSPaul Mundt 	struct thread_struct *thread = &tsk->thread;
8334d0b5afSPaul Mundt 	struct perf_event *bp;
8434d0b5afSPaul Mundt 	struct perf_event_attr attr;
8534d0b5afSPaul Mundt 
8634d0b5afSPaul Mundt 	bp = thread->ptrace_bps[0];
8734d0b5afSPaul Mundt 	if (!bp) {
8873266fc1SFrederic Weisbecker 		ptrace_breakpoint_init(&attr);
8934d0b5afSPaul Mundt 
9034d0b5afSPaul Mundt 		attr.bp_addr = addr;
9134d0b5afSPaul Mundt 		attr.bp_len = HW_BREAKPOINT_LEN_2;
9234d0b5afSPaul Mundt 		attr.bp_type = HW_BREAKPOINT_R;
9334d0b5afSPaul Mundt 
94*4dc0da86SAvi Kivity 		bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
95*4dc0da86SAvi Kivity 						 NULL, tsk);
9634d0b5afSPaul Mundt 		if (IS_ERR(bp))
9734d0b5afSPaul Mundt 			return PTR_ERR(bp);
9834d0b5afSPaul Mundt 
9934d0b5afSPaul Mundt 		thread->ptrace_bps[0] = bp;
10034d0b5afSPaul Mundt 	} else {
10134d0b5afSPaul Mundt 		int err;
10234d0b5afSPaul Mundt 
10334d0b5afSPaul Mundt 		attr = bp->attr;
10434d0b5afSPaul Mundt 		attr.bp_addr = addr;
105fb7f045aSDavid Engraf 		/* reenable breakpoint */
106fb7f045aSDavid Engraf 		attr.disabled = false;
10734d0b5afSPaul Mundt 		err = modify_user_hw_breakpoint(bp, &attr);
10834d0b5afSPaul Mundt 		if (unlikely(err))
10934d0b5afSPaul Mundt 			return err;
11034d0b5afSPaul Mundt 	}
11134d0b5afSPaul Mundt 
11234d0b5afSPaul Mundt 	return 0;
11334d0b5afSPaul Mundt }
11434d0b5afSPaul Mundt 
115c459dbf2SPaul Mundt void user_enable_single_step(struct task_struct *child)
116c459dbf2SPaul Mundt {
11734d0b5afSPaul Mundt 	unsigned long pc = get_stack_long(child, offsetof(struct pt_regs, pc));
118c459dbf2SPaul Mundt 
119c459dbf2SPaul Mundt 	set_tsk_thread_flag(child, TIF_SINGLESTEP);
12034d0b5afSPaul Mundt 
121e0ac8457SFrederic Weisbecker 	if (ptrace_get_breakpoints(child) < 0)
122e0ac8457SFrederic Weisbecker 		return;
123e0ac8457SFrederic Weisbecker 
12434d0b5afSPaul Mundt 	set_single_step(child, pc);
125e0ac8457SFrederic Weisbecker 	ptrace_put_breakpoints(child);
126c459dbf2SPaul Mundt }
127c459dbf2SPaul Mundt 
128c459dbf2SPaul Mundt void user_disable_single_step(struct task_struct *child)
12948b22cf9SPaul Mundt {
13048b22cf9SPaul Mundt 	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
13148b22cf9SPaul Mundt }
13248b22cf9SPaul Mundt 
13348b22cf9SPaul Mundt /*
13448b22cf9SPaul Mundt  * Called by kernel/ptrace.c when detaching..
13548b22cf9SPaul Mundt  *
13648b22cf9SPaul Mundt  * Make sure single step bits etc are not set.
13748b22cf9SPaul Mundt  */
13848b22cf9SPaul Mundt void ptrace_disable(struct task_struct *child)
13948b22cf9SPaul Mundt {
140c459dbf2SPaul Mundt 	user_disable_single_step(child);
14148b22cf9SPaul Mundt }
14248b22cf9SPaul Mundt 
143934135c1SPaul Mundt static int genregs_get(struct task_struct *target,
144934135c1SPaul Mundt 		       const struct user_regset *regset,
145934135c1SPaul Mundt 		       unsigned int pos, unsigned int count,
146934135c1SPaul Mundt 		       void *kbuf, void __user *ubuf)
147934135c1SPaul Mundt {
148934135c1SPaul Mundt 	const struct pt_regs *regs = task_pt_regs(target);
149934135c1SPaul Mundt 	int ret;
150934135c1SPaul Mundt 
151934135c1SPaul Mundt 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
152934135c1SPaul Mundt 				  regs->regs,
153934135c1SPaul Mundt 				  0, 16 * sizeof(unsigned long));
154934135c1SPaul Mundt 	if (!ret)
155934135c1SPaul Mundt 		/* PC, PR, SR, GBR, MACH, MACL, TRA */
156934135c1SPaul Mundt 		ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
157934135c1SPaul Mundt 					  &regs->pc,
158934135c1SPaul Mundt 					  offsetof(struct pt_regs, pc),
159934135c1SPaul Mundt 					  sizeof(struct pt_regs));
160934135c1SPaul Mundt 	if (!ret)
161934135c1SPaul Mundt 		ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
162934135c1SPaul Mundt 					       sizeof(struct pt_regs), -1);
163934135c1SPaul Mundt 
164934135c1SPaul Mundt 	return ret;
165934135c1SPaul Mundt }
166934135c1SPaul Mundt 
167934135c1SPaul Mundt static int genregs_set(struct task_struct *target,
168934135c1SPaul Mundt 		       const struct user_regset *regset,
169934135c1SPaul Mundt 		       unsigned int pos, unsigned int count,
170934135c1SPaul Mundt 		       const void *kbuf, const void __user *ubuf)
171934135c1SPaul Mundt {
172934135c1SPaul Mundt 	struct pt_regs *regs = task_pt_regs(target);
173934135c1SPaul Mundt 	int ret;
174934135c1SPaul Mundt 
175934135c1SPaul Mundt 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
176934135c1SPaul Mundt 				 regs->regs,
177934135c1SPaul Mundt 				 0, 16 * sizeof(unsigned long));
178934135c1SPaul Mundt 	if (!ret && count > 0)
179934135c1SPaul Mundt 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
180934135c1SPaul Mundt 					 &regs->pc,
181934135c1SPaul Mundt 					 offsetof(struct pt_regs, pc),
182934135c1SPaul Mundt 					 sizeof(struct pt_regs));
183934135c1SPaul Mundt 	if (!ret)
184934135c1SPaul Mundt 		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
185934135c1SPaul Mundt 						sizeof(struct pt_regs), -1);
186934135c1SPaul Mundt 
187934135c1SPaul Mundt 	return ret;
188934135c1SPaul Mundt }
189934135c1SPaul Mundt 
190e7ab3cd2SPaul Mundt #ifdef CONFIG_SH_FPU
191e7ab3cd2SPaul Mundt int fpregs_get(struct task_struct *target,
192e7ab3cd2SPaul Mundt 	       const struct user_regset *regset,
193e7ab3cd2SPaul Mundt 	       unsigned int pos, unsigned int count,
194e7ab3cd2SPaul Mundt 	       void *kbuf, void __user *ubuf)
195e7ab3cd2SPaul Mundt {
196e7ab3cd2SPaul Mundt 	int ret;
197e7ab3cd2SPaul Mundt 
198e7ab3cd2SPaul Mundt 	ret = init_fpu(target);
199e7ab3cd2SPaul Mundt 	if (ret)
200e7ab3cd2SPaul Mundt 		return ret;
201e7ab3cd2SPaul Mundt 
202e7ab3cd2SPaul Mundt 	if ((boot_cpu_data.flags & CPU_HAS_FPU))
203e7ab3cd2SPaul Mundt 		return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
2040ea820cfSPaul Mundt 					   &target->thread.xstate->hardfpu, 0, -1);
205e7ab3cd2SPaul Mundt 
206e7ab3cd2SPaul Mundt 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
2070ea820cfSPaul Mundt 				   &target->thread.xstate->softfpu, 0, -1);
208e7ab3cd2SPaul Mundt }
209e7ab3cd2SPaul Mundt 
210e7ab3cd2SPaul Mundt static int fpregs_set(struct task_struct *target,
211e7ab3cd2SPaul Mundt 		       const struct user_regset *regset,
212e7ab3cd2SPaul Mundt 		       unsigned int pos, unsigned int count,
213e7ab3cd2SPaul Mundt 		       const void *kbuf, const void __user *ubuf)
214e7ab3cd2SPaul Mundt {
215e7ab3cd2SPaul Mundt 	int ret;
216e7ab3cd2SPaul Mundt 
217e7ab3cd2SPaul Mundt 	ret = init_fpu(target);
218e7ab3cd2SPaul Mundt 	if (ret)
219e7ab3cd2SPaul Mundt 		return ret;
220e7ab3cd2SPaul Mundt 
221e7ab3cd2SPaul Mundt 	set_stopped_child_used_math(target);
222e7ab3cd2SPaul Mundt 
223e7ab3cd2SPaul Mundt 	if ((boot_cpu_data.flags & CPU_HAS_FPU))
224e7ab3cd2SPaul Mundt 		return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
2250ea820cfSPaul Mundt 					  &target->thread.xstate->hardfpu, 0, -1);
226e7ab3cd2SPaul Mundt 
227e7ab3cd2SPaul Mundt 	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
2280ea820cfSPaul Mundt 				  &target->thread.xstate->softfpu, 0, -1);
229e7ab3cd2SPaul Mundt }
230e7ab3cd2SPaul Mundt 
231e7ab3cd2SPaul Mundt static int fpregs_active(struct task_struct *target,
232e7ab3cd2SPaul Mundt 			 const struct user_regset *regset)
233e7ab3cd2SPaul Mundt {
234e7ab3cd2SPaul Mundt 	return tsk_used_math(target) ? regset->n : 0;
235e7ab3cd2SPaul Mundt }
236e7ab3cd2SPaul Mundt #endif
237e7ab3cd2SPaul Mundt 
2385dadb343SPaul Mundt #ifdef CONFIG_SH_DSP
2395dadb343SPaul Mundt static int dspregs_get(struct task_struct *target,
2405dadb343SPaul Mundt 		       const struct user_regset *regset,
2415dadb343SPaul Mundt 		       unsigned int pos, unsigned int count,
2425dadb343SPaul Mundt 		       void *kbuf, void __user *ubuf)
2435dadb343SPaul Mundt {
24401ab1039SMichael Trimarchi 	const struct pt_dspregs *regs =
24501ab1039SMichael Trimarchi 		(struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
2465dadb343SPaul Mundt 	int ret;
2475dadb343SPaul Mundt 
2485dadb343SPaul Mundt 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
2495dadb343SPaul Mundt 				  0, sizeof(struct pt_dspregs));
2505dadb343SPaul Mundt 	if (!ret)
2515dadb343SPaul Mundt 		ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
2525dadb343SPaul Mundt 					       sizeof(struct pt_dspregs), -1);
2535dadb343SPaul Mundt 
2545dadb343SPaul Mundt 	return ret;
2555dadb343SPaul Mundt }
2565dadb343SPaul Mundt 
2575dadb343SPaul Mundt static int dspregs_set(struct task_struct *target,
2585dadb343SPaul Mundt 		       const struct user_regset *regset,
2595dadb343SPaul Mundt 		       unsigned int pos, unsigned int count,
2605dadb343SPaul Mundt 		       const void *kbuf, const void __user *ubuf)
2615dadb343SPaul Mundt {
26201ab1039SMichael Trimarchi 	struct pt_dspregs *regs =
26301ab1039SMichael Trimarchi 		(struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
2645dadb343SPaul Mundt 	int ret;
2655dadb343SPaul Mundt 
2665dadb343SPaul Mundt 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
2675dadb343SPaul Mundt 				 0, sizeof(struct pt_dspregs));
2685dadb343SPaul Mundt 	if (!ret)
2695dadb343SPaul Mundt 		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
2705dadb343SPaul Mundt 						sizeof(struct pt_dspregs), -1);
2715dadb343SPaul Mundt 
2725dadb343SPaul Mundt 	return ret;
2735dadb343SPaul Mundt }
27472461997SPaul Mundt 
27572461997SPaul Mundt static int dspregs_active(struct task_struct *target,
27672461997SPaul Mundt 			  const struct user_regset *regset)
27772461997SPaul Mundt {
27872461997SPaul Mundt 	struct pt_regs *regs = task_pt_regs(target);
27972461997SPaul Mundt 
28072461997SPaul Mundt 	return regs->sr & SR_DSP ? regset->n : 0;
28172461997SPaul Mundt }
2825dadb343SPaul Mundt #endif
2835dadb343SPaul Mundt 
284eaaaeef3SPaul Mundt const struct pt_regs_offset regoffset_table[] = {
285eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(0),
286eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(1),
287eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(2),
288eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(3),
289eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(4),
290eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(5),
291eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(6),
292eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(7),
293eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(8),
294eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(9),
295eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(10),
296eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(11),
297eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(12),
298eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(13),
299eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(14),
300eaaaeef3SPaul Mundt 	REGS_OFFSET_NAME(15),
301eaaaeef3SPaul Mundt 	REG_OFFSET_NAME(pc),
302eaaaeef3SPaul Mundt 	REG_OFFSET_NAME(pr),
303eaaaeef3SPaul Mundt 	REG_OFFSET_NAME(sr),
304eaaaeef3SPaul Mundt 	REG_OFFSET_NAME(gbr),
305eaaaeef3SPaul Mundt 	REG_OFFSET_NAME(mach),
306eaaaeef3SPaul Mundt 	REG_OFFSET_NAME(macl),
307eaaaeef3SPaul Mundt 	REG_OFFSET_NAME(tra),
308eaaaeef3SPaul Mundt 	REG_OFFSET_END,
309eaaaeef3SPaul Mundt };
310eaaaeef3SPaul Mundt 
311934135c1SPaul Mundt /*
312934135c1SPaul Mundt  * These are our native regset flavours.
313934135c1SPaul Mundt  */
314934135c1SPaul Mundt enum sh_regset {
315934135c1SPaul Mundt 	REGSET_GENERAL,
316e7ab3cd2SPaul Mundt #ifdef CONFIG_SH_FPU
317e7ab3cd2SPaul Mundt 	REGSET_FPU,
318e7ab3cd2SPaul Mundt #endif
3195dadb343SPaul Mundt #ifdef CONFIG_SH_DSP
3205dadb343SPaul Mundt 	REGSET_DSP,
3215dadb343SPaul Mundt #endif
322934135c1SPaul Mundt };
323934135c1SPaul Mundt 
324934135c1SPaul Mundt static const struct user_regset sh_regsets[] = {
325934135c1SPaul Mundt 	/*
326934135c1SPaul Mundt 	 * Format is:
327934135c1SPaul Mundt 	 *	R0 --> R15
328934135c1SPaul Mundt 	 *	PC, PR, SR, GBR, MACH, MACL, TRA
329934135c1SPaul Mundt 	 */
330934135c1SPaul Mundt 	[REGSET_GENERAL] = {
331934135c1SPaul Mundt 		.core_note_type	= NT_PRSTATUS,
332934135c1SPaul Mundt 		.n		= ELF_NGREG,
333934135c1SPaul Mundt 		.size		= sizeof(long),
334934135c1SPaul Mundt 		.align		= sizeof(long),
335934135c1SPaul Mundt 		.get		= genregs_get,
336934135c1SPaul Mundt 		.set		= genregs_set,
337934135c1SPaul Mundt 	},
3385dadb343SPaul Mundt 
339e7ab3cd2SPaul Mundt #ifdef CONFIG_SH_FPU
340e7ab3cd2SPaul Mundt 	[REGSET_FPU] = {
341e7ab3cd2SPaul Mundt 		.core_note_type	= NT_PRFPREG,
342e7ab3cd2SPaul Mundt 		.n		= sizeof(struct user_fpu_struct) / sizeof(long),
343e7ab3cd2SPaul Mundt 		.size		= sizeof(long),
344e7ab3cd2SPaul Mundt 		.align		= sizeof(long),
345e7ab3cd2SPaul Mundt 		.get		= fpregs_get,
346e7ab3cd2SPaul Mundt 		.set		= fpregs_set,
347e7ab3cd2SPaul Mundt 		.active		= fpregs_active,
348e7ab3cd2SPaul Mundt 	},
349e7ab3cd2SPaul Mundt #endif
350e7ab3cd2SPaul Mundt 
3515dadb343SPaul Mundt #ifdef CONFIG_SH_DSP
3525dadb343SPaul Mundt 	[REGSET_DSP] = {
3535dadb343SPaul Mundt 		.n		= sizeof(struct pt_dspregs) / sizeof(long),
3545dadb343SPaul Mundt 		.size		= sizeof(long),
3555dadb343SPaul Mundt 		.align		= sizeof(long),
3565dadb343SPaul Mundt 		.get		= dspregs_get,
3575dadb343SPaul Mundt 		.set		= dspregs_set,
35872461997SPaul Mundt 		.active		= dspregs_active,
3595dadb343SPaul Mundt 	},
3605dadb343SPaul Mundt #endif
361934135c1SPaul Mundt };
362934135c1SPaul Mundt 
363934135c1SPaul Mundt static const struct user_regset_view user_sh_native_view = {
364934135c1SPaul Mundt 	.name		= "sh",
365934135c1SPaul Mundt 	.e_machine	= EM_SH,
366934135c1SPaul Mundt 	.regsets	= sh_regsets,
367934135c1SPaul Mundt 	.n		= ARRAY_SIZE(sh_regsets),
368934135c1SPaul Mundt };
369934135c1SPaul Mundt 
370f9540eceSPaul Mundt const struct user_regset_view *task_user_regset_view(struct task_struct *task)
371f9540eceSPaul Mundt {
372f9540eceSPaul Mundt 	return &user_sh_native_view;
373f9540eceSPaul Mundt }
374f9540eceSPaul Mundt 
3759b05a69eSNamhyung Kim long arch_ptrace(struct task_struct *child, long request,
3769b05a69eSNamhyung Kim 		 unsigned long addr, unsigned long data)
37748b22cf9SPaul Mundt {
378fa43972fSPaul Mundt 	unsigned long __user *datap = (unsigned long __user *)data;
37948b22cf9SPaul Mundt 	int ret;
38048b22cf9SPaul Mundt 
38148b22cf9SPaul Mundt 	switch (request) {
38248b22cf9SPaul Mundt 	/* read the word at location addr in the USER area. */
38348b22cf9SPaul Mundt 	case PTRACE_PEEKUSR: {
38448b22cf9SPaul Mundt 		unsigned long tmp;
38548b22cf9SPaul Mundt 
38648b22cf9SPaul Mundt 		ret = -EIO;
38748b22cf9SPaul Mundt 		if ((addr & 3) || addr < 0 ||
38848b22cf9SPaul Mundt 		    addr > sizeof(struct user) - 3)
38948b22cf9SPaul Mundt 			break;
39048b22cf9SPaul Mundt 
39148b22cf9SPaul Mundt 		if (addr < sizeof(struct pt_regs))
39248b22cf9SPaul Mundt 			tmp = get_stack_long(child, addr);
3939e1cb206SNamhyung Kim 		else if (addr >= offsetof(struct user, fpu) &&
3949e1cb206SNamhyung Kim 			 addr < offsetof(struct user, u_fpvalid)) {
39548b22cf9SPaul Mundt 			if (!tsk_used_math(child)) {
3969e1cb206SNamhyung Kim 				if (addr == offsetof(struct user, fpu.fpscr))
39748b22cf9SPaul Mundt 					tmp = FPSCR_INIT;
39848b22cf9SPaul Mundt 				else
39948b22cf9SPaul Mundt 					tmp = 0;
4009e1cb206SNamhyung Kim 			} else {
4019e1cb206SNamhyung Kim 				unsigned long index;
402c49b6ecfSPhil Edworthy 				ret = init_fpu(child);
403c49b6ecfSPhil Edworthy 				if (ret)
404c49b6ecfSPhil Edworthy 					break;
4059e1cb206SNamhyung Kim 				index = addr - offsetof(struct user, fpu);
4069b05a69eSNamhyung Kim 				tmp = ((unsigned long *)child->thread.xstate)
4079e1cb206SNamhyung Kim 					[index >> 2];
4089e1cb206SNamhyung Kim 			}
4099e1cb206SNamhyung Kim 		} else if (addr == offsetof(struct user, u_fpvalid))
41048b22cf9SPaul Mundt 			tmp = !!tsk_used_math(child);
411ba0d4740SPeter Griffin 		else if (addr == PT_TEXT_ADDR)
412ba0d4740SPeter Griffin 			tmp = child->mm->start_code;
413ba0d4740SPeter Griffin 		else if (addr == PT_DATA_ADDR)
414ba0d4740SPeter Griffin 			tmp = child->mm->start_data;
415ba0d4740SPeter Griffin 		else if (addr == PT_TEXT_END_ADDR)
416ba0d4740SPeter Griffin 			tmp = child->mm->end_code;
417ba0d4740SPeter Griffin 		else if (addr == PT_TEXT_LEN)
418ba0d4740SPeter Griffin 			tmp = child->mm->end_code - child->mm->start_code;
41948b22cf9SPaul Mundt 		else
42048b22cf9SPaul Mundt 			tmp = 0;
421fa43972fSPaul Mundt 		ret = put_user(tmp, datap);
42248b22cf9SPaul Mundt 		break;
42348b22cf9SPaul Mundt 	}
42448b22cf9SPaul Mundt 
42548b22cf9SPaul Mundt 	case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
42648b22cf9SPaul Mundt 		ret = -EIO;
42748b22cf9SPaul Mundt 		if ((addr & 3) || addr < 0 ||
42848b22cf9SPaul Mundt 		    addr > sizeof(struct user) - 3)
42948b22cf9SPaul Mundt 			break;
43048b22cf9SPaul Mundt 
43148b22cf9SPaul Mundt 		if (addr < sizeof(struct pt_regs))
43248b22cf9SPaul Mundt 			ret = put_stack_long(child, addr, data);
4339e1cb206SNamhyung Kim 		else if (addr >= offsetof(struct user, fpu) &&
4349e1cb206SNamhyung Kim 			 addr < offsetof(struct user, u_fpvalid)) {
4359e1cb206SNamhyung Kim 			unsigned long index;
436c49b6ecfSPhil Edworthy 			ret = init_fpu(child);
437c49b6ecfSPhil Edworthy 			if (ret)
438c49b6ecfSPhil Edworthy 				break;
4399e1cb206SNamhyung Kim 			index = addr - offsetof(struct user, fpu);
44048b22cf9SPaul Mundt 			set_stopped_child_used_math(child);
4419b05a69eSNamhyung Kim 			((unsigned long *)child->thread.xstate)
4429e1cb206SNamhyung Kim 				[index >> 2] = data;
44348b22cf9SPaul Mundt 			ret = 0;
4449e1cb206SNamhyung Kim 		} else if (addr == offsetof(struct user, u_fpvalid)) {
44548b22cf9SPaul Mundt 			conditional_stopped_child_used_math(data, child);
44648b22cf9SPaul Mundt 			ret = 0;
44748b22cf9SPaul Mundt 		}
44848b22cf9SPaul Mundt 		break;
44948b22cf9SPaul Mundt 
450934135c1SPaul Mundt 	case PTRACE_GETREGS:
451934135c1SPaul Mundt 		return copy_regset_to_user(child, &user_sh_native_view,
452934135c1SPaul Mundt 					   REGSET_GENERAL,
453934135c1SPaul Mundt 					   0, sizeof(struct pt_regs),
4549e1cb206SNamhyung Kim 					   datap);
455934135c1SPaul Mundt 	case PTRACE_SETREGS:
456934135c1SPaul Mundt 		return copy_regset_from_user(child, &user_sh_native_view,
457934135c1SPaul Mundt 					     REGSET_GENERAL,
458934135c1SPaul Mundt 					     0, sizeof(struct pt_regs),
4599e1cb206SNamhyung Kim 					     datap);
460e7ab3cd2SPaul Mundt #ifdef CONFIG_SH_FPU
461e7ab3cd2SPaul Mundt 	case PTRACE_GETFPREGS:
462e7ab3cd2SPaul Mundt 		return copy_regset_to_user(child, &user_sh_native_view,
463e7ab3cd2SPaul Mundt 					   REGSET_FPU,
464e7ab3cd2SPaul Mundt 					   0, sizeof(struct user_fpu_struct),
4659e1cb206SNamhyung Kim 					   datap);
466e7ab3cd2SPaul Mundt 	case PTRACE_SETFPREGS:
467e7ab3cd2SPaul Mundt 		return copy_regset_from_user(child, &user_sh_native_view,
468e7ab3cd2SPaul Mundt 					     REGSET_FPU,
469e7ab3cd2SPaul Mundt 					     0, sizeof(struct user_fpu_struct),
4709e1cb206SNamhyung Kim 					     datap);
471e7ab3cd2SPaul Mundt #endif
47248b22cf9SPaul Mundt #ifdef CONFIG_SH_DSP
4735dadb343SPaul Mundt 	case PTRACE_GETDSPREGS:
4745dadb343SPaul Mundt 		return copy_regset_to_user(child, &user_sh_native_view,
4755dadb343SPaul Mundt 					   REGSET_DSP,
4765dadb343SPaul Mundt 					   0, sizeof(struct pt_dspregs),
4779e1cb206SNamhyung Kim 					   datap);
4785dadb343SPaul Mundt 	case PTRACE_SETDSPREGS:
4795dadb343SPaul Mundt 		return copy_regset_from_user(child, &user_sh_native_view,
4805dadb343SPaul Mundt 					     REGSET_DSP,
4815dadb343SPaul Mundt 					     0, sizeof(struct pt_dspregs),
4829e1cb206SNamhyung Kim 					     datap);
48348b22cf9SPaul Mundt #endif
48448b22cf9SPaul Mundt 	default:
48548b22cf9SPaul Mundt 		ret = ptrace_request(child, request, addr, data);
48648b22cf9SPaul Mundt 		break;
48748b22cf9SPaul Mundt 	}
48848b22cf9SPaul Mundt 
48948b22cf9SPaul Mundt 	return ret;
49048b22cf9SPaul Mundt }
49148b22cf9SPaul Mundt 
4929e5e2117SPaul Mundt static inline int audit_arch(void)
4939e5e2117SPaul Mundt {
4949e5e2117SPaul Mundt 	int arch = EM_SH;
4959e5e2117SPaul Mundt 
4969e5e2117SPaul Mundt #ifdef CONFIG_CPU_LITTLE_ENDIAN
4979e5e2117SPaul Mundt 	arch |= __AUDIT_ARCH_LE;
4989e5e2117SPaul Mundt #endif
4999e5e2117SPaul Mundt 
5009e5e2117SPaul Mundt 	return arch;
5019e5e2117SPaul Mundt }
5029e5e2117SPaul Mundt 
503ab99c733SPaul Mundt asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
50448b22cf9SPaul Mundt {
505ab99c733SPaul Mundt 	long ret = 0;
50648b22cf9SPaul Mundt 
507c4637d47SPaul Mundt 	secure_computing(regs->regs[0]);
508c4637d47SPaul Mundt 
509ab99c733SPaul Mundt 	if (test_thread_flag(TIF_SYSCALL_TRACE) &&
510ab99c733SPaul Mundt 	    tracehook_report_syscall_entry(regs))
51148b22cf9SPaul Mundt 		/*
512ab99c733SPaul Mundt 		 * Tracing decided this syscall should not happen.
513ab99c733SPaul Mundt 		 * We'll return a bogus call number to get an ENOSYS
514ab99c733SPaul Mundt 		 * error, but leave the original number in regs->regs[0].
51548b22cf9SPaul Mundt 		 */
516ab99c733SPaul Mundt 		ret = -1L;
5171322b9deSYuichi Nakamura 
518a74f7e04SPaul Mundt 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
519a74f7e04SPaul Mundt 		trace_sys_enter(regs, regs->regs[0]);
520c652d780SMatt Fleming 
521ab99c733SPaul Mundt 	if (unlikely(current->audit_context))
5229e5e2117SPaul Mundt 		audit_syscall_entry(audit_arch(), regs->regs[3],
5231322b9deSYuichi Nakamura 				    regs->regs[4], regs->regs[5],
5241322b9deSYuichi Nakamura 				    regs->regs[6], regs->regs[7]);
5251322b9deSYuichi Nakamura 
526ab99c733SPaul Mundt 	return ret ?: regs->regs[0];
527ab99c733SPaul Mundt }
528ab99c733SPaul Mundt 
529ab99c733SPaul Mundt asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
530ab99c733SPaul Mundt {
531ab99c733SPaul Mundt 	int step;
532ab99c733SPaul Mundt 
533ab99c733SPaul Mundt 	if (unlikely(current->audit_context))
534ab99c733SPaul Mundt 		audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
535ab99c733SPaul Mundt 				   regs->regs[0]);
536ab99c733SPaul Mundt 
537a74f7e04SPaul Mundt 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
538a74f7e04SPaul Mundt 		trace_sys_exit(regs, regs->regs[0]);
539c652d780SMatt Fleming 
540ab99c733SPaul Mundt 	step = test_thread_flag(TIF_SINGLESTEP);
541ab99c733SPaul Mundt 	if (step || test_thread_flag(TIF_SYSCALL_TRACE))
542ab99c733SPaul Mundt 		tracehook_report_syscall_exit(regs, step);
54348b22cf9SPaul Mundt }
544