annotate src/cpu/x86/vm/nativeInst_x86.cpp @ 21559:be896a1983c0

recast all Graal native code as JVMCI code (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Thu, 28 May 2015 15:36:48 +0200
parents 52b4284cb496
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
17937
78bbf4d43a14 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 7199
diff changeset
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 304
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 304
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 304
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
25 #include "precompiled.hpp"
7199
cd3d6a6b95d9 8003240: x86: move MacroAssembler into separate file
twisti
parents: 4759
diff changeset
26 #include "asm/macroAssembler.hpp"
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
27 #include "memory/resourceArea.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
28 #include "nativeInst_x86.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
29 #include "oops/oop.inline.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
30 #include "runtime/handles.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
31 #include "runtime/sharedRuntime.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
32 #include "runtime/stubRoutines.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
33 #include "utilities/ostream.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
34 #ifdef COMPILER1
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
35 #include "c1/c1_Runtime1.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
36 #endif
0
a61af66fc99e Initial load
duke
parents:
diff changeset
37
17937
78bbf4d43a14 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 7199
diff changeset
38 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
78bbf4d43a14 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 7199
diff changeset
39
0
a61af66fc99e Initial load
duke
parents:
diff changeset
40 void NativeInstruction::wrote(int offset) {
a61af66fc99e Initial load
duke
parents:
diff changeset
41 ICache::invalidate_word(addr_at(offset));
a61af66fc99e Initial load
duke
parents:
diff changeset
42 }
a61af66fc99e Initial load
duke
parents:
diff changeset
43
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 void NativeCall::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // Make sure code pattern is actually a call imm32 instruction.
a61af66fc99e Initial load
duke
parents:
diff changeset
47 int inst = ubyte_at(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
48 if (inst != instruction_code) {
a61af66fc99e Initial load
duke
parents:
diff changeset
49 tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", instruction_address(),
a61af66fc99e Initial load
duke
parents:
diff changeset
50 inst);
a61af66fc99e Initial load
duke
parents:
diff changeset
51 fatal("not a call disp32");
a61af66fc99e Initial load
duke
parents:
diff changeset
52 }
a61af66fc99e Initial load
duke
parents:
diff changeset
53 }
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 address NativeCall::destination() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
56 // Getting the destination of a call isn't safe because that call can
a61af66fc99e Initial load
duke
parents:
diff changeset
57 // be getting patched while you're calling this. There's only special
a61af66fc99e Initial load
duke
parents:
diff changeset
58 // places where this can be called but not automatically verifiable by
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // checking which locks are held. The solution is true atomic patching
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // on x86, nyi.
a61af66fc99e Initial load
duke
parents:
diff changeset
61 return return_address() + displacement();
a61af66fc99e Initial load
duke
parents:
diff changeset
62 }
a61af66fc99e Initial load
duke
parents:
diff changeset
63
a61af66fc99e Initial load
duke
parents:
diff changeset
64 void NativeCall::print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
65 tty->print_cr(PTR_FORMAT ": call " PTR_FORMAT,
a61af66fc99e Initial load
duke
parents:
diff changeset
66 instruction_address(), destination());
a61af66fc99e Initial load
duke
parents:
diff changeset
67 }
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 // Inserts a native call instruction at a given pc
a61af66fc99e Initial load
duke
parents:
diff changeset
70 void NativeCall::insert(address code_pos, address entry) {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
a61af66fc99e Initial load
duke
parents:
diff changeset
72 #ifdef AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
73 guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset");
a61af66fc99e Initial load
duke
parents:
diff changeset
74 #endif // AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
75 *code_pos = instruction_code;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 *((int32_t *)(code_pos+1)) = (int32_t) disp;
a61af66fc99e Initial load
duke
parents:
diff changeset
77 ICache::invalidate_range(code_pos, instruction_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 // MT-safe patching of a call instruction.
a61af66fc99e Initial load
duke
parents:
diff changeset
81 // First patches first word of instruction to two jmp's that jmps to them
a61af66fc99e Initial load
duke
parents:
diff changeset
82 // selfs (spinlock). Then patches the last byte, and then atomicly replaces
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // the jmp's with the first 4 byte of the new instruction.
a61af66fc99e Initial load
duke
parents:
diff changeset
84 void NativeCall::replace_mt_safe(address instr_addr, address code_buffer) {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 assert(Patching_lock->is_locked() ||
a61af66fc99e Initial load
duke
parents:
diff changeset
86 SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
a61af66fc99e Initial load
duke
parents:
diff changeset
87 assert (instr_addr != NULL, "illegal address for code patching");
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 NativeCall* n_call = nativeCall_at (instr_addr); // checking that it is a call
a61af66fc99e Initial load
duke
parents:
diff changeset
90 if (os::is_MP()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
91 guarantee((intptr_t)instr_addr % BytesPerWord == 0, "must be aligned");
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94 // First patch dummy jmp in place
a61af66fc99e Initial load
duke
parents:
diff changeset
95 unsigned char patch[4];
a61af66fc99e Initial load
duke
parents:
diff changeset
96 assert(sizeof(patch)==sizeof(jint), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
97 patch[0] = 0xEB; // jmp rel8
a61af66fc99e Initial load
duke
parents:
diff changeset
98 patch[1] = 0xFE; // jmp to self
a61af66fc99e Initial load
duke
parents:
diff changeset
99 patch[2] = 0xEB;
a61af66fc99e Initial load
duke
parents:
diff changeset
100 patch[3] = 0xFE;
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 // First patch dummy jmp in place
a61af66fc99e Initial load
duke
parents:
diff changeset
103 *(jint*)instr_addr = *(jint *)patch;
a61af66fc99e Initial load
duke
parents:
diff changeset
104
a61af66fc99e Initial load
duke
parents:
diff changeset
105 // Invalidate. Opteron requires a flush after every write.
a61af66fc99e Initial load
duke
parents:
diff changeset
106 n_call->wrote(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
107
a61af66fc99e Initial load
duke
parents:
diff changeset
108 // Patch 4th byte
a61af66fc99e Initial load
duke
parents:
diff changeset
109 instr_addr[4] = code_buffer[4];
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 n_call->wrote(4);
a61af66fc99e Initial load
duke
parents:
diff changeset
112
a61af66fc99e Initial load
duke
parents:
diff changeset
113 // Patch bytes 0-3
a61af66fc99e Initial load
duke
parents:
diff changeset
114 *(jint*)instr_addr = *(jint *)code_buffer;
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 n_call->wrote(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
119 // verify patching
a61af66fc99e Initial load
duke
parents:
diff changeset
120 for ( int i = 0; i < instruction_size; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 address ptr = (address)((intptr_t)code_buffer + i);
a61af66fc99e Initial load
duke
parents:
diff changeset
122 int a_byte = (*ptr) & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
123 assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
a61af66fc99e Initial load
duke
parents:
diff changeset
124 }
a61af66fc99e Initial load
duke
parents:
diff changeset
125 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
126
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 // Similar to replace_mt_safe, but just changes the destination. The
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // important thing is that free-running threads are able to execute this
a61af66fc99e Initial load
duke
parents:
diff changeset
132 // call instruction at all times. If the displacement field is aligned
a61af66fc99e Initial load
duke
parents:
diff changeset
133 // we can simply rely on atomicity of 32-bit writes to make sure other threads
a61af66fc99e Initial load
duke
parents:
diff changeset
134 // will see no intermediate states. Otherwise, the first two bytes of the
a61af66fc99e Initial load
duke
parents:
diff changeset
135 // call are guaranteed to be aligned, and can be atomically patched to a
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // self-loop to guard the instruction while we change the other bytes.
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // We cannot rely on locks here, since the free-running threads must run at
a61af66fc99e Initial load
duke
parents:
diff changeset
139 // full speed.
a61af66fc99e Initial load
duke
parents:
diff changeset
140 //
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // Used in the runtime linkage of calls; see class CompiledIC.
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // (Cf. 4506997 and 4479829, where threads witnessed garbage displacements.)
a61af66fc99e Initial load
duke
parents:
diff changeset
143 void NativeCall::set_destination_mt_safe(address dest) {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 debug_only(verify());
a61af66fc99e Initial load
duke
parents:
diff changeset
145 // Make sure patching code is locked. No two threads can patch at the same
a61af66fc99e Initial load
duke
parents:
diff changeset
146 // time but one may be executing this code.
a61af66fc99e Initial load
duke
parents:
diff changeset
147 assert(Patching_lock->is_locked() ||
a61af66fc99e Initial load
duke
parents:
diff changeset
148 SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
a61af66fc99e Initial load
duke
parents:
diff changeset
149 // Both C1 and C2 should now be generating code which aligns the patched address
a61af66fc99e Initial load
duke
parents:
diff changeset
150 // to be within a single cache line except that C1 does not do the alignment on
a61af66fc99e Initial load
duke
parents:
diff changeset
151 // uniprocessor systems.
a61af66fc99e Initial load
duke
parents:
diff changeset
152 bool is_aligned = ((uintptr_t)displacement_address() + 0) / cache_line_size ==
a61af66fc99e Initial load
duke
parents:
diff changeset
153 ((uintptr_t)displacement_address() + 3) / cache_line_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155 guarantee(!os::is_MP() || is_aligned, "destination must be aligned");
a61af66fc99e Initial load
duke
parents:
diff changeset
156
a61af66fc99e Initial load
duke
parents:
diff changeset
157 if (is_aligned) {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 // Simple case: The destination lies within a single cache line.
a61af66fc99e Initial load
duke
parents:
diff changeset
159 set_destination(dest);
a61af66fc99e Initial load
duke
parents:
diff changeset
160 } else if ((uintptr_t)instruction_address() / cache_line_size ==
a61af66fc99e Initial load
duke
parents:
diff changeset
161 ((uintptr_t)instruction_address()+1) / cache_line_size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
162 // Tricky case: The instruction prefix lies within a single cache line.
a61af66fc99e Initial load
duke
parents:
diff changeset
163 intptr_t disp = dest - return_address();
a61af66fc99e Initial load
duke
parents:
diff changeset
164 #ifdef AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
165 guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset");
a61af66fc99e Initial load
duke
parents:
diff changeset
166 #endif // AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 int call_opcode = instruction_address()[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
169
a61af66fc99e Initial load
duke
parents:
diff changeset
170 // First patch dummy jump in place:
a61af66fc99e Initial load
duke
parents:
diff changeset
171 {
a61af66fc99e Initial load
duke
parents:
diff changeset
172 u_char patch_jump[2];
a61af66fc99e Initial load
duke
parents:
diff changeset
173 patch_jump[0] = 0xEB; // jmp rel8
a61af66fc99e Initial load
duke
parents:
diff changeset
174 patch_jump[1] = 0xFE; // jmp to self
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 assert(sizeof(patch_jump)==sizeof(short), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
177 *(short*)instruction_address() = *(short*)patch_jump;
a61af66fc99e Initial load
duke
parents:
diff changeset
178 }
a61af66fc99e Initial load
duke
parents:
diff changeset
179 // Invalidate. Opteron requires a flush after every write.
a61af66fc99e Initial load
duke
parents:
diff changeset
180 wrote(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
181
a61af66fc99e Initial load
duke
parents:
diff changeset
182 // (Note: We assume any reader which has already started to read
a61af66fc99e Initial load
duke
parents:
diff changeset
183 // the unpatched call will completely read the whole unpatched call
a61af66fc99e Initial load
duke
parents:
diff changeset
184 // without seeing the next writes we are about to make.)
a61af66fc99e Initial load
duke
parents:
diff changeset
185
a61af66fc99e Initial load
duke
parents:
diff changeset
186 // Next, patch the last three bytes:
a61af66fc99e Initial load
duke
parents:
diff changeset
187 u_char patch_disp[5];
a61af66fc99e Initial load
duke
parents:
diff changeset
188 patch_disp[0] = call_opcode;
a61af66fc99e Initial load
duke
parents:
diff changeset
189 *(int32_t*)&patch_disp[1] = (int32_t)disp;
a61af66fc99e Initial load
duke
parents:
diff changeset
190 assert(sizeof(patch_disp)==instruction_size, "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
191 for (int i = sizeof(short); i < instruction_size; i++)
a61af66fc99e Initial load
duke
parents:
diff changeset
192 instruction_address()[i] = patch_disp[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
193
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // Invalidate. Opteron requires a flush after every write.
a61af66fc99e Initial load
duke
parents:
diff changeset
195 wrote(sizeof(short));
a61af66fc99e Initial load
duke
parents:
diff changeset
196
a61af66fc99e Initial load
duke
parents:
diff changeset
197 // (Note: We assume that any reader which reads the opcode we are
a61af66fc99e Initial load
duke
parents:
diff changeset
198 // about to repatch will also read the writes we just made.)
a61af66fc99e Initial load
duke
parents:
diff changeset
199
a61af66fc99e Initial load
duke
parents:
diff changeset
200 // Finally, overwrite the jump:
a61af66fc99e Initial load
duke
parents:
diff changeset
201 *(short*)instruction_address() = *(short*)patch_disp;
a61af66fc99e Initial load
duke
parents:
diff changeset
202 // Invalidate. Opteron requires a flush after every write.
a61af66fc99e Initial load
duke
parents:
diff changeset
203 wrote(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
204
a61af66fc99e Initial load
duke
parents:
diff changeset
205 debug_only(verify());
a61af66fc99e Initial load
duke
parents:
diff changeset
206 guarantee(destination() == dest, "patch succeeded");
a61af66fc99e Initial load
duke
parents:
diff changeset
207 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 // Impossible: One or the other must be atomically writable.
a61af66fc99e Initial load
duke
parents:
diff changeset
209 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
210 }
a61af66fc99e Initial load
duke
parents:
diff changeset
211 }
a61af66fc99e Initial load
duke
parents:
diff changeset
212
a61af66fc99e Initial load
duke
parents:
diff changeset
213
a61af66fc99e Initial load
duke
parents:
diff changeset
214 void NativeMovConstReg::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
215 #ifdef AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
216 // make sure code pattern is actually a mov reg64, imm64 instruction
a61af66fc99e Initial load
duke
parents:
diff changeset
217 if ((ubyte_at(0) != Assembler::REX_W && ubyte_at(0) != Assembler::REX_WB) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
218 (ubyte_at(1) & (0xff ^ register_mask)) != 0xB8) {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 print();
a61af66fc99e Initial load
duke
parents:
diff changeset
220 fatal("not a REX.W[B] mov reg64, imm64");
a61af66fc99e Initial load
duke
parents:
diff changeset
221 }
a61af66fc99e Initial load
duke
parents:
diff changeset
222 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
223 // make sure code pattern is actually a mov reg, imm32 instruction
a61af66fc99e Initial load
duke
parents:
diff changeset
224 u_char test_byte = *(u_char*)instruction_address();
a61af66fc99e Initial load
duke
parents:
diff changeset
225 u_char test_byte_2 = test_byte & ( 0xff ^ register_mask);
a61af66fc99e Initial load
duke
parents:
diff changeset
226 if (test_byte_2 != instruction_code) fatal("not a mov reg, imm32");
a61af66fc99e Initial load
duke
parents:
diff changeset
227 #endif // AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
228 }
a61af66fc99e Initial load
duke
parents:
diff changeset
229
a61af66fc99e Initial load
duke
parents:
diff changeset
230
a61af66fc99e Initial load
duke
parents:
diff changeset
231 void NativeMovConstReg::print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
232 tty->print_cr(PTR_FORMAT ": mov reg, " INTPTR_FORMAT,
a61af66fc99e Initial load
duke
parents:
diff changeset
233 instruction_address(), data());
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235
a61af66fc99e Initial load
duke
parents:
diff changeset
236 //-------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
237
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
238 int NativeMovRegMem::instruction_start() const {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
239 int off = 0;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
240 u_char instr_0 = ubyte_at(off);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
241
4759
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
242 // See comment in Assembler::locate_operand() about VEX prefixes.
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
243 if (instr_0 == instruction_VEX_prefix_2bytes) {
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
244 assert((UseAVX > 0), "shouldn't have VEX prefix");
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
245 NOT_LP64(assert((0xC0 & ubyte_at(1)) == 0xC0, "shouldn't have LDS and LES instructions"));
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
246 return 2;
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
247 }
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
248 if (instr_0 == instruction_VEX_prefix_3bytes) {
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
249 assert((UseAVX > 0), "shouldn't have VEX prefix");
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
250 NOT_LP64(assert((0xC0 & ubyte_at(1)) == 0xC0, "shouldn't have LDS and LES instructions"));
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
251 return 3;
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
252 }
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
253
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
254 // First check to see if we have a (prefixed or not) xor
4759
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
255 if (instr_0 >= instruction_prefix_wide_lo && // 0x40
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
256 instr_0 <= instruction_prefix_wide_hi) { // 0x4f
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
257 off++;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
258 instr_0 = ubyte_at(off);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
259 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
260
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
261 if (instr_0 == instruction_code_xor) {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
262 off += 2;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
263 instr_0 = ubyte_at(off);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
264 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
265
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
266 // Now look for the real instruction and the many prefix/size specifiers.
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
267
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
268 if (instr_0 == instruction_operandsize_prefix ) { // 0x66
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
269 off++; // Not SSE instructions
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
270 instr_0 = ubyte_at(off);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
271 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
272
4759
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
273 if ( instr_0 == instruction_code_xmm_ss_prefix || // 0xf3
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
274 instr_0 == instruction_code_xmm_sd_prefix) { // 0xf2
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
275 off++;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
276 instr_0 = ubyte_at(off);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
277 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
278
4759
127b3692c168 7116452: Add support for AVX instructions
kvn
parents: 1972
diff changeset
279 if ( instr_0 >= instruction_prefix_wide_lo && // 0x40
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
280 instr_0 <= instruction_prefix_wide_hi) { // 0x4f
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
281 off++;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
282 instr_0 = ubyte_at(off);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
283 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
284
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
285
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
286 if (instr_0 == instruction_extended_prefix ) { // 0x0f
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
287 off++;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
288 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
289
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
290 return off;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
291 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
292
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
293 address NativeMovRegMem::instruction_address() const {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
294 return addr_at(instruction_start());
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
295 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
296
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
297 address NativeMovRegMem::next_instruction_address() const {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
298 address ret = instruction_address() + instruction_size;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
299 u_char instr_0 = *(u_char*) instruction_address();
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
300 switch (instr_0) {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
301 case instruction_operandsize_prefix:
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
302
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
303 fatal("should have skipped instruction_operandsize_prefix");
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
304 break;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
305
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
306 case instruction_extended_prefix:
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
307 fatal("should have skipped instruction_extended_prefix");
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
308 break;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
309
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
310 case instruction_code_mem2reg_movslq: // 0x63
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
311 case instruction_code_mem2reg_movzxb: // 0xB6
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
312 case instruction_code_mem2reg_movsxb: // 0xBE
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
313 case instruction_code_mem2reg_movzxw: // 0xB7
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
314 case instruction_code_mem2reg_movsxw: // 0xBF
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
315 case instruction_code_reg2mem: // 0x89 (q/l)
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
316 case instruction_code_mem2reg: // 0x8B (q/l)
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
317 case instruction_code_reg2memb: // 0x88
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
318 case instruction_code_mem2regb: // 0x8a
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
319
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
320 case instruction_code_float_s: // 0xd9 fld_s a
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
321 case instruction_code_float_d: // 0xdd fld_d a
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
322
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
323 case instruction_code_xmm_load: // 0x10
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
324 case instruction_code_xmm_store: // 0x11
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
325 case instruction_code_xmm_lpd: // 0x12
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
326 {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
327 // If there is an SIB then instruction is longer than expected
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
328 u_char mod_rm = *(u_char*)(instruction_address() + 1);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
329 if ((mod_rm & 7) == 0x4) {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
330 ret++;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
331 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
332 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
333 case instruction_code_xor:
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
334 fatal("should have skipped xor lead in");
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
335 break;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
336
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
337 default:
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
338 fatal("not a NativeMovRegMem");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
339 }
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
340 return ret;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
341
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
342 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
343
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
344 int NativeMovRegMem::offset() const{
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
345 int off = data_offset + instruction_start();
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
346 u_char mod_rm = *(u_char*)(instruction_address() + 1);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
347 // nnnn(r12|rsp) isn't coded as simple mod/rm since that is
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
348 // the encoding to use an SIB byte. Which will have the nnnn
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
349 // field off by one byte
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
350 if ((mod_rm & 7) == 0x4) {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
351 off++;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
352 }
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
353 return int_at(off);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
354 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
355
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
356 void NativeMovRegMem::set_offset(int x) {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
357 int off = data_offset + instruction_start();
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
358 u_char mod_rm = *(u_char*)(instruction_address() + 1);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
359 // nnnn(r12|rsp) isn't coded as simple mod/rm since that is
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
360 // the encoding to use an SIB byte. Which will have the nnnn
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
361 // field off by one byte
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
362 if ((mod_rm & 7) == 0x4) {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
363 off++;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
364 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
365 set_int_at(off, x);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
366 }
a61af66fc99e Initial load
duke
parents:
diff changeset
367
a61af66fc99e Initial load
duke
parents:
diff changeset
368 void NativeMovRegMem::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
369 // make sure code pattern is actually a mov [reg+offset], reg instruction
a61af66fc99e Initial load
duke
parents:
diff changeset
370 u_char test_byte = *(u_char*)instruction_address();
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
371 switch (test_byte) {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
372 case instruction_code_reg2memb: // 0x88 movb a, r
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
373 case instruction_code_reg2mem: // 0x89 movl a, r (can be movq in 64bit)
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
374 case instruction_code_mem2regb: // 0x8a movb r, a
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
375 case instruction_code_mem2reg: // 0x8b movl r, a (can be movq in 64bit)
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
376 break;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
377
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
378 case instruction_code_mem2reg_movslq: // 0x63 movsql r, a
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
379 case instruction_code_mem2reg_movzxb: // 0xb6 movzbl r, a (movzxb)
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
380 case instruction_code_mem2reg_movzxw: // 0xb7 movzwl r, a (movzxw)
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
381 case instruction_code_mem2reg_movsxb: // 0xbe movsbl r, a (movsxb)
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
382 case instruction_code_mem2reg_movsxw: // 0xbf movswl r, a (movsxw)
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
383 break;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
384
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
385 case instruction_code_float_s: // 0xd9 fld_s a
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
386 case instruction_code_float_d: // 0xdd fld_d a
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
387 case instruction_code_xmm_load: // 0x10 movsd xmm, a
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
388 case instruction_code_xmm_store: // 0x11 movsd a, xmm
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
389 case instruction_code_xmm_lpd: // 0x12 movlpd xmm, a
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
390 break;
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
391
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
392 default:
0
a61af66fc99e Initial load
duke
parents:
diff changeset
393 fatal ("not a mov [reg+offs], reg instruction");
a61af66fc99e Initial load
duke
parents:
diff changeset
394 }
a61af66fc99e Initial load
duke
parents:
diff changeset
395 }
a61af66fc99e Initial load
duke
parents:
diff changeset
396
a61af66fc99e Initial load
duke
parents:
diff changeset
397
a61af66fc99e Initial load
duke
parents:
diff changeset
398 void NativeMovRegMem::print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
399 tty->print_cr("0x%x: mov reg, [reg + %x]", instruction_address(), offset());
a61af66fc99e Initial load
duke
parents:
diff changeset
400 }
a61af66fc99e Initial load
duke
parents:
diff changeset
401
a61af66fc99e Initial load
duke
parents:
diff changeset
402 //-------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
403
a61af66fc99e Initial load
duke
parents:
diff changeset
404 void NativeLoadAddress::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
405 // make sure code pattern is actually a mov [reg+offset], reg instruction
a61af66fc99e Initial load
duke
parents:
diff changeset
406 u_char test_byte = *(u_char*)instruction_address();
304
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
407 #ifdef _LP64
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
408 if ( (test_byte == instruction_prefix_wide ||
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
409 test_byte == instruction_prefix_wide_extended) ) {
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
410 test_byte = *(u_char*)(instruction_address() + 1);
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
411 }
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
412 #endif // _LP64
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
413 if ( ! ((test_byte == lea_instruction_code)
dc7f315e41f7 5108146: Merge i486 and amd64 cpu directories
never
parents: 196
diff changeset
414 LP64_ONLY(|| (test_byte == mov64_instruction_code) ))) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
415 fatal ("not a lea reg, [reg+offs] instruction");
a61af66fc99e Initial load
duke
parents:
diff changeset
416 }
a61af66fc99e Initial load
duke
parents:
diff changeset
417 }
a61af66fc99e Initial load
duke
parents:
diff changeset
418
a61af66fc99e Initial load
duke
parents:
diff changeset
419
a61af66fc99e Initial load
duke
parents:
diff changeset
420 void NativeLoadAddress::print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
421 tty->print_cr("0x%x: lea [reg + %x], reg", instruction_address(), offset());
a61af66fc99e Initial load
duke
parents:
diff changeset
422 }
a61af66fc99e Initial load
duke
parents:
diff changeset
423
a61af66fc99e Initial load
duke
parents:
diff changeset
424 //--------------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
425
a61af66fc99e Initial load
duke
parents:
diff changeset
426 void NativeJump::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
427 if (*(u_char*)instruction_address() != instruction_code) {
a61af66fc99e Initial load
duke
parents:
diff changeset
428 fatal("not a jump instruction");
a61af66fc99e Initial load
duke
parents:
diff changeset
429 }
a61af66fc99e Initial load
duke
parents:
diff changeset
430 }
a61af66fc99e Initial load
duke
parents:
diff changeset
431
a61af66fc99e Initial load
duke
parents:
diff changeset
432
a61af66fc99e Initial load
duke
parents:
diff changeset
433 void NativeJump::insert(address code_pos, address entry) {
a61af66fc99e Initial load
duke
parents:
diff changeset
434 intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
a61af66fc99e Initial load
duke
parents:
diff changeset
435 #ifdef AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
436 guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
a61af66fc99e Initial load
duke
parents:
diff changeset
437 #endif // AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
438
a61af66fc99e Initial load
duke
parents:
diff changeset
439 *code_pos = instruction_code;
a61af66fc99e Initial load
duke
parents:
diff changeset
440 *((int32_t*)(code_pos + 1)) = (int32_t)disp;
a61af66fc99e Initial load
duke
parents:
diff changeset
441
a61af66fc99e Initial load
duke
parents:
diff changeset
442 ICache::invalidate_range(code_pos, instruction_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
443 }
a61af66fc99e Initial load
duke
parents:
diff changeset
444
a61af66fc99e Initial load
duke
parents:
diff changeset
445 void NativeJump::check_verified_entry_alignment(address entry, address verified_entry) {
a61af66fc99e Initial load
duke
parents:
diff changeset
446 // Patching to not_entrant can happen while activations of the method are
a61af66fc99e Initial load
duke
parents:
diff changeset
447 // in use. The patching in that instance must happen only when certain
a61af66fc99e Initial load
duke
parents:
diff changeset
448 // alignment restrictions are true. These guarantees check those
a61af66fc99e Initial load
duke
parents:
diff changeset
449 // conditions.
a61af66fc99e Initial load
duke
parents:
diff changeset
450 #ifdef AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
451 const int linesize = 64;
a61af66fc99e Initial load
duke
parents:
diff changeset
452 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
453 const int linesize = 32;
a61af66fc99e Initial load
duke
parents:
diff changeset
454 #endif // AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
455
a61af66fc99e Initial load
duke
parents:
diff changeset
456 // Must be wordSize aligned
a61af66fc99e Initial load
duke
parents:
diff changeset
457 guarantee(((uintptr_t) verified_entry & (wordSize -1)) == 0,
a61af66fc99e Initial load
duke
parents:
diff changeset
458 "illegal address for code patching 2");
a61af66fc99e Initial load
duke
parents:
diff changeset
459 // First 5 bytes must be within the same cache line - 4827828
a61af66fc99e Initial load
duke
parents:
diff changeset
460 guarantee((uintptr_t) verified_entry / linesize ==
a61af66fc99e Initial load
duke
parents:
diff changeset
461 ((uintptr_t) verified_entry + 4) / linesize,
a61af66fc99e Initial load
duke
parents:
diff changeset
462 "illegal address for code patching 3");
a61af66fc99e Initial load
duke
parents:
diff changeset
463 }
a61af66fc99e Initial load
duke
parents:
diff changeset
464
a61af66fc99e Initial load
duke
parents:
diff changeset
465
a61af66fc99e Initial load
duke
parents:
diff changeset
466 // MT safe inserting of a jump over an unknown instruction sequence (used by nmethod::makeZombie)
a61af66fc99e Initial load
duke
parents:
diff changeset
467 // The problem: jmp <dest> is a 5-byte instruction. Atomical write can be only with 4 bytes.
a61af66fc99e Initial load
duke
parents:
diff changeset
468 // First patches the first word atomically to be a jump to itself.
a61af66fc99e Initial load
duke
parents:
diff changeset
469 // Then patches the last byte and then atomically patches the first word (4-bytes),
a61af66fc99e Initial load
duke
parents:
diff changeset
470 // thus inserting the desired jump
a61af66fc99e Initial load
duke
parents:
diff changeset
471 // This code is mt-safe with the following conditions: entry point is 4 byte aligned,
a61af66fc99e Initial load
duke
parents:
diff changeset
472 // entry point is in same cache line as unverified entry point, and the instruction being
a61af66fc99e Initial load
duke
parents:
diff changeset
473 // patched is >= 5 byte (size of patch).
a61af66fc99e Initial load
duke
parents:
diff changeset
474 //
a61af66fc99e Initial load
duke
parents:
diff changeset
475 // In C2 the 5+ byte sized instruction is enforced by code in MachPrologNode::emit.
a61af66fc99e Initial load
duke
parents:
diff changeset
476 // In C1 the restriction is enforced by CodeEmitter::method_entry
21559
be896a1983c0 recast all Graal native code as JVMCI code (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 18041
diff changeset
477 // In JVMCI, the restriction is enforced by HotSpotFrameContext.enter(...)
0
a61af66fc99e Initial load
duke
parents:
diff changeset
478 //
a61af66fc99e Initial load
duke
parents:
diff changeset
479 void NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) {
a61af66fc99e Initial load
duke
parents:
diff changeset
480 // complete jump instruction (to be inserted) is in code_buffer;
a61af66fc99e Initial load
duke
parents:
diff changeset
481 unsigned char code_buffer[5];
a61af66fc99e Initial load
duke
parents:
diff changeset
482 code_buffer[0] = instruction_code;
a61af66fc99e Initial load
duke
parents:
diff changeset
483 intptr_t disp = (intptr_t)dest - ((intptr_t)verified_entry + 1 + 4);
a61af66fc99e Initial load
duke
parents:
diff changeset
484 #ifdef AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
485 guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
a61af66fc99e Initial load
duke
parents:
diff changeset
486 #endif // AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
487 *(int32_t*)(code_buffer + 1) = (int32_t)disp;
a61af66fc99e Initial load
duke
parents:
diff changeset
488
a61af66fc99e Initial load
duke
parents:
diff changeset
489 check_verified_entry_alignment(entry, verified_entry);
a61af66fc99e Initial load
duke
parents:
diff changeset
490
a61af66fc99e Initial load
duke
parents:
diff changeset
491 // Can't call nativeJump_at() because it's asserts jump exists
a61af66fc99e Initial load
duke
parents:
diff changeset
492 NativeJump* n_jump = (NativeJump*) verified_entry;
a61af66fc99e Initial load
duke
parents:
diff changeset
493
a61af66fc99e Initial load
duke
parents:
diff changeset
494 //First patch dummy jmp in place
a61af66fc99e Initial load
duke
parents:
diff changeset
495
a61af66fc99e Initial load
duke
parents:
diff changeset
496 unsigned char patch[4];
a61af66fc99e Initial load
duke
parents:
diff changeset
497 assert(sizeof(patch)==sizeof(int32_t), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
498 patch[0] = 0xEB; // jmp rel8
a61af66fc99e Initial load
duke
parents:
diff changeset
499 patch[1] = 0xFE; // jmp to self
a61af66fc99e Initial load
duke
parents:
diff changeset
500 patch[2] = 0xEB;
a61af66fc99e Initial load
duke
parents:
diff changeset
501 patch[3] = 0xFE;
a61af66fc99e Initial load
duke
parents:
diff changeset
502
a61af66fc99e Initial load
duke
parents:
diff changeset
503 // First patch dummy jmp in place
a61af66fc99e Initial load
duke
parents:
diff changeset
504 *(int32_t*)verified_entry = *(int32_t *)patch;
a61af66fc99e Initial load
duke
parents:
diff changeset
505
a61af66fc99e Initial load
duke
parents:
diff changeset
506 n_jump->wrote(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
507
a61af66fc99e Initial load
duke
parents:
diff changeset
508 // Patch 5th byte (from jump instruction)
a61af66fc99e Initial load
duke
parents:
diff changeset
509 verified_entry[4] = code_buffer[4];
a61af66fc99e Initial load
duke
parents:
diff changeset
510
a61af66fc99e Initial load
duke
parents:
diff changeset
511 n_jump->wrote(4);
a61af66fc99e Initial load
duke
parents:
diff changeset
512
a61af66fc99e Initial load
duke
parents:
diff changeset
513 // Patch bytes 0-3 (from jump instruction)
a61af66fc99e Initial load
duke
parents:
diff changeset
514 *(int32_t*)verified_entry = *(int32_t *)code_buffer;
a61af66fc99e Initial load
duke
parents:
diff changeset
515 // Invalidate. Opteron requires a flush after every write.
a61af66fc99e Initial load
duke
parents:
diff changeset
516 n_jump->wrote(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
517
a61af66fc99e Initial load
duke
parents:
diff changeset
518 }
a61af66fc99e Initial load
duke
parents:
diff changeset
519
a61af66fc99e Initial load
duke
parents:
diff changeset
520 void NativePopReg::insert(address code_pos, Register reg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
521 assert(reg->encoding() < 8, "no space for REX");
a61af66fc99e Initial load
duke
parents:
diff changeset
522 assert(NativePopReg::instruction_size == sizeof(char), "right address unit for update");
a61af66fc99e Initial load
duke
parents:
diff changeset
523 *code_pos = (u_char)(instruction_code | reg->encoding());
a61af66fc99e Initial load
duke
parents:
diff changeset
524 ICache::invalidate_range(code_pos, instruction_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
525 }
a61af66fc99e Initial load
duke
parents:
diff changeset
526
a61af66fc99e Initial load
duke
parents:
diff changeset
527
a61af66fc99e Initial load
duke
parents:
diff changeset
528 void NativeIllegalInstruction::insert(address code_pos) {
a61af66fc99e Initial load
duke
parents:
diff changeset
529 assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update");
a61af66fc99e Initial load
duke
parents:
diff changeset
530 *(short *)code_pos = instruction_code;
a61af66fc99e Initial load
duke
parents:
diff changeset
531 ICache::invalidate_range(code_pos, instruction_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
532 }
a61af66fc99e Initial load
duke
parents:
diff changeset
533
a61af66fc99e Initial load
duke
parents:
diff changeset
534 void NativeGeneralJump::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
535 assert(((NativeInstruction *)this)->is_jump() ||
a61af66fc99e Initial load
duke
parents:
diff changeset
536 ((NativeInstruction *)this)->is_cond_jump(), "not a general jump instruction");
a61af66fc99e Initial load
duke
parents:
diff changeset
537 }
a61af66fc99e Initial load
duke
parents:
diff changeset
538
a61af66fc99e Initial load
duke
parents:
diff changeset
539
a61af66fc99e Initial load
duke
parents:
diff changeset
540 void NativeGeneralJump::insert_unconditional(address code_pos, address entry) {
a61af66fc99e Initial load
duke
parents:
diff changeset
541 intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
a61af66fc99e Initial load
duke
parents:
diff changeset
542 #ifdef AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
543 guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
a61af66fc99e Initial load
duke
parents:
diff changeset
544 #endif // AMD64
a61af66fc99e Initial load
duke
parents:
diff changeset
545
a61af66fc99e Initial load
duke
parents:
diff changeset
546 *code_pos = unconditional_long_jump;
a61af66fc99e Initial load
duke
parents:
diff changeset
547 *((int32_t *)(code_pos+1)) = (int32_t) disp;
a61af66fc99e Initial load
duke
parents:
diff changeset
548 ICache::invalidate_range(code_pos, instruction_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
549 }
a61af66fc99e Initial load
duke
parents:
diff changeset
550
a61af66fc99e Initial load
duke
parents:
diff changeset
551
a61af66fc99e Initial load
duke
parents:
diff changeset
552 // MT-safe patching of a long jump instruction.
a61af66fc99e Initial load
duke
parents:
diff changeset
553 // First patches first word of instruction to two jmp's that jmps to them
a61af66fc99e Initial load
duke
parents:
diff changeset
554 // selfs (spinlock). Then patches the last byte, and then atomicly replaces
a61af66fc99e Initial load
duke
parents:
diff changeset
555 // the jmp's with the first 4 byte of the new instruction.
a61af66fc99e Initial load
duke
parents:
diff changeset
556 void NativeGeneralJump::replace_mt_safe(address instr_addr, address code_buffer) {
a61af66fc99e Initial load
duke
parents:
diff changeset
557 assert (instr_addr != NULL, "illegal address for code patching (4)");
a61af66fc99e Initial load
duke
parents:
diff changeset
558 NativeGeneralJump* n_jump = nativeGeneralJump_at (instr_addr); // checking that it is a jump
a61af66fc99e Initial load
duke
parents:
diff changeset
559
a61af66fc99e Initial load
duke
parents:
diff changeset
560 // Temporary code
a61af66fc99e Initial load
duke
parents:
diff changeset
561 unsigned char patch[4];
a61af66fc99e Initial load
duke
parents:
diff changeset
562 assert(sizeof(patch)==sizeof(int32_t), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
563 patch[0] = 0xEB; // jmp rel8
a61af66fc99e Initial load
duke
parents:
diff changeset
564 patch[1] = 0xFE; // jmp to self
a61af66fc99e Initial load
duke
parents:
diff changeset
565 patch[2] = 0xEB;
a61af66fc99e Initial load
duke
parents:
diff changeset
566 patch[3] = 0xFE;
a61af66fc99e Initial load
duke
parents:
diff changeset
567
a61af66fc99e Initial load
duke
parents:
diff changeset
568 // First patch dummy jmp in place
a61af66fc99e Initial load
duke
parents:
diff changeset
569 *(int32_t*)instr_addr = *(int32_t *)patch;
a61af66fc99e Initial load
duke
parents:
diff changeset
570 n_jump->wrote(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
571
a61af66fc99e Initial load
duke
parents:
diff changeset
572 // Patch 4th byte
a61af66fc99e Initial load
duke
parents:
diff changeset
573 instr_addr[4] = code_buffer[4];
a61af66fc99e Initial load
duke
parents:
diff changeset
574
a61af66fc99e Initial load
duke
parents:
diff changeset
575 n_jump->wrote(4);
a61af66fc99e Initial load
duke
parents:
diff changeset
576
a61af66fc99e Initial load
duke
parents:
diff changeset
577 // Patch bytes 0-3
a61af66fc99e Initial load
duke
parents:
diff changeset
578 *(jint*)instr_addr = *(jint *)code_buffer;
a61af66fc99e Initial load
duke
parents:
diff changeset
579
a61af66fc99e Initial load
duke
parents:
diff changeset
580 n_jump->wrote(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
581
a61af66fc99e Initial load
duke
parents:
diff changeset
582 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
583 // verify patching
a61af66fc99e Initial load
duke
parents:
diff changeset
584 for ( int i = 0; i < instruction_size; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
585 address ptr = (address)((intptr_t)code_buffer + i);
a61af66fc99e Initial load
duke
parents:
diff changeset
586 int a_byte = (*ptr) & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
587 assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
a61af66fc99e Initial load
duke
parents:
diff changeset
588 }
a61af66fc99e Initial load
duke
parents:
diff changeset
589 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
590
a61af66fc99e Initial load
duke
parents:
diff changeset
591 }
a61af66fc99e Initial load
duke
parents:
diff changeset
592
a61af66fc99e Initial load
duke
parents:
diff changeset
593
a61af66fc99e Initial load
duke
parents:
diff changeset
594
a61af66fc99e Initial load
duke
parents:
diff changeset
595 address NativeGeneralJump::jump_destination() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
596 int op_code = ubyte_at(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
597 bool is_rel32off = (op_code == 0xE9 || op_code == 0x0F);
a61af66fc99e Initial load
duke
parents:
diff changeset
598 int offset = (op_code == 0x0F) ? 2 : 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
599 int length = offset + ((is_rel32off) ? 4 : 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
600
a61af66fc99e Initial load
duke
parents:
diff changeset
601 if (is_rel32off)
a61af66fc99e Initial load
duke
parents:
diff changeset
602 return addr_at(0) + length + int_at(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
603 else
a61af66fc99e Initial load
duke
parents:
diff changeset
604 return addr_at(0) + length + sbyte_at(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
605 }
116
018d5b58dd4f 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 0
diff changeset
606
018d5b58dd4f 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 0
diff changeset
607 bool NativeInstruction::is_dtrace_trap() {
018d5b58dd4f 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 0
diff changeset
608 return (*(int32_t*)this & 0xff) == 0xcc;
018d5b58dd4f 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 0
diff changeset
609 }