author | duke |
Wed, 05 Jul 2017 23:10:03 +0200 | |
changeset 44509 | 02253db2ace1 |
parent 41697 | 94ef14db8a20 |
child 46271 | 979ebd346ecf |
permissions | -rw-r--r-- |
33160 | 1 |
/* |
36063
96e86c7f8fec
8149689: [JVMCI] CodeInstaller::pd_patch_DataSectionReference should be able to throw exceptions
twisti
parents:
34153
diff
changeset
|
2 |
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. |
33160 | 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 |
||
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
39 |
jint CodeInstaller::pd_next_offset(NativeInstruction* inst, jint pc_offset, Handle method, TRAPS) { |
33160 | 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 |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
56 |
assert(method.not_null(), "only valid for virtual calls"); |
33160 | 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 { |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
62 |
JVMCI_ERROR_0("unsupported type of instruction for call site"); |
33160 | 63 |
} |
64 |
} |
|
65 |
||
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
66 |
void CodeInstaller::pd_patch_OopConstant(int pc_offset, Handle constant, TRAPS) { |
33160 | 67 |
address pc = _instructions->start() + pc_offset; |
68 |
Handle obj = HotSpotObjectConstantImpl::object(constant); |
|
69 |
jobject value = JNIHandles::make_local(obj()); |
|
70 |
if (HotSpotObjectConstantImpl::compressed(constant)) { |
|
71 |
#ifdef _LP64 |
|
72 |
address operand = Assembler::locate_operand(pc, Assembler::narrow_oop_operand); |
|
73 |
int oop_index = _oop_recorder->find_index(value); |
|
74 |
_instructions->relocate(pc, oop_Relocation::spec(oop_index), Assembler::narrow_oop_operand); |
|
75 |
TRACE_jvmci_3("relocating (narrow oop constant) at " PTR_FORMAT "/" PTR_FORMAT, p2i(pc), p2i(operand)); |
|
76 |
#else |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
77 |
JVMCI_ERROR("compressed oop on 32bit"); |
33160 | 78 |
#endif |
79 |
} else { |
|
80 |
address operand = Assembler::locate_operand(pc, Assembler::imm_operand); |
|
81 |
*((jobject*) operand) = value; |
|
82 |
_instructions->relocate(pc, oop_Relocation::spec_for_immediate(), Assembler::imm_operand); |
|
83 |
TRACE_jvmci_3("relocating (oop constant) at " PTR_FORMAT "/" PTR_FORMAT, p2i(pc), p2i(operand)); |
|
84 |
} |
|
85 |
} |
|
86 |
||
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
87 |
void CodeInstaller::pd_patch_MetaspaceConstant(int pc_offset, Handle constant, TRAPS) { |
33632 | 88 |
address pc = _instructions->start() + pc_offset; |
89 |
if (HotSpotMetaspaceConstantImpl::compressed(constant)) { |
|
90 |
#ifdef _LP64 |
|
91 |
address operand = Assembler::locate_operand(pc, Assembler::narrow_oop_operand); |
|
41697
94ef14db8a20
8166869: [JVMCI] record metadata relocations for metadata references
never
parents:
36842
diff
changeset
|
92 |
*((narrowKlass*) operand) = record_narrow_metadata_reference(_instructions, operand, constant, CHECK); |
33632 | 93 |
TRACE_jvmci_3("relocating (narrow metaspace constant) at " PTR_FORMAT "/" PTR_FORMAT, p2i(pc), p2i(operand)); |
94 |
#else |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
95 |
JVMCI_ERROR("compressed Klass* on 32bit"); |
33632 | 96 |
#endif |
97 |
} else { |
|
98 |
address operand = Assembler::locate_operand(pc, Assembler::imm_operand); |
|
41697
94ef14db8a20
8166869: [JVMCI] record metadata relocations for metadata references
never
parents:
36842
diff
changeset
|
99 |
*((void**) operand) = record_metadata_reference(_instructions, operand, constant, CHECK); |
33632 | 100 |
TRACE_jvmci_3("relocating (metaspace constant) at " PTR_FORMAT "/" PTR_FORMAT, p2i(pc), p2i(operand)); |
101 |
} |
|
102 |
} |
|
103 |
||
36063
96e86c7f8fec
8149689: [JVMCI] CodeInstaller::pd_patch_DataSectionReference should be able to throw exceptions
twisti
parents:
34153
diff
changeset
|
104 |
void CodeInstaller::pd_patch_DataSectionReference(int pc_offset, int data_offset, TRAPS) { |
33160 | 105 |
address pc = _instructions->start() + pc_offset; |
106 |
||
107 |
address operand = Assembler::locate_operand(pc, Assembler::disp32_operand); |
|
108 |
address next_instruction = Assembler::locate_next_instruction(pc); |
|
109 |
address dest = _constants->start() + data_offset; |
|
110 |
||
111 |
long disp = dest - next_instruction; |
|
112 |
assert(disp == (jint) disp, "disp doesn't fit in 32 bits"); |
|
113 |
*((jint*) operand) = (jint) disp; |
|
114 |
||
115 |
_instructions->relocate(pc, section_word_Relocation::spec((address) dest, CodeBuffer::SECT_CONSTS), Assembler::disp32_operand); |
|
116 |
TRACE_jvmci_3("relocating at " PTR_FORMAT "/" PTR_FORMAT " with destination at " PTR_FORMAT " (%d)", p2i(pc), p2i(operand), p2i(dest), data_offset); |
|
117 |
} |
|
118 |
||
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
119 |
void CodeInstaller::pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination, TRAPS) { |
33160 | 120 |
address pc = (address) inst; |
121 |
if (inst->is_call()) { |
|
122 |
// NOTE: for call without a mov, the offset must fit a 32-bit immediate |
|
123 |
// see also CompilerToVM.getMaxCallTargetOffset() |
|
124 |
NativeCall* call = nativeCall_at(pc); |
|
125 |
call->set_destination((address) foreign_call_destination); |
|
126 |
_instructions->relocate(call->instruction_address(), runtime_call_Relocation::spec(), Assembler::call32_operand); |
|
127 |
} else if (inst->is_mov_literal64()) { |
|
128 |
NativeMovConstReg* mov = nativeMovConstReg_at(pc); |
|
129 |
mov->set_data((intptr_t) foreign_call_destination); |
|
130 |
_instructions->relocate(mov->instruction_address(), runtime_call_Relocation::spec(), Assembler::imm_operand); |
|
131 |
} else if (inst->is_jump()) { |
|
132 |
NativeJump* jump = nativeJump_at(pc); |
|
133 |
jump->set_jump_destination((address) foreign_call_destination); |
|
134 |
_instructions->relocate(jump->instruction_address(), runtime_call_Relocation::spec(), Assembler::call32_operand); |
|
135 |
} else if (inst->is_cond_jump()) { |
|
136 |
address old_dest = nativeGeneralJump_at(pc)->jump_destination(); |
|
137 |
address disp = Assembler::locate_operand(pc, Assembler::call32_operand); |
|
138 |
*(jint*) disp += ((address) foreign_call_destination) - old_dest; |
|
139 |
_instructions->relocate(pc, runtime_call_Relocation::spec(), Assembler::call32_operand); |
|
140 |
} else { |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
141 |
JVMCI_ERROR("unsupported relocation for foreign call"); |
33160 | 142 |
} |
143 |
||
144 |
TRACE_jvmci_3("relocating (foreign call) at " PTR_FORMAT, p2i(inst)); |
|
145 |
} |
|
146 |
||
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
147 |
void CodeInstaller::pd_relocate_JavaMethod(Handle hotspot_method, jint pc_offset, TRAPS) { |
33160 | 148 |
#ifdef ASSERT |
149 |
Method* method = NULL; |
|
150 |
// we need to check, this might also be an unresolved method |
|
151 |
if (hotspot_method->is_a(HotSpotResolvedJavaMethodImpl::klass())) { |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
152 |
method = getMethodFromHotSpotMethod(hotspot_method()); |
33160 | 153 |
} |
154 |
#endif |
|
155 |
switch (_next_call_type) { |
|
156 |
case INLINE_INVOKE: |
|
157 |
break; |
|
158 |
case INVOKEVIRTUAL: |
|
159 |
case INVOKEINTERFACE: { |
|
160 |
assert(method == NULL || !method->is_static(), "cannot call static method with invokeinterface"); |
|
161 |
||
162 |
NativeCall* call = nativeCall_at(_instructions->start() + pc_offset); |
|
163 |
call->set_destination(SharedRuntime::get_resolve_virtual_call_stub()); |
|
164 |
_instructions->relocate(call->instruction_address(), |
|
165 |
virtual_call_Relocation::spec(_invoke_mark_pc), |
|
166 |
Assembler::call32_operand); |
|
167 |
break; |
|
168 |
} |
|
169 |
case INVOKESTATIC: { |
|
170 |
assert(method == NULL || method->is_static(), "cannot call non-static method with invokestatic"); |
|
171 |
||
172 |
NativeCall* call = nativeCall_at(_instructions->start() + pc_offset); |
|
173 |
call->set_destination(SharedRuntime::get_resolve_static_call_stub()); |
|
174 |
_instructions->relocate(call->instruction_address(), |
|
175 |
relocInfo::static_call_type, Assembler::call32_operand); |
|
176 |
break; |
|
177 |
} |
|
178 |
case INVOKESPECIAL: { |
|
179 |
assert(method == NULL || !method->is_static(), "cannot call static method with invokespecial"); |
|
180 |
NativeCall* call = nativeCall_at(_instructions->start() + pc_offset); |
|
181 |
call->set_destination(SharedRuntime::get_resolve_opt_virtual_call_stub()); |
|
182 |
_instructions->relocate(call->instruction_address(), |
|
183 |
relocInfo::opt_virtual_call_type, Assembler::call32_operand); |
|
184 |
break; |
|
185 |
} |
|
186 |
default: |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
187 |
JVMCI_ERROR("invalid _next_call_type value"); |
33160 | 188 |
break; |
189 |
} |
|
190 |
} |
|
191 |
||
192 |
static void relocate_poll_near(address pc) { |
|
193 |
NativeInstruction* ni = nativeInstruction_at(pc); |
|
194 |
int32_t* disp = (int32_t*) Assembler::locate_operand(pc, Assembler::disp32_operand); |
|
195 |
int32_t offset = *disp; // The Java code installed the polling page offset into the disp32 operand |
|
196 |
intptr_t new_disp = (intptr_t) (os::get_polling_page() + offset) - (intptr_t) ni; |
|
197 |
*disp = (int32_t)new_disp; |
|
198 |
} |
|
199 |
||
200 |
||
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
201 |
void CodeInstaller::pd_relocate_poll(address pc, jint mark, TRAPS) { |
33160 | 202 |
switch (mark) { |
203 |
case POLL_NEAR: { |
|
204 |
relocate_poll_near(pc); |
|
205 |
_instructions->relocate(pc, relocInfo::poll_type, Assembler::disp32_operand); |
|
206 |
break; |
|
207 |
} |
|
208 |
case POLL_FAR: |
|
209 |
// This is a load from a register so there is no relocatable operand. |
|
210 |
// We just have to ensure that the format is not disp32_operand |
|
211 |
// so that poll_Relocation::fix_relocation_after_move does the right |
|
212 |
// thing (i.e. ignores this relocation record) |
|
213 |
_instructions->relocate(pc, relocInfo::poll_type, Assembler::imm_operand); |
|
214 |
break; |
|
215 |
case POLL_RETURN_NEAR: { |
|
216 |
relocate_poll_near(pc); |
|
217 |
_instructions->relocate(pc, relocInfo::poll_return_type, Assembler::disp32_operand); |
|
218 |
break; |
|
219 |
} |
|
220 |
case POLL_RETURN_FAR: |
|
221 |
// see comment above for POLL_FAR |
|
222 |
_instructions->relocate(pc, relocInfo::poll_return_type, Assembler::imm_operand); |
|
223 |
break; |
|
224 |
default: |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
225 |
JVMCI_ERROR("invalid mark value: %d", mark); |
33160 | 226 |
break; |
227 |
} |
|
228 |
} |
|
229 |
||
230 |
// convert JVMCI register indices (as used in oop maps) to HotSpot registers |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
231 |
VMReg CodeInstaller::get_hotspot_reg(jint jvmci_reg, TRAPS) { |
33160 | 232 |
if (jvmci_reg < RegisterImpl::number_of_registers) { |
233 |
return as_Register(jvmci_reg)->as_VMReg(); |
|
234 |
} else { |
|
235 |
jint floatRegisterNumber = jvmci_reg - RegisterImpl::number_of_registers; |
|
236 |
if (floatRegisterNumber < XMMRegisterImpl::number_of_registers) { |
|
237 |
return as_XMMRegister(floatRegisterNumber)->as_VMReg(); |
|
238 |
} |
|
34153
cbcfa2a6fe0b
8139589: [JVMCI] throw exceptions in faulty code installation operations
twisti
parents:
33632
diff
changeset
|
239 |
JVMCI_ERROR_NULL("invalid register number: %d", jvmci_reg); |
33160 | 240 |
} |
241 |
} |
|
242 |
||
243 |
bool CodeInstaller::is_general_purpose_reg(VMReg hotspotRegister) { |
|
244 |
return !(hotspotRegister->is_FloatRegister() || hotspotRegister->is_XMMRegister()); |
|
245 |
} |