2 * Copyright (c) 2015 The FreeBSD Foundation
5 * This software was developed by Andrew Turner under
6 * sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <machine/asm.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/errno.h>
36 #include <machine/vmparam.h>
40 .macro check_user_access user_arg, size_arg, bad_access_func
41 adds x6, x\user_arg, x\size_arg
43 ldr x7, =VM_MAXUSER_ADDRESS
49 * Fault handler for the copy{in,out} functions below.
52 SET_FAULT_HANDLER(xzr, x1) /* Clear the handler */
53 EXIT_USER_ACCESS_CHECK(w0, x1)
60 * Copies from a kernel to user address
62 * int copyout(const void *kaddr, void *udaddr, size_t len)
66 check_user_access 1, 2, copyio_fault_nopcb
70 1: mov x0, xzr /* return 0 */
76 * Copies from a user to kernel address
78 * int copyin(const void *uaddr, void *kdaddr, size_t len)
82 check_user_access 0, 2, copyio_fault_nopcb
86 1: mov x0, xzr /* return 0 */
92 * Copies a string from a user to kernel address
94 * int copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *done)
97 mov x5, xzr /* count = 0 */
98 mov w4, #1 /* If zero return faulure */
99 cbz x2, 3f /* If len == 0 then skip loop */
101 adr x6, copyio_fault /* Get the handler address */
102 SET_FAULT_HANDLER(x6, x7) /* Set the handler */
104 ldr x7, =VM_MAXUSER_ADDRESS
107 ldtrb w4, [x0] /* Load from uaddr */
108 add x0, x0, #1 /* Next char */
109 strb w4, [x1], #1 /* Store in kaddr */
110 add x5, x5, #1 /* count++ */
111 cbz w4, 2f /* Break when NUL-terminated */
112 sub x2, x2, #1 /* len-- */
115 2: SET_FAULT_HANDLER(xzr, x7) /* Clear the handler */
118 3: cbz x3, 4f /* Check if done != NULL */
119 str x5, [x3] /* done = count */
121 4: mov w1, #ENAMETOOLONG /* Load ENAMETOOLONG to return if failed */
122 cmp w4, #0 /* Check if we saved the NUL-terminator */
123 csel w0, wzr, w1, eq /* If so return success, else failure */
133 * lr - the return address, so jump here instead of calling
135 * This function is optimized to minimize concurrent memory accesses. In
136 * present form it is suited for cores with a single memory prefetching
139 * Consider using separate functions for each ARM64 core. Adding memory
140 * access interleaving might increase a total throughput on A57 or A72.
145 .type copycommon,@function
148 adr x6, copyio_fault /* Get the handler address */
149 SET_FAULT_HANDLER(x6, x7) /* Set the handler */
150 ENTER_USER_ACCESS(w6, x7)
152 /* Check alignment */
157 /* Unaligned is byte by byte copy */
174 ldp x3, x4, [x0], #0x10
175 ldp x5, x6, [x0], #0x10
176 ldp x7, x8, [x0], #0x10
177 ldp x9, x10, [x0], #0x10
178 stp x3, x4, [x1], #0x10
179 stp x5, x6, [x1], #0x10
180 stp x7, x8, [x1], #0x10
181 stp x9, x10, [x1], #0x10
192 ldp x3, x4, [x0], #0x10
193 stp x3, x4, [x1], #0x10
197 /* Less than 16 bytes to copy */
199 tbz x2, #0x03, last_word
204 tbz x2, #0x02, last_hword
209 tbz x2, #0x01, last_byte
214 tbz x2, #0x00, ending
219 EXIT_USER_ACCESS_CHECK(w6, x7)
220 SET_FAULT_HANDLER(xzr, x7) /* Clear the handler */
222 mov x0, xzr /* return 0 */
224 .size copycommon, . - copycommon