1/*	$NetBSD: copystr.S,v 1.8 2002/10/13 14:54:48 bjh21 Exp $	*/
2
3/*-
4 * Copyright (c) 1995 Mark Brinicombe.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Mark Brinicombe.
18 * 4. The name of the company nor the name of the author may be used to
19 *    endorse or promote products derived from this software without specific
20 *    prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * copystr.S
35 *
36 * optimised and fault protected copystr functions
37 *
38 * Created      : 16/05/95
39 */
40
41#include "assym.s"
42#include <machine/asm.h>
43#include <machine/armreg.h>
44__FBSDID("$FreeBSD$");
45
46#include <sys/errno.h>
47
48	.text
49	.align	2
50
51#if __ARM_ARCH >= 6
52#define GET_PCB(tmp) \
53	mrc p15, 0, tmp, c13, c0, 4; \
54	add	tmp, tmp, #(TD_PCB)
55#else
56.Lpcb:
57	.word	_C_LABEL(__pcpu) + PC_CURPCB
58
59#define GET_PCB(tmp) \
60	ldr	tmp, .Lpcb
61#endif
62
63/*
64 * r0 - from
65 * r1 - to
66 * r2 - maxlens
67 * r3 - lencopied
68 *
69 * Copy string from r0 to r1
70 */
71ENTRY(copystr)
72	stmfd	sp!, {r4-r5}			/* stack is 8 byte aligned */
73	teq	r2, #0x00000000
74	mov	r5, #0x00000000
75	moveq	r0, #ENAMETOOLONG
76	beq	2f
77
781:	ldrb	r4, [r0], #0x0001
79	add	r5, r5, #0x00000001
80	teq	r4, #0x00000000
81	strb	r4, [r1], #0x0001
82	teqne	r5, r2
83	bne	1b
84
85	teq	r4, #0x00000000
86	moveq	r0, #0x00000000
87	movne	r0, #ENAMETOOLONG
88
892:	teq	r3, #0x00000000
90	strne	r5, [r3]
91
92	ldmfd	sp!, {r4-r5}			/* stack is 8 byte aligned */
93	RET
94END(copystr)
95
96#define SAVE_REGS	stmfd	sp!, {r4-r6}
97#define RESTORE_REGS	ldmfd	sp!, {r4-r6}
98
99/*
100 * r0 - user space address
101 * r1 - kernel space address
102 * r2 - maxlens
103 * r3 - lencopied
104 *
105 * Copy string from user space to kernel space
106 */
107ENTRY(copyinstr)
108	SAVE_REGS
109
110	teq	r2, #0x00000000
111	mov	r6, #0x00000000
112	moveq	r0, #ENAMETOOLONG
113	beq	2f
114
115	ldr	r12, =VM_MAXUSER_ADDRESS
116
117	GET_PCB(r4)
118	ldr	r4, [r4]
119
120#ifdef DIAGNOSTIC
121	teq	r4, #0x00000000
122	beq	.Lcopystrpcbfault
123#endif
124
125	adr	r5, .Lcopystrfault
126	str	r5, [r4, #PCB_ONFAULT]
127
1281:
129	cmp	r0, r12
130	bcs	.Lcopystrfault
131	ldrbt	r5, [r0], #0x0001
132	add	r6, r6, #0x00000001
133	teq	r5, #0x00000000
134	strb	r5, [r1], #0x0001
135	teqne	r6, r2
136	bne	1b
137
138	mov	r0, #0x00000000
139	str	r0, [r4, #PCB_ONFAULT]
140
141	teq	r5, #0x00000000
142	moveq	r0, #0x00000000
143	movne	r0, #ENAMETOOLONG
144
1452:	teq	r3, #0x00000000
146	strne	r6, [r3]
147
148	RESTORE_REGS
149	RET
150END(copyinstr)
151
152/*
153 * r0 - kernel space address
154 * r1 - user space address
155 * r2 - maxlens
156 * r3 - lencopied
157 *
158 * Copy string from kernel space to user space
159 */
160ENTRY(copyoutstr)
161	SAVE_REGS
162
163	teq	r2, #0x00000000
164	mov	r6, #0x00000000
165	moveq	r0, #ENAMETOOLONG
166	beq	2f
167
168	ldr	r12, =VM_MAXUSER_ADDRESS
169
170	GET_PCB(r4)
171	ldr	r4, [r4]
172
173#ifdef DIAGNOSTIC
174	teq	r4, #0x00000000
175	beq	.Lcopystrpcbfault
176#endif
177
178	adr	r5, .Lcopystrfault
179	str	r5, [r4, #PCB_ONFAULT]
180
1811:
182	cmp	r0, r12
183	bcs	.Lcopystrfault
184	ldrb	r5, [r0], #0x0001
185	add	r6, r6, #0x00000001
186	teq	r5, #0x00000000
187	strbt	r5, [r1], #0x0001
188	teqne	r6, r2
189	bne	1b
190
191	mov	r0, #0x00000000
192	str	r0, [r4, #PCB_ONFAULT]
193
194	teq	r5, #0x00000000
195	moveq	r0, #0x00000000
196	movne	r0, #ENAMETOOLONG
197
1982:	teq	r3, #0x00000000
199	strne	r6, [r3]
200
201	RESTORE_REGS
202	RET
203END(copyoutstr)
204
205/* A fault occurred during the copy */
206.Lcopystrfault:
207	mov	r1, #0x00000000
208	str	r1, [r4, #PCB_ONFAULT]
209	mov	r0, #EFAULT
210	RESTORE_REGS
211	RET
212
213#ifdef DIAGNOSTIC
214.Lcopystrpcbfault:
215	mov	r2, r1
216	mov	r1, r0
217	adr	r0, Lcopystrpcbfaulttext
218	bic	sp, sp, #7			/* align stack to 8 bytes */
219	b	_C_LABEL(panic)
220
221Lcopystrpcbfaulttext:
222	.asciz	"No valid PCB during copyinoutstr() addr1=%08x addr2=%08x\n"
223	.align	2
224#endif
225