hotspot/src/cpu/x86/vm/nativeInst_x86.hpp
author xdono
Wed, 02 Jul 2008 12:55:16 -0700
changeset 670 ddf3e9583f2f
parent 363 99d43e8a76ad
child 1066 717c3345024f
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
670
ddf3e9583f2f 6719955: Update copyright year
xdono
parents: 363
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  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
 *
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
// We have interfaces for the following instructions:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
// - NativeInstruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
// - - NativeCall
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// - - NativeMovConstReg
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// - - NativeMovConstRegPatching
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// - - NativeMovRegMem
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// - - NativeMovRegMemPatching
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// - - NativeJump
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// - - NativeIllegalOpCode
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// - - NativeGeneralJump
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// - - NativeReturn
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// - - NativeReturnX (return with argument)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// - - NativePushConst
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// - - NativeTstRegMem
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// The base class for different kinds of native instruction abstractions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// Provides the primitive operations to manipulate code relative to this.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
class NativeInstruction VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  friend class Relocation;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    nop_instruction_code        = 0x90,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    nop_instruction_size        =    1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  bool is_nop()                        { return ubyte_at(0) == nop_instruction_code; }
363
99d43e8a76ad 6537506: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents: 1
diff changeset
    53
  bool is_dtrace_trap();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  inline bool is_call();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  inline bool is_illegal();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  inline bool is_return();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  inline bool is_jump();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  inline bool is_cond_jump();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  inline bool is_safepoint_poll();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  inline bool is_mov_literal64();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  address addr_at(int offset) const    { return address(this) + offset; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  s_char sbyte_at(int offset) const    { return *(s_char*) addr_at(offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  u_char ubyte_at(int offset) const    { return *(u_char*) addr_at(offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  jint int_at(int offset) const         { return *(jint*) addr_at(offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  intptr_t ptr_at(int offset) const    { return *(intptr_t*) addr_at(offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  oop  oop_at (int offset) const       { return *(oop*) addr_at(offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  void set_char_at(int offset, char c)        { *addr_at(offset) = (u_char)c; wrote(offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  void set_int_at(int offset, jint  i)        { *(jint*)addr_at(offset) = i;  wrote(offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  void set_ptr_at (int offset, intptr_t  ptr) { *(intptr_t*) addr_at(offset) = ptr;  wrote(offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  void set_oop_at (int offset, oop  o)        { *(oop*) addr_at(offset) = o;  wrote(offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  // This doesn't really do anything on Intel, but it is the place where
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  // cache invalidation belongs, generically:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  void wrote(int offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  // unit test stuff
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  static void test() {}                 // override for testing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  inline friend NativeInstruction* nativeInstruction_at(address address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
inline NativeInstruction* nativeInstruction_at(address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  NativeInstruction* inst = (NativeInstruction*)address;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  //inst->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  return inst;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
inline NativeCall* nativeCall_at(address address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
// The NativeCall is an abstraction for accessing/manipulating native call imm32/rel32off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
// instructions (used to manipulate inline caches, primitive & dll calls, etc.).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
class NativeCall: public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    instruction_code            = 0xE8,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    instruction_size            =    5,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    instruction_offset          =    0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    displacement_offset         =    1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    return_address_offset       =    5
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  enum { cache_line_size = BytesPerWord };  // conservative estimate!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  address instruction_address() const       { return addr_at(instruction_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  address next_instruction_address() const  { return addr_at(return_address_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  int   displacement() const                { return (jint) int_at(displacement_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  address displacement_address() const      { return addr_at(displacement_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  address return_address() const            { return addr_at(return_address_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  address destination() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  void  set_destination(address dest)       {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    assert((labs((intptr_t) dest - (intptr_t) return_address())  &
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
            0xFFFFFFFF00000000) == 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
           "must be 32bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    set_int_at(displacement_offset, dest - return_address());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  void  set_destination_mt_safe(address dest);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  void  verify_alignment() { assert((intptr_t)addr_at(displacement_offset) % BytesPerInt == 0, "must be aligned"); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  void  verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  void  print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  // Creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  inline friend NativeCall* nativeCall_at(address address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  inline friend NativeCall* nativeCall_before(address return_address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  static bool is_call_at(address instr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    return ((*instr) & 0xFF) == NativeCall::instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  static bool is_call_before(address return_address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    return is_call_at(return_address - NativeCall::return_address_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  static bool is_call_to(address instr, address target) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    return nativeInstruction_at(instr)->is_call() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      nativeCall_at(instr)->destination() == target;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  // MT-safe patching of a call instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  static void insert(address code_pos, address entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  static void replace_mt_safe(address instr_addr, address code_buffer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
inline NativeCall* nativeCall_at(address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  NativeCall* call = (NativeCall*)(address - NativeCall::instruction_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  call->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  return call;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
inline NativeCall* nativeCall_before(address return_address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  NativeCall* call = (NativeCall*)(return_address - NativeCall::return_address_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  call->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  return call;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
// An interface for accessing/manipulating native mov reg, imm32 instructions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
// (used to manipulate inlined 32bit data dll calls, etc.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
class NativeMovConstReg: public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  static const bool has_rex = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  static const int rex_size = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  static const bool has_rex = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  static const int rex_size = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    instruction_code            = 0xB8,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    instruction_size            =    1 + rex_size + wordSize,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    instruction_offset          =    0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    data_offset                 =    1 + rex_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    next_instruction_offset     =    instruction_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    register_mask               = 0x07
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  address instruction_address() const       { return addr_at(instruction_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  address next_instruction_address() const  { return addr_at(next_instruction_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  intptr_t data() const                     { return ptr_at(data_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  void  set_data(intptr_t x)                { set_ptr_at(data_offset, x); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  void  verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  void  print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  // unit test stuff
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  static void test() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  // Creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  inline friend NativeMovConstReg* nativeMovConstReg_at(address address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  inline friend NativeMovConstReg* nativeMovConstReg_before(address address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
inline NativeMovConstReg* nativeMovConstReg_at(address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  NativeMovConstReg* test = (NativeMovConstReg*)(address - NativeMovConstReg::instruction_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  test->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  return test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
inline NativeMovConstReg* nativeMovConstReg_before(address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  NativeMovConstReg* test = (NativeMovConstReg*)(address - NativeMovConstReg::instruction_size - NativeMovConstReg::instruction_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  test->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  return test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
class NativeMovConstRegPatching: public NativeMovConstReg {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    friend NativeMovConstRegPatching* nativeMovConstRegPatching_at(address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    NativeMovConstRegPatching* test = (NativeMovConstRegPatching*)(address - instruction_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    #ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      test->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    return test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
#ifndef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
// An interface for accessing/manipulating native moves of the form:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
//      mov[b/w/l] [reg + offset], reg   (instruction_code_reg2mem)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
//      mov[b/w/l] reg, [reg+offset]     (instruction_code_mem2reg
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
//      mov[s/z]x[w/b] [reg + offset], reg
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
//      fld_s  [reg+offset]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
//      fld_d  [reg+offset]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
//      fstp_s [reg + offset]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
//      fstp_d [reg + offset]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
// Warning: These routines must be able to handle any instruction sequences
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
// that are generated as a result of the load/store byte,word,long
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
// macros.  For example: The load_unsigned_byte instruction generates
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
// an xor reg,reg inst prior to generating the movb instruction.  This
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
// class must skip the xor instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
class NativeMovRegMem: public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    instruction_code_xor                = 0x33,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    instruction_extended_prefix         = 0x0F,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    instruction_code_mem2reg_movzxb     = 0xB6,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
    instruction_code_mem2reg_movsxb     = 0xBE,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    instruction_code_mem2reg_movzxw     = 0xB7,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    instruction_code_mem2reg_movsxw     = 0xBF,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    instruction_operandsize_prefix      = 0x66,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    instruction_code_reg2meml           = 0x89,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    instruction_code_mem2regl           = 0x8b,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    instruction_code_reg2memb           = 0x88,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    instruction_code_mem2regb           = 0x8a,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    instruction_code_float_s            = 0xd9,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    instruction_code_float_d            = 0xdd,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
    instruction_code_long_volatile      = 0xdf,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
    instruction_code_xmm_ss_prefix      = 0xf3,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    instruction_code_xmm_sd_prefix      = 0xf2,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
    instruction_code_xmm_code           = 0x0f,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    instruction_code_xmm_load           = 0x10,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    instruction_code_xmm_store          = 0x11,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
    instruction_code_xmm_lpd            = 0x12,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    instruction_size                    = 4,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
    instruction_offset                  = 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
    data_offset                         = 2,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
    next_instruction_offset             = 4
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  address instruction_address() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    if (*addr_at(instruction_offset)   == instruction_operandsize_prefix &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
        *addr_at(instruction_offset+1) != instruction_code_xmm_code) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
      return addr_at(instruction_offset+1); // Not SSE instructions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    else if (*addr_at(instruction_offset) == instruction_extended_prefix) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
      return addr_at(instruction_offset+1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
    else if (*addr_at(instruction_offset) == instruction_code_xor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
      return addr_at(instruction_offset+2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    else return addr_at(instruction_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  address next_instruction_address() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    switch (*addr_at(instruction_offset)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    case instruction_operandsize_prefix:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
      if (*addr_at(instruction_offset+1) == instruction_code_xmm_code)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
        return instruction_address() + instruction_size; // SSE instructions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    case instruction_extended_prefix:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
      return instruction_address() + instruction_size + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    case instruction_code_reg2meml:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    case instruction_code_mem2regl:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    case instruction_code_reg2memb:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    case instruction_code_mem2regb:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
    case instruction_code_xor:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
      return instruction_address() + instruction_size + 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
      return instruction_address() + instruction_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  int   offset() const{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
    if (*addr_at(instruction_offset)   == instruction_operandsize_prefix &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
        *addr_at(instruction_offset+1) != instruction_code_xmm_code) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
      return int_at(data_offset+1); // Not SSE instructions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
    else if (*addr_at(instruction_offset) == instruction_extended_prefix) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
      return int_at(data_offset+1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
    else if (*addr_at(instruction_offset) == instruction_code_xor ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
             *addr_at(instruction_offset) == instruction_code_xmm_ss_prefix ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
             *addr_at(instruction_offset) == instruction_code_xmm_sd_prefix ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
             *addr_at(instruction_offset) == instruction_operandsize_prefix) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
      return int_at(data_offset+2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    else return int_at(data_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  void  set_offset(int x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    if (*addr_at(instruction_offset)   == instruction_operandsize_prefix &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
        *addr_at(instruction_offset+1) != instruction_code_xmm_code) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
      set_int_at(data_offset+1, x); // Not SSE instructions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    else if (*addr_at(instruction_offset) == instruction_extended_prefix) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
      set_int_at(data_offset+1, x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    else if (*addr_at(instruction_offset) == instruction_code_xor ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
             *addr_at(instruction_offset) == instruction_code_xmm_ss_prefix ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
             *addr_at(instruction_offset) == instruction_code_xmm_sd_prefix ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
             *addr_at(instruction_offset) == instruction_operandsize_prefix) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
      set_int_at(data_offset+2, x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    else set_int_at(data_offset, x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  void  add_offset_in_bytes(int add_offset)     { set_offset ( ( offset() + add_offset ) ); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  void  copy_instruction_to(address new_instruction_address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  void verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  void print ();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  // unit test stuff
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  static void test() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  inline friend NativeMovRegMem* nativeMovRegMem_at (address address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
inline NativeMovRegMem* nativeMovRegMem_at (address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  NativeMovRegMem* test = (NativeMovRegMem*)(address - NativeMovRegMem::instruction_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  test->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  return test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
class NativeMovRegMemPatching: public NativeMovRegMem {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  friend NativeMovRegMemPatching* nativeMovRegMemPatching_at (address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
    NativeMovRegMemPatching* test = (NativeMovRegMemPatching*)(address - instruction_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    #ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
      test->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    return test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
// An interface for accessing/manipulating native leal instruction of form:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
//        leal reg, [reg + offset]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
class NativeLoadAddress: public NativeMovRegMem {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    instruction_code            = 0x8D
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  void verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  void print ();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  // unit test stuff
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  static void test() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  friend NativeLoadAddress* nativeLoadAddress_at (address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
    NativeLoadAddress* test = (NativeLoadAddress*)(address - instruction_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
    #ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
      test->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
    return test;
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
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
// jump rel32off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
class NativeJump: public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
    instruction_code            = 0xe9,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
    instruction_size            =    5,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
    instruction_offset          =    0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
    data_offset                 =    1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
    next_instruction_offset     =    5
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
  address instruction_address() const       { return addr_at(instruction_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  address next_instruction_address() const  { return addr_at(next_instruction_offset); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  address jump_destination() const          {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
     address dest = (int_at(data_offset)+next_instruction_address());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
#ifdef AMD64 // What is this about?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
     // return -1 if jump to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
    dest = (dest == (address) this) ? (address) -1 : dest;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
    return dest;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  void  set_jump_destination(address dest)  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
    intptr_t val = dest - next_instruction_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
    if (dest == (address) -1) { // can't encode jump to -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
      val = -5; // jump to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
      assert((labs(val)  & 0xFFFFFFFF00000000) == 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
             "must be 32bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
    set_int_at(data_offset, (jint)val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  // Creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  inline friend NativeJump* nativeJump_at(address address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  void verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  // Unit testing stuff
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  static void test() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  // Insertion of native jump instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  static void insert(address code_pos, address entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
  // MT-safe insertion of native jump at verified method entry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  static void check_verified_entry_alignment(address entry, address verified_entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  static void patch_verified_entry(address entry, address verified_entry, address dest);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
inline NativeJump* nativeJump_at(address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  NativeJump* jump = (NativeJump*)(address - NativeJump::instruction_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  jump->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  return jump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
// Handles all kinds of jump on Intel. Long/far, conditional/unconditional
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
class NativeGeneralJump: public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
    // Constants does not apply, since the lengths and offsets depends on the actual jump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
    // used
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
    // Instruction codes:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
    //   Unconditional jumps: 0xE9    (rel32off), 0xEB (rel8off)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
    //   Conditional jumps:   0x0F8x  (rel32off), 0x7x (rel8off)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
    unconditional_long_jump  = 0xe9,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
    unconditional_short_jump = 0xeb,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
    instruction_size = 5
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  address instruction_address() const       { return addr_at(0); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  address jump_destination()    const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
  // Creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  inline friend NativeGeneralJump* nativeGeneralJump_at(address address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  // Insertion of native general jump instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  static void insert_unconditional(address code_pos, address entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  static void replace_mt_safe(address instr_addr, address code_buffer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  void verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
inline NativeGeneralJump* nativeGeneralJump_at(address address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  NativeGeneralJump* jump = (NativeGeneralJump*)(address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  debug_only(jump->verify();)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  return jump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
class NativePopReg : public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
    instruction_code            = 0x58,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
    instruction_size            =    1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
    instruction_offset          =    0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
    data_offset                 =    1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
    next_instruction_offset     =    1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  // Insert a pop instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  static void insert(address code_pos, Register reg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
class NativeIllegalInstruction: public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
    instruction_code            = 0x0B0F,    // Real byte order is: 0x0F, 0x0B
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
    instruction_size            =    2,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
    instruction_offset          =    0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
    next_instruction_offset     =    2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
  // Insert illegal opcode as specific address
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
  static void insert(address code_pos);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
// return instruction that does not pop values of the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
class NativeReturn: public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
    instruction_code            = 0xC3,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
    instruction_size            =    1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
    instruction_offset          =    0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
    next_instruction_offset     =    1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
// return instruction that does pop values of the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
class NativeReturnX: public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
    instruction_code            = 0xC2,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
    instruction_size            =    2,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
    instruction_offset          =    0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
    next_instruction_offset     =    2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
// Simple test vs memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
class NativeTstRegMem: public NativeInstruction {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  enum Intel_specific_constants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
    instruction_code_memXregl   = 0x85
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
inline bool NativeInstruction::is_illegal()      { return (short)int_at(0) == (short)NativeIllegalInstruction::instruction_code; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
inline bool NativeInstruction::is_call()         { return ubyte_at(0) == NativeCall::instruction_code; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
inline bool NativeInstruction::is_return()       { return ubyte_at(0) == NativeReturn::instruction_code ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
                                                          ubyte_at(0) == NativeReturnX::instruction_code; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
inline bool NativeInstruction::is_jump()         { return ubyte_at(0) == NativeJump::instruction_code ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
                                                          ubyte_at(0) == 0xEB; /* short jump */ }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
inline bool NativeInstruction::is_cond_jump()    { return (int_at(0) & 0xF0FF) == 0x800F /* long jump */ ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
                                                          (ubyte_at(0) & 0xF0) == 0x70;  /* short jump */ }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
inline bool NativeInstruction::is_safepoint_poll() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
  return ubyte_at(0) == NativeTstRegMem::instruction_code_memXregl &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
         ubyte_at(1) == 0x05 && // 00 rax 101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
         ((intptr_t) addr_at(6)) + int_at(2) == (intptr_t) os::get_polling_page();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  return ( ubyte_at(0) == NativeMovRegMem::instruction_code_mem2regl ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
           ubyte_at(0) == NativeTstRegMem::instruction_code_memXregl ) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
           (ubyte_at(1)&0xC7) == 0x05 && /* Mod R/M == disp32 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
           (os::is_poll_address((address)int_at(2)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
inline bool NativeInstruction::is_mov_literal64() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
  return ((ubyte_at(0) == Assembler::REX_W || ubyte_at(0) == Assembler::REX_WB) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
          (ubyte_at(1) & (0xff ^ NativeMovConstReg::register_mask)) == 0xB8);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
}