git: e4656e10d131 - main - libvmmapi: Move some ioctl wrappers to vmmapi_machdep.c

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

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

commit e4656e10d13190d3149e3bb60e01c50f1c210885
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-04-03 16:52:25 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-04-10 15:17:55 +0000

    libvmmapi: Move some ioctl wrappers to vmmapi_machdep.c
    
    ioctls relating to segments and various x86-specific interrupt
    controllers are easy candidates to move to vmmapi_machdep.c.
    
    In vmmapi.h I'm just ifdefing MD prototypes for now.  We could instead
    split vmmapi.h into multiple headers, e.g., vmmapi.h and
    vmmapi_machdep.h, but it's not obvious to me yet that that's the right
    approach.
    
    No functional change intended.
    
    Reviewed by:    corvink, jhb
    MFC after:      2 weeks
    Sponsored by:   Innovate UK
    Differential Revision:  https://reviews.freebsd.org/D40999
---
 lib/libvmmapi/amd64/vmmapi_machdep.c | 195 +++++++++++++++++++++++++++++++++
 lib/libvmmapi/internal.h             |  12 ++-
 lib/libvmmapi/vmmapi.c               | 201 +----------------------------------
 lib/libvmmapi/vmmapi.h               |   8 +-
 4 files changed, 213 insertions(+), 203 deletions(-)

diff --git a/lib/libvmmapi/amd64/vmmapi_machdep.c b/lib/libvmmapi/amd64/vmmapi_machdep.c
index e0b592a29b35..5ed198d7b76a 100644
--- a/lib/libvmmapi/amd64/vmmapi_machdep.c
+++ b/lib/libvmmapi/amd64/vmmapi_machdep.c
@@ -26,11 +26,206 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
 #include <machine/specialreg.h>
+#include <machine/vmm.h>
+
+#include <string.h>
 
 #include "vmmapi.h"
 #include "internal.h"
 
+int
+vm_set_desc(struct vcpu *vcpu, int reg,
+	    uint64_t base, uint32_t limit, uint32_t access)
+{
+	int error;
+	struct vm_seg_desc vmsegdesc;
+
+	bzero(&vmsegdesc, sizeof(vmsegdesc));
+	vmsegdesc.regnum = reg;
+	vmsegdesc.desc.base = base;
+	vmsegdesc.desc.limit = limit;
+	vmsegdesc.desc.access = access;
+
+	error = vcpu_ioctl(vcpu, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
+	return (error);
+}
+
+int
+vm_get_desc(struct vcpu *vcpu, int reg, uint64_t *base, uint32_t *limit,
+    uint32_t *access)
+{
+	int error;
+	struct vm_seg_desc vmsegdesc;
+
+	bzero(&vmsegdesc, sizeof(vmsegdesc));
+	vmsegdesc.regnum = reg;
+
+	error = vcpu_ioctl(vcpu, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
+	if (error == 0) {
+		*base = vmsegdesc.desc.base;
+		*limit = vmsegdesc.desc.limit;
+		*access = vmsegdesc.desc.access;
+	}
+	return (error);
+}
+
+int
+vm_get_seg_desc(struct vcpu *vcpu, int reg, struct seg_desc *seg_desc)
+{
+	int error;
+
+	error = vm_get_desc(vcpu, reg, &seg_desc->base, &seg_desc->limit,
+	    &seg_desc->access);
+	return (error);
+}
+
+int
+vm_lapic_irq(struct vcpu *vcpu, int vector)
+{
+	struct vm_lapic_irq vmirq;
+
+	bzero(&vmirq, sizeof(vmirq));
+	vmirq.vector = vector;
+
+	return (vcpu_ioctl(vcpu, VM_LAPIC_IRQ, &vmirq));
+}
+
+int
+vm_lapic_local_irq(struct vcpu *vcpu, int vector)
+{
+	struct vm_lapic_irq vmirq;
+
+	bzero(&vmirq, sizeof(vmirq));
+	vmirq.vector = vector;
+
+	return (vcpu_ioctl(vcpu, VM_LAPIC_LOCAL_IRQ, &vmirq));
+}
+
+int
+vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
+{
+	struct vm_lapic_msi vmmsi;
+
+	bzero(&vmmsi, sizeof(vmmsi));
+	vmmsi.addr = addr;
+	vmmsi.msg = msg;
+
+	return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
+}
+
+int
+vm_apicid2vcpu(struct vmctx *ctx __unused, int apicid)
+{
+	/*
+	 * The apic id associated with the 'vcpu' has the same numerical value
+	 * as the 'vcpu' itself.
+	 */
+	return (apicid);
+}
+
+int
+vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
+{
+	struct vm_ioapic_irq ioapic_irq;
+
+	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
+	ioapic_irq.irq = irq;
+
+	return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
+}
+
+int
+vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
+{
+	struct vm_ioapic_irq ioapic_irq;
+
+	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
+	ioapic_irq.irq = irq;
+
+	return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
+}
+
+int
+vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
+{
+	struct vm_ioapic_irq ioapic_irq;
+
+	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
+	ioapic_irq.irq = irq;
+
+	return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
+}
+
+int
+vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
+{
+
+	return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
+}
+
+int
+vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
+{
+	struct vm_isa_irq isa_irq;
+
+	bzero(&isa_irq, sizeof(struct vm_isa_irq));
+	isa_irq.atpic_irq = atpic_irq;
+	isa_irq.ioapic_irq = ioapic_irq;
+
+	return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
+}
+
+int
+vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
+{
+	struct vm_isa_irq isa_irq;
+
+	bzero(&isa_irq, sizeof(struct vm_isa_irq));
+	isa_irq.atpic_irq = atpic_irq;
+	isa_irq.ioapic_irq = ioapic_irq;
+
+	return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
+}
+
+int
+vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
+{
+	struct vm_isa_irq isa_irq;
+
+	bzero(&isa_irq, sizeof(struct vm_isa_irq));
+	isa_irq.atpic_irq = atpic_irq;
+	isa_irq.ioapic_irq = ioapic_irq;
+
+	return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
+}
+
+int
+vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
+    enum vm_intr_trigger trigger)
+{
+	struct vm_isa_irq_trigger isa_irq_trigger;
+
+	bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
+	isa_irq_trigger.atpic_irq = atpic_irq;
+	isa_irq_trigger.trigger = trigger;
+
+	return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
+}
+
+int
+vm_inject_nmi(struct vcpu *vcpu)
+{
+	struct vm_nmi vmnmi;
+
+	bzero(&vmnmi, sizeof(vmnmi));
+
+	return (vcpu_ioctl(vcpu, VM_INJECT_NMI, &vmnmi));
+}
+
 /*
  * From Intel Vol 3a:
  * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
diff --git a/lib/libvmmapi/internal.h b/lib/libvmmapi/internal.h
index 142026e76345..e312e21958ca 100644
--- a/lib/libvmmapi/internal.h
+++ b/lib/libvmmapi/internal.h
@@ -7,11 +7,21 @@
 #ifndef __VMMAPI_INTERNAL_H__
 #define	__VMMAPI_INTERNAL_H__
 
-struct vmctx;
+struct vmctx {
+	int	fd;
+	uint32_t lowmem_limit;
+	int	memflags;
+	size_t	lowmem;
+	size_t	highmem;
+	char	*baseaddr;
+	char	*name;
+};
 
 struct vcpu {
 	struct vmctx *ctx;
 	int vcpuid;
 };
 
+int	vcpu_ioctl(struct vcpu *vcpu, u_long cmd, void *arg);
+
 #endif /* !__VMMAPI_INTERNAL_H__ */
diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c
index 5f812bcc832b..2401f7cd2428 100644
--- a/lib/libvmmapi/vmmapi.c
+++ b/lib/libvmmapi/vmmapi.c
@@ -69,16 +69,6 @@
 #define	PROT_RW		(PROT_READ | PROT_WRITE)
 #define	PROT_ALL	(PROT_READ | PROT_WRITE | PROT_EXEC)
 
-struct vmctx {
-	int	fd;
-	uint32_t lowmem_limit;
-	int	memflags;
-	size_t	lowmem;
-	size_t	highmem;
-	char	*baseaddr;
-	char	*name;
-};
-
 #define	CREATE(x)  sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
 #define	DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
 
@@ -594,7 +584,7 @@ done:
 	return (ptr);
 }
 
-static int
+int
 vcpu_ioctl(struct vcpu *vcpu, u_long cmd, void *arg)
 {
 	/*
@@ -606,52 +596,6 @@ vcpu_ioctl(struct vcpu *vcpu, u_long cmd, void *arg)
 	return (ioctl(vcpu->ctx->fd, cmd, arg));
 }
 
-int
-vm_set_desc(struct vcpu *vcpu, int reg,
-	    uint64_t base, uint32_t limit, uint32_t access)
-{
-	int error;
-	struct vm_seg_desc vmsegdesc;
-
-	bzero(&vmsegdesc, sizeof(vmsegdesc));
-	vmsegdesc.regnum = reg;
-	vmsegdesc.desc.base = base;
-	vmsegdesc.desc.limit = limit;
-	vmsegdesc.desc.access = access;
-
-	error = vcpu_ioctl(vcpu, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
-	return (error);
-}
-
-int
-vm_get_desc(struct vcpu *vcpu, int reg, uint64_t *base, uint32_t *limit,
-    uint32_t *access)
-{
-	int error;
-	struct vm_seg_desc vmsegdesc;
-
-	bzero(&vmsegdesc, sizeof(vmsegdesc));
-	vmsegdesc.regnum = reg;
-
-	error = vcpu_ioctl(vcpu, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
-	if (error == 0) {
-		*base = vmsegdesc.desc.base;
-		*limit = vmsegdesc.desc.limit;
-		*access = vmsegdesc.desc.access;
-	}
-	return (error);
-}
-
-int
-vm_get_seg_desc(struct vcpu *vcpu, int reg, struct seg_desc *seg_desc)
-{
-	int error;
-
-	error = vm_get_desc(vcpu, reg, &seg_desc->base, &seg_desc->limit,
-	    &seg_desc->access);
-	return (error);
-}
-
 int
 vm_set_register(struct vcpu *vcpu, int reg, uint64_t val)
 {
@@ -749,90 +693,6 @@ vm_inject_exception(struct vcpu *vcpu, int vector, int errcode_valid,
 	return (vcpu_ioctl(vcpu, VM_INJECT_EXCEPTION, &exc));
 }
 
-int
-vm_apicid2vcpu(struct vmctx *ctx __unused, int apicid)
-{
-	/*
-	 * The apic id associated with the 'vcpu' has the same numerical value
-	 * as the 'vcpu' itself.
-	 */
-	return (apicid);
-}
-
-int
-vm_lapic_irq(struct vcpu *vcpu, int vector)
-{
-	struct vm_lapic_irq vmirq;
-
-	bzero(&vmirq, sizeof(vmirq));
-	vmirq.vector = vector;
-
-	return (vcpu_ioctl(vcpu, VM_LAPIC_IRQ, &vmirq));
-}
-
-int
-vm_lapic_local_irq(struct vcpu *vcpu, int vector)
-{
-	struct vm_lapic_irq vmirq;
-
-	bzero(&vmirq, sizeof(vmirq));
-	vmirq.vector = vector;
-
-	return (vcpu_ioctl(vcpu, VM_LAPIC_LOCAL_IRQ, &vmirq));
-}
-
-int
-vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
-{
-	struct vm_lapic_msi vmmsi;
-
-	bzero(&vmmsi, sizeof(vmmsi));
-	vmmsi.addr = addr;
-	vmmsi.msg = msg;
-
-	return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
-}
-
-int
-vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
-{
-	struct vm_ioapic_irq ioapic_irq;
-
-	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
-	ioapic_irq.irq = irq;
-
-	return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
-}
-
-int
-vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
-{
-	struct vm_ioapic_irq ioapic_irq;
-
-	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
-	ioapic_irq.irq = irq;
-
-	return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
-}
-
-int
-vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
-{
-	struct vm_ioapic_irq ioapic_irq;
-
-	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
-	ioapic_irq.irq = irq;
-
-	return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
-}
-
-int
-vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
-{
-
-	return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
-}
-
 int
 vm_readwrite_kernemu_device(struct vcpu *vcpu, vm_paddr_t gpa,
     bool write, int size, uint64_t *value)
@@ -851,65 +711,6 @@ vm_readwrite_kernemu_device(struct vcpu *vcpu, vm_paddr_t gpa,
 	return (rc);
 }
 
-int
-vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
-{
-	struct vm_isa_irq isa_irq;
-
-	bzero(&isa_irq, sizeof(struct vm_isa_irq));
-	isa_irq.atpic_irq = atpic_irq;
-	isa_irq.ioapic_irq = ioapic_irq;
-
-	return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
-}
-
-int
-vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
-{
-	struct vm_isa_irq isa_irq;
-
-	bzero(&isa_irq, sizeof(struct vm_isa_irq));
-	isa_irq.atpic_irq = atpic_irq;
-	isa_irq.ioapic_irq = ioapic_irq;
-
-	return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
-}
-
-int
-vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
-{
-	struct vm_isa_irq isa_irq;
-
-	bzero(&isa_irq, sizeof(struct vm_isa_irq));
-	isa_irq.atpic_irq = atpic_irq;
-	isa_irq.ioapic_irq = ioapic_irq;
-
-	return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
-}
-
-int
-vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
-    enum vm_intr_trigger trigger)
-{
-	struct vm_isa_irq_trigger isa_irq_trigger;
-
-	bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
-	isa_irq_trigger.atpic_irq = atpic_irq;
-	isa_irq_trigger.trigger = trigger;
-
-	return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
-}
-
-int
-vm_inject_nmi(struct vcpu *vcpu)
-{
-	struct vm_nmi vmnmi;
-
-	bzero(&vmnmi, sizeof(vmnmi));
-
-	return (vcpu_ioctl(vcpu, VM_INJECT_NMI, &vmnmi));
-}
-
 static const char *capstrmap[] = {
 	[VM_CAP_HALT_EXIT]  = "hlt_exit",
 	[VM_CAP_MTRAP_EXIT] = "mtrap_exit",
diff --git a/lib/libvmmapi/vmmapi.h b/lib/libvmmapi/vmmapi.h
index 8c96bbcb1131..c38e1fca810a 100644
--- a/lib/libvmmapi/vmmapi.h
+++ b/lib/libvmmapi/vmmapi.h
@@ -141,11 +141,13 @@ int	vm_get_memflags(struct vmctx *ctx);
 const char *vm_get_name(struct vmctx *ctx);
 size_t	vm_get_lowmem_size(struct vmctx *ctx);
 size_t	vm_get_highmem_size(struct vmctx *ctx);
+#ifdef __amd64__
 int	vm_set_desc(struct vcpu *vcpu, int reg,
 		    uint64_t base, uint32_t limit, uint32_t access);
 int	vm_get_desc(struct vcpu *vcpu, int reg,
 		    uint64_t *base, uint32_t *limit, uint32_t *access);
 int	vm_get_seg_desc(struct vcpu *vcpu, int reg, struct seg_desc *seg_desc);
+#endif
 int	vm_set_register(struct vcpu *vcpu, int reg, uint64_t val);
 int	vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval);
 int	vm_set_register_set(struct vcpu *vcpu, unsigned int count,
@@ -158,6 +160,7 @@ int	vm_reinit(struct vmctx *ctx);
 int	vm_apicid2vcpu(struct vmctx *ctx, int apicid);
 int	vm_inject_exception(struct vcpu *vcpu, int vector,
     int errcode_valid, uint32_t errcode, int restart_instruction);
+#ifdef __amd64__
 int	vm_lapic_irq(struct vcpu *vcpu, int vector);
 int	vm_lapic_local_irq(struct vcpu *vcpu, int vector);
 int	vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg);
@@ -165,14 +168,15 @@ int	vm_ioapic_assert_irq(struct vmctx *ctx, int irq);
 int	vm_ioapic_deassert_irq(struct vmctx *ctx, int irq);
 int	vm_ioapic_pulse_irq(struct vmctx *ctx, int irq);
 int	vm_ioapic_pincount(struct vmctx *ctx, int *pincount);
-int	vm_readwrite_kernemu_device(struct vcpu *vcpu,
-	    vm_paddr_t gpa, bool write, int size, uint64_t *value);
 int	vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
 int	vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
 int	vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
 int	vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
 	    enum vm_intr_trigger trigger);
 int	vm_inject_nmi(struct vcpu *vcpu);
+#endif
+int	vm_readwrite_kernemu_device(struct vcpu *vcpu,
+	    vm_paddr_t gpa, bool write, int size, uint64_t *value);
 int	vm_capability_name2type(const char *capname);
 const char *vm_capability_type2name(int type);
 int	vm_get_capability(struct vcpu *vcpu, enum vm_cap_type cap,