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