Skip to contentSkip to conversation

target-ppc: Fix htab_mask calculation

PATCH Superseded Aneesh Kumar K.V 2 participants 8 messages Dec 23, 2013 – Jan 9, 2014

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 target-ppc: Fix htab_mask calculation

From: "Aneesh Kumar K.V" <[email protected]>

Correctly update the htab_mask using the return value of KVM_PPC_ALLOCATE_HTAB ioctl. Also we don't update sdr1 on GET_SREGS for HV. So don't update htab_mask if sdr1 is found to be zero. Fix the pte index calculation to be same as that found in the kernel

---
 hw/ppc/spapr.c          | 8 +++++++-
 target-ppc/cpu.h        | 1 +
 target-ppc/mmu_helper.c | 4 +++-
 3 files changed, 11 insertions(+), 2 deletions(-)
hw/ppc/spapr.c +7 -1
index 267a47d6cc4d..e99a66170661 100644
@@ -731,7 +731,13 @@ static void spapr_cpu_reset(void *opaque)
 
     env->external_htab = (uint8_t *)spapr->htab;
     env->htab_base = -1;
-    env->htab_mask = HTAB_SIZE(spapr) - 1;
+    /*
+     * htab_mask is the mask used to normalize hash value to PTEG index.
+     * htab_shift is log2 of hash table size.
+     * We have 8 hpte per group, and each hpte is 16 bytes.
+     * ie have 128 bytes per hpte entry.
+     */
+    env->htab_mask = (1ULL << ((spapr)->htab_shift - 7)) - 1;
     env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab |
         (spapr->htab_shift - 18);
 }
target-ppc/cpu.h +1 -0
index 26acdba847fa..82db9a442be5 100644
@@ -955,6 +955,7 @@ struct CPUPPCState {
 #endif
     /* segment registers */
     hwaddr htab_base;
+    /* mask used to normalize hash value to PTEG index */
     hwaddr htab_mask;
     target_ulong sr[32];
     /* externally stored hash table */
target-ppc/mmu_helper.c +3 -1
index 04a840b01697..c39cb7b89ee0 100644
@@ -2025,7 +2025,9 @@ void ppc_store_sdr1(CPUPPCState *env, target_ulong value)
                         " stored in SDR1\n", htabsize);
                 htabsize = 28;
             }
-            env->htab_mask = (1ULL << (htabsize + 18)) - 1;
+            if (htabsize) {
+                env->htab_mask = (1ULL << (htabsize + 18 - 7)) - 1;
+            }
             env->htab_base = value & SDR_64_HTABORG;
         } else
 #endif /* defined(TARGET_PPC64) */
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 2/3 target-ppc: Update external_htab even when HTAB is managed by kernel

From: "Aneesh Kumar K.V" <[email protected]>

We will use this in later patches to make sure we use the right load functions when copying hpte entries.

---
 hw/ppc/spapr.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/ppc/spapr.c +7 -0
index e99a66170661..3c0f29c99820 100644
@@ -730,6 +730,13 @@ static void spapr_cpu_reset(void *opaque)
     env->spr[SPR_HIOR] = 0;
 
     env->external_htab = (uint8_t *)spapr->htab;
+    if (kvm_enabled() && !env->external_htab) {
+        /*
+         * HV KVM, set external_htab to 1 so our ppc_hash64_load_hpte*
+         * functions do the right thing.
+         */
+        env->external_htab = (void *)1;
+    }
     env->htab_base = -1;
     /*
      * htab_mask is the mask used to normalize hash value to PTEG index.
Signature · 1 line
1.8.3.2
#2 lore raw

PATCH 3/3 target-ppc: Fix page table lookup with kvm enabled

In reply to Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

With kvm enabled, we store the hash page table information in the hypervisor. Use ioctl to read the htab contents. Without this we get the below error when trying to read the guest address

(gdb) x/10 do_fork
0xc000000000098660 <do_fork>: Cannot access memory at address 0xc000000000098660
(gdb)

---
Changes from V7:
* used uint64_t token

 hw/ppc/spapr.c          |  1 +
 hw/ppc/spapr_hcall.c    | 50 +++++++++++++++++++------------
 target-ppc/kvm.c        | 53 +++++++++++++++++++++++++++++++++
 target-ppc/kvm_ppc.h    | 19 ++++++++++++
 target-ppc/mmu-hash64.c | 78 ++++++++++++++++++++++++++++++++++++++++---------
 target-ppc/mmu-hash64.h | 19 ++++++++----
 6 files changed, 181 insertions(+), 39 deletions(-)
hw/ppc/spapr.c +1 -0
index 3c0f29c99820..05244244301a 100644
@@ -676,6 +676,7 @@ static void spapr_reset_htab(sPAPREnvironment *spapr)
     if (shift > 0) {
         /* Kernel handles htab, we don't need to allocate one */
         spapr->htab_shift = shift;
+        kvmppc_kern_htab = true;
     } else {
         if (!spapr->htab) {
             /* Allocate an htab if we don't yet have one */
hw/ppc/spapr_hcall.c +31 -19

113 lines not on this page · raw message

target-ppc/kvm.c +53 -0

62 lines not on this page · raw message

target-ppc/kvm_ppc.h +19 -0
index 5f78e4be14af..4767dc380162 100644
@@ -39,10 +39,13 @@ uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift);
 int kvmppc_fixup_cpu(PowerPCCPU *cpu);
 bool kvmppc_has_cap_epr(void);
 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function);
+bool kvmppc_has_cap_htab_fd(void);
 int kvmppc_get_htab_fd(bool write);
 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns);
 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                            uint16_t n_valid, uint16_t n_invalid);
+uint64_t kvmppc_hash64_read_pteg(PowerPCCPU *cpu, unsigned long pte_index);
+void kvmppc_hash64_free_pteg(uint64_t token);
 
 #else
 
@@ -171,6 +174,11 @@ static inline int kvmppc_define_rtas_kernel_token(uint32_t token,
     return -1;
 }
 
+static inline bool kvmppc_has_cap_htab_fd(void)
+{
+    return false;
+}
+
 static inline int kvmppc_get_htab_fd(bool write)
 {
     return -1;
@@ -188,6 +196,17 @@ static inline int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
     abort();
 }
 
+static inline uint64_t kvmppc_hash64_read_pteg(PowerPCCPU *cpu,
+                                               unsigned long pte_index)
+{
+    abort();
+}
+
+static inline void kvmppc_hash64_free_pteg(uint64_t token)
+{
+    abort();
+}
+
 #endif
 
 #ifndef CONFIG_KVM
target-ppc/mmu-hash64.c +64 -14

122 lines not on this page · raw message

target-ppc/mmu-hash64.h +13 -6
index 55f5a230fd20..5543f488fe13 100644
@@ -75,23 +75,30 @@ int ppc_hash64_handle_mmu_fault(CPUPPCState *env, target_ulong address, int rw,
 #define HPTE64_V_1TB_SEG        0x4000000000000000ULL
 #define HPTE64_V_VRMA_MASK      0x4001ffffff000000ULL
 
+
+extern bool kvmppc_kern_htab;
+uint64_t ppc_hash64_start_access(PowerPCCPU *cpu, unsigned long pte_index);
+void ppc_hash64_stop_access(uint64_t token);
+
 static inline target_ulong ppc_hash64_load_hpte0(CPUPPCState *env,
-                                                 hwaddr pte_offset)
+                                                 uint64_t token, int index)
 {
+    index *= HASH_PTE_SIZE_64;
     if (env->external_htab) {
-        return  ldq_p(env->external_htab + pte_offset);
+        return  ldq_p((const void *)(token + index));
     } else {
-        return ldq_phys(env->htab_base + pte_offset);
+        return ldq_phys(token + index);
     }
 }
 
 static inline target_ulong ppc_hash64_load_hpte1(CPUPPCState *env,
-                                                 hwaddr pte_offset)
+                                                 uint64_t token, int index)
 {
+    index *= HASH_PTE_SIZE_64;
     if (env->external_htab) {
-        return ldq_p(env->external_htab + pte_offset + HASH_PTE_SIZE_64/2);
+        return  ldq_p((const void *)(token + index + HASH_PTE_SIZE_64/2));
     } else {
-        return ldq_phys(env->htab_base + pte_offset + HASH_PTE_SIZE_64/2);
+        return ldq_phys(token + index + HASH_PTE_SIZE_64/2);
     }
 }
 
Signature · 1 line
1.8.3.2
#3 lore raw

Re: [Qemu-devel] [PATCH -V8 3/3] target-ppc: Fix page table lookup with kvm enabled

Quoted 80 lines · Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

With kvm enabled, we store the hash page table information in the hypervisor. Use ioctl to read the htab contents. Without this we get the below error when trying to read the guest address

(gdb) x/10 do_fork
0xc000000000098660 <do_fork>: Cannot access memory at address 0xc000000000098660
(gdb)

---
Changes from V7:
* used uint64_t token

hw/ppc/spapr.c          |  1 +
hw/ppc/spapr_hcall.c    | 50 +++++++++++++++++++------------
target-ppc/kvm.c        | 53 +++++++++++++++++++++++++++++++++
target-ppc/kvm_ppc.h    | 19 ++++++++++++
target-ppc/mmu-hash64.c | 78 ++++++++++++++++++++++++++++++++++++++++---------
target-ppc/mmu-hash64.h | 19 ++++++++----
6 files changed, 181 insertions(+), 39 deletions(-)
hw/ppc/spapr.c +1 -0
index 3c0f29c99820..05244244301a 100644
@@ -676,6 +676,7 @@ static void spapr_reset_htab(sPAPREnvironment *spapr)
    if (shift > 0) {
        /* Kernel handles htab, we don't need to allocate one */
        spapr->htab_shift = shift;
+        kvmppc_kern_htab = true;
    } else {
        if (!spapr->htab) {
            /* Allocate an htab if we don't yet have one */
hw/ppc/spapr_hcall.c +12 -10
index f755a5392317..01cf6b05fee7 100644
@@ -50,8 +50,9 @@ static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr,
    target_ulong ptel = args[3];
    target_ulong page_shift = 12;
    target_ulong raddr;
-    target_ulong i;
+    target_ulong index;
    hwaddr hpte;
+    uint64_t token;
 
    /* only handle 4k and 16M pages for now */
    if (pteh & HPTE64_V_LARGE) {
@@ -94,30 +95,37 @@ static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr,
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
        return H_PARAMETER;
    }
+
+    index = 0;
+    hpte = pte_index * HASH_PTE_SIZE_64;
    if (likely((flags & H_EXACT) == 0)) {
        pte_index &= ~7ULL;
-        hpte = pte_index * HASH_PTE_SIZE_64;
-        for (i = 0; ; ++i) {
-            if (i == 8) {
+        token = ppc_hash64_start_access(cpu, pte_index);
+        do {
+            if (index == 8) {
+                ppc_hash64_stop_access(token);
                return H_PTEG_FULL;
            }
-            if ((ppc_hash64_load_hpte0(env, hpte) & HPTE64_V_VALID) == 0) {
+            if ((ppc_hash64_load_hpte0(env, token, index) & HPTE64_V_VALID) == 0) {
                break;
            }
-            hpte += HASH_PTE_SIZE_64;
-        }
+        } while (index++);
+        ppc_hash64_stop_access(token);
    } else {
-        i = 0;
-        hpte = pte_index * HASH_PTE_SIZE_64;
-        if (ppc_hash64_load_hpte0(env, hpte) & HPTE64_V_VALID) {

hw/ppc/spapr_hcall.c static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr, · line 124

Quoted 10 lines
hw/ppc/spapr_hcall.c +6 -0
+        token = ppc_hash64_start_access(cpu, pte_index);
+        if (ppc_hash64_load_hpte0(env, token, 0) & HPTE64_V_VALID) {
+            ppc_hash64_stop_access(token);
            return H_PTEG_FULL;
        }
+        ppc_hash64_stop_access(token);
    }
+    hpte += index * HASH_PTE_SIZE_64;
+
    ppc_hash64_store_hpte1(env, hpte, ptel);

Didn't I mention that stores should be converted as well?

Quoted 85 lines
hw/ppc/spapr_hcall.c +13 -9
    /* eieio();  FIXME: need some sort of barrier for smp? */
    ppc_hash64_store_hpte0(env, hpte, pteh | HPTE64_V_HPTE_DIRTY);
 
-    args[0] = pte_index + i;
+    args[0] = pte_index + index;
    return H_SUCCESS;
 }
 
@@ -134,16 +142,17 @@ static RemoveResult remove_hpte(CPUPPCState *env, target_ulong ptex,
                                target_ulong *vp, target_ulong *rp)
 {
    hwaddr hpte;
+    uint64_t token;
    target_ulong v, r, rb;
 
    if ((ptex * HASH_PTE_SIZE_64) & ~env->htab_mask) {
        return REMOVE_PARM;
    }
 
-    hpte = ptex * HASH_PTE_SIZE_64;
-
-    v = ppc_hash64_load_hpte0(env, hpte);
-    r = ppc_hash64_load_hpte1(env, hpte);
+    token = ppc_hash64_start_access(ppc_env_get_cpu(env), ptex);
+    v = ppc_hash64_load_hpte0(env, token, 0);
+    r = ppc_hash64_load_hpte1(env, token, 0);
+    ppc_hash64_stop_access(token);
 
    if ((v & HPTE64_V_VALID) == 0 ||
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
@@ -152,6 +161,7 @@ static RemoveResult remove_hpte(CPUPPCState *env, target_ulong ptex,
    }
    *vp = v;
    *rp = r;
+    hpte = ptex * HASH_PTE_SIZE_64;
    ppc_hash64_store_hpte0(env, hpte, HPTE64_V_HPTE_DIRTY);
    rb = compute_tlbie_rb(v, r, ptex);
    ppc_tlb_invalidate_one(env, rb);
@@ -260,16 +270,17 @@ static target_ulong h_protect(PowerPCCPU *cpu, sPAPREnvironment *spapr,
    target_ulong pte_index = args[1];
    target_ulong avpn = args[2];
    hwaddr hpte;
+    uint64_t token;
    target_ulong v, r, rb;
 
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
        return H_PARAMETER;
    }
 
-    hpte = pte_index * HASH_PTE_SIZE_64;
-
-    v = ppc_hash64_load_hpte0(env, hpte);
-    r = ppc_hash64_load_hpte1(env, hpte);
+    token = ppc_hash64_start_access(cpu, pte_index);
+    v = ppc_hash64_load_hpte0(env, token, 0);
+    r = ppc_hash64_load_hpte1(env, token, 0);
+    ppc_hash64_stop_access(token);
 
    if ((v & HPTE64_V_VALID) == 0 ||
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
@@ -282,6 +293,7 @@ static target_ulong h_protect(PowerPCCPU *cpu, sPAPREnvironment *spapr,
    r |= (flags << 48) & HPTE64_R_KEY_HI;
    r |= flags & (HPTE64_R_PP | HPTE64_R_N | HPTE64_R_KEY_LO);
    rb = compute_tlbie_rb(v, r, pte_index);
+    hpte = pte_index * HASH_PTE_SIZE_64;
    ppc_hash64_store_hpte0(env, hpte, (v & ~HPTE64_V_VALID) | HPTE64_V_HPTE_DIRTY);
    ppc_tlb_invalidate_one(env, rb);
    ppc_hash64_store_hpte1(env, hpte, r);
target-ppc/kvm.c +5 -0
index b77ce5e94cbc..8f5c6b39eb38 100644
@@ -1783,6 +1783,11 @@ bool kvmppc_has_cap_epr(void)
    return cap_epr;
 }
 
+bool kvmppc_has_cap_htab_fd(void)
+{
+    return cap_htab_fd;
+}
+
 static int kvm_ppc_register_host_cpu_type(void)
 {
    TypeInfo type_info = {

target-ppc/kvm.c int kvm_arch_on_sigbus(int code, void *addr) · line 1914

Quoted 8 lines
target-ppc/kvm.c +5 -0
@@ -1902,3 +1907,51 @@ int kvm_arch_on_sigbus(int code, void *addr)
 void kvm_arch_init_irq_routing(KVMState *s)
 {
 }
+
+struct kvm_get_htab_buf {
+    struct kvm_get_htab_header header;
+    /*
+     * We required one extra byte for read

Why past tense?

Alex

#4 lore raw

Re: [Qemu-devel] [PATCH -V8 3/3] target-ppc: Fix page table lookup with kvm enabled

Quoted 86 lines · Alexander Graf
Quoted 84 lines · Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

With kvm enabled, we store the hash page table information in the hypervisor. Use ioctl to read the htab contents. Without this we get the below error when trying to read the guest address

(gdb) x/10 do_fork
0xc000000000098660 <do_fork>: Cannot access memory at address 0xc000000000098660
(gdb)

---
Changes from V7:
* used uint64_t token

hw/ppc/spapr.c          |  1 +
hw/ppc/spapr_hcall.c    | 50 +++++++++++++++++++------------
target-ppc/kvm.c        | 53 +++++++++++++++++++++++++++++++++
target-ppc/kvm_ppc.h    | 19 ++++++++++++
target-ppc/mmu-hash64.c | 78 ++++++++++++++++++++++++++++++++++++++++---------
target-ppc/mmu-hash64.h | 19 ++++++++----
6 files changed, 181 insertions(+), 39 deletions(-)
hw/ppc/spapr.c +1 -0
index 3c0f29c99820..05244244301a 100644
@@ -676,6 +676,7 @@ static void spapr_reset_htab(sPAPREnvironment *spapr)
    if (shift > 0) {
        /* Kernel handles htab, we don't need to allocate one */
        spapr->htab_shift = shift;
+        kvmppc_kern_htab = true;
    } else {
        if (!spapr->htab) {
            /* Allocate an htab if we don't yet have one */
hw/ppc/spapr_hcall.c +15 -10
index f755a5392317..01cf6b05fee7 100644
@@ -50,8 +50,9 @@ static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr,
    target_ulong ptel = args[3];
    target_ulong page_shift = 12;
    target_ulong raddr;
-    target_ulong i;
+    target_ulong index;
    hwaddr hpte;
+    uint64_t token;
 
    /* only handle 4k and 16M pages for now */
    if (pteh & HPTE64_V_LARGE) {
@@ -94,30 +95,37 @@ static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr,
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
        return H_PARAMETER;
    }
+
+    index = 0;
+    hpte = pte_index * HASH_PTE_SIZE_64;
    if (likely((flags & H_EXACT) == 0)) {
        pte_index &= ~7ULL;
-        hpte = pte_index * HASH_PTE_SIZE_64;
-        for (i = 0; ; ++i) {
-            if (i == 8) {
+        token = ppc_hash64_start_access(cpu, pte_index);
+        do {
+            if (index == 8) {
+                ppc_hash64_stop_access(token);
                return H_PTEG_FULL;
            }
-            if ((ppc_hash64_load_hpte0(env, hpte) & HPTE64_V_VALID) == 0) {
+            if ((ppc_hash64_load_hpte0(env, token, index) & HPTE64_V_VALID) == 0) {
                break;
            }
-            hpte += HASH_PTE_SIZE_64;
-        }
+        } while (index++);
+        ppc_hash64_stop_access(token);
    } else {
-        i = 0;
-        hpte = pte_index * HASH_PTE_SIZE_64;
-        if (ppc_hash64_load_hpte0(env, hpte) & HPTE64_V_VALID) {
+        token = ppc_hash64_start_access(cpu, pte_index);
+        if (ppc_hash64_load_hpte0(env, token, 0) & HPTE64_V_VALID) {
+            ppc_hash64_stop_access(token);
            return H_PTEG_FULL;
Quoted 8 lines

hw/ppc/spapr_hcall.c static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr, · line 124

Quoted 6 lines
hw/ppc/spapr_hcall.c +3 -0
        }
+        ppc_hash64_stop_access(token);
    }
+    hpte += index * HASH_PTE_SIZE_64;
+
    ppc_hash64_store_hpte1(env, hpte, ptel);

Didn't I mention that stores should be converted as well?

For the specific use case of x/10i we didn't needed store. And I though you agreed that just getting read alone now is useful if we can be sure store follow later. I was hoping to get to that later once this gets upstream

Quoted 88 lines
Quoted 88 lines
hw/ppc/spapr_hcall.c +13 -9
    /* eieio();  FIXME: need some sort of barrier for smp? */
    ppc_hash64_store_hpte0(env, hpte, pteh | HPTE64_V_HPTE_DIRTY);
 
-    args[0] = pte_index + i;
+    args[0] = pte_index + index;
    return H_SUCCESS;
 }
 
@@ -134,16 +142,17 @@ static RemoveResult remove_hpte(CPUPPCState *env, target_ulong ptex,
                                target_ulong *vp, target_ulong *rp)
 {
    hwaddr hpte;
+    uint64_t token;
    target_ulong v, r, rb;
 
    if ((ptex * HASH_PTE_SIZE_64) & ~env->htab_mask) {
        return REMOVE_PARM;
    }
 
-    hpte = ptex * HASH_PTE_SIZE_64;
-
-    v = ppc_hash64_load_hpte0(env, hpte);
-    r = ppc_hash64_load_hpte1(env, hpte);
+    token = ppc_hash64_start_access(ppc_env_get_cpu(env), ptex);
+    v = ppc_hash64_load_hpte0(env, token, 0);
+    r = ppc_hash64_load_hpte1(env, token, 0);
+    ppc_hash64_stop_access(token);
 
    if ((v & HPTE64_V_VALID) == 0 ||
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
@@ -152,6 +161,7 @@ static RemoveResult remove_hpte(CPUPPCState *env, target_ulong ptex,
    }
    *vp = v;
    *rp = r;
+    hpte = ptex * HASH_PTE_SIZE_64;
    ppc_hash64_store_hpte0(env, hpte, HPTE64_V_HPTE_DIRTY);
    rb = compute_tlbie_rb(v, r, ptex);
    ppc_tlb_invalidate_one(env, rb);
@@ -260,16 +270,17 @@ static target_ulong h_protect(PowerPCCPU *cpu, sPAPREnvironment *spapr,
    target_ulong pte_index = args[1];
    target_ulong avpn = args[2];
    hwaddr hpte;
+    uint64_t token;
    target_ulong v, r, rb;
 
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
        return H_PARAMETER;
    }
 
-    hpte = pte_index * HASH_PTE_SIZE_64;
-
-    v = ppc_hash64_load_hpte0(env, hpte);
-    r = ppc_hash64_load_hpte1(env, hpte);
+    token = ppc_hash64_start_access(cpu, pte_index);
+    v = ppc_hash64_load_hpte0(env, token, 0);
+    r = ppc_hash64_load_hpte1(env, token, 0);
+    ppc_hash64_stop_access(token);
 
    if ((v & HPTE64_V_VALID) == 0 ||
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
@@ -282,6 +293,7 @@ static target_ulong h_protect(PowerPCCPU *cpu, sPAPREnvironment *spapr,
    r |= (flags << 48) & HPTE64_R_KEY_HI;
    r |= flags & (HPTE64_R_PP | HPTE64_R_N | HPTE64_R_KEY_LO);
    rb = compute_tlbie_rb(v, r, pte_index);
+    hpte = pte_index * HASH_PTE_SIZE_64;
    ppc_hash64_store_hpte0(env, hpte, (v & ~HPTE64_V_VALID) | HPTE64_V_HPTE_DIRTY);
    ppc_tlb_invalidate_one(env, rb);
    ppc_hash64_store_hpte1(env, hpte, r);
target-ppc/kvm.c +5 -0
index b77ce5e94cbc..8f5c6b39eb38 100644
@@ -1783,6 +1783,11 @@ bool kvmppc_has_cap_epr(void)
    return cap_epr;
 }
 
+bool kvmppc_has_cap_htab_fd(void)
+{
+    return cap_htab_fd;
+}
+
 static int kvm_ppc_register_host_cpu_type(void)
 {
    TypeInfo type_info = {
@@ -1902,3 +1907,51 @@ int kvm_arch_on_sigbus(int code, void *addr)
 void kvm_arch_init_irq_routing(KVMState *s)
 {
Quoted 8 lines

target-ppc/kvm.c int kvm_arch_on_sigbus(int code, void *addr) · line 1914

Quoted 6 lines
target-ppc/kvm.c +5 -0
 }
+
+struct kvm_get_htab_buf {
+    struct kvm_get_htab_header header;
+    /*
+     * We required one extra byte for read

Why past tense?

Wrong english, nothing else :). Will fix

-aneesh

#5 lore raw

Re: [Qemu-devel] [PATCH -V8 3/3] target-ppc: Fix page table lookup with kvm enabled

Quoted 97 lines · Aneesh Kumar K.V
Quoted 86 lines · Alexander Graf
Quoted 84 lines · Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

With kvm enabled, we store the hash page table information in the hypervisor. Use ioctl to read the htab contents. Without this we get the below error when trying to read the guest address

(gdb) x/10 do_fork
0xc000000000098660 <do_fork>: Cannot access memory at address 0xc000000000098660
(gdb)

---
Changes from V7:
* used uint64_t token

hw/ppc/spapr.c          |  1 +
hw/ppc/spapr_hcall.c    | 50 +++++++++++++++++++------------
target-ppc/kvm.c        | 53 +++++++++++++++++++++++++++++++++
target-ppc/kvm_ppc.h    | 19 ++++++++++++
target-ppc/mmu-hash64.c | 78 ++++++++++++++++++++++++++++++++++++++++---------
target-ppc/mmu-hash64.h | 19 ++++++++----
6 files changed, 181 insertions(+), 39 deletions(-)
hw/ppc/spapr.c +1 -0
index 3c0f29c99820..05244244301a 100644
@@ -676,6 +676,7 @@ static void spapr_reset_htab(sPAPREnvironment *spapr)
   if (shift > 0) {
       /* Kernel handles htab, we don't need to allocate one */
       spapr->htab_shift = shift;
+        kvmppc_kern_htab = true;
   } else {
       if (!spapr->htab) {
           /* Allocate an htab if we don't yet have one */
hw/ppc/spapr_hcall.c +15 -10
index f755a5392317..01cf6b05fee7 100644
@@ -50,8 +50,9 @@ static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr,
   target_ulong ptel = args[3];
   target_ulong page_shift = 12;
   target_ulong raddr;
-    target_ulong i;
+    target_ulong index;
   hwaddr hpte;
+    uint64_t token;
 
   /* only handle 4k and 16M pages for now */
   if (pteh & HPTE64_V_LARGE) {
@@ -94,30 +95,37 @@ static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr,
   if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
       return H_PARAMETER;
   }
+
+    index = 0;
+    hpte = pte_index * HASH_PTE_SIZE_64;
   if (likely((flags & H_EXACT) == 0)) {
       pte_index &= ~7ULL;
-        hpte = pte_index * HASH_PTE_SIZE_64;
-        for (i = 0; ; ++i) {
-            if (i == 8) {
+        token = ppc_hash64_start_access(cpu, pte_index);
+        do {
+            if (index == 8) {
+                ppc_hash64_stop_access(token);
               return H_PTEG_FULL;
           }
-            if ((ppc_hash64_load_hpte0(env, hpte) & HPTE64_V_VALID) == 0) {
+            if ((ppc_hash64_load_hpte0(env, token, index) & HPTE64_V_VALID) == 0) {
               break;
           }
-            hpte += HASH_PTE_SIZE_64;
-        }
+        } while (index++);
+        ppc_hash64_stop_access(token);
   } else {
-        i = 0;
-        hpte = pte_index * HASH_PTE_SIZE_64;
-        if (ppc_hash64_load_hpte0(env, hpte) & HPTE64_V_VALID) {
+        token = ppc_hash64_start_access(cpu, pte_index);
+        if (ppc_hash64_load_hpte0(env, token, 0) & HPTE64_V_VALID) {
+            ppc_hash64_stop_access(token);
           return H_PTEG_FULL;
Quoted 8 lines

hw/ppc/spapr_hcall.c static target_ulong h_enter(PowerPCCPU *cpu, sPAPREnvironment *spapr, · line 124

Quoted 6 lines
hw/ppc/spapr_hcall.c +3 -0
       }
+        ppc_hash64_stop_access(token);
   }
+    hpte += index * HASH_PTE_SIZE_64;
+
   ppc_hash64_store_hpte1(env, hpte, ptel);

Didn't I mention that stores should be converted as well?

Quoted 4 lines

For the specific use case of x/10i we didn't needed store. And I though you agreed that just getting read alone now is useful if we can be sure store follow later. I was hoping to get to that later once this gets upstream

Ok, please mention these intentions in the cover letter :)

Alex

#6 lore raw
In reply to Aneesh Kumar K.V
Quoted 51 lines · Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

Correctly update the htab_mask using the return value of KVM_PPC_ALLOCATE_HTAB ioctl. Also we don't update sdr1 on GET_SREGS for HV. So don't update htab_mask if sdr1 is found to be zero. Fix the pte index calculation to be same as that found in the kernel

---

hw/ppc/spapr.c          | 8 +++++++-
target-ppc/cpu.h        | 1 +
target-ppc/mmu_helper.c | 4 +++-
3 files changed, 11 insertions(+), 2 deletions(-)
hw/ppc/spapr.c +7 -1
index 267a47d6cc4d..e99a66170661 100644
@@ -731,7 +731,13 @@ static void spapr_cpu_reset(void *opaque)
 
    env->external_htab = (uint8_t *)spapr->htab;
    env->htab_base = -1;
-    env->htab_mask = HTAB_SIZE(spapr) - 1;
+    /*
+     * htab_mask is the mask used to normalize hash value to PTEG index.
+     * htab_shift is log2 of hash table size.
+     * We have 8 hpte per group, and each hpte is 16 bytes.
+     * ie have 128 bytes per hpte entry.
+     */
+    env->htab_mask = (1ULL << ((spapr)->htab_shift - 7)) - 1;
    env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab |
        (spapr->htab_shift - 18);
 }
target-ppc/cpu.h +1 -0
index 26acdba847fa..82db9a442be5 100644
@@ -955,6 +955,7 @@ struct CPUPPCState {
 #endif
    /* segment registers */
    hwaddr htab_base;
+    /* mask used to normalize hash value to PTEG index */
    hwaddr htab_mask;
    target_ulong sr[32];
    /* externally stored hash table */
target-ppc/mmu_helper.c +0 -0
index 04a840b01697..c39cb7b89ee0 100644

target-ppc/mmu_helper.c void ppc_store_sdr1(CPUPPCState *env, target_ulong value) · line 2028

Quoted 5 lines
target-ppc/mmu_helper.c +1 -1
@@ -2025,7 +2025,9 @@ void ppc_store_sdr1(CPUPPCState *env, target_ulong value)
                        " stored in SDR1\n", htabsize);
                htabsize = 28;
            }
-            env->htab_mask = (1ULL << (htabsize + 18)) - 1;
+            if (htabsize) {

Better check for external_htab in the caller and never store SDR1 if we use an external htab. If you like, add an assert(!env->external_htab) at the beginning of ppc_store_sdr1() to make sure we really never get here.

Alex

#7 lore raw
Quoted 55 lines · Alexander Graf
Quoted 53 lines · Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

Correctly update the htab_mask using the return value of KVM_PPC_ALLOCATE_HTAB ioctl. Also we don't update sdr1 on GET_SREGS for HV. So don't update htab_mask if sdr1 is found to be zero. Fix the pte index calculation to be same as that found in the kernel

---

hw/ppc/spapr.c          | 8 +++++++-
target-ppc/cpu.h        | 1 +
target-ppc/mmu_helper.c | 4 +++-
3 files changed, 11 insertions(+), 2 deletions(-)
hw/ppc/spapr.c +7 -1
index 267a47d6cc4d..e99a66170661 100644
@@ -731,7 +731,13 @@ static void spapr_cpu_reset(void *opaque)
 
    env->external_htab = (uint8_t *)spapr->htab;
    env->htab_base = -1;
-    env->htab_mask = HTAB_SIZE(spapr) - 1;
+    /*
+     * htab_mask is the mask used to normalize hash value to PTEG index.
+     * htab_shift is log2 of hash table size.
+     * We have 8 hpte per group, and each hpte is 16 bytes.
+     * ie have 128 bytes per hpte entry.
+     */
+    env->htab_mask = (1ULL << ((spapr)->htab_shift - 7)) - 1;
    env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab |
        (spapr->htab_shift - 18);
 }
target-ppc/cpu.h +1 -0
index 26acdba847fa..82db9a442be5 100644
@@ -955,6 +955,7 @@ struct CPUPPCState {
 #endif
    /* segment registers */
    hwaddr htab_base;
+    /* mask used to normalize hash value to PTEG index */
    hwaddr htab_mask;
    target_ulong sr[32];
    /* externally stored hash table */
target-ppc/mmu_helper.c +0 -0
index 04a840b01697..c39cb7b89ee0 100644
@@ -2025,7 +2025,9 @@ void ppc_store_sdr1(CPUPPCState *env, target_ulong value)
                        " stored in SDR1\n", htabsize);
                htabsize = 28;
Quoted 8 lines

target-ppc/mmu_helper.c void ppc_store_sdr1(CPUPPCState *env, target_ulong value) · line 2028

Quoted 3 lines
target-ppc/mmu_helper.c +1 -1
            }
-            env->htab_mask = (1ULL << (htabsize + 18)) - 1;
+            if (htabsize) {

Better check for external_htab in the caller and never store SDR1 if we use an external htab. If you like, add an assert(!env->external_htab) at the beginning of ppc_store_sdr1() to make sure we really never get here.

will do

-aneesh