1 /*
   2  * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2017 SAP AG. 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 #include "precompiled.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/markOop.hpp"
  30 #include "oops/method.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/frame.inline.hpp"
  33 #include "runtime/handles.inline.hpp"
  34 #include "runtime/javaCalls.hpp"
  35 #include "runtime/monitorChunk.hpp"
  36 #include "runtime/signature.hpp"
  37 #include "runtime/stubCodeGenerator.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 #include "vmreg_ppc.inline.hpp"
  40 #ifdef COMPILER1
  41 #include "c1/c1_Runtime1.hpp"
  42 #include "runtime/vframeArray.hpp"
  43 #endif
  44 
  45 #ifdef ASSERT
  46 void RegisterMap::check_location_valid() {
  47 }
  48 #endif // ASSERT
  49 
  50 bool frame::safe_for_sender(JavaThread *thread) {
  51   bool safe = false;
  52   address   cursp = (address)sp();
  53   address   curfp = (address)fp();
  54   if ((cursp != NULL && curfp != NULL &&
  55       (cursp <= thread->stack_base() && cursp >= thread->stack_base() - thread->stack_size())) &&
  56       (curfp <= thread->stack_base() && curfp >= thread->stack_base() - thread->stack_size())) {
  57       safe = true;
  58   }
  59   return safe;
  60 }
  61 
  62 bool frame::is_interpreted_frame() const  {
  63   return Interpreter::contains(pc());
  64 }
  65 
  66 frame frame::sender_for_entry_frame(RegisterMap *map) const {
  67   assert(map != NULL, "map must be set");
  68   // Java frame called from C; skip all C frames and return top C
  69   // frame of that chunk as the sender.
  70   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
  71   assert(!entry_frame_is_first(), "next Java fp must be non zero");
  72   assert(jfa->last_Java_sp() > _sp, "must be above this frame on stack");
  73   map->clear();
  74   assert(map->include_argument_oops(), "should be set by clear");
  75 
  76   if (jfa->last_Java_pc() != NULL) {
  77     frame fr(jfa->last_Java_sp(), jfa->last_Java_pc());
  78     return fr;
  79   }
  80   // Last_java_pc is not set, if we come here from compiled code. The
  81   // constructor retrieves the PC from the stack.
  82   frame fr(jfa->last_Java_sp());
  83   return fr;
  84 }
  85 
  86 frame frame::sender_for_interpreter_frame(RegisterMap *map) const {
  87   // Pass callers initial_caller_sp as unextended_sp.
  88   return frame(sender_sp(), sender_pc(),
  89                CC_INTERP_ONLY((intptr_t*)((parent_ijava_frame_abi *)callers_abi())->initial_caller_sp)
  90                NOT_CC_INTERP((intptr_t*)get_ijava_state()->sender_sp)
  91                );
  92 }
  93 
  94 frame frame::sender_for_compiled_frame(RegisterMap *map) const {
  95   assert(map != NULL, "map must be set");
  96 
  97   // Frame owned by compiler.
  98   address pc = *compiled_sender_pc_addr(_cb);
  99   frame caller(compiled_sender_sp(_cb), pc);
 100 
 101   // Now adjust the map.
 102 
 103   // Get the rest.
 104   if (map->update_map()) {
 105     // Tell GC to use argument oopmaps for some runtime stubs that need it.
 106     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
 107     if (_cb->oop_maps() != NULL) {
 108       OopMapSet::update_register_map(this, map);
 109     }
 110   }
 111 
 112   return caller;
 113 }
 114 
 115 intptr_t* frame::compiled_sender_sp(CodeBlob* cb) const {
 116   return sender_sp();
 117 }
 118 
 119 address* frame::compiled_sender_pc_addr(CodeBlob* cb) const {
 120   return sender_pc_addr();
 121 }
 122 
 123 frame frame::sender(RegisterMap* map) const {
 124   // Default is we do have to follow them. The sender_for_xxx will
 125   // update it accordingly.
 126   map->set_include_argument_oops(false);
 127 
 128   if (is_entry_frame())       return sender_for_entry_frame(map);
 129   if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
 130   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
 131 
 132   if (_cb != NULL) {
 133     return sender_for_compiled_frame(map);
 134   }
 135   // Must be native-compiled frame, i.e. the marshaling code for native
 136   // methods that exists in the core system.
 137   return frame(sender_sp(), sender_pc());
 138 }
 139 
 140 void frame::patch_pc(Thread* thread, address pc) {
 141   if (TracePcPatching) {
 142     tty->print_cr("patch_pc at address " PTR_FORMAT " [" PTR_FORMAT " -> " PTR_FORMAT "]",
 143                   p2i(&((address*) _sp)[-1]), p2i(((address*) _sp)[-1]), p2i(pc));
 144   }
 145   own_abi()->lr = (uint64_t)pc;
 146   _cb = CodeCache::find_blob(pc);
 147   if (_cb != NULL && _cb->is_nmethod() && ((nmethod*)_cb)->is_deopt_pc(_pc)) {
 148     address orig = (((nmethod*)_cb)->get_original_pc(this));
 149     assert(orig == _pc, "expected original to be stored before patching");
 150     _deopt_state = is_deoptimized;
 151     // Leave _pc as is.
 152   } else {
 153     _deopt_state = not_deoptimized;
 154     _pc = pc;
 155   }
 156 }
 157 
 158 void frame::pd_gc_epilog() {
 159   if (is_interpreted_frame()) {
 160     // Set constant pool cache entry for interpreter.
 161     Method* m = interpreter_frame_method();
 162 
 163     *interpreter_frame_cpoolcache_addr() = m->constants()->cache();
 164   }
 165 }
 166 
 167 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
 168   // Is there anything to do?
 169   assert(is_interpreted_frame(), "Not an interpreted frame");
 170   return true;
 171 }
 172 
 173 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
 174   assert(is_interpreted_frame(), "interpreted frame expected");
 175   Method* method = interpreter_frame_method();
 176   BasicType type = method->result_type();
 177 
 178   if (method->is_native()) {
 179     // Prior to calling into the runtime to notify the method exit the possible
 180     // result value is saved into the interpreter frame.
 181 #ifdef CC_INTERP
 182     interpreterState istate = get_interpreterState();
 183     address lresult = (address)istate + in_bytes(BytecodeInterpreter::native_lresult_offset());
 184     address fresult = (address)istate + in_bytes(BytecodeInterpreter::native_fresult_offset());
 185 #else
 186     address lresult = (address)&(get_ijava_state()->lresult);
 187     address fresult = (address)&(get_ijava_state()->fresult);
 188 #endif
 189 
 190     switch (method->result_type()) {
 191       case T_OBJECT:
 192       case T_ARRAY: {
 193         *oop_result = JNIHandles::resolve(*(jobject*)lresult);
 194         break;
 195       }
 196       // We use std/stfd to store the values.
 197       case T_BOOLEAN : value_result->z = (jboolean) *(unsigned long*)lresult; break;
 198       case T_INT     : value_result->i = (jint)     *(long*)lresult;          break;
 199       case T_CHAR    : value_result->c = (jchar)    *(unsigned long*)lresult; break;
 200       case T_SHORT   : value_result->s = (jshort)   *(long*)lresult;          break;
 201       case T_BYTE    : value_result->z = (jbyte)    *(long*)lresult;          break;
 202       case T_LONG    : value_result->j = (jlong)    *(long*)lresult;          break;
 203       case T_FLOAT   : value_result->f = (jfloat)   *(double*)fresult;        break;
 204       case T_DOUBLE  : value_result->d = (jdouble)  *(double*)fresult;        break;
 205       case T_VOID    : /* Nothing to do */ break;
 206       default        : ShouldNotReachHere();
 207     }
 208   } else {
 209     intptr_t* tos_addr = interpreter_frame_tos_address();
 210     switch (method->result_type()) {
 211       case T_OBJECT:
 212       case T_ARRAY: {
 213         oop obj = *(oop*)tos_addr;
 214         assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 215         *oop_result = obj;
 216       }
 217       case T_BOOLEAN : value_result->z = (jboolean) *(jint*)tos_addr; break;
 218       case T_BYTE    : value_result->b = (jbyte) *(jint*)tos_addr; break;
 219       case T_CHAR    : value_result->c = (jchar) *(jint*)tos_addr; break;
 220       case T_SHORT   : value_result->s = (jshort) *(jint*)tos_addr; break;
 221       case T_INT     : value_result->i = *(jint*)tos_addr; break;
 222       case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
 223       case T_FLOAT   : value_result->f = *(jfloat*)tos_addr; break;
 224       case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
 225       case T_VOID    : /* Nothing to do */ break;
 226       default        : ShouldNotReachHere();
 227     }
 228   }
 229   return type;
 230 }
 231 
 232 #ifndef PRODUCT
 233 
 234 void frame::describe_pd(FrameValues& values, int frame_no) {
 235   if (is_interpreted_frame()) {
 236 #ifdef CC_INTERP
 237     interpreterState istate = get_interpreterState();
 238     values.describe(frame_no, (intptr_t*)istate, "istate");
 239     values.describe(frame_no, (intptr_t*)&(istate->_thread), " thread");
 240     values.describe(frame_no, (intptr_t*)&(istate->_bcp), " bcp");
 241     values.describe(frame_no, (intptr_t*)&(istate->_locals), " locals");
 242     values.describe(frame_no, (intptr_t*)&(istate->_constants), " constants");
 243     values.describe(frame_no, (intptr_t*)&(istate->_method), err_msg(" method = %s", istate->_method->name_and_sig_as_C_string()));
 244     values.describe(frame_no, (intptr_t*)&(istate->_mdx), " mdx");
 245     values.describe(frame_no, (intptr_t*)&(istate->_stack), " stack");
 246     values.describe(frame_no, (intptr_t*)&(istate->_msg), err_msg(" msg = %s", BytecodeInterpreter::C_msg(istate->_msg)));
 247     values.describe(frame_no, (intptr_t*)&(istate->_result), " result");
 248     values.describe(frame_no, (intptr_t*)&(istate->_prev_link), " prev_link");
 249     values.describe(frame_no, (intptr_t*)&(istate->_oop_temp), " oop_temp");
 250     values.describe(frame_no, (intptr_t*)&(istate->_stack_base), " stack_base");
 251     values.describe(frame_no, (intptr_t*)&(istate->_stack_limit), " stack_limit");
 252     values.describe(frame_no, (intptr_t*)&(istate->_monitor_base), " monitor_base");
 253     values.describe(frame_no, (intptr_t*)&(istate->_frame_bottom), " frame_bottom");
 254     values.describe(frame_no, (intptr_t*)&(istate->_last_Java_pc), " last_Java_pc");
 255     values.describe(frame_no, (intptr_t*)&(istate->_last_Java_fp), " last_Java_fp");
 256     values.describe(frame_no, (intptr_t*)&(istate->_last_Java_sp), " last_Java_sp");
 257     values.describe(frame_no, (intptr_t*)&(istate->_self_link), " self_link");
 258     values.describe(frame_no, (intptr_t*)&(istate->_native_fresult), " native_fresult");
 259     values.describe(frame_no, (intptr_t*)&(istate->_native_lresult), " native_lresult");
 260 #else
 261 #define DESCRIBE_ADDRESS(name) \
 262   values.describe(frame_no, (intptr_t*)&(get_ijava_state()->name), #name);
 263 
 264       DESCRIBE_ADDRESS(method);
 265       DESCRIBE_ADDRESS(locals);
 266       DESCRIBE_ADDRESS(monitors);
 267       DESCRIBE_ADDRESS(cpoolCache);
 268       DESCRIBE_ADDRESS(bcp);
 269       DESCRIBE_ADDRESS(esp);
 270       DESCRIBE_ADDRESS(mdx);
 271       DESCRIBE_ADDRESS(top_frame_sp);
 272       DESCRIBE_ADDRESS(sender_sp);
 273       DESCRIBE_ADDRESS(oop_tmp);
 274       DESCRIBE_ADDRESS(lresult);
 275       DESCRIBE_ADDRESS(fresult);
 276 #endif
 277   }
 278 }
 279 #endif
 280 
 281 void frame::adjust_unextended_sp() {
 282   // If we are returning to a compiled MethodHandle call site, the
 283   // saved_fp will in fact be a saved value of the unextended SP. The
 284   // simplest way to tell whether we are returning to such a call site
 285   // is as follows:
 286 
 287   if (is_compiled_frame() && false /*is_at_mh_callsite()*/) {  // TODO PPC port
 288     // If the sender PC is a deoptimization point, get the original
 289     // PC. For MethodHandle call site the unextended_sp is stored in
 290     // saved_fp.
 291     _unextended_sp = _fp - _cb->frame_size();
 292 
 293 #ifdef ASSERT
 294     nmethod *sender_nm = _cb->as_nmethod_or_null();
 295     assert(sender_nm && *_sp == *_unextended_sp, "backlink changed");
 296 
 297     intptr_t* sp = _unextended_sp;  // check if stack can be walked from here
 298     for (int x = 0; x < 5; ++x) {   // check up to a couple of backlinks
 299       intptr_t* prev_sp = *(intptr_t**)sp;
 300       if (prev_sp == 0) break;      // end of stack
 301       assert(prev_sp>sp, "broken stack");
 302       sp = prev_sp;
 303     }
 304 
 305     if (sender_nm->is_deopt_mh_entry(_pc)) { // checks for deoptimization
 306       address original_pc = sender_nm->get_original_pc(this);
 307       assert(sender_nm->insts_contains(original_pc), "original PC must be in nmethod");
 308       assert(sender_nm->is_method_handle_return(original_pc), "must be");
 309     }
 310 #endif
 311   }
 312 }
 313 
 314 intptr_t *frame::initial_deoptimization_info() {
 315   // unused... but returns fp() to minimize changes introduced by 7087445
 316   return fp();
 317 }
 318 
 319 #ifndef PRODUCT
 320 // This is a generic constructor which is only used by pns() in debug.cpp.
 321 frame::frame(void* sp, void* fp, void* pc) : _sp((intptr_t*)sp), _unextended_sp((intptr_t*)sp) {
 322   find_codeblob_and_set_pc_and_deopt_state((address)pc); // also sets _fp and adjusts _unextended_sp
 323 }
 324 #endif