-
Notifications
You must be signed in to change notification settings - Fork 9
/
emulator.c
150 lines (135 loc) · 3.68 KB
/
emulator.c
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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "emulator.h"
#include "lapic.h"
/* Util for Print Binary */
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
Emulator *create_emu(uint8_t *memory, uint32_t eip, uint32_t esp)
{
Emulator *emu = malloc(sizeof(Emulator));
/* Resets registers. */
memset(emu->registers, 0, sizeof(emu->registers));
memset(emu->segment_registers, 0, sizeof(emu->segment_registers));
memset(emu->control_registers, 0, sizeof(emu->control_registers));
emu->gdtr.base = 0;
emu->gdtr.limit = 0;
emu->idtr.base = 0;
emu->idtr.limit = 0;
emu->eip = eip;
emu->registers[ESP] = esp;
emu->int_r = 0;
emu->eflags = 0;
/* Devices */
emu->lapic = create_lapic(emu);
emu->memory = memory;
emu->disk = NULL;
/* Utility */
emu->is_pe = 0;
emu->is_pg = 0;
emu->int_enabled = 0;
emu->exception = NO_ERR;
return emu;
}
void destroy_emu(Emulator *emu)
{
free(emu->memory);
free(emu);
}
void attach_disk(Emulator *emu, Disk *disk)
{
emu->disk = disk;
}
/*
* Loads first sector of binary file into emulator.
* Each index stores 1 byte of binary.
* BIOS places a sector ending with 0x55AA at 0x7c00.
* This check is not implemented intentionally.
* 0x200 (512) * 1 byte = 512 bytes
* fread(*ptr, byte, num(of byte to read), *stream)
*/
void load_boot_sector(Emulator *emu)
{
memcpy(emu->memory + 0x7c00, emu->disk->storage, 512);
}
char *register_names[] = {"EAX", "ECX", "EDX", "EBX", "ESP", "EBP", "ESI", "EDI"};
char *seg_register_names[] = {"ES", "CS", "SS", "DS", "FS", "GS"};
char *ctr_register_names[] = {"CR0", "CR1", "CR2", "CR3", "CR4"};
void dump_registers(Emulator *emu)
{
int i;
for (i = 0; i < REGISTERS_COUNT; i++)
{
printf("%s: %08x\n", register_names[i], emu->registers[i]); // %08x: prepends 0 to make 8 digits.
}
printf("EIP: %08x\n", emu->eip);
for (i = 0; i < SEGMENT_REGISTERS_COUNT; i++)
{
printf("%s: %08x\n", seg_register_names[i], emu->segment_registers[i]);
}
for (i = 0; i < CONTROL_REGISTER_COUNT; i++)
{
printf("%s: %08x\n", ctr_register_names[i], emu->control_registers[i]);
}
printf("GDTR: %04x %08x\n", emu->gdtr.limit, emu->gdtr.base);
printf("IDTR: %04x %08x\n", emu->idtr.limit, emu->idtr.base);
printf("TR: %04x\n", emu->tr);
}
// void dump_input(Emulator *emu)
// {
// printf("Input:\n");
// int i;
// for (i = 0; i < MEMORY_SIZE; i++)
// {
// if (emu->memory[i])
// {
// printf("%08x\n", emu->memory[i]);
// }
// }
// }
void dump_memory(Emulator *emu, int from, int len)
{
int i_per_line = 32;
int i = from;
int end_i = i + len;
int j;
printf("memory %X to %X:\n", i, end_i);
printf(" ");
for (j = 0; j < i_per_line; j++)
{
printf("%02X ", j);
}
printf("\n");
while (i < end_i)
{
printf("%08X: ", i);
for (j = 0; j < i_per_line; j++)
{
printf("%02X", emu->memory[i + j]);
printf(" ");
}
i += i_per_line;
printf("\n");
}
}
void dump_eflags(Emulator *emu)
{
int i;
printf("eflags:");
for (i = 3; i > -1; i--)
{
printf(BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(emu->eflags >> (8 * i)));
printf(" ");
}
printf("\n");
}