xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/xray/xray_AArch64.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
168d75effSDimitry Andric //===-- xray_AArch64.cpp ----------------------------------------*- C++ -*-===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric // Implementation of AArch64-specific routines (64-bit).
1268d75effSDimitry Andric //
1368d75effSDimitry Andric //===----------------------------------------------------------------------===//
1468d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
1568d75effSDimitry Andric #include "xray_defs.h"
1668d75effSDimitry Andric #include "xray_interface_internal.h"
1768d75effSDimitry Andric #include <atomic>
1868d75effSDimitry Andric #include <cassert>
1968d75effSDimitry Andric 
2068d75effSDimitry Andric extern "C" void __clear_cache(void *start, void *end);
2168d75effSDimitry Andric 
2268d75effSDimitry Andric namespace __xray {
2368d75effSDimitry Andric 
2468d75effSDimitry Andric // The machine codes for some instructions used in runtime patching.
2568d75effSDimitry Andric enum class PatchOpcodes : uint32_t {
2668d75effSDimitry Andric   PO_StpX0X30SP_m16e = 0xA9BF7BE0, // STP X0, X30, [SP, #-16]!
2768d75effSDimitry Andric   PO_LdrX16_12 = 0x58000070,       // LDR X16, #12
2868d75effSDimitry Andric   PO_BlrX16 = 0xD63F0200,          // BLR X16
2968d75effSDimitry Andric   PO_LdpX0X30SP_16 = 0xA8C17BE0,   // LDP X0, X30, [SP], #16
3068d75effSDimitry Andric   PO_B32 = 0x14000008              // B #32
3168d75effSDimitry Andric };
3268d75effSDimitry Andric 
patchSled(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* TracingHook)())3368d75effSDimitry Andric inline static bool patchSled(const bool Enable, const uint32_t FuncId,
3468d75effSDimitry Andric                              const XRaySledEntry &Sled,
3568d75effSDimitry Andric                              void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
3668d75effSDimitry Andric   // When |Enable| == true,
3768d75effSDimitry Andric   // We replace the following compile-time stub (sled):
3868d75effSDimitry Andric   //
3968d75effSDimitry Andric   // xray_sled_n:
4068d75effSDimitry Andric   //   B #32
4168d75effSDimitry Andric   //   7 NOPs (24 bytes)
4268d75effSDimitry Andric   //
4368d75effSDimitry Andric   // With the following runtime patch:
4468d75effSDimitry Andric   //
4568d75effSDimitry Andric   // xray_sled_n:
4668d75effSDimitry Andric   //   STP X0, X30, [SP, #-16]! ; PUSH {r0, lr}
47*06c3fb27SDimitry Andric   //   LDR W17, #12 ; W17 := function ID
4868d75effSDimitry Andric   //   LDR X16,#12 ; X16 := address of the trampoline
4968d75effSDimitry Andric   //   BLR X16
5068d75effSDimitry Andric   //   ;DATA: 32 bits of function ID
5168d75effSDimitry Andric   //   ;DATA: lower 32 bits of the address of the trampoline
5268d75effSDimitry Andric   //   ;DATA: higher 32 bits of the address of the trampoline
5368d75effSDimitry Andric   //   LDP X0, X30, [SP], #16 ; POP {r0, lr}
5468d75effSDimitry Andric   //
5568d75effSDimitry Andric   // Replacement of the first 4-byte instruction should be the last and atomic
5668d75effSDimitry Andric   // operation, so that the user code which reaches the sled concurrently
5768d75effSDimitry Andric   // either jumps over the whole sled, or executes the whole sled when the
5868d75effSDimitry Andric   // latter is ready.
5968d75effSDimitry Andric   //
6068d75effSDimitry Andric   // When |Enable|==false, we set back the first instruction in the sled to be
6168d75effSDimitry Andric   //   B #32
6268d75effSDimitry Andric 
635ffd83dbSDimitry Andric   uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.address());
6468d75effSDimitry Andric   uint32_t *CurAddress = FirstAddress + 1;
6568d75effSDimitry Andric   if (Enable) {
66*06c3fb27SDimitry Andric     *CurAddress++ = 0x18000071; // ldr w17, #12
6768d75effSDimitry Andric     *CurAddress = uint32_t(PatchOpcodes::PO_LdrX16_12);
6868d75effSDimitry Andric     CurAddress++;
6968d75effSDimitry Andric     *CurAddress = uint32_t(PatchOpcodes::PO_BlrX16);
7068d75effSDimitry Andric     CurAddress++;
7168d75effSDimitry Andric     *CurAddress = FuncId;
7268d75effSDimitry Andric     CurAddress++;
7368d75effSDimitry Andric     *reinterpret_cast<void (**)()>(CurAddress) = TracingHook;
7468d75effSDimitry Andric     CurAddress += 2;
7568d75effSDimitry Andric     *CurAddress = uint32_t(PatchOpcodes::PO_LdpX0X30SP_16);
7668d75effSDimitry Andric     CurAddress++;
7768d75effSDimitry Andric     std::atomic_store_explicit(
7868d75effSDimitry Andric         reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
7968d75effSDimitry Andric         uint32_t(PatchOpcodes::PO_StpX0X30SP_m16e), std::memory_order_release);
8068d75effSDimitry Andric   } else {
8168d75effSDimitry Andric     std::atomic_store_explicit(
8268d75effSDimitry Andric         reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
8368d75effSDimitry Andric         uint32_t(PatchOpcodes::PO_B32), std::memory_order_release);
8468d75effSDimitry Andric   }
8568d75effSDimitry Andric   __clear_cache(reinterpret_cast<char *>(FirstAddress),
8668d75effSDimitry Andric                 reinterpret_cast<char *>(CurAddress));
8768d75effSDimitry Andric   return true;
8868d75effSDimitry Andric }
8968d75effSDimitry Andric 
patchFunctionEntry(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* Trampoline)())9068d75effSDimitry Andric bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
9168d75effSDimitry Andric                         const XRaySledEntry &Sled,
9268d75effSDimitry Andric                         void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
9368d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, Trampoline);
9468d75effSDimitry Andric }
9568d75effSDimitry Andric 
patchFunctionExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)9668d75effSDimitry Andric bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
9768d75effSDimitry Andric                        const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
9868d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
9968d75effSDimitry Andric }
10068d75effSDimitry Andric 
patchFunctionTailExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)10168d75effSDimitry Andric bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
10268d75effSDimitry Andric                            const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
10368d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
10468d75effSDimitry Andric }
10568d75effSDimitry Andric 
106*06c3fb27SDimitry Andric // AArch64AsmPrinter::LowerPATCHABLE_EVENT_CALL generates this code sequence:
107*06c3fb27SDimitry Andric //
108*06c3fb27SDimitry Andric // .Lxray_event_sled_N:
109*06c3fb27SDimitry Andric //   b 1f
110*06c3fb27SDimitry Andric //   save x0 and x1 (and also x2 for TYPED_EVENT_CALL)
111*06c3fb27SDimitry Andric //   set up x0 and x1 (and also x2 for TYPED_EVENT_CALL)
112*06c3fb27SDimitry Andric //   bl __xray_CustomEvent or __xray_TypedEvent
113*06c3fb27SDimitry Andric //   restore x0 and x1 (and also x2 for TYPED_EVENT_CALL)
114*06c3fb27SDimitry Andric // 1f
115*06c3fb27SDimitry Andric //
116*06c3fb27SDimitry Andric // There are 6 instructions for EVENT_CALL and 9 for TYPED_EVENT_CALL.
117*06c3fb27SDimitry Andric //
118*06c3fb27SDimitry Andric // Enable: b .+24 => nop
119*06c3fb27SDimitry Andric // Disable: nop => b .+24
patchCustomEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)12068d75effSDimitry Andric bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
121*06c3fb27SDimitry Andric                       const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
122*06c3fb27SDimitry Andric   uint32_t Inst = Enable ? 0xd503201f : 0x14000006;
123*06c3fb27SDimitry Andric   std::atomic_store_explicit(
124*06c3fb27SDimitry Andric       reinterpret_cast<std::atomic<uint32_t> *>(Sled.address()), Inst,
125*06c3fb27SDimitry Andric       std::memory_order_release);
12668d75effSDimitry Andric   return false;
12768d75effSDimitry Andric }
12868d75effSDimitry Andric 
129*06c3fb27SDimitry Andric // Enable: b +36 => nop
130*06c3fb27SDimitry Andric // Disable: nop => b +36
patchTypedEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)13168d75effSDimitry Andric bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
13268d75effSDimitry Andric                      const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
133*06c3fb27SDimitry Andric   uint32_t Inst = Enable ? 0xd503201f : 0x14000009;
134*06c3fb27SDimitry Andric   std::atomic_store_explicit(
135*06c3fb27SDimitry Andric       reinterpret_cast<std::atomic<uint32_t> *>(Sled.address()), Inst,
136*06c3fb27SDimitry Andric       std::memory_order_release);
13768d75effSDimitry Andric   return false;
13868d75effSDimitry Andric }
13968d75effSDimitry Andric 
14068d75effSDimitry Andric // FIXME: Maybe implement this better?
probeRequiredCPUFeatures()14168d75effSDimitry Andric bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
14268d75effSDimitry Andric 
14368d75effSDimitry Andric } // namespace __xray
144