xref: /netbsd-src/external/gpl3/gcc.old/dist/libgomp/config/linux/x86/futex.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* Copyright (C) 2005-2015 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@redhat.com>.
3 
4    This file is part of the GNU Offloading and Multi Processing Library
5    (libgomp).
6 
7    Libgomp is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11 
12    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15    more details.
16 
17    Under Section 7 of GPL version 3, you are granted additional
18    permissions described in the GCC Runtime Library Exception, version
19    3.1, as published by the Free Software Foundation.
20 
21    You should have received a copy of the GNU General Public License and
22    a copy of the GCC Runtime Library Exception along with this program;
23    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24    <http://www.gnu.org/licenses/>.  */
25 
26 /* Provide target-specific access to the futex system call.  */
27 
28 #ifdef __x86_64__
29 # ifndef SYS_futex
30 #  define SYS_futex	202
31 # endif
32 
33 static inline void
34 futex_wait (int *addr, int val)
35 {
36   register long r10 __asm__("%r10");
37   long res;
38 
39   r10 = 0;
40   __asm volatile ("syscall"
41 		  : "=a" (res)
42 		  : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wait),
43 		    "d" (val), "r" (r10)
44 		  : "r11", "rcx", "memory");
45   if (__builtin_expect (res == -ENOSYS, 0))
46     {
47       gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
48       gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
49       r10 = 0;
50       __asm volatile ("syscall"
51 		      : "=a" (res)
52 		      : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wait),
53 			"d" (val), "r" (r10)
54 		      : "r11", "rcx", "memory");
55     }
56 }
57 
58 static inline void
59 futex_wake (int *addr, int count)
60 {
61   long res;
62 
63   __asm volatile ("syscall"
64 		  : "=a" (res)
65 		  : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wake),
66 		    "d" (count)
67 		  : "r11", "rcx", "memory");
68   if (__builtin_expect (res == -ENOSYS, 0))
69     {
70       gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
71       gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
72       __asm volatile ("syscall"
73 		      : "=a" (res)
74 		      : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wake),
75 			"d" (count)
76 		      : "r11", "rcx", "memory");
77     }
78 }
79 #else
80 # ifndef SYS_futex
81 #  define SYS_futex	240
82 # endif
83 
84 # ifdef __PIC__
85 
86 static inline long
87 sys_futex0 (int *addr, int op, int val)
88 {
89   long res;
90 
91   __asm volatile ("xchgl\t%%ebx, %2\n\t"
92 		  "int\t$0x80\n\t"
93 		  "xchgl\t%%ebx, %2"
94 		  : "=a" (res)
95 		  : "0"(SYS_futex), "r" (addr), "c"(op),
96 		    "d"(val), "S"(0)
97 		  : "memory");
98   return res;
99 }
100 
101 # else
102 
103 static inline long
104 sys_futex0 (int *addr, int op, int val)
105 {
106   long res;
107 
108   __asm volatile ("int $0x80"
109 		  : "=a" (res)
110 		  : "0"(SYS_futex), "b" (addr), "c"(op),
111 		    "d"(val), "S"(0)
112 		  : "memory");
113   return res;
114 }
115 
116 # endif /* __PIC__ */
117 
118 static inline void
119 futex_wait (int *addr, int val)
120 {
121   long res = sys_futex0 (addr, gomp_futex_wait, val);
122   if (__builtin_expect (res == -ENOSYS, 0))
123     {
124       gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
125       gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
126       sys_futex0 (addr, gomp_futex_wait, val);
127     }
128 }
129 
130 static inline void
131 futex_wake (int *addr, int count)
132 {
133   long res = sys_futex0 (addr, gomp_futex_wake, count);
134   if (__builtin_expect (res == -ENOSYS, 0))
135     {
136       gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
137       gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
138       sys_futex0 (addr, gomp_futex_wake, count);
139     }
140 }
141 
142 #endif /* __x86_64__ */
143 
144 static inline void
145 cpu_relax (void)
146 {
147   __builtin_ia32_pause ();
148 }
149