Wed, 29 Mar 2017 09:41:51 +0800
#4662 TieredCompilation is turned off.
TieredCompilation is not supported yet.
aoqi@1 | 1 | /* |
aoqi@1 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@1 | 3 | * Copyright (c) 2015, 2016, Loongson Technology. All rights reserved. |
aoqi@1 | 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@1 | 5 | * |
aoqi@1 | 6 | * This code is free software; you can redistribute it and/or modify it |
aoqi@1 | 7 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@1 | 8 | * published by the Free Software Foundation. |
aoqi@1 | 9 | * |
aoqi@1 | 10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@1 | 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@1 | 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@1 | 13 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@1 | 14 | * accompanied this code). |
aoqi@1 | 15 | * |
aoqi@1 | 16 | * You should have received a copy of the GNU General Public License version |
aoqi@1 | 17 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@1 | 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@1 | 19 | * |
aoqi@1 | 20 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@1 | 21 | * or visit www.oracle.com if you need additional information or have any |
aoqi@1 | 22 | * questions. |
aoqi@1 | 23 | * |
aoqi@1 | 24 | */ |
aoqi@1 | 25 | |
aoqi@1 | 26 | #ifndef CPU_MIPS_VM_NATIVEINST_MIPS_HPP |
aoqi@1 | 27 | #define CPU_MIPS_VM_NATIVEINST_MIPS_HPP |
aoqi@1 | 28 | |
aoqi@1 | 29 | #include "asm/assembler.hpp" |
aoqi@1 | 30 | #include "memory/allocation.hpp" |
aoqi@1 | 31 | #include "runtime/icache.hpp" |
aoqi@1 | 32 | #include "runtime/os.hpp" |
aoqi@1 | 33 | #include "utilities/top.hpp" |
aoqi@1 | 34 | |
aoqi@1 | 35 | // We have interfaces for the following instructions: |
aoqi@1 | 36 | // - NativeInstruction |
aoqi@1 | 37 | // - - NativeCall |
aoqi@1 | 38 | // - - NativeMovConstReg |
aoqi@1 | 39 | // - - NativeMovConstRegPatching |
aoqi@1 | 40 | // - - NativeMovRegMem |
aoqi@1 | 41 | // - - NativeMovRegMemPatching |
aoqi@1 | 42 | // - - NativeJump |
aoqi@1 | 43 | // - - NativeIllegalOpCode |
aoqi@1 | 44 | // - - NativeGeneralJump |
aoqi@1 | 45 | // - - NativeReturn |
aoqi@1 | 46 | // - - NativeReturnX (return with argument) |
aoqi@1 | 47 | // - - NativePushConst |
aoqi@1 | 48 | // - - NativeTstRegMem |
aoqi@1 | 49 | |
aoqi@1 | 50 | // The base class for different kinds of native instruction abstractions. |
aoqi@1 | 51 | // Provides the primitive operations to manipulate code relative to this. |
aoqi@1 | 52 | |
aoqi@1 | 53 | class NativeInstruction VALUE_OBJ_CLASS_SPEC { |
aoqi@1 | 54 | friend class Relocation; |
aoqi@1 | 55 | |
aoqi@1 | 56 | public: |
aoqi@1 | 57 | enum mips_specific_constants { |
fujie@346 | 58 | nop_instruction_code = 0, |
aoqi@1 | 59 | nop_instruction_size = 4 |
aoqi@1 | 60 | }; |
aoqi@1 | 61 | |
aoqi@1 | 62 | bool is_nop() { return long_at(0) == nop_instruction_code; } |
aoqi@1 | 63 | bool is_dtrace_trap(); |
aoqi@1 | 64 | inline bool is_call(); |
aoqi@1 | 65 | inline bool is_illegal(); |
aoqi@1 | 66 | inline bool is_return(); |
aoqi@1 | 67 | bool is_jump(); |
aoqi@1 | 68 | inline bool is_cond_jump(); |
aoqi@1 | 69 | bool is_safepoint_poll(); |
aoqi@1 | 70 | |
fujie@346 | 71 | //mips has no instruction to generate a illegal instrucion exception |
fujie@346 | 72 | //we define ours: break 11 |
fujie@346 | 73 | static int illegal_instruction(); |
aoqi@1 | 74 | |
fujie@346 | 75 | bool is_int_branch(); |
fujie@346 | 76 | bool is_float_branch(); |
aoqi@1 | 77 | |
aoqi@1 | 78 | |
aoqi@1 | 79 | protected: |
aoqi@1 | 80 | address addr_at(int offset) const { return address(this) + offset; } |
aoqi@1 | 81 | address instruction_address() const { return addr_at(0); } |
aoqi@1 | 82 | address next_instruction_address() const { return addr_at(BytesPerInstWord); } |
aoqi@1 | 83 | address prev_instruction_address() const { return addr_at(-BytesPerInstWord); } |
aoqi@1 | 84 | |
aoqi@1 | 85 | s_char sbyte_at(int offset) const { return *(s_char*) addr_at(offset); } |
aoqi@1 | 86 | u_char ubyte_at(int offset) const { return *(u_char*) addr_at(offset); } |
aoqi@1 | 87 | jint int_at(int offset) const { return *(jint*) addr_at(offset); } |
aoqi@1 | 88 | intptr_t ptr_at(int offset) const { return *(intptr_t*) addr_at(offset); } |
aoqi@1 | 89 | oop oop_at (int offset) const { return *(oop*) addr_at(offset); } |
aoqi@1 | 90 | int long_at(int offset) const { return *(jint*)addr_at(offset); } |
aoqi@1 | 91 | |
aoqi@1 | 92 | |
aoqi@1 | 93 | void set_char_at(int offset, char c) { *addr_at(offset) = (u_char)c; wrote(offset); } |
aoqi@1 | 94 | void set_int_at(int offset, jint i) { *(jint*)addr_at(offset) = i; wrote(offset); } |
aoqi@1 | 95 | void set_ptr_at (int offset, intptr_t ptr) { *(intptr_t*) addr_at(offset) = ptr; wrote(offset); } |
aoqi@1 | 96 | void set_oop_at (int offset, oop o) { *(oop*) addr_at(offset) = o; wrote(offset); } |
aoqi@1 | 97 | void set_long_at(int offset, long i); |
aoqi@1 | 98 | //void set_jlong_at(int offset, jlong i); |
aoqi@1 | 99 | //void set_addr_at(int offset, address x); |
aoqi@1 | 100 | |
aoqi@1 | 101 | int insn_word() const { return long_at(0); } |
aoqi@1 | 102 | static bool is_op (int insn, Assembler::ops op) { return Assembler::opcode(insn) == (int)op; } |
aoqi@1 | 103 | bool is_op (Assembler::ops op) const { return is_op(insn_word(), op); } |
aoqi@1 | 104 | bool is_rs (int insn, Register rs) const { return Assembler::rs(insn) == (int)rs->encoding(); } |
aoqi@1 | 105 | bool is_rs (Register rs) const { return is_rs(insn_word(), rs); } |
aoqi@1 | 106 | bool is_rt (int insn, Register rt) const { return Assembler::rt(insn) == (int)rt->encoding(); } |
aoqi@1 | 107 | bool is_rt (Register rt) const { return is_rt(insn_word(), rt); } |
aoqi@1 | 108 | |
aoqi@1 | 109 | static bool is_special_op (int insn, Assembler::special_ops op) { |
aoqi@1 | 110 | return is_op(insn, Assembler::special_op) && Assembler::special(insn)==(int)op; |
aoqi@1 | 111 | } |
aoqi@1 | 112 | bool is_special_op (Assembler::special_ops op) const { return is_special_op(insn_word(), op); } |
aoqi@1 | 113 | |
aoqi@1 | 114 | // This doesn't really do anything on Intel, but it is the place where |
aoqi@1 | 115 | // cache invalidation belongs, generically: |
aoqi@1 | 116 | void wrote(int offset); |
aoqi@1 | 117 | |
aoqi@1 | 118 | public: |
aoqi@1 | 119 | |
aoqi@1 | 120 | // unit test stuff |
aoqi@1 | 121 | static void test() {} // override for testing |
aoqi@1 | 122 | |
aoqi@1 | 123 | inline friend NativeInstruction* nativeInstruction_at(address address); |
aoqi@1 | 124 | }; |
aoqi@1 | 125 | |
aoqi@1 | 126 | inline NativeInstruction* nativeInstruction_at(address address) { |
aoqi@1 | 127 | NativeInstruction* inst = (NativeInstruction*)address; |
aoqi@1 | 128 | #ifdef ASSERT |
aoqi@1 | 129 | //inst->verify(); |
aoqi@1 | 130 | #endif |
aoqi@1 | 131 | return inst; |
aoqi@1 | 132 | } |
aoqi@1 | 133 | |
aoqi@1 | 134 | inline NativeCall* nativeCall_at(address address); |
aoqi@1 | 135 | // The NativeCall is an abstraction for accessing/manipulating native call imm32/imm64 |
aoqi@1 | 136 | // instructions (used to manipulate inline caches, primitive & dll calls, etc.). |
aoqi@1 | 137 | // MIPS has no call instruction with imm32/imm64. Usually, a call was done like this: |
aoqi@1 | 138 | // 32 bits: |
aoqi@1 | 139 | // lui rt, imm16 |
aoqi@1 | 140 | // addiu rt, rt, imm16 |
aoqi@1 | 141 | // jalr rt |
aoqi@1 | 142 | // nop |
aoqi@1 | 143 | // |
aoqi@1 | 144 | // 64 bits: |
aoqi@1 | 145 | // lui rd, imm(63...48); |
aoqi@1 | 146 | // ori rd, rd, imm(47...32); |
aoqi@1 | 147 | // dsll rd, rd, 16; |
aoqi@1 | 148 | // ori rd, rd, imm(31...16); |
aoqi@1 | 149 | // dsll rd, rd, 16; |
aoqi@1 | 150 | // ori rd, rd, imm(15...0); |
aoqi@1 | 151 | // jalr rd |
aoqi@1 | 152 | // nop |
aoqi@1 | 153 | // |
aoqi@1 | 154 | |
aoqi@1 | 155 | // we just consider the above for instruction as one call instruction |
aoqi@1 | 156 | class NativeCall: public NativeInstruction { |
aoqi@1 | 157 | public: |
aoqi@1 | 158 | enum mips_specific_constants { |
aoqi@1 | 159 | instruction_offset = 0, |
aoqi@1 | 160 | #ifndef _LP64 |
aoqi@1 | 161 | instruction_size = 4 * BytesPerInstWord, |
aoqi@1 | 162 | return_address_offset = 4 * BytesPerInstWord, |
aoqi@1 | 163 | #else |
aoqi@1 | 164 | instruction_size = 6 * BytesPerInstWord, |
fujie@374 | 165 | return_address_offset_short = 4 * BytesPerInstWord, |
fujie@373 | 166 | return_address_offset_long = 6 * BytesPerInstWord, |
aoqi@1 | 167 | #endif |
fujie@373 | 168 | displacement_offset = 0 |
aoqi@1 | 169 | }; |
aoqi@1 | 170 | |
aoqi@1 | 171 | address instruction_address() const { return addr_at(instruction_offset); } |
fujie@373 | 172 | |
fujie@373 | 173 | address next_instruction_address() const { |
fujie@373 | 174 | if (is_special_op(int_at(8), Assembler::jalr_op)) { |
fujie@373 | 175 | return addr_at(return_address_offset_short); |
fujie@373 | 176 | } else { |
fujie@373 | 177 | return addr_at(return_address_offset_long); |
fujie@373 | 178 | } |
fujie@373 | 179 | } |
fujie@373 | 180 | |
fujie@373 | 181 | address return_address() const { |
fujie@373 | 182 | return next_instruction_address(); |
fujie@373 | 183 | } |
fujie@373 | 184 | |
aoqi@1 | 185 | address destination() const; |
aoqi@1 | 186 | void set_destination(address dest); |
aoqi@1 | 187 | void set_destination_mt_safe(address dest) { set_destination(dest);} |
aoqi@1 | 188 | |
fujie@366 | 189 | void patch_set48_gs(address dest); |
fujie@366 | 190 | void patch_set48(address dest); |
fujie@366 | 191 | |
fujie@379 | 192 | void patch_on_jalr_gs(address dest); |
fujie@379 | 193 | void patch_on_jalr(address dest); |
fujie@379 | 194 | |
fujie@379 | 195 | void patch_on_jal_gs(address dest); |
fujie@379 | 196 | void patch_on_jal(address dest); |
fujie@379 | 197 | |
fujie@374 | 198 | void patch_set32_gs(address dest); |
fujie@374 | 199 | void patch_set32(address dest); |
fujie@374 | 200 | |
aoqi@1 | 201 | void verify_alignment() { } |
aoqi@1 | 202 | void verify(); |
aoqi@1 | 203 | void print(); |
aoqi@1 | 204 | |
aoqi@1 | 205 | // Creation |
aoqi@1 | 206 | inline friend NativeCall* nativeCall_at(address address); |
aoqi@1 | 207 | inline friend NativeCall* nativeCall_before(address return_address); |
aoqi@1 | 208 | |
aoqi@1 | 209 | static bool is_call_at(address instr) { |
aoqi@1 | 210 | return nativeInstruction_at(instr)->is_call(); |
aoqi@1 | 211 | } |
aoqi@1 | 212 | |
aoqi@1 | 213 | static bool is_call_before(address return_address) { |
fujie@373 | 214 | return is_call_at(return_address - return_address_offset_short) | is_call_at(return_address - return_address_offset_long); |
aoqi@1 | 215 | } |
aoqi@1 | 216 | |
aoqi@1 | 217 | static bool is_call_to(address instr, address target) { |
aoqi@1 | 218 | return nativeInstruction_at(instr)->is_call() && |
aoqi@1 | 219 | nativeCall_at(instr)->destination() == target; |
aoqi@1 | 220 | } |
aoqi@1 | 221 | |
aoqi@1 | 222 | // MT-safe patching of a call instruction. |
aoqi@1 | 223 | static void insert(address code_pos, address entry); |
aoqi@1 | 224 | |
aoqi@1 | 225 | static void replace_mt_safe(address instr_addr, address code_buffer); |
aoqi@1 | 226 | }; |
aoqi@1 | 227 | |
aoqi@1 | 228 | inline NativeCall* nativeCall_at(address address) { |
aoqi@1 | 229 | NativeCall* call = (NativeCall*)(address - NativeCall::instruction_offset); |
aoqi@1 | 230 | #ifdef ASSERT |
aoqi@1 | 231 | call->verify(); |
aoqi@1 | 232 | #endif |
aoqi@1 | 233 | return call; |
aoqi@1 | 234 | } |
aoqi@1 | 235 | |
aoqi@1 | 236 | inline NativeCall* nativeCall_before(address return_address) { |
fujie@373 | 237 | NativeCall* call = NULL; |
fujie@373 | 238 | if (NativeCall::is_call_at(return_address - NativeCall::return_address_offset_long)) { |
fujie@373 | 239 | call = (NativeCall*)(return_address - NativeCall::return_address_offset_long); |
fujie@373 | 240 | } else { |
fujie@373 | 241 | call = (NativeCall*)(return_address - NativeCall::return_address_offset_short); |
fujie@373 | 242 | } |
aoqi@1 | 243 | #ifdef ASSERT |
aoqi@1 | 244 | call->verify(); |
aoqi@1 | 245 | #endif |
aoqi@1 | 246 | return call; |
aoqi@1 | 247 | } |
aoqi@1 | 248 | |
aoqi@1 | 249 | class NativeMovConstReg: public NativeInstruction { |
aoqi@1 | 250 | public: |
aoqi@1 | 251 | enum mips_specific_constants { |
aoqi@1 | 252 | instruction_offset = 0, |
aoqi@1 | 253 | #ifndef _LP64 |
aoqi@1 | 254 | instruction_size = 2 * BytesPerInstWord, |
aoqi@1 | 255 | next_instruction_offset = 2 * BytesPerInstWord, |
aoqi@1 | 256 | #else |
aoqi@1 | 257 | instruction_size = 4 * BytesPerInstWord, |
aoqi@1 | 258 | next_instruction_offset = 4 * BytesPerInstWord, |
aoqi@1 | 259 | #endif |
aoqi@1 | 260 | }; |
aoqi@1 | 261 | |
aoqi@1 | 262 | int insn_word() const { return long_at(instruction_offset); } |
aoqi@1 | 263 | address instruction_address() const { return addr_at(0); } |
aoqi@1 | 264 | address next_instruction_address() const { return addr_at(next_instruction_offset); } |
aoqi@1 | 265 | intptr_t data() const; |
aoqi@1 | 266 | void set_data(intptr_t x); |
fujie@347 | 267 | |
fujie@347 | 268 | void patch_set48(intptr_t x); |
aoqi@1 | 269 | |
aoqi@1 | 270 | void verify(); |
aoqi@1 | 271 | void print(); |
aoqi@1 | 272 | |
aoqi@1 | 273 | // unit test stuff |
aoqi@1 | 274 | static void test() {} |
aoqi@1 | 275 | |
aoqi@1 | 276 | // Creation |
aoqi@1 | 277 | inline friend NativeMovConstReg* nativeMovConstReg_at(address address); |
aoqi@1 | 278 | inline friend NativeMovConstReg* nativeMovConstReg_before(address address); |
aoqi@1 | 279 | }; |
aoqi@1 | 280 | |
aoqi@1 | 281 | inline NativeMovConstReg* nativeMovConstReg_at(address address) { |
aoqi@1 | 282 | NativeMovConstReg* test = (NativeMovConstReg*)(address - NativeMovConstReg::instruction_offset); |
aoqi@1 | 283 | #ifdef ASSERT |
aoqi@1 | 284 | test->verify(); |
aoqi@1 | 285 | #endif |
aoqi@1 | 286 | return test; |
aoqi@1 | 287 | } |
aoqi@1 | 288 | |
aoqi@1 | 289 | inline NativeMovConstReg* nativeMovConstReg_before(address address) { |
aoqi@1 | 290 | NativeMovConstReg* test = (NativeMovConstReg*)(address - NativeMovConstReg::instruction_size - NativeMovConstReg::instruction_offset); |
aoqi@1 | 291 | #ifdef ASSERT |
aoqi@1 | 292 | test->verify(); |
aoqi@1 | 293 | #endif |
aoqi@1 | 294 | return test; |
aoqi@1 | 295 | } |
aoqi@1 | 296 | |
aoqi@1 | 297 | class NativeMovConstRegPatching: public NativeMovConstReg { |
aoqi@1 | 298 | private: |
aoqi@1 | 299 | friend NativeMovConstRegPatching* nativeMovConstRegPatching_at(address address) { |
aoqi@1 | 300 | NativeMovConstRegPatching* test = (NativeMovConstRegPatching*)(address - instruction_offset); |
aoqi@1 | 301 | #ifdef ASSERT |
aoqi@1 | 302 | test->verify(); |
aoqi@1 | 303 | #endif |
aoqi@1 | 304 | return test; |
aoqi@1 | 305 | } |
aoqi@1 | 306 | }; |
aoqi@1 | 307 | |
aoqi@1 | 308 | // An interface for accessing/manipulating native moves of the form: |
aoqi@1 | 309 | // lui AT, split_high(offset) |
aoqi@1 | 310 | // addiu AT, split_low(offset) |
aoqi@1 | 311 | // add reg, reg, AT |
aoqi@1 | 312 | // lb/lbu/sb/lh/lhu/sh/lw/sw/lwc1/swc1 dest, reg, 0 |
aoqi@1 | 313 | // [lw/sw/lwc1/swc1 dest, reg, 4] |
aoqi@1 | 314 | // or |
aoqi@1 | 315 | // lb/lbu/sb/lh/lhu/sh/lw/sw/lwc1/swc1 dest, reg, offset |
aoqi@1 | 316 | // [lw/sw/lwc1/swc1 dest, reg, offset+4] |
aoqi@1 | 317 | // |
aoqi@1 | 318 | // Warning: These routines must be able to handle any instruction sequences |
aoqi@1 | 319 | // that are generated as a result of the load/store byte,word,long |
aoqi@1 | 320 | // macros. |
aoqi@1 | 321 | |
aoqi@1 | 322 | class NativeMovRegMem: public NativeInstruction { |
aoqi@1 | 323 | public: |
aoqi@1 | 324 | enum mips_specific_constants { |
aoqi@1 | 325 | instruction_offset = 0, |
aoqi@1 | 326 | hiword_offset = 4, |
aoqi@1 | 327 | ldst_offset = 12, |
aoqi@1 | 328 | immediate_size = 4, |
aoqi@1 | 329 | ldst_size = 16 |
aoqi@1 | 330 | }; |
aoqi@1 | 331 | |
aoqi@1 | 332 | //offset is less than 16 bits. |
aoqi@1 | 333 | bool is_immediate() const { return !is_op(long_at(instruction_offset), Assembler::lui_op); } |
aoqi@1 | 334 | bool is_64ldst() const { |
aoqi@1 | 335 | if (is_immediate()) { |
aoqi@1 | 336 | return (Assembler::opcode(long_at(hiword_offset)) == Assembler::opcode(long_at(instruction_offset))) && |
aoqi@1 | 337 | (Assembler::imm_off(long_at(hiword_offset)) == Assembler::imm_off(long_at(instruction_offset)) + wordSize); |
aoqi@1 | 338 | } else { |
aoqi@1 | 339 | return (Assembler::opcode(long_at(ldst_offset+hiword_offset)) == Assembler::opcode(long_at(ldst_offset))) && |
aoqi@1 | 340 | (Assembler::imm_off(long_at(ldst_offset+hiword_offset)) == Assembler::imm_off(long_at(ldst_offset)) + wordSize); |
aoqi@1 | 341 | } |
aoqi@1 | 342 | } |
aoqi@1 | 343 | |
aoqi@1 | 344 | address instruction_address() const { return addr_at(instruction_offset); } |
aoqi@1 | 345 | address next_instruction_address() const { |
aoqi@1 | 346 | return addr_at( (is_immediate()? immediate_size : ldst_size) + (is_64ldst()? 4 : 0)); |
aoqi@1 | 347 | } |
aoqi@1 | 348 | /* // helper |
aoqi@1 | 349 | int instruction_start() const; |
aoqi@1 | 350 | |
aoqi@1 | 351 | address instruction_address() const; |
aoqi@1 | 352 | |
aoqi@1 | 353 | address next_instruction_address() const; |
aoqi@1 | 354 | */ |
aoqi@1 | 355 | |
aoqi@1 | 356 | int offset() const; |
aoqi@1 | 357 | |
aoqi@1 | 358 | void set_offset(int x); |
aoqi@1 | 359 | |
aoqi@1 | 360 | void add_offset_in_bytes(int add_offset) { set_offset ( ( offset() + add_offset ) ); } |
aoqi@1 | 361 | |
aoqi@1 | 362 | void verify(); |
aoqi@1 | 363 | void print (); |
aoqi@1 | 364 | |
aoqi@1 | 365 | // unit test stuff |
aoqi@1 | 366 | static void test() {} |
aoqi@1 | 367 | |
aoqi@1 | 368 | private: |
aoqi@1 | 369 | inline friend NativeMovRegMem* nativeMovRegMem_at (address address); |
aoqi@1 | 370 | }; |
aoqi@1 | 371 | |
aoqi@1 | 372 | inline NativeMovRegMem* nativeMovRegMem_at (address address) { |
aoqi@1 | 373 | NativeMovRegMem* test = (NativeMovRegMem*)(address - NativeMovRegMem::instruction_offset); |
aoqi@1 | 374 | #ifdef ASSERT |
aoqi@1 | 375 | test->verify(); |
aoqi@1 | 376 | #endif |
aoqi@1 | 377 | return test; |
aoqi@1 | 378 | } |
aoqi@1 | 379 | |
aoqi@1 | 380 | class NativeMovRegMemPatching: public NativeMovRegMem { |
aoqi@1 | 381 | private: |
aoqi@1 | 382 | friend NativeMovRegMemPatching* nativeMovRegMemPatching_at (address address) { |
aoqi@1 | 383 | NativeMovRegMemPatching* test = (NativeMovRegMemPatching*)(address - instruction_offset); |
aoqi@1 | 384 | #ifdef ASSERT |
aoqi@1 | 385 | test->verify(); |
aoqi@1 | 386 | #endif |
aoqi@1 | 387 | return test; |
aoqi@1 | 388 | } |
aoqi@1 | 389 | }; |
aoqi@1 | 390 | |
aoqi@1 | 391 | |
aoqi@1 | 392 | // Handles all kinds of jump on Loongson. Long/far, conditional/unconditional |
aoqi@1 | 393 | // 32 bits: |
aoqi@1 | 394 | // far jump: |
aoqi@1 | 395 | // lui reg, split_high(addr) |
aoqi@1 | 396 | // addiu reg, split_low(addr) |
aoqi@1 | 397 | // jr reg |
aoqi@1 | 398 | // nop |
aoqi@1 | 399 | // or |
aoqi@1 | 400 | // beq ZERO, ZERO, offset |
aoqi@1 | 401 | // nop |
aoqi@1 | 402 | // |
aoqi@1 | 403 | |
aoqi@1 | 404 | //64 bits: |
aoqi@1 | 405 | // far jump: |
aoqi@1 | 406 | // lui rd, imm(63...48); |
aoqi@1 | 407 | // ori rd, rd, imm(47...32); |
aoqi@1 | 408 | // dsll rd, rd, 16; |
aoqi@1 | 409 | // ori rd, rd, imm(31...16); |
aoqi@1 | 410 | // dsll rd, rd, 16; |
aoqi@1 | 411 | // ori rd, rd, imm(15...0); |
aoqi@1 | 412 | // jalr rd |
aoqi@1 | 413 | // nop |
aoqi@1 | 414 | // |
aoqi@1 | 415 | class NativeGeneralJump: public NativeInstruction { |
aoqi@1 | 416 | public: |
aoqi@1 | 417 | enum mips_specific_constants { |
aoqi@1 | 418 | instruction_offset = 0, |
aoqi@1 | 419 | beq_opcode = 0x10000000,//000100|00000|00000|offset |
aoqi@1 | 420 | b_mask = 0xffff0000, |
aoqi@1 | 421 | short_size = 8, |
aoqi@1 | 422 | #ifndef _LP64 |
aoqi@1 | 423 | instruction_size = 4 * BytesPerInstWord |
aoqi@1 | 424 | #else |
aoqi@1 | 425 | instruction_size = 6 * BytesPerInstWord |
aoqi@1 | 426 | #endif |
aoqi@1 | 427 | }; |
aoqi@1 | 428 | |
aoqi@1 | 429 | bool is_short() const { return (long_at(instruction_offset) & b_mask) == beq_opcode; } |
aoqi@1 | 430 | #ifdef _LP64 |
aoqi@1 | 431 | bool is_b_far(); |
aoqi@1 | 432 | #endif |
aoqi@1 | 433 | address instruction_address() const { return addr_at(instruction_offset); } |
aoqi@1 | 434 | address jump_destination(); |
aoqi@1 | 435 | |
fujie@386 | 436 | void patch_set48_gs(address dest); |
fujie@367 | 437 | void patch_set48(address dest); |
fujie@386 | 438 | |
fujie@386 | 439 | void patch_on_jr_gs(address dest); |
fujie@386 | 440 | void patch_on_jr(address dest); |
fujie@386 | 441 | |
fujie@386 | 442 | void patch_on_j_gs(address dest); |
fujie@386 | 443 | void patch_on_j(address dest); |
fujie@386 | 444 | |
aoqi@1 | 445 | void set_jump_destination(address dest); |
aoqi@1 | 446 | |
aoqi@1 | 447 | // Creation |
aoqi@1 | 448 | inline friend NativeGeneralJump* nativeGeneralJump_at(address address); |
aoqi@1 | 449 | |
aoqi@1 | 450 | // Insertion of native general jump instruction |
aoqi@1 | 451 | static void insert_unconditional(address code_pos, address entry); |
aoqi@1 | 452 | static void replace_mt_safe(address instr_addr, address code_buffer); |
aoqi@1 | 453 | static void check_verified_entry_alignment(address entry, address verified_entry){} |
aoqi@1 | 454 | static void patch_verified_entry(address entry, address verified_entry, address dest); |
aoqi@1 | 455 | |
aoqi@1 | 456 | void verify(); |
aoqi@1 | 457 | }; |
aoqi@1 | 458 | |
aoqi@1 | 459 | inline NativeGeneralJump* nativeGeneralJump_at(address address) { |
aoqi@1 | 460 | NativeGeneralJump* jump = (NativeGeneralJump*)(address); |
aoqi@1 | 461 | debug_only(jump->verify();) |
aoqi@1 | 462 | return jump; |
aoqi@1 | 463 | } |
aoqi@1 | 464 | |
aoqi@1 | 465 | /*class NativePopReg : public NativeInstruction { |
aoqi@1 | 466 | public: |
aoqi@1 | 467 | enum Intel_specific_constants { |
aoqi@1 | 468 | instruction_code = 0x58, |
aoqi@1 | 469 | instruction_size = 1, |
aoqi@1 | 470 | instruction_offset = 0, |
aoqi@1 | 471 | data_offset = 1, |
aoqi@1 | 472 | next_instruction_offset = 1 |
aoqi@1 | 473 | }; |
aoqi@1 | 474 | |
aoqi@1 | 475 | // Insert a pop instruction |
aoqi@1 | 476 | static void insert(address code_pos, Register reg); |
aoqi@1 | 477 | };*/ |
aoqi@1 | 478 | |
aoqi@1 | 479 | |
aoqi@1 | 480 | class NativeIllegalInstruction: public NativeInstruction { |
aoqi@1 | 481 | public: |
aoqi@1 | 482 | enum Intel_specific_constants { |
aoqi@1 | 483 | instruction_size = 4, |
aoqi@1 | 484 | instruction_offset = 0, |
aoqi@1 | 485 | next_instruction_offset = 4 |
aoqi@1 | 486 | }; |
aoqi@1 | 487 | |
aoqi@1 | 488 | // Insert illegal opcode as specific address |
aoqi@1 | 489 | static void insert(address code_pos); |
aoqi@1 | 490 | }; |
aoqi@1 | 491 | |
aoqi@1 | 492 | // return instruction that does not pop values of the stack |
aoqi@1 | 493 | // jr RA |
aoqi@1 | 494 | // delay slot |
aoqi@1 | 495 | class NativeReturn: public NativeInstruction { |
aoqi@1 | 496 | public: |
aoqi@1 | 497 | enum mips_specific_constants { |
aoqi@1 | 498 | instruction_size = 8, |
aoqi@1 | 499 | instruction_offset = 0, |
aoqi@1 | 500 | next_instruction_offset = 8 |
aoqi@1 | 501 | }; |
aoqi@1 | 502 | }; |
aoqi@1 | 503 | |
aoqi@1 | 504 | |
aoqi@1 | 505 | |
aoqi@1 | 506 | |
aoqi@1 | 507 | class NativeCondJump; |
aoqi@1 | 508 | inline NativeCondJump* nativeCondJump_at(address address); |
aoqi@1 | 509 | class NativeCondJump: public NativeInstruction { |
aoqi@1 | 510 | public: |
aoqi@1 | 511 | enum mips_specific_constants { |
aoqi@1 | 512 | instruction_size = 16, |
aoqi@1 | 513 | instruction_offset = 12, |
aoqi@1 | 514 | next_instruction_offset = 20 |
aoqi@1 | 515 | }; |
aoqi@1 | 516 | |
aoqi@1 | 517 | |
aoqi@1 | 518 | int insn_word() const { return long_at(instruction_offset); } |
aoqi@1 | 519 | address instruction_address() const { return addr_at(0); } |
aoqi@1 | 520 | address next_instruction_address() const { return addr_at(next_instruction_offset); } |
aoqi@1 | 521 | |
aoqi@1 | 522 | // Creation |
aoqi@1 | 523 | inline friend NativeCondJump* nativeCondJump_at(address address); |
aoqi@1 | 524 | |
aoqi@1 | 525 | address jump_destination() const { |
aoqi@1 | 526 | return ::nativeCondJump_at(addr_at(12))->jump_destination(); |
aoqi@1 | 527 | } |
aoqi@1 | 528 | |
aoqi@1 | 529 | void set_jump_destination(address dest) { |
aoqi@1 | 530 | ::nativeCondJump_at(addr_at(12))->set_jump_destination(dest); |
aoqi@1 | 531 | } |
aoqi@1 | 532 | |
aoqi@1 | 533 | }; |
aoqi@1 | 534 | |
aoqi@1 | 535 | inline NativeCondJump* nativeCondJump_at(address address) { |
aoqi@1 | 536 | NativeCondJump* jump = (NativeCondJump*)(address); |
aoqi@1 | 537 | return jump; |
aoqi@1 | 538 | } |
aoqi@1 | 539 | |
aoqi@1 | 540 | |
aoqi@1 | 541 | |
aoqi@1 | 542 | inline bool NativeInstruction::is_illegal() { return insn_word() == illegal_instruction(); } |
aoqi@1 | 543 | |
aoqi@1 | 544 | inline bool NativeInstruction::is_call() { |
aoqi@1 | 545 | #ifndef _LP64 |
aoqi@1 | 546 | return is_op(long_at(0), Assembler::lui_op) && |
aoqi@1 | 547 | is_op(long_at(4), Assembler::addiu_op) && |
aoqi@1 | 548 | is_special_op(long_at(8), Assembler::jalr_op); |
aoqi@1 | 549 | #else |
fujie@379 | 550 | |
fujie@379 | 551 | // nop |
fujie@379 | 552 | // nop |
fujie@379 | 553 | // nop |
fujie@379 | 554 | // nop |
fujie@379 | 555 | // jal target |
fujie@379 | 556 | // nop |
fujie@379 | 557 | if ( is_nop() && |
fujie@379 | 558 | nativeInstruction_at(addr_at(4))->is_nop() && |
fujie@379 | 559 | nativeInstruction_at(addr_at(8))->is_nop() && |
fujie@379 | 560 | nativeInstruction_at(addr_at(12))->is_nop() && |
fujie@379 | 561 | nativeInstruction_at(addr_at(16))->is_op(Assembler::jal_op) && |
fujie@379 | 562 | nativeInstruction_at(addr_at(20))->is_nop() ) { |
fujie@379 | 563 | return true; |
fujie@379 | 564 | } |
fujie@379 | 565 | |
fujie@379 | 566 | // li64 |
fujie@367 | 567 | if ( is_op(Assembler::lui_op) && |
fujie@367 | 568 | is_op(int_at(4), Assembler::ori_op) && |
fujie@367 | 569 | is_special_op(int_at(8), Assembler::dsll_op) && |
fujie@367 | 570 | is_op(int_at(12), Assembler::ori_op) && |
fujie@367 | 571 | is_special_op(int_at(16), Assembler::dsll_op) && |
fujie@367 | 572 | is_op(int_at(20), Assembler::ori_op) && |
fujie@367 | 573 | is_special_op(int_at(24), Assembler::jalr_op) ) { |
fujie@367 | 574 | return true; |
aoqi@1 | 575 | } |
fujie@367 | 576 | |
fujie@367 | 577 | //lui dst, imm16 |
fujie@367 | 578 | //ori dst, dst, imm16 |
fujie@367 | 579 | //dsll dst, dst, 16 |
fujie@367 | 580 | //ori dst, dst, imm16 |
fujie@367 | 581 | if ( is_op(Assembler::lui_op) && |
fujie@367 | 582 | is_op (int_at(4), Assembler::ori_op) && |
fujie@367 | 583 | is_special_op(int_at(8), Assembler::dsll_op) && |
fujie@367 | 584 | is_op (int_at(12), Assembler::ori_op) && |
fujie@367 | 585 | is_special_op(int_at(16), Assembler::jalr_op) ) { |
fujie@367 | 586 | return true; |
fujie@367 | 587 | } |
fujie@367 | 588 | |
fujie@367 | 589 | //ori dst, R0, imm16 |
fujie@367 | 590 | //dsll dst, dst, 16 |
fujie@367 | 591 | //ori dst, dst, imm16 |
fujie@367 | 592 | //nop |
fujie@367 | 593 | if ( is_op(Assembler::ori_op) && |
fujie@367 | 594 | is_special_op(int_at(4), Assembler::dsll_op) && |
fujie@367 | 595 | is_op (int_at(8), Assembler::ori_op) && |
fujie@367 | 596 | nativeInstruction_at(addr_at(12))->is_nop() && |
fujie@367 | 597 | is_special_op(int_at(16), Assembler::jalr_op) ) { |
fujie@367 | 598 | return true; |
fujie@367 | 599 | } |
fujie@367 | 600 | |
fujie@367 | 601 | //ori dst, R0, imm16 |
fujie@367 | 602 | //dsll dst, dst, 16 |
fujie@367 | 603 | //nop |
fujie@367 | 604 | //nop |
fujie@367 | 605 | if ( is_op(Assembler::ori_op) && |
fujie@367 | 606 | is_special_op(int_at(4), Assembler::dsll_op) && |
fujie@367 | 607 | nativeInstruction_at(addr_at(8))->is_nop() && |
fujie@367 | 608 | nativeInstruction_at(addr_at(12))->is_nop() && |
fujie@367 | 609 | is_special_op(int_at(16), Assembler::jalr_op) ) { |
fujie@367 | 610 | return true; |
fujie@367 | 611 | } |
fujie@367 | 612 | |
fujie@367 | 613 | //daddiu dst, R0, imm16 |
fujie@367 | 614 | //nop |
fujie@367 | 615 | //nop |
fujie@367 | 616 | //nop |
fujie@367 | 617 | if ( is_op(Assembler::daddiu_op) && |
fujie@367 | 618 | nativeInstruction_at(addr_at(4))->is_nop() && |
fujie@367 | 619 | nativeInstruction_at(addr_at(8))->is_nop() && |
fujie@367 | 620 | nativeInstruction_at(addr_at(12))->is_nop() && |
fujie@367 | 621 | is_special_op(int_at(16), Assembler::jalr_op) ) { |
fujie@367 | 622 | return true; |
fujie@367 | 623 | } |
fujie@367 | 624 | |
fujie@367 | 625 | //lui dst, imm16 |
fujie@367 | 626 | //ori dst, dst, imm16 |
fujie@367 | 627 | //nop |
fujie@367 | 628 | //nop |
fujie@367 | 629 | if ( is_op(Assembler::lui_op) && |
fujie@367 | 630 | is_op (int_at(4), Assembler::ori_op) && |
fujie@367 | 631 | nativeInstruction_at(addr_at(8))->is_nop() && |
fujie@367 | 632 | nativeInstruction_at(addr_at(12))->is_nop() && |
fujie@367 | 633 | is_special_op(int_at(16), Assembler::jalr_op) ) { |
fujie@367 | 634 | return true; |
fujie@367 | 635 | } |
fujie@367 | 636 | |
fujie@367 | 637 | //lui dst, imm16 |
fujie@367 | 638 | //nop |
fujie@367 | 639 | //nop |
fujie@367 | 640 | //nop |
fujie@367 | 641 | if ( is_op(Assembler::lui_op) && |
fujie@367 | 642 | nativeInstruction_at(addr_at(4))->is_nop() && |
fujie@367 | 643 | nativeInstruction_at(addr_at(8))->is_nop() && |
fujie@367 | 644 | nativeInstruction_at(addr_at(12))->is_nop() && |
fujie@367 | 645 | is_special_op(int_at(16), Assembler::jalr_op) ) { |
fujie@367 | 646 | return true; |
fujie@367 | 647 | } |
fujie@367 | 648 | |
fujie@373 | 649 | |
fujie@373 | 650 | //daddiu dst, R0, imm16 |
fujie@373 | 651 | //nop |
fujie@373 | 652 | if ( is_op(Assembler::daddiu_op) && |
fujie@373 | 653 | nativeInstruction_at(addr_at(4))->is_nop() && |
fujie@373 | 654 | is_special_op(int_at(8), Assembler::jalr_op) ) { |
fujie@373 | 655 | return true; |
fujie@373 | 656 | } |
fujie@373 | 657 | |
fujie@373 | 658 | //lui dst, imm16 |
fujie@373 | 659 | //ori dst, dst, imm16 |
fujie@373 | 660 | if ( is_op(Assembler::lui_op) && |
fujie@373 | 661 | is_op (int_at(4), Assembler::ori_op) && |
fujie@373 | 662 | is_special_op(int_at(8), Assembler::jalr_op) ) { |
fujie@373 | 663 | return true; |
fujie@373 | 664 | } |
fujie@373 | 665 | |
fujie@373 | 666 | //lui dst, imm16 |
fujie@373 | 667 | //nop |
fujie@373 | 668 | if ( is_op(Assembler::lui_op) && |
fujie@373 | 669 | nativeInstruction_at(addr_at(4))->is_nop() && |
fujie@373 | 670 | is_special_op(int_at(8), Assembler::jalr_op) ) { |
fujie@373 | 671 | return true; |
fujie@373 | 672 | } |
fujie@373 | 673 | |
fujie@367 | 674 | return false; |
fujie@367 | 675 | |
aoqi@1 | 676 | #endif |
aoqi@1 | 677 | } |
aoqi@1 | 678 | |
aoqi@1 | 679 | inline bool NativeInstruction::is_return() { return is_special_op(Assembler::jr_op) && is_rs(RA);} |
aoqi@1 | 680 | |
aoqi@1 | 681 | inline bool NativeInstruction::is_cond_jump() { return is_int_branch() || is_float_branch(); } |
aoqi@1 | 682 | #endif // CPU_MIPS_VM_NATIVEINST_MIPS_HPP |
aoqi@1 | 683 |