/* * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
class NativeCall; class NativeFarCall; class NativeMovConstReg; class NativeJump; #ifndef COMPILER2 class NativeGeneralJump; class NativeMovRegMem; #endif class NativeInstruction;
// We have interface for the following instructions: // - NativeInstruction // - NativeCall // - NativeFarCall // - NativeMovConstReg // - NativeMovRegMem // - NativeJump // - NativeGeneralJump // - NativeIllegalInstruction // The base class for different kinds of native instruction abstractions. // Provides the primitive operations to manipulate code relative to this.
//------------------------------------- // N a t i v e I n s t r u c t i o n //-------------------------------------
// Bcrl is currently the only accepted instruction here. bool is_jump();
// We use an illtrap for marking a method as not_entrant. bool is_sigill_not_entrant();
bool is_safepoint_poll() { // Is the current instruction a POTENTIAL read access to the polling page? // The instruction's current arguments are not checked! return MacroAssembler::is_load_from_polling_page(addr_at(0));
}
address get_poll_address(void *ucontext) { // Extract poll address from instruction and ucontext. return MacroAssembler::get_poll_address(addr_at(0), ucontext);
}
//--------------------------------------------------- // N a t i v e I l l e g a l I n s t r u c t i o n //---------------------------------------------------
class NativeIllegalInstruction: public NativeInstruction { public: enum z_specific_constants {
instruction_size = 2
};
// Insert illegal opcode at specific address. staticvoid insert(address code_pos);
};
//----------------------- // N a t i v e C a l l //-----------------------
// The NativeCall is an abstraction for accessing/manipulating call // instructions. It is used to manipulate inline caches, primitive & // dll calls, etc.
// A native call, as defined by this abstraction layer, consists of // all instructions required to set up for and actually make the call. // // On z/Architecture, there exist three different forms of native calls: // 1) Call with pc-relative address, 1 instruction // The location of the target function is encoded as relative address // in the call instruction. The short form (BRAS) allows for a // 16-bit signed relative address (in 2-byte units). The long form // (BRASL) allows for a 32-bit signed relative address (in 2-byte units). // 2) Call with immediate address, 3 or 5 instructions. // The location of the target function is given by an immediate // constant which is loaded into a (scratch) register. Depending on // the hardware capabilities, this takes 2 or 4 instructions. // The call itself is then a "call by register"(BASR) instruction. // 3) Call with address from constant pool, 2(3) instructions (with dynamic TOC) // The location of the target function is stored in the constant pool // during compilation. From there it is loaded into a (scratch) register. // The call itself is then a "call by register"(BASR) instruction. // // When initially generating a call, the compiler uses form 2) (not // patchable, target address constant, e.g. runtime calls) or 3) (patchable, // target address might eventually get relocated). Later in the process, // a call could be transformed into form 1) (also patchable) during ShortenBranches. // // If a call is/has to be patchable, the instruction sequence generated for it // has to be constant in length. Excessive space, created e.g. by ShortenBranches, // is allocated to lower addresses and filled with nops. That is necessary to // keep the return address constant, no matter what form the call has. // Methods dealing with such calls have "patchable" as part of their name.
class NativeCall: public NativeInstruction { public:
enum z_specific_constants {
instruction_size = 18, // Used in shared code for calls with reloc_info: // value correct if !has_long_displacement_fast().
call_far_pcrelative_displacement_offset = 4, // Includes 2 bytes for the nop.
call_far_pcrelative_displacement_alignment = 4
};
// Maximum size (in bytes) of a call to an absolute address. // Used when emitting call to deopt handler blob, which is a // "load_const_call". The code pattern is: // tmpReg := load_const(address); (* depends on CPU ArchLvl, but is otherwise constant *) // call(tmpReg); (* basr, 2 bytes *) staticunsignedint max_instruction_size() { return MacroAssembler::load_const_size() + MacroAssembler::call_byregister_size();
}
// For the ordering of the checks see note at nativeCall_before.
address next_instruction_address() const {
address iaddr = instruction_address();
if (MacroAssembler::is_load_const_call(iaddr)) { // Form 2): load_const, BASR return addr_at(MacroAssembler::load_const_call_size());
}
if (MacroAssembler::is_load_const_from_toc_call(iaddr)) { // Form 3): load_const_from_toc (LARL+LG/LGRL), BASR. return addr_at(MacroAssembler::load_const_from_toc_call_size());
}
if (MacroAssembler::is_call_far_pcrelative(iaddr)) { // Form 1): NOP, BRASL // The BRASL (Branch Relative And Save Long) is patched into the space created // by the load_const_from_toc_call sequence (typically (LARL-LG)/LGRL - BASR. // The BRASL must be positioned such that it's end is FW (4-byte) aligned (for atomic patching). // It is achieved by aligning the end of the entire sequence on a 4byte boundary, by inserting // a nop, if required, at the very beginning of the instruction sequence. The nop needs to // be accounted for when calculating the next instruction address. The alignment takes place // already when generating the original instruction sequence. The alignment requirement // makes the size depend on location. // The return address of the call must always be at the end of the instruction sequence. // Inserting the extra alignment nop (or anything else) at the end is not an option. // The patched-in brasl instruction is prepended with a nop to make it easier to // distinguish from a load_const_from_toc_call sequence. return addr_at(MacroAssembler::call_far_pcrelative_size());
}
((NativeCall*)iaddr)->print();
guarantee(false, "Not a NativeCall site"); return NULL;
}
// This is a very tricky function to implement. It involves stepping // backwards in the instruction stream. On architectures with variable // instruction length, this is a risky endeavor. From the return address, // you do not know how far to step back to be at a location (your starting // point) that will eventually bring you back to the return address. // Furthermore, it may happen that there are multiple starting points. // // With only a few possible (allowed) code patterns, the risk is lower but // does not diminish completely. Experience shows that there are code patterns // which look like a load_const_from_toc_call @(return address-8), but in // fact are a call_far_pcrelative @(return address-6). The other way around // is possible as well, but was not knowingly observed so far. // // The unpredictability is caused by the pc-relative address field in both // the call_far_pcrelative (BASR) and the load_const_from_toc (LGRL) // instructions. This field can contain an arbitrary bit pattern. // // Here is a real-world example: // Mnemonics: <not a valid sequence> LGRL r10,<addr> BASR r14,r10 // Hex code: eb01 9008 007a c498 ffff c4a8 c0e5 ffc1 0dea // Mnemonics: AGSI <mem>,I8 LGRL r9,<addr> BRASL r14,<addr> correct // // If you first check for a load_const_from_toc_call @(-8), you will find // a false positive. In this example, it is obviously false, because the // preceding bytes do not form a valid instruction pattern. If you first // check for call_far_pcrelative @(-6), you get a true positive - in this // case. // // The following remedy has been implemented/enforced: // 1) Everywhere, the permissible code patterns are checked in the same // sequence: Form 2) - Form 3) - Form 1). // 2) The call_far_pcrelative, which would ideally be just one BRASL // instruction, is always prepended with a NOP. This measure avoids // ambiguities with load_const_from_toc_call. friend NativeCall* nativeCall_before(address return_address) {
NativeCall *call = NULL;
// Make sure not to return garbage
address instp = return_address - MacroAssembler::load_const_call_size(); if (MacroAssembler::is_load_const_call(instp)) { // Form 2)
call = (NativeCall*)(instp); // load_const + basr
} else {
instp = return_address - MacroAssembler::load_const_from_toc_call_size(); if (MacroAssembler::is_load_const_from_toc_call(instp)) { // Form 3)
call = (NativeCall*)(instp); // load_const_from_toc + basr
} else {
instp = return_address - MacroAssembler::call_far_pcrelative_size(); if (MacroAssembler::is_call_far_pcrelative(instp)) { // Form 1)
call = (NativeCall*)(instp); // brasl (or nop + brasl)
} else {
call = (NativeCall*)(instp);
call->print();
guarantee(false, "Not a NativeCall site");
}
}
}
// Ordering of checks 2) 3) 1) is relevant! staticbool is_call_at(address a) { // Check plain instruction sequence. Do not care about filler or alignment nops. bool b = MacroAssembler::is_load_const_call(a) || // load_const + basr
MacroAssembler::is_load_const_from_toc_call(a) || // load_const_from_toc + basr
MacroAssembler::is_call_far_pcrelative(a); // nop + brasl return b;
}
// Ordering of checks 2) 3) 1) is relevant! staticbool is_call_before(address a) { // check plain instruction sequence. Do not care about filler or alignment nops. bool b = MacroAssembler::is_load_const_call( a - MacroAssembler::load_const_call_size()) || // load_const + basr
MacroAssembler::is_load_const_from_toc_call(a - MacroAssembler::load_const_from_toc_call_size()) || // load_const_from_toc + basr
MacroAssembler::is_call_far_pcrelative( a - MacroAssembler::call_far_pcrelative_size()); // nop+brasl return b;
}
staticbool is_call_to(address instr, address target) { // Check whether there is a `NativeCall' at the address `instr' // calling to the address `target'. return is_call_at(instr) && target == ((NativeCall *)instr)->destination();
}
//----------------------------- // N a t i v e F a r C a l l //-----------------------------
// The NativeFarCall is an abstraction for accessing/manipulating native // call-anywhere instructions. // Used to call native methods which may be loaded anywhere in the address // space, possibly out of reach of a call instruction.
// Refer to NativeCall for a description of the supported call forms.
class NativeFarCall: public NativeInstruction {
public: // We use MacroAssembler::call_far_patchable() for implementing a // call-anywhere instruction.
// Returns the NativeFarCall's destination.
address destination();
// Sets the NativeCall's destination, not necessarily mt-safe. // Used when relocating code. void set_destination(address dest, int toc_offset);
// Checks whether instr points at a NativeFarCall instruction. staticbool is_far_call_at(address instr) { // Use compound inspection function which, in addition to instruction sequence, // also checks for expected nops and for instruction alignment. return MacroAssembler::is_call_far_patchable_at(instr);
}
// Does the NativeFarCall implementation use a pc-relative encoding // of the call destination? // Used when relocating code. bool is_pcrelative() {
address iaddr = (address)this;
assert(is_far_call_at(iaddr), "unexpected call type"); return MacroAssembler::is_call_far_patchable_pcrelative_at(iaddr);
}
void verify();
// Instantiates a NativeFarCall object starting at the given instruction // address and returns the NativeFarCall object. inlinefriend NativeFarCall* nativeFarCall_at(address instr) {
NativeFarCall* call = (NativeFarCall*)instr; #ifdef ASSERT
call->verify(); #endif return call;
}
};
//------------------------------------- // N a t i v e M o v C o n s t R e g //-------------------------------------
// An interface for accessing/manipulating native set_oop imm, reg instructions. // (Used to manipulate inlined data references, etc.)
// A native move of a constant into a register, as defined by this abstraction layer, // deals with instruction sequences that load "quasi constant" oops into registers // for addressing. For multiple causes, those "quasi constant" oops eventually need // to be changed (i.e. patched). The reason is quite simple: objects might get moved // around in storage. Pc-relative oop addresses have to be patched also if the // reference location is moved. That happens when executable code is relocated.
class NativeMovConstReg: public NativeInstruction { public:
enum z_specific_constants {
instruction_size = 10 // Used in shared code for calls with reloc_info.
};
// Patch data in code stream.
address set_data_plain(intptr_t x, CodeBlob *code); // Patch data in code stream and oop pool if necessary. void set_data(intptr_t x, relocInfo::relocType expected_type = relocInfo::none);
#ifdef COMPILER1 //--------------------------------- // N a t i v e M o v R e g M e m //---------------------------------
// Interface to manipulate a code sequence that performs a memory access (load/store). // The code is the patchable version of memory accesses generated by // LIR_Assembler::reg2mem() and LIR_Assembler::mem2reg(). // // Loading the offset for the mem access is target of the manipulation. // // The instruction sequence looks like this: // iihf %r1,$bits1 ; load offset for mem access // iilf %r1,$bits2 // [compress oop] ; optional, store only // load/store %r2,0(%r1,%r2) ; memory access
class NativeMovRegMem; inline NativeMovRegMem* nativeMovRegMem_at (address address); class NativeMovRegMem: public NativeInstruction { public: enum z_specific_constants {
instruction_size = 12 // load_const used with access_field_id
};
int num_bytes_to_end_of_patch() const { return instruction_size; }
//----------------------- // N a t i v e J u m p //-----------------------
// An interface for accessing/manipulating native jumps class NativeJump: public NativeInstruction { public: enum z_constants {
instruction_size = 2 // Size of z_illtrap().
};
// Maximum size (in bytes) of a jump to an absolute address. // Used when emitting branch to an exception handler which is a "load_const_optimized_branch". // Thus, a pessimistic estimate is obtained when using load_const. // code pattern is: // tmpReg := load_const(address); (* varying size *) // jumpTo(tmpReg); (* bcr, 2 bytes *) // staticunsignedint max_instruction_size() { return MacroAssembler::load_const_size() + MacroAssembler::jump_byregister_size();
}
staticbool is_jump_at(address a) { int off = 0; bool b = (MacroAssembler::is_load_const_from_toc(a+off) &&
Assembler::is_z_br(*(short*)(a+off + MacroAssembler::load_const_from_toc_size())));
b = b || (MacroAssembler::is_load_const(a+off) &&
Assembler::is_z_br(*(short*)(a+off + MacroAssembler::load_const_size()))); return b;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.