-
Notifications
You must be signed in to change notification settings - Fork 0
/
Emulator.cpp
347 lines (272 loc) · 6.74 KB
/
Emulator.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <sstream>
#include "Emulator.h"
#include "ModRM.h"
#include "device/keyboard.h"
using namespace std;
const char* registers_name16[] = { "AX", "CX", "DX", "BX", "SP", "BP", "SI", "DI"};
const char* registers_name32[] = {"EAX", "ECX", "EDX", "EBX", "ESP", "EBP", "ESI", "EDI"};
//void test(Emulator *emu){
//}
Emulator::Emulator(){
BitMode = DEFAULT_BIT_MODE;
memory_size = 1024 * 1024 * 1024;
memory = new (nothrow) uint8_t[memory_size];
if(memory == NULL){
}
InitRegisters();
is_16mode = true;
sgregs[1].base = 0x280000; //1 == cs
sgregs[3].base = 0x000000; //3 == DS
sgregs[0].base = 0x000000; //0 == Es
sgregs[2].base = 0x000000; //2 == Ss
InitInstructions16();
InitInstructions32();
// memset(memory, 0x01, memory_size);
// test(this);
}
void Emulator::SetTR(uint16_t sel){
uint32_t gdt_base, base;
uint16_t gdt_limit, limit;
TSSDesc tssdesc;
gdt_base = dtregs[GDTR].base_addr;
gdt_limit = dtregs[GDTR].table_limit;
read_data(&tssdesc, gdt_base + sel, sizeof(TSSDesc));
base = (tssdesc.base_h << 24) + (tssdesc.base_m << 16) + tssdesc.base_l;
limit = (tssdesc.limit_h << 16) + tssdesc.limit_l;
set_dtreg(TR, sel, base, limit);
}
Emulator::~Emulator(){
delete[] memory;
}
int Emulator::parse_prefix(Emulator *emu){
while(true) {
uint8_t code = emu->GetSignCode8(0);
switch(code) {
case 0x66:
emu->EIP++;
return 0;
}
break;
}
return -1;
}
size_t Emulator::read_data(void *dst, uint32_t src_addr, size_t size){
memcpy(dst, &memory[src_addr], size);
return size;
}
size_t Emulator::write_data( uint32_t dst_addr, void *src,size_t size){
memcpy(&memory[dst_addr], src, size);
return size;
}
void Emulator::set_dtreg(enum dtreg_t n, uint16_t sel, uint32_t base, uint16_t limit){
dtregs[n].selector = sel;
dtregs[n].base_addr = base;
dtregs[n].table_limit = limit;
}
void Emulator::set_ldtr(uint16_t sel){
uint32_t gdt_base, base;
uint16_t gdt_limit, limit;
LDTDesc ldt;
gdt_base = dtregs[GDTR].base_addr;
gdt_limit = dtregs[GDTR].table_limit;
read_data(&ldt, gdt_base + sel, sizeof(LDTDesc));
base = (ldt.base_h << 24) + (ldt.base_m << 16) + ldt.base_l;
limit = (ldt.limit_h << 16) + ldt.limit_l;
set_dtreg(LDTR, sel, base, limit);
}
void Emulator::set_gdtr(uint32_t base, uint16_t limit){
set_dtreg(GDTR, 0, base, limit);
}
void Emulator::set_idtr(uint32_t base, uint16_t limit){
set_dtreg(IDTR, 0, base, limit);
}
//各レジスタの初期化
void Emulator::InitRegisters(){
dtregs[GDTR].base_addr = 0x0000;
dtregs[GDTR].table_limit= 0xffff; // GDTRの初期値はIntel*3巻2.4.1参照
dtregs[IDTR].base_addr = 0x0000;
dtregs[IDTR].table_limit= 0xffff; // 同上2.4.3
}
void Emulator::LoadBinary(const char* fname, uint32_t addr, int size){
FILE *fp;
fp = fopen(fname, "rb");
if(fp == NULL){
return;
}
fread(memory + addr, 1, size, fp);
fclose(fp);
}
int Emulator::GetBitMode(){
return BitMode;
}
uint8_t Emulator::GetCode8(int index){
if(memory_size < (EIP +index)){
return 0xff;
}
return memory[sgregs[1].base + EIP + index];
}
int8_t Emulator::GetSignCode8(int index){
return (int8_t)GetCode8(index);
}
uint32_t Emulator::GetCode32(int index){
uint32_t ret = 0;
//リトルエンディアンでメモリの値を取得
for(int i=0; i<4; i++){
ret |= GetCode8(index + i) << (i * 8);
}
return ret;
}
int32_t Emulator::GetSignCode32(int index){
return (int32_t)GetCode32(index);
}
uint8_t Emulator::GetRegister8(int index){
if(index < 4){
return reg[index].low8;
}else{
return reg[index - 4].high8;
}
}
uint16_t Emulator::GetRegister16(int index){
return reg[index].reg16;
}
uint32_t Emulator::GetRegister32(int index){
return reg[index].reg32;
}
void Emulator::SetRegister8(int index, uint8_t val){
if(index < 4){
reg[index].low8 = val;
}else{
reg[index - 4].high8 = val;
}
}
void Emulator::SetRegister16(int index, uint16_t val){
reg[index].reg16 = val;
}
uint8_t Emulator::in_io8(uint16_t addr) {
uint8_t v = 0;
uint16_t base = get_portio_base(addr);
if(base)
v = port_io[base]->in8(addr);
return v;
}
uint16_t Emulator::get_portio_base(uint16_t addr){
for(int i=0; i<5; i++){
uint16_t base = (addr&(~1)) - (2*i);
if(port_io_map.count(base))
return addr < base+port_io_map[base] ? base : 0;
}
return 0;
}
void Emulator::io_out8(uint16_t addr, uint8_t value) {
uint16_t base = get_portio_base(addr);
if(base)
port_io[base]->out8(addr, value);
}
void Emulator::set_portio(uint16_t addr,size_t len, PortIO *dev){
addr &= ~1;
port_io[addr] = dev;
port_io_map[addr] = len;
}
void Emulator::set_interrupt(bool interrupt){
eflags.IF = interrupt;
}
void Emulator::SetRegister32(int index, uint32_t val){
reg[index].reg32 = val;
}
uint32_t Emulator::GetMemory8(uint32_t addr){
if(addr > memory_size){
return 0x00;
}
return memory[addr];
}
uint32_t Emulator::GetMemory16(uint32_t addr){
// little endian
uint32_t ret = 0;
for(int i=0; i<2; i++){
ret |= GetMemory8(addr + i) << (8 * i);
}
return ret;
}
uint32_t Emulator::GetMemory32(uint32_t addr){
// little endian
uint32_t ret = 0;
for(int i=0; i<4; i++){
ret |= GetMemory8(addr + i) << (8 * i);
}
return ret;
}
void Emulator::SetMemory8(uint32_t addr, uint32_t val){
if(addr > memory_size){
return;
}
memory[addr] = val & 0xFF;
return;
}
void Emulator::SetMemory16(uint32_t addr, uint32_t val){
//little endian
for(int i=0; i<2; i++){
SetMemory8(addr + i, val >> (i*8));
}
return;
}
void Emulator::SetMemory32(uint32_t addr, uint32_t val){
//little endian
for(int i=0; i<4; i++){
SetMemory8(addr + i, val >> (i*8));
}
return;
}
void Emulator::Push32(uint32_t val){
// uint32_t addr = /*GetRegister32(ESP)*/ ESP - 4;
// SetRegister32(ESP, addr);
// ESP = addr;
ESP = ESP - 4;
SetMemory32(ESP, val);
}
uint32_t Emulator::Pop32(){
uint32_t addr = ESP;//GetRegister32(ESP);
uint32_t ret = GetMemory32(addr);
// SetRegister32(ESP, addr + 4);
ESP = addr + 4;
return ret;
}
void Emulator::DumpRegisters(int bit){
switch(bit){
case 16:
for(int i=0; i<REGISTERS_COUNT; i++){
}
break;
case 32:
for(int i=0; i<REGISTERS_COUNT; i++){
}
for(int i=0; i<4; i++){
}
break;
//case 64:
// break;
default:
break;
}
return;
}
void Emulator::DumpRegisters(){
int bit = GetBitMode();
DumpRegisters(bit);
return;
}
void Emulator::DumpMemory(const char *fname, uint32_t addr, uint32_t size){
if(memory_size < sgregs[1].base + addr)
return;
if(memory_size < sgregs[1].base + addr+size){
size = memory_size - sgregs[1].base + addr;
}
FILE *fp;
fp = fopen(fname, "wb");
fwrite(memory+addr, sizeof(char), size, fp);
fclose(fp);
}