Skip to content

Commit a990149

Browse files
Sergio Gómez Del RealSergio Gómez Del Real
Sergio Gómez Del Real
authored and
Sergio Gómez Del Real
committed
working vm exit
1 parent f0c1bb7 commit a990149

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

monitor.c

+32-16
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,32 @@
1010
#include <Hypervisor/hv_vmx.h>
1111

1212
#define SEGM_SIZE 0xFFFF // size of real-mode segment
13-
#define CTRL_UNRSTR (1<<7) // To execute real-mode code
13+
14+
/* primary processor-based ctrl */
1415
#define CTRL_HALT_INSTR (1<<7) // VM exit when guest executes halt instruction
15-
#define CTRL_UNCON_IO (1<<24) // VM exit when guest executes IO instruction
16+
#define CTRL_CR8_LOAD (1<<19)
17+
#define CTRL_CR8_STORE (1<<20)
1618
#define CTRL_SECOND (1<<31) // enable secondary processor controls
1719
#define PRI_DEF1 (0x401E172) // default1's for primary
20+
#define PROC1_BITMAP (PRI_DEF1 | CTRL_HALT_INSTR | CTRL_UNCON_IO | CTRL_SECOND | CTRL_CR8_LOAD | CTRL_CR8_STORE)
21+
22+
/* secondary processor-based ctrl */
23+
#define CTRL_UNRSTR (1<<7) // To execute real-mode code
24+
#define PROC2_BITMAP CTRL_UNRSTR
25+
26+
/* pin-based ctrl */
27+
#define CTRL_UNCON_IO (1<<24) // VM exit when guest executes IO instruction
1828
#define PIN_DEF1 (0x16)
1929
#define PIN_BITMAP (PIN_DEF1 | 0x1)
20-
#define PROC1_BITMAP (PRI_DEF1 | CTRL_HALT_INSTR | CTRL_UNCON_IO | CTRL_SECOND) // primary processor-based ctrl
21-
#define PROC2_BITMAP CTRL_UNRSTR
2230

31+
/* map guest physical address to host virtual address */
2332
#define VM_MEM_MAP(uva, gpa, size, flags) \
2433
if ( (ret = hv_vm_map(uva, gpa, size, flags) != HV_SUCCESS)) { \
2534
print_err(ret); \
2635
exit(0); \
2736
}
2837

38+
/* execute vm represented by vcpu */
2939
#define HV_EXEC(vcpu) \
3040
if ( (ret = hv_vcpu_run(*vcpu)) != HV_SUCCESS) { \
3141
print_err(ret); \
@@ -93,7 +103,7 @@ static void vmcs_init_ctrl(hv_vcpuid_t *vcpu)
93103
/* set default1 bits and some capabilities */
94104

95105
read_caps(HV_VMX_CAP_PINBASED, &cap);
96-
write_vmcs(vcpu, VMCS_CTRL_PIN_BASED, PIN_BITMAP | ((cap & 0xffffffff) & (cap >> 32))); // VM-exit on external interrupts
106+
write_vmcs(vcpu, VMCS_CTRL_PIN_BASED, PIN_BITMAP | ((cap & 0xffffffff) & (cap >> 32)));
97107

98108
read_caps(HV_VMX_CAP_PROCBASED, &cap);
99109
write_vmcs(vcpu, VMCS_CTRL_CPU_BASED, (PROC1_BITMAP | (cap & 0xffffffff)) & (cap >> 32));
@@ -108,7 +118,6 @@ static void vmcs_init_ctrl(hv_vcpuid_t *vcpu)
108118
write_vmcs(vcpu, VMCS_CTRL_VMEXIT_CONTROLS, (0x36dff | (cap & 0xffffffff)) & (cap >> 32));
109119
}
110120

111-
/* incomplete */
112121
static void vmcs_init_guest(hv_vcpuid_t *vcpu)
113122
{
114123
write_vmcs(vcpu, VMCS_GUEST_RIP, 0x100); // load program segment at segment:offset -> 0x0:0x100
@@ -150,15 +159,20 @@ static void vmcs_init_guest(hv_vcpuid_t *vcpu)
150159
write_vmcs(vcpu, VMCS_GUEST_LDTR_LIMIT, SEGM_SIZE);
151160
write_vmcs(vcpu, VMCS_GUEST_LDTR_AR, 0x82);
152161
write_vmcs(vcpu, VMCS_GUEST_GDTR_BASE, 0);
162+
write_vmcs(vcpu, VMCS_GUEST_GDTR_LIMIT, SEGM_SIZE);
153163
write_vmcs(vcpu, VMCS_GUEST_IDTR_BASE, 0);
164+
write_vmcs(vcpu, VMCS_GUEST_IDTR_LIMIT, SEGM_SIZE);
154165

155-
write_vmcs(vcpu, VMCS_GUEST_CR0, 0x60000010); // in particular, PE and PG disabled; execute in real-mode
166+
write_vmcs(vcpu, VMCS_GUEST_CR0, 0x20); // in particular, PE and PG disabled; execute in real-mode
156167
write_vmcs(vcpu, VMCS_GUEST_CR3, 0x0);
157168
write_vmcs(vcpu, VMCS_GUEST_DR7, 0x0);
158-
write_vmcs(vcpu, VMCS_GUEST_SYSENTER_EIP, 0x6826);
159-
write_vmcs(vcpu, VMCS_GUEST_SYSENTER_ESP, 0x6824);
160169
write_vmcs(vcpu, VMCS_GUEST_CR4, 1L<<13);
161170

171+
write_vmcs(vcpu, VMCS_CTRL_EXC_BITMAP, 0xffffffff);
172+
write_vmcs(vcpu, VMCS_CTRL_CR0_MASK, 0x60000000);
173+
write_vmcs(vcpu, VMCS_CTRL_CR0_SHADOW, 0x0);
174+
write_vmcs(vcpu, VMCS_CTRL_CR4_MASK, 0x0);
175+
write_vmcs(vcpu, VMCS_CTRL_CR4_SHADOW, 0x0);
162176
}
163177

164178
int main(int argc, char *argv[])
@@ -205,18 +219,20 @@ int main(int argc, char *argv[])
205219

206220
/* Main loop: execute guest until VM exit */
207221
uint64_t exit_reas, err;
208-
while (1) {
222+
int stop = 0;
223+
while (!stop) {
209224
HV_EXEC(&vcpu);
210225
read_vmcs(&vcpu, VMCS_RO_EXIT_REASON, &exit_reas);
211-
//read_vmcs(&vcpu, VMCS_RO_EXIT_QUALIFIC, &exit_reas); // for debugging
212-
//read_vmcs(&vcpu, VMCS_RO_GUEST_LIN_ADDR, &exit_reas); // for debugging
213226
switch (exit_reas) {
214227
case VMX_REASON_HLT:
215-
;
216-
case VMX_REASON_IO:
217-
;
228+
printf("HLT\n");
229+
stop = 1;
230+
break;
231+
case VMX_REASON_IRQ:
232+
printf("INTERRUPT\n");
233+
break;
218234
case VMX_REASON_EPT_VIOLATION:
219-
hv_vm_protect(0x100, 0x100, HV_MEMORY_EXEC);
235+
;
220236
default:
221237
;
222238
}

0 commit comments

Comments
 (0)