170 lines · cpp
1//===-- xray_arm.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-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of XRay, a dynamic runtime instrumentation system.10//11// Implementation of ARM-specific routines (32-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 20extern "C" void __clear_cache(void *start, void *end);21 22namespace __xray {23 24// The machine codes for some instructions used in runtime patching.25enum class PatchOpcodes : uint32_t {26 PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr}27 PO_BlxIp = 0xE12FFF3C, // BLX ip28 PO_PopR0Lr = 0xE8BD4001, // POP {r0, lr}29 PO_B20 = 0xEA000005 // B #2030};31 32// 0xUUUUWXYZ -> 0x000W0XYZ33inline static uint32_t getMovwMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {34 return (Value & 0xfff) | ((Value & 0xf000) << 4);35}36 37// 0xWXYZUUUU -> 0x000W0XYZ38inline static uint32_t getMovtMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {39 return getMovwMask(Value >> 16);40}41 42// Writes the following instructions:43// MOVW R<regNo>, #<lower 16 bits of the |Value|>44// MOVT R<regNo>, #<higher 16 bits of the |Value|>45inline static uint32_t *46write32bitLoadReg(uint8_t regNo, uint32_t *Address,47 const uint32_t Value) XRAY_NEVER_INSTRUMENT {48 // This is a fatal error: we cannot just report it and continue execution.49 assert(regNo <= 15 && "Register number must be 0 to 15.");50 // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ51 *Address = (0xE3000000 | (uint32_t(regNo) << 12) | getMovwMask(Value));52 Address++;53 // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ54 *Address = (0xE3400000 | (uint32_t(regNo) << 12) | getMovtMask(Value));55 return Address + 1;56}57 58// Writes the following instructions:59// MOVW r0, #<lower 16 bits of the |Value|>60// MOVT r0, #<higher 16 bits of the |Value|>61inline static uint32_t *62write32bitLoadR0(uint32_t *Address,63 const uint32_t Value) XRAY_NEVER_INSTRUMENT {64 return write32bitLoadReg(0, Address, Value);65}66 67// Writes the following instructions:68// MOVW ip, #<lower 16 bits of the |Value|>69// MOVT ip, #<higher 16 bits of the |Value|>70inline static uint32_t *71write32bitLoadIP(uint32_t *Address,72 const uint32_t Value) XRAY_NEVER_INSTRUMENT {73 return write32bitLoadReg(12, Address, Value);74}75 76inline static bool patchSled(const bool Enable, const uint32_t FuncId,77 const XRaySledEntry &Sled,78 void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {79 // When |Enable| == true,80 // We replace the following compile-time stub (sled):81 //82 // xray_sled_n:83 // B #2084 // 6 NOPs (24 bytes)85 //86 // With the following runtime patch:87 //88 // xray_sled_n:89 // PUSH {r0, lr}90 // MOVW r0, #<lower 16 bits of function ID>91 // MOVT r0, #<higher 16 bits of function ID>92 // MOVW ip, #<lower 16 bits of address of TracingHook>93 // MOVT ip, #<higher 16 bits of address of TracingHook>94 // BLX ip95 // POP {r0, lr}96 //97 // Replacement of the first 4-byte instruction should be the last and atomic98 // operation, so that the user code which reaches the sled concurrently99 // either jumps over the whole sled, or executes the whole sled when the100 // latter is ready.101 //102 // When |Enable|==false, we set back the first instruction in the sled to be103 // B #20104 105 uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.address());106 uint32_t *CurAddress = FirstAddress + 1;107 if (Enable) {108 CurAddress =109 write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId));110 CurAddress =111 write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook));112 *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp);113 CurAddress++;114 *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr);115 CurAddress++;116 std::atomic_store_explicit(117 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),118 uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release);119 } else {120 std::atomic_store_explicit(121 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),122 uint32_t(PatchOpcodes::PO_B20), std::memory_order_release);123 }124 __clear_cache(reinterpret_cast<char *>(FirstAddress),125 reinterpret_cast<char *>(CurAddress));126 return true;127}128 129bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,130 const XRaySledEntry &Sled,131 const XRayTrampolines &Trampolines,132 bool LogArgs) XRAY_NEVER_INSTRUMENT {133 auto Trampoline =134 LogArgs ? Trampolines.LogArgsTrampoline : Trampolines.EntryTrampoline;135 return patchSled(Enable, FuncId, Sled, Trampoline);136}137 138bool patchFunctionExit(139 const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,140 const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {141 return patchSled(Enable, FuncId, Sled, Trampolines.ExitTrampoline);142}143 144bool patchFunctionTailExit(145 const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,146 const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {147 return patchSled(Enable, FuncId, Sled, Trampolines.TailExitTrampoline);148}149 150bool patchCustomEvent(const bool Enable, const uint32_t FuncId,151 const XRaySledEntry &Sled)152 XRAY_NEVER_INSTRUMENT { // FIXME: Implement in arm?153 return false;154}155 156bool patchTypedEvent(const bool Enable, const uint32_t FuncId,157 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {158 // FIXME: Implement in arm?159 return false;160}161 162// FIXME: Maybe implement this better?163bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }164 165} // namespace __xray166 167extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {168 // FIXME: this will have to be implemented in the trampoline assembly file169}170