memcpy.S 13 KB

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