xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/xray/xray_arm.cpp (revision 68d75eff68281c1b445e3010bb975eae07aac225)
1*68d75effSDimitry Andric //===-- xray_arm.cpp --------------------------------------------*- C++ -*-===//
2*68d75effSDimitry Andric //
3*68d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*68d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*68d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*68d75effSDimitry Andric //
7*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
8*68d75effSDimitry Andric //
9*68d75effSDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system.
10*68d75effSDimitry Andric //
11*68d75effSDimitry Andric // Implementation of ARM-specific routines (32-bit).
12*68d75effSDimitry Andric //
13*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
14*68d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
15*68d75effSDimitry Andric #include "xray_defs.h"
16*68d75effSDimitry Andric #include "xray_interface_internal.h"
17*68d75effSDimitry Andric #include <atomic>
18*68d75effSDimitry Andric #include <cassert>
19*68d75effSDimitry Andric 
20*68d75effSDimitry Andric extern "C" void __clear_cache(void *start, void *end);
21*68d75effSDimitry Andric 
22*68d75effSDimitry Andric namespace __xray {
23*68d75effSDimitry Andric 
24*68d75effSDimitry Andric // The machine codes for some instructions used in runtime patching.
25*68d75effSDimitry Andric enum class PatchOpcodes : uint32_t {
26*68d75effSDimitry Andric   PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr}
27*68d75effSDimitry Andric   PO_BlxIp = 0xE12FFF3C,    // BLX ip
28*68d75effSDimitry Andric   PO_PopR0Lr = 0xE8BD4001,  // POP {r0, lr}
29*68d75effSDimitry Andric   PO_B20 = 0xEA000005       // B #20
30*68d75effSDimitry Andric };
31*68d75effSDimitry Andric 
32*68d75effSDimitry Andric // 0xUUUUWXYZ -> 0x000W0XYZ
33*68d75effSDimitry Andric inline static uint32_t getMovwMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
34*68d75effSDimitry Andric   return (Value & 0xfff) | ((Value & 0xf000) << 4);
35*68d75effSDimitry Andric }
36*68d75effSDimitry Andric 
37*68d75effSDimitry Andric // 0xWXYZUUUU -> 0x000W0XYZ
38*68d75effSDimitry Andric inline static uint32_t getMovtMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT {
39*68d75effSDimitry Andric   return getMovwMask(Value >> 16);
40*68d75effSDimitry Andric }
41*68d75effSDimitry Andric 
42*68d75effSDimitry Andric // Writes the following instructions:
43*68d75effSDimitry Andric //   MOVW R<regNo>, #<lower 16 bits of the |Value|>
44*68d75effSDimitry Andric //   MOVT R<regNo>, #<higher 16 bits of the |Value|>
45*68d75effSDimitry Andric inline static uint32_t *
46*68d75effSDimitry Andric write32bitLoadReg(uint8_t regNo, uint32_t *Address,
47*68d75effSDimitry Andric                   const uint32_t Value) XRAY_NEVER_INSTRUMENT {
48*68d75effSDimitry Andric   // This is a fatal error: we cannot just report it and continue execution.
49*68d75effSDimitry Andric   assert(regNo <= 15 && "Register number must be 0 to 15.");
50*68d75effSDimitry Andric   // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ
51*68d75effSDimitry Andric   *Address = (0xE3000000 | (uint32_t(regNo) << 12) | getMovwMask(Value));
52*68d75effSDimitry Andric   Address++;
53*68d75effSDimitry Andric   // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ
54*68d75effSDimitry Andric   *Address = (0xE3400000 | (uint32_t(regNo) << 12) | getMovtMask(Value));
55*68d75effSDimitry Andric   return Address + 1;
56*68d75effSDimitry Andric }
57*68d75effSDimitry Andric 
58*68d75effSDimitry Andric // Writes the following instructions:
59*68d75effSDimitry Andric //   MOVW r0, #<lower 16 bits of the |Value|>
60*68d75effSDimitry Andric //   MOVT r0, #<higher 16 bits of the |Value|>
61*68d75effSDimitry Andric inline static uint32_t *
62*68d75effSDimitry Andric write32bitLoadR0(uint32_t *Address,
63*68d75effSDimitry Andric                  const uint32_t Value) XRAY_NEVER_INSTRUMENT {
64*68d75effSDimitry Andric   return write32bitLoadReg(0, Address, Value);
65*68d75effSDimitry Andric }
66*68d75effSDimitry Andric 
67*68d75effSDimitry Andric // Writes the following instructions:
68*68d75effSDimitry Andric //   MOVW ip, #<lower 16 bits of the |Value|>
69*68d75effSDimitry Andric //   MOVT ip, #<higher 16 bits of the |Value|>
70*68d75effSDimitry Andric inline static uint32_t *
71*68d75effSDimitry Andric write32bitLoadIP(uint32_t *Address,
72*68d75effSDimitry Andric                  const uint32_t Value) XRAY_NEVER_INSTRUMENT {
73*68d75effSDimitry Andric   return write32bitLoadReg(12, Address, Value);
74*68d75effSDimitry Andric }
75*68d75effSDimitry Andric 
76*68d75effSDimitry Andric inline static bool patchSled(const bool Enable, const uint32_t FuncId,
77*68d75effSDimitry Andric                              const XRaySledEntry &Sled,
78*68d75effSDimitry Andric                              void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
79*68d75effSDimitry Andric   // When |Enable| == true,
80*68d75effSDimitry Andric   // We replace the following compile-time stub (sled):
81*68d75effSDimitry Andric   //
82*68d75effSDimitry Andric   // xray_sled_n:
83*68d75effSDimitry Andric   //   B #20
84*68d75effSDimitry Andric   //   6 NOPs (24 bytes)
85*68d75effSDimitry Andric   //
86*68d75effSDimitry Andric   // With the following runtime patch:
87*68d75effSDimitry Andric   //
88*68d75effSDimitry Andric   // xray_sled_n:
89*68d75effSDimitry Andric   //   PUSH {r0, lr}
90*68d75effSDimitry Andric   //   MOVW r0, #<lower 16 bits of function ID>
91*68d75effSDimitry Andric   //   MOVT r0, #<higher 16 bits of function ID>
92*68d75effSDimitry Andric   //   MOVW ip, #<lower 16 bits of address of TracingHook>
93*68d75effSDimitry Andric   //   MOVT ip, #<higher 16 bits of address of TracingHook>
94*68d75effSDimitry Andric   //   BLX ip
95*68d75effSDimitry Andric   //   POP {r0, lr}
96*68d75effSDimitry Andric   //
97*68d75effSDimitry Andric   // Replacement of the first 4-byte instruction should be the last and atomic
98*68d75effSDimitry Andric   // operation, so that the user code which reaches the sled concurrently
99*68d75effSDimitry Andric   // either jumps over the whole sled, or executes the whole sled when the
100*68d75effSDimitry Andric   // latter is ready.
101*68d75effSDimitry Andric   //
102*68d75effSDimitry Andric   // When |Enable|==false, we set back the first instruction in the sled to be
103*68d75effSDimitry Andric   //   B #20
104*68d75effSDimitry Andric 
105*68d75effSDimitry Andric   uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address);
106*68d75effSDimitry Andric   uint32_t *CurAddress = FirstAddress + 1;
107*68d75effSDimitry Andric   if (Enable) {
108*68d75effSDimitry Andric     CurAddress =
109*68d75effSDimitry Andric         write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId));
110*68d75effSDimitry Andric     CurAddress =
111*68d75effSDimitry Andric         write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook));
112*68d75effSDimitry Andric     *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp);
113*68d75effSDimitry Andric     CurAddress++;
114*68d75effSDimitry Andric     *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr);
115*68d75effSDimitry Andric     CurAddress++;
116*68d75effSDimitry Andric     std::atomic_store_explicit(
117*68d75effSDimitry Andric         reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
118*68d75effSDimitry Andric         uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release);
119*68d75effSDimitry Andric   } else {
120*68d75effSDimitry Andric     std::atomic_store_explicit(
121*68d75effSDimitry Andric         reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress),
122*68d75effSDimitry Andric         uint32_t(PatchOpcodes::PO_B20), std::memory_order_release);
123*68d75effSDimitry Andric   }
124*68d75effSDimitry Andric   __clear_cache(reinterpret_cast<char *>(FirstAddress),
125*68d75effSDimitry Andric                 reinterpret_cast<char *>(CurAddress));
126*68d75effSDimitry Andric   return true;
127*68d75effSDimitry Andric }
128*68d75effSDimitry Andric 
129*68d75effSDimitry Andric bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
130*68d75effSDimitry Andric                         const XRaySledEntry &Sled,
131*68d75effSDimitry Andric                         void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
132*68d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, Trampoline);
133*68d75effSDimitry Andric }
134*68d75effSDimitry Andric 
135*68d75effSDimitry Andric bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
136*68d75effSDimitry Andric                        const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
137*68d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
138*68d75effSDimitry Andric }
139*68d75effSDimitry Andric 
140*68d75effSDimitry Andric bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
141*68d75effSDimitry Andric                            const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
142*68d75effSDimitry Andric   return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
143*68d75effSDimitry Andric }
144*68d75effSDimitry Andric 
145*68d75effSDimitry Andric bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
146*68d75effSDimitry Andric                       const XRaySledEntry &Sled)
147*68d75effSDimitry Andric     XRAY_NEVER_INSTRUMENT { // FIXME: Implement in arm?
148*68d75effSDimitry Andric   return false;
149*68d75effSDimitry Andric }
150*68d75effSDimitry Andric 
151*68d75effSDimitry Andric bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
152*68d75effSDimitry Andric                      const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
153*68d75effSDimitry Andric   // FIXME: Implement in arm?
154*68d75effSDimitry Andric   return false;
155*68d75effSDimitry Andric }
156*68d75effSDimitry Andric 
157*68d75effSDimitry Andric // FIXME: Maybe implement this better?
158*68d75effSDimitry Andric bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
159*68d75effSDimitry Andric 
160*68d75effSDimitry Andric } // namespace __xray
161*68d75effSDimitry Andric 
162*68d75effSDimitry Andric extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
163*68d75effSDimitry Andric   // FIXME: this will have to be implemented in the trampoline assembly file
164*68d75effSDimitry Andric }
165