qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "liguang.zhang" <18622748025@163.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, alistair23@gmail.com,
	"liguang.zhang" <liguang.zhang@hexintek.com>
Subject: [PATCH] target/riscv: Clearing the CSR values at reset and syncing the MPSTATE with the host
Date: Tue, 18 Jul 2023 21:02:51 +0800	[thread overview]
Message-ID: <20230718130317.12545-1-18622748025@163.com> (raw)
In-Reply-To: <20230625025018.26956-1-18622748025@163.com>

From: "liguang.zhang" <liguang.zhang@hexintek.com>

Fix the guest reboot error when using KVM
There are two issues when rebooting a guest using KVM
1. When the guest initiates a reboot the host is unable to stop the vcpu
2. When running a SMP guest the qemu monitor system_reset causes a vcpu crash

This can be fixed by clearing the CSR values at reset and syncing the
MPSTATE with the host.

Signed-off-by: liguang.zhang <liguang.zhang@hexintek.com>
---
 target/riscv/kvm.c       | 42 ++++++++++++++++++++++++++++++++++++++++
 target/riscv/kvm_riscv.h |  1 +
 2 files changed, 43 insertions(+)

diff --git a/target/riscv/kvm.c b/target/riscv/kvm.c
index 9d8a8982f9..ecc8ab8238 100644
--- a/target/riscv/kvm.c
+++ b/target/riscv/kvm.c
@@ -44,6 +44,8 @@
 #include "migration/migration.h"
 #include "sysemu/runstate.h"
 
+static bool cap_has_mp_state;
+
 static uint64_t kvm_riscv_reg_id(CPURISCVState *env, uint64_t type,
                                  uint64_t idx)
 {
@@ -790,6 +792,24 @@ int kvm_arch_get_registers(CPUState *cs)
     return ret;
 }
 
+int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state)
+{
+    if (cap_has_mp_state) {
+        struct kvm_mp_state mp_state = {
+            .mp_state = state
+        };
+
+        int ret = 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;
+        }
+    }
+
+    return 0;
+}
+
 int kvm_arch_put_registers(CPUState *cs, int level)
 {
     int ret = 0;
@@ -809,6 +829,18 @@ int kvm_arch_put_registers(CPUState *cs, int level)
         return ret;
     }
 
+    if (KVM_PUT_RESET_STATE == level) {
+        RISCVCPU *cpu = RISCV_CPU(cs);
+        if (cs->cpu_index == 0) {
+            ret = kvm_riscv_sync_mpstate_to_kvm(cpu, KVM_MP_STATE_RUNNABLE);
+        } else {
+            ret = kvm_riscv_sync_mpstate_to_kvm(cpu, KVM_MP_STATE_STOPPED);
+        }
+        if (ret) {
+            return ret;
+        }
+    }
+
     return ret;
 }
 
@@ -909,6 +941,7 @@ int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
 
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
+    cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE);
     return 0;
 }
 
@@ -987,10 +1020,19 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)
     if (!kvm_enabled()) {
         return;
     }
+    for (int i=0; i<32; i++)
+        env->gpr[i] = 0;
     env->pc = cpu->env.kernel_addr;
     env->gpr[10] = kvm_arch_vcpu_id(CPU(cpu)); /* a0 */
     env->gpr[11] = cpu->env.fdt_addr;          /* a1 */
     env->satp = 0;
+    env->mie = 0;
+    env->stvec = 0;
+    env->sscratch = 0;
+    env->sepc = 0;
+    env->scause = 0;
+    env->stval = 0;
+    env->mip = 0;
 }
 
 void kvm_riscv_set_irq(RISCVCPU *cpu, int irq, int level)
diff --git a/target/riscv/kvm_riscv.h b/target/riscv/kvm_riscv.h
index e3ba935808..3ea68c38e3 100644
--- a/target/riscv/kvm_riscv.h
+++ b/target/riscv/kvm_riscv.h
@@ -22,5 +22,6 @@
 void kvm_riscv_init_user_properties(Object *cpu_obj);
 void kvm_riscv_reset_vcpu(RISCVCPU *cpu);
 void kvm_riscv_set_irq(RISCVCPU *cpu, int irq, int level);
+int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state);
 
 #endif
-- 
2.17.1



  parent reply	other threads:[~2023-07-18 13:03 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-25  2:50 [PATCH] target/riscv: fix the issue of guest reboot then no response or crash in kvm-mode liguang.zhang
2023-07-10  1:16 ` Alistair Francis
2023-07-18 11:06   ` 18622748025
2023-07-18 11:08   ` 18622748025
2023-07-18 11:44   ` Re:Re:[PATCH v3] " liguang.zhang
2023-07-18 11:49 ` liguang.zhang
2023-07-18 12:21 ` [PATCH] target/riscv: Clearing the CSR values at reset and syncing the MPSTATE with the host liguang.zhang
2023-07-24  3:30   ` Alistair Francis
2023-07-24  6:05     ` liguang.zhang
2023-08-10 18:43       ` Alistair Francis
2023-07-24  6:12     ` [PATCH v2] " liguang.zhang
2023-07-24  6:25     ` [PATCH v3] " liguang.zhang
2023-08-10 18:34       ` Alistair Francis
2023-09-13  8:37         ` liguang.zhang
2023-09-13  9:13           ` [PATCH v4] " liguang.zhang
2023-09-19  4:05             ` Alistair Francis
2023-09-19  4:12             ` Alistair Francis
2023-09-19  4:16             ` Alistair Francis
2023-07-18 12:39 ` [PATCH] " liguang.zhang
2023-07-18 12:43 ` liguang.zhang
2023-07-18 12:47 ` [PATCH] target/riscv: fix the issue of guest reboot then no response or crash in kvm-mode liguang.zhang
2023-07-18 12:52 ` liguang.zhang
2023-07-18 13:21   ` [PATCH v4] " liguang.zhang
2023-07-18 12:56 ` [PATCH] target/riscv: Clearing the CSR values at reset and syncing the MPSTATE with the host liguang.zhang
2023-07-18 12:58 ` liguang.zhang
2023-07-18 13:02 ` liguang.zhang [this message]
2023-07-18 13:07 ` [PATCH v4] target/riscv: fix the issue of guest reboot then no response or crash in kvm-mode liguang.zhang
  -- strict thread matches above, loose matches on Subject: below --
2023-06-23  2:15 [PATCH] " Alistair Francis
2023-07-18 12:24 ` [PATCH] target/riscv: Clearing the CSR values at reset and syncing the MPSTATE with the host liguang.zhang
2023-07-18 12:29 ` liguang.zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230718130317.12545-1-18622748025@163.com \
    --to=18622748025@163.com \
    --cc=alistair23@gmail.com \
    --cc=liguang.zhang@hexintek.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).