Skip to contentSkip to conversation

KVM: PPC: sPAPR: Enable magic page mapping

PATCH In review Alexander Graf 2 participants 7 messages Jun 4, 2014

lore
Share & tools
lore
#0 lore raw

cover KVM: PPC: sPAPR: Enable magic page mapping

The magic page allows us to speed up guests significantly on PR KVM. To enable the magic page the guest needs to be able to issue KVM hypercalls. Information on how to trigger them comes via device tree.

So far we didn't expose the respective device tree node on the sPAPR machine type. Unfortunately during testing a number of issues got revealed when the guest uses our magic page, so we only enable kvm hypercalls when the host kernel is patched to make this a safe operation.

Alexander Graf (4):
linux-headers: include psci.h
linux-headers: update linux headers to kvm/next
KVM: PPC: Expose fixup hcall capability
PPC: spapr: Expose /hypervisor node in device tree

 hw/ppc/spapr.c                       | 20 ++++++++
 linux-headers/asm-arm/kvm.h          | 10 ++--
 linux-headers/asm-arm64/kvm.h        | 13 ++++--
 linux-headers/asm-mips/kvm.h         | 35 ++++++++++++++
 linux-headers/asm-powerpc/kvm.h      |  2 +-
 linux-headers/asm-powerpc/kvm_para.h |  6 +++
 linux-headers/linux/kvm.h            | 10 ++++
 linux-headers/linux/psci.h           | 90 ++++++++++++++++++++++++++++++++++++
 scripts/update-linux-headers.sh      |  3 +-
 target-ppc/kvm.c                     |  7 +++
 target-ppc/kvm_ppc.h                 |  6 +++
 11 files changed, 191 insertions(+), 11 deletions(-)
 create mode 100644 linux-headers/linux/psci.h
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/4 linux-headers: include psci.h

The kvm headers now have a dependency on psci.h, sync it into our linux header copy as well.

---
 scripts/update-linux-headers.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/update-linux-headers.sh +2 -1
index 120a694..c8e026d 100755
@@ -61,7 +61,8 @@ done
 
 rm -rf "$output/linux-headers/linux"
 mkdir -p "$output/linux-headers/linux"
-for header in kvm.h kvm_para.h vfio.h vhost.h virtio_config.h virtio_ring.h; do
+for header in kvm.h kvm_para.h vfio.h vhost.h virtio_config.h virtio_ring.h \
+              psci.h; do
     cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux"
 done
 rm -rf "$output/linux-headers/asm-generic"
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] linux-headers: include psci.h

Quoted 23 lines · Alexander Graf

The kvm headers now have a dependency on psci.h, sync it into our linux header copy as well.

---
 scripts/update-linux-headers.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/update-linux-headers.sh +2 -2
index 120a694..c8e026d 100755
@@ -61,7 +61,8 @@ done
 
 rm -rf "$output/linux-headers/linux"
 mkdir -p "$output/linux-headers/linux"
-for header in kvm.h kvm_para.h vfio.h vhost.h virtio_config.h virtio_ring.h; do
+for header in kvm.h kvm_para.h vfio.h vhost.h virtio_config.h virtio_ring.h \
+              psci.h; do
     cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux"
 done
 rm -rf "$output/linux-headers/asm-generic"
--
Quoted 1 line

1.8.1.4

This is a dup of a patch which has already been posted as part of Pranav's PSCI patchset...

thanks
-- PMM

#3 lore raw

Re: [Qemu-devel] [PATCH 1/4] linux-headers: include psci.h

Quoted 15 lines · Peter Maydell
Quoted 14 lines · Alexander Graf

The kvm headers now have a dependency on psci.h, sync it into our linux header copy as well.

---
  scripts/update-linux-headers.sh | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
scripts/update-linux-headers.sh +0 -0
index 120a694..c8e026d 100755
@@ -61,7 +61,8 @@ done
 
Quoted 12 lines
Quoted 10 lines
scripts/update-linux-headers.sh +2 -2
  rm -rf "$output/linux-headers/linux"
  mkdir -p "$output/linux-headers/linux"
-for header in kvm.h kvm_para.h vfio.h vhost.h virtio_config.h virtio_ring.h; do
+for header in kvm.h kvm_para.h vfio.h vhost.h virtio_config.h virtio_ring.h \
+              psci.h; do
      cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux"
  done
  rm -rf "$output/linux-headers/asm-generic"
--

1.8.1.4

This is a dup of a patch which has already been posted as part of Pranav's PSCI patchset...

Cool, so we now have 3 copies of the same patch ;). I guess whoever hits master first just gets in - it's not like there's special sauce in here anywhere.

Alex

#4 lore raw

PATCH 2/4 linux-headers: update linux headers to kvm/next

In reply to Alexander Graf

This updates the kvm headers to commit 820b3fcd in kvm/next.

---
 linux-headers/asm-arm/kvm.h          | 10 ++--
 linux-headers/asm-arm64/kvm.h        | 13 ++++--
 linux-headers/asm-mips/kvm.h         | 35 ++++++++++++++
 linux-headers/asm-powerpc/kvm.h      |  2 +-
 linux-headers/asm-powerpc/kvm_para.h |  6 +++
 linux-headers/linux/kvm.h            | 10 ++++
 linux-headers/linux/psci.h           | 90 ++++++++++++++++++++++++++++++++++++
 7 files changed, 156 insertions(+), 10 deletions(-)
 create mode 100644 linux-headers/linux/psci.h
linux-headers/asm-arm/kvm.h +6 -4
index ef0c878..e6ebdd3 100644
@@ -20,6 +20,7 @@
 #define __ARM_KVM_H__
 
 #include <linux/types.h>
+#include <linux/psci.h>
 #include <asm/ptrace.h>
 
 #define __KVM_HAVE_GUEST_DEBUG
@@ -83,6 +84,7 @@ struct kvm_regs {
 #define KVM_VGIC_V2_CPU_SIZE		0x2000
 
 #define KVM_ARM_VCPU_POWER_OFF		0 /* CPU is started in OFF state */
+#define KVM_ARM_VCPU_PSCI_0_2		1 /* CPU uses PSCI v0.2 */
 
 struct kvm_vcpu_init {
 	__u32 target;
@@ -201,9 +203,9 @@ struct kvm_arch_memory_slot {
 #define KVM_PSCI_FN_CPU_ON		KVM_PSCI_FN(2)
 #define KVM_PSCI_FN_MIGRATE		KVM_PSCI_FN(3)
 
-#define KVM_PSCI_RET_SUCCESS		0
-#define KVM_PSCI_RET_NI			((unsigned long)-1)
-#define KVM_PSCI_RET_INVAL		((unsigned long)-2)
-#define KVM_PSCI_RET_DENIED		((unsigned long)-3)
+#define KVM_PSCI_RET_SUCCESS		PSCI_RET_SUCCESS
+#define KVM_PSCI_RET_NI			PSCI_RET_NOT_SUPPORTED
+#define KVM_PSCI_RET_INVAL		PSCI_RET_INVALID_PARAMS
+#define KVM_PSCI_RET_DENIED		PSCI_RET_DENIED
 
 #endif /* __ARM_KVM_H__ */
linux-headers/asm-arm64/kvm.h +8 -5
index eaf54a3..e633ff8 100644
@@ -31,6 +31,7 @@
 #define KVM_NR_SPSR	5
 
 #ifndef __ASSEMBLY__
+#include <linux/psci.h>
 #include <asm/types.h>
 #include <asm/ptrace.h>
 
@@ -56,8 +57,9 @@ struct kvm_regs {
 #define KVM_ARM_TARGET_FOUNDATION_V8	1
 #define KVM_ARM_TARGET_CORTEX_A57	2
 #define KVM_ARM_TARGET_XGENE_POTENZA	3
+#define KVM_ARM_TARGET_CORTEX_A53	4
 
-#define KVM_ARM_NUM_TARGETS		4
+#define KVM_ARM_NUM_TARGETS		5
 
 /* KVM_ARM_SET_DEVICE_ADDR ioctl id encoding */
 #define KVM_ARM_DEVICE_TYPE_SHIFT	0
@@ -77,6 +79,7 @@ struct kvm_regs {
 
 #define KVM_ARM_VCPU_POWER_OFF		0 /* CPU is started in OFF state */
 #define KVM_ARM_VCPU_EL1_32BIT		1 /* CPU running a 32bit VM */
+#define KVM_ARM_VCPU_PSCI_0_2		2 /* CPU uses PSCI v0.2 */
 
 struct kvm_vcpu_init {
 	__u32 target;
@@ -186,10 +189,10 @@ struct kvm_arch_memory_slot {
 #define KVM_PSCI_FN_CPU_ON		KVM_PSCI_FN(2)
 #define KVM_PSCI_FN_MIGRATE		KVM_PSCI_FN(3)
 
-#define KVM_PSCI_RET_SUCCESS		0
-#define KVM_PSCI_RET_NI			((unsigned long)-1)
-#define KVM_PSCI_RET_INVAL		((unsigned long)-2)
-#define KVM_PSCI_RET_DENIED		((unsigned long)-3)
+#define KVM_PSCI_RET_SUCCESS		PSCI_RET_SUCCESS
+#define KVM_PSCI_RET_NI			PSCI_RET_NOT_SUPPORTED
+#define KVM_PSCI_RET_INVAL		PSCI_RET_INVALID_PARAMS
+#define KVM_PSCI_RET_DENIED		PSCI_RET_DENIED
 
 #endif
 
linux-headers/asm-mips/kvm.h +35 -0
index f09ff5a..2c04b6d 100644
@@ -106,6 +106,41 @@ struct kvm_fpu {
 #define KVM_REG_MIPS_LO (KVM_REG_MIPS | KVM_REG_SIZE_U64 | 33)
 #define KVM_REG_MIPS_PC (KVM_REG_MIPS | KVM_REG_SIZE_U64 | 34)
 
+/* KVM specific control registers */
+
+/*
+ * CP0_Count control
+ * DC:    Set 0: Master disable CP0_Count and set COUNT_RESUME to now
+ *        Set 1: Master re-enable CP0_Count with unchanged bias, handling timer
+ *               interrupts since COUNT_RESUME
+ *        This can be used to freeze the timer to get a consistent snapshot of
+ *        the CP0_Count and timer interrupt pending state, while also resuming
+ *        safely without losing time or guest timer interrupts.
+ * Other: Reserved, do not change.
+ */
+#define KVM_REG_MIPS_COUNT_CTL		(KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
+					 0x20000 | 0)
+#define KVM_REG_MIPS_COUNT_CTL_DC	0x00000001
+
+/*
+ * CP0_Count resume monotonic nanoseconds
+ * The monotonic nanosecond time of the last set of COUNT_CTL.DC (master
+ * disable). Any reads and writes of Count related registers while
+ * COUNT_CTL.DC=1 will appear to occur at this time. When COUNT_CTL.DC is
+ * cleared again (master enable) any timer interrupts since this time will be
+ * emulated.
+ * Modifications to times in the future are rejected.
+ */
+#define KVM_REG_MIPS_COUNT_RESUME	(KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
+					 0x20000 | 1)
+/*
+ * CP0_Count rate in Hz
+ * Specifies the rate of the CP0_Count timer in Hz. Modifications occur without
+ * discontinuities in CP0_Count.
+ */
+#define KVM_REG_MIPS_COUNT_HZ		(KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
+					 0x20000 | 2)
+
 /*
  * KVM MIPS specific structures and definitions
  *
linux-headers/asm-powerpc/kvm.h +1 -1
index a6665be..2bc4a94 100644
@@ -545,7 +545,6 @@ struct kvm_get_htab_header {
 #define KVM_REG_PPC_TCSCR	(KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb1)
 #define KVM_REG_PPC_PID		(KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb2)
 #define KVM_REG_PPC_ACOP	(KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb3)
-#define KVM_REG_PPC_WORT	(KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb4)
 
 #define KVM_REG_PPC_VRSAVE	(KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb4)
 #define KVM_REG_PPC_LPCR	(KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb5)
@@ -555,6 +554,7 @@ struct kvm_get_htab_header {
 #define KVM_REG_PPC_ARCH_COMPAT	(KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb7)
 
 #define KVM_REG_PPC_DABRX	(KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb8)
+#define KVM_REG_PPC_WORT	(KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xb9)
 
 /* Transactional Memory checkpointed state:
  * This is all GPRs, all VSX regs and a subset of SPRs
linux-headers/asm-powerpc/kvm_para.h +6 -0
index 7e64f57..2abcc46 100644
@@ -82,10 +82,16 @@ struct kvm_vcpu_arch_shared {
 
 #define KVM_FEATURE_MAGIC_PAGE	1
 
+/* Magic page flags from host to guest */
+
 #define KVM_MAGIC_FEAT_SR		(1 << 0)
 
 /* MASn, ESR, PIR, and high SPRGs */
 #define KVM_MAGIC_FEAT_MAS0_TO_SPRG7	(1 << 1)
 
+/* Magic page flags from guest to host */
+
+#define MAGIC_PAGE_FLAG_NOT_MAPPED_NX	(1 << 0)
+
 
 #endif /* __POWERPC_KVM_PARA_H__ */
linux-headers/linux/kvm.h +10 -0
index 42ddc2c..f5d2c38 100644
@@ -171,6 +171,7 @@ struct kvm_pit_config {
 #define KVM_EXIT_WATCHDOG         21
 #define KVM_EXIT_S390_TSCH        22
 #define KVM_EXIT_EPR              23
+#define KVM_EXIT_SYSTEM_EVENT     24
 
 /* For KVM_EXIT_INTERNAL_ERROR */
 /* Emulate instruction failed. */
@@ -301,6 +302,13 @@ struct kvm_run {
 		struct {
 			__u32 epr;
 		} epr;
+		/* KVM_EXIT_SYSTEM_EVENT */
+		struct {
+#define KVM_SYSTEM_EVENT_SHUTDOWN       1
+#define KVM_SYSTEM_EVENT_RESET          2
+			__u32 type;
+			__u64 flags;
+		} system_event;
 		/* Fix the size of the union. */
 		char padding[256];
 	};
@@ -748,6 +756,8 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_S390_IRQCHIP 99
 #define KVM_CAP_IOEVENTFD_NO_LENGTH 100
 #define KVM_CAP_VM_ATTRIBUTES 101
+#define KVM_CAP_ARM_PSCI_0_2 102
+#define KVM_CAP_PPC_FIXUP_HCALL 103
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
linux-headers/linux/psci.h new file +90 -0

90 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#5 lore raw

PATCH 3/4 KVM: PPC: Expose fixup hcall capability

In reply to Alexander Graf

New kvm versions expose a PPC_FIXUP_HCALL capability. Make it visible to machine code so we can take decisions based on it.

---
 target-ppc/kvm.c     | 7 +++++++
 target-ppc/kvm_ppc.h | 6 ++++++
 2 files changed, 13 insertions(+)
target-ppc/kvm.c +7 -0
index e6a1625..ef691fe 100644
@@ -69,6 +69,7 @@ static int cap_epr;
 static int cap_ppc_watchdog;
 static int cap_papr;
 static int cap_htab_fd;
+static int cap_fixup_hcalls;
 
 /* XXX We have a race condition where we actually have a level triggered
  *     interrupt, but the infrastructure can't expose that yet, so the guest
@@ -107,6 +108,7 @@ int kvm_arch_init(KVMState *s)
     /* Note: we don't set cap_papr here, because this capability is
      * only activated after this by kvmppc_set_papr() */
     cap_htab_fd = kvm_check_extension(s, KVM_CAP_PPC_HTAB_FD);
+    cap_fixup_hcalls = kvm_check_extension(s, KVM_CAP_PPC_FIXUP_HCALL);
 
     if (!cap_interrupt_level) {
         fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the "
@@ -1780,6 +1782,11 @@ bool kvmppc_has_cap_htab_fd(void)
     return cap_htab_fd;
 }
 
+bool kvmppc_has_cap_fixup_hcalls(void)
+{
+    return cap_fixup_hcalls;
+}
+
 static PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
 {
     ObjectClass *oc = OBJECT_CLASS(pcc);
target-ppc/kvm_ppc.h +6 -0
index 5ea209a..412cc7f 100644
@@ -50,6 +50,7 @@ void kvmppc_hash64_free_pteg(uint64_t token);
 
 void kvmppc_hash64_write_pte(CPUPPCState *env, target_ulong pte_index,
                              target_ulong pte0, target_ulong pte1);
+bool kvmppc_has_cap_fixup_hcalls(void);
 
 #else
 
@@ -223,6 +224,11 @@ static inline void kvmppc_hash64_write_pte(CPUPPCState *env,
     abort();
 }
 
+static inline bool kvmppc_has_cap_fixup_hcalls(void)
+{
+    abort();
+}
+
 #endif
 
 #ifndef CONFIG_KVM
Signature · 1 line
1.8.1.4
#6 lore raw

PATCH 4/4 PPC: spapr: Expose /hypervisor node in device tree

In reply to Alexander Graf

PR KVM supports an ePAPR compliant hypercall interface in parallel to the normal sPAPR one. Expose the ePAPR /hypervisor node and properties to the guest so it can use it.

This enables magic page sharing on PR KVM with -M pseries.

However we had a few nasty bugs in the magic page implementation on vcpus newer than 970 (p7, p8) that KVM now has workarounds for. It indicates that it does have these workarounds through the PPC_FIXUP_HCALL capability.

To not expose broken guest kernels to issues on host kernels that don't have the fixups in place, we don't expose working hypercall instructions when the fixups are not available so that the guest can never active the magic page.

---
 hw/ppc/spapr.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
hw/ppc/spapr.c +20 -0
index 20fbc59..df72380 100644
@@ -585,6 +585,26 @@ static void *spapr_create_fdt_skel(hwaddr initrd_base,
     /* event-sources */
     spapr_events_fdt_skel(fdt, epow_irq);
 
+    /* /hypervisor node */
+    if (kvm_enabled()) {
+        uint8_t hypercall[16];
+
+        /* indicate KVM hypercall interface */
+        _FDT((fdt_begin_node(fdt, "hypervisor")));
+        _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
+        if (kvmppc_has_cap_fixup_hcalls()) {
+            /*
+             * Older KVM versions with older guest kernels were broken with the
+             * magic page, don't allow the guest to map it.
+             */
+            kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
+                                 sizeof(hypercall));
+            _FDT((fdt_property(fdt, "hcall-instructions", hypercall,
+                              sizeof(hypercall))));
+        }
+        _FDT((fdt_end_node(fdt)));
+    }
+
     _FDT((fdt_end_node(fdt))); /* close root node */
     _FDT((fdt_finish(fdt)));
 
Signature · 1 line
1.8.1.4