uaccess.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Standard user space access functions based on mvcp/mvcs and doing
  3. * interesting things in the secondary space mode.
  4. *
  5. * Copyright IBM Corp. 2006,2014
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  7. * Gerald Schaefer (gerald.schaefer@de.ibm.com)
  8. */
  9. #include <linux/jump_label.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/export.h>
  12. #include <linux/errno.h>
  13. #include <linux/mm.h>
  14. #include <asm/mmu_context.h>
  15. #include <asm/facility.h>
  16. static DEFINE_STATIC_KEY_FALSE(have_mvcos);
  17. static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
  18. unsigned long size)
  19. {
  20. register unsigned long reg0 asm("0") = 0x81UL;
  21. unsigned long tmp1, tmp2;
  22. tmp1 = -4096UL;
  23. asm volatile(
  24. "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
  25. "6: jz 4f\n"
  26. "1: algr %0,%3\n"
  27. " slgr %1,%3\n"
  28. " slgr %2,%3\n"
  29. " j 0b\n"
  30. "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
  31. " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
  32. " slgr %4,%1\n"
  33. " clgr %0,%4\n" /* copy crosses next page boundary? */
  34. " jnh 5f\n"
  35. "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
  36. "7: slgr %0,%4\n"
  37. " j 5f\n"
  38. "4: slgr %0,%0\n"
  39. "5:\n"
  40. EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
  41. : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
  42. : "d" (reg0) : "cc", "memory");
  43. return size;
  44. }
  45. static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
  46. unsigned long size)
  47. {
  48. unsigned long tmp1, tmp2;
  49. load_kernel_asce();
  50. tmp1 = -256UL;
  51. asm volatile(
  52. " sacf 0\n"
  53. "0: mvcp 0(%0,%2),0(%1),%3\n"
  54. "7: jz 5f\n"
  55. "1: algr %0,%3\n"
  56. " la %1,256(%1)\n"
  57. " la %2,256(%2)\n"
  58. "2: mvcp 0(%0,%2),0(%1),%3\n"
  59. "8: jnz 1b\n"
  60. " j 5f\n"
  61. "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
  62. " lghi %3,-4096\n"
  63. " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
  64. " slgr %4,%1\n"
  65. " clgr %0,%4\n" /* copy crosses next page boundary? */
  66. " jnh 6f\n"
  67. "4: mvcp 0(%4,%2),0(%1),%3\n"
  68. "9: slgr %0,%4\n"
  69. " j 6f\n"
  70. "5: slgr %0,%0\n"
  71. "6: sacf 768\n"
  72. EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
  73. EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
  74. : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
  75. : : "cc", "memory");
  76. return size;
  77. }
  78. unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  79. {
  80. if (static_branch_likely(&have_mvcos))
  81. return copy_from_user_mvcos(to, from, n);
  82. return copy_from_user_mvcp(to, from, n);
  83. }
  84. EXPORT_SYMBOL(raw_copy_from_user);
  85. static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
  86. unsigned long size)
  87. {
  88. register unsigned long reg0 asm("0") = 0x810000UL;
  89. unsigned long tmp1, tmp2;
  90. tmp1 = -4096UL;
  91. asm volatile(
  92. "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
  93. "6: jz 4f\n"
  94. "1: algr %0,%3\n"
  95. " slgr %1,%3\n"
  96. " slgr %2,%3\n"
  97. " j 0b\n"
  98. "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
  99. " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
  100. " slgr %4,%1\n"
  101. " clgr %0,%4\n" /* copy crosses next page boundary? */
  102. " jnh 5f\n"
  103. "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
  104. "7: slgr %0,%4\n"
  105. " j 5f\n"
  106. "4: slgr %0,%0\n"
  107. "5:\n"
  108. EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
  109. : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
  110. : "d" (reg0) : "cc", "memory");
  111. return size;
  112. }
  113. static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
  114. unsigned long size)
  115. {
  116. unsigned long tmp1, tmp2;
  117. load_kernel_asce();
  118. tmp1 = -256UL;
  119. asm volatile(
  120. " sacf 0\n"
  121. "0: mvcs 0(%0,%1),0(%2),%3\n"
  122. "7: jz 5f\n"
  123. "1: algr %0,%3\n"
  124. " la %1,256(%1)\n"
  125. " la %2,256(%2)\n"
  126. "2: mvcs 0(%0,%1),0(%2),%3\n"
  127. "8: jnz 1b\n"
  128. " j 5f\n"
  129. "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
  130. " lghi %3,-4096\n"
  131. " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
  132. " slgr %4,%1\n"
  133. " clgr %0,%4\n" /* copy crosses next page boundary? */
  134. " jnh 6f\n"
  135. "4: mvcs 0(%4,%1),0(%2),%3\n"
  136. "9: slgr %0,%4\n"
  137. " j 6f\n"
  138. "5: slgr %0,%0\n"
  139. "6: sacf 768\n"
  140. EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
  141. EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
  142. : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
  143. : : "cc", "memory");
  144. return size;
  145. }
  146. unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
  147. {
  148. if (static_branch_likely(&have_mvcos))
  149. return copy_to_user_mvcos(to, from, n);
  150. return copy_to_user_mvcs(to, from, n);
  151. }
  152. EXPORT_SYMBOL(raw_copy_to_user);
  153. static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
  154. unsigned long size)
  155. {
  156. register unsigned long reg0 asm("0") = 0x810081UL;
  157. unsigned long tmp1, tmp2;
  158. tmp1 = -4096UL;
  159. /* FIXME: copy with reduced length. */
  160. asm volatile(
  161. "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
  162. " jz 2f\n"
  163. "1: algr %0,%3\n"
  164. " slgr %1,%3\n"
  165. " slgr %2,%3\n"
  166. " j 0b\n"
  167. "2:slgr %0,%0\n"
  168. "3: \n"
  169. EX_TABLE(0b,3b)
  170. : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
  171. : "d" (reg0) : "cc", "memory");
  172. return size;
  173. }
  174. static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
  175. unsigned long size)
  176. {
  177. unsigned long tmp1;
  178. load_kernel_asce();
  179. asm volatile(
  180. " sacf 256\n"
  181. " aghi %0,-1\n"
  182. " jo 5f\n"
  183. " bras %3,3f\n"
  184. "0: aghi %0,257\n"
  185. "1: mvc 0(1,%1),0(%2)\n"
  186. " la %1,1(%1)\n"
  187. " la %2,1(%2)\n"
  188. " aghi %0,-1\n"
  189. " jnz 1b\n"
  190. " j 5f\n"
  191. "2: mvc 0(256,%1),0(%2)\n"
  192. " la %1,256(%1)\n"
  193. " la %2,256(%2)\n"
  194. "3: aghi %0,-256\n"
  195. " jnm 2b\n"
  196. "4: ex %0,1b-0b(%3)\n"
  197. "5: slgr %0,%0\n"
  198. "6: sacf 768\n"
  199. EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
  200. : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
  201. : : "cc", "memory");
  202. return size;
  203. }
  204. unsigned long raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
  205. {
  206. if (static_branch_likely(&have_mvcos))
  207. return copy_in_user_mvcos(to, from, n);
  208. return copy_in_user_mvc(to, from, n);
  209. }
  210. EXPORT_SYMBOL(raw_copy_in_user);
  211. static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
  212. {
  213. register unsigned long reg0 asm("0") = 0x810000UL;
  214. unsigned long tmp1, tmp2;
  215. tmp1 = -4096UL;
  216. asm volatile(
  217. "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
  218. " jz 4f\n"
  219. "1: algr %0,%2\n"
  220. " slgr %1,%2\n"
  221. " j 0b\n"
  222. "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
  223. " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
  224. " slgr %3,%1\n"
  225. " clgr %0,%3\n" /* copy crosses next page boundary? */
  226. " jnh 5f\n"
  227. "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
  228. " slgr %0,%3\n"
  229. " j 5f\n"
  230. "4: slgr %0,%0\n"
  231. "5:\n"
  232. EX_TABLE(0b,2b) EX_TABLE(3b,5b)
  233. : "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
  234. : "a" (empty_zero_page), "d" (reg0) : "cc", "memory");
  235. return size;
  236. }
  237. static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
  238. {
  239. unsigned long tmp1, tmp2;
  240. load_kernel_asce();
  241. asm volatile(
  242. " sacf 256\n"
  243. " aghi %0,-1\n"
  244. " jo 5f\n"
  245. " bras %3,3f\n"
  246. " xc 0(1,%1),0(%1)\n"
  247. "0: aghi %0,257\n"
  248. " la %2,255(%1)\n" /* %2 = ptr + 255 */
  249. " srl %2,12\n"
  250. " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
  251. " slgr %2,%1\n"
  252. " clgr %0,%2\n" /* clear crosses next page boundary? */
  253. " jnh 5f\n"
  254. " aghi %2,-1\n"
  255. "1: ex %2,0(%3)\n"
  256. " aghi %2,1\n"
  257. " slgr %0,%2\n"
  258. " j 5f\n"
  259. "2: xc 0(256,%1),0(%1)\n"
  260. " la %1,256(%1)\n"
  261. "3: aghi %0,-256\n"
  262. " jnm 2b\n"
  263. "4: ex %0,0(%3)\n"
  264. "5: slgr %0,%0\n"
  265. "6: sacf 768\n"
  266. EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
  267. : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
  268. : : "cc", "memory");
  269. return size;
  270. }
  271. unsigned long __clear_user(void __user *to, unsigned long size)
  272. {
  273. if (static_branch_likely(&have_mvcos))
  274. return clear_user_mvcos(to, size);
  275. return clear_user_xc(to, size);
  276. }
  277. EXPORT_SYMBOL(__clear_user);
  278. static inline unsigned long strnlen_user_srst(const char __user *src,
  279. unsigned long size)
  280. {
  281. register unsigned long reg0 asm("0") = 0;
  282. unsigned long tmp1, tmp2;
  283. asm volatile(
  284. " la %2,0(%1)\n"
  285. " la %3,0(%0,%1)\n"
  286. " slgr %0,%0\n"
  287. " sacf 256\n"
  288. "0: srst %3,%2\n"
  289. " jo 0b\n"
  290. " la %0,1(%3)\n" /* strnlen_user results includes \0 */
  291. " slgr %0,%1\n"
  292. "1: sacf 768\n"
  293. EX_TABLE(0b,1b)
  294. : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
  295. : "d" (reg0) : "cc", "memory");
  296. return size;
  297. }
  298. unsigned long __strnlen_user(const char __user *src, unsigned long size)
  299. {
  300. if (unlikely(!size))
  301. return 0;
  302. load_kernel_asce();
  303. return strnlen_user_srst(src, size);
  304. }
  305. EXPORT_SYMBOL(__strnlen_user);
  306. long __strncpy_from_user(char *dst, const char __user *src, long size)
  307. {
  308. size_t done, len, offset, len_str;
  309. if (unlikely(size <= 0))
  310. return 0;
  311. done = 0;
  312. do {
  313. offset = (size_t)src & (L1_CACHE_BYTES - 1);
  314. len = min(size - done, L1_CACHE_BYTES - offset);
  315. if (copy_from_user(dst, src, len))
  316. return -EFAULT;
  317. len_str = strnlen(dst, len);
  318. done += len_str;
  319. src += len_str;
  320. dst += len_str;
  321. } while ((len_str == len) && (done < size));
  322. return done;
  323. }
  324. EXPORT_SYMBOL(__strncpy_from_user);
  325. static int __init uaccess_init(void)
  326. {
  327. if (test_facility(27))
  328. static_branch_enable(&have_mvcos);
  329. return 0;
  330. }
  331. early_initcall(uaccess_init);