maccess.c 4.8 KB

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