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, 2021, 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 RISCV64 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::unextended_sp() const     { return _unextended_sp; }
147 
148 // Return address
149 inline address* frame::sender_pc_addr() const     { return (address*) addr_at(return_addr_offset); }
150 inline address  frame::sender_pc() const          { return *sender_pc_addr(); }
151 inline intptr_t* frame::sender_sp() const         { return addr_at(sender_sp_offset); }
152 
153 // C frame methods
154 inline intptr_t* frame::c_frame_link() const      { return (intptr_t*) *(intptr_t **)addr_at(c_frame_link_offset); }
155 inline address*  frame::c_frame_sender_pc_addr() const { return (address*) addr_at(c_frame_return_addr_offset); }
156 inline address   frame::c_frame_sender_pc() const { return *c_frame_sender_pc_addr(); }
157 inline intptr_t* frame::c_frame_sender_sp() const { return addr_at(c_frame_sender_sp_offset); }
158 
159 inline intptr_t** frame::interpreter_frame_locals_addr() const {
160   return (intptr_t**)addr_at(interpreter_frame_locals_offset);
161 }
162 
163 inline intptr_t* frame::interpreter_frame_last_sp() const {
164   return *(intptr_t**)addr_at(interpreter_frame_last_sp_offset);
165 }
166 
167 inline intptr_t* frame::interpreter_frame_bcp_addr() const {
168   return (intptr_t*)addr_at(interpreter_frame_bcp_offset);
169 }
170 
171 inline intptr_t* frame::interpreter_frame_mdp_addr() const {
172   return (intptr_t*)addr_at(interpreter_frame_mdp_offset);
173 }
174 
175 
176 // Constant pool cache
177 
178 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
179   return (ConstantPoolCache**)addr_at(interpreter_frame_cache_offset);
180 }
181 
182 // Method
183 
184 inline Method** frame::interpreter_frame_method_addr() const {
185   return (Method**)addr_at(interpreter_frame_method_offset);
186 }
187 
188 // Mirror
189 
190 inline oop* frame::interpreter_frame_mirror_addr() const {
191   return (oop*)addr_at(interpreter_frame_mirror_offset);
192 }
193 
194 // top of expression stack
195 inline intptr_t* frame::interpreter_frame_tos_address() const {
196   intptr_t* last_sp = interpreter_frame_last_sp();
197   if (last_sp == NULL) {
198     return sp();
199   } else {
200     // sp() may have been extended or shrunk by an adapter.  At least
201     // check that we don't fall behind the legal region.
202     // For top deoptimized frame last_sp == interpreter_frame_monitor_end.
203     assert(last_sp <= (intptr_t*) interpreter_frame_monitor_end(), "bad tos");
204     return last_sp;
205   }
206 }
207 
208 inline oop* frame::interpreter_frame_temp_oop_addr() const {
209   return (oop *)(fp() + interpreter_frame_oop_temp_offset);
210 }
211 
212 inline int frame::interpreter_frame_monitor_size() {
213   return BasicObjectLock::size();
214 }
215 
216 
217 // expression stack
218 // (the max_stack arguments are used by the GC; see class FrameClosure)
219 
220 inline intptr_t* frame::interpreter_frame_expression_stack() const {
221   intptr_t* monitor_end = (intptr_t*) interpreter_frame_monitor_end();
222   return monitor_end-1;
223 }
224 
225 
226 // Entry frames
227 
228 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
229  return (JavaCallWrapper**)addr_at(entry_frame_call_wrapper_offset);
230 }
231 
232 
233 // Compiled frames
234 inline oop frame::saved_oop_result(RegisterMap* map) const {
235   oop* result_adr = (oop *)map->location(x10->as_VMReg());
236   if(result_adr != NULL) {
237     return (*result_adr);
238   } else {
239     ShouldNotReachHere();
240     return NULL;
241   }
242 }
243 
244 inline void frame::set_saved_oop_result(RegisterMap* map, oop obj) {
245   oop* result_adr = (oop *)map->location(x10->as_VMReg());
246   if(result_adr != NULL) {
247     *result_adr = obj;
248   } else {
249     ShouldNotReachHere();
250   }
251 }
252 
253 #endif // CPU_RISCV_FRAME_RISCV_INLINE_HPP