1 /*
   2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2013 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 #ifndef CC_INTERP
  46 #error "CC_INTERP must be defined on PPC64"
  47 #endif
  48 
  49 #ifdef ASSERT
  50 void RegisterMap::check_location_valid() {
  51 }
  52 #endif // ASSERT
  53 
  54 bool frame::safe_for_sender(JavaThread *thread) {
  55   bool safe = false;
  56   address   cursp = (address)sp();
  57   address   curfp = (address)fp();
  58   if ((cursp != NULL && curfp != NULL &&
  59       (cursp <= thread->stack_base() && cursp >= thread->stack_base() - thread->stack_size())) &&
  60       (curfp <= thread->stack_base() && curfp >= thread->stack_base() - thread->stack_size())) {
  61       safe = true;
  62   }
  63   return safe;
  64 }
  65 
  66 bool frame::is_interpreted_frame() const  {
  67   return Interpreter::contains(pc());
  68 }
  69 
  70 frame frame::sender_for_entry_frame(RegisterMap *map) const {
  71   assert(map != NULL, "map must be set");
  72   // Java frame called from C; skip all C frames and return top C
  73   // frame of that chunk as the sender.
  74   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
  75   assert(!entry_frame_is_first(), "next Java fp must be non zero");
  76   assert(jfa->last_Java_sp() > _sp, "must be above this frame on stack");
  77   map->clear();
  78   assert(map->include_argument_oops(), "should be set by clear");
  79 
  80   if (jfa->last_Java_pc() != NULL) {
  81     frame fr(jfa->last_Java_sp(), jfa->last_Java_pc());
  82     return fr;
  83   }
  84   // Last_java_pc is not set, if we come here from compiled code. The
  85   // constructor retrieves the PC from the stack.
  86   frame fr(jfa->last_Java_sp());
  87   return fr;
  88 }
  89 
  90 frame frame::sender_for_interpreter_frame(RegisterMap *map) const {
  91   // Pass callers initial_caller_sp as unextended_sp.
  92   return frame(sender_sp(), sender_pc(), (intptr_t*)((parent_ijava_frame_abi *)callers_abi())->initial_caller_sp);
  93 }
  94 
  95 frame frame::sender_for_compiled_frame(RegisterMap *map) const {
  96   assert(map != NULL, "map must be set");
  97 
  98   // Frame owned by compiler.
  99   address pc = *compiled_sender_pc_addr(_cb);
 100   frame caller(compiled_sender_sp(_cb), pc);
 101 
 102   // Now adjust the map.
 103 
 104   // Get the rest.
 105   if (map->update_map()) {
 106     // Tell GC to use argument oopmaps for some runtime stubs that need it.
 107     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
 108     if (_cb->oop_maps() != NULL) {
 109       OopMapSet::update_register_map(this, map);
 110     }
 111   }
 112 
 113   return caller;
 114 }
 115 
 116 intptr_t* frame::compiled_sender_sp(CodeBlob* cb) const {
 117   return sender_sp();
 118 }
 119 
 120 address* frame::compiled_sender_pc_addr(CodeBlob* cb) const {
 121   return sender_pc_addr();
 122 }
 123 
 124 frame frame::sender(RegisterMap* map) const {
 125   // Default is we do have to follow them. The sender_for_xxx will
 126   // update it accordingly.
 127   map->set_include_argument_oops(false);
 128 
 129   if (is_entry_frame())       return sender_for_entry_frame(map);
 130   if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
 131   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
 132 
 133   if (_cb != NULL) {
 134     return sender_for_compiled_frame(map);
 135   }
 136   // Must be native-compiled frame, i.e. the marshaling code for native
 137   // methods that exists in the core system.
 138   return frame(sender_sp(), sender_pc());
 139 }
 140 
 141 void frame::patch_pc(Thread* thread, address pc) {
 142   if (TracePcPatching) {
 143     tty->print_cr("patch_pc at address " PTR_FORMAT " [" PTR_FORMAT " -> " PTR_FORMAT "]",
 144                   &((address*) _sp)[-1], ((address*) _sp)[-1], pc);
 145   }
 146   own_abi()->lr = (uint64_t)pc;
 147   _cb = CodeCache::find_blob(pc);
 148   if (_cb != NULL && _cb->is_nmethod() && ((nmethod*)_cb)->is_deopt_pc(_pc)) {
 149     address orig = (((nmethod*)_cb)->get_original_pc(this));
 150     assert(orig == _pc, "expected original to be stored before patching");
 151     _deopt_state = is_deoptimized;
 152     // Leave _pc as is.
 153   } else {
 154     _deopt_state = not_deoptimized;
 155     _pc = pc;
 156   }
 157 }
 158 
 159 void frame::pd_gc_epilog() {
 160   if (is_interpreted_frame()) {
 161     // Set constant pool cache entry for interpreter.
 162     Method* m = interpreter_frame_method();
 163 
 164     *interpreter_frame_cpoolcache_addr() = m->constants()->cache();
 165   }
 166 }
 167 
 168 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
 169   // Is there anything to do?
 170   assert(is_interpreted_frame(), "Not an interpreted frame");
 171   return true;
 172 }
 173 
 174 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
 175   assert(is_interpreted_frame(), "interpreted frame expected");
 176   Method* method = interpreter_frame_method();
 177   BasicType type = method->result_type();
 178 
 179 #ifdef CC_INTERP
 180   if (method->is_native()) {
 181     // Prior to calling into the runtime to notify the method exit the possible
 182     // result value is saved into the interpreter frame.
 183     interpreterState istate = get_interpreterState();
 184     address lresult = (address)istate + in_bytes(BytecodeInterpreter::native_lresult_offset());
 185     address fresult = (address)istate + in_bytes(BytecodeInterpreter::native_fresult_offset());
 186 
 187     switch (method->result_type()) {
 188       case T_OBJECT:
 189       case T_ARRAY: {
 190         oop* obj_p = *(oop**)lresult;
 191         oop obj = (obj_p == NULL) ? NULL : *obj_p;
 192         assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 193         *oop_result = obj;
 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 #else
 230   Unimplemented();
 231 #endif
 232   return type;
 233 }
 234 
 235 #ifndef PRODUCT
 236 
 237 void frame::describe_pd(FrameValues& values, int frame_no) {
 238   if (is_interpreted_frame()) {
 239 #ifdef CC_INTERP
 240     interpreterState istate = get_interpreterState();
 241     values.describe(frame_no, (intptr_t*)istate, "istate");
 242     values.describe(frame_no, (intptr_t*)&(istate->_thread), " thread");
 243     values.describe(frame_no, (intptr_t*)&(istate->_bcp), " bcp");
 244     values.describe(frame_no, (intptr_t*)&(istate->_locals), " locals");
 245     values.describe(frame_no, (intptr_t*)&(istate->_constants), " constants");
 246     values.describe(frame_no, (intptr_t*)&(istate->_method), err_msg(" method = %s", istate->_method->name_and_sig_as_C_string()));
 247     values.describe(frame_no, (intptr_t*)&(istate->_mdx), " mdx");
 248     values.describe(frame_no, (intptr_t*)&(istate->_stack), " stack");
 249     values.describe(frame_no, (intptr_t*)&(istate->_msg), err_msg(" msg = %s", BytecodeInterpreter::C_msg(istate->_msg)));
 250     values.describe(frame_no, (intptr_t*)&(istate->_result), " result");
 251     values.describe(frame_no, (intptr_t*)&(istate->_prev_link), " prev_link");
 252     values.describe(frame_no, (intptr_t*)&(istate->_oop_temp), " oop_temp");
 253     values.describe(frame_no, (intptr_t*)&(istate->_stack_base), " stack_base");
 254     values.describe(frame_no, (intptr_t*)&(istate->_stack_limit), " stack_limit");
 255     values.describe(frame_no, (intptr_t*)&(istate->_monitor_base), " monitor_base");
 256     values.describe(frame_no, (intptr_t*)&(istate->_frame_bottom), " frame_bottom");
 257     values.describe(frame_no, (intptr_t*)&(istate->_last_Java_pc), " last_Java_pc");
 258     values.describe(frame_no, (intptr_t*)&(istate->_last_Java_fp), " last_Java_fp");
 259     values.describe(frame_no, (intptr_t*)&(istate->_last_Java_sp), " last_Java_sp");
 260     values.describe(frame_no, (intptr_t*)&(istate->_self_link), " self_link");
 261     values.describe(frame_no, (intptr_t*)&(istate->_native_fresult), " native_fresult");
 262     values.describe(frame_no, (intptr_t*)&(istate->_native_lresult), " native_lresult");
 263 #else
 264     Unimplemented();
 265 #endif
 266   }
 267 }
 268 #endif
 269 
 270 void frame::adjust_unextended_sp() {
 271   // If we are returning to a compiled MethodHandle call site, the
 272   // saved_fp will in fact be a saved value of the unextended SP. The
 273   // simplest way to tell whether we are returning to such a call site
 274   // is as follows:
 275 
 276   if (is_compiled_frame() && false /*is_at_mh_callsite()*/) {  // TODO PPC port
 277     // If the sender PC is a deoptimization point, get the original
 278     // PC. For MethodHandle call site the unextended_sp is stored in
 279     // saved_fp.
 280     _unextended_sp = _fp - _cb->frame_size();
 281 
 282 #ifdef ASSERT
 283     nmethod *sender_nm = _cb->as_nmethod_or_null();
 284     assert(sender_nm && *_sp == *_unextended_sp, "backlink changed");
 285 
 286     intptr_t* sp = _unextended_sp;  // check if stack can be walked from here
 287     for (int x = 0; x < 5; ++x) {   // check up to a couple of backlinks
 288       intptr_t* prev_sp = *(intptr_t**)sp;
 289       if (prev_sp == 0) break;      // end of stack
 290       assert(prev_sp>sp, "broken stack");
 291       sp = prev_sp;
 292     }
 293 
 294     if (sender_nm->is_deopt_mh_entry(_pc)) { // checks for deoptimization
 295       address original_pc = sender_nm->get_original_pc(this);
 296       assert(sender_nm->insts_contains(original_pc), "original PC must be in nmethod");
 297       assert(sender_nm->is_method_handle_return(original_pc), "must be");
 298     }
 299 #endif
 300   }
 301 }
 302 
 303 intptr_t *frame::initial_deoptimization_info() {
 304   // unused... but returns fp() to minimize changes introduced by 7087445
 305   return fp();
 306 }