Path: blob/master/src/hotspot/cpu/arm/frame_arm.cpp
40930 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}314315//------------------------------------------------------------------------------316// frame::verify_deopt_original_pc317//318// Verifies the calculated original PC of a deoptimization PC for the319// given unextended SP. The unextended SP might also be the saved SP320// for MethodHandle call sites.321#ifdef ASSERT322void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp, bool is_method_handle_return) {323frame fr;324325// This is ugly but it's better than to change {get,set}_original_pc326// to take an SP value as argument. And it's only a debugging327// method anyway.328fr._unextended_sp = unextended_sp;329330address original_pc = nm->get_original_pc(&fr);331assert(nm->insts_contains_inclusive(original_pc),332"original PC must be in the main code section of the the compiled method (or must be immediately following it)");333assert(nm->is_method_handle_return(original_pc) == is_method_handle_return, "must be");334}335#endif336337//------------------------------------------------------------------------------338// frame::adjust_unextended_sp339void frame::adjust_unextended_sp() {340// same as on x86341342// If we are returning to a compiled MethodHandle call site, the343// saved_fp will in fact be a saved value of the unextended SP. The344// simplest way to tell whether we are returning to such a call site345// is as follows:346347CompiledMethod* sender_cm = (_cb == NULL) ? NULL : _cb->as_compiled_method_or_null();348if (sender_cm != NULL) {349// If the sender PC is a deoptimization point, get the original350// PC. For MethodHandle call site the unextended_sp is stored in351// saved_fp.352if (sender_cm->is_deopt_mh_entry(_pc)) {353DEBUG_ONLY(verify_deopt_mh_original_pc(sender_cm, _fp));354_unextended_sp = _fp;355}356else if (sender_cm->is_deopt_entry(_pc)) {357DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));358}359else if (sender_cm->is_method_handle_return(_pc)) {360_unextended_sp = _fp;361}362}363}364365//------------------------------------------------------------------------------366// frame::update_map_with_saved_link367void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {368// see x86 for comments369map->set_location(FP->as_VMReg(), (address) link_addr);370}371372frame frame::sender_for_interpreter_frame(RegisterMap* map) const {373// SP is the raw SP from the sender after adapter or interpreter374// extension.375intptr_t* sender_sp = this->sender_sp();376377// This is the sp before any possible extension (adapter/locals).378intptr_t* unextended_sp = interpreter_frame_sender_sp();379380#ifdef COMPILER2381if (map->update_map()) {382update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));383}384#endif // COMPILER2385386return frame(sender_sp, unextended_sp, link(), sender_pc());387}388389frame frame::sender_for_compiled_frame(RegisterMap* map) const {390assert(map != NULL, "map must be set");391392// frame owned by optimizing compiler393assert(_cb->frame_size() >= 0, "must have non-zero frame size");394intptr_t* sender_sp = unextended_sp() + _cb->frame_size();395intptr_t* unextended_sp = sender_sp;396397address sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset);398399// This is the saved value of FP which may or may not really be an FP.400// It is only an FP if the sender is an interpreter frame (or C1?).401intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - sender_sp_offset + link_offset);402403if (map->update_map()) {404// Tell GC to use argument oopmaps for some runtime stubs that need it.405// For C1, the runtime stub might not have oop maps, so set this flag406// outside of update_register_map.407map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));408if (_cb->oop_maps() != NULL) {409OopMapSet::update_register_map(this, map);410}411412// Since the prolog does the save and restore of FP there is no oopmap413// for it so we must fill in its location as if there was an oopmap entry414// since if our caller was compiled code there could be live jvm state in it.415update_map_with_saved_link(map, saved_fp_addr);416}417418assert(sender_sp != sp(), "must have changed");419return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc);420}421422frame frame::sender(RegisterMap* map) const {423// Default is we done have to follow them. The sender_for_xxx will424// update it accordingly425map->set_include_argument_oops(false);426427if (is_entry_frame()) return sender_for_entry_frame(map);428if (is_interpreted_frame()) return sender_for_interpreter_frame(map);429assert(_cb == CodeCache::find_blob(pc()),"Must be the same");430431if (_cb != NULL) {432return sender_for_compiled_frame(map);433}434435assert(false, "should not be called for a C frame");436return frame();437}438439bool frame::is_interpreted_frame_valid(JavaThread* thread) const {440assert(is_interpreted_frame(), "Not an interpreted frame");441// These are reasonable sanity checks442if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {443return false;444}445if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {446return false;447}448if (fp() + interpreter_frame_initial_sp_offset < sp()) {449return false;450}451// These are hacks to keep us out of trouble.452// The problem with these is that they mask other problems453if (fp() <= sp()) { // this attempts to deal with unsigned comparison above454return false;455}456// do some validation of frame elements457458// first the method459460Method* m = *interpreter_frame_method_addr();461462// validate the method we'd find in this potential sender463if (!Method::is_valid_method(m)) return false;464465// stack frames shouldn't be much larger than max_stack elements466467if (fp() - sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {468return false;469}470471// validate bci/bcp472473address bcp = interpreter_frame_bcp();474if (m->validate_bci_from_bcp(bcp) < 0) {475return false;476}477478// validate ConstantPoolCache*479ConstantPoolCache* cp = *interpreter_frame_cache_addr();480if (MetaspaceObj::is_valid(cp) == false) return false;481482// validate locals483484address locals = (address) *interpreter_frame_locals_addr();485return thread->is_in_stack_range_incl(locals, (address)fp());486}487488BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {489assert(is_interpreted_frame(), "interpreted frame expected");490Method* method = interpreter_frame_method();491BasicType type = method->result_type();492493intptr_t* res_addr;494if (method->is_native()) {495// Prior to calling into the runtime to report the method_exit both of496// the possible return value registers are saved.497// Return value registers are pushed to the native stack498res_addr = (intptr_t*)sp();499#ifdef __ABI_HARD__500// FP result is pushed onto a stack along with integer result registers501if (type == T_FLOAT || type == T_DOUBLE) {502res_addr += 2;503}504#endif // __ABI_HARD__505} else {506res_addr = (intptr_t*)interpreter_frame_tos_address();507}508509switch (type) {510case T_OBJECT :511case T_ARRAY : {512oop obj;513if (method->is_native()) {514obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));515} else {516obj = *(oop*)res_addr;517}518assert(Universe::is_in_heap_or_null(obj), "sanity check");519*oop_result = obj;520break;521}522case T_BOOLEAN : value_result->z = *(jboolean*)res_addr; break;523case T_BYTE : value_result->b = *(jbyte*)res_addr; break;524case T_CHAR : value_result->c = *(jchar*)res_addr; break;525case T_SHORT : value_result->s = *(jshort*)res_addr; break;526case T_INT : value_result->i = *(jint*)res_addr; break;527case T_LONG : value_result->j = *(jlong*)res_addr; break;528case T_FLOAT : value_result->f = *(jfloat*)res_addr; break;529case T_DOUBLE : value_result->d = *(jdouble*)res_addr; break;530case T_VOID : /* Nothing to do */ break;531default : ShouldNotReachHere();532}533534return type;535}536537538intptr_t* frame::interpreter_frame_tos_at(jint offset) const {539int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);540return &interpreter_frame_tos_address()[index];541}542543#ifndef PRODUCT544545#define DESCRIBE_FP_OFFSET(name) \546values.describe(frame_no, fp() + frame::name##_offset, #name)547548void frame::describe_pd(FrameValues& values, int frame_no) {549if (is_interpreted_frame()) {550DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);551DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);552DESCRIBE_FP_OFFSET(interpreter_frame_method);553DESCRIBE_FP_OFFSET(interpreter_frame_mdp);554DESCRIBE_FP_OFFSET(interpreter_frame_cache);555DESCRIBE_FP_OFFSET(interpreter_frame_locals);556DESCRIBE_FP_OFFSET(interpreter_frame_bcp);557DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);558}559}560561// This is a generic constructor which is only used by pns() in debug.cpp.562frame::frame(void* sp, void* fp, void* pc) {563init((intptr_t*)sp, (intptr_t*)fp, (address)pc);564}565566void frame::pd_ps() {}567#endif568569intptr_t *frame::initial_deoptimization_info() {570// used to reset the saved FP571return fp();572}573574intptr_t* frame::real_fp() const {575if (is_entry_frame()) {576// Work-around: FP (currently) does not conform to the ABI for entry577// frames (see generate_call_stub). Might be worth fixing as another CR.578// Following code assumes (and asserts) this has not yet been fixed.579assert(frame::entry_frame_call_wrapper_offset == 0, "adjust this code");580intptr_t* new_fp = fp();581new_fp += 5; // saved R0,R1,R2,R4,R10582#ifndef __SOFTFP__583new_fp += 8*2; // saved D8..D15584#endif585return new_fp;586}587if (_cb != NULL) {588// use the frame size if valid589int size = _cb->frame_size();590if (size > 0) {591return unextended_sp() + size;592}593}594// else rely on fp()595assert(! is_compiled_frame(), "unknown compiled frame size");596return fp();597}598599600