duke@435: /* coleenp@4037: * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/vmSymbols.hpp" jprovino@4542: #include "utilities/macros.hpp" jprovino@4542: #if INCLUDE_ALL_GCS johnc@2781: #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp" jprovino@4542: #endif // INCLUDE_ALL_GCS stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "prims/jni.h" stefank@2314: #include "prims/jvm.h" stefank@2314: #include "runtime/globals.hpp" stefank@2314: #include "runtime/interfaceSupport.hpp" stefank@2314: #include "runtime/reflection.hpp" stefank@2314: #include "runtime/synchronizer.hpp" stefank@2314: #include "services/threadService.hpp" stefank@2314: #include "utilities/copy.hpp" stefank@2314: #include "utilities/dtrace.hpp" stefank@2314: duke@435: /* duke@435: * Implementation of class sun.misc.Unsafe duke@435: */ duke@435: dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE_DECL3(hotspot, thread__park__begin, uintptr_t, int, long long); fparain@1759: HS_DTRACE_PROBE_DECL1(hotspot, thread__park__end, uintptr_t); fparain@1759: HS_DTRACE_PROBE_DECL1(hotspot, thread__unpark, uintptr_t); dcubed@3202: #endif /* !USDT2 */ fparain@1759: duke@435: #define MAX_OBJECT_SIZE \ duke@435: ( arrayOopDesc::header_size(T_DOUBLE) * HeapWordSize \ duke@435: + ((julong)max_jint * sizeof(double)) ) duke@435: duke@435: duke@435: #define UNSAFE_ENTRY(result_type, header) \ duke@435: JVM_ENTRY(result_type, header) duke@435: duke@435: // Can't use UNSAFE_LEAF because it has the signature of a straight duke@435: // call into the runtime (just like JVM_LEAF, funny that) but it's duke@435: // called like a Java Native and thus the wrapper built for it passes duke@435: // arguments like a JNI call. It expects those arguments to be popped duke@435: // from the stack on Intel like all good JNI args are, and adjusts the duke@435: // stack according. Since the JVM_LEAF call expects no extra duke@435: // arguments the stack isn't popped in the C code, is pushed by the duke@435: // wrapper and we get sick. duke@435: //#define UNSAFE_LEAF(result_type, header) \ duke@435: // JVM_LEAF(result_type, header) duke@435: duke@435: #define UNSAFE_END JVM_END duke@435: duke@435: #define UnsafeWrapper(arg) /*nothing, for the present*/ duke@435: duke@435: duke@435: inline void* addr_from_java(jlong addr) { duke@435: // This assert fails in a variety of ways on 32-bit systems. duke@435: // It is impossible to predict whether native code that converts duke@435: // pointers to longs will sign-extend or zero-extend the addresses. duke@435: //assert(addr == (uintptr_t)addr, "must not be odd high bits"); duke@435: return (void*)(uintptr_t)addr; duke@435: } duke@435: duke@435: inline jlong addr_to_java(void* p) { duke@435: assert(p == (void*)(uintptr_t)p, "must not be odd high bits"); duke@435: return (uintptr_t)p; duke@435: } duke@435: duke@435: duke@435: // Note: The VM's obj_field and related accessors use byte-scaled duke@435: // ("unscaled") offsets, just as the unsafe methods do. duke@435: duke@435: // However, the method Unsafe.fieldOffset explicitly declines to duke@435: // guarantee this. The field offset values manipulated by the Java user duke@435: // through the Unsafe API are opaque cookies that just happen to be byte duke@435: // offsets. We represent this state of affairs by passing the cookies duke@435: // through conversion functions when going between the VM and the Unsafe API. duke@435: // The conversion functions just happen to be no-ops at present. duke@435: duke@435: inline jlong field_offset_to_byte_offset(jlong field_offset) { duke@435: return field_offset; duke@435: } duke@435: duke@435: inline jlong field_offset_from_byte_offset(jlong byte_offset) { duke@435: return byte_offset; duke@435: } duke@435: duke@435: inline jint invocation_key_from_method_slot(jint slot) { duke@435: return slot; duke@435: } duke@435: duke@435: inline jint invocation_key_to_method_slot(jint key) { duke@435: return key; duke@435: } duke@435: duke@435: inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) { duke@435: jlong byte_offset = field_offset_to_byte_offset(field_offset); never@2598: // Don't allow unsafe to be used to read or write the header word of oops never@2598: assert(p == NULL || field_offset >= oopDesc::header_size(), "offset must be outside of header"); duke@435: #ifdef ASSERT duke@435: if (p != NULL) { duke@435: assert(byte_offset >= 0 && byte_offset <= (jlong)MAX_OBJECT_SIZE, "sane offset"); duke@435: if (byte_offset == (jint)byte_offset) { duke@435: void* ptr_plus_disp = (address)p + byte_offset; coleenp@548: assert((void*)p->obj_field_addr((jint)byte_offset) == ptr_plus_disp, duke@435: "raw [ptr+disp] must be consistent with oop::field_base"); duke@435: } kvn@4200: jlong p_size = HeapWordSize * (jlong)(p->size()); kvn@4200: assert(byte_offset < p_size, err_msg("Unsafe access: offset " INT64_FORMAT " > object's size " INT64_FORMAT, byte_offset, p_size)); duke@435: } duke@435: #endif duke@435: if (sizeof(char*) == sizeof(jint)) // (this constant folds!) duke@435: return (address)p + (jint) byte_offset; duke@435: else duke@435: return (address)p + byte_offset; duke@435: } duke@435: duke@435: // Externally callable versions: duke@435: // (Use these in compiler intrinsics which emulate unsafe primitives.) duke@435: jlong Unsafe_field_offset_to_byte_offset(jlong field_offset) { duke@435: return field_offset; duke@435: } duke@435: jlong Unsafe_field_offset_from_byte_offset(jlong byte_offset) { duke@435: return byte_offset; duke@435: } duke@435: jint Unsafe_invocation_key_from_method_slot(jint slot) { duke@435: return invocation_key_from_method_slot(slot); duke@435: } duke@435: jint Unsafe_invocation_key_to_method_slot(jint key) { duke@435: return invocation_key_to_method_slot(key); duke@435: } duke@435: duke@435: duke@435: ///// Data in the Java heap. duke@435: duke@435: #define GET_FIELD(obj, offset, type_name, v) \ duke@435: oop p = JNIHandles::resolve(obj); \ duke@435: type_name v = *(type_name*)index_oop_from_field_offset_long(p, offset) duke@435: duke@435: #define SET_FIELD(obj, offset, type_name, x) \ duke@435: oop p = JNIHandles::resolve(obj); \ duke@435: *(type_name*)index_oop_from_field_offset_long(p, offset) = x duke@435: duke@435: #define GET_FIELD_VOLATILE(obj, offset, type_name, v) \ duke@435: oop p = JNIHandles::resolve(obj); \ kvn@2434: volatile type_name v = OrderAccess::load_acquire((volatile type_name*)index_oop_from_field_offset_long(p, offset)); duke@435: duke@435: #define SET_FIELD_VOLATILE(obj, offset, type_name, x) \ duke@435: oop p = JNIHandles::resolve(obj); \ kvn@2434: OrderAccess::release_store_fence((volatile type_name*)index_oop_from_field_offset_long(p, offset), x); duke@435: coleenp@548: // Macros for oops that check UseCompressedOops coleenp@548: coleenp@548: #define GET_OOP_FIELD(obj, offset, v) \ coleenp@548: oop p = JNIHandles::resolve(obj); \ coleenp@548: oop v; \ coleenp@548: if (UseCompressedOops) { \ coleenp@548: narrowOop n = *(narrowOop*)index_oop_from_field_offset_long(p, offset); \ coleenp@548: v = oopDesc::decode_heap_oop(n); \ coleenp@548: } else { \ coleenp@548: v = *(oop*)index_oop_from_field_offset_long(p, offset); \ coleenp@548: } coleenp@548: coleenp@548: duke@435: // Get/SetObject must be special-cased, since it works with handles. duke@435: duke@435: // The xxx140 variants for backward compatibility do not allow a full-width offset. duke@435: UNSAFE_ENTRY(jobject, Unsafe_GetObject140(JNIEnv *env, jobject unsafe, jobject obj, jint offset)) duke@435: UnsafeWrapper("Unsafe_GetObject"); duke@435: if (obj == NULL) THROW_0(vmSymbols::java_lang_NullPointerException()); coleenp@548: GET_OOP_FIELD(obj, offset, v) johnc@2781: jobject ret = JNIHandles::make_local(env, v); jprovino@4542: #if INCLUDE_ALL_GCS johnc@2781: // We could be accessing the referent field in a reference johnc@2781: // object. If G1 is enabled then we need to register a non-null johnc@2781: // referent with the SATB barrier. johnc@2781: if (UseG1GC) { johnc@2781: bool needs_barrier = false; johnc@2781: johnc@2781: if (ret != NULL) { johnc@2781: if (offset == java_lang_ref_Reference::referent_offset) { johnc@2781: oop o = JNIHandles::resolve_non_null(obj); coleenp@4037: Klass* k = o->klass(); coleenp@4037: if (InstanceKlass::cast(k)->reference_type() != REF_NONE) { coleenp@4037: assert(InstanceKlass::cast(k)->is_subclass_of(SystemDictionary::Reference_klass()), "sanity"); johnc@2781: needs_barrier = true; johnc@2781: } johnc@2781: } johnc@2781: } johnc@2781: johnc@2781: if (needs_barrier) { johnc@2781: oop referent = JNIHandles::resolve(ret); johnc@2781: G1SATBCardTableModRefBS::enqueue(referent); johnc@2781: } johnc@2781: } jprovino@4542: #endif // INCLUDE_ALL_GCS johnc@2781: return ret; duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetObject140(JNIEnv *env, jobject unsafe, jobject obj, jint offset, jobject x_h)) duke@435: UnsafeWrapper("Unsafe_SetObject"); duke@435: if (obj == NULL) THROW(vmSymbols::java_lang_NullPointerException()); duke@435: oop x = JNIHandles::resolve(x_h); duke@435: //SET_FIELD(obj, offset, oop, x); duke@435: oop p = JNIHandles::resolve(obj); coleenp@548: if (UseCompressedOops) { coleenp@548: if (x != NULL) { coleenp@548: // If there is a heap base pointer, we are obliged to emit a store barrier. coleenp@548: oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x); coleenp@548: } else { coleenp@548: narrowOop n = oopDesc::encode_heap_oop_not_null(x); coleenp@548: *(narrowOop*)index_oop_from_field_offset_long(p, offset) = n; coleenp@548: } duke@435: } else { coleenp@548: if (x != NULL) { coleenp@548: // If there is a heap base pointer, we are obliged to emit a store barrier. coleenp@548: oop_store((oop*)index_oop_from_field_offset_long(p, offset), x); coleenp@548: } else { coleenp@548: *(oop*)index_oop_from_field_offset_long(p, offset) = x; coleenp@548: } duke@435: } duke@435: UNSAFE_END duke@435: duke@435: // The normal variants allow a null base pointer with an arbitrary address. duke@435: // But if the base pointer is non-null, the offset should make some sense. duke@435: // That is, it should be in the range [0, MAX_OBJECT_SIZE]. duke@435: UNSAFE_ENTRY(jobject, Unsafe_GetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) duke@435: UnsafeWrapper("Unsafe_GetObject"); coleenp@548: GET_OOP_FIELD(obj, offset, v) johnc@2781: jobject ret = JNIHandles::make_local(env, v); jprovino@4542: #if INCLUDE_ALL_GCS johnc@2781: // We could be accessing the referent field in a reference johnc@2781: // object. If G1 is enabled then we need to register non-null johnc@2781: // referent with the SATB barrier. johnc@2781: if (UseG1GC) { johnc@2781: bool needs_barrier = false; johnc@2781: johnc@2781: if (ret != NULL) { johnc@2781: if (offset == java_lang_ref_Reference::referent_offset && obj != NULL) { johnc@2781: oop o = JNIHandles::resolve(obj); coleenp@4037: Klass* k = o->klass(); coleenp@4037: if (InstanceKlass::cast(k)->reference_type() != REF_NONE) { coleenp@4037: assert(InstanceKlass::cast(k)->is_subclass_of(SystemDictionary::Reference_klass()), "sanity"); johnc@2781: needs_barrier = true; johnc@2781: } johnc@2781: } johnc@2781: } johnc@2781: johnc@2781: if (needs_barrier) { johnc@2781: oop referent = JNIHandles::resolve(ret); johnc@2781: G1SATBCardTableModRefBS::enqueue(referent); johnc@2781: } johnc@2781: } jprovino@4542: #endif // INCLUDE_ALL_GCS johnc@2781: return ret; duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) duke@435: UnsafeWrapper("Unsafe_SetObject"); duke@435: oop x = JNIHandles::resolve(x_h); duke@435: oop p = JNIHandles::resolve(obj); coleenp@548: if (UseCompressedOops) { coleenp@548: oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x); coleenp@548: } else { coleenp@548: oop_store((oop*)index_oop_from_field_offset_long(p, offset), x); coleenp@548: } duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(jobject, Unsafe_GetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) duke@435: UnsafeWrapper("Unsafe_GetObjectVolatile"); twisti@3928: oop p = JNIHandles::resolve(obj); twisti@3928: void* addr = index_oop_from_field_offset_long(p, offset); twisti@3928: volatile oop v; twisti@3928: if (UseCompressedOops) { twisti@3928: volatile narrowOop n = *(volatile narrowOop*) addr; twisti@3928: v = oopDesc::decode_heap_oop(n); twisti@3928: } else { twisti@3928: v = *(volatile oop*) addr; twisti@3928: } twisti@3928: OrderAccess::acquire(); duke@435: return JNIHandles::make_local(env, v); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) duke@435: UnsafeWrapper("Unsafe_SetObjectVolatile"); duke@435: oop x = JNIHandles::resolve(x_h); duke@435: oop p = JNIHandles::resolve(obj); kvn@2434: void* addr = index_oop_from_field_offset_long(p, offset); kvn@2434: OrderAccess::release(); coleenp@548: if (UseCompressedOops) { kvn@2434: oop_store((narrowOop*)addr, x); coleenp@548: } else { kvn@2434: oop_store((oop*)addr, x); coleenp@548: } duke@435: OrderAccess::fence(); duke@435: UNSAFE_END duke@435: kvn@2434: #if defined(SPARC) || defined(X86) kvn@2434: // Sparc and X86 have atomic jlong (8 bytes) instructions kvn@2434: kvn@2434: #else kvn@2434: // Keep old code for platforms which may not have atomic jlong (8 bytes) instructions kvn@2434: duke@435: // Volatile long versions must use locks if !VM_Version::supports_cx8(). duke@435: // support_cx8 is a surrogate for 'supports atomic long memory ops'. duke@435: duke@435: UNSAFE_ENTRY(jlong, Unsafe_GetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) duke@435: UnsafeWrapper("Unsafe_GetLongVolatile"); duke@435: { duke@435: if (VM_Version::supports_cx8()) { duke@435: GET_FIELD_VOLATILE(obj, offset, jlong, v); duke@435: return v; duke@435: } duke@435: else { duke@435: Handle p (THREAD, JNIHandles::resolve(obj)); duke@435: jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset)); duke@435: ObjectLocker ol(p, THREAD); duke@435: jlong value = *addr; duke@435: return value; duke@435: } duke@435: } duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x)) duke@435: UnsafeWrapper("Unsafe_SetLongVolatile"); duke@435: { duke@435: if (VM_Version::supports_cx8()) { duke@435: SET_FIELD_VOLATILE(obj, offset, jlong, x); duke@435: } duke@435: else { duke@435: Handle p (THREAD, JNIHandles::resolve(obj)); duke@435: jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset)); duke@435: ObjectLocker ol(p, THREAD); duke@435: *addr = x; duke@435: } duke@435: } duke@435: UNSAFE_END duke@435: kvn@2434: #endif // not SPARC and not X86 duke@435: duke@435: #define DEFINE_GETSETOOP(jboolean, Boolean) \ duke@435: \ duke@435: UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset)) \ duke@435: UnsafeWrapper("Unsafe_Get"#Boolean); \ duke@435: if (obj == NULL) THROW_0(vmSymbols::java_lang_NullPointerException()); \ duke@435: GET_FIELD(obj, offset, jboolean, v); \ duke@435: return v; \ duke@435: UNSAFE_END \ duke@435: \ duke@435: UNSAFE_ENTRY(void, Unsafe_Set##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset, jboolean x)) \ duke@435: UnsafeWrapper("Unsafe_Set"#Boolean); \ duke@435: if (obj == NULL) THROW(vmSymbols::java_lang_NullPointerException()); \ duke@435: SET_FIELD(obj, offset, jboolean, x); \ duke@435: UNSAFE_END \ duke@435: \ duke@435: UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \ duke@435: UnsafeWrapper("Unsafe_Get"#Boolean); \ duke@435: GET_FIELD(obj, offset, jboolean, v); \ duke@435: return v; \ duke@435: UNSAFE_END \ duke@435: \ duke@435: UNSAFE_ENTRY(void, Unsafe_Set##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \ duke@435: UnsafeWrapper("Unsafe_Set"#Boolean); \ duke@435: SET_FIELD(obj, offset, jboolean, x); \ duke@435: UNSAFE_END \ duke@435: \ duke@435: // END DEFINE_GETSETOOP. duke@435: kvn@2434: DEFINE_GETSETOOP(jboolean, Boolean) kvn@2434: DEFINE_GETSETOOP(jbyte, Byte) kvn@2434: DEFINE_GETSETOOP(jshort, Short); kvn@2434: DEFINE_GETSETOOP(jchar, Char); kvn@2434: DEFINE_GETSETOOP(jint, Int); kvn@2434: DEFINE_GETSETOOP(jlong, Long); kvn@2434: DEFINE_GETSETOOP(jfloat, Float); kvn@2434: DEFINE_GETSETOOP(jdouble, Double); kvn@2434: kvn@2434: #undef DEFINE_GETSETOOP duke@435: duke@435: #define DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean) \ duke@435: \ duke@435: UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \ duke@435: UnsafeWrapper("Unsafe_Get"#Boolean); \ duke@435: GET_FIELD_VOLATILE(obj, offset, jboolean, v); \ duke@435: return v; \ duke@435: UNSAFE_END \ duke@435: \ duke@435: UNSAFE_ENTRY(void, Unsafe_Set##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \ duke@435: UnsafeWrapper("Unsafe_Set"#Boolean); \ duke@435: SET_FIELD_VOLATILE(obj, offset, jboolean, x); \ duke@435: UNSAFE_END \ duke@435: \ duke@435: // END DEFINE_GETSETOOP_VOLATILE. duke@435: duke@435: DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean) duke@435: DEFINE_GETSETOOP_VOLATILE(jbyte, Byte) duke@435: DEFINE_GETSETOOP_VOLATILE(jshort, Short); duke@435: DEFINE_GETSETOOP_VOLATILE(jchar, Char); duke@435: DEFINE_GETSETOOP_VOLATILE(jint, Int); duke@435: DEFINE_GETSETOOP_VOLATILE(jfloat, Float); duke@435: DEFINE_GETSETOOP_VOLATILE(jdouble, Double); duke@435: kvn@2434: #if defined(SPARC) || defined(X86) kvn@2434: // Sparc and X86 have atomic jlong (8 bytes) instructions kvn@2434: DEFINE_GETSETOOP_VOLATILE(jlong, Long); kvn@2434: #endif kvn@2434: kvn@2434: #undef DEFINE_GETSETOOP_VOLATILE duke@435: duke@435: // The non-intrinsified versions of setOrdered just use setVolatile duke@435: kvn@2434: UNSAFE_ENTRY(void, Unsafe_SetOrderedInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint x)) kvn@2434: UnsafeWrapper("Unsafe_SetOrderedInt"); kvn@2434: SET_FIELD_VOLATILE(obj, offset, jint, x); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetOrderedObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) duke@435: UnsafeWrapper("Unsafe_SetOrderedObject"); duke@435: oop x = JNIHandles::resolve(x_h); duke@435: oop p = JNIHandles::resolve(obj); kvn@2434: void* addr = index_oop_from_field_offset_long(p, offset); kvn@2434: OrderAccess::release(); coleenp@548: if (UseCompressedOops) { kvn@2434: oop_store((narrowOop*)addr, x); coleenp@548: } else { kvn@2434: oop_store((oop*)addr, x); coleenp@548: } duke@435: OrderAccess::fence(); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetOrderedLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x)) duke@435: UnsafeWrapper("Unsafe_SetOrderedLong"); kvn@2434: #if defined(SPARC) || defined(X86) kvn@2434: // Sparc and X86 have atomic jlong (8 bytes) instructions kvn@2434: SET_FIELD_VOLATILE(obj, offset, jlong, x); kvn@2434: #else kvn@2434: // Keep old code for platforms which may not have atomic long (8 bytes) instructions duke@435: { duke@435: if (VM_Version::supports_cx8()) { duke@435: SET_FIELD_VOLATILE(obj, offset, jlong, x); duke@435: } duke@435: else { duke@435: Handle p (THREAD, JNIHandles::resolve(obj)); duke@435: jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset)); duke@435: ObjectLocker ol(p, THREAD); duke@435: *addr = x; duke@435: } duke@435: } kvn@2434: #endif duke@435: UNSAFE_END duke@435: kvn@4361: UNSAFE_ENTRY(void, Unsafe_LoadFence(JNIEnv *env, jobject unsafe)) kvn@4361: UnsafeWrapper("Unsafe_LoadFence"); kvn@4361: OrderAccess::acquire(); kvn@4361: UNSAFE_END kvn@4361: kvn@4361: UNSAFE_ENTRY(void, Unsafe_StoreFence(JNIEnv *env, jobject unsafe)) kvn@4361: UnsafeWrapper("Unsafe_StoreFence"); kvn@4361: OrderAccess::release(); kvn@4361: UNSAFE_END kvn@4361: kvn@4361: UNSAFE_ENTRY(void, Unsafe_FullFence(JNIEnv *env, jobject unsafe)) kvn@4361: UnsafeWrapper("Unsafe_FullFence"); kvn@4361: OrderAccess::fence(); kvn@4361: UNSAFE_END kvn@4361: duke@435: ////// Data in the C heap. duke@435: duke@435: // Note: These do not throw NullPointerException for bad pointers. duke@435: // They just crash. Only a oop base pointer can generate a NullPointerException. duke@435: // duke@435: #define DEFINE_GETSETNATIVE(java_type, Type, native_type) \ duke@435: \ duke@435: UNSAFE_ENTRY(java_type, Unsafe_GetNative##Type(JNIEnv *env, jobject unsafe, jlong addr)) \ duke@435: UnsafeWrapper("Unsafe_GetNative"#Type); \ duke@435: void* p = addr_from_java(addr); \ duke@435: JavaThread* t = JavaThread::current(); \ duke@435: t->set_doing_unsafe_access(true); \ duke@435: java_type x = *(volatile native_type*)p; \ duke@435: t->set_doing_unsafe_access(false); \ duke@435: return x; \ duke@435: UNSAFE_END \ duke@435: \ duke@435: UNSAFE_ENTRY(void, Unsafe_SetNative##Type(JNIEnv *env, jobject unsafe, jlong addr, java_type x)) \ duke@435: UnsafeWrapper("Unsafe_SetNative"#Type); \ duke@435: JavaThread* t = JavaThread::current(); \ duke@435: t->set_doing_unsafe_access(true); \ duke@435: void* p = addr_from_java(addr); \ duke@435: *(volatile native_type*)p = x; \ duke@435: t->set_doing_unsafe_access(false); \ duke@435: UNSAFE_END \ duke@435: \ duke@435: // END DEFINE_GETSETNATIVE. duke@435: duke@435: DEFINE_GETSETNATIVE(jbyte, Byte, signed char) duke@435: DEFINE_GETSETNATIVE(jshort, Short, signed short); duke@435: DEFINE_GETSETNATIVE(jchar, Char, unsigned short); duke@435: DEFINE_GETSETNATIVE(jint, Int, jint); duke@435: // no long -- handled specially duke@435: DEFINE_GETSETNATIVE(jfloat, Float, float); duke@435: DEFINE_GETSETNATIVE(jdouble, Double, double); duke@435: duke@435: #undef DEFINE_GETSETNATIVE duke@435: duke@435: UNSAFE_ENTRY(jlong, Unsafe_GetNativeLong(JNIEnv *env, jobject unsafe, jlong addr)) duke@435: UnsafeWrapper("Unsafe_GetNativeLong"); duke@435: JavaThread* t = JavaThread::current(); duke@435: // We do it this way to avoid problems with access to heap using 64 duke@435: // bit loads, as jlong in heap could be not 64-bit aligned, and on duke@435: // some CPUs (SPARC) it leads to SIGBUS. duke@435: t->set_doing_unsafe_access(true); duke@435: void* p = addr_from_java(addr); duke@435: jlong x; duke@435: if (((intptr_t)p & 7) == 0) { duke@435: // jlong is aligned, do a volatile access duke@435: x = *(volatile jlong*)p; duke@435: } else { duke@435: jlong_accessor acc; duke@435: acc.words[0] = ((volatile jint*)p)[0]; duke@435: acc.words[1] = ((volatile jint*)p)[1]; duke@435: x = acc.long_value; duke@435: } duke@435: t->set_doing_unsafe_access(false); duke@435: return x; duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetNativeLong(JNIEnv *env, jobject unsafe, jlong addr, jlong x)) duke@435: UnsafeWrapper("Unsafe_SetNativeLong"); duke@435: JavaThread* t = JavaThread::current(); duke@435: // see comment for Unsafe_GetNativeLong duke@435: t->set_doing_unsafe_access(true); duke@435: void* p = addr_from_java(addr); duke@435: if (((intptr_t)p & 7) == 0) { duke@435: // jlong is aligned, do a volatile access duke@435: *(volatile jlong*)p = x; duke@435: } else { duke@435: jlong_accessor acc; duke@435: acc.long_value = x; duke@435: ((volatile jint*)p)[0] = acc.words[0]; duke@435: ((volatile jint*)p)[1] = acc.words[1]; duke@435: } duke@435: t->set_doing_unsafe_access(false); duke@435: UNSAFE_END duke@435: duke@435: duke@435: UNSAFE_ENTRY(jlong, Unsafe_GetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr)) duke@435: UnsafeWrapper("Unsafe_GetNativeAddress"); duke@435: void* p = addr_from_java(addr); duke@435: return addr_to_java(*(void**)p); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr, jlong x)) duke@435: UnsafeWrapper("Unsafe_SetNativeAddress"); duke@435: void* p = addr_from_java(addr); duke@435: *(void**)p = addr_from_java(x); duke@435: UNSAFE_END duke@435: duke@435: duke@435: ////// Allocation requests duke@435: duke@435: UNSAFE_ENTRY(jobject, Unsafe_AllocateInstance(JNIEnv *env, jobject unsafe, jclass cls)) duke@435: UnsafeWrapper("Unsafe_AllocateInstance"); duke@435: { duke@435: ThreadToNativeFromVM ttnfv(thread); duke@435: return env->AllocObject(cls); duke@435: } duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory(JNIEnv *env, jobject unsafe, jlong size)) duke@435: UnsafeWrapper("Unsafe_AllocateMemory"); duke@435: size_t sz = (size_t)size; duke@435: if (sz != (julong)size || size < 0) { duke@435: THROW_0(vmSymbols::java_lang_IllegalArgumentException()); duke@435: } duke@435: if (sz == 0) { duke@435: return 0; duke@435: } duke@435: sz = round_to(sz, HeapWordSize); zgu@3900: void* x = os::malloc(sz, mtInternal); duke@435: if (x == NULL) { duke@435: THROW_0(vmSymbols::java_lang_OutOfMemoryError()); duke@435: } duke@435: //Copy::fill_to_words((HeapWord*)x, sz / HeapWordSize); duke@435: return addr_to_java(x); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(jlong, Unsafe_ReallocateMemory(JNIEnv *env, jobject unsafe, jlong addr, jlong size)) duke@435: UnsafeWrapper("Unsafe_ReallocateMemory"); duke@435: void* p = addr_from_java(addr); duke@435: size_t sz = (size_t)size; duke@435: if (sz != (julong)size || size < 0) { duke@435: THROW_0(vmSymbols::java_lang_IllegalArgumentException()); duke@435: } duke@435: if (sz == 0) { duke@435: os::free(p); duke@435: return 0; duke@435: } duke@435: sz = round_to(sz, HeapWordSize); zgu@3900: void* x = (p == NULL) ? os::malloc(sz, mtInternal) : os::realloc(p, sz, mtInternal); duke@435: if (x == NULL) { duke@435: THROW_0(vmSymbols::java_lang_OutOfMemoryError()); duke@435: } duke@435: return addr_to_java(x); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_FreeMemory(JNIEnv *env, jobject unsafe, jlong addr)) duke@435: UnsafeWrapper("Unsafe_FreeMemory"); duke@435: void* p = addr_from_java(addr); duke@435: if (p == NULL) { duke@435: return; duke@435: } duke@435: os::free(p); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetMemory(JNIEnv *env, jobject unsafe, jlong addr, jlong size, jbyte value)) duke@435: UnsafeWrapper("Unsafe_SetMemory"); duke@435: size_t sz = (size_t)size; duke@435: if (sz != (julong)size || size < 0) { duke@435: THROW(vmSymbols::java_lang_IllegalArgumentException()); duke@435: } duke@435: char* p = (char*) addr_from_java(addr); duke@435: Copy::fill_to_memory_atomic(p, sz, value); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_SetMemory2(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong size, jbyte value)) duke@435: UnsafeWrapper("Unsafe_SetMemory"); duke@435: size_t sz = (size_t)size; duke@435: if (sz != (julong)size || size < 0) { duke@435: THROW(vmSymbols::java_lang_IllegalArgumentException()); duke@435: } duke@435: oop base = JNIHandles::resolve(obj); duke@435: void* p = index_oop_from_field_offset_long(base, offset); duke@435: Copy::fill_to_memory_atomic(p, sz, value); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_CopyMemory(JNIEnv *env, jobject unsafe, jlong srcAddr, jlong dstAddr, jlong size)) duke@435: UnsafeWrapper("Unsafe_CopyMemory"); duke@435: if (size == 0) { duke@435: return; duke@435: } duke@435: size_t sz = (size_t)size; duke@435: if (sz != (julong)size || size < 0) { duke@435: THROW(vmSymbols::java_lang_IllegalArgumentException()); duke@435: } duke@435: void* src = addr_from_java(srcAddr); duke@435: void* dst = addr_from_java(dstAddr); duke@435: Copy::conjoint_memory_atomic(src, dst, sz); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_CopyMemory2(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size)) duke@435: UnsafeWrapper("Unsafe_CopyMemory"); duke@435: if (size == 0) { duke@435: return; duke@435: } duke@435: size_t sz = (size_t)size; duke@435: if (sz != (julong)size || size < 0) { duke@435: THROW(vmSymbols::java_lang_IllegalArgumentException()); duke@435: } duke@435: oop srcp = JNIHandles::resolve(srcObj); duke@435: oop dstp = JNIHandles::resolve(dstObj); duke@435: if (dstp != NULL && !dstp->is_typeArray()) { duke@435: // NYI: This works only for non-oop arrays at present. duke@435: // Generalizing it would be reasonable, but requires card marking. duke@435: // Also, autoboxing a Long from 0L in copyMemory(x,y, 0L,z, n) would be bad. duke@435: THROW(vmSymbols::java_lang_IllegalArgumentException()); duke@435: } duke@435: void* src = index_oop_from_field_offset_long(srcp, srcOffset); duke@435: void* dst = index_oop_from_field_offset_long(dstp, dstOffset); duke@435: Copy::conjoint_memory_atomic(src, dst, sz); duke@435: UNSAFE_END duke@435: duke@435: duke@435: ////// Random queries duke@435: duke@435: // See comment at file start about UNSAFE_LEAF duke@435: //UNSAFE_LEAF(jint, Unsafe_AddressSize()) duke@435: UNSAFE_ENTRY(jint, Unsafe_AddressSize(JNIEnv *env, jobject unsafe)) duke@435: UnsafeWrapper("Unsafe_AddressSize"); duke@435: return sizeof(void*); duke@435: UNSAFE_END duke@435: duke@435: // See comment at file start about UNSAFE_LEAF duke@435: //UNSAFE_LEAF(jint, Unsafe_PageSize()) duke@435: UNSAFE_ENTRY(jint, Unsafe_PageSize(JNIEnv *env, jobject unsafe)) duke@435: UnsafeWrapper("Unsafe_PageSize"); duke@435: return os::vm_page_size(); duke@435: UNSAFE_END duke@435: duke@435: jint find_field_offset(jobject field, int must_be_static, TRAPS) { duke@435: if (field == NULL) { duke@435: THROW_0(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: duke@435: oop reflected = JNIHandles::resolve_non_null(field); duke@435: oop mirror = java_lang_reflect_Field::clazz(reflected); coleenp@4037: Klass* k = java_lang_Class::as_Klass(mirror); duke@435: int slot = java_lang_reflect_Field::slot(reflected); duke@435: int modifiers = java_lang_reflect_Field::modifiers(reflected); duke@435: duke@435: if (must_be_static >= 0) { duke@435: int really_is_static = ((modifiers & JVM_ACC_STATIC) != 0); duke@435: if (must_be_static != really_is_static) { duke@435: THROW_0(vmSymbols::java_lang_IllegalArgumentException()); duke@435: } duke@435: } duke@435: coleenp@4037: int offset = InstanceKlass::cast(k)->field_offset(slot); duke@435: return field_offset_from_byte_offset(offset); duke@435: } duke@435: duke@435: UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset(JNIEnv *env, jobject unsafe, jobject field)) duke@435: UnsafeWrapper("Unsafe_ObjectFieldOffset"); duke@435: return find_field_offset(field, 0, THREAD); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset(JNIEnv *env, jobject unsafe, jobject field)) duke@435: UnsafeWrapper("Unsafe_StaticFieldOffset"); duke@435: return find_field_offset(field, 1, THREAD); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromField(JNIEnv *env, jobject unsafe, jobject field)) duke@435: UnsafeWrapper("Unsafe_StaticFieldBase"); duke@435: // Note: In this VM implementation, a field address is always a short duke@435: // offset from the base of a a klass metaobject. Thus, the full dynamic duke@435: // range of the return type is never used. However, some implementations duke@435: // might put the static field inside an array shared by many classes, duke@435: // or even at a fixed address, in which case the address could be quite duke@435: // large. In that last case, this function would return NULL, since duke@435: // the address would operate alone, without any base pointer. duke@435: duke@435: if (field == NULL) THROW_0(vmSymbols::java_lang_NullPointerException()); duke@435: duke@435: oop reflected = JNIHandles::resolve_non_null(field); duke@435: oop mirror = java_lang_reflect_Field::clazz(reflected); duke@435: int modifiers = java_lang_reflect_Field::modifiers(reflected); duke@435: duke@435: if ((modifiers & JVM_ACC_STATIC) == 0) { duke@435: THROW_0(vmSymbols::java_lang_IllegalArgumentException()); duke@435: } duke@435: never@2658: return JNIHandles::make_local(env, mirror); duke@435: UNSAFE_END duke@435: duke@435: //@deprecated duke@435: UNSAFE_ENTRY(jint, Unsafe_FieldOffset(JNIEnv *env, jobject unsafe, jobject field)) duke@435: UnsafeWrapper("Unsafe_FieldOffset"); duke@435: // tries (but fails) to be polymorphic between static and non-static: duke@435: jlong offset = find_field_offset(field, -1, THREAD); duke@435: guarantee(offset == (jint)offset, "offset fits in 32 bits"); duke@435: return (jint)offset; duke@435: UNSAFE_END duke@435: duke@435: //@deprecated duke@435: UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromClass(JNIEnv *env, jobject unsafe, jobject clazz)) duke@435: UnsafeWrapper("Unsafe_StaticFieldBase"); duke@435: if (clazz == NULL) { duke@435: THROW_0(vmSymbols::java_lang_NullPointerException()); duke@435: } never@2658: return JNIHandles::make_local(env, JNIHandles::resolve_non_null(clazz)); duke@435: UNSAFE_END duke@435: twisti@3969: UNSAFE_ENTRY(void, Unsafe_EnsureClassInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) { duke@435: UnsafeWrapper("Unsafe_EnsureClassInitialized"); duke@435: if (clazz == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: oop mirror = JNIHandles::resolve_non_null(clazz); twisti@3969: coleenp@4037: Klass* klass = java_lang_Class::as_Klass(mirror); hseigel@4278: if (klass != NULL && klass->should_be_initialized()) { coleenp@4037: InstanceKlass* k = InstanceKlass::cast(klass); duke@435: k->initialize(CHECK); duke@435: } twisti@3969: } twisti@3969: UNSAFE_END twisti@3969: twisti@3969: UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) { twisti@3969: UnsafeWrapper("Unsafe_ShouldBeInitialized"); twisti@3969: if (clazz == NULL) { twisti@3969: THROW_(vmSymbols::java_lang_NullPointerException(), false); twisti@3969: } twisti@3969: oop mirror = JNIHandles::resolve_non_null(clazz); coleenp@4037: Klass* klass = java_lang_Class::as_Klass(mirror); hseigel@4278: if (klass != NULL && klass->should_be_initialized()) { twisti@3969: return true; twisti@3969: } twisti@3969: return false; twisti@3969: } duke@435: UNSAFE_END duke@435: duke@435: static void getBaseAndScale(int& base, int& scale, jclass acls, TRAPS) { duke@435: if (acls == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: oop mirror = JNIHandles::resolve_non_null(acls); coleenp@4037: Klass* k = java_lang_Class::as_Klass(mirror); coleenp@4037: if (k == NULL || !k->oop_is_array()) { duke@435: THROW(vmSymbols::java_lang_InvalidClassException()); coleenp@4037: } else if (k->oop_is_objArray()) { duke@435: base = arrayOopDesc::base_offset_in_bytes(T_OBJECT); coleenp@548: scale = heapOopSize; coleenp@4037: } else if (k->oop_is_typeArray()) { coleenp@4142: TypeArrayKlass* tak = TypeArrayKlass::cast(k); duke@435: base = tak->array_header_in_bytes(); duke@435: assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok"); duke@435: scale = (1 << tak->log2_element_size()); duke@435: } else { duke@435: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: duke@435: UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset(JNIEnv *env, jobject unsafe, jclass acls)) duke@435: UnsafeWrapper("Unsafe_ArrayBaseOffset"); duke@435: int base, scale; duke@435: getBaseAndScale(base, scale, acls, CHECK_0); duke@435: return field_offset_from_byte_offset(base); duke@435: UNSAFE_END duke@435: duke@435: duke@435: UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale(JNIEnv *env, jobject unsafe, jclass acls)) duke@435: UnsafeWrapper("Unsafe_ArrayIndexScale"); duke@435: int base, scale; duke@435: getBaseAndScale(base, scale, acls, CHECK_0); duke@435: // This VM packs both fields and array elements down to the byte. duke@435: // But watch out: If this changes, so that array references for duke@435: // a given primitive type (say, T_BOOLEAN) use different memory units duke@435: // than fields, this method MUST return zero for such arrays. duke@435: // For example, the VM used to store sub-word sized fields in full duke@435: // words in the object layout, so that accessors like getByte(Object,int) duke@435: // did not really do what one might expect for arrays. Therefore, duke@435: // this function used to report a zero scale factor, so that the user duke@435: // would know not to attempt to access sub-word array elements. duke@435: // // Code for unpacked fields: duke@435: // if (scale < wordSize) return 0; duke@435: duke@435: // The following allows for a pretty general fieldOffset cookie scheme, duke@435: // but requires it to be linear in byte offset. duke@435: return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0); duke@435: UNSAFE_END duke@435: duke@435: duke@435: static inline void throw_new(JNIEnv *env, const char *ename) { duke@435: char buf[100]; duke@435: strcpy(buf, "java/lang/"); duke@435: strcat(buf, ename); duke@435: jclass cls = env->FindClass(buf); duke@435: char* msg = NULL; duke@435: env->ThrowNew(cls, msg); duke@435: } duke@435: duke@435: static jclass Unsafe_DefineClass(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) { duke@435: { duke@435: // Code lifted from JDK 1.3 ClassLoader.c duke@435: duke@435: jbyte *body; duke@435: char *utfName; duke@435: jclass result = 0; duke@435: char buf[128]; duke@435: duke@435: if (UsePerfData) { duke@435: ClassLoader::unsafe_defineClassCallCounter()->inc(); duke@435: } duke@435: duke@435: if (data == NULL) { duke@435: throw_new(env, "NullPointerException"); duke@435: return 0; duke@435: } duke@435: duke@435: /* Work around 4153825. malloc crashes on Solaris when passed a duke@435: * negative size. duke@435: */ duke@435: if (length < 0) { duke@435: throw_new(env, "ArrayIndexOutOfBoundsException"); duke@435: return 0; duke@435: } duke@435: zgu@3900: body = NEW_C_HEAP_ARRAY(jbyte, length, mtInternal); duke@435: duke@435: if (body == 0) { duke@435: throw_new(env, "OutOfMemoryError"); duke@435: return 0; duke@435: } duke@435: duke@435: env->GetByteArrayRegion(data, offset, length, body); duke@435: duke@435: if (env->ExceptionOccurred()) duke@435: goto free_body; duke@435: duke@435: if (name != NULL) { duke@435: uint len = env->GetStringUTFLength(name); duke@435: int unicode_len = env->GetStringLength(name); duke@435: if (len >= sizeof(buf)) { zgu@3900: utfName = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); duke@435: if (utfName == NULL) { duke@435: throw_new(env, "OutOfMemoryError"); duke@435: goto free_body; duke@435: } duke@435: } else { duke@435: utfName = buf; duke@435: } duke@435: env->GetStringUTFRegion(name, 0, unicode_len, utfName); duke@435: //VerifyFixClassname(utfName); duke@435: for (uint i = 0; i < len; i++) { duke@435: if (utfName[i] == '.') utfName[i] = '/'; duke@435: } duke@435: } else { duke@435: utfName = NULL; duke@435: } duke@435: duke@435: result = JVM_DefineClass(env, utfName, loader, body, length, pd); duke@435: duke@435: if (utfName && utfName != buf) zgu@3900: FREE_C_HEAP_ARRAY(char, utfName, mtInternal); duke@435: duke@435: free_body: zgu@3900: FREE_C_HEAP_ARRAY(jbyte, body, mtInternal); duke@435: return result; duke@435: } duke@435: } duke@435: duke@435: duke@435: UNSAFE_ENTRY(jclass, Unsafe_DefineClass0(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length)) duke@435: UnsafeWrapper("Unsafe_DefineClass"); duke@435: { duke@435: ThreadToNativeFromVM ttnfv(thread); duke@435: duke@435: int depthFromDefineClass0 = 1; duke@435: jclass caller = JVM_GetCallerClass(env, depthFromDefineClass0); duke@435: jobject loader = (caller == NULL) ? NULL : JVM_GetClassLoader(env, caller); duke@435: jobject pd = (caller == NULL) ? NULL : JVM_GetProtectionDomain(env, caller); duke@435: duke@435: return Unsafe_DefineClass(env, name, data, offset, length, loader, pd); duke@435: } duke@435: UNSAFE_END duke@435: duke@435: duke@435: UNSAFE_ENTRY(jclass, Unsafe_DefineClass1(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd)) duke@435: UnsafeWrapper("Unsafe_DefineClass"); duke@435: { duke@435: ThreadToNativeFromVM ttnfv(thread); duke@435: duke@435: return Unsafe_DefineClass(env, name, data, offset, length, loader, pd); duke@435: } duke@435: UNSAFE_END duke@435: jrose@866: #define DAC_Args CLS"[B["OBJ jrose@866: // define a class but do not make it known to the class loader or system dictionary jrose@866: // - host_class: supplies context for linkage, access control, protection domain, and class loader jrose@866: // - data: bytes of a class file, a raw memory address (length gives the number of bytes) jrose@866: // - cp_patches: where non-null entries exist, they replace corresponding CP entries in data jrose@866: jrose@866: // When you load an anonymous class U, it works as if you changed its name just before loading, jrose@866: // to a name that you will never use again. Since the name is lost, no other class can directly jrose@866: // link to any member of U. Just after U is loaded, the only way to use it is reflectively, jrose@866: // through java.lang.Class methods like Class.newInstance. jrose@866: jrose@866: // Access checks for linkage sites within U continue to follow the same rules as for named classes. jrose@866: // The package of an anonymous class is given by the package qualifier on the name under which it was loaded. jrose@866: // An anonymous class also has special privileges to access any member of its host class. jrose@866: // This is the main reason why this loading operation is unsafe. The purpose of this is to jrose@866: // allow language implementations to simulate "open classes"; a host class in effect gets jrose@866: // new code when an anonymous class is loaded alongside it. A less convenient but more jrose@866: // standard way to do this is with reflection, which can also be set to ignore access jrose@866: // restrictions. jrose@866: jrose@866: // Access into an anonymous class is possible only through reflection. Therefore, there jrose@866: // are no special access rules for calling into an anonymous class. The relaxed access jrose@866: // rule for the host class is applied in the opposite direction: A host class reflectively jrose@866: // access one of its anonymous classes. jrose@866: jrose@866: // If you load the same bytecodes twice, you get two different classes. You can reload jrose@866: // the same bytecodes with or without varying CP patches. jrose@866: jrose@866: // By using the CP patching array, you can have a new anonymous class U2 refer to an older one U1. jrose@866: // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is). jrose@866: // The CONSTANT_Class entry for that name can be patched to refer directly to U1. jrose@866: jrose@866: // This allows, for example, U2 to use U1 as a superclass or super-interface, or as jrose@866: // an outer class (so that U2 is an anonymous inner class of anonymous U1). jrose@866: // It is not possible for a named class, or an older anonymous class, to refer by jrose@866: // name (via its CP) to a newer anonymous class. jrose@866: jrose@866: // CP patching may also be used to modify (i.e., hack) the names of methods, classes, jrose@866: // or type descriptors used in the loaded anonymous class. jrose@866: jrose@866: // Finally, CP patching may be used to introduce "live" objects into the constant pool, jrose@866: // instead of "dead" strings. A compiled statement like println((Object)"hello") can jrose@866: // be changed to println(greeting), where greeting is an arbitrary object created before jrose@866: // the anonymous class is loaded. This is useful in dynamic languages, in which jrose@866: // various kinds of metaobjects must be introduced as constants into bytecode. jrose@866: // Note the cast (Object), which tells the verifier to expect an arbitrary object, jrose@866: // not just a literal string. For such ldc instructions, the verifier uses the jrose@866: // type Object instead of String, if the loaded constant is not in fact a String. jrose@866: coleenp@4304: static instanceKlassHandle jrose@866: Unsafe_DefineAnonymousClass_impl(JNIEnv *env, jrose@866: jclass host_class, jbyteArray data, jobjectArray cp_patches_jh, jrose@866: HeapWord* *temp_alloc, jrose@866: TRAPS) { jrose@866: jrose@866: if (UsePerfData) { jrose@866: ClassLoader::unsafe_defineClassCallCounter()->inc(); jrose@866: } jrose@866: jrose@866: if (data == NULL) { jrose@866: THROW_0(vmSymbols::java_lang_NullPointerException()); jrose@866: } jrose@866: jrose@866: jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length(); jrose@866: jint word_length = (length + sizeof(HeapWord)-1) / sizeof(HeapWord); zgu@3900: HeapWord* body = NEW_C_HEAP_ARRAY(HeapWord, word_length, mtInternal); jrose@866: if (body == NULL) { jrose@866: THROW_0(vmSymbols::java_lang_OutOfMemoryError()); jrose@866: } jrose@866: jrose@866: // caller responsible to free it: jrose@866: (*temp_alloc) = body; jrose@866: jrose@866: { jrose@866: jbyte* array_base = typeArrayOop(JNIHandles::resolve_non_null(data))->byte_at_addr(0); jrose@866: Copy::conjoint_words((HeapWord*) array_base, body, word_length); jrose@866: } jrose@866: jrose@866: u1* class_bytes = (u1*) body; jrose@866: int class_bytes_length = (int) length; jrose@866: if (class_bytes_length < 0) class_bytes_length = 0; jrose@866: if (class_bytes == NULL jrose@866: || host_class == NULL jrose@866: || length != class_bytes_length) jrose@866: THROW_0(vmSymbols::java_lang_IllegalArgumentException()); jrose@866: jrose@866: objArrayHandle cp_patches_h; jrose@866: if (cp_patches_jh != NULL) { jrose@866: oop p = JNIHandles::resolve_non_null(cp_patches_jh); jrose@866: if (!p->is_objArray()) jrose@866: THROW_0(vmSymbols::java_lang_IllegalArgumentException()); jrose@866: cp_patches_h = objArrayHandle(THREAD, (objArrayOop)p); jrose@866: } jrose@866: coleenp@4037: KlassHandle host_klass(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(host_class))); jrose@866: const char* host_source = host_klass->external_name(); jrose@866: Handle host_loader(THREAD, host_klass->class_loader()); jrose@866: Handle host_domain(THREAD, host_klass->protection_domain()); jrose@866: jrose@866: GrowableArray* cp_patches = NULL; jrose@866: if (cp_patches_h.not_null()) { jrose@866: int alen = cp_patches_h->length(); jrose@866: for (int i = alen-1; i >= 0; i--) { jrose@866: oop p = cp_patches_h->obj_at(i); jrose@866: if (p != NULL) { jrose@866: Handle patch(THREAD, p); jrose@866: if (cp_patches == NULL) jrose@866: cp_patches = new GrowableArray(i+1, i+1, Handle()); jrose@866: cp_patches->at_put(i, patch); jrose@866: } jrose@866: } jrose@866: } jrose@866: jrose@866: ClassFileStream st(class_bytes, class_bytes_length, (char*) host_source); jrose@866: jrose@866: instanceKlassHandle anon_klass; jrose@866: { coleenp@2497: Symbol* no_class_name = NULL; coleenp@4037: Klass* anonk = SystemDictionary::parse_stream(no_class_name, jrose@866: host_loader, host_domain, jrose@866: &st, host_klass, cp_patches, jrose@866: CHECK_NULL); jrose@866: if (anonk == NULL) return NULL; jrose@866: anon_klass = instanceKlassHandle(THREAD, anonk); jrose@866: } jrose@866: coleenp@4304: return anon_klass; jrose@866: } jrose@866: jrose@866: UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh)) jrose@866: { coleenp@4304: instanceKlassHandle anon_klass; coleenp@4304: jobject res_jh = NULL; coleenp@4304: jrose@866: UnsafeWrapper("Unsafe_DefineAnonymousClass"); jrose@866: ResourceMark rm(THREAD); jrose@866: jrose@866: HeapWord* temp_alloc = NULL; jrose@866: coleenp@4304: anon_klass = Unsafe_DefineAnonymousClass_impl(env, host_class, data, coleenp@4304: cp_patches_jh, jrose@866: &temp_alloc, THREAD); coleenp@4304: if (anon_klass() != NULL) coleenp@4304: res_jh = JNIHandles::make_local(env, anon_klass->java_mirror()); jrose@866: jrose@866: // try/finally clause: jrose@866: if (temp_alloc != NULL) { zgu@3900: FREE_C_HEAP_ARRAY(HeapWord, temp_alloc, mtInternal); jrose@866: } jrose@866: coleenp@4304: // The anonymous class loader data has been artificially been kept alive to coleenp@4304: // this point. The mirror and any instances of this class have to keep coleenp@4304: // it alive afterwards. coleenp@4304: if (anon_klass() != NULL) { coleenp@4304: anon_klass->class_loader_data()->set_keep_alive(false); coleenp@4304: } coleenp@4304: coleenp@4304: // let caller initialize it as needed... coleenp@4304: jrose@866: return (jclass) res_jh; jrose@866: } jrose@866: UNSAFE_END jrose@866: jrose@866: duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_MonitorEnter(JNIEnv *env, jobject unsafe, jobject jobj)) duke@435: UnsafeWrapper("Unsafe_MonitorEnter"); duke@435: { duke@435: if (jobj == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: Handle obj(thread, JNIHandles::resolve_non_null(jobj)); duke@435: ObjectSynchronizer::jni_enter(obj, CHECK); duke@435: } duke@435: UNSAFE_END duke@435: duke@435: duke@435: UNSAFE_ENTRY(jboolean, Unsafe_TryMonitorEnter(JNIEnv *env, jobject unsafe, jobject jobj)) duke@435: UnsafeWrapper("Unsafe_TryMonitorEnter"); duke@435: { duke@435: if (jobj == NULL) { duke@435: THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE); duke@435: } duke@435: Handle obj(thread, JNIHandles::resolve_non_null(jobj)); duke@435: bool res = ObjectSynchronizer::jni_try_enter(obj, CHECK_0); duke@435: return (res ? JNI_TRUE : JNI_FALSE); duke@435: } duke@435: UNSAFE_END duke@435: duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_MonitorExit(JNIEnv *env, jobject unsafe, jobject jobj)) duke@435: UnsafeWrapper("Unsafe_MonitorExit"); duke@435: { duke@435: if (jobj == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: Handle obj(THREAD, JNIHandles::resolve_non_null(jobj)); duke@435: ObjectSynchronizer::jni_exit(obj(), CHECK); duke@435: } duke@435: UNSAFE_END duke@435: duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr)) duke@435: UnsafeWrapper("Unsafe_ThrowException"); duke@435: { duke@435: ThreadToNativeFromVM ttnfv(thread); duke@435: env->Throw(thr); duke@435: } duke@435: UNSAFE_END duke@435: duke@435: // JSR166 ------------------------------------------------------------------ duke@435: duke@435: UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) duke@435: UnsafeWrapper("Unsafe_CompareAndSwapObject"); duke@435: oop x = JNIHandles::resolve(x_h); duke@435: oop e = JNIHandles::resolve(e_h); duke@435: oop p = JNIHandles::resolve(obj); coleenp@548: HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset); coleenp@4037: oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e, true); coleenp@548: jboolean success = (res == e); duke@435: if (success) coleenp@548: update_barrier_set((void*)addr, x); duke@435: return success; duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) duke@435: UnsafeWrapper("Unsafe_CompareAndSwapInt"); duke@435: oop p = JNIHandles::resolve(obj); duke@435: jint* addr = (jint *) index_oop_from_field_offset_long(p, offset); duke@435: return (jint)(Atomic::cmpxchg(x, addr, e)) == e; duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) duke@435: UnsafeWrapper("Unsafe_CompareAndSwapLong"); duke@435: Handle p (THREAD, JNIHandles::resolve(obj)); duke@435: jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset)); duke@435: if (VM_Version::supports_cx8()) duke@435: return (jlong)(Atomic::cmpxchg(x, addr, e)) == e; duke@435: else { duke@435: jboolean success = false; duke@435: ObjectLocker ol(p, THREAD); duke@435: if (*addr == e) { *addr = x; success = true; } duke@435: return success; duke@435: } duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time)) duke@435: UnsafeWrapper("Unsafe_Park"); dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE3(hotspot, thread__park__begin, thread->parker(), (int) isAbsolute, time); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_THREAD_PARK_BEGIN( dcubed@3202: (uintptr_t) thread->parker(), (int) isAbsolute, time); dcubed@3202: #endif /* USDT2 */ duke@435: JavaThreadParkedState jtps(thread, time != 0); duke@435: thread->parker()->park(isAbsolute != 0, time); dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE1(hotspot, thread__park__end, thread->parker()); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_THREAD_PARK_END( dcubed@3202: (uintptr_t) thread->parker()); dcubed@3202: #endif /* USDT2 */ duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread)) duke@435: UnsafeWrapper("Unsafe_Unpark"); duke@435: Parker* p = NULL; duke@435: if (jthread != NULL) { duke@435: oop java_thread = JNIHandles::resolve_non_null(jthread); duke@435: if (java_thread != NULL) { duke@435: jlong lp = java_lang_Thread::park_event(java_thread); duke@435: if (lp != 0) { duke@435: // This cast is OK even though the jlong might have been read duke@435: // non-atomically on 32bit systems, since there, one word will duke@435: // always be zero anyway and the value set is always the same duke@435: p = (Parker*)addr_from_java(lp); duke@435: } else { duke@435: // Grab lock if apparently null or using older version of library duke@435: MutexLocker mu(Threads_lock); duke@435: java_thread = JNIHandles::resolve_non_null(jthread); duke@435: if (java_thread != NULL) { duke@435: JavaThread* thr = java_lang_Thread::thread(java_thread); duke@435: if (thr != NULL) { duke@435: p = thr->parker(); duke@435: if (p != NULL) { // Bind to Java thread for next time. duke@435: java_lang_Thread::set_park_event(java_thread, addr_to_java(p)); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: if (p != NULL) { dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE1(hotspot, thread__unpark, p); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_THREAD_UNPARK( dcubed@3202: (uintptr_t) p); dcubed@3202: #endif /* USDT2 */ duke@435: p->unpark(); duke@435: } duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(jint, Unsafe_Loadavg(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem)) duke@435: UnsafeWrapper("Unsafe_Loadavg"); duke@435: const int max_nelem = 3; duke@435: double la[max_nelem]; duke@435: jint ret; duke@435: duke@435: typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg)); duke@435: assert(a->is_typeArray(), "must be type array"); duke@435: duke@435: if (nelem < 0 || nelem > max_nelem || a->length() < nelem) { duke@435: ThreadToNativeFromVM ttnfv(thread); duke@435: throw_new(env, "ArrayIndexOutOfBoundsException"); duke@435: return -1; duke@435: } duke@435: duke@435: ret = os::loadavg(la, nelem); duke@435: if (ret == -1) return -1; duke@435: duke@435: // if successful, ret is the number of samples actually retrieved. duke@435: assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value"); duke@435: switch(ret) { duke@435: case 3: a->double_at_put(2, (jdouble)la[2]); // fall through duke@435: case 2: a->double_at_put(1, (jdouble)la[1]); // fall through duke@435: case 1: a->double_at_put(0, (jdouble)la[0]); break; duke@435: } duke@435: return ret; duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_PrefetchRead(JNIEnv* env, jclass ignored, jobject obj, jlong offset)) duke@435: UnsafeWrapper("Unsafe_PrefetchRead"); duke@435: oop p = JNIHandles::resolve(obj); duke@435: void* addr = index_oop_from_field_offset_long(p, 0); duke@435: Prefetch::read(addr, (intx)offset); duke@435: UNSAFE_END duke@435: duke@435: UNSAFE_ENTRY(void, Unsafe_PrefetchWrite(JNIEnv* env, jclass ignored, jobject obj, jlong offset)) duke@435: UnsafeWrapper("Unsafe_PrefetchWrite"); duke@435: oop p = JNIHandles::resolve(obj); duke@435: void* addr = index_oop_from_field_offset_long(p, 0); duke@435: Prefetch::write(addr, (intx)offset); duke@435: UNSAFE_END duke@435: duke@435: duke@435: /// JVM_RegisterUnsafeMethods duke@435: duke@435: #define ADR "J" duke@435: duke@435: #define LANG "Ljava/lang/" duke@435: duke@435: #define OBJ LANG"Object;" duke@435: #define CLS LANG"Class;" duke@435: #define CTR LANG"reflect/Constructor;" duke@435: #define FLD LANG"reflect/Field;" duke@435: #define MTH LANG"reflect/Method;" duke@435: #define THR LANG"Throwable;" duke@435: duke@435: #define DC0_Args LANG"String;[BII" duke@435: #define DC1_Args DC0_Args LANG"ClassLoader;" "Ljava/security/ProtectionDomain;" duke@435: duke@435: #define CC (char*) /*cast a literal from (const char*)*/ duke@435: #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) duke@435: duke@435: // define deprecated accessors for compabitility with 1.4.0 duke@435: #define DECLARE_GETSETOOP_140(Boolean, Z) \ duke@435: {CC"get"#Boolean, CC"("OBJ"I)"#Z, FN_PTR(Unsafe_Get##Boolean##140)}, \ duke@435: {CC"put"#Boolean, CC"("OBJ"I"#Z")V", FN_PTR(Unsafe_Set##Boolean##140)} duke@435: duke@435: // Note: In 1.4.1, getObject and kin take both int and long offsets. duke@435: #define DECLARE_GETSETOOP_141(Boolean, Z) \ duke@435: {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \ duke@435: {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)} duke@435: duke@435: // Note: In 1.5.0, there are volatile versions too duke@435: #define DECLARE_GETSETOOP(Boolean, Z) \ duke@435: {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \ duke@435: {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)}, \ duke@435: {CC"get"#Boolean"Volatile", CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean##Volatile)}, \ duke@435: {CC"put"#Boolean"Volatile", CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean##Volatile)} duke@435: duke@435: duke@435: #define DECLARE_GETSETNATIVE(Byte, B) \ duke@435: {CC"get"#Byte, CC"("ADR")"#B, FN_PTR(Unsafe_GetNative##Byte)}, \ duke@435: {CC"put"#Byte, CC"("ADR#B")V", FN_PTR(Unsafe_SetNative##Byte)} duke@435: duke@435: duke@435: duke@435: // %%% These are temporarily supported until the SDK sources duke@435: // contain the necessarily updated Unsafe.java. duke@435: static JNINativeMethod methods_140[] = { duke@435: duke@435: {CC"getObject", CC"("OBJ"I)"OBJ"", FN_PTR(Unsafe_GetObject140)}, duke@435: {CC"putObject", CC"("OBJ"I"OBJ")V", FN_PTR(Unsafe_SetObject140)}, duke@435: duke@435: DECLARE_GETSETOOP_140(Boolean, Z), duke@435: DECLARE_GETSETOOP_140(Byte, B), duke@435: DECLARE_GETSETOOP_140(Short, S), duke@435: DECLARE_GETSETOOP_140(Char, C), duke@435: DECLARE_GETSETOOP_140(Int, I), duke@435: DECLARE_GETSETOOP_140(Long, J), duke@435: DECLARE_GETSETOOP_140(Float, F), duke@435: DECLARE_GETSETOOP_140(Double, D), duke@435: duke@435: DECLARE_GETSETNATIVE(Byte, B), duke@435: DECLARE_GETSETNATIVE(Short, S), duke@435: DECLARE_GETSETNATIVE(Char, C), duke@435: DECLARE_GETSETNATIVE(Int, I), duke@435: DECLARE_GETSETNATIVE(Long, J), duke@435: DECLARE_GETSETNATIVE(Float, F), duke@435: DECLARE_GETSETNATIVE(Double, D), duke@435: duke@435: {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)}, duke@435: {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)}, duke@435: duke@435: {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)}, duke@435: {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)}, duke@435: // {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)}, duke@435: // {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)}, duke@435: {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)}, duke@435: duke@435: {CC"fieldOffset", CC"("FLD")I", FN_PTR(Unsafe_FieldOffset)}, //deprecated duke@435: {CC"staticFieldBase", CC"("CLS")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromClass)}, //deprecated duke@435: {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)}, duke@435: {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)}, duke@435: {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)}, duke@435: {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)}, duke@435: {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)}, duke@435: duke@435: {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)}, duke@435: {CC"defineClass", CC"("DC1_Args")"CLS, FN_PTR(Unsafe_DefineClass1)}, duke@435: {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)}, duke@435: {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)}, duke@435: {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)}, duke@435: {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)} duke@435: }; duke@435: duke@435: // These are the old methods prior to the JSR 166 changes in 1.5.0 duke@435: static JNINativeMethod methods_141[] = { duke@435: duke@435: {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)}, duke@435: {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)}, duke@435: duke@435: DECLARE_GETSETOOP_141(Boolean, Z), duke@435: DECLARE_GETSETOOP_141(Byte, B), duke@435: DECLARE_GETSETOOP_141(Short, S), duke@435: DECLARE_GETSETOOP_141(Char, C), duke@435: DECLARE_GETSETOOP_141(Int, I), duke@435: DECLARE_GETSETOOP_141(Long, J), duke@435: DECLARE_GETSETOOP_141(Float, F), duke@435: DECLARE_GETSETOOP_141(Double, D), duke@435: duke@435: DECLARE_GETSETNATIVE(Byte, B), duke@435: DECLARE_GETSETNATIVE(Short, S), duke@435: DECLARE_GETSETNATIVE(Char, C), duke@435: DECLARE_GETSETNATIVE(Int, I), duke@435: DECLARE_GETSETNATIVE(Long, J), duke@435: DECLARE_GETSETNATIVE(Float, F), duke@435: DECLARE_GETSETNATIVE(Double, D), duke@435: duke@435: {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)}, duke@435: {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)}, duke@435: duke@435: {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)}, duke@435: {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)}, duke@435: // {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)}, duke@435: // {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)}, duke@435: {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)}, duke@435: duke@435: {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)}, duke@435: {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)}, duke@435: {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, duke@435: {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)}, duke@435: {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)}, duke@435: {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)}, duke@435: {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)}, duke@435: {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)}, duke@435: duke@435: {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)}, duke@435: {CC"defineClass", CC"("DC1_Args")"CLS, FN_PTR(Unsafe_DefineClass1)}, duke@435: {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)}, duke@435: {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)}, duke@435: {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)}, duke@435: {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)} duke@435: duke@435: }; duke@435: duke@435: // These are the old methods prior to the JSR 166 changes in 1.6.0 duke@435: static JNINativeMethod methods_15[] = { duke@435: duke@435: {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)}, duke@435: {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)}, duke@435: {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)}, duke@435: {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)}, duke@435: duke@435: duke@435: DECLARE_GETSETOOP(Boolean, Z), duke@435: DECLARE_GETSETOOP(Byte, B), duke@435: DECLARE_GETSETOOP(Short, S), duke@435: DECLARE_GETSETOOP(Char, C), duke@435: DECLARE_GETSETOOP(Int, I), duke@435: DECLARE_GETSETOOP(Long, J), duke@435: DECLARE_GETSETOOP(Float, F), duke@435: DECLARE_GETSETOOP(Double, D), duke@435: duke@435: DECLARE_GETSETNATIVE(Byte, B), duke@435: DECLARE_GETSETNATIVE(Short, S), duke@435: DECLARE_GETSETNATIVE(Char, C), duke@435: DECLARE_GETSETNATIVE(Int, I), duke@435: DECLARE_GETSETNATIVE(Long, J), duke@435: DECLARE_GETSETNATIVE(Float, F), duke@435: DECLARE_GETSETNATIVE(Double, D), duke@435: duke@435: {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)}, duke@435: {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)}, duke@435: duke@435: {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)}, duke@435: {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)}, duke@435: // {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)}, duke@435: // {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)}, duke@435: {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)}, duke@435: duke@435: {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)}, duke@435: {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)}, duke@435: {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, duke@435: {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)}, duke@435: {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)}, duke@435: {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)}, duke@435: {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)}, duke@435: {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)}, duke@435: duke@435: {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)}, duke@435: {CC"defineClass", CC"("DC1_Args")"CLS, FN_PTR(Unsafe_DefineClass1)}, duke@435: {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)}, duke@435: {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)}, duke@435: {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)}, duke@435: {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}, duke@435: {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)}, duke@435: {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, duke@435: {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)}, duke@435: {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)}, duke@435: {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)} duke@435: duke@435: }; duke@435: duke@435: // These are the correct methods, moving forward: duke@435: static JNINativeMethod methods[] = { duke@435: duke@435: {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)}, duke@435: {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)}, duke@435: {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)}, duke@435: {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)}, duke@435: duke@435: duke@435: DECLARE_GETSETOOP(Boolean, Z), duke@435: DECLARE_GETSETOOP(Byte, B), duke@435: DECLARE_GETSETOOP(Short, S), duke@435: DECLARE_GETSETOOP(Char, C), duke@435: DECLARE_GETSETOOP(Int, I), duke@435: DECLARE_GETSETOOP(Long, J), duke@435: DECLARE_GETSETOOP(Float, F), duke@435: DECLARE_GETSETOOP(Double, D), duke@435: duke@435: DECLARE_GETSETNATIVE(Byte, B), duke@435: DECLARE_GETSETNATIVE(Short, S), duke@435: DECLARE_GETSETNATIVE(Char, C), duke@435: DECLARE_GETSETNATIVE(Int, I), duke@435: DECLARE_GETSETNATIVE(Long, J), duke@435: DECLARE_GETSETNATIVE(Float, F), duke@435: DECLARE_GETSETNATIVE(Double, D), duke@435: duke@435: {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)}, duke@435: {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)}, duke@435: duke@435: {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)}, duke@435: {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)}, duke@435: // {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)}, duke@435: // {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)}, duke@435: {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)}, duke@435: duke@435: {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)}, duke@435: {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)}, duke@435: {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, duke@435: {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)}, duke@435: {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)}, duke@435: {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)}, duke@435: {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)}, duke@435: {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)}, duke@435: duke@435: {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)}, duke@435: {CC"defineClass", CC"("DC1_Args")"CLS, FN_PTR(Unsafe_DefineClass1)}, duke@435: {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)}, duke@435: {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)}, duke@435: {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)}, duke@435: {CC"tryMonitorEnter", CC"("OBJ")Z", FN_PTR(Unsafe_TryMonitorEnter)}, duke@435: {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}, duke@435: {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)}, duke@435: {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, duke@435: {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)}, duke@435: {CC"putOrderedObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetOrderedObject)}, duke@435: {CC"putOrderedInt", CC"("OBJ"JI)V", FN_PTR(Unsafe_SetOrderedInt)}, duke@435: {CC"putOrderedLong", CC"("OBJ"JJ)V", FN_PTR(Unsafe_SetOrderedLong)}, kvn@4361: {CC"loadFence", CC"()V", FN_PTR(Unsafe_LoadFence)}, kvn@4361: {CC"storeFence", CC"()V", FN_PTR(Unsafe_StoreFence)}, kvn@4361: {CC"fullFence", CC"()V", FN_PTR(Unsafe_FullFence)}, duke@435: {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)}, duke@435: {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)} duke@435: duke@435: // {CC"getLoadAverage", CC"([DI)I", FN_PTR(Unsafe_Loadavg)}, duke@435: duke@435: // {CC"prefetchRead", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)}, duke@435: // {CC"prefetchWrite", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)} duke@435: // {CC"prefetchReadStatic", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)}, duke@435: // {CC"prefetchWriteStatic",CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)} duke@435: duke@435: }; duke@435: duke@435: JNINativeMethod loadavg_method[] = { duke@435: {CC"getLoadAverage", CC"([DI)I", FN_PTR(Unsafe_Loadavg)} duke@435: }; duke@435: duke@435: JNINativeMethod prefetch_methods[] = { duke@435: {CC"prefetchRead", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)}, duke@435: {CC"prefetchWrite", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)}, duke@435: {CC"prefetchReadStatic", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)}, duke@435: {CC"prefetchWriteStatic",CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)} duke@435: }; duke@435: duke@435: JNINativeMethod memcopy_methods[] = { duke@435: {CC"copyMemory", CC"("OBJ"J"OBJ"JJ)V", FN_PTR(Unsafe_CopyMemory2)}, duke@435: {CC"setMemory", CC"("OBJ"JJB)V", FN_PTR(Unsafe_SetMemory2)} duke@435: }; duke@435: duke@435: JNINativeMethod memcopy_methods_15[] = { duke@435: {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)}, duke@435: {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)} duke@435: }; duke@435: jrose@866: JNINativeMethod anonk_methods[] = { jrose@866: {CC"defineAnonymousClass", CC"("DAC_Args")"CLS, FN_PTR(Unsafe_DefineAnonymousClass)}, jrose@866: }; duke@435: twisti@3969: JNINativeMethod lform_methods[] = { twisti@3969: {CC"shouldBeInitialized",CC"("CLS")Z", FN_PTR(Unsafe_ShouldBeInitialized)}, twisti@3969: }; twisti@3969: duke@435: #undef CC duke@435: #undef FN_PTR duke@435: duke@435: #undef ADR duke@435: #undef LANG duke@435: #undef OBJ duke@435: #undef CLS duke@435: #undef CTR duke@435: #undef FLD duke@435: #undef MTH duke@435: #undef THR duke@435: #undef DC0_Args duke@435: #undef DC1_Args duke@435: duke@435: #undef DECLARE_GETSETOOP duke@435: #undef DECLARE_GETSETNATIVE duke@435: duke@435: duke@435: // This one function is exported, used by NativeLookup. duke@435: // The Unsafe_xxx functions above are called only from the interpreter. duke@435: // The optimizer looks at names and signatures to recognize duke@435: // individual functions. duke@435: duke@435: JVM_ENTRY(void, JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls)) duke@435: UnsafeWrapper("JVM_RegisterUnsafeMethods"); duke@435: { duke@435: ThreadToNativeFromVM ttnfv(thread); duke@435: { duke@435: env->RegisterNatives(unsafecls, loadavg_method, sizeof(loadavg_method)/sizeof(JNINativeMethod)); duke@435: if (env->ExceptionOccurred()) { duke@435: if (PrintMiscellaneous && (Verbose || WizardMode)) { duke@435: tty->print_cr("Warning: SDK 1.6 Unsafe.loadavg not found."); duke@435: } duke@435: env->ExceptionClear(); duke@435: } duke@435: } duke@435: { duke@435: env->RegisterNatives(unsafecls, prefetch_methods, sizeof(prefetch_methods)/sizeof(JNINativeMethod)); duke@435: if (env->ExceptionOccurred()) { duke@435: if (PrintMiscellaneous && (Verbose || WizardMode)) { duke@435: tty->print_cr("Warning: SDK 1.6 Unsafe.prefetchRead/Write not found."); duke@435: } duke@435: env->ExceptionClear(); duke@435: } duke@435: } duke@435: { duke@435: env->RegisterNatives(unsafecls, memcopy_methods, sizeof(memcopy_methods)/sizeof(JNINativeMethod)); duke@435: if (env->ExceptionOccurred()) { duke@435: if (PrintMiscellaneous && (Verbose || WizardMode)) { duke@435: tty->print_cr("Warning: SDK 1.7 Unsafe.copyMemory not found."); duke@435: } duke@435: env->ExceptionClear(); duke@435: env->RegisterNatives(unsafecls, memcopy_methods_15, sizeof(memcopy_methods_15)/sizeof(JNINativeMethod)); duke@435: if (env->ExceptionOccurred()) { duke@435: if (PrintMiscellaneous && (Verbose || WizardMode)) { duke@435: tty->print_cr("Warning: SDK 1.5 Unsafe.copyMemory not found."); duke@435: } duke@435: env->ExceptionClear(); duke@435: } duke@435: } duke@435: } twisti@2698: if (EnableInvokeDynamic) { jrose@866: env->RegisterNatives(unsafecls, anonk_methods, sizeof(anonk_methods)/sizeof(JNINativeMethod)); jrose@866: if (env->ExceptionOccurred()) { jrose@866: if (PrintMiscellaneous && (Verbose || WizardMode)) { jrose@866: tty->print_cr("Warning: SDK 1.7 Unsafe.defineClass (anonymous version) not found."); jrose@866: } jrose@866: env->ExceptionClear(); jrose@866: } jrose@866: } twisti@3969: if (EnableInvokeDynamic) { twisti@3969: env->RegisterNatives(unsafecls, lform_methods, sizeof(lform_methods)/sizeof(JNINativeMethod)); twisti@3969: if (env->ExceptionOccurred()) { twisti@3969: if (PrintMiscellaneous && (Verbose || WizardMode)) { twisti@3969: tty->print_cr("Warning: SDK 1.7 LambdaForm support in Unsafe not found."); twisti@3969: } twisti@3969: env->ExceptionClear(); twisti@3969: } twisti@3969: } duke@435: int status = env->RegisterNatives(unsafecls, methods, sizeof(methods)/sizeof(JNINativeMethod)); duke@435: if (env->ExceptionOccurred()) { duke@435: if (PrintMiscellaneous && (Verbose || WizardMode)) { duke@435: tty->print_cr("Warning: SDK 1.6 version of Unsafe not found."); duke@435: } duke@435: env->ExceptionClear(); duke@435: // %%% For now, be backward compatible with an older class: duke@435: status = env->RegisterNatives(unsafecls, methods_15, sizeof(methods_15)/sizeof(JNINativeMethod)); duke@435: } duke@435: if (env->ExceptionOccurred()) { duke@435: if (PrintMiscellaneous && (Verbose || WizardMode)) { duke@435: tty->print_cr("Warning: SDK 1.5 version of Unsafe not found."); duke@435: } duke@435: env->ExceptionClear(); duke@435: // %%% For now, be backward compatible with an older class: duke@435: status = env->RegisterNatives(unsafecls, methods_141, sizeof(methods_141)/sizeof(JNINativeMethod)); duke@435: } duke@435: if (env->ExceptionOccurred()) { duke@435: if (PrintMiscellaneous && (Verbose || WizardMode)) { duke@435: tty->print_cr("Warning: SDK 1.4.1 version of Unsafe not found."); duke@435: } duke@435: env->ExceptionClear(); duke@435: // %%% For now, be backward compatible with an older class: duke@435: status = env->RegisterNatives(unsafecls, methods_140, sizeof(methods_140)/sizeof(JNINativeMethod)); duke@435: } duke@435: guarantee(status == 0, "register unsafe natives"); duke@435: } duke@435: JVM_END