blob: 58a064296987bd6b85fbbe04d370863c0c3ea24d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/ptrace.c
3 *
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)
8 *
9 * Based on PowerPC version
10 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
11 *
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
16 *
17 * Modified by Cort Dougan (cort@cs.nmt.edu)
18 *
19 *
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.
23 */
24
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>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070035#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
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>
Martin Schwidefsky778959d2005-06-04 15:43:30 -070043#include <asm/unistd.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020044#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080046#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "compat_ptrace.h"
48#endif
49
50static void
51FixPerRegisters(struct task_struct *task)
52{
53 struct pt_regs *regs;
54 per_struct *per_info;
55
Al Viroc7584fb2006-01-12 01:05:49 -080056 regs = task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 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;
60
61 if (per_info->single_step) {
62 per_info->control_regs.bits.starting_addr = 0;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080063#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 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;
74 }
75 /*
76 * if any of the control reg tracing bits are on
77 * we switch on per in the psw
78 */
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;
83
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;
88}
89
Roland McGrath0ac30be2008-01-26 14:11:22 +010090void user_enable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 task->thread.per_info.single_step = 1;
93 FixPerRegisters(task);
94}
95
Roland McGrath0ac30be2008-01-26 14:11:22 +010096void user_disable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 task->thread.per_info.single_step = 0;
99 FixPerRegisters(task);
100}
101
102/*
103 * Called by kernel/ptrace.c when detaching..
104 *
105 * Make sure single step bits etc are not set.
106 */
107void
108ptrace_disable(struct task_struct *child)
109{
110 /* make sure the single step bit is not set. */
Roland McGrath0ac30be2008-01-26 14:11:22 +0100111 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800114#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115# define __ADDR_MASK 3
116#else
117# define __ADDR_MASK 7
118#endif
119
120/*
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...
128 */
129static int
130peek_user(struct task_struct *child, addr_t addr, addr_t data)
131{
132 struct user *dummy = NULL;
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700133 addr_t offset, tmp, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /*
136 * Stupid gdb peeks/pokes the access registers in 64 bit with
137 * an alignment of 4. Programmers from hell...
138 */
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700139 mask = __ADDR_MASK;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800140#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700141 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -EIO;
147
148 if (addr < (addr_t) &dummy->regs.acrs) {
149 /*
150 * psw and gprs are stored on the stack
151 */
Al Viroc7584fb2006-01-12 01:05:49 -0800152 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (addr == (addr_t) &dummy->regs.psw.mask)
154 /* Remove per bit from user psw. */
155 tmp &= ~PSW_MASK_PER;
156
157 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
158 /*
159 * access registers are stored in the thread structure
160 */
161 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800162#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700163 /*
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...
167 */
168 if (addr == (addr_t) &dummy->regs.acrs[15])
169 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
170 else
171#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
173
174 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
175 /*
176 * orig_gpr2 is stored on the kernel stack
177 */
Al Viroc7584fb2006-01-12 01:05:49 -0800178 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
181 /*
182 * floating point regs. are stored in the thread structure
183 */
184 offset = addr - (addr_t) &dummy->regs.fp_regs;
185 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700186 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
187 tmp &= (unsigned long) FPC_VALID_MASK
188 << (BITS_PER_LONG - 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
191 /*
192 * per_info is found in the thread structure
193 */
194 offset = addr - (addr_t) &dummy->regs.per_info;
195 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
196
197 } else
198 tmp = 0;
199
200 return put_user(tmp, (addr_t __user *) data);
201}
202
203/*
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.
208 */
209static int
210poke_user(struct task_struct *child, addr_t addr, addr_t data)
211{
212 struct user *dummy = NULL;
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700213 addr_t offset, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /*
216 * Stupid gdb peeks/pokes the access registers in 64 bit with
217 * an alignment of 4. Programmers from hell indeed...
218 */
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700219 mask = __ADDR_MASK;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800220#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700221 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -EIO;
227
228 if (addr < (addr_t) &dummy->regs.acrs) {
229 /*
230 * psw and gprs are stored on the stack
231 */
232 if (addr == (addr_t) &dummy->regs.psw.mask &&
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800233#ifdef CONFIG_COMPAT
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100234 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#endif
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100236 data != PSW_MASK_MERGE(psw_user_bits, data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* Invalid psw mask. */
238 return -EINVAL;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800239#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 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
Al Viroc7584fb2006-01-12 01:05:49 -0800245 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
248 /*
249 * access registers are stored in the thread structure
250 */
251 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800252#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700253 /*
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...
258 */
259 if (addr == (addr_t) &dummy->regs.acrs[15])
260 child->thread.acrs[15] = (unsigned int) (data >> 32);
261 else
262#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
264
265 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
266 /*
267 * orig_gpr2 is stored on the kernel stack
268 */
Al Viroc7584fb2006-01-12 01:05:49 -0800269 task_pt_regs(child)->orig_gpr2 = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
272 /*
273 * floating point regs. are stored in the thread structure
274 */
275 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700276 (data & ~((unsigned long) FPC_VALID_MASK
277 << (BITS_PER_LONG - 32))) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -EINVAL;
279 offset = addr - (addr_t) &dummy->regs.fp_regs;
280 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
281
282 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
283 /*
284 * per_info is found in the thread structure
285 */
286 offset = addr - (addr_t) &dummy->regs.per_info;
287 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
288
289 }
290
291 FixPerRegisters(child);
292 return 0;
293}
294
295static int
296do_ptrace_normal(struct task_struct *child, long request, long addr, long data)
297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ptrace_area parea;
299 int copied, ret;
300
301 switch (request) {
302 case PTRACE_PEEKTEXT:
303 case PTRACE_PEEKDATA:
304 /* Remove high order bit from address (only for 31 bit). */
305 addr &= PSW_ADDR_INSN;
306 /* read word at location addr. */
Alexey Dobriyan76647322007-07-17 04:03:43 -0700307 return generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 case PTRACE_PEEKUSR:
310 /* read the word at location addr in the USER area. */
311 return peek_user(child, addr, data);
312
313 case PTRACE_POKETEXT:
314 case PTRACE_POKEDATA:
315 /* Remove high order bit from address (only for 31 bit). */
316 addr &= PSW_ADDR_INSN;
317 /* write the word at location addr. */
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700318 return generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 case PTRACE_POKEUSR:
321 /* write the word at location addr in the USER area */
322 return poke_user(child, addr, data);
323
324 case PTRACE_PEEKUSR_AREA:
325 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100326 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 sizeof(parea)))
328 return -EFAULT;
329 addr = parea.kernel_addr;
330 data = parea.process_addr;
331 copied = 0;
332 while (copied < parea.len) {
333 if (request == PTRACE_PEEKUSR_AREA)
334 ret = peek_user(child, addr, data);
335 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100336 addr_t utmp;
337 if (get_user(utmp,
338 (addr_t __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EFAULT;
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100340 ret = poke_user(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 if (ret)
343 return ret;
344 addr += sizeof(unsigned long);
345 data += sizeof(unsigned long);
346 copied += sizeof(unsigned long);
347 }
348 return 0;
349 }
350 return ptrace_request(child, request, addr, data);
351}
352
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800353#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/*
355 * Now the fun part starts... a 31 bit program running in the
356 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
357 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
358 * to handle, the difference to the 64 bit versions of the requests
359 * is that the access is done in multiples of 4 byte instead of
360 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
361 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
362 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
363 * is a 31 bit program too, the content of struct user can be
364 * emulated. A 31 bit program peeking into the struct user of
365 * a 64 bit program is a no-no.
366 */
367
368/*
369 * Same as peek_user but for a 31 bit program.
370 */
371static int
372peek_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
373{
374 struct user32 *dummy32 = NULL;
375 per_struct32 *dummy_per32 = NULL;
376 addr_t offset;
377 __u32 tmp;
378
379 if (!test_thread_flag(TIF_31BIT) ||
380 (addr & 3) || addr > sizeof(struct user) - 3)
381 return -EIO;
382
383 if (addr < (addr_t) &dummy32->regs.acrs) {
384 /*
385 * psw and gprs are stored on the stack
386 */
387 if (addr == (addr_t) &dummy32->regs.psw.mask) {
388 /* Fake a 31 bit psw mask. */
Al Viroc7584fb2006-01-12 01:05:49 -0800389 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100390 tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
392 /* Fake a 31 bit psw address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800393 tmp = (__u32) task_pt_regs(child)->psw.addr |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 PSW32_ADDR_AMODE31;
395 } else {
396 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800397 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 addr*2 + 4);
399 }
400 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
401 /*
402 * access registers are stored in the thread structure
403 */
404 offset = addr - (addr_t) &dummy32->regs.acrs;
405 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
406
407 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
408 /*
409 * orig_gpr2 is stored on the kernel stack
410 */
Al Viroc7584fb2006-01-12 01:05:49 -0800411 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
414 /*
415 * floating point regs. are stored in the thread structure
416 */
417 offset = addr - (addr_t) &dummy32->regs.fp_regs;
418 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
419
420 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
421 /*
422 * per_info is found in the thread structure
423 */
424 offset = addr - (addr_t) &dummy32->regs.per_info;
425 /* This is magic. See per_struct and per_struct32. */
426 if ((offset >= (addr_t) &dummy_per32->control_regs &&
427 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
428 (offset >= (addr_t) &dummy_per32->starting_addr &&
429 offset <= (addr_t) &dummy_per32->ending_addr) ||
430 offset == (addr_t) &dummy_per32->lowcore.words.address)
431 offset = offset*2 + 4;
432 else
433 offset = offset*2;
434 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
435
436 } else
437 tmp = 0;
438
439 return put_user(tmp, (__u32 __user *) data);
440}
441
442/*
443 * Same as poke_user but for a 31 bit program.
444 */
445static int
446poke_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
447{
448 struct user32 *dummy32 = NULL;
449 per_struct32 *dummy_per32 = NULL;
450 addr_t offset;
451 __u32 tmp;
452
453 if (!test_thread_flag(TIF_31BIT) ||
454 (addr & 3) || addr > sizeof(struct user32) - 3)
455 return -EIO;
456
457 tmp = (__u32) data;
458
459 if (addr < (addr_t) &dummy32->regs.acrs) {
460 /*
461 * psw, gprs, acrs and orig_gpr2 are stored on the stack
462 */
463 if (addr == (addr_t) &dummy32->regs.psw.mask) {
464 /* Build a 64 bit psw mask from 31 bit mask. */
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100465 if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* Invalid psw mask. */
467 return -EINVAL;
Al Viroc7584fb2006-01-12 01:05:49 -0800468 task_pt_regs(child)->psw.mask =
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100469 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
471 /* Build a 64 bit psw address from 31 bit address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800472 task_pt_regs(child)->psw.addr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 (__u64) tmp & PSW32_ADDR_INSN;
474 } else {
475 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800476 *(__u32*)((addr_t) &task_pt_regs(child)->psw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 + addr*2 + 4) = tmp;
478 }
479 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
480 /*
481 * access registers are stored in the thread structure
482 */
483 offset = addr - (addr_t) &dummy32->regs.acrs;
484 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
485
486 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
487 /*
488 * orig_gpr2 is stored on the kernel stack
489 */
Al Viroc7584fb2006-01-12 01:05:49 -0800490 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
493 /*
494 * floating point regs. are stored in the thread structure
495 */
496 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
497 (tmp & ~FPC_VALID_MASK) != 0)
498 /* Invalid floating point control. */
499 return -EINVAL;
500 offset = addr - (addr_t) &dummy32->regs.fp_regs;
501 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
502
503 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
504 /*
505 * per_info is found in the thread structure.
506 */
507 offset = addr - (addr_t) &dummy32->regs.per_info;
508 /*
509 * This is magic. See per_struct and per_struct32.
510 * By incident the offsets in per_struct are exactly
511 * twice the offsets in per_struct32 for all fields.
512 * The 8 byte fields need special handling though,
513 * because the second half (bytes 4-7) is needed and
514 * not the first half.
515 */
516 if ((offset >= (addr_t) &dummy_per32->control_regs &&
517 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
518 (offset >= (addr_t) &dummy_per32->starting_addr &&
519 offset <= (addr_t) &dummy_per32->ending_addr) ||
520 offset == (addr_t) &dummy_per32->lowcore.words.address)
521 offset = offset*2 + 4;
522 else
523 offset = offset*2;
524 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
525
526 }
527
528 FixPerRegisters(child);
529 return 0;
530}
531
532static int
533do_ptrace_emu31(struct task_struct *child, long request, long addr, long data)
534{
535 unsigned int tmp; /* 4 bytes !! */
536 ptrace_area_emu31 parea;
537 int copied, ret;
538
539 switch (request) {
540 case PTRACE_PEEKTEXT:
541 case PTRACE_PEEKDATA:
542 /* read word at location addr. */
543 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
544 if (copied != sizeof(tmp))
545 return -EIO;
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100546 return put_user(tmp, (unsigned int __force __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 case PTRACE_PEEKUSR:
549 /* read the word at location addr in the USER area. */
550 return peek_user_emu31(child, addr, data);
551
552 case PTRACE_POKETEXT:
553 case PTRACE_POKEDATA:
554 /* write the word at location addr. */
555 tmp = data;
556 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
557 if (copied != sizeof(tmp))
558 return -EIO;
559 return 0;
560
561 case PTRACE_POKEUSR:
562 /* write the word at location addr in the USER area */
563 return poke_user_emu31(child, addr, data);
564
565 case PTRACE_PEEKUSR_AREA:
566 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100567 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 sizeof(parea)))
569 return -EFAULT;
570 addr = parea.kernel_addr;
571 data = parea.process_addr;
572 copied = 0;
573 while (copied < parea.len) {
574 if (request == PTRACE_PEEKUSR_AREA)
575 ret = peek_user_emu31(child, addr, data);
576 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100577 __u32 utmp;
578 if (get_user(utmp,
579 (__u32 __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -EFAULT;
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100581 ret = poke_user_emu31(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 if (ret)
584 return ret;
585 addr += sizeof(unsigned int);
586 data += sizeof(unsigned int);
587 copied += sizeof(unsigned int);
588 }
589 return 0;
590 case PTRACE_GETEVENTMSG:
591 return put_user((__u32) child->ptrace_message,
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100592 (unsigned int __force __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 case PTRACE_GETSIGINFO:
594 if (child->last_siginfo == NULL)
595 return -EINVAL;
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100596 return copy_siginfo_to_user32((compat_siginfo_t
597 __force __user *) data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 child->last_siginfo);
599 case PTRACE_SETSIGINFO:
600 if (child->last_siginfo == NULL)
601 return -EINVAL;
602 return copy_siginfo_from_user32(child->last_siginfo,
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100603 (compat_siginfo_t
604 __force __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606 return ptrace_request(child, request, addr, data);
607}
608#endif
609
610#define PT32_IEEE_IP 0x13c
611
612static int
613do_ptrace(struct task_struct *child, long request, long addr, long data)
614{
615 int ret;
616
617 if (request == PTRACE_ATTACH)
618 return ptrace_attach(child);
619
620 /*
621 * Special cases to get/store the ieee instructions pointer.
622 */
623 if (child == current) {
624 if (request == PTRACE_PEEKUSR && addr == PT_IEEE_IP)
625 return peek_user(child, addr, data);
626 if (request == PTRACE_POKEUSR && addr == PT_IEEE_IP)
627 return poke_user(child, addr, data);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800628#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (request == PTRACE_PEEKUSR &&
630 addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
631 return peek_user_emu31(child, addr, data);
632 if (request == PTRACE_POKEUSR &&
633 addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
634 return poke_user_emu31(child, addr, data);
635#endif
636 }
637
638 ret = ptrace_check_attach(child, request == PTRACE_KILL);
639 if (ret < 0)
640 return ret;
641
642 switch (request) {
643 case PTRACE_SYSCALL:
644 /* continue and stop at next (return from) syscall */
645 case PTRACE_CONT:
646 /* restart after signal. */
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700647 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return -EIO;
649 if (request == PTRACE_SYSCALL)
650 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
651 else
652 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
653 child->exit_code = data;
654 /* make sure the single step bit is not set. */
Roland McGrath0ac30be2008-01-26 14:11:22 +0100655 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 wake_up_process(child);
657 return 0;
658
659 case PTRACE_KILL:
660 /*
661 * make the child exit. Best I can do is send it a sigkill.
662 * perhaps it should be put in the status that it wants to
663 * exit.
664 */
665 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
666 return 0;
667 child->exit_code = SIGKILL;
668 /* make sure the single step bit is not set. */
Roland McGrath0ac30be2008-01-26 14:11:22 +0100669 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 wake_up_process(child);
671 return 0;
672
673 case PTRACE_SINGLESTEP:
674 /* set the trap flag. */
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700675 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -EIO;
677 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
678 child->exit_code = data;
Roland McGrath0ac30be2008-01-26 14:11:22 +0100679 user_enable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* give it a chance to run. */
681 wake_up_process(child);
682 return 0;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* Do requests that differ for 31/64 bit */
685 default:
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800686#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (test_thread_flag(TIF_31BIT))
688 return do_ptrace_emu31(child, request, addr, data);
689#endif
690 return do_ptrace_normal(child, request, addr, data);
691 }
692 /* Not reached. */
693 return -EIO;
694}
695
696asmlinkage long
697sys_ptrace(long request, long pid, long addr, long data)
698{
699 struct task_struct *child;
700 int ret;
701
702 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (request == PTRACE_TRACEME) {
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800704 ret = ptrace_traceme();
705 goto out;
706 }
707
708 child = ptrace_get_task_struct(pid);
709 if (IS_ERR(child)) {
710 ret = PTR_ERR(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 goto out;
712 }
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ret = do_ptrace(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 put_task_struct(child);
716out:
717 unlock_kernel();
718 return ret;
719}
720
721asmlinkage void
722syscall_trace(struct pt_regs *regs, int entryexit)
723{
2fd6f582005-04-29 16:08:28 +0100724 if (unlikely(current->audit_context) && entryexit)
Al Viro5411be52006-03-29 20:23:36 -0500725 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]), regs->gprs[2]);
2fd6f582005-04-29 16:08:28 +0100726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!test_thread_flag(TIF_SYSCALL_TRACE))
2fd6f582005-04-29 16:08:28 +0100728 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!(current->ptrace & PT_PTRACED))
2fd6f582005-04-29 16:08:28 +0100730 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
732 ? 0x80 : 0));
733
734 /*
Bodo Stroesserc5c3a6d2005-06-04 15:43:32 -0700735 * If the debuffer has set an invalid system call number,
736 * we prepare to skip the system call restart handling.
737 */
738 if (!entryexit && regs->gprs[2] >= NR_syscalls)
739 regs->trap = -1;
740
741 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * this isn't the same as continuing with a signal, but it will do
743 * for normal use. strace only continues with a signal if the
744 * stopping signal is not SIGTRAP. -brl
745 */
746 if (current->exit_code) {
747 send_sig(current->exit_code, current, 1);
748 current->exit_code = 0;
749 }
2fd6f582005-04-29 16:08:28 +0100750 out:
751 if (unlikely(current->audit_context) && !entryexit)
Al Viro5411be52006-03-29 20:23:36 -0500752 audit_syscall_entry(test_thread_flag(TIF_31BIT)?AUDIT_ARCH_S390:AUDIT_ARCH_S390X,
2fd6f582005-04-29 16:08:28 +0100753 regs->gprs[2], regs->orig_gpr2, regs->gprs[3],
754 regs->gprs[4], regs->gprs[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}