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

source code of compiler-rt/lib/xray/xray_loongarch64.cpp