1 1.1 kamil //===-- xray_mips.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 MIPS-specific routines (32-bit). 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_ADDIU = 0x24000000, // addiu rt, rs, imm 25 1.1 kamil PO_SW = 0xAC000000, // sw rt, offset(sp) 26 1.1 kamil PO_LUI = 0x3C000000, // lui rs, %hi(address) 27 1.1 kamil PO_ORI = 0x34000000, // ori rt, rs, %lo(address) 28 1.1 kamil PO_JALR = 0x0000F809, // jalr rs 29 1.1 kamil PO_LW = 0x8C000000, // lw rt, offset(address) 30 1.1 kamil PO_B44 = 0x1000000b, // b #44 31 1.1 kamil PO_NOP = 0x0, // nop 32 1.1 kamil }; 33 1.1 kamil 34 1.1 kamil enum RegNum : uint32_t { 35 1.1 kamil RN_T0 = 0x8, 36 1.1 kamil RN_T9 = 0x19, 37 1.1 kamil RN_RA = 0x1F, 38 1.1 kamil RN_SP = 0x1D, 39 1.1 kamil }; 40 1.1 kamil 41 1.1 kamil inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs, 42 1.1 kamil uint32_t Rt, 43 1.1 kamil uint32_t Imm) XRAY_NEVER_INSTRUMENT { 44 1.1 kamil return (Opcode | Rs << 21 | Rt << 16 | Imm); 45 1.1 kamil } 46 1.1 kamil 47 1.1 kamil inline static uint32_t 48 1.1 kamil encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd, 49 1.1 kamil uint32_t Imm) XRAY_NEVER_INSTRUMENT { 50 1.1 kamil return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode); 51 1.1 kamil } 52 1.1 kamil 53 1.1 kamil inline static bool patchSled(const bool Enable, const uint32_t FuncId, 54 1.1 kamil const XRaySledEntry &Sled, 55 1.1 kamil void (*TracingHook)()) XRAY_NEVER_INSTRUMENT { 56 1.1 kamil // When |Enable| == true, 57 1.1 kamil // We replace the following compile-time stub (sled): 58 1.1 kamil // 59 1.1 kamil // xray_sled_n: 60 1.1 kamil // B .tmpN 61 1.1 kamil // 11 NOPs (44 bytes) 62 1.1 kamil // .tmpN 63 1.1 kamil // ADDIU T9, T9, 44 64 1.1 kamil // 65 1.1 kamil // With the following runtime patch: 66 1.1 kamil // 67 1.1 kamil // xray_sled_n (32-bit): 68 1.1 kamil // addiu sp, sp, -8 ;create stack frame 69 1.1 kamil // nop 70 1.1 kamil // sw ra, 4(sp) ;save return address 71 1.1 kamil // sw t9, 0(sp) ;save register t9 72 1.1 kamil // lui t9, %hi(__xray_FunctionEntry/Exit) 73 1.1 kamil // ori t9, t9, %lo(__xray_FunctionEntry/Exit) 74 1.1 kamil // lui t0, %hi(function_id) 75 1.1 kamil // jalr t9 ;call Tracing hook 76 1.1 kamil // ori t0, t0, %lo(function_id) ;pass function id (delay slot) 77 1.1 kamil // lw t9, 0(sp) ;restore register t9 78 1.1 kamil // lw ra, 4(sp) ;restore return address 79 1.1 kamil // addiu sp, sp, 8 ;delete stack frame 80 1.1 kamil // 81 1.1 kamil // We add 44 bytes to t9 because we want to adjust the function pointer to 82 1.1 kamil // the actual start of function i.e. the address just after the noop sled. 83 1.1 kamil // We do this because gp displacement relocation is emitted at the start of 84 1.1 kamil // of the function i.e after the nop sled and to correctly calculate the 85 1.1 kamil // global offset table address, t9 must hold the address of the instruction 86 1.1 kamil // containing the gp displacement relocation. 87 1.1 kamil // FIXME: Is this correct for the static relocation model? 88 1.1 kamil // 89 1.1 kamil // Replacement of the first 4-byte instruction should be the last and atomic 90 1.1 kamil // operation, so that the user code which reaches the sled concurrently 91 1.1 kamil // either jumps over the whole sled, or executes the whole sled when the 92 1.1 kamil // latter is ready. 93 1.1 kamil // 94 1.1 kamil // When |Enable|==false, we set back the first instruction in the sled to be 95 1.1 kamil // B #44 96 1.1 kamil 97 1.1 kamil if (Enable) { 98 1.1 kamil uint32_t LoTracingHookAddr = 99 1.1 kamil reinterpret_cast<int32_t>(TracingHook) & 0xffff; 100 1.1 kamil uint32_t HiTracingHookAddr = 101 1.1 kamil (reinterpret_cast<int32_t>(TracingHook) >> 16) & 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_SW, RegNum::RN_SP, RegNum::RN_RA, 0x4); 106 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 12) = encodeInstruction( 107 1.1 kamil PatchOpcodes::PO_SW, 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, HiTracingHookAddr); 110 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 20) = encodeInstruction( 111 1.1 kamil PatchOpcodes::PO_ORI, RegNum::RN_T9, RegNum::RN_T9, LoTracingHookAddr); 112 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 24) = encodeInstruction( 113 1.1 kamil PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0, HiFunctionID); 114 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 28) = encodeSpecialInstruction( 115 1.1 kamil PatchOpcodes::PO_JALR, RegNum::RN_T9, 0x0, RegNum::RN_RA, 0X0); 116 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 32) = encodeInstruction( 117 1.1 kamil PatchOpcodes::PO_ORI, RegNum::RN_T0, RegNum::RN_T0, LoFunctionID); 118 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 36) = encodeInstruction( 119 1.1 kamil PatchOpcodes::PO_LW, RegNum::RN_SP, RegNum::RN_T9, 0x0); 120 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 40) = encodeInstruction( 121 1.1 kamil PatchOpcodes::PO_LW, RegNum::RN_SP, RegNum::RN_RA, 0x4); 122 1.1 kamil *reinterpret_cast<uint32_t *>(Sled.Address + 44) = encodeInstruction( 123 1.1 kamil PatchOpcodes::PO_ADDIU, RegNum::RN_SP, RegNum::RN_SP, 0x8); 124 1.1 kamil uint32_t CreateStackSpaceInstr = encodeInstruction( 125 1.1 kamil PatchOpcodes::PO_ADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xFFF8); 126 1.1 kamil std::atomic_store_explicit( 127 1.1 kamil reinterpret_cast<std::atomic<uint32_t> *>(Sled.Address), 128 1.1 kamil uint32_t(CreateStackSpaceInstr), std::memory_order_release); 129 1.1 kamil } else { 130 1.1 kamil std::atomic_store_explicit( 131 1.1 kamil reinterpret_cast<std::atomic<uint32_t> *>(Sled.Address), 132 1.1 kamil uint32_t(PatchOpcodes::PO_B44), std::memory_order_release); 133 1.1 kamil } 134 1.1 kamil return true; 135 1.1 kamil } 136 1.1 kamil 137 1.1 kamil bool patchFunctionEntry(const bool Enable, const uint32_t FuncId, 138 1.1 kamil const XRaySledEntry &Sled, 139 1.1 kamil void (*Trampoline)()) XRAY_NEVER_INSTRUMENT { 140 1.1 kamil return patchSled(Enable, FuncId, Sled, Trampoline); 141 1.1 kamil } 142 1.1 kamil 143 1.1 kamil bool patchFunctionExit(const bool Enable, const uint32_t FuncId, 144 1.1 kamil const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 145 1.1 kamil return patchSled(Enable, FuncId, Sled, __xray_FunctionExit); 146 1.1 kamil } 147 1.1 kamil 148 1.1 kamil bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId, 149 1.1 kamil const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 150 1.1 kamil // FIXME: In the future we'd need to distinguish between non-tail exits and 151 1.1 kamil // tail exits for better information preservation. 152 1.1 kamil return patchSled(Enable, FuncId, Sled, __xray_FunctionExit); 153 1.1 kamil } 154 1.1 kamil 155 1.1 kamil bool patchCustomEvent(const bool Enable, const uint32_t FuncId, 156 1.1 kamil const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 157 1.1 kamil // FIXME: Implement in mips? 158 1.1 kamil return false; 159 1.1 kamil } 160 1.1 kamil 161 1.1 kamil bool patchTypedEvent(const bool Enable, const uint32_t FuncId, 162 1.1 kamil const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 163 1.1 kamil // FIXME: Implement in mips? 164 1.1 kamil return false; 165 1.1 kamil } 166 1.1 kamil 167 1.1 kamil } // namespace __xray 168 1.1 kamil 169 1.1 kamil extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT { 170 1.1 kamil // FIXME: this will have to be implemented in the trampoline assembly file 171 1.1 kamil } 172