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