1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016, 2018 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/klass.inline.hpp"
  34 #include "oops/klassVtable.hpp"
  35 #include "runtime/sharedRuntime.hpp"
  36 #include "vmreg_s390.inline.hpp"
  37 #ifdef COMPILER2
  38 #include "opto/runtime.hpp"
  39 #endif
  40 
  41 #define __ masm->
  42 
  43 #ifndef PRODUCT
  44 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index);
  45 #endif
  46 
  47 // Used by compiler only; may use only caller saved, non-argument registers.
  48 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
  49   // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
  50   const int stub_code_length = code_size_limit(true);
  51   VtableStub* s = new(stub_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   // Count unused bytes in instruction sequences of variable size.
  58   // We add them to the computed buffer size in order to avoid
  59   // overflow in subsequently generated stubs.
  60   address   start_pc;
  61   int       slop_bytes = 0;
  62   int       slop_delta = 0;
  63 
  64   ResourceMark    rm;
  65   CodeBuffer      cb(s->entry_point(), stub_code_length);
  66   MacroAssembler* masm = new MacroAssembler(&cb);
  67 
  68 #if (!defined(PRODUCT) && defined(COMPILER2))
  69   if (CountCompiledCalls) {
  70     //               worst case             actual size
  71     slop_delta  = __ load_const_size() - __ load_const_optimized_rtn_len(Z_R1_scratch, (long)SharedRuntime::nof_megamorphic_calls_addr(), true);
  72     slop_bytes += slop_delta;
  73     assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
  74     // Use generic emitter for direct memory increment.
  75     // Abuse Z_method as scratch register for generic emitter.
  76     // It is loaded further down anyway before it is first used.
  77     // No dynamic code size variance here, increment is 1, always.
  78     __ add2mem_32(Address(Z_R1_scratch), 1, Z_method);
  79   }
  80 #endif
  81 
  82   assert(VtableStub::receiver_location() == Z_R2->as_VMReg(), "receiver expected in Z_ARG1");
  83 
  84   const Register rcvr_klass   = Z_R1_scratch;
  85   address        npe_addr     = __ pc(); // npe == NULL ptr exception
  86   // check if we must do an explicit check (implicit checks disabled, offset too large).
  87   __ null_check(Z_ARG1, Z_R1_scratch, oopDesc::klass_offset_in_bytes());
  88   // Get receiver klass.
  89   __ load_klass(rcvr_klass, Z_ARG1);
  90 
  91 #ifndef PRODUCT
  92   if (DebugVtables) {
  93     NearLabel L;
  94     // Check offset vs vtable length.
  95     const Register vtable_idx = Z_R0_scratch;
  96 
  97     //               worst case             actual size
  98     slop_delta  = __ load_const_size() - __ load_const_optimized_rtn_len(vtable_idx, vtable_index*vtableEntry::size(), true);
  99     slop_bytes += slop_delta;
 100     assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
 101 
 102     assert(Displacement::is_shortDisp(in_bytes(Klass::vtable_length_offset())), "disp to large");
 103     __ z_cl(vtable_idx, in_bytes(Klass::vtable_length_offset()), rcvr_klass);
 104     __ z_brl(L);
 105     __ z_lghi(Z_ARG3, vtable_index);  // Debug code, don't optimize.
 106     __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), Z_ARG1, Z_ARG3, false);
 107     // Count unused bytes (assume worst case here).
 108     slop_bytes += 12;
 109     __ bind(L);
 110   }
 111 #endif
 112 
 113   int entry_offset = in_bytes(Klass::vtable_start_offset()) +
 114                      vtable_index * vtableEntry::size_in_bytes();
 115   int v_off        = entry_offset + vtableEntry::method_offset_in_bytes();
 116 
 117   // Set method (in case of interpreted method), and destination address.
 118   // Duplicate safety code from enc_class Java_Dynamic_Call_dynTOC.
 119   if (Displacement::is_validDisp(v_off)) {
 120     __ z_lg(Z_method/*method oop*/, v_off, rcvr_klass/*class oop*/);
 121     // Account for the load_const in the else path.
 122     slop_delta  = __ load_const_size();
 123   } else {
 124     // Worse case, offset does not fit in displacement field.
 125     //               worst case             actual size
 126     slop_delta  = __ load_const_size() - __ load_const_optimized_rtn_len(Z_method, v_off, true);
 127     __ z_lg(Z_method/*method oop*/, 0, Z_method/*method offset*/, rcvr_klass/*class oop*/);
 128   }
 129   slop_bytes += slop_delta;
 130 
 131 #ifndef PRODUCT
 132   if (DebugVtables) {
 133     NearLabel L;
 134     __ z_ltgr(Z_method, Z_method);
 135     __ z_brne(L);
 136     __ stop("Vtable entry is ZERO", 102);
 137     __ bind(L);
 138   }
 139 #endif
 140 
 141   // Must do an explicit check if offset too large or implicit checks are disabled.
 142   address ame_addr = __ pc();
 143   __ null_check(Z_method, Z_R1_scratch, in_bytes(Method::from_compiled_offset()));
 144   __ z_lg(Z_R1_scratch, in_bytes(Method::from_compiled_offset()), Z_method);
 145   __ z_br(Z_R1_scratch);
 146 
 147   masm->flush();
 148   bookkeeping(masm, tty, s, npe_addr, ame_addr, true, vtable_index, slop_bytes, 0);
 149 
 150   return s;
 151 }
 152 
 153 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
 154   // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
 155   const int stub_code_length = code_size_limit(false);
 156   VtableStub* s = new(stub_code_length) VtableStub(false, itable_index);
 157   // Can be NULL if there is no free space in the code cache.
 158   if (s == NULL) {
 159     return NULL;
 160   }
 161   // Count unused bytes in instruction sequences of variable size.
 162   // We add them to the computed buffer size in order to avoid
 163   // overflow in subsequently generated stubs.
 164   address   start_pc;
 165   int       slop_bytes = 0;
 166   int       slop_delta = 0;
 167 
 168   ResourceMark    rm;
 169   CodeBuffer      cb(s->entry_point(), stub_code_length);
 170   MacroAssembler* masm = new MacroAssembler(&cb);
 171 
 172 #if (!defined(PRODUCT) && defined(COMPILER2))
 173   if (CountCompiledCalls) {
 174     //               worst case             actual size
 175     slop_delta  = __ load_const_size() - __ load_const_optimized_rtn_len(Z_R1_scratch, (long)SharedRuntime::nof_megamorphic_calls_addr(), true);
 176     slop_bytes += slop_delta;
 177     assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
 178     // Use generic emitter for direct memory increment.
 179     // Abuse Z_method as scratch register for generic emitter.
 180     // It is loaded further down anyway before it is first used.
 181     // No dynamic code size variance here, increment is 1, always.
 182     __ add2mem_32(Address(Z_R1_scratch), 1, Z_method);
 183   }
 184 #endif
 185 
 186   assert(VtableStub::receiver_location() == Z_R2->as_VMReg(), "receiver expected in Z_ARG1");
 187 
 188   // Entry arguments:
 189   //  Z_method: Interface
 190   //  Z_ARG1:   Receiver
 191   NearLabel no_such_interface;
 192   const Register rcvr_klass = Z_tmp_1,
 193                  interface  = Z_tmp_2;
 194 
 195   // Get receiver klass.
 196   // Must do an explicit check if offset too large or implicit checks are disabled.
 197   address npe_addr = __ pc(); // npe == NULL ptr exception
 198   __ null_check(Z_ARG1, Z_R1_scratch, oopDesc::klass_offset_in_bytes());
 199   __ load_klass(rcvr_klass, Z_ARG1);
 200 
 201   // Receiver subtype check against REFC.
 202   __ z_lg(interface, Address(Z_method, CompiledICHolder::holder_klass_offset()));
 203   __ lookup_interface_method(rcvr_klass, interface, noreg,
 204                              noreg, Z_R1, no_such_interface, /*return_method=*/ false);
 205 
 206   // Get Method* and entrypoint for compiler
 207   __ z_lg(interface, Address(Z_method, CompiledICHolder::holder_metadata_offset()));
 208   __ lookup_interface_method(rcvr_klass, interface, itable_index,
 209                              Z_method, Z_R1, no_such_interface, /*return_method=*/ true);
 210 
 211 #ifndef PRODUCT
 212   if (DebugVtables) {
 213     NearLabel ok1;
 214     __ z_ltgr(Z_method, Z_method);
 215     __ z_brne(ok1);
 216     __ stop("method is null", 103);
 217     __ bind(ok1);
 218   }
 219 #endif
 220 
 221   address ame_addr = __ pc();
 222   // Must do an explicit check if implicit checks are disabled.
 223   if (!ImplicitNullChecks) {
 224     __ compare64_and_branch(Z_method, (intptr_t) 0, Assembler::bcondEqual, no_such_interface);
 225   }
 226   __ z_lg(Z_R1_scratch, in_bytes(Method::from_compiled_offset()), Z_method);
 227   __ z_br(Z_R1_scratch);
 228 
 229   // Handle IncompatibleClassChangeError in itable stubs.
 230   __ bind(no_such_interface);
 231   // more detailed IncompatibleClassChangeError
 232   // we force re-resolving of the call site by jumping to
 233   // the "handle wrong method" stub, thus letting the
 234   // interpreter runtime do all the dirty work.
 235   //               worst case          actual size
 236   slop_delta  = __ load_const_size() - __ load_const_optimized_rtn_len(Z_R1_scratch, (long)SharedRuntime::get_handle_wrong_method_stub(), true);
 237   slop_bytes += slop_delta;
 238   assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
 239   __ z_br(Z_R1_scratch);
 240 
 241   masm->flush();
 242   bookkeeping(masm, tty, s, npe_addr, ame_addr, false, itable_index, slop_bytes, 0);
 243 
 244   return s;
 245 }
 246 
 247 int VtableStub::pd_code_alignment() {
 248   // System z cache line size is 256 bytes, but octoword-alignment is quite ok.
 249   const unsigned int icache_line_size = 32;
 250   return icache_line_size;
 251 }