xref: /linux/arch/sh/kernel/ptrace_32.c (revision 2277ab4a1df50e05bc732fe9488d4e902bb8399a)
1 /*
2  * SuperH process tracing
3  *
4  * Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
5  * Copyright (C) 2002 - 2008  Paul Mundt
6  *
7  * 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.
12  */
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>
36 
37 #include <trace/syscall.h>
38 
39 /*
40  * This routine will get a word off of the process kernel stack.
41  */
42 static inline int get_stack_long(struct task_struct *task, int offset)
43 {
44 	unsigned char *stack;
45 
46 	stack = (unsigned char *)task_pt_regs(task);
47 	stack += offset;
48 	return (*((int *)stack));
49 }
50 
51 /*
52  * This routine will put a word on the process kernel stack.
53  */
54 static inline int put_stack_long(struct task_struct *task, int offset,
55 				 unsigned long data)
56 {
57 	unsigned char *stack;
58 
59 	stack = (unsigned char *)task_pt_regs(task);
60 	stack += offset;
61 	*(unsigned long *) stack = data;
62 	return 0;
63 }
64 
65 void user_enable_single_step(struct task_struct *child)
66 {
67 	/* Next scheduling will set up UBC */
68 	if (child->thread.ubc_pc == 0)
69 		ubc_usercnt += 1;
70 
71 	child->thread.ubc_pc = get_stack_long(child,
72 				offsetof(struct pt_regs, pc));
73 
74 	set_tsk_thread_flag(child, TIF_SINGLESTEP);
75 }
76 
77 void user_disable_single_step(struct task_struct *child)
78 {
79 	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
80 
81 	/*
82 	 * Ensure the UBC is not programmed at the next context switch.
83 	 *
84 	 * Normally this is not needed but there are sequences such as
85 	 * singlestep, signal delivery, and continue that leave the
86 	 * ubc_pc non-zero leading to spurious SIGTRAPs.
87 	 */
88 	if (child->thread.ubc_pc != 0) {
89 		ubc_usercnt -= 1;
90 		child->thread.ubc_pc = 0;
91 	}
92 }
93 
94 /*
95  * Called by kernel/ptrace.c when detaching..
96  *
97  * Make sure single step bits etc are not set.
98  */
99 void ptrace_disable(struct task_struct *child)
100 {
101 	user_disable_single_step(child);
102 }
103 
104 static int genregs_get(struct task_struct *target,
105 		       const struct user_regset *regset,
106 		       unsigned int pos, unsigned int count,
107 		       void *kbuf, void __user *ubuf)
108 {
109 	const struct pt_regs *regs = task_pt_regs(target);
110 	int ret;
111 
112 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
113 				  regs->regs,
114 				  0, 16 * sizeof(unsigned long));
115 	if (!ret)
116 		/* PC, PR, SR, GBR, MACH, MACL, TRA */
117 		ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
118 					  &regs->pc,
119 					  offsetof(struct pt_regs, pc),
120 					  sizeof(struct pt_regs));
121 	if (!ret)
122 		ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
123 					       sizeof(struct pt_regs), -1);
124 
125 	return ret;
126 }
127 
128 static int genregs_set(struct task_struct *target,
129 		       const struct user_regset *regset,
130 		       unsigned int pos, unsigned int count,
131 		       const void *kbuf, const void __user *ubuf)
132 {
133 	struct pt_regs *regs = task_pt_regs(target);
134 	int ret;
135 
136 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
137 				 regs->regs,
138 				 0, 16 * sizeof(unsigned long));
139 	if (!ret && count > 0)
140 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
141 					 &regs->pc,
142 					 offsetof(struct pt_regs, pc),
143 					 sizeof(struct pt_regs));
144 	if (!ret)
145 		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
146 						sizeof(struct pt_regs), -1);
147 
148 	return ret;
149 }
150 
151 #ifdef CONFIG_SH_FPU
152 int fpregs_get(struct task_struct *target,
153 	       const struct user_regset *regset,
154 	       unsigned int pos, unsigned int count,
155 	       void *kbuf, void __user *ubuf)
156 {
157 	int ret;
158 
159 	ret = init_fpu(target);
160 	if (ret)
161 		return ret;
162 
163 	if ((boot_cpu_data.flags & CPU_HAS_FPU))
164 		return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
165 					   &target->thread.fpu.hard, 0, -1);
166 
167 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
168 				   &target->thread.fpu.soft, 0, -1);
169 }
170 
171 static int fpregs_set(struct task_struct *target,
172 		       const struct user_regset *regset,
173 		       unsigned int pos, unsigned int count,
174 		       const void *kbuf, const void __user *ubuf)
175 {
176 	int ret;
177 
178 	ret = init_fpu(target);
179 	if (ret)
180 		return ret;
181 
182 	set_stopped_child_used_math(target);
183 
184 	if ((boot_cpu_data.flags & CPU_HAS_FPU))
185 		return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
186 					  &target->thread.fpu.hard, 0, -1);
187 
188 	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
189 				  &target->thread.fpu.soft, 0, -1);
190 }
191 
192 static int fpregs_active(struct task_struct *target,
193 			 const struct user_regset *regset)
194 {
195 	return tsk_used_math(target) ? regset->n : 0;
196 }
197 #endif
198 
199 #ifdef CONFIG_SH_DSP
200 static int dspregs_get(struct task_struct *target,
201 		       const struct user_regset *regset,
202 		       unsigned int pos, unsigned int count,
203 		       void *kbuf, void __user *ubuf)
204 {
205 	const struct pt_dspregs *regs =
206 		(struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
207 	int ret;
208 
209 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
210 				  0, sizeof(struct pt_dspregs));
211 	if (!ret)
212 		ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
213 					       sizeof(struct pt_dspregs), -1);
214 
215 	return ret;
216 }
217 
218 static int dspregs_set(struct task_struct *target,
219 		       const struct user_regset *regset,
220 		       unsigned int pos, unsigned int count,
221 		       const void *kbuf, const void __user *ubuf)
222 {
223 	struct pt_dspregs *regs =
224 		(struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
225 	int ret;
226 
227 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
228 				 0, sizeof(struct pt_dspregs));
229 	if (!ret)
230 		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
231 						sizeof(struct pt_dspregs), -1);
232 
233 	return ret;
234 }
235 
236 static int dspregs_active(struct task_struct *target,
237 			  const struct user_regset *regset)
238 {
239 	struct pt_regs *regs = task_pt_regs(target);
240 
241 	return regs->sr & SR_DSP ? regset->n : 0;
242 }
243 #endif
244 
245 /*
246  * These are our native regset flavours.
247  */
248 enum sh_regset {
249 	REGSET_GENERAL,
250 #ifdef CONFIG_SH_FPU
251 	REGSET_FPU,
252 #endif
253 #ifdef CONFIG_SH_DSP
254 	REGSET_DSP,
255 #endif
256 };
257 
258 static const struct user_regset sh_regsets[] = {
259 	/*
260 	 * Format is:
261 	 *	R0 --> R15
262 	 *	PC, PR, SR, GBR, MACH, MACL, TRA
263 	 */
264 	[REGSET_GENERAL] = {
265 		.core_note_type	= NT_PRSTATUS,
266 		.n		= ELF_NGREG,
267 		.size		= sizeof(long),
268 		.align		= sizeof(long),
269 		.get		= genregs_get,
270 		.set		= genregs_set,
271 	},
272 
273 #ifdef CONFIG_SH_FPU
274 	[REGSET_FPU] = {
275 		.core_note_type	= NT_PRFPREG,
276 		.n		= sizeof(struct user_fpu_struct) / sizeof(long),
277 		.size		= sizeof(long),
278 		.align		= sizeof(long),
279 		.get		= fpregs_get,
280 		.set		= fpregs_set,
281 		.active		= fpregs_active,
282 	},
283 #endif
284 
285 #ifdef CONFIG_SH_DSP
286 	[REGSET_DSP] = {
287 		.n		= sizeof(struct pt_dspregs) / sizeof(long),
288 		.size		= sizeof(long),
289 		.align		= sizeof(long),
290 		.get		= dspregs_get,
291 		.set		= dspregs_set,
292 		.active		= dspregs_active,
293 	},
294 #endif
295 };
296 
297 static const struct user_regset_view user_sh_native_view = {
298 	.name		= "sh",
299 	.e_machine	= EM_SH,
300 	.regsets	= sh_regsets,
301 	.n		= ARRAY_SIZE(sh_regsets),
302 };
303 
304 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
305 {
306 	return &user_sh_native_view;
307 }
308 
309 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
310 {
311 	struct user * dummy = NULL;
312 	unsigned long __user *datap = (unsigned long __user *)data;
313 	int ret;
314 
315 	switch (request) {
316 	/* read the word at location addr in the USER area. */
317 	case PTRACE_PEEKUSR: {
318 		unsigned long tmp;
319 
320 		ret = -EIO;
321 		if ((addr & 3) || addr < 0 ||
322 		    addr > sizeof(struct user) - 3)
323 			break;
324 
325 		if (addr < sizeof(struct pt_regs))
326 			tmp = get_stack_long(child, addr);
327 		else if (addr >= (long) &dummy->fpu &&
328 			 addr < (long) &dummy->u_fpvalid) {
329 			if (!tsk_used_math(child)) {
330 				if (addr == (long)&dummy->fpu.fpscr)
331 					tmp = FPSCR_INIT;
332 				else
333 					tmp = 0;
334 			} else
335 				tmp = ((long *)&child->thread.fpu)
336 					[(addr - (long)&dummy->fpu) >> 2];
337 		} else if (addr == (long) &dummy->u_fpvalid)
338 			tmp = !!tsk_used_math(child);
339 		else if (addr == PT_TEXT_ADDR)
340 			tmp = child->mm->start_code;
341 		else if (addr == PT_DATA_ADDR)
342 			tmp = child->mm->start_data;
343 		else if (addr == PT_TEXT_END_ADDR)
344 			tmp = child->mm->end_code;
345 		else if (addr == PT_TEXT_LEN)
346 			tmp = child->mm->end_code - child->mm->start_code;
347 		else
348 			tmp = 0;
349 		ret = put_user(tmp, datap);
350 		break;
351 	}
352 
353 	case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
354 		ret = -EIO;
355 		if ((addr & 3) || addr < 0 ||
356 		    addr > sizeof(struct user) - 3)
357 			break;
358 
359 		if (addr < sizeof(struct pt_regs))
360 			ret = put_stack_long(child, addr, data);
361 		else if (addr >= (long) &dummy->fpu &&
362 			 addr < (long) &dummy->u_fpvalid) {
363 			set_stopped_child_used_math(child);
364 			((long *)&child->thread.fpu)
365 				[(addr - (long)&dummy->fpu) >> 2] = data;
366 			ret = 0;
367 		} else if (addr == (long) &dummy->u_fpvalid) {
368 			conditional_stopped_child_used_math(data, child);
369 			ret = 0;
370 		}
371 		break;
372 
373 	case PTRACE_GETREGS:
374 		return copy_regset_to_user(child, &user_sh_native_view,
375 					   REGSET_GENERAL,
376 					   0, sizeof(struct pt_regs),
377 					   (void __user *)data);
378 	case PTRACE_SETREGS:
379 		return copy_regset_from_user(child, &user_sh_native_view,
380 					     REGSET_GENERAL,
381 					     0, sizeof(struct pt_regs),
382 					     (const void __user *)data);
383 #ifdef CONFIG_SH_FPU
384 	case PTRACE_GETFPREGS:
385 		return copy_regset_to_user(child, &user_sh_native_view,
386 					   REGSET_FPU,
387 					   0, sizeof(struct user_fpu_struct),
388 					   (void __user *)data);
389 	case PTRACE_SETFPREGS:
390 		return copy_regset_from_user(child, &user_sh_native_view,
391 					     REGSET_FPU,
392 					     0, sizeof(struct user_fpu_struct),
393 					     (const void __user *)data);
394 #endif
395 #ifdef CONFIG_SH_DSP
396 	case PTRACE_GETDSPREGS:
397 		return copy_regset_to_user(child, &user_sh_native_view,
398 					   REGSET_DSP,
399 					   0, sizeof(struct pt_dspregs),
400 					   (void __user *)data);
401 	case PTRACE_SETDSPREGS:
402 		return copy_regset_from_user(child, &user_sh_native_view,
403 					     REGSET_DSP,
404 					     0, sizeof(struct pt_dspregs),
405 					     (const void __user *)data);
406 #endif
407 #ifdef CONFIG_BINFMT_ELF_FDPIC
408 	case PTRACE_GETFDPIC: {
409 		unsigned long tmp = 0;
410 
411 		switch (addr) {
412 		case PTRACE_GETFDPIC_EXEC:
413 			tmp = child->mm->context.exec_fdpic_loadmap;
414 			break;
415 		case PTRACE_GETFDPIC_INTERP:
416 			tmp = child->mm->context.interp_fdpic_loadmap;
417 			break;
418 		default:
419 			break;
420 		}
421 
422 		ret = 0;
423 		if (put_user(tmp, datap)) {
424 			ret = -EFAULT;
425 			break;
426 		}
427 		break;
428 	}
429 #endif
430 	default:
431 		ret = ptrace_request(child, request, addr, data);
432 		break;
433 	}
434 
435 	return ret;
436 }
437 
438 static inline int audit_arch(void)
439 {
440 	int arch = EM_SH;
441 
442 #ifdef CONFIG_CPU_LITTLE_ENDIAN
443 	arch |= __AUDIT_ARCH_LE;
444 #endif
445 
446 	return arch;
447 }
448 
449 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
450 {
451 	long ret = 0;
452 
453 	secure_computing(regs->regs[0]);
454 
455 	if (test_thread_flag(TIF_SYSCALL_TRACE) &&
456 	    tracehook_report_syscall_entry(regs))
457 		/*
458 		 * Tracing decided this syscall should not happen.
459 		 * We'll return a bogus call number to get an ENOSYS
460 		 * error, but leave the original number in regs->regs[0].
461 		 */
462 		ret = -1L;
463 
464 	if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
465 		ftrace_syscall_enter(regs);
466 
467 	if (unlikely(current->audit_context))
468 		audit_syscall_entry(audit_arch(), regs->regs[3],
469 				    regs->regs[4], regs->regs[5],
470 				    regs->regs[6], regs->regs[7]);
471 
472 	return ret ?: regs->regs[0];
473 }
474 
475 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
476 {
477 	int step;
478 
479 	if (unlikely(current->audit_context))
480 		audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
481 				   regs->regs[0]);
482 
483 	if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
484 		ftrace_syscall_exit(regs);
485 
486 	step = test_thread_flag(TIF_SINGLESTEP);
487 	if (step || test_thread_flag(TIF_SYSCALL_TRACE))
488 		tracehook_report_syscall_exit(regs, step);
489 }
490