Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp5141236rwe; Tue, 18 Apr 2023 02:38:30 -0700 (PDT) X-Google-Smtp-Source: AKy350aaZK/bnpgBZmnL2r4YOU8ElzMkp1MDnJ6w18rOpSHiMN10I6w1a6Zd7BT7h6/XRYQFIBPN X-Received: by 2002:a17:90a:2f03:b0:246:d182:cb48 with SMTP id s3-20020a17090a2f0300b00246d182cb48mr1361606pjd.23.1681810709995; Tue, 18 Apr 2023 02:38:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681810709; cv=none; d=google.com; s=arc-20160816; b=WVs5Y4Ee57TKsg6RegL6dMUdy6Xdhol7378S30XsLsu7ZYe7GfVZsWDoiu6+WcBguW 0Mrj92hUQeN02b8+hX0+CxCwoq8QTsKN7HssgkHoavzCSVgxlTSAEC6JcwovGYZWY0NR whqRpFwdk8xQ4Xocyd79gEuRHtdTZhj5I/gVGlaI7m7YmNccwJNw3j7hKVHJ8XHwTUXS l99/rRAUmdPDkW3yPetAUdpLG7RExWfdLxBH3bgYMAIzKROwO81o6GjLaNN6/vx9wi1j 4TWlHuYchHAo1zOxC2/6uvh/Nr9PWBt0H8vIYwqdojq95Mu6DpwY4ZGUj3j93cPvxZIM PIwA== 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 :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=HNaPxGigZLTcJPblDQSnrkUKxRp8jyFFzanu8wq9qaI=; b=enBz9eEEYlRp43LobyT87ZYNvldQZoZlarQiMD4dVb+nXXCib1lEosbRGKWGZA6eX3 UlFQB3eul2QcT5OuyuqRSjgINO0gfTcOY9pJYGNm8bxNfeDANLFTy4qK7Fmrk6NU3IXI oVT5HNzuxLxGZcF7icDepaVJFZnlXKwQ3umVpOiNB/obHjft7c8cB12hMdGVZtbjiVTk 2V4MO7p4lR1M5qsF+A9r/+TMHdHfz10xWyXbD1mU57Y9BlyeXnL7q/3xKrEu2idUS9ss +AloSA10W6ICO66Su5fG/CPq5kLfj4xfR4CLRgzA32cjhbfVps393Qs4bzzr59gwBffn ruqQ== 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:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 78-20020a630151000000b0051374678f95si14440351pgb.808.2023.04.18.02.38.15; Tue, 18 Apr 2023 02:38:29 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231192AbjDRJUK (ORCPT + 99 others); Tue, 18 Apr 2023 05:20:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229833AbjDRJUI (ORCPT ); Tue, 18 Apr 2023 05:20:08 -0400 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E8D88103; Tue, 18 Apr 2023 02:20:04 -0700 (PDT) Received: from loongson.cn (unknown [10.20.42.120]) by gateway (Coremail) with SMTP id _____8DxAf_DYD5kL2EeAA--.47650S3; Tue, 18 Apr 2023 17:20:03 +0800 (CST) Received: from [10.20.42.120] (unknown [10.20.42.120]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Dxib2_YD5kJq4sAA--.51467S3; Tue, 18 Apr 2023 17:20:00 +0800 (CST) Subject: Re: [PATCH v7 00/30] Add KVM LoongArch support To: Paolo Bonzini References: <20230417094649.874671-1-zhaotianrui@loongson.cn> Cc: Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao From: Tianrui Zhao Message-ID: <4cf93458-e5e1-d02d-4728-c2ddb4ea2088@loongson.cn> Date: Tue, 18 Apr 2023 17:19:59 +0800 User-Agent: Mozilla/5.0 (X11; Linux mips64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <20230417094649.874671-1-zhaotianrui@loongson.cn> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Dxib2_YD5kJq4sAA--.51467S3 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoW3KF47Ar4fGF1rCFykZw1fJFb_yoWDAFWxpF W7urn8Gr4kGrsaqws3t34kurn8Xr1xGrWag3Wa9348CrW2qrykZFykKr9FvFZrZ3yrJr10 qr1rKw1ag3WUAaDanT9S1TB71UUUUjJqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU bqxYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_Jrv_JF1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4 x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4UJVWxJr1l n4kS14v26r126r1DM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6x ACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r126r1DMcIj6I8E 87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lc7I2V7IY0V AS07AlzVAYIcxG8wCY1x0262kKe7AKxVWUAVWUtwCF04k20xvY0x0EwIxGrwCFx2IqxVCF s4IE7xkEbVWUJVW8JwCFI7km07C267AKxVWUAVWUtwC20s026c02F40E14v26r1j6r18MI 8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41l IxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr1lIx AIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2 jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU8Dl1DUUUUU== X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,NICE_REPLY_A, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Please ignore this mail message, as it is not a complete patch series. Please see the next V7 patches I have sent. Thanks Tianrui Zhao 在 2023年04月17日 17:46, Tianrui Zhao 写道: > This series adds KVM LoongArch support. Loongson 3A5000 supports hardware > assisted virtualization. With cpu virtualization, there are separate > hw-supported user mode and kernel mode in guest mode. With memory > virtualization, there are two-level hw mmu table for guest mode and host > mode. Also there is separate hw cpu timer with consant frequency in > guest mode, so that vm can migrate between hosts with different freq. > Currently, we are able to boot LoongArch Linux Guests. > > Few key aspects of KVM LoongArch added by this series are: > 1. Enable kvm hardware function when kvm module is loaded. > 2. Implement VM and vcpu related ioctl interface such as vcpu create, > vcpu run etc. GET_ONE_REG/SET_ONE_REG ioctl commands are use to > get general registers one by one. > 3. Hardware access about MMU, timer and csr are emulated in kernel. > 4. Hardwares such as mmio and iocsr device are emulated in user space > such as APIC, IPI, pci devices etc. > > The running environment of LoongArch virt machine: > 1. Cross tools to build kernel and uefi: > $ wget https://github.com/loongson/build-tools/releases/download/2022.09.06/loongarch64-clfs-6.3-cross-tools-gcc-glibc.tar.xz > tar -vxf loongarch64-clfs-6.3-cross-tools-gcc-glibc.tar.xz -C /opt > export PATH=/opt/cross-tools/bin:$PATH > export LD_LIBRARY_PATH=/opt/cross-tools/lib:$LD_LIBRARY_PATH > export LD_LIBRARY_PATH=/opt/cross-tools/loongarch64-unknown-linux-gnu/lib/:$LD_LIBRARY_PATH > 2. This series is based on the linux source code: > https://github.com/loongson/linux-loongarch-kvm > Build command: > git checkout kvm-loongarch > make ARCH=loongarch CROSS_COMPILE=loongarch64-unknown-linux-gnu- loongson3_defconfig > make ARCH=loongarch CROSS_COMPILE=loongarch64-unknown-linux-gnu- > 3. QEMU hypervisor with LoongArch supported: > https://github.com/loongson/qemu > Build command: > git checkout kvm-loongarch > ./configure --target-list="loongarch64-softmmu" --enable-kvm > make > 4. Uefi bios of LoongArch virt machine: > Link: https://github.com/tianocore/edk2-platforms/tree/master/Platform/Loongson/LoongArchQemuPkg#readme > 5. you can also access the binary files we have already build: > https://github.com/yangxiaojuan-loongson/qemu-binary > The command to boot loongarch virt machine: > $ qemu-system-loongarch64 -machine virt -m 4G -cpu la464 \ > -smp 1 -bios QEMU_EFI.fd -kernel vmlinuz.efi -initrd ramdisk \ > -serial stdio -monitor telnet:localhost:4495,server,nowait \ > -append "root=/dev/ram rdinit=/sbin/init console=ttyS0,115200" \ > --nographic > > Changes for v7: > 1. Fix the kvm_save/restore_hw_gcsr compiling warnings reported by > kernel test robot. The report link is: > https://lore.kernel.org/oe-kbuild-all/202304131526.iXfLaVZc-lkp@intel.com/ > 2. Fix loongarch kvm trace related compiling problems. > > Changes for v6: > 1. Fix the Documentation/virt/kvm/api.rst compile warning about > loongarch parts. > > Changes for v5: > 1. Implement get/set mp_state ioctl interface, and only the > KVM_MP_STATE_RUNNABLE state is supported now, and other states > will be completed in the future. The state is also used when vcpu > run idle instruction, if vcpu state is changed to RUNNABLE, the > vcpu will have the possibility to be woken up. > 2. Supplement kvm document about loongarch-specific part, such as add > api introduction for GET/SET_ONE_REG, GET/SET_FPU, GET/SET_MP_STATE, > etc. > 3. Improve the kvm_switch_to_guest function in switch.S, remove the > previous tmp,tmp1 arguments and replace it with t0,t1 reg. > > Changes for v4: > 1. Add a csr_need_update flag in _vcpu_put, as most csr registers keep > unchanged during process context switch, so we need not to update it > every time. We can do this only if the soft csr is different form hardware. > That is to say all of csrs should update after vcpu enter guest, as for > set_csr_ioctl, we have written soft csr to keep consistent with hardware. > 2. Improve get/set_csr_ioctl interface, we set SW or HW or INVALID flag > for all csrs according to it's features when kvm init. In get/set_csr_ioctl, > if csr is HW, we use gcsrrd/ gcsrwr instruction to access it, else if csr is > SW, we use software to emulate it, and others return false. > 3. Add set_hw_gcsr function in csr_ops.S, and it is used in set_csr_ioctl. > We have splited hw gcsr into three parts, so we can calculate the code offset > by gcsrid and jump here to run the gcsrwr instruction. We use this function to > make the code easier and avoid to use the previous SET_HW_GCSR(XXX) interface. > 4. Improve kvm mmu functions, such as flush page table and make clean page table > interface. > > Changes for v3: > 1. Remove the vpid array list in kvm_vcpu_arch and use a vpid variable here, > because a vpid will never be recycled if a vCPU migrates from physical CPU A > to B and back to A. > 2. Make some constant variables in kvm_context to global such as vpid_mask, > guest_eentry, enter_guest, etc. > 3. Add some new tracepoints, such as kvm_trace_idle, kvm_trace_cache, > kvm_trace_gspr, etc. > 4. There are some duplicate codes in kvm_handle_exit and kvm_vcpu_run, > so we move it to a new function kvm_pre_enter_guest. > 5. Change the RESUME_HOST, RESUME_GUEST value, return 1 for resume guest > and "<= 0" for resume host. > 6. Fcsr and fpu registers are saved/restored together. > > Changes for v2: > 1. Seprate the original patch-01 and patch-03 into small patches, and the > patches mainly contain kvm module init, module exit, vcpu create, vcpu run, > etc. > 2. Remove the original KVM_{GET,SET}_CSRS ioctl in the kvm uapi header, > and we use the common KVM_{GET,SET}_ONE_REG to access register. > 3. Use BIT(x) to replace the "1 << n_bits" statement. > > Tianrui Zhao (30): > LoongArch: KVM: Add kvm related header files > LoongArch: KVM: Implement kvm module related interface > LoongArch: KVM: Implement kvm hardware enable, disable interface > LoongArch: KVM: Implement VM related functions > LoongArch: KVM: Add vcpu related header files > LoongArch: KVM: Implement vcpu create and destroy interface > LoongArch: KVM: Implement vcpu run interface > LoongArch: KVM: Implement vcpu handle exit interface > LoongArch: KVM: Implement vcpu get, vcpu set registers > LoongArch: KVM: Implement vcpu ENABLE_CAP ioctl interface > LoongArch: KVM: Implement fpu related operations for vcpu > LoongArch: KVM: Implement vcpu interrupt operations > LoongArch: KVM: Implement misc vcpu related interfaces > LoongArch: KVM: Implement vcpu load and vcpu put operations > LoongArch: KVM: Implement vcpu status description > LoongArch: KVM: Implement update VM id function > LoongArch: KVM: Implement virtual machine tlb operations > LoongArch: KVM: Implement vcpu timer operations > LoongArch: KVM: Implement kvm mmu operations > LoongArch: KVM: Implement handle csr excption > LoongArch: KVM: Implement handle iocsr exception > LoongArch: KVM: Implement handle idle exception > LoongArch: KVM: Implement handle gspr exception > LoongArch: KVM: Implement handle mmio exception > LoongArch: KVM: Implement handle fpu exception > LoongArch: KVM: Implement kvm exception vector > LoongArch: KVM: Implement vcpu world switch > LoongArch: KVM: Implement probe virtualization when loongarch cpu init > LoongArch: KVM: Enable kvm config and add the makefile > LoongArch: KVM: Supplement kvm document about loongarch-specific part > > Documentation/virt/kvm/api.rst | 71 +- > arch/loongarch/Kbuild | 1 + > arch/loongarch/Kconfig | 2 + > arch/loongarch/configs/loongson3_defconfig | 2 + > arch/loongarch/include/asm/cpu-features.h | 22 + > arch/loongarch/include/asm/cpu-info.h | 13 + > arch/loongarch/include/asm/inst.h | 16 + > arch/loongarch/include/asm/kvm_csr.h | 58 ++ > arch/loongarch/include/asm/kvm_host.h | 268 +++++++ > arch/loongarch/include/asm/kvm_types.h | 11 + > arch/loongarch/include/asm/kvm_vcpu.h | 114 +++ > arch/loongarch/include/asm/loongarch.h | 213 ++++- > arch/loongarch/include/uapi/asm/kvm.h | 107 +++ > arch/loongarch/kernel/asm-offsets.c | 32 + > arch/loongarch/kernel/cpu-probe.c | 53 ++ > arch/loongarch/kvm/Kconfig | 38 + > arch/loongarch/kvm/Makefile | 22 + > arch/loongarch/kvm/csr_ops.S | 76 ++ > arch/loongarch/kvm/exit.c | 707 +++++++++++++++++ > arch/loongarch/kvm/interrupt.c | 126 +++ > arch/loongarch/kvm/main.c | 340 ++++++++ > arch/loongarch/kvm/mmu.c | 730 +++++++++++++++++ > arch/loongarch/kvm/switch.S | 301 +++++++ > arch/loongarch/kvm/timer.c | 266 +++++++ > arch/loongarch/kvm/tlb.c | 31 + > arch/loongarch/kvm/trace.h | 168 ++++ > arch/loongarch/kvm/vcpu.c | 882 +++++++++++++++++++++ > arch/loongarch/kvm/vm.c | 79 ++ > arch/loongarch/kvm/vmid.c | 65 ++ > include/uapi/linux/kvm.h | 9 + > 30 files changed, 4808 insertions(+), 15 deletions(-) > create mode 100644 arch/loongarch/include/asm/kvm_csr.h > create mode 100644 arch/loongarch/include/asm/kvm_host.h > create mode 100644 arch/loongarch/include/asm/kvm_types.h > create mode 100644 arch/loongarch/include/asm/kvm_vcpu.h > create mode 100644 arch/loongarch/include/uapi/asm/kvm.h > create mode 100644 arch/loongarch/kvm/Kconfig > create mode 100644 arch/loongarch/kvm/Makefile > create mode 100644 arch/loongarch/kvm/csr_ops.S > create mode 100644 arch/loongarch/kvm/exit.c > create mode 100644 arch/loongarch/kvm/interrupt.c > create mode 100644 arch/loongarch/kvm/main.c > create mode 100644 arch/loongarch/kvm/mmu.c > create mode 100644 arch/loongarch/kvm/switch.S > create mode 100644 arch/loongarch/kvm/timer.c > create mode 100644 arch/loongarch/kvm/tlb.c > create mode 100644 arch/loongarch/kvm/trace.h > create mode 100644 arch/loongarch/kvm/vcpu.c > create mode 100644 arch/loongarch/kvm/vm.c > create mode 100644 arch/loongarch/kvm/vmid.c >