From nobody Mon Nov 23 04:34:26 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; dmarc=pass(p=none dis=none) header.from=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1794228054; cv=none; d=zohomail.com; s=zohoarc; b=a8z+sgHzlUQl139/QEy4AD6e5Ix0kYkhsJBPHfkIugzLbqttxQgY1t61rUI9TZeD9Ioy7Jt2U5N8azSHn2FyYwmLHWytyqxTUg+VJ1L1Hrxw7A0TTh4YZ6EOFT3ORR6BqWbgdEPqDHcEt0YhFVqPa/Sc5e+CumaHC7hVOMygq+0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794228054; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=49D5boZaDUxTGilkC4LkL9wT6Z2rhfkTa4e+HDU3IuM=; b=dG9iqEejoSMoZ1Qy+7gItPskm6QxzwUadBi8oVYFUCMlDq3UStf+NIyNvB/Q6Nw4pOUAmJFuuS3MJ2fgspUER6pAp1/+ZGx+82cK9KJ0RzAbSre39NWwpSzO3ZoDlMgSm/yTeIaJ7aLD1jYW2GuvaXfFLXdwVAFSYiUPyfxLzXs= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1794228054117729.126863508022; Mon, 9 Nov 2026 04:40:54 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xQOfI-0001lR-Kv; Mon, 09 Nov 2026 07:40:01 -0500 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 1xQOfH-0001lF-6z for qemu-devel@nongnu.org; Mon, 09 Nov 2026 07:39:59 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xQOfF-0007V5-9S for qemu-devel@nongnu.org; Mon, 09 Nov 2026 07:39:58 -0500 Received: from DESKTOP-TUU1E5L.fritz.box (p50892c72.dip0.t-ipconnect.de [80.137.44.114]) by linux.microsoft.com (Postfix) with ESMTPSA id CEED220B7168; Mon, 9 Nov 2026 04:39:50 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CEED220B7168 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1794227992; bh=49D5boZaDUxTGilkC4LkL9wT6Z2rhfkTa4e+HDU3IuM=; h=From:To:Cc:Subject:Date:From; b=VW59diKyEJYX9L+znAy9AW7Z1CD6BhohWpSNElkF+vyon6tAfRU8C/z49MyY3nTGL tzUxBlA+TqSVATc5vSSs/0C22+py8vrBgluJEP2vbMtAQY5WfJVliian4DpwgNkPyR LgP03z9LBBW5qCO/gVuHpMET2CqD+BZTkDqq50Cg= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Magnus Kulke , Magnus Kulke , Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= Subject: [PATCH] target/i386/mshv: add MshvPutState for vcpu writes Date: Mon, 9 Nov 2026 13:39:49 +0100 Message-Id: <20261109123949.724711-1-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=13.77.154.182; envelope-from=magnuskulke@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham 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 @linux.microsoft.com) X-ZM-MESSAGEID: 1794228056227158500 Content-Type: text/plain; charset="utf-8" Currently QEMU marks a vcpu state as dirty also on merely inspectional reads, e.g. via "info registers", which triggers a writeback to the hypervisor. For MSRs that cause a side-effect when written to this is undesired. We introduce an MshvPutState flag into the writeback logic to differentiate "runtime" from "full" and "reset" writebacks that should occur e.g. after a migration or startup. Discrete side-effectful MSRs are excluded in runtime writebacks. So far we only mark one MSR as not runtime-safe, but probably more will be added eventually. Signed-off-by: Magnus Kulke Reviewed-by: Mohamed Mediouni Reviewed-by: Wei Liu Tested-by: Doru Bl=C3=A2nzeanu --- accel/mshv/mshv-all.c | 10 +++++----- include/system/mshv_int.h | 13 +++++++++++-- target/i386/mshv/mshv-cpu.c | 10 ++++++++-- target/i386/mshv/msr.c | 21 ++++++++++++++++++++- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index d820a67b95..d64599e2e2 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -759,10 +759,10 @@ static int mshv_cpu_exec(CPUState *cpu) =20 do { if (cpu->vcpu_dirty) { - ret =3D mshv_arch_store_vcpu_state(cpu); + ret =3D mshv_arch_store_vcpu_state(cpu, MSHV_PUT_RUNTIME_STATE= ); if (ret) { - error_report("Failed to put registers after init: %s", - strerror(-ret)); + error_report("Failed to put registers at runtime: %s", + strerror(-ret)); ret =3D -1; break; } @@ -890,7 +890,7 @@ static void mshv_start_vcpu_thread(CPUState *cpu) static void do_mshv_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg) { - int ret =3D mshv_arch_store_vcpu_state(cpu); + int ret =3D mshv_arch_store_vcpu_state(cpu, MSHV_PUT_FULL_STATE); if (ret < 0) { error_report("Failed to put registers after init: %s", strerror(-r= et)); abort(); @@ -906,7 +906,7 @@ static void mshv_cpu_synchronize_post_init(CPUState *cp= u) =20 static void mshv_cpu_synchronize_post_reset(CPUState *cpu) { - int ret =3D mshv_arch_store_vcpu_state(cpu); + int ret =3D mshv_arch_store_vcpu_state(cpu, MSHV_PUT_RESET_STATE); if (ret) { error_report("Failed to put registers after reset: %s", strerror(-ret)); diff --git a/include/system/mshv_int.h b/include/system/mshv_int.h index 1f77ffd152..b7b7c56443 100644 --- a/include/system/mshv_int.h +++ b/include/system/mshv_int.h @@ -98,6 +98,15 @@ typedef enum MshvVmExit { MshvVmExitDebug =3D 3, } MshvVmExit; =20 +typedef enum MshvPutState { + /* state subset only touched by the VCPU itself during runtime */ + MSHV_PUT_RUNTIME_STATE =3D 1, + /* state subset modified during VCPU reset */ + MSHV_PUT_RESET_STATE =3D 2, + /* full state set, modified during initialization or on vmload */ + MSHV_PUT_FULL_STATE =3D 3, +} MshvPutState; + void mshv_init_mmio_emu(void); int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int *cpu_fd); void mshv_remove_vcpu(int vm_fd, int cpu_fd); @@ -107,7 +116,7 @@ int mshv_set_generic_regs(const CPUState *cpu, const hv= _register_assoc *assocs, size_t n_regs); int mshv_get_generic_regs(CPUState *cpu, hv_register_assoc *assocs, size_t n_regs); -int mshv_arch_store_vcpu_state(const CPUState *cpu); +int mshv_arch_store_vcpu_state(const CPUState *cpu, MshvPutState state); int mshv_arch_load_vcpu_state(CPUState *cpu); int mshv_arch_set_partition_msrs(const CPUState *cpu); int mshv_arch_set_mp_state(const CPUState *cpu); @@ -152,7 +161,7 @@ void mshv_log_global_stop(MemoryListener *listener); /* msr */ int mshv_init_msrs(const CPUState *cpu); int mshv_get_msrs(CPUState *cpu); -int mshv_set_msrs(const CPUState *cpu); +int mshv_set_msrs(const CPUState *cpu, MshvPutState put_state); =20 /* synic */ int mshv_get_simp(int cpu_fd, uint8_t *page); diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 4df73bb497..72ef01a597 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -1511,7 +1511,13 @@ static int set_synic_state(const CPUState *cpu) return 0; } =20 -int mshv_arch_store_vcpu_state(const CPUState *cpu) +/* + * The put_state param is passed to decide whether we need a full restorat= ion + * of all registers (e.g. after a migration ) or just a subset during runt= ime + * (e.g after "info registers"). The latter should avoid writing registers= that + * cause a side-effect. + */ +int mshv_arch_store_vcpu_state(const CPUState *cpu, MshvPutState put_state) { int ret; =20 @@ -1541,7 +1547,7 @@ int mshv_arch_store_vcpu_state(const CPUState *cpu) return ret; } =20 - ret =3D mshv_set_msrs(cpu); + ret =3D mshv_set_msrs(cpu, put_state); if (ret < 0) { return ret; } diff --git a/target/i386/mshv/msr.c b/target/i386/mshv/msr.c index 96bd552916..a06844cc99 100644 --- a/target/i386/mshv/msr.c +++ b/target/i386/mshv/msr.c @@ -324,6 +324,20 @@ static bool msr_supported(uint32_t name) return true; } =20 +static bool msr_is_runtime_safe(uint32_t name) +{ + switch (name) { + /* + * HV_X64_REGISTER_TSC will invalidate the guest's tsc_page if written= to + * while the guest's timer is not frozen. + */ + case HV_X64_REGISTER_TSC: + return false; + default: + return true; + } +} + int mshv_get_msrs(CPUState *cpu) { int ret =3D 0; @@ -406,13 +420,14 @@ static void load_from_env(const CPUState *cpu, struct= hv_register_assoc *assocs, load_from_env_mtrr_phys(cpu, &assocs[mtrr_offset], MSHV_MTRR_MSR_COUNT= ); } =20 -int mshv_set_msrs(const CPUState *cpu) +int mshv_set_msrs(const CPUState *cpu, MshvPutState put_state) { size_t n_assocs =3D MSHV_MSR_TOTAL_COUNT; struct hv_register_assoc assocs[MSHV_MSR_TOTAL_COUNT] =3D { 0 }; int ret; size_t i, j; X86CPU *x86cpu =3D X86_CPU(cpu); + bool runtime =3D (put_state <=3D MSHV_PUT_RUNTIME_STATE); =20 load_from_env(cpu, assocs, n_assocs); =20 @@ -432,6 +447,10 @@ int mshv_set_msrs(const CPUState *cpu) continue; } =20 + if (runtime && !msr_is_runtime_safe(name)) { + continue; + } + if (j !=3D i) { assocs[j] =3D assocs[i]; } --=20 2.34.1