From nobody Mon Feb 9 03:00:19 2026 Received: from relay5.mymailcheap.com (relay5.mymailcheap.com [159.100.248.207]) (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 6C2E52D46B2; Sat, 31 Jan 2026 06:07:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.100.248.207 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769839675; cv=none; b=gG7/LmiBQWVTH+EqrHSRlxoEcdbiP0qPwM8tE3on6ZzcUiJn0AN6v7+Ii3ZRpIdJASYGizKvv3Eq/TfqCUkCIabEEBaMQ8fmtD1T/0R6Bx+r4Cvsoa12Dy/aaO8xgVBjjpIv5mlrwUduo36bMt8/q4gawZT1yO135jDk1qH00Sg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769839675; c=relaxed/simple; bh=oeog5iZO0tE/GpnDtWPS42chjSxxsQeVLLMswgVyFfc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ONuwWMm3oGQCKIWxW/tiRcuw+zz7QKoUHGQrf4r9yyGnyoEHDyKamJjk/t1mJfm0gQOme6pV2vqr+Uxg70rWSK4Ez+W/ngQ642Pd2jciIN4bUAVEIlYTpYax6xLV2o9AyXStPOyR+FsP1aW69M/JBQkcpcRa8PlszBYxD3t8S0Q= 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=LLr1uZ2R; arc=none smtp.client-ip=159.100.248.207 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="LLr1uZ2R" Received: from relay4.mymailcheap.com (relay4.mymailcheap.com [137.74.80.154]) by relay5.mymailcheap.com (Postfix) with ESMTPS id EC00926345; Sat, 31 Jan 2026 06:07:45 +0000 (UTC) Received: from nf2.mymailcheap.com (nf2.mymailcheap.com [54.39.180.165]) by relay4.mymailcheap.com (Postfix) with ESMTPS id F1F8C202EF; Sat, 31 Jan 2026 06:07:37 +0000 (UTC) Received: from mail20.mymailcheap.com (mail20.mymailcheap.com [51.83.111.147]) by nf2.mymailcheap.com (Postfix) with ESMTPSA id 33F5B400EA; Sat, 31 Jan 2026 06:07:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=aosc.io; s=default; t=1769839655; bh=oeog5iZO0tE/GpnDtWPS42chjSxxsQeVLLMswgVyFfc=; h=From:To:Cc:Subject:Date:From; b=LLr1uZ2RvxnzrGxF2U7ClmQfPams39DoMhxkR9Zk4vd/JcK7kplJGnweT5Xn1qbx6 Pqh5vDcvOFRG3Emrqa//GTRTieof1nbdI/DL+VNlj1ysDTCi0AF0wvgFG3//Qz1S7y nVrI0FbF24KWZOhXytUTNXbVb3dV+VcpFz/rAT2k= 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 EC0F840F79; Sat, 31 Jan 2026 06:07:29 +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 v2 RESEND] KVM: Add KVM_GET_REG_LIST ioctl for LoongArch Date: Sat, 31 Jan 2026 14:05:59 +0800 Message-ID: <20260131060600.169748-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: 33F5B400EA 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]; ARC_NA(0.00)[]; ASN(0.00)[asn:16276, ipnet:51.83.0.0/16, country:FR]; RCVD_COUNT_ONE(0.00)[1]; RCPT_COUNT_TWELVE(0.00)[18]; MIME_TRACE(0.00)[0:+]; RCVD_VIA_SMTP_AUTH(0.00)[]; SPFBL_URIBL_EMAIL_FAIL(0.00)[liushuyu.aosc.io:server fail]; URIBL_BLOCKED(0.00)[mail20.mymailcheap.com:rdns,mail20.mymailcheap.com:helo,aosc.io:email,aosc.io:from_smtp,aosc.io:mid,aosc.io:from_mime]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; LOCAL_OUTBOUND(0.00)[]; RCVD_TLS_ALL(0.00)[] X-Rspamd-Action: no action X-Rspamd-Server: nf2.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 | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 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..ed11438f4544 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -1186,6 +1186,73 @@ 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_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 =3D + kvm_loongarch_walk_csrs(vcpu, NULL) + KVM_MAX_CPUCFG_REGS + 1; + + if (kvm_guest_has_lbt(&vcpu->arch)) + res +=3D 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; + + 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 kvm_loongarch_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 +1318,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