xref: /netbsd-src/sys/arch/arm/arm/copystr.S (revision beb9bdb00e5421761976d5c277c0da84fd703f9b)
1/*	$NetBSD: copystr.S,v 1.14 2022/10/20 06:58:38 skrll 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 "opt_multiprocessor.h"
42#include "opt_cpuoptions.h"
43
44#include "assym.h"
45
46#include <machine/asm.h>
47
48#include <arm/locore.h>
49
50RCSID("$NetBSD: copystr.S,v 1.14 2022/10/20 06:58:38 skrll Exp $")
51
52#include <sys/errno.h>
53
54	.text
55	.align	0
56
57#define SAVE_REGS	push	{r3-r6}
58#define RESTORE_REGS	pop	{r3-r6}
59
60/*
61 * r0 - user space address
62 * r1 - kernel space address
63 * r2 - maxlens
64 * r3 - lencopied
65 *
66 * Copy string from user space to kernel space
67 */
68ENTRY(copyinstr)
69	SAVE_REGS
70
71	teq	r2, #0x00000000
72	mov	r6, #0x00000000
73	moveq	r0, #ENAMETOOLONG
74	beq	2f
75
76	GET_CURPCB(r4)
77
78#ifdef DIAGNOSTIC
79	teq	r4, #0x00000000
80	beq	.Lcopystrpcbfault
81#endif
82
83	adr	r5, .Lcopystrfault
84	str	r5, [r4, #PCB_ONFAULT]
85
861:	ldrbt	r5, [r0], #0x0001
87	add	r6, r6, #0x00000001
88	teq	r5, #0x00000000
89	strb	r5, [r1], #0x0001
90	teqne	r6, r2
91	bne	1b
92
93	mov	r0, #0x00000000
94	str	r0, [r4, #PCB_ONFAULT]
95
96	teq	r5, #0x00000000
97	moveq	r0, #0x00000000
98	movne	r0, #ENAMETOOLONG
99
1002:	teq	r3, #0x00000000
101	strne	r6, [r3]
102
103	RESTORE_REGS
104	RET
105END(copyinstr)
106
107/*
108 * r0 - kernel space address
109 * r1 - user space address
110 * r2 - maxlens
111 * r3 - lencopied
112 *
113 * Copy string from kernel space to user space
114 */
115ENTRY(copyoutstr)
116	SAVE_REGS
117
118	teq	r2, #0x00000000
119	mov	r6, #0x00000000
120	moveq	r0, #ENAMETOOLONG
121	beq	2f
122
123	GET_CURPCB(r4)
124
125#ifdef DIAGNOSTIC
126	teq	r4, #0x00000000
127	beq	.Lcopystrpcbfault
128#endif
129
130	adr	r5, .Lcopystrfault
131	str	r5, [r4, #PCB_ONFAULT]
132
1331:	ldrb	r5, [r0], #0x0001
134	add	r6, r6, #0x00000001
135	teq	r5, #0x00000000
136	strbt	r5, [r1], #0x0001
137	teqne	r6, r2
138	bne	1b
139
140	mov	r0, #0x00000000
141	str	r0, [r4, #PCB_ONFAULT]
142
143	teq	r5, #0x00000000
144	moveq	r0, #0x00000000
145	movne	r0, #ENAMETOOLONG
146
1472:	teq	r3, #0x00000000
148	strne	r6, [r3]
149
150	RESTORE_REGS
151	RET
152
153/* A fault occurred during the copy */
154.Lcopystrfault:
155	mov	r1, #0x00000000
156	str	r1, [r4, #PCB_ONFAULT]
157	RESTORE_REGS
158	RET
159
160#ifdef DIAGNOSTIC
161.Lcopystrpcbfault:
162	mov	r2, r1
163	mov	r1, r0
164	adr	r0, .Lcopystrpcbfaulttext
165	bic	sp, sp, #7			/* align stack to 8 bytes */
166	b	_C_LABEL(panic)
167
168.Lcopystrpcbfaulttext:
169	.asciz	"No valid PCB during copyinoutstr() addr1=%08x addr2=%08x\n"
170	.align	0
171#endif
172END(copyoutstr)
173