2 * arch/s390/kernel/ptrace.c
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)
9 * Based on PowerPC version
10 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
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
17 * Modified by Cort Dougan (cort@cs.nmt.edu)
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.
25 #include <linux/kernel.h>
26 #include <linux/sched.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>
35 #include <asm/segment.h>
37 #include <asm/pgtable.h>
38 #include <asm/pgalloc.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
42 #ifdef CONFIG_S390_SUPPORT
43 #include "compat_ptrace.h"
47 FixPerRegisters(struct task_struct *task)
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;
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;
64 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
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;
72 * if any of the control reg tracing bits are on
73 * we switch on per in the psw
75 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
76 regs->psw.mask |= PSW_MASK_PER;
78 regs->psw.mask &= ~PSW_MASK_PER;
80 if (per_info->control_regs.bits.em_storage_alteration)
81 per_info->control_regs.bits.storage_alt_space_ctl = 1;
83 per_info->control_regs.bits.storage_alt_space_ctl = 0;
87 set_single_step(struct task_struct *task)
89 task->thread.per_info.single_step = 1;
90 FixPerRegisters(task);
94 clear_single_step(struct task_struct *task)
96 task->thread.per_info.single_step = 0;
97 FixPerRegisters(task);
101 * Called by kernel/ptrace.c when detaching..
103 * Make sure single step bits etc are not set.
106 ptrace_disable(struct task_struct *child)
108 /* make sure the single step bit is not set. */
109 clear_single_step(child);
112 #ifndef CONFIG_ARCH_S390X
113 # define __ADDR_MASK 3
115 # define __ADDR_MASK 7
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.
128 peek_user(struct task_struct *child, addr_t addr, addr_t data)
130 struct user *dummy = NULL;
134 * Stupid gdb peeks/pokes the access registers in 64 bit with
135 * an alignment of 4. Programmers from hell...
137 if ((addr & 3) || addr > sizeof(struct user) - __ADDR_MASK)
140 if (addr < (addr_t) &dummy->regs.acrs) {
142 * psw and gprs are stored on the stack
144 tmp = *(addr_t *)((addr_t) &__KSTK_PTREGS(child)->psw + addr);
145 if (addr == (addr_t) &dummy->regs.psw.mask)
146 /* Remove per bit from user psw. */
147 tmp &= ~PSW_MASK_PER;
149 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
151 * access registers are stored in the thread structure
153 offset = addr - (addr_t) &dummy->regs.acrs;
154 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
156 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
158 * orig_gpr2 is stored on the kernel stack
160 tmp = (addr_t) __KSTK_PTREGS(child)->orig_gpr2;
162 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
164 * floating point regs. are stored in the thread structure
166 offset = addr - (addr_t) &dummy->regs.fp_regs;
167 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
169 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
171 * per_info is found in the thread structure
173 offset = addr - (addr_t) &dummy->regs.per_info;
174 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
179 return put_user(tmp, (addr_t __user *) data);
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.
189 poke_user(struct task_struct *child, addr_t addr, addr_t data)
191 struct user *dummy = NULL;
195 * Stupid gdb peeks/pokes the access registers in 64 bit with
196 * an alignment of 4. Programmers from hell indeed...
198 if ((addr & 3) || addr > sizeof(struct user) - __ADDR_MASK)
201 if (addr < (addr_t) &dummy->regs.acrs) {
203 * psw and gprs are stored on the stack
205 if (addr == (addr_t) &dummy->regs.psw.mask &&
206 #ifdef CONFIG_S390_SUPPORT
207 data != PSW_MASK_MERGE(PSW_USER32_BITS, data) &&
209 data != PSW_MASK_MERGE(PSW_USER_BITS, data))
210 /* Invalid psw mask. */
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;
218 *(addr_t *)((addr_t) &__KSTK_PTREGS(child)->psw + addr) = data;
220 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
222 * access registers are stored in the thread structure
224 offset = addr - (addr_t) &dummy->regs.acrs;
225 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
227 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
229 * orig_gpr2 is stored on the kernel stack
231 __KSTK_PTREGS(child)->orig_gpr2 = data;
233 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
235 * floating point regs. are stored in the thread structure
237 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
238 (data & ~FPC_VALID_MASK) != 0)
240 offset = addr - (addr_t) &dummy->regs.fp_regs;
241 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
243 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
245 * per_info is found in the thread structure
247 offset = addr - (addr_t) &dummy->regs.per_info;
248 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
252 FixPerRegisters(child);
257 do_ptrace_normal(struct task_struct *child, long request, long addr, long data)
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))
272 return put_user(tmp, (unsigned long __user *) data);
275 /* read the word at location addr in the USER area. */
276 return peek_user(child, addr, data);
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))
289 /* write the word at location addr in the USER area */
290 return poke_user(child, addr, data);
292 case PTRACE_PEEKUSR_AREA:
293 case PTRACE_POKEUSR_AREA:
294 if (copy_from_user(&parea, (void __user *) addr,
297 addr = parea.kernel_addr;
298 data = parea.process_addr;
300 while (copied < parea.len) {
301 if (request == PTRACE_PEEKUSR_AREA)
302 ret = peek_user(child, addr, data);
305 if (get_user (tmp, (addr_t __user *) data))
307 ret = poke_user(child, addr, tmp);
311 addr += sizeof(unsigned long);
312 data += sizeof(unsigned long);
313 copied += sizeof(unsigned long);
317 return ptrace_request(child, request, addr, data);
320 #ifdef CONFIG_S390_SUPPORT
322 * Now the fun part starts... a 31 bit program running in the
323 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
324 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
325 * to handle, the difference to the 64 bit versions of the requests
326 * is that the access is done in multiples of 4 byte instead of
327 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
328 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
329 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
330 * is a 31 bit program too, the content of struct user can be
331 * emulated. A 31 bit program peeking into the struct user of
332 * a 64 bit program is a no-no.
336 * Same as peek_user but for a 31 bit program.
339 peek_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
341 struct user32 *dummy32 = NULL;
342 per_struct32 *dummy_per32 = NULL;
346 if (!test_thread_flag(TIF_31BIT) ||
347 (addr & 3) || addr > sizeof(struct user) - 3)
350 if (addr < (addr_t) &dummy32->regs.acrs) {
352 * psw and gprs are stored on the stack
354 if (addr == (addr_t) &dummy32->regs.psw.mask) {
355 /* Fake a 31 bit psw mask. */
356 tmp = (__u32)(__KSTK_PTREGS(child)->psw.mask >> 32);
357 tmp = PSW32_MASK_MERGE(PSW32_USER_BITS, tmp);
358 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
359 /* Fake a 31 bit psw address. */
360 tmp = (__u32) __KSTK_PTREGS(child)->psw.addr |
364 tmp = *(__u32 *)((addr_t) &__KSTK_PTREGS(child)->psw +
367 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
369 * access registers are stored in the thread structure
371 offset = addr - (addr_t) &dummy32->regs.acrs;
372 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
374 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
376 * orig_gpr2 is stored on the kernel stack
378 tmp = *(__u32*)((addr_t) &__KSTK_PTREGS(child)->orig_gpr2 + 4);
380 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
382 * floating point regs. are stored in the thread structure
384 offset = addr - (addr_t) &dummy32->regs.fp_regs;
385 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
387 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
389 * per_info is found in the thread structure
391 offset = addr - (addr_t) &dummy32->regs.per_info;
392 /* This is magic. See per_struct and per_struct32. */
393 if ((offset >= (addr_t) &dummy_per32->control_regs &&
394 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
395 (offset >= (addr_t) &dummy_per32->starting_addr &&
396 offset <= (addr_t) &dummy_per32->ending_addr) ||
397 offset == (addr_t) &dummy_per32->lowcore.words.address)
398 offset = offset*2 + 4;
401 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
406 return put_user(tmp, (__u32 __user *) data);
410 * Same as poke_user but for a 31 bit program.
413 poke_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
415 struct user32 *dummy32 = NULL;
416 per_struct32 *dummy_per32 = NULL;
420 if (!test_thread_flag(TIF_31BIT) ||
421 (addr & 3) || addr > sizeof(struct user32) - 3)
426 if (addr < (addr_t) &dummy32->regs.acrs) {
428 * psw, gprs, acrs and orig_gpr2 are stored on the stack
430 if (addr == (addr_t) &dummy32->regs.psw.mask) {
431 /* Build a 64 bit psw mask from 31 bit mask. */
432 if (tmp != PSW32_MASK_MERGE(PSW32_USER_BITS, tmp))
433 /* Invalid psw mask. */
435 __KSTK_PTREGS(child)->psw.mask =
436 PSW_MASK_MERGE(PSW_USER32_BITS, (__u64) tmp << 32);
437 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
438 /* Build a 64 bit psw address from 31 bit address. */
439 __KSTK_PTREGS(child)->psw.addr =
440 (__u64) tmp & PSW32_ADDR_INSN;
443 *(__u32*)((addr_t) &__KSTK_PTREGS(child)->psw
446 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
448 * access registers are stored in the thread structure
450 offset = addr - (addr_t) &dummy32->regs.acrs;
451 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
453 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
455 * orig_gpr2 is stored on the kernel stack
457 *(__u32*)((addr_t) &__KSTK_PTREGS(child)->orig_gpr2 + 4) = tmp;
459 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
461 * floating point regs. are stored in the thread structure
463 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
464 (tmp & ~FPC_VALID_MASK) != 0)
465 /* Invalid floating point control. */
467 offset = addr - (addr_t) &dummy32->regs.fp_regs;
468 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
470 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
472 * per_info is found in the thread structure.
474 offset = addr - (addr_t) &dummy32->regs.per_info;
476 * This is magic. See per_struct and per_struct32.
477 * By incident the offsets in per_struct are exactly
478 * twice the offsets in per_struct32 for all fields.
479 * The 8 byte fields need special handling though,
480 * because the second half (bytes 4-7) is needed and
481 * not the first half.
483 if ((offset >= (addr_t) &dummy_per32->control_regs &&
484 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
485 (offset >= (addr_t) &dummy_per32->starting_addr &&
486 offset <= (addr_t) &dummy_per32->ending_addr) ||
487 offset == (addr_t) &dummy_per32->lowcore.words.address)
488 offset = offset*2 + 4;
491 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
495 FixPerRegisters(child);
500 do_ptrace_emu31(struct task_struct *child, long request, long addr, long data)
502 unsigned int tmp; /* 4 bytes !! */
503 ptrace_area_emu31 parea;
507 case PTRACE_PEEKTEXT:
508 case PTRACE_PEEKDATA:
509 /* read word at location addr. */
510 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
511 if (copied != sizeof(tmp))
513 return put_user(tmp, (unsigned int __user *) data);
516 /* read the word at location addr in the USER area. */
517 return peek_user_emu31(child, addr, data);
519 case PTRACE_POKETEXT:
520 case PTRACE_POKEDATA:
521 /* write the word at location addr. */
523 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
524 if (copied != sizeof(tmp))
529 /* write the word at location addr in the USER area */
530 return poke_user_emu31(child, addr, data);
532 case PTRACE_PEEKUSR_AREA:
533 case PTRACE_POKEUSR_AREA:
534 if (copy_from_user(&parea, (void __user *) addr,
537 addr = parea.kernel_addr;
538 data = parea.process_addr;
540 while (copied < parea.len) {
541 if (request == PTRACE_PEEKUSR_AREA)
542 ret = peek_user_emu31(child, addr, data);
545 if (get_user (tmp, (__u32 __user *) data))
547 ret = poke_user_emu31(child, addr, tmp);
551 addr += sizeof(unsigned int);
552 data += sizeof(unsigned int);
553 copied += sizeof(unsigned int);
556 case PTRACE_GETEVENTMSG:
557 return put_user((__u32) child->ptrace_message,
558 (unsigned int __user *) data);
559 case PTRACE_GETSIGINFO:
560 if (child->last_siginfo == NULL)
562 return copy_siginfo_to_user32((siginfo_t32 __user *) data,
563 child->last_siginfo);
564 case PTRACE_SETSIGINFO:
565 if (child->last_siginfo == NULL)
567 return copy_siginfo_from_user32(child->last_siginfo,
568 (siginfo_t32 __user *) data);
570 return ptrace_request(child, request, addr, data);
574 #define PT32_IEEE_IP 0x13c
577 do_ptrace(struct task_struct *child, long request, long addr, long data)
581 if (request == PTRACE_ATTACH)
582 return ptrace_attach(child);
585 * Special cases to get/store the ieee instructions pointer.
587 if (child == current) {
588 if (request == PTRACE_PEEKUSR && addr == PT_IEEE_IP)
589 return peek_user(child, addr, data);
590 if (request == PTRACE_POKEUSR && addr == PT_IEEE_IP)
591 return poke_user(child, addr, data);
592 #ifdef CONFIG_S390_SUPPORT
593 if (request == PTRACE_PEEKUSR &&
594 addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
595 return peek_user_emu31(child, addr, data);
596 if (request == PTRACE_POKEUSR &&
597 addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
598 return poke_user_emu31(child, addr, data);
602 ret = ptrace_check_attach(child, request == PTRACE_KILL);
608 /* continue and stop at next (return from) syscall */
610 /* restart after signal. */
611 if ((unsigned long) data >= _NSIG)
613 if (request == PTRACE_SYSCALL)
614 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
616 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
617 child->exit_code = data;
618 /* make sure the single step bit is not set. */
619 clear_single_step(child);
620 wake_up_process(child);
625 * make the child exit. Best I can do is send it a sigkill.
626 * perhaps it should be put in the status that it wants to
629 if (child->state == TASK_ZOMBIE) /* already dead */
631 child->exit_code = SIGKILL;
632 /* make sure the single step bit is not set. */
633 clear_single_step(child);
634 wake_up_process(child);
637 case PTRACE_SINGLESTEP:
638 /* set the trap flag. */
639 if ((unsigned long) data >= _NSIG)
641 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
642 child->exit_code = data;
643 set_single_step(child);
644 /* give it a chance to run. */
645 wake_up_process(child);
649 /* detach a process that was attached. */
650 return ptrace_detach(child, data);
653 /* Do requests that differ for 31/64 bit */
655 #ifdef CONFIG_S390_SUPPORT
656 if (test_thread_flag(TIF_31BIT))
657 return do_ptrace_emu31(child, request, addr, data);
659 return do_ptrace_normal(child, request, addr, data);
666 sys_ptrace(long request, long pid, long addr, long data)
668 struct task_struct *child;
673 if (request == PTRACE_TRACEME) {
674 /* are we already being traced? */
676 if (current->ptrace & PT_PTRACED)
678 ret = security_ptrace(current->parent, current);
681 /* set the ptrace bit in the process flags. */
682 current->ptrace |= PT_PTRACED;
687 if (pid == 1) /* you may not mess with init */
691 read_lock(&tasklist_lock);
692 child = find_task_by_pid(pid);
694 get_task_struct(child);
695 read_unlock(&tasklist_lock);
698 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
701 ret = do_ptrace(child, request, addr, data);
703 put_task_struct(child);
710 syscall_trace(struct pt_regs *regs, int entryexit)
712 if (unlikely(current->audit_context)) {
714 audit_syscall_entry(current, regs->gprs[2],
715 regs->orig_gpr2, regs->gprs[3],
716 regs->gprs[4], regs->gprs[5]);
718 audit_syscall_exit(current, regs->gprs[2]);
720 if (!test_thread_flag(TIF_SYSCALL_TRACE))
722 if (!(current->ptrace & PT_PTRACED))
724 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
728 * this isn't the same as continuing with a signal, but it will do
729 * for normal use. strace only continues with a signal if the
730 * stopping signal is not SIGTRAP. -brl
732 if (current->exit_code) {
733 send_sig(current->exit_code, current, 1);
734 current->exit_code = 0;