xref: /openbmc/linux/arch/x86/include/asm/futex.h (revision 8ee90c5c)
1 #ifndef _ASM_X86_FUTEX_H
2 #define _ASM_X86_FUTEX_H
3 
4 #ifdef __KERNEL__
5 
6 #include <linux/futex.h>
7 #include <linux/uaccess.h>
8 
9 #include <asm/asm.h>
10 #include <asm/errno.h>
11 #include <asm/processor.h>
12 #include <asm/smap.h>
13 
14 #define __futex_atomic_op1(insn, ret, oldval, uaddr, oparg)	\
15 	asm volatile("\t" ASM_STAC "\n"				\
16 		     "1:\t" insn "\n"				\
17 		     "2:\t" ASM_CLAC "\n"			\
18 		     "\t.section .fixup,\"ax\"\n"		\
19 		     "3:\tmov\t%3, %1\n"			\
20 		     "\tjmp\t2b\n"				\
21 		     "\t.previous\n"				\
22 		     _ASM_EXTABLE(1b, 3b)			\
23 		     : "=r" (oldval), "=r" (ret), "+m" (*uaddr)	\
24 		     : "i" (-EFAULT), "0" (oparg), "1" (0))
25 
26 #define __futex_atomic_op2(insn, ret, oldval, uaddr, oparg)	\
27 	asm volatile("\t" ASM_STAC "\n"				\
28 		     "1:\tmovl	%2, %0\n"			\
29 		     "\tmovl\t%0, %3\n"				\
30 		     "\t" insn "\n"				\
31 		     "2:\t" LOCK_PREFIX "cmpxchgl %3, %2\n"	\
32 		     "\tjnz\t1b\n"				\
33 		     "3:\t" ASM_CLAC "\n"			\
34 		     "\t.section .fixup,\"ax\"\n"		\
35 		     "4:\tmov\t%5, %1\n"			\
36 		     "\tjmp\t3b\n"				\
37 		     "\t.previous\n"				\
38 		     _ASM_EXTABLE(1b, 4b)			\
39 		     _ASM_EXTABLE(2b, 4b)			\
40 		     : "=&a" (oldval), "=&r" (ret),		\
41 		       "+m" (*uaddr), "=&r" (tem)		\
42 		     : "r" (oparg), "i" (-EFAULT), "1" (0))
43 
44 static inline int arch_futex_atomic_op_inuser(int op, int oparg, int *oval,
45 		u32 __user *uaddr)
46 {
47 	int oldval = 0, ret, tem;
48 
49 	pagefault_disable();
50 
51 	switch (op) {
52 	case FUTEX_OP_SET:
53 		__futex_atomic_op1("xchgl %0, %2", ret, oldval, uaddr, oparg);
54 		break;
55 	case FUTEX_OP_ADD:
56 		__futex_atomic_op1(LOCK_PREFIX "xaddl %0, %2", ret, oldval,
57 				   uaddr, oparg);
58 		break;
59 	case FUTEX_OP_OR:
60 		__futex_atomic_op2("orl %4, %3", ret, oldval, uaddr, oparg);
61 		break;
62 	case FUTEX_OP_ANDN:
63 		__futex_atomic_op2("andl %4, %3", ret, oldval, uaddr, ~oparg);
64 		break;
65 	case FUTEX_OP_XOR:
66 		__futex_atomic_op2("xorl %4, %3", ret, oldval, uaddr, oparg);
67 		break;
68 	default:
69 		ret = -ENOSYS;
70 	}
71 
72 	pagefault_enable();
73 
74 	if (!ret)
75 		*oval = oldval;
76 
77 	return ret;
78 }
79 
80 static inline int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
81 						u32 oldval, u32 newval)
82 {
83 	return user_atomic_cmpxchg_inatomic(uval, uaddr, oldval, newval);
84 }
85 
86 #endif
87 #endif /* _ASM_X86_FUTEX_H */
88