xref: /openbmc/linux/arch/s390/include/asm/futex.h (revision 43ffe817)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_S390_FUTEX_H
3 #define _ASM_S390_FUTEX_H
4 
5 #include <linux/uaccess.h>
6 #include <linux/futex.h>
7 #include <asm/mmu_context.h>
8 #include <asm/errno.h>
9 
10 #define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg)	\
11 	asm volatile(							\
12 		"   sacf  256\n"					\
13 		"0: l     %1,0(%6)\n"					\
14 		"1:"insn						\
15 		"2: cs    %1,%2,0(%6)\n"				\
16 		"3: jl    1b\n"						\
17 		"   lhi   %0,0\n"					\
18 		"4: sacf  768\n"					\
19 		EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b)		\
20 		: "=d" (ret), "=&d" (oldval), "=&d" (newval),		\
21 		  "=m" (*uaddr)						\
22 		: "0" (-EFAULT), "d" (oparg), "a" (uaddr),		\
23 		  "m" (*uaddr) : "cc");
24 
25 static inline int arch_futex_atomic_op_inuser(int op, int oparg, int *oval,
26 		u32 __user *uaddr)
27 {
28 	int oldval = 0, newval, ret;
29 	mm_segment_t old_fs;
30 
31 	old_fs = enable_sacf_uaccess();
32 	switch (op) {
33 	case FUTEX_OP_SET:
34 		__futex_atomic_op("lr %2,%5\n",
35 				  ret, oldval, newval, uaddr, oparg);
36 		break;
37 	case FUTEX_OP_ADD:
38 		__futex_atomic_op("lr %2,%1\nar %2,%5\n",
39 				  ret, oldval, newval, uaddr, oparg);
40 		break;
41 	case FUTEX_OP_OR:
42 		__futex_atomic_op("lr %2,%1\nor %2,%5\n",
43 				  ret, oldval, newval, uaddr, oparg);
44 		break;
45 	case FUTEX_OP_ANDN:
46 		__futex_atomic_op("lr %2,%1\nnr %2,%5\n",
47 				  ret, oldval, newval, uaddr, oparg);
48 		break;
49 	case FUTEX_OP_XOR:
50 		__futex_atomic_op("lr %2,%1\nxr %2,%5\n",
51 				  ret, oldval, newval, uaddr, oparg);
52 		break;
53 	default:
54 		ret = -ENOSYS;
55 	}
56 	disable_sacf_uaccess(old_fs);
57 
58 	if (!ret)
59 		*oval = oldval;
60 
61 	return ret;
62 }
63 
64 static inline int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
65 						u32 oldval, u32 newval)
66 {
67 	mm_segment_t old_fs;
68 	int ret;
69 
70 	old_fs = enable_sacf_uaccess();
71 	asm volatile(
72 		"   sacf 256\n"
73 		"0: cs   %1,%4,0(%5)\n"
74 		"1: la   %0,0\n"
75 		"2: sacf 768\n"
76 		EX_TABLE(0b,2b) EX_TABLE(1b,2b)
77 		: "=d" (ret), "+d" (oldval), "=m" (*uaddr)
78 		: "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr)
79 		: "cc", "memory");
80 	disable_sacf_uaccess(old_fs);
81 	*uval = oldval;
82 	return ret;
83 }
84 
85 #endif /* _ASM_S390_FUTEX_H */
86