xref: /netbsd/sys/lib/libkern/arch/hppa/bcopy.S (revision 6550d01e)
1/*	$NetBSD: bcopy.S,v 1.14 2011/01/31 12:10:58 skrll Exp $	*/
2
3/*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthew Fredette.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Copy routines for NetBSD/hppa.
34 */
35
36#undef _LOCORE
37#define _LOCORE	/* XXX fredette - unfortunate */
38
39#if defined(SPCOPY) && !defined(_STANDALONE)
40
41#include "opt_multiprocessor.h"
42
43#include <machine/cpu.h>
44
45#endif
46
47#include <machine/asm.h>
48#include <machine/frame.h>
49#include <machine/reg.h>
50
51#if defined(LIBC_SCCS) && !defined(lint)
52RCSID("$NetBSD: bcopy.S,v 1.14 2011/01/31 12:10:58 skrll Exp $")
53#endif /* LIBC_SCCS and not lint */
54
55/*
56 * The stbys instruction is a little asymmetric.  When (%r2 & 3)
57 * is zero, stbys,b,m %r1, 4(%r2) works like stws,ma.  You
58 * might then wish that when (%r2 & 3) == 0, stbys,e,m %r1, -4(%r2)
59 * worked like stws,mb.  But it doesn't.
60 *
61 * This macro works around this problem.  It requires that %t2
62 * hold the number of bytes that will be written by this store
63 * (meaning that it ranges from one to four).
64 *
65 * Watch the delay-slot trickery here.  The comib is used to set
66 * up which instruction, either the stws or the stbys, is run
67 * in the delay slot of the b instruction.
68 */
69#define _STBYS_E_M(r, dst_spc, dst_off)				  \
70	comib,<>	4, %t2, 4				! \
71	b		4					! \
72	stws,mb		r, -4(dst_spc, dst_off)			! \
73	stbys,e,m	r, 0(dst_spc, dst_off)
74
75/*
76 * This macro does a bulk copy with no shifting.  cmplt and m are
77 * the completer and displacement multiplier, respectively, for
78 * the load and store instructions.
79 */
80#define _COPY(src_spc, src_off, dst_spc, dst_off, count, cmplt, m) \
81								! \
82	/*							! \
83	 * Loop storing 16 bytes at a time.  Since count 	! \
84	 * may be > INT_MAX, we have to be careful and		! \
85	 * avoid comparisons that treat it as a signed 		! \
86	 * quantity, until after this loop, when count		! \
87	 * is guaranteed to be less than 16.			! \
88	 */							! \
89	comib,>>=,n	15, count, _LABEL(_skip16)		! \
90.label _LABEL(_loop16)						! \
91	addi		-16, count, count			! \
92	ldws,cmplt	m*4(src_spc, src_off), %t1		! \
93	ldws,cmplt	m*4(src_spc, src_off), %t2		! \
94	ldws,cmplt	m*4(src_spc, src_off), %t3		! \
95	ldws,cmplt	m*4(src_spc, src_off), %t4		! \
96	stws,cmplt	%t1, m*4(dst_spc, dst_off)		! \
97	stws,cmplt	%t2, m*4(dst_spc, dst_off)		! \
98	stws,cmplt	%t3, m*4(dst_spc, dst_off)		! \
99	comib,<<	15, count, _LABEL(_loop16)		! \
100	stws,cmplt	%t4, m*4(dst_spc, dst_off)		! \
101.label _LABEL(_skip16)						! \
102								! \
103	/* Loop storing 4 bytes at a time. */			! \
104	addib,<,n	-4, count, _LABEL(_skip4)		! \
105.label _LABEL(_loop4)						! \
106	ldws,cmplt	m*4(src_spc, src_off), %t1		! \
107	addib,>=	-4, count, _LABEL(_loop4)		! \
108	stws,cmplt	%t1, m*4(dst_spc, dst_off)		! \
109.label _LABEL(_skip4)						! \
110	/* Restore the correct count. */			! \
111	addi		4, count, count				! \
112								! \
113.label _LABEL(_do1)						! \
114								! \
115	/* Loop storing 1 byte at a time. */			! \
116	addib,<,n	-1, count, _LABEL(_skip1)		! \
117.label _LABEL(_loop1)						! \
118	ldbs,cmplt	m*1(src_spc, src_off), %t1		! \
119	addib,>=	-1, count, _LABEL(_loop1)		! \
120	stbs,cmplt	%t1, m*1(dst_spc, dst_off)		! \
121.label _LABEL(_skip1)						! \
122	/* Restore the correct count. */			! \
123	b		_LABEL(_done)				! \
124	addi		1, count, count
125
126/*
127 * This macro is definitely strange.  It exists purely to
128 * allow the _COPYS macro to be reused, but because it
129 * requires this long attempt to explain it, I'm starting
130 * to doubt the value of that.
131 *
132 * Part of the expansion of the _COPYS macro below are loops
133 * that copy four words or one word at a time, performing shifts
134 * to get data to line up correctly in the destination buffer.
135 *
136 * The _COPYS macro is used when copying backwards, as well
137 * as forwards.  The 4-word loop always loads into %t1, %t2, %t3,
138 * and %t4 in that order.  This means that when copying forward,
139 * %t1 will have the word from the lowest address, and %t4 will
140 * have the word from the highest address.  When copying
141 * backwards, the opposite is true.
142 *
143 * The shift instructions need pairs of registers with adjacent
144 * words, with the register containing the word from the lowest
145 * address *always* coming first.  It is this assymetry that
146 * gives rise to this macro - depending on which direction
147 * we're copying in, these ordered pairs are different.
148 *
149 * Fortunately, we can compute those register numbers at compile
150 * time, and assemble them manually into a shift instruction.
151 * That's what this macro does.
152 *
153 * This macro takes two arguments.  n ranges from 0 to 3 and
154 * is the "shift number", i.e., n = 0 means we're doing the
155 * shift for what will be the first store.
156 *
157 * m is the displacement multiplier from the _COPYS macro call.
158 * This is 1 for a forward copy and -1 for a backwards copy.
159 * So, the ((m + 1) / 2) term yields 0 for a backwards copy and
160 * 1 for a forward copy, and the ((m - 1) / 2) term yields
161 * 0 for a forward copy, and -1 for a backwards copy.
162 * These terms are used to discriminate the register computations
163 * below.
164 *
165 * When copying forward, then, the first register used with
166 * the first vshd will be 19 + (3 - ((0 - 1) & 3)), or %t4,
167 * which matches _COPYS' requirement that the word last loaded
168 * be in %t4.  The first register used for the second vshd
169 * will then "wrap" around to 19 + (3 - ((1 - 1) & 3)), or %t1.
170 * And so on to %t2 and %t3.
171 *
172 * When copying forward, the second register used with the first
173 * vshd will be (19 + (3 - ((n + 0) & 3)), or %t1.  It will
174 * continue to be %t2, then %t3, and finally %t4.
175 *
176 * When copying backwards, the values for the first and second
177 * register for each vshd are reversed from the forwards case.
178 * (Symmetry reclaimed!)  Proving this is "left as an exercise
179 * for the reader" (remember the different discriminating values!)
180 */
181#define _VSHD(n, m, t)						  \
182	.word (0xd0000000					| \
183	((19 + (3 - ((n - 1 * ((m + 1) / 2)) & 3))) << 16)	| \
184	((19 + (3 - ((n + 1 * ((m - 1) / 2)) & 3))) << 21)	| \
185	(t))
186
187/*
188 * This macro does a bulk copy with shifting.  cmplt and m are
189 * the completer and displacement multiplier, respectively, for
190 * the load and store instructions.  It is assumed that the
191 * word last loaded is already in %t4.
192 */
193#define _COPYS(src_spc, src_off, dst_spc, dst_off, count, cmplt, m) \
194								! \
195	/*							! \
196	 * Loop storing 16 bytes at a time.  Since count 	! \
197	 * may be > INT_MAX, we have to be careful and		! \
198	 * avoid comparisons that treat it as a signed 		! \
199	 * quantity, until after this loop, when count		! \
200	 * is guaranteed to be less than 16.			! \
201	 */							! \
202	comib,>>=,n	15, count, _LABEL(S_skip16)		! \
203.label _LABEL(S_loop16)						! \
204	addi		-16, count, count			! \
205	ldws,cmplt	m*4(src_spc, src_off), %t1		! \
206	ldws,cmplt	m*4(src_spc, src_off), %t2		! \
207	ldws,cmplt	m*4(src_spc, src_off), %t3		! \
208	_VSHD(0, m, 1)	/* vshd %t4, %t1, %r1 */		! \
209	ldws,cmplt	m*4(src_spc, src_off), %t4		! \
210	_VSHD(1, m, 22)	/* vshd %t1, %t2, %t1 */		! \
211	_VSHD(2, m, 21)	/* vshd %t2, %t3, %t2 */		! \
212	_VSHD(3, m, 20)	/* vshd %t3, %t4, %t3 */		! \
213	stws,cmplt	%r1, m*4(dst_spc, dst_off)		! \
214	stws,cmplt	%t1, m*4(dst_spc, dst_off)		! \
215	stws,cmplt	%t2, m*4(dst_spc, dst_off)		! \
216	comib,<<	15, count, _LABEL(S_loop16)		! \
217	stws,cmplt	%t3, m*4(dst_spc, dst_off)		! \
218.label _LABEL(S_skip16)						! \
219								! \
220	/* Loop storing 4 bytes at a time. */			! \
221	addib,<,n	-4, count, _LABEL(S_skip4)		! \
222.label _LABEL(S_loop4)						! \
223	ldws,cmplt	m*4(src_spc, src_off), %t1		! \
224	_VSHD(0, m, 1)	/* into %r1 (1) */			! \
225	copy		%t1, %t4				! \
226	addib,>=	-4, count, _LABEL(S_loop4)		! \
227	stws,cmplt	%r1, m*4(dst_spc, dst_off)		! \
228.label _LABEL(S_skip4)						! \
229								! \
230	/*							! \
231 	 * We now need to "back up" src_off by the		! \
232	 * number of bytes remaining in the FIFO		! \
233	 * (i.e., the number of bytes remaining in %t4),	! \
234	 * because (the correct) count still includes		! \
235	 * these bytes, and we intent to keep it that		! \
236	 * way, and finish with the single-byte copier.		! \
237	 *							! \
238	 * The number of bytes remaining in the FIFO is		! \
239	 * related to the shift count, so recover it,		! \
240	 * restoring the correct count at the same time.	! \
241	 */							! \
242	mfctl	%cr11, %t1					! \
243	addi	4, count, count					! \
244	shd	%r0, %t1, 3, %t1				! \
245								! \
246	/*							! \
247	 * If we're copying forward, the shift count		! \
248	 * is the number of bytes remaining in the		! \
249	 * FIFO, and we want to subtract it from src_off.	! \
250	 * If we're copying backwards, (4 - shift count)	! \
251	 * is the number of bytes remaining in the FIFO,	! \
252	 * and we want to add it to src_off.			! \
253	 *							! \
254	 * We observe that x + (4 - y) = x - (y - 4),		! \
255	 * and introduce this instruction to add -4 when	! \
256	 * m is -1, although this does mean one extra		! \
257	 * instruction in the forward case.			! \
258	 */							! \
259	addi	4*((m - 1) / 2), %t1, %t1			! \
260								! \
261	/* Now branch to the byte-at-a-time loop. */		! \
262	b	_LABEL(_do1)					! \
263	sub	src_off, %t1, src_off
264
265/*
266 * This macro copies a region in the forward direction.
267 */
268#define _COPY_FORWARD(src_spc, src_off, dst_spc, dst_off, count)  \
269								! \
270	/*							! \
271	 * Since in the shifting-left case we will		! \
272	 * load 8 bytes before checking count, to		! \
273	 * keep things simple, branch to the byte 		! \
274	 * copier unless we're copying at least 8.		! \
275	 */							! \
276	comib,>>,n	8, count, _LABEL(_do1)			! \
277								! \
278	/*							! \
279	 * Once we 4-byte align the source offset, 		! \
280	 * figure out how many bytes from the region		! \
281	 * will be in the first 4-byte word we read.		! \
282	 * Ditto for writing the destination offset.		! \
283	 */							! \
284	extru		src_off, 31, 2, %t1			! \
285	extru		dst_off, 31, 2, %t2			! \
286	subi		4, %t1, %t1				! \
287	subi		4, %t2, %t2				! \
288								! \
289	/*							! \
290	 * Calculate the byte shift required.  A 		! \
291	 * positive value means a source 4-byte word 		! \
292	 * has to be shifted to the right to line up 		! \
293	 * as a destination 4-byte word.			! \
294	 */							! \
295	sub		%t1, %t2, %t1				! \
296								! \
297	/* 4-byte align src_off. */				! \
298	depi		0, 31, 2, src_off			! \
299								! \
300	/*							! \
301	 * It's somewhat important to note that this		! \
302	 * code thinks of count as "the number of bytes		! \
303	 * that haven't been stored yet", as opposed to		! \
304	 * "the number of bytes that haven't been copied	! \
305	 * yet".  The distinction is subtle, but becomes	! \
306	 * apparent at the end of the shifting code, where	! \
307	 * we "back up" src_off to correspond to count,		! \
308	 * as opposed to flushing the FIFO.			! \
309	 *							! \
310	 * We calculated above how many bytes our first		! \
311	 * store will store, so update count now.		! \
312	 *							! \
313	 * If the shift is zero, strictly as an optimization	! \
314	 * we use a copy loop that does no shifting.		! \
315	 */							! \
316	comb,<>		%r0, %t1, _LABEL(_shifting)		! \
317	sub		count, %t2, count			! \
318								! \
319	/* Load and store the first word. */			! \
320	ldws,ma		4(src_spc, src_off), %t4		! \
321	stbys,b,m	%t4, 4(dst_spc, dst_off)		! \
322								! \
323	/* Do the rest of the copy. */				! \
324	_COPY(src_spc,src_off,dst_spc,dst_off,count,ma,1)	! \
325								! \
326.label _LABEL(_shifting)					! \
327								! \
328	/*							! \
329	 * If shift < 0, we need to shift words to the		! \
330	 * left.  Since we can't do this directly, we		! \
331	 * adjust the shift so it's a shift to the right	! \
332	 * and load the first word into the high word of	! \
333	 * the FIFO.  Otherwise, we load a zero into the	! \
334	 * high word of the FIFO.				! \
335	 */							! \
336	comb,<=		%r0, %t1, _LABEL(_shiftingrt)		! \
337	copy		%r0, %t3				! \
338	addi		4, %t1, %t1				! \
339	ldws,ma		4(src_spc, src_off), %t3		! \
340.label _LABEL(_shiftingrt)					! \
341								! \
342	/*							! \
343	 * Turn the shift byte count into a bit count,		! \
344	 * load the next word, set the Shift Amount 		! \
345	 * Register, and form and store the first word.		! \
346	 */							! \
347	sh3add		%t1, %r0, %t1				! \
348	ldws,ma		4(src_spc, src_off), %t4		! \
349	mtctl		%t1, %cr11				! \
350	vshd		%t3, %t4, %r1				! \
351	stbys,b,m	%r1, 4(dst_spc, dst_off)		! \
352								! \
353	/* Do the rest of the copy. */				! \
354	_COPYS(src_spc,src_off,dst_spc,dst_off,count,ma,1)
355
356/* This macro copies a region in the reverse direction. */
357#define _COPY_REVERSE(src_spc, src_off, dst_spc, dst_off, count)  \
358								! \
359	/* Immediately add count to both offsets. */		! \
360	add	src_off, count, src_off				! \
361	add	dst_off, count, dst_off				! \
362								! \
363	/*							! \
364	 * Since in the shifting-right case we 			! \
365	 * will load 8 bytes before checking 			! \
366	 * count, to keep things simple, branch 		! \
367	 * to the byte copier unless we're 			! \
368	 * copying at least 8 bytes.				! \
369	 */							! \
370	comib,>>,n	8, count, _LABEL(_do1)			! \
371								! \
372	/*							! \
373	 * Once we 4-byte align the source offset, 		! \
374	 * figure out how many bytes from the region		! \
375	 * will be in the first 4-byte word we read.		! \
376	 * Ditto for writing the destination offset.		! \
377	 */							! \
378	extru,<>	src_off, 31, 2, %t1			! \
379	ldi		4, %t1					! \
380	extru,<>	dst_off, 31, 2, %t2			! \
381	ldi		4, %t2					! \
382								! \
383	/*							! \
384	 * Calculate the byte shift required.  A 		! \
385	 * positive value means a source 4-byte 		! \
386	 * word has to be shifted to the right to 		! \
387	 * line up as a destination 4-byte word.		! \
388	 */							! \
389	sub		%t2, %t1, %t1				! \
390								! \
391	/*							! \
392	 * 4-byte align src_off, leaving it pointing 		! \
393	 * to the 4-byte word *after* the next word 		! \
394	 * we intend to load.					! \
395	 *							! \
396	 * It's somewhat important to note that this		! \
397	 * code thinks of count as "the number of bytes		! \
398	 * that haven't been stored yet", as opposed to		! \
399	 * "the number of bytes that haven't been copied	! \
400	 * yet".  The distinction is subtle, but becomes	! \
401	 * apparent at the end of the shifting code, where	! \
402	 * we "back up" src_off to correspond to count,		! \
403	 * as opposed to flushing the FIFO.			! \
404	 *							! \
405	 * We calculated above how many bytes our first		! \
406	 * store will store, so update count now.		! \
407	 *							! \
408	 * If the shift is zero, we use a copy loop that	! \
409	 * does no shifting.  NB: unlike the forward case,	! \
410	 * this is NOT strictly an optimization.  If the	! \
411	 * SAR is zero the vshds do NOT do the right thing.	! \
412	 * This is another assymetry more or less the "fault"	! \
413	 * of vshd.						! \
414	 */							! \
415	addi		3, src_off, src_off			! \
416	sub		count, %t2, count			! \
417	comb,<>		%r0, %t1, _LABEL(_shifting)		! \
418	depi		0, 31, 2, src_off			! \
419								! \
420	/* Load and store the first word. */			! \
421	ldws,mb		-4(src_spc, src_off), %t4		! \
422	_STBYS_E_M(%t4, dst_spc, dst_off)			! \
423								! \
424	/* Do the rest of the copy. */				! \
425	_COPY(src_spc,src_off,dst_spc,dst_off,count,mb,-1)	! \
426								! \
427.label _LABEL(_shifting)					! \
428								! \
429	/*							! \
430	 * If shift < 0, we need to shift words to the		! \
431	 * left.  Since we can't do this directly, we		! \
432	 * adjust the shift so it's a shift to the right	! \
433	 * and load a zero in to the low word of the FIFO.	! \
434	 * Otherwise, we load the first word into the		! \
435	 * low word of the FIFO.				! \
436	 *							! \
437	 * Note the nullification trickery here.  We 		! \
438	 * assume that we're shifting to the left, and		! \
439	 * load zero into the low word of the FIFO.  Then	! \
440	 * we nullify the addi if we're shifting to the		! \
441	 * right.  If the addi is not nullified, we are		! \
442 	 * shifting to the left, so we nullify the load.	! \
443	 * we branch if we're shifting to the 			! \
444	 */							! \
445	copy		%r0, %t3				! \
446	comb,<=,n	%r0, %t1, 0				! \
447	addi,tr		4, %t1, %t1				! \
448	ldws,mb		-4(src_spc, src_off), %t3		! \
449								! \
450	/*							! \
451	 * Turn the shift byte count into a bit count,		! \
452	 * load the next word, set the Shift Amount 		! \
453	 * Register, and form and store the first word.		! \
454	 */							! \
455	sh3add		%t1, %r0, %t1				! \
456	ldws,mb		-4(src_spc, src_off), %t4		! \
457	mtctl		%t1, %cr11				! \
458	vshd		%t4, %t3, %r1				! \
459	_STBYS_E_M(%r1, dst_spc, dst_off)			! \
460								! \
461	/* Do the rest of the copy. */				! \
462	_COPYS(src_spc,src_off,dst_spc,dst_off,count,mb,-1)
463
464/*
465 * For paranoia, when things aren't going well, enable this
466 * code to assemble byte-at-a-time-only copying.
467 */
468#if 1
469#undef _COPY_FORWARD
470#define _COPY_FORWARD(src_spc, src_off, dst_spc, dst_off, count)  \
471	comb,=,n	%r0, count, _LABEL(_done)		! \
472	ldbs,ma		1(src_spc, src_off), %r1		! \
473	addib,<>	-1, count, -12				! \
474	stbs,ma		%r1, 1(dst_spc, dst_off)		! \
475	b,n		_LABEL(_done)
476#undef _COPY_REVERSE
477#define _COPY_REVERSE(src_spc, src_off, dst_spc, dst_off, count)  \
478	comb,=		%r0, count, _LABEL(_done)		! \
479	add		src_off, count, src_off			! \
480	add		dst_off, count, dst_off			! \
481	ldbs,mb		-1(src_spc, src_off), %r1		! \
482	addib,<>	-1, count, -12				! \
483	stbs,mb		%r1, -1(dst_spc, dst_off)		! \
484	b,n		_LABEL(_done)
485#endif
486
487/*
488 * If none of the following are defined, define BCOPY.
489 */
490#if !(defined(SPCOPY) || defined(MEMCPY) || defined(MEMMOVE))
491#define BCOPY
492#endif
493
494#if defined(SPCOPY) && !defined(_STANDALONE)
495
496#include <sys/errno.h>
497#include "assym.h"
498
499/*
500 * int spcopy(pa_space_t ssp, const void *src, pa_space_t dsp, void *dst,
501 * 	size_t len)
502 *
503 * We assume that the regions do not overlap.
504 */
505LEAF_ENTRY(spcopy)
506
507        /*
508	 * Setup the fault handler, which will fill in %ret0 if triggered.
509	 */
510	GET_CURLWP(%r31)
511#ifdef	DIAGNOSTIC
512	comb,<>,n %r0, %r31, Lspcopy_curlwp_ok
513	ldil	L%panic, %r1
514	ldil	L%Lspcopy_curlwp_bad, %arg0
515	ldo	R%panic(%r1), %r1
516	ldo	R%Lspcopy_curlwp_bad(%arg0), %arg0
517	.call
518	bv,n    %r0(%r1)
519	nop
520Lspcopy_curlwp_bad:
521	.asciz	"spcopy: curlwp == NULL\n"
522	.align	8
523Lspcopy_curlwp_ok:
524#endif /* DIAGNOSTIC */
525	ldil    L%spcopy_fault, %r1
526	ldw     L_PCB(%r31), %r31
527	ldo     R%spcopy_fault(%r1), %r1
528	stw     %r1, PCB_ONFAULT(%r31)
529
530	/* Setup the space registers. */
531	mfsp	%sr2, %ret1
532	mtsp	%arg0, %sr1
533	mtsp	%arg2, %sr2
534
535	/* Get the len argument and do the copy. */
536	ldw	HPPA_FRAME_ARG(4)(%sp), %arg0
537#define	_LABEL(l) __CONCAT(spcopy,l)
538	_COPY_FORWARD(%sr1,%arg1,%sr2,%arg3,%arg0)
539_LABEL(_done):
540
541	/* Return. */
542	copy	%r0, %ret0
543ALTENTRY(spcopy_fault)
544	stw     %r0, PCB_ONFAULT(%r31)
545	bv	%r0(%rp)
546	mtsp	%ret1, %sr2
547EXIT(spcopy)
548#endif /* SPCOPY && !_STANDALONE */
549
550#ifdef MEMCPY
551/*
552 * void *memcpy(void *restrict dst, const void *restrict src, size_t len);
553 *
554 * memcpy is specifically restricted to working on
555 * non-overlapping regions, so we can just copy forward.
556 */
557LEAF_ENTRY(memcpy)
558	copy	%arg0, %ret0
559#define	_LABEL(l) __CONCAT(memcpy,l)
560	_COPY_FORWARD(%sr0,%arg1,%sr0,%arg0,%arg2)
561_LABEL(_done):
562	bv,n	%r0(%rp)
563	nop
564EXIT(memcpy)
565#endif /* MEMCPY */
566
567#ifdef BCOPY
568/*
569 * void bcopy(const void *src, void *dst, size_t len);
570 */
571LEAF_ENTRY(bcopy)
572	copy	%arg0, %r1
573	copy	%arg1, %arg0
574	copy	%r1, %arg1
575	/* FALLTHROUGH */
576#define _LABEL_F(l) __CONCAT(bcopy_F,l)
577#define _LABEL_R(l) __CONCAT(bcopy_R,l)
578#endif
579
580#ifdef MEMMOVE
581/*
582 * void *memmove(void *dst, const void *src, size_t len);
583 */
584LEAF_ENTRY(memmove)
585#define _LABEL_F(l) __CONCAT(memmove_F,l)
586#define _LABEL_R(l) __CONCAT(memmove_R,l)
587	copy	%arg0, %ret0
588#endif /* MEMMOVE */
589
590#if defined(BCOPY) || defined(MEMMOVE)
591
592	/*
593	 * If src >= dst or src + len <= dst, we copy
594	 * forward, else we copy in reverse.
595	 */
596	add		%arg1, %arg2, %r1
597	comb,>>=,n	%arg1, %arg0, 0
598	comb,>>,n	%r1, %arg0, _LABEL_R(_go)
599
600#define _LABEL _LABEL_F
601	_COPY_FORWARD(%sr0,%arg1,%sr0,%arg0,%arg2)
602#undef _LABEL
603
604_LABEL_R(_go):
605#define _LABEL _LABEL_R
606	_COPY_REVERSE(%sr0,%arg1,%sr0,%arg0,%arg2)
607#undef _LABEL
608
609_LABEL_F(_done):
610_LABEL_R(_done):
611	bv,n	%r0(%rp)
612	nop
613#ifdef BCOPY
614EXIT(bcopy)
615#else
616EXIT(memmove)
617#endif
618#endif /* BCOPY || MEMMOVE */
619