From patchwork Sun Oct 30 06:22:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12872 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1666222wru; Sat, 29 Oct 2022 23:29:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5yu2Rrce8exWBgDYlynbgkxZs4KA/oc01vnR2WIdsMFaRgypdQm2lIn3AhcULApO4v4fFd X-Received: by 2002:a17:90a:ad83:b0:213:9c65:c2b5 with SMTP id s3-20020a17090aad8300b002139c65c2b5mr10169479pjq.137.1667111366000; Sat, 29 Oct 2022 23:29:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667111365; cv=none; d=google.com; s=arc-20160816; b=rmp48bBGCikkmfXzum4NV3qVhMu1nfUtWbUdGoQpUDSo+BK+svOHtVZJW3Bm3rzQ+L avzCrdEp98IuAAmQlm7usgnF8iPeEVdt5U2TJ3G5gBSdHOm2spOzmazCmo2bnpYM44Ig j5BSpac84sW6fA/7vvt0FwtlSq9fd2HA5rvTFtLORIlPXj9kL+MmRdroGM6xWYw3kpuP 5q0fbqPyw+x0dSL6ha2GgPYxGyLSMlPyfDMqK9lB4F8nJKPWqie5vomtzUDhjYkSxh7+ 5suKS8ZT02Oy9k0lsewhCpJSUC7DepD4rj/Tbw4mfMtk/wPuTVgcbx+aZg1ZmxgPA23V milQ== 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=XqxAufM38FR/xrAV6R5pHhZEYOuuZnHo1AJoZLrk2eo=; b=eveLIDL2bsRV1wNjV8nM0vXsewYTe5TWD/xZSUHCGXagfyTMAIFekFchk3ziCiadss aIHtxgjCBzdXIhnzK4xFHVnKWm+Oo7SkS++97ttRcPFrZv2Cq7YMXnwTpRPZ78XkJ3mJ DYVWpXohJ3BvyNqWjI8f8MYXdQEBeEh6y6OGo3Zufi9lzklEE5r3s9778c053qCtVeN/ UxpUw4bScyKIxkZpr6SjQz2F77mOG8fTV455l5g3v6Ckm7dsEgntgTI5z0yZgEBOHv8d LYyhAKB8Qf45nwCAoE5kG0oCTz5M/uM6EmBkk3u3Cd27qM8roWRrcoW/2ysvGNTOWdKT zypQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=F5mSZl1O; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ji21-20020a170903325500b0017881dc6dc2si3603212plb.489.2022.10.29.23.29.12; Sat, 29 Oct 2022 23:29:25 -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=F5mSZl1O; 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 S230123AbiJ3G2Y (ORCPT + 99 others); Sun, 30 Oct 2022 02:28:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229971AbiJ3GY2 (ORCPT ); Sun, 30 Oct 2022 02:24:28 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA8521BA; Sat, 29 Oct 2022 23:24:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667111052; x=1698647052; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6sZcvskGCywxirHGHxxQ+Pk8Bh2FU/RqVrVJqY8gqEY=; b=F5mSZl1OAqwP3GbkVcxILdSgvvV/JcJZjgFU7Lfvrq1WUuE27W+4bCVX Achf1gEEzqRtpkxTwwf6FS2WSZJV9DJ0gubq6C7Z6vPhwkI8fakbgrsSN zTMmyPfXtOwPdDrZWzXXk9Y+edjaQQtI3pbHB1gZcFPlSedgvv/z65Afb exST86diaSVcfFmEY3dNguje4FgRVNOeJsxREIRQ4IgLi1lGzLvUWQJsq /XmuoDemgW1D1oIG3Gh4+qJVe1w7qlvAGzpsrWqdYGzvJZsm79sFxrLcG ErVeWe9xxbbrWb41T0kQARZ8AtTqlvMv3gaFNtH08XxReYqrqT+B8qQFp g==; X-IronPort-AV: E=McAfee;i="6500,9779,10515"; a="395037166" X-IronPort-AV: E=Sophos;i="5.95,225,1661842800"; d="scan'208";a="395037166" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Oct 2022 23:24:06 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10515"; a="878393017" X-IronPort-AV: E=Sophos;i="5.95,225,1661842800"; d="scan'208";a="878393017" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Oct 2022 23:24:06 -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 , Sean Christopherson , Kai Huang Subject: [PATCH v10 053/108] KVM: VMX: Split out guts of EPT violation to common/exposed function Date: Sat, 29 Oct 2022 23:22:54 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748092967844877390?= X-GMAIL-MSGID: =?utf-8?q?1748092967844877390?= 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 dd3fde9d3c32..2ff7af959e30 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" @@ -5702,11 +5703,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, @@ -5721,23 +5719,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; /* @@ -5751,7 +5732,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)