From patchwork Mon Feb 19 07:47:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Yang, Weijiang" X-Patchwork-Id: 202941 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1136993dyc; Sun, 18 Feb 2024 23:56:06 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWtNDEMHsF0f4LR+KiK8u07CYPzitwFy5ldk45k6GSPK2Ah5J7HGkyupGlXExXQfXZK0knIYbFfjEyRsc4wBwzNVpdr4g== X-Google-Smtp-Source: AGHT+IFIw02sa570lWaKZoH3rpN3ZF6Q5mbgr1qsOUpIsZkItjiXNclp0H2fwzgmzass2dK6pPMk X-Received: by 2002:a05:6359:4f82:b0:179:22c:4a4d with SMTP id nh2-20020a0563594f8200b00179022c4a4dmr12087229rwb.22.1708329365854; Sun, 18 Feb 2024 23:56:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708329365; cv=pass; d=google.com; s=arc-20160816; b=q2Prm+TZq1NAwI4WjKhZx4P0m5JTA90w+e5Agol56lDNfXbgN5Mif/JYe36plZGutF af4ZPl4U1mQn3gMTkQNGTIkvGpPYtzT98fXTl1QEgUusdBrEXEfODl2VhaTHPMUDHP5Z mG3eeorEHgMb1lTVZ02wwHpdvTARvrvrCGYzbJuKL8iAAT5B2WB8QhUsK/Nobm3pDErH sN3x+PD4t3vh2etRFKjOqrhtrYvr8Hd1rYsUYfrlzttMTYkCULttxanQVX3P/hoaiAAz e+tbzE5+JqYADvzJ/wdPgNycn1L6ZCzjv7ehdMBjqVAtt3i/D51IdoFx/CPwZNJboagp kxdg== 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=MJfIg9cWFegYR/s85WX/rkC9rlN6B2g6dK5MDEnkbMo=; fh=6bYtQoKQxNJSYrJA1a9vseXH6qHZpRYO7L/7krtpXA0=; b=C6nyrPedgyMjgQeq3YI74g7Zwus9drJM9cvA2ejIVmsZavngGg8XOf7ijN7MeAfft0 Isq4+OeIWtMCHAoR2kfKINZlg70nMZ2aIikZFJJjbLVJ3FH+MLnredG9HOGL8aByNzI1 UuBji2fnr1URvvBZAMcoCkiIIsH1pus2OlKkrmJ5tHgznuhvlQze9MCQK8OegOL/H5FB PBrPB4PryNyAX0MOIvGtyxryV52GusAx/ffLI219NmrZ8QRwGPGJVLc7qJ9BbkUdZgQL yws2asC+v3bYosnS4IEnGRzJEm3vwaDmsXXNJSCDgyzUWj50XpSFVPXrHbH+anLrcoGC mzLg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hPXf6oYZ; 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-70879-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70879-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id p41-20020a056a000a2900b006e1146f8e60si4176524pfh.96.2024.02.18.23.56.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 23:56:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70879-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hPXf6oYZ; 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-70879-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70879-ouuuleilei=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A43A9280F74 for ; Mon, 19 Feb 2024 07:56:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F14BB3CF66; Mon, 19 Feb 2024 07:47:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hPXf6oYZ" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 38AC52E631; Mon, 19 Feb 2024 07:47:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708328874; cv=none; b=TgZ4jxezj7MoTXOI0Waib3vkmkpHSF+9aA1+6hJsruhqAVXLE/+iO43eKMRB6oTg5jbDvPCLEN3UPU1U6GZ2WEeeqPK8Le/Jgn71mOITYtOFYvsMZHVkQaSVLoMtT/9rcKxN469k20313CPrNV/YI/qoJ9SFEYj3/2GORYxYjuA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708328874; c=relaxed/simple; bh=FWbGGdaxtUvQxwfS23YYfiUKdvCt6zHtKkYDzi8ptYg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hx5LAu6nISnORmq/5KBZ9jKxzBq/eAK6JtsOWAUm/kegmQCoUJvkn2bQviRTtxRIwdP4Pufk2Mm9jKJQPBUSbnSpJbIn+FU1k994yXZzk0Y3exFYCDxYVTXLEg6EbdxJshz2Mn6/WWIAkc9JrlIdrBApfZ4mJseqOnPjJBKkgm8= 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=hPXf6oYZ; arc=none smtp.client-ip=198.175.65.16 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=1708328872; x=1739864872; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FWbGGdaxtUvQxwfS23YYfiUKdvCt6zHtKkYDzi8ptYg=; b=hPXf6oYZhrDP83/kM81EO1tumnevzbUDkNzC7KTpu3LuU/IBqEUanfTD 6zgoCZpHgc1prR1NcFn6N4yqrTYOh/LFpE2Tic7HvgblwgT3gbGGywxXL KIfyiMtEm88Q2bERA5pHlamltig4KTMMy9WWgoORwfITXK6l7Ne08vNnC hsjmCyMw46CnqD3MQQvYJDwzrHheBNfi+lGLjR9LY28bPasTS3MFEAj6t 0xPjaVNbqxG3zuyqkjslAamstf5UF1zASIL72NNBgsV50fCWoCdxpPKol j3OXi7fVz1uhe6hAOYvbZuLxGzfjc+u4TcSkxH/G1wi+oRggIaSpv/KRS A==; X-IronPort-AV: E=McAfee;i="6600,9927,10988"; a="2535078" X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="2535078" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2024 23:47:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10988"; a="826966090" X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="826966090" Received: from jf.jf.intel.com (HELO jf.intel.com) ([10.165.9.183]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2024 23:47:43 -0800 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, mlevitsk@redhat.com, john.allen@amd.com, weijiang.yang@intel.com Subject: [PATCH v10 11/27] KVM: x86: Add kvm_msr_{read,write}() helpers Date: Sun, 18 Feb 2024 23:47:17 -0800 Message-ID: <20240219074733.122080-12-weijiang.yang@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240219074733.122080-1-weijiang.yang@intel.com> References: <20240219074733.122080-1-weijiang.yang@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791313173288488292 X-GMAIL-MSGID: 1791313173288488292 Wrap __kvm_{get,set}_msr() into two new helpers for KVM usage and use the helpers to replace existing usage of the raw functions. kvm_msr_{read,write}() are KVM-internal helpers, i.e. used when KVM needs to get/set a MSR value for emulating CPU behavior, i.e., host_initiated == %true in the helpers. Suggested-by: Sean Christopherson Signed-off-by: Yang Weijiang Reviewed-by: Maxim Levitsky --- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/x86.c | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 5ab122f8843e..f95e93975242 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2015,9 +2015,10 @@ void kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu); void kvm_enable_efer_bits(u64); bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer); -int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, bool host_initiated); int kvm_emulate_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data); int kvm_emulate_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data); +int kvm_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data); +int kvm_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data); int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu); int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu); int kvm_emulate_as_nop(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index d57a6255b19f..39529e14ae59 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -1548,7 +1548,7 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, *edx = entry->edx; if (function == 7 && index == 0) { u64 data; - if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) && + if (!kvm_msr_read(vcpu, MSR_IA32_TSX_CTRL, &data) && (data & TSX_CTRL_CPUID_CLEAR)) *ebx &= ~(F(RTM) | F(HLE)); } else if (function == 0x80000007) { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 5a9c07751c0e..cbd44f904ba8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1919,8 +1919,8 @@ static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu, * Returns 0 on success, non-0 otherwise. * Assumes vcpu_load() was already called. */ -int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, - bool host_initiated) +static int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, + bool host_initiated) { struct msr_data msr; int ret; @@ -1946,6 +1946,16 @@ int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, return ret; } +int kvm_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data) +{ + return __kvm_set_msr(vcpu, index, data, true); +} + +int kvm_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data) +{ + return __kvm_get_msr(vcpu, index, data, true); +} + static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu, u32 index, u64 *data, bool host_initiated) { @@ -12323,7 +12333,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) MSR_IA32_MISC_ENABLE_BTS_UNAVAIL; __kvm_set_xcr(vcpu, 0, XFEATURE_MASK_FP); - __kvm_set_msr(vcpu, MSR_IA32_XSS, 0, true); + kvm_msr_write(vcpu, MSR_IA32_XSS, 0); } /* All GPRs except RDX (handled below) are zeroed on RESET/INIT. */