Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3447667rwb; Fri, 30 Sep 2022 03:52:19 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6K36/u/jEsMYO88dB0swbmOESUc4vw8oLhkv7krhUHCLkyknS+m5MJeVJH0f19DqaRb1zm X-Received: by 2002:a63:d603:0:b0:43c:6762:20d9 with SMTP id q3-20020a63d603000000b0043c676220d9mr6972214pgg.68.1664535139031; Fri, 30 Sep 2022 03:52:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664535139; cv=none; d=google.com; s=arc-20160816; b=cX/HfFbHImW1JNFGYk85W/m/C03L3eVXG4/3wfERc6S1VoCCFpFl1Uk6LdMJbwc8Zl s22AZNX1mz/9L40RTBbVQjDkcx/ZpY99xjdqXTjkI8ZGLmIhcZ1mPPJVD2gylZ7uP0y1 7F3048VyPs4FurjNjdBifHGeIMgYNuJVsDwJqUJom4qEDUZNBJi/z7peicV7WD8/htdo uYFe6tobe8dQugB9wHXm37F97iWPlWzbxl9jfq9dKMVrw/olN8Cdoe2lkCjRiKYhaJhw hHSZpLH78TR4stXt7PPnX8Gq1IxWuI9+oVNlXBK467kkjGVC7qVpgbtcsKeGAoeE7bsM kJqg== 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=hjCfPYVVQC99XdW53jQ1T8y2ItBGFgEaRIHllsyCZ9o=; b=K5/JEsRZATfwKUcFydwhq87acSDLuzQnlcu9lcwGaa3wHodPS0hPWJoqaBnSomwdj5 ujbM/Ouw6e6sHwzAgl/AwhYtoMKzGZx7pg51YFxoAx0sWOQ3Ga4z99lMhpz/KuzhHWMr AK/IysXi8AX5xqhfYNyBBacFBSSsUEKHYDLPSn39FxF6vWkLVJ3xIbCoL3nuoNtFIRgR IstDEwk8HC+ug1D++ZbGm8RSzGnBlSmKSZM8wqGpBbqj3wa1ZUrKVW+VvlsrAOWWPrBu Zjb+YEt//qzC6Ce30WRVA8lFg82tgu6CkeugvtZLl+WJKloPuURXP8HD5Qbp9MY5/3R4 Pa5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="XV71/WPz"; 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 z18-20020a170903019200b00176939b5cd9si2972145plg.578.2022.09.30.03.52.05; Fri, 30 Sep 2022 03:52:19 -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="XV71/WPz"; 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 S232482AbiI3KZS (ORCPT + 99 others); Fri, 30 Sep 2022 06:25:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231810AbiI3KU2 (ORCPT ); Fri, 30 Sep 2022 06:20:28 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97B841EAD53; Fri, 30 Sep 2022 03:19:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664533149; x=1696069149; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lNZFPSHPlAXihOU3EkAk79iSkPC71ByJOnO4Yx7xM0s=; b=XV71/WPzDfru/IXlrKONmza9i99LdzuTMViCMK6AVC9MjK+dIU+IPiB2 VctLetLc1WpaIyUFw9MyNvPcket0Cg8+6vG8Ijnv8iKArWI+Y0LT9yRri nZNL2IJ/Q1t1sxDV67bK0rhDba66clMQ/VGTU/muMo10Ec14HlcYVLTON iWLhTH3dYzE/4dSJLheTX1OdDfTqM40kgn81PCDG8S/7+qg8MA1PiYu// YAid85L88plGvnuIiGqu/Pi2OtNIwXU6qYSlNRUts49NqYpZ90Y8tVD3x DO6MGsll/dE4vPPv5/w/10RqS8feff1fT8C+WXZpPgcxGbj9LY0Yfu7z8 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="281870111" X-IronPort-AV: E=Sophos;i="5.93,358,1654585200"; d="scan'208";a="281870111" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2022 03:18:59 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="726807661" X-IronPort-AV: E=Sophos;i="5.93,358,1654585200"; d="scan'208";a="726807661" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2022 03:18:59 -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 , Sean Christopherson , Kai Huang Subject: [PATCH v9 050/105] KVM: VMX: Split out guts of EPT violation to common/exposed function Date: Fri, 30 Sep 2022 03:17:44 -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.5 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_PASS,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 | 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 f1e25e4097e1..ec1570b151f5 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" @@ -5709,11 +5710,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, @@ -5726,25 +5726,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; /* @@ -5758,7 +5739,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