1 //===-- xray_AArch64.cc -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
12 // Implementation of AArch64-specific routines (64-bit).
14 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "xray_defs.h"
17 #include "xray_emulate_tsc.h"
18 #include "xray_interface_internal.h"
23 extern "C" void __clear_cache(void* start, void* end);
27 uint64_t cycleFrequency() XRAY_NEVER_INSTRUMENT {
28 // There is no instruction like RDTSCP in user mode on ARM. ARM's CP15 does
29 // not have a constant frequency like TSC on x86[_64]; it may go faster or
30 // slower depending on CPU's turbo or power saving modes. Furthermore, to
31 // read from CP15 on ARM a kernel modification or a driver is needed.
32 // We can not require this from users of compiler-rt.
33 // So on ARM we use clock_gettime(2) which gives the result in nanoseconds.
34 // To get the measurements per second, we scale this by the number of
35 // nanoseconds per second, pretending that the TSC frequency is 1GHz and
36 // one TSC tick is 1 nanosecond.
37 return NanosecondsPerSecond;
40 // The machine codes for some instructions used in runtime patching.
41 enum class PatchOpcodes : uint32_t {
42 PO_StpX0X30SP_m16e = 0xA9BF7BE0, // STP X0, X30, [SP, #-16]!
43 PO_LdrW0_12 = 0x18000060, // LDR W0, #12
44 PO_LdrX16_12 = 0x58000070, // LDR X16, #12
45 PO_BlrX16 = 0xD63F0200, // BLR X16
46 PO_LdpX0X30SP_16 = 0xA8C17BE0, // LDP X0, X30, [SP], #16
47 PO_B32 = 0x14000008 // B #32
50 inline static bool patchSled(const bool Enable, const uint32_t FuncId,
51 const XRaySledEntry &Sled,
52 void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
53 // When |Enable| == true,
54 // We replace the following compile-time stub (sled):
60 // With the following runtime patch:
63 // STP X0, X30, [SP, #-16]! ; PUSH {r0, lr}
64 // LDR W0, #12 ; W0 := function ID
65 // LDR X16,#12 ; X16 := address of the trampoline
67 // ;DATA: 32 bits of function ID
68 // ;DATA: lower 32 bits of the address of the trampoline
69 // ;DATA: higher 32 bits of the address of the trampoline
70 // LDP X0, X30, [SP], #16 ; POP {r0, lr}
72 // Replacement of the first 4-byte instruction should be the last and atomic
73 // operation, so that the user code which reaches the sled concurrently
74 // either jumps over the whole sled, or executes the whole sled when the
77 // When |Enable|==false, we set back the first instruction in the sled to be
80 uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address);
81 uint32_t *CurAddress = FirstAddress + 1;
83 *CurAddress = uint32_t(PatchOpcodes::PO_LdrW0_12);
85 *CurAddress = uint32_t(PatchOpcodes::PO_LdrX16_12);
87 *CurAddress = uint32_t(PatchOpcodes::PO_BlrX16);
91 *reinterpret_cast<void (**)()>(CurAddress) = TracingHook;
93 *CurAddress = uint32_t(PatchOpcodes::PO_LdpX0X30SP_16);
95 std::atomic_store_explicit(
96 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
97 uint32_t(PatchOpcodes::PO_StpX0X30SP_m16e), std::memory_order_release);
99 std::atomic_store_explicit(
100 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
101 uint32_t(PatchOpcodes::PO_B32), std::memory_order_release);
103 __clear_cache(reinterpret_cast<char*>(FirstAddress),
104 reinterpret_cast<char*>(CurAddress));
108 bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
109 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
110 return patchSled(Enable, FuncId, Sled, __xray_FunctionEntry);
113 bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
114 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
115 return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
118 bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
119 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
120 // FIXME: In the future we'd need to distinguish between non-tail exits and
121 // tail exits for better information preservation.
122 return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
125 } // namespace __xray