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