Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*	$NetBSD: bcopy.S,v 1.17 2021/08/09 19:57:58 andvar Exp $	*/

/*
 * Copyright (c) 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Matthew Fredette.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copy routines for NetBSD/hppa.
 */

#undef _LOCORE
#define _LOCORE	/* XXX fredette - unfortunate */

#if defined(SPCOPY) && !defined(_STANDALONE)

#include "opt_multiprocessor.h"

#include <machine/cpu.h>

#endif

#include <machine/asm.h>
#include <machine/frame.h>
#include <machine/reg.h>

#if defined(LIBC_SCCS) && !defined(lint)
RCSID("$NetBSD: bcopy.S,v 1.17 2021/08/09 19:57:58 andvar Exp $")
#endif /* LIBC_SCCS and not lint */

/*
 * The stbys instruction is a little asymmetric.  When (%r2 & 3)
 * is zero, stbys,b,m %r1, 4(%r2) works like stws,ma.  You
 * might then wish that when (%r2 & 3) == 0, stbys,e,m %r1, -4(%r2)
 * worked like stws,mb.  But it doesn't.
 *
 * This macro works around this problem.  It requires that %t2
 * hold the number of bytes that will be written by this store
 * (meaning that it ranges from one to four).
 *
 * Watch the delay-slot trickery here.  The comib is used to set
 * up which instruction, either the stws or the stbys, is run
 * in the delay slot of the b instruction.
 */
#define _STBYS_E_M(r, dst_spc, dst_off)				  \
	comib,<>	4, %t2, 4				! \
	b		4					! \
	stws,mb		r, -4(dst_spc, dst_off)			! \
	stbys,e,m	r, 0(dst_spc, dst_off)

/*
 * This macro does a bulk copy with no shifting.  cmplt and m are
 * the completer and displacement multiplier, respectively, for
 * the load and store instructions.
 */
#define _COPY(src_spc, src_off, dst_spc, dst_off, count, cmplt, m) \
								! \
	/*							! \
	 * Loop storing 16 bytes at a time.  Since count 	! \
	 * may be > INT_MAX, we have to be careful and		! \
	 * avoid comparisons that treat it as a signed 		! \
	 * quantity, until after this loop, when count		! \
	 * is guaranteed to be less than 16.			! \
	 */							! \
	comib,>>=,n	15, count, _LABEL(_skip16)		! \
.label _LABEL(_loop16)						! \
	addi		-16, count, count			! \
	ldws,cmplt	m*4(src_spc, src_off), %t1		! \
	ldws,cmplt	m*4(src_spc, src_off), %t2		! \
	ldws,cmplt	m*4(src_spc, src_off), %t3		! \
	ldws,cmplt	m*4(src_spc, src_off), %t4		! \
	stws,cmplt	%t1, m*4(dst_spc, dst_off)		! \
	stws,cmplt	%t2, m*4(dst_spc, dst_off)		! \
	stws,cmplt	%t3, m*4(dst_spc, dst_off)		! \
	comib,<<	15, count, _LABEL(_loop16)		! \
	stws,cmplt	%t4, m*4(dst_spc, dst_off)		! \
.label _LABEL(_skip16)						! \
								! \
	/* Loop storing 4 bytes at a time. */			! \
	addib,<,n	-4, count, _LABEL(_skip4)		! \
.label _LABEL(_loop4)						! \
	ldws,cmplt	m*4(src_spc, src_off), %t1		! \
	addib,>=	-4, count, _LABEL(_loop4)		! \
	stws,cmplt	%t1, m*4(dst_spc, dst_off)		! \
.label _LABEL(_skip4)						! \
	/* Restore the correct count. */			! \
	addi		4, count, count				! \
								! \
.label _LABEL(_do1)						! \
								! \
	/* Loop storing 1 byte at a time. */			! \
	addib,<,n	-1, count, _LABEL(_skip1)		! \
.label _LABEL(_loop1)						! \
	ldbs,cmplt	m*1(src_spc, src_off), %t1		! \
	addib,>=	-1, count, _LABEL(_loop1)		! \
	stbs,cmplt	%t1, m*1(dst_spc, dst_off)		! \
.label _LABEL(_skip1)						! \
	/* Restore the correct count. */			! \
	b		_LABEL(_done)				! \
	addi		1, count, count

/*
 * This macro is definitely strange.  It exists purely to
 * allow the _COPYS macro to be reused, but because it
 * requires this long attempt to explain it, I'm starting
 * to doubt the value of that.
 *
 * Part of the expansion of the _COPYS macro below are loops
 * that copy four words or one word at a time, performing shifts
 * to get data to line up correctly in the destination buffer.
 *
 * The _COPYS macro is used when copying backwards, as well
 * as forwards.  The 4-word loop always loads into %t1, %t2, %t3,
 * and %t4 in that order.  This means that when copying forward,
 * %t1 will have the word from the lowest address, and %t4 will
 * have the word from the highest address.  When copying
 * backwards, the opposite is true.
 *
 * The shift instructions need pairs of registers with adjacent
 * words, with the register containing the word from the lowest
 * address *always* coming first.  It is this asymmetry that
 * gives rise to this macro - depending on which direction
 * we're copying in, these ordered pairs are different.
 *
 * Fortunately, we can compute those register numbers at compile
 * time, and assemble them manually into a shift instruction.
 * That's what this macro does.
 *
 * This macro takes two arguments.  n ranges from 0 to 3 and
 * is the "shift number", i.e., n = 0 means we're doing the
 * shift for what will be the first store.
 *
 * m is the displacement multiplier from the _COPYS macro call.
 * This is 1 for a forward copy and -1 for a backwards copy.
 * So, the ((m + 1) / 2) term yields 0 for a backwards copy and
 * 1 for a forward copy, and the ((m - 1) / 2) term yields
 * 0 for a forward copy, and -1 for a backwards copy.
 * These terms are used to discriminate the register computations
 * below.
 *
 * When copying forward, then, the first register used with
 * the first vshd will be 19 + (3 - ((0 - 1) & 3)), or %t4,
 * which matches _COPYS' requirement that the word last loaded
 * be in %t4.  The first register used for the second vshd
 * will then "wrap" around to 19 + (3 - ((1 - 1) & 3)), or %t1.
 * And so on to %t2 and %t3.
 *
 * When copying forward, the second register used with the first
 * vshd will be (19 + (3 - ((n + 0) & 3)), or %t1.  It will
 * continue to be %t2, then %t3, and finally %t4.
 *
 * When copying backwards, the values for the first and second
 * register for each vshd are reversed from the forwards case.
 * (Symmetry reclaimed!)  Proving this is "left as an exercise
 * for the reader" (remember the different discriminating values!)
 */
#define _VSHD(n, m, t)						  \
	.word (0xd0000000					| \
	((19 + (3 - ((n - 1 * ((m + 1) / 2)) & 3))) << 16)	| \
	((19 + (3 - ((n + 1 * ((m - 1) / 2)) & 3))) << 21)	| \
	(t))

/*
 * This macro does a bulk copy with shifting.  cmplt and m are
 * the completer and displacement multiplier, respectively, for
 * the load and store instructions.  It is assumed that the
 * word last loaded is already in %t4.
 */
#define _COPYS(src_spc, src_off, dst_spc, dst_off, count, cmplt, m) \
								! \
	/*							! \
	 * Loop storing 16 bytes at a time.  Since count 	! \
	 * may be > INT_MAX, we have to be careful and		! \
	 * avoid comparisons that treat it as a signed 		! \
	 * quantity, until after this loop, when count		! \
	 * is guaranteed to be less than 16.			! \
	 */							! \
	comib,>>=,n	15, count, _LABEL(S_skip16)		! \
.label _LABEL(S_loop16)						! \
	addi		-16, count, count			! \
	ldws,cmplt	m*4(src_spc, src_off), %t1		! \
	ldws,cmplt	m*4(src_spc, src_off), %t2		! \
	ldws,cmplt	m*4(src_spc, src_off), %t3		! \
	_VSHD(0, m, 1)	/* vshd %t4, %t1, %r1 */		! \
	ldws,cmplt	m*4(src_spc, src_off), %t4		! \
	_VSHD(1, m, 22)	/* vshd %t1, %t2, %t1 */		! \
	_VSHD(2, m, 21)	/* vshd %t2, %t3, %t2 */		! \
	_VSHD(3, m, 20)	/* vshd %t3, %t4, %t3 */		! \
	stws,cmplt	%r1, m*4(dst_spc, dst_off)		! \
	stws,cmplt	%t1, m*4(dst_spc, dst_off)		! \
	stws,cmplt	%t2, m*4(dst_spc, dst_off)		! \
	comib,<<	15, count, _LABEL(S_loop16)		! \
	stws,cmplt	%t3, m*4(dst_spc, dst_off)		! \
.label _LABEL(S_skip16)						! \
								! \
	/* Loop storing 4 bytes at a time. */			! \
	addib,<,n	-4, count, _LABEL(S_skip4)		! \
.label _LABEL(S_loop4)						! \
	ldws,cmplt	m*4(src_spc, src_off), %t1		! \
	_VSHD(0, m, 1)	/* into %r1 (1) */			! \
	copy		%t1, %t4				! \
	addib,>=	-4, count, _LABEL(S_loop4)		! \
	stws,cmplt	%r1, m*4(dst_spc, dst_off)		! \
.label _LABEL(S_skip4)						! \
								! \
	/*							! \
 	 * We now need to "back up" src_off by the		! \
	 * number of bytes remaining in the FIFO		! \
	 * (i.e., the number of bytes remaining in %t4),	! \
	 * because (the correct) count still includes		! \
	 * these bytes, and we intent to keep it that		! \
	 * way, and finish with the single-byte copier.		! \
	 *							! \
	 * The number of bytes remaining in the FIFO is		! \
	 * related to the shift count, so recover it,		! \
	 * restoring the correct count at the same time.	! \
	 */							! \
	mfctl	%cr11, %t1					! \
	addi	4, count, count					! \
	shd	%r0, %t1, 3, %t1				! \
								! \
	/*							! \
	 * If we're copying forward, the shift count		! \
	 * is the number of bytes remaining in the		! \
	 * FIFO, and we want to subtract it from src_off.	! \
	 * If we're copying backwards, (4 - shift count)	! \
	 * is the number of bytes remaining in the FIFO,	! \
	 * and we want to add it to src_off.			! \
	 *							! \
	 * We observe that x + (4 - y) = x - (y - 4),		! \
	 * and introduce this instruction to add -4 when	! \
	 * m is -1, although this does mean one extra		! \
	 * instruction in the forward case.			! \
	 */							! \
	addi	4*((m - 1) / 2), %t1, %t1			! \
								! \
	/* Now branch to the byte-at-a-time loop. */		! \
	b	_LABEL(_do1)					! \
	sub	src_off, %t1, src_off

/*
 * This macro copies a region in the forward direction.
 */
#define _COPY_FORWARD(src_spc, src_off, dst_spc, dst_off, count)  \
								! \
	/*							! \
	 * Since in the shifting-left case we will		! \
	 * load 8 bytes before checking count, to		! \
	 * keep things simple, branch to the byte 		! \
	 * copier unless we're copying at least 8.		! \
	 */							! \
	comib,>>,n	8, count, _LABEL(_do1)			! \
								! \
	/*							! \
	 * Once we 4-byte align the source offset, 		! \
	 * figure out how many bytes from the region		! \
	 * will be in the first 4-byte word we read.		! \
	 * Ditto for writing the destination offset.		! \
	 */							! \
	extru		src_off, 31, 2, %t1			! \
	extru		dst_off, 31, 2, %t2			! \
	subi		4, %t1, %t1				! \
	subi		4, %t2, %t2				! \
								! \
	/*							! \
	 * Calculate the byte shift required.  A 		! \
	 * positive value means a source 4-byte word 		! \
	 * has to be shifted to the right to line up 		! \
	 * as a destination 4-byte word.			! \
	 */							! \
	sub		%t1, %t2, %t1				! \
								! \
	/* 4-byte align src_off. */				! \
	depi		0, 31, 2, src_off			! \
								! \
	/*							! \
	 * It's somewhat important to note that this		! \
	 * code thinks of count as "the number of bytes		! \
	 * that haven't been stored yet", as opposed to		! \
	 * "the number of bytes that haven't been copied	! \
	 * yet".  The distinction is subtle, but becomes	! \
	 * apparent at the end of the shifting code, where	! \
	 * we "back up" src_off to correspond to count,		! \
	 * as opposed to flushing the FIFO.			! \
	 *							! \
	 * We calculated above how many bytes our first		! \
	 * store will store, so update count now.		! \
	 *							! \
	 * If the shift is zero, strictly as an optimization	! \
	 * we use a copy loop that does no shifting.		! \
	 */							! \
	comb,<>		%r0, %t1, _LABEL(_shifting)		! \
	sub		count, %t2, count			! \
								! \
	/* Load and store the first word. */			! \
	ldws,ma		4(src_spc, src_off), %t4		! \
	stbys,b,m	%t4, 4(dst_spc, dst_off)		! \
								! \
	/* Do the rest of the copy. */				! \
	_COPY(src_spc,src_off,dst_spc,dst_off,count,ma,1)	! \
								! \
.label _LABEL(_shifting)					! \
								! \
	/*							! \
	 * If shift < 0, we need to shift words to the		! \
	 * left.  Since we can't do this directly, we		! \
	 * adjust the shift so it's a shift to the right	! \
	 * and load the first word into the high word of	! \
	 * the FIFO.  Otherwise, we load a zero into the	! \
	 * high word of the FIFO.				! \
	 */							! \
	comb,<=		%r0, %t1, _LABEL(_shiftingrt)		! \
	copy		%r0, %t3				! \
	addi		4, %t1, %t1				! \
	ldws,ma		4(src_spc, src_off), %t3		! \
.label _LABEL(_shiftingrt)					! \
								! \
	/*							! \
	 * Turn the shift byte count into a bit count,		! \
	 * load the next word, set the Shift Amount 		! \
	 * Register, and form and store the first word.		! \
	 */							! \
	sh3add		%t1, %r0, %t1				! \
	ldws,ma		4(src_spc, src_off), %t4		! \
	mtctl		%t1, %cr11				! \
	vshd		%t3, %t4, %r1				! \
	stbys,b,m	%r1, 4(dst_spc, dst_off)		! \
								! \
	/* Do the rest of the copy. */				! \
	_COPYS(src_spc,src_off,dst_spc,dst_off,count,ma,1)

/* This macro copies a region in the reverse direction. */
#define _COPY_REVERSE(src_spc, src_off, dst_spc, dst_off, count)  \
								! \
	/* Immediately add count to both offsets. */		! \
	add	src_off, count, src_off				! \
	add	dst_off, count, dst_off				! \
								! \
	/*							! \
	 * Since in the shifting-right case we 			! \
	 * will load 8 bytes before checking 			! \
	 * count, to keep things simple, branch 		! \
	 * to the byte copier unless we're 			! \
	 * copying at least 8 bytes.				! \
	 */							! \
	comib,>>,n	8, count, _LABEL(_do1)			! \
								! \
	/*							! \
	 * Once we 4-byte align the source offset, 		! \
	 * figure out how many bytes from the region		! \
	 * will be in the first 4-byte word we read.		! \
	 * Ditto for writing the destination offset.		! \
	 */							! \
	extru,<>	src_off, 31, 2, %t1			! \
	ldi		4, %t1					! \
	extru,<>	dst_off, 31, 2, %t2			! \
	ldi		4, %t2					! \
								! \
	/*							! \
	 * Calculate the byte shift required.  A 		! \
	 * positive value means a source 4-byte 		! \
	 * word has to be shifted to the right to 		! \
	 * line up as a destination 4-byte word.		! \
	 */							! \
	sub		%t2, %t1, %t1				! \
								! \
	/*							! \
	 * 4-byte align src_off, leaving it pointing 		! \
	 * to the 4-byte word *after* the next word 		! \
	 * we intend to load.					! \
	 *							! \
	 * It's somewhat important to note that this		! \
	 * code thinks of count as "the number of bytes		! \
	 * that haven't been stored yet", as opposed to		! \
	 * "the number of bytes that haven't been copied	! \
	 * yet".  The distinction is subtle, but becomes	! \
	 * apparent at the end of the shifting code, where	! \
	 * we "back up" src_off to correspond to count,		! \
	 * as opposed to flushing the FIFO.			! \
	 *							! \
	 * We calculated above how many bytes our first		! \
	 * store will store, so update count now.		! \
	 *							! \
	 * If the shift is zero, we use a copy loop that	! \
	 * does no shifting.  NB: unlike the forward case,	! \
	 * this is NOT strictly an optimization.  If the	! \
	 * SAR is zero the vshds do NOT do the right thing.	! \
	 * This is another asymmetry more or less the "fault"	! \
	 * of vshd.						! \
	 */							! \
	addi		3, src_off, src_off			! \
	sub		count, %t2, count			! \
	comb,<>		%r0, %t1, _LABEL(_shifting)		! \
	depi		0, 31, 2, src_off			! \
								! \
	/* Load and store the first word. */			! \
	ldws,mb		-4(src_spc, src_off), %t4		! \
	_STBYS_E_M(%t4, dst_spc, dst_off)			! \
								! \
	/* Do the rest of the copy. */				! \
	_COPY(src_spc,src_off,dst_spc,dst_off,count,mb,-1)	! \
								! \
.label _LABEL(_shifting)					! \
								! \
	/*							! \
	 * If shift < 0, we need to shift words to the		! \
	 * left.  Since we can't do this directly, we		! \
	 * adjust the shift so it's a shift to the right	! \
	 * and load a zero in to the low word of the FIFO.	! \
	 * Otherwise, we load the first word into the		! \
	 * low word of the FIFO.				! \
	 *							! \
	 * Note the nullification trickery here.  We 		! \
	 * assume that we're shifting to the left, and		! \
	 * load zero into the low word of the FIFO.  Then	! \
	 * we nullify the addi if we're shifting to the		! \
	 * right.  If the addi is not nullified, we are		! \
 	 * shifting to the left, so we nullify the load.	! \
	 * we branch if we're shifting to the 			! \
	 */							! \
	copy		%r0, %t3				! \
	comb,<=,n	%r0, %t1, 0				! \
	addi,tr		4, %t1, %t1				! \
	ldws,mb		-4(src_spc, src_off), %t3		! \
								! \
	/*							! \
	 * Turn the shift byte count into a bit count,		! \
	 * load the next word, set the Shift Amount 		! \
	 * Register, and form and store the first word.		! \
	 */							! \
	sh3add		%t1, %r0, %t1				! \
	ldws,mb		-4(src_spc, src_off), %t4		! \
	mtctl		%t1, %cr11				! \
	vshd		%t4, %t3, %r1				! \
	_STBYS_E_M(%r1, dst_spc, dst_off)			! \
								! \
	/* Do the rest of the copy. */				! \
	_COPYS(src_spc,src_off,dst_spc,dst_off,count,mb,-1)

/*
 * For paranoia, when things aren't going well, enable this
 * code to assemble byte-at-a-time-only copying.
 */
#if 1
#undef _COPY_FORWARD
#define _COPY_FORWARD(src_spc, src_off, dst_spc, dst_off, count)  \
	comb,=,n	%r0, count, _LABEL(_done)		! \
	ldbs,ma		1(src_spc, src_off), %r1		! \
	addib,<>	-1, count, -12				! \
	stbs,ma		%r1, 1(dst_spc, dst_off)		! \
	b,n		_LABEL(_done)
#undef _COPY_REVERSE
#define _COPY_REVERSE(src_spc, src_off, dst_spc, dst_off, count)  \
	comb,=		%r0, count, _LABEL(_done)		! \
	add		src_off, count, src_off			! \
	add		dst_off, count, dst_off			! \
	ldbs,mb		-1(src_spc, src_off), %r1		! \
	addib,<>	-1, count, -12				! \
	stbs,mb		%r1, -1(dst_spc, dst_off)		! \
	b,n		_LABEL(_done)
#endif

/*
 * If none of the following are defined, define BCOPY.
 */
#if !(defined(SPCOPY) || defined(MEMCPY) || defined(MEMMOVE))
#define BCOPY
#endif

#if defined(SPCOPY) && !defined(_STANDALONE)

#include <sys/errno.h>
#include "assym.h"

/*
 * int spcopy(pa_space_t ssp, const void *src, pa_space_t dsp, void *dst,
 * 	size_t len)
 *
 * We assume that the regions do not overlap.
 */
LEAF_ENTRY(spcopy)

        /*
	 * Setup the fault handler, which will fill in %ret0 if triggered.
	 */
	GET_CURLWP(%r31)
#ifdef	DIAGNOSTIC
	comb,<>,n %r0, %r31, Lspcopy_curlwp_ok
	ldil	L%panic, %r1
	ldil	L%Lspcopy_curlwp_bad, %arg0
	ldo	R%panic(%r1), %r1
	ldo	R%Lspcopy_curlwp_bad(%arg0), %arg0
	.call
	bv,n    %r0(%r1)
	nop
Lspcopy_curlwp_bad:
	.asciz	"spcopy: curlwp == NULL\n"
	.align	8
Lspcopy_curlwp_ok:
#endif /* DIAGNOSTIC */
	ldil    L%spcopy_fault, %r1
	ldw     L_PCB(%r31), %r31
	ldo     R%spcopy_fault(%r1), %r1
	stw     %r1, PCB_ONFAULT(%r31)

	/* Setup the space registers. */
	mfsp	%sr2, %ret1
	mtsp	%arg0, %sr1
	mtsp	%arg2, %sr2

	/* Get the len argument and do the copy. */
	ldw	HPPA_FRAME_ARG(4)(%sp), %arg0
#define	_LABEL(l) __CONCAT(spcopy,l)
	_COPY_FORWARD(%sr1,%arg1,%sr2,%arg3,%arg0)
_LABEL(_done):

	/* Return. */
	copy	%r0, %ret0
ALTENTRY(spcopy_fault)
	stw     %r0, PCB_ONFAULT(%r31)
	bv	%r0(%rp)
	mtsp	%ret1, %sr2
EXIT(spcopy)
#endif /* SPCOPY && !_STANDALONE */

#ifdef MEMCPY
/*
 * void *memcpy(void *restrict dst, const void *restrict src, size_t len);
 *
 * memcpy is specifically restricted to working on
 * non-overlapping regions, so we can just copy forward.
 */
LEAF_ENTRY(memcpy)
	copy	%arg0, %ret0
#define	_LABEL(l) __CONCAT(memcpy,l)
	_COPY_FORWARD(%sr0,%arg1,%sr0,%arg0,%arg2)
_LABEL(_done):
	bv,n	%r0(%rp)
	nop
EXIT(memcpy)
#endif /* MEMCPY */

#ifdef BCOPY
/*
 * void bcopy(const void *src, void *dst, size_t len);
 */
LEAF_ENTRY(bcopy)
	copy	%arg0, %r1
	copy	%arg1, %arg0
	copy	%r1, %arg1
	/* FALLTHROUGH */
#define _LABEL_F(l) __CONCAT(bcopy_F,l)
#define _LABEL_R(l) __CONCAT(bcopy_R,l)
#endif

#ifdef MEMMOVE
/*
 * void *memmove(void *dst, const void *src, size_t len);
 */
LEAF_ENTRY(memmove)
#define _LABEL_F(l) __CONCAT(memmove_F,l)
#define _LABEL_R(l) __CONCAT(memmove_R,l)
	copy	%arg0, %ret0
#endif /* MEMMOVE */

#if defined(BCOPY) || defined(MEMMOVE)

	/*
	 * If src >= dst or src + len <= dst, we copy
	 * forward, else we copy in reverse.
	 */
	add		%arg1, %arg2, %r1
	comb,>>=,n	%arg1, %arg0, 0
	comb,>>,n	%r1, %arg0, _LABEL_R(_go)

#define _LABEL _LABEL_F
	_COPY_FORWARD(%sr0,%arg1,%sr0,%arg0,%arg2)
#undef _LABEL

_LABEL_R(_go):
#define _LABEL _LABEL_R
	_COPY_REVERSE(%sr0,%arg1,%sr0,%arg0,%arg2)
#undef _LABEL

_LABEL_F(_done):
_LABEL_R(_done):
	bv,n	%r0(%rp)
	nop
#ifdef BCOPY
EXIT(bcopy)
#else
EXIT(memmove)
#endif
#endif /* BCOPY || MEMMOVE */