Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp264365rdf; Tue, 21 Nov 2023 01:54:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IFrBOOc4uX0M7jSPPKAOuN9DXw3ZIlpxFnc7eopAMLi0ikhNBmPDug5hTiEzo5OCsuR06MM X-Received: by 2002:a17:902:eccd:b0:1cf:54e1:8c8c with SMTP id a13-20020a170902eccd00b001cf54e18c8cmr10093138plh.63.1700560487886; Tue, 21 Nov 2023 01:54:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700560487; cv=none; d=google.com; s=arc-20160816; b=mz8UpIvtT1znLn1fXwM8+kiNvqCRoq8lzH9+AHDqG0FuL4CSuDeCT8TaH35OGoml2w +hvTxtnRIer3SbUmH8sDzh0asLrgJg+eqGrwmXqR6cESokvkGoeRArMpLQTbqRkyJseh YlDsGiIL8OtRdzQB1ADBlfthJtfWUuwBpJpJ//aFx7mm/iFAC/DnXPu+zP91a1sTiRsT poDIVFyi+iVdlphhj66ys/vSYvNNurpg+RXjG28xzHQrI5X5xoX/lkDgGQkMitAiktn/ DLd2hJcwnrmC5USE6VxQftHmOs/02Keryd168foNjTzkKnq3zHSLiUeNYDRz9iNgOBWa SE8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=GAlir8qlxslU5YRzA5abtvMAyu0JGh/QZsfUpJN/xdc=; fh=NfxQezD5HM8dDGd2brv3UUjjHpqyRN1t8bruD5N33og=; b=y9svz3CONho+YkoxPPbOww9xqk/D1PGHDcowZtMblsWNQPsKPlECZq9XNhPxiF2vkV Jco6tcZF1GK/9D+Zk3ikiEAh9ybWTVoVenSNDM6ljxL6vtH+4krM9uE4gvKcFHedQYQ7 Hlo4slpddV58fq+ElqFQHI684pCday3VcnyT6zP7bWQfM86Vx4Ewj5vWb3gNZDfOPxny x9Vhha670U+W2iQtwM1QsZRKQGEgX17qdIkCBZK31HSyJvGIIC5kR82kA4JNZEFDRJif 5E/QP7x74Wh9YpD82NkG1sz0dUaDEauYS7lbhaKljoOk2Cc1IxQXlC4x0vwtnLbrguFQ LNbg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id p7-20020a170902bd0700b001ce5ba27888si9570518pls.211.2023.11.21.01.54.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 01:54:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 7C1C0803B730; Tue, 21 Nov 2023 01:54:43 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234128AbjKUJy0 (ORCPT + 99 others); Tue, 21 Nov 2023 04:54:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234111AbjKUJyZ (ORCPT ); Tue, 21 Nov 2023 04:54:25 -0500 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 200C5126; Tue, 21 Nov 2023 01:54:18 -0800 (PST) Received: from loongson.cn (unknown [10.20.42.183]) by gateway (Coremail) with SMTP id _____8AxJuhHflxl_4M7AA--.59056S3; Tue, 21 Nov 2023 17:54:15 +0800 (CST) Received: from [10.20.42.183] (unknown [10.20.42.183]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxzdxCflxl80tIAA--.27825S3; Tue, 21 Nov 2023 17:54:12 +0800 (CST) Subject: Re: [PATCH v1 1/2] LoongArch: KVM: Add lsx support To: WANG Xuerui , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Huacai Chen , Greg Kroah-Hartman , loongarch@lists.linux.dev, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao References: <20231115091921.85516-1-zhaotianrui@loongson.cn> <20231115091921.85516-2-zhaotianrui@loongson.cn> <28c11122-4433-4302-bb09-95b3d458b457@xen0n.name> From: zhaotianrui Message-ID: Date: Tue, 21 Nov 2023 17:56:33 +0800 User-Agent: Mozilla/5.0 (X11; Linux loongarch64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <28c11122-4433-4302-bb09-95b3d458b457@xen0n.name> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-CM-TRANSID: AQAAf8DxzdxCflxl80tIAA--.27825S3 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBj93XoW3CF4rGr43AF1xAw4DAFWkGrX_yoWDuFy3pr ykArZ8JrWUGrn3tr1UJr1DXFy5Zr18Kw17XFy8XFy5JF1Utryjqr18WrWqgFyUJw48JF1I qF18XrnxZFyUJ3cCm3ZEXasCq-sJn29KB7ZKAUJUUUUf529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUPIb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUAVWUtwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU AVWUtwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcVAKI4 8JMxk0xIA0c2IEe2xFo4CEbIxvr21lc7CjxVAaw2AFwI0_JF0_Jw1l42xK82IYc2Ij64vI r41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_JF0_Jw1lx2IqxVAqx4xG67 AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIY rxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF7I0E14 v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8 JwCI42IY6I8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07j5o7tUUU UU= X-Spam-Status: No, score=-2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, 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 fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 21 Nov 2023 01:54:43 -0800 (PST) 在 2023/11/16 下午3:15, WANG Xuerui 写道: > > On 11/15/23 17:19, Tianrui Zhao wrote: >> This patch adds LSX support for LoongArch KVM. The LSX means >> LoongArch 128-bits vector instruction. > Maybe we don't need to explain what "LSX" is; people working on > LoongArch kernel should already know this through the kernel docs and > the various occurrences in the code. Thanks, I will remove the explain about "LSX, LASX". >> There will be LSX exception in KVM when guest use the LSX >> instruction. KVM will enable LSX and restore the vector >> registers for guest then return to guest to continue running. >> > One more extra line that should get removed. Thanks, I will remove the extra line. >> Signed-off-by: Tianrui Zhao >> --- >>   arch/loongarch/include/asm/kvm_host.h |  6 ++++ >>   arch/loongarch/include/asm/kvm_vcpu.h | 12 +++++++ >>   arch/loongarch/kvm/exit.c             | 18 ++++++++++ >>   arch/loongarch/kvm/switch.S           | 22 +++++++++++++ >>   arch/loongarch/kvm/trace.h            |  4 ++- >>   arch/loongarch/kvm/vcpu.c             | 47 +++++++++++++++++++++++++-- >>   6 files changed, 105 insertions(+), 4 deletions(-) >> >> diff --git a/arch/loongarch/include/asm/kvm_host.h >> b/arch/loongarch/include/asm/kvm_host.h >> index 11328700d4..6c65c25169 100644 >> --- a/arch/loongarch/include/asm/kvm_host.h >> +++ b/arch/loongarch/include/asm/kvm_host.h >> @@ -94,6 +94,7 @@ enum emulation_result { >>   #define KVM_LARCH_FPU        (0x1 << 0) >>   #define KVM_LARCH_SWCSR_LATEST    (0x1 << 1) >>   #define KVM_LARCH_HWCSR_USABLE    (0x1 << 2) >> +#define KVM_LARCH_LSX        (0x1 << 3) >>     struct kvm_vcpu_arch { >>       /* >> @@ -175,6 +176,11 @@ static inline void writel_sw_gcsr(struct >> loongarch_csrs *csr, int reg, unsigned >>       csr->csrs[reg] = val; >>   } >>   +static inline bool kvm_guest_has_lsx(struct kvm_vcpu_arch *arch) >> +{ >> +    return arch->cpucfg[2] & CPUCFG2_LSX; >> +} >> + >>   /* Debug: dump vcpu state */ >>   int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu); >>   diff --git a/arch/loongarch/include/asm/kvm_vcpu.h >> b/arch/loongarch/include/asm/kvm_vcpu.h >> index 553cfa2b2b..c629771e12 100644 >> --- a/arch/loongarch/include/asm/kvm_vcpu.h >> +++ b/arch/loongarch/include/asm/kvm_vcpu.h >> @@ -55,6 +55,18 @@ void kvm_save_fpu(struct loongarch_fpu *fpu); >>   void kvm_restore_fpu(struct loongarch_fpu *fpu); >>   void kvm_restore_fcsr(struct loongarch_fpu *fpu); >>   +#ifdef CONFIG_CPU_HAS_LSX >> +void kvm_own_lsx(struct kvm_vcpu *vcpu); >> +void kvm_save_lsx(struct loongarch_fpu *fpu); >> +void kvm_restore_lsx(struct loongarch_fpu *fpu); >> +void kvm_restore_lsx_upper(struct loongarch_fpu *fpu); >> +#else >> +static inline void kvm_own_lsx(struct kvm_vcpu *vcpu) { } >> +static inline void kvm_save_lsx(struct loongarch_fpu *fpu) { } >> +static inline void kvm_restore_lsx(struct loongarch_fpu *fpu) { } >> +static inline void kvm_restore_lsx_upper(struct loongarch_fpu *fpu) { } >> +#endif >> + >>   void kvm_acquire_timer(struct kvm_vcpu *vcpu); >>   void kvm_init_timer(struct kvm_vcpu *vcpu, unsigned long hz); >>   void kvm_reset_timer(struct kvm_vcpu *vcpu); >> diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c >> index ce8de3fa47..1b1c58ccc8 100644 >> --- a/arch/loongarch/kvm/exit.c >> +++ b/arch/loongarch/kvm/exit.c >> @@ -659,6 +659,23 @@ static int kvm_handle_fpu_disabled(struct >> kvm_vcpu *vcpu) >>       return RESUME_GUEST; >>   } >>   +/* >> + * kvm_handle_lsx_disabled() - Guest used LSX while disabled in root. >> + * @vcpu:      Virtual CPU context. >> + * >> + * Handle when the guest attempts to use LSX when it is disabled in >> the root >> + * context. >> + */ >> +static int kvm_handle_lsx_disabled(struct kvm_vcpu *vcpu) >> +{ >> +    if (!kvm_guest_has_lsx(&vcpu->arch)) >> +        kvm_queue_exception(vcpu, EXCCODE_INE, 0); >> +    else >> +        kvm_own_lsx(vcpu); >> + >> +    return RESUME_GUEST; >> +} >> + >>   /* >>    * LoongArch KVM callback handling for unimplemented guest exiting >>    */ >> @@ -687,6 +704,7 @@ static exit_handle_fn >> kvm_fault_tables[EXCCODE_INT_START] = { >>       [EXCCODE_TLBS]            = kvm_handle_write_fault, >>       [EXCCODE_TLBM]            = kvm_handle_write_fault, >>       [EXCCODE_FPDIS]            = kvm_handle_fpu_disabled, >> +    [EXCCODE_LSXDIS]                = kvm_handle_lsx_disabled, >>       [EXCCODE_GSPR]            = kvm_handle_gspr, >>   }; >>   diff --git a/arch/loongarch/kvm/switch.S b/arch/loongarch/kvm/switch.S >> index 0ed9040307..32ba092a44 100644 >> --- a/arch/loongarch/kvm/switch.S >> +++ b/arch/loongarch/kvm/switch.S >> @@ -245,6 +245,28 @@ SYM_FUNC_START(kvm_restore_fpu) >>       jr                 ra >>   SYM_FUNC_END(kvm_restore_fpu) >>   +#ifdef CONFIG_CPU_HAS_LSX >> +SYM_FUNC_START(kvm_save_lsx) >> +    fpu_save_csr    a0 t1 >> +    fpu_save_cc     a0 t1 t2 >> +    lsx_save_data   a0 t1 >> +    jirl            zero, ra, 0 > "jr ra" for consistency (e.g. with the function immediately above); > similarly for other such usages. Thanks, I will use "jr ra" there. Tianrui Zhao >> +SYM_FUNC_END(kvm_save_lsx) >> + >> +SYM_FUNC_START(kvm_restore_lsx) >> +    lsx_restore_data a0 t1 >> +    fpu_restore_cc   a0 t1 t2 >> +    fpu_restore_csr  a0 t1 >> +    jirl             zero, ra, 0 >> +SYM_FUNC_END(kvm_restore_lsx) >> + >> +SYM_FUNC_START(kvm_restore_lsx_upper) >> +    lsx_restore_all_upper a0 t0 t1 >> + >> +    jirl                  zero, ra, 0 >> +SYM_FUNC_END(kvm_restore_lsx_upper) >> +#endif >> + >>       .section ".rodata" >>   SYM_DATA(kvm_exception_size, .quad kvm_exc_entry_end - kvm_exc_entry) >>   SYM_DATA(kvm_enter_guest_size, .quad kvm_enter_guest_end - >> kvm_enter_guest) >> diff --git a/arch/loongarch/kvm/trace.h b/arch/loongarch/kvm/trace.h >> index a1e35d6554..7da4e230e8 100644 >> --- a/arch/loongarch/kvm/trace.h >> +++ b/arch/loongarch/kvm/trace.h >> @@ -102,6 +102,7 @@ TRACE_EVENT(kvm_exit_gspr, >>   #define KVM_TRACE_AUX_DISCARD        4 >>     #define KVM_TRACE_AUX_FPU        1 >> +#define KVM_TRACE_AUX_LSX        2 >>     #define kvm_trace_symbol_aux_op                \ >>       { KVM_TRACE_AUX_SAVE,        "save" },    \ >> @@ -111,7 +112,8 @@ TRACE_EVENT(kvm_exit_gspr, >>       { KVM_TRACE_AUX_DISCARD,    "discard" } >>     #define kvm_trace_symbol_aux_state            \ >> -    { KVM_TRACE_AUX_FPU,     "FPU" } >> +    { KVM_TRACE_AUX_FPU,     "FPU" },        \ >> +    { KVM_TRACE_AUX_LSX,     "LSX" } >>     TRACE_EVENT(kvm_aux, >>           TP_PROTO(struct kvm_vcpu *vcpu, unsigned int op, >> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c >> index 73d0c2b9c1..f0bb583353 100644 >> --- a/arch/loongarch/kvm/vcpu.c >> +++ b/arch/loongarch/kvm/vcpu.c >> @@ -378,9 +378,13 @@ static int kvm_set_one_reg(struct kvm_vcpu *vcpu, >>           break; >>       case KVM_REG_LOONGARCH_CPUCFG: >>           id = KVM_GET_IOC_CPUCFG_IDX(reg->id); >> -        if (id >= 0 && id < KVM_MAX_CPUCFG_REGS) >> +        if (id >= 0 && id < KVM_MAX_CPUCFG_REGS) { >>               vcpu->arch.cpucfg[id] = (u32)v; >> -        else >> +            if (id == 2 && v & CPUCFG2_LSX && !cpu_has_lsx) { >> +                vcpu->arch.cpucfg[id] &= ~CPUCFG2_LSX; >> +                ret = -EINVAL; >> +            } >> +        } else >>               ret = -EINVAL; >>           break; >>       case KVM_REG_LOONGARCH_KVM: >> @@ -561,12 +565,49 @@ void kvm_own_fpu(struct kvm_vcpu *vcpu) >>       preempt_enable(); >>   } >>   +#ifdef CONFIG_CPU_HAS_LSX >> +/* Enable LSX for guest and restore context */ >> +void kvm_own_lsx(struct kvm_vcpu *vcpu) >> +{ >> +    preempt_disable(); >> + >> +    /* Enable LSX for guest */ >> +    set_csr_euen(CSR_EUEN_LSXEN | CSR_EUEN_FPEN); >> +    switch (vcpu->arch.aux_inuse & KVM_LARCH_FPU) { >> +    case KVM_LARCH_FPU: >> +        /* >> +         * Guest FPU state already loaded, >> +         * only restore upper LSX state >> +         */ >> +        kvm_restore_lsx_upper(&vcpu->arch.fpu); >> +        break; >> +    default: >> +        /* Neither FP or LSX already active, >> +         * restore full LSX state >> +         */ >> +        kvm_restore_lsx(&vcpu->arch.fpu); >> +    break; >> +    } >> + >> +    trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_LSX); >> +    vcpu->arch.aux_inuse |= KVM_LARCH_LSX | KVM_LARCH_FPU; >> +    preempt_enable(); >> +} >> +#endif >> + >>   /* Save context and disable FPU */ >>   void kvm_lose_fpu(struct kvm_vcpu *vcpu) >>   { >>       preempt_disable(); >>   -    if (vcpu->arch.aux_inuse & KVM_LARCH_FPU) { >> +    if (vcpu->arch.aux_inuse & KVM_LARCH_LSX) { >> +        kvm_save_lsx(&vcpu->arch.fpu); >> +        vcpu->arch.aux_inuse &= ~(KVM_LARCH_LSX | KVM_LARCH_FPU); >> +        trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_LSX); >> + >> +        /* Disable LSX & FPU */ >> +        clear_csr_euen(CSR_EUEN_FPEN | CSR_EUEN_LSXEN); >> +    } else if (vcpu->arch.aux_inuse & KVM_LARCH_FPU) { >>           kvm_save_fpu(&vcpu->arch.fpu); >>           vcpu->arch.aux_inuse &= ~KVM_LARCH_FPU; >>           trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU); >