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