1 /* 2 * Copyright (c) 2018, 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 SHARE_OOPS_ACCESSDECORATORS_HPP 26 #define SHARE_OOPS_ACCESSDECORATORS_HPP 27 28 #include "gc/shared/barrierSetConfig.hpp" 29 #include "memory/allocation.hpp" 30 #include "metaprogramming/integralConstant.hpp" 31 #include "utilities/globalDefinitions.hpp" 32 33 // A decorator is an attribute or property that affects the way a memory access is performed in some way. 34 // There are different groups of decorators. Some have to do with memory ordering, others to do with, 35 // e.g. strength of references, strength of GC barriers, or whether compression should be applied or not. 36 // Some decorators are set at buildtime, such as whether primitives require GC barriers or not, others 37 // at callsites such as whether an access is in the heap or not, and others are resolved at runtime 38 // such as GC-specific barriers and encoding/decoding compressed oops. 39 typedef uint64_t DecoratorSet; 40 41 // The HasDecorator trait can help at compile-time determining whether a decorator set 42 // has an intersection with a certain other decorator set 43 template <DecoratorSet decorators, DecoratorSet decorator> 44 struct HasDecorator: public IntegralConstant<bool, (decorators & decorator) != 0> {}; 45 46 // == Internal Decorators - do not use == 47 // * INTERNAL_EMPTY: This is the name for the empty decorator set (in absence of other decorators). 48 // * INTERNAL_CONVERT_COMPRESSED_OOPS: This is an oop access that will require converting an oop 49 // to a narrowOop or vice versa, if UseCompressedOops is known to be set. 50 // * INTERNAL_VALUE_IS_OOP: Remember that the involved access is on oop rather than primitive. 51 const DecoratorSet INTERNAL_EMPTY = UCONST64(0); 52 const DecoratorSet INTERNAL_CONVERT_COMPRESSED_OOP = UCONST64(1) << 1; 53 const DecoratorSet INTERNAL_VALUE_IS_OOP = UCONST64(1) << 2; 54 55 // == Internal build-time Decorators == 56 // * INTERNAL_BT_BARRIER_ON_PRIMITIVES: This is set in the barrierSetConfig.hpp file. 57 // * INTERNAL_BT_TO_SPACE_INVARIANT: This is set in the barrierSetConfig.hpp file iff 58 // no GC is bundled in the build that is to-space invariant. 59 const DecoratorSet INTERNAL_BT_BARRIER_ON_PRIMITIVES = UCONST64(1) << 3; 60 const DecoratorSet INTERNAL_BT_TO_SPACE_INVARIANT = UCONST64(1) << 4; 61 62 // == Internal run-time Decorators == 63 // * INTERNAL_RT_USE_COMPRESSED_OOPS: This decorator will be set in runtime resolved 64 // access backends iff UseCompressedOops is true. 65 const DecoratorSet INTERNAL_RT_USE_COMPRESSED_OOPS = UCONST64(1) << 5; 66 67 const DecoratorSet INTERNAL_DECORATOR_MASK = INTERNAL_CONVERT_COMPRESSED_OOP | INTERNAL_VALUE_IS_OOP | 68 INTERNAL_BT_BARRIER_ON_PRIMITIVES | INTERNAL_RT_USE_COMPRESSED_OOPS; 69 70 // == Memory Ordering Decorators == 71 // The memory ordering decorators can be described in the following way: 72 // === Decorator Rules === 73 // The different types of memory ordering guarantees have a strict order of strength. 74 // Explicitly specifying the stronger ordering implies that the guarantees of the weaker 75 // property holds too. The names come from the C++11 atomic operations, and typically 76 // have a JMM equivalent property. 77 // The equivalence may be viewed like this: 78 // MO_UNORDERED is equivalent to JMM plain. 79 // MO_VOLATILE has no equivalence in JMM, because it's a C++ thing. 80 // MO_RELAXED is equivalent to JMM opaque. 81 // MO_ACQUIRE is equivalent to JMM acquire. 82 // MO_RELEASE is equivalent to JMM release. 83 // MO_SEQ_CST is equivalent to JMM volatile. 84 // 85 // === Stores === 86 // * MO_UNORDERED (Default): No guarantees. 87 // - The compiler and hardware are free to reorder aggressively. And they will. 88 // * MO_VOLATILE: Volatile stores (in the C++ sense). 89 // - The stores are not reordered by the compiler (but possibly the HW) w.r.t. other 90 // volatile accesses in program order (but possibly non-volatile accesses). 91 // * MO_RELAXED: Relaxed atomic stores. 92 // - The stores are atomic. 93 // - Guarantees from volatile stores hold. 94 // * MO_RELEASE: Releasing stores. 95 // - The releasing store will make its preceding memory accesses observable to memory accesses 96 // subsequent to an acquiring load observing this releasing store. 97 // - Guarantees from relaxed stores hold. 98 // * MO_SEQ_CST: Sequentially consistent stores. 99 // - The stores are observed in the same order by MO_SEQ_CST loads on other processors 100 // - Preceding loads and stores in program order are not reordered with subsequent loads and stores in program order. 101 // - Guarantees from releasing stores hold. 102 // === Loads === 103 // * MO_UNORDERED (Default): No guarantees 104 // - The compiler and hardware are free to reorder aggressively. And they will. 105 // * MO_VOLATILE: Volatile loads (in the C++ sense). 106 // - The loads are not reordered by the compiler (but possibly the HW) w.r.t. other 107 // volatile accesses in program order (but possibly non-volatile accesses). 108 // * MO_RELAXED: Relaxed atomic loads. 109 // - The loads are atomic. 110 // - Guarantees from volatile loads hold. 111 // * MO_ACQUIRE: Acquiring loads. 112 // - An acquiring load will make subsequent memory accesses observe the memory accesses 113 // preceding the releasing store that the acquiring load observed. 114 // - Guarantees from relaxed loads hold. 115 // * MO_SEQ_CST: Sequentially consistent loads. 116 // - These loads observe MO_SEQ_CST stores in the same order on other processors 117 // - Preceding loads and stores in program order are not reordered with subsequent loads and stores in program order. 118 // - Guarantees from acquiring loads hold. 119 // === Atomic Cmpxchg === 120 // * MO_RELAXED: Atomic but relaxed cmpxchg. 121 // - Guarantees from MO_RELAXED loads and MO_RELAXED stores hold unconditionally. 122 // * MO_SEQ_CST: Sequentially consistent cmpxchg. 123 // - Guarantees from MO_SEQ_CST loads and MO_SEQ_CST stores hold unconditionally. 124 // === Atomic Xchg === 125 // * MO_RELAXED: Atomic but relaxed atomic xchg. 126 // - Guarantees from MO_RELAXED loads and MO_RELAXED stores hold. 127 // * MO_SEQ_CST: Sequentially consistent xchg. 128 // - Guarantees from MO_SEQ_CST loads and MO_SEQ_CST stores hold. 129 const DecoratorSet MO_UNORDERED = UCONST64(1) << 6; 130 const DecoratorSet MO_VOLATILE = UCONST64(1) << 7; 131 const DecoratorSet MO_RELAXED = UCONST64(1) << 8; 132 const DecoratorSet MO_ACQUIRE = UCONST64(1) << 9; 133 const DecoratorSet MO_RELEASE = UCONST64(1) << 10; 134 const DecoratorSet MO_SEQ_CST = UCONST64(1) << 11; 135 const DecoratorSet MO_DECORATOR_MASK = MO_UNORDERED | MO_VOLATILE | MO_RELAXED | 136 MO_ACQUIRE | MO_RELEASE | MO_SEQ_CST; 137 138 // === Barrier Strength Decorators === 139 // * AS_RAW: The access will translate into a raw memory access, hence ignoring all semantic concerns 140 // except memory ordering and compressed oops. This will bypass runtime function pointer dispatching 141 // in the pipeline and hardwire to raw accesses without going trough the GC access barriers. 142 // - Accesses on oop* translate to raw memory accesses without runtime checks 143 // - Accesses on narrowOop* translate to encoded/decoded memory accesses without runtime checks 144 // - Accesses on HeapWord* translate to a runtime check choosing one of the above 145 // - Accesses on other types translate to raw memory accesses without runtime checks 146 // * AS_DEST_NOT_INITIALIZED: This property can be important to e.g. SATB barriers by 147 // marking that the previous value is uninitialized nonsense rather than a real value. 148 // * AS_NO_KEEPALIVE: The barrier is used only on oop references and will not keep any involved objects 149 // alive, regardless of the type of reference being accessed. It will however perform the memory access 150 // in a consistent way w.r.t. e.g. concurrent compaction, so that the right field is being accessed, 151 // or maintain, e.g. intergenerational or interregional pointers if applicable. This should be used with 152 // extreme caution in isolated scopes. 153 // * AS_NORMAL: The accesses will be resolved to an accessor on the BarrierSet class, giving the 154 // responsibility of performing the access and what barriers to be performed to the GC. This is the default. 155 // Note that primitive accesses will only be resolved on the barrier set if the appropriate build-time 156 // decorator for enabling primitive barriers is enabled for the build. 157 const DecoratorSet AS_RAW = UCONST64(1) << 12; 158 const DecoratorSet AS_DEST_NOT_INITIALIZED = UCONST64(1) << 13; 159 const DecoratorSet AS_NO_KEEPALIVE = UCONST64(1) << 14; 160 const DecoratorSet AS_NORMAL = UCONST64(1) << 15; 161 const DecoratorSet AS_DECORATOR_MASK = AS_RAW | AS_DEST_NOT_INITIALIZED | 162 AS_NO_KEEPALIVE | AS_NORMAL; 163 164 // === Reference Strength Decorators === 165 // These decorators only apply to accesses on oop-like types (oop/narrowOop). 166 // * ON_STRONG_OOP_REF: Memory access is performed on a strongly reachable reference. 167 // * ON_WEAK_OOP_REF: The memory access is performed on a weakly reachable reference. 168 // * ON_PHANTOM_OOP_REF: The memory access is performed on a phantomly reachable reference. 169 // This is the same ring of strength as jweak and weak oops in the VM. 170 // * ON_UNKNOWN_OOP_REF: The memory access is performed on a reference of unknown strength. 171 // This could for example come from the unsafe API. 172 // * Default (no explicit reference strength specified): ON_STRONG_OOP_REF 173 const DecoratorSet ON_STRONG_OOP_REF = UCONST64(1) << 16; 174 const DecoratorSet ON_WEAK_OOP_REF = UCONST64(1) << 17; 175 const DecoratorSet ON_PHANTOM_OOP_REF = UCONST64(1) << 18; 176 const DecoratorSet ON_UNKNOWN_OOP_REF = UCONST64(1) << 19; 177 const DecoratorSet ON_DECORATOR_MASK = ON_STRONG_OOP_REF | ON_WEAK_OOP_REF | 178 ON_PHANTOM_OOP_REF | ON_UNKNOWN_OOP_REF; 179 180 // === Access Location === 181 // Accesses can take place in, e.g. the heap, old or young generation and different native roots. 182 // The location is important to the GC as it may imply different actions. The following decorators are used: 183 // * IN_HEAP: The access is performed in the heap. Many barriers such as card marking will 184 // be omitted if this decorator is not set. 185 // * IN_HEAP_ARRAY: The access is performed on a heap allocated array. This is sometimes a special case 186 // for some GCs, and implies that it is an IN_HEAP. 187 // * IN_ROOT: The access is performed in an off-heap data structure pointing into the Java heap. 188 // * IN_CONCURRENT_ROOT: The access is performed in an off-heap data structure pointing into the Java heap, 189 // but is notably not scanned during safepoints. This is sometimes a special case for some GCs and 190 // implies that it is also an IN_ROOT. 191 const DecoratorSet IN_HEAP = UCONST64(1) << 20; 192 const DecoratorSet IN_HEAP_ARRAY = UCONST64(1) << 21; 193 const DecoratorSet IN_ROOT = UCONST64(1) << 22; 194 const DecoratorSet IN_CONCURRENT_ROOT = UCONST64(1) << 23; 195 const DecoratorSet IN_DECORATOR_MASK = IN_HEAP | IN_HEAP_ARRAY | 196 IN_ROOT | IN_CONCURRENT_ROOT; 197 198 // == Value Decorators == 199 // * OOP_NOT_NULL: This property can make certain barriers faster such as compressing oops. 200 const DecoratorSet OOP_NOT_NULL = UCONST64(1) << 25; 201 const DecoratorSet OOP_DECORATOR_MASK = OOP_NOT_NULL; 202 203 // == Arraycopy Decorators == 204 // * ARRAYCOPY_CHECKCAST: This property means that the class of the objects in source 205 // are not guaranteed to be subclasses of the class of the destination array. This requires 206 // a check-cast barrier during the copying operation. If this is not set, it is assumed 207 // that the array is covariant: (the source array type is-a destination array type) 208 // * ARRAYCOPY_DISJOINT: This property means that it is known that the two array ranges 209 // are disjoint. 210 // * ARRAYCOPY_ARRAYOF: The copy is in the arrayof form. 211 // * ARRAYCOPY_ATOMIC: The accesses have to be atomic over the size of its elements. 212 // * ARRAYCOPY_ALIGNED: The accesses have to be aligned on a HeapWord. 213 const DecoratorSet ARRAYCOPY_CHECKCAST = UCONST64(1) << 26; 214 const DecoratorSet ARRAYCOPY_DISJOINT = UCONST64(1) << 27; 215 const DecoratorSet ARRAYCOPY_ARRAYOF = UCONST64(1) << 28; 216 const DecoratorSet ARRAYCOPY_ATOMIC = UCONST64(1) << 29; 217 const DecoratorSet ARRAYCOPY_ALIGNED = UCONST64(1) << 30; 218 const DecoratorSet ARRAYCOPY_DECORATOR_MASK = ARRAYCOPY_CHECKCAST | ARRAYCOPY_DISJOINT | 219 ARRAYCOPY_DISJOINT | ARRAYCOPY_ARRAYOF | 220 ARRAYCOPY_ATOMIC | ARRAYCOPY_ALIGNED; 221 222 // Keep track of the last decorator. 223 const DecoratorSet DECORATOR_LAST = UCONST64(1) << 30; 224 225 namespace AccessInternal { 226 // This class adds implied decorators that follow according to decorator rules. 227 // For example adding default reference strength and default memory ordering 228 // semantics. 229 template <DecoratorSet input_decorators> 230 struct DecoratorFixup: AllStatic { 231 // If no reference strength has been picked, then strong will be picked 232 static const DecoratorSet ref_strength_default = input_decorators | 233 (((ON_DECORATOR_MASK & input_decorators) == 0 && (INTERNAL_VALUE_IS_OOP & input_decorators) != 0) ? 234 ON_STRONG_OOP_REF : INTERNAL_EMPTY); 235 // If no memory ordering has been picked, unordered will be picked 236 static const DecoratorSet memory_ordering_default = ref_strength_default | 237 ((MO_DECORATOR_MASK & ref_strength_default) == 0 ? MO_UNORDERED : INTERNAL_EMPTY); 238 // If no barrier strength has been picked, normal will be used 239 static const DecoratorSet barrier_strength_default = memory_ordering_default | 240 ((AS_DECORATOR_MASK & memory_ordering_default) == 0 ? AS_NORMAL : INTERNAL_EMPTY); 241 // Heap array accesses imply it is a heap access 242 static const DecoratorSet heap_array_is_in_heap = barrier_strength_default | 243 ((IN_HEAP_ARRAY & barrier_strength_default) != 0 ? IN_HEAP : INTERNAL_EMPTY); 244 static const DecoratorSet conc_root_is_root = heap_array_is_in_heap | 245 ((IN_CONCURRENT_ROOT & heap_array_is_in_heap) != 0 ? IN_ROOT : INTERNAL_EMPTY); 246 static const DecoratorSet value = conc_root_is_root | BT_BUILDTIME_DECORATORS; 247 }; 248 249 // This function implements the above DecoratorFixup rules, but without meta 250 // programming for code generation that does not use templates. 251 inline DecoratorSet decorator_fixup(DecoratorSet input_decorators) { 252 // If no reference strength has been picked, then strong will be picked 253 DecoratorSet ref_strength_default = input_decorators | 254 (((ON_DECORATOR_MASK & input_decorators) == 0 && (INTERNAL_VALUE_IS_OOP & input_decorators) != 0) ? 255 ON_STRONG_OOP_REF : INTERNAL_EMPTY); 256 // If no memory ordering has been picked, unordered will be picked 257 DecoratorSet memory_ordering_default = ref_strength_default | 258 ((MO_DECORATOR_MASK & ref_strength_default) == 0 ? MO_UNORDERED : INTERNAL_EMPTY); 259 // If no barrier strength has been picked, normal will be used 260 DecoratorSet barrier_strength_default = memory_ordering_default | 261 ((AS_DECORATOR_MASK & memory_ordering_default) == 0 ? AS_NORMAL : INTERNAL_EMPTY); 262 // Heap array accesses imply it is a heap access 263 DecoratorSet heap_array_is_in_heap = barrier_strength_default | 264 ((IN_HEAP_ARRAY & barrier_strength_default) != 0 ? IN_HEAP : INTERNAL_EMPTY); 265 DecoratorSet conc_root_is_root = heap_array_is_in_heap | 266 ((IN_CONCURRENT_ROOT & heap_array_is_in_heap) != 0 ? IN_ROOT : INTERNAL_EMPTY); 267 DecoratorSet value = conc_root_is_root | BT_BUILDTIME_DECORATORS; 268 return value; 269 } 270 } 271 272 #endif // SHARE_OOPS_ACCESSDECORATORS_HPP