blob: 53e26f8d89e6ab04e692d852be7233d378922e62 [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//===----------------------------------------------------------------------===//
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000015#include "sanitizer_common/sanitizer_common.h"
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000016#include "xray_interface_internal.h"
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000017#include <atomic>
18#include <cassert>
19
20namespace __xray {
21
22// The machine codes for some instructions used in runtime patching.
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000023enum class PatchOpcodes : uint32_t {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000024 PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr}
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000025 PO_BlxIp = 0xE12FFF3C, // BLX ip
26 PO_PopR0Lr = 0xE8BD4001, // POP {r0, lr}
27 PO_B20 = 0xEA000005 // B #20
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000028};
29
30// 0xUUUUWXYZ -> 0x000W0XYZ
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000031inline static uint32_t getMovwMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000032 return (Value & 0xfff) | ((Value & 0xf000) << 4);
33}
34
35// 0xWXYZUUUU -> 0x000W0XYZ
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000036inline static uint32_t getMovtMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000037 return getMovwMask(Value >> 16);
38}
39
40// Writes the following instructions:
41// MOVW R<regNo>, #<lower 16 bits of the |Value|>
42// MOVT R<regNo>, #<higher 16 bits of the |Value|>
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000043inline static uint32_t *
44write32bitLoadReg(uint8_t regNo, uint32_t *Address,
45 const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000046 // This is a fatal error: we cannot just report it and continue execution.
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000047 assert(regNo <= 15 && "Register number must be 0 to 15.");
48 // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000049 *Address = (0xE3000000 | (uint32_t(regNo) << 12) | getMovwMask(Value));
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000050 Address++;
51 // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000052 *Address = (0xE3400000 | (uint32_t(regNo) << 12) | getMovtMask(Value));
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000053 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|>
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000059inline static uint32_t *
60Write32bitLoadR0(uint32_t *Address,
61 const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000062 return write32bitLoadReg(0, Address, Value);
63}
64
65// Writes the following instructions:
66// MOVW ip, #<lower 16 bits of the |Value|>
67// MOVT ip, #<higher 16 bits of the |Value|>
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000068inline static uint32_t *
69Write32bitLoadIP(uint32_t *Address,
70 const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000071 return write32bitLoadReg(12, Address, Value);
72}
73
74inline static bool patchSled(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000075 const XRaySledEntry &Sled,
76 void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000077 // When |Enable| == true,
78 // We replace the following compile-time stub (sled):
79 //
80 // xray_sled_n:
81 // B #20
82 // 6 NOPs (24 bytes)
83 //
84 // With the following runtime patch:
85 //
86 // xray_sled_n:
87 // PUSH {r0, lr}
88 // MOVW r0, #<lower 16 bits of function ID>
89 // MOVT r0, #<higher 16 bits of function ID>
90 // MOVW ip, #<lower 16 bits of address of TracingHook>
91 // MOVT ip, #<higher 16 bits of address of TracingHook>
92 // BLX ip
93 // POP {r0, lr}
94 //
95 // Replacement of the first 4-byte instruction should be the last and atomic
96 // operation, so that the user code which reaches the sled concurrently
97 // either jumps over the whole sled, or executes the whole sled when the
98 // latter is ready.
99 //
100 // When |Enable|==false, we set back the first instruction in the sled to be
101 // B #20
102
103 uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address);
104 if (Enable) {
105 uint32_t *CurAddress = FirstAddress + 1;
106 CurAddress =
107 Write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId));
108 CurAddress =
109 Write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook));
110 *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp);
111 CurAddress++;
112 *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr);
113 std::atomic_store_explicit(
114 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
115 uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release);
116 } else {
117 std::atomic_store_explicit(
118 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
119 uint32_t(PatchOpcodes::PO_B20), std::memory_order_release);
120 }
121 return true;
122}
123
124bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +0000125 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000126 return patchSled(Enable, FuncId, Sled, __xray_FunctionEntry);
127}
128
129bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +0000130 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000131 return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
132}
133
Dean Michael Berris1b09aae2016-10-13 23:56:54 +0000134bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +0000135 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
Dean Michael Berris1b09aae2016-10-13 23:56:54 +0000136 // FIXME: In the future we'd need to distinguish between non-tail exits and
137 // tail exits for better information preservation.
138 return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
139}
140
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000141} // namespace __xray