strncpy_from_user.S 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * arch/alpha/lib/strncpy_from_user.S
  3. * Contributed by Richard Henderson (rth@tamu.edu)
  4. *
  5. * Just like strncpy except in the return value:
  6. *
  7. * -EFAULT if an exception occurs before the terminator is copied.
  8. * N if the buffer filled.
  9. *
  10. * Otherwise the length of the string is returned.
  11. */
  12. #include <asm/errno.h>
  13. #include <asm/regdef.h>
  14. /* Allow an exception for an insn; exit if we get one. */
  15. #define EX(x,y...) \
  16. 99: x,##y; \
  17. .section __ex_table,"a"; \
  18. .long 99b - .; \
  19. lda $31, $exception-99b($0); \
  20. .previous
  21. .set noat
  22. .set noreorder
  23. .text
  24. .globl __strncpy_from_user
  25. .ent __strncpy_from_user
  26. .frame $30, 0, $26
  27. .prologue 0
  28. .align 3
  29. $aligned:
  30. /* On entry to this basic block:
  31. t0 == the first destination word for masking back in
  32. t1 == the first source word. */
  33. /* Create the 1st output word and detect 0's in the 1st input word. */
  34. lda t2, -1 # e1 : build a mask against false zero
  35. mskqh t2, a1, t2 # e0 : detection in the src word
  36. mskqh t1, a1, t3 # e0 :
  37. ornot t1, t2, t2 # .. e1 :
  38. mskql t0, a1, t0 # e0 : assemble the first output word
  39. cmpbge zero, t2, t8 # .. e1 : bits set iff null found
  40. or t0, t3, t0 # e0 :
  41. beq a2, $a_eoc # .. e1 :
  42. bne t8, $a_eos # .. e1 :
  43. /* On entry to this basic block:
  44. t0 == a source word not containing a null. */
  45. $a_loop:
  46. stq_u t0, 0(a0) # e0 :
  47. addq a0, 8, a0 # .. e1 :
  48. EX( ldq_u t0, 0(a1) ) # e0 :
  49. addq a1, 8, a1 # .. e1 :
  50. subq a2, 1, a2 # e0 :
  51. cmpbge zero, t0, t8 # .. e1 (stall)
  52. beq a2, $a_eoc # e1 :
  53. beq t8, $a_loop # e1 :
  54. /* Take care of the final (partial) word store. At this point
  55. the end-of-count bit is set in t8 iff it applies.
  56. On entry to this basic block we have:
  57. t0 == the source word containing the null
  58. t8 == the cmpbge mask that found it. */
  59. $a_eos:
  60. negq t8, t12 # e0 : find low bit set
  61. and t8, t12, t12 # e1 (stall)
  62. /* For the sake of the cache, don't read a destination word
  63. if we're not going to need it. */
  64. and t12, 0x80, t6 # e0 :
  65. bne t6, 1f # .. e1 (zdb)
  66. /* We're doing a partial word store and so need to combine
  67. our source and original destination words. */
  68. ldq_u t1, 0(a0) # e0 :
  69. subq t12, 1, t6 # .. e1 :
  70. or t12, t6, t8 # e0 :
  71. unop #
  72. zapnot t0, t8, t0 # e0 : clear src bytes > null
  73. zap t1, t8, t1 # .. e1 : clear dst bytes <= null
  74. or t0, t1, t0 # e1 :
  75. 1: stq_u t0, 0(a0)
  76. br $finish_up
  77. /* Add the end-of-count bit to the eos detection bitmask. */
  78. $a_eoc:
  79. or t10, t8, t8
  80. br $a_eos
  81. /*** The Function Entry Point ***/
  82. .align 3
  83. __strncpy_from_user:
  84. mov a0, v0 # save the string start
  85. beq a2, $zerolength
  86. /* Are source and destination co-aligned? */
  87. xor a0, a1, t1 # e0 :
  88. and a0, 7, t0 # .. e1 : find dest misalignment
  89. and t1, 7, t1 # e0 :
  90. addq a2, t0, a2 # .. e1 : bias count by dest misalignment
  91. subq a2, 1, a2 # e0 :
  92. and a2, 7, t2 # e1 :
  93. srl a2, 3, a2 # e0 : a2 = loop counter = (count - 1)/8
  94. addq zero, 1, t10 # .. e1 :
  95. sll t10, t2, t10 # e0 : t10 = bitmask of last count byte
  96. bne t1, $unaligned # .. e1 :
  97. /* We are co-aligned; take care of a partial first word. */
  98. EX( ldq_u t1, 0(a1) ) # e0 : load first src word
  99. addq a1, 8, a1 # .. e1 :
  100. beq t0, $aligned # avoid loading dest word if not needed
  101. ldq_u t0, 0(a0) # e0 :
  102. br $aligned # .. e1 :
  103. /* The source and destination are not co-aligned. Align the destination
  104. and cope. We have to be very careful about not reading too much and
  105. causing a SEGV. */
  106. .align 3
  107. $u_head:
  108. /* We know just enough now to be able to assemble the first
  109. full source word. We can still find a zero at the end of it
  110. that prevents us from outputting the whole thing.
  111. On entry to this basic block:
  112. t0 == the first dest word, unmasked
  113. t1 == the shifted low bits of the first source word
  114. t6 == bytemask that is -1 in dest word bytes */
  115. EX( ldq_u t2, 8(a1) ) # e0 : load second src word
  116. addq a1, 8, a1 # .. e1 :
  117. mskql t0, a0, t0 # e0 : mask trailing garbage in dst
  118. extqh t2, a1, t4 # e0 :
  119. or t1, t4, t1 # e1 : first aligned src word complete
  120. mskqh t1, a0, t1 # e0 : mask leading garbage in src
  121. or t0, t1, t0 # e0 : first output word complete
  122. or t0, t6, t6 # e1 : mask original data for zero test
  123. cmpbge zero, t6, t8 # e0 :
  124. beq a2, $u_eocfin # .. e1 :
  125. bne t8, $u_final # e1 :
  126. lda t6, -1 # e1 : mask out the bits we have
  127. mskql t6, a1, t6 # e0 : already seen
  128. stq_u t0, 0(a0) # e0 : store first output word
  129. or t6, t2, t2 # .. e1 :
  130. cmpbge zero, t2, t8 # e0 : find nulls in second partial
  131. addq a0, 8, a0 # .. e1 :
  132. subq a2, 1, a2 # e0 :
  133. bne t8, $u_late_head_exit # .. e1 :
  134. /* Finally, we've got all the stupid leading edge cases taken care
  135. of and we can set up to enter the main loop. */
  136. extql t2, a1, t1 # e0 : position hi-bits of lo word
  137. EX( ldq_u t2, 8(a1) ) # .. e1 : read next high-order source word
  138. addq a1, 8, a1 # e0 :
  139. cmpbge zero, t2, t8 # e1 (stall)
  140. beq a2, $u_eoc # e1 :
  141. bne t8, $u_eos # e1 :
  142. /* Unaligned copy main loop. In order to avoid reading too much,
  143. the loop is structured to detect zeros in aligned source words.
  144. This has, unfortunately, effectively pulled half of a loop
  145. iteration out into the head and half into the tail, but it does
  146. prevent nastiness from accumulating in the very thing we want
  147. to run as fast as possible.
  148. On entry to this basic block:
  149. t1 == the shifted high-order bits from the previous source word
  150. t2 == the unshifted current source word
  151. We further know that t2 does not contain a null terminator. */
  152. .align 3
  153. $u_loop:
  154. extqh t2, a1, t0 # e0 : extract high bits for current word
  155. addq a1, 8, a1 # .. e1 :
  156. extql t2, a1, t3 # e0 : extract low bits for next time
  157. addq a0, 8, a0 # .. e1 :
  158. or t0, t1, t0 # e0 : current dst word now complete
  159. EX( ldq_u t2, 0(a1) ) # .. e1 : load high word for next time
  160. stq_u t0, -8(a0) # e0 : save the current word
  161. mov t3, t1 # .. e1 :
  162. subq a2, 1, a2 # e0 :
  163. cmpbge zero, t2, t8 # .. e1 : test new word for eos
  164. beq a2, $u_eoc # e1 :
  165. beq t8, $u_loop # e1 :
  166. /* We've found a zero somewhere in the source word we just read.
  167. If it resides in the lower half, we have one (probably partial)
  168. word to write out, and if it resides in the upper half, we
  169. have one full and one partial word left to write out.
  170. On entry to this basic block:
  171. t1 == the shifted high-order bits from the previous source word
  172. t2 == the unshifted current source word. */
  173. $u_eos:
  174. extqh t2, a1, t0 # e0 :
  175. or t0, t1, t0 # e1 : first (partial) source word complete
  176. cmpbge zero, t0, t8 # e0 : is the null in this first bit?
  177. bne t8, $u_final # .. e1 (zdb)
  178. stq_u t0, 0(a0) # e0 : the null was in the high-order bits
  179. addq a0, 8, a0 # .. e1 :
  180. subq a2, 1, a2 # e1 :
  181. $u_late_head_exit:
  182. extql t2, a1, t0 # .. e0 :
  183. cmpbge zero, t0, t8 # e0 :
  184. or t8, t10, t6 # e1 :
  185. cmoveq a2, t6, t8 # e0 :
  186. nop # .. e1 :
  187. /* Take care of a final (probably partial) result word.
  188. On entry to this basic block:
  189. t0 == assembled source word
  190. t8 == cmpbge mask that found the null. */
  191. $u_final:
  192. negq t8, t6 # e0 : isolate low bit set
  193. and t6, t8, t12 # e1 :
  194. and t12, 0x80, t6 # e0 : avoid dest word load if we can
  195. bne t6, 1f # .. e1 (zdb)
  196. ldq_u t1, 0(a0) # e0 :
  197. subq t12, 1, t6 # .. e1 :
  198. or t6, t12, t8 # e0 :
  199. zapnot t0, t8, t0 # .. e1 : kill source bytes > null
  200. zap t1, t8, t1 # e0 : kill dest bytes <= null
  201. or t0, t1, t0 # e1 :
  202. 1: stq_u t0, 0(a0) # e0 :
  203. br $finish_up
  204. $u_eoc: # end-of-count
  205. extqh t2, a1, t0
  206. or t0, t1, t0
  207. cmpbge zero, t0, t8
  208. $u_eocfin: # end-of-count, final word
  209. or t10, t8, t8
  210. br $u_final
  211. /* Unaligned copy entry point. */
  212. .align 3
  213. $unaligned:
  214. EX( ldq_u t1, 0(a1) ) # e0 : load first source word
  215. and a0, 7, t4 # .. e1 : find dest misalignment
  216. and a1, 7, t5 # e0 : find src misalignment
  217. /* Conditionally load the first destination word and a bytemask
  218. with 0xff indicating that the destination byte is sacrosanct. */
  219. mov zero, t0 # .. e1 :
  220. mov zero, t6 # e0 :
  221. beq t4, 1f # .. e1 :
  222. ldq_u t0, 0(a0) # e0 :
  223. lda t6, -1 # .. e1 :
  224. mskql t6, a0, t6 # e0 :
  225. 1:
  226. subq a1, t4, a1 # .. e1 : sub dest misalignment from src addr
  227. /* If source misalignment is larger than dest misalignment, we need
  228. extra startup checks to avoid SEGV. */
  229. cmplt t4, t5, t12 # e1 :
  230. extql t1, a1, t1 # .. e0 : shift src into place
  231. lda t2, -1 # e0 : for creating masks later
  232. beq t12, $u_head # e1 :
  233. mskqh t2, t5, t2 # e0 : begin src byte validity mask
  234. cmpbge zero, t1, t8 # .. e1 : is there a zero?
  235. extql t2, a1, t2 # e0 :
  236. or t8, t10, t5 # .. e1 : test for end-of-count too
  237. cmpbge zero, t2, t3 # e0 :
  238. cmoveq a2, t5, t8 # .. e1 :
  239. andnot t8, t3, t8 # e0 :
  240. beq t8, $u_head # .. e1 (zdb)
  241. /* At this point we've found a zero in the first partial word of
  242. the source. We need to isolate the valid source data and mask
  243. it into the original destination data. (Incidentally, we know
  244. that we'll need at least one byte of that original dest word.) */
  245. ldq_u t0, 0(a0) # e0 :
  246. negq t8, t6 # .. e1 : build bitmask of bytes <= zero
  247. mskqh t1, t4, t1 # e0 :
  248. and t6, t8, t12 # .. e1 :
  249. subq t12, 1, t6 # e0 :
  250. or t6, t12, t8 # e1 :
  251. zapnot t2, t8, t2 # e0 : prepare source word; mirror changes
  252. zapnot t1, t8, t1 # .. e1 : to source validity mask
  253. andnot t0, t2, t0 # e0 : zero place for source to reside
  254. or t0, t1, t0 # e1 : and put it there
  255. stq_u t0, 0(a0) # e0 :
  256. $finish_up:
  257. zapnot t0, t12, t4 # was last byte written null?
  258. cmovne t4, 1, t4
  259. and t12, 0xf0, t3 # binary search for the address of the
  260. and t12, 0xcc, t2 # last byte written
  261. and t12, 0xaa, t1
  262. bic a0, 7, t0
  263. cmovne t3, 4, t3
  264. cmovne t2, 2, t2
  265. cmovne t1, 1, t1
  266. addq t0, t3, t0
  267. addq t1, t2, t1
  268. addq t0, t1, t0
  269. addq t0, t4, t0 # add one if we filled the buffer
  270. subq t0, v0, v0 # find string length
  271. ret
  272. $zerolength:
  273. clr v0
  274. $exception:
  275. ret
  276. .end __strncpy_from_user