Path: blob/master/src/hotspot/cpu/arm/frame_arm.cpp
64440 views
/*1* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "compiler/oopMap.hpp"26#include "interpreter/interpreter.hpp"27#include "memory/resourceArea.hpp"28#include "memory/universe.hpp"29#include "oops/markWord.hpp"30#include "oops/method.hpp"31#include "oops/oop.inline.hpp"32#include "runtime/frame.inline.hpp"33#include "runtime/handles.inline.hpp"34#include "runtime/javaCalls.hpp"35#include "runtime/monitorChunk.hpp"36#include "runtime/os.inline.hpp"37#include "runtime/signature.hpp"38#include "runtime/stubCodeGenerator.hpp"39#include "runtime/stubRoutines.hpp"40#include "vmreg_arm.inline.hpp"41#ifdef COMPILER142#include "c1/c1_Runtime1.hpp"43#include "runtime/vframeArray.hpp"44#endif45#include "prims/methodHandles.hpp"4647#ifdef ASSERT48void RegisterMap::check_location_valid() {49}50#endif515253// Profiling/safepoint support5455bool frame::safe_for_sender(JavaThread *thread) {56address sp = (address)_sp;57address fp = (address)_fp;58address unextended_sp = (address)_unextended_sp;5960// consider stack guards when trying to determine "safe" stack pointers61// sp must be within the usable part of the stack (not in guards)62if (!thread->is_in_usable_stack(sp)) {63return false;64}6566if (!thread->is_in_stack_range_incl(unextended_sp, sp)) {67return false;68}6970// We know sp/unextended_sp are safe. Only fp is questionable here.7172bool fp_safe = thread->is_in_stack_range_incl(fp, sp);7374if (_cb != NULL ) {7576// First check if frame is complete and tester is reliable77// Unfortunately we can only check frame complete for runtime stubs and nmethod78// other generic buffer blobs are more problematic so we just assume they are79// ok. adapter blobs never have a frame complete and are never ok.8081if (!_cb->is_frame_complete_at(_pc)) {82if (_cb->is_compiled() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {83return false;84}85}8687// Could just be some random pointer within the codeBlob88if (!_cb->code_contains(_pc)) {89return false;90}9192// Entry frame checks93if (is_entry_frame()) {94// an entry frame must have a valid fp.95return fp_safe && is_entry_frame_valid(thread);96}9798intptr_t* sender_sp = NULL;99address sender_pc = NULL;100101if (is_interpreted_frame()) {102// fp must be safe103if (!fp_safe) {104return false;105}106107sender_pc = (address) this->fp()[return_addr_offset];108sender_sp = (intptr_t*) addr_at(sender_sp_offset);109110} else {111// must be some sort of compiled/runtime frame112// fp does not have to be safe (although it could be check for c1?)113114sender_sp = _unextended_sp + _cb->frame_size();115// Is sender_sp safe?116if (!thread->is_in_full_stack_checked((address)sender_sp)) {117return false;118}119// With our calling conventions, the return_address should120// end up being the word on the stack121sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset);122}123124// We must always be able to find a recognizable pc125CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);126if (sender_pc == NULL || sender_blob == NULL) {127return false;128}129130131// If the potential sender is the interpreter then we can do some more checking132if (Interpreter::contains(sender_pc)) {133134// FP is always saved in a recognizable place in any code we generate. However135// only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved FP136// is really a frame pointer.137138intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset);139if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {140return false;141}142143// construct the potential sender144145frame sender(sender_sp, saved_fp, sender_pc);146147return sender.is_interpreted_frame_valid(thread);148}149150if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {151return false;152}153154// Could just be some random pointer within the codeBlob155if (!sender_blob->code_contains(sender_pc)) {156return false;157}158159// We should never be able to see an adapter if the current frame is something from code cache160if (sender_blob->is_adapter_blob()) {161return false;162}163164// Could be the call_stub165if (StubRoutines::returns_to_call_stub(sender_pc)) {166intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset);167if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {168return false;169}170171// construct the potential sender172173frame sender(sender_sp, saved_fp, sender_pc);174175// Validate the JavaCallWrapper an entry frame must have176address jcw = (address)sender.entry_frame_call_wrapper();177178return thread->is_in_stack_range_excl(jcw, (address)sender.fp());179}180181// If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size182// because the return address counts against the callee's frame.183184if (sender_blob->frame_size() <= 0) {185assert(!sender_blob->is_compiled(), "should count return address at least");186return false;187}188189// We should never be able to see anything here except an nmethod. If something in the190// code cache (current frame) is called by an entity within the code cache that entity191// should not be anything but the call stub (already covered), the interpreter (already covered)192// or an nmethod.193194if (!sender_blob->is_compiled()) {195return false;196}197198// Could put some more validation for the potential non-interpreted sender199// frame we'd create by calling sender if I could think of any. Wait for next crash in forte...200201// One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb202203// We've validated the potential sender that would be created204return true;205}206207// Must be native-compiled frame. Since sender will try and use fp to find208// linkages it must be safe209210if (!fp_safe) {211return false;212}213214// Will the pc we fetch be non-zero (which we'll find at the oldest frame)215216if ((address) this->fp()[return_addr_offset] == NULL) return false;217218219// could try and do some more potential verification of native frame if we could think of some...220221return true;222}223224225void frame::patch_pc(Thread* thread, address pc) {226assert(_cb == CodeCache::find_blob(pc), "unexpected pc");227address* pc_addr = &((address *)sp())[-sender_sp_offset+return_addr_offset];228if (TracePcPatching) {229tty->print_cr("patch_pc at address" INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "] ",230p2i(pc_addr), p2i(*pc_addr), p2i(pc));231}232*pc_addr = pc;233address original_pc = CompiledMethod::get_deopt_original_pc(this);234if (original_pc != NULL) {235assert(original_pc == _pc, "expected original PC to be stored before patching");236_deopt_state = is_deoptimized;237// leave _pc as is238} else {239_deopt_state = not_deoptimized;240_pc = pc;241}242}243244bool frame::is_interpreted_frame() const {245return Interpreter::contains(pc());246}247248int frame::frame_size(RegisterMap* map) const {249frame sender = this->sender(map);250return sender.sp() - sp();251}252253intptr_t* frame::entry_frame_argument_at(int offset) const {254assert(is_entry_frame(), "entry frame expected");255// convert offset to index to deal with tsi256int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);257// Entry frame's arguments are always in relation to unextended_sp()258return &unextended_sp()[index];259}260261// sender_sp262intptr_t* frame::interpreter_frame_sender_sp() const {263assert(is_interpreted_frame(), "interpreted frame expected");264return (intptr_t*) at(interpreter_frame_sender_sp_offset);265}266267void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {268assert(is_interpreted_frame(), "interpreted frame expected");269ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);270}271272273// monitor elements274275BasicObjectLock* frame::interpreter_frame_monitor_begin() const {276return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);277}278279BasicObjectLock* frame::interpreter_frame_monitor_end() const {280BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);281// make sure the pointer points inside the frame282assert((intptr_t) fp() > (intptr_t) result, "result must < than frame pointer");283assert((intptr_t) sp() <= (intptr_t) result, "result must >= than stack pointer");284return result;285}286287void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {288*((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;289}290291292// Used by template based interpreter deoptimization293void frame::interpreter_frame_set_last_sp(intptr_t* sp) {294*((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;295}296297298frame frame::sender_for_entry_frame(RegisterMap* map) const {299assert(map != NULL, "map must be set");300// Java frame called from C; skip all C frames and return top C301// frame of that chunk as the sender302JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();303assert(!entry_frame_is_first(), "next Java fp must be non zero");304assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");305map->clear();306assert(map->include_argument_oops(), "should be set by clear");307if (jfa->last_Java_pc() != NULL) {308frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());309return fr;310}311frame fr(jfa->last_Java_sp(), jfa->last_Java_fp());312return fr;313}314315OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {316ShouldNotCallThis();317return nullptr;318}319320bool frame::optimized_entry_frame_is_first() const {321ShouldNotCallThis();322return false;323}324325//------------------------------------------------------------------------------326// frame::verify_deopt_original_pc327//328// Verifies the calculated original PC of a deoptimization PC for the329// given unextended SP. The unextended SP might also be the saved SP330// for MethodHandle call sites.331#ifdef ASSERT332void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp, bool is_method_handle_return) {333frame fr;334335// This is ugly but it's better than to change {get,set}_original_pc336// to take an SP value as argument. And it's only a debugging337// method anyway.338fr._unextended_sp = unextended_sp;339340address original_pc = nm->get_original_pc(&fr);341assert(nm->insts_contains_inclusive(original_pc),342"original PC must be in the main code section of the the compiled method (or must be immediately following it)");343assert(nm->is_method_handle_return(original_pc) == is_method_handle_return, "must be");344}345#endif346347//------------------------------------------------------------------------------348// frame::adjust_unextended_sp349void frame::adjust_unextended_sp() {350// same as on x86351352// If we are returning to a compiled MethodHandle call site, the353// saved_fp will in fact be a saved value of the unextended SP. The354// simplest way to tell whether we are returning to such a call site355// is as follows:356357CompiledMethod* sender_cm = (_cb == NULL) ? NULL : _cb->as_compiled_method_or_null();358if (sender_cm != NULL) {359// If the sender PC is a deoptimization point, get the original360// PC. For MethodHandle call site the unextended_sp is stored in361// saved_fp.362if (sender_cm->is_deopt_mh_entry(_pc)) {363DEBUG_ONLY(verify_deopt_mh_original_pc(sender_cm, _fp));364_unextended_sp = _fp;365}366else if (sender_cm->is_deopt_entry(_pc)) {367DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));368}369else if (sender_cm->is_method_handle_return(_pc)) {370_unextended_sp = _fp;371}372}373}374375//------------------------------------------------------------------------------376// frame::update_map_with_saved_link377void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {378// see x86 for comments379map->set_location(FP->as_VMReg(), (address) link_addr);380}381382frame frame::sender_for_interpreter_frame(RegisterMap* map) const {383// SP is the raw SP from the sender after adapter or interpreter384// extension.385intptr_t* sender_sp = this->sender_sp();386387// This is the sp before any possible extension (adapter/locals).388intptr_t* unextended_sp = interpreter_frame_sender_sp();389390#ifdef COMPILER2391if (map->update_map()) {392update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));393}394#endif // COMPILER2395396return frame(sender_sp, unextended_sp, link(), sender_pc());397}398399frame frame::sender_for_compiled_frame(RegisterMap* map) const {400assert(map != NULL, "map must be set");401402// frame owned by optimizing compiler403assert(_cb->frame_size() >= 0, "must have non-zero frame size");404intptr_t* sender_sp = unextended_sp() + _cb->frame_size();405intptr_t* unextended_sp = sender_sp;406407address sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset);408409// This is the saved value of FP which may or may not really be an FP.410// It is only an FP if the sender is an interpreter frame (or C1?).411intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - sender_sp_offset + link_offset);412413if (map->update_map()) {414// Tell GC to use argument oopmaps for some runtime stubs that need it.415// For C1, the runtime stub might not have oop maps, so set this flag416// outside of update_register_map.417map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));418if (_cb->oop_maps() != NULL) {419OopMapSet::update_register_map(this, map);420}421422// Since the prolog does the save and restore of FP there is no oopmap423// for it so we must fill in its location as if there was an oopmap entry424// since if our caller was compiled code there could be live jvm state in it.425update_map_with_saved_link(map, saved_fp_addr);426}427428assert(sender_sp != sp(), "must have changed");429return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc);430}431432frame frame::sender(RegisterMap* map) const {433// Default is we done have to follow them. The sender_for_xxx will434// update it accordingly435map->set_include_argument_oops(false);436437if (is_entry_frame()) return sender_for_entry_frame(map);438if (is_interpreted_frame()) return sender_for_interpreter_frame(map);439assert(_cb == CodeCache::find_blob(pc()),"Must be the same");440441if (_cb != NULL) {442return sender_for_compiled_frame(map);443}444445assert(false, "should not be called for a C frame");446return frame();447}448449bool frame::is_interpreted_frame_valid(JavaThread* thread) const {450assert(is_interpreted_frame(), "Not an interpreted frame");451// These are reasonable sanity checks452if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {453return false;454}455if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {456return false;457}458if (fp() + interpreter_frame_initial_sp_offset < sp()) {459return false;460}461// These are hacks to keep us out of trouble.462// The problem with these is that they mask other problems463if (fp() <= sp()) { // this attempts to deal with unsigned comparison above464return false;465}466// do some validation of frame elements467468// first the method469470Method* m = *interpreter_frame_method_addr();471472// validate the method we'd find in this potential sender473if (!Method::is_valid_method(m)) return false;474475// stack frames shouldn't be much larger than max_stack elements476477if (fp() - sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {478return false;479}480481// validate bci/bcp482483address bcp = interpreter_frame_bcp();484if (m->validate_bci_from_bcp(bcp) < 0) {485return false;486}487488// validate ConstantPoolCache*489ConstantPoolCache* cp = *interpreter_frame_cache_addr();490if (MetaspaceObj::is_valid(cp) == false) return false;491492// validate locals493494address locals = (address) *interpreter_frame_locals_addr();495return thread->is_in_stack_range_incl(locals, (address)fp());496}497498BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {499assert(is_interpreted_frame(), "interpreted frame expected");500Method* method = interpreter_frame_method();501BasicType type = method->result_type();502503intptr_t* res_addr;504if (method->is_native()) {505// Prior to calling into the runtime to report the method_exit both of506// the possible return value registers are saved.507// Return value registers are pushed to the native stack508res_addr = (intptr_t*)sp();509#ifdef __ABI_HARD__510// FP result is pushed onto a stack along with integer result registers511if (type == T_FLOAT || type == T_DOUBLE) {512res_addr += 2;513}514#endif // __ABI_HARD__515} else {516res_addr = (intptr_t*)interpreter_frame_tos_address();517}518519switch (type) {520case T_OBJECT :521case T_ARRAY : {522oop obj;523if (method->is_native()) {524obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));525} else {526obj = *(oop*)res_addr;527}528assert(Universe::is_in_heap_or_null(obj), "sanity check");529*oop_result = obj;530break;531}532case T_BOOLEAN : value_result->z = *(jboolean*)res_addr; break;533case T_BYTE : value_result->b = *(jbyte*)res_addr; break;534case T_CHAR : value_result->c = *(jchar*)res_addr; break;535case T_SHORT : value_result->s = *(jshort*)res_addr; break;536case T_INT : value_result->i = *(jint*)res_addr; break;537case T_LONG : value_result->j = *(jlong*)res_addr; break;538case T_FLOAT : value_result->f = *(jfloat*)res_addr; break;539case T_DOUBLE : value_result->d = *(jdouble*)res_addr; break;540case T_VOID : /* Nothing to do */ break;541default : ShouldNotReachHere();542}543544return type;545}546547548intptr_t* frame::interpreter_frame_tos_at(jint offset) const {549int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);550return &interpreter_frame_tos_address()[index];551}552553#ifndef PRODUCT554555#define DESCRIBE_FP_OFFSET(name) \556values.describe(frame_no, fp() + frame::name##_offset, #name)557558void frame::describe_pd(FrameValues& values, int frame_no) {559if (is_interpreted_frame()) {560DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);561DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);562DESCRIBE_FP_OFFSET(interpreter_frame_method);563DESCRIBE_FP_OFFSET(interpreter_frame_mdp);564DESCRIBE_FP_OFFSET(interpreter_frame_cache);565DESCRIBE_FP_OFFSET(interpreter_frame_locals);566DESCRIBE_FP_OFFSET(interpreter_frame_bcp);567DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);568}569}570571// This is a generic constructor which is only used by pns() in debug.cpp.572frame::frame(void* sp, void* fp, void* pc) {573init((intptr_t*)sp, (intptr_t*)fp, (address)pc);574}575576void frame::pd_ps() {}577#endif578579intptr_t *frame::initial_deoptimization_info() {580// used to reset the saved FP581return fp();582}583584intptr_t* frame::real_fp() const {585if (is_entry_frame()) {586// Work-around: FP (currently) does not conform to the ABI for entry587// frames (see generate_call_stub). Might be worth fixing as another CR.588// Following code assumes (and asserts) this has not yet been fixed.589assert(frame::entry_frame_call_wrapper_offset == 0, "adjust this code");590intptr_t* new_fp = fp();591new_fp += 5; // saved R0,R1,R2,R4,R10592#ifndef __SOFTFP__593new_fp += 8*2; // saved D8..D15594#endif595return new_fp;596}597if (_cb != NULL) {598// use the frame size if valid599int size = _cb->frame_size();600if (size > 0) {601return unextended_sp() + size;602}603}604// else rely on fp()605assert(! is_compiled_frame(), "unknown compiled frame size");606return fp();607}608609610