1 // SPDX-License-Identifier: GPL-2.0
3 * Access kernel memory without faulting -- s390 specific implementation.
5 * Copyright IBM Corp. 2009, 2015
7 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
11 #include <linux/uaccess.h>
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/errno.h>
15 #include <linux/gfp.h>
16 #include <linux/cpu.h>
17 #include <asm/ctl_reg.h>
19 #include <asm/stacktrace.h>
21 static notrace long s390_kernel_write_odd(void *dst, const void *src, size_t size)
23 unsigned long aligned, offset, count;
26 aligned = (unsigned long) dst & ~7UL;
27 offset = (unsigned long) dst & 7UL;
28 size = min(8UL - offset, size);
32 " mvc 0(1,%4),0(%5)\n"
33 "0: mvc 0(8,%3),0(%0)\n"
38 : "+&a" (aligned), "+&a" (count), "=m" (tmp)
39 : "a" (&tmp), "a" (&tmp[offset]), "a" (src)
40 : "cc", "memory", "1");
45 * s390_kernel_write - write to kernel memory bypassing DAT
46 * @dst: destination address
47 * @src: source address
48 * @size: number of bytes to copy
50 * This function writes to kernel memory bypassing DAT and possible page table
51 * write protection. It writes to the destination using the sturg instruction.
52 * Therefore we have a read-modify-write sequence: the function reads eight
53 * bytes from destination at an eight byte boundary, modifies the bytes
54 * requested and writes the result back in a loop.
56 static DEFINE_SPINLOCK(s390_kernel_write_lock);
58 notrace void *s390_kernel_write(void *dst, const void *src, size_t size)
64 spin_lock_irqsave(&s390_kernel_write_lock, flags);
65 if (!(flags & PSW_MASK_DAT)) {
66 memcpy(dst, src, size);
69 copied = s390_kernel_write_odd(tmp, src, size);
75 spin_unlock_irqrestore(&s390_kernel_write_lock, flags);
80 static int __no_sanitize_address __memcpy_real(void *dest, void *src, size_t count)
82 union register_pair _dst, _src;
85 _dst.even = (unsigned long) dest;
86 _dst.odd = (unsigned long) count;
87 _src.even = (unsigned long) src;
88 _src.odd = (unsigned long) count;
90 "0: mvcle %[dst],%[src],0\n"
95 : [rc] "+&d" (rc), [dst] "+&d" (_dst.pair), [src] "+&d" (_src.pair)
100 static unsigned long __no_sanitize_address _memcpy_real(unsigned long dest,
104 int irqs_disabled, rc;
109 flags = arch_local_irq_save();
110 irqs_disabled = arch_irqs_disabled_flags(flags);
112 trace_hardirqs_off();
113 __arch_local_irq_stnsm(0xf8); // disable DAT
114 rc = __memcpy_real((void *) dest, (void *) src, (size_t) count);
115 if (flags & PSW_MASK_DAT)
116 __arch_local_irq_stosm(0x04); // enable DAT
119 __arch_local_irq_ssm(flags);
124 * Copy memory in real mode (kernel to kernel)
126 int memcpy_real(void *dest, void *src, size_t count)
128 unsigned long _dest = (unsigned long)dest;
129 unsigned long _src = (unsigned long)src;
130 unsigned long _count = (unsigned long)count;
133 if (S390_lowcore.nodat_stack != 0) {
135 rc = call_on_stack(3, S390_lowcore.nodat_stack,
136 unsigned long, _memcpy_real,
137 unsigned long, _dest,
139 unsigned long, _count);
144 * This is a really early memcpy_real call, the stacks are
145 * not set up yet. Just call _memcpy_real on the early boot
148 return _memcpy_real(_dest, _src, _count);
152 * Copy memory in absolute mode (kernel to kernel)
154 void memcpy_absolute(void *dest, void *src, size_t count)
156 unsigned long cr0, flags, prefix;
158 flags = arch_local_irq_save();
159 __ctl_store(cr0, 0, 0);
160 __ctl_clear_bit(0, 28); /* disable lowcore protection */
161 prefix = store_prefix();
163 local_mcck_disable();
165 memcpy(dest, src, count);
169 memcpy(dest, src, count);
171 __ctl_load(cr0, 0, 0);
172 arch_local_irq_restore(flags);
176 * Copy memory from kernel (real) to user (virtual)
178 int copy_to_user_real(void __user *dest, void *src, unsigned long count)
180 int offs = 0, size, rc;
183 buf = (char *) __get_free_page(GFP_KERNEL);
187 while (offs < count) {
188 size = min(PAGE_SIZE, count - offs);
189 if (memcpy_real(buf, src + offs, size))
191 if (copy_to_user(dest + offs, buf, size))
197 free_page((unsigned long) buf);
202 * Check if physical address is within prefix or zero page
204 static int is_swapped(unsigned long addr)
209 if (addr < sizeof(struct lowcore))
211 for_each_online_cpu(cpu) {
212 lc = (unsigned long) lowcore_ptr[cpu];
213 if (addr > lc + sizeof(struct lowcore) - 1 || addr < lc)
221 * Convert a physical pointer for /dev/mem access
223 * For swapped prefix pages a new buffer is returned that contains a copy of
224 * the absolute memory. The buffer size is maximum one page large.
226 void *xlate_dev_mem_ptr(phys_addr_t addr)
228 void *bounce = (void *) addr;
233 if (is_swapped(addr)) {
234 size = PAGE_SIZE - (addr & ~PAGE_MASK);
235 bounce = (void *) __get_free_page(GFP_ATOMIC);
237 memcpy_absolute(bounce, (void *) addr, size);
245 * Free converted buffer for /dev/mem access (if necessary)
247 void unxlate_dev_mem_ptr(phys_addr_t addr, void *buf)
249 if ((void *) addr != buf)
250 free_page((unsigned long) buf);