Re: KVM: x86: ignore access permissions for hypercall patching

Stefan Bader
2010-03-11T21:22:56Z

With this patch applied on top, I was able to boot my guest on a AMD host system.

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: [<c01292e3>] kvm_leave_lazy_mmu+0x43/0x70
> *pde = 00910067 *pte = 00129161
> Oops: 0003 [#1] SMP
>
> CC: stable@kernel.org
> Reported-by: Stefan Bader <stefan.bader@canonical.com>
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Tested-by: Stefan Bader <stefan.bader@canonical.com>
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 703f637..bf5c83f 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -3253,12 +3253,17 @@ int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
> static int emulator_write_emulated_onepage(unsigned long addr,
> const void *val,
> unsigned int bytes,
> - struct kvm_vcpu *vcpu)
> + struct kvm_vcpu *vcpu,
> + bool guest_initiated)
> {
> gpa_t gpa;
> u32 error_code;
>
> - gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code);
> +
> + if (guest_initiated)
> + gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code);
> + else
> + gpa = kvm_mmu_gva_to_gpa_system(vcpu, addr, &error_code);
>
> if (gpa == UNMAPPED_GVA) {
> kvm_inject_page_fault(vcpu, addr, error_code);
> @@ -3289,24 +3294,35 @@ mmio:
> return X86EMUL_CONTINUE;
> }
>
> -int emulator_write_emulated(unsigned long addr,
> +int __emulator_write_emulated(unsigned long addr,
> const void *val,
> unsigned int bytes,
> - struct kvm_vcpu *vcpu)
> + struct kvm_vcpu *vcpu,
> + bool guest_initiated)
> {
> /* Crossing a page boundary? */
> if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
> int rc, now;
>
> now = -addr & ~PAGE_MASK;
> - rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
> + rc = emulator_write_emulated_onepage(addr, val, now, vcpu,
> + guest_initiated);
> if (rc != X86EMUL_CONTINUE)
> return rc;
> addr += now;
> val += now;
> bytes -= now;
> }
> - return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
> + return emulator_write_emulated_onepage(addr, val, bytes, vcpu,
> + guest_initiated);
> +}
> +
> +int emulator_write_emulated(unsigned long addr,
> + const void *val,
> + unsigned int bytes,
> + struct kvm_vcpu *vcpu)
> +{
> + return __emulator_write_emulated(addr, val, bytes, vcpu, true);
> }
> EXPORT_SYMBOL_GPL(emulator_write_emulated);
>
> @@ -3997,7 +4013,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 __emulator_write_emulated(rip, instruction, 3, vcpu, false);
> }
>
> static u64 mk_cr_64(u64 curr_cr, u32 new_val)