uaccess_mvcos.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_from_user_mvcos_check(size_t size, const void __user *ptr, void *x)
  64. {
  65. if (size <= 256)
  66. return copy_from_user_std(size, ptr, x);
  67. return copy_from_user_mvcos(size, ptr, x);
  68. }
  69. static size_t copy_to_user_mvcos(size_t size, void __user *ptr, const void *x)
  70. {
  71. register unsigned long reg0 asm("0") = 0x810000UL;
  72. unsigned long tmp1, tmp2;
  73. tmp1 = -4096UL;
  74. asm volatile(
  75. "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
  76. "6: jz 4f\n"
  77. "1:"ALR" %0,%3\n"
  78. " "SLR" %1,%3\n"
  79. " "SLR" %2,%3\n"
  80. " j 0b\n"
  81. "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
  82. " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
  83. " "SLR" %4,%1\n"
  84. " "CLR" %0,%4\n" /* copy crosses next page boundary? */
  85. " jnh 5f\n"
  86. "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
  87. "7:"SLR" %0,%4\n"
  88. " j 5f\n"
  89. "4:"SLR" %0,%0\n"
  90. "5: \n"
  91. EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
  92. : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
  93. : "d" (reg0) : "cc", "memory");
  94. return size;
  95. }
  96. static size_t copy_to_user_mvcos_check(size_t size, void __user *ptr,
  97. const void *x)
  98. {
  99. if (size <= 256)
  100. return copy_to_user_std(size, ptr, x);
  101. return copy_to_user_mvcos(size, ptr, x);
  102. }
  103. static size_t copy_in_user_mvcos(size_t size, void __user *to,
  104. const void __user *from)
  105. {
  106. register unsigned long reg0 asm("0") = 0x810081UL;
  107. unsigned long tmp1, tmp2;
  108. tmp1 = -4096UL;
  109. /* FIXME: copy with reduced length. */
  110. asm volatile(
  111. "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
  112. " jz 2f\n"
  113. "1:"ALR" %0,%3\n"
  114. " "SLR" %1,%3\n"
  115. " "SLR" %2,%3\n"
  116. " j 0b\n"
  117. "2:"SLR" %0,%0\n"
  118. "3: \n"
  119. EX_TABLE(0b,3b)
  120. : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
  121. : "d" (reg0) : "cc", "memory");
  122. return size;
  123. }
  124. static size_t clear_user_mvcos(size_t size, void __user *to)
  125. {
  126. register unsigned long reg0 asm("0") = 0x810000UL;
  127. unsigned long tmp1, tmp2;
  128. tmp1 = -4096UL;
  129. asm volatile(
  130. "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
  131. " jz 4f\n"
  132. "1:"ALR" %0,%2\n"
  133. " "SLR" %1,%2\n"
  134. " j 0b\n"
  135. "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
  136. " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
  137. " "SLR" %3,%1\n"
  138. " "CLR" %0,%3\n" /* copy crosses next page boundary? */
  139. " jnh 5f\n"
  140. "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
  141. " "SLR" %0,%3\n"
  142. " j 5f\n"
  143. "4:"SLR" %0,%0\n"
  144. "5: \n"
  145. EX_TABLE(0b,2b) EX_TABLE(3b,5b)
  146. : "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
  147. : "a" (empty_zero_page), "d" (reg0) : "cc", "memory");
  148. return size;
  149. }
  150. static size_t strnlen_user_mvcos(size_t count, const char __user *src)
  151. {
  152. char buf[256];
  153. int rc;
  154. size_t done, len, len_str;
  155. done = 0;
  156. do {
  157. len = min(count - done, (size_t) 256);
  158. rc = uaccess.copy_from_user(len, src + done, buf);
  159. if (unlikely(rc == len))
  160. return 0;
  161. len -= rc;
  162. len_str = strnlen(buf, len);
  163. done += len_str;
  164. } while ((len_str == len) && (done < count));
  165. return done + 1;
  166. }
  167. static size_t strncpy_from_user_mvcos(size_t count, const char __user *src,
  168. char *dst)
  169. {
  170. int rc;
  171. size_t done, len, len_str;
  172. done = 0;
  173. do {
  174. len = min(count - done, (size_t) 4096);
  175. rc = uaccess.copy_from_user(len, src + done, dst);
  176. if (unlikely(rc == len))
  177. return -EFAULT;
  178. len -= rc;
  179. len_str = strnlen(dst, len);
  180. done += len_str;
  181. } while ((len_str == len) && (done < count));
  182. return done;
  183. }
  184. struct uaccess_ops uaccess_mvcos = {
  185. .copy_from_user = copy_from_user_mvcos_check,
  186. .copy_from_user_small = copy_from_user_std,
  187. .copy_to_user = copy_to_user_mvcos_check,
  188. .copy_to_user_small = copy_to_user_std,
  189. .copy_in_user = copy_in_user_mvcos,
  190. .clear_user = clear_user_mvcos,
  191. .strnlen_user = strnlen_user_std,
  192. .strncpy_from_user = strncpy_from_user_std,
  193. .futex_atomic_op = futex_atomic_op_std,
  194. .futex_atomic_cmpxchg = futex_atomic_cmpxchg_std,
  195. };
  196. struct uaccess_ops uaccess_mvcos_switch = {
  197. .copy_from_user = copy_from_user_mvcos,
  198. .copy_from_user_small = copy_from_user_mvcos,
  199. .copy_to_user = copy_to_user_mvcos,
  200. .copy_to_user_small = copy_to_user_mvcos,
  201. .copy_in_user = copy_in_user_mvcos,
  202. .clear_user = clear_user_mvcos,
  203. .strnlen_user = strnlen_user_mvcos,
  204. .strncpy_from_user = strncpy_from_user_mvcos,
  205. .futex_atomic_op = futex_atomic_op_pt,
  206. .futex_atomic_cmpxchg = futex_atomic_cmpxchg_pt,
  207. };