blob: 99eef179e9036c7e1a82aaa6dbc31fe7d6a1780e [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>
Martin Schwidefsky63506c42008-07-14 09:58:54 +020036#include <linux/elf.h>
37#include <linux/regset.h>
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +020038#include <linux/tracehook.h>
Heiko Carstens77575912009-06-12 10:26:25 +020039#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/segment.h>
41#include <asm/page.h>
42#include <asm/pgtable.h>
43#include <asm/pgalloc.h>
44#include <asm/system.h>
45#include <asm/uaccess.h>
Martin Schwidefsky778959d2005-06-04 15:43:30 -070046#include <asm/unistd.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020047#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080049#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "compat_ptrace.h"
51#endif
52
Martin Schwidefsky63506c42008-07-14 09:58:54 +020053enum s390_regset {
54 REGSET_GENERAL,
55 REGSET_FP,
56};
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void
59FixPerRegisters(struct task_struct *task)
60{
61 struct pt_regs *regs;
62 per_struct *per_info;
63
Al Viroc7584fb2006-01-12 01:05:49 -080064 regs = task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 per_info = (per_struct *) &task->thread.per_info;
66 per_info->control_regs.bits.em_instruction_fetch =
67 per_info->single_step | per_info->instruction_fetch;
68
69 if (per_info->single_step) {
70 per_info->control_regs.bits.starting_addr = 0;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080071#ifdef CONFIG_COMPAT
Heiko Carstens77575912009-06-12 10:26:25 +020072 if (is_compat_task())
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
74 else
75#endif
76 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
77 } else {
78 per_info->control_regs.bits.starting_addr =
79 per_info->starting_addr;
80 per_info->control_regs.bits.ending_addr =
81 per_info->ending_addr;
82 }
83 /*
84 * if any of the control reg tracing bits are on
85 * we switch on per in the psw
86 */
87 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
88 regs->psw.mask |= PSW_MASK_PER;
89 else
90 regs->psw.mask &= ~PSW_MASK_PER;
91
92 if (per_info->control_regs.bits.em_storage_alteration)
93 per_info->control_regs.bits.storage_alt_space_ctl = 1;
94 else
95 per_info->control_regs.bits.storage_alt_space_ctl = 0;
96}
97
Roland McGrath0ac30be2008-01-26 14:11:22 +010098void user_enable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 task->thread.per_info.single_step = 1;
101 FixPerRegisters(task);
102}
103
Roland McGrath0ac30be2008-01-26 14:11:22 +0100104void user_disable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 task->thread.per_info.single_step = 0;
107 FixPerRegisters(task);
108}
109
110/*
111 * Called by kernel/ptrace.c when detaching..
112 *
113 * Make sure single step bits etc are not set.
114 */
115void
116ptrace_disable(struct task_struct *child)
117{
118 /* make sure the single step bit is not set. */
Roland McGrath0ac30be2008-01-26 14:11:22 +0100119 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800122#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123# define __ADDR_MASK 3
124#else
125# define __ADDR_MASK 7
126#endif
127
128/*
129 * Read the word at offset addr from the user area of a process. The
130 * trouble here is that the information is littered over different
131 * locations. The process registers are found on the kernel stack,
132 * the floating point stuff and the trace settings are stored in
133 * the task structure. In addition the different structures in
134 * struct user contain pad bytes that should be read as zeroes.
135 * Lovely...
136 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200137static unsigned long __peek_user(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 struct user *dummy = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200140 addr_t offset, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (addr < (addr_t) &dummy->regs.acrs) {
143 /*
144 * psw and gprs are stored on the stack
145 */
Al Viroc7584fb2006-01-12 01:05:49 -0800146 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (addr == (addr_t) &dummy->regs.psw.mask)
148 /* Remove per bit from user psw. */
149 tmp &= ~PSW_MASK_PER;
150
151 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
152 /*
153 * access registers are stored in the thread structure
154 */
155 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800156#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700157 /*
158 * Very special case: old & broken 64 bit gdb reading
159 * from acrs[15]. Result is a 64 bit value. Read the
160 * 32 bit acrs[15] value and shift it by 32. Sick...
161 */
162 if (addr == (addr_t) &dummy->regs.acrs[15])
163 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
164 else
165#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
167
168 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
169 /*
170 * orig_gpr2 is stored on the kernel stack
171 */
Al Viroc7584fb2006-01-12 01:05:49 -0800172 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200174 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
175 /*
176 * prevent reads of padding hole between
177 * orig_gpr2 and fp_regs on s390.
178 */
179 tmp = 0;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
182 /*
183 * floating point regs. are stored in the thread structure
184 */
185 offset = addr - (addr_t) &dummy->regs.fp_regs;
186 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700187 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
188 tmp &= (unsigned long) FPC_VALID_MASK
189 << (BITS_PER_LONG - 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
192 /*
193 * per_info is found in the thread structure
194 */
195 offset = addr - (addr_t) &dummy->regs.per_info;
196 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
197
198 } else
199 tmp = 0;
200
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200201 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static int
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200205peek_user(struct task_struct *child, addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200207 addr_t tmp, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /*
210 * Stupid gdb peeks/pokes the access registers in 64 bit with
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200211 * an alignment of 4. Programmers from hell...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700213 mask = __ADDR_MASK;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800214#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100215 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
216 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700217 mask = 3;
218#endif
219 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -EIO;
221
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200222 tmp = __peek_user(child, addr);
223 return put_user(tmp, (addr_t __user *) data);
224}
225
226/*
227 * Write a word to the user area of a process at location addr. This
228 * operation does have an additional problem compared to peek_user.
229 * Stores to the program status word and on the floating point
230 * control register needs to get checked for validity.
231 */
232static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
233{
234 struct user *dummy = NULL;
235 addr_t offset;
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (addr < (addr_t) &dummy->regs.acrs) {
238 /*
239 * psw and gprs are stored on the stack
240 */
241 if (addr == (addr_t) &dummy->regs.psw.mask &&
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800242#ifdef CONFIG_COMPAT
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100243 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244#endif
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100245 data != PSW_MASK_MERGE(psw_user_bits, data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* Invalid psw mask. */
247 return -EINVAL;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800248#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (addr == (addr_t) &dummy->regs.psw.addr)
250 /* I'd like to reject addresses without the
251 high order bit but older gdb's rely on it */
252 data |= PSW_ADDR_AMODE;
253#endif
Al Viroc7584fb2006-01-12 01:05:49 -0800254 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
257 /*
258 * access registers are stored in the thread structure
259 */
260 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800261#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700262 /*
263 * Very special case: old & broken 64 bit gdb writing
264 * to acrs[15] with a 64 bit value. Ignore the lower
265 * half of the value and write the upper 32 bit to
266 * acrs[15]. Sick...
267 */
268 if (addr == (addr_t) &dummy->regs.acrs[15])
269 child->thread.acrs[15] = (unsigned int) (data >> 32);
270 else
271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
273
274 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
275 /*
276 * orig_gpr2 is stored on the kernel stack
277 */
Al Viroc7584fb2006-01-12 01:05:49 -0800278 task_pt_regs(child)->orig_gpr2 = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200280 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
281 /*
282 * prevent writes of padding hole between
283 * orig_gpr2 and fp_regs on s390.
284 */
285 return 0;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
288 /*
289 * floating point regs. are stored in the thread structure
290 */
291 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700292 (data & ~((unsigned long) FPC_VALID_MASK
293 << (BITS_PER_LONG - 32))) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -EINVAL;
295 offset = addr - (addr_t) &dummy->regs.fp_regs;
296 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
297
298 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
299 /*
300 * per_info is found in the thread structure
301 */
302 offset = addr - (addr_t) &dummy->regs.per_info;
303 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
304
305 }
306
307 FixPerRegisters(child);
308 return 0;
309}
310
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200311static int
312poke_user(struct task_struct *child, addr_t addr, addr_t data)
313{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200314 addr_t mask;
315
316 /*
317 * Stupid gdb peeks/pokes the access registers in 64 bit with
318 * an alignment of 4. Programmers from hell indeed...
319 */
320 mask = __ADDR_MASK;
321#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100322 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
323 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200324 mask = 3;
325#endif
326 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
327 return -EIO;
328
329 return __poke_user(child, addr, data);
330}
331
Roland McGrathb499d762008-05-07 09:22:57 +0200332long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 ptrace_area parea;
335 int copied, ret;
336
337 switch (request) {
338 case PTRACE_PEEKTEXT:
339 case PTRACE_PEEKDATA:
340 /* Remove high order bit from address (only for 31 bit). */
341 addr &= PSW_ADDR_INSN;
342 /* read word at location addr. */
Alexey Dobriyan76647322007-07-17 04:03:43 -0700343 return generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 case PTRACE_PEEKUSR:
346 /* read the word at location addr in the USER area. */
347 return peek_user(child, addr, data);
348
349 case PTRACE_POKETEXT:
350 case PTRACE_POKEDATA:
351 /* Remove high order bit from address (only for 31 bit). */
352 addr &= PSW_ADDR_INSN;
353 /* write the word at location addr. */
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700354 return generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 case PTRACE_POKEUSR:
357 /* write the word at location addr in the USER area */
358 return poke_user(child, addr, data);
359
360 case PTRACE_PEEKUSR_AREA:
361 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100362 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sizeof(parea)))
364 return -EFAULT;
365 addr = parea.kernel_addr;
366 data = parea.process_addr;
367 copied = 0;
368 while (copied < parea.len) {
369 if (request == PTRACE_PEEKUSR_AREA)
370 ret = peek_user(child, addr, data);
371 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100372 addr_t utmp;
373 if (get_user(utmp,
374 (addr_t __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EFAULT;
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100376 ret = poke_user(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 if (ret)
379 return ret;
380 addr += sizeof(unsigned long);
381 data += sizeof(unsigned long);
382 copied += sizeof(unsigned long);
383 }
384 return 0;
385 }
386 return ptrace_request(child, request, addr, data);
387}
388
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800389#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
391 * Now the fun part starts... a 31 bit program running in the
392 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
393 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
394 * to handle, the difference to the 64 bit versions of the requests
395 * is that the access is done in multiples of 4 byte instead of
396 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
397 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
398 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
399 * is a 31 bit program too, the content of struct user can be
400 * emulated. A 31 bit program peeking into the struct user of
401 * a 64 bit program is a no-no.
402 */
403
404/*
405 * Same as peek_user but for a 31 bit program.
406 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200407static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct user32 *dummy32 = NULL;
410 per_struct32 *dummy_per32 = NULL;
411 addr_t offset;
412 __u32 tmp;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (addr < (addr_t) &dummy32->regs.acrs) {
415 /*
416 * psw and gprs are stored on the stack
417 */
418 if (addr == (addr_t) &dummy32->regs.psw.mask) {
419 /* Fake a 31 bit psw mask. */
Al Viroc7584fb2006-01-12 01:05:49 -0800420 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100421 tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
423 /* Fake a 31 bit psw address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800424 tmp = (__u32) task_pt_regs(child)->psw.addr |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 PSW32_ADDR_AMODE31;
426 } else {
427 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800428 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 addr*2 + 4);
430 }
431 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
432 /*
433 * access registers are stored in the thread structure
434 */
435 offset = addr - (addr_t) &dummy32->regs.acrs;
436 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
437
438 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
439 /*
440 * orig_gpr2 is stored on the kernel stack
441 */
Al Viroc7584fb2006-01-12 01:05:49 -0800442 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200444 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
445 /*
446 * prevent reads of padding hole between
447 * orig_gpr2 and fp_regs on s390.
448 */
449 tmp = 0;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
452 /*
453 * floating point regs. are stored in the thread structure
454 */
455 offset = addr - (addr_t) &dummy32->regs.fp_regs;
456 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
457
458 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
459 /*
460 * per_info is found in the thread structure
461 */
462 offset = addr - (addr_t) &dummy32->regs.per_info;
463 /* This is magic. See per_struct and per_struct32. */
464 if ((offset >= (addr_t) &dummy_per32->control_regs &&
465 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
466 (offset >= (addr_t) &dummy_per32->starting_addr &&
467 offset <= (addr_t) &dummy_per32->ending_addr) ||
468 offset == (addr_t) &dummy_per32->lowcore.words.address)
469 offset = offset*2 + 4;
470 else
471 offset = offset*2;
472 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
473
474 } else
475 tmp = 0;
476
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200477 return tmp;
478}
479
480static int peek_user_compat(struct task_struct *child,
481 addr_t addr, addr_t data)
482{
483 __u32 tmp;
484
Heiko Carstens77575912009-06-12 10:26:25 +0200485 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200486 return -EIO;
487
488 tmp = __peek_user_compat(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return put_user(tmp, (__u32 __user *) data);
490}
491
492/*
493 * Same as poke_user but for a 31 bit program.
494 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200495static int __poke_user_compat(struct task_struct *child,
496 addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct user32 *dummy32 = NULL;
499 per_struct32 *dummy_per32 = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200500 __u32 tmp = (__u32) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 addr_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (addr < (addr_t) &dummy32->regs.acrs) {
504 /*
505 * psw, gprs, acrs and orig_gpr2 are stored on the stack
506 */
507 if (addr == (addr_t) &dummy32->regs.psw.mask) {
508 /* Build a 64 bit psw mask from 31 bit mask. */
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100509 if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Invalid psw mask. */
511 return -EINVAL;
Al Viroc7584fb2006-01-12 01:05:49 -0800512 task_pt_regs(child)->psw.mask =
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100513 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
515 /* Build a 64 bit psw address from 31 bit address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800516 task_pt_regs(child)->psw.addr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 (__u64) tmp & PSW32_ADDR_INSN;
518 } else {
519 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800520 *(__u32*)((addr_t) &task_pt_regs(child)->psw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 + addr*2 + 4) = tmp;
522 }
523 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
524 /*
525 * access registers are stored in the thread structure
526 */
527 offset = addr - (addr_t) &dummy32->regs.acrs;
528 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
529
530 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
531 /*
532 * orig_gpr2 is stored on the kernel stack
533 */
Al Viroc7584fb2006-01-12 01:05:49 -0800534 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200536 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
537 /*
538 * prevent writess of padding hole between
539 * orig_gpr2 and fp_regs on s390.
540 */
541 return 0;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
544 /*
545 * floating point regs. are stored in the thread structure
546 */
547 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
548 (tmp & ~FPC_VALID_MASK) != 0)
549 /* Invalid floating point control. */
550 return -EINVAL;
551 offset = addr - (addr_t) &dummy32->regs.fp_regs;
552 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
553
554 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
555 /*
556 * per_info is found in the thread structure.
557 */
558 offset = addr - (addr_t) &dummy32->regs.per_info;
559 /*
560 * This is magic. See per_struct and per_struct32.
561 * By incident the offsets in per_struct are exactly
562 * twice the offsets in per_struct32 for all fields.
563 * The 8 byte fields need special handling though,
564 * because the second half (bytes 4-7) is needed and
565 * not the first half.
566 */
567 if ((offset >= (addr_t) &dummy_per32->control_regs &&
568 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
569 (offset >= (addr_t) &dummy_per32->starting_addr &&
570 offset <= (addr_t) &dummy_per32->ending_addr) ||
571 offset == (addr_t) &dummy_per32->lowcore.words.address)
572 offset = offset*2 + 4;
573 else
574 offset = offset*2;
575 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
576
577 }
578
579 FixPerRegisters(child);
580 return 0;
581}
582
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200583static int poke_user_compat(struct task_struct *child,
584 addr_t addr, addr_t data)
585{
Heiko Carstens77575912009-06-12 10:26:25 +0200586 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user32) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200587 return -EIO;
588
589 return __poke_user_compat(child, addr, data);
590}
591
Roland McGrathb499d762008-05-07 09:22:57 +0200592long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
593 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Roland McGrathb499d762008-05-07 09:22:57 +0200595 unsigned long addr = caddr;
596 unsigned long data = cdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 ptrace_area_emu31 parea;
598 int copied, ret;
599
600 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 case PTRACE_PEEKUSR:
602 /* read the word at location addr in the USER area. */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200603 return peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 case PTRACE_POKEUSR:
606 /* write the word at location addr in the USER area */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200607 return poke_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 case PTRACE_PEEKUSR_AREA:
610 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100611 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sizeof(parea)))
613 return -EFAULT;
614 addr = parea.kernel_addr;
615 data = parea.process_addr;
616 copied = 0;
617 while (copied < parea.len) {
618 if (request == PTRACE_PEEKUSR_AREA)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200619 ret = peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100621 __u32 utmp;
622 if (get_user(utmp,
623 (__u32 __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return -EFAULT;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200625 ret = poke_user_compat(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 if (ret)
628 return ret;
629 addr += sizeof(unsigned int);
630 data += sizeof(unsigned int);
631 copied += sizeof(unsigned int);
632 }
633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Roland McGrathb499d762008-05-07 09:22:57 +0200635 return compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637#endif
638
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200639asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200641 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /*
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200644 * The sysc_tracesys code in entry.S stored the system
645 * call number to gprs[2].
Bodo Stroesserc5c3a6d2005-06-04 15:43:32 -0700646 */
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200647 ret = regs->gprs[2];
648 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
649 (tracehook_report_syscall_entry(regs) ||
650 regs->gprs[2] >= NR_syscalls)) {
651 /*
652 * Tracing decided this syscall should not happen or the
653 * debugger stored an invalid system call number. Skip
654 * the system call and the system call restart handling.
655 */
Martin Schwidefsky59da2132008-11-27 11:05:55 +0100656 regs->svcnr = 0;
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200657 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200659
660 if (unlikely(current->audit_context))
Heiko Carstens77575912009-06-12 10:26:25 +0200661 audit_syscall_entry(is_compat_task() ?
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200662 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
663 regs->gprs[2], regs->orig_gpr2,
664 regs->gprs[3], regs->gprs[4],
665 regs->gprs[5]);
666 return ret;
667}
668
669asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
670{
671 if (unlikely(current->audit_context))
672 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
673 regs->gprs[2]);
674
675 if (test_thread_flag(TIF_SYSCALL_TRACE))
676 tracehook_report_syscall_exit(regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200678
679/*
680 * user_regset definitions.
681 */
682
683static int s390_regs_get(struct task_struct *target,
684 const struct user_regset *regset,
685 unsigned int pos, unsigned int count,
686 void *kbuf, void __user *ubuf)
687{
688 if (target == current)
689 save_access_regs(target->thread.acrs);
690
691 if (kbuf) {
692 unsigned long *k = kbuf;
693 while (count > 0) {
694 *k++ = __peek_user(target, pos);
695 count -= sizeof(*k);
696 pos += sizeof(*k);
697 }
698 } else {
699 unsigned long __user *u = ubuf;
700 while (count > 0) {
701 if (__put_user(__peek_user(target, pos), u++))
702 return -EFAULT;
703 count -= sizeof(*u);
704 pos += sizeof(*u);
705 }
706 }
707 return 0;
708}
709
710static int s390_regs_set(struct task_struct *target,
711 const struct user_regset *regset,
712 unsigned int pos, unsigned int count,
713 const void *kbuf, const void __user *ubuf)
714{
715 int rc = 0;
716
717 if (target == current)
718 save_access_regs(target->thread.acrs);
719
720 if (kbuf) {
721 const unsigned long *k = kbuf;
722 while (count > 0 && !rc) {
723 rc = __poke_user(target, pos, *k++);
724 count -= sizeof(*k);
725 pos += sizeof(*k);
726 }
727 } else {
728 const unsigned long __user *u = ubuf;
729 while (count > 0 && !rc) {
730 unsigned long word;
731 rc = __get_user(word, u++);
732 if (rc)
733 break;
734 rc = __poke_user(target, pos, word);
735 count -= sizeof(*u);
736 pos += sizeof(*u);
737 }
738 }
739
740 if (rc == 0 && target == current)
741 restore_access_regs(target->thread.acrs);
742
743 return rc;
744}
745
746static int s390_fpregs_get(struct task_struct *target,
747 const struct user_regset *regset, unsigned int pos,
748 unsigned int count, void *kbuf, void __user *ubuf)
749{
750 if (target == current)
751 save_fp_regs(&target->thread.fp_regs);
752
753 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
754 &target->thread.fp_regs, 0, -1);
755}
756
757static int s390_fpregs_set(struct task_struct *target,
758 const struct user_regset *regset, unsigned int pos,
759 unsigned int count, const void *kbuf,
760 const void __user *ubuf)
761{
762 int rc = 0;
763
764 if (target == current)
765 save_fp_regs(&target->thread.fp_regs);
766
767 /* If setting FPC, must validate it first. */
768 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
769 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
770 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
771 0, offsetof(s390_fp_regs, fprs));
772 if (rc)
773 return rc;
774 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
775 return -EINVAL;
776 target->thread.fp_regs.fpc = fpc[0];
777 }
778
779 if (rc == 0 && count > 0)
780 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
781 target->thread.fp_regs.fprs,
782 offsetof(s390_fp_regs, fprs), -1);
783
784 if (rc == 0 && target == current)
785 restore_fp_regs(&target->thread.fp_regs);
786
787 return rc;
788}
789
790static const struct user_regset s390_regsets[] = {
791 [REGSET_GENERAL] = {
792 .core_note_type = NT_PRSTATUS,
793 .n = sizeof(s390_regs) / sizeof(long),
794 .size = sizeof(long),
795 .align = sizeof(long),
796 .get = s390_regs_get,
797 .set = s390_regs_set,
798 },
799 [REGSET_FP] = {
800 .core_note_type = NT_PRFPREG,
801 .n = sizeof(s390_fp_regs) / sizeof(long),
802 .size = sizeof(long),
803 .align = sizeof(long),
804 .get = s390_fpregs_get,
805 .set = s390_fpregs_set,
806 },
807};
808
809static const struct user_regset_view user_s390_view = {
810 .name = UTS_MACHINE,
811 .e_machine = EM_S390,
812 .regsets = s390_regsets,
813 .n = ARRAY_SIZE(s390_regsets)
814};
815
816#ifdef CONFIG_COMPAT
817static int s390_compat_regs_get(struct task_struct *target,
818 const struct user_regset *regset,
819 unsigned int pos, unsigned int count,
820 void *kbuf, void __user *ubuf)
821{
822 if (target == current)
823 save_access_regs(target->thread.acrs);
824
825 if (kbuf) {
826 compat_ulong_t *k = kbuf;
827 while (count > 0) {
828 *k++ = __peek_user_compat(target, pos);
829 count -= sizeof(*k);
830 pos += sizeof(*k);
831 }
832 } else {
833 compat_ulong_t __user *u = ubuf;
834 while (count > 0) {
835 if (__put_user(__peek_user_compat(target, pos), u++))
836 return -EFAULT;
837 count -= sizeof(*u);
838 pos += sizeof(*u);
839 }
840 }
841 return 0;
842}
843
844static int s390_compat_regs_set(struct task_struct *target,
845 const struct user_regset *regset,
846 unsigned int pos, unsigned int count,
847 const void *kbuf, const void __user *ubuf)
848{
849 int rc = 0;
850
851 if (target == current)
852 save_access_regs(target->thread.acrs);
853
854 if (kbuf) {
855 const compat_ulong_t *k = kbuf;
856 while (count > 0 && !rc) {
857 rc = __poke_user_compat(target, pos, *k++);
858 count -= sizeof(*k);
859 pos += sizeof(*k);
860 }
861 } else {
862 const compat_ulong_t __user *u = ubuf;
863 while (count > 0 && !rc) {
864 compat_ulong_t word;
865 rc = __get_user(word, u++);
866 if (rc)
867 break;
868 rc = __poke_user_compat(target, pos, word);
869 count -= sizeof(*u);
870 pos += sizeof(*u);
871 }
872 }
873
874 if (rc == 0 && target == current)
875 restore_access_regs(target->thread.acrs);
876
877 return rc;
878}
879
880static const struct user_regset s390_compat_regsets[] = {
881 [REGSET_GENERAL] = {
882 .core_note_type = NT_PRSTATUS,
883 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
884 .size = sizeof(compat_long_t),
885 .align = sizeof(compat_long_t),
886 .get = s390_compat_regs_get,
887 .set = s390_compat_regs_set,
888 },
889 [REGSET_FP] = {
890 .core_note_type = NT_PRFPREG,
891 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
892 .size = sizeof(compat_long_t),
893 .align = sizeof(compat_long_t),
894 .get = s390_fpregs_get,
895 .set = s390_fpregs_set,
896 },
897};
898
899static const struct user_regset_view user_s390_compat_view = {
900 .name = "s390",
901 .e_machine = EM_S390,
902 .regsets = s390_compat_regsets,
903 .n = ARRAY_SIZE(s390_compat_regsets)
904};
905#endif
906
907const struct user_regset_view *task_user_regset_view(struct task_struct *task)
908{
909#ifdef CONFIG_COMPAT
910 if (test_tsk_thread_flag(task, TIF_31BIT))
911 return &user_s390_compat_view;
912#endif
913 return &user_s390_view;
914}