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, 2021, 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 
141     } else {
142       // must be some sort of compiled/runtime frame
143       // fp does not have to be safe (although it could be check for c1?)
144 
145       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
146       if (_cb->frame_size() <= 0) {
147         return false;
148       }
149 
150       sender_sp = _unextended_sp + _cb->frame_size();
151       // Is sender_sp safe?
152       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
153         return false;
154       }
155       sender_unextended_sp = sender_sp;
156       sender_pc = (address) *(sender_sp-1);
157       // Note: frame::sender_sp_offset is only valid for compiled frame
158       saved_fp = (intptr_t*) *(sender_sp - frame::sender_sp_offset);
159     }
160 
161 
162     // If the potential sender is the interpreter then we can do some more checking
163     if (Interpreter::contains(sender_pc)) {
164 
165       // fp is always saved in a recognizable place in any code we generate. However
166       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
167       // is really a frame pointer.
168 
169       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
170         return false;
171       }
172 
173       // construct the potential sender
174 
175       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
176 
177       return sender.is_interpreted_frame_valid(thread);
178 
179     }
180 
181     // We must always be able to find a recognizable pc
182     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
183     if (sender_pc == NULL ||  sender_blob == NULL) {
184       return false;
185     }
186 
187     // Could be a zombie method
188     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
189       return false;
190     }
191 
192     // Could just be some random pointer within the codeBlob
193     if (!sender_blob->code_contains(sender_pc)) {
194       return false;
195     }
196 
197     // We should never be able to see an adapter if the current frame is something from code cache
198     if (sender_blob->is_adapter_blob()) {
199       return false;
200     }
201 
202     // Could be the call_stub
203     if (StubRoutines::returns_to_call_stub(sender_pc)) {
204       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
205         return false;
206       }
207 
208       // construct the potential sender
209 
210       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
211 
212       // Validate the JavaCallWrapper an entry frame must have
213       address jcw = (address)sender.entry_frame_call_wrapper();
214 
215       bool jcw_safe = (jcw < thread->stack_base()) && (jcw > (address)sender.fp());
216 
217       return jcw_safe;
218     }
219 
220     CompiledMethod* nm = sender_blob->as_compiled_method_or_null();
221     if (nm != NULL) {
222       if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
223           nm->method()->is_method_handle_intrinsic()) {
224         return false;
225       }
226     }
227 
228     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
229     // because the return address counts against the callee's frame.
230 
231     if (sender_blob->frame_size() <= 0) {
232       assert(!sender_blob->is_compiled(), "should count return address at least");
233       return false;
234     }
235 
236     // We should never be able to see anything here except an nmethod. If something in the
237     // code cache (current frame) is called by an entity within the code cache that entity
238     // should not be anything but the call stub (already covered), the interpreter (already covered)
239     // or an nmethod.
240 
241     if (!sender_blob->is_compiled()) {
242         return false;
243     }
244 
245     // Could put some more validation for the potential non-interpreted sender
246     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
247 
248     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
249 
250     // We've validated the potential sender that would be created
251     return true;
252   }
253 
254   // Must be native-compiled frame. Since sender will try and use fp to find
255   // linkages it must be safe
256 
257   if (!fp_safe) {
258     return false;
259   }
260 
261   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
262 
263   if ((address) this->fp()[c_frame_return_addr_offset] == NULL) { return false; }
264 
265   return true;
266 }
267 
268 void frame::patch_pc(Thread* thread, address pc) {
269   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
270   address* pc_addr = &(((address*) sp())[-1]);
271   if (TracePcPatching) {
272     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
273                   p2i(pc_addr), p2i(*pc_addr), p2i(pc));
274   }
275   // Either the return address is the original one or we are going to
276   // patch in the same address that's already there.
277   assert(_pc == *pc_addr || pc == *pc_addr, "must be");
278   *pc_addr = pc;
279   address original_pc = CompiledMethod::get_deopt_original_pc(this);
280   if (original_pc != NULL) {
281     assert(original_pc == _pc, "expected original PC to be stored before patching");
282     _deopt_state = is_deoptimized;
283     // leave _pc as is
284   } else {
285     _deopt_state = not_deoptimized;
286     _pc = pc;
287   }
288 }
289 
290 bool frame::is_interpreted_frame() const  {
291   return Interpreter::contains(pc());
292 }
293 
294 int frame::frame_size(RegisterMap* map) const {
295   frame sender = this->sender(map);
296   return sender.sp() - sp();
297 }
298 
299 intptr_t* frame::entry_frame_argument_at(int offset) const {
300   // convert offset to index to deal with tsi
301   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
302   // Entry frame's arguments are always in relation to unextended_sp()
303   return &unextended_sp()[index];
304 }
305 
306 // sender_sp
307 intptr_t* frame::interpreter_frame_sender_sp() const {
308   assert(is_interpreted_frame(), "interpreted frame expected");
309   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
310 }
311 
312 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
313   assert(is_interpreted_frame(), "interpreted frame expected");
314   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
315 }
316 
317 
318 // monitor elements
319 
320 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
321   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
322 }
323 
324 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
325   BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
326   // make sure the pointer points inside the frame
327   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
328   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer");
329   return result;
330 }
331 
332 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
333   *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
334 }
335 
336 // Used by template based interpreter deoptimization
337 void frame::interpreter_frame_set_last_sp(intptr_t* last_sp) {
338   *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = last_sp;
339 }
340 
341 frame frame::sender_for_entry_frame(RegisterMap* map) const {
342   assert(map != NULL, "map must be set");
343   // Java frame called from C; skip all C frames and return top C
344   // frame of that chunk as the sender
345   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
346   assert(!entry_frame_is_first(), "next Java fp must be non zero");
347   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
348   // Since we are walking the stack now this nested anchor is obviously walkable
349   // even if it wasn't when it was stacked.
350   if (!jfa->walkable()) {
351     // Capture _last_Java_pc (if needed) and mark anchor walkable.
352     jfa->capture_last_Java_pc();
353   }
354   map->clear();
355   assert(map->include_argument_oops(), "should be set by clear");
356   vmassert(jfa->last_Java_pc() != NULL, "not walkable");
357   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
358   return fr;
359 }
360 
361 OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {
362   ShouldNotCallThis();
363   return nullptr;
364 }
365 
366 bool frame::optimized_entry_frame_is_first() const {
367   ShouldNotCallThis();
368   return false;
369 }
370 
371 frame frame::sender_for_optimized_entry_frame(RegisterMap* map) const {
372   ShouldNotCallThis();
373   return {};
374 }
375 
376 //------------------------------------------------------------------------------
377 // frame::verify_deopt_original_pc
378 //
379 // Verifies the calculated original PC of a deoptimization PC for the
380 // given unextended SP.
381 #ifdef ASSERT
382 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
383   frame fr;
384 
385   // This is ugly but it's better than to change {get,set}_original_pc
386   // to take an SP value as argument.  And it's only a debugging
387   // method anyway.
388   fr._unextended_sp = unextended_sp;
389 
390   assert_cond(nm != NULL);
391   address original_pc = nm->get_original_pc(&fr);
392   assert(nm->insts_contains_inclusive(original_pc),
393          "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
394 }
395 #endif
396 
397 //------------------------------------------------------------------------------
398 // frame::adjust_unextended_sp
399 void frame::adjust_unextended_sp() {
400   // On riscv64, sites calling method handle intrinsics and lambda forms are treated
401   // as any other call site. Therefore, no special action is needed when we are
402   // returning to any of these call sites.
403 
404   if (_cb != NULL) {
405     CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
406     if (sender_cm != NULL) {
407       // If the sender PC is a deoptimization point, get the original PC.
408       if (sender_cm->is_deopt_entry(_pc) ||
409           sender_cm->is_deopt_mh_entry(_pc)) {
410         DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));
411       }
412     }
413   }
414 }
415 
416 //------------------------------------------------------------------------------
417 // frame::update_map_with_saved_link
418 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
419   // The interpreter and compiler(s) always save fp in a known
420   // location on entry. We must record where that location is
421   // so that if fp was live on callout from c2 we can find
422   // the saved copy no matter what it called.
423 
424   // Since the interpreter always saves fp if we record where it is then
425   // we don't have to always save fp on entry and exit to c2 compiled
426   // code, on entry will be enough.
427   assert(map != NULL, "map must be set");
428   map->set_location(::fp->as_VMReg(), (address) link_addr);
429   // this is weird "H" ought to be at a higher address however the
430   // oopMaps seems to have the "H" regs at the same address and the
431   // vanilla register.
432   map->set_location(::fp->as_VMReg()->next(), (address) link_addr);
433 }
434 
435 
436 //------------------------------------------------------------------------------
437 // frame::sender_for_interpreter_frame
438 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
439   // SP is the raw SP from the sender after adapter or interpreter
440   // extension.
441   intptr_t* sender_sp = this->sender_sp();
442 
443   // This is the sp before any possible extension (adapter/locals).
444   intptr_t* unextended_sp = interpreter_frame_sender_sp();
445 
446 #ifdef COMPILER2
447   assert(map != NULL, "map must be set");
448   if (map->update_map()) {
449     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
450   }
451 #endif // COMPILER2
452 
453   return frame(sender_sp, unextended_sp, link(), sender_pc());
454 }
455 
456 
457 //------------------------------------------------------------------------------
458 // frame::sender_for_compiled_frame
459 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
460   // we cannot rely upon the last fp having been saved to the thread
461   // in C2 code but it will have been pushed onto the stack. so we
462   // have to find it relative to the unextended sp
463 
464   assert(_cb->frame_size() >= 0, "must have non-zero frame size");
465   intptr_t* l_sender_sp = unextended_sp() + _cb->frame_size();
466   intptr_t* unextended_sp = l_sender_sp;
467 
468   // the return_address is always the word on the stack
469   address sender_pc = (address) *(l_sender_sp-1);
470 
471   intptr_t** saved_fp_addr = (intptr_t**) (l_sender_sp - frame::sender_sp_offset);
472 
473   assert(map != NULL, "map must be set");
474   if (map->update_map()) {
475     // Tell GC to use argument oopmaps for some runtime stubs that need it.
476     // For C1, the runtime stub might not have oop maps, so set this flag
477     // outside of update_register_map.
478     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
479     if (_cb->oop_maps() != NULL) {
480       OopMapSet::update_register_map(this, map);
481     }
482 
483     // Since the prolog does the save and restore of FP there is no
484     // oopmap for it so we must fill in its location as if there was
485     // an oopmap entry since if our caller was compiled code there
486     // could be live jvm state in it.
487     update_map_with_saved_link(map, saved_fp_addr);
488   }
489 
490   return frame(l_sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
491 }
492 
493 //------------------------------------------------------------------------------
494 // frame::sender_raw
495 frame frame::sender_raw(RegisterMap* map) const {
496   // Default is we done have to follow them. The sender_for_xxx will
497   // update it accordingly
498   assert(map != NULL, "map must be set");
499   map->set_include_argument_oops(false);
500 
501   if (is_entry_frame()) {
502     return sender_for_entry_frame(map);
503   }
504   if (is_interpreted_frame()) {
505     return sender_for_interpreter_frame(map);
506   }
507   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
508 
509   // This test looks odd: why is it not is_compiled_frame() ?  That's
510   // because stubs also have OOP maps.
511   if (_cb != NULL) {
512     return sender_for_compiled_frame(map);
513   }
514 
515   // Must be native-compiled frame, i.e. the marshaling code for native
516   // methods that exists in the core system.
517   return frame(sender_sp(), link(), sender_pc());
518 }
519 
520 frame frame::sender(RegisterMap* map) const {
521   frame result = sender_raw(map);
522 
523   if (map->process_frames()) {
524     StackWatermarkSet::on_iteration(map->thread(), result);
525   }
526 
527   return result;
528 }
529 
530 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
531   assert(is_interpreted_frame(), "Not an interpreted frame");
532   // These are reasonable sanity checks
533   if (fp() == NULL || (intptr_t(fp()) & (wordSize-1)) != 0) {
534     return false;
535   }
536   if (sp() == NULL || (intptr_t(sp()) & (wordSize-1)) != 0) {
537     return false;
538   }
539   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
540     return false;
541   }
542   // These are hacks to keep us out of trouble.
543   // The problem with these is that they mask other problems
544   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
545     return false;
546   }
547 
548   // do some validation of frame elements
549 
550   // first the method
551 
552   Method* m = *interpreter_frame_method_addr();
553 
554   // validate the method we'd find in this potential sender
555   if (!Method::is_valid_method(m)) {
556     return false;
557   }
558   // stack frames shouldn't be much larger than max_stack elements
559   // this test requires the use of unextended_sp which is the sp as seen by
560   // the current frame, and not sp which is the "raw" pc which could point
561   // further because of local variables of the callee method inserted after
562   // method arguments
563   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
564     return false;
565   }
566 
567   // validate bci/bcx
568   address  bcp    = interpreter_frame_bcp();
569   if (m->validate_bci_from_bcp(bcp) < 0) {
570     return false;
571   }
572 
573   // validate constantPoolCache*
574   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
575   if (MetaspaceObj::is_valid(cp) == false) {
576     return false;
577   }
578   // validate locals
579   address locals =  (address) *interpreter_frame_locals_addr();
580 
581   if (locals > thread->stack_base() || locals < (address) fp()) {
582     return false;
583   }
584   // We'd have to be pretty unlucky to be mislead at this point
585   return true;
586 }
587 
588 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
589   assert(is_interpreted_frame(), "interpreted frame expected");
590   Method* method = interpreter_frame_method();
591   BasicType type = method->result_type();
592 
593   intptr_t* tos_addr = NULL;
594   if (method->is_native()) {
595     tos_addr = (intptr_t*)sp();
596     if (type == T_FLOAT || type == T_DOUBLE) {
597       // This is because we do a push(ltos) after push(dtos) in generate_native_entry.
598       tos_addr += 2 * Interpreter::stackElementWords;
599     }
600   } else {
601     tos_addr = (intptr_t*)interpreter_frame_tos_address();
602   }
603 
604   switch (type) {
605     case T_OBJECT  :
606     case T_ARRAY   : {
607       oop obj;
608       if (method->is_native()) {
609         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
610       } else {
611         oop* obj_p = (oop*)tos_addr;
612         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
613       }
614       assert(Universe::is_in_heap_or_null(obj), "sanity check");
615       *oop_result = obj;
616       break;
617     }
618     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
619     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
620     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
621     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
622     case T_INT     : value_result->i = *(jint*)tos_addr; break;
623     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
624     case T_FLOAT   : {
625         value_result->f = *(jfloat*)tos_addr;
626       break;
627     }
628     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
629     case T_VOID    : /* Nothing to do */ break;
630     default        : ShouldNotReachHere();
631   }
632 
633   return type;
634 }
635 
636 
637 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
638   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
639   return &interpreter_frame_tos_address()[index];
640 }
641 
642 #ifndef PRODUCT
643 
644 #define DESCRIBE_FP_OFFSET(name) \
645   values.describe(frame_no, fp() + frame::name##_offset, #name)
646 
647 void frame::describe_pd(FrameValues& values, int frame_no) {
648   if (is_interpreted_frame()) {
649     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
650     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
651     DESCRIBE_FP_OFFSET(interpreter_frame_method);
652     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
653     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
654     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
655     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
656     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
657     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
658   }
659 }
660 #endif
661 
662 intptr_t *frame::initial_deoptimization_info() {
663   // Not used on riscv64, but we must return something.
664   return NULL;
665 }
666 
667 intptr_t* frame::real_fp() const {
668   if (_cb != NULL) {
669     // use the frame size if valid
670     int size = _cb->frame_size();
671     if (size > 0) {
672       return unextended_sp() + size;
673     }
674   }
675   // else rely on fp()
676   assert(!is_compiled_frame(), "unknown compiled frame size");
677   return fp();
678 }
679 
680 #undef DESCRIBE_FP_OFFSET
681 
682 #ifndef PRODUCT
683 // This is a generic constructor which is only used by pns() in debug.cpp.
684 frame::frame(void* ptr_sp, void* ptr_fp, void* pc) {
685   init((intptr_t*)ptr_sp, (intptr_t*)ptr_fp, (address)pc);
686 }
687 
688 #endif
689 
690 void JavaFrameAnchor::make_walkable(JavaThread* thread) {
691   // last frame set?
692   if (last_Java_sp() == NULL) { return; }
693   // already walkable?
694   if (walkable()) { return; }
695   vmassert(Thread::current() == (Thread*)thread, "not current thread");
696   vmassert(last_Java_sp() != NULL, "not called from Java code?");
697   vmassert(last_Java_pc() == NULL, "already walkable");
698   capture_last_Java_pc();
699   vmassert(walkable(), "something went wrong");
700 }
701 
702 void JavaFrameAnchor::capture_last_Java_pc() {
703   vmassert(_last_Java_sp != NULL, "no last frame set");
704   vmassert(_last_Java_pc == NULL, "already walkable");
705   _last_Java_pc = (address)_last_Java_sp[-1];
706 }