Skip to content

Commit dca14c9

Browse files
committed
add vmm_get and vmm_set
1 parent b735492 commit dca14c9

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

include/hv.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ typedef enum {
7575
VMM_X64_REGISTERS_MAX,
7676
} vmm_x64_reg_t;
7777

78+
enum {
79+
VMM_CTRL_EXIT_REASON,
80+
};
81+
82+
enum {
83+
VMM_EXIT_HLT,
84+
VMM_EXIT_IO,
85+
VMM_EXIT_REASONS_MAX,
86+
};
87+
7888
int vmm_create(void);
7989
int vmm_destroy(void);
8090

@@ -90,6 +100,9 @@ int vmm_cpu_write_register(vmm_x64_reg_t reg, uint64_t value);
90100
int vmm_cpu_read_msr(uint32_t msr, uint64_t *value);
91101
int vmm_cpu_write_msr(uint32_t msr, uint64_t value);
92102

103+
int vmm_get(int id, uint64_t *value);
104+
int vmm_set(int id, uint64_t value);
105+
93106
#ifdef __cplusplus
94107
}
95108
#endif

src/hv_kvm.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,18 @@ vmm_cpu_write_register(vmm_x64_reg_t reg, uint64_t value)
188188

189189
return 0;
190190
}
191+
192+
int
193+
vmm_get(int id, uint64_t *value)
194+
{
195+
assert(id == VMM_CTRL_EXIT_REASON);
196+
197+
switch (run->exit_reason) {
198+
case KVM_EXIT_HLT: *value = VMM_EXIT_HLT; break;
199+
case KVM_EXIT_IO: *value = VMM_EXIT_IO; break;
200+
default:
201+
*value = VMM_EXIT_REASONS_MAX;
202+
return -1;
203+
}
204+
return 0;
205+
}

test/main.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,26 @@ int main(void)
6767
/* Repeatedly run code and handle VM exits. */
6868
while (1) {
6969
vmm_cpu_run();
70-
switch (run->exit_reason) {
71-
case KVM_EXIT_HLT:
70+
uint64_t exit_reason;
71+
vmm_get(VMM_CTRL_EXIT_REASON, &exit_reason);
72+
switch (exit_reason) {
73+
case VMM_EXIT_HLT:
7274
puts("KVM_EXIT_HLT");
7375
return 0;
74-
case KVM_EXIT_IO:
75-
if (run->io.direction == KVM_EXIT_IO_OUT && run->io.size == 1 && run->io.port == 0x3f8 && run->io.count == 1)
76-
putchar(*(((char *)run) + run->io.data_offset));
77-
else
78-
errx(1, "unhandled KVM_EXIT_IO");
79-
break;
80-
case KVM_EXIT_FAIL_ENTRY:
81-
errx(1, "KVM_EXIT_FAIL_ENTRY: hardware_entry_failure_reason = 0x%llx",
82-
(unsigned long long)run->fail_entry.hardware_entry_failure_reason);
83-
case KVM_EXIT_INTERNAL_ERROR:
84-
errx(1, "KVM_EXIT_INTERNAL_ERROR: suberror = 0x%x", run->internal.suberror);
76+
/* case KVM_EXIT_IO: */
77+
/* if (run->io.direction == KVM_EXIT_IO_OUT && run->io.size == 1 && run->io.port == 0x3f8 && run->io.count == 1) */
78+
/* putchar(*(((char *)run) + run->io.data_offset)); */
79+
/* else */
80+
/* errx(1, "unhandled KVM_EXIT_IO"); */
81+
/* break; */
82+
/* case KVM_EXIT_FAIL_ENTRY: */
83+
/* errx(1, "KVM_EXIT_FAIL_ENTRY: hardware_entry_failure_reason = 0x%llx", */
84+
/* (unsigned long long)run->fail_entry.hardware_entry_failure_reason); */
85+
/* case KVM_EXIT_INTERNAL_ERROR: */
86+
/* errx(1, "KVM_EXIT_INTERNAL_ERROR: suberror = 0x%x", run->internal.suberror); */
8587
default:
86-
errx(1, "exit_reason = 0x%x", run->exit_reason);
88+
fprintf(stderr, "exit_reason = 0x%x", exit_reason);
89+
abort();
8790
}
8891
}
8992
}

0 commit comments

Comments
 (0)