From mboxrd@z Thu Jan 1 00:00:00 1970 From: Gleb Natapov Subject: Re: KVM: x86: ignore access permissions for hypercall patching Date: Fri, 12 Mar 2010 07:56:00 +0200 Message-ID: <20100312055600.GA20657@redhat.com> References: <1266414330-27444-1-git-send-email-avi@redhat.com> <1266414330-27444-14-git-send-email-avi@redhat.com> <4B925E66.5@canonical.com> <4B937AF5.5020004@redhat.com> <4B950542.2030306@canonical.com> <4B9505E6.1040501@redhat.com> <4B956283.10706@canonical.com> <20100311211605.GA20718@amt.cnet> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Stefan Bader , kvm@vger.kernel.org, Avi Kivity To: Marcelo Tosatti Return-path: Received: from mx1.redhat.com ([209.132.183.28]:49241 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752447Ab0CLF4E (ORCPT ); Fri, 12 Mar 2010 00:56:04 -0500 Content-Disposition: inline In-Reply-To: <20100311211605.GA20718@amt.cnet> Sender: kvm-owner@vger.kernel.org List-ID: On Thu, Mar 11, 2010 at 06:16:05PM -0300, Marcelo Tosatti wrote: > > Ignore access permissions while patching hypercall instructions. > Otherwise KVM injects a page fault when trying to patch vmcall > on read-only text regions: > > Freeing initrd memory: 8843k freed > Freeing unused kernel memory: 660k freed > Write protecting the kernel text: 4780k > Write protecting the kernel read-only data: 1912k > BUG: unable to handle kernel paging request at c01292e3 > IP: [] kvm_leave_lazy_mmu+0x43/0x70 > *pde = 00910067 *pte = 00129161 > Oops: 0003 [#1] SMP > > CC: stable@kernel.org > Reported-by: Stefan Bader > Signed-off-by: Marcelo Tosatti > My emulator patch series introduce kvm_write_guest_virt_system(). May be used it here (only compile tested). diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3753c11..9833c25 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3157,14 +3157,18 @@ static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes, return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error); } -static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes, - struct kvm_vcpu *vcpu, u32 *error) +static int kvm_write_guest_virt_helper(gva_t addr, void *val, + unsigned int bytes, + struct kvm_vcpu *vcpu, u32 access, + u32 *error) { void *data = val; int r = X86EMUL_CONTINUE; + access |= PFERR_WRITE_MASK; + while (bytes) { - gpa_t gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error); + gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error); unsigned offset = addr & (PAGE_SIZE-1); unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); int ret; @@ -3187,6 +3191,19 @@ out: return r; } +static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes, + struct kvm_vcpu *vcpu, u32 *error) +{ + u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; + return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, access, error); +} + +static int kvm_write_guest_virt_system(gva_t addr, void *val, + unsigned int bytes, + struct kvm_vcpu *vcpu, u32 *error) +{ + return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, 0, error); +} static int emulator_read_emulated(unsigned long addr, void *val, @@ -3997,7 +4014,7 @@ int kvm_fix_hypercall(struct kvm_vcpu *vcpu) kvm_x86_ops->patch_hypercall(vcpu, instruction); - return emulator_write_emulated(rip, instruction, 3, vcpu); + return kvm_write_guest_virt_system(rip, instruction, 3, vcpu, NULL); } static u64 mk_cr_64(u64 curr_cr, u32 new_val) -- Gleb.