170 lines · cpp
1//===-------- xray_loongarch64.cpp ------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of XRay, a dynamic runtime instrumentation system.10//11// Implementation of loongarch-specific routines.12//13//===----------------------------------------------------------------------===//14#include "sanitizer_common/sanitizer_common.h"15#include "xray_defs.h"16#include "xray_interface_internal.h"17#include <atomic>18 19namespace __xray {20 21enum RegNum : uint32_t {22 RN_RA = 1,23 RN_SP = 3,24 RN_T0 = 12,25 RN_T1 = 13,26};27 28// Encode instructions in the 2RIx format, where the primary formats here29// are 2RI12-type and 2RI16-type.30static inline uint32_t31encodeInstruction2RIx(uint32_t Opcode, uint32_t Rd, uint32_t Rj,32 uint32_t Imm) XRAY_NEVER_INSTRUMENT {33 return Opcode | (Imm << 10) | (Rj << 5) | Rd;34}35 36// Encode instructions in 1RI20 format, e.g. lu12i.w/lu32i.d.37static inline uint32_t38encodeInstruction1RI20(uint32_t Opcode, uint32_t Rd,39 uint32_t Imm) XRAY_NEVER_INSTRUMENT {40 return Opcode | (Imm << 5) | Rd;41}42 43static inline bool patchSled(const bool Enable, const uint32_t FuncId,44 const XRaySledEntry &Sled,45 void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {46 // When |Enable| == true,47 // We replace the following compile-time stub (sled):48 //49 // .Lxray_sled_beginN:50 // B .Lxray_sled_endN51 // 11 NOPs (44 bytes)52 // .Lxray_sled_endN:53 //54 // With the following runtime patch:55 //56 // xray_sled_n:57 // addi.d sp, sp, -16 ; create the stack frame58 // st.d ra, sp, 8 ; save the return address59 // lu12i.w t0, %abs_hi20(__xray_FunctionEntry/Exit)60 // ori t0, t0, %abs_lo12(__xray_FunctionEntry/Exit)61 // lu32i.d t0, %abs64_lo20(__xray_FunctionEntry/Exit)62 // lu52i.d t0, t0, %abs64_hi12(__xray_FunctionEntry/Exit)63 // lu12i.w t1, %abs_hi20(function_id)64 // ori t1, t1, %abs_lo12(function_id) ; pass the function id65 // jirl ra, t0, 0 ; call the tracing hook66 // ld.d ra, sp, 8 ; restore the return address67 // addi.d sp, sp, 16 ; de-allocate the stack frame68 //69 // Replacement of the first 4-byte instruction should be the last and atomic70 // operation, so that the user code which reaches the sled concurrently71 // either jumps over the whole sled, or executes the whole sled when the72 // latter is ready.73 //74 // When |Enable|==false, we set the first instruction in the sled back to75 // B #4876 77 uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());78 if (Enable) {79 uint32_t LoTracingHookAddr = reinterpret_cast<int64_t>(TracingHook) & 0xfff;80 uint32_t HiTracingHookAddr =81 (reinterpret_cast<int64_t>(TracingHook) >> 12) & 0xfffff;82 uint32_t HigherTracingHookAddr =83 (reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xfffff;84 uint32_t HighestTracingHookAddr =85 (reinterpret_cast<int64_t>(TracingHook) >> 52) & 0xfff;86 uint32_t LoFunctionID = FuncId & 0xfff;87 uint32_t HiFunctionID = (FuncId >> 12) & 0xfffff;88 Address[1] = encodeInstruction2RIx(0x29c00000, RegNum::RN_RA, RegNum::RN_SP,89 0x8); // st.d ra, sp, 890 Address[2] = encodeInstruction1RI20(91 0x14000000, RegNum::RN_T0,92 HiTracingHookAddr); // lu12i.w t0, HiTracingHookAddr93 Address[3] = encodeInstruction2RIx(94 0x03800000, RegNum::RN_T0, RegNum::RN_T0,95 LoTracingHookAddr); // ori t0, t0, LoTracingHookAddr96 Address[4] = encodeInstruction1RI20(97 0x16000000, RegNum::RN_T0,98 HigherTracingHookAddr); // lu32i.d t0, HigherTracingHookAddr99 Address[5] = encodeInstruction2RIx(100 0x03000000, RegNum::RN_T0, RegNum::RN_T0,101 HighestTracingHookAddr); // lu52i.d t0, t0, HighestTracingHookAddr102 Address[6] =103 encodeInstruction1RI20(0x14000000, RegNum::RN_T1,104 HiFunctionID); // lu12i.w t1, HiFunctionID105 Address[7] =106 encodeInstruction2RIx(0x03800000, RegNum::RN_T1, RegNum::RN_T1,107 LoFunctionID); // ori t1, t1, LoFunctionID108 Address[8] = encodeInstruction2RIx(0x4c000000, RegNum::RN_RA, RegNum::RN_T0,109 0); // jirl ra, t0, 0110 Address[9] = encodeInstruction2RIx(0x28c00000, RegNum::RN_RA, RegNum::RN_SP,111 0x8); // ld.d ra, sp, 8112 Address[10] = encodeInstruction2RIx(113 0x02c00000, RegNum::RN_SP, RegNum::RN_SP, 0x10); // addi.d sp, sp, 16114 uint32_t CreateStackSpace = encodeInstruction2RIx(115 0x02c00000, RegNum::RN_SP, RegNum::RN_SP, 0xff0); // addi.d sp, sp, -16116 std::atomic_store_explicit(117 reinterpret_cast<std::atomic<uint32_t> *>(Address), CreateStackSpace,118 std::memory_order_release);119 } else {120 std::atomic_store_explicit(121 reinterpret_cast<std::atomic<uint32_t> *>(Address),122 uint32_t(0x50003000), std::memory_order_release); // b #48123 }124 return true;125}126 127bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,128 const XRaySledEntry &Sled,129 const XRayTrampolines &Trampolines,130 bool LogArgs) XRAY_NEVER_INSTRUMENT {131 auto Trampoline =132 LogArgs ? Trampolines.LogArgsTrampoline : Trampolines.EntryTrampoline;133 return patchSled(Enable, FuncId, Sled, Trampoline);134}135 136bool patchFunctionExit(137 const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,138 const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {139 return patchSled(Enable, FuncId, Sled, Trampolines.ExitTrampoline);140}141 142bool patchFunctionTailExit(143 const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,144 const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {145 // TODO: In the future we'd need to distinguish between non-tail exits and146 // tail exits for better information preservation.147 return patchSled(Enable, FuncId, Sled, Trampolines.ExitTrampoline);148}149 150bool patchCustomEvent(const bool Enable, const uint32_t FuncId,151 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {152 // FIXME: Implement in loongarch?153 return false;154}155 156bool patchTypedEvent(const bool Enable, const uint32_t FuncId,157 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {158 // FIXME: Implement in loongarch?159 return false;160}161} // namespace __xray162 163extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {164 // TODO: This will have to be implemented in the trampoline assembly file.165}166 167extern "C" void __xray_FunctionTailExit() XRAY_NEVER_INSTRUMENT {168 // FIXME: this will have to be implemented in the trampoline assembly file169}170