Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 1 | //===-- xray_arm.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 ARM-specific routines (32-bit). |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 15 | #include "sanitizer_common/sanitizer_common.h" |
Diana Picus | 87d025f | 2016-11-16 09:32:23 +0000 | [diff] [blame] | 16 | #include "xray_defs.h" |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 17 | #include "xray_interface_internal.h" |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 18 | #include <atomic> |
| 19 | #include <cassert> |
| 20 | |
Dean Michael Berris | 29e16de | 2017-05-12 01:07:41 +0000 | [diff] [blame] | 21 | extern "C" void __clear_cache(void *start, void *end); |
Serge Rogatch | 9bce1e7 | 2017-01-19 20:27:11 +0000 | [diff] [blame] | 22 | |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 23 | namespace __xray { |
| 24 | |
| 25 | // The machine codes for some instructions used in runtime patching. |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 26 | enum class PatchOpcodes : uint32_t { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 27 | PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr} |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 28 | PO_BlxIp = 0xE12FFF3C, // BLX ip |
| 29 | PO_PopR0Lr = 0xE8BD4001, // POP {r0, lr} |
| 30 | PO_B20 = 0xEA000005 // B #20 |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | // 0xUUUUWXYZ -> 0x000W0XYZ |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 34 | inline static uint32_t getMovwMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 35 | return (Value & 0xfff) | ((Value & 0xf000) << 4); |
| 36 | } |
| 37 | |
| 38 | // 0xWXYZUUUU -> 0x000W0XYZ |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 39 | inline static uint32_t getMovtMask(const uint32_t Value) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 40 | return getMovwMask(Value >> 16); |
| 41 | } |
| 42 | |
| 43 | // Writes the following instructions: |
| 44 | // MOVW R<regNo>, #<lower 16 bits of the |Value|> |
| 45 | // MOVT R<regNo>, #<higher 16 bits of the |Value|> |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 46 | inline static uint32_t * |
| 47 | write32bitLoadReg(uint8_t regNo, uint32_t *Address, |
| 48 | const uint32_t Value) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 49 | // This is a fatal error: we cannot just report it and continue execution. |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 50 | assert(regNo <= 15 && "Register number must be 0 to 15."); |
| 51 | // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 52 | *Address = (0xE3000000 | (uint32_t(regNo) << 12) | getMovwMask(Value)); |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 53 | Address++; |
| 54 | // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 55 | *Address = (0xE3400000 | (uint32_t(regNo) << 12) | getMovtMask(Value)); |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 56 | return Address + 1; |
| 57 | } |
| 58 | |
| 59 | // Writes the following instructions: |
| 60 | // MOVW r0, #<lower 16 bits of the |Value|> |
| 61 | // MOVT r0, #<higher 16 bits of the |Value|> |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 62 | inline static uint32_t * |
Dean Michael Berris | ea9042c | 2017-02-07 23:35:34 +0000 | [diff] [blame] | 63 | write32bitLoadR0(uint32_t *Address, |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 64 | const uint32_t Value) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 65 | return write32bitLoadReg(0, Address, Value); |
| 66 | } |
| 67 | |
| 68 | // Writes the following instructions: |
| 69 | // MOVW ip, #<lower 16 bits of the |Value|> |
| 70 | // MOVT ip, #<higher 16 bits of the |Value|> |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 71 | inline static uint32_t * |
Dean Michael Berris | ea9042c | 2017-02-07 23:35:34 +0000 | [diff] [blame] | 72 | write32bitLoadIP(uint32_t *Address, |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 73 | const uint32_t Value) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 74 | return write32bitLoadReg(12, Address, Value); |
| 75 | } |
| 76 | |
| 77 | inline static bool patchSled(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 78 | const XRaySledEntry &Sled, |
| 79 | void (*TracingHook)()) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 80 | // When |Enable| == true, |
| 81 | // We replace the following compile-time stub (sled): |
| 82 | // |
| 83 | // xray_sled_n: |
| 84 | // B #20 |
| 85 | // 6 NOPs (24 bytes) |
| 86 | // |
| 87 | // With the following runtime patch: |
| 88 | // |
| 89 | // xray_sled_n: |
| 90 | // PUSH {r0, lr} |
| 91 | // MOVW r0, #<lower 16 bits of function ID> |
| 92 | // MOVT r0, #<higher 16 bits of function ID> |
| 93 | // MOVW ip, #<lower 16 bits of address of TracingHook> |
| 94 | // MOVT ip, #<higher 16 bits of address of TracingHook> |
| 95 | // BLX ip |
| 96 | // POP {r0, lr} |
| 97 | // |
| 98 | // Replacement of the first 4-byte instruction should be the last and atomic |
| 99 | // operation, so that the user code which reaches the sled concurrently |
| 100 | // either jumps over the whole sled, or executes the whole sled when the |
| 101 | // latter is ready. |
| 102 | // |
| 103 | // When |Enable|==false, we set back the first instruction in the sled to be |
| 104 | // B #20 |
| 105 | |
| 106 | uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address); |
Serge Rogatch | 9bce1e7 | 2017-01-19 20:27:11 +0000 | [diff] [blame] | 107 | uint32_t *CurAddress = FirstAddress + 1; |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 108 | if (Enable) { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 109 | CurAddress = |
Dean Michael Berris | ea9042c | 2017-02-07 23:35:34 +0000 | [diff] [blame] | 110 | write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId)); |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 111 | CurAddress = |
Dean Michael Berris | ea9042c | 2017-02-07 23:35:34 +0000 | [diff] [blame] | 112 | write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook)); |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 113 | *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp); |
| 114 | CurAddress++; |
| 115 | *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr); |
Serge Rogatch | 9bce1e7 | 2017-01-19 20:27:11 +0000 | [diff] [blame] | 116 | CurAddress++; |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 117 | std::atomic_store_explicit( |
| 118 | reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), |
| 119 | uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release); |
| 120 | } else { |
| 121 | std::atomic_store_explicit( |
| 122 | reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), |
| 123 | uint32_t(PatchOpcodes::PO_B20), std::memory_order_release); |
| 124 | } |
Dean Michael Berris | 29e16de | 2017-05-12 01:07:41 +0000 | [diff] [blame] | 125 | __clear_cache(reinterpret_cast<char *>(FirstAddress), |
| 126 | reinterpret_cast<char *>(CurAddress)); |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool patchFunctionEntry(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | a814c94 | 2017-03-06 07:25:41 +0000 | [diff] [blame] | 131 | const XRaySledEntry &Sled, |
| 132 | void (*Trampoline)()) XRAY_NEVER_INSTRUMENT { |
| 133 | return patchSled(Enable, FuncId, Sled, Trampoline); |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | bool patchFunctionExit(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 137 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 138 | return patchSled(Enable, FuncId, Sled, __xray_FunctionExit); |
| 139 | } |
| 140 | |
Dean Michael Berris | 1b09aae | 2016-10-13 23:56:54 +0000 | [diff] [blame] | 141 | bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 142 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
Serge Rogatch | c4540b3 | 2017-01-26 16:18:13 +0000 | [diff] [blame] | 143 | return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit); |
Dean Michael Berris | 1b09aae | 2016-10-13 23:56:54 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Dean Michael Berris | 29e16de | 2017-05-12 01:07:41 +0000 | [diff] [blame] | 146 | bool patchCustomEvent(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | 724de21 | 2017-05-12 01:33:55 +0000 | [diff] [blame^] | 147 | const XRaySledEntry &Sled) |
Dean Michael Berris | 29e16de | 2017-05-12 01:07:41 +0000 | [diff] [blame] | 148 | XRAY_NEVER_INSTRUMENT { // FIXME: Implement in arm? |
| 149 | return false; |
| 150 | } |
| 151 | |
Dean Michael Berris | 607617b | 2017-02-02 07:51:21 +0000 | [diff] [blame] | 152 | // FIXME: Maybe implement this better? |
| 153 | bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; } |
| 154 | |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 155 | } // namespace __xray |
Dean Michael Berris | a814c94 | 2017-03-06 07:25:41 +0000 | [diff] [blame] | 156 | |
| 157 | extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT { |
| 158 | // FIXME: this will have to be implemented in the trampoline assembly file |
| 159 | } |