src/cpu/sparc/vm/nativeInst_sparc.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 5784
190899198332
parent 0
f90c822e73f8
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, 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_sparc.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
aoqi@0 39 bool NativeInstruction::is_dtrace_trap() {
aoqi@0 40 return !is_nop();
aoqi@0 41 }
aoqi@0 42
aoqi@0 43 void NativeInstruction::set_data64_sethi(address instaddr, intptr_t x) {
aoqi@0 44 ResourceMark rm;
aoqi@0 45 CodeBuffer buf(instaddr, 10 * BytesPerInstWord );
aoqi@0 46 MacroAssembler* _masm = new MacroAssembler(&buf);
aoqi@0 47 Register destreg;
aoqi@0 48
aoqi@0 49 destreg = inv_rd(*(unsigned int *)instaddr);
aoqi@0 50 // Generate a the new sequence
aoqi@0 51 _masm->patchable_sethi(x, destreg);
aoqi@0 52 ICache::invalidate_range(instaddr, 7 * BytesPerInstWord);
aoqi@0 53 }
aoqi@0 54
aoqi@0 55 void NativeInstruction::verify_data64_sethi(address instaddr, intptr_t x) {
aoqi@0 56 ResourceMark rm;
aoqi@0 57 unsigned char buffer[10 * BytesPerInstWord];
aoqi@0 58 CodeBuffer buf(buffer, 10 * BytesPerInstWord);
aoqi@0 59 MacroAssembler masm(&buf);
aoqi@0 60
aoqi@0 61 Register destreg = inv_rd(*(unsigned int *)instaddr);
aoqi@0 62 // Generate the proper sequence into a temporary buffer and compare
aoqi@0 63 // it with the original sequence.
aoqi@0 64 masm.patchable_sethi(x, destreg);
aoqi@0 65 int len = buffer - masm.pc();
aoqi@0 66 for (int i = 0; i < len; i++) {
aoqi@0 67 assert(instaddr[i] == buffer[i], "instructions must match");
aoqi@0 68 }
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 void NativeInstruction::verify() {
aoqi@0 72 // make sure code pattern is actually an instruction address
aoqi@0 73 address addr = addr_at(0);
aoqi@0 74 if (addr == 0 || ((intptr_t)addr & 3) != 0) {
aoqi@0 75 fatal("not an instruction address");
aoqi@0 76 }
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 void NativeInstruction::print() {
aoqi@0 80 tty->print_cr(INTPTR_FORMAT ": 0x%x", addr_at(0), long_at(0));
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 void NativeInstruction::set_long_at(int offset, int i) {
aoqi@0 84 address addr = addr_at(offset);
aoqi@0 85 *(int*)addr = i;
aoqi@0 86 ICache::invalidate_word(addr);
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 void NativeInstruction::set_jlong_at(int offset, jlong i) {
aoqi@0 90 address addr = addr_at(offset);
aoqi@0 91 *(jlong*)addr = i;
aoqi@0 92 // Don't need to invalidate 2 words here, because
aoqi@0 93 // the flush instruction operates on doublewords.
aoqi@0 94 ICache::invalidate_word(addr);
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 void NativeInstruction::set_addr_at(int offset, address x) {
aoqi@0 98 address addr = addr_at(offset);
aoqi@0 99 assert( ((intptr_t)addr & (wordSize-1)) == 0, "set_addr_at bad address alignment");
aoqi@0 100 *(uintptr_t*)addr = (uintptr_t)x;
aoqi@0 101 // Don't need to invalidate 2 words here in the 64-bit case,
aoqi@0 102 // because the flush instruction operates on doublewords.
aoqi@0 103 ICache::invalidate_word(addr);
aoqi@0 104 // The Intel code has this assertion for NativeCall::set_destination,
aoqi@0 105 // NativeMovConstReg::set_data, NativeMovRegMem::set_offset,
aoqi@0 106 // NativeJump::set_jump_destination, and NativePushImm32::set_data
aoqi@0 107 //assert (Patching_lock->owned_by_self(), "must hold lock to patch instruction")
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 bool NativeInstruction::is_zero_test(Register &reg) {
aoqi@0 111 int x = long_at(0);
aoqi@0 112 Assembler::op3s temp = (Assembler::op3s) (Assembler::sub_op3 | Assembler::cc_bit_op3);
aoqi@0 113 if (is_op3(x, temp, Assembler::arith_op) &&
aoqi@0 114 inv_immed(x) && inv_rd(x) == G0) {
aoqi@0 115 if (inv_rs1(x) == G0) {
aoqi@0 116 reg = inv_rs2(x);
aoqi@0 117 return true;
aoqi@0 118 } else if (inv_rs2(x) == G0) {
aoqi@0 119 reg = inv_rs1(x);
aoqi@0 120 return true;
aoqi@0 121 }
aoqi@0 122 }
aoqi@0 123 return false;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 bool NativeInstruction::is_load_store_with_small_offset(Register reg) {
aoqi@0 127 int x = long_at(0);
aoqi@0 128 if (is_op(x, Assembler::ldst_op) &&
aoqi@0 129 inv_rs1(x) == reg && inv_immed(x)) {
aoqi@0 130 return true;
aoqi@0 131 }
aoqi@0 132 return false;
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 void NativeCall::verify() {
aoqi@0 136 NativeInstruction::verify();
aoqi@0 137 // make sure code pattern is actually a call instruction
aoqi@0 138 if (!is_op(long_at(0), Assembler::call_op)) {
aoqi@0 139 fatal("not a call");
aoqi@0 140 }
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 void NativeCall::print() {
aoqi@0 144 tty->print_cr(INTPTR_FORMAT ": call " INTPTR_FORMAT, instruction_address(), destination());
aoqi@0 145 }
aoqi@0 146
aoqi@0 147
aoqi@0 148 // MT-safe patching of a call instruction (and following word).
aoqi@0 149 // First patches the second word, and then atomicly replaces
aoqi@0 150 // the first word with the first new instruction word.
aoqi@0 151 // Other processors might briefly see the old first word
aoqi@0 152 // followed by the new second word. This is OK if the old
aoqi@0 153 // second word is harmless, and the new second word may be
aoqi@0 154 // harmlessly executed in the delay slot of the call.
aoqi@0 155 void NativeCall::replace_mt_safe(address instr_addr, address code_buffer) {
aoqi@0 156 assert(Patching_lock->is_locked() ||
aoqi@0 157 SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
aoqi@0 158 assert (instr_addr != NULL, "illegal address for code patching");
aoqi@0 159 NativeCall* n_call = nativeCall_at (instr_addr); // checking that it is a call
aoqi@0 160 assert(NativeCall::instruction_size == 8, "wrong instruction size; must be 8");
aoqi@0 161 int i0 = ((int*)code_buffer)[0];
aoqi@0 162 int i1 = ((int*)code_buffer)[1];
aoqi@0 163 int* contention_addr = (int*) n_call->addr_at(1*BytesPerInstWord);
aoqi@0 164 assert(inv_op(*contention_addr) == Assembler::arith_op ||
aoqi@0 165 *contention_addr == nop_instruction(),
aoqi@0 166 "must not interfere with original call");
aoqi@0 167 // The set_long_at calls do the ICacheInvalidate so we just need to do them in reverse order
aoqi@0 168 n_call->set_long_at(1*BytesPerInstWord, i1);
aoqi@0 169 n_call->set_long_at(0*BytesPerInstWord, i0);
aoqi@0 170 // NOTE: It is possible that another thread T will execute
aoqi@0 171 // only the second patched word.
aoqi@0 172 // In other words, since the original instruction is this
aoqi@0 173 // call patching_stub; nop (NativeCall)
aoqi@0 174 // and the new sequence from the buffer is this:
aoqi@0 175 // sethi %hi(K), %r; add %r, %lo(K), %r (NativeMovConstReg)
aoqi@0 176 // what T will execute is this:
aoqi@0 177 // call patching_stub; add %r, %lo(K), %r
aoqi@0 178 // thereby putting garbage into %r before calling the patching stub.
aoqi@0 179 // This is OK, because the patching stub ignores the value of %r.
aoqi@0 180
aoqi@0 181 // Make sure the first-patched instruction, which may co-exist
aoqi@0 182 // briefly with the call, will do something harmless.
aoqi@0 183 assert(inv_op(*contention_addr) == Assembler::arith_op ||
aoqi@0 184 *contention_addr == nop_instruction(),
aoqi@0 185 "must not interfere with original call");
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 // Similar to replace_mt_safe, but just changes the destination. The
aoqi@0 189 // important thing is that free-running threads are able to execute this
aoqi@0 190 // call instruction at all times. Thus, the displacement field must be
aoqi@0 191 // instruction-word-aligned. This is always true on SPARC.
aoqi@0 192 //
aoqi@0 193 // Used in the runtime linkage of calls; see class CompiledIC.
aoqi@0 194 void NativeCall::set_destination_mt_safe(address dest) {
aoqi@0 195 assert(Patching_lock->is_locked() ||
aoqi@0 196 SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
aoqi@0 197 // set_destination uses set_long_at which does the ICache::invalidate
aoqi@0 198 set_destination(dest);
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 // Code for unit testing implementation of NativeCall class
aoqi@0 202 void NativeCall::test() {
aoqi@0 203 #ifdef ASSERT
aoqi@0 204 ResourceMark rm;
aoqi@0 205 CodeBuffer cb("test", 100, 100);
aoqi@0 206 MacroAssembler* a = new MacroAssembler(&cb);
aoqi@0 207 NativeCall *nc;
aoqi@0 208 uint idx;
aoqi@0 209 int offsets[] = {
aoqi@0 210 0x0,
aoqi@0 211 0xfffffff0,
aoqi@0 212 0x7ffffff0,
aoqi@0 213 0x80000000,
aoqi@0 214 0x20,
aoqi@0 215 0x4000,
aoqi@0 216 };
aoqi@0 217
aoqi@0 218 VM_Version::allow_all();
aoqi@0 219
aoqi@0 220 a->call( a->pc(), relocInfo::none );
aoqi@0 221 a->delayed()->nop();
aoqi@0 222 nc = nativeCall_at( cb.insts_begin() );
aoqi@0 223 nc->print();
aoqi@0 224
aoqi@0 225 nc = nativeCall_overwriting_at( nc->next_instruction_address() );
aoqi@0 226 for (idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
aoqi@0 227 nc->set_destination( cb.insts_begin() + offsets[idx] );
aoqi@0 228 assert(nc->destination() == (cb.insts_begin() + offsets[idx]), "check unit test");
aoqi@0 229 nc->print();
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 nc = nativeCall_before( cb.insts_begin() + 8 );
aoqi@0 233 nc->print();
aoqi@0 234
aoqi@0 235 VM_Version::revert();
aoqi@0 236 #endif
aoqi@0 237 }
aoqi@0 238 // End code for unit testing implementation of NativeCall class
aoqi@0 239
aoqi@0 240 //-------------------------------------------------------------------
aoqi@0 241
aoqi@0 242 #ifdef _LP64
aoqi@0 243
aoqi@0 244 void NativeFarCall::set_destination(address dest) {
aoqi@0 245 // Address materialized in the instruction stream, so nothing to do.
aoqi@0 246 return;
aoqi@0 247 #if 0 // What we'd do if we really did want to change the destination
aoqi@0 248 if (destination() == dest) {
aoqi@0 249 return;
aoqi@0 250 }
aoqi@0 251 ResourceMark rm;
aoqi@0 252 CodeBuffer buf(addr_at(0), instruction_size + 1);
aoqi@0 253 MacroAssembler* _masm = new MacroAssembler(&buf);
aoqi@0 254 // Generate the new sequence
aoqi@0 255 AddressLiteral(dest);
aoqi@0 256 _masm->jumpl_to(dest, O7, O7);
aoqi@0 257 ICache::invalidate_range(addr_at(0), instruction_size );
aoqi@0 258 #endif
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 void NativeFarCall::verify() {
aoqi@0 262 // make sure code pattern is actually a jumpl_to instruction
aoqi@0 263 assert((int)instruction_size == (int)NativeJump::instruction_size, "same as jump_to");
aoqi@0 264 assert((int)jmpl_offset == (int)NativeMovConstReg::add_offset, "sethi size ok");
aoqi@0 265 nativeJump_at(addr_at(0))->verify();
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 bool NativeFarCall::is_call_at(address instr) {
aoqi@0 269 return nativeInstruction_at(instr)->is_sethi();
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 void NativeFarCall::print() {
aoqi@0 273 tty->print_cr(INTPTR_FORMAT ": call " INTPTR_FORMAT, instruction_address(), destination());
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 bool NativeFarCall::destination_is_compiled_verified_entry_point() {
aoqi@0 277 nmethod* callee = CodeCache::find_nmethod(destination());
aoqi@0 278 if (callee == NULL) {
aoqi@0 279 return false;
aoqi@0 280 } else {
aoqi@0 281 return destination() == callee->verified_entry_point();
aoqi@0 282 }
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 // MT-safe patching of a far call.
aoqi@0 286 void NativeFarCall::replace_mt_safe(address instr_addr, address code_buffer) {
aoqi@0 287 Unimplemented();
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 // Code for unit testing implementation of NativeFarCall class
aoqi@0 291 void NativeFarCall::test() {
aoqi@0 292 Unimplemented();
aoqi@0 293 }
aoqi@0 294 // End code for unit testing implementation of NativeFarCall class
aoqi@0 295
aoqi@0 296 #endif // _LP64
aoqi@0 297
aoqi@0 298 //-------------------------------------------------------------------
aoqi@0 299
aoqi@0 300
aoqi@0 301 void NativeMovConstReg::verify() {
aoqi@0 302 NativeInstruction::verify();
aoqi@0 303 // make sure code pattern is actually a "set_metadata" synthetic instruction
aoqi@0 304 // see MacroAssembler::set_oop()
aoqi@0 305 int i0 = long_at(sethi_offset);
aoqi@0 306 int i1 = long_at(add_offset);
aoqi@0 307
aoqi@0 308 // verify the pattern "sethi %hi22(imm), reg ; add reg, %lo10(imm), reg"
aoqi@0 309 Register rd = inv_rd(i0);
aoqi@0 310 #ifndef _LP64
aoqi@0 311 if (!(is_op2(i0, Assembler::sethi_op2) && rd != G0 &&
aoqi@0 312 is_op3(i1, Assembler::add_op3, Assembler::arith_op) &&
aoqi@0 313 inv_immed(i1) && (unsigned)get_simm13(i1) < (1 << 10) &&
aoqi@0 314 rd == inv_rs1(i1) && rd == inv_rd(i1))) {
aoqi@0 315 fatal("not a set_metadata");
aoqi@0 316 }
aoqi@0 317 #else
aoqi@0 318 if (!is_op2(i0, Assembler::sethi_op2) && rd != G0 ) {
aoqi@0 319 fatal("not a set_metadata");
aoqi@0 320 }
aoqi@0 321 #endif
aoqi@0 322 }
aoqi@0 323
aoqi@0 324
aoqi@0 325 void NativeMovConstReg::print() {
aoqi@0 326 tty->print_cr(INTPTR_FORMAT ": mov reg, " INTPTR_FORMAT, instruction_address(), data());
aoqi@0 327 }
aoqi@0 328
aoqi@0 329
aoqi@0 330 #ifdef _LP64
aoqi@0 331 intptr_t NativeMovConstReg::data() const {
aoqi@0 332 return data64(addr_at(sethi_offset), long_at(add_offset));
aoqi@0 333 }
aoqi@0 334 #else
aoqi@0 335 intptr_t NativeMovConstReg::data() const {
aoqi@0 336 return data32(long_at(sethi_offset), long_at(add_offset));
aoqi@0 337 }
aoqi@0 338 #endif
aoqi@0 339
aoqi@0 340
aoqi@0 341 void NativeMovConstReg::set_data(intptr_t x) {
aoqi@0 342 #ifdef _LP64
aoqi@0 343 set_data64_sethi(addr_at(sethi_offset), x);
aoqi@0 344 #else
aoqi@0 345 set_long_at(sethi_offset, set_data32_sethi( long_at(sethi_offset), x));
aoqi@0 346 #endif
aoqi@0 347 set_long_at(add_offset, set_data32_simm13( long_at(add_offset), x));
aoqi@0 348
aoqi@0 349 // also store the value into an oop_Relocation cell, if any
aoqi@0 350 CodeBlob* cb = CodeCache::find_blob(instruction_address());
aoqi@0 351 nmethod* nm = cb ? cb->as_nmethod_or_null() : NULL;
aoqi@0 352 if (nm != NULL) {
aoqi@0 353 RelocIterator iter(nm, instruction_address(), next_instruction_address());
aoqi@0 354 oop* oop_addr = NULL;
aoqi@0 355 Metadata** metadata_addr = NULL;
aoqi@0 356 while (iter.next()) {
aoqi@0 357 if (iter.type() == relocInfo::oop_type) {
aoqi@0 358 oop_Relocation *r = iter.oop_reloc();
aoqi@0 359 if (oop_addr == NULL) {
aoqi@0 360 oop_addr = r->oop_addr();
aoqi@0 361 *oop_addr = cast_to_oop(x);
aoqi@0 362 } else {
aoqi@0 363 assert(oop_addr == r->oop_addr(), "must be only one set-oop here");
aoqi@0 364 }
aoqi@0 365 }
aoqi@0 366 if (iter.type() == relocInfo::metadata_type) {
aoqi@0 367 metadata_Relocation *r = iter.metadata_reloc();
aoqi@0 368 if (metadata_addr == NULL) {
aoqi@0 369 metadata_addr = r->metadata_addr();
aoqi@0 370 *metadata_addr = (Metadata*)x;
aoqi@0 371 } else {
aoqi@0 372 assert(metadata_addr == r->metadata_addr(), "must be only one set-metadata here");
aoqi@0 373 }
aoqi@0 374 }
aoqi@0 375 }
aoqi@0 376 }
aoqi@0 377 }
aoqi@0 378
aoqi@0 379
aoqi@0 380 // Code for unit testing implementation of NativeMovConstReg class
aoqi@0 381 void NativeMovConstReg::test() {
aoqi@0 382 #ifdef ASSERT
aoqi@0 383 ResourceMark rm;
aoqi@0 384 CodeBuffer cb("test", 100, 100);
aoqi@0 385 MacroAssembler* a = new MacroAssembler(&cb);
aoqi@0 386 NativeMovConstReg* nm;
aoqi@0 387 uint idx;
aoqi@0 388 int offsets[] = {
aoqi@0 389 0x0,
aoqi@0 390 0x7fffffff,
aoqi@0 391 0x80000000,
aoqi@0 392 0xffffffff,
aoqi@0 393 0x20,
aoqi@0 394 4096,
aoqi@0 395 4097,
aoqi@0 396 };
aoqi@0 397
aoqi@0 398 VM_Version::allow_all();
aoqi@0 399
aoqi@0 400 AddressLiteral al1(0xaaaabbbb, relocInfo::external_word_type);
aoqi@0 401 a->sethi(al1, I3);
aoqi@0 402 a->add(I3, al1.low10(), I3);
aoqi@0 403 AddressLiteral al2(0xccccdddd, relocInfo::external_word_type);
aoqi@0 404 a->sethi(al2, O2);
aoqi@0 405 a->add(O2, al2.low10(), O2);
aoqi@0 406
aoqi@0 407 nm = nativeMovConstReg_at( cb.insts_begin() );
aoqi@0 408 nm->print();
aoqi@0 409
aoqi@0 410 nm = nativeMovConstReg_at( nm->next_instruction_address() );
aoqi@0 411 for (idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
aoqi@0 412 nm->set_data( offsets[idx] );
aoqi@0 413 assert(nm->data() == offsets[idx], "check unit test");
aoqi@0 414 }
aoqi@0 415 nm->print();
aoqi@0 416
aoqi@0 417 VM_Version::revert();
aoqi@0 418 #endif
aoqi@0 419 }
aoqi@0 420 // End code for unit testing implementation of NativeMovConstReg class
aoqi@0 421
aoqi@0 422 //-------------------------------------------------------------------
aoqi@0 423
aoqi@0 424 void NativeMovConstRegPatching::verify() {
aoqi@0 425 NativeInstruction::verify();
aoqi@0 426 // Make sure code pattern is sethi/nop/add.
aoqi@0 427 int i0 = long_at(sethi_offset);
aoqi@0 428 int i1 = long_at(nop_offset);
aoqi@0 429 int i2 = long_at(add_offset);
aoqi@0 430 assert((int)nop_offset == (int)NativeMovConstReg::add_offset, "sethi size ok");
aoqi@0 431
aoqi@0 432 // Verify the pattern "sethi %hi22(imm), reg; nop; add reg, %lo10(imm), reg"
aoqi@0 433 // The casual reader should note that on Sparc a nop is a special case if sethi
aoqi@0 434 // in which the destination register is %g0.
aoqi@0 435 Register rd0 = inv_rd(i0);
aoqi@0 436 Register rd1 = inv_rd(i1);
aoqi@0 437 if (!(is_op2(i0, Assembler::sethi_op2) && rd0 != G0 &&
aoqi@0 438 is_op2(i1, Assembler::sethi_op2) && rd1 == G0 && // nop is a special case of sethi
aoqi@0 439 is_op3(i2, Assembler::add_op3, Assembler::arith_op) &&
aoqi@0 440 inv_immed(i2) && (unsigned)get_simm13(i2) < (1 << 10) &&
aoqi@0 441 rd0 == inv_rs1(i2) && rd0 == inv_rd(i2))) {
aoqi@0 442 fatal("not a set_metadata");
aoqi@0 443 }
aoqi@0 444 }
aoqi@0 445
aoqi@0 446
aoqi@0 447 void NativeMovConstRegPatching::print() {
aoqi@0 448 tty->print_cr(INTPTR_FORMAT ": mov reg, " INTPTR_FORMAT, instruction_address(), data());
aoqi@0 449 }
aoqi@0 450
aoqi@0 451
aoqi@0 452 int NativeMovConstRegPatching::data() const {
aoqi@0 453 #ifdef _LP64
aoqi@0 454 return data64(addr_at(sethi_offset), long_at(add_offset));
aoqi@0 455 #else
aoqi@0 456 return data32(long_at(sethi_offset), long_at(add_offset));
aoqi@0 457 #endif
aoqi@0 458 }
aoqi@0 459
aoqi@0 460
aoqi@0 461 void NativeMovConstRegPatching::set_data(int x) {
aoqi@0 462 #ifdef _LP64
aoqi@0 463 set_data64_sethi(addr_at(sethi_offset), x);
aoqi@0 464 #else
aoqi@0 465 set_long_at(sethi_offset, set_data32_sethi(long_at(sethi_offset), x));
aoqi@0 466 #endif
aoqi@0 467 set_long_at(add_offset, set_data32_simm13(long_at(add_offset), x));
aoqi@0 468
aoqi@0 469 // also store the value into an oop_Relocation cell, if any
aoqi@0 470 CodeBlob* cb = CodeCache::find_blob(instruction_address());
aoqi@0 471 nmethod* nm = cb ? cb->as_nmethod_or_null() : NULL;
aoqi@0 472 if (nm != NULL) {
aoqi@0 473 RelocIterator iter(nm, instruction_address(), next_instruction_address());
aoqi@0 474 oop* oop_addr = NULL;
aoqi@0 475 Metadata** metadata_addr = NULL;
aoqi@0 476 while (iter.next()) {
aoqi@0 477 if (iter.type() == relocInfo::oop_type) {
aoqi@0 478 oop_Relocation *r = iter.oop_reloc();
aoqi@0 479 if (oop_addr == NULL) {
aoqi@0 480 oop_addr = r->oop_addr();
aoqi@0 481 *oop_addr = cast_to_oop(x);
aoqi@0 482 } else {
aoqi@0 483 assert(oop_addr == r->oop_addr(), "must be only one set-oop here");
aoqi@0 484 }
aoqi@0 485 }
aoqi@0 486 if (iter.type() == relocInfo::metadata_type) {
aoqi@0 487 metadata_Relocation *r = iter.metadata_reloc();
aoqi@0 488 if (metadata_addr == NULL) {
aoqi@0 489 metadata_addr = r->metadata_addr();
aoqi@0 490 *metadata_addr = (Metadata*)x;
aoqi@0 491 } else {
aoqi@0 492 assert(metadata_addr == r->metadata_addr(), "must be only one set-metadata here");
aoqi@0 493 }
aoqi@0 494 }
aoqi@0 495 }
aoqi@0 496 }
aoqi@0 497 }
aoqi@0 498
aoqi@0 499
aoqi@0 500 // Code for unit testing implementation of NativeMovConstRegPatching class
aoqi@0 501 void NativeMovConstRegPatching::test() {
aoqi@0 502 #ifdef ASSERT
aoqi@0 503 ResourceMark rm;
aoqi@0 504 CodeBuffer cb("test", 100, 100);
aoqi@0 505 MacroAssembler* a = new MacroAssembler(&cb);
aoqi@0 506 NativeMovConstRegPatching* nm;
aoqi@0 507 uint idx;
aoqi@0 508 int offsets[] = {
aoqi@0 509 0x0,
aoqi@0 510 0x7fffffff,
aoqi@0 511 0x80000000,
aoqi@0 512 0xffffffff,
aoqi@0 513 0x20,
aoqi@0 514 4096,
aoqi@0 515 4097,
aoqi@0 516 };
aoqi@0 517
aoqi@0 518 VM_Version::allow_all();
aoqi@0 519
aoqi@0 520 AddressLiteral al1(0xaaaabbbb, relocInfo::external_word_type);
aoqi@0 521 a->sethi(al1, I3);
aoqi@0 522 a->nop();
aoqi@0 523 a->add(I3, al1.low10(), I3);
aoqi@0 524 AddressLiteral al2(0xccccdddd, relocInfo::external_word_type);
aoqi@0 525 a->sethi(al2, O2);
aoqi@0 526 a->nop();
aoqi@0 527 a->add(O2, al2.low10(), O2);
aoqi@0 528
aoqi@0 529 nm = nativeMovConstRegPatching_at( cb.insts_begin() );
aoqi@0 530 nm->print();
aoqi@0 531
aoqi@0 532 nm = nativeMovConstRegPatching_at( nm->next_instruction_address() );
aoqi@0 533 for (idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
aoqi@0 534 nm->set_data( offsets[idx] );
aoqi@0 535 assert(nm->data() == offsets[idx], "check unit test");
aoqi@0 536 }
aoqi@0 537 nm->print();
aoqi@0 538
aoqi@0 539 VM_Version::revert();
aoqi@0 540 #endif // ASSERT
aoqi@0 541 }
aoqi@0 542 // End code for unit testing implementation of NativeMovConstRegPatching class
aoqi@0 543
aoqi@0 544
aoqi@0 545 //-------------------------------------------------------------------
aoqi@0 546
aoqi@0 547
aoqi@0 548 void NativeMovRegMem::copy_instruction_to(address new_instruction_address) {
aoqi@0 549 Untested("copy_instruction_to");
aoqi@0 550 int instruction_size = next_instruction_address() - instruction_address();
aoqi@0 551 for (int i = 0; i < instruction_size; i += BytesPerInstWord) {
aoqi@0 552 *(int*)(new_instruction_address + i) = *(int*)(address(this) + i);
aoqi@0 553 }
aoqi@0 554 }
aoqi@0 555
aoqi@0 556
aoqi@0 557 void NativeMovRegMem::verify() {
aoqi@0 558 NativeInstruction::verify();
aoqi@0 559 // make sure code pattern is actually a "ld" or "st" of some sort.
aoqi@0 560 int i0 = long_at(0);
aoqi@0 561 int op3 = inv_op3(i0);
aoqi@0 562
aoqi@0 563 assert((int)add_offset == NativeMovConstReg::add_offset, "sethi size ok");
aoqi@0 564
aoqi@0 565 if (!(is_op(i0, Assembler::ldst_op) &&
aoqi@0 566 inv_immed(i0) &&
aoqi@0 567 0 != (op3 < op3_ldst_int_limit
aoqi@0 568 ? (1 << op3 ) & (op3_mask_ld | op3_mask_st)
aoqi@0 569 : (1 << (op3 - op3_ldst_int_limit)) & (op3_mask_ldf | op3_mask_stf))))
aoqi@0 570 {
aoqi@0 571 int i1 = long_at(ldst_offset);
aoqi@0 572 Register rd = inv_rd(i0);
aoqi@0 573
aoqi@0 574 op3 = inv_op3(i1);
aoqi@0 575 if (!is_op(i1, Assembler::ldst_op) && rd == inv_rs2(i1) &&
aoqi@0 576 0 != (op3 < op3_ldst_int_limit
aoqi@0 577 ? (1 << op3 ) & (op3_mask_ld | op3_mask_st)
aoqi@0 578 : (1 << (op3 - op3_ldst_int_limit)) & (op3_mask_ldf | op3_mask_stf))) {
aoqi@0 579 fatal("not a ld* or st* op");
aoqi@0 580 }
aoqi@0 581 }
aoqi@0 582 }
aoqi@0 583
aoqi@0 584
aoqi@0 585 void NativeMovRegMem::print() {
aoqi@0 586 if (is_immediate()) {
aoqi@0 587 tty->print_cr(INTPTR_FORMAT ": mov reg, [reg + %x]", instruction_address(), offset());
aoqi@0 588 } else {
aoqi@0 589 tty->print_cr(INTPTR_FORMAT ": mov reg, [reg + reg]", instruction_address());
aoqi@0 590 }
aoqi@0 591 }
aoqi@0 592
aoqi@0 593
aoqi@0 594 // Code for unit testing implementation of NativeMovRegMem class
aoqi@0 595 void NativeMovRegMem::test() {
aoqi@0 596 #ifdef ASSERT
aoqi@0 597 ResourceMark rm;
aoqi@0 598 CodeBuffer cb("test", 1000, 1000);
aoqi@0 599 MacroAssembler* a = new MacroAssembler(&cb);
aoqi@0 600 NativeMovRegMem* nm;
aoqi@0 601 uint idx = 0;
aoqi@0 602 uint idx1;
aoqi@0 603 int offsets[] = {
aoqi@0 604 0x0,
aoqi@0 605 0xffffffff,
aoqi@0 606 0x7fffffff,
aoqi@0 607 0x80000000,
aoqi@0 608 4096,
aoqi@0 609 4097,
aoqi@0 610 0x20,
aoqi@0 611 0x4000,
aoqi@0 612 };
aoqi@0 613
aoqi@0 614 VM_Version::allow_all();
aoqi@0 615
aoqi@0 616 AddressLiteral al1(0xffffffff, relocInfo::external_word_type);
aoqi@0 617 AddressLiteral al2(0xaaaabbbb, relocInfo::external_word_type);
aoqi@0 618 a->ldsw( G5, al1.low10(), G4 ); idx++;
aoqi@0 619 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 620 a->ldsw( G5, I3, G4 ); idx++;
aoqi@0 621 a->ldsb( G5, al1.low10(), G4 ); idx++;
aoqi@0 622 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 623 a->ldsb( G5, I3, G4 ); idx++;
aoqi@0 624 a->ldsh( G5, al1.low10(), G4 ); idx++;
aoqi@0 625 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 626 a->ldsh( G5, I3, G4 ); idx++;
aoqi@0 627 a->lduw( G5, al1.low10(), G4 ); idx++;
aoqi@0 628 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 629 a->lduw( G5, I3, G4 ); idx++;
aoqi@0 630 a->ldub( G5, al1.low10(), G4 ); idx++;
aoqi@0 631 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 632 a->ldub( G5, I3, G4 ); idx++;
aoqi@0 633 a->lduh( G5, al1.low10(), G4 ); idx++;
aoqi@0 634 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 635 a->lduh( G5, I3, G4 ); idx++;
aoqi@0 636 a->ldx( G5, al1.low10(), G4 ); idx++;
aoqi@0 637 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 638 a->ldx( G5, I3, G4 ); idx++;
aoqi@0 639 a->ldd( G5, al1.low10(), G4 ); idx++;
aoqi@0 640 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 641 a->ldd( G5, I3, G4 ); idx++;
aoqi@0 642 a->ldf( FloatRegisterImpl::D, O2, -1, F14 ); idx++;
aoqi@0 643 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 644 a->ldf( FloatRegisterImpl::S, O0, I3, F15 ); idx++;
aoqi@0 645
aoqi@0 646 a->stw( G5, G4, al1.low10() ); idx++;
aoqi@0 647 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 648 a->stw( G5, G4, I3 ); idx++;
aoqi@0 649 a->stb( G5, G4, al1.low10() ); idx++;
aoqi@0 650 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 651 a->stb( G5, G4, I3 ); idx++;
aoqi@0 652 a->sth( G5, G4, al1.low10() ); idx++;
aoqi@0 653 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 654 a->sth( G5, G4, I3 ); idx++;
aoqi@0 655 a->stx( G5, G4, al1.low10() ); idx++;
aoqi@0 656 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 657 a->stx( G5, G4, I3 ); idx++;
aoqi@0 658 a->std( G5, G4, al1.low10() ); idx++;
aoqi@0 659 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 660 a->std( G5, G4, I3 ); idx++;
aoqi@0 661 a->stf( FloatRegisterImpl::S, F18, O2, -1 ); idx++;
aoqi@0 662 a->sethi(al2, I3); a->add(I3, al2.low10(), I3);
aoqi@0 663 a->stf( FloatRegisterImpl::S, F15, O0, I3 ); idx++;
aoqi@0 664
aoqi@0 665 nm = nativeMovRegMem_at( cb.insts_begin() );
aoqi@0 666 nm->print();
aoqi@0 667 nm->set_offset( low10(0) );
aoqi@0 668 nm->print();
aoqi@0 669 nm->add_offset_in_bytes( low10(0xbb) * wordSize );
aoqi@0 670 nm->print();
aoqi@0 671
aoqi@0 672 while (--idx) {
aoqi@0 673 nm = nativeMovRegMem_at( nm->next_instruction_address() );
aoqi@0 674 nm->print();
aoqi@0 675 for (idx1 = 0; idx1 < ARRAY_SIZE(offsets); idx1++) {
aoqi@0 676 nm->set_offset( nm->is_immediate() ? low10(offsets[idx1]) : offsets[idx1] );
aoqi@0 677 assert(nm->offset() == (nm->is_immediate() ? low10(offsets[idx1]) : offsets[idx1]),
aoqi@0 678 "check unit test");
aoqi@0 679 nm->print();
aoqi@0 680 }
aoqi@0 681 nm->add_offset_in_bytes( low10(0xbb) * wordSize );
aoqi@0 682 nm->print();
aoqi@0 683 }
aoqi@0 684
aoqi@0 685 VM_Version::revert();
aoqi@0 686 #endif // ASSERT
aoqi@0 687 }
aoqi@0 688
aoqi@0 689 // End code for unit testing implementation of NativeMovRegMem class
aoqi@0 690
aoqi@0 691 //--------------------------------------------------------------------------------
aoqi@0 692
aoqi@0 693
aoqi@0 694 void NativeMovRegMemPatching::copy_instruction_to(address new_instruction_address) {
aoqi@0 695 Untested("copy_instruction_to");
aoqi@0 696 int instruction_size = next_instruction_address() - instruction_address();
aoqi@0 697 for (int i = 0; i < instruction_size; i += wordSize) {
aoqi@0 698 *(long*)(new_instruction_address + i) = *(long*)(address(this) + i);
aoqi@0 699 }
aoqi@0 700 }
aoqi@0 701
aoqi@0 702
aoqi@0 703 void NativeMovRegMemPatching::verify() {
aoqi@0 704 NativeInstruction::verify();
aoqi@0 705 // make sure code pattern is actually a "ld" or "st" of some sort.
aoqi@0 706 int i0 = long_at(0);
aoqi@0 707 int op3 = inv_op3(i0);
aoqi@0 708
aoqi@0 709 assert((int)nop_offset == (int)NativeMovConstReg::add_offset, "sethi size ok");
aoqi@0 710
aoqi@0 711 if (!(is_op(i0, Assembler::ldst_op) &&
aoqi@0 712 inv_immed(i0) &&
aoqi@0 713 0 != (op3 < op3_ldst_int_limit
aoqi@0 714 ? (1 << op3 ) & (op3_mask_ld | op3_mask_st)
aoqi@0 715 : (1 << (op3 - op3_ldst_int_limit)) & (op3_mask_ldf | op3_mask_stf)))) {
aoqi@0 716 int i1 = long_at(ldst_offset);
aoqi@0 717 Register rd = inv_rd(i0);
aoqi@0 718
aoqi@0 719 op3 = inv_op3(i1);
aoqi@0 720 if (!is_op(i1, Assembler::ldst_op) && rd == inv_rs2(i1) &&
aoqi@0 721 0 != (op3 < op3_ldst_int_limit
aoqi@0 722 ? (1 << op3 ) & (op3_mask_ld | op3_mask_st)
aoqi@0 723 : (1 << (op3 - op3_ldst_int_limit)) & (op3_mask_ldf | op3_mask_stf))) {
aoqi@0 724 fatal("not a ld* or st* op");
aoqi@0 725 }
aoqi@0 726 }
aoqi@0 727 }
aoqi@0 728
aoqi@0 729
aoqi@0 730 void NativeMovRegMemPatching::print() {
aoqi@0 731 if (is_immediate()) {
aoqi@0 732 tty->print_cr(INTPTR_FORMAT ": mov reg, [reg + %x]", instruction_address(), offset());
aoqi@0 733 } else {
aoqi@0 734 tty->print_cr(INTPTR_FORMAT ": mov reg, [reg + reg]", instruction_address());
aoqi@0 735 }
aoqi@0 736 }
aoqi@0 737
aoqi@0 738
aoqi@0 739 // Code for unit testing implementation of NativeMovRegMemPatching class
aoqi@0 740 void NativeMovRegMemPatching::test() {
aoqi@0 741 #ifdef ASSERT
aoqi@0 742 ResourceMark rm;
aoqi@0 743 CodeBuffer cb("test", 1000, 1000);
aoqi@0 744 MacroAssembler* a = new MacroAssembler(&cb);
aoqi@0 745 NativeMovRegMemPatching* nm;
aoqi@0 746 uint idx = 0;
aoqi@0 747 uint idx1;
aoqi@0 748 int offsets[] = {
aoqi@0 749 0x0,
aoqi@0 750 0xffffffff,
aoqi@0 751 0x7fffffff,
aoqi@0 752 0x80000000,
aoqi@0 753 4096,
aoqi@0 754 4097,
aoqi@0 755 0x20,
aoqi@0 756 0x4000,
aoqi@0 757 };
aoqi@0 758
aoqi@0 759 VM_Version::allow_all();
aoqi@0 760
aoqi@0 761 AddressLiteral al(0xffffffff, relocInfo::external_word_type);
aoqi@0 762 a->ldsw( G5, al.low10(), G4); idx++;
aoqi@0 763 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 764 a->ldsw( G5, I3, G4 ); idx++;
aoqi@0 765 a->ldsb( G5, al.low10(), G4); idx++;
aoqi@0 766 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 767 a->ldsb( G5, I3, G4 ); idx++;
aoqi@0 768 a->ldsh( G5, al.low10(), G4); idx++;
aoqi@0 769 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 770 a->ldsh( G5, I3, G4 ); idx++;
aoqi@0 771 a->lduw( G5, al.low10(), G4); idx++;
aoqi@0 772 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 773 a->lduw( G5, I3, G4 ); idx++;
aoqi@0 774 a->ldub( G5, al.low10(), G4); idx++;
aoqi@0 775 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 776 a->ldub( G5, I3, G4 ); idx++;
aoqi@0 777 a->lduh( G5, al.low10(), G4); idx++;
aoqi@0 778 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 779 a->lduh( G5, I3, G4 ); idx++;
aoqi@0 780 a->ldx( G5, al.low10(), G4); idx++;
aoqi@0 781 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 782 a->ldx( G5, I3, G4 ); idx++;
aoqi@0 783 a->ldd( G5, al.low10(), G4); idx++;
aoqi@0 784 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 785 a->ldd( G5, I3, G4 ); idx++;
aoqi@0 786 a->ldf( FloatRegisterImpl::D, O2, -1, F14 ); idx++;
aoqi@0 787 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 788 a->ldf( FloatRegisterImpl::S, O0, I3, F15 ); idx++;
aoqi@0 789
aoqi@0 790 a->stw( G5, G4, al.low10()); idx++;
aoqi@0 791 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 792 a->stw( G5, G4, I3 ); idx++;
aoqi@0 793 a->stb( G5, G4, al.low10()); idx++;
aoqi@0 794 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 795 a->stb( G5, G4, I3 ); idx++;
aoqi@0 796 a->sth( G5, G4, al.low10()); idx++;
aoqi@0 797 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 798 a->sth( G5, G4, I3 ); idx++;
aoqi@0 799 a->stx( G5, G4, al.low10()); idx++;
aoqi@0 800 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 801 a->stx( G5, G4, I3 ); idx++;
aoqi@0 802 a->std( G5, G4, al.low10()); idx++;
aoqi@0 803 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 804 a->std( G5, G4, I3 ); idx++;
aoqi@0 805 a->stf( FloatRegisterImpl::S, F18, O2, -1 ); idx++;
aoqi@0 806 a->sethi(al, I3); a->nop(); a->add(I3, al.low10(), I3);
aoqi@0 807 a->stf( FloatRegisterImpl::S, F15, O0, I3 ); idx++;
aoqi@0 808
aoqi@0 809 nm = nativeMovRegMemPatching_at( cb.insts_begin() );
aoqi@0 810 nm->print();
aoqi@0 811 nm->set_offset( low10(0) );
aoqi@0 812 nm->print();
aoqi@0 813 nm->add_offset_in_bytes( low10(0xbb) * wordSize );
aoqi@0 814 nm->print();
aoqi@0 815
aoqi@0 816 while (--idx) {
aoqi@0 817 nm = nativeMovRegMemPatching_at( nm->next_instruction_address() );
aoqi@0 818 nm->print();
aoqi@0 819 for (idx1 = 0; idx1 < ARRAY_SIZE(offsets); idx1++) {
aoqi@0 820 nm->set_offset( nm->is_immediate() ? low10(offsets[idx1]) : offsets[idx1] );
aoqi@0 821 assert(nm->offset() == (nm->is_immediate() ? low10(offsets[idx1]) : offsets[idx1]),
aoqi@0 822 "check unit test");
aoqi@0 823 nm->print();
aoqi@0 824 }
aoqi@0 825 nm->add_offset_in_bytes( low10(0xbb) * wordSize );
aoqi@0 826 nm->print();
aoqi@0 827 }
aoqi@0 828
aoqi@0 829 VM_Version::revert();
aoqi@0 830 #endif // ASSERT
aoqi@0 831 }
aoqi@0 832 // End code for unit testing implementation of NativeMovRegMemPatching class
aoqi@0 833
aoqi@0 834
aoqi@0 835 //--------------------------------------------------------------------------------
aoqi@0 836
aoqi@0 837
aoqi@0 838 void NativeJump::verify() {
aoqi@0 839 NativeInstruction::verify();
aoqi@0 840 int i0 = long_at(sethi_offset);
aoqi@0 841 int i1 = long_at(jmpl_offset);
aoqi@0 842 assert((int)jmpl_offset == (int)NativeMovConstReg::add_offset, "sethi size ok");
aoqi@0 843 // verify the pattern "sethi %hi22(imm), treg ; jmpl treg, %lo10(imm), lreg"
aoqi@0 844 Register rd = inv_rd(i0);
aoqi@0 845 #ifndef _LP64
aoqi@0 846 if (!(is_op2(i0, Assembler::sethi_op2) && rd != G0 &&
aoqi@0 847 (is_op3(i1, Assembler::jmpl_op3, Assembler::arith_op) ||
aoqi@0 848 (TraceJumps && is_op3(i1, Assembler::add_op3, Assembler::arith_op))) &&
aoqi@0 849 inv_immed(i1) && (unsigned)get_simm13(i1) < (1 << 10) &&
aoqi@0 850 rd == inv_rs1(i1))) {
aoqi@0 851 fatal("not a jump_to instruction");
aoqi@0 852 }
aoqi@0 853 #else
aoqi@0 854 // In LP64, the jump instruction location varies for non relocatable
aoqi@0 855 // jumps, for example is could be sethi, xor, jmp instead of the
aoqi@0 856 // 7 instructions for sethi. So let's check sethi only.
aoqi@0 857 if (!is_op2(i0, Assembler::sethi_op2) && rd != G0 ) {
aoqi@0 858 fatal("not a jump_to instruction");
aoqi@0 859 }
aoqi@0 860 #endif
aoqi@0 861 }
aoqi@0 862
aoqi@0 863
aoqi@0 864 void NativeJump::print() {
aoqi@0 865 tty->print_cr(INTPTR_FORMAT ": jmpl reg, " INTPTR_FORMAT, instruction_address(), jump_destination());
aoqi@0 866 }
aoqi@0 867
aoqi@0 868
aoqi@0 869 // Code for unit testing implementation of NativeJump class
aoqi@0 870 void NativeJump::test() {
aoqi@0 871 #ifdef ASSERT
aoqi@0 872 ResourceMark rm;
aoqi@0 873 CodeBuffer cb("test", 100, 100);
aoqi@0 874 MacroAssembler* a = new MacroAssembler(&cb);
aoqi@0 875 NativeJump* nj;
aoqi@0 876 uint idx;
aoqi@0 877 int offsets[] = {
aoqi@0 878 0x0,
aoqi@0 879 0xffffffff,
aoqi@0 880 0x7fffffff,
aoqi@0 881 0x80000000,
aoqi@0 882 4096,
aoqi@0 883 4097,
aoqi@0 884 0x20,
aoqi@0 885 0x4000,
aoqi@0 886 };
aoqi@0 887
aoqi@0 888 VM_Version::allow_all();
aoqi@0 889
aoqi@0 890 AddressLiteral al(0x7fffbbbb, relocInfo::external_word_type);
aoqi@0 891 a->sethi(al, I3);
aoqi@0 892 a->jmpl(I3, al.low10(), G0, RelocationHolder::none);
aoqi@0 893 a->delayed()->nop();
aoqi@0 894 a->sethi(al, I3);
aoqi@0 895 a->jmpl(I3, al.low10(), L3, RelocationHolder::none);
aoqi@0 896 a->delayed()->nop();
aoqi@0 897
aoqi@0 898 nj = nativeJump_at( cb.insts_begin() );
aoqi@0 899 nj->print();
aoqi@0 900
aoqi@0 901 nj = nativeJump_at( nj->next_instruction_address() );
aoqi@0 902 for (idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
aoqi@0 903 nj->set_jump_destination( nj->instruction_address() + offsets[idx] );
aoqi@0 904 assert(nj->jump_destination() == (nj->instruction_address() + offsets[idx]), "check unit test");
aoqi@0 905 nj->print();
aoqi@0 906 }
aoqi@0 907
aoqi@0 908 VM_Version::revert();
aoqi@0 909 #endif // ASSERT
aoqi@0 910 }
aoqi@0 911 // End code for unit testing implementation of NativeJump class
aoqi@0 912
aoqi@0 913
aoqi@0 914 void NativeJump::insert(address code_pos, address entry) {
aoqi@0 915 Unimplemented();
aoqi@0 916 }
aoqi@0 917
aoqi@0 918 // MT safe inserting of a jump over an unknown instruction sequence (used by nmethod::makeZombie)
aoqi@0 919 // The problem: jump_to <dest> is a 3-word instruction (including its delay slot).
aoqi@0 920 // Atomic write can be only with 1 word.
aoqi@0 921 void NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) {
aoqi@0 922 // Here's one way to do it: Pre-allocate a three-word jump sequence somewhere
aoqi@0 923 // in the header of the nmethod, within a short branch's span of the patch point.
aoqi@0 924 // Set up the jump sequence using NativeJump::insert, and then use an annulled
aoqi@0 925 // unconditional branch at the target site (an atomic 1-word update).
aoqi@0 926 // Limitations: You can only patch nmethods, with any given nmethod patched at
aoqi@0 927 // most once, and the patch must be in the nmethod's header.
aoqi@0 928 // It's messy, but you can ask the CodeCache for the nmethod containing the
aoqi@0 929 // target address.
aoqi@0 930
aoqi@0 931 // %%%%% For now, do something MT-stupid:
aoqi@0 932 ResourceMark rm;
aoqi@0 933 int code_size = 1 * BytesPerInstWord;
aoqi@0 934 CodeBuffer cb(verified_entry, code_size + 1);
aoqi@0 935 MacroAssembler* a = new MacroAssembler(&cb);
aoqi@0 936 a->ldsw(G0, 0, O7); // "ld" must agree with code in the signal handler
aoqi@0 937 ICache::invalidate_range(verified_entry, code_size);
aoqi@0 938 }
aoqi@0 939
aoqi@0 940
aoqi@0 941 void NativeIllegalInstruction::insert(address code_pos) {
aoqi@0 942 NativeIllegalInstruction* nii = (NativeIllegalInstruction*) nativeInstruction_at(code_pos);
aoqi@0 943 nii->set_long_at(0, illegal_instruction());
aoqi@0 944 }
aoqi@0 945
aoqi@0 946 static int illegal_instruction_bits = 0;
aoqi@0 947
aoqi@0 948 int NativeInstruction::illegal_instruction() {
aoqi@0 949 if (illegal_instruction_bits == 0) {
aoqi@0 950 ResourceMark rm;
aoqi@0 951 char buf[40];
aoqi@0 952 CodeBuffer cbuf((address)&buf[0], 20);
aoqi@0 953 MacroAssembler* a = new MacroAssembler(&cbuf);
aoqi@0 954 address ia = a->pc();
aoqi@0 955 a->trap(ST_RESERVED_FOR_USER_0 + 1);
aoqi@0 956 int bits = *(int*)ia;
aoqi@0 957 assert(is_op3(bits, Assembler::trap_op3, Assembler::arith_op), "bad instruction");
aoqi@0 958 illegal_instruction_bits = bits;
aoqi@0 959 assert(illegal_instruction_bits != 0, "oops");
aoqi@0 960 }
aoqi@0 961 return illegal_instruction_bits;
aoqi@0 962 }
aoqi@0 963
aoqi@0 964 static int ic_miss_trap_bits = 0;
aoqi@0 965
aoqi@0 966 bool NativeInstruction::is_ic_miss_trap() {
aoqi@0 967 if (ic_miss_trap_bits == 0) {
aoqi@0 968 ResourceMark rm;
aoqi@0 969 char buf[40];
aoqi@0 970 CodeBuffer cbuf((address)&buf[0], 20);
aoqi@0 971 MacroAssembler* a = new MacroAssembler(&cbuf);
aoqi@0 972 address ia = a->pc();
aoqi@0 973 a->trap(Assembler::notEqual, Assembler::ptr_cc, G0, ST_RESERVED_FOR_USER_0 + 2);
aoqi@0 974 int bits = *(int*)ia;
aoqi@0 975 assert(is_op3(bits, Assembler::trap_op3, Assembler::arith_op), "bad instruction");
aoqi@0 976 ic_miss_trap_bits = bits;
aoqi@0 977 assert(ic_miss_trap_bits != 0, "oops");
aoqi@0 978 }
aoqi@0 979 return long_at(0) == ic_miss_trap_bits;
aoqi@0 980 }
aoqi@0 981
aoqi@0 982
aoqi@0 983 bool NativeInstruction::is_illegal() {
aoqi@0 984 if (illegal_instruction_bits == 0) {
aoqi@0 985 return false;
aoqi@0 986 }
aoqi@0 987 return long_at(0) == illegal_instruction_bits;
aoqi@0 988 }
aoqi@0 989
aoqi@0 990
aoqi@0 991 void NativeGeneralJump::verify() {
aoqi@0 992 assert(((NativeInstruction *)this)->is_jump() ||
aoqi@0 993 ((NativeInstruction *)this)->is_cond_jump(), "not a general jump instruction");
aoqi@0 994 }
aoqi@0 995
aoqi@0 996
aoqi@0 997 void NativeGeneralJump::insert_unconditional(address code_pos, address entry) {
aoqi@0 998 Assembler::Condition condition = Assembler::always;
aoqi@0 999 int x = Assembler::op2(Assembler::br_op2) | Assembler::annul(false) |
aoqi@0 1000 Assembler::cond(condition) | Assembler::wdisp((intptr_t)entry, (intptr_t)code_pos, 22);
aoqi@0 1001 NativeGeneralJump* ni = (NativeGeneralJump*) nativeInstruction_at(code_pos);
aoqi@0 1002 ni->set_long_at(0, x);
aoqi@0 1003 }
aoqi@0 1004
aoqi@0 1005
aoqi@0 1006 // MT-safe patching of a jmp instruction (and following word).
aoqi@0 1007 // First patches the second word, and then atomicly replaces
aoqi@0 1008 // the first word with the first new instruction word.
aoqi@0 1009 // Other processors might briefly see the old first word
aoqi@0 1010 // followed by the new second word. This is OK if the old
aoqi@0 1011 // second word is harmless, and the new second word may be
aoqi@0 1012 // harmlessly executed in the delay slot of the call.
aoqi@0 1013 void NativeGeneralJump::replace_mt_safe(address instr_addr, address code_buffer) {
aoqi@0 1014 assert(Patching_lock->is_locked() ||
aoqi@0 1015 SafepointSynchronize::is_at_safepoint(), "concurrent code patching");
aoqi@0 1016 assert (instr_addr != NULL, "illegal address for code patching");
aoqi@0 1017 NativeGeneralJump* h_jump = nativeGeneralJump_at (instr_addr); // checking that it is a call
aoqi@0 1018 assert(NativeGeneralJump::instruction_size == 8, "wrong instruction size; must be 8");
aoqi@0 1019 int i0 = ((int*)code_buffer)[0];
aoqi@0 1020 int i1 = ((int*)code_buffer)[1];
aoqi@0 1021 int* contention_addr = (int*) h_jump->addr_at(1*BytesPerInstWord);
aoqi@0 1022 assert(inv_op(*contention_addr) == Assembler::arith_op ||
aoqi@0 1023 *contention_addr == nop_instruction(),
aoqi@0 1024 "must not interfere with original call");
aoqi@0 1025 // The set_long_at calls do the ICacheInvalidate so we just need to do them in reverse order
aoqi@0 1026 h_jump->set_long_at(1*BytesPerInstWord, i1);
aoqi@0 1027 h_jump->set_long_at(0*BytesPerInstWord, i0);
aoqi@0 1028 // NOTE: It is possible that another thread T will execute
aoqi@0 1029 // only the second patched word.
aoqi@0 1030 // In other words, since the original instruction is this
aoqi@0 1031 // jmp patching_stub; nop (NativeGeneralJump)
aoqi@0 1032 // and the new sequence from the buffer is this:
aoqi@0 1033 // sethi %hi(K), %r; add %r, %lo(K), %r (NativeMovConstReg)
aoqi@0 1034 // what T will execute is this:
aoqi@0 1035 // jmp patching_stub; add %r, %lo(K), %r
aoqi@0 1036 // thereby putting garbage into %r before calling the patching stub.
aoqi@0 1037 // This is OK, because the patching stub ignores the value of %r.
aoqi@0 1038
aoqi@0 1039 // Make sure the first-patched instruction, which may co-exist
aoqi@0 1040 // briefly with the call, will do something harmless.
aoqi@0 1041 assert(inv_op(*contention_addr) == Assembler::arith_op ||
aoqi@0 1042 *contention_addr == nop_instruction(),
aoqi@0 1043 "must not interfere with original call");
aoqi@0 1044 }

mercurial