src/hotspot/cpu/x86/nativeInst_x86.cpp
author mdoerr
Mon, 12 Aug 2019 10:02:25 +0200
changeset 57710 05ff6e27de45
parent 52384 d6dc479bcdd3
child 58932 8623f75be895
permissions -rw-r--r--
8229236: CriticalJNINatives: dll handling should be done in native thread state Summary: Temporarily switch thread state from _thread_in_vm to _thread_in_native to execute I/O. Reviewed-by: dlong, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
50102
454fa295105c 8202976: Add C1 lea patching support for x86
pliden
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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"
14626
0cf4eccf130f 8003240: x86: move MacroAssembler into separate file
twisti
parents: 11427
diff changeset
    26
#include "asm/macroAssembler.hpp"
52384
d6dc479bcdd3 8212681: Refactor IC locking to use a fine grained CompiledICLocker
eosterlund
parents: 51996
diff changeset
    27
#include "code/compiledIC.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/resourceArea.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "nativeInst_x86.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#include "runtime/handles.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
#include "runtime/sharedRuntime.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    33
#include "runtime/stubRoutines.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    34
#include "utilities/ostream.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    35
#ifdef COMPILER1
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    36
#include "c1/c1_Runtime1.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    37
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
void NativeInstruction::wrote(int offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  ICache::invalidate_word(addr_at(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    43
void NativeLoadGot::report_and_fail() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    44
  tty->print_cr("Addr: " INTPTR_FORMAT, p2i(instruction_address()));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    45
  fatal("not a indirect rip mov to rbx");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    46
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    47
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    48
void NativeLoadGot::verify() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    49
  if (has_rex) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    50
    int rex = ubyte_at(0);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    51
    if (rex != rex_prefix) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    52
      report_and_fail();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    53
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    54
  }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    55
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    56
  int inst = ubyte_at(rex_size);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    57
  if (inst != instruction_code) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    58
    report_and_fail();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    59
  }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    60
  int modrm = ubyte_at(rex_size + 1);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    61
  if (modrm != modrm_rbx_code && modrm != modrm_rax_code) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    62
    report_and_fail();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    63
  }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    64
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    65
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    66
intptr_t NativeLoadGot::data() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    67
  return *(intptr_t *) got_address();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    68
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    69
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    70
address NativePltCall::destination() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    71
  NativeGotJump* jump = nativeGotJump_at(plt_jump());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    72
  return jump->destination();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    73
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    74
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    75
address NativePltCall::plt_entry() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    76
  return return_address() + displacement();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    77
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    78
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    79
address NativePltCall::plt_jump() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    80
  address entry = plt_entry();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    81
  // Virtual PLT code has move instruction first
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    82
  if (((NativeGotJump*)entry)->is_GotJump()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    83
    return entry;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    84
  } else {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    85
    return nativeLoadGot_at(entry)->next_instruction_address();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    86
  }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    87
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    88
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    89
address NativePltCall::plt_load_got() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    90
  address entry = plt_entry();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    91
  if (!((NativeGotJump*)entry)->is_GotJump()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    92
    // Virtual PLT code has move instruction first
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    93
    return entry;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    94
  } else {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    95
    // Static PLT code has move instruction second (from c2i stub)
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    96
    return nativeGotJump_at(entry)->next_instruction_address();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    97
  }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    98
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
    99
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   100
address NativePltCall::plt_c2i_stub() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   101
  address entry = plt_load_got();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   102
  // This method should be called only for static calls which has C2I stub.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   103
  NativeLoadGot* load = nativeLoadGot_at(entry);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   104
  return entry;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   105
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   106
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   107
address NativePltCall::plt_resolve_call() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   108
  NativeGotJump* jump = nativeGotJump_at(plt_jump());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   109
  address entry = jump->next_instruction_address();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   110
  if (((NativeGotJump*)entry)->is_GotJump()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   111
    return entry;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   112
  } else {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   113
    // c2i stub 2 instructions
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   114
    entry = nativeLoadGot_at(entry)->next_instruction_address();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   115
    return nativeGotJump_at(entry)->next_instruction_address();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   116
  }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   117
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   118
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   119
void NativePltCall::reset_to_plt_resolve_call() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   120
  set_destination_mt_safe(plt_resolve_call());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   121
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   122
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   123
void NativePltCall::set_destination_mt_safe(address dest) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   124
  // rewriting the value in the GOT, it should always be aligned
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   125
  NativeGotJump* jump = nativeGotJump_at(plt_jump());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   126
  address* got = (address *) jump->got_address();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   127
  *got = dest;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   128
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   129
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   130
void NativePltCall::set_stub_to_clean() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   131
  NativeLoadGot* method_loader = nativeLoadGot_at(plt_c2i_stub());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   132
  NativeGotJump* jump          = nativeGotJump_at(method_loader->next_instruction_address());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   133
  method_loader->set_data(0);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   134
  jump->set_jump_destination((address)-1);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   135
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   136
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   137
void NativePltCall::verify() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   138
  // Make sure code pattern is actually a call rip+off32 instruction.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   139
  int inst = ubyte_at(0);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   140
  if (inst != instruction_code) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   141
    tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", p2i(instruction_address()),
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   142
                                                        inst);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   143
    fatal("not a call rip+off32");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   144
  }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   145
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   146
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   147
address NativeGotJump::destination() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   148
  address *got_entry = (address *) got_address();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   149
  return *got_entry;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   150
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   151
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   152
void NativeGotJump::verify() const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   153
  int inst = ubyte_at(0);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   154
  if (inst != instruction_code) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   155
    tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", p2i(instruction_address()),
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   156
                                                        inst);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   157
    fatal("not a indirect rip jump");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   158
  }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   159
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   160
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
void NativeCall::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  // Make sure code pattern is actually a call imm32 instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  int inst = ubyte_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  if (inst != instruction_code) {
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 28947
diff changeset
   165
    tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", p2i(instruction_address()),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
                                                        inst);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    fatal("not a call disp32");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
address NativeCall::destination() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  // Getting the destination of a call isn't safe because that call can
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  // be getting patched while you're calling this.  There's only special
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  // places where this can be called but not automatically verifiable by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  // checking which locks are held.  The solution is true atomic patching
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  // on x86, nyi.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  return return_address() + displacement();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
void NativeCall::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  tty->print_cr(PTR_FORMAT ": call " PTR_FORMAT,
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 28947
diff changeset
   182
                p2i(instruction_address()), p2i(destination()));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
// Inserts a native call instruction at a given pc
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
void NativeCall::insert(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  guarantee(disp == (intptr_t)(jint)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  *code_pos = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  *((int32_t *)(code_pos+1)) = (int32_t) disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
// MT-safe patching of a call instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
// First patches first word of instruction to two jmp's that jmps to them
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
// selfs (spinlock). Then patches the last byte, and then atomicly replaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
// the jmp's with the first 4 byte of the new instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
void NativeCall::replace_mt_safe(address instr_addr, address code_buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  assert(Patching_lock->is_locked() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
         SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  assert (instr_addr != NULL, "illegal address for code patching");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  NativeCall* n_call =  nativeCall_at (instr_addr); // checking that it is a call
51996
84743156e780 8188764: Obsolete AssumeMP and then remove all support for non-MP builds
dholmes
parents: 50102
diff changeset
   206
  guarantee((intptr_t)instr_addr % BytesPerWord == 0, "must be aligned");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  unsigned char patch[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  assert(sizeof(patch)==sizeof(jint), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  patch[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  patch[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  patch[2] = 0xEB;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  patch[3] = 0xFE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  *(jint*)instr_addr = *(jint *)patch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  n_call->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  // Patch 4th byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  instr_addr[4] = code_buffer[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  n_call->wrote(4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  // Patch bytes 0-3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  *(jint*)instr_addr = *(jint *)code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  n_call->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
   // verify patching
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
   for ( int i = 0; i < instruction_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
     address ptr = (address)((intptr_t)code_buffer + i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
     int a_byte = (*ptr) & 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
     assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
// Similar to replace_mt_safe, but just changes the destination.  The
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
// important thing is that free-running threads are able to execute this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
// call instruction at all times.  If the displacement field is aligned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
// we can simply rely on atomicity of 32-bit writes to make sure other threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
// will see no intermediate states.  Otherwise, the first two bytes of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
// call are guaranteed to be aligned, and can be atomically patched to a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
// self-loop to guard the instruction while we change the other bytes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
// We cannot rely on locks here, since the free-running threads must run at
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
// full speed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
// Used in the runtime linkage of calls; see class CompiledIC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
// (Cf. 4506997 and 4479829, where threads witnessed garbage displacements.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
void NativeCall::set_destination_mt_safe(address dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  debug_only(verify());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  // Make sure patching code is locked.  No two threads can patch at the same
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  // time but one may be executing this code.
52384
d6dc479bcdd3 8212681: Refactor IC locking to use a fine grained CompiledICLocker
eosterlund
parents: 51996
diff changeset
   261
  assert(Patching_lock->is_locked() || SafepointSynchronize::is_at_safepoint() ||
d6dc479bcdd3 8212681: Refactor IC locking to use a fine grained CompiledICLocker
eosterlund
parents: 51996
diff changeset
   262
         CompiledICLocker::is_safe(instruction_address()), "concurrent code patching");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  // Both C1 and C2 should now be generating code which aligns the patched address
51996
84743156e780 8188764: Obsolete AssumeMP and then remove all support for non-MP builds
dholmes
parents: 50102
diff changeset
   264
  // to be within a single cache line.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  bool is_aligned = ((uintptr_t)displacement_address() + 0) / cache_line_size ==
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
                    ((uintptr_t)displacement_address() + 3) / cache_line_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
51996
84743156e780 8188764: Obsolete AssumeMP and then remove all support for non-MP builds
dholmes
parents: 50102
diff changeset
   268
  guarantee(is_aligned, "destination must be aligned");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
51996
84743156e780 8188764: Obsolete AssumeMP and then remove all support for non-MP builds
dholmes
parents: 50102
diff changeset
   270
  // The destination lies within a single cache line.
84743156e780 8188764: Obsolete AssumeMP and then remove all support for non-MP builds
dholmes
parents: 50102
diff changeset
   271
  set_destination(dest);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
void NativeMovConstReg::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  // make sure code pattern is actually a mov reg64, imm64 instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  if ((ubyte_at(0) != Assembler::REX_W && ubyte_at(0) != Assembler::REX_WB) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
      (ubyte_at(1) & (0xff ^ register_mask)) != 0xB8) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
    print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
    fatal("not a REX.W[B] mov reg64, imm64");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  // make sure code pattern is actually a mov reg, imm32 instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  u_char test_byte = *(u_char*)instruction_address();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  u_char test_byte_2 = test_byte & ( 0xff ^ register_mask);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  if (test_byte_2 != instruction_code) fatal("not a mov reg, imm32");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
void NativeMovConstReg::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  tty->print_cr(PTR_FORMAT ": mov reg, " INTPTR_FORMAT,
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 28947
diff changeset
   294
                p2i(instruction_address()), data());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   299
int NativeMovRegMem::instruction_start() const {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   300
  int off = 0;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   301
  u_char instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   302
11427
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   303
  // See comment in Assembler::locate_operand() about VEX prefixes.
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   304
  if (instr_0 == instruction_VEX_prefix_2bytes) {
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   305
    assert((UseAVX > 0), "shouldn't have VEX prefix");
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   306
    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
   307
    return 2;
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   308
  }
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   309
  if (instr_0 == instruction_VEX_prefix_3bytes) {
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   310
    assert((UseAVX > 0), "shouldn't have VEX prefix");
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   311
    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
   312
    return 3;
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   313
  }
44518
46f88691d812 8178033: C1 crashes with -XX:UseAVX = 3: "not a mov [reg+offs], reg instruction"
thartmann
parents: 42650
diff changeset
   314
  if (instr_0 == instruction_EVEX_prefix_4bytes) {
46f88691d812 8178033: C1 crashes with -XX:UseAVX = 3: "not a mov [reg+offs], reg instruction"
thartmann
parents: 42650
diff changeset
   315
    assert(VM_Version::supports_evex(), "shouldn't have EVEX prefix");
46f88691d812 8178033: C1 crashes with -XX:UseAVX = 3: "not a mov [reg+offs], reg instruction"
thartmann
parents: 42650
diff changeset
   316
    return 4;
46f88691d812 8178033: C1 crashes with -XX:UseAVX = 3: "not a mov [reg+offs], reg instruction"
thartmann
parents: 42650
diff changeset
   317
  }
11427
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   318
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   319
  // 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
   320
  if (instr_0 >= instruction_prefix_wide_lo && // 0x40
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   321
      instr_0 <= instruction_prefix_wide_hi) { // 0x4f
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   322
    off++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   323
    instr_0 = ubyte_at(off);
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
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   326
  if (instr_0 == instruction_code_xor) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   327
    off += 2;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   328
    instr_0 = ubyte_at(off);
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
  // 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
   332
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   333
  if (instr_0 == instruction_operandsize_prefix ) {  // 0x66
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   334
    off++; // Not SSE instructions
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   335
    instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   336
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
11427
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   338
  if ( instr_0 == instruction_code_xmm_ss_prefix || // 0xf3
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   339
       instr_0 == instruction_code_xmm_sd_prefix) { // 0xf2
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   340
    off++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   341
    instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   342
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   343
11427
bf248009cbbe 7116452: Add support for AVX instructions
kvn
parents: 7397
diff changeset
   344
  if ( instr_0 >= instruction_prefix_wide_lo && // 0x40
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   345
       instr_0 <= instruction_prefix_wide_hi) { // 0x4f
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   346
    off++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   347
    instr_0 = ubyte_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   348
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   349
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   350
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   351
  if (instr_0 == instruction_extended_prefix ) {  // 0x0f
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   352
    off++;
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
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   355
  return off;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   356
}
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   357
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   358
address NativeMovRegMem::instruction_address() const {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   359
  return addr_at(instruction_start());
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   360
}
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   361
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   362
address NativeMovRegMem::next_instruction_address() const {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   363
  address ret = instruction_address() + instruction_size;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   364
  u_char instr_0 =  *(u_char*) instruction_address();
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   365
  switch (instr_0) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   366
  case instruction_operandsize_prefix:
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   367
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   368
    fatal("should have skipped instruction_operandsize_prefix");
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   369
    break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   371
  case instruction_extended_prefix:
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   372
    fatal("should have skipped instruction_extended_prefix");
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   373
    break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   374
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   375
  case instruction_code_mem2reg_movslq: // 0x63
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   376
  case instruction_code_mem2reg_movzxb: // 0xB6
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   377
  case instruction_code_mem2reg_movsxb: // 0xBE
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   378
  case instruction_code_mem2reg_movzxw: // 0xB7
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   379
  case instruction_code_mem2reg_movsxw: // 0xBF
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   380
  case instruction_code_reg2mem:        // 0x89 (q/l)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   381
  case instruction_code_mem2reg:        // 0x8B (q/l)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   382
  case instruction_code_reg2memb:       // 0x88
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   383
  case instruction_code_mem2regb:       // 0x8a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   384
50102
454fa295105c 8202976: Add C1 lea patching support for x86
pliden
parents: 47216
diff changeset
   385
  case instruction_code_lea:            // 0x8d
454fa295105c 8202976: Add C1 lea patching support for x86
pliden
parents: 47216
diff changeset
   386
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   387
  case instruction_code_float_s:        // 0xd9 fld_s a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   388
  case instruction_code_float_d:        // 0xdd fld_d a
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
  case instruction_code_xmm_load:       // 0x10
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   391
  case instruction_code_xmm_store:      // 0x11
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   392
  case instruction_code_xmm_lpd:        // 0x12
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   393
    {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   394
      // If there is an SIB then instruction is longer than expected
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   395
      u_char mod_rm = *(u_char*)(instruction_address() + 1);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   396
      if ((mod_rm & 7) == 0x4) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   397
        ret++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   398
      }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   399
    }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   400
  case instruction_code_xor:
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   401
    fatal("should have skipped xor lead in");
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   402
    break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   403
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   404
  default:
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   405
    fatal("not a NativeMovRegMem");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  }
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   407
  return ret;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   408
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   409
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   411
int NativeMovRegMem::offset() const{
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   412
  int off = data_offset + instruction_start();
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   413
  u_char mod_rm = *(u_char*)(instruction_address() + 1);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   414
  // 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
   415
  // 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
   416
  // field off by one byte
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   417
  if ((mod_rm & 7) == 0x4) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   418
    off++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  }
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   420
  return int_at(off);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   421
}
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   422
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   423
void NativeMovRegMem::set_offset(int x) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   424
  int off = data_offset + instruction_start();
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   425
  u_char mod_rm = *(u_char*)(instruction_address() + 1);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   426
  // 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
   427
  // 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
   428
  // field off by one byte
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   429
  if ((mod_rm & 7) == 0x4) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   430
    off++;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   431
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   432
  set_int_at(off, x);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
void NativeMovRegMem::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  // make sure code pattern is actually a mov [reg+offset], reg instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  u_char test_byte = *(u_char*)instruction_address();
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   438
  switch (test_byte) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   439
    case instruction_code_reg2memb:  // 0x88 movb a, r
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   440
    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
   441
    case instruction_code_mem2regb:  // 0x8a movb r, a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   442
    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
   443
      break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   444
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   445
    case instruction_code_mem2reg_movslq: // 0x63 movsql r, a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   446
    case instruction_code_mem2reg_movzxb: // 0xb6 movzbl r, a (movzxb)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   447
    case instruction_code_mem2reg_movzxw: // 0xb7 movzwl r, a (movzxw)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   448
    case instruction_code_mem2reg_movsxb: // 0xbe movsbl r, a (movsxb)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   449
    case instruction_code_mem2reg_movsxw: // 0xbf  movswl r, a (movsxw)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   450
      break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   451
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   452
    case instruction_code_float_s:   // 0xd9 fld_s a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   453
    case instruction_code_float_d:   // 0xdd fld_d a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   454
    case instruction_code_xmm_load:  // 0x10 movsd xmm, a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   455
    case instruction_code_xmm_store: // 0x11 movsd a, xmm
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   456
    case instruction_code_xmm_lpd:   // 0x12 movlpd xmm, a
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   457
      break;
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   458
50102
454fa295105c 8202976: Add C1 lea patching support for x86
pliden
parents: 47216
diff changeset
   459
    case instruction_code_lea:       // 0x8d lea r, a
454fa295105c 8202976: Add C1 lea patching support for x86
pliden
parents: 47216
diff changeset
   460
      break;
454fa295105c 8202976: Add C1 lea patching support for x86
pliden
parents: 47216
diff changeset
   461
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   462
    default:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
          fatal ("not a mov [reg+offs], reg instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
void NativeMovRegMem::print() {
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 28947
diff changeset
   469
  tty->print_cr(PTR_FORMAT ": mov reg, [reg + %x]", p2i(instruction_address()), offset());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
//-------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
void NativeLoadAddress::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  // make sure code pattern is actually a mov [reg+offset], reg instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  u_char test_byte = *(u_char*)instruction_address();
1066
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   477
#ifdef _LP64
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   478
  if ( (test_byte == instruction_prefix_wide ||
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   479
        test_byte == instruction_prefix_wide_extended) ) {
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   480
    test_byte = *(u_char*)(instruction_address() + 1);
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   481
  }
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   482
#endif // _LP64
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   483
  if ( ! ((test_byte == lea_instruction_code)
717c3345024f 5108146: Merge i486 and amd64 cpu directories
never
parents: 670
diff changeset
   484
          LP64_ONLY(|| (test_byte == mov64_instruction_code) ))) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
    fatal ("not a lea reg, [reg+offs] instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
void NativeLoadAddress::print() {
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 28947
diff changeset
   491
  tty->print_cr(PTR_FORMAT ": lea [reg + %x], reg", p2i(instruction_address()), offset());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
//--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
void NativeJump::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  if (*(u_char*)instruction_address() != instruction_code) {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   498
    // far jump
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   499
    NativeMovConstReg* mov = nativeMovConstReg_at(instruction_address());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   500
    NativeInstruction* jmp = nativeInstruction_at(mov->next_instruction_address());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   501
    if (!jmp->is_jump_reg()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   502
      fatal("not a jump instruction");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   503
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
void NativeJump::insert(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  *code_pos = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  *((int32_t*)(code_pos + 1)) = (int32_t)disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
void NativeJump::check_verified_entry_alignment(address entry, address verified_entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
  // Patching to not_entrant can happen while activations of the method are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
  // in use. The patching in that instance must happen only when certain
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
  // alignment restrictions are true. These guarantees check those
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
  // conditions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
  const int linesize = 64;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
  const int linesize = 32;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  // Must be wordSize aligned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  guarantee(((uintptr_t) verified_entry & (wordSize -1)) == 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
            "illegal address for code patching 2");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  // First 5 bytes must be within the same cache line - 4827828
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  guarantee((uintptr_t) verified_entry / linesize ==
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
            ((uintptr_t) verified_entry + 4) / linesize,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
            "illegal address for code patching 3");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
// MT safe inserting of a jump over an unknown instruction sequence (used by nmethod::makeZombie)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
// The problem: jmp <dest> is a 5-byte instruction. Atomical write can be only with 4 bytes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
// First patches the first word atomically to be a jump to itself.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
// Then patches the last byte  and then atomically patches the first word (4-bytes),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
// thus inserting the desired jump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
// This code is mt-safe with the following conditions: entry point is 4 byte aligned,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
// entry point is in same cache line as unverified entry point, and the instruction being
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
// patched is >= 5 byte (size of patch).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
// In C2 the 5+ byte sized instruction is enforced by code in MachPrologNode::emit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
// In C1 the restriction is enforced by CodeEmitter::method_entry
33160
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 28947
diff changeset
   552
// In JVMCI, the restriction is enforced by HotSpotFrameContext.enter(...)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
void NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  // complete jump instruction (to be inserted) is in code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  unsigned char code_buffer[5];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
  code_buffer[0] = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
  intptr_t disp = (intptr_t)dest - ((intptr_t)verified_entry + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
  guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
  *(int32_t*)(code_buffer + 1) = (int32_t)disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
  check_verified_entry_alignment(entry, verified_entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
  // Can't call nativeJump_at() because it's asserts jump exists
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
  NativeJump* n_jump = (NativeJump*) verified_entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
  //First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
  unsigned char patch[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
  assert(sizeof(patch)==sizeof(int32_t), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
  patch[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
  patch[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  patch[2] = 0xEB;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
  patch[3] = 0xFE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
  // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  *(int32_t*)verified_entry = *(int32_t *)patch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
  n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
  // Patch 5th byte (from jump instruction)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
  verified_entry[4] = code_buffer[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
  n_jump->wrote(4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
  // Patch bytes 0-3 (from jump instruction)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
  *(int32_t*)verified_entry = *(int32_t *)code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  // Invalidate.  Opteron requires a flush after every write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
  n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   595
address NativeFarJump::jump_destination() const          {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   596
  NativeMovConstReg* mov = nativeMovConstReg_at(addr_at(0));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   597
  return (address)mov->data();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   598
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   599
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   600
void NativeFarJump::verify() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   601
  if (is_far_jump()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   602
    NativeMovConstReg* mov = nativeMovConstReg_at(addr_at(0));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   603
    NativeInstruction* jmp = nativeInstruction_at(mov->next_instruction_address());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   604
    if (jmp->is_jump_reg()) return;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   605
  }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   606
  fatal("not a jump instruction");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   607
}
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 33198
diff changeset
   608
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
void NativePopReg::insert(address code_pos, Register reg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
  assert(reg->encoding() < 8, "no space for REX");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
  assert(NativePopReg::instruction_size == sizeof(char), "right address unit for update");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
  *code_pos = (u_char)(instruction_code | reg->encoding());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
void NativeIllegalInstruction::insert(address code_pos) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
  assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
  *(short *)code_pos = instruction_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
void NativeGeneralJump::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
  assert(((NativeInstruction *)this)->is_jump() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
         ((NativeInstruction *)this)->is_cond_jump(), "not a general jump instruction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
void NativeGeneralJump::insert_unconditional(address code_pos, address entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
  intptr_t disp = (intptr_t)entry - ((intptr_t)code_pos + 1 + 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
  guarantee(disp == (intptr_t)(int32_t)disp, "must be 32-bit offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
  *code_pos = unconditional_long_jump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
  *((int32_t *)(code_pos+1)) = (int32_t) disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
  ICache::invalidate_range(code_pos, instruction_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
// MT-safe patching of a long jump instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
// First patches first word of instruction to two jmp's that jmps to them
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
// selfs (spinlock). Then patches the last byte, and then atomicly replaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
// the jmp's with the first 4 byte of the new instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
void NativeGeneralJump::replace_mt_safe(address instr_addr, address code_buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
   assert (instr_addr != NULL, "illegal address for code patching (4)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
   NativeGeneralJump* n_jump =  nativeGeneralJump_at (instr_addr); // checking that it is a jump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
   // Temporary code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
   unsigned char patch[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
   assert(sizeof(patch)==sizeof(int32_t), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
   patch[0] = 0xEB;       // jmp rel8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
   patch[1] = 0xFE;       // jmp to self
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
   patch[2] = 0xEB;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
   patch[3] = 0xFE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
   // First patch dummy jmp in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
   *(int32_t*)instr_addr = *(int32_t *)patch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
    n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
   // Patch 4th byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
   instr_addr[4] = code_buffer[4];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
    n_jump->wrote(4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
   // Patch bytes 0-3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
   *(jint*)instr_addr = *(jint *)code_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
    n_jump->wrote(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
   // verify patching
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
   for ( int i = 0; i < instruction_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
     address ptr = (address)((intptr_t)code_buffer + i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
     int a_byte = (*ptr) & 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
     assert(*((address)((intptr_t)instr_addr + i)) == a_byte, "mt safe patching failed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
address NativeGeneralJump::jump_destination() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
  int op_code = ubyte_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
  bool is_rel32off = (op_code == 0xE9 || op_code == 0x0F);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
  int  offset  = (op_code == 0x0F)  ? 2 : 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
  int  length  = offset + ((is_rel32off) ? 4 : 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
  if (is_rel32off)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
    return addr_at(0) + length + int_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
  else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
    return addr_at(0) + length + sbyte_at(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
}