memcpy_32.S 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <arch/chip.h>
  15. /*
  16. * This file shares the implementation of the userspace memcpy and
  17. * the kernel's memcpy, copy_to_user and copy_from_user.
  18. */
  19. #include <linux/linkage.h>
  20. #define IS_MEMCPY 0
  21. #define IS_COPY_FROM_USER 1
  22. #define IS_COPY_FROM_USER_ZEROING 2
  23. #define IS_COPY_TO_USER -1
  24. .section .text.memcpy_common, "ax"
  25. .align 64
  26. /* Use this to preface each bundle that can cause an exception so
  27. * the kernel can clean up properly. The special cleanup code should
  28. * not use these, since it knows what it is doing.
  29. */
  30. #define EX \
  31. .pushsection __ex_table, "a"; \
  32. .align 4; \
  33. .word 9f, memcpy_common_fixup; \
  34. .popsection; \
  35. 9
  36. /* __copy_from_user_inatomic takes the kernel target address in r0,
  37. * the user source in r1, and the bytes to copy in r2.
  38. * It returns the number of uncopiable bytes (hopefully zero) in r0.
  39. */
  40. ENTRY(__copy_from_user_inatomic)
  41. .type __copy_from_user_inatomic, @function
  42. FEEDBACK_ENTER_EXPLICIT(__copy_from_user_inatomic, \
  43. .text.memcpy_common, \
  44. .Lend_memcpy_common - __copy_from_user_inatomic)
  45. { movei r29, IS_COPY_FROM_USER; j memcpy_common }
  46. .size __copy_from_user_inatomic, . - __copy_from_user_inatomic
  47. /* __copy_from_user_zeroing is like __copy_from_user_inatomic, but
  48. * any uncopiable bytes are zeroed in the target.
  49. */
  50. ENTRY(__copy_from_user_zeroing)
  51. .type __copy_from_user_zeroing, @function
  52. FEEDBACK_REENTER(__copy_from_user_inatomic)
  53. { movei r29, IS_COPY_FROM_USER_ZEROING; j memcpy_common }
  54. .size __copy_from_user_zeroing, . - __copy_from_user_zeroing
  55. /* __copy_to_user_inatomic takes the user target address in r0,
  56. * the kernel source in r1, and the bytes to copy in r2.
  57. * It returns the number of uncopiable bytes (hopefully zero) in r0.
  58. */
  59. ENTRY(__copy_to_user_inatomic)
  60. .type __copy_to_user_inatomic, @function
  61. FEEDBACK_REENTER(__copy_from_user_inatomic)
  62. { movei r29, IS_COPY_TO_USER; j memcpy_common }
  63. .size __copy_to_user_inatomic, . - __copy_to_user_inatomic
  64. ENTRY(memcpy)
  65. .type memcpy, @function
  66. FEEDBACK_REENTER(__copy_from_user_inatomic)
  67. { movei r29, IS_MEMCPY }
  68. .size memcpy, . - memcpy
  69. /* Fall through */
  70. .type memcpy_common, @function
  71. memcpy_common:
  72. /* On entry, r29 holds one of the IS_* macro values from above. */
  73. /* r0 is the dest, r1 is the source, r2 is the size. */
  74. /* Save aside original dest so we can return it at the end. */
  75. { sw sp, lr; move r23, r0; or r4, r0, r1 }
  76. /* Check for an empty size. */
  77. { bz r2, .Ldone; andi r4, r4, 3 }
  78. /* Save aside original values in case of a fault. */
  79. { move r24, r1; move r25, r2 }
  80. move r27, lr
  81. /* Check for an unaligned source or dest. */
  82. { bnz r4, .Lcopy_unaligned_maybe_many; addli r4, r2, -256 }
  83. .Lcheck_aligned_copy_size:
  84. /* If we are copying < 256 bytes, branch to simple case. */
  85. { blzt r4, .Lcopy_8_check; slti_u r8, r2, 8 }
  86. /* Copying >= 256 bytes, so jump to complex prefetching loop. */
  87. { andi r6, r1, 63; j .Lcopy_many }
  88. /*
  89. *
  90. * Aligned 4 byte at a time copy loop
  91. *
  92. */
  93. .Lcopy_8_loop:
  94. /* Copy two words at a time to hide load latency. */
  95. EX: { lw r3, r1; addi r1, r1, 4; slti_u r8, r2, 16 }
  96. EX: { lw r4, r1; addi r1, r1, 4 }
  97. EX: { sw r0, r3; addi r0, r0, 4; addi r2, r2, -4 }
  98. EX: { sw r0, r4; addi r0, r0, 4; addi r2, r2, -4 }
  99. .Lcopy_8_check:
  100. { bzt r8, .Lcopy_8_loop; slti_u r4, r2, 4 }
  101. /* Copy odd leftover word, if any. */
  102. { bnzt r4, .Lcheck_odd_stragglers }
  103. EX: { lw r3, r1; addi r1, r1, 4 }
  104. EX: { sw r0, r3; addi r0, r0, 4; addi r2, r2, -4 }
  105. .Lcheck_odd_stragglers:
  106. { bnz r2, .Lcopy_unaligned_few }
  107. .Ldone:
  108. /* For memcpy return original dest address, else zero. */
  109. { mz r0, r29, r23; jrp lr }
  110. /*
  111. *
  112. * Prefetching multiple cache line copy handler (for large transfers).
  113. *
  114. */
  115. /* Copy words until r1 is cache-line-aligned. */
  116. .Lalign_loop:
  117. EX: { lw r3, r1; addi r1, r1, 4 }
  118. { andi r6, r1, 63 }
  119. EX: { sw r0, r3; addi r0, r0, 4; addi r2, r2, -4 }
  120. .Lcopy_many:
  121. { bnzt r6, .Lalign_loop; addi r9, r0, 63 }
  122. { addi r3, r1, 60; andi r9, r9, -64 }
  123. /* No need to prefetch dst, we'll just do the wh64
  124. * right before we copy a line.
  125. */
  126. EX: { lw r5, r3; addi r3, r3, 64; movei r4, 1 }
  127. /* Intentionally stall for a few cycles to leave L2 cache alone. */
  128. { bnzt zero, .; move r27, lr }
  129. EX: { lw r6, r3; addi r3, r3, 64 }
  130. /* Intentionally stall for a few cycles to leave L2 cache alone. */
  131. { bnzt zero, . }
  132. EX: { lw r7, r3; addi r3, r3, 64 }
  133. /* Intentionally stall for a few cycles to leave L2 cache alone. */
  134. { bz zero, .Lbig_loop2 }
  135. /* On entry to this loop:
  136. * - r0 points to the start of dst line 0
  137. * - r1 points to start of src line 0
  138. * - r2 >= (256 - 60), only the first time the loop trips.
  139. * - r3 contains r1 + 128 + 60 [pointer to end of source line 2]
  140. * This is our prefetch address. When we get near the end
  141. * rather than prefetching off the end this is changed to point
  142. * to some "safe" recently loaded address.
  143. * - r5 contains *(r1 + 60) [i.e. last word of source line 0]
  144. * - r6 contains *(r1 + 64 + 60) [i.e. last word of source line 1]
  145. * - r9 contains ((r0 + 63) & -64)
  146. * [start of next dst cache line.]
  147. */
  148. .Lbig_loop:
  149. { jal .Lcopy_line2; add r15, r1, r2 }
  150. .Lbig_loop2:
  151. /* Copy line 0, first stalling until r5 is ready. */
  152. EX: { move r12, r5; lw r16, r1 }
  153. { bz r4, .Lcopy_8_check; slti_u r8, r2, 8 }
  154. /* Prefetch several lines ahead. */
  155. EX: { lw r5, r3; addi r3, r3, 64 }
  156. { jal .Lcopy_line }
  157. /* Copy line 1, first stalling until r6 is ready. */
  158. EX: { move r12, r6; lw r16, r1 }
  159. { bz r4, .Lcopy_8_check; slti_u r8, r2, 8 }
  160. /* Prefetch several lines ahead. */
  161. EX: { lw r6, r3; addi r3, r3, 64 }
  162. { jal .Lcopy_line }
  163. /* Copy line 2, first stalling until r7 is ready. */
  164. EX: { move r12, r7; lw r16, r1 }
  165. { bz r4, .Lcopy_8_check; slti_u r8, r2, 8 }
  166. /* Prefetch several lines ahead. */
  167. EX: { lw r7, r3; addi r3, r3, 64 }
  168. /* Use up a caches-busy cycle by jumping back to the top of the
  169. * loop. Might as well get it out of the way now.
  170. */
  171. { j .Lbig_loop }
  172. /* On entry:
  173. * - r0 points to the destination line.
  174. * - r1 points to the source line.
  175. * - r3 is the next prefetch address.
  176. * - r9 holds the last address used for wh64.
  177. * - r12 = WORD_15
  178. * - r16 = WORD_0.
  179. * - r17 == r1 + 16.
  180. * - r27 holds saved lr to restore.
  181. *
  182. * On exit:
  183. * - r0 is incremented by 64.
  184. * - r1 is incremented by 64, unless that would point to a word
  185. * beyond the end of the source array, in which case it is redirected
  186. * to point to an arbitrary word already in the cache.
  187. * - r2 is decremented by 64.
  188. * - r3 is unchanged, unless it points to a word beyond the
  189. * end of the source array, in which case it is redirected
  190. * to point to an arbitrary word already in the cache.
  191. * Redirecting is OK since if we are that close to the end
  192. * of the array we will not come back to this subroutine
  193. * and use the contents of the prefetched address.
  194. * - r4 is nonzero iff r2 >= 64.
  195. * - r9 is incremented by 64, unless it points beyond the
  196. * end of the last full destination cache line, in which
  197. * case it is redirected to a "safe address" that can be
  198. * clobbered (sp - 64)
  199. * - lr contains the value in r27.
  200. */
  201. /* r26 unused */
  202. .Lcopy_line:
  203. /* TODO: when r3 goes past the end, we would like to redirect it
  204. * to prefetch the last partial cache line (if any) just once, for the
  205. * benefit of the final cleanup loop. But we don't want to
  206. * prefetch that line more than once, or subsequent prefetches
  207. * will go into the RTF. But then .Lbig_loop should unconditionally
  208. * branch to top of loop to execute final prefetch, and its
  209. * nop should become a conditional branch.
  210. */
  211. /* We need two non-memory cycles here to cover the resources
  212. * used by the loads initiated by the caller.
  213. */
  214. { add r15, r1, r2 }
  215. .Lcopy_line2:
  216. { slt_u r13, r3, r15; addi r17, r1, 16 }
  217. /* NOTE: this will stall for one cycle as L1 is busy. */
  218. /* Fill second L1D line. */
  219. EX: { lw r17, r17; addi r1, r1, 48; mvz r3, r13, r1 } /* r17 = WORD_4 */
  220. /* Prepare destination line for writing. */
  221. EX: { wh64 r9; addi r9, r9, 64 }
  222. /* Load seven words that are L1D hits to cover wh64 L2 usage. */
  223. /* Load the three remaining words from the last L1D line, which
  224. * we know has already filled the L1D.
  225. */
  226. EX: { lw r4, r1; addi r1, r1, 4; addi r20, r1, 16 } /* r4 = WORD_12 */
  227. EX: { lw r8, r1; addi r1, r1, 4; slt_u r13, r20, r15 }/* r8 = WORD_13 */
  228. EX: { lw r11, r1; addi r1, r1, -52; mvz r20, r13, r1 } /* r11 = WORD_14 */
  229. /* Load the three remaining words from the first L1D line, first
  230. * stalling until it has filled by "looking at" r16.
  231. */
  232. EX: { lw r13, r1; addi r1, r1, 4; move zero, r16 } /* r13 = WORD_1 */
  233. EX: { lw r14, r1; addi r1, r1, 4 } /* r14 = WORD_2 */
  234. EX: { lw r15, r1; addi r1, r1, 8; addi r10, r0, 60 } /* r15 = WORD_3 */
  235. /* Load second word from the second L1D line, first
  236. * stalling until it has filled by "looking at" r17.
  237. */
  238. EX: { lw r19, r1; addi r1, r1, 4; move zero, r17 } /* r19 = WORD_5 */
  239. /* Store last word to the destination line, potentially dirtying it
  240. * for the first time, which keeps the L2 busy for two cycles.
  241. */
  242. EX: { sw r10, r12 } /* store(WORD_15) */
  243. /* Use two L1D hits to cover the sw L2 access above. */
  244. EX: { lw r10, r1; addi r1, r1, 4 } /* r10 = WORD_6 */
  245. EX: { lw r12, r1; addi r1, r1, 4 } /* r12 = WORD_7 */
  246. /* Fill third L1D line. */
  247. EX: { lw r18, r1; addi r1, r1, 4 } /* r18 = WORD_8 */
  248. /* Store first L1D line. */
  249. EX: { sw r0, r16; addi r0, r0, 4; add r16, r0, r2 } /* store(WORD_0) */
  250. EX: { sw r0, r13; addi r0, r0, 4; andi r16, r16, -64 } /* store(WORD_1) */
  251. EX: { sw r0, r14; addi r0, r0, 4; slt_u r16, r9, r16 } /* store(WORD_2) */
  252. EX: { sw r0, r15; addi r0, r0, 4; addi r13, sp, -64 } /* store(WORD_3) */
  253. /* Store second L1D line. */
  254. EX: { sw r0, r17; addi r0, r0, 4; mvz r9, r16, r13 }/* store(WORD_4) */
  255. EX: { sw r0, r19; addi r0, r0, 4 } /* store(WORD_5) */
  256. EX: { sw r0, r10; addi r0, r0, 4 } /* store(WORD_6) */
  257. EX: { sw r0, r12; addi r0, r0, 4 } /* store(WORD_7) */
  258. EX: { lw r13, r1; addi r1, r1, 4; move zero, r18 } /* r13 = WORD_9 */
  259. EX: { lw r14, r1; addi r1, r1, 4 } /* r14 = WORD_10 */
  260. EX: { lw r15, r1; move r1, r20 } /* r15 = WORD_11 */
  261. /* Store third L1D line. */
  262. EX: { sw r0, r18; addi r0, r0, 4 } /* store(WORD_8) */
  263. EX: { sw r0, r13; addi r0, r0, 4 } /* store(WORD_9) */
  264. EX: { sw r0, r14; addi r0, r0, 4 } /* store(WORD_10) */
  265. EX: { sw r0, r15; addi r0, r0, 4 } /* store(WORD_11) */
  266. /* Store rest of fourth L1D line. */
  267. EX: { sw r0, r4; addi r0, r0, 4 } /* store(WORD_12) */
  268. {
  269. EX: sw r0, r8 /* store(WORD_13) */
  270. addi r0, r0, 4
  271. /* Will r2 be > 64 after we subtract 64 below? */
  272. shri r4, r2, 7
  273. }
  274. {
  275. EX: sw r0, r11 /* store(WORD_14) */
  276. addi r0, r0, 8
  277. /* Record 64 bytes successfully copied. */
  278. addi r2, r2, -64
  279. }
  280. { jrp lr; move lr, r27 }
  281. /* Convey to the backtrace library that the stack frame is size
  282. * zero, and the real return address is on the stack rather than
  283. * in 'lr'.
  284. */
  285. { info 8 }
  286. .align 64
  287. .Lcopy_unaligned_maybe_many:
  288. /* Skip the setup overhead if we aren't copying many bytes. */
  289. { slti_u r8, r2, 20; sub r4, zero, r0 }
  290. { bnzt r8, .Lcopy_unaligned_few; andi r4, r4, 3 }
  291. { bz r4, .Ldest_is_word_aligned; add r18, r1, r2 }
  292. /*
  293. *
  294. * unaligned 4 byte at a time copy handler.
  295. *
  296. */
  297. /* Copy single bytes until r0 == 0 mod 4, so we can store words. */
  298. .Lalign_dest_loop:
  299. EX: { lb_u r3, r1; addi r1, r1, 1; addi r4, r4, -1 }
  300. EX: { sb r0, r3; addi r0, r0, 1; addi r2, r2, -1 }
  301. { bnzt r4, .Lalign_dest_loop; andi r3, r1, 3 }
  302. /* If source and dest are now *both* aligned, do an aligned copy. */
  303. { bz r3, .Lcheck_aligned_copy_size; addli r4, r2, -256 }
  304. .Ldest_is_word_aligned:
  305. EX: { andi r8, r0, 63; lwadd_na r6, r1, 4}
  306. { slti_u r9, r2, 64; bz r8, .Ldest_is_L2_line_aligned }
  307. /* This copies unaligned words until either there are fewer
  308. * than 4 bytes left to copy, or until the destination pointer
  309. * is cache-aligned, whichever comes first.
  310. *
  311. * On entry:
  312. * - r0 is the next store address.
  313. * - r1 points 4 bytes past the load address corresponding to r0.
  314. * - r2 >= 4
  315. * - r6 is the next aligned word loaded.
  316. */
  317. .Lcopy_unaligned_src_words:
  318. EX: { lwadd_na r7, r1, 4; slti_u r8, r2, 4 + 4 }
  319. /* stall */
  320. { dword_align r6, r7, r1; slti_u r9, r2, 64 + 4 }
  321. EX: { swadd r0, r6, 4; addi r2, r2, -4 }
  322. { bnz r8, .Lcleanup_unaligned_words; andi r8, r0, 63 }
  323. { bnzt r8, .Lcopy_unaligned_src_words; move r6, r7 }
  324. /* On entry:
  325. * - r0 is the next store address.
  326. * - r1 points 4 bytes past the load address corresponding to r0.
  327. * - r2 >= 4 (# of bytes left to store).
  328. * - r6 is the next aligned src word value.
  329. * - r9 = (r2 < 64U).
  330. * - r18 points one byte past the end of source memory.
  331. */
  332. .Ldest_is_L2_line_aligned:
  333. {
  334. /* Not a full cache line remains. */
  335. bnz r9, .Lcleanup_unaligned_words
  336. move r7, r6
  337. }
  338. /* r2 >= 64 */
  339. /* Kick off two prefetches, but don't go past the end. */
  340. { addi r3, r1, 63 - 4; addi r8, r1, 64 + 63 - 4 }
  341. { prefetch r3; move r3, r8; slt_u r8, r8, r18 }
  342. { mvz r3, r8, r1; addi r8, r3, 64 }
  343. { prefetch r3; move r3, r8; slt_u r8, r8, r18 }
  344. { mvz r3, r8, r1; movei r17, 0 }
  345. .Lcopy_unaligned_line:
  346. /* Prefetch another line. */
  347. { prefetch r3; addi r15, r1, 60; addi r3, r3, 64 }
  348. /* Fire off a load of the last word we are about to copy. */
  349. EX: { lw_na r15, r15; slt_u r8, r3, r18 }
  350. EX: { mvz r3, r8, r1; wh64 r0 }
  351. /* This loop runs twice.
  352. *
  353. * On entry:
  354. * - r17 is even before the first iteration, and odd before
  355. * the second. It is incremented inside the loop. Encountering
  356. * an even value at the end of the loop makes it stop.
  357. */
  358. .Lcopy_half_an_unaligned_line:
  359. EX: {
  360. /* Stall until the last byte is ready. In the steady state this
  361. * guarantees all words to load below will be in the L2 cache, which
  362. * avoids shunting the loads to the RTF.
  363. */
  364. move zero, r15
  365. lwadd_na r7, r1, 16
  366. }
  367. EX: { lwadd_na r11, r1, 12 }
  368. EX: { lwadd_na r14, r1, -24 }
  369. EX: { lwadd_na r8, r1, 4 }
  370. EX: { lwadd_na r9, r1, 4 }
  371. EX: {
  372. lwadd_na r10, r1, 8
  373. /* r16 = (r2 < 64), after we subtract 32 from r2 below. */
  374. slti_u r16, r2, 64 + 32
  375. }
  376. EX: { lwadd_na r12, r1, 4; addi r17, r17, 1 }
  377. EX: { lwadd_na r13, r1, 8; dword_align r6, r7, r1 }
  378. EX: { swadd r0, r6, 4; dword_align r7, r8, r1 }
  379. EX: { swadd r0, r7, 4; dword_align r8, r9, r1 }
  380. EX: { swadd r0, r8, 4; dword_align r9, r10, r1 }
  381. EX: { swadd r0, r9, 4; dword_align r10, r11, r1 }
  382. EX: { swadd r0, r10, 4; dword_align r11, r12, r1 }
  383. EX: { swadd r0, r11, 4; dword_align r12, r13, r1 }
  384. EX: { swadd r0, r12, 4; dword_align r13, r14, r1 }
  385. EX: { swadd r0, r13, 4; addi r2, r2, -32 }
  386. { move r6, r14; bbst r17, .Lcopy_half_an_unaligned_line }
  387. { bzt r16, .Lcopy_unaligned_line; move r7, r6 }
  388. /* On entry:
  389. * - r0 is the next store address.
  390. * - r1 points 4 bytes past the load address corresponding to r0.
  391. * - r2 >= 0 (# of bytes left to store).
  392. * - r7 is the next aligned src word value.
  393. */
  394. .Lcleanup_unaligned_words:
  395. /* Handle any trailing bytes. */
  396. { bz r2, .Lcopy_unaligned_done; slti_u r8, r2, 4 }
  397. { bzt r8, .Lcopy_unaligned_src_words; move r6, r7 }
  398. /* Move r1 back to the point where it corresponds to r0. */
  399. { addi r1, r1, -4 }
  400. /* Fall through */
  401. /*
  402. *
  403. * 1 byte at a time copy handler.
  404. *
  405. */
  406. .Lcopy_unaligned_few:
  407. EX: { lb_u r3, r1; addi r1, r1, 1 }
  408. EX: { sb r0, r3; addi r0, r0, 1; addi r2, r2, -1 }
  409. { bnzt r2, .Lcopy_unaligned_few }
  410. .Lcopy_unaligned_done:
  411. /* For memcpy return original dest address, else zero. */
  412. { mz r0, r29, r23; jrp lr }
  413. .Lend_memcpy_common:
  414. .size memcpy_common, .Lend_memcpy_common - memcpy_common
  415. .section .fixup,"ax"
  416. memcpy_common_fixup:
  417. .type memcpy_common_fixup, @function
  418. /* Skip any bytes we already successfully copied.
  419. * r2 (num remaining) is correct, but r0 (dst) and r1 (src)
  420. * may not be quite right because of unrolling and prefetching.
  421. * So we need to recompute their values as the address just
  422. * after the last byte we are sure was successfully loaded and
  423. * then stored.
  424. */
  425. /* Determine how many bytes we successfully copied. */
  426. { sub r3, r25, r2 }
  427. /* Add this to the original r0 and r1 to get their new values. */
  428. { add r0, r23, r3; add r1, r24, r3 }
  429. { bzt r29, memcpy_fixup_loop }
  430. { blzt r29, copy_to_user_fixup_loop }
  431. copy_from_user_fixup_loop:
  432. /* Try copying the rest one byte at a time, expecting a load fault. */
  433. .Lcfu: { lb_u r3, r1; addi r1, r1, 1 }
  434. { sb r0, r3; addi r0, r0, 1; addi r2, r2, -1 }
  435. { bnzt r2, copy_from_user_fixup_loop }
  436. .Lcopy_from_user_fixup_zero_remainder:
  437. { bbs r29, 2f } /* low bit set means IS_COPY_FROM_USER */
  438. /* byte-at-a-time loop faulted, so zero the rest. */
  439. { move r3, r2; bz r2, 2f /* should be impossible, but handle it. */ }
  440. 1: { sb r0, zero; addi r0, r0, 1; addi r3, r3, -1 }
  441. { bnzt r3, 1b }
  442. 2: move lr, r27
  443. { move r0, r2; jrp lr }
  444. copy_to_user_fixup_loop:
  445. /* Try copying the rest one byte at a time, expecting a store fault. */
  446. { lb_u r3, r1; addi r1, r1, 1 }
  447. .Lctu: { sb r0, r3; addi r0, r0, 1; addi r2, r2, -1 }
  448. { bnzt r2, copy_to_user_fixup_loop }
  449. .Lcopy_to_user_fixup_done:
  450. move lr, r27
  451. { move r0, r2; jrp lr }
  452. memcpy_fixup_loop:
  453. /* Try copying the rest one byte at a time. We expect a disastrous
  454. * fault to happen since we are in fixup code, but let it happen.
  455. */
  456. { lb_u r3, r1; addi r1, r1, 1 }
  457. { sb r0, r3; addi r0, r0, 1; addi r2, r2, -1 }
  458. { bnzt r2, memcpy_fixup_loop }
  459. /* This should be unreachable, we should have faulted again.
  460. * But be paranoid and handle it in case some interrupt changed
  461. * the TLB or something.
  462. */
  463. move lr, r27
  464. { move r0, r23; jrp lr }
  465. .size memcpy_common_fixup, . - memcpy_common_fixup
  466. .section __ex_table,"a"
  467. .align 4
  468. .word .Lcfu, .Lcopy_from_user_fixup_zero_remainder
  469. .word .Lctu, .Lcopy_to_user_fixup_done