hotspot/src/cpu/x86/vm/nativeInst_x86.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_x86.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
void NativeInstruction::wrote(int offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
  ICache::invalidate_word(addr_at(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
void NativeCall::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  // Make sure code pattern is actually a call imm32 instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  int inst = ubyte_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  if (inst != instruction_code) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", instruction_address(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
                                                        inst);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    fatal("not a call disp32");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
address NativeCall::destination() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  // Getting the destination of a call isn't safe because that call can
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  // be getting patched while you're calling this.  There's only special
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // places where this can be called but not automatically verifiable by
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  // checking which locks are held.  The solution is true atomic patching
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  // on x86, nyi.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  return return_address() + displacement();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
void NativeCall::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  tty->print_cr(PTR_FORMAT ": call " PTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
                instruction_address(), destination());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
// Inserts a native call instruction at a given pc
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
void NativeCall::insert(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  *code_pos = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  *((int32_t *)(code_pos+1)) = (int32_t) disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
// MT-safe patching of a call instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
// First patches first word of instruction to two jmp's that jmps to them
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
// selfs (spinlock). Then patches the last byte, and then atomicly replaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
// the jmp's with the first 4 byte of the new instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
void NativeCall::replace_mt_safe(address instr_addr, address code_buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  assert(Patching_lock->is_locked() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
         SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  assert (instr_addr != NULL, "illegal address for code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  NativeCall* n_call =  nativeCall_at (instr_addr); // checking that it is a call
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  if (os::is_MP()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    guarantee((intptr_t)instr_addr % BytesPerWord == 0, "must be aligned");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  unsigned char patch[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  assert(sizeof(patch)==sizeof(jint), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  patch[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  patch[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  patch[2] = 0xEB;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  patch[3] = 0xFE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  *(jint*)instr_addr = *(jint *)patch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  n_call->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  // Patch 4th byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  instr_addr[4] = code_buffer[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  n_call->wrote(4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // Patch bytes 0-3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  *(jint*)instr_addr = *(jint *)code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  n_call->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
   // verify patching
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
   for ( int i = 0; i < instruction_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
     address ptr = (address)((intptr_t)code_buffer + i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
     int a_byte = (*ptr) & 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
     assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
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
// Similar to replace_mt_safe, but just changes the destination.  The
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
// important thing is that free-running threads are able to execute this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
// call instruction at all times.  If the displacement field is aligned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
// we can simply rely on atomicity of 32-bit writes to make sure other threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
// will see no intermediate states.  Otherwise, the first two bytes of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
// call are guaranteed to be aligned, and can be atomically patched to a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
// self-loop to guard the instruction while we change the other bytes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
// We cannot rely on locks here, since the free-running threads must run at
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
// full speed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
// Used in the runtime linkage of calls; see class CompiledIC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
// (Cf. 4506997 and 4479829, where threads witnessed garbage displacements.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
void NativeCall::set_destination_mt_safe(address dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  debug_only(verify());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  // Make sure patching code is locked.  No two threads can patch at the same
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  // time but one may be executing this code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  assert(Patching_lock->is_locked() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
         SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  // Both C1 and C2 should now be generating code which aligns the patched address
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  // to be within a single cache line except that C1 does not do the alignment on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  // uniprocessor systems.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  bool is_aligned = ((uintptr_t)displacement_address() + 0) / cache_line_size ==
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
                    ((uintptr_t)displacement_address() + 3) / cache_line_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  guarantee(!os::is_MP() || is_aligned, "destination must be aligned");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  if (is_aligned) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    // Simple case:  The destination lies within a single cache line.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    set_destination(dest);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  } else if ((uintptr_t)instruction_address() / cache_line_size ==
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
             ((uintptr_t)instruction_address()+1) / cache_line_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    // Tricky case:  The instruction prefix lies within a single cache line.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    intptr_t disp = dest - return_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    int call_opcode = instruction_address()[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    // First patch dummy jump in place:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
      u_char patch_jump[2];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      patch_jump[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      patch_jump[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      assert(sizeof(patch_jump)==sizeof(short), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      *(short*)instruction_address() = *(short*)patch_jump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    // (Note: We assume any reader which has already started to read
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    // the unpatched call will completely read the whole unpatched call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    // without seeing the next writes we are about to make.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    // Next, patch the last three bytes:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    u_char patch_disp[5];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    patch_disp[0] = call_opcode;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    *(int32_t*)&patch_disp[1] = (int32_t)disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    assert(sizeof(patch_disp)==instruction_size, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    for (int i = sizeof(short); i < instruction_size; i++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
      instruction_address()[i] = patch_disp[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    wrote(sizeof(short));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    // (Note: We assume that any reader which reads the opcode we are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    // about to repatch will also read the writes we just made.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    // Finally, overwrite the jump:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    *(short*)instruction_address() = *(short*)patch_disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    debug_only(verify());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    guarantee(destination() == dest, "patch succeeded");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    // Impossible:  One or the other must be atomically writable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
void NativeMovConstReg::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  // make sure code pattern is actually a mov reg64, imm64 instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  if ((ubyte_at(0) != Assembler::REX_W && ubyte_at(0) != Assembler::REX_WB) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
      (ubyte_at(1) & (0xff ^ register_mask)) != 0xB8) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    fatal("not a REX.W[B] mov reg64, imm64");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  // make sure code pattern is actually a mov reg, imm32 instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  u_char test_byte = *(u_char*)instruction_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  u_char test_byte_2 = test_byte & ( 0xff ^ register_mask);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  if (test_byte_2 != instruction_code) fatal("not a mov reg, imm32");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
void NativeMovConstReg::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  tty->print_cr(PTR_FORMAT ": mov reg, " INTPTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
                instruction_address(), data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
#ifndef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
void NativeMovRegMem::copy_instruction_to(address new_instruction_address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  int inst_size = instruction_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  // See if there's an instruction size prefix override.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  if ( *(address(this))   == instruction_operandsize_prefix &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
       *(address(this)+1) != instruction_code_xmm_code ) { // Not SSE instr
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    inst_size += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  if ( *(address(this)) == instruction_extended_prefix ) inst_size += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  for (int i = 0; i < instruction_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    *(new_instruction_address + i) = *(address(this) + i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
void NativeMovRegMem::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  // make sure code pattern is actually a mov [reg+offset], reg instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  u_char test_byte = *(u_char*)instruction_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  if ( ! ( (test_byte == instruction_code_reg2memb)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
      || (test_byte == instruction_code_mem2regb)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
      || (test_byte == instruction_code_mem2regl)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
      || (test_byte == instruction_code_reg2meml)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      || (test_byte == instruction_code_mem2reg_movzxb )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      || (test_byte == instruction_code_mem2reg_movzxw )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
      || (test_byte == instruction_code_mem2reg_movsxb )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
      || (test_byte == instruction_code_mem2reg_movsxw )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
      || (test_byte == instruction_code_float_s)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
      || (test_byte == instruction_code_float_d)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      || (test_byte == instruction_code_long_volatile) ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    u_char byte1 = ((u_char*)instruction_address())[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    u_char byte2 = ((u_char*)instruction_address())[2];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    if ((test_byte != instruction_code_xmm_ss_prefix &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
         test_byte != instruction_code_xmm_sd_prefix &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
         test_byte != instruction_operandsize_prefix) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
        byte1 != instruction_code_xmm_code ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
        (byte2 != instruction_code_xmm_load &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
         byte2 != instruction_code_xmm_lpd  &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
         byte2 != instruction_code_xmm_store)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
          fatal ("not a mov [reg+offs], reg instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
void NativeMovRegMem::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  tty->print_cr("0x%x: mov reg, [reg + %x]", instruction_address(), offset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
void NativeLoadAddress::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  // make sure code pattern is actually a mov [reg+offset], reg instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  u_char test_byte = *(u_char*)instruction_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  if ( ! (test_byte == instruction_code) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    fatal ("not a lea reg, [reg+offs] instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
void NativeLoadAddress::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  tty->print_cr("0x%x: lea [reg + %x], reg", instruction_address(), offset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
#endif // !AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
//--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
void NativeJump::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  if (*(u_char*)instruction_address() != instruction_code) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    fatal("not a jump instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
void NativeJump::insert(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  *code_pos = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  *((int32_t*)(code_pos + 1)) = (int32_t)disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
void NativeJump::check_verified_entry_alignment(address entry, address verified_entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  // Patching to not_entrant can happen while activations of the method are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  // in use. The patching in that instance must happen only when certain
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  // alignment restrictions are true. These guarantees check those
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  // conditions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  const int linesize = 64;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  const int linesize = 32;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  // Must be wordSize aligned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  guarantee(((uintptr_t) verified_entry & (wordSize -1)) == 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
            "illegal address for code patching 2");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  // First 5 bytes must be within the same cache line - 4827828
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  guarantee((uintptr_t) verified_entry / linesize ==
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
            ((uintptr_t) verified_entry + 4) / linesize,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
            "illegal address for code patching 3");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
// MT safe inserting of a jump over an unknown instruction sequence (used by nmethod::makeZombie)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
// The problem: jmp <dest> is a 5-byte instruction. Atomical write can be only with 4 bytes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
// First patches the first word atomically to be a jump to itself.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
// Then patches the last byte  and then atomically patches the first word (4-bytes),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
// thus inserting the desired jump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
// This code is mt-safe with the following conditions: entry point is 4 byte aligned,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
// entry point is in same cache line as unverified entry point, and the instruction being
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
// patched is >= 5 byte (size of patch).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
// In C2 the 5+ byte sized instruction is enforced by code in MachPrologNode::emit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
// In C1 the restriction is enforced by CodeEmitter::method_entry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
void NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  // complete jump instruction (to be inserted) is in code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  unsigned char code_buffer[5];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  code_buffer[0] = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  intptr_t disp = (intptr_t)dest - ((intptr_t)verified_entry + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  *(int32_t*)(code_buffer + 1) = (int32_t)disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  check_verified_entry_alignment(entry, verified_entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  // Can't call nativeJump_at() because it's asserts jump exists
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  NativeJump* n_jump = (NativeJump*) verified_entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
  //First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  unsigned char patch[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  assert(sizeof(patch)==sizeof(int32_t), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  patch[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  patch[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  patch[2] = 0xEB;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  patch[3] = 0xFE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  *(int32_t*)verified_entry = *(int32_t *)patch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  // Patch 5th byte (from jump instruction)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  verified_entry[4] = code_buffer[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
  n_jump->wrote(4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  // Patch bytes 0-3 (from jump instruction)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  *(int32_t*)verified_entry = *(int32_t *)code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
  // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
  n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
void NativePopReg::insert(address code_pos, Register reg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  assert(reg->encoding() < 8, "no space for REX");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  assert(NativePopReg::instruction_size == sizeof(char), "right address unit for update");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  *code_pos = (u_char)(instruction_code | reg->encoding());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
void NativeIllegalInstruction::insert(address code_pos) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
  *(short *)code_pos = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
void NativeGeneralJump::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  assert(((NativeInstruction *)this)->is_jump() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
         ((NativeInstruction *)this)->is_cond_jump(), "not a general jump instruction");
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 NativeGeneralJump::insert_unconditional(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  *code_pos = unconditional_long_jump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  *((int32_t *)(code_pos+1)) = (int32_t) disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
// MT-safe patching of a long jump instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
// First patches first word of instruction to two jmp's that jmps to them
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
// selfs (spinlock). Then patches the last byte, and then atomicly replaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
// the jmp's with the first 4 byte of the new instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
void NativeGeneralJump::replace_mt_safe(address instr_addr, address code_buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
   assert (instr_addr != NULL, "illegal address for code patching (4)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
   NativeGeneralJump* n_jump =  nativeGeneralJump_at (instr_addr); // checking that it is a jump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
   // Temporary code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
   unsigned char patch[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
   assert(sizeof(patch)==sizeof(int32_t), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
   patch[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
   patch[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
   patch[2] = 0xEB;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
   patch[3] = 0xFE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
   // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
   *(int32_t*)instr_addr = *(int32_t *)patch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
    n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
   // Patch 4th byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
   instr_addr[4] = code_buffer[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
    n_jump->wrote(4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
   // Patch bytes 0-3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
   *(jint*)instr_addr = *(jint *)code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
    n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
   // verify patching
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
   for ( int i = 0; i < instruction_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
     address ptr = (address)((intptr_t)code_buffer + i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
     int a_byte = (*ptr) & 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
     assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
address NativeGeneralJump::jump_destination() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  int op_code = ubyte_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
  bool is_rel32off = (op_code == 0xE9 || op_code == 0x0F);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  int  offset  = (op_code == 0x0F)  ? 2 : 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  int  length  = offset + ((is_rel32off) ? 4 : 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  if (is_rel32off)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
    return addr_at(0) + length + int_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
    return addr_at(0) + length + sbyte_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
}
363
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
   475
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
   476
bool NativeInstruction::is_dtrace_trap() {
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
   477
  return (*(int32_t*)this & 0xff) == 0xcc;
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
   478
}