1 //===-- xray_powerpc64.cpp --------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of XRay, a dynamic runtime instrumentation system.
11 // Implementation of powerpc64 and powerpc64le routines.
13 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_common.h"
15 #include "xray_defs.h"
16 #include "xray_interface_internal.h"
17 #include "xray_utils.h"
22 #ifndef __LITTLE_ENDIAN__
23 #error powerpc64 big endian is not supported for now.
28 constexpr unsigned long long JumpOverInstNum
= 7;
30 void clearCache(void *Addr
, size_t Len
) {
31 const size_t LineSize
= 32;
33 const intptr_t Mask
= ~(LineSize
- 1);
34 const intptr_t StartLine
= ((intptr_t)Addr
) & Mask
;
35 const intptr_t EndLine
= ((intptr_t)Addr
+ Len
+ LineSize
- 1) & Mask
;
37 for (intptr_t Line
= StartLine
; Line
< EndLine
; Line
+= LineSize
)
38 asm volatile("dcbf 0, %0" : : "r"(Line
));
41 for (intptr_t Line
= StartLine
; Line
< EndLine
; Line
+= LineSize
)
42 asm volatile("icbi 0, %0" : : "r"(Line
));
43 asm volatile("isync");
48 extern "C" void __clear_cache(void *start
, void *end
);
52 bool patchFunctionEntry(const bool Enable
, uint32_t FuncId
,
53 const XRaySledEntry
&Sled
,
54 void (*Trampoline
)()) XRAY_NEVER_INSTRUMENT
{
55 const uint64_t Address
= Sled
.address();
57 // lis 0, FuncId[16..32]
58 // li 0, FuncId[0..15]
59 *reinterpret_cast<uint64_t *>(Address
) =
60 (0x3c000000ull
+ (FuncId
>> 16)) +
61 ((0x60000000ull
+ (FuncId
& 0xffff)) << 32);
63 // b +JumpOverInstNum instructions.
64 *reinterpret_cast<uint32_t *>(Address
) =
65 0x48000000ull
+ (JumpOverInstNum
<< 2);
67 clearCache(reinterpret_cast<void *>(Address
), 8);
71 bool patchFunctionExit(const bool Enable
, uint32_t FuncId
,
72 const XRaySledEntry
&Sled
) XRAY_NEVER_INSTRUMENT
{
73 const uint64_t Address
= Sled
.address();
75 // lis 0, FuncId[16..32]
76 // li 0, FuncId[0..15]
77 *reinterpret_cast<uint64_t *>(Address
) =
78 (0x3c000000ull
+ (FuncId
>> 16)) +
79 ((0x60000000ull
+ (FuncId
& 0xffff)) << 32);
81 // Copy the blr/b instruction after JumpOverInstNum instructions.
82 *reinterpret_cast<uint32_t *>(Address
) =
83 *(reinterpret_cast<uint32_t *>(Address
) + JumpOverInstNum
);
85 clearCache(reinterpret_cast<void *>(Address
), 8);
89 bool patchFunctionTailExit(const bool Enable
, const uint32_t FuncId
,
90 const XRaySledEntry
&Sled
) XRAY_NEVER_INSTRUMENT
{
91 return patchFunctionExit(Enable
, FuncId
, Sled
);
94 // FIXME: Maybe implement this better?
95 bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT
{ return true; }
97 bool patchCustomEvent(const bool Enable
, const uint32_t FuncId
,
98 const XRaySledEntry
&Sled
) XRAY_NEVER_INSTRUMENT
{
99 // FIXME: Implement in powerpc64?
103 bool patchTypedEvent(const bool Enable
, const uint32_t FuncId
,
104 const XRaySledEntry
&Sled
) XRAY_NEVER_INSTRUMENT
{
105 // FIXME: Implement in powerpc64?
109 } // namespace __xray
111 extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT
{
112 // FIXME: this will have to be implemented in the trampoline assembly file