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