| 1 | //===-- xray_AArch64.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 AArch64-specific routines (64-bit). |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "sanitizer_common/sanitizer_common.h" |
| 15 | #include "xray_defs.h" |
| 16 | #include "xray_interface_internal.h" |
| 17 | #include <atomic> |
| 18 | #include <cassert> |
| 19 | |
| 20 | extern "C" void __clear_cache(void *start, void *end); |
| 21 | |
| 22 | namespace __xray { |
| 23 | |
| 24 | // The machine codes for some instructions used in runtime patching. |
| 25 | enum class PatchOpcodes : uint32_t { |
| 26 | PO_StpX0X30SP_m16e = 0xA9BF7BE0, // STP X0, X30, [SP, #-16]! |
| 27 | PO_LdrX16_12 = 0x58000070, // LDR X16, #12 |
| 28 | PO_BlrX16 = 0xD63F0200, // BLR X16 |
| 29 | PO_LdpX0X30SP_16 = 0xA8C17BE0, // LDP X0, X30, [SP], #16 |
| 30 | PO_B32 = 0x14000008 // B #32 |
| 31 | }; |
| 32 | |
| 33 | inline static bool patchSled(const bool Enable, const uint32_t FuncId, |
| 34 | const XRaySledEntry &Sled, |
| 35 | void (*TracingHook)()) XRAY_NEVER_INSTRUMENT { |
| 36 | // When |Enable| == true, |
| 37 | // We replace the following compile-time stub (sled): |
| 38 | // |
| 39 | // xray_sled_n: |
| 40 | // B #32 |
| 41 | // 7 NOPs (24 bytes) |
| 42 | // |
| 43 | // With the following runtime patch: |
| 44 | // |
| 45 | // xray_sled_n: |
| 46 | // STP X0, X30, [SP, #-16]! ; PUSH {r0, lr} |
| 47 | // LDR W17, #12 ; W17 := function ID |
| 48 | // LDR X16,#12 ; X16 := address of the trampoline |
| 49 | // BLR X16 |
| 50 | // ;DATA: 32 bits of function ID |
| 51 | // ;DATA: lower 32 bits of the address of the trampoline |
| 52 | // ;DATA: higher 32 bits of the address of the trampoline |
| 53 | // LDP X0, X30, [SP], #16 ; POP {r0, lr} |
| 54 | // |
| 55 | // Replacement of the first 4-byte instruction should be the last and atomic |
| 56 | // operation, so that the user code which reaches the sled concurrently |
| 57 | // either jumps over the whole sled, or executes the whole sled when the |
| 58 | // latter is ready. |
| 59 | // |
| 60 | // When |Enable|==false, we set back the first instruction in the sled to be |
| 61 | // B #32 |
| 62 | |
| 63 | uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.address()); |
| 64 | uint32_t *CurAddress = FirstAddress + 1; |
| 65 | if (Enable) { |
| 66 | *CurAddress++ = 0x18000071; // ldr w17, #12 |
| 67 | *CurAddress = uint32_t(PatchOpcodes::PO_LdrX16_12); |
| 68 | CurAddress++; |
| 69 | *CurAddress = uint32_t(PatchOpcodes::PO_BlrX16); |
| 70 | CurAddress++; |
| 71 | *CurAddress = FuncId; |
| 72 | CurAddress++; |
| 73 | *reinterpret_cast<void (**)()>(CurAddress) = TracingHook; |
| 74 | CurAddress += 2; |
| 75 | *CurAddress = uint32_t(PatchOpcodes::PO_LdpX0X30SP_16); |
| 76 | CurAddress++; |
| 77 | std::atomic_store_explicit( |
| 78 | a: reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), |
| 79 | i: uint32_t(PatchOpcodes::PO_StpX0X30SP_m16e), m: std::memory_order_release); |
| 80 | } else { |
| 81 | std::atomic_store_explicit( |
| 82 | a: reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), |
| 83 | i: uint32_t(PatchOpcodes::PO_B32), m: std::memory_order_release); |
| 84 | } |
| 85 | __clear_cache(start: reinterpret_cast<char *>(FirstAddress), |
| 86 | end: reinterpret_cast<char *>(CurAddress)); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool patchFunctionEntry(const bool Enable, const uint32_t FuncId, |
| 91 | const XRaySledEntry &Sled, |
| 92 | const XRayTrampolines &Trampolines, |
| 93 | bool LogArgs) XRAY_NEVER_INSTRUMENT { |
| 94 | auto Trampoline = |
| 95 | LogArgs ? Trampolines.LogArgsTrampoline : Trampolines.EntryTrampoline; |
| 96 | return patchSled(Enable, FuncId, Sled, TracingHook: Trampoline); |
| 97 | } |
| 98 | |
| 99 | bool patchFunctionExit( |
| 100 | const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled, |
| 101 | const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT { |
| 102 | return patchSled(Enable, FuncId, Sled, TracingHook: Trampolines.ExitTrampoline); |
| 103 | } |
| 104 | |
| 105 | bool patchFunctionTailExit( |
| 106 | const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled, |
| 107 | const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT { |
| 108 | return patchSled(Enable, FuncId, Sled, TracingHook: Trampolines.TailExitTrampoline); |
| 109 | } |
| 110 | |
| 111 | // AArch64AsmPrinter::LowerPATCHABLE_EVENT_CALL generates this code sequence: |
| 112 | // |
| 113 | // .Lxray_event_sled_N: |
| 114 | // b 1f |
| 115 | // save x0 and x1 (and also x2 for TYPED_EVENT_CALL) |
| 116 | // set up x0 and x1 (and also x2 for TYPED_EVENT_CALL) |
| 117 | // bl __xray_CustomEvent or __xray_TypedEvent |
| 118 | // restore x0 and x1 (and also x2 for TYPED_EVENT_CALL) |
| 119 | // 1f |
| 120 | // |
| 121 | // There are 6 instructions for EVENT_CALL and 9 for TYPED_EVENT_CALL. |
| 122 | // |
| 123 | // Enable: b .+24 => nop |
| 124 | // Disable: nop => b .+24 |
| 125 | bool patchCustomEvent(const bool Enable, const uint32_t FuncId, |
| 126 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
| 127 | uint32_t Inst = Enable ? 0xd503201f : 0x14000006; |
| 128 | std::atomic_store_explicit( |
| 129 | a: reinterpret_cast<std::atomic<uint32_t> *>(Sled.address()), i: Inst, |
| 130 | m: std::memory_order_release); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // Enable: b +36 => nop |
| 135 | // Disable: nop => b +36 |
| 136 | bool patchTypedEvent(const bool Enable, const uint32_t FuncId, |
| 137 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
| 138 | uint32_t Inst = Enable ? 0xd503201f : 0x14000009; |
| 139 | std::atomic_store_explicit( |
| 140 | a: reinterpret_cast<std::atomic<uint32_t> *>(Sled.address()), i: Inst, |
| 141 | m: std::memory_order_release); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | // FIXME: Maybe implement this better? |
| 146 | bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; } |
| 147 | |
| 148 | } // namespace __xray |
| 149 | |