Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Access kernel memory without faulting -- s390 specific implementation. |
| 3 | * |
Heiko Carstens | 3c1a3bc | 2015-03-13 13:13:36 +0100 | [diff] [blame] | 4 | * Copyright IBM Corp. 2009, 2015 |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 5 | * |
| 6 | * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>, |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <linux/uaccess.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/errno.h> |
Michael Holzheu | 7f0bf65 | 2011-10-30 15:16:39 +0100 | [diff] [blame] | 14 | #include <linux/gfp.h> |
Michael Holzheu | b2a68c2 | 2012-05-09 16:27:36 +0200 | [diff] [blame] | 15 | #include <linux/cpu.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 16 | #include <asm/ctl_reg.h> |
Heiko Carstens | 63df41d6 | 2013-09-06 19:10:48 +0200 | [diff] [blame] | 17 | #include <asm/io.h> |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 18 | |
Heiko Carstens | 8a5d847 | 2015-03-13 12:55:56 +0100 | [diff] [blame] | 19 | static notrace long s390_kernel_write_odd(void *dst, const void *src, size_t size) |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 20 | { |
Heiko Carstens | 3c1a3bc | 2015-03-13 13:13:36 +0100 | [diff] [blame] | 21 | unsigned long aligned, offset, count; |
| 22 | char tmp[8]; |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 23 | |
Heiko Carstens | 3c1a3bc | 2015-03-13 13:13:36 +0100 | [diff] [blame] | 24 | aligned = (unsigned long) dst & ~7UL; |
| 25 | offset = (unsigned long) dst & 7UL; |
| 26 | size = min(8UL - offset, size); |
| 27 | count = size - 1; |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 28 | asm volatile( |
| 29 | " bras 1,0f\n" |
Heiko Carstens | 3c1a3bc | 2015-03-13 13:13:36 +0100 | [diff] [blame] | 30 | " mvc 0(1,%4),0(%5)\n" |
| 31 | "0: mvc 0(8,%3),0(%0)\n" |
| 32 | " ex %1,0(1)\n" |
| 33 | " lg %1,0(%3)\n" |
| 34 | " lra %0,0(%0)\n" |
| 35 | " sturg %1,%0\n" |
| 36 | : "+&a" (aligned), "+&a" (count), "=m" (tmp) |
| 37 | : "a" (&tmp), "a" (&tmp[offset]), "a" (src) |
| 38 | : "cc", "memory", "1"); |
| 39 | return size; |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Heiko Carstens | 8a5d847 | 2015-03-13 12:55:56 +0100 | [diff] [blame] | 42 | /* |
| 43 | * s390_kernel_write - write to kernel memory bypassing DAT |
| 44 | * @dst: destination address |
| 45 | * @src: source address |
| 46 | * @size: number of bytes to copy |
| 47 | * |
| 48 | * This function writes to kernel memory bypassing DAT and possible page table |
| 49 | * write protection. It writes to the destination using the sturg instruction. |
Heiko Carstens | 3c1a3bc | 2015-03-13 13:13:36 +0100 | [diff] [blame] | 50 | * Therefore we have a read-modify-write sequence: the function reads eight |
| 51 | * bytes from destination at an eight byte boundary, modifies the bytes |
Heiko Carstens | 8a5d847 | 2015-03-13 12:55:56 +0100 | [diff] [blame] | 52 | * requested and writes the result back in a loop. |
| 53 | * |
| 54 | * Note: this means that this function may not be called concurrently on |
| 55 | * several cpus with overlapping words, since this may potentially |
| 56 | * cause data corruption. |
| 57 | */ |
| 58 | void notrace s390_kernel_write(void *dst, const void *src, size_t size) |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 59 | { |
Heiko Carstens | 3c1a3bc | 2015-03-13 13:13:36 +0100 | [diff] [blame] | 60 | long copied; |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 61 | |
| 62 | while (size) { |
Heiko Carstens | 8a5d847 | 2015-03-13 12:55:56 +0100 | [diff] [blame] | 63 | copied = s390_kernel_write_odd(dst, src, size); |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 64 | dst += copied; |
| 65 | src += copied; |
| 66 | size -= copied; |
| 67 | } |
Heiko Carstens | 88df125 | 2009-06-12 10:26:42 +0200 | [diff] [blame] | 68 | } |
Michael Holzheu | 92fe313 | 2010-03-24 11:49:50 +0100 | [diff] [blame] | 69 | |
Michael Holzheu | b785e0d | 2012-04-11 14:28:06 +0200 | [diff] [blame] | 70 | static int __memcpy_real(void *dest, void *src, size_t count) |
Michael Holzheu | 92fe313 | 2010-03-24 11:49:50 +0100 | [diff] [blame] | 71 | { |
| 72 | register unsigned long _dest asm("2") = (unsigned long) dest; |
| 73 | register unsigned long _len1 asm("3") = (unsigned long) count; |
| 74 | register unsigned long _src asm("4") = (unsigned long) src; |
| 75 | register unsigned long _len2 asm("5") = (unsigned long) count; |
Michael Holzheu | 92fe313 | 2010-03-24 11:49:50 +0100 | [diff] [blame] | 76 | int rc = -EFAULT; |
| 77 | |
Michael Holzheu | 92fe313 | 2010-03-24 11:49:50 +0100 | [diff] [blame] | 78 | asm volatile ( |
| 79 | "0: mvcle %1,%2,0x0\n" |
| 80 | "1: jo 0b\n" |
| 81 | " lhi %0,0x0\n" |
| 82 | "2:\n" |
| 83 | EX_TABLE(1b,2b) |
| 84 | : "+d" (rc), "+d" (_dest), "+d" (_src), "+d" (_len1), |
| 85 | "+d" (_len2), "=m" (*((long *) dest)) |
| 86 | : "m" (*((long *) src)) |
| 87 | : "cc", "memory"); |
Michael Holzheu | b785e0d | 2012-04-11 14:28:06 +0200 | [diff] [blame] | 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Copy memory in real mode (kernel to kernel) |
| 93 | */ |
| 94 | int memcpy_real(void *dest, void *src, size_t count) |
| 95 | { |
Heiko Carstens | 52499d9 | 2016-02-12 12:40:31 +0100 | [diff] [blame] | 96 | int irqs_disabled, rc; |
Michael Holzheu | b785e0d | 2012-04-11 14:28:06 +0200 | [diff] [blame] | 97 | unsigned long flags; |
Michael Holzheu | b785e0d | 2012-04-11 14:28:06 +0200 | [diff] [blame] | 98 | |
| 99 | if (!count) |
| 100 | return 0; |
Heiko Carstens | 52499d9 | 2016-02-12 12:40:31 +0100 | [diff] [blame] | 101 | flags = __arch_local_irq_stnsm(0xf8UL); |
| 102 | irqs_disabled = arch_irqs_disabled_flags(flags); |
| 103 | if (!irqs_disabled) |
| 104 | trace_hardirqs_off(); |
Michael Holzheu | b785e0d | 2012-04-11 14:28:06 +0200 | [diff] [blame] | 105 | rc = __memcpy_real(dest, src, count); |
Heiko Carstens | 52499d9 | 2016-02-12 12:40:31 +0100 | [diff] [blame] | 106 | if (!irqs_disabled) |
Christian Borntraeger | 0986d97 | 2016-02-09 16:23:39 +0100 | [diff] [blame] | 107 | trace_hardirqs_on(); |
| 108 | __arch_local_irq_ssm(flags); |
Michael Holzheu | 92fe313 | 2010-03-24 11:49:50 +0100 | [diff] [blame] | 109 | return rc; |
| 110 | } |
Michael Holzheu | 7dd6b33 | 2011-08-03 16:44:19 +0200 | [diff] [blame] | 111 | |
| 112 | /* |
Michael Holzheu | 73bf463 | 2012-05-24 14:35:16 +0200 | [diff] [blame] | 113 | * Copy memory in absolute mode (kernel to kernel) |
Michael Holzheu | 7dd6b33 | 2011-08-03 16:44:19 +0200 | [diff] [blame] | 114 | */ |
Michael Holzheu | 73bf463 | 2012-05-24 14:35:16 +0200 | [diff] [blame] | 115 | void memcpy_absolute(void *dest, void *src, size_t count) |
Michael Holzheu | 7dd6b33 | 2011-08-03 16:44:19 +0200 | [diff] [blame] | 116 | { |
Michael Holzheu | 73bf463 | 2012-05-24 14:35:16 +0200 | [diff] [blame] | 117 | unsigned long cr0, flags, prefix; |
Michael Holzheu | 7dd6b33 | 2011-08-03 16:44:19 +0200 | [diff] [blame] | 118 | |
Michael Holzheu | 73bf463 | 2012-05-24 14:35:16 +0200 | [diff] [blame] | 119 | flags = arch_local_irq_save(); |
Michael Holzheu | 7dd6b33 | 2011-08-03 16:44:19 +0200 | [diff] [blame] | 120 | __ctl_store(cr0, 0, 0); |
| 121 | __ctl_clear_bit(0, 28); /* disable lowcore protection */ |
Michael Holzheu | 73bf463 | 2012-05-24 14:35:16 +0200 | [diff] [blame] | 122 | prefix = store_prefix(); |
| 123 | if (prefix) { |
| 124 | local_mcck_disable(); |
| 125 | set_prefix(0); |
| 126 | memcpy(dest, src, count); |
| 127 | set_prefix(prefix); |
| 128 | local_mcck_enable(); |
| 129 | } else { |
| 130 | memcpy(dest, src, count); |
| 131 | } |
Michael Holzheu | 7dd6b33 | 2011-08-03 16:44:19 +0200 | [diff] [blame] | 132 | __ctl_load(cr0, 0, 0); |
Michael Holzheu | 73bf463 | 2012-05-24 14:35:16 +0200 | [diff] [blame] | 133 | arch_local_irq_restore(flags); |
Michael Holzheu | 7dd6b33 | 2011-08-03 16:44:19 +0200 | [diff] [blame] | 134 | } |
Michael Holzheu | 7f0bf65 | 2011-10-30 15:16:39 +0100 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Copy memory from kernel (real) to user (virtual) |
| 138 | */ |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 139 | int copy_to_user_real(void __user *dest, void *src, unsigned long count) |
Michael Holzheu | 7f0bf65 | 2011-10-30 15:16:39 +0100 | [diff] [blame] | 140 | { |
| 141 | int offs = 0, size, rc; |
| 142 | char *buf; |
| 143 | |
| 144 | buf = (char *) __get_free_page(GFP_KERNEL); |
| 145 | if (!buf) |
| 146 | return -ENOMEM; |
| 147 | rc = -EFAULT; |
| 148 | while (offs < count) { |
| 149 | size = min(PAGE_SIZE, count - offs); |
| 150 | if (memcpy_real(buf, src + offs, size)) |
| 151 | goto out; |
| 152 | if (copy_to_user(dest + offs, buf, size)) |
| 153 | goto out; |
| 154 | offs += size; |
| 155 | } |
| 156 | rc = 0; |
| 157 | out: |
| 158 | free_page((unsigned long) buf); |
| 159 | return rc; |
| 160 | } |
| 161 | |
| 162 | /* |
Michael Holzheu | b2a68c2 | 2012-05-09 16:27:36 +0200 | [diff] [blame] | 163 | * Check if physical address is within prefix or zero page |
| 164 | */ |
| 165 | static int is_swapped(unsigned long addr) |
| 166 | { |
| 167 | unsigned long lc; |
| 168 | int cpu; |
| 169 | |
Heiko Carstens | c667aea | 2015-12-31 10:29:00 +0100 | [diff] [blame] | 170 | if (addr < sizeof(struct lowcore)) |
Michael Holzheu | b2a68c2 | 2012-05-09 16:27:36 +0200 | [diff] [blame] | 171 | return 1; |
| 172 | for_each_online_cpu(cpu) { |
| 173 | lc = (unsigned long) lowcore_ptr[cpu]; |
Heiko Carstens | c667aea | 2015-12-31 10:29:00 +0100 | [diff] [blame] | 174 | if (addr > lc + sizeof(struct lowcore) - 1 || addr < lc) |
Michael Holzheu | b2a68c2 | 2012-05-09 16:27:36 +0200 | [diff] [blame] | 175 | continue; |
| 176 | return 1; |
| 177 | } |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /* |
Michael Holzheu | b2a68c2 | 2012-05-09 16:27:36 +0200 | [diff] [blame] | 182 | * Convert a physical pointer for /dev/mem access |
| 183 | * |
| 184 | * For swapped prefix pages a new buffer is returned that contains a copy of |
| 185 | * the absolute memory. The buffer size is maximum one page large. |
| 186 | */ |
Thierry Reding | 4707a34 | 2014-07-28 17:20:33 +0200 | [diff] [blame] | 187 | void *xlate_dev_mem_ptr(phys_addr_t addr) |
Michael Holzheu | b2a68c2 | 2012-05-09 16:27:36 +0200 | [diff] [blame] | 188 | { |
| 189 | void *bounce = (void *) addr; |
| 190 | unsigned long size; |
| 191 | |
| 192 | get_online_cpus(); |
| 193 | preempt_disable(); |
| 194 | if (is_swapped(addr)) { |
| 195 | size = PAGE_SIZE - (addr & ~PAGE_MASK); |
| 196 | bounce = (void *) __get_free_page(GFP_ATOMIC); |
| 197 | if (bounce) |
Michael Holzheu | 73bf463 | 2012-05-24 14:35:16 +0200 | [diff] [blame] | 198 | memcpy_absolute(bounce, (void *) addr, size); |
Michael Holzheu | b2a68c2 | 2012-05-09 16:27:36 +0200 | [diff] [blame] | 199 | } |
| 200 | preempt_enable(); |
| 201 | put_online_cpus(); |
| 202 | return bounce; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Free converted buffer for /dev/mem access (if necessary) |
| 207 | */ |
Thierry Reding | 4707a34 | 2014-07-28 17:20:33 +0200 | [diff] [blame] | 208 | void unxlate_dev_mem_ptr(phys_addr_t addr, void *buf) |
Michael Holzheu | b2a68c2 | 2012-05-09 16:27:36 +0200 | [diff] [blame] | 209 | { |
| 210 | if ((void *) addr != buf) |
| 211 | free_page((unsigned long) buf); |
| 212 | } |