src/cpu/ppc/vm/nativeInst_ppc.cpp

Wed, 27 Nov 2013 16:16:21 -0800

author
goetz
date
Wed, 27 Nov 2013 16:16:21 -0800
changeset 6490
41b780b43b74
parent 6483
018b357638aa
child 6495
67fa91961822
permissions
-rw-r--r--

8029015: PPC64 (part 216): opto: trap based null and range checks
Summary: On PPC64 use tdi instruction that does a compare and raises SIGTRAP for NULL and range checks.
Reviewed-by: kvn

goetz@6458 1 /*
goetz@6458 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
goetz@6458 3 * Copyright 2012, 2013 SAP AG. All rights reserved.
goetz@6458 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
goetz@6458 5 *
goetz@6458 6 * This code is free software; you can redistribute it and/or modify it
goetz@6458 7 * under the terms of the GNU General Public License version 2 only, as
goetz@6458 8 * published by the Free Software Foundation.
goetz@6458 9 *
goetz@6458 10 * This code is distributed in the hope that it will be useful, but WITHOUT
goetz@6458 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
goetz@6458 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
goetz@6458 13 * version 2 for more details (a copy is included in the LICENSE file that
goetz@6458 14 * accompanied this code).
goetz@6458 15 *
goetz@6458 16 * You should have received a copy of the GNU General Public License version
goetz@6458 17 * 2 along with this work; if not, write to the Free Software Foundation,
goetz@6458 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
goetz@6458 19 *
goetz@6458 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
goetz@6458 21 * or visit www.oracle.com if you need additional information or have any
goetz@6458 22 * questions.
goetz@6458 23 *
goetz@6458 24 */
goetz@6458 25
goetz@6458 26 #include "precompiled.hpp"
goetz@6458 27 #include "asm/macroAssembler.inline.hpp"
goetz@6458 28 #include "memory/resourceArea.hpp"
goetz@6458 29 #include "nativeInst_ppc.hpp"
goetz@6458 30 #include "oops/oop.inline.hpp"
goetz@6458 31 #include "runtime/handles.hpp"
goetz@6458 32 #include "runtime/sharedRuntime.hpp"
goetz@6458 33 #include "runtime/stubRoutines.hpp"
goetz@6458 34 #include "utilities/ostream.hpp"
goetz@6458 35 #ifdef COMPILER1
goetz@6458 36 #include "c1/c1_Runtime1.hpp"
goetz@6458 37 #endif
goetz@6458 38
goetz@6458 39 // We use an illtrap for marking a method as not_entrant or zombie iff !UseSIGTRAP
goetz@6458 40 // Work around a C++ compiler bug which changes 'this'
goetz@6458 41 bool NativeInstruction::is_sigill_zombie_not_entrant_at(address addr) {
goetz@6458 42 assert(!UseSIGTRAP, "precondition");
goetz@6458 43 if (*(int*)addr != 0 /*illtrap*/) return false;
goetz@6458 44 CodeBlob* cb = CodeCache::find_blob_unsafe(addr);
goetz@6458 45 if (cb == NULL || !cb->is_nmethod()) return false;
goetz@6458 46 nmethod *nm = (nmethod *)cb;
goetz@6458 47 // This method is not_entrant or zombie iff the illtrap instruction is
goetz@6458 48 // located at the verified entry point.
goetz@6458 49 return nm->verified_entry_point() == addr;
goetz@6458 50 }
goetz@6458 51
goetz@6458 52 #ifdef ASSERT
goetz@6458 53 void NativeInstruction::verify() {
goetz@6458 54 // Make sure code pattern is actually an instruction address.
goetz@6458 55 address addr = addr_at(0);
goetz@6458 56 if (addr == 0 || ((intptr_t)addr & 3) != 0) {
goetz@6458 57 fatal("not an instruction address");
goetz@6458 58 }
goetz@6458 59 }
goetz@6458 60 #endif // ASSERT
goetz@6458 61
goetz@6458 62 // Extract call destination from a NativeCall. The call might use a trampoline stub.
goetz@6458 63 address NativeCall::destination() const {
goetz@6458 64 address addr = (address)this;
goetz@6458 65 address destination = Assembler::bxx_destination(addr);
goetz@6458 66
goetz@6458 67 // Do we use a trampoline stub for this call?
goetz@6458 68 CodeBlob* cb = CodeCache::find_blob_unsafe(addr); // Else we get assertion if nmethod is zombie.
goetz@6458 69 assert(cb && cb->is_nmethod(), "sanity");
goetz@6458 70 nmethod *nm = (nmethod *)cb;
goetz@6458 71 if (nm->stub_contains(destination) && is_NativeCallTrampolineStub_at(destination)) {
goetz@6458 72 // Yes we do, so get the destination from the trampoline stub.
goetz@6458 73 const address trampoline_stub_addr = destination;
goetz@6458 74 destination = NativeCallTrampolineStub_at(trampoline_stub_addr)->destination();
goetz@6458 75 }
goetz@6458 76
goetz@6458 77 return destination;
goetz@6458 78 }
goetz@6458 79
goetz@6458 80 // Similar to replace_mt_safe, but just changes the destination. The
goetz@6458 81 // important thing is that free-running threads are able to execute this
goetz@6458 82 // call instruction at all times. Thus, the displacement field must be
goetz@6458 83 // instruction-word-aligned.
goetz@6458 84 //
goetz@6458 85 // Used in the runtime linkage of calls; see class CompiledIC.
goetz@6458 86 //
goetz@6458 87 // Add parameter assert_lock to switch off assertion
goetz@6458 88 // during code generation, where no patching lock is needed.
goetz@6458 89 void NativeCall::set_destination_mt_safe(address dest, bool assert_lock) {
goetz@6458 90 assert(!assert_lock ||
goetz@6458 91 (Patching_lock->is_locked() || SafepointSynchronize::is_at_safepoint()),
goetz@6458 92 "concurrent code patching");
goetz@6458 93
goetz@6458 94 ResourceMark rm;
goetz@6458 95 int code_size = 1 * BytesPerInstWord;
goetz@6458 96 address addr_call = addr_at(0);
goetz@6458 97 assert(MacroAssembler::is_bl(*(int*)addr_call), "unexpected code at call-site");
goetz@6458 98
goetz@6458 99 CodeBuffer cb(addr_call, code_size + 1);
goetz@6458 100 MacroAssembler* a = new MacroAssembler(&cb);
goetz@6458 101
goetz@6458 102 // Patch the call.
goetz@6458 103 if (ReoptimizeCallSequences &&
goetz@6458 104 a->is_within_range_of_b(dest, addr_call)) {
goetz@6458 105 a->bl(dest);
goetz@6458 106 } else {
goetz@6458 107 address trampoline_stub_addr = get_trampoline();
goetz@6458 108
goetz@6458 109 // We did not find a trampoline stub because the current codeblob
goetz@6458 110 // does not provide this information. The branch will be patched
goetz@6458 111 // later during a final fixup, when all necessary information is
goetz@6458 112 // available.
goetz@6458 113 if (trampoline_stub_addr == 0)
goetz@6458 114 return;
goetz@6458 115
goetz@6458 116 // Patch the constant in the call's trampoline stub.
goetz@6458 117 NativeCallTrampolineStub_at(trampoline_stub_addr)->set_destination(dest);
goetz@6458 118
goetz@6458 119 a->bl(trampoline_stub_addr);
goetz@6458 120 }
goetz@6458 121 ICache::invalidate_range(addr_call, code_size);
goetz@6458 122 }
goetz@6458 123
goetz@6458 124 address NativeCall::get_trampoline() {
goetz@6458 125 address call_addr = addr_at(0);
goetz@6458 126
goetz@6458 127 CodeBlob *code = CodeCache::find_blob(call_addr);
goetz@6458 128 assert(code != NULL, "Could not find the containing code blob");
goetz@6458 129
goetz@6458 130 // There are no relocations available when the code gets relocated
goetz@6458 131 // because of CodeBuffer expansion.
goetz@6458 132 if (code->relocation_size() == 0)
goetz@6458 133 return NULL;
goetz@6458 134
goetz@6458 135 address bl_destination = Assembler::bxx_destination(call_addr);
goetz@6458 136 if (code->content_contains(bl_destination) &&
goetz@6458 137 is_NativeCallTrampolineStub_at(bl_destination))
goetz@6458 138 return bl_destination;
goetz@6458 139
goetz@6458 140 // If the codeBlob is not a nmethod, this is because we get here from the
goetz@6458 141 // CodeBlob constructor, which is called within the nmethod constructor.
goetz@6458 142 return trampoline_stub_Relocation::get_trampoline_for(call_addr, (nmethod*)code);
goetz@6458 143 }
goetz@6458 144
goetz@6458 145 #ifdef ASSERT
goetz@6458 146 void NativeCall::verify() {
goetz@6458 147 address addr = addr_at(0);
goetz@6458 148
goetz@6458 149 if (!NativeCall::is_call_at(addr)) {
goetz@6458 150 tty->print_cr("not a NativeCall at " PTR_FORMAT, addr);
goetz@6458 151 // TODO: PPC port: Disassembler::decode(addr - 20, addr + 20, tty);
goetz@6458 152 fatal(err_msg("not a NativeCall at " PTR_FORMAT, addr));
goetz@6458 153 }
goetz@6458 154 }
goetz@6458 155 #endif // ASSERT
goetz@6458 156
goetz@6458 157 #ifdef ASSERT
goetz@6458 158 void NativeFarCall::verify() {
goetz@6458 159 address addr = addr_at(0);
goetz@6458 160
goetz@6458 161 NativeInstruction::verify();
goetz@6458 162 if (!NativeFarCall::is_far_call_at(addr)) {
goetz@6458 163 tty->print_cr("not a NativeFarCall at " PTR_FORMAT, addr);
goetz@6458 164 // TODO: PPC port: Disassembler::decode(addr, 20, 20, tty);
goetz@6458 165 fatal(err_msg("not a NativeFarCall at " PTR_FORMAT, addr));
goetz@6458 166 }
goetz@6458 167 }
goetz@6458 168 #endif // ASSERT
goetz@6458 169
goetz@6458 170 address NativeMovConstReg::next_instruction_address() const {
goetz@6458 171 #ifdef ASSERT
goetz@6458 172 CodeBlob* nm = CodeCache::find_blob(instruction_address());
goetz@6458 173 assert(!MacroAssembler::is_set_narrow_oop(addr_at(0), nm->content_begin()), "Should not patch narrow oop here");
goetz@6458 174 #endif
goetz@6458 175
goetz@6458 176 if (MacroAssembler::is_load_const_from_method_toc_at(addr_at(0))) {
goetz@6458 177 return addr_at(load_const_from_method_toc_instruction_size);
goetz@6458 178 } else {
goetz@6458 179 return addr_at(load_const_instruction_size);
goetz@6458 180 }
goetz@6458 181 }
goetz@6458 182
goetz@6458 183 intptr_t NativeMovConstReg::data() const {
goetz@6458 184 address addr = addr_at(0);
goetz@6458 185 CodeBlob* cb = CodeCache::find_blob_unsafe(addr);
goetz@6458 186
goetz@6458 187 if (MacroAssembler::is_load_const_at(addr)) {
goetz@6458 188 return MacroAssembler::get_const(addr);
goetz@6458 189 } else if (MacroAssembler::is_set_narrow_oop(addr, cb->content_begin())) {
goetz@6458 190 narrowOop no = (narrowOop)MacroAssembler::get_narrow_oop(addr, cb->content_begin());
simonis@6483 191 return cast_from_oop<intptr_t>(oopDesc::decode_heap_oop(no));
goetz@6458 192 } else {
goetz@6458 193 assert(MacroAssembler::is_load_const_from_method_toc_at(addr), "must be load_const_from_pool");
goetz@6458 194
goetz@6458 195 address ctable = cb->content_begin();
goetz@6458 196 int offset = MacroAssembler::get_offset_of_load_const_from_method_toc_at(addr);
goetz@6458 197 return *(intptr_t *)(ctable + offset);
goetz@6458 198 }
goetz@6458 199 }
goetz@6458 200
goetz@6458 201 address NativeMovConstReg::set_data_plain(intptr_t data, CodeBlob *cb) {
goetz@6458 202 address addr = instruction_address();
goetz@6458 203 address next_address = NULL;
goetz@6458 204 if (!cb) cb = CodeCache::find_blob(addr);
goetz@6458 205
goetz@6458 206 if (cb != NULL && MacroAssembler::is_load_const_from_method_toc_at(addr)) {
goetz@6458 207 // A load from the method's TOC (ctable).
goetz@6458 208 assert(cb->is_nmethod(), "must be nmethod");
goetz@6458 209 const address ctable = cb->content_begin();
goetz@6458 210 const int toc_offset = MacroAssembler::get_offset_of_load_const_from_method_toc_at(addr);
goetz@6458 211 *(intptr_t *)(ctable + toc_offset) = data;
goetz@6458 212 next_address = addr + BytesPerInstWord;
goetz@6458 213 } else if (cb != NULL &&
goetz@6458 214 MacroAssembler::is_calculate_address_from_global_toc_at(addr, cb->content_begin())) {
goetz@6458 215 // A calculation relative to the global TOC.
goetz@6458 216 const int invalidated_range =
goetz@6458 217 MacroAssembler::patch_calculate_address_from_global_toc_at(addr, cb->content_begin(),
goetz@6458 218 (address)data);
goetz@6458 219 const address start = invalidated_range < 0 ? addr + invalidated_range : addr;
goetz@6458 220 // FIXME:
goetz@6458 221 const int range = invalidated_range < 0 ? 4 - invalidated_range : 8;
goetz@6458 222 ICache::invalidate_range(start, range);
goetz@6458 223 next_address = addr + 1 * BytesPerInstWord;
goetz@6458 224 } else if (MacroAssembler::is_load_const_at(addr)) {
goetz@6458 225 // A normal 5 instruction load_const code sequence.
goetz@6458 226 // This is not mt safe, ok in methods like CodeBuffer::copy_code().
goetz@6458 227 MacroAssembler::patch_const(addr, (long)data);
goetz@6458 228 ICache::invalidate_range(addr, load_const_instruction_size);
goetz@6458 229 next_address = addr + 5 * BytesPerInstWord;
goetz@6458 230 } else if (MacroAssembler::is_bl(* (int*) addr)) {
goetz@6458 231 // A single branch-and-link instruction.
goetz@6458 232 ResourceMark rm;
goetz@6458 233 const int code_size = 1 * BytesPerInstWord;
goetz@6458 234 CodeBuffer cb(addr, code_size + 1);
goetz@6458 235 MacroAssembler* a = new MacroAssembler(&cb);
goetz@6458 236 a->bl((address) data);
goetz@6458 237 ICache::invalidate_range(addr, code_size);
goetz@6458 238 next_address = addr + code_size;
goetz@6458 239 } else {
goetz@6458 240 ShouldNotReachHere();
goetz@6458 241 }
goetz@6458 242
goetz@6458 243 return next_address;
goetz@6458 244 }
goetz@6458 245
goetz@6458 246 void NativeMovConstReg::set_data(intptr_t data) {
goetz@6458 247 // Store the value into the instruction stream.
goetz@6458 248 CodeBlob *cb = CodeCache::find_blob(instruction_address());
goetz@6458 249 address next_address = set_data_plain(data, cb);
goetz@6458 250
goetz@6458 251 // Also store the value into an oop_Relocation cell, if any.
goetz@6458 252 if (cb && cb->is_nmethod()) {
goetz@6458 253 RelocIterator iter((nmethod *) cb, instruction_address(), next_address);
goetz@6458 254 oop* oop_addr = NULL;
goetz@6458 255 Metadata** metadata_addr = NULL;
goetz@6458 256 while (iter.next()) {
goetz@6458 257 if (iter.type() == relocInfo::oop_type) {
goetz@6458 258 oop_Relocation *r = iter.oop_reloc();
goetz@6458 259 if (oop_addr == NULL) {
goetz@6458 260 oop_addr = r->oop_addr();
simonis@6483 261 *oop_addr = cast_to_oop(data);
goetz@6458 262 } else {
goetz@6458 263 assert(oop_addr == r->oop_addr(), "must be only one set-oop here") ;
goetz@6458 264 }
goetz@6458 265 }
goetz@6458 266 if (iter.type() == relocInfo::metadata_type) {
goetz@6458 267 metadata_Relocation *r = iter.metadata_reloc();
goetz@6458 268 if (metadata_addr == NULL) {
goetz@6458 269 metadata_addr = r->metadata_addr();
goetz@6458 270 *metadata_addr = (Metadata*)data;
goetz@6458 271 } else {
goetz@6458 272 assert(metadata_addr == r->metadata_addr(), "must be only one set-metadata here");
goetz@6458 273 }
goetz@6458 274 }
goetz@6458 275 }
goetz@6458 276 }
goetz@6458 277 }
goetz@6458 278
goetz@6458 279 void NativeMovConstReg::set_narrow_oop(narrowOop data, CodeBlob *code /* = NULL */) {
goetz@6458 280 address addr = addr_at(0);
goetz@6458 281 CodeBlob* cb = (code) ? code : CodeCache::find_blob(instruction_address());
goetz@6458 282 const int invalidated_range =
goetz@6458 283 MacroAssembler::patch_set_narrow_oop(addr, cb->content_begin(), (long)data);
goetz@6458 284 const address start = invalidated_range < 0 ? addr + invalidated_range : addr;
goetz@6458 285 // FIXME:
goetz@6458 286 const int range = invalidated_range < 0 ? 4 - invalidated_range : 8;
goetz@6458 287 ICache::invalidate_range(start, range);
goetz@6458 288 }
goetz@6458 289
goetz@6458 290 // Do not use an assertion here. Let clients decide whether they only
goetz@6458 291 // want this when assertions are enabled.
goetz@6458 292 #ifdef ASSERT
goetz@6458 293 void NativeMovConstReg::verify() {
goetz@6458 294 address addr = addr_at(0);
goetz@6458 295 CodeBlob* cb = CodeCache::find_blob_unsafe(addr); // find_nmethod() asserts if nmethod is zombie.
goetz@6458 296 if (! MacroAssembler::is_load_const_at(addr) &&
goetz@6458 297 ! MacroAssembler::is_load_const_from_method_toc_at(addr) &&
goetz@6458 298 ! (cb != NULL && MacroAssembler::is_calculate_address_from_global_toc_at(addr, cb->content_begin())) &&
goetz@6458 299 ! (cb != NULL && MacroAssembler::is_set_narrow_oop(addr, cb->content_begin())) &&
goetz@6458 300 ! MacroAssembler::is_bl(*((int*) addr))) {
goetz@6458 301 tty->print_cr("not a NativeMovConstReg at " PTR_FORMAT, addr);
goetz@6458 302 // TODO: PPC port Disassembler::decode(addr, 20, 20, tty);
goetz@6458 303 fatal(err_msg("not a NativeMovConstReg at " PTR_FORMAT, addr));
goetz@6458 304 }
goetz@6458 305 }
goetz@6458 306 #endif // ASSERT
goetz@6458 307
goetz@6458 308 void NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) {
goetz@6458 309 ResourceMark rm;
goetz@6458 310 int code_size = 1 * BytesPerInstWord;
goetz@6458 311 CodeBuffer cb(verified_entry, code_size + 1);
goetz@6458 312 MacroAssembler* a = new MacroAssembler(&cb);
goetz@6458 313 #ifdef COMPILER2
goetz@6458 314 assert(dest == SharedRuntime::get_handle_wrong_method_stub(), "expected fixed destination of patch");
goetz@6458 315 #endif
goetz@6458 316 // Patch this nmethod atomically. Always use illtrap/trap in debug build.
goetz@6458 317 if (DEBUG_ONLY(false &&) a->is_within_range_of_b(dest, a->pc())) {
goetz@6458 318 a->b(dest);
goetz@6458 319 } else {
goetz@6458 320 // The signal handler will continue at dest=OptoRuntime::handle_wrong_method_stub().
goetz@6458 321 if (TrapBasedNotEntrantChecks) {
goetz@6458 322 // We use a special trap for marking a method as not_entrant or zombie.
goetz@6458 323 a->trap_zombie_not_entrant();
goetz@6458 324 } else {
goetz@6458 325 // We use an illtrap for marking a method as not_entrant or zombie.
goetz@6458 326 a->illtrap();
goetz@6458 327 }
goetz@6458 328 }
goetz@6458 329 ICache::invalidate_range(verified_entry, code_size);
goetz@6458 330 }
goetz@6458 331
goetz@6458 332 #ifdef ASSERT
goetz@6458 333 void NativeJump::verify() {
goetz@6458 334 address addr = addr_at(0);
goetz@6458 335
goetz@6458 336 NativeInstruction::verify();
goetz@6458 337 if (!NativeJump::is_jump_at(addr)) {
goetz@6458 338 tty->print_cr("not a NativeJump at " PTR_FORMAT, addr);
goetz@6458 339 // TODO: PPC port: Disassembler::decode(addr, 20, 20, tty);
goetz@6458 340 fatal(err_msg("not a NativeJump at " PTR_FORMAT, addr));
goetz@6458 341 }
goetz@6458 342 }
goetz@6458 343 #endif // ASSERT
goetz@6458 344
goetz@6458 345 //-------------------------------------------------------------------
goetz@6458 346
goetz@6458 347 // Call trampoline stubs.
goetz@6458 348 //
goetz@6458 349 // Layout and instructions of a call trampoline stub:
goetz@6458 350 // 0: load the TOC (part 1)
goetz@6458 351 // 4: load the TOC (part 2)
goetz@6458 352 // 8: load the call target from the constant pool (part 1)
goetz@6458 353 // [12: load the call target from the constant pool (part 2, optional)]
goetz@6458 354 // ..: branch via CTR
goetz@6458 355 //
goetz@6458 356
goetz@6458 357 address NativeCallTrampolineStub::encoded_destination_addr() const {
goetz@6458 358 address instruction_addr = addr_at(2 * BytesPerInstWord);
goetz@6458 359 assert(MacroAssembler::is_ld_largeoffset(instruction_addr),
goetz@6458 360 "must be a ld with large offset (from the constant pool)");
goetz@6458 361
goetz@6458 362 return instruction_addr;
goetz@6458 363 }
goetz@6458 364
goetz@6458 365 address NativeCallTrampolineStub::destination() const {
goetz@6458 366 CodeBlob* cb = CodeCache::find_blob(addr_at(0));
goetz@6458 367 address ctable = cb->content_begin();
goetz@6458 368
goetz@6458 369 return *(address*)(ctable + destination_toc_offset());
goetz@6458 370 }
goetz@6458 371
goetz@6458 372 int NativeCallTrampolineStub::destination_toc_offset() const {
goetz@6458 373 return MacroAssembler::get_ld_largeoffset_offset(encoded_destination_addr());
goetz@6458 374 }
goetz@6458 375
goetz@6458 376 void NativeCallTrampolineStub::set_destination(address new_destination) {
goetz@6458 377 CodeBlob* cb = CodeCache::find_blob(addr_at(0));
goetz@6458 378 address ctable = cb->content_begin();
goetz@6458 379
goetz@6458 380 *(address*)(ctable + destination_toc_offset()) = new_destination;
goetz@6458 381 }
goetz@6458 382

mercurial