]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_mips64.cc
Merge compiler-rt trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_mips64.cc
1 //===-- xray_mips64.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 MIPS64-specific routines.
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_DADDIU = 0x64000000, // daddiu rt, rs, imm
25   PO_SD = 0xFC000000,     // sd rt, base(offset)
26   PO_LUI = 0x3C000000,    // lui rt, imm
27   PO_ORI = 0x34000000,    // ori rt, rs, imm
28   PO_DSLL = 0x00000038,   // dsll rd, rt, sa
29   PO_JALR = 0x00000009,   // jalr rs
30   PO_LD = 0xDC000000,     // ld rt, base(offset)
31   PO_B60 = 0x1000000f,    // b #60
32   PO_NOP = 0x0,           // nop
33 };
34
35 enum RegNum : uint32_t {
36   RN_T0 = 0xC,
37   RN_T9 = 0x19,
38   RN_RA = 0x1F,
39   RN_SP = 0x1D,
40 };
41
42 inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs,
43                                          uint32_t Rt,
44                                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
45   return (Opcode | Rs << 21 | Rt << 16 | Imm);
46 }
47
48 inline static uint32_t
49 encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd,
50                          uint32_t Imm) XRAY_NEVER_INSTRUMENT {
51   return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode);
52 }
53
54 inline static bool patchSled(const bool Enable, const uint32_t FuncId,
55                              const XRaySledEntry &Sled,
56                              void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
57   // When |Enable| == true,
58   // We replace the following compile-time stub (sled):
59   //
60   // xray_sled_n:
61   //    B .tmpN
62   //    15 NOPs (60 bytes)
63   //    .tmpN
64   //
65   // With the following runtime patch:
66   //
67   // xray_sled_n (64-bit):
68   //    daddiu sp, sp, -16                      ;create stack frame
69   //    nop
70   //    sd ra, 8(sp)                            ;save return address
71   //    sd t9, 0(sp)                            ;save register t9
72   //    lui t9, %highest(__xray_FunctionEntry/Exit)
73   //    ori t9, t9, %higher(__xray_FunctionEntry/Exit)
74   //    dsll t9, t9, 16
75   //    ori t9, t9, %hi(__xray_FunctionEntry/Exit)
76   //    dsll t9, t9, 16
77   //    ori t9, t9, %lo(__xray_FunctionEntry/Exit)
78   //    lui t0, %hi(function_id)
79   //    jalr t9                                 ;call Tracing hook
80   //    ori t0, t0, %lo(function_id)            ;pass function id (delay slot)
81   //    ld t9, 0(sp)                            ;restore register t9
82   //    ld ra, 8(sp)                            ;restore return address
83   //    daddiu sp, sp, 16                       ;delete stack frame
84   //
85   // Replacement of the first 4-byte instruction should be the last and atomic
86   // operation, so that the user code which reaches the sled concurrently
87   // either jumps over the whole sled, or executes the whole sled when the
88   // latter is ready.
89   //
90   // When |Enable|==false, we set back the first instruction in the sled to be
91   //   B #60
92
93   if (Enable) {
94     uint32_t LoTracingHookAddr =
95         reinterpret_cast<int64_t>(TracingHook) & 0xffff;
96     uint32_t HiTracingHookAddr = (reinterpret_cast<int64_t>(TracingHook) >> 16) & 0xffff;
97     uint32_t HigherTracingHookAddr =
98         (reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xffff;
99     uint32_t HighestTracingHookAddr =
100         (reinterpret_cast<int64_t>(TracingHook) >> 48) & 0xffff;
101     uint32_t LoFunctionID = FuncId & 0xffff;
102     uint32_t HiFunctionID = (FuncId >> 16) & 0xffff;
103     *reinterpret_cast<uint32_t *>(Sled.Address + 8) = encodeInstruction(
104         PatchOpcodes::PO_SD, RegNum::RN_SP, RegNum::RN_RA, 0x8);
105     *reinterpret_cast<uint32_t *>(Sled.Address + 12) = encodeInstruction(
106         PatchOpcodes::PO_SD, RegNum::RN_SP, RegNum::RN_T9, 0x0);
107     *reinterpret_cast<uint32_t *>(Sled.Address + 16) = encodeInstruction(
108         PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T9, HighestTracingHookAddr);
109     *reinterpret_cast<uint32_t *>(Sled.Address + 20) =
110         encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9, RegNum::RN_T9,
111                           HigherTracingHookAddr);
112     *reinterpret_cast<uint32_t *>(Sled.Address + 24) = encodeSpecialInstruction(
113         PatchOpcodes::PO_DSLL, 0x0, RegNum::RN_T9, RegNum::RN_T9, 0x10);
114     *reinterpret_cast<uint32_t *>(Sled.Address + 28) = encodeInstruction(
115         PatchOpcodes::PO_ORI, RegNum::RN_T9, RegNum::RN_T9, HiTracingHookAddr);
116     *reinterpret_cast<uint32_t *>(Sled.Address + 32) = encodeSpecialInstruction(
117         PatchOpcodes::PO_DSLL, 0x0, RegNum::RN_T9, RegNum::RN_T9, 0x10);
118     *reinterpret_cast<uint32_t *>(Sled.Address + 36) = encodeInstruction(
119         PatchOpcodes::PO_ORI, RegNum::RN_T9, RegNum::RN_T9, LoTracingHookAddr);
120     *reinterpret_cast<uint32_t *>(Sled.Address + 40) = encodeInstruction(
121         PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0, HiFunctionID);
122     *reinterpret_cast<uint32_t *>(Sled.Address + 44) = encodeSpecialInstruction(
123         PatchOpcodes::PO_JALR, RegNum::RN_T9, 0x0, RegNum::RN_RA, 0X0);
124     *reinterpret_cast<uint32_t *>(Sled.Address + 48) = encodeInstruction(
125         PatchOpcodes::PO_ORI, RegNum::RN_T0, RegNum::RN_T0, LoFunctionID);
126     *reinterpret_cast<uint32_t *>(Sled.Address + 52) = encodeInstruction(
127         PatchOpcodes::PO_LD, RegNum::RN_SP, RegNum::RN_T9, 0x0);
128     *reinterpret_cast<uint32_t *>(Sled.Address + 56) = encodeInstruction(
129         PatchOpcodes::PO_LD, RegNum::RN_SP, RegNum::RN_RA, 0x8);
130     *reinterpret_cast<uint32_t *>(Sled.Address + 60) = encodeInstruction(
131         PatchOpcodes::PO_DADDIU, RegNum::RN_SP, RegNum::RN_SP, 0x10);
132     uint32_t CreateStackSpace = encodeInstruction(
133         PatchOpcodes::PO_DADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xfff0);
134     std::atomic_store_explicit(
135         reinterpret_cast<std::atomic<uint32_t> *>(Sled.Address),
136         CreateStackSpace, std::memory_order_release);
137   } else {
138     std::atomic_store_explicit(
139         reinterpret_cast<std::atomic<uint32_t> *>(Sled.Address),
140         uint32_t(PatchOpcodes::PO_B60), std::memory_order_release);
141   }
142   return true;
143 }
144
145 bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
146                         const XRaySledEntry &Sled,
147                         void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
148   return patchSled(Enable, FuncId, Sled, Trampoline);
149 }
150
151 bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
152                        const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
153   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
154 }
155
156 bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
157                            const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
158   // FIXME: In the future we'd need to distinguish between non-tail exits and
159   // tail exits for better information preservation.
160   return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
161 }
162
163 } // namespace __xray
164
165 extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
166   // FIXME: this will have to be implemented in the trampoline assembly file
167 }