1 /* 2 * Copyright (c) 2017, 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 #include "precompiled.hpp" 26 #include "accessBackend.inline.hpp" 27 #include "gc/shared/collectedHeap.hpp" 28 #include "oops/oop.inline.hpp" 29 #include "oops/valueKlass.hpp" 30 #include "runtime/mutexLocker.hpp" 31 #include "runtime/vm_version.hpp" 32 #include "utilities/copy.hpp" 33 34 namespace AccessInternal { 35 // VM_Version::supports_cx8() is a surrogate for 'supports atomic long memory ops'. 36 // 37 // On platforms which do not support atomic compare-and-swap of jlong (8 byte) 38 // values we have to use a lock-based scheme to enforce atomicity. This has to be 39 // applied to all Unsafe operations that set the value of a jlong field. Even so 40 // the compareAndSwapLong operation will not be atomic with respect to direct stores 41 // to the field from Java code. It is important therefore that any Java code that 42 // utilizes these Unsafe jlong operations does not perform direct stores. To permit 43 // direct loads of the field from Java code we must also use Atomic::store within the 44 // locked regions. And for good measure, in case there are direct stores, we also 45 // employ Atomic::load within those regions. Note that the field in question must be 46 // volatile and so must have atomic load/store accesses applied at the Java level. 47 // 48 // The locking scheme could utilize a range of strategies for controlling the locking 49 // granularity: from a lock per-field through to a single global lock. The latter is 50 // the simplest and is used for the current implementation. Note that the Java object 51 // that contains the field, can not, in general, be used for locking. To do so can lead 52 // to deadlocks as we may introduce locking into what appears to the Java code to be a 53 // lock-free path. 54 // 55 // As all the locked-regions are very short and themselves non-blocking we can treat 56 // them as leaf routines and elide safepoint checks (ie we don't perform any thread 57 // state transitions even when blocking for the lock). Note that if we do choose to 58 // add safepoint checks and thread state transitions, we must ensure that we calculate 59 // the address of the field _after_ we have acquired the lock, else the object may have 60 // been moved by the GC 61 62 #ifndef SUPPORTS_NATIVE_CX8 63 64 // This is intentionally in the cpp file rather than the .inline.hpp file. It seems 65 // desirable to trade faster JDK build times (not propagating vm_version.hpp) 66 // for slightly worse runtime atomic jlong performance on 32 bit machines with 67 // support for 64 bit atomics. 68 bool wide_atomic_needs_locking() { 69 return !VM_Version::supports_cx8(); 70 } 71 72 AccessLocker::AccessLocker() { 73 assert(!VM_Version::supports_cx8(), "why else?"); 74 UnsafeJlong_lock->lock_without_safepoint_check(); 75 } 76 77 AccessLocker::~AccessLocker() { 78 UnsafeJlong_lock->unlock(); 79 } 80 81 #endif 82 83 // These forward copying calls to Copy without exposing the Copy type in headers unnecessarily 84 85 void arraycopy_arrayof_conjoint_oops(void* src, void* dst, size_t length) { 86 Copy::arrayof_conjoint_oops(reinterpret_cast<HeapWord*>(src), 87 reinterpret_cast<HeapWord*>(dst), length); 88 } 89 90 void arraycopy_conjoint_oops(oop* src, oop* dst, size_t length) { 91 Copy::conjoint_oops_atomic(src, dst, length); 92 } 93 94 void arraycopy_conjoint_oops(narrowOop* src, narrowOop* dst, size_t length) { 95 Copy::conjoint_oops_atomic(src, dst, length); 96 } 97 98 void arraycopy_disjoint_words(void* src, void* dst, size_t length) { 99 Copy::disjoint_words(reinterpret_cast<HeapWord*>(src), 100 reinterpret_cast<HeapWord*>(dst), length); 101 } 102 103 void arraycopy_disjoint_words_atomic(void* src, void* dst, size_t length) { 104 Copy::disjoint_words_atomic(reinterpret_cast<HeapWord*>(src), 105 reinterpret_cast<HeapWord*>(dst), length); 106 } 107 108 template<> 109 void arraycopy_conjoint<jboolean>(jboolean* src, jboolean* dst, size_t length) { 110 Copy::conjoint_jbytes(reinterpret_cast<jbyte*>(src), reinterpret_cast<jbyte*>(dst), length); 111 } 112 113 template<> 114 void arraycopy_conjoint<jbyte>(jbyte* src, jbyte* dst, size_t length) { 115 Copy::conjoint_jbytes(src, dst, length); 116 } 117 118 template<> 119 void arraycopy_conjoint<jchar>(jchar* src, jchar* dst, size_t length) { 120 Copy::conjoint_jshorts_atomic(reinterpret_cast<jshort*>(src), reinterpret_cast<jshort*>(dst), length); 121 } 122 123 template<> 124 void arraycopy_conjoint<jshort>(jshort* src, jshort* dst, size_t length) { 125 Copy::conjoint_jshorts_atomic(src, dst, length); 126 } 127 128 template<> 129 void arraycopy_conjoint<jint>(jint* src, jint* dst, size_t length) { 130 Copy::conjoint_jints_atomic(src, dst, length); 131 } 132 133 template<> 134 void arraycopy_conjoint<jfloat>(jfloat* src, jfloat* dst, size_t length) { 135 Copy::conjoint_jints_atomic(reinterpret_cast<jint*>(src), reinterpret_cast<jint*>(dst), length); 136 } 137 138 template<> 139 void arraycopy_conjoint<jlong>(jlong* src, jlong* dst, size_t length) { 140 Copy::conjoint_jlongs_atomic(src, dst, length); 141 } 142 143 template<> 144 void arraycopy_conjoint<jdouble>(jdouble* src, jdouble* dst, size_t length) { 145 Copy::conjoint_jlongs_atomic(reinterpret_cast<jlong*>(src), reinterpret_cast<jlong*>(dst), length); 146 } 147 148 template<> 149 void arraycopy_arrayof_conjoint<jbyte>(jbyte* src, jbyte* dst, size_t length) { 150 Copy::arrayof_conjoint_jbytes(reinterpret_cast<HeapWord*>(src), 151 reinterpret_cast<HeapWord*>(dst), 152 length); 153 } 154 155 template<> 156 void arraycopy_arrayof_conjoint<jshort>(jshort* src, jshort* dst, size_t length) { 157 Copy::arrayof_conjoint_jshorts(reinterpret_cast<HeapWord*>(src), 158 reinterpret_cast<HeapWord*>(dst), 159 length); 160 } 161 162 template<> 163 void arraycopy_arrayof_conjoint<jint>(jint* src, jint* dst, size_t length) { 164 Copy::arrayof_conjoint_jints(reinterpret_cast<HeapWord*>(src), 165 reinterpret_cast<HeapWord*>(dst), 166 length); 167 } 168 169 template<> 170 void arraycopy_arrayof_conjoint<jlong>(jlong* src, jlong* dst, size_t length) { 171 Copy::arrayof_conjoint_jlongs(reinterpret_cast<HeapWord*>(src), 172 reinterpret_cast<HeapWord*>(dst), 173 length); 174 } 175 176 template<> 177 void arraycopy_conjoint<void>(void* src, void* dst, size_t length) { 178 Copy::conjoint_jbytes(reinterpret_cast<jbyte*>(src), 179 reinterpret_cast<jbyte*>(dst), 180 length); 181 } 182 183 template<> 184 void arraycopy_conjoint_atomic<jbyte>(jbyte* src, jbyte* dst, size_t length) { 185 Copy::conjoint_jbytes_atomic(src, dst, length); 186 } 187 188 template<> 189 void arraycopy_conjoint_atomic<jshort>(jshort* src, jshort* dst, size_t length) { 190 Copy::conjoint_jshorts_atomic(src, dst, length); 191 } 192 193 template<> 194 void arraycopy_conjoint_atomic<jint>(jint* src, jint* dst, size_t length) { 195 Copy::conjoint_jints_atomic(src, dst, length); 196 } 197 198 template<> 199 void arraycopy_conjoint_atomic<jlong>(jlong* src, jlong* dst, size_t length) { 200 Copy::conjoint_jlongs_atomic(src, dst, length); 201 } 202 203 template<> 204 void arraycopy_conjoint_atomic<void>(void* src, void* dst, size_t length) { 205 Copy::conjoint_memory_atomic(src, dst, length); 206 } 207 }