memcpy.S 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 ([email protected])
  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. * Copyright (C) 2014 Imagination Technologies Ltd.
  14. *
  15. * Mnemonic names for arguments to memcpy/__copy_user
  16. */
  17. /*
  18. * Hack to resolve longstanding prefetch issue
  19. *
  20. * Prefetching may be fatal on some systems if we're prefetching beyond the
  21. * end of memory on some systems. It's also a seriously bad idea on non
  22. * dma-coherent systems.
  23. */
  24. #ifdef CONFIG_DMA_NONCOHERENT
  25. #undef CONFIG_CPU_HAS_PREFETCH
  26. #endif
  27. #ifdef CONFIG_MIPS_MALTA
  28. #undef CONFIG_CPU_HAS_PREFETCH
  29. #endif
  30. #ifdef CONFIG_CPU_MIPSR6
  31. #undef CONFIG_CPU_HAS_PREFETCH
  32. #endif
  33. #include <asm/asm.h>
  34. #include <asm/asm-offsets.h>
  35. #include <asm/export.h>
  36. #include <asm/regdef.h>
  37. #define dst a0
  38. #define src a1
  39. #define len a2
  40. /*
  41. * Spec
  42. *
  43. * memcpy copies len bytes from src to dst and sets v0 to dst.
  44. * It assumes that
  45. * - src and dst don't overlap
  46. * - src is readable
  47. * - dst is writable
  48. * memcpy uses the standard calling convention
  49. *
  50. * __copy_user copies up to len bytes from src to dst and sets a2 (len) to
  51. * the number of uncopied bytes due to an exception caused by a read or write.
  52. * __copy_user assumes that src and dst don't overlap, and that the call is
  53. * implementing one of the following:
  54. * copy_to_user
  55. * - src is readable (no exceptions when reading src)
  56. * copy_from_user
  57. * - dst is writable (no exceptions when writing dst)
  58. * __copy_user uses a non-standard calling convention; see
  59. * include/asm-mips/uaccess.h
  60. *
  61. * When an exception happens on a load, the handler must
  62. # ensure that all of the destination buffer is overwritten to prevent
  63. * leaking information to user mode programs.
  64. */
  65. /*
  66. * Implementation
  67. */
  68. /*
  69. * The exception handler for loads requires that:
  70. * 1- AT contain the address of the byte just past the end of the source
  71. * of the copy,
  72. * 2- src_entry <= src < AT, and
  73. * 3- (dst - src) == (dst_entry - src_entry),
  74. * The _entry suffix denotes values when __copy_user was called.
  75. *
  76. * (1) is set up up by uaccess.h and maintained by not writing AT in copy_user
  77. * (2) is met by incrementing src by the number of bytes copied
  78. * (3) is met by not doing loads between a pair of increments of dst and src
  79. *
  80. * The exception handlers for stores adjust len (if necessary) and return.
  81. * These handlers do not need to overwrite any data.
  82. *
  83. * For __rmemcpy and memmove an exception is always a kernel bug, therefore
  84. * they're not protected.
  85. */
  86. /* Instruction type */
  87. #define LD_INSN 1
  88. #define ST_INSN 2
  89. /* Pretech type */
  90. #define SRC_PREFETCH 1
  91. #define DST_PREFETCH 2
  92. #define LEGACY_MODE 1
  93. #define EVA_MODE 2
  94. #define USEROP 1
  95. #define KERNELOP 2
  96. /*
  97. * Wrapper to add an entry in the exception table
  98. * in case the insn causes a memory exception.
  99. * Arguments:
  100. * insn : Load/store instruction
  101. * type : Instruction type
  102. * reg : Register
  103. * addr : Address
  104. * handler : Exception handler
  105. */
  106. #define EXC(insn, type, reg, addr, handler) \
  107. .if \mode == LEGACY_MODE; \
  108. 9: insn reg, addr; \
  109. .section __ex_table,"a"; \
  110. PTR_WD 9b, handler; \
  111. .previous; \
  112. /* This is assembled in EVA mode */ \
  113. .else; \
  114. /* If loading from user or storing to user */ \
  115. .if ((\from == USEROP) && (type == LD_INSN)) || \
  116. ((\to == USEROP) && (type == ST_INSN)); \
  117. 9: __BUILD_EVA_INSN(insn##e, reg, addr); \
  118. .section __ex_table,"a"; \
  119. PTR_WD 9b, handler; \
  120. .previous; \
  121. .else; \
  122. /* \
  123. * Still in EVA, but no need for \
  124. * exception handler or EVA insn \
  125. */ \
  126. insn reg, addr; \
  127. .endif; \
  128. .endif
  129. /*
  130. * Only on the 64-bit kernel we can made use of 64-bit registers.
  131. */
  132. #ifdef CONFIG_64BIT
  133. #define USE_DOUBLE
  134. #endif
  135. #ifdef USE_DOUBLE
  136. #define LOADK ld /* No exception */
  137. #define LOAD(reg, addr, handler) EXC(ld, LD_INSN, reg, addr, handler)
  138. #define LOADL(reg, addr, handler) EXC(ldl, LD_INSN, reg, addr, handler)
  139. #define LOADR(reg, addr, handler) EXC(ldr, LD_INSN, reg, addr, handler)
  140. #define STOREL(reg, addr, handler) EXC(sdl, ST_INSN, reg, addr, handler)
  141. #define STORER(reg, addr, handler) EXC(sdr, ST_INSN, reg, addr, handler)
  142. #define STORE(reg, addr, handler) EXC(sd, ST_INSN, reg, addr, handler)
  143. #define ADD daddu
  144. #define SUB dsubu
  145. #define SRL dsrl
  146. #define SRA dsra
  147. #define SLL dsll
  148. #define SLLV dsllv
  149. #define SRLV dsrlv
  150. #define NBYTES 8
  151. #define LOG_NBYTES 3
  152. /*
  153. * As we are sharing code base with the mips32 tree (which use the o32 ABI
  154. * register definitions). We need to redefine the register definitions from
  155. * the n64 ABI register naming to the o32 ABI register naming.
  156. */
  157. #undef t0
  158. #undef t1
  159. #undef t2
  160. #undef t3
  161. #define t0 $8
  162. #define t1 $9
  163. #define t2 $10
  164. #define t3 $11
  165. #define t4 $12
  166. #define t5 $13
  167. #define t6 $14
  168. #define t7 $15
  169. #else
  170. #define LOADK lw /* No exception */
  171. #define LOAD(reg, addr, handler) EXC(lw, LD_INSN, reg, addr, handler)
  172. #define LOADL(reg, addr, handler) EXC(lwl, LD_INSN, reg, addr, handler)
  173. #define LOADR(reg, addr, handler) EXC(lwr, LD_INSN, reg, addr, handler)
  174. #define STOREL(reg, addr, handler) EXC(swl, ST_INSN, reg, addr, handler)
  175. #define STORER(reg, addr, handler) EXC(swr, ST_INSN, reg, addr, handler)
  176. #define STORE(reg, addr, handler) EXC(sw, ST_INSN, reg, addr, handler)
  177. #define ADD addu
  178. #define SUB subu
  179. #define SRL srl
  180. #define SLL sll
  181. #define SRA sra
  182. #define SLLV sllv
  183. #define SRLV srlv
  184. #define NBYTES 4
  185. #define LOG_NBYTES 2
  186. #endif /* USE_DOUBLE */
  187. #define LOADB(reg, addr, handler) EXC(lb, LD_INSN, reg, addr, handler)
  188. #define STOREB(reg, addr, handler) EXC(sb, ST_INSN, reg, addr, handler)
  189. #ifdef CONFIG_CPU_HAS_PREFETCH
  190. # define _PREF(hint, addr, type) \
  191. .if \mode == LEGACY_MODE; \
  192. kernel_pref(hint, addr); \
  193. .else; \
  194. .if ((\from == USEROP) && (type == SRC_PREFETCH)) || \
  195. ((\to == USEROP) && (type == DST_PREFETCH)); \
  196. /* \
  197. * PREFE has only 9 bits for the offset \
  198. * compared to PREF which has 16, so it may \
  199. * need to use the $at register but this \
  200. * register should remain intact because it's \
  201. * used later on. Therefore use $v1. \
  202. */ \
  203. .set at=v1; \
  204. user_pref(hint, addr); \
  205. .set noat; \
  206. .else; \
  207. kernel_pref(hint, addr); \
  208. .endif; \
  209. .endif
  210. #else
  211. # define _PREF(hint, addr, type)
  212. #endif
  213. #define PREFS(hint, addr) _PREF(hint, addr, SRC_PREFETCH)
  214. #define PREFD(hint, addr) _PREF(hint, addr, DST_PREFETCH)
  215. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  216. #define LDFIRST LOADR
  217. #define LDREST LOADL
  218. #define STFIRST STORER
  219. #define STREST STOREL
  220. #define SHIFT_DISCARD SLLV
  221. #else
  222. #define LDFIRST LOADL
  223. #define LDREST LOADR
  224. #define STFIRST STOREL
  225. #define STREST STORER
  226. #define SHIFT_DISCARD SRLV
  227. #endif
  228. #define FIRST(unit) ((unit)*NBYTES)
  229. #define REST(unit) (FIRST(unit)+NBYTES-1)
  230. #define UNIT(unit) FIRST(unit)
  231. #define ADDRMASK (NBYTES-1)
  232. .text
  233. .set noreorder
  234. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  235. .set noat
  236. #else
  237. .set at=v1
  238. #endif
  239. .align 5
  240. /*
  241. * Macro to build the __copy_user common code
  242. * Arguments:
  243. * mode : LEGACY_MODE or EVA_MODE
  244. * from : Source operand. USEROP or KERNELOP
  245. * to : Destination operand. USEROP or KERNELOP
  246. */
  247. .macro __BUILD_COPY_USER mode, from, to
  248. /* initialize __memcpy if this the first time we execute this macro */
  249. .ifnotdef __memcpy
  250. .set __memcpy, 1
  251. .hidden __memcpy /* make sure it does not leak */
  252. .endif
  253. /*
  254. * Note: dst & src may be unaligned, len may be 0
  255. * Temps
  256. */
  257. #define rem t8
  258. R10KCBARRIER(0(ra))
  259. /*
  260. * The "issue break"s below are very approximate.
  261. * Issue delays for dcache fills will perturb the schedule, as will
  262. * load queue full replay traps, etc.
  263. *
  264. * If len < NBYTES use byte operations.
  265. */
  266. PREFS( 0, 0(src) )
  267. PREFD( 1, 0(dst) )
  268. sltu t2, len, NBYTES
  269. and t1, dst, ADDRMASK
  270. PREFS( 0, 1*32(src) )
  271. PREFD( 1, 1*32(dst) )
  272. bnez t2, .Lcopy_bytes_checklen\@
  273. and t0, src, ADDRMASK
  274. PREFS( 0, 2*32(src) )
  275. PREFD( 1, 2*32(dst) )
  276. #ifndef CONFIG_CPU_NO_LOAD_STORE_LR
  277. bnez t1, .Ldst_unaligned\@
  278. nop
  279. bnez t0, .Lsrc_unaligned_dst_aligned\@
  280. #else /* CONFIG_CPU_NO_LOAD_STORE_LR */
  281. or t0, t0, t1
  282. bnez t0, .Lcopy_unaligned_bytes\@
  283. #endif /* CONFIG_CPU_NO_LOAD_STORE_LR */
  284. /*
  285. * use delay slot for fall-through
  286. * src and dst are aligned; need to compute rem
  287. */
  288. .Lboth_aligned\@:
  289. SRL t0, len, LOG_NBYTES+3 # +3 for 8 units/iter
  290. beqz t0, .Lcleanup_both_aligned\@ # len < 8*NBYTES
  291. and rem, len, (8*NBYTES-1) # rem = len % (8*NBYTES)
  292. PREFS( 0, 3*32(src) )
  293. PREFD( 1, 3*32(dst) )
  294. .align 4
  295. 1:
  296. R10KCBARRIER(0(ra))
  297. LOAD(t0, UNIT(0)(src), .Ll_exc\@)
  298. LOAD(t1, UNIT(1)(src), .Ll_exc_copy\@)
  299. LOAD(t2, UNIT(2)(src), .Ll_exc_copy\@)
  300. LOAD(t3, UNIT(3)(src), .Ll_exc_copy\@)
  301. SUB len, len, 8*NBYTES
  302. LOAD(t4, UNIT(4)(src), .Ll_exc_copy\@)
  303. LOAD(t7, UNIT(5)(src), .Ll_exc_copy\@)
  304. STORE(t0, UNIT(0)(dst), .Ls_exc_p8u\@)
  305. STORE(t1, UNIT(1)(dst), .Ls_exc_p7u\@)
  306. LOAD(t0, UNIT(6)(src), .Ll_exc_copy\@)
  307. LOAD(t1, UNIT(7)(src), .Ll_exc_copy\@)
  308. ADD src, src, 8*NBYTES
  309. ADD dst, dst, 8*NBYTES
  310. STORE(t2, UNIT(-6)(dst), .Ls_exc_p6u\@)
  311. STORE(t3, UNIT(-5)(dst), .Ls_exc_p5u\@)
  312. STORE(t4, UNIT(-4)(dst), .Ls_exc_p4u\@)
  313. STORE(t7, UNIT(-3)(dst), .Ls_exc_p3u\@)
  314. STORE(t0, UNIT(-2)(dst), .Ls_exc_p2u\@)
  315. STORE(t1, UNIT(-1)(dst), .Ls_exc_p1u\@)
  316. PREFS( 0, 8*32(src) )
  317. PREFD( 1, 8*32(dst) )
  318. bne len, rem, 1b
  319. nop
  320. /*
  321. * len == rem == the number of bytes left to copy < 8*NBYTES
  322. */
  323. .Lcleanup_both_aligned\@:
  324. beqz len, .Ldone\@
  325. sltu t0, len, 4*NBYTES
  326. bnez t0, .Lless_than_4units\@
  327. and rem, len, (NBYTES-1) # rem = len % NBYTES
  328. /*
  329. * len >= 4*NBYTES
  330. */
  331. LOAD( t0, UNIT(0)(src), .Ll_exc\@)
  332. LOAD( t1, UNIT(1)(src), .Ll_exc_copy\@)
  333. LOAD( t2, UNIT(2)(src), .Ll_exc_copy\@)
  334. LOAD( t3, UNIT(3)(src), .Ll_exc_copy\@)
  335. SUB len, len, 4*NBYTES
  336. ADD src, src, 4*NBYTES
  337. R10KCBARRIER(0(ra))
  338. STORE(t0, UNIT(0)(dst), .Ls_exc_p4u\@)
  339. STORE(t1, UNIT(1)(dst), .Ls_exc_p3u\@)
  340. STORE(t2, UNIT(2)(dst), .Ls_exc_p2u\@)
  341. STORE(t3, UNIT(3)(dst), .Ls_exc_p1u\@)
  342. .set reorder /* DADDI_WAR */
  343. ADD dst, dst, 4*NBYTES
  344. beqz len, .Ldone\@
  345. .set noreorder
  346. .Lless_than_4units\@:
  347. /*
  348. * rem = len % NBYTES
  349. */
  350. beq rem, len, .Lcopy_bytes\@
  351. nop
  352. 1:
  353. R10KCBARRIER(0(ra))
  354. LOAD(t0, 0(src), .Ll_exc\@)
  355. ADD src, src, NBYTES
  356. SUB len, len, NBYTES
  357. STORE(t0, 0(dst), .Ls_exc_p1u\@)
  358. .set reorder /* DADDI_WAR */
  359. ADD dst, dst, NBYTES
  360. bne rem, len, 1b
  361. .set noreorder
  362. #ifndef CONFIG_CPU_NO_LOAD_STORE_LR
  363. /*
  364. * src and dst are aligned, need to copy rem bytes (rem < NBYTES)
  365. * A loop would do only a byte at a time with possible branch
  366. * mispredicts. Can't do an explicit LOAD dst,mask,or,STORE
  367. * because can't assume read-access to dst. Instead, use
  368. * STREST dst, which doesn't require read access to dst.
  369. *
  370. * This code should perform better than a simple loop on modern,
  371. * wide-issue mips processors because the code has fewer branches and
  372. * more instruction-level parallelism.
  373. */
  374. #define bits t2
  375. beqz len, .Ldone\@
  376. ADD t1, dst, len # t1 is just past last byte of dst
  377. li bits, 8*NBYTES
  378. SLL rem, len, 3 # rem = number of bits to keep
  379. LOAD(t0, 0(src), .Ll_exc\@)
  380. SUB bits, bits, rem # bits = number of bits to discard
  381. SHIFT_DISCARD t0, t0, bits
  382. STREST(t0, -1(t1), .Ls_exc\@)
  383. jr ra
  384. move len, zero
  385. .Ldst_unaligned\@:
  386. /*
  387. * dst is unaligned
  388. * t0 = src & ADDRMASK
  389. * t1 = dst & ADDRMASK; T1 > 0
  390. * len >= NBYTES
  391. *
  392. * Copy enough bytes to align dst
  393. * Set match = (src and dst have same alignment)
  394. */
  395. #define match rem
  396. LDFIRST(t3, FIRST(0)(src), .Ll_exc\@)
  397. ADD t2, zero, NBYTES
  398. LDREST(t3, REST(0)(src), .Ll_exc_copy\@)
  399. SUB t2, t2, t1 # t2 = number of bytes copied
  400. xor match, t0, t1
  401. R10KCBARRIER(0(ra))
  402. STFIRST(t3, FIRST(0)(dst), .Ls_exc\@)
  403. beq len, t2, .Ldone\@
  404. SUB len, len, t2
  405. ADD dst, dst, t2
  406. beqz match, .Lboth_aligned\@
  407. ADD src, src, t2
  408. .Lsrc_unaligned_dst_aligned\@:
  409. SRL t0, len, LOG_NBYTES+2 # +2 for 4 units/iter
  410. PREFS( 0, 3*32(src) )
  411. beqz t0, .Lcleanup_src_unaligned\@
  412. and rem, len, (4*NBYTES-1) # rem = len % 4*NBYTES
  413. PREFD( 1, 3*32(dst) )
  414. 1:
  415. /*
  416. * Avoid consecutive LD*'s to the same register since some mips
  417. * implementations can't issue them in the same cycle.
  418. * It's OK to load FIRST(N+1) before REST(N) because the two addresses
  419. * are to the same unit (unless src is aligned, but it's not).
  420. */
  421. R10KCBARRIER(0(ra))
  422. LDFIRST(t0, FIRST(0)(src), .Ll_exc\@)
  423. LDFIRST(t1, FIRST(1)(src), .Ll_exc_copy\@)
  424. SUB len, len, 4*NBYTES
  425. LDREST(t0, REST(0)(src), .Ll_exc_copy\@)
  426. LDREST(t1, REST(1)(src), .Ll_exc_copy\@)
  427. LDFIRST(t2, FIRST(2)(src), .Ll_exc_copy\@)
  428. LDFIRST(t3, FIRST(3)(src), .Ll_exc_copy\@)
  429. LDREST(t2, REST(2)(src), .Ll_exc_copy\@)
  430. LDREST(t3, REST(3)(src), .Ll_exc_copy\@)
  431. PREFS( 0, 9*32(src) ) # 0 is PREF_LOAD (not streamed)
  432. ADD src, src, 4*NBYTES
  433. #ifdef CONFIG_CPU_SB1
  434. nop # improves slotting
  435. #endif
  436. STORE(t0, UNIT(0)(dst), .Ls_exc_p4u\@)
  437. STORE(t1, UNIT(1)(dst), .Ls_exc_p3u\@)
  438. STORE(t2, UNIT(2)(dst), .Ls_exc_p2u\@)
  439. STORE(t3, UNIT(3)(dst), .Ls_exc_p1u\@)
  440. PREFD( 1, 9*32(dst) ) # 1 is PREF_STORE (not streamed)
  441. .set reorder /* DADDI_WAR */
  442. ADD dst, dst, 4*NBYTES
  443. bne len, rem, 1b
  444. .set noreorder
  445. .Lcleanup_src_unaligned\@:
  446. beqz len, .Ldone\@
  447. and rem, len, NBYTES-1 # rem = len % NBYTES
  448. beq rem, len, .Lcopy_bytes\@
  449. nop
  450. 1:
  451. R10KCBARRIER(0(ra))
  452. LDFIRST(t0, FIRST(0)(src), .Ll_exc\@)
  453. LDREST(t0, REST(0)(src), .Ll_exc_copy\@)
  454. ADD src, src, NBYTES
  455. SUB len, len, NBYTES
  456. STORE(t0, 0(dst), .Ls_exc_p1u\@)
  457. .set reorder /* DADDI_WAR */
  458. ADD dst, dst, NBYTES
  459. bne len, rem, 1b
  460. .set noreorder
  461. #endif /* !CONFIG_CPU_NO_LOAD_STORE_LR */
  462. .Lcopy_bytes_checklen\@:
  463. beqz len, .Ldone\@
  464. nop
  465. .Lcopy_bytes\@:
  466. /* 0 < len < NBYTES */
  467. R10KCBARRIER(0(ra))
  468. #define COPY_BYTE(N) \
  469. LOADB(t0, N(src), .Ll_exc\@); \
  470. SUB len, len, 1; \
  471. beqz len, .Ldone\@; \
  472. STOREB(t0, N(dst), .Ls_exc_p1\@)
  473. COPY_BYTE(0)
  474. COPY_BYTE(1)
  475. #ifdef USE_DOUBLE
  476. COPY_BYTE(2)
  477. COPY_BYTE(3)
  478. COPY_BYTE(4)
  479. COPY_BYTE(5)
  480. #endif
  481. LOADB(t0, NBYTES-2(src), .Ll_exc\@)
  482. SUB len, len, 1
  483. jr ra
  484. STOREB(t0, NBYTES-2(dst), .Ls_exc_p1\@)
  485. .Ldone\@:
  486. jr ra
  487. nop
  488. #ifdef CONFIG_CPU_NO_LOAD_STORE_LR
  489. .Lcopy_unaligned_bytes\@:
  490. 1:
  491. COPY_BYTE(0)
  492. COPY_BYTE(1)
  493. COPY_BYTE(2)
  494. COPY_BYTE(3)
  495. COPY_BYTE(4)
  496. COPY_BYTE(5)
  497. COPY_BYTE(6)
  498. COPY_BYTE(7)
  499. ADD src, src, 8
  500. b 1b
  501. ADD dst, dst, 8
  502. #endif /* CONFIG_CPU_NO_LOAD_STORE_LR */
  503. .if __memcpy == 1
  504. END(memcpy)
  505. .set __memcpy, 0
  506. .hidden __memcpy
  507. .endif
  508. .Ll_exc_copy\@:
  509. /*
  510. * Copy bytes from src until faulting load address (or until a
  511. * lb faults)
  512. *
  513. * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
  514. * may be more than a byte beyond the last address.
  515. * Hence, the lb below may get an exception.
  516. *
  517. * Assumes src < THREAD_BUADDR($28)
  518. */
  519. LOADK t0, TI_TASK($28)
  520. nop
  521. LOADK t0, THREAD_BUADDR(t0)
  522. 1:
  523. LOADB(t1, 0(src), .Ll_exc\@)
  524. ADD src, src, 1
  525. sb t1, 0(dst) # can't fault -- we're copy_from_user
  526. .set reorder /* DADDI_WAR */
  527. ADD dst, dst, 1
  528. bne src, t0, 1b
  529. .set noreorder
  530. .Ll_exc\@:
  531. LOADK t0, TI_TASK($28)
  532. nop
  533. LOADK t0, THREAD_BUADDR(t0) # t0 is just past last good address
  534. nop
  535. SUB len, AT, t0 # len number of uncopied bytes
  536. jr ra
  537. nop
  538. #define SEXC(n) \
  539. .set reorder; /* DADDI_WAR */ \
  540. .Ls_exc_p ## n ## u\@: \
  541. ADD len, len, n*NBYTES; \
  542. jr ra; \
  543. .set noreorder
  544. SEXC(8)
  545. SEXC(7)
  546. SEXC(6)
  547. SEXC(5)
  548. SEXC(4)
  549. SEXC(3)
  550. SEXC(2)
  551. SEXC(1)
  552. .Ls_exc_p1\@:
  553. .set reorder /* DADDI_WAR */
  554. ADD len, len, 1
  555. jr ra
  556. .set noreorder
  557. .Ls_exc\@:
  558. jr ra
  559. nop
  560. .endm
  561. #ifndef CONFIG_HAVE_PLAT_MEMCPY
  562. .align 5
  563. LEAF(memmove)
  564. EXPORT_SYMBOL(memmove)
  565. ADD t0, a0, a2
  566. ADD t1, a1, a2
  567. sltu t0, a1, t0 # dst + len <= src -> memcpy
  568. sltu t1, a0, t1 # dst >= src + len -> memcpy
  569. and t0, t1
  570. beqz t0, .L__memcpy
  571. move v0, a0 /* return value */
  572. beqz a2, .Lr_out
  573. END(memmove)
  574. /* fall through to __rmemcpy */
  575. LEAF(__rmemcpy) /* a0=dst a1=src a2=len */
  576. sltu t0, a1, a0
  577. beqz t0, .Lr_end_bytes_up # src >= dst
  578. nop
  579. ADD a0, a2 # dst = dst + len
  580. ADD a1, a2 # src = src + len
  581. .Lr_end_bytes:
  582. R10KCBARRIER(0(ra))
  583. lb t0, -1(a1)
  584. SUB a2, a2, 0x1
  585. sb t0, -1(a0)
  586. SUB a1, a1, 0x1
  587. .set reorder /* DADDI_WAR */
  588. SUB a0, a0, 0x1
  589. bnez a2, .Lr_end_bytes
  590. .set noreorder
  591. .Lr_out:
  592. jr ra
  593. move a2, zero
  594. .Lr_end_bytes_up:
  595. R10KCBARRIER(0(ra))
  596. lb t0, (a1)
  597. SUB a2, a2, 0x1
  598. sb t0, (a0)
  599. ADD a1, a1, 0x1
  600. .set reorder /* DADDI_WAR */
  601. ADD a0, a0, 0x1
  602. bnez a2, .Lr_end_bytes_up
  603. .set noreorder
  604. jr ra
  605. move a2, zero
  606. END(__rmemcpy)
  607. /*
  608. * A combined memcpy/__copy_user
  609. * __copy_user sets len to 0 for success; else to an upper bound of
  610. * the number of uncopied bytes.
  611. * memcpy sets v0 to dst.
  612. */
  613. .align 5
  614. LEAF(memcpy) /* a0=dst a1=src a2=len */
  615. EXPORT_SYMBOL(memcpy)
  616. move v0, dst /* return value */
  617. .L__memcpy:
  618. #ifndef CONFIG_EVA
  619. FEXPORT(__raw_copy_from_user)
  620. EXPORT_SYMBOL(__raw_copy_from_user)
  621. FEXPORT(__raw_copy_to_user)
  622. EXPORT_SYMBOL(__raw_copy_to_user)
  623. #endif
  624. /* Legacy Mode, user <-> user */
  625. __BUILD_COPY_USER LEGACY_MODE USEROP USEROP
  626. #endif
  627. #ifdef CONFIG_EVA
  628. /*
  629. * For EVA we need distinct symbols for reading and writing to user space.
  630. * This is because we need to use specific EVA instructions to perform the
  631. * virtual <-> physical translation when a virtual address is actually in user
  632. * space
  633. */
  634. /*
  635. * __copy_from_user (EVA)
  636. */
  637. LEAF(__raw_copy_from_user)
  638. EXPORT_SYMBOL(__raw_copy_from_user)
  639. __BUILD_COPY_USER EVA_MODE USEROP KERNELOP
  640. END(__raw_copy_from_user)
  641. /*
  642. * __copy_to_user (EVA)
  643. */
  644. LEAF(__raw_copy_to_user)
  645. EXPORT_SYMBOL(__raw_copy_to_user)
  646. __BUILD_COPY_USER EVA_MODE KERNELOP USEROP
  647. END(__raw_copy_to_user)
  648. #endif