Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3111144rwi; Fri, 28 Oct 2022 16:11:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5UBlP+C2bl0QgNQzi6vkGX4pOX4nLvNltcMrJ2UVgxddAbDSISe9WduzbTAxeoiBg1VwqC X-Received: by 2002:a05:6402:5191:b0:45c:dea0:2c7 with SMTP id q17-20020a056402519100b0045cdea002c7mr1643414edd.239.1666998713961; Fri, 28 Oct 2022 16:11:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666998713; cv=none; d=google.com; s=arc-20160816; b=JW/vzj4iVozNbAco/E+kkXLrtGO+mNmykRljTuF1RkKzWlHnLkwa2T31wlejPdO+ev fsIeU6nfdozYJ+mpOcsscKQQia9JtT7epKLjx0R0dlZBLB0Sed39ZhtWuV+N73Rmg54C aS9+AM/OJS0fpa9aBJZ6so9UN40RapzBtsGMQaS/XtLxE3aAlt5CaY/hpWJS0cuYHvu4 I44RX/ZD/7wVb14XIIZ8vOeTO13qRWJDG/0SFbtjuGBr3DqLN166FnjwHYhbbFboq6nv Pzz1/Dj46VKCOT/NxotLJxdqp6e/3DyXeG9sKzQW5CPNu8XAgg5M3P06tLm+ioOqYN6L 0r6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=A2c8ymd1GFvLBVMNdJ9tI50soRavZnzeQUSNcQq0dMs=; b=jYeWsV5JqjvoP/ggdHotQspbZuOgi8BohYCuyV1pU1yR6ApYoMilvxhIDV7vWyS7gv pGFCY8Pvq6zn9LPkgnErVFaItQMV8D6h2QobhMnlM2ApU4rl2NCBA/QcvuF7Cy+wesLF NcZ7/fGyXUMzDN+2toHpk8vcL++C2lU1EHiGnT/4fa97qzlyaFrws8VEHEDxHRIVsgXW F9IfFHseSdv9BqPHbc6hgJzGtsRyrCQZ1Gp+MkPZ7Dc6pO21ZwsVRNP233XW1vPUMnRI 16FrEAyLcjhnsyT1663L6I0CIerZ22wLKRKIVv5s2v1P3L/uhkakkUoMeQADG6gXrvMc I7Eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FGTGwBp7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d4-20020aa7c1c4000000b00459f6d1be02si5418126edp.387.2022.10.28.16.11.29; Fri, 28 Oct 2022 16:11:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FGTGwBp7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229616AbiJ1XIm (ORCPT + 99 others); Fri, 28 Oct 2022 19:08:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229531AbiJ1XIk (ORCPT ); Fri, 28 Oct 2022 19:08:40 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20B9A23797A for ; Fri, 28 Oct 2022 16:07:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666998472; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=A2c8ymd1GFvLBVMNdJ9tI50soRavZnzeQUSNcQq0dMs=; b=FGTGwBp7M0d02EniqDmPYaiiK4e93zjFJI/Cl41ah7bXwTgf2b1A9oWw4TvBI+hIe0bS4q QCyxUhGB6CjzRFUfNhpCZmuErSdwA6ci7EUg3/StYne+6CflNVeBbnnD2SXQossMIh48qW c+osX1yVQl65M549NKG8HaD0PwSD5Zk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-455-LZJSepqpOAikHMAm8TLHDg-1; Fri, 28 Oct 2022 19:07:50 -0400 X-MC-Unique: LZJSepqpOAikHMAm8TLHDg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E9579857D0D; Fri, 28 Oct 2022 23:07:39 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id D7CC540C6EC3; Fri, 28 Oct 2022 23:07:25 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: jmattson@google.com, seanjc@google.com, jpoimboe@kernel.org Subject: [RFC PATCH 0/7] KVM: SVM: move MSR_IA32_SPEC_CTRL save/restore to assembly Date: Fri, 28 Oct 2022 19:07:16 -0400 Message-Id: <20221028230723.3254250-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On the Intel side, restoration of the guest's IA32_SPEC_CTRL is done as late as possible before vmentry, with the comment: * IMPORTANT: To avoid RSB underflow attacks and any other nastiness, * there must not be any returns or indirect branches between this code * and vmentry. On AMD, there is no need to avoid returns or indirect branches between wrmsr and vmrun because Linux doesn't use IBRS; however, restoration of the host IA32_SPEC_CTRL value is definitely way too late. With respect to the user/kernel boundary, AMD says, "If software chooses to toggle STIBP (e.g., set STIBP on kernel entry, and clear it on kernel exit), software should set STIBP to 1 before executing the return thunk training sequence." Assuming the same requirements apply to the guest/host boundary, KVM does not respect this recommendation: the return thunk training sequence is in vmenter.S, quite close to the VM-exit, while the host's IA32_SPEC_CTRL value is only restored much later for hosts without V_SPEC_CTRL. In the absence of clarifications for AMD, move all the SPEC_CTRL handling to assembly code and, in passing, also make the Intel and AMD code a bit more similar to each other. Patches 1-2 are the Intel side, which is just a cleanup. Patch 3 prepares for adding asm-offsets.c entries in arch/x86/kvm/svm/svm.h, and patches 4-5 are a similar cleanup to the earlier VMX ones. Patch 6 is the bulk of the change, and finally patch 7 removes now dead code in asm/spec-ctrl.h and arch/x86/kernel/. This is RFC because I haven't tested SEV-ES or 32-bit yet. Paolo Paolo Bonzini (7): KVM: VMX: remove regs argument of __vmx_vcpu_run KVM: VMX: more cleanups to __vmx_vcpu_run KVM: SVM: extract VMCB accessors to a new file KVM: SVM: replace argument of __svm_vcpu_run with vcpu_svm KVM: SVM: adjust register allocation for __svm_vcpu_run KVM: SVM: move MSR_IA32_SPEC_CTRL save/restore to assembly x86, KVM: remove unnecessary argument to x86_virt_spec_ctrl and callers arch/x86/include/asm/spec-ctrl.h | 10 +- arch/x86/kernel/asm-offsets.c | 8 ++ arch/x86/kernel/cpu/bugs.c | 15 +-- arch/x86/kvm/svm/avic.c | 1 + arch/x86/kvm/svm/nested.c | 1 + arch/x86/kvm/svm/sev.c | 1 + arch/x86/kvm/svm/svm.c | 39 +++--- arch/x86/kvm/svm/svm.h | 204 +----------------------------- arch/x86/kvm/svm/svm_onhyperv.c | 1 + arch/x86/kvm/svm/vmcb.h | 211 +++++++++++++++++++++++++++++++ arch/x86/kvm/svm/vmenter.S | 164 ++++++++++++++++++------ arch/x86/kvm/vmx/nested.c | 3 +- arch/x86/kvm/vmx/vmenter.S | 92 ++++++-------- arch/x86/kvm/vmx/vmx.c | 3 +- arch/x86/kvm/vmx/vmx.h | 3 +- 15 files changed, 419 insertions(+), 337 deletions(-) create mode 100644 arch/x86/kvm/svm/vmcb.h -- 2.31.1