From nobody Sun Feb 8 19:48:40 2026 Received: from relay5.mymailcheap.com (relay5.mymailcheap.com [159.100.241.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 692732253A0; Sun, 1 Feb 2026 07:22:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.100.241.64 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769930577; cv=none; b=B/n5dZq1NYqo7oqtg7dMatkQyP1FyjX+9Vaa/1eaUHnasKmbzZUr5j0aUyuCKOk1LoIyyAa0fpDvZ0322/RsX0Sh+EYL8voMHA2oyl+6VSYiuKzTWrSLEaKQCk6Zf4pNZcrO3ergqcj8bSIHKoVQTgXEdBaP8dfB7dkZEHnovj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769930577; c=relaxed/simple; bh=tsYztuBMmDw9dhEyvXm134zQnSNewOglMstDiOxHhEs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=RGJ+OysOQr/DCo2b57osvwF7JFvuIvFZvz3KB5SdaZm7GtGuXgMsQF8moazzTINVXNUAR0EOzAN/BYEsPE+yfylcZnv3Yx062jWWYmIxou1pWMb5JOyXLETJMV5FIIOeVDdhaa1B1kBOE0q0VVoQhNEJrAKKo5tvNssuDselwgM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=aosc.io; spf=pass smtp.mailfrom=aosc.io; dkim=pass (1024-bit key) header.d=aosc.io header.i=@aosc.io header.b=iOhL+zKY; arc=none smtp.client-ip=159.100.241.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=aosc.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aosc.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=aosc.io header.i=@aosc.io header.b="iOhL+zKY" Received: from relay1.mymailcheap.com (relay1.mymailcheap.com [144.217.248.100]) by relay5.mymailcheap.com (Postfix) with ESMTPS id D68EA20229; Sun, 1 Feb 2026 07:22:46 +0000 (UTC) Received: from nf1.mymailcheap.com (nf1.mymailcheap.com [51.75.14.91]) by relay1.mymailcheap.com (Postfix) with ESMTPS id 97D883E904; Sun, 1 Feb 2026 07:22:38 +0000 (UTC) Received: from mail20.mymailcheap.com (mail20.mymailcheap.com [51.83.111.147]) by nf1.mymailcheap.com (Postfix) with ESMTPSA id 826E0400D5; Sun, 1 Feb 2026 07:22:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=aosc.io; s=default; t=1769930557; bh=tsYztuBMmDw9dhEyvXm134zQnSNewOglMstDiOxHhEs=; h=From:To:Cc:Subject:Date:From; b=iOhL+zKYkoaHk3q1iqoOy9vIybwQDY0uOe0dcyxM0fekb06r8/iElx7VHqGrQLrmW Nq5KtnialzdQJDUTS7l4exM7Cfr1d7oc0NqtorLP9rC03FzHVPirO+E/uGdk6lON0I AN60QrXkkS2p2R2XBlpim+5ZagooXqlnbQZigupg= Received: from liushuyu-p15 (unknown [117.151.13.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail20.mymailcheap.com (Postfix) with ESMTPSA id B594B40563; Sun, 1 Feb 2026 07:22:28 +0000 (UTC) From: Zixing Liu To: WANG Xuerui , Huacai Chen , Bibo Mao Cc: Kexy Biscuit , Mingcong Bai , Zixing Liu , Paolo Bonzini , Jonathan Corbet , Tianrui Zhao , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-riscv@lists.infradead.org Subject: [PATCH v3] KVM: Add KVM_GET_REG_LIST ioctl for LoongArch Date: Sun, 1 Feb 2026 15:21:23 +0800 Message-ID: <20260201072124.566587-1-liushuyu@aosc.io> X-Mailer: git-send-email 2.52.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 826E0400D5 X-Spamd-Result: default: False [1.40 / 10.00]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; MIME_GOOD(-0.10)[text/plain]; ASN(0.00)[asn:16276, ipnet:51.83.0.0/16, country:FR]; ARC_NA(0.00)[]; RCVD_COUNT_ONE(0.00)[1]; RCPT_COUNT_TWELVE(0.00)[18]; MIME_TRACE(0.00)[0:+]; RCVD_VIA_SMTP_AUTH(0.00)[]; DBL_BLOCKED(0.00)[mail20.mymailcheap.com:rdns,mail20.mymailcheap.com:helo,aosc.io:mid,aosc.io:email,aosc.io:from_smtp,aosc.io:from_mime]; TO_MATCH_ENVRCPT_SOME(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; LOCAL_OUTBOUND(0.00)[]; RCVD_TLS_ALL(0.00)[] X-Rspamd-Action: no action X-Rspamd-Server: nf1.mymailcheap.com Content-Type: text/plain; charset="utf-8" This ioctl can be used by the userspace applications to determine which (special) registers are get/set-able in a meaningful way. 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 --- Documentation/virt/kvm/api.rst | 2 +- arch/loongarch/kvm/vcpu.c | 81 ++++++++++++++++++++++++++++++++++ 2 files changed, 82 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. --------------------- =20 :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..fb8001deadc9 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -14,6 +14,8 @@ #define CREATE_TRACE_POINTS #include "trace.h" =20 +#define NUM_LBT_REGS 6 + const struct _kvm_stats_desc kvm_vcpu_stats_desc[] =3D { KVM_GENERIC_VCPU_STATS(), STATS_DESC_COUNTER(VCPU, int_exits), @@ -1186,6 +1188,67 @@ static int kvm_loongarch_vcpu_set_attr(struct kvm_vc= pu *vcpu, return ret; } =20 +static int kvm_loongarch_walk_csrs(struct kvm_vcpu *vcpu, u64 __user *uind= ices) +{ + unsigned int i, count; + + for (i =3D 0, count =3D 0; i < CSR_MAX_NUMS; i++) { + if (!(get_gcsr_flag(i) & (SW_GCSR | HW_GCSR))) + continue; + const u64 reg =3D KVM_IOC_CSRID(i); + if (uindices && put_user(reg, uindices++)) + return -EFAULT; + count++; + } + + return count; +} + +static unsigned long kvm_loongarch_num_regs(struct kvm_vcpu *vcpu) +{ + /* +1 for the KVM_REG_LOONGARCH_COUNTER register */ + unsigned long res =3D + kvm_loongarch_walk_csrs(vcpu, NULL) + KVM_MAX_CPUCFG_REGS + 1; + + if (kvm_guest_has_lbt(&vcpu->arch)) + res +=3D NUM_LBT_REGS; + + return res; +} + +static int kvm_loongarch_copy_reg_indices(struct kvm_vcpu *vcpu, + u64 __user *uindices) +{ + u64 reg; + unsigned int i; + + i =3D kvm_loongarch_walk_csrs(vcpu, uindices); + if (i < 0) + return i; + uindices +=3D i; + + for (i =3D 0; i < KVM_MAX_CPUCFG_REGS; i++) { + reg =3D KVM_IOC_CPUCFG(i); + if (put_user(reg, uindices++)) + return -EFAULT; + } + + reg =3D KVM_REG_LOONGARCH_COUNTER; + if (put_user(reg, uindices++)) + return -EFAULT; + + if (!kvm_guest_has_lbt(&vcpu->arch)) + return 0; + + for (i =3D 1; i <=3D NUM_LBT_REGS; i++) { + reg =3D (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 +1314,24 @@ long kvm_arch_vcpu_ioctl(struct file *filp, r =3D kvm_loongarch_vcpu_set_attr(vcpu, &attr); break; } + case KVM_GET_REG_LIST: { + struct kvm_reg_list __user *user_list =3D argp; + struct kvm_reg_list reg_list; + unsigned n; + + r =3D -EFAULT; + if (copy_from_user(®_list, user_list, sizeof(reg_list))) + break; + n =3D reg_list.n; + reg_list.n =3D kvm_loongarch_num_regs(vcpu); + if (copy_to_user(user_list, ®_list, sizeof(reg_list))) + break; + r =3D -E2BIG; + if (n < reg_list.n) + break; + r =3D kvm_loongarch_copy_reg_indices(vcpu, user_list->reg); + break; + } default: r =3D -ENOIOCTLCMD; break; --=20 2.52.0