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