Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 1 | //===-- 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 | |
Dean Michael Berris | 29e16de | 2017-05-12 01:07:41 +0000 | [diff] [blame^] | 21 | extern "C" void __clear_cache(void *start, void *end); |
Serge Rogatch | 4ab7a30 | 2017-01-10 16:16:33 +0000 | [diff] [blame] | 22 | |
Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 23 | namespace __xray { |
| 24 | |
Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 25 | // The machine codes for some instructions used in runtime patching. |
| 26 | enum class PatchOpcodes : uint32_t { |
| 27 | PO_StpX0X30SP_m16e = 0xA9BF7BE0, // STP X0, X30, [SP, #-16]! |
| 28 | PO_LdrW0_12 = 0x18000060, // LDR W0, #12 |
| 29 | PO_LdrX16_12 = 0x58000070, // LDR X16, #12 |
| 30 | PO_BlrX16 = 0xD63F0200, // BLR X16 |
| 31 | PO_LdpX0X30SP_16 = 0xA8C17BE0, // LDP X0, X30, [SP], #16 |
| 32 | PO_B32 = 0x14000008 // B #32 |
| 33 | }; |
| 34 | |
| 35 | inline static bool patchSled(const bool Enable, const uint32_t FuncId, |
| 36 | const XRaySledEntry &Sled, |
| 37 | void (*TracingHook)()) XRAY_NEVER_INSTRUMENT { |
| 38 | // When |Enable| == true, |
| 39 | // We replace the following compile-time stub (sled): |
| 40 | // |
| 41 | // xray_sled_n: |
| 42 | // B #32 |
| 43 | // 7 NOPs (24 bytes) |
| 44 | // |
| 45 | // With the following runtime patch: |
| 46 | // |
| 47 | // xray_sled_n: |
| 48 | // STP X0, X30, [SP, #-16]! ; PUSH {r0, lr} |
| 49 | // LDR W0, #12 ; W0 := function ID |
| 50 | // LDR X16,#12 ; X16 := address of the trampoline |
| 51 | // BLR X16 |
| 52 | // ;DATA: 32 bits of function ID |
| 53 | // ;DATA: lower 32 bits of the address of the trampoline |
| 54 | // ;DATA: higher 32 bits of the address of the trampoline |
| 55 | // LDP X0, X30, [SP], #16 ; POP {r0, lr} |
| 56 | // |
| 57 | // Replacement of the first 4-byte instruction should be the last and atomic |
| 58 | // operation, so that the user code which reaches the sled concurrently |
| 59 | // either jumps over the whole sled, or executes the whole sled when the |
| 60 | // latter is ready. |
| 61 | // |
| 62 | // When |Enable|==false, we set back the first instruction in the sled to be |
| 63 | // B #32 |
| 64 | |
| 65 | uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address); |
Serge Rogatch | 4ab7a30 | 2017-01-10 16:16:33 +0000 | [diff] [blame] | 66 | uint32_t *CurAddress = FirstAddress + 1; |
Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 67 | if (Enable) { |
Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 68 | *CurAddress = uint32_t(PatchOpcodes::PO_LdrW0_12); |
| 69 | CurAddress++; |
| 70 | *CurAddress = uint32_t(PatchOpcodes::PO_LdrX16_12); |
| 71 | CurAddress++; |
| 72 | *CurAddress = uint32_t(PatchOpcodes::PO_BlrX16); |
| 73 | CurAddress++; |
| 74 | *CurAddress = FuncId; |
| 75 | CurAddress++; |
| 76 | *reinterpret_cast<void (**)()>(CurAddress) = TracingHook; |
| 77 | CurAddress += 2; |
| 78 | *CurAddress = uint32_t(PatchOpcodes::PO_LdpX0X30SP_16); |
Serge Rogatch | 4ab7a30 | 2017-01-10 16:16:33 +0000 | [diff] [blame] | 79 | CurAddress++; |
Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 80 | std::atomic_store_explicit( |
| 81 | reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), |
| 82 | uint32_t(PatchOpcodes::PO_StpX0X30SP_m16e), std::memory_order_release); |
| 83 | } else { |
| 84 | std::atomic_store_explicit( |
| 85 | reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), |
| 86 | uint32_t(PatchOpcodes::PO_B32), std::memory_order_release); |
| 87 | } |
Dean Michael Berris | 29e16de | 2017-05-12 01:07:41 +0000 | [diff] [blame^] | 88 | __clear_cache(reinterpret_cast<char *>(FirstAddress), |
| 89 | reinterpret_cast<char *>(CurAddress)); |
Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool patchFunctionEntry(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | a814c94 | 2017-03-06 07:25:41 +0000 | [diff] [blame] | 94 | const XRaySledEntry &Sled, |
| 95 | void (*Trampoline)()) XRAY_NEVER_INSTRUMENT { |
| 96 | return patchSled(Enable, FuncId, Sled, Trampoline); |
Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool patchFunctionExit(const bool Enable, const uint32_t FuncId, |
| 100 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
| 101 | return patchSled(Enable, FuncId, Sled, __xray_FunctionExit); |
| 102 | } |
| 103 | |
| 104 | bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId, |
| 105 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
Serge Rogatch | fa846a1 | 2017-01-25 20:27:19 +0000 | [diff] [blame] | 106 | return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit); |
Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Dean Michael Berris | 29e16de | 2017-05-12 01:07:41 +0000 | [diff] [blame^] | 109 | bool patchCustomEvent(const bool Enable, const uint32_t FuncId, |
| 110 | const XRaySled &Sled) |
| 111 | XRAY_NEVER_INSTRUMENT { // FIXME: Implement in aarch64? |
| 112 | return false; |
| 113 | } |
| 114 | |
Dean Michael Berris | 607617b | 2017-02-02 07:51:21 +0000 | [diff] [blame] | 115 | // FIXME: Maybe implement this better? |
| 116 | bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; } |
| 117 | |
Dean Michael Berris | bad8f0f | 2016-11-21 03:20:43 +0000 | [diff] [blame] | 118 | } // namespace __xray |
Dean Michael Berris | a814c94 | 2017-03-06 07:25:41 +0000 | [diff] [blame] | 119 | |
| 120 | extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT { |
| 121 | // FIXME: this will have to be implemented in the trampoline assembly file |
| 122 | } |