1 /* 2 * Copyright (c) 1997, 2020, 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/compiledICHolder.hpp" 31 #include "oops/instanceKlass.hpp" 32 #include "oops/klassVtable.hpp" 33 #include "runtime/sharedRuntime.hpp" 34 #include "vmreg_x86.inline.hpp" 35 #ifdef COMPILER2 36 #include "opto/runtime.hpp" 37 #endif 38 39 // machine-dependent part of VtableStubs: create VtableStub of correct size and 40 // initialize its code 41 42 #define __ masm-> 43 44 #ifndef PRODUCT 45 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index); 46 #endif 47 48 // These stubs are used by the compiler only. 49 // Argument registers, which must be preserved: 50 // rcx - receiver (always first argument) 51 // rdx - second argument (if any) 52 // Other registers that might be usable: 53 // rax - inline cache register (is interface for itable stub) 54 // rbx - method (used when calling out to interpreter) 55 // Available now, but may become callee-save at some point: 56 // rsi, rdi 57 // Note that rax and rdx are also used for return values. 58 59 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) { 60 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing. 61 const int stub_code_length = code_size_limit(true); 62 VtableStub* s = new(stub_code_length) VtableStub(true, vtable_index); 63 // Can be NULL if there is no free space in the code cache. 64 if (s == NULL) { 65 return NULL; 66 } 67 68 // Count unused bytes in instruction sequences of variable size. 69 // We add them to the computed buffer size in order to avoid 70 // overflow in subsequently generated stubs. 71 address start_pc; 72 int slop_bytes = 0; 73 int slop_delta = 0; 74 // No variance was detected in vtable stub sizes. Setting index_dependent_slop == 0 will unveil any deviation from this observation. 75 const int index_dependent_slop = 0; 76 77 ResourceMark rm; 78 CodeBuffer cb(s->entry_point(), stub_code_length); 79 MacroAssembler* masm = new MacroAssembler(&cb); 80 81 #if (!defined(PRODUCT) && defined(COMPILER2)) 82 if (CountCompiledCalls) { 83 __ incrementl(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr())); 84 } 85 #endif 86 87 // get receiver (need to skip return address on top of stack) 88 assert(VtableStub::receiver_location() == rcx->as_VMReg(), "receiver expected in rcx"); 89 90 // get receiver klass 91 address npe_addr = __ pc(); 92 __ movptr(rax, Address(rcx, oopDesc::klass_offset_in_bytes())); 93 94 #ifndef PRODUCT 95 if (DebugVtables) { 96 Label L; 97 start_pc = __ pc(); 98 // check offset vs vtable length 99 __ cmpl(Address(rax, Klass::vtable_length_offset()), vtable_index*vtableEntry::size()); 100 slop_delta = 6 - (__ pc() - start_pc); // cmpl varies in length, depending on data 101 slop_bytes += slop_delta; 102 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta); 103 104 __ jcc(Assembler::greater, L); 105 __ movl(rbx, vtable_index); 106 // VTABLE TODO: find upper bound for call_VM length. 107 start_pc = __ pc(); 108 __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), rcx, rbx); 109 slop_delta = 500 - (__ pc() - start_pc); 110 slop_bytes += slop_delta; 111 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta); 112 __ bind(L); 113 } 114 #endif // PRODUCT 115 116 const Register method = rbx; 117 118 // load Method* and target address 119 start_pc = __ pc(); 120 __ lookup_virtual_method(rax, vtable_index, method); 121 slop_delta = 6 - (int)(__ pc() - start_pc); 122 slop_bytes += slop_delta; 123 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta); 124 125 #ifndef PRODUCT 126 if (DebugVtables) { 127 Label L; 128 __ cmpptr(method, (int32_t)NULL_WORD); 129 __ jcc(Assembler::equal, L); 130 __ cmpptr(Address(method, Method::from_compiled_offset()), (int32_t)NULL_WORD); 131 __ jcc(Assembler::notZero, L); 132 __ stop("Vtable entry is NULL"); 133 __ bind(L); 134 } 135 #endif // PRODUCT 136 137 // rax: receiver klass 138 // method (rbx): Method* 139 // rcx: receiver 140 address ame_addr = __ pc(); 141 __ jmp( Address(method, Method::from_compiled_offset())); 142 143 masm->flush(); 144 slop_bytes += index_dependent_slop; // add'l slop for size variance due to large itable offsets 145 bookkeeping(masm, tty, s, npe_addr, ame_addr, true, vtable_index, slop_bytes, index_dependent_slop); 146 147 return s; 148 } 149 150 151 VtableStub* VtableStubs::create_itable_stub(int itable_index) { 152 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing. 153 const int stub_code_length = code_size_limit(false); 154 VtableStub* s = new(stub_code_length) VtableStub(false, itable_index); 155 // Can be NULL if there is no free space in the code cache. 156 if (s == NULL) { 157 return NULL; 158 } 159 // Count unused bytes in instruction sequences of variable size. 160 // We add them to the computed buffer size in order to avoid 161 // overflow in subsequently generated stubs. 162 address start_pc; 163 int slop_bytes = 0; 164 int slop_delta = 0; 165 const int index_dependent_slop = (itable_index == 0) ? 4 : // code size change with transition from 8-bit to 32-bit constant (@index == 32). 166 (itable_index < 32) ? 3 : 0; // index == 0 generates even shorter code. 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 __ incrementl(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr())); 175 } 176 #endif /* PRODUCT */ 177 178 // Entry arguments: 179 // rax: CompiledICHolder 180 // rcx: Receiver 181 182 // Most registers are in use; we'll use rax, rbx, rsi, rdi 183 // (If we need to make rsi, rdi callee-save, do a push/pop here.) 184 const Register recv_klass_reg = rsi; 185 const Register holder_klass_reg = rax; // declaring interface klass (DECC) 186 const Register resolved_klass_reg = rbx; // resolved interface klass (REFC) 187 const Register temp_reg = rdi; 188 189 const Register icholder_reg = rax; 190 __ movptr(resolved_klass_reg, Address(icholder_reg, CompiledICHolder::holder_klass_offset())); 191 __ movptr(holder_klass_reg, Address(icholder_reg, CompiledICHolder::holder_metadata_offset())); 192 193 Label L_no_such_interface; 194 195 // get receiver klass (also an implicit null-check) 196 assert(VtableStub::receiver_location() == rcx->as_VMReg(), "receiver expected in rcx"); 197 address npe_addr = __ pc(); 198 __ load_klass(recv_klass_reg, rcx); 199 200 start_pc = __ pc(); 201 202 // Receiver subtype check against REFC. 203 // Destroys recv_klass_reg value. 204 __ lookup_interface_method(// inputs: rec. class, interface 205 recv_klass_reg, resolved_klass_reg, noreg, 206 // outputs: scan temp. reg1, scan temp. reg2 207 recv_klass_reg, temp_reg, 208 L_no_such_interface, 209 /*return_method=*/false); 210 211 const ptrdiff_t typecheckSize = __ pc() - start_pc; 212 start_pc = __ pc(); 213 214 // Get selected method from declaring class and itable index 215 const Register method = rbx; 216 __ load_klass(recv_klass_reg, rcx); // restore recv_klass_reg 217 __ lookup_interface_method(// inputs: rec. class, interface, itable index 218 recv_klass_reg, holder_klass_reg, itable_index, 219 // outputs: method, scan temp. reg 220 method, temp_reg, 221 L_no_such_interface); 222 223 const ptrdiff_t lookupSize = __ pc() - start_pc; 224 225 // We expect we need index_dependent_slop extra bytes. Reason: 226 // The emitted code in lookup_interface_method changes when itable_index exceeds 31. 227 // For windows, a narrow estimate was found to be 104. Other OSes not tested. 228 const ptrdiff_t estimate = 104; 229 const ptrdiff_t codesize = typecheckSize + lookupSize + index_dependent_slop; 230 slop_delta = (int)(estimate - codesize); 231 slop_bytes += slop_delta; 232 assert(slop_delta >= 0, "itable #%d: Code size estimate (%d) for lookup_interface_method too small, required: %d", itable_index, (int)estimate, (int)codesize); 233 234 // method (rbx): Method* 235 // rcx: receiver 236 237 #ifdef ASSERT 238 if (DebugVtables) { 239 Label L1; 240 __ cmpptr(method, (int32_t)NULL_WORD); 241 __ jcc(Assembler::equal, L1); 242 __ cmpptr(Address(method, Method::from_compiled_offset()), (int32_t)NULL_WORD); 243 __ jcc(Assembler::notZero, L1); 244 __ stop("Method* is null"); 245 __ bind(L1); 246 } 247 #endif // ASSERT 248 249 address ame_addr = __ pc(); 250 __ jmp(Address(method, Method::from_compiled_offset())); 251 252 __ bind(L_no_such_interface); 253 // Handle IncompatibleClassChangeError in itable stubs. 254 // More detailed error message. 255 // We force resolving of the call site by jumping to the "handle 256 // wrong method" stub, and so let the interpreter runtime do all the 257 // dirty work. 258 __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub())); 259 260 masm->flush(); 261 slop_bytes += index_dependent_slop; // add'l slop for size variance due to large itable offsets 262 bookkeeping(masm, tty, s, npe_addr, ame_addr, false, itable_index, slop_bytes, index_dependent_slop); 263 264 return s; 265 } 266 267 int VtableStub::pd_code_alignment() { 268 // x86 cache line size is 64 bytes, but we want to limit alignment loss. 269 const unsigned int icache_line_size = wordSize; 270 return icache_line_size; 271 }