1 /*
   2  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.inline.hpp"
  27 #include "assembler_sparc.inline.hpp"
  28 #include "code/relocInfo.hpp"
  29 #include "nativeInst_sparc.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/safepoint.hpp"
  32 
  33 void Relocation::pd_set_data_value(address x, intptr_t o) {
  34   NativeInstruction* ip = nativeInstruction_at(addr());
  35   jint inst = ip->long_at(0);
  36   assert(inst != NativeInstruction::illegal_instruction(), "no breakpoint");
  37   switch (Assembler::inv_op(inst)) {
  38 
  39   case Assembler::ldst_op:
  40     #ifdef ASSERT
  41       switch (Assembler::inv_op3(inst)) {
  42         case Assembler::lduw_op3:
  43         case Assembler::ldub_op3:
  44         case Assembler::lduh_op3:
  45         case Assembler::ldd_op3:
  46         case Assembler::ldsw_op3:
  47         case Assembler::ldsb_op3:
  48         case Assembler::ldsh_op3:
  49         case Assembler::ldx_op3:
  50         case Assembler::ldf_op3:
  51         case Assembler::lddf_op3:
  52         case Assembler::stw_op3:
  53         case Assembler::stb_op3:
  54         case Assembler::sth_op3:
  55         case Assembler::std_op3:
  56         case Assembler::stx_op3:
  57         case Assembler::stf_op3:
  58         case Assembler::stdf_op3:
  59         case Assembler::casa_op3:
  60         case Assembler::casxa_op3:
  61           break;
  62         default:
  63           ShouldNotReachHere();
  64       }
  65       goto do_non_sethi;
  66     #endif
  67 
  68   case Assembler::arith_op:
  69     #ifdef ASSERT
  70       switch (Assembler::inv_op3(inst)) {
  71         case Assembler::or_op3:
  72         case Assembler::add_op3:
  73         case Assembler::jmpl_op3:
  74           break;
  75         default:
  76           ShouldNotReachHere();
  77       }
  78     do_non_sethi:;
  79     #endif
  80     {
  81     guarantee(Assembler::inv_immed(inst), "must have a simm13 field");
  82     int simm13 = Assembler::low10((intptr_t)x) + o;
  83     guarantee(Assembler::is_simm13(simm13), "offset can't overflow simm13");
  84     inst &= ~Assembler::simm(    -1, 13);
  85     inst |=  Assembler::simm(simm13, 13);
  86     ip->set_long_at(0, inst);
  87     }
  88     break;
  89 
  90   case Assembler::branch_op:
  91     {
  92 #ifdef _LP64
  93     jint inst2;
  94     guarantee(Assembler::inv_op2(inst)==Assembler::sethi_op2, "must be sethi");
  95     if (format() != 0) {
  96       assert(type() == relocInfo::oop_type, "only narrow oops case");
  97       jint np = oopDesc::encode_heap_oop((oop)x);
  98       inst &= ~Assembler::hi22(-1);
  99       inst |=  Assembler::hi22((intptr_t)np);
 100       ip->set_long_at(0, inst);
 101       inst2 = ip->long_at( NativeInstruction::nop_instruction_size );
 102       guarantee(Assembler::inv_op(inst2)==Assembler::arith_op, "arith op");
 103       ip->set_long_at(NativeInstruction::nop_instruction_size, ip->set_data32_simm13( inst2, (intptr_t)np));
 104       break;
 105     }
 106     ip->set_data64_sethi( ip->addr_at(0), (intptr_t)x );
 107 #else
 108     guarantee(Assembler::inv_op2(inst)==Assembler::sethi_op2, "must be sethi");
 109     inst &= ~Assembler::hi22(     -1);
 110     inst |=  Assembler::hi22((intptr_t)x);
 111     // (ignore offset; it doesn't play into the sethi)
 112     ip->set_long_at(0, inst);
 113 #endif
 114     }
 115     break;
 116 
 117   default:
 118     guarantee(false, "instruction must perform arithmetic or memory access");
 119   }
 120 }
 121 
 122 
 123 address Relocation::pd_call_destination(address orig_addr) {
 124   intptr_t adj = 0;
 125   if (orig_addr != NULL) {
 126     // We just moved this call instruction from orig_addr to addr().
 127     // This means its target will appear to have grown by addr() - orig_addr.
 128     adj = -( addr() - orig_addr );
 129   }
 130   if (NativeCall::is_call_at(addr())) {
 131     NativeCall* call = nativeCall_at(addr());
 132     return call->destination() + adj;
 133   }
 134   if (NativeFarCall::is_call_at(addr())) {
 135     NativeFarCall* call = nativeFarCall_at(addr());
 136     return call->destination() + adj;
 137   }
 138   // Special case:  Patchable branch local to the code cache.
 139   // This will break badly if the code cache grows larger than a few Mb.
 140   NativeGeneralJump* br = nativeGeneralJump_at(addr());
 141   return br->jump_destination() + adj;
 142 }
 143 
 144 
 145 void Relocation::pd_set_call_destination(address x) {
 146   if (NativeCall::is_call_at(addr())) {
 147     NativeCall* call = nativeCall_at(addr());
 148     call->set_destination(x);
 149     return;
 150   }
 151   if (NativeFarCall::is_call_at(addr())) {
 152     NativeFarCall* call = nativeFarCall_at(addr());
 153     call->set_destination(x);
 154     return;
 155   }
 156   // Special case:  Patchable branch local to the code cache.
 157   // This will break badly if the code cache grows larger than a few Mb.
 158   NativeGeneralJump* br = nativeGeneralJump_at(addr());
 159   br->set_jump_destination(x);
 160 }
 161 
 162 
 163 address* Relocation::pd_address_in_code() {
 164   // SPARC never embeds addresses in code, at present.
 165   //assert(type() == relocInfo::oop_type, "only oops are inlined at present");
 166   return (address*)addr();
 167 }
 168 
 169 
 170 address Relocation::pd_get_address_from_code() {
 171   // SPARC never embeds addresses in code, at present.
 172   //assert(type() == relocInfo::oop_type, "only oops are inlined at present");
 173   return *(address*)addr();
 174 }
 175 
 176 
 177 int Relocation::pd_breakpoint_size() {
 178   // minimum breakpoint size, in short words
 179   return NativeIllegalInstruction::instruction_size / sizeof(short);
 180 }
 181 
 182 void Relocation::pd_swap_in_breakpoint(address x, short* instrs, int instrlen) {
 183   Untested("pd_swap_in_breakpoint");
 184   // %%% probably do not need a general instrlen; just use the trap size
 185   if (instrs != NULL) {
 186     assert(instrlen * sizeof(short) == NativeIllegalInstruction::instruction_size, "enough instrlen in reloc. data");
 187     for (int i = 0; i < instrlen; i++) {
 188       instrs[i] = ((short*)x)[i];
 189     }
 190   }
 191   NativeIllegalInstruction::insert(x);
 192 }
 193 
 194 
 195 void Relocation::pd_swap_out_breakpoint(address x, short* instrs, int instrlen) {
 196   Untested("pd_swap_out_breakpoint");
 197   assert(instrlen * sizeof(short) == sizeof(int), "enough buf");
 198   union { int l; short s[1]; } u;
 199   for (int i = 0; i < instrlen; i++) {
 200     u.s[i] = instrs[i];
 201   }
 202   NativeInstruction* ni = nativeInstruction_at(x);
 203   ni->set_long_at(0, u.l);
 204 }
 205 
 206 void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 207 }
 208 
 209 void poll_return_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 210 }