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