]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/s390/kernel/ptrace.c
s390/ptrace: simplify enable/disable single step
[mirror_ubuntu-zesty-kernel.git] / arch / s390 / kernel / ptrace.c
CommitLineData
1da177e4 1/*
5e9a2692 2 * Ptrace user space interface.
1da177e4 3 *
a53c8fab 4 * Copyright IBM Corp. 1999, 2010
5e9a2692 5 * Author(s): Denis Joseph Barrow
1da177e4 6 * Martin Schwidefsky (schwidefsky@de.ibm.com)
1da177e4
LT
7 */
8
9#include <linux/kernel.h>
10#include <linux/sched.h>
11#include <linux/mm.h>
12#include <linux/smp.h>
1da177e4
LT
13#include <linux/errno.h>
14#include <linux/ptrace.h>
15#include <linux/user.h>
16#include <linux/security.h>
17#include <linux/audit.h>
7ed20e1a 18#include <linux/signal.h>
63506c41
MS
19#include <linux/elf.h>
20#include <linux/regset.h>
753c4dd6 21#include <linux/tracehook.h>
bcf5cef7 22#include <linux/seccomp.h>
048cd4e5 23#include <linux/compat.h>
9bf1226b 24#include <trace/syscall.h>
1da177e4
LT
25#include <asm/segment.h>
26#include <asm/page.h>
27#include <asm/pgtable.h>
28#include <asm/pgalloc.h>
1da177e4 29#include <asm/uaccess.h>
778959db 30#include <asm/unistd.h>
a0616cde 31#include <asm/switch_to.h>
a806170e 32#include "entry.h"
1da177e4 33
347a8dc3 34#ifdef CONFIG_COMPAT
1da177e4
LT
35#include "compat_ptrace.h"
36#endif
37
1c569f02
JS
38#define CREATE_TRACE_POINTS
39#include <trace/events/syscalls.h>
5e9ad7df 40
63506c41
MS
41enum s390_regset {
42 REGSET_GENERAL,
43 REGSET_FP,
86f2552b 44 REGSET_LAST_BREAK,
d35339a4 45 REGSET_TDB,
20b40a79 46 REGSET_SYSTEM_CALL,
ea2a4d3a 47 REGSET_GENERAL_EXTENDED,
63506c41
MS
48};
49
64597f9d 50void update_cr_regs(struct task_struct *task)
1da177e4 51{
5e9a2692
MS
52 struct pt_regs *regs = task_pt_regs(task);
53 struct thread_struct *thread = &task->thread;
a45aff52 54 struct per_regs old, new;
5e9a2692 55
66389e85 56#ifdef CONFIG_64BIT
d35339a4
MS
57 /* Take care of the enable/disable of transactional execution. */
58 if (MACHINE_HAS_TE) {
64597f9d 59 unsigned long cr[3], cr_new[3];
d35339a4 60
64597f9d
MM
61 __ctl_store(cr, 0, 2);
62 cr_new[1] = cr[1];
0628a5fb 63 /* Set or clear transaction execution TXC bit 8. */
d35339a4 64 if (task->thread.per_flags & PER_FLAG_NO_TE)
0628a5fb 65 cr_new[0] = cr[0] & ~(1UL << 55);
d35339a4 66 else
0628a5fb 67 cr_new[0] = cr[0] | (1UL << 55);
64597f9d
MM
68 /* Set or clear transaction execution TDC bits 62 and 63. */
69 cr_new[2] = cr[2] & ~3UL;
70 if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND) {
71 if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND_TEND)
72 cr_new[2] |= 1UL;
73 else
74 cr_new[2] |= 2UL;
75 }
76 if (memcmp(&cr_new, &cr, sizeof(cr)))
77 __ctl_load(cr_new, 0, 2);
d35339a4 78 }
66389e85 79#endif
a45aff52
MS
80 /* Copy user specified PER registers */
81 new.control = thread->per_user.control;
82 new.start = thread->per_user.start;
83 new.end = thread->per_user.end;
84
85 /* merge TIF_SINGLE_STEP into user specified PER registers. */
86 if (test_tsk_thread_flag(task, TIF_SINGLE_STEP)) {
87 new.control |= PER_EVENT_IFETCH;
d35339a4
MS
88#ifdef CONFIG_64BIT
89 new.control |= PER_CONTROL_SUSPENSION;
90 new.control |= PER_EVENT_TRANSACTION_END;
91#endif
a45aff52
MS
92 new.start = 0;
93 new.end = PSW_ADDR_INSN;
94 }
5e9a2692
MS
95
96 /* Take care of the PER enablement bit in the PSW. */
a45aff52 97 if (!(new.control & PER_EVENT_MASK)) {
1da177e4 98 regs->psw.mask &= ~PSW_MASK_PER;
5e9a2692 99 return;
c3311c13 100 }
5e9a2692
MS
101 regs->psw.mask |= PSW_MASK_PER;
102 __ctl_store(old, 9, 11);
a45aff52
MS
103 if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
104 __ctl_load(new, 9, 11);
1da177e4
LT
105}
106
0ac30be4 107void user_enable_single_step(struct task_struct *task)
1da177e4 108{
5e9a2692 109 set_tsk_thread_flag(task, TIF_SINGLE_STEP);
1da177e4
LT
110}
111
0ac30be4 112void user_disable_single_step(struct task_struct *task)
1da177e4 113{
5e9a2692 114 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
1da177e4
LT
115}
116
117/*
118 * Called by kernel/ptrace.c when detaching..
119 *
5e9a2692 120 * Clear all debugging related fields.
1da177e4 121 */
5e9a2692 122void ptrace_disable(struct task_struct *task)
1da177e4 123{
5e9a2692
MS
124 memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
125 memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
126 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
127 clear_tsk_thread_flag(task, TIF_PER_TRAP);
d35339a4 128 task->thread.per_flags = 0;
1da177e4
LT
129}
130
347a8dc3 131#ifndef CONFIG_64BIT
1da177e4
LT
132# define __ADDR_MASK 3
133#else
134# define __ADDR_MASK 7
135#endif
136
5e9a2692
MS
137static inline unsigned long __peek_user_per(struct task_struct *child,
138 addr_t addr)
139{
140 struct per_struct_kernel *dummy = NULL;
141
142 if (addr == (addr_t) &dummy->cr9)
143 /* Control bits of the active per set. */
144 return test_thread_flag(TIF_SINGLE_STEP) ?
145 PER_EVENT_IFETCH : child->thread.per_user.control;
146 else if (addr == (addr_t) &dummy->cr10)
147 /* Start address of the active per set. */
148 return test_thread_flag(TIF_SINGLE_STEP) ?
149 0 : child->thread.per_user.start;
150 else if (addr == (addr_t) &dummy->cr11)
151 /* End address of the active per set. */
152 return test_thread_flag(TIF_SINGLE_STEP) ?
153 PSW_ADDR_INSN : child->thread.per_user.end;
154 else if (addr == (addr_t) &dummy->bits)
155 /* Single-step bit. */
156 return test_thread_flag(TIF_SINGLE_STEP) ?
157 (1UL << (BITS_PER_LONG - 1)) : 0;
158 else if (addr == (addr_t) &dummy->starting_addr)
159 /* Start address of the user specified per set. */
160 return child->thread.per_user.start;
161 else if (addr == (addr_t) &dummy->ending_addr)
162 /* End address of the user specified per set. */
163 return child->thread.per_user.end;
164 else if (addr == (addr_t) &dummy->perc_atmid)
165 /* PER code, ATMID and AI of the last PER trap */
166 return (unsigned long)
167 child->thread.per_event.cause << (BITS_PER_LONG - 16);
168 else if (addr == (addr_t) &dummy->address)
169 /* Address of the last PER trap */
170 return child->thread.per_event.address;
171 else if (addr == (addr_t) &dummy->access_id)
172 /* Access id of the last PER trap */
173 return (unsigned long)
174 child->thread.per_event.paid << (BITS_PER_LONG - 8);
175 return 0;
176}
177
1da177e4
LT
178/*
179 * Read the word at offset addr from the user area of a process. The
180 * trouble here is that the information is littered over different
181 * locations. The process registers are found on the kernel stack,
182 * the floating point stuff and the trace settings are stored in
183 * the task structure. In addition the different structures in
184 * struct user contain pad bytes that should be read as zeroes.
185 * Lovely...
186 */
63506c41 187static unsigned long __peek_user(struct task_struct *child, addr_t addr)
1da177e4
LT
188{
189 struct user *dummy = NULL;
63506c41 190 addr_t offset, tmp;
1da177e4
LT
191
192 if (addr < (addr_t) &dummy->regs.acrs) {
193 /*
194 * psw and gprs are stored on the stack
195 */
c7584fb6 196 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
5ebf250d 197 if (addr == (addr_t) &dummy->regs.psw.mask) {
b50511e4 198 /* Return a clean psw mask. */
5ebf250d
HC
199 tmp &= PSW_MASK_USER | PSW_MASK_RI;
200 tmp |= PSW_USER_BITS;
201 }
1da177e4
LT
202
203 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
204 /*
205 * access registers are stored in the thread structure
206 */
207 offset = addr - (addr_t) &dummy->regs.acrs;
347a8dc3 208#ifdef CONFIG_64BIT
778959db
MS
209 /*
210 * Very special case: old & broken 64 bit gdb reading
211 * from acrs[15]. Result is a 64 bit value. Read the
212 * 32 bit acrs[15] value and shift it by 32. Sick...
213 */
214 if (addr == (addr_t) &dummy->regs.acrs[15])
215 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
216 else
217#endif
1da177e4
LT
218 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
219
220 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
221 /*
222 * orig_gpr2 is stored on the kernel stack
223 */
c7584fb6 224 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
1da177e4 225
3d6e48f4
JW
226 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
227 /*
228 * prevent reads of padding hole between
229 * orig_gpr2 and fp_regs on s390.
230 */
231 tmp = 0;
232
1da177e4
LT
233 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
234 /*
235 * floating point regs. are stored in the thread structure
236 */
237 offset = addr - (addr_t) &dummy->regs.fp_regs;
238 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
778959db 239 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
4725c860 240 tmp <<= BITS_PER_LONG - 32;
1da177e4
LT
241
242 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
243 /*
5e9a2692 244 * Handle access to the per_info structure.
1da177e4 245 */
5e9a2692
MS
246 addr -= (addr_t) &dummy->regs.per_info;
247 tmp = __peek_user_per(child, addr);
1da177e4
LT
248
249 } else
250 tmp = 0;
251
63506c41 252 return tmp;
1da177e4
LT
253}
254
1da177e4 255static int
63506c41 256peek_user(struct task_struct *child, addr_t addr, addr_t data)
1da177e4 257{
63506c41 258 addr_t tmp, mask;
1da177e4
LT
259
260 /*
261 * Stupid gdb peeks/pokes the access registers in 64 bit with
63506c41 262 * an alignment of 4. Programmers from hell...
1da177e4 263 */
778959db 264 mask = __ADDR_MASK;
347a8dc3 265#ifdef CONFIG_64BIT
547e3cec
MS
266 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
267 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
778959db
MS
268 mask = 3;
269#endif
270 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
1da177e4
LT
271 return -EIO;
272
63506c41
MS
273 tmp = __peek_user(child, addr);
274 return put_user(tmp, (addr_t __user *) data);
275}
276
5e9a2692
MS
277static inline void __poke_user_per(struct task_struct *child,
278 addr_t addr, addr_t data)
279{
280 struct per_struct_kernel *dummy = NULL;
281
282 /*
283 * There are only three fields in the per_info struct that the
284 * debugger user can write to.
285 * 1) cr9: the debugger wants to set a new PER event mask
286 * 2) starting_addr: the debugger wants to set a new starting
287 * address to use with the PER event mask.
288 * 3) ending_addr: the debugger wants to set a new ending
289 * address to use with the PER event mask.
290 * The user specified PER event mask and the start and end
291 * addresses are used only if single stepping is not in effect.
292 * Writes to any other field in per_info are ignored.
293 */
294 if (addr == (addr_t) &dummy->cr9)
295 /* PER event mask of the user specified per set. */
296 child->thread.per_user.control =
297 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
298 else if (addr == (addr_t) &dummy->starting_addr)
299 /* Starting address of the user specified per set. */
300 child->thread.per_user.start = data;
301 else if (addr == (addr_t) &dummy->ending_addr)
302 /* Ending address of the user specified per set. */
303 child->thread.per_user.end = data;
304}
305
63506c41
MS
306/*
307 * Write a word to the user area of a process at location addr. This
308 * operation does have an additional problem compared to peek_user.
309 * Stores to the program status word and on the floating point
310 * control register needs to get checked for validity.
311 */
312static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
313{
314 struct user *dummy = NULL;
d4e81b35 315 addr_t offset;
63506c41 316
1da177e4
LT
317 if (addr < (addr_t) &dummy->regs.acrs) {
318 /*
319 * psw and gprs are stored on the stack
320 */
5ebf250d
HC
321 if (addr == (addr_t) &dummy->regs.psw.mask) {
322 unsigned long mask = PSW_MASK_USER;
323
324 mask |= is_ri_task(child) ? PSW_MASK_RI : 0;
325 if ((data & ~mask) != PSW_USER_BITS)
326 return -EINVAL;
327 if ((data & PSW_MASK_EA) && !(data & PSW_MASK_BA))
328 return -EINVAL;
329 }
c7584fb6 330 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
1da177e4
LT
331
332 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
333 /*
334 * access registers are stored in the thread structure
335 */
336 offset = addr - (addr_t) &dummy->regs.acrs;
347a8dc3 337#ifdef CONFIG_64BIT
778959db
MS
338 /*
339 * Very special case: old & broken 64 bit gdb writing
340 * to acrs[15] with a 64 bit value. Ignore the lower
341 * half of the value and write the upper 32 bit to
342 * acrs[15]. Sick...
343 */
344 if (addr == (addr_t) &dummy->regs.acrs[15])
345 child->thread.acrs[15] = (unsigned int) (data >> 32);
346 else
347#endif
1da177e4
LT
348 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
349
350 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
351 /*
352 * orig_gpr2 is stored on the kernel stack
353 */
c7584fb6 354 task_pt_regs(child)->orig_gpr2 = data;
1da177e4 355
3d6e48f4
JW
356 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
357 /*
358 * prevent writes of padding hole between
359 * orig_gpr2 and fp_regs on s390.
360 */
361 return 0;
362
1da177e4
LT
363 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
364 /*
365 * floating point regs. are stored in the thread structure
366 */
4725c860
MS
367 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
368 if ((unsigned int) data != 0 ||
369 test_fp_ctl(data >> (BITS_PER_LONG - 32)))
370 return -EINVAL;
1da177e4
LT
371 offset = addr - (addr_t) &dummy->regs.fp_regs;
372 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
373
374 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
375 /*
5e9a2692 376 * Handle access to the per_info structure.
1da177e4 377 */
5e9a2692
MS
378 addr -= (addr_t) &dummy->regs.per_info;
379 __poke_user_per(child, addr, data);
1da177e4
LT
380
381 }
382
1da177e4
LT
383 return 0;
384}
385
5e9a2692 386static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
63506c41 387{
63506c41
MS
388 addr_t mask;
389
390 /*
391 * Stupid gdb peeks/pokes the access registers in 64 bit with
392 * an alignment of 4. Programmers from hell indeed...
393 */
394 mask = __ADDR_MASK;
395#ifdef CONFIG_64BIT
547e3cec
MS
396 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
397 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
63506c41
MS
398 mask = 3;
399#endif
400 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
401 return -EIO;
402
403 return __poke_user(child, addr, data);
404}
405
9b05a69e
NK
406long arch_ptrace(struct task_struct *child, long request,
407 unsigned long addr, unsigned long data)
1da177e4 408{
1da177e4
LT
409 ptrace_area parea;
410 int copied, ret;
411
412 switch (request) {
1da177e4
LT
413 case PTRACE_PEEKUSR:
414 /* read the word at location addr in the USER area. */
415 return peek_user(child, addr, data);
416
1da177e4
LT
417 case PTRACE_POKEUSR:
418 /* write the word at location addr in the USER area */
419 return poke_user(child, addr, data);
420
421 case PTRACE_PEEKUSR_AREA:
422 case PTRACE_POKEUSR_AREA:
2b67fc46 423 if (copy_from_user(&parea, (void __force __user *) addr,
1da177e4
LT
424 sizeof(parea)))
425 return -EFAULT;
426 addr = parea.kernel_addr;
427 data = parea.process_addr;
428 copied = 0;
429 while (copied < parea.len) {
430 if (request == PTRACE_PEEKUSR_AREA)
431 ret = peek_user(child, addr, data);
432 else {
2b67fc46
HC
433 addr_t utmp;
434 if (get_user(utmp,
435 (addr_t __force __user *) data))
1da177e4 436 return -EFAULT;
2b67fc46 437 ret = poke_user(child, addr, utmp);
1da177e4
LT
438 }
439 if (ret)
440 return ret;
441 addr += sizeof(unsigned long);
442 data += sizeof(unsigned long);
443 copied += sizeof(unsigned long);
444 }
445 return 0;
86f2552b
MS
446 case PTRACE_GET_LAST_BREAK:
447 put_user(task_thread_info(child)->last_break,
448 (unsigned long __user *) data);
449 return 0;
d35339a4
MS
450 case PTRACE_ENABLE_TE:
451 if (!MACHINE_HAS_TE)
452 return -EIO;
453 child->thread.per_flags &= ~PER_FLAG_NO_TE;
454 return 0;
455 case PTRACE_DISABLE_TE:
456 if (!MACHINE_HAS_TE)
457 return -EIO;
458 child->thread.per_flags |= PER_FLAG_NO_TE;
64597f9d
MM
459 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
460 return 0;
461 case PTRACE_TE_ABORT_RAND:
462 if (!MACHINE_HAS_TE || (child->thread.per_flags & PER_FLAG_NO_TE))
463 return -EIO;
464 switch (data) {
465 case 0UL:
466 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
467 break;
468 case 1UL:
469 child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
470 child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND_TEND;
471 break;
472 case 2UL:
473 child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
474 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND_TEND;
475 break;
476 default:
477 return -EINVAL;
478 }
d35339a4 479 return 0;
07805ac8
CB
480 default:
481 /* Removing high order bit from addr (only for 31 bit). */
482 addr &= PSW_ADDR_INSN;
483 return ptrace_request(child, request, addr, data);
1da177e4 484 }
1da177e4
LT
485}
486
347a8dc3 487#ifdef CONFIG_COMPAT
1da177e4
LT
488/*
489 * Now the fun part starts... a 31 bit program running in the
490 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
491 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
492 * to handle, the difference to the 64 bit versions of the requests
493 * is that the access is done in multiples of 4 byte instead of
494 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
495 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
496 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
497 * is a 31 bit program too, the content of struct user can be
498 * emulated. A 31 bit program peeking into the struct user of
499 * a 64 bit program is a no-no.
500 */
501
5e9a2692
MS
502/*
503 * Same as peek_user_per but for a 31 bit program.
504 */
505static inline __u32 __peek_user_per_compat(struct task_struct *child,
506 addr_t addr)
507{
508 struct compat_per_struct_kernel *dummy32 = NULL;
509
510 if (addr == (addr_t) &dummy32->cr9)
511 /* Control bits of the active per set. */
512 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
513 PER_EVENT_IFETCH : child->thread.per_user.control;
514 else if (addr == (addr_t) &dummy32->cr10)
515 /* Start address of the active per set. */
516 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
517 0 : child->thread.per_user.start;
518 else if (addr == (addr_t) &dummy32->cr11)
519 /* End address of the active per set. */
520 return test_thread_flag(TIF_SINGLE_STEP) ?
521 PSW32_ADDR_INSN : child->thread.per_user.end;
522 else if (addr == (addr_t) &dummy32->bits)
523 /* Single-step bit. */
524 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
525 0x80000000 : 0;
526 else if (addr == (addr_t) &dummy32->starting_addr)
527 /* Start address of the user specified per set. */
528 return (__u32) child->thread.per_user.start;
529 else if (addr == (addr_t) &dummy32->ending_addr)
530 /* End address of the user specified per set. */
531 return (__u32) child->thread.per_user.end;
532 else if (addr == (addr_t) &dummy32->perc_atmid)
533 /* PER code, ATMID and AI of the last PER trap */
534 return (__u32) child->thread.per_event.cause << 16;
535 else if (addr == (addr_t) &dummy32->address)
536 /* Address of the last PER trap */
537 return (__u32) child->thread.per_event.address;
538 else if (addr == (addr_t) &dummy32->access_id)
539 /* Access id of the last PER trap */
540 return (__u32) child->thread.per_event.paid << 24;
541 return 0;
542}
543
1da177e4
LT
544/*
545 * Same as peek_user but for a 31 bit program.
546 */
63506c41 547static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
1da177e4 548{
5e9a2692 549 struct compat_user *dummy32 = NULL;
1da177e4
LT
550 addr_t offset;
551 __u32 tmp;
552
1da177e4 553 if (addr < (addr_t) &dummy32->regs.acrs) {
b50511e4 554 struct pt_regs *regs = task_pt_regs(child);
1da177e4
LT
555 /*
556 * psw and gprs are stored on the stack
557 */
558 if (addr == (addr_t) &dummy32->regs.psw.mask) {
559 /* Fake a 31 bit psw mask. */
b50511e4 560 tmp = (__u32)(regs->psw.mask >> 32);
5ebf250d 561 tmp &= PSW32_MASK_USER | PSW32_MASK_RI;
f26946d7 562 tmp |= PSW32_USER_BITS;
1da177e4
LT
563 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
564 /* Fake a 31 bit psw address. */
d4e81b35
MS
565 tmp = (__u32) regs->psw.addr |
566 (__u32)(regs->psw.mask & PSW_MASK_BA);
1da177e4
LT
567 } else {
568 /* gpr 0-15 */
b50511e4 569 tmp = *(__u32 *)((addr_t) &regs->psw + addr*2 + 4);
1da177e4
LT
570 }
571 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
572 /*
573 * access registers are stored in the thread structure
574 */
575 offset = addr - (addr_t) &dummy32->regs.acrs;
576 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
577
578 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
579 /*
580 * orig_gpr2 is stored on the kernel stack
581 */
c7584fb6 582 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
1da177e4 583
3d6e48f4
JW
584 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
585 /*
586 * prevent reads of padding hole between
587 * orig_gpr2 and fp_regs on s390.
588 */
589 tmp = 0;
590
1da177e4
LT
591 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
592 /*
593 * floating point regs. are stored in the thread structure
594 */
595 offset = addr - (addr_t) &dummy32->regs.fp_regs;
596 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
597
598 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
599 /*
5e9a2692 600 * Handle access to the per_info structure.
1da177e4 601 */
5e9a2692
MS
602 addr -= (addr_t) &dummy32->regs.per_info;
603 tmp = __peek_user_per_compat(child, addr);
1da177e4
LT
604
605 } else
606 tmp = 0;
607
63506c41
MS
608 return tmp;
609}
610
611static int peek_user_compat(struct task_struct *child,
612 addr_t addr, addr_t data)
613{
614 __u32 tmp;
615
7757591a 616 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
63506c41
MS
617 return -EIO;
618
619 tmp = __peek_user_compat(child, addr);
1da177e4
LT
620 return put_user(tmp, (__u32 __user *) data);
621}
622
5e9a2692
MS
623/*
624 * Same as poke_user_per but for a 31 bit program.
625 */
626static inline void __poke_user_per_compat(struct task_struct *child,
627 addr_t addr, __u32 data)
628{
629 struct compat_per_struct_kernel *dummy32 = NULL;
630
631 if (addr == (addr_t) &dummy32->cr9)
632 /* PER event mask of the user specified per set. */
633 child->thread.per_user.control =
634 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
635 else if (addr == (addr_t) &dummy32->starting_addr)
636 /* Starting address of the user specified per set. */
637 child->thread.per_user.start = data;
638 else if (addr == (addr_t) &dummy32->ending_addr)
639 /* Ending address of the user specified per set. */
640 child->thread.per_user.end = data;
641}
642
1da177e4
LT
643/*
644 * Same as poke_user but for a 31 bit program.
645 */
63506c41
MS
646static int __poke_user_compat(struct task_struct *child,
647 addr_t addr, addr_t data)
1da177e4 648{
5e9a2692 649 struct compat_user *dummy32 = NULL;
63506c41 650 __u32 tmp = (__u32) data;
1da177e4 651 addr_t offset;
1da177e4
LT
652
653 if (addr < (addr_t) &dummy32->regs.acrs) {
b50511e4 654 struct pt_regs *regs = task_pt_regs(child);
1da177e4
LT
655 /*
656 * psw, gprs, acrs and orig_gpr2 are stored on the stack
657 */
658 if (addr == (addr_t) &dummy32->regs.psw.mask) {
5ebf250d
HC
659 __u32 mask = PSW32_MASK_USER;
660
661 mask |= is_ri_task(child) ? PSW32_MASK_RI : 0;
1da177e4 662 /* Build a 64 bit psw mask from 31 bit mask. */
f26946d7 663 if ((tmp & ~mask) != PSW32_USER_BITS)
1da177e4
LT
664 /* Invalid psw mask. */
665 return -EINVAL;
b50511e4 666 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
d4e81b35 667 (regs->psw.mask & PSW_MASK_BA) |
5ebf250d 668 (__u64)(tmp & mask) << 32;
1da177e4
LT
669 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
670 /* Build a 64 bit psw address from 31 bit address. */
b50511e4 671 regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
d4e81b35
MS
672 /* Transfer 31 bit amode bit to psw mask. */
673 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
674 (__u64)(tmp & PSW32_ADDR_AMODE);
1da177e4
LT
675 } else {
676 /* gpr 0-15 */
b50511e4 677 *(__u32*)((addr_t) &regs->psw + addr*2 + 4) = tmp;
1da177e4
LT
678 }
679 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
680 /*
681 * access registers are stored in the thread structure
682 */
683 offset = addr - (addr_t) &dummy32->regs.acrs;
684 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
685
686 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
687 /*
688 * orig_gpr2 is stored on the kernel stack
689 */
c7584fb6 690 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
1da177e4 691
3d6e48f4
JW
692 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
693 /*
694 * prevent writess of padding hole between
695 * orig_gpr2 and fp_regs on s390.
696 */
697 return 0;
698
1da177e4
LT
699 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
700 /*
701 * floating point regs. are stored in the thread structure
702 */
703 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
4725c860 704 test_fp_ctl(tmp))
1da177e4
LT
705 return -EINVAL;
706 offset = addr - (addr_t) &dummy32->regs.fp_regs;
707 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
708
709 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
710 /*
5e9a2692 711 * Handle access to the per_info structure.
1da177e4 712 */
5e9a2692
MS
713 addr -= (addr_t) &dummy32->regs.per_info;
714 __poke_user_per_compat(child, addr, data);
1da177e4
LT
715 }
716
1da177e4
LT
717 return 0;
718}
719
63506c41
MS
720static int poke_user_compat(struct task_struct *child,
721 addr_t addr, addr_t data)
722{
5e9a2692
MS
723 if (!is_compat_task() || (addr & 3) ||
724 addr > sizeof(struct compat_user) - 3)
63506c41
MS
725 return -EIO;
726
727 return __poke_user_compat(child, addr, data);
728}
729
b499d76b
RM
730long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
731 compat_ulong_t caddr, compat_ulong_t cdata)
1da177e4 732{
b499d76b
RM
733 unsigned long addr = caddr;
734 unsigned long data = cdata;
5e9a2692 735 compat_ptrace_area parea;
1da177e4
LT
736 int copied, ret;
737
738 switch (request) {
1da177e4
LT
739 case PTRACE_PEEKUSR:
740 /* read the word at location addr in the USER area. */
63506c41 741 return peek_user_compat(child, addr, data);
1da177e4 742
1da177e4
LT
743 case PTRACE_POKEUSR:
744 /* write the word at location addr in the USER area */
63506c41 745 return poke_user_compat(child, addr, data);
1da177e4
LT
746
747 case PTRACE_PEEKUSR_AREA:
748 case PTRACE_POKEUSR_AREA:
2b67fc46 749 if (copy_from_user(&parea, (void __force __user *) addr,
1da177e4
LT
750 sizeof(parea)))
751 return -EFAULT;
752 addr = parea.kernel_addr;
753 data = parea.process_addr;
754 copied = 0;
755 while (copied < parea.len) {
756 if (request == PTRACE_PEEKUSR_AREA)
63506c41 757 ret = peek_user_compat(child, addr, data);
1da177e4 758 else {
2b67fc46
HC
759 __u32 utmp;
760 if (get_user(utmp,
761 (__u32 __force __user *) data))
1da177e4 762 return -EFAULT;
63506c41 763 ret = poke_user_compat(child, addr, utmp);
1da177e4
LT
764 }
765 if (ret)
766 return ret;
767 addr += sizeof(unsigned int);
768 data += sizeof(unsigned int);
769 copied += sizeof(unsigned int);
770 }
771 return 0;
86f2552b
MS
772 case PTRACE_GET_LAST_BREAK:
773 put_user(task_thread_info(child)->last_break,
774 (unsigned int __user *) data);
775 return 0;
1da177e4 776 }
b499d76b 777 return compat_ptrace_request(child, request, addr, data);
1da177e4
LT
778}
779#endif
780
753c4dd6 781asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
1da177e4 782{
545c174d 783 long ret = 0;
1da177e4 784
bcf5cef7 785 /* Do the secure computing check first. */
c63cb468
HC
786 if (secure_computing(regs->gprs[2])) {
787 /* seccomp failures shouldn't expose any additional code. */
788 ret = -1;
789 goto out;
790 }
bcf5cef7 791
c5c3a6d8 792 /*
753c4dd6
MS
793 * The sysc_tracesys code in entry.S stored the system
794 * call number to gprs[2].
c5c3a6d8 795 */
753c4dd6
MS
796 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
797 (tracehook_report_syscall_entry(regs) ||
798 regs->gprs[2] >= NR_syscalls)) {
799 /*
800 * Tracing decided this syscall should not happen or the
801 * debugger stored an invalid system call number. Skip
802 * the system call and the system call restart handling.
803 */
b6ef5bb3 804 clear_thread_flag(TIF_SYSCALL);
753c4dd6 805 ret = -1;
1da177e4 806 }
753c4dd6 807
66700001 808 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1c569f02 809 trace_sys_enter(regs, regs->gprs[2]);
9bf1226b 810
b05d8447
EP
811 audit_syscall_entry(is_compat_task() ?
812 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
813 regs->gprs[2], regs->orig_gpr2,
814 regs->gprs[3], regs->gprs[4],
815 regs->gprs[5]);
c63cb468 816out:
545c174d 817 return ret ?: regs->gprs[2];
753c4dd6
MS
818}
819
820asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
821{
d7e7528b 822 audit_syscall_exit(regs);
753c4dd6 823
66700001 824 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1c569f02 825 trace_sys_exit(regs, regs->gprs[2]);
9bf1226b 826
753c4dd6
MS
827 if (test_thread_flag(TIF_SYSCALL_TRACE))
828 tracehook_report_syscall_exit(regs, 0);
1da177e4 829}
63506c41
MS
830
831/*
832 * user_regset definitions.
833 */
834
835static int s390_regs_get(struct task_struct *target,
836 const struct user_regset *regset,
837 unsigned int pos, unsigned int count,
838 void *kbuf, void __user *ubuf)
839{
840 if (target == current)
841 save_access_regs(target->thread.acrs);
842
843 if (kbuf) {
844 unsigned long *k = kbuf;
845 while (count > 0) {
846 *k++ = __peek_user(target, pos);
847 count -= sizeof(*k);
848 pos += sizeof(*k);
849 }
850 } else {
851 unsigned long __user *u = ubuf;
852 while (count > 0) {
853 if (__put_user(__peek_user(target, pos), u++))
854 return -EFAULT;
855 count -= sizeof(*u);
856 pos += sizeof(*u);
857 }
858 }
859 return 0;
860}
861
862static int s390_regs_set(struct task_struct *target,
863 const struct user_regset *regset,
864 unsigned int pos, unsigned int count,
865 const void *kbuf, const void __user *ubuf)
866{
867 int rc = 0;
868
869 if (target == current)
870 save_access_regs(target->thread.acrs);
871
872 if (kbuf) {
873 const unsigned long *k = kbuf;
874 while (count > 0 && !rc) {
875 rc = __poke_user(target, pos, *k++);
876 count -= sizeof(*k);
877 pos += sizeof(*k);
878 }
879 } else {
880 const unsigned long __user *u = ubuf;
881 while (count > 0 && !rc) {
882 unsigned long word;
883 rc = __get_user(word, u++);
884 if (rc)
885 break;
886 rc = __poke_user(target, pos, word);
887 count -= sizeof(*u);
888 pos += sizeof(*u);
889 }
890 }
891
892 if (rc == 0 && target == current)
893 restore_access_regs(target->thread.acrs);
894
895 return rc;
896}
897
898static int s390_fpregs_get(struct task_struct *target,
899 const struct user_regset *regset, unsigned int pos,
900 unsigned int count, void *kbuf, void __user *ubuf)
901{
4725c860
MS
902 if (target == current) {
903 save_fp_ctl(&target->thread.fp_regs.fpc);
904 save_fp_regs(target->thread.fp_regs.fprs);
905 }
63506c41
MS
906
907 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
908 &target->thread.fp_regs, 0, -1);
909}
910
911static int s390_fpregs_set(struct task_struct *target,
912 const struct user_regset *regset, unsigned int pos,
913 unsigned int count, const void *kbuf,
914 const void __user *ubuf)
915{
916 int rc = 0;
917
4725c860
MS
918 if (target == current) {
919 save_fp_ctl(&target->thread.fp_regs.fpc);
920 save_fp_regs(target->thread.fp_regs.fprs);
921 }
63506c41
MS
922
923 /* If setting FPC, must validate it first. */
924 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
4725c860
MS
925 u32 ufpc[2] = { target->thread.fp_regs.fpc, 0 };
926 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ufpc,
63506c41
MS
927 0, offsetof(s390_fp_regs, fprs));
928 if (rc)
929 return rc;
4725c860 930 if (ufpc[1] != 0 || test_fp_ctl(ufpc[0]))
63506c41 931 return -EINVAL;
4725c860 932 target->thread.fp_regs.fpc = ufpc[0];
63506c41
MS
933 }
934
935 if (rc == 0 && count > 0)
936 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
937 target->thread.fp_regs.fprs,
938 offsetof(s390_fp_regs, fprs), -1);
939
4725c860
MS
940 if (rc == 0 && target == current) {
941 restore_fp_ctl(&target->thread.fp_regs.fpc);
942 restore_fp_regs(target->thread.fp_regs.fprs);
943 }
63506c41
MS
944
945 return rc;
946}
947
86f2552b
MS
948#ifdef CONFIG_64BIT
949
950static int s390_last_break_get(struct task_struct *target,
951 const struct user_regset *regset,
952 unsigned int pos, unsigned int count,
953 void *kbuf, void __user *ubuf)
954{
955 if (count > 0) {
956 if (kbuf) {
957 unsigned long *k = kbuf;
958 *k = task_thread_info(target)->last_break;
959 } else {
960 unsigned long __user *u = ubuf;
961 if (__put_user(task_thread_info(target)->last_break, u))
962 return -EFAULT;
963 }
964 }
965 return 0;
966}
967
b934069c
MS
968static int s390_last_break_set(struct task_struct *target,
969 const struct user_regset *regset,
970 unsigned int pos, unsigned int count,
971 const void *kbuf, const void __user *ubuf)
972{
973 return 0;
974}
975
d35339a4
MS
976static int s390_tdb_get(struct task_struct *target,
977 const struct user_regset *regset,
978 unsigned int pos, unsigned int count,
979 void *kbuf, void __user *ubuf)
980{
981 struct pt_regs *regs = task_pt_regs(target);
982 unsigned char *data;
983
984 if (!(regs->int_code & 0x200))
985 return -ENODATA;
986 data = target->thread.trap_tdb;
987 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, data, 0, 256);
988}
989
990static int s390_tdb_set(struct task_struct *target,
991 const struct user_regset *regset,
992 unsigned int pos, unsigned int count,
993 const void *kbuf, const void __user *ubuf)
994{
995 return 0;
996}
997
86f2552b
MS
998#endif
999
20b40a79
MS
1000static int s390_system_call_get(struct task_struct *target,
1001 const struct user_regset *regset,
1002 unsigned int pos, unsigned int count,
1003 void *kbuf, void __user *ubuf)
1004{
1005 unsigned int *data = &task_thread_info(target)->system_call;
1006 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
1007 data, 0, sizeof(unsigned int));
1008}
1009
1010static int s390_system_call_set(struct task_struct *target,
1011 const struct user_regset *regset,
1012 unsigned int pos, unsigned int count,
1013 const void *kbuf, const void __user *ubuf)
1014{
1015 unsigned int *data = &task_thread_info(target)->system_call;
1016 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1017 data, 0, sizeof(unsigned int));
1018}
1019
63506c41
MS
1020static const struct user_regset s390_regsets[] = {
1021 [REGSET_GENERAL] = {
1022 .core_note_type = NT_PRSTATUS,
1023 .n = sizeof(s390_regs) / sizeof(long),
1024 .size = sizeof(long),
1025 .align = sizeof(long),
1026 .get = s390_regs_get,
1027 .set = s390_regs_set,
1028 },
1029 [REGSET_FP] = {
1030 .core_note_type = NT_PRFPREG,
1031 .n = sizeof(s390_fp_regs) / sizeof(long),
1032 .size = sizeof(long),
1033 .align = sizeof(long),
1034 .get = s390_fpregs_get,
1035 .set = s390_fpregs_set,
1036 },
86f2552b
MS
1037#ifdef CONFIG_64BIT
1038 [REGSET_LAST_BREAK] = {
1039 .core_note_type = NT_S390_LAST_BREAK,
1040 .n = 1,
1041 .size = sizeof(long),
1042 .align = sizeof(long),
1043 .get = s390_last_break_get,
b934069c 1044 .set = s390_last_break_set,
86f2552b 1045 },
d35339a4
MS
1046 [REGSET_TDB] = {
1047 .core_note_type = NT_S390_TDB,
1048 .n = 1,
1049 .size = 256,
1050 .align = 1,
1051 .get = s390_tdb_get,
1052 .set = s390_tdb_set,
1053 },
86f2552b 1054#endif
20b40a79
MS
1055 [REGSET_SYSTEM_CALL] = {
1056 .core_note_type = NT_S390_SYSTEM_CALL,
1057 .n = 1,
1058 .size = sizeof(unsigned int),
1059 .align = sizeof(unsigned int),
1060 .get = s390_system_call_get,
1061 .set = s390_system_call_set,
1062 },
63506c41
MS
1063};
1064
1065static const struct user_regset_view user_s390_view = {
1066 .name = UTS_MACHINE,
1067 .e_machine = EM_S390,
1068 .regsets = s390_regsets,
1069 .n = ARRAY_SIZE(s390_regsets)
1070};
1071
1072#ifdef CONFIG_COMPAT
1073static int s390_compat_regs_get(struct task_struct *target,
1074 const struct user_regset *regset,
1075 unsigned int pos, unsigned int count,
1076 void *kbuf, void __user *ubuf)
1077{
1078 if (target == current)
1079 save_access_regs(target->thread.acrs);
1080
1081 if (kbuf) {
1082 compat_ulong_t *k = kbuf;
1083 while (count > 0) {
1084 *k++ = __peek_user_compat(target, pos);
1085 count -= sizeof(*k);
1086 pos += sizeof(*k);
1087 }
1088 } else {
1089 compat_ulong_t __user *u = ubuf;
1090 while (count > 0) {
1091 if (__put_user(__peek_user_compat(target, pos), u++))
1092 return -EFAULT;
1093 count -= sizeof(*u);
1094 pos += sizeof(*u);
1095 }
1096 }
1097 return 0;
1098}
1099
1100static int s390_compat_regs_set(struct task_struct *target,
1101 const struct user_regset *regset,
1102 unsigned int pos, unsigned int count,
1103 const void *kbuf, const void __user *ubuf)
1104{
1105 int rc = 0;
1106
1107 if (target == current)
1108 save_access_regs(target->thread.acrs);
1109
1110 if (kbuf) {
1111 const compat_ulong_t *k = kbuf;
1112 while (count > 0 && !rc) {
1113 rc = __poke_user_compat(target, pos, *k++);
1114 count -= sizeof(*k);
1115 pos += sizeof(*k);
1116 }
1117 } else {
1118 const compat_ulong_t __user *u = ubuf;
1119 while (count > 0 && !rc) {
1120 compat_ulong_t word;
1121 rc = __get_user(word, u++);
1122 if (rc)
1123 break;
1124 rc = __poke_user_compat(target, pos, word);
1125 count -= sizeof(*u);
1126 pos += sizeof(*u);
1127 }
1128 }
1129
1130 if (rc == 0 && target == current)
1131 restore_access_regs(target->thread.acrs);
1132
1133 return rc;
1134}
1135
ea2a4d3a
HC
1136static int s390_compat_regs_high_get(struct task_struct *target,
1137 const struct user_regset *regset,
1138 unsigned int pos, unsigned int count,
1139 void *kbuf, void __user *ubuf)
1140{
1141 compat_ulong_t *gprs_high;
1142
1143 gprs_high = (compat_ulong_t *)
1144 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1145 if (kbuf) {
1146 compat_ulong_t *k = kbuf;
1147 while (count > 0) {
1148 *k++ = *gprs_high;
1149 gprs_high += 2;
1150 count -= sizeof(*k);
1151 }
1152 } else {
1153 compat_ulong_t __user *u = ubuf;
1154 while (count > 0) {
1155 if (__put_user(*gprs_high, u++))
1156 return -EFAULT;
1157 gprs_high += 2;
1158 count -= sizeof(*u);
1159 }
1160 }
1161 return 0;
1162}
1163
1164static int s390_compat_regs_high_set(struct task_struct *target,
1165 const struct user_regset *regset,
1166 unsigned int pos, unsigned int count,
1167 const void *kbuf, const void __user *ubuf)
1168{
1169 compat_ulong_t *gprs_high;
1170 int rc = 0;
1171
1172 gprs_high = (compat_ulong_t *)
1173 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1174 if (kbuf) {
1175 const compat_ulong_t *k = kbuf;
1176 while (count > 0) {
1177 *gprs_high = *k++;
1178 *gprs_high += 2;
1179 count -= sizeof(*k);
1180 }
1181 } else {
1182 const compat_ulong_t __user *u = ubuf;
1183 while (count > 0 && !rc) {
1184 unsigned long word;
1185 rc = __get_user(word, u++);
1186 if (rc)
1187 break;
1188 *gprs_high = word;
1189 *gprs_high += 2;
1190 count -= sizeof(*u);
1191 }
1192 }
1193
1194 return rc;
1195}
1196
86f2552b
MS
1197static int s390_compat_last_break_get(struct task_struct *target,
1198 const struct user_regset *regset,
1199 unsigned int pos, unsigned int count,
1200 void *kbuf, void __user *ubuf)
1201{
1202 compat_ulong_t last_break;
1203
1204 if (count > 0) {
1205 last_break = task_thread_info(target)->last_break;
1206 if (kbuf) {
1207 unsigned long *k = kbuf;
1208 *k = last_break;
1209 } else {
1210 unsigned long __user *u = ubuf;
1211 if (__put_user(last_break, u))
1212 return -EFAULT;
1213 }
1214 }
1215 return 0;
1216}
1217
b934069c
MS
1218static int s390_compat_last_break_set(struct task_struct *target,
1219 const struct user_regset *regset,
1220 unsigned int pos, unsigned int count,
1221 const void *kbuf, const void __user *ubuf)
1222{
1223 return 0;
1224}
1225
63506c41
MS
1226static const struct user_regset s390_compat_regsets[] = {
1227 [REGSET_GENERAL] = {
1228 .core_note_type = NT_PRSTATUS,
1229 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
1230 .size = sizeof(compat_long_t),
1231 .align = sizeof(compat_long_t),
1232 .get = s390_compat_regs_get,
1233 .set = s390_compat_regs_set,
1234 },
1235 [REGSET_FP] = {
1236 .core_note_type = NT_PRFPREG,
1237 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
1238 .size = sizeof(compat_long_t),
1239 .align = sizeof(compat_long_t),
1240 .get = s390_fpregs_get,
1241 .set = s390_fpregs_set,
1242 },
86f2552b
MS
1243 [REGSET_LAST_BREAK] = {
1244 .core_note_type = NT_S390_LAST_BREAK,
1245 .n = 1,
1246 .size = sizeof(long),
1247 .align = sizeof(long),
1248 .get = s390_compat_last_break_get,
b934069c 1249 .set = s390_compat_last_break_set,
86f2552b 1250 },
d35339a4
MS
1251 [REGSET_TDB] = {
1252 .core_note_type = NT_S390_TDB,
1253 .n = 1,
1254 .size = 256,
1255 .align = 1,
1256 .get = s390_tdb_get,
1257 .set = s390_tdb_set,
1258 },
20b40a79
MS
1259 [REGSET_SYSTEM_CALL] = {
1260 .core_note_type = NT_S390_SYSTEM_CALL,
1261 .n = 1,
1262 .size = sizeof(compat_uint_t),
1263 .align = sizeof(compat_uint_t),
1264 .get = s390_system_call_get,
1265 .set = s390_system_call_set,
1266 },
ea2a4d3a 1267 [REGSET_GENERAL_EXTENDED] = {
622e99bf 1268 .core_note_type = NT_S390_HIGH_GPRS,
ea2a4d3a
HC
1269 .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
1270 .size = sizeof(compat_long_t),
1271 .align = sizeof(compat_long_t),
1272 .get = s390_compat_regs_high_get,
1273 .set = s390_compat_regs_high_set,
1274 },
63506c41
MS
1275};
1276
1277static const struct user_regset_view user_s390_compat_view = {
1278 .name = "s390",
1279 .e_machine = EM_S390,
1280 .regsets = s390_compat_regsets,
1281 .n = ARRAY_SIZE(s390_compat_regsets)
1282};
1283#endif
1284
1285const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1286{
1287#ifdef CONFIG_COMPAT
1288 if (test_tsk_thread_flag(task, TIF_31BIT))
1289 return &user_s390_compat_view;
1290#endif
1291 return &user_s390_view;
1292}
952974ac
HC
1293
1294static const char *gpr_names[NUM_GPRS] = {
1295 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1296 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1297};
1298
1299unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
1300{
1301 if (offset >= NUM_GPRS)
1302 return 0;
1303 return regs->gprs[offset];
1304}
1305
1306int regs_query_register_offset(const char *name)
1307{
1308 unsigned long offset;
1309
1310 if (!name || *name != 'r')
1311 return -EINVAL;
958d9072 1312 if (kstrtoul(name + 1, 10, &offset))
952974ac
HC
1313 return -EINVAL;
1314 if (offset >= NUM_GPRS)
1315 return -EINVAL;
1316 return offset;
1317}
1318
1319const char *regs_query_register_name(unsigned int offset)
1320{
1321 if (offset >= NUM_GPRS)
1322 return NULL;
1323 return gpr_names[offset];
1324}
1325
1326static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
1327{
1328 unsigned long ksp = kernel_stack_pointer(regs);
1329
1330 return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
1331}
1332
1333/**
1334 * regs_get_kernel_stack_nth() - get Nth entry of the stack
1335 * @regs:pt_regs which contains kernel stack pointer.
1336 * @n:stack entry number.
1337 *
1338 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
1339 * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
1340 * this returns 0.
1341 */
1342unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
1343{
1344 unsigned long addr;
1345
1346 addr = kernel_stack_pointer(regs) + n * sizeof(long);
1347 if (!regs_within_kernel_stack(regs, addr))
1348 return 0;
1349 return *(unsigned long *)addr;
1350}