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