From 8864cb0f3a9184e2420bbad64c43fcddf161e427 Mon Sep 17 00:00:00 2001 From: Zach Reizner Date: Tue, 16 Jan 2018 17:59:03 -0800 Subject: crosvm: add support for plugin process The plugin process is good for running a VM that depends substantially on devices that aren't implemented inside of crosvm. TEST=cargo build --features plugin; ./build_test BUG=chromium:800626 Change-Id: I7b4f656563742cd0bedc837205dd1240d497941d Reviewed-on: https://chromium-review.googlesource.com/869357 Commit-Ready: ChromeOS CL Exonerator Bot Tested-by: Zach Reizner Reviewed-by: Dylan Reid --- tests/plugin_adder.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++ tests/plugin_dirty_log.c | 180 ++++++++++++++++++++++++++++++++++++++ tests/plugin_ioevent.c | 197 +++++++++++++++++++++++++++++++++++++++++ tests/plugin_irqfd.c | 218 ++++++++++++++++++++++++++++++++++++++++++++++ tests/plugins.rs | 115 ++++++++++++++++++++++++ 5 files changed, 932 insertions(+) create mode 100644 tests/plugin_adder.c create mode 100644 tests/plugin_dirty_log.c create mode 100644 tests/plugin_ioevent.c create mode 100644 tests/plugin_irqfd.c create mode 100644 tests/plugins.rs (limited to 'tests') diff --git a/tests/plugin_adder.c b/tests/plugin_adder.c new file mode 100644 index 0000000..f6c514f --- /dev/null +++ b/tests/plugin_adder.c @@ -0,0 +1,222 @@ +/* + * Copyright 2017 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "crosvm.h" + +#ifndef F_LINUX_SPECIFIC_BASE +#define F_LINUX_SPECIFIC_BASE 1024 +#endif + +#ifndef F_ADD_SEALS +#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) +#endif + +#ifndef F_SEAL_SHRINK +#define F_SEAL_SHRINK 0x0002 +#endif + +#define SERIAL_ADDRESS 0x3f8 +#define KILL_ADDRESS 0x3f9 + +char g_serial_out[16]; +int g_kill_evt; + +void *vcpu_thread(void *arg) { + struct crosvm_vcpu *vcpu = arg; + struct crosvm_vcpu_event evt; + int i = 0; + while (crosvm_vcpu_wait(vcpu, &evt) == 0) { + if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) { + struct kvm_sregs sregs; + crosvm_vcpu_get_sregs(vcpu, &sregs); + sregs.cs.base = 0; + sregs.cs.selector = 0; + sregs.es.base = KILL_ADDRESS; + sregs.es.selector = 0; + crosvm_vcpu_set_sregs(vcpu, &sregs); + + struct kvm_regs regs; + crosvm_vcpu_get_regs(vcpu, ®s); + regs.rip = 0x1000; + regs.rax = 2; + regs.rbx = 7; + regs.rflags = 2; + crosvm_vcpu_set_regs(vcpu, ®s); + } + if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS) { + if (evt.io_access.address_space == CROSVM_ADDRESS_SPACE_IOPORT && + evt.io_access.address == SERIAL_ADDRESS && + evt.io_access.is_write && + evt.io_access.length == 1) { + g_serial_out[i] = evt.io_access.data[0]; + i++; + } + if (evt.io_access.address_space == CROSVM_ADDRESS_SPACE_IOPORT && + evt.io_access.address == KILL_ADDRESS && + evt.io_access.is_write && + evt.io_access.length == 1 && + evt.io_access.data[0] == 1) + { + uint64_t dummy = 1; + write(g_kill_evt, &dummy, sizeof(dummy)); + return NULL; + } + } + + crosvm_vcpu_resume(vcpu); + } + + return NULL; +} + +int main(int argc, char** argv) { + const uint8_t code[] = { + /* + 0000 BAF803 mov dx,0x3f8 + 0003 00D8 add al,bl + 0005 0430 add al,0x30 + 0007 EE out dx,al + 0008 B05C mov al,0x0a + 000A EE out dx,al + 000B BAF903 mov dx,0x3f9 + 000E B001 mov al,0x1 + 0010 EE out dx,al + 0011 F4 hlt + */ + 0xba, 0xf8, 0x03, + 0x00, 0xd8, + 0x04, '0', + 0xee, + 0xb0, '\n', + 0xee, + 0xba, 0xf9, 0x03, + 0xb0, 0x01, + 0xee, + 0xf4 + }; + + struct crosvm *crosvm; + int ret = crosvm_connect(&crosvm); + if (ret) { + fprintf(stderr, "failed to connect to crosvm: %d\n", ret); + return 1; + } + + /* + * Not strictly necessary, but demonstrates we can have as many connections + * as we please. + */ + struct crosvm *extra_crosvm; + ret = crosvm_new_connection(crosvm, &extra_crosvm); + if (ret) { + fprintf(stderr, "failed to make new socket: %d\n", ret); + return 1; + } + + /* We needs this eventfd to know when to exit before being killed. */ + g_kill_evt = crosvm_get_shutdown_eventfd(crosvm); + if (g_kill_evt < 0) { + fprintf(stderr, "failed to get kill eventfd: %d\n", g_kill_evt); + return 1; + } + + ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, SERIAL_ADDRESS, 1); + if (ret) { + fprintf(stderr, "failed to reserve ioport range: %d\n", ret); + return 1; + } + + ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, KILL_ADDRESS, 1); + if (ret) { + fprintf(stderr, "failed to reserve mmio range: %d\n", ret); + return 1; + } + + int mem_size = 0x2000; + int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING); + if (mem_fd < 0) { + fprintf(stderr, "failed to create guest memfd: %d\n", errno); + return 1; + } + ret = ftruncate(mem_fd, mem_size); + if (ret) { + fprintf(stderr, "failed to set size of guest memory: %d\n", errno); + return 1; + } + uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0x1000); + if (mem == MAP_FAILED) { + fprintf(stderr, "failed to mmap guest memory: %d\n", errno); + return 1; + } + fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK); + memcpy(mem, code, sizeof(code)); + + struct crosvm_memory *mem_obj; + ret = crosvm_create_memory(crosvm, mem_fd, 0x1000, 0x1000, 0x1000, false, &mem_obj); + if (ret) { + fprintf(stderr, "failed to create memory in crosvm: %d\n", ret); + return 1; + } + + /* get and creat a thread for each vcpu */ + struct crosvm_vcpu *vcpus[32]; + pthread_t vcpu_threads[32]; + uint32_t vcpu_count; + for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) { + ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]); + if (ret == -ENOENT) + break; + + if (ret) { + fprintf(stderr, "error while getting all vcpus: %d\n", ret); + return 1; + } + pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, vcpus[vcpu_count]); + } + + ret = crosvm_start(extra_crosvm); + if (ret) { + fprintf(stderr, "failed to tell crosvm to start: %d\n", ret); + return 1; + } + + /* Wait for crosvm to request that we exit otherwise we will be killed. */ + uint64_t dummy; + read(g_kill_evt, &dummy, 8); + + ret = crosvm_destroy_memory(crosvm, &mem_obj); + if (ret) { + fprintf(stderr, "failed to destroy memory in crosvm: %d\n", ret); + return 1; + } + + ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, SERIAL_ADDRESS, 0); + if (ret) { + fprintf(stderr, "failed to unreserve ioport range: %d\n", ret); + return 1; + } + + ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, KILL_ADDRESS, 0); + if (ret) { + fprintf(stderr, "failed to unreserve mmio range: %d\n", ret); + return 1; + } + + return strcmp(g_serial_out, "9\n"); +} diff --git a/tests/plugin_dirty_log.c b/tests/plugin_dirty_log.c new file mode 100644 index 0000000..bc3db83 --- /dev/null +++ b/tests/plugin_dirty_log.c @@ -0,0 +1,180 @@ +/* + * Copyright 2017 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "crosvm.h" + +#ifndef F_LINUX_SPECIFIC_BASE +#define F_LINUX_SPECIFIC_BASE 1024 +#endif + +#ifndef F_ADD_SEALS +#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) +#endif + +#ifndef F_SEAL_SHRINK +#define F_SEAL_SHRINK 0x0002 +#endif + +#define LOAD_ADDRESS 0x1000 +#define SI_ADDRESS 0x8000 +#define BL_VALUE 0x12 +#define KILL_ADDRESS 0x9000 + +int g_kill_evt; + +void *vcpu_thread(void *arg) { + struct crosvm_vcpu *vcpu = arg; + struct crosvm_vcpu_event evt; + int i = 0; + while (crosvm_vcpu_wait(vcpu, &evt) == 0) { + if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) { + struct kvm_sregs sregs; + crosvm_vcpu_get_sregs(vcpu, &sregs); + sregs.cs.base = 0; + sregs.cs.selector = 0; + sregs.es.base = KILL_ADDRESS; + sregs.es.selector = 0; + crosvm_vcpu_set_sregs(vcpu, &sregs); + + struct kvm_regs regs; + crosvm_vcpu_get_regs(vcpu, ®s); + regs.rflags = 2; + regs.rip = LOAD_ADDRESS; + regs.rbx = BL_VALUE; + regs.rsi = SI_ADDRESS; + crosvm_vcpu_set_regs(vcpu, ®s); + } + + if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS && + evt.io_access.address_space == CROSVM_ADDRESS_SPACE_MMIO && + evt.io_access.address == KILL_ADDRESS && + evt.io_access.is_write && + evt.io_access.length == 1 && + evt.io_access.data[0] == 1) + { + uint64_t dummy = 1; + write(g_kill_evt, &dummy, sizeof(dummy)); + return NULL; + } + + crosvm_vcpu_resume(vcpu); + } + + return NULL; +} + +int main(int argc, char** argv) { + const uint8_t code[] = { + /* + 0000 881C mov [si],bl + 0014 26C606000001 mov byte [es:0x0],0x1 + 0002 F4 hlt + */ + 0x88, 0x1c, 0x26, 0xc6, 0x06, 0x00, 0x00, 0x01, 0xf4 + }; + + struct crosvm *crosvm; + int ret = crosvm_connect(&crosvm); + if (ret) { + fprintf(stderr, "failed to connect to crosvm: %d\n", ret); + return 1; + } + + g_kill_evt = crosvm_get_shutdown_eventfd(crosvm); + if (g_kill_evt < 0) { + fprintf(stderr, "failed to get kill eventfd: %d\n", g_kill_evt); + return 1; + } + + ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_MMIO, KILL_ADDRESS, 1); + if (ret) { + fprintf(stderr, "failed to reserve mmio range: %d\n", ret); + return 1; + } + + int mem_size = 0x9000; + int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING); + if (mem_fd < 0) { + fprintf(stderr, "failed to create guest memfd: %d\n", errno); + return 1; + } + ret = ftruncate(mem_fd, mem_size); + if (ret) { + fprintf(stderr, "failed to set size of guest memory: %d\n", errno); + return 1; + } + uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0); + if (mem == MAP_FAILED) { + fprintf(stderr, "failed to mmap guest memory: %d\n", errno); + return 1; + } + fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK); + memcpy(mem + LOAD_ADDRESS, code, sizeof(code)); + + struct crosvm_memory *mem_obj; + ret = crosvm_create_memory(crosvm, mem_fd, 0, mem_size, 0, false, &mem_obj); + if (ret) { + fprintf(stderr, "failed to create memory in crosvm: %d\n", ret); + return 1; + } + + struct crosvm_vcpu *vcpus[32]; + pthread_t vcpu_threads[32]; + uint32_t vcpu_count; + for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) { + ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]); + if (ret == -ENOENT) + break; + + if (ret) { + fprintf(stderr, "error while getting all vcpus: %d\n", ret); + return 1; + } + pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, vcpus[vcpu_count]); + } + + ret = crosvm_start(crosvm); + if (ret) { + fprintf(stderr, "failed to tell crosvm to start: %d\n", ret); + return 1; + } + + uint64_t dummy; + read(g_kill_evt, &dummy, 8); + + uint8_t dirty_log[2] = {0}; + ret = crosvm_memory_get_dirty_log(crosvm, mem_obj, dirty_log); + if (ret) { + fprintf(stderr, "failed to get dirty log: %d\n", ret); + return 1; + } + + if (dirty_log[1] != 0x1) { + fprintf(stderr, "dirty log does not have expected bits: %x\n", dirty_log[1]); + return 1; + } + + uint64_t val = *(uint64_t*)(&mem[SI_ADDRESS]); + if (val != BL_VALUE) { + fprintf(stderr, "memory does not have expected value %d\n", val); + return 1; + } + + return 0; +} diff --git a/tests/plugin_ioevent.c b/tests/plugin_ioevent.c new file mode 100644 index 0000000..3662810 --- /dev/null +++ b/tests/plugin_ioevent.c @@ -0,0 +1,197 @@ +/* + * Copyright 2017 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "crosvm.h" + +#ifndef F_LINUX_SPECIFIC_BASE +#define F_LINUX_SPECIFIC_BASE 1024 +#endif + +#ifndef F_ADD_SEALS +#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) +#endif + +#ifndef F_SEAL_SHRINK +#define F_SEAL_SHRINK 0x0002 +#endif + +#define LOAD_ADDRESS 0x1000 +#define DATAMATCH_VAL 0x88 +#define KILL_ADDRESS 0x4000 + +int g_kill_evt; + +void *vcpu_thread(void *arg) { + struct crosvm_vcpu *vcpu = arg; + struct crosvm_vcpu_event evt; + int i = 0; + while (crosvm_vcpu_wait(vcpu, &evt) == 0) { + if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) { + struct kvm_sregs sregs; + crosvm_vcpu_get_sregs(vcpu, &sregs); + sregs.cs.base = 0; + sregs.cs.selector = 0; + sregs.es.base = KILL_ADDRESS; + sregs.es.selector = 0; + crosvm_vcpu_set_sregs(vcpu, &sregs); + + struct kvm_regs regs; + crosvm_vcpu_get_regs(vcpu, ®s); + regs.rflags = 2; + regs.rip = LOAD_ADDRESS; + regs.rax = DATAMATCH_VAL; + regs.rbx = DATAMATCH_VAL - 1; + crosvm_vcpu_set_regs(vcpu, ®s); + } + + if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS && + evt.io_access.address_space == CROSVM_ADDRESS_SPACE_MMIO && + evt.io_access.address == KILL_ADDRESS && + evt.io_access.is_write && + evt.io_access.length == 1 && + evt.io_access.data[0] == 1) + { + uint64_t dummy = 1; + write(g_kill_evt, &dummy, sizeof(dummy)); + return NULL; + } + + crosvm_vcpu_resume(vcpu); + } + + return NULL; +} + +int main(int argc, char** argv) { + const uint8_t code[] = { + /* + 0000 BAF803 mov dx,0x3f8 + 0003 88C3 mov bl,al + 0005 EE out dx,al + 0006 B000 mov al,0x0 + 0008 EE out dx,al + 0009 88D8 mov al,bl + 000B EE out dx,al + 0014 26C606000001 mov byte [es:0x0],0x1 + 000C F4 hlt + */ + 0xba, 0xf8, 0x03, + 0x88, 0xc3, + 0xee, + 0xb0, 0x00, + 0xee, + 0x88, 0xd8, + 0xee, + 0x26, 0xc6, 0x06, 0x00, 0x00, 0x01, + 0xf4, + }; + + struct crosvm *crosvm; + int ret = crosvm_connect(&crosvm); + if (ret) { + fprintf(stderr, "failed to connect to crosvm: %d\n", ret); + return 1; + } + + g_kill_evt = crosvm_get_shutdown_eventfd(crosvm); + if (g_kill_evt < 0) { + fprintf(stderr, "failed to get kill eventfd: %d\n", g_kill_evt); + return 1; + } + + ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_MMIO, KILL_ADDRESS, 1); + if (ret) { + fprintf(stderr, "failed to reserve mmio range: %d\n", ret); + return 1; + } + + uint8_t datamatch = DATAMATCH_VAL; + struct crosvm_io *io; + ret = crosvm_create_io_event(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, 0x3f8, 1, &datamatch, &io); + if (ret) { + fprintf(stderr, "failed to create ioevent: %d\n", ret); + return 1; + } + + int ioeventfd = crosvm_io_event_fd(io); + + int mem_size = 0x4000; + int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING); + if (mem_fd < 0) { + fprintf(stderr, "failed to create guest memfd: %d\n", errno); + return 1; + } + ret = ftruncate(mem_fd, mem_size); + if (ret) { + fprintf(stderr, "failed to set size of guest memory: %d\n", errno); + return 1; + } + uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0); + if (mem == MAP_FAILED) { + fprintf(stderr, "failed to mmap guest memory: %d\n", errno); + return 1; + } + fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK); + memcpy(mem + LOAD_ADDRESS, code, sizeof(code)); + + struct crosvm_memory *mem_obj; + ret = crosvm_create_memory(crosvm, mem_fd, 0, mem_size, 0, false, &mem_obj); + if (ret) { + fprintf(stderr, "failed to create memory in crosvm: %d\n", ret); + return 1; + } + + /* get and creat a thread for each vcpu */ + struct crosvm_vcpu *vcpus[32]; + pthread_t vcpu_threads[32]; + uint32_t vcpu_count; + for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) { + ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]); + if (ret == -ENOENT) + break; + + if (ret) { + fprintf(stderr, "error while getting all vcpus: %d\n", ret); + return 1; + } + pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, vcpus[vcpu_count]); + } + + ret = crosvm_start(crosvm); + if (ret) { + fprintf(stderr, "failed to tell crosvm to start: %d\n", ret); + return 1; + } + + uint64_t dummy; + read(g_kill_evt, &dummy, 8); + + ret = read(ioeventfd, &dummy, sizeof(dummy)); + if (ret == -1) { + fprintf(stderr, "failed to read ioeventfd: %d\n", errno); + return 1; + } + + if (dummy != 2) { + fprintf(stderr, "ioeventfd was not triggered the expected number of times: %d\n", dummy); + return 1; + } + + return 0; +} diff --git a/tests/plugin_irqfd.c b/tests/plugin_irqfd.c new file mode 100644 index 0000000..c6951df --- /dev/null +++ b/tests/plugin_irqfd.c @@ -0,0 +1,218 @@ +/* + * Copyright 2017 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "crosvm.h" + +#ifndef F_LINUX_SPECIFIC_BASE +#define F_LINUX_SPECIFIC_BASE 1024 +#endif + +#ifndef F_ADD_SEALS +#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) +#endif + +#ifndef F_SEAL_SHRINK +#define F_SEAL_SHRINK 0x0002 +#endif + +#define LOAD_ADDRESS 0x1000 +#define STACK_BASE (LOAD_ADDRESS + 0x1000) +#define STACK_SIZE 0x1000 +#define SUCCESS_ADDRESS 0x3000 +#define KILL_ADDRESS 0x4000 + +/* +org 0x1000 +bits 16 + +cli + +; Set entry 0x0 in the interrupt vector table +mov word [0x0], handle +mov word [0x2], 0x0 + +sti + +; Loop until interrupt is handled +loop: + cmp byte [si], 0x01 + jne loop + +cli + +; Signal that we are ready to end +end: + mov byte [es:0], 0x01 + hlt + +; Handle the interrupt by halting +handle: + mov byte [si], 0x01 + iret +*/ +const uint8_t g_code[] = { + 0xfa, 0xc7, 0x06, 0x00, 0x00, 0x1b, 0x10, 0xc7, 0x06, 0x02, 0x00, 0x00, + 0x00, 0xfb, 0x80, 0x3c, 0x01, 0x75, 0xfb, 0xfa, 0x26, 0xc6, 0x06, 0x00, + 0x00, 0x01, 0xf4, 0xc6, 0x04, 0x01, 0xcf +}; + +struct vcpu_context { + struct crosvm_vcpu *vcpu; + int irqeventfd; + int kill_evt; +}; + +void *vcpu_thread(void *arg) { + struct vcpu_context *ctx = arg; + struct crosvm_vcpu *vcpu = ctx->vcpu; + struct crosvm_vcpu_event evt; + uint64_t dummy = 1; + int i = 0; + int ret; + while (crosvm_vcpu_wait(vcpu, &evt) == 0) { + if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) { + struct kvm_sregs sregs; + crosvm_vcpu_get_sregs(vcpu, &sregs); + sregs.cs.base = 0; + sregs.cs.selector = 0x0; + sregs.ss.base = 0; + sregs.ss.selector = 0x0; + sregs.es.base = KILL_ADDRESS; + sregs.es.selector = 0x0; + crosvm_vcpu_set_sregs(vcpu, &sregs); + + struct kvm_regs regs; + crosvm_vcpu_get_regs(vcpu, ®s); + regs.rflags = 2; + regs.rip = LOAD_ADDRESS; + regs.rsp = STACK_BASE + STACK_SIZE; + regs.rsi = SUCCESS_ADDRESS; + crosvm_vcpu_set_regs(vcpu, ®s); + + write(ctx->irqeventfd, &dummy, sizeof(dummy)); + } + + if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS && + evt.io_access.address_space == CROSVM_ADDRESS_SPACE_MMIO && + evt.io_access.address == KILL_ADDRESS && + evt.io_access.is_write && + evt.io_access.length == 1 && + evt.io_access.data[0] == 1) + { + write(ctx->kill_evt, &dummy, sizeof(dummy)); + return NULL; + } + + crosvm_vcpu_resume(vcpu); + } + + return NULL; +} + +int main(int argc, char** argv) { + int i; + uint64_t dummy = 1; + struct crosvm *crosvm; + int ret = crosvm_connect(&crosvm); + if (ret) { + fprintf(stderr, "failed to connect to crosvm: %d\n", ret); + return 1; + } + + int kill_evt = crosvm_get_shutdown_eventfd(crosvm); + if (kill_evt < 0) { + fprintf(stderr, "failed to get kill eventfd: %d\n", kill_evt); + return 1; + } + + crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_MMIO, KILL_ADDRESS, 1); + + struct crosvm_irq *irq; + ret = crosvm_create_irq_event(crosvm, 0, &irq); + if (ret) { + fprintf(stderr, "failed to create irq event: %d\n", ret); + return 1; + } + + int irqeventfd = crosvm_irq_event_get_fd(irq); + + int mem_size = 0x4000; + int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING); + if (mem_fd < 0) { + fprintf(stderr, "failed to create guest memfd: %d\n", errno); + return 1; + } + ret = ftruncate(mem_fd, mem_size); + if (ret) { + fprintf(stderr, "failed to set size of guest memory: %d\n", errno); + return 1; + } + uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0); + if (mem == MAP_FAILED) { + fprintf(stderr, "failed to mmap guest memory: %d\n", errno); + return 1; + } + fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK); + memcpy(mem + LOAD_ADDRESS, g_code, sizeof(g_code)); + + struct crosvm_memory *mem_obj; + ret = crosvm_create_memory(crosvm, mem_fd, 0, mem_size, 0, false, &mem_obj); + if (ret) { + fprintf(stderr, "failed to create memory in crosvm: %d\n", ret); + return 1; + } + + struct crosvm_vcpu *vcpus[32]; + struct vcpu_context ctxs[32]; + pthread_t vcpu_threads[32]; + uint32_t vcpu_count; + for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) { + ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]); + if (ret == -ENOENT) + break; + + if (ret) { + fprintf(stderr, "error while getting all vcpus: %d\n", ret); + return 1; + } + ctxs[vcpu_count].vcpu = vcpus[vcpu_count]; + ctxs[vcpu_count].irqeventfd = irqeventfd; + ctxs[vcpu_count].kill_evt = kill_evt; + pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, &ctxs[vcpu_count]); + } + + ret = crosvm_start(crosvm); + if (ret) { + fprintf(stderr, "failed to tell crosvm to start: %d\n", ret); + return 1; + } + + ret = read(kill_evt, &dummy, sizeof(dummy)); + if (ret == -1) { + fprintf(stderr, "failed to read kill eventfd: %d\n", errno); + return 1; + } + + if (mem[SUCCESS_ADDRESS] != 0x01) { + fprintf(stderr, "interrupt was not handled: 0x%x\n", mem[SUCCESS_ADDRESS]); + return 1; + } + + return 0; +} diff --git a/tests/plugins.rs b/tests/plugins.rs new file mode 100644 index 0000000..349634d --- /dev/null +++ b/tests/plugins.rs @@ -0,0 +1,115 @@ +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#![cfg(feature = "plugin")] + +extern crate rand; + +use rand::{thread_rng, Rng}; + +use std::ffi::OsString; +use std::fs::remove_file; +use std::io::Write; +use std::env::{current_exe, var_os}; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; +use std::thread::sleep; +use std::time::Duration; + +struct RemovePath(PathBuf); +impl Drop for RemovePath { + fn drop(&mut self) { + if let Err(e) = remove_file(&self.0) { + eprintln!("failed to remove path: {:?}", e); + } + } +} + +fn get_crosvm_path() -> PathBuf { + let mut crosvm_path = current_exe() + .ok() + .map(|mut path| { + path.pop(); + if path.ends_with("deps") { + path.pop(); + } + path + }) + .expect("failed to get crosvm binary directory"); + crosvm_path.push("crosvm"); + crosvm_path +} + +fn build_plugin(src: &str) -> RemovePath { + let mut out_bin = PathBuf::from("target"); + let mut libcrosvm_plugin = get_crosvm_path(); + libcrosvm_plugin.set_file_name("libcrosvm_plugin.so"); + out_bin.push(thread_rng() + .gen_ascii_chars() + .take(10) + .collect::()); + let mut child = Command::new(var_os("CC").unwrap_or(OsString::from("cc"))) + .args(&["-Icrosvm_plugin", "-pthread", "-o"]) + .arg(&out_bin) + .arg(libcrosvm_plugin) + .args(&["-xc", "-"]) + .stdin(Stdio::piped()) + .spawn() + .expect("failed to spawn compiler"); + { + let stdin = child.stdin.as_mut().expect("failed to open stdin"); + stdin + .write_all(src.as_bytes()) + .expect("failed to write source to stdin"); + } + + let status = child.wait().expect("failed to wait for compiler"); + assert!(status.success(), "failed to build plugin"); + + RemovePath(PathBuf::from(out_bin)) +} + +fn run_plugin(bin_path: &Path) { + let mut child = Command::new(get_crosvm_path()) + .args(&["run", "-c", "1", "--plugin"]) + .arg(bin_path) + .spawn() + .expect("failed to spawn crosvm"); + for _ in 0..12 { + match child.try_wait().expect("failed to wait for crosvm") { + Some(status) => { + assert!(status.success()); + return; + } + None => sleep(Duration::from_millis(100)), + } + } + child.kill().expect("failed to kill crosvm"); + panic!("crosvm process has timed out"); +} + +fn test_plugin(src: &str) { + let bin_path = build_plugin(src); + run_plugin(&bin_path.0); +} + +#[test] +fn test_adder() { + test_plugin(include_str!("plugin_adder.c")); +} + +#[test] +fn test_dirty_log() { + test_plugin(include_str!("plugin_dirty_log.c")); +} + +#[test] +fn test_ioevent() { + test_plugin(include_str!("plugin_ioevent.c")); +} + +#[test] +fn test_irqfd() { + test_plugin(include_str!("plugin_irqfd.c")); +} -- cgit 1.4.1