1 /* Copyright (C) 2002-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
22 #include <stap-probe.h>
26 # include <sys/param.h>
27 # include <bits/pthreadtypes.h>
28 # include <kernel-features.h>
29 # include <tcb-offsets.h>
33 # define LOCK_INSTR /* nothing */
35 # define LOCK_INSTR "lock;"
51 #define FUTEX_CMP_REQUEUE 4
52 #define FUTEX_WAKE_OP 5
53 #define FUTEX_LOCK_PI 6
54 #define FUTEX_UNLOCK_PI 7
55 #define FUTEX_TRYLOCK_PI 8
56 #define FUTEX_WAIT_BITSET 9
57 #define FUTEX_WAKE_BITSET 10
58 #define FUTEX_WAIT_REQUEUE_PI 11
59 #define FUTEX_CMP_REQUEUE_PI 12
60 #define FUTEX_PRIVATE_FLAG 128
61 #define FUTEX_CLOCK_REALTIME 256
63 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
65 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
67 /* Values for 'private' parameter of locking macros. Yes, the
68 definition seems to be backwards. But it is not. The bit will be
69 reversed before passing to the system call. */
71 #define LLL_SHARED FUTEX_PRIVATE_FLAG
74 #if !defined NOT_IN_libc || defined IS_IN_rtld
75 /* In libc.so or ld.so all futexes are private. */
76 # ifdef __ASSUME_PRIVATE_FUTEX
77 # define __lll_private_flag(fl, private) \
78 ((fl) | FUTEX_PRIVATE_FLAG)
80 # define __lll_private_flag(fl, private) \
81 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
84 # ifdef __ASSUME_PRIVATE_FUTEX
85 # define __lll_private_flag(fl, private) \
86 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
88 # define __lll_private_flag(fl, private) \
89 (__builtin_constant_p (private) \
91 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
93 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
94 asm ("andl %%gs:%P1, %0" : "+r" (__fl) \
95 : "i" (offsetof (struct pthread, header.private_futex))); \
100 #ifndef __ASSEMBLER__
102 /* Initializer for compatibility lock. */
103 #define LLL_LOCK_INITIALIZER (0)
104 #define LLL_LOCK_INITIALIZER_LOCKED (1)
105 #define LLL_LOCK_INITIALIZER_WAITERS (2)
109 # define LLL_EBX_LOAD "xchgl %2, %%ebx\n"
110 # define LLL_EBX_REG "D"
112 # define LLL_EBX_LOAD
113 # define LLL_EBX_REG "b"
116 #ifdef I386_USE_SYSENTER
118 # define LLL_ENTER_KERNEL "call *%%gs:%P6\n\t"
120 # define LLL_ENTER_KERNEL "call *_dl_sysinfo\n\t"
123 # define LLL_ENTER_KERNEL "int $0x80\n\t"
126 /* Delay in spinlock loop. */
127 #define BUSY_WAIT_NOP asm ("rep; nop")
130 #define LLL_STUB_UNWIND_INFO_START \
131 ".section .eh_frame,\"a\",@progbits\n" \
132 "5:\t" ".long 7f-6f # Length of Common Information Entry\n" \
133 "6:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
134 ".byte 0x1 # CIE Version\n\t" \
135 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
136 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
137 ".sleb128 -4 # CIE Data Alignment Factor\n\t" \
138 ".byte 0x8 # CIE RA Column\n\t" \
139 ".uleb128 0x1 # Augmentation size\n\t" \
140 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
141 ".byte 0xc # DW_CFA_def_cfa\n\t" \
145 "7:\t" ".long 17f-8f # FDE Length\n" \
146 "8:\t" ".long 8b-5b # FDE CIE offset\n\t" \
147 ".long 1b-. # FDE initial location\n\t" \
148 ".long 4b-1b # FDE address range\n\t" \
149 ".uleb128 0x0 # Augmentation size\n\t" \
150 ".byte 0x16 # DW_CFA_val_expression\n\t" \
152 ".uleb128 10f-9f\n" \
153 "9:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
155 #define LLL_STUB_UNWIND_INFO_END \
156 ".byte 0x16 # DW_CFA_val_expression\n\t" \
158 ".uleb128 12f-11f\n" \
159 "11:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
161 "12:\t" ".byte 0x40 + (3b-2b-1) # DW_CFA_advance_loc\n\t" \
162 ".byte 0x16 # DW_CFA_val_expression\n\t" \
164 ".uleb128 16f-13f\n" \
165 "13:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
166 ".sleb128 15f-14f\n\t" \
167 ".byte 0x0d # DW_OP_const4s\n" \
168 "14:\t" ".4byte 3b-.\n\t" \
169 ".byte 0x1c # DW_OP_minus\n\t" \
170 ".byte 0x0d # DW_OP_const4s\n" \
171 "15:\t" ".4byte 18f-.\n\t" \
172 ".byte 0x22 # DW_OP_plus\n" \
173 "16:\t" ".align 4\n" \
174 "17:\t" ".previous\n"
182 #define LLL_STUB_UNWIND_INFO_3 \
183 LLL_STUB_UNWIND_INFO_START \
184 "10:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
185 LLL_STUB_UNWIND_INFO_END
194 #define LLL_STUB_UNWIND_INFO_4 \
195 LLL_STUB_UNWIND_INFO_START \
196 "10:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
197 ".byte 0x16 # DW_CFA_val_expression\n\t" \
199 ".uleb128 20f-19f\n" \
200 "19:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
202 "20:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
203 LLL_STUB_UNWIND_INFO_END
206 #define lll_futex_wait(futex, val, private) \
207 lll_futex_timed_wait (futex, val, NULL, private)
210 #define lll_futex_timed_wait(futex, val, timeout, private) \
213 register __typeof (val) _val asm ("edx") = (val); \
214 __asm __volatile (LLL_EBX_LOAD \
218 : "0" (SYS_futex), LLL_EBX_REG (futex), "S" (timeout), \
219 "c" (__lll_private_flag (FUTEX_WAIT, private)), \
220 "d" (_val), "i" (offsetof (tcbhead_t, sysinfo)) \
226 #define lll_futex_wake(futex, nr, private) \
229 register __typeof (nr) _nr asm ("edx") = (nr); \
230 LIBC_PROBE (lll_futex_wake, 3, futex, nr, private); \
231 __asm __volatile (LLL_EBX_LOAD \
235 : "0" (SYS_futex), LLL_EBX_REG (futex), \
236 "c" (__lll_private_flag (FUTEX_WAKE, private)), \
238 "i" (0) /* phony, to align next arg's number */, \
239 "i" (offsetof (tcbhead_t, sysinfo))); \
243 /* NB: in the lll_trylock macro we simply return the value in %eax
244 after the cmpxchg instruction. In case the operation succeded this
245 value is zero. In case the operation failed, the cmpxchg instruction
246 has loaded the current value of the memory work which is guaranteed
248 #if defined NOT_IN_libc || defined UP
249 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
251 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
254 "0:\tcmpxchgl %2, %1"
257 #define lll_trylock(futex) \
259 __asm __volatile (__lll_trylock_asm \
260 : "=a" (ret), "=m" (futex) \
261 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
262 "0" (LLL_LOCK_INITIALIZER), \
263 "i" (MULTIPLE_THREADS_OFFSET) \
267 #define lll_robust_trylock(futex, id) \
269 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
270 : "=a" (ret), "=m" (futex) \
271 : "r" (id), "m" (futex), \
272 "0" (LLL_LOCK_INITIALIZER) \
277 #define lll_cond_trylock(futex) \
279 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
280 : "=a" (ret), "=m" (futex) \
281 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
282 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
286 #if defined NOT_IN_libc || defined UP
287 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
289 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
292 "0:\tcmpxchgl %1, %2\n\t"
295 #define lll_lock(futex, private) \
297 ({ int ignore1, ignore2; \
298 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
299 __asm __volatile (__lll_lock_asm_start \
300 "jnz _L_lock_%=\n\t" \
301 ".subsection 1\n\t" \
302 ".type _L_lock_%=,@function\n" \
304 "1:\tleal %2, %%ecx\n" \
305 "2:\tcall __lll_lock_wait_private\n" \
307 "4:\t.size _L_lock_%=, 4b-1b\n\t" \
309 LLL_STUB_UNWIND_INFO_3 \
311 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
312 : "0" (0), "1" (1), "m" (futex), \
313 "i" (MULTIPLE_THREADS_OFFSET) \
318 __asm __volatile (__lll_lock_asm_start \
319 "jnz _L_lock_%=\n\t" \
320 ".subsection 1\n\t" \
321 ".type _L_lock_%=,@function\n" \
323 "1:\tleal %2, %%edx\n" \
324 "0:\tmovl %8, %%ecx\n" \
325 "2:\tcall __lll_lock_wait\n" \
327 "4:\t.size _L_lock_%=, 4b-1b\n\t" \
329 LLL_STUB_UNWIND_INFO_4 \
331 : "=a" (ignore1), "=c" (ignore2), \
332 "=m" (futex), "=&d" (ignore3) \
333 : "1" (1), "m" (futex), \
334 "i" (MULTIPLE_THREADS_OFFSET), "0" (0), \
335 "g" ((int) (private)) \
340 #define lll_robust_lock(futex, id, private) \
341 ({ int result, ignore1, ignore2; \
342 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
343 "jnz _L_robust_lock_%=\n\t" \
344 ".subsection 1\n\t" \
345 ".type _L_robust_lock_%=,@function\n" \
346 "_L_robust_lock_%=:\n" \
347 "1:\tleal %2, %%edx\n" \
348 "0:\tmovl %7, %%ecx\n" \
349 "2:\tcall __lll_robust_lock_wait\n" \
351 "4:\t.size _L_robust_lock_%=, 4b-1b\n\t" \
353 LLL_STUB_UNWIND_INFO_4 \
355 : "=a" (result), "=c" (ignore1), "=m" (futex), \
357 : "0" (0), "1" (id), "m" (futex), "g" ((int) (private))\
362 /* Special version of lll_lock which causes the unlock function to
363 always wakeup waiters. */
364 #define lll_cond_lock(futex, private) \
366 ({ int ignore1, ignore2, ignore3; \
367 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
368 "jnz _L_cond_lock_%=\n\t" \
369 ".subsection 1\n\t" \
370 ".type _L_cond_lock_%=,@function\n" \
371 "_L_cond_lock_%=:\n" \
372 "1:\tleal %2, %%edx\n" \
373 "0:\tmovl %7, %%ecx\n" \
374 "2:\tcall __lll_lock_wait\n" \
376 "4:\t.size _L_cond_lock_%=, 4b-1b\n\t" \
378 LLL_STUB_UNWIND_INFO_4 \
380 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
382 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
387 #define lll_robust_cond_lock(futex, id, private) \
388 ({ int result, ignore1, ignore2; \
389 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
390 "jnz _L_robust_cond_lock_%=\n\t" \
391 ".subsection 1\n\t" \
392 ".type _L_robust_cond_lock_%=,@function\n" \
393 "_L_robust_cond_lock_%=:\n" \
394 "1:\tleal %2, %%edx\n" \
395 "0:\tmovl %7, %%ecx\n" \
396 "2:\tcall __lll_robust_lock_wait\n" \
398 "4:\t.size _L_robust_cond_lock_%=, 4b-1b\n\t" \
400 LLL_STUB_UNWIND_INFO_4 \
402 : "=a" (result), "=c" (ignore1), "=m" (futex), \
404 : "0" (0), "1" (id | FUTEX_WAITERS), "m" (futex), \
405 "g" ((int) (private)) \
410 #define lll_timedlock(futex, timeout, private) \
411 ({ int result, ignore1, ignore2, ignore3; \
412 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
413 "jnz _L_timedlock_%=\n\t" \
414 ".subsection 1\n\t" \
415 ".type _L_timedlock_%=,@function\n" \
416 "_L_timedlock_%=:\n" \
417 "1:\tleal %3, %%ecx\n" \
418 "0:\tmovl %8, %%edx\n" \
419 "2:\tcall __lll_timedlock_wait\n" \
421 "4:\t.size _L_timedlock_%=, 4b-1b\n\t" \
423 LLL_STUB_UNWIND_INFO_4 \
425 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
426 "=m" (futex), "=S" (ignore3) \
427 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
428 "4" ((int) (private)) \
433 #define lll_robust_timedlock(futex, timeout, id, private) \
434 ({ int result, ignore1, ignore2, ignore3; \
435 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
436 "jnz _L_robust_timedlock_%=\n\t" \
437 ".subsection 1\n\t" \
438 ".type _L_robust_timedlock_%=,@function\n" \
439 "_L_robust_timedlock_%=:\n" \
440 "1:\tleal %3, %%ecx\n" \
441 "0:\tmovl %8, %%edx\n" \
442 "2:\tcall __lll_robust_timedlock_wait\n" \
444 "4:\t.size _L_robust_timedlock_%=, 4b-1b\n\t" \
446 LLL_STUB_UNWIND_INFO_4 \
448 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
449 "=m" (futex), "=S" (ignore3) \
450 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
451 "4" ((int) (private)) \
455 #if defined NOT_IN_libc || defined UP
456 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
458 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
464 #define lll_unlock(futex, private) \
467 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
468 __asm __volatile (__lll_unlock_asm \
469 "jne _L_unlock_%=\n\t" \
470 ".subsection 1\n\t" \
471 ".type _L_unlock_%=,@function\n" \
473 "1:\tleal %0, %%eax\n" \
474 "2:\tcall __lll_unlock_wake_private\n" \
476 "4:\t.size _L_unlock_%=, 4b-1b\n\t" \
478 LLL_STUB_UNWIND_INFO_3 \
480 : "=m" (futex), "=&a" (ignore) \
481 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
486 __asm __volatile (__lll_unlock_asm \
487 "jne _L_unlock_%=\n\t" \
488 ".subsection 1\n\t" \
489 ".type _L_unlock_%=,@function\n" \
491 "1:\tleal %0, %%eax\n" \
492 "0:\tmovl %5, %%ecx\n" \
493 "2:\tcall __lll_unlock_wake\n" \
495 "4:\t.size _L_unlock_%=, 4b-1b\n\t" \
497 LLL_STUB_UNWIND_INFO_4 \
499 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
500 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
501 "g" ((int) (private)) \
506 #define lll_robust_unlock(futex, private) \
508 ({ int ignore, ignore2; \
509 __asm __volatile (LOCK_INSTR "andl %3, %0\n\t" \
510 "jne _L_robust_unlock_%=\n\t" \
511 ".subsection 1\n\t" \
512 ".type _L_robust_unlock_%=,@function\n" \
513 "_L_robust_unlock_%=:\n\t" \
514 "1:\tleal %0, %%eax\n" \
515 "0:\tmovl %5, %%ecx\n" \
516 "2:\tcall __lll_unlock_wake\n" \
518 "4:\t.size _L_robust_unlock_%=, 4b-1b\n\t" \
520 LLL_STUB_UNWIND_INFO_4 \
522 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
523 : "i" (FUTEX_WAITERS), "m" (futex), \
524 "g" ((int) (private)) \
529 #define lll_robust_dead(futex, private) \
532 register int _nr asm ("edx") = 1; \
533 __asm __volatile (LOCK_INSTR "orl %5, (%2)\n\t" \
538 : "0" (SYS_futex), LLL_EBX_REG (&(futex)), \
539 "c" (__lll_private_flag (FUTEX_WAKE, private)), \
540 "d" (_nr), "i" (FUTEX_OWNER_DIED), \
541 "i" (offsetof (tcbhead_t, sysinfo))); \
544 #define lll_islocked(futex) \
545 (futex != LLL_LOCK_INITIALIZER)
547 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
548 wakeup when the clone terminates. The memory location contains the
549 thread ID while the clone is running and is reset to zero
552 The macro parameter must not have any side effect. */
553 #define lll_wait_tid(tid) \
556 register __typeof (tid) _tid asm ("edx") = (tid); \
558 __asm __volatile (LLL_EBX_LOAD \
559 "1:\tmovl %1, %%eax\n\t" \
561 "cmpl $0, (%%ebx)\n\t" \
565 : "i" (SYS_futex), LLL_EBX_REG (&tid), "S" (0), \
566 "c" (FUTEX_WAIT), "d" (_tid), \
567 "i" (offsetof (tcbhead_t, sysinfo)) \
571 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
572 __attribute__ ((regparm (2))) attribute_hidden
;
573 #define lll_timedwait_tid(tid, abstime) \
578 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
581 __result = __lll_timedwait_tid (&tid, abstime); \
585 #endif /* !__ASSEMBLER__ */
587 #endif /* lowlevellock.h */