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" |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 16 | #include "xray_interface_internal.h" |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 17 | #include <atomic> |
| 18 | #include <cassert> |
| 19 | |
| 20 | namespace __xray { |
| 21 | |
| 22 | // The machine codes for some instructions used in runtime patching. |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 23 | enum class PatchOpcodes : uint32_t { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 24 | PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr} |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 25 | PO_BlxIp = 0xE12FFF3C, // BLX ip |
| 26 | PO_PopR0Lr = 0xE8BD4001, // POP {r0, lr} |
| 27 | PO_B20 = 0xEA000005 // B #20 |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | // 0xUUUUWXYZ -> 0x000W0XYZ |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame^] | 31 | 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] | 32 | return (Value & 0xfff) | ((Value & 0xf000) << 4); |
| 33 | } |
| 34 | |
| 35 | // 0xWXYZUUUU -> 0x000W0XYZ |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame^] | 36 | 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] | 37 | return getMovwMask(Value >> 16); |
| 38 | } |
| 39 | |
| 40 | // Writes the following instructions: |
| 41 | // MOVW R<regNo>, #<lower 16 bits of the |Value|> |
| 42 | // MOVT R<regNo>, #<higher 16 bits of the |Value|> |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame^] | 43 | inline static uint32_t * |
| 44 | write32bitLoadReg(uint8_t regNo, uint32_t *Address, |
| 45 | const uint32_t Value) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 46 | // 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] | 47 | assert(regNo <= 15 && "Register number must be 0 to 15."); |
| 48 | // MOVW R, #0xWXYZ in machine code is 0xE30WRXYZ |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 49 | *Address = (0xE3000000 | (uint32_t(regNo) << 12) | getMovwMask(Value)); |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 50 | Address++; |
| 51 | // MOVT R, #0xWXYZ in machine code is 0xE34WRXYZ |
Dean Michael Berris | 4ef1a69 | 2016-10-06 07:09:40 +0000 | [diff] [blame] | 52 | *Address = (0xE3400000 | (uint32_t(regNo) << 12) | getMovtMask(Value)); |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 53 | return Address + 1; |
| 54 | } |
| 55 | |
| 56 | // Writes the following instructions: |
| 57 | // MOVW r0, #<lower 16 bits of the |Value|> |
| 58 | // MOVT r0, #<higher 16 bits of the |Value|> |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame^] | 59 | inline static uint32_t * |
| 60 | Write32bitLoadR0(uint32_t *Address, |
| 61 | const uint32_t Value) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 62 | return write32bitLoadReg(0, Address, Value); |
| 63 | } |
| 64 | |
| 65 | // Writes the following instructions: |
| 66 | // MOVW ip, #<lower 16 bits of the |Value|> |
| 67 | // MOVT ip, #<higher 16 bits of the |Value|> |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame^] | 68 | inline static uint32_t * |
| 69 | Write32bitLoadIP(uint32_t *Address, |
| 70 | const uint32_t Value) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 71 | return write32bitLoadReg(12, Address, Value); |
| 72 | } |
| 73 | |
| 74 | inline static bool patchSled(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame^] | 75 | const XRaySledEntry &Sled, |
| 76 | void (*TracingHook)()) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 77 | // When |Enable| == true, |
| 78 | // We replace the following compile-time stub (sled): |
| 79 | // |
| 80 | // xray_sled_n: |
| 81 | // B #20 |
| 82 | // 6 NOPs (24 bytes) |
| 83 | // |
| 84 | // With the following runtime patch: |
| 85 | // |
| 86 | // xray_sled_n: |
| 87 | // PUSH {r0, lr} |
| 88 | // MOVW r0, #<lower 16 bits of function ID> |
| 89 | // MOVT r0, #<higher 16 bits of function ID> |
| 90 | // MOVW ip, #<lower 16 bits of address of TracingHook> |
| 91 | // MOVT ip, #<higher 16 bits of address of TracingHook> |
| 92 | // BLX ip |
| 93 | // POP {r0, lr} |
| 94 | // |
| 95 | // Replacement of the first 4-byte instruction should be the last and atomic |
| 96 | // operation, so that the user code which reaches the sled concurrently |
| 97 | // either jumps over the whole sled, or executes the whole sled when the |
| 98 | // latter is ready. |
| 99 | // |
| 100 | // When |Enable|==false, we set back the first instruction in the sled to be |
| 101 | // B #20 |
| 102 | |
| 103 | uint32_t *FirstAddress = reinterpret_cast<uint32_t *>(Sled.Address); |
| 104 | if (Enable) { |
| 105 | uint32_t *CurAddress = FirstAddress + 1; |
| 106 | CurAddress = |
| 107 | Write32bitLoadR0(CurAddress, reinterpret_cast<uint32_t>(FuncId)); |
| 108 | CurAddress = |
| 109 | Write32bitLoadIP(CurAddress, reinterpret_cast<uint32_t>(TracingHook)); |
| 110 | *CurAddress = uint32_t(PatchOpcodes::PO_BlxIp); |
| 111 | CurAddress++; |
| 112 | *CurAddress = uint32_t(PatchOpcodes::PO_PopR0Lr); |
| 113 | std::atomic_store_explicit( |
| 114 | reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), |
| 115 | uint32_t(PatchOpcodes::PO_PushR0Lr), std::memory_order_release); |
| 116 | } else { |
| 117 | std::atomic_store_explicit( |
| 118 | reinterpret_cast<std::atomic<uint32_t> *>(FirstAddress), |
| 119 | uint32_t(PatchOpcodes::PO_B20), std::memory_order_release); |
| 120 | } |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | bool patchFunctionEntry(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame^] | 125 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 126 | return patchSled(Enable, FuncId, Sled, __xray_FunctionEntry); |
| 127 | } |
| 128 | |
| 129 | bool patchFunctionExit(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame^] | 130 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 131 | return patchSled(Enable, FuncId, Sled, __xray_FunctionExit); |
| 132 | } |
| 133 | |
Dean Michael Berris | 1b09aae | 2016-10-13 23:56:54 +0000 | [diff] [blame] | 134 | bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId, |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame^] | 135 | const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | 1b09aae | 2016-10-13 23:56:54 +0000 | [diff] [blame] | 136 | // FIXME: In the future we'd need to distinguish between non-tail exits and |
| 137 | // tail exits for better information preservation. |
| 138 | return patchSled(Enable, FuncId, Sled, __xray_FunctionExit); |
| 139 | } |
| 140 | |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 141 | } // namespace __xray |