1 /*
  2  * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
  4  * Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #include "precompiled.hpp"
 28 #include "compiler/oopMap.hpp"
 29 #include "interpreter/interpreter.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "memory/universe.hpp"
 32 #include "oops/markWord.hpp"
 33 #include "oops/method.hpp"
 34 #include "oops/oop.inline.hpp"
 35 #include "prims/methodHandles.hpp"
 36 #include "runtime/frame.inline.hpp"
 37 #include "runtime/handles.inline.hpp"
 38 #include "runtime/javaCalls.hpp"
 39 #include "runtime/monitorChunk.hpp"
 40 #include "runtime/os.inline.hpp"
 41 #include "runtime/signature.hpp"
 42 #include "runtime/stackWatermarkSet.hpp"
 43 #include "runtime/stubCodeGenerator.hpp"
 44 #include "runtime/stubRoutines.hpp"
 45 #include "vmreg_riscv.inline.hpp"
 46 #ifdef COMPILER1
 47 #include "c1/c1_Runtime1.hpp"
 48 #include "runtime/vframeArray.hpp"
 49 #endif
 50 
 51 #ifdef ASSERT
 52 void RegisterMap::check_location_valid() {
 53 }
 54 #endif
 55 
 56 
 57 // Profiling/safepoint support
 58 
 59 bool frame::safe_for_sender(JavaThread *thread) {
 60   address   addr_sp = (address)_sp;
 61   address   addr_fp = (address)_fp;
 62   address   unextended_sp = (address)_unextended_sp;
 63 
 64   // consider stack guards when trying to determine "safe" stack pointers
 65   // sp must be within the usable part of the stack (not in guards)
 66   if (!thread->is_in_usable_stack(addr_sp)) {
 67     return false;
 68   }
 69 
 70   // When we are running interpreted code the machine stack pointer, SP, is
 71   // set low enough so that the Java expression stack can grow and shrink
 72   // without ever exceeding the machine stack bounds.  So, ESP >= SP.
 73 
 74   // When we call out of an interpreted method, SP is incremented so that
 75   // the space between SP and ESP is removed.  The SP saved in the callee's
 76   // frame is the SP *before* this increment.  So, when we walk a stack of
 77   // interpreter frames the sender's SP saved in a frame might be less than
 78   // the SP at the point of call.
 79 
 80   // So unextended sp must be within the stack but we need not to check
 81   // that unextended sp >= sp
 82 
 83   if (!thread->is_in_full_stack_checked(unextended_sp)) {
 84     return false;
 85   }
 86 
 87   // an fp must be within the stack and above (but not equal) sp
 88   // second evaluation on fp+ is added to handle situation where fp is -1
 89   bool fp_safe = thread->is_in_stack_range_excl(addr_fp, addr_sp) &&
 90                  thread->is_in_full_stack_checked(addr_fp + (return_addr_offset * sizeof(void*)));
 91 
 92   // We know sp/unextended_sp are safe only fp is questionable here
 93 
 94   // If the current frame is known to the code cache then we can attempt to
 95   // to construct the sender and do some validation of it. This goes a long way
 96   // toward eliminating issues when we get in frame construction code
 97 
 98   if (_cb != NULL) {
 99 
100     // First check if frame is complete and tester is reliable
101     // Unfortunately we can only check frame complete for runtime stubs and nmethod
102     // other generic buffer blobs are more problematic so we just assume they are
103     // ok. adapter blobs never have a frame complete and are never ok.
104 
105     if (!_cb->is_frame_complete_at(_pc)) {
106       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
107         return false;
108       }
109     }
110 
111     // Could just be some random pointer within the codeBlob
112     if (!_cb->code_contains(_pc)) {
113       return false;
114     }
115 
116     // Entry frame checks
117     if (is_entry_frame()) {
118       // an entry frame must have a valid fp.
119       return fp_safe && is_entry_frame_valid(thread);
120     }
121 
122     intptr_t* sender_sp = NULL;
123     intptr_t* sender_unextended_sp = NULL;
124     address   sender_pc = NULL;
125     intptr_t* saved_fp =  NULL;
126 
127     if (is_interpreted_frame()) {
128       // fp must be safe
129       if (!fp_safe) {
130         return false;
131       }
132 
133       sender_pc = (address)this->fp()[return_addr_offset];
134       // for interpreted frames, the value below is the sender "raw" sp,
135       // which can be different from the sender unextended sp (the sp seen
136       // by the sender) because of current frame local variables
137       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
138       sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
139       saved_fp = (intptr_t*) this->fp()[link_offset];
140     } else {
141       // must be some sort of compiled/runtime frame
142       // fp does not have to be safe (although it could be check for c1?)
143 
144       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
145       if (_cb->frame_size() <= 0) {
146         return false;
147       }
148 
149       sender_sp = _unextended_sp + _cb->frame_size();
150       // Is sender_sp safe?
151       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
152         return false;
153       }
154 
155       sender_unextended_sp = sender_sp;
156       sender_pc = (address) *(sender_sp - 1);
157       saved_fp = (intptr_t*) *(sender_sp - 2);
158     }
159 
160 
161     // If the potential sender is the interpreter then we can do some more checking
162     if (Interpreter::contains(sender_pc)) {
163 
164       // fp is always saved in a recognizable place in any code we generate. However
165       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
166       // is really a frame pointer.
167       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
168         return false;
169       }
170 
171       // construct the potential sender
172       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
173 
174       return sender.is_interpreted_frame_valid(thread);
175     }
176 
177     // We must always be able to find a recognizable pc
178     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
179     if (sender_pc == NULL || sender_blob == NULL) {
180       return false;
181     }
182 
183     // Could be a zombie method
184     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
185       return false;
186     }
187 
188     // Could just be some random pointer within the codeBlob
189     if (!sender_blob->code_contains(sender_pc)) {
190       return false;
191     }
192 
193     // We should never be able to see an adapter if the current frame is something from code cache
194     if (sender_blob->is_adapter_blob()) {
195       return false;
196     }
197 
198     // Could be the call_stub
199     if (StubRoutines::returns_to_call_stub(sender_pc)) {
200       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
201         return false;
202       }
203 
204       // construct the potential sender
205       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
206 
207       // Validate the JavaCallWrapper an entry frame must have
208       address jcw = (address)sender.entry_frame_call_wrapper();
209 
210       bool jcw_safe = (jcw < thread->stack_base()) && (jcw > (address)sender.fp());
211 
212       return jcw_safe;
213     }
214 
215     CompiledMethod* nm = sender_blob->as_compiled_method_or_null();
216     if (nm != NULL) {
217       if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
218           nm->method()->is_method_handle_intrinsic()) {
219         return false;
220       }
221     }
222 
223     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
224     // because the return address counts against the callee's frame.
225     if (sender_blob->frame_size() <= 0) {
226       assert(!sender_blob->is_compiled(), "should count return address at least");
227       return false;
228     }
229 
230     // We should never be able to see anything here except an nmethod. If something in the
231     // code cache (current frame) is called by an entity within the code cache that entity
232     // should not be anything but the call stub (already covered), the interpreter (already covered)
233     // or an nmethod.
234     if (!sender_blob->is_compiled()) {
235         return false;
236     }
237 
238     // Could put some more validation for the potential non-interpreted sender
239     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
240 
241     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
242 
243     // We've validated the potential sender that would be created
244     return true;
245   }
246 
247   // Must be native-compiled frame. Since sender will try and use fp to find
248   // linkages it must be safe
249   if (!fp_safe) {
250     return false;
251   }
252 
253   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
254   if ((address)this->fp()[return_addr_offset] == NULL) { return false; }
255 
256   return true;
257 }
258 
259 void frame::patch_pc(Thread* thread, address pc) {
260   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
261   address* pc_addr = &(((address*) sp())[-1]);
262   if (TracePcPatching) {
263     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
264                   p2i(pc_addr), p2i(*pc_addr), p2i(pc));
265   }
266   // Either the return address is the original one or we are going to
267   // patch in the same address that's already there.
268   assert(_pc == *pc_addr || pc == *pc_addr, "must be");
269   *pc_addr = pc;
270   address original_pc = CompiledMethod::get_deopt_original_pc(this);
271   if (original_pc != NULL) {
272     assert(original_pc == _pc, "expected original PC to be stored before patching");
273     _deopt_state = is_deoptimized;
274     // leave _pc as is
275   } else {
276     _deopt_state = not_deoptimized;
277     _pc = pc;
278   }
279 }
280 
281 bool frame::is_interpreted_frame() const  {
282   return Interpreter::contains(pc());
283 }
284 
285 int frame::frame_size(RegisterMap* map) const {
286   frame sender = this->sender(map);
287   return sender.sp() - sp();
288 }
289 
290 intptr_t* frame::entry_frame_argument_at(int offset) const {
291   // convert offset to index to deal with tsi
292   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
293   // Entry frame's arguments are always in relation to unextended_sp()
294   return &unextended_sp()[index];
295 }
296 
297 // sender_sp
298 intptr_t* frame::interpreter_frame_sender_sp() const {
299   assert(is_interpreted_frame(), "interpreted frame expected");
300   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
301 }
302 
303 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
304   assert(is_interpreted_frame(), "interpreted frame expected");
305   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
306 }
307 
308 
309 // monitor elements
310 
311 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
312   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
313 }
314 
315 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
316   BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
317   // make sure the pointer points inside the frame
318   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
319   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer");
320   return result;
321 }
322 
323 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
324   *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
325 }
326 
327 // Used by template based interpreter deoptimization
328 void frame::interpreter_frame_set_last_sp(intptr_t* last_sp) {
329   *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = last_sp;
330 }
331 
332 frame frame::sender_for_entry_frame(RegisterMap* map) const {
333   assert(map != NULL, "map must be set");
334   // Java frame called from C; skip all C frames and return top C
335   // frame of that chunk as the sender
336   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
337   assert(!entry_frame_is_first(), "next Java fp must be non zero");
338   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
339   // Since we are walking the stack now this nested anchor is obviously walkable
340   // even if it wasn't when it was stacked.
341   if (!jfa->walkable()) {
342     // Capture _last_Java_pc (if needed) and mark anchor walkable.
343     jfa->capture_last_Java_pc();
344   }
345   map->clear();
346   assert(map->include_argument_oops(), "should be set by clear");
347   vmassert(jfa->last_Java_pc() != NULL, "not walkable");
348   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
349   return fr;
350 }
351 
352 OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {
353   ShouldNotCallThis();
354   return nullptr;
355 }
356 
357 bool frame::optimized_entry_frame_is_first() const {
358   ShouldNotCallThis();
359   return false;
360 }
361 
362 frame frame::sender_for_optimized_entry_frame(RegisterMap* map) const {
363   ShouldNotCallThis();
364   return {};
365 }
366 
367 //------------------------------------------------------------------------------
368 // frame::verify_deopt_original_pc
369 //
370 // Verifies the calculated original PC of a deoptimization PC for the
371 // given unextended SP.
372 #ifdef ASSERT
373 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
374   frame fr;
375 
376   // This is ugly but it's better than to change {get,set}_original_pc
377   // to take an SP value as argument.  And it's only a debugging
378   // method anyway.
379   fr._unextended_sp = unextended_sp;
380 
381   assert_cond(nm != NULL);
382   address original_pc = nm->get_original_pc(&fr);
383   assert(nm->insts_contains_inclusive(original_pc),
384          "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
385 }
386 #endif
387 
388 //------------------------------------------------------------------------------
389 // frame::adjust_unextended_sp
390 void frame::adjust_unextended_sp() {
391   // On riscv, sites calling method handle intrinsics and lambda forms are treated
392   // as any other call site. Therefore, no special action is needed when we are
393   // returning to any of these call sites.
394 
395   if (_cb != NULL) {
396     CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
397     if (sender_cm != NULL) {
398       // If the sender PC is a deoptimization point, get the original PC.
399       if (sender_cm->is_deopt_entry(_pc) ||
400           sender_cm->is_deopt_mh_entry(_pc)) {
401         DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));
402       }
403     }
404   }
405 }
406 
407 //------------------------------------------------------------------------------
408 // frame::update_map_with_saved_link
409 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
410   // The interpreter and compiler(s) always save fp in a known
411   // location on entry. We must record where that location is
412   // so that if fp was live on callout from c2 we can find
413   // the saved copy no matter what it called.
414 
415   // Since the interpreter always saves fp if we record where it is then
416   // we don't have to always save fp on entry and exit to c2 compiled
417   // code, on entry will be enough.
418   assert(map != NULL, "map must be set");
419   map->set_location(::fp->as_VMReg(), (address) link_addr);
420   // this is weird "H" ought to be at a higher address however the
421   // oopMaps seems to have the "H" regs at the same address and the
422   // vanilla register.
423   map->set_location(::fp->as_VMReg()->next(), (address) link_addr);
424 }
425 
426 
427 //------------------------------------------------------------------------------
428 // frame::sender_for_interpreter_frame
429 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
430   // SP is the raw SP from the sender after adapter or interpreter
431   // extension.
432   intptr_t* sender_sp = this->sender_sp();
433 
434   // This is the sp before any possible extension (adapter/locals).
435   intptr_t* unextended_sp = interpreter_frame_sender_sp();
436 
437 #ifdef COMPILER2
438   assert(map != NULL, "map must be set");
439   if (map->update_map()) {
440     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
441   }
442 #endif // COMPILER2
443 
444   return frame(sender_sp, unextended_sp, link(), sender_pc());
445 }
446 
447 
448 //------------------------------------------------------------------------------
449 // frame::sender_for_compiled_frame
450 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
451   // we cannot rely upon the last fp having been saved to the thread
452   // in C2 code but it will have been pushed onto the stack. so we
453   // have to find it relative to the unextended sp
454 
455   assert(_cb->frame_size() >= 0, "must have non-zero frame size");
456   intptr_t* l_sender_sp = unextended_sp() + _cb->frame_size();
457   intptr_t* unextended_sp = l_sender_sp;
458 
459   // the return_address is always the word on the stack
460   address sender_pc = (address) *(l_sender_sp + frame::return_addr_offset);
461 
462   intptr_t** saved_fp_addr = (intptr_t**) (l_sender_sp + frame::link_offset);
463 
464   assert(map != NULL, "map must be set");
465   if (map->update_map()) {
466     // Tell GC to use argument oopmaps for some runtime stubs that need it.
467     // For C1, the runtime stub might not have oop maps, so set this flag
468     // outside of update_register_map.
469     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
470     if (_cb->oop_maps() != NULL) {
471       OopMapSet::update_register_map(this, map);
472     }
473 
474     // Since the prolog does the save and restore of FP there is no
475     // oopmap for it so we must fill in its location as if there was
476     // an oopmap entry since if our caller was compiled code there
477     // could be live jvm state in it.
478     update_map_with_saved_link(map, saved_fp_addr);
479   }
480 
481   return frame(l_sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
482 }
483 
484 //------------------------------------------------------------------------------
485 // frame::sender_raw
486 frame frame::sender_raw(RegisterMap* map) const {
487   // Default is we done have to follow them. The sender_for_xxx will
488   // update it accordingly
489   assert(map != NULL, "map must be set");
490   map->set_include_argument_oops(false);
491 
492   if (is_entry_frame()) {
493     return sender_for_entry_frame(map);
494   }
495   if (is_interpreted_frame()) {
496     return sender_for_interpreter_frame(map);
497   }
498   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
499 
500   // This test looks odd: why is it not is_compiled_frame() ?  That's
501   // because stubs also have OOP maps.
502   if (_cb != NULL) {
503     return sender_for_compiled_frame(map);
504   }
505 
506   // Must be native-compiled frame, i.e. the marshaling code for native
507   // methods that exists in the core system.
508   return frame(sender_sp(), link(), sender_pc());
509 }
510 
511 frame frame::sender(RegisterMap* map) const {
512   frame result = sender_raw(map);
513 
514   if (map->process_frames()) {
515     StackWatermarkSet::on_iteration(map->thread(), result);
516   }
517 
518   return result;
519 }
520 
521 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
522   assert(is_interpreted_frame(), "Not an interpreted frame");
523   // These are reasonable sanity checks
524   if (fp() == NULL || (intptr_t(fp()) & (wordSize-1)) != 0) {
525     return false;
526   }
527   if (sp() == NULL || (intptr_t(sp()) & (wordSize-1)) != 0) {
528     return false;
529   }
530   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
531     return false;
532   }
533   // These are hacks to keep us out of trouble.
534   // The problem with these is that they mask other problems
535   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
536     return false;
537   }
538 
539   // do some validation of frame elements
540 
541   // first the method
542   Method* m = *interpreter_frame_method_addr();
543   // validate the method we'd find in this potential sender
544   if (!Method::is_valid_method(m)) {
545     return false;
546   }
547 
548   // stack frames shouldn't be much larger than max_stack elements
549   // this test requires the use of unextended_sp which is the sp as seen by
550   // the current frame, and not sp which is the "raw" pc which could point
551   // further because of local variables of the callee method inserted after
552   // method arguments
553   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
554     return false;
555   }
556 
557   // validate bci/bcx
558   address bcp = interpreter_frame_bcp();
559   if (m->validate_bci_from_bcp(bcp) < 0) {
560     return false;
561   }
562 
563   // validate constantPoolCache*
564   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
565   if (MetaspaceObj::is_valid(cp) == false) {
566     return false;
567   }
568 
569   // validate locals
570   address locals = (address) *interpreter_frame_locals_addr();
571   if (locals > thread->stack_base() || locals < (address) fp()) {
572     return false;
573   }
574 
575   // We'd have to be pretty unlucky to be mislead at this point
576   return true;
577 }
578 
579 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
580   assert(is_interpreted_frame(), "interpreted frame expected");
581   Method* method = interpreter_frame_method();
582   BasicType type = method->result_type();
583 
584   intptr_t* tos_addr = NULL;
585   if (method->is_native()) {
586     tos_addr = (intptr_t*)sp();
587     if (type == T_FLOAT || type == T_DOUBLE) {
588       // This is because we do a push(ltos) after push(dtos) in generate_native_entry.
589       tos_addr += 2 * Interpreter::stackElementWords;
590     }
591   } else {
592     tos_addr = (intptr_t*)interpreter_frame_tos_address();
593   }
594 
595   switch (type) {
596     case T_OBJECT  :
597     case T_ARRAY   : {
598       oop obj;
599       if (method->is_native()) {
600         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
601       } else {
602         oop* obj_p = (oop*)tos_addr;
603         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
604       }
605       assert(Universe::is_in_heap_or_null(obj), "sanity check");
606       *oop_result = obj;
607       break;
608     }
609     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
610     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
611     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
612     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
613     case T_INT     : value_result->i = *(jint*)tos_addr; break;
614     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
615     case T_FLOAT   : {
616         value_result->f = *(jfloat*)tos_addr;
617       break;
618     }
619     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
620     case T_VOID    : /* Nothing to do */ break;
621     default        : ShouldNotReachHere();
622   }
623 
624   return type;
625 }
626 
627 
628 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
629   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
630   return &interpreter_frame_tos_address()[index];
631 }
632 
633 #ifndef PRODUCT
634 
635 #define DESCRIBE_FP_OFFSET(name) \
636   values.describe(frame_no, fp() + frame::name##_offset, #name)
637 
638 void frame::describe_pd(FrameValues& values, int frame_no) {
639   if (is_interpreted_frame()) {
640     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
641     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
642     DESCRIBE_FP_OFFSET(interpreter_frame_method);
643     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
644     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
645     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
646     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
647     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
648     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
649   }
650 }
651 #endif
652 
653 intptr_t *frame::initial_deoptimization_info() {
654   // Not used on riscv, but we must return something.
655   return NULL;
656 }
657 
658 intptr_t* frame::real_fp() const {
659   if (_cb != NULL) {
660     // use the frame size if valid
661     int size = _cb->frame_size();
662     if (size > 0) {
663       return unextended_sp() + size;
664     }
665   }
666   // else rely on fp()
667   assert(!is_compiled_frame(), "unknown compiled frame size");
668   return fp();
669 }
670 
671 #undef DESCRIBE_FP_OFFSET
672 
673 #ifndef PRODUCT
674 // This is a generic constructor which is only used by pns() in debug.cpp.
675 frame::frame(void* ptr_sp, void* ptr_fp, void* pc) {
676   init((intptr_t*)ptr_sp, (intptr_t*)ptr_fp, (address)pc);
677 }
678 
679 #endif
680 
681 void JavaFrameAnchor::make_walkable(JavaThread* thread) {
682   // last frame set?
683   if (last_Java_sp() == NULL) { return; }
684   // already walkable?
685   if (walkable()) { return; }
686   vmassert(Thread::current() == (Thread*)thread, "not current thread");
687   vmassert(last_Java_sp() != NULL, "not called from Java code?");
688   vmassert(last_Java_pc() == NULL, "already walkable");
689   capture_last_Java_pc();
690   vmassert(walkable(), "something went wrong");
691 }
692 
693 void JavaFrameAnchor::capture_last_Java_pc() {
694   vmassert(_last_Java_sp != NULL, "no last frame set");
695   vmassert(_last_Java_pc == NULL, "already walkable");
696   _last_Java_pc = (address)_last_Java_sp[-1];
697 }