uaccess.h 19 KB

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