memcpy.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. #include <linux/config.h>
  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. #if !defined(CONFIG_DMA_COHERENT) || !defined(CONFIG_DMA_IP27)
  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. .set noat
  162. /*
  163. * A combined memcpy/__copy_user
  164. * __copy_user sets len to 0 for success; else to an upper bound of
  165. * the number of uncopied bytes.
  166. * memcpy sets v0 to dst.
  167. */
  168. .align 5
  169. LEAF(memcpy) /* a0=dst a1=src a2=len */
  170. move v0, dst /* return value */
  171. __memcpy:
  172. FEXPORT(__copy_user)
  173. /*
  174. * Note: dst & src may be unaligned, len may be 0
  175. * Temps
  176. */
  177. #define rem t8
  178. /*
  179. * The "issue break"s below are very approximate.
  180. * Issue delays for dcache fills will perturb the schedule, as will
  181. * load queue full replay traps, etc.
  182. *
  183. * If len < NBYTES use byte operations.
  184. */
  185. PREF( 0, 0(src) )
  186. PREF( 1, 0(dst) )
  187. sltu t2, len, NBYTES
  188. and t1, dst, ADDRMASK
  189. PREF( 0, 1*32(src) )
  190. PREF( 1, 1*32(dst) )
  191. bnez t2, copy_bytes_checklen
  192. and t0, src, ADDRMASK
  193. PREF( 0, 2*32(src) )
  194. PREF( 1, 2*32(dst) )
  195. bnez t1, dst_unaligned
  196. nop
  197. bnez t0, src_unaligned_dst_aligned
  198. /*
  199. * use delay slot for fall-through
  200. * src and dst are aligned; need to compute rem
  201. */
  202. both_aligned:
  203. SRL t0, len, LOG_NBYTES+3 # +3 for 8 units/iter
  204. beqz t0, cleanup_both_aligned # len < 8*NBYTES
  205. and rem, len, (8*NBYTES-1) # rem = len % (8*NBYTES)
  206. PREF( 0, 3*32(src) )
  207. PREF( 1, 3*32(dst) )
  208. .align 4
  209. 1:
  210. EXC( LOAD t0, UNIT(0)(src), l_exc)
  211. EXC( LOAD t1, UNIT(1)(src), l_exc_copy)
  212. EXC( LOAD t2, UNIT(2)(src), l_exc_copy)
  213. EXC( LOAD t3, UNIT(3)(src), l_exc_copy)
  214. SUB len, len, 8*NBYTES
  215. EXC( LOAD t4, UNIT(4)(src), l_exc_copy)
  216. EXC( LOAD t7, UNIT(5)(src), l_exc_copy)
  217. EXC( STORE t0, UNIT(0)(dst), s_exc_p8u)
  218. EXC( STORE t1, UNIT(1)(dst), s_exc_p7u)
  219. EXC( LOAD t0, UNIT(6)(src), l_exc_copy)
  220. EXC( LOAD t1, UNIT(7)(src), l_exc_copy)
  221. ADD src, src, 8*NBYTES
  222. ADD dst, dst, 8*NBYTES
  223. EXC( STORE t2, UNIT(-6)(dst), s_exc_p6u)
  224. EXC( STORE t3, UNIT(-5)(dst), s_exc_p5u)
  225. EXC( STORE t4, UNIT(-4)(dst), s_exc_p4u)
  226. EXC( STORE t7, UNIT(-3)(dst), s_exc_p3u)
  227. EXC( STORE t0, UNIT(-2)(dst), s_exc_p2u)
  228. EXC( STORE t1, UNIT(-1)(dst), s_exc_p1u)
  229. PREF( 0, 8*32(src) )
  230. PREF( 1, 8*32(dst) )
  231. bne len, rem, 1b
  232. nop
  233. /*
  234. * len == rem == the number of bytes left to copy < 8*NBYTES
  235. */
  236. cleanup_both_aligned:
  237. beqz len, done
  238. sltu t0, len, 4*NBYTES
  239. bnez t0, less_than_4units
  240. and rem, len, (NBYTES-1) # rem = len % NBYTES
  241. /*
  242. * len >= 4*NBYTES
  243. */
  244. EXC( LOAD t0, UNIT(0)(src), l_exc)
  245. EXC( LOAD t1, UNIT(1)(src), l_exc_copy)
  246. EXC( LOAD t2, UNIT(2)(src), l_exc_copy)
  247. EXC( LOAD t3, UNIT(3)(src), l_exc_copy)
  248. SUB len, len, 4*NBYTES
  249. ADD src, src, 4*NBYTES
  250. EXC( STORE t0, UNIT(0)(dst), s_exc_p4u)
  251. EXC( STORE t1, UNIT(1)(dst), s_exc_p3u)
  252. EXC( STORE t2, UNIT(2)(dst), s_exc_p2u)
  253. EXC( STORE t3, UNIT(3)(dst), s_exc_p1u)
  254. beqz len, done
  255. ADD dst, dst, 4*NBYTES
  256. less_than_4units:
  257. /*
  258. * rem = len % NBYTES
  259. */
  260. beq rem, len, copy_bytes
  261. nop
  262. 1:
  263. EXC( LOAD t0, 0(src), l_exc)
  264. ADD src, src, NBYTES
  265. SUB len, len, NBYTES
  266. EXC( STORE t0, 0(dst), s_exc_p1u)
  267. bne rem, len, 1b
  268. ADD dst, dst, NBYTES
  269. /*
  270. * src and dst are aligned, need to copy rem bytes (rem < NBYTES)
  271. * A loop would do only a byte at a time with possible branch
  272. * mispredicts. Can't do an explicit LOAD dst,mask,or,STORE
  273. * because can't assume read-access to dst. Instead, use
  274. * STREST dst, which doesn't require read access to dst.
  275. *
  276. * This code should perform better than a simple loop on modern,
  277. * wide-issue mips processors because the code has fewer branches and
  278. * more instruction-level parallelism.
  279. */
  280. #define bits t2
  281. beqz len, done
  282. ADD t1, dst, len # t1 is just past last byte of dst
  283. li bits, 8*NBYTES
  284. SLL rem, len, 3 # rem = number of bits to keep
  285. EXC( LOAD t0, 0(src), l_exc)
  286. SUB bits, bits, rem # bits = number of bits to discard
  287. SHIFT_DISCARD t0, t0, bits
  288. EXC( STREST t0, -1(t1), s_exc)
  289. jr ra
  290. move len, zero
  291. dst_unaligned:
  292. /*
  293. * dst is unaligned
  294. * t0 = src & ADDRMASK
  295. * t1 = dst & ADDRMASK; T1 > 0
  296. * len >= NBYTES
  297. *
  298. * Copy enough bytes to align dst
  299. * Set match = (src and dst have same alignment)
  300. */
  301. #define match rem
  302. EXC( LDFIRST t3, FIRST(0)(src), l_exc)
  303. ADD t2, zero, NBYTES
  304. EXC( LDREST t3, REST(0)(src), l_exc_copy)
  305. SUB t2, t2, t1 # t2 = number of bytes copied
  306. xor match, t0, t1
  307. EXC( STFIRST t3, FIRST(0)(dst), s_exc)
  308. beq len, t2, done
  309. SUB len, len, t2
  310. ADD dst, dst, t2
  311. beqz match, both_aligned
  312. ADD src, src, t2
  313. src_unaligned_dst_aligned:
  314. SRL t0, len, LOG_NBYTES+2 # +2 for 4 units/iter
  315. PREF( 0, 3*32(src) )
  316. beqz t0, cleanup_src_unaligned
  317. and rem, len, (4*NBYTES-1) # rem = len % 4*NBYTES
  318. PREF( 1, 3*32(dst) )
  319. 1:
  320. /*
  321. * Avoid consecutive LD*'s to the same register since some mips
  322. * implementations can't issue them in the same cycle.
  323. * It's OK to load FIRST(N+1) before REST(N) because the two addresses
  324. * are to the same unit (unless src is aligned, but it's not).
  325. */
  326. EXC( LDFIRST t0, FIRST(0)(src), l_exc)
  327. EXC( LDFIRST t1, FIRST(1)(src), l_exc_copy)
  328. SUB len, len, 4*NBYTES
  329. EXC( LDREST t0, REST(0)(src), l_exc_copy)
  330. EXC( LDREST t1, REST(1)(src), l_exc_copy)
  331. EXC( LDFIRST t2, FIRST(2)(src), l_exc_copy)
  332. EXC( LDFIRST t3, FIRST(3)(src), l_exc_copy)
  333. EXC( LDREST t2, REST(2)(src), l_exc_copy)
  334. EXC( LDREST t3, REST(3)(src), l_exc_copy)
  335. PREF( 0, 9*32(src) ) # 0 is PREF_LOAD (not streamed)
  336. ADD src, src, 4*NBYTES
  337. #ifdef CONFIG_CPU_SB1
  338. nop # improves slotting
  339. #endif
  340. EXC( STORE t0, UNIT(0)(dst), s_exc_p4u)
  341. EXC( STORE t1, UNIT(1)(dst), s_exc_p3u)
  342. EXC( STORE t2, UNIT(2)(dst), s_exc_p2u)
  343. EXC( STORE t3, UNIT(3)(dst), s_exc_p1u)
  344. PREF( 1, 9*32(dst) ) # 1 is PREF_STORE (not streamed)
  345. bne len, rem, 1b
  346. ADD dst, dst, 4*NBYTES
  347. cleanup_src_unaligned:
  348. beqz len, done
  349. and rem, len, NBYTES-1 # rem = len % NBYTES
  350. beq rem, len, copy_bytes
  351. nop
  352. 1:
  353. EXC( LDFIRST t0, FIRST(0)(src), l_exc)
  354. EXC( LDREST t0, REST(0)(src), l_exc_copy)
  355. ADD src, src, NBYTES
  356. SUB len, len, NBYTES
  357. EXC( STORE t0, 0(dst), s_exc_p1u)
  358. bne len, rem, 1b
  359. ADD dst, dst, NBYTES
  360. copy_bytes_checklen:
  361. beqz len, done
  362. nop
  363. copy_bytes:
  364. /* 0 < len < NBYTES */
  365. #define COPY_BYTE(N) \
  366. EXC( lb t0, N(src), l_exc); \
  367. SUB len, len, 1; \
  368. beqz len, done; \
  369. EXC( sb t0, N(dst), s_exc_p1)
  370. COPY_BYTE(0)
  371. COPY_BYTE(1)
  372. #ifdef USE_DOUBLE
  373. COPY_BYTE(2)
  374. COPY_BYTE(3)
  375. COPY_BYTE(4)
  376. COPY_BYTE(5)
  377. #endif
  378. EXC( lb t0, NBYTES-2(src), l_exc)
  379. SUB len, len, 1
  380. jr ra
  381. EXC( sb t0, NBYTES-2(dst), s_exc_p1)
  382. done:
  383. jr ra
  384. nop
  385. END(memcpy)
  386. l_exc_copy:
  387. /*
  388. * Copy bytes from src until faulting load address (or until a
  389. * lb faults)
  390. *
  391. * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
  392. * may be more than a byte beyond the last address.
  393. * Hence, the lb below may get an exception.
  394. *
  395. * Assumes src < THREAD_BUADDR($28)
  396. */
  397. LOAD t0, TI_TASK($28)
  398. nop
  399. LOAD t0, THREAD_BUADDR(t0)
  400. 1:
  401. EXC( lb t1, 0(src), l_exc)
  402. ADD src, src, 1
  403. sb t1, 0(dst) # can't fault -- we're copy_from_user
  404. bne src, t0, 1b
  405. ADD dst, dst, 1
  406. l_exc:
  407. LOAD t0, TI_TASK($28)
  408. nop
  409. LOAD t0, THREAD_BUADDR(t0) # t0 is just past last good address
  410. nop
  411. SUB len, AT, t0 # len number of uncopied bytes
  412. /*
  413. * Here's where we rely on src and dst being incremented in tandem,
  414. * See (3) above.
  415. * dst += (fault addr - src) to put dst at first byte to clear
  416. */
  417. ADD dst, t0 # compute start address in a1
  418. SUB dst, src
  419. /*
  420. * Clear len bytes starting at dst. Can't call __bzero because it
  421. * might modify len. An inefficient loop for these rare times...
  422. */
  423. beqz len, done
  424. SUB src, len, 1
  425. 1: sb zero, 0(dst)
  426. ADD dst, dst, 1
  427. bnez src, 1b
  428. SUB src, src, 1
  429. jr ra
  430. nop
  431. #define SEXC(n) \
  432. s_exc_p ## n ## u: \
  433. jr ra; \
  434. ADD len, len, n*NBYTES
  435. SEXC(8)
  436. SEXC(7)
  437. SEXC(6)
  438. SEXC(5)
  439. SEXC(4)
  440. SEXC(3)
  441. SEXC(2)
  442. SEXC(1)
  443. s_exc_p1:
  444. jr ra
  445. ADD len, len, 1
  446. s_exc:
  447. jr ra
  448. nop
  449. .align 5
  450. LEAF(memmove)
  451. ADD t0, a0, a2
  452. ADD t1, a1, a2
  453. sltu t0, a1, t0 # dst + len <= src -> memcpy
  454. sltu t1, a0, t1 # dst >= src + len -> memcpy
  455. and t0, t1
  456. beqz t0, __memcpy
  457. move v0, a0 /* return value */
  458. beqz a2, r_out
  459. END(memmove)
  460. /* fall through to __rmemcpy */
  461. LEAF(__rmemcpy) /* a0=dst a1=src a2=len */
  462. sltu t0, a1, a0
  463. beqz t0, r_end_bytes_up # src >= dst
  464. nop
  465. ADD a0, a2 # dst = dst + len
  466. ADD a1, a2 # src = src + len
  467. r_end_bytes:
  468. lb t0, -1(a1)
  469. SUB a2, a2, 0x1
  470. sb t0, -1(a0)
  471. SUB a1, a1, 0x1
  472. bnez a2, r_end_bytes
  473. SUB a0, a0, 0x1
  474. r_out:
  475. jr ra
  476. move a2, zero
  477. r_end_bytes_up:
  478. lb t0, (a1)
  479. SUB a2, a2, 0x1
  480. sb t0, (a0)
  481. ADD a1, a1, 0x1
  482. bnez a2, r_end_bytes_up
  483. ADD a0, a0, 0x1
  484. jr ra
  485. move a2, zero
  486. END(__rmemcpy)