blob: b6fc1ae2ffcbfe85dfa5f2b3d720f682e44ff61a [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 Carstensbcf5cef2009-06-12 10:26:26 +020039#include <linux/seccomp.h>
40#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/segment.h>
42#include <asm/page.h>
43#include <asm/pgtable.h>
44#include <asm/pgalloc.h>
45#include <asm/system.h>
46#include <asm/uaccess.h>
Martin Schwidefsky778959d2005-06-04 15:43:30 -070047#include <asm/unistd.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020048#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080050#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "compat_ptrace.h"
52#endif
53
Martin Schwidefsky63506c42008-07-14 09:58:54 +020054enum s390_regset {
55 REGSET_GENERAL,
56 REGSET_FP,
57};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void
60FixPerRegisters(struct task_struct *task)
61{
62 struct pt_regs *regs;
63 per_struct *per_info;
64
Al Viroc7584fb2006-01-12 01:05:49 -080065 regs = task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 per_info = (per_struct *) &task->thread.per_info;
67 per_info->control_regs.bits.em_instruction_fetch =
68 per_info->single_step | per_info->instruction_fetch;
69
70 if (per_info->single_step) {
71 per_info->control_regs.bits.starting_addr = 0;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080072#ifdef CONFIG_COMPAT
Heiko Carstens77575912009-06-12 10:26:25 +020073 if (is_compat_task())
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
75 else
76#endif
77 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
78 } else {
79 per_info->control_regs.bits.starting_addr =
80 per_info->starting_addr;
81 per_info->control_regs.bits.ending_addr =
82 per_info->ending_addr;
83 }
84 /*
85 * if any of the control reg tracing bits are on
86 * we switch on per in the psw
87 */
88 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
89 regs->psw.mask |= PSW_MASK_PER;
90 else
91 regs->psw.mask &= ~PSW_MASK_PER;
92
93 if (per_info->control_regs.bits.em_storage_alteration)
94 per_info->control_regs.bits.storage_alt_space_ctl = 1;
95 else
96 per_info->control_regs.bits.storage_alt_space_ctl = 0;
97}
98
Roland McGrath0ac30be2008-01-26 14:11:22 +010099void user_enable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 task->thread.per_info.single_step = 1;
102 FixPerRegisters(task);
103}
104
Roland McGrath0ac30be2008-01-26 14:11:22 +0100105void user_disable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 task->thread.per_info.single_step = 0;
108 FixPerRegisters(task);
109}
110
111/*
112 * Called by kernel/ptrace.c when detaching..
113 *
114 * Make sure single step bits etc are not set.
115 */
116void
117ptrace_disable(struct task_struct *child)
118{
119 /* make sure the single step bit is not set. */
Roland McGrath0ac30be2008-01-26 14:11:22 +0100120 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800123#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124# define __ADDR_MASK 3
125#else
126# define __ADDR_MASK 7
127#endif
128
129/*
130 * Read the word at offset addr from the user area of a process. The
131 * trouble here is that the information is littered over different
132 * locations. The process registers are found on the kernel stack,
133 * the floating point stuff and the trace settings are stored in
134 * the task structure. In addition the different structures in
135 * struct user contain pad bytes that should be read as zeroes.
136 * Lovely...
137 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200138static unsigned long __peek_user(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct user *dummy = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200141 addr_t offset, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if (addr < (addr_t) &dummy->regs.acrs) {
144 /*
145 * psw and gprs are stored on the stack
146 */
Al Viroc7584fb2006-01-12 01:05:49 -0800147 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (addr == (addr_t) &dummy->regs.psw.mask)
149 /* Remove per bit from user psw. */
150 tmp &= ~PSW_MASK_PER;
151
152 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
153 /*
154 * access registers are stored in the thread structure
155 */
156 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800157#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700158 /*
159 * Very special case: old & broken 64 bit gdb reading
160 * from acrs[15]. Result is a 64 bit value. Read the
161 * 32 bit acrs[15] value and shift it by 32. Sick...
162 */
163 if (addr == (addr_t) &dummy->regs.acrs[15])
164 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
165 else
166#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
168
169 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
170 /*
171 * orig_gpr2 is stored on the kernel stack
172 */
Al Viroc7584fb2006-01-12 01:05:49 -0800173 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200175 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
176 /*
177 * prevent reads of padding hole between
178 * orig_gpr2 and fp_regs on s390.
179 */
180 tmp = 0;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
183 /*
184 * floating point regs. are stored in the thread structure
185 */
186 offset = addr - (addr_t) &dummy->regs.fp_regs;
187 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700188 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
189 tmp &= (unsigned long) FPC_VALID_MASK
190 << (BITS_PER_LONG - 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
193 /*
194 * per_info is found in the thread structure
195 */
196 offset = addr - (addr_t) &dummy->regs.per_info;
197 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
198
199 } else
200 tmp = 0;
201
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200202 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static int
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200206peek_user(struct task_struct *child, addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200208 addr_t tmp, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /*
211 * Stupid gdb peeks/pokes the access registers in 64 bit with
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200212 * an alignment of 4. Programmers from hell...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700214 mask = __ADDR_MASK;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800215#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100216 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
217 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700218 mask = 3;
219#endif
220 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -EIO;
222
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200223 tmp = __peek_user(child, addr);
224 return put_user(tmp, (addr_t __user *) data);
225}
226
227/*
228 * Write a word to the user area of a process at location addr. This
229 * operation does have an additional problem compared to peek_user.
230 * Stores to the program status word and on the floating point
231 * control register needs to get checked for validity.
232 */
233static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
234{
235 struct user *dummy = NULL;
236 addr_t offset;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (addr < (addr_t) &dummy->regs.acrs) {
239 /*
240 * psw and gprs are stored on the stack
241 */
242 if (addr == (addr_t) &dummy->regs.psw.mask &&
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800243#ifdef CONFIG_COMPAT
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100244 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#endif
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100246 data != PSW_MASK_MERGE(psw_user_bits, data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Invalid psw mask. */
248 return -EINVAL;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800249#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (addr == (addr_t) &dummy->regs.psw.addr)
251 /* I'd like to reject addresses without the
252 high order bit but older gdb's rely on it */
253 data |= PSW_ADDR_AMODE;
254#endif
Al Viroc7584fb2006-01-12 01:05:49 -0800255 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
258 /*
259 * access registers are stored in the thread structure
260 */
261 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800262#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700263 /*
264 * Very special case: old & broken 64 bit gdb writing
265 * to acrs[15] with a 64 bit value. Ignore the lower
266 * half of the value and write the upper 32 bit to
267 * acrs[15]. Sick...
268 */
269 if (addr == (addr_t) &dummy->regs.acrs[15])
270 child->thread.acrs[15] = (unsigned int) (data >> 32);
271 else
272#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
274
275 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
276 /*
277 * orig_gpr2 is stored on the kernel stack
278 */
Al Viroc7584fb2006-01-12 01:05:49 -0800279 task_pt_regs(child)->orig_gpr2 = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200281 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
282 /*
283 * prevent writes of padding hole between
284 * orig_gpr2 and fp_regs on s390.
285 */
286 return 0;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
289 /*
290 * floating point regs. are stored in the thread structure
291 */
292 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700293 (data & ~((unsigned long) FPC_VALID_MASK
294 << (BITS_PER_LONG - 32))) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return -EINVAL;
296 offset = addr - (addr_t) &dummy->regs.fp_regs;
297 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
298
299 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
300 /*
301 * per_info is found in the thread structure
302 */
303 offset = addr - (addr_t) &dummy->regs.per_info;
304 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
305
306 }
307
308 FixPerRegisters(child);
309 return 0;
310}
311
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200312static int
313poke_user(struct task_struct *child, addr_t addr, addr_t data)
314{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200315 addr_t mask;
316
317 /*
318 * Stupid gdb peeks/pokes the access registers in 64 bit with
319 * an alignment of 4. Programmers from hell indeed...
320 */
321 mask = __ADDR_MASK;
322#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100323 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
324 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200325 mask = 3;
326#endif
327 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
328 return -EIO;
329
330 return __poke_user(child, addr, data);
331}
332
Roland McGrathb499d762008-05-07 09:22:57 +0200333long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 ptrace_area parea;
336 int copied, ret;
337
338 switch (request) {
339 case PTRACE_PEEKTEXT:
340 case PTRACE_PEEKDATA:
341 /* Remove high order bit from address (only for 31 bit). */
342 addr &= PSW_ADDR_INSN;
343 /* read word at location addr. */
Alexey Dobriyan76647322007-07-17 04:03:43 -0700344 return generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 case PTRACE_PEEKUSR:
347 /* read the word at location addr in the USER area. */
348 return peek_user(child, addr, data);
349
350 case PTRACE_POKETEXT:
351 case PTRACE_POKEDATA:
352 /* Remove high order bit from address (only for 31 bit). */
353 addr &= PSW_ADDR_INSN;
354 /* write the word at location addr. */
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700355 return generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 case PTRACE_POKEUSR:
358 /* write the word at location addr in the USER area */
359 return poke_user(child, addr, data);
360
361 case PTRACE_PEEKUSR_AREA:
362 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100363 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sizeof(parea)))
365 return -EFAULT;
366 addr = parea.kernel_addr;
367 data = parea.process_addr;
368 copied = 0;
369 while (copied < parea.len) {
370 if (request == PTRACE_PEEKUSR_AREA)
371 ret = peek_user(child, addr, data);
372 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100373 addr_t utmp;
374 if (get_user(utmp,
375 (addr_t __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return -EFAULT;
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100377 ret = poke_user(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 if (ret)
380 return ret;
381 addr += sizeof(unsigned long);
382 data += sizeof(unsigned long);
383 copied += sizeof(unsigned long);
384 }
385 return 0;
386 }
387 return ptrace_request(child, request, addr, data);
388}
389
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800390#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * Now the fun part starts... a 31 bit program running in the
393 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
394 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
395 * to handle, the difference to the 64 bit versions of the requests
396 * is that the access is done in multiples of 4 byte instead of
397 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
398 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
399 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
400 * is a 31 bit program too, the content of struct user can be
401 * emulated. A 31 bit program peeking into the struct user of
402 * a 64 bit program is a no-no.
403 */
404
405/*
406 * Same as peek_user but for a 31 bit program.
407 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200408static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 struct user32 *dummy32 = NULL;
411 per_struct32 *dummy_per32 = NULL;
412 addr_t offset;
413 __u32 tmp;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (addr < (addr_t) &dummy32->regs.acrs) {
416 /*
417 * psw and gprs are stored on the stack
418 */
419 if (addr == (addr_t) &dummy32->regs.psw.mask) {
420 /* Fake a 31 bit psw mask. */
Al Viroc7584fb2006-01-12 01:05:49 -0800421 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100422 tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
424 /* Fake a 31 bit psw address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800425 tmp = (__u32) task_pt_regs(child)->psw.addr |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 PSW32_ADDR_AMODE31;
427 } else {
428 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800429 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 addr*2 + 4);
431 }
432 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
433 /*
434 * access registers are stored in the thread structure
435 */
436 offset = addr - (addr_t) &dummy32->regs.acrs;
437 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
438
439 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
440 /*
441 * orig_gpr2 is stored on the kernel stack
442 */
Al Viroc7584fb2006-01-12 01:05:49 -0800443 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200445 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
446 /*
447 * prevent reads of padding hole between
448 * orig_gpr2 and fp_regs on s390.
449 */
450 tmp = 0;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
453 /*
454 * floating point regs. are stored in the thread structure
455 */
456 offset = addr - (addr_t) &dummy32->regs.fp_regs;
457 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
458
459 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
460 /*
461 * per_info is found in the thread structure
462 */
463 offset = addr - (addr_t) &dummy32->regs.per_info;
464 /* This is magic. See per_struct and per_struct32. */
465 if ((offset >= (addr_t) &dummy_per32->control_regs &&
466 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
467 (offset >= (addr_t) &dummy_per32->starting_addr &&
468 offset <= (addr_t) &dummy_per32->ending_addr) ||
469 offset == (addr_t) &dummy_per32->lowcore.words.address)
470 offset = offset*2 + 4;
471 else
472 offset = offset*2;
473 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
474
475 } else
476 tmp = 0;
477
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200478 return tmp;
479}
480
481static int peek_user_compat(struct task_struct *child,
482 addr_t addr, addr_t data)
483{
484 __u32 tmp;
485
Heiko Carstens77575912009-06-12 10:26:25 +0200486 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200487 return -EIO;
488
489 tmp = __peek_user_compat(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return put_user(tmp, (__u32 __user *) data);
491}
492
493/*
494 * Same as poke_user but for a 31 bit program.
495 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200496static int __poke_user_compat(struct task_struct *child,
497 addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct user32 *dummy32 = NULL;
500 per_struct32 *dummy_per32 = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200501 __u32 tmp = (__u32) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 addr_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (addr < (addr_t) &dummy32->regs.acrs) {
505 /*
506 * psw, gprs, acrs and orig_gpr2 are stored on the stack
507 */
508 if (addr == (addr_t) &dummy32->regs.psw.mask) {
509 /* Build a 64 bit psw mask from 31 bit mask. */
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100510 if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Invalid psw mask. */
512 return -EINVAL;
Al Viroc7584fb2006-01-12 01:05:49 -0800513 task_pt_regs(child)->psw.mask =
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100514 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
516 /* Build a 64 bit psw address from 31 bit address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800517 task_pt_regs(child)->psw.addr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 (__u64) tmp & PSW32_ADDR_INSN;
519 } else {
520 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800521 *(__u32*)((addr_t) &task_pt_regs(child)->psw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 + addr*2 + 4) = tmp;
523 }
524 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
525 /*
526 * access registers are stored in the thread structure
527 */
528 offset = addr - (addr_t) &dummy32->regs.acrs;
529 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
530
531 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
532 /*
533 * orig_gpr2 is stored on the kernel stack
534 */
Al Viroc7584fb2006-01-12 01:05:49 -0800535 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200537 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
538 /*
539 * prevent writess of padding hole between
540 * orig_gpr2 and fp_regs on s390.
541 */
542 return 0;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
545 /*
546 * floating point regs. are stored in the thread structure
547 */
548 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
549 (tmp & ~FPC_VALID_MASK) != 0)
550 /* Invalid floating point control. */
551 return -EINVAL;
552 offset = addr - (addr_t) &dummy32->regs.fp_regs;
553 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
554
555 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
556 /*
557 * per_info is found in the thread structure.
558 */
559 offset = addr - (addr_t) &dummy32->regs.per_info;
560 /*
561 * This is magic. See per_struct and per_struct32.
562 * By incident the offsets in per_struct are exactly
563 * twice the offsets in per_struct32 for all fields.
564 * The 8 byte fields need special handling though,
565 * because the second half (bytes 4-7) is needed and
566 * not the first half.
567 */
568 if ((offset >= (addr_t) &dummy_per32->control_regs &&
569 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
570 (offset >= (addr_t) &dummy_per32->starting_addr &&
571 offset <= (addr_t) &dummy_per32->ending_addr) ||
572 offset == (addr_t) &dummy_per32->lowcore.words.address)
573 offset = offset*2 + 4;
574 else
575 offset = offset*2;
576 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
577
578 }
579
580 FixPerRegisters(child);
581 return 0;
582}
583
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200584static int poke_user_compat(struct task_struct *child,
585 addr_t addr, addr_t data)
586{
Heiko Carstens77575912009-06-12 10:26:25 +0200587 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user32) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200588 return -EIO;
589
590 return __poke_user_compat(child, addr, data);
591}
592
Roland McGrathb499d762008-05-07 09:22:57 +0200593long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
594 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Roland McGrathb499d762008-05-07 09:22:57 +0200596 unsigned long addr = caddr;
597 unsigned long data = cdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ptrace_area_emu31 parea;
599 int copied, ret;
600
601 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 case PTRACE_PEEKUSR:
603 /* read the word at location addr in the USER area. */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200604 return peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 case PTRACE_POKEUSR:
607 /* write the word at location addr in the USER area */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200608 return poke_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 case PTRACE_PEEKUSR_AREA:
611 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100612 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sizeof(parea)))
614 return -EFAULT;
615 addr = parea.kernel_addr;
616 data = parea.process_addr;
617 copied = 0;
618 while (copied < parea.len) {
619 if (request == PTRACE_PEEKUSR_AREA)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200620 ret = peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100622 __u32 utmp;
623 if (get_user(utmp,
624 (__u32 __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EFAULT;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200626 ret = poke_user_compat(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 if (ret)
629 return ret;
630 addr += sizeof(unsigned int);
631 data += sizeof(unsigned int);
632 copied += sizeof(unsigned int);
633 }
634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Roland McGrathb499d762008-05-07 09:22:57 +0200636 return compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638#endif
639
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200640asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200642 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Heiko Carstensbcf5cef2009-06-12 10:26:26 +0200644 /* Do the secure computing check first. */
645 secure_computing(regs->gprs[2]);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200648 * The sysc_tracesys code in entry.S stored the system
649 * call number to gprs[2].
Bodo Stroesserc5c3a6d2005-06-04 15:43:32 -0700650 */
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200651 ret = regs->gprs[2];
652 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
653 (tracehook_report_syscall_entry(regs) ||
654 regs->gprs[2] >= NR_syscalls)) {
655 /*
656 * Tracing decided this syscall should not happen or the
657 * debugger stored an invalid system call number. Skip
658 * the system call and the system call restart handling.
659 */
Martin Schwidefsky59da2132008-11-27 11:05:55 +0100660 regs->svcnr = 0;
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200661 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200663
664 if (unlikely(current->audit_context))
Heiko Carstens77575912009-06-12 10:26:25 +0200665 audit_syscall_entry(is_compat_task() ?
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200666 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
667 regs->gprs[2], regs->orig_gpr2,
668 regs->gprs[3], regs->gprs[4],
669 regs->gprs[5]);
670 return ret;
671}
672
673asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
674{
675 if (unlikely(current->audit_context))
676 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
677 regs->gprs[2]);
678
679 if (test_thread_flag(TIF_SYSCALL_TRACE))
680 tracehook_report_syscall_exit(regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200682
683/*
684 * user_regset definitions.
685 */
686
687static int s390_regs_get(struct task_struct *target,
688 const struct user_regset *regset,
689 unsigned int pos, unsigned int count,
690 void *kbuf, void __user *ubuf)
691{
692 if (target == current)
693 save_access_regs(target->thread.acrs);
694
695 if (kbuf) {
696 unsigned long *k = kbuf;
697 while (count > 0) {
698 *k++ = __peek_user(target, pos);
699 count -= sizeof(*k);
700 pos += sizeof(*k);
701 }
702 } else {
703 unsigned long __user *u = ubuf;
704 while (count > 0) {
705 if (__put_user(__peek_user(target, pos), u++))
706 return -EFAULT;
707 count -= sizeof(*u);
708 pos += sizeof(*u);
709 }
710 }
711 return 0;
712}
713
714static int s390_regs_set(struct task_struct *target,
715 const struct user_regset *regset,
716 unsigned int pos, unsigned int count,
717 const void *kbuf, const void __user *ubuf)
718{
719 int rc = 0;
720
721 if (target == current)
722 save_access_regs(target->thread.acrs);
723
724 if (kbuf) {
725 const unsigned long *k = kbuf;
726 while (count > 0 && !rc) {
727 rc = __poke_user(target, pos, *k++);
728 count -= sizeof(*k);
729 pos += sizeof(*k);
730 }
731 } else {
732 const unsigned long __user *u = ubuf;
733 while (count > 0 && !rc) {
734 unsigned long word;
735 rc = __get_user(word, u++);
736 if (rc)
737 break;
738 rc = __poke_user(target, pos, word);
739 count -= sizeof(*u);
740 pos += sizeof(*u);
741 }
742 }
743
744 if (rc == 0 && target == current)
745 restore_access_regs(target->thread.acrs);
746
747 return rc;
748}
749
750static int s390_fpregs_get(struct task_struct *target,
751 const struct user_regset *regset, unsigned int pos,
752 unsigned int count, void *kbuf, void __user *ubuf)
753{
754 if (target == current)
755 save_fp_regs(&target->thread.fp_regs);
756
757 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
758 &target->thread.fp_regs, 0, -1);
759}
760
761static int s390_fpregs_set(struct task_struct *target,
762 const struct user_regset *regset, unsigned int pos,
763 unsigned int count, const void *kbuf,
764 const void __user *ubuf)
765{
766 int rc = 0;
767
768 if (target == current)
769 save_fp_regs(&target->thread.fp_regs);
770
771 /* If setting FPC, must validate it first. */
772 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
773 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
774 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
775 0, offsetof(s390_fp_regs, fprs));
776 if (rc)
777 return rc;
778 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
779 return -EINVAL;
780 target->thread.fp_regs.fpc = fpc[0];
781 }
782
783 if (rc == 0 && count > 0)
784 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
785 target->thread.fp_regs.fprs,
786 offsetof(s390_fp_regs, fprs), -1);
787
788 if (rc == 0 && target == current)
789 restore_fp_regs(&target->thread.fp_regs);
790
791 return rc;
792}
793
794static const struct user_regset s390_regsets[] = {
795 [REGSET_GENERAL] = {
796 .core_note_type = NT_PRSTATUS,
797 .n = sizeof(s390_regs) / sizeof(long),
798 .size = sizeof(long),
799 .align = sizeof(long),
800 .get = s390_regs_get,
801 .set = s390_regs_set,
802 },
803 [REGSET_FP] = {
804 .core_note_type = NT_PRFPREG,
805 .n = sizeof(s390_fp_regs) / sizeof(long),
806 .size = sizeof(long),
807 .align = sizeof(long),
808 .get = s390_fpregs_get,
809 .set = s390_fpregs_set,
810 },
811};
812
813static const struct user_regset_view user_s390_view = {
814 .name = UTS_MACHINE,
815 .e_machine = EM_S390,
816 .regsets = s390_regsets,
817 .n = ARRAY_SIZE(s390_regsets)
818};
819
820#ifdef CONFIG_COMPAT
821static int s390_compat_regs_get(struct task_struct *target,
822 const struct user_regset *regset,
823 unsigned int pos, unsigned int count,
824 void *kbuf, void __user *ubuf)
825{
826 if (target == current)
827 save_access_regs(target->thread.acrs);
828
829 if (kbuf) {
830 compat_ulong_t *k = kbuf;
831 while (count > 0) {
832 *k++ = __peek_user_compat(target, pos);
833 count -= sizeof(*k);
834 pos += sizeof(*k);
835 }
836 } else {
837 compat_ulong_t __user *u = ubuf;
838 while (count > 0) {
839 if (__put_user(__peek_user_compat(target, pos), u++))
840 return -EFAULT;
841 count -= sizeof(*u);
842 pos += sizeof(*u);
843 }
844 }
845 return 0;
846}
847
848static int s390_compat_regs_set(struct task_struct *target,
849 const struct user_regset *regset,
850 unsigned int pos, unsigned int count,
851 const void *kbuf, const void __user *ubuf)
852{
853 int rc = 0;
854
855 if (target == current)
856 save_access_regs(target->thread.acrs);
857
858 if (kbuf) {
859 const compat_ulong_t *k = kbuf;
860 while (count > 0 && !rc) {
861 rc = __poke_user_compat(target, pos, *k++);
862 count -= sizeof(*k);
863 pos += sizeof(*k);
864 }
865 } else {
866 const compat_ulong_t __user *u = ubuf;
867 while (count > 0 && !rc) {
868 compat_ulong_t word;
869 rc = __get_user(word, u++);
870 if (rc)
871 break;
872 rc = __poke_user_compat(target, pos, word);
873 count -= sizeof(*u);
874 pos += sizeof(*u);
875 }
876 }
877
878 if (rc == 0 && target == current)
879 restore_access_regs(target->thread.acrs);
880
881 return rc;
882}
883
884static const struct user_regset s390_compat_regsets[] = {
885 [REGSET_GENERAL] = {
886 .core_note_type = NT_PRSTATUS,
887 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
888 .size = sizeof(compat_long_t),
889 .align = sizeof(compat_long_t),
890 .get = s390_compat_regs_get,
891 .set = s390_compat_regs_set,
892 },
893 [REGSET_FP] = {
894 .core_note_type = NT_PRFPREG,
895 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
896 .size = sizeof(compat_long_t),
897 .align = sizeof(compat_long_t),
898 .get = s390_fpregs_get,
899 .set = s390_fpregs_set,
900 },
901};
902
903static const struct user_regset_view user_s390_compat_view = {
904 .name = "s390",
905 .e_machine = EM_S390,
906 .regsets = s390_compat_regsets,
907 .n = ARRAY_SIZE(s390_compat_regsets)
908};
909#endif
910
911const struct user_regset_view *task_user_regset_view(struct task_struct *task)
912{
913#ifdef CONFIG_COMPAT
914 if (test_tsk_thread_flag(task, TIF_31BIT))
915 return &user_s390_compat_view;
916#endif
917 return &user_s390_view;
918}