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