xref: /freebsd/contrib/llvm-project/compiler-rt/lib/xray/xray_AArch64.cpp (revision 1ed2ef42e01771f5d8ca9be61e07dcf0fd47feba)
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         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 
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, 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, 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, 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       reinterpret_cast<std::atomic<uint32_t> *>(Sled.address()), Inst,
130       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       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?
146 bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
147 
148 } // namespace __xray
149