1 /*
   2  * Copyright (c) 2003, 2014, 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 "code/vtableStubs.hpp"
  28 #include "interp_masm_x86.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/instanceKlass.hpp"
  31 #include "oops/klassVtable.hpp"
  32 #include "runtime/sharedRuntime.hpp"
  33 #include "vmreg_x86.inline.hpp"
  34 #ifdef COMPILER2
  35 #include "opto/runtime.hpp"
  36 #endif
  37 
  38 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  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,
  47                                           oop receiver,
  48                                           int index);
  49 #endif
  50 
  51 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
  52   const int amd64_code_length = VtableStub::pd_code_size_limit(true);
  53   VtableStub* s = new(amd64_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   ResourceMark rm;
  60   CodeBuffer cb(s->entry_point(), amd64_code_length);
  61   MacroAssembler* masm = new MacroAssembler(&cb);
  62 
  63 #ifndef PRODUCT
  64   if (CountCompiledCalls) {
  65     __ incrementl(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
  66   }
  67 #endif
  68 
  69   // get receiver (need to skip return address on top of stack)
  70   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
  71 
  72   // Free registers (non-args) are rax, rbx
  73 
  74   // get receiver klass
  75   address npe_addr = __ pc();
  76   __ load_klass(rax, j_rarg0);
  77 
  78 #ifndef PRODUCT
  79   if (DebugVtables) {
  80     Label L;
  81     // check offset vs vtable length
  82     __ cmpl(Address(rax, InstanceKlass::vtable_length_offset() * wordSize),
  83             vtable_index * vtableEntry::size());
  84     __ jcc(Assembler::greater, L);
  85     __ movl(rbx, vtable_index);
  86     __ call_VM(noreg,
  87                CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), j_rarg0, rbx);
  88     __ bind(L);
  89   }
  90 #endif // PRODUCT
  91 
  92   // load Method* and target address
  93   const Register method = rbx;
  94 
  95   __ lookup_virtual_method(rax, vtable_index, method);
  96 
  97   if (DebugVtables) {
  98     Label L;
  99     __ cmpptr(method, (int32_t)NULL_WORD);
 100     __ jcc(Assembler::equal, L);
 101     __ cmpptr(Address(method, Method::from_compiled_offset()), (int32_t)NULL_WORD);
 102     __ jcc(Assembler::notZero, L);
 103     __ stop("Vtable entry is NULL");
 104     __ bind(L);
 105   }
 106   // rax: receiver klass
 107   // rbx: Method*
 108   // rcx: receiver
 109   address ame_addr = __ pc();
 110   __ jmp( Address(rbx, Method::from_compiled_offset()));
 111 
 112   __ flush();
 113 
 114   if (PrintMiscellaneous && (WizardMode || Verbose)) {
 115     tty->print_cr("vtable #%d at " PTR_FORMAT "[%d] left over: %d",
 116                   vtable_index, s->entry_point(),
 117                   (int)(s->code_end() - s->entry_point()),
 118                   (int)(s->code_end() - __ pc()));
 119   }
 120   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 121   // shut the door on sizing bugs
 122   int slop = 3;  // 32-bit offset is this much larger than an 8-bit one
 123   assert(vtable_index > 10 || __ pc() + slop <= s->code_end(), "room for 32-bit offset");
 124 
 125   s->set_exception_points(npe_addr, ame_addr);
 126   return s;
 127 }
 128 
 129 
 130 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
 131   // Note well: pd_code_size_limit is the absolute minimum we can get
 132   // away with.  If you add code here, bump the code stub size
 133   // returned by pd_code_size_limit!
 134   const int amd64_code_length = VtableStub::pd_code_size_limit(false);
 135   VtableStub* s = new(amd64_code_length) VtableStub(false, itable_index);
 136   // Can be NULL if there is no free space in the code cache.
 137   if (s == NULL) {
 138     return NULL;
 139   }
 140 
 141   ResourceMark rm;
 142   CodeBuffer cb(s->entry_point(), amd64_code_length);
 143   MacroAssembler* masm = new MacroAssembler(&cb);
 144 
 145 #ifndef PRODUCT
 146   if (CountCompiledCalls) {
 147     __ incrementl(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
 148   }
 149 #endif
 150 
 151   // Entry arguments:
 152   //  rax: Interface
 153   //  j_rarg0: Receiver
 154 
 155   // Free registers (non-args) are rax (interface), rbx
 156 
 157   // get receiver (need to skip return address on top of stack)
 158 
 159   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
 160   // get receiver klass (also an implicit null-check)
 161   address npe_addr = __ pc();
 162 
 163   // Most registers are in use; we'll use rax, rbx, r10, r11
 164   // (various calling sequences use r[cd]x, r[sd]i, r[89]; stay away from them)
 165   __ load_klass(r10, j_rarg0);
 166 
 167   // If we take a trap while this arg is on the stack we will not
 168   // be able to walk the stack properly. This is not an issue except
 169   // when there are mistakes in this assembly code that could generate
 170   // a spurious fault. Ask me how I know...
 171 
 172   const Register method = rbx;
 173   Label throw_icce;
 174 
 175   // Get Method* and entrypoint for compiler
 176   __ lookup_interface_method(// inputs: rec. class, interface, itable index
 177                              r10, rax, itable_index,
 178                              // outputs: method, scan temp. reg
 179                              method, r11,
 180                              throw_icce);
 181 
 182   // method (rbx): Method*
 183   // j_rarg0: receiver
 184 
 185 #ifdef ASSERT
 186   if (DebugVtables) {
 187     Label L2;
 188     __ cmpptr(method, (int32_t)NULL_WORD);
 189     __ jcc(Assembler::equal, L2);
 190     __ cmpptr(Address(method, Method::from_compiled_offset()), (int32_t)NULL_WORD);
 191     __ jcc(Assembler::notZero, L2);
 192     __ stop("compiler entrypoint is null");
 193     __ bind(L2);
 194   }
 195 #endif // ASSERT
 196 
 197   // rbx: Method*
 198   // j_rarg0: receiver
 199   address ame_addr = __ pc();
 200   __ jmp(Address(method, Method::from_compiled_offset()));
 201 
 202   __ bind(throw_icce);
 203   __ jump(RuntimeAddress(StubRoutines::throw_IncompatibleClassChangeError_entry()));
 204 
 205   __ flush();
 206 
 207   if (PrintMiscellaneous && (WizardMode || Verbose)) {
 208     tty->print_cr("itable #%d at " PTR_FORMAT "[%d] left over: %d",
 209                   itable_index, s->entry_point(),
 210                   (int)(s->code_end() - s->entry_point()),
 211                   (int)(s->code_end() - __ pc()));
 212   }
 213   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 214   // shut the door on sizing bugs
 215   int slop = 3;  // 32-bit offset is this much larger than an 8-bit one
 216   assert(itable_index > 10 || __ pc() + slop <= s->code_end(), "room for 32-bit offset");
 217 
 218   s->set_exception_points(npe_addr, ame_addr);
 219   return s;
 220 }
 221 
 222 int VtableStub::pd_code_size_limit(bool is_vtable_stub) {
 223   if (is_vtable_stub) {
 224     // Vtable stub size
 225     return (DebugVtables ? 512 : 24) + (CountCompiledCalls ? 13 : 0) +
 226            (UseCompressedClassPointers ?  MacroAssembler::instr_size_for_decode_klass_not_null() : 0);
 227   } else {
 228     // Itable stub size
 229     return (DebugVtables ? 512 : 74) + (CountCompiledCalls ? 13 : 0) +
 230            (UseCompressedClassPointers ?  MacroAssembler::instr_size_for_decode_klass_not_null() : 0);
 231   }
 232   // In order to tune these parameters, run the JVM with VM options
 233   // +PrintMiscellaneous and +WizardMode to see information about
 234   // actual itable stubs.  Look for lines like this:
 235   //   itable #1 at 0x5551212[71] left over: 3
 236   // Reduce the constants so that the "left over" number is >=3
 237   // for the common cases.
 238   // Do not aim at a left-over number of zero, because a
 239   // large vtable or itable index (>= 32) will require a 32-bit
 240   // immediate displacement instead of an 8-bit one.
 241   //
 242   // The JVM98 app. _202_jess has a megamorphic interface call.
 243   // The itable code looks like this:
 244   // Decoding VtableStub itbl[1]@12
 245   //   mov    0x8(%rsi),%r10
 246   //   mov    0x198(%r10),%r11d
 247   //   lea    0x218(%r10,%r11,8),%r11
 248   //   lea    0x8(%r10),%r10
 249   //   mov    (%r11),%rbx
 250   //   cmp    %rbx,%rax
 251   //   je     success
 252   // loop:
 253   //   test   %rbx,%rbx
 254   //   je     throw_icce
 255   //   add    $0x10,%r11
 256   //   mov    (%r11),%rbx
 257   //   cmp    %rbx,%rax
 258   //   jne    loop
 259   // success:
 260   //   mov    0x8(%r11),%r11d
 261   //   mov    (%r10,%r11,1),%rbx
 262   //   jmpq   *0x60(%rbx)
 263   // throw_icce:
 264   //   jmpq   throw_ICCE_entry
 265 }
 266 
 267 int VtableStub::pd_code_alignment() {
 268   return wordSize;
 269 }