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