git: 55aa31480ced - main - arm64/vmm: Create functions to call into EL2

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Tue, 20 Aug 2024 09:02:17 UTC
The branch main has been updated by andrew:

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

commit 55aa31480ced477610b7cb0a948af6e99fefe864
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2024-08-19 12:43:42 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2024-08-20 08:49:15 +0000

    arm64/vmm: Create functions to call into EL2
    
    These will become ifuncs to enable VHE in a later change.
    
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D46075
---
 sys/arm64/vmm/io/vgic_v3.c   |  3 +-
 sys/arm64/vmm/vmm_arm64.c    | 38 ++++----------------------
 sys/arm64/vmm/vmm_handlers.c | 65 ++++++++++++++++++++++++++++++++++++++++++++
 sys/arm64/vmm/vmm_handlers.h | 48 ++++++++++++++++++++++++++++++++
 sys/conf/files.arm64         |  1 +
 sys/modules/vmm/Makefile     |  1 +
 6 files changed, 123 insertions(+), 33 deletions(-)

diff --git a/sys/arm64/vmm/io/vgic_v3.c b/sys/arm64/vmm/io/vgic_v3.c
index 7ed591c409ba..67afb3374815 100644
--- a/sys/arm64/vmm/io/vgic_v3.c
+++ b/sys/arm64/vmm/io/vgic_v3.c
@@ -68,6 +68,7 @@
 #include <arm64/vmm/hyp.h>
 #include <arm64/vmm/mmu.h>
 #include <arm64/vmm/arm64.h>
+#include <arm64/vmm/vmm_handlers.h>
 
 #include "vgic.h"
 #include "vgic_v3.h"
@@ -2252,7 +2253,7 @@ vgic_v3_init(device_t dev)
 	uint64_t ich_vtr_el2;
 	uint32_t pribits, prebits;
 
-	ich_vtr_el2 = vmm_call_hyp(HYP_READ_REGISTER, HYP_REG_ICH_VTR);
+	ich_vtr_el2 = vmm_read_reg(HYP_REG_ICH_VTR);
 
 	/* TODO: These fields are common with the vgicv2 driver */
 	pribits = ICH_VTR_EL2_PRIBITS(ich_vtr_el2);
diff --git a/sys/arm64/vmm/vmm_arm64.c b/sys/arm64/vmm/vmm_arm64.c
index e5eee47b405b..1b73ed019fad 100644
--- a/sys/arm64/vmm/vmm_arm64.c
+++ b/sys/arm64/vmm/vmm_arm64.c
@@ -65,6 +65,7 @@
 #include "io/vgic.h"
 #include "io/vgic_v3.h"
 #include "io/vtimer.h"
+#include "vmm_handlers.h"
 #include "vmm_stat.h"
 
 #define	HANDLED		1
@@ -101,9 +102,6 @@ static vm_offset_t stack_hyp_va[MAXCPU];
 static vmem_t *el2_mem_alloc;
 
 static void arm_setup_vectors(void *arg);
-static void vmm_pmap_clean_stage2_tlbi(void);
-static void vmm_pmap_invalidate_range(uint64_t, vm_offset_t, vm_offset_t, bool);
-static void vmm_pmap_invalidate_all(uint64_t);
 
 DPCPU_DEFINE_STATIC(struct hypctx *, vcpu);
 
@@ -235,7 +233,6 @@ vmmops_modinit(int ipinum)
 	vm_paddr_t vmm_base;
 	uint64_t id_aa64mmfr0_el1, pa_range_bits, pa_range_field;
 	uint64_t cnthctl_el2;
-	register_t daif;
 	int cpu, i;
 	bool rv __diagused;
 
@@ -291,9 +288,9 @@ vmmops_modinit(int ipinum)
 
 	/* Set up the stage 2 pmap callbacks */
 	MPASS(pmap_clean_stage2_tlbi == NULL);
-	pmap_clean_stage2_tlbi = vmm_pmap_clean_stage2_tlbi;
-	pmap_stage2_invalidate_range = vmm_pmap_invalidate_range;
-	pmap_stage2_invalidate_all = vmm_pmap_invalidate_all;
+	pmap_clean_stage2_tlbi = vmm_clean_s2_tlbi;
+	pmap_stage2_invalidate_range = vmm_s2_tlbi_range;
+	pmap_stage2_invalidate_all = vmm_s2_tlbi_all;
 
 	/*
 	 * Create an allocator for the virtual address space used by EL2.
@@ -429,9 +426,7 @@ vmmops_modinit(int ipinum)
 		vmem_add(el2_mem_alloc, next_hyp_va,
 		    HYP_VM_MAX_ADDRESS - next_hyp_va, M_WAITOK);
 
-	daif = intr_disable();
-	cnthctl_el2 = vmm_call_hyp(HYP_READ_REGISTER, HYP_REG_CNTHCTL);
-	intr_restore(daif);
+	cnthctl_el2 = vmm_read_reg(HYP_REG_CNTHCTL);
 
 	vgic_init();
 	vtimer_init(cnthctl_el2);
@@ -567,26 +562,6 @@ vmmops_vmspace_free(struct vmspace *vmspace)
 	vmspace_free(vmspace);
 }
 
-static void
-vmm_pmap_clean_stage2_tlbi(void)
-{
-	vmm_call_hyp(HYP_CLEAN_S2_TLBI);
-}
-
-static void
-vmm_pmap_invalidate_range(uint64_t vttbr, vm_offset_t sva, vm_offset_t eva,
-    bool final_only)
-{
-	MPASS(eva > sva);
-	vmm_call_hyp(HYP_S2_TLBI_RANGE, vttbr, sva, eva, final_only);
-}
-
-static void
-vmm_pmap_invalidate_all(uint64_t vttbr)
-{
-	vmm_call_hyp(HYP_S2_TLBI_ALL, vttbr);
-}
-
 static inline void
 arm64_print_hyp_regs(struct vm_exit *vme)
 {
@@ -1143,8 +1118,7 @@ vmmops_run(void *vcpui, register_t pc, pmap_t pmap, struct vm_eventinfo *evinfo)
 		vgic_flush_hwstate(hypctx);
 
 		/* Call into EL2 to switch to the guest */
-		excp_type = vmm_call_hyp(HYP_ENTER_GUEST,
-		    hyp->el2_addr, hypctx->el2_addr);
+		excp_type = vmm_enter_guest(hyp, hypctx);
 
 		vgic_sync_hwstate(hypctx);
 		vtimer_sync_hwstate(hypctx);
diff --git a/sys/arm64/vmm/vmm_handlers.c b/sys/arm64/vmm/vmm_handlers.c
new file mode 100644
index 000000000000..2ce674d5ba46
--- /dev/null
+++ b/sys/arm64/vmm/vmm_handlers.c
@@ -0,0 +1,65 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Arm Ltd
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/systm.h>
+
+#include <machine/ifunc.h>
+
+#include "arm64.h"
+#include "vmm_handlers.h"
+
+uint64_t
+vmm_read_reg(uint64_t reg)
+{
+	return (vmm_call_hyp(HYP_READ_REGISTER, reg));
+}
+
+uint64_t
+vmm_enter_guest(struct hyp *hyp, struct hypctx *hypctx)
+{
+	return (vmm_call_hyp(HYP_ENTER_GUEST, hyp->el2_addr, hypctx->el2_addr));
+}
+
+void
+vmm_clean_s2_tlbi(void)
+{
+	vmm_call_hyp(HYP_CLEAN_S2_TLBI);
+}
+
+void
+vmm_s2_tlbi_range(uint64_t vttbr, vm_offset_t sva, vm_offset_t eva,
+    bool final_only)
+{
+	vmm_call_hyp(HYP_S2_TLBI_RANGE, vttbr, sva, eva, final_only);
+}
+
+void
+vmm_s2_tlbi_all(uint64_t vttbr)
+{
+	vmm_call_hyp(HYP_S2_TLBI_ALL, vttbr);
+}
diff --git a/sys/arm64/vmm/vmm_handlers.h b/sys/arm64/vmm/vmm_handlers.h
new file mode 100644
index 000000000000..f651fce6f32d
--- /dev/null
+++ b/sys/arm64/vmm/vmm_handlers.h
@@ -0,0 +1,48 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Arm Ltd
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _VMM_VMM_HANDLERS_H_
+#define _VMM_VMM_HANDLERS_H_
+
+#include <sys/types.h>
+
+struct hyp;
+struct hypctx;
+
+void vmm_clean_s2_tlbi(void);
+uint64_t vmm_enter_guest(struct hyp *, struct hypctx *);
+uint64_t vmm_read_reg(uint64_t);
+void vmm_s2_tlbi_range(uint64_t, vm_offset_t, vm_offset_t, bool);
+void vmm_s2_tlbi_all(uint64_t);
+
+void vmm_vhe_clean_s2_tlbi(void);
+uint64_t vmm_vhe_enter_guest(struct hyp *, struct hypctx *);
+uint64_t vmm_vhe_read_reg(uint64_t);
+void vmm_vhe_s2_tlbi_range(uint64_t, vm_offset_t, vm_offset_t, bool);
+void vmm_vhe_s2_tlbi_all(uint64_t);
+
+#endif /* _VMM_VMM_HANDLERS_H_ */
diff --git a/sys/conf/files.arm64 b/sys/conf/files.arm64
index 10590d6c77e2..b522177221e5 100644
--- a/sys/conf/files.arm64
+++ b/sys/conf/files.arm64
@@ -122,6 +122,7 @@ arm64/vmm/vmm_instruction_emul.c		optional vmm
 arm64/vmm/vmm_stat.c				optional vmm
 arm64/vmm/vmm_arm64.c				optional vmm
 arm64/vmm/vmm_reset.c				optional vmm
+arm64/vmm/vmm_handlers.c			optional vmm
 arm64/vmm/vmm_call.S				optional vmm
 arm64/vmm/vmm_nvhe_exception.S			optional vmm		\
 	compile-with "${NOSAN_C:N-mbranch-protection*} -fpie" \
diff --git a/sys/modules/vmm/Makefile b/sys/modules/vmm/Makefile
index 409804f4e25c..a3a878a653ff 100644
--- a/sys/modules/vmm/Makefile
+++ b/sys/modules/vmm/Makefile
@@ -28,6 +28,7 @@ DPSRCS+=	assym.inc
 SRCS+=	vmm_arm64.c \
 	vmm_reset.c \
 	vmm_call.S \
+	vmm_handlers.c \
 	vmm_mmu.c \
 	vmm_hyp_el2.S