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