[S390] use facility list for cpu type safety check
[linux-2.6] / arch / s390 / kernel / ptrace.c
1 /*
2  *  arch/s390/kernel/ptrace.c
3  *
4  *  S390 version
5  *    Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
7  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
8  *
9  *  Based on PowerPC version 
10  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
11  *
12  *  Derived from "arch/m68k/kernel/ptrace.c"
13  *  Copyright (C) 1994 by Hamish Macdonald
14  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
15  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
16  *
17  * Modified by Cort Dougan (cort@cs.nmt.edu) 
18  *
19  *
20  * This file is subject to the terms and conditions of the GNU General
21  * Public License.  See the file README.legal in the main directory of
22  * this archive for more details.
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/mm.h>
28 #include <linux/smp.h>
29 #include <linux/smp_lock.h>
30 #include <linux/errno.h>
31 #include <linux/ptrace.h>
32 #include <linux/user.h>
33 #include <linux/security.h>
34 #include <linux/audit.h>
35 #include <linux/signal.h>
36 #include <linux/elf.h>
37 #include <linux/regset.h>
38 #include <linux/tracehook.h>
39
40 #include <asm/segment.h>
41 #include <asm/page.h>
42 #include <asm/pgtable.h>
43 #include <asm/pgalloc.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
46 #include <asm/unistd.h>
47 #include "entry.h"
48
49 #ifdef CONFIG_COMPAT
50 #include "compat_ptrace.h"
51 #endif
52
53 enum s390_regset {
54         REGSET_GENERAL,
55         REGSET_FP,
56 };
57
58 static void
59 FixPerRegisters(struct task_struct *task)
60 {
61         struct pt_regs *regs;
62         per_struct *per_info;
63
64         regs = task_pt_regs(task);
65         per_info = (per_struct *) &task->thread.per_info;
66         per_info->control_regs.bits.em_instruction_fetch =
67                 per_info->single_step | per_info->instruction_fetch;
68         
69         if (per_info->single_step) {
70                 per_info->control_regs.bits.starting_addr = 0;
71 #ifdef CONFIG_COMPAT
72                 if (test_thread_flag(TIF_31BIT))
73                         per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
74                 else
75 #endif
76                         per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
77         } else {
78                 per_info->control_regs.bits.starting_addr =
79                         per_info->starting_addr;
80                 per_info->control_regs.bits.ending_addr =
81                         per_info->ending_addr;
82         }
83         /*
84          * if any of the control reg tracing bits are on 
85          * we switch on per in the psw
86          */
87         if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
88                 regs->psw.mask |= PSW_MASK_PER;
89         else
90                 regs->psw.mask &= ~PSW_MASK_PER;
91
92         if (per_info->control_regs.bits.em_storage_alteration)
93                 per_info->control_regs.bits.storage_alt_space_ctl = 1;
94         else
95                 per_info->control_regs.bits.storage_alt_space_ctl = 0;
96 }
97
98 void user_enable_single_step(struct task_struct *task)
99 {
100         task->thread.per_info.single_step = 1;
101         FixPerRegisters(task);
102 }
103
104 void user_disable_single_step(struct task_struct *task)
105 {
106         task->thread.per_info.single_step = 0;
107         FixPerRegisters(task);
108 }
109
110 /*
111  * Called by kernel/ptrace.c when detaching..
112  *
113  * Make sure single step bits etc are not set.
114  */
115 void
116 ptrace_disable(struct task_struct *child)
117 {
118         /* make sure the single step bit is not set. */
119         user_disable_single_step(child);
120 }
121
122 #ifndef CONFIG_64BIT
123 # define __ADDR_MASK 3
124 #else
125 # define __ADDR_MASK 7
126 #endif
127
128 /*
129  * Read the word at offset addr from the user area of a process. The
130  * trouble here is that the information is littered over different
131  * locations. The process registers are found on the kernel stack,
132  * the floating point stuff and the trace settings are stored in
133  * the task structure. In addition the different structures in
134  * struct user contain pad bytes that should be read as zeroes.
135  * Lovely...
136  */
137 static unsigned long __peek_user(struct task_struct *child, addr_t addr)
138 {
139         struct user *dummy = NULL;
140         addr_t offset, tmp;
141
142         if (addr < (addr_t) &dummy->regs.acrs) {
143                 /*
144                  * psw and gprs are stored on the stack
145                  */
146                 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
147                 if (addr == (addr_t) &dummy->regs.psw.mask)
148                         /* Remove per bit from user psw. */
149                         tmp &= ~PSW_MASK_PER;
150
151         } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
152                 /*
153                  * access registers are stored in the thread structure
154                  */
155                 offset = addr - (addr_t) &dummy->regs.acrs;
156 #ifdef CONFIG_64BIT
157                 /*
158                  * Very special case: old & broken 64 bit gdb reading
159                  * from acrs[15]. Result is a 64 bit value. Read the
160                  * 32 bit acrs[15] value and shift it by 32. Sick...
161                  */
162                 if (addr == (addr_t) &dummy->regs.acrs[15])
163                         tmp = ((unsigned long) child->thread.acrs[15]) << 32;
164                 else
165 #endif
166                 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
167
168         } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
169                 /*
170                  * orig_gpr2 is stored on the kernel stack
171                  */
172                 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
173
174         } else if (addr < (addr_t) &dummy->regs.fp_regs) {
175                 /*
176                  * prevent reads of padding hole between
177                  * orig_gpr2 and fp_regs on s390.
178                  */
179                 tmp = 0;
180
181         } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
182                 /* 
183                  * floating point regs. are stored in the thread structure
184                  */
185                 offset = addr - (addr_t) &dummy->regs.fp_regs;
186                 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
187                 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
188                         tmp &= (unsigned long) FPC_VALID_MASK
189                                 << (BITS_PER_LONG - 32);
190
191         } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
192                 /*
193                  * per_info is found in the thread structure
194                  */
195                 offset = addr - (addr_t) &dummy->regs.per_info;
196                 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
197
198         } else
199                 tmp = 0;
200
201         return tmp;
202 }
203
204 static int
205 peek_user(struct task_struct *child, addr_t addr, addr_t data)
206 {
207         addr_t tmp, mask;
208
209         /*
210          * Stupid gdb peeks/pokes the access registers in 64 bit with
211          * an alignment of 4. Programmers from hell...
212          */
213         mask = __ADDR_MASK;
214 #ifdef CONFIG_64BIT
215         if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
216             addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
217                 mask = 3;
218 #endif
219         if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
220                 return -EIO;
221
222         tmp = __peek_user(child, addr);
223         return put_user(tmp, (addr_t __user *) data);
224 }
225
226 /*
227  * Write a word to the user area of a process at location addr. This
228  * operation does have an additional problem compared to peek_user.
229  * Stores to the program status word and on the floating point
230  * control register needs to get checked for validity.
231  */
232 static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
233 {
234         struct user *dummy = NULL;
235         addr_t offset;
236
237         if (addr < (addr_t) &dummy->regs.acrs) {
238                 /*
239                  * psw and gprs are stored on the stack
240                  */
241                 if (addr == (addr_t) &dummy->regs.psw.mask &&
242 #ifdef CONFIG_COMPAT
243                     data != PSW_MASK_MERGE(psw_user32_bits, data) &&
244 #endif
245                     data != PSW_MASK_MERGE(psw_user_bits, data))
246                         /* Invalid psw mask. */
247                         return -EINVAL;
248 #ifndef CONFIG_64BIT
249                 if (addr == (addr_t) &dummy->regs.psw.addr)
250                         /* I'd like to reject addresses without the
251                            high order bit but older gdb's rely on it */
252                         data |= PSW_ADDR_AMODE;
253 #endif
254                 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
255
256         } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
257                 /*
258                  * access registers are stored in the thread structure
259                  */
260                 offset = addr - (addr_t) &dummy->regs.acrs;
261 #ifdef CONFIG_64BIT
262                 /*
263                  * Very special case: old & broken 64 bit gdb writing
264                  * to acrs[15] with a 64 bit value. Ignore the lower
265                  * half of the value and write the upper 32 bit to
266                  * acrs[15]. Sick...
267                  */
268                 if (addr == (addr_t) &dummy->regs.acrs[15])
269                         child->thread.acrs[15] = (unsigned int) (data >> 32);
270                 else
271 #endif
272                 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
273
274         } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
275                 /*
276                  * orig_gpr2 is stored on the kernel stack
277                  */
278                 task_pt_regs(child)->orig_gpr2 = data;
279
280         } else if (addr < (addr_t) &dummy->regs.fp_regs) {
281                 /*
282                  * prevent writes of padding hole between
283                  * orig_gpr2 and fp_regs on s390.
284                  */
285                 return 0;
286
287         } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
288                 /*
289                  * floating point regs. are stored in the thread structure
290                  */
291                 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
292                     (data & ~((unsigned long) FPC_VALID_MASK
293                               << (BITS_PER_LONG - 32))) != 0)
294                         return -EINVAL;
295                 offset = addr - (addr_t) &dummy->regs.fp_regs;
296                 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
297
298         } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
299                 /*
300                  * per_info is found in the thread structure 
301                  */
302                 offset = addr - (addr_t) &dummy->regs.per_info;
303                 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
304
305         }
306
307         FixPerRegisters(child);
308         return 0;
309 }
310
311 static int
312 poke_user(struct task_struct *child, addr_t addr, addr_t data)
313 {
314         addr_t mask;
315
316         /*
317          * Stupid gdb peeks/pokes the access registers in 64 bit with
318          * an alignment of 4. Programmers from hell indeed...
319          */
320         mask = __ADDR_MASK;
321 #ifdef CONFIG_64BIT
322         if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
323             addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
324                 mask = 3;
325 #endif
326         if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
327                 return -EIO;
328
329         return __poke_user(child, addr, data);
330 }
331
332 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
333 {
334         ptrace_area parea; 
335         int copied, ret;
336
337         switch (request) {
338         case PTRACE_PEEKTEXT:
339         case PTRACE_PEEKDATA:
340                 /* Remove high order bit from address (only for 31 bit). */
341                 addr &= PSW_ADDR_INSN;
342                 /* read word at location addr. */
343                 return generic_ptrace_peekdata(child, addr, data);
344
345         case PTRACE_PEEKUSR:
346                 /* read the word at location addr in the USER area. */
347                 return peek_user(child, addr, data);
348
349         case PTRACE_POKETEXT:
350         case PTRACE_POKEDATA:
351                 /* Remove high order bit from address (only for 31 bit). */
352                 addr &= PSW_ADDR_INSN;
353                 /* write the word at location addr. */
354                 return generic_ptrace_pokedata(child, addr, data);
355
356         case PTRACE_POKEUSR:
357                 /* write the word at location addr in the USER area */
358                 return poke_user(child, addr, data);
359
360         case PTRACE_PEEKUSR_AREA:
361         case PTRACE_POKEUSR_AREA:
362                 if (copy_from_user(&parea, (void __force __user *) addr,
363                                                         sizeof(parea)))
364                         return -EFAULT;
365                 addr = parea.kernel_addr;
366                 data = parea.process_addr;
367                 copied = 0;
368                 while (copied < parea.len) {
369                         if (request == PTRACE_PEEKUSR_AREA)
370                                 ret = peek_user(child, addr, data);
371                         else {
372                                 addr_t utmp;
373                                 if (get_user(utmp,
374                                              (addr_t __force __user *) data))
375                                         return -EFAULT;
376                                 ret = poke_user(child, addr, utmp);
377                         }
378                         if (ret)
379                                 return ret;
380                         addr += sizeof(unsigned long);
381                         data += sizeof(unsigned long);
382                         copied += sizeof(unsigned long);
383                 }
384                 return 0;
385         }
386         return ptrace_request(child, request, addr, data);
387 }
388
389 #ifdef CONFIG_COMPAT
390 /*
391  * Now the fun part starts... a 31 bit program running in the
392  * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
393  * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
394  * to handle, the difference to the 64 bit versions of the requests
395  * is that the access is done in multiples of 4 byte instead of
396  * 8 bytes (sizeof(unsigned long) on 31/64 bit).
397  * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
398  * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
399  * is a 31 bit program too, the content of struct user can be
400  * emulated. A 31 bit program peeking into the struct user of
401  * a 64 bit program is a no-no.
402  */
403
404 /*
405  * Same as peek_user but for a 31 bit program.
406  */
407 static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
408 {
409         struct user32 *dummy32 = NULL;
410         per_struct32 *dummy_per32 = NULL;
411         addr_t offset;
412         __u32 tmp;
413
414         if (addr < (addr_t) &dummy32->regs.acrs) {
415                 /*
416                  * psw and gprs are stored on the stack
417                  */
418                 if (addr == (addr_t) &dummy32->regs.psw.mask) {
419                         /* Fake a 31 bit psw mask. */
420                         tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
421                         tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
422                 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
423                         /* Fake a 31 bit psw address. */
424                         tmp = (__u32) task_pt_regs(child)->psw.addr |
425                                 PSW32_ADDR_AMODE31;
426                 } else {
427                         /* gpr 0-15 */
428                         tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
429                                          addr*2 + 4);
430                 }
431         } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
432                 /*
433                  * access registers are stored in the thread structure
434                  */
435                 offset = addr - (addr_t) &dummy32->regs.acrs;
436                 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
437
438         } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
439                 /*
440                  * orig_gpr2 is stored on the kernel stack
441                  */
442                 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
443
444         } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
445                 /*
446                  * prevent reads of padding hole between
447                  * orig_gpr2 and fp_regs on s390.
448                  */
449                 tmp = 0;
450
451         } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
452                 /*
453                  * floating point regs. are stored in the thread structure 
454                  */
455                 offset = addr - (addr_t) &dummy32->regs.fp_regs;
456                 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
457
458         } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
459                 /*
460                  * per_info is found in the thread structure
461                  */
462                 offset = addr - (addr_t) &dummy32->regs.per_info;
463                 /* This is magic. See per_struct and per_struct32. */
464                 if ((offset >= (addr_t) &dummy_per32->control_regs &&
465                      offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
466                     (offset >= (addr_t) &dummy_per32->starting_addr &&
467                      offset <= (addr_t) &dummy_per32->ending_addr) ||
468                     offset == (addr_t) &dummy_per32->lowcore.words.address)
469                         offset = offset*2 + 4;
470                 else
471                         offset = offset*2;
472                 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
473
474         } else
475                 tmp = 0;
476
477         return tmp;
478 }
479
480 static int peek_user_compat(struct task_struct *child,
481                             addr_t addr, addr_t data)
482 {
483         __u32 tmp;
484
485         if (!test_thread_flag(TIF_31BIT) ||
486             (addr & 3) || addr > sizeof(struct user) - 3)
487                 return -EIO;
488
489         tmp = __peek_user_compat(child, addr);
490         return put_user(tmp, (__u32 __user *) data);
491 }
492
493 /*
494  * Same as poke_user but for a 31 bit program.
495  */
496 static int __poke_user_compat(struct task_struct *child,
497                               addr_t addr, addr_t data)
498 {
499         struct user32 *dummy32 = NULL;
500         per_struct32 *dummy_per32 = NULL;
501         __u32 tmp = (__u32) data;
502         addr_t offset;
503
504         if (addr < (addr_t) &dummy32->regs.acrs) {
505                 /*
506                  * psw, gprs, acrs and orig_gpr2 are stored on the stack
507                  */
508                 if (addr == (addr_t) &dummy32->regs.psw.mask) {
509                         /* Build a 64 bit psw mask from 31 bit mask. */
510                         if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
511                                 /* Invalid psw mask. */
512                                 return -EINVAL;
513                         task_pt_regs(child)->psw.mask =
514                                 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
515                 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
516                         /* Build a 64 bit psw address from 31 bit address. */
517                         task_pt_regs(child)->psw.addr =
518                                 (__u64) tmp & PSW32_ADDR_INSN;
519                 } else {
520                         /* gpr 0-15 */
521                         *(__u32*)((addr_t) &task_pt_regs(child)->psw
522                                   + addr*2 + 4) = tmp;
523                 }
524         } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
525                 /*
526                  * access registers are stored in the thread structure
527                  */
528                 offset = addr - (addr_t) &dummy32->regs.acrs;
529                 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
530
531         } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
532                 /*
533                  * orig_gpr2 is stored on the kernel stack
534                  */
535                 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
536
537         } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
538                 /*
539                  * prevent writess of padding hole between
540                  * orig_gpr2 and fp_regs on s390.
541                  */
542                 return 0;
543
544         } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
545                 /*
546                  * floating point regs. are stored in the thread structure 
547                  */
548                 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
549                     (tmp & ~FPC_VALID_MASK) != 0)
550                         /* Invalid floating point control. */
551                         return -EINVAL;
552                 offset = addr - (addr_t) &dummy32->regs.fp_regs;
553                 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
554
555         } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
556                 /*
557                  * per_info is found in the thread structure.
558                  */
559                 offset = addr - (addr_t) &dummy32->regs.per_info;
560                 /*
561                  * This is magic. See per_struct and per_struct32.
562                  * By incident the offsets in per_struct are exactly
563                  * twice the offsets in per_struct32 for all fields.
564                  * The 8 byte fields need special handling though,
565                  * because the second half (bytes 4-7) is needed and
566                  * not the first half.
567                  */
568                 if ((offset >= (addr_t) &dummy_per32->control_regs &&
569                      offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
570                     (offset >= (addr_t) &dummy_per32->starting_addr &&
571                      offset <= (addr_t) &dummy_per32->ending_addr) ||
572                     offset == (addr_t) &dummy_per32->lowcore.words.address)
573                         offset = offset*2 + 4;
574                 else
575                         offset = offset*2;
576                 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
577
578         }
579
580         FixPerRegisters(child);
581         return 0;
582 }
583
584 static int poke_user_compat(struct task_struct *child,
585                             addr_t addr, addr_t data)
586 {
587         if (!test_thread_flag(TIF_31BIT) ||
588             (addr & 3) || addr > sizeof(struct user32) - 3)
589                 return -EIO;
590
591         return __poke_user_compat(child, addr, data);
592 }
593
594 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
595                         compat_ulong_t caddr, compat_ulong_t cdata)
596 {
597         unsigned long addr = caddr;
598         unsigned long data = cdata;
599         ptrace_area_emu31 parea; 
600         int copied, ret;
601
602         switch (request) {
603         case PTRACE_PEEKUSR:
604                 /* read the word at location addr in the USER area. */
605                 return peek_user_compat(child, addr, data);
606
607         case PTRACE_POKEUSR:
608                 /* write the word at location addr in the USER area */
609                 return poke_user_compat(child, addr, data);
610
611         case PTRACE_PEEKUSR_AREA:
612         case PTRACE_POKEUSR_AREA:
613                 if (copy_from_user(&parea, (void __force __user *) addr,
614                                                         sizeof(parea)))
615                         return -EFAULT;
616                 addr = parea.kernel_addr;
617                 data = parea.process_addr;
618                 copied = 0;
619                 while (copied < parea.len) {
620                         if (request == PTRACE_PEEKUSR_AREA)
621                                 ret = peek_user_compat(child, addr, data);
622                         else {
623                                 __u32 utmp;
624                                 if (get_user(utmp,
625                                              (__u32 __force __user *) data))
626                                         return -EFAULT;
627                                 ret = poke_user_compat(child, addr, utmp);
628                         }
629                         if (ret)
630                                 return ret;
631                         addr += sizeof(unsigned int);
632                         data += sizeof(unsigned int);
633                         copied += sizeof(unsigned int);
634                 }
635                 return 0;
636         }
637         return compat_ptrace_request(child, request, addr, data);
638 }
639 #endif
640
641 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
642 {
643         long ret;
644
645         /*
646          * The sysc_tracesys code in entry.S stored the system
647          * call number to gprs[2].
648          */
649         ret = regs->gprs[2];
650         if (test_thread_flag(TIF_SYSCALL_TRACE) &&
651             (tracehook_report_syscall_entry(regs) ||
652              regs->gprs[2] >= NR_syscalls)) {
653                 /*
654                  * Tracing decided this syscall should not happen or the
655                  * debugger stored an invalid system call number. Skip
656                  * the system call and the system call restart handling.
657                  */
658                 regs->svcnr = 0;
659                 ret = -1;
660         }
661
662         if (unlikely(current->audit_context))
663                 audit_syscall_entry(test_thread_flag(TIF_31BIT) ?
664                                         AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
665                                     regs->gprs[2], regs->orig_gpr2,
666                                     regs->gprs[3], regs->gprs[4],
667                                     regs->gprs[5]);
668         return ret;
669 }
670
671 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
672 {
673         if (unlikely(current->audit_context))
674                 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
675                                    regs->gprs[2]);
676
677         if (test_thread_flag(TIF_SYSCALL_TRACE))
678                 tracehook_report_syscall_exit(regs, 0);
679 }
680
681 /*
682  * user_regset definitions.
683  */
684
685 static int s390_regs_get(struct task_struct *target,
686                          const struct user_regset *regset,
687                          unsigned int pos, unsigned int count,
688                          void *kbuf, void __user *ubuf)
689 {
690         if (target == current)
691                 save_access_regs(target->thread.acrs);
692
693         if (kbuf) {
694                 unsigned long *k = kbuf;
695                 while (count > 0) {
696                         *k++ = __peek_user(target, pos);
697                         count -= sizeof(*k);
698                         pos += sizeof(*k);
699                 }
700         } else {
701                 unsigned long __user *u = ubuf;
702                 while (count > 0) {
703                         if (__put_user(__peek_user(target, pos), u++))
704                                 return -EFAULT;
705                         count -= sizeof(*u);
706                         pos += sizeof(*u);
707                 }
708         }
709         return 0;
710 }
711
712 static int s390_regs_set(struct task_struct *target,
713                          const struct user_regset *regset,
714                          unsigned int pos, unsigned int count,
715                          const void *kbuf, const void __user *ubuf)
716 {
717         int rc = 0;
718
719         if (target == current)
720                 save_access_regs(target->thread.acrs);
721
722         if (kbuf) {
723                 const unsigned long *k = kbuf;
724                 while (count > 0 && !rc) {
725                         rc = __poke_user(target, pos, *k++);
726                         count -= sizeof(*k);
727                         pos += sizeof(*k);
728                 }
729         } else {
730                 const unsigned long  __user *u = ubuf;
731                 while (count > 0 && !rc) {
732                         unsigned long word;
733                         rc = __get_user(word, u++);
734                         if (rc)
735                                 break;
736                         rc = __poke_user(target, pos, word);
737                         count -= sizeof(*u);
738                         pos += sizeof(*u);
739                 }
740         }
741
742         if (rc == 0 && target == current)
743                 restore_access_regs(target->thread.acrs);
744
745         return rc;
746 }
747
748 static int s390_fpregs_get(struct task_struct *target,
749                            const struct user_regset *regset, unsigned int pos,
750                            unsigned int count, void *kbuf, void __user *ubuf)
751 {
752         if (target == current)
753                 save_fp_regs(&target->thread.fp_regs);
754
755         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
756                                    &target->thread.fp_regs, 0, -1);
757 }
758
759 static int s390_fpregs_set(struct task_struct *target,
760                            const struct user_regset *regset, unsigned int pos,
761                            unsigned int count, const void *kbuf,
762                            const void __user *ubuf)
763 {
764         int rc = 0;
765
766         if (target == current)
767                 save_fp_regs(&target->thread.fp_regs);
768
769         /* If setting FPC, must validate it first. */
770         if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
771                 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
772                 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
773                                         0, offsetof(s390_fp_regs, fprs));
774                 if (rc)
775                         return rc;
776                 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
777                         return -EINVAL;
778                 target->thread.fp_regs.fpc = fpc[0];
779         }
780
781         if (rc == 0 && count > 0)
782                 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
783                                         target->thread.fp_regs.fprs,
784                                         offsetof(s390_fp_regs, fprs), -1);
785
786         if (rc == 0 && target == current)
787                 restore_fp_regs(&target->thread.fp_regs);
788
789         return rc;
790 }
791
792 static const struct user_regset s390_regsets[] = {
793         [REGSET_GENERAL] = {
794                 .core_note_type = NT_PRSTATUS,
795                 .n = sizeof(s390_regs) / sizeof(long),
796                 .size = sizeof(long),
797                 .align = sizeof(long),
798                 .get = s390_regs_get,
799                 .set = s390_regs_set,
800         },
801         [REGSET_FP] = {
802                 .core_note_type = NT_PRFPREG,
803                 .n = sizeof(s390_fp_regs) / sizeof(long),
804                 .size = sizeof(long),
805                 .align = sizeof(long),
806                 .get = s390_fpregs_get,
807                 .set = s390_fpregs_set,
808         },
809 };
810
811 static const struct user_regset_view user_s390_view = {
812         .name = UTS_MACHINE,
813         .e_machine = EM_S390,
814         .regsets = s390_regsets,
815         .n = ARRAY_SIZE(s390_regsets)
816 };
817
818 #ifdef CONFIG_COMPAT
819 static int s390_compat_regs_get(struct task_struct *target,
820                                 const struct user_regset *regset,
821                                 unsigned int pos, unsigned int count,
822                                 void *kbuf, void __user *ubuf)
823 {
824         if (target == current)
825                 save_access_regs(target->thread.acrs);
826
827         if (kbuf) {
828                 compat_ulong_t *k = kbuf;
829                 while (count > 0) {
830                         *k++ = __peek_user_compat(target, pos);
831                         count -= sizeof(*k);
832                         pos += sizeof(*k);
833                 }
834         } else {
835                 compat_ulong_t __user *u = ubuf;
836                 while (count > 0) {
837                         if (__put_user(__peek_user_compat(target, pos), u++))
838                                 return -EFAULT;
839                         count -= sizeof(*u);
840                         pos += sizeof(*u);
841                 }
842         }
843         return 0;
844 }
845
846 static int s390_compat_regs_set(struct task_struct *target,
847                                 const struct user_regset *regset,
848                                 unsigned int pos, unsigned int count,
849                                 const void *kbuf, const void __user *ubuf)
850 {
851         int rc = 0;
852
853         if (target == current)
854                 save_access_regs(target->thread.acrs);
855
856         if (kbuf) {
857                 const compat_ulong_t *k = kbuf;
858                 while (count > 0 && !rc) {
859                         rc = __poke_user_compat(target, pos, *k++);
860                         count -= sizeof(*k);
861                         pos += sizeof(*k);
862                 }
863         } else {
864                 const compat_ulong_t  __user *u = ubuf;
865                 while (count > 0 && !rc) {
866                         compat_ulong_t word;
867                         rc = __get_user(word, u++);
868                         if (rc)
869                                 break;
870                         rc = __poke_user_compat(target, pos, word);
871                         count -= sizeof(*u);
872                         pos += sizeof(*u);
873                 }
874         }
875
876         if (rc == 0 && target == current)
877                 restore_access_regs(target->thread.acrs);
878
879         return rc;
880 }
881
882 static const struct user_regset s390_compat_regsets[] = {
883         [REGSET_GENERAL] = {
884                 .core_note_type = NT_PRSTATUS,
885                 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
886                 .size = sizeof(compat_long_t),
887                 .align = sizeof(compat_long_t),
888                 .get = s390_compat_regs_get,
889                 .set = s390_compat_regs_set,
890         },
891         [REGSET_FP] = {
892                 .core_note_type = NT_PRFPREG,
893                 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
894                 .size = sizeof(compat_long_t),
895                 .align = sizeof(compat_long_t),
896                 .get = s390_fpregs_get,
897                 .set = s390_fpregs_set,
898         },
899 };
900
901 static const struct user_regset_view user_s390_compat_view = {
902         .name = "s390",
903         .e_machine = EM_S390,
904         .regsets = s390_compat_regsets,
905         .n = ARRAY_SIZE(s390_compat_regsets)
906 };
907 #endif
908
909 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
910 {
911 #ifdef CONFIG_COMPAT
912         if (test_tsk_thread_flag(task, TIF_31BIT))
913                 return &user_s390_compat_view;
914 #endif
915         return &user_s390_view;
916 }