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