xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/xray/xray_powerpc64.cpp (revision 68d75eff68281c1b445e3010bb975eae07aac225)
1*68d75effSDimitry Andric //===-- xray_powerpc64.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 powerpc64 and powerpc64le routines.
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 "xray_utils.h"
18*68d75effSDimitry Andric #include <atomic>
19*68d75effSDimitry Andric #include <cassert>
20*68d75effSDimitry Andric #include <cstring>
21*68d75effSDimitry Andric 
22*68d75effSDimitry Andric #ifndef __LITTLE_ENDIAN__
23*68d75effSDimitry Andric #error powerpc64 big endian is not supported for now.
24*68d75effSDimitry Andric #endif
25*68d75effSDimitry Andric 
26*68d75effSDimitry Andric namespace {
27*68d75effSDimitry Andric 
28*68d75effSDimitry Andric constexpr unsigned long long JumpOverInstNum = 7;
29*68d75effSDimitry Andric 
30*68d75effSDimitry Andric void clearCache(void *Addr, size_t Len) {
31*68d75effSDimitry Andric   const size_t LineSize = 32;
32*68d75effSDimitry Andric 
33*68d75effSDimitry Andric   const intptr_t Mask = ~(LineSize - 1);
34*68d75effSDimitry Andric   const intptr_t StartLine = ((intptr_t)Addr) & Mask;
35*68d75effSDimitry Andric   const intptr_t EndLine = ((intptr_t)Addr + Len + LineSize - 1) & Mask;
36*68d75effSDimitry Andric 
37*68d75effSDimitry Andric   for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
38*68d75effSDimitry Andric     asm volatile("dcbf 0, %0" : : "r"(Line));
39*68d75effSDimitry Andric   asm volatile("sync");
40*68d75effSDimitry Andric 
41*68d75effSDimitry Andric   for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
42*68d75effSDimitry Andric     asm volatile("icbi 0, %0" : : "r"(Line));
43*68d75effSDimitry Andric   asm volatile("isync");
44*68d75effSDimitry Andric }
45*68d75effSDimitry Andric 
46*68d75effSDimitry Andric } // namespace
47*68d75effSDimitry Andric 
48*68d75effSDimitry Andric extern "C" void __clear_cache(void *start, void *end);
49*68d75effSDimitry Andric 
50*68d75effSDimitry Andric namespace __xray {
51*68d75effSDimitry Andric 
52*68d75effSDimitry Andric bool patchFunctionEntry(const bool Enable, uint32_t FuncId,
53*68d75effSDimitry Andric                         const XRaySledEntry &Sled,
54*68d75effSDimitry Andric                         void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
55*68d75effSDimitry Andric   if (Enable) {
56*68d75effSDimitry Andric     // lis 0, FuncId[16..32]
57*68d75effSDimitry Andric     // li 0, FuncId[0..15]
58*68d75effSDimitry Andric     *reinterpret_cast<uint64_t *>(Sled.Address) =
59*68d75effSDimitry Andric         (0x3c000000ull + (FuncId >> 16)) +
60*68d75effSDimitry Andric         ((0x60000000ull + (FuncId & 0xffff)) << 32);
61*68d75effSDimitry Andric   } else {
62*68d75effSDimitry Andric     // b +JumpOverInstNum instructions.
63*68d75effSDimitry Andric     *reinterpret_cast<uint32_t *>(Sled.Address) =
64*68d75effSDimitry Andric         0x48000000ull + (JumpOverInstNum << 2);
65*68d75effSDimitry Andric   }
66*68d75effSDimitry Andric   clearCache(reinterpret_cast<void *>(Sled.Address), 8);
67*68d75effSDimitry Andric   return true;
68*68d75effSDimitry Andric }
69*68d75effSDimitry Andric 
70*68d75effSDimitry Andric bool patchFunctionExit(const bool Enable, uint32_t FuncId,
71*68d75effSDimitry Andric                        const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
72*68d75effSDimitry Andric   if (Enable) {
73*68d75effSDimitry Andric     // lis 0, FuncId[16..32]
74*68d75effSDimitry Andric     // li 0, FuncId[0..15]
75*68d75effSDimitry Andric     *reinterpret_cast<uint64_t *>(Sled.Address) =
76*68d75effSDimitry Andric         (0x3c000000ull + (FuncId >> 16)) +
77*68d75effSDimitry Andric         ((0x60000000ull + (FuncId & 0xffff)) << 32);
78*68d75effSDimitry Andric   } else {
79*68d75effSDimitry Andric     // Copy the blr/b instruction after JumpOverInstNum instructions.
80*68d75effSDimitry Andric     *reinterpret_cast<uint32_t *>(Sled.Address) =
81*68d75effSDimitry Andric         *(reinterpret_cast<uint32_t *>(Sled.Address) + JumpOverInstNum);
82*68d75effSDimitry Andric   }
83*68d75effSDimitry Andric   clearCache(reinterpret_cast<void *>(Sled.Address), 8);
84*68d75effSDimitry Andric   return true;
85*68d75effSDimitry Andric }
86*68d75effSDimitry Andric 
87*68d75effSDimitry Andric bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
88*68d75effSDimitry Andric                            const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
89*68d75effSDimitry Andric   return patchFunctionExit(Enable, FuncId, Sled);
90*68d75effSDimitry Andric }
91*68d75effSDimitry Andric 
92*68d75effSDimitry Andric // FIXME: Maybe implement this better?
93*68d75effSDimitry Andric bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
94*68d75effSDimitry Andric 
95*68d75effSDimitry Andric bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
96*68d75effSDimitry Andric                       const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
97*68d75effSDimitry Andric   // FIXME: Implement in powerpc64?
98*68d75effSDimitry Andric   return false;
99*68d75effSDimitry Andric }
100*68d75effSDimitry Andric 
101*68d75effSDimitry Andric bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
102*68d75effSDimitry Andric                      const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
103*68d75effSDimitry Andric   // FIXME: Implement in powerpc64?
104*68d75effSDimitry Andric   return false;
105*68d75effSDimitry Andric }
106*68d75effSDimitry Andric 
107*68d75effSDimitry Andric } // namespace __xray
108*68d75effSDimitry Andric 
109*68d75effSDimitry Andric extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
110*68d75effSDimitry Andric   // FIXME: this will have to be implemented in the trampoline assembly file
111*68d75effSDimitry Andric }
112