USB: BKL removal: ftdi-elan
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / s390 / kernel / ptrace.c
blob33fdc5a7976472831185126e419a637807e48b1d
1 /*
2 * arch/s390/kernel/ptrace.c
4 * S390 version
5 * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
7 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 * Based on PowerPC version
10 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
12 * Derived from "arch/m68k/kernel/ptrace.c"
13 * Copyright (C) 1994 by Hamish Macdonald
14 * Taken from linux/kernel/ptrace.c and modified for M680x0.
15 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
17 * Modified by Cort Dougan (cort@cs.nmt.edu)
20 * This file is subject to the terms and conditions of the GNU General
21 * Public License. See the file README.legal in the main directory of
22 * this archive for more details.
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/mm.h>
28 #include <linux/smp.h>
29 #include <linux/errno.h>
30 #include <linux/ptrace.h>
31 #include <linux/user.h>
32 #include <linux/security.h>
33 #include <linux/audit.h>
34 #include <linux/signal.h>
35 #include <linux/elf.h>
36 #include <linux/regset.h>
37 #include <linux/tracehook.h>
38 #include <linux/seccomp.h>
39 #include <trace/syscall.h>
40 #include <asm/compat.h>
41 #include <asm/segment.h>
42 #include <asm/page.h>
43 #include <asm/pgtable.h>
44 #include <asm/pgalloc.h>
45 #include <asm/system.h>
46 #include <asm/uaccess.h>
47 #include <asm/unistd.h>
48 #include "entry.h"
50 #ifdef CONFIG_COMPAT
51 #include "compat_ptrace.h"
52 #endif
54 #define CREATE_TRACE_POINTS
55 #include <trace/events/syscalls.h>
57 enum s390_regset {
58 REGSET_GENERAL,
59 REGSET_FP,
60 REGSET_GENERAL_EXTENDED,
63 static void
64 FixPerRegisters(struct task_struct *task)
66 struct pt_regs *regs;
67 per_struct *per_info;
68 per_cr_words cr_words;
70 regs = task_pt_regs(task);
71 per_info = (per_struct *) &task->thread.per_info;
72 per_info->control_regs.bits.em_instruction_fetch =
73 per_info->single_step | per_info->instruction_fetch;
75 if (per_info->single_step) {
76 per_info->control_regs.bits.starting_addr = 0;
77 #ifdef CONFIG_COMPAT
78 if (is_compat_task())
79 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
80 else
81 #endif
82 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
83 } else {
84 per_info->control_regs.bits.starting_addr =
85 per_info->starting_addr;
86 per_info->control_regs.bits.ending_addr =
87 per_info->ending_addr;
90 * if any of the control reg tracing bits are on
91 * we switch on per in the psw
93 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
94 regs->psw.mask |= PSW_MASK_PER;
95 else
96 regs->psw.mask &= ~PSW_MASK_PER;
98 if (per_info->control_regs.bits.em_storage_alteration)
99 per_info->control_regs.bits.storage_alt_space_ctl = 1;
100 else
101 per_info->control_regs.bits.storage_alt_space_ctl = 0;
103 if (task == current) {
104 __ctl_store(cr_words, 9, 11);
105 if (memcmp(&cr_words, &per_info->control_regs.words,
106 sizeof(cr_words)) != 0)
107 __ctl_load(per_info->control_regs.words, 9, 11);
111 void user_enable_single_step(struct task_struct *task)
113 task->thread.per_info.single_step = 1;
114 FixPerRegisters(task);
117 void user_disable_single_step(struct task_struct *task)
119 task->thread.per_info.single_step = 0;
120 FixPerRegisters(task);
124 * Called by kernel/ptrace.c when detaching..
126 * Make sure single step bits etc are not set.
128 void
129 ptrace_disable(struct task_struct *child)
131 /* make sure the single step bit is not set. */
132 user_disable_single_step(child);
135 #ifndef CONFIG_64BIT
136 # define __ADDR_MASK 3
137 #else
138 # define __ADDR_MASK 7
139 #endif
142 * Read the word at offset addr from the user area of a process. The
143 * trouble here is that the information is littered over different
144 * locations. The process registers are found on the kernel stack,
145 * the floating point stuff and the trace settings are stored in
146 * the task structure. In addition the different structures in
147 * struct user contain pad bytes that should be read as zeroes.
148 * Lovely...
150 static unsigned long __peek_user(struct task_struct *child, addr_t addr)
152 struct user *dummy = NULL;
153 addr_t offset, tmp;
155 if (addr < (addr_t) &dummy->regs.acrs) {
157 * psw and gprs are stored on the stack
159 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
160 if (addr == (addr_t) &dummy->regs.psw.mask)
161 /* Remove per bit from user psw. */
162 tmp &= ~PSW_MASK_PER;
164 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
166 * access registers are stored in the thread structure
168 offset = addr - (addr_t) &dummy->regs.acrs;
169 #ifdef CONFIG_64BIT
171 * Very special case: old & broken 64 bit gdb reading
172 * from acrs[15]. Result is a 64 bit value. Read the
173 * 32 bit acrs[15] value and shift it by 32. Sick...
175 if (addr == (addr_t) &dummy->regs.acrs[15])
176 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
177 else
178 #endif
179 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
181 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
183 * orig_gpr2 is stored on the kernel stack
185 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
187 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
189 * prevent reads of padding hole between
190 * orig_gpr2 and fp_regs on s390.
192 tmp = 0;
194 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
196 * floating point regs. are stored in the thread structure
198 offset = addr - (addr_t) &dummy->regs.fp_regs;
199 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
200 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
201 tmp &= (unsigned long) FPC_VALID_MASK
202 << (BITS_PER_LONG - 32);
204 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
206 * per_info is found in the thread structure
208 offset = addr - (addr_t) &dummy->regs.per_info;
209 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
211 } else
212 tmp = 0;
214 return tmp;
217 static int
218 peek_user(struct task_struct *child, addr_t addr, addr_t data)
220 addr_t tmp, mask;
223 * Stupid gdb peeks/pokes the access registers in 64 bit with
224 * an alignment of 4. Programmers from hell...
226 mask = __ADDR_MASK;
227 #ifdef CONFIG_64BIT
228 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
229 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
230 mask = 3;
231 #endif
232 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
233 return -EIO;
235 tmp = __peek_user(child, addr);
236 return put_user(tmp, (addr_t __user *) data);
240 * Write a word to the user area of a process at location addr. This
241 * operation does have an additional problem compared to peek_user.
242 * Stores to the program status word and on the floating point
243 * control register needs to get checked for validity.
245 static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
247 struct user *dummy = NULL;
248 addr_t offset;
250 if (addr < (addr_t) &dummy->regs.acrs) {
252 * psw and gprs are stored on the stack
254 if (addr == (addr_t) &dummy->regs.psw.mask &&
255 #ifdef CONFIG_COMPAT
256 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
257 #endif
258 data != PSW_MASK_MERGE(psw_user_bits, data))
259 /* Invalid psw mask. */
260 return -EINVAL;
261 #ifndef CONFIG_64BIT
262 if (addr == (addr_t) &dummy->regs.psw.addr)
263 /* I'd like to reject addresses without the
264 high order bit but older gdb's rely on it */
265 data |= PSW_ADDR_AMODE;
266 #endif
267 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
269 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
271 * access registers are stored in the thread structure
273 offset = addr - (addr_t) &dummy->regs.acrs;
274 #ifdef CONFIG_64BIT
276 * Very special case: old & broken 64 bit gdb writing
277 * to acrs[15] with a 64 bit value. Ignore the lower
278 * half of the value and write the upper 32 bit to
279 * acrs[15]. Sick...
281 if (addr == (addr_t) &dummy->regs.acrs[15])
282 child->thread.acrs[15] = (unsigned int) (data >> 32);
283 else
284 #endif
285 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
287 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
289 * orig_gpr2 is stored on the kernel stack
291 task_pt_regs(child)->orig_gpr2 = data;
293 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
295 * prevent writes of padding hole between
296 * orig_gpr2 and fp_regs on s390.
298 return 0;
300 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
302 * floating point regs. are stored in the thread structure
304 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
305 (data & ~((unsigned long) FPC_VALID_MASK
306 << (BITS_PER_LONG - 32))) != 0)
307 return -EINVAL;
308 offset = addr - (addr_t) &dummy->regs.fp_regs;
309 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
311 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
313 * per_info is found in the thread structure
315 offset = addr - (addr_t) &dummy->regs.per_info;
316 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
320 FixPerRegisters(child);
321 return 0;
324 static int
325 poke_user(struct task_struct *child, addr_t addr, addr_t data)
327 addr_t mask;
330 * Stupid gdb peeks/pokes the access registers in 64 bit with
331 * an alignment of 4. Programmers from hell indeed...
333 mask = __ADDR_MASK;
334 #ifdef CONFIG_64BIT
335 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
336 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
337 mask = 3;
338 #endif
339 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
340 return -EIO;
342 return __poke_user(child, addr, data);
345 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
347 ptrace_area parea;
348 int copied, ret;
350 switch (request) {
351 case PTRACE_PEEKUSR:
352 /* read the word at location addr in the USER area. */
353 return peek_user(child, addr, data);
355 case PTRACE_POKEUSR:
356 /* write the word at location addr in the USER area */
357 return poke_user(child, addr, data);
359 case PTRACE_PEEKUSR_AREA:
360 case PTRACE_POKEUSR_AREA:
361 if (copy_from_user(&parea, (void __force __user *) addr,
362 sizeof(parea)))
363 return -EFAULT;
364 addr = parea.kernel_addr;
365 data = parea.process_addr;
366 copied = 0;
367 while (copied < parea.len) {
368 if (request == PTRACE_PEEKUSR_AREA)
369 ret = peek_user(child, addr, data);
370 else {
371 addr_t utmp;
372 if (get_user(utmp,
373 (addr_t __force __user *) data))
374 return -EFAULT;
375 ret = poke_user(child, addr, utmp);
377 if (ret)
378 return ret;
379 addr += sizeof(unsigned long);
380 data += sizeof(unsigned long);
381 copied += sizeof(unsigned long);
383 return 0;
384 default:
385 /* Removing high order bit from addr (only for 31 bit). */
386 addr &= PSW_ADDR_INSN;
387 return ptrace_request(child, request, addr, data);
391 #ifdef CONFIG_COMPAT
393 * Now the fun part starts... a 31 bit program running in the
394 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
395 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
396 * to handle, the difference to the 64 bit versions of the requests
397 * is that the access is done in multiples of 4 byte instead of
398 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
399 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
400 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
401 * is a 31 bit program too, the content of struct user can be
402 * emulated. A 31 bit program peeking into the struct user of
403 * a 64 bit program is a no-no.
407 * Same as peek_user but for a 31 bit program.
409 static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
411 struct user32 *dummy32 = NULL;
412 per_struct32 *dummy_per32 = NULL;
413 addr_t offset;
414 __u32 tmp;
416 if (addr < (addr_t) &dummy32->regs.acrs) {
418 * psw and gprs are stored on the stack
420 if (addr == (addr_t) &dummy32->regs.psw.mask) {
421 /* Fake a 31 bit psw mask. */
422 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
423 tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
424 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
425 /* Fake a 31 bit psw address. */
426 tmp = (__u32) task_pt_regs(child)->psw.addr |
427 PSW32_ADDR_AMODE31;
428 } else {
429 /* gpr 0-15 */
430 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
431 addr*2 + 4);
433 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
435 * access registers are stored in the thread structure
437 offset = addr - (addr_t) &dummy32->regs.acrs;
438 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
440 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
442 * orig_gpr2 is stored on the kernel stack
444 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
446 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
448 * prevent reads of padding hole between
449 * orig_gpr2 and fp_regs on s390.
451 tmp = 0;
453 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
455 * floating point regs. are stored in the thread structure
457 offset = addr - (addr_t) &dummy32->regs.fp_regs;
458 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
460 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
462 * per_info is found in the thread structure
464 offset = addr - (addr_t) &dummy32->regs.per_info;
465 /* This is magic. See per_struct and per_struct32. */
466 if ((offset >= (addr_t) &dummy_per32->control_regs &&
467 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
468 (offset >= (addr_t) &dummy_per32->starting_addr &&
469 offset <= (addr_t) &dummy_per32->ending_addr) ||
470 offset == (addr_t) &dummy_per32->lowcore.words.address)
471 offset = offset*2 + 4;
472 else
473 offset = offset*2;
474 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
476 } else
477 tmp = 0;
479 return tmp;
482 static int peek_user_compat(struct task_struct *child,
483 addr_t addr, addr_t data)
485 __u32 tmp;
487 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
488 return -EIO;
490 tmp = __peek_user_compat(child, addr);
491 return put_user(tmp, (__u32 __user *) data);
495 * Same as poke_user but for a 31 bit program.
497 static int __poke_user_compat(struct task_struct *child,
498 addr_t addr, addr_t data)
500 struct user32 *dummy32 = NULL;
501 per_struct32 *dummy_per32 = NULL;
502 __u32 tmp = (__u32) data;
503 addr_t offset;
505 if (addr < (addr_t) &dummy32->regs.acrs) {
507 * psw, gprs, acrs and orig_gpr2 are stored on the stack
509 if (addr == (addr_t) &dummy32->regs.psw.mask) {
510 /* Build a 64 bit psw mask from 31 bit mask. */
511 if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
512 /* Invalid psw mask. */
513 return -EINVAL;
514 task_pt_regs(child)->psw.mask =
515 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
516 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
517 /* Build a 64 bit psw address from 31 bit address. */
518 task_pt_regs(child)->psw.addr =
519 (__u64) tmp & PSW32_ADDR_INSN;
520 } else {
521 /* gpr 0-15 */
522 *(__u32*)((addr_t) &task_pt_regs(child)->psw
523 + addr*2 + 4) = tmp;
525 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
527 * access registers are stored in the thread structure
529 offset = addr - (addr_t) &dummy32->regs.acrs;
530 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
532 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
534 * orig_gpr2 is stored on the kernel stack
536 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
538 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
540 * prevent writess of padding hole between
541 * orig_gpr2 and fp_regs on s390.
543 return 0;
545 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
547 * floating point regs. are stored in the thread structure
549 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
550 (tmp & ~FPC_VALID_MASK) != 0)
551 /* Invalid floating point control. */
552 return -EINVAL;
553 offset = addr - (addr_t) &dummy32->regs.fp_regs;
554 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
556 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
558 * per_info is found in the thread structure.
560 offset = addr - (addr_t) &dummy32->regs.per_info;
562 * This is magic. See per_struct and per_struct32.
563 * By incident the offsets in per_struct are exactly
564 * twice the offsets in per_struct32 for all fields.
565 * The 8 byte fields need special handling though,
566 * because the second half (bytes 4-7) is needed and
567 * not the first half.
569 if ((offset >= (addr_t) &dummy_per32->control_regs &&
570 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
571 (offset >= (addr_t) &dummy_per32->starting_addr &&
572 offset <= (addr_t) &dummy_per32->ending_addr) ||
573 offset == (addr_t) &dummy_per32->lowcore.words.address)
574 offset = offset*2 + 4;
575 else
576 offset = offset*2;
577 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
581 FixPerRegisters(child);
582 return 0;
585 static int poke_user_compat(struct task_struct *child,
586 addr_t addr, addr_t data)
588 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user32) - 3)
589 return -EIO;
591 return __poke_user_compat(child, addr, data);
594 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
595 compat_ulong_t caddr, compat_ulong_t cdata)
597 unsigned long addr = caddr;
598 unsigned long data = cdata;
599 ptrace_area_emu31 parea;
600 int copied, ret;
602 switch (request) {
603 case PTRACE_PEEKUSR:
604 /* read the word at location addr in the USER area. */
605 return peek_user_compat(child, addr, data);
607 case PTRACE_POKEUSR:
608 /* write the word at location addr in the USER area */
609 return poke_user_compat(child, addr, data);
611 case PTRACE_PEEKUSR_AREA:
612 case PTRACE_POKEUSR_AREA:
613 if (copy_from_user(&parea, (void __force __user *) addr,
614 sizeof(parea)))
615 return -EFAULT;
616 addr = parea.kernel_addr;
617 data = parea.process_addr;
618 copied = 0;
619 while (copied < parea.len) {
620 if (request == PTRACE_PEEKUSR_AREA)
621 ret = peek_user_compat(child, addr, data);
622 else {
623 __u32 utmp;
624 if (get_user(utmp,
625 (__u32 __force __user *) data))
626 return -EFAULT;
627 ret = poke_user_compat(child, addr, utmp);
629 if (ret)
630 return ret;
631 addr += sizeof(unsigned int);
632 data += sizeof(unsigned int);
633 copied += sizeof(unsigned int);
635 return 0;
637 return compat_ptrace_request(child, request, addr, data);
639 #endif
641 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
643 long ret;
645 /* Do the secure computing check first. */
646 secure_computing(regs->gprs[2]);
649 * The sysc_tracesys code in entry.S stored the system
650 * call number to gprs[2].
652 ret = regs->gprs[2];
653 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
654 (tracehook_report_syscall_entry(regs) ||
655 regs->gprs[2] >= NR_syscalls)) {
657 * Tracing decided this syscall should not happen or the
658 * debugger stored an invalid system call number. Skip
659 * the system call and the system call restart handling.
661 regs->svcnr = 0;
662 ret = -1;
665 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
666 trace_sys_enter(regs, regs->gprs[2]);
668 if (unlikely(current->audit_context))
669 audit_syscall_entry(is_compat_task() ?
670 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
671 regs->gprs[2], regs->orig_gpr2,
672 regs->gprs[3], regs->gprs[4],
673 regs->gprs[5]);
674 return ret;
677 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
679 if (unlikely(current->audit_context))
680 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
681 regs->gprs[2]);
683 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
684 trace_sys_exit(regs, regs->gprs[2]);
686 if (test_thread_flag(TIF_SYSCALL_TRACE))
687 tracehook_report_syscall_exit(regs, 0);
691 * user_regset definitions.
694 static int s390_regs_get(struct task_struct *target,
695 const struct user_regset *regset,
696 unsigned int pos, unsigned int count,
697 void *kbuf, void __user *ubuf)
699 if (target == current)
700 save_access_regs(target->thread.acrs);
702 if (kbuf) {
703 unsigned long *k = kbuf;
704 while (count > 0) {
705 *k++ = __peek_user(target, pos);
706 count -= sizeof(*k);
707 pos += sizeof(*k);
709 } else {
710 unsigned long __user *u = ubuf;
711 while (count > 0) {
712 if (__put_user(__peek_user(target, pos), u++))
713 return -EFAULT;
714 count -= sizeof(*u);
715 pos += sizeof(*u);
718 return 0;
721 static int s390_regs_set(struct task_struct *target,
722 const struct user_regset *regset,
723 unsigned int pos, unsigned int count,
724 const void *kbuf, const void __user *ubuf)
726 int rc = 0;
728 if (target == current)
729 save_access_regs(target->thread.acrs);
731 if (kbuf) {
732 const unsigned long *k = kbuf;
733 while (count > 0 && !rc) {
734 rc = __poke_user(target, pos, *k++);
735 count -= sizeof(*k);
736 pos += sizeof(*k);
738 } else {
739 const unsigned long __user *u = ubuf;
740 while (count > 0 && !rc) {
741 unsigned long word;
742 rc = __get_user(word, u++);
743 if (rc)
744 break;
745 rc = __poke_user(target, pos, word);
746 count -= sizeof(*u);
747 pos += sizeof(*u);
751 if (rc == 0 && target == current)
752 restore_access_regs(target->thread.acrs);
754 return rc;
757 static int s390_fpregs_get(struct task_struct *target,
758 const struct user_regset *regset, unsigned int pos,
759 unsigned int count, void *kbuf, void __user *ubuf)
761 if (target == current)
762 save_fp_regs(&target->thread.fp_regs);
764 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
765 &target->thread.fp_regs, 0, -1);
768 static int s390_fpregs_set(struct task_struct *target,
769 const struct user_regset *regset, unsigned int pos,
770 unsigned int count, const void *kbuf,
771 const void __user *ubuf)
773 int rc = 0;
775 if (target == current)
776 save_fp_regs(&target->thread.fp_regs);
778 /* If setting FPC, must validate it first. */
779 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
780 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
781 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
782 0, offsetof(s390_fp_regs, fprs));
783 if (rc)
784 return rc;
785 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
786 return -EINVAL;
787 target->thread.fp_regs.fpc = fpc[0];
790 if (rc == 0 && count > 0)
791 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
792 target->thread.fp_regs.fprs,
793 offsetof(s390_fp_regs, fprs), -1);
795 if (rc == 0 && target == current)
796 restore_fp_regs(&target->thread.fp_regs);
798 return rc;
801 static const struct user_regset s390_regsets[] = {
802 [REGSET_GENERAL] = {
803 .core_note_type = NT_PRSTATUS,
804 .n = sizeof(s390_regs) / sizeof(long),
805 .size = sizeof(long),
806 .align = sizeof(long),
807 .get = s390_regs_get,
808 .set = s390_regs_set,
810 [REGSET_FP] = {
811 .core_note_type = NT_PRFPREG,
812 .n = sizeof(s390_fp_regs) / sizeof(long),
813 .size = sizeof(long),
814 .align = sizeof(long),
815 .get = s390_fpregs_get,
816 .set = s390_fpregs_set,
820 static const struct user_regset_view user_s390_view = {
821 .name = UTS_MACHINE,
822 .e_machine = EM_S390,
823 .regsets = s390_regsets,
824 .n = ARRAY_SIZE(s390_regsets)
827 #ifdef CONFIG_COMPAT
828 static int s390_compat_regs_get(struct task_struct *target,
829 const struct user_regset *regset,
830 unsigned int pos, unsigned int count,
831 void *kbuf, void __user *ubuf)
833 if (target == current)
834 save_access_regs(target->thread.acrs);
836 if (kbuf) {
837 compat_ulong_t *k = kbuf;
838 while (count > 0) {
839 *k++ = __peek_user_compat(target, pos);
840 count -= sizeof(*k);
841 pos += sizeof(*k);
843 } else {
844 compat_ulong_t __user *u = ubuf;
845 while (count > 0) {
846 if (__put_user(__peek_user_compat(target, pos), u++))
847 return -EFAULT;
848 count -= sizeof(*u);
849 pos += sizeof(*u);
852 return 0;
855 static int s390_compat_regs_set(struct task_struct *target,
856 const struct user_regset *regset,
857 unsigned int pos, unsigned int count,
858 const void *kbuf, const void __user *ubuf)
860 int rc = 0;
862 if (target == current)
863 save_access_regs(target->thread.acrs);
865 if (kbuf) {
866 const compat_ulong_t *k = kbuf;
867 while (count > 0 && !rc) {
868 rc = __poke_user_compat(target, pos, *k++);
869 count -= sizeof(*k);
870 pos += sizeof(*k);
872 } else {
873 const compat_ulong_t __user *u = ubuf;
874 while (count > 0 && !rc) {
875 compat_ulong_t word;
876 rc = __get_user(word, u++);
877 if (rc)
878 break;
879 rc = __poke_user_compat(target, pos, word);
880 count -= sizeof(*u);
881 pos += sizeof(*u);
885 if (rc == 0 && target == current)
886 restore_access_regs(target->thread.acrs);
888 return rc;
891 static int s390_compat_regs_high_get(struct task_struct *target,
892 const struct user_regset *regset,
893 unsigned int pos, unsigned int count,
894 void *kbuf, void __user *ubuf)
896 compat_ulong_t *gprs_high;
898 gprs_high = (compat_ulong_t *)
899 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
900 if (kbuf) {
901 compat_ulong_t *k = kbuf;
902 while (count > 0) {
903 *k++ = *gprs_high;
904 gprs_high += 2;
905 count -= sizeof(*k);
907 } else {
908 compat_ulong_t __user *u = ubuf;
909 while (count > 0) {
910 if (__put_user(*gprs_high, u++))
911 return -EFAULT;
912 gprs_high += 2;
913 count -= sizeof(*u);
916 return 0;
919 static int s390_compat_regs_high_set(struct task_struct *target,
920 const struct user_regset *regset,
921 unsigned int pos, unsigned int count,
922 const void *kbuf, const void __user *ubuf)
924 compat_ulong_t *gprs_high;
925 int rc = 0;
927 gprs_high = (compat_ulong_t *)
928 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
929 if (kbuf) {
930 const compat_ulong_t *k = kbuf;
931 while (count > 0) {
932 *gprs_high = *k++;
933 *gprs_high += 2;
934 count -= sizeof(*k);
936 } else {
937 const compat_ulong_t __user *u = ubuf;
938 while (count > 0 && !rc) {
939 unsigned long word;
940 rc = __get_user(word, u++);
941 if (rc)
942 break;
943 *gprs_high = word;
944 *gprs_high += 2;
945 count -= sizeof(*u);
949 return rc;
952 static const struct user_regset s390_compat_regsets[] = {
953 [REGSET_GENERAL] = {
954 .core_note_type = NT_PRSTATUS,
955 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
956 .size = sizeof(compat_long_t),
957 .align = sizeof(compat_long_t),
958 .get = s390_compat_regs_get,
959 .set = s390_compat_regs_set,
961 [REGSET_FP] = {
962 .core_note_type = NT_PRFPREG,
963 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
964 .size = sizeof(compat_long_t),
965 .align = sizeof(compat_long_t),
966 .get = s390_fpregs_get,
967 .set = s390_fpregs_set,
969 [REGSET_GENERAL_EXTENDED] = {
970 .core_note_type = NT_S390_HIGH_GPRS,
971 .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
972 .size = sizeof(compat_long_t),
973 .align = sizeof(compat_long_t),
974 .get = s390_compat_regs_high_get,
975 .set = s390_compat_regs_high_set,
979 static const struct user_regset_view user_s390_compat_view = {
980 .name = "s390",
981 .e_machine = EM_S390,
982 .regsets = s390_compat_regsets,
983 .n = ARRAY_SIZE(s390_compat_regsets)
985 #endif
987 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
989 #ifdef CONFIG_COMPAT
990 if (test_tsk_thread_flag(task, TIF_31BIT))
991 return &user_s390_compat_view;
992 #endif
993 return &user_s390_view;
996 static const char *gpr_names[NUM_GPRS] = {
997 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
998 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1001 unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
1003 if (offset >= NUM_GPRS)
1004 return 0;
1005 return regs->gprs[offset];
1008 int regs_query_register_offset(const char *name)
1010 unsigned long offset;
1012 if (!name || *name != 'r')
1013 return -EINVAL;
1014 if (strict_strtoul(name + 1, 10, &offset))
1015 return -EINVAL;
1016 if (offset >= NUM_GPRS)
1017 return -EINVAL;
1018 return offset;
1021 const char *regs_query_register_name(unsigned int offset)
1023 if (offset >= NUM_GPRS)
1024 return NULL;
1025 return gpr_names[offset];
1028 static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
1030 unsigned long ksp = kernel_stack_pointer(regs);
1032 return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
1036 * regs_get_kernel_stack_nth() - get Nth entry of the stack
1037 * @regs:pt_regs which contains kernel stack pointer.
1038 * @n:stack entry number.
1040 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
1041 * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
1042 * this returns 0.
1044 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
1046 unsigned long addr;
1048 addr = kernel_stack_pointer(regs) + n * sizeof(long);
1049 if (!regs_within_kernel_stack(regs, addr))
1050 return 0;
1051 return *(unsigned long *)addr;