1 /*
   2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016, 2017 SAP SE. 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 "code/vtableStubs.hpp"
  29 #include "interp_masm_s390.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_s390.inline.hpp"
  36 #ifdef COMPILER2
  37 #include "opto/runtime.hpp"
  38 #endif
  39 
  40 // Machine-dependent part of VtableStubs: create vtableStub of correct
  41 // size and 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 // Used by compiler only; may use only caller saved, non-argument registers.
  50 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
  51 
  52   const int   code_length = VtableStub::pd_code_size_limit(true);
  53   VtableStub *s = new(code_length) VtableStub(true, vtable_index);
  54   if (s == NULL) { // Indicates OOM In the code cache.
  55     return NULL;
  56   }
  57 
  58   ResourceMark    rm;
  59   CodeBuffer      cb(s->entry_point(), code_length);
  60   MacroAssembler *masm = new MacroAssembler(&cb);
  61 
  62 #if (!defined(PRODUCT) && defined(COMPILER2))
  63   if (CountCompiledCalls) {
  64     // Count unused bytes
  65     __ load_const_optimized_rtn_len(Z_R1_scratch, (long)SharedRuntime::nof_megamorphic_calls_addr(), true);
  66 
  67     // Use generic emitter for direct memory increment.
  68     // Abuse Z_method as scratch register for generic emitter.
  69     // It is loaded further down anyway before it is first used.
  70     __ add2mem_32(Address(Z_R1_scratch), 1, Z_method);
  71   }
  72 #endif
  73 
  74   assert(VtableStub::receiver_location() == Z_R2->as_VMReg(), "receiver expected in Z_ARG1");
  75 
  76   // Get receiver klass.
  77   // Must do an explicit check if implicit checks are disabled.
  78   address npe_addr = __ pc(); // npe == NULL ptr exception
  79   __ null_check(Z_ARG1, Z_R1_scratch, oopDesc::klass_offset_in_bytes());
  80   const Register rcvr_klass = Z_R1_scratch;
  81   __ load_klass(rcvr_klass, Z_ARG1);
  82 
  83   // Set method (in case of interpreted method), and destination address.
  84   int entry_offset = in_bytes(Klass::vtable_start_offset()) +
  85                      vtable_index * vtableEntry::size_in_bytes();
  86 
  87 #ifndef PRODUCT
  88   if (DebugVtables) {
  89     Label L;
  90     // Check offset vs vtable length.
  91     const Register vtable_idx = Z_R0_scratch;
  92 
  93     __ load_const_optimized_rtn_len(vtable_idx, vtable_index*vtableEntry::size_in_bytes(), true);
  94 
  95     assert(Immediate::is_uimm12(in_bytes(Klass::vtable_length_offset())), "disp to large");
  96     __ z_cl(vtable_idx, in_bytes(Klass::vtable_length_offset()), rcvr_klass);
  97     __ z_brl(L);
  98     __ z_lghi(Z_ARG3, vtable_index);  // Debug code, don't optimize.
  99     __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), Z_ARG1, Z_ARG3, false);
 100     __ bind(L);
 101   }
 102 #endif
 103 
 104   int v_off = entry_offset + vtableEntry::method_offset_in_bytes();
 105 
 106   // Duplicate safety code from enc_class Java_Dynamic_Call_dynTOC.
 107   if (Displacement::is_validDisp(v_off)) {
 108     __ z_lg(Z_method/*method oop*/, v_off, rcvr_klass/*class oop*/);
 109   } else {
 110     // Worse case, offset does not fit in displacement field.
 111     __ load_const(Z_method, v_off); // Z_method temporarily holds the offset value.
 112     __ z_lg(Z_method/*method oop*/, 0, Z_method/*method offset*/, rcvr_klass/*class oop*/);
 113   }
 114 
 115 #ifndef PRODUCT
 116   if (DebugVtables) {
 117     Label L;
 118     __ z_ltgr(Z_method, Z_method);
 119     __ z_brne(L);
 120     __ stop("Vtable entry is ZERO",102);
 121     __ bind(L);
 122   }
 123 #endif
 124 
 125   address ame_addr = __ pc(); // ame = abstract method error
 126 
 127   // Must do an explicit check if implicit checks are disabled.
 128   __ null_check(Z_method, Z_R1_scratch, in_bytes(Method::from_compiled_offset()));
 129   __ z_lg(Z_R1_scratch, in_bytes(Method::from_compiled_offset()), Z_method);
 130   __ z_br(Z_R1_scratch);
 131 
 132   masm->flush();
 133 
 134   s->set_exception_points(npe_addr, ame_addr);
 135 
 136   return s;
 137 }
 138 
 139 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
 140   const int   code_length = VtableStub::pd_code_size_limit(false);
 141   VtableStub *s = new(code_length) VtableStub(false, itable_index);
 142   if (s == NULL) { // Indicates OOM in the code cache.
 143     return NULL;
 144   }
 145 
 146   ResourceMark    rm;
 147   CodeBuffer      cb(s->entry_point(), code_length);
 148   MacroAssembler *masm = new MacroAssembler(&cb);
 149 
 150 #if (!defined(PRODUCT) && defined(COMPILER2))
 151   if (CountCompiledCalls) {
 152     __ load_const_optimized_rtn_len(Z_R1_scratch, (long)SharedRuntime::nof_megamorphic_calls_addr(), true);
 153 
 154     // Use generic emitter for direct memory increment.
 155     // Use Z_tmp_1 as scratch register for generic emitter.
 156     __ add2mem_32((Z_R1_scratch), 1, Z_tmp_1);
 157   }
 158 #endif
 159 
 160   assert(VtableStub::receiver_location() == Z_R2->as_VMReg(), "receiver expected in Z_ARG1");
 161 
 162   // Entry arguments:
 163   //  Z_method: Interface
 164   //  Z_ARG1:   Receiver
 165   NearLabel no_such_interface;
 166   const Register rcvr_klass = Z_tmp_1,
 167                  interface  = Z_tmp_2;
 168 
 169   // Get receiver klass.
 170   // Must do an explicit check if implicit checks are disabled.
 171   address npe_addr = __ pc(); // npe == NULL ptr exception
 172   __ null_check(Z_ARG1, Z_R1_scratch, oopDesc::klass_offset_in_bytes());
 173   __ load_klass(rcvr_klass, Z_ARG1);
 174 
 175   // Receiver subtype check against REFC.
 176   __ z_lg(interface, Address(Z_method, CompiledICHolder::holder_klass_offset()));
 177   __ lookup_interface_method(rcvr_klass, interface, noreg,
 178                              noreg, Z_R1, no_such_interface, /*return_method=*/ false);
 179 
 180   // Get Method* and entrypoint for compiler
 181   __ z_lg(interface, Address(Z_method, CompiledICHolder::holder_metadata_offset()));
 182   __ lookup_interface_method(rcvr_klass, interface, itable_index,
 183                              Z_method, Z_R1, no_such_interface, /*return_method=*/ true);
 184 
 185 #ifndef PRODUCT
 186   if (DebugVtables) {
 187     Label ok1;
 188     __ z_ltgr(Z_method, Z_method);
 189     __ z_brne(ok1);
 190     __ stop("method is null",103);
 191     __ bind(ok1);
 192   }
 193 #endif
 194 
 195   address ame_addr = __ pc();
 196   // Must do an explicit check if implicit checks are disabled.
 197   if (!ImplicitNullChecks) {
 198     __ compare64_and_branch(Z_method, (intptr_t) 0, Assembler::bcondEqual, no_such_interface);
 199   }
 200   __ z_lg(Z_R1_scratch, in_bytes(Method::from_compiled_offset()), Z_method);
 201   __ z_br(Z_R1_scratch);
 202 
 203   __ bind(no_such_interface);
 204   // Handle IncompatibleClassChangeError in itable stubs.
 205   // More detailed error message.
 206   // We force resolving of the call site by jumping to the "handle
 207   // wrong method" stub, and so let the interpreter runtime do all the
 208   // dirty work.
 209   __ load_const_optimized_rtn_len(Z_R1_scratch, (long)SharedRuntime::get_handle_wrong_method_stub(), true);
 210   __ z_br(Z_R1_scratch);
 211 
 212   masm->flush();
 213 
 214   s->set_exception_points(npe_addr, ame_addr);
 215   return s;
 216 }
 217 
 218 // In order to tune these parameters, run the JVM with VM options
 219 // +PrintMiscellaneous and +WizardMode to see information about
 220 // actual itable stubs. Run it with -Xmx31G -XX:+UseCompressedOops.
 221 int VtableStub::pd_code_size_limit(bool is_vtable_stub) {
 222   int size = DebugVtables ? 216 : 0;
 223   if (CountCompiledCalls) {
 224     size += 6 * 4;
 225   }
 226   size += is_vtable_stub ? 36 : 140;
 227   if (UseCompressedClassPointers) {
 228     size += MacroAssembler::instr_size_for_decode_klass_not_null();
 229   }
 230   if (!ImplicitNullChecks) {
 231     size += 36;
 232   }
 233   return size;
 234 }
 235 
 236 int VtableStub::pd_code_alignment() {
 237   const unsigned int icache_line_size = 32;
 238   return icache_line_size;
 239 }