0N/A/*
1879N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "assembler_x86.inline.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "nativeInst_x86.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/handles.hpp"
1879N/A#include "runtime/sharedRuntime.hpp"
1879N/A#include "runtime/stubRoutines.hpp"
1879N/A#include "utilities/ostream.hpp"
1879N/A#ifdef COMPILER1
1879N/A#include "c1/c1_Runtime1.hpp"
1879N/A#endif
0N/A
0N/Avoid NativeInstruction::wrote(int offset) {
0N/A ICache::invalidate_word(addr_at(offset));
0N/A}
0N/A
0N/A
0N/Avoid NativeCall::verify() {
0N/A // Make sure code pattern is actually a call imm32 instruction.
0N/A int inst = ubyte_at(0);
0N/A if (inst != instruction_code) {
0N/A tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", instruction_address(),
0N/A inst);
0N/A fatal("not a call disp32");
0N/A }
0N/A}
0N/A
0N/Aaddress NativeCall::destination() const {
0N/A // Getting the destination of a call isn't safe because that call can
0N/A // be getting patched while you're calling this. There's only special
0N/A // places where this can be called but not automatically verifiable by
0N/A // checking which locks are held. The solution is true atomic patching
0N/A // on x86, nyi.
0N/A return return_address() + displacement();
0N/A}
0N/A
0N/Avoid NativeCall::print() {
0N/A tty->print_cr(PTR_FORMAT ": call " PTR_FORMAT,
0N/A instruction_address(), destination());
0N/A}
0N/A
0N/A// Inserts a native call instruction at a given pc
0N/Avoid NativeCall::insert(address code_pos, address entry) {
0N/A intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
0N/A#ifdef AMD64
0N/A guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset");
0N/A#endif // AMD64
0N/A *code_pos = instruction_code;
0N/A *((int32_t *)(code_pos+1)) = (int32_t) disp;
0N/A ICache::invalidate_range(code_pos, instruction_size);
0N/A}
0N/A
0N/A// MT-safe patching of a call instruction.
0N/A// First patches first word of instruction to two jmp's that jmps to them
0N/A// selfs (spinlock). Then patches the last byte, and then atomicly replaces
0N/A// the jmp's with the first 4 byte of the new instruction.
0N/Avoid NativeCall::replace_mt_safe(address instr_addr, address code_buffer) {
0N/A assert(Patching_lock->is_locked() ||
0N/A SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
0N/A assert (instr_addr != NULL, "illegal address for code patching");
0N/A
0N/A NativeCall* n_call = nativeCall_at (instr_addr); // checking that it is a call
0N/A if (os::is_MP()) {
0N/A guarantee((intptr_t)instr_addr % BytesPerWord == 0, "must be aligned");
0N/A }
0N/A
0N/A // First patch dummy jmp in place
0N/A unsigned char patch[4];
0N/A assert(sizeof(patch)==sizeof(jint), "sanity check");
0N/A patch[0] = 0xEB; // jmp rel8
0N/A patch[1] = 0xFE; // jmp to self
0N/A patch[2] = 0xEB;
0N/A patch[3] = 0xFE;
0N/A
0N/A // First patch dummy jmp in place
0N/A *(jint*)instr_addr = *(jint *)patch;
0N/A
0N/A // Invalidate. Opteron requires a flush after every write.
0N/A n_call->wrote(0);
0N/A
0N/A // Patch 4th byte
0N/A instr_addr[4] = code_buffer[4];
0N/A
0N/A n_call->wrote(4);
0N/A
0N/A // Patch bytes 0-3
0N/A *(jint*)instr_addr = *(jint *)code_buffer;
0N/A
0N/A n_call->wrote(0);
0N/A
0N/A#ifdef ASSERT
0N/A // verify patching
0N/A for ( int i = 0; i < instruction_size; i++) {
0N/A address ptr = (address)((intptr_t)code_buffer + i);
0N/A int a_byte = (*ptr) & 0xFF;
0N/A assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
0N/A }
0N/A#endif
0N/A
0N/A}
0N/A
0N/A
0N/A// Similar to replace_mt_safe, but just changes the destination. The
0N/A// important thing is that free-running threads are able to execute this
0N/A// call instruction at all times. If the displacement field is aligned
0N/A// we can simply rely on atomicity of 32-bit writes to make sure other threads
0N/A// will see no intermediate states. Otherwise, the first two bytes of the
0N/A// call are guaranteed to be aligned, and can be atomically patched to a
0N/A// self-loop to guard the instruction while we change the other bytes.
0N/A
0N/A// We cannot rely on locks here, since the free-running threads must run at
0N/A// full speed.
0N/A//
0N/A// Used in the runtime linkage of calls; see class CompiledIC.
0N/A// (Cf. 4506997 and 4479829, where threads witnessed garbage displacements.)
0N/Avoid NativeCall::set_destination_mt_safe(address dest) {
0N/A debug_only(verify());
0N/A // Make sure patching code is locked. No two threads can patch at the same
0N/A // time but one may be executing this code.
0N/A assert(Patching_lock->is_locked() ||
0N/A SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
0N/A // Both C1 and C2 should now be generating code which aligns the patched address
0N/A // to be within a single cache line except that C1 does not do the alignment on
0N/A // uniprocessor systems.
0N/A bool is_aligned = ((uintptr_t)displacement_address() + 0) / cache_line_size ==
0N/A ((uintptr_t)displacement_address() + 3) / cache_line_size;
0N/A
0N/A guarantee(!os::is_MP() || is_aligned, "destination must be aligned");
0N/A
0N/A if (is_aligned) {
0N/A // Simple case: The destination lies within a single cache line.
0N/A set_destination(dest);
0N/A } else if ((uintptr_t)instruction_address() / cache_line_size ==
0N/A ((uintptr_t)instruction_address()+1) / cache_line_size) {
0N/A // Tricky case: The instruction prefix lies within a single cache line.
0N/A intptr_t disp = dest - return_address();
0N/A#ifdef AMD64
0N/A guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset");
0N/A#endif // AMD64
0N/A
0N/A int call_opcode = instruction_address()[0];
0N/A
0N/A // First patch dummy jump in place:
0N/A {
0N/A u_char patch_jump[2];
0N/A patch_jump[0] = 0xEB; // jmp rel8
0N/A patch_jump[1] = 0xFE; // jmp to self
0N/A
0N/A assert(sizeof(patch_jump)==sizeof(short), "sanity check");
0N/A *(short*)instruction_address() = *(short*)patch_jump;
0N/A }
0N/A // Invalidate. Opteron requires a flush after every write.
0N/A wrote(0);
0N/A
0N/A // (Note: We assume any reader which has already started to read
0N/A // the unpatched call will completely read the whole unpatched call
0N/A // without seeing the next writes we are about to make.)
0N/A
0N/A // Next, patch the last three bytes:
0N/A u_char patch_disp[5];
0N/A patch_disp[0] = call_opcode;
0N/A *(int32_t*)&patch_disp[1] = (int32_t)disp;
0N/A assert(sizeof(patch_disp)==instruction_size, "sanity check");
0N/A for (int i = sizeof(short); i < instruction_size; i++)
0N/A instruction_address()[i] = patch_disp[i];
0N/A
0N/A // Invalidate. Opteron requires a flush after every write.
0N/A wrote(sizeof(short));
0N/A
0N/A // (Note: We assume that any reader which reads the opcode we are
0N/A // about to repatch will also read the writes we just made.)
0N/A
0N/A // Finally, overwrite the jump:
0N/A *(short*)instruction_address() = *(short*)patch_disp;
0N/A // Invalidate. Opteron requires a flush after every write.
0N/A wrote(0);
0N/A
0N/A debug_only(verify());
0N/A guarantee(destination() == dest, "patch succeeded");
0N/A } else {
0N/A // Impossible: One or the other must be atomically writable.
0N/A ShouldNotReachHere();
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid NativeMovConstReg::verify() {
0N/A#ifdef AMD64
0N/A // make sure code pattern is actually a mov reg64, imm64 instruction
0N/A if ((ubyte_at(0) != Assembler::REX_W && ubyte_at(0) != Assembler::REX_WB) ||
0N/A (ubyte_at(1) & (0xff ^ register_mask)) != 0xB8) {
0N/A print();
0N/A fatal("not a REX.W[B] mov reg64, imm64");
0N/A }
0N/A#else
0N/A // make sure code pattern is actually a mov reg, imm32 instruction
0N/A u_char test_byte = *(u_char*)instruction_address();
0N/A u_char test_byte_2 = test_byte & ( 0xff ^ register_mask);
0N/A if (test_byte_2 != instruction_code) fatal("not a mov reg, imm32");
0N/A#endif // AMD64
0N/A}
0N/A
0N/A
0N/Avoid NativeMovConstReg::print() {
0N/A tty->print_cr(PTR_FORMAT ": mov reg, " INTPTR_FORMAT,
0N/A instruction_address(), data());
0N/A}
0N/A
0N/A//-------------------------------------------------------------------
0N/A
304N/Aint NativeMovRegMem::instruction_start() const {
304N/A int off = 0;
304N/A u_char instr_0 = ubyte_at(off);
304N/A
3039N/A // See comment in Assembler::locate_operand() about VEX prefixes.
3039N/A if (instr_0 == instruction_VEX_prefix_2bytes) {
3039N/A assert((UseAVX > 0), "shouldn't have VEX prefix");
3039N/A NOT_LP64(assert((0xC0 & ubyte_at(1)) == 0xC0, "shouldn't have LDS and LES instructions"));
3039N/A return 2;
3039N/A }
3039N/A if (instr_0 == instruction_VEX_prefix_3bytes) {
3039N/A assert((UseAVX > 0), "shouldn't have VEX prefix");
3039N/A NOT_LP64(assert((0xC0 & ubyte_at(1)) == 0xC0, "shouldn't have LDS and LES instructions"));
3039N/A return 3;
3039N/A }
3039N/A
304N/A // First check to see if we have a (prefixed or not) xor
3039N/A if (instr_0 >= instruction_prefix_wide_lo && // 0x40
3039N/A instr_0 <= instruction_prefix_wide_hi) { // 0x4f
304N/A off++;
304N/A instr_0 = ubyte_at(off);
304N/A }
304N/A
304N/A if (instr_0 == instruction_code_xor) {
304N/A off += 2;
304N/A instr_0 = ubyte_at(off);
304N/A }
304N/A
304N/A // Now look for the real instruction and the many prefix/size specifiers.
304N/A
304N/A if (instr_0 == instruction_operandsize_prefix ) { // 0x66
304N/A off++; // Not SSE instructions
304N/A instr_0 = ubyte_at(off);
304N/A }
0N/A
3039N/A if ( instr_0 == instruction_code_xmm_ss_prefix || // 0xf3
304N/A instr_0 == instruction_code_xmm_sd_prefix) { // 0xf2
304N/A off++;
304N/A instr_0 = ubyte_at(off);
304N/A }
304N/A
3039N/A if ( instr_0 >= instruction_prefix_wide_lo && // 0x40
304N/A instr_0 <= instruction_prefix_wide_hi) { // 0x4f
304N/A off++;
304N/A instr_0 = ubyte_at(off);
304N/A }
304N/A
304N/A
304N/A if (instr_0 == instruction_extended_prefix ) { // 0x0f
304N/A off++;
304N/A }
304N/A
304N/A return off;
304N/A}
304N/A
304N/Aaddress NativeMovRegMem::instruction_address() const {
304N/A return addr_at(instruction_start());
304N/A}
304N/A
304N/Aaddress NativeMovRegMem::next_instruction_address() const {
304N/A address ret = instruction_address() + instruction_size;
304N/A u_char instr_0 = *(u_char*) instruction_address();
304N/A switch (instr_0) {
304N/A case instruction_operandsize_prefix:
304N/A
304N/A fatal("should have skipped instruction_operandsize_prefix");
304N/A break;
0N/A
304N/A case instruction_extended_prefix:
304N/A fatal("should have skipped instruction_extended_prefix");
304N/A break;
304N/A
304N/A case instruction_code_mem2reg_movslq: // 0x63
304N/A case instruction_code_mem2reg_movzxb: // 0xB6
304N/A case instruction_code_mem2reg_movsxb: // 0xBE
304N/A case instruction_code_mem2reg_movzxw: // 0xB7
304N/A case instruction_code_mem2reg_movsxw: // 0xBF
304N/A case instruction_code_reg2mem: // 0x89 (q/l)
304N/A case instruction_code_mem2reg: // 0x8B (q/l)
304N/A case instruction_code_reg2memb: // 0x88
304N/A case instruction_code_mem2regb: // 0x8a
304N/A
304N/A case instruction_code_float_s: // 0xd9 fld_s a
304N/A case instruction_code_float_d: // 0xdd fld_d a
304N/A
304N/A case instruction_code_xmm_load: // 0x10
304N/A case instruction_code_xmm_store: // 0x11
304N/A case instruction_code_xmm_lpd: // 0x12
304N/A {
304N/A // If there is an SIB then instruction is longer than expected
304N/A u_char mod_rm = *(u_char*)(instruction_address() + 1);
304N/A if ((mod_rm & 7) == 0x4) {
304N/A ret++;
304N/A }
304N/A }
304N/A case instruction_code_xor:
304N/A fatal("should have skipped xor lead in");
304N/A break;
304N/A
304N/A default:
304N/A fatal("not a NativeMovRegMem");
0N/A }
304N/A return ret;
304N/A
304N/A}
0N/A
304N/Aint NativeMovRegMem::offset() const{
304N/A int off = data_offset + instruction_start();
304N/A u_char mod_rm = *(u_char*)(instruction_address() + 1);
304N/A // nnnn(r12|rsp) isn't coded as simple mod/rm since that is
304N/A // the encoding to use an SIB byte. Which will have the nnnn
304N/A // field off by one byte
304N/A if ((mod_rm & 7) == 0x4) {
304N/A off++;
0N/A }
304N/A return int_at(off);
304N/A}
304N/A
304N/Avoid NativeMovRegMem::set_offset(int x) {
304N/A int off = data_offset + instruction_start();
304N/A u_char mod_rm = *(u_char*)(instruction_address() + 1);
304N/A // nnnn(r12|rsp) isn't coded as simple mod/rm since that is
304N/A // the encoding to use an SIB byte. Which will have the nnnn
304N/A // field off by one byte
304N/A if ((mod_rm & 7) == 0x4) {
304N/A off++;
304N/A }
304N/A set_int_at(off, x);
0N/A}
0N/A
0N/Avoid NativeMovRegMem::verify() {
0N/A // make sure code pattern is actually a mov [reg+offset], reg instruction
0N/A u_char test_byte = *(u_char*)instruction_address();
304N/A switch (test_byte) {
304N/A case instruction_code_reg2memb: // 0x88 movb a, r
304N/A case instruction_code_reg2mem: // 0x89 movl a, r (can be movq in 64bit)
304N/A case instruction_code_mem2regb: // 0x8a movb r, a
304N/A case instruction_code_mem2reg: // 0x8b movl r, a (can be movq in 64bit)
304N/A break;
304N/A
304N/A case instruction_code_mem2reg_movslq: // 0x63 movsql r, a
304N/A case instruction_code_mem2reg_movzxb: // 0xb6 movzbl r, a (movzxb)
304N/A case instruction_code_mem2reg_movzxw: // 0xb7 movzwl r, a (movzxw)
304N/A case instruction_code_mem2reg_movsxb: // 0xbe movsbl r, a (movsxb)
304N/A case instruction_code_mem2reg_movsxw: // 0xbf movswl r, a (movsxw)
304N/A break;
304N/A
304N/A case instruction_code_float_s: // 0xd9 fld_s a
304N/A case instruction_code_float_d: // 0xdd fld_d a
304N/A case instruction_code_xmm_load: // 0x10 movsd xmm, a
304N/A case instruction_code_xmm_store: // 0x11 movsd a, xmm
304N/A case instruction_code_xmm_lpd: // 0x12 movlpd xmm, a
304N/A break;
304N/A
304N/A default:
0N/A fatal ("not a mov [reg+offs], reg instruction");
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid NativeMovRegMem::print() {
0N/A tty->print_cr("0x%x: mov reg, [reg + %x]", instruction_address(), offset());
0N/A}
0N/A
0N/A//-------------------------------------------------------------------
0N/A
0N/Avoid NativeLoadAddress::verify() {
0N/A // make sure code pattern is actually a mov [reg+offset], reg instruction
0N/A u_char test_byte = *(u_char*)instruction_address();
304N/A#ifdef _LP64
304N/A if ( (test_byte == instruction_prefix_wide ||
304N/A test_byte == instruction_prefix_wide_extended) ) {
304N/A test_byte = *(u_char*)(instruction_address() + 1);
304N/A }
304N/A#endif // _LP64
304N/A if ( ! ((test_byte == lea_instruction_code)
304N/A LP64_ONLY(|| (test_byte == mov64_instruction_code) ))) {
0N/A fatal ("not a lea reg, [reg+offs] instruction");
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid NativeLoadAddress::print() {
0N/A tty->print_cr("0x%x: lea [reg + %x], reg", instruction_address(), offset());
0N/A}
0N/A
0N/A//--------------------------------------------------------------------------------
0N/A
0N/Avoid NativeJump::verify() {
0N/A if (*(u_char*)instruction_address() != instruction_code) {
0N/A fatal("not a jump instruction");
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid NativeJump::insert(address code_pos, address entry) {
0N/A intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
0N/A#ifdef AMD64
0N/A guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
0N/A#endif // AMD64
0N/A
0N/A *code_pos = instruction_code;
0N/A *((int32_t*)(code_pos + 1)) = (int32_t)disp;
0N/A
0N/A ICache::invalidate_range(code_pos, instruction_size);
0N/A}
0N/A
0N/Avoid NativeJump::check_verified_entry_alignment(address entry, address verified_entry) {
0N/A // Patching to not_entrant can happen while activations of the method are
0N/A // in use. The patching in that instance must happen only when certain
0N/A // alignment restrictions are true. These guarantees check those
0N/A // conditions.
0N/A#ifdef AMD64
0N/A const int linesize = 64;
0N/A#else
0N/A const int linesize = 32;
0N/A#endif // AMD64
0N/A
0N/A // Must be wordSize aligned
0N/A guarantee(((uintptr_t) verified_entry & (wordSize -1)) == 0,
0N/A "illegal address for code patching 2");
0N/A // First 5 bytes must be within the same cache line - 4827828
0N/A guarantee((uintptr_t) verified_entry / linesize ==
0N/A ((uintptr_t) verified_entry + 4) / linesize,
0N/A "illegal address for code patching 3");
0N/A}
0N/A
0N/A
0N/A// MT safe inserting of a jump over an unknown instruction sequence (used by nmethod::makeZombie)
0N/A// The problem: jmp <dest> is a 5-byte instruction. Atomical write can be only with 4 bytes.
0N/A// First patches the first word atomically to be a jump to itself.
0N/A// Then patches the last byte and then atomically patches the first word (4-bytes),
0N/A// thus inserting the desired jump
0N/A// This code is mt-safe with the following conditions: entry point is 4 byte aligned,
0N/A// entry point is in same cache line as unverified entry point, and the instruction being
0N/A// patched is >= 5 byte (size of patch).
0N/A//
0N/A// In C2 the 5+ byte sized instruction is enforced by code in MachPrologNode::emit.
0N/A// In C1 the restriction is enforced by CodeEmitter::method_entry
0N/A//
0N/Avoid NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) {
0N/A // complete jump instruction (to be inserted) is in code_buffer;
0N/A unsigned char code_buffer[5];
0N/A code_buffer[0] = instruction_code;
0N/A intptr_t disp = (intptr_t)dest - ((intptr_t)verified_entry + 1 + 4);
0N/A#ifdef AMD64
0N/A guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
0N/A#endif // AMD64
0N/A *(int32_t*)(code_buffer + 1) = (int32_t)disp;
0N/A
0N/A check_verified_entry_alignment(entry, verified_entry);
0N/A
0N/A // Can't call nativeJump_at() because it's asserts jump exists
0N/A NativeJump* n_jump = (NativeJump*) verified_entry;
0N/A
0N/A //First patch dummy jmp in place
0N/A
0N/A unsigned char patch[4];
0N/A assert(sizeof(patch)==sizeof(int32_t), "sanity check");
0N/A patch[0] = 0xEB; // jmp rel8
0N/A patch[1] = 0xFE; // jmp to self
0N/A patch[2] = 0xEB;
0N/A patch[3] = 0xFE;
0N/A
0N/A // First patch dummy jmp in place
0N/A *(int32_t*)verified_entry = *(int32_t *)patch;
0N/A
0N/A n_jump->wrote(0);
0N/A
0N/A // Patch 5th byte (from jump instruction)
0N/A verified_entry[4] = code_buffer[4];
0N/A
0N/A n_jump->wrote(4);
0N/A
0N/A // Patch bytes 0-3 (from jump instruction)
0N/A *(int32_t*)verified_entry = *(int32_t *)code_buffer;
0N/A // Invalidate. Opteron requires a flush after every write.
0N/A n_jump->wrote(0);
0N/A
0N/A}
0N/A
0N/Avoid NativePopReg::insert(address code_pos, Register reg) {
0N/A assert(reg->encoding() < 8, "no space for REX");
0N/A assert(NativePopReg::instruction_size == sizeof(char), "right address unit for update");
0N/A *code_pos = (u_char)(instruction_code | reg->encoding());
0N/A ICache::invalidate_range(code_pos, instruction_size);
0N/A}
0N/A
0N/A
0N/Avoid NativeIllegalInstruction::insert(address code_pos) {
0N/A assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update");
0N/A *(short *)code_pos = instruction_code;
0N/A ICache::invalidate_range(code_pos, instruction_size);
0N/A}
0N/A
0N/Avoid NativeGeneralJump::verify() {
0N/A assert(((NativeInstruction *)this)->is_jump() ||
0N/A ((NativeInstruction *)this)->is_cond_jump(), "not a general jump instruction");
0N/A}
0N/A
0N/A
0N/Avoid NativeGeneralJump::insert_unconditional(address code_pos, address entry) {
0N/A intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
0N/A#ifdef AMD64
0N/A guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
0N/A#endif // AMD64
0N/A
0N/A *code_pos = unconditional_long_jump;
0N/A *((int32_t *)(code_pos+1)) = (int32_t) disp;
0N/A ICache::invalidate_range(code_pos, instruction_size);
0N/A}
0N/A
0N/A
0N/A// MT-safe patching of a long jump instruction.
0N/A// First patches first word of instruction to two jmp's that jmps to them
0N/A// selfs (spinlock). Then patches the last byte, and then atomicly replaces
0N/A// the jmp's with the first 4 byte of the new instruction.
0N/Avoid NativeGeneralJump::replace_mt_safe(address instr_addr, address code_buffer) {
0N/A assert (instr_addr != NULL, "illegal address for code patching (4)");
0N/A NativeGeneralJump* n_jump = nativeGeneralJump_at (instr_addr); // checking that it is a jump
0N/A
0N/A // Temporary code
0N/A unsigned char patch[4];
0N/A assert(sizeof(patch)==sizeof(int32_t), "sanity check");
0N/A patch[0] = 0xEB; // jmp rel8
0N/A patch[1] = 0xFE; // jmp to self
0N/A patch[2] = 0xEB;
0N/A patch[3] = 0xFE;
0N/A
0N/A // First patch dummy jmp in place
0N/A *(int32_t*)instr_addr = *(int32_t *)patch;
0N/A n_jump->wrote(0);
0N/A
0N/A // Patch 4th byte
0N/A instr_addr[4] = code_buffer[4];
0N/A
0N/A n_jump->wrote(4);
0N/A
0N/A // Patch bytes 0-3
0N/A *(jint*)instr_addr = *(jint *)code_buffer;
0N/A
0N/A n_jump->wrote(0);
0N/A
0N/A#ifdef ASSERT
0N/A // verify patching
0N/A for ( int i = 0; i < instruction_size; i++) {
0N/A address ptr = (address)((intptr_t)code_buffer + i);
0N/A int a_byte = (*ptr) & 0xFF;
0N/A assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
0N/A }
0N/A#endif
0N/A
0N/A}
0N/A
0N/A
0N/A
0N/Aaddress NativeGeneralJump::jump_destination() const {
0N/A int op_code = ubyte_at(0);
0N/A bool is_rel32off = (op_code == 0xE9 || op_code == 0x0F);
0N/A int offset = (op_code == 0x0F) ? 2 : 1;
0N/A int length = offset + ((is_rel32off) ? 4 : 1);
0N/A
0N/A if (is_rel32off)
0N/A return addr_at(0) + length + int_at(offset);
0N/A else
0N/A return addr_at(0) + length + sbyte_at(offset);
0N/A}
116N/A
116N/Abool NativeInstruction::is_dtrace_trap() {
116N/A return (*(int32_t*)this & 0xff) == 0xcc;
116N/A}