| 1 | //===-------- xray_loongarch64.cpp ------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file is a part of XRay, a dynamic runtime instrumentation system. |
| 10 | // |
| 11 | // Implementation of loongarch-specific routines. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "sanitizer_common/sanitizer_common.h" |
| 15 | #include "xray_defs.h" |
| 16 | #include "xray_interface_internal.h" |
| 17 | #include <atomic> |
| 18 | |
| 19 | namespace __xray { |
| 20 | |
| 21 | enum RegNum : uint32_t { |
| 22 | RN_RA = 1, |
| 23 | RN_SP = 3, |
| 24 | RN_T0 = 12, |
| 25 | RN_T1 = 13, |
| 26 | }; |
| 27 | |
| 28 | // Encode instructions in the 2RIx format, where the primary formats here |
| 29 | // are 2RI12-type and 2RI16-type. |
| 30 | static inline uint32_t |
| 31 | encodeInstruction2RIx(uint32_t Opcode, uint32_t Rd, uint32_t Rj, |
| 32 | uint32_t Imm) XRAY_NEVER_INSTRUMENT { |
| 33 | return Opcode | (Imm << 10) | (Rj << 5) | Rd; |
| 34 | } |
| 35 | |
| 36 | // Encode instructions in 1RI20 format, e.g. lu12i.w/lu32i.d. |
| 37 | static inline uint32_t |
| 38 | encodeInstruction1RI20(uint32_t Opcode, uint32_t Rd, |
| 39 | uint32_t Imm) XRAY_NEVER_INSTRUMENT { |
| 40 | return Opcode | (Imm << 5) | Rd; |
| 41 | } |
| 42 | |
| 43 | static inline bool patchSled(const bool Enable, const uint32_t FuncId, |
| 44 | const XRaySledEntry &Sled, |
| 45 | void (*TracingHook)()) XRAY_NEVER_INSTRUMENT { |
| 46 | // When |Enable| == true, |
| 47 | // We replace the following compile-time stub (sled): |
| 48 | // |
| 49 | // .Lxray_sled_beginN: |
| 50 | // B .Lxray_sled_endN |
| 51 | // 11 NOPs (44 bytes) |
| 52 | // .Lxray_sled_endN: |
| 53 | // |
| 54 | // With the following runtime patch: |
| 55 | // |
| 56 | // xray_sled_n: |
| 57 | // addi.d sp, sp, -16 ; create the stack frame |
| 58 | // st.d ra, sp, 8 ; save the return address |
| 59 | // lu12i.w t0, %abs_hi20(__xray_FunctionEntry/Exit) |
| 60 | // ori t0, t0, %abs_lo12(__xray_FunctionEntry/Exit) |
| 61 | // lu32i.d t0, %abs64_lo20(__xray_FunctionEntry/Exit) |
| 62 | // lu52i.d t0, t0, %abs64_hi12(__xray_FunctionEntry/Exit) |
| 63 | // lu12i.w t1, %abs_hi20(function_id) |
| 64 | // ori t1, t1, %abs_lo12(function_id) ; pass the function id |
| 65 | // jirl ra, t0, 0 ; call the tracing hook |
| 66 | // ld.d ra, sp, 8 ; restore the return address |
| 67 | // addi.d sp, sp, 16 ; de-allocate the stack frame |
| 68 | // |
| 69 | // Replacement of the first 4-byte instruction should be the last and atomic |
| 70 | // operation, so that the user code which reaches the sled concurrently |
| 71 | // either jumps over the whole sled, or executes the whole sled when the |
| 72 | // latter is ready. |
| 73 | // |
| 74 | // When |Enable|==false, we set the first instruction in the sled back to |
| 75 | // B #48 |
| 76 | |
| 77 | uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address()); |
| 78 | if (Enable) { |
| 79 | uint32_t LoTracingHookAddr = reinterpret_cast<int64_t>(TracingHook) & 0xfff; |
| 80 | uint32_t HiTracingHookAddr = |
| 81 | (reinterpret_cast<int64_t>(TracingHook) >> 12) & 0xfffff; |
| 82 | uint32_t HigherTracingHookAddr = |
| 83 | (reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xfffff; |
| 84 | uint32_t HighestTracingHookAddr = |
| 85 | (reinterpret_cast<int64_t>(TracingHook) >> 52) & 0xfff; |
| 86 | uint32_t LoFunctionID = FuncId & 0xfff; |
| 87 | uint32_t HiFunctionID = (FuncId >> 12) & 0xfffff; |
| 88 | Address[1] = encodeInstruction2RIx(Opcode: 0x29c00000, Rd: RegNum::RN_RA, Rj: RegNum::RN_SP, |
| 89 | Imm: 0x8); // st.d ra, sp, 8 |
| 90 | Address[2] = encodeInstruction1RI20( |
| 91 | Opcode: 0x14000000, Rd: RegNum::RN_T0, |
| 92 | Imm: HiTracingHookAddr); // lu12i.w t0, HiTracingHookAddr |
| 93 | Address[3] = encodeInstruction2RIx( |
| 94 | Opcode: 0x03800000, Rd: RegNum::RN_T0, Rj: RegNum::RN_T0, |
| 95 | Imm: LoTracingHookAddr); // ori t0, t0, LoTracingHookAddr |
| 96 | Address[4] = encodeInstruction1RI20( |
| 97 | Opcode: 0x16000000, Rd: RegNum::RN_T0, |
| 98 | Imm: HigherTracingHookAddr); // lu32i.d t0, HigherTracingHookAddr |
| 99 | Address[5] = encodeInstruction2RIx( |
| 100 | Opcode: 0x03000000, Rd: RegNum::RN_T0, Rj: RegNum::RN_T0, |
| 101 | Imm: HighestTracingHookAddr); // lu52i.d t0, t0, HighestTracingHookAddr |
| 102 | Address[6] = |
| 103 | encodeInstruction1RI20(Opcode: 0x14000000, Rd: RegNum::RN_T1, |
| 104 | Imm: HiFunctionID); // lu12i.w t1, HiFunctionID |
| 105 | Address[7] = |
| 106 | encodeInstruction2RIx(Opcode: 0x03800000, Rd: RegNum::RN_T1, Rj: RegNum::RN_T1, |
| 107 | Imm: LoFunctionID); // ori t1, t1, LoFunctionID |
| 108 | Address[8] = encodeInstruction2RIx(Opcode: 0x4c000000, Rd: RegNum::RN_RA, Rj: RegNum::RN_T0, |
| 109 | Imm: 0); // jirl ra, t0, 0 |
| 110 | Address[9] = encodeInstruction2RIx(Opcode: 0x28c00000, Rd: RegNum::RN_RA, Rj: RegNum::RN_SP, |
| 111 | Imm: 0x8); // ld.d ra, sp, 8 |
| 112 | Address[10] = encodeInstruction2RIx( |
| 113 | Opcode: 0x02c00000, Rd: RegNum::RN_SP, Rj: RegNum::RN_SP, Imm: 0x10); // addi.d sp, sp, 16 |
| 114 | uint32_t CreateStackSpace = encodeInstruction2RIx( |
| 115 | Opcode: 0x02c00000, Rd: RegNum::RN_SP, Rj: RegNum::RN_SP, Imm: 0xff0); // addi.d sp, sp, -16 |
| 116 | std::atomic_store_explicit( |
| 117 | a: reinterpret_cast<std::atomic<uint32_t> *>(Address), i: CreateStackSpace, |
| 118 | m: std::memory_order_release); |
| 119 | } else { |
| 120 | std::atomic_store_explicit( |
| 121 | a: reinterpret_cast<std::atomic<uint32_t> *>(Address), |
| 122 | i: uint32_t(0x50003000), m: std::memory_order_release); // b #48 |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | bool patchFunctionEntry(const bool Enable, const uint32_t FuncId, |
| 128 | const XRaySledEntry &Sled, |
| 129 | const XRayTrampolines &Trampolines, |
| 130 | bool LogArgs) XRAY_NEVER_INSTRUMENT { |
| 131 | auto Trampoline = |
| 132 | LogArgs ? Trampolines.LogArgsTrampoline : Trampolines.EntryTrampoline; |
| 133 | return patchSled(Enable, FuncId, Sled, TracingHook: Trampoline); |
| 134 | } |
| 135 | |
| 136 | bool patchFunctionExit( |
| 137 | const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled, |
| 138 | const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT { |
| 139 | return patchSled(Enable, FuncId, Sled, TracingHook: Trampolines.ExitTrampoline); |
| 140 | } |
| 141 | |
| 142 | bool patchFunctionTailExit( |
| 143 | const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled, |
| 144 | const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT { |
| 145 | // TODO: In the future we'd need to distinguish between non-tail exits and |
| 146 | // tail exits for better information preservation. |
| 147 | return patchSled(Enable, FuncId, Sled, TracingHook: Trampolines.ExitTrampoline); |
| 148 | } |
| 149 | |
| 150 | bool patchCustomEvent(const bool Enable, const uint32_t FuncId, |
| 151 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
| 152 | // FIXME: Implement in loongarch? |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | bool patchTypedEvent(const bool Enable, const uint32_t FuncId, |
| 157 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
| 158 | // FIXME: Implement in loongarch? |
| 159 | return false; |
| 160 | } |
| 161 | } // namespace __xray |
| 162 | |
| 163 | extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT { |
| 164 | // TODO: This will have to be implemented in the trampoline assembly file. |
| 165 | } |
| 166 | |
| 167 | extern "C" void __xray_FunctionTailExit() XRAY_NEVER_INSTRUMENT { |
| 168 | // FIXME: this will have to be implemented in the trampoline assembly file |
| 169 | } |
| 170 | |