1 /*
  2  * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 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 "asm/assembler.inline.hpp"
 27 #include "asm/macroAssembler.inline.hpp"
 28 #include "code/compiledIC.hpp"
 29 #include "code/vtableStubs.hpp"
 30 #include "interp_masm_aarch64.hpp"
 31 #include "memory/resourceArea.hpp"
 32 #include "oops/instanceKlass.hpp"
 33 #include "oops/klassVtable.hpp"
 34 #include "runtime/sharedRuntime.hpp"
 35 #include "vmreg_aarch64.inline.hpp"
 36 #ifdef COMPILER2
 37 #include "opto/runtime.hpp"
 38 #endif
 39 
 40 // machine-dependent part of VtableStubs: create VtableStub of correct size and
 41 // initialize its code
 42 
 43 #define __ masm->
 44 
 45 #ifndef PRODUCT
 46 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index);
 47 #endif
 48 
 49 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
 50   // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
 51   const int stub_code_length = code_size_limit(true);
 52   VtableStub* s = new(stub_code_length) VtableStub(true, vtable_index);
 53   // Can be null if there is no free space in the code cache.
 54   if (s == nullptr) {
 55     return nullptr;
 56   }
 57 
 58   // Count unused bytes in instruction sequences of variable size.
 59   // We add them to the computed buffer size in order to avoid
 60   // overflow in subsequently generated stubs.
 61   address   start_pc;
 62   int       slop_bytes = 0;
 63   int       slop_delta = 0;
 64 
 65   ResourceMark    rm;
 66   CodeBuffer      cb(s->entry_point(), stub_code_length);
 67   MacroAssembler* masm = new MacroAssembler(&cb);
 68 
 69 #if (!defined(PRODUCT) && defined(COMPILER2))
 70   if (CountCompiledCalls) {
 71     __ lea(r16, ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
 72     __ increment(Address(r16));
 73   }
 74 #endif
 75 
 76   // get receiver (need to skip return address on top of stack)
 77   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
 78 
 79   // get receiver klass
 80   address npe_addr = __ pc();
 81   __ load_klass(r16, j_rarg0);
 82 
 83 #ifndef PRODUCT
 84   if (DebugVtables) {
 85     Label L;
 86     // TODO: find upper bound for this debug code.
 87     start_pc = __ pc();
 88 
 89     // check offset vs vtable length
 90     __ ldrw(rscratch1, Address(r16, Klass::vtable_length_offset()));
 91     __ cmpw(rscratch1, vtable_index * vtableEntry::size());
 92     __ br(Assembler::GT, L);
 93     __ enter();
 94     __ mov(r2, vtable_index);
 95 
 96     __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), j_rarg0, r2);
 97     const ptrdiff_t estimate = 256;
 98     const ptrdiff_t codesize = __ pc() - start_pc;
 99     slop_delta  = estimate - codesize;  // call_VM varies in length, depending on data
100     slop_bytes += slop_delta;
101     assert(slop_delta >= 0, "vtable #%d: Code size estimate (%d) for DebugVtables too small, required: %d", vtable_index, (int)estimate, (int)codesize);
102 
103     __ leave();
104     __ bind(L);
105   }
106 #endif // PRODUCT
107 
108   start_pc = __ pc();
109   __ lookup_virtual_method(r16, vtable_index, rmethod);
110   slop_delta  = 8 - (int)(__ pc() - start_pc);
111   slop_bytes += slop_delta;
112   assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
113 
114 #ifndef PRODUCT
115   if (DebugVtables) {
116     Label L;
117     __ cbz(rmethod, L);
118     __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
119     __ cbnz(rscratch1, L);
120     __ stop("Vtable entry is null");
121     __ bind(L);
122   }
123 #endif // PRODUCT
124 
125   // r0: receiver klass
126   // rmethod: Method*
127   // r2: receiver
128   address ame_addr = __ pc();
129   __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
130   __ br(rscratch1);
131 
132   masm->flush();
133   bookkeeping(masm, tty, s, npe_addr, ame_addr, true, vtable_index, slop_bytes, 0);
134 
135   return s;
136 }
137 
138 
139 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
140   // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
141   const int stub_code_length = code_size_limit(false);
142   VtableStub* s = new(stub_code_length) VtableStub(false, itable_index);
143   // Can be null if there is no free space in the code cache.
144   if (s == nullptr) {
145     return nullptr;
146   }
147 
148   // Count unused bytes in instruction sequences of variable size.
149   // We add them to the computed buffer size in order to avoid
150   // overflow in subsequently generated stubs.
151   address   start_pc;
152   int       slop_bytes = 0;
153   int       slop_delta = 0;
154 
155   ResourceMark    rm;
156   CodeBuffer      cb(s->entry_point(), stub_code_length);
157   MacroAssembler* masm = new MacroAssembler(&cb);
158 
159 #if (!defined(PRODUCT) && defined(COMPILER2))
160   if (CountCompiledCalls) {
161     __ lea(r10, ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
162     __ increment(Address(r10));
163   }
164 #endif
165 
166   // get receiver (need to skip return address on top of stack)
167   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
168 
169   // Entry arguments:
170   //  rscratch2: CompiledICData
171   //  j_rarg0: Receiver
172 
173   // This stub is called from compiled code which has no callee-saved registers,
174   // so all registers except arguments are free at this point.
175   const Register recv_klass_reg     = r10;
176   const Register holder_klass_reg   = r16; // declaring interface klass (DEFC)
177   const Register resolved_klass_reg = r17; // resolved interface klass (REFC)
178   const Register temp_reg           = r11;
179   const Register temp_reg2          = r15;
180   const Register icdata_reg         = rscratch2;
181 
182   Label L_no_such_interface;
183 
184   __ ldr(resolved_klass_reg, Address(icdata_reg, CompiledICData::itable_refc_klass_offset()));
185   __ ldr(holder_klass_reg,   Address(icdata_reg, CompiledICData::itable_defc_klass_offset()));
186 
187   start_pc = __ pc();
188 
189   // get receiver klass (also an implicit null-check)
190   address npe_addr = __ pc();
191   __ load_klass(recv_klass_reg, j_rarg0);
192 
193   // Receiver subtype check against REFC.
194   // Get selected method from declaring class and itable index
195   __ lookup_interface_method_stub(recv_klass_reg, holder_klass_reg, resolved_klass_reg, rmethod,
196                                   temp_reg, temp_reg2, itable_index, L_no_such_interface);
197 
198   // Reduce "estimate" such that "padding" does not drop below 8.
199   const ptrdiff_t estimate = 144;
200   const ptrdiff_t codesize = __ pc() - start_pc;
201   slop_delta  = (int)(estimate - codesize);
202   slop_bytes += slop_delta;
203   assert(slop_delta >= 0, "itable #%d: Code size estimate (%d) for lookup_interface_method too small, required: %d", itable_index, (int)estimate, (int)codesize);
204 
205 #ifdef ASSERT
206   if (DebugVtables) {
207     Label L2;
208     __ cbz(rmethod, L2);
209     __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
210     __ cbnz(rscratch1, L2);
211     __ stop("compiler entrypoint is null");
212     __ bind(L2);
213   }
214 #endif // ASSERT
215 
216   // rmethod: Method*
217   // j_rarg0: receiver
218   address ame_addr = __ pc();
219   __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
220   __ br(rscratch1);
221 
222   __ bind(L_no_such_interface);
223   // Handle IncompatibleClassChangeError in itable stubs.
224   // More detailed error message.
225   // We force resolving of the call site by jumping to the "handle
226   // wrong method" stub, and so let the interpreter runtime do all the
227   // dirty work.
228   assert(SharedRuntime::get_handle_wrong_method_stub() != nullptr, "check initialization order");
229   __ far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
230 
231   masm->flush();
232   bookkeeping(masm, tty, s, npe_addr, ame_addr, false, itable_index, slop_bytes, 0);
233 
234   return s;
235 }
236 
237 int VtableStub::pd_code_alignment() {
238   // aarch64 cache line size is not an architected constant. We just align on 4 bytes (instruction size).
239   const unsigned int icache_line_size = 4;
240   return icache_line_size;
241 }