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