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