Skip to content

Commit b47d864

Browse files
committed
fix test
1 parent a214623 commit b47d864

File tree

4 files changed

+89
-25
lines changed

4 files changed

+89
-25
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ include_directories(include)
88

99
include(${CMAKE_SYSTEM_NAME}.cmake)
1010

11+
enable_testing()
1112
add_subdirectory(test)

src/vmm_kvm.c

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
#include <sys/stat.h>
1212
#include <sys/types.h>
1313

14-
int kvm, vmfd;
15-
struct kvm_run *run;
16-
struct kvm_regs regs;
17-
struct kvm_sregs sregs;
18-
int vcpufd;
14+
static int kvm, vmfd;
15+
static struct kvm_run *run;
16+
static struct kvm_regs regs;
17+
static struct kvm_sregs sregs;
18+
static int vcpufd;
1919

2020
int
2121
vmm_create(void)
2222
{
2323
int ret;
2424

2525
if ((kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC)) < 0)
26-
return VMM_ERROR;
26+
return VMM_ENOTSUP;
2727

2828
/* check API version */
2929
if ((ret = ioctl(kvm, KVM_GET_API_VERSION, NULL)) < 0)
@@ -65,7 +65,6 @@ vmm_cpu_create()
6565
/* Map the shared kvm_run structure and following data. */
6666
if ((mmap_size = ioctl(kvm, KVM_GET_VCPU_MMAP_SIZE, NULL)) < 0)
6767
return VMM_ERROR;
68-
assert(mmap_size < sizeof(*run))
6968
if ((run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, vcpufd, 0)) == 0)
7069
return VMM_ERROR;
7170

@@ -191,6 +190,72 @@ vmm_cpu_write_register(vmm_x64_reg_t reg, uint64_t value)
191190
return 0;
192191
}
193192

193+
int
194+
vmm_cpu_read_register(vmm_x64_reg_t reg, uint64_t *value)
195+
{
196+
if (ioctl(vcpufd, KVM_GET_REGS, &regs) < 0)
197+
return VMM_ERROR;
198+
if (ioctl(vcpufd, KVM_GET_SREGS, &sregs) < 0)
199+
return VMM_ERROR;
200+
201+
switch (reg) {
202+
case VMM_X64_RIP: *value = regs.rip; break;
203+
case VMM_X64_RFLAGS: *value = regs.rflags; break;
204+
case VMM_X64_RAX: *value = regs.rax; break;
205+
case VMM_X64_RBX: *value = regs.rbx; break;
206+
case VMM_X64_RCX: *value = regs.rcx; break;
207+
case VMM_X64_RDX: *value = regs.rdx; break;
208+
case VMM_X64_RSI: *value = regs.rsi; break;
209+
case VMM_X64_RDI: *value = regs.rdi; break;
210+
case VMM_X64_RSP: *value = regs.rsp; break;
211+
case VMM_X64_RBP: *value = regs.rbp; break;
212+
case VMM_X64_R8:
213+
case VMM_X64_R9:
214+
case VMM_X64_R10:
215+
case VMM_X64_R11:
216+
case VMM_X64_R12:
217+
case VMM_X64_R13:
218+
case VMM_X64_R14:
219+
case VMM_X64_R15:
220+
case VMM_X64_CS:
221+
case VMM_X64_SS:
222+
case VMM_X64_DS:
223+
case VMM_X64_ES:
224+
case VMM_X64_FS:
225+
case VMM_X64_GS:
226+
case VMM_X64_IDT_BASE:
227+
case VMM_X64_IDT_LIMIT:
228+
case VMM_X64_GDT_BASE:
229+
case VMM_X64_GDT_LIMIT:
230+
case VMM_X64_LDTR:
231+
case VMM_X64_LDT_BASE:
232+
case VMM_X64_LDT_LIMIT:
233+
case VMM_X64_LDT_AR:
234+
case VMM_X64_TR:
235+
case VMM_X64_TSS_BASE:
236+
case VMM_X64_TSS_LIMIT:
237+
case VMM_X64_TSS_AR:
238+
case VMM_X64_CR0:
239+
case VMM_X64_CR1:
240+
case VMM_X64_CR2:
241+
case VMM_X64_CR3:
242+
case VMM_X64_CR4:
243+
case VMM_X64_DR0:
244+
case VMM_X64_DR1:
245+
case VMM_X64_DR2:
246+
case VMM_X64_DR3:
247+
case VMM_X64_DR4:
248+
case VMM_X64_DR5:
249+
case VMM_X64_DR6:
250+
case VMM_X64_DR7:
251+
case VMM_X64_TPR:
252+
case VMM_X64_XCR0:
253+
default:
254+
assert(false);
255+
}
256+
return 0;
257+
}
258+
194259
int
195260
vmm_get(int id, uint64_t *value)
196261
{

test/CMakeLists.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
cmake_minimum_required(VERSION 2.4)
2-
3-
project(hv_test)
4-
5-
set(CMAKE_C_FLAGS_RELEASE "-Wall -Wextra -O2")
6-
set(CMAKE_C_FLAGS_DEBUG "-Wall -Wextra -g -O0")
7-
81
add_executable(main main.c)
9-
target_link_libraries(main hv)
2+
target_link_libraries(main vmm)
103

11-
add_test(test_main COMMAND main)
4+
add_test(NAME test_main COMMAND $<TARGET_FILE:main>)

test/main.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* IN THE SOFTWARE.
2525
*/
2626

27-
#include "hv.h"
27+
#include <vmm.h>
2828
#include <stdio.h>
2929
#include <stdlib.h>
3030
#include <string.h>
@@ -73,25 +73,30 @@ int main(void)
7373
/* Repeatedly run code and handle VM exits. */
7474
while (1) {
7575
vmm_cpu_run();
76-
uint64_t exit_reason;
76+
uint64_t exit_reason, value;
7777
vmm_get(VMM_CTRL_EXIT_REASON, &exit_reason);
7878
switch (exit_reason) {
7979
case VMM_EXIT_HLT:
8080
puts("KVM_EXIT_HLT");
8181
return 0;
82-
/* case KVM_EXIT_IO: */
83-
/* if (run->io.direction == KVM_EXIT_IO_OUT && run->io.size == 1 && run->io.port == 0x3f8 && run->io.count == 1) */
84-
/* putchar(*(((char *)run) + run->io.data_offset)); */
85-
/* else */
86-
/* errx(1, "unhandled KVM_EXIT_IO"); */
87-
/* break; */
82+
case VMM_EXIT_IO:
83+
//if (run->io.direction == KVM_EXIT_IO_OUT && run->io.size == 1 && run->io.port == 0x3f8 && run->io.count == 1)
84+
// putchar(*(((char *)run) + run->io.data_offset));
85+
//else
86+
// errx(1, "unhandled KVM_EXIT_IO");
87+
//break;
88+
vmm_cpu_read_register(VMM_X64_RAX, &value);
89+
putchar((char) value);
90+
break;
8891
/* case KVM_EXIT_FAIL_ENTRY: */
8992
/* errx(1, "KVM_EXIT_FAIL_ENTRY: hardware_entry_failure_reason = 0x%llx", */
9093
/* (unsigned long long)run->fail_entry.hardware_entry_failure_reason); */
9194
/* case KVM_EXIT_INTERNAL_ERROR: */
9295
/* errx(1, "KVM_EXIT_INTERNAL_ERROR: suberror = 0x%x", run->internal.suberror); */
9396
default:
94-
fprintf(stderr, "exit_reason = 0x%llx", exit_reason);
97+
vmm_cpu_read_register(VMM_X64_RIP, &value);
98+
fprintf(stderr, "ip = 0x%lx\n", value);
99+
fprintf(stderr, "exit_reason = 0x%lx", exit_reason);
95100
abort();
96101
}
97102
}

0 commit comments

Comments
 (0)