1 /* 2 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. 3 * Copyright 2012, 2013 SAP AG. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #ifndef CPU_PPC_VM_NATIVEINST_PPC_HPP 27 #define CPU_PPC_VM_NATIVEINST_PPC_HPP 28 29 #include "asm/assembler.hpp" 30 #include "asm/macroAssembler.hpp" 31 #include "memory/allocation.hpp" 32 #include "runtime/icache.hpp" 33 #include "runtime/os.hpp" 34 #include "utilities/top.hpp" 35 36 // We have interfaces for the following instructions: 37 // 38 // - NativeInstruction 39 // - NativeCall 40 // - NativeFarCall 41 // - NativeMovConstReg 42 // - NativeJump 43 // - NativeIllegalInstruction 44 // - NativeConditionalFarBranch 45 // - NativeCallTrampolineStub 46 47 // The base class for different kinds of native instruction abstractions. 48 // It provides the primitive operations to manipulate code relative to this. 49 class NativeInstruction VALUE_OBJ_CLASS_SPEC { 50 friend class Relocation; 51 52 public: 53 bool is_sigtrap_ic_miss_check() { 54 assert(UseSIGTRAP, "precondition"); 55 return MacroAssembler::is_trap_ic_miss_check(long_at(0)); 56 } 57 58 bool is_sigtrap_null_check() { 59 assert(UseSIGTRAP && TrapBasedNullChecks, "precondition"); 60 return MacroAssembler::is_trap_null_check(long_at(0)); 61 } 62 63 // We use a special trap for marking a method as not_entrant or zombie 64 // iff UseSIGTRAP. 65 bool is_sigtrap_zombie_not_entrant() { 66 assert(UseSIGTRAP, "precondition"); 67 return MacroAssembler::is_trap_zombie_not_entrant(long_at(0)); 68 } 69 70 // We use an illtrap for marking a method as not_entrant or zombie 71 // iff !UseSIGTRAP. 72 bool is_sigill_zombie_not_entrant() { 73 assert(!UseSIGTRAP, "precondition"); 74 // Work around a C++ compiler bug which changes 'this'. 75 return NativeInstruction::is_sigill_zombie_not_entrant_at(addr_at(0)); 76 } 77 static bool is_sigill_zombie_not_entrant_at(address addr); 78 79 // SIGTRAP-based implicit range checks 80 bool is_sigtrap_range_check() { 81 assert(UseSIGTRAP && TrapBasedRangeChecks, "precondition"); 82 return MacroAssembler::is_trap_range_check(long_at(0)); 83 } 84 85 // 'should not reach here'. 86 bool is_sigtrap_should_not_reach_here() { 87 return MacroAssembler::is_trap_should_not_reach_here(long_at(0)); 88 } 89 90 bool is_safepoint_poll() { 91 // Is the current instruction a POTENTIAL read access to the polling page? 92 // The current arguments of the instruction are not checked! 93 return MacroAssembler::is_load_from_polling_page(long_at(0), NULL); 94 } 95 96 bool is_memory_serialization(JavaThread *thread, void *ucontext) { 97 // Is the current instruction a write access of thread to the 98 // memory serialization page? 99 return MacroAssembler::is_memory_serialization(long_at(0), thread, ucontext); 100 } 101 102 address get_stack_bang_address(void *ucontext) { 103 // If long_at(0) is not a stack bang, return 0. Otherwise, return 104 // banged address. 105 return MacroAssembler::get_stack_bang_address(long_at(0), ucontext); 106 } 107 108 protected: 109 address addr_at(int offset) const { return address(this) + offset; } 110 int long_at(int offset) const { return *(int*)addr_at(offset); } 111 112 public: 113 void verify() NOT_DEBUG_RETURN; 114 }; 115 116 inline NativeInstruction* nativeInstruction_at(address address) { 117 NativeInstruction* inst = (NativeInstruction*)address; 118 inst->verify(); 119 return inst; 120 } 121 122 // The NativeCall is an abstraction for accessing/manipulating call 123 // instructions. It is used to manipulate inline caches, primitive & 124 // dll calls, etc. 125 // 126 // Sparc distinguishes `NativeCall' and `NativeFarCall'. On PPC64, 127 // at present, we provide a single class `NativeCall' representing the 128 // sequence `load_const, mtctr, bctrl' or the sequence 'ld_from_toc, 129 // mtctr, bctrl'. 130 class NativeCall: public NativeInstruction { 131 public: 132 133 enum specific_constants { 134 load_const_instruction_size = 28, 135 load_const_from_method_toc_instruction_size = 16, 136 instruction_size = 16 // Used in shared code for calls with reloc_info. 137 }; 138 139 static bool is_call_at(address a) { 140 return Assembler::is_bl(*(int*)(a)); 141 } 142 143 static bool is_call_before(address return_address) { 144 return NativeCall::is_call_at(return_address - 4); 145 } 146 147 address instruction_address() const { 148 return addr_at(0); 149 } 150 151 address next_instruction_address() const { 152 // We have only bl. 153 assert(MacroAssembler::is_bl(*(int*)instruction_address()), "Should be bl instruction!"); 154 return addr_at(4); 155 } 156 157 address return_address() const { 158 return next_instruction_address(); 159 } 160 161 address destination() const; 162 163 // The parameter assert_lock disables the assertion during code generation. 164 void set_destination_mt_safe(address dest, bool assert_lock = true); 165 166 address get_trampoline(); 167 168 void verify_alignment() {} // do nothing on ppc 169 void verify() NOT_DEBUG_RETURN; 170 }; 171 172 inline NativeCall* nativeCall_at(address instr) { 173 NativeCall* call = (NativeCall*)instr; 174 call->verify(); 175 return call; 176 } 177 178 inline NativeCall* nativeCall_before(address return_address) { 179 NativeCall* call = NULL; 180 if (MacroAssembler::is_bl(*(int*)(return_address - 4))) 181 call = (NativeCall*)(return_address - 4); 182 call->verify(); 183 return call; 184 } 185 186 // The NativeFarCall is an abstraction for accessing/manipulating native 187 // call-anywhere instructions. 188 // Used to call native methods which may be loaded anywhere in the address 189 // space, possibly out of reach of a call instruction. 190 class NativeFarCall: public NativeInstruction { 191 public: 192 // We use MacroAssembler::bl64_patchable() for implementing a 193 // call-anywhere instruction. 194 195 // Checks whether instr points at a NativeFarCall instruction. 196 static bool is_far_call_at(address instr) { 197 return MacroAssembler::is_bl64_patchable_at(instr); 198 } 199 200 // Does the NativeFarCall implementation use a pc-relative encoding 201 // of the call destination? 202 // Used when relocating code. 203 bool is_pcrelative() { 204 assert(MacroAssembler::is_bl64_patchable_at((address)this), 205 "unexpected call type"); 206 return MacroAssembler::is_bl64_patchable_pcrelative_at((address)this); 207 } 208 209 // Returns the NativeFarCall's destination. 210 address destination() const { 211 assert(MacroAssembler::is_bl64_patchable_at((address)this), 212 "unexpected call type"); 213 return MacroAssembler::get_dest_of_bl64_patchable_at((address)this); 214 } 215 216 // Sets the NativeCall's destination, not necessarily mt-safe. 217 // Used when relocating code. 218 void set_destination(address dest) { 219 // Set new destination (implementation of call may change here). 220 assert(MacroAssembler::is_bl64_patchable_at((address)this), 221 "unexpected call type"); 222 MacroAssembler::set_dest_of_bl64_patchable_at((address)this, dest); 223 } 224 225 void verify() NOT_DEBUG_RETURN; 226 }; 227 228 // Instantiates a NativeFarCall object starting at the given instruction 229 // address and returns the NativeFarCall object. 230 inline NativeFarCall* nativeFarCall_at(address instr) { 231 NativeFarCall* call = (NativeFarCall*)instr; 232 call->verify(); 233 return call; 234 } 235 236 // An interface for accessing/manipulating native set_oop imm, reg instructions. 237 // (used to manipulate inlined data references, etc.) 238 class NativeMovConstReg: public NativeInstruction { 239 public: 240 241 enum specific_constants { 242 load_const_instruction_size = 20, 243 load_const_from_method_toc_instruction_size = 8, 244 instruction_size = 8 // Used in shared code for calls with reloc_info. 245 }; 246 247 address instruction_address() const { 248 return addr_at(0); 249 } 250 251 address next_instruction_address() const; 252 253 // (The [set_]data accessor respects oop_type relocs also.) 254 intptr_t data() const; 255 256 // Patch the code stream. 257 address set_data_plain(intptr_t x, CodeBlob *code); 258 // Patch the code stream and oop pool. 259 void set_data(intptr_t x); 260 261 // Patch narrow oop constants. Use this also for narrow klass. 262 void set_narrow_oop(narrowOop data, CodeBlob *code = NULL); 263 264 void verify() NOT_DEBUG_RETURN; 265 }; 266 267 inline NativeMovConstReg* nativeMovConstReg_at(address address) { 268 NativeMovConstReg* test = (NativeMovConstReg*)address; 269 test->verify(); 270 return test; 271 } 272 273 // The NativeJump is an abstraction for accessing/manipulating native 274 // jump-anywhere instructions. 275 class NativeJump: public NativeInstruction { 276 public: 277 // We use MacroAssembler::b64_patchable() for implementing a 278 // jump-anywhere instruction. 279 280 enum specific_constants { 281 instruction_size = MacroAssembler::b64_patchable_size 282 }; 283 284 // Checks whether instr points at a NativeJump instruction. 285 static bool is_jump_at(address instr) { 286 return MacroAssembler::is_b64_patchable_at(instr) 287 || ( MacroAssembler::is_load_const_from_method_toc_at(instr) 288 && Assembler::is_mtctr(*(int*)(instr + 2 * 4)) 289 && Assembler::is_bctr(*(int*)(instr + 3 * 4))); 290 } 291 292 // Does the NativeJump implementation use a pc-relative encoding 293 // of the call destination? 294 // Used when relocating code or patching jumps. 295 bool is_pcrelative() { 296 return MacroAssembler::is_b64_patchable_pcrelative_at((address)this); 297 } 298 299 // Returns the NativeJump's destination. 300 address jump_destination() const { 301 if (MacroAssembler::is_b64_patchable_at((address)this)) { 302 return MacroAssembler::get_dest_of_b64_patchable_at((address)this); 303 } else if (MacroAssembler::is_load_const_from_method_toc_at((address)this) 304 && Assembler::is_mtctr(*(int*)((address)this + 2 * 4)) 305 && Assembler::is_bctr(*(int*)((address)this + 3 * 4))) { 306 return (address)((NativeMovConstReg *)this)->data(); 307 } else { 308 ShouldNotReachHere(); 309 return NULL; 310 } 311 } 312 313 // Sets the NativeJump's destination, not necessarily mt-safe. 314 // Used when relocating code or patching jumps. 315 void set_jump_destination(address dest) { 316 // Set new destination (implementation of call may change here). 317 if (MacroAssembler::is_b64_patchable_at((address)this)) { 318 MacroAssembler::set_dest_of_b64_patchable_at((address)this, dest); 319 } else if (MacroAssembler::is_load_const_from_method_toc_at((address)this) 320 && Assembler::is_mtctr(*(int*)((address)this + 2 * 4)) 321 && Assembler::is_bctr(*(int*)((address)this + 3 * 4))) { 322 ((NativeMovConstReg *)this)->set_data((intptr_t)dest); 323 } else { 324 ShouldNotReachHere(); 325 } 326 } 327 328 // MT-safe insertion of native jump at verified method entry 329 static void patch_verified_entry(address entry, address verified_entry, address dest); 330 331 void verify() NOT_DEBUG_RETURN; 332 333 static void check_verified_entry_alignment(address entry, address verified_entry) { 334 // We just patch one instruction on ppc64, so the jump doesn't have to 335 // be aligned. Nothing to do here. 336 } 337 }; 338 339 // Instantiates a NativeJump object starting at the given instruction 340 // address and returns the NativeJump object. 341 inline NativeJump* nativeJump_at(address instr) { 342 NativeJump* call = (NativeJump*)instr; 343 call->verify(); 344 return call; 345 } 346 347 // NativeConditionalFarBranch is abstraction for accessing/manipulating 348 // conditional far branches. 349 class NativeConditionalFarBranch : public NativeInstruction { 350 public: 351 352 static bool is_conditional_far_branch_at(address instr) { 353 return MacroAssembler::is_bc_far_at(instr); 354 } 355 356 address branch_destination() const { 357 return MacroAssembler::get_dest_of_bc_far_at((address)this); 358 } 359 360 void set_branch_destination(address dest) { 361 MacroAssembler::set_dest_of_bc_far_at((address)this, dest); 362 } 363 }; 364 365 inline NativeConditionalFarBranch* NativeConditionalFarBranch_at(address address) { 366 assert(NativeConditionalFarBranch::is_conditional_far_branch_at(address), 367 "must be a conditional far branch"); 368 return (NativeConditionalFarBranch*)address; 369 } 370 371 // Call trampoline stubs. 372 class NativeCallTrampolineStub : public NativeInstruction { 373 private: 374 375 address encoded_destination_addr() const; 376 377 public: 378 379 address destination() const; 380 int destination_toc_offset() const; 381 382 void set_destination(address new_destination); 383 }; 384 385 386 inline bool is_NativeCallTrampolineStub_at(address address) { 387 int first_instr = *(int*)address; 388 return Assembler::is_addis(first_instr) && 389 (Register)(intptr_t)Assembler::inv_rt_field(first_instr) == R12_scratch2; 390 } 391 392 inline NativeCallTrampolineStub* NativeCallTrampolineStub_at(address address) { 393 assert(is_NativeCallTrampolineStub_at(address), "no call trampoline found"); 394 return (NativeCallTrampolineStub*)address; 395 } 396 397 #endif // CPU_PPC_VM_NATIVEINST_PPC_HPP