Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp558371rwb; Thu, 12 Jan 2023 09:17:53 -0800 (PST) X-Google-Smtp-Source: AMrXdXsOP2KgllQY4d9+lx3K5m2cXKEbfot2tD6DCqtlioF1RAL/cDBMgMQ4ohSMmWPu74Q4LOr5 X-Received: by 2002:a17:907:899a:b0:864:1902:6f3c with SMTP id rr26-20020a170907899a00b0086419026f3cmr3810619ejc.38.1673543873662; Thu, 12 Jan 2023 09:17:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673543873; cv=none; d=google.com; s=arc-20160816; b=XW5iknU2f4Mmcck0bj+4oILDYKUhydG+cxOzy0SolwbzW31mE6VsKBCN5nyX3O5xR5 LcSVYwtrJ+brdXMHsq+UC6S7nYHfXsW5c6SgFRJsXXjzKhQKzmlxrtu5FiQzcu7iQAlP pZQR/qdJp/fj+W3TLIU6Mn5CQA4Xa8RbAyHfHnqbJ5jt7uiM/XkEg9/XWBxkks8rZyAd CP0KyhlRpFN8KOzzvMdxQBMVJUrX0sHHZF7PgymBdxW0gBbBY+9l5z1XquVszQamDNd/ 0GQ+FQDDtvGSDfttdj5RNZz82kp0S3Wqx2+CFwmgMm6GDuT8PdwP0++eAloKhkZ09H00 jjIw== 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=2ADj2bjbiZr/4bzwKKo2o+U/g+zKMTF0Grg4B5dMAP8=; b=clTSdLEBWR6cTO0v1Y/ERs1R/NL++UBYIp1ky5oYsLNV4oHxVCHezpnjaFLj6tS3lv H0bmO2yuiXq4tQdOpbWcGq783MX/C7Pg/Inb5imiP6k1rHzy/4DJ0MhBl3KKvBWBjKjZ 3dIpoReFg3y4M39aqsIw7ccmIieYpD4brERdCbX8cay+4VVMcCkjm9TnDbGjfPVuR1Qk jxdDZnIyC/hK3Ndhy3LJLfC+sETjl1KfA1zS9WcfcolNz/xM/KQwF0D9h6WPCuu91Bue mXgqe2lzT+Z6VPP7yb4Kqpg0Dv6UT/4Nc9S8XpPkyDOpXOXQHbF8wQnuosJ2gHYhgVSf H7HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VEUavOcW; 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 gb12-20020a170907960c00b007c1633cea0csi19921464ejc.513.2023.01.12.09.17.41; Thu, 12 Jan 2023 09:17:53 -0800 (PST) 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=VEUavOcW; 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 S240596AbjALQq5 (ORCPT + 50 others); Thu, 12 Jan 2023 11:46:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240208AbjALQie (ORCPT ); Thu, 12 Jan 2023 11:38:34 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 494A117887; Thu, 12 Jan 2023 08:34:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673541244; x=1705077244; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QDh2AR6jvp118flE4hZndFVqBtz1aej8vQtt9zThemM=; b=VEUavOcWfIpBLJ8GSVtu0TVH2ZMvptkI2i2RiQ1+6HL6X/+bHMQD4z6z vcPAnVbfjk2jC5S3w25QONS3PhfI9OA2ZNU1IyRWZ6C5s5WbHGUgkOtdV xaCqFA64dqk2wb+AAwCF6Q7I+05QL/gYvwa+dwv7yfXP7ZOpWZl83N2f+ ziDqJLswOIEvKueKIcYxMzAi+SCYtfcYf1G8OXKo4uJWp+qdtEP7g2MMw ZBWwULUlWLRKs9JtvD9k+MckY+9fjbD7VeaNzLkPwY6sxfbgr5Gj9XHUn XO1bc+LpijkMVdgobIhJiHQowRLaEQe2dvhreOYVtoxasA7Z1whiDGbu0 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="323811899" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="323811899" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:28 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="721151811" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="721151811" 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; 12 Jan 2023 08:33:28 -0800 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 , David Matlack , Sean Christopherson , Kai Huang Subject: [PATCH v11 051/113] KVM: VMX: Split out guts of EPT violation to common/exposed function Date: Thu, 12 Jan 2023 08:31:59 -0800 Message-Id: <5b3a11bfe43dc4dcc8ab75ded6574a26d152e152.1673539699.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=-4.4 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 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 | 25 +++---------------------- 2 files changed, 36 insertions(+), 22 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 0df044357e09..6394c1241374 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -51,6 +51,7 @@ #include #include "capabilities.h" +#include "common.h" #include "cpuid.h" #include "hyperv.h" #include "kvm_onhyperv.h" @@ -5791,11 +5792,8 @@ static int handle_task_switch(struct kvm_vcpu *vcpu) static int handle_ept_violation(struct kvm_vcpu *vcpu) { - unsigned long exit_qualification; + unsigned long exit_qualification = vmx_get_exit_qual(vcpu); gpa_t gpa; - u64 error_code; - - exit_qualification = vmx_get_exit_qual(vcpu); /* * EPT violation happened while executing iret from NMI, @@ -5810,23 +5808,6 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); trace_kvm_page_fault(vcpu, 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; /* @@ -5840,7 +5821,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