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