Skip to contentSkip to conversation

kvm/powerpc: Add new ioctl to retreive server MMU infos

PATCH Applied Benjamin Herrenschmidt 3 participants 9 messages Apr 27, 2012 – May 10, 2012

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm/powerpc: Add new ioctl to retreive server MMU infos

This is necessary for qemu to be able to pass the right information to the guest, such as the supported page sizes and corresponding encodings in the SLB and hash table, which can vary depending on the processor type, the type of KVM used (PR vs HV) and the version of KVM

---

Alex, can you sync with Avi ASAP on locking the ioctl & capability numbers ? The patch itself can wait til the next merge window but at least we want those locked in so we can push the qemu bits.

The qemu patch proper has fallbacks for when the kernel doesn't support the ioctl (I'll post it separately)

For example I know the ioctl/caps already collide with Paulus other patches so we need that sorted at Avi level.

 Documentation/virtual/kvm/api.txt  |   70 ++++++++++++++++++++++++++++++++++++
 arch/powerpc/include/asm/kvm_ppc.h |    3 +-
 arch/powerpc/kvm/book3s_hv.c       |   33 +++++++++++++++++
 arch/powerpc/kvm/book3s_pr.c       |   25 +++++++++++++
 arch/powerpc/kvm/powerpc.c         |   18 +++++++++-
 include/linux/kvm.h                |   27 ++++++++++++++
 6 files changed, 174 insertions(+), 2 deletions(-)
Documentation/virtual/kvm/api.txt +70 -0
index 81ff39f..e48b95d 100644
@@ -1689,6 +1689,76 @@ where the guest will clear the flag: when the soft lockup watchdog timer resets
 itself or when a soft lockup is detected.  This ioctl can be called any time
 after pausing the vcpu, but before it is resumed.
 
+4.71 KVM_PPC_GET_SMMU_INFO
+
+Capability: KVM_CAP_PPC_GET_SMMU_INFO
+Architectures: powerpc
+Type: vm ioctl
+Parameters: None
+Returns: 0 on success, -1 on error
+
+This populates and returns a structure describing the features of
+server "Server" class MMU emulation supported by KVM.
+This can in turn be used by userspace to generate the appropariate
+device-tree properties for the guest operating system.
+
+The structure contains some global informations, followed by an
+array of supported segment page sizes:
+
+      struct kvm_ppc_smmu_info {
+	     __u64 flags;
+	     __u32 slb_size;
+	     __u32 pad;
+	     struct kvm_ppc_one_seg_page_size sps[KVM_PPC_PAGE_SIZES_MAX_SZ];
+      };
+
+The supported flags are:
+
+    - KVM_PPC_PAGE_SIZES_REAL:
+        When that flag is set, guest page sizes must "fit" the backing
+        store page sizes. When not set, any page size in the list can
+        be used regardless of how they are backed by userspace.
+
+    - KVM_PPC_1T_SEGMENTS
+        The emulated MMU supports 1T segments in addition to the
+        standard 256M ones.
+
+The "slb_size" field indicates how many SLB entries are supported
+
+The "sps" array contains 8 entries indicating the supported base
+page sizes for a segment in increasing order. Each entry is defined
+as follow:
+
+   struct kvm_ppc_one_seg_page_size {
+	__u32 page_shift;	/* Base page shift of segment (or 0) */
+	__u32 slb_enc;		/* SLB encoding for BookS */
+	struct kvm_ppc_one_page_size enc[KVM_PPC_PAGE_SIZES_MAX_SZ];
+   };
+
+An entry with a "page_shift" of 0 is unused. Because the array is
+organized in increasing order, a lookup can stop when encoutering
+such an entry.
+
+The "slb_enc" field provides the encoding to use in the SLB for the
+page size. The bits are in positions such as the value can directly
+be OR'ed into the "vsid" argument of the slbmte instruction.
+
+The "enc" array is a list which for each of those segment base page
+size provides the list of supported actual page sizes (which can be
+only larger or equal to the base page size), along with the
+corresponding encoding in the hash PTE. Similarily, the array is
+8 entries sorted by increasing sizes and an entry with a "0" shift
+is an empty entry and a terminator:
+
+   struct kvm_ppc_one_page_size {
+	__u32 page_shift;	/* Page shift (or 0) */
+	__u32 pte_enc;		/* Encoding in the HPTE (>>12) */
+   };
+
+The "pte_enc" field provides a value that can OR'ed into the hash
+PTE's RPN field (ie, it needs to be shifted left by 12 to OR it
+into the hash PTE second double word).
+
 5. The kvm_run structure
 
 Application code obtains a pointer to the kvm_run structure by
arch/powerpc/include/asm/kvm_ppc.h +2 -1
index c1069f6..2a92327 100644
@@ -140,7 +140,8 @@ extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
 				struct kvm_userspace_memory_region *mem);
 extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
 				struct kvm_userspace_memory_region *mem);
-
+extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
+				      struct kvm_ppc_smmu_info *info);
 extern int kvmppc_bookehv_init(void);
 extern void kvmppc_bookehv_exit(void);
 
arch/powerpc/kvm/book3s_hv.c +33 -0
index 8ee46b9..7c6d267 100644
@@ -1174,6 +1174,39 @@ long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
 	return fd;
 }
 
+static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
+				     int linux_psize)
+{
+	struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
+
+	if (!def->shift)
+		return;
+	*sps->page_shift = def->shift;
+	*sps->slb_enc = def->sllp;
+	*sps->enc[0].page_shift = def->shift;
+	*sps->enc[0].pte_enc = def->penc;
+	*sps++;
+}
+
+int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
+{
+	struct kvm_ppc_one_seg_page_size *sps;
+	int i;
+
+	info->flags = KVM_PPC_PAGE_SIZES_REAL;
+	if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
+		info->flags |= KVM_PPC_1T_SEGMENTS;
+	info->slb_size = mmu_slb_size;
+
+	/* We only support these sizes for now, and no muti-size segments */
+	sps = &sinfo->sps[0];
+	kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
+	kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
+	kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
+
+	return 0;
+}
+
 /*
  * Get (and clear) the dirty memory log for a memory slot.
  */
arch/powerpc/kvm/book3s_pr.c +25 -0
index 5f0ee48..70d9ee6 100644
@@ -1154,6 +1154,31 @@ out:
 	return r;
 }
 
+#ifdef CONFIG_PPC64
+int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
+{
+	/* No flags */
+	info->flags = 0;
+
+	/* SLB is always 64 entries */
+	info->slb_size = 64;
+
+	/* Standard 4k base page size segment */
+	info->sps[0].page_shift = 12;
+	info->sps[0].slb_enc = 0;
+	info->sps[0].enc[0].page_shift = 12;
+	info->sps[0].enc[0].pte_enc = 0;
+
+	/* Standard 16M large page size segment */
+	info->sps[1].page_shift = 24;
+	info->sps[1].slb_enc = SLB_VSID_L;
+	info->sps[1].enc[0].page_shift = 24;
+	info->sps[1].enc[0].pte_enc = 0;
+
+	return 0;
+}
+#endif /* CONFIG_PPC64 */
+
 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
 				      struct kvm_userspace_memory_region *mem)
 {
arch/powerpc/kvm/powerpc.c +17 -1
index 6ac3115..1493c8d 100644
@@ -279,6 +279,11 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_MAX_VCPUS:
 		r = KVM_MAX_VCPUS;
 		break;
+#ifdef CONFIG_PPC_BOOK3S_64
+	case KVM_CAP_PPC_GET_SMMU_INFO:
+		r = 1;
+		break;
+#endif
 	default:
 		r = 0;
 		break;
@@ -718,7 +723,6 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 		break;
 	}
 #endif
-
 	default:
 		r = -EINVAL;
 	}
@@ -800,6 +804,18 @@ long kvm_arch_vm_ioctl(struct file *filp,
 	}
 #endif /* CONFIG_KVM_BOOK3S_64_HV */
 
+#ifdef CONFIG_PPC_BOOK3S_64
+	case KVM_PPC_GET_SMMU_INFO: {
+		struct kvm *kvm = filp->private_data;
+		struct kvm_ppc_smmu_info info;
+
+		memset(&info, 0, sizeof(info));
+		r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
+		if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
+			r = -EFAULT;
+		break;
+	}
+#endif /* CONFIG_PPC_BOOK3S_64 */
 	default:
 		r = -ENOTTY;
 	}
include/linux/kvm.h +27 -0
index 7a9dd4b..aa60473 100644
@@ -449,6 +449,30 @@ struct kvm_ppc_pvinfo {
 	__u8  pad[108];
 };
 
+/* for KVM_PPC_GET_SMMU_INFO */
+#define KVM_PPC_PAGE_SIZES_MAX_SZ	8
+
+struct kvm_ppc_one_page_size {
+	__u32 page_shift;	/* Page shift (or 0) */
+	__u32 pte_enc;		/* Encoding in the HPTE (>>12) */
+};
+
+struct kvm_ppc_one_seg_page_size {
+	__u32 page_shift;	/* Base page shift of segment (or 0) */
+	__u32 slb_enc;		/* SLB encoding for BookS */
+	struct kvm_ppc_one_page_size enc[KVM_PPC_PAGE_SIZES_MAX_SZ];
+};
+
+#define KVM_PPC_PAGE_SIZES_REAL		0x00000001
+#define KVM_PPC_1T_SEGMENTS		0x00000002
+
+struct kvm_ppc_smmu_info {
+	__u64 flags;
+	__u32 slb_size;
+	__u32 pad;
+	struct kvm_ppc_one_seg_page_size sps[KVM_PPC_PAGE_SIZES_MAX_SZ];
+};
+
 #define KVMIO 0xAE
 
 /* machine type bits, to be used as argument to KVM_CREATE_VM */
@@ -590,6 +614,7 @@ struct kvm_ppc_pvinfo {
 #define KVM_CAP_SYNC_REGS 74
 #define KVM_CAP_PCI_2_3 75
 #define KVM_CAP_KVMCLOCK_CTRL 76
+#define KVM_CAP_PPC_GET_SMMU_INFO 77
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -789,6 +814,8 @@ struct kvm_s390_ucas_mapping {
 /* Available with KVM_CAP_PCI_2_3 */
 #define KVM_ASSIGN_SET_INTX_MASK  _IOW(KVMIO,  0xa4, \
 				       struct kvm_assigned_pci_dev)
+/* Available with KVM_CAP_PPC_GET_SMMU_INFO */
+#define KVM_PPC_GET_SMMU_INFO	  _IOR(KVMIO,  0xa5, struct kvm_ppc_smmu_info)
 
 /*
  * ioctls for vcpu fds
#1 lore raw

PATCH 1/2 ppc64: Rudimentary Support for extra page sizes on server CPUs

More recent Power server chips (i.e. based on the 64 bit hash MMU) support more than just the traditional 4k and 16M page sizes. This can get quite complicated, because which page sizes are supported, which combinations are supported within an MMU segment and how these page sizes are encoded both in the SLB entry and the hash PTE can vary depending on the CPU model (they are not specified by the architecture). In addition the firmware or hypervisor may not permit use of certain page sizes, for various reasons. Whether various page sizes are supported on KVM, for example, depends on whether the PR or HV variant of KVM is in use, and on the page size of the memory backing the guest's RAM.

This patch adds information to the CPUState and cpu defs to describe the supported page sizes and encodings. Since TCG does not yet support any extended page sizes, we just set this to NULL in the static CPU definitions, expanding this to the default 4k and 16M page sizes when we initialize the cpu state. When using KVM, however, we instead determine available page sizes using the new KVM_PPC_GET_SMMU_INFO call. For old kernels without that call, we use some defaults, with some guesswork which should do the right thing for existing HV and PR implementations. The fallback might not be correct for future versions, but that's ok, because they'll have KVM_PPC_GET_SMMU_INFO.

---

This patch, to compile, requires a linux-headers/linux/kvm.h which has the new capabilities and ioctl's defined (ie, from the kernel side patch I posted earlier). This means getting the numbers locked down with Avi

target-ppc/cpu.h +29 -0
index 84c9674..d5891e4 100644
@@ -874,6 +874,29 @@ enum {
 #define DBELL_PIRTAG_MASK              0x3fff
 
 /*****************************************************************************/
+/* Segment page size information, used by recent hash MMUs
+ * The format of this structure mirrors kvm_ppc_smmu_info
+ */
+
+#define PPC_PAGE_SIZES_MAX_SZ	8
+
+struct ppc_one_page_size {
+    uint32_t page_shift;  /* Page shift (or 0) */
+    uint32_t pte_enc;	  /* Encoding in the HPTE (>>12) */
+};
+
+struct ppc_one_seg_page_size {
+    uint32_t page_shift;  /* Base page shift of segment (or 0) */
+    uint32_t slb_enc;     /* SLB encoding for BookS */
+    struct ppc_one_page_size enc[PPC_PAGE_SIZES_MAX_SZ];
+};
+
+struct ppc_segment_page_sizes {
+    struct ppc_one_seg_page_size sps[PPC_PAGE_SIZES_MAX_SZ];
+};
+
+
+/*****************************************************************************/
 /* The whole PowerPC CPU context */
 #define NB_MMU_MODES 3
 
@@ -889,6 +912,9 @@ struct ppc_def_t {
     powerpc_input_t bus_model;
     uint32_t flags;
     int bfd_mach;
+#if defined(TARGET_PPC64)
+    const struct ppc_segment_page_sizes *sps;
+#endif
     void (*init_proc)(CPUPPCState *env);
     int  (*check_pow)(CPUPPCState *env);
 };
@@ -1012,6 +1038,9 @@ struct CPUPPCState {
     uint32_t flags;
     uint64_t insns_flags;
     uint64_t insns_flags2;
+#if defined(TARGET_PPC64)
+    struct ppc_segment_page_sizes sps;
+#endif
 
 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
     target_phys_addr_t vpa;
target-ppc/kvm.c +199 -0

215 lines not on this page · raw message

target-ppc/kvm_ppc.h +5 -0
index 34ecad3..e2f8703 100644
@@ -58,6 +58,11 @@ static inline int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_l
     return -1;
 }
 
+static inline int kvmppc_read_segment_page_sizes(uint32_t *prop, int maxcells)
+{
+    return -1;
+}
+
 static inline int kvmppc_set_interrupt(CPUPPCState *env, int irq, int level)
 {
     return -1;
target-ppc/translate_init.c +21 -0
index ba4b84d..6b5079d 100644
@@ -9926,6 +9926,27 @@ int cpu_ppc_register_internal (CPUPPCState *env, const ppc_def_t *def)
     env->bfd_mach = def->bfd_mach;
     env->check_pow = def->check_pow;
 
+#if defined(TARGET_PPC64)
+    if (def->sps)
+        memcpy(&env->sps, def->sps, sizeof(*def->sps));
+    else if (env->mmu_model & POWERPC_MMU_64) {
+        /* Use default sets of page sizes */
+        static const struct ppc_segment_page_sizes defsps = {
+            .sps = {
+                { .page_shift = 12, /* 4K */
+                  .slb_enc = 0,
+                  .enc = { { .page_shift = 12, .pte_enc = 0 } }
+                },
+                { .page_shift = 24, /* 16M */
+                  .slb_enc = 0x100,
+                  .enc = { { .page_shift = 24, .pte_enc = 0 } }
+                },
+            },
+        };
+        memcpy(&env->sps, &defsps, sizeof(defsps));
+    }
+#endif /* defined(TARGET_PPC64) */
+
     if (kvm_enabled()) {
         if (kvmppc_fixup_cpu(env) != 0) {
             fprintf(stderr, "Unable to virtualize selected CPU with KVM\n");
#2 lore raw

Re: [PATCH 1/2] ppc64: Rudimentary Support for extra page sizes on server CPUs

Quoted 228 lines · Benjamin Herrenschmidt

More recent Power server chips (i.e. based on the 64 bit hash MMU) support more than just the traditional 4k and 16M page sizes. This can get quite complicated, because which page sizes are supported, which combinations are supported within an MMU segment and how these page sizes are encoded both in the SLB entry and the hash PTE can vary depending on the CPU model (they are not specified by the architecture). In addition the firmware or hypervisor may not permit use of certain page sizes, for various reasons. Whether various page sizes are supported on KVM, for example, depends on whether the PR or HV variant of KVM is in use, and on the page size of the memory backing the guest's RAM.

This patch adds information to the CPUState and cpu defs to describe the supported page sizes and encodings. Since TCG does not yet support any extended page sizes, we just set this to NULL in the static CPU definitions, expanding this to the default 4k and 16M page sizes when we initialize the cpu state. When using KVM, however, we instead determine available page sizes using the new KVM_PPC_GET_SMMU_INFO call. For old kernels without that call, we use some defaults, with some guesswork which should do the right thing for existing HV and PR implementations. The fallback might not be correct for future versions, but that's ok, because they'll have KVM_PPC_GET_SMMU_INFO.

---

This patch, to compile, requires a linux-headers/linux/kvm.h which has the new capabilities and ioctl's defined (ie, from the kernel side patch I posted earlier). This means getting the numbers locked down with Avi

target-ppc/cpu.h +29 -0
index 84c9674..d5891e4 100644
@@ -874,6 +874,29 @@ enum {
  #define DBELL_PIRTAG_MASK              0x3fff
 
  /*****************************************************************************/
+/* Segment page size information, used by recent hash MMUs
+ * The format of this structure mirrors kvm_ppc_smmu_info
+ */
+
+#define PPC_PAGE_SIZES_MAX_SZ	8
+
+struct ppc_one_page_size {
+    uint32_t page_shift;  /* Page shift (or 0) */
+    uint32_t pte_enc;	  /* Encoding in the HPTE (>>12) */
+};
+
+struct ppc_one_seg_page_size {
+    uint32_t page_shift;  /* Base page shift of segment (or 0) */
+    uint32_t slb_enc;     /* SLB encoding for BookS */
+    struct ppc_one_page_size enc[PPC_PAGE_SIZES_MAX_SZ];
+};
+
+struct ppc_segment_page_sizes {
+    struct ppc_one_seg_page_size sps[PPC_PAGE_SIZES_MAX_SZ];
+};
+
+
+/*****************************************************************************/
  /* The whole PowerPC CPU context */
  #define NB_MMU_MODES 3
 
@@ -889,6 +912,9 @@ struct ppc_def_t {
      powerpc_input_t bus_model;
      uint32_t flags;
      int bfd_mach;
+#if defined(TARGET_PPC64)
+    const struct ppc_segment_page_sizes *sps;
+#endif
      void (*init_proc)(CPUPPCState *env);
      int  (*check_pow)(CPUPPCState *env);
  };
@@ -1012,6 +1038,9 @@ struct CPUPPCState {
      uint32_t flags;
      uint64_t insns_flags;
      uint64_t insns_flags2;
+#if defined(TARGET_PPC64)
+    struct ppc_segment_page_sizes sps;
+#endif
 
  #if defined(TARGET_PPC64)&&  !defined(CONFIG_USER_ONLY)
      target_phys_addr_t vpa;
target-ppc/kvm.c +126 -0
index 0ab7630..77aa186 100644
@@ -18,6 +18,7 @@
  #include<sys/types.h>
  #include<sys/ioctl.h>
  #include<sys/mman.h>
+#include<sys/vfs.h>
 
  #include<linux/kvm.h>
 
@@ -167,10 +168,208 @@ static int kvm_booke206_tlb_init(CPUPPCState *env)
      return 0;
  }
 
+
+#if defined (TARGET_PPC64)
+static void kvm_get_fallback_smmu_info(CPUPPCState *env,
+                                       struct kvm_ppc_smmu_info *info)
+{
+    memset(info, 0, sizeof(*info));
+
+    /* We don't have the new KVM_PPC_GET_SMMU_INFO ioctl, so
+     * need to "guess" what the supported page sizes are.
+     *
+     * For that to work we make a few assumptions:
+     *
+     * - If KVM_CAP_PPC_GET_PVINFO is supported we are running "PR"
+     *   KVM which only supports 4K and 16M pages, but supports them
+     *   regardless of the backing store characteritics. We also don't
+     *   support 1T segments.
+     *
+     *   This is safe as if HV KVM ever supports that capability or PR
+     *   KVM grows supports for more page/segment sizes, those versions
+     *   will have implemented KVM_CAP_PPC_GET_SMMU_INFO and thus we
+     *   will not hit this fallback
+     *
+     * - Else we are running HV KVM. This means we only support page
+     *   sizes that fit in the backing store. Additionally we only
+     *   advertize 64K pages if the processor is ARCH 2.06 and we assume
+     *   P7 encodings for the SLB and hash table. Here too, we assume
+     *   support for any newer processor will mean a kernel that
+     *   implements KVM_CAP_PPC_GET_SMMU_INFO and thus doesn't hit
+     *   this fallback.
+     */
+    if (kvm_check_extension(env->kvm_state, KVM_CAP_PPC_GET_PVINFO)) {
+        /* No flags */
+        info->flags = 0;
+
+        /* Standard 4k base page size segment */
+        info->sps[0].page_shift = 12;
+        info->sps[0].slb_enc = 0;
+        info->sps[0].enc[0].page_shift = 12;
+        info->sps[0].enc[0].pte_enc = 0;
+
+        /* Standard 16M large page size segment */
+        info->sps[1].page_shift = 24;
+        info->sps[1].slb_enc = SLB_VSID_L;
+        info->sps[1].enc[0].page_shift = 24;
+        info->sps[1].enc[0].pte_enc = 0;
+    } else {
+	int i = 0;
+
+	/* HV KVM has backing store size restrictions */
+        info->flags = KVM_PPC_PAGE_SIZES_REAL;
+
+        if (env->mmu_model&  POWERPC_MMU_1TSEG)
+            info->flags = KVM_PPC_1T_SEGMENTS;
+
+        /* Standard 4k base page size segment */
+        info->sps[i].page_shift = 12;
+        info->sps[i].slb_enc = 0;
+        info->sps[i].enc[0].page_shift = 12;
+        info->sps[i].enc[0].pte_enc = 0;
+	i++;
+
+        /* 64K on MMU 2.06 */
+        if (env->mmu_model == POWERPC_MMU_2_06) {
+            info->sps[i].page_shift = 16;
+            info->sps[i].slb_enc = 0x110;
+            info->sps[i].enc[0].page_shift = 16;
+            info->sps[i].enc[0].pte_enc = 1;
+	    i++;
+	}
+
+        /* Standard 16M large page size segment */
+        info->sps[i].page_shift = 24;
+        info->sps[i].slb_enc = SLB_VSID_L;
+        info->sps[i].enc[0].page_shift = 24;
+        info->sps[i].enc[0].pte_enc = 0;
+    }
+}
+
+static void kvm_get_smmu_info(CPUPPCState *env, struct kvm_ppc_smmu_info *info)
+{
+    int ret;
+
+    if (kvm_check_extension(env->kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) {
+        ret = kvm_vm_ioctl(env->kvm_state, KVM_PPC_GET_SMMU_INFO, info);
+        if (ret == 0) {
+            return;
+        }
+    }
+
+    kvm_get_fallback_smmu_info(env, info);
+}
+
+static long getrampagesize(void)
+{
+    struct statfs fs;
+    int ret;
+
+    if (!mem_path) {
+        /* guest RAM is backed by normal anonymous pages */
+        return getpagesize();
+    }
+
+    do {
+        ret = statfs(mem_path,&fs);
+    } while (ret != 0&&  errno == EINTR);
+
+    if (ret != 0) {
+        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
+                strerror(errno));
+        exit(1);
+    }
+
+#define HUGETLBFS_MAGIC       0x958458f6
+
+    if (fs.f_type != HUGETLBFS_MAGIC) {
+        /* Explicit mempath, but it's ordinary pages */
+        return getpagesize();
+    }
+
+    /* It's hugepage, return the huge page size */
+    return fs.f_bsize;
+}
+
+static bool kvm_valid_page_size(uint32_t flags, long rampgsize, uint32_t shift)
+{

target-ppc/kvm.c static int kvm_booke206_tlb_init(CPUPPCState *env) · line 305

Quoted 10 lines
target-ppc/kvm.c +10 -0
+    if (!(flags&  KVM_PPC_PAGE_SIZES_REAL))
+        return true;
+
+    return (1ul<<  shift)<= rampgsize;
+}
+
+static void kvm_fixup_page_sizes(CPUPPCState *env)
+{
+    static struct kvm_ppc_smmu_info smmu_info;
+    static bool has_smmu_info;

Do we have to cache these? This is not exactly a fast path...

Quoted 35 lines
target-ppc/kvm.c +35 -0
+    long rampagesize;
+    int iq, ik, jq, jk;
+
+    /* We only handle page sizes for 64-bit server guests for now */
+    if (!(env->mmu_model&  POWERPC_MMU_64)) {
+        return;
+    }
+
+    /* Collect MMU info from kernel if not already */
+    if (!has_smmu_info) {
+        kvm_get_smmu_info(env,&smmu_info);
+        has_smmu_info = true;
+    }
+
+    rampagesize = getrampagesize();
+
+    /* Convert to QEMU form */
+    memset(&env->sps, 0, sizeof(env->sps));
+
+    for (ik = iq = 0; ik<  KVM_PPC_PAGE_SIZES_MAX_SZ; ik++) {
+        struct ppc_one_seg_page_size *qsps =&env->sps.sps[iq];
+        struct kvm_ppc_one_seg_page_size *ksps =&smmu_info.sps[ik];
+
+        if (!kvm_valid_page_size(smmu_info.flags, rampagesize,
+                                 ksps->page_shift)) {
+            continue;
+        }
+        qsps->page_shift = ksps->page_shift;
+        qsps->slb_enc = ksps->slb_enc;
+        for (jk = jq = 0; jk<  KVM_PPC_PAGE_SIZES_MAX_SZ; jk++) {
+            if (!kvm_valid_page_size(smmu_info.flags, rampagesize,
+                                     ksps->enc[jk].page_shift)) {
+                continue;
+            }
+            qsps->enc[jq].page_shift = ksps->enc[jk].page_shift;

target-ppc/kvm.c static int kvm_booke206_tlb_init(CPUPPCState *env) · line 350

Quoted 10 lines
target-ppc/kvm.c +10 -0
+            qsps->enc[jq].pte_enc = ksps->enc[jk].pte_enc;
+            if (++jq>= PPC_PAGE_SIZES_MAX_SZ) {
+                break;
+            }
+        }
+        if (++iq>= PPC_PAGE_SIZES_MAX_SZ) {
+            break;
+        }
+    }
+    env->slb_nr = smmu_info.slb_size;

What happens in the fallback case here?

Quoted 46 lines
target-ppc/kvm.c +18 -0
+    if (smmu_info.flags&  KVM_PPC_1T_SEGMENTS) {
+        env->mmu_model |= POWERPC_MMU_1TSEG;
+    } else {
+        env->mmu_model&= ~POWERPC_MMU_1TSEG;
+    }
+}
+#else /* defined (TARGET_PPC64) */
+
+static inline void kvm_fixup_page_sizes(CPUPPCState *env)
+{
+}
+
+#endif /* !defined (TARGET_PPC64) */
+
  int kvm_arch_init_vcpu(CPUPPCState *cenv)
  {
      int ret;
 
+    /* Gather server mmu info from KVM and update the CPU state*/
+    kvm_fixup_page_sizes(cenv);
+
+    /* Synchronize sregs with kvm */
      ret = kvm_arch_sync_sregs(cenv);
      if (ret) {
          return ret;
target-ppc/kvm_ppc.h +5 -0
index 34ecad3..e2f8703 100644
@@ -58,6 +58,11 @@ static inline int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_l
      return -1;
  }
 
+static inline int kvmppc_read_segment_page_sizes(uint32_t *prop, int maxcells)
+{
+    return -1;
+}
+
  static inline int kvmppc_set_interrupt(CPUPPCState *env, int irq, int level)
  {
      return -1;
target-ppc/translate_init.c +0 -0
index ba4b84d..6b5079d 100644

target-ppc/translate_init.c int cpu_ppc_register_internal (CPUPPCState *env, const ppc_def_t *def) · line 9931

Quoted 6 lines
target-ppc/translate_init.c +3 -0
@@ -9926,6 +9926,27 @@ int cpu_ppc_register_internal (CPUPPCState *env, const ppc_def_t *def)
      env->bfd_mach = def->bfd_mach;
      env->check_pow = def->check_pow;
 
+#if defined(TARGET_PPC64)
+    if (def->sps)
+        memcpy(&env->sps, def->sps, sizeof(*def->sps));

I never know if *def->... would dereference def or the complete construct. How about sizeof(env->sps)?

Alex

Quoted 27 lines
target-ppc/translate_init.c +18 -1
+    else if (env->mmu_model&  POWERPC_MMU_64) {
+        /* Use default sets of page sizes */
+        static const struct ppc_segment_page_sizes defsps = {
+            .sps = {
+                { .page_shift = 12, /* 4K */
+                  .slb_enc = 0,
+                  .enc = { { .page_shift = 12, .pte_enc = 0 } }
+                },
+                { .page_shift = 24, /* 16M */
+                  .slb_enc = 0x100,
+                  .enc = { { .page_shift = 24, .pte_enc = 0 } }
+                },
+            },
+        };
+        memcpy(&env->sps,&defsps, sizeof(defsps));
+    }
+#endif /* defined(TARGET_PPC64) */
+
      if (kvm_enabled()) {
          if (kvmppc_fixup_cpu(env) != 0) {
              fprintf(stderr, "Unable to virtualize selected CPU with KVM\n");
 
 
--

To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

Re: [PATCH 1/2] ppc64: Rudimentary Support for extra page sizes on server CPUs

Quoted 7 lines · Alexander Graf

target-ppc/translate_init.c int cpu_ppc_register_internal (CPUPPCState *env, const ppc_def_t *def) · line 9931

Quoted 3 lines
target-ppc/translate_init.c +3 -0
+#if defined(TARGET_PPC64)
+    if (def->sps)
+        memcpy(&env->sps, def->sps, sizeof(*def->sps));

I never know if *def->... would dereference def or the complete construct.

'man operator'

Quoted 1 line

How about sizeof(env->sps)?

How about

  env->sps = *def->sps;

?

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

Re: [PATCH 1/2] ppc64: Rudimentary Support for extra page sizes on server CPUs

Quoted 9 lines · Avi Kivity
Quoted 7 lines · Alexander Graf

target-ppc/translate_init.c int cpu_ppc_register_internal (CPUPPCState *env, const ppc_def_t *def) · line 9931

Quoted 3 lines
target-ppc/translate_init.c +3 -0
+#if defined(TARGET_PPC64)
+    if (def->sps)
+        memcpy(&env->sps, def->sps, sizeof(*def->sps));

I never know if *def->... would dereference def or the complete construct.

Quoted 7 lines

'man operator'

Quoted 1 line

How about sizeof(env->sps)?

How about

  env->sps = *def->sps;

Yeah, latter looks better doesn't it ? :-)

I'll change that when I respin.

Cheers,
Ben.

#5 lore raw

PATCH 2/2 pseries: Correctly create ibm,segment-page-sizes property

In reply to Benjamin Herrenschmidt

The core tcg/kvm code for ppc64 now has at least the outline capability to support pagesizes beyond the standard 4k and 16MB. The CPUState is initialized with information advertising the available pagesizes and their correct encodings, and under the right KVM setup this will be populated with page sizes beyond the standard.

Obviously guests can't use the extra page sizes unless they know they're present. For the pseries machine, at least, there is a defined method for conveying exactly this information, the "ibm-segment-page-sizes" property in the guest device tree.

This patch generates this property using the supported page size information that's already in the CPUState.

---
 hw/spapr.c       |   42 ++++++++++++++++++++++++++++++++++++++++++
 target-ppc/kvm.c |   11 +++++++++--
 2 files changed, 51 insertions(+), 2 deletions(-)
hw/spapr.c +42 -0
index 94a4e1e..7c36903 100644
@@ -149,6 +149,39 @@ static int spapr_set_associativity(void *fdt, sPAPREnvironment *spapr)
     return ret;
 }
 
+
+static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
+                                     size_t maxsize)
+{
+    size_t maxcells = maxsize / sizeof(uint32_t);
+    int i, j, count;
+    uint32_t *p = prop;
+
+    for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
+        struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
+
+        if (!sps->page_shift) {
+            break;
+        }
+        for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
+            if (sps->enc[count].page_shift == 0) {
+                break;
+            }
+        }
+        if ((p - prop) >= (maxcells - 3 - count * 2))
+            break;
+        *(p++) = cpu_to_be32(sps->page_shift);
+        *(p++) = cpu_to_be32(sps->slb_enc);
+        *(p++) = cpu_to_be32(count);
+        for (j = 0; j < count; j++) {
+            *(p++) = cpu_to_be32(sps->enc[j].page_shift);
+            *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
+        }
+    }
+
+    return (p - prop) * sizeof(uint32_t);
+}
+
 static void *spapr_create_fdt_skel(const char *cpu_model,
                                    target_phys_addr_t rma_size,
                                    target_phys_addr_t initrd_base,
@@ -304,6 +337,8 @@ static void *spapr_create_fdt_skel(const char *cpu_model,
                            0xffffffff, 0xffffffff};
         uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
         uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
+        uint32_t page_sizes_prop[64];
+        size_t page_sizes_prop_size;
 
         if ((index % smt) != 0) {
             continue;
@@ -368,6 +403,13 @@ static void *spapr_create_fdt_skel(const char *cpu_model,
             _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
         }
 
+        page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
+                                                      sizeof(page_sizes_prop));
+        if (page_sizes_prop_size) {
+            _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
+                               page_sizes_prop, page_sizes_prop_size)));
+        }
+
         _FDT((fdt_end_node(fdt)));
     }
 
target-ppc/kvm.c +9 -2
index 77aa186..860711c 100644
@@ -201,6 +201,7 @@ static void kvm_get_fallback_smmu_info(CPUPPCState *env,
     if (kvm_check_extension(env->kvm_state, KVM_CAP_PPC_GET_PVINFO)) {
         /* No flags */
         info->flags = 0;
+        info->slb_size = 64;
 
         /* Standard 4k base page size segment */
         info->sps[0].page_shift = 12;
@@ -218,9 +219,15 @@ static void kvm_get_fallback_smmu_info(CPUPPCState *env,
 
 	/* HV KVM has backing store size restrictions */
         info->flags = KVM_PPC_PAGE_SIZES_REAL;
+        if (env->mmu_model == POWERPC_MMU_2_06) {
+            info->slb_size = 32;
+        } else {
+            info->slb_size = 64;
+        }
 
-        if (env->mmu_model & POWERPC_MMU_1TSEG)
-            info->flags = KVM_PPC_1T_SEGMENTS;
+        if (env->mmu_model & POWERPC_MMU_1TSEG) {
+            info->flags |= KVM_PPC_1T_SEGMENTS;
+        }
 
         /* Standard 4k base page size segment */
         info->sps[i].page_shift = 12;
#6 lore raw

Re: [PATCH 2/2] pseries: Correctly create ibm,segment-page-sizes property

Quoted 40 lines · Benjamin Herrenschmidt

The core tcg/kvm code for ppc64 now has at least the outline capability to support pagesizes beyond the standard 4k and 16MB. The CPUState is initialized with information advertising the available pagesizes and their correct encodings, and under the right KVM setup this will be populated with page sizes beyond the standard.

Obviously guests can't use the extra page sizes unless they know they're present. For the pseries machine, at least, there is a defined method for conveying exactly this information, the "ibm-segment-page-sizes" property in the guest device tree.

This patch generates this property using the supported page size information that's already in the CPUState.

---
  hw/spapr.c       |   42 ++++++++++++++++++++++++++++++++++++++++++
  target-ppc/kvm.c |   11 +++++++++--
  2 files changed, 51 insertions(+), 2 deletions(-)
hw/spapr.c +10 -0
index 94a4e1e..7c36903 100644
@@ -149,6 +149,39 @@ static int spapr_set_associativity(void *fdt, sPAPREnvironment *spapr)
      return ret;
  }
 
+
+static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
+                                     size_t maxsize)
+{
+    size_t maxcells = maxsize / sizeof(uint32_t);
+    int i, j, count;
+    uint32_t *p = prop;
+
+    for (i = 0; i<  PPC_PAGE_SIZES_MAX_SZ; i++) {
+        struct ppc_one_seg_page_size *sps =&env->sps.sps[i];

hw/spapr.c static int spapr_set_associativity(void *fdt, sPAPREnvironment *spapr) · line 171

Quoted 10 lines
hw/spapr.c +10 -0
+
+        if (!sps->page_shift) {
+            break;
+        }
+        for (count = 0; count<  PPC_PAGE_SIZES_MAX_SZ; count++) {
+            if (sps->enc[count].page_shift == 0) {
+                break;
+            }
+        }
+        if ((p - prop)>= (maxcells - 3 - count * 2))

Is this valid C? Can you substract one pointer from another and compare the result with an int?

hw/spapr.c static int spapr_set_associativity(void *fdt, sPAPREnvironment *spapr) · line 172

Quoted 1 line
hw/spapr.c +1 -0
+            break;

Braces? Please run checkpatch :)

hw/spapr.c static int spapr_set_associativity(void *fdt, sPAPREnvironment *spapr) · line 182

Quoted 10 lines
hw/spapr.c +10 -0
+        *(p++) = cpu_to_be32(sps->page_shift);
+        *(p++) = cpu_to_be32(sps->slb_enc);
+        *(p++) = cpu_to_be32(count);
+        for (j = 0; j<  count; j++) {
+            *(p++) = cpu_to_be32(sps->enc[j].page_shift);
+            *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
+        }
+    }
+
+    return (p - prop) * sizeof(uint32_t);

I'd prefer a second integer counter "len" I think :). Pointer arithmentics always make me wary...

Quoted 33 lines
hw/spapr.c +11 -0
+}
+
  static void *spapr_create_fdt_skel(const char *cpu_model,
                                     target_phys_addr_t rma_size,
                                     target_phys_addr_t initrd_base,
@@ -304,6 +337,8 @@ static void *spapr_create_fdt_skel(const char *cpu_model,
                             0xffffffff, 0xffffffff};
          uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
          uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
+        uint32_t page_sizes_prop[64];
+        size_t page_sizes_prop_size;
 
          if ((index % smt) != 0) {
              continue;
@@ -368,6 +403,13 @@ static void *spapr_create_fdt_skel(const char *cpu_model,
              _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
          }
 
+        page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
+                                                      sizeof(page_sizes_prop));
+        if (page_sizes_prop_size) {
+            _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
+                               page_sizes_prop, page_sizes_prop_size)));
+        }
+
          _FDT((fdt_end_node(fdt)));
      }
 
target-ppc/kvm.c +0 -0
index 77aa186..860711c 100644

target-ppc/kvm.c static void kvm_get_fallback_smmu_info(CPUPPCState *env, · line 204

Quoted 4 lines
target-ppc/kvm.c +1 -0
@@ -201,6 +201,7 @@ static void kvm_get_fallback_smmu_info(CPUPPCState *env,
      if (kvm_check_extension(env->kvm_state, KVM_CAP_PPC_GET_PVINFO)) {
          /* No flags */
          info->flags = 0;
+        info->slb_size = 64;

Eh - this one belongs in the first patch, no?

target-ppc/kvm.c static void kvm_get_fallback_smmu_info(CPUPPCState *env, · line 226

Quoted 12 lines
target-ppc/kvm.c +5 -0
          /* Standard 4k base page size segment */
          info->sps[0].page_shift = 12;
@@ -218,9 +219,15 @@ static void kvm_get_fallback_smmu_info(CPUPPCState *env,
 
  	/* HV KVM has backing store size restrictions */
          info->flags = KVM_PPC_PAGE_SIZES_REAL;
+        if (env->mmu_model == POWERPC_MMU_2_06) {
+            info->slb_size = 32;
+        } else {
+            info->slb_size = 64;
+        }

This assumes that we're always using -cpu host. Is there any more reliable way of calculating the slb size? Otherwise maybe we should just error out in non-cpu-host cases for HV mode.

target-ppc/kvm.c static void kvm_get_fallback_smmu_info(CPUPPCState *env, · line 230

Quoted 6 lines
target-ppc/kvm.c +3 -2
-        if (env->mmu_model&  POWERPC_MMU_1TSEG)
-            info->flags = KVM_PPC_1T_SEGMENTS;
+        if (env->mmu_model&  POWERPC_MMU_1TSEG) {
+            info->flags |= KVM_PPC_1T_SEGMENTS;
+        }

Ahem :)

Alex

#7 lore raw

Re: [PATCH 2/2] pseries: Correctly create ibm,segment-page-sizes property

Quoted 10 lines

hw/spapr.c static int spapr_set_associativity(void *fdt, sPAPREnvironment *spapr)

Quoted 7 lines
hw/spapr.c +6 -0
+        for (count = 0; count<  PPC_PAGE_SIZES_MAX_SZ; count++) {
+            if (sps->enc[count].page_shift == 0) {
+                break;
+            }
+        }
+        if ((p - prop)>= (maxcells - 3 - count * 2))

Is this valid C? Can you substract one pointer from another and compare the result with an int?

Ugh ... yeah it's basic stuff ;-) the difference between two pointers is an integer (there's even a ptrdiff_t nowadays no ?)

Quoted 3 lines
Quoted 1 line

+ break;

Braces? Please run checkpatch :)

Ah missed that one.

Quoted 11 lines

hw/spapr.c static int spapr_set_associativity(void *fdt, sPAPREnvironment *spapr) · line 182

Quoted 10 lines
hw/spapr.c +10 -0
+        *(p++) = cpu_to_be32(sps->page_shift);
+        *(p++) = cpu_to_be32(sps->slb_enc);
+        *(p++) = cpu_to_be32(count);
+        for (j = 0; j<  count; j++) {
+            *(p++) = cpu_to_be32(sps->enc[j].page_shift);
+            *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
+        }
+    }
+
+    return (p - prop) * sizeof(uint32_t);
Quoted 3 lines

I'd prefer a second integer counter "len" I think :). Pointer arithmentics always make me wary...

And a separate variable that might accidentally get out of sync makes _me_ wary :-)

Quoted 11 lines

target-ppc/kvm.c static void kvm_get_fallback_smmu_info(CPUPPCState *env, · line 204

Quoted 9 lines
target-ppc/kvm.c +1 -0
index 77aa186..860711c 100644
@@ -201,6 +201,7 @@ static void kvm_get_fallback_smmu_info(CPUPPCState *env,
      if (kvm_check_extension(env->kvm_state, KVM_CAP_PPC_GET_PVINFO)) {
          /* No flags */
          info->flags = 0;
+        info->slb_size = 64;

Eh - this one belongs in the first patch, no?

Quite possibly, the split in 2 patches was done by David (I originally did a single patch), so I'm not 100% sure why he put that there, I'll have a closer look today.

Quoted 4 lines
Quoted 4 lines
target-ppc/kvm.c +0 -0
          /* Standard 4k base page size segment */
          info->sps[0].page_shift = 12;
@@ -218,9 +219,15 @@ static void kvm_get_fallback_smmu_info(CPUPPCState *env,
 
Quoted 11 lines

target-ppc/kvm.c static void kvm_get_fallback_smmu_info(CPUPPCState *env, · line 226

Quoted 7 lines
target-ppc/kvm.c +5 -0
  	/* HV KVM has backing store size restrictions */
          info->flags = KVM_PPC_PAGE_SIZES_REAL;
+        if (env->mmu_model == POWERPC_MMU_2_06) {
+            info->slb_size = 32;
+        } else {
+            info->slb_size = 64;
+        }

This assumes that we're always using -cpu host. Is there any more reliable way of calculating the slb size? Otherwise maybe we should just error out in non-cpu-host cases for HV mode.

This is a fallback, it's good enough. I don't think there's such a thing as non-cpu-host on HV anyway, at least for now (we should probably error out elsewhere). In any case, even if the CPU is configured for backward compat (which we don't support yet, though might using -cpu in the long run), the SLB size so far has to be exactly the one implemented by the host when using HV KVM.

So for anything we can work on today, the above will work.

Quoted 7 lines

target-ppc/kvm.c static void kvm_get_fallback_smmu_info(CPUPPCState *env, · line 230

Quoted 5 lines
target-ppc/kvm.c +3 -2
-        if (env->mmu_model&  POWERPC_MMU_1TSEG)
-            info->flags = KVM_PPC_1T_SEGMENTS;
+        if (env->mmu_model&  POWERPC_MMU_1TSEG) {
+            info->flags |= KVM_PPC_1T_SEGMENTS;
+        }

Ahem :)

What's this ? Second patch adding the braces missing in the first one ? Heh, ok, I'll fix that.

Cheers,
Ben.

#8 lore raw
In reply to Benjamin Herrenschmidt
Quoted 7 lines · Benjamin Herrenschmidt

This is necessary for qemu to be able to pass the right information to the guest, such as the supported page sizes and corresponding encodings in the SLB and hash table, which can vary depending on the processor type, the type of KVM used (PR vs HV) and the version of KVM

Thanks, applied (with fixes) to kvm-ppc-next.

Alex