Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2681688rwd; Sun, 28 May 2023 22:06:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6pEeiCfBtbTa+cz3MedhB2yCvFZ74niqQACLnuF1unjAJA7fKi3X69u4hViEmibAcwwc+2 X-Received: by 2002:a17:903:26c9:b0:1af:e1f4:4bf9 with SMTP id jg9-20020a17090326c900b001afe1f44bf9mr9353490plb.39.1685336771648; Sun, 28 May 2023 22:06:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685336771; cv=none; d=google.com; s=arc-20160816; b=lIe5xhUZrwt7AOejvXOr20cwUU0425d+02Ymom6i0BKxfS1UiL2ZBnCL+wR9xvagIe GvGJjrGrJ48XJOn28baCABGmQ66BnQD70DHV3oqMubLVHNbSDz8JLU5yb+b+zzdXZzMk TiaS7DNRk86rKTykWLxoXHY+K0hxvhFLWR0BH9V+F3p0+CX4DYC3apSFJH0ffvG/szhx ggHnIa/16uAyWIuoaBVjvOKXeYqHIEvu2ZWXQN2tHSxmeUtkCEljLIiNrxAo784Y/61e u4aoSv4fdwt1kvcGsL3oZjIzDoICzG1JqrkLRGaiG3V+UJR15oqKoskg1ZJjISILMi7m FR5Q== 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=ip365rBaEYtmZwCKz+hQpm1AjnYE3s5/a74Awqw2fnE=; b=GVfoBFM8STktuYXus0BgafTQ+5eDNpL2jyqD/XlNh6/J/3gvq5QhXkZMgdizKM/XAy ZNwte/d4ceLOrQFQqQiOVbC3Gh5Q5XSU7xuVNaGDYNao4Qi2ApCyYhlkxe8HCYzlik9E KnQ5O31C9Xh5iBKc+XD/DSRBUwtlOhJaeVX1KFwx7B5Ek3YROfMK2Z3HZl25iUWfrSrn gusqOWqH2VyBoLBg5Usg4XPQbqiwJMCTIOoXv1UC/zesPA6mtT9/ls59L0eregNf3w1l USscJFeH43wLH3ER1i27xu3zBMJC2d94QoZY73iVl2W7MAi0LFKRBBFCsLOudcDDgHZv KDIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lo3DNQUp; 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 s3-20020a170902b18300b001aaf62c76cesi3677089plr.129.2023.05.28.22.05.59; Sun, 28 May 2023 22:06:11 -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=lo3DNQUp; 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 S231824AbjE2E02 (ORCPT + 99 others); Mon, 29 May 2023 00:26:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231886AbjE2EZt (ORCPT ); Mon, 29 May 2023 00:25:49 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C70AF1B4; Sun, 28 May 2023 21:23:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685334209; x=1716870209; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pBXKqlxosCwO8aiKPZVYI09JnTZPIkxCJwhQQGT+T1U=; b=lo3DNQUpLy5z6idOjvcLNIS7DE7AT5YJCNkunBn9y9AgXJZOIuJ6eicz EQsURb/alAFA0saryr6Ad/rrOOnHPYf2O/FJYo1CPydC+MTFw2zTq5opT x9bqA9GjPmP0xi2CJWhRibzZ2EiUYLOBSsDG0p/dmG7xoa2flEKW3AGw7 6H6WjdwqL5KkNf7jmdY6OSU67K7y0sv5Lr3jP+aFZOTepex4rECnqeAtB VKkmzMRi/EHT2LMz5vrUFDPYvFSFZf2gh/X5ZnqEcpsvFLoDECUe53gJO MLn4mnIexTCT3pk4Gjg7lYHu1rp/6ZLSbSBe+mW+gJ2rZXRFbDgsb47Ep A==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="334966009" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="334966009" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="775784330" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="775784330" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:17 -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, Sean Christopherson Subject: [PATCH v14 049/113] KVM: VMX: Split out guts of EPT violation to common/exposed function Date: Sun, 28 May 2023 21:19:31 -0700 Message-Id: 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.6 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 | 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 eef2d3f708ca..a4ef08dfb5e8 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" @@ -5749,11 +5750,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, @@ -5768,23 +5766,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; /* @@ -5798,7 +5779,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