Received: by 10.223.176.5 with SMTP id f5csp2476425wra; Thu, 8 Feb 2018 15:00:22 -0800 (PST) X-Google-Smtp-Source: AH8x227H58SFNlGjW12lu5Xx70/fuwZaV7ekitPlYCWEWrJ7Z73dkKiWMN82le8oNS2XmXngaX+r X-Received: by 10.99.151.2 with SMTP id n2mr585367pge.87.1518130821909; Thu, 08 Feb 2018 15:00:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518130821; cv=none; d=google.com; s=arc-20160816; b=LxmAe2+CvtKcUkuBHnRNRS6GOsRcQcFs5Ssv4+vSfZRjffoxkyZYclgotOkzkFhL+w Y+a//IWS+pp/FoMemmzxqEytp5WK1i88biaFcLugxeSddasbsqRaSU+ew1AXUyqWWGak E3fsQcBZ5EmVTtqzyRZgw5pLWOwU4rmCGRJPONLkStSBsM25/oxt2MuwHdkzbRGPYBd7 1sSLHGPfNbAP/rwtkUN9tON8EZ7PPb6B60bIsWgDvmWbChlLz8mjpS8cIj8X9XkCWnME xbylcVnWeKHkFO1djljgXb3Tx5JHvA18OeG7xznul/Ok96jTjbLX8ShFPFw/Bru9Dzum WHWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:subject :from:dkim-signature:arc-authentication-results; bh=ZO5gfamCFRGflK/MbsoaW6ghdyNdm6ZlF6u/TKElexQ=; b=N7p5DYDumcVP3Z7NajIFhC2h618iDGKY8xVyMDkVNR9DoGelJzPuwpyevbvr2jUudV ipjJyOsqVumpIntplNhTGEC6sazCrcVZ5v6qaiY+x88MKK+ny+iSLvzZ3Zy6lcAq7GkF mnwr8XYMnX408Z1deVTabUTw9YUEC+YVbLZqXlLRMNopLzFC/gzbYo3fEVkixpl14CXP jUWtEKgm3AE/fcI5k6VYu6WySI1ziUf39t5kx8tV3owPY2qXw6EI4QR1sNHmhJsBRzwv Qlo3AM5YkzGkoE8mHnqdHcK1FzGFBDe78ZjtK7Fgw73ghD18uM7UY+YB+CDkN5ZS1mx8 q5hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector1-amd-com header.b=fv+sLU9I; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z10si531242pgo.495.2018.02.08.15.00.06; Thu, 08 Feb 2018 15:00:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector1-amd-com header.b=fv+sLU9I; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752439AbeBHW7A (ORCPT + 99 others); Thu, 8 Feb 2018 17:59:00 -0500 Received: from mail-bn3nam01on0081.outbound.protection.outlook.com ([104.47.33.81]:51584 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752372AbeBHW6y (ORCPT ); Thu, 8 Feb 2018 17:58:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=ZO5gfamCFRGflK/MbsoaW6ghdyNdm6ZlF6u/TKElexQ=; b=fv+sLU9Ic7JkqoTjG9TOlNesugigeqITbh1WJZL1VVadCvpORJpZMGHNtMoIUHwkv0QHVqzwIct8bTx1jeA3h9VE3DeDFiSpvOVdZqPS/Ly7Ud6iB9W3jEd4XZYwe4eMuTGkHK1iTqkykdU2pv6e6U0xy5UhJUTX8g4+JKYVKz0= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; Received: from tlendack-t1.amdoffice.net (165.204.77.1) by CY4PR12MB1144.namprd12.prod.outlook.com (2603:10b6:903:38::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.464.11; Thu, 8 Feb 2018 22:58:50 +0000 From: Tom Lendacky Subject: [RFC PATCH 1/2] KVM: x86: Add a framework for supporting MSR-based features To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Joerg Roedel , Borislav Petkov , Thomas Gleixner , Radim =?utf-8?b?S3LEjW3DocWZ?= Date: Thu, 08 Feb 2018 16:58:46 -0600 Message-ID: <20180208225846.22074.70944.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20180208225833.22074.25995.stgit@tlendack-t1.amdoffice.net> References: <20180208225833.22074.25995.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BN6PR1201CA0011.namprd12.prod.outlook.com (2603:10b6:405:4c::21) To CY4PR12MB1144.namprd12.prod.outlook.com (2603:10b6:903:38::8) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 64a24e69-72f3-4392-4303-08d56f47855a X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(4534165)(4627221)(201703031133081)(201702281549075)(48565401081)(5600026)(4604075)(2017052603307)(7153060)(7193020);SRVR:CY4PR12MB1144; X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1144;3:a0F0lKmcX7kI+cL/JRyns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ybJ1Ros/U4Cnc30aWiKYsGhQESQ7yklL8OlVHJZ26b0hHnlsO7e8sCAww8GP1njwAz74N48g3lyTkcRyOl/w9ADx+WpaspvBHdDlEzyte7zCOP/1c7Mlfd5HxzNOTGBoWBvdBnqEbbE7oDqW0uLf6VUKd0VEtuU1GPkb20jGk/gVURFfVqV4p+mmQMIhQovR250mKV76m9ejc66F+1saAc1H6aqhPcl0apnxyxS3R6zR2oPUfTdY66ZP6auDrgr8uQgSo3IwA9uK0UIyWfx4CDMpAqOUDVYS/EokzdUkjIeaDphBTnNK05gC+09VqOhM7OG2uKO7ErUJ3b2oQ6b1ce6/JFk1Ge7nAD3;4:0e89lsCO+JlFmwqviAI8Ee1XBka+ftyAvPWo6cmXMbxPKAj/rzywqKMLPmxhNh/ofAUIYmjPDbIAxFEJTk/qK7zku7DwRZkP0WN/+jbn1HZKX1Umqkn6Bt5kFaVUy47ySDDplmrHVgpgmQdgcjK1DFPsP1zo0PrTRznfMvsTyRnLWWmGJX9ChETgrt/jRsyDmi0q1gGQ97H2XC/NVzw4Pw8y9Ra4Ac9NHs6tyqSJfK5SM7LOkHoBcEYX6PMVQK/MW/tDX82QvPIfMzBy5NktYOHrdafbjADRpPQUU7lmSuGPbA2BXJJBIp5cH5q8YMOk X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040501)(2401047)(5005006)(8121501046)(3002001)(10201501046)(93006095)(93001095)(3231101)(2400082)(944501161)(6055026)(6041288)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123562045)(20161123564045)(6072148)(201708071742011);SRVR:CY4PR12MB1144;BCL:0;PCL:0;RULEID:;SRVR:CY4PR12MB1144; X-Forefront-PRVS: 0577AD41D6 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(346002)(39380400002)(376002)(396003)(366004)(39860400002)(199004)(189003)(52116002)(7736002)(6116002)(97746001)(69596002)(2906002)(4326008)(55016002)(83506002)(25786009)(7696005)(305945005)(9686003)(72206003)(53936002)(478600001)(76176011)(3846002)(1076002)(86362001)(105586002)(106356001)(23676004)(316002)(97736004)(2486003)(81156014)(230700001)(2950100002)(6666003)(68736007)(50466002)(81166006)(66066001)(47776003)(8936002)(103116003)(16526019)(8676002)(186003)(386003)(26005)(5660300001)(6506007)(58126008)(59450400001)(54906003)(575784001)(53416004)(217873001);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR12MB1144;H:tlendack-t1.amdoffice.net;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTRQUjEyTUIxMTQ0OzIzOmE5dE1IQWk0ZWxLWW9ES1FBcFpVQUptd3ox?= =?utf-8?B?RmZjZGt4czhRTDlnclg3UmsrWjlDUmRBditoSFRuZzVDZXlmNEd6TXdvMUhw?= =?utf-8?B?bVlhK01ZTnpFQWhyOXI2UXk2dm91eXFoMlFnY2JVSDJmU1ZjdS9pbkozNHhk?= =?utf-8?B?R3d4L2ZaSTR4ZUxObHlTQzZuYzYyM04xU0p2dkhiS0QyUkdCcDc1V1BIQjVi?= =?utf-8?B?VlRDWE9VdTMwYXhpY29CTWJ6bVNDa0JzNW5FVDZVWk9xT2J4UERVcjNVcWU0?= =?utf-8?B?TlNjVG9nMTRyQi93R2JPc1VQSThZSzkzb2g5Z3djMFdXT3o0YlZoWmtteGEv?= =?utf-8?B?eUx6Z0puRFBxcHRNbElJdGJIZWlKOGdCVk0zUy9hV1VSSjJlVi9SdUU2US9r?= =?utf-8?B?NjhPV0w4QmdRcFp0UElhR2JxL0gwV2MzVTJaTkVieG9QY2FxNlM3cGpaNUps?= =?utf-8?B?d3c0MHZldGd6dWhueFUvc2daZ01NaTR6Yjd0aU5BdXI4NXRrNTY0MTZvWURL?= =?utf-8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ITR011ZYwQI/rOv5bKNFmipIcSoiaozckhBNOprylg5AxyrxI=;7:dSlaC+4gZgcC1Rsgf5BIx8TZMFti92hyu6i6LSITKkgHK51/L4dMJbOrcE8MQKslHyNnR3IbUeZ2hnaplXhHL04oNmxGX1k2KsJdBoI/nmSLvcX182fi9dZBxiRCphoiW66EegftXn7geJozzlq/N8ExZUVNBH2MCqZ9mA4PjnxP3ljMQ8f6S/KiLpxpz2VTFqCeTdlZ/hsMcoz5UxlA290rFnYPITatAKXPnC5i8SAfj0giVdMZ3v4fR+Ui81nl SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1144;20:MaeVDIIBdTMwQ975H3ysKWqazeCorXGbriZsDdLI3ipywS90frnOCXzDmCp/SbpClj/R7EAdI30q14YmKm44dCJhuh/cB/6VxCRg89WCEPTB2eBaE90mMkW6v3WVMtryv7XznY2lxL3Wst5zQzcBWh4eSMnm2ZqpB9LFyxLwLmMhKLPv/YGJ+oJqvV+9EO0hF7YGkbJW6ap2+PEiMPMcj6U+g4TGJKRalwfN2Lni/ds4MKDVwx8/a1l5R3mL4Y0p X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Feb 2018 22:58:50.3283 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 64a24e69-72f3-4392-4303-08d56f47855a X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1144 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Provide a new KVM capability that allows bits within MSRs to be recognized as features. Two new ioctls are added to the VM ioctl routine to retrieve the list of these MSRs and their values. The MSR features can optionally be exposed based on a CPU and/or a CPU feature. Signed-off-by: Tom Lendacky --- arch/x86/include/asm/kvm_host.h | 1 arch/x86/kvm/x86.c | 108 ++++++++++++++++++++++++++++++++++++++- include/uapi/linux/kvm.h | 1 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index dd6f57a..5568d0d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1198,6 +1198,7 @@ static inline int emulate_instruction(struct kvm_vcpu *vcpu, bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer); int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr); int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr); +bool kvm_valid_msr_feature(u32 msr, u64 mask); struct x86_emulate_ctxt; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 07d1c7f..4251c34 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -69,6 +69,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include "trace.h" @@ -1048,6 +1049,41 @@ bool kvm_rdpmc(struct kvm_vcpu *vcpu) static unsigned num_emulated_msrs; +/* + * List of msr numbers which are used to expose MSR-based features that + * can be used by QEMU to validate requested CPU features. + */ +struct kvm_msr_based_features { + u32 msr; /* MSR to query */ + u64 mask; /* MSR mask */ + const struct x86_cpu_id *match; /* Match criteria */ + u64 value; /* MSR value */ +}; + +static struct kvm_msr_based_features msr_based_features[] = { + {} +}; + +static unsigned int num_msr_based_features; + +bool kvm_valid_msr_feature(u32 msr, u64 data) +{ + unsigned int i; + + for (i = 0; i < num_msr_based_features; i++) { + struct kvm_msr_based_features *m = msr_based_features + i; + + if (msr != m->msr) + continue; + + /* Make sure not trying to change unsupported bits */ + return (data & ~m->mask) ? false : true; + } + + return false; +} +EXPORT_SYMBOL_GPL(kvm_valid_msr_feature); + bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) { if (efer & efer_reserved_bits) @@ -1156,6 +1192,20 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) return kvm_set_msr(vcpu, &msr); } +static int do_get_msr_features(struct kvm_vcpu *vcpu, unsigned index, u64 *data) +{ + unsigned int i; + + for (i = 0; i < num_msr_based_features; i++) { + if (msr_based_features[i].msr == index) { + *data = msr_based_features[i].value; + return 0; + } + } + + return 1; +} + #ifdef CONFIG_X86_64 struct pvclock_gtod_data { seqcount_t seq; @@ -2681,11 +2731,15 @@ static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, { int i, idx; - idx = srcu_read_lock(&vcpu->kvm->srcu); + if (vcpu) + idx = srcu_read_lock(&vcpu->kvm->srcu); + for (i = 0; i < msrs->nmsrs; ++i) if (do_msr(vcpu, entries[i].index, &entries[i].data)) break; - srcu_read_unlock(&vcpu->kvm->srcu, idx); + + if (vcpu) + srcu_read_unlock(&vcpu->kvm->srcu, idx); return i; } @@ -2784,6 +2838,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_SET_BOOT_CPU_ID: case KVM_CAP_SPLIT_IRQCHIP: case KVM_CAP_IMMEDIATE_EXIT: + case KVM_CAP_GET_MSR_FEATURES: r = 1; break; case KVM_CAP_ADJUST_CLOCK: @@ -4408,6 +4463,36 @@ long kvm_arch_vm_ioctl(struct file *filp, r = kvm_x86_ops->mem_enc_unreg_region(kvm, ®ion); break; } + case KVM_GET_MSR_INDEX_LIST: { + u32 feature_msrs[ARRAY_SIZE(msr_based_features)]; + struct kvm_msr_list __user *user_msr_list = argp; + struct kvm_msr_list msr_list; + unsigned int i, n; + + r = -EFAULT; + if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) + goto out; + n = msr_list.nmsrs; + msr_list.nmsrs = num_msr_based_features; + if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) + goto out; + r = -E2BIG; + if (n < msr_list.nmsrs) + goto out; + + for (i = 0; i < num_msr_based_features; i++) + feature_msrs[i] = msr_based_features[i].msr; + + r = -EFAULT; + if (copy_to_user(user_msr_list->indices, &feature_msrs, + num_msr_based_features * sizeof(u32))) + goto out; + r = 0; + break; + } + case KVM_CAP_GET_MSR_FEATURES: + r = msr_io(NULL, argp, do_get_msr_features, 1); + break; default: r = -ENOTTY; } @@ -4462,6 +4547,25 @@ static void kvm_init_msr_list(void) j++; } num_emulated_msrs = j; + + for (i = j = 0; ; i++) { + struct kvm_msr_based_features *m = msr_based_features + i; + + if (!m->msr) + break; + + if (!x86_match_cpu(m->match)) + continue; + + rdmsrl_safe(m->msr, &m->value); + m->value &= m->mask; + + if (j < i) + msr_based_features[j] = msr_based_features[i]; + + j++; + } + num_msr_based_features = j; } static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 0fb5ef9..429784c 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -934,6 +934,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_S390_AIS_MIGRATION 150 #define KVM_CAP_PPC_GET_CPU_CHAR 151 #define KVM_CAP_S390_BPB 152 +#define KVM_CAP_GET_MSR_FEATURES 153 #ifdef KVM_CAP_IRQ_ROUTING