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