1 /* $NetBSD: bcopyinout.S,v 1.11 2003/10/13 21:22:40 scw Exp $ */
4 * Copyright (c) 2002 Wasabi Systems, Inc.
7 * Written by Allen Briggs for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
41 #include <machine/asm.h>
42 #include <sys/errno.h>
45 .word _C_LABEL(_arm_memcpy)
47 .word _C_LABEL(_min_memcpy_size)
49 __FBSDID("$FreeBSD$");
51 #include <arm/arm/bcopyinout_xscale.S>
57 #define GET_PCB(tmp) \
58 mrc p15, 0, tmp, c13, c0, 4; \
59 add tmp, tmp, #(TD_PCB)
61 #define SAVE_REGS stmfd sp!, {r4-r11}; _SAVE({r4-r11})
62 #define RESTORE_REGS ldmfd sp!, {r4-r11}
64 #if defined(_ARM_ARCH_5E)
66 #define PREFETCH(rx,o) pld [ rx , HELLOCPP (o) ]
68 #define PREFETCH(rx,o)
72 * r0 = user space address
73 * r1 = kernel space address
76 * Copies bytes from user space to kernel space
78 * We save/restore r4-r11:
82 /* Quick exit if length is zero */
91 ldr r12, =(VM_MAXUSER_ADDRESS + 1)
100 ldr r3, .L_min_memcpy_size
104 stmfd sp!, {r0-r2, r4, lr}
108 mov r3, #2 /* SRC_IS_USER */
109 ldr r4, .L_arm_memcpy
113 ldmfd sp!, {r0-r2, r4, lr}
123 ldr r5, [r4, #PCB_ONFAULT]
125 str r3, [r4, #PCB_ONFAULT]
131 * If not too many bytes, take the slow path.
137 * Align destination to word boundary.
140 ldr pc, [pc, r6, lsl #2]
146 .Lial3: ldrbt r6, [r0], #1
149 .Lial2: ldrbt r7, [r0], #1
152 .Lial1: ldrbt r6, [r0], #1
158 * If few bytes left, finish slow.
164 * If source is not aligned, finish slow.
169 cmp r2, #0x60 /* Must be > 0x5f for unrolled cacheline */
173 * Align destination to cacheline boundary.
174 * If source and destination are nicely aligned, this can be a big
175 * win. If not, it's still cheaper to copy in groups of 32 even if
176 * we don't get the nice cacheline alignment.
189 .Lical28:ldrt r6, [r0], #4
192 .Lical24:ldrt r7, [r0], #4
195 .Lical20:ldrt r6, [r0], #4
198 .Lical16:ldrt r7, [r0], #4
201 .Lical12:ldrt r6, [r0], #4
204 .Lical8:ldrt r7, [r0], #4
207 .Lical4:ldrt r6, [r0], #4
212 * We start with > 0x40 bytes to copy (>= 0x60 got us into this
213 * part of the code, and we may have knocked that down by as much
214 * as 0x1c getting aligned).
216 * This loop basically works out to:
218 * prefetch-next-cacheline(s)
221 * } while (bytes >= 0x40);
231 /* Copy a cacheline */
248 /* Copy a cacheline */
273 * If we're done, bail.
280 ldr pc, [pc, r6, lsl #2]
286 .Lic4: ldrbt r6, [r0], #1
289 .Lic3: ldrbt r7, [r0], #1
292 .Lic2: ldrbt r6, [r0], #1
295 .Lic1: ldrbt r7, [r0], #1
304 str r5, [r4, #PCB_ONFAULT]
311 str r5, [r4, #PCB_ONFAULT]
318 * r0 = kernel space address
319 * r1 = user space address
322 * Copies bytes from kernel space to user space
324 * We save/restore r4-r11:
329 /* Quick exit if length is zero */
338 ldr r12, =(VM_MAXUSER_ADDRESS + 1)
343 ldr r3, .L_arm_memcpy
347 ldr r3, .L_min_memcpy_size
351 stmfd sp!, {r0-r2, r4, lr}
352 _SAVE({r0-r2, r4, lr})
356 mov r3, #1 /* DST_IS_USER */
357 ldr r4, .L_arm_memcpy
361 ldmfd sp!, {r0-r2, r4, lr}
370 ldr r5, [r4, #PCB_ONFAULT]
372 str r3, [r4, #PCB_ONFAULT]
378 * If not too many bytes, take the slow path.
384 * Align destination to word boundary.
387 ldr pc, [pc, r6, lsl #2]
393 .Lal3: ldrb r6, [r0], #1
396 .Lal2: ldrb r7, [r0], #1
399 .Lal1: ldrb r6, [r0], #1
405 * If few bytes left, finish slow.
411 * If source is not aligned, finish slow.
416 cmp r2, #0x60 /* Must be > 0x5f for unrolled cacheline */
420 * Align source & destination to cacheline boundary.
433 .Lcal28:ldr r6, [r0], #4
436 .Lcal24:ldr r7, [r0], #4
439 .Lcal20:ldr r6, [r0], #4
442 .Lcal16:ldr r7, [r0], #4
445 .Lcal12:ldr r6, [r0], #4
448 .Lcal8: ldr r7, [r0], #4
451 .Lcal4: ldr r6, [r0], #4
456 * We start with > 0x40 bytes to copy (>= 0x60 got us into this
457 * part of the code, and we may have knocked that down by as much
458 * as 0x1c getting aligned).
460 * This loop basically works out to:
462 * prefetch-next-cacheline(s)
465 * } while (bytes >= 0x40);
475 /* Copy a cacheline */
492 /* Copy a cacheline */
517 * If we're done, bail.
524 ldr pc, [pc, r6, lsl #2]
530 .Lc4: ldrb r6, [r0], #1
533 .Lc3: ldrb r7, [r0], #1
536 .Lc2: ldrb r6, [r0], #1
539 .Lc1: ldrb r7, [r0], #1
548 str r5, [r4, #PCB_ONFAULT]
556 * int badaddr_read_1(const uint8_t *src, uint8_t *dest)
558 * Copies a single 8-bit value from src to dest, returning 0 on success,
559 * else EFAULT if a page fault occurred.
561 ENTRY(badaddr_read_1)
565 ldr ip, [r2, #PCB_ONFAULT]
567 str r3, [r2, #PCB_ONFAULT]
576 mov r0, #0 /* No fault */
577 1: str ip, [r2, #PCB_ONFAULT]
582 * int badaddr_read_2(const uint16_t *src, uint16_t *dest)
584 * Copies a single 16-bit value from src to dest, returning 0 on success,
585 * else EFAULT if a page fault occurred.
587 ENTRY(badaddr_read_2)
591 ldr ip, [r2, #PCB_ONFAULT]
593 str r3, [r2, #PCB_ONFAULT]
602 mov r0, #0 /* No fault */
603 1: str ip, [r2, #PCB_ONFAULT]
608 * int badaddr_read_4(const uint32_t *src, uint32_t *dest)
610 * Copies a single 32-bit value from src to dest, returning 0 on success,
611 * else EFAULT if a page fault occurred.
613 ENTRY(badaddr_read_4)
617 ldr ip, [r2, #PCB_ONFAULT]
619 str r3, [r2, #PCB_ONFAULT]
628 mov r0, #0 /* No fault */
629 1: str ip, [r2, #PCB_ONFAULT]