Skip to content

Commit ca7d58f

Browse files
Xiao Guangrongavikivity
authored andcommitted
KVM: x86: fix broken read emulation spans a page boundary
If the range spans a page boundary, the mmio access can be broke, fix it as write emulation. And we already get the guest physical address, so use it to read guest data directly to avoid walking guest page table again Signed-off-by: Xiao Guangrong <xiaoguangrong@cn.fujitsu.com> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 9be3be1 commit ca7d58f

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

‎arch/x86/kvm/x86.c‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4045,13 +4045,12 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
40454045
return 0;
40464046
}
40474047

4048-
static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4049-
unsigned long addr,
4050-
void *val,
4051-
unsigned int bytes,
4052-
struct x86_exception *exception)
4048+
static int emulator_read_emulated_onepage(unsigned long addr,
4049+
void *val,
4050+
unsigned int bytes,
4051+
struct x86_exception *exception,
4052+
struct kvm_vcpu *vcpu)
40534053
{
4054-
struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
40554054
gpa_t gpa;
40564055
int handled, ret;
40574056

@@ -4071,8 +4070,7 @@ static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
40714070
if (ret)
40724071
goto mmio;
40734072

4074-
if (kvm_read_guest_virt(ctxt, addr, val, bytes, exception)
4075-
== X86EMUL_CONTINUE)
4073+
if (!kvm_read_guest(vcpu->kvm, gpa, val, bytes))
40764074
return X86EMUL_CONTINUE;
40774075

40784076
mmio:
@@ -4101,6 +4099,31 @@ static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
41014099
return X86EMUL_IO_NEEDED;
41024100
}
41034101

4102+
static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4103+
unsigned long addr,
4104+
void *val,
4105+
unsigned int bytes,
4106+
struct x86_exception *exception)
4107+
{
4108+
struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4109+
4110+
/* Crossing a page boundary? */
4111+
if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
4112+
int rc, now;
4113+
4114+
now = -addr & ~PAGE_MASK;
4115+
rc = emulator_read_emulated_onepage(addr, val, now, exception,
4116+
vcpu);
4117+
if (rc != X86EMUL_CONTINUE)
4118+
return rc;
4119+
addr += now;
4120+
val += now;
4121+
bytes -= now;
4122+
}
4123+
return emulator_read_emulated_onepage(addr, val, bytes, exception,
4124+
vcpu);
4125+
}
4126+
41044127
int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
41054128
const void *val, int bytes)
41064129
{

0 commit comments

Comments
 (0)