ev6-copy_user.S 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * arch/alpha/lib/ev6-copy_user.S
  3. *
  4. * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com>
  5. *
  6. * Copy to/from user space, handling exceptions as we go.. This
  7. * isn't exactly pretty.
  8. *
  9. * This is essentially the same as "memcpy()", but with a few twists.
  10. * Notably, we have to make sure that $0 is always up-to-date and
  11. * contains the right "bytes left to copy" value (and that it is updated
  12. * only _after_ a successful copy). There is also some rather minor
  13. * exception setup stuff..
  14. *
  15. * NOTE! This is not directly C-callable, because the calling semantics are
  16. * different:
  17. *
  18. * Inputs:
  19. * length in $0
  20. * destination address in $6
  21. * source address in $7
  22. * return address in $28
  23. *
  24. * Outputs:
  25. * bytes left to copy in $0
  26. *
  27. * Clobbers:
  28. * $1,$2,$3,$4,$5,$6,$7
  29. *
  30. * Much of the information about 21264 scheduling/coding comes from:
  31. * Compiler Writer's Guide for the Alpha 21264
  32. * abbreviated as 'CWG' in other comments here
  33. * ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
  34. * Scheduling notation:
  35. * E - either cluster
  36. * U - upper subcluster; U0 - subcluster U0; U1 - subcluster U1
  37. * L - lower subcluster; L0 - subcluster L0; L1 - subcluster L1
  38. */
  39. /* Allow an exception for an insn; exit if we get one. */
  40. #define EXI(x,y...) \
  41. 99: x,##y; \
  42. .section __ex_table,"a"; \
  43. .long 99b - .; \
  44. lda $31, $exitin-99b($31); \
  45. .previous
  46. #define EXO(x,y...) \
  47. 99: x,##y; \
  48. .section __ex_table,"a"; \
  49. .long 99b - .; \
  50. lda $31, $exitout-99b($31); \
  51. .previous
  52. .set noat
  53. .align 4
  54. .globl __copy_user
  55. .ent __copy_user
  56. # Pipeline info: Slotting & Comments
  57. __copy_user:
  58. .prologue 0
  59. subq $0, 32, $1 # .. E .. .. : Is this going to be a small copy?
  60. beq $0, $zerolength # U .. .. .. : U L U L
  61. and $6,7,$3 # .. .. .. E : is leading dest misalignment
  62. ble $1, $onebyteloop # .. .. U .. : 1st branch : small amount of data
  63. beq $3, $destaligned # .. U .. .. : 2nd (one cycle fetcher stall)
  64. subq $3, 8, $3 # E .. .. .. : L U U L : trip counter
  65. /*
  66. * The fetcher stall also hides the 1 cycle cross-cluster stall for $3 (L --> U)
  67. * This loop aligns the destination a byte at a time
  68. * We know we have at least one trip through this loop
  69. */
  70. $aligndest:
  71. EXI( ldbu $1,0($7) ) # .. .. .. L : Keep loads separate from stores
  72. addq $6,1,$6 # .. .. E .. : Section 3.8 in the CWG
  73. addq $3,1,$3 # .. E .. .. :
  74. nop # E .. .. .. : U L U L
  75. /*
  76. * the -1 is to compensate for the inc($6) done in a previous quadpack
  77. * which allows us zero dependencies within either quadpack in the loop
  78. */
  79. EXO( stb $1,-1($6) ) # .. .. .. L :
  80. addq $7,1,$7 # .. .. E .. : Section 3.8 in the CWG
  81. subq $0,1,$0 # .. E .. .. :
  82. bne $3, $aligndest # U .. .. .. : U L U L
  83. /*
  84. * If we fell through into here, we have a minimum of 33 - 7 bytes
  85. * If we arrived via branch, we have a minimum of 32 bytes
  86. */
  87. $destaligned:
  88. and $7,7,$1 # .. .. .. E : Check _current_ source alignment
  89. bic $0,7,$4 # .. .. E .. : number bytes as a quadword loop
  90. EXI( ldq_u $3,0($7) ) # .. L .. .. : Forward fetch for fallthrough code
  91. beq $1,$quadaligned # U .. .. .. : U L U L
  92. /*
  93. * In the worst case, we've just executed an ldq_u here from 0($7)
  94. * and we'll repeat it once if we take the branch
  95. */
  96. /* Misaligned quadword loop - not unrolled. Leave it that way. */
  97. $misquad:
  98. EXI( ldq_u $2,8($7) ) # .. .. .. L :
  99. subq $4,8,$4 # .. .. E .. :
  100. extql $3,$7,$3 # .. U .. .. :
  101. extqh $2,$7,$1 # U .. .. .. : U U L L
  102. bis $3,$1,$1 # .. .. .. E :
  103. EXO( stq $1,0($6) ) # .. .. L .. :
  104. addq $7,8,$7 # .. E .. .. :
  105. subq $0,8,$0 # E .. .. .. : U L L U
  106. addq $6,8,$6 # .. .. .. E :
  107. bis $2,$2,$3 # .. .. E .. :
  108. nop # .. E .. .. :
  109. bne $4,$misquad # U .. .. .. : U L U L
  110. nop # .. .. .. E
  111. nop # .. .. E ..
  112. nop # .. E .. ..
  113. beq $0,$zerolength # U .. .. .. : U L U L
  114. /* We know we have at least one trip through the byte loop */
  115. EXI ( ldbu $2,0($7) ) # .. .. .. L : No loads in the same quad
  116. addq $6,1,$6 # .. .. E .. : as the store (Section 3.8 in CWG)
  117. nop # .. E .. .. :
  118. br $31, $dirtyentry # L0 .. .. .. : L U U L
  119. /* Do the trailing byte loop load, then hop into the store part of the loop */
  120. /*
  121. * A minimum of (33 - 7) bytes to do a quad at a time.
  122. * Based upon the usage context, it's worth the effort to unroll this loop
  123. * $0 - number of bytes to be moved
  124. * $4 - number of bytes to move as quadwords
  125. * $6 is current destination address
  126. * $7 is current source address
  127. */
  128. $quadaligned:
  129. subq $4, 32, $2 # .. .. .. E : do not unroll for small stuff
  130. nop # .. .. E ..
  131. nop # .. E .. ..
  132. blt $2, $onequad # U .. .. .. : U L U L
  133. /*
  134. * There is a significant assumption here that the source and destination
  135. * addresses differ by more than 32 bytes. In this particular case, a
  136. * sparsity of registers further bounds this to be a minimum of 8 bytes.
  137. * But if this isn't met, then the output result will be incorrect.
  138. * Furthermore, due to a lack of available registers, we really can't
  139. * unroll this to be an 8x loop (which would enable us to use the wh64
  140. * instruction memory hint instruction).
  141. */
  142. $unroll4:
  143. EXI( ldq $1,0($7) ) # .. .. .. L
  144. EXI( ldq $2,8($7) ) # .. .. L ..
  145. subq $4,32,$4 # .. E .. ..
  146. nop # E .. .. .. : U U L L
  147. addq $7,16,$7 # .. .. .. E
  148. EXO( stq $1,0($6) ) # .. .. L ..
  149. EXO( stq $2,8($6) ) # .. L .. ..
  150. subq $0,16,$0 # E .. .. .. : U L L U
  151. addq $6,16,$6 # .. .. .. E
  152. EXI( ldq $1,0($7) ) # .. .. L ..
  153. EXI( ldq $2,8($7) ) # .. L .. ..
  154. subq $4, 32, $3 # E .. .. .. : U U L L : is there enough for another trip?
  155. EXO( stq $1,0($6) ) # .. .. .. L
  156. EXO( stq $2,8($6) ) # .. .. L ..
  157. subq $0,16,$0 # .. E .. ..
  158. addq $7,16,$7 # E .. .. .. : U L L U
  159. nop # .. .. .. E
  160. nop # .. .. E ..
  161. addq $6,16,$6 # .. E .. ..
  162. bgt $3,$unroll4 # U .. .. .. : U L U L
  163. nop
  164. nop
  165. nop
  166. beq $4, $noquads
  167. $onequad:
  168. EXI( ldq $1,0($7) )
  169. subq $4,8,$4
  170. addq $7,8,$7
  171. nop
  172. EXO( stq $1,0($6) )
  173. subq $0,8,$0
  174. addq $6,8,$6
  175. bne $4,$onequad
  176. $noquads:
  177. nop
  178. nop
  179. nop
  180. beq $0,$zerolength
  181. /*
  182. * For small copies (or the tail of a larger copy), do a very simple byte loop.
  183. * There's no point in doing a lot of complex alignment calculations to try to
  184. * to quadword stuff for a small amount of data.
  185. * $0 - remaining number of bytes left to copy
  186. * $6 - current dest addr
  187. * $7 - current source addr
  188. */
  189. $onebyteloop:
  190. EXI ( ldbu $2,0($7) ) # .. .. .. L : No loads in the same quad
  191. addq $6,1,$6 # .. .. E .. : as the store (Section 3.8 in CWG)
  192. nop # .. E .. .. :
  193. nop # E .. .. .. : U L U L
  194. $dirtyentry:
  195. /*
  196. * the -1 is to compensate for the inc($6) done in a previous quadpack
  197. * which allows us zero dependencies within either quadpack in the loop
  198. */
  199. EXO ( stb $2,-1($6) ) # .. .. .. L :
  200. addq $7,1,$7 # .. .. E .. : quadpack as the load
  201. subq $0,1,$0 # .. E .. .. : change count _after_ copy
  202. bgt $0,$onebyteloop # U .. .. .. : U L U L
  203. $zerolength:
  204. $exitout: # Destination for exception recovery(?)
  205. nop # .. .. .. E
  206. nop # .. .. E ..
  207. nop # .. E .. ..
  208. ret $31,($28),1 # L0 .. .. .. : L U L U
  209. $exitin:
  210. /* A stupid byte-by-byte zeroing of the rest of the output
  211. buffer. This cures security holes by never leaving
  212. random kernel data around to be copied elsewhere. */
  213. nop
  214. nop
  215. nop
  216. mov $0,$1
  217. $101:
  218. EXO ( stb $31,0($6) ) # L
  219. subq $1,1,$1 # E
  220. addq $6,1,$6 # E
  221. bgt $1,$101 # U
  222. nop
  223. nop
  224. nop
  225. ret $31,($28),1 # L0
  226. .end __copy_user