memcpy.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Unified implementation of memcpy, memmove and the __copy_user backend.
  7. *
  8. * Copyright (C) 1998, 99, 2000, 01, 2002 Ralf Baechle (ralf@gnu.org)
  9. * Copyright (C) 1999, 2000, 01, 2002 Silicon Graphics, Inc.
  10. * Copyright (C) 2002 Broadcom, Inc.
  11. * memcpy/copy_user author: Mark Vandevoorde
  12. * Copyright (C) 2007 Maciej W. Rozycki
  13. *
  14. * Mnemonic names for arguments to memcpy/__copy_user
  15. */
  16. /*
  17. * Hack to resolve longstanding prefetch issue
  18. *
  19. * Prefetching may be fatal on some systems if we're prefetching beyond the
  20. * end of memory on some systems. It's also a seriously bad idea on non
  21. * dma-coherent systems.
  22. */
  23. #ifdef CONFIG_DMA_NONCOHERENT
  24. #undef CONFIG_CPU_HAS_PREFETCH
  25. #endif
  26. #ifdef CONFIG_MIPS_MALTA
  27. #undef CONFIG_CPU_HAS_PREFETCH
  28. #endif
  29. #include <asm/asm.h>
  30. #include <asm/asm-offsets.h>
  31. #include <asm/regdef.h>
  32. #define dst a0
  33. #define src a1
  34. #define len a2
  35. /*
  36. * Spec
  37. *
  38. * memcpy copies len bytes from src to dst and sets v0 to dst.
  39. * It assumes that
  40. * - src and dst don't overlap
  41. * - src is readable
  42. * - dst is writable
  43. * memcpy uses the standard calling convention
  44. *
  45. * __copy_user copies up to len bytes from src to dst and sets a2 (len) to
  46. * the number of uncopied bytes due to an exception caused by a read or write.
  47. * __copy_user assumes that src and dst don't overlap, and that the call is
  48. * implementing one of the following:
  49. * copy_to_user
  50. * - src is readable (no exceptions when reading src)
  51. * copy_from_user
  52. * - dst is writable (no exceptions when writing dst)
  53. * __copy_user uses a non-standard calling convention; see
  54. * include/asm-mips/uaccess.h
  55. *
  56. * When an exception happens on a load, the handler must
  57. # ensure that all of the destination buffer is overwritten to prevent
  58. * leaking information to user mode programs.
  59. */
  60. /*
  61. * Implementation
  62. */
  63. /*
  64. * The exception handler for loads requires that:
  65. * 1- AT contain the address of the byte just past the end of the source
  66. * of the copy,
  67. * 2- src_entry <= src < AT, and
  68. * 3- (dst - src) == (dst_entry - src_entry),
  69. * The _entry suffix denotes values when __copy_user was called.
  70. *
  71. * (1) is set up up by uaccess.h and maintained by not writing AT in copy_user
  72. * (2) is met by incrementing src by the number of bytes copied
  73. * (3) is met by not doing loads between a pair of increments of dst and src
  74. *
  75. * The exception handlers for stores adjust len (if necessary) and return.
  76. * These handlers do not need to overwrite any data.
  77. *
  78. * For __rmemcpy and memmove an exception is always a kernel bug, therefore
  79. * they're not protected.
  80. */
  81. #define EXC(inst_reg,addr,handler) \
  82. 9: inst_reg, addr; \
  83. .section __ex_table,"a"; \
  84. PTR 9b, handler; \
  85. .previous
  86. /*
  87. * Only on the 64-bit kernel we can made use of 64-bit registers.
  88. */
  89. #ifdef CONFIG_64BIT
  90. #define USE_DOUBLE
  91. #endif
  92. #ifdef USE_DOUBLE
  93. #define LOAD ld
  94. #define LOADL ldl
  95. #define LOADR ldr
  96. #define STOREL sdl
  97. #define STORER sdr
  98. #define STORE sd
  99. #define ADD daddu
  100. #define SUB dsubu
  101. #define SRL dsrl
  102. #define SRA dsra
  103. #define SLL dsll
  104. #define SLLV dsllv
  105. #define SRLV dsrlv
  106. #define NBYTES 8
  107. #define LOG_NBYTES 3
  108. /*
  109. * As we are sharing code base with the mips32 tree (which use the o32 ABI
  110. * register definitions). We need to redefine the register definitions from
  111. * the n64 ABI register naming to the o32 ABI register naming.
  112. */
  113. #undef t0
  114. #undef t1
  115. #undef t2
  116. #undef t3
  117. #define t0 $8
  118. #define t1 $9
  119. #define t2 $10
  120. #define t3 $11
  121. #define t4 $12
  122. #define t5 $13
  123. #define t6 $14
  124. #define t7 $15
  125. #else
  126. #define LOAD lw
  127. #define LOADL lwl
  128. #define LOADR lwr
  129. #define STOREL swl
  130. #define STORER swr
  131. #define STORE sw
  132. #define ADD addu
  133. #define SUB subu
  134. #define SRL srl
  135. #define SLL sll
  136. #define SRA sra
  137. #define SLLV sllv
  138. #define SRLV srlv
  139. #define NBYTES 4
  140. #define LOG_NBYTES 2
  141. #endif /* USE_DOUBLE */
  142. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  143. #define LDFIRST LOADR
  144. #define LDREST LOADL
  145. #define STFIRST STORER
  146. #define STREST STOREL
  147. #define SHIFT_DISCARD SLLV
  148. #else
  149. #define LDFIRST LOADL
  150. #define LDREST LOADR
  151. #define STFIRST STOREL
  152. #define STREST STORER
  153. #define SHIFT_DISCARD SRLV
  154. #endif
  155. #define FIRST(unit) ((unit)*NBYTES)
  156. #define REST(unit) (FIRST(unit)+NBYTES-1)
  157. #define UNIT(unit) FIRST(unit)
  158. #define ADDRMASK (NBYTES-1)
  159. .text
  160. .set noreorder
  161. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  162. .set noat
  163. #else
  164. .set at=v1
  165. #endif
  166. /*
  167. * A combined memcpy/__copy_user
  168. * __copy_user sets len to 0 for success; else to an upper bound of
  169. * the number of uncopied bytes.
  170. * memcpy sets v0 to dst.
  171. */
  172. .align 5
  173. LEAF(memcpy) /* a0=dst a1=src a2=len */
  174. move v0, dst /* return value */
  175. .L__memcpy:
  176. FEXPORT(__copy_user)
  177. /*
  178. * Note: dst & src may be unaligned, len may be 0
  179. * Temps
  180. */
  181. #define rem t8
  182. R10KCBARRIER(0(ra))
  183. /*
  184. * The "issue break"s below are very approximate.
  185. * Issue delays for dcache fills will perturb the schedule, as will
  186. * load queue full replay traps, etc.
  187. *
  188. * If len < NBYTES use byte operations.
  189. */
  190. PREF( 0, 0(src) )
  191. PREF( 1, 0(dst) )
  192. sltu t2, len, NBYTES
  193. and t1, dst, ADDRMASK
  194. PREF( 0, 1*32(src) )
  195. PREF( 1, 1*32(dst) )
  196. bnez t2, .Lcopy_bytes_checklen
  197. and t0, src, ADDRMASK
  198. PREF( 0, 2*32(src) )
  199. PREF( 1, 2*32(dst) )
  200. bnez t1, .Ldst_unaligned
  201. nop
  202. bnez t0, .Lsrc_unaligned_dst_aligned
  203. /*
  204. * use delay slot for fall-through
  205. * src and dst are aligned; need to compute rem
  206. */
  207. .Lboth_aligned:
  208. SRL t0, len, LOG_NBYTES+3 # +3 for 8 units/iter
  209. beqz t0, .Lcleanup_both_aligned # len < 8*NBYTES
  210. and rem, len, (8*NBYTES-1) # rem = len % (8*NBYTES)
  211. PREF( 0, 3*32(src) )
  212. PREF( 1, 3*32(dst) )
  213. .align 4
  214. 1:
  215. R10KCBARRIER(0(ra))
  216. EXC( LOAD t0, UNIT(0)(src), .Ll_exc)
  217. EXC( LOAD t1, UNIT(1)(src), .Ll_exc_copy)
  218. EXC( LOAD t2, UNIT(2)(src), .Ll_exc_copy)
  219. EXC( LOAD t3, UNIT(3)(src), .Ll_exc_copy)
  220. SUB len, len, 8*NBYTES
  221. EXC( LOAD t4, UNIT(4)(src), .Ll_exc_copy)
  222. EXC( LOAD t7, UNIT(5)(src), .Ll_exc_copy)
  223. EXC( STORE t0, UNIT(0)(dst), .Ls_exc_p8u)
  224. EXC( STORE t1, UNIT(1)(dst), .Ls_exc_p7u)
  225. EXC( LOAD t0, UNIT(6)(src), .Ll_exc_copy)
  226. EXC( LOAD t1, UNIT(7)(src), .Ll_exc_copy)
  227. ADD src, src, 8*NBYTES
  228. ADD dst, dst, 8*NBYTES
  229. EXC( STORE t2, UNIT(-6)(dst), .Ls_exc_p6u)
  230. EXC( STORE t3, UNIT(-5)(dst), .Ls_exc_p5u)
  231. EXC( STORE t4, UNIT(-4)(dst), .Ls_exc_p4u)
  232. EXC( STORE t7, UNIT(-3)(dst), .Ls_exc_p3u)
  233. EXC( STORE t0, UNIT(-2)(dst), .Ls_exc_p2u)
  234. EXC( STORE t1, UNIT(-1)(dst), .Ls_exc_p1u)
  235. PREF( 0, 8*32(src) )
  236. PREF( 1, 8*32(dst) )
  237. bne len, rem, 1b
  238. nop
  239. /*
  240. * len == rem == the number of bytes left to copy < 8*NBYTES
  241. */
  242. .Lcleanup_both_aligned:
  243. beqz len, .Ldone
  244. sltu t0, len, 4*NBYTES
  245. bnez t0, .Lless_than_4units
  246. and rem, len, (NBYTES-1) # rem = len % NBYTES
  247. /*
  248. * len >= 4*NBYTES
  249. */
  250. EXC( LOAD t0, UNIT(0)(src), .Ll_exc)
  251. EXC( LOAD t1, UNIT(1)(src), .Ll_exc_copy)
  252. EXC( LOAD t2, UNIT(2)(src), .Ll_exc_copy)
  253. EXC( LOAD t3, UNIT(3)(src), .Ll_exc_copy)
  254. SUB len, len, 4*NBYTES
  255. ADD src, src, 4*NBYTES
  256. R10KCBARRIER(0(ra))
  257. EXC( STORE t0, UNIT(0)(dst), .Ls_exc_p4u)
  258. EXC( STORE t1, UNIT(1)(dst), .Ls_exc_p3u)
  259. EXC( STORE t2, UNIT(2)(dst), .Ls_exc_p2u)
  260. EXC( STORE t3, UNIT(3)(dst), .Ls_exc_p1u)
  261. .set reorder /* DADDI_WAR */
  262. ADD dst, dst, 4*NBYTES
  263. beqz len, .Ldone
  264. .set noreorder
  265. .Lless_than_4units:
  266. /*
  267. * rem = len % NBYTES
  268. */
  269. beq rem, len, .Lcopy_bytes
  270. nop
  271. 1:
  272. R10KCBARRIER(0(ra))
  273. EXC( LOAD t0, 0(src), .Ll_exc)
  274. ADD src, src, NBYTES
  275. SUB len, len, NBYTES
  276. EXC( STORE t0, 0(dst), .Ls_exc_p1u)
  277. .set reorder /* DADDI_WAR */
  278. ADD dst, dst, NBYTES
  279. bne rem, len, 1b
  280. .set noreorder
  281. /*
  282. * src and dst are aligned, need to copy rem bytes (rem < NBYTES)
  283. * A loop would do only a byte at a time with possible branch
  284. * mispredicts. Can't do an explicit LOAD dst,mask,or,STORE
  285. * because can't assume read-access to dst. Instead, use
  286. * STREST dst, which doesn't require read access to dst.
  287. *
  288. * This code should perform better than a simple loop on modern,
  289. * wide-issue mips processors because the code has fewer branches and
  290. * more instruction-level parallelism.
  291. */
  292. #define bits t2
  293. beqz len, .Ldone
  294. ADD t1, dst, len # t1 is just past last byte of dst
  295. li bits, 8*NBYTES
  296. SLL rem, len, 3 # rem = number of bits to keep
  297. EXC( LOAD t0, 0(src), .Ll_exc)
  298. SUB bits, bits, rem # bits = number of bits to discard
  299. SHIFT_DISCARD t0, t0, bits
  300. EXC( STREST t0, -1(t1), .Ls_exc)
  301. jr ra
  302. move len, zero
  303. .Ldst_unaligned:
  304. /*
  305. * dst is unaligned
  306. * t0 = src & ADDRMASK
  307. * t1 = dst & ADDRMASK; T1 > 0
  308. * len >= NBYTES
  309. *
  310. * Copy enough bytes to align dst
  311. * Set match = (src and dst have same alignment)
  312. */
  313. #define match rem
  314. EXC( LDFIRST t3, FIRST(0)(src), .Ll_exc)
  315. ADD t2, zero, NBYTES
  316. EXC( LDREST t3, REST(0)(src), .Ll_exc_copy)
  317. SUB t2, t2, t1 # t2 = number of bytes copied
  318. xor match, t0, t1
  319. R10KCBARRIER(0(ra))
  320. EXC( STFIRST t3, FIRST(0)(dst), .Ls_exc)
  321. beq len, t2, .Ldone
  322. SUB len, len, t2
  323. ADD dst, dst, t2
  324. beqz match, .Lboth_aligned
  325. ADD src, src, t2
  326. .Lsrc_unaligned_dst_aligned:
  327. SRL t0, len, LOG_NBYTES+2 # +2 for 4 units/iter
  328. PREF( 0, 3*32(src) )
  329. beqz t0, .Lcleanup_src_unaligned
  330. and rem, len, (4*NBYTES-1) # rem = len % 4*NBYTES
  331. PREF( 1, 3*32(dst) )
  332. 1:
  333. /*
  334. * Avoid consecutive LD*'s to the same register since some mips
  335. * implementations can't issue them in the same cycle.
  336. * It's OK to load FIRST(N+1) before REST(N) because the two addresses
  337. * are to the same unit (unless src is aligned, but it's not).
  338. */
  339. R10KCBARRIER(0(ra))
  340. EXC( LDFIRST t0, FIRST(0)(src), .Ll_exc)
  341. EXC( LDFIRST t1, FIRST(1)(src), .Ll_exc_copy)
  342. SUB len, len, 4*NBYTES
  343. EXC( LDREST t0, REST(0)(src), .Ll_exc_copy)
  344. EXC( LDREST t1, REST(1)(src), .Ll_exc_copy)
  345. EXC( LDFIRST t2, FIRST(2)(src), .Ll_exc_copy)
  346. EXC( LDFIRST t3, FIRST(3)(src), .Ll_exc_copy)
  347. EXC( LDREST t2, REST(2)(src), .Ll_exc_copy)
  348. EXC( LDREST t3, REST(3)(src), .Ll_exc_copy)
  349. PREF( 0, 9*32(src) ) # 0 is PREF_LOAD (not streamed)
  350. ADD src, src, 4*NBYTES
  351. #ifdef CONFIG_CPU_SB1
  352. nop # improves slotting
  353. #endif
  354. EXC( STORE t0, UNIT(0)(dst), .Ls_exc_p4u)
  355. EXC( STORE t1, UNIT(1)(dst), .Ls_exc_p3u)
  356. EXC( STORE t2, UNIT(2)(dst), .Ls_exc_p2u)
  357. EXC( STORE t3, UNIT(3)(dst), .Ls_exc_p1u)
  358. PREF( 1, 9*32(dst) ) # 1 is PREF_STORE (not streamed)
  359. .set reorder /* DADDI_WAR */
  360. ADD dst, dst, 4*NBYTES
  361. bne len, rem, 1b
  362. .set noreorder
  363. .Lcleanup_src_unaligned:
  364. beqz len, .Ldone
  365. and rem, len, NBYTES-1 # rem = len % NBYTES
  366. beq rem, len, .Lcopy_bytes
  367. nop
  368. 1:
  369. R10KCBARRIER(0(ra))
  370. EXC( LDFIRST t0, FIRST(0)(src), .Ll_exc)
  371. EXC( LDREST t0, REST(0)(src), .Ll_exc_copy)
  372. ADD src, src, NBYTES
  373. SUB len, len, NBYTES
  374. EXC( STORE t0, 0(dst), .Ls_exc_p1u)
  375. .set reorder /* DADDI_WAR */
  376. ADD dst, dst, NBYTES
  377. bne len, rem, 1b
  378. .set noreorder
  379. .Lcopy_bytes_checklen:
  380. beqz len, .Ldone
  381. nop
  382. .Lcopy_bytes:
  383. /* 0 < len < NBYTES */
  384. R10KCBARRIER(0(ra))
  385. #define COPY_BYTE(N) \
  386. EXC( lb t0, N(src), .Ll_exc); \
  387. SUB len, len, 1; \
  388. beqz len, .Ldone; \
  389. EXC( sb t0, N(dst), .Ls_exc_p1)
  390. COPY_BYTE(0)
  391. COPY_BYTE(1)
  392. #ifdef USE_DOUBLE
  393. COPY_BYTE(2)
  394. COPY_BYTE(3)
  395. COPY_BYTE(4)
  396. COPY_BYTE(5)
  397. #endif
  398. EXC( lb t0, NBYTES-2(src), .Ll_exc)
  399. SUB len, len, 1
  400. jr ra
  401. EXC( sb t0, NBYTES-2(dst), .Ls_exc_p1)
  402. .Ldone:
  403. jr ra
  404. nop
  405. END(memcpy)
  406. .Ll_exc_copy:
  407. /*
  408. * Copy bytes from src until faulting load address (or until a
  409. * lb faults)
  410. *
  411. * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
  412. * may be more than a byte beyond the last address.
  413. * Hence, the lb below may get an exception.
  414. *
  415. * Assumes src < THREAD_BUADDR($28)
  416. */
  417. LOAD t0, TI_TASK($28)
  418. nop
  419. LOAD t0, THREAD_BUADDR(t0)
  420. 1:
  421. EXC( lb t1, 0(src), .Ll_exc)
  422. ADD src, src, 1
  423. sb t1, 0(dst) # can't fault -- we're copy_from_user
  424. .set reorder /* DADDI_WAR */
  425. ADD dst, dst, 1
  426. bne src, t0, 1b
  427. .set noreorder
  428. .Ll_exc:
  429. LOAD t0, TI_TASK($28)
  430. nop
  431. LOAD t0, THREAD_BUADDR(t0) # t0 is just past last good address
  432. nop
  433. SUB len, AT, t0 # len number of uncopied bytes
  434. /*
  435. * Here's where we rely on src and dst being incremented in tandem,
  436. * See (3) above.
  437. * dst += (fault addr - src) to put dst at first byte to clear
  438. */
  439. ADD dst, t0 # compute start address in a1
  440. SUB dst, src
  441. /*
  442. * Clear len bytes starting at dst. Can't call __bzero because it
  443. * might modify len. An inefficient loop for these rare times...
  444. */
  445. .set reorder /* DADDI_WAR */
  446. SUB src, len, 1
  447. beqz len, .Ldone
  448. .set noreorder
  449. 1: sb zero, 0(dst)
  450. ADD dst, dst, 1
  451. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  452. bnez src, 1b
  453. SUB src, src, 1
  454. #else
  455. .set push
  456. .set noat
  457. li v1, 1
  458. bnez src, 1b
  459. SUB src, src, v1
  460. .set pop
  461. #endif
  462. jr ra
  463. nop
  464. #define SEXC(n) \
  465. .set reorder; /* DADDI_WAR */ \
  466. .Ls_exc_p ## n ## u: \
  467. ADD len, len, n*NBYTES; \
  468. jr ra; \
  469. .set noreorder
  470. SEXC(8)
  471. SEXC(7)
  472. SEXC(6)
  473. SEXC(5)
  474. SEXC(4)
  475. SEXC(3)
  476. SEXC(2)
  477. SEXC(1)
  478. .Ls_exc_p1:
  479. .set reorder /* DADDI_WAR */
  480. ADD len, len, 1
  481. jr ra
  482. .set noreorder
  483. .Ls_exc:
  484. jr ra
  485. nop
  486. .align 5
  487. LEAF(memmove)
  488. ADD t0, a0, a2
  489. ADD t1, a1, a2
  490. sltu t0, a1, t0 # dst + len <= src -> memcpy
  491. sltu t1, a0, t1 # dst >= src + len -> memcpy
  492. and t0, t1
  493. beqz t0, .L__memcpy
  494. move v0, a0 /* return value */
  495. beqz a2, .Lr_out
  496. END(memmove)
  497. /* fall through to __rmemcpy */
  498. LEAF(__rmemcpy) /* a0=dst a1=src a2=len */
  499. sltu t0, a1, a0
  500. beqz t0, .Lr_end_bytes_up # src >= dst
  501. nop
  502. ADD a0, a2 # dst = dst + len
  503. ADD a1, a2 # src = src + len
  504. .Lr_end_bytes:
  505. R10KCBARRIER(0(ra))
  506. lb t0, -1(a1)
  507. SUB a2, a2, 0x1
  508. sb t0, -1(a0)
  509. SUB a1, a1, 0x1
  510. .set reorder /* DADDI_WAR */
  511. SUB a0, a0, 0x1
  512. bnez a2, .Lr_end_bytes
  513. .set noreorder
  514. .Lr_out:
  515. jr ra
  516. move a2, zero
  517. .Lr_end_bytes_up:
  518. R10KCBARRIER(0(ra))
  519. lb t0, (a1)
  520. SUB a2, a2, 0x1
  521. sb t0, (a0)
  522. ADD a1, a1, 0x1
  523. .set reorder /* DADDI_WAR */
  524. ADD a0, a0, 0x1
  525. bnez a2, .Lr_end_bytes_up
  526. .set noreorder
  527. jr ra
  528. move a2, zero
  529. END(__rmemcpy)