1 /*
   2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2017 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 #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/jniHandles.inline.hpp"
  36 #include "runtime/monitorChunk.hpp"
  37 #include "runtime/signature.hpp"
  38 #include "runtime/stubCodeGenerator.hpp"
  39 #include "runtime/stubRoutines.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(), (intptr_t*)get_ijava_state()->sender_sp);
  89 }
  90 
  91 frame frame::sender_for_compiled_frame(RegisterMap *map) const {
  92   assert(map != NULL, "map must be set");
  93 
  94   // Frame owned by compiler.
  95   address pc = *compiled_sender_pc_addr(_cb);
  96   frame caller(compiled_sender_sp(_cb), pc);
  97 
  98   // Now adjust the map.
  99 
 100   // Get the rest.
 101   if (map->update_map()) {
 102     // Tell GC to use argument oopmaps for some runtime stubs that need it.
 103     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
 104     if (_cb->oop_maps() != NULL) {
 105       OopMapSet::update_register_map(this, map);
 106     }
 107   }
 108 
 109   return caller;
 110 }
 111 
 112 intptr_t* frame::compiled_sender_sp(CodeBlob* cb) const {
 113   return sender_sp();
 114 }
 115 
 116 address* frame::compiled_sender_pc_addr(CodeBlob* cb) const {
 117   return sender_pc_addr();
 118 }
 119 
 120 frame frame::sender(RegisterMap* map) const {
 121   // Default is we do have to follow them. The sender_for_xxx will
 122   // update it accordingly.
 123   map->set_include_argument_oops(false);
 124 
 125   if (is_entry_frame())       return sender_for_entry_frame(map);
 126   if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
 127   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
 128 
 129   if (_cb != NULL) {
 130     return sender_for_compiled_frame(map);
 131   }
 132   // Must be native-compiled frame, i.e. the marshaling code for native
 133   // methods that exists in the core system.
 134   return frame(sender_sp(), sender_pc());
 135 }
 136 
 137 void frame::patch_pc(Thread* thread, address pc) {
 138   if (TracePcPatching) {
 139     tty->print_cr("patch_pc at address " PTR_FORMAT " [" PTR_FORMAT " -> " PTR_FORMAT "]",
 140                   p2i(&((address*) _sp)[-1]), p2i(((address*) _sp)[-1]), p2i(pc));
 141   }
 142   own_abi()->lr = (uint64_t)pc;
 143   _cb = CodeCache::find_blob(pc);
 144   if (_cb != NULL && _cb->is_nmethod() && ((nmethod*)_cb)->is_deopt_pc(_pc)) {
 145     address orig = (((nmethod*)_cb)->get_original_pc(this));
 146     assert(orig == _pc, "expected original to be stored before patching");
 147     _deopt_state = is_deoptimized;
 148     // Leave _pc as is.
 149   } else {
 150     _deopt_state = not_deoptimized;
 151     _pc = pc;
 152   }
 153 }
 154 
 155 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
 156   // Is there anything to do?
 157   assert(is_interpreted_frame(), "Not an interpreted frame");
 158   return true;
 159 }
 160 
 161 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
 162   assert(is_interpreted_frame(), "interpreted frame expected");
 163   Method* method = interpreter_frame_method();
 164   BasicType type = method->result_type();
 165 
 166   if (method->is_native()) {
 167     // Prior to calling into the runtime to notify the method exit the possible
 168     // result value is saved into the interpreter frame.
 169     address lresult = (address)&(get_ijava_state()->lresult);
 170     address fresult = (address)&(get_ijava_state()->fresult);
 171 
 172     switch (method->result_type()) {
 173       case T_OBJECT:
 174       case T_ARRAY: {
 175         *oop_result = JNIHandles::resolve(*(jobject*)lresult);
 176         break;
 177       }
 178       // We use std/stfd to store the values.
 179       case T_BOOLEAN : value_result->z = (jboolean) *(unsigned long*)lresult; break;
 180       case T_INT     : value_result->i = (jint)     *(long*)lresult;          break;
 181       case T_CHAR    : value_result->c = (jchar)    *(unsigned long*)lresult; break;
 182       case T_SHORT   : value_result->s = (jshort)   *(long*)lresult;          break;
 183       case T_BYTE    : value_result->z = (jbyte)    *(long*)lresult;          break;
 184       case T_LONG    : value_result->j = (jlong)    *(long*)lresult;          break;
 185       case T_FLOAT   : value_result->f = (jfloat)   *(double*)fresult;        break;
 186       case T_DOUBLE  : value_result->d = (jdouble)  *(double*)fresult;        break;
 187       case T_VOID    : /* Nothing to do */ break;
 188       default        : ShouldNotReachHere();
 189     }
 190   } else {
 191     intptr_t* tos_addr = interpreter_frame_tos_address();
 192     switch (method->result_type()) {
 193       case T_OBJECT:
 194       case T_ARRAY: {
 195         oop obj = *(oop*)tos_addr;
 196         assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 197         *oop_result = obj;
 198       }
 199       case T_BOOLEAN : value_result->z = (jboolean) *(jint*)tos_addr; break;
 200       case T_BYTE    : value_result->b = (jbyte) *(jint*)tos_addr; break;
 201       case T_CHAR    : value_result->c = (jchar) *(jint*)tos_addr; break;
 202       case T_SHORT   : value_result->s = (jshort) *(jint*)tos_addr; break;
 203       case T_INT     : value_result->i = *(jint*)tos_addr; break;
 204       case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
 205       case T_FLOAT   : value_result->f = *(jfloat*)tos_addr; break;
 206       case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
 207       case T_VOID    : /* Nothing to do */ break;
 208       default        : ShouldNotReachHere();
 209     }
 210   }
 211   return type;
 212 }
 213 
 214 #ifndef PRODUCT
 215 
 216 void frame::describe_pd(FrameValues& values, int frame_no) {
 217   if (is_interpreted_frame()) {
 218 #define DESCRIBE_ADDRESS(name) \
 219   values.describe(frame_no, (intptr_t*)&(get_ijava_state()->name), #name);
 220 
 221       DESCRIBE_ADDRESS(method);
 222       DESCRIBE_ADDRESS(mirror);
 223       DESCRIBE_ADDRESS(locals);
 224       DESCRIBE_ADDRESS(monitors);
 225       DESCRIBE_ADDRESS(cpoolCache);
 226       DESCRIBE_ADDRESS(bcp);
 227       DESCRIBE_ADDRESS(esp);
 228       DESCRIBE_ADDRESS(mdx);
 229       DESCRIBE_ADDRESS(top_frame_sp);
 230       DESCRIBE_ADDRESS(sender_sp);
 231       DESCRIBE_ADDRESS(oop_tmp);
 232       DESCRIBE_ADDRESS(lresult);
 233       DESCRIBE_ADDRESS(fresult);
 234   }
 235 }
 236 #endif
 237 
 238 intptr_t *frame::initial_deoptimization_info() {
 239   // unused... but returns fp() to minimize changes introduced by 7087445
 240   return fp();
 241 }
 242 
 243 #ifndef PRODUCT
 244 // This is a generic constructor which is only used by pns() in debug.cpp.
 245 frame::frame(void* sp, void* fp, void* pc) : _sp((intptr_t*)sp), _unextended_sp((intptr_t*)sp) {
 246   find_codeblob_and_set_pc_and_deopt_state((address)pc); // also sets _fp and adjusts _unextended_sp
 247 }
 248 
 249 void frame::pd_ps() {}
 250 #endif