]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/s390/kernel/ptrace.c
s390: fix save and restore of the floating-point-control register
[karo-tx-linux.git] / arch / s390 / kernel / ptrace.c
1 /*
2  *  Ptrace user space interface.
3  *
4  *    Copyright IBM Corp. 1999, 2010
5  *    Author(s): Denis Joseph Barrow
6  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/security.h>
17 #include <linux/audit.h>
18 #include <linux/signal.h>
19 #include <linux/elf.h>
20 #include <linux/regset.h>
21 #include <linux/tracehook.h>
22 #include <linux/seccomp.h>
23 #include <linux/compat.h>
24 #include <trace/syscall.h>
25 #include <asm/segment.h>
26 #include <asm/page.h>
27 #include <asm/pgtable.h>
28 #include <asm/pgalloc.h>
29 #include <asm/uaccess.h>
30 #include <asm/unistd.h>
31 #include <asm/switch_to.h>
32 #include "entry.h"
33
34 #ifdef CONFIG_COMPAT
35 #include "compat_ptrace.h"
36 #endif
37
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/syscalls.h>
40
41 enum s390_regset {
42         REGSET_GENERAL,
43         REGSET_FP,
44         REGSET_LAST_BREAK,
45         REGSET_TDB,
46         REGSET_SYSTEM_CALL,
47         REGSET_GENERAL_EXTENDED,
48 };
49
50 void update_cr_regs(struct task_struct *task)
51 {
52         struct pt_regs *regs = task_pt_regs(task);
53         struct thread_struct *thread = &task->thread;
54         struct per_regs old, new;
55
56 #ifdef CONFIG_64BIT
57         /* Take care of the enable/disable of transactional execution. */
58         if (MACHINE_HAS_TE) {
59                 unsigned long cr[3], cr_new[3];
60
61                 __ctl_store(cr, 0, 2);
62                 cr_new[1] = cr[1];
63                 /* Set or clear transaction execution TXC bit 8. */
64                 if (task->thread.per_flags & PER_FLAG_NO_TE)
65                         cr_new[0] = cr[0] & ~(1UL << 55);
66                 else
67                         cr_new[0] = cr[0] | (1UL << 55);
68                 /* Set or clear transaction execution TDC bits 62 and 63. */
69                 cr_new[2] = cr[2] & ~3UL;
70                 if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND) {
71                         if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND_TEND)
72                                 cr_new[2] |= 1UL;
73                         else
74                                 cr_new[2] |= 2UL;
75                 }
76                 if (memcmp(&cr_new, &cr, sizeof(cr)))
77                         __ctl_load(cr_new, 0, 2);
78         }
79 #endif
80         /* Copy user specified PER registers */
81         new.control = thread->per_user.control;
82         new.start = thread->per_user.start;
83         new.end = thread->per_user.end;
84
85         /* merge TIF_SINGLE_STEP into user specified PER registers. */
86         if (test_tsk_thread_flag(task, TIF_SINGLE_STEP)) {
87                 new.control |= PER_EVENT_IFETCH;
88 #ifdef CONFIG_64BIT
89                 new.control |= PER_CONTROL_SUSPENSION;
90                 new.control |= PER_EVENT_TRANSACTION_END;
91 #endif
92                 new.start = 0;
93                 new.end = PSW_ADDR_INSN;
94         }
95
96         /* Take care of the PER enablement bit in the PSW. */
97         if (!(new.control & PER_EVENT_MASK)) {
98                 regs->psw.mask &= ~PSW_MASK_PER;
99                 return;
100         }
101         regs->psw.mask |= PSW_MASK_PER;
102         __ctl_store(old, 9, 11);
103         if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
104                 __ctl_load(new, 9, 11);
105 }
106
107 void user_enable_single_step(struct task_struct *task)
108 {
109         set_tsk_thread_flag(task, TIF_SINGLE_STEP);
110         if (task == current)
111                 update_cr_regs(task);
112 }
113
114 void user_disable_single_step(struct task_struct *task)
115 {
116         clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
117         if (task == current)
118                 update_cr_regs(task);
119 }
120
121 /*
122  * Called by kernel/ptrace.c when detaching..
123  *
124  * Clear all debugging related fields.
125  */
126 void ptrace_disable(struct task_struct *task)
127 {
128         memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
129         memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
130         clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
131         clear_tsk_thread_flag(task, TIF_PER_TRAP);
132         task->thread.per_flags = 0;
133 }
134
135 #ifndef CONFIG_64BIT
136 # define __ADDR_MASK 3
137 #else
138 # define __ADDR_MASK 7
139 #endif
140
141 static inline unsigned long __peek_user_per(struct task_struct *child,
142                                             addr_t addr)
143 {
144         struct per_struct_kernel *dummy = NULL;
145
146         if (addr == (addr_t) &dummy->cr9)
147                 /* Control bits of the active per set. */
148                 return test_thread_flag(TIF_SINGLE_STEP) ?
149                         PER_EVENT_IFETCH : child->thread.per_user.control;
150         else if (addr == (addr_t) &dummy->cr10)
151                 /* Start address of the active per set. */
152                 return test_thread_flag(TIF_SINGLE_STEP) ?
153                         0 : child->thread.per_user.start;
154         else if (addr == (addr_t) &dummy->cr11)
155                 /* End address of the active per set. */
156                 return test_thread_flag(TIF_SINGLE_STEP) ?
157                         PSW_ADDR_INSN : child->thread.per_user.end;
158         else if (addr == (addr_t) &dummy->bits)
159                 /* Single-step bit. */
160                 return test_thread_flag(TIF_SINGLE_STEP) ?
161                         (1UL << (BITS_PER_LONG - 1)) : 0;
162         else if (addr == (addr_t) &dummy->starting_addr)
163                 /* Start address of the user specified per set. */
164                 return child->thread.per_user.start;
165         else if (addr == (addr_t) &dummy->ending_addr)
166                 /* End address of the user specified per set. */
167                 return child->thread.per_user.end;
168         else if (addr == (addr_t) &dummy->perc_atmid)
169                 /* PER code, ATMID and AI of the last PER trap */
170                 return (unsigned long)
171                         child->thread.per_event.cause << (BITS_PER_LONG - 16);
172         else if (addr == (addr_t) &dummy->address)
173                 /* Address of the last PER trap */
174                 return child->thread.per_event.address;
175         else if (addr == (addr_t) &dummy->access_id)
176                 /* Access id of the last PER trap */
177                 return (unsigned long)
178                         child->thread.per_event.paid << (BITS_PER_LONG - 8);
179         return 0;
180 }
181
182 /*
183  * Read the word at offset addr from the user area of a process. The
184  * trouble here is that the information is littered over different
185  * locations. The process registers are found on the kernel stack,
186  * the floating point stuff and the trace settings are stored in
187  * the task structure. In addition the different structures in
188  * struct user contain pad bytes that should be read as zeroes.
189  * Lovely...
190  */
191 static unsigned long __peek_user(struct task_struct *child, addr_t addr)
192 {
193         struct user *dummy = NULL;
194         addr_t offset, tmp;
195
196         if (addr < (addr_t) &dummy->regs.acrs) {
197                 /*
198                  * psw and gprs are stored on the stack
199                  */
200                 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
201                 if (addr == (addr_t) &dummy->regs.psw.mask)
202                         /* Return a clean psw mask. */
203                         tmp = PSW_USER_BITS | (tmp & PSW_MASK_USER);
204
205         } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
206                 /*
207                  * access registers are stored in the thread structure
208                  */
209                 offset = addr - (addr_t) &dummy->regs.acrs;
210 #ifdef CONFIG_64BIT
211                 /*
212                  * Very special case: old & broken 64 bit gdb reading
213                  * from acrs[15]. Result is a 64 bit value. Read the
214                  * 32 bit acrs[15] value and shift it by 32. Sick...
215                  */
216                 if (addr == (addr_t) &dummy->regs.acrs[15])
217                         tmp = ((unsigned long) child->thread.acrs[15]) << 32;
218                 else
219 #endif
220                 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
221
222         } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
223                 /*
224                  * orig_gpr2 is stored on the kernel stack
225                  */
226                 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
227
228         } else if (addr < (addr_t) &dummy->regs.fp_regs) {
229                 /*
230                  * prevent reads of padding hole between
231                  * orig_gpr2 and fp_regs on s390.
232                  */
233                 tmp = 0;
234
235         } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
236                 /* 
237                  * floating point regs. are stored in the thread structure
238                  */
239                 offset = addr - (addr_t) &dummy->regs.fp_regs;
240                 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
241                 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
242                         tmp <<= BITS_PER_LONG - 32;
243
244         } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
245                 /*
246                  * Handle access to the per_info structure.
247                  */
248                 addr -= (addr_t) &dummy->regs.per_info;
249                 tmp = __peek_user_per(child, addr);
250
251         } else
252                 tmp = 0;
253
254         return tmp;
255 }
256
257 static int
258 peek_user(struct task_struct *child, addr_t addr, addr_t data)
259 {
260         addr_t tmp, mask;
261
262         /*
263          * Stupid gdb peeks/pokes the access registers in 64 bit with
264          * an alignment of 4. Programmers from hell...
265          */
266         mask = __ADDR_MASK;
267 #ifdef CONFIG_64BIT
268         if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
269             addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
270                 mask = 3;
271 #endif
272         if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
273                 return -EIO;
274
275         tmp = __peek_user(child, addr);
276         return put_user(tmp, (addr_t __user *) data);
277 }
278
279 static inline void __poke_user_per(struct task_struct *child,
280                                    addr_t addr, addr_t data)
281 {
282         struct per_struct_kernel *dummy = NULL;
283
284         /*
285          * There are only three fields in the per_info struct that the
286          * debugger user can write to.
287          * 1) cr9: the debugger wants to set a new PER event mask
288          * 2) starting_addr: the debugger wants to set a new starting
289          *    address to use with the PER event mask.
290          * 3) ending_addr: the debugger wants to set a new ending
291          *    address to use with the PER event mask.
292          * The user specified PER event mask and the start and end
293          * addresses are used only if single stepping is not in effect.
294          * Writes to any other field in per_info are ignored.
295          */
296         if (addr == (addr_t) &dummy->cr9)
297                 /* PER event mask of the user specified per set. */
298                 child->thread.per_user.control =
299                         data & (PER_EVENT_MASK | PER_CONTROL_MASK);
300         else if (addr == (addr_t) &dummy->starting_addr)
301                 /* Starting address of the user specified per set. */
302                 child->thread.per_user.start = data;
303         else if (addr == (addr_t) &dummy->ending_addr)
304                 /* Ending address of the user specified per set. */
305                 child->thread.per_user.end = data;
306 }
307
308 /*
309  * Write a word to the user area of a process at location addr. This
310  * operation does have an additional problem compared to peek_user.
311  * Stores to the program status word and on the floating point
312  * control register needs to get checked for validity.
313  */
314 static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
315 {
316         struct user *dummy = NULL;
317         addr_t offset;
318
319         if (addr < (addr_t) &dummy->regs.acrs) {
320                 /*
321                  * psw and gprs are stored on the stack
322                  */
323                 if (addr == (addr_t) &dummy->regs.psw.mask &&
324                     ((data & ~PSW_MASK_USER) != PSW_USER_BITS ||
325                      ((data & PSW_MASK_EA) && !(data & PSW_MASK_BA))))
326                         /* Invalid psw mask. */
327                         return -EINVAL;
328                 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
329
330         } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
331                 /*
332                  * access registers are stored in the thread structure
333                  */
334                 offset = addr - (addr_t) &dummy->regs.acrs;
335 #ifdef CONFIG_64BIT
336                 /*
337                  * Very special case: old & broken 64 bit gdb writing
338                  * to acrs[15] with a 64 bit value. Ignore the lower
339                  * half of the value and write the upper 32 bit to
340                  * acrs[15]. Sick...
341                  */
342                 if (addr == (addr_t) &dummy->regs.acrs[15])
343                         child->thread.acrs[15] = (unsigned int) (data >> 32);
344                 else
345 #endif
346                 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
347
348         } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
349                 /*
350                  * orig_gpr2 is stored on the kernel stack
351                  */
352                 task_pt_regs(child)->orig_gpr2 = data;
353
354         } else if (addr < (addr_t) &dummy->regs.fp_regs) {
355                 /*
356                  * prevent writes of padding hole between
357                  * orig_gpr2 and fp_regs on s390.
358                  */
359                 return 0;
360
361         } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
362                 /*
363                  * floating point regs. are stored in the thread structure
364                  */
365                 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
366                         if ((unsigned int) data != 0 ||
367                             test_fp_ctl(data >> (BITS_PER_LONG - 32)))
368                                 return -EINVAL;
369                 offset = addr - (addr_t) &dummy->regs.fp_regs;
370                 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
371
372         } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
373                 /*
374                  * Handle access to the per_info structure.
375                  */
376                 addr -= (addr_t) &dummy->regs.per_info;
377                 __poke_user_per(child, addr, data);
378
379         }
380
381         return 0;
382 }
383
384 static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
385 {
386         addr_t mask;
387
388         /*
389          * Stupid gdb peeks/pokes the access registers in 64 bit with
390          * an alignment of 4. Programmers from hell indeed...
391          */
392         mask = __ADDR_MASK;
393 #ifdef CONFIG_64BIT
394         if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
395             addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
396                 mask = 3;
397 #endif
398         if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
399                 return -EIO;
400
401         return __poke_user(child, addr, data);
402 }
403
404 long arch_ptrace(struct task_struct *child, long request,
405                  unsigned long addr, unsigned long data)
406 {
407         ptrace_area parea; 
408         int copied, ret;
409
410         switch (request) {
411         case PTRACE_PEEKUSR:
412                 /* read the word at location addr in the USER area. */
413                 return peek_user(child, addr, data);
414
415         case PTRACE_POKEUSR:
416                 /* write the word at location addr in the USER area */
417                 return poke_user(child, addr, data);
418
419         case PTRACE_PEEKUSR_AREA:
420         case PTRACE_POKEUSR_AREA:
421                 if (copy_from_user(&parea, (void __force __user *) addr,
422                                                         sizeof(parea)))
423                         return -EFAULT;
424                 addr = parea.kernel_addr;
425                 data = parea.process_addr;
426                 copied = 0;
427                 while (copied < parea.len) {
428                         if (request == PTRACE_PEEKUSR_AREA)
429                                 ret = peek_user(child, addr, data);
430                         else {
431                                 addr_t utmp;
432                                 if (get_user(utmp,
433                                              (addr_t __force __user *) data))
434                                         return -EFAULT;
435                                 ret = poke_user(child, addr, utmp);
436                         }
437                         if (ret)
438                                 return ret;
439                         addr += sizeof(unsigned long);
440                         data += sizeof(unsigned long);
441                         copied += sizeof(unsigned long);
442                 }
443                 return 0;
444         case PTRACE_GET_LAST_BREAK:
445                 put_user(task_thread_info(child)->last_break,
446                          (unsigned long __user *) data);
447                 return 0;
448         case PTRACE_ENABLE_TE:
449                 if (!MACHINE_HAS_TE)
450                         return -EIO;
451                 child->thread.per_flags &= ~PER_FLAG_NO_TE;
452                 return 0;
453         case PTRACE_DISABLE_TE:
454                 if (!MACHINE_HAS_TE)
455                         return -EIO;
456                 child->thread.per_flags |= PER_FLAG_NO_TE;
457                 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
458                 return 0;
459         case PTRACE_TE_ABORT_RAND:
460                 if (!MACHINE_HAS_TE || (child->thread.per_flags & PER_FLAG_NO_TE))
461                         return -EIO;
462                 switch (data) {
463                 case 0UL:
464                         child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
465                         break;
466                 case 1UL:
467                         child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
468                         child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND_TEND;
469                         break;
470                 case 2UL:
471                         child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
472                         child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND_TEND;
473                         break;
474                 default:
475                         return -EINVAL;
476                 }
477                 return 0;
478         default:
479                 /* Removing high order bit from addr (only for 31 bit). */
480                 addr &= PSW_ADDR_INSN;
481                 return ptrace_request(child, request, addr, data);
482         }
483 }
484
485 #ifdef CONFIG_COMPAT
486 /*
487  * Now the fun part starts... a 31 bit program running in the
488  * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
489  * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
490  * to handle, the difference to the 64 bit versions of the requests
491  * is that the access is done in multiples of 4 byte instead of
492  * 8 bytes (sizeof(unsigned long) on 31/64 bit).
493  * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
494  * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
495  * is a 31 bit program too, the content of struct user can be
496  * emulated. A 31 bit program peeking into the struct user of
497  * a 64 bit program is a no-no.
498  */
499
500 /*
501  * Same as peek_user_per but for a 31 bit program.
502  */
503 static inline __u32 __peek_user_per_compat(struct task_struct *child,
504                                            addr_t addr)
505 {
506         struct compat_per_struct_kernel *dummy32 = NULL;
507
508         if (addr == (addr_t) &dummy32->cr9)
509                 /* Control bits of the active per set. */
510                 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
511                         PER_EVENT_IFETCH : child->thread.per_user.control;
512         else if (addr == (addr_t) &dummy32->cr10)
513                 /* Start address of the active per set. */
514                 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
515                         0 : child->thread.per_user.start;
516         else if (addr == (addr_t) &dummy32->cr11)
517                 /* End address of the active per set. */
518                 return test_thread_flag(TIF_SINGLE_STEP) ?
519                         PSW32_ADDR_INSN : child->thread.per_user.end;
520         else if (addr == (addr_t) &dummy32->bits)
521                 /* Single-step bit. */
522                 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
523                         0x80000000 : 0;
524         else if (addr == (addr_t) &dummy32->starting_addr)
525                 /* Start address of the user specified per set. */
526                 return (__u32) child->thread.per_user.start;
527         else if (addr == (addr_t) &dummy32->ending_addr)
528                 /* End address of the user specified per set. */
529                 return (__u32) child->thread.per_user.end;
530         else if (addr == (addr_t) &dummy32->perc_atmid)
531                 /* PER code, ATMID and AI of the last PER trap */
532                 return (__u32) child->thread.per_event.cause << 16;
533         else if (addr == (addr_t) &dummy32->address)
534                 /* Address of the last PER trap */
535                 return (__u32) child->thread.per_event.address;
536         else if (addr == (addr_t) &dummy32->access_id)
537                 /* Access id of the last PER trap */
538                 return (__u32) child->thread.per_event.paid << 24;
539         return 0;
540 }
541
542 /*
543  * Same as peek_user but for a 31 bit program.
544  */
545 static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
546 {
547         struct compat_user *dummy32 = NULL;
548         addr_t offset;
549         __u32 tmp;
550
551         if (addr < (addr_t) &dummy32->regs.acrs) {
552                 struct pt_regs *regs = task_pt_regs(child);
553                 /*
554                  * psw and gprs are stored on the stack
555                  */
556                 if (addr == (addr_t) &dummy32->regs.psw.mask) {
557                         /* Fake a 31 bit psw mask. */
558                         tmp = (__u32)(regs->psw.mask >> 32);
559                         tmp = psw32_user_bits | (tmp & PSW32_MASK_USER);
560                 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
561                         /* Fake a 31 bit psw address. */
562                         tmp = (__u32) regs->psw.addr |
563                                 (__u32)(regs->psw.mask & PSW_MASK_BA);
564                 } else {
565                         /* gpr 0-15 */
566                         tmp = *(__u32 *)((addr_t) &regs->psw + addr*2 + 4);
567                 }
568         } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
569                 /*
570                  * access registers are stored in the thread structure
571                  */
572                 offset = addr - (addr_t) &dummy32->regs.acrs;
573                 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
574
575         } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
576                 /*
577                  * orig_gpr2 is stored on the kernel stack
578                  */
579                 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
580
581         } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
582                 /*
583                  * prevent reads of padding hole between
584                  * orig_gpr2 and fp_regs on s390.
585                  */
586                 tmp = 0;
587
588         } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
589                 /*
590                  * floating point regs. are stored in the thread structure 
591                  */
592                 offset = addr - (addr_t) &dummy32->regs.fp_regs;
593                 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
594
595         } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
596                 /*
597                  * Handle access to the per_info structure.
598                  */
599                 addr -= (addr_t) &dummy32->regs.per_info;
600                 tmp = __peek_user_per_compat(child, addr);
601
602         } else
603                 tmp = 0;
604
605         return tmp;
606 }
607
608 static int peek_user_compat(struct task_struct *child,
609                             addr_t addr, addr_t data)
610 {
611         __u32 tmp;
612
613         if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
614                 return -EIO;
615
616         tmp = __peek_user_compat(child, addr);
617         return put_user(tmp, (__u32 __user *) data);
618 }
619
620 /*
621  * Same as poke_user_per but for a 31 bit program.
622  */
623 static inline void __poke_user_per_compat(struct task_struct *child,
624                                           addr_t addr, __u32 data)
625 {
626         struct compat_per_struct_kernel *dummy32 = NULL;
627
628         if (addr == (addr_t) &dummy32->cr9)
629                 /* PER event mask of the user specified per set. */
630                 child->thread.per_user.control =
631                         data & (PER_EVENT_MASK | PER_CONTROL_MASK);
632         else if (addr == (addr_t) &dummy32->starting_addr)
633                 /* Starting address of the user specified per set. */
634                 child->thread.per_user.start = data;
635         else if (addr == (addr_t) &dummy32->ending_addr)
636                 /* Ending address of the user specified per set. */
637                 child->thread.per_user.end = data;
638 }
639
640 /*
641  * Same as poke_user but for a 31 bit program.
642  */
643 static int __poke_user_compat(struct task_struct *child,
644                               addr_t addr, addr_t data)
645 {
646         struct compat_user *dummy32 = NULL;
647         __u32 tmp = (__u32) data;
648         addr_t offset;
649
650         if (addr < (addr_t) &dummy32->regs.acrs) {
651                 struct pt_regs *regs = task_pt_regs(child);
652                 /*
653                  * psw, gprs, acrs and orig_gpr2 are stored on the stack
654                  */
655                 if (addr == (addr_t) &dummy32->regs.psw.mask) {
656                         /* Build a 64 bit psw mask from 31 bit mask. */
657                         if ((tmp & ~PSW32_MASK_USER) != psw32_user_bits)
658                                 /* Invalid psw mask. */
659                                 return -EINVAL;
660                         regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
661                                 (regs->psw.mask & PSW_MASK_BA) |
662                                 (__u64)(tmp & PSW32_MASK_USER) << 32;
663                 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
664                         /* Build a 64 bit psw address from 31 bit address. */
665                         regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
666                         /* Transfer 31 bit amode bit to psw mask. */
667                         regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
668                                 (__u64)(tmp & PSW32_ADDR_AMODE);
669                 } else {
670                         /* gpr 0-15 */
671                         *(__u32*)((addr_t) &regs->psw + addr*2 + 4) = tmp;
672                 }
673         } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
674                 /*
675                  * access registers are stored in the thread structure
676                  */
677                 offset = addr - (addr_t) &dummy32->regs.acrs;
678                 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
679
680         } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
681                 /*
682                  * orig_gpr2 is stored on the kernel stack
683                  */
684                 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
685
686         } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
687                 /*
688                  * prevent writess of padding hole between
689                  * orig_gpr2 and fp_regs on s390.
690                  */
691                 return 0;
692
693         } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
694                 /*
695                  * floating point regs. are stored in the thread structure 
696                  */
697                 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
698                     test_fp_ctl(tmp))
699                         return -EINVAL;
700                 offset = addr - (addr_t) &dummy32->regs.fp_regs;
701                 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
702
703         } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
704                 /*
705                  * Handle access to the per_info structure.
706                  */
707                 addr -= (addr_t) &dummy32->regs.per_info;
708                 __poke_user_per_compat(child, addr, data);
709         }
710
711         return 0;
712 }
713
714 static int poke_user_compat(struct task_struct *child,
715                             addr_t addr, addr_t data)
716 {
717         if (!is_compat_task() || (addr & 3) ||
718             addr > sizeof(struct compat_user) - 3)
719                 return -EIO;
720
721         return __poke_user_compat(child, addr, data);
722 }
723
724 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
725                         compat_ulong_t caddr, compat_ulong_t cdata)
726 {
727         unsigned long addr = caddr;
728         unsigned long data = cdata;
729         compat_ptrace_area parea;
730         int copied, ret;
731
732         switch (request) {
733         case PTRACE_PEEKUSR:
734                 /* read the word at location addr in the USER area. */
735                 return peek_user_compat(child, addr, data);
736
737         case PTRACE_POKEUSR:
738                 /* write the word at location addr in the USER area */
739                 return poke_user_compat(child, addr, data);
740
741         case PTRACE_PEEKUSR_AREA:
742         case PTRACE_POKEUSR_AREA:
743                 if (copy_from_user(&parea, (void __force __user *) addr,
744                                                         sizeof(parea)))
745                         return -EFAULT;
746                 addr = parea.kernel_addr;
747                 data = parea.process_addr;
748                 copied = 0;
749                 while (copied < parea.len) {
750                         if (request == PTRACE_PEEKUSR_AREA)
751                                 ret = peek_user_compat(child, addr, data);
752                         else {
753                                 __u32 utmp;
754                                 if (get_user(utmp,
755                                              (__u32 __force __user *) data))
756                                         return -EFAULT;
757                                 ret = poke_user_compat(child, addr, utmp);
758                         }
759                         if (ret)
760                                 return ret;
761                         addr += sizeof(unsigned int);
762                         data += sizeof(unsigned int);
763                         copied += sizeof(unsigned int);
764                 }
765                 return 0;
766         case PTRACE_GET_LAST_BREAK:
767                 put_user(task_thread_info(child)->last_break,
768                          (unsigned int __user *) data);
769                 return 0;
770         }
771         return compat_ptrace_request(child, request, addr, data);
772 }
773 #endif
774
775 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
776 {
777         long ret = 0;
778
779         /* Do the secure computing check first. */
780         if (secure_computing(regs->gprs[2])) {
781                 /* seccomp failures shouldn't expose any additional code. */
782                 ret = -1;
783                 goto out;
784         }
785
786         /*
787          * The sysc_tracesys code in entry.S stored the system
788          * call number to gprs[2].
789          */
790         if (test_thread_flag(TIF_SYSCALL_TRACE) &&
791             (tracehook_report_syscall_entry(regs) ||
792              regs->gprs[2] >= NR_syscalls)) {
793                 /*
794                  * Tracing decided this syscall should not happen or the
795                  * debugger stored an invalid system call number. Skip
796                  * the system call and the system call restart handling.
797                  */
798                 clear_thread_flag(TIF_SYSCALL);
799                 ret = -1;
800         }
801
802         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
803                 trace_sys_enter(regs, regs->gprs[2]);
804
805         audit_syscall_entry(is_compat_task() ?
806                                 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
807                             regs->gprs[2], regs->orig_gpr2,
808                             regs->gprs[3], regs->gprs[4],
809                             regs->gprs[5]);
810 out:
811         return ret ?: regs->gprs[2];
812 }
813
814 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
815 {
816         audit_syscall_exit(regs);
817
818         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
819                 trace_sys_exit(regs, regs->gprs[2]);
820
821         if (test_thread_flag(TIF_SYSCALL_TRACE))
822                 tracehook_report_syscall_exit(regs, 0);
823 }
824
825 /*
826  * user_regset definitions.
827  */
828
829 static int s390_regs_get(struct task_struct *target,
830                          const struct user_regset *regset,
831                          unsigned int pos, unsigned int count,
832                          void *kbuf, void __user *ubuf)
833 {
834         if (target == current)
835                 save_access_regs(target->thread.acrs);
836
837         if (kbuf) {
838                 unsigned long *k = kbuf;
839                 while (count > 0) {
840                         *k++ = __peek_user(target, pos);
841                         count -= sizeof(*k);
842                         pos += sizeof(*k);
843                 }
844         } else {
845                 unsigned long __user *u = ubuf;
846                 while (count > 0) {
847                         if (__put_user(__peek_user(target, pos), u++))
848                                 return -EFAULT;
849                         count -= sizeof(*u);
850                         pos += sizeof(*u);
851                 }
852         }
853         return 0;
854 }
855
856 static int s390_regs_set(struct task_struct *target,
857                          const struct user_regset *regset,
858                          unsigned int pos, unsigned int count,
859                          const void *kbuf, const void __user *ubuf)
860 {
861         int rc = 0;
862
863         if (target == current)
864                 save_access_regs(target->thread.acrs);
865
866         if (kbuf) {
867                 const unsigned long *k = kbuf;
868                 while (count > 0 && !rc) {
869                         rc = __poke_user(target, pos, *k++);
870                         count -= sizeof(*k);
871                         pos += sizeof(*k);
872                 }
873         } else {
874                 const unsigned long  __user *u = ubuf;
875                 while (count > 0 && !rc) {
876                         unsigned long word;
877                         rc = __get_user(word, u++);
878                         if (rc)
879                                 break;
880                         rc = __poke_user(target, pos, word);
881                         count -= sizeof(*u);
882                         pos += sizeof(*u);
883                 }
884         }
885
886         if (rc == 0 && target == current)
887                 restore_access_regs(target->thread.acrs);
888
889         return rc;
890 }
891
892 static int s390_fpregs_get(struct task_struct *target,
893                            const struct user_regset *regset, unsigned int pos,
894                            unsigned int count, void *kbuf, void __user *ubuf)
895 {
896         if (target == current) {
897                 save_fp_ctl(&target->thread.fp_regs.fpc);
898                 save_fp_regs(target->thread.fp_regs.fprs);
899         }
900
901         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
902                                    &target->thread.fp_regs, 0, -1);
903 }
904
905 static int s390_fpregs_set(struct task_struct *target,
906                            const struct user_regset *regset, unsigned int pos,
907                            unsigned int count, const void *kbuf,
908                            const void __user *ubuf)
909 {
910         int rc = 0;
911
912         if (target == current) {
913                 save_fp_ctl(&target->thread.fp_regs.fpc);
914                 save_fp_regs(target->thread.fp_regs.fprs);
915         }
916
917         /* If setting FPC, must validate it first. */
918         if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
919                 u32 ufpc[2] = { target->thread.fp_regs.fpc, 0 };
920                 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ufpc,
921                                         0, offsetof(s390_fp_regs, fprs));
922                 if (rc)
923                         return rc;
924                 if (ufpc[1] != 0 || test_fp_ctl(ufpc[0]))
925                         return -EINVAL;
926                 target->thread.fp_regs.fpc = ufpc[0];
927         }
928
929         if (rc == 0 && count > 0)
930                 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
931                                         target->thread.fp_regs.fprs,
932                                         offsetof(s390_fp_regs, fprs), -1);
933
934         if (rc == 0 && target == current) {
935                 restore_fp_ctl(&target->thread.fp_regs.fpc);
936                 restore_fp_regs(target->thread.fp_regs.fprs);
937         }
938
939         return rc;
940 }
941
942 #ifdef CONFIG_64BIT
943
944 static int s390_last_break_get(struct task_struct *target,
945                                const struct user_regset *regset,
946                                unsigned int pos, unsigned int count,
947                                void *kbuf, void __user *ubuf)
948 {
949         if (count > 0) {
950                 if (kbuf) {
951                         unsigned long *k = kbuf;
952                         *k = task_thread_info(target)->last_break;
953                 } else {
954                         unsigned long  __user *u = ubuf;
955                         if (__put_user(task_thread_info(target)->last_break, u))
956                                 return -EFAULT;
957                 }
958         }
959         return 0;
960 }
961
962 static int s390_last_break_set(struct task_struct *target,
963                                const struct user_regset *regset,
964                                unsigned int pos, unsigned int count,
965                                const void *kbuf, const void __user *ubuf)
966 {
967         return 0;
968 }
969
970 static int s390_tdb_get(struct task_struct *target,
971                         const struct user_regset *regset,
972                         unsigned int pos, unsigned int count,
973                         void *kbuf, void __user *ubuf)
974 {
975         struct pt_regs *regs = task_pt_regs(target);
976         unsigned char *data;
977
978         if (!(regs->int_code & 0x200))
979                 return -ENODATA;
980         data = target->thread.trap_tdb;
981         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, data, 0, 256);
982 }
983
984 static int s390_tdb_set(struct task_struct *target,
985                         const struct user_regset *regset,
986                         unsigned int pos, unsigned int count,
987                         const void *kbuf, const void __user *ubuf)
988 {
989         return 0;
990 }
991
992 #endif
993
994 static int s390_system_call_get(struct task_struct *target,
995                                 const struct user_regset *regset,
996                                 unsigned int pos, unsigned int count,
997                                 void *kbuf, void __user *ubuf)
998 {
999         unsigned int *data = &task_thread_info(target)->system_call;
1000         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
1001                                    data, 0, sizeof(unsigned int));
1002 }
1003
1004 static int s390_system_call_set(struct task_struct *target,
1005                                 const struct user_regset *regset,
1006                                 unsigned int pos, unsigned int count,
1007                                 const void *kbuf, const void __user *ubuf)
1008 {
1009         unsigned int *data = &task_thread_info(target)->system_call;
1010         return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1011                                   data, 0, sizeof(unsigned int));
1012 }
1013
1014 static const struct user_regset s390_regsets[] = {
1015         [REGSET_GENERAL] = {
1016                 .core_note_type = NT_PRSTATUS,
1017                 .n = sizeof(s390_regs) / sizeof(long),
1018                 .size = sizeof(long),
1019                 .align = sizeof(long),
1020                 .get = s390_regs_get,
1021                 .set = s390_regs_set,
1022         },
1023         [REGSET_FP] = {
1024                 .core_note_type = NT_PRFPREG,
1025                 .n = sizeof(s390_fp_regs) / sizeof(long),
1026                 .size = sizeof(long),
1027                 .align = sizeof(long),
1028                 .get = s390_fpregs_get,
1029                 .set = s390_fpregs_set,
1030         },
1031 #ifdef CONFIG_64BIT
1032         [REGSET_LAST_BREAK] = {
1033                 .core_note_type = NT_S390_LAST_BREAK,
1034                 .n = 1,
1035                 .size = sizeof(long),
1036                 .align = sizeof(long),
1037                 .get = s390_last_break_get,
1038                 .set = s390_last_break_set,
1039         },
1040         [REGSET_TDB] = {
1041                 .core_note_type = NT_S390_TDB,
1042                 .n = 1,
1043                 .size = 256,
1044                 .align = 1,
1045                 .get = s390_tdb_get,
1046                 .set = s390_tdb_set,
1047         },
1048 #endif
1049         [REGSET_SYSTEM_CALL] = {
1050                 .core_note_type = NT_S390_SYSTEM_CALL,
1051                 .n = 1,
1052                 .size = sizeof(unsigned int),
1053                 .align = sizeof(unsigned int),
1054                 .get = s390_system_call_get,
1055                 .set = s390_system_call_set,
1056         },
1057 };
1058
1059 static const struct user_regset_view user_s390_view = {
1060         .name = UTS_MACHINE,
1061         .e_machine = EM_S390,
1062         .regsets = s390_regsets,
1063         .n = ARRAY_SIZE(s390_regsets)
1064 };
1065
1066 #ifdef CONFIG_COMPAT
1067 static int s390_compat_regs_get(struct task_struct *target,
1068                                 const struct user_regset *regset,
1069                                 unsigned int pos, unsigned int count,
1070                                 void *kbuf, void __user *ubuf)
1071 {
1072         if (target == current)
1073                 save_access_regs(target->thread.acrs);
1074
1075         if (kbuf) {
1076                 compat_ulong_t *k = kbuf;
1077                 while (count > 0) {
1078                         *k++ = __peek_user_compat(target, pos);
1079                         count -= sizeof(*k);
1080                         pos += sizeof(*k);
1081                 }
1082         } else {
1083                 compat_ulong_t __user *u = ubuf;
1084                 while (count > 0) {
1085                         if (__put_user(__peek_user_compat(target, pos), u++))
1086                                 return -EFAULT;
1087                         count -= sizeof(*u);
1088                         pos += sizeof(*u);
1089                 }
1090         }
1091         return 0;
1092 }
1093
1094 static int s390_compat_regs_set(struct task_struct *target,
1095                                 const struct user_regset *regset,
1096                                 unsigned int pos, unsigned int count,
1097                                 const void *kbuf, const void __user *ubuf)
1098 {
1099         int rc = 0;
1100
1101         if (target == current)
1102                 save_access_regs(target->thread.acrs);
1103
1104         if (kbuf) {
1105                 const compat_ulong_t *k = kbuf;
1106                 while (count > 0 && !rc) {
1107                         rc = __poke_user_compat(target, pos, *k++);
1108                         count -= sizeof(*k);
1109                         pos += sizeof(*k);
1110                 }
1111         } else {
1112                 const compat_ulong_t  __user *u = ubuf;
1113                 while (count > 0 && !rc) {
1114                         compat_ulong_t word;
1115                         rc = __get_user(word, u++);
1116                         if (rc)
1117                                 break;
1118                         rc = __poke_user_compat(target, pos, word);
1119                         count -= sizeof(*u);
1120                         pos += sizeof(*u);
1121                 }
1122         }
1123
1124         if (rc == 0 && target == current)
1125                 restore_access_regs(target->thread.acrs);
1126
1127         return rc;
1128 }
1129
1130 static int s390_compat_regs_high_get(struct task_struct *target,
1131                                      const struct user_regset *regset,
1132                                      unsigned int pos, unsigned int count,
1133                                      void *kbuf, void __user *ubuf)
1134 {
1135         compat_ulong_t *gprs_high;
1136
1137         gprs_high = (compat_ulong_t *)
1138                 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1139         if (kbuf) {
1140                 compat_ulong_t *k = kbuf;
1141                 while (count > 0) {
1142                         *k++ = *gprs_high;
1143                         gprs_high += 2;
1144                         count -= sizeof(*k);
1145                 }
1146         } else {
1147                 compat_ulong_t __user *u = ubuf;
1148                 while (count > 0) {
1149                         if (__put_user(*gprs_high, u++))
1150                                 return -EFAULT;
1151                         gprs_high += 2;
1152                         count -= sizeof(*u);
1153                 }
1154         }
1155         return 0;
1156 }
1157
1158 static int s390_compat_regs_high_set(struct task_struct *target,
1159                                      const struct user_regset *regset,
1160                                      unsigned int pos, unsigned int count,
1161                                      const void *kbuf, const void __user *ubuf)
1162 {
1163         compat_ulong_t *gprs_high;
1164         int rc = 0;
1165
1166         gprs_high = (compat_ulong_t *)
1167                 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1168         if (kbuf) {
1169                 const compat_ulong_t *k = kbuf;
1170                 while (count > 0) {
1171                         *gprs_high = *k++;
1172                         *gprs_high += 2;
1173                         count -= sizeof(*k);
1174                 }
1175         } else {
1176                 const compat_ulong_t  __user *u = ubuf;
1177                 while (count > 0 && !rc) {
1178                         unsigned long word;
1179                         rc = __get_user(word, u++);
1180                         if (rc)
1181                                 break;
1182                         *gprs_high = word;
1183                         *gprs_high += 2;
1184                         count -= sizeof(*u);
1185                 }
1186         }
1187
1188         return rc;
1189 }
1190
1191 static int s390_compat_last_break_get(struct task_struct *target,
1192                                       const struct user_regset *regset,
1193                                       unsigned int pos, unsigned int count,
1194                                       void *kbuf, void __user *ubuf)
1195 {
1196         compat_ulong_t last_break;
1197
1198         if (count > 0) {
1199                 last_break = task_thread_info(target)->last_break;
1200                 if (kbuf) {
1201                         unsigned long *k = kbuf;
1202                         *k = last_break;
1203                 } else {
1204                         unsigned long  __user *u = ubuf;
1205                         if (__put_user(last_break, u))
1206                                 return -EFAULT;
1207                 }
1208         }
1209         return 0;
1210 }
1211
1212 static int s390_compat_last_break_set(struct task_struct *target,
1213                                       const struct user_regset *regset,
1214                                       unsigned int pos, unsigned int count,
1215                                       const void *kbuf, const void __user *ubuf)
1216 {
1217         return 0;
1218 }
1219
1220 static const struct user_regset s390_compat_regsets[] = {
1221         [REGSET_GENERAL] = {
1222                 .core_note_type = NT_PRSTATUS,
1223                 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
1224                 .size = sizeof(compat_long_t),
1225                 .align = sizeof(compat_long_t),
1226                 .get = s390_compat_regs_get,
1227                 .set = s390_compat_regs_set,
1228         },
1229         [REGSET_FP] = {
1230                 .core_note_type = NT_PRFPREG,
1231                 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
1232                 .size = sizeof(compat_long_t),
1233                 .align = sizeof(compat_long_t),
1234                 .get = s390_fpregs_get,
1235                 .set = s390_fpregs_set,
1236         },
1237         [REGSET_LAST_BREAK] = {
1238                 .core_note_type = NT_S390_LAST_BREAK,
1239                 .n = 1,
1240                 .size = sizeof(long),
1241                 .align = sizeof(long),
1242                 .get = s390_compat_last_break_get,
1243                 .set = s390_compat_last_break_set,
1244         },
1245         [REGSET_TDB] = {
1246                 .core_note_type = NT_S390_TDB,
1247                 .n = 1,
1248                 .size = 256,
1249                 .align = 1,
1250                 .get = s390_tdb_get,
1251                 .set = s390_tdb_set,
1252         },
1253         [REGSET_SYSTEM_CALL] = {
1254                 .core_note_type = NT_S390_SYSTEM_CALL,
1255                 .n = 1,
1256                 .size = sizeof(compat_uint_t),
1257                 .align = sizeof(compat_uint_t),
1258                 .get = s390_system_call_get,
1259                 .set = s390_system_call_set,
1260         },
1261         [REGSET_GENERAL_EXTENDED] = {
1262                 .core_note_type = NT_S390_HIGH_GPRS,
1263                 .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
1264                 .size = sizeof(compat_long_t),
1265                 .align = sizeof(compat_long_t),
1266                 .get = s390_compat_regs_high_get,
1267                 .set = s390_compat_regs_high_set,
1268         },
1269 };
1270
1271 static const struct user_regset_view user_s390_compat_view = {
1272         .name = "s390",
1273         .e_machine = EM_S390,
1274         .regsets = s390_compat_regsets,
1275         .n = ARRAY_SIZE(s390_compat_regsets)
1276 };
1277 #endif
1278
1279 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1280 {
1281 #ifdef CONFIG_COMPAT
1282         if (test_tsk_thread_flag(task, TIF_31BIT))
1283                 return &user_s390_compat_view;
1284 #endif
1285         return &user_s390_view;
1286 }
1287
1288 static const char *gpr_names[NUM_GPRS] = {
1289         "r0", "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
1290         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1291 };
1292
1293 unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
1294 {
1295         if (offset >= NUM_GPRS)
1296                 return 0;
1297         return regs->gprs[offset];
1298 }
1299
1300 int regs_query_register_offset(const char *name)
1301 {
1302         unsigned long offset;
1303
1304         if (!name || *name != 'r')
1305                 return -EINVAL;
1306         if (kstrtoul(name + 1, 10, &offset))
1307                 return -EINVAL;
1308         if (offset >= NUM_GPRS)
1309                 return -EINVAL;
1310         return offset;
1311 }
1312
1313 const char *regs_query_register_name(unsigned int offset)
1314 {
1315         if (offset >= NUM_GPRS)
1316                 return NULL;
1317         return gpr_names[offset];
1318 }
1319
1320 static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
1321 {
1322         unsigned long ksp = kernel_stack_pointer(regs);
1323
1324         return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
1325 }
1326
1327 /**
1328  * regs_get_kernel_stack_nth() - get Nth entry of the stack
1329  * @regs:pt_regs which contains kernel stack pointer.
1330  * @n:stack entry number.
1331  *
1332  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
1333  * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
1334  * this returns 0.
1335  */
1336 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
1337 {
1338         unsigned long addr;
1339
1340         addr = kernel_stack_pointer(regs) + n * sizeof(long);
1341         if (!regs_within_kernel_stack(regs, addr))
1342                 return 0;
1343         return *(unsigned long *)addr;
1344 }