MIPS: KVM: Use kmap instead of CKSEG0ADDR()

There are several unportable uses of CKSEG0ADDR() in MIPS KVM, which
implicitly assume that a host physical address will be in the low 512MB
of the physical address space (accessible in KSeg0). These assumptions
don't hold for highmem or on 64-bit kernels.

When interpreting the guest physical address when reading or overwriting
a trapping instruction, use kmap_atomic() to get a usable virtual
address to access guest memory, which is portable to 64-bit and highmem
kernels.

Signed-off-by: James Hogan <james.hogan@imgtec.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Radim Krčmář" <rkrcmar@redhat.com>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: linux-mips@linux-mips.org
Cc: kvm@vger.kernel.org
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
James Hogan 2016-07-08 11:53:22 +01:00 committed by Paolo Bonzini
parent cfacaced0c
commit 28cc5bd568
2 changed files with 17 additions and 7 deletions

View File

@ -11,6 +11,7 @@
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/highmem.h>
#include <linux/kvm_host.h> #include <linux/kvm_host.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
@ -29,14 +30,18 @@
static int kvm_mips_trans_replace(struct kvm_vcpu *vcpu, u32 *opc, static int kvm_mips_trans_replace(struct kvm_vcpu *vcpu, u32 *opc,
union mips_instruction replace) union mips_instruction replace)
{ {
unsigned long kseg0_opc, flags; unsigned long paddr, flags;
void *vaddr;
if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) { if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
kseg0_opc = paddr = kvm_mips_translate_guest_kseg0_to_hpa(vcpu,
CKSEG0ADDR(kvm_mips_translate_guest_kseg0_to_hpa (unsigned long)opc);
(vcpu, (unsigned long) opc)); vaddr = kmap_atomic(pfn_to_page(PHYS_PFN(paddr)));
memcpy((void *)kseg0_opc, (void *)&replace, sizeof(u32)); vaddr += paddr & ~PAGE_MASK;
local_flush_icache_range(kseg0_opc, kseg0_opc + 32); memcpy(vaddr, (void *)&replace, sizeof(u32));
local_flush_icache_range((unsigned long)vaddr,
(unsigned long)vaddr + 32);
kunmap_atomic(vaddr);
} else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) { } else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
local_irq_save(flags); local_irq_save(flags);
memcpy((void *)opc, (void *)&replace, sizeof(u32)); memcpy((void *)opc, (void *)&replace, sizeof(u32));

View File

@ -9,6 +9,7 @@
* Authors: Sanjay Lal <sanjayl@kymasys.com> * Authors: Sanjay Lal <sanjayl@kymasys.com>
*/ */
#include <linux/highmem.h>
#include <linux/kvm_host.h> #include <linux/kvm_host.h>
#include <asm/mmu_context.h> #include <asm/mmu_context.h>
@ -330,6 +331,7 @@ u32 kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu)
struct mips_coproc *cop0 = vcpu->arch.cop0; struct mips_coproc *cop0 = vcpu->arch.cop0;
unsigned long paddr, flags, vpn2, asid; unsigned long paddr, flags, vpn2, asid;
unsigned long va = (unsigned long)opc; unsigned long va = (unsigned long)opc;
void *vaddr;
u32 inst; u32 inst;
int index; int index;
@ -360,7 +362,10 @@ u32 kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu)
local_irq_restore(flags); local_irq_restore(flags);
} else if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) { } else if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
paddr = kvm_mips_translate_guest_kseg0_to_hpa(vcpu, va); paddr = kvm_mips_translate_guest_kseg0_to_hpa(vcpu, va);
inst = *(u32 *) CKSEG0ADDR(paddr); vaddr = kmap_atomic(pfn_to_page(PHYS_PFN(paddr)));
vaddr += paddr & ~PAGE_MASK;
inst = *(u32 *)vaddr;
kunmap_atomic(vaddr);
} else { } else {
kvm_err("%s: illegal address: %p\n", __func__, opc); kvm_err("%s: illegal address: %p\n", __func__, opc);
return KVM_INVALID_INST; return KVM_INVALID_INST;