strlen_user.S 6.6 KB

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