Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp4050380pxv; Tue, 13 Jul 2021 09:35:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzX7+Lwyzcaet1Up/FOv+G9BCKI0NLEA/xtpU1rhZJl86VSFUj6GRxJI9f77m1E0gN7bwv1 X-Received: by 2002:a17:906:a195:: with SMTP id s21mr6934337ejy.373.1626194140851; Tue, 13 Jul 2021 09:35:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626194140; cv=none; d=google.com; s=arc-20160816; b=TVAbUzZZ4fSesIB+Ge36i/dpqFnkFuz6r5Iw/LuhcSFRs94Gy8Gh5GeZNppFzgn/FN ii2NM8LxFuZhgypw4dCJ1AIoZG2yZD6gDKNTXd4hHZjfjxxkOtzBvqgVQsV8A8PqcP6x JrCpuilbnlv/J03neheSW+PiV3zE+ubjsP9mZzu5eeAmtFBEVDDCxHISBLdp91LIpDan tl01U0qenbJwjDF0ZPS2q6AAnAhUhi+YkGbKcIgCJt0pyz2nur0ZX/SjkpfwNAqSq18T TasHkGSzROkA/bTPjVtcoXbwMtirRORJbYGyVx7C1q7yH1r0FfKLF7zJCJM40Yu91TvK rGSw== 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=TUihYYZX01zOKRYdFMaN5OLTZeUf4zUielfX3s9wDnE=; b=HYuwAXpFU3Bs3kh+fi37h5XoM5+ZkJLn1E3w8HFc4AQ45AVMGmpPLiRQiEZsLPCl3Q Sk21e6/z3JNq+r6GK93OMvDSFYUkN2XyZtVoBumsJdYq6gkVedCvoRQLYhWDisRxw4k3 SbLBc2gXZTU4ggWLifw0Zse/+YZRxIN+QQF6eNzcmYr/pa0rOwxnOMCWAOfQw/a0hZTt eOpzZUMaGcAaOcshDEdz5+F/+3z/aLAXuMb4bVTfYensjuqIekPSBqTINiy1VBKYTMo5 yz+nEBF1B4nRhyG9B1VOoZWercdAfFAhr1PtiyboxGxuwOOCzJJT6wC3URs21hwBd6Kz PnRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Dhw50de7; 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 oy5si5071450ejb.482.2021.07.13.09.35.12; Tue, 13 Jul 2021 09:35:40 -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=Dhw50de7; 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 S233298AbhGMQgm (ORCPT + 99 others); Tue, 13 Jul 2021 12:36:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233456AbhGMQgh (ORCPT ); Tue, 13 Jul 2021 12:36:37 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA8CCC061787 for ; Tue, 13 Jul 2021 09:33:46 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id o12-20020a5b050c0000b02904f4a117bd74so27727574ybp.17 for ; Tue, 13 Jul 2021 09:33:46 -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=TUihYYZX01zOKRYdFMaN5OLTZeUf4zUielfX3s9wDnE=; b=Dhw50de7+3iQD4SG+Jz/qLL69yJiabJyBAj5H5B7E3+UMuR2CyUW8NfEstP/fOuRRc mEf9TpUF8eo/vMnIZJA61FPtKnpR+g9c1bnT6FCBvW03iGvbh9ZAve+TVdOOoekYWcc3 u/GRtrv7FQvpaPNjH0LK6SsgJ/Cd7BLOyAHTCGvHNVDnxFvIr+QZmwfZrXsXyjBM0Vvs berGIXOUWIrmoXTgV3U3Mb++Zrzn8H/k/ZRWI1g7ZciER+Q13T0wQx3cX5RkiMV990g6 6c8Mi33kyQaz5AFnhpmrBAJdxk/3SLLpL5IAdfWzk0mWjqpENbWekjHuseVx3H0Lul5z oQBA== 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=TUihYYZX01zOKRYdFMaN5OLTZeUf4zUielfX3s9wDnE=; b=NgriXpXfjDGShUcioJYw2lRWFIkdj4ZfGJnVTmfTzCXKrROojCl3ciTTkSCBePltFB um0DDBlvTVWFeHSj276KcELlCvmzjuxMipzaK/+kbjdOvTQJGnzYoZe0VcvMIf38W2am ytIjvUQmecrExUQj3R1iE4wY61d4I9W98Nvq+Db7o4mo+/eTmSFM2mOMPlm7mt6PZq8Q itDCFfwVatfPqxJIT37n0KH870Poeic8FVh1b3cQqy+nabQnHyGubi7SeRv9wA1FrPRc ICl3sLcv0XMHvA+ISWeVy7nSjd4HUYPHdRV9RE/x5v9EMpep2JSEfvJdzMcea3Hw+AlZ 8SSA== X-Gm-Message-State: AOAM530b67p26eK3PH6cXKTwN7Ehecp1DbzUA1Q9WfON6UHbt8+eJ5Mg CYihrnLNvxAVlExOSMiMypIn8JzrPq4= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:8208:: with SMTP id q8mr7275091ybk.378.1626194026059; Tue, 13 Jul 2021 09:33:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:44 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-7-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 06/46] 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, Reiji Watanabe 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") Reviewed-by: Reiji Watanabe 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 12e49dc16efe..7da214660c64 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1277,7 +1277,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 */ @@ -1359,7 +1358,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.32.0.93.g670b81a890-goog