13cab2bb3Spatrick //===-- xray_mips.cpp -------------------------------------------*- C++ -*-===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is a part of XRay, a dynamic runtime instrumentation system.
103cab2bb3Spatrick //
113cab2bb3Spatrick // Implementation of MIPS-specific routines (32-bit).
123cab2bb3Spatrick //
133cab2bb3Spatrick //===----------------------------------------------------------------------===//
143cab2bb3Spatrick #include "sanitizer_common/sanitizer_common.h"
153cab2bb3Spatrick #include "xray_defs.h"
163cab2bb3Spatrick #include "xray_interface_internal.h"
173cab2bb3Spatrick #include <atomic>
183cab2bb3Spatrick
193cab2bb3Spatrick namespace __xray {
203cab2bb3Spatrick
213cab2bb3Spatrick // The machine codes for some instructions used in runtime patching.
223cab2bb3Spatrick enum PatchOpcodes : uint32_t {
233cab2bb3Spatrick PO_ADDIU = 0x24000000, // addiu rt, rs, imm
243cab2bb3Spatrick PO_SW = 0xAC000000, // sw rt, offset(sp)
253cab2bb3Spatrick PO_LUI = 0x3C000000, // lui rs, %hi(address)
263cab2bb3Spatrick PO_ORI = 0x34000000, // ori rt, rs, %lo(address)
273cab2bb3Spatrick PO_JALR = 0x0000F809, // jalr rs
283cab2bb3Spatrick PO_LW = 0x8C000000, // lw rt, offset(address)
293cab2bb3Spatrick PO_B44 = 0x1000000b, // b #44
303cab2bb3Spatrick PO_NOP = 0x0, // nop
313cab2bb3Spatrick };
323cab2bb3Spatrick
333cab2bb3Spatrick enum RegNum : uint32_t {
343cab2bb3Spatrick RN_T0 = 0x8,
353cab2bb3Spatrick RN_T9 = 0x19,
363cab2bb3Spatrick RN_RA = 0x1F,
373cab2bb3Spatrick RN_SP = 0x1D,
383cab2bb3Spatrick };
393cab2bb3Spatrick
encodeInstruction(uint32_t Opcode,uint32_t Rs,uint32_t Rt,uint32_t Imm)403cab2bb3Spatrick inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs,
413cab2bb3Spatrick uint32_t Rt,
423cab2bb3Spatrick uint32_t Imm) XRAY_NEVER_INSTRUMENT {
433cab2bb3Spatrick return (Opcode | Rs << 21 | Rt << 16 | Imm);
443cab2bb3Spatrick }
453cab2bb3Spatrick
463cab2bb3Spatrick inline static uint32_t
encodeSpecialInstruction(uint32_t Opcode,uint32_t Rs,uint32_t Rt,uint32_t Rd,uint32_t Imm)473cab2bb3Spatrick encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd,
483cab2bb3Spatrick uint32_t Imm) XRAY_NEVER_INSTRUMENT {
493cab2bb3Spatrick return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode);
503cab2bb3Spatrick }
513cab2bb3Spatrick
patchSled(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* TracingHook)())523cab2bb3Spatrick inline static bool patchSled(const bool Enable, const uint32_t FuncId,
533cab2bb3Spatrick const XRaySledEntry &Sled,
543cab2bb3Spatrick void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
553cab2bb3Spatrick // When |Enable| == true,
563cab2bb3Spatrick // We replace the following compile-time stub (sled):
573cab2bb3Spatrick //
583cab2bb3Spatrick // xray_sled_n:
593cab2bb3Spatrick // B .tmpN
603cab2bb3Spatrick // 11 NOPs (44 bytes)
613cab2bb3Spatrick // .tmpN
623cab2bb3Spatrick // ADDIU T9, T9, 44
633cab2bb3Spatrick //
643cab2bb3Spatrick // With the following runtime patch:
653cab2bb3Spatrick //
663cab2bb3Spatrick // xray_sled_n (32-bit):
673cab2bb3Spatrick // addiu sp, sp, -8 ;create stack frame
683cab2bb3Spatrick // nop
693cab2bb3Spatrick // sw ra, 4(sp) ;save return address
703cab2bb3Spatrick // sw t9, 0(sp) ;save register t9
713cab2bb3Spatrick // lui t9, %hi(__xray_FunctionEntry/Exit)
723cab2bb3Spatrick // ori t9, t9, %lo(__xray_FunctionEntry/Exit)
733cab2bb3Spatrick // lui t0, %hi(function_id)
743cab2bb3Spatrick // jalr t9 ;call Tracing hook
753cab2bb3Spatrick // ori t0, t0, %lo(function_id) ;pass function id (delay slot)
763cab2bb3Spatrick // lw t9, 0(sp) ;restore register t9
773cab2bb3Spatrick // lw ra, 4(sp) ;restore return address
783cab2bb3Spatrick // addiu sp, sp, 8 ;delete stack frame
793cab2bb3Spatrick //
803cab2bb3Spatrick // We add 44 bytes to t9 because we want to adjust the function pointer to
813cab2bb3Spatrick // the actual start of function i.e. the address just after the noop sled.
823cab2bb3Spatrick // We do this because gp displacement relocation is emitted at the start of
833cab2bb3Spatrick // of the function i.e after the nop sled and to correctly calculate the
843cab2bb3Spatrick // global offset table address, t9 must hold the address of the instruction
853cab2bb3Spatrick // containing the gp displacement relocation.
863cab2bb3Spatrick // FIXME: Is this correct for the static relocation model?
873cab2bb3Spatrick //
883cab2bb3Spatrick // Replacement of the first 4-byte instruction should be the last and atomic
893cab2bb3Spatrick // operation, so that the user code which reaches the sled concurrently
903cab2bb3Spatrick // either jumps over the whole sled, or executes the whole sled when the
913cab2bb3Spatrick // latter is ready.
923cab2bb3Spatrick //
933cab2bb3Spatrick // When |Enable|==false, we set back the first instruction in the sled to be
943cab2bb3Spatrick // B #44
953cab2bb3Spatrick
96*d89ec533Spatrick uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());
973cab2bb3Spatrick if (Enable) {
983cab2bb3Spatrick uint32_t LoTracingHookAddr =
993cab2bb3Spatrick reinterpret_cast<int32_t>(TracingHook) & 0xffff;
1003cab2bb3Spatrick uint32_t HiTracingHookAddr =
1013cab2bb3Spatrick (reinterpret_cast<int32_t>(TracingHook) >> 16) & 0xffff;
1023cab2bb3Spatrick uint32_t LoFunctionID = FuncId & 0xffff;
1033cab2bb3Spatrick uint32_t HiFunctionID = (FuncId >> 16) & 0xffff;
104*d89ec533Spatrick Address[2] = encodeInstruction(PatchOpcodes::PO_SW, RegNum::RN_SP,
105*d89ec533Spatrick RegNum::RN_RA, 0x4);
106*d89ec533Spatrick Address[3] = encodeInstruction(PatchOpcodes::PO_SW, RegNum::RN_SP,
107*d89ec533Spatrick RegNum::RN_T9, 0x0);
108*d89ec533Spatrick Address[4] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T9,
109*d89ec533Spatrick HiTracingHookAddr);
110*d89ec533Spatrick Address[5] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
111*d89ec533Spatrick RegNum::RN_T9, LoTracingHookAddr);
112*d89ec533Spatrick Address[6] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0,
113*d89ec533Spatrick HiFunctionID);
114*d89ec533Spatrick Address[7] = encodeSpecialInstruction(PatchOpcodes::PO_JALR, RegNum::RN_T9,
115*d89ec533Spatrick 0x0, RegNum::RN_RA, 0X0);
116*d89ec533Spatrick Address[8] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T0,
117*d89ec533Spatrick RegNum::RN_T0, LoFunctionID);
118*d89ec533Spatrick Address[9] = encodeInstruction(PatchOpcodes::PO_LW, RegNum::RN_SP,
119*d89ec533Spatrick RegNum::RN_T9, 0x0);
120*d89ec533Spatrick Address[10] = encodeInstruction(PatchOpcodes::PO_LW, RegNum::RN_SP,
121*d89ec533Spatrick RegNum::RN_RA, 0x4);
122*d89ec533Spatrick Address[11] = encodeInstruction(PatchOpcodes::PO_ADDIU, RegNum::RN_SP,
123*d89ec533Spatrick RegNum::RN_SP, 0x8);
1243cab2bb3Spatrick uint32_t CreateStackSpaceInstr = encodeInstruction(
1253cab2bb3Spatrick PatchOpcodes::PO_ADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xFFF8);
1263cab2bb3Spatrick std::atomic_store_explicit(
127*d89ec533Spatrick reinterpret_cast<std::atomic<uint32_t> *>(Address),
1283cab2bb3Spatrick uint32_t(CreateStackSpaceInstr), std::memory_order_release);
1293cab2bb3Spatrick } else {
1303cab2bb3Spatrick std::atomic_store_explicit(
131*d89ec533Spatrick reinterpret_cast<std::atomic<uint32_t> *>(Address),
1323cab2bb3Spatrick uint32_t(PatchOpcodes::PO_B44), std::memory_order_release);
1333cab2bb3Spatrick }
1343cab2bb3Spatrick return true;
1353cab2bb3Spatrick }
1363cab2bb3Spatrick
patchFunctionEntry(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* Trampoline)())1373cab2bb3Spatrick bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
1383cab2bb3Spatrick const XRaySledEntry &Sled,
1393cab2bb3Spatrick void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
1403cab2bb3Spatrick return patchSled(Enable, FuncId, Sled, Trampoline);
1413cab2bb3Spatrick }
1423cab2bb3Spatrick
patchFunctionExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1433cab2bb3Spatrick bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
1443cab2bb3Spatrick const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1453cab2bb3Spatrick return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
1463cab2bb3Spatrick }
1473cab2bb3Spatrick
patchFunctionTailExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1483cab2bb3Spatrick bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
1493cab2bb3Spatrick const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1503cab2bb3Spatrick // FIXME: In the future we'd need to distinguish between non-tail exits and
1513cab2bb3Spatrick // tail exits for better information preservation.
1523cab2bb3Spatrick return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
1533cab2bb3Spatrick }
1543cab2bb3Spatrick
patchCustomEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1553cab2bb3Spatrick bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
1563cab2bb3Spatrick const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1573cab2bb3Spatrick // FIXME: Implement in mips?
1583cab2bb3Spatrick return false;
1593cab2bb3Spatrick }
1603cab2bb3Spatrick
patchTypedEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1613cab2bb3Spatrick bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
1623cab2bb3Spatrick const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1633cab2bb3Spatrick // FIXME: Implement in mips?
1643cab2bb3Spatrick return false;
1653cab2bb3Spatrick }
1663cab2bb3Spatrick
1673cab2bb3Spatrick } // namespace __xray
1683cab2bb3Spatrick
__xray_ArgLoggerEntry()1693cab2bb3Spatrick extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
1703cab2bb3Spatrick // FIXME: this will have to be implemented in the trampoline assembly file
1713cab2bb3Spatrick }
172