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