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