blob: ae0e14b8880ca8a8176ea866bc2ac6b622a4e057 [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,
Heiko Carstensea2a4d32009-10-06 10:34:13 +020045 REGSET_GENERAL_EXTENDED,
Martin Schwidefsky63506c42008-07-14 09:58:54 +020046};
47
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010048void update_per_regs(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010050 struct pt_regs *regs = task_pt_regs(task);
51 struct thread_struct *thread = &task->thread;
Martin Schwidefskya45aff52011-10-30 15:16:07 +010052 struct per_regs old, new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Martin Schwidefskya45aff52011-10-30 15:16:07 +010054 /* Copy user specified PER registers */
55 new.control = thread->per_user.control;
56 new.start = thread->per_user.start;
57 new.end = thread->per_user.end;
58
59 /* merge TIF_SINGLE_STEP into user specified PER registers. */
60 if (test_tsk_thread_flag(task, TIF_SINGLE_STEP)) {
61 new.control |= PER_EVENT_IFETCH;
62 new.start = 0;
63 new.end = PSW_ADDR_INSN;
64 }
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010065
66 /* Take care of the PER enablement bit in the PSW. */
Martin Schwidefskya45aff52011-10-30 15:16:07 +010067 if (!(new.control & PER_EVENT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 regs->psw.mask &= ~PSW_MASK_PER;
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010069 return;
Martin Schwidefskyc3311c12010-01-13 20:44:25 +010070 }
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010071 regs->psw.mask |= PSW_MASK_PER;
72 __ctl_store(old, 9, 11);
Martin Schwidefskya45aff52011-10-30 15:16:07 +010073 if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
74 __ctl_load(new, 9, 11);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Roland McGrath0ac30be2008-01-26 14:11:22 +010077void user_enable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010079 set_tsk_thread_flag(task, TIF_SINGLE_STEP);
80 if (task == current)
81 update_per_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Roland McGrath0ac30be2008-01-26 14:11:22 +010084void user_disable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010086 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
87 if (task == current)
88 update_per_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91/*
92 * Called by kernel/ptrace.c when detaching..
93 *
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010094 * Clear all debugging related fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010096void ptrace_disable(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010098 memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
99 memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
100 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
101 clear_tsk_thread_flag(task, TIF_PER_TRAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800104#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105# define __ADDR_MASK 3
106#else
107# define __ADDR_MASK 7
108#endif
109
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100110static inline unsigned long __peek_user_per(struct task_struct *child,
111 addr_t addr)
112{
113 struct per_struct_kernel *dummy = NULL;
114
115 if (addr == (addr_t) &dummy->cr9)
116 /* Control bits of the active per set. */
117 return test_thread_flag(TIF_SINGLE_STEP) ?
118 PER_EVENT_IFETCH : child->thread.per_user.control;
119 else if (addr == (addr_t) &dummy->cr10)
120 /* Start address of the active per set. */
121 return test_thread_flag(TIF_SINGLE_STEP) ?
122 0 : child->thread.per_user.start;
123 else if (addr == (addr_t) &dummy->cr11)
124 /* End address of the active per set. */
125 return test_thread_flag(TIF_SINGLE_STEP) ?
126 PSW_ADDR_INSN : child->thread.per_user.end;
127 else if (addr == (addr_t) &dummy->bits)
128 /* Single-step bit. */
129 return test_thread_flag(TIF_SINGLE_STEP) ?
130 (1UL << (BITS_PER_LONG - 1)) : 0;
131 else if (addr == (addr_t) &dummy->starting_addr)
132 /* Start address of the user specified per set. */
133 return child->thread.per_user.start;
134 else if (addr == (addr_t) &dummy->ending_addr)
135 /* End address of the user specified per set. */
136 return child->thread.per_user.end;
137 else if (addr == (addr_t) &dummy->perc_atmid)
138 /* PER code, ATMID and AI of the last PER trap */
139 return (unsigned long)
140 child->thread.per_event.cause << (BITS_PER_LONG - 16);
141 else if (addr == (addr_t) &dummy->address)
142 /* Address of the last PER trap */
143 return child->thread.per_event.address;
144 else if (addr == (addr_t) &dummy->access_id)
145 /* Access id of the last PER trap */
146 return (unsigned long)
147 child->thread.per_event.paid << (BITS_PER_LONG - 8);
148 return 0;
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
152 * Read the word at offset addr from the user area of a process. The
153 * trouble here is that the information is littered over different
154 * locations. The process registers are found on the kernel stack,
155 * the floating point stuff and the trace settings are stored in
156 * the task structure. In addition the different structures in
157 * struct user contain pad bytes that should be read as zeroes.
158 * Lovely...
159 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200160static unsigned long __peek_user(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct user *dummy = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200163 addr_t offset, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 if (addr < (addr_t) &dummy->regs.acrs) {
166 /*
167 * psw and gprs are stored on the stack
168 */
Al Viroc7584fb2006-01-12 01:05:49 -0800169 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (addr == (addr_t) &dummy->regs.psw.mask)
171 /* Remove per bit from user psw. */
172 tmp &= ~PSW_MASK_PER;
173
174 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
175 /*
176 * access registers are stored in the thread structure
177 */
178 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800179#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700180 /*
181 * Very special case: old & broken 64 bit gdb reading
182 * from acrs[15]. Result is a 64 bit value. Read the
183 * 32 bit acrs[15] value and shift it by 32. Sick...
184 */
185 if (addr == (addr_t) &dummy->regs.acrs[15])
186 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
187 else
188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
190
191 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
192 /*
193 * orig_gpr2 is stored on the kernel stack
194 */
Al Viroc7584fb2006-01-12 01:05:49 -0800195 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200197 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
198 /*
199 * prevent reads of padding hole between
200 * orig_gpr2 and fp_regs on s390.
201 */
202 tmp = 0;
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
205 /*
206 * floating point regs. are stored in the thread structure
207 */
208 offset = addr - (addr_t) &dummy->regs.fp_regs;
209 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700210 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
211 tmp &= (unsigned long) FPC_VALID_MASK
212 << (BITS_PER_LONG - 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
215 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100216 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100218 addr -= (addr_t) &dummy->regs.per_info;
219 tmp = __peek_user_per(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 } else
222 tmp = 0;
223
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200224 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static int
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200228peek_user(struct task_struct *child, addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200230 addr_t tmp, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /*
233 * Stupid gdb peeks/pokes the access registers in 64 bit with
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200234 * an alignment of 4. Programmers from hell...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700236 mask = __ADDR_MASK;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800237#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100238 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
239 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700240 mask = 3;
241#endif
242 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -EIO;
244
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200245 tmp = __peek_user(child, addr);
246 return put_user(tmp, (addr_t __user *) data);
247}
248
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100249static inline void __poke_user_per(struct task_struct *child,
250 addr_t addr, addr_t data)
251{
252 struct per_struct_kernel *dummy = NULL;
253
254 /*
255 * There are only three fields in the per_info struct that the
256 * debugger user can write to.
257 * 1) cr9: the debugger wants to set a new PER event mask
258 * 2) starting_addr: the debugger wants to set a new starting
259 * address to use with the PER event mask.
260 * 3) ending_addr: the debugger wants to set a new ending
261 * address to use with the PER event mask.
262 * The user specified PER event mask and the start and end
263 * addresses are used only if single stepping is not in effect.
264 * Writes to any other field in per_info are ignored.
265 */
266 if (addr == (addr_t) &dummy->cr9)
267 /* PER event mask of the user specified per set. */
268 child->thread.per_user.control =
269 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
270 else if (addr == (addr_t) &dummy->starting_addr)
271 /* Starting address of the user specified per set. */
272 child->thread.per_user.start = data;
273 else if (addr == (addr_t) &dummy->ending_addr)
274 /* Ending address of the user specified per set. */
275 child->thread.per_user.end = data;
276}
277
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200278/*
279 * Write a word to the user area of a process at location addr. This
280 * operation does have an additional problem compared to peek_user.
281 * Stores to the program status word and on the floating point
282 * control register needs to get checked for validity.
283 */
284static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
285{
286 struct user *dummy = NULL;
287 addr_t offset;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (addr < (addr_t) &dummy->regs.acrs) {
290 /*
291 * psw and gprs are stored on the stack
292 */
293 if (addr == (addr_t) &dummy->regs.psw.mask &&
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800294#ifdef CONFIG_COMPAT
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100295 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296#endif
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100297 data != PSW_MASK_MERGE(psw_user_bits, data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* Invalid psw mask. */
299 return -EINVAL;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800300#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (addr == (addr_t) &dummy->regs.psw.addr)
302 /* I'd like to reject addresses without the
303 high order bit but older gdb's rely on it */
304 data |= PSW_ADDR_AMODE;
305#endif
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) {
500 /*
501 * psw and gprs are stored on the stack
502 */
503 if (addr == (addr_t) &dummy32->regs.psw.mask) {
504 /* Fake a 31 bit psw mask. */
Al Viroc7584fb2006-01-12 01:05:49 -0800505 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100506 tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
508 /* Fake a 31 bit psw address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800509 tmp = (__u32) task_pt_regs(child)->psw.addr |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 PSW32_ADDR_AMODE31;
511 } else {
512 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800513 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 addr*2 + 4);
515 }
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) {
599 /*
600 * psw, gprs, acrs and orig_gpr2 are stored on the stack
601 */
602 if (addr == (addr_t) &dummy32->regs.psw.mask) {
603 /* Build a 64 bit psw mask from 31 bit mask. */
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100604 if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Invalid psw mask. */
606 return -EINVAL;
Al Viroc7584fb2006-01-12 01:05:49 -0800607 task_pt_regs(child)->psw.mask =
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100608 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
610 /* Build a 64 bit psw address from 31 bit address. */
Al Viroc7584fb2006-01-12 01:05:49 -0800611 task_pt_regs(child)->psw.addr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 (__u64) tmp & PSW32_ADDR_INSN;
613 } else {
614 /* gpr 0-15 */
Al Viroc7584fb2006-01-12 01:05:49 -0800615 *(__u32*)((addr_t) &task_pt_regs(child)->psw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 + addr*2 + 4) = tmp;
617 }
618 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
619 /*
620 * access registers are stored in the thread structure
621 */
622 offset = addr - (addr_t) &dummy32->regs.acrs;
623 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
624
625 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
626 /*
627 * orig_gpr2 is stored on the kernel stack
628 */
Al Viroc7584fb2006-01-12 01:05:49 -0800629 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200631 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
632 /*
633 * prevent writess of padding hole between
634 * orig_gpr2 and fp_regs on s390.
635 */
636 return 0;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
639 /*
640 * floating point regs. are stored in the thread structure
641 */
642 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
643 (tmp & ~FPC_VALID_MASK) != 0)
644 /* Invalid floating point control. */
645 return -EINVAL;
646 offset = addr - (addr_t) &dummy32->regs.fp_regs;
647 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
648
649 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
650 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100651 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100653 addr -= (addr_t) &dummy32->regs.per_info;
654 __poke_user_per_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 0;
658}
659
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200660static int poke_user_compat(struct task_struct *child,
661 addr_t addr, addr_t data)
662{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100663 if (!is_compat_task() || (addr & 3) ||
664 addr > sizeof(struct compat_user) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200665 return -EIO;
666
667 return __poke_user_compat(child, addr, data);
668}
669
Roland McGrathb499d762008-05-07 09:22:57 +0200670long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
671 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Roland McGrathb499d762008-05-07 09:22:57 +0200673 unsigned long addr = caddr;
674 unsigned long data = cdata;
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100675 compat_ptrace_area parea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int copied, ret;
677
678 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 case PTRACE_PEEKUSR:
680 /* read the word at location addr in the USER area. */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200681 return peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 case PTRACE_POKEUSR:
684 /* write the word at location addr in the USER area */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200685 return poke_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 case PTRACE_PEEKUSR_AREA:
688 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100689 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 sizeof(parea)))
691 return -EFAULT;
692 addr = parea.kernel_addr;
693 data = parea.process_addr;
694 copied = 0;
695 while (copied < parea.len) {
696 if (request == PTRACE_PEEKUSR_AREA)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200697 ret = peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100699 __u32 utmp;
700 if (get_user(utmp,
701 (__u32 __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EFAULT;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200703 ret = poke_user_compat(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 if (ret)
706 return ret;
707 addr += sizeof(unsigned int);
708 data += sizeof(unsigned int);
709 copied += sizeof(unsigned int);
710 }
711 return 0;
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200712 case PTRACE_GET_LAST_BREAK:
713 put_user(task_thread_info(child)->last_break,
714 (unsigned int __user *) data);
715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Roland McGrathb499d762008-05-07 09:22:57 +0200717 return compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719#endif
720
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200721asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Gerald Schaefer545c1742010-05-12 09:32:12 +0200723 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Heiko Carstensbcf5cef2009-06-12 10:26:26 +0200725 /* Do the secure computing check first. */
726 secure_computing(regs->gprs[2]);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /*
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200729 * The sysc_tracesys code in entry.S stored the system
730 * call number to gprs[2].
Bodo Stroesserc5c3a6d2005-06-04 15:43:32 -0700731 */
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200732 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
733 (tracehook_report_syscall_entry(regs) ||
734 regs->gprs[2] >= NR_syscalls)) {
735 /*
736 * Tracing decided this syscall should not happen or the
737 * debugger stored an invalid system call number. Skip
738 * the system call and the system call restart handling.
739 */
Martin Schwidefsky59da2132008-11-27 11:05:55 +0100740 regs->svcnr = 0;
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200741 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200743
Josh Stone66700002009-08-24 14:43:11 -0700744 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -0700745 trace_sys_enter(regs, regs->gprs[2]);
Heiko Carstens9bf12262009-06-12 10:26:47 +0200746
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200747 if (unlikely(current->audit_context))
Heiko Carstens77575912009-06-12 10:26:25 +0200748 audit_syscall_entry(is_compat_task() ?
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200749 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
750 regs->gprs[2], regs->orig_gpr2,
751 regs->gprs[3], regs->gprs[4],
752 regs->gprs[5]);
Gerald Schaefer545c1742010-05-12 09:32:12 +0200753 return ret ?: regs->gprs[2];
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200754}
755
756asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
757{
758 if (unlikely(current->audit_context))
759 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
760 regs->gprs[2]);
761
Josh Stone66700002009-08-24 14:43:11 -0700762 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -0700763 trace_sys_exit(regs, regs->gprs[2]);
Heiko Carstens9bf12262009-06-12 10:26:47 +0200764
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200765 if (test_thread_flag(TIF_SYSCALL_TRACE))
766 tracehook_report_syscall_exit(regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200768
769/*
770 * user_regset definitions.
771 */
772
773static int s390_regs_get(struct task_struct *target,
774 const struct user_regset *regset,
775 unsigned int pos, unsigned int count,
776 void *kbuf, void __user *ubuf)
777{
778 if (target == current)
779 save_access_regs(target->thread.acrs);
780
781 if (kbuf) {
782 unsigned long *k = kbuf;
783 while (count > 0) {
784 *k++ = __peek_user(target, pos);
785 count -= sizeof(*k);
786 pos += sizeof(*k);
787 }
788 } else {
789 unsigned long __user *u = ubuf;
790 while (count > 0) {
791 if (__put_user(__peek_user(target, pos), u++))
792 return -EFAULT;
793 count -= sizeof(*u);
794 pos += sizeof(*u);
795 }
796 }
797 return 0;
798}
799
800static int s390_regs_set(struct task_struct *target,
801 const struct user_regset *regset,
802 unsigned int pos, unsigned int count,
803 const void *kbuf, const void __user *ubuf)
804{
805 int rc = 0;
806
807 if (target == current)
808 save_access_regs(target->thread.acrs);
809
810 if (kbuf) {
811 const unsigned long *k = kbuf;
812 while (count > 0 && !rc) {
813 rc = __poke_user(target, pos, *k++);
814 count -= sizeof(*k);
815 pos += sizeof(*k);
816 }
817 } else {
818 const unsigned long __user *u = ubuf;
819 while (count > 0 && !rc) {
820 unsigned long word;
821 rc = __get_user(word, u++);
822 if (rc)
823 break;
824 rc = __poke_user(target, pos, word);
825 count -= sizeof(*u);
826 pos += sizeof(*u);
827 }
828 }
829
830 if (rc == 0 && target == current)
831 restore_access_regs(target->thread.acrs);
832
833 return rc;
834}
835
836static int s390_fpregs_get(struct task_struct *target,
837 const struct user_regset *regset, unsigned int pos,
838 unsigned int count, void *kbuf, void __user *ubuf)
839{
840 if (target == current)
841 save_fp_regs(&target->thread.fp_regs);
842
843 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
844 &target->thread.fp_regs, 0, -1);
845}
846
847static int s390_fpregs_set(struct task_struct *target,
848 const struct user_regset *regset, unsigned int pos,
849 unsigned int count, const void *kbuf,
850 const void __user *ubuf)
851{
852 int rc = 0;
853
854 if (target == current)
855 save_fp_regs(&target->thread.fp_regs);
856
857 /* If setting FPC, must validate it first. */
858 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
859 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
860 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
861 0, offsetof(s390_fp_regs, fprs));
862 if (rc)
863 return rc;
864 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
865 return -EINVAL;
866 target->thread.fp_regs.fpc = fpc[0];
867 }
868
869 if (rc == 0 && count > 0)
870 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
871 target->thread.fp_regs.fprs,
872 offsetof(s390_fp_regs, fprs), -1);
873
874 if (rc == 0 && target == current)
875 restore_fp_regs(&target->thread.fp_regs);
876
877 return rc;
878}
879
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200880#ifdef CONFIG_64BIT
881
882static int s390_last_break_get(struct task_struct *target,
883 const struct user_regset *regset,
884 unsigned int pos, unsigned int count,
885 void *kbuf, void __user *ubuf)
886{
887 if (count > 0) {
888 if (kbuf) {
889 unsigned long *k = kbuf;
890 *k = task_thread_info(target)->last_break;
891 } else {
892 unsigned long __user *u = ubuf;
893 if (__put_user(task_thread_info(target)->last_break, u))
894 return -EFAULT;
895 }
896 }
897 return 0;
898}
899
900#endif
901
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200902static const struct user_regset s390_regsets[] = {
903 [REGSET_GENERAL] = {
904 .core_note_type = NT_PRSTATUS,
905 .n = sizeof(s390_regs) / sizeof(long),
906 .size = sizeof(long),
907 .align = sizeof(long),
908 .get = s390_regs_get,
909 .set = s390_regs_set,
910 },
911 [REGSET_FP] = {
912 .core_note_type = NT_PRFPREG,
913 .n = sizeof(s390_fp_regs) / sizeof(long),
914 .size = sizeof(long),
915 .align = sizeof(long),
916 .get = s390_fpregs_get,
917 .set = s390_fpregs_set,
918 },
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200919#ifdef CONFIG_64BIT
920 [REGSET_LAST_BREAK] = {
921 .core_note_type = NT_S390_LAST_BREAK,
922 .n = 1,
923 .size = sizeof(long),
924 .align = sizeof(long),
925 .get = s390_last_break_get,
926 },
927#endif
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200928};
929
930static const struct user_regset_view user_s390_view = {
931 .name = UTS_MACHINE,
932 .e_machine = EM_S390,
933 .regsets = s390_regsets,
934 .n = ARRAY_SIZE(s390_regsets)
935};
936
937#ifdef CONFIG_COMPAT
938static int s390_compat_regs_get(struct task_struct *target,
939 const struct user_regset *regset,
940 unsigned int pos, unsigned int count,
941 void *kbuf, void __user *ubuf)
942{
943 if (target == current)
944 save_access_regs(target->thread.acrs);
945
946 if (kbuf) {
947 compat_ulong_t *k = kbuf;
948 while (count > 0) {
949 *k++ = __peek_user_compat(target, pos);
950 count -= sizeof(*k);
951 pos += sizeof(*k);
952 }
953 } else {
954 compat_ulong_t __user *u = ubuf;
955 while (count > 0) {
956 if (__put_user(__peek_user_compat(target, pos), u++))
957 return -EFAULT;
958 count -= sizeof(*u);
959 pos += sizeof(*u);
960 }
961 }
962 return 0;
963}
964
965static int s390_compat_regs_set(struct task_struct *target,
966 const struct user_regset *regset,
967 unsigned int pos, unsigned int count,
968 const void *kbuf, const void __user *ubuf)
969{
970 int rc = 0;
971
972 if (target == current)
973 save_access_regs(target->thread.acrs);
974
975 if (kbuf) {
976 const compat_ulong_t *k = kbuf;
977 while (count > 0 && !rc) {
978 rc = __poke_user_compat(target, pos, *k++);
979 count -= sizeof(*k);
980 pos += sizeof(*k);
981 }
982 } else {
983 const compat_ulong_t __user *u = ubuf;
984 while (count > 0 && !rc) {
985 compat_ulong_t word;
986 rc = __get_user(word, u++);
987 if (rc)
988 break;
989 rc = __poke_user_compat(target, pos, word);
990 count -= sizeof(*u);
991 pos += sizeof(*u);
992 }
993 }
994
995 if (rc == 0 && target == current)
996 restore_access_regs(target->thread.acrs);
997
998 return rc;
999}
1000
Heiko Carstensea2a4d32009-10-06 10:34:13 +02001001static int s390_compat_regs_high_get(struct task_struct *target,
1002 const struct user_regset *regset,
1003 unsigned int pos, unsigned int count,
1004 void *kbuf, void __user *ubuf)
1005{
1006 compat_ulong_t *gprs_high;
1007
1008 gprs_high = (compat_ulong_t *)
1009 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1010 if (kbuf) {
1011 compat_ulong_t *k = kbuf;
1012 while (count > 0) {
1013 *k++ = *gprs_high;
1014 gprs_high += 2;
1015 count -= sizeof(*k);
1016 }
1017 } else {
1018 compat_ulong_t __user *u = ubuf;
1019 while (count > 0) {
1020 if (__put_user(*gprs_high, u++))
1021 return -EFAULT;
1022 gprs_high += 2;
1023 count -= sizeof(*u);
1024 }
1025 }
1026 return 0;
1027}
1028
1029static int s390_compat_regs_high_set(struct task_struct *target,
1030 const struct user_regset *regset,
1031 unsigned int pos, unsigned int count,
1032 const void *kbuf, const void __user *ubuf)
1033{
1034 compat_ulong_t *gprs_high;
1035 int rc = 0;
1036
1037 gprs_high = (compat_ulong_t *)
1038 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1039 if (kbuf) {
1040 const compat_ulong_t *k = kbuf;
1041 while (count > 0) {
1042 *gprs_high = *k++;
1043 *gprs_high += 2;
1044 count -= sizeof(*k);
1045 }
1046 } else {
1047 const compat_ulong_t __user *u = ubuf;
1048 while (count > 0 && !rc) {
1049 unsigned long word;
1050 rc = __get_user(word, u++);
1051 if (rc)
1052 break;
1053 *gprs_high = word;
1054 *gprs_high += 2;
1055 count -= sizeof(*u);
1056 }
1057 }
1058
1059 return rc;
1060}
1061
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001062static int s390_compat_last_break_get(struct task_struct *target,
1063 const struct user_regset *regset,
1064 unsigned int pos, unsigned int count,
1065 void *kbuf, void __user *ubuf)
1066{
1067 compat_ulong_t last_break;
1068
1069 if (count > 0) {
1070 last_break = task_thread_info(target)->last_break;
1071 if (kbuf) {
1072 unsigned long *k = kbuf;
1073 *k = last_break;
1074 } else {
1075 unsigned long __user *u = ubuf;
1076 if (__put_user(last_break, u))
1077 return -EFAULT;
1078 }
1079 }
1080 return 0;
1081}
1082
Martin Schwidefsky63506c42008-07-14 09:58:54 +02001083static const struct user_regset s390_compat_regsets[] = {
1084 [REGSET_GENERAL] = {
1085 .core_note_type = NT_PRSTATUS,
1086 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
1087 .size = sizeof(compat_long_t),
1088 .align = sizeof(compat_long_t),
1089 .get = s390_compat_regs_get,
1090 .set = s390_compat_regs_set,
1091 },
1092 [REGSET_FP] = {
1093 .core_note_type = NT_PRFPREG,
1094 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
1095 .size = sizeof(compat_long_t),
1096 .align = sizeof(compat_long_t),
1097 .get = s390_fpregs_get,
1098 .set = s390_fpregs_set,
1099 },
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001100 [REGSET_LAST_BREAK] = {
1101 .core_note_type = NT_S390_LAST_BREAK,
1102 .n = 1,
1103 .size = sizeof(long),
1104 .align = sizeof(long),
1105 .get = s390_compat_last_break_get,
1106 },
Heiko Carstensea2a4d32009-10-06 10:34:13 +02001107 [REGSET_GENERAL_EXTENDED] = {
Martin Schwidefsky622e99b2009-12-18 17:43:20 +01001108 .core_note_type = NT_S390_HIGH_GPRS,
Heiko Carstensea2a4d32009-10-06 10:34:13 +02001109 .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
1110 .size = sizeof(compat_long_t),
1111 .align = sizeof(compat_long_t),
1112 .get = s390_compat_regs_high_get,
1113 .set = s390_compat_regs_high_set,
1114 },
Martin Schwidefsky63506c42008-07-14 09:58:54 +02001115};
1116
1117static const struct user_regset_view user_s390_compat_view = {
1118 .name = "s390",
1119 .e_machine = EM_S390,
1120 .regsets = s390_compat_regsets,
1121 .n = ARRAY_SIZE(s390_compat_regsets)
1122};
1123#endif
1124
1125const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1126{
1127#ifdef CONFIG_COMPAT
1128 if (test_tsk_thread_flag(task, TIF_31BIT))
1129 return &user_s390_compat_view;
1130#endif
1131 return &user_s390_view;
1132}
Heiko Carstens952974ac62010-02-12 13:38:40 +01001133
1134static const char *gpr_names[NUM_GPRS] = {
1135 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1136 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1137};
1138
1139unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
1140{
1141 if (offset >= NUM_GPRS)
1142 return 0;
1143 return regs->gprs[offset];
1144}
1145
1146int regs_query_register_offset(const char *name)
1147{
1148 unsigned long offset;
1149
1150 if (!name || *name != 'r')
1151 return -EINVAL;
1152 if (strict_strtoul(name + 1, 10, &offset))
1153 return -EINVAL;
1154 if (offset >= NUM_GPRS)
1155 return -EINVAL;
1156 return offset;
1157}
1158
1159const char *regs_query_register_name(unsigned int offset)
1160{
1161 if (offset >= NUM_GPRS)
1162 return NULL;
1163 return gpr_names[offset];
1164}
1165
1166static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
1167{
1168 unsigned long ksp = kernel_stack_pointer(regs);
1169
1170 return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
1171}
1172
1173/**
1174 * regs_get_kernel_stack_nth() - get Nth entry of the stack
1175 * @regs:pt_regs which contains kernel stack pointer.
1176 * @n:stack entry number.
1177 *
1178 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
1179 * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
1180 * this returns 0.
1181 */
1182unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
1183{
1184 unsigned long addr;
1185
1186 addr = kernel_stack_pointer(regs) + n * sizeof(long);
1187 if (!regs_within_kernel_stack(regs, addr))
1188 return 0;
1189 return *(unsigned long *)addr;
1190}