maccess.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Access kernel memory without faulting -- s390 specific implementation.
  4. *
  5. * Copyright IBM Corp. 2009, 2015
  6. *
  7. */
  8. #include <linux/uaccess.h>
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/gfp.h>
  13. #include <linux/cpu.h>
  14. #include <linux/uio.h>
  15. #include <asm/asm-extable.h>
  16. #include <asm/ctl_reg.h>
  17. #include <asm/io.h>
  18. #include <asm/abs_lowcore.h>
  19. #include <asm/stacktrace.h>
  20. #include <asm/maccess.h>
  21. unsigned long __bootdata_preserved(__memcpy_real_area);
  22. static __ro_after_init pte_t *memcpy_real_ptep;
  23. static DEFINE_MUTEX(memcpy_real_mutex);
  24. static notrace long s390_kernel_write_odd(void *dst, const void *src, size_t size)
  25. {
  26. unsigned long aligned, offset, count;
  27. char tmp[8];
  28. aligned = (unsigned long) dst & ~7UL;
  29. offset = (unsigned long) dst & 7UL;
  30. size = min(8UL - offset, size);
  31. count = size - 1;
  32. asm volatile(
  33. " bras 1,0f\n"
  34. " mvc 0(1,%4),0(%5)\n"
  35. "0: mvc 0(8,%3),0(%0)\n"
  36. " ex %1,0(1)\n"
  37. " lg %1,0(%3)\n"
  38. " lra %0,0(%0)\n"
  39. " sturg %1,%0\n"
  40. : "+&a" (aligned), "+&a" (count), "=m" (tmp)
  41. : "a" (&tmp), "a" (&tmp[offset]), "a" (src)
  42. : "cc", "memory", "1");
  43. return size;
  44. }
  45. /*
  46. * s390_kernel_write - write to kernel memory bypassing DAT
  47. * @dst: destination address
  48. * @src: source address
  49. * @size: number of bytes to copy
  50. *
  51. * This function writes to kernel memory bypassing DAT and possible page table
  52. * write protection. It writes to the destination using the sturg instruction.
  53. * Therefore we have a read-modify-write sequence: the function reads eight
  54. * bytes from destination at an eight byte boundary, modifies the bytes
  55. * requested and writes the result back in a loop.
  56. */
  57. static DEFINE_SPINLOCK(s390_kernel_write_lock);
  58. notrace void *s390_kernel_write(void *dst, const void *src, size_t size)
  59. {
  60. void *tmp = dst;
  61. unsigned long flags;
  62. long copied;
  63. spin_lock_irqsave(&s390_kernel_write_lock, flags);
  64. if (!(flags & PSW_MASK_DAT)) {
  65. memcpy(dst, src, size);
  66. } else {
  67. while (size) {
  68. copied = s390_kernel_write_odd(tmp, src, size);
  69. tmp += copied;
  70. src += copied;
  71. size -= copied;
  72. }
  73. }
  74. spin_unlock_irqrestore(&s390_kernel_write_lock, flags);
  75. return dst;
  76. }
  77. void __init memcpy_real_init(void)
  78. {
  79. memcpy_real_ptep = vmem_get_alloc_pte(__memcpy_real_area, true);
  80. if (!memcpy_real_ptep)
  81. panic("Couldn't setup memcpy real area");
  82. }
  83. size_t memcpy_real_iter(struct iov_iter *iter, unsigned long src, size_t count)
  84. {
  85. size_t len, copied, res = 0;
  86. unsigned long phys, offset;
  87. void *chunk;
  88. pte_t pte;
  89. while (count) {
  90. phys = src & PAGE_MASK;
  91. offset = src & ~PAGE_MASK;
  92. chunk = (void *)(__memcpy_real_area + offset);
  93. len = min(count, PAGE_SIZE - offset);
  94. pte = mk_pte_phys(phys, PAGE_KERNEL_RO);
  95. mutex_lock(&memcpy_real_mutex);
  96. if (pte_val(pte) != pte_val(*memcpy_real_ptep)) {
  97. __ptep_ipte(__memcpy_real_area, memcpy_real_ptep, 0, 0, IPTE_GLOBAL);
  98. set_pte(memcpy_real_ptep, pte);
  99. }
  100. copied = copy_to_iter(chunk, len, iter);
  101. mutex_unlock(&memcpy_real_mutex);
  102. count -= copied;
  103. src += copied;
  104. res += copied;
  105. if (copied < len)
  106. break;
  107. }
  108. return res;
  109. }
  110. int memcpy_real(void *dest, unsigned long src, size_t count)
  111. {
  112. struct iov_iter iter;
  113. struct kvec kvec;
  114. kvec.iov_base = dest;
  115. kvec.iov_len = count;
  116. iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, count);
  117. if (memcpy_real_iter(&iter, src, count) < count)
  118. return -EFAULT;
  119. return 0;
  120. }
  121. /*
  122. * Find CPU that owns swapped prefix page
  123. */
  124. static int get_swapped_owner(phys_addr_t addr)
  125. {
  126. phys_addr_t lc;
  127. int cpu;
  128. for_each_online_cpu(cpu) {
  129. lc = virt_to_phys(lowcore_ptr[cpu]);
  130. if (addr > lc + sizeof(struct lowcore) - 1 || addr < lc)
  131. continue;
  132. return cpu;
  133. }
  134. return -1;
  135. }
  136. /*
  137. * Convert a physical pointer for /dev/mem access
  138. *
  139. * For swapped prefix pages a new buffer is returned that contains a copy of
  140. * the absolute memory. The buffer size is maximum one page large.
  141. */
  142. void *xlate_dev_mem_ptr(phys_addr_t addr)
  143. {
  144. void *ptr = phys_to_virt(addr);
  145. void *bounce = ptr;
  146. struct lowcore *abs_lc;
  147. unsigned long flags;
  148. unsigned long size;
  149. int this_cpu, cpu;
  150. cpus_read_lock();
  151. this_cpu = get_cpu();
  152. if (addr >= sizeof(struct lowcore)) {
  153. cpu = get_swapped_owner(addr);
  154. if (cpu < 0)
  155. goto out;
  156. }
  157. bounce = (void *)__get_free_page(GFP_ATOMIC);
  158. if (!bounce)
  159. goto out;
  160. size = PAGE_SIZE - (addr & ~PAGE_MASK);
  161. if (addr < sizeof(struct lowcore)) {
  162. abs_lc = get_abs_lowcore(&flags);
  163. ptr = (void *)abs_lc + addr;
  164. memcpy(bounce, ptr, size);
  165. put_abs_lowcore(abs_lc, flags);
  166. } else if (cpu == this_cpu) {
  167. ptr = (void *)(addr - virt_to_phys(lowcore_ptr[cpu]));
  168. memcpy(bounce, ptr, size);
  169. } else {
  170. memcpy(bounce, ptr, size);
  171. }
  172. out:
  173. put_cpu();
  174. cpus_read_unlock();
  175. return bounce;
  176. }
  177. /*
  178. * Free converted buffer for /dev/mem access (if necessary)
  179. */
  180. void unxlate_dev_mem_ptr(phys_addr_t addr, void *ptr)
  181. {
  182. if (addr != virt_to_phys(ptr))
  183. free_page((unsigned long)ptr);
  184. }