Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / arch / s390 / kernel / ptrace.c
blob35827b9bd4d1827f102f79f3e999bbb69c52f5ab
1 /*
2 * arch/s390/kernel/ptrace.c
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)
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>
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 #include <linux/audit.h>
35 #include <linux/signal.h>
37 #include <asm/segment.h>
38 #include <asm/page.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>
44 #include "entry.h"
46 #ifdef CONFIG_COMPAT
47 #include "compat_ptrace.h"
48 #endif
50 static void
51 FixPerRegisters(struct task_struct *task)
53 struct pt_regs *regs;
54 per_struct *per_info;
56 regs = task_pt_regs(task);
57 per_info = (per_struct *) &task->thread.per_info;
58 per_info->control_regs.bits.em_instruction_fetch =
59 per_info->single_step | per_info->instruction_fetch;
61 if (per_info->single_step) {
62 per_info->control_regs.bits.starting_addr = 0;
63 #ifdef CONFIG_COMPAT
64 if (test_thread_flag(TIF_31BIT))
65 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
66 else
67 #endif
68 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
69 } else {
70 per_info->control_regs.bits.starting_addr =
71 per_info->starting_addr;
72 per_info->control_regs.bits.ending_addr =
73 per_info->ending_addr;
76 * if any of the control reg tracing bits are on
77 * we switch on per in the psw
79 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
80 regs->psw.mask |= PSW_MASK_PER;
81 else
82 regs->psw.mask &= ~PSW_MASK_PER;
84 if (per_info->control_regs.bits.em_storage_alteration)
85 per_info->control_regs.bits.storage_alt_space_ctl = 1;
86 else
87 per_info->control_regs.bits.storage_alt_space_ctl = 0;
90 void user_enable_single_step(struct task_struct *task)
92 task->thread.per_info.single_step = 1;
93 FixPerRegisters(task);
96 void user_disable_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.
107 void
108 ptrace_disable(struct task_struct *child)
110 /* make sure the single step bit is not set. */
111 user_disable_single_step(child);
114 #ifndef CONFIG_64BIT
115 # define __ADDR_MASK 3
116 #else
117 # define __ADDR_MASK 7
118 #endif
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.
127 * Lovely...
129 static int
130 peek_user(struct task_struct *child, addr_t addr, addr_t data)
132 struct user *dummy = NULL;
133 addr_t offset, tmp, mask;
136 * Stupid gdb peeks/pokes the access registers in 64 bit with
137 * an alignment of 4. Programmers from hell...
139 mask = __ADDR_MASK;
140 #ifdef CONFIG_64BIT
141 if (addr >= (addr_t) &dummy->regs.acrs &&
142 addr < (addr_t) &dummy->regs.orig_gpr2)
143 mask = 3;
144 #endif
145 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
146 return -EIO;
148 if (addr < (addr_t) &dummy->regs.acrs) {
150 * psw and gprs are stored on the stack
152 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
153 if (addr == (addr_t) &dummy->regs.psw.mask)
154 /* Remove per bit from user psw. */
155 tmp &= ~PSW_MASK_PER;
157 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
159 * access registers are stored in the thread structure
161 offset = addr - (addr_t) &dummy->regs.acrs;
162 #ifdef CONFIG_64BIT
164 * Very special case: old & broken 64 bit gdb reading
165 * from acrs[15]. Result is a 64 bit value. Read the
166 * 32 bit acrs[15] value and shift it by 32. Sick...
168 if (addr == (addr_t) &dummy->regs.acrs[15])
169 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
170 else
171 #endif
172 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
174 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
176 * orig_gpr2 is stored on the kernel stack
178 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
180 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
182 * floating point regs. are stored in the thread structure
184 offset = addr - (addr_t) &dummy->regs.fp_regs;
185 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
186 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
187 tmp &= (unsigned long) FPC_VALID_MASK
188 << (BITS_PER_LONG - 32);
190 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
192 * per_info is found in the thread structure
194 offset = addr - (addr_t) &dummy->regs.per_info;
195 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
197 } else
198 tmp = 0;
200 return put_user(tmp, (addr_t __user *) data);
204 * Write a word to the user area of a process at location addr. This
205 * operation does have an additional problem compared to peek_user.
206 * Stores to the program status word and on the floating point
207 * control register needs to get checked for validity.
209 static int
210 poke_user(struct task_struct *child, addr_t addr, addr_t data)
212 struct user *dummy = NULL;
213 addr_t offset, mask;
216 * Stupid gdb peeks/pokes the access registers in 64 bit with
217 * an alignment of 4. Programmers from hell indeed...
219 mask = __ADDR_MASK;
220 #ifdef CONFIG_64BIT
221 if (addr >= (addr_t) &dummy->regs.acrs &&
222 addr < (addr_t) &dummy->regs.orig_gpr2)
223 mask = 3;
224 #endif
225 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
226 return -EIO;
228 if (addr < (addr_t) &dummy->regs.acrs) {
230 * psw and gprs are stored on the stack
232 if (addr == (addr_t) &dummy->regs.psw.mask &&
233 #ifdef CONFIG_COMPAT
234 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
235 #endif
236 data != PSW_MASK_MERGE(psw_user_bits, data))
237 /* Invalid psw mask. */
238 return -EINVAL;
239 #ifndef CONFIG_64BIT
240 if (addr == (addr_t) &dummy->regs.psw.addr)
241 /* I'd like to reject addresses without the
242 high order bit but older gdb's rely on it */
243 data |= PSW_ADDR_AMODE;
244 #endif
245 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
247 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
249 * access registers are stored in the thread structure
251 offset = addr - (addr_t) &dummy->regs.acrs;
252 #ifdef CONFIG_64BIT
254 * Very special case: old & broken 64 bit gdb writing
255 * to acrs[15] with a 64 bit value. Ignore the lower
256 * half of the value and write the upper 32 bit to
257 * acrs[15]. Sick...
259 if (addr == (addr_t) &dummy->regs.acrs[15])
260 child->thread.acrs[15] = (unsigned int) (data >> 32);
261 else
262 #endif
263 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
265 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
267 * orig_gpr2 is stored on the kernel stack
269 task_pt_regs(child)->orig_gpr2 = data;
271 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
273 * floating point regs. are stored in the thread structure
275 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
276 (data & ~((unsigned long) FPC_VALID_MASK
277 << (BITS_PER_LONG - 32))) != 0)
278 return -EINVAL;
279 offset = addr - (addr_t) &dummy->regs.fp_regs;
280 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
282 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
284 * per_info is found in the thread structure
286 offset = addr - (addr_t) &dummy->regs.per_info;
287 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
291 FixPerRegisters(child);
292 return 0;
295 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
297 ptrace_area parea;
298 int copied, ret;
300 switch (request) {
301 case PTRACE_PEEKTEXT:
302 case PTRACE_PEEKDATA:
303 /* Remove high order bit from address (only for 31 bit). */
304 addr &= PSW_ADDR_INSN;
305 /* read word at location addr. */
306 return generic_ptrace_peekdata(child, addr, data);
308 case PTRACE_PEEKUSR:
309 /* read the word at location addr in the USER area. */
310 return peek_user(child, addr, data);
312 case PTRACE_POKETEXT:
313 case PTRACE_POKEDATA:
314 /* Remove high order bit from address (only for 31 bit). */
315 addr &= PSW_ADDR_INSN;
316 /* write the word at location addr. */
317 return generic_ptrace_pokedata(child, addr, data);
319 case PTRACE_POKEUSR:
320 /* write the word at location addr in the USER area */
321 return poke_user(child, addr, data);
323 case PTRACE_PEEKUSR_AREA:
324 case PTRACE_POKEUSR_AREA:
325 if (copy_from_user(&parea, (void __force __user *) addr,
326 sizeof(parea)))
327 return -EFAULT;
328 addr = parea.kernel_addr;
329 data = parea.process_addr;
330 copied = 0;
331 while (copied < parea.len) {
332 if (request == PTRACE_PEEKUSR_AREA)
333 ret = peek_user(child, addr, data);
334 else {
335 addr_t utmp;
336 if (get_user(utmp,
337 (addr_t __force __user *) data))
338 return -EFAULT;
339 ret = poke_user(child, addr, utmp);
341 if (ret)
342 return ret;
343 addr += sizeof(unsigned long);
344 data += sizeof(unsigned long);
345 copied += sizeof(unsigned long);
347 return 0;
349 return ptrace_request(child, request, addr, data);
352 #ifdef CONFIG_COMPAT
354 * Now the fun part starts... a 31 bit program running in the
355 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
356 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
357 * to handle, the difference to the 64 bit versions of the requests
358 * is that the access is done in multiples of 4 byte instead of
359 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
360 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
361 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
362 * is a 31 bit program too, the content of struct user can be
363 * emulated. A 31 bit program peeking into the struct user of
364 * a 64 bit program is a no-no.
368 * Same as peek_user but for a 31 bit program.
370 static int
371 peek_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
373 struct user32 *dummy32 = NULL;
374 per_struct32 *dummy_per32 = NULL;
375 addr_t offset;
376 __u32 tmp;
378 if (!test_thread_flag(TIF_31BIT) ||
379 (addr & 3) || addr > sizeof(struct user) - 3)
380 return -EIO;
382 if (addr < (addr_t) &dummy32->regs.acrs) {
384 * psw and gprs are stored on the stack
386 if (addr == (addr_t) &dummy32->regs.psw.mask) {
387 /* Fake a 31 bit psw mask. */
388 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
389 tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
390 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
391 /* Fake a 31 bit psw address. */
392 tmp = (__u32) task_pt_regs(child)->psw.addr |
393 PSW32_ADDR_AMODE31;
394 } else {
395 /* gpr 0-15 */
396 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
397 addr*2 + 4);
399 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
401 * access registers are stored in the thread structure
403 offset = addr - (addr_t) &dummy32->regs.acrs;
404 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
406 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
408 * orig_gpr2 is stored on the kernel stack
410 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
412 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
414 * floating point regs. are stored in the thread structure
416 offset = addr - (addr_t) &dummy32->regs.fp_regs;
417 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
419 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
421 * per_info is found in the thread structure
423 offset = addr - (addr_t) &dummy32->regs.per_info;
424 /* This is magic. See per_struct and per_struct32. */
425 if ((offset >= (addr_t) &dummy_per32->control_regs &&
426 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
427 (offset >= (addr_t) &dummy_per32->starting_addr &&
428 offset <= (addr_t) &dummy_per32->ending_addr) ||
429 offset == (addr_t) &dummy_per32->lowcore.words.address)
430 offset = offset*2 + 4;
431 else
432 offset = offset*2;
433 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
435 } else
436 tmp = 0;
438 return put_user(tmp, (__u32 __user *) data);
442 * Same as poke_user but for a 31 bit program.
444 static int
445 poke_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
447 struct user32 *dummy32 = NULL;
448 per_struct32 *dummy_per32 = NULL;
449 addr_t offset;
450 __u32 tmp;
452 if (!test_thread_flag(TIF_31BIT) ||
453 (addr & 3) || addr > sizeof(struct user32) - 3)
454 return -EIO;
456 tmp = (__u32) data;
458 if (addr < (addr_t) &dummy32->regs.acrs) {
460 * psw, gprs, acrs and orig_gpr2 are stored on the stack
462 if (addr == (addr_t) &dummy32->regs.psw.mask) {
463 /* Build a 64 bit psw mask from 31 bit mask. */
464 if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
465 /* Invalid psw mask. */
466 return -EINVAL;
467 task_pt_regs(child)->psw.mask =
468 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
469 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
470 /* Build a 64 bit psw address from 31 bit address. */
471 task_pt_regs(child)->psw.addr =
472 (__u64) tmp & PSW32_ADDR_INSN;
473 } else {
474 /* gpr 0-15 */
475 *(__u32*)((addr_t) &task_pt_regs(child)->psw
476 + addr*2 + 4) = tmp;
478 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
480 * access registers are stored in the thread structure
482 offset = addr - (addr_t) &dummy32->regs.acrs;
483 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
485 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
487 * orig_gpr2 is stored on the kernel stack
489 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
491 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
493 * floating point regs. are stored in the thread structure
495 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
496 (tmp & ~FPC_VALID_MASK) != 0)
497 /* Invalid floating point control. */
498 return -EINVAL;
499 offset = addr - (addr_t) &dummy32->regs.fp_regs;
500 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
502 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
504 * per_info is found in the thread structure.
506 offset = addr - (addr_t) &dummy32->regs.per_info;
508 * This is magic. See per_struct and per_struct32.
509 * By incident the offsets in per_struct are exactly
510 * twice the offsets in per_struct32 for all fields.
511 * The 8 byte fields need special handling though,
512 * because the second half (bytes 4-7) is needed and
513 * not the first half.
515 if ((offset >= (addr_t) &dummy_per32->control_regs &&
516 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
517 (offset >= (addr_t) &dummy_per32->starting_addr &&
518 offset <= (addr_t) &dummy_per32->ending_addr) ||
519 offset == (addr_t) &dummy_per32->lowcore.words.address)
520 offset = offset*2 + 4;
521 else
522 offset = offset*2;
523 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
527 FixPerRegisters(child);
528 return 0;
531 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
532 compat_ulong_t caddr, compat_ulong_t cdata)
534 unsigned long addr = caddr;
535 unsigned long data = cdata;
536 ptrace_area_emu31 parea;
537 int copied, ret;
539 switch (request) {
540 case PTRACE_PEEKUSR:
541 /* read the word at location addr in the USER area. */
542 return peek_user_emu31(child, addr, data);
544 case PTRACE_POKEUSR:
545 /* write the word at location addr in the USER area */
546 return poke_user_emu31(child, addr, data);
548 case PTRACE_PEEKUSR_AREA:
549 case PTRACE_POKEUSR_AREA:
550 if (copy_from_user(&parea, (void __force __user *) addr,
551 sizeof(parea)))
552 return -EFAULT;
553 addr = parea.kernel_addr;
554 data = parea.process_addr;
555 copied = 0;
556 while (copied < parea.len) {
557 if (request == PTRACE_PEEKUSR_AREA)
558 ret = peek_user_emu31(child, addr, data);
559 else {
560 __u32 utmp;
561 if (get_user(utmp,
562 (__u32 __force __user *) data))
563 return -EFAULT;
564 ret = poke_user_emu31(child, addr, utmp);
566 if (ret)
567 return ret;
568 addr += sizeof(unsigned int);
569 data += sizeof(unsigned int);
570 copied += sizeof(unsigned int);
572 return 0;
574 return compat_ptrace_request(child, request, addr, data);
576 #endif
578 asmlinkage void
579 syscall_trace(struct pt_regs *regs, int entryexit)
581 if (unlikely(current->audit_context) && entryexit)
582 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]), regs->gprs[2]);
584 if (!test_thread_flag(TIF_SYSCALL_TRACE))
585 goto out;
586 if (!(current->ptrace & PT_PTRACED))
587 goto out;
588 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
589 ? 0x80 : 0));
592 * If the debuffer has set an invalid system call number,
593 * we prepare to skip the system call restart handling.
595 if (!entryexit && regs->gprs[2] >= NR_syscalls)
596 regs->trap = -1;
599 * this isn't the same as continuing with a signal, but it will do
600 * for normal use. strace only continues with a signal if the
601 * stopping signal is not SIGTRAP. -brl
603 if (current->exit_code) {
604 send_sig(current->exit_code, current, 1);
605 current->exit_code = 0;
607 out:
608 if (unlikely(current->audit_context) && !entryexit)
609 audit_syscall_entry(test_thread_flag(TIF_31BIT)?AUDIT_ARCH_S390:AUDIT_ARCH_S390X,
610 regs->gprs[2], regs->orig_gpr2, regs->gprs[3],
611 regs->gprs[4], regs->gprs[5]);