strncpy_from_user.S
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 <alpha/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. .gprel32 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 1
  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. ldgp $29, 0($27) # we do exceptions -- we need the gp.
  85. mov a0, v0 # save the string start
  86. beq a2, $zerolength
  87. /* Are source and destination co-aligned?  */
  88. xor a0, a1, t1 # e0    :
  89. and a0, 7, t0 # .. e1 : find dest misalignment
  90. and t1, 7, t1 # e0    :
  91. addq a2, t0, a2 # .. e1 : bias count by dest misalignment
  92. subq a2, 1, a2 # e0    :
  93. and a2, 7, t2 # e1    :
  94. srl a2, 3, a2 # e0    : a2 = loop counter = (count - 1)/8
  95. addq zero, 1, t10 # .. e1 :
  96. sll t10, t2, t10 # e0    : t10 = bitmask of last count byte
  97. bne t1, $unaligned # .. e1 :
  98. /* We are co-aligned; take care of a partial first word.  */
  99. EX( ldq_u t1, 0(a1) ) # e0    : load first src word
  100. addq a1, 8, a1 # .. e1 :
  101. beq t0, $aligned # avoid loading dest word if not needed
  102. ldq_u t0, 0(a0) # e0    :
  103. br $aligned # .. e1 :
  104. /* The source and destination are not co-aligned.  Align the destination
  105.    and cope.  We have to be very careful about not reading too much and
  106.    causing a SEGV.  */
  107. .align 3
  108. $u_head:
  109. /* We know just enough now to be able to assemble the first
  110.    full source word.  We can still find a zero at the end of it
  111.    that prevents us from outputting the whole thing.
  112.    On entry to this basic block:
  113.    t0 == the first dest word, unmasked
  114.    t1 == the shifted low bits of the first source word
  115.    t6 == bytemask that is -1 in dest word bytes */
  116. EX( ldq_u t2, 8(a1) ) # e0    : load second src word
  117. addq a1, 8, a1 # .. e1 :
  118. mskql t0, a0, t0 # e0    : mask trailing garbage in dst
  119. extqh t2, a1, t4 # e0    :
  120. or t1, t4, t1 # e1    : first aligned src word complete
  121. mskqh t1, a0, t1 # e0    : mask leading garbage in src
  122. or t0, t1, t0 # e0    : first output word complete
  123. or t0, t6, t6 # e1    : mask original data for zero test
  124. cmpbge zero, t6, t8 # e0    :
  125. beq a2, $u_eocfin # .. e1 :
  126. bne t8, $u_final # e1    :
  127. lda t6, -1 # e1    : mask out the bits we have
  128. mskql t6, a1, t6 # e0    :   already seen
  129. stq_u t0, 0(a0) # e0    : store first output word
  130. or      t6, t2, t2 # .. e1 :
  131. cmpbge zero, t2, t8 # e0    : find nulls in second partial
  132. addq a0, 8, a0 # .. e1 :
  133. subq a2, 1, a2 # e0    :
  134. bne t8, $u_late_head_exit # .. e1 :
  135. /* Finally, we've got all the stupid leading edge cases taken care
  136.    of and we can set up to enter the main loop.  */
  137. extql t2, a1, t1 # e0    : position hi-bits of lo word
  138. EX( ldq_u t2, 8(a1) ) # .. e1 : read next high-order source word
  139. addq a1, 8, a1 # e0    :
  140. cmpbge zero, t2, t8 # e1 (stall)
  141. beq a2, $u_eoc # e1    :
  142. bne t8, $u_eos # e1    :
  143. /* Unaligned copy main loop.  In order to avoid reading too much,
  144.    the loop is structured to detect zeros in aligned source words.
  145.    This has, unfortunately, effectively pulled half of a loop
  146.    iteration out into the head and half into the tail, but it does
  147.    prevent nastiness from accumulating in the very thing we want
  148.    to run as fast as possible.
  149.    On entry to this basic block:
  150.    t1 == the shifted high-order bits from the previous source word
  151.    t2 == the unshifted current source word
  152.    We further know that t2 does not contain a null terminator.  */
  153. .align 3
  154. $u_loop:
  155. extqh t2, a1, t0 # e0    : extract high bits for current word
  156. addq a1, 8, a1 # .. e1 :
  157. extql t2, a1, t3 # e0    : extract low bits for next time
  158. addq a0, 8, a0 # .. e1 :
  159. or t0, t1, t0 # e0    : current dst word now complete
  160. EX( ldq_u t2, 0(a1) ) # .. e1 : load high word for next time
  161. stq_u t0, -8(a0) # e0    : save the current word
  162. mov t3, t1 # .. e1 :
  163. subq a2, 1, a2 # e0    :
  164. cmpbge zero, t2, t8 # .. e1 : test new word for eos
  165. beq a2, $u_eoc # e1    :
  166. beq t8, $u_loop # e1    :
  167. /* We've found a zero somewhere in the source word we just read.
  168.    If it resides in the lower half, we have one (probably partial)
  169.    word to write out, and if it resides in the upper half, we
  170.    have one full and one partial word left to write out.
  171.    On entry to this basic block:
  172.    t1 == the shifted high-order bits from the previous source word
  173.    t2 == the unshifted current source word.  */
  174. $u_eos:
  175. extqh t2, a1, t0 # e0    :
  176. or t0, t1, t0 # e1    : first (partial) source word complete
  177. cmpbge zero, t0, t8 # e0    : is the null in this first bit?
  178. bne t8, $u_final # .. e1 (zdb)
  179. stq_u t0, 0(a0) # e0    : the null was in the high-order bits
  180. addq a0, 8, a0 # .. e1 :
  181. subq a2, 1, a2 # e1    :
  182. $u_late_head_exit:
  183. extql t2, a1, t0 # .. e0 :
  184. cmpbge zero, t0, t8 # e0    :
  185. or t8, t10, t6 # e1    :
  186. cmoveq a2, t6, t8 # e0    :
  187. nop # .. e1 :
  188. /* Take care of a final (probably partial) result word.
  189.    On entry to this basic block:
  190.    t0 == assembled source word
  191.    t8 == cmpbge mask that found the null.  */
  192. $u_final:
  193. negq t8, t6 # e0    : isolate low bit set
  194. and t6, t8, t12 # e1    :
  195. and t12, 0x80, t6 # e0    : avoid dest word load if we can
  196. bne t6, 1f # .. e1 (zdb)
  197. ldq_u t1, 0(a0) # e0    :
  198. subq t12, 1, t6 # .. e1 :
  199. or t6, t12, t8 # e0    :
  200. zapnot t0, t8, t0 # .. e1 : kill source bytes > null
  201. zap t1, t8, t1 # e0    : kill dest bytes <= null
  202. or t0, t1, t0 # e1    :
  203. 1: stq_u t0, 0(a0) # e0    :
  204. br $finish_up
  205. $u_eoc: # end-of-count
  206. extqh t2, a1, t0
  207. or t0, t1, t0
  208. cmpbge zero, t0, t8
  209. $u_eocfin: # end-of-count, final word
  210. or t10, t8, t8
  211. br $u_final
  212. /* Unaligned copy entry point.  */
  213. .align 3
  214. $unaligned:
  215. EX( ldq_u t1, 0(a1) ) # e0    : load first source word
  216. and a0, 7, t4 # .. e1 : find dest misalignment
  217. and a1, 7, t5 # e0    : find src misalignment
  218. /* Conditionally load the first destination word and a bytemask
  219.    with 0xff indicating that the destination byte is sacrosanct.  */
  220. mov zero, t0 # .. e1 :
  221. mov zero, t6 # e0    :
  222. beq t4, 1f # .. e1 :
  223. ldq_u t0, 0(a0) # e0    :
  224. lda t6, -1 # .. e1 :
  225. mskql t6, a0, t6 # e0    :
  226. 1:
  227. subq a1, t4, a1 # .. e1 : sub dest misalignment from src addr
  228. /* If source misalignment is larger than dest misalignment, we need
  229.    extra startup checks to avoid SEGV.  */
  230. cmplt t4, t5, t12 # e1    :
  231. extql t1, a1, t1 # .. e0 : shift src into place
  232. lda t2, -1 # e0    : for creating masks later
  233. beq t12, $u_head # e1    :
  234. mskqh t2, t5, t2 # e0    : begin src byte validity mask
  235. cmpbge zero, t1, t8 # .. e1 : is there a zero?
  236. extql t2, a1, t2 # e0    :
  237. or t8, t10, t5 # .. e1 : test for end-of-count too
  238. cmpbge zero, t2, t3 # e0    :
  239. cmoveq a2, t5, t8 # .. e1 :
  240. andnot t8, t3, t8 # e0    :
  241. beq t8, $u_head # .. e1 (zdb)
  242. /* At this point we've found a zero in the first partial word of
  243.    the source.  We need to isolate the valid source data and mask
  244.    it into the original destination data.  (Incidentally, we know
  245.    that we'll need at least one byte of that original dest word.) */
  246. ldq_u t0, 0(a0) # e0    :
  247. negq t8, t6 # .. e1 : build bitmask of bytes <= zero
  248. mskqh t1, t4, t1 # e0    :
  249. and t6, t8, t12 # .. e1 :
  250. subq t12, 1, t6 # e0    :
  251. or t6, t12, t8 # e1    :
  252. zapnot t2, t8, t2 # e0    : prepare source word; mirror changes
  253. zapnot t1, t8, t1 # .. e1 : to source validity mask
  254. andnot t0, t2, t0 # e0    : zero place for source to reside
  255. or t0, t1, t0 # e1    : and put it there
  256. stq_u t0, 0(a0) # e0    :
  257. $finish_up:
  258. zapnot t0, t12, t4 # was last byte written null?
  259. cmovne t4, 1, t4
  260. and t12, 0xf0, t3 # binary search for the address of the
  261. and t12, 0xcc, t2 # last byte written
  262. and t12, 0xaa, t1
  263. bic a0, 7, t0
  264. cmovne t3, 4, t3
  265. cmovne t2, 2, t2
  266. cmovne t1, 1, t1
  267. addq t0, t3, t0
  268. addq t1, t2, t1
  269. addq t0, t1, t0
  270. addq t0, t4, t0 # add one if we filled the buffer
  271. subq t0, v0, v0 # find string length
  272. ret
  273. $zerolength:
  274. clr v0
  275. $exception:
  276. ret
  277. .end __strncpy_from_user