uaccess_mvcos.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Optimized user space space access functions based on mvcos.
  3. *
  4. * Copyright IBM Corp. 2006
  5. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  6. * Gerald Schaefer (gerald.schaefer@de.ibm.com)
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/mm.h>
  10. #include <asm/uaccess.h>
  11. #include <asm/futex.h>
  12. #include "uaccess.h"
  13. #ifndef CONFIG_64BIT
  14. #define AHI "ahi"
  15. #define ALR "alr"
  16. #define CLR "clr"
  17. #define LHI "lhi"
  18. #define SLR "slr"
  19. #else
  20. #define AHI "aghi"
  21. #define ALR "algr"
  22. #define CLR "clgr"
  23. #define LHI "lghi"
  24. #define SLR "slgr"
  25. #endif
  26. static size_t copy_from_user_mvcos(size_t size, const void __user *ptr, void *x)
  27. {
  28. register unsigned long reg0 asm("0") = 0x81UL;
  29. unsigned long tmp1, tmp2;
  30. tmp1 = -4096UL;
  31. asm volatile(
  32. "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
  33. "9: jz 7f\n"
  34. "1:"ALR" %0,%3\n"
  35. " "SLR" %1,%3\n"
  36. " "SLR" %2,%3\n"
  37. " j 0b\n"
  38. "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
  39. " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
  40. " "SLR" %4,%1\n"
  41. " "CLR" %0,%4\n" /* copy crosses next page boundary? */
  42. " jnh 4f\n"
  43. "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
  44. "10:"SLR" %0,%4\n"
  45. " "ALR" %2,%4\n"
  46. "4:"LHI" %4,-1\n"
  47. " "ALR" %4,%0\n" /* copy remaining size, subtract 1 */
  48. " bras %3,6f\n" /* memset loop */
  49. " xc 0(1,%2),0(%2)\n"
  50. "5: xc 0(256,%2),0(%2)\n"
  51. " la %2,256(%2)\n"
  52. "6:"AHI" %4,-256\n"
  53. " jnm 5b\n"
  54. " ex %4,0(%3)\n"
  55. " j 8f\n"
  56. "7:"SLR" %0,%0\n"
  57. "8: \n"
  58. EX_TABLE(0b,2b) EX_TABLE(3b,4b) EX_TABLE(9b,2b) EX_TABLE(10b,4b)
  59. : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
  60. : "d" (reg0) : "cc", "memory");
  61. return size;
  62. }
  63. static size_t copy_to_user_mvcos(size_t size, void __user *ptr, const void *x)
  64. {
  65. register unsigned long reg0 asm("0") = 0x810000UL;
  66. unsigned long tmp1, tmp2;
  67. tmp1 = -4096UL;
  68. asm volatile(
  69. "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
  70. "6: jz 4f\n"
  71. "1:"ALR" %0,%3\n"
  72. " "SLR" %1,%3\n"
  73. " "SLR" %2,%3\n"
  74. " j 0b\n"
  75. "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
  76. " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
  77. " "SLR" %4,%1\n"
  78. " "CLR" %0,%4\n" /* copy crosses next page boundary? */
  79. " jnh 5f\n"
  80. "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
  81. "7:"SLR" %0,%4\n"
  82. " j 5f\n"
  83. "4:"SLR" %0,%0\n"
  84. "5: \n"
  85. EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
  86. : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
  87. : "d" (reg0) : "cc", "memory");
  88. return size;
  89. }
  90. static size_t copy_in_user_mvcos(size_t size, void __user *to,
  91. const void __user *from)
  92. {
  93. register unsigned long reg0 asm("0") = 0x810081UL;
  94. unsigned long tmp1, tmp2;
  95. tmp1 = -4096UL;
  96. /* FIXME: copy with reduced length. */
  97. asm volatile(
  98. "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
  99. " jz 2f\n"
  100. "1:"ALR" %0,%3\n"
  101. " "SLR" %1,%3\n"
  102. " "SLR" %2,%3\n"
  103. " j 0b\n"
  104. "2:"SLR" %0,%0\n"
  105. "3: \n"
  106. EX_TABLE(0b,3b)
  107. : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
  108. : "d" (reg0) : "cc", "memory");
  109. return size;
  110. }
  111. static size_t clear_user_mvcos(size_t size, void __user *to)
  112. {
  113. register unsigned long reg0 asm("0") = 0x810000UL;
  114. unsigned long tmp1, tmp2;
  115. tmp1 = -4096UL;
  116. asm volatile(
  117. "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
  118. " jz 4f\n"
  119. "1:"ALR" %0,%2\n"
  120. " "SLR" %1,%2\n"
  121. " j 0b\n"
  122. "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
  123. " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
  124. " "SLR" %3,%1\n"
  125. " "CLR" %0,%3\n" /* copy crosses next page boundary? */
  126. " jnh 5f\n"
  127. "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
  128. " "SLR" %0,%3\n"
  129. " j 5f\n"
  130. "4:"SLR" %0,%0\n"
  131. "5: \n"
  132. EX_TABLE(0b,2b) EX_TABLE(3b,5b)
  133. : "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
  134. : "a" (empty_zero_page), "d" (reg0) : "cc", "memory");
  135. return size;
  136. }
  137. static size_t strnlen_user_mvcos(size_t count, const char __user *src)
  138. {
  139. size_t done, len, offset, len_str;
  140. char buf[256];
  141. done = 0;
  142. do {
  143. offset = (size_t)src & ~PAGE_MASK;
  144. len = min(256UL, PAGE_SIZE - offset);
  145. len = min(count - done, len);
  146. if (copy_from_user_mvcos(len, src, buf))
  147. return 0;
  148. len_str = strnlen(buf, len);
  149. done += len_str;
  150. src += len_str;
  151. } while ((len_str == len) && (done < count));
  152. return done + 1;
  153. }
  154. static size_t strncpy_from_user_mvcos(size_t count, const char __user *src,
  155. char *dst)
  156. {
  157. size_t done, len, offset, len_str;
  158. if (unlikely(!count))
  159. return 0;
  160. done = 0;
  161. do {
  162. offset = (size_t)src & ~PAGE_MASK;
  163. len = min(count - done, PAGE_SIZE - offset);
  164. if (copy_from_user_mvcos(len, src, dst))
  165. return -EFAULT;
  166. len_str = strnlen(dst, len);
  167. done += len_str;
  168. src += len_str;
  169. dst += len_str;
  170. } while ((len_str == len) && (done < count));
  171. return done;
  172. }
  173. struct uaccess_ops uaccess_mvcos = {
  174. .copy_from_user = copy_from_user_mvcos,
  175. .copy_to_user = copy_to_user_mvcos,
  176. .copy_in_user = copy_in_user_mvcos,
  177. .clear_user = clear_user_mvcos,
  178. .strnlen_user = strnlen_user_mvcos,
  179. .strncpy_from_user = strncpy_from_user_mvcos,
  180. .futex_atomic_op = futex_atomic_op_pt,
  181. .futex_atomic_cmpxchg = futex_atomic_cmpxchg_pt,
  182. };