GNU Linux-libre 4.4.287-gnu1
[releases.git] / arch / microblaze / include / asm / futex.h
1 #ifndef _ASM_MICROBLAZE_FUTEX_H
2 #define _ASM_MICROBLAZE_FUTEX_H
3
4 #ifdef __KERNEL__
5
6 #include <linux/futex.h>
7 #include <linux/uaccess.h>
8 #include <asm/errno.h>
9
10 #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \
11 ({                                                                      \
12         __asm__ __volatile__ (                                          \
13                         "1:     lwx     %0, %2, r0; "                   \
14                                 insn                                    \
15                         "2:     swx     %1, %2, r0;                     \
16                                 addic   %1, r0, 0;                      \
17                                 bnei    %1, 1b;                         \
18                         3:                                              \
19                         .section .fixup,\"ax\";                         \
20                         4:      brid    3b;                             \
21                                 addik   %1, r0, %3;                     \
22                         .previous;                                      \
23                         .section __ex_table,\"a\";                      \
24                         .word   1b,4b,2b,4b;                            \
25                         .previous;"                                     \
26         : "=&r" (oldval), "=&r" (ret)                                   \
27         : "r" (uaddr), "i" (-EFAULT), "r" (oparg)                       \
28         );                                                              \
29 })
30
31 static inline int
32 arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
33 {
34         int oldval = 0, ret;
35
36         pagefault_disable();
37
38         switch (op) {
39         case FUTEX_OP_SET:
40                 __futex_atomic_op("or %1,%4,%4;", ret, oldval, uaddr, oparg);
41                 break;
42         case FUTEX_OP_ADD:
43                 __futex_atomic_op("add %1,%0,%4;", ret, oldval, uaddr, oparg);
44                 break;
45         case FUTEX_OP_OR:
46                 __futex_atomic_op("or %1,%0,%4;", ret, oldval, uaddr, oparg);
47                 break;
48         case FUTEX_OP_ANDN:
49                 __futex_atomic_op("andn %1,%0,%4;", ret, oldval, uaddr, oparg);
50                 break;
51         case FUTEX_OP_XOR:
52                 __futex_atomic_op("xor %1,%0,%4;", ret, oldval, uaddr, oparg);
53                 break;
54         default:
55                 ret = -ENOSYS;
56         }
57
58         pagefault_enable();
59
60         if (!ret)
61                 *oval = oldval;
62
63         return ret;
64 }
65
66 static inline int
67 futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
68                               u32 oldval, u32 newval)
69 {
70         int ret = 0, cmp;
71         u32 prev;
72
73         if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
74                 return -EFAULT;
75
76         __asm__ __volatile__ ("1:       lwx     %1, %3, r0;             \
77                                         cmp     %2, %1, %4;             \
78                                         bnei    %2, 3f;                 \
79                                 2:      swx     %5, %3, r0;             \
80                                         addic   %2, r0, 0;              \
81                                         bnei    %2, 1b;                 \
82                                 3:                                      \
83                                 .section .fixup,\"ax\";                 \
84                                 4:      brid    3b;                     \
85                                         addik   %0, r0, %6;             \
86                                 .previous;                              \
87                                 .section __ex_table,\"a\";              \
88                                 .word   1b,4b,2b,4b;                    \
89                                 .previous;"                             \
90                 : "+r" (ret), "=&r" (prev), "=&r"(cmp)  \
91                 : "r" (uaddr), "r" (oldval), "r" (newval), "i" (-EFAULT));
92
93         *uval = prev;
94         return ret;
95 }
96
97 #endif /* __KERNEL__ */
98
99 #endif