From nobody Sat Nov 15 22:35:30 2025 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=1747740763; cv=none; d=zohomail.com; s=zohoarc; b=DeeZmeiQLFvk9bD+wfklnVyQEd83qRXPIWl8J/d2B1J9xeRi52jy2FTxsU/BK6UQdHBo0KHNlZIzCwgGZbtNLYg2JOXm/KUa2T4vbz4h9FU9Byc225FsgdGbbdwyxo/gTioGUC5JzCjOj/7pnOCwDowjdnYVZCnr6ab760jUGKA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747740763; 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=+C8oPQWTbM0TkBZdInbt0LFkfJTPQQwK8XvexWWBHbc=; b=NASA0lXkv07MYVmtK/NkGNe73+DVh1SCFfEiRAwIimr2oA8jKdbI9Q3Lwgtgy8ao4pW2awFPyrGbNoE2CYg/5fd7BPWdy6t6OXj+o6uZiLnclhqQ1mxTMGo6w5raTuxAqLdgWuv/vAZUHQ+A/2i23Qi9kIEu/3byHeeMI1/BwpA= 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747740763084695.7413589540752; Tue, 20 May 2025 04:32:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHLCi-0000CS-QR; Tue, 20 May 2025 07:32:16 -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 1uHLCc-0008QN-Al for qemu-devel@nongnu.org; Tue, 20 May 2025 07:32:10 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHLCa-0001Kp-GC for qemu-devel@nongnu.org; Tue, 20 May 2025 07:32:10 -0400 Received: from DESKTOP-TUU1E5L.fritz.box (unknown [172.201.77.43]) by linux.microsoft.com (Postfix) with ESMTPSA id B8ECC2068320; Tue, 20 May 2025 04:31:53 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com B8ECC2068320 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1747740716; bh=+C8oPQWTbM0TkBZdInbt0LFkfJTPQQwK8XvexWWBHbc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Tg6KKQX0P2qLTIyAqp5kPw4sYwbu1rzw2+h8Xxn64H5vV+IRF3eQf5VQvIGrnlJur J5zvXzWV3KinrvLhHP/vSXt1JOZhE/xOXVFrurjYKo1OkJJPRpg6aMyAREBxaDnSoy pHvHtx64Ui0B6Wme8TRHs53+lT4pYxJjhiZXneiM= From: Magnus Kulke To: magnuskulke@microsoft.com, qemu-devel@nongnu.org, liuwe@microsoft.com Cc: Paolo Bonzini , "Michael S. Tsirkin" , Wei Liu , Phil Dennis-Jordan , Roman Bolshakov , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Zhao Liu , Richard Henderson , Cameron Esfahani , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [RFC PATCH 14/25] target/i386/mshv: Add CPU create and remove logic Date: Tue, 20 May 2025 13:30:07 +0200 Message-Id: <20250520113018.49569-15-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250520113018.49569-1-magnuskulke@linux.microsoft.com> References: <20250520113018.49569-1-magnuskulke@linux.microsoft.com> 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=lists.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: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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: 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: 1747740764330116600 Content-Type: text/plain; charset="utf-8" Implement MSHV-specific hooks for vCPU creation and teardown in the i386 target. A list of locks per vCPU is maintained to lock CPU state in MMIO operations. Signed-off-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 61 +++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index c4b2c297e2..0ba1dacaed 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -14,6 +14,8 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/atomic.h" +#include "qemu/lockable.h" #include "qemu/error-report.h" #include "qemu/typedefs.h" =20 @@ -30,6 +32,36 @@ #include "trace-accel_mshv.h" #include "trace.h" =20 +#include + +static QemuMutex *cpu_guards_lock; +static GHashTable *cpu_guards; + +static void add_cpu_guard(int cpu_fd) +{ + QemuMutex *guard; + + WITH_QEMU_LOCK_GUARD(cpu_guards_lock) { + guard =3D g_new0(QemuMutex, 1); + qemu_mutex_init(guard); + g_hash_table_insert(cpu_guards, GUINT_TO_POINTER(cpu_fd), guard); + } +} + +static void remove_cpu_guard(int cpu_fd) +{ + QemuMutex *guard; + + WITH_QEMU_LOCK_GUARD(cpu_guards_lock) { + guard =3D g_hash_table_lookup(cpu_guards, GUINT_TO_POINTER(cpu_fd)= ); + if (guard) { + qemu_mutex_destroy(guard); + g_free(guard); + g_hash_table_remove(cpu_guards, GUINT_TO_POINTER(cpu_fd)); + } + } +} + int mshv_store_regs(CPUState *cpu) { error_report("unimplemented"); @@ -62,20 +94,37 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message = *msg, MshvVmExit *exit) =20 void mshv_remove_vcpu(int vm_fd, int cpu_fd) { - error_report("unimplemented"); - abort(); + /* + * TODO: don't we have to perform an ioctl to remove the vcpu? + * there is WHvDeleteVirtualProcessor in the WHV api + */ + remove_cpu_guard(cpu_fd); } =20 + int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int *cpu_fd) { - error_report("unimplemented"); - abort(); + int ret; + struct mshv_create_vp vp_arg =3D { + .vp_index =3D vp_index, + }; + ret =3D ioctl(vm_fd, MSHV_CREATE_VP, &vp_arg); + if (ret < 0) { + error_report("failed to create mshv vcpu: %s", strerror(errno)); + return -1; + } + + add_cpu_guard(ret); + *cpu_fd =3D ret; + + return 0; } =20 void mshv_init_cpu_logic(void) { - error_report("unimplemented"); - abort(); + cpu_guards_lock =3D g_new0(QemuMutex, 1); + qemu_mutex_init(cpu_guards_lock); + cpu_guards =3D g_hash_table_new(g_direct_hash, g_direct_equal); } =20 void mshv_arch_init_vcpu(CPUState *cpu) --=20 2.34.1