1 /*
  2  * Copyright (c) 2004, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "asm/macroAssembler.hpp"
 28 #include "gc/shared/barrierSet.hpp"
 29 #include "gc/shared/barrierSetAssembler.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "prims/jniFastGetField.hpp"
 32 #include "prims/jvm_misc.hpp"
 33 #include "prims/jvmtiExport.hpp"
 34 #include "runtime/jfieldIDWorkaround.hpp"
 35 #include "runtime/javaThread.inline.hpp"
 36 #include "runtime/safepoint.hpp"
 37 #include "runtime/threadWXSetters.inline.hpp"
 38 
 39 #define __ masm->
 40 
 41 #define BUFFER_SIZE 30*wordSize
 42 
 43 // Instead of issuing a LoadLoad barrier we create an address
 44 // dependency between loads; this might be more efficient.
 45 
 46 // Common register usage:
 47 // r0/v0:      result
 48 // c_rarg0:    jni env
 49 // c_rarg1:    obj
 50 // c_rarg2:    jfield id
 51 
 52 static const Register robj          = r3;
 53 static const Register rcounter      = r4;
 54 static const Register roffset       = r5;
 55 static const Register rcounter_addr = r6;
 56 static const Register result        = r7;
 57 
 58 // On macos/aarch64 we need to ensure WXExec mode when running generated
 59 // FastGetXXXField, as these functions can be called from WXWrite context
 60 // (8262896).  So each FastGetXXXField is wrapped into a C++ statically
 61 // compiled template function that optionally switches to WXExec if necessary.
 62 
 63 #ifdef __APPLE__
 64 
 65 static address generated_fast_get_field[T_LONG + 1 - T_BOOLEAN];
 66 
 67 template<int BType> struct BasicTypeToJni {};
 68 template<> struct BasicTypeToJni<T_BOOLEAN> { static const jboolean jni_type; };
 69 template<> struct BasicTypeToJni<T_BYTE>    { static const jbyte    jni_type; };
 70 template<> struct BasicTypeToJni<T_CHAR>    { static const jchar    jni_type; };
 71 template<> struct BasicTypeToJni<T_SHORT>   { static const jshort   jni_type; };
 72 template<> struct BasicTypeToJni<T_INT>     { static const jint     jni_type; };
 73 template<> struct BasicTypeToJni<T_LONG>    { static const jlong    jni_type; };
 74 template<> struct BasicTypeToJni<T_FLOAT>   { static const jfloat   jni_type; };
 75 template<> struct BasicTypeToJni<T_DOUBLE>  { static const jdouble  jni_type; };
 76 
 77 template<int BType, typename JniType = decltype(BasicTypeToJni<BType>::jni_type)>
 78 JniType static_fast_get_field_wrapper(JNIEnv *env, jobject obj, jfieldID fieldID) {
 79   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
 80   ThreadWXEnable wx(WXExec, thread);
 81   address get_field_addr = generated_fast_get_field[BType - T_BOOLEAN];
 82   return ((JniType(*)(JNIEnv *env, jobject obj, jfieldID fieldID))get_field_addr)(env, obj, fieldID);
 83 }
 84 
 85 template<int BType>
 86 address JNI_FastGetField::generate_fast_get_int_field1() {
 87   generated_fast_get_field[BType - T_BOOLEAN] = generate_fast_get_int_field0((BasicType)BType);
 88   return (address)static_fast_get_field_wrapper<BType>;
 89 }
 90 
 91 #else // __APPLE__
 92 
 93 template<int BType>
 94 address JNI_FastGetField::generate_fast_get_int_field1() {
 95   return generate_fast_get_int_field0((BasicType)BType);
 96 }
 97 
 98 #endif // __APPLE__
 99 
100 address JNI_FastGetField::generate_fast_get_int_field0(BasicType type) {
101   const char *name;
102   switch (type) {
103     case T_BOOLEAN: name = "jni_fast_GetBooleanField"; break;
104     case T_BYTE:    name = "jni_fast_GetByteField";    break;
105     case T_CHAR:    name = "jni_fast_GetCharField";    break;
106     case T_SHORT:   name = "jni_fast_GetShortField";   break;
107     case T_INT:     name = "jni_fast_GetIntField";     break;
108     case T_LONG:    name = "jni_fast_GetLongField";    break;
109     case T_FLOAT:   name = "jni_fast_GetFloatField";   break;
110     case T_DOUBLE:  name = "jni_fast_GetDoubleField";  break;
111     default:        ShouldNotReachHere();
112       name = nullptr;  // unreachable
113   }
114   ResourceMark rm;
115   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
116   CodeBuffer cbuf(blob);
117   MacroAssembler* masm = new MacroAssembler(&cbuf);
118   address fast_entry = __ pc();
119 
120   Label slow;
121 
122   uint64_t offset;
123   __ adrp(rcounter_addr,
124           SafepointSynchronize::safepoint_counter_addr(), offset);
125   Address safepoint_counter_addr(rcounter_addr, offset);
126   __ ldrw(rcounter, safepoint_counter_addr);
127   __ tbnz(rcounter, 0, slow);
128 
129   // It doesn't need to issue a full barrier here even if the field
130   // is volatile, since it has already used "ldar" for it.
131   if (JvmtiExport::can_post_field_access()) {
132     // Using barrier to order wrt. JVMTI check and load of result.
133     __ membar(Assembler::LoadLoad);
134 
135     // Check to see if a field access watch has been set before we
136     // take the fast path.
137     uint64_t offset2;
138     __ adrp(result,
139             ExternalAddress((address) JvmtiExport::get_field_access_count_addr()),
140             offset2);
141     __ ldrw(result, Address(result, offset2));
142     __ cbnzw(result, slow);
143 
144     __ mov(robj, c_rarg1);
145   } else {
146     // Using address dependency to order wrt. load of result.
147     __ eor(robj, c_rarg1, rcounter);
148     __ eor(robj, robj, rcounter);         // obj, since
149                                           // robj ^ rcounter ^ rcounter == robj
150                                           // robj is address dependent on rcounter.
151   }
152 
153   // Both robj and rscratch1 are clobbered by try_resolve_jobject_in_native.
154   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
155   bs->try_resolve_jobject_in_native(masm, c_rarg0, robj, rscratch1, slow);
156 
157   __ lsr(roffset, c_rarg2, jfieldIDWorkaround::offset_shift);       // offset
158   __ add(result, robj, roffset);
159 
160   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
161   speculative_load_pclist[count] = __ pc();   // Used by the segfault handler
162   // Using acquire: Order JVMTI check and load of result wrt. succeeding check
163   // (LoadStore for volatile field).
164   switch (type) {
165     case T_BOOLEAN: __ ldarb(result, result); break;
166     case T_BYTE:    __ ldarb(result, result); __ sxtb(result, result); break;
167     case T_CHAR:    __ ldarh(result, result); break;
168     case T_SHORT:   __ ldarh(result, result); __ sxth(result, result); break;
169     case T_FLOAT:   __ ldarw(result, result); break;
170     case T_INT:     __ ldarw(result, result); __ sxtw(result, result); break;
171     case T_DOUBLE:
172     case T_LONG:    __ ldar (result, result); break;
173     default:        ShouldNotReachHere();
174   }
175 
176   __ ldrw(rscratch1, safepoint_counter_addr);
177   __ cmpw(rcounter, rscratch1);
178   __ br (Assembler::NE, slow);
179 
180   switch (type) {
181     case T_FLOAT:   __ fmovs(v0, result); break;
182     case T_DOUBLE:  __ fmovd(v0, result); break;
183     default:        __ mov(r0, result);   break;
184   }
185   __ ret(lr);
186 
187   slowcase_entry_pclist[count++] = __ pc();
188   __ bind(slow);
189   address slow_case_addr;
190   switch (type) {
191     case T_BOOLEAN: slow_case_addr = jni_GetBooleanField_addr(); break;
192     case T_BYTE:    slow_case_addr = jni_GetByteField_addr();    break;
193     case T_CHAR:    slow_case_addr = jni_GetCharField_addr();    break;
194     case T_SHORT:   slow_case_addr = jni_GetShortField_addr();   break;
195     case T_INT:     slow_case_addr = jni_GetIntField_addr();     break;
196     case T_LONG:    slow_case_addr = jni_GetLongField_addr();    break;
197     case T_FLOAT:   slow_case_addr = jni_GetFloatField_addr();   break;
198     case T_DOUBLE:  slow_case_addr = jni_GetDoubleField_addr();  break;
199     default:        ShouldNotReachHere();
200       slow_case_addr = nullptr;  // unreachable
201   }
202 
203   {
204     __ enter();
205     __ lea(rscratch1, RuntimeAddress(slow_case_addr));
206     __ blr(rscratch1);
207     __ leave();
208     __ ret(lr);
209   }
210   __ flush ();
211 
212   return fast_entry;
213 }
214 
215 address JNI_FastGetField::generate_fast_get_boolean_field() {
216   return generate_fast_get_int_field1<T_BOOLEAN>();
217 }
218 
219 address JNI_FastGetField::generate_fast_get_byte_field() {
220   return generate_fast_get_int_field1<T_BYTE>();
221 }
222 
223 address JNI_FastGetField::generate_fast_get_char_field() {
224   return generate_fast_get_int_field1<T_CHAR>();
225 }
226 
227 address JNI_FastGetField::generate_fast_get_short_field() {
228   return generate_fast_get_int_field1<T_SHORT>();
229 }
230 
231 address JNI_FastGetField::generate_fast_get_int_field() {
232   return generate_fast_get_int_field1<T_INT>();
233 }
234 
235 address JNI_FastGetField::generate_fast_get_long_field() {
236   return generate_fast_get_int_field1<T_LONG>();
237 }
238 
239 address JNI_FastGetField::generate_fast_get_float_field() {
240   return generate_fast_get_int_field1<T_FLOAT>();
241 }
242 
243 address JNI_FastGetField::generate_fast_get_double_field() {
244   return generate_fast_get_int_field1<T_DOUBLE>();
245 }