memcpy_32.S 20 KB

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