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 }
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(CodeBuffer &cbuf, 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 _instructions->relocate(call->instruction_address(), virtual_call_Relocation::spec(_invoke_mark_pc));
128 call->trampoline_jump(cbuf, SharedRuntime::get_resolve_virtual_call_stub());
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 _instructions->relocate(call->instruction_address(), relocInfo::static_call_type);
135 call->trampoline_jump(cbuf, SharedRuntime::get_resolve_static_call_stub());
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 _instructions->relocate(call->instruction_address(), relocInfo::opt_virtual_call_type);
142 call->trampoline_jump(cbuf, SharedRuntime::get_resolve_opt_virtual_call_stub());
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:
|
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 if (NativeInstruction::is_adrp_at((address)inst)) {
38 // adrp; add; blr
39 return pc_offset + 3 * NativeInstruction::instruction_size;
40 } else {
41 JVMCI_ERROR_0("unsupported type of instruction for call site");
42 }
43 }
44
45 void CodeInstaller::pd_patch_OopConstant(int pc_offset, Handle constant, TRAPS) {
46 address pc = _instructions->start() + pc_offset;
47 #ifdef ASSERT
48 {
49 NativeInstruction *insn = nativeInstruction_at(pc);
50 if (HotSpotObjectConstantImpl::compressed(constant)) {
51 // Mov narrow constant: movz n << 16, movk
52 assert(Instruction_aarch64::extract(insn->encoding(), 31, 21) == 0b11010010101 &&
53 nativeInstruction_at(pc+4)->is_movk(), "wrong insn in patch");
54 } else {
55 // Move wide constant: movz n, movk, movk.
56 assert(nativeInstruction_at(pc+4)->is_movk()
57 && nativeInstruction_at(pc+8)->is_movk(), "wrong insn in patch");
58 }
59 }
66 _instructions->relocate(pc, rspec);
67 }
68
69 void CodeInstaller::pd_patch_MetaspaceConstant(int pc_offset, Handle constant, TRAPS) {
70 address pc = _instructions->start() + pc_offset;
71 if (HotSpotMetaspaceConstantImpl::compressed(constant)) {
72 narrowKlass narrowOop = record_narrow_metadata_reference(_instructions, pc, constant, CHECK);
73 MacroAssembler::patch_narrow_klass(pc, narrowOop);
74 TRACE_jvmci_3("relocating (narrow metaspace constant) at " PTR_FORMAT "/0x%x", p2i(pc), narrowOop);
75 } else {
76 NativeMovConstReg* move = nativeMovConstReg_at(pc);
77 void* reference = record_metadata_reference(_instructions, pc, constant, CHECK);
78 move->set_data((intptr_t) reference);
79 TRACE_jvmci_3("relocating (metaspace constant) at " PTR_FORMAT "/" PTR_FORMAT, p2i(pc), p2i(reference));
80 }
81 }
82
83 void CodeInstaller::pd_patch_DataSectionReference(int pc_offset, int data_offset, TRAPS) {
84 address pc = _instructions->start() + pc_offset;
85 NativeInstruction* inst = nativeInstruction_at(pc);
86 if (inst->is_adr_aligned() || inst->is_ldr_literal()
87 || (NativeInstruction::maybe_cpool_ref(pc))) {
88 address dest = _constants->start() + data_offset;
89 _instructions->relocate(pc, section_word_Relocation::spec((address) dest, CodeBuffer::SECT_CONSTS));
90 TRACE_jvmci_3("relocating at " PTR_FORMAT " (+%d) with destination at %d", p2i(pc), pc_offset, data_offset);
91 } else {
92 JVMCI_ERROR("unknown load or move instruction at " PTR_FORMAT, p2i(pc));
93 }
94 }
95
96 void CodeInstaller::pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination, TRAPS) {
97 address pc = (address) inst;
98 if (inst->is_call()) {
99 NativeCall* call = nativeCall_at(pc);
100 call->set_destination((address) foreign_call_destination);
101 _instructions->relocate(call->instruction_address(), runtime_call_Relocation::spec());
102 } else if (inst->is_jump()) {
103 NativeJump* jump = nativeJump_at(pc);
104 jump->set_jump_destination((address) foreign_call_destination);
105 _instructions->relocate(jump->instruction_address(), runtime_call_Relocation::spec());
106 } else if (inst->is_general_jump()) {
107 NativeGeneralJump* jump = nativeGeneralJump_at(pc);
108 jump->set_jump_destination((address) foreign_call_destination);
109 _instructions->relocate(jump->instruction_address(), runtime_call_Relocation::spec());
110 } else if (NativeInstruction::is_adrp_at((address)inst)) {
111 // adrp; add; blr
112 MacroAssembler::pd_patch_instruction_size((address)inst,
113 (address)foreign_call_destination);
114 } else {
115 JVMCI_ERROR("unknown call or jump instruction at " PTR_FORMAT, p2i(pc));
116 }
117 TRACE_jvmci_3("relocating (foreign call) at " PTR_FORMAT, p2i(inst));
118 }
119
120 void CodeInstaller::pd_relocate_JavaMethod(CodeBuffer &cbuf, Handle hotspot_method, jint pc_offset, TRAPS) {
121 #ifdef ASSERT
122 Method* method = NULL;
123 // we need to check, this might also be an unresolved method
124 if (hotspot_method->is_a(HotSpotResolvedJavaMethodImpl::klass())) {
125 method = getMethodFromHotSpotMethod(hotspot_method());
126 }
127 #endif
128 switch (_next_call_type) {
129 case INLINE_INVOKE:
130 break;
131 case INVOKEVIRTUAL:
132 case INVOKEINTERFACE: {
133 assert(method == NULL || !method->is_static(), "cannot call static method with invokeinterface");
134 NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
135 _instructions->relocate(call->instruction_address(), virtual_call_Relocation::spec(_invoke_mark_pc));
136 // Don't create trampolines for immutable PIC.
137 if (!_immutable_pic_compilation) {
138 call->trampoline_jump(cbuf, SharedRuntime::get_resolve_virtual_call_stub());
139 }
140 break;
141 }
142 case INVOKESTATIC: {
143 assert(method == NULL || method->is_static(), "cannot call non-static method with invokestatic");
144 NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
145 _instructions->relocate(call->instruction_address(), relocInfo::static_call_type);
146 // Don't create trampolines for immutable PIC.
147 if (!_immutable_pic_compilation) {
148 call->trampoline_jump(cbuf, SharedRuntime::get_resolve_static_call_stub());
149 }
150 break;
151 }
152 case INVOKESPECIAL: {
153 assert(method == NULL || !method->is_static(), "cannot call static method with invokespecial");
154 NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
155 _instructions->relocate(call->instruction_address(), relocInfo::opt_virtual_call_type);
156 // Don't create trampolines for immutable PIC.
157 if (!_immutable_pic_compilation) {
158 call->trampoline_jump(cbuf, SharedRuntime::get_resolve_opt_virtual_call_stub());
159 }
160 break;
161 }
162 default:
163 JVMCI_ERROR("invalid _next_call_type value");
164 break;
165 }
166 }
167
168 void CodeInstaller::pd_relocate_poll(address pc, jint mark, TRAPS) {
169 switch (mark) {
170 case POLL_NEAR:
171 JVMCI_ERROR("unimplemented");
172 break;
173 case POLL_FAR:
174 _instructions->relocate(pc, relocInfo::poll_type);
175 break;
176 case POLL_RETURN_NEAR:
177 JVMCI_ERROR("unimplemented");
178 break;
179 case POLL_RETURN_FAR:
|