src/cpu/x86/vm/nativeInst_x86.cpp

Wed, 02 Jun 2010 22:45:42 -0700

author
jrose
date
Wed, 02 Jun 2010 22:45:42 -0700
changeset 1934
e9ff18c4ace7
parent 1907
c18cbe5936b8
child 2314
f95d63e2154a
permissions
-rw-r--r--

Merge

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

mercurial