From nobody Mon Sep 28 02:13:38 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=1785389269; cv=none; d=zohomail.com; s=zohoarc; b=iryY+ePT1TaztLcXRLvuVSUZqPRFVmcmrTUDBeho9TEHdmWQl8NNFmTSylxRoxql/RYKcAdnMePTA2XOywUAqTfrafyPV7GBf9iSCNQuwYkkQaaVOpbtvHAyBXvNJIEvumsHdMR+fqgeZe8nn8zcSeVxpHqm4w0+lKPyOL8elYU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1785389269; 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=DJ8hPiaRqQ2n5AJgLRt7ghP210MWnRvfNeB/k12XEBM=; b=mqv99Rh1lM+VTHTsc/t3LFZzCS/uKc38fSowjywAKSbgO+f4NSsaL0w9D9hPaPglEdZIRS1pnTQkQK0DPhawIsw5ogZiFvv5pMBwUD4/gSd/0GuKAe385C08fHDJBAbXPwtrwkOJFB//PtzgMWOp5GtsfRHeRQWq0TB8C+H0qYo= 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 1785389269181432.33012074153805; Wed, 29 Jul 2026 22:27:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wpJI1-0001Qz-3u; Thu, 30 Jul 2026 01:26:41 -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 1wpJHz-0001QY-9A; Thu, 30 Jul 2026 01:26:39 -0400 Received: from [218.76.62.146] (helo=ultrarisc.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wpJHx-0000dF-7Y; Thu, 30 Jul 2026 01:26:39 -0400 Received: from ur-dp1000 (unknown [192.168.100.1]) by localhost.localdomain (Coremail) with SMTP id AQAAfwA3cUL63mpqEscVAA--.18165S3; Thu, 30 Jul 2026 13:20:06 +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=DJ8hPiaRqQ2n5AJgLRt7ghP210MWnRvfNe B/k12XEBM=; b=jOx8ppMEPI+cfHpnJ0h4y+Pt7uZAdSEfj0wPvyJwrhkwi/wq41 Pgi+EBBenKxOykeXuJg4KvqLAwgECaJVymF92esqeTCBI4V0cokmsZCGE0Yrkb3x ezl9ZqlhTNFAV7NYf1Q6M8Kctwyg3CCMP/1mGqC4/De366EnjhIyPFGfY= From: Xie Bo To: qemu-devel@nongnu.org Cc: ajones@ventanamicro.com, qemu-riscv@nongnu.org, alistair23@gmail.com, pbonzini@redhat.com, anup@brainfault.org, alistair.francis@wdc.com, rkrcmar@ventanamicro.com, palmer@dabbelt.com, daniel.barboza@oss.qualcomm.com, xb@ultrarisc.com Subject: [PATCH v10 1/2] target/riscv/kvm: Synchronize privilege mode Date: Thu, 30 Jul 2026 13:19:25 +0800 Message-ID: <20260730051926.7322-2-xb@ultrarisc.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260730051926.7322-1-xb@ultrarisc.com> References: <20250915070811.3422578-1-xb@ultrarisc.com> <20260730051926.7322-1-xb@ultrarisc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: AQAAfwA3cUL63mpqEscVAA--.18165S3 X-Coremail-Antispam: 1UD129KBjvJXoW7tFWftw13GFWDtw1rAF1rXrb_yoW8Gry3pa n5Gay5KF93JFWxG3yfAr4DX3WrWr43GwsxZFWxuw129r45trWUKr9FgF4Utr95GFWrZr4Y vF4qkr1Y9F45ZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmm14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVWUJVW8JwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r1q6r43MxkIecxEwVCm-wCF04k20xvY0x0EwIxGrwCFx2IqxVCF s4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r 1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWU JVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r 1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1U YxBIdaVFxhVjvjDU0xZFpf9x0JUTT5JUUUUU= X-CM-SenderInfo: l0e63zxwud2x1vfou0bp/1tbiAQATB2pkMtIAAQAbsh 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: 1785389272393158500 Content-Type: text/plain; charset="utf-8" The KVM core register synchronization currently omits the vCPU privilege mode. As a result, env.priv can be stale when the migration stream is saved and the destination can restore the vCPU in the wrong mode. Read and write the KVM core mode register together with the other core registers. The generic RISC-V CPU VMState already carries env.priv, so no migration format change is required. Signed-off-by: Xie Bo Reviewed-by: Daniel Henrique Barboza --- target/riscv/kvm/kvm-cpu.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c index 495cb42dc8..8218832fbe 100644 --- a/target/riscv/kvm/kvm-cpu.c +++ b/target/riscv/kvm/kvm-cpu.c @@ -603,6 +603,12 @@ static int kvm_riscv_get_regs_core(CPUState *cs) } env->pc =3D reg; =20 + ret =3D kvm_get_one_reg(cs, RISCV_CORE_REG(mode), ®); + if (ret) { + return ret; + } + env->priv =3D reg; + for (i =3D 1; i < 32; i++) { uint64_t id =3D KVM_RISCV_REG_ID_ULONG(KVM_REG_RISCV_CORE, i); ret =3D kvm_get_one_reg(cs, id, ®); @@ -628,6 +634,12 @@ static int kvm_riscv_put_regs_core(CPUState *cs) return ret; } =20 + reg =3D env->priv; + ret =3D kvm_set_one_reg(cs, RISCV_CORE_REG(mode), ®); + if (ret) { + return ret; + } + for (i =3D 1; i < 32; i++) { uint64_t id =3D KVM_RISCV_REG_ID_ULONG(KVM_REG_RISCV_CORE, i); reg =3D env->gpr[i]; --=20 2.17.1 From nobody Mon Sep 28 02:13:38 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=1785389241; cv=none; d=zohomail.com; s=zohoarc; b=HtGFX0HIWfsd/BVdNVOZdHOGGFwYvtERkyCU+OhXiWxUizh+tH0ElxgykY6Kvg2zwxsIgbqSLwqyv+3H0G6HFI0fz6Ik4B9uhoyrnY1BnPX5gl7GUEnBk8f3/u2i0YIPhcJK4eDVl0I4nydu/I8frZK+AABJ9tnLEsKO1bqvyYA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1785389241; 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=HBao4nZtOzT+Vs478vTeMagKhnOy/S3IgKB2hEb50+Y=; b=AMEdlrKTmiaCkVs6XRhxuCB+tSWS/8PrSI0PIDJyqH655mCIUfJdV4Wl5loPQbprd3M+lSN+F3LF9U/IBR6m+hGa6lLwIeEJiMceG5FFn5OfkPADzISIQd15L1Sts2QTTl2JVDFPjBU/jqKtPF9qcg1zmZ6LAttMyb/HV44WNHM= 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 1785389241728498.8577683324676; Wed, 29 Jul 2026 22:27:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wpJI1-0001R5-DO; Thu, 30 Jul 2026 01:26:41 -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 1wpJI0-0001Qm-07; Thu, 30 Jul 2026 01:26:40 -0400 Received: from [218.76.62.146] (helo=ultrarisc.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wpJHx-0000dG-8f; Thu, 30 Jul 2026 01:26:39 -0400 Received: from ur-dp1000 (unknown [192.168.100.1]) by localhost.localdomain (Coremail) with SMTP id AQAAfwA3cUL63mpqEscVAA--.18165S4; Thu, 30 Jul 2026 13:20:07 +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=HBao4nZtOzT+Vs478vTeMagKhnOy/S3IgK B2hEb50+Y=; b=loruhXGyVYCNIFwKc3JNoHpwboWory09pzI0/xkQQumCuN2K7O ZiDQYnmFRCSN2hSlxUG7E8f530nK2bcBr6hPjKCmEzdg9as3TFRV9Frxdiloped2 2u9qGSnG4I2249bRxyrzeUYRiMoinjQKb3777llwgFAa7qklkJk+tyj10= From: Xie Bo To: qemu-devel@nongnu.org Cc: ajones@ventanamicro.com, qemu-riscv@nongnu.org, alistair23@gmail.com, pbonzini@redhat.com, anup@brainfault.org, alistair.francis@wdc.com, rkrcmar@ventanamicro.com, palmer@dabbelt.com, daniel.barboza@oss.qualcomm.com, xb@ultrarisc.com Subject: [PATCH v10 2/2] target/riscv/kvm: Preserve MP state across migration Date: Thu, 30 Jul 2026 13:19:26 +0800 Message-ID: <20260730051926.7322-3-xb@ultrarisc.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260730051926.7322-1-xb@ultrarisc.com> References: <20250915070811.3422578-1-xb@ultrarisc.com> <20260730051926.7322-1-xb@ultrarisc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: AQAAfwA3cUL63mpqEscVAA--.18165S4 X-Coremail-Antispam: 1UD129KBjvJXoWxKr1DGr15WF1rGrW5Cr1xZrb_yoW7Kw45pF s8GrW3CrZ5J3s7Xw1fJ34DWFn8Wr48Wr45A397GrWIkF45KrW5urWvkrW2yrs5Ja4UJrWa 93W5Ar9xCa10yw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmm14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVWUJVW8JwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r1q6r43MxkIecxEwVCm-wCF04k20xvY0x0EwIxGrwCFx2IqxVCF s4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r 1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWU JVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r 1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1U YxBIdaVFxhVjvjDU0xZFpf9x0JUADGOUUUUU= X-CM-SenderInfo: l0e63zxwud2x1vfou0bp/1tbiAQATB2pkMtIAAQAdsn 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: 1785389245034158500 Content-Type: text/plain; charset="utf-8" RISC-V KVM initializes secondary vCPUs in KVM_MP_STATE_STOPPED, but QEMU does not save their runtime MP state. A destination therefore retains reset MP state after migration and cannot reliably resume all vCPUs. Save KVM_GET_MP_STATE in a capability-gated KVM VMState subsection and restore it on KVM_PUT_FULL_STATE. Keep the existing reset initialization path unchanged. Track whether the subsection was loaded so migration streams from older QEMU versions retain their previous destination reset behavior. Using a subsection avoids changing the generic RISC-V CPU VMState version and keeps the state out of TCG migration streams. Signed-off-by: Xie Bo Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu.h | 4 +++ target/riscv/kvm/kvm-cpu.c | 50 ++++++++++++++++++++++++------------ target/riscv/kvm/kvm_riscv.h | 2 +- target/riscv/machine.c | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 17 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index c9dfa7daff..e980b5964d 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -538,6 +538,10 @@ struct CPUArchState { uint64_t kvm_timer_compare; uint64_t kvm_timer_state; uint64_t kvm_timer_frequency; + + /* KVM multiprocessor state */ + uint32_t kvm_mp_state; + bool kvm_mp_state_loaded; #endif /* CONFIG_KVM */ }; =20 diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c index 8218832fbe..cc5655d46d 100644 --- a/target/riscv/kvm/kvm-cpu.c +++ b/target/riscv/kvm/kvm-cpu.c @@ -1374,25 +1374,35 @@ int kvm_arch_get_registers(CPUState *cs, Error **er= rp) return ret; } =20 + if (cap_has_mp_state) { + struct kvm_mp_state mp_state; + + ret =3D kvm_vcpu_ioctl(cs, KVM_GET_MP_STATE, &mp_state); + if (ret) { + return ret; + } + RISCV_CPU(cs)->env.kvm_mp_state =3D mp_state.mp_state; + } + return ret; } =20 -int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state) +bool kvm_riscv_has_mp_state(void) { - if (cap_has_mp_state) { - struct kvm_mp_state mp_state =3D { - .mp_state =3D state - }; + return cap_has_mp_state; +} =20 - int ret =3D kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state); - if (ret) { - fprintf(stderr, "%s: failed to sync MP_STATE %d/%s\n", - __func__, ret, strerror(-ret)); - return -1; - } +static int kvm_riscv_put_mp_state(CPUState *cs) +{ + struct kvm_mp_state mp_state =3D { + .mp_state =3D RISCV_CPU(cs)->env.kvm_mp_state, + }; + + if (!cap_has_mp_state) { + return 0; } =20 - return 0; + return kvm_vcpu_ioctl(cs, KVM_SET_MP_STATE, &mp_state); } =20 int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp) @@ -1431,10 +1441,18 @@ int kvm_arch_put_registers(CPUState *cs, KvmPutStat= e level, Error **errp) } =20 if (KVM_PUT_RESET_STATE =3D=3D level) { - RISCVCPU *cpu =3D RISCV_CPU(cs); - int state =3D cs->cpu_index =3D=3D 0 ? KVM_MP_STATE_RUNNABLE - : KVM_MP_STATE_STOPPED; - ret =3D kvm_riscv_sync_mpstate_to_kvm(cpu, state); + CPURISCVState *env =3D &RISCV_CPU(cs)->env; + + env->kvm_mp_state =3D cs->cpu_index =3D=3D 0 ? KVM_MP_STATE_RUNNAB= LE + : KVM_MP_STATE_STOPPED; + env->kvm_mp_state_loaded =3D false; + ret =3D kvm_riscv_put_mp_state(cs); + if (ret) { + return ret; + } + } else if (KVM_PUT_FULL_STATE =3D=3D level && + RISCV_CPU(cs)->env.kvm_mp_state_loaded) { + ret =3D kvm_riscv_put_mp_state(cs); if (ret) { return ret; } diff --git a/target/riscv/kvm/kvm_riscv.h b/target/riscv/kvm/kvm_riscv.h index b2bcd1041f..61eaa12443 100644 --- a/target/riscv/kvm/kvm_riscv.h +++ b/target/riscv/kvm/kvm_riscv.h @@ -28,7 +28,7 @@ void kvm_riscv_aia_create(MachineState *machine, uint64_t= group_shift, uint64_t aplic_base, uint64_t imsic_base, uint64_t guest_num); void riscv_kvm_aplic_request(void *opaque, int irq, int level); -int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state); +bool kvm_riscv_has_mp_state(void); 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 0ab613a298..26ecee2a48 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -25,6 +25,9 @@ #include "exec/icount.h" #include "target/riscv/tcg/debug.h" #include "hw/riscv/machines-qom.h" +#ifdef CONFIG_KVM +#include "kvm/kvm_riscv.h" +#endif =20 static bool pmp_needed(void *opaque) { @@ -222,6 +225,44 @@ static const VMStateDescription vmstate_kvmtimer =3D { VMSTATE_END_OF_LIST() } }; + +static int riscv_cpu_kvm_pre_load(void *opaque) +{ + RISCVCPU *cpu =3D opaque; + + cpu->env.kvm_mp_state_loaded =3D false; + return 0; +} + +static bool kvm_mp_state_needed(void *opaque) +{ + return kvm_enabled() && kvm_riscv_has_mp_state(); +} + +static int kvm_mp_state_post_load(void *opaque, int version_id) +{ + RISCVCPU *cpu =3D opaque; + CPURISCVState *env =3D &cpu->env; + + if (!kvm_enabled() || !kvm_riscv_has_mp_state()) { + return -ENOTSUP; + } + + env->kvm_mp_state_loaded =3D true; + return 0; +} + +static const VMStateDescription vmstate_kvm_mp_state =3D { + .name =3D "cpu/kvm-mp-state", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D kvm_mp_state_needed, + .post_load =3D kvm_mp_state_post_load, + .fields =3D (const VMStateField[]) { + VMSTATE_UINT32(env.kvm_mp_state, RISCVCPU), + VMSTATE_END_OF_LIST() + } +}; #endif =20 static bool debug_needed(void *opaque) @@ -459,6 +500,9 @@ const VMStateDescription vmstate_riscv_cpu =3D { .name =3D "cpu", .version_id =3D 11, .minimum_version_id =3D 11, +#ifdef CONFIG_KVM + .pre_load =3D riscv_cpu_kvm_pre_load, +#endif .post_load =3D riscv_cpu_post_load, .fields =3D (const VMStateField[]) { VMSTATE_UINT64_ARRAY(env.gpr, RISCVCPU, 32), @@ -522,6 +566,7 @@ const VMStateDescription vmstate_riscv_cpu =3D { &vmstate_rv128, #ifdef CONFIG_KVM &vmstate_kvmtimer, + &vmstate_kvm_mp_state, #endif &vmstate_envcfg, &vmstate_debug, --=20 2.17.1