Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1185343rbb; Mon, 26 Feb 2024 01:06:42 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXePps15TE4DUy2x+D6PRuTKISRSloIuVTj5M4PJi1FHQrDnEYM4Bn79Q9o+Ixz4RJOTuurPmxe0/8xlhZOmmh7zcQX6qXTVbm7P6q5fg== X-Google-Smtp-Source: AGHT+IFjOJB0RF5o/NxRO99nyvSOKhP8DOJYDQzGnci3X8lHnXePaqTuq0VCZFk42evI+Jpk6fDE X-Received: by 2002:a17:902:f709:b0:1dc:a8aa:3c80 with SMTP id h9-20020a170902f70900b001dca8aa3c80mr1154177plo.43.1708938402094; Mon, 26 Feb 2024 01:06:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708938402; cv=pass; d=google.com; s=arc-20160816; b=yVJ47VlLM7QA/jzeysR8Ue4eqTyZz/AvDEO+EU9On5+cR4rXQN6UEq+qo+vsLDhtkW w5jnJi3hu8125bSwSCpSCGN9wywNMLvcBXEPYkLb24z5wXG7Qlfr6+CC1tr2bUMYWVpy CYh9eSEeROWOxpjfjoQE0948bqpV3fASYLsKszAX3NIuxorduqZOvQX/Bn8X8PcZOhPZ aVYA8dwBryughd5gw5o9m5cqNhXi75S0+9sW4gu/kbW3WtT0d+YS24zLjUbxWI/2M3/P 1vWUihy744SS/RlKUkaUPA000sOkZyyTKYxxgW96KaJG4p+Ev4i97GHDGx6J/j+FgIbh 5wxQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=mXsOIIPSohGsoCFdbpSg5V/Ij/qcraVM6vethKcmn2A=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=qfDwpzkdAexJgAFXiULD1AgLaP7AZLYmblR8iF5ZxpTtihWf0c+bbSEkvdUEFzBbcu E1gvQsFr2dzLlytEsrz8b0DpQEe87Df3jNflCfDFCZoMflxCWCtE5d1Xi8Qt7cZGJppC N4pH1QbeoNeZwqnAv+WcNtQ1vh5ZN1RZp8K7mWn/iGR+bdaqAXI6aYkSSCa/gGfiigMd mKH1ZG/eAsvEwUREFyYxSy6RTr2p9rJ3p8+IOpRJoaOpHyHh4xWjQJh8EVBC/Wa9mLgs 27DlzTGmQFd9LeAP/pdoCTBvNYrGBp9zFPubdJsNIgEZyBolwI4JaugR0jSwqW8BxCrt l2Pg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WHGHP071; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80833-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80833-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id u7-20020a170902e80700b001dcabfa61absi479998plg.164.2024.02.26.01.06.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 01:06:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80833-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WHGHP071; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80833-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80833-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 3B285B2351C for ; Mon, 26 Feb 2024 08:50:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D65167E80; Mon, 26 Feb 2024 08:28:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="WHGHP071" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E785634FA; Mon, 26 Feb 2024 08:28:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936112; cv=none; b=f5rTPIZRpCTfbZGmWOtDRsYuvzICNNOiG9jlH8DHm9o69lR3enp+9HQGYwW+uAUuZZiQQKaRrJfdqTz8SSxugZz1HuMzOOTYHPshX6tIDw1B0HjzOvcehx6BAeLIH3A8ZpILVpsoPUqCxDB75s0+XwYguTxUT8EkZyMsQv4QN18= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936112; c=relaxed/simple; bh=XCouPvykKRbiuUVkgxuTWRQsNvpWFhGpyLsG7vsW4vY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rdYWeHrXR85bPYeVfHHxV8lb3FXv202rb5OwtUe6EyKsTTaFcjbVfz5e1596QTvNc86bbkytNWDIFPfSs+JqqdvBLY7GTm6mraMZF+LIlD+s/OtfmQyc2iOYEFQwDlicLMEoBEzZk/9n8+0Fm3oBSKLZweIzoGHkk7A4MiPcoYA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=WHGHP071; arc=none smtp.client-ip=198.175.65.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708936111; x=1740472111; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XCouPvykKRbiuUVkgxuTWRQsNvpWFhGpyLsG7vsW4vY=; b=WHGHP071EefHT2T9wCscumgBTW2PK8o/Oijrcj7hNLrmJLYGJVoSMhwy tBgc6pyPcbgW3IuAWyYyh6JEl96okGAkoXzu5/meJPlx2TUCavXmIbcZ+ pVjrw/CvdCzxMWpF9fZKM6RY1Lnvx7uhXv3HTph/wExytsgcW0EhPzWae ghTaF90IG/jBA2en0Y2n8/7FFuG6Q7frtUmeXeTjG4JgxjAzkOOXR8IP9 juG7jpTucfm3tjepEWUCLTdrrHpvv5Cxk9a6Owfz+8eyIYUwioSW5KNbe 8dJkq9r1uqrGbLo3oxjS5uLbaRGlYOVBBQQyALk/LKd55+FUKkoUCWLD3 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="3069469" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="3069469" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:30 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="11272440" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:30 -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 , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v19 071/130] KVM: TDX: MTRR: implement get_mt_mask() for TDX Date: Mon, 26 Feb 2024 00:26:13 -0800 Message-Id: <1502930eaf250202d01a1707949a2eac9682fec4.1708933498.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Isaku Yamahata Because TDX virtualize cpuid[0x1].EDX[MTRR: bit 12] to fixed 1, guest TD thinks MTRR is supported. Although TDX supports only WB for private GPA, it's desirable to support MTRR for shared GPA. As guest access to MTRR MSRs causes #VE and KVM/x86 tracks the values of MTRR MSRs, the remaining part is to implement get_mt_mask method for TDX for shared GPA. Suggested-by: Kai Huang Signed-off-by: Isaku Yamahata --- v19: - typo in the commit message - Deleted stale paragraph in the commit message --- arch/x86/kvm/vmx/main.c | 10 +++++++++- arch/x86/kvm/vmx/tdx.c | 23 +++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index 8c5bac3defdf..c5672909fdae 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -219,6 +219,14 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, vmx_load_mmu_pgd(vcpu, root_hpa, pgd_level); } +static u8 vt_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) +{ + if (is_td_vcpu(vcpu)) + return tdx_get_mt_mask(vcpu, gfn, is_mmio); + + return vmx_get_mt_mask(vcpu, gfn, is_mmio); +} + static int vt_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { if (!is_td(kvm)) @@ -348,7 +356,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_tss_addr = vmx_set_tss_addr, .set_identity_map_addr = vmx_set_identity_map_addr, - .get_mt_mask = vmx_get_mt_mask, + .get_mt_mask = vt_get_mt_mask, .get_exit_info = vmx_get_exit_info, diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 39ef80857b6a..e65fff43cb1b 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -393,6 +393,29 @@ int tdx_vm_init(struct kvm *kvm) return 0; } +u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) +{ + if (is_mmio) + return MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; + + if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) + return (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) | VMX_EPT_IPAT_BIT; + + /* + * TDX enforces CR0.CD = 0 and KVM MTRR emulation enforces writeback. + * TODO: implement MTRR MSR emulation so that + * MTRRCap: SMRR=0: SMRR interface unsupported + * WC=0: write combining unsupported + * FIX=0: Fixed range registers unsupported + * VCNT=0: number of variable range regitsers = 0 + * MTRRDefType: E=1, FE=0, type=writeback only. Don't allow other value. + * E=1: enable MTRR + * FE=0: disable fixed range MTRRs + * type: default memory type=writeback + */ + return MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT; +} + int tdx_vcpu_create(struct kvm_vcpu *vcpu) { struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm); diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index d5f75efd87e6..5335d35bc655 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -150,6 +150,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp); int tdx_vcpu_create(struct kvm_vcpu *vcpu); void tdx_vcpu_free(struct kvm_vcpu *vcpu); void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event); +u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio); int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp); @@ -178,6 +179,7 @@ static inline int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { return -EOP static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; } static inline void tdx_vcpu_free(struct kvm_vcpu *vcpu) {} static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {} +static inline u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) { return 0; } static inline int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp) { return -EOPNOTSUPP; } -- 2.25.1