149 lines · cpp
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-exception6//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 20extern "C" void __clear_cache(void *start, void *end);21 22namespace __xray {23 24// The machine codes for some instructions used in runtime patching.25enum class PatchOpcodes : uint32_t {26 PO_StpX0X30SP_m16e = 0xA9BF7BE0, // STP X0, X30, [SP, #-16]!27 PO_LdrX16_12 = 0x58000070, // LDR X16, #1228 PO_BlrX16 = 0xD63F0200, // BLR X1629 PO_LdpX0X30SP_16 = 0xA8C17BE0, // LDP X0, X30, [SP], #1630 PO_B32 = 0x14000008 // B #3231};32 33inline 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 #3241 // 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 ID48 // LDR X16,#12 ; X16 := address of the trampoline49 // BLR X1650 // ;DATA: 32 bits of function ID51 // ;DATA: lower 32 bits of the address of the trampoline52 // ;DATA: higher 32 bits of the address of the trampoline53 // LDP X0, X30, [SP], #16 ; POP {r0, lr}54 //55 // Replacement of the first 4-byte instruction should be the last and atomic56 // operation, so that the user code which reaches the sled concurrently57 // either jumps over the whole sled, or executes the whole sled when the58 // latter is ready.59 //60 // When |Enable|==false, we set back the first instruction in the sled to be61 // B #3262 63 uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.address());64 uint32_t *CurAddress = FirstAddress + 1;65 if (Enable) {66 *CurAddress++ = 0x18000071; // ldr w17, #1267 *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 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),79 uint32_t(PatchOpcodes::PO_StpX0X30SP_m16e), std::memory_order_release);80 } else {81 std::atomic_store_explicit(82 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),83 uint32_t(PatchOpcodes::PO_B32), std::memory_order_release);84 }85 __clear_cache(reinterpret_cast<char *>(FirstAddress),86 reinterpret_cast<char *>(CurAddress));87 return true;88}89 90bool 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, Trampoline);97}98 99bool 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, Trampolines.ExitTrampoline);103}104 105bool 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, Trampolines.TailExitTrampoline);109}110 111// AArch64AsmPrinter::LowerPATCHABLE_EVENT_CALL generates this code sequence:112//113// .Lxray_event_sled_N:114// b 1f115// 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_TypedEvent118// restore x0 and x1 (and also x2 for TYPED_EVENT_CALL)119// 1f120//121// There are 6 instructions for EVENT_CALL and 9 for TYPED_EVENT_CALL.122//123// Enable: b .+24 => nop124// Disable: nop => b .+24125bool 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 reinterpret_cast<std::atomic<uint32_t> *>(Sled.address()), Inst,130 std::memory_order_release);131 return false;132}133 134// Enable: b +36 => nop135// Disable: nop => b +36136bool 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 reinterpret_cast<std::atomic<uint32_t> *>(Sled.address()), Inst,141 std::memory_order_release);142 return false;143}144 145// FIXME: Maybe implement this better?146bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }147 148} // namespace __xray149