blob: 06926dff3668a757ea25f3f32e8fede8edcc011f [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Elliott Hughes15b641a2014-05-14 18:18:55 -07002 * 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 Project1dc9e472009-03-03 19:28:35 -080015 */
16
Fengwei Yinee18fb42012-03-28 17:25:17 +080017#include <endian.h>
Yabin Cui6a3ff012015-01-29 10:47:45 -080018#include <limits.h>
19#undef _USING_LIBCXX // Prevent using of <atomic>.
20#include <stdatomic.h>
Elliott Hugheseb847bc2013-10-09 15:50:50 -070021
Yabin Cui6a3ff012015-01-29 10:47:45 -080022#include <stddef.h>
23
Elliott Hugheseb847bc2013-10-09 15:50:50 -070024#include "private/bionic_futex.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080025
Elliott Hughes387d4b72012-08-09 15:17:46 -070026// This file contains C++ ABI support functions for one time
27// constructors as defined in the "Run-time ABI for the ARM Architecture"
28// section 4.4.2
29//
Fengwei Yinee18fb42012-03-28 17:25:17 +080030// ARM C++ ABI and Itanium/x86 C++ ABI has different definition for
31// one time construction:
32//
33// ARM C++ ABI defines the LSB of guard variable should be tested
34// by compiler-generated code before calling __cxa_guard_acquire et al.
35//
36// The Itanium/x86 C++ ABI defines the low-order _byte_ should be
37// tested instead.
38//
39// Meanwhile, guard variable are 32bit aligned for ARM, and 64bit
40// aligned for x86.
41//
42// Reference documentation:
43//
44// section 3.2.3 of ARM IHI 0041C (for ARM)
45// section 3.3.2 of the Itanium C++ ABI specification v1.83 (for x86).
46//
47// There is no C++ ABI available for other ARCH. But the gcc source
48// shows all other ARCH follow the definition of Itanium/x86 C++ ABI.
49
Fengwei Yinee18fb42012-03-28 17:25:17 +080050#if defined(__arm__)
Elliott Hughes15b641a2014-05-14 18:18:55 -070051// The ARM C++ ABI mandates that guard variables are 32-bit aligned, 32-bit
52// values. The LSB is tested by the compiler-generated code before calling
Fengwei Yinee18fb42012-03-28 17:25:17 +080053// __cxa_guard_acquire.
Elliott Hughes15b641a2014-05-14 18:18:55 -070054union _guard_t {
Yabin Cui6a3ff012015-01-29 10:47:45 -080055 atomic_int state;
56 int32_t aligner;
Elliott Hughes15b641a2014-05-14 18:18:55 -070057};
Fengwei Yinee18fb42012-03-28 17:25:17 +080058
Elliott Hughes15b641a2014-05-14 18:18:55 -070059#else
60// The Itanium/x86 C++ ABI (used by all other architectures) mandates that
61// guard variables are 64-bit aligned, 64-bit values. The LSB is tested by
62// the compiler-generated code before calling __cxa_guard_acquire.
63union _guard_t {
Yabin Cui6a3ff012015-01-29 10:47:45 -080064 atomic_int state;
65 int64_t aligner;
Elliott Hughes15b641a2014-05-14 18:18:55 -070066};
Fengwei Yinee18fb42012-03-28 17:25:17 +080067
Fengwei Yinee18fb42012-03-28 17:25:17 +080068#endif
69
Yabin Cui6a3ff012015-01-29 10:47:45 -080070// Set construction state values according to reference documentation.
71// 0 is the initialization value.
72// Arm requires ((*gv & 1) == 1) after __cxa_guard_release, ((*gv & 3) == 0) after __cxa_guard_abort.
73// X86 requires first byte not modified by __cxa_guard_acquire, first byte is non-zero after
74// __cxa_guard_release.
75
76#define CONSTRUCTION_NOT_YET_STARTED 0
77#define CONSTRUCTION_COMPLETE 1
78#define CONSTRUCTION_UNDERWAY_WITHOUT_WAITER 0x100
79#define CONSTRUCTION_UNDERWAY_WITH_WAITER 0x200
80
Elliott Hughes15b641a2014-05-14 18:18:55 -070081extern "C" int __cxa_guard_acquire(_guard_t* gv) {
Hans Boehm963daed2017-02-22 15:34:29 -080082 int old_value = atomic_load_explicit(&gv->state, memory_order_acquire);
83 // In the common CONSTRUCTION_COMPLETE case we have to ensure that all the stores performed by
84 // the construction function are observable on this CPU after we exit. A similar constraint may
85 // apply in the CONSTRUCTION_NOT_YET_STARTED case with a prior abort.
Fengwei Yinee18fb42012-03-28 17:25:17 +080086
Yabin Cui6a3ff012015-01-29 10:47:45 -080087 while (true) {
88 if (old_value == CONSTRUCTION_COMPLETE) {
Yabin Cui6a3ff012015-01-29 10:47:45 -080089 return 0;
90 } else if (old_value == CONSTRUCTION_NOT_YET_STARTED) {
91 if (!atomic_compare_exchange_weak_explicit(&gv->state, &old_value,
92 CONSTRUCTION_UNDERWAY_WITHOUT_WAITER,
Hans Boehm963daed2017-02-22 15:34:29 -080093 memory_order_acquire /* or relaxed in C++17 */,
94 memory_order_acquire)) {
Yabin Cui6a3ff012015-01-29 10:47:45 -080095 continue;
96 }
Yabin Cui6a3ff012015-01-29 10:47:45 -080097 return 1;
98 } else if (old_value == CONSTRUCTION_UNDERWAY_WITHOUT_WAITER) {
99 if (!atomic_compare_exchange_weak_explicit(&gv->state, &old_value,
100 CONSTRUCTION_UNDERWAY_WITH_WAITER,
Hans Boehm963daed2017-02-22 15:34:29 -0800101 memory_order_acquire /* or relaxed in C++17 */,
102 memory_order_acquire)) {
Yabin Cui6a3ff012015-01-29 10:47:45 -0800103 continue;
104 }
Elliott Hughes15b641a2014-05-14 18:18:55 -0700105 }
David 'Digit' Turnerd4667802010-06-11 13:18:41 -0700106
Yabin Cuic9a659c2015-11-05 15:36:08 -0800107 __futex_wait_ex(&gv->state, false, CONSTRUCTION_UNDERWAY_WITH_WAITER, false, nullptr);
Hans Boehm963daed2017-02-22 15:34:29 -0800108 old_value = atomic_load_explicit(&gv->state, memory_order_acquire);
Yabin Cui6a3ff012015-01-29 10:47:45 -0800109 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110}
111
Elliott Hughes15b641a2014-05-14 18:18:55 -0700112extern "C" void __cxa_guard_release(_guard_t* gv) {
Yabin Cui6a3ff012015-01-29 10:47:45 -0800113 // Release fence is used to make all stores performed by the construction function
114 // visible in other threads.
115 int old_value = atomic_exchange_explicit(&gv->state, CONSTRUCTION_COMPLETE, memory_order_release);
116 if (old_value == CONSTRUCTION_UNDERWAY_WITH_WAITER) {
117 __futex_wake_ex(&gv->state, false, INT_MAX);
118 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119}
120
Elliott Hughes15b641a2014-05-14 18:18:55 -0700121extern "C" void __cxa_guard_abort(_guard_t* gv) {
Yabin Cui6a3ff012015-01-29 10:47:45 -0800122 // Release fence is used to make all stores performed by the construction function
123 // visible in other threads.
124 int old_value = atomic_exchange_explicit(&gv->state, CONSTRUCTION_NOT_YET_STARTED, memory_order_release);
125 if (old_value == CONSTRUCTION_UNDERWAY_WITH_WAITER) {
126 __futex_wake_ex(&gv->state, false, INT_MAX);
127 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128}