1 1.1 kamil //===-- xray_mips64.cc ------------------------------------------*- C++ -*-===// 2 1.1 kamil // 3 1.1 kamil // The LLVM Compiler Infrastructure 4 1.1 kamil // 5 1.1 kamil // This file is distributed under the University of Illinois Open Source 6 1.1 kamil // License. See LICENSE.TXT for details. 7 1.1 kamil // 8 1.1 kamil //===----------------------------------------------------------------------===// 9 1.1 kamil // 10 1.1 kamil // This file is a part of XRay, a dynamic runtime instrumentation system. 11 1.1 kamil // 12 1.1 kamil // Implementation of MIPS64-specific routines. 13 1.1 kamil // 14 1.1 kamil //===----------------------------------------------------------------------===// 15 1.1 kamil #include "sanitizer_common/sanitizer_common.h" 16 1.1 kamil #include "xray_defs.h" 17 1.1 kamil #include "xray_interface_internal.h" 18 1.1 kamil #include <atomic> 19 1.1 kamil 20 1.1 kamil namespace __xray { 21 1.1 kamil 22 1.1 kamil // The machine codes for some instructions used in runtime patching. 23 1.1 kamil enum PatchOpcodes : uint32_t { 24 1.1 kamil PO_DADDIU = 0x64000000, // daddiu rt, rs, imm 25 1.1 kamil PO_SD = 0xFC000000, // sd rt, base(offset) 26 1.1 kamil PO_LUI = 0x3C000000, // lui rt, imm 27 1.1 kamil PO_ORI = 0x34000000, // ori rt, rs, imm 28 1.1 kamil PO_DSLL = 0x00000038, // dsll rd, rt, sa 29 1.1 kamil PO_JALR = 0x00000009, // jalr rs 30 1.1 kamil PO_LD = 0xDC000000, // ld rt, base(offset) 31 1.1 kamil PO_B60 = 0x1000000f, // b #60 32 1.1 kamil PO_NOP = 0x0, // nop 33 1.1 kamil }; 34 1.1 kamil 35 1.1 kamil enum RegNum : uint32_t { 36 1.1 kamil RN_T0 = 0xC, 37 1.1 kamil RN_T9 = 0x19, 38 1.1 kamil RN_RA = 0x1F, 39 1.1 kamil RN_SP = 0x1D, 40 1.1 kamil }; 41 1.1 kamil 42 1.1 kamil inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs, 43 1.1 kamil uint32_t Rt, 44 1.1 kamil uint32_t Imm) XRAY_NEVER_INSTRUMENT { 45 1.1 kamil return (Opcode | Rs << 21 | Rt << 16 | Imm); 46 1.1 kamil } 47 1.1 kamil 48 1.1 kamil inline static uint32_t 49 1.1 kamil encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd, 50 1.1 kamil uint32_t Imm) XRAY_NEVER_INSTRUMENT { 51 1.1 kamil return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode); 52 1.1 kamil } 53 1.1 kamil 54 1.1 kamil inline static bool patchSled(const bool Enable, const uint32_t FuncId, 55 1.1 kamil const XRaySledEntry &Sled, 56 1.1 kamil void (*TracingHook)()) XRAY_NEVER_INSTRUMENT { 57 1.1 kamil // When |Enable| == true, 58 1.1 kamil // We replace the following compile-time stub (sled): 59 1.1 kamil // 60 1.1 kamil // xray_sled_n: 61 1.1 kamil // B .tmpN 62 1.1 kamil // 15 NOPs (60 bytes) 63 1.1 kamil // .tmpN 64 1.1 kamil // 65 1.1 kamil // With the following runtime patch: 66 1.1 kamil // 67 1.1 kamil // xray_sled_n (64-bit): 68 1.1 kamil // daddiu sp, sp, -16 ;create stack frame 69 1.1 kamil // nop 70 1.1 kamil // sd ra, 8(sp) ;save return address 71 1.1 kamil // sd t9, 0(sp) ;save register t9 72 1.1 kamil // lui t9, %highest(__xray_FunctionEntry/Exit) 73 1.1 kamil // ori t9, t9, %higher(__xray_FunctionEntry/Exit) 74 1.1 kamil // dsll t9, t9, 16 75 1.1 kamil // ori t9, t9, %hi(__xray_FunctionEntry/Exit) 76 1.1 kamil // dsll t9, t9, 16 77 1.1 kamil // ori t9, t9, %lo(__xray_FunctionEntry/Exit) 78 1.1 kamil // lui t0, %hi(function_id) 79 1.1 kamil // jalr t9 ;call Tracing hook 80 1.1 kamil // ori t0, t0, %lo(function_id) ;pass function id (delay slot) 81 1.1 kamil // ld t9, 0(sp) ;restore register t9 82 1.1 kamil // ld ra, 8(sp) ;restore return address 83 1.1 kamil // daddiu sp, sp, 16 ;delete stack frame 84 1.1 kamil // 85 1.1 kamil // Replacement of the first 4-byte instruction should be the last and atomic 86 1.1 kamil // operation, so that the user code which reaches the sled concurrently 87 1.1 kamil // either jumps over the whole sled, or executes the whole sled when the 88 1.1 kamil // latter is ready. 89 1.1 kamil // 90 1.1 kamil // When |Enable|==false, we set back the first instruction in the sled to be 91 1.1 kamil // B #60 92 1.1 kamil 93 1.1 kamil if (Enable) { 94 1.1 kamil uint32_t LoTracingHookAddr = 95 1.1 kamil reinterpret_cast<int64_t>(TracingHook) & 0xffff; 96 1.1 kamil uint32_t HiTracingHookAddr = 97 1.1 kamil (reinterpret_cast<int64_t>(TracingHook) >> 16) & 0xffff; 98 1.1 kamil uint32_t HigherTracingHookAddr = 99 1.1 kamil (reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xffff; 100 1.1 kamil uint32_t HighestTracingHookAddr = 101 1.1 kamil (reinterpret_cast<int64_t>(TracingHook) >> 48) & 0xffff; 102 1.1 kamil uint32_t LoFunctionID = FuncId & 0xffff; 103 1.1 kamil uint32_t HiFunctionID = (FuncId >> 16) & 0xffff; 104 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 8) = encodeInstruction( 105 1.1 kamil PatchOpcodes::PO_SD, RegNum::RN_SP, RegNum::RN_RA, 0x8); 106 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 12) = encodeInstruction( 107 1.1 kamil PatchOpcodes::PO_SD, RegNum::RN_SP, RegNum::RN_T9, 0x0); 108 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 16) = encodeInstruction( 109 1.1 kamil PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T9, HighestTracingHookAddr); 110 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 20) = 111 1.1 kamil encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9, RegNum::RN_T9, 112 1.1 kamil HigherTracingHookAddr); 113 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 24) = encodeSpecialInstruction( 114 1.1 kamil PatchOpcodes::PO_DSLL, 0x0, RegNum::RN_T9, RegNum::RN_T9, 0x10); 115 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 28) = encodeInstruction( 116 1.1 kamil PatchOpcodes::PO_ORI, RegNum::RN_T9, RegNum::RN_T9, HiTracingHookAddr); 117 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 32) = encodeSpecialInstruction( 118 1.1 kamil PatchOpcodes::PO_DSLL, 0x0, RegNum::RN_T9, RegNum::RN_T9, 0x10); 119 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 36) = encodeInstruction( 120 1.1 kamil PatchOpcodes::PO_ORI, RegNum::RN_T9, RegNum::RN_T9, LoTracingHookAddr); 121 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 40) = encodeInstruction( 122 1.1 kamil PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0, HiFunctionID); 123 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 44) = encodeSpecialInstruction( 124 1.1 kamil PatchOpcodes::PO_JALR, RegNum::RN_T9, 0x0, RegNum::RN_RA, 0X0); 125 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 48) = encodeInstruction( 126 1.1 kamil PatchOpcodes::PO_ORI, RegNum::RN_T0, RegNum::RN_T0, LoFunctionID); 127 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 52) = encodeInstruction( 128 1.1 kamil PatchOpcodes::PO_LD, RegNum::RN_SP, RegNum::RN_T9, 0x0); 129 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 56) = encodeInstruction( 130 1.1 kamil PatchOpcodes::PO_LD, RegNum::RN_SP, RegNum::RN_RA, 0x8); 131 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 60) = encodeInstruction( 132 1.1 kamil PatchOpcodes::PO_DADDIU, RegNum::RN_SP, RegNum::RN_SP, 0x10); 133 1.1 kamil uint32_t CreateStackSpace = encodeInstruction( 134 1.1 kamil PatchOpcodes::PO_DADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xfff0); 135 1.1 kamil std::atomic_store_explicit( 136 1.1 kamil reinterpret_cast<std::atomic<uint32_t> *>(Sled.Address), 137 1.1 kamil CreateStackSpace, std::memory_order_release); 138 1.1 kamil } else { 139 1.1 kamil std::atomic_store_explicit( 140 1.1 kamil reinterpret_cast<std::atomic<uint32_t> *>(Sled.Address), 141 1.1 kamil uint32_t(PatchOpcodes::PO_B60), std::memory_order_release); 142 1.1 kamil } 143 1.1 kamil return true; 144 1.1 kamil } 145 1.1 kamil 146 1.1 kamil bool patchFunctionEntry(const bool Enable, const uint32_t FuncId, 147 1.1 kamil const XRaySledEntry &Sled, 148 1.1 kamil void (*Trampoline)()) XRAY_NEVER_INSTRUMENT { 149 1.1 kamil return patchSled(Enable, FuncId, Sled, Trampoline); 150 1.1 kamil } 151 1.1 kamil 152 1.1 kamil bool patchFunctionExit(const bool Enable, const uint32_t FuncId, 153 1.1 kamil const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 154 1.1 kamil return patchSled(Enable, FuncId, Sled, __xray_FunctionExit); 155 1.1 kamil } 156 1.1 kamil 157 1.1 kamil bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId, 158 1.1 kamil const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 159 1.1 kamil // FIXME: In the future we'd need to distinguish between non-tail exits and 160 1.1 kamil // tail exits for better information preservation. 161 1.1 kamil return patchSled(Enable, FuncId, Sled, __xray_FunctionExit); 162 1.1 kamil } 163 1.1 kamil 164 1.1 kamil bool patchCustomEvent(const bool Enable, const uint32_t FuncId, 165 1.1 kamil const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 166 1.1 kamil // FIXME: Implement in mips64? 167 1.1 kamil return false; 168 1.1 kamil } 169 1.1 kamil 170 1.1 kamil bool patchTypedEvent(const bool Enable, const uint32_t FuncId, 171 1.1 kamil const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 172 1.1 kamil // FIXME: Implement in mips64? 173 1.1 kamil return false; 174 1.1 kamil } 175 1.1 kamil } // namespace __xray 176 1.1 kamil 177 1.1 kamil extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT { 178 1.1 kamil // FIXME: this will have to be implemented in the trampoline assembly file 179 1.1 kamil } 180