1 //===-- xray_powerpc64.cc ---------------------------------------*- 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 {
56 // lis 0, FuncId[16..32]
57 // li 0, FuncId[0..15]
58 *reinterpret_cast<uint64_t *>(Sled.Address) =
59 (0x3c000000ull + (FuncId >> 16)) +
60 ((0x60000000ull + (FuncId & 0xffff)) << 32);
62 // b +JumpOverInstNum instructions.
63 *reinterpret_cast<uint32_t *>(Sled.Address) =
64 0x48000000ull + (JumpOverInstNum << 2);
66 clearCache(reinterpret_cast<void *>(Sled.Address), 8);
70 bool patchFunctionExit(const bool Enable, uint32_t FuncId,
71 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
73 // lis 0, FuncId[16..32]
74 // li 0, FuncId[0..15]
75 *reinterpret_cast<uint64_t *>(Sled.Address) =
76 (0x3c000000ull + (FuncId >> 16)) +
77 ((0x60000000ull + (FuncId & 0xffff)) << 32);
79 // Copy the blr/b instruction after JumpOverInstNum instructions.
80 *reinterpret_cast<uint32_t *>(Sled.Address) =
81 *(reinterpret_cast<uint32_t *>(Sled.Address) + JumpOverInstNum);
83 clearCache(reinterpret_cast<void *>(Sled.Address), 8);
87 bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
88 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
89 return patchFunctionExit(Enable, FuncId, Sled);
92 // FIXME: Maybe implement this better?
93 bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
95 bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
96 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
97 // FIXME: Implement in powerpc64?
101 bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
102 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
103 // FIXME: Implement in powerpc64?
107 } // namespace __xray
109 extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
110 // FIXME: this will have to be implemented in the trampoline assembly file