aboutsummaryrefslogtreecommitdiff
path: root/arch/alpha/lib/ev6-strncpy_from_user.S
blob: d2e28178caccc8b59fb2aea43b88e2934e54a7db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * arch/alpha/lib/ev6-strncpy_from_user.S
 * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com>
 *
 * Just like strncpy except in the return value:
 *
 * -EFAULT       if an exception occurs before the terminator is copied.
 * N             if the buffer filled.
 *
 * Otherwise the length of the string is returned.
 *
 * Much of the information about 21264 scheduling/coding comes from:
 *	Compiler Writer's Guide for the Alpha 21264
 *	abbreviated as 'CWG' in other comments here
 *	ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
 * Scheduling notation:
 *	E	- either cluster
 *	U	- upper subcluster; U0 - subcluster U0; U1 - subcluster U1
 *	L	- lower subcluster; L0 - subcluster L0; L1 - subcluster L1
 * A bunch of instructions got moved and temp registers were changed
 * to aid in scheduling.  Control flow was also re-arranged to eliminate
 * branches, and to provide longer code sequences to enable better scheduling.
 * A total rewrite (using byte load/stores for start & tail sequences)
 * is desirable, but very difficult to do without a from-scratch rewrite.
 * Save that for the future.
 */


#include <asm/errno.h>
#include <asm/regdef.h>


/* Allow an exception for an insn; exit if we get one.  */
#define EX(x,y...)			\
	99: x,##y;			\
	.section __ex_table,"a";	\
	.long 99b - .;			\
	lda $31, $exception-99b($0); 	\
	.previous


	.set noat
	.set noreorder
	.text

	.globl __strncpy_from_user
	.ent __strncpy_from_user
	.frame $30, 0, $26
	.prologue 0

	.align 4
__strncpy_from_user:
	and	a0, 7, t3	# E : find dest misalignment
	beq	a2, $zerolength	# U :

	/* Are source and destination co-aligned?  */
	mov	a0, v0		# E : save the string start
	xor	a0, a1, t4	# E :
	EX( ldq_u t1, 0(a1) )	# L : Latency=3 load first quadword
	ldq_u	t0, 0(a0)	# L : load first (partial) aligned dest quadword

	addq	a2, t3, a2	# E : bias count by dest misalignment
	subq	a2, 1, a3	# E :
	addq	zero, 1, t10	# E :
	and	t4, 7, t4	# E : misalignment between the two

	and	a3, 7, t6	# E : number of tail bytes
	sll	t10, t6, t10	# E : t10 = bitmask of last count byte
	bne	t4, $unaligned	# U :
	lda	t2, -1		# E : build a mask against false zero

	/*
	 * We are co-aligned; take care of a partial first word.
	 * On entry to this basic block:
	 * t0 == the first destination word for masking back in
	 * t1 == the first source word.
	 */

	srl	a3, 3, a2	# E : a2 = loop counter = (count - 1)/8
	addq	a1, 8, a1	# E :
	mskqh	t2, a1, t2	# U :   detection in the src word
	nop

	/* Create the 1st output word and detect 0's in the 1st input word.  */
	mskqh	t1, a1, t3	# U :
	mskql	t0, a1, t0	# U : assemble the first output word
	ornot	t1, t2, t2	# E :
	nop

	cmpbge	zero, t2, t8	# E : bits set iff null found
	or	t0, t3, t0	# E :
	beq	a2, $a_eoc	# U :
	bne	t8, $a_eos	# U : 2nd branch in a quad.  Bad.

	/* On entry to this basic block:
	 * t0 == a source quad not containing a null.
	 * a0 - current aligned destination address
	 * a1 - current aligned source address
	 * a2 - count of quadwords to move.
	 * NOTE: Loop improvement - unrolling this is going to be
	 *	a huge win, since we're going to stall otherwise.
	 *	Fix this later.  For _really_ large copies, look
	 *	at using wh64 on a look-ahead basis.  See the code
	 *	in clear_user.S and copy_user.S.
	 * Presumably, since (a0) and (a1) do not overlap (by C definition)
	 * Lots of nops here:
	 *	- Separate loads from stores
	 *	- Keep it to 1 branch/quadpack so the branch predictor
	 *	  can train.
	 */
$a_loop:
	stq_u	t0, 0(a0)	# L :
	addq	a0, 8, a0	# E :
	nop
	subq	a2, 1, a2	# E :

	EX( ldq_u t0, 0(a1) )	# L :
	addq	a1, 8, a1	# E :
	cmpbge	zero, t0, t8	# E : Stall 2 cycles on t0
	beq	a2, $a_eoc      # U :

	beq	t8, $a_loop	# U :
	nop
	nop
	nop

	/* Take care of the final (partial) word store.  At this point
	 * the end-of-count bit is set in t8 iff it applies.
	 *
	 * On entry to this basic block we have:
	 * t0 == the source word containing the null
	 * t8 == the cmpbge mask that found it.
	 */
$a_eos:
	negq	t8, t12		# E : find low bit set
	and	t8, t12, t12	# E : 

	/* We're doing a partial word store and so need to combine
	   our source and original destination words.  */
	ldq_u	t1, 0(a0)	# L :
	subq	t12, 1, t6	# E :

	or	t12, t6, t8	# E :
	zapnot	t0, t8, t0	# U : clear src bytes > null
	zap	t1, t8, t1	# U : clear dst bytes <= null
	or	t0, t1, t0	# E :

	stq_u	t0, 0(a0)	# L :
	br	$finish_up	# L0 :
	nop
	nop

	/* Add the end-of-count bit to the eos detection bitmask.  */
	.align 4
$a_eoc:
	or	t10, t8, t8
	br	$a_eos
	nop
	nop


/* The source and destination are not co-aligned.  Align the destination
   and cope.  We have to be very careful about not reading too much and
   causing a SEGV.  */

	.align 4
$u_head:
	/* We know just enough now to be able to assemble the first
	   full source word.  We can still find a zero at the end of it
	   that prevents us from outputting the whole thing.

	   On entry to this basic block:
	   t0 == the first dest word, unmasked
	   t1 == the shifted low bits of the first source word
	   t6 == bytemask that is -1 in dest word bytes */

	EX( ldq_u t2, 8(a1) )	# L : load second src word
	addq	a1, 8, a1	# E :
	mskql	t0, a0, t0	# U : mask trailing garbage in dst
	extqh	t2, a1, t4	# U :

	or	t1, t4, t1	# E : first aligned src word complete
	mskqh	t1, a0, t1	# U : mask leading garbage in src
	or	t0, t1, t0	# E : first output word complete
	or	t0, t6, t6	# E : mask original data for zero test

	cmpbge	zero, t6, t8	# E :
	beq	a2, $u_eocfin	# U :
	bne	t8, $u_final	# U : bad news - 2nd branch in a quad
	lda	t6, -1		# E : mask out the bits we have

	mskql	t6, a1, t6	# U :   already seen
	stq_u	t0, 0(a0)	# L : store first output word
	or      t6, t2, t2	# E :
	cmpbge	zero, t2, t8	# E : find nulls in second partial

	addq	a0, 8, a0		# E :
	subq	a2, 1, a2		# E :
	bne	t8, $u_late_head_exit	# U :
	nop

	/* Finally, we've got all the stupid leading edge cases taken care
	   of and we can set up to enter the main loop.  */

	extql	t2, a1, t1	# U : position hi-bits of lo word
	EX( ldq_u t2, 8(a1) )	# L : read next high-order source word
	addq	a1, 8, a1	# E :
	cmpbge	zero, t2, t8	# E :

	beq	a2, $u_eoc	# U :
	bne	t8, $u_eos	# U :
	nop
	nop

	/* Unaligned copy main loop.  In order to avoid reading too much,
	   the loop is structured to detect zeros in aligned source words.
	   This has, unfortunately, effectively pulled half of a loop
	   iteration out into the head and half into the tail, but it does
	   prevent nastiness from accumulating in the very thing we want
	   to run as fast as possible.

	   On entry to this basic block:
	   t1 == the shifted high-order bits from the previous source