init from v2.6.32.60
[mach-moxart.git] / arch / s390 / kernel / ptrace.c
blob170e7af7355585060e915a562c891c75ebfba0e1
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 <linux/compat.h>
40 #include <trace/syscall.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;
69 regs = task_pt_regs(task);
70 per_info = (per_struct *) &task->thread.per_info;
71 per_info->control_regs.bits.em_instruction_fetch =
72 per_info->single_step | per_info->instruction_fetch;
74 if (per_info->single_step) {
75 per_info->control_regs.bits.starting_addr = 0;
76 #ifdef CONFIG_COMPAT
77 if (is_compat_task())
78 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
79 else
80 #endif
81 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
82 } else {
83 per_info->control_regs.bits.starting_addr =
84 per_info->starting_addr;
85 per_info->control_regs.bits.ending_addr =
86 per_info->ending_addr;
89 * if any of the control reg tracing bits are on
90 * we switch on per in the psw
92 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
93 regs->psw.mask |= PSW_MASK_PER;
94 else
95 regs->psw.mask &= ~PSW_MASK_PER;
97 if (per_info->control_regs.bits.em_storage_alteration)
98 per_info->control_regs.bits.storage_alt_space_ctl = 1;
99 else
100 per_info->control_regs.bits.storage_alt_space_ctl = 0;
103 void user_enable_single_step(struct task_struct *task)
105 task->thread.per_info.single_step = 1;
106 FixPerRegisters(task);
109 void user_disable_single_step(struct task_struct *task)
111 task->thread.per_info.single_step = 0;
112 FixPerRegisters(task);
116 * Called by kernel/ptrace.c when detaching..
118 * Make sure single step bits etc are not set.
120 void
121 ptrace_disable(struct task_struct *child)
123 /* make sure the single step bit is not set. */
124 user_disable_single_step(child);
127 #ifndef CONFIG_64BIT
128 # define __ADDR_MASK 3
129 #else
130 # define __ADDR_MASK 7
131 #endif
134 * Read the word at offset addr from the user area of a process. The
135 * trouble here is that the information is littered over different
136 * locations. The process registers are found on the kernel stack,
137 * the floating point stuff and the trace settings are stored in
138 * the task structure. In addition the different structures in
139 * struct user contain pad bytes that should be read as zeroes.
140 * Lovely...
142 static unsigned long __peek_user(struct task_struct *child, addr_t addr)
144 struct user *dummy = NULL;
145 addr_t offset, tmp;
147 if (addr < (addr_t) &dummy->regs.acrs) {
149 * psw and gprs are stored on the stack
151 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
152 if (addr == (addr_t) &dummy->regs.psw.mask)
153 /* Remove per bit from user psw. */
154 tmp &= ~PSW_MASK_PER;
156 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
158 * access registers are stored in the thread structure
160 offset = addr - (addr_t) &dummy->regs.acrs;
161 #ifdef CONFIG_64BIT
163 * Very special case: old & broken 64 bit gdb reading
164 * from acrs[15]. Result is a 64 bit value. Read the
165 * 32 bit acrs[15] value and shift it by 32. Sick...
167 if (addr == (addr_t) &dummy->regs.acrs[15])
168 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
169 else
170 #endif
171 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
173 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
175 * orig_gpr2 is stored on the kernel stack
177 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
179 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
181 * prevent reads of padding hole between
182 * orig_gpr2 and fp_regs on s390.
184 tmp = 0;
186 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
188 * floating point regs. are stored in the thread structure
190 offset = addr - (addr_t) &dummy->regs.fp_regs;
191 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
192 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
193 tmp &= (unsigned long) FPC_VALID_MASK
194 << (BITS_PER_LONG - 32);
196 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
198 * per_info is found in the thread structure
200 offset = addr - (addr_t) &dummy->regs.per_info;
201 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
203 } else
204 tmp = 0;
206 return tmp;
209 static int
210 peek_user(struct task_struct *child, addr_t addr, addr_t data)
212 addr_t tmp, mask;
215 * Stupid gdb peeks/pokes the access registers in 64 bit with
216 * an alignment of 4. Programmers from hell...
218 mask = __ADDR_MASK;
219 #ifdef CONFIG_64BIT
220 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
221 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
222 mask = 3;
223 #endif
224 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
225 return -EIO;
227 tmp = __peek_user(child, addr);
228 return put_user(tmp, (addr_t __user *) data);
232 * Write a word to the user area of a process at location addr. This
233 * operation does have an additional problem compared to peek_user.
234 * Stores to the program status word and on the floating point
235 * control register needs to get checked for validity.
237 static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
239 struct user *dummy = NULL;
240 addr_t offset;
242 if (addr < (addr_t) &dummy->regs.acrs) {
244 * psw and gprs are stored on the stack
246 if (addr == (addr_t) &dummy->regs.psw.mask &&
247 #ifdef CONFIG_COMPAT
248 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
249 #endif
250 data != PSW_MASK_MERGE(psw_user_bits, data))
251 /* Invalid psw mask. */
252 return -EINVAL;
253 #ifndef CONFIG_64BIT
254 if (addr == (addr_t) &dummy->regs.psw.addr)
255 /* I'd like to reject addresses without the
256 high order bit but older gdb's rely on it */
257 data |= PSW_ADDR_AMODE;
258 #endif
259 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
261 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
263 * access registers are stored in the thread structure
265 offset = addr - (addr_t) &dummy->regs.acrs;
266 #ifdef CONFIG_64BIT
268 * Very special case: old & broken 64 bit gdb writing
269 * to acrs[15] with a 64 bit value. Ignore the lower
270 * half of the value and write the upper 32 bit to
271 * acrs[15]. Sick...
273 if (addr == (addr_t) &dummy->regs.acrs[15])
274 child->thread.acrs[15] = (unsigned int) (data >> 32);
275 else
276 #endif
277 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
279 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
281 * orig_gpr2 is stored on the kernel stack
283 task_pt_regs(child)->orig_gpr2 = data;
285 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
287 * prevent writes of padding hole between
288 * orig_gpr2 and fp_regs on s390.
290 return 0;
292 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
294 * floating point regs. are stored in the thread structure
296 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
297 (data & ~((unsigned long) FPC_VALID_MASK
298 << (BITS_PER_LONG - 32))) != 0)
299 return -EINVAL;
300 offset = addr - (addr_t) &dummy->regs.fp_regs;
301 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
303 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
305 * per_info is found in the thread structure
307 offset = addr - (addr_t) &dummy->regs.per_info;
308 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
312 FixPerRegisters(child);
313 return 0;
316 static int
317 poke_user(struct task_struct *child, addr_t addr, addr_t data)
319 addr_t mask;
322 * Stupid gdb peeks/pokes the access registers in 64 bit with
323 * an alignment of 4. Programmers from hell indeed...
325 mask = __ADDR_MASK;
326 #ifdef CONFIG_64BIT
327 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
328 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
329 mask = 3;
330 #endif
331 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
332 return -EIO;
334 return __poke_user(child, addr, data);
337 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
339 ptrace_area parea;
340 int copied, ret;
342 switch (request) {
343 case PTRACE_PEEKUSR:
344 /* read the word at location addr in the USER area. */
345 return peek_user(child, addr, data);
347 case PTRACE_POKEUSR:
348 /* write the word at location addr in the USER area */
349 return poke_user(child, addr, data);
351 case PTRACE_PEEKUSR_AREA:
352 case PTRACE_POKEUSR_AREA:
353 if (copy_from_user(&parea, (void __force __user *) addr,
354 sizeof(parea)))
355 return -EFAULT;
356 addr = parea.kernel_addr;
357 data = parea.process_addr;
358 copied = 0;
359 while (copied < parea.len) {
360 if (request == PTRACE_PEEKUSR_AREA)
361 ret = peek_user(child, addr, data);
362 else {
363 addr_t utmp;
364 if (get_user(utmp,
365 (addr_t __force __user *) data))
366 return -EFAULT;
367 ret = poke_user(child, addr, utmp);
369 if (ret)
370 return ret;
371 addr += sizeof(unsigned long);
372 data += sizeof(unsigned long);
373 copied += sizeof(unsigned long);
375 return 0;
376 default:
377 /* Removing high order bit from addr (only for 31 bit). */
378 addr &= PSW_ADDR_INSN;
379 return ptrace_request(child, request, addr, data);
383 #ifdef CONFIG_COMPAT
385 * Now the fun part starts... a 31 bit program running in the
386 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
387 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
388 * to handle, the difference to the 64 bit versions of the requests
389 * is that the access is done in multiples of 4 byte instead of
390 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
391 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
392 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
393 * is a 31 bit program too, the content of struct user can be
394 * emulated. A 31 bit program peeking into the struct user of
395 * a 64 bit program is a no-no.
399 * Same as peek_user but for a 31 bit program.
401 static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
403 struct user32 *dummy32 = NULL;
404 per_struct32 *dummy_per32 = NULL;
405 addr_t offset;
406 __u32 tmp;
408 if (addr < (addr_t) &dummy32->regs.acrs) {
410 * psw and gprs are stored on the stack
412 if (addr == (addr_t) &dummy32->regs.psw.mask) {
413 /* Fake a 31 bit psw mask. */
414 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
415 tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
416 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
417 /* Fake a 31 bit psw address. */
418 tmp = (__u32) task_pt_regs(child)->psw.addr |
419 PSW32_ADDR_AMODE31;
420 } else {
421 /* gpr 0-15 */
422 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
423 addr*2 + 4);
425 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
427 * access registers are stored in the thread structure
429 offset = addr - (addr_t) &dummy32->regs.acrs;
430 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
432 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
434 * orig_gpr2 is stored on the kernel stack
436 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
438 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
440 * prevent reads of padding hole between
441 * orig_gpr2 and fp_regs on s390.
443 tmp = 0;
445 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
447 * floating point regs. are stored in the thread structure
449 offset = addr - (addr_t) &dummy32->regs.fp_regs;
450 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
452 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
454 * per_info is found in the thread structure
456 offset = addr - (addr_t) &dummy32->regs.per_info;
457 /* This is magic. See per_struct and per_struct32. */
458 if ((offset >= (addr_t) &dummy_per32->control_regs &&
459 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
460 (offset >= (addr_t) &dummy_per32->starting_addr &&
461 offset <= (addr_t) &dummy_per32->ending_addr) ||
462 offset == (addr_t) &dummy_per32->lowcore.words.address)
463 offset = offset*2 + 4;
464 else
465 offset = offset*2;
466 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
468 } else
469 tmp = 0;
471 return tmp;
474 static int peek_user_compat(struct task_struct *child,
475 addr_t addr, addr_t data)
477 __u32 tmp;
479 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
480 return -EIO;
482 tmp = __peek_user_compat(child, addr);
483 return put_user(tmp, (__u32 __user *) data);
487 * Same as poke_user but for a 31 bit program.
489 static int __poke_user_compat(struct task_struct *child,
490 addr_t addr, addr_t data)
492 struct user32 *dummy32 = NULL;
493 per_struct32 *dummy_per32 = NULL;
494 __u32 tmp = (__u32) data;
495 addr_t offset;
497 if (addr < (addr_t) &dummy32->regs.acrs) {
499 * psw, gprs, acrs and orig_gpr2 are stored on the stack
501 if (addr == (addr_t) &dummy32->regs.psw.mask) {
502 /* Build a 64 bit psw mask from 31 bit mask. */
503 if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
504 /* Invalid psw mask. */
505 return -EINVAL;
506 task_pt_regs(child)->psw.mask =
507 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
508 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
509 /* Build a 64 bit psw address from 31 bit address. */
510 task_pt_regs(child)->psw.addr =
511 (__u64) tmp & PSW32_ADDR_INSN;
512 } else {
513 /* gpr 0-15 */
514 *(__u32*)((addr_t) &task_pt_regs(child)->psw
515 + addr*2 + 4) = tmp;
517 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
519 * access registers are stored in the thread structure
521 offset = addr - (addr_t) &dummy32->regs.acrs;
522 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
524 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
526 * orig_gpr2 is stored on the kernel stack
528 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
530 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
532 * prevent writess of padding hole between
533 * orig_gpr2 and fp_regs on s390.
535 return 0;
537 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
539 * floating point regs. are stored in the thread structure
541 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
542 (tmp & ~FPC_VALID_MASK) != 0)
543 /* Invalid floating point control. */
544 return -EINVAL;
545 offset = addr - (addr_t) &dummy32->regs.fp_regs;
546 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
548 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
550 * per_info is found in the thread structure.
552 offset = addr - (addr_t) &dummy32->regs.per_info;
554 * This is magic. See per_struct and per_struct32.
555 * By incident the offsets in per_struct are exactly
556 * twice the offsets in per_struct32 for all fields.
557 * The 8 byte fields need special handling though,
558 * because the second half (bytes 4-7) is needed and
559 * not the first half.
561 if ((offset >= (addr_t) &dummy_per32->control_regs &&
562 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
563 (offset >= (addr_t) &dummy_per32->starting_addr &&
564 offset <= (addr_t) &dummy_per32->ending_addr) ||
565 offset == (addr_t) &dummy_per32->lowcore.words.address)
566 offset = offset*2 + 4;
567 else
568 offset = offset*2;
569 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
573 FixPerRegisters(child);
574 return 0;
577 static int poke_user_compat(struct task_struct *child,
578 addr_t addr, addr_t data)
580 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user32) - 3)
581 return -EIO;
583 return __poke_user_compat(child, addr, data);
586 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
587 compat_ulong_t caddr, compat_ulong_t cdata)
589 unsigned long addr = caddr;
590 unsigned long data = cdata;
591 ptrace_area_emu31 parea;
592 int copied, ret;
594 switch (request) {
595 case PTRACE_PEEKUSR:
596 /* read the word at location addr in the USER area. */
597 return peek_user_compat(child, addr, data);
599 case PTRACE_POKEUSR:
600 /* write the word at location addr in the USER area */
601 return poke_user_compat(child, addr, data);
603 case PTRACE_PEEKUSR_AREA:
604 case PTRACE_POKEUSR_AREA:
605 if (copy_from_user(&parea, (void __force __user *) addr,
606 sizeof(parea)))
607 return -EFAULT;
608 addr = parea.kernel_addr;
609 data = parea.process_addr;
610 copied = 0;
611 while (copied < parea.len) {
612 if (request == PTRACE_PEEKUSR_AREA)
613 ret = peek_user_compat(child, addr, data);
614 else {
615 __u32 utmp;
616 if (get_user(utmp,
617 (__u32 __force __user *) data))
618 return -EFAULT;
619 ret = poke_user_compat(child, addr, utmp);
621 if (ret)
622 return ret;
623 addr += sizeof(unsigned int);
624 data += sizeof(unsigned int);
625 copied += sizeof(unsigned int);
627 return 0;
629 return compat_ptrace_request(child, request, addr, data);
631 #endif
633 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
635 long ret = 0;
637 /* Do the secure computing check first. */
638 secure_computing(regs->gprs[2]);
641 * The sysc_tracesys code in entry.S stored the system
642 * call number to gprs[2].
644 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
645 (tracehook_report_syscall_entry(regs) ||
646 regs->gprs[2] >= NR_syscalls)) {
648 * Tracing decided this syscall should not happen or the
649 * debugger stored an invalid system call number. Skip
650 * the system call and the system call restart handling.
652 regs->svcnr = 0;
653 ret = -1;
656 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
657 trace_sys_enter(regs, regs->gprs[2]);
659 if (unlikely(current->audit_context))
660 audit_syscall_entry(is_compat_task() ?
661 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
662 regs->gprs[2], regs->orig_gpr2,
663 regs->gprs[3], regs->gprs[4],
664 regs->gprs[5]);
665 return ret ?: regs->gprs[2];
668 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
670 if (unlikely(current->audit_context))
671 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
672 regs->gprs[2]);
674 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
675 trace_sys_exit(regs, regs->gprs[2]);
677 if (test_thread_flag(TIF_SYSCALL_TRACE))
678 tracehook_report_syscall_exit(regs, 0);
682 * user_regset definitions.
685 static int s390_regs_get(struct task_struct *target,
686 const struct user_regset *regset,
687 unsigned int pos, unsigned int count,
688 void *kbuf, void __user *ubuf)
690 if (target == current)
691 save_access_regs(target->thread.acrs);
693 if (kbuf) {
694 unsigned long *k = kbuf;
695 while (count > 0) {
696 *k++ = __peek_user(target, pos);
697 count -= sizeof(*k);
698 pos += sizeof(*k);
700 } else {
701 unsigned long __user *u = ubuf;
702 while (count > 0) {
703 if (__put_user(__peek_user(target, pos), u++))
704 return -EFAULT;
705 count -= sizeof(*u);
706 pos += sizeof(*u);
709 return 0;
712 static int s390_regs_set(struct task_struct *target,
713 const struct user_regset *regset,
714 unsigned int pos, unsigned int count,
715 const void *kbuf, const void __user *ubuf)
717 int rc = 0;
719 if (target == current)
720 save_access_regs(target->thread.acrs);
722 if (kbuf) {
723 const unsigned long *k = kbuf;
724 while (count > 0 && !rc) {
725 rc = __poke_user(target, pos, *k++);
726 count -= sizeof(*k);
727 pos += sizeof(*k);
729 } else {
730 const unsigned long __user *u = ubuf;
731 while (count > 0 && !rc) {
732 unsigned long word;
733 rc = __get_user(word, u++);
734 if (rc)
735 break;
736 rc = __poke_user(target, pos, word);
737 count -= sizeof(*u);
738 pos += sizeof(*u);
742 if (rc == 0 && target == current)
743 restore_access_regs(target->thread.acrs);
745 return rc;
748 static int s390_fpregs_get(struct task_struct *target,
749 const struct user_regset *regset, unsigned int pos,
750 unsigned int count, void *kbuf, void __user *ubuf)
752 if (target == current)
753 save_fp_regs(&target->thread.fp_regs);
755 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
756 &target->thread.fp_regs, 0, -1);
759 static int s390_fpregs_set(struct task_struct *target,
760 const struct user_regset *regset, unsigned int pos,
761 unsigned int count, const void *kbuf,
762 const void __user *ubuf)
764 int rc = 0;
766 if (target == current)
767 save_fp_regs(&target->thread.fp_regs);
769 /* If setting FPC, must validate it first. */
770 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
771 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
772 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
773 0, offsetof(s390_fp_regs, fprs));
774 if (rc)
775 return rc;
776 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
777 return -EINVAL;
778 target->thread.fp_regs.fpc = fpc[0];
781 if (rc == 0 && count > 0)
782 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
783 target->thread.fp_regs.fprs,
784 offsetof(s390_fp_regs, fprs), -1);
786 if (rc == 0 && target == current)
787 restore_fp_regs(&target->thread.fp_regs);
789 return rc;
792 static const struct user_regset s390_regsets[] = {
793 [REGSET_GENERAL] = {
794 .core_note_type = NT_PRSTATUS,
795 .n = sizeof(s390_regs) / sizeof(long),
796 .size = sizeof(long),
797 .align = sizeof(long),
798 .get = s390_regs_get,
799 .set = s390_regs_set,
801 [REGSET_FP] = {
802 .core_note_type = NT_PRFPREG,
803 .n = sizeof(s390_fp_regs) / sizeof(long),
804 .size = sizeof(long),
805 .align = sizeof(long),
806 .get = s390_fpregs_get,
807 .set = s390_fpregs_set,
811 static const struct user_regset_view user_s390_view = {
812 .name = UTS_MACHINE,
813 .e_machine = EM_S390,
814 .regsets = s390_regsets,
815 .n = ARRAY_SIZE(s390_regsets)
818 #ifdef CONFIG_COMPAT
819 static int s390_compat_regs_get(struct task_struct *target,
820 const struct user_regset *regset,
821 unsigned int pos, unsigned int count,
822 void *kbuf, void __user *ubuf)
824 if (target == current)
825 save_access_regs(target->thread.acrs);
827 if (kbuf) {
828 compat_ulong_t *k = kbuf;
829 while (count > 0) {
830 *k++ = __peek_user_compat(target, pos);
831 count -= sizeof(*k);
832 pos += sizeof(*k);
834 } else {
835 compat_ulong_t __user *u = ubuf;
836 while (count > 0) {
837 if (__put_user(__peek_user_compat(target, pos), u++))
838 return -EFAULT;
839 count -= sizeof(*u);
840 pos += sizeof(*u);
843 return 0;
846 static int s390_compat_regs_set(struct task_struct *target,
847 const struct user_regset *regset,
848 unsigned int pos, unsigned int count,
849 const void *kbuf, const void __user *ubuf)
851 int rc = 0;
853 if (target == current)
854 save_access_regs(target->thread.acrs);
856 if (kbuf) {
857 const compat_ulong_t *k = kbuf;
858 while (count > 0 && !rc) {
859 rc = __poke_user_compat(target, pos, *k++);
860 count -= sizeof(*k);
861 pos += sizeof(*k);
863 } else {
864 const compat_ulong_t __user *u = ubuf;
865 while (count > 0 && !rc) {
866 compat_ulong_t word;
867 rc = __get_user(word, u++);
868 if (rc)
869 break;
870 rc = __poke_user_compat(target, pos, word);
871 count -= sizeof(*u);
872 pos += sizeof(*u);
876 if (rc == 0 && target == current)
877 restore_access_regs(target->thread.acrs);
879 return rc;
882 static int s390_compat_regs_high_get(struct task_struct *target,
883 const struct user_regset *regset,
884 unsigned int pos, unsigned int count,
885 void *kbuf, void __user *ubuf)
887 compat_ulong_t *gprs_high;
889 gprs_high = (compat_ulong_t *)
890 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
891 if (kbuf) {
892 compat_ulong_t *k = kbuf;
893 while (count > 0) {
894 *k++ = *gprs_high;
895 gprs_high += 2;
896 count -= sizeof(*k);
898 } else {
899 compat_ulong_t __user *u = ubuf;
900 while (count > 0) {
901 if (__put_user(*gprs_high, u++))
902 return -EFAULT;
903 gprs_high += 2;
904 count -= sizeof(*u);
907 return 0;
910 static int s390_compat_regs_high_set(struct task_struct *target,
911 const struct user_regset *regset,
912 unsigned int pos, unsigned int count,
913 const void *kbuf, const void __user *ubuf)
915 compat_ulong_t *gprs_high;
916 int rc = 0;
918 gprs_high = (compat_ulong_t *)
919 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
920 if (kbuf) {
921 const compat_ulong_t *k = kbuf;
922 while (count > 0) {
923 *gprs_high = *k++;
924 *gprs_high += 2;
925 count -= sizeof(*k);
927 } else {
928 const compat_ulong_t __user *u = ubuf;
929 while (count > 0 && !rc) {
930 unsigned long word;
931 rc = __get_user(word, u++);
932 if (rc)
933 break;
934 *gprs_high = word;
935 *gprs_high += 2;
936 count -= sizeof(*u);
940 return rc;
943 static const struct user_regset s390_compat_regsets[] = {
944 [REGSET_GENERAL] = {
945 .core_note_type = NT_PRSTATUS,
946 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
947 .size = sizeof(compat_long_t),
948 .align = sizeof(compat_long_t),
949 .get = s390_compat_regs_get,
950 .set = s390_compat_regs_set,
952 [REGSET_FP] = {
953 .core_note_type = NT_PRFPREG,
954 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
955 .size = sizeof(compat_long_t),
956 .align = sizeof(compat_long_t),
957 .get = s390_fpregs_get,
958 .set = s390_fpregs_set,
960 [REGSET_GENERAL_EXTENDED] = {
961 .core_note_type = NT_PRXSTATUS,
962 .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
963 .size = sizeof(compat_long_t),
964 .align = sizeof(compat_long_t),
965 .get = s390_compat_regs_high_get,
966 .set = s390_compat_regs_high_set,
970 static const struct user_regset_view user_s390_compat_view = {
971 .name = "s390",
972 .e_machine = EM_S390,
973 .regsets = s390_compat_regsets,
974 .n = ARRAY_SIZE(s390_compat_regsets)
976 #endif
978 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
980 #ifdef CONFIG_COMPAT
981 if (test_tsk_thread_flag(task, TIF_31BIT))
982 return &user_s390_compat_view;
983 #endif
984 return &user_s390_view;