git: 7f00e46b85e8 - main - libvmmapi: Split the ioctl list into MI and MD lists

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Wed, 10 Apr 2024 15:19:06 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=7f00e46b85e8c60259ad0bdd53593ea246f3e549

commit 7f00e46b85e8c60259ad0bdd53593ea246f3e549
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-04-03 16:55:54 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-04-10 15:17:56 +0000

    libvmmapi: Split the ioctl list into MI and MD lists
    
    To enable use in capability mode, libvmmapi needs a list of all the
    ioctls that might be invoked on the vmm device handle.  Some of these
    ioctls are amd64-specific.  Move the ioctl list to vmmapi_machdep.c and
    define a list of MI ioctls so that the arm64 port can build its own list
    without duplicating common ioctls.  No functional change intended.
    
    Reviewed by:    corvink, jhb
    MFC after:      2 weeks
    Sponsored by:   Innovate UK
    Differential Revision:  https://reviews.freebsd.org/D41001
---
 lib/libvmmapi/amd64/vmmapi_machdep.c | 34 +++++++++++++++++++++++++
 lib/libvmmapi/internal.h             | 48 ++++++++++++++++++++++++++++++++++++
 lib/libvmmapi/vmmapi.c               | 37 +++++----------------------
 3 files changed, 88 insertions(+), 31 deletions(-)

diff --git a/lib/libvmmapi/amd64/vmmapi_machdep.c b/lib/libvmmapi/amd64/vmmapi_machdep.c
index c19e9c6f935f..2b50b2b1f3b7 100644
--- a/lib/libvmmapi/amd64/vmmapi_machdep.c
+++ b/lib/libvmmapi/amd64/vmmapi_machdep.c
@@ -31,6 +31,8 @@
 
 #include <machine/specialreg.h>
 #include <machine/vmm.h>
+#include <machine/vmm_dev.h>
+#include <machine/vmm_snapshot.h>
 
 #include <string.h>
 
@@ -52,6 +54,38 @@ const char *vm_capstrmap[] = {
 	[VM_CAP_MAX] = NULL,
 };
 
+#define	VM_MD_IOCTLS			\
+	VM_SET_SEGMENT_DESCRIPTOR,	\
+	VM_GET_SEGMENT_DESCRIPTOR,	\
+	VM_SET_KERNEMU_DEV,		\
+	VM_GET_KERNEMU_DEV,		\
+	VM_LAPIC_IRQ,			\
+	VM_LAPIC_LOCAL_IRQ,		\
+	VM_LAPIC_MSI,			\
+	VM_IOAPIC_ASSERT_IRQ,		\
+	VM_IOAPIC_DEASSERT_IRQ,		\
+	VM_IOAPIC_PULSE_IRQ,		\
+	VM_IOAPIC_PINCOUNT,		\
+	VM_ISA_ASSERT_IRQ,		\
+	VM_ISA_DEASSERT_IRQ,		\
+	VM_ISA_PULSE_IRQ,		\
+	VM_ISA_SET_IRQ_TRIGGER,		\
+	VM_INJECT_NMI,			\
+	VM_SET_X2APIC_STATE,		\
+	VM_GET_X2APIC_STATE,		\
+	VM_GET_HPET_CAPABILITIES,	\
+	VM_RTC_WRITE,			\
+	VM_RTC_READ,			\
+	VM_RTC_SETTIME,			\
+	VM_RTC_GETTIME
+
+const cap_ioctl_t vm_ioctl_cmds[] = {
+	VM_COMMON_IOCTLS,
+	VM_PPT_IOCTLS,
+	VM_MD_IOCTLS,
+};
+size_t vm_ioctl_ncmds = nitems(vm_ioctl_cmds);
+
 int
 vm_set_desc(struct vcpu *vcpu, int reg,
 	    uint64_t base, uint32_t limit, uint32_t access)
diff --git a/lib/libvmmapi/internal.h b/lib/libvmmapi/internal.h
index 973a70afe022..98e50f9a1bf4 100644
--- a/lib/libvmmapi/internal.h
+++ b/lib/libvmmapi/internal.h
@@ -7,6 +7,8 @@
 #ifndef __VMMAPI_INTERNAL_H__
 #define	__VMMAPI_INTERNAL_H__
 
+#include <sys/types.h>
+
 struct vmctx {
 	int	fd;
 	uint32_t lowmem_limit;
@@ -26,4 +28,50 @@ int	vcpu_ioctl(struct vcpu *vcpu, u_long cmd, void *arg);
 
 extern const char *vm_capstrmap[];
 
+#define	VM_COMMON_IOCTLS	\
+	VM_RUN,			\
+	VM_SUSPEND,		\
+	VM_REINIT,		\
+	VM_ALLOC_MEMSEG,	\
+	VM_GET_MEMSEG,		\
+	VM_MMAP_MEMSEG,		\
+	VM_MMAP_MEMSEG,		\
+	VM_MMAP_GETNEXT,	\
+	VM_MUNMAP_MEMSEG,	\
+	VM_SET_REGISTER,	\
+	VM_GET_REGISTER,	\
+	VM_SET_REGISTER_SET,	\
+	VM_GET_REGISTER_SET,	\
+	VM_INJECT_EXCEPTION,	\
+	VM_SET_CAPABILITY,	\
+	VM_GET_CAPABILITY,	\
+	VM_STATS,		\
+	VM_STAT_DESC,		\
+	VM_GET_GPA_PMAP,	\
+	VM_GLA2GPA,		\
+	VM_GLA2GPA_NOFAULT,	\
+	VM_ACTIVATE_CPU,	\
+	VM_GET_CPUS,		\
+	VM_SUSPEND_CPU,		\
+	VM_RESUME_CPU,		\
+	VM_SET_INTINFO,		\
+	VM_GET_INTINFO,		\
+	VM_RESTART_INSTRUCTION,	\
+	VM_SET_TOPOLOGY,	\
+	VM_GET_TOPOLOGY,	\
+	VM_SNAPSHOT_REQ,	\
+	VM_RESTORE_TIME
+
+#define	VM_PPT_IOCTLS		\
+	VM_BIND_PPTDEV,		\
+	VM_UNBIND_PPTDEV,	\
+	VM_MAP_PPTDEV_MMIO,	\
+	VM_PPTDEV_MSI,		\
+	VM_PPTDEV_MSIX,		\
+	VM_UNMAP_PPTDEV_MMIO,	\
+	VM_PPTDEV_DISABLE_MSIX
+
+extern const cap_ioctl_t vm_ioctl_cmds[];
+extern size_t vm_ioctl_ncmds;
+
 #endif /* !__VMMAPI_INTERNAL_H__ */
diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c
index cc765deb904c..db442a4cd94f 100644
--- a/lib/libvmmapi/vmmapi.c
+++ b/lib/libvmmapi/vmmapi.c
@@ -1351,42 +1351,15 @@ vm_get_topology(struct vmctx *ctx,
 	return (error);
 }
 
-/* Keep in sync with machine/vmm_dev.h. */
-static const cap_ioctl_t vm_ioctl_cmds[] = { VM_RUN, VM_SUSPEND, VM_REINIT,
-    VM_ALLOC_MEMSEG, VM_GET_MEMSEG, VM_MMAP_MEMSEG, VM_MMAP_MEMSEG,
-    VM_MMAP_GETNEXT, VM_MUNMAP_MEMSEG, VM_SET_REGISTER, VM_GET_REGISTER,
-    VM_SET_SEGMENT_DESCRIPTOR, VM_GET_SEGMENT_DESCRIPTOR,
-    VM_SET_REGISTER_SET, VM_GET_REGISTER_SET,
-    VM_SET_KERNEMU_DEV, VM_GET_KERNEMU_DEV,
-    VM_INJECT_EXCEPTION, VM_LAPIC_IRQ, VM_LAPIC_LOCAL_IRQ,
-    VM_LAPIC_MSI, VM_IOAPIC_ASSERT_IRQ, VM_IOAPIC_DEASSERT_IRQ,
-    VM_IOAPIC_PULSE_IRQ, VM_IOAPIC_PINCOUNT, VM_ISA_ASSERT_IRQ,
-    VM_ISA_DEASSERT_IRQ, VM_ISA_PULSE_IRQ, VM_ISA_SET_IRQ_TRIGGER,
-    VM_SET_CAPABILITY, VM_GET_CAPABILITY, VM_BIND_PPTDEV,
-    VM_UNBIND_PPTDEV, VM_MAP_PPTDEV_MMIO, VM_PPTDEV_MSI,
-    VM_PPTDEV_MSIX, VM_UNMAP_PPTDEV_MMIO, VM_PPTDEV_DISABLE_MSIX,
-    VM_INJECT_NMI, VM_STATS, VM_STAT_DESC,
-    VM_SET_X2APIC_STATE, VM_GET_X2APIC_STATE,
-    VM_GET_HPET_CAPABILITIES, VM_GET_GPA_PMAP, VM_GLA2GPA,
-    VM_GLA2GPA_NOFAULT,
-    VM_ACTIVATE_CPU, VM_GET_CPUS, VM_SUSPEND_CPU, VM_RESUME_CPU,
-    VM_SET_INTINFO, VM_GET_INTINFO,
-    VM_RTC_WRITE, VM_RTC_READ, VM_RTC_SETTIME, VM_RTC_GETTIME,
-    VM_RESTART_INSTRUCTION, VM_SET_TOPOLOGY, VM_GET_TOPOLOGY,
-    VM_SNAPSHOT_REQ, VM_RESTORE_TIME
-};
-
 int
 vm_limit_rights(struct vmctx *ctx)
 {
 	cap_rights_t rights;
-	size_t ncmds;
 
 	cap_rights_init(&rights, CAP_IOCTL, CAP_MMAP_RW);
 	if (caph_rights_limit(ctx->fd, &rights) != 0)
 		return (-1);
-	ncmds = nitems(vm_ioctl_cmds);
-	if (caph_ioctls_limit(ctx->fd, vm_ioctl_cmds, ncmds) != 0)
+	if (caph_ioctls_limit(ctx->fd, vm_ioctl_cmds, vm_ioctl_ncmds) != 0)
 		return (-1);
 	return (0);
 }
@@ -1407,15 +1380,17 @@ const cap_ioctl_t *
 vm_get_ioctls(size_t *len)
 {
 	cap_ioctl_t *cmds;
+	size_t sz;
 
 	if (len == NULL) {
-		cmds = malloc(sizeof(vm_ioctl_cmds));
+		sz = vm_ioctl_ncmds * sizeof(vm_ioctl_cmds[0]);
+		cmds = malloc(sz);
 		if (cmds == NULL)
 			return (NULL);
-		bcopy(vm_ioctl_cmds, cmds, sizeof(vm_ioctl_cmds));
+		bcopy(vm_ioctl_cmds, cmds, sz);
 		return (cmds);
 	}
 
-	*len = nitems(vm_ioctl_cmds);
+	*len = vm_ioctl_ncmds;
 	return (NULL);
 }