clear_user.S 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * This routine clears to zero a linear memory buffer in user space.
  3. *
  4. * Inputs:
  5. * in0: address of buffer
  6. * in1: length of buffer in bytes
  7. * Outputs:
  8. * r8: number of bytes that didn't get cleared due to a fault
  9. *
  10. * Copyright (C) 1998, 1999, 2001 Hewlett-Packard Co
  11. * Stephane Eranian <eranian@hpl.hp.com>
  12. */
  13. #include <asm/asmmacro.h>
  14. #include <asm/export.h>
  15. //
  16. // arguments
  17. //
  18. #define buf r32
  19. #define len r33
  20. //
  21. // local registers
  22. //
  23. #define cnt r16
  24. #define buf2 r17
  25. #define saved_lc r18
  26. #define saved_pfs r19
  27. #define tmp r20
  28. #define len2 r21
  29. #define len3 r22
  30. //
  31. // Theory of operations:
  32. // - we check whether or not the buffer is small, i.e., less than 17
  33. // in which case we do the byte by byte loop.
  34. //
  35. // - Otherwise we go progressively from 1 byte store to 8byte store in
  36. // the head part, the body is a 16byte store loop and we finish we the
  37. // tail for the last 15 bytes.
  38. // The good point about this breakdown is that the long buffer handling
  39. // contains only 2 branches.
  40. //
  41. // The reason for not using shifting & masking for both the head and the
  42. // tail is to stay semantically correct. This routine is not supposed
  43. // to write bytes outside of the buffer. While most of the time this would
  44. // be ok, we can't tolerate a mistake. A classical example is the case
  45. // of multithreaded code were to the extra bytes touched is actually owned
  46. // by another thread which runs concurrently to ours. Another, less likely,
  47. // example is with device drivers where reading an I/O mapped location may
  48. // have side effects (same thing for writing).
  49. //
  50. GLOBAL_ENTRY(__do_clear_user)
  51. .prologue
  52. .save ar.pfs, saved_pfs
  53. alloc saved_pfs=ar.pfs,2,0,0,0
  54. cmp.eq p6,p0=r0,len // check for zero length
  55. .save ar.lc, saved_lc
  56. mov saved_lc=ar.lc // preserve ar.lc (slow)
  57. .body
  58. ;; // avoid WAW on CFM
  59. adds tmp=-1,len // br.ctop is repeat/until
  60. mov ret0=len // return value is length at this point
  61. (p6) br.ret.spnt.many rp
  62. ;;
  63. cmp.lt p6,p0=16,len // if len > 16 then long memset
  64. mov ar.lc=tmp // initialize lc for small count
  65. (p6) br.cond.dptk .long_do_clear
  66. ;; // WAR on ar.lc
  67. //
  68. // worst case 16 iterations, avg 8 iterations
  69. //
  70. // We could have played with the predicates to use the extra
  71. // M slot for 2 stores/iteration but the cost the initialization
  72. // the various counters compared to how long the loop is supposed
  73. // to last on average does not make this solution viable.
  74. //
  75. 1:
  76. EX( .Lexit1, st1 [buf]=r0,1 )
  77. adds len=-1,len // countdown length using len
  78. br.cloop.dptk 1b
  79. ;; // avoid RAW on ar.lc
  80. //
  81. // .Lexit4: comes from byte by byte loop
  82. // len contains bytes left
  83. .Lexit1:
  84. mov ret0=len // faster than using ar.lc
  85. mov ar.lc=saved_lc
  86. br.ret.sptk.many rp // end of short clear_user
  87. //
  88. // At this point we know we have more than 16 bytes to copy
  89. // so we focus on alignment (no branches required)
  90. //
  91. // The use of len/len2 for countdown of the number of bytes left
  92. // instead of ret0 is due to the fact that the exception code
  93. // changes the values of r8.
  94. //
  95. .long_do_clear:
  96. tbit.nz p6,p0=buf,0 // odd alignment (for long_do_clear)
  97. ;;
  98. EX( .Lexit3, (p6) st1 [buf]=r0,1 ) // 1-byte aligned
  99. (p6) adds len=-1,len;; // sync because buf is modified
  100. tbit.nz p6,p0=buf,1
  101. ;;
  102. EX( .Lexit3, (p6) st2 [buf]=r0,2 ) // 2-byte aligned
  103. (p6) adds len=-2,len;;
  104. tbit.nz p6,p0=buf,2
  105. ;;
  106. EX( .Lexit3, (p6) st4 [buf]=r0,4 ) // 4-byte aligned
  107. (p6) adds len=-4,len;;
  108. tbit.nz p6,p0=buf,3
  109. ;;
  110. EX( .Lexit3, (p6) st8 [buf]=r0,8 ) // 8-byte aligned
  111. (p6) adds len=-8,len;;
  112. shr.u cnt=len,4 // number of 128-bit (2x64bit) words
  113. ;;
  114. cmp.eq p6,p0=r0,cnt
  115. adds tmp=-1,cnt
  116. (p6) br.cond.dpnt .dotail // we have less than 16 bytes left
  117. ;;
  118. adds buf2=8,buf // setup second base pointer
  119. mov ar.lc=tmp
  120. ;;
  121. //
  122. // 16bytes/iteration core loop
  123. //
  124. // The second store can never generate a fault because
  125. // we come into the loop only when we are 16-byte aligned.
  126. // This means that if we cross a page then it will always be
  127. // in the first store and never in the second.
  128. //
  129. //
  130. // We need to keep track of the remaining length. A possible (optimistic)
  131. // way would be to use ar.lc and derive how many byte were left by
  132. // doing : left= 16*ar.lc + 16. this would avoid the addition at
  133. // every iteration.
  134. // However we need to keep the synchronization point. A template
  135. // M;;MB does not exist and thus we can keep the addition at no
  136. // extra cycle cost (use a nop slot anyway). It also simplifies the
  137. // (unlikely) error recovery code
  138. //
  139. 2: EX(.Lexit3, st8 [buf]=r0,16 )
  140. ;; // needed to get len correct when error
  141. st8 [buf2]=r0,16
  142. adds len=-16,len
  143. br.cloop.dptk 2b
  144. ;;
  145. mov ar.lc=saved_lc
  146. //
  147. // tail correction based on len only
  148. //
  149. // We alternate the use of len3,len2 to allow parallelism and correct
  150. // error handling. We also reuse p6/p7 to return correct value.
  151. // The addition of len2/len3 does not cost anything more compared to
  152. // the regular memset as we had empty slots.
  153. //
  154. .dotail:
  155. mov len2=len // for parallelization of error handling
  156. mov len3=len
  157. tbit.nz p6,p0=len,3
  158. ;;
  159. EX( .Lexit2, (p6) st8 [buf]=r0,8 ) // at least 8 bytes
  160. (p6) adds len3=-8,len2
  161. tbit.nz p7,p6=len,2
  162. ;;
  163. EX( .Lexit2, (p7) st4 [buf]=r0,4 ) // at least 4 bytes
  164. (p7) adds len2=-4,len3
  165. tbit.nz p6,p7=len,1
  166. ;;
  167. EX( .Lexit2, (p6) st2 [buf]=r0,2 ) // at least 2 bytes
  168. (p6) adds len3=-2,len2
  169. tbit.nz p7,p6=len,0
  170. ;;
  171. EX( .Lexit2, (p7) st1 [buf]=r0 ) // only 1 byte left
  172. mov ret0=r0 // success
  173. br.ret.sptk.many rp // end of most likely path
  174. //
  175. // Outlined error handling code
  176. //
  177. //
  178. // .Lexit3: comes from core loop, need restore pr/lc
  179. // len contains bytes left
  180. //
  181. //
  182. // .Lexit2:
  183. // if p6 -> coming from st8 or st2 : len2 contains what's left
  184. // if p7 -> coming from st4 or st1 : len3 contains what's left
  185. // We must restore lc/pr even though might not have been used.
  186. .Lexit2:
  187. .pred.rel "mutex", p6, p7
  188. (p6) mov len=len2
  189. (p7) mov len=len3
  190. ;;
  191. //
  192. // .Lexit4: comes from head, need not restore pr/lc
  193. // len contains bytes left
  194. //
  195. .Lexit3:
  196. mov ret0=len
  197. mov ar.lc=saved_lc
  198. br.ret.sptk.many rp
  199. END(__do_clear_user)
  200. EXPORT_SYMBOL(__do_clear_user)