patch-2.6.6-vs1.9.0
[linux-2.6.git] / 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
35 #include <asm/segment.h>
36 #include <asm/page.h>
37 #include <asm/pgtable.h>
38 #include <asm/pgalloc.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41
42 #ifdef CONFIG_S390_SUPPORT
43 #include "compat_ptrace.h"
44 #endif
45
46 static void
47 FixPerRegisters(struct task_struct *task)
48 {
49         struct pt_regs *regs;
50         per_struct *per_info;
51
52         regs = __KSTK_PTREGS(task);
53         per_info = (per_struct *) &task->thread.per_info;
54         per_info->control_regs.bits.em_instruction_fetch =
55                 per_info->single_step | per_info->instruction_fetch;
56         
57         if (per_info->single_step) {
58                 per_info->control_regs.bits.starting_addr = 0;
59 #ifdef CONFIG_S390_SUPPORT
60                 if (test_thread_flag(TIF_31BIT))
61                         per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
62                 else
63 #endif
64                         per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
65         } else {
66                 per_info->control_regs.bits.starting_addr =
67                         per_info->starting_addr;
68                 per_info->control_regs.bits.ending_addr =
69                         per_info->ending_addr;
70         }
71         /*
72          * if any of the control reg tracing bits are on 
73          * we switch on per in the psw
74          */
75         if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
76                 regs->psw.mask |= PSW_MASK_PER;
77         else
78                 regs->psw.mask &= ~PSW_MASK_PER;
79
80         if (per_info->control_regs.bits.em_storage_alteration)
81                 per_info->control_regs.bits.storage_alt_space_ctl = 1;
82         else
83                 per_info->control_regs.bits.storage_alt_space_ctl = 0;
84 }
85
86 void
87 set_single_step(struct task_struct *task)
88 {
89         task->thread.per_info.single_step = 1;
90         FixPerRegisters(task);
91 }
92
93 void
94 clear_single_step(struct task_struct *task)
95 {
96         task->thread.per_info.single_step = 0;
97         FixPerRegisters(task);
98 }
99
100 /*
101  * Called by kernel/ptrace.c when detaching..
102  *
103  * Make sure single step bits etc are not set.
104  */
105 void
106 ptrace_disable(struct task_struct *child)
107 {
108         /* make sure the single step bit is not set. */
109         clear_single_step(child);
110 }
111
112 #ifndef CONFIG_ARCH_S390X
113 # define __ADDR_MASK 3
114 #else
115 # define __ADDR_MASK 7
116 #endif
117
118 /*
119  * Read the word at offset addr from the user area of a process. The
120  * trouble here is that the information is littered over different
121  * locations. The process registers are found on the kernel stack,
122  * the floating point stuff and the trace settings are stored in
123  * the task structure. In addition the different structures in
124  * struct user contain pad bytes that should be read as zeroes.
125  * Lovely...
126  */
127 static int
128 peek_user(struct task_struct *child, addr_t addr, addr_t data)
129 {
130         struct user *dummy = NULL;
131         addr_t offset, tmp;
132
133         /*
134          * Stupid gdb peeks/pokes the access registers in 64 bit with
135          * an alignment of 4. Programmers from hell...
136          */
137         if ((addr & 3) || addr > sizeof(struct user) - __ADDR_MASK)
138                 return -EIO;
139
140         if (addr < (addr_t) &dummy->regs.acrs) {
141                 /*
142                  * psw and gprs are stored on the stack
143                  */
144                 tmp = *(addr_t *)((addr_t) __KSTK_PTREGS(child) + addr);
145                 if (addr == (addr_t) &dummy->regs.psw.mask)
146                         /* Remove per bit from user psw. */
147                         tmp &= ~PSW_MASK_PER;
148
149         } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
150                 /*
151                  * access registers are stored in the thread structure
152                  */
153                 offset = addr - (addr_t) &dummy->regs.acrs;
154                 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
155
156         } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
157                 /*
158                  * orig_gpr2 is stored on the kernel stack
159                  */
160                 tmp = (addr_t) __KSTK_PTREGS(child)->orig_gpr2;
161
162         } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
163                 /* 
164                  * floating point regs. are stored in the thread structure
165                  */
166                 offset = addr - (addr_t) &dummy->regs.fp_regs;
167                 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
168
169         } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
170                 /*
171                  * per_info is found in the thread structure
172                  */
173                 offset = addr - (addr_t) &dummy->regs.per_info;
174                 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
175
176         } else
177                 tmp = 0;
178
179         return put_user(tmp, (addr_t *) data);
180 }
181
182 /*
183  * Write a word to the user area of a process at location addr. This
184  * operation does have an additional problem compared to peek_user.
185  * Stores to the program status word and on the floating point
186  * control register needs to get checked for validity.
187  */
188 static int
189 poke_user(struct task_struct *child, addr_t addr, addr_t data)
190 {
191         struct user *dummy = NULL;
192         addr_t offset;
193
194         /*
195          * Stupid gdb peeks/pokes the access registers in 64 bit with
196          * an alignment of 4. Programmers from hell indeed...
197          */
198         if ((addr & 3) || addr > sizeof(struct user) - __ADDR_MASK)
199                 return -EIO;
200
201         if (addr < (addr_t) &dummy->regs.acrs) {
202                 /*
203                  * psw and gprs are stored on the stack
204                  */
205                 if (addr == (addr_t) &dummy->regs.psw.mask &&
206 #ifdef CONFIG_S390_SUPPORT
207                     data != PSW_MASK_MERGE(PSW_USER32_BITS, data) &&
208 #endif
209                     data != PSW_MASK_MERGE(PSW_USER_BITS, data))
210                         /* Invalid psw mask. */
211                         return -EINVAL;
212 #ifndef CONFIG_ARCH_S390X
213                 if (addr == (addr_t) &dummy->regs.psw.addr)
214                         /* I'd like to reject addresses without the
215                            high order bit but older gdb's rely on it */
216                         data |= PSW_ADDR_AMODE;
217 #endif
218                 *(addr_t *)((addr_t) __KSTK_PTREGS(child) + addr) = data;
219
220         } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
221                 /*
222                  * access registers are stored in the thread structure
223                  */
224                 offset = addr - (addr_t) &dummy->regs.acrs;
225                 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
226
227         } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
228                 /*
229                  * orig_gpr2 is stored on the kernel stack
230                  */
231                 __KSTK_PTREGS(child)->orig_gpr2 = data;
232
233         } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
234                 /*
235                  * floating point regs. are stored in the thread structure
236                  */
237                 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
238                     (data & ~FPC_VALID_MASK) != 0)
239                         return -EINVAL;
240                 offset = addr - (addr_t) &dummy->regs.fp_regs;
241                 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
242
243         } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
244                 /*
245                  * per_info is found in the thread structure 
246                  */
247                 offset = addr - (addr_t) &dummy->regs.per_info;
248                 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
249
250         }
251
252         FixPerRegisters(child);
253         return 0;
254 }
255
256 static int
257 do_ptrace_normal(struct task_struct *child, long request, long addr, long data)
258 {
259         unsigned long tmp;
260         ptrace_area parea; 
261         int copied, ret;
262
263         switch (request) {
264         case PTRACE_PEEKTEXT:
265         case PTRACE_PEEKDATA:
266                 /* Remove high order bit from address (only for 31 bit). */
267                 addr &= PSW_ADDR_INSN;
268                 /* read word at location addr. */
269                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
270                 if (copied != sizeof(tmp))
271                         return -EIO;
272                 return put_user(tmp, (unsigned long *) data);
273
274         case PTRACE_PEEKUSR:
275                 /* read the word at location addr in the USER area. */
276                 return peek_user(child, addr, data);
277
278         case PTRACE_POKETEXT:
279         case PTRACE_POKEDATA:
280                 /* Remove high order bit from address (only for 31 bit). */
281                 addr &= PSW_ADDR_INSN;
282                 /* write the word at location addr. */
283                 copied = access_process_vm(child, addr, &data, sizeof(data),1);
284                 if (copied != sizeof(data))
285                         return -EIO;
286                 return 0;
287
288         case PTRACE_POKEUSR:
289                 /* write the word at location addr in the USER area */
290                 return poke_user(child, addr, data);
291
292         case PTRACE_PEEKUSR_AREA:
293         case PTRACE_POKEUSR_AREA:
294                 if (copy_from_user(&parea, (void *) addr, sizeof(parea)))
295                         return -EFAULT;
296                 addr = parea.kernel_addr;
297                 data = parea.process_addr;
298                 copied = 0;
299                 while (copied < parea.len) {
300                         if (request == PTRACE_PEEKUSR_AREA)
301                                 ret = peek_user(child, addr, data);
302                         else {
303                                 addr_t tmp;
304                                 if (get_user (tmp, (addr_t *) data))
305                                         return -EFAULT;
306                                 ret = poke_user(child, addr, tmp);
307                         }
308                         if (ret)
309                                 return ret;
310                         addr += sizeof(unsigned long);
311                         data += sizeof(unsigned long);
312                         copied += sizeof(unsigned long);
313                 }
314                 return 0;
315         }
316         return ptrace_request(child, request, addr, data);
317 }
318
319 #ifdef CONFIG_S390_SUPPORT
320 /*
321  * Now the fun part starts... a 31 bit program running in the
322  * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
323  * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
324  * to handle, the difference to the 64 bit versions of the requests
325  * is that the access is done in multiples of 4 byte instead of
326  * 8 bytes (sizeof(unsigned long) on 31/64 bit).
327  * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
328  * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
329  * is a 31 bit program too, the content of struct user can be
330  * emulated. A 31 bit program peeking into the struct user of
331  * a 64 bit program is a no-no.
332  */
333
334 /*
335  * Same as peek_user but for a 31 bit program.
336  */
337 static int
338 peek_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
339 {
340         struct user32 *dummy32 = NULL;
341         per_struct32 *dummy_per32 = NULL;
342         addr_t offset;
343         __u32 tmp;
344
345         if (!test_thread_flag(TIF_31BIT) ||
346             (addr & 3) || addr > sizeof(struct user) - 3)
347                 return -EIO;
348
349         if (addr < (addr_t) &dummy32->regs.acrs) {
350                 /*
351                  * psw and gprs are stored on the stack
352                  */
353                 if (addr == (addr_t) &dummy32->regs.psw.mask) {
354                         /* Fake a 31 bit psw mask. */
355                         tmp = (__u32)(__KSTK_PTREGS(child)->psw.mask >> 32);
356                         tmp = PSW32_MASK_MERGE(PSW32_USER_BITS, tmp);
357                 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
358                         /* Fake a 31 bit psw address. */
359                         tmp = (__u32) __KSTK_PTREGS(child)->psw.addr |
360                                 PSW32_ADDR_AMODE31;
361                 } else {
362                         /* gpr 0-15 */
363                         tmp = *(__u32 *)((addr_t) __KSTK_PTREGS(child) + 
364                                          addr*2 + 4);
365                 }
366         } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
367                 /*
368                  * access registers are stored in the thread structure
369                  */
370                 offset = addr - (addr_t) &dummy32->regs.acrs;
371                 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
372
373         } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
374                 /*
375                  * orig_gpr2 is stored on the kernel stack
376                  */
377                 tmp = *(__u32*)((addr_t) &__KSTK_PTREGS(child)->orig_gpr2 + 4);
378
379         } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
380                 /*
381                  * floating point regs. are stored in the thread structure 
382                  */
383                 offset = addr - (addr_t) &dummy32->regs.fp_regs;
384                 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
385
386         } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
387                 /*
388                  * per_info is found in the thread structure
389                  */
390                 offset = addr - (addr_t) &dummy32->regs.per_info;
391                 /* This is magic. See per_struct and per_struct32. */
392                 if ((offset >= (addr_t) &dummy_per32->control_regs &&
393                      offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
394                     (offset >= (addr_t) &dummy_per32->starting_addr &&
395                      offset <= (addr_t) &dummy_per32->ending_addr) ||
396                     offset == (addr_t) &dummy_per32->lowcore.words.address)
397                         offset = offset*2 + 4;
398                 else
399                         offset = offset*2;
400                 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
401
402         } else
403                 tmp = 0;
404
405         return put_user(tmp, (__u32 *) data);
406 }
407
408 /*
409  * Same as poke_user but for a 31 bit program.
410  */
411 static int
412 poke_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
413 {
414         struct user32 *dummy32 = NULL;
415         per_struct32 *dummy_per32 = NULL;
416         addr_t offset;
417         __u32 tmp;
418
419         if (!test_thread_flag(TIF_31BIT) ||
420             (addr & 3) || addr > sizeof(struct user32) - 3)
421                 return -EIO;
422
423         tmp = (__u32) data;
424
425         if (addr < (addr_t) &dummy32->regs.acrs) {
426                 /*
427                  * psw, gprs, acrs and orig_gpr2 are stored on the stack
428                  */
429                 if (addr == (addr_t) &dummy32->regs.psw.mask) {
430                         /* Build a 64 bit psw mask from 31 bit mask. */
431                         if (tmp != PSW32_MASK_MERGE(PSW32_USER_BITS, tmp))
432                                 /* Invalid psw mask. */
433                                 return -EINVAL;
434                         __KSTK_PTREGS(child)->psw.mask =
435                                 PSW_MASK_MERGE(PSW_USER32_BITS, (__u64) tmp << 32);
436                 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
437                         /* Build a 64 bit psw address from 31 bit address. */
438                         __KSTK_PTREGS(child)->psw.addr = 
439                                 (__u64) tmp & PSW32_ADDR_INSN;
440                 } else {
441                         /* gpr 0-15 */
442                         *(__u32*)((addr_t) __KSTK_PTREGS(child) + addr*2 + 4) =
443                                 tmp;
444                 }
445         } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
446                 /*
447                  * access registers are stored in the thread structure
448                  */
449                 offset = addr - (addr_t) &dummy32->regs.acrs;
450                 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
451
452         } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
453                 /*
454                  * orig_gpr2 is stored on the kernel stack
455                  */
456                 *(__u32*)((addr_t) &__KSTK_PTREGS(child)->orig_gpr2 + 4) = tmp;
457
458         } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
459                 /*
460                  * floating point regs. are stored in the thread structure 
461                  */
462                 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
463                     (tmp & ~FPC_VALID_MASK) != 0)
464                         /* Invalid floating point control. */
465                         return -EINVAL;
466                 offset = addr - (addr_t) &dummy32->regs.fp_regs;
467                 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
468
469         } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
470                 /*
471                  * per_info is found in the thread structure.
472                  */
473                 offset = addr - (addr_t) &dummy32->regs.per_info;
474                 /*
475                  * This is magic. See per_struct and per_struct32.
476                  * By incident the offsets in per_struct are exactly
477                  * twice the offsets in per_struct32 for all fields.
478                  * The 8 byte fields need special handling though,
479                  * because the second half (bytes 4-7) is needed and
480                  * not the first half.
481                  */
482                 if ((offset >= (addr_t) &dummy_per32->control_regs &&
483                      offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
484                     (offset >= (addr_t) &dummy_per32->starting_addr &&
485                      offset <= (addr_t) &dummy_per32->ending_addr) ||
486                     offset == (addr_t) &dummy_per32->lowcore.words.address)
487                         offset = offset*2 + 4;
488                 else
489                         offset = offset*2;
490                 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
491
492         }
493
494         FixPerRegisters(child);
495         return 0;
496 }
497
498 static int
499 do_ptrace_emu31(struct task_struct *child, long request, long addr, long data)
500 {
501         unsigned int tmp;  /* 4 bytes !! */
502         ptrace_area_emu31 parea; 
503         int copied, ret;
504
505         switch (request) {
506         case PTRACE_PEEKTEXT:
507         case PTRACE_PEEKDATA:
508                 /* read word at location addr. */
509                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
510                 if (copied != sizeof(tmp))
511                         return -EIO;
512                 return put_user(tmp, (unsigned int *) data);
513
514         case PTRACE_PEEKUSR:
515                 /* read the word at location addr in the USER area. */
516                 return peek_user_emu31(child, addr, data);
517
518         case PTRACE_POKETEXT:
519         case PTRACE_POKEDATA:
520                 /* write the word at location addr. */
521                 tmp = data;
522                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
523                 if (copied != sizeof(tmp))
524                         return -EIO;
525                 return 0;
526
527         case PTRACE_POKEUSR:
528                 /* write the word at location addr in the USER area */
529                 return poke_user_emu31(child, addr, data);
530
531         case PTRACE_PEEKUSR_AREA:
532         case PTRACE_POKEUSR_AREA:
533                 if (copy_from_user(&parea, (void *) addr, sizeof(parea)))
534                         return -EFAULT;
535                 addr = parea.kernel_addr;
536                 data = parea.process_addr;
537                 copied = 0;
538                 while (copied < parea.len) {
539                         if (request == PTRACE_PEEKUSR_AREA)
540                                 ret = peek_user_emu31(child, addr, data);
541                         else {
542                                 __u32 tmp;
543                                 if (get_user (tmp, (__u32 *) data))
544                                         return -EFAULT;
545                                 ret = poke_user_emu31(child, addr, tmp);
546                         }
547                         if (ret)
548                                 return ret;
549                         addr += sizeof(unsigned int);
550                         data += sizeof(unsigned int);
551                         copied += sizeof(unsigned int);
552                 }
553                 return 0;
554         }
555         return ptrace_request(child, request, addr, data);
556 }
557 #endif
558
559 #define PT32_IEEE_IP 0x13c
560
561 static int
562 do_ptrace(struct task_struct *child, long request, long addr, long data)
563 {
564         int ret;
565
566         if (request == PTRACE_ATTACH)
567                 return ptrace_attach(child);
568
569         /*
570          * Special cases to get/store the ieee instructions pointer.
571          */
572         if (child == current) {
573                 if (request == PTRACE_PEEKUSR && addr == PT_IEEE_IP)
574                         return peek_user(child, addr, data);
575                 if (request == PTRACE_POKEUSR && addr == PT_IEEE_IP)
576                         return poke_user(child, addr, data);
577 #ifdef CONFIG_S390_SUPPORT
578                 if (request == PTRACE_PEEKUSR &&
579                     addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
580                         return peek_user_emu31(child, addr, data);
581                 if (request == PTRACE_POKEUSR &&
582                     addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
583                         return poke_user_emu31(child, addr, data);
584 #endif
585         }
586
587         ret = ptrace_check_attach(child, request == PTRACE_KILL);
588         if (ret < 0)
589                 return ret;
590
591         switch (request) {
592         case PTRACE_SYSCALL:
593                 /* continue and stop at next (return from) syscall */
594         case PTRACE_CONT:
595                 /* restart after signal. */
596                 if ((unsigned long) data >= _NSIG)
597                         return -EIO;
598                 if (request == PTRACE_SYSCALL)
599                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
600                 else
601                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
602                 child->exit_code = data;
603                 /* make sure the single step bit is not set. */
604                 clear_single_step(child);
605                 wake_up_process(child);
606                 return 0;
607
608         case PTRACE_KILL:
609                 /*
610                  * make the child exit.  Best I can do is send it a sigkill. 
611                  * perhaps it should be put in the status that it wants to 
612                  * exit.
613                  */
614                 if (child->state == TASK_ZOMBIE) /* already dead */
615                         return 0;
616                 child->exit_code = SIGKILL;
617                 /* make sure the single step bit is not set. */
618                 clear_single_step(child);
619                 wake_up_process(child);
620                 return 0;
621
622         case PTRACE_SINGLESTEP:
623                 /* set the trap flag. */
624                 if ((unsigned long) data >= _NSIG)
625                         return -EIO;
626                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
627                 child->exit_code = data;
628                 set_single_step(child);
629                 /* give it a chance to run. */
630                 wake_up_process(child);
631                 return 0;
632
633         case PTRACE_DETACH:
634                 /* detach a process that was attached. */
635                 return ptrace_detach(child, data);
636
637
638         /* Do requests that differ for 31/64 bit */
639         default:
640 #ifdef CONFIG_S390_SUPPORT
641                 if (test_thread_flag(TIF_31BIT))
642                         return do_ptrace_emu31(child, request, addr, data);
643 #endif
644                 return do_ptrace_normal(child, request, addr, data);
645         }
646         /* Not reached.  */
647         return -EIO;
648 }
649
650 asmlinkage long
651 sys_ptrace(long request, long pid, long addr, long data)
652 {
653         struct task_struct *child;
654         int ret;
655
656         lock_kernel();
657
658         if (request == PTRACE_TRACEME) {
659                 /* are we already being traced? */
660                 ret = -EPERM;
661                 if (current->ptrace & PT_PTRACED)
662                         goto out;
663                 ret = security_ptrace(current->parent, current);
664                 if (ret)
665                         goto out;
666                 /* set the ptrace bit in the process flags. */
667                 current->ptrace |= PT_PTRACED;
668                 goto out;
669         }
670
671         ret = -EPERM;
672         if (pid == 1)           /* you may not mess with init */
673                 goto out;
674
675         ret = -ESRCH;
676         read_lock(&tasklist_lock);
677         child = find_task_by_pid(pid);
678         if (child)
679                 get_task_struct(child);
680         read_unlock(&tasklist_lock);
681         if (!child)
682                 goto out;
683         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
684                 goto out_tsk;
685
686         ret = do_ptrace(child, request, addr, data);
687 out_tsk:
688         put_task_struct(child);
689 out:
690         unlock_kernel();
691         return ret;
692 }
693
694 asmlinkage void
695 syscall_trace(struct pt_regs *regs, int entryexit)
696 {
697         if (unlikely(current->audit_context)) {
698                 if (!entryexit)
699                         audit_syscall_entry(current, regs->gprs[2],
700                                             regs->orig_gpr2, regs->gprs[3],
701                                             regs->gprs[4], regs->gprs[5]);
702                 else
703                         audit_syscall_exit(current, regs->gprs[2]);
704         }
705         if (!test_thread_flag(TIF_SYSCALL_TRACE))
706                 return;
707         if (!(current->ptrace & PT_PTRACED))
708                 return;
709         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
710                                  ? 0x80 : 0));
711
712         /*
713          * this isn't the same as continuing with a signal, but it will do
714          * for normal use.  strace only continues with a signal if the
715          * stopping signal is not SIGTRAP.  -brl
716          */
717         if (current->exit_code) {
718                 send_sig(current->exit_code, current, 1);
719                 current->exit_code = 0;
720         }
721 }