2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 2018 Turing Robotic Industries Inc.
5 * Copyright (C) 2022 Dmitry Chagin <dchagin@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <machine/asm.h>
32 __FBSDID("$FreeBSD$");
34 #include <machine/param.h>
35 #include <machine/vmparam.h>
37 #include <sys/errno.h>
41 .macro check_user_access user_arg, limit, bad_addr_func
48 SET_FAULT_HANDLER(xzr, x1)
49 EXIT_USER_ACCESS_CHECK(w0, x1)
54 #define LINUX_FUTEX_MAX_LOOPS 128
57 * int oparg, uint32_t *uaddr, int *oldval
59 * Return 0 on success, errno on failure,
60 * EAGAIN is returned if LL/SC operation fails.
62 * XXX. VM_MAXUSER_ADDRESS is not applicable here, should be replaced
63 * by something like LINUX_SHAREDPAGE.
66 /* (int *)uaddr2 = oparg */
68 check_user_access 1, (VM_MAXUSER_ADDRESS-3), futex_fault_nopcb
69 adr x9, futex_fault /* Load the fault handler */
70 SET_FAULT_HANDLER(x9, x4) /* And set it */
71 ENTER_USER_ACCESS(w9, x4)
72 mov w5, #LINUX_FUTEX_MAX_LOOPS
74 mov w6, w0 /* Save oparg */
75 1: ldxr w4, [x1] /* Load oldval from uaddr */
76 stlxr w0, w6, [x1] /* Store oparg to uaddr */
77 cbz w0, 3f /* Exit on success */
78 sub w5, w5, w0 /* Dec loop counter, w0 is 1 */
79 cbnz w5, 1b /* Loop */
80 mov x0, #EAGAIN /* Store of newval failed */
83 SET_FAULT_HANDLER(xzr, x9) /* Reset the fault handler */
84 str w4, [x2] /* Store oldval */
88 /* (int *)uaddr2 += oparg */
90 check_user_access 1, (VM_MAXUSER_ADDRESS-3), futex_fault_nopcb
92 SET_FAULT_HANDLER(x9, x4)
93 ENTER_USER_ACCESS(w9, x4)
94 mov w5, #LINUX_FUTEX_MAX_LOOPS
98 add w3, w4, w6 /* oldval + oparg */
106 SET_FAULT_HANDLER(xzr, x9)
111 /* (int *)uaddr2 |= oparg */
113 check_user_access 1, (VM_MAXUSER_ADDRESS-3), futex_fault_nopcb
115 SET_FAULT_HANDLER(x9, x4)
116 ENTER_USER_ACCESS(w9, x4)
117 mov w5, #LINUX_FUTEX_MAX_LOOPS
121 orr w3, w4, w6 /* oldavl |= oparg */
129 SET_FAULT_HANDLER(xzr, x9)
134 /* (int *)uaddr2 &= oparg */
136 check_user_access 1, (VM_MAXUSER_ADDRESS-3), futex_fault_nopcb
138 SET_FAULT_HANDLER(x9, x4)
139 ENTER_USER_ACCESS(w9, x4)
140 mov w5, #LINUX_FUTEX_MAX_LOOPS
144 and w3, w4, w6 /* oldval &= oparg */
152 SET_FAULT_HANDLER(xzr, x9)
157 /* (int *)uaddr2 ^= oparg */
159 check_user_access 1, (VM_MAXUSER_ADDRESS-3), futex_fault_nopcb
161 SET_FAULT_HANDLER(x9, x4)
162 ENTER_USER_ACCESS(w9, x4)
163 mov w5, #LINUX_FUTEX_MAX_LOOPS
167 eor w3, w4, w6 /* oldval ^= oparg */
175 SET_FAULT_HANDLER(xzr, x9)