1 /*
  2  * Copyright (c) 1997, 2023, 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 "interpreter/interpreter.hpp"
 27 #include "interpreter/interpreterRuntime.hpp"
 28 #include "interpreter/interp_masm.hpp"
 29 #include "interpreter/templateInterpreter.hpp"
 30 #include "interpreter/templateInterpreterGenerator.hpp"
 31 #include "interpreter/templateTable.hpp"
 32 #include "logging/log.hpp"
 33 #include "memory/resourceArea.hpp"
 34 #include "prims/jvmtiExport.hpp"
 35 #include "runtime/safepoint.hpp"
 36 #include "runtime/timerTrace.hpp"
 37 #include "utilities/checkedCast.hpp"
 38 #include "utilities/copy.hpp"
 39 
 40 # define __ _masm->
 41 
 42 void TemplateInterpreter::initialize_stub() {
 43   // assertions
 44   assert(_code == nullptr, "must only initialize once");
 45   assert((int)Bytecodes::number_of_codes <= (int)DispatchTable::length,
 46          "dispatch table too small");
 47 
 48   // allocate interpreter
 49   int code_size = InterpreterCodeSize;
 50   NOT_PRODUCT(code_size *= 4;)  // debug uses extra interpreter code space
 51   // 270+ interpreter codelets are generated and each of them is aligned to HeapWordSize,
 52   // plus their code section is aligned to CodeEntryAlignement. So we need additional size due to alignment.
 53   int max_aligned_codelets = 280;
 54   int max_aligned_bytes = checked_cast<int>(max_aligned_codelets * (HeapWordSize + CodeEntryAlignment));
 55   _code = new StubQueue(new InterpreterCodeletInterface, code_size + max_aligned_bytes, nullptr,
 56                         "Interpreter");
 57 }
 58 
 59 void TemplateInterpreter::initialize_code() {
 60   AbstractInterpreter::initialize();
 61 
 62   TemplateTable::initialize();
 63 
 64   // generate interpreter
 65   { ResourceMark rm;
 66     TraceTime timer("Interpreter generation", TRACETIME_LOG(Info, startuptime));
 67     TemplateInterpreterGenerator g;
 68     // Free the unused memory not occupied by the interpreter and the stubs
 69     _code->deallocate_unused_tail();
 70   }
 71 
 72   if (PrintInterpreter) {
 73     ResourceMark rm;
 74     print();
 75   }
 76 
 77   // initialize dispatch table
 78   _active_table = _normal_table;
 79 }
 80 
 81 //------------------------------------------------------------------------------------------------------------------------
 82 // Implementation of EntryPoint
 83 
 84 EntryPoint::EntryPoint() {
 85   assert(number_of_states == 10, "check the code below");
 86   _entry[btos] = nullptr;
 87   _entry[ztos] = nullptr;
 88   _entry[ctos] = nullptr;
 89   _entry[stos] = nullptr;
 90   _entry[atos] = nullptr;
 91   _entry[itos] = nullptr;
 92   _entry[ltos] = nullptr;
 93   _entry[ftos] = nullptr;
 94   _entry[dtos] = nullptr;
 95   _entry[vtos] = nullptr;
 96 }
 97 
 98 
 99 EntryPoint::EntryPoint(address bentry, address zentry, address centry, address sentry, address aentry, address ientry, address lentry, address fentry, address dentry, address ventry) {
100   assert(number_of_states == 10, "check the code below");
101   _entry[btos] = bentry;
102   _entry[ztos] = zentry;
103   _entry[ctos] = centry;
104   _entry[stos] = sentry;
105   _entry[atos] = aentry;
106   _entry[itos] = ientry;
107   _entry[ltos] = lentry;
108   _entry[ftos] = fentry;
109   _entry[dtos] = dentry;
110   _entry[vtos] = ventry;
111 }
112 
113 EntryPoint::EntryPoint(address aentry, address ientry, address lentry, address fentry, address dentry, address ventry) {
114   assert(number_of_states == 10, "check the code below");
115   _entry[btos] = ientry;
116   _entry[ztos] = ientry;
117   _entry[ctos] = ientry;
118   _entry[stos] = ientry;
119   _entry[atos] = aentry;
120   _entry[itos] = ientry;
121   _entry[ltos] = lentry;
122   _entry[ftos] = fentry;
123   _entry[dtos] = dentry;
124   _entry[vtos] = ventry;
125 }
126 
127 void EntryPoint::set_entry(TosState state, address entry) {
128   assert(0 <= state && state < number_of_states, "state out of bounds");
129   _entry[state] = entry;
130 }
131 
132 
133 address EntryPoint::entry(TosState state) const {
134   assert(0 <= state && state < number_of_states, "state out of bounds");
135   return _entry[state];
136 }
137 
138 
139 void EntryPoint::print() {
140   tty->print("[");
141   for (int i = 0; i < number_of_states; i++) {
142     if (i > 0) tty->print(", ");
143     tty->print(INTPTR_FORMAT, p2i(_entry[i]));
144   }
145   tty->print("]");
146 }
147 
148 
149 bool EntryPoint::operator == (const EntryPoint& y) {
150   int i = number_of_states;
151   while (i-- > 0) {
152     if (_entry[i] != y._entry[i]) return false;
153   }
154   return true;
155 }
156 
157 
158 //------------------------------------------------------------------------------------------------------------------------
159 // Implementation of DispatchTable
160 
161 EntryPoint DispatchTable::entry(int i) const {
162   assert(0 <= i && i < length, "index out of bounds");
163   return
164     EntryPoint(
165       _table[btos][i],
166       _table[ztos][i],
167       _table[ctos][i],
168       _table[stos][i],
169       _table[atos][i],
170       _table[itos][i],
171       _table[ltos][i],
172       _table[ftos][i],
173       _table[dtos][i],
174       _table[vtos][i]
175     );
176 }
177 
178 
179 void DispatchTable::set_entry(int i, EntryPoint& entry) {
180   assert(0 <= i && i < length, "index out of bounds");
181   assert(number_of_states == 10, "check the code below");
182   _table[btos][i] = entry.entry(btos);
183   _table[ztos][i] = entry.entry(ztos);
184   _table[ctos][i] = entry.entry(ctos);
185   _table[stos][i] = entry.entry(stos);
186   _table[atos][i] = entry.entry(atos);
187   _table[itos][i] = entry.entry(itos);
188   _table[ltos][i] = entry.entry(ltos);
189   _table[ftos][i] = entry.entry(ftos);
190   _table[dtos][i] = entry.entry(dtos);
191   _table[vtos][i] = entry.entry(vtos);
192 }
193 
194 
195 bool DispatchTable::operator == (DispatchTable& y) {
196   int i = length;
197   while (i-- > 0) {
198     EntryPoint t = y.entry(i); // for compiler compatibility (BugId 4150096)
199     if (!(entry(i) == t)) return false;
200   }
201   return true;
202 }
203 
204 address    TemplateInterpreter::_remove_activation_entry                    = nullptr;
205 address    TemplateInterpreter::_remove_activation_preserving_args_entry    = nullptr;
206 
207 
208 address    TemplateInterpreter::_throw_ArrayIndexOutOfBoundsException_entry = nullptr;
209 address    TemplateInterpreter::_throw_ArrayStoreException_entry            = nullptr;
210 address    TemplateInterpreter::_throw_ArithmeticException_entry            = nullptr;
211 address    TemplateInterpreter::_throw_ClassCastException_entry             = nullptr;
212 address    TemplateInterpreter::_throw_NullPointerException_entry           = nullptr;
213 address    TemplateInterpreter::_throw_StackOverflowError_entry             = nullptr;
214 address    TemplateInterpreter::_throw_exception_entry                      = nullptr;
215 address    TemplateInterpreter::_cont_preempt_rerun_interpreter_adapter     = nullptr;
216 
217 #ifndef PRODUCT
218 EntryPoint TemplateInterpreter::_trace_code;
219 #endif // !PRODUCT
220 EntryPoint TemplateInterpreter::_return_entry[TemplateInterpreter::number_of_return_entries];
221 EntryPoint TemplateInterpreter::_earlyret_entry;
222 EntryPoint TemplateInterpreter::_deopt_entry [TemplateInterpreter::number_of_deopt_entries ];
223 address    TemplateInterpreter::_deopt_reexecute_return_entry;
224 EntryPoint TemplateInterpreter::_safept_entry;
225 
226 address TemplateInterpreter::_invoke_return_entry[TemplateInterpreter::number_of_return_addrs];
227 address TemplateInterpreter::_invokeinterface_return_entry[TemplateInterpreter::number_of_return_addrs];
228 address TemplateInterpreter::_invokedynamic_return_entry[TemplateInterpreter::number_of_return_addrs];
229 
230 DispatchTable TemplateInterpreter::_active_table;
231 DispatchTable TemplateInterpreter::_normal_table;
232 DispatchTable TemplateInterpreter::_safept_table;
233 address    TemplateInterpreter::_wentry_point[DispatchTable::length];
234 
235 
236 //------------------------------------------------------------------------------------------------------------------------
237 // Entry points
238 
239 /**
240  * Returns the return entry table for the given invoke bytecode.
241  */
242 address* TemplateInterpreter::invoke_return_entry_table_for(Bytecodes::Code code) {
243   switch (code) {
244   case Bytecodes::_invokestatic:
245   case Bytecodes::_invokespecial:
246   case Bytecodes::_invokevirtual:
247   case Bytecodes::_invokehandle:
248   case Bytecodes::_fast_invokevfinal:
249     return Interpreter::invoke_return_entry_table();
250   case Bytecodes::_invokeinterface:
251     return Interpreter::invokeinterface_return_entry_table();
252   case Bytecodes::_invokedynamic:
253     return Interpreter::invokedynamic_return_entry_table();
254   default:
255     fatal("invalid bytecode: %s", Bytecodes::name(code));
256     return nullptr;
257   }
258 }
259 
260 /**
261  * Returns the return entry address for the given top-of-stack state and bytecode.
262  */
263 address TemplateInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
264   guarantee(0 <= length && length < Interpreter::number_of_return_entries, "illegal length");
265   const int index = TosState_as_index(state);
266   switch (code) {
267   case Bytecodes::_invokestatic:
268   case Bytecodes::_invokespecial:
269   case Bytecodes::_invokevirtual:
270   case Bytecodes::_invokehandle:
271     return _invoke_return_entry[index];
272   case Bytecodes::_invokeinterface:
273     return _invokeinterface_return_entry[index];
274   case Bytecodes::_invokedynamic:
275     return _invokedynamic_return_entry[index];
276   default:
277     assert(!Bytecodes::is_invoke(code), "invoke instructions should be handled separately: %s", Bytecodes::name(code));
278     address entry = _return_entry[length].entry(state);
279     vmassert(entry != nullptr, "unsupported return entry requested, length=%d state=%d", length, index);
280     return entry;
281   }
282 }
283 
284 
285 address TemplateInterpreter::deopt_entry(TosState state, int length) {
286   guarantee(0 <= length && length < Interpreter::number_of_deopt_entries, "illegal length");
287   address entry = _deopt_entry[length].entry(state);
288   vmassert(entry != nullptr, "unsupported deopt entry requested, length=%d state=%d", length, TosState_as_index(state));
289   return entry;
290 }
291 
292 //------------------------------------------------------------------------------------------------------------------------
293 // Support for invokes
294 
295 int TemplateInterpreter::TosState_as_index(TosState state) {
296   assert( state < number_of_states , "Invalid state in TosState_as_index");
297   assert(0 <= (int)state && (int)state < TemplateInterpreter::number_of_return_addrs, "index out of bounds");
298   return (int)state;
299 }
300 
301 
302 //------------------------------------------------------------------------------------------------------------------------
303 // Safepoint support
304 
305 static inline void copy_table(address* from, address* to, int size) {
306   // Copy non-overlapping tables.
307   if (SafepointSynchronize::is_at_safepoint()) {
308     // Nothing is using the table at a safepoint so skip atomic word copy.
309     Copy::disjoint_words((HeapWord*)from, (HeapWord*)to, (size_t)size);
310   } else {
311     // Use atomic word copy when not at a safepoint for safety.
312     Copy::disjoint_words_atomic((HeapWord*)from, (HeapWord*)to, (size_t)size);
313   }
314 }
315 
316 void TemplateInterpreter::notice_safepoints() {
317   if (!_notice_safepoints) {
318     log_debug(interpreter, safepoint)("switching active_table to safept_table.");
319     // switch to safepoint dispatch table
320     _notice_safepoints = true;
321     copy_table((address*)&_safept_table, (address*)&_active_table, sizeof(_active_table) / sizeof(address));
322   } else {
323     log_debug(interpreter, safepoint)("active_table is already safept_table; "
324                                       "notice_safepoints() call is no-op.");
325   }
326 }
327 
328 // switch from the dispatch table which notices safepoints back to the
329 // normal dispatch table.  So that we can notice single stepping points,
330 // keep the safepoint dispatch table if we are single stepping in JVMTI.
331 // Note that the should_post_single_step test is exactly as fast as the
332 // JvmtiExport::_enabled test and covers both cases.
333 void TemplateInterpreter::ignore_safepoints() {
334   if (_notice_safepoints) {
335     if (!JvmtiExport::should_post_single_step()) {
336       log_debug(interpreter, safepoint)("switching active_table to normal_table.");
337       // switch to normal dispatch table
338       _notice_safepoints = false;
339       copy_table((address*)&_normal_table, (address*)&_active_table, sizeof(_active_table) / sizeof(address));
340     } else {
341       log_debug(interpreter, safepoint)("single stepping is still active; "
342                                         "ignoring ignore_safepoints() call.");
343     }
344   } else {
345     log_debug(interpreter, safepoint)("active_table is already normal_table; "
346                                       "ignore_safepoints() call is no-op.");
347   }
348 }
349 
350 //------------------------------------------------------------------------------------------------------------------------
351 // Deoptimization support
352 
353 // If deoptimization happens, this function returns the point of next bytecode to continue execution
354 address TemplateInterpreter::deopt_continue_after_entry(Method* method, address bcp, int callee_parameters, bool is_top_frame) {
355   return AbstractInterpreter::deopt_continue_after_entry(method, bcp, callee_parameters, is_top_frame);
356 }
357 
358 // If deoptimization happens, this function returns the point where the interpreter reexecutes
359 // the bytecode.
360 // Note: Bytecodes::_athrow (C1 only) and Bytecodes::_return are the special cases
361 //       that do not return "Interpreter::deopt_entry(vtos, 0)"
362 address TemplateInterpreter::deopt_reexecute_entry(Method* method, address bcp) {
363   assert(method->contains(bcp), "just checkin'");
364   Bytecodes::Code code   = Bytecodes::code_at(method, bcp);
365   if (code == Bytecodes::_return_register_finalizer) {
366     // This is used for deopt during registration of finalizers
367     // during Object.<init>.  We simply need to resume execution at
368     // the standard return vtos bytecode to pop the frame normally.
369     // reexecuting the real bytecode would cause double registration
370     // of the finalizable object.
371     return Interpreter::deopt_reexecute_return_entry();
372   } else {
373     return AbstractInterpreter::deopt_reexecute_entry(method, bcp);
374   }
375 }
376 
377 // If deoptimization happens, the interpreter should reexecute this bytecode.
378 // This function mainly helps the compilers to set up the reexecute bit.
379 bool TemplateInterpreter::bytecode_should_reexecute(Bytecodes::Code code) {
380   if (code == Bytecodes::_return) {
381     //Yes, we consider Bytecodes::_return as a special case of reexecution
382     return true;
383   } else {
384     return AbstractInterpreter::bytecode_should_reexecute(code);
385   }
386 }
387 
388 InterpreterCodelet* TemplateInterpreter::codelet_containing(address pc) {
389   return (InterpreterCodelet*)_code->stub_containing(pc);
390 }