git: 0a897e675481 - main - riscv: Add support for building vmm as a kernel module

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Wed, 13 Nov 2024 14:15:18 UTC
The branch main has been updated by markj:

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

commit 0a897e67548156ed731dae68eafd21728894ef91
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-11-13 14:09:58 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-11-13 14:15:07 +0000

    riscv: Add support for building vmm as a kernel module
    
    - Update sys/modules/vmm/Makefile.
    - Add some required symbol definitions.
    - Hook up vmm in sys/modules/Makefile.
    
    Reviewed by:    br
    Differential Revision:  https://reviews.freebsd.org/D47477
---
 sys/conf/files.riscv     |  2 +-
 sys/modules/Makefile     |  3 ++-
 sys/modules/vmm/Makefile | 20 +++++++++++++++-----
 sys/riscv/vmm/vmm.c      |  5 +++++
 4 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/sys/conf/files.riscv b/sys/conf/files.riscv
index f75fee72fde2..d325502e03ee 100644
--- a/sys/conf/files.riscv
+++ b/sys/conf/files.riscv
@@ -46,7 +46,7 @@ riscv/riscv/dump_machdep.c	standard
 riscv/riscv/elf_machdep.c	standard
 riscv/riscv/exception.S		standard
 riscv/riscv/exec_machdep.c	standard
-riscv/riscv/fpe.c		optional	vmm
+riscv/riscv/fpe.c		standard
 riscv/riscv/gdb_machdep.c	optional	gdb
 riscv/riscv/intc.c		standard
 riscv/riscv/identcpu.c		standard
diff --git a/sys/modules/Makefile b/sys/modules/Makefile
index d76e16b1da1b..be485b9d393c 100644
--- a/sys/modules/Makefile
+++ b/sys/modules/Makefile
@@ -856,7 +856,8 @@ _smartpqi=	smartpqi
 _p2sb=		p2sb
 .endif
 
-.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64"
+.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \
+    ${MACHINE_CPUARCH} == "riscv"
 .if ${MK_BHYVE} != "no" || defined(ALL_MODULES)
 .if ${KERN_OPTS:MSMP}
 _vmm=		vmm
diff --git a/sys/modules/vmm/Makefile b/sys/modules/vmm/Makefile
index 15454b345934..0ec1147d0d4b 100644
--- a/sys/modules/vmm/Makefile
+++ b/sys/modules/vmm/Makefile
@@ -3,16 +3,12 @@
 KMOD=	vmm
 
 .if ${MACHINE_CPUARCH} == "amd64"
-SRCS+=	opt_acpi.h		\
-	opt_bhyve_snapshot.h	\
-	opt_ddb.h
 .endif
 
 SRCS+=	acpi_if.h bus_if.h device_if.h pci_if.h pcib_if.h vnode_if.h
 
 CFLAGS+= -DVMM_KEEP_STATS
 CFLAGS+= -I${SRCTOP}/sys/${MACHINE}/vmm
-CFLAGS+= -I${SRCTOP}/sys/${MACHINE}/vmm/io
 
 # generic vmm support
 .PATH: ${SRCTOP}/sys/dev/vmm ${SRCTOP}/sys/${MACHINE}/vmm
@@ -24,6 +20,7 @@ SRCS+=	vmm.c			\
 	vmm_stat.c
 
 .if ${MACHINE_CPUARCH} == "aarch64"
+CFLAGS+= -I${SRCTOP}/sys/${MACHINE}/vmm/io
 DPSRCS+=	assym.inc
 
 # TODO: Add the new EL2 code
@@ -71,12 +68,17 @@ vmm_hyp_blob.bin:	vmm_hyp_blob.elf
 vmm_hyp_el2.o:	vmm_hyp_blob.bin
 
 .elif ${MACHINE_CPUARCH} == "amd64"
+CFLAGS+= -I${SRCTOP}/sys/${MACHINE}/vmm/io
 DPSRCS+=	vmx_assym.h svm_assym.h
 DPSRCS+=	vmx_genassym.c svm_genassym.c offset.inc
 
 CFLAGS+= -I${SRCTOP}/sys/amd64/vmm/intel
 CFLAGS+= -I${SRCTOP}/sys/amd64/vmm/amd
 
+SRCS+=	opt_acpi.h		\
+	opt_bhyve_snapshot.h	\
+	opt_ddb.h
+
 SRCS+=	vmm_host.c	\
 	vmm_ioport.c	\
 	vmm_lapic.c	\
@@ -123,7 +125,6 @@ CLEANFILES+=	vmx_assym.h vmx_genassym.o svm_assym.h svm_genassym.o
 
 OBJS_DEPEND_GUESS.vmx_support.o+= vmx_assym.h
 OBJS_DEPEND_GUESS.svm_support.o+= svm_assym.h
-.endif
 
 vmx_assym.h:    vmx_genassym.o
 	sh ${SYSDIR}/kern/genassym.sh vmx_genassym.o > ${.TARGET}
@@ -148,4 +149,13 @@ vmx_genassym.o: offset.inc
 svm_genassym.o: offset.inc
 	${CC} -c ${NOSAN_CFLAGS:N-flto*:N-fno-common} -fcommon ${.IMPSRC}
 
+.elif ${MACHINE_CPUARCH} == "riscv"
+
+SRCS+=	vmm_aplic.c	\
+	vmm_riscv.c	\
+	vmm_sbi.c	\
+	vmm_switch.S
+
+.endif
+
 .include <bsd.kmod.mk>
diff --git a/sys/riscv/vmm/vmm.c b/sys/riscv/vmm/vmm.c
index f8be363b6170..33a0cb5fe420 100644
--- a/sys/riscv/vmm/vmm.c
+++ b/sys/riscv/vmm/vmm.c
@@ -176,6 +176,11 @@ static void vm_free_memmap(struct vm *vm, int ident);
 static bool sysmem_mapping(struct vm *vm, struct mem_map *mm);
 static void vcpu_notify_event_locked(struct vcpu *vcpu);
 
+/* global statistics */
+VMM_STAT(VMEXIT_COUNT, "total number of vm exits");
+VMM_STAT(VMEXIT_IRQ, "number of vmexits for an irq");
+VMM_STAT(VMEXIT_UNHANDLED, "number of vmexits for an unhandled exception");
+
 /*
  * Upper limit on vm_maxcpu. We could increase this to 28 bits, but this
  * is a safe value for now.