-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVirtualContext.cpp
More file actions
168 lines (144 loc) · 4.68 KB
/
VirtualContext.cpp
File metadata and controls
168 lines (144 loc) · 4.68 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
#include "VirtualContext.h"
#define HAX_EMUL_ONE 0x1
#define HAX_EMUL_REAL 0x2
#define HAX_EMUL_HLT 0x4
#define HAX_EMUL_EXITLOOP 0x5
void dump_vcpu_state(hax_vcpu_state *CPU)
{
vcpu_state_t state, *cpu=&state;
memset(&state, 0, sizeof(vcpu_state_t));
if (hax_sync_vcpu_state(CPU, &state, 0) < 0)
__debugbreak();
printf("eax=%08X ebx=%08X ecx=%08X edx=%08X\n", cpu->_eax, cpu->_ebx, cpu->_ecx, cpu->_edx);
printf("esp=%08X ebp=%08X esi=%08X edi=%08X\n", cpu->_esp, cpu->_ebp, cpu->_esi, cpu->_edi);
printf("eip=%08X elfags=%08X\n", cpu->_eip, cpu->_eflags);
printf("cr0=%08X cr2=%08X cr3=%08X cr4=%08X\n", cpu->_cr0, cpu->_cr2, cpu->_cr3, cpu->_cr4);
printf("dr0=%08X dr1=%08X dr2=%08X dr3=%08X\n", cpu->_dr0, cpu->_dr1, cpu->_dr2, cpu->_dr3);
printf("dr6=%08X dr7=%08X\n", cpu->_dr6, cpu->_dr7);
printf("\n");
}
/*
* Request the HAX kernel module to run the CPU for us until one of
* the following occurs:
* 1. Guest crashes or is shut down
* 2. We need QEMU's emulation like when the guest executes a MMIO
* instruction or guest enters emulation mode (non-PG mode)
* 3. Guest executes HLT
* 4. Qemu has Signal/event pending
* 5. An unknown VMX-exit happens
*/
int hax_vcpu_interrupt(hax_vcpu_state *CPU);
int VCpu_Exec(hax_vcpu_state *CPU)
{
int ret = 0;
hax_tunnel *ht = CPU->tunnel;
dump_vcpu_state(CPU);
do
{
hax_vcpu_interrupt(CPU);
int haxResult = hax_vcpu_run(CPU);
dump_vcpu_state(CPU);
// Simply continue the vcpu_run if system call interrupted
if (haxResult == -EINTR || haxResult == -EAGAIN)
{
printf("IO window interrupted\n");
continue;
}
// Fail if system call failed
if (haxResult < 0)
{
printf("vCPU run failed for vCPU %x\n", CPU->vcpu_id);
abort();
}
switch (ht->_exit_status)
{
// Regular I/O
case HAX_EXIT_IO:
/*
{
ret = hax_handle_io(env, ht->pio._df, ht->pio._port,
ht->pio._direction,
ht->pio._size, ht->pio._count, vcpu->iobuf);
}
*/
break;
// Memory-mapped I/O
case HAX_EXIT_MMIO:
ret = HAX_EMUL_ONE;
break;
// Fast memory-mapped I/O
case HAX_EXIT_FAST_MMIO:
//ret = hax_handle_fastmmio(env, (hax_fastmmio *)vcpu->iobuf);
break;
// Guest running in real mode
case HAX_EXIT_REAL:
ret = HAX_EMUL_REAL;
break;
// Guest state changed, currently only for shutdown
case HAX_EXIT_STATECHANGE:
printf("VCPU shutdown request\n");
ret = HAX_EMUL_EXITLOOP;
break;
case HAX_EXIT_UNKNOWN_VMEXIT:
dprint("Unknown VMX exit %x from guest\n", ht->_exit_reason);
//qemu_system_reset_request();
//hax_prepare_emulation(env);
//cpu_dump_state(env, stderr, fprintf, 0);
ret = HAX_EMUL_EXITLOOP;
break;
// HLT instruction executed
case HAX_EXIT_HLT:
/*
if (!(env->interrupt_request & CPU_INTERRUPT_HARD) &&
!(env->interrupt_request & CPU_INTERRUPT_NMI)) {
// hlt instruction with interrupt disabled is shutdown
env->eflags |= IF_MASK;
env->halted = 1;
env->exception_index = EXCP_HLT;
ret = HAX_EMUL_HLT;
}
*/
break;
// these situations will continue to the Hax module
case HAX_EXIT_INTERRUPT:
case HAX_EXIT_PAUSED:
break;
default:
printf("Unknown exit %x from Hax driver\n", ht->_exit_status);
ret = HAX_EMUL_EXITLOOP;
break;
}
}while (!ret);
return ret;
}
#define HAX_RAM_INFO_ROM 0x1
/*
static void set_v8086_seg(struct segment_desc_t *lhs, const SegmentCache *rhs)
{
memset(lhs, 0, sizeof(struct segment_desc_t ));
lhs->selector = rhs->selector;
lhs->base = rhs->base;
lhs->limit = rhs->limit;
lhs->type = 3;
lhs->present = 1;
lhs->dpl = 3;
lhs->operand_size = 0;
lhs->desc = 1;
lhs->long_mode = 0;
lhs->granularity = 0;
lhs->available = 0;
}
*/
void hax_msr_entry_set(struct vmx_msr *item, uint32_t index, uint64_t value)
{
item->entry = index;
item->value = value;
}
bool hax_get_fpu(hax_vcpu_state *CPU, fx_layout *FPU)
{
return hax_sync_fpu(CPU, FPU, 0) >= 0;
}
bool hax_set_fpu(hax_vcpu_state *CPU, fx_layout *FPU)
{
return hax_sync_fpu(CPU, FPU, 1) >= 0;
}