[PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-30 Thread Alexander Graf
Currently the shadow paging code keeps an array of entries it knows about.
Whenever the guest invalidates an entry, we loop through that entry,
trying to invalidate matching parts.

While this is a really simple implementation, it is probably the most
ineffective one possible. So instead, let's keep an array of lists around
that are indexed by a hash. This way each PTE can be added by 4 list_add,
removed by 4 list_del invocations and the search only needs to loop through
entries that share the same hash.

This patch implements said lookup and exports generic functions that both
the 32-bit and 64-bit backend can use.

Signed-off-by: Alexander Graf 

---

v1 -> v2:

  - remove hpte_all list
  - lookup all using vpte_long lists
  - decrease size of vpte_long hash
  - fix missing brackets

v2 -> v3:

  - use hlist
  - use global kmem cache
---
 arch/powerpc/kvm/book3s_mmu_hpte.c |  277 
 1 files changed, 277 insertions(+), 0 deletions(-)
 create mode 100644 arch/powerpc/kvm/book3s_mmu_hpte.c

diff --git a/arch/powerpc/kvm/book3s_mmu_hpte.c 
b/arch/powerpc/kvm/book3s_mmu_hpte.c
new file mode 100644
index 000..4868d4a
--- /dev/null
+++ b/arch/powerpc/kvm/book3s_mmu_hpte.c
@@ -0,0 +1,277 @@
+/*
+ * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
+ *
+ * Authors:
+ * Alexander Graf 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define PTE_SIZE   12
+
+/* #define DEBUG_MMU */
+
+#ifdef DEBUG_MMU
+#define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
+#else
+#define dprintk_mmu(a, ...) do { } while(0)
+#endif
+
+static struct kmem_cache *hpte_cache;
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr)
+{
+   return hash_64(eaddr >> PTE_SIZE, HPTEG_HASH_BITS_PTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage)
+{
+   return hash_64(vpage & 0xfULL, HPTEG_HASH_BITS_VPTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage)
+{
+   return hash_64((vpage & 0xff000ULL) >> 12,
+  HPTEG_HASH_BITS_VPTE_LONG);
+}
+
+void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
+{
+   u64 index;
+
+   /* Add to ePTE list */
+   index = kvmppc_mmu_hash_pte(pte->pte.eaddr);
+   hlist_add_head(&pte->list_pte, &vcpu->arch.hpte_hash_pte[index]);
+
+   /* Add to vPTE list */
+   index = kvmppc_mmu_hash_vpte(pte->pte.vpage);
+   hlist_add_head(&pte->list_vpte, &vcpu->arch.hpte_hash_vpte[index]);
+
+   /* Add to vPTE_long list */
+   index = kvmppc_mmu_hash_vpte_long(pte->pte.vpage);
+   hlist_add_head(&pte->list_vpte_long,
+  &vcpu->arch.hpte_hash_vpte_long[index]);
+}
+
+static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
+{
+   dprintk_mmu("KVM: Flushing SPT: 0x%lx (0x%llx) -> 0x%llx\n",
+   pte->pte.eaddr, pte->pte.vpage, pte->host_va);
+
+   /* Different for 32 and 64 bit */
+   kvmppc_mmu_invalidate_pte(vcpu, pte);
+
+   if (pte->pte.may_write)
+   kvm_release_pfn_dirty(pte->pfn);
+   else
+   kvm_release_pfn_clean(pte->pfn);
+
+   hlist_del(&pte->list_pte);
+   hlist_del(&pte->list_vpte);
+   hlist_del(&pte->list_vpte_long);
+
+   vcpu->arch.hpte_cache_count--;
+   kmem_cache_free(hpte_cache, pte);
+}
+
+static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
+{
+   struct hpte_cache *pte;
+   struct hlist_node *node, *tmp;
+   int i;
+
+   for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
+   struct hlist_head *list = &vcpu->arch.hpte_hash_vpte_long[i];
+
+   hlist_for_each_entry_safe(pte, node, tmp, list, list_vpte_long)
+   invalidate_pte(vcpu, pte);
+   }
+}
+
+static void kvmppc_mmu_pte_flush_page(struct kvm_vcpu *vcpu, ulong guest_ea)
+{
+   struct hlist_head *list;
+   struct hlist_node *node, *tmp;
+   struct hpte_cache *pte;
+
+   /* Find the list of entries in the map */
+   list = &vcpu->arch.hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)];
+
+   /* Check the list for matching entries and invalidate */
+   hlist_for_each_entry_safe(pte, node, tmp, list, list_pte)
+   if ((pte->pte.eaddr & ~0xfffUL) == g

Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-27 Thread Benjamin Herrenschmidt
On Sun, 2010-06-27 at 10:53 +0300, Avi Kivity wrote:
> On 06/27/2010 01:58 AM, Benjamin Herrenschmidt wrote:
> >
> >> Then mmu intensive loads can expect to be slow.
> >>  
> > Well, depends. ppc64 indeed requires the hash to be managed by the
> > hypervisor, so inserting or invalidating translations will mean a
> > roundtrip to the hypervisor, though there are ways at least the
> > insertion could be alleviated (for example, the HV could service the
> > hash misses directly walking the guest page tables).
> >
> 
> But the guest page tables are software defined, no?  That means the 
> interface will break if the page table format changes.

Yes. Unless the hypervisor or architecture defines the format to be
used :-) IE. That's what Niagara 1 did. But we don't do that indeed
currently.

> > But that's due in part to a design choice (whether it's a good one or
> > not I'm not going to argue here) which favors huge reasonably static
> > workloads where the hash is expected to contain all translations for
> > everything.
> >
> 
> What about when you have memory pressure?  The hash will have to reflect 
> those pte_clear_flush_young(), no?

Well, our architects would argue that the kind of workloads we target
don't have memory pressure :-)

But yes, I agree, harvesting of dirty and young bits is going to force a
hash flush which can be pretty expensive. Heh, we've been trying to
convince our own architects at designers that the MMU sucks for long
enough...

> It seems horribly expensive.
> 
> > However, note that BookE (the embedded variant of the architecture) uses
> > a different model for virtualization, including options in its latest
> > variant for a HW logical->real translation (via a small dedicated TLB)
> > and direct access to some TLB ops from the guest.
> >
> 
> I'm somewhat familiar with it, yes.

Cheers,
Ben.


___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-27 Thread Avi Kivity

On 06/27/2010 01:58 AM, Benjamin Herrenschmidt wrote:



Then mmu intensive loads can expect to be slow.
 

Well, depends. ppc64 indeed requires the hash to be managed by the
hypervisor, so inserting or invalidating translations will mean a
roundtrip to the hypervisor, though there are ways at least the
insertion could be alleviated (for example, the HV could service the
hash misses directly walking the guest page tables).
   


But the guest page tables are software defined, no?  That means the 
interface will break if the page table format changes.



But that's due in part to a design choice (whether it's a good one or
not I'm not going to argue here) which favors huge reasonably static
workloads where the hash is expected to contain all translations for
everything.
   


What about when you have memory pressure?  The hash will have to reflect 
those pte_clear_flush_young(), no?


It seems horribly expensive.


However, note that BookE (the embedded variant of the architecture) uses
a different model for virtualization, including options in its latest
variant for a HW logical->real translation (via a small dedicated TLB)
and direct access to some TLB ops from the guest.
   


I'm somewhat familiar with it, yes.

--
error compiling committee.c: too many arguments to function

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-26 Thread Benjamin Herrenschmidt
On Tue, 2010-06-22 at 15:20 +0300, Avi Kivity wrote:
> On 06/22/2010 03:14 PM, Alexander Graf wrote:
> > Avi Kivity wrote:
> >
> >> On 06/22/2010 03:10 PM, Alexander Graf wrote:
> >>  
> >>> If you have more performance hints, I'll gladly take them :).
> >>>
> >>>
> >> Using a cpu that virtualizes the mmu in hardware helps tremendously.
> >>
> >>  
> > PPC never does that. Even with the virtualization extensions the MMU is
> > still software managed.
> 
> Then mmu intensive loads can expect to be slow.

Well, depends. ppc64 indeed requires the hash to be managed by the
hypervisor, so inserting or invalidating translations will mean a
roundtrip to the hypervisor, though there are ways at least the
insertion could be alleviated (for example, the HV could service the
hash misses directly walking the guest page tables).

But that's due in part to a design choice (whether it's a good one or
not I'm not going to argue here) which favors huge reasonably static
workloads where the hash is expected to contain all translations for
everything.

However, note that BookE (the embedded variant of the architecture) uses
a different model for virtualization, including options in its latest
variant for a HW logical->real translation (via a small dedicated TLB)
and direct access to some TLB ops from the guest.

> > I was also more thinking of hints like
> > "kmem_cache_zalloc is slow" or so ;).
> >
> 
> Stuff like that is usually worthless.  To give real feedback I need to 
> understand the hardware, so I'm reduced to coding style and indentation 
> review.

In that case, I'd say that BAT manipulation is rare enough (mostly only
at boot time) to warrant indeed speeding up the normal PTE operations &
invalidations at the expense of the BAT change case.

Cheers,
Ben.


___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-22 Thread Avi Kivity

On 06/22/2010 03:14 PM, Alexander Graf wrote:

Avi Kivity wrote:
   

On 06/22/2010 03:10 PM, Alexander Graf wrote:
 

If you have more performance hints, I'll gladly take them :).

   

Using a cpu that virtualizes the mmu in hardware helps tremendously.

 

PPC never does that. Even with the virtualization extensions the MMU is
still software managed.


Then mmu intensive loads can expect to be slow.


I was also more thinking of hints like
"kmem_cache_zalloc is slow" or so ;).
   


Stuff like that is usually worthless.  To give real feedback I need to 
understand the hardware, so I'm reduced to coding style and indentation 
review.


--
error compiling committee.c: too many arguments to function

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-22 Thread Alexander Graf
Avi Kivity wrote:
> On 06/22/2010 03:10 PM, Alexander Graf wrote:
>> If you have more performance hints, I'll gladly take them :).
>>
>
> Using a cpu that virtualizes the mmu in hardware helps tremendously.
>

PPC never does that. Even with the virtualization extensions the MMU is
still software managed. I was also more thinking of hints like
"kmem_cache_zalloc is slow" or so ;).


Alex

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-22 Thread Avi Kivity

On 06/22/2010 03:10 PM, Alexander Graf wrote:

If you have more performance hints, I'll gladly take them :).
   


Using a cpu that virtualizes the mmu in hardware helps tremendously.

--
error compiling committee.c: too many arguments to function

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-22 Thread Alexander Graf
Avi Kivity wrote:
> On 06/22/2010 03:04 PM, Alexander Graf wrote:
>> Avi Kivity wrote:
>>   
>>> On 06/21/2010 04:44 PM, Alexander Graf wrote:
>>> 
 Currently the shadow paging code keeps an array of entries it knows
 about.
 Whenever the guest invalidates an entry, we loop through that entry,
 trying to invalidate matching parts.

 While this is a really simple implementation, it is probably the most
 ineffective one possible. So instead, let's keep an array of lists
 around
 that are indexed by a hash. This way each PTE can be added by 4
 list_add,
 removed by 4 list_del invocations and the search only needs to loop
 through
 entries that share the same hash.

 This patch implements said lookup and exports generic functions that
 both
 the 32-bit and 64-bit backend can use.


>>> Mind explaining the all list in there?
>>>  
>> The all list is used to flush all entries when we need to get rid of all
>> entries, for example when we write a BAT.
>>
>>
>
> Yes, I more or less gathered that when I saw patch 2.  Does it make
> sense to avoid it by looping over all vpte lists in the vpte hash? 
> More effort for a full flush, esp. when the mmu is sparse, but less
> for individual pte operations.

Hrm. We could probably make the vpte_long list shorter. Currently all
lists are 1 << 13 entries wide. So we have 8192 lists to loop through.
For vpte_long 1 << 8 = 256 is probably enough. With that it would
probably make sense, yes.

If you have more performance hints, I'll gladly take them :).


Alex

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-22 Thread Avi Kivity

On 06/22/2010 03:04 PM, Alexander Graf wrote:

Avi Kivity wrote:
   

On 06/21/2010 04:44 PM, Alexander Graf wrote:
 

Currently the shadow paging code keeps an array of entries it knows
about.
Whenever the guest invalidates an entry, we loop through that entry,
trying to invalidate matching parts.

While this is a really simple implementation, it is probably the most
ineffective one possible. So instead, let's keep an array of lists
around
that are indexed by a hash. This way each PTE can be added by 4
list_add,
removed by 4 list_del invocations and the search only needs to loop
through
entries that share the same hash.

This patch implements said lookup and exports generic functions that
both
the 32-bit and 64-bit backend can use.

   

Mind explaining the all list in there?
 

The all list is used to flush all entries when we need to get rid of all
entries, for example when we write a BAT.

   


Yes, I more or less gathered that when I saw patch 2.  Does it make 
sense to avoid it by looping over all vpte lists in the vpte hash?  More 
effort for a full flush, esp. when the mmu is sparse, but less for 
individual pte operations.


--
error compiling committee.c: too many arguments to function

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-22 Thread Alexander Graf
Avi Kivity wrote:
> On 06/21/2010 04:44 PM, Alexander Graf wrote:
>> Currently the shadow paging code keeps an array of entries it knows
>> about.
>> Whenever the guest invalidates an entry, we loop through that entry,
>> trying to invalidate matching parts.
>>
>> While this is a really simple implementation, it is probably the most
>> ineffective one possible. So instead, let's keep an array of lists
>> around
>> that are indexed by a hash. This way each PTE can be added by 4
>> list_add,
>> removed by 4 list_del invocations and the search only needs to loop
>> through
>> entries that share the same hash.
>>
>> This patch implements said lookup and exports generic functions that
>> both
>> the 32-bit and 64-bit backend can use.
>>
>
> Mind explaining the all list in there?

The all list is used to flush all entries when we need to get rid of all
entries, for example when we write a BAT.

>
>>
>> +
>> +static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
>> +return hash_64(eaddr>>  PTE_SIZE, HPTEG_HASH_BITS);
>> +}
>> +
>> +static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
>> +return hash_64(vpage&  0xfULL, HPTEG_HASH_BITS);
>> +}
>> +
>> +static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
>> +return hash_64((vpage&  0xff000ULL)>>  12, HPTEG_HASH_BITS);
>> +}
>>
>
> Please use ordinary formatting for the functions above.

Ouch.

>
>> +/* Flush with mask 0xff000 */
>> +static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu *vcpu, u64
>> guest_vp)
>> +{
>> +struct list_head *list;
>> +struct hpte_cache *pte, *tmp;
>> +u64 vp_mask = 0xff000ULL;
>> +
>> +list
>> =&vcpu->arch.hpte_hash_vpte_long[kvmppc_mmu_hash_vpte_long(guest_vp)];
>> +
>> +/* No entries to flush */
>> +if (!list)
>> +return;
>> +
>> +/* Check the list for matching entries */
>> +list_for_each_entry_safe(pte, tmp, list, list_vpte_long)
>> +/* Jump over the helper entry */
>> +if (&pte->list_vpte_long == list)
>> +continue;
>> +
>> +if ((pte->pte.vpage&  vp_mask) == guest_vp)
>> +invalidate_pte(vcpu, pte);
>> +}
>>
>
> C wants brackets around blocks.
>


Even more ouch.


Alex

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


Re: [PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-22 Thread Avi Kivity

On 06/21/2010 04:44 PM, Alexander Graf wrote:

Currently the shadow paging code keeps an array of entries it knows about.
Whenever the guest invalidates an entry, we loop through that entry,
trying to invalidate matching parts.

While this is a really simple implementation, it is probably the most
ineffective one possible. So instead, let's keep an array of lists around
that are indexed by a hash. This way each PTE can be added by 4 list_add,
removed by 4 list_del invocations and the search only needs to loop through
entries that share the same hash.

This patch implements said lookup and exports generic functions that both
the 32-bit and 64-bit backend can use.
   


Mind explaining the all list in there?



+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+   return hash_64(eaddr>>  PTE_SIZE, HPTEG_HASH_BITS);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+   return hash_64(vpage&  0xfULL, HPTEG_HASH_BITS);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+   return hash_64((vpage&  0xff000ULL)>>  12, HPTEG_HASH_BITS);
+}
   


Please use ordinary formatting for the functions above.


+/* Flush with mask 0xff000 */
+static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu *vcpu, u64 guest_vp)
+{
+   struct list_head *list;
+   struct hpte_cache *pte, *tmp;
+   u64 vp_mask = 0xff000ULL;
+
+   list 
=&vcpu->arch.hpte_hash_vpte_long[kvmppc_mmu_hash_vpte_long(guest_vp)];
+
+   /* No entries to flush */
+   if (!list)
+   return;
+
+   /* Check the list for matching entries */
+   list_for_each_entry_safe(pte, tmp, list, list_vpte_long)
+   /* Jump over the helper entry */
+   if (&pte->list_vpte_long == list)
+   continue;
+
+   if ((pte->pte.vpage&  vp_mask) == guest_vp)
+   invalidate_pte(vcpu, pte);
+}
   


C wants brackets around blocks.

--
error compiling committee.c: too many arguments to function

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev


[PATCH 1/2] KVM: PPC: Add generic hpte management functions

2010-06-21 Thread Alexander Graf
Currently the shadow paging code keeps an array of entries it knows about.
Whenever the guest invalidates an entry, we loop through that entry,
trying to invalidate matching parts.

While this is a really simple implementation, it is probably the most
ineffective one possible. So instead, let's keep an array of lists around
that are indexed by a hash. This way each PTE can be added by 4 list_add,
removed by 4 list_del invocations and the search only needs to loop through
entries that share the same hash.

This patch implements said lookup and exports generic functions that both
the 32-bit and 64-bit backend can use.

Signed-off-by: Alexander Graf 
---
 arch/powerpc/kvm/book3s_mmu_hpte.c |  287 
 1 files changed, 287 insertions(+), 0 deletions(-)
 create mode 100644 arch/powerpc/kvm/book3s_mmu_hpte.c

diff --git a/arch/powerpc/kvm/book3s_mmu_hpte.c 
b/arch/powerpc/kvm/book3s_mmu_hpte.c
new file mode 100644
index 000..8ee0f1e
--- /dev/null
+++ b/arch/powerpc/kvm/book3s_mmu_hpte.c
@@ -0,0 +1,287 @@
+/*
+ * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
+ *
+ * Authors:
+ * Alexander Graf 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define PTE_SIZE   12
+
+/* #define DEBUG_MMU */
+/* #define DEBUG_SLB */
+
+#ifdef DEBUG_MMU
+#define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
+#else
+#define dprintk_mmu(a, ...) do { } while(0)
+#endif
+
+#ifdef DEBUG_SLB
+#define dprintk_slb(a, ...) printk(KERN_INFO a, __VA_ARGS__)
+#else
+#define dprintk_slb(a, ...) do { } while(0)
+#endif
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+   return hash_64(eaddr >> PTE_SIZE, HPTEG_HASH_BITS);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+   return hash_64(vpage & 0xfULL, HPTEG_HASH_BITS);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+   return hash_64((vpage & 0xff000ULL) >> 12, HPTEG_HASH_BITS);
+}
+
+void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
+{
+   u64 index;
+
+   /* Add to ePTE list */
+   index = kvmppc_mmu_hash_pte(pte->pte.eaddr);
+   list_add(&pte->list_pte, &vcpu->arch.hpte_hash_pte[index]);
+
+   /* Add to vPTE list */
+   index = kvmppc_mmu_hash_vpte(pte->pte.vpage);
+   list_add(&pte->list_vpte, &vcpu->arch.hpte_hash_vpte[index]);
+
+   /* Add to vPTE_long list */
+   index = kvmppc_mmu_hash_vpte_long(pte->pte.vpage);
+   list_add(&pte->list_vpte_long, &vcpu->arch.hpte_hash_vpte_long[index]);
+
+   /* Add to all list */
+   list_add(&pte->list_all, &vcpu->arch.hpte_all);
+}
+
+static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
+{
+   dprintk_mmu("KVM: Flushing SPT: 0x%lx (0x%llx) -> 0x%llx\n",
+   pte->pte.eaddr, pte->pte.vpage, pte->host_va);
+
+   /* Different for 32 and 64 bit */
+   kvmppc_mmu_invalidate_pte(vcpu, pte);
+
+   if (pte->pte.may_write)
+   kvm_release_pfn_dirty(pte->pfn);
+   else
+   kvm_release_pfn_clean(pte->pfn);
+
+   list_del(&pte->list_pte);
+   list_del(&pte->list_vpte);
+   list_del(&pte->list_vpte_long);
+   list_del(&pte->list_all);
+
+   kmem_cache_free(vcpu->arch.hpte_cache, pte);
+}
+
+static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
+{
+   struct hpte_cache *pte, *tmp;
+
+   list_for_each_entry_safe(pte, tmp, &vcpu->arch.hpte_all, list_all) {
+   /* Jump over the helper entry */
+   if (&pte->list_all == &vcpu->arch.hpte_all)
+   continue;
+
+   invalidate_pte(vcpu, pte);
+   }
+}
+
+void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
+{
+   u64 i;
+
+   dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%lx & 0x%lx\n",
+   vcpu->arch.hpte_cache_count, guest_ea, ea_mask);
+
+   switch (ea_mask) {
+   case ~0xfffUL:
+   {
+   struct list_head *list;
+   struct hpte_cache *pte, *tmp;
+
+   /* Find the list of entries in the map */
+   list = &vcpu->arch.hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)];
+
+   /* Check the list for matching entries */
+   lis