2 * Copyright (c) 2015 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 __FBSDID("$FreeBSD$");
38 #include <machine/setjmp.h>
43 * One of the fu* or su* functions failed, return -1.
46 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 bgt a0, a4, fsu_fault_nopcb
58 la a6, fsu_fault /* Load the fault handler */
59 SET_FAULT_HANDLER(a6, a4) /* And set it */
60 1: lr.w a4, 0(a0) /* Load-exclusive the data */
61 bne a4, a1, 2f /* If not equal then exit */
62 sc.w a5, a3, 0(a0) /* Store the new data */
63 bnez a5, 1b /* Retry on failure */
64 2: SET_FAULT_HANDLER(x0, a5) /* Reset the fault handler */
65 sw a4, 0(a2) /* Store the read data */
66 li a0, 0 /* Success */
71 * int casueword(volatile u_long *, u_long, u_long *, u_long)
74 li a4, (VM_MAXUSER_ADDRESS-7)
75 bgt a0, a4, fsu_fault_nopcb
76 la a6, fsu_fault /* Load the fault handler */
77 SET_FAULT_HANDLER(a6, a4) /* And set it */
78 1: lr.d a4, 0(a0) /* Load-exclusive the data */
79 bne a4, a1, 2f /* If not equal then exit */
80 sc.d a5, a3, 0(a0) /* Store the new data */
81 bnez a5, 1b /* Retry on failure */
82 2: SET_FAULT_HANDLER(x0, a5) /* Reset the fault handler */
83 sd a4, 0(a2) /* Store the read data */
84 li a0, 0 /* Success */
89 * int fubyte(volatile const void *)
92 li a1, VM_MAXUSER_ADDRESS
93 bgt a0, a1, fsu_fault_nopcb
94 la a6, fsu_fault /* Load the fault handler */
95 SET_FAULT_HANDLER(a6, a1) /* And set it */
96 lb a0, 0(a0) /* Try loading the data */
97 SET_FAULT_HANDLER(x0, a1) /* Reset the fault handler */
102 * int fuword(volatile const void *)
105 li a1, (VM_MAXUSER_ADDRESS-1)
106 bgt a0, a1, fsu_fault_nopcb
107 la a6, fsu_fault /* Load the fault handler */
108 SET_FAULT_HANDLER(a6, a1) /* And set it */
109 lh a0, 0(a0) /* Try loading the data */
110 SET_FAULT_HANDLER(x0, a1) /* Reset the fault handler */
115 * int32_t fueword32(volatile const void *, int32_t *)
118 li a2, (VM_MAXUSER_ADDRESS-3)
119 bgt a0, a2, fsu_fault_nopcb
120 la a6, fsu_fault /* Load the fault handler */
121 SET_FAULT_HANDLER(a6, a2) /* And set it */
122 lw a0, 0(a0) /* Try loading the data */
123 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
124 sw a0, 0(a1) /* Save the data in kernel space */
125 li a0, 0 /* Success */
130 * long fueword(volatile const void *, int64_t *)
131 * int64_t fueword64(volatile const void *, int64_t *)
135 li a2, (VM_MAXUSER_ADDRESS-7)
136 bgt a0, a2, fsu_fault_nopcb
137 la a6, fsu_fault /* Load the fault handler */
138 SET_FAULT_HANDLER(a6, a2) /* And set it */
139 ld a0, 0(a0) /* Try loading the data */
140 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
141 sd a0, 0(a1) /* Save the data in kernel space */
142 li a0, 0 /* Success */
148 * int subyte(volatile void *, int)
151 li a2, VM_MAXUSER_ADDRESS
152 bgt a0, a2, fsu_fault_nopcb
153 la a6, fsu_fault /* Load the fault handler */
154 SET_FAULT_HANDLER(a6, a2) /* And set it */
155 sb a1, 0(a0) /* Try storing the data */
156 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
157 li a0, 0 /* Success */
162 * int suword16(volatile void *, int)
165 li a2, (VM_MAXUSER_ADDRESS-1)
166 bgt a0, a2, fsu_fault_nopcb
167 la a6, fsu_fault /* Load the fault handler */
168 SET_FAULT_HANDLER(a6, a2) /* And set it */
169 sh a1, 0(a0) /* Try storing the data */
170 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
171 li a0, 0 /* Success */
176 * int suword32(volatile void *, int)
179 li a2, (VM_MAXUSER_ADDRESS-3)
180 bgt a0, a2, fsu_fault_nopcb
181 la a6, fsu_fault /* Load the fault handler */
182 SET_FAULT_HANDLER(a6, a2) /* And set it */
183 sw a1, 0(a0) /* Try storing the data */
184 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
185 li a0, 0 /* Success */
190 * int suword(volatile void *, long)
194 li a2, (VM_MAXUSER_ADDRESS-7)
195 bgt a0, a2, fsu_fault_nopcb
196 la a6, fsu_fault /* Load the fault handler */
197 SET_FAULT_HANDLER(a6, a2) /* And set it */
198 sd a1, 0(a0) /* Try storing the data */
199 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
200 li a0, 0 /* Success */
206 * fuswintr and suswintr are just like fusword and susword except that if
207 * the page is not in memory or would cause a trap, then we return an error.
208 * The important thing is to prevent sleep() and switch().
212 * Special handler so the trap code knows not to sleep.
214 ENTRY(fsu_intr_fault)
215 SET_FAULT_HANDLER(x0, a1) /* Reset the handler function */
221 * int fuswintr(void *)
224 li a1, (VM_MAXUSER_ADDRESS-3)
225 bgt a0, a1, fsu_fault_nopcb
226 la a6, fsu_intr_fault /* Load the fault handler */
227 SET_FAULT_HANDLER(a6, a1) /* And set it */
228 lw a0, 0(a0) /* Try loading the data */
229 SET_FAULT_HANDLER(x0, x1) /* Reset the fault handler */
234 * int suswintr(void *base, int word)
237 li a2, (VM_MAXUSER_ADDRESS-3)
238 bgt a0, a2, fsu_fault_nopcb
239 la a6, fsu_intr_fault /* Load the fault handler */
240 SET_FAULT_HANDLER(a6, a2) /* And set it */
241 sw a1, 0(a0) /* Try storing the data */
242 SET_FAULT_HANDLER(x0, a2) /* Reset the fault handler */
243 li a0, 0 /* Success */
248 /* Store the stack pointer */
252 /* Store the general purpose registers and ra */
273 /* Restore the stack pointer */
277 /* Restore the general purpose registers and ra */
292 /* Load the return value */