Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2215983pxp; Mon, 21 Mar 2022 14:06:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3Ax3+SMVcelQYvzMIIazTuRPEmzvMOwq47mdXhZgUOYGA5hhzmp4vkoTNvy5a6mfG3ZTH X-Received: by 2002:a17:902:f551:b0:153:b179:291a with SMTP id h17-20020a170902f55100b00153b179291amr14716206plf.13.1647896769429; Mon, 21 Mar 2022 14:06:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647896769; cv=none; d=google.com; s=arc-20160816; b=ccmtBzYBsJmy1XIAEO4AYJKcDpH2Up188CW4p5+gSdPAgJbhIdkza2PzO1zO21QydX vpnhETrzbdVw4WORC85n0pbUsuqNOoip5o864WaxSbxwppjtlNl+gA0WinjggKJqQupW 5aF05tCdbhtoYVCwaI4wLWsNi8ME88pX5ITvESKt4FThHfSIQyJYkgxGJNnO0wUL3Pqq d1763w4V6kVIoAIFQqH38OfgoYMDyCZLQMj05D60Rfa1ymrQmroj4ujoVAr6PSMjw5GO ny/ObcvADQ8iUiuQ9kTgHAA6Lfgy7y36lDJepxqxU0nEo8Skcnm3n4B34ud487c2Xxyh mu7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=oyGzY8l76MsegSAn4xQxjQPLkZzO2340czzi6D4Ior8=; b=wQ9sebT3kO5cfChMXGiB2ByqQuLymeKv+Kog7kqPIypF+F7jYGl19cgiW9li90Tq6G c91l0JvplAcaXiUk+dA/3hF+S2gcPPgAOvXIGVIBSnGlBxx2tEPE+gayoXyi4EAOa9/7 D1B8QY1NyUnX6tdemGKzv/1Xq/zcyYWzSOURyI8DftdUgF8GBoqPWLUt66cV8yF3GUzt xX2SRDUifp0lLvtU7vHdMdVCA5x/Eb4xf44lBQcbjFszTt4fEF7EW7nIzwCrDg58QHLy ghdpqWsCXdq1KD6f9z9+dqj4OCEvbxtbFs7UQ2C4CqQhycfQsTz/hTfenWnNVT61eWm8 R1Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Ej2kjVjf; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id w9-20020a1709029a8900b00153b2d164easi10549442plp.242.2022.03.21.14.06.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:06:09 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Ej2kjVjf; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AF2EA181790; Mon, 21 Mar 2022 13:59:53 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352195AbiCUSd7 (ORCPT + 99 others); Mon, 21 Mar 2022 14:33:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352188AbiCUSd6 (ORCPT ); Mon, 21 Mar 2022 14:33:58 -0400 Received: from mail-yw1-x1134.google.com (mail-yw1-x1134.google.com [IPv6:2607:f8b0:4864:20::1134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00DC6188A3F for ; Mon, 21 Mar 2022 11:32:32 -0700 (PDT) Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-2d07ae0b1c4so165300677b3.11 for ; Mon, 21 Mar 2022 11:32:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=oyGzY8l76MsegSAn4xQxjQPLkZzO2340czzi6D4Ior8=; b=Ej2kjVjfZTFuj5I2XgPtiMJGOUTZa2UlGmehWn+6/bXjwFlLt3vvGW6qp/Za/xN9fH v4Hk1/ip8lasFXz+5aL0B0M4VJKXF4qVcUb8276ltduclc2KWaFsTGi0zr9B/RraDkhA fD8Czx/aQABtevWJt4MhO+ulvmAvvgEWsYNu8CEdVK4z2gSYbB1y+P6/TTIzQ9nMvewJ NSf7sxvzM2YrA+iNNLQk0A3q6U2Y3HMlG3dOjFEtwnRc8eszCrWcScnCMTHDNIjjlrc7 wZEsROJjcoKd7liyPjdKYFNXAYGT6OgRr62kuQd1sp6xXzufWYlAjm0Uom1qisVS4QRg Cc+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=oyGzY8l76MsegSAn4xQxjQPLkZzO2340czzi6D4Ior8=; b=WfEXIDCzRfcuwWw8Z/634Ni9ms8WkQMr6RUspNiUzRLezkSG98gQ43QoD7pZ8t0FtJ CR7X9tb8hF1zum8hR1gYGkYkuWo68GAir+QeDvGrjiBu+oQU6P2z4Oymn/Vn+WEvqiOe y0HC3Pudr7vFDDyDbVRYH3HESavaezyeT6dNeg6qHbe6VJmX/IoAMKfHVOxDrfwVcZEi m6zNwXN3O3E8hxpPbkLDWMciFMj0bcUiOotGGUMv6EsVkTQkZMo4XF6kM041X99Mqwix zTmn1P7G/I3ZG0FeKnOiFybEJoA7BsqwNxXRGxjSYfEdb+48NMDIcsDP6Ys7jojpgCoX Q7iQ== X-Gm-Message-State: AOAM530bKZC358Y2noyZHBqD3LdZ6J9qyAKPPaz10P+r9eicJitjghNE Av3oESD44n0BRTsf6wKuaEpzHQr+OetJL8vhX8y1IA== X-Received: by 2002:a81:351:0:b0:2e5:9cb9:8c44 with SMTP id 78-20020a810351000000b002e59cb98c44mr26060988ywd.250.1647887551971; Mon, 21 Mar 2022 11:32:31 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Sagi Shahar Date: Mon, 21 Mar 2022 11:32:21 -0700 Message-ID: Subject: Re: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function To: "Yamahata, Isaku" Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , Jim Mattson , Erdem Aktas , Connor Kuehl , Sean Christopherson Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL 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, Mar 4, 2022 at 12:00 PM wrote: > > From: Sean Christopherson > > By necessity, TDX will use a different register ABI for hypercalls. > Break out the core functionality so that it may be reused for TDX. > > Signed-off-by: Sean Christopherson > Signed-off-by: Isaku Yamahata > --- > arch/x86/include/asm/kvm_host.h | 4 +++ > arch/x86/kvm/x86.c | 54 ++++++++++++++++++++------------- > 2 files changed, 37 insertions(+), 21 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 8dab9f16f559..33b75b0e3de1 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -1818,6 +1818,10 @@ void kvm_request_apicv_update(struct kvm *kvm, bool activate, > void __kvm_request_apicv_update(struct kvm *kvm, bool activate, > unsigned long bit); > > +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr, > + unsigned long a0, unsigned long a1, > + unsigned long a2, unsigned long a3, > + int op_64_bit); > int kvm_emulate_hypercall(struct kvm_vcpu *vcpu); > > int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 314ae43e07bf..9acb33a17445 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -9090,26 +9090,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu) > return kvm_skip_emulated_instruction(vcpu); > } > > -int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) > +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr, > + unsigned long a0, unsigned long a1, > + unsigned long a2, unsigned long a3, > + int op_64_bit) > { > - unsigned long nr, a0, a1, a2, a3, ret; > - int op_64_bit; > - > - if (kvm_xen_hypercall_enabled(vcpu->kvm)) > - return kvm_xen_hypercall(vcpu); > - > - if (kvm_hv_hypercall_enabled(vcpu)) > - return kvm_hv_hypercall(vcpu); > - > - nr = kvm_rax_read(vcpu); > - a0 = kvm_rbx_read(vcpu); > - a1 = kvm_rcx_read(vcpu); > - a2 = kvm_rdx_read(vcpu); > - a3 = kvm_rsi_read(vcpu); > + unsigned long ret; > > trace_kvm_hypercall(nr, a0, a1, a2, a3); > > - op_64_bit = is_64_bit_hypercall(vcpu); > if (!op_64_bit) { > nr &= 0xFFFFFFFF; > a0 &= 0xFFFFFFFF; > @@ -9118,11 +9107,6 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) > a3 &= 0xFFFFFFFF; > } > > - if (static_call(kvm_x86_get_cpl)(vcpu) != 0) { > - ret = -KVM_EPERM; > - goto out; > - } > - > ret = -KVM_ENOSYS; > > switch (nr) { > @@ -9181,6 +9165,34 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) > ret = -KVM_ENOSYS; > break; > } > + return ret; > +} > +EXPORT_SYMBOL_GPL(__kvm_emulate_hypercall); > + > +int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) > +{ > + unsigned long nr, a0, a1, a2, a3, ret; > + int op_64_bit; > + > + if (kvm_xen_hypercall_enabled(vcpu->kvm)) > + return kvm_xen_hypercall(vcpu); > + > + if (kvm_hv_hypercall_enabled(vcpu)) > + return kvm_hv_hypercall(vcpu); > + > + nr = kvm_rax_read(vcpu); > + a0 = kvm_rbx_read(vcpu); > + a1 = kvm_rcx_read(vcpu); > + a2 = kvm_rdx_read(vcpu); > + a3 = kvm_rsi_read(vcpu); > + op_64_bit = is_64_bit_mode(vcpu); I think this should be "op_64_bit = is_64_bit_hypercall(vcpu);" is_64_bit_mode was replaced with is_64_bit_hypercall to support protected guests here: https://lore.kernel.org/all/87cztf8h43.fsf@vitty.brq.redhat.com/T/ Without it, op_64_bit will be set to 0 for TD VMs which will cause the upper 32 bit of the registers to be cleared in __kvm_emulate_hypercall > + > + if (static_call(kvm_x86_get_cpl)(vcpu) != 0) { > + ret = -KVM_EPERM; > + goto out; > + } > + > + ret = __kvm_emulate_hypercall(vcpu, nr, a0, a1, a2, a3, op_64_bit); > out: > if (!op_64_bit) > ret = (u32)ret; > -- > 2.25.1 > Sagi