xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/xray/xray_mips64.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
168d75effSDimitry Andric //===-- xray_mips64.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 MIPS64-specific routines.
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_DADDIU = 0x64000000, // daddiu rt, rs, imm
2468d75effSDimitry Andric   PO_SD = 0xFC000000,     // sd rt, base(offset)
2568d75effSDimitry Andric   PO_LUI = 0x3C000000,    // lui rt, imm
2668d75effSDimitry Andric   PO_ORI = 0x34000000,    // ori rt, rs, imm
2768d75effSDimitry Andric   PO_DSLL = 0x00000038,   // dsll rd, rt, sa
2868d75effSDimitry Andric   PO_JALR = 0x00000009,   // jalr rs
2968d75effSDimitry Andric   PO_LD = 0xDC000000,     // ld rt, base(offset)
3068d75effSDimitry Andric   PO_B60 = 0x1000000f,    // b #60
3168d75effSDimitry Andric   PO_NOP = 0x0,           // nop
3268d75effSDimitry Andric };
3368d75effSDimitry Andric 
3468d75effSDimitry Andric enum RegNum : uint32_t {
3568d75effSDimitry Andric   RN_T0 = 0xC,
3668d75effSDimitry Andric   RN_T9 = 0x19,
3768d75effSDimitry Andric   RN_RA = 0x1F,
3868d75effSDimitry Andric   RN_SP = 0x1D,
3968d75effSDimitry Andric };
4068d75effSDimitry Andric 
encodeInstruction(uint32_t Opcode,uint32_t Rs,uint32_t Rt,uint32_t Imm)4168d75effSDimitry Andric inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs,
4268d75effSDimitry Andric                                          uint32_t Rt,
4368d75effSDimitry Andric                                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
4468d75effSDimitry Andric   return (Opcode | Rs << 21 | Rt << 16 | Imm);
4568d75effSDimitry Andric }
4668d75effSDimitry Andric 
4768d75effSDimitry Andric inline static uint32_t
encodeSpecialInstruction(uint32_t Opcode,uint32_t Rs,uint32_t Rt,uint32_t Rd,uint32_t Imm)4868d75effSDimitry Andric encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd,
4968d75effSDimitry Andric                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
5068d75effSDimitry Andric   return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode);
5168d75effSDimitry Andric }
5268d75effSDimitry Andric 
patchSled(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* TracingHook)())5368d75effSDimitry Andric inline static bool patchSled(const bool Enable, const uint32_t FuncId,
5468d75effSDimitry Andric                              const XRaySledEntry &Sled,
5568d75effSDimitry Andric                              void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
5668d75effSDimitry Andric   // When |Enable| == true,
5768d75effSDimitry Andric   // We replace the following compile-time stub (sled):
5868d75effSDimitry Andric   //
5968d75effSDimitry Andric   // xray_sled_n:
6068d75effSDimitry Andric   //	B .tmpN
6168d75effSDimitry Andric   //	15 NOPs (60 bytes)
6268d75effSDimitry Andric   //	.tmpN
6368d75effSDimitry Andric   //
6468d75effSDimitry Andric   // With the following runtime patch:
6568d75effSDimitry Andric   //
6668d75effSDimitry Andric   // xray_sled_n (64-bit):
6768d75effSDimitry Andric   //    daddiu sp, sp, -16                      ;create stack frame
6868d75effSDimitry Andric   //    nop
6968d75effSDimitry Andric   //    sd ra, 8(sp)                            ;save return address
7068d75effSDimitry Andric   //    sd t9, 0(sp)                            ;save register t9
7168d75effSDimitry Andric   //    lui t9, %highest(__xray_FunctionEntry/Exit)
7268d75effSDimitry Andric   //    ori t9, t9, %higher(__xray_FunctionEntry/Exit)
7368d75effSDimitry Andric   //    dsll t9, t9, 16
7468d75effSDimitry Andric   //    ori t9, t9, %hi(__xray_FunctionEntry/Exit)
7568d75effSDimitry Andric   //    dsll t9, t9, 16
7668d75effSDimitry Andric   //    ori t9, t9, %lo(__xray_FunctionEntry/Exit)
7768d75effSDimitry Andric   //    lui t0, %hi(function_id)
7868d75effSDimitry Andric   //    jalr t9                                 ;call Tracing hook
7968d75effSDimitry Andric   //    ori t0, t0, %lo(function_id)            ;pass function id (delay slot)
8068d75effSDimitry Andric   //    ld t9, 0(sp)                            ;restore register t9
8168d75effSDimitry Andric   //    ld ra, 8(sp)                            ;restore return address
8268d75effSDimitry Andric   //    daddiu sp, sp, 16                       ;delete stack frame
8368d75effSDimitry Andric   //
8468d75effSDimitry Andric   // Replacement of the first 4-byte instruction should be the last and atomic
8568d75effSDimitry Andric   // operation, so that the user code which reaches the sled concurrently
8668d75effSDimitry Andric   // either jumps over the whole sled, or executes the whole sled when the
8768d75effSDimitry Andric   // latter is ready.
8868d75effSDimitry Andric   //
8968d75effSDimitry Andric   // When |Enable|==false, we set back the first instruction in the sled to be
9068d75effSDimitry Andric   //   B #60
9168d75effSDimitry Andric 
92*e8d8bef9SDimitry Andric   uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());
9368d75effSDimitry Andric   if (Enable) {
9468d75effSDimitry Andric     uint32_t LoTracingHookAddr =
9568d75effSDimitry Andric         reinterpret_cast<int64_t>(TracingHook) & 0xffff;
9668d75effSDimitry Andric     uint32_t HiTracingHookAddr =
9768d75effSDimitry Andric         (reinterpret_cast<int64_t>(TracingHook) >> 16) & 0xffff;
9868d75effSDimitry Andric     uint32_t HigherTracingHookAddr =
9968d75effSDimitry Andric         (reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xffff;
10068d75effSDimitry Andric     uint32_t HighestTracingHookAddr =
10168d75effSDimitry Andric         (reinterpret_cast<int64_t>(TracingHook) >> 48) & 0xffff;
10268d75effSDimitry Andric     uint32_t LoFunctionID = FuncId & 0xffff;
10368d75effSDimitry Andric     uint32_t HiFunctionID = (FuncId >> 16) & 0xffff;
104*e8d8bef9SDimitry Andric     Address[2] = encodeInstruction(PatchOpcodes::PO_SD, RegNum::RN_SP,
105*e8d8bef9SDimitry Andric                                    RegNum::RN_RA, 0x8);
106*e8d8bef9SDimitry Andric     Address[3] = encodeInstruction(PatchOpcodes::PO_SD, 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                                    HighestTracingHookAddr);
110*e8d8bef9SDimitry Andric     Address[5] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
111*e8d8bef9SDimitry Andric                                    RegNum::RN_T9, HigherTracingHookAddr);
112*e8d8bef9SDimitry Andric     Address[6] = encodeSpecialInstruction(PatchOpcodes::PO_DSLL, 0x0,
113*e8d8bef9SDimitry Andric                                           RegNum::RN_T9, RegNum::RN_T9, 0x10);
114*e8d8bef9SDimitry Andric     Address[7] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
115*e8d8bef9SDimitry Andric                                    RegNum::RN_T9, HiTracingHookAddr);
116*e8d8bef9SDimitry Andric     Address[8] = encodeSpecialInstruction(PatchOpcodes::PO_DSLL, 0x0,
117*e8d8bef9SDimitry Andric                                           RegNum::RN_T9, RegNum::RN_T9, 0x10);
118*e8d8bef9SDimitry Andric     Address[9] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
119*e8d8bef9SDimitry Andric                                    RegNum::RN_T9, LoTracingHookAddr);
120*e8d8bef9SDimitry Andric     Address[10] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0,
121*e8d8bef9SDimitry Andric                                     HiFunctionID);
122*e8d8bef9SDimitry Andric     Address[11] = encodeSpecialInstruction(PatchOpcodes::PO_JALR, RegNum::RN_T9,
123*e8d8bef9SDimitry Andric                                            0x0, RegNum::RN_RA, 0X0);
124*e8d8bef9SDimitry Andric     Address[12] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T0,
125*e8d8bef9SDimitry Andric                                     RegNum::RN_T0, LoFunctionID);
126*e8d8bef9SDimitry Andric     Address[13] = encodeInstruction(PatchOpcodes::PO_LD, RegNum::RN_SP,
127*e8d8bef9SDimitry Andric                                     RegNum::RN_T9, 0x0);
128*e8d8bef9SDimitry Andric     Address[14] = encodeInstruction(PatchOpcodes::PO_LD, RegNum::RN_SP,
129*e8d8bef9SDimitry Andric                                     RegNum::RN_RA, 0x8);
130*e8d8bef9SDimitry Andric     Address[15] = encodeInstruction(PatchOpcodes::PO_DADDIU, RegNum::RN_SP,
131*e8d8bef9SDimitry Andric                                     RegNum::RN_SP, 0x10);
13268d75effSDimitry Andric     uint32_t CreateStackSpace = encodeInstruction(
13368d75effSDimitry Andric         PatchOpcodes::PO_DADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xfff0);
13468d75effSDimitry Andric     std::atomic_store_explicit(
135*e8d8bef9SDimitry Andric         reinterpret_cast<std::atomic<uint32_t> *>(Address), CreateStackSpace,
136*e8d8bef9SDimitry Andric         std::memory_order_release);
13768d75effSDimitry Andric   } else {
13868d75effSDimitry Andric     std::atomic_store_explicit(
139*e8d8bef9SDimitry Andric         reinterpret_cast<std::atomic<uint32_t> *>(Address),
14068d75effSDimitry Andric         uint32_t(PatchOpcodes::PO_B60), std::memory_order_release);
14168d75effSDimitry Andric   }
14268d75effSDimitry Andric   return true;
14368d75effSDimitry Andric }
14468d75effSDimitry Andric 
patchFunctionEntry(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* Trampoline)())14568d75effSDimitry Andric bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
14668d75effSDimitry Andric                         const XRaySledEntry &Sled,
14768d75effSDimitry Andric                         void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
14868d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, Trampoline);
14968d75effSDimitry Andric }
15068d75effSDimitry Andric 
patchFunctionExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)15168d75effSDimitry Andric bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
15268d75effSDimitry Andric                        const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
15368d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
15468d75effSDimitry Andric }
15568d75effSDimitry Andric 
patchFunctionTailExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)15668d75effSDimitry Andric bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
15768d75effSDimitry Andric                            const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
15868d75effSDimitry Andric   // FIXME: In the future we'd need to distinguish between non-tail exits and
15968d75effSDimitry Andric   // tail exits for better information preservation.
16068d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
16168d75effSDimitry Andric }
16268d75effSDimitry Andric 
patchCustomEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)16368d75effSDimitry Andric bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
16468d75effSDimitry Andric                       const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
16568d75effSDimitry Andric   // FIXME: Implement in mips64?
16668d75effSDimitry Andric   return false;
16768d75effSDimitry Andric }
16868d75effSDimitry Andric 
patchTypedEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)16968d75effSDimitry Andric bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
17068d75effSDimitry Andric                      const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
17168d75effSDimitry Andric   // FIXME: Implement in mips64?
17268d75effSDimitry Andric   return false;
17368d75effSDimitry Andric }
17468d75effSDimitry Andric } // namespace __xray
17568d75effSDimitry Andric 
__xray_ArgLoggerEntry()17668d75effSDimitry Andric extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
17768d75effSDimitry Andric   // FIXME: this will have to be implemented in the trampoline assembly file
17868d75effSDimitry Andric }
179