From nobody Sat Sep 26 20:51:41 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1788162173; cv=none; d=zohomail.com; s=zohoarc; b=L9N9ZkKUb35go0vnEtbVY0Ytxhtbfxq6ztK0BZo9WbzCdJ/a7KUHohR1VTFu6KETd5AR8LZlfaQgjrB9EVF67bmjRZPddJ3TC6HiocGCUsaYUm0HjFRSwy7nZrs7lJ2aE0hRYgMecun2JnSwCy2fy4840NhlnnZiKSa1c/E3WTw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788162173; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=hzQGgmdUEimuLT4PLlaAK7STMI5+FWdCII0k4o9DR9I=; b=WZD5DFfSQeeK7Jg7bp+Fd1MWssiOfDLjRENSJo0RBjl7M+duEMfQzMaCyCG71JEZscMvuzKLahFnyPb5ogyOCBS3QTmtpZ0p8kzYpAjAcIX8BdeqvOWy5OtAlpJ9lwKWzkSVzGw+TD+EVFDrvDcMUWB1Rn5wxk6WJyYzulNPT88= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1788162173532164.77914371071415; Mon, 31 Aug 2026 00:42:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x0weX-0003k2-50; Mon, 31 Aug 2026 03:42:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1x0weV-0003jQ-Vt; Mon, 31 Aug 2026 03:42:00 -0400 Received: from [218.76.62.146] (helo=ultrarisc.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x0weT-00052i-Ur; Mon, 31 Aug 2026 03:41:59 -0400 Received: from ur-dp1000 (unknown [192.168.100.1]) by localhost.localdomain (Coremail) with SMTP id AQAAfwDHQAceMJVq4BUAAA--.112S3; Mon, 31 Aug 2026 15:41:56 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ultrarisc.com; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=hzQGgmdUEimuLT4PLlaAK7STMI5+FWdCII 0k4o9DR9I=; b=Rk81FYsVuOAnkdLapGac5GqD8pKAV5n437Abpyes+JZ2G+h1VH jW1ir6gl5SDL0ss9HlsUCZUOfZOt85OGqsKgP8Q28Wep8/5rgRVxE2pXtjpzHuLD jLgfzeOYYGpDp3CEIGOr2OnTXZi04fHiJnV+GURABvXrkucNWmTjP63Xo= From: Xie Bo To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-stable@nongnu.org, Xie Bo Subject: [PATCH 1/3] target/riscv/kvm: Preserve FCSR across migration Date: Mon, 31 Aug 2026 15:41:04 +0800 Message-ID: <20260831074106.1798671-2-xb@ultrarisc.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260831074106.1798671-1-xb@ultrarisc.com> References: <20260831074106.1798671-1-xb@ultrarisc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: AQAAfwDHQAceMJVq4BUAAA--.112S3 X-Coremail-Antispam: 1UD129KBjvJXoWxur4kZFyUJF1fWF17Gr15Arb_yoWrXFy5pF s5CFZIkrn5JFZ7X3yfJF1DXr45Wr4rGws8Aws7u3yIya15GrWYqFs7tay2yr1rJFyrWr4S 9F4rAr15CF4jyrDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmm14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r1I6r4UM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r1Y6r17McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r1q6r43MxkIecxEwVCm-wCF04k20xvY0x0EwIxGrwCFx2IqxVCF s4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r 1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWU JVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r 1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1U YxBIdaVFxhVjvjDU0xZFpf9x0JUa0PDUUUUU= X-CM-SenderInfo: l0e63zxwud2x1vfou0bp/1tbiAQAMB2qOWvsAQwANsB X-Host-Lookup-Failed: Reverse DNS lookup failed for 218.76.62.146 (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=218.76.62.146; envelope-from=xb@ultrarisc.com; helo=ultrarisc.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RDNS_NONE=0.793, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ultrarisc.com) X-ZM-MESSAGEID: 1788162176499158500 Content-Type: text/plain; charset="utf-8" kvm_riscv_get_regs_fp() and kvm_riscv_put_regs_fp() handle f0-f31 but not FCSR. KVM exposes FCSR as a separate 32-bit register in both the F and D register groups, so frm and fflags are lost after migration. Read and write FCSR together with the FP registers and save it in a KVM-only subsection. Older streams do not contain the subsection; in that case the destination keeps its reset value. Fixes: 937f0b45120b ("target/riscv: Implement kvm_arch_get_registers") Fixes: 9997cc1e19d1 ("target/riscv: Implement kvm_arch_put_registers") Signed-off-by: Xie Bo Cc: qemu-stable@nongnu.org Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu.h | 3 +++ target/riscv/kvm/kvm-cpu.c | 20 ++++++++++++++++---- target/riscv/machine.c | 27 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 718b66487a..a5ec69c3cc 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -532,6 +532,9 @@ struct CPUArchState { hwaddr fdt_addr; =20 #ifdef CONFIG_KVM + /* KVM floating-point state */ + uint32_t kvm_fcsr; + /* kvm timer */ bool kvm_timer_dirty; uint64_t kvm_timer_time; diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c index 68e1501b21..c8ccd9124e 100644 --- a/target/riscv/kvm/kvm-cpu.c +++ b/target/riscv/kvm/kvm-cpu.c @@ -111,6 +111,14 @@ static uint64_t kvm_riscv_vector_reg_id(RISCVCPU *cpu, =20 #define RISCV_FP_D_REG(idx) KVM_RISCV_REG_ID_U64(KVM_REG_RISCV_FP_D, idx) =20 +#define RISCV_FP_F_FCSR_REG \ + KVM_RISCV_REG_ID_U32(KVM_REG_RISCV_FP_F, \ + KVM_REG_RISCV_FP_F_REG(fcsr)) + +#define RISCV_FP_D_FCSR_REG \ + KVM_RISCV_REG_ID_U32(KVM_REG_RISCV_FP_D, \ + KVM_REG_RISCV_FP_D_REG(fcsr)) + #define RISCV_VECTOR_CSR_REG(name) \ KVM_RISCV_REG_ID_ULONG(KVM_REG_RISCV_VECTOR, \ KVM_REG_RISCV_VECTOR_CSR_REG(name)) @@ -743,7 +751,8 @@ static int kvm_riscv_get_regs_fp(CPUState *cs) } env->fpr[i] =3D reg; } - return ret; + return kvm_get_one_reg(cs, RISCV_FP_D_FCSR_REG, + &env->kvm_fcsr); } =20 if (riscv_has_ext(env, RVF)) { @@ -755,7 +764,8 @@ static int kvm_riscv_get_regs_fp(CPUState *cs) } env->fpr[i] =3D reg; } - return ret; + return kvm_get_one_reg(cs, RISCV_FP_F_FCSR_REG, + &env->kvm_fcsr); } =20 return ret; @@ -776,7 +786,8 @@ static int kvm_riscv_put_regs_fp(CPUState *cs) return ret; } } - return ret; + return kvm_set_one_reg(cs, RISCV_FP_D_FCSR_REG, + &env->kvm_fcsr); } =20 if (riscv_has_ext(env, RVF)) { @@ -788,7 +799,8 @@ static int kvm_riscv_put_regs_fp(CPUState *cs) return ret; } } - return ret; + return kvm_set_one_reg(cs, RISCV_FP_F_FCSR_REG, + &env->kvm_fcsr); } =20 return ret; diff --git a/target/riscv/machine.c b/target/riscv/machine.c index 31c49ca3e6..2685338901 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -198,6 +198,32 @@ static const VMStateDescription vmstate_rv128 =3D { }; =20 #ifdef CONFIG_KVM +static bool kvm_fcsr_needed(void *opaque) +{ + RISCVCPU *cpu =3D opaque; + CPURISCVState *env =3D &cpu->env; + + return kvm_enabled() && + (riscv_has_ext(env, RVF) || riscv_has_ext(env, RVD)); +} + +static int kvm_fcsr_post_load(void *opaque, int version_id) +{ + return kvm_enabled() ? 0 : -ENOTSUP; +} + +static const VMStateDescription vmstate_kvm_fcsr =3D { + .name =3D "cpu/kvm-fcsr", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D kvm_fcsr_needed, + .post_load =3D kvm_fcsr_post_load, + .fields =3D (const VMStateField[]) { + VMSTATE_UINT32(env.kvm_fcsr, RISCVCPU), + VMSTATE_END_OF_LIST() + } +}; + static bool kvmtimer_needed(void *opaque) { return kvm_enabled(); @@ -565,6 +591,7 @@ const VMStateDescription vmstate_riscv_cpu =3D { &vmstate_pointermasking, &vmstate_rv128, #ifdef CONFIG_KVM + &vmstate_kvm_fcsr, &vmstate_kvmtimer, &vmstate_kvm_mp_state, #endif --=20 2.17.1 From nobody Sat Sep 26 20:51:41 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1788162159; cv=none; d=zohomail.com; s=zohoarc; b=JN7UYQr42ZVqYN66bSwKwqheriDCE11mUDbmEyTtWl+t7Sc/4VKh5a80vgPlV+DQQoA5AWb0uZzlb1xoJjp9zA18fxPRN0N+WlrmzSSe1tZW3VpzsBkFhPlx6tkpa2caWyOpg3v9rF7Fy+9iSWsnOe+u2fg10vSvMrcjn/nsTWI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788162159; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QHWoB8L1QdgmjIJacxbVgFVoXkhd0BIYoMpVj7+RDWg=; b=bl1SJLjBAsP+gLHOnG4VuFCgBj78lkTzxQbSU8iz2BqqZrPy/0gYUAN54vd98xZ8XtR9D3BPt3U9eD7iuarEnR+7lrp+71ShucVk8wgus5rlLNcDSEWTL0TVrb1ncY8AxnP1+lSESv28n3vXEYpMHg2BXkj2aAXfqviLBPs1k+E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1788162159201921.738068298179; Mon, 31 Aug 2026 00:42:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x0weU-0003hI-FK; Mon, 31 Aug 2026 03:41:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1x0weS-0003eL-TV; Mon, 31 Aug 2026 03:41:56 -0400 Received: from [218.76.62.146] (helo=ultrarisc.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x0weQ-00052o-H0; Mon, 31 Aug 2026 03:41:56 -0400 Received: from ur-dp1000 (unknown [192.168.100.1]) by localhost.localdomain (Coremail) with SMTP id AQAAfwDHQAceMJVq4BUAAA--.112S4; Mon, 31 Aug 2026 15:41:59 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ultrarisc.com; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=QHWoB8L1QdgmjIJacxbVgFVoXkhd0BIYoM pVj7+RDWg=; b=mxPKdlP91ZPeUfrirR4LyBYl4H1nMJfoo3Jn43L7aDtxJfSMGf vK9h8Z/AKwJqwAzXksdq1Nclp7adWz0arCn3L3vsPFOoDszakEzo+rEmCvQFJQd7 PsNiFw2HKFdV4MZTniOqnWrZ9nKXV8IJJeJf7DBX2CSm2JaUEaIWTHjno= From: Xie Bo To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-stable@nongnu.org, Xie Bo Subject: [PATCH 2/3] target/riscv/kvm: Reject migration with a different timer frequency Date: Mon, 31 Aug 2026 15:41:05 +0800 Message-ID: <20260831074106.1798671-3-xb@ultrarisc.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260831074106.1798671-1-xb@ultrarisc.com> References: <20260831074106.1798671-1-xb@ultrarisc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: AQAAfwDHQAceMJVq4BUAAA--.112S4 X-Coremail-Antispam: 1UD129KBjvJXoWxtryfJw47XrW7uryfGrykZrb_yoW7WF4xpF s5G3y3Crn8Xr93tw1fJr1DGry5Crs5uFnxC348Cr4Iva13JrWDXFZ2ga47ZF4fAFWDGwnx uFn5uFnI9F45ZrUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUml14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr1j6F4UJwAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCY02Avz4vE-syl42xK82IYc2Ij64vIr41l4I8I3I0E 4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGV WUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_ Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rV WUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4U JbIYCTnIWIevJa73UjIFyTuYvjfUbfO7UUUUU X-CM-SenderInfo: l0e63zxwud2x1vfou0bp/1tbiAQAMB2qOWvsAQwAPsD X-Host-Lookup-Failed: Reverse DNS lookup failed for 218.76.62.146 (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=218.76.62.146; envelope-from=xb@ultrarisc.com; helo=ultrarisc.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RDNS_NONE=0.793, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ultrarisc.com) X-ZM-MESSAGEID: 1788162163447154100 Content-Type: text/plain; charset="utf-8" The timer frequency check is currently in kvm_riscv_put_regs_timer() and guarded by migration_is_running(). That condition is false on the incoming side, and the source frequency is not part of the migration stream, so migration between hosts with different frequencies completes. Add the source frequency to a KVM-only subsection. Its post_load checks the destination frequency before the timer registers are restored and rejects a mismatch. When loading an older stream without the subsection, skip the check and retain the old behavior. Fixes: 27abe66f31ef ("target/riscv: Add kvm_riscv_get/put_regs_timer") Signed-off-by: Xie Bo Cc: qemu-stable@nongnu.org Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu.h | 1 + target/riscv/kvm/kvm-cpu.c | 40 +++++++++++++++++++++++------------- target/riscv/kvm/kvm_riscv.h | 1 + target/riscv/machine.c | 31 ++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index a5ec69c3cc..193f81f00d 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -541,6 +541,7 @@ struct CPUArchState { uint64_t kvm_timer_compare; uint64_t kvm_timer_state; uint64_t kvm_timer_frequency; + bool kvm_timer_frequency_loaded; =20 /* KVM multiprocessor state */ uint32_t kvm_mp_state; diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c index c8ccd9124e..1993bd446b 100644 --- a/target/riscv/kvm/kvm-cpu.c +++ b/target/riscv/kvm/kvm-cpu.c @@ -45,7 +45,6 @@ #include "kvm_riscv.h" #include "sbi_ecall_interface.h" #include "chardev/char-fe.h" -#include "migration/misc.h" #include "system/runstate.h" #include "hw/riscv/numa.h" =20 @@ -822,9 +821,34 @@ static void kvm_riscv_get_regs_timer(CPUState *cs) env->kvm_timer_dirty =3D true; } =20 +int kvm_riscv_check_timer_frequency(RISCVCPU *cpu) +{ + CPUState *cs =3D CPU(cpu); + CPURISCVState *env =3D &cpu->env; + int ret; + uint64_t frequency; + + if (!env->kvm_timer_frequency_loaded) { + return 0; + } + + ret =3D kvm_get_one_reg(cs, RISCV_TIMER_REG(frequency), &frequency); + if (ret) { + return ret; + } + + if (frequency !=3D env->kvm_timer_frequency) { + error_report("KVM timer frequency mismatch: source %" PRIu64 + " Hz, destination %" PRIu64 " Hz", + env->kvm_timer_frequency, frequency); + return -EINVAL; + } + + return 0; +} + static void kvm_riscv_put_regs_timer(CPUState *cs) { - uint64_t reg; CPURISCVState *env =3D &RISCV_CPU(cs)->env; =20 if (!env->kvm_timer_dirty) { @@ -844,18 +868,6 @@ static void kvm_riscv_put_regs_timer(CPUState *cs) KVM_RISCV_SET_TIMER(cs, state, env->kvm_timer_state); } =20 - /* - * For now, migration will not work between Hosts with different timer - * frequency. Therefore, we should check whether they are the same here - * during the migration. - */ - if (migration_is_running()) { - KVM_RISCV_GET_TIMER(cs, frequency, reg); - if (reg !=3D env->kvm_timer_frequency) { - error_report("Dst Hosts timer frequency !=3D Src Hosts"); - } - } - env->kvm_timer_dirty =3D false; } =20 diff --git a/target/riscv/kvm/kvm_riscv.h b/target/riscv/kvm/kvm_riscv.h index 61eaa12443..0e27ce8bf3 100644 --- a/target/riscv/kvm/kvm_riscv.h +++ b/target/riscv/kvm/kvm_riscv.h @@ -29,6 +29,7 @@ void kvm_riscv_aia_create(MachineState *machine, uint64_t= group_shift, uint64_t guest_num); void riscv_kvm_aplic_request(void *opaque, int irq, int level); bool kvm_riscv_has_mp_state(void); +int kvm_riscv_check_timer_frequency(RISCVCPU *cpu); void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp); uint64_t kvm_riscv_get_timebase_frequency(RISCVCPU *cpu); =20 diff --git a/target/riscv/machine.c b/target/riscv/machine.c index 2685338901..14a50f1d33 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -198,6 +198,35 @@ static const VMStateDescription vmstate_rv128 =3D { }; =20 #ifdef CONFIG_KVM +static bool kvm_timer_frequency_needed(void *opaque) +{ + return kvm_enabled(); +} + +static int kvm_timer_frequency_post_load(void *opaque, int version_id) +{ + RISCVCPU *cpu =3D opaque; + + if (!kvm_enabled()) { + return -ENOTSUP; + } + + cpu->env.kvm_timer_frequency_loaded =3D true; + return kvm_riscv_check_timer_frequency(cpu); +} + +static const VMStateDescription vmstate_kvm_timer_frequency =3D { + .name =3D "cpu/kvm-timer-frequency", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D kvm_timer_frequency_needed, + .post_load =3D kvm_timer_frequency_post_load, + .fields =3D (const VMStateField[]) { + VMSTATE_UINT64(env.kvm_timer_frequency, RISCVCPU), + VMSTATE_END_OF_LIST() + } +}; + static bool kvm_fcsr_needed(void *opaque) { RISCVCPU *cpu =3D opaque; @@ -256,6 +285,7 @@ static int riscv_cpu_kvm_pre_load(void *opaque) { RISCVCPU *cpu =3D opaque; =20 + cpu->env.kvm_timer_frequency_loaded =3D false; cpu->env.kvm_mp_state_loaded =3D false; return 0; } @@ -592,6 +622,7 @@ const VMStateDescription vmstate_riscv_cpu =3D { &vmstate_rv128, #ifdef CONFIG_KVM &vmstate_kvm_fcsr, + &vmstate_kvm_timer_frequency, &vmstate_kvmtimer, &vmstate_kvm_mp_state, #endif --=20 2.17.1 From nobody Sat Sep 26 20:51:41 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1788162158; cv=none; d=zohomail.com; s=zohoarc; b=LXazah+7tglemG+Eweog9iYHr1TO0SSrObNo9o237C1APtVwvtBPvB9TKfTsLTRKpoBz693uS3/vGiLcwRoACQ9twS0HKLE6C+jBJJzudXhd5MVhpjFh0/ua+zGZ0QHAblenm10AzftHVBYRPn4bhf3CtuUqlZBiW/n30iyeA2w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788162158; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1aMCn457NawyLRoDOeCVaYbrIcUjzFHs6NmlhXiS9sU=; b=VFIOzQBu2v0K2LkFI29nmapefQFlWoKZ1X+g5PxqO3hQzo7ff3nFIogRD4/re3Ci2pUPgOwLTlZsu2sQeyjjEb4LJWh0BEaEecar3XCx86TGHIJz95nX0dDYGYv+LrSzSYE5VyatRmNZZ8qMJC7Uje2ENoOjWCMAy7qEJynj+ZI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1788162158566529.4196838369578; Mon, 31 Aug 2026 00:42:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x0web-0003mQ-6t; Mon, 31 Aug 2026 03:42:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1x0weY-0003kw-Ro; Mon, 31 Aug 2026 03:42:02 -0400 Received: from [218.76.62.146] (helo=ultrarisc.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x0weW-00053C-KK; Mon, 31 Aug 2026 03:42:02 -0400 Received: from ur-dp1000 (unknown [192.168.100.1]) by localhost.localdomain (Coremail) with SMTP id AQAAfwDHQAceMJVq4BUAAA--.112S5; Mon, 31 Aug 2026 15:41:59 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ultrarisc.com; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=1aMCn457NawyLRoDOeCVaYbrIcUjzFHs6N mlhXiS9sU=; b=gVNv1E28R2fdFOgwsgShvOvWbjhQPfXEOKO5g6SMc3NEW0ucO6 J5u8CLavSogbXDcwK3ZYo/3v9edK0bEMjVt15nz0aCNGQFRrM73ZiOtrldRP0wBL XWu+KOzfqj8qjdMvh+ITjiqVzdcVCMQMyRAfXGFKksq/mXtjiTdcwWcFg= From: Xie Bo To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-stable@nongnu.org, Xie Bo Subject: [PATCH 3/3] target/riscv/kvm: Preserve VCSR across migration Date: Mon, 31 Aug 2026 15:41:06 +0800 Message-ID: <20260831074106.1798671-4-xb@ultrarisc.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260831074106.1798671-1-xb@ultrarisc.com> References: <20260831074106.1798671-1-xb@ultrarisc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: AQAAfwDHQAceMJVq4BUAAA--.112S5 X-Coremail-Antispam: 1UD129KBjvJXoW7CryUZr1ktryrWw43GF45Wrg_yoW8Wr17pa n7WFW3Kr93tFZ7J34Sywn0qr4rAr4rKw4DAFZxuwna9r45JFZ0qF1vgw4YqF15Ja4rJFnY gFWUCr95urs8Xa7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUml14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr1j6F4UJwAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCY02Avz4vE-syl42xK82IYc2Ij64vIr41l4I8I3I0E 4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGV WUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_ Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rV WUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4U JbIYCTnIWIevJa73UjIFyTuYvjfU5CztUUUUU X-CM-SenderInfo: l0e63zxwud2x1vfou0bp/1tbiAQAMB2qOWvsAQwARsd X-Host-Lookup-Failed: Reverse DNS lookup failed for 218.76.62.146 (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=218.76.62.146; envelope-from=xb@ultrarisc.com; helo=ultrarisc.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RDNS_NONE=0.793, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ultrarisc.com) X-ZM-MESSAGEID: 1788162160904158500 Content-Type: text/plain; charset="utf-8" The vector state sync handles vstart, vl and vtype, but leaves vcsr behind. vxrm and vxsat in the migration stream are therefore stale, and the destination does not restore the values held by KVM. Read vcsr when collecting vector state and split it into vxrm and vxsat. Rebuild it from those fields when writing vector state back. Fixes: 3ca78c0689d5 ("target/riscv/kvm: add RVV and Vector CSR regs") Cc: qemu-stable@nongnu.org Signed-off-by: Xie Bo Reviewed-by: Daniel Henrique Barboza --- target/riscv/kvm/kvm-cpu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c index 1993bd446b..45ad5e1525 100644 --- a/target/riscv/kvm/kvm-cpu.c +++ b/target/riscv/kvm/kvm-cpu.c @@ -910,6 +910,13 @@ static int kvm_riscv_get_regs_vector(CPUState *cs) } env->vtype =3D reg; =20 + ret =3D kvm_get_one_reg(cs, RISCV_VECTOR_CSR_REG(vcsr), ®); + if (ret) { + return ret; + } + env->vxrm =3D (reg & VCSR_VXRM) >> VCSR_VXRM_SHIFT; + env->vxsat =3D (reg & VCSR_VXSAT) >> VCSR_VXSAT_SHIFT; + if (kvm_v_vlenb.supported) { ret =3D kvm_get_one_reg(cs, RISCV_VECTOR_CSR_REG(vlenb), ®); if (ret) { @@ -966,6 +973,13 @@ static int kvm_riscv_put_regs_vector(CPUState *cs) return ret; } =20 + reg =3D (env->vxrm << VCSR_VXRM_SHIFT) | + (env->vxsat << VCSR_VXSAT_SHIFT); + ret =3D kvm_set_one_reg(cs, RISCV_VECTOR_CSR_REG(vcsr), ®); + if (ret) { + return ret; + } + if (kvm_v_vlenb.supported) { reg =3D cpu->cfg.vlenb; ret =3D kvm_set_one_reg(cs, RISCV_VECTOR_CSR_REG(vlenb), ®); --=20 2.17.1