1*06c3fb27SDimitry Andric //===-------- xray_loongarch64.cpp ------------------------------*- C++ -*-===//
2*06c3fb27SDimitry Andric //
3*06c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*06c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*06c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*06c3fb27SDimitry Andric //
7*06c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
8*06c3fb27SDimitry Andric //
9*06c3fb27SDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system.
10*06c3fb27SDimitry Andric //
11*06c3fb27SDimitry Andric // Implementation of loongarch-specific routines.
12*06c3fb27SDimitry Andric //
13*06c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
14*06c3fb27SDimitry Andric #include "sanitizer_common/sanitizer_common.h"
15*06c3fb27SDimitry Andric #include "xray_defs.h"
16*06c3fb27SDimitry Andric #include "xray_interface_internal.h"
17*06c3fb27SDimitry Andric #include <atomic>
18*06c3fb27SDimitry Andric
19*06c3fb27SDimitry Andric namespace __xray {
20*06c3fb27SDimitry Andric
21*06c3fb27SDimitry Andric enum RegNum : uint32_t {
22*06c3fb27SDimitry Andric RN_RA = 1,
23*06c3fb27SDimitry Andric RN_SP = 3,
24*06c3fb27SDimitry Andric RN_T0 = 12,
25*06c3fb27SDimitry Andric RN_T1 = 13,
26*06c3fb27SDimitry Andric };
27*06c3fb27SDimitry Andric
28*06c3fb27SDimitry Andric // Encode instructions in the 2RIx format, where the primary formats here
29*06c3fb27SDimitry Andric // are 2RI12-type and 2RI16-type.
30*06c3fb27SDimitry Andric static inline uint32_t
encodeInstruction2RIx(uint32_t Opcode,uint32_t Rd,uint32_t Rj,uint32_t Imm)31*06c3fb27SDimitry Andric encodeInstruction2RIx(uint32_t Opcode, uint32_t Rd, uint32_t Rj,
32*06c3fb27SDimitry Andric uint32_t Imm) XRAY_NEVER_INSTRUMENT {
33*06c3fb27SDimitry Andric return Opcode | (Imm << 10) | (Rj << 5) | Rd;
34*06c3fb27SDimitry Andric }
35*06c3fb27SDimitry Andric
36*06c3fb27SDimitry Andric // Encode instructions in 1RI20 format, e.g. lu12i.w/lu32i.d.
37*06c3fb27SDimitry Andric static inline uint32_t
encodeInstruction1RI20(uint32_t Opcode,uint32_t Rd,uint32_t Imm)38*06c3fb27SDimitry Andric encodeInstruction1RI20(uint32_t Opcode, uint32_t Rd,
39*06c3fb27SDimitry Andric uint32_t Imm) XRAY_NEVER_INSTRUMENT {
40*06c3fb27SDimitry Andric return Opcode | (Imm << 5) | Rd;
41*06c3fb27SDimitry Andric }
42*06c3fb27SDimitry Andric
patchSled(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* TracingHook)())43*06c3fb27SDimitry Andric static inline bool patchSled(const bool Enable, const uint32_t FuncId,
44*06c3fb27SDimitry Andric const XRaySledEntry &Sled,
45*06c3fb27SDimitry Andric void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
46*06c3fb27SDimitry Andric // When |Enable| == true,
47*06c3fb27SDimitry Andric // We replace the following compile-time stub (sled):
48*06c3fb27SDimitry Andric //
49*06c3fb27SDimitry Andric // .Lxray_sled_beginN:
50*06c3fb27SDimitry Andric // B .Lxray_sled_endN
51*06c3fb27SDimitry Andric // 11 NOPs (44 bytes)
52*06c3fb27SDimitry Andric // .Lxray_sled_endN:
53*06c3fb27SDimitry Andric //
54*06c3fb27SDimitry Andric // With the following runtime patch:
55*06c3fb27SDimitry Andric //
56*06c3fb27SDimitry Andric // xray_sled_n:
57*06c3fb27SDimitry Andric // addi.d sp, sp, -16 ; create the stack frame
58*06c3fb27SDimitry Andric // st.d ra, sp, 8 ; save the return address
59*06c3fb27SDimitry Andric // lu12i.w t0, %abs_hi20(__xray_FunctionEntry/Exit)
60*06c3fb27SDimitry Andric // ori t0, t0, %abs_lo12(__xray_FunctionEntry/Exit)
61*06c3fb27SDimitry Andric // lu32i.d t0, %abs64_lo20(__xray_FunctionEntry/Exit)
62*06c3fb27SDimitry Andric // lu52i.d t0, t0, %abs64_hi12(__xray_FunctionEntry/Exit)
63*06c3fb27SDimitry Andric // lu12i.w t1, %abs_hi20(function_id)
64*06c3fb27SDimitry Andric // ori t1, t1, %abs_lo12(function_id) ; pass the function id
65*06c3fb27SDimitry Andric // jirl ra, t0, 0 ; call the tracing hook
66*06c3fb27SDimitry Andric // ld.d ra, sp, 8 ; restore the return address
67*06c3fb27SDimitry Andric // addi.d sp, sp, 16 ; de-allocate the stack frame
68*06c3fb27SDimitry Andric //
69*06c3fb27SDimitry Andric // Replacement of the first 4-byte instruction should be the last and atomic
70*06c3fb27SDimitry Andric // operation, so that the user code which reaches the sled concurrently
71*06c3fb27SDimitry Andric // either jumps over the whole sled, or executes the whole sled when the
72*06c3fb27SDimitry Andric // latter is ready.
73*06c3fb27SDimitry Andric //
74*06c3fb27SDimitry Andric // When |Enable|==false, we set the first instruction in the sled back to
75*06c3fb27SDimitry Andric // B #48
76*06c3fb27SDimitry Andric
77*06c3fb27SDimitry Andric uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());
78*06c3fb27SDimitry Andric if (Enable) {
79*06c3fb27SDimitry Andric uint32_t LoTracingHookAddr = reinterpret_cast<int64_t>(TracingHook) & 0xfff;
80*06c3fb27SDimitry Andric uint32_t HiTracingHookAddr =
81*06c3fb27SDimitry Andric (reinterpret_cast<int64_t>(TracingHook) >> 12) & 0xfffff;
82*06c3fb27SDimitry Andric uint32_t HigherTracingHookAddr =
83*06c3fb27SDimitry Andric (reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xfffff;
84*06c3fb27SDimitry Andric uint32_t HighestTracingHookAddr =
85*06c3fb27SDimitry Andric (reinterpret_cast<int64_t>(TracingHook) >> 52) & 0xfff;
86*06c3fb27SDimitry Andric uint32_t LoFunctionID = FuncId & 0xfff;
87*06c3fb27SDimitry Andric uint32_t HiFunctionID = (FuncId >> 12) & 0xfffff;
88*06c3fb27SDimitry Andric Address[1] = encodeInstruction2RIx(0x29c00000, RegNum::RN_RA, RegNum::RN_SP,
89*06c3fb27SDimitry Andric 0x8); // st.d ra, sp, 8
90*06c3fb27SDimitry Andric Address[2] = encodeInstruction1RI20(
91*06c3fb27SDimitry Andric 0x14000000, RegNum::RN_T0,
92*06c3fb27SDimitry Andric HiTracingHookAddr); // lu12i.w t0, HiTracingHookAddr
93*06c3fb27SDimitry Andric Address[3] = encodeInstruction2RIx(
94*06c3fb27SDimitry Andric 0x03800000, RegNum::RN_T0, RegNum::RN_T0,
95*06c3fb27SDimitry Andric LoTracingHookAddr); // ori t0, t0, LoTracingHookAddr
96*06c3fb27SDimitry Andric Address[4] = encodeInstruction1RI20(
97*06c3fb27SDimitry Andric 0x16000000, RegNum::RN_T0,
98*06c3fb27SDimitry Andric HigherTracingHookAddr); // lu32i.d t0, HigherTracingHookAddr
99*06c3fb27SDimitry Andric Address[5] = encodeInstruction2RIx(
100*06c3fb27SDimitry Andric 0x03000000, RegNum::RN_T0, RegNum::RN_T0,
101*06c3fb27SDimitry Andric HighestTracingHookAddr); // lu52i.d t0, t0, HighestTracingHookAddr
102*06c3fb27SDimitry Andric Address[6] =
103*06c3fb27SDimitry Andric encodeInstruction1RI20(0x14000000, RegNum::RN_T1,
104*06c3fb27SDimitry Andric HiFunctionID); // lu12i.w t1, HiFunctionID
105*06c3fb27SDimitry Andric Address[7] =
106*06c3fb27SDimitry Andric encodeInstruction2RIx(0x03800000, RegNum::RN_T1, RegNum::RN_T1,
107*06c3fb27SDimitry Andric LoFunctionID); // ori t1, t1, LoFunctionID
108*06c3fb27SDimitry Andric Address[8] = encodeInstruction2RIx(0x4c000000, RegNum::RN_RA, RegNum::RN_T0,
109*06c3fb27SDimitry Andric 0); // jirl ra, t0, 0
110*06c3fb27SDimitry Andric Address[9] = encodeInstruction2RIx(0x28c00000, RegNum::RN_RA, RegNum::RN_SP,
111*06c3fb27SDimitry Andric 0x8); // ld.d ra, sp, 8
112*06c3fb27SDimitry Andric Address[10] = encodeInstruction2RIx(
113*06c3fb27SDimitry Andric 0x02c00000, RegNum::RN_SP, RegNum::RN_SP, 0x10); // addi.d sp, sp, 16
114*06c3fb27SDimitry Andric uint32_t CreateStackSpace = encodeInstruction2RIx(
115*06c3fb27SDimitry Andric 0x02c00000, RegNum::RN_SP, RegNum::RN_SP, 0xff0); // addi.d sp, sp, -16
116*06c3fb27SDimitry Andric std::atomic_store_explicit(
117*06c3fb27SDimitry Andric reinterpret_cast<std::atomic<uint32_t> *>(Address), CreateStackSpace,
118*06c3fb27SDimitry Andric std::memory_order_release);
119*06c3fb27SDimitry Andric } else {
120*06c3fb27SDimitry Andric std::atomic_store_explicit(
121*06c3fb27SDimitry Andric reinterpret_cast<std::atomic<uint32_t> *>(Address),
122*06c3fb27SDimitry Andric uint32_t(0x50003000), std::memory_order_release); // b #48
123*06c3fb27SDimitry Andric }
124*06c3fb27SDimitry Andric return true;
125*06c3fb27SDimitry Andric }
126*06c3fb27SDimitry Andric
patchFunctionEntry(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled,void (* Trampoline)())127*06c3fb27SDimitry Andric bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
128*06c3fb27SDimitry Andric const XRaySledEntry &Sled,
129*06c3fb27SDimitry Andric void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
130*06c3fb27SDimitry Andric return patchSled(Enable, FuncId, Sled, Trampoline);
131*06c3fb27SDimitry Andric }
132*06c3fb27SDimitry Andric
patchFunctionExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)133*06c3fb27SDimitry Andric bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
134*06c3fb27SDimitry Andric const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
135*06c3fb27SDimitry Andric return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
136*06c3fb27SDimitry Andric }
137*06c3fb27SDimitry Andric
patchFunctionTailExit(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)138*06c3fb27SDimitry Andric bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
139*06c3fb27SDimitry Andric const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
140*06c3fb27SDimitry Andric // TODO: In the future we'd need to distinguish between non-tail exits and
141*06c3fb27SDimitry Andric // tail exits for better information preservation.
142*06c3fb27SDimitry Andric return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
143*06c3fb27SDimitry Andric }
144*06c3fb27SDimitry Andric
patchCustomEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)145*06c3fb27SDimitry Andric bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
146*06c3fb27SDimitry Andric const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
147*06c3fb27SDimitry Andric // FIXME: Implement in loongarch?
148*06c3fb27SDimitry Andric return false;
149*06c3fb27SDimitry Andric }
150*06c3fb27SDimitry Andric
patchTypedEvent(const bool Enable,const uint32_t FuncId,const XRaySledEntry & Sled)151*06c3fb27SDimitry Andric bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
152*06c3fb27SDimitry Andric const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
153*06c3fb27SDimitry Andric // FIXME: Implement in loongarch?
154*06c3fb27SDimitry Andric return false;
155*06c3fb27SDimitry Andric }
156*06c3fb27SDimitry Andric } // namespace __xray
157*06c3fb27SDimitry Andric
__xray_ArgLoggerEntry()158*06c3fb27SDimitry Andric extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
159*06c3fb27SDimitry Andric // TODO: This will have to be implemented in the trampoline assembly file.
160*06c3fb27SDimitry Andric }
161