Linux 2.6.28-rc5
[cris-mirror.git] / arch / s390 / kernel / ptrace.c
blob1f31be1ecc4bd57bd3af99ef004f00df4f24987b
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/smp_lock.h>
30 #include <linux/errno.h>
31 #include <linux/ptrace.h>
32 #include <linux/user.h>
33 #include <linux/security.h>
34 #include <linux/audit.h>
35 #include <linux/signal.h>
36 #include <linux/elf.h>
37 #include <linux/regset.h>
38 #include <linux/tracehook.h>
40 #include <asm/segment.h>
41 #include <asm/page.h>
42 #include <asm/pgtable.h>
43 #include <asm/pgalloc.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
46 #include <asm/unistd.h>
47 #include "entry.h"
49 #ifdef CONFIG_COMPAT
50 #include "compat_ptrace.h"
51 #endif
53 enum s390_regset {
54 REGSET_GENERAL,
55 REGSET_FP,
58 static void
59 FixPerRegisters(struct task_struct *task)
61 struct pt_regs *regs;
62 per_struct *per_info;
64 regs = task_pt_regs(task);
65 per_info = (per_struct *) &task->thread.per_info;
66 per_info->control_regs.bits.em_instruction_fetch =
67 per_info->single_step | per_info->instruction_fetch;
69 if (per_info->single_step) {
70 per_info->control_regs.bits.starting_addr = 0;
71 #ifdef CONFIG_COMPAT
72 if (test_thread_flag(TIF_31BIT))
73 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
74 else
75 #endif
76 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
77 } else {
78 per_info->control_regs.bits.starting_addr =
79 per_info->starting_addr;
80 per_info->control_regs.bits.ending_addr =
81 per_info->ending_addr;
84 * if any of the control reg tracing bits are on
85 * we switch on per in the psw
87 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
88 regs->psw.mask |= PSW_MASK_PER;
89 else
90 regs->psw.mask &= ~PSW_MASK_PER;
92 if (per_info->control_regs.bits.em_storage_alteration)
93 per_info->control_regs.bits.storage_alt_space_ctl = 1;
94 else
95 per_info->control_regs.bits.storage_alt_space_ctl = 0;
98 void user_enable_single_step(struct task_struct *task)
100 task->thread.per_info.single_step = 1;
101 FixPerRegisters(task);
104 void user_disable_single_step(struct task_struct *task)
106 task->thread.per_info.single_step = 0;
107 FixPerRegisters(task);
111 * Called by kernel/ptrace.c when detaching..
113 * Make sure single step bits etc are not set.
115 void
116 ptrace_disable(struct task_struct *child)
118 /* make sure the single step bit is not set. */
119 user_disable_single_step(child);
122 #ifndef CONFIG_64BIT
123 # define __ADDR_MASK 3
124 #else
125 # define __ADDR_MASK 7
126 #endif
129 * Read the word at offset addr from the user area of a process. The
130 * trouble here is that the information is littered over different
131 * locations. The process registers are found on the kernel stack,
132 * the floating point stuff and the trace settings are stored in
133 * the task structure. In addition the different structures in
134 * struct user contain pad bytes that should be read as zeroes.
135 * Lovely...
137 static unsigned long __peek_user(struct task_struct *child, addr_t addr)
139 struct user *dummy = NULL;
140 addr_t offset, tmp;
142 if (addr < (addr_t) &dummy->regs.acrs) {
144 * psw and gprs are stored on the stack
146 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
147 if (addr == (addr_t) &dummy->regs.psw.mask)
148 /* Remove per bit from user psw. */
149 tmp &= ~PSW_MASK_PER;
151 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
153 * access registers are stored in the thread structure
155 offset = addr - (addr_t) &dummy->regs.acrs;
156 #ifdef CONFIG_64BIT
158 * Very special case: old & broken 64 bit gdb reading
159 * from acrs[15]. Result is a 64 bit value. Read the
160 * 32 bit acrs[15] value and shift it by 32. Sick...
162 if (addr == (addr_t) &dummy->regs.acrs[15])
163 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
164 else
165 #endif
166 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
168 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
170 * orig_gpr2 is stored on the kernel stack
172 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
174 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
176 * prevent reads of padding hole between
177 * orig_gpr2 and fp_regs on s390.
179 tmp = 0;
181 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
183 * floating point regs. are stored in the thread structure
185 offset = addr - (addr_t) &dummy->regs.fp_regs;
186 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
187 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
188 tmp &= (unsigned long) FPC_VALID_MASK
189 << (BITS_PER_LONG - 32);
191 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
193 * per_info is found in the thread structure
195 offset = addr - (addr_t) &dummy->regs.per_info;
196 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
198 } else
199 tmp = 0;
201 return tmp;
204 static int
205 peek_user(struct task_struct *child, addr_t addr, addr_t data)
207 struct user *dummy = NULL;
208 addr_t tmp, mask;
211 * Stupid gdb peeks/pokes the access registers in 64 bit with
212 * an alignment of 4. Programmers from hell...
214 mask = __ADDR_MASK;
215 #ifdef CONFIG_64BIT
216 if (addr >= (addr_t) &dummy->regs.acrs &&
217 addr < (addr_t) &dummy->regs.orig_gpr2)
218 mask = 3;
219 #endif
220 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
221 return -EIO;
223 tmp = __peek_user(child, addr);
224 return put_user(tmp, (addr_t __user *) data);
228 * Write a word to the user area of a process at location addr. This
229 * operation does have an additional problem compared to peek_user.
230 * Stores to the program status word and on the floating point
231 * control register needs to get checked for validity.
233 static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
235 struct user *dummy = NULL;
236 addr_t offset;
238 if (addr < (addr_t) &dummy->regs.acrs) {
240 * psw and gprs are stored on the stack
242 if (addr == (addr_t) &dummy->regs.psw.mask &&
243 #ifdef CONFIG_COMPAT
244 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
245 #endif
246 data != PSW_MASK_MERGE(psw_user_bits, data))
247 /* Invalid psw mask. */
248 return -EINVAL;
249 #ifndef CONFIG_64BIT
250 if (addr == (addr_t) &dummy->regs.psw.addr)
251 /* I'd like to reject addresses without the
252 high order bit but older gdb's rely on it */
253 data |= PSW_ADDR_AMODE;
254 #endif
255 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
257 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
259 * access registers are stored in the thread structure
261 offset = addr - (addr_t) &dummy->regs.acrs;
262 #ifdef CONFIG_64BIT
264 * Very special case: old & broken 64 bit gdb writing
265 * to acrs[15] with a 64 bit value. Ignore the lower
266 * half of the value and write the upper 32 bit to
267 * acrs[15]. Sick...
269 if (addr == (addr_t) &dummy->regs.acrs[15])
270 child->thread.acrs[15] = (unsigned int) (data >> 32);
271 else
272 #endif
273 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
275 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
277 * orig_gpr2 is stored on the kernel stack
279 task_pt_regs(child)->orig_gpr2 = data;
281 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
283 * prevent writes of padding hole between
284 * orig_gpr2 and fp_regs on s390.
286 return 0;
288 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
290 * floating point regs. are stored in the thread structure
292 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
293 (data & ~((unsigned long) FPC_VALID_MASK
294 << (BITS_PER_LONG - 32))) != 0)
295 return -EINVAL;
296 offset = addr - (addr_t) &dummy->regs.fp_regs;
297 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
299 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
301 * per_info is found in the thread structure
303 offset = addr - (addr_t) &dummy->regs.per_info;
304 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
308 FixPerRegisters(child);
309 return 0;
312 static int
313 poke_user(struct task_struct *child, addr_t addr, addr_t data)
315 struct user *dummy = NULL;
316 addr_t mask;
319 * Stupid gdb peeks/pokes the access registers in 64 bit with
320 * an alignment of 4. Programmers from hell indeed...
322 mask = __ADDR_MASK;
323 #ifdef CONFIG_64BIT
324 if (addr >= (addr_t) &dummy->regs.acrs &&
325 addr < (addr_t) &dummy->regs.orig_gpr2)
326 mask = 3;
327 #endif
328 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
329 return -EIO;
331 return __poke_user(child, addr, data);
334 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
336 ptrace_area parea;
337 int copied, ret;
339 switch (request) {
340 case PTRACE_PEEKTEXT:
341 case PTRACE_PEEKDATA:
342 /* Remove high order bit from address (only for 31 bit). */
343 addr &= PSW_ADDR_INSN;
344 /* read word at location addr. */
345 return generic_ptrace_peekdata(child, addr, data);
347 case PTRACE_PEEKUSR:
348 /* read the word at location addr in the USER area. */
349 return peek_user(child, addr, data);
351 case PTRACE_POKETEXT:
352 case PTRACE_POKEDATA:
353 /* Remove high order bit from address (only for 31 bit). */
354 addr &= PSW_ADDR_INSN;
355 /* write the word at location addr. */
356 return generic_ptrace_pokedata(child, addr, data);
358 case PTRACE_POKEUSR:
359 /* write the word at location addr in the USER area */
360 return poke_user(child, addr, data);
362 case PTRACE_PEEKUSR_AREA:
363 case PTRACE_POKEUSR_AREA:
364 if (copy_from_user(&parea, (void __force __user *) addr,
365 sizeof(parea)))
366 return -EFAULT;
367 addr = parea.kernel_addr;
368 data = parea.process_addr;
369 copied = 0;
370 while (copied < parea.len) {
371 if (request == PTRACE_PEEKUSR_AREA)
372 ret = peek_user(child, addr, data);
373 else {
374 addr_t utmp;
375 if (get_user(utmp,
376 (addr_t __force __user *) data))
377 return -EFAULT;
378 ret = poke_user(child, addr, utmp);
380 if (ret)
381 return ret;
382 addr += sizeof(unsigned long);
383 data += sizeof(unsigned long);
384 copied += sizeof(unsigned long);
386 return 0;
388 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 (!test_thread_flag(TIF_31BIT) ||
488 (addr & 3) || addr > sizeof(struct user) - 3)
489 return -EIO;
491 tmp = __peek_user_compat(child, addr);
492 return put_user(tmp, (__u32 __user *) data);
496 * Same as poke_user but for a 31 bit program.
498 static int __poke_user_compat(struct task_struct *child,
499 addr_t addr, addr_t data)
501 struct user32 *dummy32 = NULL;
502 per_struct32 *dummy_per32 = NULL;
503 __u32 tmp = (__u32) data;
504 addr_t offset;
506 if (addr < (addr_t) &dummy32->regs.acrs) {
508 * psw, gprs, acrs and orig_gpr2 are stored on the stack
510 if (addr == (addr_t) &dummy32->regs.psw.mask) {
511 /* Build a 64 bit psw mask from 31 bit mask. */
512 if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
513 /* Invalid psw mask. */
514 return -EINVAL;
515 task_pt_regs(child)->psw.mask =
516 PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
517 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
518 /* Build a 64 bit psw address from 31 bit address. */
519 task_pt_regs(child)->psw.addr =
520 (__u64) tmp & PSW32_ADDR_INSN;
521 } else {
522 /* gpr 0-15 */
523 *(__u32*)((addr_t) &task_pt_regs(child)->psw
524 + addr*2 + 4) = tmp;
526 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
528 * access registers are stored in the thread structure
530 offset = addr - (addr_t) &dummy32->regs.acrs;
531 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
533 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
535 * orig_gpr2 is stored on the kernel stack
537 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
539 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
541 * prevent writess of padding hole between
542 * orig_gpr2 and fp_regs on s390.
544 return 0;
546 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
548 * floating point regs. are stored in the thread structure
550 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
551 (tmp & ~FPC_VALID_MASK) != 0)
552 /* Invalid floating point control. */
553 return -EINVAL;
554 offset = addr - (addr_t) &dummy32->regs.fp_regs;
555 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
557 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
559 * per_info is found in the thread structure.
561 offset = addr - (addr_t) &dummy32->regs.per_info;
563 * This is magic. See per_struct and per_struct32.
564 * By incident the offsets in per_struct are exactly
565 * twice the offsets in per_struct32 for all fields.
566 * The 8 byte fields need special handling though,
567 * because the second half (bytes 4-7) is needed and
568 * not the first half.
570 if ((offset >= (addr_t) &dummy_per32->control_regs &&
571 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
572 (offset >= (addr_t) &dummy_per32->starting_addr &&
573 offset <= (addr_t) &dummy_per32->ending_addr) ||
574 offset == (addr_t) &dummy_per32->lowcore.words.address)
575 offset = offset*2 + 4;
576 else
577 offset = offset*2;
578 *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
582 FixPerRegisters(child);
583 return 0;
586 static int poke_user_compat(struct task_struct *child,
587 addr_t addr, addr_t data)
589 if (!test_thread_flag(TIF_31BIT) ||
590 (addr & 3) || addr > sizeof(struct user32) - 3)
591 return -EIO;
593 return __poke_user_compat(child, addr, data);
596 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
597 compat_ulong_t caddr, compat_ulong_t cdata)
599 unsigned long addr = caddr;
600 unsigned long data = cdata;
601 ptrace_area_emu31 parea;
602 int copied, ret;
604 switch (request) {
605 case PTRACE_PEEKUSR:
606 /* read the word at location addr in the USER area. */
607 return peek_user_compat(child, addr, data);
609 case PTRACE_POKEUSR:
610 /* write the word at location addr in the USER area */
611 return poke_user_compat(child, addr, data);
613 case PTRACE_PEEKUSR_AREA:
614 case PTRACE_POKEUSR_AREA:
615 if (copy_from_user(&parea, (void __force __user *) addr,
616 sizeof(parea)))
617 return -EFAULT;
618 addr = parea.kernel_addr;
619 data = parea.process_addr;
620 copied = 0;
621 while (copied < parea.len) {
622 if (request == PTRACE_PEEKUSR_AREA)
623 ret = peek_user_compat(child, addr, data);
624 else {
625 __u32 utmp;
626 if (get_user(utmp,
627 (__u32 __force __user *) data))
628 return -EFAULT;
629 ret = poke_user_compat(child, addr, utmp);
631 if (ret)
632 return ret;
633 addr += sizeof(unsigned int);
634 data += sizeof(unsigned int);
635 copied += sizeof(unsigned int);
637 return 0;
639 return compat_ptrace_request(child, request, addr, data);
641 #endif
643 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
645 long ret;
648 * The sysc_tracesys code in entry.S stored the system
649 * call number to gprs[2].
651 ret = regs->gprs[2];
652 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
653 (tracehook_report_syscall_entry(regs) ||
654 regs->gprs[2] >= NR_syscalls)) {
656 * Tracing decided this syscall should not happen or the
657 * debugger stored an invalid system call number. Skip
658 * the system call and the system call restart handling.
660 regs->trap = -1;
661 ret = -1;
664 if (unlikely(current->audit_context))
665 audit_syscall_entry(test_thread_flag(TIF_31BIT) ?
666 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
667 regs->gprs[2], regs->orig_gpr2,
668 regs->gprs[3], regs->gprs[4],
669 regs->gprs[5]);
670 return ret;
673 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
675 if (unlikely(current->audit_context))
676 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
677 regs->gprs[2]);
679 if (test_thread_flag(TIF_SYSCALL_TRACE))
680 tracehook_report_syscall_exit(regs, 0);
684 * user_regset definitions.
687 static int s390_regs_get(struct task_struct *target,
688 const struct user_regset *regset,
689 unsigned int pos, unsigned int count,
690 void *kbuf, void __user *ubuf)
692 if (target == current)
693 save_access_regs(target->thread.acrs);
695 if (kbuf) {
696 unsigned long *k = kbuf;
697 while (count > 0) {
698 *k++ = __peek_user(target, pos);
699 count -= sizeof(*k);
700 pos += sizeof(*k);
702 } else {
703 unsigned long __user *u = ubuf;
704 while (count > 0) {
705 if (__put_user(__peek_user(target, pos), u++))
706 return -EFAULT;
707 count -= sizeof(*u);
708 pos += sizeof(*u);
711 return 0;
714 static int s390_regs_set(struct task_struct *target,
715 const struct user_regset *regset,
716 unsigned int pos, unsigned int count,
717 const void *kbuf, const void __user *ubuf)
719 int rc = 0;
721 if (target == current)
722 save_access_regs(target->thread.acrs);
724 if (kbuf) {
725 const unsigned long *k = kbuf;
726 while (count > 0 && !rc) {
727 rc = __poke_user(target, pos, *k++);
728 count -= sizeof(*k);
729 pos += sizeof(*k);
731 } else {
732 const unsigned long __user *u = ubuf;
733 while (count > 0 && !rc) {
734 unsigned long word;
735 rc = __get_user(word, u++);
736 if (rc)
737 break;
738 rc = __poke_user(target, pos, word);
739 count -= sizeof(*u);
740 pos += sizeof(*u);
744 if (rc == 0 && target == current)
745 restore_access_regs(target->thread.acrs);
747 return rc;
750 static int s390_fpregs_get(struct task_struct *target,
751 const struct user_regset *regset, unsigned int pos,
752 unsigned int count, void *kbuf, void __user *ubuf)
754 if (target == current)
755 save_fp_regs(&target->thread.fp_regs);
757 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
758 &target->thread.fp_regs, 0, -1);
761 static int s390_fpregs_set(struct task_struct *target,
762 const struct user_regset *regset, unsigned int pos,
763 unsigned int count, const void *kbuf,
764 const void __user *ubuf)
766 int rc = 0;
768 if (target == current)
769 save_fp_regs(&target->thread.fp_regs);
771 /* If setting FPC, must validate it first. */
772 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
773 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
774 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
775 0, offsetof(s390_fp_regs, fprs));
776 if (rc)
777 return rc;
778 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
779 return -EINVAL;
780 target->thread.fp_regs.fpc = fpc[0];
783 if (rc == 0 && count > 0)
784 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
785 target->thread.fp_regs.fprs,
786 offsetof(s390_fp_regs, fprs), -1);
788 if (rc == 0 && target == current)
789 restore_fp_regs(&target->thread.fp_regs);
791 return rc;
794 static const struct user_regset s390_regsets[] = {
795 [REGSET_GENERAL] = {
796 .core_note_type = NT_PRSTATUS,
797 .n = sizeof(s390_regs) / sizeof(long),
798 .size = sizeof(long),
799 .align = sizeof(long),
800 .get = s390_regs_get,
801 .set = s390_regs_set,
803 [REGSET_FP] = {
804 .core_note_type = NT_PRFPREG,
805 .n = sizeof(s390_fp_regs) / sizeof(long),
806 .size = sizeof(long),
807 .align = sizeof(long),
808 .get = s390_fpregs_get,
809 .set = s390_fpregs_set,
813 static const struct user_regset_view user_s390_view = {
814 .name = UTS_MACHINE,
815 .e_machine = EM_S390,
816 .regsets = s390_regsets,
817 .n = ARRAY_SIZE(s390_regsets)
820 #ifdef CONFIG_COMPAT
821 static int s390_compat_regs_get(struct task_struct *target,
822 const struct user_regset *regset,
823 unsigned int pos, unsigned int count,
824 void *kbuf, void __user *ubuf)
826 if (target == current)
827 save_access_regs(target->thread.acrs);
829 if (kbuf) {
830 compat_ulong_t *k = kbuf;
831 while (count > 0) {
832 *k++ = __peek_user_compat(target, pos);
833 count -= sizeof(*k);
834 pos += sizeof(*k);
836 } else {
837 compat_ulong_t __user *u = ubuf;
838 while (count > 0) {
839 if (__put_user(__peek_user_compat(target, pos), u++))
840 return -EFAULT;
841 count -= sizeof(*u);
842 pos += sizeof(*u);
845 return 0;
848 static int s390_compat_regs_set(struct task_struct *target,
849 const struct user_regset *regset,
850 unsigned int pos, unsigned int count,
851 const void *kbuf, const void __user *ubuf)
853 int rc = 0;
855 if (target == current)
856 save_access_regs(target->thread.acrs);
858 if (kbuf) {
859 const compat_ulong_t *k = kbuf;
860 while (count > 0 && !rc) {
861 rc = __poke_user_compat(target, pos, *k++);
862 count -= sizeof(*k);
863 pos += sizeof(*k);
865 } else {
866 const compat_ulong_t __user *u = ubuf;
867 while (count > 0 && !rc) {
868 compat_ulong_t word;
869 rc = __get_user(word, u++);
870 if (rc)
871 break;
872 rc = __poke_user_compat(target, pos, word);
873 count -= sizeof(*u);
874 pos += sizeof(*u);
878 if (rc == 0 && target == current)
879 restore_access_regs(target->thread.acrs);
881 return rc;
884 static const struct user_regset s390_compat_regsets[] = {
885 [REGSET_GENERAL] = {
886 .core_note_type = NT_PRSTATUS,
887 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
888 .size = sizeof(compat_long_t),
889 .align = sizeof(compat_long_t),
890 .get = s390_compat_regs_get,
891 .set = s390_compat_regs_set,
893 [REGSET_FP] = {
894 .core_note_type = NT_PRFPREG,
895 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
896 .size = sizeof(compat_long_t),
897 .align = sizeof(compat_long_t),
898 .get = s390_fpregs_get,
899 .set = s390_fpregs_set,
903 static const struct user_regset_view user_s390_compat_view = {
904 .name = "s390",
905 .e_machine = EM_S390,
906 .regsets = s390_compat_regsets,
907 .n = ARRAY_SIZE(s390_compat_regsets)
909 #endif
911 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
913 #ifdef CONFIG_COMPAT
914 if (test_tsk_thread_flag(task, TIF_31BIT))
915 return &user_s390_compat_view;
916 #endif
917 return &user_s390_view;