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