xref: /openbsd-src/gnu/llvm/compiler-rt/lib/xray/xray_mips64.cpp (revision d89ec533011f513df1010f142a111086a0785f09)
13cab2bb3Spatrick //===-- xray_mips64.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 MIPS64-specific routines.
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_DADDIU = 0x64000000, // daddiu rt, rs, imm
243cab2bb3Spatrick   PO_SD = 0xFC000000,     // sd rt, base(offset)
253cab2bb3Spatrick   PO_LUI = 0x3C000000,    // lui rt, imm
263cab2bb3Spatrick   PO_ORI = 0x34000000,    // ori rt, rs, imm
273cab2bb3Spatrick   PO_DSLL = 0x00000038,   // dsll rd, rt, sa
283cab2bb3Spatrick   PO_JALR = 0x00000009,   // jalr rs
293cab2bb3Spatrick   PO_LD = 0xDC000000,     // ld rt, base(offset)
303cab2bb3Spatrick   PO_B60 = 0x1000000f,    // b #60
313cab2bb3Spatrick   PO_NOP = 0x0,           // nop
323cab2bb3Spatrick };
333cab2bb3Spatrick 
343cab2bb3Spatrick enum RegNum : uint32_t {
353cab2bb3Spatrick   RN_T0 = 0xC,
363cab2bb3Spatrick   RN_T9 = 0x19,
373cab2bb3Spatrick   RN_RA = 0x1F,
383cab2bb3Spatrick   RN_SP = 0x1D,
393cab2bb3Spatrick };
403cab2bb3Spatrick 
encodeInstruction(uint32_t Opcode,uint32_t Rs,uint32_t Rt,uint32_t Imm)413cab2bb3Spatrick inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs,
423cab2bb3Spatrick                                          uint32_t Rt,
433cab2bb3Spatrick                                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
443cab2bb3Spatrick   return (Opcode | Rs << 21 | Rt << 16 | Imm);
453cab2bb3Spatrick }
463cab2bb3Spatrick 
473cab2bb3Spatrick inline static uint32_t
encodeSpecialInstruction(uint32_t Opcode,uint32_t Rs,uint32_t Rt,uint32_t Rd,uint32_t Imm)483cab2bb3Spatrick encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd,
493cab2bb3Spatrick                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
503cab2bb3Spatrick   return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode);
513cab2bb3Spatrick }
523cab2bb3Spatrick 
patchSled(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* TracingHook)())533cab2bb3Spatrick inline static bool patchSled(const bool Enable, const uint32_t FuncId,
543cab2bb3Spatrick                              const XRaySledEntry &Sled,
553cab2bb3Spatrick                              void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
563cab2bb3Spatrick   // When |Enable| == true,
573cab2bb3Spatrick   // We replace the following compile-time stub (sled):
583cab2bb3Spatrick   //
593cab2bb3Spatrick   // xray_sled_n:
603cab2bb3Spatrick   //	B .tmpN
613cab2bb3Spatrick   //	15 NOPs (60 bytes)
623cab2bb3Spatrick   //	.tmpN
633cab2bb3Spatrick   //
643cab2bb3Spatrick   // With the following runtime patch:
653cab2bb3Spatrick   //
663cab2bb3Spatrick   // xray_sled_n (64-bit):
673cab2bb3Spatrick   //    daddiu sp, sp, -16                      ;create stack frame
683cab2bb3Spatrick   //    nop
693cab2bb3Spatrick   //    sd ra, 8(sp)                            ;save return address
703cab2bb3Spatrick   //    sd t9, 0(sp)                            ;save register t9
713cab2bb3Spatrick   //    lui t9, %highest(__xray_FunctionEntry/Exit)
723cab2bb3Spatrick   //    ori t9, t9, %higher(__xray_FunctionEntry/Exit)
733cab2bb3Spatrick   //    dsll t9, t9, 16
743cab2bb3Spatrick   //    ori t9, t9, %hi(__xray_FunctionEntry/Exit)
753cab2bb3Spatrick   //    dsll t9, t9, 16
763cab2bb3Spatrick   //    ori t9, t9, %lo(__xray_FunctionEntry/Exit)
773cab2bb3Spatrick   //    lui t0, %hi(function_id)
783cab2bb3Spatrick   //    jalr t9                                 ;call Tracing hook
793cab2bb3Spatrick   //    ori t0, t0, %lo(function_id)            ;pass function id (delay slot)
803cab2bb3Spatrick   //    ld t9, 0(sp)                            ;restore register t9
813cab2bb3Spatrick   //    ld ra, 8(sp)                            ;restore return address
823cab2bb3Spatrick   //    daddiu sp, sp, 16                       ;delete stack frame
833cab2bb3Spatrick   //
843cab2bb3Spatrick   // Replacement of the first 4-byte instruction should be the last and atomic
853cab2bb3Spatrick   // operation, so that the user code which reaches the sled concurrently
863cab2bb3Spatrick   // either jumps over the whole sled, or executes the whole sled when the
873cab2bb3Spatrick   // latter is ready.
883cab2bb3Spatrick   //
893cab2bb3Spatrick   // When |Enable|==false, we set back the first instruction in the sled to be
903cab2bb3Spatrick   //   B #60
913cab2bb3Spatrick 
92*d89ec533Spatrick   uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());
933cab2bb3Spatrick   if (Enable) {
943cab2bb3Spatrick     uint32_t LoTracingHookAddr =
953cab2bb3Spatrick         reinterpret_cast<int64_t>(TracingHook) & 0xffff;
963cab2bb3Spatrick     uint32_t HiTracingHookAddr =
973cab2bb3Spatrick         (reinterpret_cast<int64_t>(TracingHook) >> 16) & 0xffff;
983cab2bb3Spatrick     uint32_t HigherTracingHookAddr =
993cab2bb3Spatrick         (reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xffff;
1003cab2bb3Spatrick     uint32_t HighestTracingHookAddr =
1013cab2bb3Spatrick         (reinterpret_cast<int64_t>(TracingHook) >> 48) & 0xffff;
1023cab2bb3Spatrick     uint32_t LoFunctionID = FuncId & 0xffff;
1033cab2bb3Spatrick     uint32_t HiFunctionID = (FuncId >> 16) & 0xffff;
104*d89ec533Spatrick     Address[2] = encodeInstruction(PatchOpcodes::PO_SD, RegNum::RN_SP,
105*d89ec533Spatrick                                    RegNum::RN_RA, 0x8);
106*d89ec533Spatrick     Address[3] = encodeInstruction(PatchOpcodes::PO_SD, RegNum::RN_SP,
107*d89ec533Spatrick                                    RegNum::RN_T9, 0x0);
108*d89ec533Spatrick     Address[4] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T9,
109*d89ec533Spatrick                                    HighestTracingHookAddr);
110*d89ec533Spatrick     Address[5] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
111*d89ec533Spatrick                                    RegNum::RN_T9, HigherTracingHookAddr);
112*d89ec533Spatrick     Address[6] = encodeSpecialInstruction(PatchOpcodes::PO_DSLL, 0x0,
113*d89ec533Spatrick                                           RegNum::RN_T9, RegNum::RN_T9, 0x10);
114*d89ec533Spatrick     Address[7] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
115*d89ec533Spatrick                                    RegNum::RN_T9, HiTracingHookAddr);
116*d89ec533Spatrick     Address[8] = encodeSpecialInstruction(PatchOpcodes::PO_DSLL, 0x0,
117*d89ec533Spatrick                                           RegNum::RN_T9, RegNum::RN_T9, 0x10);
118*d89ec533Spatrick     Address[9] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
119*d89ec533Spatrick                                    RegNum::RN_T9, LoTracingHookAddr);
120*d89ec533Spatrick     Address[10] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0,
121*d89ec533Spatrick                                     HiFunctionID);
122*d89ec533Spatrick     Address[11] = encodeSpecialInstruction(PatchOpcodes::PO_JALR, RegNum::RN_T9,
123*d89ec533Spatrick                                            0x0, RegNum::RN_RA, 0X0);
124*d89ec533Spatrick     Address[12] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T0,
125*d89ec533Spatrick                                     RegNum::RN_T0, LoFunctionID);
126*d89ec533Spatrick     Address[13] = encodeInstruction(PatchOpcodes::PO_LD, RegNum::RN_SP,
127*d89ec533Spatrick                                     RegNum::RN_T9, 0x0);
128*d89ec533Spatrick     Address[14] = encodeInstruction(PatchOpcodes::PO_LD, RegNum::RN_SP,
129*d89ec533Spatrick                                     RegNum::RN_RA, 0x8);
130*d89ec533Spatrick     Address[15] = encodeInstruction(PatchOpcodes::PO_DADDIU, RegNum::RN_SP,
131*d89ec533Spatrick                                     RegNum::RN_SP, 0x10);
1323cab2bb3Spatrick     uint32_t CreateStackSpace = encodeInstruction(
1333cab2bb3Spatrick         PatchOpcodes::PO_DADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xfff0);
1343cab2bb3Spatrick     std::atomic_store_explicit(
135*d89ec533Spatrick         reinterpret_cast<std::atomic<uint32_t> *>(Address), CreateStackSpace,
136*d89ec533Spatrick         std::memory_order_release);
1373cab2bb3Spatrick   } else {
1383cab2bb3Spatrick     std::atomic_store_explicit(
139*d89ec533Spatrick         reinterpret_cast<std::atomic<uint32_t> *>(Address),
1403cab2bb3Spatrick         uint32_t(PatchOpcodes::PO_B60), std::memory_order_release);
1413cab2bb3Spatrick   }
1423cab2bb3Spatrick   return true;
1433cab2bb3Spatrick }
1443cab2bb3Spatrick 
patchFunctionEntry(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* Trampoline)())1453cab2bb3Spatrick bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
1463cab2bb3Spatrick                         const XRaySledEntry &Sled,
1473cab2bb3Spatrick                         void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
1483cab2bb3Spatrick   return patchSled(Enable, FuncId, Sled, Trampoline);
1493cab2bb3Spatrick }
1503cab2bb3Spatrick 
patchFunctionExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1513cab2bb3Spatrick bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
1523cab2bb3Spatrick                        const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1533cab2bb3Spatrick   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
1543cab2bb3Spatrick }
1553cab2bb3Spatrick 
patchFunctionTailExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1563cab2bb3Spatrick bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
1573cab2bb3Spatrick                            const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1583cab2bb3Spatrick   // FIXME: In the future we'd need to distinguish between non-tail exits and
1593cab2bb3Spatrick   // tail exits for better information preservation.
1603cab2bb3Spatrick   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
1613cab2bb3Spatrick }
1623cab2bb3Spatrick 
patchCustomEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1633cab2bb3Spatrick bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
1643cab2bb3Spatrick                       const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1653cab2bb3Spatrick   // FIXME: Implement in mips64?
1663cab2bb3Spatrick   return false;
1673cab2bb3Spatrick }
1683cab2bb3Spatrick 
patchTypedEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1693cab2bb3Spatrick bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
1703cab2bb3Spatrick                      const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1713cab2bb3Spatrick   // FIXME: Implement in mips64?
1723cab2bb3Spatrick   return false;
1733cab2bb3Spatrick }
1743cab2bb3Spatrick } // namespace __xray
1753cab2bb3Spatrick 
__xray_ArgLoggerEntry()1763cab2bb3Spatrick extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
1773cab2bb3Spatrick   // FIXME: this will have to be implemented in the trampoline assembly file
1783cab2bb3Spatrick }
179