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