xref: /openbmc/linux/arch/s390/mm/maccess.c (revision 8a5d8473dd7e2b0bc2864e34bd6836b520589fa1)
188df125fSHeiko Carstens /*
288df125fSHeiko Carstens  * Access kernel memory without faulting -- s390 specific implementation.
388df125fSHeiko Carstens  *
488df125fSHeiko Carstens  * Copyright IBM Corp. 2009
588df125fSHeiko Carstens  *
688df125fSHeiko Carstens  *   Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
788df125fSHeiko Carstens  *
888df125fSHeiko Carstens  */
988df125fSHeiko Carstens 
1088df125fSHeiko Carstens #include <linux/uaccess.h>
1188df125fSHeiko Carstens #include <linux/kernel.h>
1288df125fSHeiko Carstens #include <linux/types.h>
1388df125fSHeiko Carstens #include <linux/errno.h>
147f0bf656SMichael Holzheu #include <linux/gfp.h>
15b2a68c23SMichael Holzheu #include <linux/cpu.h>
16a0616cdeSDavid Howells #include <asm/ctl_reg.h>
1763df41d6SHeiko Carstens #include <asm/io.h>
1888df125fSHeiko Carstens 
19*8a5d8473SHeiko Carstens static notrace long s390_kernel_write_odd(void *dst, const void *src, size_t size)
2088df125fSHeiko Carstens {
2188df125fSHeiko Carstens 	unsigned long count, aligned;
2288df125fSHeiko Carstens 	int offset, mask;
2388df125fSHeiko Carstens 	int rc = -EFAULT;
2488df125fSHeiko Carstens 
2588df125fSHeiko Carstens 	aligned = (unsigned long) dst & ~3UL;
2688df125fSHeiko Carstens 	offset = (unsigned long) dst & 3;
2788df125fSHeiko Carstens 	count = min_t(unsigned long, 4 - offset, size);
2888df125fSHeiko Carstens 	mask = (0xf << (4 - count)) & 0xf;
2988df125fSHeiko Carstens 	mask >>= offset;
3088df125fSHeiko Carstens 	asm volatile(
3188df125fSHeiko Carstens 		"	bras	1,0f\n"
3288df125fSHeiko Carstens 		"	icm	0,0,0(%3)\n"
3388df125fSHeiko Carstens 		"0:	l	0,0(%1)\n"
3488df125fSHeiko Carstens 		"	lra	%1,0(%1)\n"
3588df125fSHeiko Carstens 		"1:	ex	%2,0(1)\n"
3688df125fSHeiko Carstens 		"2:	stura	0,%1\n"
3788df125fSHeiko Carstens 		"	la	%0,0\n"
3888df125fSHeiko Carstens 		"3:\n"
3988df125fSHeiko Carstens 		EX_TABLE(0b,3b) EX_TABLE(1b,3b) EX_TABLE(2b,3b)
4088df125fSHeiko Carstens 		: "+d" (rc), "+a" (aligned)
4188df125fSHeiko Carstens 		: "a" (mask), "a" (src) : "cc", "memory", "0", "1");
4288df125fSHeiko Carstens 	return rc ? rc : count;
4388df125fSHeiko Carstens }
4488df125fSHeiko Carstens 
45*8a5d8473SHeiko Carstens /*
46*8a5d8473SHeiko Carstens  * s390_kernel_write - write to kernel memory bypassing DAT
47*8a5d8473SHeiko Carstens  * @dst: destination address
48*8a5d8473SHeiko Carstens  * @src: source address
49*8a5d8473SHeiko Carstens  * @size: number of bytes to copy
50*8a5d8473SHeiko Carstens  *
51*8a5d8473SHeiko Carstens  * This function writes to kernel memory bypassing DAT and possible page table
52*8a5d8473SHeiko Carstens  * write protection. It writes to the destination using the sturg instruction.
53*8a5d8473SHeiko Carstens  * Therefore we have a read-modify-write sequence: the function reads four
54*8a5d8473SHeiko Carstens  * bytes from destination at a four byte boundary, modifies the bytes
55*8a5d8473SHeiko Carstens  * requested and writes the result back in a loop.
56*8a5d8473SHeiko Carstens  *
57*8a5d8473SHeiko Carstens  * Note: this means that this function may not be called concurrently on
58*8a5d8473SHeiko Carstens  *	 several cpus with overlapping words, since this may potentially
59*8a5d8473SHeiko Carstens  *	 cause data corruption.
60*8a5d8473SHeiko Carstens  */
61*8a5d8473SHeiko Carstens void notrace s390_kernel_write(void *dst, const void *src, size_t size)
6288df125fSHeiko Carstens {
6388df125fSHeiko Carstens 	long copied = 0;
6488df125fSHeiko Carstens 
6588df125fSHeiko Carstens 	while (size) {
66*8a5d8473SHeiko Carstens 		copied = s390_kernel_write_odd(dst, src, size);
6788df125fSHeiko Carstens 		if (copied < 0)
6888df125fSHeiko Carstens 			break;
6988df125fSHeiko Carstens 		dst += copied;
7088df125fSHeiko Carstens 		src += copied;
7188df125fSHeiko Carstens 		size -= copied;
7288df125fSHeiko Carstens 	}
7388df125fSHeiko Carstens }
7492fe3132SMichael Holzheu 
75b785e0d0SMichael Holzheu static int __memcpy_real(void *dest, void *src, size_t count)
7692fe3132SMichael Holzheu {
7792fe3132SMichael Holzheu 	register unsigned long _dest asm("2") = (unsigned long) dest;
7892fe3132SMichael Holzheu 	register unsigned long _len1 asm("3") = (unsigned long) count;
7992fe3132SMichael Holzheu 	register unsigned long _src  asm("4") = (unsigned long) src;
8092fe3132SMichael Holzheu 	register unsigned long _len2 asm("5") = (unsigned long) count;
8192fe3132SMichael Holzheu 	int rc = -EFAULT;
8292fe3132SMichael Holzheu 
8392fe3132SMichael Holzheu 	asm volatile (
8492fe3132SMichael Holzheu 		"0:	mvcle	%1,%2,0x0\n"
8592fe3132SMichael Holzheu 		"1:	jo	0b\n"
8692fe3132SMichael Holzheu 		"	lhi	%0,0x0\n"
8792fe3132SMichael Holzheu 		"2:\n"
8892fe3132SMichael Holzheu 		EX_TABLE(1b,2b)
8992fe3132SMichael Holzheu 		: "+d" (rc), "+d" (_dest), "+d" (_src), "+d" (_len1),
9092fe3132SMichael Holzheu 		  "+d" (_len2), "=m" (*((long *) dest))
9192fe3132SMichael Holzheu 		: "m" (*((long *) src))
9292fe3132SMichael Holzheu 		: "cc", "memory");
93b785e0d0SMichael Holzheu 	return rc;
94b785e0d0SMichael Holzheu }
95b785e0d0SMichael Holzheu 
96b785e0d0SMichael Holzheu /*
97b785e0d0SMichael Holzheu  * Copy memory in real mode (kernel to kernel)
98b785e0d0SMichael Holzheu  */
99b785e0d0SMichael Holzheu int memcpy_real(void *dest, void *src, size_t count)
100b785e0d0SMichael Holzheu {
101b785e0d0SMichael Holzheu 	unsigned long flags;
102b785e0d0SMichael Holzheu 	int rc;
103b785e0d0SMichael Holzheu 
104b785e0d0SMichael Holzheu 	if (!count)
105b785e0d0SMichael Holzheu 		return 0;
106b785e0d0SMichael Holzheu 	local_irq_save(flags);
107b785e0d0SMichael Holzheu 	__arch_local_irq_stnsm(0xfbUL);
108b785e0d0SMichael Holzheu 	rc = __memcpy_real(dest, src, count);
109b785e0d0SMichael Holzheu 	local_irq_restore(flags);
11092fe3132SMichael Holzheu 	return rc;
11192fe3132SMichael Holzheu }
1127dd6b334SMichael Holzheu 
1137dd6b334SMichael Holzheu /*
11473bf463eSMichael Holzheu  * Copy memory in absolute mode (kernel to kernel)
1157dd6b334SMichael Holzheu  */
11673bf463eSMichael Holzheu void memcpy_absolute(void *dest, void *src, size_t count)
1177dd6b334SMichael Holzheu {
11873bf463eSMichael Holzheu 	unsigned long cr0, flags, prefix;
1197dd6b334SMichael Holzheu 
12073bf463eSMichael Holzheu 	flags = arch_local_irq_save();
1217dd6b334SMichael Holzheu 	__ctl_store(cr0, 0, 0);
1227dd6b334SMichael Holzheu 	__ctl_clear_bit(0, 28); /* disable lowcore protection */
12373bf463eSMichael Holzheu 	prefix = store_prefix();
12473bf463eSMichael Holzheu 	if (prefix) {
12573bf463eSMichael Holzheu 		local_mcck_disable();
12673bf463eSMichael Holzheu 		set_prefix(0);
12773bf463eSMichael Holzheu 		memcpy(dest, src, count);
12873bf463eSMichael Holzheu 		set_prefix(prefix);
12973bf463eSMichael Holzheu 		local_mcck_enable();
13073bf463eSMichael Holzheu 	} else {
13173bf463eSMichael Holzheu 		memcpy(dest, src, count);
13273bf463eSMichael Holzheu 	}
1337dd6b334SMichael Holzheu 	__ctl_load(cr0, 0, 0);
13473bf463eSMichael Holzheu 	arch_local_irq_restore(flags);
1357dd6b334SMichael Holzheu }
1367f0bf656SMichael Holzheu 
1377f0bf656SMichael Holzheu /*
1387f0bf656SMichael Holzheu  * Copy memory from kernel (real) to user (virtual)
1397f0bf656SMichael Holzheu  */
140211deca6SHeiko Carstens int copy_to_user_real(void __user *dest, void *src, unsigned long count)
1417f0bf656SMichael Holzheu {
1427f0bf656SMichael Holzheu 	int offs = 0, size, rc;
1437f0bf656SMichael Holzheu 	char *buf;
1447f0bf656SMichael Holzheu 
1457f0bf656SMichael Holzheu 	buf = (char *) __get_free_page(GFP_KERNEL);
1467f0bf656SMichael Holzheu 	if (!buf)
1477f0bf656SMichael Holzheu 		return -ENOMEM;
1487f0bf656SMichael Holzheu 	rc = -EFAULT;
1497f0bf656SMichael Holzheu 	while (offs < count) {
1507f0bf656SMichael Holzheu 		size = min(PAGE_SIZE, count - offs);
1517f0bf656SMichael Holzheu 		if (memcpy_real(buf, src + offs, size))
1527f0bf656SMichael Holzheu 			goto out;
1537f0bf656SMichael Holzheu 		if (copy_to_user(dest + offs, buf, size))
1547f0bf656SMichael Holzheu 			goto out;
1557f0bf656SMichael Holzheu 		offs += size;
1567f0bf656SMichael Holzheu 	}
1577f0bf656SMichael Holzheu 	rc = 0;
1587f0bf656SMichael Holzheu out:
1597f0bf656SMichael Holzheu 	free_page((unsigned long) buf);
1607f0bf656SMichael Holzheu 	return rc;
1617f0bf656SMichael Holzheu }
1627f0bf656SMichael Holzheu 
1637f0bf656SMichael Holzheu /*
164b2a68c23SMichael Holzheu  * Check if physical address is within prefix or zero page
165b2a68c23SMichael Holzheu  */
166b2a68c23SMichael Holzheu static int is_swapped(unsigned long addr)
167b2a68c23SMichael Holzheu {
168b2a68c23SMichael Holzheu 	unsigned long lc;
169b2a68c23SMichael Holzheu 	int cpu;
170b2a68c23SMichael Holzheu 
171b2a68c23SMichael Holzheu 	if (addr < sizeof(struct _lowcore))
172b2a68c23SMichael Holzheu 		return 1;
173b2a68c23SMichael Holzheu 	for_each_online_cpu(cpu) {
174b2a68c23SMichael Holzheu 		lc = (unsigned long) lowcore_ptr[cpu];
175b2a68c23SMichael Holzheu 		if (addr > lc + sizeof(struct _lowcore) - 1 || addr < lc)
176b2a68c23SMichael Holzheu 			continue;
177b2a68c23SMichael Holzheu 		return 1;
178b2a68c23SMichael Holzheu 	}
179b2a68c23SMichael Holzheu 	return 0;
180b2a68c23SMichael Holzheu }
181b2a68c23SMichael Holzheu 
182b2a68c23SMichael Holzheu /*
183b2a68c23SMichael Holzheu  * Convert a physical pointer for /dev/mem access
184b2a68c23SMichael Holzheu  *
185b2a68c23SMichael Holzheu  * For swapped prefix pages a new buffer is returned that contains a copy of
186b2a68c23SMichael Holzheu  * the absolute memory. The buffer size is maximum one page large.
187b2a68c23SMichael Holzheu  */
1884707a341SThierry Reding void *xlate_dev_mem_ptr(phys_addr_t addr)
189b2a68c23SMichael Holzheu {
190b2a68c23SMichael Holzheu 	void *bounce = (void *) addr;
191b2a68c23SMichael Holzheu 	unsigned long size;
192b2a68c23SMichael Holzheu 
193b2a68c23SMichael Holzheu 	get_online_cpus();
194b2a68c23SMichael Holzheu 	preempt_disable();
195b2a68c23SMichael Holzheu 	if (is_swapped(addr)) {
196b2a68c23SMichael Holzheu 		size = PAGE_SIZE - (addr & ~PAGE_MASK);
197b2a68c23SMichael Holzheu 		bounce = (void *) __get_free_page(GFP_ATOMIC);
198b2a68c23SMichael Holzheu 		if (bounce)
19973bf463eSMichael Holzheu 			memcpy_absolute(bounce, (void *) addr, size);
200b2a68c23SMichael Holzheu 	}
201b2a68c23SMichael Holzheu 	preempt_enable();
202b2a68c23SMichael Holzheu 	put_online_cpus();
203b2a68c23SMichael Holzheu 	return bounce;
204b2a68c23SMichael Holzheu }
205b2a68c23SMichael Holzheu 
206b2a68c23SMichael Holzheu /*
207b2a68c23SMichael Holzheu  * Free converted buffer for /dev/mem access (if necessary)
208b2a68c23SMichael Holzheu  */
2094707a341SThierry Reding void unxlate_dev_mem_ptr(phys_addr_t addr, void *buf)
210b2a68c23SMichael Holzheu {
211b2a68c23SMichael Holzheu 	if ((void *) addr != buf)
212b2a68c23SMichael Holzheu 		free_page((unsigned long) buf);
213b2a68c23SMichael Holzheu }
214