xref: /netbsd-src/sys/arch/hppa/hppa/copy.S (revision ca08b3e761e6fc5454aa5d028935f5d82d92ea3d)
1/*	$NetBSD: copy.S,v 1.25 2020/06/30 16:20:01 maxv 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/*	$OpenBSD: locore.S,v 1.46 2001/09/20 18:33:03 mickey Exp $	*/
33
34/*
35 * Copyright (c) 1998-2004 Michael Shalayeff
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
51 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
52 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
56 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
57 * THE POSSIBILITY OF SUCH DAMAGE.
58 *
59 * Portitions of this file are derived from other sources, see
60 * the copyrights and acknowledgements below.
61 */
62/*
63 * Copyright (c) 1990,1991,1992,1994 The University of Utah and
64 * the Computer Systems Laboratory (CSL).  All rights reserved.
65 *
66 * THE UNIVERSITY OF UTAH AND CSL PROVIDE THIS SOFTWARE IN ITS "AS IS"
67 * CONDITION, AND DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
68 * WHATSOEVER RESULTING FROM ITS USE.
69 *
70 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
71 * improvements that they make and grant CSL redistribution rights.
72 *
73 *	Utah $Hdr: locore.s 1.62 94/12/15$
74 */
75/*
76 *  (c) Copyright 1988 HEWLETT-PACKARD COMPANY
77 *
78 *  To anyone who acknowledges that this file is provided "AS IS"
79 *  without any express or implied warranty:
80 *      permission to use, copy, modify, and distribute this file
81 *  for any purpose is hereby granted without fee, provided that
82 *  the above copyright notice and this notice appears in all
83 *  copies, and that the name of Hewlett-Packard Company not be
84 *  used in advertising or publicity pertaining to distribution
85 *  of the software without specific, written prior permission.
86 *  Hewlett-Packard Company makes no representations about the
87 *  suitability of this software for any purpose.
88 */
89
90/*
91 * This file contains the functions for user-space access:
92 * copyin/copyout, ufetch/ustore, etc.
93 */
94
95	.align	NBPG	/* let's fit 'em on a single page */
96
97#define	FUSUX(name)				  \
98LEAF_ENTRY_NOPROFILE(name)			! \
99	ldil	L%VM_MAXUSER_ADDRESS, %t1	! \
100	comb,>>= %arg0, %t1, ufetchstore_efault	! \
101	GET_CURLWP(%t1)				! \
102	ldw	L_PCB(%t1), %t1			! \
103	ldil	L%ufetchstore_fault, %t2	! \
104	ldo	R%ufetchstore_fault(%t2), %t2	! \
105	ldw	PCB_ONFAULT(%t1), %t3		! \
106	stw	%t2, PCB_ONFAULT(%t1)		! \
107	ldw	PCB_SPACE(%t1), %t2		! \
108	mtsp	%t2, %sr1
109
110#define	FUX(name,linsn,sinsn)			  \
111	FUSUX(name)				! \
112	linsn	0(%sr1, %arg0), %t2		! \
113	stw	%r0, PCB_ONFAULT(%t1)		! \
114	sinsn	%t2, 0(%arg1)			! \
115	bv	%r0(%rp)			! \
116	 ldi	0, %ret0			! \
117EXIT(name)
118
119#define	SUX(name,insn)				  \
120	FUSUX(name)				! \
121	insn	%arg1, 0(%sr1, %arg0)		! \
122	stw	%r0, PCB_ONFAULT(%t1)		! \
123	bv	%r0(%rp)			! \
124	 ldi	0, %ret0			! \
125EXIT(name)
126
127LEAF_ENTRY_NOPROFILE(ufetchstore_fault)
128	bv	0(%rp)
129	 stw	%r0, PCB_ONFAULT(%t1)
130EXIT(ufetchstore_fault)
131
132LEAF_ENTRY_NOPROFILE(ufetchstore_efault)
133	bv	0(%rp)
134	 ldi	EFAULT, %ret0
135EXIT(ufetchstore_efault)
136
137FUX(_ufetch_8,  ldb, stb)
138FUX(_ufetch_16, ldh, sth)
139FUX(_ufetch_32, ldw, stw)
140
141SUX(_ustore_8,  stb)
142SUX(_ustore_16, sth)
143SUX(_ustore_32, stw)
144
145	.align	64
146
147/*
148 * int spstrcpy (pa_space_t ssp, const void *src, pa_space_t dsp, void *dst,
149 *     size_t size, size_t *rsize)
150 *
151 * Do a space to space size bounded string copy and return actual copy size in
152 * rsize. If the copy ran out of space before '\0' is seen return ENAMETOOLONG
153 *
154 */
155LEAF_ENTRY(spstrcpy)
156	/* setup fault handler */
157	GET_CURLWP(%r31)
158	ldw	L_PCB(%r31), %r31
159	ldil	L%L$spstrcpy_fault, %t2
160	ldo	R%L$spstrcpy_fault(%t2), %t2
161	stw	%t2, PCB_ONFAULT(%r31)
162
163	ldw	HPPA_FRAME_ARG(4)(%sp), %t2	/* size */
164	mtsp	%arg0, %sr1
165	mtsp	%arg2, %sr2
166	copy	%arg1, %arg0			/* save src */
167
168L$spstrcpy_loop:
169	comb,=,n %r0, %t2, L$spstrcpy_exit
170	ldbs,ma	1(%sr1, %arg1), %t1
171	stbs,ma	%t1, 1(%sr2, %arg3)
172	comb,<>	%r0, %t1, L$spstrcpy_loop
173	ldo	-1(%t2), %t2
174
175L$spstrcpy_exit:
176	comiclr,= 0, %t1, %ret0
177	ldi	ENAMETOOLONG,%ret0
178
179L$spstrcpy_fault:
180	/* reset fault handler */
181	stw	%r0, PCB_ONFAULT(%r31)
182	sub	%arg1, %arg0, %arg1
183	ldw	HPPA_FRAME_ARG(5)(%sp), %arg0		/* rsize */
184	comiclr,= 0, %arg0, %r0
185	stw	%arg1, 0(%arg0)
186	bv,n	0(%rp)
187EXIT(spstrcpy)
188
189/*
190 * This macro expands into one function that calls spcopy
191 * or strspcpy.  The function name is name, the copy function
192 * is copyfn, and the assembly to set up the space arguments
193 * is in spaceargs.
194 */
195#define _SPCOPY(name, fncall, spaceargs)	  \
196ENTRY(name, HPPA_FRAME_SIZE * 2)		! \
197						! \
198	/* Start stack calling convention. */	! \
199	stw	%rp, HPPA_FRAME_CRP(%sp)	! \
200	copy	%r3, %r1			! \
201	copy	%sp, %r3			! \
202	stw,ma	%r1, (HPPA_FRAME_SIZE * 2)(%sp)	! \
203						! \
204	/* 					! \
205	 * Set up all arguments for the copy	! \
206	 * function.  spcopy only takes five	! \
207	 * arguments, but the sixth argument	! \
208	 * we set up is harmless.		! \
209	 */					! \
210	stw	%arg3, HPPA_FRAME_ARG(5)(%sp)	! \
211	stw	%arg2, HPPA_FRAME_ARG(4)(%sp)	! \
212	copy	%arg1, %arg3			! \
213	copy	%arg0, %arg1			! \
214	spaceargs				! \
215						! \
216	/* Call the copy function. */		! \
217	fncall					! \
218						! \
219	/* End stack calling convention. */	! \
220	ldw	HPPA_FRAME_CRP(%r3), %rp	! \
221	ldo	HPPA_FRAME_SIZE(%r3), %sp	! \
222	ldw,mb	-HPPA_FRAME_SIZE(%sp), %r3	! \
223	bv,n	%r0(%rp)			! \
224EXIT(name)
225
226/* This loads curlwp's space into the given register. */
227#define SPACE_CURLWP(reg)			  \
228	GET_CURLWP(reg)				! \
229	ldw	L_PCB(reg), reg			! \
230	ldw	PCB_SPACE(reg), reg
231
232/* This loads the kernel's space into the given register. */
233#define SPACE_KERNEL(reg)			  \
234	ldi	HPPA_SID_KERNEL, reg
235
236/* This calls the spcopy function. */
237#define	CALL_SPCOPY				  \
238	ldil	L%spcopy, %r1			! \
239	ldo	R%spcopy(%r1), %r1		! \
240	blr	%r0, %rp			! \
241	bv,n	%r0(%r1)			! \
242	nop
243
244/* This calls the spstrcpy function. */
245#define CALL_SPSTRCPY				  \
246	bl,n	spstrcpy, %rp			! \
247	nop
248
249/*
250 * This function expands to one pair of copyin or copyout
251 * functions - one non-string copier and one string copier.
252 */
253#define SPCOPY(name, namestr, spaceargs)	  \
254	_SPCOPY(name, CALL_SPCOPY, spaceargs)	! \
255	_SPCOPY(namestr, CALL_SPSTRCPY, spaceargs)
256
257/*
258 * int kcopy(const void *src, void *dst, size_t len);
259 */
260_SPCOPY(kcopy, CALL_SPCOPY,
261	SPACE_KERNEL(%arg0) ! SPACE_KERNEL(%arg2))
262
263/*
264 * int copyin(const void *src, void *dst, size_t size);
265 * int copyinstr(const void *src, void *dst, size_t size, size_t *lenp);
266 */
267SPCOPY(copyin, copyinstr,
268	SPACE_CURLWP(%arg0) ! SPACE_KERNEL(%arg2))
269
270/*
271 * int copyout(const void *src, void *dst, size_t size);
272 * int copyoutstr(const void *src, void *dst, size_t size, size_t *lenp);
273 */
274SPCOPY(copyout, copyoutstr,
275	SPACE_KERNEL(%arg0) ! SPACE_CURLWP(%arg2))
276
277/*
278 * For lack of a better place to put them, these functions
279 * write the kernel text.  The kernel text is normally mapped
280 * read/execute, but since it is mapped directly, we can use
281 * absolute accesses on it.
282 */
283ENTRY(hppa_ktext_stw, HPPA_FRAME_SIZE)
284
285	/* Start stack calling convention. */
286	stw	%rp, HPPA_FRAME_CRP(%sp)
287	copy	%r3, %r1
288	copy	%sp, %r3
289	stw,ma	%r1, HPPA_FRAME_SIZE(%sp)
290
291	/* Do the store. */
292	stwas	%arg1, 0(%arg0)
293
294	/* Call fcacheall(). */
295	.import	fcacheall, code
296	ldil	L%fcacheall, %r1
297	ldo	R%fcacheall(%r1), %r1
298	blr	%r0, %rp
299	.call
300	bv,n	%r0(%r1)
301	nop
302
303	/* Add some nops for good measure. */
304	nop
305	nop
306	nop
307	nop
308	nop
309	nop
310	nop
311	nop
312
313	/* End stack calling convention. */
314	ldw	HPPA_FRAME_CRP(%r3), %rp
315	ldo	HPPA_FRAME_SIZE(%r3), %sp
316	ldw,mb	-HPPA_FRAME_SIZE(%sp), %r3
317	bv,n	%r0(%rp)
318EXIT(hppa_ktext_stw)
319
320ENTRY(hppa_ktext_stb, HPPA_FRAME_SIZE)
321
322	/*
323	 * Make the deposit location for the byte in the
324	 * aligned word.
325	 */
326	ldi     7, %t2
327	extru	%arg0, 31, 2, %t1
328	sh3add	%t1, %t2, %t1
329	mtsar	%t1
330
331	/* Load the aligned word and deposit the byte. */
332	dep	%r0, 31, 2, %arg0
333	ldwas	0(%arg0), %t2
334	vdep	%arg1, 8, %t2
335
336	/* Jump to hppa_ktext_stw to finish. */
337	b	hppa_ktext_stw
338	copy	%t2, %arg1
339EXIT(hppa_ktext_stb)
340