1 /*
   2  * Copyright (c) 2015, 2016, 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 #include "jvmci/jvmciCodeInstaller.hpp"
  25 #include "jvmci/jvmciRuntime.hpp"
  26 #include "jvmci/jvmciCompilerToVM.hpp"
  27 #include "jvmci/jvmciJavaClasses.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "runtime/sharedRuntime.hpp"
  30 #include "vmreg_aarch64.inline.hpp"
  31 
  32 jint CodeInstaller::pd_next_offset(NativeInstruction* inst, jint pc_offset, Handle method, TRAPS) {
  33   if (inst->is_call() || inst->is_jump() || inst->is_blr()) {
  34     return pc_offset + NativeCall::instruction_size;
  35   } else if (inst->is_general_jump()) {
  36     return pc_offset + NativeGeneralJump::instruction_size;
  37   } else {
  38     JVMCI_ERROR_0("unsupported type of instruction for call site");
  39   }
  40 }
  41 
  42 void CodeInstaller::pd_patch_OopConstant(int pc_offset, Handle constant, TRAPS) {
  43   address pc = _instructions->start() + pc_offset;
  44   Handle obj = HotSpotObjectConstantImpl::object(constant);
  45   jobject value = JNIHandles::make_local(obj());
  46   MacroAssembler::patch_oop(pc, (address)obj());
  47   int oop_index = _oop_recorder->find_index(value);
  48   RelocationHolder rspec = oop_Relocation::spec(oop_index);
  49   _instructions->relocate(pc, rspec);
  50 }
  51 
  52 void CodeInstaller::pd_patch_MetaspaceConstant(int pc_offset, Handle constant, TRAPS) {
  53   address pc = _instructions->start() + pc_offset;
  54   if (HotSpotMetaspaceConstantImpl::compressed(constant)) {
  55     narrowKlass narrowOop = record_narrow_metadata_reference(_instructions, pc, constant, CHECK);
  56     MacroAssembler::patch_narrow_klass(pc, narrowOop);
  57     TRACE_jvmci_3("relocating (narrow metaspace constant) at " PTR_FORMAT "/0x%x", p2i(pc), narrowOop);
  58   } else {
  59     NativeMovConstReg* move = nativeMovConstReg_at(pc);
  60     void* reference = record_metadata_reference(_instructions, pc, constant, CHECK);
  61     move->set_data((intptr_t) reference);
  62     TRACE_jvmci_3("relocating (metaspace constant) at " PTR_FORMAT "/" PTR_FORMAT, p2i(pc), p2i(reference));
  63   }
  64 }
  65 
  66 void CodeInstaller::pd_patch_DataSectionReference(int pc_offset, int data_offset, TRAPS) {
  67   address pc = _instructions->start() + pc_offset;
  68   NativeInstruction* inst = nativeInstruction_at(pc);
  69   if (inst->is_adr_aligned() || inst->is_ldr_literal()) {
  70     address dest = _constants->start() + data_offset;
  71     _instructions->relocate(pc, section_word_Relocation::spec((address) dest, CodeBuffer::SECT_CONSTS));
  72     TRACE_jvmci_3("relocating at " PTR_FORMAT " (+%d) with destination at %d", p2i(pc), pc_offset, data_offset);
  73   } else {
  74     JVMCI_ERROR("unknown load or move instruction at " PTR_FORMAT, p2i(pc));
  75   }
  76 }
  77 
  78 void CodeInstaller::pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination, TRAPS) {
  79   address pc = (address) inst;
  80   if (inst->is_call()) {
  81     NativeCall* call = nativeCall_at(pc);
  82     call->set_destination((address) foreign_call_destination);
  83     _instructions->relocate(call->instruction_address(), runtime_call_Relocation::spec());
  84   } else if (inst->is_jump()) {
  85     NativeJump* jump = nativeJump_at(pc);
  86     jump->set_jump_destination((address) foreign_call_destination);
  87     _instructions->relocate(jump->instruction_address(), runtime_call_Relocation::spec());
  88   } else if (inst->is_general_jump()) {
  89     NativeGeneralJump* jump = nativeGeneralJump_at(pc);
  90     jump->set_jump_destination((address) foreign_call_destination);
  91     _instructions->relocate(jump->instruction_address(), runtime_call_Relocation::spec());
  92   } else {
  93     JVMCI_ERROR("unknown call or jump instruction at " PTR_FORMAT, p2i(pc));
  94   }
  95   TRACE_jvmci_3("relocating (foreign call) at " PTR_FORMAT, p2i(inst));
  96 }
  97 
  98 void CodeInstaller::pd_relocate_JavaMethod(Handle hotspot_method, jint pc_offset, TRAPS) {
  99 #ifdef ASSERT
 100   Method* method = NULL;
 101   // we need to check, this might also be an unresolved method
 102   if (hotspot_method->is_a(HotSpotResolvedJavaMethodImpl::klass())) {
 103     method = getMethodFromHotSpotMethod(hotspot_method());
 104   }
 105 #endif
 106   switch (_next_call_type) {
 107     case INLINE_INVOKE:
 108       break;
 109     case INVOKEVIRTUAL:
 110     case INVOKEINTERFACE: {
 111       assert(method == NULL || !method->is_static(), "cannot call static method with invokeinterface");
 112       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
 113       call->set_destination(SharedRuntime::get_resolve_virtual_call_stub());
 114       _instructions->relocate(call->instruction_address(), virtual_call_Relocation::spec(_invoke_mark_pc));
 115       break;
 116     }
 117     case INVOKESTATIC: {
 118       assert(method == NULL || method->is_static(), "cannot call non-static method with invokestatic");
 119       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
 120       call->set_destination(SharedRuntime::get_resolve_static_call_stub());
 121       _instructions->relocate(call->instruction_address(), relocInfo::static_call_type);
 122       break;
 123     }
 124     case INVOKESPECIAL: {
 125       assert(method == NULL || !method->is_static(), "cannot call static method with invokespecial");
 126       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
 127       call->set_destination(SharedRuntime::get_resolve_opt_virtual_call_stub());
 128       _instructions->relocate(call->instruction_address(), relocInfo::opt_virtual_call_type);
 129       break;
 130     }
 131     default:
 132       JVMCI_ERROR("invalid _next_call_type value");
 133       break;
 134   }
 135 }
 136 
 137 void CodeInstaller::pd_relocate_poll(address pc, jint mark, TRAPS) {
 138   switch (mark) {
 139     case POLL_NEAR:
 140       JVMCI_ERROR("unimplemented");
 141       break;
 142     case POLL_FAR:
 143       _instructions->relocate(pc, relocInfo::poll_type);
 144       break;
 145     case POLL_RETURN_NEAR:
 146       JVMCI_ERROR("unimplemented");
 147       break;
 148     case POLL_RETURN_FAR:
 149       _instructions->relocate(pc, relocInfo::poll_return_type);
 150       break;
 151     default:
 152       JVMCI_ERROR("invalid mark value");
 153       break;
 154   }
 155 }
 156 
 157 // convert JVMCI register indices (as used in oop maps) to HotSpot registers
 158 VMReg CodeInstaller::get_hotspot_reg(jint jvmci_reg, TRAPS) {
 159   if (jvmci_reg < RegisterImpl::number_of_registers) {
 160     return as_Register(jvmci_reg)->as_VMReg();
 161   } else {
 162     jint floatRegisterNumber = jvmci_reg - RegisterImpl::number_of_registers_for_jvmci;
 163     if (floatRegisterNumber >= 0 && floatRegisterNumber < FloatRegisterImpl::number_of_registers) {
 164       return as_FloatRegister(floatRegisterNumber)->as_VMReg();
 165     }
 166     JVMCI_ERROR_NULL("invalid register number: %d", jvmci_reg);
 167   }
 168 }
 169 
 170 bool CodeInstaller::is_general_purpose_reg(VMReg hotspotRegister) {
 171   return !hotspotRegister->is_FloatRegister();
 172 }