1 /*
   2  * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2018 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/macroAssembler.inline.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "code/vmreg.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "interpreter/interp_masm.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "nativeInst_ppc.hpp"
  35 #include "opto/runtime.hpp"
  36 #include "runtime/interfaceSupport.inline.hpp"
  37 #include "runtime/sharedRuntime.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 #include "runtime/vframeArray.hpp"
  40 #include "utilities/globalDefinitions.hpp"
  41 #endif
  42 
  43 #define __ masm->
  44 
  45 
  46 #ifdef COMPILER2
  47 
  48 //------------------------------generate_exception_blob---------------------------
  49 // Creates exception blob at the end.
  50 // Using exception blob, this code is jumped from a compiled method.
  51 //
  52 // Given an exception pc at a call we call into the runtime for the
  53 // handler in this method. This handler might merely restore state
  54 // (i.e. callee save registers) unwind the frame and jump to the
  55 // exception handler for the nmethod if there is no Java level handler
  56 // for the nmethod.
  57 //
  58 // This code is entered with a jmp.
  59 //
  60 // Arguments:
  61 //   R3_ARG1: exception oop
  62 //   R4_ARG2: exception pc
  63 //
  64 // Results:
  65 //   R3_ARG1: exception oop
  66 //   R4_ARG2: exception pc in caller
  67 //   destination: exception handler of caller
  68 //
  69 // Note: the exception pc MUST be at a call (precise debug information)
  70 //
  71 void OptoRuntime::generate_exception_blob() {
  72   // Allocate space for the code.
  73   ResourceMark rm;
  74   // Setup code generation tools.
  75   CodeBuffer buffer("exception_blob", 2048, 1024);
  76   InterpreterMacroAssembler* masm = new InterpreterMacroAssembler(&buffer);
  77 
  78   address start = __ pc();
  79 
  80   int frame_size_in_bytes = frame::abi_reg_args_size;
  81   OopMap* map = new OopMap(frame_size_in_bytes / sizeof(jint), 0);
  82 
  83   // Exception pc is 'return address' for stack walker.
  84   __ std(R4_ARG2/*exception pc*/, _abi(lr), R1_SP);
  85 
  86   // Store the exception in the Thread object.
  87   __ std(R3_ARG1/*exception oop*/, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
  88   __ std(R4_ARG2/*exception pc*/,  in_bytes(JavaThread::exception_pc_offset()),  R16_thread);
  89 
  90   // Save callee-saved registers.
  91   // Push a C frame for the exception blob. It is needed for the C call later on.
  92   __ push_frame_reg_args(0, R11_scratch1);
  93 
  94   // This call does all the hard work. It checks if an exception handler
  95   // exists in the method.
  96   // If so, it returns the handler address.
  97   // If not, it prepares for stack-unwinding, restoring the callee-save
  98   // registers of the frame being removed.
  99   __ set_last_Java_frame(/*sp=*/R1_SP, noreg);
 100 
 101   __ mr(R3_ARG1, R16_thread);
 102 #if defined(ABI_ELFv2)
 103   __ call_c((address) OptoRuntime::handle_exception_C, relocInfo::none);
 104 #else
 105   __ call_c(CAST_FROM_FN_PTR(FunctionDescriptor*, OptoRuntime::handle_exception_C),
 106             relocInfo::none);
 107 #endif
 108   address calls_return_pc = __ last_calls_return_pc();
 109 # ifdef ASSERT
 110   __ cmpdi(CCR0, R3_RET, 0);
 111   __ asm_assert_ne("handle_exception_C must not return NULL", 0x601);
 112 # endif
 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   OopMapSet* oop_maps = new OopMapSet();
 120   oop_maps->add_gc_map(calls_return_pc - start, map);
 121 
 122   __ mtctr(R3_RET); // Move address of exception handler to SR_CTR.
 123   __ reset_last_Java_frame();
 124   __ pop_frame();
 125 
 126   // We have a handler in register SR_CTR (could be deopt blob).
 127 
 128   // Get the exception oop.
 129   __ ld(R3_ARG1, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
 130 
 131   // Get the exception pc in case we are deoptimized.
 132   __ ld(R4_ARG2, in_bytes(JavaThread::exception_pc_offset()), R16_thread);
 133 
 134   // Reset thread values.
 135   __ li(R0, 0);
 136 #ifdef ASSERT
 137   __ std(R0, in_bytes(JavaThread::exception_handler_pc_offset()), R16_thread);
 138   __ std(R0, in_bytes(JavaThread::exception_pc_offset()), R16_thread);
 139 #endif
 140   // Clear the exception oop so GC no longer processes it as a root.
 141   __ std(R0, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
 142 
 143   // Move exception pc into SR_LR.
 144   __ mtlr(R4_ARG2);
 145   __ bctr();
 146 
 147   // Make sure all code is generated.
 148   masm->flush();
 149 
 150   // Set exception blob.
 151   _exception_blob = ExceptionBlob::create(&buffer, oop_maps,
 152                                           frame_size_in_bytes/wordSize);
 153 }
 154 
 155 #endif // COMPILER2