1 /*
   2  * Copyright (c) 1998, 2011, 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/macroAssembler.inline.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "code/vmreg.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "nativeInst_sparc.hpp"
  32 #include "opto/runtime.hpp"
  33 #include "runtime/interfaceSupport.hpp"
  34 #include "runtime/sharedRuntime.hpp"
  35 #include "runtime/stubRoutines.hpp"
  36 #include "runtime/vframeArray.hpp"
  37 #include "utilities/globalDefinitions.hpp"
  38 #include "vmreg_sparc.inline.hpp"
  39 #endif
  40 
  41 
  42 #define __ masm->
  43 
  44 //------------------------------ generate_exception_blob ---------------------------
  45 // creates exception blob at the end
  46 // Using exception blob, this code is jumped from a compiled method.
  47 // (see emit_exception_handler in sparc.ad file)
  48 //
  49 // Given an exception pc at a call we call into the runtime for the
  50 // handler in this method. This handler might merely restore state
  51 // (i.e. callee save registers) unwind the frame and jump to the
  52 // exception handler for the nmethod if there is no Java level handler
  53 // for the nmethod.
  54 //
  55 // This code is entered with a jmp.
  56 //
  57 // Arguments:
  58 //   O0: exception oop
  59 //   O1: exception pc
  60 //
  61 // Results:
  62 //   O0: exception oop
  63 //   O1: exception pc in caller or ???
  64 //   destination: exception handler of caller
  65 //
  66 // Note: the exception pc MUST be at a call (precise debug information)
  67 //
  68 void OptoRuntime::generate_exception_blob() {
  69   // allocate space for code
  70   ResourceMark rm;
  71   int pad = VerifyThread ? 256 : 0;// Extra slop space for more verify code
  72 
  73   // setup code generation tools
  74   // Measured 8/7/03 at 256 in 32bit debug build (no VerifyThread)
  75   // Measured 8/7/03 at 528 in 32bit debug build (VerifyThread)
  76   CodeBuffer buffer("exception_blob", 600+pad, 512);
  77   MacroAssembler* masm     = new MacroAssembler(&buffer);
  78 
  79   int framesize_in_bytes = __ total_frame_size_in_bytes(0);
  80   int framesize_in_words = framesize_in_bytes / wordSize;
  81   int framesize_in_slots = framesize_in_bytes / sizeof(jint);
  82 
  83   Label L;
  84 
  85   int start = __ offset();
  86 
  87   __ verify_thread();
  88   __ st_ptr(Oexception,  G2_thread, JavaThread::exception_oop_offset());
  89   __ st_ptr(Oissuing_pc, G2_thread, JavaThread::exception_pc_offset());
  90 
  91   // This call does all the hard work. It checks if an exception catch
  92   // exists in the method.
  93   // If so, it returns the handler address.
  94   // If the nmethod has been deoptimized and it had a handler the handler
  95   // address is the deopt blob unpack_with_exception entry.
  96   //
  97   // If no handler exists it prepares for stack-unwinding, restoring the callee-save
  98   // registers of the frame being removed.
  99   //
 100   __ save_frame(0);
 101 
 102   __ mov(G2_thread, O0);
 103   __ set_last_Java_frame(SP, noreg);
 104   __ save_thread(L7_thread_cache);
 105 
 106   // This call can block at exit and nmethod can be deoptimized at that
 107   // point. If the nmethod had a catch point we would jump to the
 108   // now deoptimized catch point and fall thru the vanilla deopt
 109   // path and lose the exception
 110   // Sure would be simpler if this call didn't block!
 111   __ call(CAST_FROM_FN_PTR(address, OptoRuntime::handle_exception_C), relocInfo::runtime_call_type);
 112   __ delayed()->mov(L7_thread_cache, O0);
 113 
 114   // Set an oopmap for the call site.  This oopmap will only be used if we
 115   // are unwinding the stack.  Hence, all locations will be dead.
 116   // Callee-saved registers will be the same as the frame above (i.e.,
 117   // handle_exception_stub), since they were restored when we got the
 118   // exception.
 119 
 120   OopMapSet *oop_maps = new OopMapSet();
 121   oop_maps->add_gc_map( __ offset()-start, new OopMap(framesize_in_slots, 0));
 122 
 123   __ bind(L);
 124   __ restore_thread(L7_thread_cache);
 125   __ reset_last_Java_frame();
 126 
 127   __ mov(O0, G3_scratch);             // Move handler address to temp
 128   __ restore();
 129 
 130   // Restore SP from L7 if the exception PC is a MethodHandle call site.
 131   __ lduw(Address(G2_thread, JavaThread::is_method_handle_return_offset()), O7);
 132   __ tst(O7);
 133   __ movcc(Assembler::notZero, false, Assembler::icc, L7_mh_SP_save, SP);
 134 
 135   // G3_scratch contains handler address
 136   // Since this may be the deopt blob we must set O7 to look like we returned
 137   // from the original pc that threw the exception
 138 
 139   __ ld_ptr(G2_thread, JavaThread::exception_pc_offset(), O7);
 140   __ sub(O7, frame::pc_return_offset, O7);
 141 
 142 
 143   assert(Assembler::is_simm13(in_bytes(JavaThread::exception_oop_offset())), "exception offset overflows simm13, following ld instruction cannot be in delay slot");
 144   __ ld_ptr(G2_thread, JavaThread::exception_oop_offset(), Oexception); // O0
 145 #ifdef ASSERT
 146   __ st_ptr(G0, G2_thread, JavaThread::exception_handler_pc_offset());
 147   __ st_ptr(G0, G2_thread, JavaThread::exception_pc_offset());
 148 #endif
 149   __ JMP(G3_scratch, 0);
 150   // Clear the exception oop so GC no longer processes it as a root.
 151   __ delayed()->st_ptr(G0, G2_thread, JavaThread::exception_oop_offset());
 152 
 153   // -------------
 154   // make sure all code is generated
 155   masm->flush();
 156 
 157   _exception_blob = ExceptionBlob::create(&buffer, oop_maps, framesize_in_words);
 158 }