blob: 8d1c7c5d807f17495545ddc72b1a73c09a56ef06 [file] [log] [blame]
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +00001//===-- xray_AArch64.cc -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of XRay, a dynamic runtime instrumentation system.
11//
12// Implementation of AArch64-specific routines (64-bit).
13//
14//===----------------------------------------------------------------------===//
15#include "sanitizer_common/sanitizer_common.h"
16#include "xray_defs.h"
17#include "xray_interface_internal.h"
18#include <atomic>
19#include <cassert>
20
Serge Rogatch4ab7a302017-01-10 16:16:33 +000021
22extern "C" void __clear_cache(void* start, void* end);
23
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +000024namespace __xray {
25
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +000026// The machine codes for some instructions used in runtime patching.
27enum class PatchOpcodes : uint32_t {
28 PO_StpX0X30SP_m16e = 0xA9BF7BE0, // STP X0, X30, [SP, #-16]!
29 PO_LdrW0_12 = 0x18000060, // LDR W0, #12
30 PO_LdrX16_12 = 0x58000070, // LDR X16, #12
31 PO_BlrX16 = 0xD63F0200, // BLR X16
32 PO_LdpX0X30SP_16 = 0xA8C17BE0, // LDP X0, X30, [SP], #16
33 PO_B32 = 0x14000008 // B #32
34};
35
36inline static bool patchSled(const bool Enable, const uint32_t FuncId,
37 const XRaySledEntry &Sled,
38 void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
39 // When |Enable| == true,
40 // We replace the following compile-time stub (sled):
41 //
42 // xray_sled_n:
43 // B #32
44 // 7 NOPs (24 bytes)
45 //
46 // With the following runtime patch:
47 //
48 // xray_sled_n:
49 // STP X0, X30, [SP, #-16]! ; PUSH {r0, lr}
50 // LDR W0, #12 ; W0 := function ID
51 // LDR X16,#12 ; X16 := address of the trampoline
52 // BLR X16
53 // ;DATA: 32 bits of function ID
54 // ;DATA: lower 32 bits of the address of the trampoline
55 // ;DATA: higher 32 bits of the address of the trampoline
56 // LDP X0, X30, [SP], #16 ; POP {r0, lr}
57 //
58 // Replacement of the first 4-byte instruction should be the last and atomic
59 // operation, so that the user code which reaches the sled concurrently
60 // either jumps over the whole sled, or executes the whole sled when the
61 // latter is ready.
62 //
63 // When |Enable|==false, we set back the first instruction in the sled to be
64 // B #32
65
66 uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address);
Serge Rogatch4ab7a302017-01-10 16:16:33 +000067 uint32_t *CurAddress = FirstAddress + 1;
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +000068 if (Enable) {
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +000069 *CurAddress = uint32_t(PatchOpcodes::PO_LdrW0_12);
70 CurAddress++;
71 *CurAddress = uint32_t(PatchOpcodes::PO_LdrX16_12);
72 CurAddress++;
73 *CurAddress = uint32_t(PatchOpcodes::PO_BlrX16);
74 CurAddress++;
75 *CurAddress = FuncId;
76 CurAddress++;
77 *reinterpret_cast<void (**)()>(CurAddress) = TracingHook;
78 CurAddress += 2;
79 *CurAddress = uint32_t(PatchOpcodes::PO_LdpX0X30SP_16);
Serge Rogatch4ab7a302017-01-10 16:16:33 +000080 CurAddress++;
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +000081 std::atomic_store_explicit(
82 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
83 uint32_t(PatchOpcodes::PO_StpX0X30SP_m16e), std::memory_order_release);
84 } else {
85 std::atomic_store_explicit(
86 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
87 uint32_t(PatchOpcodes::PO_B32), std::memory_order_release);
88 }
Serge Rogatch4ab7a302017-01-10 16:16:33 +000089 __clear_cache(reinterpret_cast<char*>(FirstAddress),
90 reinterpret_cast<char*>(CurAddress));
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +000091 return true;
92}
93
94bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
Dean Michael Berrisa814c942017-03-06 07:25:41 +000095 const XRaySledEntry &Sled,
96 void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
97 return patchSled(Enable, FuncId, Sled, Trampoline);
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +000098}
99
100bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
101 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
102 return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
103}
104
105bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
106 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
Serge Rogatchfa846a12017-01-25 20:27:19 +0000107 return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +0000108}
109
Dean Michael Berris607617b2017-02-02 07:51:21 +0000110// FIXME: Maybe implement this better?
111bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
112
Dean Michael Berrisbad8f0f2016-11-21 03:20:43 +0000113} // namespace __xray
Dean Michael Berrisa814c942017-03-06 07:25:41 +0000114
115extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
116 // FIXME: this will have to be implemented in the trampoline assembly file
117}