-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
313 lines (231 loc) · 5.79 KB
/
main.cpp
File metadata and controls
313 lines (231 loc) · 5.79 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/kernel.h>
#include <ucontext.h>
#include <signal.h>
#include <sys/wait.h>
#include <iostream>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include "Emulator.h"
#include "interrupt.h"
#include "device/PIC.h"
#include "GUI.h"
#include "device/Device.h"
#include "device/keyboard.h"
#include "queue"
#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
#define DEBUG
#define QUIET
#define INTERNAL_BOXFILL
//#define TEST_VRAM
#define HARIBOTE_UI
using namespace std;
Emulator *emu;
PIC *pic;
keyboard *kb;
Interrupt *inter;
GUI *gui;
Display *disp;
extern "C" void _pc(uintptr_t, int);
extern "C" void _iret();
typedef struct _sig_ucontext {
unsigned long uc_flags;
struct ucontext *uc_link;
stack_t uc_stack;
struct sigcontext uc_mcontext;
sigset_t uc_sigmask;
} sig_ucontext_t;
int osType = 0;
void _hoge(int i) {
}
void trap(int sig_num, siginfo_t * info, void * ucontext){
emu->AX = emu->EAX;
emu->AL = emu->EAX;
emu->AH = emu->EAX;
emu->DX = emu->EDX;
emu->DL = emu->EDX;
emu->DH = emu->EDX;
emu->CX = emu->ECX;
emu->CL = emu->ECX;
emu->CH = emu->ECX;
//like irq hardware polling
pic->chk_irq(emu);
//exec INT xx instruction
inter->exec_interrupt(pic, emu);
instruction_func_t* func;
_THIS_IP_;
sig_ucontext_t* uc = (sig_ucontext_t *) ucontext;
uint8_t * pc = (uint8_t *)uc->uc_mcontext.rip;
uc->uc_mcontext.rip++;
/*
void * sp = __builtin_frame_address(0);
struct _sig_ucontext *context = (struct _sig_ucontext*)(sp + 1);
unsigned char* pc;
pc = (unsigned char*)context->uc_mcontext.rip;
context->uc_mcontext.rip++;
*/
func = instructions16[*pc];
#ifndef QUIET
#endif
if(func == NULL){
}
//execute
func(emu);
if(emu->EIP > emu->GetMemSize()){
}
//emu->DumpRegisters(32);
//emu->DumpMemory("memdump.bin");
}
int main(int argc, char **argv){
int opt;
opterr = 1;
bool hypervisor = false;
while ((opt = getopt(argc, argv, "ho:")) != -1){
switch (opt) {
case 'o':
if(*optarg == 'x'){
osType = 1;
} else if(*optarg == 'h'){
osType = 0;
}
break;
case 'h':
hypervisor = true;
break;
default:
break;
}
}
if(hypervisor) {
int p_id, status;
// プロセスの生成
if ((p_id = fork()) == 0) {
emu = new Emulator();
pic = new PIC();
//kb = new keyboard();
inter = new Interrupt();
emu->LoadBinary("../xv6-public/xv6.img", 0x7c00, 1024 * 1024 * 1024);
struct sigaction sigact;
sigact.sa_sigaction = trap;
sigact.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL);
sigaction(SIGILL, &sigact, (struct sigaction *)NULL);
char buffer[500];
_pc((uintptr_t)emu->memory, 0x7c00);
delete emu;
delete pic;
delete inter;
exit(EXIT_SUCCESS);
}
// 親の処理
wait(&status); // 子プロセス全部終わるまで待つ
exit(EXIT_FAILURE);
}
if(osType == 0) { //hariboteOS
emu = new Emulator();
pic = new PIC();
kb = new keyboard();
inter = new Interrupt();
disp = new Display(emu->memory + VRAM_ADDR);
gui = new GUI(disp);
// getchar();
//iplとasmheadの処理
emu->LoadBinary(argv[1], 0x100000, 1440 * 1024);
std::memcpy(&emu->memory[0x280000], &emu->memory[0x104390], 512 * 1024);
uint32_t source;
uint32_t dest;
uint32_t interval;
std::memcpy(&source, &emu->memory[0x280014], 4);
std::memcpy(&dest, &emu->memory[0x28000c], 4 ); source += 0x280000;
std::memcpy(&interval, &emu->memory[0x280010], 4);
std::memcpy(&emu->memory[dest], &emu->memory[source], interval);
emu->EIP = 0x1b;
emu->ESP = dest;
//set vmode scrnx scrny vram
emu->memory[0xff2] = 8;
emu->SetMemory16(0xff4, 320);
emu->SetMemory16(0xff6, 200);
emu->SetMemory32(0xff8, 0xa0000);
// emu->memory[0xff8] = 0xa0;
gui->OpenWindow();
// emu->ESP = 0xffff;
// emu->DumpRegisters(32);
//emulation
emu->set_portio(0x20, 2, pic);
emu->set_portio(0x60, 1, kb);
emu->set_portio(0x60, 12, kb);
} else if(osType == 1){ //xv6
emu = new Emulator();
pic = new PIC();
//kb = new keyboard();
inter = new Interrupt();
disp = new Display(emu->memory + 0xA0000);
gui = new GUI(disp);
//BIOS
//set_dram
emu->LoadBinary("../xv6-public/xv6.img", 0x7c00, 1024 * 1024);
//set_pc
emu->EIP = 0x7c00;
emu->sgregs[1].base = 0x0;
}
for(size_t i = 0; true; i++){
emu->AX = emu->EAX;
emu->AL = emu->EAX;
emu->AH = emu->EAX;
emu->DX = emu->EDX;
emu->DL = emu->EDX;
emu->DH = emu->EDX;
emu->CX = emu->ECX;
emu->CL = emu->ECX;
emu->CH = emu->ECX;
//like irq hardware polling
pic->chk_irq(emu);
//exec INT xx instruction
inter->exec_interrupt(pic, emu);
emu->instr.prefix = emu->parse_prefix(emu);
emu->instr.opcode = emu->memory[emu->EIP + emu->sgregs[1].base];
instruction_func_t* func;
//two byte opecode
switch(emu->instr.opcode) {
case 0x0f:
emu->EIP++;
emu->instr.opcode = (emu->instr.opcode << 8) + (uint8_t)emu->GetSignCode8(0);
}
#ifndef QUIET
#endif
if(osType == 0) {
if(emu->instr.prefix) {
func = instructions32[emu->instr.opcode];
}else {
func = instructions16[emu->instr.opcode];
}
} else if(osType == 1) {
if(emu->instr.prefix && emu->is_16mode) {
func = instructions16[emu->instr.opcode];
}else {
func = instructions32[emu->instr.opcode];
}
}
if(func == NULL){
break;
}
//execute
func(emu);
if(emu->EIP == 0){
break;
}
if(emu->EIP > emu->GetMemSize()){
break;
}
}
emu->DumpRegisters(32);
emu->DumpMemory("memdump.bin");
delete emu;
delete gui;
delete disp;
return 0;
}