Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1861025rwd; Fri, 9 Jun 2023 03:21:37 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5x/mBe1mLFfKfmmKeqOlE/A05lxPgxMm13AjgOw0FJA2gp1/T01ajhVWDES1EWNdvWJI+C X-Received: by 2002:a05:6870:a2d5:b0:19f:28ac:d41a with SMTP id w21-20020a056870a2d500b0019f28acd41amr909675oak.8.1686306096982; Fri, 09 Jun 2023 03:21:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686306096; cv=none; d=google.com; s=arc-20160816; b=t0WlOx+ZPqRlPxkYIZp5O9y4eReLw9qrX08+wFVZ4j6pipn/C554+zWt4zB5pXKkKh /5+gbANqBClvKkI7tFMFIC+cyELAK11u7DJ0AGt149bV63aLi887DzDVo+wd54GP+Jfz FFXNjsimFOhkogaah07bPm7hWPq8A8Jxj+C6kJrvGzWUWYAHEbsllXbIJoxA8uMm0BI1 4sO9ZD1MAr7BQ65TJKwQkbVy5PqYbEoy9pYCc3zfcvko7NhHlP/HL4a1fGuy+rntMBlT tzbA6UNoWadxW5lXD0AtBl0YKd4HYPWwgx3wVy8nEp+jrbuS0hOArzNpwAjAUod+og9i zzFg== 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=Frp69ZbPi1MXarTj5o7SqF2CCv/09j+ySVghffdfqx4=; b=oZmDZIbzNa+cl6wuh4dvSi5JGFUQ4LWOXnEherx1khRPOMormun4f0V4ul/f45061z mw5qRTCXBBhx7WDjz7KPUuOqisaaAuanJyOzver04XKBhsRlVbbdgZPgQWM/4SZEUl/h eT94CzSvKLFIdHW2EG3QZimn7JGRN41HvUfhIxaKQHIPxsV3J5Fw7XUndIUuG1y4k256 YRlRJSCP5eFBMPDXJA1Je1ZtpWvnXnQCW7XSGwLjWcmJIObRQj30rWNaVIaIDKtGZkF6 WkzdYvsckWVWIbOHS59BUcW89Zf8e6NowO052n8L87qzqvwjpLcaB/TDYd3G7VB/WR0p XKEA== 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 h191-20020a636cc8000000b00542d3727da9si2471960pgc.464.2023.06.09.03.21.25; Fri, 09 Jun 2023 03:21:36 -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 S241847AbjFIJzW (ORCPT + 99 others); Fri, 9 Jun 2023 05:55:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241954AbjFIJyr (ORCPT ); Fri, 9 Jun 2023 05:54:47 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A5E7E7D86; Fri, 9 Jun 2023 02:46:29 -0700 (PDT) Received: from loongson.cn (unknown [10.40.46.158]) by gateway (Coremail) with SMTP id _____8Cxd+nz9IJkHAIBAA--.1214S3; Fri, 09 Jun 2023 17:46:27 +0800 (CST) Received: from [192.168.124.126] (unknown [10.40.46.158]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxbMrx9IJk5JcKAA--.23909S3; Fri, 09 Jun 2023 17:46:25 +0800 (CST) Subject: Re: [PATCH v13 00/30] Add KVM LoongArch support To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao , tangyouling@loongson.cn References: <20230609090518.2130926-1-zhaotianrui@loongson.cn> From: zhaotianrui Message-ID: <2d02f26a-fba7-0db9-0c0e-ccc267fc696d@loongson.cn> Date: Fri, 9 Jun 2023 17:46:25 +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: <20230609090518.2130926-1-zhaotianrui@loongson.cn> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-CM-TRANSID: AQAAf8DxbMrx9IJk5JcKAA--.23909S3 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBj9fXoW3KF47Ar4fGF17XF18Jw1rKrX_yoW8Jry8Go WfAFWjqr48Kr18u34v9ws8KFWjqFyxCr4rZ39rZa98GF4rJ345KFy3Kw4Yy3WavFn5Wr1U G34UWw4DZrZ7JFn3l-sFpf9Il3svdjkaLaAFLSUrUUUU2b8apTn2vfkv8UJUUUU8wcxFpf 9Il3svdxBIdaVrn0xqx4xG64xvF2IEw4CE5I8CrVC2j2Jv73VFW2AGmfu7bjvjm3AaLaJ3 UjIYCTnIWjp_UUUO07kC6x804xWl14x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI 8IcIk0rVWrJVCq3wAFIxvE14AKwVWUXVWUAwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xG Y2AK021l84ACjcxK6xIIjxv20xvE14v26ryj6F1UM28EF7xvwVC0I7IYx2IY6xkF7I0E14 v26r4j6F4UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIEc7CjxVAF wI0_Cr1j6rxdM2kKe7AKxVW8ZVWrXwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU tVWrXwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcVAKI4 8JMxk0xIA0c2IEe2xFo4CEbIxvr21lc7CjxVAaw2AFwI0_GFv_Wryl42xK82IYc2Ij64vI r41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_GFv_Wrylx2IqxVAqx4xG67 AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIY rxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Gr0_Xr1lIxAIcVC0I7IYx2IY6xkF7I0E14 v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWx JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUD4lkDU UUU X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,NICE_REPLY_A, SPF_HELO_NONE,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 patch series, as it is incomplete. Thanks Tianrui Zhao ?? 2023/6/9 ????5:04, 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 v13: > 1. Remove patch-28 "Implement probe virtualization when cpu init", as the > virtualization information about FPU,PMP,LSX in guest.options,options_dyn > is not used and the gcfg reg value can be read in kvm_hardware_enable, so > remove the previous cpu_probe_lvz function. > 2. Fix vcpu_enable_cap interface, it should return -EINVAL directly, as > FPU cap is enable by default, and do not support any other caps now. > 3. Simplify the jirl instruction with jr when without return addr, > simplify case HW0 ... HW7 statment in interrupt.c > 4. Rename host_stack,host_gp in kvm_vcpu_arch to host_sp,host_tp. > 5. Remove 'cpu' parameter in _kvm_check_requests, as 'cpu' is not used, > and remove 'cpu' parameter in kvm_check_vmid function, as it can get > cpu number by itself. > > Changes for v12: > 1. Improve the gcsr write/read/xchg interface to avoid the previous > instruction statment like parse_r and make the code easy understanding, > they are implemented in asm/insn-def.h and the instructions consistent > of "opcode" "rj" "rd" "simm14" arguments. > 2. Fix the maintainers list of LoongArch KVM. > > Changes for v11: > 1. Add maintainers for LoongArch KVM. > > Changes for v10: > 1. Fix grammatical problems in LoongArch documentation. > 2. It is not necessary to save or restore the LOONGARCH_CSR_PGD when > vcpu put and vcpu load, so we remove it. > > Changes for v9: > 1. Apply the new defined interrupt number macros in loongarch.h to kvm, > such as INT_SWI0, INT_HWI0, INT_TI, INT_IPI, etc. And remove the > previous unused macros. > 2. Remove unused variables in kvm_vcpu_arch, and reorder the variables > to make them more standard. > > Changes for v8: > 1. Adjust the cpu_data.guest.options structure, add the ases flag into > it, and remove the previous guest.ases. We do this to keep consistent > with host cpu_data.options structure. > 2. Remove the "#include " in some files which also > include the "". As linux/kvm_host.h already include > the asm/kvm_host.h. > 3. Fix some unstandard spelling and grammar errors in comments, and > improve a little code format to make it easier and standard. > > 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: Enable kvm config and add the makefile > LoongArch: KVM: Supplement kvm document about LoongArch-specific part > LoongArch: KVM: Add maintainers for LoongArch KVM > > Documentation/virt/kvm/api.rst | 71 +- > MAINTAINERS | 12 + > arch/loongarch/Kbuild | 1 + > arch/loongarch/Kconfig | 2 + > arch/loongarch/configs/loongson3_defconfig | 2 + > arch/loongarch/include/asm/insn-def.h | 55 ++ > arch/loongarch/include/asm/inst.h | 16 + > arch/loongarch/include/asm/kvm_csr.h | 231 ++++++ > arch/loongarch/include/asm/kvm_host.h | 253 ++++++ > arch/loongarch/include/asm/kvm_types.h | 11 + > arch/loongarch/include/asm/kvm_vcpu.h | 97 +++ > arch/loongarch/include/asm/loongarch.h | 20 +- > arch/loongarch/include/uapi/asm/kvm.h | 106 +++ > arch/loongarch/kernel/asm-offsets.c | 32 + > 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 | 113 +++ > arch/loongarch/kvm/main.c | 347 ++++++++ > arch/loongarch/kvm/mmu.c | 725 +++++++++++++++++ > arch/loongarch/kvm/switch.S | 301 +++++++ > arch/loongarch/kvm/timer.c | 266 +++++++ > arch/loongarch/kvm/tlb.c | 32 + > arch/loongarch/kvm/trace.h | 168 ++++ > arch/loongarch/kvm/vcpu.c | 869 +++++++++++++++++++++ > arch/loongarch/kvm/vm.c | 76 ++ > arch/loongarch/kvm/vmid.c | 66 ++ > include/uapi/linux/kvm.h | 9 + > 29 files changed, 4710 insertions(+), 14 deletions(-) > create mode 100644 arch/loongarch/include/asm/insn-def.h > 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 >