annotate src/hotspot/cpu/aarch64/frame_aarch64.cpp @ 54526:ee29b516a36a

revert changes
author jlaskey
date Wed, 23 Jan 2019 16:09:20 -0400
parents e7519209f7ff
children ce36f8180d03
rev   line source
aph@29183 1 /*
hseigel@51955 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
aph@29183 3 * Copyright (c) 2014, Red Hat Inc. All rights reserved.
aph@29183 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aph@29183 5 *
aph@29183 6 * This code is free software; you can redistribute it and/or modify it
aph@29183 7 * under the terms of the GNU General Public License version 2 only, as
aph@29183 8 * published by the Free Software Foundation.
aph@29183 9 *
aph@29183 10 * This code is distributed in the hope that it will be useful, but WITHOUT
aph@29183 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aph@29183 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aph@29183 13 * version 2 for more details (a copy is included in the LICENSE file that
aph@29183 14 * accompanied this code).
aph@29183 15 *
aph@29183 16 * You should have received a copy of the GNU General Public License version
aph@29183 17 * 2 along with this work; if not, write to the Free Software Foundation,
aph@29183 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aph@29183 19 *
aph@29183 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aph@29183 21 * or visit www.oracle.com if you need additional information or have any
aph@29183 22 * questions.
aph@29183 23 *
aph@29183 24 */
aph@29183 25
aph@29183 26 #include "precompiled.hpp"
aph@29183 27 #include "interpreter/interpreter.hpp"
aph@29183 28 #include "memory/resourceArea.hpp"
aph@29183 29 #include "oops/markOop.hpp"
aph@29183 30 #include "oops/method.hpp"
aph@29183 31 #include "oops/oop.inline.hpp"
aph@29183 32 #include "prims/methodHandles.hpp"
aph@29183 33 #include "runtime/frame.inline.hpp"
aph@29183 34 #include "runtime/handles.inline.hpp"
aph@29183 35 #include "runtime/javaCalls.hpp"
aph@29183 36 #include "runtime/monitorChunk.hpp"
aph@29183 37 #include "runtime/os.hpp"
aph@29183 38 #include "runtime/signature.hpp"
aph@29183 39 #include "runtime/stubCodeGenerator.hpp"
aph@29183 40 #include "runtime/stubRoutines.hpp"
aph@29183 41 #include "vmreg_aarch64.inline.hpp"
aph@29183 42 #ifdef COMPILER1
aph@29183 43 #include "c1/c1_Runtime1.hpp"
aph@29183 44 #include "runtime/vframeArray.hpp"
aph@29183 45 #endif
aph@29183 46
aph@29183 47 #ifdef ASSERT
aph@29183 48 void RegisterMap::check_location_valid() {
aph@29183 49 }
aph@29183 50 #endif
aph@29183 51
aph@29183 52
aph@29183 53 // Profiling/safepoint support
aph@29183 54
aph@29183 55 bool frame::safe_for_sender(JavaThread *thread) {
aph@29183 56 address sp = (address)_sp;
aph@29183 57 address fp = (address)_fp;
aph@29183 58 address unextended_sp = (address)_unextended_sp;
aph@29183 59
aph@29183 60 // consider stack guards when trying to determine "safe" stack pointers
goetz@35201 61 static size_t stack_guard_size = os::uses_stack_guard_pages() ?
goetz@35201 62 (JavaThread::stack_red_zone_size() + JavaThread::stack_yellow_zone_size()) : 0;
aph@29183 63 size_t usable_stack_size = thread->stack_size() - stack_guard_size;
aph@29183 64
aph@29183 65 // sp must be within the usable part of the stack (not in guards)
aph@29183 66 bool sp_safe = (sp < thread->stack_base()) &&
aph@29183 67 (sp >= thread->stack_base() - usable_stack_size);
aph@29183 68
aph@29183 69
aph@29183 70 if (!sp_safe) {
aph@29183 71 return false;
aph@29183 72 }
aph@29183 73
dsamersoff@51377 74 // When we are running interpreted code the machine stack pointer, SP, is
dsamersoff@51377 75 // set low enough so that the Java expression stack can grow and shrink
dsamersoff@51377 76 // without ever exceeding the machine stack bounds. So, ESP >= SP.
dsamersoff@51377 77
dsamersoff@51377 78 // When we call out of an interpreted method, SP is incremented so that
dsamersoff@51377 79 // the space between SP and ESP is removed. The SP saved in the callee's
dsamersoff@51377 80 // frame is the SP *before* this increment. So, when we walk a stack of
dsamersoff@51377 81 // interpreter frames the sender's SP saved in a frame might be less than
dsamersoff@51377 82 // the SP at the point of call.
dsamersoff@51377 83
dsamersoff@51377 84 // So unextended sp must be within the stack but we need not to check
dsamersoff@51377 85 // that unextended sp >= sp
dsamersoff@51377 86
dsamersoff@51377 87 bool unextended_sp_safe = (unextended_sp < thread->stack_base());
aph@29183 88
aph@29183 89 if (!unextended_sp_safe) {
aph@29183 90 return false;
aph@29183 91 }
aph@29183 92
aph@29183 93 // an fp must be within the stack and above (but not equal) sp
aph@29183 94 // second evaluation on fp+ is added to handle situation where fp is -1
aph@29183 95 bool fp_safe = (fp < thread->stack_base() && (fp > sp) && (((fp + (return_addr_offset * sizeof(void*))) < thread->stack_base())));
aph@29183 96
aph@29183 97 // We know sp/unextended_sp are safe only fp is questionable here
aph@29183 98
aph@29183 99 // If the current frame is known to the code cache then we can attempt to
aph@29183 100 // to construct the sender and do some validation of it. This goes a long way
aph@29183 101 // toward eliminating issues when we get in frame construction code
aph@29183 102
aph@29183 103 if (_cb != NULL ) {
aph@29183 104
aph@29183 105 // First check if frame is complete and tester is reliable
aph@29183 106 // Unfortunately we can only check frame complete for runtime stubs and nmethod
aph@29183 107 // other generic buffer blobs are more problematic so we just assume they are
aph@29183 108 // ok. adapter blobs never have a frame complete and are never ok.
aph@29183 109
aph@29183 110 if (!_cb->is_frame_complete_at(_pc)) {
aph@29183 111 if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
aph@29183 112 return false;
aph@29183 113 }
aph@29183 114 }
aph@29183 115
aph@29183 116 // Could just be some random pointer within the codeBlob
aph@29183 117 if (!_cb->code_contains(_pc)) {
aph@29183 118 return false;
aph@29183 119 }
aph@29183 120
aph@29183 121 // Entry frame checks
aph@29183 122 if (is_entry_frame()) {
aph@29183 123 // an entry frame must have a valid fp.
coleenp@40332 124 return fp_safe && is_entry_frame_valid(thread);
aph@29183 125 }
aph@29183 126
aph@29183 127 intptr_t* sender_sp = NULL;
aph@29192 128 intptr_t* sender_unextended_sp = NULL;
aph@29183 129 address sender_pc = NULL;
aph@29192 130 intptr_t* saved_fp = NULL;
aph@29183 131
aph@29183 132 if (is_interpreted_frame()) {
aph@29183 133 // fp must be safe
aph@29183 134 if (!fp_safe) {
aph@29183 135 return false;
aph@29183 136 }
aph@29183 137
aph@29183 138 sender_pc = (address) this->fp()[return_addr_offset];
aph@29192 139 // for interpreted frames, the value below is the sender "raw" sp,
aph@29192 140 // which can be different from the sender unextended sp (the sp seen
aph@29192 141 // by the sender) because of current frame local variables
aph@29183 142 sender_sp = (intptr_t*) addr_at(sender_sp_offset);
aph@29192 143 sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
aph@29192 144 saved_fp = (intptr_t*) this->fp()[link_offset];
aph@29183 145
aph@29183 146 } else {
aph@29183 147 // must be some sort of compiled/runtime frame
aph@29183 148 // fp does not have to be safe (although it could be check for c1?)
aph@29183 149
aph@29183 150 // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
aph@29183 151 if (_cb->frame_size() <= 0) {
aph@29183 152 return false;
aph@29183 153 }
aph@29183 154
aph@29183 155 sender_sp = _unextended_sp + _cb->frame_size();
dlong@42042 156 // Is sender_sp safe?
dlong@42042 157 if ((address)sender_sp >= thread->stack_base()) {
dlong@42042 158 return false;
dlong@42042 159 }
aph@29192 160 sender_unextended_sp = sender_sp;
aph@29183 161 sender_pc = (address) *(sender_sp-1);
aph@29192 162 // Note: frame::sender_sp_offset is only valid for compiled frame
aph@29192 163 saved_fp = (intptr_t*) *(sender_sp - frame::sender_sp_offset);
aph@29183 164 }
aph@29183 165
aph@29183 166
aph@29183 167 // If the potential sender is the interpreter then we can do some more checking
aph@29183 168 if (Interpreter::contains(sender_pc)) {
aph@29183 169
aph@29183 170 // fp is always saved in a recognizable place in any code we generate. However
aph@29183 171 // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
aph@29183 172 // is really a frame pointer.
aph@29183 173
aph@29183 174 bool saved_fp_safe = ((address)saved_fp < thread->stack_base()) && (saved_fp > sender_sp);
aph@29183 175
aph@29183 176 if (!saved_fp_safe) {
aph@29183 177 return false;
aph@29183 178 }
aph@29183 179
aph@29183 180 // construct the potential sender
aph@29183 181
aph@29192 182 frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
aph@29183 183
aph@29183 184 return sender.is_interpreted_frame_valid(thread);
aph@29183 185
aph@29183 186 }
aph@29183 187
aph@29183 188 // We must always be able to find a recognizable pc
aph@29183 189 CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
aph@29183 190 if (sender_pc == NULL || sender_blob == NULL) {
aph@29183 191 return false;
aph@29183 192 }
aph@29183 193
aph@29183 194 // Could be a zombie method
aph@29183 195 if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
aph@29183 196 return false;
aph@29183 197 }
aph@29183 198
aph@29183 199 // Could just be some random pointer within the codeBlob
aph@29183 200 if (!sender_blob->code_contains(sender_pc)) {
aph@29183 201 return false;
aph@29183 202 }
aph@29183 203
aph@29183 204 // We should never be able to see an adapter if the current frame is something from code cache
aph@29183 205 if (sender_blob->is_adapter_blob()) {
aph@29183 206 return false;
aph@29183 207 }
aph@29183 208
aph@29183 209 // Could be the call_stub
aph@29183 210 if (StubRoutines::returns_to_call_stub(sender_pc)) {
aph@29183 211 bool saved_fp_safe = ((address)saved_fp < thread->stack_base()) && (saved_fp > sender_sp);
aph@29183 212
aph@29183 213 if (!saved_fp_safe) {
aph@29183 214 return false;
aph@29183 215 }
aph@29183 216
aph@29183 217 // construct the potential sender
cjplummer@42051 218
aph@29192 219 frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
cjplummer@42051 220
cjplummer@42051 221 // Validate the JavaCallWrapper an entry frame must have
cjplummer@42051 222 address jcw = (address)sender.entry_frame_call_wrapper();
cjplummer@42051 223
cjplummer@42051 224 bool jcw_safe = (jcw < thread->stack_base()) && (jcw > (address)sender.fp());
cjplummer@42051 225
cjplummer@42051 226 return jcw_safe;
aph@29183 227 }
aph@29183 228
rbackman@38133 229 CompiledMethod* nm = sender_blob->as_compiled_method_or_null();
rbackman@38133 230 if (nm != NULL) {
rbackman@38133 231 if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
rbackman@38133 232 nm->method()->is_method_handle_intrinsic()) {
rbackman@38133 233 return false;
rbackman@38133 234 }
aph@29183 235 }
aph@29183 236
aph@29183 237 // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
aph@29183 238 // because the return address counts against the callee's frame.
aph@29183 239
aph@29183 240 if (sender_blob->frame_size() <= 0) {
rbackman@38133 241 assert(!sender_blob->is_compiled(), "should count return address at least");
aph@29183 242 return false;
aph@29183 243 }
aph@29183 244
aph@29183 245 // We should never be able to see anything here except an nmethod. If something in the
aph@29183 246 // code cache (current frame) is called by an entity within the code cache that entity
aph@29183 247 // should not be anything but the call stub (already covered), the interpreter (already covered)
aph@29183 248 // or an nmethod.
aph@29183 249
rbackman@38133 250 if (!sender_blob->is_compiled()) {
aph@29183 251 return false;
aph@29183 252 }
aph@29183 253
aph@29183 254 // Could put some more validation for the potential non-interpreted sender
aph@29183 255 // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
aph@29183 256
aph@29183 257 // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
aph@29183 258
aph@29183 259 // We've validated the potential sender that would be created
aph@29183 260 return true;
aph@29183 261 }
aph@29183 262
aph@29183 263 // Must be native-compiled frame. Since sender will try and use fp to find
aph@29183 264 // linkages it must be safe
aph@29183 265
aph@29183 266 if (!fp_safe) {
aph@29183 267 return false;
aph@29183 268 }
aph@29183 269
aph@29183 270 // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
aph@29183 271
aph@29183 272 if ( (address) this->fp()[return_addr_offset] == NULL) return false;
aph@29183 273
aph@29183 274
aph@29183 275 // could try and do some more potential verification of native frame if we could think of some...
aph@29183 276
aph@29183 277 return true;
aph@29183 278
aph@29183 279 }
aph@29183 280
aph@29183 281 void frame::patch_pc(Thread* thread, address pc) {
aph@29183 282 address* pc_addr = &(((address*) sp())[-1]);
aph@29183 283 if (TracePcPatching) {
aph@29183 284 tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
aph@29183 285 p2i(pc_addr), p2i(*pc_addr), p2i(pc));
aph@29183 286 }
aph@29183 287 // Either the return address is the original one or we are going to
aph@29183 288 // patch in the same address that's already there.
aph@29183 289 assert(_pc == *pc_addr || pc == *pc_addr, "must be");
aph@29183 290 *pc_addr = pc;
aph@29183 291 _cb = CodeCache::find_blob(pc);
rbackman@38133 292 address original_pc = CompiledMethod::get_deopt_original_pc(this);
aph@29183 293 if (original_pc != NULL) {
aph@29183 294 assert(original_pc == _pc, "expected original PC to be stored before patching");
aph@29183 295 _deopt_state = is_deoptimized;
aph@29183 296 // leave _pc as is
aph@29183 297 } else {
aph@29183 298 _deopt_state = not_deoptimized;
aph@29183 299 _pc = pc;
aph@29183 300 }
aph@29183 301 }
aph@29183 302
aph@29183 303 bool frame::is_interpreted_frame() const {
aph@29183 304 return Interpreter::contains(pc());
aph@29183 305 }
aph@29183 306
aph@29183 307 int frame::frame_size(RegisterMap* map) const {
aph@29183 308 frame sender = this->sender(map);
aph@29183 309 return sender.sp() - sp();
aph@29183 310 }
aph@29183 311
aph@29183 312 intptr_t* frame::entry_frame_argument_at(int offset) const {
aph@29183 313 // convert offset to index to deal with tsi
aph@29183 314 int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
aph@29183 315 // Entry frame's arguments are always in relation to unextended_sp()
aph@29183 316 return &unextended_sp()[index];
aph@29183 317 }
aph@29183 318
aph@29183 319 // sender_sp
aph@29183 320 intptr_t* frame::interpreter_frame_sender_sp() const {
aph@29183 321 assert(is_interpreted_frame(), "interpreted frame expected");
aph@29183 322 return (intptr_t*) at(interpreter_frame_sender_sp_offset);
aph@29183 323 }
aph@29183 324
aph@29183 325 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
aph@29183 326 assert(is_interpreted_frame(), "interpreted frame expected");
aph@29183 327 ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
aph@29183 328 }
aph@29183 329
aph@29183 330
aph@29183 331 // monitor elements
aph@29183 332
aph@29183 333 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
aph@29183 334 return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
aph@29183 335 }
aph@29183 336
aph@29183 337 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
aph@29183 338 BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
aph@29183 339 // make sure the pointer points inside the frame
aph@29183 340 assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
aph@29183 341 assert((intptr_t*) result < fp(), "monitor end should be strictly below the frame pointer");
aph@29183 342 return result;
aph@29183 343 }
aph@29183 344
aph@29183 345 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
aph@29183 346 *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
aph@29183 347 }
aph@29183 348
aph@29183 349 // Used by template based interpreter deoptimization
aph@29183 350 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
aph@29183 351 *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
aph@29183 352 }
aph@29183 353
aph@29183 354 frame frame::sender_for_entry_frame(RegisterMap* map) const {
aph@29183 355 assert(map != NULL, "map must be set");
aph@29183 356 // Java frame called from C; skip all C frames and return top C
aph@29183 357 // frame of that chunk as the sender
aph@29183 358 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
aph@29183 359 assert(!entry_frame_is_first(), "next Java fp must be non zero");
aph@29183 360 assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
aph@40643 361 // Since we are walking the stack now this nested anchor is obviously walkable
aph@40643 362 // even if it wasn't when it was stacked.
aph@40643 363 if (!jfa->walkable()) {
aph@40643 364 // Capture _last_Java_pc (if needed) and mark anchor walkable.
aph@40643 365 jfa->capture_last_Java_pc();
aph@40643 366 }
aph@29183 367 map->clear();
aph@29183 368 assert(map->include_argument_oops(), "should be set by clear");
aph@40643 369 vmassert(jfa->last_Java_pc() != NULL, "not walkable");
aph@40643 370 frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
aph@29183 371 return fr;
aph@29183 372 }
aph@29183 373
aph@29183 374 //------------------------------------------------------------------------------
aph@29183 375 // frame::verify_deopt_original_pc
aph@29183 376 //
aph@29183 377 // Verifies the calculated original PC of a deoptimization PC for the
enevill@30552 378 // given unextended SP.
aph@29183 379 #ifdef ASSERT
rbackman@38133 380 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
aph@29183 381 frame fr;
aph@29183 382
aph@29183 383 // This is ugly but it's better than to change {get,set}_original_pc
aph@29183 384 // to take an SP value as argument. And it's only a debugging
aph@29183 385 // method anyway.
aph@29183 386 fr._unextended_sp = unextended_sp;
aph@29183 387
aph@29183 388 address original_pc = nm->get_original_pc(&fr);
zmajo@43474 389 assert(nm->insts_contains_inclusive(original_pc),
zmajo@43474 390 "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
aph@29183 391 }
aph@29183 392 #endif
aph@29183 393
aph@29183 394 //------------------------------------------------------------------------------
aph@29183 395 // frame::adjust_unextended_sp
aph@29183 396 void frame::adjust_unextended_sp() {
enevill@30552 397 // On aarch64, sites calling method handle intrinsics and lambda forms are treated
enevill@30552 398 // as any other call site. Therefore, no special action is needed when we are
enevill@30552 399 // returning to any of these call sites.
aph@29183 400
rbackman@38133 401 if (_cb != NULL) {
rbackman@38133 402 CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
rbackman@38133 403 if (sender_cm != NULL) {
rbackman@38133 404 // If the sender PC is a deoptimization point, get the original PC.
rbackman@38133 405 if (sender_cm->is_deopt_entry(_pc) ||
rbackman@38133 406 sender_cm->is_deopt_mh_entry(_pc)) {
rbackman@38133 407 DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));
rbackman@38133 408 }
aph@29183 409 }
aph@29183 410 }
aph@29183 411 }
aph@29183 412
aph@29183 413 //------------------------------------------------------------------------------
aph@29183 414 // frame::update_map_with_saved_link
aph@29183 415 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
aph@29183 416 // The interpreter and compiler(s) always save fp in a known
aph@29183 417 // location on entry. We must record where that location is
aph@29183 418 // so that if fp was live on callout from c2 we can find
aph@29183 419 // the saved copy no matter what it called.
aph@29183 420
aph@29183 421 // Since the interpreter always saves fp if we record where it is then
aph@29183 422 // we don't have to always save fp on entry and exit to c2 compiled
aph@29183 423 // code, on entry will be enough.
aph@29183 424 map->set_location(rfp->as_VMReg(), (address) link_addr);
aph@29183 425 // this is weird "H" ought to be at a higher address however the
aph@29183 426 // oopMaps seems to have the "H" regs at the same address and the
aph@29183 427 // vanilla register.
aph@29183 428 // XXXX make this go away
aph@29183 429 if (true) {
aph@29183 430 map->set_location(rfp->as_VMReg()->next(), (address) link_addr);
aph@29183 431 }
aph@29183 432 }
aph@29183 433
aph@29183 434
aph@29183 435 //------------------------------------------------------------------------------
aph@29183 436 // frame::sender_for_interpreter_frame
aph@29183 437 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
aph@29183 438 // SP is the raw SP from the sender after adapter or interpreter
aph@29183 439 // extension.
aph@29183 440 intptr_t* sender_sp = this->sender_sp();
aph@29183 441
aph@29183 442 // This is the sp before any possible extension (adapter/locals).
aph@29183 443 intptr_t* unextended_sp = interpreter_frame_sender_sp();
aph@29183 444
jcm@47980 445 #if COMPILER2_OR_JVMCI
aph@29183 446 if (map->update_map()) {
aph@29183 447 update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
aph@29183 448 }
jcm@47980 449 #endif // COMPILER2_OR_JVMCI
aph@29183 450
aph@29183 451 return frame(sender_sp, unextended_sp, link(), sender_pc());
aph@29183 452 }
aph@29183 453
aph@29183 454
aph@29183 455 //------------------------------------------------------------------------------
aph@29183 456 // frame::sender_for_compiled_frame
aph@29183 457 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
aph@29183 458 // we cannot rely upon the last fp having been saved to the thread
aph@29183 459 // in C2 code but it will have been pushed onto the stack. so we
aph@29183 460 // have to find it relative to the unextended sp
aph@29183 461
aph@29183 462 assert(_cb->frame_size() >= 0, "must have non-zero frame size");
aph@29183 463 intptr_t* l_sender_sp = unextended_sp() + _cb->frame_size();
aph@29183 464 intptr_t* unextended_sp = l_sender_sp;
aph@29183 465
aph@29183 466 // the return_address is always the word on the stack
aph@29183 467 address sender_pc = (address) *(l_sender_sp-1);
aph@29183 468
aph@29183 469 intptr_t** saved_fp_addr = (intptr_t**) (l_sender_sp - frame::sender_sp_offset);
aph@29183 470
aph@29183 471 // assert (sender_sp() == l_sender_sp, "should be");
aph@29183 472 // assert (*saved_fp_addr == link(), "should be");
aph@29183 473
aph@29183 474 if (map->update_map()) {
aph@29183 475 // Tell GC to use argument oopmaps for some runtime stubs that need it.
aph@29183 476 // For C1, the runtime stub might not have oop maps, so set this flag
aph@29183 477 // outside of update_register_map.
aph@29183 478 map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
aph@29183 479 if (_cb->oop_maps() != NULL) {
aph@29183 480 OopMapSet::update_register_map(this, map);
aph@29183 481 }
aph@29183 482
aph@29183 483 // Since the prolog does the save and restore of FP there is no
aph@29183 484 // oopmap for it so we must fill in its location as if there was
aph@29183 485 // an oopmap entry since if our caller was compiled code there
aph@29183 486 // could be live jvm state in it.
aph@29183 487 update_map_with_saved_link(map, saved_fp_addr);
aph@29183 488 }
aph@29183 489
aph@29183 490 return frame(l_sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
aph@29183 491 }
aph@29183 492
aph@29183 493 //------------------------------------------------------------------------------
aph@29183 494 // frame::sender
aph@29183 495 frame frame::sender(RegisterMap* map) const {
aph@29183 496 // Default is we done have to follow them. The sender_for_xxx will
aph@29183 497 // update it accordingly
aph@29183 498 map->set_include_argument_oops(false);
aph@29183 499
aph@29183 500 if (is_entry_frame())
aph@29183 501 return sender_for_entry_frame(map);
aph@29183 502 if (is_interpreted_frame())
aph@29183 503 return sender_for_interpreter_frame(map);
aph@29183 504 assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
aph@29183 505
aph@29183 506 // This test looks odd: why is it not is_compiled_frame() ? That's
aph@29183 507 // because stubs also have OOP maps.
aph@29183 508 if (_cb != NULL) {
aph@29183 509 return sender_for_compiled_frame(map);
aph@29183 510 }
aph@29183 511
aph@29183 512 // Must be native-compiled frame, i.e. the marshaling code for native
aph@29183 513 // methods that exists in the core system.
aph@29183 514 return frame(sender_sp(), link(), sender_pc());
aph@29183 515 }
aph@29183 516
aph@29183 517 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
aph@29183 518 assert(is_interpreted_frame(), "Not an interpreted frame");
aph@29183 519 // These are reasonable sanity checks
aph@29183 520 if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
aph@29183 521 return false;
aph@29183 522 }
aph@29183 523 if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
aph@29183 524 return false;
aph@29183 525 }
aph@29183 526 if (fp() + interpreter_frame_initial_sp_offset < sp()) {
aph@29183 527 return false;
aph@29183 528 }
aph@29183 529 // These are hacks to keep us out of trouble.
aph@29183 530 // The problem with these is that they mask other problems
aph@29183 531 if (fp() <= sp()) { // this attempts to deal with unsigned comparison above
aph@29183 532 return false;
aph@29183 533 }
aph@29183 534
aph@29183 535 // do some validation of frame elements
aph@29183 536
aph@29183 537 // first the method
aph@29183 538
aph@29183 539 Method* m = *interpreter_frame_method_addr();
aph@29183 540
aph@29183 541 // validate the method we'd find in this potential sender
hseigel@51955 542 if (!Method::is_valid_method(m)) return false;
aph@29183 543
aph@29183 544 // stack frames shouldn't be much larger than max_stack elements
aph@29192 545 // this test requires the use of unextended_sp which is the sp as seen by
aph@29192 546 // the current frame, and not sp which is the "raw" pc which could point
aph@29192 547 // further because of local variables of the callee method inserted after
aph@29192 548 // method arguments
aph@29192 549 if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
aph@29183 550 return false;
aph@29183 551 }
aph@29183 552
aph@29183 553 // validate bci/bcx
aph@29183 554
aph@29183 555 address bcp = interpreter_frame_bcp();
aph@29183 556 if (m->validate_bci_from_bcp(bcp) < 0) {
aph@29183 557 return false;
aph@29183 558 }
aph@29183 559
aph@29183 560 // validate constantPoolCache*
aph@29183 561 ConstantPoolCache* cp = *interpreter_frame_cache_addr();
aph@29183 562 if (cp == NULL || !cp->is_metaspace_object()) return false;
aph@29183 563
aph@29183 564 // validate locals
aph@29183 565
aph@29183 566 address locals = (address) *interpreter_frame_locals_addr();
aph@29183 567
aph@29183 568 if (locals > thread->stack_base() || locals < (address) fp()) return false;
aph@29183 569
aph@29183 570 // We'd have to be pretty unlucky to be mislead at this point
aph@29183 571 return true;
aph@29183 572 }
aph@29183 573
aph@29183 574 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
aph@29183 575 assert(is_interpreted_frame(), "interpreted frame expected");
aph@29183 576 Method* method = interpreter_frame_method();
aph@29183 577 BasicType type = method->result_type();
aph@29183 578
aph@29183 579 intptr_t* tos_addr;
aph@29183 580 if (method->is_native()) {
aph@29183 581 // TODO : ensure AARCH64 does the same as Intel here i.e. push v0 then r0
aph@29183 582 // Prior to calling into the runtime to report the method_exit the possible
aph@29183 583 // return value is pushed to the native stack. If the result is a jfloat/jdouble
aph@29183 584 // then ST0 is saved before EAX/EDX. See the note in generate_native_result
aph@29183 585 tos_addr = (intptr_t*)sp();
aph@29183 586 if (type == T_FLOAT || type == T_DOUBLE) {
aph@29183 587 // This is times two because we do a push(ltos) after pushing XMM0
aph@29183 588 // and that takes two interpreter stack slots.
aph@29183 589 tos_addr += 2 * Interpreter::stackElementWords;
aph@29183 590 }
aph@29183 591 } else {
aph@29183 592 tos_addr = (intptr_t*)interpreter_frame_tos_address();
aph@29183 593 }
aph@29183 594
aph@29183 595 switch (type) {
aph@29183 596 case T_OBJECT :
aph@29183 597 case T_ARRAY : {
aph@29183 598 oop obj;
aph@29183 599 if (method->is_native()) {
aph@29183 600 obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
aph@29183 601 } else {
aph@29183 602 oop* obj_p = (oop*)tos_addr;
aph@29183 603 obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
aph@29183 604 }
aph@29183 605 assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
aph@29183 606 *oop_result = obj;
aph@29183 607 break;
aph@29183 608 }
aph@29183 609 case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
aph@29183 610 case T_BYTE : value_result->b = *(jbyte*)tos_addr; break;
aph@29183 611 case T_CHAR : value_result->c = *(jchar*)tos_addr; break;
aph@29183 612 case T_SHORT : value_result->s = *(jshort*)tos_addr; break;
aph@29183 613 case T_INT : value_result->i = *(jint*)tos_addr; break;
aph@29183 614 case T_LONG : value_result->j = *(jlong*)tos_addr; break;
aph@29183 615 case T_FLOAT : {
aph@29183 616 value_result->f = *(jfloat*)tos_addr;
aph@29183 617 break;
aph@29183 618 }
aph@29183 619 case T_DOUBLE : value_result->d = *(jdouble*)tos_addr; break;
aph@29183 620 case T_VOID : /* Nothing to do */ break;
aph@29183 621 default : ShouldNotReachHere();
aph@29183 622 }
aph@29183 623
aph@29183 624 return type;
aph@29183 625 }
aph@29183 626
aph@29183 627
aph@29183 628 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
aph@29183 629 int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
aph@29183 630 return &interpreter_frame_tos_address()[index];
aph@29183 631 }
aph@29183 632
aph@29183 633 #ifndef PRODUCT
aph@29183 634
aph@29183 635 #define DESCRIBE_FP_OFFSET(name) \
aph@29183 636 values.describe(frame_no, fp() + frame::name##_offset, #name)
aph@29183 637
aph@29183 638 void frame::describe_pd(FrameValues& values, int frame_no) {
aph@29183 639 if (is_interpreted_frame()) {
aph@29183 640 DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
aph@29183 641 DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
aph@29183 642 DESCRIBE_FP_OFFSET(interpreter_frame_method);
aph@29183 643 DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
aph@43439 644 DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
aph@29183 645 DESCRIBE_FP_OFFSET(interpreter_frame_cache);
aph@29183 646 DESCRIBE_FP_OFFSET(interpreter_frame_locals);
aph@29183 647 DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
aph@29183 648 DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
aph@29183 649 }
aph@29183 650 }
aph@29183 651 #endif
aph@29183 652
aph@29183 653 intptr_t *frame::initial_deoptimization_info() {
aph@29183 654 // Not used on aarch64, but we must return something.
aph@29183 655 return NULL;
aph@29183 656 }
aph@29183 657
aph@29183 658 intptr_t* frame::real_fp() const {
aph@29183 659 if (_cb != NULL) {
aph@29183 660 // use the frame size if valid
aph@29183 661 int size = _cb->frame_size();
aph@29183 662 if (size > 0) {
aph@29183 663 return unextended_sp() + size;
aph@29183 664 }
aph@29183 665 }
aph@29183 666 // else rely on fp()
aph@29183 667 assert(! is_compiled_frame(), "unknown compiled frame size");
aph@29183 668 return fp();
aph@29183 669 }
aph@29183 670
aph@29183 671 #undef DESCRIBE_FP_OFFSET
aph@29183 672
aph@29183 673 #define DESCRIBE_FP_OFFSET(name) \
aph@29183 674 { \
aph@29183 675 unsigned long *p = (unsigned long *)fp; \
aph@29183 676 printf("0x%016lx 0x%016lx %s\n", (unsigned long)(p + frame::name##_offset), \
aph@29183 677 p[frame::name##_offset], #name); \
aph@29183 678 }
aph@29183 679
aph@29183 680 static __thread unsigned long nextfp;
aph@29183 681 static __thread unsigned long nextpc;
aph@29183 682 static __thread unsigned long nextsp;
aph@29183 683 static __thread RegisterMap *reg_map;
aph@29183 684
aph@29183 685 static void printbc(Method *m, intptr_t bcx) {
aph@29183 686 const char *name;
aph@29183 687 char buf[16];
aph@29183 688 if (m->validate_bci_from_bcp((address)bcx) < 0
aph@29183 689 || !m->contains((address)bcx)) {
aph@29183 690 name = "???";
aph@29183 691 snprintf(buf, sizeof buf, "(bad)");
aph@29183 692 } else {
aph@29183 693 int bci = m->bci_from((address)bcx);
aph@29183 694 snprintf(buf, sizeof buf, "%d", bci);
aph@29183 695 name = Bytecodes::name(m->code_at(bci));
aph@29183 696 }
aph@29183 697 ResourceMark rm;
aph@29183 698 printf("%s : %s ==> %s\n", m->name_and_sig_as_C_string(), buf, name);
aph@29183 699 }
aph@29183 700
aph@29183 701 void internal_pf(unsigned long sp, unsigned long fp, unsigned long pc, unsigned long bcx) {
aph@29183 702 if (! fp)
aph@29183 703 return;
aph@29183 704
aph@29183 705 DESCRIBE_FP_OFFSET(return_addr);
aph@29183 706 DESCRIBE_FP_OFFSET(link);
aph@29183 707 DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
aph@29183 708 DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
aph@29183 709 DESCRIBE_FP_OFFSET(interpreter_frame_method);
aph@29183 710 DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
aph@29183 711 DESCRIBE_FP_OFFSET(interpreter_frame_cache);
aph@29183 712 DESCRIBE_FP_OFFSET(interpreter_frame_locals);
aph@29183 713 DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
aph@29183 714 DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
aph@29183 715 unsigned long *p = (unsigned long *)fp;
aph@29183 716
aph@29183 717 // We want to see all frames, native and Java. For compiled and
aph@29183 718 // interpreted frames we have special information that allows us to
aph@29183 719 // unwind them; for everything else we assume that the native frame
aph@29183 720 // pointer chain is intact.
aph@29183 721 frame this_frame((intptr_t*)sp, (intptr_t*)fp, (address)pc);
aph@29183 722 if (this_frame.is_compiled_frame() ||
aph@29183 723 this_frame.is_interpreted_frame()) {
aph@29183 724 frame sender = this_frame.sender(reg_map);
aph@29183 725 nextfp = (unsigned long)sender.fp();
aph@29183 726 nextpc = (unsigned long)sender.pc();
aph@29183 727 nextsp = (unsigned long)sender.unextended_sp();
aph@29183 728 } else {
aph@29183 729 nextfp = p[frame::link_offset];
aph@29183 730 nextpc = p[frame::return_addr_offset];
aph@29183 731 nextsp = (unsigned long)&p[frame::sender_sp_offset];
aph@29183 732 }
aph@29183 733
aph@29183 734 if (bcx == -1ul)
aph@29183 735 bcx = p[frame::interpreter_frame_bcp_offset];
aph@29183 736
aph@29183 737 if (Interpreter::contains((address)pc)) {
aph@29183 738 Method* m = (Method*)p[frame::interpreter_frame_method_offset];
aph@29183 739 if(m && m->is_method()) {
aph@29183 740 printbc(m, bcx);
aph@29183 741 } else
aph@29183 742 printf("not a Method\n");
aph@29183 743 } else {
aph@29183 744 CodeBlob *cb = CodeCache::find_blob((address)pc);
aph@29183 745 if (cb != NULL) {
aph@29183 746 if (cb->is_nmethod()) {
aph@29183 747 ResourceMark rm;
aph@29183 748 nmethod* nm = (nmethod*)cb;
aph@29183 749 printf("nmethod %s\n", nm->method()->name_and_sig_as_C_string());
aph@29183 750 } else if (cb->name()) {
aph@29183 751 printf("CodeBlob %s\n", cb->name());
aph@29183 752 }
aph@29183 753 }
aph@29183 754 }
aph@29183 755 }
aph@29183 756
aph@29183 757 extern "C" void npf() {
aph@29183 758 CodeBlob *cb = CodeCache::find_blob((address)nextpc);
aph@29183 759 // C2 does not always chain the frame pointers when it can, instead
aph@29183 760 // preferring to use fixed offsets from SP, so a simple leave() does
aph@29183 761 // not work. Instead, it adds the frame size to SP then pops FP and
aph@29183 762 // LR. We have to do the same thing to get a good call chain.
aph@29183 763 if (cb && cb->frame_size())
aph@29183 764 nextfp = nextsp + wordSize * (cb->frame_size() - 2);
aph@29183 765 internal_pf (nextsp, nextfp, nextpc, -1);
aph@29183 766 }
aph@29183 767
aph@29183 768 extern "C" void pf(unsigned long sp, unsigned long fp, unsigned long pc,
aph@29183 769 unsigned long bcx, unsigned long thread) {
aph@29183 770 RegisterMap map((JavaThread*)thread, false);
aph@29183 771 if (!reg_map) {
aph@29183 772 reg_map = (RegisterMap*)os::malloc(sizeof map, mtNone);
aph@29183 773 }
aph@29183 774 memcpy(reg_map, &map, sizeof map);
aph@29183 775 {
aph@29183 776 CodeBlob *cb = CodeCache::find_blob((address)pc);
aph@29183 777 if (cb && cb->frame_size())
aph@29183 778 fp = sp + wordSize * (cb->frame_size() - 2);
aph@29183 779 }
aph@29183 780 internal_pf(sp, fp, pc, bcx);
aph@29183 781 }
aph@29183 782
aph@29183 783 // support for printing out where we are in a Java method
aph@29183 784 // needs to be passed current fp and bcp register values
aph@29183 785 // prints method name, bc index and bytecode name
aph@29183 786 extern "C" void pm(unsigned long fp, unsigned long bcx) {
aph@29183 787 DESCRIBE_FP_OFFSET(interpreter_frame_method);
aph@29183 788 unsigned long *p = (unsigned long *)fp;
aph@29183 789 Method* m = (Method*)p[frame::interpreter_frame_method_offset];
aph@29183 790 printbc(m, bcx);
aph@29183 791 }
aph@29183 792
aph@29183 793 #ifndef PRODUCT
aph@29183 794 // This is a generic constructor which is only used by pns() in debug.cpp.
aph@29183 795 frame::frame(void* sp, void* fp, void* pc) {
aph@29183 796 init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
aph@29183 797 }
coleenp@46589 798
coleenp@46589 799 void frame::pd_ps() {}
aph@29183 800 #endif
aph@40643 801
aph@40643 802 void JavaFrameAnchor::make_walkable(JavaThread* thread) {
aph@40643 803 // last frame set?
aph@40643 804 if (last_Java_sp() == NULL) return;
aph@40643 805 // already walkable?
aph@40643 806 if (walkable()) return;
aph@40643 807 vmassert(Thread::current() == (Thread*)thread, "not current thread");
aph@40643 808 vmassert(last_Java_sp() != NULL, "not called from Java code?");
aph@40643 809 vmassert(last_Java_pc() == NULL, "already walkable");
aph@40643 810 capture_last_Java_pc();
aph@40643 811 vmassert(walkable(), "something went wrong");
aph@40643 812 }
aph@40643 813
aph@40643 814 void JavaFrameAnchor::capture_last_Java_pc() {
aph@40643 815 vmassert(_last_Java_sp != NULL, "no last frame set");
aph@40643 816 vmassert(_last_Java_pc == NULL, "already walkable");
aph@40643 817 _last_Java_pc = (address)_last_Java_sp[-1];
aph@40643 818 }