{"id":2224402,"url":"http://patchwork.ozlabs.org/api/patches/2224402/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20260417105618.3621-34-magnuskulke@linux.microsoft.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20260417105618.3621-34-magnuskulke@linux.microsoft.com>","list_archive_url":null,"date":"2026-04-17T10:56:17","name":"[33/34] target/i386/mshv: migrate MP_STATE","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"5a5b255b879aec1ff4dde6ca8ba143330271ac6c","submitter":{"id":90753,"url":"http://patchwork.ozlabs.org/api/people/90753/?format=json","name":"Magnus Kulke","email":"magnuskulke@linux.microsoft.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20260417105618.3621-34-magnuskulke@linux.microsoft.com/mbox/","series":[{"id":500310,"url":"http://patchwork.ozlabs.org/api/series/500310/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=500310","date":"2026-04-17T10:55:44","name":"Add migration support to the MSHV accelerator","version":1,"mbox":"http://patchwork.ozlabs.org/series/500310/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2224402/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2224402/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=linux.microsoft.com header.i=@linux.microsoft.com\n header.a=rsa-sha256 header.s=default header.b=HhnM1UYM;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists1p.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fxsMH2ySsz1yHp\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 17 Apr 2026 20:59:07 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists1p.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wDguG-0006Wd-K3; Fri, 17 Apr 2026 06:58:40 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <magnuskulke@linux.microsoft.com>)\n id 1wDguF-0006LS-AB\n for qemu-devel@nongnu.org; Fri, 17 Apr 2026 06:58:39 -0400","from linux.microsoft.com ([13.77.154.182])\n by eggs.gnu.org with esmtp (Exim 4.90_1)\n (envelope-from <magnuskulke@linux.microsoft.com>) id 1wDguC-0001st-MP\n for qemu-devel@nongnu.org; Fri, 17 Apr 2026 06:58:38 -0400","from DESKTOP-TUU1E5L.fritz.box (p5086d620.dip0.t-ipconnect.de\n [80.134.214.32])\n by linux.microsoft.com (Postfix) with ESMTPSA id 2022920B6F0C;\n Fri, 17 Apr 2026 03:58:22 -0700 (PDT)"],"DKIM-Filter":"OpenDKIM Filter v2.11.0 linux.microsoft.com 2022920B6F0C","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;\n s=default; t=1776423506;\n bh=X85EpAHSMzz7M95oI+CtSvddIAuC0r/f6yk0GhyxFBA=;\n h=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n b=HhnM1UYMafnLMKftyD2QVQTz31/+heGgk02vAGAsK/Ju3VFjVvacvxcgAzHJ/4xbQ\n 2QD+nqIYszFI6OxKiCae9owPeJLE4b9z+tiFXkE7bqz5I1SGr6fH3z9mtEGHfM41E4\n 88ZNtabmB7Nz5UkBaxeCmB5MpnXcDZ1ZA+1K18Rg=","From":"Magnus Kulke <magnuskulke@linux.microsoft.com>","To":"qemu-devel@nongnu.org","Cc":"kvm@vger.kernel.org, Magnus Kulke <magnuskulke@microsoft.com>,\n Wei Liu <liuwe@microsoft.com>, \"Michael S. Tsirkin\" <mst@redhat.com>,\n\t=?utf-8?q?C=C3=A9dric_Le_Goater?= <clg@redhat.com>,\n Zhao Liu <zhao1.liu@intel.com>,\n Richard Henderson <richard.henderson@linaro.org>,\n Paolo Bonzini <pbonzini@redhat.com>, Wei Liu <wei.liu@kernel.org>,\n Magnus Kulke <magnuskulke@linux.microsoft.com>,\n Alex Williamson <alex@shazbot.org>,\n Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, =?utf-8?q?Philippe_Mathieu-D?=\n\t=?utf-8?q?aud=C3=A9?= <philmd@linaro.org>,\n Marcelo Tosatti <mtosatti@redhat.com>","Subject":"[PATCH 33/34] target/i386/mshv: migrate MP_STATE","Date":"Fri, 17 Apr 2026 12:56:17 +0200","Message-Id":"<20260417105618.3621-34-magnuskulke@linux.microsoft.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20260417105618.3621-1-magnuskulke@linux.microsoft.com>","References":"<20260417105618.3621-1-magnuskulke@linux.microsoft.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Received-SPF":"pass client-ip=13.77.154.182;\n envelope-from=magnuskulke@linux.microsoft.com; helo=linux.microsoft.com","X-Spam_score_int":"-42","X-Spam_score":"-4.3","X-Spam_bar":"----","X-Spam_report":"(-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3,\n RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001,\n 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 <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"},"content":"MSHV's \"internal activity state\" roughly maps to QEMU's env->mp_state\nand cpu->halted states that describe state of APs in a guest.\n\nWe don't invoke set_mp_state as part of store_vcpu_state() b/c we would\nput all BSP + APs in a RUNNABLE (0) state immediately, breaking SMP boot\n\nInstead we store the mp state as part of the load_cleanup() routine\nafter a migration.\n\nSigned-off-by: Magnus Kulke <magnuskulke@linux.microsoft.com>\n---\n accel/mshv/mshv-all.c       | 10 +++++\n include/system/mshv_int.h   |  1 +\n target/i386/mshv/mshv-cpu.c | 80 +++++++++++++++++++++++++++++++++++++\n 3 files changed, 91 insertions(+)","diff":"diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c\nindex facdc733e7..ffe84d6151 100644\n--- a/accel/mshv/mshv-all.c\n+++ b/accel/mshv/mshv-all.c\n@@ -205,6 +205,7 @@ static int mshv_load_setup(QEMUFile *f, void *opaque, Error **errp)\n static int mshv_load_cleanup(void *opaque)\n {\n     MshvState *s = opaque;\n+    CPUState *cpu;\n     int ret;\n \n     ret = mshv_arch_set_partition_msrs(first_cpu);\n@@ -213,6 +214,15 @@ static int mshv_load_cleanup(void *opaque)\n         return -1;\n     }\n \n+    CPU_FOREACH(cpu) {\n+        ret = mshv_arch_set_mp_state(cpu);\n+        if (ret < 0) {\n+            error_report(\"Failed to set mp state for vCPU %d: %s\",\n+                         cpu->cpu_index, strerror(-ret));\n+            return -1;\n+        }\n+    }\n+\n     resume_vm(s->vm);\n \n     return 0;\ndiff --git a/include/system/mshv_int.h b/include/system/mshv_int.h\nindex bc16b794b2..c24efc8675 100644\n--- a/include/system/mshv_int.h\n+++ b/include/system/mshv_int.h\n@@ -98,6 +98,7 @@ int mshv_get_generic_regs(CPUState *cpu, hv_register_assoc *assocs,\n int mshv_arch_store_vcpu_state(const CPUState *cpu);\n int mshv_arch_load_vcpu_state(CPUState *cpu);\n int mshv_arch_set_partition_msrs(const CPUState *cpu);\n+int mshv_arch_set_mp_state(const CPUState *cpu);\n void mshv_arch_init_vcpu(CPUState *cpu);\n void mshv_arch_destroy_vcpu(CPUState *cpu);\n void mshv_arch_amend_proc_features(\ndiff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c\nindex 463bf7efa0..ae70133c3c 100644\n--- a/target/i386/mshv/mshv-cpu.c\n+++ b/target/i386/mshv/mshv-cpu.c\n@@ -33,6 +33,11 @@\n \n #include <sys/ioctl.h>\n \n+#define MSHV_MP_STATE_RUNNABLE       0\n+#define MSHV_MP_STATE_UNINITIALIZED  1\n+#define MSHV_MP_STATE_INIT_RECEIVED  2\n+#define MSHV_MP_STATE_HALTED         3\n+\n #define MAX_REGISTER_COUNT (MAX_CONST(ARRAY_SIZE(STANDARD_REGISTER_NAMES), \\\n                             MAX_CONST(ARRAY_SIZE(SPECIAL_REGISTER_NAMES), \\\n                                       ARRAY_SIZE(FPU_REGISTER_NAMES))))\n@@ -814,6 +819,76 @@ static int set_vcpu_events(const CPUState *cpu)\n     return 0;\n }\n \n+static int get_mp_state(CPUState *cpu)\n+{\n+    X86CPU *x86cpu = X86_CPU(cpu);\n+    CPUX86State *env = &x86cpu->env;\n+    struct hv_register_assoc assoc = {\n+        .name = HV_REGISTER_INTERNAL_ACTIVITY_STATE,\n+    };\n+    union hv_internal_activity_register activity;\n+    int ret;\n+\n+    ret = mshv_get_generic_regs(cpu, &assoc, 1);\n+    if (ret < 0) {\n+        error_report(\"failed to get internal activity state\");\n+        return -1;\n+    }\n+\n+    activity.as_uint64 = assoc.value.reg64;\n+\n+    /*\n+     * map MSHV activity state to KVM mp_state values, which are used as the\n+     * shared representation in env->mp_state and serialized by vmstate_x86_cpu.\n+     */\n+\n+    if (activity.startup_suspend) {\n+        env->mp_state = MSHV_MP_STATE_UNINITIALIZED;\n+    } else if (activity.halt_suspend) {\n+        env->mp_state = MSHV_MP_STATE_HALTED;\n+    } else {\n+        env->mp_state = MSHV_MP_STATE_RUNNABLE;\n+    }\n+\n+    cpu->halted = (env->mp_state == MSHV_MP_STATE_HALTED);\n+\n+    return 0;\n+}\n+\n+int mshv_arch_set_mp_state(const CPUState *cpu)\n+{\n+    X86CPU *x86cpu = X86_CPU(cpu);\n+    CPUX86State *env = &x86cpu->env;\n+    union hv_internal_activity_register activity = { 0 };\n+    struct hv_register_assoc assoc = {\n+        .name = HV_REGISTER_INTERNAL_ACTIVITY_STATE,\n+    };\n+    int ret;\n+\n+    switch (env->mp_state) {\n+    case MSHV_MP_STATE_HALTED:\n+        activity.halt_suspend = 1;\n+        break;\n+    case MSHV_MP_STATE_UNINITIALIZED:\n+    case MSHV_MP_STATE_INIT_RECEIVED:\n+        activity.startup_suspend = 1;\n+        break;\n+    case MSHV_MP_STATE_RUNNABLE:\n+    default:\n+        break;\n+    }\n+\n+    assoc.value.reg64 = activity.as_uint64;\n+\n+    ret = mshv_set_generic_regs(cpu, &assoc, 1);\n+    if (ret < 0) {\n+        error_report(\"failed to set internal activity state\");\n+        return -1;\n+    }\n+\n+    return 0;\n+}\n+\n static int update_hflags(CPUState *cpu)\n {\n     X86CPU *x86cpu = X86_CPU(cpu);\n@@ -876,6 +951,11 @@ int mshv_arch_load_vcpu_state(CPUState *cpu)\n         return ret;\n     }\n \n+    ret = get_mp_state(cpu);\n+    if (ret < 0) {\n+        return ret;\n+    }\n+\n     return 0;\n }\n \n","prefixes":["33/34"]}