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) + 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 *) 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) + 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 *) 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 *) addr, sizeof(parea)))
296 addr = parea.kernel_addr;
297 data = parea.process_addr;
299 while (copied < parea.len) {
300 if (request == PTRACE_PEEKUSR_AREA)
301 ret = peek_user(child, addr, data);
304 if (get_user (tmp, (addr_t *) data))
306 ret = poke_user(child, addr, tmp);
310 addr += sizeof(unsigned long);
311 data += sizeof(unsigned long);
312 copied += sizeof(unsigned long);
316 return ptrace_request(child, request, addr, data);
319 #ifdef CONFIG_S390_SUPPORT
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.
335 * Same as peek_user but for a 31 bit program.
338 peek_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
340 struct user32 *dummy32 = NULL;
341 per_struct32 *dummy_per32 = NULL;
345 if (!test_thread_flag(TIF_31BIT) ||
346 (addr & 3) || addr > sizeof(struct user) - 3)
349 if (addr < (addr_t) &dummy32->regs.acrs) {
351 * psw and gprs are stored on the stack
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 |
363 tmp = *(__u32 *)((addr_t) __KSTK_PTREGS(child) +
366 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
368 * access registers are stored in the thread structure
370 offset = addr - (addr_t) &dummy32->regs.acrs;
371 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
373 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
375 * orig_gpr2 is stored on the kernel stack
377 tmp = *(__u32*)((addr_t) &__KSTK_PTREGS(child)->orig_gpr2 + 4);
379 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
381 * floating point regs. are stored in the thread structure
383 offset = addr - (addr_t) &dummy32->regs.fp_regs;
384 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
386 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
388 * per_info is found in the thread structure
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;
400 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
405 return put_user(tmp, (__u32 *) data);
409 * Same as poke_user but for a 31 bit program.
412 poke_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
414 struct user32 *dummy32 = NULL;
415 per_struct32 *dummy_per32 = NULL;
419 if (!test_thread_flag(TIF_31BIT) ||
420 (addr & 3) || addr > sizeof(struct user32) - 3)
425 if (addr < (addr_t) &dummy32->regs.acrs) {
427 * psw, gprs, acrs and orig_gpr2 are stored on the stack
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. */
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;
442 *(__u32*)((addr_t) __KSTK_PTREGS(child) + addr*2 + 4) =
445 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
447 * access registers are stored in the thread structure
449 offset = addr - (addr_t) &dummy32->regs.acrs;
450 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
452 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
454 * orig_gpr2 is stored on the kernel stack
456 *(__u32*)((addr_t) &__KSTK_PTREGS(child)->orig_gpr2 + 4) = tmp;
458 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
460 * floating point regs. are stored in the thread structure
462 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
463 (tmp & ~FPC_VALID_MASK) != 0)
464 /* Invalid floating point control. */
466 offset = addr - (addr_t) &dummy32->regs.fp_regs;
467 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
469 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
471 * per_info is found in the thread structure.
473 offset = addr - (addr_t) &dummy32->regs.per_info;
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.
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;
490 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
494 FixPerRegisters(child);
499 do_ptrace_emu31(struct task_struct *child, long request, long addr, long data)
501 unsigned int tmp; /* 4 bytes !! */
502 ptrace_area_emu31 parea;
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))
512 return put_user(tmp, (unsigned int *) data);
515 /* read the word at location addr in the USER area. */
516 return peek_user_emu31(child, addr, data);
518 case PTRACE_POKETEXT:
519 case PTRACE_POKEDATA:
520 /* write the word at location addr. */
522 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
523 if (copied != sizeof(tmp))
528 /* write the word at location addr in the USER area */
529 return poke_user_emu31(child, addr, data);
531 case PTRACE_PEEKUSR_AREA:
532 case PTRACE_POKEUSR_AREA:
533 if (copy_from_user(&parea, (void *) addr, sizeof(parea)))
535 addr = parea.kernel_addr;
536 data = parea.process_addr;
538 while (copied < parea.len) {
539 if (request == PTRACE_PEEKUSR_AREA)
540 ret = peek_user_emu31(child, addr, data);
543 if (get_user (tmp, (__u32 *) data))
545 ret = poke_user_emu31(child, addr, tmp);
549 addr += sizeof(unsigned int);
550 data += sizeof(unsigned int);
551 copied += sizeof(unsigned int);
555 return ptrace_request(child, request, addr, data);
559 #define PT32_IEEE_IP 0x13c
562 do_ptrace(struct task_struct *child, long request, long addr, long data)
566 if (request == PTRACE_ATTACH)
567 return ptrace_attach(child);
570 * Special cases to get/store the ieee instructions pointer.
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);
587 ret = ptrace_check_attach(child, request == PTRACE_KILL);
593 /* continue and stop at next (return from) syscall */
595 /* restart after signal. */
596 if ((unsigned long) data >= _NSIG)
598 if (request == PTRACE_SYSCALL)
599 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
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);
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
614 if (child->state == TASK_ZOMBIE) /* already dead */
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);
622 case PTRACE_SINGLESTEP:
623 /* set the trap flag. */
624 if ((unsigned long) data >= _NSIG)
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);
634 /* detach a process that was attached. */
635 return ptrace_detach(child, data);
638 /* Do requests that differ for 31/64 bit */
640 #ifdef CONFIG_S390_SUPPORT
641 if (test_thread_flag(TIF_31BIT))
642 return do_ptrace_emu31(child, request, addr, data);
644 return do_ptrace_normal(child, request, addr, data);
651 sys_ptrace(long request, long pid, long addr, long data)
653 struct task_struct *child;
658 if (request == PTRACE_TRACEME) {
659 /* are we already being traced? */
661 if (current->ptrace & PT_PTRACED)
663 ret = security_ptrace(current->parent, current);
666 /* set the ptrace bit in the process flags. */
667 current->ptrace |= PT_PTRACED;
672 if (pid == 1) /* you may not mess with init */
676 read_lock(&tasklist_lock);
677 child = find_task_by_pid(pid);
679 get_task_struct(child);
680 read_unlock(&tasklist_lock);
683 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
686 ret = do_ptrace(child, request, addr, data);
688 put_task_struct(child);
695 syscall_trace(struct pt_regs *regs, int entryexit)
697 if (unlikely(current->audit_context)) {
699 audit_syscall_entry(current, regs->gprs[2],
700 regs->orig_gpr2, regs->gprs[3],
701 regs->gprs[4], regs->gprs[5]);
703 audit_syscall_exit(current, regs->gprs[2]);
705 if (!test_thread_flag(TIF_SYSCALL_TRACE))
707 if (!(current->ptrace & PT_PTRACED))
709 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
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
717 if (current->exit_code) {
718 send_sig(current->exit_code, current, 1);
719 current->exit_code = 0;