Path: blob/main/contrib/llvm-project/compiler-rt/lib/xray/xray_mips.cpp
35265 views
//===-- xray_mips.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 MIPS-specific routines (32-bit).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_ADDIU = 0x24000000, // addiu rt, rs, imm23PO_SW = 0xAC000000, // sw rt, offset(sp)24PO_LUI = 0x3C000000, // lui rs, %hi(address)25PO_ORI = 0x34000000, // ori rt, rs, %lo(address)26PO_JALR = 0x0000F809, // jalr rs27PO_LW = 0x8C000000, // lw rt, offset(address)28PO_B44 = 0x1000000b, // b #4429PO_NOP = 0x0, // nop30};3132enum RegNum : uint32_t {33RN_T0 = 0x8,34RN_T9 = 0x19,35RN_RA = 0x1F,36RN_SP = 0x1D,37};3839inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs,40uint32_t Rt,41uint32_t Imm) XRAY_NEVER_INSTRUMENT {42return (Opcode | Rs << 21 | Rt << 16 | Imm);43}4445inline static uint32_t46encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd,47uint32_t Imm) XRAY_NEVER_INSTRUMENT {48return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode);49}5051inline static bool patchSled(const bool Enable, const uint32_t FuncId,52const XRaySledEntry &Sled,53void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {54// When |Enable| == true,55// We replace the following compile-time stub (sled):56//57// xray_sled_n:58// B .tmpN59// 11 NOPs (44 bytes)60// .tmpN61// ADDIU T9, T9, 4462//63// With the following runtime patch:64//65// xray_sled_n (32-bit):66// addiu sp, sp, -8 ;create stack frame67// nop68// sw ra, 4(sp) ;save return address69// sw t9, 0(sp) ;save register t970// lui t9, %hi(__xray_FunctionEntry/Exit)71// ori t9, t9, %lo(__xray_FunctionEntry/Exit)72// lui t0, %hi(function_id)73// jalr t9 ;call Tracing hook74// ori t0, t0, %lo(function_id) ;pass function id (delay slot)75// lw t9, 0(sp) ;restore register t976// lw ra, 4(sp) ;restore return address77// addiu sp, sp, 8 ;delete stack frame78//79// We add 44 bytes to t9 because we want to adjust the function pointer to80// the actual start of function i.e. the address just after the noop sled.81// We do this because gp displacement relocation is emitted at the start of82// of the function i.e after the nop sled and to correctly calculate the83// global offset table address, t9 must hold the address of the instruction84// containing the gp displacement relocation.85// FIXME: Is this correct for the static relocation model?86//87// Replacement of the first 4-byte instruction should be the last and atomic88// operation, so that the user code which reaches the sled concurrently89// either jumps over the whole sled, or executes the whole sled when the90// latter is ready.91//92// When |Enable|==false, we set back the first instruction in the sled to be93// B #449495uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());96if (Enable) {97uint32_t LoTracingHookAddr =98reinterpret_cast<int32_t>(TracingHook) & 0xffff;99uint32_t HiTracingHookAddr =100(reinterpret_cast<int32_t>(TracingHook) >> 16) & 0xffff;101uint32_t LoFunctionID = FuncId & 0xffff;102uint32_t HiFunctionID = (FuncId >> 16) & 0xffff;103Address[2] = encodeInstruction(PatchOpcodes::PO_SW, RegNum::RN_SP,104RegNum::RN_RA, 0x4);105Address[3] = encodeInstruction(PatchOpcodes::PO_SW, RegNum::RN_SP,106RegNum::RN_T9, 0x0);107Address[4] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T9,108HiTracingHookAddr);109Address[5] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,110RegNum::RN_T9, LoTracingHookAddr);111Address[6] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0,112HiFunctionID);113Address[7] = encodeSpecialInstruction(PatchOpcodes::PO_JALR, RegNum::RN_T9,1140x0, RegNum::RN_RA, 0X0);115Address[8] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T0,116RegNum::RN_T0, LoFunctionID);117Address[9] = encodeInstruction(PatchOpcodes::PO_LW, RegNum::RN_SP,118RegNum::RN_T9, 0x0);119Address[10] = encodeInstruction(PatchOpcodes::PO_LW, RegNum::RN_SP,120RegNum::RN_RA, 0x4);121Address[11] = encodeInstruction(PatchOpcodes::PO_ADDIU, RegNum::RN_SP,122RegNum::RN_SP, 0x8);123uint32_t CreateStackSpaceInstr = encodeInstruction(124PatchOpcodes::PO_ADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xFFF8);125std::atomic_store_explicit(126reinterpret_cast<std::atomic<uint32_t> *>(Address),127uint32_t(CreateStackSpaceInstr), std::memory_order_release);128} else {129std::atomic_store_explicit(130reinterpret_cast<std::atomic<uint32_t> *>(Address),131uint32_t(PatchOpcodes::PO_B44), std::memory_order_release);132}133return true;134}135136bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,137const XRaySledEntry &Sled,138void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {139return patchSled(Enable, FuncId, Sled, Trampoline);140}141142bool patchFunctionExit(const bool Enable, const uint32_t FuncId,143const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {144return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);145}146147bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,148const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {149// FIXME: In the future we'd need to distinguish between non-tail exits and150// tail exits for better information preservation.151return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);152}153154bool patchCustomEvent(const bool Enable, const uint32_t FuncId,155const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {156// FIXME: Implement in mips?157return false;158}159160bool patchTypedEvent(const bool Enable, const uint32_t FuncId,161const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {162// FIXME: Implement in mips?163return false;164}165166} // namespace __xray167168extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {169// FIXME: this will have to be implemented in the trampoline assembly file170}171172173