Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3472229pxb; Mon, 4 Apr 2022 18:03:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZtT/WZe9d7yhBA5e39+qdNBhWFLPVn4mYy285GFypkq5Qe2DfS8nt7HdEHceHZXDp4VmI X-Received: by 2002:a17:902:f64d:b0:151:3895:46bf with SMTP id m13-20020a170902f64d00b00151389546bfmr963792plg.31.1649120608116; Mon, 04 Apr 2022 18:03:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649120608; cv=none; d=google.com; s=arc-20160816; b=h19QK/IOPsKG0GzpGFVNBreI85iAJ09Bb95PKurJzXJUB4Q61CjhiqpIwisrXJSsG5 dhuPKO/EfEq/Lu5ydKQmHxLOl6g7u4jbOPyB9kAk71+yhpQ8IXPQ7CpLzGKg5zcyc7XB 1DOWfMh0hA4wN4w2CFW6LwDF0hI4/AZj0ZSU/SSmH94dkFtXhugewAjViWkJLc4jgTIo vgvk14HS2w6Cx7/1nAFNpIOOJFoLHLMQPh47YRijGedJhUrNoJMV1S/1vNT2sXRR/ZCB H4auGMPofK6PpeFESg/ReOsxuArhoNhORvwa+baFYPaoP42GgxRWzB8KoKbXsGk26pAg QHtw== 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 :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=UnGPjnMk0HWFLFp7dnhS2WarhT53NPNrXRrJQZdc19E=; b=vHTEnQZEajcSt+lpdPbZtAiO8he426w02dRLajzaI+xcLmbn8VgIDgNfDoYEqvJ7xc MQYPFZzmNaqsw41SClfJj/k5JndBmCpdNyFv7846jHjKsb01JhqX0ABDapD0g8g/3U8S MK9xN5oHEdb1a7dq4yEx4tb1sj6xmnrCY5hVMB9qsT/Hk4wAT+ASIsmr+NphPwG7b9bv EtGOnNRSIIFKrDEQ5IguqGl/Gx3DRzagolStRSgve2gjNssOYP2h1YuOjOdJG/ENLyH2 Mox5i6bF1BWL88/lGPJX9Qmqz3dAhwnXw7hLs/T/0bErssAROFgBeGqzDXmCzo8ofbur mcyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MinpBI2a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id x11-20020aa79a4b000000b004fa92f57a9bsi11199251pfj.278.2022.04.04.18.03.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 18:03:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MinpBI2a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 18E6C6BDC9; Mon, 4 Apr 2022 17:11:10 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354760AbiDDJww (ORCPT + 99 others); Mon, 4 Apr 2022 05:52:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354718AbiDDJwu (ORCPT ); Mon, 4 Apr 2022 05:52:50 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 026FA3AA44 for ; Mon, 4 Apr 2022 02:50:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649065854; 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: in-reply-to:in-reply-to:references:references; bh=UnGPjnMk0HWFLFp7dnhS2WarhT53NPNrXRrJQZdc19E=; b=MinpBI2axfVZFolEm+PRQCI+bqDdPBQFjrQ1kTqGmEzYIfbaAbtLA4mrb1SPM+Yak612Zx UV1uwne+O9YoZV7qRFo18kQYE/14PYF6qbhLvLqzYFT6u8ufGfACjUaGF1VBQAT0e4935K frLdTF+bHe2vuPKuY2vnHka1LQpCQKI= 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-606-hwBMZr7cPFq81Zbfr0yabA-1; Mon, 04 Apr 2022 05:50:48 -0400 X-MC-Unique: hwBMZr7cPFq81Zbfr0yabA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AA0813C14CC3; Mon, 4 Apr 2022 09:50:47 +0000 (UTC) Received: from starship (unknown [10.40.194.231]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3DBCB2156732; Mon, 4 Apr 2022 09:50:44 +0000 (UTC) Message-ID: Subject: Re: [PATCH 1/5] KVM: nSVM: Sync next_rip field from vmcb12 to vmcb02 From: Maxim Levitsky To: Sean Christopherson , "Maciej S. Szmigiero" Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Tom Lendacky , Brijesh Singh , Jon Grimm , David Kaplan , Boris Ostrovsky , Liam Merwick , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Date: Mon, 04 Apr 2022 12:50:43 +0300 In-Reply-To: References: <19c757487eeeff5344ff3684fe9c090235b07d05.1646944472.git.maciej.szmigiero@oracle.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Fri, 2022-04-01 at 21:51 +0000, Sean Christopherson wrote: > On Fri, Apr 01, 2022, Maciej S. Szmigiero wrote: > > On 1.04.2022 20:32, Sean Christopherson wrote: > > > On Thu, Mar 10, 2022, Maciej S. Szmigiero wrote: > > > > + /* The return address pushed on stack by the CPU for some injected events */ > > > > + svm->vmcb->control.next_rip = svm->nested.ctl.next_rip; > > > > > > This needs to be gated by nrips being enabled _and_ exposed to L1, i.e. > > > > > > if (svm->nrips_enabled) > > > vmcb02->control.next_rip = svm->nested.ctl.next_rip; > > > > It can be done, however what if we run on a nrips-capable CPU, > > but don't expose this capability to the L1? > > Oh, right, because the field will be populated by the CPU on VM-Exit. Ah, the > correct behavior is to grab RIP from vmcb12 to emulate nrips=0 hardware simply > not updating RIP. E.g. zeroing it out would send L2 into the weeds on IRET due > the CPU pushing '0' on the stack when vectoring the injected event. > > if (svm->nrips_enabled) > vmcb02->control.next_rip = svm->nested.ctl.next_rip; > else if (boot_cpu_has(X86_FEATURE_NRIPS)) > vmcb02->control.next_rip = vmcb12_rip; > > > The CPU will then push whatever value was left in this field as > > the return address for some L1 injected events. This makes sense. Note that even AMD's PRM has a note about this: " 15.20 Event Injection ... Software interrupts cannot be properly injected if the processor does not support the NextRIP field. Support is indicated by CPUID Fn8000_000A_EDX[NRIPS] = 1. Hypervisor software should emulate the event injection of software interrupts if NextRIP is not supported " > > > > Although without nrips feature the L1 shouldn't even attempt event > > injection, copying this field anyway will make it work if L1 just > > expects this capability based on the current CPU model rather than > > by checking specific CPUID feature bits. The guest really ought to check CPUID bits. Plus the CPU model is also usually virtualized (for named machine types in Qemu for example). > > L1 may still inject the exception, it just advances the RIP manually. As above, > the really messy thing is that, because there's no flag to say "don't use NextRIP!", > the CPU will still consume NextRIP and push '0' on the stack for the return RIP > from the INTn/INT3/INTO. Yay. > > I found that out the hard way (patch in-progress). The way to handle event > injection if KVM is loaded with nrips=0 but nrips is supported in hardware is to > stuff NextRIP on event injection even if nrips=0, otherwise the guest is hosed. > > > > > + u64 next_rip; > > > > u64 nested_cr3; > > > > u64 virt_ext; > > > > u32 clean; > > > > > > I don't know why this struct has > > > > > > u8 reserved_sw[32]; > > > > > > but presumably it's for padding, i.e. probably should be reduced to 24 bytes. > > > > Apparently the "reserved_sw" field stores Hyper-V enlightenments state - > > see commit 66c03a926f18 ("KVM: nSVM: Implement Enlightened MSR-Bitmap feature") > > and nested_svm_vmrun_msrpm() in nested.c. > > Argh, that's a terrible name. Thanks for doing the homework, I was being lazy. That was added around the commit 1183646a67d01 ("KVM: SVM: hyper-v: Direct Virtual Flush support") Seems to be used by HV to store 'struct hv_enlightenments', but I don't know 100% if that is the only thing that can be stored in this area. Best regards, Maxim Levitsky >