1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#ifndef _ASM_X86_RWSEM_H
33#define _ASM_X86_RWSEM_H
34
35#ifndef _LINUX_RWSEM_H
36#error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
37#endif
38
39#ifdef __KERNEL__
40#include <asm/asm.h>
41
42
43
44
45
46
47
48#ifdef CONFIG_X86_64
49# define RWSEM_ACTIVE_MASK 0xffffffffL
50#else
51# define RWSEM_ACTIVE_MASK 0x0000ffffL
52#endif
53
54#define RWSEM_UNLOCKED_VALUE 0x00000000L
55#define RWSEM_ACTIVE_BIAS 0x00000001L
56#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
57#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
58#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
59
60
61
62
63static inline void __down_read(struct rw_semaphore *sem)
64{
65 asm volatile("# beginning down_read\n\t"
66 LOCK_PREFIX _ASM_INC "(%1)\n\t"
67
68 " jns 1f\n"
69 " call call_rwsem_down_read_failed\n"
70 "1:\n\t"
71 "# ending down_read\n\t"
72 : "+m" (sem->count)
73 : "a" (sem)
74 : "memory", "cc");
75}
76
77
78
79
80static inline bool __down_read_trylock(struct rw_semaphore *sem)
81{
82 long result, tmp;
83 asm volatile("# beginning __down_read_trylock\n\t"
84 " mov %0,%1\n\t"
85 "1:\n\t"
86 " mov %1,%2\n\t"
87 " add %3,%2\n\t"
88 " jle 2f\n\t"
89 LOCK_PREFIX " cmpxchg %2,%0\n\t"
90 " jnz 1b\n\t"
91 "2:\n\t"
92 "# ending __down_read_trylock\n\t"
93 : "+m" (sem->count), "=&a" (result), "=&r" (tmp)
94 : "i" (RWSEM_ACTIVE_READ_BIAS)
95 : "memory", "cc");
96 return result >= 0;
97}
98
99
100
101
102#define ____down_write(sem, slow_path) \
103({ \
104 long tmp; \
105 struct rw_semaphore* ret; \
106 register void *__sp asm(_ASM_SP); \
107 \
108 asm volatile("# beginning down_write\n\t" \
109 LOCK_PREFIX " xadd %1,(%4)\n\t" \
110 \
111 " test " __ASM_SEL(%w1,%k1) "," __ASM_SEL(%w1,%k1) "\n\t" \
112 \
113 " jz 1f\n" \
114 " call " slow_path "\n" \
115 "1:\n" \
116 "# ending down_write" \
117 : "+m" (sem->count), "=d" (tmp), "=a" (ret), "+r" (__sp) \
118 : "a" (sem), "1" (RWSEM_ACTIVE_WRITE_BIAS) \
119 : "memory", "cc"); \
120 ret; \
121})
122
123static inline void __down_write(struct rw_semaphore *sem)
124{
125 ____down_write(sem, "call_rwsem_down_write_failed");
126}
127
128static inline int __down_write_killable(struct rw_semaphore *sem)
129{
130 if (IS_ERR(____down_write(sem, "call_rwsem_down_write_failed_killable")))
131 return -EINTR;
132
133 return 0;
134}
135
136
137
138
139static inline bool __down_write_trylock(struct rw_semaphore *sem)
140{
141 bool result;
142 long tmp0, tmp1;
143 asm volatile("# beginning __down_write_trylock\n\t"
144 " mov %0,%1\n\t"
145 "1:\n\t"
146 " test " __ASM_SEL(%w1,%k1) "," __ASM_SEL(%w1,%k1) "\n\t"
147
148 " jnz 2f\n\t"
149 " mov %1,%2\n\t"
150 " add %4,%2\n\t"
151 LOCK_PREFIX " cmpxchg %2,%0\n\t"
152 " jnz 1b\n\t"
153 "2:\n\t"
154 CC_SET(e)
155 "# ending __down_write_trylock\n\t"
156 : "+m" (sem->count), "=&a" (tmp0), "=&r" (tmp1),
157 CC_OUT(e) (result)
158 : "er" (RWSEM_ACTIVE_WRITE_BIAS)
159 : "memory");
160 return result;
161}
162
163
164
165
166static inline void __up_read(struct rw_semaphore *sem)
167{
168 long tmp;
169 asm volatile("# beginning __up_read\n\t"
170 LOCK_PREFIX " xadd %1,(%2)\n\t"
171
172 " jns 1f\n\t"
173 " call call_rwsem_wake\n"
174 "1:\n"
175 "# ending __up_read\n"
176 : "+m" (sem->count), "=d" (tmp)
177 : "a" (sem), "1" (-RWSEM_ACTIVE_READ_BIAS)
178 : "memory", "cc");
179}
180
181
182
183
184static inline void __up_write(struct rw_semaphore *sem)
185{
186 long tmp;
187 asm volatile("# beginning __up_write\n\t"
188 LOCK_PREFIX " xadd %1,(%2)\n\t"
189
190 " jns 1f\n\t"
191 " call call_rwsem_wake\n"
192 "1:\n\t"
193 "# ending __up_write\n"
194 : "+m" (sem->count), "=d" (tmp)
195 : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
196 : "memory", "cc");
197}
198
199
200
201
202static inline void __downgrade_write(struct rw_semaphore *sem)
203{
204 asm volatile("# beginning __downgrade_write\n\t"
205 LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t"
206
207
208
209
210 " jns 1f\n\t"
211 " call call_rwsem_downgrade_wake\n"
212 "1:\n\t"
213 "# ending __downgrade_write\n"
214 : "+m" (sem->count)
215 : "a" (sem), "er" (-RWSEM_WAITING_BIAS)
216 : "memory", "cc");
217}
218
219#endif
220#endif
221