duke@435: /* iveresov@2686: * Copyright (c) 1998, 2011, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "asm/assembler.inline.hpp" stefank@2314: #include "assembler_x86.inline.hpp" stefank@2314: #include "code/relocInfo.hpp" stefank@2314: #include "nativeInst_x86.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/safepoint.hpp" duke@435: duke@435: never@2657: void Relocation::pd_set_data_value(address x, intptr_t o, bool verify_only) { duke@435: #ifdef AMD64 duke@435: x += o; duke@435: typedef Assembler::WhichOperand WhichOperand; never@739: WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm, call32, narrow oop duke@435: assert(which == Assembler::disp32_operand || kvn@599: which == Assembler::narrow_oop_operand || never@739: which == Assembler::imm_operand, "format unpacks ok"); never@739: if (which == Assembler::imm_operand) { never@2657: if (verify_only) { never@2657: assert(*pd_address_in_code() == x, "instructions must match"); never@2657: } else { never@2657: *pd_address_in_code() = x; never@2657: } kvn@599: } else if (which == Assembler::narrow_oop_operand) { kvn@599: address disp = Assembler::locate_operand(addr(), which); never@2657: if (verify_only) { never@2657: assert(*(uint32_t*) disp == oopDesc::encode_heap_oop((oop)x), "instructions must match"); never@2657: } else { never@2657: *(int32_t*) disp = oopDesc::encode_heap_oop((oop)x); never@2657: } duke@435: } else { duke@435: // Note: Use runtime_call_type relocations for call32_operand. duke@435: address ip = addr(); duke@435: address disp = Assembler::locate_operand(ip, which); duke@435: address next_ip = Assembler::locate_next_instruction(ip); never@2657: if (verify_only) { never@2657: assert(*(int32_t*) disp == (x - next_ip), "instructions must match"); never@2657: } else { never@2657: *(int32_t*) disp = x - next_ip; never@2657: } duke@435: } duke@435: #else never@2657: if (verify_only) { never@2657: assert(*pd_address_in_code() == (x + o), "instructions must match"); never@2657: } else { never@2657: *pd_address_in_code() = x + o; never@2657: } duke@435: #endif // AMD64 duke@435: } duke@435: duke@435: duke@435: address Relocation::pd_call_destination(address orig_addr) { duke@435: intptr_t adj = 0; duke@435: if (orig_addr != NULL) { duke@435: // We just moved this call instruction from orig_addr to addr(). duke@435: // This means its target will appear to have grown by addr() - orig_addr. duke@435: adj = -( addr() - orig_addr ); duke@435: } duke@435: NativeInstruction* ni = nativeInstruction_at(addr()); duke@435: if (ni->is_call()) { duke@435: return nativeCall_at(addr())->destination() + adj; duke@435: } else if (ni->is_jump()) { duke@435: return nativeJump_at(addr())->jump_destination() + adj; duke@435: } else if (ni->is_cond_jump()) { duke@435: return nativeGeneralJump_at(addr())->jump_destination() + adj; duke@435: } else if (ni->is_mov_literal64()) { duke@435: return (address) ((NativeMovConstReg*)ni)->data(); duke@435: } else { duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: } duke@435: duke@435: duke@435: void Relocation::pd_set_call_destination(address x) { duke@435: NativeInstruction* ni = nativeInstruction_at(addr()); duke@435: if (ni->is_call()) { duke@435: nativeCall_at(addr())->set_destination(x); duke@435: } else if (ni->is_jump()) { duke@435: NativeJump* nj = nativeJump_at(addr()); never@739: never@739: // Unresolved jumps are recognized by a destination of -1 never@739: // However 64bit can't actually produce such an address never@739: // and encodes a jump to self but jump_destination will never@739: // return a -1 as the signal. We must not relocate this never@739: // jmp or the ic code will not see it as unresolved. never@739: duke@435: if (nj->jump_destination() == (address) -1) { never@739: x = addr(); // jump to self duke@435: } duke@435: nj->set_jump_destination(x); duke@435: } else if (ni->is_cond_jump()) { duke@435: // %%%% kludge this, for now, until we get a jump_destination method duke@435: address old_dest = nativeGeneralJump_at(addr())->jump_destination(); duke@435: address disp = Assembler::locate_operand(addr(), Assembler::call32_operand); duke@435: *(jint*)disp += (x - old_dest); duke@435: } else if (ni->is_mov_literal64()) { duke@435: ((NativeMovConstReg*)ni)->set_data((intptr_t)x); duke@435: } else { duke@435: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: duke@435: duke@435: address* Relocation::pd_address_in_code() { duke@435: // All embedded Intel addresses are stored in 32-bit words. duke@435: // Since the addr points at the start of the instruction, duke@435: // we must parse the instruction a bit to find the embedded word. duke@435: assert(is_data(), "must be a DataRelocation"); duke@435: typedef Assembler::WhichOperand WhichOperand; never@739: WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm/imm32 duke@435: #ifdef AMD64 duke@435: assert(which == Assembler::disp32_operand || duke@435: which == Assembler::call32_operand || never@739: which == Assembler::imm_operand, "format unpacks ok"); never@739: if (which != Assembler::imm_operand) { duke@435: // The "address" in the code is a displacement can't return it as duke@435: // and address* since it is really a jint* duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: #else never@739: assert(which == Assembler::disp32_operand || which == Assembler::imm_operand, "format unpacks ok"); duke@435: #endif // AMD64 duke@435: return (address*) Assembler::locate_operand(addr(), which); duke@435: } duke@435: duke@435: duke@435: address Relocation::pd_get_address_from_code() { duke@435: #ifdef AMD64 duke@435: // All embedded Intel addresses are stored in 32-bit words. duke@435: // Since the addr points at the start of the instruction, duke@435: // we must parse the instruction a bit to find the embedded word. duke@435: assert(is_data(), "must be a DataRelocation"); duke@435: typedef Assembler::WhichOperand WhichOperand; never@739: WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm/imm32 duke@435: assert(which == Assembler::disp32_operand || duke@435: which == Assembler::call32_operand || never@739: which == Assembler::imm_operand, "format unpacks ok"); never@739: if (which != Assembler::imm_operand) { duke@435: address ip = addr(); duke@435: address disp = Assembler::locate_operand(ip, which); duke@435: address next_ip = Assembler::locate_next_instruction(ip); duke@435: address a = next_ip + *(int32_t*) disp; duke@435: return a; duke@435: } duke@435: #endif // AMD64 duke@435: return *pd_address_in_code(); duke@435: } duke@435: duke@435: int Relocation::pd_breakpoint_size() { duke@435: // minimum breakpoint size, in short words duke@435: return NativeIllegalInstruction::instruction_size / sizeof(short); duke@435: } duke@435: duke@435: void Relocation::pd_swap_in_breakpoint(address x, short* instrs, int instrlen) { duke@435: Untested("pd_swap_in_breakpoint"); duke@435: if (instrs != NULL) { duke@435: assert(instrlen * sizeof(short) == NativeIllegalInstruction::instruction_size, "enough instrlen in reloc. data"); duke@435: for (int i = 0; i < instrlen; i++) { duke@435: instrs[i] = ((short*)x)[i]; duke@435: } duke@435: } duke@435: NativeIllegalInstruction::insert(x); duke@435: } duke@435: duke@435: duke@435: void Relocation::pd_swap_out_breakpoint(address x, short* instrs, int instrlen) { duke@435: Untested("pd_swap_out_breakpoint"); duke@435: assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update"); duke@435: NativeInstruction* ni = nativeInstruction_at(x); duke@435: *(short*)ni->addr_at(0) = instrs[0]; duke@435: } never@739: never@739: void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { never@739: #ifdef _LP64 iveresov@2686: if (!Assembler::is_polling_page_far()) { iveresov@2686: typedef Assembler::WhichOperand WhichOperand; iveresov@2686: WhichOperand which = (WhichOperand) format(); iveresov@2686: // This format is imm but it is really disp32 iveresov@2686: which = Assembler::disp32_operand; iveresov@2686: address orig_addr = old_addr_for(addr(), src, dest); iveresov@2686: NativeInstruction* oni = nativeInstruction_at(orig_addr); iveresov@2686: int32_t* orig_disp = (int32_t*) Assembler::locate_operand(orig_addr, which); iveresov@2686: // This poll_addr is incorrect by the size of the instruction it is irrelevant iveresov@2686: intptr_t poll_addr = (intptr_t)oni + *orig_disp; never@739: iveresov@2686: NativeInstruction* ni = nativeInstruction_at(addr()); iveresov@2686: intptr_t new_disp = poll_addr - (intptr_t) ni; never@739: iveresov@2686: int32_t* disp = (int32_t*) Assembler::locate_operand(addr(), which); iveresov@2686: * disp = (int32_t)new_disp; iveresov@2686: } never@739: #endif // _LP64 never@739: } never@739: never@739: void poll_return_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { never@739: #ifdef _LP64 iveresov@2686: if (!Assembler::is_polling_page_far()) { iveresov@2686: typedef Assembler::WhichOperand WhichOperand; iveresov@2686: WhichOperand which = (WhichOperand) format(); iveresov@2686: // This format is imm but it is really disp32 iveresov@2686: which = Assembler::disp32_operand; iveresov@2686: address orig_addr = old_addr_for(addr(), src, dest); iveresov@2686: NativeInstruction* oni = nativeInstruction_at(orig_addr); iveresov@2686: int32_t* orig_disp = (int32_t*) Assembler::locate_operand(orig_addr, which); iveresov@2686: // This poll_addr is incorrect by the size of the instruction it is irrelevant iveresov@2686: intptr_t poll_addr = (intptr_t)oni + *orig_disp; never@739: iveresov@2686: NativeInstruction* ni = nativeInstruction_at(addr()); iveresov@2686: intptr_t new_disp = poll_addr - (intptr_t) ni; never@739: iveresov@2686: int32_t* disp = (int32_t*) Assembler::locate_operand(addr(), which); iveresov@2686: * disp = (int32_t)new_disp; iveresov@2686: } never@739: #endif // _LP64 never@739: }