uaccess.c 9.9 KB

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