xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/xray/xray_mips.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
168d75effSDimitry Andric //===-- xray_mips.cpp -------------------------------------------*- C++ -*-===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric // Implementation of MIPS-specific routines (32-bit).
1268d75effSDimitry Andric //
1368d75effSDimitry Andric //===----------------------------------------------------------------------===//
1468d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
1568d75effSDimitry Andric #include "xray_defs.h"
1668d75effSDimitry Andric #include "xray_interface_internal.h"
1768d75effSDimitry Andric #include <atomic>
1868d75effSDimitry Andric 
1968d75effSDimitry Andric namespace __xray {
2068d75effSDimitry Andric 
2168d75effSDimitry Andric // The machine codes for some instructions used in runtime patching.
2268d75effSDimitry Andric enum PatchOpcodes : uint32_t {
2368d75effSDimitry Andric   PO_ADDIU = 0x24000000, // addiu rt, rs, imm
2468d75effSDimitry Andric   PO_SW = 0xAC000000,    // sw rt, offset(sp)
2568d75effSDimitry Andric   PO_LUI = 0x3C000000,   // lui rs, %hi(address)
2668d75effSDimitry Andric   PO_ORI = 0x34000000,   // ori rt, rs, %lo(address)
2768d75effSDimitry Andric   PO_JALR = 0x0000F809,  // jalr rs
2868d75effSDimitry Andric   PO_LW = 0x8C000000,    // lw rt, offset(address)
2968d75effSDimitry Andric   PO_B44 = 0x1000000b,   // b #44
3068d75effSDimitry Andric   PO_NOP = 0x0,          // nop
3168d75effSDimitry Andric };
3268d75effSDimitry Andric 
3368d75effSDimitry Andric enum RegNum : uint32_t {
3468d75effSDimitry Andric   RN_T0 = 0x8,
3568d75effSDimitry Andric   RN_T9 = 0x19,
3668d75effSDimitry Andric   RN_RA = 0x1F,
3768d75effSDimitry Andric   RN_SP = 0x1D,
3868d75effSDimitry Andric };
3968d75effSDimitry Andric 
encodeInstruction(uint32_t Opcode,uint32_t Rs,uint32_t Rt,uint32_t Imm)4068d75effSDimitry Andric inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs,
4168d75effSDimitry Andric                                          uint32_t Rt,
4268d75effSDimitry Andric                                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
4368d75effSDimitry Andric   return (Opcode | Rs << 21 | Rt << 16 | Imm);
4468d75effSDimitry Andric }
4568d75effSDimitry Andric 
4668d75effSDimitry Andric inline static uint32_t
encodeSpecialInstruction(uint32_t Opcode,uint32_t Rs,uint32_t Rt,uint32_t Rd,uint32_t Imm)4768d75effSDimitry Andric encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd,
4868d75effSDimitry Andric                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
4968d75effSDimitry Andric   return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode);
5068d75effSDimitry Andric }
5168d75effSDimitry Andric 
patchSled(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* TracingHook)())5268d75effSDimitry Andric inline static bool patchSled(const bool Enable, const uint32_t FuncId,
5368d75effSDimitry Andric                              const XRaySledEntry &Sled,
5468d75effSDimitry Andric                              void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
5568d75effSDimitry Andric   // When |Enable| == true,
5668d75effSDimitry Andric   // We replace the following compile-time stub (sled):
5768d75effSDimitry Andric   //
5868d75effSDimitry Andric   // xray_sled_n:
5968d75effSDimitry Andric   //	B .tmpN
6068d75effSDimitry Andric   //	11 NOPs (44 bytes)
6168d75effSDimitry Andric   //	.tmpN
6268d75effSDimitry Andric   //	ADDIU T9, T9, 44
6368d75effSDimitry Andric   //
6468d75effSDimitry Andric   // With the following runtime patch:
6568d75effSDimitry Andric   //
6668d75effSDimitry Andric   // xray_sled_n (32-bit):
6768d75effSDimitry Andric   //    addiu sp, sp, -8                        ;create stack frame
6868d75effSDimitry Andric   //    nop
6968d75effSDimitry Andric   //    sw ra, 4(sp)                            ;save return address
7068d75effSDimitry Andric   //    sw t9, 0(sp)                            ;save register t9
7168d75effSDimitry Andric   //    lui t9, %hi(__xray_FunctionEntry/Exit)
7268d75effSDimitry Andric   //    ori t9, t9, %lo(__xray_FunctionEntry/Exit)
7368d75effSDimitry Andric   //    lui t0, %hi(function_id)
7468d75effSDimitry Andric   //    jalr t9                                 ;call Tracing hook
7568d75effSDimitry Andric   //    ori t0, t0, %lo(function_id)            ;pass function id (delay slot)
7668d75effSDimitry Andric   //    lw t9, 0(sp)                            ;restore register t9
7768d75effSDimitry Andric   //    lw ra, 4(sp)                            ;restore return address
7868d75effSDimitry Andric   //    addiu sp, sp, 8                         ;delete stack frame
7968d75effSDimitry Andric   //
8068d75effSDimitry Andric   // We add 44 bytes to t9 because we want to adjust the function pointer to
8168d75effSDimitry Andric   // the actual start of function i.e. the address just after the noop sled.
8268d75effSDimitry Andric   // We do this because gp displacement relocation is emitted at the start of
8368d75effSDimitry Andric   // of the function i.e after the nop sled and to correctly calculate the
8468d75effSDimitry Andric   // global offset table address, t9 must hold the address of the instruction
8568d75effSDimitry Andric   // containing the gp displacement relocation.
8668d75effSDimitry Andric   // FIXME: Is this correct for the static relocation model?
8768d75effSDimitry Andric   //
8868d75effSDimitry Andric   // Replacement of the first 4-byte instruction should be the last and atomic
8968d75effSDimitry Andric   // operation, so that the user code which reaches the sled concurrently
9068d75effSDimitry Andric   // either jumps over the whole sled, or executes the whole sled when the
9168d75effSDimitry Andric   // latter is ready.
9268d75effSDimitry Andric   //
9368d75effSDimitry Andric   // When |Enable|==false, we set back the first instruction in the sled to be
9468d75effSDimitry Andric   //   B #44
9568d75effSDimitry Andric 
96*e8d8bef9SDimitry Andric   uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());
9768d75effSDimitry Andric   if (Enable) {
9868d75effSDimitry Andric     uint32_t LoTracingHookAddr =
9968d75effSDimitry Andric         reinterpret_cast<int32_t>(TracingHook) & 0xffff;
10068d75effSDimitry Andric     uint32_t HiTracingHookAddr =
10168d75effSDimitry Andric         (reinterpret_cast<int32_t>(TracingHook) >> 16) & 0xffff;
10268d75effSDimitry Andric     uint32_t LoFunctionID = FuncId & 0xffff;
10368d75effSDimitry Andric     uint32_t HiFunctionID = (FuncId >> 16) & 0xffff;
104*e8d8bef9SDimitry Andric     Address[2] = encodeInstruction(PatchOpcodes::PO_SW, RegNum::RN_SP,
105*e8d8bef9SDimitry Andric                                    RegNum::RN_RA, 0x4);
106*e8d8bef9SDimitry Andric     Address[3] = encodeInstruction(PatchOpcodes::PO_SW, RegNum::RN_SP,
107*e8d8bef9SDimitry Andric                                    RegNum::RN_T9, 0x0);
108*e8d8bef9SDimitry Andric     Address[4] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T9,
109*e8d8bef9SDimitry Andric                                    HiTracingHookAddr);
110*e8d8bef9SDimitry Andric     Address[5] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
111*e8d8bef9SDimitry Andric                                    RegNum::RN_T9, LoTracingHookAddr);
112*e8d8bef9SDimitry Andric     Address[6] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0,
113*e8d8bef9SDimitry Andric                                    HiFunctionID);
114*e8d8bef9SDimitry Andric     Address[7] = encodeSpecialInstruction(PatchOpcodes::PO_JALR, RegNum::RN_T9,
115*e8d8bef9SDimitry Andric                                           0x0, RegNum::RN_RA, 0X0);
116*e8d8bef9SDimitry Andric     Address[8] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T0,
117*e8d8bef9SDimitry Andric                                    RegNum::RN_T0, LoFunctionID);
118*e8d8bef9SDimitry Andric     Address[9] = encodeInstruction(PatchOpcodes::PO_LW, RegNum::RN_SP,
119*e8d8bef9SDimitry Andric                                    RegNum::RN_T9, 0x0);
120*e8d8bef9SDimitry Andric     Address[10] = encodeInstruction(PatchOpcodes::PO_LW, RegNum::RN_SP,
121*e8d8bef9SDimitry Andric                                     RegNum::RN_RA, 0x4);
122*e8d8bef9SDimitry Andric     Address[11] = encodeInstruction(PatchOpcodes::PO_ADDIU, RegNum::RN_SP,
123*e8d8bef9SDimitry Andric                                     RegNum::RN_SP, 0x8);
12468d75effSDimitry Andric     uint32_t CreateStackSpaceInstr = encodeInstruction(
12568d75effSDimitry Andric         PatchOpcodes::PO_ADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xFFF8);
12668d75effSDimitry Andric     std::atomic_store_explicit(
127*e8d8bef9SDimitry Andric         reinterpret_cast<std::atomic<uint32_t> *>(Address),
12868d75effSDimitry Andric         uint32_t(CreateStackSpaceInstr), std::memory_order_release);
12968d75effSDimitry Andric   } else {
13068d75effSDimitry Andric     std::atomic_store_explicit(
131*e8d8bef9SDimitry Andric         reinterpret_cast<std::atomic<uint32_t> *>(Address),
13268d75effSDimitry Andric         uint32_t(PatchOpcodes::PO_B44), std::memory_order_release);
13368d75effSDimitry Andric   }
13468d75effSDimitry Andric   return true;
13568d75effSDimitry Andric }
13668d75effSDimitry Andric 
patchFunctionEntry(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* Trampoline)())13768d75effSDimitry Andric bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
13868d75effSDimitry Andric                         const XRaySledEntry &Sled,
13968d75effSDimitry Andric                         void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
14068d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, Trampoline);
14168d75effSDimitry Andric }
14268d75effSDimitry Andric 
patchFunctionExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)14368d75effSDimitry Andric bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
14468d75effSDimitry Andric                        const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
14568d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
14668d75effSDimitry Andric }
14768d75effSDimitry Andric 
patchFunctionTailExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)14868d75effSDimitry Andric bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
14968d75effSDimitry Andric                            const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
15068d75effSDimitry Andric   // FIXME: In the future we'd need to distinguish between non-tail exits and
15168d75effSDimitry Andric   // tail exits for better information preservation.
15268d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
15368d75effSDimitry Andric }
15468d75effSDimitry Andric 
patchCustomEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)15568d75effSDimitry Andric bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
15668d75effSDimitry Andric                       const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
15768d75effSDimitry Andric   // FIXME: Implement in mips?
15868d75effSDimitry Andric   return false;
15968d75effSDimitry Andric }
16068d75effSDimitry Andric 
patchTypedEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)16168d75effSDimitry Andric bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
16268d75effSDimitry Andric                      const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
16368d75effSDimitry Andric   // FIXME: Implement in mips?
16468d75effSDimitry Andric   return false;
16568d75effSDimitry Andric }
16668d75effSDimitry Andric 
16768d75effSDimitry Andric } // namespace __xray
16868d75effSDimitry Andric 
__xray_ArgLoggerEntry()16968d75effSDimitry Andric extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
17068d75effSDimitry Andric   // FIXME: this will have to be implemented in the trampoline assembly file
17168d75effSDimitry Andric }
172