Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | #include <functional> |
| 20 | |
| 21 | #include <unwindstack/Elf.h> |
| 22 | #include <unwindstack/MapInfo.h> |
| 23 | #include <unwindstack/Memory.h> |
| 24 | #include <unwindstack/RegsX86_64.h> |
| 25 | |
| 26 | #include "MachineX86_64.h" |
| 27 | #include "UcontextX86_64.h" |
| 28 | #include "UserX86_64.h" |
| 29 | |
| 30 | namespace unwindstack { |
| 31 | |
| 32 | RegsX86_64::RegsX86_64() |
| 33 | : RegsImpl<uint64_t>(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {} |
| 34 | |
| 35 | ArchEnum RegsX86_64::Arch() { |
| 36 | return ARCH_X86_64; |
| 37 | } |
| 38 | |
| 39 | uint64_t RegsX86_64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { |
| 40 | if (!elf->valid()) { |
| 41 | return rel_pc; |
| 42 | } |
| 43 | |
| 44 | if (rel_pc == 0) { |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | return rel_pc - 1; |
| 49 | } |
| 50 | |
| 51 | void RegsX86_64::SetFromRaw() { |
| 52 | set_pc(regs_[X86_64_REG_PC]); |
| 53 | set_sp(regs_[X86_64_REG_SP]); |
| 54 | } |
| 55 | |
| 56 | bool RegsX86_64::SetPcFromReturnAddress(Memory* process_memory) { |
| 57 | // Attempt to get the return address from the top of the stack. |
| 58 | uint64_t new_pc; |
| 59 | if (!process_memory->ReadFully(sp_, &new_pc, sizeof(new_pc)) || new_pc == pc()) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | set_pc(new_pc); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void RegsX86_64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) { |
| 68 | fn("rax", regs_[X86_64_REG_RAX]); |
| 69 | fn("rbx", regs_[X86_64_REG_RBX]); |
| 70 | fn("rcx", regs_[X86_64_REG_RCX]); |
| 71 | fn("rdx", regs_[X86_64_REG_RDX]); |
| 72 | fn("r8", regs_[X86_64_REG_R8]); |
| 73 | fn("r9", regs_[X86_64_REG_R9]); |
| 74 | fn("r10", regs_[X86_64_REG_R10]); |
| 75 | fn("r11", regs_[X86_64_REG_R11]); |
| 76 | fn("r12", regs_[X86_64_REG_R12]); |
| 77 | fn("r13", regs_[X86_64_REG_R13]); |
| 78 | fn("r14", regs_[X86_64_REG_R14]); |
| 79 | fn("r15", regs_[X86_64_REG_R15]); |
| 80 | fn("rdi", regs_[X86_64_REG_RDI]); |
| 81 | fn("rsi", regs_[X86_64_REG_RSI]); |
| 82 | fn("rbp", regs_[X86_64_REG_RBP]); |
| 83 | fn("rsp", regs_[X86_64_REG_RSP]); |
| 84 | fn("rip", regs_[X86_64_REG_RIP]); |
| 85 | } |
| 86 | |
| 87 | Regs* RegsX86_64::Read(void* remote_data) { |
| 88 | x86_64_user_regs* user = reinterpret_cast<x86_64_user_regs*>(remote_data); |
| 89 | |
| 90 | RegsX86_64* regs = new RegsX86_64(); |
| 91 | (*regs)[X86_64_REG_RAX] = user->rax; |
| 92 | (*regs)[X86_64_REG_RBX] = user->rbx; |
| 93 | (*regs)[X86_64_REG_RCX] = user->rcx; |
| 94 | (*regs)[X86_64_REG_RDX] = user->rdx; |
| 95 | (*regs)[X86_64_REG_R8] = user->r8; |
| 96 | (*regs)[X86_64_REG_R9] = user->r9; |
| 97 | (*regs)[X86_64_REG_R10] = user->r10; |
| 98 | (*regs)[X86_64_REG_R11] = user->r11; |
| 99 | (*regs)[X86_64_REG_R12] = user->r12; |
| 100 | (*regs)[X86_64_REG_R13] = user->r13; |
| 101 | (*regs)[X86_64_REG_R14] = user->r14; |
| 102 | (*regs)[X86_64_REG_R15] = user->r15; |
| 103 | (*regs)[X86_64_REG_RDI] = user->rdi; |
| 104 | (*regs)[X86_64_REG_RSI] = user->rsi; |
| 105 | (*regs)[X86_64_REG_RBP] = user->rbp; |
| 106 | (*regs)[X86_64_REG_RSP] = user->rsp; |
| 107 | (*regs)[X86_64_REG_RIP] = user->rip; |
| 108 | |
| 109 | regs->SetFromRaw(); |
| 110 | return regs; |
| 111 | } |
| 112 | |
| 113 | void RegsX86_64::SetFromUcontext(x86_64_ucontext_t* ucontext) { |
| 114 | // R8-R15 |
| 115 | memcpy(®s_[X86_64_REG_R8], &ucontext->uc_mcontext.r8, 8 * sizeof(uint64_t)); |
| 116 | |
| 117 | // Rest of the registers. |
| 118 | regs_[X86_64_REG_RDI] = ucontext->uc_mcontext.rdi; |
| 119 | regs_[X86_64_REG_RSI] = ucontext->uc_mcontext.rsi; |
| 120 | regs_[X86_64_REG_RBP] = ucontext->uc_mcontext.rbp; |
| 121 | regs_[X86_64_REG_RBX] = ucontext->uc_mcontext.rbx; |
| 122 | regs_[X86_64_REG_RDX] = ucontext->uc_mcontext.rdx; |
| 123 | regs_[X86_64_REG_RAX] = ucontext->uc_mcontext.rax; |
| 124 | regs_[X86_64_REG_RCX] = ucontext->uc_mcontext.rcx; |
| 125 | regs_[X86_64_REG_RSP] = ucontext->uc_mcontext.rsp; |
| 126 | regs_[X86_64_REG_RIP] = ucontext->uc_mcontext.rip; |
| 127 | |
| 128 | SetFromRaw(); |
| 129 | } |
| 130 | |
| 131 | Regs* RegsX86_64::CreateFromUcontext(void* ucontext) { |
| 132 | x86_64_ucontext_t* x86_64_ucontext = reinterpret_cast<x86_64_ucontext_t*>(ucontext); |
| 133 | |
| 134 | RegsX86_64* regs = new RegsX86_64(); |
| 135 | regs->SetFromUcontext(x86_64_ucontext); |
| 136 | return regs; |
| 137 | } |
| 138 | |
| 139 | bool RegsX86_64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) { |
| 140 | uint64_t data; |
| 141 | Memory* elf_memory = elf->memory(); |
| 142 | // Read from elf memory since it is usually more expensive to read from |
| 143 | // process memory. |
| 144 | if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data)) || data != 0x0f0000000fc0c748) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | uint16_t data2; |
| 149 | if (!elf_memory->ReadFully(rel_pc + 8, &data2, sizeof(data2)) || data2 != 0x0f05) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // __restore_rt: |
| 154 | // 0x48 0xc7 0xc0 0x0f 0x00 0x00 0x00 mov $0xf,%rax |
| 155 | // 0x0f 0x05 syscall |
| 156 | // 0x0f nopl 0x0($rax) |
| 157 | |
| 158 | // Read the mcontext data from the stack. |
| 159 | // sp points to the ucontext data structure, read only the mcontext part. |
| 160 | x86_64_ucontext_t x86_64_ucontext; |
| 161 | if (!process_memory->ReadFully(sp() + 0x28, &x86_64_ucontext.uc_mcontext, |
| 162 | sizeof(x86_64_mcontext_t))) { |
| 163 | return false; |
| 164 | } |
| 165 | SetFromUcontext(&x86_64_ucontext); |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | } // namespace unwindstack |