Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp401763rwb; Tue, 25 Jul 2023 18:12:07 -0700 (PDT) X-Google-Smtp-Source: APBJJlE0ZXjC7AEnVtPCTuMv+OP1Rf2uHhh22vjYG3s1gvj/4QWbj+v3zn4DeKCyt5QL9RqRvFWD X-Received: by 2002:a17:902:f68f:b0:1bb:9212:55b4 with SMTP id l15-20020a170902f68f00b001bb921255b4mr696487plg.3.1690333927256; Tue, 25 Jul 2023 18:12:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690333927; cv=none; d=google.com; s=arc-20160816; b=pr7qQ+rF1QQCcSh5zLKQ0qYJTUeCiBx2oDi8Q1XW2WUoK1rXFjjCK46Vm/RTHoDKbf /JRXJ6Rz411K+OCqv1J5rxhhM9nXb926O/+03SKOBH+R+FYNAsyAvdB0KuUwfErudNma GCeCLkJEVBptYc5V8ldxRGrssyF6hPcKz06bTAIssVRNe7e9UzVtfjZ1IPd/zNdXiCYd l0A1GV7AYLKbhW4uEXl084BsnJVNXlVNN0TAJRGTwyAjvyBbNZWI1QKfUMhJy0Rja6Gf vStxYcajQQacVvtlYsqfvMPE0JLrqOnOIhC+DspW53oJU2ZEeOWDKoOgY59AekWGZjhi +4Mg== 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=6tMNQk/4wsSdolLKVvVHSOmSKP2apwzj8t5RTjCsZhs=; fh=aHvdLX1Pa61KvQFmeZODM982ht845x7CZ22gIB9kxIo=; b=CbvfmBY2OWlfUylQRKRXP/cNaNBYQOc/7qrdq+YQW78FWwXhlCZVJXkYqGXpbvx34R VKPXWFGwaFxOveSq24foJm9uRp2tWEVGUdKSpQEgYRHxGyv9/MT2bhl2+beIc8wpF8la A0Q9KqGPeJUmA5rCDgAlODx2jJSw5y7OqKkl7qG9Zj2s1fapROWWnU/dD78eVi1AuP3p TFA0q1Q8n9lj8jmiu0O3As558D+9kpueTa2oU/lShKz1a8vKsSGxGtwYSId8OFdDDrM6 bT+eh706jEPubuCMsD8Z+Eg/cubA+RmbJvf8giT/ZCXvf07HJ517W5shYJ417WZ0eTIx izQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=R66V2Wtg; 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 u16-20020a170902e81000b001bb2845bfeasi13581798plg.12.2023.07.25.18.11.54; Tue, 25 Jul 2023 18:12:07 -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=R66V2Wtg; 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 S232592AbjGYWVi (ORCPT + 99 others); Tue, 25 Jul 2023 18:21:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232502AbjGYWUD (ORCPT ); Tue, 25 Jul 2023 18:20:03 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DB4049D3; Tue, 25 Jul 2023 15:17:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690323426; x=1721859426; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bIvss2mZaMxkdM4kifawdK2jvwpOiCxUFAgl88Gf7Bc=; b=R66V2WtgvH4LWqv01e1tsgbEHQyIUMuYKtTd8EUxE/+gF1q0GFUyWZtP WhlmQfirTjNzSmwDFQ/f/1IpGWFpJLwd42V6Rn4KpHHSQ1YPqZh4K7NIv NP+94YqC/AsdW9IB4fK0C26oSlJEf73HDJOqgwcXBHeCP3CjC/dptne4g 3YuhanMoyRIyRmV5f0N40R+DEIlPaEnKa30Da+Nf/tZLOmxY0LQet4dur pPE85eIVzniM+4QDn7olX256CossQM7KT54tMMCv8ATTcufiYz+XaB1bq DWETeCCM8Y1qARAYEadnsNsApMI7Fg6B1HZzFgvz1cvowHCledbNwGFKc g==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="357863293" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="357863293" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="1056938947" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="1056938947" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:40 -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 , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Sean Christopherson Subject: [PATCH v15 049/115] KVM: VMX: Split out guts of EPT violation to common/exposed function Date: Tue, 25 Jul 2023 15:14:00 -0700 Message-Id: <1a2e839aeabc0663551504c93a5513bf09f0e953.1690322424.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, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 c9020e751f69..408c155f8566 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" @@ -5752,11 +5753,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, @@ -5771,23 +5769,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; /* @@ -5801,7 +5782,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