1 /*
  2  * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2012, 2026 SAP SE. 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 "asm/macroAssembler.inline.hpp"
 27 #include "gc/shared/barrierSet.hpp"
 28 #include "gc/shared/barrierSetAssembler.hpp"
 29 #include "memory/resourceArea.hpp"
 30 #include "prims/jniFastGetField.hpp"
 31 #include "prims/jvm_misc.hpp"
 32 #include "prims/jvmtiExport.hpp"
 33 #include "runtime/jfieldIDWorkaround.hpp"
 34 #include "runtime/safepoint.hpp"
 35 
 36 #define __ masm->
 37 
 38 #define BUFFER_SIZE 48*BytesPerInstWord
 39 
 40 
 41 // Common register usage:
 42 // R3/F0:      result
 43 // R3_ARG1:    jni env
 44 // R4_ARG2:    obj
 45 // R5_ARG3:    jfield id
 46 
 47 address JNI_FastGetField::generate_fast_get_int_field0(BasicType type) {
 48   const char *name;
 49   switch (type) {
 50     case T_BOOLEAN: name = "jni_fast_GetBooleanField"; break;
 51     case T_BYTE:    name = "jni_fast_GetByteField";    break;
 52     case T_CHAR:    name = "jni_fast_GetCharField";    break;
 53     case T_SHORT:   name = "jni_fast_GetShortField";   break;
 54     case T_INT:     name = "jni_fast_GetIntField";     break;
 55     case T_LONG:    name = "jni_fast_GetLongField";    break;
 56     case T_FLOAT:   name = "jni_fast_GetFloatField";   break;
 57     case T_DOUBLE:  name = "jni_fast_GetDoubleField";  break;
 58     default:        ShouldNotReachHere();
 59       name = nullptr;  // unreachable
 60   }
 61   ResourceMark rm;
 62   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
 63   CodeBuffer cbuf(blob);
 64   MacroAssembler* masm = new MacroAssembler(&cbuf);
 65   address fast_entry = __ function_entry();
 66 
 67   Label slow;
 68 
 69   const Register Rcounter_addr = R6_ARG4,
 70                  Rcounter      = R7_ARG5,
 71                  Robj          = R8_ARG6,
 72                  Rtmp          = R9_ARG7;
 73   const int counter_offs = __ load_const_optimized(Rcounter_addr,
 74                                                    SafepointSynchronize::safepoint_counter_addr(),
 75                                                    R0, true);
 76 
 77   __ ld(Rcounter, counter_offs, Rcounter_addr);
 78   __ andi_(R0, Rcounter, 1);
 79   __ bne(CR0, slow);
 80 
 81   if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
 82     // Field may be volatile.
 83     __ fence();
 84   } else {
 85     // Using acquire to order wrt. JVMTI check and load of result.
 86     __ isync(); // order wrt. to following load(s)
 87   }
 88 
 89   if (JvmtiExport::can_post_field_access()) {
 90     // Check to see if a field access watch has been set before we
 91     // take the fast path.
 92     int fac_offs = __ load_const_optimized(Rtmp, JvmtiExport::get_field_access_count_addr(),
 93                                            R0, true);
 94     __ lwa(Rtmp, fac_offs, Rtmp);
 95     __ cmpwi(CR0, Rtmp, 0);
 96     __ bne(CR0, slow);
 97   }
 98 
 99   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
100   bs->try_resolve_jobject_in_native(masm, Robj, R3_ARG1, R4_ARG2, Rtmp, slow);
101 
102   __ srwi(Rtmp, R5_ARG3, jfieldIDWorkaround::offset_shift); // offset
103 
104   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
105   speculative_load_pclist[count] = __ pc();   // Used by the segfault handler
106   bool is_fp = false;
107   switch (type) {
108     case T_BOOLEAN: __ lbzx(Rtmp, Rtmp, Robj); break;
109     case T_BYTE:    __ lbzx(Rtmp, Rtmp, Robj); __ extsb(Rtmp, Rtmp); break;
110     case T_CHAR:    __ lhzx(Rtmp, Rtmp, Robj); break;
111     case T_SHORT:   __ lhax(Rtmp, Rtmp, Robj); break;
112     case T_INT:     __ lwax(Rtmp, Rtmp, Robj); break;
113     case T_LONG:    __ ldx( Rtmp, Rtmp, Robj); break;
114     case T_FLOAT:   __ lfsx(F1_RET, Rtmp, Robj); is_fp = true; break;
115     case T_DOUBLE:  __ lfdx(F1_RET, Rtmp, Robj); is_fp = true; break;
116     default:        ShouldNotReachHere();
117   }
118 
119   // Order preceding load(s) wrt. succeeding check (LoadStore for volatile field).
120   if (is_fp) {
121     Label next;
122     __ fcmpu(CR0, F1_RET, F1_RET);
123     __ bne(CR0, next);
124     __ bind(next);
125   } else {
126     __ twi_0(Rtmp);
127   }
128   __ isync();
129 
130   __ ld(R0, counter_offs, Rcounter_addr);
131   __ cmpd(CR0, R0, Rcounter);
132   __ bne(CR0, slow);
133 
134   if (!is_fp) {
135     __ mr(R3_RET, Rtmp);
136   }
137   __ blr();
138 
139   slowcase_entry_pclist[count++] = __ pc();
140   __ bind(slow);
141   address slow_case_addr;
142   switch (type) {
143     case T_BOOLEAN: slow_case_addr = jni_GetBooleanField_addr(); break;
144     case T_BYTE:    slow_case_addr = jni_GetByteField_addr();    break;
145     case T_CHAR:    slow_case_addr = jni_GetCharField_addr();    break;
146     case T_SHORT:   slow_case_addr = jni_GetShortField_addr();   break;
147     case T_INT:     slow_case_addr = jni_GetIntField_addr();     break;
148     case T_LONG:    slow_case_addr = jni_GetLongField_addr();    break;
149     case T_FLOAT:   slow_case_addr = jni_GetFloatField_addr();   break;
150     case T_DOUBLE:  slow_case_addr = jni_GetDoubleField_addr();  break;
151     default:        ShouldNotReachHere();
152       slow_case_addr = nullptr;  // unreachable
153   }
154   __ load_const_optimized(R12, slow_case_addr, R0);
155   __ call_c_and_return_to_caller(R12); // tail call
156 
157   __ flush();
158 
159   return fast_entry;
160 }
161 
162 address JNI_FastGetField::generate_fast_get_boolean_field() {
163   return generate_fast_get_int_field0(T_BOOLEAN);
164 }
165 
166 address JNI_FastGetField::generate_fast_get_byte_field() {
167   return generate_fast_get_int_field0(T_BYTE);
168 }
169 
170 address JNI_FastGetField::generate_fast_get_char_field() {
171   return generate_fast_get_int_field0(T_CHAR);
172 }
173 
174 address JNI_FastGetField::generate_fast_get_short_field() {
175   return generate_fast_get_int_field0(T_SHORT);
176 }
177 
178 address JNI_FastGetField::generate_fast_get_int_field() {
179   return generate_fast_get_int_field0(T_INT);
180 }
181 
182 address JNI_FastGetField::generate_fast_get_long_field() {
183   return generate_fast_get_int_field0(T_LONG);
184 }
185 
186 address JNI_FastGetField::generate_fast_get_float_field() {
187   return generate_fast_get_int_field0(T_FLOAT);
188 }
189 
190 address JNI_FastGetField::generate_fast_get_double_field() {
191   return generate_fast_get_int_field0(T_DOUBLE);
192 }