1 /*
2  * Copyright (c) 2008, 2017, 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 "assembler_arm.inline.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "prims/jniFastGetField.hpp"
30 #include "prims/jvm_misc.hpp"
31 #include "runtime/safepoint.hpp"
32 
33 #define __ masm->
34 
35 #define BUFFER_SIZE  96
36 
generate_fast_get_int_field0(BasicType type)37 address JNI_FastGetField::generate_fast_get_int_field0(BasicType type) {
38   const char* name = NULL;
39   address slow_case_addr = NULL;
40   switch (type) {
41     case T_BOOLEAN:
42       name = "jni_fast_GetBooleanField";
43       slow_case_addr = jni_GetBooleanField_addr();
44       break;
45     case T_BYTE:
46       name = "jni_fast_GetByteField";
47       slow_case_addr = jni_GetByteField_addr();
48       break;
49     case T_CHAR:
50       name = "jni_fast_GetCharField";
51       slow_case_addr = jni_GetCharField_addr();
52       break;
53     case T_SHORT:
54       name = "jni_fast_GetShortField";
55       slow_case_addr = jni_GetShortField_addr();
56       break;
57     case T_INT:
58       name = "jni_fast_GetIntField";
59       slow_case_addr = jni_GetIntField_addr();
60       break;
61     case T_LONG:
62       name = "jni_fast_GetLongField";
63       slow_case_addr = jni_GetLongField_addr();
64       break;
65     case T_FLOAT:
66       name = "jni_fast_GetFloatField";
67       slow_case_addr = jni_GetFloatField_addr();
68       break;
69     case T_DOUBLE:
70       name = "jni_fast_GetDoubleField";
71       slow_case_addr = jni_GetDoubleField_addr();
72       break;
73     default:
74       ShouldNotReachHere();
75   }
76 
77   // R0 - jni env
78   // R1 - object handle
79   // R2 - jfieldID
80 
81   const Register Rsafepoint_counter_addr = AARCH64_ONLY(R4) NOT_AARCH64(R3);
82   const Register Robj = AARCH64_ONLY(R5) NOT_AARCH64(R1);
83   const Register Rres = AARCH64_ONLY(R6) NOT_AARCH64(R0);
84 #ifndef AARCH64
85   const Register Rres_hi = R1;
86 #endif // !AARCH64
87   const Register Rsafept_cnt = Rtemp;
88   const Register Rsafept_cnt2 = Rsafepoint_counter_addr;
89   const Register Rtmp1 = AARCH64_ONLY(R7) NOT_AARCH64(R3); // same as Rsafepoint_counter_addr on 32-bit ARM
90   const Register Rtmp2 = AARCH64_ONLY(R8) NOT_AARCH64(R2); // same as jfieldID on 32-bit ARM
91 
92 #ifdef AARCH64
93   assert_different_registers(Rsafepoint_counter_addr, Rsafept_cnt, Robj, Rres, Rtmp1, Rtmp2, R0, R1, R2, LR);
94   assert_different_registers(Rsafept_cnt2, Rsafept_cnt, Rres, R0, R1, R2, LR);
95 #else
96   assert_different_registers(Rsafepoint_counter_addr, Rsafept_cnt, Robj, Rres, LR);
97   assert_different_registers(Rsafept_cnt, R1, R2, Rtmp1, LR);
98   assert_different_registers(Rsafepoint_counter_addr, Rsafept_cnt, Rres, Rres_hi, Rtmp2, LR);
99   assert_different_registers(Rsafept_cnt2, Rsafept_cnt, Rres, Rres_hi, LR);
100 #endif // AARCH64
101 
102   address fast_entry;
103 
104   ResourceMark rm;
105   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
106   CodeBuffer cbuf(blob);
107   MacroAssembler* masm = new MacroAssembler(&cbuf);
108   fast_entry = __ pc();
109 
110   // Safepoint check
111   InlinedAddress safepoint_counter_addr(SafepointSynchronize::safepoint_counter_addr());
112   Label slow_case;
113   __ ldr_literal(Rsafepoint_counter_addr, safepoint_counter_addr);
114 
115 #ifndef AARCH64
116   __ push(RegisterSet(R0, R3));  // save incoming arguments for slow case
117 #endif // !AARCH64
118 
119   __ ldr_s32(Rsafept_cnt, Address(Rsafepoint_counter_addr));
120   __ tbnz(Rsafept_cnt, 0, slow_case);
121 
122 #ifdef AARCH64
123   // If mask changes we need to ensure that the inverse is still encodable as an immediate
124   STATIC_ASSERT(JNIHandles::weak_tag_mask == 1);
125   __ andr(R1, R1, ~JNIHandles::weak_tag_mask);
126 #else
127   __ bic(R1, R1, JNIHandles::weak_tag_mask);
128 #endif
129 
130   if (os::is_MP()) {
131     // Address dependency restricts memory access ordering. It's cheaper than explicit LoadLoad barrier
132     __ andr(Rtmp1, Rsafept_cnt, (unsigned)1);
133     __ ldr(Robj, Address(R1, Rtmp1));
134   } else {
135     __ ldr(Robj, Address(R1));
136   }
137 
138 #ifdef AARCH64
139   __ add(Robj, Robj, AsmOperand(R2, lsr, 2));
140   Address field_addr = Address(Robj);
141 #else
142   Address field_addr;
143   if (type != T_BOOLEAN
144       && type != T_INT
145 #ifndef __ABI_HARD__
146       && type != T_FLOAT
147 #endif // !__ABI_HARD__
148       ) {
149     // Only ldr and ldrb support embedded shift, other loads do not
150     __ add(Robj, Robj, AsmOperand(R2, lsr, 2));
151     field_addr = Address(Robj);
152   } else {
153     field_addr = Address(Robj, R2, lsr, 2);
154   }
155 #endif // AARCH64
156   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
157   speculative_load_pclist[count] = __ pc();
158 
159   switch (type) {
160     case T_BOOLEAN:
161       __ ldrb(Rres, field_addr);
162       break;
163     case T_BYTE:
164       __ ldrsb(Rres, field_addr);
165       break;
166     case T_CHAR:
167       __ ldrh(Rres, field_addr);
168       break;
169     case T_SHORT:
170       __ ldrsh(Rres, field_addr);
171       break;
172     case T_INT:
173 #ifndef __ABI_HARD__
174     case T_FLOAT:
175 #endif
176       __ ldr_s32(Rres, field_addr);
177       break;
178     case T_LONG:
179 #ifndef __ABI_HARD__
180     case T_DOUBLE:
181 #endif
182 #ifdef AARCH64
183       __ ldr(Rres, field_addr);
184 #else
185       // Safe to use ldrd since long and double fields are 8-byte aligned
186       __ ldrd(Rres, field_addr);
187 #endif // AARCH64
188       break;
189 #ifdef __ABI_HARD__
190     case T_FLOAT:
191       __ ldr_float(S0, field_addr);
192       break;
193     case T_DOUBLE:
194       __ ldr_double(D0, field_addr);
195       break;
196 #endif // __ABI_HARD__
197     default:
198       ShouldNotReachHere();
199   }
200 
201   if(os::is_MP()) {
202       // Address dependency restricts memory access ordering. It's cheaper than explicit LoadLoad barrier
203 #if defined(__ABI_HARD__) && !defined(AARCH64)
204     if (type == T_FLOAT || type == T_DOUBLE) {
205       __ ldr_literal(Rsafepoint_counter_addr, safepoint_counter_addr);
206       __ fmrrd(Rres, Rres_hi, D0);
207       __ eor(Rtmp2, Rres, Rres);
208       __ ldr_s32(Rsafept_cnt2, Address(Rsafepoint_counter_addr, Rtmp2));
209     } else
210 #endif // __ABI_HARD__ && !AARCH64
211     {
212 #ifndef AARCH64
213       __ ldr_literal(Rsafepoint_counter_addr, safepoint_counter_addr);
214 #endif // !AARCH64
215       __ eor(Rtmp2, Rres, Rres);
216       __ ldr_s32(Rsafept_cnt2, Address(Rsafepoint_counter_addr, Rtmp2));
217     }
218   } else {
219     __ ldr_s32(Rsafept_cnt2, Address(Rsafepoint_counter_addr));
220   }
221   __ cmp(Rsafept_cnt2, Rsafept_cnt);
222 #ifdef AARCH64
223   __ b(slow_case, ne);
224   __ mov(R0, Rres);
225   __ ret();
226 #else
227   // discards saved R0 R1 R2 R3
228   __ add(SP, SP, 4 * wordSize, eq);
229   __ bx(LR, eq);
230 #endif // AARCH64
231 
232   slowcase_entry_pclist[count++] = __ pc();
233 
234   __ bind(slow_case);
235 #ifndef AARCH64
236   __ pop(RegisterSet(R0, R3));
237 #endif // !AARCH64
238   // thumb mode switch handled by MacroAssembler::jump if needed
239   __ jump(slow_case_addr, relocInfo::none, Rtemp);
240 
241   __ bind_literal(safepoint_counter_addr);
242 
243   __ flush();
244 
245   guarantee((__ pc() - fast_entry) <= BUFFER_SIZE, "BUFFER_SIZE too small");
246 
247   return fast_entry;
248 }
249 
generate_fast_get_float_field0(BasicType type)250 address JNI_FastGetField::generate_fast_get_float_field0(BasicType type) {
251   ShouldNotReachHere();
252   return NULL;
253 }
254 
generate_fast_get_boolean_field()255 address JNI_FastGetField::generate_fast_get_boolean_field() {
256   return generate_fast_get_int_field0(T_BOOLEAN);
257 }
258 
generate_fast_get_byte_field()259 address JNI_FastGetField::generate_fast_get_byte_field() {
260   return generate_fast_get_int_field0(T_BYTE);
261 }
262 
generate_fast_get_char_field()263 address JNI_FastGetField::generate_fast_get_char_field() {
264   return generate_fast_get_int_field0(T_CHAR);
265 }
266 
generate_fast_get_short_field()267 address JNI_FastGetField::generate_fast_get_short_field() {
268   return generate_fast_get_int_field0(T_SHORT);
269 }
270 
generate_fast_get_int_field()271 address JNI_FastGetField::generate_fast_get_int_field() {
272   return generate_fast_get_int_field0(T_INT);
273 }
274 
generate_fast_get_long_field()275 address JNI_FastGetField::generate_fast_get_long_field() {
276   return generate_fast_get_int_field0(T_LONG);
277 }
278 
generate_fast_get_float_field()279 address JNI_FastGetField::generate_fast_get_float_field() {
280   return generate_fast_get_int_field0(T_FLOAT);
281 }
282 
generate_fast_get_double_field()283 address JNI_FastGetField::generate_fast_get_double_field() {
284   return generate_fast_get_int_field0(T_DOUBLE);
285 }
286