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