blob: b817cc5e49aea592146702b932fb087caef4f6cf [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 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 1999, 2010
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +01005 * 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 Carstens048cd4e2012-02-27 10:01:52 +010023#include <linux/compat.h>
Heiko Carstens9bf12262009-06-12 10:26:47 +020024#include <trace/syscall.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
Martin Schwidefsky778959d2005-06-04 15:43:30 -070030#include <asm/unistd.h>
David Howellsa0616cd2012-03-28 18:30:02 +010031#include <asm/switch_to.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 Schwidefskyd35339a2012-07-31 11:03:04 +020045 REGSET_TDB,
Martin Schwidefsky20b40a72011-10-30 15:16:47 +010046 REGSET_SYSTEM_CALL,
Heiko Carstensea2a4d32009-10-06 10:34:13 +020047 REGSET_GENERAL_EXTENDED,
Martin Schwidefsky63506c42008-07-14 09:58:54 +020048};
49
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010050void update_per_regs(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010052 struct pt_regs *regs = task_pt_regs(task);
53 struct thread_struct *thread = &task->thread;
Martin Schwidefskya45aff52011-10-30 15:16:07 +010054 struct per_regs old, new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Martin Schwidefskyd35339a2012-07-31 11:03:04 +020056 /* Take care of the enable/disable of transactional execution. */
57 if (MACHINE_HAS_TE) {
58 unsigned long cr0, cr0_new;
59
60 __ctl_store(cr0, 0, 0);
61 /* set or clear transaction execution bits 8 and 9. */
62 if (task->thread.per_flags & PER_FLAG_NO_TE)
63 cr0_new = cr0 & ~(3UL << 54);
64 else
65 cr0_new = cr0 | (3UL << 54);
66 /* Only load control register 0 if necessary. */
67 if (cr0 != cr0_new)
68 __ctl_load(cr0_new, 0, 0);
69 }
70
Martin Schwidefskya45aff52011-10-30 15:16:07 +010071 /* Copy user specified PER registers */
72 new.control = thread->per_user.control;
73 new.start = thread->per_user.start;
74 new.end = thread->per_user.end;
75
76 /* merge TIF_SINGLE_STEP into user specified PER registers. */
77 if (test_tsk_thread_flag(task, TIF_SINGLE_STEP)) {
78 new.control |= PER_EVENT_IFETCH;
Martin Schwidefskyd35339a2012-07-31 11:03:04 +020079#ifdef CONFIG_64BIT
80 new.control |= PER_CONTROL_SUSPENSION;
81 new.control |= PER_EVENT_TRANSACTION_END;
82#endif
Martin Schwidefskya45aff52011-10-30 15:16:07 +010083 new.start = 0;
84 new.end = PSW_ADDR_INSN;
85 }
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010086
87 /* Take care of the PER enablement bit in the PSW. */
Martin Schwidefskya45aff52011-10-30 15:16:07 +010088 if (!(new.control & PER_EVENT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 regs->psw.mask &= ~PSW_MASK_PER;
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010090 return;
Martin Schwidefskyc3311c12010-01-13 20:44:25 +010091 }
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +010092 regs->psw.mask |= PSW_MASK_PER;
93 __ctl_store(old, 9, 11);
Martin Schwidefskya45aff52011-10-30 15:16:07 +010094 if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
95 __ctl_load(new, 9, 11);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Roland McGrath0ac30be2008-01-26 14:11:22 +010098void user_enable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100100 set_tsk_thread_flag(task, TIF_SINGLE_STEP);
101 if (task == current)
102 update_per_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Roland McGrath0ac30be2008-01-26 14:11:22 +0100105void user_disable_single_step(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100107 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
108 if (task == current)
109 update_per_regs(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/*
113 * Called by kernel/ptrace.c when detaching..
114 *
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100115 * Clear all debugging related fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100117void ptrace_disable(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100119 memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
120 memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
121 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
122 clear_tsk_thread_flag(task, TIF_PER_TRAP);
Martin Schwidefskyd35339a2012-07-31 11:03:04 +0200123 task->thread.per_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800126#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127# define __ADDR_MASK 3
128#else
129# define __ADDR_MASK 7
130#endif
131
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100132static inline unsigned long __peek_user_per(struct task_struct *child,
133 addr_t addr)
134{
135 struct per_struct_kernel *dummy = NULL;
136
137 if (addr == (addr_t) &dummy->cr9)
138 /* Control bits of the active per set. */
139 return test_thread_flag(TIF_SINGLE_STEP) ?
140 PER_EVENT_IFETCH : child->thread.per_user.control;
141 else if (addr == (addr_t) &dummy->cr10)
142 /* Start address of the active per set. */
143 return test_thread_flag(TIF_SINGLE_STEP) ?
144 0 : child->thread.per_user.start;
145 else if (addr == (addr_t) &dummy->cr11)
146 /* End address of the active per set. */
147 return test_thread_flag(TIF_SINGLE_STEP) ?
148 PSW_ADDR_INSN : child->thread.per_user.end;
149 else if (addr == (addr_t) &dummy->bits)
150 /* Single-step bit. */
151 return test_thread_flag(TIF_SINGLE_STEP) ?
152 (1UL << (BITS_PER_LONG - 1)) : 0;
153 else if (addr == (addr_t) &dummy->starting_addr)
154 /* Start address of the user specified per set. */
155 return child->thread.per_user.start;
156 else if (addr == (addr_t) &dummy->ending_addr)
157 /* End address of the user specified per set. */
158 return child->thread.per_user.end;
159 else if (addr == (addr_t) &dummy->perc_atmid)
160 /* PER code, ATMID and AI of the last PER trap */
161 return (unsigned long)
162 child->thread.per_event.cause << (BITS_PER_LONG - 16);
163 else if (addr == (addr_t) &dummy->address)
164 /* Address of the last PER trap */
165 return child->thread.per_event.address;
166 else if (addr == (addr_t) &dummy->access_id)
167 /* Access id of the last PER trap */
168 return (unsigned long)
169 child->thread.per_event.paid << (BITS_PER_LONG - 8);
170 return 0;
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * Read the word at offset addr from the user area of a process. The
175 * trouble here is that the information is littered over different
176 * locations. The process registers are found on the kernel stack,
177 * the floating point stuff and the trace settings are stored in
178 * the task structure. In addition the different structures in
179 * struct user contain pad bytes that should be read as zeroes.
180 * Lovely...
181 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200182static unsigned long __peek_user(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct user *dummy = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200185 addr_t offset, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (addr < (addr_t) &dummy->regs.acrs) {
188 /*
189 * psw and gprs are stored on the stack
190 */
Al Viroc7584fb2006-01-12 01:05:49 -0800191 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (addr == (addr_t) &dummy->regs.psw.mask)
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100193 /* Return a clean psw mask. */
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100194 tmp = psw_user_bits | (tmp & PSW_MASK_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
197 /*
198 * access registers are stored in the thread structure
199 */
200 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800201#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700202 /*
203 * Very special case: old & broken 64 bit gdb reading
204 * from acrs[15]. Result is a 64 bit value. Read the
205 * 32 bit acrs[15] value and shift it by 32. Sick...
206 */
207 if (addr == (addr_t) &dummy->regs.acrs[15])
208 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
209 else
210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
212
213 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
214 /*
215 * orig_gpr2 is stored on the kernel stack
216 */
Al Viroc7584fb2006-01-12 01:05:49 -0800217 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200219 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
220 /*
221 * prevent reads of padding hole between
222 * orig_gpr2 and fp_regs on s390.
223 */
224 tmp = 0;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
227 /*
228 * floating point regs. are stored in the thread structure
229 */
230 offset = addr - (addr_t) &dummy->regs.fp_regs;
231 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700232 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
233 tmp &= (unsigned long) FPC_VALID_MASK
234 << (BITS_PER_LONG - 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
237 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100238 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100240 addr -= (addr_t) &dummy->regs.per_info;
241 tmp = __peek_user_per(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 } else
244 tmp = 0;
245
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200246 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static int
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200250peek_user(struct task_struct *child, addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200252 addr_t tmp, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /*
255 * Stupid gdb peeks/pokes the access registers in 64 bit with
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200256 * an alignment of 4. Programmers from hell...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700258 mask = __ADDR_MASK;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800259#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100260 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
261 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700262 mask = 3;
263#endif
264 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EIO;
266
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200267 tmp = __peek_user(child, addr);
268 return put_user(tmp, (addr_t __user *) data);
269}
270
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100271static inline void __poke_user_per(struct task_struct *child,
272 addr_t addr, addr_t data)
273{
274 struct per_struct_kernel *dummy = NULL;
275
276 /*
277 * There are only three fields in the per_info struct that the
278 * debugger user can write to.
279 * 1) cr9: the debugger wants to set a new PER event mask
280 * 2) starting_addr: the debugger wants to set a new starting
281 * address to use with the PER event mask.
282 * 3) ending_addr: the debugger wants to set a new ending
283 * address to use with the PER event mask.
284 * The user specified PER event mask and the start and end
285 * addresses are used only if single stepping is not in effect.
286 * Writes to any other field in per_info are ignored.
287 */
288 if (addr == (addr_t) &dummy->cr9)
289 /* PER event mask of the user specified per set. */
290 child->thread.per_user.control =
291 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
292 else if (addr == (addr_t) &dummy->starting_addr)
293 /* Starting address of the user specified per set. */
294 child->thread.per_user.start = data;
295 else if (addr == (addr_t) &dummy->ending_addr)
296 /* Ending address of the user specified per set. */
297 child->thread.per_user.end = data;
298}
299
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200300/*
301 * Write a word to the user area of a process at location addr. This
302 * operation does have an additional problem compared to peek_user.
303 * Stores to the program status word and on the floating point
304 * control register needs to get checked for validity.
305 */
306static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
307{
308 struct user *dummy = NULL;
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100309 addr_t offset;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (addr < (addr_t) &dummy->regs.acrs) {
312 /*
313 * psw and gprs are stored on the stack
314 */
315 if (addr == (addr_t) &dummy->regs.psw.mask &&
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100316 ((data & ~PSW_MASK_USER) != psw_user_bits ||
317 ((data & PSW_MASK_EA) && !(data & PSW_MASK_BA))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* Invalid psw mask. */
319 return -EINVAL;
Al Viroc7584fb2006-01-12 01:05:49 -0800320 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
323 /*
324 * access registers are stored in the thread structure
325 */
326 offset = addr - (addr_t) &dummy->regs.acrs;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800327#ifdef CONFIG_64BIT
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700328 /*
329 * Very special case: old & broken 64 bit gdb writing
330 * to acrs[15] with a 64 bit value. Ignore the lower
331 * half of the value and write the upper 32 bit to
332 * acrs[15]. Sick...
333 */
334 if (addr == (addr_t) &dummy->regs.acrs[15])
335 child->thread.acrs[15] = (unsigned int) (data >> 32);
336 else
337#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
339
340 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
341 /*
342 * orig_gpr2 is stored on the kernel stack
343 */
Al Viroc7584fb2006-01-12 01:05:49 -0800344 task_pt_regs(child)->orig_gpr2 = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200346 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
347 /*
348 * prevent writes of padding hole between
349 * orig_gpr2 and fp_regs on s390.
350 */
351 return 0;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
354 /*
355 * floating point regs. are stored in the thread structure
356 */
357 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
Martin Schwidefsky778959d2005-06-04 15:43:30 -0700358 (data & ~((unsigned long) FPC_VALID_MASK
359 << (BITS_PER_LONG - 32))) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -EINVAL;
361 offset = addr - (addr_t) &dummy->regs.fp_regs;
362 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
363
364 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
365 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100366 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100368 addr -= (addr_t) &dummy->regs.per_info;
369 __poke_user_per(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 }
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100376static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200377{
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200378 addr_t mask;
379
380 /*
381 * Stupid gdb peeks/pokes the access registers in 64 bit with
382 * an alignment of 4. Programmers from hell indeed...
383 */
384 mask = __ADDR_MASK;
385#ifdef CONFIG_64BIT
Martin Schwidefsky547e3ce2008-12-25 13:39:00 +0100386 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
387 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200388 mask = 3;
389#endif
390 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
391 return -EIO;
392
393 return __poke_user(child, addr, data);
394}
395
Namhyung Kim9b05a692010-10-27 15:33:47 -0700396long arch_ptrace(struct task_struct *child, long request,
397 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 ptrace_area parea;
400 int copied, ret;
401
402 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 case PTRACE_PEEKUSR:
404 /* read the word at location addr in the USER area. */
405 return peek_user(child, addr, data);
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 case PTRACE_POKEUSR:
408 /* write the word at location addr in the USER area */
409 return poke_user(child, addr, data);
410
411 case PTRACE_PEEKUSR_AREA:
412 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100413 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 sizeof(parea)))
415 return -EFAULT;
416 addr = parea.kernel_addr;
417 data = parea.process_addr;
418 copied = 0;
419 while (copied < parea.len) {
420 if (request == PTRACE_PEEKUSR_AREA)
421 ret = peek_user(child, addr, data);
422 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100423 addr_t utmp;
424 if (get_user(utmp,
425 (addr_t __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return -EFAULT;
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100427 ret = poke_user(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 if (ret)
430 return ret;
431 addr += sizeof(unsigned long);
432 data += sizeof(unsigned long);
433 copied += sizeof(unsigned long);
434 }
435 return 0;
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200436 case PTRACE_GET_LAST_BREAK:
437 put_user(task_thread_info(child)->last_break,
438 (unsigned long __user *) data);
439 return 0;
Martin Schwidefskyd35339a2012-07-31 11:03:04 +0200440 case PTRACE_ENABLE_TE:
441 if (!MACHINE_HAS_TE)
442 return -EIO;
443 child->thread.per_flags &= ~PER_FLAG_NO_TE;
444 return 0;
445 case PTRACE_DISABLE_TE:
446 if (!MACHINE_HAS_TE)
447 return -EIO;
448 child->thread.per_flags |= PER_FLAG_NO_TE;
449 return 0;
Christian Borntraeger07805ac2009-09-22 22:58:48 +0200450 default:
451 /* Removing high order bit from addr (only for 31 bit). */
452 addr &= PSW_ADDR_INSN;
453 return ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800457#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * Now the fun part starts... a 31 bit program running in the
460 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
461 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
462 * to handle, the difference to the 64 bit versions of the requests
463 * is that the access is done in multiples of 4 byte instead of
464 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
465 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
466 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
467 * is a 31 bit program too, the content of struct user can be
468 * emulated. A 31 bit program peeking into the struct user of
469 * a 64 bit program is a no-no.
470 */
471
472/*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100473 * Same as peek_user_per but for a 31 bit program.
474 */
475static inline __u32 __peek_user_per_compat(struct task_struct *child,
476 addr_t addr)
477{
478 struct compat_per_struct_kernel *dummy32 = NULL;
479
480 if (addr == (addr_t) &dummy32->cr9)
481 /* Control bits of the active per set. */
482 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
483 PER_EVENT_IFETCH : child->thread.per_user.control;
484 else if (addr == (addr_t) &dummy32->cr10)
485 /* Start address of the active per set. */
486 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
487 0 : child->thread.per_user.start;
488 else if (addr == (addr_t) &dummy32->cr11)
489 /* End address of the active per set. */
490 return test_thread_flag(TIF_SINGLE_STEP) ?
491 PSW32_ADDR_INSN : child->thread.per_user.end;
492 else if (addr == (addr_t) &dummy32->bits)
493 /* Single-step bit. */
494 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
495 0x80000000 : 0;
496 else if (addr == (addr_t) &dummy32->starting_addr)
497 /* Start address of the user specified per set. */
498 return (__u32) child->thread.per_user.start;
499 else if (addr == (addr_t) &dummy32->ending_addr)
500 /* End address of the user specified per set. */
501 return (__u32) child->thread.per_user.end;
502 else if (addr == (addr_t) &dummy32->perc_atmid)
503 /* PER code, ATMID and AI of the last PER trap */
504 return (__u32) child->thread.per_event.cause << 16;
505 else if (addr == (addr_t) &dummy32->address)
506 /* Address of the last PER trap */
507 return (__u32) child->thread.per_event.address;
508 else if (addr == (addr_t) &dummy32->access_id)
509 /* Access id of the last PER trap */
510 return (__u32) child->thread.per_event.paid << 24;
511 return 0;
512}
513
514/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * Same as peek_user but for a 31 bit program.
516 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200517static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100519 struct compat_user *dummy32 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 addr_t offset;
521 __u32 tmp;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (addr < (addr_t) &dummy32->regs.acrs) {
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100524 struct pt_regs *regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /*
526 * psw and gprs are stored on the stack
527 */
528 if (addr == (addr_t) &dummy32->regs.psw.mask) {
529 /* Fake a 31 bit psw mask. */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100530 tmp = (__u32)(regs->psw.mask >> 32);
531 tmp = psw32_user_bits | (tmp & PSW32_MASK_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
533 /* Fake a 31 bit psw address. */
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100534 tmp = (__u32) regs->psw.addr |
535 (__u32)(regs->psw.mask & PSW_MASK_BA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } else {
537 /* gpr 0-15 */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100538 tmp = *(__u32 *)((addr_t) &regs->psw + addr*2 + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
541 /*
542 * access registers are stored in the thread structure
543 */
544 offset = addr - (addr_t) &dummy32->regs.acrs;
545 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
546
547 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
548 /*
549 * orig_gpr2 is stored on the kernel stack
550 */
Al Viroc7584fb2006-01-12 01:05:49 -0800551 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200553 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
554 /*
555 * prevent reads of padding hole between
556 * orig_gpr2 and fp_regs on s390.
557 */
558 tmp = 0;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
561 /*
562 * floating point regs. are stored in the thread structure
563 */
564 offset = addr - (addr_t) &dummy32->regs.fp_regs;
565 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
566
567 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
568 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100569 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100571 addr -= (addr_t) &dummy32->regs.per_info;
572 tmp = __peek_user_per_compat(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 } else
575 tmp = 0;
576
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200577 return tmp;
578}
579
580static int peek_user_compat(struct task_struct *child,
581 addr_t addr, addr_t data)
582{
583 __u32 tmp;
584
Heiko Carstens77575912009-06-12 10:26:25 +0200585 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200586 return -EIO;
587
588 tmp = __peek_user_compat(child, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return put_user(tmp, (__u32 __user *) data);
590}
591
592/*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100593 * Same as poke_user_per but for a 31 bit program.
594 */
595static inline void __poke_user_per_compat(struct task_struct *child,
596 addr_t addr, __u32 data)
597{
598 struct compat_per_struct_kernel *dummy32 = NULL;
599
600 if (addr == (addr_t) &dummy32->cr9)
601 /* PER event mask of the user specified per set. */
602 child->thread.per_user.control =
603 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
604 else if (addr == (addr_t) &dummy32->starting_addr)
605 /* Starting address of the user specified per set. */
606 child->thread.per_user.start = data;
607 else if (addr == (addr_t) &dummy32->ending_addr)
608 /* Ending address of the user specified per set. */
609 child->thread.per_user.end = data;
610}
611
612/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * Same as poke_user but for a 31 bit program.
614 */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200615static int __poke_user_compat(struct task_struct *child,
616 addr_t addr, addr_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100618 struct compat_user *dummy32 = NULL;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200619 __u32 tmp = (__u32) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 addr_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (addr < (addr_t) &dummy32->regs.acrs) {
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100623 struct pt_regs *regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
625 * psw, gprs, acrs and orig_gpr2 are stored on the stack
626 */
627 if (addr == (addr_t) &dummy32->regs.psw.mask) {
628 /* Build a 64 bit psw mask from 31 bit mask. */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100629 if ((tmp & ~PSW32_MASK_USER) != psw32_user_bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Invalid psw mask. */
631 return -EINVAL;
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100632 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100633 (regs->psw.mask & PSW_MASK_BA) |
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100634 (__u64)(tmp & PSW32_MASK_USER) << 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
636 /* Build a 64 bit psw address from 31 bit address. */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100637 regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100638 /* Transfer 31 bit amode bit to psw mask. */
639 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
640 (__u64)(tmp & PSW32_ADDR_AMODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 } else {
642 /* gpr 0-15 */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100643 *(__u32*)((addr_t) &regs->psw + addr*2 + 4) = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
646 /*
647 * access registers are stored in the thread structure
648 */
649 offset = addr - (addr_t) &dummy32->regs.acrs;
650 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
651
652 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
653 /*
654 * orig_gpr2 is stored on the kernel stack
655 */
Al Viroc7584fb2006-01-12 01:05:49 -0800656 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jarod Wilson3d6e48f2008-09-09 12:38:56 +0200658 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
659 /*
660 * prevent writess of padding hole between
661 * orig_gpr2 and fp_regs on s390.
662 */
663 return 0;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
666 /*
667 * floating point regs. are stored in the thread structure
668 */
669 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
670 (tmp & ~FPC_VALID_MASK) != 0)
671 /* Invalid floating point control. */
672 return -EINVAL;
673 offset = addr - (addr_t) &dummy32->regs.fp_regs;
674 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
675
676 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
677 /*
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100678 * Handle access to the per_info structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100680 addr -= (addr_t) &dummy32->regs.per_info;
681 __poke_user_per_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 0;
685}
686
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200687static int poke_user_compat(struct task_struct *child,
688 addr_t addr, addr_t data)
689{
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100690 if (!is_compat_task() || (addr & 3) ||
691 addr > sizeof(struct compat_user) - 3)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200692 return -EIO;
693
694 return __poke_user_compat(child, addr, data);
695}
696
Roland McGrathb499d762008-05-07 09:22:57 +0200697long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
698 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Roland McGrathb499d762008-05-07 09:22:57 +0200700 unsigned long addr = caddr;
701 unsigned long data = cdata;
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100702 compat_ptrace_area parea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int copied, ret;
704
705 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 case PTRACE_PEEKUSR:
707 /* read the word at location addr in the USER area. */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200708 return peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 case PTRACE_POKEUSR:
711 /* write the word at location addr in the USER area */
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200712 return poke_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 case PTRACE_PEEKUSR_AREA:
715 case PTRACE_POKEUSR_AREA:
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100716 if (copy_from_user(&parea, (void __force __user *) addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 sizeof(parea)))
718 return -EFAULT;
719 addr = parea.kernel_addr;
720 data = parea.process_addr;
721 copied = 0;
722 while (copied < parea.len) {
723 if (request == PTRACE_PEEKUSR_AREA)
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200724 ret = peek_user_compat(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 else {
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100726 __u32 utmp;
727 if (get_user(utmp,
728 (__u32 __force __user *) data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -EFAULT;
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200730 ret = poke_user_compat(child, addr, utmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732 if (ret)
733 return ret;
734 addr += sizeof(unsigned int);
735 data += sizeof(unsigned int);
736 copied += sizeof(unsigned int);
737 }
738 return 0;
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200739 case PTRACE_GET_LAST_BREAK:
740 put_user(task_thread_info(child)->last_break,
741 (unsigned int __user *) data);
742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Roland McGrathb499d762008-05-07 09:22:57 +0200744 return compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746#endif
747
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200748asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Gerald Schaefer545c1742010-05-12 09:32:12 +0200750 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Heiko Carstensbcf5cef2009-06-12 10:26:26 +0200752 /* Do the secure computing check first. */
Heiko Carstensc63cb462012-07-31 15:37:13 +0200753 if (secure_computing(regs->gprs[2])) {
754 /* seccomp failures shouldn't expose any additional code. */
755 ret = -1;
756 goto out;
757 }
Heiko Carstensbcf5cef2009-06-12 10:26:26 +0200758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /*
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200760 * The sysc_tracesys code in entry.S stored the system
761 * call number to gprs[2].
Bodo Stroesserc5c3a6d2005-06-04 15:43:32 -0700762 */
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200763 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
764 (tracehook_report_syscall_entry(regs) ||
765 regs->gprs[2] >= NR_syscalls)) {
766 /*
767 * Tracing decided this syscall should not happen or the
768 * debugger stored an invalid system call number. Skip
769 * the system call and the system call restart handling.
770 */
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100771 clear_thread_flag(TIF_SYSCALL);
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200772 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200774
Josh Stone66700002009-08-24 14:43:11 -0700775 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -0700776 trace_sys_enter(regs, regs->gprs[2]);
Heiko Carstens9bf12262009-06-12 10:26:47 +0200777
Eric Parisb05d8442012-01-03 14:23:06 -0500778 audit_syscall_entry(is_compat_task() ?
779 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
780 regs->gprs[2], regs->orig_gpr2,
781 regs->gprs[3], regs->gprs[4],
782 regs->gprs[5]);
Heiko Carstensc63cb462012-07-31 15:37:13 +0200783out:
Gerald Schaefer545c1742010-05-12 09:32:12 +0200784 return ret ?: regs->gprs[2];
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200785}
786
787asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
788{
Eric Parisd7e75282012-01-03 14:23:06 -0500789 audit_syscall_exit(regs);
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200790
Josh Stone66700002009-08-24 14:43:11 -0700791 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
Josh Stone1c569f02009-08-24 14:43:14 -0700792 trace_sys_exit(regs, regs->gprs[2]);
Heiko Carstens9bf12262009-06-12 10:26:47 +0200793
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200794 if (test_thread_flag(TIF_SYSCALL_TRACE))
795 tracehook_report_syscall_exit(regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200797
798/*
799 * user_regset definitions.
800 */
801
802static int s390_regs_get(struct task_struct *target,
803 const struct user_regset *regset,
804 unsigned int pos, unsigned int count,
805 void *kbuf, void __user *ubuf)
806{
807 if (target == current)
808 save_access_regs(target->thread.acrs);
809
810 if (kbuf) {
811 unsigned long *k = kbuf;
812 while (count > 0) {
813 *k++ = __peek_user(target, pos);
814 count -= sizeof(*k);
815 pos += sizeof(*k);
816 }
817 } else {
818 unsigned long __user *u = ubuf;
819 while (count > 0) {
820 if (__put_user(__peek_user(target, pos), u++))
821 return -EFAULT;
822 count -= sizeof(*u);
823 pos += sizeof(*u);
824 }
825 }
826 return 0;
827}
828
829static int s390_regs_set(struct task_struct *target,
830 const struct user_regset *regset,
831 unsigned int pos, unsigned int count,
832 const void *kbuf, const void __user *ubuf)
833{
834 int rc = 0;
835
836 if (target == current)
837 save_access_regs(target->thread.acrs);
838
839 if (kbuf) {
840 const unsigned long *k = kbuf;
841 while (count > 0 && !rc) {
842 rc = __poke_user(target, pos, *k++);
843 count -= sizeof(*k);
844 pos += sizeof(*k);
845 }
846 } else {
847 const unsigned long __user *u = ubuf;
848 while (count > 0 && !rc) {
849 unsigned long word;
850 rc = __get_user(word, u++);
851 if (rc)
852 break;
853 rc = __poke_user(target, pos, word);
854 count -= sizeof(*u);
855 pos += sizeof(*u);
856 }
857 }
858
859 if (rc == 0 && target == current)
860 restore_access_regs(target->thread.acrs);
861
862 return rc;
863}
864
865static int s390_fpregs_get(struct task_struct *target,
866 const struct user_regset *regset, unsigned int pos,
867 unsigned int count, void *kbuf, void __user *ubuf)
868{
869 if (target == current)
870 save_fp_regs(&target->thread.fp_regs);
871
872 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
873 &target->thread.fp_regs, 0, -1);
874}
875
876static int s390_fpregs_set(struct task_struct *target,
877 const struct user_regset *regset, unsigned int pos,
878 unsigned int count, const void *kbuf,
879 const void __user *ubuf)
880{
881 int rc = 0;
882
883 if (target == current)
884 save_fp_regs(&target->thread.fp_regs);
885
886 /* If setting FPC, must validate it first. */
887 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
888 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
889 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
890 0, offsetof(s390_fp_regs, fprs));
891 if (rc)
892 return rc;
893 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
894 return -EINVAL;
895 target->thread.fp_regs.fpc = fpc[0];
896 }
897
898 if (rc == 0 && count > 0)
899 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
900 target->thread.fp_regs.fprs,
901 offsetof(s390_fp_regs, fprs), -1);
902
903 if (rc == 0 && target == current)
904 restore_fp_regs(&target->thread.fp_regs);
905
906 return rc;
907}
908
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200909#ifdef CONFIG_64BIT
910
911static int s390_last_break_get(struct task_struct *target,
912 const struct user_regset *regset,
913 unsigned int pos, unsigned int count,
914 void *kbuf, void __user *ubuf)
915{
916 if (count > 0) {
917 if (kbuf) {
918 unsigned long *k = kbuf;
919 *k = task_thread_info(target)->last_break;
920 } else {
921 unsigned long __user *u = ubuf;
922 if (__put_user(task_thread_info(target)->last_break, u))
923 return -EFAULT;
924 }
925 }
926 return 0;
927}
928
Martin Schwidefskyb9340692011-12-01 13:32:17 +0100929static int s390_last_break_set(struct task_struct *target,
930 const struct user_regset *regset,
931 unsigned int pos, unsigned int count,
932 const void *kbuf, const void __user *ubuf)
933{
934 return 0;
935}
936
Martin Schwidefskyd35339a2012-07-31 11:03:04 +0200937static int s390_tdb_get(struct task_struct *target,
938 const struct user_regset *regset,
939 unsigned int pos, unsigned int count,
940 void *kbuf, void __user *ubuf)
941{
942 struct pt_regs *regs = task_pt_regs(target);
943 unsigned char *data;
944
945 if (!(regs->int_code & 0x200))
946 return -ENODATA;
947 data = target->thread.trap_tdb;
948 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, data, 0, 256);
949}
950
951static int s390_tdb_set(struct task_struct *target,
952 const struct user_regset *regset,
953 unsigned int pos, unsigned int count,
954 const void *kbuf, const void __user *ubuf)
955{
956 return 0;
957}
958
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200959#endif
960
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100961static int s390_system_call_get(struct task_struct *target,
962 const struct user_regset *regset,
963 unsigned int pos, unsigned int count,
964 void *kbuf, void __user *ubuf)
965{
966 unsigned int *data = &task_thread_info(target)->system_call;
967 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
968 data, 0, sizeof(unsigned int));
969}
970
971static int s390_system_call_set(struct task_struct *target,
972 const struct user_regset *regset,
973 unsigned int pos, unsigned int count,
974 const void *kbuf, const void __user *ubuf)
975{
976 unsigned int *data = &task_thread_info(target)->system_call;
977 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
978 data, 0, sizeof(unsigned int));
979}
980
Martin Schwidefsky63506c42008-07-14 09:58:54 +0200981static const struct user_regset s390_regsets[] = {
982 [REGSET_GENERAL] = {
983 .core_note_type = NT_PRSTATUS,
984 .n = sizeof(s390_regs) / sizeof(long),
985 .size = sizeof(long),
986 .align = sizeof(long),
987 .get = s390_regs_get,
988 .set = s390_regs_set,
989 },
990 [REGSET_FP] = {
991 .core_note_type = NT_PRFPREG,
992 .n = sizeof(s390_fp_regs) / sizeof(long),
993 .size = sizeof(long),
994 .align = sizeof(long),
995 .get = s390_fpregs_get,
996 .set = s390_fpregs_set,
997 },
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200998#ifdef CONFIG_64BIT
999 [REGSET_LAST_BREAK] = {
1000 .core_note_type = NT_S390_LAST_BREAK,
1001 .n = 1,
1002 .size = sizeof(long),
1003 .align = sizeof(long),
1004 .get = s390_last_break_get,
Martin Schwidefskyb9340692011-12-01 13:32:17 +01001005 .set = s390_last_break_set,
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001006 },
Martin Schwidefskyd35339a2012-07-31 11:03:04 +02001007 [REGSET_TDB] = {
1008 .core_note_type = NT_S390_TDB,
1009 .n = 1,
1010 .size = 256,
1011 .align = 1,
1012 .get = s390_tdb_get,
1013 .set = s390_tdb_set,
1014 },
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001015#endif
Martin Schwidefsky20b40a72011-10-30 15:16:47 +01001016 [REGSET_SYSTEM_CALL] = {
1017 .core_note_type = NT_S390_SYSTEM_CALL,
1018 .n = 1,
1019 .size = sizeof(unsigned int),
1020 .align = sizeof(unsigned int),
1021 .get = s390_system_call_get,
1022 .set = s390_system_call_set,
1023 },
Martin Schwidefsky63506c42008-07-14 09:58:54 +02001024};
1025
1026static const struct user_regset_view user_s390_view = {
1027 .name = UTS_MACHINE,
1028 .e_machine = EM_S390,
1029 .regsets = s390_regsets,
1030 .n = ARRAY_SIZE(s390_regsets)
1031};
1032
1033#ifdef CONFIG_COMPAT
1034static int s390_compat_regs_get(struct task_struct *target,
1035 const struct user_regset *regset,
1036 unsigned int pos, unsigned int count,
1037 void *kbuf, void __user *ubuf)
1038{
1039 if (target == current)
1040 save_access_regs(target->thread.acrs);
1041
1042 if (kbuf) {
1043 compat_ulong_t *k = kbuf;
1044 while (count > 0) {
1045 *k++ = __peek_user_compat(target, pos);
1046 count -= sizeof(*k);
1047 pos += sizeof(*k);
1048 }
1049 } else {
1050 compat_ulong_t __user *u = ubuf;
1051 while (count > 0) {
1052 if (__put_user(__peek_user_compat(target, pos), u++))
1053 return -EFAULT;
1054 count -= sizeof(*u);
1055 pos += sizeof(*u);
1056 }
1057 }
1058 return 0;
1059}
1060
1061static int s390_compat_regs_set(struct task_struct *target,
1062 const struct user_regset *regset,
1063 unsigned int pos, unsigned int count,
1064 const void *kbuf, const void __user *ubuf)
1065{
1066 int rc = 0;
1067
1068 if (target == current)
1069 save_access_regs(target->thread.acrs);
1070
1071 if (kbuf) {
1072 const compat_ulong_t *k = kbuf;
1073 while (count > 0 && !rc) {
1074 rc = __poke_user_compat(target, pos, *k++);
1075 count -= sizeof(*k);
1076 pos += sizeof(*k);
1077 }
1078 } else {
1079 const compat_ulong_t __user *u = ubuf;
1080 while (count > 0 && !rc) {
1081 compat_ulong_t word;
1082 rc = __get_user(word, u++);
1083 if (rc)
1084 break;
1085 rc = __poke_user_compat(target, pos, word);
1086 count -= sizeof(*u);
1087 pos += sizeof(*u);
1088 }
1089 }
1090
1091 if (rc == 0 && target == current)
1092 restore_access_regs(target->thread.acrs);
1093
1094 return rc;
1095}
1096
Heiko Carstensea2a4d32009-10-06 10:34:13 +02001097static int s390_compat_regs_high_get(struct task_struct *target,
1098 const struct user_regset *regset,
1099 unsigned int pos, unsigned int count,
1100 void *kbuf, void __user *ubuf)
1101{
1102 compat_ulong_t *gprs_high;
1103
1104 gprs_high = (compat_ulong_t *)
1105 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1106 if (kbuf) {
1107 compat_ulong_t *k = kbuf;
1108 while (count > 0) {
1109 *k++ = *gprs_high;
1110 gprs_high += 2;
1111 count -= sizeof(*k);
1112 }
1113 } else {
1114 compat_ulong_t __user *u = ubuf;
1115 while (count > 0) {
1116 if (__put_user(*gprs_high, u++))
1117 return -EFAULT;
1118 gprs_high += 2;
1119 count -= sizeof(*u);
1120 }
1121 }
1122 return 0;
1123}
1124
1125static int s390_compat_regs_high_set(struct task_struct *target,
1126 const struct user_regset *regset,
1127 unsigned int pos, unsigned int count,
1128 const void *kbuf, const void __user *ubuf)
1129{
1130 compat_ulong_t *gprs_high;
1131 int rc = 0;
1132
1133 gprs_high = (compat_ulong_t *)
1134 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1135 if (kbuf) {
1136 const compat_ulong_t *k = kbuf;
1137 while (count > 0) {
1138 *gprs_high = *k++;
1139 *gprs_high += 2;
1140 count -= sizeof(*k);
1141 }
1142 } else {
1143 const compat_ulong_t __user *u = ubuf;
1144 while (count > 0 && !rc) {
1145 unsigned long word;
1146 rc = __get_user(word, u++);
1147 if (rc)
1148 break;
1149 *gprs_high = word;
1150 *gprs_high += 2;
1151 count -= sizeof(*u);
1152 }
1153 }
1154
1155 return rc;
1156}
1157
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001158static int s390_compat_last_break_get(struct task_struct *target,
1159 const struct user_regset *regset,
1160 unsigned int pos, unsigned int count,
1161 void *kbuf, void __user *ubuf)
1162{
1163 compat_ulong_t last_break;
1164
1165 if (count > 0) {
1166 last_break = task_thread_info(target)->last_break;
1167 if (kbuf) {
1168 unsigned long *k = kbuf;
1169 *k = last_break;
1170 } else {
1171 unsigned long __user *u = ubuf;
1172 if (__put_user(last_break, u))
1173 return -EFAULT;
1174 }
1175 }
1176 return 0;
1177}
1178
Martin Schwidefskyb9340692011-12-01 13:32:17 +01001179static int s390_compat_last_break_set(struct task_struct *target,
1180 const struct user_regset *regset,
1181 unsigned int pos, unsigned int count,
1182 const void *kbuf, const void __user *ubuf)
1183{
1184 return 0;
1185}
1186
Martin Schwidefsky63506c42008-07-14 09:58:54 +02001187static const struct user_regset s390_compat_regsets[] = {
1188 [REGSET_GENERAL] = {
1189 .core_note_type = NT_PRSTATUS,
1190 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
1191 .size = sizeof(compat_long_t),
1192 .align = sizeof(compat_long_t),
1193 .get = s390_compat_regs_get,
1194 .set = s390_compat_regs_set,
1195 },
1196 [REGSET_FP] = {
1197 .core_note_type = NT_PRFPREG,
1198 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
1199 .size = sizeof(compat_long_t),
1200 .align = sizeof(compat_long_t),
1201 .get = s390_fpregs_get,
1202 .set = s390_fpregs_set,
1203 },
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001204 [REGSET_LAST_BREAK] = {
1205 .core_note_type = NT_S390_LAST_BREAK,
1206 .n = 1,
1207 .size = sizeof(long),
1208 .align = sizeof(long),
1209 .get = s390_compat_last_break_get,
Martin Schwidefskyb9340692011-12-01 13:32:17 +01001210 .set = s390_compat_last_break_set,
Martin Schwidefsky86f25522010-05-17 10:00:05 +02001211 },
Martin Schwidefskyd35339a2012-07-31 11:03:04 +02001212 [REGSET_TDB] = {
1213 .core_note_type = NT_S390_TDB,
1214 .n = 1,
1215 .size = 256,
1216 .align = 1,
1217 .get = s390_tdb_get,
1218 .set = s390_tdb_set,
1219 },
Martin Schwidefsky20b40a72011-10-30 15:16:47 +01001220 [REGSET_SYSTEM_CALL] = {
1221 .core_note_type = NT_S390_SYSTEM_CALL,
1222 .n = 1,
1223 .size = sizeof(compat_uint_t),
1224 .align = sizeof(compat_uint_t),
1225 .get = s390_system_call_get,
1226 .set = s390_system_call_set,
1227 },
Heiko Carstensea2a4d32009-10-06 10:34:13 +02001228 [REGSET_GENERAL_EXTENDED] = {
Martin Schwidefsky622e99b2009-12-18 17:43:20 +01001229 .core_note_type = NT_S390_HIGH_GPRS,
Heiko Carstensea2a4d32009-10-06 10:34:13 +02001230 .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
1231 .size = sizeof(compat_long_t),
1232 .align = sizeof(compat_long_t),
1233 .get = s390_compat_regs_high_get,
1234 .set = s390_compat_regs_high_set,
1235 },
Martin Schwidefsky63506c42008-07-14 09:58:54 +02001236};
1237
1238static const struct user_regset_view user_s390_compat_view = {
1239 .name = "s390",
1240 .e_machine = EM_S390,
1241 .regsets = s390_compat_regsets,
1242 .n = ARRAY_SIZE(s390_compat_regsets)
1243};
1244#endif
1245
1246const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1247{
1248#ifdef CONFIG_COMPAT
1249 if (test_tsk_thread_flag(task, TIF_31BIT))
1250 return &user_s390_compat_view;
1251#endif
1252 return &user_s390_view;
1253}
Heiko Carstens952974ac62010-02-12 13:38:40 +01001254
1255static const char *gpr_names[NUM_GPRS] = {
1256 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1257 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1258};
1259
1260unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
1261{
1262 if (offset >= NUM_GPRS)
1263 return 0;
1264 return regs->gprs[offset];
1265}
1266
1267int regs_query_register_offset(const char *name)
1268{
1269 unsigned long offset;
1270
1271 if (!name || *name != 'r')
1272 return -EINVAL;
1273 if (strict_strtoul(name + 1, 10, &offset))
1274 return -EINVAL;
1275 if (offset >= NUM_GPRS)
1276 return -EINVAL;
1277 return offset;
1278}
1279
1280const char *regs_query_register_name(unsigned int offset)
1281{
1282 if (offset >= NUM_GPRS)
1283 return NULL;
1284 return gpr_names[offset];
1285}
1286
1287static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
1288{
1289 unsigned long ksp = kernel_stack_pointer(regs);
1290
1291 return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
1292}
1293
1294/**
1295 * regs_get_kernel_stack_nth() - get Nth entry of the stack
1296 * @regs:pt_regs which contains kernel stack pointer.
1297 * @n:stack entry number.
1298 *
1299 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
1300 * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
1301 * this returns 0.
1302 */
1303unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
1304{
1305 unsigned long addr;
1306
1307 addr = kernel_stack_pointer(regs) + n * sizeof(long);
1308 if (!regs_within_kernel_stack(regs, addr))
1309 return 0;
1310 return *(unsigned long *)addr;
1311}