1 /*
2 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef OS_CPU_WINDOWS_X86_ATOMIC_WINDOWS_X86_HPP
26 #define OS_CPU_WINDOWS_X86_ATOMIC_WINDOWS_X86_HPP
27
28 #include "runtime/os.hpp"
29
30 // Note that in MSVC, volatile memory accesses are explicitly
31 // guaranteed to have acquire release semantics (w.r.t. compiler
32 // reordering) and therefore does not even need a compiler barrier
33 // for normal acquire release accesses. And all generalized
34 // bound calls like release_store go through Atomic::load
35 // and Atomic::store which do volatile memory accesses.
36 template<> inline void ScopedFence<X_ACQUIRE>::postfix() { }
37 template<> inline void ScopedFence<RELEASE_X>::prefix() { }
38 template<> inline void ScopedFence<RELEASE_X_FENCE>::prefix() { }
39 template<> inline void ScopedFence<RELEASE_X_FENCE>::postfix() { OrderAccess::fence(); }
40
41 // The following alternative implementations are needed because
42 // Windows 95 doesn't support (some of) the corresponding Windows NT
43 // calls. Furthermore, these versions allow inlining in the caller.
44 // (More precisely: The documentation for InterlockedExchange says
45 // it is supported for Windows 95. However, when single-stepping
46 // through the assembly code we cannot step into the routine and
47 // when looking at the routine address we see only garbage code.
48 // Better safe then sorry!). Was bug 7/31/98 (gri).
49 //
50 // Performance note: On uniprocessors, the 'lock' prefixes are not
51 // necessary (and expensive). We should generate separate cases if
52 // this becomes a performance problem.
53
54 #pragma warning(disable: 4035) // Disables warnings reporting missing return statement
55
56 template<size_t byte_size>
57 struct Atomic::PlatformAdd {
58 template<typename D, typename I>
59 D add_and_fetch(D volatile* dest, I add_value, atomic_memory_order order) const;
60
61 template<typename D, typename I>
62 D fetch_and_add(D volatile* dest, I add_value, atomic_memory_order order) const {
63 return add_and_fetch(dest, add_value, order) - add_value;
64 }
65 };
66
67 #ifdef AMD64
68 template<>
69 template<typename D, typename I>
70 inline D Atomic::PlatformAdd<4>::add_and_fetch(D volatile* dest, I add_value,
71 atomic_memory_order order) const {
72 return add_using_helper<int32_t>(os::atomic_add_func, dest, add_value);
73 }
74
75 template<>
76 template<typename D, typename I>
77 inline D Atomic::PlatformAdd<8>::add_and_fetch(D volatile* dest, I add_value,
78 atomic_memory_order order) const {
79 return add_using_helper<int64_t>(os::atomic_add_long_func, dest, add_value);
80 }
81
82 #define DEFINE_STUB_XCHG(ByteSize, StubType, StubName) \
83 template<> \
84 template<typename T> \
85 inline T Atomic::PlatformXchg<ByteSize>::operator()(T volatile* dest, \
86 T exchange_value, \
87 atomic_memory_order order) const { \
88 STATIC_ASSERT(ByteSize == sizeof(T)); \
89 return xchg_using_helper<StubType>(StubName, dest, exchange_value); \
90 }
91
92 DEFINE_STUB_XCHG(4, int32_t, os::atomic_xchg_func)
93 DEFINE_STUB_XCHG(8, int64_t, os::atomic_xchg_long_func)
94
95 #undef DEFINE_STUB_XCHG
96
97 #define DEFINE_STUB_CMPXCHG(ByteSize, StubType, StubName) \
98 template<> \
99 template<typename T> \
100 inline T Atomic::PlatformCmpxchg<ByteSize>::operator()(T volatile* dest, \
101 T compare_value, \
102 T exchange_value, \
103 atomic_memory_order order) const { \
104 STATIC_ASSERT(ByteSize == sizeof(T)); \
105 return cmpxchg_using_helper<StubType>(StubName, dest, compare_value, exchange_value); \
106 }
107
108 DEFINE_STUB_CMPXCHG(1, int8_t, os::atomic_cmpxchg_byte_func)
109 DEFINE_STUB_CMPXCHG(4, int32_t, os::atomic_cmpxchg_func)
110 DEFINE_STUB_CMPXCHG(8, int64_t, os::atomic_cmpxchg_long_func)
111
112 #undef DEFINE_STUB_CMPXCHG
113
114 #else // !AMD64
115
116 template<>
117 template<typename D, typename I>
118 inline D Atomic::PlatformAdd<4>::add_and_fetch(D volatile* dest, I add_value,
119 atomic_memory_order order) const {
120 STATIC_ASSERT(4 == sizeof(I));
121 STATIC_ASSERT(4 == sizeof(D));
122 __asm {
123 mov edx, dest;
124 mov eax, add_value;
125 mov ecx, eax;
126 lock xadd dword ptr [edx], eax;
127 add eax, ecx;
128 }
129 }
130
131 template<>
132 template<typename T>
133 inline T Atomic::PlatformXchg<4>::operator()(T volatile* dest,
134 T exchange_value,
135 atomic_memory_order order) const {
136 STATIC_ASSERT(4 == sizeof(T));
137 // alternative for InterlockedExchange
138 __asm {
139 mov eax, exchange_value;
140 mov ecx, dest;
141 xchg eax, dword ptr [ecx];
142 }
143 }
144
145 template<>
146 template<typename T>
147 inline T Atomic::PlatformCmpxchg<1>::operator()(T volatile* dest,
148 T compare_value,
149 T exchange_value,
150 atomic_memory_order order) const {
151 STATIC_ASSERT(1 == sizeof(T));
152 // alternative for InterlockedCompareExchange
153 __asm {
154 mov edx, dest
155 mov cl, exchange_value
156 mov al, compare_value
157 lock cmpxchg byte ptr [edx], cl
158 }
159 }
160
161 template<>
162 template<typename T>
163 inline T Atomic::PlatformCmpxchg<4>::operator()(T volatile* dest,
164 T compare_value,
165 T exchange_value,
166 atomic_memory_order order) const {
167 STATIC_ASSERT(4 == sizeof(T));
168 // alternative for InterlockedCompareExchange
169 __asm {
170 mov edx, dest
171 mov ecx, exchange_value
172 mov eax, compare_value
173 lock cmpxchg dword ptr [edx], ecx
174 }
175 }
176
177 template<>
178 template<typename T>
179 inline T Atomic::PlatformCmpxchg<8>::operator()(T volatile* dest,
180 T compare_value,
181 T exchange_value,
182 atomic_memory_order order) const {
183 STATIC_ASSERT(8 == sizeof(T));
184 int32_t ex_lo = (int32_t)exchange_value;
185 int32_t ex_hi = *( ((int32_t*)&exchange_value) + 1 );
186 int32_t cmp_lo = (int32_t)compare_value;
187 int32_t cmp_hi = *( ((int32_t*)&compare_value) + 1 );
188 __asm {
189 push ebx
190 push edi
191 mov eax, cmp_lo
192 mov edx, cmp_hi
193 mov edi, dest
194 mov ebx, ex_lo
195 mov ecx, ex_hi
196 lock cmpxchg8b qword ptr [edi]
197 pop edi
198 pop ebx
199 }
200 }
201
202 template<>
203 template<typename T>
204 inline T Atomic::PlatformLoad<8>::operator()(T const volatile* src) const {
205 STATIC_ASSERT(8 == sizeof(T));
206 volatile T dest;
207 volatile T* pdest = &dest;
208 __asm {
209 mov eax, src
210 fild qword ptr [eax]
211 mov eax, pdest
212 fistp qword ptr [eax]
213 }
214 return dest;
215 }
216
217 template<>
218 template<typename T>
219 inline void Atomic::PlatformStore<8>::operator()(T volatile* dest,
220 T store_value) const {
221 STATIC_ASSERT(8 == sizeof(T));
222 volatile T* src = &store_value;
223 __asm {
224 mov eax, src
225 fild qword ptr [eax]
226 mov eax, dest
227 fistp qword ptr [eax]
228 }
229 }
230
231 #endif // AMD64
232
233 #pragma warning(default: 4035) // Enables warnings reporting missing return statement
234
235 #ifndef AMD64
236 template<>
237 struct Atomic::PlatformOrderedStore<1, RELEASE_X_FENCE>
238 {
239 template <typename T>
240 void operator()(volatile T* p, T v) const {
241 __asm {
242 mov edx, p;
243 mov al, v;
244 xchg al, byte ptr [edx];
245 }
246 }
247 };
248
249 template<>
250 struct Atomic::PlatformOrderedStore<2, RELEASE_X_FENCE>
251 {
252 template <typename T>
253 void operator()(volatile T* p, T v) const {
254 __asm {
255 mov edx, p;
|
1 /*
2 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef OS_CPU_WINDOWS_X86_ATOMIC_WINDOWS_X86_HPP
26 #define OS_CPU_WINDOWS_X86_ATOMIC_WINDOWS_X86_HPP
27
28 #include <intrin.h>
29 #include "runtime/os.hpp"
30
31 // Note that in MSVC, volatile memory accesses are explicitly
32 // guaranteed to have acquire release semantics (w.r.t. compiler
33 // reordering) and therefore does not even need a compiler barrier
34 // for normal acquire release accesses. And all generalized
35 // bound calls like release_store go through Atomic::load
36 // and Atomic::store which do volatile memory accesses.
37 template<> inline void ScopedFence<X_ACQUIRE>::postfix() { }
38 template<> inline void ScopedFence<RELEASE_X>::prefix() { }
39 template<> inline void ScopedFence<RELEASE_X_FENCE>::prefix() { }
40 template<> inline void ScopedFence<RELEASE_X_FENCE>::postfix() { OrderAccess::fence(); }
41
42 template<size_t byte_size>
43 struct Atomic::PlatformAdd {
44 template<typename D, typename I>
45 D add_and_fetch(D volatile* dest, I add_value, atomic_memory_order order) const;
46
47 template<typename D, typename I>
48 D fetch_and_add(D volatile* dest, I add_value, atomic_memory_order order) const {
49 return add_and_fetch(dest, add_value, order) - add_value;
50 }
51 };
52
53 // The Interlocked* APIs only take long and will not accept __int32. That is
54 // acceptable on Windows, since long is a 32-bits integer type.
55
56 #define DEFINE_INTRINSIC_ADD(IntrinsicName, IntrinsicType) \
57 template<> \
58 template<typename D, typename I> \
59 inline D Atomic::PlatformAdd<sizeof(IntrinsicType)>::add_and_fetch(D volatile* dest, \
60 I add_value, \
61 atomic_memory_order order) const { \
62 STATIC_ASSERT(sizeof(IntrinsicType) == sizeof(D)); \
63 return PrimitiveConversions::cast<D>( \
64 IntrinsicName(reinterpret_cast<IntrinsicType volatile *>(dest), \
65 PrimitiveConversions::cast<IntrinsicType>(add_value))); \
66 }
67
68 DEFINE_INTRINSIC_ADD(InterlockedAdd, long)
69 DEFINE_INTRINSIC_ADD(InterlockedAdd64, __int64)
70
71 #undef DEFINE_INTRINSIC_ADD
72
73 #define DEFINE_INTRINSIC_XCHG(IntrinsicName, IntrinsicType) \
74 template<> \
75 template<typename T> \
76 inline T Atomic::PlatformXchg<sizeof(IntrinsicType)>::operator()(T volatile* dest, \
77 T exchange_value, \
78 atomic_memory_order order) const { \
79 STATIC_ASSERT(sizeof(IntrinsicType) == sizeof(T)); \
80 return PrimitiveConversions::cast<T>( \
81 IntrinsicName(reinterpret_cast<IntrinsicType volatile *>(dest), \
82 PrimitiveConversions::cast<IntrinsicType>(exchange_value))); \
83 }
84
85 DEFINE_INTRINSIC_XCHG(InterlockedExchange, long)
86 DEFINE_INTRINSIC_XCHG(InterlockedExchange64, __int64)
87
88 #undef DEFINE_INTRINSIC_XCHG
89
90 // Note: the order of the parameters is different between
91 // Atomic::PlatformCmpxchg<*>::operator() and the
92 // InterlockedCompareExchange* API.
93
94 #define DEFINE_INTRINSIC_CMPXCHG(IntrinsicName, IntrinsicType) \
95 template<> \
96 template<typename T> \
97 inline T Atomic::PlatformCmpxchg<sizeof(IntrinsicType)>::operator()(T volatile* dest, \
98 T compare_value, \
99 T exchange_value, \
100 atomic_memory_order order) const { \
101 STATIC_ASSERT(sizeof(IntrinsicType) == sizeof(T)); \
102 return PrimitiveConversions::cast<T>( \
103 IntrinsicName(reinterpret_cast<IntrinsicType volatile *>(dest), \
104 PrimitiveConversions::cast<IntrinsicType>(exchange_value), \
105 PrimitiveConversions::cast<IntrinsicType>(compare_value))); \
106 }
107
108 DEFINE_INTRINSIC_CMPXCHG(_InterlockedCompareExchange8, char) // Use the intrinsic as InterlockedCompareExchange8 does not exist
109 DEFINE_INTRINSIC_CMPXCHG(InterlockedCompareExchange, long)
110 DEFINE_INTRINSIC_CMPXCHG(InterlockedCompareExchange64, __int64)
111
112 #undef DEFINE_INTRINSIC_CMPXCHG
113
114 #ifndef AMD64
115
116 #pragma warning(disable: 4035) // Disables warnings reporting missing return statement
117
118 template<>
119 template<typename T>
120 inline T Atomic::PlatformLoad<8>::operator()(T const volatile* src) const {
121 STATIC_ASSERT(8 == sizeof(T));
122 volatile T dest;
123 volatile T* pdest = &dest;
124 __asm {
125 mov eax, src
126 fild qword ptr [eax]
127 mov eax, pdest
128 fistp qword ptr [eax]
129 }
130 return dest;
131 }
132
133 template<>
134 template<typename T>
135 inline void Atomic::PlatformStore<8>::operator()(T volatile* dest,
136 T store_value) const {
137 STATIC_ASSERT(8 == sizeof(T));
138 volatile T* src = &store_value;
139 __asm {
140 mov eax, src
141 fild qword ptr [eax]
142 mov eax, dest
143 fistp qword ptr [eax]
144 }
145 }
146
147 #pragma warning(default: 4035) // Enables warnings reporting missing return statement
148
149 template<>
150 struct Atomic::PlatformOrderedStore<1, RELEASE_X_FENCE>
151 {
152 template <typename T>
153 void operator()(volatile T* p, T v) const {
154 __asm {
155 mov edx, p;
156 mov al, v;
157 xchg al, byte ptr [edx];
158 }
159 }
160 };
161
162 template<>
163 struct Atomic::PlatformOrderedStore<2, RELEASE_X_FENCE>
164 {
165 template <typename T>
166 void operator()(volatile T* p, T v) const {
167 __asm {
168 mov edx, p;
|