-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathverify.cpp
275 lines (210 loc) · 4.79 KB
/
verify.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#if !defined(__linux__) || defined(__EMSCRIPTEN__)
namespace VERIFY {
void init(){}
void update(){}
}
#else
#include <iostream>
#include <cstdio>
#include <numeric>
#include "SDL2/SDL.h"
#include <list>
#include <thread>
#include <mutex>
#include <chrono>
#include <string>
#include <sstream>
#include "types.hpp"
#include "cpu.hpp"
#include "gdb.hpp"
#include "state.hpp"
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
extern bool hasQuit;
#define READ 0
#define WRITE 1
pid_t popen2(const char *command, int *infp, int *outfp){
int p_stdin[2], p_stdout[2];
pid_t pid;
if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
return -1;
pid = fork();
if (pid < 0)
return pid;
else if (pid == 0)
{
close(p_stdin[WRITE]);
dup2(p_stdin[READ], READ);
close(p_stdout[READ]);
dup2(p_stdout[WRITE], WRITE);
execl("/bin/sh", "sh", "-c", command, NULL);
perror("execl");
exit(1);
}
if (infp == NULL)
close(p_stdin[WRITE]);
else
*infp = p_stdin[WRITE];
if (outfp == NULL)
close(p_stdout[READ]);
else
*outfp = p_stdout[READ];
return pid;
}
namespace VERIFY {
pid_t pid;
int infp, outfp;
std::string lastRead;
std::mutex readMut;
std::thread worker;
void pollInput(){
char buf[257];
while( !hasQuit ){
size_t s = ::read(outfp, buf, 256);
if( s == -1 ) return;
{
buf[s] = 0;
std::lock_guard<std::mutex> rml(readMut);
lastRead += buf;
}
}
}
std::string read(){
std::this_thread::sleep_for( std::chrono::milliseconds(150) );
std::string ret;
{
std::lock_guard<std::mutex> rml(readMut);
ret = lastRead;
lastRead.clear();
}
return ret;
}
std::string send( const std::string &msg ){
// std::this_thread::sleep_for( std::chrono::milliseconds(100) );
{
std::lock_guard<std::mutex> rml(readMut);
lastRead.clear();
}
size_t ws = write(infp, msg.c_str(), msg.size() );
if( ws < 0 ){
pid = 0;
return "ERROR";
}
if( ws != msg.size() )
SDL_Log("BORK");
return read();
}
void stop(){
emustate = EmuState::STOPPED;
}
void go(){
emustate = EmuState::STEP;
send("si\n");
}
CPU::reg_pair reg[45];
bool changes[45];
bool firstRead = true;
void readRegisters(){
for( u32 i=0; i<45; ++i )
changes[i] = false;
std::string acc;
std::string s = send("i r\n");
while( !s.empty() ){
acc += s;
s = read();
}
acc = acc.substr(acc.find('r'));
acc = acc.substr(0, acc.size()-7);
std::stringstream ss(acc);
u32 rid = 0;
while( std::getline( ss, s ) ){
std::stringstream line(s);
std::string regname;
uint32_t value;
line >> regname >> std::hex >> value;
if( reg[rid].I != value ){
changes[rid] = true;
// SDL_Log("Register: {{%s}} %s=%x", s.c_str(), regname.c_str(), value);
}
reg[rid].I = value;
rid++;
}
if( firstRead ){
firstRead = false;
for( u32 i=0; i<=16; ++i ){
CPU::reg[i].I = reg[i].I;
changes[i] = false;
}
CPU::CPUUpdateFlags();
}
}
u32 difCount;
void compareRegisters(){
CPU::CPUUpdateCPSR();
bool dif = false;
for( u32 i=0; i<=16; ++i ){
u32 rv = CPU::reg[i].I;
if( i == 15 ) rv = CPU::armNextPC&~1;
if( reg[i].I != rv && changes[i] ){
dif = true;
break;
}
}
if( !dif ){
go();
return;
}
std::stringstream line;
line << std::hex << CPU::armNextPC << ": ";
for( u32 i=0; i<=16; ++i ){
u32 rv = CPU::reg[i].I;
if( i == 15 ) rv = CPU::armNextPC&~1;
if( reg[i].I != rv )
line << "r"
<< std::to_string(i)
<< ": " << CPU::reg[i].I
<< " != " << reg[i].I
<< "\n";
if( i <= 15 )
CPU::reg[i].I = reg[i].I;
if( i == 15 ){
CPU::armNextPC = reg[i].I;
CPU::reg[15].I += 2;
CPU::THUMB_PREFETCH();
}
}
SDL_Log( "DIF: %s", line.str().c_str() );
if( reg[16].I != CPU::reg[16].I && changes[16] )
difCount++;
if( difCount > 20 )
stop();
else
go();
}
void init(){
pid = popen2( "arm-none-eabi-gdb -q", &infp, &outfp );
if( !pid )
return;
worker = std::thread(pollInput);
worker.detach();
emustate = EmuState::JUST_STOPPED;
SDL_Log("GDB PID %d, RESP %s", pid, read().c_str() );
SDL_Log("CONNECT: %s", send("target remote :2331\n").c_str());
std::this_thread::sleep_for( std::chrono::milliseconds(500) );
SDL_Log("RESET: %s", send("mon reset 0\n").c_str());
std::this_thread::sleep_for( std::chrono::milliseconds(500) );
send("si\n");
SDL_Log("RESET: %s", send("mon reset 0\n").c_str());
std::this_thread::sleep_for( std::chrono::milliseconds(500) );
}
void update(){
if( !pid || emustate != EmuState::JUST_STOPPED )
return;
readRegisters();
compareRegisters();
}
}
#endif