x86/amd-iommu: Add function to complete a tlb flush
[linux/fpc-iii.git] / arch / s390 / kernel / ptrace.c
blob653c6a1787404480f532d7f033223540e2fcc616
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;
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;
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 ret = regs->gprs[2];
645 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
646 (tracehook_report_syscall_entry(regs) ||
647 regs->gprs[2] >= NR_syscalls)) {
649 * Tracing decided this syscall should not happen or the
650 * debugger stored an invalid system call number. Skip
651 * the system call and the system call restart handling.
653 regs->svcnr = 0;
654 ret = -1;
657 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
658 trace_sys_enter(regs, regs->gprs[2]);
660 if (unlikely(current->audit_context))
661 audit_syscall_entry(is_compat_task() ?
662 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
663 regs->gprs[2], regs->orig_gpr2,
664 regs->gprs[3], regs->gprs[4],
665 regs->gprs[5]);
666 return ret;
669 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
671 if (unlikely(current->audit_context))
672 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
673 regs->gprs[2]);
675 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
676 trace_sys_exit(regs, regs->gprs[2]);
678 if (test_thread_flag(TIF_SYSCALL_TRACE))
679 tracehook_report_syscall_exit(regs, 0);
683 * user_regset definitions.
686 static int s390_regs_get(struct task_struct *target,
687 const struct user_regset *regset,
688 unsigned int pos, unsigned int count,
689 void *kbuf, void __user *ubuf)
691 if (target == current)
692 save_access_regs(target->thread.acrs);
694 if (kbuf) {
695 unsigned long *k = kbuf;
696 while (count > 0) {
697 *k++ = __peek_user(target, pos);
698 count -= sizeof(*k);
699 pos += sizeof(*k);
701 } else {
702 unsigned long __user *u = ubuf;
703 while (count > 0) {
704 if (__put_user(__peek_user(target, pos), u++))
705 return -EFAULT;
706 count -= sizeof(*u);
707 pos += sizeof(*u);
710 return 0;
713 static int s390_regs_set(struct task_struct *target,
714 const struct user_regset *regset,
715 unsigned int pos, unsigned int count,
716 const void *kbuf, const void __user *ubuf)
718 int rc = 0;
720 if (target == current)
721 save_access_regs(target->thread.acrs);
723 if (kbuf) {
724 const unsigned long *k = kbuf;
725 while (count > 0 && !rc) {
726 rc = __poke_user(target, pos, *k++);
727 count -= sizeof(*k);
728 pos += sizeof(*k);
730 } else {
731 const unsigned long __user *u = ubuf;
732 while (count > 0 && !rc) {
733 unsigned long word;
734 rc = __get_user(word, u++);
735 if (rc)
736 break;
737 rc = __poke_user(target, pos, word);
738 count -= sizeof(*u);
739 pos += sizeof(*u);
743 if (rc == 0 && target == current)
744 restore_access_regs(target->thread.acrs);
746 return rc;
749 static int s390_fpregs_get(struct task_struct *target,
750 const struct user_regset *regset, unsigned int pos,
751 unsigned int count, void *kbuf, void __user *ubuf)
753 if (target == current)
754 save_fp_regs(&target->thread.fp_regs);
756 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
757 &target->thread.fp_regs, 0, -1);
760 static int s390_fpregs_set(struct task_struct *target,
761 const struct user_regset *regset, unsigned int pos,
762 unsigned int count, const void *kbuf,
763 const void __user *ubuf)
765 int rc = 0;
767 if (target == current)
768 save_fp_regs(&target->thread.fp_regs);
770 /* If setting FPC, must validate it first. */
771 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
772 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
773 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
774 0, offsetof(s390_fp_regs, fprs));
775 if (rc)
776 return rc;
777 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
778 return -EINVAL;
779 target->thread.fp_regs.fpc = fpc[0];
782 if (rc == 0 && count > 0)
783 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
784 target->thread.fp_regs.fprs,
785 offsetof(s390_fp_regs, fprs), -1);
787 if (rc == 0 && target == current)
788 restore_fp_regs(&target->thread.fp_regs);
790 return rc;
793 static const struct user_regset s390_regsets[] = {
794 [REGSET_GENERAL] = {
795 .core_note_type = NT_PRSTATUS,
796 .n = sizeof(s390_regs) / sizeof(long),
797 .size = sizeof(long),
798 .align = sizeof(long),
799 .get = s390_regs_get,
800 .set = s390_regs_set,
802 [REGSET_FP] = {
803 .core_note_type = NT_PRFPREG,
804 .n = sizeof(s390_fp_regs) / sizeof(long),
805 .size = sizeof(long),
806 .align = sizeof(long),
807 .get = s390_fpregs_get,
808 .set = s390_fpregs_set,
812 static const struct user_regset_view user_s390_view = {
813 .name = UTS_MACHINE,
814 .e_machine = EM_S390,
815 .regsets = s390_regsets,
816 .n = ARRAY_SIZE(s390_regsets)
819 #ifdef CONFIG_COMPAT
820 static int s390_compat_regs_get(struct task_struct *target,
821 const struct user_regset *regset,
822 unsigned int pos, unsigned int count,
823 void *kbuf, void __user *ubuf)
825 if (target == current)
826 save_access_regs(target->thread.acrs);
828 if (kbuf) {
829 compat_ulong_t *k = kbuf;
830 while (count > 0) {
831 *k++ = __peek_user_compat(target, pos);
832 count -= sizeof(*k);
833 pos += sizeof(*k);
835 } else {
836 compat_ulong_t __user *u = ubuf;
837 while (count > 0) {
838 if (__put_user(__peek_user_compat(target, pos), u++))
839 return -EFAULT;
840 count -= sizeof(*u);
841 pos += sizeof(*u);
844 return 0;
847 static int s390_compat_regs_set(struct task_struct *target,
848 const struct user_regset *regset,
849 unsigned int pos, unsigned int count,
850 const void *kbuf, const void __user *ubuf)
852 int rc = 0;
854 if (target == current)
855 save_access_regs(target->thread.acrs);
857 if (kbuf) {
858 const compat_ulong_t *k = kbuf;
859 while (count > 0 && !rc) {
860 rc = __poke_user_compat(target, pos, *k++);
861 count -= sizeof(*k);
862 pos += sizeof(*k);
864 } else {
865 const compat_ulong_t __user *u = ubuf;
866 while (count > 0 && !rc) {
867 compat_ulong_t word;
868 rc = __get_user(word, u++);
869 if (rc)
870 break;
871 rc = __poke_user_compat(target, pos, word);
872 count -= sizeof(*u);
873 pos += sizeof(*u);
877 if (rc == 0 && target == current)
878 restore_access_regs(target->thread.acrs);
880 return rc;
883 static int s390_compat_regs_high_get(struct task_struct *target,
884 const struct user_regset *regset,
885 unsigned int pos, unsigned int count,
886 void *kbuf, void __user *ubuf)
888 compat_ulong_t *gprs_high;
890 gprs_high = (compat_ulong_t *)
891 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
892 if (kbuf) {
893 compat_ulong_t *k = kbuf;
894 while (count > 0) {
895 *k++ = *gprs_high;
896 gprs_high += 2;
897 count -= sizeof(*k);
899 } else {
900 compat_ulong_t __user *u = ubuf;
901 while (count > 0) {
902 if (__put_user(*gprs_high, u++))
903 return -EFAULT;
904 gprs_high += 2;
905 count -= sizeof(*u);
908 return 0;
911 static int s390_compat_regs_high_set(struct task_struct *target,
912 const struct user_regset *regset,
913 unsigned int pos, unsigned int count,
914 const void *kbuf, const void __user *ubuf)
916 compat_ulong_t *gprs_high;
917 int rc = 0;
919 gprs_high = (compat_ulong_t *)
920 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
921 if (kbuf) {
922 const compat_ulong_t *k = kbuf;
923 while (count > 0) {
924 *gprs_high = *k++;
925 *gprs_high += 2;
926 count -= sizeof(*k);
928 } else {
929 const compat_ulong_t __user *u = ubuf;
930 while (count > 0 && !rc) {
931 unsigned long word;
932 rc = __get_user(word, u++);
933 if (rc)
934 break;
935 *gprs_high = word;
936 *gprs_high += 2;
937 count -= sizeof(*u);
941 return rc;
944 static const struct user_regset s390_compat_regsets[] = {
945 [REGSET_GENERAL] = {
946 .core_note_type = NT_PRSTATUS,
947 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
948 .size = sizeof(compat_long_t),
949 .align = sizeof(compat_long_t),
950 .get = s390_compat_regs_get,
951 .set = s390_compat_regs_set,
953 [REGSET_FP] = {
954 .core_note_type = NT_PRFPREG,
955 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
956 .size = sizeof(compat_long_t),
957 .align = sizeof(compat_long_t),
958 .get = s390_fpregs_get,
959 .set = s390_fpregs_set,
961 [REGSET_GENERAL_EXTENDED] = {
962 .core_note_type = NT_PRXSTATUS,
963 .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
964 .size = sizeof(compat_long_t),
965 .align = sizeof(compat_long_t),
966 .get = s390_compat_regs_high_get,
967 .set = s390_compat_regs_high_set,
971 static const struct user_regset_view user_s390_compat_view = {
972 .name = "s390",
973 .e_machine = EM_S390,
974 .regsets = s390_compat_regsets,
975 .n = ARRAY_SIZE(s390_compat_regsets)
977 #endif
979 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
981 #ifdef CONFIG_COMPAT
982 if (test_tsk_thread_flag(task, TIF_31BIT))
983 return &user_s390_compat_view;
984 #endif
985 return &user_s390_view;