Skip to content

Commit f3383cf

Browse files
committed
KVM: PPC: Disable NX for old magic page using guests
Old guests try to use the magic page, but map their trampoline code inside of an NX region. Since we can't fix those old kernels, try to detect whether the guest is sane or not. If not, just disable NX functionality in KVM so that old guests at least work at all. For newer guests, add a bit that we can set to keep NX functionality available. Signed-off-by: Alexander Graf <agraf@suse.de>
1 parent 1f365bb commit f3383cf

5 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎Documentation/virtual/kvm/ppc-pv.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,24 @@ a bitmap of available features inside the magic page.
9494
The following enhancements to the magic page are currently available:
9595

9696
KVM_MAGIC_FEAT_SR Maps SR registers r/w in the magic page
97+
KVM_MAGIC_FEAT_MAS0_TO_SPRG7 Maps MASn, ESR, PIR and high SPRGs
9798

9899
For enhanced features in the magic page, please check for the existence of the
99100
feature before using them!
100101

102+
Magic page flags
103+
================
104+
105+
In addition to features that indicate whether a host is capable of a particular
106+
feature we also have a channel for a guest to tell the guest whether it's capable
107+
of something. This is what we call "flags".
108+
109+
Flags are passed to the host in the low 12 bits of the Effective Address.
110+
111+
The following flags are currently available for a guest to expose:
112+
113+
MAGIC_PAGE_FLAG_NOT_MAPPED_NX Guest handles NX bits correclty wrt magic page
114+
101115
MSR bits
102116
========
103117

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ struct kvm_vcpu_arch {
631631
#endif
632632
unsigned long magic_page_pa; /* phys addr to map the magic page to */
633633
unsigned long magic_page_ea; /* effect. addr to map the magic page to */
634+
bool disable_kernel_nx;
634635

635636
int irq_type; /* one of KVM_IRQ_* */
636637
int irq_cpu_id;

‎arch/powerpc/include/uapi/asm/kvm_para.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,16 @@ struct kvm_vcpu_arch_shared {
8282

8383
#define KVM_FEATURE_MAGIC_PAGE 1
8484

85+
/* Magic page flags from host to guest */
86+
8587
#define KVM_MAGIC_FEAT_SR (1 << 0)
8688

8789
/* MASn, ESR, PIR, and high SPRGs */
8890
#define KVM_MAGIC_FEAT_MAS0_TO_SPRG7 (1 << 1)
8991

92+
/* Magic page flags from guest to host */
93+
94+
#define MAGIC_PAGE_FLAG_NOT_MAPPED_NX (1 << 0)
95+
9096

9197
#endif /* _UAPI__POWERPC_KVM_PARA_H__ */

‎arch/powerpc/kvm/book3s_64_mmu.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
313313
gpte->raddr = (r & HPTE_R_RPN & ~eaddr_mask) | (eaddr & eaddr_mask);
314314
gpte->page_size = pgsize;
315315
gpte->may_execute = ((r & HPTE_R_N) ? false : true);
316+
if (unlikely(vcpu->arch.disable_kernel_nx) &&
317+
!(kvmppc_get_msr(vcpu) & MSR_PR))
318+
gpte->may_execute = true;
316319
gpte->may_read = false;
317320
gpte->may_write = false;
318321

‎arch/powerpc/kvm/powerpc.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,18 @@ int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
177177
vcpu->arch.shared_big_endian = shared_big_endian;
178178
#endif
179179

180-
vcpu->arch.magic_page_pa = param1;
181-
vcpu->arch.magic_page_ea = param2;
180+
if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
181+
/*
182+
* Older versions of the Linux magic page code had
183+
* a bug where they would map their trampoline code
184+
* NX. If that's the case, remove !PR NX capability.
185+
*/
186+
vcpu->arch.disable_kernel_nx = true;
187+
kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
188+
}
189+
190+
vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
191+
vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
182192

183193
r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
184194

0 commit comments

Comments
 (0)