blob: c5e87d891ca01ab0f1335890e79665089e6a5f61 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/errno.h>
30#include <linux/ptrace.h>
31#include <linux/user.h>
32#include <linux/security.h>
33#include <linux/audit.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070034#include <linux/signal.h>
Martin Schwidefsky63506c42008-07-14 09:58:54 +020035#include <linux/elf.h>
36#include <linux/regset.h>
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +020037#include <linux/tracehook.h>
Heiko Carstensbcf5cef2009-06-12 10:26:26 +020038#include <linux/seccomp.h>
Heiko Carstens9bf12262009-06-12 10:26:47 +020039#include <trace/syscall.h>
Heiko Carstensbcf5cef2009-06-12 10:26:26 +020040#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
Ingo Molnar5e9ad7d2009-08-18 10:41:57 +020054DEFINE_TRACE(syscall_enter);
55DEFINE_TRACE(syscall_exit);
56
Martin Schwidefsky63506c42008-07-14 09:58:54 +020057enum s390_regset {
58 REGSET_GENERAL,
59 REGSET_FP,
60};
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void
63FixPerRegisters(struct task_struct *task)
64{
65 struct pt_regs *regs;
66 per_struct *per_info;
67
Al Viroc7584fb2006-01-12 01:05:49 -080068 regs = task_pt_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 per_info = (per_struct *) &task->thread.per_info;
70 per_info->control_regs.bits.em_instruction_fetch =
71 per_info->single_step | per_info->instruction_fetch;
72
73 if (per_info->single_step) {
74 per_info->control_regs.bits.starting_addr = 0;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080075#ifdef CONFIG_COMPAT
Heiko Carstens77575912009-06-12 10:26:25 +020076 if (is_compat_task())
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
78 else
79#endif
80 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
81 } else {
82 per_info->control_regs.bits.starting_addr =
83 per_info->starting_addr;
84 per_info->control_regs.bits.ending_addr =
85 per_info->ending_addr;
86 }
87 /*
88 * if any of the control reg tracing bits are on
89 * we switch on per in the psw
90 */
91 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
92 regs->psw.mask |= PSW_MASK_PER;
93 else
94 regs->psw.mask &= ~PSW_MASK_PER;
95
96 if (per_info->control_regs.bits.em_storage_alteration)
97 per_info->control_regs.bits.storage_alt_space_ctl = 1;
98 else
99 per_info->control_regs.bits.storage_alt_space_ctl = 0;
100}
101
Roland McGrath0ac30be2008-01-26 14:11:22 +0100102void user_enable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 task->thread.per_info.single_step = 1;
105 FixPerRegisters(task);
106}
107
Roland McGrath0ac30be2008-01-26 14:11:22 +0100108void user_disable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 task->thread.per_info.single_step = 0;
111 FixPerRegisters(task);
112}
113
114/*
115 * Called by kernel/ptrace.c when detaching..
116 *
117 * Make sure single step bits etc are not set.
118 */
119void
120ptrace_disable(struct task_struct *child)
121{
122 /* make sure the single step bit is not set. */
Roland McGrath0ac30be2008-01-26 14:11:22 +0100123 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800126#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127# define __ADDR_MASK 3
128#else
129# define __ADDR_MASK 7
130#endif
131
132/*
133 * Read the word at offset addr from the user area of a process. The
134 * trouble here is that the information is littered over different
135 * locations. The process registers are found on the kernel stack,
136 * the floating point stuff and the trace settings are stored in
137 * the task structure. In addition the different structures in
138 * struct user contain pad bytes that should be read as zeroes.
139 * Lovely...
140 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200141static unsigned long __peek_user(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct user *dummy = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200144 addr_t offset, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (addr < (addr_t) &dummy->regs.acrs) {
147 /*
148 * psw and gprs are stored on the stack
149 */
Al Viroc7584fb2006-01-12 01:05:49 -0800150 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (addr == (addr_t) &dummy->regs.psw.mask)
152 /* Remove per bit from user psw. */
153 tmp &= ~PSW_MASK_PER;
154
155 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
156 /*
157 * access registers are stored in the thread structure
158 */
159 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800160#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700161 /*
162 * Very special case: old & broken 64 bit gdb reading
163 * from acrs[15]. Result is a 64 bit value. Read the
164 * 32 bit acrs[15] value and shift it by 32. Sick...
165 */
166 if (addr == (addr_t) &dummy->regs.acrs[15])
167 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
168 else
169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
171
172 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
173 /*
174 * orig_gpr2 is stored on the kernel stack
175 */
Al Viroc7584fb2006-01-12 01:05:49 -0800176 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200178 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
179 /*
180 * prevent reads of padding hole between
181 * orig_gpr2 and fp_regs on s390.
182 */
183 tmp = 0;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
186 /*
187 * floating point regs. are stored in the thread structure
188 */
189 offset = addr - (addr_t) &dummy->regs.fp_regs;
190 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700191 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
192 tmp &= (unsigned long) FPC_VALID_MASK
193 << (BITS_PER_LONG - 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
196 /*
197 * per_info is found in the thread structure
198 */
199 offset = addr - (addr_t) &dummy->regs.per_info;
200 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
201
202 } else
203 tmp = 0;
204
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200205 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static int
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200209peek_user(struct task_struct *child, addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200211 addr_t tmp, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /*
214 * Stupid gdb peeks/pokes the access registers in 64 bit with
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200215 * an alignment of 4. Programmers from hell...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700217 mask = __ADDR_MASK;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800218#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100219 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
220 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700221 mask = 3;
222#endif
223 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EIO;
225
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200226 tmp = __peek_user(child, addr);
227 return put_user(tmp, (addr_t __user *) data);
228}
229
230/*
231 * Write a word to the user area of a process at location addr. This
232 * operation does have an additional problem compared to peek_user.
233 * Stores to the program status word and on the floating point
234 * control register needs to get checked for validity.
235 */
236static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
237{
238 struct user *dummy = NULL;
239 addr_t offset;
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (addr < (addr_t) &dummy->regs.acrs) {
242 /*
243 * psw and gprs are stored on the stack
244 */
245 if (addr == (addr_t) &dummy->regs.psw.mask &&
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800246#ifdef CONFIG_COMPAT
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100247 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#endif
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100249 data != PSW_MASK_MERGE(psw_user_bits, data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* Invalid psw mask. */
251 return -EINVAL;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800252#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (addr == (addr_t) &dummy->regs.psw.addr)
254 /* I'd like to reject addresses without the
255 high order bit but older gdb's rely on it */
256 data |= PSW_ADDR_AMODE;
257#endif
Al Viroc7584fb2006-01-12 01:05:49 -0800258 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
261 /*
262 * access registers are stored in the thread structure
263 */
264 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800265#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700266 /*
267 * Very special case: old & broken 64 bit gdb writing
268 * to acrs[15] with a 64 bit value. Ignore the lower
269 * half of the value and write the upper 32 bit to
270 * acrs[15]. Sick...
271 */
272 if (addr == (addr_t) &dummy->regs.acrs[15])
273 child->thread.acrs[15] = (unsigned int) (data >> 32);
274 else
275#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
277
278 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
279 /*
280 * orig_gpr2 is stored on the kernel stack
281 */
Al Viroc7584fb2006-01-12 01:05:49 -0800282 task_pt_regs(child)->orig_gpr2 = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200284 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
285 /*
286 * prevent writes of padding hole between
287 * orig_gpr2 and fp_regs on s390.
288 */
289 return 0;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
292 /*
293 * floating point regs. are stored in the thread structure
294 */
295 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700296 (data & ~((unsigned long) FPC_VALID_MASK
297 << (BITS_PER_LONG - 32))) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return -EINVAL;
299 offset = addr - (addr_t) &dummy->regs.fp_regs;
300 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
301
302 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
303 /*
304 * per_info is found in the thread structure
305 */
306 offset = addr - (addr_t) &dummy->regs.per_info;
307 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
308
309 }
310
311 FixPerRegisters(child);
312 return 0;
313}
314
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200315static int
316poke_user(struct task_struct *child, addr_t addr, addr_t data)
317{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200318 addr_t mask;
319
320 /*
321 * Stupid gdb peeks/pokes the access registers in 64 bit with
322 * an alignment of 4. Programmers from hell indeed...
323 */
324 mask = __ADDR_MASK;
325#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100326 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
327 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200328 mask = 3;
329#endif
330 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
331 return -EIO;
332
333 return __poke_user(child, addr, data);
334}
335
Roland McGrathb499d762008-05-07 09:22:57 +0200336long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 ptrace_area parea;
339 int copied, ret;
340
341 switch (request) {
342 case PTRACE_PEEKTEXT:
343 case PTRACE_PEEKDATA:
344 /* Remove high order bit from address (only for 31 bit). */
345 addr &= PSW_ADDR_INSN;
346 /* read word at location addr. */
Alexey Dobriyan76647322007-07-17 04:03:43 -0700347 return generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 case PTRACE_PEEKUSR:
350 /* read the word at location addr in the USER area. */
351 return peek_user(child, addr, data);
352
353 case PTRACE_POKETEXT:
354 case PTRACE_POKEDATA:
355 /* Remove high order bit from address (only for 31 bit). */
356 addr &= PSW_ADDR_INSN;
357 /* write the word at location addr. */
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700358 return generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 case PTRACE_POKEUSR:
361 /* write the word at location addr in the USER area */
362 return poke_user(child, addr, data);
363
364 case PTRACE_PEEKUSR_AREA:
365 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100366 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 sizeof(parea)))
368 return -EFAULT;
369 addr = parea.kernel_addr;
370 data = parea.process_addr;
371 copied = 0;
372 while (copied < parea.len) {
373 if (request == PTRACE_PEEKUSR_AREA)
374 ret = peek_user(child, addr, data);
375 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100376 addr_t utmp;
377 if (get_user(utmp,
378 (addr_t __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -EFAULT;
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100380 ret = poke_user(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 if (ret)
383 return ret;
384 addr += sizeof(unsigned long);
385 data += sizeof(unsigned long);
386 copied += sizeof(unsigned long);
387 }
388 return 0;
389 }
390 return ptrace_request(child, request, addr, data);
391}
392
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800393#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/*
395 * Now the fun part starts... a 31 bit program running in the
396 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
397 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
398 * to handle, the difference to the 64 bit versions of the requests
399 * is that the access is done in multiples of 4 byte instead of
400 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
401 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
402 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
403 * is a 31 bit program too, the content of struct user can be
404 * emulated. A 31 bit program peeking into the struct user of
405 * a 64 bit program is a no-no.
406 */
407
408/*
409 * Same as peek_user but for a 31 bit program.
410 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200411static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct user32 *dummy32 = NULL;
414 per_struct32 *dummy_per32 = NULL;
415 addr_t offset;
416 __u32 tmp;
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (addr < (addr_t) &dummy32->regs.acrs) {
419 /*
420 * psw and gprs are stored on the stack
421 */
422 if (addr == (addr_t) &dummy32->regs.psw.mask) {
423 /* Fake a 31 bit psw mask. */
Al Viroc7584fb2006-01-12 01:05:49 -0800424 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100425 tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
427 /* Fake a 31 bit psw address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800428 tmp = (__u32) task_pt_regs(child)->psw.addr |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 PSW32_ADDR_AMODE31;
430 } else {
431 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800432 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 addr*2 + 4);
434 }
435 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
436 /*
437 * access registers are stored in the thread structure
438 */
439 offset = addr - (addr_t) &dummy32->regs.acrs;
440 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
441
442 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
443 /*
444 * orig_gpr2 is stored on the kernel stack
445 */
Al Viroc7584fb2006-01-12 01:05:49 -0800446 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200448 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
449 /*
450 * prevent reads of padding hole between
451 * orig_gpr2 and fp_regs on s390.
452 */
453 tmp = 0;
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
456 /*
457 * floating point regs. are stored in the thread structure
458 */
459 offset = addr - (addr_t) &dummy32->regs.fp_regs;
460 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
461
462 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
463 /*
464 * per_info is found in the thread structure
465 */
466 offset = addr - (addr_t) &dummy32->regs.per_info;
467 /* This is magic. See per_struct and per_struct32. */
468 if ((offset >= (addr_t) &dummy_per32->control_regs &&
469 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
470 (offset >= (addr_t) &dummy_per32->starting_addr &&
471 offset <= (addr_t) &dummy_per32->ending_addr) ||
472 offset == (addr_t) &dummy_per32->lowcore.words.address)
473 offset = offset*2 + 4;
474 else
475 offset = offset*2;
476 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
477
478 } else
479 tmp = 0;
480
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200481 return tmp;
482}
483
484static int peek_user_compat(struct task_struct *child,
485 addr_t addr, addr_t data)
486{
487 __u32 tmp;
488
Heiko Carstens77575912009-06-12 10:26:25 +0200489 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200490 return -EIO;
491
492 tmp = __peek_user_compat(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return put_user(tmp, (__u32 __user *) data);
494}
495
496/*
497 * Same as poke_user but for a 31 bit program.
498 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200499static int __poke_user_compat(struct task_struct *child,
500 addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct user32 *dummy32 = NULL;
503 per_struct32 *dummy_per32 = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200504 __u32 tmp = (__u32) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 addr_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (addr < (addr_t) &dummy32->regs.acrs) {
508 /*
509 * psw, gprs, acrs and orig_gpr2 are stored on the stack
510 */
511 if (addr == (addr_t) &dummy32->regs.psw.mask) {
512 /* Build a 64 bit psw mask from 31 bit mask. */
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100513 if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Invalid psw mask. */
515 return -EINVAL;
Al Viroc7584fb2006-01-12 01:05:49 -0800516 task_pt_regs(child)->psw.mask =
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100517 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
519 /* Build a 64 bit psw address from 31 bit address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800520 task_pt_regs(child)->psw.addr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 (__u64) tmp & PSW32_ADDR_INSN;
522 } else {
523 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800524 *(__u32*)((addr_t) &task_pt_regs(child)->psw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 + addr*2 + 4) = tmp;
526 }
527 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
528 /*
529 * access registers are stored in the thread structure
530 */
531 offset = addr - (addr_t) &dummy32->regs.acrs;
532 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
533
534 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
535 /*
536 * orig_gpr2 is stored on the kernel stack
537 */
Al Viroc7584fb2006-01-12 01:05:49 -0800538 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200540 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
541 /*
542 * prevent writess of padding hole between
543 * orig_gpr2 and fp_regs on s390.
544 */
545 return 0;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
548 /*
549 * floating point regs. are stored in the thread structure
550 */
551 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
552 (tmp & ~FPC_VALID_MASK) != 0)
553 /* Invalid floating point control. */
554 return -EINVAL;
555 offset = addr - (addr_t) &dummy32->regs.fp_regs;
556 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
557
558 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
559 /*
560 * per_info is found in the thread structure.
561 */
562 offset = addr - (addr_t) &dummy32->regs.per_info;
563 /*
564 * This is magic. See per_struct and per_struct32.
565 * By incident the offsets in per_struct are exactly
566 * twice the offsets in per_struct32 for all fields.
567 * The 8 byte fields need special handling though,
568 * because the second half (bytes 4-7) is needed and
569 * not the first half.
570 */
571 if ((offset >= (addr_t) &dummy_per32->control_regs &&
572 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
573 (offset >= (addr_t) &dummy_per32->starting_addr &&
574 offset <= (addr_t) &dummy_per32->ending_addr) ||
575 offset == (addr_t) &dummy_per32->lowcore.words.address)
576 offset = offset*2 + 4;
577 else
578 offset = offset*2;
579 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
580
581 }
582
583 FixPerRegisters(child);
584 return 0;
585}
586
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200587static int poke_user_compat(struct task_struct *child,
588 addr_t addr, addr_t data)
589{
Heiko Carstens77575912009-06-12 10:26:25 +0200590 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user32) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200591 return -EIO;
592
593 return __poke_user_compat(child, addr, data);
594}
595
Roland McGrathb499d762008-05-07 09:22:57 +0200596long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
597 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Roland McGrathb499d762008-05-07 09:22:57 +0200599 unsigned long addr = caddr;
600 unsigned long data = cdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 ptrace_area_emu31 parea;
602 int copied, ret;
603
604 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 case PTRACE_PEEKUSR:
606 /* read the word at location addr in the USER area. */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200607 return peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 case PTRACE_POKEUSR:
610 /* write the word at location addr in the USER area */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200611 return poke_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 case PTRACE_PEEKUSR_AREA:
614 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100615 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 sizeof(parea)))
617 return -EFAULT;
618 addr = parea.kernel_addr;
619 data = parea.process_addr;
620 copied = 0;
621 while (copied < parea.len) {
622 if (request == PTRACE_PEEKUSR_AREA)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200623 ret = peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100625 __u32 utmp;
626 if (get_user(utmp,
627 (__u32 __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -EFAULT;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200629 ret = poke_user_compat(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631 if (ret)
632 return ret;
633 addr += sizeof(unsigned int);
634 data += sizeof(unsigned int);
635 copied += sizeof(unsigned int);
636 }
637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Roland McGrathb499d762008-05-07 09:22:57 +0200639 return compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641#endif
642
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200643asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200645 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Heiko Carstensbcf5cef2009-06-12 10:26:26 +0200647 /* Do the secure computing check first. */
648 secure_computing(regs->gprs[2]);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /*
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200651 * The sysc_tracesys code in entry.S stored the system
652 * call number to gprs[2].
Bodo Stroesserc5c3a6d2005-06-04 15:43:32 -0700653 */
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200654 ret = regs->gprs[2];
655 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
656 (tracehook_report_syscall_entry(regs) ||
657 regs->gprs[2] >= NR_syscalls)) {
658 /*
659 * Tracing decided this syscall should not happen or the
660 * debugger stored an invalid system call number. Skip
661 * the system call and the system call restart handling.
662 */
Martin Schwidefsky59da2132008-11-27 11:05:55 +0100663 regs->svcnr = 0;
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200664 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200666
Heiko Carstens9bf12262009-06-12 10:26:47 +0200667 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
Ingo Molnar5e9ad7d2009-08-18 10:41:57 +0200668 trace_syscall_enter(regs, regs->gprs[2]);
Heiko Carstens9bf12262009-06-12 10:26:47 +0200669
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200670 if (unlikely(current->audit_context))
Heiko Carstens77575912009-06-12 10:26:25 +0200671 audit_syscall_entry(is_compat_task() ?
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200672 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
673 regs->gprs[2], regs->orig_gpr2,
674 regs->gprs[3], regs->gprs[4],
675 regs->gprs[5]);
676 return ret;
677}
678
679asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
680{
681 if (unlikely(current->audit_context))
682 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
683 regs->gprs[2]);
684
Heiko Carstens9bf12262009-06-12 10:26:47 +0200685 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
Ingo Molnar5e9ad7d2009-08-18 10:41:57 +0200686 trace_syscall_exit(regs, regs->gprs[2]);
Heiko Carstens9bf12262009-06-12 10:26:47 +0200687
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200688 if (test_thread_flag(TIF_SYSCALL_TRACE))
689 tracehook_report_syscall_exit(regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200691
692/*
693 * user_regset definitions.
694 */
695
696static int s390_regs_get(struct task_struct *target,
697 const struct user_regset *regset,
698 unsigned int pos, unsigned int count,
699 void *kbuf, void __user *ubuf)
700{
701 if (target == current)
702 save_access_regs(target->thread.acrs);
703
704 if (kbuf) {
705 unsigned long *k = kbuf;
706 while (count > 0) {
707 *k++ = __peek_user(target, pos);
708 count -= sizeof(*k);
709 pos += sizeof(*k);
710 }
711 } else {
712 unsigned long __user *u = ubuf;
713 while (count > 0) {
714 if (__put_user(__peek_user(target, pos), u++))
715 return -EFAULT;
716 count -= sizeof(*u);
717 pos += sizeof(*u);
718 }
719 }
720 return 0;
721}
722
723static int s390_regs_set(struct task_struct *target,
724 const struct user_regset *regset,
725 unsigned int pos, unsigned int count,
726 const void *kbuf, const void __user *ubuf)
727{
728 int rc = 0;
729
730 if (target == current)
731 save_access_regs(target->thread.acrs);
732
733 if (kbuf) {
734 const unsigned long *k = kbuf;
735 while (count > 0 && !rc) {
736 rc = __poke_user(target, pos, *k++);
737 count -= sizeof(*k);
738 pos += sizeof(*k);
739 }
740 } else {
741 const unsigned long __user *u = ubuf;
742 while (count > 0 && !rc) {
743 unsigned long word;
744 rc = __get_user(word, u++);
745 if (rc)
746 break;
747 rc = __poke_user(target, pos, word);
748 count -= sizeof(*u);
749 pos += sizeof(*u);
750 }
751 }
752
753 if (rc == 0 && target == current)
754 restore_access_regs(target->thread.acrs);
755
756 return rc;
757}
758
759static int s390_fpregs_get(struct task_struct *target,
760 const struct user_regset *regset, unsigned int pos,
761 unsigned int count, void *kbuf, void __user *ubuf)
762{
763 if (target == current)
764 save_fp_regs(&target->thread.fp_regs);
765
766 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
767 &target->thread.fp_regs, 0, -1);
768}
769
770static int s390_fpregs_set(struct task_struct *target,
771 const struct user_regset *regset, unsigned int pos,
772 unsigned int count, const void *kbuf,
773 const void __user *ubuf)
774{
775 int rc = 0;
776
777 if (target == current)
778 save_fp_regs(&target->thread.fp_regs);
779
780 /* If setting FPC, must validate it first. */
781 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
782 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
783 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
784 0, offsetof(s390_fp_regs, fprs));
785 if (rc)
786 return rc;
787 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
788 return -EINVAL;
789 target->thread.fp_regs.fpc = fpc[0];
790 }
791
792 if (rc == 0 && count > 0)
793 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
794 target->thread.fp_regs.fprs,
795 offsetof(s390_fp_regs, fprs), -1);
796
797 if (rc == 0 && target == current)
798 restore_fp_regs(&target->thread.fp_regs);
799
800 return rc;
801}
802
803static const struct user_regset s390_regsets[] = {
804 [REGSET_GENERAL] = {
805 .core_note_type = NT_PRSTATUS,
806 .n = sizeof(s390_regs) / sizeof(long),
807 .size = sizeof(long),
808 .align = sizeof(long),
809 .get = s390_regs_get,
810 .set = s390_regs_set,
811 },
812 [REGSET_FP] = {
813 .core_note_type = NT_PRFPREG,
814 .n = sizeof(s390_fp_regs) / sizeof(long),
815 .size = sizeof(long),
816 .align = sizeof(long),
817 .get = s390_fpregs_get,
818 .set = s390_fpregs_set,
819 },
820};
821
822static const struct user_regset_view user_s390_view = {
823 .name = UTS_MACHINE,
824 .e_machine = EM_S390,
825 .regsets = s390_regsets,
826 .n = ARRAY_SIZE(s390_regsets)
827};
828
829#ifdef CONFIG_COMPAT
830static int s390_compat_regs_get(struct task_struct *target,
831 const struct user_regset *regset,
832 unsigned int pos, unsigned int count,
833 void *kbuf, void __user *ubuf)
834{
835 if (target == current)
836 save_access_regs(target->thread.acrs);
837
838 if (kbuf) {
839 compat_ulong_t *k = kbuf;
840 while (count > 0) {
841 *k++ = __peek_user_compat(target, pos);
842 count -= sizeof(*k);
843 pos += sizeof(*k);
844 }
845 } else {
846 compat_ulong_t __user *u = ubuf;
847 while (count > 0) {
848 if (__put_user(__peek_user_compat(target, pos), u++))
849 return -EFAULT;
850 count -= sizeof(*u);
851 pos += sizeof(*u);
852 }
853 }
854 return 0;
855}
856
857static int s390_compat_regs_set(struct task_struct *target,
858 const struct user_regset *regset,
859 unsigned int pos, unsigned int count,
860 const void *kbuf, const void __user *ubuf)
861{
862 int rc = 0;
863
864 if (target == current)
865 save_access_regs(target->thread.acrs);
866
867 if (kbuf) {
868 const compat_ulong_t *k = kbuf;
869 while (count > 0 && !rc) {
870 rc = __poke_user_compat(target, pos, *k++);
871 count -= sizeof(*k);
872 pos += sizeof(*k);
873 }
874 } else {
875 const compat_ulong_t __user *u = ubuf;
876 while (count > 0 && !rc) {
877 compat_ulong_t word;
878 rc = __get_user(word, u++);
879 if (rc)
880 break;
881 rc = __poke_user_compat(target, pos, word);
882 count -= sizeof(*u);
883 pos += sizeof(*u);
884 }
885 }
886
887 if (rc == 0 && target == current)
888 restore_access_regs(target->thread.acrs);
889
890 return rc;
891}
892
893static const struct user_regset s390_compat_regsets[] = {
894 [REGSET_GENERAL] = {
895 .core_note_type = NT_PRSTATUS,
896 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
897 .size = sizeof(compat_long_t),
898 .align = sizeof(compat_long_t),
899 .get = s390_compat_regs_get,
900 .set = s390_compat_regs_set,
901 },
902 [REGSET_FP] = {
903 .core_note_type = NT_PRFPREG,
904 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
905 .size = sizeof(compat_long_t),
906 .align = sizeof(compat_long_t),
907 .get = s390_fpregs_get,
908 .set = s390_fpregs_set,
909 },
910};
911
912static const struct user_regset_view user_s390_compat_view = {
913 .name = "s390",
914 .e_machine = EM_S390,
915 .regsets = s390_compat_regsets,
916 .n = ARRAY_SIZE(s390_compat_regsets)
917};
918#endif
919
920const struct user_regset_view *task_user_regset_view(struct task_struct *task)
921{
922#ifdef CONFIG_COMPAT
923 if (test_tsk_thread_flag(task, TIF_31BIT))
924 return &user_s390_compat_view;
925#endif
926 return &user_s390_view;
927}