The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
Fengwei Yin | ee18fb4 | 2012-03-28 17:25:17 +0800 | [diff] [blame] | 17 | #include <endian.h> |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 18 | #include <limits.h> |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 19 | #include <stdatomic.h> |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 20 | #include <stddef.h> |
| 21 | |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 22 | #include "private/bionic_futex.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | 387d4b7 | 2012-08-09 15:17:46 -0700 | [diff] [blame] | 24 | // This file contains C++ ABI support functions for one time |
| 25 | // constructors as defined in the "Run-time ABI for the ARM Architecture" |
| 26 | // section 4.4.2 |
| 27 | // |
Fengwei Yin | ee18fb4 | 2012-03-28 17:25:17 +0800 | [diff] [blame] | 28 | // ARM C++ ABI and Itanium/x86 C++ ABI has different definition for |
| 29 | // one time construction: |
| 30 | // |
| 31 | // ARM C++ ABI defines the LSB of guard variable should be tested |
| 32 | // by compiler-generated code before calling __cxa_guard_acquire et al. |
| 33 | // |
| 34 | // The Itanium/x86 C++ ABI defines the low-order _byte_ should be |
| 35 | // tested instead. |
| 36 | // |
| 37 | // Meanwhile, guard variable are 32bit aligned for ARM, and 64bit |
| 38 | // aligned for x86. |
| 39 | // |
| 40 | // Reference documentation: |
| 41 | // |
| 42 | // section 3.2.3 of ARM IHI 0041C (for ARM) |
| 43 | // section 3.3.2 of the Itanium C++ ABI specification v1.83 (for x86). |
| 44 | // |
| 45 | // There is no C++ ABI available for other ARCH. But the gcc source |
| 46 | // shows all other ARCH follow the definition of Itanium/x86 C++ ABI. |
| 47 | |
Fengwei Yin | ee18fb4 | 2012-03-28 17:25:17 +0800 | [diff] [blame] | 48 | #if defined(__arm__) |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 49 | // The ARM C++ ABI mandates that guard variables are 32-bit aligned, 32-bit |
| 50 | // values. The LSB is tested by the compiler-generated code before calling |
Fengwei Yin | ee18fb4 | 2012-03-28 17:25:17 +0800 | [diff] [blame] | 51 | // __cxa_guard_acquire. |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 52 | union _guard_t { |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 53 | atomic_int state; |
| 54 | int32_t aligner; |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 55 | }; |
Fengwei Yin | ee18fb4 | 2012-03-28 17:25:17 +0800 | [diff] [blame] | 56 | |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 57 | #else |
| 58 | // The Itanium/x86 C++ ABI (used by all other architectures) mandates that |
| 59 | // guard variables are 64-bit aligned, 64-bit values. The LSB is tested by |
| 60 | // the compiler-generated code before calling __cxa_guard_acquire. |
| 61 | union _guard_t { |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 62 | atomic_int state; |
| 63 | int64_t aligner; |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 64 | }; |
Fengwei Yin | ee18fb4 | 2012-03-28 17:25:17 +0800 | [diff] [blame] | 65 | |
Fengwei Yin | ee18fb4 | 2012-03-28 17:25:17 +0800 | [diff] [blame] | 66 | #endif |
| 67 | |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 68 | // Set construction state values according to reference documentation. |
| 69 | // 0 is the initialization value. |
| 70 | // Arm requires ((*gv & 1) == 1) after __cxa_guard_release, ((*gv & 3) == 0) after __cxa_guard_abort. |
| 71 | // X86 requires first byte not modified by __cxa_guard_acquire, first byte is non-zero after |
| 72 | // __cxa_guard_release. |
| 73 | |
| 74 | #define CONSTRUCTION_NOT_YET_STARTED 0 |
| 75 | #define CONSTRUCTION_COMPLETE 1 |
| 76 | #define CONSTRUCTION_UNDERWAY_WITHOUT_WAITER 0x100 |
| 77 | #define CONSTRUCTION_UNDERWAY_WITH_WAITER 0x200 |
| 78 | |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 79 | extern "C" int __cxa_guard_acquire(_guard_t* gv) { |
Hans Boehm | 963daed | 2017-02-22 15:34:29 -0800 | [diff] [blame] | 80 | int old_value = atomic_load_explicit(&gv->state, memory_order_acquire); |
| 81 | // In the common CONSTRUCTION_COMPLETE case we have to ensure that all the stores performed by |
| 82 | // the construction function are observable on this CPU after we exit. A similar constraint may |
| 83 | // apply in the CONSTRUCTION_NOT_YET_STARTED case with a prior abort. |
Fengwei Yin | ee18fb4 | 2012-03-28 17:25:17 +0800 | [diff] [blame] | 84 | |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 85 | while (true) { |
| 86 | if (old_value == CONSTRUCTION_COMPLETE) { |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 87 | return 0; |
| 88 | } else if (old_value == CONSTRUCTION_NOT_YET_STARTED) { |
| 89 | if (!atomic_compare_exchange_weak_explicit(&gv->state, &old_value, |
| 90 | CONSTRUCTION_UNDERWAY_WITHOUT_WAITER, |
Hans Boehm | 963daed | 2017-02-22 15:34:29 -0800 | [diff] [blame] | 91 | memory_order_acquire /* or relaxed in C++17 */, |
| 92 | memory_order_acquire)) { |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 93 | continue; |
| 94 | } |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 95 | return 1; |
| 96 | } else if (old_value == CONSTRUCTION_UNDERWAY_WITHOUT_WAITER) { |
| 97 | if (!atomic_compare_exchange_weak_explicit(&gv->state, &old_value, |
| 98 | CONSTRUCTION_UNDERWAY_WITH_WAITER, |
Hans Boehm | 963daed | 2017-02-22 15:34:29 -0800 | [diff] [blame] | 99 | memory_order_acquire /* or relaxed in C++17 */, |
| 100 | memory_order_acquire)) { |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 101 | continue; |
| 102 | } |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 103 | } |
David 'Digit' Turner | d466780 | 2010-06-11 13:18:41 -0700 | [diff] [blame] | 104 | |
Tom Cherry | ac49ced | 2017-08-17 13:18:52 -0700 | [diff] [blame] | 105 | __futex_wait_ex(&gv->state, false, CONSTRUCTION_UNDERWAY_WITH_WAITER); |
Hans Boehm | 963daed | 2017-02-22 15:34:29 -0800 | [diff] [blame] | 106 | old_value = atomic_load_explicit(&gv->state, memory_order_acquire); |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 107 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 110 | extern "C" void __cxa_guard_release(_guard_t* gv) { |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 111 | // Release fence is used to make all stores performed by the construction function |
| 112 | // visible in other threads. |
| 113 | int old_value = atomic_exchange_explicit(&gv->state, CONSTRUCTION_COMPLETE, memory_order_release); |
| 114 | if (old_value == CONSTRUCTION_UNDERWAY_WITH_WAITER) { |
| 115 | __futex_wake_ex(&gv->state, false, INT_MAX); |
| 116 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 119 | extern "C" void __cxa_guard_abort(_guard_t* gv) { |
Yabin Cui | 6a3ff01 | 2015-01-29 10:47:45 -0800 | [diff] [blame] | 120 | // Release fence is used to make all stores performed by the construction function |
| 121 | // visible in other threads. |
| 122 | int old_value = atomic_exchange_explicit(&gv->state, CONSTRUCTION_NOT_YET_STARTED, memory_order_release); |
| 123 | if (old_value == CONSTRUCTION_UNDERWAY_WITH_WAITER) { |
| 124 | __futex_wake_ex(&gv->state, false, INT_MAX); |
| 125 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 126 | } |