Back to home page

LXR

 
 

    


0001 /*
0002  *
0003  * Optimized version of the standard strlen() function
0004  *
0005  *
0006  * Inputs:
0007  *  in0 address of string
0008  *
0009  * Outputs:
0010  *  ret0    the number of characters in the string (0 if empty string)
0011  *  does not count the \0
0012  *
0013  * Copyright (C) 1999, 2001 Hewlett-Packard Co
0014  *  Stephane Eranian <eranian@hpl.hp.com>
0015  *
0016  * 09/24/99 S.Eranian add speculation recovery code
0017  */
0018 
0019 #include <asm/asmmacro.h>
0020 #include <asm/export.h>
0021 
0022 //
0023 //
0024 // This is an enhanced version of the basic strlen. it includes a combination
0025 // of compute zero index (czx), parallel comparisons, speculative loads and
0026 // loop unroll using rotating registers.
0027 //
0028 // General Ideas about the algorithm:
0029 //    The goal is to look at the string in chunks of 8 bytes.
0030 //    so we need to do a few extra checks at the beginning because the
0031 //    string may not be 8-byte aligned. In this case we load the 8byte
0032 //    quantity which includes the start of the string and mask the unused
0033 //    bytes with 0xff to avoid confusing czx.
0034 //    We use speculative loads and software pipelining to hide memory
0035 //    latency and do read ahead safely. This way we defer any exception.
0036 //
0037 //    Because we don't want the kernel to be relying on particular
0038 //    settings of the DCR register, we provide recovery code in case
0039 //    speculation fails. The recovery code is going to "redo" the work using
0040 //    only normal loads. If we still get a fault then we generate a
0041 //    kernel panic. Otherwise we return the strlen as usual.
0042 //
0043 //    The fact that speculation may fail can be caused, for instance, by
0044 //    the DCR.dm bit being set. In this case TLB misses are deferred, i.e.,
0045 //    a NaT bit will be set if the translation is not present. The normal
0046 //    load, on the other hand, will cause the translation to be inserted
0047 //    if the mapping exists.
0048 //
0049 //    It should be noted that we execute recovery code only when we need
0050 //    to use the data that has been speculatively loaded: we don't execute
0051 //    recovery code on pure read ahead data.
0052 //
0053 // Remarks:
0054 //  - the cmp r0,r0 is used as a fast way to initialize a predicate
0055 //    register to 1. This is required to make sure that we get the parallel
0056 //    compare correct.
0057 //
0058 //  - we don't use the epilogue counter to exit the loop but we need to set
0059 //    it to zero beforehand.
0060 //
0061 //  - after the loop we must test for Nat values because neither the
0062 //    czx nor cmp instruction raise a NaT consumption fault. We must be
0063 //    careful not to look too far for a Nat for which we don't care.
0064 //    For instance we don't need to look at a NaT in val2 if the zero byte
0065 //    was in val1.
0066 //
0067 //  - Clearly performance tuning is required.
0068 //
0069 //
0070 //
0071 #define saved_pfs   r11
0072 #define tmp     r10
0073 #define base        r16
0074 #define orig        r17
0075 #define saved_pr    r18
0076 #define src     r19
0077 #define mask        r20
0078 #define val     r21
0079 #define val1        r22
0080 #define val2        r23
0081 
0082 GLOBAL_ENTRY(strlen)
0083     .prologue
0084     .save ar.pfs, saved_pfs
0085     alloc saved_pfs=ar.pfs,11,0,0,8 // rotating must be multiple of 8
0086 
0087     .rotr v[2], w[2]    // declares our 4 aliases
0088 
0089     extr.u tmp=in0,0,3  // tmp=least significant 3 bits
0090     mov orig=in0        // keep trackof initial byte address
0091     dep src=0,in0,0,3   // src=8byte-aligned in0 address
0092     .save pr, saved_pr
0093     mov saved_pr=pr     // preserve predicates (rotation)
0094     ;;
0095 
0096     .body
0097 
0098     ld8 v[1]=[src],8    // must not speculate: can fail here
0099     shl tmp=tmp,3       // multiply by 8bits/byte
0100     mov mask=-1     // our mask
0101     ;;
0102     ld8.s w[1]=[src],8  // speculatively load next
0103     cmp.eq p6,p0=r0,r0  // sets p6 to true for cmp.and
0104     sub tmp=64,tmp      // how many bits to shift our mask on the right
0105     ;;
0106     shr.u   mask=mask,tmp   // zero enough bits to hold v[1] valuable part
0107     mov ar.ec=r0        // clear epilogue counter (saved in ar.pfs)
0108     ;;
0109     add base=-16,src    // keep track of aligned base
0110     or v[1]=v[1],mask   // now we have a safe initial byte pattern
0111     ;;
0112 1:
0113     ld8.s v[0]=[src],8  // speculatively load next
0114     czx1.r val1=v[1]    // search 0 byte from right
0115     czx1.r val2=w[1]    // search 0 byte from right following 8bytes
0116     ;;
0117     ld8.s w[0]=[src],8  // speculatively load next to next
0118     cmp.eq.and p6,p0=8,val1 // p6 = p6 and val1==8
0119     cmp.eq.and p6,p0=8,val2 // p6 = p6 and mask==8
0120 (p6)    br.wtop.dptk 1b     // loop until p6 == 0
0121     ;;
0122     //
0123     // We must return try the recovery code iff
0124     // val1_is_nat || (val1==8 && val2_is_nat)
0125     //
0126     // XXX Fixme
0127     //  - there must be a better way of doing the test
0128     //
0129     cmp.eq  p8,p9=8,val1    // p6 = val1 had zero (disambiguate)
0130     tnat.nz p6,p7=val1  // test NaT on val1
0131 (p6)    br.cond.spnt .recover   // jump to recovery if val1 is NaT
0132     ;;
0133     //
0134     // if we come here p7 is true, i.e., initialized for // cmp
0135     //
0136     cmp.eq.and  p7,p0=8,val1// val1==8?
0137     tnat.nz.and p7,p0=val2  // test NaT if val2
0138 (p7)    br.cond.spnt .recover   // jump to recovery if val2 is NaT
0139     ;;
0140 (p8)    mov val1=val2       // the other test got us out of the loop
0141 (p8)    adds src=-16,src    // correct position when 3 ahead
0142 (p9)    adds src=-24,src    // correct position when 4 ahead
0143     ;;
0144     sub ret0=src,orig   // distance from base
0145     sub tmp=8,val1      // which byte in word
0146     mov pr=saved_pr,0xffffffffffff0000
0147     ;;
0148     sub ret0=ret0,tmp   // adjust
0149     mov ar.pfs=saved_pfs    // because of ar.ec, restore no matter what
0150     br.ret.sptk.many rp // end of normal execution
0151 
0152     //
0153     // Outlined recovery code when speculation failed
0154     //
0155     // This time we don't use speculation and rely on the normal exception
0156     // mechanism. that's why the loop is not as good as the previous one
0157     // because read ahead is not possible
0158     //
0159     // IMPORTANT:
0160     // Please note that in the case of strlen() as opposed to strlen_user()
0161     // we don't use the exception mechanism, as this function is not
0162     // supposed to fail. If that happens it means we have a bug and the
0163     // code will cause of kernel fault.
0164     //
0165     // XXX Fixme
0166     //  - today we restart from the beginning of the string instead
0167     //    of trying to continue where we left off.
0168     //
0169 .recover:
0170     ld8 val=[base],8    // will fail if unrecoverable fault
0171     ;;
0172     or val=val,mask     // remask first bytes
0173     cmp.eq p0,p6=r0,r0  // nullify first ld8 in loop
0174     ;;
0175     //
0176     // ar.ec is still zero here
0177     //
0178 2:
0179 (p6)    ld8 val=[base],8    // will fail if unrecoverable fault
0180     ;;
0181     czx1.r val1=val     // search 0 byte from right
0182     ;;
0183     cmp.eq p6,p0=8,val1 // val1==8 ?
0184 (p6)    br.wtop.dptk 2b     // loop until p6 == 0
0185     ;;          // (avoid WAW on p63)
0186     sub ret0=base,orig  // distance from base
0187     sub tmp=8,val1
0188     mov pr=saved_pr,0xffffffffffff0000
0189     ;;
0190     sub ret0=ret0,tmp   // length=now - back -1
0191     mov ar.pfs=saved_pfs    // because of ar.ec, restore no matter what
0192     br.ret.sptk.many rp // end of successful recovery code
0193 END(strlen)
0194 EXPORT_SYMBOL(strlen)