1 /*
  2  * Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
  4  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #include "precompiled.hpp"
 28 #include "asm/macroAssembler.hpp"
 29 #include "code/relocInfo.hpp"
 30 #include "nativeInst_riscv.hpp"
 31 #include "oops/oop.inline.hpp"
 32 #include "runtime/safepoint.hpp"
 33 
 34 void Relocation::pd_set_data_value(address x, intptr_t o, bool verify_only) {
 35   if (verify_only) {
 36     return;
 37   }
 38 
 39   int bytes;
 40 
 41   switch(type()) {
 42     case relocInfo::oop_type: {
 43       oop_Relocation *reloc = (oop_Relocation *)this;
 44       // in movoop when BarrierSet::barrier_set()->barrier_set_nmethod() != NULL || !immediate
 45       if (NativeInstruction::is_load_pc_relative_at(addr())) {
 46         address constptr = (address)code()->oop_addr_at(reloc->oop_index());
 47         bytes = MacroAssembler::pd_patch_instruction_size(addr(), constptr);
 48         assert(*(address*)constptr == x, "error in oop relocation");
 49       } else {
 50         bytes = MacroAssembler::patch_oop(addr(), x);
 51       }
 52       break;
 53     }
 54     default:
 55       bytes = MacroAssembler::pd_patch_instruction_size(addr(), x);
 56       break;
 57   }
 58   ICache::invalidate_range(addr(), bytes);
 59 }
 60 
 61 address Relocation::pd_call_destination(address orig_addr) {
 62   assert(is_call(), "should be an address instruction here");
 63   if (NativeCall::is_call_at(addr())) {
 64     address trampoline = nativeCall_at(addr())->get_trampoline();
 65     if (trampoline != NULL) {
 66       return nativeCallTrampolineStub_at(trampoline)->destination();
 67     }
 68   }
 69   if (orig_addr != NULL) {
 70     // the extracted address from the instructions in address orig_addr
 71     address new_addr = MacroAssembler::pd_call_destination(orig_addr);
 72     // If call is branch to self, don't try to relocate it, just leave it
 73     // as branch to self. This happens during code generation if the code
 74     // buffer expands. It will be relocated to the trampoline above once
 75     // code generation is complete.
 76     new_addr = (new_addr == orig_addr) ? addr() : new_addr;
 77     return new_addr;
 78   }
 79   return MacroAssembler::pd_call_destination(addr());
 80 }
 81 
 82 void Relocation::pd_set_call_destination(address x) {
 83   assert(is_call(), "should be an address instruction here");
 84   if (NativeCall::is_call_at(addr())) {
 85     address trampoline = nativeCall_at(addr())->get_trampoline();
 86     if (trampoline != NULL) {
 87       nativeCall_at(addr())->set_destination_mt_safe(x, /* assert_lock */false);
 88       return;
 89     }
 90   }
 91   MacroAssembler::pd_patch_instruction_size(addr(), x);
 92   address pd_call = pd_call_destination(addr());
 93   assert(pd_call == x, "fail in reloc");
 94 }
 95 
 96 address* Relocation::pd_address_in_code() {
 97   assert(NativeCall::is_load_pc_relative_at(addr()), "Not the expected instruction sequence!");
 98   return (address*)(MacroAssembler::target_addr_for_insn(addr()));
 99 }
100 
101 address Relocation::pd_get_address_from_code() {
102   return MacroAssembler::pd_call_destination(addr());
103 }
104 
105 void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
106   if (NativeInstruction::maybe_cpool_ref(addr())) {
107     address old_addr = old_addr_for(addr(), src, dest);
108     MacroAssembler::pd_patch_instruction_size(addr(), MacroAssembler::target_addr_for_insn(old_addr));
109   }
110 }
111 
112 void metadata_Relocation::pd_fix_value(address x) {
113 }