hotspot/src/cpu/sparc/vm/nativeInst_sparc.cpp
author kamg
Thu, 17 Apr 2008 22:18:15 -0400
changeset 363 99d43e8a76ad
parent 1 489c9b5090e2
child 670 ddf3e9583f2f
permissions -rw-r--r--
6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes Summary: Initial checkin of JSDT code Reviewed-by: acorn, sbohne
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_nativeInst_sparc.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
363
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
    29
bool NativeInstruction::is_dtrace_trap() {
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
    30
  return !is_nop();
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
    31
}
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
    32
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
void NativeInstruction::set_data64_sethi(address instaddr, intptr_t x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  CodeBuffer buf(instaddr, 10 * BytesPerInstWord );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  MacroAssembler* _masm = new MacroAssembler(&buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  Register destreg;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  destreg = inv_rd(*(unsigned int *)instaddr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  // Generate a the new sequence
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  Address dest( destreg, (address)x );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  _masm->sethi( dest, true );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  ICache::invalidate_range(instaddr, 7 * BytesPerInstWord);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
void NativeInstruction::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  // make sure code pattern is actually an instruction address
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  address addr = addr_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  if (addr == 0 || ((intptr_t)addr & 3) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    fatal("not an instruction address");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
void NativeInstruction::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  tty->print_cr(INTPTR_FORMAT ": 0x%x", addr_at(0), long_at(0));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
void NativeInstruction::set_long_at(int offset, int i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  address addr = addr_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  *(int*)addr = i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  ICache::invalidate_word(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
void NativeInstruction::set_jlong_at(int offset, jlong i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  address addr = addr_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  *(jlong*)addr = i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  // Don't need to invalidate 2 words here, because
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  // the flush instruction operates on doublewords.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  ICache::invalidate_word(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
void NativeInstruction::set_addr_at(int offset, address x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  address addr = addr_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  assert( ((intptr_t)addr & (wordSize-1)) == 0, "set_addr_at bad address alignment");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  *(uintptr_t*)addr = (uintptr_t)x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  // Don't need to invalidate 2 words here in the 64-bit case,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  // because the flush instruction operates on doublewords.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  ICache::invalidate_word(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  // The Intel code has this assertion for NativeCall::set_destination,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  // NativeMovConstReg::set_data, NativeMovRegMem::set_offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  // NativeJump::set_jump_destination, and NativePushImm32::set_data
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  //assert (Patching_lock->owned_by_self(), "must hold lock to patch instruction")
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
bool NativeInstruction::is_zero_test(Register &reg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  int x = long_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  Assembler::op3s temp = (Assembler::op3s) (Assembler::sub_op3 | Assembler::cc_bit_op3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  if (is_op3(x, temp, Assembler::arith_op) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      inv_immed(x) && inv_rd(x) == G0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
      if (inv_rs1(x) == G0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
        reg = inv_rs2(x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
        return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      } else if (inv_rs2(x) == G0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
        reg = inv_rs1(x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
        return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
bool NativeInstruction::is_load_store_with_small_offset(Register reg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  int x = long_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  if (is_op(x, Assembler::ldst_op) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
      inv_rs1(x) == reg && inv_immed(x)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
void NativeCall::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  NativeInstruction::verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  // make sure code pattern is actually a call instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  if (!is_op(long_at(0), Assembler::call_op)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    fatal("not a call");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
void NativeCall::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  tty->print_cr(INTPTR_FORMAT ": call " INTPTR_FORMAT, instruction_address(), destination());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
// MT-safe patching of a call instruction (and following word).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
// First patches the second word, and then atomicly replaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
// the first word with the first new instruction word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
// Other processors might briefly see the old first word
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
// followed by the new second word.  This is OK if the old
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
// second word is harmless, and the new second word may be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
// harmlessly executed in the delay slot of the call.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
void NativeCall::replace_mt_safe(address instr_addr, address code_buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  assert(Patching_lock->is_locked() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
         SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
   assert (instr_addr != NULL, "illegal address for code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
   NativeCall* n_call =  nativeCall_at (instr_addr); // checking that it is a call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
   assert(NativeCall::instruction_size == 8, "wrong instruction size; must be 8");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
   int i0 = ((int*)code_buffer)[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
   int i1 = ((int*)code_buffer)[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
   int* contention_addr = (int*) n_call->addr_at(1*BytesPerInstWord);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
   assert(inv_op(*contention_addr) == Assembler::arith_op ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
          *contention_addr == nop_instruction() || !VM_Version::v9_instructions_work(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
          "must not interfere with original call");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
   // The set_long_at calls do the ICacheInvalidate so we just need to do them in reverse order
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
   n_call->set_long_at(1*BytesPerInstWord, i1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
   n_call->set_long_at(0*BytesPerInstWord, i0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
   // NOTE:  It is possible that another thread T will execute
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
   // only the second patched word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
   // In other words, since the original instruction is this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
   //    call patching_stub; nop                   (NativeCall)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
   // and the new sequence from the buffer is this:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
   //    sethi %hi(K), %r; add %r, %lo(K), %r      (NativeMovConstReg)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
   // what T will execute is this:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
   //    call patching_stub; add %r, %lo(K), %r
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
   // thereby putting garbage into %r before calling the patching stub.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
   // This is OK, because the patching stub ignores the value of %r.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
   // Make sure the first-patched instruction, which may co-exist
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
   // briefly with the call, will do something harmless.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
   assert(inv_op(*contention_addr) == Assembler::arith_op ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
          *contention_addr == nop_instruction() || !VM_Version::v9_instructions_work(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
          "must not interfere with original call");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
// Similar to replace_mt_safe, but just changes the destination.  The
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
// important thing is that free-running threads are able to execute this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
// call instruction at all times.  Thus, the displacement field must be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
// instruction-word-aligned.  This is always true on SPARC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
// Used in the runtime linkage of calls; see class CompiledIC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
void NativeCall::set_destination_mt_safe(address dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  assert(Patching_lock->is_locked() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
         SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  // set_destination uses set_long_at which does the ICache::invalidate
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  set_destination(dest);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
// Code for unit testing implementation of NativeCall class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
void NativeCall::test() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  CodeBuffer cb("test", 100, 100);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  MacroAssembler* a = new MacroAssembler(&cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  NativeCall  *nc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  uint idx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  int offsets[] = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    0x0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    0xfffffff0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    0x7ffffff0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    0x80000000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    0x20,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    0x4000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  VM_Version::allow_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  a->call( a->pc(), relocInfo::none );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  a->delayed()->nop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  nc = nativeCall_at( cb.code_begin() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  nc->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  nc = nativeCall_overwriting_at( nc->next_instruction_address() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  for (idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    nc->set_destination( cb.code_begin() + offsets[idx] );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    assert(nc->destination() == (cb.code_begin() + offsets[idx]), "check unit test");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    nc->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  nc = nativeCall_before( cb.code_begin() + 8 );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  nc->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  VM_Version::revert();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
// End code for unit testing implementation of NativeCall class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
#ifdef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
void NativeFarCall::set_destination(address dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  // Address materialized in the instruction stream, so nothing to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
#if 0 // What we'd do if we really did want to change the destination
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  if (destination() == dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  CodeBuffer buf(addr_at(0), instruction_size + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  MacroAssembler* _masm = new MacroAssembler(&buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  // Generate the new sequence
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  Address(O7, dest);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  _masm->jumpl_to(dest, O7);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  ICache::invalidate_range(addr_at(0), instruction_size );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
void NativeFarCall::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  // make sure code pattern is actually a jumpl_to instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  assert((int)instruction_size == (int)NativeJump::instruction_size, "same as jump_to");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  assert((int)jmpl_offset == (int)NativeMovConstReg::add_offset, "sethi size ok");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  nativeJump_at(addr_at(0))->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
bool NativeFarCall::is_call_at(address instr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  return nativeInstruction_at(instr)->is_sethi();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
void NativeFarCall::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  tty->print_cr(INTPTR_FORMAT ": call " INTPTR_FORMAT, instruction_address(), destination());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
bool NativeFarCall::destination_is_compiled_verified_entry_point() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  nmethod* callee = CodeCache::find_nmethod(destination());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  if (callee == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
    return destination() == callee->verified_entry_point();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
// MT-safe patching of a far call.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
void NativeFarCall::replace_mt_safe(address instr_addr, address code_buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  Unimplemented();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
// Code for unit testing implementation of NativeFarCall class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
void NativeFarCall::test() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  Unimplemented();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
// End code for unit testing implementation of NativeFarCall class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
#endif // _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
void NativeMovConstReg::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  NativeInstruction::verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  // make sure code pattern is actually a "set_oop" synthetic instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  // see MacroAssembler::set_oop()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  int i0 = long_at(sethi_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  int i1 = long_at(add_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  // verify the pattern "sethi %hi22(imm), reg ;  add reg, %lo10(imm), reg"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  Register rd = inv_rd(i0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
#ifndef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  if (!(is_op2(i0, Assembler::sethi_op2) && rd != G0 &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
        is_op3(i1, Assembler::add_op3, Assembler::arith_op) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
        inv_immed(i1) && (unsigned)get_simm13(i1) < (1 << 10) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
        rd == inv_rs1(i1) && rd == inv_rd(i1))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    fatal("not a set_oop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  if (!is_op2(i0, Assembler::sethi_op2) && rd != G0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    fatal("not a set_oop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
void NativeMovConstReg::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  tty->print_cr(INTPTR_FORMAT ": mov reg, " INTPTR_FORMAT, instruction_address(), data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
#ifdef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
intptr_t NativeMovConstReg::data() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  return data64(addr_at(sethi_offset), long_at(add_offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
intptr_t NativeMovConstReg::data() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  return data32(long_at(sethi_offset), long_at(add_offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
void NativeMovConstReg::set_data(intptr_t x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
#ifdef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  set_data64_sethi(addr_at(sethi_offset), x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  set_long_at(sethi_offset, set_data32_sethi(  long_at(sethi_offset), x));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  set_long_at(add_offset,   set_data32_simm13( long_at(add_offset),   x));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  // also store the value into an oop_Relocation cell, if any
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  CodeBlob* nm = CodeCache::find_blob(instruction_address());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  if (nm != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    RelocIterator iter(nm, instruction_address(), next_instruction_address());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    oop* oop_addr = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    while (iter.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
      if (iter.type() == relocInfo::oop_type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
        oop_Relocation *r = iter.oop_reloc();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
        if (oop_addr == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
          oop_addr = r->oop_addr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
          *oop_addr = (oop)x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
          assert(oop_addr == r->oop_addr(), "must be only one set-oop here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
// Code for unit testing implementation of NativeMovConstReg class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
void NativeMovConstReg::test() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  CodeBuffer cb("test", 100, 100);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  MacroAssembler* a = new MacroAssembler(&cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  NativeMovConstReg* nm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  uint idx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  int offsets[] = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    0x0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    0x7fffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
    0x80000000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    0xffffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    0x20,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    4096,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    4097,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  VM_Version::allow_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  a->sethi(0xccccdddd, O2, true, RelocationHolder::none);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  a->add(O2, low10(0xccccdddd), O2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  nm = nativeMovConstReg_at( cb.code_begin() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  nm = nativeMovConstReg_at( nm->next_instruction_address() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  for (idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
    nm->set_data( offsets[idx] );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    assert(nm->data() == offsets[idx], "check unit test");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  VM_Version::revert();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
// End code for unit testing implementation of NativeMovConstReg class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
void NativeMovConstRegPatching::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  NativeInstruction::verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
  // Make sure code pattern is sethi/nop/add.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
  int i0 = long_at(sethi_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  int i1 = long_at(nop_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  int i2 = long_at(add_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  assert((int)nop_offset == (int)NativeMovConstReg::add_offset, "sethi size ok");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  // Verify the pattern "sethi %hi22(imm), reg; nop; add reg, %lo10(imm), reg"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  // The casual reader should note that on Sparc a nop is a special case if sethi
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  // in which the destination register is %g0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  Register rd0 = inv_rd(i0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  Register rd1 = inv_rd(i1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
  if (!(is_op2(i0, Assembler::sethi_op2) && rd0 != G0 &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
        is_op2(i1, Assembler::sethi_op2) && rd1 == G0 &&        // nop is a special case of sethi
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
        is_op3(i2, Assembler::add_op3, Assembler::arith_op) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
        inv_immed(i2) && (unsigned)get_simm13(i2) < (1 << 10) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
        rd0 == inv_rs1(i2) && rd0 == inv_rd(i2))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    fatal("not a set_oop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
void NativeMovConstRegPatching::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  tty->print_cr(INTPTR_FORMAT ": mov reg, " INTPTR_FORMAT, instruction_address(), data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
int NativeMovConstRegPatching::data() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
#ifdef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  return data64(addr_at(sethi_offset), long_at(add_offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  return data32(long_at(sethi_offset), long_at(add_offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
void NativeMovConstRegPatching::set_data(int x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
#ifdef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  set_data64_sethi(addr_at(sethi_offset), x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  set_long_at(sethi_offset, set_data32_sethi(long_at(sethi_offset), x));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  set_long_at(add_offset, set_data32_simm13(long_at(add_offset), x));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  // also store the value into an oop_Relocation cell, if any
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  CodeBlob* nm = CodeCache::find_blob(instruction_address());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  if (nm != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
    RelocIterator iter(nm, instruction_address(), next_instruction_address());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
    oop* oop_addr = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
    while (iter.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
      if (iter.type() == relocInfo::oop_type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
        oop_Relocation *r = iter.oop_reloc();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
        if (oop_addr == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
          oop_addr = r->oop_addr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
          *oop_addr = (oop)x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
          assert(oop_addr == r->oop_addr(), "must be only one set-oop here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
// Code for unit testing implementation of NativeMovConstRegPatching class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
void NativeMovConstRegPatching::test() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  CodeBuffer cb("test", 100, 100);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  MacroAssembler* a = new MacroAssembler(&cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
  NativeMovConstRegPatching* nm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  uint idx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  int offsets[] = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
    0x0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
    0x7fffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
    0x80000000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
    0xffffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
    0x20,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
    4096,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
    4097,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  VM_Version::allow_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  a->nop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  a->sethi(0xccccdddd, O2, true, RelocationHolder::none);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  a->nop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  a->add(O2, low10(0xccccdddd), O2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  nm = nativeMovConstRegPatching_at( cb.code_begin() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  nm = nativeMovConstRegPatching_at( nm->next_instruction_address() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  for (idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
    nm->set_data( offsets[idx] );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
    assert(nm->data() == offsets[idx], "check unit test");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  VM_Version::revert();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
// End code for unit testing implementation of NativeMovConstRegPatching class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
void NativeMovRegMem::copy_instruction_to(address new_instruction_address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  Untested("copy_instruction_to");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  int instruction_size = next_instruction_address() - instruction_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  for (int i = 0; i < instruction_size; i += BytesPerInstWord) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
    *(int*)(new_instruction_address + i) = *(int*)(address(this) + i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
void NativeMovRegMem::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  NativeInstruction::verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
  // make sure code pattern is actually a "ld" or "st" of some sort.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  int i0 = long_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  int op3 = inv_op3(i0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  assert((int)add_offset == NativeMovConstReg::add_offset, "sethi size ok");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  if (!(is_op(i0, Assembler::ldst_op) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
        inv_immed(i0) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
        0 != (op3 < op3_ldst_int_limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
         ? (1 <<  op3                      ) & (op3_mask_ld  | op3_mask_st)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
         : (1 << (op3 - op3_ldst_int_limit)) & (op3_mask_ldf | op3_mask_stf))))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
    int i1 = long_at(ldst_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
    Register rd = inv_rd(i0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
    op3 = inv_op3(i1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
    if (!is_op(i1, Assembler::ldst_op) && rd == inv_rs2(i1) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
         0 != (op3 < op3_ldst_int_limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
              ? (1 <<  op3                      ) & (op3_mask_ld  | op3_mask_st)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
               : (1 << (op3 - op3_ldst_int_limit)) & (op3_mask_ldf | op3_mask_stf))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
      fatal("not a ld* or st* op");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
void NativeMovRegMem::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  if (is_immediate()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
    tty->print_cr(INTPTR_FORMAT ": mov reg, [reg + %x]", instruction_address(), offset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
    tty->print_cr(INTPTR_FORMAT ": mov reg, [reg + reg]", instruction_address());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
// Code for unit testing implementation of NativeMovRegMem class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
void NativeMovRegMem::test() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
  CodeBuffer cb("test", 1000, 1000);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
  MacroAssembler* a = new MacroAssembler(&cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
  NativeMovRegMem* nm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
  uint idx = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
  uint idx1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
  int offsets[] = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
    0x0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
    0xffffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
    0x7fffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
    0x80000000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
    4096,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
    4097,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
    0x20,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
    0x4000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
  VM_Version::allow_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
  a->ldsw( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
  a->ldsw( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
  a->ldsb( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
  a->ldsb( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
  a->ldsh( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
  a->ldsh( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
  a->lduw( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
  a->lduw( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
  a->ldub( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  a->ldub( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
  a->lduh( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
  a->lduh( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
  a->ldx( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
  a->ldx( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
  a->ldd( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
  a->ldd( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
  a->ldf( FloatRegisterImpl::D, O2, -1, F14 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
  a->ldf( FloatRegisterImpl::S, O0, I3, F15 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
  a->stw( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  a->stw( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
  a->stb( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
  a->stb( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
  a->sth( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
  a->sth( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
  a->stx( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
  a->stx( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
  a->std( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
  a->std( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
  a->stf( FloatRegisterImpl::S, F18, O2, -1 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
  a->stf( FloatRegisterImpl::S, F15, O0, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
  nm = nativeMovRegMem_at( cb.code_begin() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
  nm->set_offset( low10(0) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
  nm->add_offset_in_bytes( low10(0xbb) * wordSize );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
  while (--idx) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
    nm = nativeMovRegMem_at( nm->next_instruction_address() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
    nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
    for (idx1 = 0; idx1 < ARRAY_SIZE(offsets); idx1++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
      nm->set_offset( nm->is_immediate() ? low10(offsets[idx1]) : offsets[idx1] );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
      assert(nm->offset() == (nm->is_immediate() ? low10(offsets[idx1]) : offsets[idx1]),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
             "check unit test");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
      nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
    nm->add_offset_in_bytes( low10(0xbb) * wordSize );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
    nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
  VM_Version::revert();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
// End code for unit testing implementation of NativeMovRegMem class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
//--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
void NativeMovRegMemPatching::copy_instruction_to(address new_instruction_address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
  Untested("copy_instruction_to");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  int instruction_size = next_instruction_address() - instruction_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
  for (int i = 0; i < instruction_size; i += wordSize) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
    *(long*)(new_instruction_address + i) = *(long*)(address(this) + i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
void NativeMovRegMemPatching::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
  NativeInstruction::verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
  // make sure code pattern is actually a "ld" or "st" of some sort.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
  int i0 = long_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
  int op3 = inv_op3(i0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
  assert((int)nop_offset == (int)NativeMovConstReg::add_offset, "sethi size ok");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
  if (!(is_op(i0, Assembler::ldst_op) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
        inv_immed(i0) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
        0 != (op3 < op3_ldst_int_limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
         ? (1 <<  op3                      ) & (op3_mask_ld  | op3_mask_st)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
         : (1 << (op3 - op3_ldst_int_limit)) & (op3_mask_ldf | op3_mask_stf)))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
    int i1 = long_at(ldst_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
    Register rd = inv_rd(i0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
    op3 = inv_op3(i1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
    if (!is_op(i1, Assembler::ldst_op) && rd == inv_rs2(i1) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
         0 != (op3 < op3_ldst_int_limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
              ? (1 <<  op3                      ) & (op3_mask_ld  | op3_mask_st)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
              : (1 << (op3 - op3_ldst_int_limit)) & (op3_mask_ldf | op3_mask_stf))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
      fatal("not a ld* or st* op");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
void NativeMovRegMemPatching::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
  if (is_immediate()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
    tty->print_cr(INTPTR_FORMAT ": mov reg, [reg + %x]", instruction_address(), offset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
    tty->print_cr(INTPTR_FORMAT ": mov reg, [reg + reg]", instruction_address());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
// Code for unit testing implementation of NativeMovRegMemPatching class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
void NativeMovRegMemPatching::test() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
  CodeBuffer cb("test", 1000, 1000);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
  MacroAssembler* a = new MacroAssembler(&cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
  NativeMovRegMemPatching* nm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
  uint idx = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
  uint idx1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
  int offsets[] = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
    0x0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
    0xffffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
    0x7fffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
    0x80000000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
    4096,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
    4097,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
    0x20,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
    0x4000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
  VM_Version::allow_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
  a->ldsw( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
  a->ldsw( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
  a->ldsb( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
  a->ldsb( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
  a->ldsh( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
  a->ldsh( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
  a->lduw( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
  a->lduw( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
  a->ldub( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
  a->ldub( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
  a->lduh( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
  a->lduh( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
  a->ldx( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
  a->ldx( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
  a->ldd( G5, low10(0xffffffff), G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
  a->ldd( G5, I3, G4 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
  a->ldf( FloatRegisterImpl::D, O2, -1, F14 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
  a->ldf( FloatRegisterImpl::S, O0, I3, F15 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
  a->stw( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
  a->stw( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
  a->stb( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
  a->stb( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
  a->sth( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
  a->sth( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
  a->stx( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
  a->stx( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
  a->std( G5, G4, low10(0xffffffff) ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
  a->std( G5, G4, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
  a->stf( FloatRegisterImpl::S, F18, O2, -1 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
  a->sethi(0xaaaabbbb, I3, true, RelocationHolder::none); a->nop(); a->add(I3, low10(0xaaaabbbb), I3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
  a->stf( FloatRegisterImpl::S, F15, O0, I3 ); idx++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
  nm = nativeMovRegMemPatching_at( cb.code_begin() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
  nm->set_offset( low10(0) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
  nm->add_offset_in_bytes( low10(0xbb) * wordSize );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
  nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
  while (--idx) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
    nm = nativeMovRegMemPatching_at( nm->next_instruction_address() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
    nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
    for (idx1 = 0; idx1 < ARRAY_SIZE(offsets); idx1++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
      nm->set_offset( nm->is_immediate() ? low10(offsets[idx1]) : offsets[idx1] );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
      assert(nm->offset() == (nm->is_immediate() ? low10(offsets[idx1]) : offsets[idx1]),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
             "check unit test");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
      nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
    nm->add_offset_in_bytes( low10(0xbb) * wordSize );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
    nm->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
  VM_Version::revert();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
// End code for unit testing implementation of NativeMovRegMemPatching class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
//--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
void NativeJump::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
  NativeInstruction::verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
  int i0 = long_at(sethi_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
  int i1 = long_at(jmpl_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
  assert((int)jmpl_offset == (int)NativeMovConstReg::add_offset, "sethi size ok");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
  // verify the pattern "sethi %hi22(imm), treg ;  jmpl treg, %lo10(imm), lreg"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
  Register rd = inv_rd(i0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
#ifndef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
  if (!(is_op2(i0, Assembler::sethi_op2) && rd != G0 &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
        (is_op3(i1, Assembler::jmpl_op3, Assembler::arith_op) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
        (TraceJumps && is_op3(i1, Assembler::add_op3, Assembler::arith_op))) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
        inv_immed(i1) && (unsigned)get_simm13(i1) < (1 << 10) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
        rd == inv_rs1(i1))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
    fatal("not a jump_to instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
  // In LP64, the jump instruction location varies for non relocatable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
  // jumps, for example is could be sethi, xor, jmp instead of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
  // 7 instructions for sethi.  So let's check sethi only.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
  if (!is_op2(i0, Assembler::sethi_op2) && rd != G0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
    fatal("not a jump_to instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
void NativeJump::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
  tty->print_cr(INTPTR_FORMAT ": jmpl reg, " INTPTR_FORMAT, instruction_address(), jump_destination());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
// Code for unit testing implementation of NativeJump class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
void NativeJump::test() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
  CodeBuffer cb("test", 100, 100);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
  MacroAssembler* a = new MacroAssembler(&cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
  NativeJump* nj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
  uint idx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
  int offsets[] = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
    0x0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
    0xffffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
    0x7fffffff,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
    0x80000000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
    4096,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
    4097,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
    0x20,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
    0x4000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
  VM_Version::allow_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
  a->sethi(0x7fffbbbb, I3, true, RelocationHolder::none);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
  a->jmpl(I3, low10(0x7fffbbbb), G0, RelocationHolder::none);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
  a->delayed()->nop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
  a->sethi(0x7fffbbbb, I3, true, RelocationHolder::none);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
  a->jmpl(I3, low10(0x7fffbbbb), L3, RelocationHolder::none);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
  a->delayed()->nop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
  nj = nativeJump_at( cb.code_begin() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
  nj->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
  nj = nativeJump_at( nj->next_instruction_address() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
  for (idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
    nj->set_jump_destination( nj->instruction_address() + offsets[idx] );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
    assert(nj->jump_destination() == (nj->instruction_address() + offsets[idx]), "check unit test");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
    nj->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
  VM_Version::revert();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
// End code for unit testing implementation of NativeJump class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
void NativeJump::insert(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
  Unimplemented();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
// MT safe inserting of a jump over an unknown instruction sequence (used by nmethod::makeZombie)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
// The problem: jump_to <dest> is a 3-word instruction (including its delay slot).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
// Atomic write can be only with 1 word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
void NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
  // Here's one way to do it:  Pre-allocate a three-word jump sequence somewhere
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
  // in the header of the nmethod, within a short branch's span of the patch point.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
  // Set up the jump sequence using NativeJump::insert, and then use an annulled
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
  // unconditional branch at the target site (an atomic 1-word update).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
  // Limitations:  You can only patch nmethods, with any given nmethod patched at
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
  // most once, and the patch must be in the nmethod's header.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
  // It's messy, but you can ask the CodeCache for the nmethod containing the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
  // target address.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
  // %%%%% For now, do something MT-stupid:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
  int code_size = 1 * BytesPerInstWord;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
  CodeBuffer cb(verified_entry, code_size + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
  MacroAssembler* a = new MacroAssembler(&cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
  if (VM_Version::v9_instructions_work()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
    a->ldsw(G0, 0, O7); // "ld" must agree with code in the signal handler
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
    a->lduw(G0, 0, O7); // "ld" must agree with code in the signal handler
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
  ICache::invalidate_range(verified_entry, code_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
void NativeIllegalInstruction::insert(address code_pos) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
  NativeIllegalInstruction* nii = (NativeIllegalInstruction*) nativeInstruction_at(code_pos);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
  nii->set_long_at(0, illegal_instruction());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
static int illegal_instruction_bits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
int NativeInstruction::illegal_instruction() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
  if (illegal_instruction_bits == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
    ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
    char buf[40];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
    CodeBuffer cbuf((address)&buf[0], 20);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
    MacroAssembler* a = new MacroAssembler(&cbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
    address ia = a->pc();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
    a->trap(ST_RESERVED_FOR_USER_0 + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
    int bits = *(int*)ia;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
    assert(is_op3(bits, Assembler::trap_op3, Assembler::arith_op), "bad instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
    illegal_instruction_bits = bits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
    assert(illegal_instruction_bits != 0, "oops");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
  return illegal_instruction_bits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
static int ic_miss_trap_bits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
bool NativeInstruction::is_ic_miss_trap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
  if (ic_miss_trap_bits == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
    ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
    char buf[40];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
    CodeBuffer cbuf((address)&buf[0], 20);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
    MacroAssembler* a = new MacroAssembler(&cbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
    address ia = a->pc();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
    a->trap(Assembler::notEqual, Assembler::ptr_cc, G0, ST_RESERVED_FOR_USER_0 + 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
    int bits = *(int*)ia;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
    assert(is_op3(bits, Assembler::trap_op3, Assembler::arith_op), "bad instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
    ic_miss_trap_bits = bits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
    assert(ic_miss_trap_bits != 0, "oops");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
  return long_at(0) == ic_miss_trap_bits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
bool NativeInstruction::is_illegal() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
  if (illegal_instruction_bits == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
  return long_at(0) == illegal_instruction_bits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
void NativeGeneralJump::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
  assert(((NativeInstruction *)this)->is_jump() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
         ((NativeInstruction *)this)->is_cond_jump(), "not a general jump instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
void NativeGeneralJump::insert_unconditional(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
  Assembler::Condition condition = Assembler::always;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
  int x = Assembler::op2(Assembler::br_op2) | Assembler::annul(false) |
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
    Assembler::cond(condition) | Assembler::wdisp((intptr_t)entry, (intptr_t)code_pos, 22);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
  NativeGeneralJump* ni = (NativeGeneralJump*) nativeInstruction_at(code_pos);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
  ni->set_long_at(0, x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
// MT-safe patching of a jmp instruction (and following word).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
// First patches the second word, and then atomicly replaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
// the first word with the first new instruction word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
// Other processors might briefly see the old first word
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
// followed by the new second word.  This is OK if the old
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
// second word is harmless, and the new second word may be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
// harmlessly executed in the delay slot of the call.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
void NativeGeneralJump::replace_mt_safe(address instr_addr, address code_buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
   assert(Patching_lock->is_locked() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
         SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
   assert (instr_addr != NULL, "illegal address for code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
   NativeGeneralJump* h_jump =  nativeGeneralJump_at (instr_addr); // checking that it is a call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
   assert(NativeGeneralJump::instruction_size == 8, "wrong instruction size; must be 8");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
   int i0 = ((int*)code_buffer)[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
   int i1 = ((int*)code_buffer)[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
   int* contention_addr = (int*) h_jump->addr_at(1*BytesPerInstWord);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
   assert(inv_op(*contention_addr) == Assembler::arith_op ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
          *contention_addr == nop_instruction() || !VM_Version::v9_instructions_work(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
          "must not interfere with original call");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
   // The set_long_at calls do the ICacheInvalidate so we just need to do them in reverse order
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
   h_jump->set_long_at(1*BytesPerInstWord, i1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
   h_jump->set_long_at(0*BytesPerInstWord, i0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
   // NOTE:  It is possible that another thread T will execute
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
   // only the second patched word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
   // In other words, since the original instruction is this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
   //    jmp patching_stub; nop                    (NativeGeneralJump)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
   // and the new sequence from the buffer is this:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
   //    sethi %hi(K), %r; add %r, %lo(K), %r      (NativeMovConstReg)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
   // what T will execute is this:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
   //    jmp patching_stub; add %r, %lo(K), %r
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
   // thereby putting garbage into %r before calling the patching stub.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
   // This is OK, because the patching stub ignores the value of %r.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
   // Make sure the first-patched instruction, which may co-exist
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
   // briefly with the call, will do something harmless.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
   assert(inv_op(*contention_addr) == Assembler::arith_op ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
          *contention_addr == nop_instruction() || !VM_Version::v9_instructions_work(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
          "must not interfere with original call");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
}