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