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/javaThread.inline.hpp"
 35 #include "runtime/safepoint.hpp"
 36 #include "runtime/threadWXSetters.inline.hpp"
 37 
 38 #define __ masm->
 39 
 40 #define BUFFER_SIZE 30*wordSize
 41 
 42 // Instead of issuing a LoadLoad barrier we create an address
 43 // dependency between loads; this might be more efficient.
 44 
 45 // Common register usage:
 46 // r0/v0:      result
 47 // c_rarg0:    jni env
 48 // c_rarg1:    obj
 49 // c_rarg2:    jfield id
 50 
 51 static const Register robj          = r3;
 52 static const Register rcounter      = r4;
 53 static const Register roffset       = r5;
 54 static const Register rcounter_addr = r6;
 55 static const Register result        = r7;
 56 
 57 // On macos/aarch64 we need to ensure WXExec mode when running generated
 58 // FastGetXXXField, as these functions can be called from WXWrite context
 59 // (8262896).  So each FastGetXXXField is wrapped into a C++ statically
 60 // compiled template function that optionally switches to WXExec if necessary.
 61 
 62 #ifdef __APPLE__
 63 
 64 static address generated_fast_get_field[T_LONG + 1 - T_BOOLEAN];
 65 
 66 template<int BType> struct BasicTypeToJni {};
 67 template<> struct BasicTypeToJni<T_BOOLEAN> { static const jboolean jni_type; };
 68 template<> struct BasicTypeToJni<T_BYTE>    { static const jbyte    jni_type; };
 69 template<> struct BasicTypeToJni<T_CHAR>    { static const jchar    jni_type; };
 70 template<> struct BasicTypeToJni<T_SHORT>   { static const jshort   jni_type; };
 71 template<> struct BasicTypeToJni<T_INT>     { static const jint     jni_type; };
 72 template<> struct BasicTypeToJni<T_LONG>    { static const jlong    jni_type; };
 73 template<> struct BasicTypeToJni<T_FLOAT>   { static const jfloat   jni_type; };
 74 template<> struct BasicTypeToJni<T_DOUBLE>  { static const jdouble  jni_type; };
 75 
 76 template<int BType, typename JniType = decltype(BasicTypeToJni<BType>::jni_type)>
 77 JniType static_fast_get_field_wrapper(JNIEnv *env, jobject obj, jfieldID fieldID) {
 78   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
 79   ThreadWXEnable wx(WXExec, thread);
 80   address get_field_addr = generated_fast_get_field[BType - T_BOOLEAN];
 81   return ((JniType(*)(JNIEnv *env, jobject obj, jfieldID fieldID))get_field_addr)(env, obj, fieldID);
 82 }
 83 
 84 template<int BType>
 85 address JNI_FastGetField::generate_fast_get_int_field1() {
 86   generated_fast_get_field[BType - T_BOOLEAN] = generate_fast_get_int_field0((BasicType)BType);
 87   return (address)static_fast_get_field_wrapper<BType>;
 88 }
 89 
 90 #else // __APPLE__
 91 
 92 template<int BType>
 93 address JNI_FastGetField::generate_fast_get_int_field1() {
 94   return generate_fast_get_int_field0((BasicType)BType);
 95 }
 96 
 97 #endif // __APPLE__
 98 
 99 address JNI_FastGetField::generate_fast_get_int_field0(BasicType type) {
100   const char *name;
101   switch (type) {
102     case T_BOOLEAN: name = "jni_fast_GetBooleanField"; break;
103     case T_BYTE:    name = "jni_fast_GetByteField";    break;
104     case T_CHAR:    name = "jni_fast_GetCharField";    break;
105     case T_SHORT:   name = "jni_fast_GetShortField";   break;
106     case T_INT:     name = "jni_fast_GetIntField";     break;
107     case T_LONG:    name = "jni_fast_GetLongField";    break;
108     case T_FLOAT:   name = "jni_fast_GetFloatField";   break;
109     case T_DOUBLE:  name = "jni_fast_GetDoubleField";  break;
110     default:        ShouldNotReachHere();
111       name = nullptr;  // unreachable
112   }
113   ResourceMark rm;
114   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
115   CodeBuffer cbuf(blob);
116   MacroAssembler* masm = new MacroAssembler(&cbuf);
117   address fast_entry = __ pc();
118 
119   Label slow;
120 
121   uint64_t offset;
122   __ adrp(rcounter_addr,
123           SafepointSynchronize::safepoint_counter_addr(), offset);
124   Address safepoint_counter_addr(rcounter_addr, offset);
125   __ ldrw(rcounter, safepoint_counter_addr);
126   __ tbnz(rcounter, 0, slow);
127 
128   // It doesn't need to issue a full barrier here even if the field
129   // is volatile, since it has already used "ldar" for it.
130   if (JvmtiExport::can_post_field_access()) {
131     // Using barrier to order wrt. JVMTI check and load of result.
132     __ membar(Assembler::LoadLoad);
133 
134     // Check to see if a field access watch has been set before we
135     // take the fast path.
136     uint64_t offset2;
137     __ adrp(result,
138             ExternalAddress((address) JvmtiExport::get_field_access_count_addr()),
139             offset2);
140     __ ldrw(result, Address(result, offset2));
141     __ cbnzw(result, slow);
142 
143     __ mov(robj, c_rarg1);
144   } else {
145     // Using address dependency to order wrt. load of result.
146     __ eor(robj, c_rarg1, rcounter);
147     __ eor(robj, robj, rcounter);         // obj, since
148                                           // robj ^ rcounter ^ rcounter == robj
149                                           // robj is address dependent on rcounter.
150   }
151 
152   // Both robj and rscratch1 are clobbered by try_resolve_jobject_in_native.
153   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
154   bs->try_resolve_jobject_in_native(masm, c_rarg0, robj, rscratch1, slow);
155 
156   __ lsr(roffset, c_rarg2, 2);                // offset
157   __ add(result, robj, roffset);
158 
159   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
160   speculative_load_pclist[count] = __ pc();   // Used by the segfault handler
161   // Using acquire: Order JVMTI check and load of result wrt. succeeding check
162   // (LoadStore for volatile field).
163   switch (type) {
164     case T_BOOLEAN: __ ldarb(result, result); break;
165     case T_BYTE:    __ ldarb(result, result); __ sxtb(result, result); break;
166     case T_CHAR:    __ ldarh(result, result); break;
167     case T_SHORT:   __ ldarh(result, result); __ sxth(result, result); break;
168     case T_FLOAT:   __ ldarw(result, result); break;
169     case T_INT:     __ ldarw(result, result); __ sxtw(result, result); break;
170     case T_DOUBLE:
171     case T_LONG:    __ ldar (result, result); break;
172     default:        ShouldNotReachHere();
173   }
174 
175   __ ldrw(rscratch1, safepoint_counter_addr);
176   __ cmpw(rcounter, rscratch1);
177   __ br (Assembler::NE, slow);
178 
179   switch (type) {
180     case T_FLOAT:   __ fmovs(v0, result); break;
181     case T_DOUBLE:  __ fmovd(v0, result); break;
182     default:        __ mov(r0, result);   break;
183   }
184   __ ret(lr);
185 
186   slowcase_entry_pclist[count++] = __ pc();
187   __ bind(slow);
188   address slow_case_addr;
189   switch (type) {
190     case T_BOOLEAN: slow_case_addr = jni_GetBooleanField_addr(); break;
191     case T_BYTE:    slow_case_addr = jni_GetByteField_addr();    break;
192     case T_CHAR:    slow_case_addr = jni_GetCharField_addr();    break;
193     case T_SHORT:   slow_case_addr = jni_GetShortField_addr();   break;
194     case T_INT:     slow_case_addr = jni_GetIntField_addr();     break;
195     case T_LONG:    slow_case_addr = jni_GetLongField_addr();    break;
196     case T_FLOAT:   slow_case_addr = jni_GetFloatField_addr();   break;
197     case T_DOUBLE:  slow_case_addr = jni_GetDoubleField_addr();  break;
198     default:        ShouldNotReachHere();
199       slow_case_addr = nullptr;  // unreachable
200   }
201 
202   {
203     __ enter();
204     __ lea(rscratch1, RuntimeAddress(slow_case_addr));
205     __ blr(rscratch1);
206     __ leave();
207     __ ret(lr);
208   }
209   __ flush ();
210 
211   return fast_entry;
212 }
213 
214 address JNI_FastGetField::generate_fast_get_boolean_field() {
215   return generate_fast_get_int_field1<T_BOOLEAN>();
216 }
217 
218 address JNI_FastGetField::generate_fast_get_byte_field() {
219   return generate_fast_get_int_field1<T_BYTE>();
220 }
221 
222 address JNI_FastGetField::generate_fast_get_char_field() {
223   return generate_fast_get_int_field1<T_CHAR>();
224 }
225 
226 address JNI_FastGetField::generate_fast_get_short_field() {
227   return generate_fast_get_int_field1<T_SHORT>();
228 }
229 
230 address JNI_FastGetField::generate_fast_get_int_field() {
231   return generate_fast_get_int_field1<T_INT>();
232 }
233 
234 address JNI_FastGetField::generate_fast_get_long_field() {
235   return generate_fast_get_int_field1<T_LONG>();
236 }
237 
238 address JNI_FastGetField::generate_fast_get_float_field() {
239   return generate_fast_get_int_field1<T_FLOAT>();
240 }
241 
242 address JNI_FastGetField::generate_fast_get_double_field() {
243   return generate_fast_get_int_field1<T_DOUBLE>();
244 }