src/cpu/x86/vm/nativeInst_x86.cpp

Thu, 24 May 2018 17:06:56 +0800

author
aoqi
date
Thu, 24 May 2018 17:06:56 +0800
changeset 8604
04d83ba48607
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

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

mercurial