]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_mips.cc
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_mips.cc
1 //===-- xray_mips.cc --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
11 //
12 // Implementation of MIPS-specific routines (32-bit).
13 //
14 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "xray_defs.h"
17 #include "xray_interface_internal.h"
18 #include <atomic>
19
20 namespace __xray {
21
22 // The machine codes for some instructions used in runtime patching.
23 enum PatchOpcodes : uint32_t {
24   PO_ADDIU = 0x24000000, // addiu rt, rs, imm
25   PO_SW = 0xAC000000,    // sw rt, offset(sp)
26   PO_LUI = 0x3C000000,   // lui rs, %hi(address)
27   PO_ORI = 0x34000000,   // ori rt, rs, %lo(address)
28   PO_JALR = 0x0000F809,  // jalr rs
29   PO_LW = 0x8C000000,    // lw rt, offset(address)
30   PO_B44 = 0x1000000b,   // b #44
31   PO_NOP = 0x0,          // nop
32 };
33
34 enum RegNum : uint32_t {
35   RN_T0 = 0x8,
36   RN_T9 = 0x19,
37   RN_RA = 0x1F,
38   RN_SP = 0x1D,
39 };
40
41 inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs,
42                                          uint32_t Rt,
43                                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
44   return (Opcode | Rs << 21 | Rt << 16 | Imm);
45 }
46
47 inline static uint32_t
48 encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd,
49                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
50   return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode);
51 }
52
53 inline static bool patchSled(const bool Enable, const uint32_t FuncId,
54                              const XRaySledEntry &Sled,
55                              void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
56   // When |Enable| == true,
57   // We replace the following compile-time stub (sled):
58   //
59   // xray_sled_n:
60   //    B .tmpN
61   //    11 NOPs (44 bytes)
62   //    .tmpN
63   //    ADDIU T9, T9, 44
64   //
65   // With the following runtime patch:
66   //
67   // xray_sled_n (32-bit):
68   //    addiu sp, sp, -8                        ;create stack frame
69   //    nop
70   //    sw ra, 4(sp)                            ;save return address
71   //    sw t9, 0(sp)                            ;save register t9
72   //    lui t9, %hi(__xray_FunctionEntry/Exit)
73   //    ori t9, t9, %lo(__xray_FunctionEntry/Exit)
74   //    lui t0, %hi(function_id)
75   //    jalr t9                                 ;call Tracing hook
76   //    ori t0, t0, %lo(function_id)            ;pass function id (delay slot)
77   //    lw t9, 0(sp)                            ;restore register t9
78   //    lw ra, 4(sp)                            ;restore return address
79   //    addiu sp, sp, 8                         ;delete stack frame
80   //
81   // We add 44 bytes to t9 because we want to adjust the function pointer to
82   // the actual start of function i.e. the address just after the noop sled.
83   // We do this because gp displacement relocation is emitted at the start of
84   // of the function i.e after the nop sled and to correctly calculate the
85   // global offset table address, t9 must hold the address of the instruction
86   // containing the gp displacement relocation.
87   // FIXME: Is this correct for the static relocation model?
88   //
89   // Replacement of the first 4-byte instruction should be the last and atomic
90   // operation, so that the user code which reaches the sled concurrently
91   // either jumps over the whole sled, or executes the whole sled when the
92   // latter is ready.
93   //
94   // When |Enable|==false, we set back the first instruction in the sled to be
95   //   B #44
96
97   if (Enable) {
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     *reinterpret_cast<uint32_t *>(Sled.Address + 8) = encodeInstruction(
105         PatchOpcodes::PO_SW, RegNum::RN_SP, RegNum::RN_RA, 0x4);
106     *reinterpret_cast<uint32_t *>(Sled.Address + 12) = encodeInstruction(
107         PatchOpcodes::PO_SW, RegNum::RN_SP, RegNum::RN_T9, 0x0);
108     *reinterpret_cast<uint32_t *>(Sled.Address + 16) = encodeInstruction(
109         PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T9, HiTracingHookAddr);
110     *reinterpret_cast<uint32_t *>(Sled.Address + 20) = encodeInstruction(
111         PatchOpcodes::PO_ORI, RegNum::RN_T9, RegNum::RN_T9, LoTracingHookAddr);
112     *reinterpret_cast<uint32_t *>(Sled.Address + 24) = encodeInstruction(
113         PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0, HiFunctionID);
114     *reinterpret_cast<uint32_t *>(Sled.Address + 28) = encodeSpecialInstruction(
115         PatchOpcodes::PO_JALR, RegNum::RN_T9, 0x0, RegNum::RN_RA, 0X0);
116     *reinterpret_cast<uint32_t *>(Sled.Address + 32) = encodeInstruction(
117         PatchOpcodes::PO_ORI, RegNum::RN_T0, RegNum::RN_T0, LoFunctionID);
118     *reinterpret_cast<uint32_t *>(Sled.Address + 36) = encodeInstruction(
119         PatchOpcodes::PO_LW, RegNum::RN_SP, RegNum::RN_T9, 0x0);
120     *reinterpret_cast<uint32_t *>(Sled.Address + 40) = encodeInstruction(
121         PatchOpcodes::PO_LW, RegNum::RN_SP, RegNum::RN_RA, 0x4);
122     *reinterpret_cast<uint32_t *>(Sled.Address + 44) = encodeInstruction(
123         PatchOpcodes::PO_ADDIU, RegNum::RN_SP, RegNum::RN_SP, 0x8);
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> *>(Sled.Address),
128         uint32_t(CreateStackSpaceInstr), std::memory_order_release);
129   } else {
130     std::atomic_store_explicit(
131         reinterpret_cast<std::atomic<uint32_t> *>(Sled.Address),
132         uint32_t(PatchOpcodes::PO_B44), std::memory_order_release);
133   }
134   return true;
135 }
136
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);
141 }
142
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);
146 }
147
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);
153 }
154
155 bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
156                       const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
157   // FIXME: Implement in mips?
158   return false;
159 }
160
161 bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
162                      const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
163   // FIXME: Implement in mips?
164   return false;
165 }
166
167 } // namespace __xray
168
169 extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
170   // FIXME: this will have to be implemented in the trampoline assembly file
171 }