memcpy.S 12 KB

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