From nobody Sat Nov 15 12:43:53 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=1751390967; cv=none; d=zohomail.com; s=zohoarc; b=GPF9WcOxTUI3oOcLTKc6QAn8nbdqVrMaioxUvij20D/zPxkqAUxQy2FfXmdcGlG1J71sT+6N+ykerrUO74BWy6vbYdX+PMFsiXY0+26zRg4vIcwFf3XrbFOBuQX+0FnNGUPYsscFkMfvEXPYOhKtvwbs7OBDG9TylghhXHYLdzk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751390967; 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=5XVtskIyehqzUfzD7n3E/UoVw7FlSFU9LC3s1SxuJo8=; b=B/58wtIaoLJ1FoQbFDHls/IaGIiDsqfDcqnKrvja9jtMzoESj/wF8qShIi9ZV+m1Pv1Sai8LsQ8g7JV/yRgg6BUZKNYtOyWbuNPSM9FFeqvZaaeJejuXIdjkI2kPbjMsyT9Pi89F/E1RHgj0CPk0a/L0A0UHkgiTbcR7ErERur8= 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 1751390967503873.332079660936; Tue, 1 Jul 2025 10:29:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWen9-0002QS-LO; Tue, 01 Jul 2025 13:29:11 -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 1uWen7-0002PQ-ET for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:09 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWen5-0006SY-CF for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:09 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id AA0A72112219; Tue, 1 Jul 2025 10:29:02 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com AA0A72112219 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390946; bh=5XVtskIyehqzUfzD7n3E/UoVw7FlSFU9LC3s1SxuJo8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZLg7V7Ye3T7iGGRp6ZdEpUG03EgcuLUyy/nClTiUBESu2BuZqXPf+HUA8K7FsPz/x NrcSjjynAxfrTr9irAI1hKXzDEfnS2V+SiuUOSD9LCDrWNzAnrfP/Esgn6+m2+vqFm UuTP9YhnbrJLclbVeLBxWa4ozSqbBrMbDeuyjaJk= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 01/27] accel: Add Meson and config support for MSHV accelerator Date: Tue, 1 Jul 2025 19:28:08 +0200 Message-Id: <20250701172834.44849-2-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751390968716116600 Content-Type: text/plain; charset="utf-8" Introduce a Meson feature option and default-config entry to allow building QEMU with MSHV (Microsoft Hypervisor) acceleration support. This is the first step toward implementing an MSHV backend in QEMU. Signed-off-by: Magnus Kulke --- accel/Kconfig | 3 +++ meson.build | 10 ++++++++++ meson_options.txt | 2 ++ scripts/meson-buildoptions.sh | 3 +++ 4 files changed, 18 insertions(+) diff --git a/accel/Kconfig b/accel/Kconfig index 4263cab722..a60f114923 100644 --- a/accel/Kconfig +++ b/accel/Kconfig @@ -13,6 +13,9 @@ config TCG config KVM bool =20 +config MSHV + bool + config XEN bool select FSDEV_9P if VIRTFS diff --git a/meson.build b/meson.build index dbc97bfdf7..927f3474ea 100644 --- a/meson.build +++ b/meson.build @@ -334,6 +334,7 @@ elif cpu =3D=3D 'x86_64' 'CONFIG_HVF': ['x86_64-softmmu'], 'CONFIG_NVMM': ['i386-softmmu', 'x86_64-softmmu'], 'CONFIG_WHPX': ['i386-softmmu', 'x86_64-softmmu'], + 'CONFIG_MSHV': ['x86_64-softmmu'], } endif =20 @@ -884,6 +885,14 @@ accelerators =3D [] if get_option('kvm').allowed() and host_os =3D=3D 'linux' accelerators +=3D 'CONFIG_KVM' endif + +if get_option('mshv').allowed() and host_os =3D=3D 'linux' + if get_option('mshv').enabled() and host_machine.cpu() !=3D 'x86_64' + error('mshv accelerator requires x64_64 host') + endif + accelerators +=3D 'CONFIG_MSHV' +endif + if get_option('whpx').allowed() and host_os =3D=3D 'windows' if get_option('whpx').enabled() and host_machine.cpu() !=3D 'x86_64' error('WHPX requires 64-bit host') @@ -4797,6 +4806,7 @@ if have_system summary_info +=3D {'HVF support': config_all_accel.has_key('CONFIG= _HVF')} summary_info +=3D {'WHPX support': config_all_accel.has_key('CONFIG= _WHPX')} summary_info +=3D {'NVMM support': config_all_accel.has_key('CONFIG= _NVMM')} + summary_info +=3D {'MSHV support': config_all_accel.has_key('CONFI= G_MSHV')} summary_info +=3D {'Xen support': xen.found()} if xen.found() summary_info +=3D {'xen ctrl version': xen.version()} diff --git a/meson_options.txt b/meson_options.txt index a442be2995..1c02a4f54b 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -71,6 +71,8 @@ option('malloc', type : 'combo', choices : ['system', 'tc= malloc', 'jemalloc'], =20 option('kvm', type: 'feature', value: 'auto', description: 'KVM acceleration support') +option('mshv', type: 'feature', value: 'auto', + description: 'MSHV acceleration support') option('whpx', type: 'feature', value: 'auto', description: 'WHPX acceleration support') option('hvf', type: 'feature', value: 'auto', diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index 73e0770f42..d21bf4dc33 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -153,6 +153,7 @@ meson_options_help() { printf "%s\n" ' membarrier membarrier system call (for Linux 4.14+= or Windows' printf "%s\n" ' modules modules support (non Windows)' printf "%s\n" ' mpath Multipath persistent reservation passth= rough' + printf "%s\n" ' mshv MSHV acceleration support' printf "%s\n" ' multiprocess Out of process device emulation support' printf "%s\n" ' netmap netmap network backend support' printf "%s\n" ' nettle nettle cryptography support' @@ -404,6 +405,8 @@ _meson_option_parse() { --disable-modules) printf "%s" -Dmodules=3Ddisabled ;; --enable-mpath) printf "%s" -Dmpath=3Denabled ;; --disable-mpath) printf "%s" -Dmpath=3Ddisabled ;; + --enable-mshv) printf "%s" -Dmshv=3Denabled ;; + --disable-mshv) printf "%s" -Dmshv=3Ddisabled ;; --enable-multiprocess) printf "%s" -Dmultiprocess=3Denabled ;; --disable-multiprocess) printf "%s" -Dmultiprocess=3Ddisabled ;; --enable-netmap) printf "%s" -Dnetmap=3Denabled ;; --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751390962; cv=none; d=zohomail.com; s=zohoarc; b=DJK7pOh5Uj72uQU/+9XHRHT5V+/ROcYztnV/2RrxbWzFdBbz6IiPYUP1mrBCgwgAVA72P5VB68Z+7kEBndwekSkExrRdfqGnISl6MCitIZZPmkHftLIcPGdqVn2Dxj8uH9/URjaxLKi5pL6bs49wh6m9l0inTxxw/ssx4Pp3968= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751390962; 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=6liSC5uPnVmbC+x1NjCI6F9/8bBgz4KWGj8cxMl/S4Q=; b=dbJrhdKn0EEc5vbGbiOFxwYrDwLxrk4YVKDijnLqTx2YOURszWKV9768dF6l0RaA7+5xkNrqinpBZdIprVzPbbKES38Agw1r0yl2YHA+eb/P+5Ea/aXfh1HHmckLPsRLOUt00qXzo0cHideGdHuPX1bcTOqTt7HE5QpMBO/N5nY= 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 175139096289366.71491766137603; Tue, 1 Jul 2025 10:29:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenC-0002RV-QA; Tue, 01 Jul 2025 13:29:15 -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 1uWenB-0002RF-Jb for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:13 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWen9-0006Us-Ct for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:13 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id AD28D211220B; Tue, 1 Jul 2025 10:29:06 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com AD28D211220B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390950; bh=6liSC5uPnVmbC+x1NjCI6F9/8bBgz4KWGj8cxMl/S4Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NmoKriVeq7os7hDGL4WsEfKVRckoEG0CF1bDsJZeikVc4QBOF3BIy+waQZU4SQfbv e0Ojal6JBqdJlzD3/g/GnppzwkElvPRLwiifrS/y8uuArQwCqP7C5oKKMWmY5hYE9c 1z3sMGyj/KJAuYfOc925u5MtMGVjnSeuCsRJVrgk= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 02/27] target/i386/emulate: Allow instruction decoding from stream Date: Tue, 1 Jul 2025 19:28:09 +0200 Message-Id: <20250701172834.44849-3-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751390964739116600 Content-Type: text/plain; charset="utf-8" Introduce a new helper function to decode x86 instructions from a raw instruction byte stream. MSHV delivers an instruction stream in a buffer of the vm_exit message. It can be used to speed up MMIO emulation, since instructions do not have to be fetched and translated. Added "fetch_instruction()" op to x86_emul_ops() to improve traceability. Signed-off-by: Magnus Kulke --- target/i386/emulate/x86_decode.c | 31 +++++++++++++++++++++++++++---- target/i386/emulate/x86_decode.h | 10 ++++++++++ target/i386/emulate/x86_emu.c | 3 ++- target/i386/emulate/x86_emu.h | 1 + 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/target/i386/emulate/x86_decode.c b/target/i386/emulate/x86_dec= ode.c index 2eca39802e..133065b50a 100644 --- a/target/i386/emulate/x86_decode.c +++ b/target/i386/emulate/x86_decode.c @@ -60,6 +60,7 @@ static inline uint64_t decode_bytes(CPUX86State *env, str= uct x86_decode *decode, int size) { uint64_t val =3D 0; + target_ulong va; =20 switch (size) { case 1: @@ -71,10 +72,17 @@ static inline uint64_t decode_bytes(CPUX86State *env, s= truct x86_decode *decode, VM_PANIC_EX("%s invalid size %d\n", __func__, size); break; } - target_ulong va =3D linear_rip(env_cpu(env), env->eip) + decode->len; - emul_ops->read_mem(env_cpu(env), &val, va, size); + + /* copy the bytes from the instruction stream, if available */ + if (decode->stream && decode->len + size <=3D decode->stream->len) { + memcpy(&val, decode->stream->bytes + decode->len, size); + } else { + va =3D linear_rip(env_cpu(env), env->eip) + decode->len; + emul_ops->fetch_instruction(env_cpu(env), &val, va, size); + } decode->len +=3D size; - =20 + + return val; } =20 @@ -2076,9 +2084,10 @@ static void decode_opcodes(CPUX86State *env, struct = x86_decode *decode) } } =20 -uint32_t decode_instruction(CPUX86State *env, struct x86_decode *decode) +static uint32_t decode_opcode(CPUX86State *env, struct x86_decode *decode) { memset(decode, 0, sizeof(*decode)); + decode_prefix(env, decode); set_addressing_size(env, decode); set_operand_size(env, decode); @@ -2088,6 +2097,20 @@ uint32_t decode_instruction(CPUX86State *env, struct= x86_decode *decode) return decode->len; } =20 +uint32_t decode_instruction(CPUX86State *env, struct x86_decode *decode) +{ + return decode_opcode(env, decode); +} + +uint32_t decode_instruction_stream(CPUX86State *env, struct x86_decode *de= code, + struct x86_insn_stream *stream) +{ + if (stream !=3D NULL) { + decode->stream =3D stream; + } + return decode_opcode(env, decode); +} + void init_decoder(void) { int i; diff --git a/target/i386/emulate/x86_decode.h b/target/i386/emulate/x86_dec= ode.h index 927645af1a..f5e9738914 100644 --- a/target/i386/emulate/x86_decode.h +++ b/target/i386/emulate/x86_decode.h @@ -272,6 +272,11 @@ typedef struct x86_decode_op { }; } x86_decode_op; =20 +typedef struct x86_insn_stream { + const uint8_t *bytes; + size_t len; +} x86_insn_stream; + typedef struct x86_decode { int len; uint8_t opcode[4]; @@ -298,11 +303,16 @@ typedef struct x86_decode { struct x86_modrm modrm; struct x86_decode_op op[4]; bool is_fpu; + + x86_insn_stream *stream; } x86_decode; =20 uint64_t sign(uint64_t val, int size); =20 uint32_t decode_instruction(CPUX86State *env, struct x86_decode *decode); +uint32_t decode_instruction_stream(CPUX86State *env, + struct x86_decode *decode, + struct x86_insn_stream *stream); =20 void *get_reg_ref(CPUX86State *env, int reg, int rex_present, int is_extended, int size); diff --git a/target/i386/emulate/x86_emu.c b/target/i386/emulate/x86_emu.c index db7a7f7437..f7e6bf01bc 100644 --- a/target/i386/emulate/x86_emu.c +++ b/target/i386/emulate/x86_emu.c @@ -1246,7 +1246,8 @@ static void init_cmd_handler(void) bool exec_instruction(CPUX86State *env, struct x86_decode *ins) { if (!_cmd_handler[ins->cmd].handler) { - printf("Unimplemented handler (" TARGET_FMT_lx ") for %d (%x %x) \= n", env->eip, + printf("Unimplemented handler (" TARGET_FMT_lx ") for %d (%x %x) \= n", + env->eip, ins->cmd, ins->opcode[0], ins->opcode_len > 1 ? ins->opcode[1] : 0); env->eip +=3D ins->len; diff --git a/target/i386/emulate/x86_emu.h b/target/i386/emulate/x86_emu.h index a1a961284b..f1680c41f4 100644 --- a/target/i386/emulate/x86_emu.h +++ b/target/i386/emulate/x86_emu.h @@ -24,6 +24,7 @@ #include "cpu.h" =20 struct x86_emul_ops { + void (*fetch_instruction)(CPUState *cpu, void *data, target_ulong addr= , int bytes); void (*read_mem)(CPUState *cpu, void *data, target_ulong addr, int byt= es); void (*write_mem)(CPUState *cpu, void *data, target_ulong addr, int by= tes); void (*read_segment_descriptor)(CPUState *cpu, struct x86_segment_desc= riptor *desc, --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751390983; cv=none; d=zohomail.com; s=zohoarc; b=jb5lgiVjQZ4ZVjcwRi+Y4PjszYQpP1D42KxL8/xGz92FAVp/V7FaDXIUjbmLacZsBvObuURWSm4soUE2FqFyIh4kOJAyJLTN8oiiqXL/p7BYSO8Ihed8mU8S2ci9kg2ACa8aGKf106A07o/+x0mEKIwd20QJ34IRtltnzkH8s5s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751390983; 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=EWCKlb6Nhh0QNsC/EhDfeSFEihSY0t16npg7RScTWcI=; b=jbOV2Jc+KQX+0gGYFdtXXe9+adyqQW/PSKKvAHcHIosM54rvJETgDtGSaRMEG1Ewp/ehKdGnFed8q9ZLJC1yLgO1/pxmd+8bCEzmbpInaVcy3GGVn+zi1qlqK2Q2baCDqZjuFj9zHW5HEyOi/rpkDmYaGVQFPLHZYE8Hg2vjb5o= 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 1751390983680539.5047190919496; Tue, 1 Jul 2025 10:29:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenJ-0002nB-P3; Tue, 01 Jul 2025 13:29:21 -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 1uWenI-0002ib-7l for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:20 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenD-0006X3-QF for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:19 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id AC552211221D; Tue, 1 Jul 2025 10:29:10 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com AC552211221D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390954; bh=EWCKlb6Nhh0QNsC/EhDfeSFEihSY0t16npg7RScTWcI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VLQ+mX0dw114tTjyFelO2s3w7vyQI+kU2DU/8PifwbI0dTraukTySvqc92wP5JpuW Vd3h84Iu6lrHJlksCLatpajlKtgf078S5iCCQlFIn2LcVc5hdxV9l0xRgyb1SA4tSm yxKTuqcjq+Inm5cgThC8VKLdhKqiCEs40dHUsnUc= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 03/27] target/i386/mshv: Add x86 decoder/emu implementation Date: Tue, 1 Jul 2025 19:28:10 +0200 Message-Id: <20250701172834.44849-4-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751390984835116600 Content-Type: text/plain; charset="utf-8" The MSHV accelerator requires a x86 decoder/emulator in userland to emulate MMIO instructions. This change contains the implementations for the generalized i386 instruction decoder/emulator. Signed-off-by: Magnus Kulke --- include/system/mshv.h | 25 +++ target/i386/cpu.h | 2 +- target/i386/emulate/meson.build | 7 +- target/i386/meson.build | 2 + target/i386/mshv/meson.build | 7 + target/i386/mshv/x86.c | 296 ++++++++++++++++++++++++++++++++ 6 files changed, 336 insertions(+), 3 deletions(-) create mode 100644 include/system/mshv.h create mode 100644 target/i386/mshv/meson.build create mode 100644 target/i386/mshv/x86.c diff --git a/include/system/mshv.h b/include/system/mshv.h new file mode 100644 index 0000000000..a971982b52 --- /dev/null +++ b/include/system/mshv.h @@ -0,0 +1,25 @@ +/* + * QEMU MSHV support + * + * Copyright Microsoft, Corp. 2025 + * + * Authors: Ziqiao Zhou + * Magnus Kulke + * Jinank Jain + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + */ + +#ifndef QEMU_MSHV_INT_H +#define QEMU_MSHV_INT_H + +#ifdef COMPILING_PER_TARGET +#ifdef CONFIG_MSHV +#define CONFIG_MSHV_IS_POSSIBLE +#endif +#else +#define CONFIG_MSHV_IS_POSSIBLE +#endif + +#endif diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 51e10139df..48979432f6 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -2122,7 +2122,7 @@ typedef struct CPUArchState { QEMUTimer *xen_periodic_timer; QemuMutex xen_timers_lock; #endif -#if defined(CONFIG_HVF) +#if defined(CONFIG_HVF) || defined(CONFIG_MSHV) void *emu_mmio_buf; #endif =20 diff --git a/target/i386/emulate/meson.build b/target/i386/emulate/meson.bu= ild index 4edd4f462f..b6dafb6a5b 100644 --- a/target/i386/emulate/meson.build +++ b/target/i386/emulate/meson.build @@ -1,5 +1,8 @@ -i386_system_ss.add(when: [hvf, 'CONFIG_HVF'], if_true: files( +emulator_files =3D files( 'x86_decode.c', 'x86_emu.c', 'x86_flags.c', -)) +) + +i386_system_ss.add(when: [hvf, 'CONFIG_HVF'], if_true: emulator_files) +i386_system_ss.add(when: 'CONFIG_MSHV', if_true: emulator_files) diff --git a/target/i386/meson.build b/target/i386/meson.build index c1aacea613..6097e5c427 100644 --- a/target/i386/meson.build +++ b/target/i386/meson.build @@ -11,6 +11,7 @@ i386_ss.add(when: 'CONFIG_SEV', if_true: files('host-cpu.= c', 'confidential-guest # x86 cpu type i386_ss.add(when: 'CONFIG_KVM', if_true: files('host-cpu.c')) i386_ss.add(when: 'CONFIG_HVF', if_true: files('host-cpu.c')) +i386_ss.add(when: 'CONFIG_MSHV', if_true: files('host-cpu.c')) =20 i386_system_ss =3D ss.source_set() i386_system_ss.add(files( @@ -32,6 +33,7 @@ subdir('nvmm') subdir('hvf') subdir('tcg') subdir('emulate') +subdir('mshv') =20 target_arch +=3D {'i386': i386_ss} target_system_arch +=3D {'i386': i386_system_ss} diff --git a/target/i386/mshv/meson.build b/target/i386/mshv/meson.build new file mode 100644 index 0000000000..8ddaa7c11d --- /dev/null +++ b/target/i386/mshv/meson.build @@ -0,0 +1,7 @@ +i386_mshv_ss =3D ss.source_set() + +i386_mshv_ss.add(files( + 'x86.c', +)) + +i386_system_ss.add_all(when: 'CONFIG_MSHV', if_true: i386_mshv_ss) diff --git a/target/i386/mshv/x86.c b/target/i386/mshv/x86.c new file mode 100644 index 0000000000..54c40b8064 --- /dev/null +++ b/target/i386/mshv/x86.c @@ -0,0 +1,296 @@ +/* + * QEMU MSHV support + * + * Copyright Microsoft, Corp. 2025 + * + * Authors: Magnus Kulke + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" + +#include "cpu.h" +#include "emulate/x86_decode.h" +#include "emulate/x86_emu.h" +#include "qemu/typedefs.h" +#include "qemu/error-report.h" +#include "system/mshv.h" + +/* RW or Exec segment */ +static const uint8_t RWRX_SEGMENT_TYPE =3D 0x2; +static const uint8_t CODE_SEGMENT_TYPE =3D 0x8; +static const uint8_t EXPAND_DOWN_SEGMENT_TYPE =3D 0x4; + +typedef enum CpuMode { + REAL_MODE, + PROTECTED_MODE, + LONG_MODE, +} CpuMode; + +static CpuMode cpu_mode(CPUState *cpu) +{ + enum CpuMode m =3D REAL_MODE; + + if (x86_is_protected(cpu)) { + m =3D PROTECTED_MODE; + + if (x86_is_long_mode(cpu)) { + m =3D LONG_MODE; + } + } + + return m; +} + +static bool segment_type_ro(const SegmentCache *seg) +{ + uint32_t type_ =3D (seg->flags >> DESC_TYPE_SHIFT) & 15; + return (type_ & (~RWRX_SEGMENT_TYPE)) =3D=3D 0; +} + +static bool segment_type_code(const SegmentCache *seg) +{ + uint32_t type_ =3D (seg->flags >> DESC_TYPE_SHIFT) & 15; + return (type_ & CODE_SEGMENT_TYPE) !=3D 0; +} + +static bool segment_expands_down(const SegmentCache *seg) +{ + uint32_t type_ =3D (seg->flags >> DESC_TYPE_SHIFT) & 15; + + if (segment_type_code(seg)) { + return false; + } + + return (type_ & EXPAND_DOWN_SEGMENT_TYPE) !=3D 0; +} + +static uint32_t segment_limit(const SegmentCache *seg) +{ + uint32_t limit =3D seg->limit; + uint32_t granularity =3D (seg->flags & DESC_G_MASK) !=3D 0; + + if (granularity !=3D 0) { + limit =3D (limit << 12) | 0xFFF; + } + + return limit; +} + +static uint8_t segment_db(const SegmentCache *seg) +{ + return (seg->flags >> DESC_B_SHIFT) & 1; +} + +static uint32_t segment_max_limit(const SegmentCache *seg) +{ + if (segment_db(seg) !=3D 0) { + return 0xFFFFFFFF; + } + return 0xFFFF; +} + +static int linearize(CPUState *cpu, + target_ulong logical_addr, target_ulong *linear_addr, + X86Seg seg_idx) +{ + enum CpuMode mode; + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + SegmentCache *seg =3D &env->segs[seg_idx]; + target_ulong base =3D seg->base; + target_ulong logical_addr_32b; + uint32_t limit; + /* TODO: the emulator will not pass us "write" indicator yet */ + bool write =3D false; + + mode =3D cpu_mode(cpu); + + switch (mode) { + case LONG_MODE: + if (__builtin_add_overflow(logical_addr, base, linear_addr)) { + error_report("Address overflow"); + return -1; + } + break; + case PROTECTED_MODE: + case REAL_MODE: + if (segment_type_ro(seg) && write) { + error_report("Cannot write to read-only segment"); + return -1; + } + + logical_addr_32b =3D logical_addr & 0xFFFFFFFF; + limit =3D segment_limit(seg); + + if (segment_expands_down(seg)) { + if (logical_addr_32b >=3D limit) { + error_report("Address exceeds limit (expands down)"); + return -1; + } + + limit =3D segment_max_limit(seg); + } + + if (logical_addr_32b > limit) { + error_report("Address exceeds limit %u", limit); + return -1; + } + *linear_addr =3D logical_addr_32b + base; + break; + default: + error_report("Unknown cpu mode: %d", mode); + return -1; + } + + return 0; +} + +bool x86_read_segment_descriptor(CPUState *cpu, + struct x86_segment_descriptor *desc, + x86_segment_selector sel) +{ + target_ulong base; + uint32_t limit; + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + target_ulong gva; + + memset(desc, 0, sizeof(*desc)); + + /* valid gdt descriptors start from index 1 */ + if (!sel.index && GDT_SEL =3D=3D sel.ti) { + return false; + } + + if (GDT_SEL =3D=3D sel.ti) { + base =3D env->gdt.base; + limit =3D env->gdt.limit; + } else { + base =3D env->ldt.base; + limit =3D env->ldt.limit; + } + + if (sel.index * 8 >=3D limit) { + return false; + } + + gva =3D base + sel.index * 8; + emul_ops->read_mem(cpu, desc, gva, sizeof(*desc)); + + return true; +} + +bool x86_read_call_gate(CPUState *cpu, struct x86_call_gate *idt_desc, + int gate) +{ + target_ulong base; + uint32_t limit; + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + target_ulong gva; + + base =3D env->idt.base; + limit =3D env->idt.limit; + + memset(idt_desc, 0, sizeof(*idt_desc)); + if (gate * 8 >=3D limit) { + perror("call gate exceeds idt limit"); + return false; + } + + gva =3D base + gate * 8; + emul_ops->read_mem(cpu, idt_desc, gva, sizeof(*idt_desc)); + + return true; +} + +bool x86_is_protected(CPUState *cpu) +{ + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + uint64_t cr0 =3D env->cr[0]; + + return cr0 & CR0_PE_MASK; +} + +bool x86_is_real(CPUState *cpu) +{ + return !x86_is_protected(cpu); +} + +bool x86_is_v8086(CPUState *cpu) +{ + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + return x86_is_protected(cpu) && (env->eflags & VM_MASK); +} + +bool x86_is_long_mode(CPUState *cpu) +{ + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + uint64_t efer =3D env->efer; + + return ((efer & (EFER_LME | EFER_LMA)) =3D=3D (EFER_LME | EFER_LMA)); +} + +bool x86_is_long64_mode(CPUState *cpu) +{ + error_report("unimplemented: is_long64_mode()"); + abort(); +} + +bool x86_is_paging_mode(CPUState *cpu) +{ + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + uint64_t cr0 =3D env->cr[0]; + + return cr0 & CR0_PG_MASK; +} + +bool x86_is_pae_enabled(CPUState *cpu) +{ + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + uint64_t cr4 =3D env->cr[4]; + + return cr4 & CR4_PAE_MASK; +} + +target_ulong linear_addr(CPUState *cpu, target_ulong addr, X86Seg seg) +{ + int ret; + target_ulong linear_addr; + + ret =3D linearize(cpu, addr, &linear_addr, seg); + if (ret < 0) { + error_report("failed to linearize address"); + abort(); + } + + return linear_addr; +} + +target_ulong linear_addr_size(CPUState *cpu, target_ulong addr, int size, + X86Seg seg) +{ + switch (size) { + case 2: + addr =3D (uint16_t)addr; + break; + case 4: + addr =3D (uint32_t)addr; + break; + default: + break; + } + return linear_addr(cpu, addr, seg); +} + +target_ulong linear_rip(CPUState *cpu, target_ulong rip) +{ + return linear_addr(cpu, rip, R_CS); +} --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751390991; cv=none; d=zohomail.com; s=zohoarc; b=AfTq6z+vK7jDrvJWM0GroenBZCY66uPN91c9zV7JgIpSLKLwq7Qc8+gP7g/0dJGvp6lOpUx6uER31SnK00NMNM4V5BEH4RMU+wbjJVX+ZqSQlmRejxqNf+Ssu39msYttuFNmq3I7yPiQWFx/k3uAjmbe3fP4GyvAseWwnOuKwVo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751390991; 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=jLJRI9UQwPQ7QvAFK4jr1bU+zUCcMtzt90fSPxsEtvE=; b=a1u38R+WpnqspXi3ap7ogM82k3iPEwDFeK6iStfNZg2/1E9At802Pa5RkEoqTA0nbx7aB8nqcJbl8oo9oDzjr64LjsYBnNjTen9RrTkc6bpbiq87gmjbnhShRpm6sC3V7lrqD3BdbtmdlF0Ah5rq6b5fYz4KaexouJxcS6hy0Rw= 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 1751390991069759.1556916887818; Tue, 1 Jul 2025 10:29:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenP-0002wz-PU; Tue, 01 Jul 2025 13:29:29 -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 1uWenL-0002us-DI for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:23 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenH-0006Yp-RJ for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:23 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id D359B2112221; Tue, 1 Jul 2025 10:29:14 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com D359B2112221 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390958; bh=jLJRI9UQwPQ7QvAFK4jr1bU+zUCcMtzt90fSPxsEtvE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LxWDnhO+NNXBZF1fBxlpvI+0D3dT4+gIHIS2VodDiCv6/NjDmGlQD7XAn+RJ9AdPL rm0DMjSnhtd1DehUeyI4pjYzCE+sWE6Pn6Y18UIzJgl36pJ3E0EB2pWCzQz+/j8ytt djwj2kaCjJNNFwrC04eyYGLcp5EpqmxxyNC0DaIE= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 04/27] hw/intc: Generalize APIC helper names from kvm_* to accel_* Date: Tue, 1 Jul 2025 19:28:11 +0200 Message-Id: <20250701172834.44849-5-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751390993429116600 Content-Type: text/plain; charset="utf-8" Rename APIC helper functions to use an accel_* prefix instead of kvm_* to support use by accelerators other than KVM. This is a preparatory step for integrating MSHV support with common APIC logic. Signed-off-by: Magnus Kulke --- accel/accel-irq.c | 95 ++++++++++++++++++++++++++++++++++++++ accel/meson.build | 2 +- hw/intc/ioapic.c | 20 +++++--- hw/virtio/virtio-pci.c | 21 +++++---- include/system/accel-irq.h | 26 +++++++++++ include/system/mshv.h | 21 +++++++++ 6 files changed, 167 insertions(+), 18 deletions(-) create mode 100644 accel/accel-irq.c create mode 100644 include/system/accel-irq.h diff --git a/accel/accel-irq.c b/accel/accel-irq.c new file mode 100644 index 0000000000..63f8ed260a --- /dev/null +++ b/accel/accel-irq.c @@ -0,0 +1,95 @@ +#include "qemu/osdep.h" +#include "hw/pci/msi.h" + +#include "system/kvm.h" +#include "system/mshv.h" +#include "system/accel-irq.h" + +int accel_irqchip_add_msi_route(KVMRouteChange *c, int vector, PCIDevice *= dev) +{ +#ifdef CONFIG_MSHV_IS_POSSIBLE + if (mshv_msi_via_irqfd_enabled()) { + return mshv_irqchip_add_msi_route(vector, dev); + } +#endif + if (kvm_enabled()) { + return kvm_irqchip_add_msi_route(c, vector, dev); + } + return -ENOSYS; +} + +int accel_irqchip_update_msi_route(int vector, MSIMessage msg, PCIDevice *= dev) +{ +#ifdef CONFIG_MSHV_IS_POSSIBLE + if (mshv_msi_via_irqfd_enabled()) { + return mshv_irqchip_update_msi_route(vector, msg, dev); + } +#endif + if (kvm_enabled()) { + return kvm_irqchip_update_msi_route(kvm_state, vector, msg, dev); + } + return -ENOSYS; +} + +void accel_irqchip_commit_route_changes(KVMRouteChange *c) +{ +#ifdef CONFIG_MSHV_IS_POSSIBLE + if (mshv_msi_via_irqfd_enabled()) { + mshv_irqchip_commit_routes(); + } +#endif + if (kvm_enabled()) { + kvm_irqchip_commit_route_changes(c); + } +} + +void accel_irqchip_commit_routes(void) +{ +#ifdef CONFIG_MSHV_IS_POSSIBLE + if (mshv_msi_via_irqfd_enabled()) { + mshv_irqchip_commit_routes(); + } +#endif + if (kvm_enabled()) { + kvm_irqchip_commit_routes(kvm_state); + } +} + +void accel_irqchip_release_virq(int virq) +{ +#ifdef CONFIG_MSHV_IS_POSSIBLE + if (mshv_msi_via_irqfd_enabled()) { + mshv_irqchip_release_virq(virq); + } +#endif + if (kvm_enabled()) { + kvm_irqchip_release_virq(kvm_state, virq); + } +} + +int accel_irqchip_add_irqfd_notifier_gsi(EventNotifier *n, EventNotifier *= rn, + int virq) +{ +#ifdef CONFIG_MSHV_IS_POSSIBLE + if (mshv_msi_via_irqfd_enabled()) { + return mshv_irqchip_add_irqfd_notifier_gsi(n, rn, virq); + } +#endif + if (kvm_enabled()) { + return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, rn, virq); + } + return -ENOSYS; +} + +int accel_irqchip_remove_irqfd_notifier_gsi(EventNotifier *n, int virq) +{ +#ifdef CONFIG_MSHV_IS_POSSIBLE + if (mshv_msi_via_irqfd_enabled()) { + return mshv_irqchip_remove_irqfd_notifier_gsi(n, virq); + } +#endif + if (kvm_enabled()) { + return kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, virq); + } + return -ENOSYS; +} diff --git a/accel/meson.build b/accel/meson.build index 52909314bf..d5e982d152 100644 --- a/accel/meson.build +++ b/accel/meson.build @@ -1,6 +1,6 @@ common_ss.add(files('accel-common.c')) specific_ss.add(files('accel-target.c')) -system_ss.add(files('accel-system.c', 'accel-blocker.c')) +system_ss.add(files('accel-system.c', 'accel-blocker.c', 'accel-irq.c')) user_ss.add(files('accel-user.c')) =20 subdir('tcg') diff --git a/hw/intc/ioapic.c b/hw/intc/ioapic.c index 133bef852d..e431d00311 100644 --- a/hw/intc/ioapic.c +++ b/hw/intc/ioapic.c @@ -30,12 +30,18 @@ #include "hw/intc/ioapic_internal.h" #include "hw/pci/msi.h" #include "hw/qdev-properties.h" +#include "system/accel-irq.h" #include "system/kvm.h" #include "system/system.h" #include "hw/i386/apic-msidef.h" #include "hw/i386/x86-iommu.h" #include "trace.h" =20 + +#if defined(CONFIG_KVM) || defined(CONFIG_MSHV) +#define ACCEL_GSI_IRQFD_POSSIBLE +#endif + #define APIC_DELIVERY_MODE_SHIFT 8 #define APIC_POLARITY_SHIFT 14 #define APIC_TRIG_MODE_SHIFT 15 @@ -191,10 +197,10 @@ static void ioapic_set_irq(void *opaque, int vector, = int level) =20 static void ioapic_update_kvm_routes(IOAPICCommonState *s) { -#ifdef CONFIG_KVM +#ifdef ACCEL_GSI_IRQFD_POSSIBLE int i; =20 - if (kvm_irqchip_is_split()) { + if (accel_irqchip_is_split()) { for (i =3D 0; i < IOAPIC_NUM_PINS; i++) { MSIMessage msg; struct ioapic_entry_info info; @@ -202,15 +208,15 @@ static void ioapic_update_kvm_routes(IOAPICCommonStat= e *s) if (!info.masked) { msg.address =3D info.addr; msg.data =3D info.data; - kvm_irqchip_update_msi_route(kvm_state, i, msg, NULL); + accel_irqchip_update_msi_route(i, msg, NULL); } } - kvm_irqchip_commit_routes(kvm_state); + accel_irqchip_commit_routes(); } #endif } =20 -#ifdef CONFIG_KVM +#ifdef ACCEL_KERNEL_GSI_IRQFD_POSSIBLE static void ioapic_iec_notifier(void *private, bool global, uint32_t index, uint32_t mask) { @@ -428,11 +434,11 @@ static const MemoryRegionOps ioapic_io_ops =3D { =20 static void ioapic_machine_done_notify(Notifier *notifier, void *data) { -#ifdef CONFIG_KVM +#ifdef ACCEL_KERNEL_GSI_IRQFD_POSSIBLE IOAPICCommonState *s =3D container_of(notifier, IOAPICCommonState, machine_done); =20 - if (kvm_irqchip_is_split()) { + if (accel_irqchip_is_split()) { X86IOMMUState *iommu =3D x86_iommu_get_default(); if (iommu) { /* Register this IOAPIC with IOMMU IEC notifier, so that diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index fba2372c93..a582bb082d 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -33,6 +33,7 @@ #include "hw/pci/msi.h" #include "hw/pci/msix.h" #include "hw/loader.h" +#include "system/accel-irq.h" #include "system/kvm.h" #include "hw/virtio/virtio-pci.h" #include "qemu/range.h" @@ -824,11 +825,11 @@ static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProx= y *proxy, =20 if (irqfd->users =3D=3D 0) { KVMRouteChange c =3D kvm_irqchip_begin_route_changes(kvm_state); - ret =3D kvm_irqchip_add_msi_route(&c, vector, &proxy->pci_dev); + ret =3D accel_irqchip_add_msi_route(&c, vector, &proxy->pci_dev); if (ret < 0) { return ret; } - kvm_irqchip_commit_route_changes(&c); + accel_irqchip_commit_route_changes(&c); irqfd->virq =3D ret; } irqfd->users++; @@ -840,7 +841,7 @@ static void kvm_virtio_pci_vq_vector_release(VirtIOPCIP= roxy *proxy, { VirtIOIRQFD *irqfd =3D &proxy->vector_irqfd[vector]; if (--irqfd->users =3D=3D 0) { - kvm_irqchip_release_virq(kvm_state, irqfd->virq); + accel_irqchip_release_virq(irqfd->virq); } } =20 @@ -849,7 +850,7 @@ static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *pro= xy, unsigned int vector) { VirtIOIRQFD *irqfd =3D &proxy->vector_irqfd[vector]; - return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->v= irq); + return accel_irqchip_add_irqfd_notifier_gsi(n, NULL, irqfd->virq); } =20 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, @@ -859,7 +860,7 @@ static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy= *proxy, VirtIOIRQFD *irqfd =3D &proxy->vector_irqfd[vector]; int ret; =20 - ret =3D kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->vir= q); + ret =3D accel_irqchip_remove_irqfd_notifier_gsi(n, irqfd->virq); assert(ret =3D=3D 0); } static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no, @@ -994,12 +995,12 @@ static int virtio_pci_one_vector_unmask(VirtIOPCIProx= y *proxy, if (proxy->vector_irqfd) { irqfd =3D &proxy->vector_irqfd[vector]; if (irqfd->msg.data !=3D msg.data || irqfd->msg.address !=3D msg.a= ddress) { - ret =3D kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, m= sg, - &proxy->pci_dev); + ret =3D accel_irqchip_update_msi_route(irqfd->virq, msg, + &proxy->pci_dev); if (ret < 0) { return ret; } - kvm_irqchip_commit_routes(kvm_state); + accel_irqchip_commit_routes(); } } =20 @@ -1228,7 +1229,7 @@ static int virtio_pci_set_guest_notifiers(DeviceState= *d, int nvqs, bool assign) VirtioDeviceClass *k =3D VIRTIO_DEVICE_GET_CLASS(vdev); int r, n; bool with_irqfd =3D msix_enabled(&proxy->pci_dev) && - kvm_msi_via_irqfd_enabled(); + accel_msi_via_irqfd_enabled() ; =20 nvqs =3D MIN(nvqs, VIRTIO_QUEUE_MAX); =20 @@ -1432,7 +1433,7 @@ static void virtio_pci_set_vector(VirtIODevice *vdev, uint16_t new_vector) { bool kvm_irqfd =3D (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) && - msix_enabled(&proxy->pci_dev) && kvm_msi_via_irqfd_enabled(); + msix_enabled(&proxy->pci_dev) && accel_msi_via_irqfd_enabled(); =20 if (new_vector =3D=3D old_vector) { return; diff --git a/include/system/accel-irq.h b/include/system/accel-irq.h new file mode 100644 index 0000000000..8d17fe45ea --- /dev/null +++ b/include/system/accel-irq.h @@ -0,0 +1,26 @@ +#ifndef SYSEMU_ACCEL_H +#define SYSEMU_ACCEL_H +#include "hw/pci/msi.h" +#include "qemu/osdep.h" +#include "system/kvm.h" +#include "system/mshv.h" + +static inline bool accel_msi_via_irqfd_enabled(void) +{ + return mshv_msi_via_irqfd_enabled() || kvm_msi_via_irqfd_enabled(); +} + +static inline bool accel_irqchip_is_split(void) +{ + return mshv_msi_via_irqfd_enabled() || kvm_irqchip_is_split(); +} + +int accel_irqchip_add_msi_route(KVMRouteChange *c, int vector, PCIDevice *= dev); +int accel_irqchip_update_msi_route(int vector, MSIMessage msg, PCIDevice *= dev); +void accel_irqchip_commit_route_changes(KVMRouteChange *c); +void accel_irqchip_commit_routes(void); +void accel_irqchip_release_virq(int virq); +int accel_irqchip_add_irqfd_notifier_gsi(EventNotifier *n, EventNotifier *= rn, + int virq); +int accel_irqchip_remove_irqfd_notifier_gsi(EventNotifier *n, int virq); +#endif diff --git a/include/system/mshv.h b/include/system/mshv.h index a971982b52..a358691428 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -22,4 +22,25 @@ #define CONFIG_MSHV_IS_POSSIBLE #endif =20 +#ifdef CONFIG_MSHV_IS_POSSIBLE +extern bool mshv_allowed; +#define mshv_enabled() (mshv_allowed) +#else /* CONFIG_MSHV_IS_POSSIBLE */ +#define mshv_enabled() false +#endif +#ifdef MSHV_USE_KERNEL_GSI_IRQFD +#define mshv_msi_via_irqfd_enabled() mshv_enabled() +#else +#define mshv_msi_via_irqfd_enabled() false +#endif + +/* interrupt */ +int mshv_irqchip_add_msi_route(int vector, PCIDevice *dev); +int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev= ); +void mshv_irqchip_commit_routes(void); +void mshv_irqchip_release_virq(int virq); +int mshv_irqchip_add_irqfd_notifier_gsi(const EventNotifier *n, + const EventNotifier *rn, int virq); +int mshv_irqchip_remove_irqfd_notifier_gsi(const EventNotifier *n, int vir= q); + #endif --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391006; cv=none; d=zohomail.com; s=zohoarc; b=fpglTRHVgOBNyxN0UtTPTN0XxWuaQitbze54vt3Twl2jBpRcyqUXANDTRPpN0XXGeYWtBp220VlhhS1cyxVCAdOtqo/GQUhFWqgLCSjn7JtK5b2X/Pj/0k1IN2odZBjWUDS1OY7CFPE4Wpvnjo8xHkiCYU1tH2TIwFbEwy4OXl0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391006; 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=kNodfKMmlx5eKrp/YYzpoCrE07qG2omSWqEOJ1/C8Cs=; b=H7K3x/WdsPVmKeX1Xw616vWIwzlyLA3VoGUNKka0GHmsFqevokU9a+bs/1dRAGaCB6fVsd7aHlf95gNCFcYAkTsm1WpVKpTIVBzvic2stbUb1ZNvxKg14b0s11Q4qMhXldpqJYtV11MEDd/O7BytqNylXIItkTBgoe6p302drVM= 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 17513910068811019.2623384805999; Tue, 1 Jul 2025 10:30:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenc-0003Ah-Ro; Tue, 01 Jul 2025 13:29:40 -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 1uWenT-00031l-9H for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:32 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenM-0006at-OX for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:30 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id D34A52112229; Tue, 1 Jul 2025 10:29:18 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com D34A52112229 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390962; bh=kNodfKMmlx5eKrp/YYzpoCrE07qG2omSWqEOJ1/C8Cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VrU/K8vqHcG1w7fzXsLyMq5n12WwWpfuBwxtTmfn6tT6z2AM+WhsGPTURcU18Tzfz tDgjBATMPnx/lizZdEFljeNUQizTFzPLUuHA1TzJB3XUBl79WS+8FFHYDJSot0qPHR WkqrNHxNnXiaWL8CbVJE0U0X0C7w2cOkSWsFqEkg= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 05/27] include/hw/hyperv: Add MSHV ABI header definitions Date: Tue, 1 Jul 2025 19:28:12 +0200 Message-Id: <20250701172834.44849-6-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391014602116600 Content-Type: text/plain; charset="utf-8" Introduce headers for the Microsoft Hypervisor (MSHV) userspace ABI, including IOCTLs and structures used to interface with the hypervisor. These definitions are based on the upstream Linux MSHV interface and will be used by the MSHV accelerator backend in later patches. Note that for the time being the header `linux-mshv.h` is also being included to allow building on machines that do not ship the header yet. The header will be available in kernel 6.15 (at the time of writing we're at -rc6) we will probably drop it in later revisions of the patch set. Signed-off-by: Magnus Kulke --- include/hw/hyperv/hvgdk.h | 19 + include/hw/hyperv/hvhdk.h | 164 +++++ include/hw/hyperv/hvhdk_mini.h | 105 ++++ linux-headers/linux/mshv.h | 1038 +++++++++++++++++++++++++++++++ scripts/update-linux-headers.sh | 2 +- 5 files changed, 1327 insertions(+), 1 deletion(-) create mode 100644 include/hw/hyperv/hvgdk.h create mode 100644 include/hw/hyperv/hvhdk.h create mode 100644 include/hw/hyperv/hvhdk_mini.h create mode 100644 linux-headers/linux/mshv.h diff --git a/include/hw/hyperv/hvgdk.h b/include/hw/hyperv/hvgdk.h new file mode 100644 index 0000000000..d37c2b188d --- /dev/null +++ b/include/hw/hyperv/hvgdk.h @@ -0,0 +1,19 @@ +/* + * Type definitions for the mshv guest interface. + * + * Copyright Microsoft, Corp. 2025 + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef _HVGDK_H +#define _HVGDK_H + +#define HVGDK_H_VERSION (25125) + +enum hv_unimplemented_msr_action { + HV_UNIMPLEMENTED_MSR_ACTION_FAULT =3D 0, + HV_UNIMPLEMENTED_MSR_ACTION_IGNORE_WRITE_READ_ZERO =3D 1, + HV_UNIMPLEMENTED_MSR_ACTION_COUNT =3D 2, +}; + +#endif /* _HVGDK_H */ diff --git a/include/hw/hyperv/hvhdk.h b/include/hw/hyperv/hvhdk.h new file mode 100644 index 0000000000..d22cc49742 --- /dev/null +++ b/include/hw/hyperv/hvhdk.h @@ -0,0 +1,164 @@ +/* + * Type definitions for the mshv host. + * + * Copyright Microsoft, Corp. 2025 + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef HW_HYPERV_HVHDK_H +#define HW_HYPERV_HVHDK_H + +#define HV_PARTITION_SYNTHETIC_PROCESSOR_FEATURES_BANKS 1 + +struct hv_input_set_partition_property { + uint64_t partition_id; + uint32_t property_code; /* enum hv_partition_property_code */ + uint32_t padding; + uint64_t property_value; +}; + +union hv_partition_synthetic_processor_features { + uint64_t as_uint64[HV_PARTITION_SYNTHETIC_PROCESSOR_FEATURES_BANKS]; + + struct { + /* + * Report a hypervisor is present. CPUID leaves + * 0x40000000 and 0x40000001 are supported. + */ + uint64_t hypervisor_present:1; + + /* + * Features associated with HV#1: + */ + + /* Report support for Hv1 (CPUID leaves 0x40000000 - 0x40000006). = */ + uint64_t hv1:1; + + /* + * Access to HV_X64_MSR_VP_RUNTIME. + * Corresponds to access_vp_run_time_reg privilege. + */ + uint64_t access_vp_run_time_reg:1; + + /* + * Access to HV_X64_MSR_TIME_REF_COUNT. + * Corresponds to access_partition_reference_counter privilege. + */ + uint64_t access_partition_reference_counter:1; + + /* + * Access to SINT-related registers (HV_X64_MSR_SCONTROL through + * HV_X64_MSR_EOM and HV_X64_MSR_SINT0 through HV_X64_MSR_SINT15). + * Corresponds to access_synic_regs privilege. + */ + uint64_t access_synic_regs:1; + + /* + * Access to synthetic timers and associated MSRs + * (HV_X64_MSR_STIMER0_CONFIG through HV_X64_MSR_STIMER3_COUNT). + * Corresponds to access_synthetic_timer_regs privilege. + */ + uint64_t access_synthetic_timer_regs:1; + + /* + * Access to APIC MSRs (HV_X64_MSR_EOI, HV_X64_MSR_ICR and + * HV_X64_MSR_TPR) as well as the VP assist page. + * Corresponds to access_intr_ctrl_regs privilege. + */ + uint64_t access_intr_ctrl_regs:1; + + /* + * Access to registers associated with hypercalls + * (HV_X64_MSR_GUEST_OS_ID and HV_X64_MSR_HYPERCALL). + * Corresponds to access_hypercall_msrs privilege. + */ + uint64_t access_hypercall_regs:1; + + /* VP index can be queried. corresponds to access_vp_index privile= ge. */ + uint64_t access_vp_index:1; + + /* + * Access to the reference TSC. Corresponds to + * access_partition_reference_tsc privilege. + */ + uint64_t access_partition_reference_tsc:1; + + /* + * Partition has access to the guest idle reg. Corresponds to + * access_guest_idle_reg privilege. + */ + uint64_t access_guest_idle_reg:1; + + /* + * Partition has access to frequency regs. corresponds to + * access_frequency_regs privilege. + */ + uint64_t access_frequency_regs:1; + + uint64_t reserved_z12:1; /* Reserved for access_reenlightenment_co= ntrols */ + uint64_t reserved_z13:1; /* Reserved for access_root_scheduler_reg= */ + uint64_t reserved_z14:1; /* Reserved for access_tsc_invariant_cont= rols */ + + /* + * Extended GVA ranges for HvCallFlushVirtualAddressList hypercall. + * Corresponds to privilege. + */ + uint64_t enable_extended_gva_ranges_for_flush_virtual_address_list= :1; + + uint64_t reserved_z16:1; /* Reserved for access_vsm. */ + uint64_t reserved_z17:1; /* Reserved for access_vp_registers. */ + + /* Use fast hypercall output. Corresponds to privilege. */ + uint64_t fast_hypercall_output:1; + + uint64_t reserved_z19:1; /* Reserved for enable_extended_hypercall= s. */ + + /* + * HvStartVirtualProcessor can be used to start virtual processors. + * Corresponds to privilege. + */ + uint64_t start_virtual_processor:1; + + uint64_t reserved_z21:1; /* Reserved for Isolation. */ + + /* Synthetic timers in direct mode. */ + uint64_t direct_synthetic_timers:1; + + uint64_t reserved_z23:1; /* Reserved for synthetic time unhalted t= imer */ + + /* Use extended processor masks. */ + uint64_t extended_processor_masks:1; + + /* + * HvCallFlushVirtualAddressSpace / HvCallFlushVirtualAddressList = are + * supported. + */ + uint64_t tb_flush_hypercalls:1; + + /* HvCallSendSyntheticClusterIpi is supported. */ + uint64_t synthetic_cluster_ipi:1; + + /* HvCallNotifyLongSpinWait is supported. */ + uint64_t notify_long_spin_wait:1; + + /* HvCallQueryNumaDistance is supported. */ + uint64_t query_numa_distance:1; + + /* HvCallSignalEvent is supported. Corresponds to privilege. */ + uint64_t signal_events:1; + + /* HvCallRetargetDeviceInterrupt is supported. */ + uint64_t retarget_device_interrupt:1; + + /* HvCallRestorePartitionTime is supported. */ + uint64_t restore_time:1; + + /* EnlightenedVmcs nested enlightenment is supported. */ + uint64_t enlightened_vmcs:1; + + uint64_t reserved:30; + }; +}; + +#endif diff --git a/include/hw/hyperv/hvhdk_mini.h b/include/hw/hyperv/hvhdk_mini.h new file mode 100644 index 0000000000..cffd16e0de --- /dev/null +++ b/include/hw/hyperv/hvhdk_mini.h @@ -0,0 +1,105 @@ +/* + * Type definitions for the mshv host interface. + * + * Copyright Microsoft, Corp. 2025 + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef _HVHDK_MINI_H +#define _HVHDK_MINI_H + +#define HVHVK_MINI_VERSION (25294) + +/* Each generic set contains 64 elements */ +#define HV_GENERIC_SET_SHIFT (6) +#define HV_GENERIC_SET_MASK (63) + +#define HVCALL_GET_PARTITION_PROPERTY 0x0044 +#define HVCALL_SET_PARTITION_PROPERTY 0x0045 +#define HVCALL_ASSERT_VIRTUAL_INTERRUPT 0x0094 + +enum hv_generic_set_format { + HV_GENERIC_SET_SPARSE_4K, + HV_GENERIC_SET_ALL, +}; + +enum hv_partition_property_code { + /* Privilege properties */ + HV_PARTITION_PROPERTY_PRIVILEGE_FLAGS =3D 0x00010000, + HV_PARTITION_PROPERTY_SYNTHETIC_PROC_FEATURES =3D 0x00010001, + + /* Scheduling properties */ + HV_PARTITION_PROPERTY_SUSPEND =3D 0x00020000, + HV_PARTITION_PROPERTY_CPU_RESERVE =3D 0x00020001, + HV_PARTITION_PROPERTY_CPU_CAP =3D 0x00020002, + HV_PARTITION_PROPERTY_CPU_WEIGHT =3D 0x00020003, + HV_PARTITION_PROPERTY_CPU_GROUP_ID =3D 0x00020004, + + /* Time properties */ + HV_PARTITION_PROPERTY_TIME_FREEZE =3D 0x00030003, + HV_PARTITION_PROPERTY_REFERENCE_TIME =3D 0x00030005, + + /* Debugging properties */ + HV_PARTITION_PROPERTY_DEBUG_CHANNEL_ID =3D 0x00040000, + + /* Resource properties */ + HV_PARTITION_PROPERTY_VIRTUAL_TLB_PAGE_COUNT =3D 0x000= 50000, + HV_PARTITION_PROPERTY_VSM_CONFIG =3D 0x000= 50001, + HV_PARTITION_PROPERTY_ZERO_MEMORY_ON_RESET =3D 0x000= 50002, + HV_PARTITION_PROPERTY_PROCESSORS_PER_SOCKET =3D 0x000= 50003, + HV_PARTITION_PROPERTY_NESTED_TLB_SIZE =3D 0x000= 50004, + HV_PARTITION_PROPERTY_GPA_PAGE_ACCESS_TRACKING =3D 0x000= 50005, + HV_PARTITION_PROPERTY_VSM_PERMISSIONS_DIRTY_SINCE_LAST_QUERY =3D 0x000= 50006, + HV_PARTITION_PROPERTY_SGX_LAUNCH_CONTROL_CONFIG =3D 0x000= 50007, + HV_PARTITION_PROPERTY_DEFAULT_SGX_LAUNCH_CONTROL0 =3D 0x000= 50008, + HV_PARTITION_PROPERTY_DEFAULT_SGX_LAUNCH_CONTROL1 =3D 0x000= 50009, + HV_PARTITION_PROPERTY_DEFAULT_SGX_LAUNCH_CONTROL2 =3D 0x000= 5000a, + HV_PARTITION_PROPERTY_DEFAULT_SGX_LAUNCH_CONTROL3 =3D 0x000= 5000b, + HV_PARTITION_PROPERTY_ISOLATION_STATE =3D 0x000= 5000c, + HV_PARTITION_PROPERTY_ISOLATION_CONTROL =3D 0x000= 5000d, + HV_PARTITION_PROPERTY_ALLOCATION_ID =3D 0x000= 5000e, + HV_PARTITION_PROPERTY_MONITORING_ID =3D 0x000= 5000f, + HV_PARTITION_PROPERTY_IMPLEMENTED_PHYSICAL_ADDRESS_BITS =3D 0x000= 50010, + HV_PARTITION_PROPERTY_NON_ARCHITECTURAL_CORE_SHARING =3D 0x000= 50011, + HV_PARTITION_PROPERTY_HYPERCALL_DOORBELL_PAGE =3D 0x000= 50012, + HV_PARTITION_PROPERTY_ISOLATION_POLICY =3D 0x000= 50014, + HV_PARTITION_PROPERTY_UNIMPLEMENTED_MSR_ACTION =3D 0x000= 50017, + HV_PARTITION_PROPERTY_SEV_VMGEXIT_OFFLOADS =3D 0x000= 50022, + + /* Compatibility properties */ + HV_PARTITION_PROPERTY_PROCESSOR_VENDOR =3D 0x00060000, + HV_PARTITION_PROPERTY_PROCESSOR_FEATURES_DEPRECATED =3D 0x00060001, + HV_PARTITION_PROPERTY_PROCESSOR_XSAVE_FEATURES =3D 0x00060002, + HV_PARTITION_PROPERTY_PROCESSOR_CL_FLUSH_SIZE =3D 0x00060003, + HV_PARTITION_PROPERTY_ENLIGHTENMENT_MODIFICATIONS =3D 0x00060004, + HV_PARTITION_PROPERTY_COMPATIBILITY_VERSION =3D 0x00060005, + HV_PARTITION_PROPERTY_PHYSICAL_ADDRESS_WIDTH =3D 0x00060006, + HV_PARTITION_PROPERTY_XSAVE_STATES =3D 0x00060007, + HV_PARTITION_PROPERTY_MAX_XSAVE_DATA_SIZE =3D 0x00060008, + HV_PARTITION_PROPERTY_PROCESSOR_CLOCK_FREQUENCY =3D 0x00060009, + HV_PARTITION_PROPERTY_PROCESSOR_FEATURES0 =3D 0x0006000a, + HV_PARTITION_PROPERTY_PROCESSOR_FEATURES1 =3D 0x0006000b, + + /* Guest software properties */ + HV_PARTITION_PROPERTY_GUEST_OS_ID =3D 0x00070000, + + /* Nested virtualization properties */ + HV_PARTITION_PROPERTY_PROCESSOR_VIRTUALIZATION_FEATURES =3D 0x00080000, +}; + +/* HV Map GPA (Guest Physical Address) Flags */ +#define HV_MAP_GPA_PERMISSIONS_NONE 0x0 +#define HV_MAP_GPA_READABLE 0x1 +#define HV_MAP_GPA_WRITABLE 0x2 +#define HV_MAP_GPA_KERNEL_EXECUTABLE 0x4 +#define HV_MAP_GPA_USER_EXECUTABLE 0x8 +#define HV_MAP_GPA_EXECUTABLE 0xC +#define HV_MAP_GPA_PERMISSIONS_MASK 0xF +#define HV_MAP_GPA_ADJUSTABLE 0x8000 +#define HV_MAP_GPA_NO_ACCESS 0x10000 +#define HV_MAP_GPA_NOT_CACHED 0x200000 +#define HV_MAP_GPA_LARGE_PAGE 0x80000000 + +#define HV_PFN_RNG_PAGEBITS 24 /* HV_SPA_PAGE_RANGE_ADDITIONAL_PAGES_BITS= */ + +#endif /* _HVHDK_MINI_H */ diff --git a/linux-headers/linux/mshv.h b/linux-headers/linux/mshv.h new file mode 100644 index 0000000000..9b1e1f7ce1 --- /dev/null +++ b/linux-headers/linux/mshv.h @@ -0,0 +1,1038 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Userspace interfaces for /dev/mshv* devices and derived fds + * Includes: + * - VMM APIs for parent (nested/baremetal root) partition APIs + * - VMM APIs for VTL0 APIs + * - Debug and performance metrics APIs + * + * This file is divided into sections containing data structures and IOCTL= s for + * a particular set of related devices or derived file descriptors. + * + * The IOCTL definitions are at the end of each section. They are grouped = by + * device/fd, so that new IOCTLs can easily be added with a monotonically + * increasing number. + */ + +#ifndef HW_HYPERV_LINUX_MSHV_H +#define HW_HYPERV_LINUX_MSHV_H + +#include +#include + +#define MSHV_IOCTL 0xB8 + +typedef enum hv_register_name { + /* Pending Interruption Register */ + HV_REGISTER_PENDING_INTERRUPTION =3D 0x00010002, + + /* X64 User-Mode Registers */ + HV_X64_REGISTER_RAX =3D 0x00020000, + HV_X64_REGISTER_RCX =3D 0x00020001, + HV_X64_REGISTER_RDX =3D 0x00020002, + HV_X64_REGISTER_RBX =3D 0x00020003, + HV_X64_REGISTER_RSP =3D 0x00020004, + HV_X64_REGISTER_RBP =3D 0x00020005, + HV_X64_REGISTER_RSI =3D 0x00020006, + HV_X64_REGISTER_RDI =3D 0x00020007, + HV_X64_REGISTER_R8 =3D 0x00020008, + HV_X64_REGISTER_R9 =3D 0x00020009, + HV_X64_REGISTER_R10 =3D 0x0002000A, + HV_X64_REGISTER_R11 =3D 0x0002000B, + HV_X64_REGISTER_R12 =3D 0x0002000C, + HV_X64_REGISTER_R13 =3D 0x0002000D, + HV_X64_REGISTER_R14 =3D 0x0002000E, + HV_X64_REGISTER_R15 =3D 0x0002000F, + HV_X64_REGISTER_RIP =3D 0x00020010, + HV_X64_REGISTER_RFLAGS =3D 0x00020011, + + /* X64 Floating Point and Vector Registers */ + HV_X64_REGISTER_XMM0 =3D 0x00030000, + HV_X64_REGISTER_XMM1 =3D 0x00030001, + HV_X64_REGISTER_XMM2 =3D 0x00030002, + HV_X64_REGISTER_XMM3 =3D 0x00030003, + HV_X64_REGISTER_XMM4 =3D 0x00030004, + HV_X64_REGISTER_XMM5 =3D 0x00030005, + HV_X64_REGISTER_XMM6 =3D 0x00030006, + HV_X64_REGISTER_XMM7 =3D 0x00030007, + HV_X64_REGISTER_XMM8 =3D 0x00030008, + HV_X64_REGISTER_XMM9 =3D 0x00030009, + HV_X64_REGISTER_XMM10 =3D 0x0003000A, + HV_X64_REGISTER_XMM11 =3D 0x0003000B, + HV_X64_REGISTER_XMM12 =3D 0x0003000C, + HV_X64_REGISTER_XMM13 =3D 0x0003000D, + HV_X64_REGISTER_XMM14 =3D 0x0003000E, + HV_X64_REGISTER_XMM15 =3D 0x0003000F, + HV_X64_REGISTER_FP_MMX0 =3D 0x00030010, + HV_X64_REGISTER_FP_MMX1 =3D 0x00030011, + HV_X64_REGISTER_FP_MMX2 =3D 0x00030012, + HV_X64_REGISTER_FP_MMX3 =3D 0x00030013, + HV_X64_REGISTER_FP_MMX4 =3D 0x00030014, + HV_X64_REGISTER_FP_MMX5 =3D 0x00030015, + HV_X64_REGISTER_FP_MMX6 =3D 0x00030016, + HV_X64_REGISTER_FP_MMX7 =3D 0x00030017, + HV_X64_REGISTER_FP_CONTROL_STATUS =3D 0x00030018, + HV_X64_REGISTER_XMM_CONTROL_STATUS =3D 0x00030019, + + /* X64 Control Registers */ + HV_X64_REGISTER_CR0 =3D 0x00040000, + HV_X64_REGISTER_CR2 =3D 0x00040001, + HV_X64_REGISTER_CR3 =3D 0x00040002, + HV_X64_REGISTER_CR4 =3D 0x00040003, + HV_X64_REGISTER_CR8 =3D 0x00040004, + HV_X64_REGISTER_XFEM =3D 0x00040005, + + /* X64 Segment Registers */ + HV_X64_REGISTER_ES =3D 0x00060000, + HV_X64_REGISTER_CS =3D 0x00060001, + HV_X64_REGISTER_SS =3D 0x00060002, + HV_X64_REGISTER_DS =3D 0x00060003, + HV_X64_REGISTER_FS =3D 0x00060004, + HV_X64_REGISTER_GS =3D 0x00060005, + HV_X64_REGISTER_LDTR =3D 0x00060006, + HV_X64_REGISTER_TR =3D 0x00060007, + + /* X64 Table Registers */ + HV_X64_REGISTER_IDTR =3D 0x00070000, + HV_X64_REGISTER_GDTR =3D 0x00070001, + + /* X64 Virtualized MSRs */ + HV_X64_REGISTER_TSC =3D 0x00080000, + HV_X64_REGISTER_EFER =3D 0x00080001, + HV_X64_REGISTER_KERNEL_GS_BASE =3D 0x00080002, + HV_X64_REGISTER_APIC_BASE =3D 0x00080003, + HV_X64_REGISTER_PAT =3D 0x00080004, + HV_X64_REGISTER_SYSENTER_CS =3D 0x00080005, + HV_X64_REGISTER_SYSENTER_EIP =3D 0x00080006, + HV_X64_REGISTER_SYSENTER_ESP =3D 0x00080007, + HV_X64_REGISTER_STAR =3D 0x00080008, + HV_X64_REGISTER_LSTAR =3D 0x00080009, + HV_X64_REGISTER_CSTAR =3D 0x0008000A, + HV_X64_REGISTER_SFMASK =3D 0x0008000B, + HV_X64_REGISTER_INITIAL_APIC_ID =3D 0x0008000C, + + /* X64 Cache control MSRs */ + HV_X64_REGISTER_MSR_MTRR_CAP =3D 0x0008000D, + HV_X64_REGISTER_MSR_MTRR_DEF_TYPE =3D 0x0008000E, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE0 =3D 0x00080010, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE1 =3D 0x00080011, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE2 =3D 0x00080012, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE3 =3D 0x00080013, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE4 =3D 0x00080014, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE5 =3D 0x00080015, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE6 =3D 0x00080016, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE7 =3D 0x00080017, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE8 =3D 0x00080018, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASE9 =3D 0x00080019, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASEA =3D 0x0008001A, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASEB =3D 0x0008001B, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASEC =3D 0x0008001C, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASED =3D 0x0008001D, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASEE =3D 0x0008001E, + HV_X64_REGISTER_MSR_MTRR_PHYS_BASEF =3D 0x0008001F, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK0 =3D 0x00080040, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK1 =3D 0x00080041, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK2 =3D 0x00080042, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK3 =3D 0x00080043, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK4 =3D 0x00080044, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK5 =3D 0x00080045, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK6 =3D 0x00080046, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK7 =3D 0x00080047, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK8 =3D 0x00080048, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASK9 =3D 0x00080049, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASKA =3D 0x0008004A, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASKB =3D 0x0008004B, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASKC =3D 0x0008004C, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASKD =3D 0x0008004D, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASKE =3D 0x0008004E, + HV_X64_REGISTER_MSR_MTRR_PHYS_MASKF =3D 0x0008004F, + HV_X64_REGISTER_MSR_MTRR_FIX64K00000 =3D 0x00080070, + HV_X64_REGISTER_MSR_MTRR_FIX16K80000 =3D 0x00080071, + HV_X64_REGISTER_MSR_MTRR_FIX16KA0000 =3D 0x00080072, + HV_X64_REGISTER_MSR_MTRR_FIX4KC0000 =3D 0x00080073, + HV_X64_REGISTER_MSR_MTRR_FIX4KC8000 =3D 0x00080074, + HV_X64_REGISTER_MSR_MTRR_FIX4KD0000 =3D 0x00080075, + HV_X64_REGISTER_MSR_MTRR_FIX4KD8000 =3D 0x00080076, + HV_X64_REGISTER_MSR_MTRR_FIX4KE0000 =3D 0x00080077, + HV_X64_REGISTER_MSR_MTRR_FIX4KE8000 =3D 0x00080078, + HV_X64_REGISTER_MSR_MTRR_FIX4KF0000 =3D 0x00080079, + HV_X64_REGISTER_MSR_MTRR_FIX4KF8000 =3D 0x0008007A, + + HV_X64_REGISTER_TSC_AUX =3D 0x0008007B, + HV_X64_REGISTER_BNDCFGS =3D 0x0008007C, + HV_X64_REGISTER_DEBUG_CTL =3D 0x0008007D, + + /* Available */ + + HV_X64_REGISTER_SPEC_CTRL =3D 0x00080084, + HV_X64_REGISTER_TSC_ADJUST =3D 0x00080096, + + /* Other MSRs */ + HV_X64_REGISTER_MSR_IA32_MISC_ENABLE =3D 0x000800A0, + + /* Misc */ + HV_REGISTER_GUEST_OS_ID =3D 0x00090002, + HV_REGISTER_REFERENCE_TSC =3D 0x00090017, + + /* Hypervisor-defined Registers (Synic) */ + HV_REGISTER_SINT0 =3D 0x000A0000, + HV_REGISTER_SINT1 =3D 0x000A0001, + HV_REGISTER_SINT2 =3D 0x000A0002, + HV_REGISTER_SINT3 =3D 0x000A0003, + HV_REGISTER_SINT4 =3D 0x000A0004, + HV_REGISTER_SINT5 =3D 0x000A0005, + HV_REGISTER_SINT6 =3D 0x000A0006, + HV_REGISTER_SINT7 =3D 0x000A0007, + HV_REGISTER_SINT8 =3D 0x000A0008, + HV_REGISTER_SINT9 =3D 0x000A0009, + HV_REGISTER_SINT10 =3D 0x000A000A, + HV_REGISTER_SINT11 =3D 0x000A000B, + HV_REGISTER_SINT12 =3D 0x000A000C, + HV_REGISTER_SINT13 =3D 0x000A000D, + HV_REGISTER_SINT14 =3D 0x000A000E, + HV_REGISTER_SINT15 =3D 0x000A000F, + HV_REGISTER_SCONTROL =3D 0x000A0010, + HV_REGISTER_SVERSION =3D 0x000A0011, + HV_REGISTER_SIEFP =3D 0x000A0012, + HV_REGISTER_SIMP =3D 0x000A0013, + HV_REGISTER_EOM =3D 0x000A0014, + HV_REGISTER_SIRBP =3D 0x000A0015, +} hv_register_name; + +enum hv_intercept_type { + HV_INTERCEPT_TYPE_X64_IO_PORT =3D 0X00000000, + HV_INTERCEPT_TYPE_X64_MSR =3D 0X00000001, + HV_INTERCEPT_TYPE_X64_CPUID =3D 0X00000002, + HV_INTERCEPT_TYPE_EXCEPTION =3D 0X00000003, + + /* Used to be HV_INTERCEPT_TYPE_REGISTER */ + HV_INTERCEPT_TYPE_RESERVED0 =3D 0X00000004, + HV_INTERCEPT_TYPE_MMIO =3D 0X00000005, + HV_INTERCEPT_TYPE_X64_GLOBAL_CPUID =3D 0X00000006, + HV_INTERCEPT_TYPE_X64_APIC_SMI =3D 0X00000007, + HV_INTERCEPT_TYPE_HYPERCALL =3D 0X00000008, + + HV_INTERCEPT_TYPE_X64_APIC_INIT_SIPI =3D 0X00000009, + HV_INTERCEPT_MC_UPDATE_PATCH_LEVEL_MSR_READ =3D 0X0000000A, + + HV_INTERCEPT_TYPE_X64_APIC_WRITE =3D 0X0000000B, + HV_INTERCEPT_TYPE_X64_MSR_INDEX =3D 0X0000000C, + HV_INTERCEPT_TYPE_MAX, + HV_INTERCEPT_TYPE_INVALID =3D 0XFFFFFFFF, +}; + +struct hv_u128 { + __u64 low_part; + __u64 high_part; +}; + +union hv_x64_xmm_control_status_register { + struct hv_u128 as_uint128; + struct { + union { + /* long mode */ + __u64 last_fp_rdp; + /* 32 bit mode */ + struct { + __u32 last_fp_dp; + __u16 last_fp_ds; + __u16 padding; + }; + }; + __u32 xmm_status_control; + __u32 xmm_status_control_mask; + }; +}; + +union hv_x64_fp_register { + struct hv_u128 as_uint128; + struct { + __u64 mantissa; + __u64 biased_exponent : 15; + __u64 sign : 1; + __u64 reserved : 48; + }; +}; + +union hv_x64_pending_exception_event { + __u64 as_uint64[2]; + struct { + __u32 event_pending : 1; + __u32 event_type : 3; + __u32 reserved0 : 4; + __u32 deliver_error_code : 1; + __u32 reserved1 : 7; + __u32 vector : 16; + __u32 error_code; + __u64 exception_parameter; + }; +}; + +union hv_x64_pending_virtualization_fault_event { + __u64 as_uint64[2]; + struct { + __u32 event_pending : 1; + __u32 event_type : 3; + __u32 reserved0 : 4; + __u32 reserved1 : 8; + __u32 parameter0 : 16; + __u32 code; + __u64 parameter1; + }; +}; + +union hv_x64_pending_interruption_register { + __u64 as_uint64; + struct { + __u32 interruption_pending : 1; + __u32 interruption_type : 3; + __u32 deliver_error_code : 1; + __u32 instruction_length : 4; + __u32 nested_event : 1; + __u32 reserved : 6; + __u32 interruption_vector : 16; + __u32 error_code; + }; +}; + +union hv_x64_register_sev_control { + __u64 as_uint64; + struct { + __u64 enable_encrypted_state : 1; + __u64 reserved_z : 11; + __u64 vmsa_gpa_page_number : 52; + }; +}; + +union hv_x64_msr_npiep_config_contents { + __u64 as_uint64; + struct { + /* + * These bits enable instruction execution prevention for + * specific instructions. + */ + __u64 prevents_gdt : 1; + __u64 prevents_idt : 1; + __u64 prevents_ldt : 1; + __u64 prevents_tr : 1; + + /* The reserved bits must always be 0. */ + __u64 reserved : 60; + }; +}; + +typedef struct hv_x64_segment_register { + __u64 base; + __u32 limit; + __u16 selector; + union { + struct { + __u16 segment_type : 4; + __u16 non_system_segment : 1; + __u16 descriptor_privilege_level : 2; + __u16 present : 1; + __u16 reserved : 4; + __u16 available : 1; + __u16 _long : 1; + __u16 _default : 1; + __u16 granularity : 1; + }; + __u16 attributes; + }; +} hv_x64_segment_register; + +typedef struct hv_x64_table_register { + __u16 pad[3]; + __u16 limit; + __u64 base; +} hv_x64_table_register; + +union hv_x64_fp_control_status_register { + struct hv_u128 as_uint128; + struct { + __u16 fp_control; + __u16 fp_status; + __u8 fp_tag; + __u8 reserved; + __u16 last_fp_op; + union { + /* long mode */ + __u64 last_fp_rip; + /* 32 bit mode */ + struct { + __u32 last_fp_eip; + __u16 last_fp_cs; + __u16 padding; + }; + }; + }; +}; + +/* General Hypervisor Register Content Definitions */ + +union hv_explicit_suspend_register { + __u64 as_uint64; + struct { + __u64 suspended : 1; + __u64 reserved : 63; + }; +}; + +union hv_internal_activity_register { + __u64 as_uint64; + + struct { + __u64 startup_suspend : 1; + __u64 halt_suspend : 1; + __u64 idle_suspend : 1; + __u64 rsvd_z : 61; + }; +}; + +union hv_x64_interrupt_state_register { + __u64 as_uint64; + struct { + __u64 interrupt_shadow : 1; + __u64 nmi_masked : 1; + __u64 reserved : 62; + }; +}; + +union hv_intercept_suspend_register { + __u64 as_uint64; + struct { + __u64 suspended : 1; + __u64 reserved : 63; + }; +}; + +union hv_register_value { + struct hv_u128 reg128; + __u64 reg64; + __u32 reg32; + __u16 reg16; + __u8 reg8; + union hv_x64_fp_register fp; + union hv_x64_fp_control_status_register fp_control_status; + union hv_x64_xmm_control_status_register xmm_control_status; + struct hv_x64_segment_register segment; + struct hv_x64_table_register table; + union hv_explicit_suspend_register explicit_suspend; + union hv_intercept_suspend_register intercept_suspend; + union hv_internal_activity_register internal_activity; + union hv_x64_interrupt_state_register interrupt_state; + union hv_x64_pending_interruption_register pending_interruption; + union hv_x64_msr_npiep_config_contents npiep_config; + union hv_x64_pending_exception_event pending_exception_event; + union hv_x64_pending_virtualization_fault_event + pending_virtualization_fault_event; + union hv_x64_register_sev_control sev_control; +}; + +typedef struct hv_register_assoc { + __u32 name; /* enum hv_register_name */ + __u32 reserved1; + __u64 reserved2; + union hv_register_value value; +} hv_register_assoc; + +#define MSHV_VP_MAX_REGISTERS 128 + +struct mshv_vp_registers { + int count; /* at most MSHV_VP_MAX_REGISTERS */ + struct hv_register_assoc *regs; +}; + +/** + * struct mshv_user_mem_region - arguments for MSHV_SET_GUEST_MEMORY + * @size: Size of the memory region (bytes). Must be aligned to PAGE_SIZE + * @guest_pfn: Base guest page number to map + * @userspace_addr: Base address of userspace memory. Must be aligned to + * PAGE_SIZE + * @flags: Bitmask of 1 << MSHV_SET_MEM_BIT_*. If (1 << MSHV_SET_MEM_BIT_U= NMAP) + * is set, ignore other bits. + * @rsvd: MBZ + * + * Map or unmap a region of userspace memory to Guest Physical Addresses (= GPA). + * Mappings can't overlap in GPA space or userspace. + * To unmap, these fields must match an existing mapping. + */ +typedef struct mshv_user_mem_region { + __u64 size; + __u64 guest_pfn; + __u64 userspace_addr; + __u8 flags; + __u8 rsvd[7]; +} mshv_user_mem_region; + +enum { + MSHV_SET_MEM_BIT_WRITABLE, + MSHV_SET_MEM_BIT_EXECUTABLE, + MSHV_SET_MEM_BIT_UNMAP, + MSHV_SET_MEM_BIT_COUNT +}; +#define MSHV_SET_MEM_FLAGS_MASK ((1 << MSHV_SET_MEM_BIT_COUNT) - 1) + +enum { + MSHV_PT_BIT_LAPIC, + MSHV_PT_BIT_X2APIC, + MSHV_PT_BIT_GPA_SUPER_PAGES, + MSHV_PT_BIT_COUNT, +}; +#define MSHV_PT_FLAGS_MASK ((1 << MSHV_PT_BIT_COUNT) - 1) + +enum { + MSHV_PT_ISOLATION_NONE, + MSHV_PT_ISOLATION_SNP, + MSHV_PT_ISOLATION_COUNT, +}; + +enum { + MSHV_IOEVENTFD_BIT_DATAMATCH, + MSHV_IOEVENTFD_BIT_PIO, + MSHV_IOEVENTFD_BIT_DEASSIGN, + MSHV_IOEVENTFD_BIT_COUNT, +}; +#define MSHV_IOEVENTFD_FLAGS_MASK ((1 << MSHV_IOEVENTFD_BIT_COUNT) - 1) + +union hv_interrupt_control { + __u64 as_uint64; + struct { + __u32 interrupt_type; /* enum hv_interrupt type */ + __u32 level_triggered : 1; + __u32 logical_dest_mode : 1; + __u32 rsvd : 30; + }; +}; + +struct hv_input_assert_virtual_interrupt { + __u64 partition_id; + union hv_interrupt_control control; + __u64 dest_addr; /* cpu's apic id */ + __u32 vector; + __u8 target_vtl; + __u8 rsvd_z0; + __u16 rsvd_z1; +}; + +struct hv_register_x64_cpuid_result_parameters { + struct { + __u32 eax; + __u32 ecx; + __u8 subleaf_specific; + __u8 always_override; + __u16 padding; + } input; + struct { + __u32 eax; + __u32 eax_mask; + __u32 ebx; + __u32 ebx_mask; + __u32 ecx; + __u32 ecx_mask; + __u32 edx; + __u32 edx_mask; + } result; +}; + +struct hv_register_x64_msr_result_parameters { + __u32 msr_index; + __u32 access_type; + __u32 action; /* enum hv_unimplemented_msr_action */ +}; + +union hv_register_intercept_result_parameters { + struct hv_register_x64_cpuid_result_parameters cpuid; + struct hv_register_x64_msr_result_parameters msr; +}; + +struct mshv_register_intercept_result { + __u32 intercept_type; /* enum hv_intercept_type */ + union hv_register_intercept_result_parameters parameters; +}; + +typedef struct mshv_user_ioeventfd { + __u64 datamatch; + __u64 addr; /* legal pio/mmio address */ + __u32 len; /* 1, 2, 4, or 8 bytes */ + __s32 fd; + __u32 flags; + __u8 rsvd[4]; +} mshv_user_ioeventfd; + +typedef struct mshv_user_irq_entry { + __u32 gsi; + __u32 address_lo; + __u32 address_hi; + __u32 data; +} mshv_user_irq_entry; + +struct mshv_user_irq_table { + __u32 nr; + __u32 rsvd; /* MBZ */ + struct mshv_user_irq_entry entries[0]; +}; + +enum { + MSHV_IRQFD_BIT_DEASSIGN, + MSHV_IRQFD_BIT_RESAMPLE, + MSHV_IRQFD_BIT_COUNT, +}; +#define MSHV_IRQFD_FLAGS_MASK ((1 << MSHV_IRQFD_BIT_COUNT) - 1) + +struct mshv_user_irqfd { + __s32 fd; + __s32 resamplefd; + __u32 gsi; + __u32 flags; +}; + +/** + * struct mshv_create_partition - arguments for MSHV_CREATE_PARTITION + * @pt_flags: Bitmask of 1 << MSHV_PT_BIT_* + * @pt_isolation: MSHV_PT_ISOLATION_* + * + * Returns a file descriptor to act as a handle to a guest partition. + * At this point the partition is not yet initialized in the hypervisor. + * Some operations must be done with the partition in this state, e.g. set= ting + * so-called "early" partition properties. The partition can then be + * initialized with MSHV_INITIALIZE_PARTITION. + */ +struct mshv_create_partition { + __u64 pt_flags; + __u64 pt_isolation; +}; + +struct mshv_create_vp { + __u32 vp_index; +}; + +enum hv_translate_gva_result_code { + HV_TRANSLATE_GVA_SUCCESS =3D 0, + + /* Translation failures. */ + HV_TRANSLATE_GVA_PAGE_NOT_PRESENT =3D 1, + HV_TRANSLATE_GVA_PRIVILEGE_VIOLATION =3D 2, + HV_TRANSLATE_GVA_INVALIDE_PAGE_TABLE_FLAGS =3D 3, + + /* GPA access failures. */ + HV_TRANSLATE_GVA_GPA_UNMAPPED =3D 4, + HV_TRANSLATE_GVA_GPA_NO_READ_ACCESS =3D 5, + HV_TRANSLATE_GVA_GPA_NO_WRITE_ACCESS =3D 6, + HV_TRANSLATE_GVA_GPA_ILLEGAL_OVERLAY_ACCESS =3D 7, + + /* + * Intercept for memory access by either + * - a higher VTL + * - a nested hypervisor (due to a violation of the nested page table) + */ + HV_TRANSLATE_GVA_INTERCEPT =3D 8, + + HV_TRANSLATE_GVA_GPA_UNACCEPTED =3D 9, +}; + +union hv_translate_gva_result { + __u64 as_uint64; + struct { + __u32 result_code; /* enum hv_translate_hva_result_code */ + __u32 cache_type : 8; + __u32 overlay_page : 1; + __u32 reserved : 23; + }; +}; + +typedef struct mshv_translate_gva { + __u64 gva; + __u64 flags; + union hv_translate_gva_result *result; + __u64 *gpa; +} mshv_translate_gva; + +/* /dev/mshv */ +#define MSHV_CREATE_PARTITION _IOW(MSHV_IOCTL, 0x00, struct mshv_create_pa= rtition) +#define MSHV_CREATE_VP _IOW(MSHV_IOCTL, 0x01, struct mshv_create_vp) + +/* Partition fds created with MSHV_CREATE_PARTITION */ +#define MSHV_INITIALIZE_PARTITION _IO(MSHV_IOCTL, 0x00) +#define MSHV_SET_GUEST_MEMORY _IOW(MSHV_IOCTL, 0x02, struct mshv_user_mem= _region) +#define MSHV_IRQFD _IOW(MSHV_IOCTL, 0x03, struct mshv_user_irqfd) +#define MSHV_IOEVENTFD _IOW(MSHV_IOCTL, 0x04, struct mshv_user_ioeve= ntfd) +#define MSHV_SET_MSI_ROUTING _IOW(MSHV_IOCTL, 0x05, struct mshv_user_irq_= table) + +/* TODO: replace with ROOT_HVCALL */ +#define MSHV_GET_VP_REGISTERS _IOWR(MSHV_IOCTL, 0xF0, struct mshv_vp_regi= sters) +#define MSHV_SET_VP_REGISTERS _IOW(MSHV_IOCTL, 0xF1, struct mshv_vp_regis= ters) +#define MSHV_TRANSLATE_GVA _IOWR(MSHV_IOCTL, 0xF2, struct mshv_translate= _gva) + +#define MSHV_VP_REGISTER_INTERCEPT_RESULT _IOW(MSHV_IOCTL, 0xF3, struct ms= hv_register_intercept_result) + +/* + ******************************** + * VP APIs for child partitions * + ******************************** + */ + +enum { + MSHV_VP_STATE_LAPIC =3D 0, + MSHV_VP_STATE_XSAVE, /* XSAVE data in compacted form */ + MSHV_VP_STATE_SIMP, + MSHV_VP_STATE_SIEFP, + MSHV_VP_STATE_SYNTHETIC_TIMERS, + MSHV_VP_STATE_COUNT, +}; + +typedef struct mshv_get_set_vp_state { + __u8 type; /* MSHV_VP_STATE_* */ + __u8 rsvd[3]; /* MBZ */ + __u32 buf_sz; /* in - 4k page-aligned size of buffer. + * out - actual size of data. + * On EINVAL, check this to see if buffer was too small + */ + __u64 buf_ptr; /* 4k page-aligned data buffer. */ +} mshv_get_set_vp_state; + +struct hv_local_interrupt_controller_state { + /* HV_X64_INTERRUPT_CONTROLLER_STATE */ + __u32 apic_id; + __u32 apic_version; + __u32 apic_ldr; + __u32 apic_dfr; + __u32 apic_spurious; + __u32 apic_isr[8]; + __u32 apic_tmr[8]; + __u32 apic_irr[8]; + __u32 apic_esr; + __u32 apic_icr_high; + __u32 apic_icr_low; + __u32 apic_lvt_timer; + __u32 apic_lvt_thermal; + __u32 apic_lvt_perfmon; + __u32 apic_lvt_lint0; + __u32 apic_lvt_lint1; + __u32 apic_lvt_error; + __u32 apic_lvt_cmci; + __u32 apic_error_status; + __u32 apic_initial_count; + __u32 apic_counter_value; + __u32 apic_divide_configuration; + __u32 apic_remote_read; +}; + +#define MSHV_RUN_VP_BUF_SZ 256 + +struct mshv_run_vp { + __u8 msg_buf[MSHV_RUN_VP_BUF_SZ]; +}; + +#define MSHV_RUN_VP _IOR(MSHV_IOCTL, 0x00, struct mshv_run_vp) +#define MSHV_GET_VP_STATE _IOWR(MSHV_IOCTL, 0x01, struct mshv_get_set_vp_= state) +#define MSHV_SET_VP_STATE _IOWR(MSHV_IOCTL, 0x02, struct mshv_get_set_vp_= state) + +/** + * struct mshv_root_hvcall - arguments for MSHV_ROOT_HVCALL + * @code: Hypercall code (HVCALL_*) + * @reps: in: Rep count ('repcount') + * out: Reps completed ('repcomp'). MBZ unless rep hvcall + * @in_sz: Size of input incl rep data. <=3D HV_HYP_PAGE_SIZE + * @out_sz: Size of output buffer. <=3D HV_HYP_PAGE_SIZE. MBZ if out_ptr i= s 0 + * @status: in: MBZ + * out: HV_STATUS_* from hypercall + * @rsvd: MBZ + * @in_ptr: Input data buffer (struct hv_input_*). If used with partition = or + * vp fd, partition id field is added by kernel. + * @out_ptr: Output data buffer (optional) + */ +struct mshv_root_hvcall { + __u16 code; + __u16 reps; + __u16 in_sz; + __u16 out_sz; + __u16 status; + __u8 rsvd[6]; + __u64 in_ptr; + __u64 out_ptr; +}; + +/* Generic hypercall */ +#define MSHV_ROOT_HVCALL _IOWR(MSHV_IOCTL, 0x07, struct mshv_root_hvcall) + +/* From hvgdk_mini.h */ + +#define HV_X64_MSR_GUEST_OS_ID 0x40000000 +#define HV_X64_MSR_SINT0 0x40000090 +#define HV_X64_MSR_SINT1 0x40000091 +#define HV_X64_MSR_SINT2 0x40000092 +#define HV_X64_MSR_SINT3 0x40000093 +#define HV_X64_MSR_SINT4 0x40000094 +#define HV_X64_MSR_SINT5 0x40000095 +#define HV_X64_MSR_SINT6 0x40000096 +#define HV_X64_MSR_SINT7 0x40000097 +#define HV_X64_MSR_SINT8 0x40000098 +#define HV_X64_MSR_SINT9 0x40000099 +#define HV_X64_MSR_SINT10 0x4000009A +#define HV_X64_MSR_SINT11 0x4000009B +#define HV_X64_MSR_SINT12 0x4000009C +#define HV_X64_MSR_SINT13 0x4000009D +#define HV_X64_MSR_SINT14 0x4000009E +#define HV_X64_MSR_SINT15 0x4000009F +#define HV_X64_MSR_SCONTROL 0x40000080 +#define HV_X64_MSR_SIEFP 0x40000082 +#define HV_X64_MSR_SIMP 0x40000083 +#define HV_X64_MSR_REFERENCE_TSC 0x40000021 +#define HV_X64_MSR_EOM 0x40000084 + +/* Define port identifier type. */ +union hv_port_id { + __u32 as__u32; + struct { + __u32 id : 24; + __u32 reserved : 8; + }; +}; + +#define HV_MESSAGE_SIZE (256) +#define HV_MESSAGE_PAYLOAD_BYTE_COUNT (240) +#define HV_MESSAGE_PAYLOAD_QWORD_COUNT (30) + +/* Define hypervisor message types. */ +enum hv_message_type { + HVMSG_NONE =3D 0x00000000, + + /* Memory access messages. */ + HVMSG_UNMAPPED_GPA =3D 0x80000000, + HVMSG_GPA_INTERCEPT =3D 0x80000001, + HVMSG_UNACCEPTED_GPA =3D 0x80000003, + HVMSG_GPA_ATTRIBUTE_INTERCEPT =3D 0x80000004, + + /* Timer notification messages. */ + HVMSG_TIMER_EXPIRED =3D 0x80000010, + + /* Error messages. */ + HVMSG_INVALID_VP_REGISTER_VALUE =3D 0x80000020, + HVMSG_UNRECOVERABLE_EXCEPTION =3D 0x80000021, + HVMSG_UNSUPPORTED_FEATURE =3D 0x80000022, + + /* + * Opaque intercept message. The original intercept message is only + * accessible from the mapped intercept message page. + */ + HVMSG_OPAQUE_INTERCEPT =3D 0x8000003F, + + /* Trace buffer complete messages. */ + HVMSG_EVENTLOG_BUFFERCOMPLETE =3D 0x80000040, + + /* Hypercall intercept */ + HVMSG_HYPERCALL_INTERCEPT =3D 0x80000050, + + /* SynIC intercepts */ + HVMSG_SYNIC_EVENT_INTERCEPT =3D 0x80000060, + HVMSG_SYNIC_SINT_INTERCEPT =3D 0x80000061, + HVMSG_SYNIC_SINT_DELIVERABLE =3D 0x80000062, + + /* Async call completion intercept */ + HVMSG_ASYNC_CALL_COMPLETION =3D 0x80000070, + + /* Root scheduler messages */ + HVMSG_SCHEDULER_VP_SIGNAL_BITSE =3D 0x80000100, + HVMSG_SCHEDULER_VP_SIGNAL_PAIR =3D 0x80000101, + + /* Platform-specific processor intercept messages. */ + HVMSG_X64_IO_PORT_INTERCEPT =3D 0x80010000, + HVMSG_X64_MSR_INTERCEPT =3D 0x80010001, + HVMSG_X64_CPUID_INTERCEPT =3D 0x80010002, + HVMSG_X64_EXCEPTION_INTERCEPT =3D 0x80010003, + HVMSG_X64_APIC_EOI =3D 0x80010004, + HVMSG_X64_LEGACY_FP_ERROR =3D 0x80010005, + HVMSG_X64_IOMMU_PRQ =3D 0x80010006, + HVMSG_X64_HALT =3D 0x80010007, + HVMSG_X64_INTERRUPTION_DELIVERABLE =3D 0x80010008, + HVMSG_X64_SIPI_INTERCEPT =3D 0x80010009, + HVMSG_X64_SEV_VMGEXIT_INTERCEPT =3D 0x80010013, +}; + +union hv_x64_vp_execution_state { + __u16 as_uint16; + struct { + __u16 cpl:2; + __u16 cr0_pe:1; + __u16 cr0_am:1; + __u16 efer_lma:1; + __u16 debug_active:1; + __u16 interruption_pending:1; + __u16 vtl:4; + __u16 enclave_mode:1; + __u16 interrupt_shadow:1; + __u16 virtualization_fault_active:1; + __u16 reserved:2; + }; +}; + +/* From openvmm::hvdef */ +enum hv_x64_intercept_access_type { + HV_X64_INTERCEPT_ACCESS_TYPE_READ =3D 0, + HV_X64_INTERCEPT_ACCESS_TYPE_WRITE =3D 1, + HV_X64_INTERCEPT_ACCESS_TYPE_EXECUTE =3D 2, +}; + +struct hv_x64_intercept_message_header { + __u32 vp_index; + __u8 instruction_length:4; + __u8 cr8:4; /* Only set for exo partitions */ + __u8 intercept_access_type; + union hv_x64_vp_execution_state execution_state; + struct hv_x64_segment_register cs_segment; + __u64 rip; + __u64 rflags; +}; + +union hv_x64_io_port_access_info { + __u8 as_uint8; + struct { + __u8 access_size:3; + __u8 string_op:1; + __u8 rep_prefix:1; + __u8 reserved:3; + }; +}; + +typedef struct hv_x64_io_port_intercept_message { + struct hv_x64_intercept_message_header header; + __u16 port_number; + union hv_x64_io_port_access_info access_info; + __u8 instruction_byte_count; + __u32 reserved; + __u64 rax; + __u8 instruction_bytes[16]; + struct hv_x64_segment_register ds_segment; + struct hv_x64_segment_register es_segment; + __u64 rcx; + __u64 rsi; + __u64 rdi; +} hv_x64_io_port_intercept_message; + +union hv_x64_memory_access_info { + __u8 as_uint8; + struct { + __u8 gva_valid:1; + __u8 gva_gpa_valid:1; + __u8 hypercall_output_pending:1; + __u8 tlb_locked_no_overlay:1; + __u8 reserved:4; + }; +}; + +struct hv_x64_memory_intercept_message { + struct hv_x64_intercept_message_header header; + __u32 cache_type; /* enum hv_cache_type */ + __u8 instruction_byte_count; + union hv_x64_memory_access_info memory_access_info; + __u8 tpr_priority; + __u8 reserved1; + __u64 guest_virtual_address; + __u64 guest_physical_address; + __u8 instruction_bytes[16]; +}; + +union hv_message_flags { + __u8 asu8; + struct { + __u8 msg_pending : 1; + __u8 reserved : 7; + }; +}; + +struct hv_message_header { + __u32 message_type; + __u8 payload_size; + union hv_message_flags message_flags; + __u8 reserved[2]; + union { + __u64 sender; + union hv_port_id port; + }; +}; + +struct hv_message { + struct hv_message_header header; + union { + __u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT]; + } u; +}; + +/* From github.com/rust-vmm/mshv-bindings/src/x86_64/regs.rs */ + +struct hv_cpuid_entry { + uint32_t function; + uint32_t index; + uint32_t flags; + uint32_t eax; + uint32_t ebx; + uint32_t ecx; + uint32_t edx; + uint32_t padding[3]; +}; + +struct hv_cpuid { + uint32_t nent; + uint32_t padding; + struct hv_cpuid_entry entries[0]; +}; + +#define IA32_MSR_TSC 0x00000010 +#define IA32_MSR_EFER 0xC0000080 +#define IA32_MSR_KERNEL_GS_BASE 0xC0000102 +#define IA32_MSR_APIC_BASE 0x0000001B +#define IA32_MSR_PAT 0x0277 +#define IA32_MSR_SYSENTER_CS 0x00000174 +#define IA32_MSR_SYSENTER_ESP 0x00000175 +#define IA32_MSR_SYSENTER_EIP 0x00000176 +#define IA32_MSR_STAR 0xC0000081 +#define IA32_MSR_LSTAR 0xC0000082 +#define IA32_MSR_CSTAR 0xC0000083 +#define IA32_MSR_SFMASK 0xC0000084 + +#define IA32_MSR_MTRR_CAP 0x00FE +#define IA32_MSR_MTRR_DEF_TYPE 0x02FF +#define IA32_MSR_MTRR_PHYSBASE0 0x0200 +#define IA32_MSR_MTRR_PHYSMASK0 0x0201 +#define IA32_MSR_MTRR_PHYSBASE1 0x0202 +#define IA32_MSR_MTRR_PHYSMASK1 0x0203 +#define IA32_MSR_MTRR_PHYSBASE2 0x0204 +#define IA32_MSR_MTRR_PHYSMASK2 0x0205 +#define IA32_MSR_MTRR_PHYSBASE3 0x0206 +#define IA32_MSR_MTRR_PHYSMASK3 0x0207 +#define IA32_MSR_MTRR_PHYSBASE4 0x0208 +#define IA32_MSR_MTRR_PHYSMASK4 0x0209 +#define IA32_MSR_MTRR_PHYSBASE5 0x020A +#define IA32_MSR_MTRR_PHYSMASK5 0x020B +#define IA32_MSR_MTRR_PHYSBASE6 0x020C +#define IA32_MSR_MTRR_PHYSMASK6 0x020D +#define IA32_MSR_MTRR_PHYSBASE7 0x020E +#define IA32_MSR_MTRR_PHYSMASK7 0x020F + +#define IA32_MSR_MTRR_FIX64K_00000 0x0250 +#define IA32_MSR_MTRR_FIX16K_80000 0x0258 +#define IA32_MSR_MTRR_FIX16K_A0000 0x0259 +#define IA32_MSR_MTRR_FIX4K_C0000 0x0268 +#define IA32_MSR_MTRR_FIX4K_C8000 0x0269 +#define IA32_MSR_MTRR_FIX4K_D0000 0x026A +#define IA32_MSR_MTRR_FIX4K_D8000 0x026B +#define IA32_MSR_MTRR_FIX4K_E0000 0x026C +#define IA32_MSR_MTRR_FIX4K_E8000 0x026D +#define IA32_MSR_MTRR_FIX4K_F0000 0x026E +#define IA32_MSR_MTRR_FIX4K_F8000 0x026F + +#define IA32_MSR_TSC_AUX 0xC0000103 +#define IA32_MSR_BNDCFGS 0x00000d90 +#define IA32_MSR_DEBUG_CTL 0x1D9 +#define IA32_MSR_SPEC_CTRL 0x00000048 +#define IA32_MSR_TSC_ADJUST 0x0000003b + +#define IA32_MSR_MISC_ENABLE 0x000001a0 + + +#define HV_TRANSLATE_GVA_VALIDATE_READ (0x0001) +#define HV_TRANSLATE_GVA_VALIDATE_WRITE (0x0002) +#define HV_TRANSLATE_GVA_VALIDATE_EXECUTE (0x0004) + +#endif diff --git a/scripts/update-linux-headers.sh b/scripts/update-linux-headers= .sh index b43b8ef75a..396df4a99a 100755 --- a/scripts/update-linux-headers.sh +++ b/scripts/update-linux-headers.sh @@ -200,7 +200,7 @@ rm -rf "$output/linux-headers/linux" mkdir -p "$output/linux-headers/linux" for header in const.h stddef.h kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h= \ psci.h psp-sev.h userfaultfd.h memfd.h mman.h nvme_ioctl.h \ - vduse.h iommufd.h bits.h; do + vduse.h iommufd.h bits.h mshv.h; do cp "$hdrdir/include/linux/$header" "$output/linux-headers/linux" done =20 --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751390992; cv=none; d=zohomail.com; s=zohoarc; b=GvQy2zK20jcJTjRdgcfe0oXF0+e3XKjEv3QO8wLsaBlLbe/qWkS3aZAaPf5LOpihnt74MKJkn8Wrdyf7ex3p43YvwCqD3q0BfWo/zslu5dk0KQx8i3AE6DgpNAL45zGoaiZt4VHYcdHCPz49odJpvIVzxmuStfPXN2OA9T2KHCE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751390992; 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=ydLqRr0fZA5vg6knK8ONzssFOzXhTziC3Vp61xGzK24=; b=EMEI+znx00xzURIBQSLXFw1xxqOjqF1G2PimB6bGagjHpH/+zYSAHv65DGjODaQgLduNJxeZN3BmUf84yfdzCjnUU7bq0mDWThOyfV/bOFgeqQJndSG0o34r7M4RW2dmB06q9ad2BJSs5ZkFfc5WLLtnxFh2mi0A1chtVihQVnM= 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 1751390992170254.01127923954266; Tue, 1 Jul 2025 10:29:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWene-0003H3-LT; Tue, 01 Jul 2025 13:29:42 -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 1uWenX-00037O-1B for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:37 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenR-0006cZ-5v for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:33 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 36C9D2112225; Tue, 1 Jul 2025 10:29:23 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 36C9D2112225 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390966; bh=ydLqRr0fZA5vg6knK8ONzssFOzXhTziC3Vp61xGzK24=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dc9S39+1NzgAj0C7jpiKx2Dso3QKCotUaEUtFjwUN/a8QVF/AdF9cYZy26TIsgjAf I2BawyStSF5DWNBczR/1wU9Mt3NSLO+4AxT65ICRy0LvZDZHJSHkXiwzi6JnP3moGv 3e28s2ZPcHDEkx/Hwyld6bV2gTuvUt7OoDTcWjTI= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 06/27] accel/mshv: Add accelerator skeleton Date: Tue, 1 Jul 2025 19:28:13 +0200 Message-Id: <20250701172834.44849-7-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751390993246116600 Content-Type: text/plain; charset="utf-8" Introduce the initial scaffold for the MSHV (Microsoft Hypervisor) accelerator backend. This includes the basic directory structure and stub implementations needed to integrate with QEMU's accelerator framework. Signed-off-by: Magnus Kulke --- accel/meson.build | 1 + accel/mshv/meson.build | 6 ++ accel/mshv/mshv-all.c | 143 +++++++++++++++++++++++++++++++++++++++++ include/system/mshv.h | 34 ++++++++++ 4 files changed, 184 insertions(+) create mode 100644 accel/mshv/meson.build create mode 100644 accel/mshv/mshv-all.c diff --git a/accel/meson.build b/accel/meson.build index d5e982d152..efa62879b6 100644 --- a/accel/meson.build +++ b/accel/meson.build @@ -10,6 +10,7 @@ if have_system subdir('kvm') subdir('xen') subdir('stubs') + subdir('mshv') endif =20 # qtest diff --git a/accel/mshv/meson.build b/accel/mshv/meson.build new file mode 100644 index 0000000000..4c03ac7921 --- /dev/null +++ b/accel/mshv/meson.build @@ -0,0 +1,6 @@ +mshv_ss =3D ss.source_set() +mshv_ss.add(if_true: files( + 'mshv-all.c' +)) + +specific_ss.add_all(when: 'CONFIG_MSHV', if_true: mshv_ss) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c new file mode 100644 index 0000000000..ecc34594c2 --- /dev/null +++ b/accel/mshv/mshv-all.c @@ -0,0 +1,143 @@ +/* + * QEMU MSHV support + * + * Copyright Microsoft, Corp. 2025 + * + * Authors: + * Ziqiao Zhou + * Magnus Kulke + * Jinank Jain + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/event_notifier.h" +#include "qemu/module.h" +#include "qemu/main-loop.h" +#include "hw/boards.h" + +#include "hw/hyperv/hvhdk.h" +#include "hw/hyperv/hvhdk_mini.h" +#include "hw/hyperv/hvgdk.h" +#include "linux/mshv.h" + +#include "qemu/accel.h" +#include "qemu/guest-random.h" +#include "system/accel-ops.h" +#include "system/cpus.h" +#include "system/runstate.h" +#include "system/accel-blocker.h" +#include "system/address-spaces.h" +#include "system/mshv.h" +#include "system/reset.h" +#include "trace.h" +#include +#include +#include + +#define TYPE_MSHV_ACCEL ACCEL_CLASS_NAME("mshv") + +DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE, TYPE_MSHV_ACCEL) + +bool mshv_allowed; + +MshvState *mshv_state; + + +static int mshv_init(MachineState *ms) +{ + error_report("unimplemented"); + abort(); +} + +static void mshv_start_vcpu_thread(CPUState *cpu) +{ + error_report("unimplemented"); + abort(); +} + +static void mshv_cpu_synchronize_post_init(CPUState *cpu) +{ + error_report("unimplemented"); + abort(); +} + +static void mshv_cpu_synchronize_post_reset(CPUState *cpu) +{ + error_report("unimplemented"); + abort(); +} + +static void mshv_cpu_synchronize_pre_loadvm(CPUState *cpu) +{ + error_report("unimplemented"); + abort(); +} + +static void mshv_cpu_synchronize(CPUState *cpu) +{ + error_report("unimplemented"); + abort(); +} + +static bool mshv_cpus_are_resettable(void) +{ + error_report("unimplemented"); + abort(); +} + +static void mshv_accel_class_init(ObjectClass *oc, const void *data) +{ + AccelClass *ac =3D ACCEL_CLASS(oc); + + ac->name =3D "MSHV"; + ac->init_machine =3D mshv_init; + ac->allowed =3D &mshv_allowed; +} + +static void mshv_accel_instance_init(Object *obj) +{ + MshvState *s =3D MSHV_STATE(obj); + + s->vm =3D 0; +} + +static const TypeInfo mshv_accel_type =3D { + .name =3D TYPE_MSHV_ACCEL, + .parent =3D TYPE_ACCEL, + .instance_init =3D mshv_accel_instance_init, + .class_init =3D mshv_accel_class_init, + .instance_size =3D sizeof(MshvState), +}; + +static void mshv_accel_ops_class_init(ObjectClass *oc, const void *data) +{ + AccelOpsClass *ops =3D ACCEL_OPS_CLASS(oc); + + ops->create_vcpu_thread =3D mshv_start_vcpu_thread; + ops->synchronize_post_init =3D mshv_cpu_synchronize_post_init; + ops->synchronize_post_reset =3D mshv_cpu_synchronize_post_reset; + ops->synchronize_state =3D mshv_cpu_synchronize; + ops->synchronize_pre_loadvm =3D mshv_cpu_synchronize_pre_loadvm; + ops->cpus_are_resettable =3D mshv_cpus_are_resettable; +} + +static const TypeInfo mshv_accel_ops_type =3D { + .name =3D ACCEL_OPS_NAME("mshv"), + .parent =3D TYPE_ACCEL_OPS, + .class_init =3D mshv_accel_ops_class_init, + .abstract =3D true, +}; + +static void mshv_type_init(void) +{ + type_register_static(&mshv_accel_type); + type_register_static(&mshv_accel_ops_type); +} + +type_init(mshv_type_init); diff --git a/include/system/mshv.h b/include/system/mshv.h index a358691428..695a843582 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -14,6 +14,14 @@ #ifndef QEMU_MSHV_INT_H #define QEMU_MSHV_INT_H =20 +#include "qemu/osdep.h" +#include "qemu/accel.h" +#include "hw/hyperv/hyperv-proto.h" +#include "linux/mshv.h" +#include "hw/hyperv/hvhdk.h" +#include "qapi/qapi-types-common.h" +#include "system/memory.h" + #ifdef COMPILING_PER_TARGET #ifdef CONFIG_MSHV #define CONFIG_MSHV_IS_POSSIBLE @@ -25,6 +33,32 @@ #ifdef CONFIG_MSHV_IS_POSSIBLE extern bool mshv_allowed; #define mshv_enabled() (mshv_allowed) + +typedef struct MshvMemoryListener { + MemoryListener listener; + int as_id; +} MshvMemoryListener; + +typedef struct MshvAddressSpace { + MshvMemoryListener *ml; + AddressSpace *as; +} MshvAddressSpace; + +typedef struct MshvState { + AccelState parent_obj; + int vm; + MshvMemoryListener memory_listener; + /* number of listeners */ + int nr_as; + MshvAddressSpace *as; +} MshvState; +extern MshvState *mshv_state; + +struct AccelCPUState { + int cpufd; + bool dirty; +}; + #else /* CONFIG_MSHV_IS_POSSIBLE */ #define mshv_enabled() false #endif --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391011; cv=none; d=zohomail.com; s=zohoarc; b=K6jxPsbQtJUEb3yUE+kc301AgtermfxE8qlVaTGV7Bfyat0RAmrT7cS6WvnKo4kskSiLDDcmK3Rk1AdUZ8kuz0p4zKMcIrTEsrv4OM88QCwdfEG06U447/YpZl6w8Pc855gVHkhwLTOfmV1pn9QFrlPTYoQeUEgO5cf+4eBHhHs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391011; 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=K1lIkrlruu6oeT5j1K7etit/5d86FNERSzl80JICpEE=; b=O7S6ZKstQy6BQTpH2tHZKi0/ooEWffj0LeS2/A3JmmZlFwhOq1eyKbwYq+tsXWBMWQkuYUBgSV9RXkxM9ferp/EiO6Yy2gNZ/xiP7Jzdz0m/YX+qBcD/qxUeubpDLrqeU81RONFCu2XvNrHGkvvB1H9fsYeis013ls5QkjGeuV0= 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 1751391011931366.21146686804127; Tue, 1 Jul 2025 10:30:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenf-0003Mu-Kt; Tue, 01 Jul 2025 13:29:43 -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 1uWena-00038I-9E for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:38 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenU-0006eH-GZ for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:38 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 50359211222F; Tue, 1 Jul 2025 10:29:27 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 50359211222F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390970; bh=K1lIkrlruu6oeT5j1K7etit/5d86FNERSzl80JICpEE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WjTCB0SClFruSx3+9hSzI7VGOBZ9YvhYOBHqiTalIL3noOlE0uZBeNmlRg2Ii+ZAk AmUK76EE/++Xn2r1eR4fOV7UrRq4H7vauanuVz4MxNLDZuCmonq+WBuv9ju/fdZoVr y1+bOQYtp5sJW84rkU9IguwVzChYiZrH4rGtTaWA= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 07/27] accel/mshv: Register memory region listeners Date: Tue, 1 Jul 2025 19:28:14 +0200 Message-Id: <20250701172834.44849-8-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_SAFE_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: 1751391013444116600 Content-Type: text/plain; charset="utf-8" Add memory listener hooks for the MSHV accelerator to track guest memory regions. This enables the backend to respond to region additions, removals and will be used to manage guest memory mappings inside the hypervisor. Actually registering physical memory in the hypervisor is still stubbed out. Signed-off-by: Magnus Kulke --- accel/mshv/mem.c | 25 ++++++++++++++++ accel/mshv/meson.build | 1 + accel/mshv/mshv-all.c | 68 ++++++++++++++++++++++++++++++++++++++++-- include/system/mshv.h | 4 +++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 accel/mshv/mem.c diff --git a/accel/mshv/mem.c b/accel/mshv/mem.c new file mode 100644 index 0000000000..eddd83ae83 --- /dev/null +++ b/accel/mshv/mem.c @@ -0,0 +1,25 @@ +/* + * QEMU MSHV support + * + * Copyright Microsoft, Corp. 2025 + * + * Authors: + * Magnus Kulke + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "system/address-spaces.h" +#include "system/mshv.h" + +void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryRegionSection *secti= on, + bool add) +{ + error_report("unimplemented"); + abort(); +} + diff --git a/accel/mshv/meson.build b/accel/mshv/meson.build index 4c03ac7921..8a6beb3fb1 100644 --- a/accel/mshv/meson.build +++ b/accel/mshv/meson.build @@ -1,5 +1,6 @@ mshv_ss =3D ss.source_set() mshv_ss.add(if_true: files( + 'mem.c', 'mshv-all.c' )) =20 diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index ecc34594c2..9e0590c4f9 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -49,10 +49,74 @@ bool mshv_allowed; MshvState *mshv_state; =20 =20 +static void mem_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + MshvMemoryListener *mml; + mml =3D container_of(listener, MshvMemoryListener, listener); + memory_region_ref(section->mr); + mshv_set_phys_mem(mml, section, true); +} + +static void mem_region_del(MemoryListener *listener, + MemoryRegionSection *section) +{ + MshvMemoryListener *mml; + mml =3D container_of(listener, MshvMemoryListener, listener); + mshv_set_phys_mem(mml, section, false); + memory_region_unref(section->mr); +} + +static MemoryListener mshv_memory_listener =3D { + .name =3D "mshv", + .priority =3D MEMORY_LISTENER_PRIORITY_ACCEL, + .region_add =3D mem_region_add, + .region_del =3D mem_region_del, +}; + +static MemoryListener mshv_io_listener =3D { + .name =3D "mshv", .priority =3D MEMORY_LISTENER_PRIORITY_DEV_BACKEND, + /* MSHV does not support PIO eventfd */ +}; + +static void register_mshv_memory_listener(MshvState *s, MshvMemoryListener= *mml, + AddressSpace *as, int as_id, + const char *name) +{ + int i; + + mml->listener =3D mshv_memory_listener; + mml->listener.name =3D name; + memory_listener_register(&mml->listener, as); + for (i =3D 0; i < s->nr_as; ++i) { + if (!s->as[i].as) { + s->as[i].as =3D as; + s->as[i].ml =3D mml; + break; + } + } +} + + static int mshv_init(MachineState *ms) { - error_report("unimplemented"); - abort(); + MshvState *s; + s =3D MSHV_STATE(ms->accelerator); + + accel_blocker_init(); + + s->vm =3D 0; + + s->nr_as =3D 1; + s->as =3D g_new0(MshvAddressSpace, s->nr_as); + + mshv_state =3D s; + + register_mshv_memory_listener(s, &s->memory_listener, &address_space_m= emory, + 0, "mshv-memory"); + memory_listener_register(&mshv_io_listener, &address_space_io); + + return 0; } =20 static void mshv_start_vcpu_thread(CPUState *cpu) diff --git a/include/system/mshv.h b/include/system/mshv.h index 695a843582..43a22e0f48 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -68,6 +68,10 @@ struct AccelCPUState { #define mshv_msi_via_irqfd_enabled() false #endif =20 +/* memory */ +void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryRegionSection *secti= on, + bool add); + /* interrupt */ int mshv_irqchip_add_msi_route(int vector, PCIDevice *dev); int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev= ); --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391036; cv=none; d=zohomail.com; s=zohoarc; b=GHn+M4GnU9IjnAspOvT2Obg1rvf0rV630Q+wVMOXcYFQU0kutoLxrL6DZRz/TRenqu+A9Ot+in2p913LE95K1t1c3BZ36zTgm9bUigjXtV6VMobyC5q1zx759+AF+VQBvfkG8ZxZ8Iw7S5l6gjwzbwZQBVI+RzqtX18/n+tLQOY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391036; 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=k9Vqnknl7noR3cqrniCSd1rI0N2lT3zf5F9ifkoQOh0=; b=LWuK+uJgGTE4TEap7X2SG0VTyJ+kg65zmf96DT+iTpCUD4qtvS2jTBrTAXezGbQqSfnG+vbDzSzgjGPKpLvRI/EaAH9Tp25FihZqmatOVL+gS+Azy0aH2DMsCOJRKDiYSQdVri7cviw/Zo/Gg2dtGKsbmv2czdYp3QDxjA8SKfQ= 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 1751391036216709.7494528087808; Tue, 1 Jul 2025 10:30:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeng-0003R1-II; Tue, 01 Jul 2025 13:29:44 -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 1uWenb-00039T-55 for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:40 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenY-0006gq-QT for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:38 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 4FE872112233; Tue, 1 Jul 2025 10:29:31 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4FE872112233 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390974; bh=k9Vqnknl7noR3cqrniCSd1rI0N2lT3zf5F9ifkoQOh0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qHzOz+SwjcZrIPYJrJI/QMvdkgiOF7V7mQFHDbD0IxGMqYTb0d+Cc/GaqxStBZDuj jRyIhFG0JgoSGfUJNGOh31Hv/jh8X4aEDatzBNKD/0yESLE60S1LW0wOMmMaCAazye u+Ba83BsfACzn4acufzruh/OqdIgv5jqREzVAmB4= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 08/27] accel/mshv: Initialize VM partition Date: Tue, 1 Jul 2025 19:28:15 +0200 Message-Id: <20250701172834.44849-9-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391038077116600 Content-Type: text/plain; charset="utf-8" Create the MSHV virtual machine by opening a partition and issuing the necessary ioctl to initialize it. This sets up the basic VM structure and initial configuration used by MSHV to manage guest state. Signed-off-by: Magnus Kulke --- accel/mshv/mshv-all.c | 210 ++++++++++++++++++++++++++++++++++- accel/mshv/trace-events | 3 + accel/mshv/trace.h | 1 + include/system/mshv.h | 20 +++- meson.build | 1 + target/i386/mshv/meson.build | 1 + target/i386/mshv/mshv-cpu.c | 71 ++++++++++++ 7 files changed, 300 insertions(+), 7 deletions(-) create mode 100644 accel/mshv/trace-events create mode 100644 accel/mshv/trace.h create mode 100644 target/i386/mshv/mshv-cpu.c diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index 9e0590c4f9..712e651627 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -46,8 +46,177 @@ DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE, TYPE_MS= HV_ACCEL) =20 bool mshv_allowed; =20 -MshvState *mshv_state; +MshvState *mshv_state =3D NULL; =20 +static int init_mshv(int *mshv_fd) +{ + int fd =3D open("/dev/mshv", O_RDWR | O_CLOEXEC); + if (fd < 0) { + error_report("Failed to open /dev/mshv: %s", strerror(errno)); + return -1; + } + *mshv_fd =3D fd; + return 0; +} + +/* freeze 1 to pause, 0 to resume */ +static int set_time_freeze(int vm_fd, int freeze) +{ + int ret; + + if (freeze !=3D 0 && freeze !=3D 1) { + error_report("Invalid time freeze value"); + return -1; + } + + struct hv_input_set_partition_property in =3D {0}; + in.property_code =3D HV_PARTITION_PROPERTY_TIME_FREEZE; + in.property_value =3D freeze; + + struct mshv_root_hvcall args =3D {0}; + args.code =3D HVCALL_SET_PARTITION_PROPERTY; + args.in_sz =3D sizeof(in); + args.in_ptr =3D (uint64_t)∈ + + ret =3D mshv_hvcall(vm_fd, &args); + if (ret < 0) { + error_report("Failed to set time freeze"); + return -1; + } + + return 0; +} + +static int pause_vm(int vm_fd) +{ + int ret; + + ret =3D set_time_freeze(vm_fd, 1); + if (ret < 0) { + error_report("Failed to pause partition: %s", strerror(errno)); + return -1; + } + + return 0; +} + +static int resume_vm(int vm_fd) +{ + int ret; + + ret =3D set_time_freeze(vm_fd, 0); + if (ret < 0) { + error_report("Failed to resume partition: %s", strerror(errno)); + return -1; + } + + return 0; +} + +static int create_partition(int mshv_fd, int *vm_fd) +{ + int ret; + struct mshv_create_partition args =3D {0}; + + /* Initialize pt_flags with the desired features */ + uint64_t pt_flags =3D (1ULL << MSHV_PT_BIT_LAPIC) | + (1ULL << MSHV_PT_BIT_X2APIC) | + (1ULL << MSHV_PT_BIT_GPA_SUPER_PAGES); + + /* Set default isolation type */ + uint64_t pt_isolation =3D MSHV_PT_ISOLATION_NONE; + + args.pt_flags =3D pt_flags; + args.pt_isolation =3D pt_isolation; + + ret =3D ioctl(mshv_fd, MSHV_CREATE_PARTITION, &args); + if (ret < 0) { + error_report("Failed to create partition: %s", strerror(errno)); + return -1; + } + + *vm_fd =3D ret; + return 0; +} + +static int set_synthetic_proc_features(int vm_fd) +{ + int ret; + struct hv_input_set_partition_property in =3D {0}; + union hv_partition_synthetic_processor_features features =3D {0}; + + /* Access the bitfield and set the desired features */ + features.hypervisor_present =3D 1; + features.hv1 =3D 1; + features.access_partition_reference_counter =3D 1; + features.access_synic_regs =3D 1; + features.access_synthetic_timer_regs =3D 1; + features.access_partition_reference_tsc =3D 1; + features.access_frequency_regs =3D 1; + features.access_intr_ctrl_regs =3D 1; + features.access_vp_index =3D 1; + features.access_hypercall_regs =3D 1; + features.tb_flush_hypercalls =3D 1; + features.synthetic_cluster_ipi =3D 1; + features.direct_synthetic_timers =3D 1; + + mshv_arch_amend_proc_features(&features); + + in.property_code =3D HV_PARTITION_PROPERTY_SYNTHETIC_PROC_FEATURES; + in.property_value =3D features.as_uint64[0]; + + struct mshv_root_hvcall args =3D {0}; + args.code =3D HVCALL_SET_PARTITION_PROPERTY; + args.in_sz =3D sizeof(in); + args.in_ptr =3D (uint64_t)∈ + + trace_mshv_hvcall_args("synthetic_proc_features", args.code, args.in_s= z); + + ret =3D mshv_hvcall(vm_fd, &args); + if (ret < 0) { + error_report("Failed to set synthethic proc features"); + return -errno; + } + return 0; +} + +static int initialize_vm(int vm_fd) +{ + int ret =3D ioctl(vm_fd, MSHV_INITIALIZE_PARTITION); + if (ret < 0) { + error_report("Failed to initialize partition: %s", strerror(errno)= ); + return -1; + } + return 0; +} + +static int create_vm(int mshv_fd, int *vm_fd) +{ + int ret =3D create_partition(mshv_fd, vm_fd); + if (ret < 0) { + return -1; + } + + ret =3D set_synthetic_proc_features(*vm_fd); + if (ret < 0) { + return -1; + } + + ret =3D initialize_vm(*vm_fd); + if (ret < 0) { + return -1; + } + + ret =3D mshv_arch_post_init_vm(*vm_fd); + if (ret < 0) { + return -1; + } + + /* Always create a frozen partition */ + pause_vm(*vm_fd); + + return 0; +} =20 static void mem_region_add(MemoryListener *listener, MemoryRegionSection *section) @@ -97,16 +266,55 @@ static void register_mshv_memory_listener(MshvState *s= , MshvMemoryListener *mml, } } =20 +int mshv_hvcall(int vm_fd, const struct mshv_root_hvcall *args) +{ + int ret =3D 0; + + ret =3D ioctl(vm_fd, MSHV_ROOT_HVCALL, args); + if (ret < 0) { + error_report("Failed to perform hvcall: %s", strerror(errno)); + return -1; + } + return ret; +} + =20 static int mshv_init(MachineState *ms) { MshvState *s; + int mshv_fd, vm_fd, ret; + + if (mshv_state) { + warn_report("MSHV accelerator already initialized"); + return 0; + } + s =3D MSHV_STATE(ms->accelerator); =20 accel_blocker_init(); =20 s->vm =3D 0; =20 + ret =3D init_mshv(&mshv_fd); + if (ret < 0) { + return -1; + } + + ret =3D create_vm(mshv_fd, &vm_fd); + if (ret < 0) { + close(mshv_fd); + return -1; + } + + ret =3D resume_vm(vm_fd); + if (ret < 0) { + close(mshv_fd); + close(vm_fd); + return -1; + } + + s->vm =3D vm_fd; + s->fd =3D mshv_fd; s->nr_as =3D 1; s->as =3D g_new0(MshvAddressSpace, s->nr_as); =20 diff --git a/accel/mshv/trace-events b/accel/mshv/trace-events new file mode 100644 index 0000000000..f99e8c5a41 --- /dev/null +++ b/accel/mshv/trace-events @@ -0,0 +1,3 @@ +# See docs/devel/tracing.rst for syntax documentation. + +mshv_hvcall_args(const char* hvcall, uint16_t code, uint16_t in_sz) "built= args for '%s' code: %d in_sz: %d" diff --git a/accel/mshv/trace.h b/accel/mshv/trace.h new file mode 100644 index 0000000000..da5b40cd24 --- /dev/null +++ b/accel/mshv/trace.h @@ -0,0 +1 @@ +#include "trace/trace-accel_mshv.h" diff --git a/include/system/mshv.h b/include/system/mshv.h index 43a22e0f48..2ac594d0aa 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -45,12 +45,13 @@ typedef struct MshvAddressSpace { } MshvAddressSpace; =20 typedef struct MshvState { - AccelState parent_obj; - int vm; - MshvMemoryListener memory_listener; - /* number of listeners */ - int nr_as; - MshvAddressSpace *as; + AccelState parent_obj; + int vm; + MshvMemoryListener memory_listener; + /* number of listeners */ + int nr_as; + MshvAddressSpace *as; + int fd; } MshvState; extern MshvState *mshv_state; =20 @@ -68,6 +69,13 @@ struct AccelCPUState { #define mshv_msi_via_irqfd_enabled() false #endif =20 +/* cpu */ +void mshv_arch_amend_proc_features( + union hv_partition_synthetic_processor_features *features); +int mshv_arch_post_init_vm(int vm_fd); + +int mshv_hvcall(int mshv_fd, const struct mshv_root_hvcall *args); + /* memory */ void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryRegionSection *secti= on, bool add); diff --git a/meson.build b/meson.build index 927f3474ea..b19772d27f 100644 --- a/meson.build +++ b/meson.build @@ -3640,6 +3640,7 @@ endif if have_system trace_events_subdirs +=3D [ 'accel/kvm', + 'accel/mshv', 'audio', 'backends', 'backends/tpm', diff --git a/target/i386/mshv/meson.build b/target/i386/mshv/meson.build index 8ddaa7c11d..647e5dafb7 100644 --- a/target/i386/mshv/meson.build +++ b/target/i386/mshv/meson.build @@ -1,6 +1,7 @@ i386_mshv_ss =3D ss.source_set() =20 i386_mshv_ss.add(files( + 'mshv-cpu.c', 'x86.c', )) =20 diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c new file mode 100644 index 0000000000..c00e98dfba --- /dev/null +++ b/target/i386/mshv/mshv-cpu.c @@ -0,0 +1,71 @@ +/* + * QEMU MSHV support + * + * Copyright Microsoft, Corp. 2025 + * + * Authors: Ziqiao Zhou + * Magnus Kulke + * Jinank Jain + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "qemu/typedefs.h" + +#include "system/mshv.h" +#include "system/address-spaces.h" +#include "linux/mshv.h" +#include "hw/hyperv/hvhdk_mini.h" +#include "hw/hyperv/hvgdk.h" + + +#include "trace-accel_mshv.h" +#include "trace.h" + +void mshv_arch_amend_proc_features( + union hv_partition_synthetic_processor_features *features) +{ + features->access_guest_idle_reg =3D 1; +} + +/* + * Default Microsoft Hypervisor behavior for unimplemented MSR is to send a + * fault to the guest if it tries to access it. It is possible to override + * this behavior with a more suitable option i.e., ignore writes from the = guest + * and return zero in attempt to read unimplemented. + */ +static int set_unimplemented_msr_action(int vm_fd) +{ + struct hv_input_set_partition_property in =3D {0}; + struct mshv_root_hvcall args =3D {0}; + + in.property_code =3D HV_PARTITION_PROPERTY_UNIMPLEMENTED_MSR_ACTION; + in.property_value =3D HV_UNIMPLEMENTED_MSR_ACTION_IGNORE_WRITE_READ_ZE= RO; + + args.code =3D HVCALL_SET_PARTITION_PROPERTY; + args.in_sz =3D sizeof(in); + args.in_ptr =3D (uint64_t)∈ + + trace_mshv_hvcall_args("unimplemented_msr_action", args.code, args.in_= sz); + + int ret =3D mshv_hvcall(vm_fd, &args); + if (ret < 0) { + error_report("Failed to set unimplemented MSR action"); + return -1; + } + return 0; +} + +int mshv_arch_post_init_vm(int vm_fd) +{ + int ret; + + ret =3D set_unimplemented_msr_action(vm_fd); + if (ret < 0) { + error_report("Failed to set unimplemented MSR action"); + } + + return ret; +} --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391038; cv=none; d=zohomail.com; s=zohoarc; b=PYNBaCHwQsa3SejonICcd+0PMdtjc6J4OyZpCgy6T9T5byBBpLmocMzJMgqGjX1gwfgj122ogGntKpswM4CX9USV6Pe1Ol7Gu/6AMpWgQRwEBdb6NnIo2tTTR2966g9KU1OyFR2XP3HNggQVr2l1O2HgOkvIzNAh6MmzteICx8M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391038; 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=zf2TV5FOzkuzCPJD7r+iNLBabEyrEw/LvBzD8827WT8=; b=jalWktTa3VbFq7Q75/GaoxEnDGUT/LCgYh2VV6FDL8TrMDHy/B3PA332j5XlTuX3Dxr8leYw6oInzyUq5g9kr6HeKMFas8nkSP4L8GLTAnwdmhPk06Zz5fYp6yneqzx8xoaS8gnMI40SFuwMlh1UBBwkgci/VysBVqEz8vqSjMk= 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 17513910388450.5768415113944911; Tue, 1 Jul 2025 10:30:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenl-0003ww-1I; Tue, 01 Jul 2025 13:29:49 -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 1uWeni-0003iM-Ii for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:46 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenc-0006jf-7a for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:46 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 4C1632112234; Tue, 1 Jul 2025 10:29:35 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4C1632112234 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390979; bh=zf2TV5FOzkuzCPJD7r+iNLBabEyrEw/LvBzD8827WT8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J3a4DYeIan81StTRhJu3Bx+xoCIwt0Wb62hrTRbgALSL7GG+4FU79Sm4uqCdPHHXj Cd4ZtK+PoicjR7JmkMHrG+Xw/8qJYI3vmJH7EWgrDI4L1sQzrTcVxpgAuSmGnRcYgH gvrJyLzPa1UP+exyq/vKF3MhB/eEMmnzk6KXI4ac= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 09/27] accel/mshv: Register guest memory regions with hypervisor Date: Tue, 1 Jul 2025 19:28:16 +0200 Message-Id: <20250701172834.44849-10-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391039954116600 Content-Type: text/plain; charset="utf-8" Handle region_add events by invoking the MSHV memory registration ioctl to map guest memory into the hypervisor partition. This allows the guest to access memory through MSHV-managed mappings. Note that this assumes the hypervisor will accept regions that overlap in userspace_addr. Currently that's not the case, it will be addressed in a later commit in the series. Signed-off-by: Magnus Kulke --- accel/mshv/mem.c | 127 +++++++++++++++++++++++++++++++++++++++- accel/mshv/trace-events | 16 +++++ include/system/mshv.h | 11 ++++ 3 files changed, 151 insertions(+), 3 deletions(-) diff --git a/accel/mshv/mem.c b/accel/mshv/mem.c index eddd83ae83..f51e9fee8e 100644 --- a/accel/mshv/mem.c +++ b/accel/mshv/mem.c @@ -13,13 +13,134 @@ =20 #include "qemu/osdep.h" #include "qemu/error-report.h" +#include "linux/mshv.h" #include "system/address-spaces.h" #include "system/mshv.h" +#include "exec/memattrs.h" +#include +#include "trace.h" + +static int set_guest_memory(int vm_fd, const mshv_user_mem_region *region) +{ + int ret; + + ret =3D ioctl(vm_fd, MSHV_SET_GUEST_MEMORY, region); + if (ret < 0) { + error_report("failed to set guest memory"); + return -errno; + } + + return 0; +} + +static int map_or_unmap(int vm_fd, const MshvMemoryRegion *mr, bool map) +{ + struct mshv_user_mem_region region =3D {0}; + + region.guest_pfn =3D mr->guest_phys_addr >> MSHV_PAGE_SHIFT; + region.size =3D mr->memory_size; + region.userspace_addr =3D mr->userspace_addr; + + if (!map) { + region.flags |=3D (1 << MSHV_SET_MEM_BIT_UNMAP); + trace_mshv_unmap_memory(mr->userspace_addr, mr->guest_phys_addr, + mr->memory_size); + return set_guest_memory(vm_fd, ®ion); + } + + region.flags =3D BIT(MSHV_SET_MEM_BIT_EXECUTABLE); + if (!mr->readonly) { + region.flags |=3D BIT(MSHV_SET_MEM_BIT_WRITABLE); + } + + trace_mshv_map_memory(mr->userspace_addr, mr->guest_phys_addr, + mr->memory_size); + return set_guest_memory(vm_fd, ®ion); +} + +static int set_memory(const MshvMemoryRegion *mshv_mr, bool add) +{ + int ret =3D 0; + + if (!mshv_mr) { + error_report("Invalid mshv_mr"); + return -1; + } + + trace_mshv_set_memory(add, mshv_mr->guest_phys_addr, + mshv_mr->memory_size, + mshv_mr->userspace_addr, mshv_mr->readonly, + ret); + return map_or_unmap(mshv_state->vm, mshv_mr, add); +} + +/* + * Calculate and align the start address and the size of the section. + * Return the size. If the size is 0, the aligned section is empty. + */ +static hwaddr align_section(MemoryRegionSection *section, hwaddr *start) +{ + hwaddr size =3D int128_get64(section->size); + hwaddr delta, aligned; + + /* + * works in page size chunks, but the function may be called + * with sub-page size and unaligned start address. Pad the start + * address to next and truncate size to previous page boundary. + */ + aligned =3D ROUND_UP(section->offset_within_address_space, + qemu_real_host_page_size()); + delta =3D aligned - section->offset_within_address_space; + *start =3D aligned; + if (delta > size) { + return 0; + } + + return (size - delta) & qemu_real_host_page_mask(); +} =20 void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryRegionSection *secti= on, bool add) { - error_report("unimplemented"); - abort(); -} + int ret =3D 0; + MemoryRegion *area =3D section->mr; + bool writable =3D !area->readonly && !area->rom_device; + hwaddr start_addr, mr_offset, size; + void *ram; + MshvMemoryRegion mshv_mr =3D {0}; + + trace_mshv_set_phys_mem(add, section->mr->name); + + /* If the memory device is a writable non-ram area, we do not + * want to map it into the guest memory. If it is not a ROM device, + * we want to remove mshv memory mapping, so accesses will trap. + */ + if (!memory_region_is_ram(area)) { + if (writable) { + return; + } else if (!area->romd_mode) { + add =3D false; + } + } + + size =3D align_section(section, &start_addr); + if (!size) { + return; + } =20 + mr_offset =3D section->offset_within_region + start_addr - + section->offset_within_address_space; + + ram =3D memory_region_get_ram_ptr(area) + mr_offset; + + mshv_mr.guest_phys_addr =3D start_addr; + mshv_mr.memory_size =3D size; + mshv_mr.readonly =3D !writable; + mshv_mr.userspace_addr =3D (uint64_t)ram; + + ret =3D set_memory(&mshv_mr, add); + if (ret < 0) { + error_report("Failed to set memory region"); + abort(); + } +} diff --git a/accel/mshv/trace-events b/accel/mshv/trace-events index f99e8c5a41..9a3af6b8be 100644 --- a/accel/mshv/trace-events +++ b/accel/mshv/trace-events @@ -1,3 +1,19 @@ # See docs/devel/tracing.rst for syntax documentation. =20 +mshv_set_memory(bool add, uint64_t gpa, uint64_t size, uint64_t user_addr,= bool readonly, int ret) "[add =3D %d] gpa =3D %lx size =3D %lx user =3D %l= x readonly =3D %d result =3D %d" mshv_hvcall_args(const char* hvcall, uint16_t code, uint16_t in_sz) "built= args for '%s' code: %d in_sz: %d" + +mshv_set_msi_routing(uint32_t gsi, uint64_t addr, uint32_t data) "gsi=3D%d= addr=3D%lx data=3D%x" +mshv_remove_msi_routing(uint32_t gsi) "gsi=3D%d" +mshv_add_msi_routing(uint64_t addr, uint32_t data) "addr=3D%lx data=3D%x" +mshv_commit_msi_routing_table(int vm_fd, int len) "vm_fd=3D%d table_size= =3D%d" +mshv_register_irqfd(int vm_fd, int event_fd, uint32_t gsi) "vm_fd=3D%d eve= nt_fd=3D%d gsi=3D%d" +mshv_irqchip_update_irqfd_notifier_gsi(int event_fd, int resample_fd, int = virq, bool add) "event_fd=3D%d resample_fd=3D%d virq=3D%d add=3D%d" + +mshv_insn_fetch(uint64_t addr, size_t size) "gpa=3D%lx size=3D%lu" +mshv_mem_write(uint64_t addr, size_t size) "\tgpa=3D%lx size=3D%lu" +mshv_mem_read(uint64_t addr, size_t size) "\tgpa=3D%lx size=3D%lu" +mshv_map_memory(uint64_t userspace_addr, uint64_t gpa, uint64_t size) "\tu= _a=3D%lx gpa=3D%010lx size=3D%08lx" +mshv_unmap_memory(uint64_t userspace_addr, uint64_t gpa, uint64_t size) "\= tu_a=3D%lx gpa=3D%010lx size=3D%08lx" +mshv_set_phys_mem(bool add, const char *name) "\tadd=3D%d name=3D%s" +mshv_handle_mmio(uint64_t gva, uint64_t gpa, uint64_t size, uint8_t access= _type) "\tgva=3D%lx gpa=3D%010lx size=3D%lx access_type=3D%d" diff --git a/include/system/mshv.h b/include/system/mshv.h index 2ac594d0aa..3624d9477f 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -30,6 +30,8 @@ #define CONFIG_MSHV_IS_POSSIBLE #endif =20 +#define MSHV_PAGE_SHIFT 12 + #ifdef CONFIG_MSHV_IS_POSSIBLE extern bool mshv_allowed; #define mshv_enabled() (mshv_allowed) @@ -77,6 +79,15 @@ int mshv_arch_post_init_vm(int vm_fd); int mshv_hvcall(int mshv_fd, const struct mshv_root_hvcall *args); =20 /* memory */ +typedef struct MshvMemoryRegion { + uint64_t guest_phys_addr; + uint64_t memory_size; + uint64_t userspace_addr; + bool readonly; +} MshvMemoryRegion; + +int mshv_add_mem(int vm_fd, const MshvMemoryRegion *mr); +int mshv_remove_mem(int vm_fd, const MshvMemoryRegion *mr); void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryRegionSection *secti= on, bool add); =20 --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391367; cv=none; d=zohomail.com; s=zohoarc; b=ktQglB9XLgXMJR6oyFdd28sU1cj5ObKvq63wwYCv/BKnavornLd7OHels4biLsBY+eS6p8/cb2N2wyLnUdz3PmifKVJISUup/U/cB+ts2EI563W3JtQfZfpTN5rxePaQiFqSQ08fM87QQQW/nCnGsek5xaLsn0xc9LD8sLlwjEw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391367; 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=Y4vhPW1L/apWF7A2PygowCDp5jO99b8a9HgfYCjLboI=; b=D7Mv3wq8uoEpg+vZOt+RM5Gj3d/B7iVndjgj8fpQ+wCjQRjmzRTi9eEFrRJO/BsU0r4nsgKw8+7Bi+oLYzMd3QKCw6nX/sOPU6l1fW2BhQ7uVf7ATRIU1yNs3W1AdwBDxocrDIEYZsZZWihTqrb7iyP1wOGF22TGNozhaotpuIA= 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 175139136765464.70023587687115; Tue, 1 Jul 2025 10:36:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenl-0003y7-IG; Tue, 01 Jul 2025 13:29:49 -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 1uWenj-0003np-Mp for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:47 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeng-0006ln-5d for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:47 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 6F53C2112236; Tue, 1 Jul 2025 10:29:39 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 6F53C2112236 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390983; bh=Y4vhPW1L/apWF7A2PygowCDp5jO99b8a9HgfYCjLboI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hycZXmQZF60SDp6nNrn1MYE18EJXtTfFFU1NarpNWbg/EuM1KC/ZhBIzdniwggSjb gwM7RytFV6O74eg37fLRDjm83RSdohXNxDZLe0Q2gaJwVGadaD7qNoqbkgqQ3mVLtK SjxAYfdzQx6gKJmawKtjpV3wltEo50cWmZ0S2Z08= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 10/27] accel/mshv: Add ioeventfd support Date: Tue, 1 Jul 2025 19:28:17 +0200 Message-Id: <20250701172834.44849-11-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391368437116600 Content-Type: text/plain; charset="utf-8" Implement ioeventfd registration in the MSHV accelerator backend to handle guest-triggered events. This enables integration with QEMU's eventfd-based I/O mechanism. Signed-off-by: Magnus Kulke --- accel/mshv/mshv-all.c | 116 ++++++++++++++++++++++++++++++++++++++++ accel/mshv/trace-events | 3 ++ include/system/mshv.h | 8 +++ 3 files changed, 127 insertions(+) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index 712e651627..2ae9d1cffa 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -7,6 +7,7 @@ * Ziqiao Zhou * Magnus Kulke * Jinank Jain + * Wei Liu * * This work is licensed under the terms of the GNU GPL, version 2 or late= r. * See the COPYING file in the top-level directory. @@ -236,11 +237,126 @@ static void mem_region_del(MemoryListener *listener, memory_region_unref(section->mr); } =20 +typedef enum { + DATAMATCH_NONE, + DATAMATCH_U32, + DATAMATCH_U64, +} DatamatchTag; + +typedef struct { + DatamatchTag tag; + union { + uint32_t u32; + uint64_t u64; + } value; +} Datamatch; + +/* flags: determine whether to de/assign */ +static int ioeventfd(int vm_fd, int event_fd, uint64_t addr, Datamatch dm, + uint32_t flags) +{ + mshv_user_ioeventfd args =3D {0}; + args.fd =3D event_fd; + args.addr =3D addr; + args.flags =3D flags; + + if (dm.tag =3D=3D DATAMATCH_NONE) { + args.datamatch =3D 0; + } else { + flags |=3D BIT(MSHV_IOEVENTFD_BIT_DATAMATCH); + args.flags =3D flags; + if (dm.tag =3D=3D DATAMATCH_U64) { + args.len =3D sizeof(uint64_t); + args.datamatch =3D dm.value.u64; + } else { + args.len =3D sizeof(uint32_t); + args.datamatch =3D dm.value.u32; + } + } + + return ioctl(vm_fd, MSHV_IOEVENTFD, &args); +} + +static int unregister_ioevent(int vm_fd, int event_fd, uint64_t mmio_addr) +{ + uint32_t flags =3D 0; + Datamatch dm =3D {0}; + + flags |=3D BIT(MSHV_IOEVENTFD_BIT_DEASSIGN); + dm.tag =3D DATAMATCH_NONE; + + return ioeventfd(vm_fd, event_fd, mmio_addr, dm, flags); +} + +static int register_ioevent(int vm_fd, int event_fd, uint64_t mmio_addr, + uint64_t val, bool is_64bit, bool is_datamatch) +{ + uint32_t flags =3D 0; + Datamatch dm =3D {0}; + + if (!is_datamatch) { + dm.tag =3D DATAMATCH_NONE; + } else if (is_64bit) { + dm.tag =3D DATAMATCH_U64; + dm.value.u64 =3D val; + } else { + dm.tag =3D DATAMATCH_U32; + dm.value.u32 =3D val; + } + + return ioeventfd(vm_fd, event_fd, mmio_addr, dm, flags); +} + +static void mem_ioeventfd_add(MemoryListener *listener, + MemoryRegionSection *section, + bool match_data, uint64_t data, + EventNotifier *e) +{ + int fd =3D event_notifier_get_fd(e); + int ret; + bool is_64 =3D int128_get64(section->size) =3D=3D 8; + uint64_t addr =3D section->offset_within_address_space; + + trace_mshv_mem_ioeventfd_add(addr, int128_get64(section->size), data); + + ret =3D register_ioevent(mshv_state->vm, fd, addr, data, is_64, match_= data); + + if (ret < 0) { + error_report("Failed to register ioeventfd: %s (%d)", strerror(-re= t), + -ret); + abort(); + } +} + +static void mem_ioeventfd_del(MemoryListener *listener, + MemoryRegionSection *section, + bool match_data, uint64_t data, + EventNotifier *e) +{ + int fd =3D event_notifier_get_fd(e); + int ret; + uint64_t addr =3D section->offset_within_address_space; + + trace_mshv_mem_ioeventfd_del(section->offset_within_address_space, + int128_get64(section->size), data); + + ret =3D unregister_ioevent(mshv_state->vm, fd, addr); + if (ret < 0) { + error_report("Failed to unregister ioeventfd: %s (%d)", strerror(-= ret), + -ret); + abort(); + } +} + static MemoryListener mshv_memory_listener =3D { .name =3D "mshv", .priority =3D MEMORY_LISTENER_PRIORITY_ACCEL, .region_add =3D mem_region_add, .region_del =3D mem_region_del, +#ifdef MSHV_USE_IOEVENTFD + .eventfd_add =3D mem_ioeventfd_add, + .eventfd_del =3D mem_ioeventfd_del, +#endif }; =20 static MemoryListener mshv_io_listener =3D { diff --git a/accel/mshv/trace-events b/accel/mshv/trace-events index 9a3af6b8be..b49a5b1702 100644 --- a/accel/mshv/trace-events +++ b/accel/mshv/trace-events @@ -1,6 +1,9 @@ # See docs/devel/tracing.rst for syntax documentation. =20 mshv_set_memory(bool add, uint64_t gpa, uint64_t size, uint64_t user_addr,= bool readonly, int ret) "[add =3D %d] gpa =3D %lx size =3D %lx user =3D %l= x readonly =3D %d result =3D %d" +mshv_mem_ioeventfd_add(uint64_t addr, uint32_t size, uint32_t data) "addr = %lx size %d data %x" +mshv_mem_ioeventfd_del(uint64_t addr, uint32_t size, uint32_t data) "addr = %lx size %d data %x" + mshv_hvcall_args(const char* hvcall, uint16_t code, uint16_t in_sz) "built= args for '%s' code: %d in_sz: %d" =20 mshv_set_msi_routing(uint32_t gsi, uint64_t addr, uint32_t data) "gsi=3D%d= addr=3D%lx data=3D%x" diff --git a/include/system/mshv.h b/include/system/mshv.h index 3624d9477f..c2b0414c85 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -30,6 +30,14 @@ #define CONFIG_MSHV_IS_POSSIBLE #endif =20 +/* + * Set to 0 if we do not want to use eventfd to optimize the MMIO events. + * Set to 1 so that mshv kernel driver receives doorbell when the VM access + * MMIO memory and then signal eventfd to notify the qemu device + * without extra switching to qemu to emulate mmio access. + */ +#define MSHV_USE_IOEVENTFD 1 + #define MSHV_PAGE_SHIFT 12 =20 #ifdef CONFIG_MSHV_IS_POSSIBLE --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391021; cv=none; d=zohomail.com; s=zohoarc; b=achZGd5zzW4od0qQKAymQVeRyDQKJwbZ32I2VVNaFxeAys+0kfjqOsKoE8M8IvKXoMiWuTrDc6GyXXIEn/gPRyj75pg9CMrtPVpHNva2duMURw7cZWT6SdlYtj2XnyodqLdA+i7mIjq7YSZh3gWSkoW6mXcyfJuoMont5DVGuMs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391021; 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=6zIGOx5vf3yl1XsgSb8tjWh62etNn82izmqXHhOSkEk=; b=f+lnQPgxDSGhH2Ql/+hlBtSv1i/KuWN21Gd48gUt9c93gUGSz8PS4TVYjcbQKq0WFUGYW636y8IAT0RI9A2Is/v4IGnsgxs2TU1pTXicKLWEdoJYkfm8QKvTlTGaD52Qec89xa5DUFLpfc66t+BKniucG66PuM2LbkY6Agyi4XY= 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 1751391021794630.2748943518509; Tue, 1 Jul 2025 10:30:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWens-0004Bh-9j; Tue, 01 Jul 2025 13:29:57 -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 1uWenn-00047U-RW for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:51 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenl-0006nQ-4j for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:51 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 69B102112235; Tue, 1 Jul 2025 10:29:43 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 69B102112235 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390987; bh=6zIGOx5vf3yl1XsgSb8tjWh62etNn82izmqXHhOSkEk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QmJ23jA9BT5XF3f33XvxZUyFeQcQHeiZNUsguWmU7bF0tykMoDujjXoy+YbYB9nJp R0N3m8SqQaRpR3djDfBXTVj67BFiIPIPu3z4zoTas9xsahzVmZvIXzUhW9oAQnK17t LiscfpyYpj3lF0YvQmM9VUo1vMZbGwdaO73jv7xg= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 11/27] accel/mshv: Add basic interrupt injection support Date: Tue, 1 Jul 2025 19:28:18 +0200 Message-Id: <20250701172834.44849-12-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391023791116600 Content-Type: text/plain; charset="utf-8" Implement initial interrupt handling logic in the MSHV backend. This includes management of MSI and un/registering of irqfd mechanisms. Co-authored-by: Stanislav Kinsburskii Signed-off-by: Magnus Kulke --- accel/mshv/irq.c | 369 ++++++++++++++++++++++++++++++++++++++++ accel/mshv/meson.build | 1 + accel/mshv/mshv-all.c | 4 +- accel/mshv/trace-events | 1 + hw/intc/apic.c | 9 + include/system/mshv.h | 24 +-- 6 files changed, 395 insertions(+), 13 deletions(-) create mode 100644 accel/mshv/irq.c diff --git a/accel/mshv/irq.c b/accel/mshv/irq.c new file mode 100644 index 0000000000..9c2647fa06 --- /dev/null +++ b/accel/mshv/irq.c @@ -0,0 +1,369 @@ +/* + * QEMU MSHV support + * + * Copyright Microsoft, Corp. 2025 + * + * Authors: Ziqiao Zhou + * Magnus Kulke + * Stanislav Kinsburskii + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "linux/mshv.h" +#include "hw/hyperv/hvhdk_mini.h" +#include "qemu/osdep.h" +#include "hw/pci/msi.h" +#include "system/mshv.h" +#include "trace.h" +#include +#include + +#define MSHV_IRQFD_RESAMPLE_FLAG (1 << MSHV_IRQFD_BIT_RESAMPLE) +#define MSHV_IRQFD_BIT_DEASSIGN_FLAG (1 << MSHV_IRQFD_BIT_DEASSIGN) + +static MshvMsiControl *msi_control; +static QemuMutex msi_control_mutex; + +void mshv_init_msicontrol(void) +{ + qemu_mutex_init(&msi_control_mutex); + msi_control =3D g_new0(MshvMsiControl, 1); + msi_control->gsi_routes =3D g_hash_table_new(g_direct_hash, g_direct_e= qual); + msi_control->updated =3D false; +} + +static int set_msi_routing(uint32_t gsi, uint64_t addr, uint32_t data) +{ + struct mshv_user_irq_entry *entry; + uint32_t high_addr =3D addr >> 32; + uint32_t low_addr =3D addr & 0xFFFFFFFF; + GHashTable *gsi_routes; + + trace_mshv_set_msi_routing(gsi, addr, data); + + if (gsi >=3D MSHV_MAX_MSI_ROUTES) { + error_report("gsi >=3D MSHV_MAX_MSI_ROUTES"); + return -1; + } + + assert(msi_control); + + WITH_QEMU_LOCK_GUARD(&msi_control_mutex) { + gsi_routes =3D msi_control->gsi_routes; + entry =3D g_hash_table_lookup(gsi_routes, GINT_TO_POINTER(gsi)); + + if (entry + && entry->address_hi =3D=3D high_addr + && entry->address_lo =3D=3D low_addr + && entry->data =3D=3D data) + { + /* nothing to update */ + return 0; + } + + /* free old entry */ + g_free(entry); + + /* create new entry */ + entry =3D g_new0(mshv_user_irq_entry, 1); + entry->gsi =3D gsi; + entry->address_hi =3D high_addr; + entry->address_lo =3D low_addr; + entry->data =3D data; + + g_hash_table_insert(gsi_routes, GINT_TO_POINTER(gsi), entry); + msi_control->updated =3D true; + } + + return 0; +} + +static int add_msi_routing(uint64_t addr, uint32_t data) +{ + struct mshv_user_irq_entry *route_entry; + uint32_t high_addr =3D addr >> 32; + uint32_t low_addr =3D addr & 0xFFFFFFFF; + int gsi; + GHashTable *gsi_routes; + + trace_mshv_add_msi_routing(addr, data); + + assert(msi_control); + + WITH_QEMU_LOCK_GUARD(&msi_control_mutex) { + /* find an empty slot */ + gsi =3D 0; + gsi_routes =3D msi_control->gsi_routes; + while (gsi < MSHV_MAX_MSI_ROUTES) { + route_entry =3D g_hash_table_lookup(gsi_routes, GINT_TO_POINTE= R(gsi)); + if (!route_entry) { + break; + } + gsi++; + } + if (gsi >=3D MSHV_MAX_MSI_ROUTES) { + error_report("No empty gsi slot available"); + return -1; + } + + /* create new entry */ + route_entry =3D g_new0(struct mshv_user_irq_entry, 1); + route_entry->gsi =3D gsi; + route_entry->address_hi =3D high_addr; + route_entry->address_lo =3D low_addr; + route_entry->data =3D data; + + g_hash_table_insert(gsi_routes, GINT_TO_POINTER(gsi), route_entry); + msi_control->updated =3D true; + } + + return gsi; +} + +static int commit_msi_routing_table(void) +{ + guint len; + int i, ret; + size_t table_size; + struct mshv_user_irq_table *table; + GHashTableIter iter; + gpointer key, value; + int vm_fd =3D mshv_state->vm; + + assert(msi_control); + + WITH_QEMU_LOCK_GUARD(&msi_control_mutex) { + if (!msi_control->updated) { + /* nothing to update */ + return 0; + } + + /* Calculate the size of the table */ + len =3D g_hash_table_size(msi_control->gsi_routes); + table_size =3D sizeof(struct mshv_user_irq_table) + + len * sizeof(struct mshv_user_irq_entry); + table =3D g_malloc0(table_size); + + g_hash_table_iter_init(&iter, msi_control->gsi_routes); + i =3D 0; + while (g_hash_table_iter_next(&iter, &key, &value)) { + struct mshv_user_irq_entry *entry =3D value; + table->entries[i] =3D *entry; + i++; + } + table->nr =3D i; + + trace_mshv_commit_msi_routing_table(vm_fd, len); + + ret =3D ioctl(vm_fd, MSHV_SET_MSI_ROUTING, table); + g_free(table); + if (ret < 0) { + error_report("Failed to commit msi routing table"); + return -1; + } + msi_control->updated =3D false; + } + return 0; +} + +static int remove_msi_routing(uint32_t gsi) +{ + struct mshv_user_irq_entry *route_entry; + GHashTable *gsi_routes; + + trace_mshv_remove_msi_routing(gsi); + + if (gsi >=3D MSHV_MAX_MSI_ROUTES) { + error_report("Invalid GSI: %u", gsi); + return -1; + } + + assert(msi_control); + + WITH_QEMU_LOCK_GUARD(&msi_control_mutex) { + gsi_routes =3D msi_control->gsi_routes; + route_entry =3D g_hash_table_lookup(gsi_routes, GINT_TO_POINTER(gs= i)); + if (route_entry) { + g_hash_table_remove(gsi_routes, GINT_TO_POINTER(gsi)); + g_free(route_entry); + msi_control->updated =3D true; + } + } + + return 0; +} + +/* Pass an eventfd which is to be used for injecting interrupts from userl= and */ +static int irqfd(int vm_fd, int fd, int resample_fd, uint32_t gsi, + uint32_t flags) +{ + int ret; + struct mshv_user_irqfd arg =3D { + .fd =3D fd, + .resamplefd =3D resample_fd, + .gsi =3D gsi, + .flags =3D flags, + }; + + ret =3D ioctl(vm_fd, MSHV_IRQFD, &arg); + if (ret < 0) { + error_report("Failed to set irqfd: gsi=3D%u, fd=3D%d", gsi, fd); + return -1; + } + return ret; +} + +static int register_irqfd(int vm_fd, int event_fd, uint32_t gsi) +{ + int ret; + + trace_mshv_register_irqfd(vm_fd, event_fd, gsi); + + ret =3D irqfd(vm_fd, event_fd, 0, gsi, 0); + if (ret < 0) { + error_report("Failed to register irqfd: gsi=3D%u", gsi); + return -1; + } + return 0; +} + +static int register_irqfd_with_resample(int vm_fd, int event_fd, + int resample_fd, uint32_t gsi) +{ + int ret; + uint32_t flags =3D MSHV_IRQFD_RESAMPLE_FLAG; + + ret =3D irqfd(vm_fd, event_fd, resample_fd, gsi, flags); + if (ret < 0) { + error_report("Failed to register irqfd with resample: gsi=3D%u", g= si); + return -errno; + } + return 0; +} + +static int unregister_irqfd(int vm_fd, int event_fd, uint32_t gsi) +{ + int ret; + uint32_t flags =3D MSHV_IRQFD_BIT_DEASSIGN_FLAG; + + ret =3D irqfd(vm_fd, event_fd, 0, gsi, flags); + if (ret < 0) { + error_report("Failed to unregister irqfd: gsi=3D%u", gsi); + return -errno; + } + return 0; +} + +static int irqchip_update_irqfd_notifier_gsi(const EventNotifier *event, + const EventNotifier *resample, + int virq, bool add) +{ + int fd =3D event_notifier_get_fd(event); + int rfd =3D resample ? event_notifier_get_fd(resample) : -1; + int vm_fd =3D mshv_state->vm; + + trace_mshv_irqchip_update_irqfd_notifier_gsi(fd, rfd, virq, add); + + if (!add) { + return unregister_irqfd(vm_fd, fd, virq); + } + + if (rfd > 0) { + return register_irqfd_with_resample(vm_fd, fd, rfd, virq); + } + + return register_irqfd(vm_fd, fd, virq); +} + + +int mshv_irqchip_add_msi_route(int vector, PCIDevice *dev) +{ + MSIMessage msg =3D { 0, 0 }; + int virq =3D 0; + + if (pci_available && dev) { + msg =3D pci_get_msi_message(dev, vector); + virq =3D add_msi_routing(msg.address, le32_to_cpu(msg.data)); + } + + return virq; +} + +void mshv_irqchip_release_virq(int virq) +{ + remove_msi_routing(virq); +} + +int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev) +{ + int ret; + + ret =3D set_msi_routing(virq, msg.address, le32_to_cpu(msg.data)); + if (ret < 0) { + error_report("Failed to set msi routing"); + return -1; + } + + return 0; +} + +int mshv_request_interrupt(int vm_fd, uint32_t interrupt_type, uint32_t ve= ctor, + uint32_t vp_index, bool logical_dest_mode, + bool level_triggered) +{ + int ret; + + if (vector =3D=3D 0) { + warn_report("Ignoring request for interrupt vector 0"); + return 0; + } + + union hv_interrupt_control control =3D { + .interrupt_type =3D interrupt_type, + .level_triggered =3D level_triggered, + .logical_dest_mode =3D logical_dest_mode, + .rsvd =3D 0, + }; + + struct hv_input_assert_virtual_interrupt arg =3D {0}; + arg.control =3D control; + arg.dest_addr =3D (uint64_t)vp_index; + arg.vector =3D vector; + + struct mshv_root_hvcall args =3D {0}; + args.code =3D HVCALL_ASSERT_VIRTUAL_INTERRUPT; + args.in_sz =3D sizeof(arg); + args.in_ptr =3D (uint64_t)&arg; + + ret =3D mshv_hvcall(vm_fd, &args); + if (ret < 0) { + error_report("Failed to request interrupt"); + return -errno; + } + return 0; +} + +void mshv_irqchip_commit_routes(void) +{ + int ret; + + ret =3D commit_msi_routing_table(); + if (ret < 0) { + error_report("Failed to commit msi routing table"); + abort(); + } +} + +int mshv_irqchip_add_irqfd_notifier_gsi(const EventNotifier *event, + const EventNotifier *resample, + int virq) +{ + return irqchip_update_irqfd_notifier_gsi(event, resample, virq, true); +} + +int mshv_irqchip_remove_irqfd_notifier_gsi(const EventNotifier *event, + int virq) +{ + return irqchip_update_irqfd_notifier_gsi(event, NULL, virq, false); +} diff --git a/accel/mshv/meson.build b/accel/mshv/meson.build index 8a6beb3fb1..f88fc8678c 100644 --- a/accel/mshv/meson.build +++ b/accel/mshv/meson.build @@ -1,5 +1,6 @@ mshv_ss =3D ss.source_set() mshv_ss.add(if_true: files( + 'irq.c', 'mem.c', 'mshv-all.c' )) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index 2ae9d1cffa..9f6dcacf33 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -353,10 +353,8 @@ static MemoryListener mshv_memory_listener =3D { .priority =3D MEMORY_LISTENER_PRIORITY_ACCEL, .region_add =3D mem_region_add, .region_del =3D mem_region_del, -#ifdef MSHV_USE_IOEVENTFD .eventfd_add =3D mem_ioeventfd_add, .eventfd_del =3D mem_ioeventfd_del, -#endif }; =20 static MemoryListener mshv_io_listener =3D { @@ -416,6 +414,8 @@ static int mshv_init(MachineState *ms) return -1; } =20 + mshv_init_msicontrol(); + ret =3D create_vm(mshv_fd, &vm_fd); if (ret < 0) { close(mshv_fd); diff --git a/accel/mshv/trace-events b/accel/mshv/trace-events index b49a5b1702..66f5057caa 100644 --- a/accel/mshv/trace-events +++ b/accel/mshv/trace-events @@ -1,5 +1,6 @@ # See docs/devel/tracing.rst for syntax documentation. =20 +mshv_handle_interrupt(uint32_t cpu, int mask) "cpu_index %d mask %x" mshv_set_memory(bool add, uint64_t gpa, uint64_t size, uint64_t user_addr,= bool readonly, int ret) "[add =3D %d] gpa =3D %lx size =3D %lx user =3D %l= x readonly =3D %d result =3D %d" mshv_mem_ioeventfd_add(uint64_t addr, uint32_t size, uint32_t data) "addr = %lx size %d data %x" mshv_mem_ioeventfd_del(uint64_t addr, uint32_t size, uint32_t data) "addr = %lx size %d data %x" diff --git a/hw/intc/apic.c b/hw/intc/apic.c index bcb103560c..4d1fe7cdd1 100644 --- a/hw/intc/apic.c +++ b/hw/intc/apic.c @@ -27,6 +27,7 @@ #include "hw/pci/msi.h" #include "qemu/host-utils.h" #include "system/kvm.h" +#include "system/mshv.h" #include "trace.h" #include "hw/i386/apic-msidef.h" #include "qapi/error.h" @@ -932,6 +933,14 @@ static void apic_send_msi(MSIMessage *msi) uint8_t trigger_mode =3D (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1; uint8_t delivery =3D (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7; /* XXX: Ignore redirection hint. */ +#ifdef CONFIG_MSHV + if (mshv_enabled()) { + /* TODO: error handling? */ + mshv_request_interrupt(mshv_state->vm, delivery, vector, dest, + dest_mode, trigger_mode); + return; + } +#endif apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode); } =20 diff --git a/include/system/mshv.h b/include/system/mshv.h index c2b0414c85..8a03a89b0c 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -30,13 +30,9 @@ #define CONFIG_MSHV_IS_POSSIBLE #endif =20 -/* - * Set to 0 if we do not want to use eventfd to optimize the MMIO events. - * Set to 1 so that mshv kernel driver receives doorbell when the VM access - * MMIO memory and then signal eventfd to notify the qemu device - * without extra switching to qemu to emulate mmio access. - */ -#define MSHV_USE_IOEVENTFD 1 +typedef struct hyperv_message hv_message; + +#define MSHV_MAX_MSI_ROUTES 4096 =20 #define MSHV_PAGE_SHIFT 12 =20 @@ -70,14 +66,15 @@ struct AccelCPUState { bool dirty; }; =20 +typedef struct MshvMsiControl { + bool updated; + GHashTable *gsi_routes; +} MshvMsiControl; + #else /* CONFIG_MSHV_IS_POSSIBLE */ #define mshv_enabled() false #endif -#ifdef MSHV_USE_KERNEL_GSI_IRQFD #define mshv_msi_via_irqfd_enabled() mshv_enabled() -#else -#define mshv_msi_via_irqfd_enabled() false -#endif =20 /* cpu */ void mshv_arch_amend_proc_features( @@ -100,6 +97,11 @@ void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryR= egionSection *section, bool add); =20 /* interrupt */ +void mshv_init_msicontrol(void); +int mshv_request_interrupt(int vm_fd, uint32_t interrupt_type, uint32_t ve= ctor, + uint32_t vp_index, bool logical_destination_mod= e, + bool level_triggered); + int mshv_irqchip_add_msi_route(int vector, PCIDevice *dev); int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev= ); void mshv_irqchip_commit_routes(void); --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391030; cv=none; d=zohomail.com; s=zohoarc; b=D7Dvnuq2tsYdErAvTNjcy466nUPELOruiQRloohQAe8JgR9G4GHuOk8ojhnsMugibskEVeML3m2UNZxK5BaMO08E/TWyXDZnMLYo6DuBbh0fhiESLzNS4LiVS3cPsrjuUzM36iQw0aFvKhoFGWYXtjMWhJ4cMHFWoMJW4Eg5EZM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391030; 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=ju8qZk0r56f4oIIN4Veu1xJUr1cS2ONnO5h3qfIMJUc=; b=TlHFgENCQeXPOtRy2I5g5ubVMEV0jeS7ln/O+myJ8ZsDWCuKGxnhBTxE1axA25TBQua7Zc6ev49I4JGUgvURZeohcWrHdmsHZ96ppioOZicdM+OeL9TnshcMhYUVEHuc+rkK4ZaRPRvYGUfJNdb5UZdiTIX3661gxtha3YQ+Rv8= 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 1751391030796833.3102099517715; Tue, 1 Jul 2025 10:30:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeo9-0004Np-JI; Tue, 01 Jul 2025 13:30:15 -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 1uWent-0004J0-IF for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:58 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeno-0006tV-Ec for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:57 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 66DAF2112237; Tue, 1 Jul 2025 10:29:47 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 66DAF2112237 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390990; bh=ju8qZk0r56f4oIIN4Veu1xJUr1cS2ONnO5h3qfIMJUc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XWJGH+ziqws6iUukpZhzZDcfEHcYPKoek1JL7FFeDnX+AblbGp9hDhvyV/fYndMZW 46jLHiBbEgQUrVa+jNDcCAqYtWPN5uilbM+4mEtsIVyE+8sR6JVaRfNpsh/OBQoFYN bk7MH4s7NDcehQ4kYAV/ZNHX82wrZZ5lMuRsRTUI= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 12/27] accel/mshv: Add vCPU creation and execution loop Date: Tue, 1 Jul 2025 19:28:19 +0200 Message-Id: <20250701172834.44849-13-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391032050116600 Content-Type: text/plain; charset="utf-8" Create MSHV vCPUs using MSHV_CREATE_VP and initialize their state. Register the MSHV CPU execution loop loop with the QEMU accelerator framework to enable guest code execution. The target/i386 functionality is still mostly stubbed out and will be populated in a later commit in this series. Signed-off-by: Magnus Kulke --- accel/mshv/mshv-all.c | 187 +++++++++++++++++++++++++++++++++--- accel/mshv/trace-events | 1 + include/system/mshv.h | 17 ++++ target/i386/mshv/mshv-cpu.c | 63 ++++++++++++ 4 files changed, 256 insertions(+), 12 deletions(-) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index 9f6dcacf33..04900a2bfe 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -392,6 +392,24 @@ int mshv_hvcall(int vm_fd, const struct mshv_root_hvca= ll *args) return ret; } =20 +static int mshv_init_vcpu(CPUState *cpu) +{ + int vm_fd =3D mshv_state->vm; + uint8_t vp_index =3D cpu->cpu_index; + int ret; + + mshv_arch_init_vcpu(cpu); + cpu->accel =3D g_new0(AccelCPUState, 1); + + ret =3D mshv_create_vcpu(vm_fd, vp_index, &cpu->accel->cpufd); + if (ret < 0) { + return -1; + } + + cpu->accel->dirty =3D true; + + return 0; +} =20 static int mshv_init(MachineState *ms) { @@ -414,6 +432,8 @@ static int mshv_init(MachineState *ms) return -1; } =20 + mshv_init_cpu_logic(); + mshv_init_msicontrol(); =20 ret =3D create_vm(mshv_fd, &vm_fd); @@ -443,40 +463,183 @@ static int mshv_init(MachineState *ms) return 0; } =20 +static int mshv_destroy_vcpu(CPUState *cpu) +{ + int cpu_fd =3D mshv_vcpufd(cpu); + int vm_fd =3D mshv_state->vm; + + mshv_remove_vcpu(vm_fd, cpu_fd); + mshv_vcpufd(cpu) =3D 0; + + mshv_arch_destroy_vcpu(cpu); + g_free(cpu->accel); + return 0; +} + +static int mshv_cpu_exec(CPUState *cpu) +{ + hv_message mshv_msg; + enum MshvVmExit exit_reason; + int ret =3D 0; + + bql_unlock(); + cpu_exec_start(cpu); + + do { + if (cpu->accel->dirty) { + ret =3D mshv_arch_put_registers(cpu); + if (ret) { + error_report("Failed to put registers after init: %s", + strerror(-ret)); + ret =3D -1; + break; + } + cpu->accel->dirty =3D false; + } + + ret =3D mshv_run_vcpu(mshv_state->vm, cpu, &mshv_msg, &exit_reason= ); + if (ret < 0) { + error_report("Failed to run on vcpu %d", cpu->cpu_index); + abort(); + } + + switch (exit_reason) { + case MshvVmExitIgnore: + break; + default: + ret =3D EXCP_INTERRUPT; + break; + } + } while (ret =3D=3D 0); + + cpu_exec_end(cpu); + bql_lock(); + + if (ret < 0) { + cpu_dump_state(cpu, stderr, CPU_DUMP_CODE); + vm_stop(RUN_STATE_INTERNAL_ERROR); + } + + qatomic_set(&cpu->exit_request, 0); + return ret; +} + +static void *mshv_vcpu_thread(void *arg) +{ + CPUState *cpu =3D arg; + int ret; + + rcu_register_thread(); + + bql_lock(); + qemu_thread_get_self(cpu->thread); + cpu->thread_id =3D qemu_get_thread_id(); + current_cpu =3D cpu; + ret =3D mshv_init_vcpu(cpu); + if (ret < 0) { + error_report("Failed to init vcpu %d", cpu->cpu_index); + goto cleanup; + } + + /* signal CPU creation */ + cpu_thread_signal_created(cpu); + qemu_guest_random_seed_thread_part2(cpu->random_seed); + + do { + if (cpu_can_run(cpu)) { + mshv_cpu_exec(cpu); + } + qemu_wait_io_event(cpu); + } while (!cpu->unplug || cpu_can_run(cpu)); + + mshv_destroy_vcpu(cpu); +cleanup: + cpu_thread_signal_destroyed(cpu); + bql_unlock(); + rcu_unregister_thread(); + return NULL; +} + static void mshv_start_vcpu_thread(CPUState *cpu) { - error_report("unimplemented"); - abort(); + char thread_name[VCPU_THREAD_NAME_SIZE]; + + cpu->thread =3D g_malloc0(sizeof(QemuThread)); + cpu->halt_cond =3D g_malloc0(sizeof(QemuCond)); + + qemu_cond_init(cpu->halt_cond); + + trace_mshv_start_vcpu_thread(thread_name, cpu->cpu_index); + qemu_thread_create(cpu->thread, thread_name, mshv_vcpu_thread, cpu, + QEMU_THREAD_JOINABLE); +} + +static void do_mshv_cpu_synchronize_post_init(CPUState *cpu, + run_on_cpu_data arg) +{ + int ret =3D mshv_arch_put_registers(cpu); + if (ret < 0) { + error_report("Failed to put registers after init: %s", strerror(-r= et)); + abort(); + } + + cpu->accel->dirty =3D false; } =20 static void mshv_cpu_synchronize_post_init(CPUState *cpu) { - error_report("unimplemented"); - abort(); + run_on_cpu(cpu, do_mshv_cpu_synchronize_post_init, RUN_ON_CPU_NULL); } =20 static void mshv_cpu_synchronize_post_reset(CPUState *cpu) { - error_report("unimplemented"); - abort(); + int ret =3D mshv_arch_put_registers(cpu); + if (ret) { + error_report("Failed to put registers after reset: %s", + strerror(-ret)); + cpu_dump_state(cpu, stderr, CPU_DUMP_CODE); + vm_stop(RUN_STATE_INTERNAL_ERROR); + } + cpu->accel->dirty =3D false; +} + +static void do_mshv_cpu_synchronize_pre_loadvm(CPUState *cpu, + run_on_cpu_data arg) +{ + cpu->accel->dirty =3D true; } =20 static void mshv_cpu_synchronize_pre_loadvm(CPUState *cpu) { - error_report("unimplemented"); - abort(); + run_on_cpu(cpu, do_mshv_cpu_synchronize_pre_loadvm, RUN_ON_CPU_NULL); +} + +static void do_mshv_cpu_synchronize(CPUState *cpu, run_on_cpu_data arg) +{ + if (!cpu->accel->dirty) { + int ret =3D mshv_load_regs(cpu); + if (ret < 0) { + error_report("Failed to load registers for vcpu %d", + cpu->cpu_index); + + cpu_dump_state(cpu, stderr, CPU_DUMP_CODE); + vm_stop(RUN_STATE_INTERNAL_ERROR); + } + + cpu->accel->dirty =3D true; + } } =20 static void mshv_cpu_synchronize(CPUState *cpu) { - error_report("unimplemented"); - abort(); + if (!cpu->accel->dirty) { + run_on_cpu(cpu, do_mshv_cpu_synchronize, RUN_ON_CPU_NULL); + } } =20 static bool mshv_cpus_are_resettable(void) { - error_report("unimplemented"); - abort(); + return false; } =20 static void mshv_accel_class_init(ObjectClass *oc, const void *data) diff --git a/accel/mshv/trace-events b/accel/mshv/trace-events index 66f5057caa..bade57e22c 100644 --- a/accel/mshv/trace-events +++ b/accel/mshv/trace-events @@ -1,5 +1,6 @@ # See docs/devel/tracing.rst for syntax documentation. =20 +mshv_start_vcpu_thread(const char* thread, uint32_t cpu) "thread %s cpu_in= dex %d" mshv_handle_interrupt(uint32_t cpu, int mask) "cpu_index %d mask %x" mshv_set_memory(bool add, uint64_t gpa, uint64_t size, uint64_t user_addr,= bool readonly, int ret) "[add =3D %d] gpa =3D %lx size =3D %lx user =3D %l= x readonly =3D %d result =3D %d" mshv_mem_ioeventfd_add(uint64_t addr, uint32_t size, uint32_t data) "addr = %lx size %d data %x" diff --git a/include/system/mshv.h b/include/system/mshv.h index 8a03a89b0c..0ff19d4eb5 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -71,12 +71,29 @@ typedef struct MshvMsiControl { GHashTable *gsi_routes; } MshvMsiControl; =20 +#define mshv_vcpufd(cpu) (cpu->accel->cpufd) + #else /* CONFIG_MSHV_IS_POSSIBLE */ #define mshv_enabled() false #endif #define mshv_msi_via_irqfd_enabled() mshv_enabled() =20 /* cpu */ +typedef enum MshvVmExit { + MshvVmExitIgnore =3D 0, + MshvVmExitShutdown =3D 1, + MshvVmExitSpecial =3D 2, +} MshvVmExit; + +void mshv_init_cpu_logic(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); +int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit); +int mshv_load_regs(CPUState *cpu); +int mshv_store_regs(CPUState *cpu); +int mshv_arch_put_registers(const CPUState *cpu); +void mshv_arch_init_vcpu(CPUState *cpu); +void mshv_arch_destroy_vcpu(CPUState *cpu); void mshv_arch_amend_proc_features( union hv_partition_synthetic_processor_features *features); int mshv_arch_post_init_vm(int vm_fd); diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index c00e98dfba..2fe5319201 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -20,16 +20,79 @@ #include "hw/hyperv/hvhdk_mini.h" #include "hw/hyperv/hvgdk.h" =20 +#include "cpu.h" +#include "emulate/x86_decode.h" +#include "emulate/x86_emu.h" +#include "emulate/x86_flags.h" =20 #include "trace-accel_mshv.h" #include "trace.h" =20 +int mshv_store_regs(CPUState *cpu) +{ + error_report("unimplemented"); + abort(); +} + +int mshv_load_regs(CPUState *cpu) +{ + error_report("unimplemented"); + abort(); +} + +int mshv_arch_put_registers(const CPUState *cpu) +{ + error_report("unimplemented"); + abort(); +} + void mshv_arch_amend_proc_features( union hv_partition_synthetic_processor_features *features) { features->access_guest_idle_reg =3D 1; } =20 +int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit) +{ + error_report("unimplemented"); + abort(); +} + +void mshv_remove_vcpu(int vm_fd, int cpu_fd) +{ + error_report("unimplemented"); + abort(); +} + +int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int *cpu_fd) +{ + error_report("unimplemented"); + abort(); +} + +void mshv_init_cpu_logic(void) +{ + error_report("unimplemented"); + abort(); +} + +void mshv_arch_init_vcpu(CPUState *cpu) +{ + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + + env->emu_mmio_buf =3D g_new(char, 4096); +} + +void mshv_arch_destroy_vcpu(CPUState *cpu) +{ + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + + g_free(env->emu_mmio_buf); + env->emu_mmio_buf =3D NULL; +} + /* * Default Microsoft Hypervisor behavior for unimplemented MSR is to send a * fault to the guest if it tries to access it. It is possible to override --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391230; cv=none; d=zohomail.com; s=zohoarc; b=WsLguWy3Izoljg7VgjfnNNVRU9mFtSQJtRog9KSyljc5k5rcVGsPj6kD+iySEtJsQ4X7m6z9aBdHJ2wTEqHbATxOXUeRncQcMVpP2AOpM0VCUiPIhavYCMXzAK0SKBoVtBFoIZHHAni05k3wRzaq+z4bTUD2hs2PZpCxSRt8srg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391230; 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=HKF4J8hoT7FC44YXYO93SNqvkBKB3tZSrogS2AjQ5UE=; b=DQ2TJXJ9Okfbz3anHTnyNL7qbDKMFqZYQzfQann+uEr7s9iF6UBjxE8gLwzjf0O5bv0I/lfqQgpFXJEvF1w9Sw/Ql+4wmxhA3O5GvOqK+8MRIP+yTUgL/jtMK9tA2zAeIXd3cK2rcmj1LQ7AxbpWThzT4vnZrS8gtUr6W2XU97k= 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 1751391229997232.10515076868865; Tue, 1 Jul 2025 10:33:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoK-0004r0-1f; Tue, 01 Jul 2025 13:30:24 -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 1uWenu-0004JZ-Qy for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:59 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWens-0006w1-VL for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:29:58 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 644CE211223A; Tue, 1 Jul 2025 10:29:51 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 644CE211223A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390994; bh=HKF4J8hoT7FC44YXYO93SNqvkBKB3tZSrogS2AjQ5UE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Lvv5edfUjTTtU4K6roulYYhoy5FVWxEiiYKukTw5ZQ/JqxkXi8VyrvGw8Vbm10VJM GWsDb6pcfoDtm0s7dVKaxbMe1o1+gBuUIvKuKh8eTahvy1rW58UKy8dW3Tnj/gPqVZ 8QFL1XkvQWhrTfu1XWWME5t4PhP1hS0iVEY1hVpA= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 13/27] accel/mshv: Add vCPU signal handling Date: Tue, 1 Jul 2025 19:28:20 +0200 Message-Id: <20250701172834.44849-14-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391232346116600 Content-Type: text/plain; charset="utf-8" Implement signal handling for MSHV vCPUs to support asynchronous interrupts from the main thread. Signed-off-by: Magnus Kulke --- accel/mshv/mshv-all.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index 04900a2bfe..d95893806f 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -524,6 +524,35 @@ static int mshv_cpu_exec(CPUState *cpu) return ret; } =20 +/* + * The signal handler is triggered when QEMU's main thread receives a SIG_= IPI + * (SIGUSR1). This signal causes the current CPU thread to be kicked, forc= ing a + * VM exit on the CPU. The VM exit generates an exit reason that breaks th= e loop + * (see mshv_cpu_exec). If the exit is due to a Ctrl+A+x command, the syst= em + * will shut down. For other cases, the system will continue running. + */ +static void sa_ipi_handler(int sig) +{ + /* TODO: call IOCTL to set_immediate_exit, once implemented. */ + + qemu_cpu_kick_self(); +} + +static void init_signal(CPUState *cpu) +{ + /* init cpu signals */ + struct sigaction sigact; + sigset_t set; + + memset(&sigact, 0, sizeof(sigact)); + sigact.sa_handler =3D sa_ipi_handler; + sigaction(SIG_IPI, &sigact, NULL); + + pthread_sigmask(SIG_BLOCK, NULL, &set); + sigdelset(&set, SIG_IPI); + pthread_sigmask(SIG_SETMASK, &set, NULL); +} + static void *mshv_vcpu_thread(void *arg) { CPUState *cpu =3D arg; @@ -540,6 +569,7 @@ static void *mshv_vcpu_thread(void *arg) error_report("Failed to init vcpu %d", cpu->cpu_index); goto cleanup; } + init_signal(cpu); =20 /* signal CPU creation */ cpu_thread_signal_created(cpu); --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391047; cv=none; d=zohomail.com; s=zohoarc; b=AXD9T5v7jRng+3HXPCcQKVLfVDn7oais/oaIfm7qJyL/b2J9hwjRL0jGV7iFJFAVGM/hLDplHghF8FFgbpOSsZ78unCU7clGn70v8x1kkYGU2ydVXVVUYhDxSDm9YpRviDV3Cmqvkfa2bt87rpWjoqctsbZbrPOEQ4yeGQep6mE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391047; 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=Nt4UBWXes3AxB90SryE8zirrNwQ3P5gh+k2XSafdH4c=; b=DevL+zIGLIuduV8c2X7y+FxjxZE9WZmP5Zg3dyviHYYQf3XsLmAZsqV3IgvN06bfrQBR8/gNs3lazyrlhdirTfO5MK9k3SGRk7mzv40mKdR8CT1VNdIk3K8Ih/Mg5YGTsjStgPuAPQWNreE28fCQvhaJnVXzCBKbWo64B2I+i/w= 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 1751391047741346.1994016334328; Tue, 1 Jul 2025 10:30:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoM-00052u-Bs; Tue, 01 Jul 2025 13:30:26 -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 1uWenx-0004OF-IG for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:07 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWenv-0006xf-Rs for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:01 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 63C2D2112238; Tue, 1 Jul 2025 10:29:55 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 63C2D2112238 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751390998; bh=Nt4UBWXes3AxB90SryE8zirrNwQ3P5gh+k2XSafdH4c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jE4X1C5OZB2A60Iyva1AwZaDd2FnsRZvqOs53OEToOEv/3nbat3dbeZRXxigxURLJ jP4nv/baG82dEqCwxYz61UwbCOTQg8vZPWxozCQ8k0BMC6194pspwSH1iQd0/kbvMy v85xI/StrYGtT/4JR7A28SUnCYzVCfsxIlq+g3WQ= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 14/27] target/i386/mshv: Add CPU create and remove logic Date: Tue, 1 Jul 2025 19:28:21 +0200 Message-Id: <20250701172834.44849-15-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391049852116600 Content-Type: text/plain; charset="utf-8" Implement MSHV-specific hooks for vCPU creation and teardown in the i386 target. Signed-off-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 2fe5319201..7a6965d7fb 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -28,6 +28,8 @@ #include "trace-accel_mshv.h" #include "trace.h" =20 +#include + int mshv_store_regs(CPUState *cpu) { error_report("unimplemented"); @@ -60,20 +62,29 @@ 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(); + close(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; + } + + *cpu_fd =3D ret; + + return 0; } =20 void mshv_init_cpu_logic(void) { - error_report("unimplemented"); - abort(); } =20 void mshv_arch_init_vcpu(CPUState *cpu) --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391194; cv=none; d=zohomail.com; s=zohoarc; b=gTr/7bo8t5xjyljuiCsmnaWvj213qUWkAXnXRfQeeXp54FBhjYrBtExhxBjmSB/t0ZronFA49/uDSmMIXKErDSFDXFG4Utz/hm/q6UFI0fMl16uGTBvUaeVapSLIyAZRZyqTjus98JMuIPWUhPEdWnnOFtfuQbHVfpeJCmW+nC0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391194; 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=g4T8hjfmlC+E5NLakuoBJUixqAkV+uy2hfu/eDEiWoU=; b=cjOM5wfGl0g2XmhS2mo7mVhvQOuYfRidF2O2C1RsUsO6l0icdwXa6BU3gHESJKAtbMYON/w6ADwtpEyRq5ISwWw6DWKvyyTqr85zwF97HgOXxFJE+9YNJpnZATRaD2jXhpU4MS3EAWareR5o6q97y4RDPnpp9cEkNoRhZcvmwlo= 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 175139119411173.89643696912981; Tue, 1 Jul 2025 10:33:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoO-0005B8-3i; Tue, 01 Jul 2025 13:30:28 -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 1uWeo7-0004ae-OR for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:15 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeo1-00074k-Jx for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:11 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 5ED352112239; Tue, 1 Jul 2025 10:29:59 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5ED352112239 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391002; bh=g4T8hjfmlC+E5NLakuoBJUixqAkV+uy2hfu/eDEiWoU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P1uXv4oNsmV7+ALwoLQn4gekK2KOKzQWQWZ3KRA2hsq+mkzEhyuiKj1jyF4/gjUU+ 9ogS7xCmcjXLl0sgKjkU/0EP33TYW5WQ35Z24PB0BBxlKpzcBuSM16KZgqGW0uFqqX c64t5+79pcqzqs4xbEYuE4Gd/GFmj4zKJt2Qthls= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 15/27] target/i386/mshv: Implement mshv_store_regs() Date: Tue, 1 Jul 2025 19:28:22 +0200 Message-Id: <20250701172834.44849-16-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391195960116600 Content-Type: text/plain; charset="utf-8" Add support for writing general-purpose registers to MSHV vCPUs during initialization or migration using the MSHV register interface. A generic set_register call is introduced to abstract the HV call over the various register types. Signed-off-by: Magnus Kulke --- include/system/mshv.h | 1 + target/i386/mshv/mshv-cpu.c | 84 ++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/include/system/mshv.h b/include/system/mshv.h index 0ff19d4eb5..adce4153d9 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -91,6 +91,7 @@ void mshv_remove_vcpu(int vm_fd, int cpu_fd); int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit); int mshv_load_regs(CPUState *cpu); int mshv_store_regs(CPUState *cpu); +int mshv_set_generic_regs(int cpu_fd, hv_register_assoc *assocs, size_t n_= regs); int mshv_arch_put_registers(const CPUState *cpu); void mshv_arch_init_vcpu(CPUState *cpu); void mshv_arch_destroy_vcpu(CPUState *cpu); diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 7a6965d7fb..4bd4e29b72 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -30,12 +30,92 @@ =20 #include =20 +static enum hv_register_name STANDARD_REGISTER_NAMES[18] =3D { + HV_X64_REGISTER_RAX, + HV_X64_REGISTER_RBX, + HV_X64_REGISTER_RCX, + HV_X64_REGISTER_RDX, + HV_X64_REGISTER_RSI, + HV_X64_REGISTER_RDI, + HV_X64_REGISTER_RSP, + HV_X64_REGISTER_RBP, + HV_X64_REGISTER_R8, + HV_X64_REGISTER_R9, + HV_X64_REGISTER_R10, + HV_X64_REGISTER_R11, + HV_X64_REGISTER_R12, + HV_X64_REGISTER_R13, + HV_X64_REGISTER_R14, + HV_X64_REGISTER_R15, + HV_X64_REGISTER_RIP, + HV_X64_REGISTER_RFLAGS, +}; + +int mshv_set_generic_regs(int cpu_fd, hv_register_assoc *assocs, size_t n_= regs) +{ + struct mshv_vp_registers input =3D { + .count =3D n_regs, + .regs =3D assocs, + }; + + return ioctl(cpu_fd, MSHV_SET_VP_REGISTERS, &input); +} + +static int set_standard_regs(const CPUState *cpu) +{ + X86CPU *x86cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86cpu->env; + hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)]; + int ret; + int cpu_fd =3D mshv_vcpufd(cpu); + size_t n_regs =3D ARRAY_SIZE(STANDARD_REGISTER_NAMES); + + /* set names */ + for (size_t i =3D 0; i < ARRAY_SIZE(STANDARD_REGISTER_NAMES); i++) { + assocs[i].name =3D STANDARD_REGISTER_NAMES[i]; + } + assocs[0].value.reg64 =3D env->regs[R_EAX]; + assocs[1].value.reg64 =3D env->regs[R_EBX]; + assocs[2].value.reg64 =3D env->regs[R_ECX]; + assocs[3].value.reg64 =3D env->regs[R_EDX]; + assocs[4].value.reg64 =3D env->regs[R_ESI]; + assocs[5].value.reg64 =3D env->regs[R_EDI]; + assocs[6].value.reg64 =3D env->regs[R_ESP]; + assocs[7].value.reg64 =3D env->regs[R_EBP]; + assocs[8].value.reg64 =3D env->regs[R_R8]; + assocs[9].value.reg64 =3D env->regs[R_R9]; + assocs[10].value.reg64 =3D env->regs[R_R10]; + assocs[11].value.reg64 =3D env->regs[R_R11]; + assocs[12].value.reg64 =3D env->regs[R_R12]; + assocs[13].value.reg64 =3D env->regs[R_R13]; + assocs[14].value.reg64 =3D env->regs[R_R14]; + assocs[15].value.reg64 =3D env->regs[R_R15]; + assocs[16].value.reg64 =3D env->eip; + lflags_to_rflags(env); + assocs[17].value.reg64 =3D env->eflags; + + ret =3D mshv_set_generic_regs(cpu_fd, assocs, n_regs); + if (ret < 0) { + error_report("failed to set standard registers"); + return -errno; + } + return 0; +} + int mshv_store_regs(CPUState *cpu) { - error_report("unimplemented"); - abort(); + int ret; + + ret =3D set_standard_regs(cpu); + if (ret < 0) { + error_report("Failed to store standard registers"); + return -1; + } + + return 0; } =20 + int mshv_load_regs(CPUState *cpu) { error_report("unimplemented"); --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391248; cv=none; d=zohomail.com; s=zohoarc; b=ftwOZzzFGhQKOywtMO7f0JbLkYyRI5xDt0iQU6IbkI/PUXVMydThLIC6XNMURFrL5DH3Iy6ZW6CnG8qtSsC3V3vJJ0uYpurdljKo7KO8T0/Q1BUQgUJjribh4MNP11XEnqxcvUPZhnc9KOp5EWoflUkTPcaTsI3Js/o1+b+2k7Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391248; 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=C+7n42woL/2+vR6WztTsm5hGebodSG3pZWyoUwEkXhA=; b=GC7bLR9g1c/gyE9E2z9BPJ5gehIn85Vm208Xa2YmJf72draiIiFtoxEC68QlgCm1ApYc7LfmJLZFfLyyM1hB5fZ4roVEJA9KeQtwZohwBKHUi6wjTp5/iJVZKVvJE+G3OWUSEUYfnoyMP111lmUpLT5dpg64yCPVXo9iugFZnB0= 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 1751391248126274.0434046011068; Tue, 1 Jul 2025 10:34:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoM-00053K-F7; Tue, 01 Jul 2025 13:30:26 -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 1uWeoJ-0004qY-QD for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:23 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoF-0007CK-Dn for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:22 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 5AEDE211223E; Tue, 1 Jul 2025 10:30:03 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5AEDE211223E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391007; bh=C+7n42woL/2+vR6WztTsm5hGebodSG3pZWyoUwEkXhA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Rat76WuM0Nhji7YcwgK8wNL1O2t2VWAGjtiYFxMgiIMSM4gGj7/xCtxPdBNbhTHYW L8r76aumOPgZkJf4YjHUWiE5+XyXGG7D4kl2uP7oynVBJh6OUiTcXkkfO56PmidNiD xhPkWZl3yPkh0i1i/9eXiR9twANow2cM1Flho2lQ= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 16/27] target/i386/mshv: Implement mshv_get_standard_regs() Date: Tue, 1 Jul 2025 19:28:23 +0200 Message-Id: <20250701172834.44849-17-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391248453116600 Content-Type: text/plain; charset="utf-8" Fetch standard register state from MSHV vCPUs to support debugging, migration, and other introspection features in QEMU. Fetch standard register state from a MHSV vCPU's. A generic get_regs() function and a mapper to map the different register representations are introduced. Signed-off-by: Magnus Kulke --- include/system/mshv.h | 1 + target/i386/mshv/mshv-cpu.c | 69 +++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/include/system/mshv.h b/include/system/mshv.h index adce4153d9..65f7fa15a0 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -88,6 +88,7 @@ typedef enum MshvVmExit { void mshv_init_cpu_logic(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); +int mshv_get_standard_regs(CPUState *cpu); int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit); int mshv_load_regs(CPUState *cpu); int mshv_store_regs(CPUState *cpu); diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 4bd4e29b72..cb59d74eb4 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -61,6 +61,18 @@ int mshv_set_generic_regs(int cpu_fd, hv_register_assoc = *assocs, size_t n_regs) return ioctl(cpu_fd, MSHV_SET_VP_REGISTERS, &input); } =20 +static int get_generic_regs(int cpu_fd, struct hv_register_assoc *assocs, + size_t n_regs) +{ + struct mshv_vp_registers input =3D { + .count =3D n_regs, + .regs =3D assocs, + }; + + return ioctl(cpu_fd, MSHV_GET_VP_REGISTERS, &input); +} + + static int set_standard_regs(const CPUState *cpu) { X86CPU *x86cpu =3D X86_CPU(cpu); @@ -115,11 +127,64 @@ int mshv_store_regs(CPUState *cpu) return 0; } =20 +static void populate_standard_regs(const hv_register_assoc *assocs, + CPUX86State *env) +{ + env->regs[R_EAX] =3D assocs[0].value.reg64; + env->regs[R_EBX] =3D assocs[1].value.reg64; + env->regs[R_ECX] =3D assocs[2].value.reg64; + env->regs[R_EDX] =3D assocs[3].value.reg64; + env->regs[R_ESI] =3D assocs[4].value.reg64; + env->regs[R_EDI] =3D assocs[5].value.reg64; + env->regs[R_ESP] =3D assocs[6].value.reg64; + env->regs[R_EBP] =3D assocs[7].value.reg64; + env->regs[R_R8] =3D assocs[8].value.reg64; + env->regs[R_R9] =3D assocs[9].value.reg64; + env->regs[R_R10] =3D assocs[10].value.reg64; + env->regs[R_R11] =3D assocs[11].value.reg64; + env->regs[R_R12] =3D assocs[12].value.reg64; + env->regs[R_R13] =3D assocs[13].value.reg64; + env->regs[R_R14] =3D assocs[14].value.reg64; + env->regs[R_R15] =3D assocs[15].value.reg64; + + env->eip =3D assocs[16].value.reg64; + env->eflags =3D assocs[17].value.reg64; + rflags_to_lflags(env); +} + +int mshv_get_standard_regs(CPUState *cpu) +{ + struct hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)]; + int ret; + X86CPU *x86cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86cpu->env; + int cpu_fd =3D mshv_vcpufd(cpu); + size_t n_regs =3D ARRAY_SIZE(STANDARD_REGISTER_NAMES); + + for (size_t i =3D 0; i < n_regs; i++) { + assocs[i].name =3D STANDARD_REGISTER_NAMES[i]; + } + ret =3D get_generic_regs(cpu_fd, assocs, n_regs); + if (ret < 0) { + error_report("failed to get standard registers"); + return -1; + } + + populate_standard_regs(assocs, env); + return 0; +} =20 int mshv_load_regs(CPUState *cpu) { - error_report("unimplemented"); - abort(); + int ret; + + ret =3D mshv_get_standard_regs(cpu); + if (ret < 0) { + error_report("Failed to load standard registers"); + return -1; + } + + return 0; } =20 int mshv_arch_put_registers(const CPUState *cpu) --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391075; cv=none; d=zohomail.com; s=zohoarc; b=agoF6EedwpwolGLTgF7unPo0vWNcUQN0kOLjAhT/kOscviii1FtXedIrQHjghq00geExJUlKVYJXU+/IL+KpCQ1fdUpOrgNIXzWT8nLpGr4rZPV/OLl08q9wQ5ZAts0gBiMI+OxPpNCpm/Bn8ni+sbTkla1+jS/+JIy947/JzMg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391075; 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=c9G5jqgbSCxpruLnLGaQ/ULvofCIGPDnFonvhQCzbgE=; b=Bnpk5xZYa7QRqKiA3NRPqMWEFW8WEdLuqQfp/5G0LIOx+lLXQcq3DX465Wa1SZloEmZ/HDQ9S5lkrn2htIIZhcvB5jaBlm7Lvrj8PXpZp6d/UKX1yTBLFziAGnt4S/LCe3/+YqFzrEqRW2vlSl0mx63ACdyEvotD9/c6s9bas+A= 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 1751391075966608.6754514264243; Tue, 1 Jul 2025 10:31:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoP-0005Dd-IE; Tue, 01 Jul 2025 13:30:29 -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 1uWeoM-00052g-71 for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:26 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoJ-0007DO-Nq for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:25 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 759112119388; Tue, 1 Jul 2025 10:30:07 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 759112119388 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391011; bh=c9G5jqgbSCxpruLnLGaQ/ULvofCIGPDnFonvhQCzbgE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bV9ScnJehozLtJrX3hLSJGPfkqjp5PgBdFZaUgZd/RUZ6PZaQf3ijESSESjY8/ThO e536Iq5O1TJ0xGvycwEwQrIPbii93BJvx8OxRU7sgZgG+b+YK4IcZ1hY8H2QfplFjj Xu08f0o3fA6sGylAJIHlH7O59yzw2oBQMKczEaMw= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 17/27] target/i386/mshv: Implement mshv_get_special_regs() Date: Tue, 1 Jul 2025 19:28:24 +0200 Message-Id: <20250701172834.44849-18-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391078255116600 Content-Type: text/plain; charset="utf-8" Retrieve special registers (e.g. segment, control, and descriptor table registers) from MSHV vCPUs. Various helper functions to map register state representations between Qemu and MSHV are introduced. Signed-off-by: Magnus Kulke --- include/system/mshv.h | 1 + target/i386/mshv/mshv-cpu.c | 105 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/include/system/mshv.h b/include/system/mshv.h index 65f7fa15a0..7d0fed3c42 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -89,6 +89,7 @@ void mshv_init_cpu_logic(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); int mshv_get_standard_regs(CPUState *cpu); +int mshv_get_special_regs(CPUState *cpu); int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit); int mshv_load_regs(CPUState *cpu); int mshv_store_regs(CPUState *cpu); diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index cb59d74eb4..53b6722af4 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -51,6 +51,26 @@ static enum hv_register_name STANDARD_REGISTER_NAMES[18]= =3D { HV_X64_REGISTER_RFLAGS, }; =20 +static enum hv_register_name SPECIAL_REGISTER_NAMES[17] =3D { + HV_X64_REGISTER_CS, + HV_X64_REGISTER_DS, + HV_X64_REGISTER_ES, + HV_X64_REGISTER_FS, + HV_X64_REGISTER_GS, + HV_X64_REGISTER_SS, + HV_X64_REGISTER_TR, + HV_X64_REGISTER_LDTR, + HV_X64_REGISTER_GDTR, + HV_X64_REGISTER_IDTR, + HV_X64_REGISTER_CR0, + HV_X64_REGISTER_CR2, + HV_X64_REGISTER_CR3, + HV_X64_REGISTER_CR4, + HV_X64_REGISTER_CR8, + HV_X64_REGISTER_EFER, + HV_X64_REGISTER_APIC_BASE, +}; + int mshv_set_generic_regs(int cpu_fd, hv_register_assoc *assocs, size_t n_= regs) { struct mshv_vp_registers input =3D { @@ -174,6 +194,85 @@ int mshv_get_standard_regs(CPUState *cpu) return 0; } =20 +static inline void populate_segment_reg(const hv_x64_segment_register *hv_= seg, + SegmentCache *seg) +{ + memset(seg, 0, sizeof(SegmentCache)); + + seg->base =3D hv_seg->base; + seg->limit =3D hv_seg->limit; + seg->selector =3D hv_seg->selector; + + seg->flags =3D (hv_seg->segment_type << DESC_TYPE_SHIFT) + | (hv_seg->present * DESC_P_MASK) + | (hv_seg->descriptor_privilege_level << DESC_DPL_SHIFT) + | (hv_seg->_default << DESC_B_SHIFT) + | (hv_seg->non_system_segment * DESC_S_MASK) + | (hv_seg->_long << DESC_L_SHIFT) + | (hv_seg->granularity * DESC_G_MASK) + | (hv_seg->available * DESC_AVL_MASK); + +} + +static inline void populate_table_reg(const hv_x64_table_register *hv_seg, + SegmentCache *tbl) +{ + memset(tbl, 0, sizeof(SegmentCache)); + + tbl->base =3D hv_seg->base; + tbl->limit =3D hv_seg->limit; +} + +static void populate_special_regs(const hv_register_assoc *assocs, + X86CPU *x86cpu) +{ + CPUX86State *env =3D &x86cpu->env; + + populate_segment_reg(&assocs[0].value.segment, &env->segs[R_CS]); + populate_segment_reg(&assocs[1].value.segment, &env->segs[R_DS]); + populate_segment_reg(&assocs[2].value.segment, &env->segs[R_ES]); + populate_segment_reg(&assocs[3].value.segment, &env->segs[R_FS]); + populate_segment_reg(&assocs[4].value.segment, &env->segs[R_GS]); + populate_segment_reg(&assocs[5].value.segment, &env->segs[R_SS]); + + populate_segment_reg(&assocs[6].value.segment, &env->tr); + populate_segment_reg(&assocs[7].value.segment, &env->ldt); + + populate_table_reg(&assocs[8].value.table, &env->gdt); + populate_table_reg(&assocs[9].value.table, &env->idt); + + env->cr[0] =3D assocs[10].value.reg64; + env->cr[2] =3D assocs[11].value.reg64; + env->cr[3] =3D assocs[12].value.reg64; + env->cr[4] =3D assocs[13].value.reg64; + + cpu_set_apic_tpr(x86cpu->apic_state, assocs[14].value.reg64); + env->efer =3D assocs[15].value.reg64; + cpu_set_apic_base(x86cpu->apic_state, assocs[16].value.reg64); +} + + +int mshv_get_special_regs(CPUState *cpu) +{ + struct hv_register_assoc assocs[ARRAY_SIZE(SPECIAL_REGISTER_NAMES)]; + int ret; + X86CPU *x86cpu =3D X86_CPU(cpu); + int cpu_fd =3D mshv_vcpufd(cpu); + size_t n_regs =3D ARRAY_SIZE(SPECIAL_REGISTER_NAMES); + + for (size_t i =3D 0; i < n_regs; i++) { + assocs[i].name =3D SPECIAL_REGISTER_NAMES[i]; + } + ret =3D get_generic_regs(cpu_fd, assocs, n_regs); + if (ret < 0) { + error_report("failed to get special registers"); + return -errno; + } + + populate_special_regs(assocs, x86cpu); + return 0; +} + int mshv_load_regs(CPUState *cpu) { int ret; @@ -184,6 +283,12 @@ int mshv_load_regs(CPUState *cpu) return -1; } =20 + ret =3D mshv_get_special_regs(cpu); + if (ret < 0) { + error_report("Failed to load special registers"); + return -1; + } + return 0; } =20 --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391446; cv=none; d=zohomail.com; s=zohoarc; b=nx9bc7PWsoQ59mjYYPgTuqrS5zj7qTLu/9mrEwGyOdqVXsvGddH15jCp0msgWpNm4FgoCYI05HgBV1s8NGAOpinYho3XkWn/ywH8osYMH+0T+0Mb4pVyqeLBzHuthbKX3wUhid4Cuno4RxSpGlQmHvYBqU6EDxQ3ZlLXN33JodI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391446; 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=Hv1A0/BBJCH+ZaazKE5WAuyjLHQQRvK8zfN+/ltaJiw=; b=YnKwrdABqhNB0DlNZO275pHz1Yd+rr5rBp2jsLm222azkez5cUKbNlDHkK1d5MjCOZL8m4Qi+HGxEHZk6R6yaYtheYruWEsQ/qe4Cvyy9tFkPKWsWGw58I5Rfno6zKb+oK+vswV6jnBEbpLP+x24IdZPMJrFLTcSK5Co1BzSC6w= 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 1751391446601647.4359737071808; Tue, 1 Jul 2025 10:37:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoT-0005XR-Q2; Tue, 01 Jul 2025 13:30:33 -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 1uWeoR-0005Ox-7y for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:31 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoM-0007Ef-2Z for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:30 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 73DE7211938A; Tue, 1 Jul 2025 10:30:11 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 73DE7211938A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391015; bh=Hv1A0/BBJCH+ZaazKE5WAuyjLHQQRvK8zfN+/ltaJiw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m8bq0V2yBV9kaT7YWQCKRpf2ss5DP/XtZcxnLGbJXsSFmZecupNL57ep8Rq3bJo8Z Cs8AcJlNQQMA7pHlU70QOLHH7VvX1v79QLfwhI41ZEROvX7Nr73tpF6lemGY4AyQiM /akgEQJ7kSi90x5f/pU/jiRZ50MbUfHvrM6w4zbc= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 18/27] target/i386/mshv: Implement mshv_arch_put_registers() Date: Tue, 1 Jul 2025 19:28:25 +0200 Message-Id: <20250701172834.44849-19-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391447304116600 Content-Type: text/plain; charset="utf-8" Write CPU register state to MSHV vCPUs. Various mapping functions to prepare the payload for the HV call have been implemented. Signed-off-by: Magnus Kulke --- include/system/mshv.h | 15 +++ target/i386/mshv/mshv-cpu.c | 239 ++++++++++++++++++++++++++++++++++++ 2 files changed, 254 insertions(+) diff --git a/include/system/mshv.h b/include/system/mshv.h index 7d0fed3c42..40510d5f80 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -79,6 +79,20 @@ typedef struct MshvMsiControl { #define mshv_msi_via_irqfd_enabled() mshv_enabled() =20 /* cpu */ +typedef struct MshvFPU { + uint8_t fpr[8][16]; + uint16_t fcw; + uint16_t fsw; + uint8_t ftwx; + uint8_t pad1; + uint16_t last_opcode; + uint64_t last_ip; + uint64_t last_dp; + uint8_t xmm[16][16]; + uint32_t mxcsr; + uint32_t pad2; +} MshvFPU; + typedef enum MshvVmExit { MshvVmExitIgnore =3D 0, MshvVmExitShutdown =3D 1, @@ -88,6 +102,7 @@ typedef enum MshvVmExit { void mshv_init_cpu_logic(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); +int mshv_configure_vcpu(const CPUState *cpu, const MshvFPU *fpu, uint64_t = xcr0); int mshv_get_standard_regs(CPUState *cpu); int mshv_get_special_regs(CPUState *cpu); int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit); diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 53b6722af4..dddb2da428 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -71,6 +71,35 @@ static enum hv_register_name SPECIAL_REGISTER_NAMES[17] = =3D { HV_X64_REGISTER_APIC_BASE, }; =20 +static enum hv_register_name FPU_REGISTER_NAMES[26] =3D { + HV_X64_REGISTER_XMM0, + HV_X64_REGISTER_XMM1, + HV_X64_REGISTER_XMM2, + HV_X64_REGISTER_XMM3, + HV_X64_REGISTER_XMM4, + HV_X64_REGISTER_XMM5, + HV_X64_REGISTER_XMM6, + HV_X64_REGISTER_XMM7, + HV_X64_REGISTER_XMM8, + HV_X64_REGISTER_XMM9, + HV_X64_REGISTER_XMM10, + HV_X64_REGISTER_XMM11, + HV_X64_REGISTER_XMM12, + HV_X64_REGISTER_XMM13, + HV_X64_REGISTER_XMM14, + HV_X64_REGISTER_XMM15, + HV_X64_REGISTER_FP_MMX0, + HV_X64_REGISTER_FP_MMX1, + HV_X64_REGISTER_FP_MMX2, + HV_X64_REGISTER_FP_MMX3, + HV_X64_REGISTER_FP_MMX4, + HV_X64_REGISTER_FP_MMX5, + HV_X64_REGISTER_FP_MMX6, + HV_X64_REGISTER_FP_MMX7, + HV_X64_REGISTER_FP_CONTROL_STATUS, + HV_X64_REGISTER_XMM_CONTROL_STATUS, +}; + int mshv_set_generic_regs(int cpu_fd, hv_register_assoc *assocs, size_t n_= regs) { struct mshv_vp_registers input =3D { @@ -292,8 +321,218 @@ int mshv_load_regs(CPUState *cpu) return 0; } =20 +static inline void populate_hv_segment_reg(SegmentCache *seg, + hv_x64_segment_register *hv_reg) +{ + uint32_t flags =3D seg->flags; + + hv_reg->base =3D seg->base; + hv_reg->limit =3D seg->limit; + hv_reg->selector =3D seg->selector; + hv_reg->segment_type =3D (flags >> DESC_TYPE_SHIFT) & 0xF; + hv_reg->non_system_segment =3D (flags & DESC_S_MASK) !=3D 0; + hv_reg->descriptor_privilege_level =3D (flags >> DESC_DPL_SHIFT) & 0x3; + hv_reg->present =3D (flags & DESC_P_MASK) !=3D 0; + hv_reg->reserved =3D 0; + hv_reg->available =3D (flags & DESC_AVL_MASK) !=3D 0; + hv_reg->_long =3D (flags >> DESC_L_SHIFT) & 0x1; + hv_reg->_default =3D (flags >> DESC_B_SHIFT) & 0x1; + hv_reg->granularity =3D (flags & DESC_G_MASK) !=3D 0; +} + +static inline void populate_hv_table_reg(const struct SegmentCache *seg, + hv_x64_table_register *hv_reg) +{ + memset(hv_reg, 0, sizeof(*hv_reg)); + + hv_reg->base =3D seg->base; + hv_reg->limit =3D seg->limit; +} + +static int set_special_regs(const CPUState *cpu) +{ + X86CPU *x86cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86cpu->env; + int cpu_fd =3D mshv_vcpufd(cpu); + struct hv_register_assoc assocs[ARRAY_SIZE(SPECIAL_REGISTER_NAMES)]; + size_t n_regs =3D ARRAY_SIZE(SPECIAL_REGISTER_NAMES); + int ret; + + /* set names */ + for (size_t i =3D 0; i < n_regs; i++) { + assocs[i].name =3D SPECIAL_REGISTER_NAMES[i]; + } + populate_hv_segment_reg(&env->segs[R_CS], &assocs[0].value.segment); + populate_hv_segment_reg(&env->segs[R_DS], &assocs[1].value.segment); + populate_hv_segment_reg(&env->segs[R_ES], &assocs[2].value.segment); + populate_hv_segment_reg(&env->segs[R_FS], &assocs[3].value.segment); + populate_hv_segment_reg(&env->segs[R_GS], &assocs[4].value.segment); + populate_hv_segment_reg(&env->segs[R_SS], &assocs[5].value.segment); + populate_hv_segment_reg(&env->tr, &assocs[6].value.segment); + populate_hv_segment_reg(&env->ldt, &assocs[7].value.segment); + + populate_hv_table_reg(&env->gdt, &assocs[8].value.table); + populate_hv_table_reg(&env->idt, &assocs[9].value.table); + + assocs[10].value.reg64 =3D env->cr[0]; + assocs[11].value.reg64 =3D env->cr[2]; + assocs[12].value.reg64 =3D env->cr[3]; + assocs[13].value.reg64 =3D env->cr[4]; + assocs[14].value.reg64 =3D cpu_get_apic_tpr(x86cpu->apic_state); + assocs[15].value.reg64 =3D env->efer; + assocs[16].value.reg64 =3D cpu_get_apic_base(x86cpu->apic_state); + + ret =3D mshv_set_generic_regs(cpu_fd, assocs, n_regs); + if (ret < 0) { + error_report("failed to set special registers"); + return -1; + } + + return 0; +} + +static int set_fpu(int cpu_fd, const struct MshvFPU *regs) +{ + struct hv_register_assoc assocs[ARRAY_SIZE(FPU_REGISTER_NAMES)]; + union hv_register_value *value; + size_t fp_i; + union hv_x64_fp_control_status_register *ctrl_status; + union hv_x64_xmm_control_status_register *xmm_ctrl_status; + int ret; + size_t n_regs =3D ARRAY_SIZE(FPU_REGISTER_NAMES); + + /* first 16 registers are xmm0-xmm15 */ + for (size_t i =3D 0; i < 16; i++) { + assocs[i].name =3D FPU_REGISTER_NAMES[i]; + value =3D &assocs[i].value; + memcpy(&value->reg128, ®s->xmm[i], 16); + } + + /* next 8 registers are fp_mmx0-fp_mmx7 */ + for (size_t i =3D 16; i < 24; i++) { + assocs[i].name =3D FPU_REGISTER_NAMES[i]; + fp_i =3D (i - 16); + value =3D &assocs[i].value; + memcpy(&value->reg128, ®s->fpr[fp_i], 16); + } + + /* last two registers are fp_control_status and xmm_control_status */ + assocs[24].name =3D FPU_REGISTER_NAMES[24]; + value =3D &assocs[24].value; + ctrl_status =3D &value->fp_control_status; + ctrl_status->fp_control =3D regs->fcw; + ctrl_status->fp_status =3D regs->fsw; + ctrl_status->fp_tag =3D regs->ftwx; + ctrl_status->reserved =3D 0; + ctrl_status->last_fp_op =3D regs->last_opcode; + ctrl_status->last_fp_rip =3D regs->last_ip; + + assocs[25].name =3D FPU_REGISTER_NAMES[25]; + value =3D &assocs[25].value; + xmm_ctrl_status =3D &value->xmm_control_status; + xmm_ctrl_status->xmm_status_control =3D regs->mxcsr; + xmm_ctrl_status->xmm_status_control_mask =3D 0; + xmm_ctrl_status->last_fp_rdp =3D regs->last_dp; + + ret =3D mshv_set_generic_regs(cpu_fd, assocs, n_regs); + if (ret < 0) { + error_report("failed to set fpu registers"); + return -1; + } + + return 0; +} + +static int set_xc_reg(int cpu_fd, uint64_t xcr0) +{ + int ret; + struct hv_register_assoc assoc =3D { + .name =3D HV_X64_REGISTER_XFEM, + .value.reg64 =3D xcr0, + }; + + ret =3D mshv_set_generic_regs(cpu_fd, &assoc, 1); + if (ret < 0) { + error_report("failed to set xcr0"); + return -errno; + } + return 0; +} + +static int set_cpu_state(const CPUState *cpu, const MshvFPU *fpu_regs, + uint64_t xcr0) +{ + int ret; + int cpu_fd =3D mshv_vcpufd(cpu); + + ret =3D set_standard_regs(cpu); + if (ret < 0) { + return ret; + } + ret =3D set_special_regs(cpu); + if (ret < 0) { + return ret; + } + ret =3D set_fpu(cpu_fd, fpu_regs); + if (ret < 0) { + return ret; + } + ret =3D set_xc_reg(cpu_fd, xcr0); + if (ret < 0) { + return ret; + } + return 0; +} + +/* + * TODO: populate topology info: + * + * X86CPU *x86cpu =3D X86_CPU(cpu); + * CPUX86State *env =3D &x86cpu->env; + * X86CPUTopoInfo *topo_info =3D &env->topo_info; + */ +int mshv_configure_vcpu(const CPUState *cpu, const struct MshvFPU *fpu, + uint64_t xcr0) +{ + int ret; + + ret =3D set_cpu_state(cpu, fpu, xcr0); + if (ret < 0) { + error_report("failed to set cpu state"); + return -1; + } + + return 0; +} + +static int put_regs(const CPUState *cpu) +{ + X86CPU *x86cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86cpu->env; + MshvFPU fpu =3D {0}; + int ret; + + memset(&fpu, 0, sizeof(fpu)); + + ret =3D mshv_configure_vcpu(cpu, &fpu, env->xcr0); + if (ret < 0) { + error_report("failed to configure vcpu"); + return ret; + } + + return 0; +} + int mshv_arch_put_registers(const CPUState *cpu) { + int ret; + + ret =3D put_regs(cpu); + if (ret < 0) { + error_report("Failed to put registers"); + return -1; + } + error_report("unimplemented"); abort(); } --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391065; cv=none; d=zohomail.com; s=zohoarc; b=EZGD+B8OxZv+IIxK7NeBPqgSOUVJIi3AxxWaJZnb2OI6+82g4dnTt5m8pQ22iYw7PqkZZNfAArfRQA6g4r88Vw0SM6oKAyAOK7C9Inc65VFHBd6QdAnQRscHiBjdfXwYlMyVApdVZwX8kNXSmfwF30Vx1MfSl5QEJNwKcdAN6QI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391065; 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=2+TtjmdoBXZAuImsqWw23qI6VIFIMg/1UXP7ySjDv2M=; b=H5AouO/mmZaa7RoJDBwaWrsJzEHl1RYCCPEatu+OjWsgJvmNRIrPfCiUx/8jzOBP1COkRxRwgbRtOfhi3C9scSVXoCw7dMNzQFD4/sGdveA/Omd6Aw2sGPufBr0+m9JrC7dpeE+3iQj19jkPc+nZWUdFdlH0SF9xR9WyqlFHhSg= 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 1751391065459617.6973755157396; Tue, 1 Jul 2025 10:31:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoX-0005r4-GC; Tue, 01 Jul 2025 13:30:37 -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 1uWeoT-0005X9-KG for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:33 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoP-0007G9-TN for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:33 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 6F26D2112239; Tue, 1 Jul 2025 10:30:15 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 6F26D2112239 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391019; bh=2+TtjmdoBXZAuImsqWw23qI6VIFIMg/1UXP7ySjDv2M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iYbV9eOc93T7WC8201t27yIQk0IH14utN1bi+qdwVulq+G5WUkS7/EzTF7qCouNMr 6rtQvJSus/6ZcHX8cMgyXGH6XNl7nlCgFMqVgLvnUI1k+i+fzD9L2/N2FAEgQpxAKc VdJFeSdMzRiN1eNsXWvXJsDEukXfwQA7LrPfPznQ= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 19/27] target/i386/mshv: Set local interrupt controller state Date: Tue, 1 Jul 2025 19:28:26 +0200 Message-Id: <20250701172834.44849-20-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391066025116600 Content-Type: text/plain; charset="utf-8" To set the local interrupt controller state, perform hv calls retrieving partition state from the hypervisor. Signed-off-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 117 ++++++++++++++++++++++++++++++++++++ target/i386/mshv/x86.c | 3 +- 2 files changed, 119 insertions(+), 1 deletion(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index dddb2da428..8716fe350b 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -12,6 +12,7 @@ =20 #include "qemu/osdep.h" #include "qemu/error-report.h" +#include "qemu/memalign.h" #include "qemu/typedefs.h" =20 #include "system/mshv.h" @@ -19,6 +20,7 @@ #include "linux/mshv.h" #include "hw/hyperv/hvhdk_mini.h" #include "hw/hyperv/hvgdk.h" +#include "hw/i386/apic_internal.h" =20 #include "cpu.h" #include "emulate/x86_decode.h" @@ -484,6 +486,114 @@ static int set_cpu_state(const CPUState *cpu, const M= shvFPU *fpu_regs, return 0; } =20 +static int get_vp_state(int cpu_fd, mshv_get_set_vp_state *state) +{ + int ret; + + ret =3D ioctl(cpu_fd, MSHV_GET_VP_STATE, state); + if (ret < 0) { + error_report("failed to get partition state: %s", strerror(errno)); + return -1; + } + + return 0; +} + +static int get_lapic(int cpu_fd, + struct hv_local_interrupt_controller_state *state) +{ + int ret; + size_t size =3D 4096; + /* buffer aligned to 4k, as *state requires that */ + void *buffer =3D qemu_memalign(size, size); + struct mshv_get_set_vp_state mshv_state =3D { 0 }; + + mshv_state.buf_ptr =3D (uint64_t) buffer; + mshv_state.buf_sz =3D size; + mshv_state.type =3D MSHV_VP_STATE_LAPIC; + + ret =3D get_vp_state(cpu_fd, &mshv_state); + if (ret =3D=3D 0) { + memcpy(state, buffer, sizeof(*state)); + } + qemu_vfree(buffer); + if (ret < 0) { + error_report("failed to get lapic"); + return -1; + } + + return 0; +} + +static uint32_t set_apic_delivery_mode(uint32_t reg, uint32_t mode) +{ + return ((reg) & ~0x700) | ((mode) << 8); +} + +static int set_vp_state(int cpu_fd, const mshv_get_set_vp_state *state) +{ + int ret; + + ret =3D ioctl(cpu_fd, MSHV_SET_VP_STATE, state); + if (ret < 0) { + error_report("failed to set partition state: %s", strerror(errno)); + return -1; + } + + return 0; +} + +static int set_lapic(int cpu_fd, + const struct hv_local_interrupt_controller_state *sta= te) +{ + int ret; + size_t size =3D 4096; + /* buffer aligned to 4k, as *state requires that */ + void *buffer =3D qemu_memalign(size, size); + struct mshv_get_set_vp_state mshv_state =3D { 0 }; + + if (!state) { + error_report("lapic state is NULL"); + return -1; + } + memcpy(buffer, state, sizeof(*state)); + + mshv_state.buf_ptr =3D (uint64_t) buffer; + mshv_state.buf_sz =3D size; + mshv_state.type =3D MSHV_VP_STATE_LAPIC; + + ret =3D set_vp_state(cpu_fd, &mshv_state); + qemu_vfree(buffer); + if (ret < 0) { + error_report("failed to set lapic: %s", strerror(errno)); + return -1; + } + + return 0; +} + +static int set_lint(int cpu_fd) +{ + int ret; + uint32_t *lvt_lint0, *lvt_lint1; + + struct hv_local_interrupt_controller_state lapic_state =3D { 0 }; + ret =3D get_lapic(cpu_fd, &lapic_state); + if (ret < 0) { + return ret; + } + + lvt_lint0 =3D &lapic_state.apic_lvt_lint0; + *lvt_lint0 =3D set_apic_delivery_mode(*lvt_lint0, APIC_DM_EXTINT); + + lvt_lint1 =3D &lapic_state.apic_lvt_lint1; + *lvt_lint1 =3D set_apic_delivery_mode(*lvt_lint1, APIC_DM_NMI); + + /* TODO: should we skip setting lapic if the values are the same? */ + + return set_lapic(cpu_fd, &lapic_state); +} + /* * TODO: populate topology info: * @@ -495,6 +605,7 @@ int mshv_configure_vcpu(const CPUState *cpu, const stru= ct MshvFPU *fpu, uint64_t xcr0) { int ret; + int cpu_fd =3D mshv_vcpufd(cpu); =20 ret =3D set_cpu_state(cpu, fpu, xcr0); if (ret < 0) { @@ -502,6 +613,12 @@ int mshv_configure_vcpu(const CPUState *cpu, const str= uct MshvFPU *fpu, return -1; } =20 + ret =3D set_lint(cpu_fd); + if (ret < 0) { + error_report("failed to set lpic int"); + return -1; + } + return 0; } =20 diff --git a/target/i386/mshv/x86.c b/target/i386/mshv/x86.c index 54c40b8064..d574b3bc52 100644 --- a/target/i386/mshv/x86.c +++ b/target/i386/mshv/x86.c @@ -232,8 +232,9 @@ bool x86_is_long_mode(CPUState *cpu) X86CPU *x86_cpu =3D X86_CPU(cpu); CPUX86State *env =3D &x86_cpu->env; uint64_t efer =3D env->efer; + uint64_t lme_lma =3D (MSR_EFER_LME | MSR_EFER_LMA); =20 - return ((efer & (EFER_LME | EFER_LMA)) =3D=3D (EFER_LME | EFER_LMA)); + return ((efer & lme_lma) =3D=3D lme_lma); } =20 bool x86_is_long64_mode(CPUState *cpu) --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391201; cv=none; d=zohomail.com; s=zohoarc; b=cDvNp19cVM9l+j/acGtTROzCkpicMpl4Xtlpyr7FCRyl0QWZV3khG7KFQ4HlVYqKEL4Eeyak2juXkOBEnPCm+0brZO5T9bG/EqJJaEeowTmYKXq7fsVzTGAv2kcbVjKAoU32mvAN465VUNaojXihdurFesMBfo3uHpdI7ny3GHA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391201; 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=IcalwT8+xtmV9BIr8XPQRT0cWxIIe+Fs8gXDtFxzNdo=; b=W82lvdOu/cTJwxN6SUDGfAbC2V8DwwGgS8dojU9U4oRD+t/6mMzpicItRowseEMolBRBGqeZVnnjNK8FqrwBdCUTgFe+EAKcCxdeXSCmUNehoQZOCuV7eM6BjaQYa1RvthHluRli8ag/HxuezK7Z1g3mdR/5gJ4P1mXaMfoHERo= 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 1751391200827933.9561831490073; Tue, 1 Jul 2025 10:33:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoy-0006Q6-F5; Tue, 01 Jul 2025 13:31:06 -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 1uWeob-00069F-G8 for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:43 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoV-0007Gq-1X for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:41 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 69590211938F; Tue, 1 Jul 2025 10:30:19 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 69590211938F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391023; bh=IcalwT8+xtmV9BIr8XPQRT0cWxIIe+Fs8gXDtFxzNdo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bLnDudvdzHAd4amKVX1eRRBu3yY4SAcdQ+Rkz35MY0IoA9pnMsawoceWvktkUvL+m UGB+T6NBmsH71I1zxM2v0mkycTSIPr0yziVoqQUNPNSWbeTn/kHPDH96kDx9SvDJww ldOzIP1kzUR3dUy4g6B+whbgQl6jBmFt1ZXVzhO4= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 20/27] target/i386/mshv: Register CPUID entries with MSHV Date: Tue, 1 Jul 2025 19:28:27 +0200 Message-Id: <20250701172834.44849-21-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391202190116600 Content-Type: text/plain; charset="utf-8" Convert the guest CPU's CPUID model into MSHV's format and register it with the hypervisor. This ensures that the guest observes the correct CPU feature set during CPUID instructions. Signed-off-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 199 ++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 8716fe350b..210bd85e11 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -323,6 +323,199 @@ int mshv_load_regs(CPUState *cpu) return 0; } =20 +static void add_cpuid_entry(GList *cpuid_entries, + uint32_t function, uint32_t index, + uint32_t eax, uint32_t ebx, + uint32_t ecx, uint32_t edx) +{ + struct hv_cpuid_entry *entry; + + entry =3D g_malloc0(sizeof(struct hv_cpuid_entry)); + entry->function =3D function; + entry->index =3D index; + entry->eax =3D eax; + entry->ebx =3D ebx; + entry->ecx =3D ecx; + entry->edx =3D edx; + + cpuid_entries =3D g_list_append(cpuid_entries, entry); +} + +static void collect_cpuid_entries(const CPUState *cpu, GList *cpuid_entrie= s) +{ + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + uint32_t eax, ebx, ecx, edx; + uint32_t leaf, subleaf; + size_t max_leaf =3D 0x1F; + size_t max_subleaf =3D 0x20; + + uint32_t leaves_with_subleaves[] =3D {0x4, 0x7, 0xD, 0xF, 0x10}; + int n_subleaf_leaves =3D ARRAY_SIZE(leaves_with_subleaves); + + /* Regular leaves without subleaves */ + for (leaf =3D 0; leaf <=3D max_leaf; leaf++) { + bool has_subleaves =3D false; + for (int i =3D 0; i < n_subleaf_leaves; i++) { + if (leaf =3D=3D leaves_with_subleaves[i]) { + has_subleaves =3D true; + break; + } + } + + if (!has_subleaves) { + cpu_x86_cpuid(env, leaf, 0, &eax, &ebx, &ecx, &edx); + if (eax =3D=3D 0 && ebx =3D=3D 0 && ecx =3D=3D 0 && edx =3D=3D= 0) { + /* all zeroes indicates no more leaves */ + continue; + } + + add_cpuid_entry(cpuid_entries, leaf, 0, eax, ebx, ecx, edx); + continue; + } + + subleaf =3D 0; + while (subleaf < max_subleaf) { + cpu_x86_cpuid(env, leaf, subleaf, &eax, &ebx, &ecx, &edx); + + if (eax =3D=3D 0 && ebx =3D=3D 0 && ecx =3D=3D 0 && edx =3D=3D= 0) { + /* all zeroes indicates no more leaves */ + break; + } + add_cpuid_entry(cpuid_entries, leaf, 0, eax, ebx, ecx, edx); + subleaf++; + } + } +} + +static int register_intercept_result_cpuid_entry(int cpu_fd, + uint8_t subleaf_specific, + uint8_t always_override, + struct hv_cpuid_entry *en= try) +{ + struct hv_register_x64_cpuid_result_parameters cpuid_params =3D { + .input.eax =3D entry->function, + .input.ecx =3D entry->index, + .input.subleaf_specific =3D subleaf_specific, + .input.always_override =3D always_override, + .input.padding =3D 0, + /* + * With regard to masks - these are to specify bits to be overwrit= ten + * The current CpuidEntry structure wouldn't allow to carry the ma= sks + * in addition to the actual register values. For this reason, the + * masks are set to the exact values of the corresponding register= bits + * to be registered for an overwrite. To view resulting values the + * hypervisor would return, HvCallGetVpCpuidValues hypercall can be + * used. + */ + .result.eax =3D entry->eax, + .result.eax_mask =3D entry->eax, + .result.ebx =3D entry->ebx, + .result.ebx_mask =3D entry->ebx, + .result.ecx =3D entry->ecx, + .result.ecx_mask =3D entry->ecx, + .result.edx =3D entry->edx, + .result.edx_mask =3D entry->edx, + }; + union hv_register_intercept_result_parameters parameters =3D { + .cpuid =3D cpuid_params, + }; + struct mshv_register_intercept_result args =3D { + .intercept_type =3D HV_INTERCEPT_TYPE_X64_CPUID, + .parameters =3D parameters, + }; + int ret; + + ret =3D ioctl(cpu_fd, MSHV_VP_REGISTER_INTERCEPT_RESULT, &args); + if (ret < 0) { + error_report("failed to register intercept result for cpuid: %s", + strerror(errno)); + return -1; + } + + return 0; +} + +static int register_intercept_result_cpuid(int cpu_fd, struct hv_cpuid *cp= uid) +{ + int ret =3D 0, entry_ret; + struct hv_cpuid_entry *entry; + uint8_t subleaf_specific, always_override; + + for (size_t i =3D 0; i < cpuid->nent; i++) { + entry =3D &cpuid->entries[i]; + + /* set defaults */ + subleaf_specific =3D 0; + always_override =3D 1; + + /* Intel */ + /* 0xb - Extended Topology Enumeration Leaf */ + /* 0x1f - V2 Extended Topology Enumeration Leaf */ + /* AMD */ + /* 0x8000_001e - Processor Topology Information */ + /* 0x8000_0026 - Extended CPU Topology */ + if (entry->function =3D=3D 0xb + || entry->function =3D=3D 0x1f + || entry->function =3D=3D 0x8000001e + || entry->function =3D=3D 0x80000026) { + subleaf_specific =3D 1; + always_override =3D 1; + } else if (entry->function =3D=3D 0x00000001 + || entry->function =3D=3D 0x80000000 + || entry->function =3D=3D 0x80000001 + || entry->function =3D=3D 0x80000008) { + subleaf_specific =3D 0; + always_override =3D 1; + } + + entry_ret =3D register_intercept_result_cpuid_entry(cpu_fd, + subleaf_specific, + always_override, + entry); + if ((entry_ret < 0) && (ret =3D=3D 0)) { + ret =3D entry_ret; + } + } + + return ret; +} + +static int set_cpuid2(const CPUState *cpu) +{ + int ret; + size_t n_entries, cpuid_size; + struct hv_cpuid *cpuid; + struct hv_cpuid_entry *entry; + GList *entries =3D NULL; + int cpu_fd =3D mshv_vcpufd(cpu); + + collect_cpuid_entries(cpu, entries); + n_entries =3D g_list_length(entries); + + cpuid_size =3D sizeof(struct hv_cpuid) + + n_entries * sizeof(struct hv_cpuid_entry); + + cpuid =3D g_malloc0(cpuid_size); + cpuid->nent =3D n_entries; + cpuid->padding =3D 0; + + for (size_t i =3D 0; i < n_entries; i++) { + entry =3D g_list_nth_data(entries, i); + cpuid->entries[i] =3D *entry; + g_free(entry); + } + g_list_free(entries); + + ret =3D register_intercept_result_cpuid(cpu_fd, cpuid); + g_free(cpuid); + if (ret < 0) { + return ret; + } + + return 0; +} + static inline void populate_hv_segment_reg(SegmentCache *seg, hv_x64_segment_register *hv_reg) { @@ -607,6 +800,12 @@ int mshv_configure_vcpu(const CPUState *cpu, const str= uct MshvFPU *fpu, int ret; int cpu_fd =3D mshv_vcpufd(cpu); =20 + ret =3D set_cpuid2(cpu); + if (ret < 0) { + error_report("failed to set cpuid"); + return -1; + } + ret =3D set_cpu_state(cpu, fpu, xcr0); if (ret < 0) { error_report("failed to set cpu state"); --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391192; cv=none; d=zohomail.com; s=zohoarc; b=ZBm4YZtBH6J4EF/0QAK5za6WkPjbGjXfJBYq2pw4H6dNhfKoZaeQRE3+N2f8tRIMCNZyp8ngoCM9CQDVKLuVn6UbZOnR/mR53ZVMHfoLklPw7jA8gYE87UF4ujxrva6AzXISeqGWWvEsw0eBLxw6egWh5CBC2JzTsYAAUkKOLdY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391192; 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=0Vv7Y0Pmj311UbJKfroCFrvii350KiVQ+/9R//QZAT4=; b=ehzxEoOq4TlXYrNfkSDmiVNWQqDF3VuUdcTXqyCSKn9NmbigdlxAfpSV1hnZA8p8qB6Lhp1QWMoZQJpP+DBQcCBV8frmbfm4o1GtfVoryBf6b9rcMxRzmNqtO6eVb9GrdjulCTHUvsOVy0Ey6KvPWV5RO+X5JFuRg5A+ptaxEp4= 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 1751391192385537.7119275346312; Tue, 1 Jul 2025 10:33:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWep1-0006X7-DW; Tue, 01 Jul 2025 13:31: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 1uWeoh-0006OJ-8q for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:47 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoY-0007Hg-Gy for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:44 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 65D49211223E; Tue, 1 Jul 2025 10:30:23 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 65D49211223E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391026; bh=0Vv7Y0Pmj311UbJKfroCFrvii350KiVQ+/9R//QZAT4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CTlDmBkLU0JeOMrMAlzjnZMjvv4kEeQt1eULuRmDWt8xt/GnPrVKziCPeJ6r6vA5N D1Sr9C0khOmGyB1eDpNIi01JRJ41NcqbWLTm9bYt6OxPF8to2syUseqSb+CJpGvFsV jOBf4U3JVcb3E41nMjRBDuKJ/kVxRLzXFngTYTX0= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 21/27] target/i386/mshv: Register MSRs with MSHV Date: Tue, 1 Jul 2025 19:28:28 +0200 Message-Id: <20250701172834.44849-22-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391193954116600 Content-Type: text/plain; charset="utf-8" Build and register the guest vCPU's model-specific registers using the MSHV interface. Signed-off-by: Magnus Kulke --- accel/mshv/meson.build | 1 + accel/mshv/msr.c | 372 ++++++++++++++++++++++++++++++++++++ include/system/mshv.h | 23 +++ target/i386/cpu.h | 2 + target/i386/mshv/mshv-cpu.c | 33 ++++ 5 files changed, 431 insertions(+) create mode 100644 accel/mshv/msr.c diff --git a/accel/mshv/meson.build b/accel/mshv/meson.build index f88fc8678c..d3a2b32581 100644 --- a/accel/mshv/meson.build +++ b/accel/mshv/meson.build @@ -2,6 +2,7 @@ mshv_ss =3D ss.source_set() mshv_ss.add(if_true: files( 'irq.c', 'mem.c', + 'msr.c', 'mshv-all.c' )) =20 diff --git a/accel/mshv/msr.c b/accel/mshv/msr.c new file mode 100644 index 0000000000..b4aecd68ed --- /dev/null +++ b/accel/mshv/msr.c @@ -0,0 +1,372 @@ +/* + * QEMU MSHV support + * + * Copyright Microsoft, Corp. 2025 + * + * Authors: Magnus Kulke + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "system/mshv.h" +#include "linux/mshv.h" +#include "qemu/error-report.h" + +static uint32_t supported_msrs[64] =3D { + IA32_MSR_TSC, + IA32_MSR_EFER, + IA32_MSR_KERNEL_GS_BASE, + IA32_MSR_APIC_BASE, + IA32_MSR_PAT, + IA32_MSR_SYSENTER_CS, + IA32_MSR_SYSENTER_ESP, + IA32_MSR_SYSENTER_EIP, + IA32_MSR_STAR, + IA32_MSR_LSTAR, + IA32_MSR_CSTAR, + IA32_MSR_SFMASK, + IA32_MSR_MTRR_DEF_TYPE, + IA32_MSR_MTRR_PHYSBASE0, + IA32_MSR_MTRR_PHYSMASK0, + IA32_MSR_MTRR_PHYSBASE1, + IA32_MSR_MTRR_PHYSMASK1, + IA32_MSR_MTRR_PHYSBASE2, + IA32_MSR_MTRR_PHYSMASK2, + IA32_MSR_MTRR_PHYSBASE3, + IA32_MSR_MTRR_PHYSMASK3, + IA32_MSR_MTRR_PHYSBASE4, + IA32_MSR_MTRR_PHYSMASK4, + IA32_MSR_MTRR_PHYSBASE5, + IA32_MSR_MTRR_PHYSMASK5, + IA32_MSR_MTRR_PHYSBASE6, + IA32_MSR_MTRR_PHYSMASK6, + IA32_MSR_MTRR_PHYSBASE7, + IA32_MSR_MTRR_PHYSMASK7, + IA32_MSR_MTRR_FIX64K_00000, + IA32_MSR_MTRR_FIX16K_80000, + IA32_MSR_MTRR_FIX16K_A0000, + IA32_MSR_MTRR_FIX4K_C0000, + IA32_MSR_MTRR_FIX4K_C8000, + IA32_MSR_MTRR_FIX4K_D0000, + IA32_MSR_MTRR_FIX4K_D8000, + IA32_MSR_MTRR_FIX4K_E0000, + IA32_MSR_MTRR_FIX4K_E8000, + IA32_MSR_MTRR_FIX4K_F0000, + IA32_MSR_MTRR_FIX4K_F8000, + IA32_MSR_TSC_AUX, + IA32_MSR_DEBUG_CTL, + HV_X64_MSR_GUEST_OS_ID, + HV_X64_MSR_SINT0, + HV_X64_MSR_SINT1, + HV_X64_MSR_SINT2, + HV_X64_MSR_SINT3, + HV_X64_MSR_SINT4, + HV_X64_MSR_SINT5, + HV_X64_MSR_SINT6, + HV_X64_MSR_SINT7, + HV_X64_MSR_SINT8, + HV_X64_MSR_SINT9, + HV_X64_MSR_SINT10, + HV_X64_MSR_SINT11, + HV_X64_MSR_SINT12, + HV_X64_MSR_SINT13, + HV_X64_MSR_SINT14, + HV_X64_MSR_SINT15, + HV_X64_MSR_SCONTROL, + HV_X64_MSR_SIEFP, + HV_X64_MSR_SIMP, + HV_X64_MSR_REFERENCE_TSC, + HV_X64_MSR_EOM, +}; +static const size_t msr_count =3D ARRAY_SIZE(supported_msrs); + +static int compare_msr_index(const void *a, const void *b) +{ + return *(uint32_t *)a - *(uint32_t *)b; +} + +__attribute__((constructor)) +static void init_sorted_msr_map(void) +{ + qsort(supported_msrs, msr_count, sizeof(uint32_t), compare_msr_index); +} + +static int mshv_is_supported_msr(uint32_t msr) +{ + return bsearch(&msr, supported_msrs, msr_count, sizeof(uint32_t), + compare_msr_index) !=3D NULL; +} + +static int mshv_msr_to_hv_reg_name(uint32_t msr, uint32_t *hv_reg) +{ + switch (msr) { + case IA32_MSR_TSC: + *hv_reg =3D HV_X64_REGISTER_TSC; + return 0; + case IA32_MSR_EFER: + *hv_reg =3D HV_X64_REGISTER_EFER; + return 0; + case IA32_MSR_KERNEL_GS_BASE: + *hv_reg =3D HV_X64_REGISTER_KERNEL_GS_BASE; + return 0; + case IA32_MSR_APIC_BASE: + *hv_reg =3D HV_X64_REGISTER_APIC_BASE; + return 0; + case IA32_MSR_PAT: + *hv_reg =3D HV_X64_REGISTER_PAT; + return 0; + case IA32_MSR_SYSENTER_CS: + *hv_reg =3D HV_X64_REGISTER_SYSENTER_CS; + return 0; + case IA32_MSR_SYSENTER_ESP: + *hv_reg =3D HV_X64_REGISTER_SYSENTER_ESP; + return 0; + case IA32_MSR_SYSENTER_EIP: + *hv_reg =3D HV_X64_REGISTER_SYSENTER_EIP; + return 0; + case IA32_MSR_STAR: + *hv_reg =3D HV_X64_REGISTER_STAR; + return 0; + case IA32_MSR_LSTAR: + *hv_reg =3D HV_X64_REGISTER_LSTAR; + return 0; + case IA32_MSR_CSTAR: + *hv_reg =3D HV_X64_REGISTER_CSTAR; + return 0; + case IA32_MSR_SFMASK: + *hv_reg =3D HV_X64_REGISTER_SFMASK; + return 0; + case IA32_MSR_MTRR_CAP: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_CAP; + return 0; + case IA32_MSR_MTRR_DEF_TYPE: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_DEF_TYPE; + return 0; + case IA32_MSR_MTRR_PHYSBASE0: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_BASE0; + return 0; + case IA32_MSR_MTRR_PHYSMASK0: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_MASK0; + return 0; + case IA32_MSR_MTRR_PHYSBASE1: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_BASE1; + return 0; + case IA32_MSR_MTRR_PHYSMASK1: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_MASK1; + return 0; + case IA32_MSR_MTRR_PHYSBASE2: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_BASE2; + return 0; + case IA32_MSR_MTRR_PHYSMASK2: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_MASK2; + return 0; + case IA32_MSR_MTRR_PHYSBASE3: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_BASE3; + return 0; + case IA32_MSR_MTRR_PHYSMASK3: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_MASK3; + return 0; + case IA32_MSR_MTRR_PHYSBASE4: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_BASE4; + return 0; + case IA32_MSR_MTRR_PHYSMASK4: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_MASK4; + return 0; + case IA32_MSR_MTRR_PHYSBASE5: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_BASE5; + return 0; + case IA32_MSR_MTRR_PHYSMASK5: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_MASK5; + return 0; + case IA32_MSR_MTRR_PHYSBASE6: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_BASE6; + return 0; + case IA32_MSR_MTRR_PHYSMASK6: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_MASK6; + return 0; + case IA32_MSR_MTRR_PHYSBASE7: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_BASE7; + return 0; + case IA32_MSR_MTRR_PHYSMASK7: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_PHYS_MASK7; + return 0; + case IA32_MSR_MTRR_FIX64K_00000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX64K00000; + return 0; + case IA32_MSR_MTRR_FIX16K_80000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX16K80000; + return 0; + case IA32_MSR_MTRR_FIX16K_A0000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX16KA0000; + return 0; + case IA32_MSR_MTRR_FIX4K_C0000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX4KC0000; + return 0; + case IA32_MSR_MTRR_FIX4K_C8000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX4KC8000; + return 0; + case IA32_MSR_MTRR_FIX4K_D0000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX4KD0000; + return 0; + case IA32_MSR_MTRR_FIX4K_D8000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX4KD8000; + return 0; + case IA32_MSR_MTRR_FIX4K_E0000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX4KE0000; + return 0; + case IA32_MSR_MTRR_FIX4K_E8000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX4KE8000; + return 0; + case IA32_MSR_MTRR_FIX4K_F0000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX4KF0000; + return 0; + case IA32_MSR_MTRR_FIX4K_F8000: + *hv_reg =3D HV_X64_REGISTER_MSR_MTRR_FIX4KF8000; + return 0; + case IA32_MSR_TSC_AUX: + *hv_reg =3D HV_X64_REGISTER_TSC_AUX; + return 0; + case IA32_MSR_BNDCFGS: + *hv_reg =3D HV_X64_REGISTER_BNDCFGS; + return 0; + case IA32_MSR_DEBUG_CTL: + *hv_reg =3D HV_X64_REGISTER_DEBUG_CTL; + return 0; + case IA32_MSR_TSC_ADJUST: + *hv_reg =3D HV_X64_REGISTER_TSC_ADJUST; + return 0; + case IA32_MSR_SPEC_CTRL: + *hv_reg =3D HV_X64_REGISTER_SPEC_CTRL; + return 0; + case HV_X64_MSR_GUEST_OS_ID: + *hv_reg =3D HV_REGISTER_GUEST_OS_ID; + return 0; + case HV_X64_MSR_SINT0: + *hv_reg =3D HV_REGISTER_SINT0; + return 0; + case HV_X64_MSR_SINT1: + *hv_reg =3D HV_REGISTER_SINT1; + return 0; + case HV_X64_MSR_SINT2: + *hv_reg =3D HV_REGISTER_SINT2; + return 0; + case HV_X64_MSR_SINT3: + *hv_reg =3D HV_REGISTER_SINT3; + return 0; + case HV_X64_MSR_SINT4: + *hv_reg =3D HV_REGISTER_SINT4; + return 0; + case HV_X64_MSR_SINT5: + *hv_reg =3D HV_REGISTER_SINT5; + return 0; + case HV_X64_MSR_SINT6: + *hv_reg =3D HV_REGISTER_SINT6; + return 0; + case HV_X64_MSR_SINT7: + *hv_reg =3D HV_REGISTER_SINT7; + return 0; + case HV_X64_MSR_SINT8: + *hv_reg =3D HV_REGISTER_SINT8; + return 0; + case HV_X64_MSR_SINT9: + *hv_reg =3D HV_REGISTER_SINT9; + return 0; + case HV_X64_MSR_SINT10: + *hv_reg =3D HV_REGISTER_SINT10; + return 0; + case HV_X64_MSR_SINT11: + *hv_reg =3D HV_REGISTER_SINT11; + return 0; + case HV_X64_MSR_SINT12: + *hv_reg =3D HV_REGISTER_SINT12; + return 0; + case HV_X64_MSR_SINT13: + *hv_reg =3D HV_REGISTER_SINT13; + return 0; + case HV_X64_MSR_SINT14: + *hv_reg =3D HV_REGISTER_SINT14; + return 0; + case HV_X64_MSR_SINT15: + *hv_reg =3D HV_REGISTER_SINT15; + return 0; + case IA32_MSR_MISC_ENABLE: + *hv_reg =3D HV_X64_REGISTER_MSR_IA32_MISC_ENABLE; + return 0; + case HV_X64_MSR_SCONTROL: + *hv_reg =3D HV_REGISTER_SCONTROL; + return 0; + case HV_X64_MSR_SIEFP: + *hv_reg =3D HV_REGISTER_SIEFP; + return 0; + case HV_X64_MSR_SIMP: + *hv_reg =3D HV_REGISTER_SIMP; + return 0; + case HV_X64_MSR_REFERENCE_TSC: + *hv_reg =3D HV_REGISTER_REFERENCE_TSC; + return 0; + case HV_X64_MSR_EOM: + *hv_reg =3D HV_REGISTER_EOM; + return 0; + default: + error_report("failed to map MSR %u to HV register name", msr); + return -1; + } +} + +static int set_msrs(int cpu_fd, GList *msrs) +{ + size_t n_msrs; + GList *entries; + MshvMsrEntry *entry; + enum hv_register_name name; + struct hv_register_assoc *assoc; + int ret; + size_t i =3D 0; + + n_msrs =3D g_list_length(msrs); + hv_register_assoc *assocs =3D g_new0(hv_register_assoc, n_msrs); + + entries =3D msrs; + for (const GList *elem =3D entries; elem !=3D NULL; elem =3D elem->nex= t) { + entry =3D elem->data; + ret =3D mshv_msr_to_hv_reg_name(entry->index, &name); + if (ret < 0) { + g_free(assocs); + return ret; + } + assoc =3D &assocs[i]; + assoc->name =3D name; + /* the union has been initialized to 0 */ + assoc->value.reg64 =3D entry->data; + i++; + } + ret =3D mshv_set_generic_regs(cpu_fd, assocs, n_msrs); + g_free(assocs); + if (ret < 0) { + error_report("failed to set msrs"); + return -1; + } + return 0; +} + + +int mshv_configure_msr(int cpu_fd, const MshvMsrEntry *msrs, size_t n_msrs) +{ + GList *valid_msrs =3D NULL; + uint32_t msr_index; + int ret; + + for (size_t i =3D 0; i < n_msrs; i++) { + msr_index =3D msrs[i].index; + /* check whether index of msrs is in SUPPORTED_MSRS */ + if (mshv_is_supported_msr(msr_index)) { + valid_msrs =3D g_list_append(valid_msrs, (void *) &msrs[i]); + } + } + + ret =3D set_msrs(cpu_fd, valid_msrs); + g_list_free(valid_msrs); + + return ret; +} diff --git a/include/system/mshv.h b/include/system/mshv.h index 40510d5f80..9e3242d5e6 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -36,6 +36,8 @@ typedef struct hyperv_message hv_message; =20 #define MSHV_PAGE_SHIFT 12 =20 +#define MSHV_MSR_ENTRIES_COUNT 64 + #ifdef CONFIG_MSHV_IS_POSSIBLE extern bool mshv_allowed; #define mshv_enabled() (mshv_allowed) @@ -116,8 +118,29 @@ void mshv_arch_amend_proc_features( union hv_partition_synthetic_processor_features *features); int mshv_arch_post_init_vm(int vm_fd); =20 +/* pio */ +int mshv_pio_write(uint64_t port, const uint8_t *data, uintptr_t size, + bool is_secure_mode); +void mshv_pio_read(uint64_t port, uint8_t *data, uintptr_t size, + bool is_secure_mode); + +/* generic */ int mshv_hvcall(int mshv_fd, const struct mshv_root_hvcall *args); =20 +/* msr */ +typedef struct MshvMsrEntry { + uint32_t index; + uint32_t reserved; + uint64_t data; +} MshvMsrEntry; + +typedef struct MshvMsrEntries { + MshvMsrEntry entries[MSHV_MSR_ENTRIES_COUNT]; + uint32_t nmsrs; +} MshvMsrEntries; + +int mshv_configure_msr(int cpu_fd, const MshvMsrEntry *msrs, size_t n_msrs= ); + /* memory */ typedef struct MshvMemoryRegion { uint64_t guest_phys_addr; diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 48979432f6..8c3ddbaae7 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -435,9 +435,11 @@ typedef enum X86Seg { #define MSR_SMI_COUNT 0x34 #define MSR_CORE_THREAD_COUNT 0x35 #define MSR_MTRRcap 0xfe +#define MSR_MTRR_MEM_TYPE_WB 0x06 #define MSR_MTRRcap_VCNT 8 #define MSR_MTRRcap_FIXRANGE_SUPPORT (1 << 8) #define MSR_MTRRcap_WC_SUPPORTED (1 << 10) +#define MSR_MTRR_ENABLE (1 << 11) =20 #define MSR_IA32_SYSENTER_CS 0x174 #define MSR_IA32_SYSENTER_ESP 0x175 diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 210bd85e11..a17be79a77 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -787,6 +787,33 @@ static int set_lint(int cpu_fd) return set_lapic(cpu_fd, &lapic_state); } =20 +static int setup_msrs(int cpu_fd) +{ + int ret; + uint64_t default_type =3D MSR_MTRR_ENABLE | MSR_MTRR_MEM_TYPE_WB; + + /* boot msr entries */ + MshvMsrEntry msrs[9] =3D { + { .index =3D IA32_MSR_SYSENTER_CS, .data =3D 0x0, }, + { .index =3D IA32_MSR_SYSENTER_ESP, .data =3D 0x0, }, + { .index =3D IA32_MSR_SYSENTER_EIP, .data =3D 0x0, }, + { .index =3D IA32_MSR_STAR, .data =3D 0x0, }, + { .index =3D IA32_MSR_CSTAR, .data =3D 0x0, }, + { .index =3D IA32_MSR_LSTAR, .data =3D 0x0, }, + { .index =3D IA32_MSR_KERNEL_GS_BASE, .data =3D 0x0, }, + { .index =3D IA32_MSR_SFMASK, .data =3D 0x0, }, + { .index =3D IA32_MSR_MTRR_DEF_TYPE, .data =3D default_type, }, + }; + + ret =3D mshv_configure_msr(cpu_fd, msrs, 9); + if (ret < 0) { + error_report("failed to setup msrs"); + return -1; + } + + return 0; +} + /* * TODO: populate topology info: * @@ -806,6 +833,12 @@ int mshv_configure_vcpu(const CPUState *cpu, const str= uct MshvFPU *fpu, return -1; } =20 + ret =3D setup_msrs(cpu_fd); + if (ret < 0) { + error_report("failed to setup msrs"); + return -1; + } + ret =3D set_cpu_state(cpu, fpu, xcr0); if (ret < 0) { error_report("failed to set cpu state"); --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391367; cv=none; d=zohomail.com; s=zohoarc; b=i63nn8iibl/ZJCOykZ9Ujsf/mDECxqO26w4QyLPnQigfU5OyPono1aH9MSIHGyRVxVNzpzbdD4XnQvmMx6XuXxuMmXJ1w5rzgu5C+ppYVxk0B13cGQEQbRFoMV8ek9hJ+wYzmz2VbX/5FiDGqY7m47XCbuSwqyGjLHkjj+c72kQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391367; 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=4LvCJGwmpIjKrsgYwC24zk+8wmMxFtEhjy4AN5gbITE=; b=k9XJGJba+DXceDa4L7VcRQPoHTLT3QC5vnmVOy06QbWEu7ZzFflM3E3kzjyiAJAFkxB3P/1ln6oEnTij1PaIygF8UeD5gUoO2l1YjPXjizFSlpFHK5wH9pIKEqIRGAOjh+o3r+7g4muOIoBo1wm6NbtCBDvtnSfFsrcU6fDiLlU= 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 1751391367619214.55533558548814; Tue, 1 Jul 2025 10:36:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWepK-0006e5-Ux; Tue, 01 Jul 2025 13:31:27 -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 1uWeog-0006OH-So for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:51 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeoc-0007Iv-4p for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:45 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 638AF2119388; Tue, 1 Jul 2025 10:30:27 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 638AF2119388 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391030; bh=4LvCJGwmpIjKrsgYwC24zk+8wmMxFtEhjy4AN5gbITE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SWQ0iY6so0cyh9AVREV7pV/NW4hgpVese8FeGiYayblnHQ/P4fNWN6/Yoq67zdumE QH0oANfL84JB7TrZDpel2rMdg8dUhyBHmx0r1H/xpy93N8ixf1ZfmNAJ7x6zNNjes2 I4kkJvveooWYwJeAqk8R8oVDnDcQ0DMWmQpfQYtw= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 22/27] target/i386/mshv: Integrate x86 instruction decoder/emulator Date: Tue, 1 Jul 2025 19:28:29 +0200 Message-Id: <20250701172834.44849-23-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391368561116600 Content-Type: text/plain; charset="utf-8" Connect the x86 instruction decoder and emulator to the MSHV backend to handle intercepted instructions. This enables software emulation of MMIO operations in MSHV guests. MSHV has a translate_gva hypercall that is used to accessing the physical guest memory. A guest might read from unmapped memory regions (e.g. OVMF will probe 0xfed40000 for a vTPM). In those cases 0xFF bytes is returned instead of aborting the execution. Signed-off-by: Magnus Kulke --- accel/mshv/mem.c | 65 +++++++++++++++++++ accel/mshv/mshv-all.c | 2 +- include/system/mshv.h | 6 +- target/i386/mshv/mshv-cpu.c | 126 +++++++++++++++++++++++++++++++++++- 4 files changed, 196 insertions(+), 3 deletions(-) diff --git a/accel/mshv/mem.c b/accel/mshv/mem.c index f51e9fee8e..6d7a726898 100644 --- a/accel/mshv/mem.c +++ b/accel/mshv/mem.c @@ -58,6 +58,71 @@ static int map_or_unmap(int vm_fd, const MshvMemoryRegio= n *mr, bool map) return set_guest_memory(vm_fd, ®ion); } =20 +static int handle_unmapped_mmio_region_read(uint64_t gpa, uint64_t size, + uint8_t *data) +{ + warn_report("read from unmapped mmio region gpa=3D0x%lx size=3D%lu", g= pa, size); + + if (size =3D=3D 0 || size > 8) { + error_report("invalid size %lu for reading from unmapped mmio regi= on", + size); + return -1; + } + + memset(data, 0xFF, size); + + return 0; +} + +int mshv_guest_mem_read(uint64_t gpa, uint8_t *data, uintptr_t size, + bool is_secure_mode, bool instruction_fetch) +{ + int ret; + MemTxAttrs memattr =3D { .secure =3D is_secure_mode }; + + if (instruction_fetch) { + trace_mshv_insn_fetch(gpa, size); + } else { + trace_mshv_mem_read(gpa, size); + } + + ret =3D address_space_rw(&address_space_memory, gpa, memattr, (void *)= data, + size, false); + if (ret =3D=3D MEMTX_OK) { + return 0; + } + + if (ret =3D=3D MEMTX_DECODE_ERROR) { + return handle_unmapped_mmio_region_read(gpa, size, data); + } + + error_report("failed to read guest memory at 0x%lx", gpa); + return -1; +} + +int mshv_guest_mem_write(uint64_t gpa, const uint8_t *data, uintptr_t size, + bool is_secure_mode) +{ + int ret; + MemTxAttrs memattr =3D { .secure =3D is_secure_mode }; + + trace_mshv_mem_write(gpa, size); + ret =3D address_space_rw(&address_space_memory, gpa, memattr, (void *)= data, + size, true); + if (ret =3D=3D MEMTX_OK) { + return 0; + } + + if (ret =3D=3D MEMTX_DECODE_ERROR) { + warn_report("write to unmapped mmio region gpa=3D0x%lx size=3D%lu"= , gpa, + size); + return 0; + } + + error_report("Failed to write guest memory"); + return -1; +} + static int set_memory(const MshvMemoryRegion *mshv_mr, bool add) { int ret =3D 0; diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index d95893806f..e9f880b83e 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -432,7 +432,7 @@ static int mshv_init(MachineState *ms) return -1; } =20 - mshv_init_cpu_logic(); + mshv_init_mmio_emu(); =20 mshv_init_msicontrol(); =20 diff --git a/include/system/mshv.h b/include/system/mshv.h index 9e3242d5e6..63104af68c 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -101,7 +101,7 @@ typedef enum MshvVmExit { MshvVmExitSpecial =3D 2, } MshvVmExit; =20 -void mshv_init_cpu_logic(void); +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); int mshv_configure_vcpu(const CPUState *cpu, const MshvFPU *fpu, uint64_t = xcr0); @@ -151,6 +151,10 @@ typedef struct MshvMemoryRegion { =20 int mshv_add_mem(int vm_fd, const MshvMemoryRegion *mr); int mshv_remove_mem(int vm_fd, const MshvMemoryRegion *mr); +int mshv_guest_mem_read(uint64_t gpa, uint8_t *data, uintptr_t size, + bool is_secure_mode, bool instruction_fetch); +int mshv_guest_mem_write(uint64_t gpa, const uint8_t *data, uintptr_t size, + bool is_secure_mode); void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryRegionSection *secti= on, bool add); =20 diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index a17be79a77..d177fe5826 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -102,6 +102,34 @@ static enum hv_register_name FPU_REGISTER_NAMES[26] = =3D { HV_X64_REGISTER_XMM_CONTROL_STATUS, }; =20 +static int translate_gva(int cpu_fd, uint64_t gva, uint64_t *gpa, + uint64_t flags) +{ + int ret; + union hv_translate_gva_result result =3D { 0 }; + + *gpa =3D 0; + mshv_translate_gva args =3D { + .gva =3D gva, + .flags =3D flags, + .gpa =3D (__u64 *)gpa, + .result =3D &result, + }; + + ret =3D ioctl(cpu_fd, MSHV_TRANSLATE_GVA, &args); + if (ret < 0) { + error_report("failed to invoke gpa->gva translation"); + return -errno; + } + if (result.result_code !=3D HV_TRANSLATE_GVA_SUCCESS) { + error_report("failed to translate gva (" TARGET_FMT_lx ") to gpa",= gva); + return -1; + + } + + return 0; +} + int mshv_set_generic_regs(int cpu_fd, hv_register_assoc *assocs, size_t n_= regs) { struct mshv_vp_registers input =3D { @@ -921,8 +949,104 @@ int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int= *cpu_fd) return 0; } =20 -void mshv_init_cpu_logic(void) +static int guest_mem_read_with_gva(const CPUState *cpu, uint64_t gva, + uint8_t *data, uintptr_t size, + bool fetch_instruction) +{ + int ret; + uint64_t gpa, flags; + int cpu_fd =3D mshv_vcpufd(cpu); + + flags =3D HV_TRANSLATE_GVA_VALIDATE_READ; + ret =3D translate_gva(cpu_fd, gva, &gpa, flags); + if (ret < 0) { + error_report("failed to translate gva to gpa"); + return -1; + } + + ret =3D mshv_guest_mem_read(gpa, data, size, false, fetch_instruction); + if (ret < 0) { + error_report("failed to read from guest memory"); + return -1; + } + + return 0; +} + +static int guest_mem_write_with_gva(const CPUState *cpu, uint64_t gva, + const uint8_t *data, uintptr_t size) +{ + int ret; + uint64_t gpa, flags; + int cpu_fd =3D mshv_vcpufd(cpu); + + flags =3D HV_TRANSLATE_GVA_VALIDATE_WRITE; + ret =3D translate_gva(cpu_fd, gva, &gpa, flags); + if (ret < 0) { + error_report("failed to translate gva to gpa"); + return -1; + } + ret =3D mshv_guest_mem_write(gpa, data, size, false); + if (ret < 0) { + error_report("failed to write to guest memory"); + return -1; + } + return 0; +} + +static void write_mem(CPUState *cpu, void *data, target_ulong addr, int by= tes) +{ + if (guest_mem_write_with_gva(cpu, addr, data, bytes) < 0) { + error_report("failed to write memory"); + abort(); + } +} + +static void read_mem(CPUState *cpu, void *data, target_ulong addr, int byt= es) +{ + if (guest_mem_read_with_gva(cpu, addr, data, bytes, false) < 0) { + error_report("failed to read memory"); + abort(); + } +} + +static void fetch_instruction(CPUState *cpu, void *data, + target_ulong addr, int bytes) +{ + if (guest_mem_read_with_gva(cpu, addr, data, bytes, true) < 0) { + error_report("failed to fetch instruction"); + abort(); + } +} + +static void read_segment_descriptor(CPUState *cpu, + struct x86_segment_descriptor *desc, + enum X86Seg seg_idx) +{ + bool ret; + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + SegmentCache *seg =3D &env->segs[seg_idx]; + x86_segment_selector sel =3D { .sel =3D seg->selector & 0xFFFF }; + + ret =3D x86_read_segment_descriptor(cpu, desc, sel); + if (ret =3D=3D false) { + error_report("failed to read segment descriptor"); + abort(); + } +} + +static const struct x86_emul_ops mshv_x86_emul_ops =3D { + .fetch_instruction =3D fetch_instruction, + .read_mem =3D read_mem, + .write_mem =3D write_mem, + .read_segment_descriptor =3D read_segment_descriptor, +}; + +void mshv_init_mmio_emu(void) { + init_decoder(); + init_emu(&mshv_x86_emul_ops); } =20 void mshv_arch_init_vcpu(CPUState *cpu) --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391367; cv=none; d=zohomail.com; s=zohoarc; b=Q9rxu/1aQzJEuRVn0LqPbB4TYcd+zBhSzEcd2EdDe+Gz6dHghbmdxhNXGMBn1CWtpNo3jCgbpUfeCuHqha87zCw8N+pjxqn4piI240vlhg7FzQ0rMe235wxurtu2nOiYuVqvGrVa3nJnM+jQvDje6H5AMPIiF07poIbrO4ZWec4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391367; 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=3UM7Nw3GLTOBdCo/L/+8k7oSOt0mDKqpVYagjRStkRE=; b=oCxAD8DneQan66DX/4RaFWWOAMgQjsOIAaSkrOK4dQ4upJAT6zSBirxk1sZKn/OYc+pOS0GrvUt/X2MhOYZ2CdBGCen5hqWxVaruZ3e6GZD/Jhc4liCZT4vlQpdPGlb/yFCHuaeg4a/Mff/ghSkLTriqAPGkfla/dRaHDwdivUo= 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 1751391367817476.76616687638364; Tue, 1 Jul 2025 10:36:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWepi-0007Eg-Ay; Tue, 01 Jul 2025 13:31:50 -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 1uWeok-0006On-Ei for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:54 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeog-0007Ji-W8 for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:49 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 5EDDB211938A; Tue, 1 Jul 2025 10:30:31 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5EDDB211938A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391034; bh=3UM7Nw3GLTOBdCo/L/+8k7oSOt0mDKqpVYagjRStkRE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VF0NMkcnN9r5dbFq/5SEMPFY0wFxYFce3qVu439P7BD5bNrxI3RcjHR/LKvAQwvjB 43o695GNtMP5oa+c44dowfq5O3FmEeg3e2JtYV+UAekDYqnbj2wFE5ztZJFslw6qQQ u+Pae4hTO6NnEQ0lg5FNl03uGJVYisiV91Q7CZaE= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 23/27] target/i386/mshv: Write MSRs to the hypervisor Date: Tue, 1 Jul 2025 19:28:30 +0200 Message-Id: <20250701172834.44849-24-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391368250116600 Content-Type: text/plain; charset="utf-8" Push current model-specific register (MSR) values to MSHV's vCPUs as part of setting state to the hypervisor. Signed-off-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 68 +++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index d177fe5826..8def964862 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -900,6 +900,65 @@ static int put_regs(const CPUState *cpu) return 0; } =20 +struct MsrPair { + uint32_t index; + uint64_t value; +}; + +static int put_msrs(const CPUState *cpu) +{ + int ret =3D 0; + X86CPU *x86cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86cpu->env; + MshvMsrEntries *msrs =3D g_malloc0(sizeof(MshvMsrEntries)); + + struct MsrPair pairs[] =3D { + { MSR_IA32_SYSENTER_CS, env->sysenter_cs }, + { MSR_IA32_SYSENTER_ESP, env->sysenter_esp }, + { MSR_IA32_SYSENTER_EIP, env->sysenter_eip }, + { MSR_EFER, env->efer }, + { MSR_PAT, env->pat }, + { MSR_STAR, env->star }, + { MSR_CSTAR, env->cstar }, + { MSR_LSTAR, env->lstar }, + { MSR_KERNELGSBASE, env->kernelgsbase }, + { MSR_FMASK, env->fmask }, + { MSR_MTRRdefType, env->mtrr_deftype }, + { MSR_VM_HSAVE_PA, env->vm_hsave }, + { MSR_SMI_COUNT, env->msr_smi_count }, + { MSR_IA32_PKRS, env->pkrs }, + { MSR_IA32_BNDCFGS, env->msr_bndcfgs }, + { MSR_IA32_XSS, env->xss }, + { MSR_IA32_UMWAIT_CONTROL, env->umwait }, + { MSR_IA32_TSX_CTRL, env->tsx_ctrl }, + { MSR_AMD64_TSC_RATIO, env->amd_tsc_scale_msr }, + { MSR_TSC_AUX, env->tsc_aux }, + { MSR_TSC_ADJUST, env->tsc_adjust }, + { MSR_IA32_SMBASE, env->smbase }, + { MSR_IA32_SPEC_CTRL, env->spec_ctrl }, + { MSR_VIRT_SSBD, env->virt_ssbd }, + }; + + if (ARRAY_SIZE(pairs) > MSHV_MSR_ENTRIES_COUNT) { + error_report("MSR entries exceed maximum size"); + g_free(msrs); + return -1; + } + + for (size_t i =3D 0; i < ARRAY_SIZE(pairs); i++) { + MshvMsrEntry *entry =3D &msrs->entries[i]; + entry->index =3D pairs[i].index; + entry->reserved =3D 0; + entry->data =3D pairs[i].value; + msrs->nmsrs++; + } + + ret =3D mshv_configure_msr(mshv_vcpufd(cpu), &msrs->entries[0], msrs->= nmsrs); + g_free(msrs); + return ret; +} + + int mshv_arch_put_registers(const CPUState *cpu) { int ret; @@ -910,8 +969,13 @@ int mshv_arch_put_registers(const CPUState *cpu) return -1; } =20 - error_report("unimplemented"); - abort(); + ret =3D put_msrs(cpu); + if (ret < 0) { + error_report("Failed to put msrs"); + return -1; + } + + return 0; } =20 void mshv_arch_amend_proc_features( --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391258; cv=none; d=zohomail.com; s=zohoarc; b=Ws5L67L1NLESlcT7BoUM/KXKFCoMLgwTh1aT/V6h5vNHKDAuRUiN2T8Hk6beR4DG6w8dWuUxfzJoOG5VZ/N4/CYhF5OY4MAGwbiZY0JcjpMvyPBdZIK7Axere9LUK2h9AQ8oBJIZG1XRuzewE+QYc+lzV1PXYbtcB1WG3RWe624= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391258; 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=74QqBYYVDLmh00uPX2tb/zHhMNGHChVTPpJto0oEgZY=; b=oLha1lKgk0FhjgpUpyPrgBwN54nJkxbe/Jswt0w/BfPZlNHcNZ1rIrbcu+YDG1T0/LGCU2RNx6w+0zP4HRc6uD12B5zsotDP1AM9YUc9xOc+KRKiygyDIEXXF014tuxlnht1QluSxgs6xdk8eGEp2KPJspi44EBCovq0OEWp3uI= 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 1751391258330784.6167514479464; Tue, 1 Jul 2025 10:34:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWepi-0007Eh-A7; Tue, 01 Jul 2025 13:31:50 -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 1uWeoq-0006Q5-12 for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:59 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeok-0007Kb-Tm for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:55 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 595402119397; Tue, 1 Jul 2025 10:30:35 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 595402119397 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391038; bh=74QqBYYVDLmh00uPX2tb/zHhMNGHChVTPpJto0oEgZY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UtNGClOHKkNc/XTPZodlvTdd//tkXNjb8LH8Yl8CXKAP38CUgFn9GXMZ5Jzz2Ru/f KDgn4ixKkB2/7oFofJKDcwnb4FOlXSFbyJi3ym2eR2ey6pOOp+BdSKUMoVMOEDmiAK dFWpKFOLro0n5wKszzQNP7w6XcqhnS+hEI2lXFwI= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 24/27] target/i386/mshv: Implement mshv_vcpu_run() Date: Tue, 1 Jul 2025 19:28:31 +0200 Message-Id: <20250701172834.44849-25-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391258839116600 Content-Type: text/plain; charset="utf-8" Add the main vCPU execution loop for MSHV using the MSHV_RUN_VP ioctl. A translate_gva() hypercall is implemented. The execution loop handles guest entry and VM exits. There are handlers for memory r/w, PIO and MMIO to which the exit events are dispatched. In case of MMIO the i386 instruction decoder/emulator is invoked to perform the operation in user space. Signed-off-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 463 +++++++++++++++++++++++++++++++++++- 1 file changed, 461 insertions(+), 2 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 8def964862..353073ed50 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -984,10 +984,469 @@ void mshv_arch_amend_proc_features( features->access_guest_idle_reg =3D 1; } =20 +static int set_memory_info(const struct hyperv_message *msg, + struct hv_x64_memory_intercept_message *info) +{ + if (msg->header.message_type !=3D HVMSG_GPA_INTERCEPT + && msg->header.message_type !=3D HVMSG_UNMAPPED_GPA + && msg->header.message_type !=3D HVMSG_UNACCEPTED_GPA) { + error_report("invalid message type"); + return -1; + } + memcpy(info, msg->payload, sizeof(*info)); + + return 0; +} + +static int emulate_instruction(CPUState *cpu, + const uint8_t *insn_bytes, size_t insn_len, + uint64_t gva, uint64_t gpa) +{ + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + struct x86_decode decode =3D { 0 }; + int ret; + x86_insn_stream stream =3D { .bytes =3D insn_bytes, .len =3D insn_len = }; + + ret =3D mshv_load_regs(cpu); + if (ret < 0) { + error_report("failed to load registers"); + return -1; + } + + decode_instruction_stream(env, &decode, &stream); + exec_instruction(env, &decode); + + ret =3D mshv_store_regs(cpu); + if (ret < 0) { + error_report("failed to store registers"); + return -1; + } + + return 0; +} + +static int handle_mmio(CPUState *cpu, const struct hyperv_message *msg, + MshvVmExit *exit_reason) +{ + struct hv_x64_memory_intercept_message info =3D { 0 }; + size_t insn_len; + uint8_t access_type; + uint8_t *instruction_bytes; + int ret; + + ret =3D set_memory_info(msg, &info); + if (ret < 0) { + error_report("failed to convert message to memory info"); + return -1; + } + insn_len =3D info.instruction_byte_count; + access_type =3D info.header.intercept_access_type; + + if (access_type =3D=3D HV_X64_INTERCEPT_ACCESS_TYPE_EXECUTE) { + error_report("invalid intercept access type: execute"); + return -1; + } + + if (insn_len > 16) { + error_report("invalid mmio instruction length: %zu", insn_len); + return -1; + } + + trace_mshv_handle_mmio(info.guest_virtual_address, + info.guest_physical_address, + info.instruction_byte_count, access_type); + + instruction_bytes =3D info.instruction_bytes; + + ret =3D emulate_instruction(cpu, instruction_bytes, insn_len, + info.guest_virtual_address, + info.guest_physical_address); + if (ret < 0) { + error_report("failed to emulate mmio"); + return -1; + } + + *exit_reason =3D MshvVmExitIgnore; + + return 0; +} + +static int set_ioport_info(const struct hyperv_message *msg, + hv_x64_io_port_intercept_message *info) +{ + if (msg->header.message_type !=3D HVMSG_X64_IO_PORT_INTERCEPT) { + error_report("Invalid message type"); + return -1; + } + memcpy(info, msg->payload, sizeof(*info)); + + return 0; +} + +typedef struct X64Registers { + const uint32_t *names; + const uint64_t *values; + uintptr_t count; +} X64Registers; + +static int set_x64_registers(int cpu_fd, const X64Registers *regs) +{ + size_t n_regs =3D regs->count; + struct hv_register_assoc *assocs; + + assocs =3D g_new0(hv_register_assoc, n_regs); + for (size_t i =3D 0; i < n_regs; i++) { + assocs[i].name =3D regs->names[i]; + assocs[i].value.reg64 =3D regs->values[i]; + } + int ret; + + ret =3D mshv_set_generic_regs(cpu_fd, assocs, n_regs); + g_free(assocs); + if (ret < 0) { + error_report("failed to set x64 registers"); + return -1; + } + + return 0; +} + +static inline MemTxAttrs get_mem_attrs(bool is_secure_mode) +{ + MemTxAttrs memattr =3D {0}; + memattr.secure =3D is_secure_mode; + return memattr; +} + +static void pio_read(uint64_t port, uint8_t *data, uintptr_t size, + bool is_secure_mode) +{ + int ret =3D 0; + MemTxAttrs memattr =3D get_mem_attrs(is_secure_mode); + ret =3D address_space_rw(&address_space_io, port, memattr, (void *)dat= a, size, + false); + if (ret !=3D MEMTX_OK) { + error_report("Failed to read from port %lx: %d", port, ret); + abort(); + } +} + +static int pio_write(uint64_t port, const uint8_t *data, uintptr_t size, + bool is_secure_mode) +{ + int ret =3D 0; + MemTxAttrs memattr =3D get_mem_attrs(is_secure_mode); + ret =3D address_space_rw(&address_space_io, port, memattr, (void *)dat= a, size, + true); + return ret; +} + +static int handle_pio_non_str(const CPUState *cpu, + hv_x64_io_port_intercept_message *info) { + size_t len =3D info->access_info.access_size; + uint8_t access_type =3D info->header.intercept_access_type; + int ret; + uint32_t val, eax; + const uint32_t eax_mask =3D 0xffffffffu >> (32 - len * 8); + size_t insn_len; + uint64_t rip, rax; + uint32_t reg_names[2]; + uint64_t reg_values[2]; + struct X64Registers x64_regs =3D { 0 }; + uint16_t port =3D info->port_number; + int cpu_fd =3D mshv_vcpufd(cpu); + + if (access_type =3D=3D HV_X64_INTERCEPT_ACCESS_TYPE_WRITE) { + union { + uint32_t u32; + uint8_t bytes[4]; + } conv; + + /* convert the first 4 bytes of rax to bytes */ + conv.u32 =3D (uint32_t)info->rax; + /* secure mode is set to false */ + ret =3D pio_write(port, conv.bytes, len, false); + if (ret < 0) { + error_report("Failed to write to io port"); + return -1; + } + } else { + uint8_t data[4] =3D { 0 }; + /* secure mode is set to false */ + pio_read(info->port_number, data, len, false); + + /* Preserve high bits in EAX, but clear out high bits in RAX */ + val =3D *(uint32_t *)data; + eax =3D (((uint32_t)info->rax) & ~eax_mask) | (val & eax_mask); + info->rax =3D (uint64_t)eax; + } + + insn_len =3D info->header.instruction_length; + + /* Advance RIP and update RAX */ + rip =3D info->header.rip + insn_len; + rax =3D info->rax; + + reg_names[0] =3D HV_X64_REGISTER_RIP; + reg_values[0] =3D rip; + reg_names[1] =3D HV_X64_REGISTER_RAX; + reg_values[1] =3D rax; + + x64_regs.names =3D reg_names; + x64_regs.values =3D reg_values; + x64_regs.count =3D 2; + + ret =3D set_x64_registers(cpu_fd, &x64_regs); + if (ret < 0) { + error_report("Failed to set x64 registers"); + return -1; + } + + cpu->accel->dirty =3D false; + + return 0; +} + +static int fetch_guest_state(CPUState *cpu) +{ + int ret; + + ret =3D mshv_get_standard_regs(cpu); + if (ret < 0) { + error_report("Failed to get standard registers"); + return -1; + } + + ret =3D mshv_get_special_regs(cpu); + if (ret < 0) { + error_report("Failed to get special registers"); + return -1; + } + + return 0; +} + +static int read_memory(int cpu_fd, uint64_t initial_gva, uint64_t initial_= gpa, + uint64_t gva, uint8_t *data, size_t len) +{ + int ret; + uint64_t gpa, flags; + + if (gva =3D=3D initial_gva) { + gpa =3D initial_gpa; + } else { + flags =3D HV_TRANSLATE_GVA_VALIDATE_READ; + ret =3D translate_gva(cpu_fd, gva, &gpa, flags); + if (ret < 0) { + return -1; + } + + ret =3D mshv_guest_mem_read(gpa, data, len, false, false); + if (ret < 0) { + error_report("failed to read guest mem"); + return -1; + } + } + + return 0; +} + +static int write_memory(int cpu_fd, uint64_t initial_gva, uint64_t initial= _gpa, + uint64_t gva, const uint8_t *data, size_t len) +{ + int ret; + uint64_t gpa, flags; + + if (gva =3D=3D initial_gva) { + gpa =3D initial_gpa; + } else { + flags =3D HV_TRANSLATE_GVA_VALIDATE_WRITE; + ret =3D translate_gva(cpu_fd, gva, &gpa, flags); + if (ret < 0) { + error_report("failed to translate gva to gpa"); + return -1; + } + } + ret =3D mshv_guest_mem_write(gpa, data, len, false); + if (ret !=3D MEMTX_OK) { + error_report("failed to write to mmio"); + return -1; + } + + return 0; +} + +static int handle_pio_str_write(CPUState *cpu, + hv_x64_io_port_intercept_message *info, + size_t repeat, uint16_t port, + bool direction_flag) +{ + int ret; + uint64_t src; + uint8_t data[4] =3D { 0 }; + size_t len =3D info->access_info.access_size; + int cpu_fd =3D mshv_vcpufd(cpu); + + src =3D linear_addr(cpu, info->rsi, R_DS); + + for (size_t i =3D 0; i < repeat; i++) { + ret =3D read_memory(cpu_fd, 0, 0, src, data, len); + if (ret < 0) { + error_report("Failed to read memory"); + return -1; + } + ret =3D pio_write(port, data, len, false); + if (ret < 0) { + error_report("Failed to write to io port"); + return -1; + } + src +=3D direction_flag ? -len : len; + info->rsi +=3D direction_flag ? -len : len; + } + + return 0; +} + +static int handle_pio_str_read(CPUState *cpu, + hv_x64_io_port_intercept_message *info, + size_t repeat, uint16_t port, + bool direction_flag) +{ + int ret; + uint64_t dst; + size_t len =3D info->access_info.access_size; + uint8_t data[4] =3D { 0 }; + int cpu_fd =3D mshv_vcpufd(cpu); + + dst =3D linear_addr(cpu, info->rdi, R_ES); + + for (size_t i =3D 0; i < repeat; i++) { + pio_read(port, data, len, false); + + ret =3D write_memory(cpu_fd, 0, 0, dst, data, len); + if (ret < 0) { + error_report("Failed to write memory"); + return -1; + } + dst +=3D direction_flag ? -len : len; + info->rdi +=3D direction_flag ? -len : len; + } + + return 0; +} + +static int handle_pio_str(CPUState *cpu, + hv_x64_io_port_intercept_message *info) +{ + uint8_t access_type =3D info->header.intercept_access_type; + uint16_t port =3D info->port_number; + bool repop =3D info->access_info.rep_prefix =3D=3D 1; + size_t repeat =3D repop ? info->rcx : 1; + size_t insn_len =3D info->header.instruction_length; + bool direction_flag; + uint32_t reg_names[3]; + uint64_t reg_values[3]; + int ret; + struct X64Registers x64_regs =3D { 0 }; + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + int cpu_fd =3D mshv_vcpufd(cpu); + + ret =3D fetch_guest_state(cpu); + if (ret < 0) { + error_report("Failed to fetch guest state"); + return -1; + } + + direction_flag =3D (env->eflags & DESC_E_MASK) !=3D 0; + + if (access_type =3D=3D HV_X64_INTERCEPT_ACCESS_TYPE_WRITE) { + ret =3D handle_pio_str_write(cpu, info, repeat, port, direction_fl= ag); + if (ret < 0) { + error_report("Failed to handle pio str write"); + return -1; + } + reg_names[0] =3D HV_X64_REGISTER_RSI; + reg_values[0] =3D info->rsi; + } else { + ret =3D handle_pio_str_read(cpu, info, repeat, port, direction_fla= g); + reg_names[0] =3D HV_X64_REGISTER_RDI; + reg_values[0] =3D info->rdi; + } + + reg_names[1] =3D HV_X64_REGISTER_RIP; + reg_values[1] =3D info->header.rip + insn_len; + reg_names[2] =3D HV_X64_REGISTER_RAX; + reg_values[2] =3D info->rax; + + x64_regs.names =3D reg_names; + x64_regs.values =3D reg_values; + x64_regs.count =3D 2; + + ret =3D set_x64_registers(cpu_fd, &x64_regs); + if (ret < 0) { + error_report("Failed to set x64 registers"); + return -1; + } + + cpu->accel->dirty =3D false; + + return 0; +} + +static int handle_pio(CPUState *cpu, const struct hyperv_message *msg) +{ + struct hv_x64_io_port_intercept_message info =3D { 0 }; + int ret; + + ret =3D set_ioport_info(msg, &info); + if (ret < 0) { + error_report("Failed to convert message to ioport info"); + return -1; + } + + if (info.access_info.string_op) { + return handle_pio_str(cpu, &info); + } + + return handle_pio_non_str(cpu, &info); +} + int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit) { - error_report("unimplemented"); - abort(); + int ret; + enum MshvVmExit exit_reason; + int cpu_fd =3D mshv_vcpufd(cpu); + + ret =3D ioctl(cpu_fd, MSHV_RUN_VP, msg); + if (ret < 0) { + return MshvVmExitShutdown; + } + + switch (msg->header.message_type) { + case HVMSG_UNRECOVERABLE_EXCEPTION: + return MshvVmExitShutdown; + case HVMSG_UNMAPPED_GPA: + case HVMSG_GPA_INTERCEPT: + ret =3D handle_mmio(cpu, msg, &exit_reason); + if (ret < 0) { + error_report("failed to handle mmio"); + return -1; + } + return exit_reason; + case HVMSG_X64_IO_PORT_INTERCEPT: + ret =3D handle_pio(cpu, msg); + if (ret < 0) { + return MshvVmExitSpecial; + } + return MshvVmExitIgnore; + default: + break; + } + + *exit =3D MshvVmExitIgnore; + return 0; } =20 void mshv_remove_vcpu(int vm_fd, int cpu_fd) --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391367; cv=none; d=zohomail.com; s=zohoarc; b=GSE1zei3F7B+7n7SXkecyYG4ncCuisWWeulZO681Y/1zlN17ZWnXtBql5zVxO1shE4wYQ+lTVNUb1B3soA1Eh2OYCP8SZtZpMh3nXTOCY2r1QnVfLtPoiM4jozWXsZr/bSrPDBhFTXCj3PjZyk6DuBYGDmcZU4wLsw+N+ng/TEA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391367; 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=QZDzWZMzPcxjbTxKmB09N+RGsXAVaQXEfIPlk952L+A=; b=X52u8a4BPkoqlsX89d42MvWNFsbAM1+z50XNeEehmQUG9ChxFfMjbQml6mZMignM3aE0ewBCnF38UX1xeaoPza34w7tKlMq48Vtz875/0dA7Q71+tRyP3X0bbbODlGY0t69+nwEH6Ab7olzFyCejNTc6LjNvYfceKlGZs23aQ78= 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 1751391367553328.93391832931593; Tue, 1 Jul 2025 10:36:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWepZ-0006zn-Jp; Tue, 01 Jul 2025 13:31:45 -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 1uWeos-0006QX-69 for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:59 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeon-0007Lo-Uq for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:30:57 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 587C92112239; Tue, 1 Jul 2025 10:30:39 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 587C92112239 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391042; bh=QZDzWZMzPcxjbTxKmB09N+RGsXAVaQXEfIPlk952L+A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nAoKdtVNyuqUyc09TylOCS8LtYVm73YSqrpvHbe0mdOLOXS7U0Z83VvOF0lKc7MNP MP9tPX7sPMG5pi717O1/+BUipKnzRY4smMTShilc8FLlVurqoXXq74O46Fodbd5Tox w+BWxs9Mgf+IelfAdhogp/BFiVrTQ0XLZhc9+om4= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 25/27] target/i386/mshv: Handle HVMSG_X64_HALT vm exit Date: Tue, 1 Jul 2025 19:28:32 +0200 Message-Id: <20250701172834.44849-26-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391368338116600 Content-Type: text/plain; charset="utf-8" Implemented handler for HVMSG_X64_HALT exit messages from the hypervisor. Signed-off-by: Magnus Kulke --- accel/mshv/mshv-all.c | 3 +++ include/system/mshv.h | 1 + target/i386/mshv/mshv-cpu.c | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index e9f880b83e..f6ad2b6d2b 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -506,6 +506,9 @@ static int mshv_cpu_exec(CPUState *cpu) switch (exit_reason) { case MshvVmExitIgnore: break; + case MshvVmExitHlt: + ret =3D EXCP_HLT; + break; default: ret =3D EXCP_INTERRUPT; break; diff --git a/include/system/mshv.h b/include/system/mshv.h index 63104af68c..27d7e3dff3 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -99,6 +99,7 @@ typedef enum MshvVmExit { MshvVmExitIgnore =3D 0, MshvVmExitShutdown =3D 1, MshvVmExitSpecial =3D 2, + MshvVmExitHlt =3D 3, } MshvVmExit; =20 void mshv_init_mmio_emu(void); diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 353073ed50..41a3398ec8 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -14,6 +14,7 @@ #include "qemu/error-report.h" #include "qemu/memalign.h" #include "qemu/typedefs.h" +#include "qemu/main-loop.h" =20 #include "system/mshv.h" #include "system/address-spaces.h" @@ -1413,6 +1414,26 @@ static int handle_pio(CPUState *cpu, const struct hy= perv_message *msg) return handle_pio_non_str(cpu, &info); } =20 +static int handle_halt(CPUState *cpu) +{ + int ret =3D 0; + X86CPU *x86cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86cpu->env; + uint32_t hw_irq =3D cpu->interrupt_request & CPU_INTERRUPT_HARD; + uint32_t nmi_irq =3D cpu->interrupt_request & CPU_INTERRUPT_NMI; + bool irqs_enabled =3D (env->eflags & IF_MASK) !=3D 0; + + bql_lock(); + if (!nmi_irq && (!hw_irq || !irqs_enabled)) { + cpu->exception_index =3D EXCP_HLT; + cpu->halted =3D true; + ret =3D 1; + } + bql_unlock(); + + return ret; +} + int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit) { int ret; @@ -1441,6 +1462,11 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_messa= ge *msg, MshvVmExit *exit) return MshvVmExitSpecial; } return MshvVmExitIgnore; + case HVMSG_X64_HALT: + ret =3D handle_halt(cpu); + if (ret < 0) { + return MshvVmExitHlt; + } default: break; } --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391628; cv=none; d=zohomail.com; s=zohoarc; b=ii4NJ9lI6BoUnNARuDNpF+esDxCAE0+Pa7gfkdzWFI19NvtJrUFqLjDS1udQXdgd1rc1YaVv6O/5DhXxAw+KhCIEORv+wOPBEQtKp82gW6M5g7cAtAV0B+jef4uj48Hs3+Dz3Qfi+XCU3eHhu8k84u4GIK1pxums6CbCllJuvCs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391628; 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=LlF/ug667Qtq+1TC8h6j8A9o+o2DhbC1syYUSBOc37c=; b=H8uNuZE5CrPnwxreOuQDj5vxjhg/7ugTpDn1mg+kxL5EVYqS+24mqm8Wp4Kd6QSKqIIKzzSZYv+iJwKvIRbhLE9eBzbwn7rlHQcDQZ0gsfPqzRiKB8Rd9EJh1X8y4Nca5hRMNpj2OmQW12ofkDREvIjztLLcvxYxU6S7CpoLqVI= 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 175139162813537.59325469711132; Tue, 1 Jul 2025 10:40:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWepd-000731-3A; Tue, 01 Jul 2025 13:31:45 -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 1uWeov-0006TW-Kc for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:31:04 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeor-0007Iv-IV for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:31:01 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 54D2F211938F; Tue, 1 Jul 2025 10:30:43 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 54D2F211938F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391046; bh=LlF/ug667Qtq+1TC8h6j8A9o+o2DhbC1syYUSBOc37c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=duSBAGKAyNTxw3RoZmxCXzaAOamb7dc0cU12IliCC1m6jE3baf+qBa7CnsxbGt2P0 MzyNImixqtjIOeeoH+VYnMAVh4mtTFvv555qCQpblWQAX0YD5KLR/kDisQ+tygXxlM bzx9nrWrLQKIJLZmRukHGXHhWWqA4a0sy/MWHeXI= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 26/27] accel/mshv: Workaround for overlappig mem mappings Date: Tue, 1 Jul 2025 19:28:33 +0200 Message-Id: <20250701172834.44849-27-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: 1751391630288116600 Content-Type: text/plain; charset="utf-8" QEMU maps certain regions into the guest multiple times, as seen in the trace below. Currently the MSHV kernel driver will reject those mappings. To workaround this, a record is kept (a static global list of "slots", inspired by what the HVF accelerator has implemented). An overlapping region is not registered at the hypervisor, and marked as mapped=3Dfalse. If there is an UNMAPPED_GPA exit, we can look for a slot that is unmapped and would cover the GPA. In this case we map out the conflicting slot and map in the requested region. mshv_set_phys_mem add=3D1 name=3Dpc.bios mshv_map_memory =3D> u_a=3D7ffff4e00000 gpa=3D00fffc0000 size=3D000400= 00 mshv_set_phys_mem add=3D1 name=3Dioapic mshv_set_phys_mem add=3D1 name=3Dhpet mshv_set_phys_mem add=3D0 name=3Dpc.ram mshv_unmap_memory u_a=3D7fff67e00000 gpa=3D0000000000 size=3D80000000 mshv_set_phys_mem add=3D1 name=3Dpc.ram mshv_map_memory u_a=3D7fff67e00000 gpa=3D0000000000 size=3D000c0000 mshv_set_phys_mem add=3D1 name=3Dpc.rom mshv_map_memory u_a=3D7ffff4c00000 gpa=3D00000c0000 size=3D00020000 mshv_set_phys_mem add=3D1 name=3Dpc.bios mshv_remap_attempt =3D> u_a=3D7ffff4e20000 gpa=3D00000e0000 size=3D000200= 00 Signed-off-by: Magnus Kulke --- accel/mshv/mem.c | 264 ++++++++++++++++++++++++++++++++---- accel/mshv/trace-events | 7 +- include/system/mshv.h | 16 ++- target/i386/mshv/mshv-cpu.c | 43 ++++++ 4 files changed, 295 insertions(+), 35 deletions(-) diff --git a/accel/mshv/mem.c b/accel/mshv/mem.c index 6d7a726898..0ffe379601 100644 --- a/accel/mshv/mem.c +++ b/accel/mshv/mem.c @@ -20,44 +20,167 @@ #include #include "trace.h" =20 +MshvMemorySlot mem_slots[MSHV_MAX_MEM_SLOTS]; + +static MshvMemorySlot *find_free_mem_slot(void) +{ + for (int i =3D 0; i < MSHV_MAX_MEM_SLOTS; i++) { + if (mem_slots[i].memory_size =3D=3D 0) { + return &mem_slots[i]; + } + } + + return NULL; +} + +/* Find _currently mapped_ memory slot, that is overlapping in userspace */ +static MshvMemorySlot *find_overlap_mem_slot(const MshvMemorySlot *slot) +{ + MshvMemorySlot *other; + bool overlaps; + uint64_t start_1 =3D slot->userspace_addr, start_2; + size_t len_1 =3D slot->memory_size, len_2; + + for (int i =3D 0; i < MSHV_MAX_MEM_SLOTS; i++) { + other =3D &mem_slots[i]; + + if (other =3D=3D slot) { + continue; + } + + start_2 =3D other->userspace_addr; + len_2 =3D other->memory_size; + + overlaps =3D ranges_overlap(start_1, len_1, start_2, len_2); + if (other->mapped && overlaps) { + return other; + } + } + + return NULL; +} + static int set_guest_memory(int vm_fd, const mshv_user_mem_region *region) { int ret; =20 ret =3D ioctl(vm_fd, MSHV_SET_GUEST_MEMORY, region); if (ret < 0) { - error_report("failed to set guest memory"); - return -errno; + error_report("failed to set guest memory: %s", strerror(errno)); + return -1; } =20 return 0; } =20 -static int map_or_unmap(int vm_fd, const MshvMemoryRegion *mr, bool map) +static int map_or_unmap(int vm_fd, const MshvMemorySlot *slot, bool map) { struct mshv_user_mem_region region =3D {0}; =20 - region.guest_pfn =3D mr->guest_phys_addr >> MSHV_PAGE_SHIFT; - region.size =3D mr->memory_size; - region.userspace_addr =3D mr->userspace_addr; + region.guest_pfn =3D slot->guest_phys_addr >> MSHV_PAGE_SHIFT; + region.size =3D slot->memory_size; + region.userspace_addr =3D slot->userspace_addr; =20 if (!map) { region.flags |=3D (1 << MSHV_SET_MEM_BIT_UNMAP); - trace_mshv_unmap_memory(mr->userspace_addr, mr->guest_phys_addr, - mr->memory_size); + trace_mshv_unmap_memory(slot->userspace_addr, slot->guest_phys_add= r, + slot->memory_size); return set_guest_memory(vm_fd, ®ion); } =20 region.flags =3D BIT(MSHV_SET_MEM_BIT_EXECUTABLE); - if (!mr->readonly) { + if (!slot->readonly) { region.flags |=3D BIT(MSHV_SET_MEM_BIT_WRITABLE); } =20 - trace_mshv_map_memory(mr->userspace_addr, mr->guest_phys_addr, - mr->memory_size); + trace_mshv_map_memory(slot->userspace_addr, slot->guest_phys_addr, + slot->memory_size); return set_guest_memory(vm_fd, ®ion); } =20 +static MshvMemorySlot *find_mem_slot_by_region(uint64_t gpa, uint64_t size, + uint64_t userspace_addr) +{ + MshvMemorySlot *slot; + + for (int i =3D 0; i < MSHV_MAX_MEM_SLOTS; i++) { + slot =3D &mem_slots[i]; + + if (slot->guest_phys_addr =3D=3D gpa && + slot->userspace_addr =3D=3D userspace_addr && + slot->memory_size =3D=3D size) { + trace_mshv_found_slot(slot->userspace_addr, slot->guest_phys_a= ddr, + slot->memory_size); + return slot; + } + } + + return NULL; +} + +static MshvMemorySlot* find_mem_slot_by_gpa(uint64_t gpa) +{ + uint64_t gpa_offset; + MshvMemorySlot *slot; + + trace_mshv_find_slot_by_gpa(gpa); + + for (int i =3D 0; i < MSHV_MAX_MEM_SLOTS; i++) { + slot =3D &mem_slots[i]; + + gpa_offset =3D gpa - slot->guest_phys_addr; + if (slot->guest_phys_addr <=3D gpa && gpa_offset < slot->memory_si= ze) { + trace_mshv_found_slot(slot->userspace_addr, slot->guest_phys_a= ddr, + slot->memory_size); + return slot; + } + } + + return NULL; +} + +MshvRemapResult mshv_remap_overlap_region(int vm_fd, uint64_t gpa) +{ + MshvMemorySlot *gpa_slot, *overlap_slot; + int ret; + + /* return early if no slot is found */ + gpa_slot =3D find_mem_slot_by_gpa(gpa); + if (gpa_slot =3D=3D NULL) { + return MshvRemapNoMapping; + } + + overlap_slot =3D find_overlap_mem_slot(gpa_slot); + if (overlap_slot =3D=3D NULL) { + return MshvRemapNoOverlap; + } + + /* unmap overlapping slot */ + ret =3D map_or_unmap(vm_fd, overlap_slot, false); + if (ret < 0) { + error_report("failed to unmap overlap region"); + abort(); + } + overlap_slot->mapped =3D false; + warn_report("mapped out userspace_addr=3D0x%016lx gpa=3D0x%010lx size= =3D0x%lx", + overlap_slot->userspace_addr, + overlap_slot->guest_phys_addr, + overlap_slot->memory_size); + + /* map region for gpa */ + ret =3D map_or_unmap(vm_fd, gpa_slot, true); + if (ret < 0) { + error_report("failed to map new region"); + abort(); + } + gpa_slot->mapped =3D true; + warn_report("mapped in userspace_addr=3D0x%016lx gpa=3D0x%010lx size= =3D0x%lx", + gpa_slot->userspace_addr, gpa_slot->guest_phys_addr, + gpa_slot->memory_size); + + return MshvRemapOk; +} + static int handle_unmapped_mmio_region_read(uint64_t gpa, uint64_t size, uint8_t *data) { @@ -123,20 +246,106 @@ int mshv_guest_mem_write(uint64_t gpa, const uint8_t= *data, uintptr_t size, return -1; } =20 -static int set_memory(const MshvMemoryRegion *mshv_mr, bool add) +static void clear_slot(MshvMemorySlot *slot) { - int ret =3D 0; + assert(slot); + + *slot =3D (MshvMemorySlot) { 0 }; +} + +static int tracked_unmap(int vm_fd, uint64_t gpa, uint64_t size, + uint64_t userspace_addr) +{ + int ret; + MshvMemorySlot *slot; + + slot =3D find_mem_slot_by_region(gpa, size, userspace_addr); + if (!slot) { + trace_mshv_skip_unset_mem(userspace_addr, gpa, size); + /* no work to do */ + return 0; + } + + if (!slot->mapped) { + /* remove slot, no need to unmap */ + clear_slot(slot); + return 0; + } + + ret =3D map_or_unmap(vm_fd, slot, false); + if (ret < 0) { + error_report("failed to unmap memory region"); + return ret; + } + clear_slot(slot); + + return 0; +} =20 - if (!mshv_mr) { - error_report("Invalid mshv_mr"); +static int tracked_map(int vm_fd, uint64_t gpa, uint64_t size, bool readon= ly, + uint64_t userspace_addr) +{ + MshvMemorySlot *slot, *overlap_slot; + int ret; + + slot =3D find_mem_slot_by_region(gpa, size, userspace_addr); + if (slot) { + error_report("memory region already mapped at gpa=3D0x%lx, " + "userspace_addr=3D0x%lx, size=3D0x%lx", + slot->guest_phys_addr, slot->userspace_addr, + slot->memory_size); + return -1; + } + + slot =3D find_free_mem_slot(); + if (!slot) { + error_report("no free memory slot available"); + return -1; + } + + slot->guest_phys_addr =3D gpa; + slot->userspace_addr =3D userspace_addr; + slot->memory_size =3D size; + slot->readonly =3D readonly; + + overlap_slot =3D find_overlap_mem_slot(slot); + if (overlap_slot) { + trace_mshv_remap_attempt(slot->userspace_addr, + slot->guest_phys_addr, + slot->memory_size); + warn_report("attempt to map region [0x%lx-0x%lx], while " + "[0x%lx-0x%lx] is already mapped in the guest", + userspace_addr, userspace_addr + size - 1, + overlap_slot->userspace_addr, + overlap_slot->userspace_addr + + overlap_slot->memory_size - 1); + + /* do not register mem slot in hv, but record for later swap-in */ + slot->mapped =3D false; + + return 0; + } + + ret =3D map_or_unmap(vm_fd, slot, true); + if (ret < 0) { + error_report("failed to map memory region"); return -1; } + slot->mapped =3D true; =20 - trace_mshv_set_memory(add, mshv_mr->guest_phys_addr, - mshv_mr->memory_size, - mshv_mr->userspace_addr, mshv_mr->readonly, - ret); - return map_or_unmap(mshv_state->vm, mshv_mr, add); + return 0; +} + +static int set_memory(uint64_t gpa, uint64_t size, bool readonly, + uint64_t userspace_addr, bool add) +{ + int vm_fd =3D mshv_state->vm; + + if (add) { + return tracked_map(vm_fd, gpa, size, readonly, userspace_addr); + } + + return tracked_unmap(vm_fd, gpa, size, userspace_addr); } =20 /* @@ -172,9 +381,10 @@ void mshv_set_phys_mem(MshvMemoryListener *mml, Memory= RegionSection *section, bool writable =3D !area->readonly && !area->rom_device; hwaddr start_addr, mr_offset, size; void *ram; - MshvMemoryRegion mshv_mr =3D {0}; =20 - trace_mshv_set_phys_mem(add, section->mr->name); + size =3D align_section(section, &start_addr); + + trace_mshv_set_phys_mem(add, section->mr->name, start_addr); =20 /* If the memory device is a writable non-ram area, we do not * want to map it into the guest memory. If it is not a ROM device, @@ -188,7 +398,6 @@ void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryR= egionSection *section, } } =20 - size =3D align_section(section, &start_addr); if (!size) { return; } @@ -198,14 +407,9 @@ void mshv_set_phys_mem(MshvMemoryListener *mml, Memory= RegionSection *section, =20 ram =3D memory_region_get_ram_ptr(area) + mr_offset; =20 - mshv_mr.guest_phys_addr =3D start_addr; - mshv_mr.memory_size =3D size; - mshv_mr.readonly =3D !writable; - mshv_mr.userspace_addr =3D (uint64_t)ram; - - ret =3D set_memory(&mshv_mr, add); + ret =3D set_memory(start_addr, size, !writable, (uint64_t)ram, add); if (ret < 0) { - error_report("Failed to set memory region"); + error_report("failed to set memory region"); abort(); } } diff --git a/accel/mshv/trace-events b/accel/mshv/trace-events index bade57e22c..efd9dd7b3c 100644 --- a/accel/mshv/trace-events +++ b/accel/mshv/trace-events @@ -20,5 +20,10 @@ mshv_mem_write(uint64_t addr, size_t size) "\tgpa=3D%lx = size=3D%lu" mshv_mem_read(uint64_t addr, size_t size) "\tgpa=3D%lx size=3D%lu" mshv_map_memory(uint64_t userspace_addr, uint64_t gpa, uint64_t size) "\tu= _a=3D%lx gpa=3D%010lx size=3D%08lx" mshv_unmap_memory(uint64_t userspace_addr, uint64_t gpa, uint64_t size) "\= tu_a=3D%lx gpa=3D%010lx size=3D%08lx" -mshv_set_phys_mem(bool add, const char *name) "\tadd=3D%d name=3D%s" +mshv_set_phys_mem(bool add, const char *name, uint64_t gpa) "\tadd=3D%d na= me=3D%s gpa=3D%lx" + +mshv_found_slot(uint64_t userspace_addr, uint64_t gpa, uint64_t size) "\tu= _a=3D%lx gpa=3D%010lx size=3D%08lx" +mshv_skip_unset_mem(uint64_t userspace_addr, uint64_t gpa, uint64_t size) = "\tu_a=3D%lx gpa=3D%010lx size=3D%08lx" +mshv_remap_attempt(uint64_t userspace_addr, uint64_t gpa, uint64_t size) "= \tu_a=3D%lx gpa=3D%010lx size=3D%08lx" +mshv_find_slot_by_gpa(uint64_t gpa) "\tgpa=3D%010lx" mshv_handle_mmio(uint64_t gva, uint64_t gpa, uint64_t size, uint8_t access= _type) "\tgva=3D%lx gpa=3D%010lx size=3D%lx access_type=3D%d" diff --git a/include/system/mshv.h b/include/system/mshv.h index 27d7e3dff3..124da05885 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -38,6 +38,8 @@ typedef struct hyperv_message hv_message; =20 #define MSHV_MSR_ENTRIES_COUNT 64 =20 +#define MSHV_MAX_MEM_SLOTS 32 + #ifdef CONFIG_MSHV_IS_POSSIBLE extern bool mshv_allowed; #define mshv_enabled() (mshv_allowed) @@ -102,6 +104,12 @@ typedef enum MshvVmExit { MshvVmExitHlt =3D 3, } MshvVmExit; =20 +typedef enum MshvRemapResult { + MshvRemapOk =3D 0, + MshvRemapNoMapping =3D 1, + MshvRemapNoOverlap =3D 2, +} MshvRemapResult; + 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); @@ -143,15 +151,15 @@ typedef struct MshvMsrEntries { int mshv_configure_msr(int cpu_fd, const MshvMsrEntry *msrs, size_t n_msrs= ); =20 /* memory */ -typedef struct MshvMemoryRegion { +typedef struct MshvMemorySlot { uint64_t guest_phys_addr; uint64_t memory_size; uint64_t userspace_addr; bool readonly; -} MshvMemoryRegion; + bool mapped; +} MshvMemorySlot; =20 -int mshv_add_mem(int vm_fd, const MshvMemoryRegion *mr); -int mshv_remove_mem(int vm_fd, const MshvMemoryRegion *mr); +MshvRemapResult mshv_remap_overlap_region(int vm_fd, uint64_t gpa); int mshv_guest_mem_read(uint64_t gpa, uint8_t *data, uintptr_t size, bool is_secure_mode, bool instruction_fetch); int mshv_guest_mem_write(uint64_t gpa, const uint8_t *data, uintptr_t size, diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 41a3398ec8..083f161274 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -1073,6 +1073,43 @@ static int handle_mmio(CPUState *cpu, const struct h= yperv_message *msg, return 0; } =20 +static int handle_unmapped_mem(int vm_fd, CPUState *cpu, + const struct hyperv_message *msg, + MshvVmExit *exit_reason) +{ + struct hv_x64_memory_intercept_message info =3D { 0 }; + uint64_t gpa; + int ret; + enum MshvRemapResult remap_result; + + ret =3D set_memory_info(msg, &info); + if (ret < 0) { + error_report("failed to convert message to memory info"); + return -1; + } + + gpa =3D info.guest_physical_address; + + /* attempt to remap the region, in case of overlapping userspace mappi= ngs */ + remap_result =3D mshv_remap_overlap_region(vm_fd, gpa); + *exit_reason =3D MshvVmExitIgnore; + + switch (remap_result) { + case MshvRemapNoMapping: + /* if we didn't find a mapping, it is probably mmio */ + return handle_mmio(cpu, msg, exit_reason); + case MshvRemapOk: + break; + case MshvRemapNoOverlap: + /* This should not happen, but we are forgiving it */ + warn_report("found no overlap for unmapped region"); + *exit_reason =3D MshvVmExitSpecial; + break; + } + + return 0; +} + static int set_ioport_info(const struct hyperv_message *msg, hv_x64_io_port_intercept_message *info) { @@ -1449,6 +1486,12 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_messa= ge *msg, MshvVmExit *exit) case HVMSG_UNRECOVERABLE_EXCEPTION: return MshvVmExitShutdown; case HVMSG_UNMAPPED_GPA: + ret =3D handle_unmapped_mem(vm_fd, cpu, msg, &exit_reason); + if (ret < 0) { + error_report("failed to handle unmapped memory"); + return -1; + } + return exit_reason; case HVMSG_GPA_INTERCEPT: ret =3D handle_mmio(cpu, msg, &exit_reason); if (ret < 0) { --=20 2.34.1 From nobody Sat Nov 15 12:43:53 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=1751391304; cv=none; d=zohomail.com; s=zohoarc; b=nHPxPE0vi/2l6gzZdovCiqKyrV0vN7pTmSZhfmnocV3UFyNx90ffGxo8BHVMauN0xXXowyrq6NtVcGXB1rEF24TaYswQtMNNPZ3VYyxiyF5YeQQiYw+AP+NJiHfS0VxzJVaX3CRA7b/lMhvMDDlgquFsbYVJL0Ri4GFBHqEtHVE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751391304; 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=+Y//l1YbBSWT9WUeIyoAcZdX9+ugwF8RlxPl7j64R+M=; b=LVA+Dzla761v3fcNhIjMOcEJY0rw2mS80V1mzodomfJFQDJSL3ItplQLUzpaK3hkSPPJB1fO/o9HcTTeGdAaFVuQckg1iUUuVQ8sxEhQ4wAi4pGQNzKvMCRdEsU0wNm9vWPaTkNq4b5J5Mrx3U/8U5fbqM51z5VMiRv+LGALuGU= 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 1751391304708828.1532922773623; Tue, 1 Jul 2025 10:35:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWepX-0006zL-Pd; Tue, 01 Jul 2025 13:31:40 -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 1uWep1-0006YL-1D for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:31:08 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uWeov-0007Ji-IA for qemu-devel@nongnu.org; Tue, 01 Jul 2025 13:31:06 -0400 Received: from localhost.localdomain (unknown [167.220.208.67]) by linux.microsoft.com (Postfix) with ESMTPSA id 4EC232119398; Tue, 1 Jul 2025 10:30:47 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4EC232119398 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1751391050; bh=+Y//l1YbBSWT9WUeIyoAcZdX9+ugwF8RlxPl7j64R+M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LRwTvdREwCG6AuL6pDHKagwUPecqGRmAYAXx4ZAecA/N1NYnS5fQQCFgTfcR36Uju ZGVh9lEFMtqB1eBJwf0COfdS7IjHliBaXp+CAHZlFsdLQOGhqdalzwapg05l4R2TV8 M39j5WCxo6Tj1GRkPOMPDj0KffbPGsGfIDjCCIa4= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Cameron Esfahani , Phil Dennis-Jordan , Roman Bolshakov , Thomas Huth , Zhao Liu , Wei Liu , Paolo Bonzini , Wei Liu , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Cornelia Huck , Magnus Kulke , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 27/27] docs: Add mshv to documentation Date: Tue, 1 Jul 2025 19:28:34 +0200 Message-Id: <20250701172834.44849-28-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250701172834.44849-1-magnuskulke@linux.microsoft.com> References: <20250701172834.44849-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: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 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-ZohoMail-DKIM: pass (identity @linux.microsoft.com) X-ZM-MESSAGEID: 1751391305475116600 Content-Type: text/plain; charset="utf-8" Added mshv to the list of accelerators in doc text. Signed-off-by: Magnus Kulke --- docs/devel/codebase.rst | 2 +- qemu-options.hx | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/devel/codebase.rst b/docs/devel/codebase.rst index 2a3143787a..69d8827117 100644 --- a/docs/devel/codebase.rst +++ b/docs/devel/codebase.rst @@ -48,7 +48,7 @@ yet, so sometimes the source code is all you have. * `accel `_: Infrastructure and architecture agnostic code related to the various `accelerators ` supported by QEMU - (TCG, KVM, hvf, whpx, xen, nvmm). + (TCG, KVM, hvf, whpx, xen, nvmm, mshv). Contains interfaces for operations that will be implemented per `target `_. * `audio `_: diff --git a/qemu-options.hx b/qemu-options.hx index 1f862b19a6..a749edff04 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -28,7 +28,7 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \ "-machine [type=3D]name[,prop[=3Dvalue][,...]]\n" " selects emulated machine ('-machine help' for list)\n" " property accel=3Daccel1[:accel2[:...]] selects accele= rator\n" - " supported accelerators are kvm, xen, hvf, nvmm, whpx = or tcg (default: tcg)\n" + " supported accelerators are kvm, xen, hvf, nvmm, whpx,= mshv or tcg (default: tcg)\n" " vmport=3Don|off|auto controls emulation of vmport (de= fault: auto)\n" " dump-guest-core=3Don|off include guest memory in a co= re dump (default=3Don)\n" " mem-merge=3Don|off controls memory merge support (def= ault: on)\n" @@ -65,10 +65,10 @@ SRST =20 ``accel=3Daccels1[:accels2[:...]]`` This is used to enable an accelerator. Depending on the target - architecture, kvm, xen, hvf, nvmm, whpx or tcg can be available. - By default, tcg is used. If there is more than one accelerator - specified, the next one is used if the previous one fails to - initialize. + architecture, kvm, xen, hvf, nvmm, whpx, mshv or tcg can be + available. By default, tcg is used. If there is more than one + accelerator specified, the next one is used if the previous one + fails to initialize. =20 ``vmport=3Don|off|auto`` Enables emulation of VMWare IO port, for vmmouse etc. auto says @@ -221,7 +221,7 @@ ERST =20 DEF("accel", HAS_ARG, QEMU_OPTION_accel, "-accel [accel=3D]accelerator[,prop[=3Dvalue][,...]]\n" - " select accelerator (kvm, xen, hvf, nvmm, whpx or tcg;= use 'help' for a list)\n" + " select accelerator (kvm, xen, hvf, nvmm, whpx, mshv o= r tcg; use 'help' for a list)\n" " igd-passthru=3Don|off (enable Xen integrated Intel gr= aphics passthrough, default=3Doff)\n" " kernel-irqchip=3Don|off|split controls accelerated ir= qchip support (default=3Don)\n" " kvm-shadow-mem=3Dsize of KVM shadow MMU in bytes\n" @@ -236,8 +236,8 @@ DEF("accel", HAS_ARG, QEMU_OPTION_accel, SRST ``-accel name[,prop=3Dvalue[,...]]`` This is used to enable an accelerator. Depending on the target - architecture, kvm, xen, hvf, nvmm, whpx or tcg can be available. By - default, tcg is used. If there is more than one accelerator + architecture, kvm, xen, hvf, nvmm, whpx, mshv or tcg can be available. + By default, tcg is used. If there is more than one accelerator specified, the next one is used if the previous one fails to initialize. =20 --=20 2.34.1