1 /*
   2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2015 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #ifdef COMPILER2
  28 #include "asm/assembler.inline.hpp"
  29 #include "asm/macroAssembler.inline.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "code/vmreg.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "interpreter/interp_masm.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "nativeInst_ppc.hpp"
  36 #include "opto/runtime.hpp"
  37 #include "runtime/interfaceSupport.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "runtime/vframeArray.hpp"
  41 #include "utilities/globalDefinitions.hpp"
  42 #endif
  43 
  44 #define __ masm->
  45 
  46 
  47 #ifdef COMPILER2
  48 
  49 //------------------------------generate_exception_blob---------------------------
  50 // Creates exception blob at the end.
  51 // Using exception blob, this code is jumped from a compiled method.
  52 //
  53 // Given an exception pc at a call we call into the runtime for the
  54 // handler in this method. This handler might merely restore state
  55 // (i.e. callee save registers) unwind the frame and jump to the
  56 // exception handler for the nmethod if there is no Java level handler
  57 // for the nmethod.
  58 //
  59 // This code is entered with a jmp.
  60 //
  61 // Arguments:
  62 //   R3_ARG1: exception oop
  63 //   R4_ARG2: exception pc
  64 //
  65 // Results:
  66 //   R3_ARG1: exception oop
  67 //   R4_ARG2: exception pc in caller
  68 //   destination: exception handler of caller
  69 //
  70 // Note: the exception pc MUST be at a call (precise debug information)
  71 //
  72 void OptoRuntime::generate_exception_blob() {
  73   // Allocate space for the code.
  74   ResourceMark rm;
  75   // Setup code generation tools.
  76   CodeBuffer buffer("exception_blob", 2048, 1024);
  77   InterpreterMacroAssembler* masm = new InterpreterMacroAssembler(&buffer);
  78 
  79   address start = __ pc();
  80 
  81   int frame_size_in_bytes = frame::abi_reg_args_size;
  82   OopMap* map = new OopMap(frame_size_in_bytes / sizeof(jint), 0);
  83 
  84   // Exception pc is 'return address' for stack walker.
  85   __ std(R4_ARG2/*exception pc*/, _abi(lr), R1_SP);
  86 
  87   // Store the exception in the Thread object.
  88   __ std(R3_ARG1/*exception oop*/, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
  89   __ std(R4_ARG2/*exception pc*/,  in_bytes(JavaThread::exception_pc_offset()),  R16_thread);
  90 
  91   // Save callee-saved registers.
  92   // Push a C frame for the exception blob. It is needed for the C call later on.
  93   __ push_frame_reg_args(0, R11_scratch1);
  94 
  95   // This call does all the hard work. It checks if an exception handler
  96   // exists in the method.
  97   // If so, it returns the handler address.
  98   // If not, it prepares for stack-unwinding, restoring the callee-save
  99   // registers of the frame being removed.
 100   __ set_last_Java_frame(/*sp=*/R1_SP, noreg);
 101 
 102   __ mr(R3_ARG1, R16_thread);
 103 #if defined(ABI_ELFv2)
 104   __ call_c((address) OptoRuntime::handle_exception_C, relocInfo::none);
 105 #else
 106   __ call_c(CAST_FROM_FN_PTR(FunctionDescriptor*, OptoRuntime::handle_exception_C),
 107             relocInfo::none);
 108 #endif
 109   address calls_return_pc = __ last_calls_return_pc();
 110 # ifdef ASSERT
 111   __ cmpdi(CCR0, R3_RET, 0);
 112   __ asm_assert_ne("handle_exception_C must not return NULL", 0x601);
 113 # endif
 114 
 115   // Set an oopmap for the call site. This oopmap will only be used if we
 116   // are unwinding the stack. Hence, all locations will be dead.
 117   // Callee-saved registers will be the same as the frame above (i.e.,
 118   // handle_exception_stub), since they were restored when we got the
 119   // exception.
 120   OopMapSet* oop_maps = new OopMapSet();
 121   oop_maps->add_gc_map(calls_return_pc - start, map);
 122 
 123   __ mtctr(R3_RET); // Move address of exception handler to SR_CTR.
 124   __ reset_last_Java_frame();
 125   __ pop_frame();
 126 
 127   // We have a handler in register SR_CTR (could be deopt blob).
 128 
 129   // Get the exception oop.
 130   __ ld(R3_ARG1, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
 131 
 132   // Get the exception pc in case we are deoptimized.
 133   __ ld(R4_ARG2, in_bytes(JavaThread::exception_pc_offset()), R16_thread);
 134 
 135   // Reset thread values.
 136   __ li(R0, 0);
 137 #ifdef ASSERT
 138   __ std(R0, in_bytes(JavaThread::exception_handler_pc_offset()), R16_thread);
 139   __ std(R0, in_bytes(JavaThread::exception_pc_offset()), R16_thread);
 140 #endif
 141   // Clear the exception oop so GC no longer processes it as a root.
 142   __ std(R0, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
 143 
 144   // Move exception pc into SR_LR.
 145   __ mtlr(R4_ARG2);
 146   __ bctr();
 147 
 148   // Make sure all code is generated.
 149   masm->flush();
 150 
 151   // Set exception blob.
 152   _exception_blob = ExceptionBlob::create(&buffer, oop_maps,
 153                                           frame_size_in_bytes/wordSize);
 154 }
 155 
 156 #endif // COMPILER2