Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // This file is an internal atomic implementation, use base/atomicops.h instead. |
michael@0 | 6 | |
michael@0 | 7 | #ifndef BASE_ATOMICOPS_INTERNALS_X86_GCC_H_ |
michael@0 | 8 | #define BASE_ATOMICOPS_INTERNALS_X86_GCC_H_ |
michael@0 | 9 | |
michael@0 | 10 | // This struct is not part of the public API of this module; clients may not |
michael@0 | 11 | // use it. |
michael@0 | 12 | // Features of this x86. Values may not be correct before main() is run, |
michael@0 | 13 | // but are set conservatively. |
michael@0 | 14 | struct AtomicOps_x86CPUFeatureStruct { |
michael@0 | 15 | bool has_amd_lock_mb_bug; // Processor has AMD memory-barrier bug; do lfence |
michael@0 | 16 | // after acquire compare-and-swap. |
michael@0 | 17 | bool has_sse2; // Processor has SSE2. |
michael@0 | 18 | }; |
michael@0 | 19 | extern struct AtomicOps_x86CPUFeatureStruct AtomicOps_Internalx86CPUFeatures; |
michael@0 | 20 | |
michael@0 | 21 | #define ATOMICOPS_COMPILER_BARRIER() __asm__ __volatile__("" : : : "memory") |
michael@0 | 22 | |
michael@0 | 23 | namespace base { |
michael@0 | 24 | namespace subtle { |
michael@0 | 25 | |
michael@0 | 26 | // 32-bit low-level operations on any platform. |
michael@0 | 27 | |
michael@0 | 28 | inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 29 | Atomic32 old_value, |
michael@0 | 30 | Atomic32 new_value) { |
michael@0 | 31 | Atomic32 prev; |
michael@0 | 32 | __asm__ __volatile__("lock; cmpxchgl %1,%2" |
michael@0 | 33 | : "=a" (prev) |
michael@0 | 34 | : "q" (new_value), "m" (*ptr), "0" (old_value) |
michael@0 | 35 | : "memory"); |
michael@0 | 36 | return prev; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, |
michael@0 | 40 | Atomic32 new_value) { |
michael@0 | 41 | __asm__ __volatile__("xchgl %1,%0" // The lock prefix is implicit for xchg. |
michael@0 | 42 | : "=r" (new_value) |
michael@0 | 43 | : "m" (*ptr), "0" (new_value) |
michael@0 | 44 | : "memory"); |
michael@0 | 45 | return new_value; // Now it's the previous value. |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, |
michael@0 | 49 | Atomic32 increment) { |
michael@0 | 50 | Atomic32 temp = increment; |
michael@0 | 51 | __asm__ __volatile__("lock; xaddl %0,%1" |
michael@0 | 52 | : "+r" (temp), "+m" (*ptr) |
michael@0 | 53 | : : "memory"); |
michael@0 | 54 | // temp now holds the old value of *ptr |
michael@0 | 55 | return temp + increment; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, |
michael@0 | 59 | Atomic32 increment) { |
michael@0 | 60 | Atomic32 temp = increment; |
michael@0 | 61 | __asm__ __volatile__("lock; xaddl %0,%1" |
michael@0 | 62 | : "+r" (temp), "+m" (*ptr) |
michael@0 | 63 | : : "memory"); |
michael@0 | 64 | // temp now holds the old value of *ptr |
michael@0 | 65 | if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) { |
michael@0 | 66 | __asm__ __volatile__("lfence" : : : "memory"); |
michael@0 | 67 | } |
michael@0 | 68 | return temp + increment; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 72 | Atomic32 old_value, |
michael@0 | 73 | Atomic32 new_value) { |
michael@0 | 74 | Atomic32 x = NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
michael@0 | 75 | if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) { |
michael@0 | 76 | __asm__ __volatile__("lfence" : : : "memory"); |
michael@0 | 77 | } |
michael@0 | 78 | return x; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 82 | Atomic32 old_value, |
michael@0 | 83 | Atomic32 new_value) { |
michael@0 | 84 | return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 88 | *ptr = value; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | #if defined(__x86_64__) |
michael@0 | 92 | |
michael@0 | 93 | // 64-bit implementations of memory barrier can be simpler, because it |
michael@0 | 94 | // "mfence" is guaranteed to exist. |
michael@0 | 95 | inline void MemoryBarrier() { |
michael@0 | 96 | __asm__ __volatile__("mfence" : : : "memory"); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 100 | *ptr = value; |
michael@0 | 101 | MemoryBarrier(); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | #else |
michael@0 | 105 | |
michael@0 | 106 | inline void MemoryBarrier() { |
michael@0 | 107 | if (AtomicOps_Internalx86CPUFeatures.has_sse2) { |
michael@0 | 108 | __asm__ __volatile__("mfence" : : : "memory"); |
michael@0 | 109 | } else { // mfence is faster but not present on PIII |
michael@0 | 110 | Atomic32 x = 0; |
michael@0 | 111 | NoBarrier_AtomicExchange(&x, 0); // acts as a barrier on PIII |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 116 | if (AtomicOps_Internalx86CPUFeatures.has_sse2) { |
michael@0 | 117 | *ptr = value; |
michael@0 | 118 | __asm__ __volatile__("mfence" : : : "memory"); |
michael@0 | 119 | } else { |
michael@0 | 120 | NoBarrier_AtomicExchange(ptr, value); |
michael@0 | 121 | // acts as a barrier on PIII |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | #endif |
michael@0 | 125 | |
michael@0 | 126 | inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 127 | ATOMICOPS_COMPILER_BARRIER(); |
michael@0 | 128 | *ptr = value; // An x86 store acts as a release barrier. |
michael@0 | 129 | // See comments in Atomic64 version of Release_Store(), below. |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { |
michael@0 | 133 | return *ptr; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { |
michael@0 | 137 | Atomic32 value = *ptr; // An x86 load acts as a acquire barrier. |
michael@0 | 138 | // See comments in Atomic64 version of Release_Store(), below. |
michael@0 | 139 | ATOMICOPS_COMPILER_BARRIER(); |
michael@0 | 140 | return value; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | inline Atomic32 Release_Load(volatile const Atomic32* ptr) { |
michael@0 | 144 | MemoryBarrier(); |
michael@0 | 145 | return *ptr; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | #if defined(__x86_64__) |
michael@0 | 149 | |
michael@0 | 150 | // 64-bit low-level operations on 64-bit platform. |
michael@0 | 151 | |
michael@0 | 152 | inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, |
michael@0 | 153 | Atomic64 old_value, |
michael@0 | 154 | Atomic64 new_value) { |
michael@0 | 155 | Atomic64 prev; |
michael@0 | 156 | __asm__ __volatile__("lock; cmpxchgq %1,%2" |
michael@0 | 157 | : "=a" (prev) |
michael@0 | 158 | : "q" (new_value), "m" (*ptr), "0" (old_value) |
michael@0 | 159 | : "memory"); |
michael@0 | 160 | return prev; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, |
michael@0 | 164 | Atomic64 new_value) { |
michael@0 | 165 | __asm__ __volatile__("xchgq %1,%0" // The lock prefix is implicit for xchg. |
michael@0 | 166 | : "=r" (new_value) |
michael@0 | 167 | : "m" (*ptr), "0" (new_value) |
michael@0 | 168 | : "memory"); |
michael@0 | 169 | return new_value; // Now it's the previous value. |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, |
michael@0 | 173 | Atomic64 increment) { |
michael@0 | 174 | Atomic64 temp = increment; |
michael@0 | 175 | __asm__ __volatile__("lock; xaddq %0,%1" |
michael@0 | 176 | : "+r" (temp), "+m" (*ptr) |
michael@0 | 177 | : : "memory"); |
michael@0 | 178 | // temp now contains the previous value of *ptr |
michael@0 | 179 | return temp + increment; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, |
michael@0 | 183 | Atomic64 increment) { |
michael@0 | 184 | Atomic64 temp = increment; |
michael@0 | 185 | __asm__ __volatile__("lock; xaddq %0,%1" |
michael@0 | 186 | : "+r" (temp), "+m" (*ptr) |
michael@0 | 187 | : : "memory"); |
michael@0 | 188 | // temp now contains the previous value of *ptr |
michael@0 | 189 | if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) { |
michael@0 | 190 | __asm__ __volatile__("lfence" : : : "memory"); |
michael@0 | 191 | } |
michael@0 | 192 | return temp + increment; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { |
michael@0 | 196 | *ptr = value; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, |
michael@0 | 200 | Atomic64 old_value, |
michael@0 | 201 | Atomic64 new_value) { |
michael@0 | 202 | Atomic64 x = NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
michael@0 | 203 | /* XXX/cjones: no idea if this is necessary... */ |
michael@0 | 204 | if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) { |
michael@0 | 205 | __asm__ __volatile__("lfence" : : : "memory"); |
michael@0 | 206 | } |
michael@0 | 207 | return x; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { |
michael@0 | 211 | *ptr = value; |
michael@0 | 212 | MemoryBarrier(); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { |
michael@0 | 216 | ATOMICOPS_COMPILER_BARRIER(); |
michael@0 | 217 | |
michael@0 | 218 | *ptr = value; // An x86 store acts as a release barrier |
michael@0 | 219 | // for current AMD/Intel chips as of Jan 2008. |
michael@0 | 220 | // See also Acquire_Load(), below. |
michael@0 | 221 | |
michael@0 | 222 | // When new chips come out, check: |
michael@0 | 223 | // IA-32 Intel Architecture Software Developer's Manual, Volume 3: |
michael@0 | 224 | // System Programming Guide, Chatper 7: Multiple-processor management, |
michael@0 | 225 | // Section 7.2, Memory Ordering. |
michael@0 | 226 | // Last seen at: |
michael@0 | 227 | // http://developer.intel.com/design/pentium4/manuals/index_new.htm |
michael@0 | 228 | // |
michael@0 | 229 | // x86 stores/loads fail to act as barriers for a few instructions (clflush |
michael@0 | 230 | // maskmovdqu maskmovq movntdq movnti movntpd movntps movntq) but these are |
michael@0 | 231 | // not generated by the compiler, and are rare. Users of these instructions |
michael@0 | 232 | // need to know about cache behaviour in any case since all of these involve |
michael@0 | 233 | // either flushing cache lines or non-temporal cache hints. |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { |
michael@0 | 237 | return *ptr; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { |
michael@0 | 241 | Atomic64 value = *ptr; // An x86 load acts as a acquire barrier, |
michael@0 | 242 | // for current AMD/Intel chips as of Jan 2008. |
michael@0 | 243 | // See also Release_Store(), above. |
michael@0 | 244 | ATOMICOPS_COMPILER_BARRIER(); |
michael@0 | 245 | return value; |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | inline Atomic64 Release_Load(volatile const Atomic64* ptr) { |
michael@0 | 249 | MemoryBarrier(); |
michael@0 | 250 | return *ptr; |
michael@0 | 251 | } |
michael@0 | 252 | #endif // defined(__x86_64__) |
michael@0 | 253 | |
michael@0 | 254 | } // namespace base::subtle |
michael@0 | 255 | } // namespace base |
michael@0 | 256 | |
michael@0 | 257 | #undef ATOMICOPS_COMPILER_BARRIER |
michael@0 | 258 | |
michael@0 | 259 | #endif // BASE_ATOMICOPS_INTERNALS_X86_GCC_H_ |