1 /*
  2  * Copyright (c) 2004, 2020, 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 30*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 // Warning: do not use rip relative addressing after the first counter load
 53 // since that may scratch r10!
 54 
 55 address JNI_FastGetField::generate_fast_get_int_field0(BasicType type) {
 56   const char *name = NULL;
 57   switch (type) {
 58     case T_BOOLEAN: name = "jni_fast_GetBooleanField"; break;
 59     case T_BYTE:    name = "jni_fast_GetByteField";    break;
 60     case T_CHAR:    name = "jni_fast_GetCharField";    break;
 61     case T_SHORT:   name = "jni_fast_GetShortField";   break;
 62     case T_INT:     name = "jni_fast_GetIntField";     break;
 63     case T_LONG:    name = "jni_fast_GetLongField";    break;
 64     default:        ShouldNotReachHere();
 65   }
 66   ResourceMark rm;
 67   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
 68   CodeBuffer cbuf(blob);
 69   MacroAssembler* masm = new MacroAssembler(&cbuf);
 70   address fast_entry = __ pc();
 71 
 72   Label slow;
 73 
 74   ExternalAddress counter(SafepointSynchronize::safepoint_counter_addr());
 75   __ mov32 (rcounter, counter);
 76   __ mov   (robj, c_rarg1);
 77   __ testb (rcounter, 1);
 78   __ jcc (Assembler::notZero, slow);
 79 
 80   if (JvmtiExport::can_post_field_access()) {
 81     // Check to see if a field access watch has been set before we
 82     // take the fast path.
 83     assert_different_registers(rscratch1, robj, rcounter); // cmp32 clobbers rscratch1!
 84     __ cmp32(ExternalAddress((address) JvmtiExport::get_field_access_count_addr()), 0);
 85     __ jcc(Assembler::notZero, slow);
 86   }
 87 
 88   __ mov   (roffset, c_rarg2);
 89   __ shrptr(roffset, jfieldIDWorkaround::offset_shift);                         // offset
 90 
 91   // Both robj and rtmp are clobbered by try_resolve_jobject_in_native.
 92   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
 93   bs->try_resolve_jobject_in_native(masm, /* jni_env */ c_rarg0, robj, rtmp, slow);
 94   DEBUG_ONLY(__ movl(rtmp, 0xDEADC0DE);)
 95 
 96   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
 97   speculative_load_pclist[count] = __ pc();
 98   switch (type) {
 99     case T_BOOLEAN: __ movzbl (rax, Address(robj, roffset, Address::times_1)); break;
100     case T_BYTE:    __ movsbl (rax, Address(robj, roffset, Address::times_1)); break;
101     case T_CHAR:    __ movzwl (rax, Address(robj, roffset, Address::times_1)); break;
102     case T_SHORT:   __ movswl (rax, Address(robj, roffset, Address::times_1)); break;
103     case T_INT:     __ movl   (rax, Address(robj, roffset, Address::times_1)); break;
104     case T_LONG:    __ movq   (rax, Address(robj, roffset, Address::times_1)); break;
105     default:        ShouldNotReachHere();
106   }
107 
108   __ cmp32 (rcounter, counter);
109   __ jcc (Assembler::notEqual, slow);
110 
111   __ ret (0);
112 
113   slowcase_entry_pclist[count++] = __ pc();
114   __ bind (slow);
115   address slow_case_addr = NULL;
116   switch (type) {
117     case T_BOOLEAN: slow_case_addr = jni_GetBooleanField_addr(); break;
118     case T_BYTE:    slow_case_addr = jni_GetByteField_addr();    break;
119     case T_CHAR:    slow_case_addr = jni_GetCharField_addr();    break;
120     case T_SHORT:   slow_case_addr = jni_GetShortField_addr();   break;
121     case T_INT:     slow_case_addr = jni_GetIntField_addr();     break;
122     case T_LONG:    slow_case_addr = jni_GetLongField_addr();    break;
123     default:                                                     break;
124   }
125   // tail call
126   __ jump (ExternalAddress(slow_case_addr));
127 
128   __ flush ();
129 
130   return fast_entry;
131 }
132 
133 address JNI_FastGetField::generate_fast_get_boolean_field() {
134   return generate_fast_get_int_field0(T_BOOLEAN);
135 }
136 
137 address JNI_FastGetField::generate_fast_get_byte_field() {
138   return generate_fast_get_int_field0(T_BYTE);
139 }
140 
141 address JNI_FastGetField::generate_fast_get_char_field() {
142   return generate_fast_get_int_field0(T_CHAR);
143 }
144 
145 address JNI_FastGetField::generate_fast_get_short_field() {
146   return generate_fast_get_int_field0(T_SHORT);
147 }
148 
149 address JNI_FastGetField::generate_fast_get_int_field() {
150   return generate_fast_get_int_field0(T_INT);
151 }
152 
153 address JNI_FastGetField::generate_fast_get_long_field() {
154   return generate_fast_get_int_field0(T_LONG);
155 }
156 
157 address JNI_FastGetField::generate_fast_get_float_field0(BasicType type) {
158   const char *name = NULL;
159   switch (type) {
160     case T_FLOAT:     name = "jni_fast_GetFloatField";     break;
161     case T_DOUBLE:    name = "jni_fast_GetDoubleField";    break;
162     default:          ShouldNotReachHere();
163   }
164   ResourceMark rm;
165   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
166   CodeBuffer cbuf(blob);
167   MacroAssembler* masm = new MacroAssembler(&cbuf);
168   address fast_entry = __ pc();
169 
170   Label slow;
171 
172   ExternalAddress counter(SafepointSynchronize::safepoint_counter_addr());
173   __ mov32 (rcounter, counter);
174   __ mov   (robj, c_rarg1);
175   __ testb (rcounter, 1);
176   __ jcc (Assembler::notZero, slow);
177 
178   if (JvmtiExport::can_post_field_access()) {
179     // Check to see if a field access watch has been set before we
180     // take the fast path.
181     __ cmp32(ExternalAddress((address) JvmtiExport::get_field_access_count_addr()), 0);
182     __ jcc(Assembler::notZero, slow);
183   }
184 
185   // Both robj and rtmp are clobbered by try_resolve_jobject_in_native.
186   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
187   bs->try_resolve_jobject_in_native(masm, /* jni_env */ c_rarg0, robj, rtmp, slow);
188   DEBUG_ONLY(__ movl(rtmp, 0xDEADC0DE);)
189 
190   __ mov   (roffset, c_rarg2);
191   __ shrptr(roffset, jfieldIDWorkaround::offset_shift);                         // offset
192 
193   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
194   speculative_load_pclist[count] = __ pc();
195   switch (type) {
196     case T_FLOAT:  __ movflt (xmm0, Address(robj, roffset, Address::times_1)); break;
197     case T_DOUBLE: __ movdbl (xmm0, Address(robj, roffset, Address::times_1)); break;
198     default:        ShouldNotReachHere();
199   }
200   __ cmp32 (rcounter, counter);
201   __ jcc (Assembler::notEqual, slow);
202 
203   __ ret (0);
204 
205   slowcase_entry_pclist[count++] = __ pc();
206   __ bind (slow);
207   address slow_case_addr = NULL;
208   switch (type) {
209     case T_FLOAT:     slow_case_addr = jni_GetFloatField_addr();  break;
210     case T_DOUBLE:    slow_case_addr = jni_GetDoubleField_addr(); break;
211     default:                                                      break;
212   }
213   // tail call
214   __ jump (ExternalAddress(slow_case_addr));
215 
216   __ flush ();
217 
218   return fast_entry;
219 }
220 
221 address JNI_FastGetField::generate_fast_get_float_field() {
222   return generate_fast_get_float_field0(T_FLOAT);
223 }
224 
225 address JNI_FastGetField::generate_fast_get_double_field() {
226   return generate_fast_get_float_field0(T_DOUBLE);
227 }