1 /*
   2  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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 #ifdef COMPILER2
  27 #include "asm/assembler.hpp"
  28 #include "assembler_x86.inline.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "code/vmreg.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "nativeInst_x86.hpp"
  33 #include "opto/runtime.hpp"
  34 #include "runtime/interfaceSupport.hpp"
  35 #include "runtime/sharedRuntime.hpp"
  36 #include "runtime/stubRoutines.hpp"
  37 #include "runtime/vframeArray.hpp"
  38 #include "utilities/globalDefinitions.hpp"
  39 #include "vmreg_x86.inline.hpp"
  40 #endif
  41 
  42 
  43 #define __ masm->
  44 
  45 ExceptionBlob*     OptoRuntime::_exception_blob;
  46 
  47 //------------------------------generate_exception_blob---------------------------
  48 // creates exception blob at the end
  49 // Using exception blob, this code is jumped from a compiled method.
  50 //
  51 // Given an exception pc at a call we call into the runtime for the
  52 // handler in this method. This handler might merely restore state
  53 // (i.e. callee save registers) unwind the frame and jump to the
  54 // exception handler for the nmethod if there is no Java level handler
  55 // for the nmethod.
  56 //
  57 // This code is entered with a jmp.
  58 //
  59 // Arguments:
  60 //   rax: exception oop
  61 //   rdx: exception pc
  62 //
  63 // Results:
  64 //   rax: exception oop
  65 //   rdx: exception pc in caller or ???
  66 //   destination: exception handler of caller
  67 //
  68 // Note: the exception pc MUST be at a call (precise debug information)
  69 //       Only register rax, rdx, rcx are not callee saved.
  70 //
  71 
  72 void OptoRuntime::generate_exception_blob() {
  73 
  74   // Capture info about frame layout
  75   enum layout {
  76     thread_off,                 // last_java_sp
  77     // The frame sender code expects that rbp will be in the "natural" place and
  78     // will override any oopMap setting for it. We must therefore force the layout
  79     // so that it agrees with the frame sender code.
  80     rbp_off,
  81     return_off,                 // slot for return address
  82     framesize
  83   };
  84 
  85   // allocate space for the code
  86   ResourceMark rm;
  87   // setup code generation tools
  88   CodeBuffer   buffer("exception_blob", 512, 512);
  89   MacroAssembler* masm = new MacroAssembler(&buffer);
  90 
  91   OopMapSet *oop_maps = new OopMapSet();
  92 
  93   address start = __ pc();
  94 
  95   __ push(rdx);
  96   __ subptr(rsp, return_off * wordSize);   // Prolog!
  97 
  98   // rbp, location is implicitly known
  99   __ movptr(Address(rsp,rbp_off  *wordSize), rbp);
 100 
 101   // Store exception in Thread object. We cannot pass any arguments to the
 102   // handle_exception call, since we do not want to make any assumption
 103   // about the size of the frame where the exception happened in.
 104   __ get_thread(rcx);
 105   __ movptr(Address(rcx, JavaThread::exception_oop_offset()), rax);
 106   __ movptr(Address(rcx, JavaThread::exception_pc_offset()),  rdx);
 107 
 108   // This call does all the hard work.  It checks if an exception handler
 109   // exists in the method.
 110   // If so, it returns the handler address.
 111   // If not, it prepares for stack-unwinding, restoring the callee-save
 112   // registers of the frame being removed.
 113   //
 114   __ movptr(Address(rsp, thread_off * wordSize), rcx); // Thread is first argument
 115   __ set_last_Java_frame(rcx, noreg, noreg, NULL);
 116 
 117   __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, OptoRuntime::handle_exception_C)));
 118 
 119   // No registers to map, rbp is known implicitly
 120   oop_maps->add_gc_map( __ pc() - start,  new OopMap( framesize, 0 ));
 121   __ get_thread(rcx);
 122   __ reset_last_Java_frame(rcx, false, false);
 123 
 124   // Restore callee-saved registers
 125   __ movptr(rbp, Address(rsp, rbp_off * wordSize));
 126 
 127   __ addptr(rsp, return_off * wordSize);   // Epilog!
 128   __ pop(rdx); // Exception pc
 129 
 130   // rax: exception handler for given <exception oop/exception pc>
 131 
 132   // Restore SP from BP if the exception PC is a MethodHandle call site.
 133   __ cmpl(Address(rcx, JavaThread::is_method_handle_return_offset()), 0);
 134   __ cmovptr(Assembler::notEqual, rsp, rbp_mh_SP_save);
 135 
 136   // We have a handler in rax, (could be deopt blob)
 137   // rdx - throwing pc, deopt blob will need it.
 138 
 139   __ push(rax);
 140 
 141   // Get the exception
 142   __ movptr(rax, Address(rcx, JavaThread::exception_oop_offset()));
 143   // Get the exception pc in case we are deoptimized
 144   __ movptr(rdx, Address(rcx, JavaThread::exception_pc_offset()));
 145 #ifdef ASSERT
 146   __ movptr(Address(rcx, JavaThread::exception_handler_pc_offset()), NULL_WORD);
 147   __ movptr(Address(rcx, JavaThread::exception_pc_offset()), NULL_WORD);
 148 #endif
 149   // Clear the exception oop so GC no longer processes it as a root.
 150   __ movptr(Address(rcx, JavaThread::exception_oop_offset()), NULL_WORD);
 151 
 152   __ pop(rcx);
 153 
 154   // rax: exception oop
 155   // rcx: exception handler
 156   // rdx: exception pc
 157   __ jmp (rcx);
 158 
 159   // -------------
 160   // make sure all code is generated
 161   masm->flush();
 162 
 163   _exception_blob = ExceptionBlob::create(&buffer, oop_maps, framesize);
 164 }