From patchwork Wed Nov 9 14:51:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 1394 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp379444wru; Wed, 9 Nov 2022 06:57:31 -0800 (PST) X-Google-Smtp-Source: AMsMyM7S46iCwuYIuvQGSbTNHOA3CbKWSUX3xCeEPVkW2eb4wHRR8yS1MNkDPgYZJ1uf06FYbkDP X-Received: by 2002:a05:6402:2802:b0:43a:9098:55a0 with SMTP id h2-20020a056402280200b0043a909855a0mr59245684ede.179.1668005850980; Wed, 09 Nov 2022 06:57:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668005850; cv=none; d=google.com; s=arc-20160816; b=lnA0n/e5ILhmewj7C1CJfLlshaqnGHX7hJwKUkWN1Jw+hDWtHEB8LUnFL1eNiQUu/j /3eLt2ib24Ud0O0L+5QO221z1q1p1tIN8mcQprHUvAjrxe9F+fSA2UmYLBPeKRDPZvmj KiEo+5sIlN94WARebooaJDArDiTIbZSr2zDOGk9NQeOulgcngajNFBTl8MKk2kFF3nvs z48L5vlPlEqQ40rqqCVdT6+KptxUt8T8pXNwwd5WtyCHnSuKrjiPQEd3y3FqjtpC5nKF g/zOmnempI7VcsvBClYLPgO/J5vpf03Sm2W6+QRL+Ep/Glr397IVtgZjzmoaHbczZtR/ pdkA== 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=rDPg823+ssgoo9QOHHChyrv6cfKJarSq3LFy2v+O4Og=; b=DUkKH/sIJSvGTZNLnKookypBBSbgZEcZ8C1cE96Pwcvukx1beZKYDMk1mHav+7m4Yk 9IruHpwL+V/Hiqx7fN83bvn/0fMX9zIbMUa/b9AJGiyNBF+s4GnccRc3v8oLCoUT4nTG ApEvMVhEmYruKZy0TNGv7refGfKLQ6ogKrq3oqzHZ/GbiPUYcbQECqey58lc6lk4K5yB UsfYVVtbijwcJZ0QWrgQHuNLP3NCQ+JIzTRBllcYJFNdwucPRCJQNojA7z2iQ5b3bzVc e9cld0YDNwmCWfwyA33oc4y0xNKgT8WOKuAfcEHNfDrld/NqoVzRccZ9Omcx6BIIqjW6 KkxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Nycd0DBC; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ji11-20020a170907980b00b007acdb4016e0si15582456ejc.224.2022.11.09.06.57.06; Wed, 09 Nov 2022 06:57:30 -0800 (PST) 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=Nycd0DBC; 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 S231715AbiKIOyL (ORCPT + 99 others); Wed, 9 Nov 2022 09:54:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230072AbiKIOyA (ORCPT ); Wed, 9 Nov 2022 09:54:00 -0500 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 E89C31C136 for ; Wed, 9 Nov 2022 06:52:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668005523; 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=rDPg823+ssgoo9QOHHChyrv6cfKJarSq3LFy2v+O4Og=; b=Nycd0DBCEJz/yxqUbPwxzdGSfPCNy/3J7ac/KI2QB/idtAXzKmt0Cpb+nYK9VfGXcV1mnq RPMfTIKg7ZAvvpE7BIPmUlVquvDpTkzUwLPVpdf7e0UM7Cj3UQv05GNfJjZzM+xG67Z0Np +CYtiwOAQ5h91HZt+WPHYEs7lopBBR8= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-147-nGbcBkZFMwmsKOgK8rOuFg-1; Wed, 09 Nov 2022 09:51:58 -0500 X-MC-Unique: nGbcBkZFMwmsKOgK8rOuFg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 95DC72815216; Wed, 9 Nov 2022 14:51:57 +0000 (UTC) Received: from virtlab511.virt.lab.eng.bos.redhat.com (virtlab511.virt.lab.eng.bos.redhat.com [10.19.152.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6B43E1401C2E; Wed, 9 Nov 2022 14:51:57 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: thomas.lendacky@amd.com, jmattson@google.com, seanjc@google.com Subject: [PATCH v3 00/11] KVM: SVM: fixes for vmentry code Date: Wed, 9 Nov 2022 09:51:45 -0500 Message-Id: <20221109145156.84714-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=-2.1 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=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749030903301151213?= X-GMAIL-MSGID: =?utf-8?q?1749030903301151213?= This series comprises two related fixes: - the FILL_RETURN_BUFFER macro in -next needs to access percpu data, hence the GS segment base needs to be loaded before FILL_RETURN_BUFFER. This means moving guest vmload/vmsave and host vmload to assembly (patches 8 and 9). - because AMD wants the OS to set STIBP to 1 before executing the return thunk (un)training sequence, IA32_SPEC_CTRL must be restored before UNTRAIN_RET, too. This must also be moved to assembly and, for consistency, the guest SPEC_CTRL is also loaded in there (patch 10). Neither is particularly hard, however because of 32-bit systems one needs to keep the number of arguments to __svm_vcpu_run to three or fewer. Therefore, patches 2 to 7 move various accesses to the vcpu_svm struct and to percpu data to vmenter.S, cleaning up various bits along the way to keep the assembly code nice. I think the code is simpler than before after these prerequisites, and even at the end of the series it is not much harder to follow despite doing a lot more stuff. Care has been taken to keep the "normal" and SEV-ES code as similar as possible, even though the latter would not hit the three argument barrier; even the prototype is the same. The above summary leaves patch 1, which introduces a separate asm-offsets.c file for KVM, so that kernel/asm-offsets.c does not have to do ugly includes with ../ paths; and patch 11, which is just more dead code removal. Thanks, Paolo v2->v3: store SME-adjusted save_area physical address in svm_cpu_data, access it with PER_CPU_VAR instead of an argument [Sean, sort of] split out-of-line spec-ctrl restore macro in two parts [Sean] adjust comments to point out clobbered registers [Sean] Paolo Bonzini (11): KVM: x86: use a separate asm-offsets.c file KVM: SVM: replace regs argument of __svm_vcpu_run with vcpu_svm KVM: SVM: adjust register allocation for __svm_vcpu_run KVM: SVM: retrieve VMCB from assembly KVM: SVM: remove unused field from struct vcpu_svm KVM: SVM: remove dead field from struct svm_cpu_data KVM: SVM: do not allocate struct svm_cpu_data dynamically KVM: SVM: move guest vmsave/vmload back to assembly KVM: SVM: restore host save area from assembly 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 | 6 - arch/x86/kernel/cpu/bugs.c | 15 +- arch/x86/kvm/.gitignore | 2 + arch/x86/kvm/Makefile | 12 ++ arch/x86/kvm/kvm-asm-offsets.c | 29 ++++ arch/x86/kvm/svm/sev.c | 4 +- arch/x86/kvm/svm/svm.c | 105 +++++-------- arch/x86/kvm/svm/svm.h | 11 +- arch/x86/kvm/svm/svm_ops.h | 5 - arch/x86/kvm/svm/vmenter.S | 260 +++++++++++++++++++++++++------ arch/x86/kvm/vmx/vmenter.S | 2 +- 12 files changed, 305 insertions(+), 156 deletions(-) create mode 100644 arch/x86/kvm/.gitignore create mode 100644 arch/x86/kvm/kvm-asm-offsets.c