1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
288df125fSHeiko Carstens /*
388df125fSHeiko Carstens * Access kernel memory without faulting -- s390 specific implementation.
488df125fSHeiko Carstens *
53c1a3bceSHeiko Carstens * Copyright IBM Corp. 2009, 2015
688df125fSHeiko Carstens *
788df125fSHeiko Carstens */
888df125fSHeiko Carstens
988df125fSHeiko Carstens #include <linux/uaccess.h>
1088df125fSHeiko Carstens #include <linux/kernel.h>
1188df125fSHeiko Carstens #include <linux/types.h>
1288df125fSHeiko Carstens #include <linux/errno.h>
137f0bf656SMichael Holzheu #include <linux/gfp.h>
14b2a68c23SMichael Holzheu #include <linux/cpu.h>
152f0e8aaeSAlexander Gordeev #include <linux/uio.h>
16b378a982SHeiko Carstens #include <linux/io.h>
17d09a307fSHeiko Carstens #include <asm/asm-extable.h>
18a0616cdeSDavid Howells #include <asm/ctl_reg.h>
194df29d2bSAlexander Gordeev #include <asm/abs_lowcore.h>
2078c98f90SMartin Schwidefsky #include <asm/stacktrace.h>
219267bdd8SAlexander Gordeev #include <asm/maccess.h>
2288df125fSHeiko Carstens
232f0e8aaeSAlexander Gordeev unsigned long __bootdata_preserved(__memcpy_real_area);
248e9205d2SAlexander Gordeev pte_t *__bootdata_preserved(memcpy_real_ptep);
252f0e8aaeSAlexander Gordeev static DEFINE_MUTEX(memcpy_real_mutex);
262f0e8aaeSAlexander Gordeev
s390_kernel_write_odd(void * dst,const void * src,size_t size)278a5d8473SHeiko Carstens static notrace long s390_kernel_write_odd(void *dst, const void *src, size_t size)
2888df125fSHeiko Carstens {
293c1a3bceSHeiko Carstens unsigned long aligned, offset, count;
303c1a3bceSHeiko Carstens char tmp[8];
3188df125fSHeiko Carstens
323c1a3bceSHeiko Carstens aligned = (unsigned long) dst & ~7UL;
333c1a3bceSHeiko Carstens offset = (unsigned long) dst & 7UL;
343c1a3bceSHeiko Carstens size = min(8UL - offset, size);
353c1a3bceSHeiko Carstens count = size - 1;
3688df125fSHeiko Carstens asm volatile(
3788df125fSHeiko Carstens " bras 1,0f\n"
383c1a3bceSHeiko Carstens " mvc 0(1,%4),0(%5)\n"
393c1a3bceSHeiko Carstens "0: mvc 0(8,%3),0(%0)\n"
403c1a3bceSHeiko Carstens " ex %1,0(1)\n"
413c1a3bceSHeiko Carstens " lg %1,0(%3)\n"
423c1a3bceSHeiko Carstens " lra %0,0(%0)\n"
433c1a3bceSHeiko Carstens " sturg %1,%0\n"
443c1a3bceSHeiko Carstens : "+&a" (aligned), "+&a" (count), "=m" (tmp)
453c1a3bceSHeiko Carstens : "a" (&tmp), "a" (&tmp[offset]), "a" (src)
463c1a3bceSHeiko Carstens : "cc", "memory", "1");
473c1a3bceSHeiko Carstens return size;
4888df125fSHeiko Carstens }
4988df125fSHeiko Carstens
508a5d8473SHeiko Carstens /*
518a5d8473SHeiko Carstens * s390_kernel_write - write to kernel memory bypassing DAT
528a5d8473SHeiko Carstens * @dst: destination address
538a5d8473SHeiko Carstens * @src: source address
548a5d8473SHeiko Carstens * @size: number of bytes to copy
558a5d8473SHeiko Carstens *
568a5d8473SHeiko Carstens * This function writes to kernel memory bypassing DAT and possible page table
578a5d8473SHeiko Carstens * write protection. It writes to the destination using the sturg instruction.
583c1a3bceSHeiko Carstens * Therefore we have a read-modify-write sequence: the function reads eight
593c1a3bceSHeiko Carstens * bytes from destination at an eight byte boundary, modifies the bytes
608a5d8473SHeiko Carstens * requested and writes the result back in a loop.
618a5d8473SHeiko Carstens */
62a646ef39SMartin Schwidefsky static DEFINE_SPINLOCK(s390_kernel_write_lock);
63a646ef39SMartin Schwidefsky
s390_kernel_write(void * dst,const void * src,size_t size)64cb2cceaeSJosh Poimboeuf notrace void *s390_kernel_write(void *dst, const void *src, size_t size)
6588df125fSHeiko Carstens {
66cb2cceaeSJosh Poimboeuf void *tmp = dst;
67a646ef39SMartin Schwidefsky unsigned long flags;
683c1a3bceSHeiko Carstens long copied;
6988df125fSHeiko Carstens
70a646ef39SMartin Schwidefsky spin_lock_irqsave(&s390_kernel_write_lock, flags);
7188df125fSHeiko Carstens while (size) {
72cb2cceaeSJosh Poimboeuf copied = s390_kernel_write_odd(tmp, src, size);
73cb2cceaeSJosh Poimboeuf tmp += copied;
7488df125fSHeiko Carstens src += copied;
7588df125fSHeiko Carstens size -= copied;
7688df125fSHeiko Carstens }
77a646ef39SMartin Schwidefsky spin_unlock_irqrestore(&s390_kernel_write_lock, flags);
78cb2cceaeSJosh Poimboeuf
79cb2cceaeSJosh Poimboeuf return dst;
8088df125fSHeiko Carstens }
8192fe3132SMichael Holzheu
memcpy_real_iter(struct iov_iter * iter,unsigned long src,size_t count)822f0e8aaeSAlexander Gordeev size_t memcpy_real_iter(struct iov_iter *iter, unsigned long src, size_t count)
83b785e0d0SMichael Holzheu {
842f0e8aaeSAlexander Gordeev size_t len, copied, res = 0;
852f0e8aaeSAlexander Gordeev unsigned long phys, offset;
862f0e8aaeSAlexander Gordeev void *chunk;
872f0e8aaeSAlexander Gordeev pte_t pte;
88b785e0d0SMichael Holzheu
89*a984f27eSAlexander Gordeev BUILD_BUG_ON(MEMCPY_REAL_SIZE != PAGE_SIZE);
902f0e8aaeSAlexander Gordeev while (count) {
91*a984f27eSAlexander Gordeev phys = src & MEMCPY_REAL_MASK;
92*a984f27eSAlexander Gordeev offset = src & ~MEMCPY_REAL_MASK;
932f0e8aaeSAlexander Gordeev chunk = (void *)(__memcpy_real_area + offset);
94*a984f27eSAlexander Gordeev len = min(count, MEMCPY_REAL_SIZE - offset);
952f0e8aaeSAlexander Gordeev pte = mk_pte_phys(phys, PAGE_KERNEL_RO);
962f0e8aaeSAlexander Gordeev
972f0e8aaeSAlexander Gordeev mutex_lock(&memcpy_real_mutex);
982f0e8aaeSAlexander Gordeev if (pte_val(pte) != pte_val(*memcpy_real_ptep)) {
992f0e8aaeSAlexander Gordeev __ptep_ipte(__memcpy_real_area, memcpy_real_ptep, 0, 0, IPTE_GLOBAL);
1002f0e8aaeSAlexander Gordeev set_pte(memcpy_real_ptep, pte);
1012f0e8aaeSAlexander Gordeev }
1022f0e8aaeSAlexander Gordeev copied = copy_to_iter(chunk, len, iter);
1032f0e8aaeSAlexander Gordeev mutex_unlock(&memcpy_real_mutex);
1042f0e8aaeSAlexander Gordeev
1052f0e8aaeSAlexander Gordeev count -= copied;
1062f0e8aaeSAlexander Gordeev src += copied;
1072f0e8aaeSAlexander Gordeev res += copied;
1082f0e8aaeSAlexander Gordeev if (copied < len)
1092f0e8aaeSAlexander Gordeev break;
1102f0e8aaeSAlexander Gordeev }
1112f0e8aaeSAlexander Gordeev return res;
11292fe3132SMichael Holzheu }
1137dd6b334SMichael Holzheu
memcpy_real(void * dest,unsigned long src,size_t count)114303fd988SAlexander Gordeev int memcpy_real(void *dest, unsigned long src, size_t count)
115ce3dc447SMartin Schwidefsky {
1162f0e8aaeSAlexander Gordeev struct iov_iter iter;
1172f0e8aaeSAlexander Gordeev struct kvec kvec;
1187f28dad3SVasily Gorbik
1192f0e8aaeSAlexander Gordeev kvec.iov_base = dest;
1202f0e8aaeSAlexander Gordeev kvec.iov_len = count;
121de4eda9dSAl Viro iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, count);
1222f0e8aaeSAlexander Gordeev if (memcpy_real_iter(&iter, src, count) < count)
1232f0e8aaeSAlexander Gordeev return -EFAULT;
1242f0e8aaeSAlexander Gordeev return 0;
125ce3dc447SMartin Schwidefsky }
126ce3dc447SMartin Schwidefsky
127ce3dc447SMartin Schwidefsky /*
1284df29d2bSAlexander Gordeev * Find CPU that owns swapped prefix page
1297dd6b334SMichael Holzheu */
get_swapped_owner(phys_addr_t addr)1304df29d2bSAlexander Gordeev static int get_swapped_owner(phys_addr_t addr)
131b2a68c23SMichael Holzheu {
1321f231e29SAlexander Gordeev phys_addr_t lc;
133b2a68c23SMichael Holzheu int cpu;
134b2a68c23SMichael Holzheu
135b2a68c23SMichael Holzheu for_each_online_cpu(cpu) {
1361f231e29SAlexander Gordeev lc = virt_to_phys(lowcore_ptr[cpu]);
137c667aeacSHeiko Carstens if (addr > lc + sizeof(struct lowcore) - 1 || addr < lc)
138b2a68c23SMichael Holzheu continue;
1394df29d2bSAlexander Gordeev return cpu;
140b2a68c23SMichael Holzheu }
1414df29d2bSAlexander Gordeev return -1;
142b2a68c23SMichael Holzheu }
143b2a68c23SMichael Holzheu
144b2a68c23SMichael Holzheu /*
145b2a68c23SMichael Holzheu * Convert a physical pointer for /dev/mem access
146b2a68c23SMichael Holzheu *
147b2a68c23SMichael Holzheu * For swapped prefix pages a new buffer is returned that contains a copy of
148b2a68c23SMichael Holzheu * the absolute memory. The buffer size is maximum one page large.
149b2a68c23SMichael Holzheu */
xlate_dev_mem_ptr(phys_addr_t addr)1504707a341SThierry Reding void *xlate_dev_mem_ptr(phys_addr_t addr)
151b2a68c23SMichael Holzheu {
1521f231e29SAlexander Gordeev void *ptr = phys_to_virt(addr);
1531f231e29SAlexander Gordeev void *bounce = ptr;
1544df29d2bSAlexander Gordeev struct lowcore *abs_lc;
155b2a68c23SMichael Holzheu unsigned long size;
1564df29d2bSAlexander Gordeev int this_cpu, cpu;
157b2a68c23SMichael Holzheu
158a73de293SSebastian Andrzej Siewior cpus_read_lock();
1594df29d2bSAlexander Gordeev this_cpu = get_cpu();
1604df29d2bSAlexander Gordeev if (addr >= sizeof(struct lowcore)) {
1614df29d2bSAlexander Gordeev cpu = get_swapped_owner(addr);
1624df29d2bSAlexander Gordeev if (cpu < 0)
1634df29d2bSAlexander Gordeev goto out;
1647d06fed7SAlexander Gordeev }
1654df29d2bSAlexander Gordeev bounce = (void *)__get_free_page(GFP_ATOMIC);
1664df29d2bSAlexander Gordeev if (!bounce)
1674df29d2bSAlexander Gordeev goto out;
1684df29d2bSAlexander Gordeev size = PAGE_SIZE - (addr & ~PAGE_MASK);
1694df29d2bSAlexander Gordeev if (addr < sizeof(struct lowcore)) {
1702154e0b3SAlexander Gordeev abs_lc = get_abs_lowcore();
1714df29d2bSAlexander Gordeev ptr = (void *)abs_lc + addr;
1724df29d2bSAlexander Gordeev memcpy(bounce, ptr, size);
1732154e0b3SAlexander Gordeev put_abs_lowcore(abs_lc);
1744df29d2bSAlexander Gordeev } else if (cpu == this_cpu) {
1754df29d2bSAlexander Gordeev ptr = (void *)(addr - virt_to_phys(lowcore_ptr[cpu]));
1764df29d2bSAlexander Gordeev memcpy(bounce, ptr, size);
1774df29d2bSAlexander Gordeev } else {
1784df29d2bSAlexander Gordeev memcpy(bounce, ptr, size);
1794df29d2bSAlexander Gordeev }
1804df29d2bSAlexander Gordeev out:
1814df29d2bSAlexander Gordeev put_cpu();
182a73de293SSebastian Andrzej Siewior cpus_read_unlock();
183b2a68c23SMichael Holzheu return bounce;
184b2a68c23SMichael Holzheu }
185b2a68c23SMichael Holzheu
186b2a68c23SMichael Holzheu /*
187b2a68c23SMichael Holzheu * Free converted buffer for /dev/mem access (if necessary)
188b2a68c23SMichael Holzheu */
unxlate_dev_mem_ptr(phys_addr_t addr,void * ptr)1891f231e29SAlexander Gordeev void unxlate_dev_mem_ptr(phys_addr_t addr, void *ptr)
190b2a68c23SMichael Holzheu {
1911f231e29SAlexander Gordeev if (addr != virt_to_phys(ptr))
1921f231e29SAlexander Gordeev free_page((unsigned long)ptr);
193b2a68c23SMichael Holzheu }
194