src/cpu/x86/vm/nativeInst_x86.cpp

Mon, 13 Feb 2012 02:29:22 -0800

author
twisti
date
Mon, 13 Feb 2012 02:29:22 -0800
changeset 3566
45a1bf98f1bb
parent 3388
127b3692c168
child 4318
cd3d6a6b95d9
permissions
-rw-r--r--

7141329: Strange values of stack_size in -XX:+TraceMethodHandles output
Reviewed-by: kvn, never

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

mercurial