-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghyper.cpp
226 lines (163 loc) · 4.78 KB
/
ghyper.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
#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"
#include "ghyper.h"
#include <gtest/gtest.h>
#include <pthread.h>
#include <errno.h>
//#define DEBUG
#define QUIET
#define INTERNAL_BOXFILL
//#define TEST_VRAM
#define HARIBOTE_UI
using namespace std;
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
Emulator *emu;
PIC *pic;
keyboard *kb;
Interrupt *inter;
GUI *gui;
Display *disp;
extern "C" void _pc(uintptr_t, int);
void trap(int sig_num, siginfo_t * info, void * ucontext){
sig_ucontext_t* uc = (sig_ucontext_t *) ucontext;
emu->evacuateRegister(uc);
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);
hinstruction_func_t* func;
uint8_t * pc = (uint8_t *)uc->uc_mcontext.rip;
printf("opecode : %x\n", *pc);
printf("opecode : %x\n", *(pc + 1));
printf("rip: %p\n", (void *)uc->uc_mcontext.rip);
printf("emu->memory : %p\n", emu->memory);
emu->instr.prefix = emu->parse_prefix(emu, uc);
func = hinstructions16[*pc];
#ifndef QUIET
cout<<"emu: ";
cout<<"EIP = "<<hex<<showbase<<emu->EIP<<", ";
cout<<"Code = "<<(uint32_t)emu->instr.opcode<<endl;
#endif
printf("address : %lx\n", pc - emu->memory - 0x28001b);
if(func == NULL){
cout<<"命令("<<showbase<<__builtin_bswap64(*pc)<<")は実装されていません。"<<endl;
exit(1);
}else{
//execute
func(emu, uc);
}
if(emu->EIP > emu->GetMemSize()){
cout<<"out of memory."<<endl;
}
emu->returnRegister(uc);
//emu->DumpRegisters(32);
//emu->DumpMemory("memdump.bin");
}
void *thread_hv(void *arg){
struct sigaction sigact;
int fill = sigfillset(&sigact.sa_mask);
static char handler_stack[0xffff];
stack_t ss ;
ss.ss_flags = 0;
ss.ss_size = 0xffff;
ss.ss_sp = handler_stack;
sigaltstack(&ss, 0);
memset(&sigact, 0, sizeof(sigact));
sigact.sa_sigaction = trap;
sigact.sa_flags = SA_RESTART | SA_SIGINFO | SA_NODEFER | SA_ONSTACK;
int rc = sigaction(SIGILL, &sigact, (struct sigaction *)NULL);
int sc = sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL);
printf("hoge : %lx\n", emu->memory + emu->EIP);
#ifdef DEBUG
/*
typedef int (*jit)(void);
jit func = (jit)emu->memory;
func();
*/
_pc((uintptr_t)emu->memory, 0x7c00);
#else
void *addr = alloca(16384);
__asm("jmpq %0" : : "r"((uintptr_t)emu->memory + emu->EIP) : );
#endif
}
int Ghyper::hv(uint8_t asmb[15]) {
cout << "プロセス生成" << endl;
emu = new Emulator();
pic = new PIC();
//kb = new keyboard();
//init ES
emu->sgregs[0].base = 0x820; //0 == Es
inter = new Interrupt();
cout<<"emulator created."<<endl;
#ifdef DEBUG
emu->memory = asmb;
emu->EIP = 0;
#else
emu->LoadBinary("/home/a/haribote/harib27f/haribote.img", 0x7c00, 1024 * 1024 * 1024);
// 16bit ------------------------------------------------------------
std::memcpy(&emu->memory[0x280000], &emu->memory[0x4390 + 0x7c00], 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 = 0x28001b;
printf("dest : %x\n", dest);
emu->ESP = dest;
emu->memory[0xff2] = 8;
emu->SetMemory16(0xff4, 320);
emu->SetMemory16(0xff6, 200);
emu->SetMemory32(0xff8, 0xa0000);
// end 16bit -------------------------------------------------------------
#endif
printf("emu->memory : %p \n", emu->memory);
//pthread
pthread_attr_t tattr;
int size;
int ret;
void *res;
pthread_t tid;
void *arg;
ret = pthread_attr_init(&tattr);
if (ret != 0)
handle_error_en(ret, "attar");
ret = pthread_attr_setstacksize(&tattr, 16384 );
if (ret != 0)
handle_error_en(ret, "size");
ret = pthread_create(&tid, &tattr, thread_hv, arg);
if (ret != 0)
handle_error_en(ret, "create");
ret = pthread_join(tid, &res);
if (ret != 0)
handle_error_en(ret, "join");
return (0);
}