[SCSI] libiscsi: fix session age rollover and remove cid encoding
[linux-2.6] / include / asm-x86 / futex.h
1 #ifndef _ASM_X86_FUTEX_H
2 #define _ASM_X86_FUTEX_H
3
4 #ifdef __KERNEL__
5
6 #include <linux/futex.h>
7
8 #include <asm/asm.h>
9 #include <asm/errno.h>
10 #include <asm/processor.h>
11 #include <asm/system.h>
12 #include <asm/uaccess.h>
13
14 #define __futex_atomic_op1(insn, ret, oldval, uaddr, oparg)     \
15   __asm__ __volatile(                                           \
16 "1:     " insn "\n"                                             \
17 "2:     .section .fixup,\"ax\"\n                                \
18 3:      mov     %3, %1\n                                        \
19         jmp     2b\n                                            \
20         .previous\n"                                            \
21         _ASM_EXTABLE(1b,3b)                                     \
22         : "=r" (oldval), "=r" (ret), "+m" (*uaddr)              \
23         : "i" (-EFAULT), "0" (oparg), "1" (0))
24
25 #define __futex_atomic_op2(insn, ret, oldval, uaddr, oparg)     \
26   __asm__ __volatile(                                           \
27 "1:     movl    %2, %0\n                                        \
28         movl    %0, %3\n"                                       \
29         insn "\n"                                               \
30 "2:     lock; cmpxchgl %3, %2\n                                 \
31         jnz     1b\n                                            \
32 3:      .section .fixup,\"ax\"\n                                \
33 4:      mov     %5, %1\n                                        \
34         jmp     3b\n                                            \
35         .previous\n"                                            \
36         _ASM_EXTABLE(1b,4b)                                     \
37         _ASM_EXTABLE(2b,4b)                                     \
38         : "=&a" (oldval), "=&r" (ret), "+m" (*uaddr),           \
39           "=&r" (tem)                                           \
40         : "r" (oparg), "i" (-EFAULT), "1" (0))
41
42 static inline int
43 futex_atomic_op_inuser(int encoded_op, int __user *uaddr)
44 {
45         int op = (encoded_op >> 28) & 7;
46         int cmp = (encoded_op >> 24) & 15;
47         int oparg = (encoded_op << 8) >> 20;
48         int cmparg = (encoded_op << 20) >> 20;
49         int oldval = 0, ret, tem;
50
51         if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
52                 oparg = 1 << oparg;
53
54         if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
55                 return -EFAULT;
56
57 #if defined(CONFIG_X86_32) && !defined(CONFIG_X86_BSWAP)
58         /* Real i386 machines can only support FUTEX_OP_SET */
59         if (op != FUTEX_OP_SET && boot_cpu_data.x86 == 3)
60                 return -ENOSYS;
61 #endif
62
63         pagefault_disable();
64
65         switch (op) {
66         case FUTEX_OP_SET:
67                 __futex_atomic_op1("xchgl %0, %2", ret, oldval, uaddr, oparg);
68                 break;
69         case FUTEX_OP_ADD:
70                 __futex_atomic_op1("lock; xaddl %0, %2", ret, oldval,
71                                    uaddr, oparg);
72                 break;
73         case FUTEX_OP_OR:
74                 __futex_atomic_op2("orl %4, %3", ret, oldval, uaddr, oparg);
75                 break;
76         case FUTEX_OP_ANDN:
77                 __futex_atomic_op2("andl %4, %3", ret, oldval, uaddr, ~oparg);
78                 break;
79         case FUTEX_OP_XOR:
80                 __futex_atomic_op2("xorl %4, %3", ret, oldval, uaddr, oparg);
81                 break;
82         default:
83                 ret = -ENOSYS;
84         }
85
86         pagefault_enable();
87
88         if (!ret) {
89                 switch (cmp) {
90                 case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
91                 case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
92                 case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
93                 case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
94                 case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
95                 case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
96                 default: ret = -ENOSYS;
97                 }
98         }
99         return ret;
100 }
101
102 static inline int
103 futex_atomic_cmpxchg_inatomic(int __user *uaddr, int oldval, int newval)
104 {
105         if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
106                 return -EFAULT;
107
108         __asm__ __volatile__(
109                 "1:     lock; cmpxchgl %3, %1                   \n"
110                 "2:     .section .fixup, \"ax\"                 \n"
111                 "3:     mov     %2, %0                          \n"
112                 "       jmp     2b                              \n"
113                 "       .previous                               \n"
114                 _ASM_EXTABLE(1b,3b)
115                 : "=a" (oldval), "+m" (*uaddr)
116                 : "i" (-EFAULT), "r" (newval), "0" (oldval)
117                 : "memory"
118         );
119
120         return oldval;
121 }
122
123 #endif
124 #endif