strlen.S 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. *
  3. * Optimized version of the standard strlen() function
  4. *
  5. *
  6. * Inputs:
  7. * in0 address of string
  8. *
  9. * Outputs:
  10. * ret0 the number of characters in the string (0 if empty string)
  11. * does not count the \0
  12. *
  13. * Copyright (C) 1999, 2001 Hewlett-Packard Co
  14. * Stephane Eranian <eranian@hpl.hp.com>
  15. *
  16. * 09/24/99 S.Eranian add speculation recovery code
  17. */
  18. #include <asm/asmmacro.h>
  19. //
  20. //
  21. // This is an enhanced version of the basic strlen. it includes a combination
  22. // of compute zero index (czx), parallel comparisons, speculative loads and
  23. // loop unroll using rotating registers.
  24. //
  25. // General Ideas about the algorithm:
  26. // The goal is to look at the string in chunks of 8 bytes.
  27. // so we need to do a few extra checks at the beginning because the
  28. // string may not be 8-byte aligned. In this case we load the 8byte
  29. // quantity which includes the start of the string and mask the unused
  30. // bytes with 0xff to avoid confusing czx.
  31. // We use speculative loads and software pipelining to hide memory
  32. // latency and do read ahead safely. This way we defer any exception.
  33. //
  34. // Because we don't want the kernel to be relying on particular
  35. // settings of the DCR register, we provide recovery code in case
  36. // speculation fails. The recovery code is going to "redo" the work using
  37. // only normal loads. If we still get a fault then we generate a
  38. // kernel panic. Otherwise we return the strlen as usual.
  39. //
  40. // The fact that speculation may fail can be caused, for instance, by
  41. // the DCR.dm bit being set. In this case TLB misses are deferred, i.e.,
  42. // a NaT bit will be set if the translation is not present. The normal
  43. // load, on the other hand, will cause the translation to be inserted
  44. // if the mapping exists.
  45. //
  46. // It should be noted that we execute recovery code only when we need
  47. // to use the data that has been speculatively loaded: we don't execute
  48. // recovery code on pure read ahead data.
  49. //
  50. // Remarks:
  51. // - the cmp r0,r0 is used as a fast way to initialize a predicate
  52. // register to 1. This is required to make sure that we get the parallel
  53. // compare correct.
  54. //
  55. // - we don't use the epilogue counter to exit the loop but we need to set
  56. // it to zero beforehand.
  57. //
  58. // - after the loop we must test for Nat values because neither the
  59. // czx nor cmp instruction raise a NaT consumption fault. We must be
  60. // careful not to look too far for a Nat for which we don't care.
  61. // For instance we don't need to look at a NaT in val2 if the zero byte
  62. // was in val1.
  63. //
  64. // - Clearly performance tuning is required.
  65. //
  66. //
  67. //
  68. #define saved_pfs r11
  69. #define tmp r10
  70. #define base r16
  71. #define orig r17
  72. #define saved_pr r18
  73. #define src r19
  74. #define mask r20
  75. #define val r21
  76. #define val1 r22
  77. #define val2 r23
  78. GLOBAL_ENTRY(strlen)
  79. .prologue
  80. .save ar.pfs, saved_pfs
  81. alloc saved_pfs=ar.pfs,11,0,0,8 // rotating must be multiple of 8
  82. .rotr v[2], w[2] // declares our 4 aliases
  83. extr.u tmp=in0,0,3 // tmp=least significant 3 bits
  84. mov orig=in0 // keep trackof initial byte address
  85. dep src=0,in0,0,3 // src=8byte-aligned in0 address
  86. .save pr, saved_pr
  87. mov saved_pr=pr // preserve predicates (rotation)
  88. ;;
  89. .body
  90. ld8 v[1]=[src],8 // must not speculate: can fail here
  91. shl tmp=tmp,3 // multiply by 8bits/byte
  92. mov mask=-1 // our mask
  93. ;;
  94. ld8.s w[1]=[src],8 // speculatively load next
  95. cmp.eq p6,p0=r0,r0 // sets p6 to true for cmp.and
  96. sub tmp=64,tmp // how many bits to shift our mask on the right
  97. ;;
  98. shr.u mask=mask,tmp // zero enough bits to hold v[1] valuable part
  99. mov ar.ec=r0 // clear epilogue counter (saved in ar.pfs)
  100. ;;
  101. add base=-16,src // keep track of aligned base
  102. or v[1]=v[1],mask // now we have a safe initial byte pattern
  103. ;;
  104. 1:
  105. ld8.s v[0]=[src],8 // speculatively load next
  106. czx1.r val1=v[1] // search 0 byte from right
  107. czx1.r val2=w[1] // search 0 byte from right following 8bytes
  108. ;;
  109. ld8.s w[0]=[src],8 // speculatively load next to next
  110. cmp.eq.and p6,p0=8,val1 // p6 = p6 and val1==8
  111. cmp.eq.and p6,p0=8,val2 // p6 = p6 and mask==8
  112. (p6) br.wtop.dptk 1b // loop until p6 == 0
  113. ;;
  114. //
  115. // We must return try the recovery code iff
  116. // val1_is_nat || (val1==8 && val2_is_nat)
  117. //
  118. // XXX Fixme
  119. // - there must be a better way of doing the test
  120. //
  121. cmp.eq p8,p9=8,val1 // p6 = val1 had zero (disambiguate)
  122. tnat.nz p6,p7=val1 // test NaT on val1
  123. (p6) br.cond.spnt .recover // jump to recovery if val1 is NaT
  124. ;;
  125. //
  126. // if we come here p7 is true, i.e., initialized for // cmp
  127. //
  128. cmp.eq.and p7,p0=8,val1// val1==8?
  129. tnat.nz.and p7,p0=val2 // test NaT if val2
  130. (p7) br.cond.spnt .recover // jump to recovery if val2 is NaT
  131. ;;
  132. (p8) mov val1=val2 // the other test got us out of the loop
  133. (p8) adds src=-16,src // correct position when 3 ahead
  134. (p9) adds src=-24,src // correct position when 4 ahead
  135. ;;
  136. sub ret0=src,orig // distance from base
  137. sub tmp=8,val1 // which byte in word
  138. mov pr=saved_pr,0xffffffffffff0000
  139. ;;
  140. sub ret0=ret0,tmp // adjust
  141. mov ar.pfs=saved_pfs // because of ar.ec, restore no matter what
  142. br.ret.sptk.many rp // end of normal execution
  143. //
  144. // Outlined recovery code when speculation failed
  145. //
  146. // This time we don't use speculation and rely on the normal exception
  147. // mechanism. that's why the loop is not as good as the previous one
  148. // because read ahead is not possible
  149. //
  150. // IMPORTANT:
  151. // Please note that in the case of strlen() as opposed to strlen_user()
  152. // we don't use the exception mechanism, as this function is not
  153. // supposed to fail. If that happens it means we have a bug and the
  154. // code will cause of kernel fault.
  155. //
  156. // XXX Fixme
  157. // - today we restart from the beginning of the string instead
  158. // of trying to continue where we left off.
  159. //
  160. .recover:
  161. ld8 val=[base],8 // will fail if unrecoverable fault
  162. ;;
  163. or val=val,mask // remask first bytes
  164. cmp.eq p0,p6=r0,r0 // nullify first ld8 in loop
  165. ;;
  166. //
  167. // ar.ec is still zero here
  168. //
  169. 2:
  170. (p6) ld8 val=[base],8 // will fail if unrecoverable fault
  171. ;;
  172. czx1.r val1=val // search 0 byte from right
  173. ;;
  174. cmp.eq p6,p0=8,val1 // val1==8 ?
  175. (p6) br.wtop.dptk 2b // loop until p6 == 0
  176. ;; // (avoid WAW on p63)
  177. sub ret0=base,orig // distance from base
  178. sub tmp=8,val1
  179. mov pr=saved_pr,0xffffffffffff0000
  180. ;;
  181. sub ret0=ret0,tmp // length=now - back -1
  182. mov ar.pfs=saved_pfs // because of ar.ec, restore no matter what
  183. br.ret.sptk.many rp // end of successful recovery code
  184. END(strlen)