csum_partial.S 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. /* odd buffer alignment? */
  234. beqz t7, 1f
  235. nop
  236. sll v1, sum, 8
  237. srl sum, sum, 8
  238. or sum, v1
  239. andi sum, 0xffff
  240. .set pop
  241. 1:
  242. .set reorder
  243. /* Add the passed partial csum. */
  244. ADDC32(sum, a2)
  245. jr ra
  246. .set noreorder
  247. END(csum_partial)
  248. /*
  249. * checksum and copy routines based on memcpy.S
  250. *
  251. * csum_partial_copy_nocheck(src, dst, len, sum)
  252. * __csum_partial_copy_user(src, dst, len, sum, errp)
  253. *
  254. * See "Spec" in memcpy.S for details. Unlike __copy_user, all
  255. * function in this file use the standard calling convention.
  256. */
  257. #define src a0
  258. #define dst a1
  259. #define len a2
  260. #define psum a3
  261. #define sum v0
  262. #define odd t8
  263. #define errptr t9
  264. /*
  265. * The exception handler for loads requires that:
  266. * 1- AT contain the address of the byte just past the end of the source
  267. * of the copy,
  268. * 2- src_entry <= src < AT, and
  269. * 3- (dst - src) == (dst_entry - src_entry),
  270. * The _entry suffix denotes values when __copy_user was called.
  271. *
  272. * (1) is set up up by __csum_partial_copy_from_user and maintained by
  273. * not writing AT in __csum_partial_copy
  274. * (2) is met by incrementing src by the number of bytes copied
  275. * (3) is met by not doing loads between a pair of increments of dst and src
  276. *
  277. * The exception handlers for stores stores -EFAULT to errptr and return.
  278. * These handlers do not need to overwrite any data.
  279. */
  280. #define EXC(inst_reg,addr,handler) \
  281. 9: inst_reg, addr; \
  282. .section __ex_table,"a"; \
  283. PTR 9b, handler; \
  284. .previous
  285. #ifdef USE_DOUBLE
  286. #define LOAD ld
  287. #define LOADL ldl
  288. #define LOADR ldr
  289. #define STOREL sdl
  290. #define STORER sdr
  291. #define STORE sd
  292. #define ADD daddu
  293. #define SUB dsubu
  294. #define SRL dsrl
  295. #define SLL dsll
  296. #define SLLV dsllv
  297. #define SRLV dsrlv
  298. #define NBYTES 8
  299. #define LOG_NBYTES 3
  300. #else
  301. #define LOAD lw
  302. #define LOADL lwl
  303. #define LOADR lwr
  304. #define STOREL swl
  305. #define STORER swr
  306. #define STORE sw
  307. #define ADD addu
  308. #define SUB subu
  309. #define SRL srl
  310. #define SLL sll
  311. #define SLLV sllv
  312. #define SRLV srlv
  313. #define NBYTES 4
  314. #define LOG_NBYTES 2
  315. #endif /* USE_DOUBLE */
  316. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  317. #define LDFIRST LOADR
  318. #define LDREST LOADL
  319. #define STFIRST STORER
  320. #define STREST STOREL
  321. #define SHIFT_DISCARD SLLV
  322. #define SHIFT_DISCARD_REVERT SRLV
  323. #else
  324. #define LDFIRST LOADL
  325. #define LDREST LOADR
  326. #define STFIRST STOREL
  327. #define STREST STORER
  328. #define SHIFT_DISCARD SRLV
  329. #define SHIFT_DISCARD_REVERT SLLV
  330. #endif
  331. #define FIRST(unit) ((unit)*NBYTES)
  332. #define REST(unit) (FIRST(unit)+NBYTES-1)
  333. #define ADDRMASK (NBYTES-1)
  334. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  335. .set noat
  336. #else
  337. .set at=v1
  338. #endif
  339. LEAF(__csum_partial_copy_user)
  340. PTR_ADDU AT, src, len /* See (1) above. */
  341. #ifdef CONFIG_64BIT
  342. move errptr, a4
  343. #else
  344. lw errptr, 16(sp)
  345. #endif
  346. FEXPORT(csum_partial_copy_nocheck)
  347. move sum, zero
  348. move odd, zero
  349. /*
  350. * Note: dst & src may be unaligned, len may be 0
  351. * Temps
  352. */
  353. /*
  354. * The "issue break"s below are very approximate.
  355. * Issue delays for dcache fills will perturb the schedule, as will
  356. * load queue full replay traps, etc.
  357. *
  358. * If len < NBYTES use byte operations.
  359. */
  360. sltu t2, len, NBYTES
  361. and t1, dst, ADDRMASK
  362. bnez t2, .Lcopy_bytes_checklen
  363. and t0, src, ADDRMASK
  364. andi odd, dst, 0x1 /* odd buffer? */
  365. bnez t1, .Ldst_unaligned
  366. nop
  367. bnez t0, .Lsrc_unaligned_dst_aligned
  368. /*
  369. * use delay slot for fall-through
  370. * src and dst are aligned; need to compute rem
  371. */
  372. .Lboth_aligned:
  373. SRL t0, len, LOG_NBYTES+3 # +3 for 8 units/iter
  374. beqz t0, .Lcleanup_both_aligned # len < 8*NBYTES
  375. nop
  376. SUB len, 8*NBYTES # subtract here for bgez loop
  377. .align 4
  378. 1:
  379. EXC( LOAD t0, UNIT(0)(src), .Ll_exc)
  380. EXC( LOAD t1, UNIT(1)(src), .Ll_exc_copy)
  381. EXC( LOAD t2, UNIT(2)(src), .Ll_exc_copy)
  382. EXC( LOAD t3, UNIT(3)(src), .Ll_exc_copy)
  383. EXC( LOAD t4, UNIT(4)(src), .Ll_exc_copy)
  384. EXC( LOAD t5, UNIT(5)(src), .Ll_exc_copy)
  385. EXC( LOAD t6, UNIT(6)(src), .Ll_exc_copy)
  386. EXC( LOAD t7, UNIT(7)(src), .Ll_exc_copy)
  387. SUB len, len, 8*NBYTES
  388. ADD src, src, 8*NBYTES
  389. EXC( STORE t0, UNIT(0)(dst), .Ls_exc)
  390. ADDC(sum, t0)
  391. EXC( STORE t1, UNIT(1)(dst), .Ls_exc)
  392. ADDC(sum, t1)
  393. EXC( STORE t2, UNIT(2)(dst), .Ls_exc)
  394. ADDC(sum, t2)
  395. EXC( STORE t3, UNIT(3)(dst), .Ls_exc)
  396. ADDC(sum, t3)
  397. EXC( STORE t4, UNIT(4)(dst), .Ls_exc)
  398. ADDC(sum, t4)
  399. EXC( STORE t5, UNIT(5)(dst), .Ls_exc)
  400. ADDC(sum, t5)
  401. EXC( STORE t6, UNIT(6)(dst), .Ls_exc)
  402. ADDC(sum, t6)
  403. EXC( STORE t7, UNIT(7)(dst), .Ls_exc)
  404. ADDC(sum, t7)
  405. .set reorder /* DADDI_WAR */
  406. ADD dst, dst, 8*NBYTES
  407. bgez len, 1b
  408. .set noreorder
  409. ADD len, 8*NBYTES # revert len (see above)
  410. /*
  411. * len == the number of bytes left to copy < 8*NBYTES
  412. */
  413. .Lcleanup_both_aligned:
  414. #define rem t7
  415. beqz len, .Ldone
  416. sltu t0, len, 4*NBYTES
  417. bnez t0, .Lless_than_4units
  418. and rem, len, (NBYTES-1) # rem = len % NBYTES
  419. /*
  420. * len >= 4*NBYTES
  421. */
  422. EXC( LOAD t0, UNIT(0)(src), .Ll_exc)
  423. EXC( LOAD t1, UNIT(1)(src), .Ll_exc_copy)
  424. EXC( LOAD t2, UNIT(2)(src), .Ll_exc_copy)
  425. EXC( LOAD t3, UNIT(3)(src), .Ll_exc_copy)
  426. SUB len, len, 4*NBYTES
  427. ADD src, src, 4*NBYTES
  428. EXC( STORE t0, UNIT(0)(dst), .Ls_exc)
  429. ADDC(sum, t0)
  430. EXC( STORE t1, UNIT(1)(dst), .Ls_exc)
  431. ADDC(sum, t1)
  432. EXC( STORE t2, UNIT(2)(dst), .Ls_exc)
  433. ADDC(sum, t2)
  434. EXC( STORE t3, UNIT(3)(dst), .Ls_exc)
  435. ADDC(sum, t3)
  436. .set reorder /* DADDI_WAR */
  437. ADD dst, dst, 4*NBYTES
  438. beqz len, .Ldone
  439. .set noreorder
  440. .Lless_than_4units:
  441. /*
  442. * rem = len % NBYTES
  443. */
  444. beq rem, len, .Lcopy_bytes
  445. nop
  446. 1:
  447. EXC( LOAD t0, 0(src), .Ll_exc)
  448. ADD src, src, NBYTES
  449. SUB len, len, NBYTES
  450. EXC( STORE t0, 0(dst), .Ls_exc)
  451. ADDC(sum, t0)
  452. .set reorder /* DADDI_WAR */
  453. ADD dst, dst, NBYTES
  454. bne rem, len, 1b
  455. .set noreorder
  456. /*
  457. * src and dst are aligned, need to copy rem bytes (rem < NBYTES)
  458. * A loop would do only a byte at a time with possible branch
  459. * mispredicts. Can't do an explicit LOAD dst,mask,or,STORE
  460. * because can't assume read-access to dst. Instead, use
  461. * STREST dst, which doesn't require read access to dst.
  462. *
  463. * This code should perform better than a simple loop on modern,
  464. * wide-issue mips processors because the code has fewer branches and
  465. * more instruction-level parallelism.
  466. */
  467. #define bits t2
  468. beqz len, .Ldone
  469. ADD t1, dst, len # t1 is just past last byte of dst
  470. li bits, 8*NBYTES
  471. SLL rem, len, 3 # rem = number of bits to keep
  472. EXC( LOAD t0, 0(src), .Ll_exc)
  473. SUB bits, bits, rem # bits = number of bits to discard
  474. SHIFT_DISCARD t0, t0, bits
  475. EXC( STREST t0, -1(t1), .Ls_exc)
  476. SHIFT_DISCARD_REVERT t0, t0, bits
  477. .set reorder
  478. ADDC(sum, t0)
  479. b .Ldone
  480. .set noreorder
  481. .Ldst_unaligned:
  482. /*
  483. * dst is unaligned
  484. * t0 = src & ADDRMASK
  485. * t1 = dst & ADDRMASK; T1 > 0
  486. * len >= NBYTES
  487. *
  488. * Copy enough bytes to align dst
  489. * Set match = (src and dst have same alignment)
  490. */
  491. #define match rem
  492. EXC( LDFIRST t3, FIRST(0)(src), .Ll_exc)
  493. ADD t2, zero, NBYTES
  494. EXC( LDREST t3, REST(0)(src), .Ll_exc_copy)
  495. SUB t2, t2, t1 # t2 = number of bytes copied
  496. xor match, t0, t1
  497. EXC( STFIRST t3, FIRST(0)(dst), .Ls_exc)
  498. SLL t4, t1, 3 # t4 = number of bits to discard
  499. SHIFT_DISCARD t3, t3, t4
  500. /* no SHIFT_DISCARD_REVERT to handle odd buffer properly */
  501. ADDC(sum, t3)
  502. beq len, t2, .Ldone
  503. SUB len, len, t2
  504. ADD dst, dst, t2
  505. beqz match, .Lboth_aligned
  506. ADD src, src, t2
  507. .Lsrc_unaligned_dst_aligned:
  508. SRL t0, len, LOG_NBYTES+2 # +2 for 4 units/iter
  509. beqz t0, .Lcleanup_src_unaligned
  510. and rem, len, (4*NBYTES-1) # rem = len % 4*NBYTES
  511. 1:
  512. /*
  513. * Avoid consecutive LD*'s to the same register since some mips
  514. * implementations can't issue them in the same cycle.
  515. * It's OK to load FIRST(N+1) before REST(N) because the two addresses
  516. * are to the same unit (unless src is aligned, but it's not).
  517. */
  518. EXC( LDFIRST t0, FIRST(0)(src), .Ll_exc)
  519. EXC( LDFIRST t1, FIRST(1)(src), .Ll_exc_copy)
  520. SUB len, len, 4*NBYTES
  521. EXC( LDREST t0, REST(0)(src), .Ll_exc_copy)
  522. EXC( LDREST t1, REST(1)(src), .Ll_exc_copy)
  523. EXC( LDFIRST t2, FIRST(2)(src), .Ll_exc_copy)
  524. EXC( LDFIRST t3, FIRST(3)(src), .Ll_exc_copy)
  525. EXC( LDREST t2, REST(2)(src), .Ll_exc_copy)
  526. EXC( LDREST t3, REST(3)(src), .Ll_exc_copy)
  527. ADD src, src, 4*NBYTES
  528. #ifdef CONFIG_CPU_SB1
  529. nop # improves slotting
  530. #endif
  531. EXC( STORE t0, UNIT(0)(dst), .Ls_exc)
  532. ADDC(sum, t0)
  533. EXC( STORE t1, UNIT(1)(dst), .Ls_exc)
  534. ADDC(sum, t1)
  535. EXC( STORE t2, UNIT(2)(dst), .Ls_exc)
  536. ADDC(sum, t2)
  537. EXC( STORE t3, UNIT(3)(dst), .Ls_exc)
  538. ADDC(sum, t3)
  539. .set reorder /* DADDI_WAR */
  540. ADD dst, dst, 4*NBYTES
  541. bne len, rem, 1b
  542. .set noreorder
  543. .Lcleanup_src_unaligned:
  544. beqz len, .Ldone
  545. and rem, len, NBYTES-1 # rem = len % NBYTES
  546. beq rem, len, .Lcopy_bytes
  547. nop
  548. 1:
  549. EXC( LDFIRST t0, FIRST(0)(src), .Ll_exc)
  550. EXC( LDREST t0, REST(0)(src), .Ll_exc_copy)
  551. ADD src, src, NBYTES
  552. SUB len, len, NBYTES
  553. EXC( STORE t0, 0(dst), .Ls_exc)
  554. ADDC(sum, t0)
  555. .set reorder /* DADDI_WAR */
  556. ADD dst, dst, NBYTES
  557. bne len, rem, 1b
  558. .set noreorder
  559. .Lcopy_bytes_checklen:
  560. beqz len, .Ldone
  561. nop
  562. .Lcopy_bytes:
  563. /* 0 < len < NBYTES */
  564. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  565. #define SHIFT_START 0
  566. #define SHIFT_INC 8
  567. #else
  568. #define SHIFT_START 8*(NBYTES-1)
  569. #define SHIFT_INC -8
  570. #endif
  571. move t2, zero # partial word
  572. li t3, SHIFT_START # shift
  573. /* use .Ll_exc_copy here to return correct sum on fault */
  574. #define COPY_BYTE(N) \
  575. EXC( lbu t0, N(src), .Ll_exc_copy); \
  576. SUB len, len, 1; \
  577. EXC( sb t0, N(dst), .Ls_exc); \
  578. SLLV t0, t0, t3; \
  579. addu t3, SHIFT_INC; \
  580. beqz len, .Lcopy_bytes_done; \
  581. or t2, t0
  582. COPY_BYTE(0)
  583. COPY_BYTE(1)
  584. #ifdef USE_DOUBLE
  585. COPY_BYTE(2)
  586. COPY_BYTE(3)
  587. COPY_BYTE(4)
  588. COPY_BYTE(5)
  589. #endif
  590. EXC( lbu t0, NBYTES-2(src), .Ll_exc_copy)
  591. SUB len, len, 1
  592. EXC( sb t0, NBYTES-2(dst), .Ls_exc)
  593. SLLV t0, t0, t3
  594. or t2, t0
  595. .Lcopy_bytes_done:
  596. ADDC(sum, t2)
  597. .Ldone:
  598. /* fold checksum */
  599. .set push
  600. .set noat
  601. #ifdef USE_DOUBLE
  602. dsll32 v1, sum, 0
  603. daddu sum, v1
  604. sltu v1, sum, v1
  605. dsra32 sum, sum, 0
  606. addu sum, v1
  607. #endif
  608. /* odd buffer alignment? */
  609. beqz odd, 1f
  610. nop
  611. sll v1, sum, 8
  612. srl sum, sum, 8
  613. or sum, v1
  614. andi sum, 0xffff
  615. .set pop
  616. 1:
  617. .set reorder
  618. ADDC32(sum, psum)
  619. jr ra
  620. .set noreorder
  621. .Ll_exc_copy:
  622. /*
  623. * Copy bytes from src until faulting load address (or until a
  624. * lb faults)
  625. *
  626. * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
  627. * may be more than a byte beyond the last address.
  628. * Hence, the lb below may get an exception.
  629. *
  630. * Assumes src < THREAD_BUADDR($28)
  631. */
  632. LOAD t0, TI_TASK($28)
  633. li t2, SHIFT_START
  634. LOAD t0, THREAD_BUADDR(t0)
  635. 1:
  636. EXC( lbu t1, 0(src), .Ll_exc)
  637. ADD src, src, 1
  638. sb t1, 0(dst) # can't fault -- we're copy_from_user
  639. SLLV t1, t1, t2
  640. addu t2, SHIFT_INC
  641. ADDC(sum, t1)
  642. .set reorder /* DADDI_WAR */
  643. ADD dst, dst, 1
  644. bne src, t0, 1b
  645. .set noreorder
  646. .Ll_exc:
  647. LOAD t0, TI_TASK($28)
  648. nop
  649. LOAD t0, THREAD_BUADDR(t0) # t0 is just past last good address
  650. nop
  651. SUB len, AT, t0 # len number of uncopied bytes
  652. /*
  653. * Here's where we rely on src and dst being incremented in tandem,
  654. * See (3) above.
  655. * dst += (fault addr - src) to put dst at first byte to clear
  656. */
  657. ADD dst, t0 # compute start address in a1
  658. SUB dst, src
  659. /*
  660. * Clear len bytes starting at dst. Can't call __bzero because it
  661. * might modify len. An inefficient loop for these rare times...
  662. */
  663. .set reorder /* DADDI_WAR */
  664. SUB src, len, 1
  665. beqz len, .Ldone
  666. .set noreorder
  667. 1: sb zero, 0(dst)
  668. ADD dst, dst, 1
  669. .set push
  670. .set noat
  671. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  672. bnez src, 1b
  673. SUB src, src, 1
  674. #else
  675. li v1, 1
  676. bnez src, 1b
  677. SUB src, src, v1
  678. #endif
  679. li v1, -EFAULT
  680. b .Ldone
  681. sw v1, (errptr)
  682. .Ls_exc:
  683. li v0, -1 /* invalid checksum */
  684. li v1, -EFAULT
  685. jr ra
  686. sw v1, (errptr)
  687. .set pop
  688. END(__csum_partial_copy_user)