]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/copyinout.S
busdma: tidy bus_dma_run_filter() functions
[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 #include <machine/riscvreg.h>
38 #include <sys/errno.h>
39
40 #include "assym.inc"
41
42 /*
43  * Fault handler for the copy{in,out} functions below.
44  */
45 ENTRY(copyio_fault)
46         SET_FAULT_HANDLER(x0, a1) /* Clear the handler */
47         EXIT_USER_ACCESS(a1)
48 copyio_fault_nopcb:
49         li      a0, EFAULT
50         ret
51 END(copyio_fault)
52
53 /*
54  * copycommon - common copy routine
55  *
56  * a0 - Source address
57  * a1 - Destination address
58  * a2 - Size of copy
59  */
60         .macro copycommon
61         la      a6, copyio_fault        /* Get the handler address */
62         SET_FAULT_HANDLER(a6, a7)       /* Set the handler */
63         ENTER_USER_ACCESS(a7)
64
65         li      t2, XLEN_BYTES
66         blt     a2, t2, 4f              /* Byte-copy if len < XLEN_BYTES */
67
68         /*
69          * Compare lower bits of src and dest.
70          * If they are aligned with each other, we can do word copy.
71          */
72         andi    t0, a0, (XLEN_BYTES-1)  /* Low bits of src */
73         andi    t1, a1, (XLEN_BYTES-1)  /* Low bits of dest */
74         bne     t0, t1, 4f              /* Misaligned. Go to byte copy */
75         beqz    t0, 2f                  /* Already word-aligned, skip ahead */
76
77         /* Byte copy until the first word-aligned address */
78 1:      lb      a4, 0(a0)               /* Load byte from src */
79         addi    a0, a0, 1
80         sb      a4, 0(a1)               /* Store byte in dest */
81         addi    a1, a1, 1
82         addi    a2, a2, -1              /* len-- */
83         andi    t0, a0, (XLEN_BYTES-1)
84         bnez    t0, 1b
85         j       3f
86
87         /* Copy words */
88 2:      ld      a4, 0(a0)               /* Load word from src */
89         addi    a0, a0, XLEN_BYTES
90         sd      a4, 0(a1)               /* Store word in dest */
91         addi    a1, a1, XLEN_BYTES
92         addi    a2, a2, -XLEN_BYTES     /* len -= XLEN_BYTES */
93 3:      bgeu    a2, t2, 2b              /* Again if len >= XLEN_BYTES */
94
95         /* Check if we're finished */
96         beqz    a2, 5f
97
98         /* Copy any remaining bytes */
99 4:      lb      a4, 0(a0)               /* Load byte from src */
100         addi    a0, a0, 1
101         sb      a4, 0(a1)               /* Store byte in dest */
102         addi    a1, a1, 1
103         addi    a2, a2, -1              /* len-- */
104         bnez    a2, 4b
105
106 5:      EXIT_USER_ACCESS(a7)
107         SET_FAULT_HANDLER(x0, a7)       /* Clear the handler */
108         .endm
109
110 /*
111  * Copies from a kernel to user address
112  *
113  * int copyout(const void *kaddr, void *udaddr, size_t len)
114  */
115 ENTRY(copyout)
116         beqz    a2, copyout_end /* If len == 0 then skip loop */
117         add     a3, a1, a2
118         li      a4, VM_MAXUSER_ADDRESS
119         bgeu    a3, a4, copyio_fault_nopcb
120
121         copycommon
122
123 copyout_end:
124         li      a0, 0           /* return 0 */
125         ret
126 END(copyout)
127
128 /*
129  * Copies from a user to kernel address
130  *
131  * int copyin(const void *uaddr, void *kaddr, size_t len)
132  */
133 ENTRY(copyin)
134         beqz    a2, copyin_end  /* If len == 0 then skip loop */
135         add     a3, a0, a2
136         li      a4, VM_MAXUSER_ADDRESS
137         bgeu    a3, a4, copyio_fault_nopcb
138
139         copycommon
140
141 copyin_end:
142         li      a0, 0           /* return 0 */
143         ret
144 END(copyin)
145
146 /*
147  * Copies a string from a user to kernel address
148  *
149  * int copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *done)
150  */
151 ENTRY(copyinstr)
152         mv      a5, x0          /* count = 0 */
153         beqz    a2, 3f          /* If len == 0 then skip loop */
154
155         la      a6, copyio_fault /* Get the handler address */
156         SET_FAULT_HANDLER(a6, a7) /* Set the handler */
157         ENTER_USER_ACCESS(a7)
158
159         li      a7, VM_MAXUSER_ADDRESS
160 1:      bgeu    a0, a7, copyio_fault
161         lb      a4, 0(a0)       /* Load from uaddr */
162         addi    a0, a0, 1
163         sb      a4, 0(a1)       /* Store in kaddr */
164         addi    a1, a1, 1
165         beqz    a4, 2f
166         addi    a2, a2, -1      /* len-- */
167         addi    a5, a5, 1       /* count++ */
168         bnez    a2, 1b
169
170 2:      EXIT_USER_ACCESS(a7)
171         SET_FAULT_HANDLER(x0, a7) /* Clear the handler */
172
173 3:      beqz    a3, 4f          /* Check if done != NULL */
174         addi    a5, a5, 1       /* count++ */
175         sd      a5, 0(a3)       /* done = count */
176
177 4:      mv      a0, x0          /* return 0 */
178         beqz    a4, 5f
179         li      a0, ENAMETOOLONG
180 5:
181         ret
182 END(copyinstr)