Skip to content

Commit 342d3db

Browse files
paulusmackavikivity
authored andcommitted
KVM: PPC: Implement MMU notifiers for Book3S HV guests
This adds the infrastructure to enable us to page out pages underneath a Book3S HV guest, on processors that support virtualized partition memory, that is, POWER7. Instead of pinning all the guest's pages, we now look in the host userspace Linux page tables to find the mapping for a given guest page. Then, if the userspace Linux PTE gets invalidated, kvm_unmap_hva() gets called for that address, and we replace all the guest HPTEs that refer to that page with absent HPTEs, i.e. ones with the valid bit clear and the HPTE_V_ABSENT bit set, which will cause an HDSI when the guest tries to access them. Finally, the page fault handler is extended to reinstantiate the guest HPTE when the guest tries to access a page which has been paged out. Since we can't intercept the guest DSI and ISI interrupts on PPC970, we still have to pin all the guest pages on PPC970. We have a new flag, kvm->arch.using_mmu_notifiers, that indicates whether we can page guest pages out. If it is not set, the MMU notifier callbacks do nothing and everything operates as before. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Alexander Graf <agraf@suse.de> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 697d389 commit 342d3db

11 files changed

Lines changed: 499 additions & 65 deletions

File tree

‎arch/powerpc/include/asm/kvm_book3s.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
143143
extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
144144
extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
145145
extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
146+
extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
147+
unsigned long *rmap, long pte_index, int realmode);
148+
extern void kvmppc_invalidate_hpte(struct kvm *kvm, unsigned long *hptep,
149+
unsigned long pte_index);
146150
extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
147151
unsigned long *nb_ret);
148152
extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr);

‎arch/powerpc/include/asm/kvm_book3s_64.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,37 @@ static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type)
136136
return (wimg & (HPTE_R_W | HPTE_R_I)) == io_type;
137137
}
138138

139+
/*
140+
* Lock and read a linux PTE. If it's present and writable, atomically
141+
* set dirty and referenced bits and return the PTE, otherwise return 0.
142+
*/
143+
static inline pte_t kvmppc_read_update_linux_pte(pte_t *p)
144+
{
145+
pte_t pte, tmp;
146+
147+
/* wait until _PAGE_BUSY is clear then set it atomically */
148+
__asm__ __volatile__ (
149+
"1: ldarx %0,0,%3\n"
150+
" andi. %1,%0,%4\n"
151+
" bne- 1b\n"
152+
" ori %1,%0,%4\n"
153+
" stdcx. %1,0,%3\n"
154+
" bne- 1b"
155+
: "=&r" (pte), "=&r" (tmp), "=m" (*p)
156+
: "r" (p), "i" (_PAGE_BUSY)
157+
: "cc");
158+
159+
if (pte_present(pte)) {
160+
pte = pte_mkyoung(pte);
161+
if (pte_write(pte))
162+
pte = pte_mkdirty(pte);
163+
}
164+
165+
*p = pte; /* clears _PAGE_BUSY */
166+
167+
return pte;
168+
}
169+
139170
/* Return HPTE cache control bits corresponding to Linux pte bits */
140171
static inline unsigned long hpte_cache_bits(unsigned long pte_val)
141172
{

‎arch/powerpc/include/asm/kvm_host.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/atomic.h>
3333
#include <asm/kvm_asm.h>
3434
#include <asm/processor.h>
35+
#include <asm/page.h>
3536

3637
#define KVM_MAX_VCPUS NR_CPUS
3738
#define KVM_MAX_VCORES NR_CPUS
@@ -44,6 +45,19 @@
4445
#define KVM_COALESCED_MMIO_PAGE_OFFSET 1
4546
#endif
4647

48+
#ifdef CONFIG_KVM_BOOK3S_64_HV
49+
#include <linux/mmu_notifier.h>
50+
51+
#define KVM_ARCH_WANT_MMU_NOTIFIER
52+
53+
struct kvm;
54+
extern int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
55+
extern int kvm_age_hva(struct kvm *kvm, unsigned long hva);
56+
extern int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
57+
extern void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
58+
59+
#endif
60+
4761
/* We don't currently support large pages. */
4862
#define KVM_HPAGE_GFN_SHIFT(x) 0
4963
#define KVM_NR_PAGE_SIZES 1
@@ -212,6 +226,7 @@ struct kvm_arch {
212226
struct kvmppc_rma_info *rma;
213227
unsigned long vrma_slb_v;
214228
int rma_setup_done;
229+
int using_mmu_notifiers;
215230
struct list_head spapr_tce_tables;
216231
spinlock_t slot_phys_lock;
217232
unsigned long *slot_phys[KVM_MEM_SLOTS_NUM];
@@ -460,6 +475,7 @@ struct kvm_vcpu_arch {
460475
struct list_head run_list;
461476
struct task_struct *run_task;
462477
struct kvm_run *kvm_run;
478+
pgd_t *pgdir;
463479
#endif
464480
};
465481

‎arch/powerpc/include/asm/reg.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,9 @@
495495
#define SPRN_SPRG7 0x117 /* Special Purpose Register General 7 */
496496
#define SPRN_SRR0 0x01A /* Save/Restore Register 0 */
497497
#define SPRN_SRR1 0x01B /* Save/Restore Register 1 */
498+
#define SRR1_ISI_NOPT 0x40000000 /* ISI: Not found in hash */
499+
#define SRR1_ISI_N_OR_G 0x10000000 /* ISI: Access is no-exec or G */
500+
#define SRR1_ISI_PROT 0x08000000 /* ISI: Other protection fault */
498501
#define SRR1_WAKEMASK 0x00380000 /* reason for wakeup */
499502
#define SRR1_WAKESYSERR 0x00300000 /* System error */
500503
#define SRR1_WAKEEE 0x00200000 /* External interrupt */

‎arch/powerpc/kvm/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ config KVM_BOOK3S_64
6969
config KVM_BOOK3S_64_HV
7070
bool "KVM support for POWER7 and PPC970 using hypervisor mode in host"
7171
depends on KVM_BOOK3S_64
72+
select MMU_NOTIFIER
7273
---help---
7374
Support running unmodified book3s_64 guest kernels in
7475
virtual machines on POWER7 and PPC970 processors that have

0 commit comments

Comments
 (0)