Path: blob/main/contrib/llvm-project/compiler-rt/lib/xray/xray_AArch64.cpp
35265 views
//===-- xray_AArch64.cpp ----------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of XRay, a dynamic runtime instrumentation system.9//10// Implementation of AArch64-specific routines (64-bit).11//12//===----------------------------------------------------------------------===//13#include "sanitizer_common/sanitizer_common.h"14#include "xray_defs.h"15#include "xray_interface_internal.h"16#include <atomic>17#include <cassert>1819extern "C" void __clear_cache(void *start, void *end);2021namespace __xray {2223// The machine codes for some instructions used in runtime patching.24enum class PatchOpcodes : uint32_t {25PO_StpX0X30SP_m16e = 0xA9BF7BE0, // STP X0, X30, [SP, #-16]!26PO_LdrX16_12 = 0x58000070, // LDR X16, #1227PO_BlrX16 = 0xD63F0200, // BLR X1628PO_LdpX0X30SP_16 = 0xA8C17BE0, // LDP X0, X30, [SP], #1629PO_B32 = 0x14000008 // B #3230};3132inline static bool patchSled(const bool Enable, const uint32_t FuncId,33const XRaySledEntry &Sled,34void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {35// When |Enable| == true,36// We replace the following compile-time stub (sled):37//38// xray_sled_n:39// B #3240// 7 NOPs (24 bytes)41//42// With the following runtime patch:43//44// xray_sled_n:45// STP X0, X30, [SP, #-16]! ; PUSH {r0, lr}46// LDR W17, #12 ; W17 := function ID47// LDR X16,#12 ; X16 := address of the trampoline48// BLR X1649// ;DATA: 32 bits of function ID50// ;DATA: lower 32 bits of the address of the trampoline51// ;DATA: higher 32 bits of the address of the trampoline52// LDP X0, X30, [SP], #16 ; POP {r0, lr}53//54// Replacement of the first 4-byte instruction should be the last and atomic55// operation, so that the user code which reaches the sled concurrently56// either jumps over the whole sled, or executes the whole sled when the57// latter is ready.58//59// When |Enable|==false, we set back the first instruction in the sled to be60// B #326162uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.address());63uint32_t *CurAddress = FirstAddress + 1;64if (Enable) {65*CurAddress++ = 0x18000071; // ldr w17, #1266*CurAddress = uint32_t(PatchOpcodes::PO_LdrX16_12);67CurAddress++;68*CurAddress = uint32_t(PatchOpcodes::PO_BlrX16);69CurAddress++;70*CurAddress = FuncId;71CurAddress++;72*reinterpret_cast<void (**)()>(CurAddress) = TracingHook;73CurAddress += 2;74*CurAddress = uint32_t(PatchOpcodes::PO_LdpX0X30SP_16);75CurAddress++;76std::atomic_store_explicit(77reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),78uint32_t(PatchOpcodes::PO_StpX0X30SP_m16e), std::memory_order_release);79} else {80std::atomic_store_explicit(81reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),82uint32_t(PatchOpcodes::PO_B32), std::memory_order_release);83}84__clear_cache(reinterpret_cast<char *>(FirstAddress),85reinterpret_cast<char *>(CurAddress));86return true;87}8889bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,90const XRaySledEntry &Sled,91void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {92return patchSled(Enable, FuncId, Sled, Trampoline);93}9495bool patchFunctionExit(const bool Enable, const uint32_t FuncId,96const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {97return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);98}99100bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,101const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {102return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);103}104105// AArch64AsmPrinter::LowerPATCHABLE_EVENT_CALL generates this code sequence:106//107// .Lxray_event_sled_N:108// b 1f109// save x0 and x1 (and also x2 for TYPED_EVENT_CALL)110// set up x0 and x1 (and also x2 for TYPED_EVENT_CALL)111// bl __xray_CustomEvent or __xray_TypedEvent112// restore x0 and x1 (and also x2 for TYPED_EVENT_CALL)113// 1f114//115// There are 6 instructions for EVENT_CALL and 9 for TYPED_EVENT_CALL.116//117// Enable: b .+24 => nop118// Disable: nop => b .+24119bool patchCustomEvent(const bool Enable, const uint32_t FuncId,120const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {121uint32_t Inst = Enable ? 0xd503201f : 0x14000006;122std::atomic_store_explicit(123reinterpret_cast<std::atomic<uint32_t> *>(Sled.address()), Inst,124std::memory_order_release);125return false;126}127128// Enable: b +36 => nop129// Disable: nop => b +36130bool patchTypedEvent(const bool Enable, const uint32_t FuncId,131const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {132uint32_t Inst = Enable ? 0xd503201f : 0x14000009;133std::atomic_store_explicit(134reinterpret_cast<std::atomic<uint32_t> *>(Sled.address()), Inst,135std::memory_order_release);136return false;137}138139// FIXME: Maybe implement this better?140bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }141142} // namespace __xray143144145