1 /*
  2  * Copyright (c) 1997, 2023, 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 #ifndef SHARE_RUNTIME_FRAME_INLINE_HPP
 26 #define SHARE_RUNTIME_FRAME_INLINE_HPP
 27 
 28 #include "runtime/frame.hpp"
 29 
 30 #include "c1/c1_Runtime1.hpp"
 31 #include "code/codeBlob.inline.hpp"
 32 #include "code/compiledMethod.inline.hpp"
 33 #include "interpreter/interpreter.hpp"
 34 #include "oops/stackChunkOop.inline.hpp"
 35 #include "oops/method.hpp"
 36 #include "runtime/continuation.hpp"
 37 #include "runtime/registerMap.hpp"
 38 #include "runtime/stubRoutines.hpp"
 39 #include "utilities/macros.hpp"
 40 #ifdef ZERO
 41 # include "entryFrame_zero.hpp"
 42 # include "fakeStubFrame_zero.hpp"
 43 # include "interpreterFrame_zero.hpp"
 44 #endif
 45 
 46 #include CPU_HEADER_INLINE(frame)
 47 
 48 inline bool frame::is_entry_frame() const {
 49   return StubRoutines::returns_to_call_stub(pc());
 50 }
 51 
 52 inline bool frame::is_stub_frame() const {
 53   return StubRoutines::is_stub_code(pc()) || (_cb != nullptr && _cb->is_adapter_blob());
 54 }
 55 
 56 inline bool frame::is_first_frame() const {
 57   return (is_entry_frame() && entry_frame_is_first())
 58       // Upcall stub frames entry frames are only present on certain platforms
 59       || (is_upcall_stub_frame() && upcall_stub_frame_is_first());
 60 }
 61 
 62 inline bool frame::is_upcall_stub_frame() const {
 63   return _cb != nullptr && _cb->is_upcall_stub();
 64 }
 65 
 66 inline bool frame::is_compiled_frame() const {
 67   if (_cb != nullptr &&
 68       _cb->is_compiled() &&
 69       ((CompiledMethod*)_cb)->is_java_method()) {
 70     return true;
 71   }
 72   return false;
 73 }
 74 
 75 #ifdef ASSERT
 76 static address get_register_address_in_stub(const frame& stub_fr, VMReg reg) {
 77   RegisterMap map(nullptr,
 78                   RegisterMap::UpdateMap::include,
 79                   RegisterMap::ProcessFrames::skip,
 80                   RegisterMap::WalkContinuation::skip);
 81   stub_fr.oop_map()->update_register_map(&stub_fr, &map);
 82   return map.location(reg, stub_fr.sp());
 83 }
 84 #endif
 85 
 86 inline JavaThread** frame::saved_thread_address(const frame& f) {
 87   CodeBlob* cb = f.cb();
 88   assert(cb != nullptr && cb->is_runtime_stub(), "invalid frame");
 89 
 90   JavaThread** thread_addr;
 91   if (cb == Runtime1::blob_for(Runtime1::monitorenter_id) ||
 92       cb == Runtime1::blob_for(Runtime1::monitorenter_nofpu_id)) {
 93     thread_addr = (JavaThread**)(f.sp() + Runtime1::runtime_blob_current_thread_offset(f));
 94   } else {
 95     // c2 only saves rbp in the stub frame so nothing to do.
 96     thread_addr = nullptr;
 97   }
 98   assert(get_register_address_in_stub(f, SharedRuntime::thread_register()) == (address)thread_addr, "wrong thread address");
 99   return thread_addr;
100 }
101 
102 template <typename RegisterMapT>
103 inline address frame::oopmapreg_to_location(VMReg reg, const RegisterMapT* reg_map) const {
104   if (reg->is_reg()) {
105     // If it is passed in a register, it got spilled in the stub frame.
106     return reg_map->location(reg, sp());
107   } else {
108     int sp_offset_in_bytes = reg->reg2stack() * VMRegImpl::stack_slot_size;
109     if (reg_map->in_cont()) {
110       return (address)((intptr_t)reg_map->as_RegisterMap()->stack_chunk()->relativize_usp_offset(*this, sp_offset_in_bytes));
111     }
112     address usp = (address)unextended_sp();
113     assert(reg_map->thread() == nullptr || reg_map->thread()->is_in_usable_stack(usp), INTPTR_FORMAT, p2i(usp));
114     return (usp + sp_offset_in_bytes);
115   }
116 }
117 
118 template <typename RegisterMapT>
119 inline oop* frame::oopmapreg_to_oop_location(VMReg reg, const RegisterMapT* reg_map) const {
120   return (oop*)oopmapreg_to_location(reg, reg_map);
121 }
122 
123 inline CodeBlob* frame::get_cb() const {
124   // if (_cb == nullptr) _cb = CodeCache::find_blob(_pc);
125   if (_cb == nullptr) {
126     int slot;
127     _cb = CodeCache::find_blob_and_oopmap(_pc, slot);
128     if (_oop_map == nullptr && slot >= 0) {
129       _oop_map = _cb->oop_map_for_slot(slot, _pc);
130     }
131   }
132   return _cb;
133 }
134 
135 inline const ImmutableOopMap* frame::get_oop_map() const {
136   if (_cb == nullptr || _cb->oop_maps() == nullptr) return nullptr;
137 
138   NativePostCallNop* nop = nativePostCallNop_at(_pc);
139   int oopmap_slot;
140   int cb_offset;
141   if (nop != nullptr && nop->decode(oopmap_slot, cb_offset)) {
142     return _cb->oop_map_for_slot(oopmap_slot, _pc);
143   }
144   const ImmutableOopMap* oop_map = OopMapSet::find_map(this);
145   return oop_map;
146 }
147 
148 inline int frame::interpreter_frame_monitor_size_in_bytes() {
149   // Number of bytes for a monitor.
150   return frame::interpreter_frame_monitor_size() * wordSize;
151 }
152 
153 #endif // SHARE_RUNTIME_FRAME_INLINE_HPP