From nobody Sun Sep 28 16:38:11 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757920258643767.2471238743434; Mon, 15 Sep 2025 00:10:58 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uy3Kn-0005g3-Kb; Mon, 15 Sep 2025 03:09:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uy3Kf-0005eJ-Iw; Mon, 15 Sep 2025 03:09:03 -0400 Received: from [218.76.62.146] (helo=ultrarisc.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uy3KT-0007Lc-LG; Mon, 15 Sep 2025 03:09:01 -0400 Received: from ur-dp1000.. (unknown [192.168.100.1]) by localhost.localdomain (Coremail) with SMTP id AQAAfwCXwdChu8do+wkuAA--.45058S3; Mon, 15 Sep 2025 15:09:38 +0800 (CST) 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, xiamy@ultrarisc.com, Xie Bo Subject: [PATCH v9 1/2] Set KVM initial privilege mode and mp_state Date: Mon, 15 Sep 2025 15:08:07 +0800 Message-ID: <20250915070811.3422578-2-xb@ultrarisc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250915070811.3422578-1-xb@ultrarisc.com> References: <20250915070811.3422578-1-xb@ultrarisc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-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4j6F4UMIIF0xvE42 xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF 7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjfU8tC7UUUUU X-CM-SenderInfo: l0e63zxwud2x1vfou0bp/1tbiAQAGB2jHbnEAJwAAsX 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=lists.gnu.org; Received-SPF: pass client-ip=218.76.62.146; envelope-from=xb@ultrarisc.com; helo=ultrarisc.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, RDNS_NONE=0.793, SPF_PASS=-0.001, T_SPF_HELO_TEMPERROR=0.01 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: 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-ZM-MESSAGEID: 1757920260958116600 Content-Type: text/plain; charset="utf-8" For KVM mode, the privilege mode should not include M-mode, and the initial value should be set to S-mode. Additionally, a following patch adds the implementation of putting the vCPU privilege mode to KVM. When the vCPU runs for the first time, QEMU will first put the privilege state to KVM. If the initial value is set to M-mode, KVM will encounter an error. In addition, this patch introduces the 'mp_state' field to RISC-V vCPUs, following the convention used by KVM on x86. The 'mp_state' reflects the multiprocessor state of a vCPU, and is used to control whether the vCPU is runnable by KVM. Randomly select one CPU as the boot CPU. Since each CPU executes the riscv_cpu_reset_hold() function and CPU0 executes first, only CPU0 randomly selects the boot CPU. Signed-off-by: Xie Bo --- target/riscv/cpu.c | 17 ++++++++++++++++- target/riscv/cpu.h | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index d055ddf462..55892a2fc7 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -37,6 +37,7 @@ #include "kvm/kvm_riscv.h" #include "tcg/tcg-cpu.h" #include "tcg/tcg.h" +#include "hw/boards.h" =20 /* RISC-V CPU definitions */ static const char riscv_single_letter_exts[] =3D "IEMAFDQCBPVH"; @@ -685,18 +686,32 @@ static void riscv_cpu_reset_hold(Object *obj, ResetTy= pe type) #ifndef CONFIG_USER_ONLY uint8_t iprio; int i, irq, rdzero; + static int boot_cpu_index; #endif CPUState *cs =3D CPU(obj); RISCVCPU *cpu =3D RISCV_CPU(cs); RISCVCPUClass *mcc =3D RISCV_CPU_GET_CLASS(obj); CPURISCVState *env =3D &cpu->env; + MachineState *ms =3D MACHINE(qdev_get_machine()); =20 if (mcc->parent_phases.hold) { mcc->parent_phases.hold(obj, type); } #ifndef CONFIG_USER_ONLY env->misa_mxl =3D mcc->def->misa_mxl_max; - env->priv =3D PRV_M; + if (kvm_enabled()) { + env->priv =3D PRV_S; + } else { + env->priv =3D PRV_M; + } + if (cs->cpu_index =3D=3D 0) { + boot_cpu_index =3D g_random_int_range(0, ms->smp.cpus); + } + if (cs->cpu_index =3D=3D boot_cpu_index) { + env->mp_state =3D KVM_MP_STATE_RUNNABLE; + } else { + env->mp_state =3D KVM_MP_STATE_STOPPED; + } env->mstatus &=3D ~(MSTATUS_MIE | MSTATUS_MPRV); if (env->misa_mxl > MXL_RV32) { /* diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 738e68fa6e..7ea4859de7 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -269,6 +269,8 @@ struct CPUArchState { #endif =20 target_ulong priv; + /* Current multiprocessor state of this vCPU. */ + uint32_t mp_state; /* CSRs for execution environment configuration */ uint64_t menvcfg; target_ulong senvcfg; --=20 2.43.0