hotspot/src/cpu/x86/vm/jvmciCodeInstaller_x86.cpp
changeset 33160 c59f1676d27e
child 33632 038347770a9e
equal deleted inserted replaced
33159:89b942323bd1 33160:c59f1676d27e
       
     1 /*
       
     2  * Copyright (c) 2013, 2015, 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 "precompiled.hpp"
       
    25 #include "compiler/disassembler.hpp"
       
    26 #include "oops/oop.inline.hpp"
       
    27 #include "runtime/javaCalls.hpp"
       
    28 #include "runtime/sharedRuntime.hpp"
       
    29 #include "jvmci/jvmciEnv.hpp"
       
    30 #include "jvmci/jvmciCodeInstaller.hpp"
       
    31 #include "jvmci/jvmciJavaClasses.hpp"
       
    32 #include "jvmci/jvmciCompilerToVM.hpp"
       
    33 #include "jvmci/jvmciRuntime.hpp"
       
    34 #include "asm/register.hpp"
       
    35 #include "classfile/vmSymbols.hpp"
       
    36 #include "code/vmreg.hpp"
       
    37 #include "vmreg_x86.inline.hpp"
       
    38 
       
    39 jint CodeInstaller::pd_next_offset(NativeInstruction* inst, jint pc_offset, oop method) {
       
    40   if (inst->is_call() || inst->is_jump()) {
       
    41     assert(NativeCall::instruction_size == (int)NativeJump::instruction_size, "unexpected size");
       
    42     return (pc_offset + NativeCall::instruction_size);
       
    43   } else if (inst->is_mov_literal64()) {
       
    44     // mov+call instruction pair
       
    45     jint offset = pc_offset + NativeMovConstReg::instruction_size;
       
    46     u_char* call = (u_char*) (_instructions->start() + offset);
       
    47     if (call[0] == Assembler::REX_B) {
       
    48       offset += 1; /* prefix byte for extended register R8-R15 */
       
    49       call++;
       
    50     }
       
    51     assert(call[0] == 0xFF, "expected call");
       
    52     offset += 2; /* opcode byte + modrm byte */
       
    53     return (offset);
       
    54   } else if (inst->is_call_reg()) {
       
    55     // the inlined vtable stub contains a "call register" instruction
       
    56     assert(method != NULL, "only valid for virtual calls");
       
    57     return (pc_offset + ((NativeCallReg *) inst)->next_instruction_offset());
       
    58   } else if (inst->is_cond_jump()) {
       
    59     address pc = (address) (inst);
       
    60     return pc_offset + (jint) (Assembler::locate_next_instruction(pc) - pc);
       
    61   } else {
       
    62     fatal("unsupported type of instruction for call site");
       
    63     return 0;
       
    64   }
       
    65 }
       
    66 
       
    67 void CodeInstaller::pd_patch_OopConstant(int pc_offset, Handle& constant) {
       
    68   address pc = _instructions->start() + pc_offset;
       
    69   Handle obj = HotSpotObjectConstantImpl::object(constant);
       
    70   jobject value = JNIHandles::make_local(obj());
       
    71   if (HotSpotObjectConstantImpl::compressed(constant)) {
       
    72 #ifdef _LP64
       
    73     address operand = Assembler::locate_operand(pc, Assembler::narrow_oop_operand);
       
    74     int oop_index = _oop_recorder->find_index(value);
       
    75     _instructions->relocate(pc, oop_Relocation::spec(oop_index), Assembler::narrow_oop_operand);
       
    76     TRACE_jvmci_3("relocating (narrow oop constant) at " PTR_FORMAT "/" PTR_FORMAT, p2i(pc), p2i(operand));
       
    77 #else
       
    78     fatal("compressed oop on 32bit");
       
    79 #endif
       
    80   } else {
       
    81     address operand = Assembler::locate_operand(pc, Assembler::imm_operand);
       
    82     *((jobject*) operand) = value;
       
    83     _instructions->relocate(pc, oop_Relocation::spec_for_immediate(), Assembler::imm_operand);
       
    84     TRACE_jvmci_3("relocating (oop constant) at " PTR_FORMAT "/" PTR_FORMAT, p2i(pc), p2i(operand));
       
    85   }
       
    86 }
       
    87 
       
    88 void CodeInstaller::pd_patch_DataSectionReference(int pc_offset, int data_offset) {
       
    89   address pc = _instructions->start() + pc_offset;
       
    90 
       
    91   address operand = Assembler::locate_operand(pc, Assembler::disp32_operand);
       
    92   address next_instruction = Assembler::locate_next_instruction(pc);
       
    93   address dest = _constants->start() + data_offset;
       
    94 
       
    95   long disp = dest - next_instruction;
       
    96   assert(disp == (jint) disp, "disp doesn't fit in 32 bits");
       
    97   *((jint*) operand) = (jint) disp;
       
    98 
       
    99   _instructions->relocate(pc, section_word_Relocation::spec((address) dest, CodeBuffer::SECT_CONSTS), Assembler::disp32_operand);
       
   100   TRACE_jvmci_3("relocating at " PTR_FORMAT "/" PTR_FORMAT " with destination at " PTR_FORMAT " (%d)", p2i(pc), p2i(operand), p2i(dest), data_offset);
       
   101 }
       
   102 
       
   103 void CodeInstaller::pd_relocate_CodeBlob(CodeBlob* cb, NativeInstruction* inst) {
       
   104   if (cb->is_nmethod()) {
       
   105     nmethod* nm = (nmethod*) cb;
       
   106     nativeJump_at((address)inst)->set_jump_destination(nm->verified_entry_point());
       
   107   } else {
       
   108     nativeJump_at((address)inst)->set_jump_destination(cb->code_begin());
       
   109   }
       
   110   _instructions->relocate((address)inst, runtime_call_Relocation::spec(), Assembler::call32_operand);
       
   111 }
       
   112 
       
   113 void CodeInstaller::pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination) {
       
   114   address pc = (address) inst;
       
   115   if (inst->is_call()) {
       
   116     // NOTE: for call without a mov, the offset must fit a 32-bit immediate
       
   117     //       see also CompilerToVM.getMaxCallTargetOffset()
       
   118     NativeCall* call = nativeCall_at(pc);
       
   119     call->set_destination((address) foreign_call_destination);
       
   120     _instructions->relocate(call->instruction_address(), runtime_call_Relocation::spec(), Assembler::call32_operand);
       
   121   } else if (inst->is_mov_literal64()) {
       
   122     NativeMovConstReg* mov = nativeMovConstReg_at(pc);
       
   123     mov->set_data((intptr_t) foreign_call_destination);
       
   124     _instructions->relocate(mov->instruction_address(), runtime_call_Relocation::spec(), Assembler::imm_operand);
       
   125   } else if (inst->is_jump()) {
       
   126     NativeJump* jump = nativeJump_at(pc);
       
   127     jump->set_jump_destination((address) foreign_call_destination);
       
   128     _instructions->relocate(jump->instruction_address(), runtime_call_Relocation::spec(), Assembler::call32_operand);
       
   129   } else if (inst->is_cond_jump()) {
       
   130     address old_dest = nativeGeneralJump_at(pc)->jump_destination();
       
   131     address disp = Assembler::locate_operand(pc, Assembler::call32_operand);
       
   132     *(jint*) disp += ((address) foreign_call_destination) - old_dest;
       
   133     _instructions->relocate(pc, runtime_call_Relocation::spec(), Assembler::call32_operand);
       
   134   } else {
       
   135     fatal("unsupported relocation for foreign call");
       
   136   }
       
   137 
       
   138   TRACE_jvmci_3("relocating (foreign call)  at " PTR_FORMAT, p2i(inst));
       
   139 }
       
   140 
       
   141 void CodeInstaller::pd_relocate_JavaMethod(oop hotspot_method, jint pc_offset) {
       
   142 #ifdef ASSERT
       
   143   Method* method = NULL;
       
   144   // we need to check, this might also be an unresolved method
       
   145   if (hotspot_method->is_a(HotSpotResolvedJavaMethodImpl::klass())) {
       
   146     method = getMethodFromHotSpotMethod(hotspot_method);
       
   147   }
       
   148 #endif
       
   149   switch (_next_call_type) {
       
   150     case INLINE_INVOKE:
       
   151       break;
       
   152     case INVOKEVIRTUAL:
       
   153     case INVOKEINTERFACE: {
       
   154       assert(method == NULL || !method->is_static(), "cannot call static method with invokeinterface");
       
   155 
       
   156       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
       
   157       call->set_destination(SharedRuntime::get_resolve_virtual_call_stub());
       
   158       _instructions->relocate(call->instruction_address(),
       
   159                                              virtual_call_Relocation::spec(_invoke_mark_pc),
       
   160                                              Assembler::call32_operand);
       
   161       break;
       
   162     }
       
   163     case INVOKESTATIC: {
       
   164       assert(method == NULL || method->is_static(), "cannot call non-static method with invokestatic");
       
   165 
       
   166       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
       
   167       call->set_destination(SharedRuntime::get_resolve_static_call_stub());
       
   168       _instructions->relocate(call->instruction_address(),
       
   169                                              relocInfo::static_call_type, Assembler::call32_operand);
       
   170       break;
       
   171     }
       
   172     case INVOKESPECIAL: {
       
   173       assert(method == NULL || !method->is_static(), "cannot call static method with invokespecial");
       
   174       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
       
   175       call->set_destination(SharedRuntime::get_resolve_opt_virtual_call_stub());
       
   176       _instructions->relocate(call->instruction_address(),
       
   177                               relocInfo::opt_virtual_call_type, Assembler::call32_operand);
       
   178       break;
       
   179     }
       
   180     default:
       
   181       break;
       
   182   }
       
   183 }
       
   184 
       
   185 static void relocate_poll_near(address pc) {
       
   186   NativeInstruction* ni = nativeInstruction_at(pc);
       
   187   int32_t* disp = (int32_t*) Assembler::locate_operand(pc, Assembler::disp32_operand);
       
   188   int32_t offset = *disp; // The Java code installed the polling page offset into the disp32 operand
       
   189   intptr_t new_disp = (intptr_t) (os::get_polling_page() + offset) - (intptr_t) ni;
       
   190   *disp = (int32_t)new_disp;
       
   191 }
       
   192 
       
   193 
       
   194 void CodeInstaller::pd_relocate_poll(address pc, jint mark) {
       
   195   switch (mark) {
       
   196     case POLL_NEAR: {
       
   197       relocate_poll_near(pc);
       
   198       _instructions->relocate(pc, relocInfo::poll_type, Assembler::disp32_operand);
       
   199       break;
       
   200     }
       
   201     case POLL_FAR:
       
   202       // This is a load from a register so there is no relocatable operand.
       
   203       // We just have to ensure that the format is not disp32_operand
       
   204       // so that poll_Relocation::fix_relocation_after_move does the right
       
   205       // thing (i.e. ignores this relocation record)
       
   206       _instructions->relocate(pc, relocInfo::poll_type, Assembler::imm_operand);
       
   207       break;
       
   208     case POLL_RETURN_NEAR: {
       
   209       relocate_poll_near(pc);
       
   210       _instructions->relocate(pc, relocInfo::poll_return_type, Assembler::disp32_operand);
       
   211       break;
       
   212     }
       
   213     case POLL_RETURN_FAR:
       
   214       // see comment above for POLL_FAR
       
   215       _instructions->relocate(pc, relocInfo::poll_return_type, Assembler::imm_operand);
       
   216       break;
       
   217     default:
       
   218       fatal("invalid mark value");
       
   219       break;
       
   220   }
       
   221 }
       
   222 
       
   223 // convert JVMCI register indices (as used in oop maps) to HotSpot registers
       
   224 VMReg CodeInstaller::get_hotspot_reg(jint jvmci_reg) {
       
   225   if (jvmci_reg < RegisterImpl::number_of_registers) {
       
   226     return as_Register(jvmci_reg)->as_VMReg();
       
   227   } else {
       
   228     jint floatRegisterNumber = jvmci_reg - RegisterImpl::number_of_registers;
       
   229     if (floatRegisterNumber < XMMRegisterImpl::number_of_registers) {
       
   230       return as_XMMRegister(floatRegisterNumber)->as_VMReg();
       
   231     }
       
   232     ShouldNotReachHere();
       
   233     return NULL;
       
   234   }
       
   235 }
       
   236 
       
   237 bool CodeInstaller::is_general_purpose_reg(VMReg hotspotRegister) {
       
   238   return !(hotspotRegister->is_FloatRegister() || hotspotRegister->is_XMMRegister());
       
   239 }