| 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> | 
| Florian Mayer | 3f1f2e0 | 2018-10-23 15:56:28 +0100 | [diff] [blame] | 18 | #include <string.h> | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 19 |  | 
 | 20 | #include <functional> | 
 | 21 |  | 
 | 22 | #include <unwindstack/Elf.h> | 
| Christopher Ferris | 5391416 | 2018-02-08 19:27:47 -0800 | [diff] [blame] | 23 | #include <unwindstack/MachineX86_64.h> | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 24 | #include <unwindstack/MapInfo.h> | 
 | 25 | #include <unwindstack/Memory.h> | 
 | 26 | #include <unwindstack/RegsX86_64.h> | 
| Christopher Ferris | 5391416 | 2018-02-08 19:27:47 -0800 | [diff] [blame] | 27 | #include <unwindstack/UcontextX86_64.h> | 
 | 28 | #include <unwindstack/UserX86_64.h> | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 29 |  | 
 | 30 | namespace unwindstack { | 
 | 31 |  | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 32 | RegsX86_64::RegsX86_64() : RegsImpl<uint64_t>(X86_64_REG_LAST, Location(LOCATION_SP_OFFSET, -8)) {} | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 33 |  | 
 | 34 | ArchEnum RegsX86_64::Arch() { | 
 | 35 |   return ARCH_X86_64; | 
 | 36 | } | 
 | 37 |  | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 38 | uint64_t RegsX86_64::pc() { | 
 | 39 |   return regs_[X86_64_REG_PC]; | 
 | 40 | } | 
 | 41 |  | 
 | 42 | uint64_t RegsX86_64::sp() { | 
 | 43 |   return regs_[X86_64_REG_SP]; | 
 | 44 | } | 
 | 45 |  | 
 | 46 | void RegsX86_64::set_pc(uint64_t pc) { | 
 | 47 |   regs_[X86_64_REG_PC] = pc; | 
 | 48 | } | 
 | 49 |  | 
 | 50 | void RegsX86_64::set_sp(uint64_t sp) { | 
 | 51 |   regs_[X86_64_REG_SP] = sp; | 
 | 52 | } | 
 | 53 |  | 
| Christopher Ferris | 6dbc28e | 2018-03-28 15:12:49 -0700 | [diff] [blame] | 54 | uint64_t RegsX86_64::GetPcAdjustment(uint64_t rel_pc, Elf*) { | 
 | 55 |   if (rel_pc == 0) { | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 56 |     return 0; | 
 | 57 |   } | 
| Christopher Ferris | a2ec50b | 2018-02-21 15:39:07 -0800 | [diff] [blame] | 58 |   return 1; | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 59 | } | 
 | 60 |  | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 61 | bool RegsX86_64::SetPcFromReturnAddress(Memory* process_memory) { | 
 | 62 |   // Attempt to get the return address from the top of the stack. | 
 | 63 |   uint64_t new_pc; | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 64 |   if (!process_memory->ReadFully(regs_[X86_64_REG_SP], &new_pc, sizeof(new_pc)) || | 
 | 65 |       new_pc == regs_[X86_64_REG_PC]) { | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 66 |     return false; | 
 | 67 |   } | 
 | 68 |  | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 69 |   regs_[X86_64_REG_PC] = new_pc; | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 70 |   return true; | 
 | 71 | } | 
 | 72 |  | 
 | 73 | void RegsX86_64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) { | 
 | 74 |   fn("rax", regs_[X86_64_REG_RAX]); | 
 | 75 |   fn("rbx", regs_[X86_64_REG_RBX]); | 
 | 76 |   fn("rcx", regs_[X86_64_REG_RCX]); | 
 | 77 |   fn("rdx", regs_[X86_64_REG_RDX]); | 
 | 78 |   fn("r8", regs_[X86_64_REG_R8]); | 
 | 79 |   fn("r9", regs_[X86_64_REG_R9]); | 
 | 80 |   fn("r10", regs_[X86_64_REG_R10]); | 
 | 81 |   fn("r11", regs_[X86_64_REG_R11]); | 
 | 82 |   fn("r12", regs_[X86_64_REG_R12]); | 
 | 83 |   fn("r13", regs_[X86_64_REG_R13]); | 
 | 84 |   fn("r14", regs_[X86_64_REG_R14]); | 
 | 85 |   fn("r15", regs_[X86_64_REG_R15]); | 
 | 86 |   fn("rdi", regs_[X86_64_REG_RDI]); | 
 | 87 |   fn("rsi", regs_[X86_64_REG_RSI]); | 
 | 88 |   fn("rbp", regs_[X86_64_REG_RBP]); | 
 | 89 |   fn("rsp", regs_[X86_64_REG_RSP]); | 
 | 90 |   fn("rip", regs_[X86_64_REG_RIP]); | 
 | 91 | } | 
 | 92 |  | 
 | 93 | Regs* RegsX86_64::Read(void* remote_data) { | 
 | 94 |   x86_64_user_regs* user = reinterpret_cast<x86_64_user_regs*>(remote_data); | 
 | 95 |  | 
 | 96 |   RegsX86_64* regs = new RegsX86_64(); | 
 | 97 |   (*regs)[X86_64_REG_RAX] = user->rax; | 
 | 98 |   (*regs)[X86_64_REG_RBX] = user->rbx; | 
 | 99 |   (*regs)[X86_64_REG_RCX] = user->rcx; | 
 | 100 |   (*regs)[X86_64_REG_RDX] = user->rdx; | 
 | 101 |   (*regs)[X86_64_REG_R8] = user->r8; | 
 | 102 |   (*regs)[X86_64_REG_R9] = user->r9; | 
 | 103 |   (*regs)[X86_64_REG_R10] = user->r10; | 
 | 104 |   (*regs)[X86_64_REG_R11] = user->r11; | 
 | 105 |   (*regs)[X86_64_REG_R12] = user->r12; | 
 | 106 |   (*regs)[X86_64_REG_R13] = user->r13; | 
 | 107 |   (*regs)[X86_64_REG_R14] = user->r14; | 
 | 108 |   (*regs)[X86_64_REG_R15] = user->r15; | 
 | 109 |   (*regs)[X86_64_REG_RDI] = user->rdi; | 
 | 110 |   (*regs)[X86_64_REG_RSI] = user->rsi; | 
 | 111 |   (*regs)[X86_64_REG_RBP] = user->rbp; | 
 | 112 |   (*regs)[X86_64_REG_RSP] = user->rsp; | 
 | 113 |   (*regs)[X86_64_REG_RIP] = user->rip; | 
 | 114 |  | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 115 |   return regs; | 
 | 116 | } | 
 | 117 |  | 
 | 118 | void RegsX86_64::SetFromUcontext(x86_64_ucontext_t* ucontext) { | 
 | 119 |   // R8-R15 | 
 | 120 |   memcpy(®s_[X86_64_REG_R8], &ucontext->uc_mcontext.r8, 8 * sizeof(uint64_t)); | 
 | 121 |  | 
 | 122 |   // Rest of the registers. | 
 | 123 |   regs_[X86_64_REG_RDI] = ucontext->uc_mcontext.rdi; | 
 | 124 |   regs_[X86_64_REG_RSI] = ucontext->uc_mcontext.rsi; | 
 | 125 |   regs_[X86_64_REG_RBP] = ucontext->uc_mcontext.rbp; | 
 | 126 |   regs_[X86_64_REG_RBX] = ucontext->uc_mcontext.rbx; | 
 | 127 |   regs_[X86_64_REG_RDX] = ucontext->uc_mcontext.rdx; | 
 | 128 |   regs_[X86_64_REG_RAX] = ucontext->uc_mcontext.rax; | 
 | 129 |   regs_[X86_64_REG_RCX] = ucontext->uc_mcontext.rcx; | 
 | 130 |   regs_[X86_64_REG_RSP] = ucontext->uc_mcontext.rsp; | 
 | 131 |   regs_[X86_64_REG_RIP] = ucontext->uc_mcontext.rip; | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 132 | } | 
 | 133 |  | 
 | 134 | Regs* RegsX86_64::CreateFromUcontext(void* ucontext) { | 
 | 135 |   x86_64_ucontext_t* x86_64_ucontext = reinterpret_cast<x86_64_ucontext_t*>(ucontext); | 
 | 136 |  | 
 | 137 |   RegsX86_64* regs = new RegsX86_64(); | 
 | 138 |   regs->SetFromUcontext(x86_64_ucontext); | 
 | 139 |   return regs; | 
 | 140 | } | 
 | 141 |  | 
| Christopher Ferris | f0c82e7 | 2019-12-04 13:37:11 -0800 | [diff] [blame] | 142 | bool RegsX86_64::StepIfSignalHandler(uint64_t elf_offset, Elf* elf, Memory* process_memory) { | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 143 |   uint64_t data; | 
 | 144 |   Memory* elf_memory = elf->memory(); | 
 | 145 |   // Read from elf memory since it is usually more expensive to read from | 
 | 146 |   // process memory. | 
| Christopher Ferris | f0c82e7 | 2019-12-04 13:37:11 -0800 | [diff] [blame] | 147 |   if (!elf_memory->ReadFully(elf_offset, &data, sizeof(data)) || data != 0x0f0000000fc0c748) { | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 148 |     return false; | 
 | 149 |   } | 
 | 150 |  | 
 | 151 |   uint16_t data2; | 
| Christopher Ferris | f0c82e7 | 2019-12-04 13:37:11 -0800 | [diff] [blame] | 152 |   if (!elf_memory->ReadFully(elf_offset + 8, &data2, sizeof(data2)) || data2 != 0x0f05) { | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 153 |     return false; | 
 | 154 |   } | 
 | 155 |  | 
 | 156 |   // __restore_rt: | 
 | 157 |   // 0x48 0xc7 0xc0 0x0f 0x00 0x00 0x00   mov $0xf,%rax | 
 | 158 |   // 0x0f 0x05                            syscall | 
 | 159 |   // 0x0f                                 nopl 0x0($rax) | 
 | 160 |  | 
 | 161 |   // Read the mcontext data from the stack. | 
 | 162 |   // sp points to the ucontext data structure, read only the mcontext part. | 
 | 163 |   x86_64_ucontext_t x86_64_ucontext; | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 164 |   if (!process_memory->ReadFully(regs_[X86_64_REG_SP] + 0x28, &x86_64_ucontext.uc_mcontext, | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 165 |                                  sizeof(x86_64_mcontext_t))) { | 
 | 166 |     return false; | 
 | 167 |   } | 
 | 168 |   SetFromUcontext(&x86_64_ucontext); | 
 | 169 |   return true; | 
 | 170 | } | 
 | 171 |  | 
| Josh Gao | 2f37a15 | 2018-04-20 11:51:14 -0700 | [diff] [blame] | 172 | Regs* RegsX86_64::Clone() { | 
 | 173 |   return new RegsX86_64(*this); | 
 | 174 | } | 
 | 175 |  | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 176 | }  // namespace unwindstack |