1 /*
   2  * Copyright (c) 2016, 2019, 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_FRAME_S390_INLINE_HPP
  27 #define CPU_S390_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), _pc(NULL), _cb(NULL), _deopt_state(unknown), _unextended_sp(NULL), _fp(NULL) {}
  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), _pc(NULL), _cb(NULL), _unextended_sp((intptr_t*)unextended_sp) {
  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_unchecked() const {
  81   z_ijava_state* state = (z_ijava_state*) ((uintptr_t)fp() - z_ijava_state_size);
  82   return state;
  83 }
  84 
  85 inline frame::z_ijava_state* frame::ijava_state() const {
  86   z_ijava_state* state = ijava_state_unchecked();
  87   assert(state->magic == (intptr_t) frame::z_istate_magic_number,
  88          "wrong z_ijava_state in interpreter frame (no magic found)");
  89   return state;
  90 }
  91 
  92 inline BasicObjectLock** frame::interpreter_frame_monitors_addr() const {
  93   return (BasicObjectLock**) &(ijava_state()->monitors);
  94 }
  95 
  96 // The next two funcions read and write z_ijava_state.monitors.
  97 inline BasicObjectLock* frame::interpreter_frame_monitors() const {
  98   return *interpreter_frame_monitors_addr();
  99 }
 100 inline void frame::interpreter_frame_set_monitors(BasicObjectLock* monitors) {
 101   *interpreter_frame_monitors_addr() = monitors;
 102 }
 103 
 104 // Accessors
 105 
 106 // Return unique id for this frame. The id must have a value where we
 107 // can distinguish identity and younger/older relationship. NULL
 108 // represents an invalid (incomparable) frame.
 109 inline intptr_t* frame::id(void) const {
 110   // Use _fp. _sp or _unextended_sp wouldn't be correct due to resizing.
 111   return _fp;
 112 }
 113 
 114 // Return true if this frame is older (less recent activation) than
 115 // the frame represented by id.
 116 inline bool frame::is_older(intptr_t* id) const {
 117   assert(this->id() != NULL && id != NULL, "NULL frame id");
 118   // Stack grows towards smaller addresses on z/Architecture.
 119   return this->id() > id;
 120 }
 121 
 122 inline int frame::frame_size(RegisterMap* map) const {
 123   // Stack grows towards smaller addresses on z/Linux: sender is at a higher address.
 124   return sender_sp() - sp();
 125 }
 126 
 127 // Ignore c2i adapter frames.
 128 inline intptr_t* frame::unextended_sp() const {
 129   return _unextended_sp;
 130 }
 131 
 132 inline address frame::sender_pc() const {
 133   return (address) callers_abi()->return_pc;
 134 }
 135 
 136 // Get caller pc, if caller is native from stack slot of gpr14.
 137 inline address frame::native_sender_pc() const {
 138   return (address) callers_abi()->gpr14;
 139 }
 140 
 141 // Get caller pc from stack slot of gpr10.
 142 inline address frame::callstub_sender_pc() const {
 143   return (address) callers_abi()->gpr10;
 144 }
 145 
 146 inline address* frame::sender_pc_addr() const {
 147   return (address*) &(callers_abi()->return_pc);
 148 }
 149 
 150 inline intptr_t* frame::sender_sp() const {
 151   return (intptr_t*) callers_abi();
 152 }
 153 
 154 inline intptr_t* frame::link() const {
 155   return (intptr_t*) callers_abi()->callers_sp;
 156 }
 157 
 158 inline intptr_t** frame::interpreter_frame_locals_addr() const {
 159   return (intptr_t**) &(ijava_state()->locals);
 160 }
 161 
 162 inline intptr_t* frame::interpreter_frame_bcp_addr() const {
 163   return (intptr_t*) &(ijava_state()->bcp);
 164 }
 165 
 166 inline intptr_t* frame::interpreter_frame_mdp_addr() const {
 167   return (intptr_t*) &(ijava_state()->mdx);
 168 }
 169 
 170 // Bottom(base) of the expression stack (highest address).
 171 inline intptr_t* frame::interpreter_frame_expression_stack() const {
 172   return (intptr_t*)interpreter_frame_monitor_end() - 1;
 173 }
 174 
 175 inline intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
 176   return &interpreter_frame_tos_address()[offset];
 177 }
 178 
 179 
 180 // monitor elements
 181 
 182 // End is lower in memory than begin, and beginning element is oldest element.
 183 // Also begin is one past last monitor.
 184 
 185 inline intptr_t* frame::interpreter_frame_top_frame_sp() {
 186   return (intptr_t*)ijava_state()->top_frame_sp;
 187 }
 188 
 189 inline void frame::interpreter_frame_set_top_frame_sp(intptr_t* top_frame_sp) {
 190   ijava_state()->top_frame_sp = (intptr_t) top_frame_sp;
 191 }
 192 
 193 inline void frame::interpreter_frame_set_sender_sp(intptr_t* sender_sp) {
 194   ijava_state()->sender_sp = (intptr_t) sender_sp;
 195 }
 196 
 197 #ifdef ASSERT
 198 inline void frame::interpreter_frame_set_magic() {
 199   ijava_state()->magic = (intptr_t) frame::z_istate_magic_number;
 200 }
 201 #endif
 202 
 203 // Where z_ijava_state.esp is saved.
 204 inline intptr_t** frame::interpreter_frame_esp_addr() const {
 205   return (intptr_t**) &(ijava_state()->esp);
 206 }
 207 
 208 // top of expression stack (lowest address)
 209 inline intptr_t* frame::interpreter_frame_tos_address() const {
 210   return *interpreter_frame_esp_addr() + 1;
 211 }
 212 
 213 inline void frame::interpreter_frame_set_tos_address(intptr_t* x) {
 214   *interpreter_frame_esp_addr() = x - 1;
 215 }
 216 
 217 // Stack slot needed for native calls and GC.
 218 inline oop * frame::interpreter_frame_temp_oop_addr() const {
 219   return (oop *) ((address) _fp + _z_ijava_state_neg(oop_tmp));
 220 }
 221 
 222 // In keeping with Intel side: end is lower in memory than begin.
 223 // Beginning element is oldest element. Also begin is one past last monitor.
 224 inline BasicObjectLock * frame::interpreter_frame_monitor_begin() const {
 225   return (BasicObjectLock*)ijava_state();
 226 }
 227 
 228 inline BasicObjectLock * frame::interpreter_frame_monitor_end() const {
 229   return interpreter_frame_monitors();
 230 }
 231 
 232 inline void frame::interpreter_frame_set_monitor_end(BasicObjectLock* monitors) {
 233   interpreter_frame_set_monitors((BasicObjectLock *)monitors);
 234 }
 235 
 236 inline int frame::interpreter_frame_monitor_size() {
 237   // Number of stack slots for a monitor
 238   return align_up(BasicObjectLock::size() /* number of stack slots */,
 239                   WordsPerLong /* Number of stack slots for a Java long. */);
 240 }
 241 
 242 inline int frame::interpreter_frame_monitor_size_in_bytes() {
 243   // Number of bytes for a monitor.
 244   return frame::interpreter_frame_monitor_size() * wordSize;
 245 }
 246 
 247 inline int frame::interpreter_frame_interpreterstate_size_in_bytes() {
 248   return z_ijava_state_size;
 249 }
 250 
 251 inline Method** frame::interpreter_frame_method_addr() const {
 252   return (Method**)&(ijava_state()->method);
 253 }
 254 
 255 inline oop* frame::interpreter_frame_mirror_addr() const {
 256   return (oop*)&(ijava_state()->mirror);
 257 }
 258 
 259 // Constant pool cache
 260 
 261 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
 262   return (ConstantPoolCache**)&(ijava_state()->cpoolCache);
 263 }
 264 
 265 // entry frames
 266 
 267 inline intptr_t* frame::entry_frame_argument_at(int offset) const {
 268   // Since an entry frame always calls the interpreter first,
 269   // the parameters are on the stack and relative to known register in the
 270   // entry frame.
 271   intptr_t* tos = (intptr_t*) entry_frame_locals()->arguments_tos_address;
 272   return &tos[offset + 1]; // prepushed tos
 273 }
 274 
 275 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
 276   return (JavaCallWrapper**) &entry_frame_locals()->call_wrapper_address;
 277 }
 278 
 279 inline oop frame::saved_oop_result(const RegisterMap* map) const {
 280   return *((oop*) map->location(Z_R2->as_VMReg()));  // R2 is return register.
 281 }
 282 
 283 inline void frame::set_saved_oop_result(const RegisterMap* map, oop obj) {
 284   *((oop*) map->location(Z_R2->as_VMReg())) = obj;  // R2 is return register.
 285 }
 286 
 287 inline intptr_t* frame::real_fp() const {
 288   return fp();
 289 }
 290 
 291 #endif // CPU_S390_FRAME_S390_INLINE_HPP