From decf12ca795931c09396d0d1ef90df0a028297bc Mon Sep 17 00:00:00 2001 From: WangBowen Date: Tue, 7 Apr 2020 23:55:00 +0800 Subject: [PATCH] Add max vcpu IOCTL and support 64 vcpu (netbsd 16) This commit tried to add ioctl HAX_IOCTL_CAP_MAX_VCPU that will return the HAXM max vcpu support. Also, the switch case for Linux hax and vm device add return -ENOSYS in the default case. Moreover, the max HAXM vcpu value is updated from 16 to 64 (netbsd still 16). Previously, HAXM doesn't support IOCTL that would return the max vcpu value. This issue is resolved by adding a new IOCTL HAX_IOCTL_CAP_MAX_VCPU that will simply return HAX_MAX_VCPUS. (IOCTL naming credit to KVM) This commit results in if QEMU calls this IOCTL, it will get the max vcpu value HAXM driver supported and can then compare it with the QEMU max value and smp value to determine whether the smp value is valid. Signed-off-by: WangBowen --- core/include/config.h | 5 +++-- include/darwin/hax_interface_mac.h | 1 + include/linux/hax_interface_linux.h | 1 + include/netbsd/hax_interface_netbsd.h | 1 + platforms/darwin/com_intel_hax_ui.c | 6 +++++- platforms/linux/components.c | 1 + platforms/linux/hax_entry.c | 12 ++++++++++++ platforms/netbsd/hax_entry_hax.c | 4 ++++ platforms/windows/hax_entry.c | 10 ++++++++++ platforms/windows/hax_entry.h | 2 ++ 10 files changed, 40 insertions(+), 3 deletions(-) diff --git a/core/include/config.h b/core/include/config.h index 3582e494..06338953 100644 --- a/core/include/config.h +++ b/core/include/config.h @@ -66,12 +66,13 @@ struct config_t { int no_msr_pass_through; }; -#define HAX_MAX_VCPUS 16 #ifdef HAX_PLATFORM_NETBSD -// TODO: Handle 64 VMs +// TODO: Handle 64 VMs and 64 VCPUs +#define HAX_MAX_VCPUS 16 #define HAX_MAX_VMS 8 #else +#define HAX_MAX_VCPUS 64 // Matches the number of bits in vm_mid_bits (see vm.c) #define HAX_MAX_VMS 64 #endif diff --git a/include/darwin/hax_interface_mac.h b/include/darwin/hax_interface_mac.h index 2487cdf0..17a62d75 100644 --- a/include/darwin/hax_interface_mac.h +++ b/include/darwin/hax_interface_mac.h @@ -41,6 +41,7 @@ #define HAX_IOCTL_DESTROY_VM _IOW(0, 0x22, uint32_t) #define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo) #define HAX_IOCTL_SET_MEMLIMIT _IOWR(0, 0x24, struct hax_set_memlimit) +#define HAX_IOCTL_CAP_MAX_VCPU _IOR(0, 0x25, uint32_t) // Only for backward compatibility with old Qemu. #define HAX_VM_IOCTL_VCPU_CREATE_ORIG _IOR(0, 0x80, int) diff --git a/include/linux/hax_interface_linux.h b/include/linux/hax_interface_linux.h index 97810934..f26d26c5 100644 --- a/include/linux/hax_interface_linux.h +++ b/include/linux/hax_interface_linux.h @@ -42,6 +42,7 @@ #define HAX_IOCTL_DESTROY_VM _IOW(0, 0x22, uint32_t) #define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo) #define HAX_IOCTL_SET_MEMLIMIT _IOWR(0, 0x24, struct hax_set_memlimit) +#define HAX_IOCTL_CAP_MAX_VCPU _IOR(0, 0x25, uint32_t) // Only for backward compatibility with old Qemu. #define HAX_VM_IOCTL_VCPU_CREATE_ORIG _IOR(0, 0x80, int) diff --git a/include/netbsd/hax_interface_netbsd.h b/include/netbsd/hax_interface_netbsd.h index 8cd233d3..bb2245d3 100644 --- a/include/netbsd/hax_interface_netbsd.h +++ b/include/netbsd/hax_interface_netbsd.h @@ -45,6 +45,7 @@ #define HAX_IOCTL_DESTROY_VM _IOW(0, 0x22, uint32_t) #define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo) #define HAX_IOCTL_SET_MEMLIMIT _IOWR(0, 0x24, struct hax_set_memlimit) +#define HAX_IOCTL_CAP_MAX_VCPU _IOR(0, 0x25, uint32_t) // Only for backward compatibility with old Qemu. #define HAX_VM_IOCTL_VCPU_CREATE_ORIG _IOR(0, 0x80, int) diff --git a/platforms/darwin/com_intel_hax_ui.c b/platforms/darwin/com_intel_hax_ui.c index 8d803da4..6a3f92e8 100644 --- a/platforms/darwin/com_intel_hax_ui.c +++ b/platforms/darwin/com_intel_hax_ui.c @@ -29,6 +29,7 @@ */ #include "com_intel_hax.h" +#include "../../core/include/config.h" #include #include @@ -596,7 +597,10 @@ static int hax_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, *((uint32_t *)data) = vm_id; break; } - + case HAX_IOCTL_CAP_MAX_VCPU: { + *((uint32_t *)data) = HAX_MAX_VCPUS; + break; + } default: { handle_unknown_ioctl(dev, cmd, p); ret = -ENOSYS; diff --git a/platforms/linux/components.c b/platforms/linux/components.c index a9adcd96..a13af000 100644 --- a/platforms/linux/components.c +++ b/platforms/linux/components.c @@ -653,6 +653,7 @@ static long hax_vm_ioctl(struct file *filp, unsigned int cmd, default: // TODO: Print information about the process that sent the ioctl. hax_log(HAX_LOGE, "Unknown VM IOCTL 0x%lx\n", cmd); + ret = -ENOSYS; break; } hax_put_vm(cvm); diff --git a/platforms/linux/hax_entry.c b/platforms/linux/hax_entry.c index 9a66de18..ddffddee 100644 --- a/platforms/linux/hax_entry.c +++ b/platforms/linux/hax_entry.c @@ -39,6 +39,7 @@ #include "../../include/hax_interface.h" #include "../../include/hax_release_ver.h" #include "../../core/include/hax_core_interface.h" +#include "../../core/include/config.h" MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR("Kryptos Logic"); @@ -106,7 +107,18 @@ static long hax_dev_ioctl(struct file *filp, unsigned int cmd, return -EFAULT; break; } + case HAX_IOCTL_CAP_MAX_VCPU: { + int max_vcpu = HAX_MAX_VCPUS; + + if (copy_to_user(argp, &max_vcpu, sizeof(max_vcpu))) + return -EFAULT; + + break; + } + default: + hax_log(HAX_LOGE, "Invalid HAX IOCTL 0x%lx\n", cmd); + ret = -ENOSYS; break; } return ret; diff --git a/platforms/netbsd/hax_entry_hax.c b/platforms/netbsd/hax_entry_hax.c index 0367116d..1963f97d 100644 --- a/platforms/netbsd/hax_entry_hax.c +++ b/platforms/netbsd/hax_entry_hax.c @@ -116,6 +116,10 @@ int hax_ioctl(dev_t self __unused, u_long cmd, void *data, int flag, *((uint32_t *)data) = vm_id; break; } + case HAX_IOCTL_CAP_MAX_VCPU: { + *((uint32_t *)data) = HAX_MAX_VCPUS; + break; + } default: hax_log(HAX_LOGE, "Unknown ioctl %#lx, pid=%d ('%s')\n", cmd, l->l_proc->p_pid, l->l_proc->p_comm); diff --git a/platforms/windows/hax_entry.c b/platforms/windows/hax_entry.c index 16167d14..b25c693d 100644 --- a/platforms/windows/hax_entry.c +++ b/platforms/windows/hax_entry.c @@ -35,6 +35,7 @@ #include #include "hax_win.h" +#include "../../core/include/config.h" // vcpu.h int vcpu_takeoff(struct vcpu_t *vcpu); @@ -714,6 +715,15 @@ NTSTATUS HaxDeviceControl(PDEVICE_OBJECT DeviceObject, infret = sizeof(uint32_t); ret = STATUS_SUCCESS; break; + case HAX_IOCTL_CAP_MAX_VCPU: + if (outBufLength < sizeof(uint32_t)) { + ret = STATUS_BUFFER_TOO_SMALL; + goto done; + } + *((uint32_t *)outBuf) = HAX_MAX_VCPUS; + infret = sizeof(uint32_t); + ret = STATUS_SUCCESS; + break; default: ret = STATUS_INVALID_DEVICE_REQUEST; hax_log(HAX_LOGE, "Invalid hax ioctl %x\n", diff --git a/platforms/windows/hax_entry.h b/platforms/windows/hax_entry.h index 4659fe32..1d599271 100644 --- a/platforms/windows/hax_entry.h +++ b/platforms/windows/hax_entry.h @@ -120,6 +120,8 @@ extern PDRIVER_OBJECT HaxDriverObject; CTL_CODE(HAX_DEVICE_TYPE, 0x910, METHOD_BUFFERED, FILE_ANY_ACCESS) #define HAX_IOCTL_SET_MEMLIMIT \ CTL_CODE(HAX_DEVICE_TYPE, 0x911, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define HAX_IOCTL_CAP_MAX_VCPU \ + CTL_CODE(HAX_DEVICE_TYPE, 0x917, METHOD_BUFFERED, FILE_ANY_ACCESS) #define HAX_VM_IOCTL_VCPU_CREATE \ CTL_CODE(HAX_DEVICE_TYPE, 0x902, METHOD_BUFFERED, FILE_ANY_ACCESS)