maccess.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <linux/io.h>
  16. #include <asm/asm-extable.h>
  17. #include <asm/abs_lowcore.h>
  18. #include <asm/stacktrace.h>
  19. #include <asm/maccess.h>
  20. #include <asm/ctlreg.h>
  21. unsigned long __bootdata_preserved(__memcpy_real_area);
  22. pte_t *__bootdata_preserved(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. while (size) {
  65. copied = s390_kernel_write_odd(tmp, src, size);
  66. tmp += copied;
  67. src += copied;
  68. size -= copied;
  69. }
  70. spin_unlock_irqrestore(&s390_kernel_write_lock, flags);
  71. return dst;
  72. }
  73. size_t memcpy_real_iter(struct iov_iter *iter, unsigned long src, size_t count)
  74. {
  75. size_t len, copied, res = 0;
  76. unsigned long phys, offset;
  77. void *chunk;
  78. pte_t pte;
  79. BUILD_BUG_ON(MEMCPY_REAL_SIZE != PAGE_SIZE);
  80. while (count) {
  81. phys = src & MEMCPY_REAL_MASK;
  82. offset = src & ~MEMCPY_REAL_MASK;
  83. chunk = (void *)(__memcpy_real_area + offset);
  84. len = min(count, MEMCPY_REAL_SIZE - offset);
  85. pte = mk_pte_phys(phys, PAGE_KERNEL_RO);
  86. mutex_lock(&memcpy_real_mutex);
  87. if (pte_val(pte) != pte_val(*memcpy_real_ptep)) {
  88. __ptep_ipte(__memcpy_real_area, memcpy_real_ptep, 0, 0, IPTE_GLOBAL);
  89. set_pte(memcpy_real_ptep, pte);
  90. }
  91. copied = copy_to_iter(chunk, len, iter);
  92. mutex_unlock(&memcpy_real_mutex);
  93. count -= copied;
  94. src += copied;
  95. res += copied;
  96. if (copied < len)
  97. break;
  98. }
  99. return res;
  100. }
  101. int memcpy_real(void *dest, unsigned long src, size_t count)
  102. {
  103. struct iov_iter iter;
  104. struct kvec kvec;
  105. kvec.iov_base = dest;
  106. kvec.iov_len = count;
  107. iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, count);
  108. if (memcpy_real_iter(&iter, src, count) < count)
  109. return -EFAULT;
  110. return 0;
  111. }
  112. /*
  113. * Find CPU that owns swapped prefix page
  114. */
  115. static int get_swapped_owner(phys_addr_t addr)
  116. {
  117. phys_addr_t lc;
  118. int cpu;
  119. for_each_online_cpu(cpu) {
  120. lc = virt_to_phys(lowcore_ptr[cpu]);
  121. if (addr > lc + sizeof(struct lowcore) - 1 || addr < lc)
  122. continue;
  123. return cpu;
  124. }
  125. return -1;
  126. }
  127. /*
  128. * Convert a physical pointer for /dev/mem access
  129. *
  130. * For swapped prefix pages a new buffer is returned that contains a copy of
  131. * the absolute memory. The buffer size is maximum one page large.
  132. */
  133. void *xlate_dev_mem_ptr(phys_addr_t addr)
  134. {
  135. void *ptr = phys_to_virt(addr);
  136. void *bounce = ptr;
  137. struct lowcore *abs_lc;
  138. unsigned long size;
  139. int this_cpu, cpu;
  140. cpus_read_lock();
  141. this_cpu = get_cpu();
  142. if (addr >= sizeof(struct lowcore)) {
  143. cpu = get_swapped_owner(addr);
  144. if (cpu < 0)
  145. goto out;
  146. }
  147. bounce = (void *)__get_free_page(GFP_ATOMIC);
  148. if (!bounce)
  149. goto out;
  150. size = PAGE_SIZE - (addr & ~PAGE_MASK);
  151. if (addr < sizeof(struct lowcore)) {
  152. abs_lc = get_abs_lowcore();
  153. ptr = (void *)abs_lc + addr;
  154. memcpy(bounce, ptr, size);
  155. put_abs_lowcore(abs_lc);
  156. } else if (cpu == this_cpu) {
  157. ptr = (void *)(addr - virt_to_phys(lowcore_ptr[cpu]));
  158. memcpy(bounce, ptr, size);
  159. } else {
  160. memcpy(bounce, ptr, size);
  161. }
  162. out:
  163. put_cpu();
  164. cpus_read_unlock();
  165. return bounce;
  166. }
  167. /*
  168. * Free converted buffer for /dev/mem access (if necessary)
  169. */
  170. void unxlate_dev_mem_ptr(phys_addr_t addr, void *ptr)
  171. {
  172. if (addr != virt_to_phys(ptr))
  173. free_page((unsigned long)ptr);
  174. }