1 /*
  2  * Copyright (c) 2004, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "asm/macroAssembler.hpp"
 27 #include "code/codeBlob.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 "runtime/jfieldIDWorkaround.hpp"
 34 #include "prims/jvmtiExport.hpp"
 35 #include "runtime/safepoint.hpp"
 36 
 37 #define __ masm->
 38 
 39 #define BUFFER_SIZE 40*wordSize
 40 
 41 // Common register usage:
 42 // rax/xmm0: result
 43 // c_rarg0:    jni env
 44 // c_rarg1:    obj
 45 // c_rarg2:    jfield id
 46 
 47 static const Register rtmp     = rax; // r8 == c_rarg2 on Windows
 48 static const Register robj     = r9;
 49 static const Register roffset  = r10;
 50 static const Register rcounter = r11;
 51 
 52 address JNI_FastGetField::generate_fast_get_int_field0(BasicType type) {
 53   const char *name = nullptr;
 54   switch (type) {
 55     case T_BOOLEAN: name = "jni_fast_GetBooleanField"; break;
 56     case T_BYTE:    name = "jni_fast_GetByteField";    break;
 57     case T_CHAR:    name = "jni_fast_GetCharField";    break;
 58     case T_SHORT:   name = "jni_fast_GetShortField";   break;
 59     case T_INT:     name = "jni_fast_GetIntField";     break;
 60     case T_LONG:    name = "jni_fast_GetLongField";    break;
 61     default:        ShouldNotReachHere();
 62   }
 63   ResourceMark rm;
 64   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
 65   CodeBuffer cbuf(blob);
 66   MacroAssembler* masm = new MacroAssembler(&cbuf);
 67   address fast_entry = __ pc();
 68 
 69   Label slow;
 70 
 71   ExternalAddress counter(SafepointSynchronize::safepoint_counter_addr());
 72   __ mov32 (rcounter, counter);
 73   __ mov   (robj, c_rarg1);
 74   __ testb (rcounter, 1);
 75   __ jcc (Assembler::notZero, slow);
 76 
 77   if (JvmtiExport::can_post_field_access()) {
 78     // Check to see if a field access watch has been set before we take the fast path.
 79     assert_different_registers(rscratch1, robj, rcounter);
 80     __ cmp32(ExternalAddress(JvmtiExport::get_field_access_count_addr()), 0, rscratch1);
 81     __ jcc(Assembler::notZero, slow);
 82   }
 83 
 84   __ mov   (roffset, c_rarg2);
 85   __ shrptr(roffset, jfieldIDWorkaround::offset_shift);                         // offset
 86 
 87   // Both robj and rtmp are clobbered by try_resolve_jobject_in_native.
 88   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
 89   bs->try_resolve_jobject_in_native(masm, /* jni_env */ c_rarg0, robj, rtmp, slow);
 90   DEBUG_ONLY(__ movl(rtmp, 0xDEADC0DE);)
 91 
 92   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
 93   speculative_load_pclist[count] = __ pc();
 94   switch (type) {
 95     case T_BOOLEAN: __ movzbl (rax, Address(robj, roffset, Address::times_1)); break;
 96     case T_BYTE:    __ movsbl (rax, Address(robj, roffset, Address::times_1)); break;
 97     case T_CHAR:    __ movzwl (rax, Address(robj, roffset, Address::times_1)); break;
 98     case T_SHORT:   __ movswl (rax, Address(robj, roffset, Address::times_1)); break;
 99     case T_INT:     __ movl   (rax, Address(robj, roffset, Address::times_1)); break;
100     case T_LONG:    __ movq   (rax, Address(robj, roffset, Address::times_1)); break;
101     default:        ShouldNotReachHere();
102   }
103 
104   __ cmp32 (rcounter, counter, rscratch1);
105   __ jcc (Assembler::notEqual, slow);
106 
107   __ ret (0);
108 
109   slowcase_entry_pclist[count++] = __ pc();
110   __ bind (slow);
111   address slow_case_addr = nullptr;
112   switch (type) {
113     case T_BOOLEAN: slow_case_addr = jni_GetBooleanField_addr(); break;
114     case T_BYTE:    slow_case_addr = jni_GetByteField_addr();    break;
115     case T_CHAR:    slow_case_addr = jni_GetCharField_addr();    break;
116     case T_SHORT:   slow_case_addr = jni_GetShortField_addr();   break;
117     case T_INT:     slow_case_addr = jni_GetIntField_addr();     break;
118     case T_LONG:    slow_case_addr = jni_GetLongField_addr();    break;
119     default:                                                     break;
120   }
121   // tail call
122   __ jump (RuntimeAddress(slow_case_addr), rscratch1);
123 
124   __ flush ();
125 
126   return fast_entry;
127 }
128 
129 address JNI_FastGetField::generate_fast_get_boolean_field() {
130   return generate_fast_get_int_field0(T_BOOLEAN);
131 }
132 
133 address JNI_FastGetField::generate_fast_get_byte_field() {
134   return generate_fast_get_int_field0(T_BYTE);
135 }
136 
137 address JNI_FastGetField::generate_fast_get_char_field() {
138   return generate_fast_get_int_field0(T_CHAR);
139 }
140 
141 address JNI_FastGetField::generate_fast_get_short_field() {
142   return generate_fast_get_int_field0(T_SHORT);
143 }
144 
145 address JNI_FastGetField::generate_fast_get_int_field() {
146   return generate_fast_get_int_field0(T_INT);
147 }
148 
149 address JNI_FastGetField::generate_fast_get_long_field() {
150   return generate_fast_get_int_field0(T_LONG);
151 }
152 
153 address JNI_FastGetField::generate_fast_get_float_field0(BasicType type) {
154   const char *name = nullptr;
155   switch (type) {
156     case T_FLOAT:     name = "jni_fast_GetFloatField";     break;
157     case T_DOUBLE:    name = "jni_fast_GetDoubleField";    break;
158     default:          ShouldNotReachHere();
159   }
160   ResourceMark rm;
161   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
162   CodeBuffer cbuf(blob);
163   MacroAssembler* masm = new MacroAssembler(&cbuf);
164   address fast_entry = __ pc();
165 
166   Label slow;
167 
168   ExternalAddress counter(SafepointSynchronize::safepoint_counter_addr());
169   __ mov32 (rcounter, counter);
170   __ mov   (robj, c_rarg1);
171   __ testb (rcounter, 1);
172   __ jcc (Assembler::notZero, slow);
173 
174   if (JvmtiExport::can_post_field_access()) {
175     // Check to see if a field access watch has been set before we
176     // take the fast path.
177     __ cmp32(ExternalAddress(JvmtiExport::get_field_access_count_addr()), 0, rscratch1);
178     __ jcc(Assembler::notZero, slow);
179   }
180 
181   // Both robj and rtmp are clobbered by try_resolve_jobject_in_native.
182   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
183   bs->try_resolve_jobject_in_native(masm, /* jni_env */ c_rarg0, robj, rtmp, slow);
184   DEBUG_ONLY(__ movl(rtmp, 0xDEADC0DE);)
185 
186   __ mov   (roffset, c_rarg2);
187   __ shrptr(roffset, jfieldIDWorkaround::offset_shift);                         // offset
188 
189   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
190   speculative_load_pclist[count] = __ pc();
191   switch (type) {
192     case T_FLOAT:  __ movflt (xmm0, Address(robj, roffset, Address::times_1)); break;
193     case T_DOUBLE: __ movdbl (xmm0, Address(robj, roffset, Address::times_1)); break;
194     default:        ShouldNotReachHere();
195   }
196   __ cmp32 (rcounter, counter, rscratch1);
197   __ jcc (Assembler::notEqual, slow);
198 
199   __ ret (0);
200 
201   slowcase_entry_pclist[count++] = __ pc();
202   __ bind (slow);
203   address slow_case_addr = nullptr;
204   switch (type) {
205     case T_FLOAT:     slow_case_addr = jni_GetFloatField_addr();  break;
206     case T_DOUBLE:    slow_case_addr = jni_GetDoubleField_addr(); break;
207     default:                                                      break;
208   }
209   // tail call
210   __ jump (RuntimeAddress(slow_case_addr), rscratch1);
211 
212   __ flush ();
213 
214   return fast_entry;
215 }
216 
217 address JNI_FastGetField::generate_fast_get_float_field() {
218   return generate_fast_get_float_field0(T_FLOAT);
219 }
220 
221 address JNI_FastGetField::generate_fast_get_double_field() {
222   return generate_fast_get_float_field0(T_DOUBLE);
223 }