
euleros inclusion category: feature bugzilla: NA CVE: NA When a guest accesses a mmio address and a trap is raised to be handled by KVM, KVM checks whether the mmio address access is operated by kernel. Link: https://gitee.com/openeuler/kernel/issues/I1SQJ2 Signed-off-by: Yifei Jiang <jiangyifei@huawei.com> Signed-off-by: Mingwang Li <limingwang@huawei.com> --- arch/riscv/kvm/vcpu_exit.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index f3a3acdc8..57563c433 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -191,6 +191,8 @@ static int virtual_inst_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, static int emulate_load(struct kvm_vcpu *vcpu, struct kvm_run *run, unsigned long fault_addr, unsigned long htinst) { + int ret; + u8 data_buf[8]; unsigned long insn; int shift = 0, len = 0, insn_len = 0; struct kvm_cpu_trap utrap = { 0 }; @@ -275,10 +277,21 @@ static int emulate_load(struct kvm_vcpu *vcpu, struct kvm_run *run, vcpu->arch.mmio_decode.len = len; vcpu->arch.mmio_decode.return_handled = 0; + run->mmio.is_write = false; + + ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_addr, len, + data_buf); + if (!ret) { + /* We handled the access successfully in the kernel. */ + memcpy(run->mmio.data, data_buf, len); + vcpu->stat.mmio_exit_kernel++; + kvm_riscv_vcpu_mmio_return(vcpu, run); + return 1; + } + /* Exit to userspace for MMIO emulation */ vcpu->stat.mmio_exit_user++; run->exit_reason = KVM_EXIT_MMIO; - run->mmio.is_write = false; run->mmio.phys_addr = fault_addr; run->mmio.len = len; @@ -288,6 +301,7 @@ static int emulate_load(struct kvm_vcpu *vcpu, struct kvm_run *run, static int emulate_store(struct kvm_vcpu *vcpu, struct kvm_run *run, unsigned long fault_addr, unsigned long htinst) { + int ret; u8 data8; u16 data16; u32 data32; @@ -384,10 +398,20 @@ static int emulate_store(struct kvm_vcpu *vcpu, struct kvm_run *run, return -ENOTSUPP; }; + run->mmio.is_write = true; + + ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_addr, len, + run->mmio.data); + if (!ret) { + /* We handled the access successfully in the kernel. */ + vcpu->stat.mmio_exit_kernel++; + kvm_riscv_vcpu_mmio_return(vcpu, run); + return 1; + } + /* Exit to userspace for MMIO emulation */ vcpu->stat.mmio_exit_user++; run->exit_reason = KVM_EXIT_MMIO; - run->mmio.is_write = true; run->mmio.phys_addr = fault_addr; run->mmio.len = len; -- 2.19.1