uaccess.c 9.8 KB

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