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