Re: [PATCH 2/3] x86/split_lock: Refactor and export handle_user_split_lock() for KVM

From: Paolo Bonzini
Date: Fri Apr 10 2020 - 06:22:17 EST


On 10/04/20 06:39, Xiaoyao Li wrote:
>
> Â +static bool guest_handles_ac(struct kvm_vcpu *vcpu)
> +{
> +ÂÂÂ /*
> +ÂÂÂÂ * If guest has alignment checking enabled in CR0 and activated in
> +ÂÂÂÂ * eflags, then the #AC originated from CPL3 and the guest is able
> +ÂÂÂÂ * to handle it. It does not matter whether this is a regular or
> +ÂÂÂÂ * a split lock operation induced #AC.
> +ÂÂÂÂ */
> +ÂÂÂ if (vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) &&
> +ÂÂÂÂÂÂÂ kvm_get_rflags(vcpu) & X86_EFLAGS_AC)
> +ÂÂÂÂÂÂÂ return true;
> +
> +ÂÂÂ /* Add guest SLD handling checks here once it's supported */
> +ÂÂÂ return false;
> +}
> +
> Â static int handle_exception_nmi(struct kvm_vcpu *vcpu)
> Â {
> ÂÂÂÂÂ struct vcpu_vmx *vmx = to_vmx(vcpu);
> @@ -4630,6 +4647,7 @@ static int handle_exception_nmi(struct k
> ÂÂÂÂÂ u32 intr_info, ex_no, error_code;
> ÂÂÂÂÂ unsigned long cr2, rip, dr6;
> ÂÂÂÂÂ u32 vect_info;
> +ÂÂÂ int err;
> Â ÂÂÂÂÂ vect_info = vmx->idt_vectoring_info;
> ÂÂÂÂÂ intr_info = vmx->exit_intr_info;
> @@ -4688,9 +4706,6 @@ static int handle_exception_nmi(struct k
> ÂÂÂÂÂÂÂÂÂ return handle_rmode_exception(vcpu, ex_no, error_code);
> Â ÂÂÂÂÂ switch (ex_no) {
> -ÂÂÂ case AC_VECTOR:
> -ÂÂÂÂÂÂÂ kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
> -ÂÂÂÂÂÂÂ return 1;
> ÂÂÂÂÂ case DB_VECTOR:
> ÂÂÂÂÂÂÂÂÂ dr6 = vmcs_readl(EXIT_QUALIFICATION);
> ÂÂÂÂÂÂÂÂÂ if (!(vcpu->guest_debug &
> @@ -4719,6 +4734,29 @@ static int handle_exception_nmi(struct k
> ÂÂÂÂÂÂÂÂÂ kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
> ÂÂÂÂÂÂÂÂÂ kvm_run->debug.arch.exception = ex_no;
> ÂÂÂÂÂÂÂÂÂ break;
> +ÂÂÂ case AC_VECTOR:
> +ÂÂÂÂÂÂÂ if (guest_handles_ac(vcpu)) {
> +ÂÂÂÂÂÂÂÂÂÂÂ kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
> +ÂÂÂÂÂÂÂÂÂÂÂ return 1;
> +ÂÂÂÂÂÂÂ }
> +ÂÂÂÂÂÂÂ /*
> +ÂÂÂÂÂÂÂÂ * Handle #AC caused by split lock detection. If the host
> +ÂÂÂÂÂÂÂÂ * mode is sld_warn, then it warns, marks current with
> +ÂÂÂÂÂÂÂÂ * TIF_SLD and disables split lock detection. So the guest
> +ÂÂÂÂÂÂÂÂ * can just continue.
> +ÂÂÂÂÂÂÂÂ *
> +ÂÂÂÂÂÂÂÂ * If the host mode is fatal, the handling code warned. Let
> +ÂÂÂÂÂÂÂÂ * qemu kill itself.
> +ÂÂÂÂÂÂÂÂ *
> +ÂÂÂÂÂÂÂÂ * If the host mode is off, then this #AC is bonkers and
> +ÂÂÂÂÂÂÂÂ * something is badly wrong. Let it fail as well.
> +ÂÂÂÂÂÂÂÂ */
> +ÂÂÂÂÂÂÂ err = handle_ac_split_lock(kvm_rip_read(vcpu));
> +ÂÂÂÂÂÂÂ if (!err)
> +ÂÂÂÂÂÂÂÂÂÂÂ return 1;
> +ÂÂÂÂÂÂÂ /* Propagate the error type to user space */
> +ÂÂÂÂÂÂÂ error_code = err == -EFAULT ? 0x100 : 0x200;
> +ÂÂÂÂÂÂÂ fallthrough;


Acked-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>