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