]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/copyinout.S
Copy googletest 1.8.1 from ^/vendor/google/googletest/1.8.1 to .../contrib/googletest
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / copyinout.S
1 /*-
2  * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
3  * Copyright (c) 2019 Mitchell Horne
4  * All rights reserved.
5  *
6  * Portions of this software were developed by SRI International and the
7  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Portions of this software were developed by the University of Cambridge
11  * Computer Laboratory as part of the CTSRD Project, with support from the
12  * UK Higher Education Innovation Fund (HEIF).
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR 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
36 #include <machine/asm.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <machine/riscvreg.h>
40 #include <sys/errno.h>
41
42 #include "assym.inc"
43
44 /*
45  * Fault handler for the copy{in,out} functions below.
46  */
47 ENTRY(copyio_fault)
48         SET_FAULT_HANDLER(x0, a1) /* Clear the handler */
49         EXIT_USER_ACCESS(a1)
50 copyio_fault_nopcb:
51         li      a0, EFAULT
52         ret
53 END(copyio_fault)
54
55 /*
56  * copycommon - common copy routine
57  *
58  * a0 - Source address
59  * a1 - Destination address
60  * a2 - Size of copy
61  */
62         .macro copycommon
63         la      a6, copyio_fault        /* Get the handler address */
64         SET_FAULT_HANDLER(a6, a7)       /* Set the handler */
65         ENTER_USER_ACCESS(a7)
66
67         li      t2, XLEN_BYTES
68         blt     a2, t2, 3f              /* Byte-copy if len < XLEN_BYTES */
69
70         /*
71          * Compare lower bits of src and dest.
72          * If they are aligned with each other, we can do word copy.
73          */
74         andi    t0, a0, (XLEN_BYTES-1)  /* Low bits of src */
75         andi    t1, a1, (XLEN_BYTES-1)  /* Low bits of dest */
76         bne     t0, t1, 3f              /* Misaligned. Go to byte copy */
77         beqz    t0, 2f                  /* Already word-aligned, skip ahead */
78
79         /* Byte copy until the first word-aligned address */
80 1:      lb      a4, 0(a0)               /* Load byte from src */
81         addi    a0, a0, 1
82         sb      a4, 0(a1)               /* Store byte in dest */
83         addi    a1, a1, 1
84         addi    a2, a2, -1              /* len-- */
85         andi    t0, a0, (XLEN_BYTES-1)
86         bnez    t0, 1b
87
88         /* Copy words */
89 2:      ld      a4, 0(a0)               /* Load word from src */
90         addi    a0, a0, XLEN_BYTES
91         sd      a4, 0(a1)               /* Store word in dest */
92         addi    a1, a1, XLEN_BYTES
93         addi    a2, a2, -XLEN_BYTES     /* len -= XLEN_BYTES */
94         bgeu    a2, t2, 2b              /* Again if len >= XLEN_BYTES */
95
96         /* Check if we're finished */
97         beqz    a2, 4f
98
99         /* Copy any remaining bytes */
100 3:      lb      a4, 0(a0)               /* Load byte from src */
101         addi    a0, a0, 1
102         sb      a4, 0(a1)               /* Store byte in dest */
103         addi    a1, a1, 1
104         addi    a2, a2, -1              /* len-- */
105         bnez    a2, 3b
106
107 4:      EXIT_USER_ACCESS(a7)
108         SET_FAULT_HANDLER(x0, a7)       /* Clear the handler */
109         .endm
110
111 /*
112  * Copies from a kernel to user address
113  *
114  * int copyout(const void *kaddr, void *udaddr, size_t len)
115  */
116 ENTRY(copyout)
117         beqz    a2, copyout_end /* If len == 0 then skip loop */
118         add     a3, a1, a2
119         li      a4, VM_MAXUSER_ADDRESS
120         bgt     a3, a4, copyio_fault_nopcb
121
122         copycommon
123
124 copyout_end:
125         li      a0, 0           /* return 0 */
126         ret
127 END(copyout)
128
129 /*
130  * Copies from a user to kernel address
131  *
132  * int copyin(const void *uaddr, void *kaddr, size_t len)
133  */
134 ENTRY(copyin)
135         beqz    a2, copyin_end  /* If len == 0 then skip loop */
136         add     a3, a0, a2
137         li      a4, VM_MAXUSER_ADDRESS
138         bgt     a3, a4, copyio_fault_nopcb
139
140         copycommon
141
142 copyin_end:
143         li      a0, 0           /* return 0 */
144         ret
145 END(copyin)
146
147 /*
148  * Copies a string from a user to kernel address
149  *
150  * int copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *done)
151  */
152 ENTRY(copyinstr)
153         mv      a5, x0          /* count = 0 */
154         beqz    a2, 3f          /* If len == 0 then skip loop */
155
156         la      a6, copyio_fault /* Get the handler address */
157         SET_FAULT_HANDLER(a6, a7) /* Set the handler */
158         ENTER_USER_ACCESS(a7)
159
160         li      a7, VM_MAXUSER_ADDRESS
161 1:      bgt     a0, a7, copyio_fault
162         lb      a4, 0(a0)       /* Load from uaddr */
163         addi    a0, a0, 1
164         sb      a4, 0(a1)       /* Store in kaddr */
165         addi    a1, a1, 1
166         beqz    a4, 2f
167         addi    a2, a2, -1      /* len-- */
168         addi    a5, a5, 1       /* count++ */
169         bnez    a2, 1b
170
171 2:      EXIT_USER_ACCESS(a7)
172         SET_FAULT_HANDLER(x0, a7) /* Clear the handler */
173
174 3:      beqz    a3, 4f          /* Check if done != NULL */
175         addi    a5, a5, 1       /* count++ */
176         sd      a5, 0(a3)       /* done = count */
177
178 4:      mv      a0, x0          /* return 0 */
179         beqz    a4, 5f
180         li      a0, ENAMETOOLONG
181 5:
182         ret
183 END(copyinstr)