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>
44 #ifdef CONFIG_S390_SUPPORT
45 #include "compat_ptrace.h"
49 FixPerRegisters(struct task_struct *task)
54 regs = __KSTK_PTREGS(task);
55 per_info = (per_struct *) &task->thread.per_info;
56 per_info->control_regs.bits.em_instruction_fetch =
57 per_info->single_step | per_info->instruction_fetch;
59 if (per_info->single_step) {
60 per_info->control_regs.bits.starting_addr = 0;
61 #ifdef CONFIG_S390_SUPPORT
62 if (test_thread_flag(TIF_31BIT))
63 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
66 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
68 per_info->control_regs.bits.starting_addr =
69 per_info->starting_addr;
70 per_info->control_regs.bits.ending_addr =
71 per_info->ending_addr;
74 * if any of the control reg tracing bits are on
75 * we switch on per in the psw
77 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
78 regs->psw.mask |= PSW_MASK_PER;
80 regs->psw.mask &= ~PSW_MASK_PER;
82 if (per_info->control_regs.bits.em_storage_alteration)
83 per_info->control_regs.bits.storage_alt_space_ctl = 1;
85 per_info->control_regs.bits.storage_alt_space_ctl = 0;
89 set_single_step(struct task_struct *task)
91 task->thread.per_info.single_step = 1;
92 FixPerRegisters(task);
96 clear_single_step(struct task_struct *task)
98 task->thread.per_info.single_step = 0;
99 FixPerRegisters(task);
103 * Called by kernel/ptrace.c when detaching..
105 * Make sure single step bits etc are not set.
108 ptrace_disable(struct task_struct *child)
110 /* make sure the single step bit is not set. */
111 clear_single_step(child);
114 #ifndef CONFIG_ARCH_S390X
115 # define __ADDR_MASK 3
117 # define __ADDR_MASK 7
121 * Read the word at offset addr from the user area of a process. The
122 * trouble here is that the information is littered over different
123 * locations. The process registers are found on the kernel stack,
124 * the floating point stuff and the trace settings are stored in
125 * the task structure. In addition the different structures in
126 * struct user contain pad bytes that should be read as zeroes.
130 peek_user(struct task_struct *child, addr_t addr, addr_t data)
132 struct user *dummy = NULL;
136 * Stupid gdb peeks/pokes the access registers in 64 bit with
137 * an alignment of 4. Programmers from hell...
139 if ((addr & 3) || addr > sizeof(struct user) - __ADDR_MASK)
142 if (addr < (addr_t) &dummy->regs.acrs) {
144 * psw and gprs are stored on the stack
146 tmp = *(addr_t *)((addr_t) &__KSTK_PTREGS(child)->psw + addr);
147 if (addr == (addr_t) &dummy->regs.psw.mask)
148 /* Remove per bit from user psw. */
149 tmp &= ~PSW_MASK_PER;
151 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
153 * access registers are stored in the thread structure
155 offset = addr - (addr_t) &dummy->regs.acrs;
156 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
158 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
160 * orig_gpr2 is stored on the kernel stack
162 tmp = (addr_t) __KSTK_PTREGS(child)->orig_gpr2;
164 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
166 * floating point regs. are stored in the thread structure
168 offset = addr - (addr_t) &dummy->regs.fp_regs;
169 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
171 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
173 * per_info is found in the thread structure
175 offset = addr - (addr_t) &dummy->regs.per_info;
176 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
181 return put_user(tmp, (addr_t __user *) data);
185 * Write a word to the user area of a process at location addr. This
186 * operation does have an additional problem compared to peek_user.
187 * Stores to the program status word and on the floating point
188 * control register needs to get checked for validity.
191 poke_user(struct task_struct *child, addr_t addr, addr_t data)
193 struct user *dummy = NULL;
197 * Stupid gdb peeks/pokes the access registers in 64 bit with
198 * an alignment of 4. Programmers from hell indeed...
200 if ((addr & 3) || addr > sizeof(struct user) - __ADDR_MASK)
203 if (addr < (addr_t) &dummy->regs.acrs) {
205 * psw and gprs are stored on the stack
207 if (addr == (addr_t) &dummy->regs.psw.mask &&
208 #ifdef CONFIG_S390_SUPPORT
209 data != PSW_MASK_MERGE(PSW_USER32_BITS, data) &&
211 data != PSW_MASK_MERGE(PSW_USER_BITS, data))
212 /* Invalid psw mask. */
214 #ifndef CONFIG_ARCH_S390X
215 if (addr == (addr_t) &dummy->regs.psw.addr)
216 /* I'd like to reject addresses without the
217 high order bit but older gdb's rely on it */
218 data |= PSW_ADDR_AMODE;
220 *(addr_t *)((addr_t) &__KSTK_PTREGS(child)->psw + addr) = data;
222 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
224 * access registers are stored in the thread structure
226 offset = addr - (addr_t) &dummy->regs.acrs;
227 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
229 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
231 * orig_gpr2 is stored on the kernel stack
233 __KSTK_PTREGS(child)->orig_gpr2 = data;
235 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
237 * floating point regs. are stored in the thread structure
239 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
240 (data & ~FPC_VALID_MASK) != 0)
242 offset = addr - (addr_t) &dummy->regs.fp_regs;
243 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
245 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
247 * per_info is found in the thread structure
249 offset = addr - (addr_t) &dummy->regs.per_info;
250 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
254 FixPerRegisters(child);
259 do_ptrace_normal(struct task_struct *child, long request, long addr, long data)
266 case PTRACE_PEEKTEXT:
267 case PTRACE_PEEKDATA:
268 /* Remove high order bit from address (only for 31 bit). */
269 addr &= PSW_ADDR_INSN;
270 /* read word at location addr. */
271 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
272 if (copied != sizeof(tmp))
274 return put_user(tmp, (unsigned long __user *) data);
277 /* read the word at location addr in the USER area. */
278 return peek_user(child, addr, data);
280 case PTRACE_POKETEXT:
281 case PTRACE_POKEDATA:
282 /* Remove high order bit from address (only for 31 bit). */
283 addr &= PSW_ADDR_INSN;
284 /* write the word at location addr. */
285 copied = access_process_vm(child, addr, &data, sizeof(data),1);
286 if (copied != sizeof(data))
291 /* write the word at location addr in the USER area */
292 return poke_user(child, addr, data);
294 case PTRACE_PEEKUSR_AREA:
295 case PTRACE_POKEUSR_AREA:
296 if (copy_from_user(&parea, (void __user *) addr,
299 addr = parea.kernel_addr;
300 data = parea.process_addr;
302 while (copied < parea.len) {
303 if (request == PTRACE_PEEKUSR_AREA)
304 ret = peek_user(child, addr, data);
307 if (get_user (tmp, (addr_t __user *) data))
309 ret = poke_user(child, addr, tmp);
313 addr += sizeof(unsigned long);
314 data += sizeof(unsigned long);
315 copied += sizeof(unsigned long);
319 return ptrace_request(child, request, addr, data);
322 #ifdef CONFIG_S390_SUPPORT
324 * Now the fun part starts... a 31 bit program running in the
325 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
326 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
327 * to handle, the difference to the 64 bit versions of the requests
328 * is that the access is done in multiples of 4 byte instead of
329 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
330 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
331 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
332 * is a 31 bit program too, the content of struct user can be
333 * emulated. A 31 bit program peeking into the struct user of
334 * a 64 bit program is a no-no.
338 * Same as peek_user but for a 31 bit program.
341 peek_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
343 struct user32 *dummy32 = NULL;
344 per_struct32 *dummy_per32 = NULL;
348 if (!test_thread_flag(TIF_31BIT) ||
349 (addr & 3) || addr > sizeof(struct user) - 3)
352 if (addr < (addr_t) &dummy32->regs.acrs) {
354 * psw and gprs are stored on the stack
356 if (addr == (addr_t) &dummy32->regs.psw.mask) {
357 /* Fake a 31 bit psw mask. */
358 tmp = (__u32)(__KSTK_PTREGS(child)->psw.mask >> 32);
359 tmp = PSW32_MASK_MERGE(PSW32_USER_BITS, tmp);
360 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
361 /* Fake a 31 bit psw address. */
362 tmp = (__u32) __KSTK_PTREGS(child)->psw.addr |
366 tmp = *(__u32 *)((addr_t) &__KSTK_PTREGS(child)->psw +
369 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
371 * access registers are stored in the thread structure
373 offset = addr - (addr_t) &dummy32->regs.acrs;
374 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
376 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
378 * orig_gpr2 is stored on the kernel stack
380 tmp = *(__u32*)((addr_t) &__KSTK_PTREGS(child)->orig_gpr2 + 4);
382 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
384 * floating point regs. are stored in the thread structure
386 offset = addr - (addr_t) &dummy32->regs.fp_regs;
387 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
389 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
391 * per_info is found in the thread structure
393 offset = addr - (addr_t) &dummy32->regs.per_info;
394 /* This is magic. See per_struct and per_struct32. */
395 if ((offset >= (addr_t) &dummy_per32->control_regs &&
396 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
397 (offset >= (addr_t) &dummy_per32->starting_addr &&
398 offset <= (addr_t) &dummy_per32->ending_addr) ||
399 offset == (addr_t) &dummy_per32->lowcore.words.address)
400 offset = offset*2 + 4;
403 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
408 return put_user(tmp, (__u32 __user *) data);
412 * Same as poke_user but for a 31 bit program.
415 poke_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
417 struct user32 *dummy32 = NULL;
418 per_struct32 *dummy_per32 = NULL;
422 if (!test_thread_flag(TIF_31BIT) ||
423 (addr & 3) || addr > sizeof(struct user32) - 3)
428 if (addr < (addr_t) &dummy32->regs.acrs) {
430 * psw, gprs, acrs and orig_gpr2 are stored on the stack
432 if (addr == (addr_t) &dummy32->regs.psw.mask) {
433 /* Build a 64 bit psw mask from 31 bit mask. */
434 if (tmp != PSW32_MASK_MERGE(PSW32_USER_BITS, tmp))
435 /* Invalid psw mask. */
437 __KSTK_PTREGS(child)->psw.mask =
438 PSW_MASK_MERGE(PSW_USER32_BITS, (__u64) tmp << 32);
439 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
440 /* Build a 64 bit psw address from 31 bit address. */
441 __KSTK_PTREGS(child)->psw.addr =
442 (__u64) tmp & PSW32_ADDR_INSN;
445 *(__u32*)((addr_t) &__KSTK_PTREGS(child)->psw
448 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
450 * access registers are stored in the thread structure
452 offset = addr - (addr_t) &dummy32->regs.acrs;
453 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
455 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
457 * orig_gpr2 is stored on the kernel stack
459 *(__u32*)((addr_t) &__KSTK_PTREGS(child)->orig_gpr2 + 4) = tmp;
461 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
463 * floating point regs. are stored in the thread structure
465 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
466 (tmp & ~FPC_VALID_MASK) != 0)
467 /* Invalid floating point control. */
469 offset = addr - (addr_t) &dummy32->regs.fp_regs;
470 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
472 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
474 * per_info is found in the thread structure.
476 offset = addr - (addr_t) &dummy32->regs.per_info;
478 * This is magic. See per_struct and per_struct32.
479 * By incident the offsets in per_struct are exactly
480 * twice the offsets in per_struct32 for all fields.
481 * The 8 byte fields need special handling though,
482 * because the second half (bytes 4-7) is needed and
483 * not the first half.
485 if ((offset >= (addr_t) &dummy_per32->control_regs &&
486 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
487 (offset >= (addr_t) &dummy_per32->starting_addr &&
488 offset <= (addr_t) &dummy_per32->ending_addr) ||
489 offset == (addr_t) &dummy_per32->lowcore.words.address)
490 offset = offset*2 + 4;
493 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
497 FixPerRegisters(child);
502 do_ptrace_emu31(struct task_struct *child, long request, long addr, long data)
504 unsigned int tmp; /* 4 bytes !! */
505 ptrace_area_emu31 parea;
509 case PTRACE_PEEKTEXT:
510 case PTRACE_PEEKDATA:
511 /* read word at location addr. */
512 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
513 if (copied != sizeof(tmp))
515 return put_user(tmp, (unsigned int __user *) data);
518 /* read the word at location addr in the USER area. */
519 return peek_user_emu31(child, addr, data);
521 case PTRACE_POKETEXT:
522 case PTRACE_POKEDATA:
523 /* write the word at location addr. */
525 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
526 if (copied != sizeof(tmp))
531 /* write the word at location addr in the USER area */
532 return poke_user_emu31(child, addr, data);
534 case PTRACE_PEEKUSR_AREA:
535 case PTRACE_POKEUSR_AREA:
536 if (copy_from_user(&parea, (void __user *) addr,
539 addr = parea.kernel_addr;
540 data = parea.process_addr;
542 while (copied < parea.len) {
543 if (request == PTRACE_PEEKUSR_AREA)
544 ret = peek_user_emu31(child, addr, data);
547 if (get_user (tmp, (__u32 __user *) data))
549 ret = poke_user_emu31(child, addr, tmp);
553 addr += sizeof(unsigned int);
554 data += sizeof(unsigned int);
555 copied += sizeof(unsigned int);
558 case PTRACE_GETEVENTMSG:
559 return put_user((__u32) child->ptrace_message,
560 (unsigned int __user *) data);
561 case PTRACE_GETSIGINFO:
562 if (child->last_siginfo == NULL)
564 return copy_siginfo_to_user32((compat_siginfo_t __user *) data,
565 child->last_siginfo);
566 case PTRACE_SETSIGINFO:
567 if (child->last_siginfo == NULL)
569 return copy_siginfo_from_user32(child->last_siginfo,
570 (compat_siginfo_t __user *) data);
572 return ptrace_request(child, request, addr, data);
576 #define PT32_IEEE_IP 0x13c
579 do_ptrace(struct task_struct *child, long request, long addr, long data)
583 if (request == PTRACE_ATTACH)
584 return ptrace_attach(child);
587 * Special cases to get/store the ieee instructions pointer.
589 if (child == current) {
590 if (request == PTRACE_PEEKUSR && addr == PT_IEEE_IP)
591 return peek_user(child, addr, data);
592 if (request == PTRACE_POKEUSR && addr == PT_IEEE_IP)
593 return poke_user(child, addr, data);
594 #ifdef CONFIG_S390_SUPPORT
595 if (request == PTRACE_PEEKUSR &&
596 addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
597 return peek_user_emu31(child, addr, data);
598 if (request == PTRACE_POKEUSR &&
599 addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
600 return poke_user_emu31(child, addr, data);
604 ret = ptrace_check_attach(child, request == PTRACE_KILL);
610 /* continue and stop at next (return from) syscall */
612 /* restart after signal. */
613 if (!valid_signal(data))
615 if (request == PTRACE_SYSCALL)
616 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
618 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
619 child->exit_code = data;
620 /* make sure the single step bit is not set. */
621 clear_single_step(child);
622 wake_up_process(child);
627 * make the child exit. Best I can do is send it a sigkill.
628 * perhaps it should be put in the status that it wants to
631 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
633 child->exit_code = SIGKILL;
634 /* make sure the single step bit is not set. */
635 clear_single_step(child);
636 wake_up_process(child);
639 case PTRACE_SINGLESTEP:
640 /* set the trap flag. */
641 if (!valid_signal(data))
643 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
644 child->exit_code = data;
646 set_tsk_thread_flag(child, TIF_SINGLE_STEP);
648 set_single_step(child);
649 /* give it a chance to run. */
650 wake_up_process(child);
654 /* detach a process that was attached. */
655 return ptrace_detach(child, data);
658 /* Do requests that differ for 31/64 bit */
660 #ifdef CONFIG_S390_SUPPORT
661 if (test_thread_flag(TIF_31BIT))
662 return do_ptrace_emu31(child, request, addr, data);
664 return do_ptrace_normal(child, request, addr, data);
671 sys_ptrace(long request, long pid, long addr, long data)
673 struct task_struct *child;
678 if (request == PTRACE_TRACEME) {
679 /* are we already being traced? */
681 if (current->ptrace & PT_PTRACED)
683 ret = security_ptrace(current->parent, current);
686 /* set the ptrace bit in the process flags. */
687 current->ptrace |= PT_PTRACED;
692 if (pid == 1) /* you may not mess with init */
696 read_lock(&tasklist_lock);
697 child = find_task_by_pid(pid);
699 get_task_struct(child);
700 read_unlock(&tasklist_lock);
704 ret = do_ptrace(child, request, addr, data);
706 put_task_struct(child);
713 syscall_trace(struct pt_regs *regs, int entryexit)
715 if (unlikely(current->audit_context) && entryexit)
716 audit_syscall_exit(current, AUDITSC_RESULT(regs->gprs[2]), regs->gprs[2]);
718 if (!test_thread_flag(TIF_SYSCALL_TRACE))
720 if (!(current->ptrace & PT_PTRACED))
722 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
726 * this isn't the same as continuing with a signal, but it will do
727 * for normal use. strace only continues with a signal if the
728 * stopping signal is not SIGTRAP. -brl
730 if (current->exit_code) {
731 send_sig(current->exit_code, current, 1);
732 current->exit_code = 0;
735 if (unlikely(current->audit_context) && !entryexit)
736 audit_syscall_entry(current,
737 test_thread_flag(TIF_31BIT)?AUDIT_ARCH_S390:AUDIT_ARCH_S390X,
738 regs->gprs[2], regs->orig_gpr2, regs->gprs[3],
739 regs->gprs[4], regs->gprs[5]);