[S390] CVE-2008-1514: prevent ptrace padding area read/write in 31-bit mode
[linux-2.6.git] / arch / s390 / kernel / ptrace.c
blobc8b08289eb87e1d78c9c92996fe7bbfe30c16645
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>
39 #include <asm/segment.h>
40 #include <asm/page.h>
41 #include <asm/pgtable.h>
42 #include <asm/pgalloc.h>
43 #include <asm/system.h>
44 #include <asm/uaccess.h>
45 #include <asm/unistd.h>
46 #include "entry.h"
48 #ifdef CONFIG_COMPAT
49 #include "compat_ptrace.h"
50 #endif
52 enum s390_regset {
53 REGSET_GENERAL,
54 REGSET_FP,
57 static void
58 FixPerRegisters(struct task_struct *task)
60 struct pt_regs *regs;
61 per_struct *per_info;
63 regs = task_pt_regs(task);
64 per_info = (per_struct *) &task->thread.per_info;
65 per_info->control_regs.bits.em_instruction_fetch =
66 per_info->single_step | per_info->instruction_fetch;
68 if (per_info->single_step) {
69 per_info->control_regs.bits.starting_addr = 0;
70 #ifdef CONFIG_COMPAT
71 if (test_thread_flag(TIF_31BIT))
72 per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
73 else
74 #endif
75 per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
76 } else {
77 per_info->control_regs.bits.starting_addr =
78 per_info->starting_addr;
79 per_info->control_regs.bits.ending_addr =
80 per_info->ending_addr;
83 * if any of the control reg tracing bits are on
84 * we switch on per in the psw
86 if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
87 regs->psw.mask |= PSW_MASK_PER;
88 else
89 regs->psw.mask &= ~PSW_MASK_PER;
91 if (per_info->control_regs.bits.em_storage_alteration)
92 per_info->control_regs.bits.storage_alt_space_ctl = 1;
93 else
94 per_info->control_regs.bits.storage_alt_space_ctl = 0;
97 void user_enable_single_step(struct task_struct *task)
99 task->thread.per_info.single_step = 1;
100 FixPerRegisters(task);
103 void user_disable_single_step(struct task_struct *task)
105 task->thread.per_info.single_step = 0;
106 FixPerRegisters(task);
110 * Called by kernel/ptrace.c when detaching..
112 * Make sure single step bits etc are not set.
114 void
115 ptrace_disable(struct task_struct *child)
117 /* make sure the single step bit is not set. */
118 user_disable_single_step(child);
121 #ifndef CONFIG_64BIT
122 # define __ADDR_MASK 3
123 #else
124 # define __ADDR_MASK 7
125 #endif
128 * Read the word at offset addr from the user area of a process. The
129 * trouble here is that the information is littered over different
130 * locations. The process registers are found on the kernel stack,
131 * the floating point stuff and the trace settings are stored in
132 * the task structure. In addition the different structures in
133 * struct user contain pad bytes that should be read as zeroes.
134 * Lovely...
136 static unsigned long __peek_user(struct task_struct *child, addr_t addr)
138 struct user *dummy = NULL;
139 addr_t offset, tmp;
141 if (addr < (addr_t) &dummy->regs.acrs) {
143 * psw and gprs are stored on the stack
145 tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
146 if (addr == (addr_t) &dummy->regs.psw.mask)
147 /* Remove per bit from user psw. */
148 tmp &= ~PSW_MASK_PER;
150 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
152 * access registers are stored in the thread structure
154 offset = addr - (addr_t) &dummy->regs.acrs;
155 #ifdef CONFIG_64BIT
157 * Very special case: old & broken 64 bit gdb reading
158 * from acrs[15]. Result is a 64 bit value. Read the
159 * 32 bit acrs[15] value and shift it by 32. Sick...
161 if (addr == (addr_t) &dummy->regs.acrs[15])
162 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
163 else
164 #endif
165 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
167 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
169 * orig_gpr2 is stored on the kernel stack
171 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
173 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
175 * prevent reads of padding hole between
176 * orig_gpr2 and fp_regs on s390.
178 tmp = 0;
180 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
182 * floating point regs. are stored in the thread structure
184 offset = addr - (addr_t) &dummy->regs.fp_regs;
185 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
186 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
187 tmp &= (unsigned long) FPC_VALID_MASK
188 << (BITS_PER_LONG - 32);
190 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
192 * per_info is found in the thread structure
194 offset = addr - (addr_t) &dummy->regs.per_info;
195 tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
197 } else
198 tmp = 0;
200 return tmp;
203 static int
204 peek_user(struct task_struct *child, addr_t addr, addr_t data)
206 struct user *dummy = NULL;
207 addr_t tmp, mask;
210 * Stupid gdb peeks/pokes the access registers in 64 bit with
211 * an alignment of 4. Programmers from hell...
213 mask = __ADDR_MASK;
214 #ifdef CONFIG_64BIT
215 if (addr >= (addr_t) &dummy->regs.acrs &&
216 addr < (addr_t) &dummy->regs.orig_gpr2)
217 mask = 3;
218 #endif
219 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
220 return -EIO;
222 tmp = __peek_user(child, addr);
223 return put_user(tmp, (addr_t __user *) data);
227 * Write a word to the user area of a process at location addr. This
228 * operation does have an additional problem compared to peek_user.
229 * Stores to the program status word and on the floating point
230 * control register needs to get checked for validity.
232 static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
234 struct user *dummy = NULL;
235 addr_t offset;
237 if (addr < (addr_t) &dummy->regs.acrs) {
239 * psw and gprs are stored on the stack
241 if (addr == (addr_t) &dummy->regs.psw.mask &&
242 #ifdef CONFIG_COMPAT
243 data != PSW_MASK_MERGE(psw_user32_bits, data) &&
244 #endif
245 data != PSW_MASK_MERGE(psw_user_bits, data))
246 /* Invalid psw mask. */
247 return -EINVAL;
248 #ifndef CONFIG_64BIT
249 if (addr == (addr_t) &dummy->regs.psw.addr)
250 /* I'd like to reject addresses without the
251 high order bit but older gdb's rely on it */
252 data |= PSW_ADDR_AMODE;
253 #endif
254 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
256 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
258 * access registers are stored in the thread structure
260 offset = addr - (addr_t) &dummy->regs.acrs;
261 #ifdef CONFIG_64BIT
263 * Very special case: old & broken 64 bit gdb writing
264 * to acrs[15] with a 64 bit value. Ignore the lower
265 * half of the value and write the upper 32 bit to
266 * acrs[15]. Sick...
268 if (addr == (addr_t) &dummy->regs.acrs[15])
269 child->thread.acrs[15] = (unsigned int) (data >> 32);
270 else
271 #endif
272 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
274 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
276 * orig_gpr2 is stored on the kernel stack
278 task_pt_regs(child)->orig_gpr2 = data;
280 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
282 * prevent writes of padding hole between
283 * orig_gpr2 and fp_regs on s390.
285 return 0;
287 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
289 * floating point regs. are stored in the thread structure
291 if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
292 (data & ~((unsigned long) FPC_VALID_MASK
293 << (BITS_PER_LONG - 32))) != 0)
294 return -EINVAL;
295 offset = addr - (addr_t) &dummy->regs.fp_regs;
296 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
298 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
300 * per_info is found in the thread structure
302 offset = addr - (addr_t) &dummy->regs.per_info;
303 *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
307 FixPerRegisters(child);
308 return 0;
311 static int
312 poke_user(struct task_struct *child, addr_t addr, addr_t data)
314 struct user *dummy = NULL;
315 addr_t mask;
318 * Stupid gdb peeks/pokes the access registers in 64 bit with
319 * an alignment of 4. Programmers from hell indeed...
321 mask = __ADDR_MASK;
322 #ifdef CONFIG_64BIT
323 if (addr >= (addr_t) &dummy->regs.acrs &&
324 addr < (addr_t) &dummy->regs.orig_gpr2)
325 mask = 3;
326 #endif
327 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
328 return -EIO;
330 return __poke_user(child, addr, data);
333 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
335 ptrace_area parea;
336 int copied, ret;
338 switch (request) {
339 case PTRACE_PEEKTEXT:
340 case PTRACE_PEEKDATA:
341 /* Remove high order bit from address (only for 31 bit). */
342 addr &= PSW_ADDR_INSN;
343 /* read word at location addr. */
344 return generic_ptrace_peekdata(child, addr, data);
346 case PTRACE_PEEKUSR:
347 /* read the word at location addr in the USER area. */
348 return peek_user(child, addr, data);
350 case PTRACE_POKETEXT:
351 case PTRACE_POKEDATA:
352 /* Remove high order bit from address (only for 31 bit). */
353 addr &= PSW_ADDR_INSN;
354 /* write the word at location addr. */
355 return generic_ptrace_pokedata(child, addr, data);
357 case PTRACE_POKEUSR:
358 /* write the word at location addr in the USER area */
359 return poke_user(child, addr, data);
361 case PTRACE_PEEKUSR_AREA:
362 case PTRACE_POKEUSR_AREA:
363 if (copy_from_user(&parea, (void __force __user *) addr,
364 sizeof(parea)))
365 return -EFAULT;
366 addr = parea.kernel_addr;
367 data = parea.process_addr;
368 copied = 0;
369 while (copied < parea.len) {
370 if (request == PTRACE_PEEKUSR_AREA)
371 ret = peek_user(child, addr, data);
372 else {
373 addr_t utmp;
374 if (get_user(utmp,
375 (addr_t __force __user *) data))
376 return -EFAULT;
377 ret = poke_user(child, addr, utmp);
379 if (ret)
380 return ret;
381 addr += sizeof(unsigned long);
382 data += sizeof(unsigned long);
383 copied += sizeof(unsigned long);
385 return 0;
387 return ptrace_request(child, request, addr, data);
390 #ifdef CONFIG_COMPAT
392 * Now the fun part starts... a 31 bit program running in the
393 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
394 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
395 * to handle, the difference to the 64 bit versions of the requests
396 * is that the access is done in multiples of 4 byte instead of
397 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
398 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
399 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
400 * is a 31 bit program too, the content of struct user can be
401 * emulated. A 31 bit program peeking into the struct user of
402 * a 64 bit program is a no-no.
406 * Same as peek_user but for a 31 bit program.
408 static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
410 struct user32 *dummy32 = NULL;
411 per_struct32 *dummy_per32 = NULL;
412 addr_t offset;
413 __u32 tmp;
415 if (addr < (addr_t) &dummy32->regs.acrs) {
417 * psw and gprs are stored on the stack
419 if (addr == (addr_t) &dummy32->regs.psw.mask) {
420 /* Fake a 31 bit psw mask. */
421 tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
422 tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
423 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
424 /* Fake a 31 bit psw address. */
425 tmp = (__u32) task_pt_regs(child)->psw.addr |
426 PSW32_ADDR_AMODE31;
427 } else {
428 /* gpr 0-15 */
429 tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
430 addr*2 + 4);
432 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
434 * access registers are stored in the thread structure
436 offset = addr - (addr_t) &dummy32->regs.acrs;
437 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
439 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
441 * orig_gpr2 is stored on the kernel stack
443 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
445 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
447 * prevent reads of padding hole between
448 * orig_gpr2 and fp_regs on s390.
450 tmp = 0;
452 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
454 * floating point regs. are stored in the thread structure
456 offset = addr - (addr_t) &dummy32->regs.fp_regs;
457 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
459 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
461 * per_info is found in the thread structure
463 offset = addr - (addr_t) &dummy32->regs.per_info;
464 /* This is magic. See per_struct and per_struct32. */
465 if ((offset >= (addr_t) &dummy_per32->control_regs &&
466 offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
467 (offset >= (addr_t) &dummy_per32->starting_addr &&
468 offset <= (addr_t) &dummy_per32->ending_addr) ||
469 offset == (addr_t) &dummy_per32->lowcore.words.address)
470 offset = offset*2 + 4;
471 else
472 offset = offset*2;
473 tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
475 } else
476 tmp = 0;
478 return tmp;
481 static int peek_user_compat(struct task_struct *child,
482 addr_t addr, addr_t data)
484 __u32 tmp;
486 if (!test_thread_flag(TIF_31BIT) ||
487 (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 (!test_thread_flag(TIF_31BIT) ||
589 (addr & 3) || addr > sizeof(struct user32) - 3)
590 return -EIO;
592 return __poke_user_compat(child, addr, data);
595 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
596 compat_ulong_t caddr, compat_ulong_t cdata)
598 unsigned long addr = caddr;
599 unsigned long data = cdata;
600 ptrace_area_emu31 parea;
601 int copied, ret;
603 switch (request) {
604 case PTRACE_PEEKUSR:
605 /* read the word at location addr in the USER area. */
606 return peek_user_compat(child, addr, data);
608 case PTRACE_POKEUSR:
609 /* write the word at location addr in the USER area */
610 return poke_user_compat(child, addr, data);
612 case PTRACE_PEEKUSR_AREA:
613 case PTRACE_POKEUSR_AREA:
614 if (copy_from_user(&parea, (void __force __user *) addr,
615 sizeof(parea)))
616 return -EFAULT;
617 addr = parea.kernel_addr;
618 data = parea.process_addr;
619 copied = 0;
620 while (copied < parea.len) {
621 if (request == PTRACE_PEEKUSR_AREA)
622 ret = peek_user_compat(child, addr, data);
623 else {
624 __u32 utmp;
625 if (get_user(utmp,
626 (__u32 __force __user *) data))
627 return -EFAULT;
628 ret = poke_user_compat(child, addr, utmp);
630 if (ret)
631 return ret;
632 addr += sizeof(unsigned int);
633 data += sizeof(unsigned int);
634 copied += sizeof(unsigned int);
636 return 0;
638 return compat_ptrace_request(child, request, addr, data);
640 #endif
642 asmlinkage void
643 syscall_trace(struct pt_regs *regs, int entryexit)
645 if (unlikely(current->audit_context) && entryexit)
646 audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]), regs->gprs[2]);
648 if (!test_thread_flag(TIF_SYSCALL_TRACE))
649 goto out;
650 if (!(current->ptrace & PT_PTRACED))
651 goto out;
652 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
653 ? 0x80 : 0));
656 * If the debuffer has set an invalid system call number,
657 * we prepare to skip the system call restart handling.
659 if (!entryexit && regs->gprs[2] >= NR_syscalls)
660 regs->trap = -1;
663 * this isn't the same as continuing with a signal, but it will do
664 * for normal use. strace only continues with a signal if the
665 * stopping signal is not SIGTRAP. -brl
667 if (current->exit_code) {
668 send_sig(current->exit_code, current, 1);
669 current->exit_code = 0;
671 out:
672 if (unlikely(current->audit_context) && !entryexit)
673 audit_syscall_entry(test_thread_flag(TIF_31BIT)?AUDIT_ARCH_S390:AUDIT_ARCH_S390X,
674 regs->gprs[2], regs->orig_gpr2, regs->gprs[3],
675 regs->gprs[4], regs->gprs[5]);
679 * user_regset definitions.
682 static int s390_regs_get(struct task_struct *target,
683 const struct user_regset *regset,
684 unsigned int pos, unsigned int count,
685 void *kbuf, void __user *ubuf)
687 if (target == current)
688 save_access_regs(target->thread.acrs);
690 if (kbuf) {
691 unsigned long *k = kbuf;
692 while (count > 0) {
693 *k++ = __peek_user(target, pos);
694 count -= sizeof(*k);
695 pos += sizeof(*k);
697 } else {
698 unsigned long __user *u = ubuf;
699 while (count > 0) {
700 if (__put_user(__peek_user(target, pos), u++))
701 return -EFAULT;
702 count -= sizeof(*u);
703 pos += sizeof(*u);
706 return 0;
709 static int s390_regs_set(struct task_struct *target,
710 const struct user_regset *regset,
711 unsigned int pos, unsigned int count,
712 const void *kbuf, const void __user *ubuf)
714 int rc = 0;
716 if (target == current)
717 save_access_regs(target->thread.acrs);
719 if (kbuf) {
720 const unsigned long *k = kbuf;
721 while (count > 0 && !rc) {
722 rc = __poke_user(target, pos, *k++);
723 count -= sizeof(*k);
724 pos += sizeof(*k);
726 } else {
727 const unsigned long __user *u = ubuf;
728 while (count > 0 && !rc) {
729 unsigned long word;
730 rc = __get_user(word, u++);
731 if (rc)
732 break;
733 rc = __poke_user(target, pos, word);
734 count -= sizeof(*u);
735 pos += sizeof(*u);
739 if (rc == 0 && target == current)
740 restore_access_regs(target->thread.acrs);
742 return rc;
745 static int s390_fpregs_get(struct task_struct *target,
746 const struct user_regset *regset, unsigned int pos,
747 unsigned int count, void *kbuf, void __user *ubuf)
749 if (target == current)
750 save_fp_regs(&target->thread.fp_regs);
752 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
753 &target->thread.fp_regs, 0, -1);
756 static int s390_fpregs_set(struct task_struct *target,
757 const struct user_regset *regset, unsigned int pos,
758 unsigned int count, const void *kbuf,
759 const void __user *ubuf)
761 int rc = 0;
763 if (target == current)
764 save_fp_regs(&target->thread.fp_regs);
766 /* If setting FPC, must validate it first. */
767 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
768 u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
769 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
770 0, offsetof(s390_fp_regs, fprs));
771 if (rc)
772 return rc;
773 if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
774 return -EINVAL;
775 target->thread.fp_regs.fpc = fpc[0];
778 if (rc == 0 && count > 0)
779 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
780 target->thread.fp_regs.fprs,
781 offsetof(s390_fp_regs, fprs), -1);
783 if (rc == 0 && target == current)
784 restore_fp_regs(&target->thread.fp_regs);
786 return rc;
789 static const struct user_regset s390_regsets[] = {
790 [REGSET_GENERAL] = {
791 .core_note_type = NT_PRSTATUS,
792 .n = sizeof(s390_regs) / sizeof(long),
793 .size = sizeof(long),
794 .align = sizeof(long),
795 .get = s390_regs_get,
796 .set = s390_regs_set,
798 [REGSET_FP] = {
799 .core_note_type = NT_PRFPREG,
800 .n = sizeof(s390_fp_regs) / sizeof(long),
801 .size = sizeof(long),
802 .align = sizeof(long),
803 .get = s390_fpregs_get,
804 .set = s390_fpregs_set,
808 static const struct user_regset_view user_s390_view = {
809 .name = UTS_MACHINE,
810 .e_machine = EM_S390,
811 .regsets = s390_regsets,
812 .n = ARRAY_SIZE(s390_regsets)
815 #ifdef CONFIG_COMPAT
816 static int s390_compat_regs_get(struct task_struct *target,
817 const struct user_regset *regset,
818 unsigned int pos, unsigned int count,
819 void *kbuf, void __user *ubuf)
821 if (target == current)
822 save_access_regs(target->thread.acrs);
824 if (kbuf) {
825 compat_ulong_t *k = kbuf;
826 while (count > 0) {
827 *k++ = __peek_user_compat(target, pos);
828 count -= sizeof(*k);
829 pos += sizeof(*k);
831 } else {
832 compat_ulong_t __user *u = ubuf;
833 while (count > 0) {
834 if (__put_user(__peek_user_compat(target, pos), u++))
835 return -EFAULT;
836 count -= sizeof(*u);
837 pos += sizeof(*u);
840 return 0;
843 static int s390_compat_regs_set(struct task_struct *target,
844 const struct user_regset *regset,
845 unsigned int pos, unsigned int count,
846 const void *kbuf, const void __user *ubuf)
848 int rc = 0;
850 if (target == current)
851 save_access_regs(target->thread.acrs);
853 if (kbuf) {
854 const compat_ulong_t *k = kbuf;
855 while (count > 0 && !rc) {
856 rc = __poke_user_compat(target, pos, *k++);
857 count -= sizeof(*k);
858 pos += sizeof(*k);
860 } else {
861 const compat_ulong_t __user *u = ubuf;
862 while (count > 0 && !rc) {
863 compat_ulong_t word;
864 rc = __get_user(word, u++);
865 if (rc)
866 break;
867 rc = __poke_user_compat(target, pos, word);
868 count -= sizeof(*u);
869 pos += sizeof(*u);
873 if (rc == 0 && target == current)
874 restore_access_regs(target->thread.acrs);
876 return rc;
879 static const struct user_regset s390_compat_regsets[] = {
880 [REGSET_GENERAL] = {
881 .core_note_type = NT_PRSTATUS,
882 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
883 .size = sizeof(compat_long_t),
884 .align = sizeof(compat_long_t),
885 .get = s390_compat_regs_get,
886 .set = s390_compat_regs_set,
888 [REGSET_FP] = {
889 .core_note_type = NT_PRFPREG,
890 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
891 .size = sizeof(compat_long_t),
892 .align = sizeof(compat_long_t),
893 .get = s390_fpregs_get,
894 .set = s390_fpregs_set,
898 static const struct user_regset_view user_s390_compat_view = {
899 .name = "s390",
900 .e_machine = EM_S390,
901 .regsets = s390_compat_regsets,
902 .n = ARRAY_SIZE(s390_compat_regsets)
904 #endif
906 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
908 #ifdef CONFIG_COMPAT
909 if (test_tsk_thread_flag(task, TIF_31BIT))
910 return &user_s390_compat_view;
911 #endif
912 return &user_s390_view;