csum_partial.S 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. * Quick'n'dirty IP checksum ...
  7. *
  8. * Copyright (C) 1998, 1999 Ralf Baechle
  9. * Copyright (C) 1999 Silicon Graphics, Inc.
  10. * Copyright (C) 2007 Maciej W. Rozycki
  11. */
  12. #include <linux/errno.h>
  13. #include <asm/asm.h>
  14. #include <asm/asm-offsets.h>
  15. #include <asm/regdef.h>
  16. #ifdef CONFIG_64BIT
  17. /*
  18. * As we are sharing code base with the mips32 tree (which use the o32 ABI
  19. * register definitions). We need to redefine the register definitions from
  20. * the n64 ABI register naming to the o32 ABI register naming.
  21. */
  22. #undef t0
  23. #undef t1
  24. #undef t2
  25. #undef t3
  26. #define t0 $8
  27. #define t1 $9
  28. #define t2 $10
  29. #define t3 $11
  30. #define t4 $12
  31. #define t5 $13
  32. #define t6 $14
  33. #define t7 $15
  34. #define USE_DOUBLE
  35. #endif
  36. #ifdef USE_DOUBLE
  37. #define LOAD ld
  38. #define LOAD32 lwu
  39. #define ADD daddu
  40. #define NBYTES 8
  41. #else
  42. #define LOAD lw
  43. #define LOAD32 lw
  44. #define ADD addu
  45. #define NBYTES 4
  46. #endif /* USE_DOUBLE */
  47. #define UNIT(unit) ((unit)*NBYTES)
  48. #define ADDC(sum,reg) \
  49. ADD sum, reg; \
  50. sltu v1, sum, reg; \
  51. ADD sum, v1; \
  52. #define ADDC32(sum,reg) \
  53. addu sum, reg; \
  54. sltu v1, sum, reg; \
  55. addu sum, v1; \
  56. #define CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3) \
  57. LOAD _t0, (offset + UNIT(0))(src); \
  58. LOAD _t1, (offset + UNIT(1))(src); \
  59. LOAD _t2, (offset + UNIT(2))(src); \
  60. LOAD _t3, (offset + UNIT(3))(src); \
  61. ADDC(sum, _t0); \
  62. ADDC(sum, _t1); \
  63. ADDC(sum, _t2); \
  64. ADDC(sum, _t3)
  65. #ifdef USE_DOUBLE
  66. #define CSUM_BIGCHUNK(src, offset, sum, _t0, _t1, _t2, _t3) \
  67. CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3)
  68. #else
  69. #define CSUM_BIGCHUNK(src, offset, sum, _t0, _t1, _t2, _t3) \
  70. CSUM_BIGCHUNK1(src, offset, sum, _t0, _t1, _t2, _t3); \
  71. CSUM_BIGCHUNK1(src, offset + 0x10, sum, _t0, _t1, _t2, _t3)
  72. #endif
  73. /*
  74. * a0: source address
  75. * a1: length of the area to checksum
  76. * a2: partial checksum
  77. */
  78. #define src a0
  79. #define sum v0
  80. .text
  81. .set noreorder
  82. .align 5
  83. LEAF(csum_partial)
  84. move sum, zero
  85. move t7, zero
  86. sltiu t8, a1, 0x8
  87. bnez t8, .Lsmall_csumcpy /* < 8 bytes to copy */
  88. move t2, a1
  89. andi t7, src, 0x1 /* odd buffer? */
  90. .Lhword_align:
  91. beqz t7, .Lword_align
  92. andi t8, src, 0x2
  93. lbu t0, (src)
  94. LONG_SUBU a1, a1, 0x1
  95. #ifdef __MIPSEL__
  96. sll t0, t0, 8
  97. #endif
  98. ADDC(sum, t0)
  99. PTR_ADDU src, src, 0x1
  100. andi t8, src, 0x2
  101. .Lword_align:
  102. beqz t8, .Ldword_align
  103. sltiu t8, a1, 56
  104. lhu t0, (src)
  105. LONG_SUBU a1, a1, 0x2
  106. ADDC(sum, t0)
  107. sltiu t8, a1, 56
  108. PTR_ADDU src, src, 0x2
  109. .Ldword_align:
  110. bnez t8, .Ldo_end_words
  111. move t8, a1
  112. andi t8, src, 0x4
  113. beqz t8, .Lqword_align
  114. andi t8, src, 0x8
  115. LOAD32 t0, 0x00(src)
  116. LONG_SUBU a1, a1, 0x4
  117. ADDC(sum, t0)
  118. PTR_ADDU src, src, 0x4
  119. andi t8, src, 0x8
  120. .Lqword_align:
  121. beqz t8, .Loword_align
  122. andi t8, src, 0x10
  123. #ifdef USE_DOUBLE
  124. ld t0, 0x00(src)
  125. LONG_SUBU a1, a1, 0x8
  126. ADDC(sum, t0)
  127. #else
  128. lw t0, 0x00(src)
  129. lw t1, 0x04(src)
  130. LONG_SUBU a1, a1, 0x8
  131. ADDC(sum, t0)
  132. ADDC(sum, t1)
  133. #endif
  134. PTR_ADDU src, src, 0x8
  135. andi t8, src, 0x10
  136. .Loword_align:
  137. beqz t8, .Lbegin_movement
  138. LONG_SRL t8, a1, 0x7
  139. #ifdef USE_DOUBLE
  140. ld t0, 0x00(src)
  141. ld t1, 0x08(src)
  142. ADDC(sum, t0)
  143. ADDC(sum, t1)
  144. #else
  145. CSUM_BIGCHUNK1(src, 0x00, sum, t0, t1, t3, t4)
  146. #endif
  147. LONG_SUBU a1, a1, 0x10
  148. PTR_ADDU src, src, 0x10
  149. LONG_SRL t8, a1, 0x7
  150. .Lbegin_movement:
  151. beqz t8, 1f
  152. andi t2, a1, 0x40
  153. .Lmove_128bytes:
  154. CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
  155. CSUM_BIGCHUNK(src, 0x20, sum, t0, t1, t3, t4)
  156. CSUM_BIGCHUNK(src, 0x40, sum, t0, t1, t3, t4)
  157. CSUM_BIGCHUNK(src, 0x60, sum, t0, t1, t3, t4)
  158. LONG_SUBU t8, t8, 0x01
  159. .set reorder /* DADDI_WAR */
  160. PTR_ADDU src, src, 0x80
  161. bnez t8, .Lmove_128bytes
  162. .set noreorder
  163. 1:
  164. beqz t2, 1f
  165. andi t2, a1, 0x20
  166. .Lmove_64bytes:
  167. CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
  168. CSUM_BIGCHUNK(src, 0x20, sum, t0, t1, t3, t4)
  169. PTR_ADDU src, src, 0x40
  170. 1:
  171. beqz t2, .Ldo_end_words
  172. andi t8, a1, 0x1c
  173. .Lmove_32bytes:
  174. CSUM_BIGCHUNK(src, 0x00, sum, t0, t1, t3, t4)
  175. andi t8, a1, 0x1c
  176. PTR_ADDU src, src, 0x20
  177. .Ldo_end_words:
  178. beqz t8, .Lsmall_csumcpy
  179. andi t2, a1, 0x3
  180. LONG_SRL t8, t8, 0x2
  181. .Lend_words:
  182. LOAD32 t0, (src)
  183. LONG_SUBU t8, t8, 0x1
  184. ADDC(sum, t0)
  185. .set reorder /* DADDI_WAR */
  186. PTR_ADDU src, src, 0x4
  187. bnez t8, .Lend_words
  188. .set noreorder
  189. /* unknown src alignment and < 8 bytes to go */
  190. .Lsmall_csumcpy:
  191. move a1, t2
  192. andi t0, a1, 4
  193. beqz t0, 1f
  194. andi t0, a1, 2
  195. /* Still a full word to go */
  196. ulw t1, (src)
  197. PTR_ADDIU src, 4
  198. #ifdef USE_DOUBLE
  199. dsll t1, t1, 32 /* clear lower 32bit */
  200. #endif
  201. ADDC(sum, t1)
  202. 1: move t1, zero
  203. beqz t0, 1f
  204. andi t0, a1, 1
  205. /* Still a halfword to go */
  206. ulhu t1, (src)
  207. PTR_ADDIU src, 2
  208. 1: beqz t0, 1f
  209. sll t1, t1, 16
  210. lbu t2, (src)
  211. nop
  212. #ifdef __MIPSEB__
  213. sll t2, t2, 8
  214. #endif
  215. or t1, t2
  216. 1: ADDC(sum, t1)
  217. /* fold checksum */
  218. #ifdef USE_DOUBLE
  219. dsll32 v1, sum, 0
  220. daddu sum, v1
  221. sltu v1, sum, v1
  222. dsra32 sum, sum, 0
  223. addu sum, v1
  224. #endif
  225. /* odd buffer alignment? */
  226. #ifdef CPU_MIPSR2
  227. wsbh v1, sum
  228. movn sum, v1, t7
  229. #else
  230. beqz t7, 1f /* odd buffer alignment? */
  231. lui v1, 0x00ff
  232. addu v1, 0x00ff
  233. and t0, sum, v1
  234. sll t0, t0, 8
  235. srl sum, sum, 8
  236. and sum, sum, v1
  237. or sum, sum, t0
  238. 1:
  239. #endif
  240. .set reorder
  241. /* Add the passed partial csum. */
  242. ADDC32(sum, a2)
  243. jr ra
  244. .set noreorder
  245. END(csum_partial)
  246. /*
  247. * checksum and copy routines based on memcpy.S
  248. *
  249. * csum_partial_copy_nocheck(src, dst, len, sum)
  250. * __csum_partial_copy_user(src, dst, len, sum, errp)
  251. *
  252. * See "Spec" in memcpy.S for details. Unlike __copy_user, all
  253. * function in this file use the standard calling convention.
  254. */
  255. #define src a0
  256. #define dst a1
  257. #define len a2
  258. #define psum a3
  259. #define sum v0
  260. #define odd t8
  261. #define errptr t9
  262. /*
  263. * The exception handler for loads requires that:
  264. * 1- AT contain the address of the byte just past the end of the source
  265. * of the copy,
  266. * 2- src_entry <= src < AT, and
  267. * 3- (dst - src) == (dst_entry - src_entry),
  268. * The _entry suffix denotes values when __copy_user was called.
  269. *
  270. * (1) is set up up by __csum_partial_copy_from_user and maintained by
  271. * not writing AT in __csum_partial_copy
  272. * (2) is met by incrementing src by the number of bytes copied
  273. * (3) is met by not doing loads between a pair of increments of dst and src
  274. *
  275. * The exception handlers for stores stores -EFAULT to errptr and return.
  276. * These handlers do not need to overwrite any data.
  277. */
  278. #define EXC(inst_reg,addr,handler) \
  279. 9: inst_reg, addr; \
  280. .section __ex_table,"a"; \
  281. PTR 9b, handler; \
  282. .previous
  283. #ifdef USE_DOUBLE
  284. #define LOAD ld
  285. #define LOADL ldl
  286. #define LOADR ldr
  287. #define STOREL sdl
  288. #define STORER sdr
  289. #define STORE sd
  290. #define ADD daddu
  291. #define SUB dsubu
  292. #define SRL dsrl
  293. #define SLL dsll
  294. #define SLLV dsllv
  295. #define SRLV dsrlv
  296. #define NBYTES 8
  297. #define LOG_NBYTES 3
  298. #else
  299. #define LOAD lw
  300. #define LOADL lwl
  301. #define LOADR lwr
  302. #define STOREL swl
  303. #define STORER swr
  304. #define STORE sw
  305. #define ADD addu
  306. #define SUB subu
  307. #define SRL srl
  308. #define SLL sll
  309. #define SLLV sllv
  310. #define SRLV srlv
  311. #define NBYTES 4
  312. #define LOG_NBYTES 2
  313. #endif /* USE_DOUBLE */
  314. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  315. #define LDFIRST LOADR
  316. #define LDREST LOADL
  317. #define STFIRST STORER
  318. #define STREST STOREL
  319. #define SHIFT_DISCARD SLLV
  320. #define SHIFT_DISCARD_REVERT SRLV
  321. #else
  322. #define LDFIRST LOADL
  323. #define LDREST LOADR
  324. #define STFIRST STOREL
  325. #define STREST STORER
  326. #define SHIFT_DISCARD SRLV
  327. #define SHIFT_DISCARD_REVERT SLLV
  328. #endif
  329. #define FIRST(unit) ((unit)*NBYTES)
  330. #define REST(unit) (FIRST(unit)+NBYTES-1)
  331. #define ADDRMASK (NBYTES-1)
  332. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  333. .set noat
  334. #else
  335. .set at=v1
  336. #endif
  337. LEAF(__csum_partial_copy_user)
  338. PTR_ADDU AT, src, len /* See (1) above. */
  339. #ifdef CONFIG_64BIT
  340. move errptr, a4
  341. #else
  342. lw errptr, 16(sp)
  343. #endif
  344. FEXPORT(csum_partial_copy_nocheck)
  345. move sum, zero
  346. move odd, zero
  347. /*
  348. * Note: dst & src may be unaligned, len may be 0
  349. * Temps
  350. */
  351. /*
  352. * The "issue break"s below are very approximate.
  353. * Issue delays for dcache fills will perturb the schedule, as will
  354. * load queue full replay traps, etc.
  355. *
  356. * If len < NBYTES use byte operations.
  357. */
  358. sltu t2, len, NBYTES
  359. and t1, dst, ADDRMASK
  360. bnez t2, .Lcopy_bytes_checklen
  361. and t0, src, ADDRMASK
  362. andi odd, dst, 0x1 /* odd buffer? */
  363. bnez t1, .Ldst_unaligned
  364. nop
  365. bnez t0, .Lsrc_unaligned_dst_aligned
  366. /*
  367. * use delay slot for fall-through
  368. * src and dst are aligned; need to compute rem
  369. */
  370. .Lboth_aligned:
  371. SRL t0, len, LOG_NBYTES+3 # +3 for 8 units/iter
  372. beqz t0, .Lcleanup_both_aligned # len < 8*NBYTES
  373. nop
  374. SUB len, 8*NBYTES # subtract here for bgez loop
  375. .align 4
  376. 1:
  377. EXC( LOAD t0, UNIT(0)(src), .Ll_exc)
  378. EXC( LOAD t1, UNIT(1)(src), .Ll_exc_copy)
  379. EXC( LOAD t2, UNIT(2)(src), .Ll_exc_copy)
  380. EXC( LOAD t3, UNIT(3)(src), .Ll_exc_copy)
  381. EXC( LOAD t4, UNIT(4)(src), .Ll_exc_copy)
  382. EXC( LOAD t5, UNIT(5)(src), .Ll_exc_copy)
  383. EXC( LOAD t6, UNIT(6)(src), .Ll_exc_copy)
  384. EXC( LOAD t7, UNIT(7)(src), .Ll_exc_copy)
  385. SUB len, len, 8*NBYTES
  386. ADD src, src, 8*NBYTES
  387. EXC( STORE t0, UNIT(0)(dst), .Ls_exc)
  388. ADDC(sum, t0)
  389. EXC( STORE t1, UNIT(1)(dst), .Ls_exc)
  390. ADDC(sum, t1)
  391. EXC( STORE t2, UNIT(2)(dst), .Ls_exc)
  392. ADDC(sum, t2)
  393. EXC( STORE t3, UNIT(3)(dst), .Ls_exc)
  394. ADDC(sum, t3)
  395. EXC( STORE t4, UNIT(4)(dst), .Ls_exc)
  396. ADDC(sum, t4)
  397. EXC( STORE t5, UNIT(5)(dst), .Ls_exc)
  398. ADDC(sum, t5)
  399. EXC( STORE t6, UNIT(6)(dst), .Ls_exc)
  400. ADDC(sum, t6)
  401. EXC( STORE t7, UNIT(7)(dst), .Ls_exc)
  402. ADDC(sum, t7)
  403. .set reorder /* DADDI_WAR */
  404. ADD dst, dst, 8*NBYTES
  405. bgez len, 1b
  406. .set noreorder
  407. ADD len, 8*NBYTES # revert len (see above)
  408. /*
  409. * len == the number of bytes left to copy < 8*NBYTES
  410. */
  411. .Lcleanup_both_aligned:
  412. #define rem t7
  413. beqz len, .Ldone
  414. sltu t0, len, 4*NBYTES
  415. bnez t0, .Lless_than_4units
  416. and rem, len, (NBYTES-1) # rem = len % NBYTES
  417. /*
  418. * len >= 4*NBYTES
  419. */
  420. EXC( LOAD t0, UNIT(0)(src), .Ll_exc)
  421. EXC( LOAD t1, UNIT(1)(src), .Ll_exc_copy)
  422. EXC( LOAD t2, UNIT(2)(src), .Ll_exc_copy)
  423. EXC( LOAD t3, UNIT(3)(src), .Ll_exc_copy)
  424. SUB len, len, 4*NBYTES
  425. ADD src, src, 4*NBYTES
  426. EXC( STORE t0, UNIT(0)(dst), .Ls_exc)
  427. ADDC(sum, t0)
  428. EXC( STORE t1, UNIT(1)(dst), .Ls_exc)
  429. ADDC(sum, t1)
  430. EXC( STORE t2, UNIT(2)(dst), .Ls_exc)
  431. ADDC(sum, t2)
  432. EXC( STORE t3, UNIT(3)(dst), .Ls_exc)
  433. ADDC(sum, t3)
  434. .set reorder /* DADDI_WAR */
  435. ADD dst, dst, 4*NBYTES
  436. beqz len, .Ldone
  437. .set noreorder
  438. .Lless_than_4units:
  439. /*
  440. * rem = len % NBYTES
  441. */
  442. beq rem, len, .Lcopy_bytes
  443. nop
  444. 1:
  445. EXC( LOAD t0, 0(src), .Ll_exc)
  446. ADD src, src, NBYTES
  447. SUB len, len, NBYTES
  448. EXC( STORE t0, 0(dst), .Ls_exc)
  449. ADDC(sum, t0)
  450. .set reorder /* DADDI_WAR */
  451. ADD dst, dst, NBYTES
  452. bne rem, len, 1b
  453. .set noreorder
  454. /*
  455. * src and dst are aligned, need to copy rem bytes (rem < NBYTES)
  456. * A loop would do only a byte at a time with possible branch
  457. * mispredicts. Can't do an explicit LOAD dst,mask,or,STORE
  458. * because can't assume read-access to dst. Instead, use
  459. * STREST dst, which doesn't require read access to dst.
  460. *
  461. * This code should perform better than a simple loop on modern,
  462. * wide-issue mips processors because the code has fewer branches and
  463. * more instruction-level parallelism.
  464. */
  465. #define bits t2
  466. beqz len, .Ldone
  467. ADD t1, dst, len # t1 is just past last byte of dst
  468. li bits, 8*NBYTES
  469. SLL rem, len, 3 # rem = number of bits to keep
  470. EXC( LOAD t0, 0(src), .Ll_exc)
  471. SUB bits, bits, rem # bits = number of bits to discard
  472. SHIFT_DISCARD t0, t0, bits
  473. EXC( STREST t0, -1(t1), .Ls_exc)
  474. SHIFT_DISCARD_REVERT t0, t0, bits
  475. .set reorder
  476. ADDC(sum, t0)
  477. b .Ldone
  478. .set noreorder
  479. .Ldst_unaligned:
  480. /*
  481. * dst is unaligned
  482. * t0 = src & ADDRMASK
  483. * t1 = dst & ADDRMASK; T1 > 0
  484. * len >= NBYTES
  485. *
  486. * Copy enough bytes to align dst
  487. * Set match = (src and dst have same alignment)
  488. */
  489. #define match rem
  490. EXC( LDFIRST t3, FIRST(0)(src), .Ll_exc)
  491. ADD t2, zero, NBYTES
  492. EXC( LDREST t3, REST(0)(src), .Ll_exc_copy)
  493. SUB t2, t2, t1 # t2 = number of bytes copied
  494. xor match, t0, t1
  495. EXC( STFIRST t3, FIRST(0)(dst), .Ls_exc)
  496. SLL t4, t1, 3 # t4 = number of bits to discard
  497. SHIFT_DISCARD t3, t3, t4
  498. /* no SHIFT_DISCARD_REVERT to handle odd buffer properly */
  499. ADDC(sum, t3)
  500. beq len, t2, .Ldone
  501. SUB len, len, t2
  502. ADD dst, dst, t2
  503. beqz match, .Lboth_aligned
  504. ADD src, src, t2
  505. .Lsrc_unaligned_dst_aligned:
  506. SRL t0, len, LOG_NBYTES+2 # +2 for 4 units/iter
  507. beqz t0, .Lcleanup_src_unaligned
  508. and rem, len, (4*NBYTES-1) # rem = len % 4*NBYTES
  509. 1:
  510. /*
  511. * Avoid consecutive LD*'s to the same register since some mips
  512. * implementations can't issue them in the same cycle.
  513. * It's OK to load FIRST(N+1) before REST(N) because the two addresses
  514. * are to the same unit (unless src is aligned, but it's not).
  515. */
  516. EXC( LDFIRST t0, FIRST(0)(src), .Ll_exc)
  517. EXC( LDFIRST t1, FIRST(1)(src), .Ll_exc_copy)
  518. SUB len, len, 4*NBYTES
  519. EXC( LDREST t0, REST(0)(src), .Ll_exc_copy)
  520. EXC( LDREST t1, REST(1)(src), .Ll_exc_copy)
  521. EXC( LDFIRST t2, FIRST(2)(src), .Ll_exc_copy)
  522. EXC( LDFIRST t3, FIRST(3)(src), .Ll_exc_copy)
  523. EXC( LDREST t2, REST(2)(src), .Ll_exc_copy)
  524. EXC( LDREST t3, REST(3)(src), .Ll_exc_copy)
  525. ADD src, src, 4*NBYTES
  526. #ifdef CONFIG_CPU_SB1
  527. nop # improves slotting
  528. #endif
  529. EXC( STORE t0, UNIT(0)(dst), .Ls_exc)
  530. ADDC(sum, t0)
  531. EXC( STORE t1, UNIT(1)(dst), .Ls_exc)
  532. ADDC(sum, t1)
  533. EXC( STORE t2, UNIT(2)(dst), .Ls_exc)
  534. ADDC(sum, t2)
  535. EXC( STORE t3, UNIT(3)(dst), .Ls_exc)
  536. ADDC(sum, t3)
  537. .set reorder /* DADDI_WAR */
  538. ADD dst, dst, 4*NBYTES
  539. bne len, rem, 1b
  540. .set noreorder
  541. .Lcleanup_src_unaligned:
  542. beqz len, .Ldone
  543. and rem, len, NBYTES-1 # rem = len % NBYTES
  544. beq rem, len, .Lcopy_bytes
  545. nop
  546. 1:
  547. EXC( LDFIRST t0, FIRST(0)(src), .Ll_exc)
  548. EXC( LDREST t0, REST(0)(src), .Ll_exc_copy)
  549. ADD src, src, NBYTES
  550. SUB len, len, NBYTES
  551. EXC( STORE t0, 0(dst), .Ls_exc)
  552. ADDC(sum, t0)
  553. .set reorder /* DADDI_WAR */
  554. ADD dst, dst, NBYTES
  555. bne len, rem, 1b
  556. .set noreorder
  557. .Lcopy_bytes_checklen:
  558. beqz len, .Ldone
  559. nop
  560. .Lcopy_bytes:
  561. /* 0 < len < NBYTES */
  562. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  563. #define SHIFT_START 0
  564. #define SHIFT_INC 8
  565. #else
  566. #define SHIFT_START 8*(NBYTES-1)
  567. #define SHIFT_INC -8
  568. #endif
  569. move t2, zero # partial word
  570. li t3, SHIFT_START # shift
  571. /* use .Ll_exc_copy here to return correct sum on fault */
  572. #define COPY_BYTE(N) \
  573. EXC( lbu t0, N(src), .Ll_exc_copy); \
  574. SUB len, len, 1; \
  575. EXC( sb t0, N(dst), .Ls_exc); \
  576. SLLV t0, t0, t3; \
  577. addu t3, SHIFT_INC; \
  578. beqz len, .Lcopy_bytes_done; \
  579. or t2, t0
  580. COPY_BYTE(0)
  581. COPY_BYTE(1)
  582. #ifdef USE_DOUBLE
  583. COPY_BYTE(2)
  584. COPY_BYTE(3)
  585. COPY_BYTE(4)
  586. COPY_BYTE(5)
  587. #endif
  588. EXC( lbu t0, NBYTES-2(src), .Ll_exc_copy)
  589. SUB len, len, 1
  590. EXC( sb t0, NBYTES-2(dst), .Ls_exc)
  591. SLLV t0, t0, t3
  592. or t2, t0
  593. .Lcopy_bytes_done:
  594. ADDC(sum, t2)
  595. .Ldone:
  596. /* fold checksum */
  597. #ifdef USE_DOUBLE
  598. dsll32 v1, sum, 0
  599. daddu sum, v1
  600. sltu v1, sum, v1
  601. dsra32 sum, sum, 0
  602. addu sum, v1
  603. #endif
  604. #ifdef CPU_MIPSR2
  605. wsbh v1, sum
  606. movn sum, v1, odd
  607. #else
  608. beqz odd, 1f /* odd buffer alignment? */
  609. lui v1, 0x00ff
  610. addu v1, 0x00ff
  611. and t0, sum, v1
  612. sll t0, t0, 8
  613. srl sum, sum, 8
  614. and sum, sum, v1
  615. or sum, sum, t0
  616. 1:
  617. #endif
  618. .set reorder
  619. ADDC32(sum, psum)
  620. jr ra
  621. .set noreorder
  622. .Ll_exc_copy:
  623. /*
  624. * Copy bytes from src until faulting load address (or until a
  625. * lb faults)
  626. *
  627. * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
  628. * may be more than a byte beyond the last address.
  629. * Hence, the lb below may get an exception.
  630. *
  631. * Assumes src < THREAD_BUADDR($28)
  632. */
  633. LOAD t0, TI_TASK($28)
  634. li t2, SHIFT_START
  635. LOAD t0, THREAD_BUADDR(t0)
  636. 1:
  637. EXC( lbu t1, 0(src), .Ll_exc)
  638. ADD src, src, 1
  639. sb t1, 0(dst) # can't fault -- we're copy_from_user
  640. SLLV t1, t1, t2
  641. addu t2, SHIFT_INC
  642. ADDC(sum, t1)
  643. .set reorder /* DADDI_WAR */
  644. ADD dst, dst, 1
  645. bne src, t0, 1b
  646. .set noreorder
  647. .Ll_exc:
  648. LOAD t0, TI_TASK($28)
  649. nop
  650. LOAD t0, THREAD_BUADDR(t0) # t0 is just past last good address
  651. nop
  652. SUB len, AT, t0 # len number of uncopied bytes
  653. /*
  654. * Here's where we rely on src and dst being incremented in tandem,
  655. * See (3) above.
  656. * dst += (fault addr - src) to put dst at first byte to clear
  657. */
  658. ADD dst, t0 # compute start address in a1
  659. SUB dst, src
  660. /*
  661. * Clear len bytes starting at dst. Can't call __bzero because it
  662. * might modify len. An inefficient loop for these rare times...
  663. */
  664. .set reorder /* DADDI_WAR */
  665. SUB src, len, 1
  666. beqz len, .Ldone
  667. .set noreorder
  668. 1: sb zero, 0(dst)
  669. ADD dst, dst, 1
  670. .set push
  671. .set noat
  672. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  673. bnez src, 1b
  674. SUB src, src, 1
  675. #else
  676. li v1, 1
  677. bnez src, 1b
  678. SUB src, src, v1
  679. #endif
  680. li v1, -EFAULT
  681. b .Ldone
  682. sw v1, (errptr)
  683. .Ls_exc:
  684. li v0, -1 /* invalid checksum */
  685. li v1, -EFAULT
  686. jr ra
  687. sw v1, (errptr)
  688. .set pop
  689. END(__csum_partial_copy_user)