Back to home page

LXR

 
 

    


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