Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp4118921rwb; Sun, 7 Aug 2022 15:35:26 -0700 (PDT) X-Google-Smtp-Source: AA6agR5buMyXxT7HtefPfXRbM0RrRIHhvpCcvkE/jjgVHnK0zCzT61QHIhKZ2y+8gNyHfSM2men3 X-Received: by 2002:a17:902:ecd2:b0:16e:d87f:d19e with SMTP id a18-20020a170902ecd200b0016ed87fd19emr16444342plh.75.1659911726129; Sun, 07 Aug 2022 15:35:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659911726; cv=none; d=google.com; s=arc-20160816; b=lRkoe99MIrhKei2kDIUKn4tG9rzaVPddCG9xdN6fadhVnwV2920ieRnzGnemXOIy8Z D7YNSEEQG97xCICb91DtO4Hi20biWXYeSkil/k9eBtD4bIXXWeh+h7asrIN2KExYI0IR PuPYAK3v4M7LK5Uv617vWUWW4/1DMwWCQfe9C53/kXc0XkCknr/hqQTv3IKWucs4WkRa InKzIbTsGtAtyY3IHy/vDuRPfuO3dZb4rYhaIjVGsP2JXqW7rXYjDUBNTQDf4dXb67J3 0Ag1lwxdBuhoEYL7kcfOhB/rnaA2AoALYHlk6XpCz7XRqG7h+1vXHkfvpkVixj10iv6U LZZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=yvzX0aMD5acCVukf4pbTycuagI+SKB3iwe4+ExYRHi4=; b=qN/LhcsOmICgLa0EozVddqah+6xnrC0ct26VPhMIRngjJ2aVq9cwF1nr4inQTdeSBW 8aNsHTxGxvw2wghIWy3xd/QtVqpBojUxbDcnrKY5G4bK/YPzv7/wdpoc4cATWSYh7eEr Ppm+25BCOFthD5fxvGCvu8QoF21oPVYVCi8Ihzv4ZXroBlgFJjWU+4nW/AO/4wfsneOx BIJp0monYV42b5sMv85l3XrToJKgm0zzLH8enhRO3OCFz4z3UQYBFJjihaSYFJN3eluq rdPo2kMSTEQt6Za6I8q84UZETBDFYDdKl4tRAMmAe+OEoZstQIZn+/ERKw2XChWpyGeJ pXSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BRY1zyte; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h5-20020a170902f70500b001708b184ff9si5546009plo.213.2022.08.07.15.35.12; Sun, 07 Aug 2022 15:35:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BRY1zyte; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242436AbiHGWLL (ORCPT + 99 others); Sun, 7 Aug 2022 18:11:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241875AbiHGWHQ (ORCPT ); Sun, 7 Aug 2022 18:07:16 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E0FBBC23; Sun, 7 Aug 2022 15:03:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659909799; x=1691445799; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EWCmz0YPdEkZKaztOqbl2gxRbnZsK1UUdHs5n1fDHWQ=; b=BRY1zyte576aA4AfSSbWTSazj0dO6+4MGIk5oLme1/9qTEfc8P5o1Wb8 EVy7o0QEjPP2zGhnGbRF3CZy0NsWNIJEWeATFOqrqoBVnJi2mahzoZEl2 6jgw99HqE9yVEIPeeDM8nTekk8byXwW8S2RzwaH+Dj6/ePY0dhWEhwkrc Cij+pvHU2WcyGkyM4NQz91UAaIx6bVVkVuqACp5gNwtf3Nhnc0h0sTJ31 QgxZXFsXlPGtymm382C+02Foch3S+PrGiM7SQfxqe3SHsXvuZC0vl4xQj mEpYNxPBVAFLu5kpGyHaArgilZHal2/zsPySq+fipZCE8qDu0pTxOJ1uZ A==; X-IronPort-AV: E=McAfee;i="6400,9594,10432"; a="289224144" X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="289224144" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:36 -0700 X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="663682599" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:36 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar Subject: [PATCH v8 048/103] KVM: VMX: Split out guts of EPT violation to common/exposed function Date: Sun, 7 Aug 2022 15:01:33 -0700 Message-Id: <43ba6d1e69e8379fa438fbe15f260c94e2ee8f0b.1659854790.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson The difference of TDX EPT violation is how to retrieve information, GPA, and exit qualification. To share the code to handle EPT violation, split out the guts of EPT violation handler so that VMX/TDX exit handler can call it after retrieving GPA and exit qualification. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata Reviewed-by: Paolo Bonzini Reviewed-by: Kai Huang --- arch/x86/kvm/vmx/common.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 29 +++++------------------------ 2 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 arch/x86/kvm/vmx/common.h diff --git a/arch/x86/kvm/vmx/common.h b/arch/x86/kvm/vmx/common.h new file mode 100644 index 000000000000..235908f3e044 --- /dev/null +++ b/arch/x86/kvm/vmx/common.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_X86_VMX_COMMON_H +#define __KVM_X86_VMX_COMMON_H + +#include + +#include "mmu.h" + +static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa, + unsigned long exit_qualification) +{ + u64 error_code; + + /* Is it a read fault? */ + error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) + ? PFERR_USER_MASK : 0; + /* Is it a write fault? */ + error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) + ? PFERR_WRITE_MASK : 0; + /* Is it a fetch fault? */ + error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) + ? PFERR_FETCH_MASK : 0; + /* ept page table entry is present? */ + error_code |= (exit_qualification & EPT_VIOLATION_RWX_MASK) + ? PFERR_PRESENT_MASK : 0; + + error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) != 0 ? + PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; + + return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); +} + +#endif /* __KVM_X86_VMX_COMMON_H */ diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 4a3b3dc5d4d2..3af8cd164274 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -50,6 +50,7 @@ #include #include "capabilities.h" +#include "common.h" #include "cpuid.h" #include "evmcs.h" #include "hyperv.h" @@ -5646,11 +5647,10 @@ static int handle_task_switch(struct kvm_vcpu *vcpu) static int handle_ept_violation(struct kvm_vcpu *vcpu) { - unsigned long exit_qualification; - gpa_t gpa; - u64 error_code; + unsigned long exit_qualification = vmx_get_exit_qual(vcpu); + gpa_t gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); - exit_qualification = vmx_get_exit_qual(vcpu); + trace_kvm_page_fault(gpa, exit_qualification); /* * EPT violation happened while executing iret from NMI, @@ -5663,25 +5663,6 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) (exit_qualification & INTR_INFO_UNBLOCK_NMI)) vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); - gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); - trace_kvm_page_fault(gpa, exit_qualification); - - /* Is it a read fault? */ - error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) - ? PFERR_USER_MASK : 0; - /* Is it a write fault? */ - error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) - ? PFERR_WRITE_MASK : 0; - /* Is it a fetch fault? */ - error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) - ? PFERR_FETCH_MASK : 0; - /* ept page table entry is present? */ - error_code |= (exit_qualification & EPT_VIOLATION_RWX_MASK) - ? PFERR_PRESENT_MASK : 0; - - error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) != 0 ? - PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; - vcpu->arch.exit_qualification = exit_qualification; /* @@ -5695,7 +5676,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) if (unlikely(allow_smaller_maxphyaddr && kvm_vcpu_is_illegal_gpa(vcpu, gpa))) return kvm_emulate_instruction(vcpu, 0); - return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); + return __vmx_handle_ept_violation(vcpu, gpa, exit_qualification); } static int handle_ept_misconfig(struct kvm_vcpu *vcpu) -- 2.25.1