1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef CPU_AARCH64_VM_FRAME_AARCH64_INLINE_HPP
  27 #define CPU_AARCH64_VM_FRAME_AARCH64_INLINE_HPP
  28 
  29 #include "code/codeCache.hpp"
  30 #include "code/vmreg.inline.hpp"
  31 
  32 // Inline functions for AArch64 frames:
  33 
  34 // Constructors:
  35 
  36 inline frame::frame() {
  37   _pc = NULL;
  38   _sp = NULL;
  39   _unextended_sp = NULL;
  40   _fp = NULL;
  41   _cb = NULL;
  42   _deopt_state = unknown;
  43 }
  44 
  45 static int spin;
  46 
  47 inline void frame::init(intptr_t* sp, intptr_t* fp, address pc) {
  48   intptr_t a = intptr_t(sp);
  49   intptr_t b = intptr_t(fp);
  50 #ifndef PRODUCT
  51   if (fp)
  52     if (sp > fp || (fp - sp > 0x100000))
  53       for(;;)
  54         asm("nop");
  55 #endif
  56   _sp = sp;
  57   _unextended_sp = sp;
  58   _fp = fp;
  59   _pc = pc;
  60   assert(pc != NULL, "no pc?");
  61   _cb = CodeCache::find_blob(pc);
  62   adjust_unextended_sp();
  63 
  64   address original_pc = nmethod::get_deopt_original_pc(this);
  65   if (original_pc != NULL) {
  66     _pc = original_pc;
  67     _deopt_state = is_deoptimized;
  68   } else {
  69     _deopt_state = not_deoptimized;
  70   }
  71 }
  72 
  73 inline frame::frame(intptr_t* sp, intptr_t* fp, address pc) {
  74   init(sp, fp, pc);
  75 }
  76 
  77 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc) {
  78   intptr_t a = intptr_t(sp);
  79   intptr_t b = intptr_t(fp);
  80 #ifndef PRODUCT
  81   if (fp)
  82     if (sp > fp || (fp - sp > 0x100000))
  83       for(;;)
  84         asm("nop");
  85 #endif
  86   _sp = sp;
  87   _unextended_sp = unextended_sp;
  88   _fp = fp;
  89   _pc = pc;
  90   assert(pc != NULL, "no pc?");
  91   _cb = CodeCache::find_blob(pc);
  92   adjust_unextended_sp();
  93 
  94   address original_pc = nmethod::get_deopt_original_pc(this);
  95   if (original_pc != NULL) {
  96     _pc = original_pc;
  97     assert(((nmethod*)_cb)->insts_contains(_pc), "original PC must be in nmethod");
  98     _deopt_state = is_deoptimized;
  99   } else {
 100     _deopt_state = not_deoptimized;
 101   }
 102 }
 103 
 104 inline frame::frame(intptr_t* sp, intptr_t* fp) {
 105   intptr_t a = intptr_t(sp);
 106   intptr_t b = intptr_t(fp);
 107 #ifndef PRODUCT
 108   if (fp)
 109     if (sp > fp || (fp - sp > 0x100000))
 110       for(;;)
 111         asm("nop");
 112 #endif
 113   _sp = sp;
 114   _unextended_sp = sp;
 115   _fp = fp;
 116   _pc = (address)(sp[-1]);
 117 
 118   // Here's a sticky one. This constructor can be called via AsyncGetCallTrace
 119   // when last_Java_sp is non-null but the pc fetched is junk. If we are truly
 120   // unlucky the junk value could be to a zombied method and we'll die on the
 121   // find_blob call. This is also why we can have no asserts on the validity
 122   // of the pc we find here. AsyncGetCallTrace -> pd_get_top_frame_for_signal_handler
 123   // -> pd_last_frame should use a specialized version of pd_last_frame which could
 124   // call a specilaized frame constructor instead of this one.
 125   // Then we could use the assert below. However this assert is of somewhat dubious
 126   // value.
 127   // assert(_pc != NULL, "no pc?");
 128 
 129   _cb = CodeCache::find_blob(_pc);
 130   adjust_unextended_sp();
 131 
 132   address original_pc = nmethod::get_deopt_original_pc(this);
 133   if (original_pc != NULL) {
 134     _pc = original_pc;
 135     _deopt_state = is_deoptimized;
 136   } else {
 137     _deopt_state = not_deoptimized;
 138   }
 139 }
 140 
 141 // Accessors
 142 
 143 inline bool frame::equal(frame other) const {
 144   bool ret =  sp() == other.sp()
 145               && unextended_sp() == other.unextended_sp()
 146               && fp() == other.fp()
 147               && pc() == other.pc();
 148   assert(!ret || ret && cb() == other.cb() && _deopt_state == other._deopt_state, "inconsistent construction");
 149   return ret;
 150 }
 151 
 152 // Return unique id for this frame. The id must have a value where we can distinguish
 153 // identity and younger/older relationship. NULL represents an invalid (incomparable)
 154 // frame.
 155 inline intptr_t* frame::id(void) const { return unextended_sp(); }
 156 
 157 // Relationals on frames based
 158 // Return true if the frame is younger (more recent activation) than the frame represented by id
 159 inline bool frame::is_younger(intptr_t* id) const { assert(this->id() != NULL && id != NULL, "NULL frame id");
 160                                                     return this->id() < id ; }
 161 
 162 // Return true if the frame is older (less recent activation) than the frame represented by id
 163 inline bool frame::is_older(intptr_t* id) const   { assert(this->id() != NULL && id != NULL, "NULL frame id");
 164                                                     return this->id() > id ; }
 165 
 166 
 167 
 168 inline intptr_t* frame::link() const              { return (intptr_t*) *(intptr_t **)addr_at(link_offset); }
 169 
 170 
 171 inline intptr_t* frame::unextended_sp() const     { return _unextended_sp; }
 172 
 173 // Return address:
 174 
 175 inline address* frame::sender_pc_addr()      const { return (address*) addr_at( return_addr_offset); }
 176 inline address  frame::sender_pc()           const { return *sender_pc_addr(); }
 177 
 178 #ifdef CC_INTERP
 179 
 180 inline interpreterState frame::get_interpreterState() const {
 181   return ((interpreterState)addr_at( -((int)sizeof(BytecodeInterpreter))/wordSize ));
 182 }
 183 
 184 inline intptr_t*    frame::sender_sp()        const {
 185   // Hmm this seems awfully expensive QQQ, is this really called with interpreted frames?
 186   if (is_interpreted_frame()) {
 187     assert(false, "should never happen");
 188     return get_interpreterState()->sender_sp();
 189   } else {
 190     return            addr_at(sender_sp_offset);
 191   }
 192 }
 193 
 194 inline intptr_t** frame::interpreter_frame_locals_addr() const {
 195   assert(is_interpreted_frame(), "must be interpreted");
 196   return &(get_interpreterState()->_locals);
 197 }
 198 
 199 inline intptr_t* frame::interpreter_frame_bcx_addr() const {
 200   assert(is_interpreted_frame(), "must be interpreted");
 201   return (intptr_t*) &(get_interpreterState()->_bcp);
 202 }
 203 
 204 
 205 // Constant pool cache
 206 
 207 inline constantPoolCacheOop* frame::interpreter_frame_cache_addr() const {
 208   assert(is_interpreted_frame(), "must be interpreted");
 209   return &(get_interpreterState()->_constants);
 210 }
 211 
 212 // Method
 213 
 214 inline methodOop* frame::interpreter_frame_method_addr() const {
 215   assert(is_interpreted_frame(), "must be interpreted");
 216   return &(get_interpreterState()->_method);
 217 }
 218 
 219 inline intptr_t* frame::interpreter_frame_mdx_addr() const {
 220   assert(is_interpreted_frame(), "must be interpreted");
 221   return (intptr_t*) &(get_interpreterState()->_mdx);
 222 }
 223 
 224 // top of expression stack
 225 inline intptr_t* frame::interpreter_frame_tos_address() const {
 226   assert(is_interpreted_frame(), "wrong frame type");
 227   return get_interpreterState()->_stack + 1;
 228 }
 229 
 230 #else /* asm interpreter */
 231 inline intptr_t*    frame::sender_sp()        const { return            addr_at(   sender_sp_offset); }
 232 
 233 inline intptr_t** frame::interpreter_frame_locals_addr() const {
 234   return (intptr_t**)addr_at(interpreter_frame_locals_offset);
 235 }
 236 
 237 inline intptr_t* frame::interpreter_frame_last_sp() const {
 238   return *(intptr_t**)addr_at(interpreter_frame_last_sp_offset);
 239 }
 240 
 241 inline intptr_t* frame::interpreter_frame_bcp_addr() const {
 242   return (intptr_t*)addr_at(interpreter_frame_bcp_offset);
 243 }
 244 
 245 inline intptr_t* frame::interpreter_frame_mdp_addr() const {
 246   return (intptr_t*)addr_at(interpreter_frame_mdp_offset);
 247 }
 248 
 249 
 250 // Constant pool cache
 251 
 252 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
 253   return (ConstantPoolCache**)addr_at(interpreter_frame_cache_offset);
 254 }
 255 
 256 // Method
 257 
 258 inline Method** frame::interpreter_frame_method_addr() const {
 259   return (Method**)addr_at(interpreter_frame_method_offset);
 260 }
 261 
 262 // top of expression stack
 263 inline intptr_t* frame::interpreter_frame_tos_address() const {
 264   intptr_t* last_sp = interpreter_frame_last_sp();
 265   if (last_sp == NULL) {
 266     return sp();
 267   } else {
 268     // sp() may have been extended or shrunk by an adapter.  At least
 269     // check that we don't fall behind the legal region.
 270     // For top deoptimized frame last_sp == interpreter_frame_monitor_end.
 271     assert(last_sp <= (intptr_t*) interpreter_frame_monitor_end(), "bad tos");
 272     return last_sp;
 273   }
 274 }
 275 
 276 inline oop* frame::interpreter_frame_temp_oop_addr() const {
 277   return (oop *)(fp() + interpreter_frame_oop_temp_offset);
 278 }
 279 
 280 #endif /* CC_INTERP */
 281 
 282 inline int frame::pd_oop_map_offset_adjustment() const {
 283   return 0;
 284 }
 285 
 286 inline int frame::interpreter_frame_monitor_size() {
 287   return BasicObjectLock::size();
 288 }
 289 
 290 
 291 // expression stack
 292 // (the max_stack arguments are used by the GC; see class FrameClosure)
 293 
 294 inline intptr_t* frame::interpreter_frame_expression_stack() const {
 295   intptr_t* monitor_end = (intptr_t*) interpreter_frame_monitor_end();
 296   return monitor_end-1;
 297 }
 298 
 299 
 300 inline jint frame::interpreter_frame_expression_stack_direction() { return -1; }
 301 
 302 
 303 // Entry frames
 304 
 305 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
 306  return (JavaCallWrapper**)addr_at(entry_frame_call_wrapper_offset);
 307 }
 308 
 309 
 310 // Compiled frames
 311 
 312 inline int frame::local_offset_for_compiler(int local_index, int nof_args, int max_nof_locals, int max_nof_monitors) {
 313   return (nof_args - local_index + (local_index < nof_args ? 1: -1));
 314 }
 315 
 316 inline int frame::monitor_offset_for_compiler(int local_index, int nof_args, int max_nof_locals, int max_nof_monitors) {
 317   return local_offset_for_compiler(local_index, nof_args, max_nof_locals, max_nof_monitors);
 318 }
 319 
 320 inline int frame::min_local_offset_for_compiler(int nof_args, int max_nof_locals, int max_nof_monitors) {
 321   return (nof_args - (max_nof_locals + max_nof_monitors*2) - 1);
 322 }
 323 
 324 inline bool frame::volatile_across_calls(Register reg) {
 325   return true;
 326 }
 327 
 328 
 329 
 330 inline oop frame::saved_oop_result(RegisterMap* map) const {
 331   oop* result_adr = (oop *)map->location(r0->as_VMReg());
 332   guarantee(result_adr != NULL, "bad register save location");
 333 
 334   return (*result_adr);
 335 }
 336 
 337 inline void frame::set_saved_oop_result(RegisterMap* map, oop obj) {
 338   oop* result_adr = (oop *)map->location(r0->as_VMReg());
 339   guarantee(result_adr != NULL, "bad register save location");
 340 
 341   *result_adr = obj;
 342 }
 343 
 344 #endif // CPU_AARCH64_VM_FRAME_AARCH64_INLINE_HPP