blob: f05649fd05476d5c96043eed47379a4321db43c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +01002 * Ptrace user space interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +01004 * Copyright IBM Corp. 1999,2010
5 * Author(s): Denis Joseph Barrow
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Martin Schwidefsky (schwidefsky@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9#include <linux/kernel.h>
10#include <linux/sched.h>
11#include <linux/mm.h>
12#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
14#include <linux/ptrace.h>
15#include <linux/user.h>
16#include <linux/security.h>
17#include <linux/audit.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070018#include <linux/signal.h>
Martin Schwidefsky63506c42008-07-14 09:58:54 +020019#include <linux/elf.h>
20#include <linux/regset.h>
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +020021#include <linux/tracehook.h>
Heiko Carstensbcf5cef2009-06-12 10:26:26 +020022#include <linux/seccomp.h>
Heiko Carstens9bf12262009-06-12 10:26:47 +020023#include <trace/syscall.h>
Heiko Carstensbcf5cef2009-06-12 10:26:26 +020024#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/segment.h>
26#include <asm/page.h>
27#include <asm/pgtable.h>
28#include <asm/pgalloc.h>
29#include <asm/system.h>
30#include <asm/uaccess.h>
Martin Schwidefsky778959d2005-06-04 15:43:30 -070031#include <asm/unistd.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020032#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080034#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "compat_ptrace.h"
36#endif
37
Josh Stone1c569f02009-08-24 14:43:14 -070038#define CREATE_TRACE_POINTS
39#include <trace/events/syscalls.h>
Ingo Molnar5e9ad7d2009-08-18 10:41:57 +020040
Martin Schwidefsky63506c42008-07-14 09:58:54 +020041enum s390_regset {
42 REGSET_GENERAL,
43 REGSET_FP,
Martin Schwidefsky86f25522010-05-17 10:00:05 +020044 REGSET_LAST_BREAK,
Martin Schwidefsky20b40a72011-10-30 15:16:47 +010045 REGSET_SYSTEM_CALL,
Heiko Carstensea2a4d32009-10-06 10:34:13 +020046 REGSET_GENERAL_EXTENDED,
Martin Schwidefsky63506c42008-07-14 09:58:54 +020047};
48
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010049void update_per_regs(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010051 struct pt_regs *regs = task_pt_regs(task);
52 struct thread_struct *thread = &task->thread;
Martin Schwidefskya45aff52011-10-30 15:16:07 +010053 struct per_regs old, new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Martin Schwidefskya45aff52011-10-30 15:16:07 +010055 /* Copy user specified PER registers */
56 new.control = thread->per_user.control;
57 new.start = thread->per_user.start;
58 new.end = thread->per_user.end;
59
60 /* merge TIF_SINGLE_STEP into user specified PER registers. */
61 if (test_tsk_thread_flag(task, TIF_SINGLE_STEP)) {
62 new.control |= PER_EVENT_IFETCH;
63 new.start = 0;
64 new.end = PSW_ADDR_INSN;
65 }
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010066
67 /* Take care of the PER enablement bit in the PSW. */
Martin Schwidefskya45aff52011-10-30 15:16:07 +010068 if (!(new.control & PER_EVENT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 regs->psw.mask &= ~PSW_MASK_PER;
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010070 return;
Martin Schwidefskyc3311c12010-01-13 20:44:25 +010071 }
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010072 regs->psw.mask |= PSW_MASK_PER;
73 __ctl_store(old, 9, 11);
Martin Schwidefskya45aff52011-10-30 15:16:07 +010074 if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
75 __ctl_load(new, 9, 11);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Roland McGrath0ac30be2008-01-26 14:11:22 +010078void user_enable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010080 set_tsk_thread_flag(task, TIF_SINGLE_STEP);
81 if (task == current)
82 update_per_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Roland McGrath0ac30be2008-01-26 14:11:22 +010085void user_disable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010087 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
88 if (task == current)
89 update_per_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92/*
93 * Called by kernel/ptrace.c when detaching..
94 *
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010095 * Clear all debugging related fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010097void ptrace_disable(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010099 memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
100 memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
101 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
102 clear_tsk_thread_flag(task, TIF_PER_TRAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800105#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106# define __ADDR_MASK 3
107#else
108# define __ADDR_MASK 7
109#endif
110
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100111static inline unsigned long __peek_user_per(struct task_struct *child,
112 addr_t addr)
113{
114 struct per_struct_kernel *dummy = NULL;
115
116 if (addr == (addr_t) &dummy->cr9)
117 /* Control bits of the active per set. */
118 return test_thread_flag(TIF_SINGLE_STEP) ?
119 PER_EVENT_IFETCH : child->thread.per_user.control;
120 else if (addr == (addr_t) &dummy->cr10)
121 /* Start address of the active per set. */
122 return test_thread_flag(TIF_SINGLE_STEP) ?
123 0 : child->thread.per_user.start;
124 else if (addr == (addr_t) &dummy->cr11)
125 /* End address of the active per set. */
126 return test_thread_flag(TIF_SINGLE_STEP) ?
127 PSW_ADDR_INSN : child->thread.per_user.end;
128 else if (addr == (addr_t) &dummy->bits)
129 /* Single-step bit. */
130 return test_thread_flag(TIF_SINGLE_STEP) ?
131 (1UL << (BITS_PER_LONG - 1)) : 0;
132 else if (addr == (addr_t) &dummy->starting_addr)
133 /* Start address of the user specified per set. */
134 return child->thread.per_user.start;
135 else if (addr == (addr_t) &dummy->ending_addr)
136 /* End address of the user specified per set. */
137 return child->thread.per_user.end;
138 else if (addr == (addr_t) &dummy->perc_atmid)
139 /* PER code, ATMID and AI of the last PER trap */
140 return (unsigned long)
141 child->thread.per_event.cause << (BITS_PER_LONG - 16);
142 else if (addr == (addr_t) &dummy->address)
143 /* Address of the last PER trap */
144 return child->thread.per_event.address;
145 else if (addr == (addr_t) &dummy->access_id)
146 /* Access id of the last PER trap */
147 return (unsigned long)
148 child->thread.per_event.paid << (BITS_PER_LONG - 8);
149 return 0;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
153 * Read the word at offset addr from the user area of a process. The
154 * trouble here is that the information is littered over different
155 * locations. The process registers are found on the kernel stack,
156 * the floating point stuff and the trace settings are stored in
157 * the task structure. In addition the different structures in
158 * struct user contain pad bytes that should be read as zeroes.
159 * Lovely...
160 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200161static unsigned long __peek_user(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct user *dummy = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200164 addr_t offset, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (addr < (addr_t) &dummy->regs.acrs) {
167 /*
168 * psw and gprs are stored on the stack
169 */
Al Viroc7584fb2006-01-12 01:05:49 -0800170 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (addr == (addr_t) &dummy->regs.psw.mask)
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100172 /* Return a clean psw mask. */
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100173 tmp = psw_user_bits | (tmp & PSW_MASK_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
176 /*
177 * access registers are stored in the thread structure
178 */
179 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800180#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700181 /*
182 * Very special case: old & broken 64 bit gdb reading
183 * from acrs[15]. Result is a 64 bit value. Read the
184 * 32 bit acrs[15] value and shift it by 32. Sick...
185 */
186 if (addr == (addr_t) &dummy->regs.acrs[15])
187 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
188 else
189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
191
192 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
193 /*
194 * orig_gpr2 is stored on the kernel stack
195 */
Al Viroc7584fb2006-01-12 01:05:49 -0800196 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200198 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
199 /*
200 * prevent reads of padding hole between
201 * orig_gpr2 and fp_regs on s390.
202 */
203 tmp = 0;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
206 /*
207 * floating point regs. are stored in the thread structure
208 */
209 offset = addr - (addr_t) &dummy->regs.fp_regs;
210 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700211 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
212 tmp &= (unsigned long) FPC_VALID_MASK
213 << (BITS_PER_LONG - 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
216 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100217 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100219 addr -= (addr_t) &dummy->regs.per_info;
220 tmp = __peek_user_per(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 } else
223 tmp = 0;
224
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200225 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200229peek_user(struct task_struct *child, addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200231 addr_t tmp, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /*
234 * Stupid gdb peeks/pokes the access registers in 64 bit with
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200235 * an alignment of 4. Programmers from hell...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700237 mask = __ADDR_MASK;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800238#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100239 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
240 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700241 mask = 3;
242#endif
243 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -EIO;
245
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200246 tmp = __peek_user(child, addr);
247 return put_user(tmp, (addr_t __user *) data);
248}
249
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100250static inline void __poke_user_per(struct task_struct *child,
251 addr_t addr, addr_t data)
252{
253 struct per_struct_kernel *dummy = NULL;
254
255 /*
256 * There are only three fields in the per_info struct that the
257 * debugger user can write to.
258 * 1) cr9: the debugger wants to set a new PER event mask
259 * 2) starting_addr: the debugger wants to set a new starting
260 * address to use with the PER event mask.
261 * 3) ending_addr: the debugger wants to set a new ending
262 * address to use with the PER event mask.
263 * The user specified PER event mask and the start and end
264 * addresses are used only if single stepping is not in effect.
265 * Writes to any other field in per_info are ignored.
266 */
267 if (addr == (addr_t) &dummy->cr9)
268 /* PER event mask of the user specified per set. */
269 child->thread.per_user.control =
270 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
271 else if (addr == (addr_t) &dummy->starting_addr)
272 /* Starting address of the user specified per set. */
273 child->thread.per_user.start = data;
274 else if (addr == (addr_t) &dummy->ending_addr)
275 /* Ending address of the user specified per set. */
276 child->thread.per_user.end = data;
277}
278
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200279/*
280 * Write a word to the user area of a process at location addr. This
281 * operation does have an additional problem compared to peek_user.
282 * Stores to the program status word and on the floating point
283 * control register needs to get checked for validity.
284 */
285static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
286{
287 struct user *dummy = NULL;
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100288 addr_t offset;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (addr < (addr_t) &dummy->regs.acrs) {
291 /*
292 * psw and gprs are stored on the stack
293 */
294 if (addr == (addr_t) &dummy->regs.psw.mask &&
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100295 ((data & ~PSW_MASK_USER) != psw_user_bits ||
296 ((data & PSW_MASK_EA) && !(data & PSW_MASK_BA))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* Invalid psw mask. */
298 return -EINVAL;
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100299 if (addr == (addr_t) &dummy->regs.psw.addr)
300 /*
301 * The debugger changed the instruction address,
302 * reset system call restart, see signal.c:do_signal
303 */
304 task_thread_info(child)->system_call = 0;
305
Al Viroc7584fb2006-01-12 01:05:49 -0800306 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
309 /*
310 * access registers are stored in the thread structure
311 */
312 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800313#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700314 /*
315 * Very special case: old & broken 64 bit gdb writing
316 * to acrs[15] with a 64 bit value. Ignore the lower
317 * half of the value and write the upper 32 bit to
318 * acrs[15]. Sick...
319 */
320 if (addr == (addr_t) &dummy->regs.acrs[15])
321 child->thread.acrs[15] = (unsigned int) (data >> 32);
322 else
323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
325
326 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
327 /*
328 * orig_gpr2 is stored on the kernel stack
329 */
Al Viroc7584fb2006-01-12 01:05:49 -0800330 task_pt_regs(child)->orig_gpr2 = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200332 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
333 /*
334 * prevent writes of padding hole between
335 * orig_gpr2 and fp_regs on s390.
336 */
337 return 0;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
340 /*
341 * floating point regs. are stored in the thread structure
342 */
343 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700344 (data & ~((unsigned long) FPC_VALID_MASK
345 << (BITS_PER_LONG - 32))) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -EINVAL;
347 offset = addr - (addr_t) &dummy->regs.fp_regs;
348 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
349
350 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
351 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100352 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100354 addr -= (addr_t) &dummy->regs.per_info;
355 __poke_user_per(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 }
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360}
361
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100362static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200363{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200364 addr_t mask;
365
366 /*
367 * Stupid gdb peeks/pokes the access registers in 64 bit with
368 * an alignment of 4. Programmers from hell indeed...
369 */
370 mask = __ADDR_MASK;
371#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100372 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
373 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200374 mask = 3;
375#endif
376 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
377 return -EIO;
378
379 return __poke_user(child, addr, data);
380}
381
Namhyung Kim9b05a692010-10-27 15:33:47 -0700382long arch_ptrace(struct task_struct *child, long request,
383 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 ptrace_area parea;
386 int copied, ret;
387
388 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 case PTRACE_PEEKUSR:
390 /* read the word at location addr in the USER area. */
391 return peek_user(child, addr, data);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 case PTRACE_POKEUSR:
394 /* write the word at location addr in the USER area */
395 return poke_user(child, addr, data);
396
397 case PTRACE_PEEKUSR_AREA:
398 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100399 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 sizeof(parea)))
401 return -EFAULT;
402 addr = parea.kernel_addr;
403 data = parea.process_addr;
404 copied = 0;
405 while (copied < parea.len) {
406 if (request == PTRACE_PEEKUSR_AREA)
407 ret = peek_user(child, addr, data);
408 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100409 addr_t utmp;
410 if (get_user(utmp,
411 (addr_t __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -EFAULT;
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100413 ret = poke_user(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 if (ret)
416 return ret;
417 addr += sizeof(unsigned long);
418 data += sizeof(unsigned long);
419 copied += sizeof(unsigned long);
420 }
421 return 0;
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200422 case PTRACE_GET_LAST_BREAK:
423 put_user(task_thread_info(child)->last_break,
424 (unsigned long __user *) data);
425 return 0;
Christian Borntraeger07805ac2009-09-22 22:58:48 +0200426 default:
427 /* Removing high order bit from addr (only for 31 bit). */
428 addr &= PSW_ADDR_INSN;
429 return ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800433#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * Now the fun part starts... a 31 bit program running in the
436 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
437 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
438 * to handle, the difference to the 64 bit versions of the requests
439 * is that the access is done in multiples of 4 byte instead of
440 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
441 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
442 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
443 * is a 31 bit program too, the content of struct user can be
444 * emulated. A 31 bit program peeking into the struct user of
445 * a 64 bit program is a no-no.
446 */
447
448/*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100449 * Same as peek_user_per but for a 31 bit program.
450 */
451static inline __u32 __peek_user_per_compat(struct task_struct *child,
452 addr_t addr)
453{
454 struct compat_per_struct_kernel *dummy32 = NULL;
455
456 if (addr == (addr_t) &dummy32->cr9)
457 /* Control bits of the active per set. */
458 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
459 PER_EVENT_IFETCH : child->thread.per_user.control;
460 else if (addr == (addr_t) &dummy32->cr10)
461 /* Start address of the active per set. */
462 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
463 0 : child->thread.per_user.start;
464 else if (addr == (addr_t) &dummy32->cr11)
465 /* End address of the active per set. */
466 return test_thread_flag(TIF_SINGLE_STEP) ?
467 PSW32_ADDR_INSN : child->thread.per_user.end;
468 else if (addr == (addr_t) &dummy32->bits)
469 /* Single-step bit. */
470 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
471 0x80000000 : 0;
472 else if (addr == (addr_t) &dummy32->starting_addr)
473 /* Start address of the user specified per set. */
474 return (__u32) child->thread.per_user.start;
475 else if (addr == (addr_t) &dummy32->ending_addr)
476 /* End address of the user specified per set. */
477 return (__u32) child->thread.per_user.end;
478 else if (addr == (addr_t) &dummy32->perc_atmid)
479 /* PER code, ATMID and AI of the last PER trap */
480 return (__u32) child->thread.per_event.cause << 16;
481 else if (addr == (addr_t) &dummy32->address)
482 /* Address of the last PER trap */
483 return (__u32) child->thread.per_event.address;
484 else if (addr == (addr_t) &dummy32->access_id)
485 /* Access id of the last PER trap */
486 return (__u32) child->thread.per_event.paid << 24;
487 return 0;
488}
489
490/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * Same as peek_user but for a 31 bit program.
492 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200493static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100495 struct compat_user *dummy32 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 addr_t offset;
497 __u32 tmp;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (addr < (addr_t) &dummy32->regs.acrs) {
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100500 struct pt_regs *regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /*
502 * psw and gprs are stored on the stack
503 */
504 if (addr == (addr_t) &dummy32->regs.psw.mask) {
505 /* Fake a 31 bit psw mask. */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100506 tmp = (__u32)(regs->psw.mask >> 32);
507 tmp = psw32_user_bits | (tmp & PSW32_MASK_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
509 /* Fake a 31 bit psw address. */
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100510 tmp = (__u32) regs->psw.addr |
511 (__u32)(regs->psw.mask & PSW_MASK_BA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 } else {
513 /* gpr 0-15 */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100514 tmp = *(__u32 *)((addr_t) &regs->psw + addr*2 + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
517 /*
518 * access registers are stored in the thread structure
519 */
520 offset = addr - (addr_t) &dummy32->regs.acrs;
521 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
522
523 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
524 /*
525 * orig_gpr2 is stored on the kernel stack
526 */
Al Viroc7584fb2006-01-12 01:05:49 -0800527 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200529 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
530 /*
531 * prevent reads of padding hole between
532 * orig_gpr2 and fp_regs on s390.
533 */
534 tmp = 0;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
537 /*
538 * floating point regs. are stored in the thread structure
539 */
540 offset = addr - (addr_t) &dummy32->regs.fp_regs;
541 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
542
543 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
544 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100545 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100547 addr -= (addr_t) &dummy32->regs.per_info;
548 tmp = __peek_user_per_compat(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 } else
551 tmp = 0;
552
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200553 return tmp;
554}
555
556static int peek_user_compat(struct task_struct *child,
557 addr_t addr, addr_t data)
558{
559 __u32 tmp;
560
Heiko Carstens77575912009-06-12 10:26:25 +0200561 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200562 return -EIO;
563
564 tmp = __peek_user_compat(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return put_user(tmp, (__u32 __user *) data);
566}
567
568/*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100569 * Same as poke_user_per but for a 31 bit program.
570 */
571static inline void __poke_user_per_compat(struct task_struct *child,
572 addr_t addr, __u32 data)
573{
574 struct compat_per_struct_kernel *dummy32 = NULL;
575
576 if (addr == (addr_t) &dummy32->cr9)
577 /* PER event mask of the user specified per set. */
578 child->thread.per_user.control =
579 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
580 else if (addr == (addr_t) &dummy32->starting_addr)
581 /* Starting address of the user specified per set. */
582 child->thread.per_user.start = data;
583 else if (addr == (addr_t) &dummy32->ending_addr)
584 /* Ending address of the user specified per set. */
585 child->thread.per_user.end = data;
586}
587
588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * Same as poke_user but for a 31 bit program.
590 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200591static int __poke_user_compat(struct task_struct *child,
592 addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100594 struct compat_user *dummy32 = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200595 __u32 tmp = (__u32) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 addr_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (addr < (addr_t) &dummy32->regs.acrs) {
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100599 struct pt_regs *regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
601 * psw, gprs, acrs and orig_gpr2 are stored on the stack
602 */
603 if (addr == (addr_t) &dummy32->regs.psw.mask) {
604 /* Build a 64 bit psw mask from 31 bit mask. */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100605 if ((tmp & ~PSW32_MASK_USER) != psw32_user_bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* Invalid psw mask. */
607 return -EINVAL;
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100608 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100609 (regs->psw.mask & PSW_MASK_BA) |
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100610 (__u64)(tmp & PSW32_MASK_USER) << 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
612 /* Build a 64 bit psw address from 31 bit address. */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100613 regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100614 /* Transfer 31 bit amode bit to psw mask. */
615 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
616 (__u64)(tmp & PSW32_ADDR_AMODE);
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100617 /*
618 * The debugger changed the instruction address,
619 * reset system call restart, see signal.c:do_signal
620 */
621 task_thread_info(child)->system_call = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 } else {
623 /* gpr 0-15 */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100624 *(__u32*)((addr_t) &regs->psw + addr*2 + 4) = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
627 /*
628 * access registers are stored in the thread structure
629 */
630 offset = addr - (addr_t) &dummy32->regs.acrs;
631 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
632
633 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
634 /*
635 * orig_gpr2 is stored on the kernel stack
636 */
Al Viroc7584fb2006-01-12 01:05:49 -0800637 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200639 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
640 /*
641 * prevent writess of padding hole between
642 * orig_gpr2 and fp_regs on s390.
643 */
644 return 0;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
647 /*
648 * floating point regs. are stored in the thread structure
649 */
650 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
651 (tmp & ~FPC_VALID_MASK) != 0)
652 /* Invalid floating point control. */
653 return -EINVAL;
654 offset = addr - (addr_t) &dummy32->regs.fp_regs;
655 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
656
657 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
658 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100659 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100661 addr -= (addr_t) &dummy32->regs.per_info;
662 __poke_user_per_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return 0;
666}
667
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200668static int poke_user_compat(struct task_struct *child,
669 addr_t addr, addr_t data)
670{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100671 if (!is_compat_task() || (addr & 3) ||
672 addr > sizeof(struct compat_user) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200673 return -EIO;
674
675 return __poke_user_compat(child, addr, data);
676}
677
Roland McGrathb499d762008-05-07 09:22:57 +0200678long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
679 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Roland McGrathb499d762008-05-07 09:22:57 +0200681 unsigned long addr = caddr;
682 unsigned long data = cdata;
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100683 compat_ptrace_area parea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int copied, ret;
685
686 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 case PTRACE_PEEKUSR:
688 /* read the word at location addr in the USER area. */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200689 return peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 case PTRACE_POKEUSR:
692 /* write the word at location addr in the USER area */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200693 return poke_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 case PTRACE_PEEKUSR_AREA:
696 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100697 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 sizeof(parea)))
699 return -EFAULT;
700 addr = parea.kernel_addr;
701 data = parea.process_addr;
702 copied = 0;
703 while (copied < parea.len) {
704 if (request == PTRACE_PEEKUSR_AREA)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200705 ret = peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100707 __u32 utmp;
708 if (get_user(utmp,
709 (__u32 __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EFAULT;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200711 ret = poke_user_compat(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 if (ret)
714 return ret;
715 addr += sizeof(unsigned int);
716 data += sizeof(unsigned int);
717 copied += sizeof(unsigned int);
718 }
719 return 0;
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200720 case PTRACE_GET_LAST_BREAK:
721 put_user(task_thread_info(child)->last_break,
722 (unsigned int __user *) data);
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Roland McGrathb499d762008-05-07 09:22:57 +0200725 return compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727#endif
728
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200729asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Gerald Schaefer545c1742010-05-12 09:32:12 +0200731 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Heiko Carstensbcf5cef2009-06-12 10:26:26 +0200733 /* Do the secure computing check first. */
734 secure_computing(regs->gprs[2]);
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /*
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200737 * The sysc_tracesys code in entry.S stored the system
738 * call number to gprs[2].
Bodo Stroesserc5c3a6d2005-06-04 15:43:32 -0700739 */
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200740 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
741 (tracehook_report_syscall_entry(regs) ||
742 regs->gprs[2] >= NR_syscalls)) {
743 /*
744 * Tracing decided this syscall should not happen or the
745 * debugger stored an invalid system call number. Skip
746 * the system call and the system call restart handling.
747 */
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100748 clear_thread_flag(TIF_SYSCALL);
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200749 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200751
Josh Stone66700002009-08-24 14:43:11 -0700752 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -0700753 trace_sys_enter(regs, regs->gprs[2]);
Heiko Carstens9bf12262009-06-12 10:26:47 +0200754
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200755 if (unlikely(current->audit_context))
Heiko Carstens77575912009-06-12 10:26:25 +0200756 audit_syscall_entry(is_compat_task() ?
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200757 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
758 regs->gprs[2], regs->orig_gpr2,
759 regs->gprs[3], regs->gprs[4],
760 regs->gprs[5]);
Gerald Schaefer545c1742010-05-12 09:32:12 +0200761 return ret ?: regs->gprs[2];
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200762}
763
764asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
765{
766 if (unlikely(current->audit_context))
767 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
768 regs->gprs[2]);
769
Josh Stone66700002009-08-24 14:43:11 -0700770 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -0700771 trace_sys_exit(regs, regs->gprs[2]);
Heiko Carstens9bf12262009-06-12 10:26:47 +0200772
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200773 if (test_thread_flag(TIF_SYSCALL_TRACE))
774 tracehook_report_syscall_exit(regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200776
777/*
778 * user_regset definitions.
779 */
780
781static int s390_regs_get(struct task_struct *target,
782 const struct user_regset *regset,
783 unsigned int pos, unsigned int count,
784 void *kbuf, void __user *ubuf)
785{
786 if (target == current)
787 save_access_regs(target->thread.acrs);
788
789 if (kbuf) {
790 unsigned long *k = kbuf;
791 while (count > 0) {
792 *k++ = __peek_user(target, pos);
793 count -= sizeof(*k);
794 pos += sizeof(*k);
795 }
796 } else {
797 unsigned long __user *u = ubuf;
798 while (count > 0) {
799 if (__put_user(__peek_user(target, pos), u++))
800 return -EFAULT;
801 count -= sizeof(*u);
802 pos += sizeof(*u);
803 }
804 }
805 return 0;
806}
807
808static int s390_regs_set(struct task_struct *target,
809 const struct user_regset *regset,
810 unsigned int pos, unsigned int count,
811 const void *kbuf, const void __user *ubuf)
812{
813 int rc = 0;
814
815 if (target == current)
816 save_access_regs(target->thread.acrs);
817
818 if (kbuf) {
819 const unsigned long *k = kbuf;
820 while (count > 0 && !rc) {
821 rc = __poke_user(target, pos, *k++);
822 count -= sizeof(*k);
823 pos += sizeof(*k);
824 }
825 } else {
826 const unsigned long __user *u = ubuf;
827 while (count > 0 && !rc) {
828 unsigned long word;
829 rc = __get_user(word, u++);
830 if (rc)
831 break;
832 rc = __poke_user(target, pos, word);
833 count -= sizeof(*u);
834 pos += sizeof(*u);
835 }
836 }
837
838 if (rc == 0 && target == current)
839 restore_access_regs(target->thread.acrs);
840
841 return rc;
842}
843
844static int s390_fpregs_get(struct task_struct *target,
845 const struct user_regset *regset, unsigned int pos,
846 unsigned int count, void *kbuf, void __user *ubuf)
847{
848 if (target == current)
849 save_fp_regs(&target->thread.fp_regs);
850
851 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
852 &target->thread.fp_regs, 0, -1);
853}
854
855static int s390_fpregs_set(struct task_struct *target,
856 const struct user_regset *regset, unsigned int pos,
857 unsigned int count, const void *kbuf,
858 const void __user *ubuf)
859{
860 int rc = 0;
861
862 if (target == current)
863 save_fp_regs(&target->thread.fp_regs);
864
865 /* If setting FPC, must validate it first. */
866 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
867 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
868 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
869 0, offsetof(s390_fp_regs, fprs));
870 if (rc)
871 return rc;
872 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
873 return -EINVAL;
874 target->thread.fp_regs.fpc = fpc[0];
875 }
876
877 if (rc == 0 && count > 0)
878 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
879 target->thread.fp_regs.fprs,
880 offsetof(s390_fp_regs, fprs), -1);
881
882 if (rc == 0 && target == current)
883 restore_fp_regs(&target->thread.fp_regs);
884
885 return rc;
886}
887
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200888#ifdef CONFIG_64BIT
889
890static int s390_last_break_get(struct task_struct *target,
891 const struct user_regset *regset,
892 unsigned int pos, unsigned int count,
893 void *kbuf, void __user *ubuf)
894{
895 if (count > 0) {
896 if (kbuf) {
897 unsigned long *k = kbuf;
898 *k = task_thread_info(target)->last_break;
899 } else {
900 unsigned long __user *u = ubuf;
901 if (__put_user(task_thread_info(target)->last_break, u))
902 return -EFAULT;
903 }
904 }
905 return 0;
906}
907
Martin Schwidefskyb9340692011-12-01 13:32:17 +0100908static int s390_last_break_set(struct task_struct *target,
909 const struct user_regset *regset,
910 unsigned int pos, unsigned int count,
911 const void *kbuf, const void __user *ubuf)
912{
913 return 0;
914}
915
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200916#endif
917
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100918static int s390_system_call_get(struct task_struct *target,
919 const struct user_regset *regset,
920 unsigned int pos, unsigned int count,
921 void *kbuf, void __user *ubuf)
922{
923 unsigned int *data = &task_thread_info(target)->system_call;
924 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
925 data, 0, sizeof(unsigned int));
926}
927
928static int s390_system_call_set(struct task_struct *target,
929 const struct user_regset *regset,
930 unsigned int pos, unsigned int count,
931 const void *kbuf, const void __user *ubuf)
932{
933 unsigned int *data = &task_thread_info(target)->system_call;
934 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
935 data, 0, sizeof(unsigned int));
936}
937
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200938static const struct user_regset s390_regsets[] = {
939 [REGSET_GENERAL] = {
940 .core_note_type = NT_PRSTATUS,
941 .n = sizeof(s390_regs) / sizeof(long),
942 .size = sizeof(long),
943 .align = sizeof(long),
944 .get = s390_regs_get,
945 .set = s390_regs_set,
946 },
947 [REGSET_FP] = {
948 .core_note_type = NT_PRFPREG,
949 .n = sizeof(s390_fp_regs) / sizeof(long),
950 .size = sizeof(long),
951 .align = sizeof(long),
952 .get = s390_fpregs_get,
953 .set = s390_fpregs_set,
954 },
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200955#ifdef CONFIG_64BIT
956 [REGSET_LAST_BREAK] = {
957 .core_note_type = NT_S390_LAST_BREAK,
958 .n = 1,
959 .size = sizeof(long),
960 .align = sizeof(long),
961 .get = s390_last_break_get,
Martin Schwidefskyb9340692011-12-01 13:32:17 +0100962 .set = s390_last_break_set,
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200963 },
964#endif
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100965 [REGSET_SYSTEM_CALL] = {
966 .core_note_type = NT_S390_SYSTEM_CALL,
967 .n = 1,
968 .size = sizeof(unsigned int),
969 .align = sizeof(unsigned int),
970 .get = s390_system_call_get,
971 .set = s390_system_call_set,
972 },
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200973};
974
975static const struct user_regset_view user_s390_view = {
976 .name = UTS_MACHINE,
977 .e_machine = EM_S390,
978 .regsets = s390_regsets,
979 .n = ARRAY_SIZE(s390_regsets)
980};
981
982#ifdef CONFIG_COMPAT
983static int s390_compat_regs_get(struct task_struct *target,
984 const struct user_regset *regset,
985 unsigned int pos, unsigned int count,
986 void *kbuf, void __user *ubuf)
987{
988 if (target == current)
989 save_access_regs(target->thread.acrs);
990
991 if (kbuf) {
992 compat_ulong_t *k = kbuf;
993 while (count > 0) {
994 *k++ = __peek_user_compat(target, pos);
995 count -= sizeof(*k);
996 pos += sizeof(*k);
997 }
998 } else {
999 compat_ulong_t __user *u = ubuf;
1000 while (count > 0) {
1001 if (__put_user(__peek_user_compat(target, pos), u++))
1002 return -EFAULT;
1003 count -= sizeof(*u);
1004 pos += sizeof(*u);
1005 }
1006 }
1007 return 0;
1008}
1009
1010static int s390_compat_regs_set(struct task_struct *target,
1011 const struct user_regset *regset,
1012 unsigned int pos, unsigned int count,
1013 const void *kbuf, const void __user *ubuf)
1014{
1015 int rc = 0;
1016
1017 if (target == current)
1018 save_access_regs(target->thread.acrs);
1019
1020 if (kbuf) {
1021 const compat_ulong_t *k = kbuf;
1022 while (count > 0 && !rc) {
1023 rc = __poke_user_compat(target, pos, *k++);
1024 count -= sizeof(*k);
1025 pos += sizeof(*k);
1026 }
1027 } else {
1028 const compat_ulong_t __user *u = ubuf;
1029 while (count > 0 && !rc) {
1030 compat_ulong_t word;
1031 rc = __get_user(word, u++);
1032 if (rc)
1033 break;
1034 rc = __poke_user_compat(target, pos, word);
1035 count -= sizeof(*u);
1036 pos += sizeof(*u);
1037 }
1038 }
1039
1040 if (rc == 0 && target == current)
1041 restore_access_regs(target->thread.acrs);
1042
1043 return rc;
1044}
1045
Heiko Carstensea2a4d32009-10-06 10:34:13 +02001046static int s390_compat_regs_high_get(struct task_struct *target,
1047 const struct user_regset *regset,
1048 unsigned int pos, unsigned int count,
1049 void *kbuf, void __user *ubuf)
1050{
1051 compat_ulong_t *gprs_high;
1052
1053 gprs_high = (compat_ulong_t *)
1054 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1055 if (kbuf) {
1056 compat_ulong_t *k = kbuf;
1057 while (count > 0) {
1058 *k++ = *gprs_high;
1059 gprs_high += 2;
1060 count -= sizeof(*k);
1061 }
1062 } else {
1063 compat_ulong_t __user *u = ubuf;
1064 while (count > 0) {
1065 if (__put_user(*gprs_high, u++))
1066 return -EFAULT;
1067 gprs_high += 2;
1068 count -= sizeof(*u);
1069 }
1070 }
1071 return 0;
1072}
1073
1074static int s390_compat_regs_high_set(struct task_struct *target,
1075 const struct user_regset *regset,
1076 unsigned int pos, unsigned int count,
1077 const void *kbuf, const void __user *ubuf)
1078{
1079 compat_ulong_t *gprs_high;
1080 int rc = 0;
1081
1082 gprs_high = (compat_ulong_t *)
1083 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1084 if (kbuf) {
1085 const compat_ulong_t *k = kbuf;
1086 while (count > 0) {
1087 *gprs_high = *k++;
1088 *gprs_high += 2;
1089 count -= sizeof(*k);
1090 }
1091 } else {
1092 const compat_ulong_t __user *u = ubuf;
1093 while (count > 0 && !rc) {
1094 unsigned long word;
1095 rc = __get_user(word, u++);
1096 if (rc)
1097 break;
1098 *gprs_high = word;
1099 *gprs_high += 2;
1100 count -= sizeof(*u);
1101 }
1102 }
1103
1104 return rc;
1105}
1106
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001107static int s390_compat_last_break_get(struct task_struct *target,
1108 const struct user_regset *regset,
1109 unsigned int pos, unsigned int count,
1110 void *kbuf, void __user *ubuf)
1111{
1112 compat_ulong_t last_break;
1113
1114 if (count > 0) {
1115 last_break = task_thread_info(target)->last_break;
1116 if (kbuf) {
1117 unsigned long *k = kbuf;
1118 *k = last_break;
1119 } else {
1120 unsigned long __user *u = ubuf;
1121 if (__put_user(last_break, u))
1122 return -EFAULT;
1123 }
1124 }
1125 return 0;
1126}
1127
Martin Schwidefskyb9340692011-12-01 13:32:17 +01001128static int s390_compat_last_break_set(struct task_struct *target,
1129 const struct user_regset *regset,
1130 unsigned int pos, unsigned int count,
1131 const void *kbuf, const void __user *ubuf)
1132{
1133 return 0;
1134}
1135
Martin Schwidefsky63506c42008-07-14 09:58:54 +02001136static const struct user_regset s390_compat_regsets[] = {
1137 [REGSET_GENERAL] = {
1138 .core_note_type = NT_PRSTATUS,
1139 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
1140 .size = sizeof(compat_long_t),
1141 .align = sizeof(compat_long_t),
1142 .get = s390_compat_regs_get,
1143 .set = s390_compat_regs_set,
1144 },
1145 [REGSET_FP] = {
1146 .core_note_type = NT_PRFPREG,
1147 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
1148 .size = sizeof(compat_long_t),
1149 .align = sizeof(compat_long_t),
1150 .get = s390_fpregs_get,
1151 .set = s390_fpregs_set,
1152 },
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001153 [REGSET_LAST_BREAK] = {
1154 .core_note_type = NT_S390_LAST_BREAK,
1155 .n = 1,
1156 .size = sizeof(long),
1157 .align = sizeof(long),
1158 .get = s390_compat_last_break_get,
Martin Schwidefskyb9340692011-12-01 13:32:17 +01001159 .set = s390_compat_last_break_set,
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001160 },
Martin Schwidefsky20b40a72011-10-30 15:16:47 +01001161 [REGSET_SYSTEM_CALL] = {
1162 .core_note_type = NT_S390_SYSTEM_CALL,
1163 .n = 1,
1164 .size = sizeof(compat_uint_t),
1165 .align = sizeof(compat_uint_t),
1166 .get = s390_system_call_get,
1167 .set = s390_system_call_set,
1168 },
Heiko Carstensea2a4d32009-10-06 10:34:13 +02001169 [REGSET_GENERAL_EXTENDED] = {
Martin Schwidefsky622e99b2009-12-18 17:43:20 +01001170 .core_note_type = NT_S390_HIGH_GPRS,
Heiko Carstensea2a4d32009-10-06 10:34:13 +02001171 .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
1172 .size = sizeof(compat_long_t),
1173 .align = sizeof(compat_long_t),
1174 .get = s390_compat_regs_high_get,
1175 .set = s390_compat_regs_high_set,
1176 },
Martin Schwidefsky63506c42008-07-14 09:58:54 +02001177};
1178
1179static const struct user_regset_view user_s390_compat_view = {
1180 .name = "s390",
1181 .e_machine = EM_S390,
1182 .regsets = s390_compat_regsets,
1183 .n = ARRAY_SIZE(s390_compat_regsets)
1184};
1185#endif
1186
1187const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1188{
1189#ifdef CONFIG_COMPAT
1190 if (test_tsk_thread_flag(task, TIF_31BIT))
1191 return &user_s390_compat_view;
1192#endif
1193 return &user_s390_view;
1194}
Heiko Carstens952974ac62010-02-12 13:38:40 +01001195
1196static const char *gpr_names[NUM_GPRS] = {
1197 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1198 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1199};
1200
1201unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
1202{
1203 if (offset >= NUM_GPRS)
1204 return 0;
1205 return regs->gprs[offset];
1206}
1207
1208int regs_query_register_offset(const char *name)
1209{
1210 unsigned long offset;
1211
1212 if (!name || *name != 'r')
1213 return -EINVAL;
1214 if (strict_strtoul(name + 1, 10, &offset))
1215 return -EINVAL;
1216 if (offset >= NUM_GPRS)
1217 return -EINVAL;
1218 return offset;
1219}
1220
1221const char *regs_query_register_name(unsigned int offset)
1222{
1223 if (offset >= NUM_GPRS)
1224 return NULL;
1225 return gpr_names[offset];
1226}
1227
1228static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
1229{
1230 unsigned long ksp = kernel_stack_pointer(regs);
1231
1232 return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
1233}
1234
1235/**
1236 * regs_get_kernel_stack_nth() - get Nth entry of the stack
1237 * @regs:pt_regs which contains kernel stack pointer.
1238 * @n:stack entry number.
1239 *
1240 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
1241 * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
1242 * this returns 0.
1243 */
1244unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
1245{
1246 unsigned long addr;
1247
1248 addr = kernel_stack_pointer(regs) + n * sizeof(long);
1249 if (!regs_within_kernel_stack(regs, addr))
1250 return 0;
1251 return *(unsigned long *)addr;
1252}