1 /*
  2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "compiler/oopMap.hpp"
 28 #include "interpreter/interpreter.hpp"
 29 #include "memory/resourceArea.hpp"
 30 #include "memory/universe.hpp"
 31 #include "oops/markWord.hpp"
 32 #include "oops/method.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "prims/methodHandles.hpp"
 35 #include "runtime/frame.inline.hpp"
 36 #include "runtime/handles.inline.hpp"
 37 #include "runtime/javaCalls.hpp"
 38 #include "runtime/monitorChunk.hpp"
 39 #include "runtime/os.inline.hpp"
 40 #include "runtime/signature.hpp"
 41 #include "runtime/stackWatermarkSet.hpp"
 42 #include "runtime/stubCodeGenerator.hpp"
 43 #include "runtime/stubRoutines.hpp"
 44 #include "vmreg_aarch64.inline.hpp"
 45 #ifdef COMPILER1
 46 #include "c1/c1_Runtime1.hpp"
 47 #include "runtime/vframeArray.hpp"
 48 #endif
 49 
 50 #ifdef ASSERT
 51 void RegisterMap::check_location_valid() {
 52 }
 53 #endif
 54 
 55 
 56 // Profiling/safepoint support
 57 
 58 bool frame::safe_for_sender(JavaThread *thread) {
 59   address   sp = (address)_sp;
 60   address   fp = (address)_fp;
 61   address   unextended_sp = (address)_unextended_sp;
 62 
 63   // consider stack guards when trying to determine "safe" stack pointers
 64   // sp must be within the usable part of the stack (not in guards)
 65   if (!thread->is_in_usable_stack(sp)) {
 66     return false;
 67   }
 68 
 69   // When we are running interpreted code the machine stack pointer, SP, is
 70   // set low enough so that the Java expression stack can grow and shrink
 71   // without ever exceeding the machine stack bounds.  So, ESP >= SP.
 72 
 73   // When we call out of an interpreted method, SP is incremented so that
 74   // the space between SP and ESP is removed.  The SP saved in the callee's
 75   // frame is the SP *before* this increment.  So, when we walk a stack of
 76   // interpreter frames the sender's SP saved in a frame might be less than
 77   // the SP at the point of call.
 78 
 79   // So unextended sp must be within the stack but we need not to check
 80   // that unextended sp >= sp
 81   if (!thread->is_in_full_stack_checked(unextended_sp)) {
 82     return false;
 83   }
 84 
 85   // an fp must be within the stack and above (but not equal) sp
 86   // second evaluation on fp+ is added to handle situation where fp is -1
 87   bool fp_safe = thread->is_in_stack_range_excl(fp, sp) &&
 88                  thread->is_in_full_stack_checked(fp + (return_addr_offset * sizeof(void*)));
 89 
 90   // We know sp/unextended_sp are safe only fp is questionable here
 91 
 92   // If the current frame is known to the code cache then we can attempt to
 93   // to construct the sender and do some validation of it. This goes a long way
 94   // toward eliminating issues when we get in frame construction code
 95 
 96   if (_cb != NULL ) {
 97 
 98     // First check if frame is complete and tester is reliable
 99     // Unfortunately we can only check frame complete for runtime stubs and nmethod
100     // other generic buffer blobs are more problematic so we just assume they are
101     // ok. adapter blobs never have a frame complete and are never ok.
102 
103     if (!_cb->is_frame_complete_at(_pc)) {
104       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
105         return false;
106       }
107     }
108 
109     // Could just be some random pointer within the codeBlob
110     if (!_cb->code_contains(_pc)) {
111       return false;
112     }
113 
114     // Entry frame checks
115     if (is_entry_frame()) {
116       // an entry frame must have a valid fp.
117       return fp_safe && is_entry_frame_valid(thread);
118     }
119 
120     intptr_t* sender_sp = NULL;
121     intptr_t* sender_unextended_sp = NULL;
122     address   sender_pc = NULL;
123     intptr_t* saved_fp =  NULL;
124 
125     if (is_interpreted_frame()) {
126       // fp must be safe
127       if (!fp_safe) {
128         return false;
129       }
130 
131       sender_pc = (address) this->fp()[return_addr_offset];
132       // for interpreted frames, the value below is the sender "raw" sp,
133       // which can be different from the sender unextended sp (the sp seen
134       // by the sender) because of current frame local variables
135       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
136       sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
137       saved_fp = (intptr_t*) this->fp()[link_offset];
138 
139     } else {
140       // must be some sort of compiled/runtime frame
141       // fp does not have to be safe (although it could be check for c1?)
142 
143       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
144       if (_cb->frame_size() <= 0) {
145         return false;
146       }
147 
148       sender_sp = _unextended_sp + _cb->frame_size();
149       // Is sender_sp safe?
150       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
151         return false;
152       }

153       sender_pc = (address) *(sender_sp-1);
154       // Note: frame::sender_sp_offset is only valid for compiled frame
155       intptr_t **saved_fp_addr = (intptr_t**) (sender_sp - frame::sender_sp_offset);
156       saved_fp = *saved_fp_addr;
157 
158       // Repair the sender sp if this is a method with scalarized inline type args
159       sender_sp = repair_sender_sp(sender_sp, saved_fp_addr);
160       sender_unextended_sp = sender_sp;
161     }
162 
163     // If the potential sender is the interpreter then we can do some more checking
164     if (Interpreter::contains(sender_pc)) {
165 
166       // fp is always saved in a recognizable place in any code we generate. However
167       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
168       // is really a frame pointer.
169 
170       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
171         return false;
172       }
173 
174       // construct the potential sender
175 
176       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
177 
178       return sender.is_interpreted_frame_valid(thread);
179 
180     }
181 
182     // We must always be able to find a recognizable pc
183     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
184     if (sender_pc == NULL ||  sender_blob == NULL) {
185       return false;
186     }
187 
188     // Could be a zombie method
189     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
190       return false;
191     }
192 
193     // Could just be some random pointer within the codeBlob
194     if (!sender_blob->code_contains(sender_pc)) {
195       return false;
196     }
197 
198     // We should never be able to see an adapter if the current frame is something from code cache
199     if (sender_blob->is_adapter_blob()) {
200       return false;
201     }
202 
203     // Could be the call_stub
204     if (StubRoutines::returns_to_call_stub(sender_pc)) {
205       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
206         return false;
207       }
208 
209       // construct the potential sender
210 
211       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
212 
213       // Validate the JavaCallWrapper an entry frame must have
214       address jcw = (address)sender.entry_frame_call_wrapper();
215 
216       return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
217     }
218 
219     CompiledMethod* nm = sender_blob->as_compiled_method_or_null();
220     if (nm != NULL) {
221       if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
222           nm->method()->is_method_handle_intrinsic()) {
223         return false;
224       }
225     }
226 
227     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
228     // because the return address counts against the callee's frame.
229 
230     if (sender_blob->frame_size() <= 0) {
231       assert(!sender_blob->is_compiled(), "should count return address at least");
232       return false;
233     }
234 
235     // We should never be able to see anything here except an nmethod. If something in the
236     // code cache (current frame) is called by an entity within the code cache that entity
237     // should not be anything but the call stub (already covered), the interpreter (already covered)
238     // or an nmethod.
239 
240     if (!sender_blob->is_compiled()) {
241         return false;
242     }
243 
244     // Could put some more validation for the potential non-interpreted sender
245     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
246 
247     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
248 
249     // We've validated the potential sender that would be created
250     return true;
251   }
252 
253   // Must be native-compiled frame. Since sender will try and use fp to find
254   // linkages it must be safe
255 
256   if (!fp_safe) {
257     return false;
258   }
259 
260   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
261 
262   if ( (address) this->fp()[return_addr_offset] == NULL) return false;
263 
264 
265   // could try and do some more potential verification of native frame if we could think of some...
266 
267   return true;
268 
269 }
270 
271 void frame::patch_pc(Thread* thread, address pc) {
272   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
273   address* pc_addr = &(((address*) sp())[-1]);
274   if (TracePcPatching) {
275     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
276                   p2i(pc_addr), p2i(*pc_addr), p2i(pc));
277   }
278 
279   // Only generated code frames should be patched, therefore the return address will not be signed.
280   assert(pauth_ptr_is_raw(*pc_addr), "cannot be signed");
281   // Either the return address is the original one or we are going to
282   // patch in the same address that's already there.
283   assert(_pc == *pc_addr || pc == *pc_addr, "must be");
284   *pc_addr = pc;
285   address original_pc = CompiledMethod::get_deopt_original_pc(this);
286   if (original_pc != NULL) {
287     assert(original_pc == _pc, "expected original PC to be stored before patching");
288     _deopt_state = is_deoptimized;
289     // leave _pc as is
290   } else {
291     _deopt_state = not_deoptimized;
292     _pc = pc;
293   }
294 }
295 
296 bool frame::is_interpreted_frame() const  {
297   return Interpreter::contains(pc());
298 }
299 
300 int frame::frame_size(RegisterMap* map) const {
301   frame sender = this->sender(map);
302   return sender.sp() - sp();
303 }
304 
305 intptr_t* frame::entry_frame_argument_at(int offset) const {
306   // convert offset to index to deal with tsi
307   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
308   // Entry frame's arguments are always in relation to unextended_sp()
309   return &unextended_sp()[index];
310 }
311 
312 // sender_sp
313 intptr_t* frame::interpreter_frame_sender_sp() const {
314   assert(is_interpreted_frame(), "interpreted frame expected");
315   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
316 }
317 
318 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
319   assert(is_interpreted_frame(), "interpreted frame expected");
320   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
321 }
322 
323 
324 // monitor elements
325 
326 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
327   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
328 }
329 
330 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
331   BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
332   // make sure the pointer points inside the frame
333   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
334   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer");
335   return result;
336 }
337 
338 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
339   *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
340 }
341 
342 // Used by template based interpreter deoptimization
343 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
344     *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
345 }
346 
347 frame frame::sender_for_entry_frame(RegisterMap* map) const {
348   assert(map != NULL, "map must be set");
349   // Java frame called from C; skip all C frames and return top C
350   // frame of that chunk as the sender
351   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
352   assert(!entry_frame_is_first(), "next Java fp must be non zero");
353   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
354   // Since we are walking the stack now this nested anchor is obviously walkable
355   // even if it wasn't when it was stacked.
356   if (!jfa->walkable()) {
357     // Capture _last_Java_pc (if needed) and mark anchor walkable.
358     jfa->capture_last_Java_pc();
359   }
360   map->clear();
361   assert(map->include_argument_oops(), "should be set by clear");
362   vmassert(jfa->last_Java_pc() != NULL, "not walkable");
363   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
364 
365   return fr;
366 }
367 
368 OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {
369   ShouldNotCallThis();
370   return nullptr;
371 }
372 
373 bool frame::optimized_entry_frame_is_first() const {
374   ShouldNotCallThis();
375   return false;
376 }
377 
378 frame frame::sender_for_optimized_entry_frame(RegisterMap* map) const {
379   ShouldNotCallThis();
380   return {};
381 }
382 
383 //------------------------------------------------------------------------------
384 // frame::verify_deopt_original_pc
385 //
386 // Verifies the calculated original PC of a deoptimization PC for the
387 // given unextended SP.
388 #ifdef ASSERT
389 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
390   frame fr;
391 
392   // This is ugly but it's better than to change {get,set}_original_pc
393   // to take an SP value as argument.  And it's only a debugging
394   // method anyway.
395   fr._unextended_sp = unextended_sp;
396 
397   address original_pc = nm->get_original_pc(&fr);
398   assert(nm->insts_contains_inclusive(original_pc),
399          "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
400 }
401 #endif
402 
403 //------------------------------------------------------------------------------
404 // frame::adjust_unextended_sp
405 void frame::adjust_unextended_sp() {
406   // On aarch64, sites calling method handle intrinsics and lambda forms are treated
407   // as any other call site. Therefore, no special action is needed when we are
408   // returning to any of these call sites.
409 
410   if (_cb != NULL) {
411     CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
412     if (sender_cm != NULL) {
413       // If the sender PC is a deoptimization point, get the original PC.
414       if (sender_cm->is_deopt_entry(_pc) ||
415           sender_cm->is_deopt_mh_entry(_pc)) {
416         DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));
417       }
418     }
419   }
420 }
421 
422 //------------------------------------------------------------------------------
423 // frame::update_map_with_saved_link
424 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
425   // The interpreter and compiler(s) always save fp in a known
426   // location on entry. We must record where that location is
427   // so that if fp was live on callout from c2 we can find
428   // the saved copy no matter what it called.
429 
430   // Since the interpreter always saves fp if we record where it is then
431   // we don't have to always save fp on entry and exit to c2 compiled
432   // code, on entry will be enough.
433   map->set_location(rfp->as_VMReg(), (address) link_addr);
434   // this is weird "H" ought to be at a higher address however the
435   // oopMaps seems to have the "H" regs at the same address and the
436   // vanilla register.
437   // XXXX make this go away
438   if (true) {
439     map->set_location(rfp->as_VMReg()->next(), (address) link_addr);
440   }
441 }
442 
443 
444 //------------------------------------------------------------------------------
445 // frame::sender_for_interpreter_frame
446 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
447   // SP is the raw SP from the sender after adapter or interpreter
448   // extension.
449   intptr_t* sender_sp = this->sender_sp();
450 
451   // This is the sp before any possible extension (adapter/locals).
452   intptr_t* unextended_sp = interpreter_frame_sender_sp();
453 
454 #if COMPILER2_OR_JVMCI
455   if (map->update_map()) {
456     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
457   }
458 #endif // COMPILER2_OR_JVMCI
459 
460   // Use the raw version of pc - the interpreter should not have signed it.
461 
462   return frame(sender_sp, unextended_sp, link(), sender_pc_maybe_signed());
463 }
464 
465 
466 //------------------------------------------------------------------------------
467 // frame::sender_for_compiled_frame
468 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
469   // we cannot rely upon the last fp having been saved to the thread
470   // in C2 code but it will have been pushed onto the stack. so we
471   // have to find it relative to the unextended sp
472 
473   assert(_cb->frame_size() >= 0, "must have non-zero frame size");
474   intptr_t* l_sender_sp = unextended_sp() + _cb->frame_size();

475 
476 #ifdef ASSERT
477   address sender_pc_copy = (address) *(l_sender_sp-1);
478 #endif
479 
480   intptr_t** saved_fp_addr = (intptr_t**) (l_sender_sp - frame::sender_sp_offset);
481 
482   // assert (sender_sp() == l_sender_sp, "should be");
483   // assert (*saved_fp_addr == link(), "should be");
484 
485   // Repair the sender sp if the frame has been extended
486   l_sender_sp = repair_sender_sp(l_sender_sp, saved_fp_addr);
487 
488   // The return address is always the first word on the stack
489   address sender_pc = (address) *(l_sender_sp-1);
490 
491 #ifdef ASSERT
492   if (sender_pc != sender_pc_copy) {
493     // When extending the stack in the callee method entry to make room for unpacking of value
494     // type args, we keep a copy of the sender pc at the expected location in the callee frame.
495     // If the sender pc is patched due to deoptimization, the copy is not consistent anymore.
496     nmethod* nm = CodeCache::find_blob(sender_pc)->as_nmethod();
497     assert(sender_pc == nm->deopt_mh_handler_begin() || sender_pc == nm->deopt_handler_begin(), "unexpected sender pc");
498   }
499 #endif
500 
501   if (map->update_map()) {
502     // Tell GC to use argument oopmaps for some runtime stubs that need it.
503     // For C1, the runtime stub might not have oop maps, so set this flag
504     // outside of update_register_map.
505     bool caller_args = _cb->caller_must_gc_arguments(map->thread());
506 #ifdef COMPILER1
507     if (!caller_args) {
508       nmethod* nm = _cb->as_nmethod_or_null();
509       if (nm != NULL && nm->is_compiled_by_c1() && nm->method()->has_scalarized_args() &&
510           pc() < nm->verified_inline_entry_point()) {
511         // The VEP and VIEP(RO) of C1-compiled methods call buffer_inline_args_xxx
512         // before doing any argument shuffling, so we need to scan the oops
513         // as the caller passes them.
514         caller_args = true;
515       }
516     }
517 #endif
518     map->set_include_argument_oops(caller_args);
519     if (_cb->oop_maps() != NULL) {
520       OopMapSet::update_register_map(this, map);
521     }
522 
523     // Since the prolog does the save and restore of FP there is no
524     // oopmap for it so we must fill in its location as if there was
525     // an oopmap entry since if our caller was compiled code there
526     // could be live jvm state in it.
527     update_map_with_saved_link(map, saved_fp_addr);
528   }
529 
530   return frame(l_sender_sp, l_sender_sp, *saved_fp_addr, sender_pc);
531 }
532 
533 //------------------------------------------------------------------------------
534 // frame::sender_raw
535 frame frame::sender_raw(RegisterMap* map) const {
536   // Default is we done have to follow them. The sender_for_xxx will
537   // update it accordingly
538    map->set_include_argument_oops(false);
539 
540   if (is_entry_frame())
541     return sender_for_entry_frame(map);
542   if (is_interpreted_frame())
543     return sender_for_interpreter_frame(map);
544   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
545 
546   // This test looks odd: why is it not is_compiled_frame() ?  That's
547   // because stubs also have OOP maps.
548   if (_cb != NULL) {
549     return sender_for_compiled_frame(map);
550   }
551 
552   // Must be native-compiled frame, i.e. the marshaling code for native
553   // methods that exists in the core system.
554 
555   return frame(sender_sp(), link(), sender_pc());
556 }
557 
558 frame frame::sender(RegisterMap* map) const {
559   frame result = sender_raw(map);
560 
561   if (map->process_frames()) {
562     StackWatermarkSet::on_iteration(map->thread(), result);
563   }
564 
565   return result;
566 }
567 
568 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
569   assert(is_interpreted_frame(), "Not an interpreted frame");
570   // These are reasonable sanity checks
571   if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
572     return false;
573   }
574   if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
575     return false;
576   }
577   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
578     return false;
579   }
580   // These are hacks to keep us out of trouble.
581   // The problem with these is that they mask other problems
582   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
583     return false;
584   }
585 
586   // do some validation of frame elements
587 
588   // first the method
589 
590   Method* m = *interpreter_frame_method_addr();
591 
592   // validate the method we'd find in this potential sender
593   if (!Method::is_valid_method(m)) return false;
594 
595   // stack frames shouldn't be much larger than max_stack elements
596   // this test requires the use of unextended_sp which is the sp as seen by
597   // the current frame, and not sp which is the "raw" pc which could point
598   // further because of local variables of the callee method inserted after
599   // method arguments
600   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
601     return false;
602   }
603 
604   // validate bci/bcx
605 
606   address  bcp    = interpreter_frame_bcp();
607   if (m->validate_bci_from_bcp(bcp) < 0) {
608     return false;
609   }
610 
611   // validate constantPoolCache*
612   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
613   if (MetaspaceObj::is_valid(cp) == false) return false;
614 
615   // validate locals
616 
617   address locals =  (address) *interpreter_frame_locals_addr();
618   return thread->is_in_stack_range_incl(locals, (address)fp());
619 }
620 
621 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
622   assert(is_interpreted_frame(), "interpreted frame expected");
623   Method* method = interpreter_frame_method();
624   BasicType type = method->result_type();
625 
626   intptr_t* tos_addr;
627   if (method->is_native()) {
628     // TODO : ensure AARCH64 does the same as Intel here i.e. push v0 then r0
629     // Prior to calling into the runtime to report the method_exit the possible
630     // return value is pushed to the native stack. If the result is a jfloat/jdouble
631     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
632     tos_addr = (intptr_t*)sp();
633     if (type == T_FLOAT || type == T_DOUBLE) {
634       // This is times two because we do a push(ltos) after pushing XMM0
635       // and that takes two interpreter stack slots.
636       tos_addr += 2 * Interpreter::stackElementWords;
637     }
638   } else {
639     tos_addr = (intptr_t*)interpreter_frame_tos_address();
640   }
641 
642   switch (type) {
643     case T_INLINE_TYPE :
644     case T_OBJECT  :
645     case T_ARRAY   : {
646       oop obj;
647       if (method->is_native()) {
648         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
649       } else {
650         oop* obj_p = (oop*)tos_addr;
651         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
652       }
653       assert(Universe::is_in_heap_or_null(obj), "sanity check");
654       *oop_result = obj;
655       break;
656     }
657     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
658     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
659     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
660     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
661     case T_INT     : value_result->i = *(jint*)tos_addr; break;
662     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
663     case T_FLOAT   : {
664         value_result->f = *(jfloat*)tos_addr;
665       break;
666     }
667     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
668     case T_VOID    : /* Nothing to do */ break;
669     default        : ShouldNotReachHere();
670   }
671 
672   return type;
673 }
674 
675 
676 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
677   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
678   return &interpreter_frame_tos_address()[index];
679 }
680 
681 #ifndef PRODUCT
682 
683 #define DESCRIBE_FP_OFFSET(name) \
684   values.describe(frame_no, fp() + frame::name##_offset, #name)
685 
686 void frame::describe_pd(FrameValues& values, int frame_no) {
687   if (is_interpreted_frame()) {
688     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
689     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
690     DESCRIBE_FP_OFFSET(interpreter_frame_method);
691     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
692     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
693     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
694     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
695     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
696     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
697   }
698 }
699 #endif
700 
701 intptr_t *frame::initial_deoptimization_info() {
702   // Not used on aarch64, but we must return something.
703   return NULL;
704 }
705 
706 intptr_t* frame::real_fp() const {
707   if (_cb != NULL) {
708     // use the frame size if valid
709     int size = _cb->frame_size();
710     if (size > 0) {
711       return unextended_sp() + size;
712     }
713   }
714   // else rely on fp()
715   assert(! is_compiled_frame(), "unknown compiled frame size");
716   return fp();
717 }
718 
719 #undef DESCRIBE_FP_OFFSET
720 
721 #define DESCRIBE_FP_OFFSET(name)                     \
722   {                                                  \
723     uintptr_t *p = (uintptr_t *)fp;                  \
724     printf(INTPTR_FORMAT " " INTPTR_FORMAT " %s\n",  \
725            (uintptr_t)(p + frame::name##_offset),    \
726            p[frame::name##_offset], #name);          \
727   }
728 
729 static THREAD_LOCAL uintptr_t nextfp;
730 static THREAD_LOCAL uintptr_t nextpc;
731 static THREAD_LOCAL uintptr_t nextsp;
732 static THREAD_LOCAL RegisterMap *reg_map;
733 
734 static void printbc(Method *m, intptr_t bcx) {
735   const char *name;
736   char buf[16];
737   if (m->validate_bci_from_bcp((address)bcx) < 0
738       || !m->contains((address)bcx)) {
739     name = "???";
740     snprintf(buf, sizeof buf, "(bad)");
741   } else {
742     int bci = m->bci_from((address)bcx);
743     snprintf(buf, sizeof buf, "%d", bci);
744     name = Bytecodes::name(m->code_at(bci));
745   }
746   ResourceMark rm;
747   printf("%s : %s ==> %s\n", m->name_and_sig_as_C_string(), buf, name);
748 }
749 
750 void internal_pf(uintptr_t sp, uintptr_t fp, uintptr_t pc, uintptr_t bcx) {
751   if (! fp)
752     return;
753 
754   DESCRIBE_FP_OFFSET(return_addr);
755   DESCRIBE_FP_OFFSET(link);
756   DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
757   DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
758   DESCRIBE_FP_OFFSET(interpreter_frame_method);
759   DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
760   DESCRIBE_FP_OFFSET(interpreter_frame_cache);
761   DESCRIBE_FP_OFFSET(interpreter_frame_locals);
762   DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
763   DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
764   uintptr_t *p = (uintptr_t *)fp;
765 
766   // We want to see all frames, native and Java.  For compiled and
767   // interpreted frames we have special information that allows us to
768   // unwind them; for everything else we assume that the native frame
769   // pointer chain is intact.
770   frame this_frame((intptr_t*)sp, (intptr_t*)fp, (address)pc);
771   if (this_frame.is_compiled_frame() ||
772       this_frame.is_interpreted_frame()) {
773     frame sender = this_frame.sender(reg_map);
774     nextfp = (uintptr_t)sender.fp();
775     nextpc = (uintptr_t)sender.pc();
776     nextsp = (uintptr_t)sender.unextended_sp();
777   } else {
778     nextfp = p[frame::link_offset];
779     nextpc = p[frame::return_addr_offset];
780     nextsp = (uintptr_t)&p[frame::sender_sp_offset];
781   }
782 
783   if (bcx == -1ULL)
784     bcx = p[frame::interpreter_frame_bcp_offset];
785 
786   if (Interpreter::contains((address)pc)) {
787     Method* m = (Method*)p[frame::interpreter_frame_method_offset];
788     if(m && m->is_method()) {
789       printbc(m, bcx);
790     } else
791       printf("not a Method\n");
792   } else {
793     CodeBlob *cb = CodeCache::find_blob((address)pc);
794     if (cb != NULL) {
795       if (cb->is_nmethod()) {
796         ResourceMark rm;
797         nmethod* nm = (nmethod*)cb;
798         printf("nmethod %s\n", nm->method()->name_and_sig_as_C_string());
799       } else if (cb->name()) {
800         printf("CodeBlob %s\n", cb->name());
801       }
802     }
803   }
804 }
805 
806 extern "C" void npf() {
807   CodeBlob *cb = CodeCache::find_blob((address)nextpc);
808   // C2 does not always chain the frame pointers when it can, instead
809   // preferring to use fixed offsets from SP, so a simple leave() does
810   // not work.  Instead, it adds the frame size to SP then pops FP and
811   // LR.  We have to do the same thing to get a good call chain.
812   if (cb && cb->frame_size())
813     nextfp = nextsp + wordSize * (cb->frame_size() - 2);
814   internal_pf (nextsp, nextfp, nextpc, -1);
815 }
816 
817 extern "C" void pf(uintptr_t sp, uintptr_t fp, uintptr_t pc,
818                    uintptr_t bcx, uintptr_t thread) {
819   if (!reg_map) {
820     reg_map = NEW_C_HEAP_OBJ(RegisterMap, mtInternal);
821     ::new (reg_map) RegisterMap((JavaThread*)thread, false);
822   } else {
823     *reg_map = RegisterMap((JavaThread*)thread, false);
824   }
825 
826   {
827     CodeBlob *cb = CodeCache::find_blob((address)pc);
828     if (cb && cb->frame_size())
829       fp = sp + wordSize * (cb->frame_size() - 2);
830   }
831   internal_pf(sp, fp, pc, bcx);
832 }
833 
834 // support for printing out where we are in a Java method
835 // needs to be passed current fp and bcp register values
836 // prints method name, bc index and bytecode name
837 extern "C" void pm(uintptr_t fp, uintptr_t bcx) {
838   DESCRIBE_FP_OFFSET(interpreter_frame_method);
839   uintptr_t *p = (uintptr_t *)fp;
840   Method* m = (Method*)p[frame::interpreter_frame_method_offset];
841   printbc(m, bcx);
842 }
843 
844 #ifndef PRODUCT
845 // This is a generic constructor which is only used by pns() in debug.cpp.
846 frame::frame(void* sp, void* fp, void* pc) {
847   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
848 }
849 
850 #endif
851 
852 // Check for a method with scalarized inline type arguments that needs
853 // a stack repair and return the repaired sender stack pointer.
854 intptr_t* frame::repair_sender_sp(intptr_t* sender_sp, intptr_t** saved_fp_addr) const {
855   CompiledMethod* cm = _cb->as_compiled_method_or_null();
856   if (cm != NULL && cm->needs_stack_repair()) {
857     // The stack increment resides just below the saved FP on the stack and
858     // records the total frame size exluding the two words for saving FP and LR.
859     intptr_t* sp_inc_addr = (intptr_t*) (saved_fp_addr - 1);
860     assert(*sp_inc_addr % StackAlignmentInBytes == 0, "sp_inc not aligned");
861     int real_frame_size = (*sp_inc_addr / wordSize) + 2;
862     assert(real_frame_size >= _cb->frame_size() && real_frame_size <= 1000000, "invalid frame size");
863     sender_sp = unextended_sp() + real_frame_size;
864   }
865   return sender_sp;
866 }
867 
868 void JavaFrameAnchor::make_walkable(JavaThread* thread) {
869   // last frame set?
870   if (last_Java_sp() == NULL) return;
871   // already walkable?
872   if (walkable()) return;
873   vmassert(Thread::current() == (Thread*)thread, "not current thread");
874   vmassert(last_Java_sp() != NULL, "not called from Java code?");
875   vmassert(last_Java_pc() == NULL, "already walkable");
876   capture_last_Java_pc();
877   vmassert(walkable(), "something went wrong");
878 }
879 
880 void JavaFrameAnchor::capture_last_Java_pc() {
881   vmassert(_last_Java_sp != NULL, "no last frame set");
882   vmassert(_last_Java_pc == NULL, "already walkable");
883   _last_Java_pc = (address)_last_Java_sp[-1];
884 }
--- EOF ---