duke@435: /* duke@435: * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_nativeInst_x86.cpp.incl" duke@435: duke@435: void NativeInstruction::wrote(int offset) { duke@435: ICache::invalidate_word(addr_at(offset)); duke@435: } duke@435: duke@435: duke@435: void NativeCall::verify() { duke@435: // Make sure code pattern is actually a call imm32 instruction. duke@435: int inst = ubyte_at(0); duke@435: if (inst != instruction_code) { duke@435: tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", instruction_address(), duke@435: inst); duke@435: fatal("not a call disp32"); duke@435: } duke@435: } duke@435: duke@435: address NativeCall::destination() const { duke@435: // Getting the destination of a call isn't safe because that call can duke@435: // be getting patched while you're calling this. There's only special duke@435: // places where this can be called but not automatically verifiable by duke@435: // checking which locks are held. The solution is true atomic patching duke@435: // on x86, nyi. duke@435: return return_address() + displacement(); duke@435: } duke@435: duke@435: void NativeCall::print() { duke@435: tty->print_cr(PTR_FORMAT ": call " PTR_FORMAT, duke@435: instruction_address(), destination()); duke@435: } duke@435: duke@435: // Inserts a native call instruction at a given pc duke@435: void NativeCall::insert(address code_pos, address entry) { duke@435: intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4); duke@435: #ifdef AMD64 duke@435: guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset"); duke@435: #endif // AMD64 duke@435: *code_pos = instruction_code; duke@435: *((int32_t *)(code_pos+1)) = (int32_t) disp; duke@435: ICache::invalidate_range(code_pos, instruction_size); duke@435: } duke@435: duke@435: // MT-safe patching of a call instruction. duke@435: // First patches first word of instruction to two jmp's that jmps to them duke@435: // selfs (spinlock). Then patches the last byte, and then atomicly replaces duke@435: // the jmp's with the first 4 byte of the new instruction. duke@435: void NativeCall::replace_mt_safe(address instr_addr, address code_buffer) { duke@435: assert(Patching_lock->is_locked() || duke@435: SafepointSynchronize::is_at_safepoint(), "concurrent code patching"); duke@435: assert (instr_addr != NULL, "illegal address for code patching"); duke@435: duke@435: NativeCall* n_call = nativeCall_at (instr_addr); // checking that it is a call duke@435: if (os::is_MP()) { duke@435: guarantee((intptr_t)instr_addr % BytesPerWord == 0, "must be aligned"); duke@435: } duke@435: duke@435: // First patch dummy jmp in place duke@435: unsigned char patch[4]; duke@435: assert(sizeof(patch)==sizeof(jint), "sanity check"); duke@435: patch[0] = 0xEB; // jmp rel8 duke@435: patch[1] = 0xFE; // jmp to self duke@435: patch[2] = 0xEB; duke@435: patch[3] = 0xFE; duke@435: duke@435: // First patch dummy jmp in place duke@435: *(jint*)instr_addr = *(jint *)patch; duke@435: duke@435: // Invalidate. Opteron requires a flush after every write. duke@435: n_call->wrote(0); duke@435: duke@435: // Patch 4th byte duke@435: instr_addr[4] = code_buffer[4]; duke@435: duke@435: n_call->wrote(4); duke@435: duke@435: // Patch bytes 0-3 duke@435: *(jint*)instr_addr = *(jint *)code_buffer; duke@435: duke@435: n_call->wrote(0); duke@435: duke@435: #ifdef ASSERT duke@435: // verify patching duke@435: for ( int i = 0; i < instruction_size; i++) { duke@435: address ptr = (address)((intptr_t)code_buffer + i); duke@435: int a_byte = (*ptr) & 0xFF; duke@435: assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed"); duke@435: } duke@435: #endif duke@435: duke@435: } duke@435: duke@435: duke@435: // Similar to replace_mt_safe, but just changes the destination. The duke@435: // important thing is that free-running threads are able to execute this duke@435: // call instruction at all times. If the displacement field is aligned duke@435: // we can simply rely on atomicity of 32-bit writes to make sure other threads duke@435: // will see no intermediate states. Otherwise, the first two bytes of the duke@435: // call are guaranteed to be aligned, and can be atomically patched to a duke@435: // self-loop to guard the instruction while we change the other bytes. duke@435: duke@435: // We cannot rely on locks here, since the free-running threads must run at duke@435: // full speed. duke@435: // duke@435: // Used in the runtime linkage of calls; see class CompiledIC. duke@435: // (Cf. 4506997 and 4479829, where threads witnessed garbage displacements.) duke@435: void NativeCall::set_destination_mt_safe(address dest) { duke@435: debug_only(verify()); duke@435: // Make sure patching code is locked. No two threads can patch at the same duke@435: // time but one may be executing this code. duke@435: assert(Patching_lock->is_locked() || duke@435: SafepointSynchronize::is_at_safepoint(), "concurrent code patching"); duke@435: // Both C1 and C2 should now be generating code which aligns the patched address duke@435: // to be within a single cache line except that C1 does not do the alignment on duke@435: // uniprocessor systems. duke@435: bool is_aligned = ((uintptr_t)displacement_address() + 0) / cache_line_size == duke@435: ((uintptr_t)displacement_address() + 3) / cache_line_size; duke@435: duke@435: guarantee(!os::is_MP() || is_aligned, "destination must be aligned"); duke@435: duke@435: if (is_aligned) { duke@435: // Simple case: The destination lies within a single cache line. duke@435: set_destination(dest); duke@435: } else if ((uintptr_t)instruction_address() / cache_line_size == duke@435: ((uintptr_t)instruction_address()+1) / cache_line_size) { duke@435: // Tricky case: The instruction prefix lies within a single cache line. duke@435: intptr_t disp = dest - return_address(); duke@435: #ifdef AMD64 duke@435: guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset"); duke@435: #endif // AMD64 duke@435: duke@435: int call_opcode = instruction_address()[0]; duke@435: duke@435: // First patch dummy jump in place: duke@435: { duke@435: u_char patch_jump[2]; duke@435: patch_jump[0] = 0xEB; // jmp rel8 duke@435: patch_jump[1] = 0xFE; // jmp to self duke@435: duke@435: assert(sizeof(patch_jump)==sizeof(short), "sanity check"); duke@435: *(short*)instruction_address() = *(short*)patch_jump; duke@435: } duke@435: // Invalidate. Opteron requires a flush after every write. duke@435: wrote(0); duke@435: duke@435: // (Note: We assume any reader which has already started to read duke@435: // the unpatched call will completely read the whole unpatched call duke@435: // without seeing the next writes we are about to make.) duke@435: duke@435: // Next, patch the last three bytes: duke@435: u_char patch_disp[5]; duke@435: patch_disp[0] = call_opcode; duke@435: *(int32_t*)&patch_disp[1] = (int32_t)disp; duke@435: assert(sizeof(patch_disp)==instruction_size, "sanity check"); duke@435: for (int i = sizeof(short); i < instruction_size; i++) duke@435: instruction_address()[i] = patch_disp[i]; duke@435: duke@435: // Invalidate. Opteron requires a flush after every write. duke@435: wrote(sizeof(short)); duke@435: duke@435: // (Note: We assume that any reader which reads the opcode we are duke@435: // about to repatch will also read the writes we just made.) duke@435: duke@435: // Finally, overwrite the jump: duke@435: *(short*)instruction_address() = *(short*)patch_disp; duke@435: // Invalidate. Opteron requires a flush after every write. duke@435: wrote(0); duke@435: duke@435: debug_only(verify()); duke@435: guarantee(destination() == dest, "patch succeeded"); duke@435: } else { duke@435: // Impossible: One or the other must be atomically writable. duke@435: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: duke@435: duke@435: void NativeMovConstReg::verify() { duke@435: #ifdef AMD64 duke@435: // make sure code pattern is actually a mov reg64, imm64 instruction duke@435: if ((ubyte_at(0) != Assembler::REX_W && ubyte_at(0) != Assembler::REX_WB) || duke@435: (ubyte_at(1) & (0xff ^ register_mask)) != 0xB8) { duke@435: print(); duke@435: fatal("not a REX.W[B] mov reg64, imm64"); duke@435: } duke@435: #else duke@435: // make sure code pattern is actually a mov reg, imm32 instruction duke@435: u_char test_byte = *(u_char*)instruction_address(); duke@435: u_char test_byte_2 = test_byte & ( 0xff ^ register_mask); duke@435: if (test_byte_2 != instruction_code) fatal("not a mov reg, imm32"); duke@435: #endif // AMD64 duke@435: } duke@435: duke@435: duke@435: void NativeMovConstReg::print() { duke@435: tty->print_cr(PTR_FORMAT ": mov reg, " INTPTR_FORMAT, duke@435: instruction_address(), data()); duke@435: } duke@435: duke@435: //------------------------------------------------------------------- duke@435: duke@435: #ifndef AMD64 duke@435: duke@435: void NativeMovRegMem::copy_instruction_to(address new_instruction_address) { duke@435: int inst_size = instruction_size; duke@435: duke@435: // See if there's an instruction size prefix override. duke@435: if ( *(address(this)) == instruction_operandsize_prefix && duke@435: *(address(this)+1) != instruction_code_xmm_code ) { // Not SSE instr duke@435: inst_size += 1; duke@435: } duke@435: if ( *(address(this)) == instruction_extended_prefix ) inst_size += 1; duke@435: duke@435: for (int i = 0; i < instruction_size; i++) { duke@435: *(new_instruction_address + i) = *(address(this) + i); duke@435: } duke@435: } duke@435: duke@435: void NativeMovRegMem::verify() { duke@435: // make sure code pattern is actually a mov [reg+offset], reg instruction duke@435: u_char test_byte = *(u_char*)instruction_address(); duke@435: if ( ! ( (test_byte == instruction_code_reg2memb) duke@435: || (test_byte == instruction_code_mem2regb) duke@435: || (test_byte == instruction_code_mem2regl) duke@435: || (test_byte == instruction_code_reg2meml) duke@435: || (test_byte == instruction_code_mem2reg_movzxb ) duke@435: || (test_byte == instruction_code_mem2reg_movzxw ) duke@435: || (test_byte == instruction_code_mem2reg_movsxb ) duke@435: || (test_byte == instruction_code_mem2reg_movsxw ) duke@435: || (test_byte == instruction_code_float_s) duke@435: || (test_byte == instruction_code_float_d) duke@435: || (test_byte == instruction_code_long_volatile) ) ) duke@435: { duke@435: u_char byte1 = ((u_char*)instruction_address())[1]; duke@435: u_char byte2 = ((u_char*)instruction_address())[2]; duke@435: if ((test_byte != instruction_code_xmm_ss_prefix && duke@435: test_byte != instruction_code_xmm_sd_prefix && duke@435: test_byte != instruction_operandsize_prefix) || duke@435: byte1 != instruction_code_xmm_code || duke@435: (byte2 != instruction_code_xmm_load && duke@435: byte2 != instruction_code_xmm_lpd && duke@435: byte2 != instruction_code_xmm_store)) { duke@435: fatal ("not a mov [reg+offs], reg instruction"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void NativeMovRegMem::print() { duke@435: tty->print_cr("0x%x: mov reg, [reg + %x]", instruction_address(), offset()); duke@435: } duke@435: duke@435: //------------------------------------------------------------------- duke@435: duke@435: void NativeLoadAddress::verify() { duke@435: // make sure code pattern is actually a mov [reg+offset], reg instruction duke@435: u_char test_byte = *(u_char*)instruction_address(); duke@435: if ( ! (test_byte == instruction_code) ) { duke@435: fatal ("not a lea reg, [reg+offs] instruction"); duke@435: } duke@435: } duke@435: duke@435: duke@435: void NativeLoadAddress::print() { duke@435: tty->print_cr("0x%x: lea [reg + %x], reg", instruction_address(), offset()); duke@435: } duke@435: duke@435: #endif // !AMD64 duke@435: duke@435: //-------------------------------------------------------------------------------- duke@435: duke@435: void NativeJump::verify() { duke@435: if (*(u_char*)instruction_address() != instruction_code) { duke@435: fatal("not a jump instruction"); duke@435: } duke@435: } duke@435: duke@435: duke@435: void NativeJump::insert(address code_pos, address entry) { duke@435: intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4); duke@435: #ifdef AMD64 duke@435: guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset"); duke@435: #endif // AMD64 duke@435: duke@435: *code_pos = instruction_code; duke@435: *((int32_t*)(code_pos + 1)) = (int32_t)disp; duke@435: duke@435: ICache::invalidate_range(code_pos, instruction_size); duke@435: } duke@435: duke@435: void NativeJump::check_verified_entry_alignment(address entry, address verified_entry) { duke@435: // Patching to not_entrant can happen while activations of the method are duke@435: // in use. The patching in that instance must happen only when certain duke@435: // alignment restrictions are true. These guarantees check those duke@435: // conditions. duke@435: #ifdef AMD64 duke@435: const int linesize = 64; duke@435: #else duke@435: const int linesize = 32; duke@435: #endif // AMD64 duke@435: duke@435: // Must be wordSize aligned duke@435: guarantee(((uintptr_t) verified_entry & (wordSize -1)) == 0, duke@435: "illegal address for code patching 2"); duke@435: // First 5 bytes must be within the same cache line - 4827828 duke@435: guarantee((uintptr_t) verified_entry / linesize == duke@435: ((uintptr_t) verified_entry + 4) / linesize, duke@435: "illegal address for code patching 3"); duke@435: } duke@435: duke@435: duke@435: // MT safe inserting of a jump over an unknown instruction sequence (used by nmethod::makeZombie) duke@435: // The problem: jmp is a 5-byte instruction. Atomical write can be only with 4 bytes. duke@435: // First patches the first word atomically to be a jump to itself. duke@435: // Then patches the last byte and then atomically patches the first word (4-bytes), duke@435: // thus inserting the desired jump duke@435: // This code is mt-safe with the following conditions: entry point is 4 byte aligned, duke@435: // entry point is in same cache line as unverified entry point, and the instruction being duke@435: // patched is >= 5 byte (size of patch). duke@435: // duke@435: // In C2 the 5+ byte sized instruction is enforced by code in MachPrologNode::emit. duke@435: // In C1 the restriction is enforced by CodeEmitter::method_entry duke@435: // duke@435: void NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) { duke@435: // complete jump instruction (to be inserted) is in code_buffer; duke@435: unsigned char code_buffer[5]; duke@435: code_buffer[0] = instruction_code; duke@435: intptr_t disp = (intptr_t)dest - ((intptr_t)verified_entry + 1 + 4); duke@435: #ifdef AMD64 duke@435: guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset"); duke@435: #endif // AMD64 duke@435: *(int32_t*)(code_buffer + 1) = (int32_t)disp; duke@435: duke@435: check_verified_entry_alignment(entry, verified_entry); duke@435: duke@435: // Can't call nativeJump_at() because it's asserts jump exists duke@435: NativeJump* n_jump = (NativeJump*) verified_entry; duke@435: duke@435: //First patch dummy jmp in place duke@435: duke@435: unsigned char patch[4]; duke@435: assert(sizeof(patch)==sizeof(int32_t), "sanity check"); duke@435: patch[0] = 0xEB; // jmp rel8 duke@435: patch[1] = 0xFE; // jmp to self duke@435: patch[2] = 0xEB; duke@435: patch[3] = 0xFE; duke@435: duke@435: // First patch dummy jmp in place duke@435: *(int32_t*)verified_entry = *(int32_t *)patch; duke@435: duke@435: n_jump->wrote(0); duke@435: duke@435: // Patch 5th byte (from jump instruction) duke@435: verified_entry[4] = code_buffer[4]; duke@435: duke@435: n_jump->wrote(4); duke@435: duke@435: // Patch bytes 0-3 (from jump instruction) duke@435: *(int32_t*)verified_entry = *(int32_t *)code_buffer; duke@435: // Invalidate. Opteron requires a flush after every write. duke@435: n_jump->wrote(0); duke@435: duke@435: } duke@435: duke@435: void NativePopReg::insert(address code_pos, Register reg) { duke@435: assert(reg->encoding() < 8, "no space for REX"); duke@435: assert(NativePopReg::instruction_size == sizeof(char), "right address unit for update"); duke@435: *code_pos = (u_char)(instruction_code | reg->encoding()); duke@435: ICache::invalidate_range(code_pos, instruction_size); duke@435: } duke@435: duke@435: duke@435: void NativeIllegalInstruction::insert(address code_pos) { duke@435: assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update"); duke@435: *(short *)code_pos = instruction_code; duke@435: ICache::invalidate_range(code_pos, instruction_size); duke@435: } duke@435: duke@435: void NativeGeneralJump::verify() { duke@435: assert(((NativeInstruction *)this)->is_jump() || duke@435: ((NativeInstruction *)this)->is_cond_jump(), "not a general jump instruction"); duke@435: } duke@435: duke@435: duke@435: void NativeGeneralJump::insert_unconditional(address code_pos, address entry) { duke@435: intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4); duke@435: #ifdef AMD64 duke@435: guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset"); duke@435: #endif // AMD64 duke@435: duke@435: *code_pos = unconditional_long_jump; duke@435: *((int32_t *)(code_pos+1)) = (int32_t) disp; duke@435: ICache::invalidate_range(code_pos, instruction_size); duke@435: } duke@435: duke@435: duke@435: // MT-safe patching of a long jump instruction. duke@435: // First patches first word of instruction to two jmp's that jmps to them duke@435: // selfs (spinlock). Then patches the last byte, and then atomicly replaces duke@435: // the jmp's with the first 4 byte of the new instruction. duke@435: void NativeGeneralJump::replace_mt_safe(address instr_addr, address code_buffer) { duke@435: assert (instr_addr != NULL, "illegal address for code patching (4)"); duke@435: NativeGeneralJump* n_jump = nativeGeneralJump_at (instr_addr); // checking that it is a jump duke@435: duke@435: // Temporary code duke@435: unsigned char patch[4]; duke@435: assert(sizeof(patch)==sizeof(int32_t), "sanity check"); duke@435: patch[0] = 0xEB; // jmp rel8 duke@435: patch[1] = 0xFE; // jmp to self duke@435: patch[2] = 0xEB; duke@435: patch[3] = 0xFE; duke@435: duke@435: // First patch dummy jmp in place duke@435: *(int32_t*)instr_addr = *(int32_t *)patch; duke@435: n_jump->wrote(0); duke@435: duke@435: // Patch 4th byte duke@435: instr_addr[4] = code_buffer[4]; duke@435: duke@435: n_jump->wrote(4); duke@435: duke@435: // Patch bytes 0-3 duke@435: *(jint*)instr_addr = *(jint *)code_buffer; duke@435: duke@435: n_jump->wrote(0); duke@435: duke@435: #ifdef ASSERT duke@435: // verify patching duke@435: for ( int i = 0; i < instruction_size; i++) { duke@435: address ptr = (address)((intptr_t)code_buffer + i); duke@435: int a_byte = (*ptr) & 0xFF; duke@435: assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed"); duke@435: } duke@435: #endif duke@435: duke@435: } duke@435: duke@435: duke@435: duke@435: address NativeGeneralJump::jump_destination() const { duke@435: int op_code = ubyte_at(0); duke@435: bool is_rel32off = (op_code == 0xE9 || op_code == 0x0F); duke@435: int offset = (op_code == 0x0F) ? 2 : 1; duke@435: int length = offset + ((is_rel32off) ? 4 : 1); duke@435: duke@435: if (is_rel32off) duke@435: return addr_at(0) + length + int_at(offset); duke@435: else duke@435: return addr_at(0) + length + sbyte_at(offset); duke@435: } kamg@551: kamg@551: bool NativeInstruction::is_dtrace_trap() { kamg@551: return (*(int32_t*)this & 0xff) == 0xcc; kamg@551: }