From nobody Sun Nov 29 02:11:46 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1795793456; cv=none; d=zohomail.com; s=zohoarc; b=N6Y/3DxvZWWC21AX6ePbi5WuXHwMqJDWW3D9V+bQxTlFfV4fx1pjJDrjpuGaqMFfxNeXQyOQtUcvBxJ9sCMBbSINAfD19SIVpCFxEqxYlgGQytt0MBWmoVo3xp22ODDyT82fa1lEBNjIfTzeAietN6ZgFEZTjdzLTSz1RNYnlHw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795793456; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=YPiZfyq7QVUD3+7p0OXsQba01iJq1e4+QQLTcv8l6OM=; b=nSjiT/eM9vsnqmR7sY5YY++OSWQLPMafqjNK8GfcjodkMOs5mAuk8hWdpBc4md0TXBvJXNv1mR6zwe+Quj/V0jMgP+t7bYkilgX7yB4lIFMPvzuq0mrX00TmST894+kQrqRv8GbKnXCKLUsMRAR+hG/GQj9pekA3a3knkRglHPE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1795793456427889.3140061458791; Fri, 27 Nov 2026 07:30:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWxuL-0006MG-Oy; Fri, 27 Nov 2026 10:30:41 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWxuD-0006LM-FZ for qemu-devel@nongnu.org; Fri, 27 Nov 2026 10:30:37 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWxuA-0003SR-Ch for qemu-devel@nongnu.org; Fri, 27 Nov 2026 10:30:32 -0500 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.58]) by linux.microsoft.com (Postfix) with ESMTPSA id 1024C20B7166; Fri, 27 Nov 2026 07:30:19 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 1024C20B7166 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1795793421; bh=YPiZfyq7QVUD3+7p0OXsQba01iJq1e4+QQLTcv8l6OM=; h=From:To:Cc:Subject:Date:From; b=UIx1W6yCy9WKornxwYY6JhLJ3MvrrVrZ7KMe+xZ2SBQ9+2GEHiPJsRzPWHlH3hjGL gev1ZCVPh0BGddPFoVlqMbfDv6zRPtR7GPvLHI4YxWMKe0lUNFuSYHXd55Cv0XZ0H0 HwrwTjmoDPvSGjnBAHrkthIww7DcFvJB5ojTjtNM= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Magnus Kulke , Paolo Bonzini Subject: [PATCH] target/i386/mshv: use common emulator for str pio Date: Fri, 27 Nov 2026 16:30:20 +0100 Message-Id: <20261127153020.179180-1-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=magnuskulke@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.microsoft.com) X-ZM-MESSAGEID: 1795793458622158501 Content-Type: text/plain; charset="utf-8" The mshv-local PIO emulation had a bug in which we did not honor a segment-override prefix on OUTS (the handler always read the source from DS:RSI, even when ES:RSI was requested). This was triggered by IDE boot of Windows ISOs. This change switches to the common decoder/emulator for string pio. This resolves the bug and removes the last remaining call to translate_gva() in favor of a userland table walk. Signed-off-by: Magnus Kulke Reviewed-by: Mohamed Mediouni --- target/i386/mshv/mshv-cpu.c | 328 +++++++++++------------------------- 1 file changed, 98 insertions(+), 230 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index c145bbc447..2465267a54 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -28,6 +28,7 @@ #include "emulate/x86_decode.h" #include "emulate/x86_emu.h" #include "emulate/x86_flags.h" +#include "emulate/x86_mmu.h" #include "gdbstub/enums.h" =20 #include "accel/accel-cpu-target.h" @@ -309,47 +310,6 @@ static int get_xc_reg(CPUState *cpu) return 0; } =20 -static int translate_gva(const CPUState *cpu, uint64_t gva, uint64_t *gpa, - uint64_t flags) -{ - int ret; - int cpu_fd =3D mshv_vcpufd(cpu); - int vp_index =3D cpu->cpu_index; - - hv_input_translate_virtual_address in =3D { 0 }; - hv_output_translate_virtual_address out =3D { 0 }; - struct mshv_root_hvcall args =3D {0}; - uint64_t gva_page =3D gva >> HV_HYP_PAGE_SHIFT; - - in.vp_index =3D vp_index; - in.control_flags =3D flags; - in.gva_page =3D gva_page; - - /* create the hvcall envelope */ - args.code =3D HVCALL_TRANSLATE_VIRTUAL_ADDRESS; - args.in_sz =3D sizeof(in); - args.in_ptr =3D (uint64_t) ∈ - args.out_sz =3D sizeof(out); - args.out_ptr =3D (uint64_t) &out; - - /* perform the call */ - ret =3D mshv_hvcall(cpu_fd, &args); - if (ret < 0) { - error_report("Failed to invoke gva->gpa translation"); - return -errno; - } - - if (out.translation_result.result_code !=3D HV_TRANSLATE_GVA_SUCCESS) { - error_report("Failed to translate gva (" TARGET_FMT_lx ") to gpa",= gva); - return -1; - } - - *gpa =3D ((out.gpa_page << HV_HYP_PAGE_SHIFT) - | (gva & ~(uint64_t)HV_HYP_PAGE_MASK)); - - return 0; -} - int mshv_set_generic_regs(const CPUState *cpu, const hv_register_assoc *as= socs, size_t n_regs) { @@ -1711,27 +1671,55 @@ static inline MemTxAttrs get_mem_attrs(bool is_secu= re_mode) return memattr; } =20 -static void pio_read(uint64_t port, uint8_t *data, uintptr_t size, - bool is_secure_mode) +static int pio_read(uint16_t port, uint8_t *data, uintptr_t size, + bool is_secure_mode) { - int ret =3D 0; + int ret; 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(); + error_report("Failed to read from port %x: %d", port, ret); + return -1; } + + return 0; } =20 -static int pio_write(uint64_t port, const uint8_t *data, uintptr_t size, +static int pio_write(uint16_t port, const uint8_t *data, uintptr_t size, bool is_secure_mode) { - int ret =3D 0; + int ret; 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; + if (ret !=3D MEMTX_OK) { + error_report("Failed to write to port %x: %d", port, ret); + return -1; + } + + return 0; +} + +static void handle_io(CPUState *cpu, uint16_t port, void *buffer, + int direction, int size, int count) +{ + uint8_t *data =3D buffer; + int i, ret; + MemTxAttrs memattr =3D {0}; + bool write =3D (direction !=3D 0); + + for (i =3D 0; i < count; i++, data +=3D size) { + ret =3D address_space_rw(&address_space_io, port, memattr, (void *= )data, + size, write); + if (ret !=3D MEMTX_OK) { + error_report("Failed to %s port %x: %d", + write ? "write to" : "read from", port, ret); + abort(); + } + } } =20 static int handle_pio_non_str(CPUState *cpu, @@ -1812,202 +1800,68 @@ static int handle_pio_non_str(CPUState *cpu, return 0; } =20 -static int read_memory(const CPUState *cpu, uint64_t initial_gva, - uint64_t initial_gpa, uint64_t gva, uint8_t *data, - size_t len) +static int handle_pio_str(CPUState *cpu, hv_x64_io_port_intercept_message = *info) { - int ret; - uint64_t gpa, flags; - uint64_t cur_gva =3D gva; - size_t page_left, chunk; - uint8_t *cur_data =3D data; - - /* - * If the read spans multiple pages, - * we need to translate and read each page separately - */ - while (len > 0) { - page_left =3D HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1)= ); - chunk =3D MIN(len, page_left); - - if (cur_gva =3D=3D initial_gva) { - gpa =3D initial_gpa; - } else { - flags =3D HV_TRANSLATE_GVA_VALIDATE_READ; - ret =3D translate_gva(cpu, cur_gva, &gpa, flags); - if (ret < 0) { - return -1; - } - } - - ret =3D mshv_guest_mem_read(gpa, cur_data, chunk, - false, false); - if (ret < 0) { - error_report("failed to read guest mem"); - return -1; - } + uint8_t access_type =3D info->header.intercept_access_type; + size_t insn_len =3D info->header.instruction_length; + bool success; + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + struct x86_decode decode =3D { 0 }; + x86_insn_stream stream =3D { + .bytes =3D info->instruction_bytes, + .len =3D insn_len, + }; =20 - cur_gva +=3D chunk; - cur_data +=3D chunk; - len -=3D chunk; + if (info->access_info.access_size !=3D 1 && + info->access_info.access_size !=3D 2 && + info->access_info.access_size !=3D 4) { + error_report("Invalid string pio access size: %u", + info->access_info.access_size); + return -1; } =20 - return 0; -} - -static int write_memory(const CPUState *cpu, uint64_t gva, const uint8_t *= data, - size_t len) -{ - int ret; - uint64_t gpa, flags; - uint64_t cur_gva =3D gva; - size_t page_left, chunk; - const uint8_t *cur_data =3D data; - - /* - * If the write spans multiple pages, - * we need to translate and write each page separately - */ - while (len > 0) { - page_left =3D HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1)= ); - chunk =3D MIN(len, page_left); - - flags =3D HV_TRANSLATE_GVA_VALIDATE_WRITE; - ret =3D translate_gva(cpu, cur_gva, &gpa, flags); - if (ret < 0) { - error_report("failed to translate gva to gpa"); - return -1; - } - - ret =3D mshv_guest_mem_write(gpa, cur_data, chunk, false); - if (ret !=3D MEMTX_OK) { - error_report("failed to write to mmio"); - return -1; - } - - cur_gva +=3D chunk; - cur_data +=3D chunk; - len -=3D chunk; + if (insn_len =3D=3D 0 || + insn_len > info->instruction_byte_count || + info->instruction_byte_count > sizeof(info->instruction_bytes)) { + error_report("Invalid string pio instruction length: %zu", insn_le= n); + return -1; } =20 - 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; - - src =3D linear_addr(cpu, info->rsi, R_DS); + load_regs(cpu); =20 - for (size_t i =3D 0; i < repeat; i++) { - ret =3D read_memory(cpu, 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; + if (decode_instruction_stream(env, &decode, &stream) !=3D insn_len) { + error_report("String pio instruction length mismatch: " + "decoded %d, expected %zu", decode.len, insn_len); + return -1; } =20 - 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 }; - - 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, 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; + if (access_type =3D=3D HV_X64_INTERCEPT_ACCESS_TYPE_WRITE + ? decode.cmd !=3D X86_DECODE_CMD_OUTS + : decode.cmd !=3D X86_DECODE_CMD_INS) { + error_report("String pio direction mismatch"); + return -1; } =20 - 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; - uint64_t rip; - bool direction_flag; - uint32_t reg_names[3]; - uint64_t reg_values[3]; - int ret; - X86CPU *x86_cpu =3D X86_CPU(cpu); - CPUX86State *env =3D &x86_cpu->env; - - load_regs(cpu); - - 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); - if (ret < 0) { - error_report("Failed to handle pio str read"); - return -1; - } - reg_names[0] =3D HV_X64_REGISTER_RDI; - reg_values[0] =3D info->rdi; + if (decode.operand_size !=3D info->access_info.access_size) { + error_report("String pio access size mismatch: decoded %d, expecte= d %u", + decode.operand_size, info->access_info.access_size); + return -1; } =20 - rip =3D info->header.rip + insn_len; + if (!!decode.rep !=3D !!info->access_info.rep_prefix) { + error_report("String pio REP prefix mismatch"); + return -1; + } =20 - if (cpu->vcpu_dirty) { - env->eip =3D rip; - if (access_type =3D=3D HV_X64_INTERCEPT_ACCESS_TYPE_WRITE) { - env->regs[R_ESI] =3D info->rsi; - } else { - env->regs[R_EDI] =3D info->rdi; - } - /* Sync modified standard registers back and clear dirty. */ - store_regs(cpu); - cpu->vcpu_dirty =3D false; - } else { - reg_names[1] =3D HV_X64_REGISTER_RIP; - reg_values[1] =3D rip; + success =3D exec_instruction(env, &decode); + store_regs(cpu); + cpu->vcpu_dirty =3D false; =20 - ret =3D set_x64_registers(cpu, reg_names, reg_values); - if (ret < 0) { - error_report("Failed to set x64 registers"); - return -1; - } + if (!success) { + error_report("Failed to emulate string pio at " TARGET_FMT_lx, + info->header.rip); + return -1; } =20 return 0; @@ -2290,8 +2144,22 @@ static void read_segment_descriptor(CPUState *cpu, x86_set_segment_base(desc, seg->base); } =20 +static bool is_user_mode(CPUState *cpu) +{ + CPUX86State *env =3D &X86_CPU(cpu)->env; + + /* If the guest is in real mode, it is not user mode. */ + if (!(env->cr[0] & CR0_PG_MASK)) { + return false; + } + + return (env->segs[R_CS].selector & 3) =3D=3D 3 || (env->eflags & VM_MA= SK); +} + static const struct x86_emul_ops mshv_x86_emul_ops =3D { .read_segment_descriptor =3D read_segment_descriptor, + .is_user_mode =3D is_user_mode, + .handle_io =3D handle_io, }; =20 void mshv_init_mmio_emu(void) --=20 2.34.1