csum_partial.S 16 KB

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