1 /*
   2  * Copyright (c) 2008, 2016, 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/assembler.hpp"
  27 #include "assembler_arm.inline.hpp"
  28 #include "code/vtableStubs.hpp"
  29 #include "interp_masm_arm.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/compiledICHolder.hpp"
  32 #include "oops/instanceKlass.hpp"
  33 #include "oops/klassVtable.hpp"
  34 #include "runtime/sharedRuntime.hpp"
  35 #include "vmreg_arm.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   const int code_length = VtableStub::pd_code_size_limit(true);
  51   VtableStub* s = new(code_length) VtableStub(true, vtable_index);
  52   // Can be NULL if there is no free space in the code cache.
  53   if (s == NULL) {
  54     return NULL;
  55   }
  56 
  57   ResourceMark rm;
  58   CodeBuffer cb(s->entry_point(), code_length);
  59   MacroAssembler* masm = new MacroAssembler(&cb);
  60 
  61   assert(VtableStub::receiver_location() == R0->as_VMReg(), "receiver expected in R0");
  62 
  63   const Register tmp = Rtemp; // Rtemp OK, should be free at call sites
  64 
  65   address npe_addr = __ pc();
  66   __ load_klass(tmp, R0);
  67 
  68   {
  69   int entry_offset = in_bytes(Klass::vtable_start_offset()) + vtable_index * vtableEntry::size_in_bytes();
  70   int method_offset = vtableEntry::method_offset_in_bytes() + entry_offset;
  71 
  72   assert ((method_offset & (wordSize - 1)) == 0, "offset should be aligned");
  73   int offset_mask = AARCH64_ONLY(0xfff << LogBytesPerWord) NOT_AARCH64(0xfff);
  74   if (method_offset & ~offset_mask) {
  75     __ add(tmp, tmp, method_offset & ~offset_mask);
  76   }
  77   __ ldr(Rmethod, Address(tmp, method_offset & offset_mask));
  78   }
  79 
  80   address ame_addr = __ pc();
  81 #ifdef AARCH64
  82   __ ldr(tmp, Address(Rmethod, Method::from_compiled_offset()));
  83   __ br(tmp);
  84 #else
  85   __ ldr(PC, Address(Rmethod, Method::from_compiled_offset()));
  86 #endif // AARCH64
  87 
  88   masm->flush();
  89 
  90   if (PrintMiscellaneous && (WizardMode || Verbose)) {
  91     tty->print_cr("vtable #%d at " PTR_FORMAT "[%d] left over: %d",
  92                   vtable_index, p2i(s->entry_point()),
  93                   (int)(s->code_end() - s->entry_point()),
  94                   (int)(s->code_end() - __ pc()));
  95   }
  96   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
  97   // FIXME ARM: need correct 'slop' - below is x86 code
  98   // shut the door on sizing bugs
  99   //int slop = 8;  // 32-bit offset is this much larger than a 13-bit one
 100   //assert(vtable_index > 10 || __ pc() + slop <= s->code_end(), "room for 32-bit offset");
 101 
 102   s->set_exception_points(npe_addr, ame_addr);
 103   return s;
 104 }
 105 
 106 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
 107   const int code_length = VtableStub::pd_code_size_limit(false);
 108   VtableStub* s = new(code_length) VtableStub(false, itable_index);
 109   // Can be NULL if there is no free space in the code cache.
 110   if (s == NULL) {
 111     return NULL;
 112   }
 113 
 114   ResourceMark rm;
 115   CodeBuffer cb(s->entry_point(), code_length);
 116   MacroAssembler* masm = new MacroAssembler(&cb);
 117 
 118   assert(VtableStub::receiver_location() == R0->as_VMReg(), "receiver expected in R0");
 119 
 120   // R0-R3 / R0-R7 registers hold the arguments and cannot be spoiled
 121   const Register Rclass  = AARCH64_ONLY(R9)  NOT_AARCH64(R4);
 122   const Register Rintf   = AARCH64_ONLY(R10) NOT_AARCH64(R5);
 123   const Register Rscan   = AARCH64_ONLY(R11) NOT_AARCH64(R6);
 124 
 125   assert_different_registers(Ricklass, Rclass, Rintf, Rscan, Rtemp);
 126 
 127   // Calculate the start of itable (itable goes after vtable)
 128   const int scale = exact_log2(vtableEntry::size_in_bytes());
 129   address npe_addr = __ pc();
 130   __ load_klass(Rclass, R0);
 131 
 132   Label L_no_such_interface;
 133 
 134   // Receiver subtype check against REFC.
 135   __ ldr(Rintf, Address(Ricklass, CompiledICHolder::holder_klass_offset()));
 136   __ lookup_interface_method(// inputs: rec. class, interface, itable index
 137                              Rclass, Rintf, noreg,
 138                              // outputs: temp reg1, temp reg2
 139                              noreg, Rscan, Rtemp,
 140                              L_no_such_interface);
 141 
 142   // Get Method* and entry point for compiler
 143   __ ldr(Rintf, Address(Ricklass, CompiledICHolder::holder_metadata_offset()));
 144   __ lookup_interface_method(// inputs: rec. class, interface, itable index
 145                              Rclass, Rintf, itable_index,
 146                              // outputs: temp reg1, temp reg2, temp reg3
 147                              Rmethod, Rscan, Rtemp,
 148                              L_no_such_interface);
 149 
 150   address ame_addr = __ pc();
 151 
 152 #ifdef AARCH64
 153   __ ldr(Rtemp, Address(Rmethod, Method::from_compiled_offset()));
 154   __ br(Rtemp);
 155 #else
 156   __ ldr(PC, Address(Rmethod, Method::from_compiled_offset()));
 157 #endif // AARCH64
 158 
 159   __ bind(L_no_such_interface);
 160 
 161   assert(StubRoutines::throw_IncompatibleClassChangeError_entry() != NULL, "check initialization order");
 162   __ jump(StubRoutines::throw_IncompatibleClassChangeError_entry(), relocInfo::runtime_call_type, Rtemp);
 163 
 164   masm->flush();
 165 
 166   if (PrintMiscellaneous && (WizardMode || Verbose)) {
 167     tty->print_cr("itable #%d at " PTR_FORMAT "[%d] left over: %d",
 168                   itable_index, p2i(s->entry_point()),
 169                   (int)(s->code_end() - s->entry_point()),
 170                   (int)(s->code_end() - __ pc()));
 171   }
 172   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 173   // FIXME ARM: need correct 'slop' - below is x86 code
 174   // shut the door on sizing bugs
 175   //int slop = 8;  // 32-bit offset is this much larger than a 13-bit one
 176   //assert(itable_index > 10 || __ pc() + slop <= s->code_end(), "room for 32-bit offset");
 177 
 178   s->set_exception_points(npe_addr, ame_addr);
 179   return s;
 180 }
 181 
 182 int VtableStub::pd_code_size_limit(bool is_vtable_stub) {
 183   int instr_count;
 184 
 185   if (is_vtable_stub) {
 186     // vtable stub size
 187     instr_count = NOT_AARCH64(4) AARCH64_ONLY(5);
 188   } else {
 189     // itable stub size
 190     instr_count = NOT_AARCH64(31) AARCH64_ONLY(31);
 191   }
 192 
 193 #ifdef AARCH64
 194   if (UseCompressedClassPointers) {
 195     instr_count += MacroAssembler::instr_count_for_decode_klass_not_null();
 196   }
 197 #endif // AARCH64
 198 
 199   return instr_count * Assembler::InstructionSize;
 200 }
 201 
 202 int VtableStub::pd_code_alignment() {
 203   return 8;
 204 }