blob: 26d673ec23a019e0e6902d141eba44f9a4c03a21 [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
Serge Rogatch9bce1e72017-01-19 20:27:11 +000021extern "C" void __clear_cache(void* start, void* end);
22
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000023namespace __xray {
24
25// The machine codes for some instructions used in runtime patching.
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000026enum class PatchOpcodes : uint32_t {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000027 PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr}
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000028 PO_BlxIp = 0xE12FFF3C, // BLX ip
29 PO_PopR0Lr = 0xE8BD4001, // POP {r0, lr}
30 PO_B20 = 0xEA000005 // B #20
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000031};
32
33// 0xUUUUWXYZ -> 0x000W0XYZ
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000034inline static uint32_t getMovwMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000035 return (Value & 0xfff) | ((Value & 0xf000) << 4);
36}
37
38// 0xWXYZUUUU -> 0x000W0XYZ
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000039inline static uint32_t getMovtMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000040 return getMovwMask(Value >> 16);
41}
42
43// Writes the following instructions:
44// MOVW R<regNo>, #<lower 16 bits of the |Value|>
45// MOVT R<regNo>, #<higher 16 bits of the |Value|>
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000046inline static uint32_t *
47write32bitLoadReg(uint8_t regNo, uint32_t *Address,
48 const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000049 // This is a fatal error: we cannot just report it and continue execution.
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000050 assert(regNo <= 15 && "Register number must be 0 to 15.");
51 // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000052 *Address = (0xE3000000 | (uint32_t(regNo) << 12) | getMovwMask(Value));
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000053 Address++;
54 // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ
Dean Michael Berris4ef1a692016-10-06 07:09:40 +000055 *Address = (0xE3400000 | (uint32_t(regNo) << 12) | getMovtMask(Value));
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000056 return Address + 1;
57}
58
59// Writes the following instructions:
60// MOVW r0, #<lower 16 bits of the |Value|>
61// MOVT r0, #<higher 16 bits of the |Value|>
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000062inline static uint32_t *
Dean Michael Berrisea9042c2017-02-07 23:35:34 +000063write32bitLoadR0(uint32_t *Address,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000064 const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000065 return write32bitLoadReg(0, Address, Value);
66}
67
68// Writes the following instructions:
69// MOVW ip, #<lower 16 bits of the |Value|>
70// MOVT ip, #<higher 16 bits of the |Value|>
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000071inline static uint32_t *
Dean Michael Berrisea9042c2017-02-07 23:35:34 +000072write32bitLoadIP(uint32_t *Address,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000073 const uint32_t Value) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000074 return write32bitLoadReg(12, Address, Value);
75}
76
77inline static bool patchSled(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +000078 const XRaySledEntry &Sled,
79 void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +000080 // When |Enable| == true,
81 // We replace the following compile-time stub (sled):
82 //
83 // xray_sled_n:
84 // B #20
85 // 6 NOPs (24 bytes)
86 //
87 // With the following runtime patch:
88 //
89 // xray_sled_n:
90 // PUSH {r0, lr}
91 // MOVW r0, #<lower 16 bits of function ID>
92 // MOVT r0, #<higher 16 bits of function ID>
93 // MOVW ip, #<lower 16 bits of address of TracingHook>
94 // MOVT ip, #<higher 16 bits of address of TracingHook>
95 // BLX ip
96 // POP {r0, lr}
97 //
98 // Replacement of the first 4-byte instruction should be the last and atomic
99 // operation, so that the user code which reaches the sled concurrently
100 // either jumps over the whole sled, or executes the whole sled when the
101 // latter is ready.
102 //
103 // When |Enable|==false, we set back the first instruction in the sled to be
104 // B #20
105
106 uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address);
Serge Rogatch9bce1e72017-01-19 20:27:11 +0000107 uint32_t *CurAddress = FirstAddress + 1;
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000108 if (Enable) {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000109 CurAddress =
Dean Michael Berrisea9042c2017-02-07 23:35:34 +0000110 write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId));
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000111 CurAddress =
Dean Michael Berrisea9042c2017-02-07 23:35:34 +0000112 write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook));
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000113 *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp);
114 CurAddress++;
115 *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr);
Serge Rogatch9bce1e72017-01-19 20:27:11 +0000116 CurAddress++;
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000117 std::atomic_store_explicit(
118 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
119 uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release);
120 } else {
121 std::atomic_store_explicit(
122 reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
123 uint32_t(PatchOpcodes::PO_B20), std::memory_order_release);
124 }
Serge Rogatch9bce1e72017-01-19 20:27:11 +0000125 __clear_cache(reinterpret_cast<char*>(FirstAddress),
126 reinterpret_cast<char*>(CurAddress));
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000127 return true;
128}
129
130bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
Dean Michael Berrisa814c942017-03-06 07:25:41 +0000131 const XRaySledEntry &Sled,
132 void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
133 return patchSled(Enable, FuncId, Sled, Trampoline);
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000134}
135
136bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +0000137 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000138 return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
139}
140
Dean Michael Berris1b09aae2016-10-13 23:56:54 +0000141bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
Dean Michael Berris4031e4b2016-11-16 01:01:13 +0000142 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
Serge Rogatchc4540b32017-01-26 16:18:13 +0000143 return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
Dean Michael Berris1b09aae2016-10-13 23:56:54 +0000144}
145
Dean Michael Berris607617b2017-02-02 07:51:21 +0000146// FIXME: Maybe implement this better?
147bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
148
Dean Michael Berrisd1617cd2016-09-20 14:35:57 +0000149} // namespace __xray
Dean Michael Berrisa814c942017-03-06 07:25:41 +0000150
151extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
152 // FIXME: this will have to be implemented in the trampoline assembly file
153}