2 * Copyright (c) 2015-2020 Ruslan Bukin <br@bsdpad.com>
5 * Portions of this software were developed by SRI International and the
6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Portions of this software were developed by the University of Cambridge
10 * Computer Laboratory as part of the CTSRD Project, with support from the
11 * UK Higher Education Innovation Fund (HEIF).
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR 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
35 #include <machine/asm.h>
36 #include <machine/setjmp.h>
37 #include <machine/riscvreg.h>
42 * One of the fu* or su* functions failed, return -1.
45 SET_FAULT_HANDLER(x0, a1) /* Reset the handler function */
53 * int casueword32(volatile uint32_t *, uint32_t, uint32_t *, uint32_t)
56 li a4, (VM_MAXUSER_ADDRESS-3)
57 bgeu a0, a4, fsu_fault_nopcb
58 la a6, fsu_fault /* Load the fault handler */
59 SET_FAULT_HANDLER(a6, a4) /* And set it */
61 lr.w a4, 0(a0) /* Load-exclusive the data */
62 bne a4, a1, 1f /* If not equal then exit */
63 sc.w a5, a3, 0(a0) /* Store the new data */
64 beqz a5, 2f /* Success */
65 1: li a5, 1 /* Normalize failure result */
66 2: EXIT_USER_ACCESS(a6)
67 SET_FAULT_HANDLER(x0, a6) /* Reset the fault handler */
68 sw a4, 0(a2) /* Store the read data */
69 mv a0, a5 /* Success indicator */
74 * int casueword(volatile u_long *, u_long, u_long *, u_long)
77 li a4, (VM_MAXUSER_ADDRESS-7)
78 bgeu a0, a4, fsu_fault_nopcb
79 la a6, fsu_fault /* Load the fault handler */
80 SET_FAULT_HANDLER(a6, a4) /* And set it */
82 lr.d a4, 0(a0) /* Load-exclusive the data */
83 bne a4, a1, 1f /* If not equal then exit */
84 sc.d a5, a3, 0(a0) /* Store the new data */
85 beqz a5, 2f /* Success */
86 1: li a5, 1 /* Normalize failure result */
87 2: EXIT_USER_ACCESS(a6)
88 SET_FAULT_HANDLER(x0, a6) /* Reset the fault handler */
89 sd a4, 0(a2) /* Store the read data */
90 mv a0, a5 /* Success indicator */
95 * int fubyte(volatile const void *)
98 li a1, VM_MAXUSER_ADDRESS
99 bgeu a0, a1, fsu_fault_nopcb
100 la a6, fsu_fault /* Load the fault handler */
101 SET_FAULT_HANDLER(a6, a1) /* And set it */
102 ENTER_USER_ACCESS(a1)
103 lbu a0, 0(a0) /* Try loading the data */
105 SET_FAULT_HANDLER(x0, a1) /* Reset the fault handler */
110 * int fuword(volatile const void *)
113 li a1, (VM_MAXUSER_ADDRESS-1)
114 bgeu a0, a1, fsu_fault_nopcb
115 la a6, fsu_fault /* Load the fault handler */
116 SET_FAULT_HANDLER(a6, a1) /* And set it */
117 ENTER_USER_ACCESS(a1)
118 lhu a0, 0(a0) /* Try loading the data */
120 SET_FAULT_HANDLER(x0, a1) /* Reset the fault handler */
125 * int32_t fueword32(volatile const void *, int32_t *)
128 li a2, (VM_MAXUSER_ADDRESS-3)
129 bgeu a0, a2, fsu_fault_nopcb
130 la a6, fsu_fault /* Load the fault handler */
131 SET_FAULT_HANDLER(a6, a2) /* And set it */
132 ENTER_USER_ACCESS(a2)
133 lw a0, 0(a0) /* Try loading the data */
135 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
136 sw a0, 0(a1) /* Save the data in kernel space */
137 li a0, 0 /* Success */
142 * long fueword(volatile const void *, int64_t *)
143 * int64_t fueword64(volatile const void *, int64_t *)
147 li a2, (VM_MAXUSER_ADDRESS-7)
148 bgeu a0, a2, fsu_fault_nopcb
149 la a6, fsu_fault /* Load the fault handler */
150 SET_FAULT_HANDLER(a6, a2) /* And set it */
151 ENTER_USER_ACCESS(a2)
152 ld a0, 0(a0) /* Try loading the data */
154 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
155 sd a0, 0(a1) /* Save the data in kernel space */
156 li a0, 0 /* Success */
162 * int subyte(volatile void *, int)
165 li a2, VM_MAXUSER_ADDRESS
166 bgeu a0, a2, fsu_fault_nopcb
167 la a6, fsu_fault /* Load the fault handler */
168 SET_FAULT_HANDLER(a6, a2) /* And set it */
169 ENTER_USER_ACCESS(a2)
170 sb a1, 0(a0) /* Try storing the data */
172 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
173 li a0, 0 /* Success */
178 * int suword16(volatile void *, int)
181 li a2, (VM_MAXUSER_ADDRESS-1)
182 bgeu a0, a2, fsu_fault_nopcb
183 la a6, fsu_fault /* Load the fault handler */
184 SET_FAULT_HANDLER(a6, a2) /* And set it */
185 ENTER_USER_ACCESS(a2)
186 sh a1, 0(a0) /* Try storing the data */
188 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
189 li a0, 0 /* Success */
194 * int suword32(volatile void *, int)
197 li a2, (VM_MAXUSER_ADDRESS-3)
198 bgeu a0, a2, fsu_fault_nopcb
199 la a6, fsu_fault /* Load the fault handler */
200 SET_FAULT_HANDLER(a6, a2) /* And set it */
201 ENTER_USER_ACCESS(a2)
202 sw a1, 0(a0) /* Try storing the data */
204 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
205 li a0, 0 /* Success */
210 * int suword(volatile void *, long)
214 li a2, (VM_MAXUSER_ADDRESS-7)
215 bgeu a0, a2, fsu_fault_nopcb
216 la a6, fsu_fault /* Load the fault handler */
217 SET_FAULT_HANDLER(a6, a2) /* And set it */
218 ENTER_USER_ACCESS(a2)
219 sd a1, 0(a0) /* Try storing the data */
221 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
222 li a0, 0 /* Success */
228 /* Store the stack pointer */
232 /* Store the general purpose registers and ra */
253 /* Restore the stack pointer */
257 /* Restore the general purpose registers and ra */
272 /* Load the return value */