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