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