git: 3170dcaea96d - main - libvmmapi: Move more amd64-specific ioctl wrappers to vmmapi_machdep.c

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

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

commit 3170dcaea96debdd383d974f2b1582d2eb126f50
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-04-03 16:56:11 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-04-10 15:17:56 +0000

    libvmmapi: Move more amd64-specific ioctl wrappers to vmmapi_machdep.c
    
    No functional change intended.
    
    Reviewed by:    corvink, jhb
    MFC after:      2 weeks
    Sponsored by:   Innovate UK
    Differential Revision:  https://reviews.freebsd.org/D41002
---
 lib/libvmmapi/amd64/vmmapi_machdep.c | 124 +++++++++++++++++++++++++++++++++++
 lib/libvmmapi/vmmapi.c               | 124 -----------------------------------
 lib/libvmmapi/vmmapi.h               |   8 ++-
 3 files changed, 130 insertions(+), 126 deletions(-)

diff --git a/lib/libvmmapi/amd64/vmmapi_machdep.c b/lib/libvmmapi/amd64/vmmapi_machdep.c
index 2b50b2b1f3b7..49844be8a574 100644
--- a/lib/libvmmapi/amd64/vmmapi_machdep.c
+++ b/lib/libvmmapi/amd64/vmmapi_machdep.c
@@ -275,6 +275,130 @@ vm_inject_nmi(struct vcpu *vcpu)
 	return (vcpu_ioctl(vcpu, VM_INJECT_NMI, &vmnmi));
 }
 
+int
+vm_inject_exception(struct vcpu *vcpu, int vector, int errcode_valid,
+    uint32_t errcode, int restart_instruction)
+{
+	struct vm_exception exc;
+
+	exc.vector = vector;
+	exc.error_code = errcode;
+	exc.error_code_valid = errcode_valid;
+	exc.restart_instruction = restart_instruction;
+
+	return (vcpu_ioctl(vcpu, VM_INJECT_EXCEPTION, &exc));
+}
+
+int
+vm_readwrite_kernemu_device(struct vcpu *vcpu, vm_paddr_t gpa,
+    bool write, int size, uint64_t *value)
+{
+	struct vm_readwrite_kernemu_device irp = {
+		.access_width = fls(size) - 1,
+		.gpa = gpa,
+		.value = write ? *value : ~0ul,
+	};
+	long cmd = (write ? VM_SET_KERNEMU_DEV : VM_GET_KERNEMU_DEV);
+	int rc;
+
+	rc = vcpu_ioctl(vcpu, cmd, &irp);
+	if (rc == 0 && !write)
+		*value = irp.value;
+	return (rc);
+}
+
+int
+vm_get_x2apic_state(struct vcpu *vcpu, enum x2apic_state *state)
+{
+	int error;
+	struct vm_x2apic x2apic;
+
+	bzero(&x2apic, sizeof(x2apic));
+
+	error = vcpu_ioctl(vcpu, VM_GET_X2APIC_STATE, &x2apic);
+	*state = x2apic.state;
+	return (error);
+}
+
+int
+vm_set_x2apic_state(struct vcpu *vcpu, enum x2apic_state state)
+{
+	int error;
+	struct vm_x2apic x2apic;
+
+	bzero(&x2apic, sizeof(x2apic));
+	x2apic.state = state;
+
+	error = vcpu_ioctl(vcpu, VM_SET_X2APIC_STATE, &x2apic);
+
+	return (error);
+}
+
+int
+vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
+{
+	int error;
+	struct vm_hpet_cap cap;
+
+	bzero(&cap, sizeof(struct vm_hpet_cap));
+	error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
+	if (capabilities != NULL)
+		*capabilities = cap.capabilities;
+	return (error);
+}
+
+int
+vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value)
+{
+	struct vm_rtc_data rtcdata;
+	int error;
+
+	bzero(&rtcdata, sizeof(struct vm_rtc_data));
+	rtcdata.offset = offset;
+	rtcdata.value = value;
+	error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata);
+	return (error);
+}
+
+int
+vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval)
+{
+	struct vm_rtc_data rtcdata;
+	int error;
+
+	bzero(&rtcdata, sizeof(struct vm_rtc_data));
+	rtcdata.offset = offset;
+	error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata);
+	if (error == 0)
+		*retval = rtcdata.value;
+	return (error);
+}
+
+int
+vm_rtc_settime(struct vmctx *ctx, time_t secs)
+{
+	struct vm_rtc_time rtctime;
+	int error;
+
+	bzero(&rtctime, sizeof(struct vm_rtc_time));
+	rtctime.secs = secs;
+	error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime);
+	return (error);
+}
+
+int
+vm_rtc_gettime(struct vmctx *ctx, time_t *secs)
+{
+	struct vm_rtc_time rtctime;
+	int error;
+
+	bzero(&rtctime, sizeof(struct vm_rtc_time));
+	error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime);
+	if (error == 0)
+		*secs = rtctime.secs;
+	return (error);
+}
+
 /*
  * From Intel Vol 3a:
  * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c
index db442a4cd94f..4bf4ded17481 100644
--- a/lib/libvmmapi/vmmapi.c
+++ b/lib/libvmmapi/vmmapi.c
@@ -679,38 +679,6 @@ vm_reinit(struct vmctx *ctx)
 	return (ioctl(ctx->fd, VM_REINIT, 0));
 }
 
-int
-vm_inject_exception(struct vcpu *vcpu, int vector, int errcode_valid,
-    uint32_t errcode, int restart_instruction)
-{
-	struct vm_exception exc;
-
-	exc.vector = vector;
-	exc.error_code = errcode;
-	exc.error_code_valid = errcode_valid;
-	exc.restart_instruction = restart_instruction;
-
-	return (vcpu_ioctl(vcpu, VM_INJECT_EXCEPTION, &exc));
-}
-
-int
-vm_readwrite_kernemu_device(struct vcpu *vcpu, vm_paddr_t gpa,
-    bool write, int size, uint64_t *value)
-{
-	struct vm_readwrite_kernemu_device irp = {
-		.access_width = fls(size) - 1,
-		.gpa = gpa,
-		.value = write ? *value : ~0ul,
-	};
-	long cmd = (write ? VM_SET_KERNEMU_DEV : VM_GET_KERNEMU_DEV);
-	int rc;
-
-	rc = vcpu_ioctl(vcpu, cmd, &irp);
-	if (rc == 0 && !write)
-		*value = irp.value;
-	return (rc);
-}
-
 int
 vm_capability_name2type(const char *capname)
 {
@@ -924,33 +892,6 @@ vm_get_stat_desc(struct vmctx *ctx, int index)
 		return (NULL);
 }
 
-int
-vm_get_x2apic_state(struct vcpu *vcpu, enum x2apic_state *state)
-{
-	int error;
-	struct vm_x2apic x2apic;
-
-	bzero(&x2apic, sizeof(x2apic));
-
-	error = vcpu_ioctl(vcpu, VM_GET_X2APIC_STATE, &x2apic);
-	*state = x2apic.state;
-	return (error);
-}
-
-int
-vm_set_x2apic_state(struct vcpu *vcpu, enum x2apic_state state)
-{
-	int error;
-	struct vm_x2apic x2apic;
-
-	bzero(&x2apic, sizeof(x2apic));
-	x2apic.state = state;
-
-	error = vcpu_ioctl(vcpu, VM_SET_X2APIC_STATE, &x2apic);
-
-	return (error);
-}
-
 int
 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
 {
@@ -971,19 +912,6 @@ vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
 	return (error);
 }
 
-int
-vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
-{
-	int error;
-	struct vm_hpet_cap cap;
-
-	bzero(&cap, sizeof(struct vm_hpet_cap));
-	error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
-	if (capabilities != NULL)
-		*capabilities = cap.capabilities;
-	return (error);
-}
-
 int
 vm_gla2gpa(struct vcpu *vcpu, struct vm_guest_paging *paging,
     uint64_t gla, int prot, uint64_t *gpa, int *fault)
@@ -1236,58 +1164,6 @@ vm_set_intinfo(struct vcpu *vcpu, uint64_t info1)
 	return (error);
 }
 
-int
-vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value)
-{
-	struct vm_rtc_data rtcdata;
-	int error;
-
-	bzero(&rtcdata, sizeof(struct vm_rtc_data));
-	rtcdata.offset = offset;
-	rtcdata.value = value;
-	error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata);
-	return (error);
-}
-
-int
-vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval)
-{
-	struct vm_rtc_data rtcdata;
-	int error;
-
-	bzero(&rtcdata, sizeof(struct vm_rtc_data));
-	rtcdata.offset = offset;
-	error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata);
-	if (error == 0)
-		*retval = rtcdata.value;
-	return (error);
-}
-
-int
-vm_rtc_settime(struct vmctx *ctx, time_t secs)
-{
-	struct vm_rtc_time rtctime;
-	int error;
-
-	bzero(&rtctime, sizeof(struct vm_rtc_time));
-	rtctime.secs = secs;
-	error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime);
-	return (error);
-}
-
-int
-vm_rtc_gettime(struct vmctx *ctx, time_t *secs)
-{
-	struct vm_rtc_time rtctime;
-	int error;
-
-	bzero(&rtctime, sizeof(struct vm_rtc_time));
-	error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime);
-	if (error == 0)
-		*secs = rtctime.secs;
-	return (error);
-}
-
 int
 vm_restart_instruction(struct vcpu *vcpu)
 {
diff --git a/lib/libvmmapi/vmmapi.h b/lib/libvmmapi/vmmapi.h
index c38e1fca810a..b69f02cde7e4 100644
--- a/lib/libvmmapi/vmmapi.h
+++ b/lib/libvmmapi/vmmapi.h
@@ -158,9 +158,9 @@ int	vm_run(struct vcpu *vcpu, struct vm_run *vmrun);
 int	vm_suspend(struct vmctx *ctx, enum vm_suspend_how how);
 int	vm_reinit(struct vmctx *ctx);
 int	vm_apicid2vcpu(struct vmctx *ctx, int apicid);
+#ifdef __amd64__
 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);
@@ -174,9 +174,9 @@ 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);
+#endif
 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,
@@ -206,10 +206,12 @@ uint64_t *vm_get_stats(struct vcpu *vcpu, struct timeval *ret_tv,
 		       int *ret_entries);
 const char *vm_get_stat_desc(struct vmctx *ctx, int index);
 
+#ifdef __amd64__
 int	vm_get_x2apic_state(struct vcpu *vcpu, enum x2apic_state *s);
 int	vm_set_x2apic_state(struct vcpu *vcpu, enum x2apic_state s);
 
 int	vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities);
+#endif
 
 /*
  * Translate the GLA range [gla,gla+len) into GPA segments in 'iov'.
@@ -227,11 +229,13 @@ void	vm_copyin(struct iovec *guest_iov, void *host_dst, size_t len);
 void	vm_copyout(const void *host_src, struct iovec *guest_iov, size_t len);
 void	vm_copy_teardown(struct iovec *iov, int iovcnt);
 
+#ifdef __amd64__
 /* RTC */
 int	vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value);
 int	vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval);
 int	vm_rtc_settime(struct vmctx *ctx, time_t secs);
 int	vm_rtc_gettime(struct vmctx *ctx, time_t *secs);
+#endif
 
 /* Reset vcpu register state */
 int	vcpu_reset(struct vcpu *vcpu);