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