From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E1C8F2BB17; Mon, 26 Jan 2026 04:00:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769400027; cv=none; b=ZxgvJ2+G0DlqAZu67lCmcxTIoNa03F8ik2zgo4suTVrwwcy16Xr1rwTXeC9KkPgTpa8VQP1F7I2HLoZwx4BtoZO/sEgdHoapNDmrBm2mdzwPZZ8TwuESD0s+/G4Bew5PfrvJzW3dLeT/o3xZ1nNToKySk0RjVtHrQsBBbKMPiPI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769400027; c=relaxed/simple; bh=r/hU7H+WlM01gB6KHVku+qtCFvFZp5WiIiuu9jeoop4=; h=Subject:To:Cc:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=ZzG6b1f5JDTGxJvA+Ka438EzpwkJq0VZwBceVHv4FrS8kMcCs3dqXxPVOQ9tXvtTaLNmPjqAgtbwiO6hoAGIDP87fFZSyzgsEz6XgjU62eXvnuFG44HdxXPgNegmXb+ewJiCVwAtMjKJZ91W1lqNVBXdJ1/Fg1cfDyfOwWQ22tk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.20.42.62]) by gateway (Coremail) with SMTP id _____8BxHMPU5nZpWJoMAA--.40831S3; Mon, 26 Jan 2026 12:00:20 +0800 (CST) Received: from [10.20.42.62] (unknown [10.20.42.62]) by front1 (Coremail) with SMTP id qMiowJCx+8HF5nZpb1kxAA--.31227S3; Mon, 26 Jan 2026 12:00:07 +0800 (CST) Subject: Re: [PATCH 1/1] KVM: Add KVM_GET_REG_LIST ioctl for LoongArch To: liushuyu , WANG Xuerui , Huacai Chen Cc: Kexy Biscuit , Mingcong Bai , Paolo Bonzini , Jonathan Corbet , Tianrui Zhao , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=c3=b6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-riscv@lists.infradead.org, rust-for-linux@vger.kernel.org References: <20260125054322.1237687-1-liushuyu@aosc.io> <4b504274-4241-0e3e-3ed3-7804b72b7ee8@loongson.cn> <972430be-d2f3-49b6-851b-a057ddfcafec@aosc.io> From: Bibo Mao Message-ID: <29565e27-f153-c2c5-cbc3-e0457d45f094@loongson.cn> Date: Mon, 26 Jan 2026 11:57:31 +0800 User-Agent: Mozilla/5.0 (X11; Linux loongarch64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <972430be-d2f3-49b6-851b-a057ddfcafec@aosc.io> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-CM-TRANSID:qMiowJCx+8HF5nZpb1kxAA--.31227S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3GFW5ZryfGrWkCF1fAw1fuFX_yoW7trWkpr ykAFW5Jry5Jrn3Jr1jgw15WFyUAr18J3ZrZrn3XF18ArsFyr12gr18WryqgF1UJ3y8JF40 vr1UXrnxZ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o, >> >> >> On 2026/1/25 下午1:43, Zixing Liu wrote: >>> This ioctl can be used by userspace applications to determine which >>> (special) registers are get/set-able. >>> >>> This can be very useful for cross-platform VMMs so that they do not have >>> to hardcode register indices for each supported architectures. >>> >>> Signed-off-by: Zixing Liu >>> --- >>> >>> For example, this ioctl could be used by rust-vmm/rust-kvm or maybe >>> VirtualBox-kvm in the future. >>> >>>   Documentation/virt/kvm/api.rst |  2 +- >>>   arch/loongarch/kvm/vcpu.c      | 69 ++++++++++++++++++++++++++++++++++ >>>   2 files changed, 70 insertions(+), 1 deletion(-) >>> >>> diff --git a/Documentation/virt/kvm/api.rst >>> b/Documentation/virt/kvm/api.rst >>> index 01a3abef8abb..f46dd8be282f 100644 >>> --- a/Documentation/virt/kvm/api.rst >>> +++ b/Documentation/virt/kvm/api.rst >>> @@ -3603,7 +3603,7 @@ VCPU matching underlying host. >>>   --------------------- >>>     :Capability: basic >>> -:Architectures: arm64, mips, riscv, x86 (if KVM_CAP_ONE_REG) >>> +:Architectures: arm64, loongarch, mips, riscv, x86 (if KVM_CAP_ONE_REG) >>>   :Type: vcpu ioctl >>>   :Parameters: struct kvm_reg_list (in/out) >>>   :Returns: 0 on success; -1 on error >>> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c >>> index 656b954c1134..b884eb9c76aa 100644 >>> --- a/arch/loongarch/kvm/vcpu.c >>> +++ b/arch/loongarch/kvm/vcpu.c >>> @@ -1186,6 +1186,57 @@ static int kvm_loongarch_vcpu_set_attr(struct >>> kvm_vcpu *vcpu, >>>       return ret; >>>   } >>>   +static unsigned long kvm_loongarch_num_lbt_regs(void) >>> +{ >>> +    /* +1 for the LBT_FTOP flag (inside arch.fpu) */ >>> +    return sizeof(struct loongarch_lbt) / sizeof(unsigned long) + 1; >>> +} >>> + >>> +static unsigned long kvm_loongarch_num_regs(struct kvm_vcpu *vcpu) >>> +{ >>> +    /* +1 for the KVM_REG_LOONGARCH_COUNTER register */ >>> +    unsigned long res = CSR_MAX_NUMS + KVM_MAX_CPUCFG_REGS + 1; >>> + >>> +    if (kvm_guest_has_lbt(&vcpu->arch)) >>> +        res += kvm_loongarch_num_lbt_regs(); >>> + >>> +    return res; >>> +} >>> + >>> +static int kvm_loongarch_copy_reg_indices(struct kvm_vcpu *vcpu, >>> +                      u64 __user *uindices) >>> +{ >>> +    u64 reg; >>> +    unsigned int i; >>> + >>> +    for (i = 0; i < CSR_MAX_NUMS; i++) { >>> +        reg = KVM_IOC_CSRID(i); >>> +        if (put_user(reg, uindices++)) >>> +            return -EFAULT; >>> +    } >> CSR_MAX_NUMS is max number of accessible CSR registers, instead only >> part of them is used by vCPU model. By my understanding, there will be >> no much meaning if CSR_MAX_NUMS is returned. And I think it will be >> better if real CSR register id and number is returned. >> > Did you mean we should only return the CSR registers initialized in this > function > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/loongarch/kvm/main.c?id=63804fed149a6750ffd28610c5c1c98cce6bd377#n48 > ? > > That looks like a very large list and the values of the register IDs are > not fully continuous. If that is the case, how do we maintain the > get/set-able CSR register list? It will be better if CSR register list can be categorized, for example with LoongArch CPU manual CSR register is split into 8 types from chapter 7.4 -- 7.11 At the beginning, there is big array with size CSR_MAX_NUMS without any category, it can be fine-gained in late. Regards Bibo Mao > >> Regards >> Bibo Mao >>> + >>> +    for (i = 0; i < KVM_MAX_CPUCFG_REGS; i++) { >>> +        reg = KVM_IOC_CPUCFG(i); >>> +        if (put_user(reg, uindices++)) >>> +            return -EFAULT; >>> +    } >>> + >>> +    reg = KVM_REG_LOONGARCH_COUNTER; >>> +    if (put_user(reg, uindices++)) >>> +        return -EFAULT; >>> + >>> +    if (!kvm_guest_has_lbt(&vcpu->arch)) >>> +        return 0; >>> + >>> +    for (i = 1; i <= kvm_loongarch_num_lbt_regs(); i++) { >>> +        reg = (KVM_REG_LOONGARCH_LBT | KVM_REG_SIZE_U64 | i); >>> +        if (put_user(reg, uindices++)) >>> +            return -EFAULT; >>> +    } >>> + >>> +    return 0; >>> +} >>> + >>>   long kvm_arch_vcpu_ioctl(struct file *filp, >>>                unsigned int ioctl, unsigned long arg) >>>   { >>> @@ -1251,6 +1302,24 @@ long kvm_arch_vcpu_ioctl(struct file *filp, >>>           r = kvm_loongarch_vcpu_set_attr(vcpu, &attr); >>>           break; >>>       } >>> +    case KVM_GET_REG_LIST: { >>> +        struct kvm_reg_list __user *user_list = argp; >>> +        struct kvm_reg_list reg_list; >>> +        unsigned n; >>> + >>> +        r = -EFAULT; >>> +        if (copy_from_user(®_list, user_list, sizeof(reg_list))) >>> +            break; >>> +        n = reg_list.n; >>> +        reg_list.n = kvm_loongarch_num_regs(vcpu); >>> +        if (copy_to_user(user_list, ®_list, sizeof(reg_list))) >>> +            break; >>> +        r = -E2BIG; >>> +        if (n < reg_list.n) >>> +            break; >>> +        r = kvm_loongarch_copy_reg_indices(vcpu, user_list->reg); >>> +        break; >>> +    } >>>       default: >>>           r = -ENOIOCTLCMD; >>>           break; >>> >> > Thanks, > Zixing >