Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/cpu/ppc/vm/nativeInst_ppc.cpp
32285 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* Copyright 2012, 2014 SAP AG. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "asm/macroAssembler.inline.hpp"27#include "memory/resourceArea.hpp"28#include "nativeInst_ppc.hpp"29#include "oops/oop.inline.hpp"30#include "runtime/handles.hpp"31#include "runtime/sharedRuntime.hpp"32#include "runtime/stubRoutines.hpp"33#include "utilities/ostream.hpp"34#ifdef COMPILER135#include "c1/c1_Runtime1.hpp"36#endif3738// We use an illtrap for marking a method as not_entrant or zombie iff !UseSIGTRAP39// Work around a C++ compiler bug which changes 'this'40bool NativeInstruction::is_sigill_zombie_not_entrant_at(address addr) {41assert(!UseSIGTRAP, "precondition");42if (*(int*)addr != 0 /*illtrap*/) return false;43CodeBlob* cb = CodeCache::find_blob_unsafe(addr);44if (cb == NULL || !cb->is_nmethod()) return false;45nmethod *nm = (nmethod *)cb;46// This method is not_entrant or zombie iff the illtrap instruction is47// located at the verified entry point.48return nm->verified_entry_point() == addr;49}5051#ifdef ASSERT52void NativeInstruction::verify() {53// Make sure code pattern is actually an instruction address.54address addr = addr_at(0);55if (addr == 0 || ((intptr_t)addr & 3) != 0) {56fatal("not an instruction address");57}58}59#endif // ASSERT6061// Extract call destination from a NativeCall. The call might use a trampoline stub.62address NativeCall::destination() const {63address addr = (address)this;64address destination = Assembler::bxx_destination(addr);6566// Do we use a trampoline stub for this call?67CodeBlob* cb = CodeCache::find_blob_unsafe(addr); // Else we get assertion if nmethod is zombie.68assert(cb && cb->is_nmethod(), "sanity");69nmethod *nm = (nmethod *)cb;70if (nm->stub_contains(destination) && is_NativeCallTrampolineStub_at(destination)) {71// Yes we do, so get the destination from the trampoline stub.72const address trampoline_stub_addr = destination;73destination = NativeCallTrampolineStub_at(trampoline_stub_addr)->destination(nm);74}7576return destination;77}7879// Similar to replace_mt_safe, but just changes the destination. The80// important thing is that free-running threads are able to execute this81// call instruction at all times. Thus, the displacement field must be82// instruction-word-aligned.83//84// Used in the runtime linkage of calls; see class CompiledIC.85//86// Add parameter assert_lock to switch off assertion87// during code generation, where no patching lock is needed.88void NativeCall::set_destination_mt_safe(address dest, bool assert_lock) {89assert(!assert_lock ||90(Patching_lock->is_locked() || SafepointSynchronize::is_at_safepoint()),91"concurrent code patching");9293ResourceMark rm;94int code_size = 1 * BytesPerInstWord;95address addr_call = addr_at(0);96assert(MacroAssembler::is_bl(*(int*)addr_call), "unexpected code at call-site");9798CodeBuffer cb(addr_call, code_size + 1);99MacroAssembler* a = new MacroAssembler(&cb);100101// Patch the call.102if (!ReoptimizeCallSequences || !a->is_within_range_of_b(dest, addr_call)) {103address trampoline_stub_addr = get_trampoline();104105// We did not find a trampoline stub because the current codeblob106// does not provide this information. The branch will be patched107// later during a final fixup, when all necessary information is108// available.109if (trampoline_stub_addr == 0)110return;111112// Patch the constant in the call's trampoline stub.113NativeCallTrampolineStub_at(trampoline_stub_addr)->set_destination(dest);114dest = trampoline_stub_addr;115}116117OrderAccess::release();118a->bl(dest);119120ICache::ppc64_flush_icache_bytes(addr_call, code_size);121}122123address NativeCall::get_trampoline() {124address call_addr = addr_at(0);125126CodeBlob *code = CodeCache::find_blob(call_addr);127assert(code != NULL, "Could not find the containing code blob");128129// There are no relocations available when the code gets relocated130// because of CodeBuffer expansion.131if (code->relocation_size() == 0)132return NULL;133134address bl_destination = Assembler::bxx_destination(call_addr);135if (code->content_contains(bl_destination) &&136is_NativeCallTrampolineStub_at(bl_destination))137return bl_destination;138139// If the codeBlob is not a nmethod, this is because we get here from the140// CodeBlob constructor, which is called within the nmethod constructor.141return trampoline_stub_Relocation::get_trampoline_for(call_addr, (nmethod*)code);142}143144#ifdef ASSERT145void NativeCall::verify() {146address addr = addr_at(0);147148if (!NativeCall::is_call_at(addr)) {149tty->print_cr("not a NativeCall at " PTR_FORMAT, p2i(addr));150// TODO: PPC port: Disassembler::decode(addr - 20, addr + 20, tty);151fatal(err_msg("not a NativeCall at " PTR_FORMAT, p2i(addr)));152}153}154#endif // ASSERT155156#ifdef ASSERT157void NativeFarCall::verify() {158address addr = addr_at(0);159160NativeInstruction::verify();161if (!NativeFarCall::is_far_call_at(addr)) {162tty->print_cr("not a NativeFarCall at " PTR_FORMAT, p2i(addr));163// TODO: PPC port: Disassembler::decode(addr, 20, 20, tty);164fatal(err_msg("not a NativeFarCall at " PTR_FORMAT, p2i(addr)));165}166}167#endif // ASSERT168169address NativeMovConstReg::next_instruction_address() const {170#ifdef ASSERT171CodeBlob* nm = CodeCache::find_blob(instruction_address());172assert(!MacroAssembler::is_set_narrow_oop(addr_at(0), nm->content_begin()), "Should not patch narrow oop here");173#endif174175if (MacroAssembler::is_load_const_from_method_toc_at(addr_at(0))) {176return addr_at(load_const_from_method_toc_instruction_size);177} else {178return addr_at(load_const_instruction_size);179}180}181182intptr_t NativeMovConstReg::data() const {183address addr = addr_at(0);184185if (MacroAssembler::is_load_const_at(addr)) {186return MacroAssembler::get_const(addr);187}188189CodeBlob* cb = CodeCache::find_blob_unsafe(addr);190if (MacroAssembler::is_set_narrow_oop(addr, cb->content_begin())) {191narrowOop no = (narrowOop)MacroAssembler::get_narrow_oop(addr, cb->content_begin());192return cast_from_oop<intptr_t>(oopDesc::decode_heap_oop(no));193} else {194assert(MacroAssembler::is_load_const_from_method_toc_at(addr), "must be load_const_from_pool");195196address ctable = cb->content_begin();197int offset = MacroAssembler::get_offset_of_load_const_from_method_toc_at(addr);198return *(intptr_t *)(ctable + offset);199}200}201202address NativeMovConstReg::set_data_plain(intptr_t data, CodeBlob *cb) {203address addr = instruction_address();204address next_address = NULL;205if (!cb) cb = CodeCache::find_blob(addr);206207if (cb != NULL && MacroAssembler::is_load_const_from_method_toc_at(addr)) {208// A load from the method's TOC (ctable).209assert(cb->is_nmethod(), "must be nmethod");210const address ctable = cb->content_begin();211const int toc_offset = MacroAssembler::get_offset_of_load_const_from_method_toc_at(addr);212*(intptr_t *)(ctable + toc_offset) = data;213next_address = addr + BytesPerInstWord;214} else if (cb != NULL &&215MacroAssembler::is_calculate_address_from_global_toc_at(addr, cb->content_begin())) {216// A calculation relative to the global TOC.217if (MacroAssembler::get_address_of_calculate_address_from_global_toc_at(addr, cb->content_begin()) !=218(address)data) {219const int invalidated_range =220MacroAssembler::patch_calculate_address_from_global_toc_at(addr, cb->content_begin(),221(address)data);222const address start = invalidated_range < 0 ? addr + invalidated_range : addr;223// FIXME:224const int range = invalidated_range < 0 ? 4 - invalidated_range : 8;225ICache::ppc64_flush_icache_bytes(start, range);226}227next_address = addr + 1 * BytesPerInstWord;228} else if (MacroAssembler::is_load_const_at(addr)) {229// A normal 5 instruction load_const code sequence.230if (MacroAssembler::get_const(addr) != (long)data) {231// This is not mt safe, ok in methods like CodeBuffer::copy_code().232MacroAssembler::patch_const(addr, (long)data);233ICache::ppc64_flush_icache_bytes(addr, load_const_instruction_size);234}235next_address = addr + 5 * BytesPerInstWord;236} else if (MacroAssembler::is_bl(* (int*) addr)) {237// A single branch-and-link instruction.238ResourceMark rm;239const int code_size = 1 * BytesPerInstWord;240CodeBuffer cb(addr, code_size + 1);241MacroAssembler* a = new MacroAssembler(&cb);242a->bl((address) data);243ICache::ppc64_flush_icache_bytes(addr, code_size);244next_address = addr + code_size;245} else {246ShouldNotReachHere();247}248249return next_address;250}251252void NativeMovConstReg::set_data(intptr_t data) {253// Store the value into the instruction stream.254CodeBlob *cb = CodeCache::find_blob(instruction_address());255address next_address = set_data_plain(data, cb);256257// Also store the value into an oop_Relocation cell, if any.258if (cb && cb->is_nmethod()) {259RelocIterator iter((nmethod *) cb, instruction_address(), next_address);260oop* oop_addr = NULL;261Metadata** metadata_addr = NULL;262while (iter.next()) {263if (iter.type() == relocInfo::oop_type) {264oop_Relocation *r = iter.oop_reloc();265if (oop_addr == NULL) {266oop_addr = r->oop_addr();267*oop_addr = cast_to_oop(data);268} else {269assert(oop_addr == r->oop_addr(), "must be only one set-oop here") ;270}271}272if (iter.type() == relocInfo::metadata_type) {273metadata_Relocation *r = iter.metadata_reloc();274if (metadata_addr == NULL) {275metadata_addr = r->metadata_addr();276*metadata_addr = (Metadata*)data;277} else {278assert(metadata_addr == r->metadata_addr(), "must be only one set-metadata here");279}280}281}282}283}284285void NativeMovConstReg::set_narrow_oop(narrowOop data, CodeBlob *code /* = NULL */) {286address addr = addr_at(0);287CodeBlob* cb = (code) ? code : CodeCache::find_blob(instruction_address());288if (MacroAssembler::get_narrow_oop(addr, cb->content_begin()) == (long)data) return;289const int invalidated_range =290MacroAssembler::patch_set_narrow_oop(addr, cb->content_begin(), (long)data);291const address start = invalidated_range < 0 ? addr + invalidated_range : addr;292// FIXME:293const int range = invalidated_range < 0 ? 4 - invalidated_range : 8;294ICache::ppc64_flush_icache_bytes(start, range);295}296297// Do not use an assertion here. Let clients decide whether they only298// want this when assertions are enabled.299#ifdef ASSERT300void NativeMovConstReg::verify() {301address addr = addr_at(0);302if (! MacroAssembler::is_load_const_at(addr) &&303! MacroAssembler::is_load_const_from_method_toc_at(addr)) {304CodeBlob* cb = CodeCache::find_blob_unsafe(addr); // find_nmethod() asserts if nmethod is zombie.305if (! (cb != NULL && MacroAssembler::is_calculate_address_from_global_toc_at(addr, cb->content_begin())) &&306! (cb != NULL && MacroAssembler::is_set_narrow_oop(addr, cb->content_begin())) &&307! MacroAssembler::is_bl(*((int*) addr))) {308tty->print_cr("not a NativeMovConstReg at " PTR_FORMAT, p2i(addr));309// TODO: PPC port: Disassembler::decode(addr, 20, 20, tty);310fatal(err_msg("not a NativeMovConstReg at " PTR_FORMAT, p2i(addr)));311}312}313}314#endif // ASSERT315316void NativeJump::patch_verified_entry(address entry, address verified_entry, address dest) {317ResourceMark rm;318int code_size = 1 * BytesPerInstWord;319CodeBuffer cb(verified_entry, code_size + 1);320MacroAssembler* a = new MacroAssembler(&cb);321#ifdef COMPILER2322assert(dest == SharedRuntime::get_handle_wrong_method_stub(), "expected fixed destination of patch");323#endif324// Patch this nmethod atomically. Always use illtrap/trap in debug build.325if (DEBUG_ONLY(false &&) a->is_within_range_of_b(dest, a->pc())) {326a->b(dest);327} else {328// The signal handler will continue at dest=OptoRuntime::handle_wrong_method_stub().329if (TrapBasedNotEntrantChecks) {330// We use a special trap for marking a method as not_entrant or zombie.331a->trap_zombie_not_entrant();332} else {333// We use an illtrap for marking a method as not_entrant or zombie.334a->illtrap();335}336}337ICache::ppc64_flush_icache_bytes(verified_entry, code_size);338}339340#ifdef ASSERT341void NativeJump::verify() {342address addr = addr_at(0);343344NativeInstruction::verify();345if (!NativeJump::is_jump_at(addr)) {346tty->print_cr("not a NativeJump at " PTR_FORMAT, p2i(addr));347// TODO: PPC port: Disassembler::decode(addr, 20, 20, tty);348fatal(err_msg("not a NativeJump at " PTR_FORMAT, p2i(addr)));349}350}351#endif // ASSERT352353//-------------------------------------------------------------------354355// Call trampoline stubs.356//357// Layout and instructions of a call trampoline stub:358// 0: load the TOC (part 1)359// 4: load the TOC (part 2)360// 8: load the call target from the constant pool (part 1)361// [12: load the call target from the constant pool (part 2, optional)]362// ..: branch via CTR363//364365address NativeCallTrampolineStub::encoded_destination_addr() const {366address instruction_addr = addr_at(2 * BytesPerInstWord);367assert(MacroAssembler::is_ld_largeoffset(instruction_addr),368"must be a ld with large offset (from the constant pool)");369370return instruction_addr;371}372373address NativeCallTrampolineStub::destination(nmethod *nm) const {374CodeBlob* cb = nm ? nm : CodeCache::find_blob_unsafe(addr_at(0));375address ctable = cb->content_begin();376377return *(address*)(ctable + destination_toc_offset());378}379380int NativeCallTrampolineStub::destination_toc_offset() const {381return MacroAssembler::get_ld_largeoffset_offset(encoded_destination_addr());382}383384void NativeCallTrampolineStub::set_destination(address new_destination) {385CodeBlob* cb = CodeCache::find_blob(addr_at(0));386address ctable = cb->content_begin();387388*(address*)(ctable + destination_toc_offset()) = new_destination;389}390391392393