1 /*
  2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
  4  * Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #ifndef CPU_RISCV_FRAME_RISCV_INLINE_HPP
 28 #define CPU_RISCV_FRAME_RISCV_INLINE_HPP
 29 
 30 #include "code/codeCache.hpp"
 31 #include "code/vmreg.inline.hpp"
 32 
 33 // Inline functions for RISCV frames:
 34 
 35 // Constructors:
 36 
 37 inline frame::frame() {
 38   _pc = NULL;
 39   _sp = NULL;
 40   _unextended_sp = NULL;
 41   _fp = NULL;
 42   _cb = NULL;
 43   _deopt_state = unknown;
 44 }
 45 
 46 static int spin;
 47 
 48 inline void frame::init(intptr_t* ptr_sp, intptr_t* ptr_fp, address pc) {
 49   intptr_t a = intptr_t(ptr_sp);
 50   intptr_t b = intptr_t(ptr_fp);
 51   _sp = ptr_sp;
 52   _unextended_sp = ptr_sp;
 53   _fp = ptr_fp;
 54   _pc = pc;
 55   assert(pc != NULL, "no pc?");
 56   _cb = CodeCache::find_blob(pc);
 57   adjust_unextended_sp();
 58 
 59   address original_pc = CompiledMethod::get_deopt_original_pc(this);
 60   if (original_pc != NULL) {
 61     _pc = original_pc;
 62     _deopt_state = is_deoptimized;
 63   } else {
 64     _deopt_state = not_deoptimized;
 65   }
 66 }
 67 
 68 inline frame::frame(intptr_t* ptr_sp, intptr_t* ptr_fp, address pc) {
 69   init(ptr_sp, ptr_fp, pc);
 70 }
 71 
 72 inline frame::frame(intptr_t* ptr_sp, intptr_t* unextended_sp, intptr_t* ptr_fp, address pc) {
 73   intptr_t a = intptr_t(ptr_sp);
 74   intptr_t b = intptr_t(ptr_fp);
 75   _sp = ptr_sp;
 76   _unextended_sp = unextended_sp;
 77   _fp = ptr_fp;
 78   _pc = pc;
 79   assert(pc != NULL, "no pc?");
 80   _cb = CodeCache::find_blob(pc);
 81   adjust_unextended_sp();
 82 
 83   address original_pc = CompiledMethod::get_deopt_original_pc(this);
 84   if (original_pc != NULL) {
 85     _pc = original_pc;
 86     assert(_cb->as_compiled_method()->insts_contains_inclusive(_pc),
 87            "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
 88     _deopt_state = is_deoptimized;
 89   } else {
 90     _deopt_state = not_deoptimized;
 91   }
 92 }
 93 
 94 inline frame::frame(intptr_t* ptr_sp, intptr_t* ptr_fp) {
 95   intptr_t a = intptr_t(ptr_sp);
 96   intptr_t b = intptr_t(ptr_fp);
 97   _sp = ptr_sp;
 98   _unextended_sp = ptr_sp;
 99   _fp = ptr_fp;
100   _pc = (address)(ptr_sp[-1]);
101 
102   // Here's a sticky one. This constructor can be called via AsyncGetCallTrace
103   // when last_Java_sp is non-null but the pc fetched is junk. If we are truly
104   // unlucky the junk value could be to a zombied method and we'll die on the
105   // find_blob call. This is also why we can have no asserts on the validity
106   // of the pc we find here. AsyncGetCallTrace -> pd_get_top_frame_for_signal_handler
107   // -> pd_last_frame should use a specialized version of pd_last_frame which could
108   // call a specilaized frame constructor instead of this one.
109   // Then we could use the assert below. However this assert is of somewhat dubious
110   // value.
111 
112   _cb = CodeCache::find_blob(_pc);
113   adjust_unextended_sp();
114 
115   address original_pc = CompiledMethod::get_deopt_original_pc(this);
116   if (original_pc != NULL) {
117     _pc = original_pc;
118     _deopt_state = is_deoptimized;
119   } else {
120     _deopt_state = not_deoptimized;
121   }
122 }
123 
124 // Accessors
125 
126 inline bool frame::equal(frame other) const {
127   bool ret =  sp() == other.sp() &&
128               unextended_sp() == other.unextended_sp() &&
129               fp() == other.fp() &&
130               pc() == other.pc();
131   assert(!ret || ret && cb() == other.cb() && _deopt_state == other._deopt_state, "inconsistent construction");
132   return ret;
133 }
134 
135 // Return unique id for this frame. The id must have a value where we can distinguish
136 // identity and younger/older relationship. NULL represents an invalid (incomparable)
137 // frame.
138 inline intptr_t* frame::id(void) const { return unextended_sp(); }
139 
140 // Return true if the frame is older (less recent activation) than the frame represented by id
141 inline bool frame::is_older(intptr_t* id) const   { assert(this->id() != NULL && id != NULL, "NULL frame id");
142                                                     return this->id() > id ; }
143 
144 inline intptr_t* frame::link() const              { return (intptr_t*) *(intptr_t **)addr_at(link_offset); }
145 
146 inline intptr_t* frame::link_or_null() const {
147   intptr_t** ptr = (intptr_t **)addr_at(link_offset);
148   return os::is_readable_pointer(ptr) ? *ptr : NULL;
149 }
150 
151 inline intptr_t* frame::unextended_sp() const     { return _unextended_sp; }
152 
153 // Return address
154 inline address* frame::sender_pc_addr() const     { return (address*) addr_at(return_addr_offset); }
155 inline address  frame::sender_pc() const          { return *sender_pc_addr(); }
156 inline intptr_t* frame::sender_sp() const         { return addr_at(sender_sp_offset); }
157 
158 inline intptr_t** frame::interpreter_frame_locals_addr() const {
159   return (intptr_t**)addr_at(interpreter_frame_locals_offset);
160 }
161 
162 inline intptr_t* frame::interpreter_frame_last_sp() const {
163   return *(intptr_t**)addr_at(interpreter_frame_last_sp_offset);
164 }
165 
166 inline intptr_t* frame::interpreter_frame_bcp_addr() const {
167   return (intptr_t*)addr_at(interpreter_frame_bcp_offset);
168 }
169 
170 inline intptr_t* frame::interpreter_frame_mdp_addr() const {
171   return (intptr_t*)addr_at(interpreter_frame_mdp_offset);
172 }
173 
174 
175 // Constant pool cache
176 
177 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
178   return (ConstantPoolCache**)addr_at(interpreter_frame_cache_offset);
179 }
180 
181 // Method
182 
183 inline Method** frame::interpreter_frame_method_addr() const {
184   return (Method**)addr_at(interpreter_frame_method_offset);
185 }
186 
187 // Mirror
188 
189 inline oop* frame::interpreter_frame_mirror_addr() const {
190   return (oop*)addr_at(interpreter_frame_mirror_offset);
191 }
192 
193 // top of expression stack
194 inline intptr_t* frame::interpreter_frame_tos_address() const {
195   intptr_t* last_sp = interpreter_frame_last_sp();
196   if (last_sp == NULL) {
197     return sp();
198   } else {
199     // sp() may have been extended or shrunk by an adapter.  At least
200     // check that we don't fall behind the legal region.
201     // For top deoptimized frame last_sp == interpreter_frame_monitor_end.
202     assert(last_sp <= (intptr_t*) interpreter_frame_monitor_end(), "bad tos");
203     return last_sp;
204   }
205 }
206 
207 inline oop* frame::interpreter_frame_temp_oop_addr() const {
208   return (oop *)(fp() + interpreter_frame_oop_temp_offset);
209 }
210 
211 inline int frame::interpreter_frame_monitor_size() {
212   return BasicObjectLock::size();
213 }
214 
215 
216 // expression stack
217 // (the max_stack arguments are used by the GC; see class FrameClosure)
218 
219 inline intptr_t* frame::interpreter_frame_expression_stack() const {
220   intptr_t* monitor_end = (intptr_t*) interpreter_frame_monitor_end();
221   return monitor_end-1;
222 }
223 
224 
225 // Entry frames
226 
227 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
228  return (JavaCallWrapper**)addr_at(entry_frame_call_wrapper_offset);
229 }
230 
231 
232 // Compiled frames
233 
234 inline oop frame::saved_oop_result(RegisterMap* map) const {
235   oop* result_adr = (oop *)map->location(x10->as_VMReg());
236   guarantee(result_adr != NULL, "bad register save location");
237   return (*result_adr);
238 }
239 
240 inline void frame::set_saved_oop_result(RegisterMap* map, oop obj) {
241   oop* result_adr = (oop *)map->location(x10->as_VMReg());
242   guarantee(result_adr != NULL, "bad register save location");
243   *result_adr = obj;
244 }
245 
246 #endif // CPU_RISCV_FRAME_RISCV_INLINE_HPP