Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3445720pxb; Mon, 4 Apr 2022 17:13:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/NyY7vYozRPBu9E9QEfUBHa/SuuX+x1GzhD1gzngivoIQ6+RLEg14NH+eCQK0oK33u0ek X-Received: by 2002:a17:902:7794:b0:156:7641:a6cf with SMTP id o20-20020a170902779400b001567641a6cfmr638356pll.35.1649117623411; Mon, 04 Apr 2022 17:13:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649117623; cv=none; d=google.com; s=arc-20160816; b=Ji1S1EMaIcVI2rD0yQWbmdMw45DNIcTkrDhGAMXZOOaShDRU/xwehPNAIaEPrcRxkr 4GcuwDJcU8dLThsAOkrPTr3v0+WpBPpA8/hT+r0xw095+o3ZWxOJqIOMVeKuqbOGHEus U/cwSkHLCfOtuGBLKkX1lWz46FAArRGR7uAmClyfV/9wizEwpyfeSyF6W6YHxfVI//Eu eHKXu1hKhLVpR/cLGS7vh0GKfSX2CBt8c/N51R9L2Brbf7u422IZuthJfcb7aOu6h7Ao 7a2xnu+HfwPPTqHROvhuvr3Hj4obJkRTLf53OzT8ztvu7kBw/Aas0Sy80QIGWEPv+FeL KIZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id; bh=Acf48FY/9hohPHln3FrW6Ye9MzVAa+J8z0vzMfkg7ek=; b=hmwuhXKZahfXO9kwkVM1ccf67HfD3Mu71gsEYD3LlKXY2XmuGeuiy6FhGLx7ZFM2ue ELJr4pNEtZV4g3IxeH8RS6N7HGh//ypqI3gtY0gm4kb1Y+AZNZLGAQQCTCuOEY+uf7mQ svNbhoqo3yUEq2KVeJEfDkD4orK6NIA2tnaRsamBj05AvYS0ub2Hl7tLgs6qg2SlK/gL QTXzoAA1R1hO42RCxx9ZtLEP5ybyDB+OkMOkkss1UzToZEhIccpzV8iSRa1LDJDhxuVz cUT7ajvZWVwYQhHlLAtlWEAaU2pjFENtgW7xkzmvv/4+RaSY577xpwOaCL7RzkGzwpSu Geow== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id ot10-20020a17090b3b4a00b001c6a8481f5csi568880pjb.1.2022.04.04.17.13.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 17:13:43 -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; 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 17D9C6D4D6; Mon, 4 Apr 2022 16:44:01 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1381793AbiDDVYD (ORCPT + 99 others); Mon, 4 Apr 2022 17:24:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379672AbiDDRsF (ORCPT ); Mon, 4 Apr 2022 13:48:05 -0400 Received: from vps-vb.mhejs.net (vps-vb.mhejs.net [37.28.154.113]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 087F92E9CD; Mon, 4 Apr 2022 10:46:07 -0700 (PDT) Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1nbQm6-000327-H6; Mon, 04 Apr 2022 19:45:58 +0200 Message-ID: <20d050db-78f2-2a78-b319-a84f726a0bf9@maciej.szmigiero.name> Date: Mon, 4 Apr 2022 19:45:52 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Content-Language: en-US To: Sean Christopherson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org References: <20220402010903.727604-1-seanjc@google.com> <20220402010903.727604-2-seanjc@google.com> <112c2108-7548-f5bd-493d-19b944701f1b@maciej.szmigiero.name> From: "Maciej S. Szmigiero" Subject: Re: [PATCH 1/8] KVM: nSVM: Sync next_rip field from vmcb12 to vmcb02 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE 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 On 4.04.2022 19:21, Sean Christopherson wrote: > On Mon, Apr 04, 2022, Maciej S. Szmigiero wrote: >>> @@ -1606,7 +1622,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, >>> nested_copy_vmcb_control_to_cache(svm, ctl); >>> svm_switch_vmcb(svm, &svm->nested.vmcb02); >>> - nested_vmcb02_prepare_control(svm); >>> + nested_vmcb02_prepare_control(svm, save->rip); >> >> ^ >> I guess this should be "svm->vmcb->save.rip", since >> KVM_{GET,SET}_NESTED_STATE "save" field contains vmcb01 data, >> not vmcb{0,1}2 (in contrast to the "control" field). > > Argh, yes. Is userspace required to set L2 guest state prior to KVM_SET_NESTED_STATE? > If not, this will result in garbage being loaded into vmcb02. I'm not sure about particular KVM API guarantees, but looking at the code I guess it is supposed to handle both cases: 1) VMM loads the usual basic KVM state via KVM_SET_{S,}REGS then immediately issues KVM_SET_NESTED_STATE to load the remaining nested data. Assuming that it was the L2 that was running at the save time, at first the basic L2 state will be loaded into vmcb01, then at KVM_SET_NESTED_STATE time: > if (is_guest_mode(vcpu)) > svm_leave_nested(vcpu); > else > svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save; The !is_guest_mode(vcpu) branch will be taken (since the new VM haven't entered the guest mode yet), which will copy the basic L2 state from vmcb01 to vmcb02 and then the remaining code will restore vmcb01 save and vmcb{0,1}2 control normally and then enter the guest mode. 2) VMM first issues KVM_SET_NESTED_STATE then immediately loads the basic state. Sane as the above, only some initial VM state will be copied into vmcb02 from vmcb01 by the code mentioned above, then vmcb01 save and vmcb{0,1}2 control will be restored and guest mode will be entered. If the VMM then immediately issues KVM_SET_{S,}REGS then it will restore L2 basic state straight into vmcb02. However, this all is my guess work from just looking at the relevant code, I haven't run any tests to make sure that I haven't missed something. Thanks, Maciej