1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2006 Ralf Baechle (ralf@linux-mips.org)
4 * Copyright (c) 2018 Jim Wilson (jimw@sifive.com)
7 #ifndef _ASM_RISCV_FUTEX_H
8 #define _ASM_RISCV_FUTEX_H
10 #include <linux/futex.h>
11 #include <linux/uaccess.h>
12 #include <linux/errno.h>
15 /* We don't even really need the extable code, but for now keep it simple */
17 #define __enable_user_access() do { } while (0)
18 #define __disable_user_access() do { } while (0)
21 #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \
24 __enable_user_access(); \
25 __asm__ __volatile__ ( \
28 " .section .fixup,\"ax\" \n" \
30 "3: li %[r],%[e] \n" \
33 " .section __ex_table,\"a\" \n" \
34 " .balign " RISCV_SZPTR " \n" \
35 " " RISCV_PTR " 1b, 3b \n" \
37 : [r] "+r" (ret), [ov] "=&r" (oldval), \
38 [u] "+m" (*uaddr), [t] "=&r" (tmp) \
39 : [op] "Jr" (oparg), [e] "i" (-EFAULT) \
41 __disable_user_access(); \
45 arch_futex_atomic_op_inuser(int op
, int oparg
, int *oval
, u32 __user
*uaddr
)
47 int oldval
= 0, ret
= 0;
49 if (!access_ok(uaddr
, sizeof(u32
)))
54 __futex_atomic_op("amoswap.w.aqrl %[ov],%z[op],%[u]",
55 ret
, oldval
, uaddr
, oparg
);
58 __futex_atomic_op("amoadd.w.aqrl %[ov],%z[op],%[u]",
59 ret
, oldval
, uaddr
, oparg
);
62 __futex_atomic_op("amoor.w.aqrl %[ov],%z[op],%[u]",
63 ret
, oldval
, uaddr
, oparg
);
66 __futex_atomic_op("amoand.w.aqrl %[ov],%z[op],%[u]",
67 ret
, oldval
, uaddr
, ~oparg
);
70 __futex_atomic_op("amoxor.w.aqrl %[ov],%z[op],%[u]",
71 ret
, oldval
, uaddr
, oparg
);
84 futex_atomic_cmpxchg_inatomic(u32
*uval
, u32 __user
*uaddr
,
85 u32 oldval
, u32 newval
)
91 if (!access_ok(uaddr
, sizeof(u32
)))
94 __enable_user_access();
95 __asm__
__volatile__ (
96 "1: lr.w.aqrl %[v],%[u] \n"
97 " bne %[v],%z[ov],3f \n"
98 "2: sc.w.aqrl %[t],%z[nv],%[u] \n"
101 " .section .fixup,\"ax\" \n"
106 " .section __ex_table,\"a\" \n"
107 " .balign " RISCV_SZPTR
" \n"
108 " " RISCV_PTR
" 1b, 4b \n"
109 " " RISCV_PTR
" 2b, 4b \n"
111 : [r
] "+r" (ret
), [v
] "=&r" (val
), [u
] "+m" (*uaddr
), [t
] "=&r" (tmp
)
112 : [ov
] "Jr" (oldval
), [nv
] "Jr" (newval
), [e
] "i" (-EFAULT
)
114 __disable_user_access();
120 #endif /* _ASM_RISCV_FUTEX_H */