1use std::future::Future;
5
6pub trait Cpu: AccessCpuState {
7 type Error;
9
10 fn read_instruction(
12 &mut self,
13 gva: u64,
14 bytes: &mut [u8],
15 ) -> impl Future<Output = Result<(), Self::Error>>;
16
17 fn read_memory(
19 &mut self,
20 gva: u64,
21 bytes: &mut [u8],
22 ) -> impl Future<Output = Result<(), Self::Error>>;
23
24 fn read_physical_memory(
26 &mut self,
27 gpa: u64,
28 bytes: &mut [u8],
29 ) -> impl Future<Output = Result<(), Self::Error>>;
30
31 fn write_memory(
33 &mut self,
34 gva: u64,
35 bytes: &[u8],
36 ) -> impl Future<Output = Result<(), Self::Error>>;
37
38 fn write_physical_memory(
40 &mut self,
41 gpa: u64,
42 bytes: &[u8],
43 ) -> impl Future<Output = Result<(), Self::Error>>;
44
45 fn compare_and_write_memory(
58 &mut self,
59 gva: u64,
60 current: &[u8],
61 new: &[u8],
62 success: &mut bool,
63 ) -> impl Future<Output = Result<(), Self::Error>>;
64}
65
66impl<T: Cpu + ?Sized> Cpu for &mut T {
67 type Error = T::Error;
68
69 fn read_instruction(
70 &mut self,
71 gva: u64,
72 bytes: &mut [u8],
73 ) -> impl Future<Output = Result<(), Self::Error>> {
74 (*self).read_memory(gva, bytes)
75 }
76
77 fn read_memory(
78 &mut self,
79 gva: u64,
80 bytes: &mut [u8],
81 ) -> impl Future<Output = Result<(), Self::Error>> {
82 (*self).read_memory(gva, bytes)
83 }
84
85 fn read_physical_memory(
86 &mut self,
87 gpa: u64,
88 bytes: &mut [u8],
89 ) -> impl Future<Output = Result<(), Self::Error>> {
90 (*self).read_physical_memory(gpa, bytes)
91 }
92
93 fn write_memory(
94 &mut self,
95 gva: u64,
96 bytes: &[u8],
97 ) -> impl Future<Output = Result<(), Self::Error>> {
98 (*self).write_memory(gva, bytes)
99 }
100
101 fn write_physical_memory(
102 &mut self,
103 gpa: u64,
104 bytes: &[u8],
105 ) -> impl Future<Output = Result<(), Self::Error>> {
106 (*self).write_physical_memory(gpa, bytes)
107 }
108
109 fn compare_and_write_memory(
110 &mut self,
111 gva: u64,
112 current: &[u8],
113 new: &[u8],
114 success: &mut bool,
115 ) -> impl Future<Output = Result<(), Self::Error>> {
116 (*self).compare_and_write_memory(gva, current, new, success)
117 }
118}
119
120pub trait AccessCpuState {
121 fn commit(&mut self);
123
124 fn x(&mut self, index: u8) -> u64;
126
127 fn update_x(&mut self, index: u8, data: u64);
129
130 fn q(&self, index: u8) -> u128;
132
133 fn update_q(&mut self, index: u8, data: u128);
135
136 fn d(&self, index: u8) -> u64;
138
139 fn update_d(&mut self, index: u8, data: u64);
141
142 fn h(&self, index: u8) -> u32;
144
145 fn update_h(&mut self, index: u8, data: u32);
147
148 fn s(&self, index: u8) -> u16;
150
151 fn update_s(&mut self, index: u8, data: u16);
153
154 fn b(&self, index: u8) -> u8;
156
157 fn update_b(&mut self, index: u8, data: u8);
159
160 fn sp(&mut self) -> u64;
162
163 fn update_sp(&mut self, data: u64);
165
166 fn fp(&mut self) -> u64;
168
169 fn update_fp(&mut self, data: u64);
171
172 fn lr(&mut self) -> u64;
174
175 fn update_lr(&mut self, data: u64);
177
178 fn pc(&mut self) -> u64;
180
181 fn update_pc(&mut self, data: u64);
183
184 fn cpsr(&mut self) -> aarch64defs::Cpsr64;
186}
187
188impl<T: AccessCpuState + ?Sized> AccessCpuState for &mut T {
189 fn commit(&mut self) {
190 (*self).commit()
191 }
192 fn x(&mut self, index: u8) -> u64 {
193 (*self).x(index)
194 }
195 fn update_x(&mut self, index: u8, data: u64) {
196 (*self).update_x(index, data)
197 }
198 fn q(&self, index: u8) -> u128 {
199 (**self).q(index)
200 }
201 fn update_q(&mut self, index: u8, data: u128) {
202 (*self).update_q(index, data)
203 }
204 fn d(&self, index: u8) -> u64 {
205 (**self).d(index)
206 }
207 fn update_d(&mut self, index: u8, data: u64) {
208 (*self).update_d(index, data)
209 }
210 fn h(&self, index: u8) -> u32 {
211 (**self).h(index)
212 }
213 fn update_h(&mut self, index: u8, data: u32) {
214 (*self).update_h(index, data)
215 }
216 fn s(&self, index: u8) -> u16 {
217 (**self).s(index)
218 }
219 fn update_s(&mut self, index: u8, data: u16) {
220 (*self).update_s(index, data)
221 }
222 fn b(&self, index: u8) -> u8 {
223 (**self).b(index)
224 }
225 fn update_b(&mut self, index: u8, data: u8) {
226 (*self).update_b(index, data)
227 }
228 fn sp(&mut self) -> u64 {
229 (*self).sp()
230 }
231 fn update_sp(&mut self, data: u64) {
232 (*self).update_sp(data)
233 }
234 fn fp(&mut self) -> u64 {
235 (*self).fp()
236 }
237 fn update_fp(&mut self, data: u64) {
238 (*self).update_fp(data)
239 }
240 fn lr(&mut self) -> u64 {
241 (*self).lr()
242 }
243 fn update_lr(&mut self, data: u64) {
244 (*self).update_lr(data)
245 }
246 fn pc(&mut self) -> u64 {
247 (*self).pc()
248 }
249 fn update_pc(&mut self, data: u64) {
250 (*self).update_pc(data)
251 }
252 fn cpsr(&mut self) -> aarch64defs::Cpsr64 {
253 (*self).cpsr()
254 }
255}