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>
34 #include <linux/audit.h>
35 #include <linux/signal.h>
37 #include <asm/segment.h>
39 #include <asm/pgtable.h>
40 #include <asm/pgalloc.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43 #include <asm/unistd.h>
46 #include "compat_ptrace.h"
50 FixPerRegisters(struct task_struct *task)
55 regs = task_pt_regs(task);
56 per_info = (per_struct *) &task->thread.per_info;
57 per_info->control_regs.bits.em_instruction_fetch =
58 per_info->single_step | per_info->instruction_fetch;
60 if (per_info->single_step) {
61 per_info->control_regs.bits.starting_addr = 0;
63 if (test_thread_flag(TIF_31BIT))
64 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
67 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
69 per_info->control_regs.bits.starting_addr =
70 per_info->starting_addr;
71 per_info->control_regs.bits.ending_addr =
72 per_info->ending_addr;
75 * if any of the control reg tracing bits are on
76 * we switch on per in the psw
78 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
79 regs->psw.mask |= PSW_MASK_PER;
81 regs->psw.mask &= ~PSW_MASK_PER;
83 if (per_info->control_regs.bits.em_storage_alteration)
84 per_info->control_regs.bits.storage_alt_space_ctl = 1;
86 per_info->control_regs.bits.storage_alt_space_ctl = 0;
90 set_single_step(struct task_struct *task)
92 task->thread.per_info.single_step = 1;
93 FixPerRegisters(task);
97 clear_single_step(struct task_struct *task)
99 task->thread.per_info.single_step = 0;
100 FixPerRegisters(task);
104 * Called by kernel/ptrace.c when detaching..
106 * Make sure single step bits etc are not set.
109 ptrace_disable(struct task_struct *child)
111 /* make sure the single step bit is not set. */
112 clear_single_step(child);
116 # define __ADDR_MASK 3
118 # define __ADDR_MASK 7
122 * Read the word at offset addr from the user area of a process. The
123 * trouble here is that the information is littered over different
124 * locations. The process registers are found on the kernel stack,
125 * the floating point stuff and the trace settings are stored in
126 * the task structure. In addition the different structures in
127 * struct user contain pad bytes that should be read as zeroes.
131 peek_user(struct task_struct *child, addr_t addr, addr_t data)
133 struct user *dummy = NULL;
134 addr_t offset, tmp, mask;
137 * Stupid gdb peeks/pokes the access registers in 64 bit with
138 * an alignment of 4. Programmers from hell...
142 if (addr >= (addr_t) &dummy->regs.acrs &&
143 addr < (addr_t) &dummy->regs.orig_gpr2)
146 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
149 if (addr < (addr_t) &dummy->regs.acrs) {
151 * psw and gprs are stored on the stack
153 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
154 if (addr == (addr_t) &dummy->regs.psw.mask)
155 /* Remove per bit from user psw. */
156 tmp &= ~PSW_MASK_PER;
158 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
160 * access registers are stored in the thread structure
162 offset = addr - (addr_t) &dummy->regs.acrs;
165 * Very special case: old & broken 64 bit gdb reading
166 * from acrs[15]. Result is a 64 bit value. Read the
167 * 32 bit acrs[15] value and shift it by 32. Sick...
169 if (addr == (addr_t) &dummy->regs.acrs[15])
170 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
173 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
175 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
177 * orig_gpr2 is stored on the kernel stack
179 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
181 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
183 * floating point regs. are stored in the thread structure
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);
191 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
193 * per_info is found in the thread structure
195 offset = addr - (addr_t) &dummy->regs.per_info;
196 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
201 return put_user(tmp, (addr_t __user *) data);
205 * Write a word to the user area of a process at location addr. This
206 * operation does have an additional problem compared to peek_user.
207 * Stores to the program status word and on the floating point
208 * control register needs to get checked for validity.
211 poke_user(struct task_struct *child, addr_t addr, addr_t data)
213 struct user *dummy = NULL;
217 * Stupid gdb peeks/pokes the access registers in 64 bit with
218 * an alignment of 4. Programmers from hell indeed...
222 if (addr >= (addr_t) &dummy->regs.acrs &&
223 addr < (addr_t) &dummy->regs.orig_gpr2)
226 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
229 if (addr < (addr_t) &dummy->regs.acrs) {
231 * psw and gprs are stored on the stack
233 if (addr == (addr_t) &dummy->regs.psw.mask &&
235 data != PSW_MASK_MERGE(PSW_USER32_BITS, data) &&
237 data != PSW_MASK_MERGE(PSW_USER_BITS, data))
238 /* Invalid psw mask. */
241 if (addr == (addr_t) &dummy->regs.psw.addr)
242 /* I'd like to reject addresses without the
243 high order bit but older gdb's rely on it */
244 data |= PSW_ADDR_AMODE;
246 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
248 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
250 * access registers are stored in the thread structure
252 offset = addr - (addr_t) &dummy->regs.acrs;
255 * Very special case: old & broken 64 bit gdb writing
256 * to acrs[15] with a 64 bit value. Ignore the lower
257 * half of the value and write the upper 32 bit to
260 if (addr == (addr_t) &dummy->regs.acrs[15])
261 child->thread.acrs[15] = (unsigned int) (data >> 32);
264 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
266 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
268 * orig_gpr2 is stored on the kernel stack
270 task_pt_regs(child)->orig_gpr2 = data;
272 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
274 * floating point regs. are stored in the thread structure
276 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
277 (data & ~((unsigned long) FPC_VALID_MASK
278 << (BITS_PER_LONG - 32))) != 0)
280 offset = addr - (addr_t) &dummy->regs.fp_regs;
281 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
283 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
285 * per_info is found in the thread structure
287 offset = addr - (addr_t) &dummy->regs.per_info;
288 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
292 FixPerRegisters(child);
297 do_ptrace_normal(struct task_struct *child, long request, long addr, long data)
304 case PTRACE_PEEKTEXT:
305 case PTRACE_PEEKDATA:
306 /* Remove high order bit from address (only for 31 bit). */
307 addr &= PSW_ADDR_INSN;
308 /* read word at location addr. */
309 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
310 if (copied != sizeof(tmp))
312 return put_user(tmp, (unsigned long __user *) data);
315 /* read the word at location addr in the USER area. */
316 return peek_user(child, addr, data);
318 case PTRACE_POKETEXT:
319 case PTRACE_POKEDATA:
320 /* Remove high order bit from address (only for 31 bit). */
321 addr &= PSW_ADDR_INSN;
322 /* write the word at location addr. */
323 copied = access_process_vm(child, addr, &data, sizeof(data),1);
324 if (copied != sizeof(data))
329 /* write the word at location addr in the USER area */
330 return poke_user(child, addr, data);
332 case PTRACE_PEEKUSR_AREA:
333 case PTRACE_POKEUSR_AREA:
334 if (copy_from_user(&parea, (void __user *) addr,
337 addr = parea.kernel_addr;
338 data = parea.process_addr;
340 while (copied < parea.len) {
341 if (request == PTRACE_PEEKUSR_AREA)
342 ret = peek_user(child, addr, data);
345 if (get_user (tmp, (addr_t __user *) data))
347 ret = poke_user(child, addr, tmp);
351 addr += sizeof(unsigned long);
352 data += sizeof(unsigned long);
353 copied += sizeof(unsigned long);
357 return ptrace_request(child, request, addr, data);
362 * Now the fun part starts... a 31 bit program running in the
363 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
364 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
365 * to handle, the difference to the 64 bit versions of the requests
366 * is that the access is done in multiples of 4 byte instead of
367 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
368 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
369 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
370 * is a 31 bit program too, the content of struct user can be
371 * emulated. A 31 bit program peeking into the struct user of
372 * a 64 bit program is a no-no.
376 * Same as peek_user but for a 31 bit program.
379 peek_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
381 struct user32 *dummy32 = NULL;
382 per_struct32 *dummy_per32 = NULL;
386 if (!test_thread_flag(TIF_31BIT) ||
387 (addr & 3) || addr > sizeof(struct user) - 3)
390 if (addr < (addr_t) &dummy32->regs.acrs) {
392 * psw and gprs are stored on the stack
394 if (addr == (addr_t) &dummy32->regs.psw.mask) {
395 /* Fake a 31 bit psw mask. */
396 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
397 tmp = PSW32_MASK_MERGE(PSW32_USER_BITS, tmp);
398 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
399 /* Fake a 31 bit psw address. */
400 tmp = (__u32) task_pt_regs(child)->psw.addr |
404 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
407 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
409 * access registers are stored in the thread structure
411 offset = addr - (addr_t) &dummy32->regs.acrs;
412 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
414 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
416 * orig_gpr2 is stored on the kernel stack
418 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
420 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
422 * floating point regs. are stored in the thread structure
424 offset = addr - (addr_t) &dummy32->regs.fp_regs;
425 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
427 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
429 * per_info is found in the thread structure
431 offset = addr - (addr_t) &dummy32->regs.per_info;
432 /* This is magic. See per_struct and per_struct32. */
433 if ((offset >= (addr_t) &dummy_per32->control_regs &&
434 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
435 (offset >= (addr_t) &dummy_per32->starting_addr &&
436 offset <= (addr_t) &dummy_per32->ending_addr) ||
437 offset == (addr_t) &dummy_per32->lowcore.words.address)
438 offset = offset*2 + 4;
441 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
446 return put_user(tmp, (__u32 __user *) data);
450 * Same as poke_user but for a 31 bit program.
453 poke_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
455 struct user32 *dummy32 = NULL;
456 per_struct32 *dummy_per32 = NULL;
460 if (!test_thread_flag(TIF_31BIT) ||
461 (addr & 3) || addr > sizeof(struct user32) - 3)
466 if (addr < (addr_t) &dummy32->regs.acrs) {
468 * psw, gprs, acrs and orig_gpr2 are stored on the stack
470 if (addr == (addr_t) &dummy32->regs.psw.mask) {
471 /* Build a 64 bit psw mask from 31 bit mask. */
472 if (tmp != PSW32_MASK_MERGE(PSW32_USER_BITS, tmp))
473 /* Invalid psw mask. */
475 task_pt_regs(child)->psw.mask =
476 PSW_MASK_MERGE(PSW_USER32_BITS, (__u64) tmp << 32);
477 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
478 /* Build a 64 bit psw address from 31 bit address. */
479 task_pt_regs(child)->psw.addr =
480 (__u64) tmp & PSW32_ADDR_INSN;
483 *(__u32*)((addr_t) &task_pt_regs(child)->psw
486 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
488 * access registers are stored in the thread structure
490 offset = addr - (addr_t) &dummy32->regs.acrs;
491 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
493 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
495 * orig_gpr2 is stored on the kernel stack
497 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
499 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
501 * floating point regs. are stored in the thread structure
503 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
504 (tmp & ~FPC_VALID_MASK) != 0)
505 /* Invalid floating point control. */
507 offset = addr - (addr_t) &dummy32->regs.fp_regs;
508 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
510 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
512 * per_info is found in the thread structure.
514 offset = addr - (addr_t) &dummy32->regs.per_info;
516 * This is magic. See per_struct and per_struct32.
517 * By incident the offsets in per_struct are exactly
518 * twice the offsets in per_struct32 for all fields.
519 * The 8 byte fields need special handling though,
520 * because the second half (bytes 4-7) is needed and
521 * not the first half.
523 if ((offset >= (addr_t) &dummy_per32->control_regs &&
524 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
525 (offset >= (addr_t) &dummy_per32->starting_addr &&
526 offset <= (addr_t) &dummy_per32->ending_addr) ||
527 offset == (addr_t) &dummy_per32->lowcore.words.address)
528 offset = offset*2 + 4;
531 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
535 FixPerRegisters(child);
540 do_ptrace_emu31(struct task_struct *child, long request, long addr, long data)
542 unsigned int tmp; /* 4 bytes !! */
543 ptrace_area_emu31 parea;
547 case PTRACE_PEEKTEXT:
548 case PTRACE_PEEKDATA:
549 /* read word at location addr. */
550 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
551 if (copied != sizeof(tmp))
553 return put_user(tmp, (unsigned int __user *) data);
556 /* read the word at location addr in the USER area. */
557 return peek_user_emu31(child, addr, data);
559 case PTRACE_POKETEXT:
560 case PTRACE_POKEDATA:
561 /* write the word at location addr. */
563 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
564 if (copied != sizeof(tmp))
569 /* write the word at location addr in the USER area */
570 return poke_user_emu31(child, addr, data);
572 case PTRACE_PEEKUSR_AREA:
573 case PTRACE_POKEUSR_AREA:
574 if (copy_from_user(&parea, (void __user *) addr,
577 addr = parea.kernel_addr;
578 data = parea.process_addr;
580 while (copied < parea.len) {
581 if (request == PTRACE_PEEKUSR_AREA)
582 ret = peek_user_emu31(child, addr, data);
585 if (get_user (tmp, (__u32 __user *) data))
587 ret = poke_user_emu31(child, addr, tmp);
591 addr += sizeof(unsigned int);
592 data += sizeof(unsigned int);
593 copied += sizeof(unsigned int);
596 case PTRACE_GETEVENTMSG:
597 return put_user((__u32) child->ptrace_message,
598 (unsigned int __user *) data);
599 case PTRACE_GETSIGINFO:
600 if (child->last_siginfo == NULL)
602 return copy_siginfo_to_user32((compat_siginfo_t __user *) data,
603 child->last_siginfo);
604 case PTRACE_SETSIGINFO:
605 if (child->last_siginfo == NULL)
607 return copy_siginfo_from_user32(child->last_siginfo,
608 (compat_siginfo_t __user *) data);
610 return ptrace_request(child, request, addr, data);
614 #define PT32_IEEE_IP 0x13c
617 do_ptrace(struct task_struct *child, long request, long addr, long data)
621 if (request == PTRACE_ATTACH)
622 return ptrace_attach(child);
625 * Special cases to get/store the ieee instructions pointer.
627 if (child == current) {
628 if (request == PTRACE_PEEKUSR && addr == PT_IEEE_IP)
629 return peek_user(child, addr, data);
630 if (request == PTRACE_POKEUSR && addr == PT_IEEE_IP)
631 return poke_user(child, addr, data);
633 if (request == PTRACE_PEEKUSR &&
634 addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
635 return peek_user_emu31(child, addr, data);
636 if (request == PTRACE_POKEUSR &&
637 addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
638 return poke_user_emu31(child, addr, data);
642 ret = ptrace_check_attach(child, request == PTRACE_KILL);
648 /* continue and stop at next (return from) syscall */
650 /* restart after signal. */
651 if (!valid_signal(data))
653 if (request == PTRACE_SYSCALL)
654 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
656 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
657 child->exit_code = data;
658 /* make sure the single step bit is not set. */
659 clear_single_step(child);
660 wake_up_process(child);
665 * make the child exit. Best I can do is send it a sigkill.
666 * perhaps it should be put in the status that it wants to
669 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
671 child->exit_code = SIGKILL;
672 /* make sure the single step bit is not set. */
673 clear_single_step(child);
674 wake_up_process(child);
677 case PTRACE_SINGLESTEP:
678 /* set the trap flag. */
679 if (!valid_signal(data))
681 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
682 child->exit_code = data;
684 set_tsk_thread_flag(child, TIF_SINGLE_STEP);
686 set_single_step(child);
687 /* give it a chance to run. */
688 wake_up_process(child);
692 /* detach a process that was attached. */
693 return ptrace_detach(child, data);
696 /* Do requests that differ for 31/64 bit */
699 if (test_thread_flag(TIF_31BIT))
700 return do_ptrace_emu31(child, request, addr, data);
702 return do_ptrace_normal(child, request, addr, data);
709 sys_ptrace(long request, long pid, long addr, long data)
711 struct task_struct *child;
715 if (request == PTRACE_TRACEME) {
716 ret = ptrace_traceme();
720 child = ptrace_get_task_struct(pid);
722 ret = PTR_ERR(child);
726 ret = do_ptrace(child, request, addr, data);
727 put_task_struct(child);
734 syscall_trace(struct pt_regs *regs, int entryexit)
736 if (unlikely(current->audit_context) && entryexit)
737 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]), regs->gprs[2]);
739 if (!test_thread_flag(TIF_SYSCALL_TRACE))
741 if (!(current->ptrace & PT_PTRACED))
743 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
747 * If the debuffer has set an invalid system call number,
748 * we prepare to skip the system call restart handling.
750 if (!entryexit && regs->gprs[2] >= NR_syscalls)
754 * this isn't the same as continuing with a signal, but it will do
755 * for normal use. strace only continues with a signal if the
756 * stopping signal is not SIGTRAP. -brl
758 if (current->exit_code) {
759 send_sig(current->exit_code, current, 1);
760 current->exit_code = 0;
763 if (unlikely(current->audit_context) && !entryexit)
764 audit_syscall_entry(test_thread_flag(TIF_31BIT)?AUDIT_ARCH_S390:AUDIT_ARCH_S390X,
765 regs->gprs[2], regs->orig_gpr2, regs->gprs[3],
766 regs->gprs[4], regs->gprs[5]);