blob: 9ce8451bee84d1e2beeac7fd0b5ee626f614a507 [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"
Diana Picus87d025f2016-11-16 09:32:23 +000016#include "xray_defs.h"
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000017#include "xray_interface_internal.h"
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000018#include <atomic>
19#include <cassert>
20
21namespace __xray {
22
23// The machine codes for some instructions used in runtime patching.
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000024enum class PatchOpcodes : uint32_t {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000025 PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr}
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000026 PO_BlxIp = 0xE12FFF3C, // BLX ip
27 PO_PopR0Lr = 0xE8BD4001, // POP {r0, lr}
28 PO_B20 = 0xEA000005 // B #20
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000029};
30
31// 0xUUUUWXYZ -> 0x000W0XYZ
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000032inline static uint32_t getMovwMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000033 return (Value & 0xfff) | ((Value & 0xf000) << 4);
34}
35
36// 0xWXYZUUUU -> 0x000W0XYZ
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000037inline static uint32_t getMovtMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000038 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|>
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000044inline static uint32_t *
45write32bitLoadReg(uint8_t regNo, uint32_t *Address,
46 const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000047 // This is a fatal error: we cannot just report it and continue execution.
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000048 assert(regNo <= 15 && "Register number must be 0 to 15.");
49 // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000050 *Address = (0xE3000000 | (uint32_t(regNo) << 12) | getMovwMask(Value));
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000051 Address++;
52 // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000053 *Address = (0xE3400000 | (uint32_t(regNo) << 12) | getMovtMask(Value));
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000054 return Address + 1;
55}
56
57// Writes the following instructions:
58// MOVW r0, #<lower 16 bits of the |Value|>
59// MOVT r0, #<higher 16 bits of the |Value|>
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000060inline static uint32_t *
61Write32bitLoadR0(uint32_t *Address,
62 const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000063 return write32bitLoadReg(0, Address, Value);
64}
65
66// Writes the following instructions:
67// MOVW ip, #<lower 16 bits of the |Value|>
68// MOVT ip, #<higher 16 bits of the |Value|>
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000069inline static uint32_t *
70Write32bitLoadIP(uint32_t *Address,
71 const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000072 return write32bitLoadReg(12, Address, Value);
73}
74
75inline static bool patchSled(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000076 const XRaySledEntry &Sled,
77 void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000078 // When |Enable| == true,
79 // We replace the following compile-time stub (sled):
80 //
81 // xray_sled_n:
82 // B #20
83 // 6 NOPs (24 bytes)
84 //
85 // With the following runtime patch:
86 //
87 // xray_sled_n:
88 // PUSH {r0, lr}
89 // MOVW r0, #<lower 16 bits of function ID>
90 // MOVT r0, #<higher 16 bits of function ID>
91 // MOVW ip, #<lower 16 bits of address of TracingHook>
92 // MOVT ip, #<higher 16 bits of address of TracingHook>
93 // BLX ip
94 // POP {r0, lr}
95 //
96 // Replacement of the first 4-byte instruction should be the last and atomic
97 // operation, so that the user code which reaches the sled concurrently
98 // either jumps over the whole sled, or executes the whole sled when the
99 // latter is ready.
100 //
101 // When |Enable|==false, we set back the first instruction in the sled to be
102 // B #20
103
104 uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address);
105 if (Enable) {
106 uint32_t *CurAddress = FirstAddress + 1;
107 CurAddress =
108 Write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId));
109 CurAddress =
110 Write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook));
111 *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp);
112 CurAddress++;
113 *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr);
114 std::atomic_store_explicit(
115 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
116 uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release);
117 } else {
118 std::atomic_store_explicit(
119 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
120 uint32_t(PatchOpcodes::PO_B20), std::memory_order_release);
121 }
122 return true;
123}
124
125bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +0000126 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000127 return patchSled(Enable, FuncId, Sled, __xray_FunctionEntry);
128}
129
130bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +0000131 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000132 return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
133}
134
Dean Michael Berris1b09aae2016-10-13 23:56:54 +0000135bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +0000136 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
Dean Michael Berris1b09aae2016-10-13 23:56:54 +0000137 // FIXME: In the future we'd need to distinguish between non-tail exits and
138 // tail exits for better information preservation.
139 return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
140}
141
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000142} // namespace __xray