KVM PPC PV framework v3
Subsystems (7)
cover KVM PPC PV framework v3
On PPC we run PR=0 (kernel mode) code in PR=1 (user mode) and don't use the hypervisor extensions.
While that is all great to show that virtualization is possible, there are quite some cases where the emulation overhead of privileged instructions is killing performance.
This patchset tackles exactly that issue. It introduces a paravirtual framework using which KVM and Linux share a page to exchange register state with. That way we don't have to switch to the hypervisor just to change a value of a privileged register.
To prove my point, I ran the same test I did for the MMU optimizations against the PV framework. Here are the results:
[without]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello > /dev/null; donereal 0m14.659s user 0m8.967s sys 0m5.688s
[with]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello > /dev/null; donereal 0m7.557s user 0m4.121s sys 0m3.426s
So this is a significant performance improvement! I'm quite happy how fast this whole thing becomes :)
I tried to take all comments I've heard from people so far about such a PV framework into account. In case you told me something before that is a no-go and I still did it, please just tell me again.
To make use of this whole thing you also need patches to qemu and openbios. I have them in my queue, but want to see this set upstream first before I start sending patches to the other projects.
Now go and have fun with fast VMs on PPC! Get yourself a G5 on ebay and start experiencing the power yourself. - heh
v1 -> v2:
- change hypervisor calls to use r0 and r3
- make crit detection only trigger in supervisor mode
- RMO -> PAM
- introduce kvm_patch_ins
- only flush icache when patching
- introduce kvm_patch_ins_b
- update documentation
v2 -> v3:
- use pPAPR conventions for hypercall interface
- only use r0 as magic sc number
- remove PVR detection
- remove BookE shared page mapping support
- combine book3s-64 and -32 magic page ra override
- add self-test check if the mapping works to guest code
- add safety check for relocatable kernels
Alexander Graf (27):
KVM: PPC: Introduce shared page
KVM: PPC: Convert MSR to shared page
KVM: PPC: Convert DSISR to shared page
KVM: PPC: Convert DAR to shared page.
KVM: PPC: Convert SRR0 and SRR1 to shared page
KVM: PPC: Convert SPRG[0-4] to shared page
KVM: PPC: Implement hypervisor interface
KVM: PPC: Add PV guest critical sections
KVM: PPC: Add PV guest scratch registers
KVM: PPC: Tell guest about pending interrupts
KVM: PPC: Make PAM a define
KVM: PPC: First magic page steps
KVM: PPC: Magic Page Book3s support
KVM: PPC: Expose magic page support to guest
KVM: Move kvm_guest_init out of generic code
KVM: PPC: Generic KVM PV guest support
KVM: PPC: KVM PV guest stubs
KVM: PPC: PV instructions to loads and stores
KVM: PPC: PV tlbsync to nop
KVM: PPC: Introduce kvm_tmp framework
KVM: PPC: Introduce branch patching helper
KVM: PPC: PV assembler helpers
KVM: PPC: PV mtmsrd L=1
KVM: PPC: PV mtmsrd L=0 and mtmsr
KVM: PPC: PV wrteei
KVM: PPC: Add Documentation about PV interface
KVM: PPC: Add get_pvinfo interface to query hypercall instructions
Documentation/kvm/api.txt | 23 ++ Documentation/kvm/ppc-pv.txt | 180 +++++++++++ arch/powerpc/include/asm/kvm_book3s.h | 2 +- arch/powerpc/include/asm/kvm_host.h | 15 +- arch/powerpc/include/asm/kvm_para.h | 135 ++++++++- arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/kernel/Makefile | 2 + arch/powerpc/kernel/asm-offsets.c | 18 +- arch/powerpc/kernel/kvm.c | 485 ++++++++++++++++++++++++++++++ arch/powerpc/kernel/kvm_emul.S | 247 +++++++++++++++ arch/powerpc/kvm/44x.c | 7 + arch/powerpc/kvm/44x_tlb.c | 8 +- arch/powerpc/kvm/book3s.c | 188 ++++++++---- arch/powerpc/kvm/book3s_32_mmu.c | 28 ++- arch/powerpc/kvm/book3s_32_mmu_host.c | 6 +- arch/powerpc/kvm/book3s_64_mmu.c | 42 +++- arch/powerpc/kvm/book3s_64_mmu_host.c | 13 +- arch/powerpc/kvm/book3s_emulate.c | 25 +- arch/powerpc/kvm/book3s_paired_singles.c | 11 +- arch/powerpc/kvm/booke.c | 83 ++++-- arch/powerpc/kvm/booke.h | 6 +- arch/powerpc/kvm/booke_emulate.c | 14 +- arch/powerpc/kvm/booke_interrupts.S | 3 +- arch/powerpc/kvm/e500.c | 7 + arch/powerpc/kvm/e500_tlb.c | 12 +- arch/powerpc/kvm/e500_tlb.h | 2 +- arch/powerpc/kvm/emulate.c | 36 ++- arch/powerpc/kvm/powerpc.c | 84 +++++- arch/powerpc/platforms/Kconfig | 10 + arch/x86/include/asm/kvm_para.h | 6 + include/linux/kvm.h | 11 + include/linux/kvm_para.h | 7 +- 32 files changed, 1538 insertions(+), 179 deletions(-) create mode 100644 Documentation/kvm/ppc-pv.txt create mode 100644 arch/powerpc/kernel/kvm.c create mode 100644 arch/powerpc/kernel/kvm_emul.S
A message that is not in the archive (sent off-list or not yet received): 1280407688-9815-1-git-send-email-agraf-l3A5Bk7waGM@public.gmane.org
PATCH 1/27 KVM: PPC: Introduce shared page
For transparent variable sharing between the hypervisor and guest, I introduce a shared page. This shared page will contain all the registers the guest can read and write safely without exiting guest context.
This patch only implements the stubs required for the basic structure of the shared page. The actual register moving follows.
--- arch/powerpc/include/asm/kvm_host.h | 2 ++ arch/powerpc/include/asm/kvm_para.h | 5 +++++ arch/powerpc/kernel/asm-offsets.c | 1 + arch/powerpc/kvm/44x.c | 7 +++++++ arch/powerpc/kvm/book3s.c | 9 ++++++++- arch/powerpc/kvm/e500.c | 7 +++++++ 6 files changed, 30 insertions(+), 1 deletions(-)
arch/powerpc/include/asm/kvm_host.h +2 -0
@@ -25,6 +25,7 @@#include <linux/interrupt.h> #include <linux/types.h> #include <linux/kvm_types.h> +#include <linux/kvm_para.h> #include <asm/kvm_asm.h> #define KVM_MAX_VCPUS 1
@@ -290,6 +291,7 @@ struct kvm_vcpu_arch {struct tasklet_struct tasklet;u64 dec_jiffies;unsigned long pending_exceptions; +struct kvm_vcpu_arch_shared *shared;#ifdef CONFIG_PPC_BOOK3S struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE ];
arch/powerpc/include/asm/kvm_para.h +5 -0
@@ -20,6 +20,11 @@#ifndef __POWERPC_KVM_PARA_H__#define __POWERPC_KVM_PARA_H__ +#include <linux/types.h> + +struct kvm_vcpu_arch_shared { +}; +#ifdef __KERNEL__static inline int kvm_para_available (void )
arch/powerpc/kernel/asm-offsets.c +1 -0
@@ -400,6 +400,7 @@ int main(void) DEFINE(VCPU_SPRG6, offsetof(struct kvm_vcpu, arch.sprg6)); DEFINE(VCPU_SPRG7, offsetof(struct kvm_vcpu, arch.sprg7)); DEFINE(VCPU_SHADOW_PID, offsetof(struct kvm_vcpu, arch.shadow_pid)); + DEFINE(VCPU_SHARED, offsetof(struct kvm_vcpu, arch.shared));/* book3s */ #ifdef CONFIG_PPC_BOOK3S
arch/powerpc/kvm/44x.c +7 -0
@@ -123,8 +123,14 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)if (err) goto free_vcpu; + vcpu->arch.shared = (void *)__get_free_page (GFP_KERNEL |__GFP_ZERO); +if (!vcpu->arch.shared) + goto uninit_vcpu; +return vcpu; +uninit_vcpu: +kvm_vcpu_uninit (vcpu); free_vcpu:kmem_cache_free (kvm_vcpu_cache, vcpu_44x); out:
@@ -135,6 +141,7 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) {struct kvmppc_vcpu_44x *vcpu_44x =to_44x (vcpu); +free_page ((unsigned long )vcpu->arch.shared);kvm_vcpu_uninit (vcpu);kmem_cache_free (kvm_vcpu_cache, vcpu_44x); }
arch/powerpc/kvm/book3s.c +8 -1
@@ -1242,6 +1242,10 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)if (err) goto free_shadow_vcpu; + vcpu->arch.shared = (void *)__get_free_page (GFP_KERNEL |__GFP_ZERO); +if (!vcpu->arch.shared) + goto uninit_vcpu; + vcpu->arch.host_retip = kvm_return_point; vcpu->arch.host_msr =mfmsr ();#ifdef CONFIG_PPC_BOOK3S_64
@@ -1268,10 +1272,12 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) err =kvmppc_mmu_init (vcpu);if (err <0 ) - goto free_shadow_vcpu; + goto uninit_vcpu;return vcpu; +uninit_vcpu: +kvm_vcpu_uninit (vcpu); free_shadow_vcpu:kfree (vcpu_book3s->shadow_vcpu); free_vcpu:
@@ -1284,6 +1290,7 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) {struct kvmppc_vcpu_book3s *vcpu_book3s =to_book3s (vcpu); +free_page ((unsigned long )vcpu->arch.shared);kvm_vcpu_uninit (vcpu);kfree (vcpu_book3s->shadow_vcpu);vfree (vcpu_book3s);
arch/powerpc/kvm/e500.c +7 -0
@@ -117,8 +117,14 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)if (err) goto uninit_vcpu; + vcpu->arch.shared = (void *)__get_free_page (GFP_KERNEL |__GFP_ZERO); +if (!vcpu->arch.shared) + goto uninit_tlb; +return vcpu; +uninit_tlb: +kvmppc_e500_tlb_uninit (vcpu_e500); uninit_vcpu:kvm_vcpu_uninit (vcpu); free_vcpu:
@@ -131,6 +137,7 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) {struct kvmppc_vcpu_e500 *vcpu_e500 =to_e500 (vcpu); +free_page ((unsigned long )vcpu->arch.shared);kvmppc_e500_tlb_uninit (vcpu_e500);kvm_vcpu_uninit (vcpu);kmem_cache_free (kvm_vcpu_cache, vcpu_e500);
Signature · 1 line
1.6.0.2
A message that is not in the archive (sent off-list or not yet received): 1280407688-9815-2-git-send-email-agraf-l3A5Bk7waGM@public.gmane.org
Re: [PATCH 01/27] KVM: PPC: Introduce shared page
Quoted 150 lines · Alexander Graf
For transparent variable sharing between the hypervisor and guest, I introduce a shared page. This shared page will contain all the registers the guest can read and write safely without exiting guest context.
This patch only implements the stubs required for the basic structure of the shared page. The actual register moving follows.
Signed-off-by Alexander Graf<[email protected]>--- arch/powerpc/include/asm/kvm_host.h | 2 ++ arch/powerpc/include/asm/kvm_para.h | 5 +++++ arch/powerpc/kernel/asm-offsets.c | 1 + arch/powerpc/kvm/44x.c | 7 +++++++ arch/powerpc/kvm/book3s.c | 9 ++++++++- arch/powerpc/kvm/e500.c | 7 +++++++ 6 files changed, 30 insertions(+), 1 deletions(-)arch/powerpc/include/asm/kvm_host.h +2
-0@@ -25,6 +25,7 @@#include <linux/interrupt.h> #include <linux/types.h> #include <linux/kvm_types.h> +#include <linux/kvm_para.h> #include <asm/kvm_asm.h> #define KVM_MAX_VCPUS 1@@ -290,6 +291,7 @@ struct kvm_vcpu_arch {struct tasklet_struct tasklet;u64 dec_jiffies;unsigned long pending_exceptions; +struct kvm_vcpu_arch_shared *shared;#ifdef CONFIG_PPC_BOOK3S struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE ];arch/powerpc/include/asm/kvm_para.h +5
-0@@ -20,6 +20,11 @@#ifndef __POWERPC_KVM_PARA_H__#define __POWERPC_KVM_PARA_H__ +#include <linux/types.h> + +struct kvm_vcpu_arch_shared { +}; +#ifdef __KERNEL__static inline int kvm_para_available (void )arch/powerpc/kernel/asm-offsets.c +1
-0@@ -400,6 +400,7 @@ int main(void) DEFINE(VCPU_SPRG6, offsetof(struct kvm_vcpu, arch.sprg6)); DEFINE(VCPU_SPRG7, offsetof(struct kvm_vcpu, arch.sprg7)); DEFINE(VCPU_SHADOW_PID, offsetof(struct kvm_vcpu, arch.shadow_pid)); + DEFINE(VCPU_SHARED, offsetof(struct kvm_vcpu, arch.shared));/* book3s */ #ifdef CONFIG_PPC_BOOK3S arch/powerpc/kvm/44x.c +7
-0@@ -123,8 +123,14 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)if (err) goto free_vcpu; + vcpu->arch.shared = (void *)__get_free_page (GFP_KERNEL |__GFP_ZERO); +if (!vcpu->arch.shared) + goto uninit_vcpu; +return vcpu; +uninit_vcpu: +kvm_vcpu_uninit (vcpu); free_vcpu:kmem_cache_free (kvm_vcpu_cache, vcpu_44x); out:@@ -135,6 +141,7 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) {struct kvmppc_vcpu_44x *vcpu_44x =to_44x (vcpu); +free_page ((unsigned long )vcpu->arch.shared);kvm_vcpu_uninit (vcpu);kmem_cache_free (kvm_vcpu_cache, vcpu_44x); }arch/powerpc/kvm/book3s.c +8
-1@@ -1242,6 +1242,10 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)if (err) goto free_shadow_vcpu; + vcpu->arch.shared = (void *)__get_free_page (GFP_KERNEL |__GFP_ZERO); +if (!vcpu->arch.shared) + goto uninit_vcpu; + vcpu->arch.host_retip = kvm_return_point; vcpu->arch.host_msr =mfmsr ();#ifdef CONFIG_PPC_BOOK3S_64 @@ -1268,10 +1272,12 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) err =kvmppc_mmu_init (vcpu);if (err<0 ) - goto free_shadow_vcpu; + goto uninit_vcpu;return vcpu; +uninit_vcpu: +kvm_vcpu_uninit (vcpu); free_shadow_vcpu:kfree (vcpu_book3s->shadow_vcpu); free_vcpu:@@ -1284,6 +1290,7 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) {struct kvmppc_vcpu_book3s *vcpu_book3s =to_book3s (vcpu); +free_page ((unsigned long )vcpu->arch.shared);kvm_vcpu_uninit (vcpu);kfree (vcpu_book3s->shadow_vcpu);vfree (vcpu_book3s);arch/powerpc/kvm/e500.c +6
-0@@ -117,8 +117,14 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)if (err) goto uninit_vcpu; + vcpu->arch.shared = (void *)__get_free_page (GFP_KERNEL |__GFP_ZERO); +if (!vcpu->arch.shared) + goto uninit_tlb; +return vcpu; +uninit_tlb: +kvmppc_e500_tlb_uninit (vcpu_e500); uninit_vcpu:kvm_vcpu_uninit (vcpu); free_vcpu:
arch/powerpc/kvm/e500.c void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) · line 143
Quoted 7 lines
arch/powerpc/kvm/e500.c +1
-0@@ -131,6 +137,7 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) {struct kvmppc_vcpu_e500 *vcpu_e500 =to_e500 (vcpu); +free_page ((unsigned long )vcpu->arch.shared);kvmppc_e500_tlb_uninit (vcpu_e500);kvm_vcpu_uninit (vcpu);kmem_cache_free (kvm_vcpu_cache, vcpu_e500);
Why not put all this in a common function like kvm_arch_vcpu_init()? There are layers of shared code inside arch/powerpc/kvm: e.g. powerpc.c -> booke.c -> 44x.c...
Hollis Blanchard
Mentor Graphics, Embedded Systems Division
PATCH 2/27 KVM: PPC: Convert MSR to shared page
In reply to a message not in the archiveOne of the most obvious registers to share with the guest directly is the MSR. The MSR contains the "interrupts enabled" flag which the guest has to toggle in critical sections.
So in order to bring the overhead of interrupt en- and disabling down, let's put msr into the shared page. Keep in mind that even though you can fully read its contents, writing to it doesn't always update all state. There are a few safe fields that don't require hypervisor interaction. See the documentation for a list of MSR bits that are safe to be set from inside the guest.
--- arch/powerpc/include/asm/kvm_host.h | 1 - arch/powerpc/include/asm/kvm_para.h | 1 + arch/powerpc/kernel/asm-offsets.c | 2 +- arch/powerpc/kvm/44x_tlb.c | 8 ++-- arch/powerpc/kvm/book3s.c | 65 ++++++++++++++++-------------- arch/powerpc/kvm/book3s_32_mmu.c | 12 +++--- arch/powerpc/kvm/book3s_32_mmu_host.c | 4 +- arch/powerpc/kvm/book3s_64_mmu.c | 12 +++--- arch/powerpc/kvm/book3s_64_mmu_host.c | 4 +- arch/powerpc/kvm/book3s_emulate.c | 9 ++-- arch/powerpc/kvm/book3s_paired_singles.c | 7 ++- arch/powerpc/kvm/booke.c | 20 +++++----- arch/powerpc/kvm/booke.h | 6 +- arch/powerpc/kvm/booke_emulate.c | 6 +- arch/powerpc/kvm/booke_interrupts.S | 3 +- arch/powerpc/kvm/e500_tlb.c | 12 +++--- arch/powerpc/kvm/e500_tlb.h | 2 +- arch/powerpc/kvm/powerpc.c | 3 +- 18 files changed, 93 insertions(+), 84 deletions(-)
arch/powerpc/include/asm/kvm_host.h +0 -1
@@ -211,7 +211,6 @@ struct kvm_vcpu_arch {u32 cr;#endif -ulong msr;#ifdef CONFIG_PPC_BOOK3S ulong shadow_msr;ulong hflags;
arch/powerpc/include/asm/kvm_para.h +1 -0
@@ -23,6 +23,7 @@#include <linux/types.h> struct kvm_vcpu_arch_shared { +__u64 msr; };#ifdef __KERNEL__
arch/powerpc/kernel/asm-offsets.c +1 -1
@@ -394,13 +394,13 @@ int main(void) DEFINE(VCPU_HOST_STACK, offsetof(struct kvm_vcpu, arch.host_stack)); DEFINE(VCPU_HOST_PID, offsetof(struct kvm_vcpu, arch.host_pid)); DEFINE(VCPU_GPRS, offsetof(struct kvm_vcpu, arch.gpr)); - DEFINE(VCPU_MSR, offsetof(struct kvm_vcpu, arch.msr)); DEFINE(VCPU_SPRG4, offsetof(struct kvm_vcpu, arch.sprg4)); DEFINE(VCPU_SPRG5, offsetof(struct kvm_vcpu, arch.sprg5)); DEFINE(VCPU_SPRG6, offsetof(struct kvm_vcpu, arch.sprg6)); DEFINE(VCPU_SPRG7, offsetof(struct kvm_vcpu, arch.sprg7)); DEFINE(VCPU_SHADOW_PID, offsetof(struct kvm_vcpu, arch.shadow_pid)); DEFINE(VCPU_SHARED, offsetof(struct kvm_vcpu, arch.shared)); +DEFINE (VCPU_SHARED_MSR , offsetof(struct kvm_vcpu_arch_shared , msr));/* book3s */ #ifdef CONFIG_PPC_BOOK3S
arch/powerpc/kvm/44x_tlb.c +4 -4
@@ -221,14 +221,14 @@ gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,int kvmppc_mmu_itlb_index (struct kvm_vcpu *vcpu,gva_t eaddr) { -unsigned int as = !!(vcpu->arch.msr &MSR_IS ); +unsigned int as = !!(vcpu->arch.shared->msr &MSR_IS );return kvmppc_44x_tlb_index (vcpu, eaddr, vcpu->arch.pid, as); }int kvmppc_mmu_dtlb_index (struct kvm_vcpu *vcpu,gva_t eaddr) { -unsigned int as = !!(vcpu->arch.msr &MSR_DS ); +unsigned int as = !!(vcpu->arch.shared->msr &MSR_DS );return kvmppc_44x_tlb_index (vcpu, eaddr, vcpu->arch.pid, as); }
@@ -353,7 +353,7 @@ void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr, stlbe.word1 = (hpaddr &0xfffffc00 ) | ((hpaddr >>32 ) &0xf ); stlbe.word2 =kvmppc_44x_tlb_shadow_attrib (flags, - vcpu->arch.msr &MSR_PR ); + vcpu->arch.shared->msr &MSR_PR ); stlbe.tid = !(asid &0xff );/* Keep track of the reference so we can properly release it later. */
@@ -422,7 +422,7 @@ static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,/* Does it match current guest AS? */ /* XXX what about IS != DS? */ -if (get_tlb_ts (tlbe) != !!(vcpu->arch.msr &MSR_IS )) +if (get_tlb_ts (tlbe) != !!(vcpu->arch.shared->msr &MSR_IS ))return 0 ; gpa =get_tlb_raddr (tlbe);
arch/powerpc/kvm/book3s.c +35 -30
192 lines not on this page · raw message
arch/powerpc/kvm/book3s_32_mmu.c +6 -6
@@ -133,7 +133,7 @@ static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,else bat = &vcpu_book3s->ibat[i]; -if (vcpu->arch.msr &MSR_PR ) { +if (vcpu->arch.shared->msr &MSR_PR ) {if (!bat->vp)continue ; }else {
@@ -214,8 +214,8 @@ static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr, pte->raddr = (pteg[i+1 ] & ~(0xFFFULL )) | (eaddr &0xFFF ); pp = pteg[i+1 ] &3 ; -if ((sre->Kp && (vcpu->arch.msr &MSR_PR )) || - (sre->Ks && !(vcpu->arch.msr &MSR_PR ))) +if ((sre->Kp && (vcpu->arch.shared->msr &MSR_PR )) || + (sre->Ks && !(vcpu->arch.shared->msr &MSR_PR ))) pp |=4 ; pte->may_write = false;
@@ -334,7 +334,7 @@ static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,struct kvmppc_sr *sr;u64 gvsid = esid; -if (vcpu->arch.msr & (MSR_DR |MSR_IR )) { +if (vcpu->arch.shared->msr & (MSR_DR |MSR_IR )) { sr =find_sr (to_book3s (vcpu), ea);if (sr->valid) gvsid = sr->vsid;
@@ -343,7 +343,7 @@ static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,/* In case we only have one of MSR_IR or MSR_DR set, let's put that in the real-mode context (and hope RM doesn't access high memory) */ -switch (vcpu->arch.msr & (MSR_DR |MSR_IR )) { +switch (vcpu->arch.shared->msr & (MSR_DR |MSR_IR )) {case 0 : *vsid =VSID_REAL | esid;break ;
@@ -363,7 +363,7 @@ static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,BUG (); } -if (vcpu->arch.msr &MSR_PR ) +if (vcpu->arch.shared->msr &MSR_PR ) *vsid |=VSID_PR ;return 0 ;
arch/powerpc/kvm/book3s_32_mmu_host.c +2 -2
@@ -86,7 +86,7 @@ static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)struct kvmppc_sid_map *map;u16 sid_map_mask; -if (vcpu->arch.msr &MSR_PR ) +if (vcpu->arch.shared->msr &MSR_PR ) gvsid |=VSID_PR ; sid_map_mask =kvmppc_sid_hash (vcpu, gvsid);
@@ -253,7 +253,7 @@ static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)u16 sid_map_mask;static int backwards_map =0 ; -if (vcpu->arch.msr &MSR_PR ) +if (vcpu->arch.shared->msr &MSR_PR ) gvsid |=VSID_PR ;/* We might get collisions that trap in preceding order, so let's
arch/powerpc/kvm/book3s_64_mmu.c +6 -6
@@ -180,9 +180,9 @@ do_second: goto no_page_found; } -if ((vcpu->arch.msr &MSR_PR ) && slbe->Kp) +if ((vcpu->arch.shared->msr &MSR_PR ) && slbe->Kp) key =4 ; -else if (!(vcpu->arch.msr &MSR_PR ) && slbe->Ks) +else if (!(vcpu->arch.shared->msr &MSR_PR ) && slbe->Ks) key =4 ;for (i=0 ; i<16 ; i+=2 ) {
@@ -381,7 +381,7 @@ static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu *vcpu)for (i =1 ; i < vcpu_book3s->slb_nr; i++) vcpu_book3s->slb[i].valid = false; -if (vcpu->arch.msr &MSR_IR ) { +if (vcpu->arch.shared->msr &MSR_IR ) {kvmppc_mmu_flush_segments (vcpu);kvmppc_mmu_map_segment (vcpu,kvmppc_get_pc (vcpu)); }
@@ -446,13 +446,13 @@ static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,struct kvmppc_slb *slb;u64 gvsid = esid; -if (vcpu->arch.msr & (MSR_DR |MSR_IR )) { +if (vcpu->arch.shared->msr & (MSR_DR |MSR_IR )) { slb =kvmppc_mmu_book3s_64_find_slbe (to_book3s (vcpu), ea);if (slb) gvsid = slb->vsid; } -switch (vcpu->arch.msr & (MSR_DR |MSR_IR )) { +switch (vcpu->arch.shared->msr & (MSR_DR |MSR_IR )) {case 0 : *vsid =VSID_REAL | esid;break ;
@@ -473,7 +473,7 @@ static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,break ; } -if (vcpu->arch.msr &MSR_PR ) +if (vcpu->arch.shared->msr &MSR_PR ) *vsid |=VSID_PR ;return 0 ;
arch/powerpc/kvm/book3s_64_mmu_host.c +2 -2
@@ -66,7 +66,7 @@ static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)struct kvmppc_sid_map *map;u16 sid_map_mask; -if (vcpu->arch.msr &MSR_PR ) +if (vcpu->arch.shared->msr &MSR_PR ) gvsid |=VSID_PR ; sid_map_mask =kvmppc_sid_hash (vcpu, gvsid);
@@ -191,7 +191,7 @@ static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)u16 sid_map_mask;static int backwards_map =0 ; -if (vcpu->arch.msr &MSR_PR ) +if (vcpu->arch.shared->msr &MSR_PR ) gvsid |=VSID_PR ;/* We might get collisions that trap in preceding order, so let's
arch/powerpc/kvm/book3s_emulate.c +5 -4
@@ -86,14 +86,15 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,case 31 :switch (get_xop (inst)) {case OP_31_XOP_MFMSR : -kvmppc_set_gpr (vcpu,get_rt (inst), vcpu->arch.msr); +kvmppc_set_gpr (vcpu,get_rt (inst), + vcpu->arch.shared->msr);break ;case OP_31_XOP_MTMSRD : {ulong rs =kvmppc_get_gpr (vcpu,get_rs (inst));if (inst &0x10000 ) { - vcpu->arch.msr &= ~(MSR_RI |MSR_EE ); - vcpu->arch.msr |= rs & (MSR_RI |MSR_EE ); + vcpu->arch.shared->msr &= ~(MSR_RI |MSR_EE ); + vcpu->arch.shared->msr |= rs & (MSR_RI |MSR_EE ); }else kvmppc_set_msr (vcpu, rs);break ;
@@ -204,7 +205,7 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, ra =kvmppc_get_gpr (vcpu,get_ra (inst)); addr = (ra + rb) & ~31ULL ; -if (!(vcpu->arch.msr &MSR_SF )) +if (!(vcpu->arch.shared->msr &MSR_SF )) addr &=0xffffffff ; vaddr = addr;
arch/powerpc/kvm/book3s_paired_singles.c +4 -3
@@ -165,9 +165,10 @@ static inline void kvmppc_sync_qpr(struct kvm_vcpu *vcpu, int rt)static void kvmppc_inject_pf (struct kvm_vcpu *vcpu,ulong eaddr,bool is_store) {u64 dsisr; +struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared; - vcpu->arch.msr =kvmppc_set_field (vcpu->arch.msr,33 ,36 ,0 ); - vcpu->arch.msr =kvmppc_set_field (vcpu->arch.msr,42 ,47 ,0 ); + shared->msr =kvmppc_set_field (shared->msr,33 ,36 ,0 ); + shared->msr =kvmppc_set_field (shared->msr,42 ,47 ,0 ); vcpu->arch.dear = eaddr;/* Page Fault */ dsisr =kvmppc_set_field (0 ,33 ,33 ,1 );
@@ -658,7 +659,7 @@ int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu)if (!kvmppc_inst_is_paired_single (vcpu, inst))return EMULATE_FAIL ; -if (!(vcpu->arch.msr &MSR_FP )) { +if (!(vcpu->arch.shared->msr &MSR_FP )) {kvmppc_book3s_queue_irqprio (vcpu,BOOK3S_INTERRUPT_FP_UNAVAIL );return EMULATE_AGAIN ; }
arch/powerpc/kvm/booke.c +10 -10
@@ -62,7 +62,7 @@ void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu) {int i; -printk ("pc: %08lx msr: %08lx\n" , vcpu->arch.pc, vcpu->arch.msr); +printk ("pc: %08lx msr: %08llx\n" , vcpu->arch.pc, vcpu->arch.shared->msr);printk ("lr: %08lx ctr: %08lx\n" , vcpu->arch.lr, vcpu->arch.ctr);printk ("srr0: %08lx srr1: %08lx\n" , vcpu->arch.srr0, vcpu->arch.srr1);
@@ -169,34 +169,34 @@ static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,break ;case BOOKE_IRQPRIO_CRITICAL :case BOOKE_IRQPRIO_WATCHDOG : - allowed = vcpu->arch.msr &MSR_CE ; + allowed = vcpu->arch.shared->msr &MSR_CE ; msr_mask =MSR_ME ;break ;case BOOKE_IRQPRIO_MACHINE_CHECK : - allowed = vcpu->arch.msr &MSR_ME ; + allowed = vcpu->arch.shared->msr &MSR_ME ; msr_mask =0 ;break ;case BOOKE_IRQPRIO_EXTERNAL :case BOOKE_IRQPRIO_DECREMENTER :case BOOKE_IRQPRIO_FIT : - allowed = vcpu->arch.msr &MSR_EE ; + allowed = vcpu->arch.shared->msr &MSR_EE ; msr_mask =MSR_CE |MSR_ME |MSR_DE ;break ;case BOOKE_IRQPRIO_DEBUG : - allowed = vcpu->arch.msr &MSR_DE ; + allowed = vcpu->arch.shared->msr &MSR_DE ; msr_mask =MSR_ME ;break ; }if (allowed) { vcpu->arch.srr0 = vcpu->arch.pc; - vcpu->arch.srr1 = vcpu->arch.msr; + vcpu->arch.srr1 = vcpu->arch.shared->msr; vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];if (update_esr == true) vcpu->arch.esr = vcpu->arch.queued_esr;if (update_dear == true) vcpu->arch.dear = vcpu->arch.queued_dear; -kvmppc_set_msr (vcpu, vcpu->arch.msr & msr_mask); +kvmppc_set_msr (vcpu, vcpu->arch.shared->msr & msr_mask);clear_bit (priority, &vcpu->arch.pending_exceptions); }
@@ -265,7 +265,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,break ;case BOOKE_INTERRUPT_PROGRAM : -if (vcpu->arch.msr &MSR_PR ) { +if (vcpu->arch.shared->msr &MSR_PR ) {/* Program traps generated by user-level software must be handled * by the guest kernel. */ kvmppc_core_queue_program (vcpu, vcpu->arch.fault_esr);
@@ -467,7 +467,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,int kvm_arch_vcpu_setup (struct kvm_vcpu *vcpu) { vcpu->arch.pc =0 ; - vcpu->arch.msr =0 ; + vcpu->arch.shared->msr =0 ;kvmppc_set_gpr (vcpu,1 , (16 <<20 ) -8 );/* -8 for the callee-save LR slot */ vcpu->arch.shadow_pid =1 ;
@@ -490,7 +490,7 @@ int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) regs->ctr = vcpu->arch.ctr; regs->lr = vcpu->arch.lr; regs->xer =kvmppc_get_xer (vcpu); - regs->msr = vcpu->arch.msr; + regs->msr = vcpu->arch.shared->msr; regs->srr0 = vcpu->arch.srr0; regs->srr1 = vcpu->arch.srr1; regs->pid = vcpu->arch.pid;
arch/powerpc/kvm/booke.h +3 -3
@@ -54,12 +54,12 @@ extern unsigned long kvmppc_booke_handlers;* changing. */ static inline void kvmppc_set_msr (struct kvm_vcpu *vcpu,u32 new_msr) { -if ((new_msr &MSR_PR ) != (vcpu->arch.msr &MSR_PR )) +if ((new_msr &MSR_PR ) != (vcpu->arch.shared->msr &MSR_PR ))kvmppc_mmu_priv_switch (vcpu, new_msr &MSR_PR ); - vcpu->arch.msr = new_msr; + vcpu->arch.shared->msr = new_msr; -if (vcpu->arch.msr &MSR_WE ) { +if (vcpu->arch.shared->msr &MSR_WE ) {kvm_vcpu_block (vcpu);kvmppc_set_exit_type (vcpu,EMULATED_MTMSRWE_EXITS ); };
arch/powerpc/kvm/booke_emulate.c +3 -3
@@ -62,7 +62,7 @@ int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,case OP_31_XOP_MFMSR : rt =get_rt (inst); -kvmppc_set_gpr (vcpu, rt, vcpu->arch.msr); +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->msr);kvmppc_set_exit_type (vcpu,EMULATED_MFMSR_EXITS );break ;
@@ -74,13 +74,13 @@ int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,case OP_31_XOP_WRTEE : rs =get_rs (inst); - vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE ) + vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE ) | (kvmppc_get_gpr (vcpu, rs) &MSR_EE );kvmppc_set_exit_type (vcpu,EMULATED_WRTEE_EXITS );break ;case OP_31_XOP_WRTEEI : - vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE ) + vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE ) | (inst &MSR_EE );kvmppc_set_exit_type (vcpu,EMULATED_WRTEE_EXITS );break ;
arch/powerpc/kvm/booke_interrupts.S +2 -1
@@ -415,7 +415,8 @@ lightweight_exit: lwz r8, VCPU_GPR(r8)(r4) lwz r3, VCPU_PC(r4) mtsrr0 r3 - lwz r3, VCPU_MSR(r4) + lwz r3, VCPU_SHARED(r4) + lwz r3, VCPU_SHARED_MSR(r3) oris r3, r3, KVMPPC_MSR_MASK@h ori r3, r3, KVMPPC_MSR_MASK@l mtsrr1 r3
arch/powerpc/kvm/e500_tlb.c +6 -6
@@ -317,10 +317,10 @@ static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID; stlbe->mas2 = (gvaddr &MAS2_EPN ) |e500_shadow_mas2_attrib (gtlbe->mas2, - vcpu_e500->vcpu.arch.msr &MSR_PR ); + vcpu_e500->vcpu.arch.shared->msr &MSR_PR ); stlbe->mas3 = (hpaddr &MAS3_RPN ) |e500_shadow_mas3_attrib (gtlbe->mas3, - vcpu_e500->vcpu.arch.msr &MSR_PR ); + vcpu_e500->vcpu.arch.shared->msr &MSR_PR ); stlbe->mas7 = (hpaddr >>32 ) &MAS7_RPN ; trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
@@ -579,28 +579,28 @@ int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)int kvmppc_mmu_itlb_index (struct kvm_vcpu *vcpu,gva_t eaddr) { -unsigned int as = !!(vcpu->arch.msr &MSR_IS ); +unsigned int as = !!(vcpu->arch.shared->msr &MSR_IS );return kvmppc_e500_tlb_search (vcpu, eaddr,get_cur_pid (vcpu), as); }int kvmppc_mmu_dtlb_index (struct kvm_vcpu *vcpu,gva_t eaddr) { -unsigned int as = !!(vcpu->arch.msr &MSR_DS ); +unsigned int as = !!(vcpu->arch.shared->msr &MSR_DS );return kvmppc_e500_tlb_search (vcpu, eaddr,get_cur_pid (vcpu), as); }void kvmppc_mmu_itlb_miss (struct kvm_vcpu *vcpu) { -unsigned int as = !!(vcpu->arch.msr &MSR_IS ); +unsigned int as = !!(vcpu->arch.shared->msr &MSR_IS );kvmppc_e500_deliver_tlb_miss (vcpu, vcpu->arch.pc, as); }void kvmppc_mmu_dtlb_miss (struct kvm_vcpu *vcpu) { -unsigned int as = !!(vcpu->arch.msr &MSR_DS ); +unsigned int as = !!(vcpu->arch.shared->msr &MSR_DS );kvmppc_e500_deliver_tlb_miss (vcpu, vcpu->arch.fault_dear, as); }
arch/powerpc/kvm/e500_tlb.h +1 -1
@@ -171,7 +171,7 @@ static inline int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,/* Does it match current guest AS? */ /* XXX what about IS != DS? */ -if (get_tlb_ts (tlbe) != !!(vcpu->arch.msr &MSR_IS )) +if (get_tlb_ts (tlbe) != !!(vcpu->arch.shared->msr &MSR_IS ))return 0 ; gpa =get_tlb_raddr (tlbe);
arch/powerpc/kvm/powerpc.c +2 -1
@@ -38,7 +38,8 @@int kvm_arch_vcpu_runnable (struct kvm_vcpu *v) { -return !(v->arch.msr &MSR_WE ) || !!(v->arch.pending_exceptions); +return !(v->arch.shared->msr &MSR_WE ) || + !!(v->arch.pending_exceptions); }
Signature · 1 line
1.6.0.2
PATCH 3/27 KVM: PPC: Convert DSISR to shared page
In reply to a message not in the archiveThe DSISR register contains information about a data page fault. It is fully read/write from inside the guest context and we don't need to worry about interacting based on writes of this register.
This patch converts all users of the current field to the shared page.
--- arch/powerpc/include/asm/kvm_book3s.h | 1 - arch/powerpc/include/asm/kvm_para.h | 1 + arch/powerpc/kvm/book3s.c | 11 ++++++----- arch/powerpc/kvm/book3s_emulate.c | 6 +++--- arch/powerpc/kvm/book3s_paired_singles.c | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +0 -1
@@ -85,7 +85,6 @@ struct kvmppc_vcpu_book3s {u64 hid[6 ];u64 gqr[8 ];int slb_nr; -u32 dsisr;u64 sdr1;u64 hior;u64 msr_mask;
arch/powerpc/include/asm/kvm_para.h +1 -0
@@ -24,6 +24,7 @@struct kvm_vcpu_arch_shared {__u64 msr; +__u32 dsisr; };#ifdef __KERNEL__
arch/powerpc/kvm/book3s.c +6 -5
@@ -595,15 +595,16 @@ int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,if (page_found == -ENOENT ) {/* Page not found in guest PTE entries */ vcpu->arch.dear =kvmppc_get_fault_dar (vcpu); -to_book3s (vcpu)->dsisr =to_svcpu (vcpu)->fault_dsisr; + vcpu->arch.shared->dsisr =to_svcpu (vcpu)->fault_dsisr; vcpu->arch.shared->msr |= (to_svcpu (vcpu)->shadow_srr1 &0x00000000f8000000ULL );kvmppc_book3s_queue_irqprio (vcpu, vec); }else if (page_found == -EPERM ) {/* Storage protection */ vcpu->arch.dear =kvmppc_get_fault_dar (vcpu); -to_book3s (vcpu)->dsisr =to_svcpu (vcpu)->fault_dsisr & ~DSISR_NOHPTE ; -to_book3s (vcpu)->dsisr |=DSISR_PROTFAULT ; + vcpu->arch.shared->dsisr = +to_svcpu (vcpu)->fault_dsisr & ~DSISR_NOHPTE ; + vcpu->arch.shared->dsisr |=DSISR_PROTFAULT ; vcpu->arch.shared->msr |= (to_svcpu (vcpu)->shadow_srr1 &0x00000000f8000000ULL );kvmppc_book3s_queue_irqprio (vcpu, vec);
@@ -867,7 +868,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, r =kvmppc_handle_pagefault (run, vcpu, dar, exit_nr); }else { vcpu->arch.dear = dar; -to_book3s (vcpu)->dsisr =to_svcpu (vcpu)->fault_dsisr; + vcpu->arch.shared->dsisr =to_svcpu (vcpu)->fault_dsisr;kvmppc_book3s_queue_irqprio (vcpu, exit_nr);kvmppc_mmu_pte_flush (vcpu, vcpu->arch.dear, ~0xFFFUL ); r =RESUME_GUEST ;
@@ -994,7 +995,7 @@ program_interrupt: }case BOOK3S_INTERRUPT_ALIGNMENT :if (kvmppc_read_inst (vcpu) ==EMULATE_DONE ) { -to_book3s (vcpu)->dsisr =kvmppc_alignment_dsisr (vcpu, + vcpu->arch.shared->dsisr =kvmppc_alignment_dsisr (vcpu,kvmppc_get_last_inst (vcpu)); vcpu->arch.dear =kvmppc_alignment_dar (vcpu,kvmppc_get_last_inst (vcpu));
arch/powerpc/kvm/book3s_emulate.c +3 -3
@@ -221,7 +221,7 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,else if (r == -EPERM ) dsisr |=DSISR_PROTFAULT ; -to_book3s (vcpu)->dsisr = dsisr; + vcpu->arch.shared->dsisr = dsisr;to_svcpu (vcpu)->fault_dsisr = dsisr; kvmppc_book3s_queue_irqprio(vcpu,
@@ -327,7 +327,7 @@ int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)to_book3s (vcpu)->sdr1 = spr_val;break ;case SPRN_DSISR : -to_book3s (vcpu)->dsisr = spr_val; + vcpu->arch.shared->dsisr = spr_val;break ;case SPRN_DAR : vcpu->arch.dear = spr_val;
@@ -440,7 +440,7 @@ int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->sdr1);break ;case SPRN_DSISR : -kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->dsisr); +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->dsisr);break ;case SPRN_DAR :kvmppc_set_gpr (vcpu, rt, vcpu->arch.dear);
arch/powerpc/kvm/book3s_paired_singles.c +1 -1
@@ -173,7 +173,7 @@ static void kvmppc_inject_pf(struct kvm_vcpu *vcpu, ulong eaddr, bool is_store)/* Page Fault */ dsisr =kvmppc_set_field (0 ,33 ,33 ,1 );if (is_store) -to_book3s (vcpu)->dsisr =kvmppc_set_field (dsisr,38 ,38 ,1 ); + shared->dsisr =kvmppc_set_field (dsisr,38 ,38 ,1 );kvmppc_book3s_queue_irqprio (vcpu,BOOK3S_INTERRUPT_DATA_STORAGE ); }
Signature · 1 line
1.6.0.2
PATCH 6/27 KVM: PPC: Convert SPRG[0-4] to shared page
In reply to a message not in the archiveWhen in kernel mode there are 4 additional registers available that are simple data storage. Instead of exiting to the hypervisor to read and write those, we can just share them with the guest using the page.
This patch converts all users of the current field to the shared page.
--- arch/powerpc/include/asm/kvm_host.h | 4 ---- arch/powerpc/include/asm/kvm_para.h | 4 ++++ arch/powerpc/kvm/book3s.c | 16 ++++++++-------- arch/powerpc/kvm/booke.c | 16 ++++++++-------- arch/powerpc/kvm/emulate.c | 24 ++++++++++++++++-------- 5 files changed, 36 insertions(+), 28 deletions(-)
arch/powerpc/include/asm/kvm_host.h +0 -4
@@ -217,10 +217,6 @@ struct kvm_vcpu_arch {ulong guest_owned_ext;#endif u32 mmucr; -ulong sprg0; -ulong sprg1; -ulong sprg2; -ulong sprg3;ulong sprg4;ulong sprg5;ulong sprg6;
arch/powerpc/include/asm/kvm_para.h +4 -0
@@ -23,6 +23,10 @@#include <linux/types.h> struct kvm_vcpu_arch_shared { +__u64 sprg0; +__u64 sprg1; +__u64 sprg2; +__u64 sprg3;__u64 srr0;__u64 srr1;__u64 dar;
arch/powerpc/kvm/book3s.c +8 -8
@@ -1062,10 +1062,10 @@ int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) regs->srr0 = vcpu->arch.shared->srr0; regs->srr1 = vcpu->arch.shared->srr1; regs->pid = vcpu->arch.pid; - regs->sprg0 = vcpu->arch.sprg0; - regs->sprg1 = vcpu->arch.sprg1; - regs->sprg2 = vcpu->arch.sprg2; - regs->sprg3 = vcpu->arch.sprg3; + regs->sprg0 = vcpu->arch.shared->sprg0; + regs->sprg1 = vcpu->arch.shared->sprg1; + regs->sprg2 = vcpu->arch.shared->sprg2; + regs->sprg3 = vcpu->arch.shared->sprg3; regs->sprg5 = vcpu->arch.sprg4; regs->sprg6 = vcpu->arch.sprg5; regs->sprg7 = vcpu->arch.sprg6;
@@ -1088,10 +1088,10 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)kvmppc_set_msr (vcpu, regs->msr); vcpu->arch.shared->srr0 = regs->srr0; vcpu->arch.shared->srr1 = regs->srr1; - vcpu->arch.sprg0 = regs->sprg0; - vcpu->arch.sprg1 = regs->sprg1; - vcpu->arch.sprg2 = regs->sprg2; - vcpu->arch.sprg3 = regs->sprg3; + vcpu->arch.shared->sprg0 = regs->sprg0; + vcpu->arch.shared->sprg1 = regs->sprg1; + vcpu->arch.shared->sprg2 = regs->sprg2; + vcpu->arch.shared->sprg3 = regs->sprg3; vcpu->arch.sprg5 = regs->sprg4; vcpu->arch.sprg6 = regs->sprg5; vcpu->arch.sprg7 = regs->sprg6;
arch/powerpc/kvm/booke.c +8 -8
@@ -495,10 +495,10 @@ int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) regs->srr0 = vcpu->arch.shared->srr0; regs->srr1 = vcpu->arch.shared->srr1; regs->pid = vcpu->arch.pid; - regs->sprg0 = vcpu->arch.sprg0; - regs->sprg1 = vcpu->arch.sprg1; - regs->sprg2 = vcpu->arch.sprg2; - regs->sprg3 = vcpu->arch.sprg3; + regs->sprg0 = vcpu->arch.shared->sprg0; + regs->sprg1 = vcpu->arch.shared->sprg1; + regs->sprg2 = vcpu->arch.shared->sprg2; + regs->sprg3 = vcpu->arch.shared->sprg3; regs->sprg5 = vcpu->arch.sprg4; regs->sprg6 = vcpu->arch.sprg5; regs->sprg7 = vcpu->arch.sprg6;
@@ -521,10 +521,10 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)kvmppc_set_msr (vcpu, regs->msr); vcpu->arch.shared->srr0 = regs->srr0; vcpu->arch.shared->srr1 = regs->srr1; - vcpu->arch.sprg0 = regs->sprg0; - vcpu->arch.sprg1 = regs->sprg1; - vcpu->arch.sprg2 = regs->sprg2; - vcpu->arch.sprg3 = regs->sprg3; + vcpu->arch.shared->sprg0 = regs->sprg0; + vcpu->arch.shared->sprg1 = regs->sprg1; + vcpu->arch.shared->sprg2 = regs->sprg2; + vcpu->arch.shared->sprg3 = regs->sprg3; vcpu->arch.sprg5 = regs->sprg4; vcpu->arch.sprg6 = regs->sprg5; vcpu->arch.sprg7 = regs->sprg6;
arch/powerpc/kvm/emulate.c +16 -8
@@ -263,13 +263,17 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)kvmppc_set_gpr (vcpu, rt,get_tb ());break ;case SPRN_SPRG0 : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.sprg0);break ; +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->sprg0); +break ;case SPRN_SPRG1 : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.sprg1);break ; +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->sprg1); +break ;case SPRN_SPRG2 : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.sprg2);break ; +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->sprg2); +break ;case SPRN_SPRG3 : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.sprg3);break ; +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->sprg3); +break ;/* Note: SPRG4-7 are user-readable, so we don't get * a trap. */
@@ -341,13 +345,17 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)break ;case SPRN_SPRG0 : - vcpu->arch.sprg0 =kvmppc_get_gpr (vcpu, rs);break ; + vcpu->arch.shared->sprg0 =kvmppc_get_gpr (vcpu, rs); +break ;case SPRN_SPRG1 : - vcpu->arch.sprg1 =kvmppc_get_gpr (vcpu, rs);break ; + vcpu->arch.shared->sprg1 =kvmppc_get_gpr (vcpu, rs); +break ;case SPRN_SPRG2 : - vcpu->arch.sprg2 =kvmppc_get_gpr (vcpu, rs);break ; + vcpu->arch.shared->sprg2 =kvmppc_get_gpr (vcpu, rs); +break ;case SPRN_SPRG3 : - vcpu->arch.sprg3 =kvmppc_get_gpr (vcpu, rs);break ; + vcpu->arch.shared->sprg3 =kvmppc_get_gpr (vcpu, rs); +break ;default : emulated =kvmppc_core_emulate_mtspr (vcpu, sprn, rs);
Signature · 1 line
1.6.0.2
PATCH 8/27 KVM: PPC: Add PV guest critical sections
In reply to a message not in the archiveWhen running in hooked code we need a way to disable interrupts without clobbering any interrupts or exiting out to the hypervisor.
To achieve this, we have an additional critical field in the shared page. If that field is equal to the r1 register of the guest, it tells the hypervisor that we're in such a critical section and thus may not receive any interrupts.
---
v1 -> v2:
- make crit detection only trigger in supervisor mode
--- arch/powerpc/include/asm/kvm_para.h | 1 + arch/powerpc/kvm/book3s.c | 18 ++++++++++++++++-- arch/powerpc/kvm/booke.c | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/kvm_para.h +1 -0
@@ -24,6 +24,7 @@#include <linux/of.h> struct kvm_vcpu_arch_shared { +__u64 critical;/* Guest may not get interrupts if == r1 */ __u64 sprg0;__u64 sprg1;__u64 sprg2;
arch/powerpc/kvm/book3s.c +16 -2
@@ -251,14 +251,28 @@ int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)int deliver =1 ;int vec =0 ;ulong flags =0ULL ; +ulong crit_raw = vcpu->arch.shared->critical; +ulong crit_r1 =kvmppc_get_gpr (vcpu,1 ); +bool crit; + +/* Truncate crit indicators in 32 bit mode */ +if (!(vcpu->arch.shared->msr &MSR_SF )) { + crit_raw &=0xffffffff ; + crit_r1 &=0xffffffff ; + } + +/* Critical section when crit == r1 */ + crit = (crit_raw == crit_r1); +/* ... and we're in supervisor mode */ + crit = crit && !(vcpu->arch.shared->msr &MSR_PR );switch (priority) {case BOOK3S_IRQPRIO_DECREMENTER : - deliver = vcpu->arch.shared->msr &MSR_EE ; + deliver = (vcpu->arch.shared->msr &MSR_EE ) && !crit; vec =BOOK3S_INTERRUPT_DECREMENTER ;break ;case BOOK3S_IRQPRIO_EXTERNAL : - deliver = vcpu->arch.shared->msr &MSR_EE ; + deliver = (vcpu->arch.shared->msr &MSR_EE ) && !crit; vec =BOOK3S_INTERRUPT_EXTERNAL ;break ;case BOOK3S_IRQPRIO_SYSTEM_RESET :
arch/powerpc/kvm/booke.c +15 -0
@@ -147,6 +147,20 @@ static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,int allowed =0 ;ulong uninitialized_var (msr_mask );bool update_esr = false, update_dear = false; +ulong crit_raw = vcpu->arch.shared->critical; +ulong crit_r1 =kvmppc_get_gpr (vcpu,1 ); +bool crit; + +/* Truncate crit indicators in 32 bit mode */ +if (!(vcpu->arch.shared->msr &MSR_SF )) { + crit_raw &=0xffffffff ; + crit_r1 &=0xffffffff ; + } + +/* Critical section when crit == r1 */ + crit = (crit_raw == crit_r1); +/* ... and we're in supervisor mode */ + crit = crit && !(vcpu->arch.shared->msr &MSR_PR );switch (priority) {case BOOKE_IRQPRIO_DTLB_MISS :
@@ -181,6 +195,7 @@ static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,case BOOKE_IRQPRIO_DECREMENTER :case BOOKE_IRQPRIO_FIT : allowed = vcpu->arch.shared->msr &MSR_EE ; + allowed = allowed && !crit; msr_mask =MSR_CE |MSR_ME |MSR_DE ;break ;case BOOKE_IRQPRIO_DEBUG :
Signature · 1 line
1.6.0.2
PATCH 10/27 KVM: PPC: Tell guest about pending interrupts
In reply to a message not in the archiveWhen the guest turns on interrupts again, it needs to know if we have an interrupt pending for it. Because if so, it should rather get out of guest context and get the interrupt.
So we introduce a new field in the shared page that we use to tell the guest that there's a pending interrupt lying around.
--- arch/powerpc/include/asm/kvm_para.h | 1 + arch/powerpc/kvm/book3s.c | 7 +++++++ arch/powerpc/kvm/booke.c | 7 +++++++ 3 files changed, 15 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_para.h +1 -0
@@ -37,6 +37,7 @@ struct kvm_vcpu_arch_shared {__u64 dar;__u64 msr;__u32 dsisr; +__u32 int_pending;/* Tells the guest if we have an interrupt */ };#define KVM_SC_MAGIC_R0 0x4b564d21/* "KVM!" */
arch/powerpc/kvm/book3s.c +7 -0
@@ -337,6 +337,7 @@ int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)void kvmppc_core_deliver_interrupts (struct kvm_vcpu *vcpu) {unsigned long *pending = &vcpu->arch.pending_exceptions; +unsigned long old_pending = vcpu->arch.pending_exceptions;unsigned int priority;#ifdef EXIT_DEBUG
@@ -356,6 +357,12 @@ void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)BITS_PER_BYTE *sizeof (*pending), priority + 1); } + +/* Tell the guest about our interrupt status */ +if (*pending) + vcpu->arch.shared->int_pending =1 ; +else if (old_pending) + vcpu->arch.shared->int_pending =0 ; }void kvmppc_set_pvr (struct kvm_vcpu *vcpu,u32 pvr)
arch/powerpc/kvm/booke.c +7 -0
@@ -224,6 +224,7 @@ static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,void kvmppc_core_deliver_interrupts (struct kvm_vcpu *vcpu) {unsigned long *pending = &vcpu->arch.pending_exceptions; +unsigned long old_pending = vcpu->arch.pending_exceptions;unsigned int priority; priority =__ffs (*pending);
@@ -235,6 +236,12 @@ void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)BITS_PER_BYTE *sizeof (*pending), priority + 1); } + +/* Tell the guest about our interrupt status */ +if (*pending) + vcpu->arch.shared->int_pending =1 ; +else if (old_pending) + vcpu->arch.shared->int_pending =0 ; }/**
Signature · 1 line
1.6.0.2
PATCH 14/27 KVM: PPC: Expose magic page support to guest
In reply to a message not in the archiveNow that we have the shared page in place and the MMU code knows about the magic page, we can expose that capability to the guest!
---
v2 -> v3:
- align hypercalls to in/out of ePAPR
--- arch/powerpc/include/asm/kvm_para.h | 2 ++ arch/powerpc/kvm/powerpc.c | 11 +++++++++++ 2 files changed, 13 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_para.h +2 -0
@@ -45,6 +45,8 @@ struct kvm_vcpu_arch_shared {#define HC_EV_SUCCESS 0#define HC_EV_UNIMPLEMENTED 12 +#define KVM_FEATURE_MAGIC_PAGE 1 +#ifdef __KERNEL__#ifdef CONFIG_KVM_GUEST
arch/powerpc/kvm/powerpc.c +11 -0
@@ -61,8 +61,19 @@ int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) }switch (nr) { +case HC_VENDOR_KVM |KVM_HC_PPC_MAP_MAGIC_PAGE : + { + vcpu->arch.magic_page_pa = param1; + vcpu->arch.magic_page_ea = param2; + + r =HC_EV_SUCCESS ; +break ; + }case HC_VENDOR_KVM |KVM_HC_FEATURES : r =HC_EV_SUCCESS ; +#if defined(CONFIG_PPC_BOOK3S )/* XXX Missing magic page on BookE */ + r2 |= (1 <<KVM_FEATURE_MAGIC_PAGE ); +#endif /* Second return value is in r4 */ kvmppc_set_gpr (vcpu,4 , r2);
Signature · 1 line
1.6.0.2
PATCH 19/27 KVM: PPC: PV tlbsync to nop
In reply to a message not in the archiveWith our current MMU scheme we don't need to know about the tlbsync instruction. So we can just nop it out.
---
v1 -> v2:
- use kvm_patch_ins
--- arch/powerpc/kernel/kvm.c | 12 ++++++++++++ 1 files changed, 12 insertions(+), 0 deletions(-)
arch/powerpc/kernel/kvm.c +12 -0
@@ -62,6 +62,8 @@#define KVM_INST_MTSPR_DAR 0x7c1303a6#define KVM_INST_MTSPR_DSISR 0x7c1203a6 +#define KVM_INST_TLBSYNC 0x7c00046c +static bool kvm_patching_worked = true;static inline void kvm_patch_ins (u32 *inst,u32 new_inst)
@@ -98,6 +100,11 @@ static void kvm_patch_ins_stw(u32 *inst, long addr, u32 rt)kvm_patch_ins (inst,KVM_INST_STW | rt | (addr &0x0000fffc )); } +static void kvm_patch_ins_nop (u32 *inst) +{ +kvm_patch_ins (inst,KVM_INST_NOP ); +} +static void kvm_map_magic_page (void *data) { kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
@@ -166,6 +173,11 @@ static void kvm_check_ins(u32 *inst)case KVM_INST_MTSPR_DSISR :kvm_patch_ins_stw (inst,magic_var (dsisr), inst_rt);break ; + +/* Nops */ +case KVM_INST_TLBSYNC : +kvm_patch_ins_nop (inst); +break ; }switch (_inst) {
Signature · 1 line
1.6.0.2
PATCH 20/27 KVM: PPC: Introduce kvm_tmp framework
In reply to a message not in the archiveWe will soon require more sophisticated methods to replace single instructions with multiple instructions. We do that by branching to a memory region where we write replacement code for the instruction to.
This region needs to be within 32 MB of the patched instruction though, because that's the furthest we can jump with immediate branches.
So we keep 1MB of free space around in bss. After we're done initing we can just tell the mm system that the unused pages are free, but until then we have enough space to fit all our code in.
--- arch/powerpc/kernel/kvm.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 40 insertions(+), 2 deletions(-)
arch/powerpc/kernel/kvm.c +40 -2
@@ -65,6 +65,8 @@#define KVM_INST_TLBSYNC 0x7c00046cstatic bool kvm_patching_worked = true; +static char kvm_tmp[1024 *1024 ]; +static int kvm_tmp_index;static inline void kvm_patch_ins (u32 *inst,u32 new_inst) {
@@ -105,6 +107,23 @@ static void kvm_patch_ins_nop(u32 *inst)kvm_patch_ins (inst,KVM_INST_NOP ); } +static u32 *kvm_alloc (int len) +{ +u32 *p; + +if ((kvm_tmp_index + len) >ARRAY_SIZE (kvm_tmp)) { +printk (KERN_ERR "KVM: No more space (%d + %d)\n" , + kvm_tmp_index, len); + kvm_patching_worked = false; +return NULL ; + } + + p = (void *)&kvm_tmp[kvm_tmp_index]; + kvm_tmp_index += len; + +return p; +} +static void kvm_map_magic_page (void *data) { kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
@@ -270,17 +289,36 @@ static int kvm_para_setup(void)return 0 ; } +static __init void kvm_free_tmp (void ) +{ +unsigned long start, end; + + start = (ulong)&kvm_tmp[kvm_tmp_index + (PAGE_SIZE -1 )] &PAGE_MASK ; + end = (ulong)&kvm_tmp[ARRAY_SIZE (kvm_tmp)] &PAGE_MASK ; + +/* Free the tmp space we don't need */ +for (; start < end; start +=PAGE_SIZE ) { +ClearPageReserved (virt_to_page (start)); +init_page_count (virt_to_page (start)); +free_page (start); + totalram_pages++; + } +} +static int __init kvm_guest_init (void ) {if (!kvm_para_available ()) -return 0 ; + goto free_tmp;if (kvm_para_setup ()) -return 0 ; + goto free_tmp;if (kvm_para_has_feature (KVM_FEATURE_MAGIC_PAGE ))kvm_use_magic_page (); +free_tmp: +kvm_free_tmp (); +return 0 ; }
Signature · 1 line
1.6.0.2
PATCH 21/27 KVM: PPC: Introduce branch patching helper
In reply to a message not in the archiveWe will need to patch several instruction streams over to a different code path, so we need a way to patch a single instruction with a branch somewhere else.
This patch adds a helper to facilitate this patching.
---
v2 -> v3:
- add safety check for relocatable kernels
--- arch/powerpc/kernel/kvm.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)
arch/powerpc/kernel/kvm.c +14 -0
@@ -107,6 +107,20 @@ static void kvm_patch_ins_nop(u32 *inst)kvm_patch_ins (inst,KVM_INST_NOP ); } +static void kvm_patch_ins_b (u32 *inst,int addr) +{ +#ifdef CONFIG_RELOCATABLE +/* On relocatable kernels interrupts handlers and our code +can be in different regions, so we don't patch them */ + +extern u32 __end_interrupts; +if ((ulong )inst < (ulong)&__end_interrupts) +return ; +#endif + +kvm_patch_ins (inst,KVM_INST_B | (addr &KVM_INST_B_MASK )); +} +static u32 *kvm_alloc (int len) {u32 *p;
Signature · 1 line
1.6.0.2
PATCH 23/27 KVM: PPC: PV mtmsrd L=1
In reply to a message not in the archiveThe PowerPC ISA has a special instruction for mtmsr that only changes the EE and RI bits, namely the L=1 form.
Since that one is reasonably often occuring and simple to implement, let's go with this first. Writing EE=0 is always just a store. Doing EE=1 also requires us to check for pending interrupts and if necessary exit back to the hypervisor.
---
v1 -> v2:
- use kvm_patch_ins_b
--- arch/powerpc/kernel/kvm.c | 45 ++++++++++++++++++++++++++++++++ arch/powerpc/kernel/kvm_emul.S | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 0 deletions(-)
arch/powerpc/kernel/kvm.c +45 -0
@@ -63,6 +63,7 @@#define KVM_INST_MTSPR_DSISR 0x7c1203a6#define KVM_INST_TLBSYNC 0x7c00046c +#define KVM_INST_MTMSRD_L1 0x7c010164static bool kvm_patching_worked = true;static char kvm_tmp[1024 *1024 ];
@@ -138,6 +139,43 @@ static u32 *kvm_alloc(int len)return p; } +extern u32 kvm_emulate_mtmsrd_branch_offs; +extern u32 kvm_emulate_mtmsrd_reg_offs; +extern u32 kvm_emulate_mtmsrd_len; +extern u32 kvm_emulate_mtmsrd[]; + +static void kvm_patch_ins_mtmsrd (u32 *inst,u32 rt) +{ +u32 *p; +int distance_start; +int distance_end; +ulong next_inst; + + p =kvm_alloc (kvm_emulate_mtmsrd_len *4 ); +if (!p) +return ; + +/* Find out where we are and put everything there */ + distance_start = (ulong )p - (ulong )inst; + next_inst = ((ulong )inst +4 ); + distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsrd_branch_offs]; + +/* Make sure we only write valid b instructions */ +if (distance_start >KVM_INST_B_MAX ) { + kvm_patching_worked = false; +return ; + } + +/* Modify the chunk to fit the invocation */ +memcpy (p, kvm_emulate_mtmsrd, kvm_emulate_mtmsrd_len *4 ); + p[kvm_emulate_mtmsrd_branch_offs] |= distance_end &KVM_INST_B_MASK ; + p[kvm_emulate_mtmsrd_reg_offs] |= rt; +flush_icache_range ((ulong )p, (ulong )p + kvm_emulate_mtmsrd_len *4 ); + +/* Patch the invocation */ +kvm_patch_ins_b (inst, distance_start); +} +static void kvm_map_magic_page (void *data) { kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
@@ -211,6 +249,13 @@ static void kvm_check_ins(u32 *inst)case KVM_INST_TLBSYNC :kvm_patch_ins_nop (inst);break ; + +/* Rewrites */ +case KVM_INST_MTMSRD_L1 : +/* We use r30 and r31 during the hook */ +if (get_rt (inst_rt) <30 ) +kvm_patch_ins_mtmsrd (inst, inst_rt); +break ; }switch (_inst) {
arch/powerpc/kernel/kvm_emul.S +56 -0
59 lines not on this page · raw message
Signature · 1 line
1.6.0.2
PATCH 25/27 KVM: PPC: PV wrteei
In reply to a message not in the archiveOn BookE the preferred way to write the EE bit is the wrteei instruction. It already encodes the EE bit in the instruction.
So in order to get BookE some speedups as well, let's also PV'nize thati instruction.
---
v1 -> v2:
- use kvm_patch_ins_b
--- arch/powerpc/kernel/kvm.c | 50 ++++++++++++++++++++++++++++++++++++++++ arch/powerpc/kernel/kvm_emul.S | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 0 deletions(-)
arch/powerpc/kernel/kvm.c +50 -0
@@ -67,6 +67,9 @@#define KVM_INST_MTMSRD_L1 0x7c010164#define KVM_INST_MTMSR 0x7c000124 +#define KVM_INST_WRTEEI_0 0x7c000146 +#define KVM_INST_WRTEEI_1 0x7c008146 +static bool kvm_patching_worked = true;static char kvm_tmp[1024 *1024 ];static int kvm_tmp_index;
@@ -221,6 +224,47 @@ static void kvm_patch_ins_mtmsr(u32 *inst, u32 rt)kvm_patch_ins_b (inst, distance_start); } +#ifdef CONFIG_BOOKE + +extern u32 kvm_emulate_wrteei_branch_offs; +extern u32 kvm_emulate_wrteei_ee_offs; +extern u32 kvm_emulate_wrteei_len; +extern u32 kvm_emulate_wrteei[]; + +static void kvm_patch_ins_wrteei (u32 *inst) +{ +u32 *p; +int distance_start; +int distance_end; +ulong next_inst; + + p =kvm_alloc (kvm_emulate_wrteei_len *4 ); +if (!p) +return ; + +/* Find out where we are and put everything there */ + distance_start = (ulong )p - (ulong )inst; + next_inst = ((ulong )inst +4 ); + distance_end = next_inst - (ulong)&p[kvm_emulate_wrteei_branch_offs]; + +/* Make sure we only write valid b instructions */ +if (distance_start >KVM_INST_B_MAX ) { + kvm_patching_worked = false; +return ; + } + +/* Modify the chunk to fit the invocation */ +memcpy (p, kvm_emulate_wrteei, kvm_emulate_wrteei_len *4 ); + p[kvm_emulate_wrteei_branch_offs] |= distance_end &KVM_INST_B_MASK ; + p[kvm_emulate_wrteei_ee_offs] |= (*inst &MSR_EE ); +flush_icache_range ((ulong )p, (ulong )p + kvm_emulate_wrteei_len *4 ); + +/* Patch the invocation */ +kvm_patch_ins_b (inst, distance_start); +} + +#endif +static void kvm_map_magic_page (void *data) { kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
@@ -310,6 +354,12 @@ static void kvm_check_ins(u32 *inst) }switch (_inst) { +#ifdef CONFIG_BOOKE +case KVM_INST_WRTEEI_0 : +case KVM_INST_WRTEEI_1 : +kvm_patch_ins_wrteei (inst); +break ; +#endif } }
arch/powerpc/kernel/kvm_emul.S +41 -0
44 lines not on this page · raw message
Signature · 1 line
1.6.0.2
PATCH 26/27 KVM: PPC: Add Documentation about PV interface
In reply to a message not in the archiveWe just introduced a new PV interface that screams for documentation. So here it is - a shiny new and awesome text file describing the internal works of the PPC KVM paravirtual interface.
---
v1 -> v2:
- clarify guest implementation
- clarify that privileged instructions still work
- explain safe MSR bits
- Fix dsisr patch description
- change hypervisor calls to use new register values
v2 -> v3:
- update documentation to new hypercall interface
- change detection to be device tree based
--- Documentation/kvm/ppc-pv.txt | 180 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 180 insertions(+), 0 deletions(-) create mode 100644 Documentation/kvm/ppc-pv.txt
Documentation/kvm/ppc-pv.txt new file +180 -0
@@ -0,0 +1,180 @@ +The PPC KVM paravirtual interface +================================= + +The basic execution principle by which KVM on PowerPC works is to run all kernel +space code in PR=1 which is user space. This way we trap all privileged +instructions and can emulate them accordingly. + +Unfortunately that is also the downfall. There are quite some privileged +instructions that needlessly return us to the hypervisor even though they +could be handled differently. + +This is what the PPC PV interface helps with. It takes privileged instructions +and transforms them into unprivileged ones with some help from the hypervisor. +This cuts down virtualization costs by about 50% on some of my benchmarks. + +The code for that interface can be found in arch/powerpc/kernel/kvm* + +Querying for existence +====================== + +To find out if we're running on KVM or not, we leverage the device tree. When +Linux is running on KVM, a node /hypervisor exists. That node contains a +compatible property with the value "linux,kvm". + +Once you determined you're running under a PV capable KVM, you can now use +hypercalls as described below. + +KVM hypercalls +============== + +Inside the device tree's /hypervisor node there's a property called +'hypercall-instructions'. This property contains at most 4 opcodes that make +up the hypercall. To call a hypercall, just call these instructions. + +The parameters are as follows: + + Register IN OUT + + r0 - volatile + r3 1st parameter Return code + r4 2nd parameter 1st output value + r5 3rd parameter 2nd output value + r6 4th parameter 3rd output value + r7 5th parameter 4th output value + r8 6th parameter 5th output value + r9 7th parameter 6th output value + r10 8th parameter 7th output value + r11 hypercall number 8th output value + r12 - volatile + +Hypercall definitions are shared in generic code, so the same hypercall numbers +apply for x86 and powerpc alike with the exception that each KVM hypercall +also needs to be ORed with the KVM vendor code which is (42 << 16). + +Return codes can be as follows: + + Code Meaning + + 0 Success + 12 Hypercall not implemented + <0 Error + +The magic page +============== + +To enable communication between the hypervisor and guest there is a new shared +page that contains parts of supervisor visible register state. The guest can +map this shared page using the KVM hypercall KVM_HC_PPC_MAP_MAGIC_PAGE. + +With this hypercall issued the guest always gets the magic page mapped at the +desired location in effective and physical address space. For now, we always +map the page to -4096. This way we can access it using absolute load and store +functions. The following instruction reads the first field of the magic page: + + ld rX, -4096(0) + +The interface is designed to be extensible should there be need later to add +additional registers to the magic page. If you add fields to the magic page, +also define a new hypercall feature to indicate that the host can give you more +registers. Only if the host supports the additional features, make use of them. + +The magic page has the following layout as described in +arch/powerpc/include/asm/kvm_para.h: + +struct kvm_vcpu_arch_shared { + __u64 scratch1; + __u64 scratch2; + __u64 scratch3; + __u64 critical; /* Guest may not get interrupts if == r1 */ + __u64 sprg0; + __u64 sprg1; + __u64 sprg2; + __u64 sprg3; + __u64 srr0; + __u64 srr1; + __u64 dar; + __u64 msr; + __u32 dsisr; + __u32 int_pending; /* Tells the guest if we have an interrupt */ +}; + +Additions to the page must only occur at the end. Struct fields are always 32 +or 64 bit aligned, depending on them being 32 or 64 bit wide respectively. + +MSR bits +======== + +The MSR contains bits that require hypervisor intervention and bits that do +not require direct hypervisor intervention because they only get interpreted +when entering the guest or don't have any impact on the hypervisor's behavior. + +The following bits are safe to be set inside the guest: + + MSR_EE + MSR_RI + MSR_CR + MSR_ME + +If any other bit changes in the MSR, please still use mtmsr(d). + +Patched instructions +==================== + +The "ld" and "std" instructions are transormed to "lwz" and "stw" instructions +respectively on 32 bit systems with an added offset of 4 to accomodate for big +endianness. + +The following is a list of mapping the Linux kernel performs when running as +guest. Implementing any of those mappings is optional, as the instruction traps +also act on the shared page. So calling privileged instructions still works as +before. + +From To +==== == + +mfmsr rX ld rX, magic_page->msr +mfsprg rX, 0 ld rX, magic_page->sprg0 +mfsprg rX, 1 ld rX, magic_page->sprg1 +mfsprg rX, 2 ld rX, magic_page->sprg2 +mfsprg rX, 3 ld rX, magic_page->sprg3 +mfsrr0 rX ld rX, magic_page->srr0 +mfsrr1 rX ld rX, magic_page->srr1 +mfdar rX ld rX, magic_page->dar +mfdsisr rX lwz rX, magic_page->dsisr + +mtmsr rX std rX, magic_page->msr +mtsprg 0, rX std rX, magic_page->sprg0 +mtsprg 1, rX std rX, magic_page->sprg1 +mtsprg 2, rX std rX, magic_page->sprg2 +mtsprg 3, rX std rX, magic_page->sprg3 +mtsrr0 rX std rX, magic_page->srr0 +mtsrr1 rX std rX, magic_page->srr1 +mtdar rX std rX, magic_page->dar +mtdsisr rX stw rX, magic_page->dsisr + +tlbsync nop + +mtmsrd rX, 0 b <special mtmsr section> +mtmsr rX b <special mtmsr section> + +mtmsrd rX, 1 b <special mtmsrd section> + +[BookE only] +wrteei [0|1] b <special wrteei section> + + +Some instructions require more logic to determine what's going on than a load +or store instruction can deliver. To enable patching of those, we keep some +RAM around where we can live translate instructions to. What happens is the +following: + + 1) copy emulation code to memory + 2) patch that code to fit the emulated instruction + 3) patch that code to return to the original pc + 4 + 4) patch the original instruction to branch to the new code + +That way we can inject an arbitrary amount of code as replacement for a single +instruction. This allows us to check for pending interrupts when setting EE=1 +for example. +
Signature · 1 line
1.6.0.2
Quoted 58 lines · Alexander Graf
On PPC we run PR=0 (kernel mode) code in PR=1 (user mode) and don't use the hypervisor extensions.
While that is all great to show that virtualization is possible, there are quite some cases where the emulation overhead of privileged instructions is killing performance.
This patchset tackles exactly that issue. It introduces a paravirtual framework using which KVM and Linux share a page to exchange register state with. That way we don't have to switch to the hypervisor just to change a value of a privileged register.
To prove my point, I ran the same test I did for the MMU optimizations against the PV framework. Here are the results:
[without]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello> /dev/null; donereal 0m14.659s user 0m8.967s sys 0m5.688s[with]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello> /dev/null; donereal 0m7.557s user 0m4.121s sys 0m3.426sSo this is a significant performance improvement! I'm quite happy how fast this whole thing becomes :)
I tried to take all comments I've heard from people so far about such a PV framework into account. In case you told me something before that is a no-go and I still did it, please just tell me again.
To make use of this whole thing you also need patches to qemu and openbios. I have them in my queue, but want to see this set upstream first before I start sending patches to the other projects.
Now go and have fun with fast VMs on PPC! Get yourself a G5 on ebay and start experiencing the power yourself. - heh
v1 -> v2:
- change hypervisor calls to use r0 and r3
- make crit detection only trigger in supervisor mode
- RMO -> PAM
- introducekvm_patch_ins
- only flush icache when patching
- introducekvm_patch_ins_b
- update documentationv2 -> v3:
Quoted 8 lines
- use pPAPR conventions for hypercall interface
- only use r0 as magic sc number
- remove PVR detection
- remove BookE shared page mapping support
- combine book3s-64 and -32 magic page ra override
- add self-test check if the mapping works to guest code
- add safety check for relocatable kernels
Looks reasonable. Since it's fair to say I understand nothing about powerpc, I'd like someone who does to review it and ack, please, with an emphasis on the interfaces.
Signature · 1 line
error compiling committee.c: too many arguments to function
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 68 lines · Avi Kivity
Quoted 58 lines · Alexander Graf
On PPC we run PR=0 (kernel mode) code in PR=1 (user mode) and don't use the hypervisor extensions.
While that is all great to show that virtualization is possible, there are quite some cases where the emulation overhead of privileged instructions is killing performance.
This patchset tackles exactly that issue. It introduces a paravirtual framework using which KVM and Linux share a page to exchange register state with. That way we don't have to switch to the hypervisor just to change a value of a privileged register.
To prove my point, I ran the same test I did for the MMU optimizations against the PV framework. Here are the results:
[without]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello> /dev/null; donereal 0m14.659s user 0m8.967s sys 0m5.688s[with]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello> /dev/null; donereal 0m7.557s user 0m4.121s sys 0m3.426sSo this is a significant performance improvement! I'm quite happy how fast this whole thing becomes :)
I tried to take all comments I've heard from people so far about such a PV framework into account. In case you told me something before that is a no-go and I still did it, please just tell me again.
To make use of this whole thing you also need patches to qemu and openbios. I have them in my queue, but want to see this set upstream first before I start sending patches to the other projects.
Now go and have fun with fast VMs on PPC! Get yourself a G5 on ebay and start experiencing the power yourself. - heh
v1 -> v2:
- change hypervisor calls to use r0 and r3
- make crit detection only trigger in supervisor mode
- RMO -> PAM
- introducekvm_patch_ins
- only flush icache when patching
- introducekvm_patch_ins_b
- update documentationv2 -> v3:
Quoted 8 lines
- use pPAPR conventions for hypercall interface
- only use r0 as magic sc number
- remove PVR detection
- remove BookE shared page mapping support
- combine book3s-64 and -32 magic page ra override
- add self-test check if the mapping works to guest code
- add safety check for relocatable kernels
Quoted 1 line
Looks reasonable. Since it's fair to say I understand nothing about powerpc, I'd like someone who does to review it and ack, please, with an emphasis on the interfaces.
Sounds good. Preferably someone with access to the ePAPR spec :).
Alex
Quoted 6 lines · On Sun, 1 Aug 2010 22:21:37 +0200 Alexander Graf <[email protected]> wrote:
Quoted 1 line · Avi Kivity
Looks reasonable. Since it's fair to say I understand nothing about powerpc, I'd like someone who does to review it and ack, please, with an emphasis on the interfaces.
Sounds good. Preferably someone with access to the ePAPR spec :).
The ePAPR-relevant stuff in patches 7, 16, and 17 looks reasonable. Did I miss any ePAPR-relevant stuff in the other patches?
-Scott
A message that is not in the archive (sent off-list or not yet received): 20100803111611.38ca87e6-1MYqz8GpK7RekFaExTCHk1jVikpgYyvb5NbjCUgZEJk@public.gmane.org
Quoted 9 lines · Scott Wood
Quoted 4 lines · On Sun, 1 Aug 2010 22:21:37 +0200 Alexander…
Quoted 1 line · Avi Kivity
Looks reasonable. Since it's fair to say I understand nothing about powerpc, I'd like someone who does to review it and ack, please, with an emphasis on the interfaces.
Sounds good. Preferably someone with access to the ePAPR spec :).
The ePAPR-relevant stuff in patches 7, 16, and 17 looks reasonable. Did I miss any ePAPR-relevant stuff in the other patches?
Shall I take this as an ACK?
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 4 lines · Avi Kivity
Quoted 3 lines · Scott Wood
On Sun, 1 Aug 2010 22:21:37 +0200
Alexander Graf<[email protected]> wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines · Avi Kivity
Quoted 2 lines
Looks reasonable. Since it's fair to say I understand nothing about powerpc, I'd like someone who does to review it and ack, please, with an emphasis on the interfaces.
Sounds good. Preferably someone with access to the ePAPR spec :).
The ePAPR-relevant stuff in patches 7, 16, and 17 looks reasonable. Did I miss any ePAPR-relevant stuff in the other patches?
Shall I take this as an ACK?
Hollis wanted to take a look at it too. But given the fact that I have another ~10 patches lying here I'd appreciate if things could get committed. If changes are so dramatic that they'd render things incompatible, we can always just release both patches for an actual kernel release, right?
Alex
Quoted 3 lines · Alexander Graf
Quoted 2 lines
Shall I take this as an ACK?
Hollis wanted to take a look at it too. But given the fact that I have another ~10 patches lying here I'd appreciate if things could get committed. If changes are so dramatic that they'd render things incompatible, we can always just release both patches for an actual kernel release, right?
That's true, we have some time to get it right.
Hollis, please let us know either way once you've reviewed things.
Signature · 1 line
error compiling committee.c: too many arguments to function
Avi Kivity
AppliedQuoted 39 lines · Alexander Graf
On PPC we run PR=0 (kernel mode) code in PR=1 (user mode) and don't use the hypervisor extensions.
While that is all great to show that virtualization is possible, there are quite some cases where the emulation overhead of privileged instructions is killing performance.
This patchset tackles exactly that issue. It introduces a paravirtual framework using which KVM and Linux share a page to exchange register state with. That way we don't have to switch to the hypervisor just to change a value of a privileged register.
To prove my point, I ran the same test I did for the MMU optimizations against the PV framework. Here are the results:
[without]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello> /dev/null; donereal 0m14.659s user 0m8.967s sys 0m5.688s[with]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello> /dev/null; donereal 0m7.557s user 0m4.121s sys 0m3.426sSo this is a significant performance improvement! I'm quite happy how fast this whole thing becomes :)
I tried to take all comments I've heard from people so far about such a PV framework into account. In case you told me something before that is a no-go and I still did it, please just tell me again.
Quoted 7 lines
To make use of this whole thing you also need patches to qemu and openbios. I have them in my queue, but want to see this set upstream first before I start sending patches to the other projects.
Now go and have fun with fast VMs on PPC! Get yourself a G5 on ebay and start experiencing the power yourself. - heh
Applied this and your follow on 7-part series, thanks.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 8 lines · Alexander Graf
[without]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello > /dev/null; donereal 0m14.659s user 0m8.967s sys 0m5.688s
Quoted 7 lines
[with]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello > /dev/null; donereal 0m7.557s user 0m4.121s sys 0m3.426s
Do you have #'s for w/o HV to compare to.
- k--
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
A message that is not in the archive (sent off-list or not yet received): 17DF4824-9EC7-44F4-9408-EA6241C33E70-XVmvHMARGAS8U2dJNN8I7kB+6BGkLq7r@public.gmane.org
Quoted 13 lines · Kumar Gala
Quoted 10 lines · Alexander Graf
[without]
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello > /dev/null; donereal 0m14.659s user 0m8.967s sys 0m5.688s[with]
Quoted 7 lines
Quoted 5 lines
debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello > /dev/null; donereal 0m7.557s user 0m4.121s sys 0m3.426sDo you have #'s for w/o HV to compare to.
Native was around 2-3 seconds overall. So we still have quite a big impact on performance, but things are improving :).
Alex
PATCH 4/27 KVM: PPC: Convert DAR to shared page.
In reply to Alexander GrafThe DAR register contains the address a data page fault occured at. This register behaves pretty much like a simple data storage register that gets written to on data faults. There is no hypervisor interaction required on read or write.
This patch converts all users of the current field to the shared page.
--- arch/powerpc/include/asm/kvm_host.h | 1 - arch/powerpc/include/asm/kvm_para.h | 1 + arch/powerpc/kvm/book3s.c | 14 +++++++------- arch/powerpc/kvm/book3s_emulate.c | 6 +++--- arch/powerpc/kvm/book3s_paired_singles.c | 2 +- arch/powerpc/kvm/booke.c | 2 +- arch/powerpc/kvm/booke_emulate.c | 4 ++-- 7 files changed, 15 insertions(+), 15 deletions(-)
arch/powerpc/include/asm/kvm_host.h +0 -1
@@ -231,7 +231,6 @@ struct kvm_vcpu_arch {ulong csrr1;ulong dsrr0;ulong dsrr1; -ulong dear;ulong esr;u32 dec;u32 decar;
arch/powerpc/include/asm/kvm_para.h +1 -0
@@ -23,6 +23,7 @@#include <linux/types.h> struct kvm_vcpu_arch_shared { +__u64 dar;__u64 msr;__u32 dsisr; };
arch/powerpc/kvm/book3s.c +7 -7
@@ -594,14 +594,14 @@ int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,if (page_found = -ENOENT ) {/* Page not found in guest PTE entries */ - vcpu->arch.dear =kvmppc_get_fault_dar (vcpu); + vcpu->arch.shared->dar =kvmppc_get_fault_dar (vcpu); vcpu->arch.shared->dsisr =to_svcpu (vcpu)->fault_dsisr; vcpu->arch.shared->msr | (to_svcpu (vcpu)->shadow_srr1 &0x00000000f8000000ULL );kvmppc_book3s_queue_irqprio (vcpu, vec); }else if (page_found = -EPERM ) {/* Storage protection */ - vcpu->arch.dear =kvmppc_get_fault_dar (vcpu); + vcpu->arch.shared->dar =kvmppc_get_fault_dar (vcpu); vcpu->arch.shared->dsisr to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE; vcpu->arch.shared->dsisr |=DSISR_PROTFAULT ;
@@ -610,7 +610,7 @@ int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,kvmppc_book3s_queue_irqprio (vcpu, vec); }else if (page_found = -EINVAL ) {/* Page not found in guest SLB */ - vcpu->arch.dear =kvmppc_get_fault_dar (vcpu); + vcpu->arch.shared->dar =kvmppc_get_fault_dar (vcpu);kvmppc_book3s_queue_irqprio (vcpu, vec +0x80 ); }else if (!is_mmio &&kvmppc_visible_gfn (vcpu, pte.raddr >>PAGE_SHIFT )) {
@@ -867,17 +867,17 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,if (to_svcpu (vcpu)->fault_dsisr &DSISR_NOHPTE ) { r =kvmppc_handle_pagefault (run, vcpu, dar, exit_nr); }else { - vcpu->arch.dear = dar; + vcpu->arch.shared->dar = dar; vcpu->arch.shared->dsisr =to_svcpu (vcpu)->fault_dsisr;kvmppc_book3s_queue_irqprio (vcpu, exit_nr); -kvmppc_mmu_pte_flush (vcpu, vcpu->arch.dear, ~0xFFFUL ); +kvmppc_mmu_pte_flush (vcpu, dar, ~0xFFFUL ); r =RESUME_GUEST ; }break ; }case BOOK3S_INTERRUPT_DATA_SEGMENT :if (kvmppc_mmu_map_segment (vcpu,kvmppc_get_fault_dar (vcpu)) <0 ) { - vcpu->arch.dear =kvmppc_get_fault_dar (vcpu); + vcpu->arch.shared->dar =kvmppc_get_fault_dar (vcpu);kvmppc_book3s_queue_irqprio (vcpu,BOOK3S_INTERRUPT_DATA_SEGMENT ); }
@@ -997,7 +997,7 @@ program_interrupt:if (kvmppc_read_inst (vcpu) =EMULATE_DONE ) { vcpu->arch.shared->dsisr =kvmppc_alignment_dsisr (vcpu,kvmppc_get_last_inst (vcpu)); - vcpu->arch.dear =kvmppc_alignment_dar (vcpu, + vcpu->arch.shared->dar =kvmppc_alignment_dar (vcpu,kvmppc_get_last_inst (vcpu));kvmppc_book3s_queue_irqprio (vcpu, exit_nr); }
arch/powerpc/kvm/book3s_emulate.c +3 -3
@@ -212,7 +212,7 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, r =kvmppc_st (vcpu, &addr,32 , zeros, true);if ((r = -ENOENT ) || (r = -EPERM )) { *advance =0 ; - vcpu->arch.dear = vaddr; + vcpu->arch.shared->dar = vaddr;to_svcpu (vcpu)->fault_dar = vaddr; dsisr =DSISR_ISSTORE ;
@@ -330,7 +330,7 @@ int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs) vcpu->arch.shared->dsisr = spr_val;break ;case SPRN_DAR : - vcpu->arch.dear = spr_val; + vcpu->arch.shared->dar = spr_val;break ;case SPRN_HIOR :to_book3s (vcpu)->hior = spr_val;
@@ -443,7 +443,7 @@ int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->dsisr);break ;case SPRN_DAR : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.dear); +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->dar);break ;case SPRN_HIOR :kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->hior);
arch/powerpc/kvm/book3s_paired_singles.c +1 -1
@@ -169,7 +169,7 @@ static void kvmppc_inject_pf(struct kvm_vcpu *vcpu, ulong eaddr, bool is_store) shared->msr =kvmppc_set_field (shared->msr,33 ,36 ,0 ); shared->msr =kvmppc_set_field (shared->msr,42 ,47 ,0 ); - vcpu->arch.dear = eaddr; + shared->dar = eaddr;/* Page Fault */ dsisr =kvmppc_set_field (0 ,33 ,33 ,1 );if (is_store)
arch/powerpc/kvm/booke.c +1 -1
@@ -195,7 +195,7 @@ static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,if (update_esr = true) vcpu->arch.esr = vcpu->arch.queued_esr;if (update_dear = true) - vcpu->arch.dear = vcpu->arch.queued_dear; + vcpu->arch.shared->dar = vcpu->arch.queued_dear;kvmppc_set_msr (vcpu, vcpu->arch.shared->msr & msr_mask);clear_bit (priority, &vcpu->arch.pending_exceptions);
arch/powerpc/kvm/booke_emulate.c +2 -2
@@ -105,7 +105,7 @@ int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)switch (sprn) {case SPRN_DEAR : - vcpu->arch.dear = spr_val;break ; + vcpu->arch.shared->dar = spr_val;break ;case SPRN_ESR : vcpu->arch.esr = spr_val;break ;case SPRN_DBCR0 :
@@ -200,7 +200,7 @@ int kvmppc_booke_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)case SPRN_IVPR :kvmppc_set_gpr (vcpu, rt, vcpu->arch.ivpr);break ;case SPRN_DEAR : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.dear);break ; +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->dar);break ;case SPRN_ESR :kvmppc_set_gpr (vcpu, rt, vcpu->arch.esr);break ;case SPRN_DBCR0 :
Signature · 1 line
1.6.0.2
PATCH 5/27 KVM: PPC: Convert SRR0 and SRR1 to shared page
In reply to Alexander GrafThe SRR0 and SRR1 registers contain cached values of the PC and MSR respectively. They get written to by the hypervisor when an interrupt occurs or directly by the kernel. They are also used to tell the rfi(d) instruction where to jump to.
Because it only gets touched on defined events that, it's very simple to share with the guest. Hypervisor and guest both have full r/w access.
This patch converts all users of the current field to the shared page.
--- arch/powerpc/include/asm/kvm_host.h | 2 -- arch/powerpc/include/asm/kvm_para.h | 2 ++ arch/powerpc/kvm/book3s.c | 12 ++++++------ arch/powerpc/kvm/book3s_emulate.c | 4 ++-- arch/powerpc/kvm/booke.c | 15 ++++++++------- arch/powerpc/kvm/booke_emulate.c | 4 ++-- arch/powerpc/kvm/emulate.c | 12 ++++++++---- 7 files changed, 28 insertions(+), 23 deletions(-)
arch/powerpc/include/asm/kvm_host.h +0 -2
@@ -225,8 +225,6 @@ struct kvm_vcpu_arch {ulong sprg5;ulong sprg6;ulong sprg7; -ulong srr0; -ulong srr1;ulong csrr0;ulong csrr1;ulong dsrr0;
arch/powerpc/include/asm/kvm_para.h +2 -0
@@ -23,6 +23,8 @@#include <linux/types.h> struct kvm_vcpu_arch_shared { +__u64 srr0; +__u64 srr1;__u64 dar;__u64 msr;__u32 dsisr;
arch/powerpc/kvm/book3s.c +6 -6
@@ -162,8 +162,8 @@ void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)void kvmppc_inject_interrupt (struct kvm_vcpu *vcpu,int vec,u64 flags) { - vcpu->arch.srr0 =kvmppc_get_pc (vcpu); - vcpu->arch.srr1 = vcpu->arch.shared->msr | flags; + vcpu->arch.shared->srr0 =kvmppc_get_pc (vcpu); + vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags;kvmppc_set_pc (vcpu,to_book3s (vcpu)->hior + vec); vcpu->arch.mmu.reset_msr (vcpu); }
@@ -1059,8 +1059,8 @@ int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) regs->lr =kvmppc_get_lr (vcpu); regs->xer =kvmppc_get_xer (vcpu); regs->msr = vcpu->arch.shared->msr; - regs->srr0 = vcpu->arch.srr0; - regs->srr1 = vcpu->arch.srr1; + regs->srr0 = vcpu->arch.shared->srr0; + regs->srr1 = vcpu->arch.shared->srr1; regs->pid = vcpu->arch.pid; regs->sprg0 = vcpu->arch.sprg0; regs->sprg1 = vcpu->arch.sprg1;
@@ -1086,8 +1086,8 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)kvmppc_set_lr (vcpu, regs->lr);kvmppc_set_xer (vcpu, regs->xer);kvmppc_set_msr (vcpu, regs->msr); - vcpu->arch.srr0 = regs->srr0; - vcpu->arch.srr1 = regs->srr1; + vcpu->arch.shared->srr0 = regs->srr0; + vcpu->arch.shared->srr1 = regs->srr1; vcpu->arch.sprg0 = regs->sprg0; vcpu->arch.sprg1 = regs->sprg1; vcpu->arch.sprg2 = regs->sprg2;
arch/powerpc/kvm/book3s_emulate.c +2 -2
@@ -73,8 +73,8 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,switch (get_xop (inst)) {case OP_19_XOP_RFID :case OP_19_XOP_RFI : -kvmppc_set_pc (vcpu, vcpu->arch.srr0); -kvmppc_set_msr (vcpu, vcpu->arch.srr1); +kvmppc_set_pc (vcpu, vcpu->arch.shared->srr0); +kvmppc_set_msr (vcpu, vcpu->arch.shared->srr1); *advance =0 ;break ;
arch/powerpc/kvm/booke.c +8 -7
@@ -64,7 +64,8 @@ void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)printk ("pc: %08lx msr: %08llx\n" , vcpu->arch.pc, vcpu->arch.shared->msr);printk ("lr: %08lx ctr: %08lx\n" , vcpu->arch.lr, vcpu->arch.ctr); -printk ("srr0: %08lx srr1: %08lx\n" , vcpu->arch.srr0, vcpu->arch.srr1); +printk ("srr0: %08llx srr1: %08llx\n" , vcpu->arch.shared->srr0, + vcpu->arch.shared->srr1);printk ("exceptions: %08lx\n" , vcpu->arch.pending_exceptions);
@@ -189,8 +190,8 @@ static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu, }if (allowed) { - vcpu->arch.srr0 = vcpu->arch.pc; - vcpu->arch.srr1 = vcpu->arch.shared->msr; + vcpu->arch.shared->srr0 = vcpu->arch.pc; + vcpu->arch.shared->srr1 = vcpu->arch.shared->msr; vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];if (update_esr = true) vcpu->arch.esr = vcpu->arch.queued_esr;
@@ -491,8 +492,8 @@ int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) regs->lr = vcpu->arch.lr; regs->xer =kvmppc_get_xer (vcpu); regs->msr = vcpu->arch.shared->msr; - regs->srr0 = vcpu->arch.srr0; - regs->srr1 = vcpu->arch.srr1; + regs->srr0 = vcpu->arch.shared->srr0; + regs->srr1 = vcpu->arch.shared->srr1; regs->pid = vcpu->arch.pid; regs->sprg0 = vcpu->arch.sprg0; regs->sprg1 = vcpu->arch.sprg1;
@@ -518,8 +519,8 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) vcpu->arch.lr = regs->lr;kvmppc_set_xer (vcpu, regs->xer);kvmppc_set_msr (vcpu, regs->msr); - vcpu->arch.srr0 = regs->srr0; - vcpu->arch.srr1 = regs->srr1; + vcpu->arch.shared->srr0 = regs->srr0; + vcpu->arch.shared->srr1 = regs->srr1; vcpu->arch.sprg0 = regs->sprg0; vcpu->arch.sprg1 = regs->sprg1; vcpu->arch.sprg2 = regs->sprg2;
arch/powerpc/kvm/booke_emulate.c +2 -2
@@ -31,8 +31,8 @@static void kvmppc_emul_rfi (struct kvm_vcpu *vcpu) { - vcpu->arch.pc = vcpu->arch.srr0; -kvmppc_set_msr (vcpu, vcpu->arch.srr1); + vcpu->arch.pc = vcpu->arch.shared->srr0; +kvmppc_set_msr (vcpu, vcpu->arch.shared->srr1); } int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
arch/powerpc/kvm/emulate.c +8 -4
@@ -242,9 +242,11 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)switch (sprn) {case SPRN_SRR0 : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.srr0);break ; +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->srr0); +break ;case SPRN_SRR1 : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.srr1);break ; +kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->srr1); +break ;case SPRN_PVR :kvmppc_set_gpr (vcpu, rt, vcpu->arch.pvr);break ;case SPRN_PIR :
@@ -320,9 +322,11 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) rs =get_rs (inst);switch (sprn) {case SPRN_SRR0 : - vcpu->arch.srr0 =kvmppc_get_gpr (vcpu, rs);break ; + vcpu->arch.shared->srr0 =kvmppc_get_gpr (vcpu, rs); +break ;case SPRN_SRR1 : - vcpu->arch.srr1 =kvmppc_get_gpr (vcpu, rs);break ; + vcpu->arch.shared->srr1 =kvmppc_get_gpr (vcpu, rs); +break ;/* XXX We need to context-switch the timebase for * watchdog and FIT. */
Signature · 1 line
1.6.0.2
PATCH 7/27 KVM: PPC: Implement hypervisor interface
In reply to Alexander GrafTo communicate with KVM directly we need to plumb some sort of interface between the guest and KVM. Usually those interfaces use hypercalls.
This hypercall implementation is described in the last patch of the series in a special documentation file. Please read that for further information.
This patch implements stubs to handle KVM PPC hypercalls on the host and guest side alike.
---
v1 -> v2:
- change hypervisor calls to use new register values
v2 -> v3:
- move PV interface to ePAPR
- only check R0 on hypercall
- remove PVR hack
- align hypercalls to in/out of ePAPR
- add kvm.c with hypercall function
--- arch/powerpc/include/asm/kvm_para.h | 114 ++++++++++++++++++++++++++++++++++- arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/kernel/Makefile | 2 + arch/powerpc/kernel/kvm.c | 68 +++++++++++++++++++++ arch/powerpc/kvm/book3s.c | 9 ++- arch/powerpc/kvm/booke.c | 10 +++- arch/powerpc/kvm/powerpc.c | 32 ++++++++++ include/linux/kvm_para.h | 1 + 8 files changed, 233 insertions(+), 4 deletions(-) create mode 100644 arch/powerpc/kernel/kvm.c
arch/powerpc/include/asm/kvm_para.h +113 -1
@@ -21,6 +21,7 @@#define __POWERPC_KVM_PARA_H__#include <linux/types.h> +#include <linux/of.h> struct kvm_vcpu_arch_shared {__u64 sprg0;
@@ -34,16 +35,127 @@ struct kvm_vcpu_arch_shared {__u32 dsisr; }; +#define KVM_SC_MAGIC_R0 0x4b564d21/* "KVM!" */ +#define HC_VENDOR_KVM (42 << 16) +#define HC_EV_SUCCESS 0 +#define HC_EV_UNIMPLEMENTED 12 +#ifdef __KERNEL__ +#ifdef CONFIG_KVM_GUEST + +static inline int kvm_para_available (void ) +{ +struct device_node *hyper_node; + + hyper_node =of_find_node_by_path ("/hypervisor" ); +if (!hyper_node) +return 0 ; + +if (!of_device_is_compatible (hyper_node,"linux,kvm" )) +return 0 ; + +return 1 ; +} + +extern unsigned long kvm_hypercall (unsigned long *in, +unsigned long *out, +unsigned long nr); + +#else +static inline int kvm_para_available (void ) {return 0 ; } +static unsigned long kvm_hypercall (unsigned long *in, +unsigned long *out, +unsigned long nr) +{ +return HC_EV_UNIMPLEMENTED ; +} + +#endif + +static inline long kvm_hypercall0_1 (unsigned int nr,unsigned long *r2) +{ +unsigned long in[8 ]; +unsigned long out[8 ]; +unsigned long r; + + r =kvm_hypercall (in, out, nr |HC_VENDOR_KVM ); + *r2 = out[0 ]; + +return r; +} + +static inline long kvm_hypercall0 (unsigned int nr) +{ +unsigned long in[8 ]; +unsigned long out[8 ]; + +return kvm_hypercall (in, out, nr |HC_VENDOR_KVM ); +} + +static inline long kvm_hypercall1 (unsigned int nr,unsigned long p1) +{ +unsigned long in[8 ]; +unsigned long out[8 ]; + + in[0 ] = p1; +return kvm_hypercall (in, out, nr |HC_VENDOR_KVM ); +} + +static inline long kvm_hypercall2 (unsigned int nr,unsigned long p1, +unsigned long p2) +{ +unsigned long in[8 ]; +unsigned long out[8 ]; + + in[0 ] = p1; + in[1 ] = p2; +return kvm_hypercall (in, out, nr |HC_VENDOR_KVM ); +} + +static inline long kvm_hypercall3 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3) +{ +unsigned long in[8 ]; +unsigned long out[8 ]; + + in[0 ] = p1; + in[1 ] = p2; + in[2 ] = p3; +return kvm_hypercall (in, out, nr |HC_VENDOR_KVM ); +} + +static inline long kvm_hypercall4 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3, +unsigned long p4) +{ +unsigned long in[8 ]; +unsigned long out[8 ]; + + in[0 ] = p1; + in[1 ] = p2; + in[2 ] = p3; + in[3 ] = p4; +return kvm_hypercall (in, out, nr |HC_VENDOR_KVM ); +} + +static inline unsigned int kvm_arch_para_features (void ) { -return 0 ; +unsigned long r; + +if (!kvm_para_available ()) +return 0 ; + +if (kvm_hypercall0_1 (KVM_HC_FEATURES , &r)) +return 0 ; + +return r; }#endif /* __KERNEL__ */
arch/powerpc/include/asm/kvm_ppc.h +1 -0
@@ -107,6 +107,7 @@ extern int kvmppc_booke_init(void);extern void kvmppc_booke_exit (void );extern void kvmppc_core_destroy_mmu (struct kvm_vcpu *vcpu); +extern int kvmppc_kvm_pv (struct kvm_vcpu *vcpu);/* * Cuts out inst bits with ordering according to spec.
arch/powerpc/kernel/Makefile +2 -0
@@ -125,6 +125,8 @@ ifneq ($(CONFIG_XMON)$(CONFIG_KEXEC),) obj-y += ppc_save_regs.o endif +obj-$(CONFIG_KVM_GUEST) += kvm.o + # Disable GCOV in odd or sensitive code GCOV_PROFILE_prom_init.o := n GCOV_PROFILE_ftrace.o := n
arch/powerpc/kernel/kvm.c new file +68 -0
68 lines not on this page · raw message
arch/powerpc/kvm/book3s.c +7 -2
@@ -947,10 +947,10 @@ program_interrupt:break ; }case BOOK3S_INTERRUPT_SYSCALL : -// XXX make user settable if (vcpu->arch.osi_enabled && (((u32 )kvmppc_get_gpr (vcpu,3 )) =OSI_SC_MAGIC_R3 ) && (((u32 )kvmppc_get_gpr (vcpu,4 )) =OSI_SC_MAGIC_R4 )) { +/* MOL hypercalls */ u64 *gprs = run->osi.gprs;int i;
@@ -959,8 +959,13 @@ program_interrupt: gprs[i] =kvmppc_get_gpr (vcpu, i); vcpu->arch.osi_needed =1 ; r =RESUME_HOST_NV ; - + }else if (!(vcpu->arch.shared->msr &MSR_PR ) && + (((u32 )kvmppc_get_gpr (vcpu,0 )) =KVM_SC_MAGIC_R0 )) { +/* KVM PV hypercalls */ +kvmppc_set_gpr (vcpu,3 ,kvmppc_kvm_pv (vcpu)); + r =RESUME_GUEST ; }else { +/* Guest syscalls */ vcpu->stat.syscall_exits++;kvmppc_book3s_queue_irqprio (vcpu, exit_nr); r =RESUME_GUEST ;
arch/powerpc/kvm/booke.c +9 -1
@@ -338,7 +338,15 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,break ;case BOOKE_INTERRUPT_SYSCALL : -kvmppc_booke_queue_irqprio (vcpu,BOOKE_IRQPRIO_SYSCALL ); +if (!(vcpu->arch.shared->msr &MSR_PR ) && + (((u32 )kvmppc_get_gpr (vcpu,0 )) =KVM_SC_MAGIC_R0 )) { +/* KVM PV hypercalls */ +kvmppc_set_gpr (vcpu,3 ,kvmppc_kvm_pv (vcpu)); + r =RESUME_GUEST ; + }else { +/* Guest syscalls */ +kvmppc_booke_queue_irqprio (vcpu,BOOKE_IRQPRIO_SYSCALL ); + }kvmppc_account_exit (vcpu,SYSCALL_EXITS ); r =RESUME_GUEST ;break ;
arch/powerpc/kvm/powerpc.c +32 -0
@@ -42,6 +42,38 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) !!(v->arch.pending_exceptions); } +int kvmppc_kvm_pv (struct kvm_vcpu *vcpu) +{ +int nr =kvmppc_get_gpr (vcpu,11 ); +int r; +unsigned long __maybe_unused param1 =kvmppc_get_gpr (vcpu,3 ); +unsigned long __maybe_unused param2 =kvmppc_get_gpr (vcpu,4 ); +unsigned long __maybe_unused param3 =kvmppc_get_gpr (vcpu,5 ); +unsigned long __maybe_unused param4 =kvmppc_get_gpr (vcpu,6 ); +unsigned long r2 =0 ; + +if (!(vcpu->arch.shared->msr &MSR_SF )) { +/* 32 bit mode */ + param1 &=0xffffffff ; + param2 &=0xffffffff ; + param3 &=0xffffffff ; + param4 &=0xffffffff ; + } + +switch (nr) { +case HC_VENDOR_KVM |KVM_HC_FEATURES : + r =HC_EV_SUCCESS ; + +/* Second return value is in r4 */ +kvmppc_set_gpr (vcpu,4 , r2); +break ; +default : + r =HC_EV_UNIMPLEMENTED ; +break ; + } + +return r; +}int kvmppc_emulate_mmio (struct kvm_run *run,struct kvm_vcpu *vcpu) {
include/linux/kvm_para.h +1 -0
@@ -17,6 +17,7 @@#define KVM_HC_VAPIC_POLL_IRQ 1#define KVM_HC_MMU_OP 2 +#define KVM_HC_FEATURES 3/* * hypercalls use architecture specific
Signature · 1 line
1.6.0.2
PATCH 9/27 KVM: PPC: Add PV guest scratch registers
In reply to Alexander GrafWhile running in hooked code we need to store register contents out because we must not clobber any registers.
So let's add some fields to the shared page we can just happily write to.
--- arch/powerpc/include/asm/kvm_para.h | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_para.h +3 -0
@@ -24,6 +24,9 @@#include <linux/of.h> struct kvm_vcpu_arch_shared { +__u64 scratch1; +__u64 scratch2; +__u64 scratch3;__u64 critical;/* Guest may not get interrupts if = r1 */ __u64 sprg0;__u64 sprg1;
Signature · 1 line
1.6.0.2
PATCH 11/27 KVM: PPC: Make PAM a define
In reply to Alexander GrafOn PowerPC it's very normal to not support all of the physical RAM in real mode. To check if we're matching on the shared page or not, we need to know the limits so we can restrain ourselves to that range.
So let's make it a define instead of open-coding it. And while at it, let's also increase it.
v2 -> v3:
- RMO -> PAM (non-magic page)
--- arch/powerpc/include/asm/kvm_host.h | 3 +++ arch/powerpc/kvm/book3s.c | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/kvm_host.h +3 -0
@@ -48,6 +48,9 @@#define HPTEG_HASH_NUM_VPTE (1 << HPTEG_HASH_BITS_VPTE)#define HPTEG_HASH_NUM_VPTE_LONG (1 << HPTEG_HASH_BITS_VPTE_LONG) +/* Physical Address Mask - allowed range of real mode RAM access */ +#define KVM_PAM 0x0fffffffffffffffULL +struct kvm ;struct kvm_run ;struct kvm_vcpu ;
arch/powerpc/kvm/book3s.c +2 -2
@@ -465,7 +465,7 @@ static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data, r = vcpu->arch.mmu.xlate (vcpu, eaddr, pte, data); }else { pte->eaddr = eaddr; - pte->raddr = eaddr &0xffffffff ; + pte->raddr = eaddr &KVM_PAM ; pte->vpage =VSID_REAL | eaddr >>12 ; pte->may_read = true; pte->may_write = true;
@@ -579,7 +579,7 @@ int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, pte.may_execute = true; pte.may_read = true; pte.may_write = true; - pte.raddr = eaddr &0xffffffff ; + pte.raddr = eaddr &KVM_PAM ; pte.eaddr = eaddr; pte.vpage = eaddr >>12 ; }
Signature · 1 line
1.6.0.2
PATCH 12/27 KVM: PPC: First magic page steps
In reply to Alexander GrafWe will be introducing a method to project the shared page in guest context. As soon as we're talking about this coupling, the shared page is colled magic page.
This patch introduces simple defines, so the follow-up patches are easier to read.
--- arch/powerpc/include/asm/kvm_host.h | 2 ++ include/linux/kvm_para.h | 1 + 2 files changed, 3 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_host.h +2 -0
@@ -287,6 +287,8 @@ struct kvm_vcpu_arch {u64 dec_jiffies;unsigned long pending_exceptions;struct kvm_vcpu_arch_shared *shared; +unsigned long magic_page_pa;/* phys addr to map the magic page to */ +unsigned long magic_page_ea;/* effect. addr to map the magic page to */ #ifdef CONFIG_PPC_BOOK3S struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE ];
include/linux/kvm_para.h +1 -0
@@ -18,6 +18,7 @@#define KVM_HC_VAPIC_POLL_IRQ 1#define KVM_HC_MMU_OP 2#define KVM_HC_FEATURES 3 +#define KVM_HC_PPC_MAP_MAGIC_PAGE 4/* * hypercalls use architecture specific
Signature · 1 line
1.6.0.2
PATCH 13/27 KVM: PPC: Magic Page Book3s support
In reply to Alexander GrafWe need to override EA as well as PA lookups for the magic page. When the guest tells us to project it, the magic page overrides any guest mappings.
In order to reflect that, we need to hook into all the MMU layers of KVM to force map the magic page if necessary.
---
v2 -> v3:
- RMO -> PAM
- combine 32 and 64 real page magic override
- remove leftover goto point
- align hypercalls to in/out of ePAPR
--- arch/powerpc/include/asm/kvm_book3s.h | 1 + arch/powerpc/kvm/book3s.c | 35 ++++++++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_32_mmu.c | 16 +++++++++++++++ arch/powerpc/kvm/book3s_32_mmu_host.c | 2 +- arch/powerpc/kvm/book3s_64_mmu.c | 30 +++++++++++++++++++++++++++- arch/powerpc/kvm/book3s_64_mmu_host.c | 9 +------ 6 files changed, 81 insertions(+), 12 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +1 -0
@@ -130,6 +130,7 @@ extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper, u32 val);extern void kvmppc_giveup_ext (struct kvm_vcpu *vcpu,ulong msr);extern int kvmppc_emulate_paired_single (struct kvm_run *run,struct kvm_vcpu *vcpu); +extern pfn_t kvmppc_gfn_to_pfn (struct kvm_vcpu *vcpu,gfn_t gfn);extern u32 kvmppc_trampoline_lowmem;extern u32 kvmppc_trampoline_enter;
arch/powerpc/kvm/book3s.c +32 -3
@@ -419,6 +419,25 @@ void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr) } } +pfn_t kvmppc_gfn_to_pfn (struct kvm_vcpu *vcpu,gfn_t gfn) +{ +ulong mp_pa = vcpu->arch.magic_page_pa; + +/* Magic page override */ +if (unlikely (mp_pa) && +unlikely (((gfn <<PAGE_SHIFT ) &KVM_PAM ) = + ((mp_pa &PAGE_MASK ) &KVM_PAM ))) { +ulong shared_page = ((ulong )vcpu->arch.shared) &PAGE_MASK ; +pfn_t pfn; + + pfn = (pfn_t )virt_to_phys ((void *)shared_page) >>PAGE_SHIFT ; +get_page (pfn_to_page (pfn)); +return pfn; + } + +return gfn_to_pfn (vcpu->kvm, gfn); +} +/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to * emulate 32 bytes dcbz length.
@@ -554,6 +573,13 @@ mmio:static int kvmppc_visible_gfn (struct kvm_vcpu *vcpu,gfn_t gfn) { +ulong mp_pa = vcpu->arch.magic_page_pa; + +if (unlikely (mp_pa) && +unlikely ((mp_pa &KVM_PAM ) >>PAGE_SHIFT = gfn)) { +return 1 ; + } +return kvm_is_visible_gfn (vcpu->kvm, gfn); }
@@ -1257,6 +1283,7 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)struct kvmppc_vcpu_book3s *vcpu_book3s;struct kvm_vcpu *vcpu;int err = -ENOMEM ; +unsigned long p; vcpu_book3s =vmalloc (sizeof (struct kvmppc_vcpu_book3s ));if (!vcpu_book3s)
@@ -1274,8 +1301,10 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)if (err) goto free_shadow_vcpu; - vcpu->arch.shared = (void *)__get_free_page (GFP_KERNEL |__GFP_ZERO); -if (!vcpu->arch.shared) + p =__get_free_page (GFP_KERNEL |__GFP_ZERO); +/* the real shared page fills the last 4k of our page */ + vcpu->arch.shared = (void *)(p +PAGE_SIZE -4096 ); +if (!p) goto uninit_vcpu; vcpu->arch.host_retip = kvm_return_point;
@@ -1322,7 +1351,7 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) {struct kvmppc_vcpu_book3s *vcpu_book3s =to_book3s (vcpu); -free_page ((unsigned long )vcpu->arch.shared); +free_page ((unsigned long )vcpu->arch.shared &PAGE_MASK );kvm_vcpu_uninit (vcpu);kfree (vcpu_book3s->shadow_vcpu);vfree (vcpu_book3s);
arch/powerpc/kvm/book3s_32_mmu.c +16 -0
@@ -281,8 +281,24 @@ static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, struct kvmppc_pte *pte, bool data) { int r; +ulong mp_ea = vcpu->arch.magic_page_ea; pte->eaddr = eaddr; + +/* Magic page override */ +if (unlikely (mp_ea) && +unlikely ((eaddr & ~0xfffULL ) = (mp_ea & ~0xfffULL )) && + !(vcpu->arch.shared->msr &MSR_PR )) { + pte->vpage =kvmppc_mmu_book3s_32_ea_to_vp (vcpu, eaddr, data); + pte->raddr = vcpu->arch.magic_page_pa | (pte->raddr &0xfff ); + pte->raddr &=KVM_PAM ; + pte->may_execute = true; + pte->may_read = true; + pte->may_write = true; + +return 0 ; + } + r =kvmppc_mmu_book3s_32_xlate_bat (vcpu, eaddr, pte, data);if (r <0 ) r =kvmppc_mmu_book3s_32_xlate_pte (vcpu, eaddr, pte, data, true);
arch/powerpc/kvm/book3s_32_mmu_host.c +1 -1
@@ -147,7 +147,7 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)struct hpte_cache *pte;/* Get host physical address for gpa */ - hpaddr =gfn_to_pfn (vcpu->kvm, orig_pte->raddr >>PAGE_SHIFT ); + hpaddr =kvmppc_gfn_to_pfn (vcpu, orig_pte->raddr >>PAGE_SHIFT );if (kvm_is_error_hva (hpaddr)) {printk (KERN_INFO "Couldn't get guest page for gfn %lx!\n" , orig_pte->eaddr);
arch/powerpc/kvm/book3s_64_mmu.c +29 -1
@@ -163,6 +163,22 @@ static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,bool found = false;bool perm_err = false;int second =0 ; +ulong mp_ea = vcpu->arch.magic_page_ea; + +/* Magic page override */ +if (unlikely (mp_ea) && +unlikely ((eaddr & ~0xfffULL ) = (mp_ea & ~0xfffULL )) && + !(vcpu->arch.shared->msr &MSR_PR )) { + gpte->eaddr = eaddr; + gpte->vpage =kvmppc_mmu_book3s_64_ea_to_vp (vcpu, eaddr, data); + gpte->raddr = vcpu->arch.magic_page_pa | (gpte->raddr &0xfff ); + gpte->raddr &=KVM_PAM ; + gpte->may_execute = true; + gpte->may_read = true; + gpte->may_write = true; + +return 0 ; + } slbe =kvmppc_mmu_book3s_64_find_slbe (vcpu_book3s, eaddr);if (!slbe)
@@ -445,6 +461,7 @@ static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,ulong ea = esid <<SID_SHIFT ;struct kvmppc_slb *slb;u64 gvsid = esid; +ulong mp_ea = vcpu->arch.magic_page_ea;if (vcpu->arch.shared->msr & (MSR_DR |MSR_IR )) { slb =kvmppc_mmu_book3s_64_find_slbe (to_book3s (vcpu), ea);
@@ -464,7 +481,7 @@ static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,break ;case MSR_DR |MSR_IR :if (!slb) -return -ENOENT ; + goto no_slb; *vsid = gvsid;break ;
@@ -477,6 +494,17 @@ static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid, *vsid |=VSID_PR ;return 0 ; + +no_slb: +/* Catch magic page case */ +if (unlikely (mp_ea) && +unlikely (esid = (mp_ea >>SID_SHIFT )) && + !(vcpu->arch.shared->msr &MSR_PR )) { + *vsid =VSID_REAL | esid; +return 0 ; + } + +return -EINVAL ; }static bool kvmppc_mmu_book3s_64_is_dcbz32 (struct kvm_vcpu *vcpu)
arch/powerpc/kvm/book3s_64_mmu_host.c +2 -7
@@ -101,18 +101,13 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)struct kvmppc_sid_map *map;/* Get host physical address for gpa */ - hpaddr =gfn_to_pfn (vcpu->kvm, orig_pte->raddr >>PAGE_SHIFT ); + hpaddr =kvmppc_gfn_to_pfn (vcpu, orig_pte->raddr >>PAGE_SHIFT );if (kvm_is_error_hva (hpaddr)) {printk (KERN_INFO "Couldn't get guest page for gfn %lx!\n" , orig_pte->eaddr);return -EINVAL ; } hpaddr <<=PAGE_SHIFT ; -#if PAGE_SHIFT = 12 -#elif PAGE_SHIFT = 16 - hpaddr |= orig_pte->raddr &0xf000 ; -#else -#error Unknown page size -#endif + hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK );/* and write the mapping ea -> hpa into the pt */ vcpu->arch.mmu.esid_to_vsid (vcpu, orig_pte->eaddr >>SID_SHIFT , &vsid);
Signature · 1 line
1.6.0.2
PATCH 15/27 KVM: Move kvm_guest_init out of generic code
In reply to Alexander GrafCurrently x86 is the only architecture that uses kvm_guest_init(). With PowerPC we're getting a second user, but the signature is different there and we don't need to export it, as it uses the normal kernel init framework.
So let's move the x86 specific definition of that function over to the x86 specfic header file.
--- arch/x86/include/asm/kvm_para.h | 6 ++++++ include/linux/kvm_para.h | 5 ----- 2 files changed, 6 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_para.h +6 -0
@@ -158,6 +158,12 @@ static inline unsigned int kvm_arch_para_features(void)return cpuid_eax (KVM_CPUID_FEATURES ); } +#ifdef CONFIG_KVM_GUEST +void __init kvm_guest_init (void ); +#else +#define kvm_guest_init () do { } while (0)#endif +#endif /* __KERNEL__ */ +#endif /* _ASM_X86_KVM_PARA_H */
include/linux/kvm_para.h +0 -5
@@ -26,11 +26,6 @@#include <asm/kvm_para.h> #ifdef __KERNEL__ -#ifdef CONFIG_KVM_GUEST -void __init kvm_guest_init (void ); -#else -#define kvm_guest_init () do { } while (0) -#endif static inline int kvm_para_has_feature (unsigned int feature) {
Signature · 1 line
1.6.0.2
PATCH 16/27 KVM: PPC: Generic KVM PV guest support
In reply to Alexander GrafWe have all the hypervisor pieces in place now, but the guest parts are still missing.
This patch implements basic awareness of KVM when running Linux as guest. It doesn't do anything with it yet though.
---
v2 -> v3:
- Add hypercall stub
--- arch/powerpc/kernel/Makefile | 2 +- arch/powerpc/kernel/asm-offsets.c | 15 +++++++++++++++ arch/powerpc/kernel/kvm.c | 3 +++ arch/powerpc/kernel/kvm_emul.S | 37 +++++++++++++++++++++++++++++++++++++ arch/powerpc/platforms/Kconfig | 10 ++++++++++ 5 files changed, 66 insertions(+), 1 deletions(-) create mode 100644 arch/powerpc/kernel/kvm_emul.S
arch/powerpc/kernel/Makefile +1 -1
@@ -125,7 +125,7 @@ ifneq ($(CONFIG_XMON)$(CONFIG_KEXEC),) obj-y += ppc_save_regs.o endif -obj-$(CONFIG_KVM_GUEST) += kvm.o +obj-$(CONFIG_KVM_GUEST) += kvm.o kvm_emul.o # Disable GCOV in odd or sensitive code GCOV_PROFILE_prom_init.o := n
arch/powerpc/kernel/asm-offsets.c +15 -0
@@ -465,6 +465,21 @@ int main(void) DEFINE(VCPU_FAULT_ESR, offsetof(struct kvm_vcpu, arch.fault_esr));#endif /* CONFIG_PPC_BOOK3S */ #endif + +#ifdef CONFIG_KVM_GUEST +DEFINE (KVM_MAGIC_SCRATCH1 , offsetof(struct kvm_vcpu_arch_shared , + scratch1)); +DEFINE (KVM_MAGIC_SCRATCH2 , offsetof(struct kvm_vcpu_arch_shared , + scratch2)); +DEFINE (KVM_MAGIC_SCRATCH3 , offsetof(struct kvm_vcpu_arch_shared , + scratch3)); +DEFINE (KVM_MAGIC_INT , offsetof(struct kvm_vcpu_arch_shared , + int_pending)); +DEFINE (KVM_MAGIC_MSR , offsetof(struct kvm_vcpu_arch_shared , msr)); +DEFINE (KVM_MAGIC_CRITICAL , offsetof(struct kvm_vcpu_arch_shared , + critical)); +#endif +#ifdef CONFIG_44xDEFINE (PGD_T_LOG2 ,PGD_T_LOG2 );DEFINE (PTE_T_LOG2 ,PTE_T_LOG2 );
arch/powerpc/kernel/kvm.c +3 -0
@@ -30,6 +30,9 @@#include <asm/cacheflush.h> #include <asm/disassemble.h> +#define KVM_MAGIC_PAGE (-4096L) +#define magic_var (x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x) +unsigned long kvm_hypercall (unsigned long *in,unsigned long *out,unsigned long nr)
arch/powerpc/kernel/kvm_emul.S new file +37 -0
@@ -0,0 +1,37 @@ +/* + * 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. + * + * Copyright SUSE Linux Products GmbH 2010 + * + * Authors: Alexander Graf <[email protected]> + */ + +#include <asm/ppc_asm.h> +#include <asm/kvm_asm.h> +#include <asm/reg.h> +#include <asm/page.h> +#include <asm/asm-offsets.h> + +/* Hypercall entry point. Will be patched with device tree instructions. */ + +.global kvm_hypercall_start +kvm_hypercall_start: + li r3, -1 + nop + nop + nop + blr + +#define KVM_MAGIC_PAGE (-4096) +
arch/powerpc/platforms/Kconfig +10 -0
@@ -21,6 +21,16 @@ source "arch/powerpc/platforms/44x/Kconfig" source "arch/powerpc/platforms/40x/Kconfig" source "arch/powerpc/platforms/amigaone/Kconfig" +config KVM_GUEST + bool "KVM Guest support" + default y + ---help--- + This option enables various optimizations for running under the KVM + hypervisor. Overhead for the kernel when not running inside KVM should + be minimal. + + In case of doubt, say Y + config PPC_NATIVE bool depends on 6xx || PPC64
Signature · 1 line
1.6.0.2
PATCH 17/27 KVM: PPC: KVM PV guest stubs
In reply to Alexander GrafWe will soon start and replace instructions from the text section with other, paravirtualized versions. To ease the readability of those patches I split out the generic looping and magic page mapping code out.
This patch still only contains stubs. But at least it loops through the text section :).
---
v1 -> v2:
- kvm guest patch framework: introduce patch_ins
v2 -> v3:
- add self-test in guest code
- remove superfluous new lines in generic guest code
--- arch/powerpc/kernel/kvm.c | 95 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 95 insertions(+), 0 deletions(-)
arch/powerpc/kernel/kvm.c +95 -0
@@ -33,6 +33,62 @@#define KVM_MAGIC_PAGE (-4096L)#define magic_var (x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x) +#define KVM_MASK_RT 0x03e00000 + +static bool kvm_patching_worked = true; + +static inline void kvm_patch_ins (u32 *inst,u32 new_inst) +{ + *inst = new_inst; +flush_icache_range ((ulong )inst, (ulong )inst +4 ); +} + +static void kvm_map_magic_page (void *data) +{ +kvm_hypercall2 (KVM_HC_PPC_MAP_MAGIC_PAGE , +KVM_MAGIC_PAGE ,/* Physical Address */ +KVM_MAGIC_PAGE );/* Effective Address */ +} + +static void kvm_check_ins (u32 *inst) +{ +u32 _inst = *inst; +u32 inst_no_rt = _inst & ~KVM_MASK_RT ; +u32 inst_rt = _inst &KVM_MASK_RT ; + +switch (inst_no_rt) { + } + +switch (_inst) { + } +} + +static void kvm_use_magic_page (void ) +{ +u32 *p; +u32 *start, *end; +u32 tmp; + +/* Tell the host to map the magic page to -4096 on all CPUs */ +on_each_cpu (kvm_map_magic_page,NULL ,1 ); + +/* Quick self-test to see if the mapping works */ +if (__get_user (tmp, (u32 *)KVM_MAGIC_PAGE )) { + kvm_patching_worked = false; +return ; + } + +/* Now loop through all code and find instructions */ + start = (void *)_stext; + end = (void *)_etext; + +for (p = start; p < end; p++) +kvm_check_ins (p); + +printk (KERN_INFO "KVM: Live patching for a fast VM %s\n" , + kvm_patching_worked ?"worked" :"failed" ); +} +unsigned long kvm_hypercall (unsigned long *in,unsigned long *out,unsigned long nr)
@@ -69,3 +125,42 @@ unsigned long kvm_hypercall(unsigned long *in,return r3; }EXPORT_SYMBOL_GPL (kvm_hypercall); + +static int kvm_para_setup (void ) +{ +extern u32 kvm_hypercall_start; +struct device_node *hyper_node; +u32 *insts; +int len, i; + + hyper_node =of_find_node_by_path ("/hypervisor" ); +if (!hyper_node) +return -1 ; + + insts = (u32 *)of_get_property (hyper_node,"hcall-instructions" , &len); +if (len %4 ) +return -1 ; +if (len > (4 *4 )) +return -1 ; + +for (i =0 ; i < (len /4 ); i++) +kvm_patch_ins (&(&kvm_hypercall_start)[i], insts[i]); + +return 0 ; +} + +static int __init kvm_guest_init (void ) +{ +if (!kvm_para_available ()) +return 0 ; + +if (kvm_para_setup ()) +return 0 ; + +if (kvm_para_has_feature (KVM_FEATURE_MAGIC_PAGE )) +kvm_use_magic_page (); + +return 0 ; +} + +postcore_initcall (kvm_guest_init);
Signature · 1 line
1.6.0.2
A message that is not in the archive (sent off-list or not yet received): 1280407688-9815-18-git-send-email-agraf-l3A5Bk7waGM@public.gmane.org
Re: [PATCH 17/27] KVM: PPC: KVM PV guest stubs
Quoted 77 lines · Alexander Graf
We will soon start and replace instructions from the text section with other, paravirtualized versions. To ease the readability of those patches I split out the generic looping and magic page mapping code out.
This patch still only contains stubs. But at least it loops through the text section :).
Signed-off-by Alexander Graf<[email protected]>---
v1 -> v2:
- kvm guest patch framework: introduce
patch_insv2 -> v3:
- add self-test in guest code
- remove superfluous new lines in generic guest code--- arch/powerpc/kernel/kvm.c | 95 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 95 insertions(+), 0 deletions(-)arch/powerpc/kernel/kvm.c +46
-0@@ -33,6 +33,62 @@#define KVM_MAGIC_PAGE (-4096L)#define magic_var (x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x) +#define KVM_MASK_RT 0x03e00000 + +static bool kvm_patching_worked = true; + +static inline void kvm_patch_ins (u32 *inst,u32 new_inst) +{ + *inst = new_inst; +flush_icache_range ((ulong )inst, (ulong )inst +4 ); +} + +static void kvm_map_magic_page (void *data) +{ +kvm_hypercall2 (KVM_HC_PPC_MAP_MAGIC_PAGE , +KVM_MAGIC_PAGE ,/* Physical Address */ +KVM_MAGIC_PAGE );/* Effective Address */ +} + +static void kvm_check_ins (u32 *inst) +{ +u32 _inst = *inst; +u32 inst_no_rt = _inst& ~KVM_MASK_RT ; +u32 inst_rt = _inst&KVM_MASK_RT ; + +switch (inst_no_rt) { + } + +switch (_inst) { + } +} + +static void kvm_use_magic_page (void ) +{ +u32 *p; +u32 *start, *end; +u32 tmp; + +/* Tell the host to map the magic page to -4096 on all CPUs */ +on_each_cpu (kvm_map_magic_page,NULL ,1 ); + +/* Quick self-test to see if the mapping works */ +if (__get_user (tmp, (u32 *)KVM_MAGIC_PAGE )) { + kvm_patching_worked = false; +return ; + } + +/* Now loop through all code and find instructions */
arch/powerpc/kernel/kvm.c line 91
Quoted 10 lines
arch/powerpc/kernel/kvm.c +9
-0+ start = (void *)_stext; + end = (void *)_etext; + +for (p = start; p< end; p++) +kvm_check_ins (p); + +printk (KERN_INFO "KVM: Live patching for a fast VM %s\n" , + kvm_patching_worked ?"worked" :"failed" ); +}
Rather than have the guest loop through every instruction in its text, why can't you use the existing cputable self-patching mechanism? The kernel already uses that in a number of places to patch itself at runtime in fast paths... see Documentation/powerpc/cpu_features.txt for some background.
Since we already know (at build time) the location of code that needs patching, we don't need to scan at all. (I also shudder to think of the number of page faults this scan will incur.)
Hollis Blanchard
Mentor Graphics, Embedded Systems Division
PATCH 18/27 KVM: PPC: PV instructions to loads and stores
In reply to Alexander GrafSome instructions can simply be replaced by load and store instructions to or from the magic page.
This patch replaces often called instructions that fall into the above category.
---
v1 -> v2:
- use kvm_patch_ins
--- arch/powerpc/kernel/kvm.c | 109 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 109 insertions(+), 0 deletions(-)
arch/powerpc/kernel/kvm.c +109 -0
@@ -33,7 +33,34 @@#define KVM_MAGIC_PAGE (-4096L)#define magic_var (x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x) +#define KVM_INST_LWZ 0x80000000 +#define KVM_INST_STW 0x90000000 +#define KVM_INST_LD 0xe8000000 +#define KVM_INST_STD 0xf8000000 +#define KVM_INST_NOP 0x60000000 +#define KVM_INST_B 0x48000000 +#define KVM_INST_B_MASK 0x03ffffff +#define KVM_INST_B_MAX 0x01ffffff +#define KVM_MASK_RT 0x03e00000 +#define KVM_INST_MFMSR 0x7c0000a6 +#define KVM_INST_MFSPR_SPRG0 0x7c1042a6 +#define KVM_INST_MFSPR_SPRG1 0x7c1142a6 +#define KVM_INST_MFSPR_SPRG2 0x7c1242a6 +#define KVM_INST_MFSPR_SPRG3 0x7c1342a6 +#define KVM_INST_MFSPR_SRR0 0x7c1a02a6 +#define KVM_INST_MFSPR_SRR1 0x7c1b02a6 +#define KVM_INST_MFSPR_DAR 0x7c1302a6 +#define KVM_INST_MFSPR_DSISR 0x7c1202a6 + +#define KVM_INST_MTSPR_SPRG0 0x7c1043a6 +#define KVM_INST_MTSPR_SPRG1 0x7c1143a6 +#define KVM_INST_MTSPR_SPRG2 0x7c1243a6 +#define KVM_INST_MTSPR_SPRG3 0x7c1343a6 +#define KVM_INST_MTSPR_SRR0 0x7c1a03a6 +#define KVM_INST_MTSPR_SRR1 0x7c1b03a6 +#define KVM_INST_MTSPR_DAR 0x7c1303a6 +#define KVM_INST_MTSPR_DSISR 0x7c1203a6static bool kvm_patching_worked = true;
@@ -43,6 +70,34 @@ static inline void kvm_patch_ins(u32 *inst, u32 new_inst)flush_icache_range ((ulong )inst, (ulong )inst +4 ); } +static void kvm_patch_ins_ld (u32 *inst,long addr,u32 rt) +{ +#ifdef CONFIG_64BIT +kvm_patch_ins (inst,KVM_INST_LD | rt | (addr &0x0000fffc )); +#else +kvm_patch_ins (inst,KVM_INST_LWZ | rt | ((addr +4 ) &0x0000fffc )); +#endif +} + +static void kvm_patch_ins_lwz (u32 *inst,long addr,u32 rt) +{ +kvm_patch_ins (inst,KVM_INST_LWZ | rt | (addr &0x0000ffff )); +} + +static void kvm_patch_ins_std (u32 *inst,long addr,u32 rt) +{ +#ifdef CONFIG_64BIT +kvm_patch_ins (inst,KVM_INST_STD | rt | (addr &0x0000fffc )); +#else +kvm_patch_ins (inst,KVM_INST_STW | rt | ((addr +4 ) &0x0000fffc )); +#endif +} + +static void kvm_patch_ins_stw (u32 *inst,long addr,u32 rt) +{ +kvm_patch_ins (inst,KVM_INST_STW | rt | (addr &0x0000fffc )); +} +static void kvm_map_magic_page (void *data) { kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
@@ -57,6 +112,60 @@ static void kvm_check_ins(u32 *inst)u32 inst_rt = _inst &KVM_MASK_RT ;switch (inst_no_rt) { +/* Loads */ +case KVM_INST_MFMSR : +kvm_patch_ins_ld (inst,magic_var (msr), inst_rt); +break ; +case KVM_INST_MFSPR_SPRG0 : +kvm_patch_ins_ld (inst,magic_var (sprg0), inst_rt); +break ; +case KVM_INST_MFSPR_SPRG1 : +kvm_patch_ins_ld (inst,magic_var (sprg1), inst_rt); +break ; +case KVM_INST_MFSPR_SPRG2 : +kvm_patch_ins_ld (inst,magic_var (sprg2), inst_rt); +break ; +case KVM_INST_MFSPR_SPRG3 : +kvm_patch_ins_ld (inst,magic_var (sprg3), inst_rt); +break ; +case KVM_INST_MFSPR_SRR0 : +kvm_patch_ins_ld (inst,magic_var (srr0), inst_rt); +break ; +case KVM_INST_MFSPR_SRR1 : +kvm_patch_ins_ld (inst,magic_var (srr1), inst_rt); +break ; +case KVM_INST_MFSPR_DAR : +kvm_patch_ins_ld (inst,magic_var (dar), inst_rt); +break ; +case KVM_INST_MFSPR_DSISR : +kvm_patch_ins_lwz (inst,magic_var (dsisr), inst_rt); +break ; + +/* Stores */ +case KVM_INST_MTSPR_SPRG0 : +kvm_patch_ins_std (inst,magic_var (sprg0), inst_rt); +break ; +case KVM_INST_MTSPR_SPRG1 : +kvm_patch_ins_std (inst,magic_var (sprg1), inst_rt); +break ; +case KVM_INST_MTSPR_SPRG2 : +kvm_patch_ins_std (inst,magic_var (sprg2), inst_rt); +break ; +case KVM_INST_MTSPR_SPRG3 : +kvm_patch_ins_std (inst,magic_var (sprg3), inst_rt); +break ; +case KVM_INST_MTSPR_SRR0 : +kvm_patch_ins_std (inst,magic_var (srr0), inst_rt); +break ; +case KVM_INST_MTSPR_SRR1 : +kvm_patch_ins_std (inst,magic_var (srr1), inst_rt); +break ; +case KVM_INST_MTSPR_DAR : +kvm_patch_ins_std (inst,magic_var (dar), inst_rt); +break ; +case KVM_INST_MTSPR_DSISR : +kvm_patch_ins_stw (inst,magic_var (dsisr), inst_rt); +break ; }switch (_inst) {
Signature · 1 line
1.6.0.2
PATCH 22/27 KVM: PPC: PV assembler helpers
In reply to Alexander GrafWhen we hook an instruction we need to make sure we don't clobber any of the registers at that point. So we write them out to scratch space in the magic page. To make sure we don't fall into a race with another piece of hooked code, we need to disable interrupts.
To make the later patches and code in general easier readable, let's introduce a set of defines that save and restore r30, r31 and cr. Let's also define some helpers to read the lower 32 bits of a 64 bit field on 32 bit systems.
--- arch/powerpc/kernel/kvm_emul.S | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-)
arch/powerpc/kernel/kvm_emul.S +29 -0
@@ -35,3 +35,32 @@ kvm_hypercall_start: #define KVM_MAGIC_PAGE (-4096) +#ifdef CONFIG_64BIT +#define LL64(reg, offs, reg2) ld reg, (offs)(reg2) +#define STL64(reg, offs, reg2) std reg, (offs)(reg2) +#else +#define LL64(reg, offs, reg2) lwz reg, (offs + 4)(reg2) +#define STL64(reg, offs, reg2) stw reg, (offs + 4)(reg2) +#endif + +#define SCRATCH_SAVE \ + /* Enable critical section. We are critical if \ + shared->critical = r1 */ \ + STL64(r1, KVM_MAGIC_PAGE + KVM_MAGIC_CRITICAL, 0); \ + \ + /* Save state */ \ + PPC_STL r31, (KVM_MAGIC_PAGE + KVM_MAGIC_SCRATCH1)(0); \ + PPC_STL r30, (KVM_MAGIC_PAGE + KVM_MAGIC_SCRATCH2)(0); \ + mfcr r31; \ + stw r31, (KVM_MAGIC_PAGE + KVM_MAGIC_SCRATCH3)(0); + +#define SCRATCH_RESTORE \ + /* Restore state */ \ + PPC_LL r31, (KVM_MAGIC_PAGE + KVM_MAGIC_SCRATCH1)(0); \ + lwz r30, (KVM_MAGIC_PAGE + KVM_MAGIC_SCRATCH3)(0); \ + mtcr r30; \ + PPC_LL r30, (KVM_MAGIC_PAGE + KVM_MAGIC_SCRATCH2)(0); \ + \ + /* Disable critical section. We are critical if \ + shared->critical = r1 and r2 is always != r1 */ \ + STL64(r2, KVM_MAGIC_PAGE + KVM_MAGIC_CRITICAL, 0);
Signature · 1 line
1.6.0.2
PATCH 24/27 KVM: PPC: PV mtmsrd L=0 and mtmsr
In reply to Alexander GrafThere is also a form of mtmsr where all bits need to be addressed. While the PPC64 Linux kernel behaves resonably well here, on PPC32 we do not have an L=1 form. It does mtmsr even for simple things like only changing EE.
So we need to hook into that one as well and check for a mask of bits that we deem safe to change from within guest context.
---
v1 -> v2:
- use kvm_patch_ins_b
--- arch/powerpc/kernel/kvm.c | 51 ++++++++++++++++++++++++ arch/powerpc/kernel/kvm_emul.S | 84 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 0 deletions(-)
arch/powerpc/kernel/kvm.c +51 -0
@@ -63,7 +63,9 @@#define KVM_INST_MTSPR_DSISR 0x7c1203a6#define KVM_INST_TLBSYNC 0x7c00046c +#define KVM_INST_MTMSRD_L0 0x7c000164#define KVM_INST_MTMSRD_L1 0x7c010164 +#define KVM_INST_MTMSR 0x7c000124static bool kvm_patching_worked = true;static char kvm_tmp[1024 *1024 ];
@@ -176,6 +178,49 @@ static void kvm_patch_ins_mtmsrd(u32 *inst, u32 rt)kvm_patch_ins_b (inst, distance_start); } +extern u32 kvm_emulate_mtmsr_branch_offs; +extern u32 kvm_emulate_mtmsr_reg1_offs; +extern u32 kvm_emulate_mtmsr_reg2_offs; +extern u32 kvm_emulate_mtmsr_reg3_offs; +extern u32 kvm_emulate_mtmsr_orig_ins_offs; +extern u32 kvm_emulate_mtmsr_len; +extern u32 kvm_emulate_mtmsr[]; + +static void kvm_patch_ins_mtmsr (u32 *inst,u32 rt) +{ +u32 *p; +int distance_start; +int distance_end; +ulong next_inst; + + p =kvm_alloc (kvm_emulate_mtmsr_len *4 ); +if (!p) +return ; + +/* Find out where we are and put everything there */ + distance_start = (ulong )p - (ulong )inst; + next_inst = ((ulong )inst +4 ); + distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsr_branch_offs]; + +/* Make sure we only write valid b instructions */ +if (distance_start >KVM_INST_B_MAX ) { + kvm_patching_worked = false; +return ; + } + +/* Modify the chunk to fit the invocation */ +memcpy (p, kvm_emulate_mtmsr, kvm_emulate_mtmsr_len *4 ); + p[kvm_emulate_mtmsr_branch_offs] |= distance_end &KVM_INST_B_MASK ; + p[kvm_emulate_mtmsr_reg1_offs] |= rt; + p[kvm_emulate_mtmsr_reg2_offs] |= rt; + p[kvm_emulate_mtmsr_reg3_offs] |= rt; + p[kvm_emulate_mtmsr_orig_ins_offs] = *inst; +flush_icache_range ((ulong )p, (ulong )p + kvm_emulate_mtmsr_len *4 ); + +/* Patch the invocation */ +kvm_patch_ins_b (inst, distance_start); +} +static void kvm_map_magic_page (void *data) { kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
@@ -256,6 +301,12 @@ static void kvm_check_ins(u32 *inst)if (get_rt (inst_rt) <30 )kvm_patch_ins_mtmsrd (inst, inst_rt);break ; +case KVM_INST_MTMSR : +case KVM_INST_MTMSRD_L0 : +/* We use r30 and r31 during the hook */ +if (get_rt (inst_rt) <30 ) +kvm_patch_ins_mtmsr (inst, inst_rt); +break ; }switch (_inst) {
arch/powerpc/kernel/kvm_emul.S +84 -0
87 lines not on this page · raw message
Signature · 1 line
1.6.0.2
PATCH 27/27 KVM: PPC: Add get_pvinfo interface to query hypercall instructions
In reply to Alexander GrafWe need to tell the guest the opcodes that make up a hypercall through interfaces that are controlled by userspace. So we need to add a call for userspace to allow it to query those opcodes so it can pass them on.
This is required because the hypercall opcodes can change based on the hypervisor conditions. If we're running in hardware accelerated hypervisor mode, a hypercall looks different from when we're running without hardware acceleration.
--- Documentation/kvm/api.txt | 23 +++++++++++++++++++++++ arch/powerpc/kvm/powerpc.c | 38 ++++++++++++++++++++++++++++++++++++++ include/linux/kvm.h | 11 +++++++++++ 3 files changed, 72 insertions(+), 0 deletions(-)
Documentation/kvm/api.txt +23 -0
@@ -1032,6 +1032,29 @@ are defined as follows: eax, ebx, ecx, edx: the values returned by the cpuid instruction for this function/index combination +4.46 KVM_PPC_GET_PVINFO + +Capability: KVM_CAP_PPC_GET_PVINFO +Architectures: ppc +Type: vm ioctl +Parameters: struct kvm_ppc_pvinfo (out) +Returns: 0 on success, !0 on error + +struct kvm_ppc_pvinfo { + __u32 flags; + __u32 hcall[4]; + __u8 pad[108]; +}; + +This ioctl fetches PV specific information that need to be passed to the guest +using the device tree or other means from vm context. + +For now the only implemented piece of information distributed here is an array +of 4 instructions that make up a hypercall. + +If any additional field gets added to this structure later on, a bit for that +additional piece of information will be set in the flags bitmap. + 5. The kvm_run structure Application code obtains a pointer to the kvm_run structure by
arch/powerpc/kvm/powerpc.c +38 -0
@@ -191,6 +191,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_PPC_UNSET_IRQ :case KVM_CAP_ENABLE_CAP :case KVM_CAP_PPC_OSI : +case KVM_CAP_PPC_GET_PVINFO : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
@@ -578,16 +579,53 @@ out:return r; } +static int kvm_vm_ioctl_get_pvinfo (struct kvm_ppc_pvinfo *pvinfo) +{ +u32 inst_lis =0x3c000000 ; +u32 inst_ori =0x60000000 ; +u32 inst_nop =0x60000000 ; +u32 inst_sc =0x44000002 ; +u32 inst_imm_mask =0xffff ; + +/* +* The hypercall to get into KVM from within guest context is as +* follows: +* +* lis r0, r0, KVM_SC_MAGIC_R0@h +* ori r0, KVM_SC_MAGIC_R0@l +* sc +* nop +*/ + pvinfo->hcall[0 ] = inst_lis | ((KVM_SC_MAGIC_R0 >>16 ) & inst_imm_mask); + pvinfo->hcall[1 ] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask); + pvinfo->hcall[2 ] = inst_sc; + pvinfo->hcall[3 ] = inst_nop; + +return 0 ; +} +long kvm_arch_vm_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) { +void __user *argp = (void __user *)arg;long r;switch (ioctl) { +case KVM_PPC_GET_PVINFO : { +struct kvm_ppc_pvinfo pvinfo; + r =kvm_vm_ioctl_get_pvinfo (&pvinfo); +if (copy_to_user (argp, &pvinfo,sizeof (pvinfo))) { + r = -EFAULT ; + goto out; + } + +break ; + }default : r = -ENOTTY ; } +out:return r; }
include/linux/kvm.h +11 -0
@@ -414,6 +414,14 @@ struct kvm_enable_cap {__u8 pad[64 ]; }; +/* for KVM_PPC_GET_PVINFO */ +struct kvm_ppc_pvinfo { +/* out */ +__u32 flags; +__u32 hcall[4 ]; +__u8 pad[108 ]; +}; +#define KVMIO 0xAE/*
@@ -530,6 +538,7 @@ struct kvm_enable_cap {#ifdef __KVM_HAVE_XCRS#define KVM_CAP_XCRS 56#endif +#define KVM_CAP_PPC_GET_PVINFO 57#ifdef KVM_CAP_IRQ_ROUTING
@@ -664,6 +673,8 @@ struct kvm_clock_data {/* Available with KVM_CAP_PIT_STATE2 */ #define KVM_GET_PIT2 _IOR(KVMIO, 0x9f, struct kvm_pit_state2)#define KVM_SET_PIT2 _IOW(KVMIO, 0xa0, struct kvm_pit_state2) +/* Available with KVM_CAP_PPC_GET_PVINFO */ +#define KVM_PPC_GET_PVINFO _IOW(KVMIO, 0xa1, struct kvm_ppc_pvinfo)/* * ioctls for vcpu fds
Signature · 1 line
1.6.0.2