12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "interpreter/interpreter.hpp"
27 #include "memory/resourceArea.hpp"
28 #include "oops/markOop.hpp"
29 #include "oops/method.hpp"
30 #include "oops/oop.inline.hpp"
31 #include "prims/methodHandles.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/os.inline.hpp"
37 #include "runtime/signature.hpp"
38 #include "runtime/stubCodeGenerator.hpp"
39 #include "runtime/stubRoutines.hpp"
40 #include "vmreg_x86.inline.hpp"
41 #ifdef COMPILER1
42 #include "c1/c1_Runtime1.hpp"
43 #include "runtime/vframeArray.hpp"
44 #endif
45
46 #ifdef ASSERT
47 void RegisterMap::check_location_valid() {
48 }
49 #endif
50
51 // Profiling/safepoint support
258 // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
259
260 if ( (address) this->fp()[return_addr_offset] == NULL) return false;
261
262
263 // could try and do some more potential verification of native frame if we could think of some...
264
265 return true;
266
267 }
268
269
270 void frame::patch_pc(Thread* thread, address pc) {
271 address* pc_addr = &(((address*) sp())[-1]);
272 if (TracePcPatching) {
273 tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
274 p2i(pc_addr), p2i(*pc_addr), p2i(pc));
275 }
276 // Either the return address is the original one or we are going to
277 // patch in the same address that's already there.
278 assert(_pc == *pc_addr || pc == *pc_addr, "must be");
279 *pc_addr = pc;
280 _cb = CodeCache::find_blob(pc);
281 address original_pc = CompiledMethod::get_deopt_original_pc(this);
282 if (original_pc != NULL) {
283 assert(original_pc == _pc, "expected original PC to be stored before patching");
284 _deopt_state = is_deoptimized;
285 // leave _pc as is
286 } else {
287 _deopt_state = not_deoptimized;
288 _pc = pc;
289 }
290 }
291
292 bool frame::is_interpreted_frame() const {
293 return Interpreter::contains(pc());
294 }
295
296 int frame::frame_size(RegisterMap* map) const {
297 frame sender = this->sender(map);
298 return sender.sp() - sp();
299 }
300
301 intptr_t* frame::entry_frame_argument_at(int offset) const {
311 assert(is_interpreted_frame(), "interpreted frame expected");
312 return (intptr_t*) at(interpreter_frame_sender_sp_offset);
313 }
314
315 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
316 assert(is_interpreted_frame(), "interpreted frame expected");
317 ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
318 }
319
320
321 // monitor elements
322
323 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
324 return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
325 }
326
327 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
328 BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
329 // make sure the pointer points inside the frame
330 assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
331 assert((intptr_t*) result < fp(), "monitor end should be strictly below the frame pointer");
332 return result;
333 }
334
335 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
336 *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
337 }
338
339 // Used by template based interpreter deoptimization
340 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
341 *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
342 }
343
344 frame frame::sender_for_entry_frame(RegisterMap* map) const {
345 assert(map != NULL, "map must be set");
346 // Java frame called from C; skip all C frames and return top C
347 // frame of that chunk as the sender
348 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
349 assert(!entry_frame_is_first(), "next Java fp must be non zero");
350 assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
351 // Since we are walking the stack now this nested anchor is obviously walkable
360 frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
361 return fr;
362 }
363
364 //------------------------------------------------------------------------------
365 // frame::verify_deopt_original_pc
366 //
367 // Verifies the calculated original PC of a deoptimization PC for the
368 // given unextended SP.
369 #ifdef ASSERT
370 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
371 frame fr;
372
373 // This is ugly but it's better than to change {get,set}_original_pc
374 // to take an SP value as argument. And it's only a debugging
375 // method anyway.
376 fr._unextended_sp = unextended_sp;
377
378 address original_pc = nm->get_original_pc(&fr);
379 assert(nm->insts_contains_inclusive(original_pc),
380 "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
381 }
382 #endif
383
384 //------------------------------------------------------------------------------
385 // frame::adjust_unextended_sp
386 #ifdef ASSERT
387 void frame::adjust_unextended_sp() {
388 // On x86, sites calling method handle intrinsics and lambda forms are treated
389 // as any other call site. Therefore, no special action is needed when we are
390 // returning to any of these call sites.
391
392 if (_cb != NULL) {
393 CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
394 if (sender_cm != NULL) {
395 // If the sender PC is a deoptimization point, get the original PC.
396 if (sender_cm->is_deopt_entry(_pc) ||
397 sender_cm->is_deopt_mh_entry(_pc)) {
398 verify_deopt_original_pc(sender_cm, _unextended_sp);
399 }
400 }
408 // The interpreter and compiler(s) always save EBP/RBP in a known
409 // location on entry. We must record where that location is
410 // so this if EBP/RBP was live on callout from c2 we can find
411 // the saved copy no matter what it called.
412
413 // Since the interpreter always saves EBP/RBP if we record where it is then
414 // we don't have to always save EBP/RBP on entry and exit to c2 compiled
415 // code, on entry will be enough.
416 map->set_location(rbp->as_VMReg(), (address) link_addr);
417 #ifdef AMD64
418 // this is weird "H" ought to be at a higher address however the
419 // oopMaps seems to have the "H" regs at the same address and the
420 // vanilla register.
421 // XXXX make this go away
422 if (true) {
423 map->set_location(rbp->as_VMReg()->next(), (address) link_addr);
424 }
425 #endif // AMD64
426 }
427
428
429 //------------------------------------------------------------------------------
430 // frame::sender_for_interpreter_frame
431 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
432 // SP is the raw SP from the sender after adapter or interpreter
433 // extension.
434 intptr_t* sender_sp = this->sender_sp();
435
436 // This is the sp before any possible extension (adapter/locals).
437 intptr_t* unextended_sp = interpreter_frame_sender_sp();
438
439 #if COMPILER2_OR_JVMCI
440 if (map->update_map()) {
441 update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
442 }
443 #endif // COMPILER2_OR_JVMCI
444
445 return frame(sender_sp, unextended_sp, link(), sender_pc());
446 }
447
448
449 //------------------------------------------------------------------------------
450 // frame::sender_for_compiled_frame
451 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
452 assert(map != NULL, "map must be set");
453
454 // frame owned by optimizing compiler
455 assert(_cb->frame_size() >= 0, "must have non-zero frame size");
456 intptr_t* sender_sp = unextended_sp() + _cb->frame_size();
457 intptr_t* unextended_sp = sender_sp;
458
459 // On Intel the return_address is always the word on the stack
460 address sender_pc = (address) *(sender_sp-1);
461
462 // This is the saved value of EBP which may or may not really be an FP.
463 // It is only an FP if the sender is an interpreter frame (or C1?).
464 intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - frame::sender_sp_offset);
465
466 if (map->update_map()) {
467 // Tell GC to use argument oopmaps for some runtime stubs that need it.
468 // For C1, the runtime stub might not have oop maps, so set this flag
469 // outside of update_register_map.
470 map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
471 if (_cb->oop_maps() != NULL) {
472 OopMapSet::update_register_map(this, map);
473 }
474
475 // Since the prolog does the save and restore of EBP there is no oopmap
476 // for it so we must fill in its location as if there was an oopmap entry
477 // since if our caller was compiled code there could be live jvm state in it.
478 update_map_with_saved_link(map, saved_fp_addr);
479 }
480
481 assert(sender_sp != sp(), "must have changed");
482 return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
483 }
484
485
486 //------------------------------------------------------------------------------
487 // frame::sender
488 frame frame::sender(RegisterMap* map) const {
489 // Default is we done have to follow them. The sender_for_xxx will
490 // update it accordingly
491 map->set_include_argument_oops(false);
492
493 if (is_entry_frame()) return sender_for_entry_frame(map);
494 if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
495 assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
496
497 if (_cb != NULL) {
498 return sender_for_compiled_frame(map);
499 }
500 // Must be native-compiled frame, i.e. the marshaling code for native
501 // methods that exists in the core system.
502 return frame(sender_sp(), link(), sender_pc());
503 }
504
505 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
506 assert(is_interpreted_frame(), "Not an interpreted frame");
507 // These are reasonable sanity checks
508 if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
509 return false;
510 }
511 if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
512 return false;
513 }
514 if (fp() + interpreter_frame_initial_sp_offset < sp()) {
515 return false;
516 }
517 // These are hacks to keep us out of trouble.
518 // The problem with these is that they mask other problems
|
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "interpreter/interpreter.hpp"
27 #include "memory/resourceArea.hpp"
28 #include "oops/markOop.hpp"
29 #include "oops/method.hpp"
30 #include "oops/oop.inline.hpp"
31 #include "prims/methodHandles.hpp"
32 #include "runtime/continuation.hpp"
33 #include "runtime/frame.inline.hpp"
34 #include "runtime/handles.inline.hpp"
35 #include "runtime/javaCalls.hpp"
36 #include "runtime/monitorChunk.hpp"
37 #include "runtime/os.inline.hpp"
38 #include "runtime/signature.hpp"
39 #include "runtime/stubCodeGenerator.hpp"
40 #include "runtime/stubRoutines.hpp"
41 #include "vmreg_x86.inline.hpp"
42 #ifdef COMPILER1
43 #include "c1/c1_Runtime1.hpp"
44 #include "runtime/vframeArray.hpp"
45 #endif
46
47 #ifdef ASSERT
48 void RegisterMap::check_location_valid() {
49 }
50 #endif
51
52 // Profiling/safepoint support
259 // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
260
261 if ( (address) this->fp()[return_addr_offset] == NULL) return false;
262
263
264 // could try and do some more potential verification of native frame if we could think of some...
265
266 return true;
267
268 }
269
270
271 void frame::patch_pc(Thread* thread, address pc) {
272 address* pc_addr = &(((address*) sp())[-1]);
273 if (TracePcPatching) {
274 tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
275 p2i(pc_addr), p2i(*pc_addr), p2i(pc));
276 }
277 // Either the return address is the original one or we are going to
278 // patch in the same address that's already there.
279 assert(!Continuation::is_return_barrier_entry(*pc_addr), "return barrier");
280 // #ifdef ASSERT
281 // bool good = (_pc == *pc_addr || pc == *pc_addr);
282 // if (!good) {
283 // tty->print_cr("pc: %p", pc); os::print_location(tty, *(intptr_t*)&pc);
284 // tty->print_cr("_pc: %p", _pc); os::print_location(tty, *(intptr_t*)&_pc);
285 // tty->print_cr("*pc_addr: %p", *pc_addr); os::print_location(tty, *(intptr_t*)pc_addr);
286 // }
287 // #endif
288 assert(_pc == *pc_addr || pc == *pc_addr, "must be (pc: %p _pc: %p *pc_addr: %p)", pc, _pc, *pc_addr);
289 *pc_addr = pc;
290 _cb = CodeCache::find_blob(pc);
291 address original_pc = CompiledMethod::get_deopt_original_pc(this);
292 // #ifdef ASSERT
293 // if (!good) {
294 // tty->print_cr("_pc: %p original_pc: %p", _pc, original_pc);
295 // CompiledMethod* cm = _cb->as_compiled_method_or_null();
296 // tty->print_cr("_pc: %p is_deopt _pc: %d is_deopt pc: %d", _pc, cm->is_deopt_pc(_pc), cm->is_deopt_pc(pc));
297 // }
298 // #endif
299 if (original_pc != NULL) {
300 assert(original_pc == _pc, "expected original PC to be stored before patching");
301 _deopt_state = is_deoptimized;
302 // leave _pc as is
303 } else {
304 _deopt_state = not_deoptimized;
305 _pc = pc;
306 }
307 }
308
309 bool frame::is_interpreted_frame() const {
310 return Interpreter::contains(pc());
311 }
312
313 int frame::frame_size(RegisterMap* map) const {
314 frame sender = this->sender(map);
315 return sender.sp() - sp();
316 }
317
318 intptr_t* frame::entry_frame_argument_at(int offset) const {
328 assert(is_interpreted_frame(), "interpreted frame expected");
329 return (intptr_t*) at(interpreter_frame_sender_sp_offset);
330 }
331
332 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
333 assert(is_interpreted_frame(), "interpreted frame expected");
334 ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
335 }
336
337
338 // monitor elements
339
340 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
341 return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
342 }
343
344 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
345 BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
346 // make sure the pointer points inside the frame
347 assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
348 assert((intptr_t*) result < fp(), "monitor end should be strictly below the frame pointer: result: %p fp: %p", result, fp());
349 return result;
350 }
351
352 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
353 *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
354 }
355
356 // Used by template based interpreter deoptimization
357 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
358 *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
359 }
360
361 frame frame::sender_for_entry_frame(RegisterMap* map) const {
362 assert(map != NULL, "map must be set");
363 // Java frame called from C; skip all C frames and return top C
364 // frame of that chunk as the sender
365 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
366 assert(!entry_frame_is_first(), "next Java fp must be non zero");
367 assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
368 // Since we are walking the stack now this nested anchor is obviously walkable
377 frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
378 return fr;
379 }
380
381 //------------------------------------------------------------------------------
382 // frame::verify_deopt_original_pc
383 //
384 // Verifies the calculated original PC of a deoptimization PC for the
385 // given unextended SP.
386 #ifdef ASSERT
387 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
388 frame fr;
389
390 // This is ugly but it's better than to change {get,set}_original_pc
391 // to take an SP value as argument. And it's only a debugging
392 // method anyway.
393 fr._unextended_sp = unextended_sp;
394
395 address original_pc = nm->get_original_pc(&fr);
396 assert(nm->insts_contains_inclusive(original_pc),
397 "original PC must be in the main code section of the the compiled method (or must be immediately following it) original_pc: %p unextended_sp: %p name: %s", original_pc, unextended_sp, nm->name());
398 }
399 #endif
400
401 //------------------------------------------------------------------------------
402 // frame::adjust_unextended_sp
403 #ifdef ASSERT
404 void frame::adjust_unextended_sp() {
405 // On x86, sites calling method handle intrinsics and lambda forms are treated
406 // as any other call site. Therefore, no special action is needed when we are
407 // returning to any of these call sites.
408
409 if (_cb != NULL) {
410 CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
411 if (sender_cm != NULL) {
412 // If the sender PC is a deoptimization point, get the original PC.
413 if (sender_cm->is_deopt_entry(_pc) ||
414 sender_cm->is_deopt_mh_entry(_pc)) {
415 verify_deopt_original_pc(sender_cm, _unextended_sp);
416 }
417 }
425 // The interpreter and compiler(s) always save EBP/RBP in a known
426 // location on entry. We must record where that location is
427 // so this if EBP/RBP was live on callout from c2 we can find
428 // the saved copy no matter what it called.
429
430 // Since the interpreter always saves EBP/RBP if we record where it is then
431 // we don't have to always save EBP/RBP on entry and exit to c2 compiled
432 // code, on entry will be enough.
433 map->set_location(rbp->as_VMReg(), (address) link_addr);
434 #ifdef AMD64
435 // this is weird "H" ought to be at a higher address however the
436 // oopMaps seems to have the "H" regs at the same address and the
437 // vanilla register.
438 // XXXX make this go away
439 if (true) {
440 map->set_location(rbp->as_VMReg()->next(), (address) link_addr);
441 }
442 #endif // AMD64
443 }
444
445 intptr_t** frame::saved_link_address(RegisterMap* map) {
446 return (intptr_t**)map->location(rbp->as_VMReg());
447 }
448
449
450 //------------------------------------------------------------------------------
451 // frame::sender_for_interpreter_frame
452 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
453 // SP is the raw SP from the sender after adapter or interpreter
454 // extension.
455 intptr_t* sender_sp = this->sender_sp();
456
457 // This is the sp before any possible extension (adapter/locals).
458 intptr_t* unextended_sp = interpreter_frame_sender_sp();
459
460 #if COMPILER2_OR_JVMCI
461 if (map->update_map()) {
462 update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
463 }
464 #endif // COMPILER2_OR_JVMCI
465
466 return frame(sender_sp, unextended_sp, link(), sender_pc());
467 }
468
469
470 //------------------------------------------------------------------------------
471 // frame::sender_for_compiled_frame
472 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
473 assert(map != NULL, "map must be set");
474
475 // frame owned by optimizing compiler
476 assert(_cb->frame_size() >= 0, "must have non-zero frame size");
477 intptr_t* sender_sp = unextended_sp() + _cb->frame_size();
478 intptr_t* unextended_sp = sender_sp;
479
480 assert (sender_sp == real_fp(), "sender_sp: %p real_fp: %p", sender_sp, real_fp());
481
482 // On Intel the return_address is always the word on the stack
483 address sender_pc = (address) *(sender_sp-1);
484
485 // This is the saved value of EBP which may or may not really be an FP.
486 // It is only an FP if the sender is an interpreter frame (or C1?).
487 intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - frame::sender_sp_offset);
488
489 if (map->update_map()) {
490 // Tell GC to use argument oopmaps for some runtime stubs that need it.
491 // For C1, the runtime stub might not have oop maps, so set this flag
492 // outside of update_register_map.
493 map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
494 if (_cb->oop_maps() != NULL) {
495 OopMapSet::update_register_map(this, map);
496 }
497
498 // Since the prolog does the save and restore of EBP there is no oopmap
499 // for it so we must fill in its location as if there was an oopmap entry
500 // since if our caller was compiled code there could be live jvm state in it.
501 update_map_with_saved_link(map, saved_fp_addr);
502 }
503
504 assert(sender_sp != sp(), "must have changed");
505 return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
506 }
507
508
509 //------------------------------------------------------------------------------
510 // frame::sender
511 frame frame::sender(RegisterMap* map) const {
512 // Default is we done have to follow them. The sender_for_xxx will
513 // update it accordingly
514 map->set_include_argument_oops(false);
515
516 if (is_entry_frame()) return Continuation::fix_continuation_bottom_sender(*this, sender_for_entry_frame(map), map);
517 if (is_interpreted_frame()) return Continuation::fix_continuation_bottom_sender(*this, sender_for_interpreter_frame(map), map);
518 assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
519
520 if (_cb != NULL) {
521 return Continuation::fix_continuation_bottom_sender(*this, sender_for_compiled_frame(map), map);
522 }
523 // Must be native-compiled frame, i.e. the marshaling code for native
524 // methods that exists in the core system.
525 return frame(sender_sp(), link(), sender_pc());
526 }
527
528 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
529 assert(is_interpreted_frame(), "Not an interpreted frame");
530 // These are reasonable sanity checks
531 if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
532 return false;
533 }
534 if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
535 return false;
536 }
537 if (fp() + interpreter_frame_initial_sp_offset < sp()) {
538 return false;
539 }
540 // These are hacks to keep us out of trouble.
541 // The problem with these is that they mask other problems
|