blob: 00150f62cf19f068ffe111d77c7bee9bb1da6c95 [file] [log] [blame]
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +00001//===-- xray_arm.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 ARM-specific routines (32-bit).
13//
14//===----------------------------------------------------------------------===//
15#include "xray_interface_internal.h"
16#include "sanitizer_common/sanitizer_common.h"
17#include <atomic>
18#include <cassert>
19
20namespace __xray {
21
22// The machine codes for some instructions used in runtime patching.
23enum class PatchOpcodes : uint32_t
24{
25 PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr}
26 PO_BlxIp = 0xE12FFF3C, // BLX ip
27 PO_PopR0Lr = 0xE8BD4001, // POP {r0, lr}
28 PO_B20 = 0xEA000005 // B #20
29};
30
31// 0xUUUUWXYZ -> 0x000W0XYZ
32inline static uint32_t getMovwMask(const uint32_t Value) {
33 return (Value & 0xfff) | ((Value & 0xf000) << 4);
34}
35
36// 0xWXYZUUUU -> 0x000W0XYZ
37inline static uint32_t getMovtMask(const uint32_t Value) {
38 return getMovwMask(Value >> 16);
39}
40
41// Writes the following instructions:
42// MOVW R<regNo>, #<lower 16 bits of the |Value|>
43// MOVT R<regNo>, #<higher 16 bits of the |Value|>
44inline static uint32_t* write32bitLoadReg(uint8_t regNo, uint32_t* Address,
45 const uint32_t Value) {
46 //This is a fatal error: we cannot just report it and continue execution.
47 assert(regNo <= 15 && "Register number must be 0 to 15.");
48 // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ
49 *Address = (0xE3000000 | (uint32_t(regNo)<<12) | getMovwMask(Value));
50 Address++;
51 // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ
52 *Address = (0xE3400000 | (uint32_t(regNo)<<12) | getMovtMask(Value));
53 return Address + 1;
54}
55
56// Writes the following instructions:
57// MOVW r0, #<lower 16 bits of the |Value|>
58// MOVT r0, #<higher 16 bits of the |Value|>
59inline static uint32_t *Write32bitLoadR0(uint32_t *Address,
60 const uint32_t Value) {
61 return write32bitLoadReg(0, Address, Value);
62}
63
64// Writes the following instructions:
65// MOVW ip, #<lower 16 bits of the |Value|>
66// MOVT ip, #<higher 16 bits of the |Value|>
67inline static uint32_t *Write32bitLoadIP(uint32_t *Address,
68 const uint32_t Value) {
69 return write32bitLoadReg(12, Address, Value);
70}
71
72inline static bool patchSled(const bool Enable, const uint32_t FuncId,
73 const XRaySledEntry &Sled, void (*TracingHook)()) {
74 // When |Enable| == true,
75 // We replace the following compile-time stub (sled):
76 //
77 // xray_sled_n:
78 // B #20
79 // 6 NOPs (24 bytes)
80 //
81 // With the following runtime patch:
82 //
83 // xray_sled_n:
84 // PUSH {r0, lr}
85 // MOVW r0, #<lower 16 bits of function ID>
86 // MOVT r0, #<higher 16 bits of function ID>
87 // MOVW ip, #<lower 16 bits of address of TracingHook>
88 // MOVT ip, #<higher 16 bits of address of TracingHook>
89 // BLX ip
90 // POP {r0, lr}
91 //
92 // Replacement of the first 4-byte instruction should be the last and atomic
93 // operation, so that the user code which reaches the sled concurrently
94 // either jumps over the whole sled, or executes the whole sled when the
95 // latter is ready.
96 //
97 // When |Enable|==false, we set back the first instruction in the sled to be
98 // B #20
99
100 uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address);
101 if (Enable) {
102 uint32_t *CurAddress = FirstAddress + 1;
103 CurAddress =
104 Write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId));
105 CurAddress =
106 Write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook));
107 *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp);
108 CurAddress++;
109 *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr);
110 std::atomic_store_explicit(
111 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
112 uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release);
113 } else {
114 std::atomic_store_explicit(
115 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
116 uint32_t(PatchOpcodes::PO_B20), std::memory_order_release);
117 }
118 return true;
119}
120
121bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
122 const XRaySledEntry &Sled) {
123 return patchSled(Enable, FuncId, Sled, __xray_FunctionEntry);
124}
125
126bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
127 const XRaySledEntry &Sled) {
128 return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
129}
130
131} // namespace __xray