2 * Access kernel memory without faulting -- s390 specific implementation.
4 * Copyright IBM Corp. 2009
6 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
10 #include <linux/uaccess.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/gfp.h>
15 #include <asm/system.h>
18 * This function writes to kernel memory bypassing DAT and possible
19 * write protection. It copies one to four bytes from src to dst
20 * using the stura instruction.
21 * Returns the number of bytes copied or -EFAULT.
23 static long probe_kernel_write_odd(void *dst
, const void *src
, size_t size
)
25 unsigned long count
, aligned
;
29 aligned
= (unsigned long) dst
& ~3UL;
30 offset
= (unsigned long) dst
& 3;
31 count
= min_t(unsigned long, 4 - offset
, size
);
32 mask
= (0xf << (4 - count
)) & 0xf;
43 EX_TABLE(0b
,3b
) EX_TABLE(1b
,3b
) EX_TABLE(2b
,3b
)
44 : "+d" (rc
), "+a" (aligned
)
45 : "a" (mask
), "a" (src
) : "cc", "memory", "0", "1");
46 return rc
? rc
: count
;
49 long probe_kernel_write(void *dst
, const void *src
, size_t size
)
54 copied
= probe_kernel_write_odd(dst
, src
, size
);
61 return copied
< 0 ? -EFAULT
: 0;
65 * Copy memory in real mode (kernel to kernel)
67 int memcpy_real(void *dest
, void *src
, size_t count
)
69 register unsigned long _dest
asm("2") = (unsigned long) dest
;
70 register unsigned long _len1
asm("3") = (unsigned long) count
;
71 register unsigned long _src
asm("4") = (unsigned long) src
;
72 register unsigned long _len2
asm("5") = (unsigned long) count
;
78 flags
= __arch_local_irq_stnsm(0xf8UL
);
80 "0: mvcle %1,%2,0x0\n"
85 : "+d" (rc
), "+d" (_dest
), "+d" (_src
), "+d" (_len1
),
86 "+d" (_len2
), "=m" (*((long *) dest
))
87 : "m" (*((long *) src
))
89 arch_local_irq_restore(flags
);
94 * Copy memory to absolute zero
96 void copy_to_absolute_zero(void *dest
, void *src
, size_t count
)
100 BUG_ON((unsigned long) dest
+ count
>= sizeof(struct _lowcore
));
102 __ctl_store(cr0
, 0, 0);
103 __ctl_clear_bit(0, 28); /* disable lowcore protection */
104 memcpy_real(dest
+ store_prefix(), src
, count
);
105 __ctl_load(cr0
, 0, 0);
110 * Copy memory from kernel (real) to user (virtual)
112 int copy_to_user_real(void __user
*dest
, void *src
, size_t count
)
114 int offs
= 0, size
, rc
;
117 buf
= (char *) __get_free_page(GFP_KERNEL
);
121 while (offs
< count
) {
122 size
= min(PAGE_SIZE
, count
- offs
);
123 if (memcpy_real(buf
, src
+ offs
, size
))
125 if (copy_to_user(dest
+ offs
, buf
, size
))
131 free_page((unsigned long) buf
);
136 * Copy memory from user (virtual) to kernel (real)
138 int copy_from_user_real(void *dest
, void __user
*src
, size_t count
)
140 int offs
= 0, size
, rc
;
143 buf
= (char *) __get_free_page(GFP_KERNEL
);
147 while (offs
< count
) {
148 size
= min(PAGE_SIZE
, count
- offs
);
149 if (copy_from_user(buf
, src
+ offs
, size
))
151 if (memcpy_real(dest
+ offs
, buf
, size
))
157 free_page((unsigned long) buf
);