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