xref: /openbsd-src/gnu/llvm/compiler-rt/lib/xray/xray_AArch64.cpp (revision 1f9cb04fc6f537ca6cf5a53c28927340cba218a2)
13cab2bb3Spatrick //===-- xray_AArch64.cpp ----------------------------------------*- C++ -*-===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is a part of XRay, a dynamic runtime instrumentation system.
103cab2bb3Spatrick //
113cab2bb3Spatrick // Implementation of AArch64-specific routines (64-bit).
123cab2bb3Spatrick //
133cab2bb3Spatrick //===----------------------------------------------------------------------===//
143cab2bb3Spatrick #include "sanitizer_common/sanitizer_common.h"
153cab2bb3Spatrick #include "xray_defs.h"
163cab2bb3Spatrick #include "xray_interface_internal.h"
173cab2bb3Spatrick #include <atomic>
183cab2bb3Spatrick #include <cassert>
193cab2bb3Spatrick 
203cab2bb3Spatrick extern "C" void __clear_cache(void *start, void *end);
213cab2bb3Spatrick 
223cab2bb3Spatrick namespace __xray {
233cab2bb3Spatrick 
243cab2bb3Spatrick // The machine codes for some instructions used in runtime patching.
253cab2bb3Spatrick enum class PatchOpcodes : uint32_t {
263cab2bb3Spatrick   PO_StpX0X30SP_m16e = 0xA9BF7BE0, // STP X0, X30, [SP, #-16]!
273cab2bb3Spatrick   PO_LdrW0_12 = 0x18000060,        // LDR W0, #12
283cab2bb3Spatrick   PO_LdrX16_12 = 0x58000070,       // LDR X16, #12
293cab2bb3Spatrick   PO_BlrX16 = 0xD63F0200,          // BLR X16
303cab2bb3Spatrick   PO_LdpX0X30SP_16 = 0xA8C17BE0,   // LDP X0, X30, [SP], #16
313cab2bb3Spatrick   PO_B32 = 0x14000008              // B #32
323cab2bb3Spatrick };
333cab2bb3Spatrick 
patchSled(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* TracingHook)())343cab2bb3Spatrick inline static bool patchSled(const bool Enable, const uint32_t FuncId,
353cab2bb3Spatrick                              const XRaySledEntry &Sled,
363cab2bb3Spatrick                              void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
373cab2bb3Spatrick   // When |Enable| == true,
383cab2bb3Spatrick   // We replace the following compile-time stub (sled):
393cab2bb3Spatrick   //
403cab2bb3Spatrick   // xray_sled_n:
413cab2bb3Spatrick   //   B #32
423cab2bb3Spatrick   //   7 NOPs (24 bytes)
433cab2bb3Spatrick   //
443cab2bb3Spatrick   // With the following runtime patch:
453cab2bb3Spatrick   //
463cab2bb3Spatrick   // xray_sled_n:
473cab2bb3Spatrick   //   STP X0, X30, [SP, #-16]! ; PUSH {r0, lr}
483cab2bb3Spatrick   //   LDR W0, #12 ; W0 := function ID
493cab2bb3Spatrick   //   LDR X16,#12 ; X16 := address of the trampoline
503cab2bb3Spatrick   //   BLR X16
513cab2bb3Spatrick   //   ;DATA: 32 bits of function ID
523cab2bb3Spatrick   //   ;DATA: lower 32 bits of the address of the trampoline
533cab2bb3Spatrick   //   ;DATA: higher 32 bits of the address of the trampoline
543cab2bb3Spatrick   //   LDP X0, X30, [SP], #16 ; POP {r0, lr}
553cab2bb3Spatrick   //
563cab2bb3Spatrick   // Replacement of the first 4-byte instruction should be the last and atomic
573cab2bb3Spatrick   // operation, so that the user code which reaches the sled concurrently
583cab2bb3Spatrick   // either jumps over the whole sled, or executes the whole sled when the
593cab2bb3Spatrick   // latter is ready.
603cab2bb3Spatrick   //
613cab2bb3Spatrick   // When |Enable|==false, we set back the first instruction in the sled to be
623cab2bb3Spatrick   //   B #32
633cab2bb3Spatrick 
64*1f9cb04fSpatrick   uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.address());
653cab2bb3Spatrick   uint32_t *CurAddress = FirstAddress + 1;
663cab2bb3Spatrick   if (Enable) {
673cab2bb3Spatrick     *CurAddress = uint32_t(PatchOpcodes::PO_LdrW0_12);
683cab2bb3Spatrick     CurAddress++;
693cab2bb3Spatrick     *CurAddress = uint32_t(PatchOpcodes::PO_LdrX16_12);
703cab2bb3Spatrick     CurAddress++;
713cab2bb3Spatrick     *CurAddress = uint32_t(PatchOpcodes::PO_BlrX16);
723cab2bb3Spatrick     CurAddress++;
733cab2bb3Spatrick     *CurAddress = FuncId;
743cab2bb3Spatrick     CurAddress++;
753cab2bb3Spatrick     *reinterpret_cast<void (**)()>(CurAddress) = TracingHook;
763cab2bb3Spatrick     CurAddress += 2;
773cab2bb3Spatrick     *CurAddress = uint32_t(PatchOpcodes::PO_LdpX0X30SP_16);
783cab2bb3Spatrick     CurAddress++;
793cab2bb3Spatrick     std::atomic_store_explicit(
803cab2bb3Spatrick         reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
813cab2bb3Spatrick         uint32_t(PatchOpcodes::PO_StpX0X30SP_m16e), std::memory_order_release);
823cab2bb3Spatrick   } else {
833cab2bb3Spatrick     std::atomic_store_explicit(
843cab2bb3Spatrick         reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
853cab2bb3Spatrick         uint32_t(PatchOpcodes::PO_B32), std::memory_order_release);
863cab2bb3Spatrick   }
873cab2bb3Spatrick   __clear_cache(reinterpret_cast<char *>(FirstAddress),
883cab2bb3Spatrick                 reinterpret_cast<char *>(CurAddress));
893cab2bb3Spatrick   return true;
903cab2bb3Spatrick }
913cab2bb3Spatrick 
patchFunctionEntry(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* Trampoline)())923cab2bb3Spatrick bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
933cab2bb3Spatrick                         const XRaySledEntry &Sled,
943cab2bb3Spatrick                         void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
953cab2bb3Spatrick   return patchSled(Enable, FuncId, Sled, Trampoline);
963cab2bb3Spatrick }
973cab2bb3Spatrick 
patchFunctionExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)983cab2bb3Spatrick bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
993cab2bb3Spatrick                        const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1003cab2bb3Spatrick   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
1013cab2bb3Spatrick }
1023cab2bb3Spatrick 
patchFunctionTailExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1033cab2bb3Spatrick bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
1043cab2bb3Spatrick                            const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1053cab2bb3Spatrick   return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
1063cab2bb3Spatrick }
1073cab2bb3Spatrick 
patchCustomEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1083cab2bb3Spatrick bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
1093cab2bb3Spatrick                       const XRaySledEntry &Sled)
1103cab2bb3Spatrick     XRAY_NEVER_INSTRUMENT { // FIXME: Implement in aarch64?
1113cab2bb3Spatrick   return false;
1123cab2bb3Spatrick }
1133cab2bb3Spatrick 
patchTypedEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1143cab2bb3Spatrick bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
1153cab2bb3Spatrick                      const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1163cab2bb3Spatrick   // FIXME: Implement in aarch64?
1173cab2bb3Spatrick   return false;
1183cab2bb3Spatrick }
1193cab2bb3Spatrick 
1203cab2bb3Spatrick // FIXME: Maybe implement this better?
probeRequiredCPUFeatures()1213cab2bb3Spatrick bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
1223cab2bb3Spatrick 
1233cab2bb3Spatrick } // namespace __xray
1243cab2bb3Spatrick 
__xray_ArgLoggerEntry()1253cab2bb3Spatrick extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
1263cab2bb3Spatrick   // FIXME: this will have to be implemented in the trampoline assembly file
1273cab2bb3Spatrick }
128