DebugServer2
Loading...
Searching...
No Matches
CPUState.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#if !defined(CPUSTATE_H_INTERNAL)
14#error "You shall not include this file directly."
15#endif
16
17#include "DebugServer2/Architecture/ARM/CPUState.h" // Include the A32 variant
18#include "DebugServer2/Architecture/ARM64/RegistersDescriptors.h"
19
20namespace ds2 {
21namespace Architecture {
22namespace ARM64 {
23
24//
25// VFP is shared between A32 and A64
26//
30
31//
32// Import the A32 variant
33//
34typedef ds2::Architecture::ARM::CPUState CPUState32;
35
36//
37// Define the 64-bit variant
38//
39struct CPUState64 {
40 union {
41 uint64_t regs[31 + 1 + 1 + 1];
42 struct {
43 uint64_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14,
44 x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28,
45 fp, lr, sp, pc, cpsr;
46 };
47 } gp;
48
49 union {
50 VFPSingle sng[32];
51 VFPDouble dbl[32];
52 VFPQuad quad[16];
53 } vfp;
54
55 //
56 // Accessors
57 //
58 inline uint64_t pc() const { return gp.pc; }
59 inline void setPC(uint64_t pc) { gp.pc = pc; }
60
61 inline uint64_t sp() const { return gp.sp; }
62 inline void setSP(uint64_t sp) { gp.sp = sp; }
63
64 inline uint64_t retval() const { return gp.x0; }
65
66public:
67 inline void getGPState(GPRegisterValueVector &regs) const {
68 regs.clear();
69 for (size_t n = 0; n < array_sizeof(gp.regs); n++) {
70 regs.push_back(GPRegisterValue{sizeof(gp.regs[n]), gp.regs[n]});
71 }
72 }
73
74 inline void setGPState(std::vector<uint64_t> const &regs) {
75 for (size_t n = 0; n < regs.size() && n < array_sizeof(gp.regs); n++) {
76 gp.regs[n] = regs[n];
77 }
78 }
79
80public:
81 inline void getStopGPState(GPRegisterStopMap &regs, bool forLLDB) const {
82 if (forLLDB) {
83 for (size_t n = 0; n < 33; n++) {
84 regs[n + reg_lldb_x0] = GPRegisterValue{sizeof(gp.regs[n]), gp.regs[n]};
85 }
86 regs[reg_lldb_cpsr] = GPRegisterValue{sizeof(gp.cpsr), gp.cpsr};
87 } else {
88 // GDB can live with non-zero registers
89 for (size_t n = 0; n < 33; n++) {
90 if (n >= 13) {
91 regs[n + reg_gdb_x0] =
92 GPRegisterValue{sizeof(gp.regs[n]), gp.regs[n]};
93 }
94 }
95 regs[reg_gdb_cpsr] = GPRegisterValue{sizeof(gp.cpsr), gp.cpsr};
96 }
97 }
98
99public:
100 inline bool getLLDBRegisterPtr(int regno, void **ptr, size_t *length) const {
101 if (regno >= reg_lldb_x0 && regno <= reg_lldb_x30) {
102 *ptr = const_cast<uint64_t *>(&gp.regs[regno - reg_lldb_x0]);
103 *length = sizeof(gp.regs[0]);
104 } else if (regno == reg_lldb_sp) {
105 *ptr = const_cast<uint64_t *>(&gp.sp);
106 *length = sizeof(gp.sp);
107 } else if (regno == reg_lldb_pc) {
108 *ptr = const_cast<uint64_t *>(&gp.pc);
109 *length = sizeof(gp.pc);
110 } else if (regno == reg_lldb_cpsr) {
111 *ptr = const_cast<uint64_t *>(&gp.cpsr);
112 *length = sizeof(gp.cpsr);
113 } else {
114 return false;
115 }
116
117 return true;
118 }
119
120 inline bool getGDBRegisterPtr(int regno, void **ptr, size_t *length) const {
121 if (regno >= reg_gdb_x0 && regno <= reg_gdb_x30) {
122 *ptr = const_cast<uint64_t *>(&gp.regs[regno - reg_gdb_x0]);
123 *length = sizeof(gp.regs[0]);
124 } else if (regno == reg_gdb_sp) {
125 *ptr = const_cast<uint64_t *>(&gp.sp);
126 *length = sizeof(gp.sp);
127 } else if (regno == reg_gdb_pc) {
128 *ptr = const_cast<uint64_t *>(&gp.pc);
129 *length = sizeof(gp.pc);
130 } else if (regno == reg_gdb_cpsr) {
131 *ptr = const_cast<uint64_t *>(&gp.cpsr);
132 *length = sizeof(gp.cpsr);
133 } else {
134 return false;
135 }
136
137 return true;
138 }
139};
140
141//
142// Define the union of the two variants, this is the public
143// structure.
144//
145
146struct CPUState {
147 bool isA32; // Select which is valid between state32 and state64.
148
149 union {
150 CPUState32 state32;
151 CPUState64 state64;
152 };
153
154 CPUState() : state64() {}
155
156 //
157 // Accessors
158 //
159 inline uint64_t pc() const {
160 return isA32 ? static_cast<uint64_t>(state32.pc()) : state64.pc();
161 }
162 inline void setPC(uint64_t pc) {
163 if (isA32)
164 state32.setPC(pc);
165 else
166 state64.setPC(pc);
167 }
168
169 inline uint64_t sp() const {
170 return isA32 ? static_cast<uint64_t>(state32.sp()) : state64.sp();
171 }
172 inline void setSP(uint64_t sp) {
173 if (isA32)
174 state32.setSP(sp);
175 else
176 state64.setSP(sp);
177 }
178
179 inline uint64_t retval() const {
180 return isA32 ? static_cast<uint64_t>(state32.retval()) : state64.retval();
181 }
182
183 inline bool isThumb() const { return isA32 ? state32.isThumb() : false; }
184
185public:
186 inline void getGPState(GPRegisterValueVector &regs) const {
187 if (isA32) {
188 state32.getGPState(regs);
189 } else {
190 state64.getGPState(regs);
191 }
192 }
193
194 inline void setGPState(std::vector<uint64_t> const &regs) {
195 if (isA32) {
196 state32.setGPState(regs);
197 } else {
198 state64.setGPState(regs);
199 }
200 }
201
202public:
203 inline void getStopGPState(GPRegisterStopMap &regs, bool forLLDB) const {
204 if (isA32) {
205 state32.getStopGPState(regs, forLLDB);
206 } else {
207 state64.getStopGPState(regs, forLLDB);
208 }
209 }
210
211public:
212 inline bool getLLDBRegisterPtr(int regno, void **ptr, size_t *length) const {
213 if (isA32) {
214 return state32.getLLDBRegisterPtr(regno, ptr, length);
215 } else {
216 return state64.getLLDBRegisterPtr(regno, ptr, length);
217 }
218 }
219
220 inline bool getGDBRegisterPtr(int regno, void **ptr, size_t *length) const {
221 if (isA32) {
222 return state32.getGDBRegisterPtr(regno, ptr, length);
223 } else {
224 return state64.getGDBRegisterPtr(regno, ptr, length);
225 }
226 }
227};
228} // namespace ARM64
229} // namespace Architecture
230} // namespace ds2
Definition CPUState.h:39
Definition CPUState.h:146
Definition CPUState.h:51
Definition CPUState.h:35
Definition CPUState.h:39
Definition CPUState.h:25
Definition RegisterLayout.h:167