• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Kernel support for the ptrace() and syscall tracing interfaces.
4  *
5  * Copyright (C) 2000 Hewlett-Packard Co, Linuxcare Inc.
6  * Copyright (C) 2000 Matthew Wilcox <matthew@wil.cx>
7  * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org>
8  * Copyright (C) 2008-2016 Helge Deller <deller@gmx.de>
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/elf.h>
16 #include <linux/errno.h>
17 #include <linux/ptrace.h>
18 #include <linux/tracehook.h>
19 #include <linux/user.h>
20 #include <linux/personality.h>
21 #include <linux/regset.h>
22 #include <linux/security.h>
23 #include <linux/seccomp.h>
24 #include <linux/compat.h>
25 #include <linux/signal.h>
26 #include <linux/audit.h>
27 
28 #include <linux/uaccess.h>
29 #include <asm/processor.h>
30 #include <asm/asm-offsets.h>
31 
32 /* PSW bits we allow the debugger to modify */
33 #define USER_PSW_BITS	(PSW_N | PSW_B | PSW_V | PSW_CB)
34 
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/syscalls.h>
37 
38 /*
39  * These are our native regset flavors.
40  */
41 enum parisc_regset {
42 	REGSET_GENERAL,
43 	REGSET_FP
44 };
45 
46 /*
47  * Called by kernel/ptrace.c when detaching..
48  *
49  * Make sure single step bits etc are not set.
50  */
ptrace_disable(struct task_struct * task)51 void ptrace_disable(struct task_struct *task)
52 {
53 	clear_tsk_thread_flag(task, TIF_SINGLESTEP);
54 	clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
55 
56 	/* make sure the trap bits are not set */
57 	pa_psw(task)->r = 0;
58 	pa_psw(task)->t = 0;
59 	pa_psw(task)->h = 0;
60 	pa_psw(task)->l = 0;
61 }
62 
63 /*
64  * The following functions are called by ptrace_resume() when
65  * enabling or disabling single/block tracing.
66  */
user_disable_single_step(struct task_struct * task)67 void user_disable_single_step(struct task_struct *task)
68 {
69 	ptrace_disable(task);
70 }
71 
user_enable_single_step(struct task_struct * task)72 void user_enable_single_step(struct task_struct *task)
73 {
74 	clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
75 	set_tsk_thread_flag(task, TIF_SINGLESTEP);
76 
77 	if (pa_psw(task)->n) {
78 		/* Nullified, just crank over the queue. */
79 		task_regs(task)->iaoq[0] = task_regs(task)->iaoq[1];
80 		task_regs(task)->iasq[0] = task_regs(task)->iasq[1];
81 		task_regs(task)->iaoq[1] = task_regs(task)->iaoq[0] + 4;
82 		pa_psw(task)->n = 0;
83 		pa_psw(task)->x = 0;
84 		pa_psw(task)->y = 0;
85 		pa_psw(task)->z = 0;
86 		pa_psw(task)->b = 0;
87 		ptrace_disable(task);
88 		/* Don't wake up the task, but let the
89 		   parent know something happened. */
90 		force_sig_fault_to_task(SIGTRAP, TRAP_TRACE,
91 					(void __user *) (task_regs(task)->iaoq[0] & ~3),
92 					task);
93 		/* notify_parent(task, SIGCHLD); */
94 		return;
95 	}
96 
97 	/* Enable recovery counter traps.  The recovery counter
98 	 * itself will be set to zero on a task switch.  If the
99 	 * task is suspended on a syscall then the syscall return
100 	 * path will overwrite the recovery counter with a suitable
101 	 * value such that it traps once back in user space.  We
102 	 * disable interrupts in the tasks PSW here also, to avoid
103 	 * interrupts while the recovery counter is decrementing.
104 	 */
105 	pa_psw(task)->r = 1;
106 	pa_psw(task)->t = 0;
107 	pa_psw(task)->h = 0;
108 	pa_psw(task)->l = 0;
109 }
110 
user_enable_block_step(struct task_struct * task)111 void user_enable_block_step(struct task_struct *task)
112 {
113 	clear_tsk_thread_flag(task, TIF_SINGLESTEP);
114 	set_tsk_thread_flag(task, TIF_BLOCKSTEP);
115 
116 	/* Enable taken branch trap. */
117 	pa_psw(task)->r = 0;
118 	pa_psw(task)->t = 1;
119 	pa_psw(task)->h = 0;
120 	pa_psw(task)->l = 0;
121 }
122 
arch_ptrace(struct task_struct * child,long request,unsigned long addr,unsigned long data)123 long arch_ptrace(struct task_struct *child, long request,
124 		 unsigned long addr, unsigned long data)
125 {
126 	unsigned long __user *datap = (unsigned long __user *)data;
127 	unsigned long tmp;
128 	long ret = -EIO;
129 
130 	unsigned long user_regs_struct_size = sizeof(struct user_regs_struct);
131 #ifdef CONFIG_64BIT
132 	if (is_compat_task())
133 		user_regs_struct_size /= 2;
134 #endif
135 
136 	switch (request) {
137 
138 	/* Read the word at location addr in the USER area.  For ptraced
139 	   processes, the kernel saves all regs on a syscall. */
140 	case PTRACE_PEEKUSR:
141 		if ((addr & (sizeof(unsigned long)-1)) ||
142 		     addr >= sizeof(struct pt_regs))
143 			break;
144 		tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
145 		ret = put_user(tmp, datap);
146 		break;
147 
148 	/* Write the word at location addr in the USER area.  This will need
149 	   to change when the kernel no longer saves all regs on a syscall.
150 	   FIXME.  There is a problem at the moment in that r3-r18 are only
151 	   saved if the process is ptraced on syscall entry, and even then
152 	   those values are overwritten by actual register values on syscall
153 	   exit. */
154 	case PTRACE_POKEUSR:
155 		/* Some register values written here may be ignored in
156 		 * entry.S:syscall_restore_rfi; e.g. iaoq is written with
157 		 * r31/r31+4, and not with the values in pt_regs.
158 		 */
159 		if (addr == PT_PSW) {
160 			/* Allow writing to Nullify, Divide-step-correction,
161 			 * and carry/borrow bits.
162 			 * BEWARE, if you set N, and then single step, it won't
163 			 * stop on the nullified instruction.
164 			 */
165 			data &= USER_PSW_BITS;
166 			task_regs(child)->gr[0] &= ~USER_PSW_BITS;
167 			task_regs(child)->gr[0] |= data;
168 			ret = 0;
169 			break;
170 		}
171 
172 		if ((addr & (sizeof(unsigned long)-1)) ||
173 		     addr >= sizeof(struct pt_regs))
174 			break;
175 		if (addr == PT_IAOQ0 || addr == PT_IAOQ1) {
176 			data |= 3; /* ensure userspace privilege */
177 		}
178 		if ((addr >= PT_GR1 && addr <= PT_GR31) ||
179 				addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
180 				(addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
181 				addr == PT_SAR) {
182 			*(unsigned long *) ((char *) task_regs(child) + addr) = data;
183 			ret = 0;
184 		}
185 		break;
186 
187 	case PTRACE_GETREGS:	/* Get all gp regs from the child. */
188 		return copy_regset_to_user(child,
189 					   task_user_regset_view(current),
190 					   REGSET_GENERAL,
191 					   0, user_regs_struct_size,
192 					   datap);
193 
194 	case PTRACE_SETREGS:	/* Set all gp regs in the child. */
195 		return copy_regset_from_user(child,
196 					     task_user_regset_view(current),
197 					     REGSET_GENERAL,
198 					     0, user_regs_struct_size,
199 					     datap);
200 
201 	case PTRACE_GETFPREGS:	/* Get the child FPU state. */
202 		return copy_regset_to_user(child,
203 					   task_user_regset_view(current),
204 					   REGSET_FP,
205 					   0, sizeof(struct user_fp_struct),
206 					   datap);
207 
208 	case PTRACE_SETFPREGS:	/* Set the child FPU state. */
209 		return copy_regset_from_user(child,
210 					     task_user_regset_view(current),
211 					     REGSET_FP,
212 					     0, sizeof(struct user_fp_struct),
213 					     datap);
214 
215 	default:
216 		ret = ptrace_request(child, request, addr, data);
217 		break;
218 	}
219 
220 	return ret;
221 }
222 
223 
224 #ifdef CONFIG_COMPAT
225 
226 /* This function is needed to translate 32 bit pt_regs offsets in to
227  * 64 bit pt_regs offsets.  For example, a 32 bit gdb under a 64 bit kernel
228  * will request offset 12 if it wants gr3, but the lower 32 bits of
229  * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
230  * This code relies on a 32 bit pt_regs being comprised of 32 bit values
231  * except for the fp registers which (a) are 64 bits, and (b) follow
232  * the gr registers at the start of pt_regs.  The 32 bit pt_regs should
233  * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
234  * being 64 bit in both cases.
235  */
236 
translate_usr_offset(compat_ulong_t offset)237 static compat_ulong_t translate_usr_offset(compat_ulong_t offset)
238 {
239 	compat_ulong_t pos;
240 
241 	if (offset < 32*4)	/* gr[0..31] */
242 		pos = offset * 2 + 4;
243 	else if (offset < 32*4+32*8)	/* fr[0] ... fr[31] */
244 		pos = (offset - 32*4) + PT_FR0;
245 	else if (offset < sizeof(struct pt_regs)/2 + 32*4) /* sr[0] ... ipsw */
246 		pos = (offset - 32*4 - 32*8) * 2 + PT_SR0 + 4;
247 	else
248 		pos = sizeof(struct pt_regs);
249 
250 	return pos;
251 }
252 
compat_arch_ptrace(struct task_struct * child,compat_long_t request,compat_ulong_t addr,compat_ulong_t data)253 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
254 			compat_ulong_t addr, compat_ulong_t data)
255 {
256 	compat_uint_t tmp;
257 	long ret = -EIO;
258 
259 	switch (request) {
260 
261 	case PTRACE_PEEKUSR:
262 		if (addr & (sizeof(compat_uint_t)-1))
263 			break;
264 		addr = translate_usr_offset(addr);
265 		if (addr >= sizeof(struct pt_regs))
266 			break;
267 
268 		tmp = *(compat_uint_t *) ((char *) task_regs(child) + addr);
269 		ret = put_user(tmp, (compat_uint_t *) (unsigned long) data);
270 		break;
271 
272 	/* Write the word at location addr in the USER area.  This will need
273 	   to change when the kernel no longer saves all regs on a syscall.
274 	   FIXME.  There is a problem at the moment in that r3-r18 are only
275 	   saved if the process is ptraced on syscall entry, and even then
276 	   those values are overwritten by actual register values on syscall
277 	   exit. */
278 	case PTRACE_POKEUSR:
279 		/* Some register values written here may be ignored in
280 		 * entry.S:syscall_restore_rfi; e.g. iaoq is written with
281 		 * r31/r31+4, and not with the values in pt_regs.
282 		 */
283 		if (addr == PT_PSW) {
284 			/* Since PT_PSW==0, it is valid for 32 bit processes
285 			 * under 64 bit kernels as well.
286 			 */
287 			ret = arch_ptrace(child, request, addr, data);
288 		} else {
289 			if (addr & (sizeof(compat_uint_t)-1))
290 				break;
291 			addr = translate_usr_offset(addr);
292 			if (addr >= sizeof(struct pt_regs))
293 				break;
294 			if (addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4) {
295 				data |= 3; /* ensure userspace privilege */
296 			}
297 			if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
298 				/* Special case, fp regs are 64 bits anyway */
299 				*(__u32 *) ((char *) task_regs(child) + addr) = data;
300 				ret = 0;
301 			}
302 			else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
303 					addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
304 					addr == PT_SAR+4) {
305 				/* Zero the top 32 bits */
306 				*(__u32 *) ((char *) task_regs(child) + addr - 4) = 0;
307 				*(__u32 *) ((char *) task_regs(child) + addr) = data;
308 				ret = 0;
309 			}
310 		}
311 		break;
312 	case PTRACE_GETREGS:
313 	case PTRACE_SETREGS:
314 	case PTRACE_GETFPREGS:
315 	case PTRACE_SETFPREGS:
316 		return arch_ptrace(child, request, addr, data);
317 
318 	default:
319 		ret = compat_ptrace_request(child, request, addr, data);
320 		break;
321 	}
322 
323 	return ret;
324 }
325 #endif
326 
do_syscall_trace_enter(struct pt_regs * regs)327 long do_syscall_trace_enter(struct pt_regs *regs)
328 {
329 	if (test_thread_flag(TIF_SYSCALL_TRACE)) {
330 		int rc = tracehook_report_syscall_entry(regs);
331 
332 		/*
333 		 * As tracesys_next does not set %r28 to -ENOSYS
334 		 * when %r20 is set to -1, initialize it here.
335 		 */
336 		regs->gr[28] = -ENOSYS;
337 
338 		if (rc) {
339 			/*
340 			 * A nonzero return code from
341 			 * tracehook_report_syscall_entry() tells us
342 			 * to prevent the syscall execution.  Skip
343 			 * the syscall call and the syscall restart handling.
344 			 *
345 			 * Note that the tracer may also just change
346 			 * regs->gr[20] to an invalid syscall number,
347 			 * that is handled by tracesys_next.
348 			 */
349 			regs->gr[20] = -1UL;
350 			return -1;
351 		}
352 	}
353 
354 	/* Do the secure computing check after ptrace. */
355 	if (secure_computing() == -1)
356 		return -1;
357 
358 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
359 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
360 		trace_sys_enter(regs, regs->gr[20]);
361 #endif
362 
363 #ifdef CONFIG_64BIT
364 	if (!is_compat_task())
365 		audit_syscall_entry(regs->gr[20], regs->gr[26], regs->gr[25],
366 				    regs->gr[24], regs->gr[23]);
367 	else
368 #endif
369 		audit_syscall_entry(regs->gr[20] & 0xffffffff,
370 			regs->gr[26] & 0xffffffff,
371 			regs->gr[25] & 0xffffffff,
372 			regs->gr[24] & 0xffffffff,
373 			regs->gr[23] & 0xffffffff);
374 
375 	/*
376 	 * Sign extend the syscall number to 64bit since it may have been
377 	 * modified by a compat ptrace call
378 	 */
379 	return (int) ((u32) regs->gr[20]);
380 }
381 
do_syscall_trace_exit(struct pt_regs * regs)382 void do_syscall_trace_exit(struct pt_regs *regs)
383 {
384 	int stepping = test_thread_flag(TIF_SINGLESTEP) ||
385 		test_thread_flag(TIF_BLOCKSTEP);
386 
387 	audit_syscall_exit(regs);
388 
389 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
390 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
391 		trace_sys_exit(regs, regs->gr[20]);
392 #endif
393 
394 	if (stepping || test_thread_flag(TIF_SYSCALL_TRACE))
395 		tracehook_report_syscall_exit(regs, stepping);
396 }
397 
398 
399 /*
400  * regset functions.
401  */
402 
fpr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)403 static int fpr_get(struct task_struct *target,
404 		     const struct user_regset *regset,
405 		     struct membuf to)
406 {
407 	struct pt_regs *regs = task_regs(target);
408 
409 	return membuf_write(&to, regs->fr, ELF_NFPREG * sizeof(__u64));
410 }
411 
fpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)412 static int fpr_set(struct task_struct *target,
413 		     const struct user_regset *regset,
414 		     unsigned int pos, unsigned int count,
415 		     const void *kbuf, const void __user *ubuf)
416 {
417 	struct pt_regs *regs = task_regs(target);
418 	const __u64 *k = kbuf;
419 	const __u64 __user *u = ubuf;
420 	__u64 reg;
421 
422 	pos /= sizeof(reg);
423 	count /= sizeof(reg);
424 
425 	if (kbuf)
426 		for (; count > 0 && pos < ELF_NFPREG; --count)
427 			regs->fr[pos++] = *k++;
428 	else
429 		for (; count > 0 && pos < ELF_NFPREG; --count) {
430 			if (__get_user(reg, u++))
431 				return -EFAULT;
432 			regs->fr[pos++] = reg;
433 		}
434 
435 	kbuf = k;
436 	ubuf = u;
437 	pos *= sizeof(reg);
438 	count *= sizeof(reg);
439 	return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
440 					 ELF_NFPREG * sizeof(reg), -1);
441 }
442 
443 #define RI(reg) (offsetof(struct user_regs_struct,reg) / sizeof(long))
444 
get_reg(struct pt_regs * regs,int num)445 static unsigned long get_reg(struct pt_regs *regs, int num)
446 {
447 	switch (num) {
448 	case RI(gr[0]) ... RI(gr[31]):	return regs->gr[num - RI(gr[0])];
449 	case RI(sr[0]) ... RI(sr[7]):	return regs->sr[num - RI(sr[0])];
450 	case RI(iasq[0]):		return regs->iasq[0];
451 	case RI(iasq[1]):		return regs->iasq[1];
452 	case RI(iaoq[0]):		return regs->iaoq[0];
453 	case RI(iaoq[1]):		return regs->iaoq[1];
454 	case RI(sar):			return regs->sar;
455 	case RI(iir):			return regs->iir;
456 	case RI(isr):			return regs->isr;
457 	case RI(ior):			return regs->ior;
458 	case RI(ipsw):			return regs->ipsw;
459 	case RI(cr27):			return regs->cr27;
460 	case RI(cr0):			return mfctl(0);
461 	case RI(cr24):			return mfctl(24);
462 	case RI(cr25):			return mfctl(25);
463 	case RI(cr26):			return mfctl(26);
464 	case RI(cr28):			return mfctl(28);
465 	case RI(cr29):			return mfctl(29);
466 	case RI(cr30):			return mfctl(30);
467 	case RI(cr31):			return mfctl(31);
468 	case RI(cr8):			return mfctl(8);
469 	case RI(cr9):			return mfctl(9);
470 	case RI(cr12):			return mfctl(12);
471 	case RI(cr13):			return mfctl(13);
472 	case RI(cr10):			return mfctl(10);
473 	case RI(cr15):			return mfctl(15);
474 	default:			return 0;
475 	}
476 }
477 
set_reg(struct pt_regs * regs,int num,unsigned long val)478 static void set_reg(struct pt_regs *regs, int num, unsigned long val)
479 {
480 	switch (num) {
481 	case RI(gr[0]): /*
482 			 * PSW is in gr[0].
483 			 * Allow writing to Nullify, Divide-step-correction,
484 			 * and carry/borrow bits.
485 			 * BEWARE, if you set N, and then single step, it won't
486 			 * stop on the nullified instruction.
487 			 */
488 			val &= USER_PSW_BITS;
489 			regs->gr[0] &= ~USER_PSW_BITS;
490 			regs->gr[0] |= val;
491 			return;
492 	case RI(gr[1]) ... RI(gr[31]):
493 			regs->gr[num - RI(gr[0])] = val;
494 			return;
495 	case RI(iaoq[0]):
496 	case RI(iaoq[1]):
497 			/* set 2 lowest bits to ensure userspace privilege: */
498 			regs->iaoq[num - RI(iaoq[0])] = val | 3;
499 			return;
500 	case RI(sar):	regs->sar = val;
501 			return;
502 	default:	return;
503 #if 0
504 	/* do not allow to change any of the following registers (yet) */
505 	case RI(sr[0]) ... RI(sr[7]):	return regs->sr[num - RI(sr[0])];
506 	case RI(iasq[0]):		return regs->iasq[0];
507 	case RI(iasq[1]):		return regs->iasq[1];
508 	case RI(iir):			return regs->iir;
509 	case RI(isr):			return regs->isr;
510 	case RI(ior):			return regs->ior;
511 	case RI(ipsw):			return regs->ipsw;
512 	case RI(cr27):			return regs->cr27;
513         case cr0, cr24, cr25, cr26, cr27, cr28, cr29, cr30, cr31;
514         case cr8, cr9, cr12, cr13, cr10, cr15;
515 #endif
516 	}
517 }
518 
gpr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)519 static int gpr_get(struct task_struct *target,
520 		     const struct user_regset *regset,
521 		     struct membuf to)
522 {
523 	struct pt_regs *regs = task_regs(target);
524 	unsigned int pos;
525 
526 	for (pos = 0; pos < ELF_NGREG; pos++)
527 		membuf_store(&to, get_reg(regs, pos));
528 	return 0;
529 }
530 
gpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)531 static int gpr_set(struct task_struct *target,
532 		     const struct user_regset *regset,
533 		     unsigned int pos, unsigned int count,
534 		     const void *kbuf, const void __user *ubuf)
535 {
536 	struct pt_regs *regs = task_regs(target);
537 	const unsigned long *k = kbuf;
538 	const unsigned long __user *u = ubuf;
539 	unsigned long reg;
540 
541 	pos /= sizeof(reg);
542 	count /= sizeof(reg);
543 
544 	if (kbuf)
545 		for (; count > 0 && pos < ELF_NGREG; --count)
546 			set_reg(regs, pos++, *k++);
547 	else
548 		for (; count > 0 && pos < ELF_NGREG; --count) {
549 			if (__get_user(reg, u++))
550 				return -EFAULT;
551 			set_reg(regs, pos++, reg);
552 		}
553 
554 	kbuf = k;
555 	ubuf = u;
556 	pos *= sizeof(reg);
557 	count *= sizeof(reg);
558 	return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
559 					 ELF_NGREG * sizeof(reg), -1);
560 }
561 
562 static const struct user_regset native_regsets[] = {
563 	[REGSET_GENERAL] = {
564 		.core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
565 		.size = sizeof(long), .align = sizeof(long),
566 		.regset_get = gpr_get, .set = gpr_set
567 	},
568 	[REGSET_FP] = {
569 		.core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
570 		.size = sizeof(__u64), .align = sizeof(__u64),
571 		.regset_get = fpr_get, .set = fpr_set
572 	}
573 };
574 
575 static const struct user_regset_view user_parisc_native_view = {
576 	.name = "parisc", .e_machine = ELF_ARCH, .ei_osabi = ELFOSABI_LINUX,
577 	.regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
578 };
579 
580 #ifdef CONFIG_64BIT
gpr32_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)581 static int gpr32_get(struct task_struct *target,
582 		     const struct user_regset *regset,
583 		     struct membuf to)
584 {
585 	struct pt_regs *regs = task_regs(target);
586 	unsigned int pos;
587 
588 	for (pos = 0; pos < ELF_NGREG; pos++)
589 		membuf_store(&to, (compat_ulong_t)get_reg(regs, pos));
590 
591 	return 0;
592 }
593 
gpr32_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)594 static int gpr32_set(struct task_struct *target,
595 		     const struct user_regset *regset,
596 		     unsigned int pos, unsigned int count,
597 		     const void *kbuf, const void __user *ubuf)
598 {
599 	struct pt_regs *regs = task_regs(target);
600 	const compat_ulong_t *k = kbuf;
601 	const compat_ulong_t __user *u = ubuf;
602 	compat_ulong_t reg;
603 
604 	pos /= sizeof(reg);
605 	count /= sizeof(reg);
606 
607 	if (kbuf)
608 		for (; count > 0 && pos < ELF_NGREG; --count)
609 			set_reg(regs, pos++, *k++);
610 	else
611 		for (; count > 0 && pos < ELF_NGREG; --count) {
612 			if (__get_user(reg, u++))
613 				return -EFAULT;
614 			set_reg(regs, pos++, reg);
615 		}
616 
617 	kbuf = k;
618 	ubuf = u;
619 	pos *= sizeof(reg);
620 	count *= sizeof(reg);
621 	return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
622 					 ELF_NGREG * sizeof(reg), -1);
623 }
624 
625 /*
626  * These are the regset flavors matching the 32bit native set.
627  */
628 static const struct user_regset compat_regsets[] = {
629 	[REGSET_GENERAL] = {
630 		.core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
631 		.size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
632 		.regset_get = gpr32_get, .set = gpr32_set
633 	},
634 	[REGSET_FP] = {
635 		.core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
636 		.size = sizeof(__u64), .align = sizeof(__u64),
637 		.regset_get = fpr_get, .set = fpr_set
638 	}
639 };
640 
641 static const struct user_regset_view user_parisc_compat_view = {
642 	.name = "parisc", .e_machine = EM_PARISC, .ei_osabi = ELFOSABI_LINUX,
643 	.regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
644 };
645 #endif	/* CONFIG_64BIT */
646 
task_user_regset_view(struct task_struct * task)647 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
648 {
649 	BUILD_BUG_ON(sizeof(struct user_regs_struct)/sizeof(long) != ELF_NGREG);
650 	BUILD_BUG_ON(sizeof(struct user_fp_struct)/sizeof(__u64) != ELF_NFPREG);
651 #ifdef CONFIG_64BIT
652 	if (is_compat_task())
653 		return &user_parisc_compat_view;
654 #endif
655 	return &user_parisc_native_view;
656 }
657 
658 
659 /* HAVE_REGS_AND_STACK_ACCESS_API feature */
660 
661 struct pt_regs_offset {
662 	const char *name;
663 	int offset;
664 };
665 
666 #define REG_OFFSET_NAME(r)    {.name = #r, .offset = offsetof(struct pt_regs, r)}
667 #define REG_OFFSET_INDEX(r,i) {.name = #r#i, .offset = offsetof(struct pt_regs, r[i])}
668 #define REG_OFFSET_END {.name = NULL, .offset = 0}
669 
670 static const struct pt_regs_offset regoffset_table[] = {
671 	REG_OFFSET_INDEX(gr,0),
672 	REG_OFFSET_INDEX(gr,1),
673 	REG_OFFSET_INDEX(gr,2),
674 	REG_OFFSET_INDEX(gr,3),
675 	REG_OFFSET_INDEX(gr,4),
676 	REG_OFFSET_INDEX(gr,5),
677 	REG_OFFSET_INDEX(gr,6),
678 	REG_OFFSET_INDEX(gr,7),
679 	REG_OFFSET_INDEX(gr,8),
680 	REG_OFFSET_INDEX(gr,9),
681 	REG_OFFSET_INDEX(gr,10),
682 	REG_OFFSET_INDEX(gr,11),
683 	REG_OFFSET_INDEX(gr,12),
684 	REG_OFFSET_INDEX(gr,13),
685 	REG_OFFSET_INDEX(gr,14),
686 	REG_OFFSET_INDEX(gr,15),
687 	REG_OFFSET_INDEX(gr,16),
688 	REG_OFFSET_INDEX(gr,17),
689 	REG_OFFSET_INDEX(gr,18),
690 	REG_OFFSET_INDEX(gr,19),
691 	REG_OFFSET_INDEX(gr,20),
692 	REG_OFFSET_INDEX(gr,21),
693 	REG_OFFSET_INDEX(gr,22),
694 	REG_OFFSET_INDEX(gr,23),
695 	REG_OFFSET_INDEX(gr,24),
696 	REG_OFFSET_INDEX(gr,25),
697 	REG_OFFSET_INDEX(gr,26),
698 	REG_OFFSET_INDEX(gr,27),
699 	REG_OFFSET_INDEX(gr,28),
700 	REG_OFFSET_INDEX(gr,29),
701 	REG_OFFSET_INDEX(gr,30),
702 	REG_OFFSET_INDEX(gr,31),
703 	REG_OFFSET_INDEX(sr,0),
704 	REG_OFFSET_INDEX(sr,1),
705 	REG_OFFSET_INDEX(sr,2),
706 	REG_OFFSET_INDEX(sr,3),
707 	REG_OFFSET_INDEX(sr,4),
708 	REG_OFFSET_INDEX(sr,5),
709 	REG_OFFSET_INDEX(sr,6),
710 	REG_OFFSET_INDEX(sr,7),
711 	REG_OFFSET_INDEX(iasq,0),
712 	REG_OFFSET_INDEX(iasq,1),
713 	REG_OFFSET_INDEX(iaoq,0),
714 	REG_OFFSET_INDEX(iaoq,1),
715 	REG_OFFSET_NAME(cr27),
716 	REG_OFFSET_NAME(ksp),
717 	REG_OFFSET_NAME(kpc),
718 	REG_OFFSET_NAME(sar),
719 	REG_OFFSET_NAME(iir),
720 	REG_OFFSET_NAME(isr),
721 	REG_OFFSET_NAME(ior),
722 	REG_OFFSET_NAME(ipsw),
723 	REG_OFFSET_END,
724 };
725 
726 /**
727  * regs_query_register_offset() - query register offset from its name
728  * @name:	the name of a register
729  *
730  * regs_query_register_offset() returns the offset of a register in struct
731  * pt_regs from its name. If the name is invalid, this returns -EINVAL;
732  */
regs_query_register_offset(const char * name)733 int regs_query_register_offset(const char *name)
734 {
735 	const struct pt_regs_offset *roff;
736 	for (roff = regoffset_table; roff->name != NULL; roff++)
737 		if (!strcmp(roff->name, name))
738 			return roff->offset;
739 	return -EINVAL;
740 }
741 
742 /**
743  * regs_query_register_name() - query register name from its offset
744  * @offset:	the offset of a register in struct pt_regs.
745  *
746  * regs_query_register_name() returns the name of a register from its
747  * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
748  */
regs_query_register_name(unsigned int offset)749 const char *regs_query_register_name(unsigned int offset)
750 {
751 	const struct pt_regs_offset *roff;
752 	for (roff = regoffset_table; roff->name != NULL; roff++)
753 		if (roff->offset == offset)
754 			return roff->name;
755 	return NULL;
756 }
757 
758 /**
759  * regs_within_kernel_stack() - check the address in the stack
760  * @regs:      pt_regs which contains kernel stack pointer.
761  * @addr:      address which is checked.
762  *
763  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
764  * If @addr is within the kernel stack, it returns true. If not, returns false.
765  */
regs_within_kernel_stack(struct pt_regs * regs,unsigned long addr)766 int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
767 {
768 	return ((addr & ~(THREAD_SIZE - 1))  ==
769 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
770 }
771 
772 /**
773  * regs_get_kernel_stack_nth() - get Nth entry of the stack
774  * @regs:	pt_regs which contains kernel stack pointer.
775  * @n:		stack entry number.
776  *
777  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
778  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
779  * this returns 0.
780  */
regs_get_kernel_stack_nth(struct pt_regs * regs,unsigned int n)781 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
782 {
783 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
784 
785 	addr -= n;
786 
787 	if (!regs_within_kernel_stack(regs, (unsigned long)addr))
788 		return 0;
789 
790 	return *addr;
791 }
792