hotspot/src/cpu/x86/vm/nativeInst_x86.cpp
author kvn
Wed, 24 Oct 2012 14:33:22 -0700
changeset 14132 3c1437abcefd
parent 11427 bf248009cbbe
child 14626 0cf4eccf130f
permissions -rw-r--r--
7184394: add intrinsics to use AES instructions Summary: Use new x86 AES instructions for AESCrypt. Reviewed-by: twisti, kvn, roland Contributed-by: tom.deneau@amd.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
1
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
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1066
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1066
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1066
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "assembler_x86.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "memory/resourceArea.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "nativeInst_x86.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "runtime/handles.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#include "runtime/sharedRuntime.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
#include "runtime/stubRoutines.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    33
#include "utilities/ostream.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    34
#ifdef COMPILER1
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    35
#include "c1/c1_Runtime1.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    36
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
void NativeInstruction::wrote(int offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  ICache::invalidate_word(addr_at(offset));
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
void NativeCall::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  // Make sure code pattern is actually a call imm32 instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  int inst = ubyte_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  if (inst != instruction_code) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", instruction_address(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
                                                        inst);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    fatal("not a call disp32");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
address NativeCall::destination() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  // Getting the destination of a call isn't safe because that call can
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  // be getting patched while you're calling this.  There's only special
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  // places where this can be called but not automatically verifiable by
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  // checking which locks are held.  The solution is true atomic patching
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  // on x86, nyi.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  return return_address() + displacement();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
void NativeCall::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  tty->print_cr(PTR_FORMAT ": call " PTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
                instruction_address(), destination());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
// Inserts a native call instruction at a given pc
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
void NativeCall::insert(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  *code_pos = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  *((int32_t *)(code_pos+1)) = (int32_t) disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
// MT-safe patching of a call instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
// First patches first word of instruction to two jmp's that jmps to them
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
// selfs (spinlock). Then patches the last byte, and then atomicly replaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
// the jmp's with the first 4 byte of the new instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
void NativeCall::replace_mt_safe(address instr_addr, address code_buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  assert(Patching_lock->is_locked() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
         SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  assert (instr_addr != NULL, "illegal address for code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  NativeCall* n_call =  nativeCall_at (instr_addr); // checking that it is a call
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  if (os::is_MP()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    guarantee((intptr_t)instr_addr % BytesPerWord == 0, "must be aligned");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  unsigned char patch[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  assert(sizeof(patch)==sizeof(jint), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  patch[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  patch[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  patch[2] = 0xEB;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  patch[3] = 0xFE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  *(jint*)instr_addr = *(jint *)patch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // Invalidate.  Opteron requires a flush after every write.
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
  // Patch 4th byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  instr_addr[4] = code_buffer[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  n_call->wrote(4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  // Patch bytes 0-3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  *(jint*)instr_addr = *(jint *)code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  n_call->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
   // verify patching
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
   for ( int i = 0; i < instruction_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
     address ptr = (address)((intptr_t)code_buffer + i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
     int a_byte = (*ptr) & 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
     assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
// Similar to replace_mt_safe, but just changes the destination.  The
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
// important thing is that free-running threads are able to execute this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
// call instruction at all times.  If the displacement field is aligned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
// we can simply rely on atomicity of 32-bit writes to make sure other threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
// will see no intermediate states.  Otherwise, the first two bytes of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
// call are guaranteed to be aligned, and can be atomically patched to a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
// self-loop to guard the instruction while we change the other bytes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
// We cannot rely on locks here, since the free-running threads must run at
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
// full speed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
// Used in the runtime linkage of calls; see class CompiledIC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
// (Cf. 4506997 and 4479829, where threads witnessed garbage displacements.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
void NativeCall::set_destination_mt_safe(address dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  debug_only(verify());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  // Make sure patching code is locked.  No two threads can patch at the same
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  // time but one may be executing this code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  assert(Patching_lock->is_locked() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
         SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  // Both C1 and C2 should now be generating code which aligns the patched address
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  // to be within a single cache line except that C1 does not do the alignment on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  // uniprocessor systems.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  bool is_aligned = ((uintptr_t)displacement_address() + 0) / cache_line_size ==
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
                    ((uintptr_t)displacement_address() + 3) / cache_line_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  guarantee(!os::is_MP() || is_aligned, "destination must be aligned");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  if (is_aligned) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    // Simple case:  The destination lies within a single cache line.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    set_destination(dest);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  } else if ((uintptr_t)instruction_address() / cache_line_size ==
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
             ((uintptr_t)instruction_address()+1) / cache_line_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    // Tricky case:  The instruction prefix lies within a single cache line.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    intptr_t disp = dest - return_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    int call_opcode = instruction_address()[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    // First patch dummy jump in place:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      u_char patch_jump[2];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
      patch_jump[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      patch_jump[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      assert(sizeof(patch_jump)==sizeof(short), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
      *(short*)instruction_address() = *(short*)patch_jump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    // (Note: We assume any reader which has already started to read
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    // the unpatched call will completely read the whole unpatched call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    // without seeing the next writes we are about to make.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    // Next, patch the last three bytes:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    u_char patch_disp[5];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    patch_disp[0] = call_opcode;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    *(int32_t*)&patch_disp[1] = (int32_t)disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    assert(sizeof(patch_disp)==instruction_size, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    for (int i = sizeof(short); i < instruction_size; i++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
      instruction_address()[i] = patch_disp[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    wrote(sizeof(short));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    // (Note: We assume that any reader which reads the opcode we are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    // about to repatch will also read the writes we just made.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    // Finally, overwrite the jump:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    *(short*)instruction_address() = *(short*)patch_disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    debug_only(verify());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    guarantee(destination() == dest, "patch succeeded");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    // Impossible:  One or the other must be atomically writable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
void NativeMovConstReg::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  // make sure code pattern is actually a mov reg64, imm64 instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  if ((ubyte_at(0) != Assembler::REX_W && ubyte_at(0) != Assembler::REX_WB) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
      (ubyte_at(1) & (0xff ^ register_mask)) != 0xB8) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    fatal("not a REX.W[B] mov reg64, imm64");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  // make sure code pattern is actually a mov reg, imm32 instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  u_char test_byte = *(u_char*)instruction_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  u_char test_byte_2 = test_byte & ( 0xff ^ register_mask);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  if (test_byte_2 != instruction_code) fatal("not a mov reg, imm32");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
void NativeMovConstReg::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  tty->print_cr(PTR_FORMAT ": mov reg, " INTPTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
                instruction_address(), data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   236
int NativeMovRegMem::instruction_start() const {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   237
  int off = 0;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   238
  u_char instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   239
11427
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   240
  // See comment in Assembler::locate_operand() about VEX prefixes.
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   241
  if (instr_0 == instruction_VEX_prefix_2bytes) {
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   242
    assert((UseAVX > 0), "shouldn't have VEX prefix");
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   243
    NOT_LP64(assert((0xC0 & ubyte_at(1)) == 0xC0, "shouldn't have LDS and LES instructions"));
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   244
    return 2;
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   245
  }
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   246
  if (instr_0 == instruction_VEX_prefix_3bytes) {
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   247
    assert((UseAVX > 0), "shouldn't have VEX prefix");
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   248
    NOT_LP64(assert((0xC0 & ubyte_at(1)) == 0xC0, "shouldn't have LDS and LES instructions"));
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   249
    return 3;
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   250
  }
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   251
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   252
  // First check to see if we have a (prefixed or not) xor
11427
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   253
  if (instr_0 >= instruction_prefix_wide_lo && // 0x40
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   254
      instr_0 <= instruction_prefix_wide_hi) { // 0x4f
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   255
    off++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   256
    instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   257
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   258
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   259
  if (instr_0 == instruction_code_xor) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   260
    off += 2;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   261
    instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   262
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   263
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   264
  // Now look for the real instruction and the many prefix/size specifiers.
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   265
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   266
  if (instr_0 == instruction_operandsize_prefix ) {  // 0x66
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   267
    off++; // Not SSE instructions
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   268
    instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   269
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
11427
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   271
  if ( instr_0 == instruction_code_xmm_ss_prefix || // 0xf3
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   272
       instr_0 == instruction_code_xmm_sd_prefix) { // 0xf2
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   273
    off++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   274
    instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   275
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   276
11427
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   277
  if ( instr_0 >= instruction_prefix_wide_lo && // 0x40
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   278
       instr_0 <= instruction_prefix_wide_hi) { // 0x4f
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   279
    off++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   280
    instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   281
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   282
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   283
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   284
  if (instr_0 == instruction_extended_prefix ) {  // 0x0f
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   285
    off++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   286
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   287
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   288
  return off;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   289
}
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   290
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   291
address NativeMovRegMem::instruction_address() const {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   292
  return addr_at(instruction_start());
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   293
}
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   294
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   295
address NativeMovRegMem::next_instruction_address() const {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   296
  address ret = instruction_address() + instruction_size;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   297
  u_char instr_0 =  *(u_char*) instruction_address();
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   298
  switch (instr_0) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   299
  case instruction_operandsize_prefix:
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   300
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   301
    fatal("should have skipped instruction_operandsize_prefix");
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   302
    break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   304
  case instruction_extended_prefix:
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   305
    fatal("should have skipped instruction_extended_prefix");
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   306
    break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   307
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   308
  case instruction_code_mem2reg_movslq: // 0x63
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   309
  case instruction_code_mem2reg_movzxb: // 0xB6
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   310
  case instruction_code_mem2reg_movsxb: // 0xBE
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   311
  case instruction_code_mem2reg_movzxw: // 0xB7
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   312
  case instruction_code_mem2reg_movsxw: // 0xBF
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   313
  case instruction_code_reg2mem:        // 0x89 (q/l)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   314
  case instruction_code_mem2reg:        // 0x8B (q/l)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   315
  case instruction_code_reg2memb:       // 0x88
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   316
  case instruction_code_mem2regb:       // 0x8a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   317
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   318
  case instruction_code_float_s:        // 0xd9 fld_s a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   319
  case instruction_code_float_d:        // 0xdd fld_d a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   320
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   321
  case instruction_code_xmm_load:       // 0x10
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   322
  case instruction_code_xmm_store:      // 0x11
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   323
  case instruction_code_xmm_lpd:        // 0x12
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   324
    {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   325
      // If there is an SIB then instruction is longer than expected
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   326
      u_char mod_rm = *(u_char*)(instruction_address() + 1);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   327
      if ((mod_rm & 7) == 0x4) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   328
        ret++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   329
      }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   330
    }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   331
  case instruction_code_xor:
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   332
    fatal("should have skipped xor lead in");
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   333
    break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   334
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   335
  default:
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   336
    fatal("not a NativeMovRegMem");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  }
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   338
  return ret;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   339
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   340
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   342
int NativeMovRegMem::offset() const{
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   343
  int off = data_offset + instruction_start();
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   344
  u_char mod_rm = *(u_char*)(instruction_address() + 1);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   345
  // nnnn(r12|rsp) isn't coded as simple mod/rm since that is
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   346
  // the encoding to use an SIB byte. Which will have the nnnn
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   347
  // field off by one byte
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   348
  if ((mod_rm & 7) == 0x4) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   349
    off++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  }
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   351
  return int_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   352
}
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   353
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   354
void NativeMovRegMem::set_offset(int x) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   355
  int off = data_offset + instruction_start();
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   356
  u_char mod_rm = *(u_char*)(instruction_address() + 1);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   357
  // nnnn(r12|rsp) isn't coded as simple mod/rm since that is
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   358
  // the encoding to use an SIB byte. Which will have the nnnn
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   359
  // field off by one byte
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   360
  if ((mod_rm & 7) == 0x4) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   361
    off++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   362
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   363
  set_int_at(off, x);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
void NativeMovRegMem::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  // make sure code pattern is actually a mov [reg+offset], reg instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  u_char test_byte = *(u_char*)instruction_address();
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   369
  switch (test_byte) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   370
    case instruction_code_reg2memb:  // 0x88 movb a, r
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   371
    case instruction_code_reg2mem:   // 0x89 movl a, r (can be movq in 64bit)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   372
    case instruction_code_mem2regb:  // 0x8a movb r, a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   373
    case instruction_code_mem2reg:   // 0x8b movl r, a (can be movq in 64bit)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   374
      break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   375
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   376
    case instruction_code_mem2reg_movslq: // 0x63 movsql r, a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   377
    case instruction_code_mem2reg_movzxb: // 0xb6 movzbl r, a (movzxb)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   378
    case instruction_code_mem2reg_movzxw: // 0xb7 movzwl r, a (movzxw)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   379
    case instruction_code_mem2reg_movsxb: // 0xbe movsbl r, a (movsxb)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   380
    case instruction_code_mem2reg_movsxw: // 0xbf  movswl r, a (movsxw)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   381
      break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   382
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   383
    case instruction_code_float_s:   // 0xd9 fld_s a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   384
    case instruction_code_float_d:   // 0xdd fld_d a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   385
    case instruction_code_xmm_load:  // 0x10 movsd xmm, a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   386
    case instruction_code_xmm_store: // 0x11 movsd a, xmm
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   387
    case instruction_code_xmm_lpd:   // 0x12 movlpd xmm, a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   388
      break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   389
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   390
    default:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
          fatal ("not a mov [reg+offs], reg instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
void NativeMovRegMem::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  tty->print_cr("0x%x: mov reg, [reg + %x]", instruction_address(), offset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
void NativeLoadAddress::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  // make sure code pattern is actually a mov [reg+offset], reg instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  u_char test_byte = *(u_char*)instruction_address();
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   405
#ifdef _LP64
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   406
  if ( (test_byte == instruction_prefix_wide ||
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   407
        test_byte == instruction_prefix_wide_extended) ) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   408
    test_byte = *(u_char*)(instruction_address() + 1);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   409
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   410
#endif // _LP64
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   411
  if ( ! ((test_byte == lea_instruction_code)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   412
          LP64_ONLY(|| (test_byte == mov64_instruction_code) ))) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
    fatal ("not a lea reg, [reg+offs] instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
void NativeLoadAddress::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  tty->print_cr("0x%x: lea [reg + %x], reg", instruction_address(), offset());
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
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
void NativeJump::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  if (*(u_char*)instruction_address() != instruction_code) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    fatal("not a jump instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
void NativeJump::insert(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  *code_pos = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  *((int32_t*)(code_pos + 1)) = (int32_t)disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
void NativeJump::check_verified_entry_alignment(address entry, address verified_entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  // Patching to not_entrant can happen while activations of the method are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  // in use. The patching in that instance must happen only when certain
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
  // alignment restrictions are true. These guarantees check those
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  // conditions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  const int linesize = 64;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  const int linesize = 32;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  // Must be wordSize aligned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  guarantee(((uintptr_t) verified_entry & (wordSize -1)) == 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
            "illegal address for code patching 2");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
  // First 5 bytes must be within the same cache line - 4827828
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  guarantee((uintptr_t) verified_entry / linesize ==
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
            ((uintptr_t) verified_entry + 4) / linesize,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
            "illegal address for code patching 3");
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
// MT safe inserting of a jump over an unknown instruction sequence (used by nmethod::makeZombie)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
// The problem: jmp <dest> is a 5-byte instruction. Atomical write can be only with 4 bytes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
// First patches the first word atomically to be a jump to itself.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
// Then patches the last byte  and then atomically patches the first word (4-bytes),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
// thus inserting the desired jump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
// This code is mt-safe with the following conditions: entry point is 4 byte aligned,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
// entry point is in same cache line as unverified entry point, and the instruction being
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
// patched is >= 5 byte (size of patch).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
// In C2 the 5+ byte sized instruction is enforced by code in MachPrologNode::emit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
// In C1 the restriction is enforced by CodeEmitter::method_entry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
void NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  // complete jump instruction (to be inserted) is in code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  unsigned char code_buffer[5];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  code_buffer[0] = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
  intptr_t disp = (intptr_t)dest - ((intptr_t)verified_entry + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  *(int32_t*)(code_buffer + 1) = (int32_t)disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  check_verified_entry_alignment(entry, verified_entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  // Can't call nativeJump_at() because it's asserts jump exists
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  NativeJump* n_jump = (NativeJump*) verified_entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  //First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
  unsigned char patch[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  assert(sizeof(patch)==sizeof(int32_t), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
  patch[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  patch[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  patch[2] = 0xEB;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  patch[3] = 0xFE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
  *(int32_t*)verified_entry = *(int32_t *)patch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
  // Patch 5th byte (from jump instruction)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  verified_entry[4] = code_buffer[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
  n_jump->wrote(4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  // Patch bytes 0-3 (from jump instruction)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  *(int32_t*)verified_entry = *(int32_t *)code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
void NativePopReg::insert(address code_pos, Register reg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  assert(reg->encoding() < 8, "no space for REX");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  assert(NativePopReg::instruction_size == sizeof(char), "right address unit for update");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  *code_pos = (u_char)(instruction_code | reg->encoding());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
void NativeIllegalInstruction::insert(address code_pos) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
  assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
  *(short *)code_pos = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
void NativeGeneralJump::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  assert(((NativeInstruction *)this)->is_jump() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
         ((NativeInstruction *)this)->is_cond_jump(), "not a general jump instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
void NativeGeneralJump::insert_unconditional(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
  intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
  guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
  *code_pos = unconditional_long_jump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
  *((int32_t *)(code_pos+1)) = (int32_t) disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
// MT-safe patching of a long jump instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
// First patches first word of instruction to two jmp's that jmps to them
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
// selfs (spinlock). Then patches the last byte, and then atomicly replaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
// the jmp's with the first 4 byte of the new instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
void NativeGeneralJump::replace_mt_safe(address instr_addr, address code_buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
   assert (instr_addr != NULL, "illegal address for code patching (4)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
   NativeGeneralJump* n_jump =  nativeGeneralJump_at (instr_addr); // checking that it is a jump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
   // Temporary code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
   unsigned char patch[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
   assert(sizeof(patch)==sizeof(int32_t), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
   patch[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
   patch[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
   patch[2] = 0xEB;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
   patch[3] = 0xFE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
   // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
   *(int32_t*)instr_addr = *(int32_t *)patch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
    n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
   // Patch 4th byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
   instr_addr[4] = code_buffer[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
    n_jump->wrote(4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
   // Patch bytes 0-3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
   *(jint*)instr_addr = *(jint *)code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
    n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
   // verify patching
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
   for ( int i = 0; i < instruction_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
     address ptr = (address)((intptr_t)code_buffer + i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
     int a_byte = (*ptr) & 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
     assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
address NativeGeneralJump::jump_destination() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
  int op_code = ubyte_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
  bool is_rel32off = (op_code == 0xE9 || op_code == 0x0F);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  int  offset  = (op_code == 0x0F)  ? 2 : 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
  int  length  = offset + ((is_rel32off) ? 4 : 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
  if (is_rel32off)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
    return addr_at(0) + length + int_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
  else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
    return addr_at(0) + length + sbyte_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
}
363
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
   603
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
   604
bool NativeInstruction::is_dtrace_trap() {
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
   605
  return (*(int32_t*)this & 0xff) == 0xcc;
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
   606
}