18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Optimized version of the standard strlen() function 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Inputs: 88c2ecf20Sopenharmony_ci * in0 address of string 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Outputs: 118c2ecf20Sopenharmony_ci * ret0 the number of characters in the string (0 if empty string) 128c2ecf20Sopenharmony_ci * does not count the \0 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * Copyright (C) 1999, 2001 Hewlett-Packard Co 158c2ecf20Sopenharmony_ci * Stephane Eranian <eranian@hpl.hp.com> 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * 09/24/99 S.Eranian add speculation recovery code 188c2ecf20Sopenharmony_ci */ 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <asm/asmmacro.h> 218c2ecf20Sopenharmony_ci#include <asm/export.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci// 248c2ecf20Sopenharmony_ci// 258c2ecf20Sopenharmony_ci// This is an enhanced version of the basic strlen. it includes a combination 268c2ecf20Sopenharmony_ci// of compute zero index (czx), parallel comparisons, speculative loads and 278c2ecf20Sopenharmony_ci// loop unroll using rotating registers. 288c2ecf20Sopenharmony_ci// 298c2ecf20Sopenharmony_ci// General Ideas about the algorithm: 308c2ecf20Sopenharmony_ci// The goal is to look at the string in chunks of 8 bytes. 318c2ecf20Sopenharmony_ci// so we need to do a few extra checks at the beginning because the 328c2ecf20Sopenharmony_ci// string may not be 8-byte aligned. In this case we load the 8byte 338c2ecf20Sopenharmony_ci// quantity which includes the start of the string and mask the unused 348c2ecf20Sopenharmony_ci// bytes with 0xff to avoid confusing czx. 358c2ecf20Sopenharmony_ci// We use speculative loads and software pipelining to hide memory 368c2ecf20Sopenharmony_ci// latency and do read ahead safely. This way we defer any exception. 378c2ecf20Sopenharmony_ci// 388c2ecf20Sopenharmony_ci// Because we don't want the kernel to be relying on particular 398c2ecf20Sopenharmony_ci// settings of the DCR register, we provide recovery code in case 408c2ecf20Sopenharmony_ci// speculation fails. The recovery code is going to "redo" the work using 418c2ecf20Sopenharmony_ci// only normal loads. If we still get a fault then we generate a 428c2ecf20Sopenharmony_ci// kernel panic. Otherwise we return the strlen as usual. 438c2ecf20Sopenharmony_ci// 448c2ecf20Sopenharmony_ci// The fact that speculation may fail can be caused, for instance, by 458c2ecf20Sopenharmony_ci// the DCR.dm bit being set. In this case TLB misses are deferred, i.e., 468c2ecf20Sopenharmony_ci// a NaT bit will be set if the translation is not present. The normal 478c2ecf20Sopenharmony_ci// load, on the other hand, will cause the translation to be inserted 488c2ecf20Sopenharmony_ci// if the mapping exists. 498c2ecf20Sopenharmony_ci// 508c2ecf20Sopenharmony_ci// It should be noted that we execute recovery code only when we need 518c2ecf20Sopenharmony_ci// to use the data that has been speculatively loaded: we don't execute 528c2ecf20Sopenharmony_ci// recovery code on pure read ahead data. 538c2ecf20Sopenharmony_ci// 548c2ecf20Sopenharmony_ci// Remarks: 558c2ecf20Sopenharmony_ci// - the cmp r0,r0 is used as a fast way to initialize a predicate 568c2ecf20Sopenharmony_ci// register to 1. This is required to make sure that we get the parallel 578c2ecf20Sopenharmony_ci// compare correct. 588c2ecf20Sopenharmony_ci// 598c2ecf20Sopenharmony_ci// - we don't use the epilogue counter to exit the loop but we need to set 608c2ecf20Sopenharmony_ci// it to zero beforehand. 618c2ecf20Sopenharmony_ci// 628c2ecf20Sopenharmony_ci// - after the loop we must test for Nat values because neither the 638c2ecf20Sopenharmony_ci// czx nor cmp instruction raise a NaT consumption fault. We must be 648c2ecf20Sopenharmony_ci// careful not to look too far for a Nat for which we don't care. 658c2ecf20Sopenharmony_ci// For instance we don't need to look at a NaT in val2 if the zero byte 668c2ecf20Sopenharmony_ci// was in val1. 678c2ecf20Sopenharmony_ci// 688c2ecf20Sopenharmony_ci// - Clearly performance tuning is required. 698c2ecf20Sopenharmony_ci// 708c2ecf20Sopenharmony_ci// 718c2ecf20Sopenharmony_ci// 728c2ecf20Sopenharmony_ci#define saved_pfs r11 738c2ecf20Sopenharmony_ci#define tmp r10 748c2ecf20Sopenharmony_ci#define base r16 758c2ecf20Sopenharmony_ci#define orig r17 768c2ecf20Sopenharmony_ci#define saved_pr r18 778c2ecf20Sopenharmony_ci#define src r19 788c2ecf20Sopenharmony_ci#define mask r20 798c2ecf20Sopenharmony_ci#define val r21 808c2ecf20Sopenharmony_ci#define val1 r22 818c2ecf20Sopenharmony_ci#define val2 r23 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ciGLOBAL_ENTRY(strlen) 848c2ecf20Sopenharmony_ci .prologue 858c2ecf20Sopenharmony_ci .save ar.pfs, saved_pfs 868c2ecf20Sopenharmony_ci alloc saved_pfs=ar.pfs,11,0,0,8 // rotating must be multiple of 8 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci .rotr v[2], w[2] // declares our 4 aliases 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci extr.u tmp=in0,0,3 // tmp=least significant 3 bits 918c2ecf20Sopenharmony_ci mov orig=in0 // keep trackof initial byte address 928c2ecf20Sopenharmony_ci dep src=0,in0,0,3 // src=8byte-aligned in0 address 938c2ecf20Sopenharmony_ci .save pr, saved_pr 948c2ecf20Sopenharmony_ci mov saved_pr=pr // preserve predicates (rotation) 958c2ecf20Sopenharmony_ci ;; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci .body 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci ld8 v[1]=[src],8 // must not speculate: can fail here 1008c2ecf20Sopenharmony_ci shl tmp=tmp,3 // multiply by 8bits/byte 1018c2ecf20Sopenharmony_ci mov mask=-1 // our mask 1028c2ecf20Sopenharmony_ci ;; 1038c2ecf20Sopenharmony_ci ld8.s w[1]=[src],8 // speculatively load next 1048c2ecf20Sopenharmony_ci cmp.eq p6,p0=r0,r0 // sets p6 to true for cmp.and 1058c2ecf20Sopenharmony_ci sub tmp=64,tmp // how many bits to shift our mask on the right 1068c2ecf20Sopenharmony_ci ;; 1078c2ecf20Sopenharmony_ci shr.u mask=mask,tmp // zero enough bits to hold v[1] valuable part 1088c2ecf20Sopenharmony_ci mov ar.ec=r0 // clear epilogue counter (saved in ar.pfs) 1098c2ecf20Sopenharmony_ci ;; 1108c2ecf20Sopenharmony_ci add base=-16,src // keep track of aligned base 1118c2ecf20Sopenharmony_ci or v[1]=v[1],mask // now we have a safe initial byte pattern 1128c2ecf20Sopenharmony_ci ;; 1138c2ecf20Sopenharmony_ci1: 1148c2ecf20Sopenharmony_ci ld8.s v[0]=[src],8 // speculatively load next 1158c2ecf20Sopenharmony_ci czx1.r val1=v[1] // search 0 byte from right 1168c2ecf20Sopenharmony_ci czx1.r val2=w[1] // search 0 byte from right following 8bytes 1178c2ecf20Sopenharmony_ci ;; 1188c2ecf20Sopenharmony_ci ld8.s w[0]=[src],8 // speculatively load next to next 1198c2ecf20Sopenharmony_ci cmp.eq.and p6,p0=8,val1 // p6 = p6 and val1==8 1208c2ecf20Sopenharmony_ci cmp.eq.and p6,p0=8,val2 // p6 = p6 and mask==8 1218c2ecf20Sopenharmony_ci(p6) br.wtop.dptk 1b // loop until p6 == 0 1228c2ecf20Sopenharmony_ci ;; 1238c2ecf20Sopenharmony_ci // 1248c2ecf20Sopenharmony_ci // We must return try the recovery code iff 1258c2ecf20Sopenharmony_ci // val1_is_nat || (val1==8 && val2_is_nat) 1268c2ecf20Sopenharmony_ci // 1278c2ecf20Sopenharmony_ci // XXX Fixme 1288c2ecf20Sopenharmony_ci // - there must be a better way of doing the test 1298c2ecf20Sopenharmony_ci // 1308c2ecf20Sopenharmony_ci cmp.eq p8,p9=8,val1 // p6 = val1 had zero (disambiguate) 1318c2ecf20Sopenharmony_ci tnat.nz p6,p7=val1 // test NaT on val1 1328c2ecf20Sopenharmony_ci(p6) br.cond.spnt .recover // jump to recovery if val1 is NaT 1338c2ecf20Sopenharmony_ci ;; 1348c2ecf20Sopenharmony_ci // 1358c2ecf20Sopenharmony_ci // if we come here p7 is true, i.e., initialized for // cmp 1368c2ecf20Sopenharmony_ci // 1378c2ecf20Sopenharmony_ci cmp.eq.and p7,p0=8,val1// val1==8? 1388c2ecf20Sopenharmony_ci tnat.nz.and p7,p0=val2 // test NaT if val2 1398c2ecf20Sopenharmony_ci(p7) br.cond.spnt .recover // jump to recovery if val2 is NaT 1408c2ecf20Sopenharmony_ci ;; 1418c2ecf20Sopenharmony_ci(p8) mov val1=val2 // the other test got us out of the loop 1428c2ecf20Sopenharmony_ci(p8) adds src=-16,src // correct position when 3 ahead 1438c2ecf20Sopenharmony_ci(p9) adds src=-24,src // correct position when 4 ahead 1448c2ecf20Sopenharmony_ci ;; 1458c2ecf20Sopenharmony_ci sub ret0=src,orig // distance from base 1468c2ecf20Sopenharmony_ci sub tmp=8,val1 // which byte in word 1478c2ecf20Sopenharmony_ci mov pr=saved_pr,0xffffffffffff0000 1488c2ecf20Sopenharmony_ci ;; 1498c2ecf20Sopenharmony_ci sub ret0=ret0,tmp // adjust 1508c2ecf20Sopenharmony_ci mov ar.pfs=saved_pfs // because of ar.ec, restore no matter what 1518c2ecf20Sopenharmony_ci br.ret.sptk.many rp // end of normal execution 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci // 1548c2ecf20Sopenharmony_ci // Outlined recovery code when speculation failed 1558c2ecf20Sopenharmony_ci // 1568c2ecf20Sopenharmony_ci // This time we don't use speculation and rely on the normal exception 1578c2ecf20Sopenharmony_ci // mechanism. that's why the loop is not as good as the previous one 1588c2ecf20Sopenharmony_ci // because read ahead is not possible 1598c2ecf20Sopenharmony_ci // 1608c2ecf20Sopenharmony_ci // IMPORTANT: 1618c2ecf20Sopenharmony_ci // Please note that in the case of strlen() as opposed to strlen_user() 1628c2ecf20Sopenharmony_ci // we don't use the exception mechanism, as this function is not 1638c2ecf20Sopenharmony_ci // supposed to fail. If that happens it means we have a bug and the 1648c2ecf20Sopenharmony_ci // code will cause of kernel fault. 1658c2ecf20Sopenharmony_ci // 1668c2ecf20Sopenharmony_ci // XXX Fixme 1678c2ecf20Sopenharmony_ci // - today we restart from the beginning of the string instead 1688c2ecf20Sopenharmony_ci // of trying to continue where we left off. 1698c2ecf20Sopenharmony_ci // 1708c2ecf20Sopenharmony_ci.recover: 1718c2ecf20Sopenharmony_ci ld8 val=[base],8 // will fail if unrecoverable fault 1728c2ecf20Sopenharmony_ci ;; 1738c2ecf20Sopenharmony_ci or val=val,mask // remask first bytes 1748c2ecf20Sopenharmony_ci cmp.eq p0,p6=r0,r0 // nullify first ld8 in loop 1758c2ecf20Sopenharmony_ci ;; 1768c2ecf20Sopenharmony_ci // 1778c2ecf20Sopenharmony_ci // ar.ec is still zero here 1788c2ecf20Sopenharmony_ci // 1798c2ecf20Sopenharmony_ci2: 1808c2ecf20Sopenharmony_ci(p6) ld8 val=[base],8 // will fail if unrecoverable fault 1818c2ecf20Sopenharmony_ci ;; 1828c2ecf20Sopenharmony_ci czx1.r val1=val // search 0 byte from right 1838c2ecf20Sopenharmony_ci ;; 1848c2ecf20Sopenharmony_ci cmp.eq p6,p0=8,val1 // val1==8 ? 1858c2ecf20Sopenharmony_ci(p6) br.wtop.dptk 2b // loop until p6 == 0 1868c2ecf20Sopenharmony_ci ;; // (avoid WAW on p63) 1878c2ecf20Sopenharmony_ci sub ret0=base,orig // distance from base 1888c2ecf20Sopenharmony_ci sub tmp=8,val1 1898c2ecf20Sopenharmony_ci mov pr=saved_pr,0xffffffffffff0000 1908c2ecf20Sopenharmony_ci ;; 1918c2ecf20Sopenharmony_ci sub ret0=ret0,tmp // length=now - back -1 1928c2ecf20Sopenharmony_ci mov ar.pfs=saved_pfs // because of ar.ec, restore no matter what 1938c2ecf20Sopenharmony_ci br.ret.sptk.many rp // end of successful recovery code 1948c2ecf20Sopenharmony_ciEND(strlen) 1958c2ecf20Sopenharmony_ciEXPORT_SYMBOL(strlen) 196