Path: blob/main/contrib/llvm-project/compiler-rt/lib/xray/xray_loongarch64.cpp
35263 views
//===-------- xray_loongarch64.cpp ------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of XRay, a dynamic runtime instrumentation system.9//10// Implementation of loongarch-specific routines.11//12//===----------------------------------------------------------------------===//13#include "sanitizer_common/sanitizer_common.h"14#include "xray_defs.h"15#include "xray_interface_internal.h"16#include <atomic>1718namespace __xray {1920enum RegNum : uint32_t {21RN_RA = 1,22RN_SP = 3,23RN_T0 = 12,24RN_T1 = 13,25};2627// Encode instructions in the 2RIx format, where the primary formats here28// are 2RI12-type and 2RI16-type.29static inline uint32_t30encodeInstruction2RIx(uint32_t Opcode, uint32_t Rd, uint32_t Rj,31uint32_t Imm) XRAY_NEVER_INSTRUMENT {32return Opcode | (Imm << 10) | (Rj << 5) | Rd;33}3435// Encode instructions in 1RI20 format, e.g. lu12i.w/lu32i.d.36static inline uint32_t37encodeInstruction1RI20(uint32_t Opcode, uint32_t Rd,38uint32_t Imm) XRAY_NEVER_INSTRUMENT {39return Opcode | (Imm << 5) | Rd;40}4142static inline bool patchSled(const bool Enable, const uint32_t FuncId,43const XRaySledEntry &Sled,44void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {45// When |Enable| == true,46// We replace the following compile-time stub (sled):47//48// .Lxray_sled_beginN:49// B .Lxray_sled_endN50// 11 NOPs (44 bytes)51// .Lxray_sled_endN:52//53// With the following runtime patch:54//55// xray_sled_n:56// addi.d sp, sp, -16 ; create the stack frame57// st.d ra, sp, 8 ; save the return address58// lu12i.w t0, %abs_hi20(__xray_FunctionEntry/Exit)59// ori t0, t0, %abs_lo12(__xray_FunctionEntry/Exit)60// lu32i.d t0, %abs64_lo20(__xray_FunctionEntry/Exit)61// lu52i.d t0, t0, %abs64_hi12(__xray_FunctionEntry/Exit)62// lu12i.w t1, %abs_hi20(function_id)63// ori t1, t1, %abs_lo12(function_id) ; pass the function id64// jirl ra, t0, 0 ; call the tracing hook65// ld.d ra, sp, 8 ; restore the return address66// addi.d sp, sp, 16 ; de-allocate the stack frame67//68// Replacement of the first 4-byte instruction should be the last and atomic69// operation, so that the user code which reaches the sled concurrently70// either jumps over the whole sled, or executes the whole sled when the71// latter is ready.72//73// When |Enable|==false, we set the first instruction in the sled back to74// B #487576uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());77if (Enable) {78uint32_t LoTracingHookAddr = reinterpret_cast<int64_t>(TracingHook) & 0xfff;79uint32_t HiTracingHookAddr =80(reinterpret_cast<int64_t>(TracingHook) >> 12) & 0xfffff;81uint32_t HigherTracingHookAddr =82(reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xfffff;83uint32_t HighestTracingHookAddr =84(reinterpret_cast<int64_t>(TracingHook) >> 52) & 0xfff;85uint32_t LoFunctionID = FuncId & 0xfff;86uint32_t HiFunctionID = (FuncId >> 12) & 0xfffff;87Address[1] = encodeInstruction2RIx(0x29c00000, RegNum::RN_RA, RegNum::RN_SP,880x8); // st.d ra, sp, 889Address[2] = encodeInstruction1RI20(900x14000000, RegNum::RN_T0,91HiTracingHookAddr); // lu12i.w t0, HiTracingHookAddr92Address[3] = encodeInstruction2RIx(930x03800000, RegNum::RN_T0, RegNum::RN_T0,94LoTracingHookAddr); // ori t0, t0, LoTracingHookAddr95Address[4] = encodeInstruction1RI20(960x16000000, RegNum::RN_T0,97HigherTracingHookAddr); // lu32i.d t0, HigherTracingHookAddr98Address[5] = encodeInstruction2RIx(990x03000000, RegNum::RN_T0, RegNum::RN_T0,100HighestTracingHookAddr); // lu52i.d t0, t0, HighestTracingHookAddr101Address[6] =102encodeInstruction1RI20(0x14000000, RegNum::RN_T1,103HiFunctionID); // lu12i.w t1, HiFunctionID104Address[7] =105encodeInstruction2RIx(0x03800000, RegNum::RN_T1, RegNum::RN_T1,106LoFunctionID); // ori t1, t1, LoFunctionID107Address[8] = encodeInstruction2RIx(0x4c000000, RegNum::RN_RA, RegNum::RN_T0,1080); // jirl ra, t0, 0109Address[9] = encodeInstruction2RIx(0x28c00000, RegNum::RN_RA, RegNum::RN_SP,1100x8); // ld.d ra, sp, 8111Address[10] = encodeInstruction2RIx(1120x02c00000, RegNum::RN_SP, RegNum::RN_SP, 0x10); // addi.d sp, sp, 16113uint32_t CreateStackSpace = encodeInstruction2RIx(1140x02c00000, RegNum::RN_SP, RegNum::RN_SP, 0xff0); // addi.d sp, sp, -16115std::atomic_store_explicit(116reinterpret_cast<std::atomic<uint32_t> *>(Address), CreateStackSpace,117std::memory_order_release);118} else {119std::atomic_store_explicit(120reinterpret_cast<std::atomic<uint32_t> *>(Address),121uint32_t(0x50003000), std::memory_order_release); // b #48122}123return true;124}125126bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,127const XRaySledEntry &Sled,128void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {129return patchSled(Enable, FuncId, Sled, Trampoline);130}131132bool patchFunctionExit(const bool Enable, const uint32_t FuncId,133const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {134return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);135}136137bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,138const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {139// TODO: In the future we'd need to distinguish between non-tail exits and140// tail exits for better information preservation.141return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);142}143144bool patchCustomEvent(const bool Enable, const uint32_t FuncId,145const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {146// FIXME: Implement in loongarch?147return false;148}149150bool patchTypedEvent(const bool Enable, const uint32_t FuncId,151const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {152// FIXME: Implement in loongarch?153return false;154}155} // namespace __xray156157extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {158// TODO: This will have to be implemented in the trampoline assembly file.159}160161162