Skip to contentSkip to conversation

KVM: s390: Introduce arm64 KVM

PATCH Superseded Steffen Eiden 2 participants 47 messages May 29 – May 30

Subsystems (22)
x86 KVM PowerPC Kbuild +18 more subsystems

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
author reviewer
#5 lore raw

PATCH v3 3/27 KVM, vfio: remove symbol_get(kvm_put_kvm) from vfio

In reply to Steffen Eiden

From: Paolo Bonzini <[email protected]>

Right now, KVM and VFIO are using symbol_get to access each other's symbols because of a circular reference between the modules, as well as to avoid loading them unnecessarily.

The remaining use in VFIO is for kvm_put_kvm, which is not inline because it needs to call kvm_destroy_vm. However, storing the address of kvm_destroy_vm in the "struct kvm" is enough to remove the dependency from VFIO.

This also makes it possible to direct kvm_put_kvm to either the arm64 or the s390 implementation of kvm_destroy_vm.

---
 drivers/vfio/vfio_main.c  | 24 +++---------------------
 include/linux/kvm_host.h  |  1 -
 include/linux/kvm_types.h |  8 ++++++++
 include/linux/vfio.h      |  1 -
 virt/kvm/kvm_main.c       |  9 ++-------
 5 files changed, 13 insertions(+), 30 deletions(-)
drivers/vfio/vfio_main.c +3 -21
index e606db41ff3f..053559a8c7f3 100644
@@ -17,7 +17,7 @@
 #include <linux/idr.h>
 #include <linux/iommu.h>
 #if IS_ENABLED(CONFIG_KVM)
-#include <linux/kvm_host.h>
+#include <linux/kvm_types.h>
 #endif
 #include <linux/list.h>
 #include <linux/miscdevice.h>
@@ -444,9 +444,6 @@ EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 #if IS_ENABLED(CONFIG_KVM)
 void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm, struct module *kvm_module)
 {
-	void (*pfn)(struct kvm *kvm);
-	bool ret;
-
 	lockdep_assert_held(&device->dev_set->lock);
 
 	if (!kvm)
@@ -455,21 +452,13 @@ void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm, struc
 	if (!try_module_get(kvm_module))
 		return;
 
-	pfn = symbol_get(kvm_put_kvm);
-	if (WARN_ON(!pfn))
+	if (!kvm_get_kvm_safe(kvm))
 		goto out_put_mod;
 
-	ret = kvm_get_kvm_safe(kvm);
-	if (!ret)
-		goto out_put_sym;
-
-	device->put_kvm = pfn;
 	device->kvm = kvm;
 	device->kvm_module = kvm_module;
 	return;
 
-out_put_sym:
-	symbol_put(kvm_put_kvm);
 out_put_mod:
 	module_put(kvm_module);
 }
@@ -481,14 +470,7 @@ void vfio_device_put_kvm(struct vfio_device *device)
 	if (!device->kvm)
 		return;
 
-	if (WARN_ON(!device->put_kvm))
-		goto clear;
-
-	device->put_kvm(device->kvm);
-	device->put_kvm = NULL;
-	symbol_put(kvm_put_kvm);
-
-clear:
+	kvm_put_kvm(device->kvm);
 	device->kvm = NULL;
 	module_put(device->kvm_module);
 	device->kvm_module = NULL;
include/linux/kvm_host.h +0 -1
index 8a068cbf12ff..088986d0aca2 100644
@@ -1072,7 +1072,6 @@ static inline void kvm_irqfd_exit(void)
 int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module);
 void kvm_exit(void);
 
-void kvm_put_kvm(struct kvm *kvm);
 bool file_is_kvm(struct file *file);
 void kvm_put_kvm_no_destroy(struct kvm *kvm);
 
include/linux/kvm_types.h +8 -0
index 4cb68c71a13c..8a9be07cb8dd 100644
@@ -144,6 +144,7 @@ struct kvm_vcpu_stat_generic {
 
 struct kvm_refcount {
 	refcount_t users_count;
+	void (*destroy)(struct kvm *kvm);
 };
 
 static inline void kvm_get_kvm(struct kvm *kvm)
@@ -162,6 +163,13 @@ static inline bool kvm_get_kvm_safe(struct kvm *kvm)
 	return refcount_inc_not_zero(&rc->users_count);
 }
 
+static inline void kvm_put_kvm(struct kvm *kvm)
+{
+	struct kvm_refcount *rc = (struct kvm_refcount *)kvm;
+	if (refcount_dec_and_test(&rc->users_count))
+		rc->destroy(kvm);
+}
+
 #endif /* !__ASSEMBLER__ */
 
 #endif /* __KVM_TYPES_H__ */
include/linux/vfio.h +0 -1
index 5bfec0339804..81f04e60ebfd 100644
@@ -67,7 +67,6 @@ struct vfio_device {
 	unsigned int open_count;
 	struct completion comp;
 	struct iommufd_access *iommufd_access;
-	void (*put_kvm)(struct kvm *kvm);
 	struct inode *inode;
 #if IS_ENABLED(CONFIG_IOMMUFD)
 	struct iommufd_device *iommufd_device;
virt/kvm/kvm_main.c +2 -7
index 3c08f640c996..2eca44a69a9a 100644
@@ -119,6 +119,7 @@ static struct dentry *kvm_debugfs_dir;
 
 static const struct file_operations stat_fops_per_vm;
 
+static void kvm_destroy_vm(struct kvm *kvm);
 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
 			   unsigned long arg);
 #ifdef CONFIG_KVM_COMPAT
@@ -1148,6 +1149,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
 
 	BUILD_BUG_ON(offsetof(struct kvm, rc) != 0);
 	refcount_set(&kvm->rc.users_count, 1);
+	kvm->rc.destroy = kvm_destroy_vm;
 
 	for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
 		for (j = 0; j < 2; j++) {
@@ -1310,13 +1312,6 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	mmdrop(mm);
 }
 
-void kvm_put_kvm(struct kvm *kvm)
-{
-	if (refcount_dec_and_test(&kvm->rc.users_count))
-		kvm_destroy_vm(kvm);
-}
-EXPORT_SYMBOL_GPL(kvm_put_kvm);
-
 /*
  * Used to put a reference that was taken on behalf of an object associated
  * with a user-visible file descriptor, e.g. a vcpu or device, if installation
Signature · 1 line
2.53.0
author reviewer
#29 lore raw

PATCH v3 17/27 KVM: Remove KVM_MMIO as config option

In reply to Steffen Eiden

Defining KVM_MMIO is not flexible enough for multi-KVM systems with different Kconfig options regarding KVM_MMIO. Therefore, remove KVM_MMIO from the config space and use the macro HAVE_KVM_MMIO instead.

---
 arch/arm64/include/asm/kvm_host.h     | 2 ++
 arch/arm64/kvm/Kconfig                | 1 -
 arch/loongarch/include/asm/kvm_host.h | 2 ++
 arch/loongarch/kvm/Kconfig            | 1 -
 arch/mips/include/asm/kvm_host.h      | 2 ++
 arch/mips/kvm/Kconfig                 | 1 -
 arch/powerpc/include/asm/kvm_host.h   | 7 +++++++
 arch/powerpc/kvm/Kconfig              | 4 ----
 arch/riscv/include/asm/kvm_host.h     | 2 ++
 arch/riscv/kvm/Kconfig                | 1 -
 arch/x86/include/asm/kvm_host.h       | 2 ++
 arch/x86/kvm/Kconfig                  | 1 -
 include/linux/kvm_host.h              | 2 +-
 virt/kvm/Kconfig                      | 3 ---
 virt/kvm/Makefile.kvm                 | 3 +--
 virt/kvm/coalesced_mmio.c             | 3 +++
 virt/kvm/coalesced_mmio.h             | 2 +-
 virt/kvm/kvm_main.c                   | 8 ++++----
 18 files changed, 27 insertions(+), 20 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
index bcc30419e2de..2db51746b4d8 100644
@@ -33,6 +33,8 @@
 
 #define KVM_HALT_POLL_NS_DEFAULT 500000
 
+#define HAVE_KVM_MMIO
+
 #include <kvm/arm_vgic.h>
 #include <kvm/arm_arch_timer.h>
 #include <kvm/arm_pmu.h>
arch/arm64/kvm/Kconfig +0 -1
index 449154f9a485..dd138b730217 100644
@@ -22,7 +22,6 @@ menuconfig KVM
 	select KVM_COMMON
 	select KVM_GENERIC_HARDWARE_ENABLING
 	select HAVE_KVM_CPU_RELAX_INTERCEPT
-	select KVM_MMIO
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
 	select VIRT_XFER_TO_GUEST_WORK
 	select KVM_VFIO
arch/loongarch/include/asm/kvm_host.h +2 -0
index 776bc487a705..f4f52c71fdbb 100644
@@ -27,6 +27,8 @@
 
 #define __KVM_HAVE_ARCH_INTC_INITIALIZED
 
+#define HAVE_KVM_MMIO
+
 /* Loongarch KVM register ids */
 #define KVM_GET_IOC_CSR_IDX(id)		((id & KVM_CSR_IDX_MASK) >> LOONGARCH_REG_SHIFT)
 #define KVM_GET_IOC_CPUCFG_IDX(id)	((id & KVM_CPUCFG_IDX_MASK) >> LOONGARCH_REG_SHIFT)
arch/loongarch/kvm/Kconfig +0 -1
index 15da2d88c0c1..f03872ba0ba8 100644
@@ -28,7 +28,6 @@ config KVM
 	select KVM_COMMON
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
 	select KVM_GENERIC_HARDWARE_ENABLING
-	select KVM_MMIO
 	select VIRT_XFER_TO_GUEST_WORK
 	select SCHED_INFO
 	select GUEST_PERF_EVENTS if PERF_EVENTS
arch/mips/include/asm/kvm_host.h +2 -0
index c14b10821817..e3cb7c8c9461 100644
@@ -26,6 +26,8 @@
 
 #include <kvm/iodev.h>
 
+#define HAVE_KVM_MMIO
+
 /* MIPS KVM register ids */
 #define MIPS_CP0_32(_R, _S)					\
 	(KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
arch/mips/kvm/Kconfig +0 -1
index b1b9a1d67758..c65596fdf54f 100644
@@ -22,7 +22,6 @@ config KVM
 	select EXPORT_UASM
 	select KVM_COMMON
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
-	select KVM_MMIO
 	select KVM_GENERIC_HARDWARE_ENABLING
 	select HAVE_KVM_READONLY_MEM
 	help
arch/powerpc/include/asm/kvm_host.h +7 -0
index 2d139c807577..92d14438e31b 100644
@@ -29,6 +29,13 @@
 
 #define __KVM_HAVE_ARCH_VCPU_DEBUGFS
 
+#if defined(CONFIG_KVM_BOOK3S_32_HANDLER) || \
+	defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) || \
+	defined(CONFIG_KVM_E500V2) || \
+	defined(CONFIG_KVM_E500MC)
+#define HAVE_KVM_MMIO
+#endif
+
 #define KVM_MAX_VCPUS		NR_CPUS
 #define KVM_MAX_VCORES		NR_CPUS
 
arch/powerpc/kvm/Kconfig +0 -4
index 9a0d1c1aca6c..d97a4a51ec55 100644
@@ -29,7 +29,6 @@ config KVM_BOOK3S_HANDLER
 config KVM_BOOK3S_32_HANDLER
 	bool
 	select KVM_BOOK3S_HANDLER
-	select KVM_MMIO
 
 config KVM_BOOK3S_64_HANDLER
 	bool
@@ -37,7 +36,6 @@ config KVM_BOOK3S_64_HANDLER
 
 config KVM_BOOK3S_PR_POSSIBLE
 	bool
-	select KVM_MMIO
 
 config KVM_BOOK3S_HV_POSSIBLE
 	bool
@@ -200,7 +198,6 @@ config KVM_E500V2
 	depends on PPC_E500 && !PPC_E500MC
 	depends on !CONTEXT_TRACKING_USER
 	select KVM
-	select KVM_MMIO
 	help
 	  Support running unmodified E500 guest kernels in virtual machines on
 	  E500v2 host processors.
@@ -215,7 +212,6 @@ config KVM_E500MC
 	depends on PPC_E500MC
 	depends on !CONTEXT_TRACKING_USER
 	select KVM
-	select KVM_MMIO
 	select KVM_BOOKE_HV
 	help
 	  Support running unmodified E500MC/E5500/E6500 guest kernels in
arch/riscv/include/asm/kvm_host.h +2 -0
index 75b0a951c1bc..b004427c07a0 100644
@@ -26,6 +26,8 @@
 #include <asm/kvm_vcpu_timer.h>
 #include <asm/kvm_vcpu_pmu.h>
 
+#define HAVE_KVM_MMIO
+
 #define KVM_MAX_VCPUS			1024
 
 #define KVM_HALT_POLL_NS_DEFAULT	500000
arch/riscv/kvm/Kconfig +0 -1
index ec2cee0a39e0..3aea8d4939d2 100644
@@ -28,7 +28,6 @@ config KVM
 	select KVM_COMMON
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
 	select KVM_GENERIC_HARDWARE_ENABLING
-	select KVM_MMIO
 	select VIRT_XFER_TO_GUEST_WORK
 	select SCHED_INFO
 	select GUEST_PERF_EVENTS if PERF_EVENTS
arch/x86/include/asm/kvm_host.h +2 -0
index c470e40a00aa..aa630838e039 100644
@@ -46,6 +46,8 @@
 
 #define __KVM_HAVE_ARCH_VCPU_DEBUGFS
 
+#define HAVE_KVM_MMIO
+
 /*
  * CONFIG_KVM_MAX_NR_VCPUS is defined iff CONFIG_KVM!=n, provide a dummy max if
  * KVM is disabled (arbitrarily use the default from CONFIG_KVM_MAX_NR_VCPUS).
arch/x86/kvm/Kconfig +0 -1
index 801bf9e520db..da5d9fa8a11c 100644
@@ -32,7 +32,6 @@ config KVM_X86
 	select VHOST_TASK
 	select KVM_ASYNC_PF
 	select USER_RETURN_NOTIFIER
-	select KVM_MMIO
 	select SCHED_INFO
 	select PERF_EVENTS
 	select GUEST_PERF_EVENTS
include/linux/kvm_host.h +1 -1
index f44885fc6e6a..0fb194e3d950 100644
@@ -834,7 +834,7 @@ struct kvm {
 	struct list_head ioeventfds;
 	struct kvm_vm_stat stat;
 	struct kvm_arch arch;
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 	struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
 	spinlock_t ring_lock;
 	struct list_head coalesced_zones;
virt/kvm/Kconfig +0 -3
index 794976b88c6f..c4a983e41765 100644
@@ -40,9 +40,6 @@ config NEED_KVM_DIRTY_RING_WITH_BITMAP
 	bool
 	depends on HAVE_KVM_DIRTY_RING
 
-config KVM_MMIO
-       bool
-
 config KVM_ASYNC_PF
        bool
 
virt/kvm/Makefile.kvm +1 -2
index d047d4cf58c9..3c27c18b49a0 100644
@@ -5,9 +5,8 @@
 
 KVM ?= ../../../virt/kvm
 
-kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o
+kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o $(KVM)/coalesced_mmio.o
 kvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o
-kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o
 kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
 kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o
 kvm-$(CONFIG_HAVE_KVM_DIRTY_RING) += $(KVM)/dirty_ring.o
virt/kvm/coalesced_mmio.c +3 -0
index 6b1d90161099..070eba3c6534 100644
@@ -12,6 +12,8 @@
 #include <kvm/iodev.h>
 
 #include <linux/kvm_host.h>
+
+#ifdef HAVE_KVM_MMIO
 #include <linux/slab.h>
 #include <linux/kvm.h>
 
@@ -188,3 +190,4 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
 	 */
 	return 0;
 }
+#endif /* ifdef HAVE_KVM_MMIO */
virt/kvm/coalesced_mmio.h +1 -1
index 36f84264ed25..a8430d41945b 100644
@@ -11,7 +11,7 @@
  *
  */
 
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 
 #include <linux/list.h>
 
virt/kvm/kvm_main.c +4 -4
index 454d99b45fa0..38de0a19d17e 100644
@@ -4034,7 +4034,7 @@ static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
 	else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->arch.pio_data);
 #endif
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
@@ -4860,7 +4860,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 	case KVM_CAP_ENABLE_CAP_VM:
 	case KVM_CAP_HALT_POLL:
 		return 1;
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 	case KVM_CAP_COALESCED_MMIO:
 		return KVM_COALESCED_MMIO_PAGE_OFFSET;
 	case KVM_CAP_COALESCED_PIO:
@@ -5201,7 +5201,7 @@ static long kvm_vm_ioctl(struct file *filp,
 		break;
 	}
 #endif
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 	case KVM_REGISTER_COALESCED_MMIO: {
 		struct kvm_coalesced_mmio_zone zone;
 
@@ -5523,7 +5523,7 @@ static long kvm_dev_ioctl(struct file *filp,
 #ifdef CONFIG_X86
 		r += PAGE_SIZE;    /* pio data page */
 #endif
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 		r += PAGE_SIZE;    /* coalesced mmio ring page */
 #endif
 		break;
Signature · 1 line
2.53.0
author reviewer
#15 lore raw

PATCH v3 8/27 arm64: Extract pstate definitions from ptrace

In reply to Steffen Eiden

From: Nina Schoetterl-Glausch <[email protected]>

Split all definitions that can be used by non-native architectures into a separate file pstate.h. This allows other architectures using the pstate definitions. While at it refactor SPSR related definitions to use the BIT(n) macro and move them into sysreg-defs.h

---
 arch/arm64/include/asm/ptrace.h      | 34 +-----------------
 arch/arm64/include/uapi/asm/pstate.h | 53 ++++++++++++++++++++++++++++
 arch/arm64/include/uapi/asm/ptrace.h | 49 +------------------------
 include/arch/arm64/asm/pstate.h      | 46 ++++++++++++++++++++++++
 include/arch/arm64/asm/sysreg-defs.h | 42 ++++++++++++++++++++++
 include/uapi/arch/arm64/asm/Kbuild   |  1 +
 6 files changed, 144 insertions(+), 81 deletions(-)
 create mode 100644 arch/arm64/include/uapi/asm/pstate.h
 create mode 100644 include/arch/arm64/asm/pstate.h
arch/arm64/include/asm/ptrace.h +1 -33
index 39582511ad72..72ea0a8af960 100644
@@ -9,6 +9,7 @@
 #define __ASM_PTRACE_H
 
 #include <asm/cpufeature.h>
+#include <asm/pstate.h>
 
 #include <uapi/asm/ptrace.h>
 
@@ -28,10 +29,6 @@
 
 #define GIC_PRIO_PSR_I_SET	GICV3_PRIO_PSR_I_SET
 
-/* Additional SPSR bits not exposed in the UABI */
-#define PSR_MODE_THREAD_BIT	(1 << 0)
-#define PSR_IL_BIT		(1 << 20)
-
 /* AArch32-specific ptrace requests */
 #define COMPAT_PTRACE_GETREGS		12
 #define COMPAT_PTRACE_SETREGS		13
@@ -42,41 +39,12 @@
 #define COMPAT_PTRACE_GETHBPREGS	29
 #define COMPAT_PTRACE_SETHBPREGS	30
 
-/* SPSR_ELx bits for exceptions taken from AArch32 */
-#define PSR_AA32_MODE_MASK	0x0000001f
-#define PSR_AA32_MODE_USR	0x00000010
-#define PSR_AA32_MODE_FIQ	0x00000011
-#define PSR_AA32_MODE_IRQ	0x00000012
-#define PSR_AA32_MODE_SVC	0x00000013
-#define PSR_AA32_MODE_ABT	0x00000017
-#define PSR_AA32_MODE_HYP	0x0000001a
-#define PSR_AA32_MODE_UND	0x0000001b
-#define PSR_AA32_MODE_SYS	0x0000001f
-#define PSR_AA32_T_BIT		0x00000020
-#define PSR_AA32_F_BIT		0x00000040
-#define PSR_AA32_I_BIT		0x00000080
-#define PSR_AA32_A_BIT		0x00000100
-#define PSR_AA32_E_BIT		0x00000200
-#define PSR_AA32_PAN_BIT	0x00400000
-#define PSR_AA32_SSBS_BIT	0x00800000
-#define PSR_AA32_DIT_BIT	0x01000000
-#define PSR_AA32_Q_BIT		0x08000000
-#define PSR_AA32_V_BIT		0x10000000
-#define PSR_AA32_C_BIT		0x20000000
-#define PSR_AA32_Z_BIT		0x40000000
-#define PSR_AA32_N_BIT		0x80000000
-#define PSR_AA32_IT_MASK	0x0600fc00	/* If-Then execution state mask */
-#define PSR_AA32_GE_MASK	0x000f0000
-
 #ifdef CONFIG_CPU_BIG_ENDIAN
 #define PSR_AA32_ENDSTATE	PSR_AA32_E_BIT
 #else
 #define PSR_AA32_ENDSTATE	0
 #endif
 
-/* AArch32 CPSR bits, as seen in AArch32 */
-#define COMPAT_PSR_DIT_BIT	0x00200000
-
 /*
  * These are 'magic' values for PTRACE_PEEKUSR that return info about where a
  * process is located in memory.
arch/arm64/include/uapi/asm/pstate.h new file +53 -0

53 lines not on this page · raw message

arch/arm64/include/uapi/asm/ptrace.h +1 -48

55 lines not on this page · raw message

include/arch/arm64/asm/pstate.h new file +46 -0

46 lines not on this page · raw message

include/arch/arm64/asm/sysreg-defs.h +42 -0

48 lines not on this page · raw message

include/uapi/arch/arm64/asm/Kbuild +1 -0
index 73b69888ea28..f29b28d40e16 100644
@@ -3,3 +3,4 @@ shared-uapi-src := arch/arm64/include/uapi/asm
 
 shared-uapi-y += kvm.h
 shared-uapi-y += sve_context.h
+shared-uapi-y += pstate.h
Signature · 1 line
2.53.0
author reviewer
#3 lore raw

PATCH v3 2/27 KVM, vfio: remove symbol_get(kvm_get_kvm_safe) from vfio

In reply to Steffen Eiden

From: Paolo Bonzini <[email protected]>

Right now, KVM and VFIO are using symbol_get to access each other's symbols because of a circular reference between the modules, as well as to avoid loading them unnecessarily.

However, usage of symbol_get is mostly deprecated and there are just a handful of users left. In the case of VFIO, in particular, the functions it calls can be made inline. Start with kvm_get_kvm_safe, for which it is trivial to do so.

While at it, move the function from kvm_host.h to kvm_types.h. Unlike e.g. drivers/s390/crypto/vfio_ap_ops.c, there's no need for VFIO to know any implementation details of KVM, and struct kvm can be treated as an opaque type.

---
 arch/x86/kvm/mmu/tdp_mmu.c |  2 +-
 arch/x86/kvm/vmx/nested.h  |  4 ++--
 drivers/vfio/vfio_main.c   |  8 +-------
 include/linux/kvm_host.h   |  8 ++++----
 include/linux/kvm_types.h  | 22 ++++++++++++++++++++++
 virt/kvm/kvm_main.c        | 27 ++++++---------------------
 6 files changed, 36 insertions(+), 35 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
index 7b1102d26f9c..af5d90bf6aa4 100644
@@ -1136,7 +1136,7 @@ void kvm_tdp_mmu_invalidate_roots(struct kvm *kvm,
 	 * being destroyed in an error path of KVM_CREATE_VM.
 	 */
 	if (IS_ENABLED(CONFIG_PROVE_LOCKING) &&
-	    refcount_read(&kvm->users_count) && kvm->created_vcpus)
+	    refcount_read(&kvm->rc.users_count) && kvm->created_vcpus)
 		lockdep_assert_held_write(&kvm->mmu_lock);
 
 	/*
arch/x86/kvm/vmx/nested.h +2 -2
index 213a448104af..2c83fc905698 100644
@@ -58,7 +58,7 @@ bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
 {
 	lockdep_assert_once(lockdep_is_held(&vcpu->mutex) ||
-			    !refcount_read(&vcpu->kvm->users_count));
+			    !refcount_read(&vcpu->kvm->rc.users_count));
 
 	return to_vmx(vcpu)->nested.cached_vmcs12;
 }
@@ -66,7 +66,7 @@ static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
 static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
 {
 	lockdep_assert_once(lockdep_is_held(&vcpu->mutex) ||
-			    !refcount_read(&vcpu->kvm->users_count));
+			    !refcount_read(&vcpu->kvm->rc.users_count));
 
 	return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
 }
drivers/vfio/vfio_main.c +1 -7
index 78b9cad348b1..e606db41ff3f 100644
@@ -445,7 +445,6 @@ EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm, struct module *kvm_module)
 {
 	void (*pfn)(struct kvm *kvm);
-	bool (*fn)(struct kvm *kvm);
 	bool ret;
 
 	lockdep_assert_held(&device->dev_set->lock);
@@ -460,12 +459,7 @@ void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm, struc
 	if (WARN_ON(!pfn))
 		goto out_put_mod;
 
-	fn = symbol_get(kvm_get_kvm_safe);
-	if (WARN_ON(!fn))
-		goto out_put_sym;
-
-	ret = fn(kvm);
-	symbol_put(kvm_get_kvm_safe);
+	ret = kvm_get_kvm_safe(kvm);
 	if (!ret)
 		goto out_put_sym;
 
include/linux/kvm_host.h +4 -4
index 4c14aee1fb06..8a068cbf12ff 100644
@@ -768,6 +768,9 @@ struct kvm_memslots {
 };
 
 struct kvm {
+	/* Must be the first field, see function definitions in kvm_types.h.  */
+	struct kvm_refcount rc;
+
 #ifdef KVM_HAVE_MMU_RWLOCK
 	rwlock_t mmu_lock;
 #else
@@ -831,7 +834,6 @@ struct kvm {
 	struct list_head ioeventfds;
 	struct kvm_vm_stat stat;
 	struct kvm_arch arch;
-	refcount_t users_count;
 #ifdef CONFIG_KVM_MMIO
 	struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
 	spinlock_t ring_lock;
@@ -1070,8 +1072,6 @@ static inline void kvm_irqfd_exit(void)
 int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module);
 void kvm_exit(void);
 
-void kvm_get_kvm(struct kvm *kvm);
-bool kvm_get_kvm_safe(struct kvm *kvm);
 void kvm_put_kvm(struct kvm *kvm);
 bool file_is_kvm(struct file *file);
 void kvm_put_kvm_no_destroy(struct kvm *kvm);
@@ -1081,7 +1081,7 @@ static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id)
 	as_id = array_index_nospec(as_id, KVM_MAX_NR_ADDRESS_SPACES);
 	return srcu_dereference_check(kvm->memslots[as_id], &kvm->srcu,
 			lockdep_is_held(&kvm->slots_lock) ||
-			!refcount_read(&kvm->users_count));
+			!refcount_read(&kvm->rc.users_count));
 }
 
 static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
include/linux/kvm_types.h +22 -0
index a568d8e6f4e8..4cb68c71a13c 100644
@@ -33,6 +33,7 @@
 
 #include <linux/mutex.h>
 #include <linux/spinlock_types.h>
+#include <linux/refcount.h>
 
 struct kvm;
 struct kvm_async_pf;
@@ -140,6 +141,27 @@ struct kvm_vcpu_stat_generic {
 };
 
 #define KVM_STATS_NAME_SIZE	48
+
+struct kvm_refcount {
+	refcount_t users_count;
+};
+
+static inline void kvm_get_kvm(struct kvm *kvm)
+{
+	struct kvm_refcount *rc = (struct kvm_refcount *)kvm;
+	refcount_inc(&rc->users_count);
+}
+
+/*
+ * A safe version of kvm_get_kvm(), making sure the vm is not being destroyed.
+ * Return true if kvm referenced successfully, false otherwise.
+ */
+static inline bool kvm_get_kvm_safe(struct kvm *kvm)
+{
+	struct kvm_refcount *rc = (struct kvm_refcount *)kvm;
+	return refcount_inc_not_zero(&rc->users_count);
+}
+
 #endif /* !__ASSEMBLER__ */
 
 #endif /* __KVM_TYPES_H__ */
virt/kvm/kvm_main.c +6 -21
index 89489996fbc1..3c08f640c996 100644
@@ -1089,7 +1089,7 @@ static inline struct kvm_io_bus *kvm_get_bus_for_destruction(struct kvm *kvm,
 							     enum kvm_bus idx)
 {
 	return rcu_dereference_protected(kvm->buses[idx],
-					 !refcount_read(&kvm->users_count));
+					 !refcount_read(&kvm->rc.users_count));
 }
 
 static int kvm_enable_virtualization(void);
@@ -1146,7 +1146,8 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
 	if (r)
 		goto out_err_no_irq_routing;
 
-	refcount_set(&kvm->users_count, 1);
+	BUILD_BUG_ON(offsetof(struct kvm, rc) != 0);
+	refcount_set(&kvm->rc.users_count, 1);
 
 	for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
 		for (j = 0; j < 2; j++) {
@@ -1216,7 +1217,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
 out_err_no_disable:
 	kvm_arch_destroy_vm(kvm);
 out_err_no_arch_destroy_vm:
-	WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
+	WARN_ON_ONCE(!refcount_dec_and_test(&kvm->rc.users_count));
 	for (i = 0; i < KVM_NR_BUSES; i++)
 		kfree(kvm_get_bus_for_destruction(kvm, i));
 	kvm_free_irq_routing(kvm);
@@ -1309,25 +1310,9 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	mmdrop(mm);
 }
 
-void kvm_get_kvm(struct kvm *kvm)
-{
-	refcount_inc(&kvm->users_count);
-}
-EXPORT_SYMBOL_GPL(kvm_get_kvm);
-
-/*
- * Make sure the vm is not during destruction, which is a safe version of
- * kvm_get_kvm().  Return true if kvm referenced successfully, false otherwise.
- */
-bool kvm_get_kvm_safe(struct kvm *kvm)
-{
-	return refcount_inc_not_zero(&kvm->users_count);
-}
-EXPORT_SYMBOL_GPL(kvm_get_kvm_safe);
-
 void kvm_put_kvm(struct kvm *kvm)
 {
-	if (refcount_dec_and_test(&kvm->users_count))
+	if (refcount_dec_and_test(&kvm->rc.users_count))
 		kvm_destroy_vm(kvm);
 }
 EXPORT_SYMBOL_GPL(kvm_put_kvm);
@@ -1341,7 +1326,7 @@ EXPORT_SYMBOL_GPL(kvm_put_kvm);
  */
 void kvm_put_kvm_no_destroy(struct kvm *kvm)
 {
-	WARN_ON(refcount_dec_and_test(&kvm->users_count));
+	WARN_ON(refcount_dec_and_test(&kvm->rc.users_count));
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_put_kvm_no_destroy);
 
Signature · 1 line
2.53.0
author reviewer
#20 lore raw

PATCH v3 11/27 KVM: arm64: Access elements of vcpu_gp_regs individually

In reply to Steffen Eiden

While for arm64 the members of vcpu_gp_regs are allocated continuous this is not necessarily true for other architectures implementing ARM.

Let vcpu_gp_regs() no longer return the address of the user_pt_regs in the vcpu context but the address of the gp-register array field in the user_pt_reg struct.

---
 arch/arm64/include/asm/kvm_emulate.h       | 9 +++++++--
 arch/arm64/include/asm/kvm_host.h          | 2 +-
 arch/arm64/kvm/hyp/exception.c             | 7 +++++--
 arch/arm64/kvm/hyp/include/hyp/adjust_pc.h | 4 ++--
 arch/arm64/kvm/hyp/include/hyp/switch.h    | 6 +++---
 arch/arm64/kvm/reset.c                     | 6 ++++--
 include/kvm/arm64/kvm_emulate.h            | 4 ++--
 virt/kvm/arm64/guest.c                     | 6 +++---
 8 files changed, 27 insertions(+), 17 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +7 -2
index 39fa3a12730c..41eac2b5de14 100644
@@ -95,12 +95,17 @@ static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
 
 static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
 {
-	return (unsigned long *)&vcpu_gp_regs(vcpu)->pc;
+	return (unsigned long *)&vcpu->arch.ctxt.regs.pc;
 }
 
 static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
 {
-	return (unsigned long *)&vcpu_gp_regs(vcpu)->pstate;
+	return (unsigned long *)&vcpu->arch.ctxt.regs.pstate;
+}
+
+static __always_inline unsigned long *vcpu_sp_el0(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.ctxt.regs.sp;
 }
 
 static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
arch/arm64/include/asm/kvm_host.h +1 -1
index 549e9a6aa12c..6af4f5b39f41 100644
@@ -1089,7 +1089,7 @@ struct kvm_vcpu_arch {
 #define vcpu_clear_on_unsupported_cpu(vcpu)				\
 	vcpu_clear_flag(vcpu, ON_UNSUPPORTED_CPU)
 
-#define vcpu_gp_regs(v)		(&(v)->arch.ctxt.regs)
+#define vcpu_gp_regs(v)		((v)->arch.ctxt.regs.regs)
 
 /*
  * Only use __vcpu_sys_reg/ctxt_sys_reg if you know you want the
arch/arm64/kvm/hyp/exception.c +5 -2
index bef40ddb16db..82611442a2d1 100644
@@ -277,6 +277,9 @@ static const u8 return_offsets[8][2] = {
 	[7] = { 4, 4 },		/* FIQ, unused */
 };
 
+#define OFFSETOF_PT_REG(__r) offsetof(struct user_pt_regs, __r)
+#define COMPAT_IDX(__c) ((OFFSETOF_PT_REG(__c) - OFFSETOF_PT_REG(regs[0])) / sizeof(u64))
+
 static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
 {
 	unsigned long spsr = *vcpu_cpsr(vcpu);
@@ -292,12 +295,12 @@ static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
 	switch(mode) {
 	case PSR_AA32_MODE_ABT:
 		__vcpu_write_spsr_abt(vcpu, host_spsr_to_spsr32(spsr));
-		vcpu_gp_regs(vcpu)->compat_lr_abt = return_address;
+		vcpu_gp_regs(vcpu)[COMPAT_IDX(compat_lr_abt)] = return_address;
 		break;
 
 	case PSR_AA32_MODE_UND:
 		__vcpu_write_spsr_und(vcpu, host_spsr_to_spsr32(spsr));
-		vcpu_gp_regs(vcpu)->compat_lr_und = return_address;
+		vcpu_gp_regs(vcpu)[COMPAT_IDX(compat_lr_und)] = return_address;
 		break;
 	}
 
arch/arm64/kvm/hyp/include/hyp/adjust_pc.h +2 -2
index 15e1e5db73e1..4e4cb67824c0 100644
@@ -20,11 +20,11 @@
 static inline void __kvm_skip_instr(struct kvm_vcpu *vcpu)
 {
 	*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
-	vcpu_gp_regs(vcpu)->pstate = read_sysreg_el2(SYS_SPSR);
+	*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
 
 	kvm_skip_instr(vcpu);
 
-	write_sysreg_el2(vcpu_gp_regs(vcpu)->pstate, SYS_SPSR);
+	write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
 	write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
 }
 
arch/arm64/kvm/hyp/include/hyp/switch.h +3 -3
index bf0eb5e43427..29f638e4a55f 100644
@@ -447,7 +447,7 @@ static inline bool __populate_fault_info(struct kvm_vcpu *vcpu)
 static inline bool kvm_hyp_handle_mops(struct kvm_vcpu *vcpu, u64 *exit_code)
 {
 	*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
-	arm64_mops_reset_regs(vcpu_gp_regs(vcpu), vcpu->arch.fault.esr_el2);
+	arm64_mops_reset_regs(&vcpu->arch.ctxt.regs, vcpu->arch.fault.esr_el2);
 	write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
 
 	/*
@@ -888,7 +888,7 @@ static inline void synchronize_vcpu_pstate(struct kvm_vcpu *vcpu)
 	/*
 	 * Check for the conditions of Cortex-A510's #2077057. When these occur
 	 * SPSR_EL2 can't be trusted, but isn't needed either as it is
-	 * unchanged from the value in vcpu_gp_regs(vcpu)->pstate.
+	 * unchanged from the value in vcpu_cpsr(vcpu).
 	 * Are we single-stepping the guest, and took a PAC exception from the
 	 * active-not-pending state?
 	 */
@@ -898,7 +898,7 @@ static inline void synchronize_vcpu_pstate(struct kvm_vcpu *vcpu)
 	    ESR_ELx_EC(read_sysreg_el2(SYS_ESR)) == ESR_ELx_EC_PAC)
 		write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
 
-	vcpu->arch.ctxt.regs.pstate = read_sysreg_el2(SYS_SPSR);
+	*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
 }
 
 /*
arch/arm64/kvm/reset.c +4 -2
index 036bf2dff976..e952dec01d74 100644
@@ -218,13 +218,15 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 		pstate = VCPU_RESET_PSTATE_EL1;
 
 	/* Reset core registers */
-	memset(vcpu_gp_regs(vcpu), 0, sizeof(*vcpu_gp_regs(vcpu)));
+	memset(vcpu_gp_regs(vcpu), 0, sizeof(vcpu_gp_regs(vcpu)));
+	*vcpu_pc(vcpu) = 0;
+	*vcpu_sp_el0(vcpu) = 0;
 	memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
 	vcpu->arch.ctxt.spsr_abt = 0;
 	vcpu->arch.ctxt.spsr_und = 0;
 	vcpu->arch.ctxt.spsr_irq = 0;
 	vcpu->arch.ctxt.spsr_fiq = 0;
-	vcpu_gp_regs(vcpu)->pstate = pstate;
+	*vcpu_cpsr(vcpu) = pstate;
 
 	/* Reset system registers */
 	kvm_reset_sys_regs(vcpu);
include/kvm/arm64/kvm_emulate.h +2 -2
index 25322b95af21..0e16d18e53d2 100644
@@ -77,14 +77,14 @@ static inline void kvm_skip_instr(struct kvm_vcpu *vcpu)
 static __always_inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
 						  u8 reg_num)
 {
-	return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs[reg_num];
+	return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)[reg_num];
 }
 
 static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
 					 unsigned long val)
 {
 	if (reg_num != 31)
-		vcpu_gp_regs(vcpu)->regs[reg_num] = val;
+		vcpu_gp_regs(vcpu)[reg_num] = val;
 }
 
 static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
virt/kvm/arm64/guest.c +3 -3
index 83e33e0143b9..e283a4456df8 100644
@@ -81,16 +81,16 @@ static void *core_reg_addr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	     KVM_REG_ARM_CORE_REG(regs.regs[30]):
 		off -= KVM_REG_ARM_CORE_REG(regs.regs[0]);
 		off /= 2;
-		return &vcpu_gp_regs(vcpu)->regs[off];
+		return &vcpu_gp_regs(vcpu)[off];
 
 	case KVM_REG_ARM_CORE_REG(regs.sp):
-		return &vcpu_gp_regs(vcpu)->sp;
+		return vcpu_sp_el0(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(regs.pc):
 		return vcpu_pc(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(regs.pstate):
-		return &vcpu_gp_regs(vcpu)->pstate;
+		return vcpu_cpsr(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(sp_el1):
 		return __ctxt_sys_reg(&vcpu->arch.ctxt, SP_EL1);
Signature · 1 line
2.53.0
author reviewer
#32 lore raw

PATCH v3 19/27 s390: Introduce Start Arm Execution instruction

In reply to Steffen Eiden

The Start Arm Execution (SAE) instruction is the centerpiece for executing arm64 (KVM) guests on s390. Its purpose is, similar to SIE, to enable accelerated execution of arm64 virtual machines. SAE expects the physical address of a control block as the only argument.

The host is responsible to save & restore
- GPRs 0-13
- access register 0-15
- breaking event register (BEAR)
- vector/floating point registers
between SAE executions to guarantee host consistency.

GPRs and BEAR are save and restores in the asm functions. The other register are handled in within C code. Access registers are handled in a later patch and SVEs will be handled when they are introduced in a future series. Most arm64 registers are handled by a satellite block called save_area. Some registers, frequently used by hypervisors, are placed into the SAE control block itself.

---
 arch/s390/include/asm/asm-prototypes.h       |   1 +
 arch/s390/include/asm/kvm_host_arm64_types.h | 129 +++++++++++++++++++
 arch/s390/include/asm/sae.h                  |  56 ++++++++
 arch/s390/include/asm/stacktrace.h           |   5 +
 arch/s390/kernel/asm-offsets.c               |   1 +
 arch/s390/kernel/entry.S                     |  25 ++++
 arch/s390/tools/opcodes.txt                  |   3 +
 7 files changed, 220 insertions(+)
 create mode 100644 arch/s390/include/asm/kvm_host_arm64_types.h
 create mode 100644 arch/s390/include/asm/sae.h
arch/s390/include/asm/asm-prototypes.h +1 -0
index 7bd1801cf241..2bf4b52f4d2d 100644
@@ -6,6 +6,7 @@
 #include <asm/bug.h>
 #include <asm/fpu.h>
 #include <asm/nospec-branch.h>
+#include <asm/sae.h>
 #include <asm-generic/asm-prototypes.h>
 
 __int128_t __ashlti3(__int128_t a, int b);
arch/s390/include/asm/kvm_host_arm64_types.h new file +129 -0

129 lines not on this page · raw message

arch/s390/include/asm/sae.h new file +56 -0

56 lines not on this page · raw message

arch/s390/include/asm/stacktrace.h +5 -0
index ac3606c3babe..2d332d7c8145 100644
@@ -59,6 +59,7 @@ static inline bool on_stack(struct stack_info *info,
 struct stack_frame {
 	union {
 		unsigned long empty[9];
+		/* SIE stack frame */
 		struct {
 			unsigned long sie_control_block;
 			unsigned long sie_savearea;
@@ -68,6 +69,10 @@ struct stack_frame {
 			unsigned long sie_guest_asce;
 			unsigned long sie_irq;
 		};
+		/* SAE stack frame */
+		struct {
+			unsigned long sae_bear;
+		};
 	};
 	unsigned long gprs[10];
 	unsigned long back_chain;
arch/s390/kernel/asm-offsets.c +1 -0
index 8619adf91cdb..8ef992734bf8 100644
@@ -68,6 +68,7 @@ int main(void)
 	OFFSET(__SF_SIE_CONTROL_PHYS, stack_frame, sie_control_block_phys);
 	OFFSET(__SF_SIE_GUEST_ASCE, stack_frame, sie_guest_asce);
 	OFFSET(__SF_SIE_IRQ, stack_frame, sie_irq);
+	OFFSET(__SF_SAE_BEAR, stack_frame, sae_bear);
 	DEFINE(STACK_FRAME_OVERHEAD, sizeof(struct stack_frame));
 	BLANK();
 	OFFSET(__SFUSER_BACKCHAIN, stack_frame_user, back_chain);
arch/s390/kernel/entry.S +25 -0
index cd4fbdec67a7..d44daedcd47b 100644
@@ -244,6 +244,31 @@ EXPORT_SYMBOL(__sie64a)
 EXPORT_SYMBOL(sie_exit)
 #endif
 
+#if IS_ENABLED(CONFIG_KVM_ARM64)
+/*
+ * __sae64a calling convention:
+ * %r2 pointer to sae control block physical address
+ */
+SYM_FUNC_START(__sae64a)
+	stmg	%r6,%r14,__SF_GPRS(%r15)	# store kernel registers
+	STBEAR	__SF_SAE_BEAR(%r15)		# save breaking event address register
+	.insn	rre,0xb9a50000,%r2,0		# Start Arm Execution
+# Let the next instruction be NOP to avoid triggering a machine check
+# and handling it in a guest as result of the instruction execution.
+	nopr	7
+	LBEAR	__SF_SAE_BEAR(%r15)		# restore breaking event address register
+	lmg	%r6,%r14,__SF_GPRS(%r15)	# restore kernel registers
+	xgr	%r0,%r0				# clear guest registers to
+	xgr	%r1,%r1				# prevent speculative use
+	xgr	%r3,%r3
+	xgr	%r2,%r2
+	xgr	%r4,%r4
+	xgr	%r5,%r5
+	BR_EX	%r14
+SYM_FUNC_END(__sae64a)
+EXPORT_SYMBOL(__sae64a)
+#endif
+
 /*
  * SVC interrupt handler routine. System calls are synchronous events and
  * are entered with interrupts disabled.
arch/s390/tools/opcodes.txt +3 -0
index def2659f6602..0e4773c94af0 100644
@@ -594,6 +594,9 @@ b9a0	clp	RRF_U0RR
 b9a1	tpei	RRE_RR
 b9a2	ptf	RRE_R0
 b9a4	uvc	RRF_URR
+b9a5	sae	RRE_R0
+b9a6	lasrm	RRE_R0
+b9a7	stiasrm	RRE_R0
 b9aa	lptea	RRF_RURR2
 b9ab	essa	RRF_U0RR
 b9ac	irbm	RRE_RR
Signature · 1 line
2.53.0
author reviewer
#11 lore raw

PATCH v3 6/27 arm64: Provide arm64 API for non-native architectures

In reply to Steffen Eiden

Enable the usage of arm64 asm headers independent of the CPU architecture. Introduce a new directory, include/arch/arm64/asm to store (host) architecture- agnostic headers for arm64 implementors.

The new path is added to the include patch of native arm64. Therefore, arm64 will continue to reference to those headers via include <asm/header.h> without any code change. This means that the header names should be unique for arm64 asm headers. For native arm64, headers at the new path take precedence over any other include path.

For other architectures to use the esr string names, move *esr_class_str from traps.c to esr.h.

For a seamless integration non-native architectures must also include the new path to the include path.

---
 MAINTAINERS                                   |  1 +
 arch/arm64/Makefile                           |  2 +
 arch/arm64/kernel/traps.c                     | 53 ----------------
 .../arch/arm64}/asm/brk-imm.h                 |  6 +-
 .../include => include/arch/arm64}/asm/esr.h  | 62 +++++++++++++++++--
 .../arch/arm64}/asm/sysreg-defs.h             |  6 +-
 6 files changed, 65 insertions(+), 65 deletions(-)
 rename {arch/arm64/include => include/arch/arm64}/asm/brk-imm.h (92%)
 rename {arch/arm64/include => include/arch/arm64}/asm/esr.h (88%)
 rename {arch/arm64/include => include/arch/arm64}/asm/sysreg-defs.h (99%)
MAINTAINERS +1 -0
index 52b66c8b97f4..f1903e4f0dcf 100644
@@ -3838,6 +3838,7 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git
 F:	Documentation/arch/arm64/
 F:	arch/arm64/
+F:	include/arch/arm64/
 F:	include/uapi/arch/arm64/
 F:	drivers/virt/coco/arm-cca-guest/
 F:	drivers/virt/coco/pkvm-guest/
arch/arm64/Makefile +2 -0
index 73a10f65ce8b..b244e837cedd 100644
@@ -221,6 +221,8 @@ PHONY += virtconfig
 virtconfig:
 	$(call merge_into_defconfig_override,defconfig,virt)
 
+LINUXINCLUDE := -I$(srctree)/include/arch/arm64/ $(LINUXINCLUDE)
+
 define archhelp
   echo  '* Image.gz      - Compressed kernel image (arch/$(ARCH)/boot/Image.gz)'
   echo  '  Image         - Uncompressed kernel image (arch/$(ARCH)/boot/Image)'
arch/arm64/kernel/traps.c +0 -53

59 lines not on this page · raw message

include/arch/arm64/asm/brk-imm.h renamed from arch/arm64/include/asm/brk-imm.h +3 -3
similarity index 92%
rename from arch/arm64/include/asm/brk-imm.h
rename to include/arch/arm64/asm/brk-imm.h
index beb42c62b6ac..c311085886f5 100644
@@ -3,8 +3,8 @@
  * Copyright (C) 2012 ARM Ltd.
  */
 
-#ifndef __ASM_BRK_IMM_H
-#define __ASM_BRK_IMM_H
+#ifndef __ARM64_ASM_BRK_IMM_H
+#define __ARM64_ASM_BRK_IMM_H
 
 /*
  * #imm16 values used for BRK instruction generation
@@ -39,4 +39,4 @@
 #define CFI_BRK_IMM_BASE		0x8000
 #define CFI_BRK_IMM_MASK		(CFI_BRK_IMM_TARGET | CFI_BRK_IMM_TYPE)
 
-#endif
+#endif /* __ARM64_ASM_BRK_IMM_H */
include/arch/arm64/asm/esr.h renamed from arch/arm64/include/asm/esr.h +56 -6

74 lines not on this page · raw message

include/arch/arm64/asm/sysreg-defs.h renamed from arch/arm64/include/asm/sysreg-defs.h +3 -3
similarity index 99%
rename from arch/arm64/include/asm/sysreg-defs.h
rename to include/arch/arm64/asm/sysreg-defs.h
index 06bfc8e65b8d..58cb7eb0bf4e 100644
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
-#ifndef __ASM_SYSREG_DEFS_H
-#define __ASM_SYSREG_DEFS_H
+#ifndef __ARM64_ASM_SYSREG_DEFS_H
+#define __ARM64_ASM_SYSREG_DEFS_H
 
 #include <linux/bits.h>
 
@@ -969,4 +969,4 @@
 		 FIELD_PREP(reg##_##field##_MASK,	\
 			    SYS_FIELD_VALUE(reg, field, val))
 #endif /* __ASSEMBLER__ */
-#endif	/* __ASM_SYSREG_DEFS_H */
+#endif /* __ARM64_ASM_SYSREG_DEFS_H */
Signature · 1 line
2.53.0
author reviewer
#27 lore raw

PATCH v3 16/27 KVM: Make device name configurable

In reply to Steffen Eiden

Allow KVM implementations to choose alternative device names. This is especially useful for architectures providing multiple KVM implementations simultaneously.

---
 include/linux/kvm_host.h |  4 +++-
 virt/kvm/kvm_main.c      | 21 ++++++++++++++-------
 2 files changed, 17 insertions(+), 8 deletions(-)
include/linux/kvm_host.h +3 -1
index 76da4196b684..f44885fc6e6a 100644
@@ -1069,7 +1069,9 @@ static inline void kvm_irqfd_exit(void)
 {
 }
 #endif
-int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module);
+int kvm_init(unsigned int vcpu_size, unsigned int vcpu_align, struct module *module);
+int kvm_init_with_dev(unsigned int vcpu_size, unsigned int vcpu_align,
+		      struct module *module, const char *dev_name, int minor);
 void kvm_exit(void);
 
 bool file_is_kvm(struct file *file);
virt/kvm/kvm_main.c +14 -7
index 2eca44a69a9a..454d99b45fa0 100644
@@ -5541,9 +5541,7 @@ static struct file_operations kvm_chardev_ops = {
 };
 
 static struct miscdevice kvm_dev = {
-	KVM_MINOR,
-	"kvm",
-	&kvm_chardev_ops,
+	.fops = &kvm_chardev_ops,
 };
 
 #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
@@ -6321,13 +6319,13 @@ static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
 	kfree(env);
 }
 
-static void kvm_init_debug(void)
+static void kvm_init_debug(const char *dev_name)
 {
 	const struct file_operations *fops;
 	const struct kvm_stats_desc *pdesc;
 	int i;
 
-	kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
+	kvm_debugfs_dir = debugfs_create_dir(dev_name, NULL);
 
 	for (i = 0; i < kvm_vm_stats_header.num_desc; ++i) {
 		pdesc = &kvm_vm_stats_desc[i];
@@ -6463,11 +6461,20 @@ void kvm_unregister_perf_callbacks(void)
 }
 #endif
 
-int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
+int kvm_init(unsigned int vcpu_size, unsigned int vcpu_align, struct module *module)
+{
+	return kvm_init_with_dev(vcpu_size, vcpu_align, module, "kvm", KVM_MINOR);
+}
+
+int kvm_init_with_dev(unsigned int vcpu_size, unsigned int vcpu_align,
+		      struct module *module, const char *dev_name, int minor)
 {
 	int r;
 	int cpu;
 
+	kvm_dev.name = dev_name;
+	kvm_dev.minor = minor;
+
 	/* A kmem cache lets us meet the alignment requirements of fx_save. */
 	if (!vcpu_align)
 		vcpu_align = __alignof__(struct kvm_vcpu);
@@ -6505,7 +6512,7 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
 	kvm_preempt_ops.sched_in = kvm_sched_in;
 	kvm_preempt_ops.sched_out = kvm_sched_out;
 
-	kvm_init_debug();
+	kvm_init_debug(kvm_dev.name);
 
 	r = kvm_vfio_ops_init();
 	if (WARN_ON_ONCE(r))
Signature · 1 line
2.53.0
author reviewer
#35 lore raw

PATCH v3 21/27 s390/hwcaps: Report SAE support as hwcap

In reply to Steffen Eiden

From: Hendrik Brueckner <[email protected]>

Report SAE support as hwcap (and /proc/cpuinfo)

---
 arch/s390/include/asm/elf.h    | 2 ++
 arch/s390/include/asm/sclp.h   | 5 ++++-
 arch/s390/kernel/processor.c   | 3 +++
 drivers/s390/char/sclp_early.c | 1 +
 4 files changed, 10 insertions(+), 1 deletion(-)
arch/s390/include/asm/elf.h +2 -0
index bb63fa4d20bb..ad3108ecfb07 100644
@@ -123,6 +123,7 @@ enum {
 	HWCAP_NR_NNPA		= 20,
 	HWCAP_NR_PCI_MIO	= 21,
 	HWCAP_NR_SIE		= 22,
+	HWCAP_NR_SAE		= 23,
 	HWCAP_NR_MAX
 };
 
@@ -150,6 +151,7 @@ enum {
 #define HWCAP_NNPA		BIT(HWCAP_NR_NNPA)
 #define HWCAP_PCI_MIO		BIT(HWCAP_NR_PCI_MIO)
 #define HWCAP_SIE		BIT(HWCAP_NR_SIE)
+#define HWCAP_SAE		BIT(HWCAP_NR_SAE)
 
 /*
  * These are used to set parameters in the core dumps.
arch/s390/include/asm/sclp.h +4 -1
index 0f184dbdbe5e..18e46654227a 100644
@@ -52,7 +52,9 @@ struct sclp_core_entry {
 	u8 siif : 1;
 	u8 sigpif : 1;
 	u8 : 3;
-	u8 reserved2[3];
+	u8 aef: 1;
+	u8 : 7;
+	u8 reserved2[2];
 	u8 : 2;
 	u8 ib : 1;
 	u8 cei : 1;
@@ -104,6 +106,7 @@ struct sclp_info {
 	unsigned char has_aisii : 1;
 	unsigned char has_aeni : 1;
 	unsigned char has_aisi : 1;
+	unsigned char has_aef : 1;
 	unsigned int ibc;
 	unsigned int mtid;
 	unsigned int mtid_cp;
arch/s390/kernel/processor.c +3 -0
index e33a3eccda56..6da55a158027 100644
@@ -150,6 +150,7 @@ static void show_cpu_summary(struct seq_file *m, void *v)
 		[HWCAP_NR_NNPA]		= "nnpa",
 		[HWCAP_NR_PCI_MIO]	= "pcimio",
 		[HWCAP_NR_SIE]		= "sie",
+		[HWCAP_NR_SAE]		= "sae",
 	};
 	int i, cpu;
 
@@ -254,6 +255,8 @@ static int __init setup_hwcaps(void)
 	/* virtualization support */
 	if (sclp.has_sief2)
 		elf_hwcap |= HWCAP_SIE;
+	if (sclp.has_aef)
+		elf_hwcap |= HWCAP_SAE;
 
 	return 0;
 }
drivers/s390/char/sclp_early.c +1 -0
index 6bf501ad8ff0..26a76f09b19a 100644
@@ -94,6 +94,7 @@ static void __init sclp_early_facilities_detect(void)
 		sclp.has_ib = cpue->ib;
 		sclp.has_cei = cpue->cei;
 		sclp.has_skey = cpue->skey;
+		sclp.has_aef = cpue->aef;
 		break;
 	}
 
Signature · 1 line
2.53.0
author reviewer
#39 lore raw

PATCH v3 24/27 KVM: s390: arm64: Implement vm/vcpu create destroy.

In reply to Steffen Eiden

Implement init and destroy IOCTLS for vcpu and vm. Implement arch vm IOCTL. Use s390 gmap.

---
 arch/s390/kvm/arm64/arm.c | 161 ++++++++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/arm.h |  54 +++++++++++++
 2 files changed, 215 insertions(+)
arch/s390/kvm/arm64/arm.c +161 -0
index 591e2e874891..79ce6570fa03 100644
@@ -8,7 +8,10 @@
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
 
+#include <gmap.h>
+
 #include "arm.h"
+#include "reset.h"
 
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 {
@@ -45,6 +48,61 @@ static u64 kvm_max_guest_address(void)
 	return ALIGN_DOWN(max_addr + 1, 1 << 30) - 1;
 }
 
+static int kvm_gmap_init(struct kvm *kvm)
+{
+	struct crst_table *table;
+
+	kvm->arch.gmap = gmap_new(kvm, gpa_to_gfn(kvm->arch.guest_phys_size));
+
+	if (!kvm->arch.gmap)
+		return -ENOMEM;
+
+	/* arm64 (on s390) do not have pfault */
+	clear_bit(GMAP_FLAG_PFAULT_ENABLED, &kvm->arch.gmap->flags);
+	set_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &kvm->arch.gmap->flags);
+
+	table = dereference_asce(kvm->arch.gmap->asce);
+	crst_table_init((void *)table, _CRSTE_HOLE(table->crstes[0].h.tt).val);
+
+	return 0;
+}
+
+int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
+{
+	char debug_name[32];
+	int ret;
+
+	if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
+		return -EINVAL;
+
+	ret = kvm_vm_type_ipa_size_shift(type);
+	if (ret < 0)
+		return ret;
+	kvm->arch.guest_phys_size = 1UL << ret;
+
+	mutex_init(&kvm->arch.config_lock);
+	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
+
+	snprintf(debug_name, sizeof(debug_name), "kvm-arm64-%u", current->pid);
+	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
+	if (!kvm->arch.dbf)
+		return -ENOMEM;
+	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
+
+	ret = kvm_gmap_init(kvm);
+	if (ret)
+		goto out_err;
+	kvm->arch.mem_limit = kvm_max_guest_address();
+
+	VM_EVENT(kvm, 3, "vm created with type %lu", type);
+	return 0;
+
+out_err:
+	debug_unregister(kvm->arch.dbf);
+
+	return ret;
+}
+
 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
 {
 	return VM_FAULT_SIGBUS;
@@ -56,6 +114,13 @@ long kvm_arch_dev_ioctl(struct file *filp,
 	return -EINVAL;
 }
 
+void kvm_arch_destroy_vm(struct kvm *kvm)
+{
+	kvm_destroy_vcpus(kvm);
+	debug_unregister(kvm->arch.dbf);
+	kvm->arch.gmap = gmap_put(kvm->arch.gmap);
+}
+
 u32 get_kvm_ipa_limit(void)
 {
 	return fls64(kvm_max_guest_address() + 1) - 1;
@@ -66,10 +131,39 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
 	return 0;
 }
 
+int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
+{
+	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+
+	spin_lock_init(&vcpu->arch.mp_state_lock);
+
+	/* Force users to call KVM_ARM_VCPU_INIT */
+	vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
+
+	vcpu->arch.mc = kvm_s390_new_mmu_cache();
+	if (!vcpu->arch.mc)
+		return -ENOMEM;
+
+	sae_block->hbasce = vcpu->kvm->arch.gmap->asce.val;
+	sae_block->mso = 0L;
+	sae_block->msl = kvm_max_guest_address();
+
+	VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%p, sae block at 0x%p, satellite at 0x%p",
+		 vcpu->vcpu_id, vcpu, &vcpu->arch.sae_block, &vcpu->arch.save_area);
+	return 0;
+}
+
 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
 {
 }
 
+void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
+{
+	kvm_s390_free_mmu_cache(vcpu->arch.mc);
+
+	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
+}
+
 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
 {
 }
@@ -107,6 +201,52 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 	return 0;
 }
 
+int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
+			       struct kvm_dirty_log *log)
+{
+	return gmap_get_dirty_log(kvm, log);
+}
+
+bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+	scoped_guard(read_lock, &kvm->mmu_lock)
+		return gmap_age_gfn(kvm->arch.gmap, range->start, range->end);
+}
+
+void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
+{
+	gfn_t last_gfn = memslot->base_gfn + memslot->npages;
+
+	scoped_guard(read_lock, &kvm->mmu_lock)
+		gmap_sync_dirty_log(kvm->arch.gmap, memslot->base_gfn, last_gfn);
+}
+
+int kvm_arch_prepare_memory_region(struct kvm *kvm,
+				   const struct kvm_memory_slot *old,
+				   struct kvm_memory_slot *new,
+				   enum kvm_mr_change change)
+{
+	return gmap_prepare_memory_region(kvm, old, new, change);
+}
+
+void kvm_arch_commit_memory_region(struct kvm *kvm,
+				   struct kvm_memory_slot *old,
+				   const struct kvm_memory_slot *new,
+				   enum kvm_mr_change change)
+{
+	gmap_commit_memory_region(kvm, old, new, change);
+}
+
+bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+	return gmap_unmap_gfn_range(kvm->arch.gmap, range->slot, range->start, range->end);
+}
+
+bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+	return gmap_test_age_gfn(kvm, range);
+}
+
 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
 					     struct kvm_memory_slot *slot,
 					     gfn_t gfn_offset,
@@ -114,6 +254,27 @@ void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
 {
 }
 
+int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
+{
+	void __user *argp = (void __user *)arg;
+
+	switch (ioctl) {
+	case KVM_ARM_PREFERRED_TARGET: {
+		struct kvm_vcpu_init init = {
+			.target = KVM_ARM_TARGET_GENERIC_V8,
+		};
+
+		if (copy_to_user(argp, &init, sizeof(init)))
+			return -EFAULT;
+
+		return 0;
+	}
+
+	default:
+		return -EINVAL;
+	}
+}
+
 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
 {
 	return false;
arch/s390/kvm/arm64/arm.h +54 -0

58 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#16 lore raw

PATCH v3 9/27 KVM: arm64: Share kvm_emulate definitions

In reply to Steffen Eiden

Move functions and definitions useful for emulating arm64 instructions to include/kvm/arm64.

---
 arch/arm64/include/asm/kvm_emulate.h       | 235 +-----------------
 arch/arm64/kvm/hyp/include/hyp/adjust_pc.h |  13 -
 include/kvm/arm64/kvm_emulate.h            | 268 +++++++++++++++++++++
 3 files changed, 269 insertions(+), 247 deletions(-)
 create mode 100644 include/kvm/arm64/kvm_emulate.h
arch/arm64/include/asm/kvm_emulate.h +1 -234
index 822f6077b107..39fa3a12730c 100644
@@ -23,18 +23,7 @@
 #include <asm/virt.h>
 
 #include <kvm/arm64/kvm_arm.h>
-
-#define CURRENT_EL_SP_EL0_VECTOR	0x0
-#define CURRENT_EL_SP_ELx_VECTOR	0x200
-#define LOWER_EL_AArch64_VECTOR		0x400
-#define LOWER_EL_AArch32_VECTOR		0x600
-
-enum exception_type {
-	except_type_sync	= 0,
-	except_type_irq		= 0x80,
-	except_type_fiq		= 0x100,
-	except_type_serror	= 0x180,
-};
+#include <kvm/arm64/kvm_emulate.h>
 
 #define kvm_exception_type_names		\
 	{ except_type_sync,	"SYNC"   },	\
@@ -45,36 +34,8 @@ enum exception_type {
 bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
 void kvm_skip_instr32(struct kvm_vcpu *vcpu);
 
-void kvm_inject_undefined(struct kvm_vcpu *vcpu);
 void kvm_inject_sync(struct kvm_vcpu *vcpu, u64 esr);
-int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
-int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
 int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);
-void kvm_inject_size_fault(struct kvm_vcpu *vcpu);
-
-static inline int kvm_inject_sea_dabt(struct kvm_vcpu *vcpu, u64 addr)
-{
-	return kvm_inject_sea(vcpu, false, addr);
-}
-
-static inline int kvm_inject_sea_iabt(struct kvm_vcpu *vcpu, u64 addr)
-{
-	return kvm_inject_sea(vcpu, true, addr);
-}
-
-static inline int kvm_inject_serror(struct kvm_vcpu *vcpu)
-{
-	/*
-	 * ESR_ELx.ISV (later renamed to IDS) indicates whether or not
-	 * ESR_ELx.ISS contains IMPLEMENTATION DEFINED syndrome information.
-	 *
-	 * Set the bit when injecting an SError w/o an ESR to indicate ISS
-	 * does not follow the architected format.
-	 */
-	return kvm_inject_serror_esr(vcpu, ESR_ELx_ISV);
-}
-
-void kvm_vcpu_wfi(struct kvm_vcpu *vcpu);
 
 void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu);
 int kvm_inject_nested_sync(struct kvm_vcpu *vcpu, u64 esr_el2);
@@ -160,24 +121,6 @@ static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
 	*vcpu_cpsr(vcpu) |= PSR_AA32_T_BIT;
 }
 
-/*
- * vcpu_get_reg and vcpu_set_reg should always be passed a register number
- * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on
- * AArch32 with banked registers.
- */
-static __always_inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
-					 u8 reg_num)
-{
-	return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs[reg_num];
-}
-
-static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
-				unsigned long val)
-{
-	if (reg_num != 31)
-		vcpu_gp_regs(vcpu)->regs[reg_num] = val;
-}
-
 static inline bool vcpu_is_el2_ctxt(const struct kvm_cpu_context *ctxt)
 {
 	switch (ctxt->regs.pstate & (PSR_MODE32_BIT | PSR_MODE_MASK)) {
@@ -361,82 +304,11 @@ static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu)
 	return vcpu->arch.fault.disr_el1;
 }
 
-static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
-{
-	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_xVC_IMM_MASK;
-}
-
-static __always_inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu)
-{
-	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_ISV);
-}
-
 static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized(const struct kvm_vcpu *vcpu)
 {
 	return kvm_vcpu_get_esr(vcpu) & (ESR_ELx_CM | ESR_ELx_WNR | ESR_ELx_FSC);
 }
 
-static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu)
-{
-	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SSE);
-}
-
-static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
-{
-	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SF);
-}
-
-static __always_inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu)
-{
-	return (kvm_vcpu_get_esr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
-}
-
-static __always_inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu)
-{
-	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_S1PTW);
-}
-
-/* Always check for S1PTW *before* using this. */
-static __always_inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu)
-{
-	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_WNR;
-}
-
-static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu)
-{
-	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_CM);
-}
-
-static __always_inline unsigned int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)
-{
-	return 1 << ((kvm_vcpu_get_esr(vcpu) & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT);
-}
-
-/* This one is not specific to Data Abort */
-static __always_inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu)
-{
-	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_IL);
-}
-
-static __always_inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
-{
-	return ESR_ELx_EC(kvm_vcpu_get_esr(vcpu));
-}
-
-static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
-{
-	return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW;
-}
-
-static inline bool kvm_vcpu_trap_is_exec_fault(const struct kvm_vcpu *vcpu)
-{
-	return kvm_vcpu_trap_is_iabt(vcpu) && !kvm_vcpu_abt_iss1tw(vcpu);
-}
-
-static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)
-{
-	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC;
-}
 
 static inline
 bool kvm_vcpu_trap_is_permission_fault(const struct kvm_vcpu *vcpu)
@@ -472,36 +344,6 @@ static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
 	}
 }
 
-static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
-{
-	u64 esr = kvm_vcpu_get_esr(vcpu);
-	return ESR_ELx_SYS64_ISS_RT(esr);
-}
-
-static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
-{
-	if (kvm_vcpu_abt_iss1tw(vcpu)) {
-		/*
-		 * Only a permission fault on a S1PTW should be
-		 * considered as a write. Otherwise, page tables baked
-		 * in a read-only memslot will result in an exception
-		 * being delivered in the guest.
-		 *
-		 * The drawback is that we end-up faulting twice if the
-		 * guest is using any of HW AF/DB: a translation fault
-		 * to map the page containing the PT (read only at
-		 * first), then a permission fault to allow the flags
-		 * to be set.
-		 */
-		return kvm_vcpu_trap_is_permission_fault(vcpu);
-	}
-
-	if (kvm_vcpu_trap_is_iabt(vcpu))
-		return false;
-
-	return kvm_vcpu_dabt_iswrite(vcpu);
-}
-
 static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
 {
 	return __vcpu_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
@@ -537,81 +379,6 @@ static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
 	return vcpu_read_sys_reg(vcpu, r) & bit;
 }
 
-static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
-						    unsigned long data,
-						    unsigned int len)
-{
-	if (kvm_vcpu_is_be(vcpu)) {
-		switch (len) {
-		case 1:
-			return data & 0xff;
-		case 2:
-			return be16_to_cpu(data & 0xffff);
-		case 4:
-			return be32_to_cpu(data & 0xffffffff);
-		default:
-			return be64_to_cpu(data);
-		}
-	} else {
-		switch (len) {
-		case 1:
-			return data & 0xff;
-		case 2:
-			return le16_to_cpu(data & 0xffff);
-		case 4:
-			return le32_to_cpu(data & 0xffffffff);
-		default:
-			return le64_to_cpu(data);
-		}
-	}
-
-	return data;		/* Leave LE untouched */
-}
-
-static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
-						    unsigned long data,
-						    unsigned int len)
-{
-	if (kvm_vcpu_is_be(vcpu)) {
-		switch (len) {
-		case 1:
-			return data & 0xff;
-		case 2:
-			return cpu_to_be16(data & 0xffff);
-		case 4:
-			return cpu_to_be32(data & 0xffffffff);
-		default:
-			return cpu_to_be64(data);
-		}
-	} else {
-		switch (len) {
-		case 1:
-			return data & 0xff;
-		case 2:
-			return cpu_to_le16(data & 0xffff);
-		case 4:
-			return cpu_to_le32(data & 0xffffffff);
-		default:
-			return cpu_to_le64(data);
-		}
-	}
-
-	return data;		/* Leave LE untouched */
-}
-
-static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu)
-{
-	WARN_ON(vcpu_get_flag(vcpu, PENDING_EXCEPTION));
-	vcpu_set_flag(vcpu, INCREMENT_PC);
-}
-
-#define kvm_pend_exception(v, e)					\
-	do {								\
-		WARN_ON(vcpu_get_flag((v), INCREMENT_PC));		\
-		vcpu_set_flag((v), PENDING_EXCEPTION);			\
-		vcpu_set_flag((v), e);					\
-	} while (0)
-
 /*
  * Returns a 'sanitised' view of CPTR_EL2, translating from nVHE to the VHE
  * format if E2H isn't set.
arch/arm64/kvm/hyp/include/hyp/adjust_pc.h +0 -13
index 4fdfeabefeb4..15e1e5db73e1 100644
@@ -13,19 +13,6 @@
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_host.h>
 
-static inline void kvm_skip_instr(struct kvm_vcpu *vcpu)
-{
-	if (vcpu_mode_is_32bit(vcpu)) {
-		kvm_skip_instr32(vcpu);
-	} else {
-		*vcpu_pc(vcpu) += 4;
-		*vcpu_cpsr(vcpu) &= ~PSR_BTYPE_MASK;
-	}
-
-	/* advance the singlestep state machine */
-	*vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
-}
-
 /*
  * Skip an instruction which has been emulated at hyp while most guest sysregs
  * are live.
include/kvm/arm64/kvm_emulate.h new file +268 -0

268 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#36 lore raw

PATCH v3 22/27 KVM: s390: Add basic arm64 kvm module

In reply to Steffen Eiden

Add basic code for the new arm64 on s390 KVM implementation. Add kernel module boilerplate code and trivial functions.

---
 arch/s390/kvm/arm64/arm.c   | 186 ++++++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/arm.h   |   7 ++
 arch/s390/kvm/arm64/guest.c |  95 ++++++++++++++++++
 arch/s390/kvm/arm64/guest.h |  10 ++
 4 files changed, 298 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/arm.c
 create mode 100644 arch/s390/kvm/arm64/arm.h
 create mode 100644 arch/s390/kvm/arm64/guest.c
 create mode 100644 arch/s390/kvm/arm64/guest.h
arch/s390/kvm/arm64/arm.c new file +186 -0
new file mode 100644
index 000000000000..591e2e874891
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define KMSG_COMPONENT "kvm-s390-arm64"
+#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
+
+#include <linux/miscdevice.h>
+#include <linux/kvm.h>
+#include <linux/kvm_types.h>
+#include <linux/kvm_host.h>
+
+#include "arm.h"
+
+int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
+{
+	int ret;
+
+	switch (ext) {
+	case KVM_CAP_NR_VCPUS:
+	case KVM_CAP_MAX_VCPUS:
+	case KVM_CAP_MAX_VCPU_ID:
+		ret = KVM_MAX_VCPUS;
+		break;
+	case KVM_CAP_ARM_VM_IPA_SIZE:
+		ret = get_kvm_ipa_limit();
+		break;
+	case KVM_CAP_IOEVENTFD:
+	case KVM_CAP_ARM_USER_IRQ:
+		ret = 1;
+		break;
+	default:
+		ret = 0;
+	}
+
+	return ret;
+}
+
+static u64 kvm_max_guest_address(void)
+{
+	u64 max_addr;
+
+	if (sclp.hamax == U64_MAX)
+		max_addr = TASK_SIZE_MAX;
+	else
+		max_addr = min_t(u64, TASK_SIZE_MAX, sclp.hamax);
+	return ALIGN_DOWN(max_addr + 1, 1 << 30) - 1;
+}
+
+vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
+{
+	return VM_FAULT_SIGBUS;
+}
+
+long kvm_arch_dev_ioctl(struct file *filp,
+			unsigned int ioctl, unsigned long arg)
+{
+	return -EINVAL;
+}
+
+u32 get_kvm_ipa_limit(void)
+{
+	return fls64(kvm_max_guest_address() + 1) - 1;
+}
+
+int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
+{
+	return 0;
+}
+
+void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
+{
+}
+
+void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
+{
+}
+
+void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
+{
+}
+
+int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
+				    struct kvm_mp_state *mp_state)
+{
+	*mp_state = READ_ONCE(vcpu->arch.mp_state);
+	return 0;
+}
+
+int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
+				    struct kvm_mp_state *mp_state)
+{
+	return -EINVAL;
+}
+
+int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
+{
+	return 0;
+}
+
+unsigned long system_supported_vcpu_features(void)
+{
+	return KVM_VCPU_VALID_FEATURES;
+}
+
+int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
+			  bool line_status)
+{
+	return 0;
+}
+
+void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
+					     struct kvm_memory_slot *slot,
+					     gfn_t gfn_offset,
+					     unsigned long mask)
+{
+}
+
+bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
+{
+	return false;
+}
+
+void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
+{
+}
+
+void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
+{
+}
+
+int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
+		struct kvm *kvm, int irq_source_id,
+		int level, bool line_status)
+{
+	return -EINVAL;
+}
+
+int kvm_set_routing_entry(struct kvm *kvm,
+			  struct kvm_kernel_irq_routing_entry *e,
+			  const struct kvm_irq_routing_entry *ue)
+{
+	return -EINVAL;
+}
+
+void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
+				   struct kvm_memory_slot *slot)
+{
+}
+
+void kvm_arch_flush_shadow_all(struct kvm *kvm)
+{
+}
+
+int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+#ifdef CONFIG_HAVE_KVM_NO_POLL
+__weak bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+#endif
+
+long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
+				  unsigned long arg)
+{
+	return -ENOIOCTLCMD;
+}
+
+static int __init kvm_s390_arm64_init(void)
+{
+	if (!sclp.has_aef)
+		return -ENXIO;
+
+	return kvm_init_with_dev(sizeof(struct kvm_vcpu), 0, THIS_MODULE,
+				 KVM_DEV_NAME, MISC_DYNAMIC_MINOR);
+}
+
+static __exit void kvm_s390_arm64_exit(void)
+{
+	kvm_exit();
+}
+
+module_init(kvm_s390_arm64_init);
+module_exit(kvm_s390_arm64_exit);
arch/s390/kvm/arm64/arm.h new file +7 -0
new file mode 100644
index 000000000000..a3db254462c0
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ARCH_S390_KVM_ARM64_H
+#define ARCH_S390_KVM_ARM64_H
+
+#define KVM_DEV_NAME "kvm-arm64"
+
+#endif /* ARCH_S390_KVM_ARM64_H */
arch/s390/kvm/arm64/guest.c new file +95 -0

95 lines not on this page · raw message

arch/s390/kvm/arm64/guest.h new file +10 -0
new file mode 100644
index 000000000000..db635d513c2c
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef KVM_ARM_GUEST_H
+#define KVM_ARM_GUEST_H
+
+#include <linux/kvm_host.h>
+#include <kvm/arm64/guest.h>
+
+unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
+
+#endif /* KVM_ARM_GUEST_H */
Signature · 1 line
2.53.0
author reviewer
#43 lore raw

PATCH v3 26/27 KVM: s390: arm64: Implement basic page fault handler

In reply to Steffen Eiden

Add host functionality to page in guest memory. If the guest does something unexpected or illegal exit to userspace which very likely has to stop guest execution. This behaviour will be changed to guest error injects once all sysregs are accessible for the host.

---
 arch/s390/kvm/arm64/arm.c         |   1 +
 arch/s390/kvm/arm64/handle_exit.c |   2 +
 arch/s390/kvm/arm64/mmu.c         | 178 ++++++++++++++++++++++++++++++
 3 files changed, 181 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/mmu.c
arch/s390/kvm/arm64/arm.c +1 -0
index 18eca2261e0d..bf0866659421 100644
@@ -437,6 +437,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 				  vcpu->arch.sae_block.vir);
 			ret = -EINVAL;
 			break;
+		case SAE_ICPTR_HOST_ACCESS_EXCEPTION:
 		case SAE_ICPTR_SYNCHRONOUS_EXCEPTION:
 			ret = handle_trap_exceptions(vcpu);
 			break;
arch/s390/kvm/arm64/handle_exit.c +2 -0
index 89933a604876..bccd644fb07c 100644
@@ -47,4 +47,6 @@ static int handle_hvc(struct kvm_vcpu *vcpu)
 exit_handle_fn arm_exit_handlers[] = {
 	[0 ... ESR_ELx_EC_MAX]	= kvm_handle_unknown_ec,
 	[ESR_ELx_EC_HVC64]	= handle_hvc,
+	[ESR_ELx_EC_IABT_LOW]	= kvm_handle_guest_abort,
+	[ESR_ELx_EC_DABT_LOW]	= kvm_handle_guest_abort,
 };
arch/s390/kvm/arm64/mmu.c new file +178 -0

178 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#45 lore raw

PATCH v3 27/27 KVM: s390: arm64: Enable KVM_ARM64 config and Kbuild

In reply to Steffen Eiden

Add all Kbuild/Makefile configurations to build a second KVM module on s390 implementing the arm64-KVM API. To prevent symbol conflicts with kvm-s390 all internal symbols in kvm-arm64 are mangled if compiled as built-in. The new module ins named kvm-arm64.

As in this case the build does not go through the normal build process the module parameter handling would be messed up. By forcing KBUILD_MODNAME to kvm-arm64 all parameters are at the same location and not at object/basename of the object file the parameter is introduced.

---
 arch/s390/configs/defconfig  |   1 +
 arch/s390/include/asm/Kbuild |   3 +
 arch/s390/kvm/Kconfig        |   1 +
 arch/s390/kvm/Makefile       |   1 +
 arch/s390/kvm/arm64/Kconfig  |  22 +++++++
 arch/s390/kvm/arm64/Makefile | 108 +++++++++++++++++++++++++++++++++++
 arch/s390/tools/Makefile     |   2 +
 7 files changed, 138 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/Kconfig
 create mode 100644 arch/s390/kvm/arm64/Makefile
arch/s390/configs/defconfig +1 -0
index 21e803354845..65d5e739539b 100644
@@ -58,6 +58,7 @@ CONFIG_CMM=m
 CONFIG_APPLDATA_BASE=y
 CONFIG_S390_HYPFS_FS=y
 CONFIG_KVM_S390=m
+CONFIG_KVM_ARM64=m
 CONFIG_S390_UNWIND_SELFTEST=m
 CONFIG_S390_KPROBES_SANITY_TEST=m
 CONFIG_S390_MODULES_SANITY_TEST=m
arch/s390/include/asm/Kbuild +3 -0
index 0c1fc47c3ba0..f6fd67d1c651 100644
@@ -8,3 +8,6 @@ generic-y += asm-offsets.h
 generic-y += mcs_spinlock.h
 generic-y += mmzone.h
 generic-y += ring_buffer.h
+
+generated-y += sysreg-gen-defs.h
+generated-y += kvm_arm64_modname.h
arch/s390/kvm/Kconfig +1 -0
index f8d4a9a38dae..bbae58aa8bc4 100644
@@ -20,5 +20,6 @@ config KVM
 	tristate
 
 source "arch/s390/kvm/s390/Kconfig"
+source "arch/s390/kvm/arm64/Kconfig"
 
 endif # VIRTUALIZATION
arch/s390/kvm/Makefile +1 -0
index c43d7dffca13..38bdd7c9b42d 100644
@@ -4,3 +4,4 @@
 # Copyright IBM Corp. 2008
 
 obj-$(CONFIG_KVM_S390) += s390/
+obj-$(CONFIG_KVM_ARM64) += arm64/
arch/s390/kvm/arm64/Kconfig new file +22 -0
new file mode 100644
index 000000000000..65b9dc25fe62
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config KVM_ARM64
+	def_tristate y
+	prompt "Kernel-based Virtual Machine (KVM) support for arm64 guests"
+	depends on HAS_IOMEM
+	select KVM
+	select KVM_VFIO
+	select IRQ_BYPASS_MANAGER
+	select SCHED_INFO
+	select XARRAY_MULTI
+	select KVM_COMMON
+	select HAVE_KVM_CPU_RELAX_INTERCEPT
+	select HAVE_KVM_MSI
+	select HAVE_KVM_IRQCHIP
+	select HAVE_KVM_IRQ_ROUTING
+	select HAVE_KVM_VCPU_RUN_PID_CHANGE
+	select GUEST_PERF_EVENTS if PERF_EVENTS
+	help
+	  Support hosting virtualized arm64 guest machines on s390 host machines.
+
+	  If unsure, say N.
arch/s390/kvm/arm64/Makefile new file +108 -0

108 lines not on this page · raw message

arch/s390/tools/Makefile +2 -0
index f2862364fb42..921261dcde28 100644
@@ -6,6 +6,8 @@
 kapi := arch/$(ARCH)/include/generated/asm
 kapi-hdrs-y := $(kapi)/facility-defs.h $(kapi)/dis-defs.h
 
+include $(srctree)/arch/arm64/tools/Makefile.sysreg
+
 PHONY += kapi
 
 kapi:	$(kapi-hdrs-y)
Signature · 1 line
2.53.0

Steffen Eiden

author reviewer
#0 lore raw

v3 cover KVM: s390: Introduce arm64 KVM

By introducing a novel virtualization acceleration for the ARM architecture on s390 architecture, we aim to expand the platform's software ecosystem. This initial patch series lays the groundwork by enabling KVM-accelerated ARM CPU virtualization on s390. To achieve this, a common KVM layer between s390 and arm64 is introduced (see below for more details). Design considerations of arm64 on the s390 Architecture The s390 virtualization architecture is extended with a set of new instructions dedicated to supporting ARM-based virtual machines. The s390 KVM host acts as EL2 (hypervisor) for a EL1/EL0 (OS/application) arm64 guest. To achieve this, the new Start-Arm-Execution (SAE) instruction enables accelerated execution of arm64 VMs.  Additional new s390 instructions are introduced to query available arm64 features, used to populate the arm64 ID register contents, as well as, new s390 instructions to save/restore various arm64 registers in the VM context.

Summary of changes to arm64 KVM

UAPI / KAPI changes

The arm64 KVM UAPI headers are relocated to include/uapi/arch/arm64/, allowing non‑arm64 hosts (such as s390) to use the arm64 KVM userspace API. Likewise, the arm64 KVM kernel‑internal headers are relocated to include/kvm/arm64/, and several arm64 asm headers are relocated to include/arch/arm64/asm for architecture‑independent consumption.

Refactoring of arm64 headers and shared arm64 KVM functionality

To avoid code duplication, sharing logic between arm64/kvm and s390/kvm/arm64 is maximized while refactoring noise was minimized. IOCTL (arch) entry points are deliberately kept separate for arm64 and arm64 on s390. This ensures full control over execution paths for each KVM implementation. The arm64 sysreg definitions and pstate/SPSR constants are extracted from their native headers into dedicated, shareable files (sysreg-defs.h, pstate.h). This enables other architectures to access sysreg name‑to‑ID mappings and pstate definitions without relying on arm64‑specific, non‑shareable headers.

A new virt/kvm/arm64/ source directory is introduced to hold shared Arm64 KVM implementation code, including guest register handling, exit handling, general‑register reset logic, and IPA size/shift
calculations. Several functions are also refactored to improve portability for non‑arm64 KVM implementations.

Maintainership considerations

Introducing a shared arm64 KVM code base for both native arm64 and s390 implementations may have subtle implications for each architecture, depending on the context and the contributor’s expertise. We therefore recognize the importance of clear maintainership guidelines and well-defined review processes to ensure the stability and correctness of both implementations. We welcome community feedback on how best to structure maintainership and the review workflow, and we are open to suggestions for effective coordination between the arm64 and s390 maintainer teams.

UAPI design

The arm64 KVM UAPI headers are relocated from arch/arm64/include/uapi/ to the new include/uapi/arch/arm64/ directory, and generic KVM definitions are split into include/uapi/linux/kvm-generic.h for use across all architectures. To maintain ABI compatibility, type aliases are introduced; they resolve to native arm64 types on arm64 and to equivalent inline struct definitions on foreign hosts. The build system installs the headers in their original location on arm64, while conditionally exporting them to the new location for s390.

KAPI design

The include/arch/arm64/asm/ directory is introduced to host arm64 asm headers that are independent of the host CPU. On native arm64 systems, this path is added with the highest precedence so that existing <asm/header.h> includes continue to work without modification. Foreign architectures can opt in by adding this path to their include search, enabling them to use these architecture‑agnostic headers.

The KVM‑related headers are moved to include/kvm/arm64/, decoupling them from the arm64 architecture directory. The design convention is that architecture‑specific headers under <arch>/include/asm/ include from this shared location, allowing non‑arm64 hosts to consume the arm64 KVM infrastructure without duplicating code.

Series structure

KVM symbol cleanup:
Three preparatory patches clean up KVM module symbol exports, making it possible to load two KVM modules side by side.

Arm64 header and code sharing:
Selected arm64 UAPI, asm, and KVM kernel headers are
refactored into architecture-agnostic include paths, and shared arm64 KVM code is relocated accordingly. This enables non-arm64 hosts to use the arm64 KVM infrastructure without duplication.

s390 & KVM reorganization:
The existing s390 KVM code is moved into a dedicated s390 subdirectory to make room for a second KVM implementation alongside it. The KVM core is extended to support a configurable device name (needed for two KVM devices on one architecture) Arm64

KVM on s390:
The SAE (Start Arm Execution) instruction is introduced as the s390 mechanism for running Arm64 guests, and a new kvm-arm64 module is built up incrementally.

Upcoming patch series will introduce system-register handling, interrupt support, hypercalls, and additional features such as PMU.

We appreciate your feedback and review.

The Linux on s390 team

since v2 [2]:
- removed the already picked MAINTAINER patch
- arm64
- inverted the direction how arm uapi is generated. Now, foreign architectures are copying arch/arm64/asm/kvm.h to the dedicated generated uapi location location instead of vice versa. Arm64 keeps its UAPI header local but they are annotated with a
GENERATED in the guard
- s390
- improve inline asm in sae.h
- load & restore fpu & vx regs

since v1 [1]:
- moved mispositioned kvm_supports_32bit_el0 from shared to s390 (thanks Marc)
- various small issues fixed (non exhaustive)
- un-move macros creating arm instructions from sysreg-defs.h back to sysreg.h
- reset sp register
- use 'a' inline asm modifier for address registers in stiasrm and lasrm
- clear x2 after SAE exit
- fix unnecessary recompilations of the kvm-arm64 module
- Add Steffen to KVM/arm64 reviewer
- Rebased onto & included Paolos latest vfio series

[1] https://lore.kernel.org/linux-s390/[email protected]/
[2] https://lore.kernel.org/linux-s390/[email protected]/

Hendrik Brueckner (1):
s390/hwcaps: Report SAE support as hwcap

Nina Schoetterl-Glausch (3):
arm64: Extract sysreg definitions
arm64: Extract pstate definitions from ptrace
KVM: arm64: Share reset general register code

Paolo Bonzini (3):
VFIO: take reference to the KVM module
KVM, vfio: remove symbol_get(kvm_get_kvm_safe) from vfio KVM, vfio: remove symbol_get(kvm_put_kvm) from vfio

Steffen Eiden (20):
uapi: KVM: Provide arm64 UAPI for other host architectures
arm64: Provide arm64 API for non-native architectures
KVM: arm64: Provide arm64 KVM API for non-native architectures
KVM: arm64: Share kvm_emulate definitions
KVM: arm64: Make some arm64 KVM code shareable
KVM: arm64: Access elements of vcpu_gp_regs individually
KVM: arm64: Extract & share ipa size shift calculation
KVM: s390: Move s390 kvm code into a subdirectory
KVM: S390: Refactor gmap
KVM: Make device name configurable
KVM: Remove KVM_MMIO as config option
KVM: s390: Prepare kvm-s390 for a second kvm module
s390: Introduce Start Arm Execution instruction
KVM: s390: arm64: Introduce host definitions
KVM: s390: Add basic arm64 kvm module
KVM: s390: arm64: Implement required functions
KVM: s390: arm64: Implement vm/vcpu create destroy.
KVM: s390: arm64: Implement vCPU IOCTLs
KVM: s390: arm64: Implement basic page fault handler
KVM: s390: arm64: Enable KVM_ARM64 config and Kbuild

 MAINTAINERS                                   |    5 +
 Makefile                                      |    1 +
 arch/arm64/Makefile                           |    2 +
 arch/arm64/include/asm/Kbuild                 |    2 +-
 arch/arm64/include/asm/el2_setup.h            |    2 +-
 arch/arm64/include/asm/hardirq.h              |    2 +-
 arch/arm64/include/asm/kvm_emulate.h          |  245 +--
 arch/arm64/include/asm/kvm_host.h             |  200 +-
 arch/arm64/include/asm/kvm_mmu.h              |   41 +-
 arch/arm64/include/asm/ptrace.h               |   34 +-
 arch/arm64/include/asm/sysreg.h               |  967 +---------
 arch/arm64/include/uapi/asm/kvm.h             |   28 +-
 arch/arm64/include/uapi/asm/pstate.h          |   53 +
 arch/arm64/include/uapi/asm/ptrace.h          |   49 +-
 arch/arm64/include/uapi/asm/sve_context.h     |    4 +-
 arch/arm64/kernel/head.S                      |    2 +-
 arch/arm64/kernel/hyp-stub.S                  |    2 +-
 arch/arm64/kernel/traps.c                     |   53 -
 arch/arm64/kvm/Kconfig                        |    1 -
 arch/arm64/kvm/Makefile                       |    5 +-
 arch/arm64/kvm/arm.c                          |   54 +-
 arch/arm64/kvm/debug.c                        |    2 +-
 arch/arm64/kvm/guest.c                        |  294 +--
 arch/arm64/kvm/handle_exit.c                  |   52 +-
 arch/arm64/kvm/hyp/entry.S                    |    2 +-
 arch/arm64/kvm/hyp/exception.c                |    7 +-
 arch/arm64/kvm/hyp/hyp-entry.S                |    2 +-
 arch/arm64/kvm/hyp/include/hyp/adjust_pc.h    |   17 +-
 arch/arm64/kvm/hyp/include/hyp/switch.h       |    6 +-
 arch/arm64/kvm/hyp/nvhe/host.S                |    2 +-
 arch/arm64/kvm/hyp/nvhe/hyp-init.S            |    2 +-
 arch/arm64/kvm/mmu.c                          |   21 +-
 arch/arm64/kvm/nested.c                       |    2 +-
 arch/arm64/kvm/reset.c                        |   34 +-
 arch/arm64/kvm/sys_regs.c                     |    2 +-
 arch/arm64/kvm/trace_arm.h                    |   25 -
 arch/arm64/kvm/vgic/vgic-its.c                |    2 +-
 arch/arm64/kvm/vgic/vgic-mmio-v3.c            |    2 +-
 arch/arm64/kvm/vgic/vgic-v3-nested.c          |    2 +-
 arch/arm64/tools/Makefile                     |   10 +-
 arch/arm64/tools/Makefile.sysreg              |   12 +
 arch/arm64/tools/gen-sysreg.awk               |    6 +-
 arch/loongarch/include/asm/kvm_host.h         |    2 +
 arch/loongarch/kvm/Kconfig                    |    1 -
 arch/mips/include/asm/kvm_host.h              |    2 +
 arch/mips/kvm/Kconfig                         |    1 -
 arch/powerpc/include/asm/kvm_host.h           |    7 +
 arch/powerpc/kvm/Kconfig                      |    4 -
 arch/riscv/include/asm/kvm_host.h             |    2 +
 arch/riscv/kvm/Kconfig                        |    1 -
 arch/s390/Kconfig                             |    2 +-
 arch/s390/Makefile                            |    2 +
 arch/s390/boot/ipl_parm.c                     |    2 +-
 arch/s390/boot/uv.c                           |    2 +-
 arch/s390/configs/debug_defconfig             |    2 +-
 arch/s390/configs/defconfig                   |    3 +-
 arch/s390/include/asm/Kbuild                  |    3 +
 arch/s390/include/asm/asm-prototypes.h        |    1 +
 arch/s390/include/asm/elf.h                   |    2 +
 arch/s390/include/asm/kvm.h                   |    6 +
 arch/s390/include/asm/kvm_emulate.h           |  131 ++
 arch/s390/include/asm/kvm_host.h              |  749 +-------
 arch/s390/include/asm/kvm_host_arm64.h        |  212 +++
 arch/s390/include/asm/kvm_host_arm64_types.h  |  129 ++
 .../asm/{kvm_host.h => kvm_host_s390.h}       |   19 +-
 ...kvm_host_types.h => kvm_host_s390_types.h} |    0
 arch/s390/include/asm/kvm_mmu.h               |   12 +
 arch/s390/include/asm/kvm_nested.h            |   13 +
 arch/s390/include/asm/sae.h                   |   56 +
 arch/s390/include/asm/sclp.h                  |    5 +-
 arch/s390/include/asm/stacktrace.h            |    5 +
 arch/s390/kernel/asm-offsets.c                |    3 +-
 arch/s390/kernel/early.c                      |    2 +-
 arch/s390/kernel/entry.S                      |   35 +-
 arch/s390/kernel/perf_event.c                 |    2 +-
 arch/s390/kernel/processor.c                  |    3 +
 arch/s390/kvm/Kconfig                         |   36 +-
 arch/s390/kvm/Makefile                        |   12 +-
 arch/s390/kvm/arm64/Kconfig                   |   22 +
 arch/s390/kvm/arm64/Makefile                  |  108 ++
 arch/s390/kvm/arm64/arm.c                     |  708 +++++++
 arch/s390/kvm/arm64/arm.h                     |   61 +
 arch/s390/kvm/arm64/guest.c                   |  162 ++
 arch/s390/kvm/arm64/guest.h                   |   15 +
 arch/s390/kvm/arm64/handle_exit.c             |   52 +
 arch/s390/kvm/arm64/inject_fault.c            |   21 +
 arch/s390/kvm/arm64/mmu.c                     |  178 ++
 arch/s390/kvm/arm64/reset.c                   |   57 +
 arch/s390/kvm/arm64/reset.h                   |   11 +
 arch/s390/kvm/gmap/Makefile                   |    5 +
 arch/s390/kvm/{ => gmap}/dat.c                |    0
 arch/s390/kvm/{ => gmap}/dat.h                |    6 +-
 arch/s390/kvm/{ => gmap}/faultin.c            |   11 +-
 arch/s390/kvm/{ => gmap}/faultin.h            |    6 +-
 arch/s390/kvm/{ => gmap}/gmap.c               |   13 +-
 arch/s390/kvm/{ => gmap}/gmap.h               |   17 +-
 arch/s390/kvm/gmap/mmu.c                      |  147 ++
 arch/s390/kvm/gmap/trace-gmap.h               |   59 +
 arch/s390/kvm/{ => s390}/Kconfig              |   27 +-
 arch/s390/kvm/{ => s390}/Makefile             |   10 +-
 arch/s390/kvm/{ => s390}/diag.c               |    2 +-
 arch/s390/kvm/{ => s390}/gaccess.c            |    2 +-
 arch/s390/kvm/{ => s390}/gaccess.h            |    2 +-
 arch/s390/kvm/{ => s390}/guestdbg.c           |    2 +-
 arch/s390/kvm/{ => s390}/intercept.c          |    2 +-
 arch/s390/kvm/{ => s390}/interrupt.c          |    2 +-
 arch/s390/kvm/{ => s390}/pci.c                |    2 +-
 arch/s390/kvm/{ => s390}/pci.h                |    0
 arch/s390/kvm/{ => s390}/priv.c               |    2 +-
 arch/s390/kvm/{ => s390}/pv.c                 |    2 +-
 arch/s390/kvm/{kvm-s390.c => s390/s390.c}     |  118 +-
 arch/s390/kvm/{kvm-s390.h => s390/s390.h}     |   18 +-
 arch/s390/kvm/{ => s390}/sigp.c               |    2 +-
 arch/s390/kvm/{ => s390}/trace-s390.h         |    0
 arch/s390/kvm/{ => s390}/trace.h              |   14 -
 arch/s390/kvm/{ => s390}/vsie.c               |    2 +-
 arch/s390/tools/Makefile                      |    2 +
 arch/s390/tools/opcodes.txt                   |    3 +
 arch/x86/include/asm/kvm_host.h               |    2 +
 arch/x86/kvm/Kconfig                          |    1 -
 arch/x86/kvm/mmu/tdp_mmu.c                    |    2 +-
 arch/x86/kvm/vmx/nested.h                     |    4 +-
 drivers/s390/char/sclp_early.c                |    1 +
 drivers/vfio/device_cdev.c                    |    2 +-
 drivers/vfio/group.c                          |    5 +-
 drivers/vfio/vfio.h                           |   15 +-
 drivers/vfio/vfio_main.c                      |   51 +-
 .../arch/arm64}/asm/brk-imm.h                 |    6 +-
 .../include => include/arch/arm64}/asm/esr.h  |   62 +-
 include/arch/arm64/asm/pstate.h               |   46 +
 .../arch/arm64/asm/sysreg-defs.h              |  355 +---
 include/kvm/arm64/guest.h                     |   13 +
 include/kvm/arm64/handle_exit.h               |   14 +
 .../asm => include/kvm/arm64}/kvm_arm.h       |    5 +-
 include/kvm/arm64/kvm_emulate.h               |  268 +++
 include/kvm/arm64/kvm_host.h                  |  197 ++
 include/kvm/arm64/kvm_mmu.h                   |   47 +
 include/kvm/arm64/reset.h                     |    8 +
 include/linux/kvm_host.h                      |   17 +-
 include/linux/kvm_types.h                     |   30 +
 include/linux/vfio.h                          |    4 +-
 include/uapi/Kbuild                           |    4 +
 include/uapi/arch/arm64/asm/Kbuild            |    6 +
 include/uapi/arch/arm64/linux/kvm.h           |    8 +
 include/uapi/linux/{kvm.h => kvm-generic.h}   |   11 +-
 include/uapi/linux/kvm.h                      | 1660 +----------------
 scripts/Makefile.asm-headers                  |   13 +-
 usr/include/Makefile                          |    1 +
 virt/kvm/Kconfig                              |    3 -
 virt/kvm/Makefile.kvm                         |    3 +-
 virt/kvm/arm64/Makefile.kvm                   |   13 +
 virt/kvm/arm64/arm.c                          |   75 +
 virt/kvm/arm64/guest.c                        |  302 +++
 virt/kvm/arm64/handle_exit.c                  |   54 +
 {arch/arm64/kvm => virt/kvm/arm64}/mmio.c     |    1 +
 virt/kvm/arm64/reset.c                        |   43 +
 virt/kvm/arm64/trace.h                        |   42 +
 virt/kvm/coalesced_mmio.c                     |    3 +
 virt/kvm/coalesced_mmio.h                     |    2 +-
 virt/kvm/kvm_main.c                           |   63 +-
 virt/kvm/vfio.c                               |    4 +-
 161 files changed, 3925 insertions(+), 5139 deletions(-)
 create mode 100644 arch/arm64/include/uapi/asm/pstate.h
 create mode 100644 arch/arm64/tools/Makefile.sysreg
 create mode 100644 arch/s390/include/asm/kvm.h
 create mode 100644 arch/s390/include/asm/kvm_emulate.h
 create mode 100644 arch/s390/include/asm/kvm_host_arm64.h
 create mode 100644 arch/s390/include/asm/kvm_host_arm64_types.h
 copy arch/s390/include/asm/{kvm_host.h => kvm_host_s390.h} (98%)
 rename arch/s390/include/asm/{kvm_host_types.h => kvm_host_s390_types.h} (100%)
 create mode 100644 arch/s390/include/asm/kvm_mmu.h
 create mode 100644 arch/s390/include/asm/kvm_nested.h
 create mode 100644 arch/s390/include/asm/sae.h
 create mode 100644 arch/s390/kvm/arm64/Kconfig
 create mode 100644 arch/s390/kvm/arm64/Makefile
 create mode 100644 arch/s390/kvm/arm64/arm.c
 create mode 100644 arch/s390/kvm/arm64/arm.h
 create mode 100644 arch/s390/kvm/arm64/guest.c
 create mode 100644 arch/s390/kvm/arm64/guest.h
 create mode 100644 arch/s390/kvm/arm64/handle_exit.c
 create mode 100644 arch/s390/kvm/arm64/inject_fault.c
 create mode 100644 arch/s390/kvm/arm64/mmu.c
 create mode 100644 arch/s390/kvm/arm64/reset.c
 create mode 100644 arch/s390/kvm/arm64/reset.h
 create mode 100644 arch/s390/kvm/gmap/Makefile
 rename arch/s390/kvm/{ => gmap}/dat.c (100%)
 rename arch/s390/kvm/{ => gmap}/dat.h (99%)
 rename arch/s390/kvm/{ => gmap}/faultin.c (96%)
 rename arch/s390/kvm/{ => gmap}/faultin.h (96%)
 rename arch/s390/kvm/{ => gmap}/gmap.c (99%)
 rename arch/s390/kvm/{ => gmap}/gmap.h (95%)
 create mode 100644 arch/s390/kvm/gmap/mmu.c
 create mode 100644 arch/s390/kvm/gmap/trace-gmap.h
 copy arch/s390/kvm/{ => s390}/Kconfig (60%)
 copy arch/s390/kvm/{ => s390}/Makefile (53%)
 rename arch/s390/kvm/{ => s390}/diag.c (99%)
 rename arch/s390/kvm/{ => s390}/gaccess.c (99%)
 rename arch/s390/kvm/{ => s390}/gaccess.h (99%)
 rename arch/s390/kvm/{ => s390}/guestdbg.c (99%)
 rename arch/s390/kvm/{ => s390}/intercept.c (99%)
 rename arch/s390/kvm/{ => s390}/interrupt.c (99%)
 rename arch/s390/kvm/{ => s390}/pci.c (99%)
 rename arch/s390/kvm/{ => s390}/pci.h (100%)
 rename arch/s390/kvm/{ => s390}/priv.c (99%)
 rename arch/s390/kvm/{ => s390}/pv.c (99%)
 rename arch/s390/kvm/{kvm-s390.c => s390/s390.c} (98%)
 rename arch/s390/kvm/{kvm-s390.h => s390/s390.h} (97%)
 rename arch/s390/kvm/{ => s390}/sigp.c (99%)
 rename arch/s390/kvm/{ => s390}/trace-s390.h (100%)
 rename arch/s390/kvm/{ => s390}/trace.h (97%)
 rename arch/s390/kvm/{ => s390}/vsie.c (99%)
 rename {arch/arm64/include => include/arch/arm64}/asm/brk-imm.h (92%)
 rename {arch/arm64/include => include/arch/arm64}/asm/esr.h (88%)
 create mode 100644 include/arch/arm64/asm/pstate.h
 copy arch/arm64/include/asm/sysreg.h => include/arch/arm64/asm/sysreg-defs.h (79%)
 create mode 100644 include/kvm/arm64/guest.h
 create mode 100644 include/kvm/arm64/handle_exit.h
 rename {arch/arm64/include/asm => include/kvm/arm64}/kvm_arm.h (99%)
 create mode 100644 include/kvm/arm64/kvm_emulate.h
 create mode 100644 include/kvm/arm64/kvm_host.h
 create mode 100644 include/kvm/arm64/kvm_mmu.h
 create mode 100644 include/kvm/arm64/reset.h
 create mode 100644 include/uapi/arch/arm64/asm/Kbuild
 create mode 100644 include/uapi/arch/arm64/linux/kvm.h
 copy include/uapi/linux/{kvm.h => kvm-generic.h} (99%)
 create mode 100644 virt/kvm/arm64/Makefile.kvm
 create mode 100644 virt/kvm/arm64/arm.c
 create mode 100644 virt/kvm/arm64/guest.c
 create mode 100644 virt/kvm/arm64/handle_exit.c
 rename {arch/arm64/kvm => virt/kvm/arm64}/mmio.c (99%)
 create mode 100644 virt/kvm/arm64/reset.c
 create mode 100644 virt/kvm/arm64/trace.h

base-commit: 8fde5d1d47f69db6082dfa34500c27f8485389a5

Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH v3 1/27 VFIO: take reference to the KVM module

From: Paolo Bonzini <[email protected]>

VFIO is implicitly taking a reference to the KVM module between vfio_device_get_kvm_safe and vfio_device_put_kvm, thanks to symbol_get and symbol_put.

In preparation for removing symbol_get and symbol_put themselves from VFIO, actually store a pointer to the KVM module and use module_get()/module_put() to keep KVM alive.

---
 drivers/vfio/device_cdev.c |  2 +-
 drivers/vfio/group.c       |  5 +++--
 drivers/vfio/vfio.h        | 15 ++++++++++-----
 drivers/vfio/vfio_main.c   | 37 +++++++++++++++++++++++--------------
 include/linux/vfio.h       |  3 ++-
 virt/kvm/vfio.c            |  4 ++--
 6 files changed, 41 insertions(+), 25 deletions(-)
drivers/vfio/device_cdev.c +1 -1
index 54abf312cf04..a729be99d06c 100644
@@ -56,7 +56,7 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
 {
 	spin_lock(&df->kvm_ref_lock);
-	vfio_device_get_kvm_safe(df->device, df->kvm);
+	vfio_device_get_kvm_safe(df->device, df->kvm, df->kvm_module);
 	spin_unlock(&df->kvm_ref_lock);
 }
 
drivers/vfio/group.c +3 -2
index b2299e5bc6df..fc24c0b2cc6f 100644
@@ -163,7 +163,7 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
 static void vfio_device_group_get_kvm_safe(struct vfio_device *device)
 {
 	spin_lock(&device->group->kvm_ref_lock);
-	vfio_device_get_kvm_safe(device, device->group->kvm);
+	vfio_device_get_kvm_safe(device, device->group->kvm, device->group->kvm_module);
 	spin_unlock(&device->group->kvm_ref_lock);
 }
 
@@ -860,10 +860,11 @@ bool vfio_group_enforced_coherent(struct vfio_group *group)
 	return ret;
 }
 
-void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
+void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm, struct module *kvm_module)
 {
 	spin_lock(&group->kvm_ref_lock);
 	group->kvm = kvm;
+	group->kvm_module = kvm_module;
 	spin_unlock(&group->kvm_ref_lock);
 }
 
drivers/vfio/vfio.h +10 -5
index e4b72e79b7e3..8209da17246c 100644
@@ -22,8 +22,9 @@ struct vfio_device_file {
 
 	u8 access_granted;
 	u32 devid; /* only valid when iommufd is valid */
-	spinlock_t kvm_ref_lock; /* protect kvm field */
+	spinlock_t kvm_ref_lock; /* protect kvm and kvm_module fields */
 	struct kvm *kvm;
+	struct module *kvm_module;
 	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
 };
 
@@ -89,6 +90,7 @@ struct vfio_group {
 	enum vfio_group_type		type;
 	struct mutex			group_lock;
 	struct kvm			*kvm;
+	struct module			*kvm_module;
 	struct file			*opened_file;
 	struct iommufd_ctx		*iommufd;
 	spinlock_t			kvm_ref_lock;
@@ -107,7 +109,7 @@ void vfio_device_group_unuse_iommu(struct vfio_device *device);
 void vfio_df_group_close(struct vfio_device_file *df);
 struct vfio_group *vfio_group_from_file(struct file *file);
 bool vfio_group_enforced_coherent(struct vfio_group *group);
-void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
+void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm, struct module *kvm_module);
 bool vfio_device_has_container(struct vfio_device *device);
 int __init vfio_group_init(void);
 void vfio_group_cleanup(void);
@@ -170,7 +172,8 @@ static inline bool vfio_group_enforced_coherent(struct vfio_group *group)
 	return true;
 }
 
-static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
+static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm,
+				      struct module *kvm_module)
 {
 }
 
@@ -434,11 +437,13 @@ static inline void vfio_virqfd_exit(void)
 #endif
 
 #if IS_ENABLED(CONFIG_KVM)
-void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm);
+void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,
+			      struct module *kvm_module);
 void vfio_device_put_kvm(struct vfio_device *device);
 #else
 static inline void vfio_device_get_kvm_safe(struct vfio_device *device,
-					    struct kvm *kvm)
+					    struct kvm *kvm,
+					    struct module *kvm_module)
 {
 }
 
drivers/vfio/vfio_main.c +23 -14
index 6222376ab6ab..78b9cad348b1 100644
@@ -442,7 +442,7 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 
 #if IS_ENABLED(CONFIG_KVM)
-void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm)
+void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm, struct module *kvm_module)
 {
 	void (*pfn)(struct kvm *kvm);
 	bool (*fn)(struct kvm *kvm);
@@ -453,25 +453,31 @@ void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm)
 	if (!kvm)
 		return;
 
+	if (!try_module_get(kvm_module))
+		return;
+
 	pfn = symbol_get(kvm_put_kvm);
 	if (WARN_ON(!pfn))
-		return;
+		goto out_put_mod;
 
 	fn = symbol_get(kvm_get_kvm_safe);
-	if (WARN_ON(!fn)) {
-		symbol_put(kvm_put_kvm);
-		return;
-	}
+	if (WARN_ON(!fn))
+		goto out_put_sym;
 
 	ret = fn(kvm);
 	symbol_put(kvm_get_kvm_safe);
-	if (!ret) {
-		symbol_put(kvm_put_kvm);
-		return;
-	}
+	if (!ret)
+		goto out_put_sym;
 
 	device->put_kvm = pfn;
 	device->kvm = kvm;
+	device->kvm_module = kvm_module;
+	return;
+
+out_put_sym:
+	symbol_put(kvm_put_kvm);
+out_put_mod:
+	module_put(kvm_module);
 }
 
 void vfio_device_put_kvm(struct vfio_device *device)
@@ -490,6 +496,8 @@ void vfio_device_put_kvm(struct vfio_device *device)
 
 clear:
 	device->kvm = NULL;
+	module_put(device->kvm_module);
+	device->kvm_module = NULL;
 }
 #endif
 
@@ -1513,7 +1521,7 @@ bool vfio_file_enforced_coherent(struct file *file)
 }
 EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
 
-static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
+static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm, struct module *kvm_module)
 {
 	struct vfio_device_file *df = file->private_data;
 
@@ -1524,6 +1532,7 @@ static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
 	 */
 	spin_lock(&df->kvm_ref_lock);
 	df->kvm = kvm;
+	df->kvm_module = kvm_module;
 	spin_unlock(&df->kvm_ref_lock);
 }
 
@@ -1535,16 +1544,16 @@ static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
  * When a VFIO device is first opened the KVM will be available in
  * device->kvm if one was associated with the file.
  */
-void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
+void vfio_file_set_kvm(struct file *file, struct kvm *kvm, struct module *kvm_module)
 {
 	struct vfio_group *group;
 
 	group = vfio_group_from_file(file);
 	if (group)
-		vfio_group_set_kvm(group, kvm);
+		vfio_group_set_kvm(group, kvm, kvm_module);
 
 	if (vfio_device_from_file(file))
-		vfio_device_file_set_kvm(file, kvm);
+		vfio_device_file_set_kvm(file, kvm, kvm_module);
 }
 EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
 
include/linux/vfio.h +2 -1
index 31b826efba00..5bfec0339804 100644
@@ -55,6 +55,7 @@ struct vfio_device {
 	unsigned int migration_flags;
 	u8 precopy_info_v2;
 	struct kvm *kvm;
+	struct module *kvm_module;
 
 	/* Members below here are private, not for driver use */
 	unsigned int index;
@@ -377,7 +378,7 @@ static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *devi
 #endif
 bool vfio_file_is_valid(struct file *file);
 bool vfio_file_enforced_coherent(struct file *file);
-void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
+void vfio_file_set_kvm(struct file *file, struct kvm *kvm, struct module *kvm_module);
 
 #define VFIO_PIN_PAGES_MAX_ENTRIES	(PAGE_SIZE/sizeof(unsigned long))
 
virt/kvm/vfio.c +2 -2
index 9f9acb66cc1e..515ed445d8e1 100644
@@ -37,13 +37,13 @@ struct kvm_vfio {
 
 static void kvm_vfio_file_set_kvm(struct file *file, struct kvm *kvm)
 {
-	void (*fn)(struct file *file, struct kvm *kvm);
+	void (*fn)(struct file *file, struct kvm *kvm, struct module *kvm_module);
 
 	fn = symbol_get(vfio_file_set_kvm);
 	if (!fn)
 		return;
 
-	fn(file, kvm);
+	fn(file, kvm, kvm ? THIS_MODULE : NULL);
 
 	symbol_put(vfio_file_set_kvm);
 }
Signature · 1 line
2.53.0
author reviewer
#38 lore raw

PATCH v3 23/27 KVM: s390: arm64: Implement required functions

In reply to Steffen Eiden

Implement the mostly trivial functions that the shared arm64 (kvm) headers oblige s390 to implement.

Implement a very basic smccc handler that (non-compliantly) is just able to stop a vcpu.

---
 arch/s390/include/asm/kvm_emulate.h | 131 ++++++++++++++++++++++++++++
 arch/s390/include/asm/kvm_mmu.h     |  12 +++
 arch/s390/include/asm/kvm_nested.h  |  13 +++
 arch/s390/kvm/arm64/handle_exit.c   |  50 +++++++++++
 arch/s390/kvm/arm64/inject_fault.c  |  21 +++++
 5 files changed, 227 insertions(+)
 create mode 100644 arch/s390/include/asm/kvm_emulate.h
 create mode 100644 arch/s390/include/asm/kvm_mmu.h
 create mode 100644 arch/s390/include/asm/kvm_nested.h
 create mode 100644 arch/s390/kvm/arm64/handle_exit.c
 create mode 100644 arch/s390/kvm/arm64/inject_fault.c
arch/s390/include/asm/kvm_emulate.h new file +131 -0
new file mode 100644
index 000000000000..93b9e777b51e
@@ -0,0 +1,131 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Emulation functionality for arm64 guests.
+ */
+
+#ifndef __S390_ARM64_KVM_EMULATE_H__
+#define __S390_ARM64_KVM_EMULATE_H__
+
+#include <asm/fault.h>
+#include <asm/ptrace.h>
+#include <linux/kvm_host.h>
+
+#include <kvm/arm64/kvm_arm.h>
+#include <kvm/arm64/kvm_emulate.h>
+
+static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.sae_block.pc;
+}
+
+static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.sae_block.pstate;
+}
+
+static __always_inline unsigned long *vcpu_sp_el0(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.sae_block.sp_el0;
+}
+
+static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static __always_inline u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.esr_elz;
+}
+
+static __always_inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.far_elz;
+}
+
+static __always_inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.teid.addr * PAGE_SIZE;
+}
+
+static inline u16 kvm_vcpu_fault_pic(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.pic & PGM_INT_CODE_MASK;
+}
+
+/* Should be unreachable, arm64 on s390 does not claim KVM_CAP_ARM_NISV_TO_USER*/
+static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized(const struct kvm_vcpu *vcpu)
+{
+	return kvm_vcpu_get_esr(vcpu) & (ESR_ELx_CM | ESR_ELx_WNR);
+}
+
+static __always_inline
+bool kvm_vcpu_trap_is_permission_fault(const struct kvm_vcpu *vcpu)
+{
+	return kvm_vcpu_fault_pic(vcpu) == PGM_PROTECTION;
+}
+
+static __always_inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
+{
+	return true;
+}
+
+static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
+{
+	vcpu->arch.hcr_elz = HCR_E2H | HCR_RW | HCR_AMO | HCR_IMO | HCR_FMO |
+			     HCR_PTW;
+	/* traps */
+	vcpu->arch.hcr_elz |= HCR_TSC | HCR_TID1 | HCR_TID2 | HCR_TID3 |
+			      HCR_TID4 | HCR_TID5 | HCR_TIDCP;
+}
+
+static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu)
+{
+	WARN(true, "not implemented, just feat RAS");
+
+	return 0L;
+}
+
+static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
+{
+	WARN(true, "not implemented, just feat RAS");
+}
+
+static inline bool vcpu_el2_tge_is_set(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline int kvm_vcpu_abt_gltl(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.gltl;
+}
+
+static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline bool is_nested_ctxt(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
+{
+	u32 mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
+
+	return mode != PSR_MODE_EL0t;
+}
+
+#endif /* __S390_ARM64_KVM_EMULATE_H__ */
arch/s390/include/asm/kvm_mmu.h new file +12 -0
new file mode 100644
index 000000000000..ac354fd5bac9
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * KVM MMU for arm64 guests.
+ */
+#ifndef __S390_ARM64_KVM_MMU_H__
+#define __S390_ARM64_KVM_MMU_H__
+
+#include <linux/kvm_host.h>
+
+#include <kvm/arm64/kvm_mmu.h>
+
+#endif /* __S390_ARM64_KVM_MMU_H__ */
arch/s390/include/asm/kvm_nested.h new file +13 -0
new file mode 100644
index 000000000000..7158932e718b
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Nested KVM for arm64 guests. (Not supported by s390)
+ */
+#ifndef ASM_KVM_NESTED_H
+#define ASM_KVM_NESTED_H
+
+static inline bool vcpu_has_nv(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+#endif /* ASM_KVM_NESTED_H */
arch/s390/kvm/arm64/handle_exit.c new file +50 -0

50 lines not on this page · raw message

arch/s390/kvm/arm64/inject_fault.c new file +21 -0
new file mode 100644
index 000000000000..650c041efde2
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <asm/kvm_emulate.h>
+
+/**
+ * kvm_inject_undefined - inject an undefined instruction into the guest
+ * @vcpu: The vCPU in which to inject the exception
+ *
+ * It is assumed that this code is called from the VCPU thread and that the
+ * VCPU therefore is not currently executing guest code.
+ */
+void kvm_inject_undefined(struct kvm_vcpu *vcpu)
+{
+	/* Stub until s390 supports arm64 sysregs TODO sysregs*/
+}
+
+int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr)
+{
+	/* Stub until s390 supports arm64 sysregs TODO sysregs*/
+	return 1;
+}
Signature · 1 line
2.53.0
author reviewer
#21 lore raw

PATCH v3 12/27 KVM: arm64: Share reset general register code

In reply to Steffen Eiden

From: Nina Schoetterl-Glausch <[email protected]>

Move code and required definitions to reset general registers into the shared location. Additionally, add defines to arch/arm64 such that accessing general registers becomes architecture agnostic.

---
 arch/arm64/include/asm/kvm_host.h | 97 +++++--------------------------
 arch/arm64/kvm/reset.c            | 34 +----------
 include/kvm/arm64/kvm_host.h      | 82 ++++++++++++++++++++++++++
 include/kvm/arm64/reset.h         |  8 +++
 virt/kvm/arm64/Makefile.kvm       |  1 +
 virt/kvm/arm64/guest.c            |  8 +--
 virt/kvm/arm64/reset.c            | 43 ++++++++++++++
 7 files changed, 155 insertions(+), 118 deletions(-)
 create mode 100644 include/kvm/arm64/reset.h
 create mode 100644 virt/kvm/arm64/reset.c
arch/arm64/include/asm/kvm_host.h +14 -83
index 6af4f5b39f41..bcc30419e2de 100644
@@ -916,39 +916,6 @@ struct kvm_vcpu_arch {
 	pid_t pid;
 };
 
-/*
- * Each 'flag' is composed of a comma-separated triplet:
- *
- * - the flag-set it belongs to in the vcpu->arch structure
- * - the value for that flag
- * - the mask for that flag
- *
- *  __vcpu_single_flag() builds such a triplet for a single-bit flag.
- * unpack_vcpu_flag() extract the flag value from the triplet for
- * direct use outside of the flag accessors.
- */
-#define __vcpu_single_flag(_set, _f)	_set, (_f), (_f)
-
-#define __unpack_flag(_set, _f, _m)	_f
-#define unpack_vcpu_flag(...)		__unpack_flag(__VA_ARGS__)
-
-#define __build_check_flag(v, flagset, f, m)			\
-	do {							\
-		typeof(v->arch.flagset) *_fset;			\
-								\
-		/* Check that the flags fit in the mask */	\
-		BUILD_BUG_ON(HWEIGHT(m) != HWEIGHT((f) | (m)));	\
-		/* Check that the flags fit in the type */	\
-		BUILD_BUG_ON((sizeof(*_fset) * 8) <= __fls(m));	\
-	} while (0)
-
-#define __vcpu_get_flag(v, flagset, f, m)			\
-	({							\
-		__build_check_flag(v, flagset, f, m);		\
-								\
-		READ_ONCE(v->arch.flagset) & (m);		\
-	})
-
 /*
  * Note that the set/clear accessors must be preempt-safe in order to
  * avoid nesting them with load/put which also manipulate flags...
@@ -962,54 +929,14 @@ struct kvm_vcpu_arch {
 #define __vcpu_flags_preempt_enable()	preempt_enable()
 #endif
 
-#define __vcpu_set_flag(v, flagset, f, m)			\
-	do {							\
-		typeof(v->arch.flagset) *fset;			\
-								\
-		__build_check_flag(v, flagset, f, m);		\
-								\
-		fset = &v->arch.flagset;			\
-		__vcpu_flags_preempt_disable();			\
-		if (HWEIGHT(m) > 1)				\
-			*fset &= ~(m);				\
-		*fset |= (f);					\
-		__vcpu_flags_preempt_enable();			\
-	} while (0)
-
-#define __vcpu_clear_flag(v, flagset, f, m)			\
-	do {							\
-		typeof(v->arch.flagset) *fset;			\
-								\
-		__build_check_flag(v, flagset, f, m);		\
-								\
-		fset = &v->arch.flagset;			\
-		__vcpu_flags_preempt_disable();			\
-		*fset &= ~(m);					\
-		__vcpu_flags_preempt_enable();			\
-	} while (0)
-
-#define __vcpu_test_and_clear_flag(v, flagset, f, m)		\
-	({							\
-		typeof(v->arch.flagset) set;			\
-								\
-		set = __vcpu_get_flag(v, flagset, f, m);	\
-		__vcpu_clear_flag(v, flagset, f, m);		\
-								\
-		set;						\
-	})
-
-#define vcpu_get_flag(v, ...)	__vcpu_get_flag((v), __VA_ARGS__)
-#define vcpu_set_flag(v, ...)	__vcpu_set_flag((v), __VA_ARGS__)
-#define vcpu_clear_flag(v, ...)	__vcpu_clear_flag((v), __VA_ARGS__)
-#define vcpu_test_and_clear_flag(v, ...)			\
-	__vcpu_test_and_clear_flag((v), __VA_ARGS__)
-
-/* KVM_ARM_VCPU_INIT completed */
-#define VCPU_INITIALIZED	__vcpu_single_flag(cflags, BIT(0))
-/* SVE config completed */
-#define VCPU_SVE_FINALIZED	__vcpu_single_flag(cflags, BIT(1))
-/* pKVM VCPU setup completed */
-#define VCPU_PKVM_FINALIZED	__vcpu_single_flag(cflags, BIT(2))
+#define _vcpu_get_flag(v, flagset, ...)	\
+	__vcpu_get_flag(&(v)->arch.flagset, __VA_ARGS__)
+#define _vcpu_set_flag(v, flagset, ...)	\
+	__vcpu_set_flag(&(v)->arch.flagset, __VA_ARGS__)
+#define _vcpu_clear_flag(v, flagset, ...)	\
+	__vcpu_clear_flag(&(v)->arch.flagset, __VA_ARGS__)
+#define _vcpu_test_and_clear_flag(v, flagset, ...)	\
+	__vcpu_test_and_clear_flag(&(v)->arch.flagset, __VA_ARGS__)
 
 /* Physical CPU not in supported_cpus */
 #define ON_UNSUPPORTED_CPU	__vcpu_single_flag(sflags, BIT(0))
@@ -1120,6 +1047,12 @@ static inline u64 *___ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r)
 
 #define ctxt_sys_reg(c,r)	(*__ctxt_sys_reg(c,r))
 
+#define kvm_vcpu_get_sp_el1(__vcpu) (__ctxt_sys_reg(&(__vcpu)->arch.ctxt, SP_EL1))
+#define kvm_vcpu_get_vreg(__vcpu, _n) (&(__vcpu)->arch.ctxt.fp_regs.vregs[_n])
+#define kvm_vcpu_get_vregs(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.vregs)
+#define kvm_vcpu_get_fpsr(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.fpsr)
+#define kvm_vcpu_get_fpcr(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.fpcr)
+
 u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *, enum vcpu_sysreg, u64);
 
 #define __vcpu_assign_sys_reg(v, r, val)				\
@@ -1452,8 +1385,6 @@ static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
 #define kvm_vcpu_has_feature(k, f)	__vcpu_has_feature(&(k)->arch, (f))
 #define vcpu_has_feature(v, f)	__vcpu_has_feature(&(v)->kvm->arch, (f))
 
-#define kvm_vcpu_initialized(v) vcpu_get_flag(v, VCPU_INITIALIZED)
-
 int kvm_trng_call(struct kvm_vcpu *vcpu);
 #ifdef CONFIG_KVM
 extern phys_addr_t hyp_mem_base;
arch/arm64/kvm/reset.c +3 -31
index e952dec01d74..b4f579df0beb 100644
@@ -30,22 +30,11 @@
 #include <asm/kvm_nested.h>
 #include <asm/virt.h>
 
+#include <kvm/arm64/reset.h>
+
 /* Maximum phys_shift supported for any VM on this host */
 static u32 __ro_after_init kvm_ipa_limit;
 unsigned int __ro_after_init kvm_host_sve_max_vl;
-
-/*
- * ARMv8 Reset Values
- */
-#define VCPU_RESET_PSTATE_EL1	(PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT | \
-				 PSR_F_BIT | PSR_D_BIT)
-
-#define VCPU_RESET_PSTATE_EL2	(PSR_MODE_EL2h | PSR_A_BIT | PSR_I_BIT | \
-				 PSR_F_BIT | PSR_D_BIT)
-
-#define VCPU_RESET_PSTATE_SVC	(PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \
-				 PSR_AA32_I_BIT | PSR_AA32_F_BIT)
-
 unsigned int __ro_after_init kvm_sve_max_vl;
 
 int __init kvm_arm_init_sve(void)
@@ -191,7 +180,6 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_reset_state reset_state;
 	bool loaded;
-	u32 pstate;
 
 	spin_lock(&vcpu->arch.mp_state_lock);
 	reset_state = vcpu->arch.reset_state;
@@ -210,23 +198,7 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 		kvm_vcpu_reset_sve(vcpu);
 	}
 
-	if (vcpu_el1_is_32bit(vcpu))
-		pstate = VCPU_RESET_PSTATE_SVC;
-	else if (vcpu_has_nv(vcpu))
-		pstate = VCPU_RESET_PSTATE_EL2;
-	else
-		pstate = VCPU_RESET_PSTATE_EL1;
-
-	/* Reset core registers */
-	memset(vcpu_gp_regs(vcpu), 0, sizeof(vcpu_gp_regs(vcpu)));
-	*vcpu_pc(vcpu) = 0;
-	*vcpu_sp_el0(vcpu) = 0;
-	memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
-	vcpu->arch.ctxt.spsr_abt = 0;
-	vcpu->arch.ctxt.spsr_und = 0;
-	vcpu->arch.ctxt.spsr_irq = 0;
-	vcpu->arch.ctxt.spsr_fiq = 0;
-	*vcpu_cpsr(vcpu) = pstate;
+	kvm_reset_vcpu_core_regs(vcpu);
 
 	/* Reset system registers */
 	kvm_reset_sys_regs(vcpu);
include/kvm/arm64/kvm_host.h +82 -0

94 lines not on this page · raw message

include/kvm/arm64/reset.h new file +8 -0
new file mode 100644
index 000000000000..a0bca4769b13
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __KVM_ARM64_RESET_H__
+#define __KVM_ARM64_RESET_H__
+
+void kvm_reset_vcpu_core_regs(struct kvm_vcpu *vcpu);
+
+#endif /* __KVM_ARM64_RESET_H__ */
virt/kvm/arm64/Makefile.kvm +1 -0
index ac969bf1c016..c5e1db570a09 100644
@@ -9,4 +9,5 @@ shared-arm64-obj := \
 	$(KVM_ARM64)/guest.o \
 	$(KVM_ARM64)/handle_exit.o \
 	$(KVM_ARM64)/mmio.o \
+	$(KVM_ARM64)/reset.o \
 
virt/kvm/arm64/guest.c +4 -4
index e283a4456df8..35ba03033b4c 100644
@@ -93,7 +93,7 @@ static void *core_reg_addr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 		return vcpu_cpsr(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(sp_el1):
-		return __ctxt_sys_reg(&vcpu->arch.ctxt, SP_EL1);
+		return kvm_vcpu_get_sp_el1(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(elr_el1):
 		return __ctxt_sys_reg(&vcpu->arch.ctxt, ELR_EL1);
@@ -117,13 +117,13 @@ static void *core_reg_addr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	     KVM_REG_ARM_CORE_REG(fp_regs.vregs[31]):
 		off -= KVM_REG_ARM_CORE_REG(fp_regs.vregs[0]);
 		off /= 4;
-		return &vcpu->arch.ctxt.fp_regs.vregs[off];
+		return kvm_vcpu_get_vreg(vcpu, off);
 
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpsr):
-		return &vcpu->arch.ctxt.fp_regs.fpsr;
+		return  kvm_vcpu_get_fpsr(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpcr):
-		return &vcpu->arch.ctxt.fp_regs.fpcr;
+		return  kvm_vcpu_get_fpcr(vcpu);
 
 	default:
 		return NULL;
virt/kvm/arm64/reset.c new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#25 lore raw

PATCH v3 15/27 KVM: S390: Refactor gmap

In reply to Steffen Eiden

Refactor gmap code such that a second s390 (host) KVM implementation can use the gmap code as well. Move relevant definitions into the shared kvm_host.h. Move mmu code and traces from s390 to gmap.

---
 arch/s390/include/asm/kvm_host.h      |   9 ++
 arch/s390/include/asm/kvm_host_s390.h |  11 +-
 arch/s390/kvm/gmap/Makefile           |   2 +-
 arch/s390/kvm/gmap/faultin.c          |  11 +-
 arch/s390/kvm/gmap/gmap.c             |  11 +-
 arch/s390/kvm/gmap/gmap.h             |  11 ++
 arch/s390/kvm/gmap/mmu.c              | 147 ++++++++++++++++++++++++++
 arch/s390/kvm/gmap/trace-gmap.h       |  59 +++++++++++
 arch/s390/kvm/s390/s390.c             | 108 +------------------
 arch/s390/kvm/s390/s390.h             |  16 +++
 arch/s390/kvm/s390/trace.h            |  14 ---
 11 files changed, 265 insertions(+), 134 deletions(-)
 create mode 100644 arch/s390/kvm/gmap/mmu.c
 create mode 100644 arch/s390/kvm/gmap/trace-gmap.h
arch/s390/include/asm/kvm_host.h +9 -0
index 6ff643ac0d15..1c20168a3ef5 100644
@@ -5,4 +5,13 @@
 
 #include <asm/kvm_host_s390.h>
 
+#define PGM_PROTECTION			0x04
+#define PGM_ADDRESSING			0x05
+#define PGM_SEGMENT_TRANSLATION		0x10
+#define PGM_PAGE_TRANSLATION		0x11
+#define PGM_ASCE_TYPE			0x38
+#define PGM_REGION_FIRST_TRANS		0x39
+#define PGM_REGION_SECOND_TRANS		0x3a
+#define PGM_REGION_THIRD_TRANS		0x3b
+
 #endif
arch/s390/include/asm/kvm_host_s390.h +3 -8
index 8fe21e94f33c..d4bf6d0df49f 100644
@@ -153,8 +153,7 @@ struct kvm_vcpu_stat {
 #define PGM_OPERATION			0x01
 #define PGM_PRIVILEGED_OP		0x02
 #define PGM_EXECUTE			0x03
-#define PGM_PROTECTION			0x04
-#define PGM_ADDRESSING			0x05
+/* 0x04 & 0x05 defined in kvm_host.h */
 #define PGM_SPECIFICATION		0x06
 #define PGM_DATA			0x07
 #define PGM_FIXED_POINT_OVERFLOW	0x08
@@ -165,8 +164,7 @@ struct kvm_vcpu_stat {
 #define PGM_HFP_EXPONENT_UNDERFLOW	0x0d
 #define PGM_HFP_SIGNIFICANCE		0x0e
 #define PGM_HFP_DIVIDE			0x0f
-#define PGM_SEGMENT_TRANSLATION		0x10
-#define PGM_PAGE_TRANSLATION		0x11
+/* 0x10 & 0x11 defined in kvm_host.h */
 #define PGM_TRANSLATION_SPEC		0x12
 #define PGM_SPECIAL_OPERATION		0x13
 #define PGM_OPERAND			0x15
@@ -196,10 +194,7 @@ struct kvm_vcpu_stat {
 #define PGM_STACK_SPECIFICATION		0x32
 #define PGM_STACK_TYPE			0x33
 #define PGM_STACK_OPERATION		0x34
-#define PGM_ASCE_TYPE			0x38
-#define PGM_REGION_FIRST_TRANS		0x39
-#define PGM_REGION_SECOND_TRANS		0x3a
-#define PGM_REGION_THIRD_TRANS		0x3b
+/* 0x38 - 0x3b defined in kvm_host.h */
 #define PGM_SECURE_STORAGE_ACCESS	0x3d
 #define PGM_NON_SECURE_STORAGE_ACCESS	0x3e
 #define PGM_SECURE_STORAGE_VIOLATION	0x3f
arch/s390/kvm/gmap/Makefile +1 -1
index 21967ed88877..140914c5c14f 100644
@@ -2,4 +2,4 @@
 
 GMAP ?= ../gmap
 
-kvm-y += $(GMAP)/dat.o $(GMAP)/gmap.o $(GMAP)/faultin.o
+kvm-y += $(GMAP)/dat.o $(GMAP)/gmap.o $(GMAP)/faultin.o $(GMAP)/mmu.o
arch/s390/kvm/gmap/faultin.c +8 -3
index ddf0ca71f374..e6c2bdde3038 100644
@@ -9,10 +9,15 @@
 #include <linux/kvm_host.h>
 
 #include "gmap.h"
-#include "trace.h"
 #include "faultin.h"
-
-bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu);
+#ifdef KVM_S390_ARM64
+#include "arm.h"
+#else
+#include "s390.h"
+#endif
+
+#define CREATE_TRACE_POINTS
+#include "trace-gmap.h"
 
 /*
  * kvm_s390_faultin_gfn() - handle a dat fault.
arch/s390/kvm/gmap/gmap.c +6 -5
index 89d84086a75d..6dbe99c4d2c8 100644
@@ -21,14 +21,13 @@
 
 #include "dat.h"
 #include "gmap.h"
+#ifdef KVM_S390_ARM64
+#include "arm.h"
+#else
 #include "s390.h"
+#endif
 #include "faultin.h"
 
-static inline bool kvm_s390_is_in_sie(struct kvm_vcpu *vcpu)
-{
-	return vcpu->arch.sie_block->prog0c & PROG_IN_SIE;
-}
-
 static int gmap_limit_to_type(gfn_t limit)
 {
 	if (!limit)
@@ -253,6 +252,7 @@ int s390_replace_asce(struct gmap *gmap)
 
 bool _gmap_unmap_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end, bool hint)
 {
+#ifndef KVM_S390_ARM64
 	struct kvm *kvm = gmap->kvm;
 	struct kvm_vcpu *vcpu;
 	gfn_t prefix_gfn;
@@ -271,6 +271,7 @@ bool _gmap_unmap_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end, bool hint)
 			kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
 		}
 	}
+#endif /* ifdef KVM_S390_ARM64 */
 	return true;
 }
 
arch/s390/kvm/gmap/gmap.h +11 -0
index 67e42e16c337..51ae54469f69 100644
@@ -307,4 +307,15 @@ static inline bool gmap_is_shadow_valid(struct gmap *sg, union asce asce, int ed
 	return sg->guest_asce.val == asce.val && sg->edat_level == edat_level;
 }
 
+int gmap_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log);
+int gmap_prepare_memory_region(struct kvm *kvm,
+			       const struct kvm_memory_slot *old,
+			       struct kvm_memory_slot *new,
+			       enum kvm_mr_change change);
+void gmap_commit_memory_region(struct kvm *kvm,
+			       struct kvm_memory_slot *old,
+			       const struct kvm_memory_slot *new,
+			       enum kvm_mr_change change);
+bool gmap_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
+
 #endif /* ARCH_KVM_GMAP_GMAP_H */
arch/s390/kvm/gmap/mmu.c new file +147 -0

147 lines not on this page · raw message

arch/s390/kvm/gmap/trace-gmap.h new file +59 -0

59 lines not on this page · raw message

arch/s390/kvm/s390/s390.c +5 -103

138 lines not on this page · raw message

arch/s390/kvm/s390/s390.h +16 -0
index 41c92a358b17..e82e58ae83de 100644
@@ -32,6 +32,11 @@ union kvm_s390_quad {
 	unsigned char one;
 };
 
+static inline bool kvm_s390_is_in_sie(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sie_block->prog0c & PROG_IN_SIE;
+}
+
 static inline void kvm_s390_fpu_store(struct kvm_run *run)
 {
 	fpu_stfpc(&run->s.regs.fpc);
@@ -591,6 +596,11 @@ static inline bool kvm_s390_cur_gmap_fault_is_write(void)
 	return test_facility(75) && (current->thread.gmap_teid.fsi == TEID_FSI_STORE);
 }
 
+static __always_inline int kvm_s390_is_migration_mode(struct kvm *kvm)
+{
+	return kvm->arch.migration_mode;
+}
+
 /**
  * kvm_s390_vcpu_crypto_reset_all
  *
@@ -621,4 +631,10 @@ void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm);
  */
 extern unsigned int diag9c_forwarding_hz;
 
+/*
+ * Must be called with kvm->slots_lock to avoid races with ourselves and
+ * kvm_s390_vm_start_migration.
+ */
+int kvm_s390_vm_stop_migration(struct kvm *kvm);
+
 #endif
arch/s390/kvm/s390/trace.h +0 -14
index aa419eb6a0c8..97774fe09a85 100644
@@ -45,20 +45,6 @@ TRACE_EVENT(kvm_s390_skey_related_inst,
 	    VCPU_TP_PRINTK("%s", "storage key related instruction")
 	);
 
-TRACE_EVENT(kvm_s390_major_guest_pfault,
-	    TP_PROTO(VCPU_PROTO_COMMON),
-	    TP_ARGS(VCPU_ARGS_COMMON),
-
-	    TP_STRUCT__entry(
-		    VCPU_FIELD_COMMON
-		    ),
-
-	    TP_fast_assign(
-		    VCPU_ASSIGN_COMMON
-		    ),
-	    VCPU_TP_PRINTK("%s", "major fault, maybe applicable for pfault")
-	);
-
 TRACE_EVENT(kvm_s390_pfault_init,
 	    TP_PROTO(VCPU_PROTO_COMMON, long pfault_token),
 	    TP_ARGS(VCPU_ARGS_COMMON, pfault_token),
Signature · 1 line
2.53.0
author reviewer
#18 lore raw

PATCH v3 10/27 KVM: arm64: Make some arm64 KVM code shareable

In reply to Steffen Eiden

Support sharing architecture-agnostic KVM-arm64 code between different architectures. Introduce a new directory virt/kvm/arm64 which holds arm64-KVM code that can be used by multiple architectures implementing the arm64-KVM API.

Additionally, to improve portability slightly modify core_reg_addr to use functions instead of direct field accesses.

---
 MAINTAINERS                               |   1 +
 arch/arm64/kvm/Makefile                   |   5 +-
 arch/arm64/kvm/arm.c                      |  48 ----
 arch/arm64/kvm/guest.c                    | 292 ---------------------
 arch/arm64/kvm/handle_exit.c              |  52 +---
 arch/arm64/kvm/trace_arm.h                |  25 --
 include/kvm/arm64/guest.h                 |   5 +-
 include/kvm/arm64/handle_exit.h           |  14 +
 include/kvm/arm64/kvm_host.h              |   9 +
 virt/kvm/arm64/Makefile.kvm               |  12 +
 virt/kvm/arm64/arm.c                      |  54 ++++
 virt/kvm/arm64/guest.c                    | 302 ++++++++++++++++++++++
 virt/kvm/arm64/handle_exit.c              |  54 ++++
 {arch/arm64/kvm => virt/kvm/arm64}/mmio.c |   1 +
 virt/kvm/arm64/trace.h                    |  42 +++
 15 files changed, 500 insertions(+), 416 deletions(-)
 create mode 100644 include/kvm/arm64/handle_exit.h
 create mode 100644 virt/kvm/arm64/Makefile.kvm
 create mode 100644 virt/kvm/arm64/arm.c
 create mode 100644 virt/kvm/arm64/guest.c
 create mode 100644 virt/kvm/arm64/handle_exit.c
 rename {arch/arm64/kvm => virt/kvm/arm64}/mmio.c (99%)
 create mode 100644 virt/kvm/arm64/trace.h
MAINTAINERS +1 -0
index 311fcfde9b11..3a07a707ec28 100644
@@ -14080,6 +14080,7 @@ F:	arch/arm64/include/uapi/asm/kvm*
 F:	include/kvm/arm64/
 F:	include/uapi/arch/arm64/asm/kvm*
 F:	arch/arm64/kvm/
+F:	virt/kvm/arm64/
 F:	include/kvm/arm_*
 F:	tools/testing/selftests/kvm/*/arm64/
 F:	tools/testing/selftests/kvm/arm64/
arch/arm64/kvm/Makefile +4 -1
index 59612d2f277c..5b4a8d002fc9 100644
@@ -6,6 +6,7 @@
 ccflags-y += -I $(src)
 
 include $(srctree)/virt/kvm/Makefile.kvm
+include $(srctree)/virt/kvm/arm64/Makefile.kvm
 
 obj-$(CONFIG_KVM) += kvm.o
 obj-$(CONFIG_KVM) += hyp/
@@ -13,7 +14,7 @@ obj-$(CONFIG_KVM) += hyp/
 CFLAGS_sys_regs.o += -Wno-override-init
 CFLAGS_handle_exit.o += -Wno-override-init
 
-kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
+kvm-y += arm.o mmu.o psci.o hypercalls.o pvtime.o \
 	 inject_fault.o va_layout.o handle_exit.o config.o \
 	 guest.o debug.o reset.o sys_regs.o stacktrace.o \
 	 vgic-sys-reg-v3.o fpsimd.o pkvm.o \
@@ -26,6 +27,8 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
 	 vgic/vgic-v5.o
 
+kvm-y += $(shared-arm64-obj)
+
 kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
 kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
 kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o
arch/arm64/kvm/arm.c +0 -48

54 lines not on this page · raw message

arch/arm64/kvm/guest.c +0 -292

304 lines not on this page · raw message

arch/arm64/kvm/handle_exit.c +3 -49

79 lines not on this page · raw message

arch/arm64/kvm/trace_arm.h +0 -25
index 9c60f6465c78..40c656937136 100644
@@ -136,31 +136,6 @@ TRACE_EVENT(kvm_mmio_emulate,
 		  __entry->vcpu_pc, __entry->instr, __entry->cpsr)
 );
 
-TRACE_EVENT(kvm_mmio_nisv,
-	TP_PROTO(unsigned long vcpu_pc, unsigned long esr,
-		 unsigned long far, unsigned long ipa),
-	TP_ARGS(vcpu_pc, esr, far, ipa),
-
-	TP_STRUCT__entry(
-		__field(	unsigned long,	vcpu_pc		)
-		__field(	unsigned long,	esr		)
-		__field(	unsigned long,	far		)
-		__field(	unsigned long,	ipa		)
-	),
-
-	TP_fast_assign(
-		__entry->vcpu_pc		= vcpu_pc;
-		__entry->esr			= esr;
-		__entry->far			= far;
-		__entry->ipa			= ipa;
-	),
-
-	TP_printk("ipa %#016lx, esr %#016lx, far %#016lx, pc %#016lx",
-		  __entry->ipa, __entry->esr,
-		  __entry->far, __entry->vcpu_pc)
-);
-
-
 TRACE_EVENT(kvm_set_way_flush,
 	    TP_PROTO(unsigned long vcpu_pc, bool cache),
 	    TP_ARGS(vcpu_pc, cache),
include/kvm/arm64/guest.h +4 -1
index fa67d992e8fd..ac042ed71157 100644
@@ -4,7 +4,10 @@
 #define __KVM_ARM64_GUEST_H__
 
 /* Implemented by virt/kvm/arm64/guest.c */
-unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
+int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
+int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
+int copy_core_reg_indices(const struct kvm_vcpu *vcpu, u64 __user *uindices);
+unsigned long num_core_regs(const struct kvm_vcpu *vcpu);
 
 #endif /* __KVM_ARM64_GUEST_H__ */
include/kvm/arm64/handle_exit.h new file +14 -0
new file mode 100644
index 000000000000..9f3f41eaf3ac
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef KVM_ARM64_HANDLE_EXIT_H
+#define KVM_ARM64_HANDLE_EXIT_H
+
+#include <linux/kvm_host.h>
+
+typedef int (*exit_handle_fn)(struct kvm_vcpu *);
+extern exit_handle_fn arm_exit_handlers[ESR_ELx_EC_MAX + 1];
+
+int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu);
+exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu);
+int handle_trap_exceptions(struct kvm_vcpu *vcpu);
+
+#endif /* KVM_ARM64_HANDLE_EXIT_H */
include/kvm/arm64/kvm_host.h +9 -0
index ef4f4d44ac39..d1eccbe44c50 100644
@@ -28,6 +28,12 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+/* Implemented in virt/kvm/arm64/arm.c */
+int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
+				 const struct kvm_vcpu_init *init);
+bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
+			   const struct kvm_vcpu_init *init);
+
 /* MMIO helpers */
 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data);
 unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len);
@@ -102,4 +108,7 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa);
 /* Unhandled SEAs are taken to userspace */
 #define KVM_ARCH_FLAG_EXIT_SEA				11
 
+/* Implemented in architecture specific code */
+unsigned long system_supported_vcpu_features(void);
+
 #endif /* __KVM_ARM64_KVM_HOST_H */
virt/kvm/arm64/Makefile.kvm new file +12 -0
new file mode 100644
index 000000000000..ac969bf1c016
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for Kernel-based Virtual Machine modules for arm64 guests
+#
+KVM_ARM64 ?= $(KVM)/arm64
+
+shared-arm64-obj := \
+	$(KVM_ARM64)/arm.o \
+	$(KVM_ARM64)/guest.o \
+	$(KVM_ARM64)/handle_exit.o \
+	$(KVM_ARM64)/mmio.o \
+
virt/kvm/arm64/arm.c new file +54 -0

54 lines not on this page · raw message

virt/kvm/arm64/guest.c new file +302 -0

302 lines not on this page · raw message

virt/kvm/arm64/handle_exit.c new file +54 -0

54 lines not on this page · raw message

virt/kvm/arm64/mmio.c renamed from arch/arm64/kvm/mmio.c +1 -0
similarity index 99%
rename from arch/arm64/kvm/mmio.c
rename to virt/kvm/arm64/mmio.c
index e2285ed8c91d..438a554ec1ed 100644
@@ -8,6 +8,7 @@
 #include <asm/kvm_emulate.h>
 #include <trace/events/kvm.h>
 
+#define CREATE_TRACE_POINTS
 #include "trace.h"
 
 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data)
virt/kvm/arm64/trace.h new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#13 lore raw

PATCH v3 7/27 KVM: arm64: Provide arm64 KVM API for non-native architectures

In reply to Steffen Eiden

The KVM‑related headers are moved to include/kvm/arm64/, decoupling them from the arm64 architecture directory. The design convention is that architecture‑specific headers under <arch>/include/asm/ include from this shared location, allowing non‑native hosts to consume the arm64 KVM infrastructure without duplicating code.

This refactoring enables non-native hosts to include and utilize arm64 KVM infrastructure without duplicating code or creating architecture specific dependencies.

---
 MAINTAINERS                                   |   1 +
 arch/arm64/include/asm/el2_setup.h            |   2 +-
 arch/arm64/include/asm/hardirq.h              |   2 +-
 arch/arm64/include/asm/kvm_emulate.h          |   3 +-
 arch/arm64/include/asm/kvm_host.h             |  99 +----------------
 arch/arm64/include/asm/kvm_mmu.h              |  41 +------
 arch/arm64/kernel/head.S                      |   2 +-
 arch/arm64/kernel/hyp-stub.S                  |   2 +-
 arch/arm64/kvm/arm.c                          |   6 +-
 arch/arm64/kvm/debug.c                        |   2 +-
 arch/arm64/kvm/guest.c                        |   2 +
 arch/arm64/kvm/hyp/entry.S                    |   2 +-
 arch/arm64/kvm/hyp/hyp-entry.S                |   2 +-
 arch/arm64/kvm/hyp/nvhe/host.S                |   2 +-
 arch/arm64/kvm/hyp/nvhe/hyp-init.S            |   2 +-
 arch/arm64/kvm/mmu.c                          |   2 +-
 arch/arm64/kvm/nested.c                       |   2 +-
 arch/arm64/kvm/reset.c                        |   2 +-
 arch/arm64/kvm/sys_regs.c                     |   2 +-
 arch/arm64/kvm/vgic/vgic-its.c                |   2 +-
 arch/arm64/kvm/vgic/vgic-mmio-v3.c            |   2 +-
 arch/arm64/kvm/vgic/vgic-v3-nested.c          |   2 +-
 include/kvm/arm64/guest.h                     |  10 ++
 .../asm => include/kvm/arm64}/kvm_arm.h       |   5 +-
 include/kvm/arm64/kvm_host.h                  | 105 ++++++++++++++++++
 include/kvm/arm64/kvm_mmu.h                   |  47 ++++++++
 26 files changed, 195 insertions(+), 156 deletions(-)
 create mode 100644 include/kvm/arm64/guest.h
 rename {arch/arm64/include/asm => include/kvm/arm64}/kvm_arm.h (99%)
 create mode 100644 include/kvm/arm64/kvm_host.h
 create mode 100644 include/kvm/arm64/kvm_mmu.h
MAINTAINERS +1 -0
index f1903e4f0dcf..311fcfde9b11 100644
@@ -14077,6 +14077,7 @@ F:	Documentation/virt/kvm/arm/
 F:	Documentation/virt/kvm/devices/arm*
 F:	arch/arm64/include/asm/kvm*
 F:	arch/arm64/include/uapi/asm/kvm*
+F:	include/kvm/arm64/
 F:	include/uapi/arch/arm64/asm/kvm*
 F:	arch/arm64/kvm/
 F:	include/kvm/arm_*
arch/arm64/include/asm/el2_setup.h +1 -1
index 587507a9980e..33ef4f196380 100644
@@ -11,7 +11,7 @@
 #error Assembly-only header
 #endif
 
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/ptrace.h>
 #include <asm/sysreg.h>
 #include <linux/irqchip/arm-gic-v3.h>
arch/arm64/include/asm/hardirq.h +1 -1
index 77d6b8c63d4e..0eceb8ab6abb 100644
@@ -10,7 +10,7 @@
 #include <linux/threads.h>
 #include <asm/barrier.h>
 #include <asm/irq.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/sysreg.h>
 
 #define ack_bad_irq ack_bad_irq
arch/arm64/include/asm/kvm_emulate.h +2 -1
index 5bf3d7e1d92c..822f6077b107 100644
@@ -16,13 +16,14 @@
 
 #include <asm/debug-monitors.h>
 #include <asm/esr.h>
-#include <asm/kvm_arm.h>
 #include <asm/kvm_hyp.h>
 #include <asm/kvm_nested.h>
 #include <asm/ptrace.h>
 #include <asm/cputype.h>
 #include <asm/virt.h>
 
+#include <kvm/arm64/kvm_arm.h>
+
 #define CURRENT_EL_SP_EL0_VECTOR	0x0
 #define CURRENT_EL_SP_ELx_VECTOR	0x200
 #define LOWER_EL_AArch64_VECTOR		0x400
arch/arm64/include/asm/kvm_host.h +3 -96

145 lines not on this page · raw message

arch/arm64/include/asm/kvm_mmu.h +2 -39

71 lines not on this page · raw message

arch/arm64/kernel/head.S +1 -1
index 87a822e5c4ca..853952be8021 100644
@@ -25,7 +25,7 @@
 #include <asm/elf.h>
 #include <asm/image.h>
 #include <asm/kernel-pgtable.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/memory.h>
 #include <asm/pgtable-hwdef.h>
 #include <asm/page.h>
arch/arm64/kernel/hyp-stub.S +1 -1
index 634ddc904244..607ac05fcb7b 100644
@@ -11,7 +11,7 @@
 
 #include <asm/assembler.h>
 #include <asm/el2_setup.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/ptrace.h>
 #include <asm/virt.h>
arch/arm64/kvm/arm.c +4 -2
index 9453321ef8c6..04a4ef1c7f06 100644
@@ -34,7 +34,7 @@
 #include <asm/cacheflush.h>
 #include <asm/cpufeature.h>
 #include <asm/virt.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
@@ -52,6 +52,8 @@
 
 #include <linux/irqchip/arm-gic-v5.h>
 
+#include <kvm/arm64/guest.h>
+
 #include "sys_regs.h"
 
 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
@@ -1541,7 +1543,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 	return -EINVAL;
 }
 
-static unsigned long system_supported_vcpu_features(void)
+unsigned long system_supported_vcpu_features(void)
 {
 	unsigned long features = KVM_VCPU_VALID_FEATURES;
 
arch/arm64/kvm/debug.c +1 -1
index f4d7b12045e8..4ba906568c38 100644
@@ -13,7 +13,7 @@
 #include <asm/arm_pmuv3.h>
 #include <asm/debug-monitors.h>
 #include <asm/kvm_asm.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_emulate.h>
 
 static int cpu_has_spe(u64 dfr0)
arch/arm64/kvm/guest.c +2 -0
index 332c453b87cf..557c380ffa37 100644
@@ -27,6 +27,8 @@
 #include <asm/kvm_nested.h>
 #include <asm/sigcontext.h>
 
+#include <kvm/arm64/guest.h>
+
 #include "trace.h"
 
 const struct kvm_stats_desc kvm_vm_stats_desc[] = {
arch/arm64/kvm/hyp/entry.S +1 -1
index 11a10d8f5beb..b1694f738208 100644
@@ -10,7 +10,7 @@
 #include <asm/assembler.h>
 #include <asm/fpsimdmacros.h>
 #include <asm/kvm.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_mmu.h>
 #include <asm/kvm_mte.h>
arch/arm64/kvm/hyp/hyp-entry.S +1 -1
index 03f97d71984c..dfec4e6e5d62 100644
@@ -10,7 +10,7 @@
 #include <asm/alternative.h>
 #include <asm/assembler.h>
 #include <asm/cpufeature.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/mmu.h>
 #include <asm/spectre.h>
arch/arm64/kvm/hyp/nvhe/host.S +1 -1
index f337770ec459..aef2b2540bc0 100644
@@ -7,7 +7,7 @@
 #include <linux/linkage.h>
 
 #include <asm/assembler.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_mmu.h>
 #include <asm/kvm_ptrauth.h>
arch/arm64/kvm/hyp/nvhe/hyp-init.S +1 -1
index 89cb553be1e5..e9f603c3de37 100644
@@ -11,7 +11,7 @@
 #include <asm/alternative.h>
 #include <asm/assembler.h>
 #include <asm/el2_setup.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_mmu.h>
 #include <asm/pgtable-hwdef.h>
arch/arm64/kvm/mmu.c +1 -1
index 4da9281312eb..f683eb18c4ec 100644
@@ -14,7 +14,7 @@
 #include <asm/acpi.h>
 #include <asm/pgalloc.h>
 #include <asm/cacheflush.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_mmu.h>
 #include <asm/kvm_pgtable.h>
 #include <asm/kvm_pkvm.h>
arch/arm64/kvm/nested.c +1 -1
index 883b6c1008fb..5321911f19e0 100644
@@ -9,7 +9,7 @@
 #include <linux/kvm_host.h>
 
 #include <asm/fixmap.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_mmu.h>
 #include <asm/kvm_nested.h>
arch/arm64/kvm/reset.c +1 -1
index b963fd975aac..036bf2dff976 100644
@@ -23,7 +23,7 @@
 #include <asm/cputype.h>
 #include <asm/fpsimd.h>
 #include <asm/ptrace.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_mmu.h>
arch/arm64/kvm/sys_regs.c +1 -1
index 148fc3400ea8..2434bcc2d50d 100644
@@ -24,7 +24,7 @@
 #include <asm/cputype.h>
 #include <asm/debug-monitors.h>
 #include <asm/esr.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
 #include <asm/kvm_mmu.h>
arch/arm64/kvm/vgic/vgic-its.c +1 -1
index 1d7e5d560af4..d20575e5da44 100644
@@ -17,7 +17,7 @@
 #include <linux/irqchip/arm-gic-v3.h>
 
 #include <asm/kvm_emulate.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_mmu.h>
 
 #include "vgic.h"
arch/arm64/kvm/vgic/vgic-mmio-v3.c +1 -1
index 5913a20d8301..daeb0ffa0471 100644
@@ -12,7 +12,7 @@
 #include <kvm/arm_vgic.h>
 
 #include <asm/kvm_emulate.h>
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_mmu.h>
 
 #include "vgic.h"
arch/arm64/kvm/vgic/vgic-v3-nested.c +1 -1
index 5c69fa615823..a2070a637f51 100644
@@ -9,7 +9,7 @@
 
 #include <kvm/arm_vgic.h>
 
-#include <asm/kvm_arm.h>
+#include <kvm/arm64/kvm_arm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_nested.h>
 
include/kvm/arm64/guest.h new file +10 -0
new file mode 100644
index 000000000000..fa67d992e8fd
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __KVM_ARM64_GUEST_H__
+#define __KVM_ARM64_GUEST_H__
+
+/* Implemented by virt/kvm/arm64/guest.c */
+unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
+int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
+
+#endif /* __KVM_ARM64_GUEST_H__ */
include/kvm/arm64/kvm_arm.h renamed from arch/arm64/include/asm/kvm_arm.h +3 -2
similarity index 99%
rename from arch/arm64/include/asm/kvm_arm.h
rename to include/kvm/arm64/kvm_arm.h
index 3f9233b5a130..b9c45e4dad72 100644
@@ -8,8 +8,9 @@
 #define __ARM64_KVM_ARM_H__
 
 #include <asm/esr.h>
-#include <asm/memory.h>
-#include <asm/sysreg.h>
+#include <linux/const.h>
+#include <linux/bits.h>
+#include <asm/sysreg-defs.h>
 #include <asm/types.h>
 
 /*
include/kvm/arm64/kvm_host.h new file +105 -0

105 lines not on this page · raw message

include/kvm/arm64/kvm_mmu.h new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#33 lore raw

PATCH v3 20/27 KVM: s390: arm64: Introduce host definitions

In reply to Steffen Eiden

Add all basic definitions the arm on s390 KVM host requires. Including, but not limited to:
- struct kvm*arch definitions
- various functions (to be implemented in the following patches)
- various defines required to run arm64 guests

---
 arch/s390/include/asm/kvm.h            |   6 +
 arch/s390/include/asm/kvm_host.h       |   9 ++
 arch/s390/include/asm/kvm_host_arm64.h | 212 +++++++++++++++++++++++++
 3 files changed, 227 insertions(+)
 create mode 100644 arch/s390/include/asm/kvm.h
 create mode 100644 arch/s390/include/asm/kvm_host_arm64.h
arch/s390/include/asm/kvm.h new file +6 -0
new file mode 100644
index 000000000000..d9e727d2378c
@@ -0,0 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifdef KVM_S390_ARM64
+#include <uapi/arch/arm64/asm/kvm.h>
+#else
+#include <uapi/asm/kvm.h>
+#endif
arch/s390/include/asm/kvm_host.h +9 -0
index 1c20168a3ef5..0330d3e503d8 100644
@@ -3,7 +3,16 @@
 #ifndef ASM_KVM_HOST_H
 #define ASM_KVM_HOST_H
 
+#ifdef KVM_S390_ARM64
+#include <asm/kvm_host_arm64.h>
+#else
 #include <asm/kvm_host_s390.h>
+#endif
+
+static inline  bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
 
 #define PGM_PROTECTION			0x04
 #define PGM_ADDRESSING			0x05
arch/s390/include/asm/kvm_host_arm64.h new file +212 -0

212 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#7 lore raw

PATCH v3 4/27 uapi: KVM: Provide arm64 UAPI for other host architectures

In reply to Steffen Eiden

Enable the ARM64 userspace API to be used on non-arm64 host architectures, with initial support for s390.

The arm64 KVM UAPI headers are relocated to include/uapi/arch/arm64/, allowing non‑arm64 hosts (such as s390) to use the arm64 KVM userspace API.

To achieve architecture independence, some type aliases are introduced, which conditionally resolve to native arm64 types when building on arm64 or to fallback to ABI compatible inline struct definitions on other architectures.

The build system is updated to install the moved UAPI headers to their original location but in and conditionally export arm64 architecture headers for s390. This infrastructure enables s390 systems to host arm64 virtual machines while maintaining full compatibility with the existing arm64 KVM-UAPI, requiring only minimal, compatible changes to the arm64 UAPI-headers itself.

---
 MAINTAINERS                                 |    2 +
 Makefile                                    |    1 +
 arch/arm64/include/uapi/asm/kvm.h           |   28 +-
 arch/arm64/include/uapi/asm/sve_context.h   |    4 +-
 arch/s390/Makefile                          |    2 +
 include/uapi/Kbuild                         |    4 +
 include/uapi/arch/arm64/asm/Kbuild          |    5 +
 include/uapi/arch/arm64/linux/kvm.h         |    8 +
 include/uapi/linux/{kvm.h => kvm-generic.h} |   11 +-
 include/uapi/linux/kvm.h                    | 1660 +------------------
 scripts/Makefile.asm-headers                |   13 +-
 usr/include/Makefile                        |    1 +
 12 files changed, 69 insertions(+), 1670 deletions(-)
 create mode 100644 include/uapi/arch/arm64/asm/Kbuild
 create mode 100644 include/uapi/arch/arm64/linux/kvm.h
 copy include/uapi/linux/{kvm.h => kvm-generic.h} (99%)
MAINTAINERS +2 -0
index 461a3eed6129..52b66c8b97f4 100644
@@ -3838,6 +3838,7 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git
 F:	Documentation/arch/arm64/
 F:	arch/arm64/
+F:	include/uapi/arch/arm64/
 F:	drivers/virt/coco/arm-cca-guest/
 F:	drivers/virt/coco/pkvm-guest/
 F:	tools/testing/selftests/arm64/
@@ -14075,6 +14076,7 @@ F:	Documentation/virt/kvm/arm/
 F:	Documentation/virt/kvm/devices/arm*
 F:	arch/arm64/include/asm/kvm*
 F:	arch/arm64/include/uapi/asm/kvm*
+F:	include/uapi/arch/arm64/asm/kvm*
 F:	arch/arm64/kvm/
 F:	include/kvm/arm_*
 F:	tools/testing/selftests/kvm/*/arm64/
Makefile +1 -0
index f056c921ea9c..c34f823a9db0 100644
@@ -588,6 +588,7 @@ LINUXINCLUDE    := \
 		-I$(objtree)/arch/$(SRCARCH)/include/generated \
 		-I$(srctree)/include \
 		-I$(objtree)/include \
+		-I$(objtree)/include/generated \
 		$(USERINCLUDE)
 
 KBUILD_AFLAGS   := -D__ASSEMBLY__ -fno-PIE
arch/arm64/include/uapi/asm/kvm.h +24 -4
index 1c13bfa2d38a..75cf5aff940e 100644
@@ -20,7 +20,9 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef __ARM_KVM_H__
+#ifndef __ARM64_KVM_SHARED_H__
+#define __ARM64_KVM_SHARED_H__
+
 #define __ARM_KVM_H__
 
 #define KVM_SPSR_EL1	0
@@ -34,8 +36,26 @@
 #ifndef __ASSEMBLER__
 #include <linux/psci.h>
 #include <linux/types.h>
+#include "sve_context.h"
+
+#ifdef __aarch64__
 #include <asm/ptrace.h>
-#include <asm/sve_context.h>
+typedef struct user_pt_regs user_pt_regs_arm64;
+typedef struct user_fpsimd_state user_fpsimd_state_arm64;
+#else
+typedef struct {
+	__u64		regs[31];
+	__u64		sp;
+	__u64		pc;
+	__u64		pstate;
+} user_pt_regs_arm64;
+typedef struct {
+	__uint128_t	vregs[32];
+	__u32		fpsr;
+	__u32		fpcr;
+	__u32		__reserved[2];
+} __attribute__((aligned(16))) user_fpsimd_state_arm64;
+#endif /* __aarch64__ */
 
 #define __KVM_HAVE_IRQ_LINE
 #define __KVM_HAVE_VCPU_EVENTS
@@ -44,14 +64,14 @@
 #define KVM_DIRTY_LOG_PAGE_OFFSET 64
 
 struct kvm_regs {
-	struct user_pt_regs regs;	/* sp = sp_el0 */
+	user_pt_regs_arm64 regs;	/* sp = sp_el0 */
 
 	__u64	sp_el1;
 	__u64	elr_el1;
 
 	__u64	spsr[KVM_NR_SPSR];
 
-	struct user_fpsimd_state fp_regs;
+	user_fpsimd_state_arm64 fp_regs;
 };
 
 /*
arch/arm64/include/uapi/asm/sve_context.h +3 -1
index 72aefc081061..ae59eecb7e86 100644
@@ -6,7 +6,9 @@
  * Do not make direct use of header or its definitions.
  */
 
-#ifndef _UAPI__ASM_SVE_CONTEXT_H
+#ifndef __ARM64_SVE_CONTEXT_SHARED_H__
+#define __ARM64_SVE_CONTEXT_SHARED_H__
+
 #define _UAPI__ASM_SVE_CONTEXT_H
 
 #include <linux/types.h>
arch/s390/Makefile +2 -0
index 297976b41088..62689ac7a2e1 100644
@@ -138,6 +138,8 @@ zfcpdump:
 
 archheaders:
 	$(Q)$(MAKE) $(build)=$(syscalls) all
+	$(Q)$(MAKE) $(asm-generic)=$(objtree)/include/generated/uapi/arch/arm64/asm \
+		kbuild-file=$(srctree)/include/uapi/arch/arm64/asm/Kbuild
 
 archprepare:
 	$(Q)$(MAKE) $(build)=$(tools) kapi $(extra_tools)
include/uapi/Kbuild +4 -0
index 61ee6e59c930..452bf9d5c9c0 100644
@@ -12,3 +12,7 @@ ifeq ($(wildcard $(objtree)/arch/$(SRCARCH)/include/generated/uapi/asm/kvm_para.
 no-export-headers += linux/kvm_para.h
 endif
 endif
+
+ifneq ($(ARCH),s390)
+no-export-headers += arch/%
+endif
include/uapi/arch/arm64/asm/Kbuild new file +5 -0
new file mode 100644
index 000000000000..73b69888ea28
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+shared-uapi-src := arch/arm64/include/uapi/asm
+
+shared-uapi-y += kvm.h
+shared-uapi-y += sve_context.h
include/uapi/arch/arm64/linux/kvm.h new file +8 -0
new file mode 100644
index 000000000000..21528a146b4d
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef __LINUX_KVM_H
+#define __LINUX_KVM_H
+
+#include <arch/arm64/asm/kvm.h>
+#include <linux/kvm-generic.h>
+
+#endif /*__LINUX_KVM_H */
include/uapi/linux/kvm-generic.h renamed from include/uapi/linux/kvm.h +7 -4
similarity index 99%
copy from include/uapi/linux/kvm.h
copy to include/uapi/linux/kvm-generic.h
index 6c8afa2047bf..a9c757029808 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
-#ifndef __LINUX_KVM_H
-#define __LINUX_KVM_H
+#ifndef __LINUX_GENERIC_KVM_H
+#define __LINUX_GENERIC_KVM_H
 
 /*
  * Userspace interface for /dev/kvm - kernel based virtual machine
@@ -8,12 +8,15 @@
  * Note: you must update KVM_API_VERSION if you change this interface.
  */
 
+#ifndef __LINUX_KVM_H
+#error "Error: Do not directly include <linux/kvm-generic.h> include <linux/kvm.h>"
+#endif /* __LINUX_KVM_H */
+
 #include <linux/const.h>
 #include <linux/types.h>
 #include <linux/compiler.h>
 #include <linux/stddef.h>
 #include <linux/ioctl.h>
-#include <asm/kvm.h>
 
 #ifdef __KERNEL__
 #include <linux/kvm_types.h>
@@ -1669,4 +1672,4 @@ struct kvm_pre_fault_memory {
 	__u64 padding[5];
 };
 
-#endif /* __LINUX_KVM_H */
+#endif /* __LINUX_KVM_GENERIC_H */
include/uapi/linux/kvm.h +1 -1659

1,666 lines not on this page · raw message

scripts/Makefile.asm-headers +11 -2
index 8a4856e74180..86d4d7173fcd 100644
@@ -46,14 +46,18 @@ generic-y   += $(foreach f, $(mandatory-y), $(if $(wildcard $(src)/$(f)),,$(f)))
 generic-y   := $(addprefix $(obj)/, $(generic-y))
 syscall-y   := $(addprefix $(obj)/, $(syscall-y))
 generated-y := $(addprefix $(obj)/, $(generated-y))
+shared-uapi-y  := $(addprefix $(obj)/, $(shared-uapi-y))
 
 # Remove stale wrappers when the corresponding files are removed from generic-y
 old-headers := $(wildcard $(obj)/*.h)
-unwanted    := $(filter-out $(generic-y) $(generated-y) $(syscall-y),$(old-headers))
+unwanted    := $(filter-out $(generic-y) $(generated-y) $(syscall-y) $(shared-uapi-y),$(old-headers))
 
 quiet_cmd_wrap = WRAP    $@
       cmd_wrap = echo "\#include <asm-generic/$*.h>" > $@
 
+quiet_cmd_share_hdr = SHARE   $@
+      cmd_share_hdr = cp $< $@
+
 quiet_cmd_remove = REMOVE  $(unwanted)
       cmd_remove = rm -f $(unwanted)
 
@@ -70,13 +74,18 @@ quiet_cmd_systbl = SYSTBL  $@
 		   --abis $(subst $(space),$(comma),$(strip $(syscall_abis_$*))) \
 		   $< $@
 
-all: $(generic-y) $(syscall-y)
+all: $(generic-y) $(syscall-y) $(shared-uapi-y)
 	$(if $(unwanted),$(call cmd,remove))
 	@:
 
 $(obj)/%.h: $(srctree)/$(generic)/%.h
 	$(call cmd,wrap)
 
+# Let architectures define architecture specific headers that are shared with
+# other architectures to enable single-system cross architecture communication
+$(shared-uapi-y): $(obj)/%.h: $(shared-uapi-src)/%.h FORCE
+	$(call if_changed,share_hdr)
+
 $(obj)/unistd_%.h: $(syscalltbl) $(syshdr) FORCE
 	$(call if_changed,syshdr)
 
usr/include/Makefile +1 -0
index ee69dd9d970e..3bab8df90d4e 100644
@@ -26,6 +26,7 @@ no-header-test += linux/cyclades.h
 no-header-test += linux/errqueue.h
 no-header-test += linux/hdlc/ioctl.h
 no-header-test += linux/ivtv.h
+no-header-test += linux/kvm-generic.h
 no-header-test += linux/matroxfb.h
 no-header-test += linux/omap3isp.h
 no-header-test += linux/omapfb.h
Signature · 1 line
2.53.0
author reviewer
#9 lore raw

PATCH v3 5/27 arm64: Extract sysreg definitions

In reply to Steffen Eiden

From: Nina Schoetterl-Glausch <[email protected]>

Split all definitions that can be used by non-native architectures into a separate file sysreg-defs.h. The generated sysreg definitions are generated into sysreg-defs-gen.h. This allows other architectures using the sysreg definitions.

---
 arch/arm64/include/asm/Kbuild                 |   2 +-
 .../include/asm/{sysreg.h => sysreg-defs.h}   | 313 +-----
 arch/arm64/include/asm/sysreg.h               | 967 +-----------------
 arch/arm64/tools/Makefile                     |  10 +-
 arch/arm64/tools/Makefile.sysreg              |  12 +
 arch/arm64/tools/gen-sysreg.awk               |   6 +-
 6 files changed, 27 insertions(+), 1283 deletions(-)
 copy arch/arm64/include/asm/{sysreg.h => sysreg-defs.h} (79%)
 create mode 100644 arch/arm64/tools/Makefile.sysreg
arch/arm64/include/asm/Kbuild +1 -1
index 31441790b808..6adbc0bc36af 100644
@@ -18,4 +18,4 @@ generic-y += user.h
 
 generated-y += cpucap-defs.h
 generated-y += kernel-hwcap.h
-generated-y += sysreg-defs.h
+generated-y += sysreg-gen-defs.h
arch/arm64/include/asm/sysreg-defs.h renamed from arch/arm64/include/asm/sysreg.h +7 -306

379 lines not on this page · raw message

arch/arm64/include/asm/sysreg.h +1 -966

1,018 lines not on this page · raw message

arch/arm64/tools/Makefile +3 -7
index a94b3d9caad6..4147187b6627 100644
@@ -3,11 +3,12 @@
 gen := arch/$(ARCH)/include/generated
 kapi := $(gen)/asm
 
-kapisyshdr-y := cpucap-defs.h kernel-hwcap.h sysreg-defs.h
+include $(srctree)/arch/arm64/tools/Makefile.sysreg
 
+kapisyshdr-y := cpucap-defs.h kernel-hwcap.h
 kapi-hdrs-y := $(addprefix $(kapi)/, $(kapisyshdr-y))
 
-targets += $(addprefix ../../../, $(kapi-hdrs-y))
+targets += $(addprefix ../../../, $(kapi)/cpucap-defs.h)
 
 PHONY += kapi
 
@@ -21,14 +22,9 @@ quiet_cmd_gen_cpucaps = GEN     $@
 quiet_cmd_gen_kernel_hwcap = GEN     $@
       cmd_gen_kernel_hwcap = mkdir -p $(dir $@); /bin/sh -e $(real-prereqs) > $@
 
-quiet_cmd_gen_sysreg = GEN     $@
-      cmd_gen_sysreg = mkdir -p $(dir $@); $(AWK) -f $(real-prereqs) > $@
 
 $(kapi)/cpucap-defs.h: $(src)/gen-cpucaps.awk $(src)/cpucaps FORCE
 	$(call if_changed,gen_cpucaps)
 
 $(kapi)/kernel-hwcap.h: $(src)/gen-kernel-hwcaps.sh $(srctree)/arch/arm64/include/uapi/asm/hwcap.h FORCE
 	$(call if_changed,gen_kernel_hwcap)
-
-$(kapi)/sysreg-defs.h: $(src)/gen-sysreg.awk $(src)/sysreg FORCE
-	$(call if_changed,gen_sysreg)
arch/arm64/tools/Makefile.sysreg new file +12 -0
new file mode 100644
index 000000000000..1c2096208f14
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0
+
+targets += $(addprefix ../../../, $(kapi)/sysreg-gen-defs.h)
+
+kapi:   $(kapi)/sysreg-gen-defs.h
+
+quiet_cmd_gen_sysreg = GEN     $@
+      cmd_gen_sysreg = mkdir -p $(dir $@); $(AWK) -f $(real-prereqs) > $@
+
+$(kapi)/sysreg-gen-defs.h: $(srctree)/arch/arm64/tools/gen-sysreg.awk $(srctree)/arch/arm64/tools/sysreg FORCE
+	$(call if_changed,gen_sysreg)
+
arch/arm64/tools/gen-sysreg.awk +3 -3
index 86860ab672dc..ffdabd3ff84a 100755
@@ -107,8 +107,8 @@ function parse_bitdef(reg, field, bitdef, _bits)
 }
 
 BEGIN {
-	print "#ifndef __ASM_SYSREG_DEFS_H"
-	print "#define __ASM_SYSREG_DEFS_H"
+	print "#ifndef __ASM_SYSREG_GEN_DEFS_H"
+	print "#define __ASM_SYSREG_GEN_DEFS_H"
 	print ""
 	print "/* Generated file - do not edit */"
 	print ""
@@ -121,7 +121,7 @@ END {
 	if (__current_block_depth != 0)
 		fatal("Missing terminator for " block_current() " block")
 
-	print "#endif /* __ASM_SYSREG_DEFS_H */"
+	print "#endif /* __ASM_SYSREG_GEN_DEFS_H */"
 }
 
 # skip blank lines and comment lines
Signature · 1 line
2.53.0
author reviewer
#23 lore raw

PATCH v3 14/27 KVM: s390: Move s390 kvm code into a subdirectory

In reply to Steffen Eiden

Move all the code required to run s390 KVM guests on s390 to a s390 subdirectory. Move gmap related code into a gmap directory to share gmap code between KVM implementations. Additionally, prepare the build system and s390-kvm headers for a second KVM implementation.

While at it, rename the main s390-kvm file from s390-kvm.{c,h} to just s390.{c,h} to match the naming scheme of other architectures.

The module name stays kvm.

---
 arch/s390/Kconfig                             |   2 +-
 arch/s390/boot/ipl_parm.c                     |   2 +-
 arch/s390/boot/uv.c                           |   2 +-
 arch/s390/configs/debug_defconfig             |   2 +-
 arch/s390/configs/defconfig                   |   2 +-
 arch/s390/include/asm/kvm_host.h              | 751 +-----------------
 .../asm/{kvm_host.h => kvm_host_s390.h}       |   8 +-
 ...kvm_host_types.h => kvm_host_s390_types.h} |   0
 arch/s390/kernel/asm-offsets.c                |   2 +-
 arch/s390/kernel/early.c                      |   2 +-
 arch/s390/kernel/entry.S                      |  10 +-
 arch/s390/kernel/perf_event.c                 |   2 +-
 arch/s390/kvm/Kconfig                         |  35 +-
 arch/s390/kvm/Makefile                        |  11 +-
 arch/s390/kvm/gmap/Makefile                   |   5 +
 arch/s390/kvm/{ => gmap}/dat.c                |   0
 arch/s390/kvm/{ => gmap}/dat.h                |   6 +-
 arch/s390/kvm/{ => gmap}/faultin.c            |   0
 arch/s390/kvm/{ => gmap}/faultin.h            |   6 +-
 arch/s390/kvm/{ => gmap}/gmap.c               |   2 +-
 arch/s390/kvm/{ => gmap}/gmap.h               |   6 +-
 arch/s390/kvm/{ => s390}/Kconfig              |  27 +-
 arch/s390/kvm/{ => s390}/Makefile             |  10 +-
 arch/s390/kvm/{ => s390}/diag.c               |   2 +-
 arch/s390/kvm/{ => s390}/gaccess.c            |   2 +-
 arch/s390/kvm/{ => s390}/gaccess.h            |   2 +-
 arch/s390/kvm/{ => s390}/guestdbg.c           |   2 +-
 arch/s390/kvm/{ => s390}/intercept.c          |   2 +-
 arch/s390/kvm/{ => s390}/interrupt.c          |   2 +-
 arch/s390/kvm/{ => s390}/pci.c                |   2 +-
 arch/s390/kvm/{ => s390}/pci.h                |   0
 arch/s390/kvm/{ => s390}/priv.c               |   2 +-
 arch/s390/kvm/{ => s390}/pv.c                 |   2 +-
 arch/s390/kvm/{kvm-s390.c => s390/s390.c}     |   2 +-
 arch/s390/kvm/{kvm-s390.h => s390/s390.h}     |   2 +-
 arch/s390/kvm/{ => s390}/sigp.c               |   2 +-
 arch/s390/kvm/{ => s390}/trace-s390.h         |   0
 arch/s390/kvm/{ => s390}/trace.h              |   0
 arch/s390/kvm/{ => s390}/vsie.c               |   2 +-
 include/linux/kvm_host.h                      |   2 +-
 40 files changed, 62 insertions(+), 859 deletions(-)
 copy arch/s390/include/asm/{kvm_host.h => kvm_host_s390.h} (99%)
 rename arch/s390/include/asm/{kvm_host_types.h => kvm_host_s390_types.h} (100%)
 create mode 100644 arch/s390/kvm/gmap/Makefile
 rename arch/s390/kvm/{ => gmap}/dat.c (100%)
 rename arch/s390/kvm/{ => gmap}/dat.h (99%)
 rename arch/s390/kvm/{ => gmap}/faultin.c (100%)
 rename arch/s390/kvm/{ => gmap}/faultin.h (96%)
 rename arch/s390/kvm/{ => gmap}/gmap.c (99%)
 rename arch/s390/kvm/{ => gmap}/gmap.h (99%)
 copy arch/s390/kvm/{ => s390}/Kconfig (60%)
 copy arch/s390/kvm/{ => s390}/Makefile (53%)
 rename arch/s390/kvm/{ => s390}/diag.c (99%)
 rename arch/s390/kvm/{ => s390}/gaccess.c (99%)
 rename arch/s390/kvm/{ => s390}/gaccess.h (99%)
 rename arch/s390/kvm/{ => s390}/guestdbg.c (99%)
 rename arch/s390/kvm/{ => s390}/intercept.c (99%)
 rename arch/s390/kvm/{ => s390}/interrupt.c (99%)
 rename arch/s390/kvm/{ => s390}/pci.c (99%)
 rename arch/s390/kvm/{ => s390}/pci.h (100%)
 rename arch/s390/kvm/{ => s390}/priv.c (99%)
 rename arch/s390/kvm/{ => s390}/pv.c (99%)
 rename arch/s390/kvm/{kvm-s390.c => s390/s390.c} (99%)
 rename arch/s390/kvm/{kvm-s390.h => s390/s390.h} (99%)
 rename arch/s390/kvm/{ => s390}/sigp.c (99%)
 rename arch/s390/kvm/{ => s390}/trace-s390.h (100%)
 rename arch/s390/kvm/{ => s390}/trace.h (100%)
 rename arch/s390/kvm/{ => s390}/vsie.c (99%)
arch/s390/Kconfig +1 -1
index ecbcbb781e40..5583529918db 100644
@@ -837,7 +837,7 @@ config VFIO_CCW
 config VFIO_AP
 	def_tristate n
 	prompt "VFIO support for AP devices"
-	depends on KVM
+	depends on KVM_S390
 	depends on VFIO
 	depends on AP
 	select VFIO_MDEV
arch/s390/boot/ipl_parm.c +1 -1
index 6bc950b92be7..b8906ba7e5e8 100644
@@ -300,7 +300,7 @@ void parse_boot_command_line(void)
 			stack_protector_debug = 1;
 #endif
 
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 		if (!strcmp(param, "prot_virt")) {
 			rc = kstrtobool(val, &enabled);
 			if (!rc && enabled)
arch/s390/boot/uv.c +1 -1
index 4568e8f81dac..f7b68bdcedc1 100644
@@ -26,7 +26,7 @@ void uv_query_info(void)
 	if (uv_call(0, (uint64_t)&uvcb) && uvcb.header.rc != UVC_RC_MORE_DATA)
 		return;
 
-	if (IS_ENABLED(CONFIG_KVM)) {
+	if (IS_ENABLED(CONFIG_KVM_S390)) {
 		memcpy(uv_info.inst_calls_list, uvcb.inst_calls_list, sizeof(uv_info.inst_calls_list));
 		uv_info.uv_base_stor_len = uvcb.uv_base_stor_len;
 		uv_info.guest_base_stor_len = uvcb.conf_base_phys_stor_len;
arch/s390/configs/debug_defconfig +1 -1
index c28f9a7d0bd8..6acde221e4be 100644
@@ -59,7 +59,7 @@ CONFIG_VFIO_AP=m
 CONFIG_CMM=m
 CONFIG_APPLDATA_BASE=y
 CONFIG_S390_HYPFS_FS=y
-CONFIG_KVM=m
+CONFIG_KVM_S390=m
 CONFIG_KVM_S390_UCONTROL=y
 CONFIG_S390_UNWIND_SELFTEST=m
 CONFIG_S390_KPROBES_SANITY_TEST=m
arch/s390/configs/defconfig +1 -1
index d89c988f33ea..21e803354845 100644
@@ -57,7 +57,7 @@ CONFIG_VFIO_AP=m
 CONFIG_CMM=m
 CONFIG_APPLDATA_BASE=y
 CONFIG_S390_HYPFS_FS=y
-CONFIG_KVM=m
+CONFIG_KVM_S390=m
 CONFIG_S390_UNWIND_SELFTEST=m
 CONFIG_S390_KPROBES_SANITY_TEST=m
 CONFIG_S390_MODULES_SANITY_TEST=m
arch/s390/include/asm/kvm_host.h +1 -750

758 lines not on this page · raw message

arch/s390/include/asm/kvm_host_s390.h renamed from arch/s390/include/asm/kvm_host.h +4 -4
similarity index 99%
copy from arch/s390/include/asm/kvm_host.h
copy to arch/s390/include/asm/kvm_host_s390.h
index 8a4f4a39f7a2..8fe21e94f33c 100644
@@ -8,8 +8,8 @@
  */
 
 
-#ifndef ASM_KVM_HOST_H
-#define ASM_KVM_HOST_H
+#ifndef ASM_KVM_HOST_S390_H
+#define ASM_KVM_HOST_S390_H
 
 #include <linux/types.h>
 #include <linux/hrtimer.h>
@@ -20,7 +20,7 @@
 #include <linux/module.h>
 #include <linux/pci.h>
 #include <linux/mmu_notifier.h>
-#include <asm/kvm_host_types.h>
+#include <asm/kvm_host_s390_types.h>
 #include <asm/debug.h>
 #include <asm/cpu.h>
 #include <asm/fpu.h>
@@ -754,4 +754,4 @@ struct zpci_kvm_hook {
 
 extern struct zpci_kvm_hook zpci_kvm_hook;
 
-#endif
+#endif /* ASM_KVM_HOST_S390_H */
arch/s390/include/asm/kvm_host_s390_types.h renamed from arch/s390/include/asm/kvm_host_types.h +0 -0
similarity index 100%
rename from arch/s390/include/asm/kvm_host_types.h
rename to arch/s390/include/asm/kvm_host_s390_types.h
arch/s390/kernel/asm-offsets.c +1 -1
index fbd26f3e9f96..8619adf91cdb 100644
@@ -11,7 +11,7 @@
 #include <linux/purgatory.h>
 #include <linux/pgtable.h>
 #include <linux/ftrace_regs.h>
-#include <asm/kvm_host_types.h>
+#include <asm/kvm_host_s390_types.h>
 #include <asm/stacktrace.h>
 #include <asm/ptrace.h>
 
arch/s390/kernel/early.c +1 -1
index b27239c03d79..464f8918d447 100644
@@ -57,7 +57,7 @@ decompressor_handled_param(cmma);
 decompressor_handled_param(relocate_lowcore);
 decompressor_handled_param(bootdebug);
 __decompressor_handled_param(debug_alternative, debug-alternative);
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 decompressor_handled_param(prot_virt);
 #endif
 
arch/s390/kernel/entry.S +5 -5
index bb806d1ddae0..cd4fbdec67a7 100644
@@ -113,7 +113,7 @@ _LPP_OFFSET	= __LC_LPP
 		    "jnz .+8; .insn rrf,0xb2e80000,0,0,13,0", ALT_SPEC(82)
 	.endm
 
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	.macro SIEEXIT sie_control,lowcore
 	lg	%r9,\sie_control			# get control block pointer
 	ni	__SIE_PROG0C+3(%r9),0xfe		# no longer in SIE
@@ -184,7 +184,7 @@ EXPORT_SYMBOL(__WARN_trap)
 
 #endif /* CONFIG_BUG && CONFIG_CC_HAS_ASM_IMMEDIATE_STRINGS */
 
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 /*
  * __sie64a calling convention:
  * %r2 pointer to sie control block phys
@@ -315,7 +315,7 @@ SYM_CODE_START(pgm_check_handler)
 	xgr	%r10,%r10
 	tmhh	%r8,0x0001		# coming from user space?
 	jo	3f			# -> fault in user space
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	lg	%r11,__LC_CURRENT(%r13)
 	tm	__TI_sie(%r11),0xff
 	jz	1f
@@ -386,7 +386,7 @@ SYM_CODE_START(\name)
 	lmg	%r8,%r9,\lc_old_psw(%r13)
 	tmhh	%r8,0x0001			# interrupting from user ?
 	jnz	1f
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	lg	%r10,__LC_CURRENT(%r13)
 	tm	__TI_sie(%r10),0xff
 	jz	0f
@@ -465,7 +465,7 @@ SYM_CODE_START(mcck_int_handler)
 	jnz	.Lmcck_user
 	TSTMSK	__LC_MCCK_CODE(%r13),MCCK_CODE_PSW_IA_VALID
 	jno	.Lmcck_panic
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	lg	%r10,__LC_CURRENT(%r13)
 	tm	__TI_sie(%r10),0xff
 	jz	.Lmcck_user
arch/s390/kernel/perf_event.c +1 -1
index 606750bae508..6441746b8e72 100644
@@ -37,7 +37,7 @@ static bool is_in_guest(struct pt_regs *regs)
 {
 	if (user_mode(regs))
 		return false;
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	return instruction_pointer(regs) == (unsigned long) &sie_exit;
 #else
 	return false;
arch/s390/kvm/Kconfig +2 -33
index 5b835bc6a194..f8d4a9a38dae 100644
@@ -17,39 +17,8 @@ menuconfig VIRTUALIZATION
 if VIRTUALIZATION
 
 config KVM
-	def_tristate y
-	prompt "Kernel-based Virtual Machine (KVM) support"
-	select HAVE_KVM_CPU_RELAX_INTERCEPT
-	select KVM_ASYNC_PF
-	select KVM_ASYNC_PF_SYNC
-	select KVM_COMMON
-	select HAVE_KVM_IRQCHIP
-	select HAVE_KVM_IRQ_ROUTING
-	select HAVE_KVM_INVALID_WAKEUPS
-	select HAVE_KVM_NO_POLL
-	select KVM_VFIO
-	select VIRT_XFER_TO_GUEST_WORK
-	select KVM_MMU_LOCKLESS_AGING
-	help
-	  Support hosting paravirtualized guest machines using the SIE
-	  virtualization capability on the mainframe. This should work
-	  on any 64bit machine.
-
-	  This module provides access to the hardware capabilities through
-	  a character device node named /dev/kvm.
-
-	  To compile this as a module, choose M here: the module
-	  will be called kvm.
-
-	  If unsure, say N.
-
-config KVM_S390_UCONTROL
-	bool "Userspace controlled virtual machines"
-	depends on KVM
-	help
-	  Allow CAP_SYS_ADMIN users to create KVM virtual machines that are
-	  controlled by userspace.
+	tristate
 
-	  If unsure, say N.
+source "arch/s390/kvm/s390/Kconfig"
 
 endif # VIRTUALIZATION
arch/s390/kvm/Makefile +1 -10
index dac9d53b23d8..c43d7dffca13 100644
@@ -3,13 +3,4 @@
 #
 # Copyright IBM Corp. 2008
 
-include $(srctree)/virt/kvm/Makefile.kvm
-
-ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
-
-kvm-y += kvm-s390.o intercept.o interrupt.o priv.o sigp.o
-kvm-y += diag.o gaccess.o guestdbg.o vsie.o pv.o
-kvm-y += dat.o gmap.o faultin.o
-
-kvm-$(CONFIG_VFIO_PCI_ZDEV_KVM) += pci.o
-obj-$(CONFIG_KVM) += kvm.o
+obj-$(CONFIG_KVM_S390) += s390/
arch/s390/kvm/gmap/Makefile new file +5 -0
new file mode 100644
index 000000000000..21967ed88877
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+GMAP ?= ../gmap
+
+kvm-y += $(GMAP)/dat.o $(GMAP)/gmap.o $(GMAP)/faultin.o
arch/s390/kvm/gmap/dat.c renamed from arch/s390/kvm/dat.c +0 -0
similarity index 100%
rename from arch/s390/kvm/dat.c
rename to arch/s390/kvm/gmap/dat.c
arch/s390/kvm/gmap/dat.h renamed from arch/s390/kvm/dat.h +3 -3
similarity index 99%
rename from arch/s390/kvm/dat.h
rename to arch/s390/kvm/gmap/dat.h
index 873e13ac5a27..47c4e9b78c62 100644
@@ -6,8 +6,8 @@
  *    Author(s): Claudio Imbrenda <[email protected]>
  */
 
-#ifndef __KVM_S390_DAT_H
-#define __KVM_S390_DAT_H
+#ifndef ARCH_KVM_GMAP_DAT_H
+#define ARCH_KVM_GMAP_DAT_H
 
 #include <linux/radix-tree.h>
 #include <linux/refcount.h>
@@ -974,4 +974,4 @@ static inline bool crste_is_ucas(union crste crste)
 	return is_pmd(crste) && crste.h.i && crste.h.fc0.tl == 1 && crste.h.fc == 0;
 }
 
-#endif /* __KVM_S390_DAT_H */
+#endif /* ARCH_KVM_GMAP_DAT_H */
arch/s390/kvm/gmap/faultin.c renamed from arch/s390/kvm/faultin.c +0 -0
similarity index 100%
rename from arch/s390/kvm/faultin.c
rename to arch/s390/kvm/gmap/faultin.c
arch/s390/kvm/gmap/faultin.h renamed from arch/s390/kvm/faultin.h +3 -3
similarity index 96%
rename from arch/s390/kvm/faultin.h
rename to arch/s390/kvm/gmap/faultin.h
index f86176d2769c..f343b6fb6f16 100644
@@ -6,8 +6,8 @@
  *    Author(s): Claudio Imbrenda <[email protected]>
  */
 
-#ifndef __KVM_S390_FAULTIN_H
-#define __KVM_S390_FAULTIN_H
+#ifndef ARCH_KVM_GMAP_FAULTIN_H
+#define ARCH_KVM_GMAP_FAULTIN_H
 
 #include <linux/kvm_host.h>
 
@@ -89,4 +89,4 @@ static inline int kvm_s390_get_guest_pages(struct kvm *kvm, struct guest_fault *
 #define kvm_s390_array_needs_retry_safe(kvm, seq, array) \
 	kvm_s390_multiple_faults_need_retry(kvm, seq, array, ARRAY_SIZE(array), false)
 
-#endif /* __KVM_S390_FAULTIN_H */
+#endif /* ARCH_KVM_GMAP_FAULTIN_H */
arch/s390/kvm/gmap/gmap.c renamed from arch/s390/kvm/gmap.c +1 -1
similarity index 99%
rename from arch/s390/kvm/gmap.c
rename to arch/s390/kvm/gmap/gmap.c
index 957126ab991c..89d84086a75d 100644
@@ -21,7 +21,7 @@
 
 #include "dat.h"
 #include "gmap.h"
-#include "kvm-s390.h"
+#include "s390.h"
 #include "faultin.h"
 
 static inline bool kvm_s390_is_in_sie(struct kvm_vcpu *vcpu)
arch/s390/kvm/gmap/gmap.h renamed from arch/s390/kvm/gmap.h +3 -3
similarity index 99%
rename from arch/s390/kvm/gmap.h
rename to arch/s390/kvm/gmap/gmap.h
index 742e42a31744..67e42e16c337 100644
@@ -7,8 +7,8 @@
  *               Claudio Imbrenda <[email protected]>
  */
 
-#ifndef ARCH_KVM_S390_GMAP_H
-#define ARCH_KVM_S390_GMAP_H
+#ifndef ARCH_KVM_GMAP_GMAP_H
+#define ARCH_KVM_GMAP_GMAP_H
 
 #include "dat.h"
 
@@ -307,4 +307,4 @@ static inline bool gmap_is_shadow_valid(struct gmap *sg, union asce asce, int ed
 	return sg->guest_asce.val == asce.val && sg->edat_level == edat_level;
 }
 
-#endif /* ARCH_KVM_S390_GMAP_H */
+#endif /* ARCH_KVM_GMAP_GMAP_H */
arch/s390/kvm/s390/Kconfig renamed from arch/s390/kvm/Kconfig +6 -21
similarity index 60%
copy from arch/s390/kvm/Kconfig
copy to arch/s390/kvm/s390/Kconfig
index 5b835bc6a194..52ab1e1bb7ce 100644
@@ -1,24 +1,11 @@
 # SPDX-License-Identifier: GPL-2.0
 #
-# KVM configuration
-#
-source "virt/kvm/Kconfig"
-
-menuconfig VIRTUALIZATION
-	def_bool y
-	prompt "KVM"
-	help
-	  Say Y here to get to see options for using your Linux host to run other
-	  operating systems inside virtual machines (guests).
-	  This option alone does not add any kernel code.
+# KVM_S390 configuration
 
-	  If you say N, all options in this submenu will be skipped and disabled.
-
-if VIRTUALIZATION
-
-config KVM
+config KVM_S390
 	def_tristate y
-	prompt "Kernel-based Virtual Machine (KVM) support"
+	prompt "Kernel-based Virtual Machine (KVM) support (s390)"
+	select KVM
 	select HAVE_KVM_CPU_RELAX_INTERCEPT
 	select KVM_ASYNC_PF
 	select KVM_ASYNC_PF_SYNC
@@ -31,7 +18,7 @@ config KVM
 	select VIRT_XFER_TO_GUEST_WORK
 	select KVM_MMU_LOCKLESS_AGING
 	help
-	  Support hosting paravirtualized guest machines using the SIE
+	  Support hosting paravirtualized s390 guest machines using the SIE
 	  virtualization capability on the mainframe. This should work
 	  on any 64bit machine.
 
@@ -45,11 +32,9 @@ config KVM
 
 config KVM_S390_UCONTROL
 	bool "Userspace controlled virtual machines"
-	depends on KVM
+	depends on KVM_S390
 	help
 	  Allow CAP_SYS_ADMIN users to create KVM virtual machines that are
 	  controlled by userspace.
 
 	  If unsure, say N.
-
-endif # VIRTUALIZATION
arch/s390/kvm/s390/Makefile renamed from arch/s390/kvm/Makefile +6 -4
similarity index 53%
copy from arch/s390/kvm/Makefile
copy to arch/s390/kvm/s390/Makefile
index dac9d53b23d8..51aee874b36f 100644
@@ -3,13 +3,15 @@
 #
 # Copyright IBM Corp. 2008
 
+KVM := ../../../../virt/kvm
 include $(srctree)/virt/kvm/Makefile.kvm
+include $(srctree)/arch/s390/kvm/gmap/Makefile
 
-ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
+ccflags-y := -I$(src) -I$(srctree)/arch/s390/kvm/gmap
 
-kvm-y += kvm-s390.o intercept.o interrupt.o priv.o sigp.o
+kvm-y += s390.o intercept.o interrupt.o priv.o sigp.o
 kvm-y += diag.o gaccess.o guestdbg.o vsie.o pv.o
-kvm-y += dat.o gmap.o faultin.o
 
 kvm-$(CONFIG_VFIO_PCI_ZDEV_KVM) += pci.o
-obj-$(CONFIG_KVM) += kvm.o
+
+obj-$(CONFIG_KVM_S390) += kvm.o
arch/s390/kvm/s390/diag.c renamed from arch/s390/kvm/diag.c +1 -1
similarity index 99%
rename from arch/s390/kvm/diag.c
rename to arch/s390/kvm/s390/diag.c
index d89d1c381522..700d9b7b68bc 100644
@@ -12,7 +12,7 @@
 #include <linux/kvm_host.h>
 #include <asm/gmap_helpers.h>
 #include <asm/virtio-ccw.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "trace.h"
 #include "trace-s390.h"
 #include "gaccess.h"
arch/s390/kvm/s390/gaccess.c renamed from arch/s390/kvm/gaccess.c +1 -1
similarity index 99%
rename from arch/s390/kvm/gaccess.c
rename to arch/s390/kvm/s390/gaccess.c
index 4f8d5592c9a9..67fe0ca62f01 100644
@@ -17,7 +17,7 @@
 #include <asm/access-regs.h>
 #include <asm/fault.h>
 #include <asm/dat-bits.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "dat.h"
 #include "gmap.h"
 #include "gaccess.h"
arch/s390/kvm/s390/gaccess.h renamed from arch/s390/kvm/gaccess.h +1 -1
similarity index 99%
rename from arch/s390/kvm/gaccess.h
rename to arch/s390/kvm/s390/gaccess.h
index b5385cec60f4..ef922b3b4990 100644
@@ -14,7 +14,7 @@
 #include <linux/kvm_host.h>
 #include <linux/uaccess.h>
 #include <linux/ptrace.h>
-#include "kvm-s390.h"
+#include "s390.h"
 
 /**
  * kvm_s390_real_to_abs - convert guest real address to guest absolute address
arch/s390/kvm/s390/guestdbg.c renamed from arch/s390/kvm/guestdbg.c +1 -1
similarity index 99%
rename from arch/s390/kvm/guestdbg.c
rename to arch/s390/kvm/s390/guestdbg.c
index 69835e1d4f20..c880ebbdef2f 100644
@@ -8,7 +8,7 @@
  */
 #include <linux/kvm_host.h>
 #include <linux/errno.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 
 /*
arch/s390/kvm/s390/intercept.c renamed from arch/s390/kvm/intercept.c +1 -1
similarity index 99%
rename from arch/s390/kvm/intercept.c
rename to arch/s390/kvm/s390/intercept.c
index 39aff324203e..9b897328e4b9 100644
@@ -17,7 +17,7 @@
 #include <asm/sysinfo.h>
 #include <asm/uv.h>
 
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 #include "trace.h"
 #include "trace-s390.h"
arch/s390/kvm/s390/interrupt.c renamed from arch/s390/kvm/interrupt.c +1 -1
similarity index 99%
rename from arch/s390/kvm/interrupt.c
rename to arch/s390/kvm/s390/interrupt.c
index 3bcdbbbb6891..7f0b92a13d9a 100644
@@ -29,7 +29,7 @@
 #include <asm/nmi.h>
 #include <asm/airq.h>
 #include <asm/tpi.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 #include "trace-s390.h"
 #include "pci.h"
arch/s390/kvm/s390/pci.c renamed from arch/s390/kvm/pci.c +1 -1
similarity index 99%
rename from arch/s390/kvm/pci.c
rename to arch/s390/kvm/s390/pci.c
index 5b075c38998e..efdc1ddf6ba8 100644
@@ -14,7 +14,7 @@
 #include <asm/pci_io.h>
 #include <asm/sclp.h>
 #include "pci.h"
-#include "kvm-s390.h"
+#include "s390.h"
 
 struct zpci_aift *aift;
 
arch/s390/kvm/s390/pci.h renamed from arch/s390/kvm/pci.h +0 -0
similarity index 100%
rename from arch/s390/kvm/pci.h
rename to arch/s390/kvm/s390/pci.h
arch/s390/kvm/s390/priv.c renamed from arch/s390/kvm/priv.c +1 -1
similarity index 99%
rename from arch/s390/kvm/priv.c
rename to arch/s390/kvm/s390/priv.c
index cc0553da14cb..1a138ae45268 100644
@@ -26,7 +26,7 @@
 #include <asm/ap.h>
 #include <asm/gmap_helpers.h>
 #include "gaccess.h"
-#include "kvm-s390.h"
+#include "s390.h"
 #include "trace.h"
 #include "gmap.h"
 
arch/s390/kvm/s390/pv.c renamed from arch/s390/kvm/pv.c +1 -1
similarity index 99%
rename from arch/s390/kvm/pv.c
rename to arch/s390/kvm/s390/pv.c
index c2dafd812a3b..1bddc9aeb1a9 100644
@@ -17,7 +17,7 @@
 #include <linux/pagewalk.h>
 #include <linux/sched/mm.h>
 #include <linux/mmu_notifier.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "dat.h"
 #include "gaccess.h"
 #include "gmap.h"
arch/s390/kvm/s390/s390.c renamed from arch/s390/kvm/kvm-s390.c +1 -1
similarity index 99%
rename from arch/s390/kvm/kvm-s390.c
rename to arch/s390/kvm/s390/s390.c
index e09960c2e6ed..acf9832ca69a 100644
@@ -50,7 +50,7 @@
 #include <asm/fpu.h>
 #include <asm/ap.h>
 #include <asm/uv.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 #include "gmap.h"
 #include "faultin.h"
arch/s390/kvm/s390/s390.h renamed from arch/s390/kvm/kvm-s390.h +1 -1
similarity index 99%
rename from arch/s390/kvm/kvm-s390.h
rename to arch/s390/kvm/s390/s390.h
index dc0573b7aa4b..41c92a358b17 100644
@@ -451,7 +451,7 @@ void kvm_s390_vsie_destroy(struct kvm *kvm);
 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);
 int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
 
-/* implemented in kvm-s390.c */
+/* implemented in s390.c */
 int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod);
 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr);
 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);
arch/s390/kvm/s390/sigp.c renamed from arch/s390/kvm/sigp.c +1 -1
similarity index 99%
rename from arch/s390/kvm/sigp.c
rename to arch/s390/kvm/s390/sigp.c
index 55c34cb35428..131b3371ef4f 100644
@@ -14,7 +14,7 @@
 #include <linux/slab.h>
 #include <asm/sigp.h>
 #include "gaccess.h"
-#include "kvm-s390.h"
+#include "s390.h"
 #include "trace.h"
 
 static int __sigp_sense(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
arch/s390/kvm/s390/trace-s390.h renamed from arch/s390/kvm/trace-s390.h +0 -0
similarity index 100%
rename from arch/s390/kvm/trace-s390.h
rename to arch/s390/kvm/s390/trace-s390.h
arch/s390/kvm/s390/trace.h renamed from arch/s390/kvm/trace.h +0 -0
similarity index 100%
rename from arch/s390/kvm/trace.h
rename to arch/s390/kvm/s390/trace.h
arch/s390/kvm/s390/vsie.c renamed from arch/s390/kvm/vsie.c +1 -1
similarity index 99%
rename from arch/s390/kvm/vsie.c
rename to arch/s390/kvm/s390/vsie.c
index e5a23f1c9749..43dce397c613 100644
@@ -20,7 +20,7 @@
 #include <asm/nmi.h>
 #include <asm/dis.h>
 #include <asm/facility.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 #include "gmap.h"
 
include/linux/kvm_host.h +1 -1
index 088986d0aca2..76da4196b684 100644
@@ -1536,7 +1536,7 @@ void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu);
 bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu);
 
-#ifndef CONFIG_S390
+#ifndef CONFIG_KVM_S390
 void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, bool wait);
 
 static inline void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
Signature · 1 line
2.53.0
author reviewer
#22 lore raw

PATCH v3 13/27 KVM: arm64: Extract & share ipa size shift calculation

In reply to Steffen Eiden

Extract the ipa shift calculation from kvm_init_ipa_range into its own function kvm_vm_type_ipa_size_shift to be shared across architectures.

User space passes a type parameter to the VM creation ioctl, indicating the physical size of the VM. Therefore extract the ipa shift calculation from kvm_init_ipa_range into its own function kvm_vm_type_ipa_size_shift, so all implementers of arm64 KVM can make use of it for VM creation.

---
 arch/arm64/kvm/mmu.c         | 19 ++++++-------------
 include/kvm/arm64/kvm_host.h |  1 +
 virt/kvm/arm64/arm.c         | 21 +++++++++++++++++++++
 3 files changed, 28 insertions(+), 13 deletions(-)
arch/arm64/kvm/mmu.c +6 -13
index f683eb18c4ec..e202063fcdda 100644
@@ -877,24 +877,17 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
 
 static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
 {
-	u32 kvm_ipa_limit = get_kvm_ipa_limit();
 	u64 mmfr0, mmfr1;
 	u32 phys_shift;
+	int r;
 
-	phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
 	if (is_protected_kvm_enabled()) {
-		phys_shift = kvm_ipa_limit;
-	} else if (phys_shift) {
-		if (phys_shift > kvm_ipa_limit ||
-		    phys_shift < ARM64_MIN_PARANGE_BITS)
-			return -EINVAL;
+		phys_shift = get_kvm_ipa_limit();
 	} else {
-		phys_shift = KVM_PHYS_SHIFT;
-		if (phys_shift > kvm_ipa_limit) {
-			pr_warn_once("%s using unsupported default IPA limit, upgrade your VMM\n",
-				     current->comm);
-			return -EINVAL;
-		}
+		r = kvm_vm_type_ipa_size_shift(type);
+		if (r < 0)
+			return r;
+		phys_shift = r;
 	}
 
 	mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
include/kvm/arm64/kvm_host.h +1 -0
index d36ebea4d60f..8bf399508757 100644
@@ -33,6 +33,7 @@ int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
 				 const struct kvm_vcpu_init *init);
 bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
 			   const struct kvm_vcpu_init *init);
+int kvm_vm_type_ipa_size_shift(unsigned long type);
 
 /* MMIO helpers */
 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data);
virt/kvm/arm64/arm.c +21 -0
index b47adef65e5f..0bbfbe63e558 100644
@@ -52,3 +52,24 @@ bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
 	return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features,
 			     KVM_VCPU_MAX_FEATURES);
 }
+
+int kvm_vm_type_ipa_size_shift(unsigned long type)
+{
+	int phys_shift;
+
+	phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
+	if (phys_shift) {
+		if (phys_shift > get_kvm_ipa_limit() ||
+		    phys_shift < ARM64_MIN_PARANGE_BITS)
+			return -EINVAL;
+	} else {
+		phys_shift = KVM_PHYS_SHIFT;
+		if (phys_shift > get_kvm_ipa_limit()) {
+			pr_warn_once("%s using unsupported default IPA limit, upgrade your VMM\n",
+				     current->comm);
+			return -EINVAL;
+		}
+	}
+
+	return phys_shift;
+}
Signature · 1 line
2.53.0
author reviewer
#31 lore raw

PATCH v3 18/27 KVM: s390: Prepare kvm-s390 for a second kvm module

In reply to Steffen Eiden

The second KVM module will have a different Kconfig set. When both modules are compiled the Kconfig sets get merged and the native s390 KVM needs to implement functionality required by the respective config options. Ensure that s390-KVM will still compile.

---
 arch/s390/kvm/s390/s390.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/s390/kvm/s390/s390.c +8 -0
index c9dff21f823e..e9352864a492 100644
@@ -5683,6 +5683,14 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 	return gmap_unmap_gfn_range(kvm->arch.gmap, range->slot, range->start, range->end);
 }
 
+#ifdef CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE
+/* Make s390 compile if arm64-on-s390 is selected */
+int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+#endif /* CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE */
+
 static inline unsigned long nonhyp_mask(int i)
 {
 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
Signature · 1 line
2.53.0
author reviewer
#41 lore raw

PATCH v3 25/27 KVM: s390: arm64: Implement vCPU IOCTLs

In reply to Steffen Eiden

Implement all vCPU IOCTLs.

---
 arch/s390/kvm/arm64/arm.c   | 360 ++++++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/guest.c |  71 ++++++-
 arch/s390/kvm/arm64/guest.h |   5 +
 arch/s390/kvm/arm64/reset.c |  57 ++++++
 arch/s390/kvm/arm64/reset.h |  11 ++
 5 files changed, 502 insertions(+), 2 deletions(-)
 create mode 100644 arch/s390/kvm/arm64/reset.c
 create mode 100644 arch/s390/kvm/arm64/reset.h
arch/s390/kvm/arm64/arm.c +360 -0
index 79ce6570fa03..18eca2261e0d 100644
@@ -7,10 +7,18 @@
 #include <linux/kvm.h>
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
+#include <linux/fpu.h>
+
+#include <asm/access-regs.h>
+#include <asm/kvm_emulate.h>
+
+#include <kvm/arm64/handle_exit.h>
+#include <kvm/arm64/kvm_emulate.h>
 
 #include <gmap.h>
 
 #include "arm.h"
+#include "guest.h"
 #include "reset.h"
 
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
@@ -172,6 +180,22 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
 {
 }
 
+void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
+{
+	save_access_regs(&vcpu->arch.host_acrs[0]);
+	vcpu->cpu = cpu;
+
+	lasrm(&vcpu->arch.save_area);
+}
+
+void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
+{
+	stiasrm(&vcpu->arch.save_area);
+
+	vcpu->cpu = -1;
+	restore_access_regs(&vcpu->arch.host_acrs[0]);
+}
+
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
 				    struct kvm_mp_state *mp_state)
 {
@@ -195,12 +219,348 @@ unsigned long system_supported_vcpu_features(void)
 	return KVM_VCPU_VALID_FEATURES;
 }
 
+bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return vcpu_mode_priv(vcpu);
+}
+
+int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm_vcpu_initialized(vcpu))
+		return -ENOEXEC;
+
+	if (!kvm_arm_vcpu_is_finalized(vcpu))
+		return -EPERM;
+
+	if (likely(READ_ONCE(vcpu->pid)))
+		return 0;
+
+
+	mutex_lock(&kvm->arch.config_lock);
+	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
+	mutex_unlock(&kvm->arch.config_lock);
+
+	return 0;
+}
+
+/**
+ * check_vcpu_requests - check and handle pending vCPU requests
+ * @vcpu:	the VCPU pointer
+ *
+ * Return: 1 if we should enter the guest
+ *	   0 if we should exit to userspace
+ *	   < 0 if we should exit to userspace, where the return value indicates
+ *	   an error
+ */
+static int check_vcpu_requests(struct kvm_vcpu *vcpu)
+{
+	if (kvm_request_pending(vcpu)) {
+		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
+			kvm_reset_vcpu(vcpu);
+		/*
+		 * Clear IRQ_PENDING requests that were made to guarantee
+		 * that a VCPU sees new virtual interrupts.
+		 */
+		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
+	}
+
+	return 1;
+}
+
+static int kvm_vcpu_initialize(struct kvm_vcpu *vcpu,
+				 const struct kvm_vcpu_init *init)
+{
+	unsigned long features = init->features[0];
+	struct kvm *kvm = vcpu->kvm;
+	int ret = -EINVAL;
+
+	mutex_lock(&kvm->arch.config_lock);
+
+	if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
+	    kvm_vcpu_init_changed(vcpu, init))
+		goto out_unlock;
+
+	bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
+
+	kvm_reset_vcpu(vcpu);
+
+	set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags);
+	vcpu_set_flag(vcpu, VCPU_INITIALIZED);
+
+	ret = 0;
+out_unlock:
+	mutex_unlock(&kvm->arch.config_lock);
+	return ret;
+}
+
+static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
+			       const struct kvm_vcpu_init *init)
+{
+	int ret;
+
+	if (init->target != KVM_ARM_TARGET_GENERIC_V8)
+		return -EINVAL;
+
+	ret = kvm_vcpu_init_check_features(vcpu, init);
+	if (ret)
+		return ret;
+
+	if (!kvm_vcpu_initialized(vcpu))
+		return kvm_vcpu_initialize(vcpu, init);
+
+	if (kvm_vcpu_init_changed(vcpu, init))
+		return -EINVAL;
+
+	kvm_reset_vcpu(vcpu);
+
+	return 0;
+}
+
+static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
+					 struct kvm_vcpu_init *init)
+{
+	struct kvm_sae_save_area *save_area = &vcpu->arch.save_area;
+	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+	int ret;
+
+	sae_block->save_area = virt_to_phys(save_area);
+	save_area->sdo = virt_to_phys(sae_block);
+
+	ret = kvm_vcpu_set_target(vcpu, init);
+	if (ret)
+		return ret;
+
+	spin_lock(&vcpu->arch.mp_state_lock);
+	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
+	spin_unlock(&vcpu->arch.mp_state_lock);
+
+	return 0;
+}
+
 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 			  bool line_status)
 {
 	return 0;
 }
 
+static void adjust_pc(struct kvm_vcpu *vcpu)
+{
+	if (vcpu_get_flag(vcpu, INCREMENT_PC)) {
+		kvm_skip_instr(vcpu);
+		vcpu_clear_flag(vcpu, INCREMENT_PC);
+	}
+}
+
+static void arm_vcpu_run(struct kvm_vcpu *vcpu)
+{
+	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+
+	adjust_pc(vcpu);
+
+	local_irq_disable();
+	guest_enter_irqoff();
+	local_irq_enable();
+
+	sae_block->icptr = 0;
+
+	sae64a(sae_block);
+
+	local_irq_disable();
+	guest_exit_irqoff();
+	local_irq_enable();
+}
+
+/** kvm_arch_vcpu_ioctl_run() - run arm64 vCPU
+ *
+ * Execute arm64 guest instructions using SAE.
+ *
+ * Returns:
+ * 1 enter the guest (should not be observed by userspace)
+ * 0 exit to userspace
+ * < 0 exit to userspace, where the return value indicates n error
+ *
+ *
+ */
+int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
+{
+	DECLARE_KERNEL_FPU_ONSTACK32(fpu_save);
+	struct kvm_run *kvm_run = vcpu->run;
+	u8 icptr;
+	int ret;
+
+	if (kvm_run->exit_reason == KVM_EXIT_MMIO) {
+		ret = kvm_handle_mmio_return(vcpu);
+		if (ret <= 0)
+			return ret;
+	}
+
+	vcpu_load(vcpu);
+
+	kernel_fpu_begin(&fpu_save, KERNEL_FPC | KERNEL_VXR);
+	load_vx_regs((vcpu->arch.ctxt.vregs));
+
+	if (!vcpu->wants_to_run) {
+		ret = -EINTR;
+		goto out;
+	}
+
+	kvm_sigset_activate(vcpu);
+
+	might_fault();
+
+	ret = 1;
+	do {
+		if (signal_pending(current)) {
+			kvm_run->exit_reason = KVM_EXIT_INTR;
+			ret = -EINTR;
+			continue;
+		}
+
+		if (need_resched())
+			schedule();
+
+		if (ret > 0)
+			ret = check_vcpu_requests(vcpu);
+
+		vcpu->arch.sae_block.icptr = 0;
+
+		arm_vcpu_run(vcpu);
+
+		icptr = vcpu->arch.sae_block.icptr;
+		switch (icptr) {
+		case SAE_ICPTR_SPURIOUS:
+			break;
+		case SAE_ICPTR_VALIDITY:
+			WARN_ONCE(true, "SAE: validity intercept. vir: 0x%04x",
+				  vcpu->arch.sae_block.vir);
+			ret = -EINVAL;
+			break;
+		case SAE_ICPTR_SYNCHRONOUS_EXCEPTION:
+			ret = handle_trap_exceptions(vcpu);
+			break;
+		default:
+			WARN_ONCE(true, "SAE: unknown interception reason 0x%02x", icptr);
+			ret = -EINVAL;
+		}
+	} while (ret > 0);
+
+	kvm_sigset_deactivate(vcpu);
+out:
+	if (unlikely(vcpu_get_flag(vcpu, INCREMENT_PC)))
+		adjust_pc(vcpu);
+
+	save_vx_regs(vcpu->arch.ctxt.vregs);
+	kernel_fpu_end(&fpu_save, KERNEL_FPC | KERNEL_VXR);
+	vcpu_put(vcpu);
+
+	return ret;
+}
+
+long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
+{
+	struct kvm_vcpu *vcpu = filp->private_data;
+	void __user *argp = (void __user *)arg;
+	struct kvm_device_attr attr;
+	int ret;
+
+	switch (ioctl) {
+	case KVM_ARM_VCPU_INIT: {
+		struct kvm_vcpu_init init;
+
+		ret = -EFAULT;
+		if (copy_from_user(&init, argp, sizeof(init)))
+			break;
+
+		ret = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
+		break;
+	}
+	case KVM_SET_ONE_REG:
+	case KVM_GET_ONE_REG: {
+		struct kvm_one_reg reg;
+
+		ret = -ENOEXEC;
+		if (unlikely(!kvm_vcpu_initialized(vcpu)))
+			break;
+
+		ret = -EFAULT;
+		if (copy_from_user(&reg, argp, sizeof(reg)))
+			break;
+
+		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
+			kvm_reset_vcpu(vcpu);
+
+		if (ioctl == KVM_SET_ONE_REG)
+			ret = kvm_arm_set_reg(vcpu, &reg);
+		else
+			ret = kvm_arm_get_reg(vcpu, &reg);
+		break;
+	}
+	case KVM_GET_REG_LIST: {
+		struct kvm_reg_list __user *user_list = argp;
+		struct kvm_reg_list reg_list;
+		unsigned int n;
+
+		ret = -ENOEXEC;
+		if (unlikely(!kvm_vcpu_initialized(vcpu)))
+			break;
+		ret = -EPERM;
+		if (!kvm_arm_vcpu_is_finalized(vcpu))
+			break;
+		ret = -EFAULT;
+		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
+			break;
+		n = reg_list.n;
+		reg_list.n = kvm_arm_num_regs(vcpu);
+		if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
+			break;
+		ret = -E2BIG;
+		if (n < reg_list.n)
+			break;
+		ret = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
+		break;
+	}
+	case KVM_ARM_VCPU_FINALIZE: {
+		int what;
+
+		if (!kvm_vcpu_initialized(vcpu))
+			return -ENOEXEC;
+
+		if (get_user(what, (const int __user *)argp))
+			return -EFAULT;
+
+		ret = kvm_arm_vcpu_finalize(vcpu, what);
+		break;
+	}
+	case KVM_SET_DEVICE_ATTR: {
+		ret = -EFAULT;
+		if (copy_from_user(&attr, argp, sizeof(attr)))
+			break;
+		ret = kvm_arm_vcpu_set_attr(vcpu, &attr);
+		break;
+	}
+	case KVM_GET_DEVICE_ATTR: {
+		ret = -EFAULT;
+		if (copy_from_user(&attr, argp, sizeof(attr)))
+			break;
+		ret = kvm_arm_vcpu_get_attr(vcpu, &attr);
+		break;
+	}
+	case KVM_HAS_DEVICE_ATTR: {
+		ret = -EFAULT;
+		if (copy_from_user(&attr, argp, sizeof(attr)))
+			break;
+		ret = kvm_arm_vcpu_has_attr(vcpu, &attr);
+		break;
+	}
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
 			       struct kvm_dirty_log *log)
 {
arch/s390/kvm/arm64/guest.c +69 -2

89 lines not on this page · raw message

arch/s390/kvm/arm64/guest.h +5 -0
index db635d513c2c..847489fb81be 100644
@@ -6,5 +6,10 @@
 #include <kvm/arm64/guest.h>
 
 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
+int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
+int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
+int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
+int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
+int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
 
 #endif /* KVM_ARM_GUEST_H */
arch/s390/kvm/arm64/reset.c new file +57 -0

57 lines not on this page · raw message

arch/s390/kvm/arm64/reset.h new file +11 -0
new file mode 100644
index 000000000000..a5c5304e47bc
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef KVM_ARM_RESET_H
+#define KVM_ARM_RESET_H
+
+#include <linux/kvm_host.h>
+
+bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
+void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
+int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
+
+#endif /* KVM_ARM_RESET_H */
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH v3 02/27] KVM, vfio: remove symbol_get(kvm_get_kvm_safe) from vfio

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Build failure in arch/x86/kvm/svm/sev.c due to missing `users_count` struct field update. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Build failure in arch/x86/kvm/svm/sev.c due to missing users_count struct field update. --

commit d29c547487163d60419f67e02b219cd59361c39b
Author: Paolo Bonzini <[email protected]>

KVM, vfio: remove symbol_get(kvm_get_kvm_safe) from vfio

This removes the deprecated symbol_get usage for kvm_get_kvm_safe in VFIO by making the function inline. As part of this, the function is moved to kvm_types.h and the users_count field in struct kvm is moved into a new kvm_refcount struct.

[ ... ]

Quoted 15 lines
include/linux/kvm_host.h +3 -0
index 4c14aee1fb063..8a068cbf12ff2 100644
@@ -768,6 +768,9 @@ struct kvm_memslots {
 };
 
 struct kvm {
+	/* Must be the first field, see function definitions in kvm_types.h.  */
+	struct kvm_refcount rc;
+
 #ifdef KVM_HAVE_MMU_RWLOCK
 	rwlock_t mmu_lock;
 #else

include/linux/kvm_host.h struct kvm { · line 834

Quoted 4 lines
include/linux/kvm_host.h +0 -1
@@ -831,7 +834,6 @@ struct kvm {
 	struct list_head ioeventfds;
 	struct kvm_vm_stat stat;
 	struct kvm_arch arch;
-	refcount_t users_count;

[Severity: Low]
Does removing users_count from struct kvm break the build for x86 with AMD SEV?

Looking at kvm_lockdep_assert_sev_lock_held() in arch/x86/kvm/svm/sev.c, it still references this directly:

arch/x86/kvm/svm/sev.c:kvm_lockdep_assert_sev_lock_held() {
    ...
	/*
	 * Querying SEV+ support is safe if there are no other references, i.e.
	 * if concurrent initialization of SEV+ is impossible.
	 */
	if (!refcount_read(&kvm->users_count))
		return;
    ...
}

Should this reference be updated to use kvm->rc.users_count like the other files modified in this patch?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#2 lore raw

Re: [PATCH v3 01/27] VFIO: take reference to the KVM module

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Unconditional KVM reference overwrite and subsequent premature release in the VFIO cdev bind error path. --

commit 0e9c12cfda2c8eedbfdfe11e2a9a3b8ef09209f4
Author: Paolo Bonzini <[email protected]>

VFIO: take reference to the KVM module

VFIO implicitly took a reference to the KVM module via symbol_get and symbol_put. In preparation for removing these, this patch stores a pointer to the KVM module and uses module_get()/module_put() to keep KVM alive.

Quoted 4 lines
drivers/vfio/vfio_main.c +0 -0
index 6222376ab6ab5..78b9cad348b19 100644

[ ... ]

drivers/vfio/vfio_main.c void vfio_device_put_kvm(struct vfio_device *device) · line 502

Quoted 8 lines
drivers/vfio/vfio_main.c +2 -0
@@ -490,6 +496,8 @@ void vfio_device_put_kvm(struct vfio_device *device)
 
 clear:
 	device->kvm = NULL;
+	module_put(device->kvm_module);
+	device->kvm_module = NULL;
 }
 #endif

[Severity: High]
This isn't a bug introduced by this patch, but does this change exacerbate a bug in the cdev bind error path by adding a module_put() underflow?

In vfio_df_ioctl_bind_iommufd(), if a second bind attempt is made on an already-open device via a new file descriptor, the code unconditionally calls vfio_df_get_kvm_safe() before checking device->open_count:

	vfio_df_get_kvm_safe(df);

	ret = vfio_df_open(df);
	if (ret)
		goto out_put_kvm;

If the new descriptor's kvm is populated, vfio_device_get_kvm_safe() blindly overwrites device->kvm and device->kvm_module, leaking the prior references held by the first valid bind.

If the new descriptor's kvm is NULL, vfio_device_get_kvm_safe() returns early, leaving device->kvm still pointing to the first descriptor's KVM.

Then vfio_df_open() predictably fails with -EINVAL because device->open_count != 0, and the error path jumps to out_put_kvm:

out_put_kvm:

	vfio_device_put_kvm(device);

This drops the KVM references and clears device->kvm to NULL. This destroys the KVM association for the initially bound, actively running process, and could lead to a use-after-free of KVM structures.

By adding module_put(device->kvm_module) to vfio_device_put_kvm(), does this also cause a module reference underflow for the actively running process?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#8 lore raw

Re: [PATCH v3 04/27] uapi: KVM: Provide arm64 UAPI for other host architectures

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider: - [Critical] Including both the host and guest architecture KVM headers in the same file results in fatal…

Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider:
- [Critical] Including both the host and guest architecture KVM headers in the same file results in fatal compiler errors due to redefinition of standard structures.
- [Critical] struct kvm_run memory layout is corrupted for arm64 translation units running on s390 due to the absence of the __KVM_S390 macro.
- [High] The newly introduced include/uapi/arch/arm64/linux/kvm.h and the existing generic include/uapi/linux/kvm.h use the identical include guard __LINUX_KVM_H.
- [High] Unconditional use of the __uint128_t compiler extension in the fallback definition of user_fpsimd_state_arm64 breaks compilation for 32-bit targets.
- [High] Kbuild logic restricts cross-architecture header exports exclusively to the s390 architecture, breaking unified cross-platform compilation.
- [Medium] The patch exports headers into a new top-level arch/ directory within the standard system include path.
- [Medium] Missing vital architectural constants due to the exclusion of <asm/ptrace.h> on non-aarch64 hosts.
- [Medium] Missing $(srctree)/ prefix in Makefile pattern rule prerequisite can break out-of-tree builds.
- [Low] Unnecessary addition of the raw generated include directory to the global LINUXINCLUDE path.
- [Low] Mismatched header guard name in comment.
--

commit ca2d355250085514c5eaba71ad083aa58746bc53
Author: Steffen Eiden <[email protected]>

uapi: KVM: Provide arm64 UAPI for other host architectures

Enable the ARM64 userspace API to be used on non-arm64 host architectures, with initial support for s390. This introduces conditional aliases to achieve architecture independence and updates the build system to export arm64 architecture headers for s390.

Makefile LINUXINCLUDE := \ · line 591

Quoted 9 lines
Makefile +1 -0
index f056c921ea9cd..c34f823a9db05 100644
@@ -588,6 +588,7 @@ LINUXINCLUDE    := \
 		-I$(objtree)/arch/$(SRCARCH)/include/generated \
 		-I$(srctree)/include \
 		-I$(objtree)/include \
+		-I$(objtree)/include/generated \

[Severity: Low]
Is this addition necessary? The kernel's USERINCLUDE variable already includes -I$(objtree)/include/generated/uapi, which is where the arm64 generic headers are correctly generated. Adding the parent directory might improperly expose internal generated headers to the global search path.

Quoted 4 lines
arch/arm64/include/uapi/asm/kvm.h +0 -0
index 1c13bfa2d38aa..75cf5aff940e1 100644

[ ... ]

arch/arm64/include/uapi/asm/kvm.h line 45

Quoted 5 lines
arch/arm64/include/uapi/asm/kvm.h +2 -2
+#ifdef __aarch64__
 #include <asm/ptrace.h>
-typedef struct user_pt_regs user_pt_regs_arm64;
-typedef struct user_fpsimd_state user_fpsimd_state_arm64;
+#else

[Severity: Medium]
By excluding <asm/ptrace.h> on non-aarch64 hosts, are we missing vital architectural constants (such as PSR_MODE_EL1h) needed to correctly initialize the pstate variable in user_pt_regs_arm64? It looks like applications might be forced to manually redefine these core arm64 architectural constants.

arch/arm64/include/uapi/asm/kvm.h line 53

Quoted 8 lines
arch/arm64/include/uapi/asm/kvm.h +8 -0
+typedef struct {
+	__u64		regs[31];
+	__u64		sp;
+	__u64		pc;
+	__u64		pstate;
+} user_pt_regs_arm64;
+typedef struct {
+	__uint128_t	vregs[32];

[Severity: High]
Does using the __uint128_t compiler extension here break compilation for 32-bit userspace targets? Since this type is not universally supported on 32-bit compilers (like 32-bit x86 or 32-bit s390 compat environments), would it be safer to use a standard integer type like a __u64 array to guarantee ABI compatibility?

Quoted 5 lines
arch/arm64/include/uapi/asm/kvm.h +5 -0
+	__u32		fpsr;
+	__u32		fpcr;
+	__u32		__reserved[2];
+} __attribute__((aligned(16))) user_fpsimd_state_arm64;
+#endif /* __aarch64__ */

[ ... ]

arch/arm64/include/uapi/asm/kvm.h line 75

Quoted 12 lines

struct kvm_regs {

arch/arm64/include/uapi/asm/kvm.h +2 -2
-	struct user_pt_regs regs;	/* sp = sp_el0 */
+	user_pt_regs_arm64 regs;	/* sp = sp_el0 */
 
 	__u64	sp_el1;
 	__u64	elr_el1;
 
 	__u64	spsr[KVM_NR_SPSR];
 
-	struct user_fpsimd_state fp_regs;
+	user_fpsimd_state_arm64 fp_regs;
 };

[Severity: Critical]
If an application includes both the host's <asm/kvm.h> (e.g., s390) and the guest's <arch/arm64/asm/kvm.h> to interact with both architectures, will this result in compilation errors? Both files unconditionally define standard architectural KVM structures like struct kvm_regs, struct kvm_sregs, and struct kvm_fpu.

include/uapi/Kbuild ifeq ($(wildcard $(objtree)/arch/$(SRCARCH)/include/generated/uapi/asm/kvm_para. · line 18

Quoted 12 lines
include/uapi/Kbuild +4 -0
index 61ee6e59c930b..452bf9d5c9c0f 100644
@@ -12,3 +12,7 @@ ifeq ($(wildcard $(objtree)/arch/$(SRCARCH)/include/generated/uapi/asm/kvm_para.
 no-export-headers += linux/kvm_para.h
 endif
 endif
+
+ifneq ($(ARCH),s390)
+no-export-headers += arch/%
+endif

[Severity: High]
Does this logic restrict cross-architecture header exports exclusively to the s390 architecture? It appears this will prevent include/uapi/arch/arm64/linux/kvm.h from being exported during make headers_install on native arm64 systems, meaning userspace applications cannot unconditionally include <arch/arm64/linux/kvm.h> when targeting arm64 VMs.

include/uapi/arch/arm64/linux/kvm.h line 3

Quoted 9 lines
include/uapi/arch/arm64/linux/kvm.h new file +3 -0
new file mode 100644
index 0000000000000..21528a146b4d9
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef __LINUX_KVM_H
+#define __LINUX_KVM_H

[Severity: High]
Is it safe to use __LINUX_KVM_H as the include guard here? The existing generic include/uapi/linux/kvm.h uses the exact same guard. If the generic or host KVM header is included first, the ARM64-specific definitions from this file will be skipped, preventing userspace applications from simultaneously accessing host and guest KVM ioctls and types.

[Severity: Medium]
Does exporting headers into a new top-level arch/ directory violate Linux UAPI conventions? By placing headers in include/uapi/arch/arm64/, make headers_install will create an arch/ directory in the global system include directory (e.g., /usr/include/arch/). Standard convention dictates that exported headers belong in linux/, asm/, or asm-generic/.

Quoted 11 lines
include/uapi/arch/arm64/linux/kvm.h new file +5 -0
+
+#include <arch/arm64/asm/kvm.h>
+#include <linux/kvm-generic.h>
+
+#endif /*__LINUX_KVM_H */
include/uapi/linux/kvm-generic.h new file +0 -0
new file mode 100644
index 0000000000000..a9c757029808b

[ ... ]

Quoted 3 lines
include/uapi/linux/kvm-generic.h new file +3 -0
+struct kvm_run {
+	/* in */
+	__u8 request_interrupt_window;

[ ... ]

include/uapi/linux/kvm-generic.h

Quoted 8 lines
include/uapi/linux/kvm-generic.h new file +8 -0
+	__u64 cr8;
+	__u64 apic_base;
+
+#ifdef __KVM_S390
+	/* the processor status word for s390 */
+	__u64 psw_mask; /* psw upper half */
+	__u64 psw_addr; /* psw lower half */
+#endif

[Severity: Critical]
Could this cause memory layout corruption for arm64 translation units running on s390? If a developer uses a separate translation unit for arm64 logic and includes only <arch/arm64/linux/kvm.h>, the host's <asm/kvm.h> is not included and __KVM_S390 remains undefined.

This removes 16 bytes from the struct, shifting the offsets of all subsequent union fields and causing a silent ABI mismatch with the host kernel.

[ ... ]

include/uapi/linux/kvm-generic.h

Quoted 8 lines
include/uapi/linux/kvm-generic.h new file +8 -0
+struct kvm_pre_fault_memory {
+	__u64 gpa;
+	__u64 size;
+	__u64 flags;
+	__u64 padding[5];
+};
+
+#endif /* __LINUX_KVM_GENERIC_H */

[Severity: Low]
This isn't a bug, but there is a typo here. The #endif comment refers to __LINUX_KVM_GENERIC_H, while the actual header guard defined at the top of the file is __LINUX_GENERIC_KVM_H.

Quoted 4 lines
scripts/Makefile.asm-headers +0 -0
index 8a4856e741803..86d4d7173fcdf 100644

[ ... ]

scripts/Makefile.asm-headers quiet_cmd_systbl = SYSTBL $@ · line 87

Quoted 4 lines
scripts/Makefile.asm-headers +4 -0
+# Let architectures define architecture specific headers that are shared with
+# other architectures to enable single-system cross architecture communication
+$(shared-uapi-y): $(obj)/%.h: $(shared-uapi-src)/%.h FORCE
+	$(call if_changed,share_hdr)

[Severity: Medium]
Can omitting the $(srctree)/ prefix in this pattern rule prerequisite break out-of-tree builds? Unlike other rules that properly use $(srctree)/, relying entirely on VPATH resolution violates Kbuild best practices and can lead to missing prerequisites during out-of-tree (O=...) builds.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#6 lore raw

Re: [PATCH v3 03/27] KVM, vfio: remove symbol_get(kvm_put_kvm) from vfio

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Unlocking kvm->lock after calling kvm_put_kvm() results in a Use-After-Free if it was the last reference. --

commit 79e5304c707c4ed618be6e55a950250edf9a7337
Author: Paolo Bonzini <[email protected]>

KVM, vfio: remove symbol_get(kvm_put_kvm) from vfio

This patch replaces the symbol_get() approach for KVM/VFIO interaction by storing the address of kvm_destroy_vm in "struct kvm" to handle kvm_put_kvm calls. This allows removing the cross-module dependencies and directs kvm_put_kvm to the correct architecture implementation.

[ ... ]

Quoted 4 lines
include/linux/kvm_types.h +0 -0
index 4cb68c71a13cd..8a9be07cb8dd6 100644

[ ... ]

include/linux/kvm_types.h static inline bool kvm_get_kvm_safe(struct kvm *kvm) · line 171

Quoted 10 lines
include/linux/kvm_types.h +6 -0
@@ -162,6 +163,13 @@ static inline bool kvm_get_kvm_safe(struct kvm *kvm)
 	return refcount_inc_not_zero(&rc->users_count);
 }
 
+static inline void kvm_put_kvm(struct kvm *kvm)
+{
+	struct kvm_refcount *rc = (struct kvm_refcount *)kvm;
+	if (refcount_dec_and_test(&rc->users_count))
+		rc->destroy(kvm);
+}

[Severity: Critical]
This is a pre-existing issue, but can calling kvm_put_kvm() here lead to a use-after-free in vfio_ap_mdev_unset_kvm()?

Looking at vfio_ap_mdev_unset_kvm() in drivers/s390/crypto/vfio_ap_ops.c, it calls get_update_locks_for_kvm(kvm) which acquires mutex_lock(&kvm->lock).

It then calls kvm_put_kvm(kvm) which decrements the reference count and can trigger kvm_destroy_vm(), completely freeing the kvm struct memory:

drivers/s390/crypto/vfio_ap_ops.c:vfio_ap_mdev_unset_kvm() {
    ...
		get_update_locks_for_kvm(kvm);

		kvm_arch_crypto_clear_masks(kvm);
		vfio_ap_mdev_reset_queues(matrix_mdev);
		kvm_put_kvm(kvm);
		matrix_mdev->kvm = NULL;

		release_update_locks_for_kvm(kvm);
    ...
}

If the KVM file descriptor has already been closed by userspace, this could be the final reference to the KVM object. When release_update_locks_for_kvm(kvm) is subsequently called, it will attempt to execute mutex_unlock(&kvm->lock) on the freed kvm pointer.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#10 lore raw

Re: [PATCH v3 05/27] arm64: Extract sysreg definitions

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Dropping `kernel-hwcap.h` from Kbuild's `targets` array causes it to be unconditionally rebuilt…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Dropping kernel-hwcap.h from Kbuild's targets array causes it to be unconditionally rebuilt on every make invocation, breaking incremental builds. --

commit 56233effa49d4a8119244cffc6426b8b9f08a322
Author: Nina Schoetterl-Glausch <[email protected]>

arm64: Extract sysreg definitions

This commit splits all definitions that can be used by non-native architectures into a separate file sysreg-defs.h. The generated sysreg definitions are placed into sysreg-defs-gen.h, and the tools Makefiles are updated to accommodate this change.

Quoted 4 lines
arch/arm64/tools/Makefile +0 -0
index a94b3d9caad64..4147187b66273 100644

[ ... ]

arch/arm64/tools/Makefile line 11

Quoted 8 lines
arch/arm64/tools/Makefile +3 -2
-kapisyshdr-y := cpucap-defs.h kernel-hwcap.h sysreg-defs.h
+include $(srctree)/arch/arm64/tools/Makefile.sysreg
 
+kapisyshdr-y := cpucap-defs.h kernel-hwcap.h
 kapi-hdrs-y := $(addprefix $(kapi)/, $(kapisyshdr-y))
 
-targets += $(addprefix ../../../, $(kapi-hdrs-y))
+targets += $(addprefix ../../../, $(kapi)/cpucap-defs.h)

[Severity: Medium]
Does removing kernel-hwcap.h from the targets array break incremental builds?

Kbuild's if_changed macro requires all generated targets to be listed in the targets variable so that their corresponding .cmd files are included to track command lines and prerequisites.

Without this, won't Kbuild assume the target must be rebuilt every time, updating the timestamp of kernel-hwcap.h on every build and triggering unnecessary recompilation of all files that depend on it?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#12 lore raw

Re: [PATCH v3 06/27] arm64: Provide arm64 API for non-native architectures

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Moving the large `esr_get_class_string` function to a `static inline` definition in a widely…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Moving the large esr_get_class_string function to a static inline definition in a widely included header causes unnecessary kernel text bloat. --

commit f19382b1cbd1896b8ea8e1b5b65a1f8581246812
Author: Steffen Eiden <[email protected]>

arm64: Provide arm64 API for non-native architectures

This commit introduces a new directory include/arch/arm64/asm to store architecture-agnostic headers for arm64 implementors. As part of this change, the esr_get_class_string() function and its associated string mappings are moved from traps.c to a static inline function in esr.h.

Quoted 7 lines
include/arch/arm64/asm/esr.h renamed from arch/arm64/include/asm/esr.h +0 -0
similarity index 88%
rename from arch/arm64/include/asm/esr.h
rename to include/arch/arm64/asm/esr.h
index 7e86d400864e0..a7421cc966f98 100644

[ ... ]

include/arch/arm64/asm/esr.h static inline bool esr_iss_is_eretab(unsigned long esr) · line 549

Quoted 10 lines
include/arch/arm64/asm/esr.h renamed from arch/arm64/include/asm/esr.h +5 -1
@@ -541,7 +542,56 @@ static inline bool esr_iss_is_eretab(unsigned long esr)
 	return esr & ESR_ELx_ERET_ISS_ERETA;
 }
 
-const char *esr_get_class_string(unsigned long esr);
+static inline const char *esr_get_class_string(unsigned long esr)
+{
+	switch (ESR_ELx_EC(esr)) {
+	case ESR_ELx_EC_UNKNOWN:	return "Unknown/Uncategorized";
+	case ESR_ELx_EC_WFx:		return "WFI/WFE";

[Severity: Low]
Does moving this 45-case switch statement to a static inline function in a widely included header cause unnecessary kernel text bloat?

Previously, this was implemented as a simple string array in a single compilation unit (traps.c). By making it a static inline function, the compiled switch statement and any compiler-generated jump tables or .rodata string mappings might be unnecessarily duplicated in every compilation unit that calls it.

While the performance impact is likely negligible since it is primarily called in slow-path error handling locations, it might be better to keep the implementation out of the header.

Quoted 2 lines
include/arch/arm64/asm/esr.h renamed from arch/arm64/include/asm/esr.h +2 -0
+	case ESR_ELx_EC_CP15_32:	return "CP15 MCR/MRC";
+	case ESR_ELx_EC_CP15_64:	return "CP15 MCRR/MRRC";

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#14 lore raw

Re: [PATCH v3 07/27] KVM: arm64: Provide arm64 KVM API for non-native architectures

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] The refactoring of `kvm_arm.h` violates the design convention explicitly stated in the commit…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] The refactoring of kvm_arm.h violates the design convention explicitly stated in the commit message.
- [Low] system_supported_vcpu_features() is made globally visible without a prototype declaration.
- [Low] Newly extracted headers are not self-contained and lack necessary forward declarations and #include directives. --

commit 106dc17001f0c5affae224d8814a51a51af0225e
Author: Steffen Eiden <[email protected]>

KVM: arm64: Provide arm64 KVM API for non-native architectures

This patch moves KVM-related headers to include/kvm/arm64/ to decouple them from the arm64 architecture directory. This allows non-native hosts to consume the arm64 KVM infrastructure without duplicating code.

Quoted 5 lines

The KVM-related headers are moved to include/kvm/arm64/, decoupling them from the arm64 architecture directory. The design convention is that architecture-specific headers under <arch>/include/asm/ include from this shared location, allowing non-native hosts to consume the arm64 KVM infrastructure without duplicating code.

[Severity: Medium]
The commit message states "architecture-specific headers under <arch>/include/asm/ include from this shared location". Does moving kvm_arm.h entirely and updating all the source files to include <kvm/arm64/kvm_arm.h> directly bypass this convention?

Would it be more consistent to leave a wrapper header in arch/arm64/include/asm/kvm_arm.h as was done for kvm_mmu.h and kvm_host.h?

Quoted 4 lines
arch/arm64/kvm/arm.c +0 -0
index 9453321ef8c67..04a4ef1c7f062 100644

[ ... ]

arch/arm64/kvm/arm.c int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, · line 1549

Quoted 9 lines
arch/arm64/kvm/arm.c +1 -1
@@ -1541,7 +1543,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 	return -EINVAL;
 }
 
-static unsigned long system_supported_vcpu_features(void)
+unsigned long system_supported_vcpu_features(void)
 {
 	unsigned long features = KVM_VCPU_VALID_FEATURES;
 

[Severity: Low]
The function system_supported_vcpu_features() is now globally visible. Should a prototype be added to a header file to avoid missing prototype compiler warnings?

Quoted 6 lines
include/kvm/arm64/guest.h new file +0 -0
new file mode 100644
index 0000000000000..fa67d992e8fd2

include/kvm/arm64/guest.h line 10

Quoted 10 lines
include/kvm/arm64/guest.h new file +10 -0
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __KVM_ARM64_GUEST_H__
+#define __KVM_ARM64_GUEST_H__
+
+/* Implemented by virt/kvm/arm64/guest.c */
+unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
+int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
+
+#endif /* __KVM_ARM64_GUEST_H__ */

[Severity: Low]
This new header uses types like u64, __user, and struct kvm_vcpu. Should it include <linux/types.h>, <linux/compiler.h>, and forward-declare struct kvm_vcpu so it is fully self-contained?

Quoted 15 lines
include/kvm/arm64/kvm_mmu.h new file +9 -0
new file mode 100644
index 0000000000000..91607105eaf67
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef KVM_ARM64_KVM_MMU_H__
+#define KVM_ARM64_KVM_MMU_H__
+
+/*
+ * We currently support using a VM-specified IPA size. For backward
+ * compatibility, the default IPA size is fixed to 40bits.
+ */

include/kvm/arm64/kvm_mmu.h line 19

Quoted 10 lines
include/kvm/arm64/kvm_mmu.h new file +10 -0
+#define KVM_PHYS_SHIFT	(40)
+
+/*
+ * We are not in the kvm->srcu critical section most of the time, so we take
+ * the SRCU read lock here. Since we copy the data from the user page, we
+ * can immediately drop the lock again.
+ */
+static inline int kvm_read_guest_lock(struct kvm *kvm,
+				      gpa_t gpa, void *data, unsigned long len)
+{

[Severity: Low]
This header uses gpa_t, phys_addr_t, size_t, bool, and srcu functions. Does it need to include <linux/types.h>, <linux/srcu.h>, and other relevant headers to ensure it compiles independently?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#17 lore raw

Re: [PATCH v3 09/27] KVM: arm64: Share kvm_emulate definitions

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The forward declaration of `kvm_vcpu_get_esr` in the shared header is missing the `inline` keyword.…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The forward declaration of kvm_vcpu_get_esr in the shared header is missing the inline keyword. --

commit cd45780d22b912a0d1f22d6af7c40c20667b35da
Author: Steffen Eiden <[email protected]>

KVM: arm64: Share kvm_emulate definitions

This commit moves functions and definitions useful for emulating arm64 instructions from arch/arm64 to include/kvm/arm64 so they can be shared.

Quoted 9 lines
include/kvm/arm64/kvm_emulate.h new file +3 -0
new file mode 100644
index 0000000000000..25322b95af21b
@@ -0,0 +1,268 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef KVM_ARM64_KVM_EMULATE_H
+#define KVM_ARM64_KVM_EMULATE_H

include/kvm/arm64/kvm_emulate.h line 13

Quoted 10 lines
include/kvm/arm64/kvm_emulate.h new file +10 -0
+
+#include <asm/esr.h>
+#include <asm/pstate.h>
+#include <asm/sysreg-defs.h>
+
+static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu);
+static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu);
+static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu);
+static inline bool kvm_vcpu_trap_is_permission_fault(const struct kvm_vcpu *vcpu);
+static u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu);

[Severity: Low]
This isn't a bug, but should this forward declaration include the "inline" keyword? The surrounding forward declarations use "static inline" or "static __always_inline" to match their definitions, but this one is just "static u64" which creates a slight stylistic inconsistency.

Quoted 1 line
include/kvm/arm64/kvm_emulate.h new file +1 -0
+static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu);

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#19 lore raw

Re: [PATCH v3 10/27] KVM: arm64: Make some arm64 KVM code shareable

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] Missing `<linux/uaccess.h>` in `virt/kvm/arm64/guest.c` causes implicit declarations - [Low] The…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] Missing <linux/uaccess.h> in virt/kvm/arm64/guest.c causes implicit declarations
- [Low] The header include/kvm/arm64/handle_exit.h is not self-contained
--

commit de2ec796cee358898f6d5a8336e76db1e095f6a8
Author: Steffen Eiden <[email protected]>

KVM: arm64: Make some arm64 KVM code shareable

Support sharing architecture-agnostic KVM-arm64 code by introducing a new directory virt/kvm/arm64. This also modifies core_reg_addr to use functions instead of direct field accesses for better portability.

Quoted 6 lines
include/kvm/arm64/handle_exit.h new file +0 -0
new file mode 100644
index 0000000000000..9f3f41eaf3ac6

include/kvm/arm64/handle_exit.h line 8

Quoted 8 lines
include/kvm/arm64/handle_exit.h new file +8 -0
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef KVM_ARM64_HANDLE_EXIT_H
+#define KVM_ARM64_HANDLE_EXIT_H
+
+#include <linux/kvm_host.h>
+
+typedef int (*exit_handle_fn)(struct kvm_vcpu *);
+extern exit_handle_fn arm_exit_handlers[ESR_ELx_EC_MAX + 1];

[Severity: Low]
This header uses ESR_ELx_EC_MAX, but doesn't include <asm/esr.h>. Will this cause build failures if a C file includes this header without previously including <asm/esr.h>?

Quoted 22 lines
include/kvm/arm64/handle_exit.h new file +6 -0
+
+int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu);
+exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu);
+int handle_trap_exceptions(struct kvm_vcpu *vcpu);
+
+#endif /* KVM_ARM64_HANDLE_EXIT_H */
virt/kvm/arm64/guest.c new file +10 -0
new file mode 100644
index 0000000000000..83e33e0143b93
@@ -0,0 +1,302 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/kvm_host.h>
+#include <linux/kvm.h>
+#include <asm/pstate.h>
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_nested.h>
+#include <asm/sigcontext.h>
+
+#include <kvm/arm64/guest.h>

[ ... ]

Quoted 2 lines
virt/kvm/arm64/guest.c new file +2 -0
+int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
+{

[ ... ]

virt/kvm/arm64/guest.c line 157

Quoted 2 lines
virt/kvm/arm64/guest.c new file +2 -0
+	if (copy_to_user(uaddr, addr, KVM_REG_SIZE(reg->id)))
+		return -EFAULT;

[Severity: Low]
This file uses copy_to_user() here, and also uses copy_from_user() and put_user() later on, but it does not include <linux/uaccess.h>. Since <linux/uaccess.h> isn't transitively included by the current headers, will this lead to implicit declaration build errors?

Quoted 3 lines
virt/kvm/arm64/guest.c new file +3 -0
+
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#24 lore raw

Re: [PATCH v3 14/27] KVM: s390: Move s390 kvm code into a subdirectory

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] VFIO_PCI_ZDEV_KVM was not updated to depend on KVM_S390, causing VFIO PCI passthrough failures…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] VFIO_PCI_ZDEV_KVM was not updated to depend on KVM_S390, causing VFIO PCI passthrough failures and potential host memory corruption for ARM64 VMs. --

commit 18673c1ce56f6036c29d750f15a7a93d3693b423
Author: Steffen Eiden <[email protected]>

KVM: s390: Move s390 kvm code into a subdirectory

This commit moves s390 KVM guest code into an s390 subdirectory and separates gmap-related code. It renames the main s390-kvm file and updates headers to prepare for a second KVM implementation on s390.

arch/s390/Kconfig config VFIO_CCW · line 840

Quoted 10 lines
arch/s390/Kconfig +1 -1
index ecbcbb781e40a..5583529918dbe 100644
@@ -837,7 +837,7 @@ config VFIO_CCW
 config VFIO_AP
 	def_tristate n
 	prompt "VFIO support for AP devices"
-	depends on KVM
+	depends on KVM_S390

[Severity: Critical]
Should VFIO_PCI_ZDEV_KVM in drivers/vfio/pci/Kconfig also be updated to depend on KVM_S390 instead of KVM?

Leaving VFIO_PCI_ZDEV_KVM dependent on the generic KVM config means it could be compiled in for other KVM implementations, such as ARM64 VMs.

If KVM_S390 is disabled or unloaded, zpci_kvm_hook.kvm_register is NULL, which could cause vfio_pci_zdev_open_device() to return -ENOENT, breaking VFIO PCI passthrough for those VMs.

If KVM_S390 is active, opening a VFIO PCI device for an ARM64 VM will call kvm_s390_pci_register_kvm() and pass it the ARM64 KVM instance. Since the ARM64 kvm_arch structure has a completely different layout, accessing kvm->arch.sie_page2->gisa reads a garbage pointer. Could this calculate an invalid physical address and configure the hardware PCI device to DMA its adapter event notifications to that garbage address, potentially causing host memory corruption and kernel panics?

Quoted 3 lines
arch/s390/Kconfig +0 -0
 	depends on VFIO
 	depends on AP
 	select VFIO_MDEV
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#26 lore raw

Re: [PATCH v3 15/27] KVM: S390: Refactor gmap

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Memory allocation failure in gmap_commit_memory_region silently leaves stale guest hardware page table mappings, risking Guest-to-Host sandbox escapes.
- [High] Non-atomic clearing of the KVM dirty bitmap via memset races with concurrent memory faults, causing permanently lost dirty page records. --

commit a87cd956788430d74dd24a5b4e2b190986ff3010
Author: Steffen Eiden <[email protected]>

KVM: S390: Refactor gmap

This commit refactors gmap code so that a second s390 (host) KVM implementation can use it. It moves relevant definitions into the shared kvm_host.h and moves mmu code and traces from s390 to gmap.

Quoted 5 lines
arch/s390/kvm/gmap/mmu.c new file +0 -0
new file mode 100644
index 0000000000000..d424aa361e795

[ ... ]

Quoted 14 lines
arch/s390/kvm/gmap/mmu.c new file +14 -0
+int gmap_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
+{
+	int r;
+	unsigned long n;
+	struct kvm_memory_slot *memslot;
+	int is_dirty;
+
+	if (kvm_is_ucontrol(kvm))
+		return -EINVAL;
+
+	mutex_lock(&kvm->slots_lock);
+
+	r = -EINVAL;
+	if (log->slot >= KVM_USER_MEM_SLOTS)

arch/s390/kvm/gmap/mmu.c line 40

Quoted 10 lines
arch/s390/kvm/gmap/mmu.c new file +10 -0
+		goto out;
+
+	r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
+	if (r)
+		goto out;
+
+	/* Clear the dirty log */
+	if (is_dirty) {
+		n = kvm_dirty_bitmap_bytes(memslot);
+		memset(memslot->dirty_bitmap, 0, n);

[Severity: High]
This is a pre-existing issue, but does using memset here to clear the dirty_bitmap create a race condition?

Because memset performs non-atomic byte writes, could it race with concurrent mark_page_dirty calls that use atomic bitset operations?

If a concurrent dirty page update is overwritten and lost, could this cause guest memory corruption during live migration since the missed dirty page would not be transferred to the destination?

Quoted 6 lines
arch/s390/kvm/gmap/mmu.c new file +6 -0
+	}
+	r = 0;
+out:
+	mutex_unlock(&kvm->slots_lock);
+	return r;
+}

[ ... ]

Quoted 5 lines
arch/s390/kvm/gmap/mmu.c new file +5 -0
+void gmap_commit_memory_region(struct kvm *kvm,
+			       struct kvm_memory_slot *old,
+			       const struct kvm_memory_slot *new,
+			       enum kvm_mr_change change)
+{

arch/s390/kvm/gmap/mmu.c line 107

Quoted 10 lines
arch/s390/kvm/gmap/mmu.c new file +10 -0
+	struct kvm_s390_mmu_cache *mc = NULL;
+	int rc = 0;
+
+	if (change == KVM_MR_FLAGS_ONLY)
+		return;
+
+	mc = kvm_s390_new_mmu_cache();
+	if (!mc) {
+		rc = -ENOMEM;
+		goto out;

[Severity: Critical]
This isn't a bug introduced by this patch, but is it safe to perform a fallible allocation in gmap_commit_memory_region? Because KVM commit hooks return void, the common code cannot roll back memslot changes if this fails.

If kvm_s390_new_mmu_cache fails with -ENOMEM here, we jump out and skip updating the hardware DAT tables via dat_delete_slot. Could the guest retain hardware-level access to a removed memory region as a result?

If userspace reuses this virtual address range for sensitive internal data rather than unmapping it, could the guest read or write it, leading to a sandbox escape? Should this fallible allocation be moved to gmap_prepare_memory_region instead?

Quoted 4 lines
arch/s390/kvm/gmap/mmu.c new file +4 -0
+	}
+
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		switch (change) {

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#28 lore raw

Re: [PATCH v3 16/27] KVM: Make device name configurable

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Leaked debugfs directory and Use-After-Free on module initialization failure. --

commit 04c2f7cc9b71f864ca5d4b3147f1edb93f37b395
Author: Steffen Eiden <[email protected]>

KVM: Make device name configurable

This commit allows KVM implementations to configure the device name, which is particularly useful for architectures providing multiple implementations. It introduces kvm_init_with_dev() and updates kvm_init_debug() to accept a device name.

Quoted 3 lines
virt/kvm/kvm_main.c +0 -0

[ ... ]

virt/kvm/kvm_main.c int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) · line 6515

Quoted 6 lines
virt/kvm/kvm_main.c +1 -1
@@ -6505,7 +6512,7 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
 	kvm_preempt_ops.sched_in = kvm_sched_in;
 	kvm_preempt_ops.sched_out = kvm_sched_out;
 
-	kvm_init_debug();
+	kvm_init_debug(kvm_dev.name);

[Severity: High]
This is a pre-existing issue, but does the error handling in kvm_init_with_dev() leak the debugfs directory on failure?

kvm_init_debug() creates the debugfs directory and populates it with stats files. If any of the subsequent initializations fail (such as kvm_vfio_ops_init(), kvm_gmem_init(), or misc_register()), the rollback path jumps to error labels like err_vfio or err_register.

None of these error paths appear to call debugfs_remove_recursive() or an equivalent cleanup function for the debugfs directory. If KVM is compiled as a module and fails to initialize, the module memory is freed. Would this leave stale debugfs entries behind, potentially causing a use-after-free if a user accesses those files and triggers the fops pointing to the freed module memory?

Quoted 3 lines
virt/kvm/kvm_main.c +0 -0
 	r = kvm_vfio_ops_init();
 	if (WARN_ON_ONCE(r))
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
#30 lore raw

Re: [PATCH v3 17/27] KVM: Remove KVM_MMIO as config option

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Brittle header dependency introduced in `virt/kvm/coalesced_mmio.h`. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Brittle header dependency introduced in virt/kvm/coalesced_mmio.h. --

commit fd122dad5412c5f1816bc36167f1d3f0cd523036
Author: Steffen Eiden <[email protected]>

KVM: Remove KVM_MMIO as config option

Defining KVM_MMIO is not flexible enough for multi-KVM systems with different Kconfig options regarding KVM_MMIO. Therefore, remove KVM_MMIO from the config space and use the macro HAVE_KVM_MMIO instead.

[ ... ]

virt/kvm/coalesced_mmio.h line 14

Quoted 10 lines
virt/kvm/coalesced_mmio.h +1 -1
index 36f84264ed257..a8430d41945b9 100644
@@ -11,7 +11,7 @@
  *
  */
 
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO

[Severity: Low]
Does this make the header implicitly dependent on the inclusion order?

Since HAVE_KVM_MMIO is no longer a globally available config macro but is instead defined in <asm/kvm_host.h>, evaluating it here without explicitly including <linux/kvm_host.h> relies on the calling C files including it first.

While this doesn't cause an immediate build failure with the current includers (kvm_main.c and coalesced_mmio.c), a future includer that does not include <linux/kvm_host.h> first would silently evaluate the macro as false, resulting in missing struct declarations.

Quoted 3 lines
virt/kvm/coalesced_mmio.h +0 -0
 #include <linux/list.h>
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
#34 lore raw

Re: [PATCH v3 20/27] KVM: s390: arm64: Introduce host definitions

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Macro `__ctxt_sys_reg` returns NULL, breaking KVM_GET_ONE_REG/KVM_SET_ONE_REG for ELR_EL1 and…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Macro __ctxt_sys_reg returns NULL, breaking KVM_GET_ONE_REG/KVM_SET_ONE_REG for ELR_EL1 and SPSR_EL1 --

commit e22b4e249b651c1cc6aa77134aa91b1e8f9d4063
Author: Steffen Eiden <[email protected]>
KVM: s390: arm64: Introduce host definitions

This commit adds basic definitions the arm on s390 KVM host requires, including struct kvm*arch definitions, function prototypes, and various macros required to run arm64 guests.

Quoted 4 lines
arch/s390/include/asm/kvm_host_arm64.h new file +0 -0
new file mode 100644

[ ... ]

arch/s390/include/asm/kvm_host_arm64.h line 28

Quoted 5 lines
arch/s390/include/asm/kvm_host_arm64.h new file +5 -0
+/* Minimal (=no) vgic definitions */
+#define KVM_IRQCHIP_NUM_PINS 1
+#define irqchip_in_kernel(_v) false;
+
+#define __ctxt_sys_reg(ctx, reg) NULL

[Severity: High]
Does defining this macro to return NULL break KVM_GET_ONE_REG and KVM_SET_ONE_REG for ELR_EL1 and SPSR_EL1?

Shared code in virt/kvm/arm64/guest.c uses this macro to resolve the storage address of core registers ELR_EL1 and SPSR_EL1. Returning NULL causes core_reg_addr() to return NULL, resulting in the ioctls failing with -EINVAL.

However, copy_core_reg_indices() includes these registers in the list returned by KVM_GET_REG_LIST because core_reg_size_from_offset() returns a valid size for them.

Advertising support for registers in KVM_GET_REG_LIST but failing to get or set them is an API contract violation that can cause userspace VMMs like QEMU to abort during vCPU initialization.

Quoted 6 lines
arch/s390/include/asm/kvm_host_arm64.h new file +6 -0
+struct kvm_cpu_context {
+	/*
+	 * These are just for 32 bit, which we don't have, making them RES0.
+	 * They are exposed to user space.
+	 */
+	u64	spsr_abt;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
#37 lore raw

Re: [PATCH v3 22/27] KVM: s390: Add basic arm64 kvm module

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Calling `kvm_init_with_dev()` from a secondary/independent KVM architecture backend…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Calling kvm_init_with_dev() from a secondary/independent KVM architecture backend (kvm-s390-arm64) causes catastrophic double-initialization of the KVM core subsystem, leading to global state corruption and kernel panics.
- [High] Integer underflow in kvm_max_guest_address() causes KVM to report an invalid, massively incorrect maximum IPA size on host systems with limited memory.
--

commit cceba4a7a6d94b89a1513f32f00ede5f3c39fa4c
Author: Steffen Eiden <[email protected]>

KVM: s390: Add basic arm64 kvm module

Add basic code for the new arm64 on s390 KVM implementation. Add kernel module boilerplate code and trivial functions.

Quoted 5 lines
arch/s390/kvm/arm64/arm.c new file +0 -0
new file mode 100644
index 0000000000000..591e2e874891a

[ ... ]

arch/s390/kvm/arm64/arm.c line 45

Quoted 9 lines
arch/s390/kvm/arm64/arm.c new file +9 -0
+static u64 kvm_max_guest_address(void)
+{
+	u64 max_addr;
+
+	if (sclp.hamax == U64_MAX)
+		max_addr = TASK_SIZE_MAX;
+	else
+		max_addr = min_t(u64, TASK_SIZE_MAX, sclp.hamax);
+	return ALIGN_DOWN(max_addr + 1, 1 << 30) - 1;

[Severity: High]
If the host system has less than 1GB of addressable memory (i.e., sclp.hamax < 1GB - 1), will ALIGN_DOWN() evaluate to 0 and cause the subsequent subtraction of 1 to underflow to U64_MAX?

Quoted 1 line

+}

[ ... ]

arch/s390/kvm/arm64/arm.c line 61

Quoted 3 lines
arch/s390/kvm/arm64/arm.c new file +3 -0
+u32 get_kvm_ipa_limit(void)
+{
+	return fls64(kvm_max_guest_address() + 1) - 1;

[Severity: High]
Following the underflow in kvm_max_guest_address(), would this result in fls64(U64_MAX + 1) - 1?

Since U64_MAX + 1 is 0 and fls64(0) returns 0, this calculates to -1, which would be cast to 0xFFFFFFFF. Does this inadvertently return an invalid 32-bit limit of 0xFFFFFFFF to userspace capability checks?

Quoted 1 line

+}

[ ... ]

arch/s390/kvm/arm64/arm.c line 177

Quoted 7 lines
arch/s390/kvm/arm64/arm.c new file +7 -0
+static int __init kvm_s390_arm64_init(void)
+{
+	if (!sclp.has_aef)
+		return -ENXIO;
+
+	return kvm_init_with_dev(sizeof(struct kvm_vcpu), 0, THIS_MODULE,
+				 KVM_DEV_NAME, MISC_DYNAMIC_MINOR);

[Severity: Critical]
Since kvm_init_with_dev() is a core KVM function that manipulates global state, does calling it from a secondary KVM architecture backend cause a double-initialization if the primary s390 KVM module is already active?

Loading this module while kvm.ko is active appears to invoke INIT_LIST_HEAD() on a miscdevice already in the list, potentially corrupting the doubly linked list and causing a panic on traversal.

Could it also allocate a duplicate kvm_vcpu_cache, overwriting the global pointer and leaking the original cache?

Quoted 1 line

+}

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
#40 lore raw

Re: [PATCH v3 24/27] KVM: s390: arm64: Implement vm/vcpu create destroy.

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] In kvm_arch_init_vm, kvm->arch.mem_limit is improperly set to the inclusive hardware limit (kvm_max_guest_address()) instead of the VM's requested IPA size (kvm->arch.guest_phys_size). This causes gmap_prepare_memory_region to incorrectly reject perfectly sized memslots (because it compares an exclusive memslot size against an inclusive limit) and allows userspace to map memslots far beyond the VM's requested address space. These out-of-bounds memslots are then silently ignored during the commit phase by _dat_walk_gfn_range (due to DAT_WALK_IGN_HOLES), creating a fatal desynchronization between KVM's memslots and the hardware DAT tables.
- [High] gmap_commit_memory_region performs a fallible memory allocation (kvm_s390_new_mmu_cache()) during the KVM memslot commit phase, which is architecturally required to be non-failable (it returns void). If this allocation fails, the function aborts and silently skips updating the gmap DAT tables.

Pre-existing issues:
- [High] gmap_get_dirty_log() uses a non-atomic memset() to clear the memory slot's dirty_bitmap after copying it to userspace via kvm_get_dirty_log(). This introduces a severe race condition against concurrent callers of mark_page_dirty() (e.g., from interrupt injection in s390/kvm/s390/interrupt.c or nested virtualization), which use atomic bit operations (set_bit_le). --

commit 001e0a58ea811dbd19411f46d5babaaf3bbbbc47
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement vm/vcpu create destroy.

Implement init and destroy IOCTLS for vcpu and vm. Implement arch vm IOCTL. Use s390 gmap.

Quoted 7 lines
arch/s390/kvm/arm64/arm.c +0 -0
index 591e2e874891a..79ce6570fa031 100644
@@ -45,6 +48,61 @@ static u64 kvm_max_guest_address(void)
 	return ALIGN_DOWN(max_addr + 1, 1 << 30) - 1;
 }

[ ... ]

Quoted 2 lines
arch/s390/kvm/arm64/arm.c +2 -0
+int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
+{

[ ... ]

arch/s390/kvm/arm64/arm.c static u64 kvm_max_guest_address(void) · line 95

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +4 -0
+	ret = kvm_gmap_init(kvm);
+	if (ret)
+		goto out_err;
+	kvm->arch.mem_limit = kvm_max_guest_address();

[Severity: High]
Does setting kvm->arch.mem_limit to the inclusive hardware limit kvm_max_guest_address() instead of kvm->arch.guest_phys_size cause memslot validation issues?

When gmap_prepare_memory_region() validates the memory slots:

arch/s390/kvm/gmap/mmu.c:gmap_prepare_memory_region() {
        ...
        if ((new->base_gfn + new->npages) * PAGE_SIZE > kvm->arch.mem_limit)
                return -EINVAL;
        ...
}

It compares an exclusive size against this inclusive limit. Could this incorrectly reject perfectly sized memslots?

Additionally, since it uses the maximum host physical capacity instead of the VM's requested IPA size, could it allow userspace to map memslots beyond the VM's address space, resulting in a desynchronization between KVM and the hardware DAT tables?

[ ... ]

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, · line 208

Quoted 9 lines
arch/s390/kvm/arm64/arm.c +5 -0
@@ -107,6 +201,52 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 	return 0;
 }
 
+int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
+			       struct kvm_dirty_log *log)
+{
+	return gmap_get_dirty_log(kvm, log);
+}

[Severity: High]
This is a pre-existing issue, but is it safe to use gmap_get_dirty_log() here given how it clears the dirty bitmap?

In the underlying implementation:

arch/s390/kvm/gmap/mmu.c:gmap_get_dirty_log() {
        ...
        if (is_dirty) {
                n = kvm_dirty_bitmap_bytes(memslot);
                memset(memslot->dirty_bitmap, 0, n);
        }
        ...
}

The memset() is non-atomic. Could this permanently clobber concurrently set dirty bits from mark_page_dirty(), which uses atomic bit operations? Any dirty bits set concurrently during or immediately before the memset could be lost.

[ ... ]

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, · line 238

Quoted 7 lines
arch/s390/kvm/arm64/arm.c +7 -0
+void kvm_arch_commit_memory_region(struct kvm *kvm,
+				   struct kvm_memory_slot *old,
+				   const struct kvm_memory_slot *new,
+				   enum kvm_mr_change change)
+{
+	gmap_commit_memory_region(kvm, old, new, change);
+}

[Severity: High]
Is it safe to rely on a fallible allocation inside the void kvm_arch_commit_memory_region() phase?

Calling gmap_commit_memory_region() here triggers a memory allocation:

arch/s390/kvm/gmap/mmu.c:gmap_commit_memory_region() {
        ...
        mc = kvm_s390_new_mmu_cache();
        if (!mc) {
                rc = -ENOMEM;
                goto out;
        }
        ...
}

Since the commit phase returns void and cannot fail to userspace, if this allocation fails under memory pressure, it will silently skip updating the gmap DAT tables. Could this cause KVM's generic memslots to become permanently desynchronized from the hardware gmap state?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
#42 lore raw

Re: [PATCH v3 25/27] KVM: s390: arm64: Implement vCPU IOCTLs

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] Userspace-reachable WARN_ONCE creates a host-side DoS vulnerability via hardware validity…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] Userspace-reachable WARN_ONCE creates a host-side DoS vulnerability via hardware validity intercepts.
- [High] Calling local_irq_enable() inside an RCU extended quiescent state breaks context tracking and introduces a race condition.
- [High] The vCPU run loop fails to set vcpu->mode to IN_GUEST_MODE, leading to silently dropped IPI kicks.
- [High] Crucial KVM requests (such as KVM_REQ_VM_DEAD and KVM_REQ_SLEEP) are ignored, allowing execution in an invalid or fatal state.
- [High] The vCPU run loop checks need_resched() and signal_pending() with interrupts enabled, causing a TOCTOU race with host interrupts.
- [Medium] kvm_arm_vcpu_finalize unconditionally returns success for unsupported features.
--

commit 9e53079e1aa1b7ab592dcb9275360b129725a075
Author: Steffen Eiden <[email protected]>
Subject: KVM: s390: arm64: Implement vCPU IOCTLs

This patch implements the core vCPU ioctls for the s390 arm64 KVM port. It adds support for initializing and running the guest, as well as handling registers and handling exceptions through the SAE block.

arch/s390/kvm/arm64/arm.c +0 -0

[ ... ]
@@ -244,12 +268,12 @@

Quoted 4 lines
(quoted diff) +4 -0
+static int check_vcpu_requests(struct kvm_vcpu *vcpu)
+{
+	if (kvm_request_pending(vcpu)) {
+		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))

Review

Quoted 10 lines
(quoted diff) +10 -0
+			kvm_reset_vcpu(vcpu);
+		/*
+		 * Clear IRQ_PENDING requests that were made to guarantee
+		 * that a VCPU sees new virtual interrupts.
+		 */
+		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
+	}
+
+	return 1;
+}

[Severity: High]
Does this code ignore crucial KVM requests like KVM_REQ_VM_DEAD or KVM_REQ_SLEEP? It looks like it only processes KVM_REQ_VCPU_RESET and KVM_REQ_IRQ_PENDING. If a fatal error occurs and KVM_REQ_VM_DEAD is pending, would returning 1 here cause the vCPU to immediately resume guest execution and override the KVM failsafe?

[ ... ]
@@ -348,15 +373,15 @@

Review

Quoted 9 lines
(quoted diff) +9 -0
+static void arm_vcpu_run(struct kvm_vcpu *vcpu)
+{
+	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+
+	adjust_pc(vcpu);
+
+	local_irq_disable();
+	guest_enter_irqoff();
+	local_irq_enable();

[Severity: High]
Does calling local_irq_enable() immediately after guest_enter_irqoff() break context tracking? guest_enter_irqoff() signals to RCU that the CPU is entering an extended quiescent state. If a host interrupt fires after local_irq_enable() but before the actual guest entry, would the interrupt handler execute while RCU considers the CPU quiescent, leading to lockdep/RCU splats or potential use-after-free bugs?

Review

Quoted 9 lines
(quoted diff) +9 -0
+
+	sae_block->icptr = 0;
+
+	sae64a(sae_block);
+
+	local_irq_disable();
+	guest_exit_irqoff();
+	local_irq_enable();
+}

[ ... ]
@@ -395,20 +420,20 @@

Review

Quoted 10 lines
(quoted diff) +10 -0
+	ret = 1;
+	do {
+		if (signal_pending(current)) {
+			kvm_run->exit_reason = KVM_EXIT_INTR;
+			ret = -EINTR;
+			continue;
+		}
+
+		if (need_resched())
+			schedule();

[Severity: High]
Can this sequence cause a time-of-check to time-of-use race with host interrupts? The checks for signal_pending() and need_resched() in kvm_arch_vcpu_ioctl_run() are performed with local interrupts enabled. If a host interrupt arrives immediately after these checks and sets a reschedule or signal flag, would the vCPU blindly enter the guest via arm_vcpu_run() and ignore the pending request for a long time?

Review

Quoted 7 lines
(quoted diff) +7 -0
+
+		if (ret > 0)
+			ret = check_vcpu_requests(vcpu);
+
+		vcpu->arch.sae_block.icptr = 0;
+
+		arm_vcpu_run(vcpu);

[Severity: High]
Are we missing the transition of vcpu->mode to IN_GUEST_MODE before calling arm_vcpu_run()? KVM's generic kvm_vcpu_kick() mechanism relies on checking if vcpu->mode == IN_GUEST_MODE to send an IPI. If the state remains OUTSIDE_GUEST_MODE, would concurrent kicks for virtual interrupt injections or halt requests silently drop the IPI and leave the vCPU trapped in guest execution?

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+		icptr = vcpu->arch.sae_block.icptr;
+		switch (icptr) {
+		case SAE_ICPTR_SPURIOUS:
+			break;
+		case SAE_ICPTR_VALIDITY:
+			WARN_ONCE(true, "SAE: validity intercept. vir: 0x%04x",
+				  vcpu->arch.sae_block.vir);
+			ret = -EINVAL;
+			break;

[Severity: High]
Is it possible for malicious userspace to trigger this WARN_ONCE? If an unprivileged host userspace provides invalid vCPU register states via KVM_SET_ONE_REG prior to running the vCPU, it could force a validity intercept. KVM subsystem guidelines forbid guest- or host-userspace-reachable WARN_ON assertions since they can crash the host if panic_on_warn is enabled.

Quoted 3 lines
(quoted diff) +3 -0
+		case SAE_ICPTR_SYNCHRONOUS_EXCEPTION:
+			ret = handle_trap_exceptions(vcpu);
+			break;

[ ... ]

arch/s390/kvm/arm64/reset.c new file +0 -0
new file mode 100644

[ ... ]
@@ -48,10 +48,10 @@

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
+{
+	return 0;
+}

[Severity: Medium]
Should this function validate the requested feature instead of unconditionally returning success? By ignoring the feature argument and returning 0, would userspace incorrectly believe an unsupported feature is successfully configured, leading to unpredictable failures later?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=25
#44 lore raw

Re: [PATCH v3 26/27] KVM: s390: arm64: Implement basic page fault handler

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

New issues:
- [High] Guest-reachable or hardware-driven edge cases trigger WARN_ON/BUG_ON macros, creating a host Denial of Service vector.
- [Low] Unused parameters slot and hva in user_mem_abort().

Pre-existing issues:
- [Critical] Handling traps in-kernel causes an infinite loop because the guest PC is not advanced before re-entering the guest.
- [High] The VCPU run loop unconditionally enters the guest, ignoring the return value of check_vcpu_requests(). --

commit 6963b72c83b66b00932e10f26e0df9e655610d27
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement basic page fault handler

Add host functionality to page in guest memory. If the guest does something unexpected or illegal exit to userspace which very likely has to stop guest execution. This behaviour will be changed to guest error injects once all sysregs are accessible for the host.

arch/s390/kvm/arm64/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · line 443

Quoted 12 lines
arch/s390/kvm/arm64/arm.c +1 -0
index 18eca2261e0da..bf0866659421a 100644
@@ -437,6 +437,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 				  vcpu->arch.sae_block.vir);
 			ret = -EINVAL;
 			break;
+		case SAE_ICPTR_HOST_ACCESS_EXCEPTION:
 		case SAE_ICPTR_SYNCHRONOUS_EXCEPTION:
 			ret = handle_trap_exceptions(vcpu);
 			break;

[Severity: High]
This is a pre-existing issue, but since we are modifying the run loop handling here, is there an issue with how check_vcpu_requests() return values are handled?

Looking at kvm_arch_vcpu_ioctl_run(), check_vcpu_requests() is called and its result is assigned to ret, but then arm_vcpu_run(vcpu) is called unconditionally immediately after. Does this bypass pending signals, IPIs, and userspace exits if check_vcpu_requests() returns a value less than or equal to 0?

[ ... ]

Quoted 4 lines
arch/s390/kvm/arm64/mmu.c new file +0 -0
new file mode 100644

[ ... ]

arch/s390/kvm/arm64/mmu.c line 23

Quoted 3 lines
arch/s390/kvm/arm64/mmu.c new file +3 -0
+static int user_mem_abort(struct kvm_vcpu *vcpu, gpa_t fault_ipa,
+			  struct kvm_memory_slot *slot, hva_t hva)
+{

[Severity: Low]
Do we need the slot and hva parameters in this function? It looks like they are unused since kvm_s390_faultin_gfn() handles the page translation itself.

Quoted 2 lines
arch/s390/kvm/arm64/mmu.c new file +2 -0
+	struct guest_fault f = { };
+	int ret;

[ ... ]

Quoted 3 lines
arch/s390/kvm/arm64/mmu.c new file +3 -0
+static int kvm_handle_pic(struct kvm_vcpu *vcpu, bool *translation)
+{
+	switch (kvm_vcpu_fault_pic(vcpu)) {

[ ... ]

arch/s390/kvm/arm64/mmu.c line 64

Quoted 7 lines
arch/s390/kvm/arm64/mmu.c new file +7 -0
+	/* unexpected cases: */
+	case 0:
+		KVM_BUG(1, vcpu->kvm, "On MMU fault path but no fault occurred");
+		return -EFAULT;
+	default:
+		KVM_BUG(1, vcpu->kvm, "Unexpected program interrupt 0x%x, TEID 0x%016lx",
+			vcpu->arch.sae_block.hai.pic, vcpu->arch.sae_block.hai.teid.val);

[Severity: High]
Can this KVM_BUG be triggered by a guest action? If an unexpected program interrupt can be driven by guest behavior, it would hit this macro which expands to a WARN_ONCE, potentially creating a host denial of service vector via log spam.

Quoted 9 lines
arch/s390/kvm/arm64/mmu.c new file +9 -0
+		send_sig(SIGSEGV, current, 0);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
+{

[ ... ]

arch/s390/kvm/arm64/mmu.c line 148

Quoted 8 lines
arch/s390/kvm/arm64/mmu.c new file +8 -0
+		/*
+		 * Check for a cache maintenance operation. Assume the guest is
+		 * cautious and skip instruction
+		 */
+		if (kvm_is_error_hva(hva) && kvm_vcpu_dabt_is_cm(vcpu)) {
+			kvm_incr_pc(vcpu);
+			ret = 1;
+			goto out_unlock;

[Severity: Critical]
This is a pre-existing issue, but does returning 1 here to resume the guest result in an infinite loop?

When kvm_incr_pc() sets the INCREMENT_PC flag and ret is set to 1, the run loop in kvm_arch_vcpu_ioctl_run() iterates without calling adjust_pc(), because adjust_pc() is located at the out label completely outside the do-while loop:

arch/s390/kvm/arm64/arm.c:kvm_arch_vcpu_ioctl_run() {
    ...
    do {
        ...
        arm_vcpu_run(vcpu);
        ...
    } while (ret > 0);

out:
if (unlikely(vcpu_get_flag(vcpu, INCREMENT_PC)))
adjust_pc(vcpu);
}

Would this cause arm_vcpu_run() to be re-executed with the exact same PC, hanging the host thread?

[ ... ]

arch/s390/kvm/arm64/mmu.c line 170

Quoted 9 lines
arch/s390/kvm/arm64/mmu.c new file +9 -0
+	/* Userspace should not be able to register out-of-bounds IPAs */
+	VM_BUG_ON(fault_ipa >= kvm_phys_size(vcpu->kvm));
+	/*
+	 * Proper guest size faults have been injected.
+	 * In theory it's fine to have device memory higher than MSL,
+	 * even if not currently possible, but that would have been handled above.
+	 * So if we get here with a guest size intercept, we have a bug somewhere.
+	 */
+	VM_BUG_ON(guest_size_err);

[Severity: High]
Is it possible for a guest to trigger this assertion? If a guest triggers an address size fault where fault_ipa < kvm_phys_size(vcpu->kvm), the host kernel would hit this VM_BUG_ON. This could cause a panic when CONFIG_DEBUG_VM is enabled.

Quoted 8 lines
arch/s390/kvm/arm64/mmu.c new file +8 -0
+
+	ret = user_mem_abort(vcpu, fault_ipa, memslot, hva);
+	if (!ret)
+		ret = 1;
+out_unlock:
+	srcu_read_unlock(&vcpu->kvm->srcu, idx);
+	return ret;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26
#46 lore raw

Re: [PATCH v3 27/27] KVM: s390: arm64: Enable KVM_ARM64 config and Kbuild

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The Makefile uses GNU Binutils incompatible flags `$(NM) -jU` and `$(OBJCOPY) -O default`, causing…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The Makefile uses GNU Binutils incompatible flags $(NM) -jU and $(OBJCOPY) -O default, causing immediate build failures.
- [High] Compiling both kvm-s390 and kvm-arm64 causes fatal linker multiple-definition errors when built-in, or duplicate-export failures when loaded as modules. --

commit 8f8fd4d3ea8855f34fafa2691472eea31e58dd4d
Author: Steffen Eiden <[email protected]>
Subject: KVM: s390: arm64: Enable KVM_ARM64 config and Kbuild

This commit adds the Kbuild/Makefile configurations to build a second KVM module on s390 that implements the arm64-KVM API. It mangles internal symbols in kvm-arm64 when compiled as built-in to prevent symbol conflicts with kvm-s390.

Quoted 5 lines
arch/s390/kvm/arm64/Makefile new file +0 -0
new file mode 100644
index 0000000000000..d0aac34b8d2ff

[ ... ]

arch/s390/kvm/arm64/Makefile line 31

Quoted 5 lines
arch/s390/kvm/arm64/Makefile new file +5 -0
+ifeq ($(CONFIG_KVM_ARM64),m)
+
+kvm-arm64-y = $(kvm-arm64-obj)
+
+else ifeq ($(CONFIG_KVM_ARM64),y)

[Severity: High]
Does this bypass namespacing entirely when built as a module?

If localization is skipped, it seems both kvm-s390.ko and kvm-arm64.ko will attempt to export identical KVM core symbols (such as kvm_init). Will the kernel module loader refuse to load the second module due to duplicate exports?

[ ... ]

arch/s390/kvm/arm64/Makefile line 72

Quoted 3 lines
arch/s390/kvm/arm64/Makefile new file +3 -0
+quiet_cmd_nm_filter = NMFLTR  $@
+	cmd_nm_filter = \
+$(NM) -jU $< | awk '							\

[Severity: High]
Can these nm flags cause build failures on standard GCC/binutils s390x setups?

The -j (just symbol names) and -U flags appear to be LLVM extensions that are not supported by standard GNU nm, which might result in an invalid option error during standard kernel builds.

arch/s390/kvm/arm64/Makefile line 75

Quoted 3 lines
arch/s390/kvm/arm64/Makefile new file +3 -0
+{	if (match($$0, /^__export_symbol_(.*)$$/, exp_sym)) {		\
+		exp_syms[exp_sym[1]] = 1;				\
+	} else {							\

[Severity: High]
By filtering out exported symbols from the localization list here, do the exported functions remain globally visible?

If both KVM implementations are compiled into vmlinux, this looks like it will cause fatal multiple definition linker errors since they both compile their own copy of KVM core files.

[ ... ]

arch/s390/kvm/arm64/Makefile line 96

Quoted 7 lines
arch/s390/kvm/arm64/Makefile new file +7 -0
+define rule_oc_o_o
+	$(call cmd_and_savecmd,objcopy)
+	$(call cmd,gen_objtooldep)
+	$(call cmd,gen_symversions_o)
+endef
+
+OBJCOPYFLAGS_kvm-namespaced.o := -O default --localize-symbols=$(obj)/kvm_symbol_list

[Severity: High]
Is "-O default" supported by standard GNU objcopy?

Standard GNU objcopy does not recognize "default" as a valid BFD output target (such as elf64-s390) and fails with an "invalid bfd target" error. Could the -O flag be omitted entirely to retain the input format?

Quoted 2 lines

+$(obj)/kvm-namespaced.o: $(obj)/kvm-unnamespaced.o $(obj)/kvm_symbol_list FORCE

arch/s390/kvm/arm64/Makefile new file +1 -0
+	$(call if_changed_rule,oc_o_o)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=27