1 //===-- xray_mips.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 MIPS-specific routines (32-bit).
13 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_common.h"
15 #include "xray_defs.h"
16 #include "xray_interface_internal.h"
21 // The machine codes for some instructions used in runtime patching.
22 enum PatchOpcodes
: uint32_t {
23 PO_ADDIU
= 0x24000000, // addiu rt, rs, imm
24 PO_SW
= 0xAC000000, // sw rt, offset(sp)
25 PO_LUI
= 0x3C000000, // lui rs, %hi(address)
26 PO_ORI
= 0x34000000, // ori rt, rs, %lo(address)
27 PO_JALR
= 0x0000F809, // jalr rs
28 PO_LW
= 0x8C000000, // lw rt, offset(address)
29 PO_B44
= 0x1000000b, // b #44
33 enum RegNum
: uint32_t {
40 inline static uint32_t encodeInstruction(uint32_t Opcode
, uint32_t Rs
,
42 uint32_t Imm
) XRAY_NEVER_INSTRUMENT
{
43 return (Opcode
| Rs
<< 21 | Rt
<< 16 | Imm
);
46 inline static uint32_t
47 encodeSpecialInstruction(uint32_t Opcode
, uint32_t Rs
, uint32_t Rt
, uint32_t Rd
,
48 uint32_t Imm
) XRAY_NEVER_INSTRUMENT
{
49 return (Rs
<< 21 | Rt
<< 16 | Rd
<< 11 | Imm
<< 6 | Opcode
);
52 inline static bool patchSled(const bool Enable
, const uint32_t FuncId
,
53 const XRaySledEntry
&Sled
,
54 void (*TracingHook
)()) XRAY_NEVER_INSTRUMENT
{
55 // When |Enable| == true,
56 // We replace the following compile-time stub (sled):
64 // With the following runtime patch:
66 // xray_sled_n (32-bit):
67 // addiu sp, sp, -8 ;create stack frame
69 // sw ra, 4(sp) ;save return address
70 // sw t9, 0(sp) ;save register t9
71 // lui t9, %hi(__xray_FunctionEntry/Exit)
72 // ori t9, t9, %lo(__xray_FunctionEntry/Exit)
73 // lui t0, %hi(function_id)
74 // jalr t9 ;call Tracing hook
75 // ori t0, t0, %lo(function_id) ;pass function id (delay slot)
76 // lw t9, 0(sp) ;restore register t9
77 // lw ra, 4(sp) ;restore return address
78 // addiu sp, sp, 8 ;delete stack frame
80 // We add 44 bytes to t9 because we want to adjust the function pointer to
81 // the actual start of function i.e. the address just after the noop sled.
82 // We do this because gp displacement relocation is emitted at the start of
83 // of the function i.e after the nop sled and to correctly calculate the
84 // global offset table address, t9 must hold the address of the instruction
85 // containing the gp displacement relocation.
86 // FIXME: Is this correct for the static relocation model?
88 // Replacement of the first 4-byte instruction should be the last and atomic
89 // operation, so that the user code which reaches the sled concurrently
90 // either jumps over the whole sled, or executes the whole sled when the
93 // When |Enable|==false, we set back the first instruction in the sled to be
96 uint32_t *Address
= reinterpret_cast<uint32_t *>(Sled
.address());
98 uint32_t LoTracingHookAddr
=
99 reinterpret_cast<int32_t>(TracingHook
) & 0xffff;
100 uint32_t HiTracingHookAddr
=
101 (reinterpret_cast<int32_t>(TracingHook
) >> 16) & 0xffff;
102 uint32_t LoFunctionID
= FuncId
& 0xffff;
103 uint32_t HiFunctionID
= (FuncId
>> 16) & 0xffff;
104 Address
[2] = encodeInstruction(PatchOpcodes::PO_SW
, RegNum::RN_SP
,
106 Address
[3] = encodeInstruction(PatchOpcodes::PO_SW
, RegNum::RN_SP
,
108 Address
[4] = encodeInstruction(PatchOpcodes::PO_LUI
, 0x0, RegNum::RN_T9
,
110 Address
[5] = encodeInstruction(PatchOpcodes::PO_ORI
, RegNum::RN_T9
,
111 RegNum::RN_T9
, LoTracingHookAddr
);
112 Address
[6] = encodeInstruction(PatchOpcodes::PO_LUI
, 0x0, RegNum::RN_T0
,
114 Address
[7] = encodeSpecialInstruction(PatchOpcodes::PO_JALR
, RegNum::RN_T9
,
115 0x0, RegNum::RN_RA
, 0X0);
116 Address
[8] = encodeInstruction(PatchOpcodes::PO_ORI
, RegNum::RN_T0
,
117 RegNum::RN_T0
, LoFunctionID
);
118 Address
[9] = encodeInstruction(PatchOpcodes::PO_LW
, RegNum::RN_SP
,
120 Address
[10] = encodeInstruction(PatchOpcodes::PO_LW
, RegNum::RN_SP
,
122 Address
[11] = encodeInstruction(PatchOpcodes::PO_ADDIU
, RegNum::RN_SP
,
124 uint32_t CreateStackSpaceInstr
= encodeInstruction(
125 PatchOpcodes::PO_ADDIU
, RegNum::RN_SP
, RegNum::RN_SP
, 0xFFF8);
126 std::atomic_store_explicit(
127 reinterpret_cast<std::atomic
<uint32_t> *>(Address
),
128 uint32_t(CreateStackSpaceInstr
), std::memory_order_release
);
130 std::atomic_store_explicit(
131 reinterpret_cast<std::atomic
<uint32_t> *>(Address
),
132 uint32_t(PatchOpcodes::PO_B44
), std::memory_order_release
);
137 bool patchFunctionEntry(const bool Enable
, const uint32_t FuncId
,
138 const XRaySledEntry
&Sled
,
139 void (*Trampoline
)()) XRAY_NEVER_INSTRUMENT
{
140 return patchSled(Enable
, FuncId
, Sled
, Trampoline
);
143 bool patchFunctionExit(const bool Enable
, const uint32_t FuncId
,
144 const XRaySledEntry
&Sled
) XRAY_NEVER_INSTRUMENT
{
145 return patchSled(Enable
, FuncId
, Sled
, __xray_FunctionExit
);
148 bool patchFunctionTailExit(const bool Enable
, const uint32_t FuncId
,
149 const XRaySledEntry
&Sled
) XRAY_NEVER_INSTRUMENT
{
150 // FIXME: In the future we'd need to distinguish between non-tail exits and
151 // tail exits for better information preservation.
152 return patchSled(Enable
, FuncId
, Sled
, __xray_FunctionExit
);
155 bool patchCustomEvent(const bool Enable
, const uint32_t FuncId
,
156 const XRaySledEntry
&Sled
) XRAY_NEVER_INSTRUMENT
{
157 // FIXME: Implement in mips?
161 bool patchTypedEvent(const bool Enable
, const uint32_t FuncId
,
162 const XRaySledEntry
&Sled
) XRAY_NEVER_INSTRUMENT
{
163 // FIXME: Implement in mips?
167 } // namespace __xray
169 extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT
{
170 // FIXME: this will have to be implemented in the trampoline assembly file