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>
14 #include <asm/asm-extable.h>
16 /* We don't even really need the extable code, but for now keep it simple */
18 #define __enable_user_access() do { } while (0)
19 #define __disable_user_access() do { } while (0)
22 #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \
24 __enable_user_access(); \
25 __asm__ __volatile__ ( \
28 _ASM_EXTABLE_UACCESS_ERR(1b, 2b, %[r]) \
29 : [r] "+r" (ret), [ov] "=&r" (oldval), \
33 __disable_user_access(); \
37 arch_futex_atomic_op_inuser(int op
, int oparg
, int *oval
, u32 __user
*uaddr
)
39 int oldval
= 0, ret
= 0;
41 if (!access_ok(uaddr
, sizeof(u32
)))
46 __futex_atomic_op("amoswap.w.aqrl %[ov],%z[op],%[u]",
47 ret
, oldval
, uaddr
, oparg
);
50 __futex_atomic_op("amoadd.w.aqrl %[ov],%z[op],%[u]",
51 ret
, oldval
, uaddr
, oparg
);
54 __futex_atomic_op("amoor.w.aqrl %[ov],%z[op],%[u]",
55 ret
, oldval
, uaddr
, oparg
);
58 __futex_atomic_op("amoand.w.aqrl %[ov],%z[op],%[u]",
59 ret
, oldval
, uaddr
, ~oparg
);
62 __futex_atomic_op("amoxor.w.aqrl %[ov],%z[op],%[u]",
63 ret
, oldval
, uaddr
, oparg
);
76 futex_atomic_cmpxchg_inatomic(u32
*uval
, u32 __user
*uaddr
,
77 u32 oldval
, u32 newval
)
83 if (!access_ok(uaddr
, sizeof(u32
)))
86 __enable_user_access();
87 __asm__
__volatile__ (
88 "1: lr.w.aqrl %[v],%[u] \n"
89 " bne %[v],%z[ov],3f \n"
90 "2: sc.w.aqrl %[t],%z[nv],%[u] \n"
93 _ASM_EXTABLE_UACCESS_ERR(1b
, 3b
, %[r
]) \
94 _ASM_EXTABLE_UACCESS_ERR(2b
, 3b
, %[r
]) \
95 : [r
] "+r" (ret
), [v
] "=&r" (val
), [u
] "+m" (*uaddr
), [t
] "=&r" (tmp
)
96 : [ov
] "Jr" (oldval
), [nv
] "Jr" (newval
)
98 __disable_user_access();
104 #endif /* _ASM_RISCV_FUTEX_H */