1 /*
2  *  PowerPC version
3  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4  *
5  *  Derived from "arch/m68k/kernel/ptrace.c"
6  *  Copyright (C) 1994 by Hamish Macdonald
7  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
8  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
9  *
10  * Modified by Cort Dougan (cort@hq.fsmlabs.com)
11  * and Paul Mackerras (paulus@samba.org).
12  *
13  * This file is subject to the terms and conditions of the GNU General
14  * Public License.  See the file README.legal in the main directory of
15  * this archive for more details.
16  */
17 
18 #include <linux/regset.h>
19 #include <linux/tracehook.h>
20 #include <linux/audit.h>
21 #include <linux/context_tracking.h>
22 #include <linux/syscalls.h>
23 
24 #include <asm/switch_to.h>
25 #include <asm/asm-prototypes.h>
26 #include <asm/debug.h>
27 
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/syscalls.h>
30 
31 #include "ptrace-decl.h"
32 
33 /*
34  * Called by kernel/ptrace.c when detaching..
35  *
36  * Make sure single step bits etc are not set.
37  */
38 void ptrace_disable(struct task_struct *child)
39 {
40 	/* make sure the single step bit is not set. */
41 	user_disable_single_step(child);
42 }
43 
44 long arch_ptrace(struct task_struct *child, long request,
45 		 unsigned long addr, unsigned long data)
46 {
47 	int ret = -EPERM;
48 	void __user *datavp = (void __user *) data;
49 	unsigned long __user *datalp = datavp;
50 
51 	switch (request) {
52 	/* read the word at location addr in the USER area. */
53 	case PTRACE_PEEKUSR: {
54 		unsigned long index, tmp;
55 
56 		ret = -EIO;
57 		/* convert to index and check */
58 		index = addr / sizeof(long);
59 		if ((addr & (sizeof(long) - 1)) || (index > PT_FPSCR)
60 		    || (child->thread.regs == NULL))
61 			break;
62 
63 		CHECK_FULL_REGS(child->thread.regs);
64 		if (index < PT_FPR0) {
65 			ret = ptrace_get_reg(child, (int) index, &tmp);
66 			if (ret)
67 				break;
68 		} else {
69 			unsigned int fpidx = index - PT_FPR0;
70 
71 			flush_fp_to_thread(child);
72 			if (fpidx < (PT_FPSCR - PT_FPR0))
73 				memcpy(&tmp, &child->thread.TS_FPR(fpidx),
74 				       sizeof(long));
75 			else
76 				tmp = child->thread.fp_state.fpscr;
77 		}
78 		ret = put_user(tmp, datalp);
79 		break;
80 	}
81 
82 	/* write the word at location addr in the USER area */
83 	case PTRACE_POKEUSR: {
84 		unsigned long index;
85 
86 		ret = -EIO;
87 		/* convert to index and check */
88 		index = addr / sizeof(long);
89 		if ((addr & (sizeof(long) - 1)) || (index > PT_FPSCR)
90 		    || (child->thread.regs == NULL))
91 			break;
92 
93 		CHECK_FULL_REGS(child->thread.regs);
94 		if (index < PT_FPR0) {
95 			ret = ptrace_put_reg(child, index, data);
96 		} else {
97 			unsigned int fpidx = index - PT_FPR0;
98 
99 			flush_fp_to_thread(child);
100 			if (fpidx < (PT_FPSCR - PT_FPR0))
101 				memcpy(&child->thread.TS_FPR(fpidx), &data,
102 				       sizeof(long));
103 			else
104 				child->thread.fp_state.fpscr = data;
105 			ret = 0;
106 		}
107 		break;
108 	}
109 
110 	case PPC_PTRACE_GETHWDBGINFO: {
111 		struct ppc_debug_info dbginfo;
112 
113 		ppc_gethwdinfo(&dbginfo);
114 
115 		if (copy_to_user(datavp, &dbginfo,
116 				 sizeof(struct ppc_debug_info)))
117 			return -EFAULT;
118 		return 0;
119 	}
120 
121 	case PPC_PTRACE_SETHWDEBUG: {
122 		struct ppc_hw_breakpoint bp_info;
123 
124 		if (copy_from_user(&bp_info, datavp,
125 				   sizeof(struct ppc_hw_breakpoint)))
126 			return -EFAULT;
127 		return ppc_set_hwdebug(child, &bp_info);
128 	}
129 
130 	case PPC_PTRACE_DELHWDEBUG: {
131 		ret = ppc_del_hwdebug(child, data);
132 		break;
133 	}
134 
135 	case PTRACE_GET_DEBUGREG:
136 		ret = ptrace_get_debugreg(child, addr, datalp);
137 		break;
138 
139 	case PTRACE_SET_DEBUGREG:
140 		ret = ptrace_set_debugreg(child, addr, data);
141 		break;
142 
143 #ifdef CONFIG_PPC64
144 	case PTRACE_GETREGS64:
145 #endif
146 	case PTRACE_GETREGS:	/* Get all pt_regs from the child. */
147 		return copy_regset_to_user(child, &user_ppc_native_view,
148 					   REGSET_GPR,
149 					   0, sizeof(struct user_pt_regs),
150 					   datavp);
151 
152 #ifdef CONFIG_PPC64
153 	case PTRACE_SETREGS64:
154 #endif
155 	case PTRACE_SETREGS:	/* Set all gp regs in the child. */
156 		return copy_regset_from_user(child, &user_ppc_native_view,
157 					     REGSET_GPR,
158 					     0, sizeof(struct user_pt_regs),
159 					     datavp);
160 
161 	case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
162 		return copy_regset_to_user(child, &user_ppc_native_view,
163 					   REGSET_FPR,
164 					   0, sizeof(elf_fpregset_t),
165 					   datavp);
166 
167 	case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
168 		return copy_regset_from_user(child, &user_ppc_native_view,
169 					     REGSET_FPR,
170 					     0, sizeof(elf_fpregset_t),
171 					     datavp);
172 
173 #ifdef CONFIG_ALTIVEC
174 	case PTRACE_GETVRREGS:
175 		return copy_regset_to_user(child, &user_ppc_native_view,
176 					   REGSET_VMX,
177 					   0, (33 * sizeof(vector128) +
178 					       sizeof(u32)),
179 					   datavp);
180 
181 	case PTRACE_SETVRREGS:
182 		return copy_regset_from_user(child, &user_ppc_native_view,
183 					     REGSET_VMX,
184 					     0, (33 * sizeof(vector128) +
185 						 sizeof(u32)),
186 					     datavp);
187 #endif
188 #ifdef CONFIG_VSX
189 	case PTRACE_GETVSRREGS:
190 		return copy_regset_to_user(child, &user_ppc_native_view,
191 					   REGSET_VSX,
192 					   0, 32 * sizeof(double),
193 					   datavp);
194 
195 	case PTRACE_SETVSRREGS:
196 		return copy_regset_from_user(child, &user_ppc_native_view,
197 					     REGSET_VSX,
198 					     0, 32 * sizeof(double),
199 					     datavp);
200 #endif
201 #ifdef CONFIG_SPE
202 	case PTRACE_GETEVRREGS:
203 		/* Get the child spe register state. */
204 		return copy_regset_to_user(child, &user_ppc_native_view,
205 					   REGSET_SPE, 0, 35 * sizeof(u32),
206 					   datavp);
207 
208 	case PTRACE_SETEVRREGS:
209 		/* Set the child spe register state. */
210 		return copy_regset_from_user(child, &user_ppc_native_view,
211 					     REGSET_SPE, 0, 35 * sizeof(u32),
212 					     datavp);
213 #endif
214 
215 	default:
216 		ret = ptrace_request(child, request, addr, data);
217 		break;
218 	}
219 	return ret;
220 }
221 
222 #ifdef CONFIG_SECCOMP
223 static int do_seccomp(struct pt_regs *regs)
224 {
225 	if (!test_thread_flag(TIF_SECCOMP))
226 		return 0;
227 
228 	/*
229 	 * The ABI we present to seccomp tracers is that r3 contains
230 	 * the syscall return value and orig_gpr3 contains the first
231 	 * syscall parameter. This is different to the ptrace ABI where
232 	 * both r3 and orig_gpr3 contain the first syscall parameter.
233 	 */
234 	regs->gpr[3] = -ENOSYS;
235 
236 	/*
237 	 * We use the __ version here because we have already checked
238 	 * TIF_SECCOMP. If this fails, there is nothing left to do, we
239 	 * have already loaded -ENOSYS into r3, or seccomp has put
240 	 * something else in r3 (via SECCOMP_RET_ERRNO/TRACE).
241 	 */
242 	if (__secure_computing(NULL))
243 		return -1;
244 
245 	/*
246 	 * The syscall was allowed by seccomp, restore the register
247 	 * state to what audit expects.
248 	 * Note that we use orig_gpr3, which means a seccomp tracer can
249 	 * modify the first syscall parameter (in orig_gpr3) and also
250 	 * allow the syscall to proceed.
251 	 */
252 	regs->gpr[3] = regs->orig_gpr3;
253 
254 	return 0;
255 }
256 #else
257 static inline int do_seccomp(struct pt_regs *regs) { return 0; }
258 #endif /* CONFIG_SECCOMP */
259 
260 /**
261  * do_syscall_trace_enter() - Do syscall tracing on kernel entry.
262  * @regs: the pt_regs of the task to trace (current)
263  *
264  * Performs various types of tracing on syscall entry. This includes seccomp,
265  * ptrace, syscall tracepoints and audit.
266  *
267  * The pt_regs are potentially visible to userspace via ptrace, so their
268  * contents is ABI.
269  *
270  * One or more of the tracers may modify the contents of pt_regs, in particular
271  * to modify arguments or even the syscall number itself.
272  *
273  * It's also possible that a tracer can choose to reject the system call. In
274  * that case this function will return an illegal syscall number, and will put
275  * an appropriate return value in regs->r3.
276  *
277  * Return: the (possibly changed) syscall number.
278  */
279 long do_syscall_trace_enter(struct pt_regs *regs)
280 {
281 	u32 flags;
282 
283 	user_exit();
284 
285 	flags = READ_ONCE(current_thread_info()->flags) &
286 		(_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE);
287 
288 	if (flags) {
289 		int rc = tracehook_report_syscall_entry(regs);
290 
291 		if (unlikely(flags & _TIF_SYSCALL_EMU)) {
292 			/*
293 			 * A nonzero return code from
294 			 * tracehook_report_syscall_entry() tells us to prevent
295 			 * the syscall execution, but we are not going to
296 			 * execute it anyway.
297 			 *
298 			 * Returning -1 will skip the syscall execution. We want
299 			 * to avoid clobbering any registers, so we don't goto
300 			 * the skip label below.
301 			 */
302 			return -1;
303 		}
304 
305 		if (rc) {
306 			/*
307 			 * The tracer decided to abort the syscall. Note that
308 			 * the tracer may also just change regs->gpr[0] to an
309 			 * invalid syscall number, that is handled below on the
310 			 * exit path.
311 			 */
312 			goto skip;
313 		}
314 	}
315 
316 	/* Run seccomp after ptrace; allow it to set gpr[3]. */
317 	if (do_seccomp(regs))
318 		return -1;
319 
320 	/* Avoid trace and audit when syscall is invalid. */
321 	if (regs->gpr[0] >= NR_syscalls)
322 		goto skip;
323 
324 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
325 		trace_sys_enter(regs, regs->gpr[0]);
326 
327 	if (!is_32bit_task())
328 		audit_syscall_entry(regs->gpr[0], regs->gpr[3], regs->gpr[4],
329 				    regs->gpr[5], regs->gpr[6]);
330 	else
331 		audit_syscall_entry(regs->gpr[0],
332 				    regs->gpr[3] & 0xffffffff,
333 				    regs->gpr[4] & 0xffffffff,
334 				    regs->gpr[5] & 0xffffffff,
335 				    regs->gpr[6] & 0xffffffff);
336 
337 	/* Return the possibly modified but valid syscall number */
338 	return regs->gpr[0];
339 
340 skip:
341 	/*
342 	 * If we are aborting explicitly, or if the syscall number is
343 	 * now invalid, set the return value to -ENOSYS.
344 	 */
345 	regs->gpr[3] = -ENOSYS;
346 	return -1;
347 }
348 
349 void do_syscall_trace_leave(struct pt_regs *regs)
350 {
351 	int step;
352 
353 	audit_syscall_exit(regs);
354 
355 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
356 		trace_sys_exit(regs, regs->result);
357 
358 	step = test_thread_flag(TIF_SINGLESTEP);
359 	if (step || test_thread_flag(TIF_SYSCALL_TRACE))
360 		tracehook_report_syscall_exit(regs, step);
361 
362 	user_enter();
363 }
364 
365 void __init pt_regs_check(void);
366 
367 /*
368  * Dummy function, its purpose is to break the build if struct pt_regs and
369  * struct user_pt_regs don't match.
370  */
371 void __init pt_regs_check(void)
372 {
373 	BUILD_BUG_ON(offsetof(struct pt_regs, gpr) !=
374 		     offsetof(struct user_pt_regs, gpr));
375 	BUILD_BUG_ON(offsetof(struct pt_regs, nip) !=
376 		     offsetof(struct user_pt_regs, nip));
377 	BUILD_BUG_ON(offsetof(struct pt_regs, msr) !=
378 		     offsetof(struct user_pt_regs, msr));
379 	BUILD_BUG_ON(offsetof(struct pt_regs, msr) !=
380 		     offsetof(struct user_pt_regs, msr));
381 	BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
382 		     offsetof(struct user_pt_regs, orig_gpr3));
383 	BUILD_BUG_ON(offsetof(struct pt_regs, ctr) !=
384 		     offsetof(struct user_pt_regs, ctr));
385 	BUILD_BUG_ON(offsetof(struct pt_regs, link) !=
386 		     offsetof(struct user_pt_regs, link));
387 	BUILD_BUG_ON(offsetof(struct pt_regs, xer) !=
388 		     offsetof(struct user_pt_regs, xer));
389 	BUILD_BUG_ON(offsetof(struct pt_regs, ccr) !=
390 		     offsetof(struct user_pt_regs, ccr));
391 #ifdef __powerpc64__
392 	BUILD_BUG_ON(offsetof(struct pt_regs, softe) !=
393 		     offsetof(struct user_pt_regs, softe));
394 #else
395 	BUILD_BUG_ON(offsetof(struct pt_regs, mq) !=
396 		     offsetof(struct user_pt_regs, mq));
397 #endif
398 	BUILD_BUG_ON(offsetof(struct pt_regs, trap) !=
399 		     offsetof(struct user_pt_regs, trap));
400 	BUILD_BUG_ON(offsetof(struct pt_regs, dar) !=
401 		     offsetof(struct user_pt_regs, dar));
402 	BUILD_BUG_ON(offsetof(struct pt_regs, dsisr) !=
403 		     offsetof(struct user_pt_regs, dsisr));
404 	BUILD_BUG_ON(offsetof(struct pt_regs, result) !=
405 		     offsetof(struct user_pt_regs, result));
406 
407 	BUILD_BUG_ON(sizeof(struct user_pt_regs) > sizeof(struct pt_regs));
408 
409 	// Now check that the pt_regs offsets match the uapi #defines
410 	#define CHECK_REG(_pt, _reg) \
411 		BUILD_BUG_ON(_pt != (offsetof(struct user_pt_regs, _reg) / \
412 				     sizeof(unsigned long)));
413 
414 	CHECK_REG(PT_R0,  gpr[0]);
415 	CHECK_REG(PT_R1,  gpr[1]);
416 	CHECK_REG(PT_R2,  gpr[2]);
417 	CHECK_REG(PT_R3,  gpr[3]);
418 	CHECK_REG(PT_R4,  gpr[4]);
419 	CHECK_REG(PT_R5,  gpr[5]);
420 	CHECK_REG(PT_R6,  gpr[6]);
421 	CHECK_REG(PT_R7,  gpr[7]);
422 	CHECK_REG(PT_R8,  gpr[8]);
423 	CHECK_REG(PT_R9,  gpr[9]);
424 	CHECK_REG(PT_R10, gpr[10]);
425 	CHECK_REG(PT_R11, gpr[11]);
426 	CHECK_REG(PT_R12, gpr[12]);
427 	CHECK_REG(PT_R13, gpr[13]);
428 	CHECK_REG(PT_R14, gpr[14]);
429 	CHECK_REG(PT_R15, gpr[15]);
430 	CHECK_REG(PT_R16, gpr[16]);
431 	CHECK_REG(PT_R17, gpr[17]);
432 	CHECK_REG(PT_R18, gpr[18]);
433 	CHECK_REG(PT_R19, gpr[19]);
434 	CHECK_REG(PT_R20, gpr[20]);
435 	CHECK_REG(PT_R21, gpr[21]);
436 	CHECK_REG(PT_R22, gpr[22]);
437 	CHECK_REG(PT_R23, gpr[23]);
438 	CHECK_REG(PT_R24, gpr[24]);
439 	CHECK_REG(PT_R25, gpr[25]);
440 	CHECK_REG(PT_R26, gpr[26]);
441 	CHECK_REG(PT_R27, gpr[27]);
442 	CHECK_REG(PT_R28, gpr[28]);
443 	CHECK_REG(PT_R29, gpr[29]);
444 	CHECK_REG(PT_R30, gpr[30]);
445 	CHECK_REG(PT_R31, gpr[31]);
446 	CHECK_REG(PT_NIP, nip);
447 	CHECK_REG(PT_MSR, msr);
448 	CHECK_REG(PT_ORIG_R3, orig_gpr3);
449 	CHECK_REG(PT_CTR, ctr);
450 	CHECK_REG(PT_LNK, link);
451 	CHECK_REG(PT_XER, xer);
452 	CHECK_REG(PT_CCR, ccr);
453 #ifdef CONFIG_PPC64
454 	CHECK_REG(PT_SOFTE, softe);
455 #else
456 	CHECK_REG(PT_MQ, mq);
457 #endif
458 	CHECK_REG(PT_TRAP, trap);
459 	CHECK_REG(PT_DAR, dar);
460 	CHECK_REG(PT_DSISR, dsisr);
461 	CHECK_REG(PT_RESULT, result);
462 	#undef CHECK_REG
463 
464 	BUILD_BUG_ON(PT_REGS_COUNT != sizeof(struct user_pt_regs) / sizeof(unsigned long));
465 
466 	/*
467 	 * PT_DSCR isn't a real reg, but it's important that it doesn't overlap the
468 	 * real registers.
469 	 */
470 	BUILD_BUG_ON(PT_DSCR < sizeof(struct user_pt_regs) / sizeof(unsigned long));
471 }
472