1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016 SAP SE. 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_S390_VM_FRAME_S390_INLINE_HPP
  27 #define CPU_S390_VM_FRAME_S390_INLINE_HPP
  28 
  29 #include "code/codeCache.hpp"
  30 #include "code/vmreg.inline.hpp"
  31 #include "utilities/align.hpp"
  32 
  33 // Inline functions for z/Architecture frames:
  34 
  35 inline void frame::find_codeblob_and_set_pc_and_deopt_state(address pc) {
  36   assert(pc != NULL, "precondition: must have PC");
  37 
  38   _cb = CodeCache::find_blob(pc);
  39   _pc = pc;   // Must be set for get_deopt_original_pc().
  40 
  41   _fp = (intptr_t *) own_abi()->callers_sp;
  42 
  43   address original_pc = CompiledMethod::get_deopt_original_pc(this);
  44   if (original_pc != NULL) {
  45     _pc = original_pc;
  46     _deopt_state = is_deoptimized;
  47   } else {
  48     _deopt_state = not_deoptimized;
  49   }
  50 
  51   assert(((uint64_t)_sp & 0x7) == 0, "SP must be 8-byte aligned");
  52 }
  53 
  54 // Constructors
  55 
  56 // Initialize all fields, _unextended_sp will be adjusted in find_codeblob_and_set_pc_and_deopt_state.
  57 inline frame::frame() : _sp(NULL), _unextended_sp(NULL), _fp(NULL), _cb(NULL), _pc(NULL), _deopt_state(unknown) {}
  58 
  59 inline frame::frame(intptr_t* sp) : _sp(sp), _unextended_sp(sp) {
  60   find_codeblob_and_set_pc_and_deopt_state((address)own_abi()->return_pc);
  61 }
  62 
  63 inline frame::frame(intptr_t* sp, address pc) : _sp(sp), _unextended_sp(sp) {
  64   find_codeblob_and_set_pc_and_deopt_state(pc); // Also sets _fp and adjusts _unextended_sp.
  65 }
  66 
  67 inline frame::frame(intptr_t* sp, address pc, intptr_t* unextended_sp) : _sp(sp), _unextended_sp(unextended_sp) {
  68   find_codeblob_and_set_pc_and_deopt_state(pc); // Also sets _fp and adjusts _unextended_sp.
  69 }
  70 
  71 // Generic constructor. Used by pns() in debug.cpp only
  72 #ifndef PRODUCT
  73 inline frame::frame(void* sp, void* pc, void* unextended_sp) :
  74   _sp((intptr_t*)sp), _unextended_sp((intptr_t*)unextended_sp), _cb(NULL), _pc(NULL) {
  75   find_codeblob_and_set_pc_and_deopt_state((address)pc); // Also sets _fp and adjusts _unextended_sp.
  76 }
  77 #endif
  78 
  79 // template interpreter state
  80 inline frame::z_ijava_state* frame::ijava_state() const {
  81   z_ijava_state* state = (z_ijava_state*) ((uintptr_t)fp() - z_ijava_state_size);
  82   assert(state->magic == (intptr_t) frame::z_istate_magic_number,
  83          "wrong z_ijava_state in interpreter frame (no magic found)");
  84   return state;
  85 }
  86 
  87 inline BasicObjectLock** frame::interpreter_frame_monitors_addr() const {
  88   return (BasicObjectLock**) &(ijava_state()->monitors);
  89 }
  90 
  91 // The next two funcions read and write z_ijava_state.monitors.
  92 inline BasicObjectLock* frame::interpreter_frame_monitors() const {
  93   return *interpreter_frame_monitors_addr();
  94 }
  95 inline void frame::interpreter_frame_set_monitors(BasicObjectLock* monitors) {
  96   *interpreter_frame_monitors_addr() = monitors;
  97 }
  98 
  99 // Accessors
 100 
 101 // Return unique id for this frame. The id must have a value where we
 102 // can distinguish identity and younger/older relationship. NULL
 103 // represents an invalid (incomparable) frame.
 104 inline intptr_t* frame::id(void) const {
 105   // Use _fp. _sp or _unextended_sp wouldn't be correct due to resizing.
 106   return _fp;
 107 }
 108 
 109 // Return true if this frame is younger (more recent activation) than
 110 // the frame represented by id.
 111 inline bool frame::is_younger(intptr_t* id) const {
 112   assert(this->id() != NULL && id != NULL, "NULL frame id");
 113   // Stack grows towards smaller addresses on z/Architecture.
 114   return this->id() < id;
 115 }
 116 
 117 // Return true if this frame is older (less recent activation) than
 118 // the frame represented by id.
 119 inline bool frame::is_older(intptr_t* id) const {
 120   assert(this->id() != NULL && id != NULL, "NULL frame id");
 121   // Stack grows towards smaller addresses on z/Architecture.
 122   return this->id() > id;
 123 }
 124 
 125 inline int frame::frame_size(RegisterMap* map) const {
 126   // Stack grows towards smaller addresses on z/Linux: sender is at a higher address.
 127   return sender_sp() - sp();
 128 }
 129 
 130 // Ignore c2i adapter frames.
 131 inline intptr_t* frame::unextended_sp() const {
 132   return _unextended_sp;
 133 }
 134 
 135 inline address frame::sender_pc() const {
 136   return (address) callers_abi()->return_pc;
 137 }
 138 
 139 // Get caller pc, if caller is native from stack slot of gpr14.
 140 inline address frame::native_sender_pc() const {
 141   return (address) callers_abi()->gpr14;
 142 }
 143 
 144 // Get caller pc from stack slot of gpr10.
 145 inline address frame::callstub_sender_pc() const {
 146   return (address) callers_abi()->gpr10;
 147 }
 148 
 149 inline address* frame::sender_pc_addr() const {
 150   return (address*) &(callers_abi()->return_pc);
 151 }
 152 
 153 inline intptr_t* frame::sender_sp() const {
 154   return (intptr_t*) callers_abi();
 155 }
 156 
 157 inline intptr_t* frame::link() const {
 158   return (intptr_t*) callers_abi()->callers_sp;
 159 }
 160 
 161 inline intptr_t** frame::interpreter_frame_locals_addr() const {
 162   return (intptr_t**) &(ijava_state()->locals);
 163 }
 164 
 165 inline intptr_t* frame::interpreter_frame_bcp_addr() const {
 166   return (intptr_t*) &(ijava_state()->bcp);
 167 }
 168 
 169 inline intptr_t* frame::interpreter_frame_mdp_addr() const {
 170   return (intptr_t*) &(ijava_state()->mdx);
 171 }
 172 
 173 // Bottom(base) of the expression stack (highest address).
 174 inline intptr_t* frame::interpreter_frame_expression_stack() const {
 175   return (intptr_t*)interpreter_frame_monitor_end() - 1;
 176 }
 177 
 178 inline jint frame::interpreter_frame_expression_stack_direction() {
 179   return -1;
 180 }
 181 
 182 inline intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
 183   return &interpreter_frame_tos_address()[offset];
 184 }
 185 
 186 
 187 // monitor elements
 188 
 189 // End is lower in memory than begin, and beginning element is oldest element.
 190 // Also begin is one past last monitor.
 191 
 192 inline intptr_t* frame::interpreter_frame_top_frame_sp() {
 193   return (intptr_t*)ijava_state()->top_frame_sp;
 194 }
 195 
 196 inline void frame::interpreter_frame_set_top_frame_sp(intptr_t* top_frame_sp) {
 197   ijava_state()->top_frame_sp = (intptr_t) top_frame_sp;
 198 }
 199 
 200 inline void frame::interpreter_frame_set_sender_sp(intptr_t* sender_sp) {
 201   ijava_state()->sender_sp = (intptr_t) sender_sp;
 202 }
 203 
 204 #ifdef ASSERT
 205 inline void frame::interpreter_frame_set_magic() {
 206   ijava_state()->magic = (intptr_t) frame::z_istate_magic_number;
 207 }
 208 #endif
 209 
 210 // Where z_ijava_state.esp is saved.
 211 inline intptr_t** frame::interpreter_frame_esp_addr() const {
 212   return (intptr_t**) &(ijava_state()->esp);
 213 }
 214 
 215 // top of expression stack (lowest address)
 216 inline intptr_t* frame::interpreter_frame_tos_address() const {
 217   return *interpreter_frame_esp_addr() + 1;
 218 }
 219 
 220 inline void frame::interpreter_frame_set_tos_address(intptr_t* x) {
 221   *interpreter_frame_esp_addr() = x - 1;
 222 }
 223 
 224 // Stack slot needed for native calls and GC.
 225 inline oop * frame::interpreter_frame_temp_oop_addr() const {
 226   return (oop *) ((address) _fp + _z_ijava_state_neg(oop_tmp));
 227 }
 228 
 229 // In keeping with Intel side: end is lower in memory than begin.
 230 // Beginning element is oldest element. Also begin is one past last monitor.
 231 inline BasicObjectLock * frame::interpreter_frame_monitor_begin() const {
 232   return (BasicObjectLock*)ijava_state();
 233 }
 234 
 235 inline BasicObjectLock * frame::interpreter_frame_monitor_end() const {
 236   return interpreter_frame_monitors();
 237 }
 238 
 239 inline void frame::interpreter_frame_set_monitor_end(BasicObjectLock* monitors) {
 240   interpreter_frame_set_monitors((BasicObjectLock *)monitors);
 241 }
 242 
 243 inline int frame::interpreter_frame_monitor_size() {
 244   // Number of stack slots for a monitor
 245   return align_up(BasicObjectLock::size() /* number of stack slots */,
 246                   WordsPerLong /* Number of stack slots for a Java long. */);
 247 }
 248 
 249 inline int frame::interpreter_frame_monitor_size_in_bytes() {
 250   // Number of bytes for a monitor.
 251   return frame::interpreter_frame_monitor_size() * wordSize;
 252 }
 253 
 254 inline int frame::interpreter_frame_interpreterstate_size_in_bytes() {
 255   return z_ijava_state_size;
 256 }
 257 
 258 inline Method** frame::interpreter_frame_method_addr() const {
 259   return (Method**)&(ijava_state()->method);
 260 }
 261 
 262 inline oop* frame::interpreter_frame_mirror_addr() const {
 263   return (oop*)&(ijava_state()->mirror);
 264 }
 265 
 266 // Constant pool cache
 267 
 268 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
 269   return (ConstantPoolCache**)&(ijava_state()->cpoolCache);
 270 }
 271 
 272 // entry frames
 273 
 274 inline intptr_t* frame::entry_frame_argument_at(int offset) const {
 275   // Since an entry frame always calls the interpreter first,
 276   // the parameters are on the stack and relative to known register in the
 277   // entry frame.
 278   intptr_t* tos = (intptr_t*) entry_frame_locals()->arguments_tos_address;
 279   return &tos[offset + 1]; // prepushed tos
 280 }
 281 
 282 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
 283   return (JavaCallWrapper**) &entry_frame_locals()->call_wrapper_address;
 284 }
 285 
 286 inline oop frame::saved_oop_result(RegisterMap* map) const {
 287   return *((oop*) map->location(Z_R2->as_VMReg()));  // R2 is return register.
 288 }
 289 
 290 inline void frame::set_saved_oop_result(RegisterMap* map, oop obj) {
 291   *((oop*) map->location(Z_R2->as_VMReg())) = obj;  // R2 is return register.
 292 }
 293 
 294 inline intptr_t* frame::real_fp() const {
 295   return fp();
 296 }
 297 
 298 #endif // CPU_S390_VM_FRAME_S390_INLINE_HPP