Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1793560pxy; Fri, 23 Apr 2021 17:51:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOb0h6rlMtZdfKmTOxhLNB29zWXNOQGlvaeH4apVHBEQJtBMsVaI3MLi13Z2fROLGVAdRc X-Received: by 2002:aa7:d54c:: with SMTP id u12mr7852691edr.234.1619225461184; Fri, 23 Apr 2021 17:51:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619225461; cv=none; d=google.com; s=arc-20160816; b=wgvtzlZBq1v5YyUHSpYb0ulw4qGTkCnHmDvbNgvoxT/XDnvUG3MtvdqEOjVywmRlNc DKK4zdO6XfmPRAiiFGV1Nezg/eH619B4EgwrxUojL1RaMj7gnSjLrnvhnv2l/CYlwE6w TY3amLP0UZErWvJ/4nN3V/q0aHGWhXY51UeagGDaNiI10DU8bSSVkh8dmtvzIZDzdMvP 69rb5IbRWW9SeTw85YdJ45e94X3vm1JrqsAknDAV2bg/x/QpBLFw1coY+XpBoIODRRcU Uobnar/xnOcaopAauzzFAcLIHAUoCcBGpXijZzuXm2u7koNFBfOwptB0AXXH6+w5kwA8 3lJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=Z23zPQAgxlBigLhFQaq/Iwh63wl+bRNTg9ZoEclpoEQ=; b=toI9KuAElrAOdamzek3l/FclQue8dSV5NLX3mDiNNbap2abA2y5M1DSpxobH9gWb2Q j+bk0AXX5BVfBIKhGDAOrSSOcGI9A0E+bHaQc5fbNO+J8UCT8/ug1MPGv0O+lNlAN28+ Re2WH5Dc8ZM4seZuYBaZJullAt+HPln00Cd6RTZ/6I+D6iI5oRHYFMng9daH2gb91DZT cettLA+UBNPT3Y9y07CL5qeka3DhdRNWH4OA1iGwW/OjSpcSdGD0J9hx/7cDYk734lT/ 3RVNTV02e0xyrkixxXkQNhhXQbnoTU5nO8U+yJi+uEGYFrOIsoGgCSx3G0NG+DH4Ez0Z muug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="SWsFp/9E"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c14si7445690edr.409.2021.04.23.17.50.38; Fri, 23 Apr 2021 17:51:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="SWsFp/9E"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244228AbhDXAsC (ORCPT + 99 others); Fri, 23 Apr 2021 20:48:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244151AbhDXArs (ORCPT ); Fri, 23 Apr 2021 20:47:48 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 698C6C06175F for ; Fri, 23 Apr 2021 17:47:09 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id 99-20020a0c80ec0000b029017de514d56fso19458082qvb.17 for ; Fri, 23 Apr 2021 17:47:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=Z23zPQAgxlBigLhFQaq/Iwh63wl+bRNTg9ZoEclpoEQ=; b=SWsFp/9EiXizbzTZHrHr0qgh4MJgPmOaDWL3ZJFP7m6lSN0+Oc4/yRfnkMabUXBbO8 rrPWAtC/mzWxZlqtyksyKvjwtdZ6W4jL96KxqfaPjk8N4mxYETzupFdQgq7p7PZ3pXTi vGgmGYA1EDadD+YakYmwdBZILiSbwl/3hMaITsMCr9utLlKPjkUWBMiYa/CKEKoe3eiu ZKujj+e+blJrpSXrFco3lN84sEvEejoq9HVR78JJmEAa+HX3tn3+ZJK2wA9hymcKfp++ RtBF3/69GAczeXPMUbVIJQFWveCrfDknw3pN8A8mInXKqOoBDP/THOWRvRh8P9Ft/A4j XSPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=Z23zPQAgxlBigLhFQaq/Iwh63wl+bRNTg9ZoEclpoEQ=; b=iGFmKScHsTlyS6dhUqxgD2r0KqSVqxEBxsOGbSdUvoOLRzJhvbhm62PUcyIRrX+3qD 26cXVeD+Wjk8CbS5UmGDMblO5xdKxmzPHw7zZa2B+rsL+E3W9SP22OOy8WakDt0ghMBO 0evsW8ER+/qfAQFPMdiPPzfCha8BNzcAP/hkzIHXkdWsfxpw6tVGFqeT7rC7ZpuEdlDv 0U3DjA/DwUZQURj+QI73bPWEw2NZQI2SpK0FIWXBfZxR05xPCwm520fXBZuz9ebXx+av ZyeWwZNNBPbqbeNUbro+FSSh6k07m1AtC+KNl7JgzsSOVBX5vkoICJZGqEnT8PRIavYe vBTg== X-Gm-Message-State: AOAM53349QQhnl6J66TNgYi6OIRZDAmIFET/jSYKPAqMhcQ9oRTjd3Sk 5xeYYr9j8pNwhfAco/eWlnZDelo5Kso= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:f:10:ad52:3246:e190:f070]) (user=seanjc job=sendgmr) by 2002:a05:6214:88d:: with SMTP id cz13mr7404534qvb.13.1619225228657; Fri, 23 Apr 2021 17:47:08 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 23 Apr 2021 17:46:06 -0700 In-Reply-To: <20210424004645.3950558-1-seanjc@google.com> Message-Id: <20210424004645.3950558-5-seanjc@google.com> Mime-Version: 1.0 References: <20210424004645.3950558-1-seanjc@google.com> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog Subject: [PATCH 04/43] KVM: SVM: Fall back to KVM's hardcoded value for EDX at RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org At vCPU RESET/INIT (mostly RESET), stuff EDX with KVM's hardcoded, default Family-Model-Stepping ID of 0x600 if CPUID.0x1 isn't defined. At RESET, the CPUID lookup is guaranteed to "miss" because KVM emulates RESET before exposing the vCPU to userspace, i.e. userspace can't possibly have done set the vCPU's CPUID model, and thus KVM will always write '0'. At INIT, using 0x600 is less bad than using '0'. While initializing EDX to '0' is _extremely_ unlikely to be noticed by the guest, let alone break the guest, and can be overridden by userspace for the RESET case, using 0x600 is preferable as it will allow consolidating the relevant VMX and SVM RESET/INIT logic in the future. And, digging through old specs suggests that neither Intel nor AMD have ever shipped a CPU that initialized EDX to '0' at RESET. Regarding 0x600 as KVM's default Family, it is a sane default and in many ways the most appropriate. Prior to the 386 implementations, DX was undefined at RESET. With the 386, 486, 586/P5, and 686/P6/Athlon, both Intel and AMD set EDX to 3, 4, 5, and 6 respectively. AMD switched to using '15' as its primary Family with the introduction of AMD64, but Intel has continued using '6' for the last few decades. So, '6' is a valid Family for both Intel and AMD CPUs, is compatible with both 32-bit and 64-bit CPUs (albeit not a perfect fit for 64-bit AMD), and of the common Families (3 - 6), is the best fit with respect to KVM's virtual CPU model. E.g. prior to the P6, Intel CPUs did not have a STI window. Modern operating systems, Linux included, rely on the STI window, e.g. for "safe halt", and KVM unconditionally assumes the virtual CPU has an STI window. Thus enumerating a Family ID of 3, 4, or 5 would be provably wrong. Opportunistically remove a stale comment. Fixes: 66f7b72e1171 ("KVM: x86: Make register state after reset conform to specification") Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b9e3229ddc27..d4d7720ce42f 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1219,7 +1219,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) kvm_mmu_reset_context(vcpu); save->cr4 = X86_CR4_PAE; - /* rdx = ?? */ if (npt_enabled) { /* Setup VMCB for Nested Paging */ @@ -1299,7 +1298,15 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) } init_vmcb(vcpu); - kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true); + /* + * Fall back to KVM's default Family/Model/Stepping if no CPUID match + * is found. Note, it's impossible to get a match at RESET since KVM + * emulates RESET before exposing the vCPU to userspace, i.e. it's + * impossible for kvm_cpuid() to find a valid entry on RESET. But, go + * through the motions in case that's ever remedied, and to be pedantic. + */ + if (!kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true)) + eax = get_rdx_init_val(); kvm_rdx_write(vcpu, eax); if (kvm_vcpu_apicv_active(vcpu) && !init_event) -- 2.31.1.498.g6c1eba8ee3d-goog