xref: /openbsd/sys/arch/arm/arm/copystr.S (revision 4bdff4be)
1/*	$OpenBSD: copystr.S,v 1.12 2023/01/31 15:18:54 deraadt 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	cpu_info
54#else
55.Lcpu_info_primary:
56	.word	cpu_info_primary
57#endif
58
59#define SAVE_REGS	stmfd	sp!, {r4-r6}
60#define RESTORE_REGS	ldmfd	sp!, {r4-r6}
61
62/*
63 * r0 - user space address
64 * r1 - kernel space address
65 * r2 - maxlens
66 * r3 - lencopied
67 *
68 * Copy string from user space to kernel space
69 */
70ENTRY(_copyinstr)
71	SAVE_REGS
72
73	teq	r2, #0x00000000
74	mov	r6, #0x00000000
75	moveq	r0, #ENAMETOOLONG
76	beq	2f
77
78	/* Get curcpu from TPIDRPRW. */
79	mrc	CP15_TPIDRPRW(r4)
80	ldr	r4, [r4, #CI_CURPCB]
81
82#ifdef DEBUG
83	teq	r4, #0x00000000
84	beq	.Lcopystrpcbfault
85#endif
86
87	adr	r5, .Lcopystrfault
88	str	r5, [r4, #PCB_ONFAULT]
89
901:	ldrbt	r5, [r0], #0x0001
91	add	r6, r6, #0x00000001
92	teq	r5, #0x00000000
93	strb	r5, [r1], #0x0001
94	teqne	r6, r2
95	bne	1b
96
97	mov	r0, #0x00000000
98	str	r0, [r4, #PCB_ONFAULT]
99
100	teq	r5, #0x00000000
101	moveq	r0, #0x00000000
102	movne	r0, #ENAMETOOLONG
103
1042:	teq	r3, #0x00000000
105	strne	r6, [r3]
106
107	RESTORE_REGS
108	mov	pc, lr
109
110/*
111 * r0 - kernel space address
112 * r1 - user space address
113 * r2 - maxlens
114 * r3 - lencopied
115 *
116 * Copy string from kernel space to user space
117 */
118ENTRY(copyoutstr)
119	SAVE_REGS
120
121	teq	r2, #0x00000000
122	mov	r6, #0x00000000
123	moveq	r0, #ENAMETOOLONG
124	beq	2f
125
126	/* Get curcpu from TPIDRPRW. */
127	mrc	CP15_TPIDRPRW(r4)
128	ldr	r4, [r4, #CI_CURPCB]
129
130#ifdef DEBUG
131	teq	r4, #0x00000000
132	beq	.Lcopystrpcbfault
133#endif
134
135	adr	r5, .Lcopystrfault
136	str	r5, [r4, #PCB_ONFAULT]
137
1381:	ldrb	r5, [r0], #0x0001
139	add	r6, r6, #0x00000001
140	teq	r5, #0x00000000
141	strbt	r5, [r1], #0x0001
142	teqne	r6, r2
143	bne	1b
144
145	mov	r0, #0x00000000
146	str	r0, [r4, #PCB_ONFAULT]
147
148	teq	r5, #0x00000000
149	moveq	r0, #0x00000000
150	movne	r0, #ENAMETOOLONG
151
1522:	teq	r3, #0x00000000
153	strne	r6, [r3]
154
155	RESTORE_REGS
156	mov	pc, lr
157
158/* A fault occurred during the copy */
159.Lcopystrfault:
160	mov	r1, #0x00000000
161	str	r1, [r4, #PCB_ONFAULT]
162	RESTORE_REGS
163	mov	pc, lr
164
165#ifdef DEBUG
166.Lcopystrpcbfault:
167	mov	r2, r1
168	mov	r1, r0
169	adr	r0, Lcopystrpcbfaulttext
170	bic	sp, sp, #7			/* align stack to 8 bytes */
171	b	panic
172
173Lcopystrpcbfaulttext:
174	.asciz	"No valid PCB during copyinoutstr() addr1=%08x addr2=%08x\n"
175	.align	2
176#endif
177