2024-07-21 15:46:26 +00:00
|
|
|
// This file contains KVM wrappers for Rust side. The reason we need these wrappers is because the
|
|
|
|
// KVM ioctls is not available in the libc binding.
|
|
|
|
#include "core.h"
|
2024-03-16 10:23:34 +00:00
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2024-03-16 15:59:19 +00:00
|
|
|
#include <stddef.h>
|
2024-03-24 09:31:52 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2024-03-16 10:23:34 +00:00
|
|
|
|
|
|
|
extern "C" int kvm_check_version(int kvm, bool *compat)
|
|
|
|
{
|
|
|
|
auto v = ioctl(kvm, KVM_GET_API_VERSION);
|
|
|
|
|
|
|
|
if (v < 0) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
*compat = (v == KVM_API_VERSION);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-16 15:59:19 +00:00
|
|
|
extern "C" int kvm_max_vcpus(int kvm, size_t *max)
|
|
|
|
{
|
|
|
|
auto num = ioctl(kvm, KVM_CHECK_EXTENSION, KVM_CAP_MAX_VCPUS);
|
|
|
|
|
|
|
|
if (num < 0) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
*max = static_cast<size_t>(num);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-16 10:23:34 +00:00
|
|
|
extern "C" int kvm_create_vm(int kvm, int *fd)
|
|
|
|
{
|
|
|
|
auto vm = ioctl(kvm, KVM_CREATE_VM, 0);
|
|
|
|
|
|
|
|
if (vm < 0) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
*fd = vm;
|
|
|
|
return 0;
|
|
|
|
}
|
2024-03-24 09:31:52 +00:00
|
|
|
|
|
|
|
extern "C" int kvm_set_user_memory_region(
|
|
|
|
int vm,
|
|
|
|
uint32_t slot,
|
|
|
|
uint64_t addr,
|
|
|
|
uint64_t len,
|
|
|
|
void *mem)
|
|
|
|
{
|
|
|
|
kvm_userspace_memory_region mr;
|
|
|
|
|
|
|
|
memset(&mr, 0, sizeof(mr));
|
|
|
|
|
|
|
|
mr.slot = slot;
|
|
|
|
mr.guest_phys_addr = addr;
|
|
|
|
mr.memory_size = len;
|
|
|
|
mr.userspace_addr = reinterpret_cast<uint64_t>(mem);
|
|
|
|
|
|
|
|
if (ioctl(vm, KVM_SET_USER_MEMORY_REGION, &mr) < 0) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-04-20 14:36:06 +00:00
|
|
|
|
2024-04-28 17:07:04 +00:00
|
|
|
extern "C" int kvm_get_vcpu_mmap_size(int kvm)
|
|
|
|
{
|
2024-06-22 12:21:32 +00:00
|
|
|
return ioctl(kvm, KVM_GET_VCPU_MMAP_SIZE, 0);
|
2024-04-28 17:07:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 06:31:57 +00:00
|
|
|
extern "C" int kvm_create_vcpu(int vm, uint32_t id, int *fd)
|
2024-04-20 14:36:06 +00:00
|
|
|
{
|
|
|
|
auto vcpu = ioctl(vm, KVM_CREATE_VCPU, id);
|
|
|
|
|
|
|
|
if (vcpu < 0) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
*fd = vcpu;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-04-28 17:07:04 +00:00
|
|
|
|
|
|
|
extern "C" int kvm_run(int vcpu)
|
|
|
|
{
|
2024-07-28 12:04:14 +00:00
|
|
|
return ioctl(vcpu, KVM_RUN, 0);
|
2024-04-28 17:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int kvm_get_regs(int vcpu, kvm_regs *regs)
|
|
|
|
{
|
|
|
|
return ioctl(vcpu, KVM_GET_REGS, regs);
|
|
|
|
}
|
|
|
|
|
2024-07-21 15:46:26 +00:00
|
|
|
extern "C" int kvm_set_regs(int vcpu, const kvm_regs *regs)
|
2024-04-28 17:07:04 +00:00
|
|
|
{
|
|
|
|
return ioctl(vcpu, KVM_SET_REGS, regs);
|
|
|
|
}
|
2024-07-27 14:17:32 +00:00
|
|
|
|
|
|
|
extern "C" int kvm_get_sregs(int vcpu, kvm_sregs *regs)
|
|
|
|
{
|
|
|
|
return ioctl(vcpu, KVM_GET_SREGS, regs);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int kvm_set_sregs(int vcpu, const kvm_sregs *regs)
|
|
|
|
{
|
|
|
|
return ioctl(vcpu, KVM_SET_SREGS, regs);
|
|
|
|
}
|
2024-08-17 18:16:48 +00:00
|
|
|
|
|
|
|
extern "C" int kvm_translate(int vcpu, kvm_translation *arg) {
|
|
|
|
return ioctl(vcpu, KVM_TRANSLATE, arg);
|
|
|
|
}
|