Path: blob/main/contrib/llvm-project/compiler-rt/lib/xray/xray_mips64.cpp
35265 views
//===-- xray_mips64.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 MIPS64-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 {1920// The machine codes for some instructions used in runtime patching.21enum PatchOpcodes : uint32_t {22PO_DADDIU = 0x64000000, // daddiu rt, rs, imm23PO_SD = 0xFC000000, // sd rt, base(offset)24PO_LUI = 0x3C000000, // lui rt, imm25PO_ORI = 0x34000000, // ori rt, rs, imm26PO_DSLL = 0x00000038, // dsll rd, rt, sa27PO_JALR = 0x00000009, // jalr rs28PO_LD = 0xDC000000, // ld rt, base(offset)29PO_B60 = 0x1000000f, // b #6030PO_NOP = 0x0, // nop31};3233enum RegNum : uint32_t {34RN_T0 = 0xC,35RN_T9 = 0x19,36RN_RA = 0x1F,37RN_SP = 0x1D,38};3940inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs,41uint32_t Rt,42uint32_t Imm) XRAY_NEVER_INSTRUMENT {43return (Opcode | Rs << 21 | Rt << 16 | Imm);44}4546inline static uint32_t47encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd,48uint32_t Imm) XRAY_NEVER_INSTRUMENT {49return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode);50}5152inline static bool patchSled(const bool Enable, const uint32_t FuncId,53const XRaySledEntry &Sled,54void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {55// When |Enable| == true,56// We replace the following compile-time stub (sled):57//58// xray_sled_n:59// B .tmpN60// 15 NOPs (60 bytes)61// .tmpN62//63// With the following runtime patch:64//65// xray_sled_n (64-bit):66// daddiu sp, sp, -16 ;create stack frame67// nop68// sd ra, 8(sp) ;save return address69// sd t9, 0(sp) ;save register t970// lui t9, %highest(__xray_FunctionEntry/Exit)71// ori t9, t9, %higher(__xray_FunctionEntry/Exit)72// dsll t9, t9, 1673// ori t9, t9, %hi(__xray_FunctionEntry/Exit)74// dsll t9, t9, 1675// ori t9, t9, %lo(__xray_FunctionEntry/Exit)76// lui t0, %hi(function_id)77// jalr t9 ;call Tracing hook78// ori t0, t0, %lo(function_id) ;pass function id (delay slot)79// ld t9, 0(sp) ;restore register t980// ld ra, 8(sp) ;restore return address81// daddiu sp, sp, 16 ;delete stack frame82//83// Replacement of the first 4-byte instruction should be the last and atomic84// operation, so that the user code which reaches the sled concurrently85// either jumps over the whole sled, or executes the whole sled when the86// latter is ready.87//88// When |Enable|==false, we set back the first instruction in the sled to be89// B #609091uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());92if (Enable) {93uint32_t LoTracingHookAddr =94reinterpret_cast<int64_t>(TracingHook) & 0xffff;95uint32_t HiTracingHookAddr =96(reinterpret_cast<int64_t>(TracingHook) >> 16) & 0xffff;97uint32_t HigherTracingHookAddr =98(reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xffff;99uint32_t HighestTracingHookAddr =100(reinterpret_cast<int64_t>(TracingHook) >> 48) & 0xffff;101uint32_t LoFunctionID = FuncId & 0xffff;102uint32_t HiFunctionID = (FuncId >> 16) & 0xffff;103Address[2] = encodeInstruction(PatchOpcodes::PO_SD, RegNum::RN_SP,104RegNum::RN_RA, 0x8);105Address[3] = encodeInstruction(PatchOpcodes::PO_SD, RegNum::RN_SP,106RegNum::RN_T9, 0x0);107Address[4] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T9,108HighestTracingHookAddr);109Address[5] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,110RegNum::RN_T9, HigherTracingHookAddr);111Address[6] = encodeSpecialInstruction(PatchOpcodes::PO_DSLL, 0x0,112RegNum::RN_T9, RegNum::RN_T9, 0x10);113Address[7] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,114RegNum::RN_T9, HiTracingHookAddr);115Address[8] = encodeSpecialInstruction(PatchOpcodes::PO_DSLL, 0x0,116RegNum::RN_T9, RegNum::RN_T9, 0x10);117Address[9] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,118RegNum::RN_T9, LoTracingHookAddr);119Address[10] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0,120HiFunctionID);121Address[11] = encodeSpecialInstruction(PatchOpcodes::PO_JALR, RegNum::RN_T9,1220x0, RegNum::RN_RA, 0X0);123Address[12] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T0,124RegNum::RN_T0, LoFunctionID);125Address[13] = encodeInstruction(PatchOpcodes::PO_LD, RegNum::RN_SP,126RegNum::RN_T9, 0x0);127Address[14] = encodeInstruction(PatchOpcodes::PO_LD, RegNum::RN_SP,128RegNum::RN_RA, 0x8);129Address[15] = encodeInstruction(PatchOpcodes::PO_DADDIU, RegNum::RN_SP,130RegNum::RN_SP, 0x10);131uint32_t CreateStackSpace = encodeInstruction(132PatchOpcodes::PO_DADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xfff0);133std::atomic_store_explicit(134reinterpret_cast<std::atomic<uint32_t> *>(Address), CreateStackSpace,135std::memory_order_release);136} else {137std::atomic_store_explicit(138reinterpret_cast<std::atomic<uint32_t> *>(Address),139uint32_t(PatchOpcodes::PO_B60), std::memory_order_release);140}141return true;142}143144bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,145const XRaySledEntry &Sled,146void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {147return patchSled(Enable, FuncId, Sled, Trampoline);148}149150bool patchFunctionExit(const bool Enable, const uint32_t FuncId,151const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {152return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);153}154155bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,156const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {157// FIXME: In the future we'd need to distinguish between non-tail exits and158// tail exits for better information preservation.159return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);160}161162bool patchCustomEvent(const bool Enable, const uint32_t FuncId,163const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {164// FIXME: Implement in mips64?165return false;166}167168bool patchTypedEvent(const bool Enable, const uint32_t FuncId,169const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {170// FIXME: Implement in mips64?171return false;172}173} // namespace __xray174175extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {176// FIXME: this will have to be implemented in the trampoline assembly file177}178179180