13cab2bb3Spatrick //===-- xray_arm.cpp --------------------------------------------*- C++ -*-===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is a part of XRay, a dynamic runtime instrumentation system.
103cab2bb3Spatrick //
113cab2bb3Spatrick // Implementation of ARM-specific routines (32-bit).
123cab2bb3Spatrick //
133cab2bb3Spatrick //===----------------------------------------------------------------------===//
143cab2bb3Spatrick #include "sanitizer_common/sanitizer_common.h"
153cab2bb3Spatrick #include "xray_defs.h"
163cab2bb3Spatrick #include "xray_interface_internal.h"
173cab2bb3Spatrick #include <atomic>
183cab2bb3Spatrick #include <cassert>
193cab2bb3Spatrick
203cab2bb3Spatrick extern "C" void __clear_cache(void *start, void *end);
213cab2bb3Spatrick
223cab2bb3Spatrick namespace __xray {
233cab2bb3Spatrick
243cab2bb3Spatrick // The machine codes for some instructions used in runtime patching.
253cab2bb3Spatrick enum class PatchOpcodes : uint32_t {
263cab2bb3Spatrick PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr}
273cab2bb3Spatrick PO_BlxIp = 0xE12FFF3C, // BLX ip
283cab2bb3Spatrick PO_PopR0Lr = 0xE8BD4001, // POP {r0, lr}
293cab2bb3Spatrick PO_B20 = 0xEA000005 // B #20
303cab2bb3Spatrick };
313cab2bb3Spatrick
323cab2bb3Spatrick // 0xUUUUWXYZ -> 0x000W0XYZ
getMovwMask(const uint32_t Value)333cab2bb3Spatrick inline static uint32_t getMovwMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
343cab2bb3Spatrick return (Value & 0xfff) | ((Value & 0xf000) << 4);
353cab2bb3Spatrick }
363cab2bb3Spatrick
373cab2bb3Spatrick // 0xWXYZUUUU -> 0x000W0XYZ
getMovtMask(const uint32_t Value)383cab2bb3Spatrick inline static uint32_t getMovtMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
393cab2bb3Spatrick return getMovwMask(Value >> 16);
403cab2bb3Spatrick }
413cab2bb3Spatrick
423cab2bb3Spatrick // Writes the following instructions:
433cab2bb3Spatrick // MOVW R<regNo>, #<lower 16 bits of the |Value|>
443cab2bb3Spatrick // MOVT R<regNo>, #<higher 16 bits of the |Value|>
453cab2bb3Spatrick inline static uint32_t *
write32bitLoadReg(uint8_t regNo,uint32_t * Address,const uint32_t Value)463cab2bb3Spatrick write32bitLoadReg(uint8_t regNo, uint32_t *Address,
473cab2bb3Spatrick const uint32_t Value) XRAY_NEVER_INSTRUMENT {
483cab2bb3Spatrick // This is a fatal error: we cannot just report it and continue execution.
493cab2bb3Spatrick assert(regNo <= 15 && "Register number must be 0 to 15.");
503cab2bb3Spatrick // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ
513cab2bb3Spatrick *Address = (0xE3000000 | (uint32_t(regNo) << 12) | getMovwMask(Value));
523cab2bb3Spatrick Address++;
533cab2bb3Spatrick // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ
543cab2bb3Spatrick *Address = (0xE3400000 | (uint32_t(regNo) << 12) | getMovtMask(Value));
553cab2bb3Spatrick return Address + 1;
563cab2bb3Spatrick }
573cab2bb3Spatrick
583cab2bb3Spatrick // Writes the following instructions:
593cab2bb3Spatrick // MOVW r0, #<lower 16 bits of the |Value|>
603cab2bb3Spatrick // MOVT r0, #<higher 16 bits of the |Value|>
613cab2bb3Spatrick inline static uint32_t *
write32bitLoadR0(uint32_t * Address,const uint32_t Value)623cab2bb3Spatrick write32bitLoadR0(uint32_t *Address,
633cab2bb3Spatrick const uint32_t Value) XRAY_NEVER_INSTRUMENT {
643cab2bb3Spatrick return write32bitLoadReg(0, Address, Value);
653cab2bb3Spatrick }
663cab2bb3Spatrick
673cab2bb3Spatrick // Writes the following instructions:
683cab2bb3Spatrick // MOVW ip, #<lower 16 bits of the |Value|>
693cab2bb3Spatrick // MOVT ip, #<higher 16 bits of the |Value|>
703cab2bb3Spatrick inline static uint32_t *
write32bitLoadIP(uint32_t * Address,const uint32_t Value)713cab2bb3Spatrick write32bitLoadIP(uint32_t *Address,
723cab2bb3Spatrick const uint32_t Value) XRAY_NEVER_INSTRUMENT {
733cab2bb3Spatrick return write32bitLoadReg(12, Address, Value);
743cab2bb3Spatrick }
753cab2bb3Spatrick
patchSled(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* TracingHook)())763cab2bb3Spatrick inline static bool patchSled(const bool Enable, const uint32_t FuncId,
773cab2bb3Spatrick const XRaySledEntry &Sled,
783cab2bb3Spatrick void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
793cab2bb3Spatrick // When |Enable| == true,
803cab2bb3Spatrick // We replace the following compile-time stub (sled):
813cab2bb3Spatrick //
823cab2bb3Spatrick // xray_sled_n:
833cab2bb3Spatrick // B #20
843cab2bb3Spatrick // 6 NOPs (24 bytes)
853cab2bb3Spatrick //
863cab2bb3Spatrick // With the following runtime patch:
873cab2bb3Spatrick //
883cab2bb3Spatrick // xray_sled_n:
893cab2bb3Spatrick // PUSH {r0, lr}
903cab2bb3Spatrick // MOVW r0, #<lower 16 bits of function ID>
913cab2bb3Spatrick // MOVT r0, #<higher 16 bits of function ID>
923cab2bb3Spatrick // MOVW ip, #<lower 16 bits of address of TracingHook>
933cab2bb3Spatrick // MOVT ip, #<higher 16 bits of address of TracingHook>
943cab2bb3Spatrick // BLX ip
953cab2bb3Spatrick // POP {r0, lr}
963cab2bb3Spatrick //
973cab2bb3Spatrick // Replacement of the first 4-byte instruction should be the last and atomic
983cab2bb3Spatrick // operation, so that the user code which reaches the sled concurrently
993cab2bb3Spatrick // either jumps over the whole sled, or executes the whole sled when the
1003cab2bb3Spatrick // latter is ready.
1013cab2bb3Spatrick //
1023cab2bb3Spatrick // When |Enable|==false, we set back the first instruction in the sled to be
1033cab2bb3Spatrick // B #20
1043cab2bb3Spatrick
105*1f9cb04fSpatrick uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.address());
1063cab2bb3Spatrick uint32_t *CurAddress = FirstAddress + 1;
1073cab2bb3Spatrick if (Enable) {
1083cab2bb3Spatrick CurAddress =
1093cab2bb3Spatrick write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId));
1103cab2bb3Spatrick CurAddress =
1113cab2bb3Spatrick write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook));
1123cab2bb3Spatrick *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp);
1133cab2bb3Spatrick CurAddress++;
1143cab2bb3Spatrick *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr);
1153cab2bb3Spatrick CurAddress++;
1163cab2bb3Spatrick std::atomic_store_explicit(
1173cab2bb3Spatrick reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
1183cab2bb3Spatrick uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release);
1193cab2bb3Spatrick } else {
1203cab2bb3Spatrick std::atomic_store_explicit(
1213cab2bb3Spatrick reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
1223cab2bb3Spatrick uint32_t(PatchOpcodes::PO_B20), std::memory_order_release);
1233cab2bb3Spatrick }
1243cab2bb3Spatrick __clear_cache(reinterpret_cast<char *>(FirstAddress),
1253cab2bb3Spatrick reinterpret_cast<char *>(CurAddress));
1263cab2bb3Spatrick return true;
1273cab2bb3Spatrick }
1283cab2bb3Spatrick
patchFunctionEntry(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* Trampoline)())1293cab2bb3Spatrick bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
1303cab2bb3Spatrick const XRaySledEntry &Sled,
1313cab2bb3Spatrick void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
1323cab2bb3Spatrick return patchSled(Enable, FuncId, Sled, Trampoline);
1333cab2bb3Spatrick }
1343cab2bb3Spatrick
patchFunctionExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1353cab2bb3Spatrick bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
1363cab2bb3Spatrick const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1373cab2bb3Spatrick return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
1383cab2bb3Spatrick }
1393cab2bb3Spatrick
patchFunctionTailExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1403cab2bb3Spatrick bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
1413cab2bb3Spatrick const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1423cab2bb3Spatrick return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
1433cab2bb3Spatrick }
1443cab2bb3Spatrick
patchCustomEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1453cab2bb3Spatrick bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
1463cab2bb3Spatrick const XRaySledEntry &Sled)
1473cab2bb3Spatrick XRAY_NEVER_INSTRUMENT { // FIXME: Implement in arm?
1483cab2bb3Spatrick return false;
1493cab2bb3Spatrick }
1503cab2bb3Spatrick
patchTypedEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)1513cab2bb3Spatrick bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
1523cab2bb3Spatrick const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
1533cab2bb3Spatrick // FIXME: Implement in arm?
1543cab2bb3Spatrick return false;
1553cab2bb3Spatrick }
1563cab2bb3Spatrick
1573cab2bb3Spatrick // FIXME: Maybe implement this better?
probeRequiredCPUFeatures()1583cab2bb3Spatrick bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
1593cab2bb3Spatrick
1603cab2bb3Spatrick } // namespace __xray
1613cab2bb3Spatrick
__xray_ArgLoggerEntry()1623cab2bb3Spatrick extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
1633cab2bb3Spatrick // FIXME: this will have to be implemented in the trampoline assembly file
1643cab2bb3Spatrick }
165