168d75effSDimitry Andric //===-- xray_arm.cpp --------------------------------------------*- C++ -*-===// 268d75effSDimitry Andric // 368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 668d75effSDimitry Andric // 768d75effSDimitry Andric //===----------------------------------------------------------------------===// 868d75effSDimitry Andric // 968d75effSDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system. 1068d75effSDimitry Andric // 1168d75effSDimitry Andric // Implementation of ARM-specific routines (32-bit). 1268d75effSDimitry Andric // 1368d75effSDimitry Andric //===----------------------------------------------------------------------===// 1468d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h" 1568d75effSDimitry Andric #include "xray_defs.h" 1668d75effSDimitry Andric #include "xray_interface_internal.h" 1768d75effSDimitry Andric #include <atomic> 1868d75effSDimitry Andric #include <cassert> 1968d75effSDimitry Andric 2068d75effSDimitry Andric extern "C" void __clear_cache(void *start, void *end); 2168d75effSDimitry Andric 2268d75effSDimitry Andric namespace __xray { 2368d75effSDimitry Andric 2468d75effSDimitry Andric // The machine codes for some instructions used in runtime patching. 2568d75effSDimitry Andric enum class PatchOpcodes : uint32_t { 2668d75effSDimitry Andric PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr} 2768d75effSDimitry Andric PO_BlxIp = 0xE12FFF3C, // BLX ip 2868d75effSDimitry Andric PO_PopR0Lr = 0xE8BD4001, // POP {r0, lr} 2968d75effSDimitry Andric PO_B20 = 0xEA000005 // B #20 3068d75effSDimitry Andric }; 3168d75effSDimitry Andric 3268d75effSDimitry Andric // 0xUUUUWXYZ -> 0x000W0XYZ 3368d75effSDimitry Andric inline static uint32_t getMovwMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT { 3468d75effSDimitry Andric return (Value & 0xfff) | ((Value & 0xf000) << 4); 3568d75effSDimitry Andric } 3668d75effSDimitry Andric 3768d75effSDimitry Andric // 0xWXYZUUUU -> 0x000W0XYZ 3868d75effSDimitry Andric inline static uint32_t getMovtMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT { 3968d75effSDimitry Andric return getMovwMask(Value >> 16); 4068d75effSDimitry Andric } 4168d75effSDimitry Andric 4268d75effSDimitry Andric // Writes the following instructions: 4368d75effSDimitry Andric // MOVW R<regNo>, #<lower 16 bits of the |Value|> 4468d75effSDimitry Andric // MOVT R<regNo>, #<higher 16 bits of the |Value|> 4568d75effSDimitry Andric inline static uint32_t * 4668d75effSDimitry Andric write32bitLoadReg(uint8_t regNo, uint32_t *Address, 4768d75effSDimitry Andric const uint32_t Value) XRAY_NEVER_INSTRUMENT { 4868d75effSDimitry Andric // This is a fatal error: we cannot just report it and continue execution. 4968d75effSDimitry Andric assert(regNo <= 15 && "Register number must be 0 to 15."); 5068d75effSDimitry Andric // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ 5168d75effSDimitry Andric *Address = (0xE3000000 | (uint32_t(regNo) << 12) | getMovwMask(Value)); 5268d75effSDimitry Andric Address++; 5368d75effSDimitry Andric // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ 5468d75effSDimitry Andric *Address = (0xE3400000 | (uint32_t(regNo) << 12) | getMovtMask(Value)); 5568d75effSDimitry Andric return Address + 1; 5668d75effSDimitry Andric } 5768d75effSDimitry Andric 5868d75effSDimitry Andric // Writes the following instructions: 5968d75effSDimitry Andric // MOVW r0, #<lower 16 bits of the |Value|> 6068d75effSDimitry Andric // MOVT r0, #<higher 16 bits of the |Value|> 6168d75effSDimitry Andric inline static uint32_t * 6268d75effSDimitry Andric write32bitLoadR0(uint32_t *Address, 6368d75effSDimitry Andric const uint32_t Value) XRAY_NEVER_INSTRUMENT { 6468d75effSDimitry Andric return write32bitLoadReg(0, Address, Value); 6568d75effSDimitry Andric } 6668d75effSDimitry Andric 6768d75effSDimitry Andric // Writes the following instructions: 6868d75effSDimitry Andric // MOVW ip, #<lower 16 bits of the |Value|> 6968d75effSDimitry Andric // MOVT ip, #<higher 16 bits of the |Value|> 7068d75effSDimitry Andric inline static uint32_t * 7168d75effSDimitry Andric write32bitLoadIP(uint32_t *Address, 7268d75effSDimitry Andric const uint32_t Value) XRAY_NEVER_INSTRUMENT { 7368d75effSDimitry Andric return write32bitLoadReg(12, Address, Value); 7468d75effSDimitry Andric } 7568d75effSDimitry Andric 7668d75effSDimitry Andric inline static bool patchSled(const bool Enable, const uint32_t FuncId, 7768d75effSDimitry Andric const XRaySledEntry &Sled, 7868d75effSDimitry Andric void (*TracingHook)()) XRAY_NEVER_INSTRUMENT { 7968d75effSDimitry Andric // When |Enable| == true, 8068d75effSDimitry Andric // We replace the following compile-time stub (sled): 8168d75effSDimitry Andric // 8268d75effSDimitry Andric // xray_sled_n: 8368d75effSDimitry Andric // B #20 8468d75effSDimitry Andric // 6 NOPs (24 bytes) 8568d75effSDimitry Andric // 8668d75effSDimitry Andric // With the following runtime patch: 8768d75effSDimitry Andric // 8868d75effSDimitry Andric // xray_sled_n: 8968d75effSDimitry Andric // PUSH {r0, lr} 9068d75effSDimitry Andric // MOVW r0, #<lower 16 bits of function ID> 9168d75effSDimitry Andric // MOVT r0, #<higher 16 bits of function ID> 9268d75effSDimitry Andric // MOVW ip, #<lower 16 bits of address of TracingHook> 9368d75effSDimitry Andric // MOVT ip, #<higher 16 bits of address of TracingHook> 9468d75effSDimitry Andric // BLX ip 9568d75effSDimitry Andric // POP {r0, lr} 9668d75effSDimitry Andric // 9768d75effSDimitry Andric // Replacement of the first 4-byte instruction should be the last and atomic 9868d75effSDimitry Andric // operation, so that the user code which reaches the sled concurrently 9968d75effSDimitry Andric // either jumps over the whole sled, or executes the whole sled when the 10068d75effSDimitry Andric // latter is ready. 10168d75effSDimitry Andric // 10268d75effSDimitry Andric // When |Enable|==false, we set back the first instruction in the sled to be 10368d75effSDimitry Andric // B #20 10468d75effSDimitry Andric 105*5ffd83dbSDimitry Andric uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.address()); 10668d75effSDimitry Andric uint32_t *CurAddress = FirstAddress + 1; 10768d75effSDimitry Andric if (Enable) { 10868d75effSDimitry Andric CurAddress = 10968d75effSDimitry Andric write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId)); 11068d75effSDimitry Andric CurAddress = 11168d75effSDimitry Andric write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook)); 11268d75effSDimitry Andric *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp); 11368d75effSDimitry Andric CurAddress++; 11468d75effSDimitry Andric *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr); 11568d75effSDimitry Andric CurAddress++; 11668d75effSDimitry Andric std::atomic_store_explicit( 11768d75effSDimitry Andric reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), 11868d75effSDimitry Andric uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release); 11968d75effSDimitry Andric } else { 12068d75effSDimitry Andric std::atomic_store_explicit( 12168d75effSDimitry Andric reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), 12268d75effSDimitry Andric uint32_t(PatchOpcodes::PO_B20), std::memory_order_release); 12368d75effSDimitry Andric } 12468d75effSDimitry Andric __clear_cache(reinterpret_cast<char *>(FirstAddress), 12568d75effSDimitry Andric reinterpret_cast<char *>(CurAddress)); 12668d75effSDimitry Andric return true; 12768d75effSDimitry Andric } 12868d75effSDimitry Andric 12968d75effSDimitry Andric bool patchFunctionEntry(const bool Enable, const uint32_t FuncId, 13068d75effSDimitry Andric const XRaySledEntry &Sled, 13168d75effSDimitry Andric void (*Trampoline)()) XRAY_NEVER_INSTRUMENT { 13268d75effSDimitry Andric return patchSled(Enable, FuncId, Sled, Trampoline); 13368d75effSDimitry Andric } 13468d75effSDimitry Andric 13568d75effSDimitry Andric bool patchFunctionExit(const bool Enable, const uint32_t FuncId, 13668d75effSDimitry Andric const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 13768d75effSDimitry Andric return patchSled(Enable, FuncId, Sled, __xray_FunctionExit); 13868d75effSDimitry Andric } 13968d75effSDimitry Andric 14068d75effSDimitry Andric bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId, 14168d75effSDimitry Andric const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 14268d75effSDimitry Andric return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit); 14368d75effSDimitry Andric } 14468d75effSDimitry Andric 14568d75effSDimitry Andric bool patchCustomEvent(const bool Enable, const uint32_t FuncId, 14668d75effSDimitry Andric const XRaySledEntry &Sled) 14768d75effSDimitry Andric XRAY_NEVER_INSTRUMENT { // FIXME: Implement in arm? 14868d75effSDimitry Andric return false; 14968d75effSDimitry Andric } 15068d75effSDimitry Andric 15168d75effSDimitry Andric bool patchTypedEvent(const bool Enable, const uint32_t FuncId, 15268d75effSDimitry Andric const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { 15368d75effSDimitry Andric // FIXME: Implement in arm? 15468d75effSDimitry Andric return false; 15568d75effSDimitry Andric } 15668d75effSDimitry Andric 15768d75effSDimitry Andric // FIXME: Maybe implement this better? 15868d75effSDimitry Andric bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; } 15968d75effSDimitry Andric 16068d75effSDimitry Andric } // namespace __xray 16168d75effSDimitry Andric 16268d75effSDimitry Andric extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT { 16368d75effSDimitry Andric // FIXME: this will have to be implemented in the trampoline assembly file 16468d75effSDimitry Andric } 165