uaccess.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * vineetg: June 2010
  9. * -__clear_user( ) called multiple times during elf load was byte loop
  10. * converted to do as much word clear as possible.
  11. *
  12. * vineetg: Dec 2009
  13. * -Hand crafted constant propagation for "constant" copy sizes
  14. * -stock kernel shrunk by 33K at -O3
  15. *
  16. * vineetg: Sept 2009
  17. * -Added option to (UN)inline copy_(to|from)_user to reduce code sz
  18. * -kernel shrunk by 200K even at -O3 (gcc 4.2.1)
  19. * -Enabled when doing -Os
  20. *
  21. * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
  22. */
  23. #ifndef _ASM_ARC_UACCESS_H
  24. #define _ASM_ARC_UACCESS_H
  25. #include <linux/string.h> /* for generic string functions */
  26. #define __kernel_ok (uaccess_kernel())
  27. /*
  28. * Algorithmically, for __user_ok() we want do:
  29. * (start < TASK_SIZE) && (start+len < TASK_SIZE)
  30. * where TASK_SIZE could either be retrieved from thread_info->addr_limit or
  31. * emitted directly in code.
  32. *
  33. * This can however be rewritten as follows:
  34. * (len <= TASK_SIZE) && (start+len < TASK_SIZE)
  35. *
  36. * Because it essentially checks if buffer end is within limit and @len is
  37. * non-ngeative, which implies that buffer start will be within limit too.
  38. *
  39. * The reason for rewriting being, for majority of cases, @len is generally
  40. * compile time constant, causing first sub-expression to be compile time
  41. * subsumed.
  42. *
  43. * The second part would generate weird large LIMMs e.g. (0x6000_0000 - 0x10),
  44. * so we check for TASK_SIZE using get_fs() since the addr_limit load from mem
  45. * would already have been done at this call site for __kernel_ok()
  46. *
  47. */
  48. #define __user_ok(addr, sz) (((sz) <= TASK_SIZE) && \
  49. ((addr) <= (get_fs() - (sz))))
  50. #define __access_ok(addr, sz) (unlikely(__kernel_ok) || \
  51. likely(__user_ok((addr), (sz))))
  52. /*********** Single byte/hword/word copies ******************/
  53. #define __get_user_fn(sz, u, k) \
  54. ({ \
  55. long __ret = 0; /* success by default */ \
  56. switch (sz) { \
  57. case 1: __arc_get_user_one(*(k), u, "ldb", __ret); break; \
  58. case 2: __arc_get_user_one(*(k), u, "ldw", __ret); break; \
  59. case 4: __arc_get_user_one(*(k), u, "ld", __ret); break; \
  60. case 8: __arc_get_user_one_64(*(k), u, __ret); break; \
  61. } \
  62. __ret; \
  63. })
  64. /*
  65. * Returns 0 on success, -EFAULT if not.
  66. * @ret already contains 0 - given that errors will be less likely
  67. * (hence +r asm constraint below).
  68. * In case of error, fixup code will make it -EFAULT
  69. */
  70. #define __arc_get_user_one(dst, src, op, ret) \
  71. __asm__ __volatile__( \
  72. "1: "op" %1,[%2]\n" \
  73. "2: ;nop\n" \
  74. " .section .fixup, \"ax\"\n" \
  75. " .align 4\n" \
  76. "3: # return -EFAULT\n" \
  77. " mov %0, %3\n" \
  78. " # zero out dst ptr\n" \
  79. " mov %1, 0\n" \
  80. " j 2b\n" \
  81. " .previous\n" \
  82. " .section __ex_table, \"a\"\n" \
  83. " .align 4\n" \
  84. " .word 1b,3b\n" \
  85. " .previous\n" \
  86. \
  87. : "+r" (ret), "=r" (dst) \
  88. : "r" (src), "ir" (-EFAULT))
  89. #define __arc_get_user_one_64(dst, src, ret) \
  90. __asm__ __volatile__( \
  91. "1: ld %1,[%2]\n" \
  92. "4: ld %R1,[%2, 4]\n" \
  93. "2: ;nop\n" \
  94. " .section .fixup, \"ax\"\n" \
  95. " .align 4\n" \
  96. "3: # return -EFAULT\n" \
  97. " mov %0, %3\n" \
  98. " # zero out dst ptr\n" \
  99. " mov %1, 0\n" \
  100. " mov %R1, 0\n" \
  101. " j 2b\n" \
  102. " .previous\n" \
  103. " .section __ex_table, \"a\"\n" \
  104. " .align 4\n" \
  105. " .word 1b,3b\n" \
  106. " .word 4b,3b\n" \
  107. " .previous\n" \
  108. \
  109. : "+r" (ret), "=r" (dst) \
  110. : "r" (src), "ir" (-EFAULT))
  111. #define __put_user_fn(sz, u, k) \
  112. ({ \
  113. long __ret = 0; /* success by default */ \
  114. switch (sz) { \
  115. case 1: __arc_put_user_one(*(k), u, "stb", __ret); break; \
  116. case 2: __arc_put_user_one(*(k), u, "stw", __ret); break; \
  117. case 4: __arc_put_user_one(*(k), u, "st", __ret); break; \
  118. case 8: __arc_put_user_one_64(*(k), u, __ret); break; \
  119. } \
  120. __ret; \
  121. })
  122. #define __arc_put_user_one(src, dst, op, ret) \
  123. __asm__ __volatile__( \
  124. "1: "op" %1,[%2]\n" \
  125. "2: ;nop\n" \
  126. " .section .fixup, \"ax\"\n" \
  127. " .align 4\n" \
  128. "3: mov %0, %3\n" \
  129. " j 2b\n" \
  130. " .previous\n" \
  131. " .section __ex_table, \"a\"\n" \
  132. " .align 4\n" \
  133. " .word 1b,3b\n" \
  134. " .previous\n" \
  135. \
  136. : "+r" (ret) \
  137. : "r" (src), "r" (dst), "ir" (-EFAULT))
  138. #define __arc_put_user_one_64(src, dst, ret) \
  139. __asm__ __volatile__( \
  140. "1: st %1,[%2]\n" \
  141. "4: st %R1,[%2, 4]\n" \
  142. "2: ;nop\n" \
  143. " .section .fixup, \"ax\"\n" \
  144. " .align 4\n" \
  145. "3: mov %0, %3\n" \
  146. " j 2b\n" \
  147. " .previous\n" \
  148. " .section __ex_table, \"a\"\n" \
  149. " .align 4\n" \
  150. " .word 1b,3b\n" \
  151. " .word 4b,3b\n" \
  152. " .previous\n" \
  153. \
  154. : "+r" (ret) \
  155. : "r" (src), "r" (dst), "ir" (-EFAULT))
  156. static inline unsigned long
  157. raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  158. {
  159. long res = 0;
  160. char val;
  161. unsigned long tmp1, tmp2, tmp3, tmp4;
  162. unsigned long orig_n = n;
  163. if (n == 0)
  164. return 0;
  165. /* unaligned */
  166. if (((unsigned long)to & 0x3) || ((unsigned long)from & 0x3)) {
  167. unsigned char tmp;
  168. __asm__ __volatile__ (
  169. " mov.f lp_count, %0 \n"
  170. " lpnz 2f \n"
  171. "1: ldb.ab %1, [%3, 1] \n"
  172. " stb.ab %1, [%2, 1] \n"
  173. " sub %0,%0,1 \n"
  174. "2: ;nop \n"
  175. " .section .fixup, \"ax\" \n"
  176. " .align 4 \n"
  177. "3: j 2b \n"
  178. " .previous \n"
  179. " .section __ex_table, \"a\" \n"
  180. " .align 4 \n"
  181. " .word 1b, 3b \n"
  182. " .previous \n"
  183. : "+r" (n),
  184. /*
  185. * Note as an '&' earlyclobber operand to make sure the
  186. * temporary register inside the loop is not the same as
  187. * FROM or TO.
  188. */
  189. "=&r" (tmp), "+r" (to), "+r" (from)
  190. :
  191. : "lp_count", "memory");
  192. return n;
  193. }
  194. /*
  195. * Hand-crafted constant propagation to reduce code sz of the
  196. * laddered copy 16x,8,4,2,1
  197. */
  198. if (__builtin_constant_p(orig_n)) {
  199. res = orig_n;
  200. if (orig_n / 16) {
  201. orig_n = orig_n % 16;
  202. __asm__ __volatile__(
  203. " lsr lp_count, %7,4 \n"
  204. " lp 3f \n"
  205. "1: ld.ab %3, [%2, 4] \n"
  206. "11: ld.ab %4, [%2, 4] \n"
  207. "12: ld.ab %5, [%2, 4] \n"
  208. "13: ld.ab %6, [%2, 4] \n"
  209. " st.ab %3, [%1, 4] \n"
  210. " st.ab %4, [%1, 4] \n"
  211. " st.ab %5, [%1, 4] \n"
  212. " st.ab %6, [%1, 4] \n"
  213. " sub %0,%0,16 \n"
  214. "3: ;nop \n"
  215. " .section .fixup, \"ax\" \n"
  216. " .align 4 \n"
  217. "4: j 3b \n"
  218. " .previous \n"
  219. " .section __ex_table, \"a\" \n"
  220. " .align 4 \n"
  221. " .word 1b, 4b \n"
  222. " .word 11b,4b \n"
  223. " .word 12b,4b \n"
  224. " .word 13b,4b \n"
  225. " .previous \n"
  226. : "+r" (res), "+r"(to), "+r"(from),
  227. "=r"(tmp1), "=r"(tmp2), "=r"(tmp3), "=r"(tmp4)
  228. : "ir"(n)
  229. : "lp_count", "memory");
  230. }
  231. if (orig_n / 8) {
  232. orig_n = orig_n % 8;
  233. __asm__ __volatile__(
  234. "14: ld.ab %3, [%2,4] \n"
  235. "15: ld.ab %4, [%2,4] \n"
  236. " st.ab %3, [%1,4] \n"
  237. " st.ab %4, [%1,4] \n"
  238. " sub %0,%0,8 \n"
  239. "31: ;nop \n"
  240. " .section .fixup, \"ax\" \n"
  241. " .align 4 \n"
  242. "4: j 31b \n"
  243. " .previous \n"
  244. " .section __ex_table, \"a\" \n"
  245. " .align 4 \n"
  246. " .word 14b,4b \n"
  247. " .word 15b,4b \n"
  248. " .previous \n"
  249. : "+r" (res), "+r"(to), "+r"(from),
  250. "=r"(tmp1), "=r"(tmp2)
  251. :
  252. : "memory");
  253. }
  254. if (orig_n / 4) {
  255. orig_n = orig_n % 4;
  256. __asm__ __volatile__(
  257. "16: ld.ab %3, [%2,4] \n"
  258. " st.ab %3, [%1,4] \n"
  259. " sub %0,%0,4 \n"
  260. "32: ;nop \n"
  261. " .section .fixup, \"ax\" \n"
  262. " .align 4 \n"
  263. "4: j 32b \n"
  264. " .previous \n"
  265. " .section __ex_table, \"a\" \n"
  266. " .align 4 \n"
  267. " .word 16b,4b \n"
  268. " .previous \n"
  269. : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
  270. :
  271. : "memory");
  272. }
  273. if (orig_n / 2) {
  274. orig_n = orig_n % 2;
  275. __asm__ __volatile__(
  276. "17: ldw.ab %3, [%2,2] \n"
  277. " stw.ab %3, [%1,2] \n"
  278. " sub %0,%0,2 \n"
  279. "33: ;nop \n"
  280. " .section .fixup, \"ax\" \n"
  281. " .align 4 \n"
  282. "4: j 33b \n"
  283. " .previous \n"
  284. " .section __ex_table, \"a\" \n"
  285. " .align 4 \n"
  286. " .word 17b,4b \n"
  287. " .previous \n"
  288. : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
  289. :
  290. : "memory");
  291. }
  292. if (orig_n & 1) {
  293. __asm__ __volatile__(
  294. "18: ldb.ab %3, [%2,2] \n"
  295. " stb.ab %3, [%1,2] \n"
  296. " sub %0,%0,1 \n"
  297. "34: ; nop \n"
  298. " .section .fixup, \"ax\" \n"
  299. " .align 4 \n"
  300. "4: j 34b \n"
  301. " .previous \n"
  302. " .section __ex_table, \"a\" \n"
  303. " .align 4 \n"
  304. " .word 18b,4b \n"
  305. " .previous \n"
  306. : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
  307. :
  308. : "memory");
  309. }
  310. } else { /* n is NOT constant, so laddered copy of 16x,8,4,2,1 */
  311. __asm__ __volatile__(
  312. " mov %0,%3 \n"
  313. " lsr.f lp_count, %3,4 \n" /* 16x bytes */
  314. " lpnz 3f \n"
  315. "1: ld.ab %5, [%2, 4] \n"
  316. "11: ld.ab %6, [%2, 4] \n"
  317. "12: ld.ab %7, [%2, 4] \n"
  318. "13: ld.ab %8, [%2, 4] \n"
  319. " st.ab %5, [%1, 4] \n"
  320. " st.ab %6, [%1, 4] \n"
  321. " st.ab %7, [%1, 4] \n"
  322. " st.ab %8, [%1, 4] \n"
  323. " sub %0,%0,16 \n"
  324. "3: and.f %3,%3,0xf \n" /* stragglers */
  325. " bz 34f \n"
  326. " bbit0 %3,3,31f \n" /* 8 bytes left */
  327. "14: ld.ab %5, [%2,4] \n"
  328. "15: ld.ab %6, [%2,4] \n"
  329. " st.ab %5, [%1,4] \n"
  330. " st.ab %6, [%1,4] \n"
  331. " sub.f %0,%0,8 \n"
  332. "31: bbit0 %3,2,32f \n" /* 4 bytes left */
  333. "16: ld.ab %5, [%2,4] \n"
  334. " st.ab %5, [%1,4] \n"
  335. " sub.f %0,%0,4 \n"
  336. "32: bbit0 %3,1,33f \n" /* 2 bytes left */
  337. "17: ldw.ab %5, [%2,2] \n"
  338. " stw.ab %5, [%1,2] \n"
  339. " sub.f %0,%0,2 \n"
  340. "33: bbit0 %3,0,34f \n"
  341. "18: ldb.ab %5, [%2,1] \n" /* 1 byte left */
  342. " stb.ab %5, [%1,1] \n"
  343. " sub.f %0,%0,1 \n"
  344. "34: ;nop \n"
  345. " .section .fixup, \"ax\" \n"
  346. " .align 4 \n"
  347. "4: j 34b \n"
  348. " .previous \n"
  349. " .section __ex_table, \"a\" \n"
  350. " .align 4 \n"
  351. " .word 1b, 4b \n"
  352. " .word 11b,4b \n"
  353. " .word 12b,4b \n"
  354. " .word 13b,4b \n"
  355. " .word 14b,4b \n"
  356. " .word 15b,4b \n"
  357. " .word 16b,4b \n"
  358. " .word 17b,4b \n"
  359. " .word 18b,4b \n"
  360. " .previous \n"
  361. : "=r" (res), "+r"(to), "+r"(from), "+r"(n), "=r"(val),
  362. "=r"(tmp1), "=r"(tmp2), "=r"(tmp3), "=r"(tmp4)
  363. :
  364. : "lp_count", "memory");
  365. }
  366. return res;
  367. }
  368. static inline unsigned long
  369. raw_copy_to_user(void __user *to, const void *from, unsigned long n)
  370. {
  371. long res = 0;
  372. char val;
  373. unsigned long tmp1, tmp2, tmp3, tmp4;
  374. unsigned long orig_n = n;
  375. if (n == 0)
  376. return 0;
  377. /* unaligned */
  378. if (((unsigned long)to & 0x3) || ((unsigned long)from & 0x3)) {
  379. unsigned char tmp;
  380. __asm__ __volatile__(
  381. " mov.f lp_count, %0 \n"
  382. " lpnz 3f \n"
  383. " ldb.ab %1, [%3, 1] \n"
  384. "1: stb.ab %1, [%2, 1] \n"
  385. " sub %0, %0, 1 \n"
  386. "3: ;nop \n"
  387. " .section .fixup, \"ax\" \n"
  388. " .align 4 \n"
  389. "4: j 3b \n"
  390. " .previous \n"
  391. " .section __ex_table, \"a\" \n"
  392. " .align 4 \n"
  393. " .word 1b, 4b \n"
  394. " .previous \n"
  395. : "+r" (n),
  396. /* Note as an '&' earlyclobber operand to make sure the
  397. * temporary register inside the loop is not the same as
  398. * FROM or TO.
  399. */
  400. "=&r" (tmp), "+r" (to), "+r" (from)
  401. :
  402. : "lp_count", "memory");
  403. return n;
  404. }
  405. if (__builtin_constant_p(orig_n)) {
  406. res = orig_n;
  407. if (orig_n / 16) {
  408. orig_n = orig_n % 16;
  409. __asm__ __volatile__(
  410. " lsr lp_count, %7,4 \n"
  411. " lp 3f \n"
  412. " ld.ab %3, [%2, 4] \n"
  413. " ld.ab %4, [%2, 4] \n"
  414. " ld.ab %5, [%2, 4] \n"
  415. " ld.ab %6, [%2, 4] \n"
  416. "1: st.ab %3, [%1, 4] \n"
  417. "11: st.ab %4, [%1, 4] \n"
  418. "12: st.ab %5, [%1, 4] \n"
  419. "13: st.ab %6, [%1, 4] \n"
  420. " sub %0, %0, 16 \n"
  421. "3:;nop \n"
  422. " .section .fixup, \"ax\" \n"
  423. " .align 4 \n"
  424. "4: j 3b \n"
  425. " .previous \n"
  426. " .section __ex_table, \"a\" \n"
  427. " .align 4 \n"
  428. " .word 1b, 4b \n"
  429. " .word 11b,4b \n"
  430. " .word 12b,4b \n"
  431. " .word 13b,4b \n"
  432. " .previous \n"
  433. : "+r" (res), "+r"(to), "+r"(from),
  434. "=r"(tmp1), "=r"(tmp2), "=r"(tmp3), "=r"(tmp4)
  435. : "ir"(n)
  436. : "lp_count", "memory");
  437. }
  438. if (orig_n / 8) {
  439. orig_n = orig_n % 8;
  440. __asm__ __volatile__(
  441. " ld.ab %3, [%2,4] \n"
  442. " ld.ab %4, [%2,4] \n"
  443. "14: st.ab %3, [%1,4] \n"
  444. "15: st.ab %4, [%1,4] \n"
  445. " sub %0, %0, 8 \n"
  446. "31:;nop \n"
  447. " .section .fixup, \"ax\" \n"
  448. " .align 4 \n"
  449. "4: j 31b \n"
  450. " .previous \n"
  451. " .section __ex_table, \"a\" \n"
  452. " .align 4 \n"
  453. " .word 14b,4b \n"
  454. " .word 15b,4b \n"
  455. " .previous \n"
  456. : "+r" (res), "+r"(to), "+r"(from),
  457. "=r"(tmp1), "=r"(tmp2)
  458. :
  459. : "memory");
  460. }
  461. if (orig_n / 4) {
  462. orig_n = orig_n % 4;
  463. __asm__ __volatile__(
  464. " ld.ab %3, [%2,4] \n"
  465. "16: st.ab %3, [%1,4] \n"
  466. " sub %0, %0, 4 \n"
  467. "32:;nop \n"
  468. " .section .fixup, \"ax\" \n"
  469. " .align 4 \n"
  470. "4: j 32b \n"
  471. " .previous \n"
  472. " .section __ex_table, \"a\" \n"
  473. " .align 4 \n"
  474. " .word 16b,4b \n"
  475. " .previous \n"
  476. : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
  477. :
  478. : "memory");
  479. }
  480. if (orig_n / 2) {
  481. orig_n = orig_n % 2;
  482. __asm__ __volatile__(
  483. " ldw.ab %3, [%2,2] \n"
  484. "17: stw.ab %3, [%1,2] \n"
  485. " sub %0, %0, 2 \n"
  486. "33:;nop \n"
  487. " .section .fixup, \"ax\" \n"
  488. " .align 4 \n"
  489. "4: j 33b \n"
  490. " .previous \n"
  491. " .section __ex_table, \"a\" \n"
  492. " .align 4 \n"
  493. " .word 17b,4b \n"
  494. " .previous \n"
  495. : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
  496. :
  497. : "memory");
  498. }
  499. if (orig_n & 1) {
  500. __asm__ __volatile__(
  501. " ldb.ab %3, [%2,1] \n"
  502. "18: stb.ab %3, [%1,1] \n"
  503. " sub %0, %0, 1 \n"
  504. "34: ;nop \n"
  505. " .section .fixup, \"ax\" \n"
  506. " .align 4 \n"
  507. "4: j 34b \n"
  508. " .previous \n"
  509. " .section __ex_table, \"a\" \n"
  510. " .align 4 \n"
  511. " .word 18b,4b \n"
  512. " .previous \n"
  513. : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
  514. :
  515. : "memory");
  516. }
  517. } else { /* n is NOT constant, so laddered copy of 16x,8,4,2,1 */
  518. __asm__ __volatile__(
  519. " mov %0,%3 \n"
  520. " lsr.f lp_count, %3,4 \n" /* 16x bytes */
  521. " lpnz 3f \n"
  522. " ld.ab %5, [%2, 4] \n"
  523. " ld.ab %6, [%2, 4] \n"
  524. " ld.ab %7, [%2, 4] \n"
  525. " ld.ab %8, [%2, 4] \n"
  526. "1: st.ab %5, [%1, 4] \n"
  527. "11: st.ab %6, [%1, 4] \n"
  528. "12: st.ab %7, [%1, 4] \n"
  529. "13: st.ab %8, [%1, 4] \n"
  530. " sub %0, %0, 16 \n"
  531. "3: and.f %3,%3,0xf \n" /* stragglers */
  532. " bz 34f \n"
  533. " bbit0 %3,3,31f \n" /* 8 bytes left */
  534. " ld.ab %5, [%2,4] \n"
  535. " ld.ab %6, [%2,4] \n"
  536. "14: st.ab %5, [%1,4] \n"
  537. "15: st.ab %6, [%1,4] \n"
  538. " sub.f %0, %0, 8 \n"
  539. "31: bbit0 %3,2,32f \n" /* 4 bytes left */
  540. " ld.ab %5, [%2,4] \n"
  541. "16: st.ab %5, [%1,4] \n"
  542. " sub.f %0, %0, 4 \n"
  543. "32: bbit0 %3,1,33f \n" /* 2 bytes left */
  544. " ldw.ab %5, [%2,2] \n"
  545. "17: stw.ab %5, [%1,2] \n"
  546. " sub.f %0, %0, 2 \n"
  547. "33: bbit0 %3,0,34f \n"
  548. " ldb.ab %5, [%2,1] \n" /* 1 byte left */
  549. "18: stb.ab %5, [%1,1] \n"
  550. " sub.f %0, %0, 1 \n"
  551. "34: ;nop \n"
  552. " .section .fixup, \"ax\" \n"
  553. " .align 4 \n"
  554. "4: j 34b \n"
  555. " .previous \n"
  556. " .section __ex_table, \"a\" \n"
  557. " .align 4 \n"
  558. " .word 1b, 4b \n"
  559. " .word 11b,4b \n"
  560. " .word 12b,4b \n"
  561. " .word 13b,4b \n"
  562. " .word 14b,4b \n"
  563. " .word 15b,4b \n"
  564. " .word 16b,4b \n"
  565. " .word 17b,4b \n"
  566. " .word 18b,4b \n"
  567. " .previous \n"
  568. : "=r" (res), "+r"(to), "+r"(from), "+r"(n), "=r"(val),
  569. "=r"(tmp1), "=r"(tmp2), "=r"(tmp3), "=r"(tmp4)
  570. :
  571. : "lp_count", "memory");
  572. }
  573. return res;
  574. }
  575. static inline unsigned long __arc_clear_user(void __user *to, unsigned long n)
  576. {
  577. long res = n;
  578. unsigned char *d_char = to;
  579. __asm__ __volatile__(
  580. " bbit0 %0, 0, 1f \n"
  581. "75: stb.ab %2, [%0,1] \n"
  582. " sub %1, %1, 1 \n"
  583. "1: bbit0 %0, 1, 2f \n"
  584. "76: stw.ab %2, [%0,2] \n"
  585. " sub %1, %1, 2 \n"
  586. "2: asr.f lp_count, %1, 2 \n"
  587. " lpnz 3f \n"
  588. "77: st.ab %2, [%0,4] \n"
  589. " sub %1, %1, 4 \n"
  590. "3: bbit0 %1, 1, 4f \n"
  591. "78: stw.ab %2, [%0,2] \n"
  592. " sub %1, %1, 2 \n"
  593. "4: bbit0 %1, 0, 5f \n"
  594. "79: stb.ab %2, [%0,1] \n"
  595. " sub %1, %1, 1 \n"
  596. "5: \n"
  597. " .section .fixup, \"ax\" \n"
  598. " .align 4 \n"
  599. "3: j 5b \n"
  600. " .previous \n"
  601. " .section __ex_table, \"a\" \n"
  602. " .align 4 \n"
  603. " .word 75b, 3b \n"
  604. " .word 76b, 3b \n"
  605. " .word 77b, 3b \n"
  606. " .word 78b, 3b \n"
  607. " .word 79b, 3b \n"
  608. " .previous \n"
  609. : "+r"(d_char), "+r"(res)
  610. : "i"(0)
  611. : "lp_count", "memory");
  612. return res;
  613. }
  614. static inline long
  615. __arc_strncpy_from_user(char *dst, const char __user *src, long count)
  616. {
  617. long res = 0;
  618. char val;
  619. if (count == 0)
  620. return 0;
  621. __asm__ __volatile__(
  622. " mov lp_count, %5 \n"
  623. " lp 3f \n"
  624. "1: ldb.ab %3, [%2, 1] \n"
  625. " breq.d %3, 0, 3f \n"
  626. " stb.ab %3, [%1, 1] \n"
  627. " add %0, %0, 1 # Num of NON NULL bytes copied \n"
  628. "3: \n"
  629. " .section .fixup, \"ax\" \n"
  630. " .align 4 \n"
  631. "4: mov %0, %4 # sets @res as -EFAULT \n"
  632. " j 3b \n"
  633. " .previous \n"
  634. " .section __ex_table, \"a\" \n"
  635. " .align 4 \n"
  636. " .word 1b, 4b \n"
  637. " .previous \n"
  638. : "+r"(res), "+r"(dst), "+r"(src), "=r"(val)
  639. : "g"(-EFAULT), "r"(count)
  640. : "lp_count", "memory");
  641. return res;
  642. }
  643. static inline long __arc_strnlen_user(const char __user *s, long n)
  644. {
  645. long res, tmp1, cnt;
  646. char val;
  647. __asm__ __volatile__(
  648. " mov %2, %1 \n"
  649. "1: ldb.ab %3, [%0, 1] \n"
  650. " breq.d %3, 0, 2f \n"
  651. " sub.f %2, %2, 1 \n"
  652. " bnz 1b \n"
  653. " sub %2, %2, 1 \n"
  654. "2: sub %0, %1, %2 \n"
  655. "3: ;nop \n"
  656. " .section .fixup, \"ax\" \n"
  657. " .align 4 \n"
  658. "4: mov %0, 0 \n"
  659. " j 3b \n"
  660. " .previous \n"
  661. " .section __ex_table, \"a\" \n"
  662. " .align 4 \n"
  663. " .word 1b, 4b \n"
  664. " .previous \n"
  665. : "=r"(res), "=r"(tmp1), "=r"(cnt), "=r"(val)
  666. : "0"(s), "1"(n)
  667. : "memory");
  668. return res;
  669. }
  670. #ifndef CONFIG_CC_OPTIMIZE_FOR_SIZE
  671. #define INLINE_COPY_TO_USER
  672. #define INLINE_COPY_FROM_USER
  673. #define __clear_user(d, n) __arc_clear_user(d, n)
  674. #define __strncpy_from_user(d, s, n) __arc_strncpy_from_user(d, s, n)
  675. #define __strnlen_user(s, n) __arc_strnlen_user(s, n)
  676. #else
  677. extern unsigned long arc_clear_user_noinline(void __user *to,
  678. unsigned long n);
  679. extern long arc_strncpy_from_user_noinline (char *dst, const char __user *src,
  680. long count);
  681. extern long arc_strnlen_user_noinline(const char __user *src, long n);
  682. #define __clear_user(d, n) arc_clear_user_noinline(d, n)
  683. #define __strncpy_from_user(d, s, n) arc_strncpy_from_user_noinline(d, s, n)
  684. #define __strnlen_user(s, n) arc_strnlen_user_noinline(s, n)
  685. #endif
  686. #include <asm-generic/uaccess.h>
  687. #endif