[{"id":3678685,"web_url":"http://patchwork.ozlabs.org/comment/3678685/","msgid":"<B59D4B8F-2F54-4E64-9133-C5A4675BFA21@unpredictable.fr>","list_archive_url":null,"date":"2026-04-17T11:56:47","subject":"Re: [PATCH 30/34] target/i386: add de/compaction to xsave_helper","submitter":{"id":91318,"url":"http://patchwork.ozlabs.org/api/people/91318/","name":"Mohamed Mediouni","email":"mohamed@unpredictable.fr"},"content":"> On 17. Apr 2026, at 12:56, Magnus Kulke <magnuskulke@linux.microsoft.com> wrote:\n> \n> HyperV use XSAVES which stores extended state in compacted format in\n> which components are packed contiguously, while QEMU's internal XSAVE\n> representation use the standard format in which each component is places\n> at a fixed offset. Hence for this purpose we add two conversion fn's to\n> the xsave helper to roundtrip XSAVE state in a migration.\n> \n> - decompact_xsave_area(): converts compacted format to standard.\n>  XSTATE_BV is masked to host XCR0 since IA32_XSS is managed\n>  by the hypervisor.\n> \n> - compact_xsave_area(): converts standard format back to compacted\n>  format. XCOMP_BV is set from the host's CPUID 0xD.0 rather than the\n>  guest's XCR0, as this is what the hypervisor expects.\n> \n> Both functions use the host's CPUID leaf 0xD subleaves to determine component\n> sizes, offsets, and alignment requirements.\n> \n> There are situations when the host advertises features that we want to\n> disable for the guest, e.g. AMX TILE. In this case we cannot rely on the\n> host's xcr0, but instead we use the feature mask that has been generated\n> in as part of the CPU realization process (x86_cpu_expand_features).\n> \n> Signed-off-by: Magnus Kulke <magnuskulke@linux.microsoft.com>\n> ---\n> target/i386/cpu.h          |   2 +\n> target/i386/xsave_helper.c | 255 +++++++++++++++++++++++++++++++++++++\n> 2 files changed, 257 insertions(+)\n\nReviewed-by: Mohamed Mediouni <mohamed@unpredictable.fr>\n> \n> diff --git a/target/i386/cpu.h b/target/i386/cpu.h\n> index 4ad4a35ce9..cd5d5a5369 100644\n> --- a/target/i386/cpu.h\n> +++ b/target/i386/cpu.h\n> @@ -3033,6 +3033,8 @@ void x86_cpu_xrstor_all_areas(X86CPU *cpu, const void *buf, uint32_t buflen);\n> void x86_cpu_xsave_all_areas(X86CPU *cpu, void *buf, uint32_t buflen);\n> uint32_t xsave_area_size(uint64_t mask, bool compacted);\n> void x86_update_hflags(CPUX86State* env);\n> +int decompact_xsave_area(const void *buf, size_t buflen, CPUX86State *env);\n> +int compact_xsave_area(CPUX86State *env, void *buf, size_t buflen);\n> \n> static inline bool hyperv_feat_enabled(X86CPU *cpu, int feat)\n> {\n> diff --git a/target/i386/xsave_helper.c b/target/i386/xsave_helper.c\n> index bab2258732..2272b83f5f 100644\n> --- a/target/i386/xsave_helper.c\n> +++ b/target/i386/xsave_helper.c\n> @@ -3,6 +3,7 @@\n>  * See the COPYING file in the top-level directory.\n>  */\n> #include \"qemu/osdep.h\"\n> +#include \"qemu/error-report.h\"\n> \n> #include \"cpu.h\"\n> \n> @@ -293,3 +294,257 @@ void x86_cpu_xrstor_all_areas(X86CPU *cpu, const void *buf, uint32_t buflen)\n>     }\n> #endif\n> }\n> +\n> +#define XSTATE_BV_IN_HDR  offsetof(X86XSaveHeader, xstate_bv)\n> +#define XCOMP_BV_IN_HDR   offsetof(X86XSaveHeader, xcomp_bvo)\n> +\n> +typedef struct X86XSaveAreaView {\n> +    /* 512 bytes */\n> +    X86LegacyXSaveArea legacy;\n> +    /* 64 bytes */\n> +    X86XSaveHeader     header;\n> +    /* ...followed by individual xsave areas */\n> +} X86XSaveAreaView;\n> +\n> +#define XSAVE_XSTATE_BV_OFFSET  offsetof(X86XSaveAreaView, header.xstate_bv)\n> +#define XSAVE_XCOMP_BV_OFFSET   offsetof(X86XSaveAreaView, header.xcomp_bv)\n> +#define XSAVE_EXT_OFFSET        (sizeof(X86LegacyXSaveArea) + \\\n> +                                 sizeof(X86XSaveHeader))\n> +\n> +/**\n> + * decompact_xsave_area - Convert compacted XSAVE format to standard format\n> + * @buf: Source buffer containing compacted XSAVE data\n> + * @buflen: Size of source buffer\n> + * @env: CPU state where the standard format buffer will be written to\n> + *\n> + * Accelerator backends like MSHV might return XSAVE state in compacted format\n> + * (XSAVEC). The state components have to be packed contiguously without gaps.\n> + * The XSAVE qemu buffers are in standard format where each component has a\n> + * fixed offset.\n> + *\n> + * Returns: 0 on success, negative errno on failure\n> + */\n> +int decompact_xsave_area(const void *buf, size_t buflen, CPUX86State *env)\n> +{\n> +    uint64_t compacted_xstate_bv, compacted_xcomp_bv, compacted_layout_bv;\n> +    uint64_t xsave_offset, *xcomp_bv;\n> +    size_t i;\n> +    uint32_t eax, ebx, ecx, edx;\n> +    uint32_t size, dst_off;\n> +    bool align64;\n> +    uint64_t guest_xcr0, *xstate_bv;\n> +\n> +    compacted_xstate_bv = *(uint64_t *)(buf + XSAVE_XSTATE_BV_OFFSET);\n> +    compacted_xcomp_bv  = *(uint64_t *)(buf + XSAVE_XCOMP_BV_OFFSET);\n> +\n> +    /* This function only handles compacted format (bit 63 set) */\n> +    assert((compacted_xcomp_bv >> 63) & 1);\n> +\n> +    /* Low bits of XCOMP_BV describe which components are in the layout */\n> +    compacted_layout_bv = compacted_xcomp_bv & ~(1ULL << 63);\n> +\n> +    /* Zero out buffer, then copy legacy region (FP + SSE) and header as-is */\n> +    memset(env->xsave_buf, 0, env->xsave_buf_len);\n> +    memcpy(env->xsave_buf, buf, XSAVE_EXT_OFFSET);\n> +\n> +    /*\n> +     * We mask XSTATE_BV with the guest's supported XCR0 because:\n> +     * 1. Supervisor state (IA32_XSS) is hypervisor-managed, we don't use\n> +     *    this state for migration.\n> +     * 2. Features disabled at partition creation (e.g. AMX) must be excluded\n> +     */\n> +    guest_xcr0 = ((uint64_t)env->features[FEAT_XSAVE_XCR0_HI] << 32) |\n> +                 env->features[FEAT_XSAVE_XCR0_LO];\n> +    xstate_bv = (uint64_t *)(env->xsave_buf + XSAVE_XSTATE_BV_OFFSET);\n> +    *xstate_bv &= guest_xcr0;\n> +\n> +    /* Clear bit 63 - output is standard format, not compacted */\n> +    xcomp_bv = (uint64_t *)(env->xsave_buf + XSAVE_XCOMP_BV_OFFSET);\n> +    *xcomp_bv = *xcomp_bv & ~(1ULL << 63);\n> +\n> +    /*\n> +     * Process each extended state component in the compacted layout.\n> +     * Components 0 and 1 (FP and SSE) are in the legacy region, so we\n> +     * start at component 2. For each component:\n> +     * - Calculate its offset in the compacted source (contiguous layout)\n> +     * - Get its fixed offset in the standard destination from CPUID\n> +     * - Copy if the component has non-init state (bit set in XSTATE_BV)\n> +     */\n> +    xsave_offset = XSAVE_EXT_OFFSET;\n> +    for (i = 2; i < 63; i++) {\n> +        if (((compacted_layout_bv >> i) & 1) == 0) {\n> +            continue;\n> +        }\n> +\n> +        /* Query guest CPUID for this component's size and standard offset */\n> +        cpu_x86_cpuid(env, 0xD, i, &eax, &ebx, &ecx, &edx);\n> +\n> +        size = eax;\n> +        dst_off = ebx;\n> +        align64 = (ecx & (1u << 1)) != 0;\n> +\n> +        /* Component is in the layout but unknown to the guest CPUID model */\n> +        if (size == 0) {\n> +            /*\n> +             * The hypervisor might expose a component that has no\n> +             * representation in the guest CPUID model. We query the host to\n> +             * retrieve the size of the component, so we can skip over it.\n> +             */\n> +            host_cpuid(0xD, i, &eax, &ebx, &ecx, &edx);\n> +            size = eax;\n> +            align64 = (ecx & (1u << 1)) != 0;\n> +            if (size == 0) {\n> +                error_report(\"xsave component %zu: size unknown to both \"\n> +                             \"guest and host CPUID\", i);\n> +                return -EINVAL;\n> +            }\n> +\n> +            if (align64) {\n> +                xsave_offset = QEMU_ALIGN_UP(xsave_offset, 64);\n> +            }\n> +\n> +            if (xsave_offset + size > buflen) {\n> +                error_report(\"xsave component %zu overruns source buffer: \"\n> +                             \"offset=%zu size=%u buflen=%zu\",\n> +                             i, xsave_offset, size, buflen);\n> +                return -E2BIG;\n> +            }\n> +\n> +            xsave_offset += size;\n> +            continue;\n> +        }\n> +\n> +        if (align64) {\n> +            xsave_offset = QEMU_ALIGN_UP(xsave_offset, 64);\n> +        }\n> +\n> +        if ((xsave_offset + size) > buflen) {\n> +            error_report(\"xsave component %zu overruns source buffer: \"\n> +                         \"offset=%zu size=%u buflen=%zu\",\n> +                         i, xsave_offset, size, buflen);\n> +            return -E2BIG;\n> +        }\n> +\n> +        if ((dst_off + size) > env->xsave_buf_len) {\n> +            error_report(\"xsave component %zu overruns destination buffer: \"\n> +                         \"offset=%u size=%u buflen=%zu\",\n> +                         i, dst_off, size, (size_t)env->xsave_buf_len);\n> +            return -E2BIG;\n> +        }\n> +\n> +        /* Copy components marked present in XSTATE_BV to guest model */\n> +        if (((compacted_xstate_bv >> i) & 1) != 0) {\n> +            memcpy(env->xsave_buf + dst_off, buf + xsave_offset, size);\n> +        }\n> +\n> +        xsave_offset += size;\n> +    }\n> +\n> +    return 0;\n> +}\n> +\n> +/**\n> + * compact_xsave_area - Convert standard XSAVE format to compacted format\n> + * @env: CPU state containing the standard format XSAVE buffer\n> + * @buf: Destination buffer for compacted XSAVE data (to send to hypervisor)\n> + * @buflen: Size of destination buffer\n> + *\n> + * Accelerator backends like MSHV might expect XSAVE state in compacted format\n> + * (XSAVEC). The state components are packed contiguously without gaps.\n> + * The XSAVE qemu buffers are in standard format where each component has a\n> + * fixed offset.\n> + *\n> + * This function converts from standard to compacted format, it accepts a\n> + * pre-allocated destination buffer of sufficient size, it is the\n> + * responsibility of the caller to ensure the buffer is big enough.\n> + *\n> + * Returns: total size of compacted XSAVE data written to @buf\n> + */\n> +int compact_xsave_area(CPUX86State *env, void *buf, size_t buflen)\n> +{\n> +    uint64_t *xcomp_bv;\n> +    size_t i;\n> +    uint32_t eax, ebx, ecx, edx;\n> +    uint32_t size, src_off;\n> +    bool align64;\n> +    size_t compact_offset;\n> +    uint64_t host_xcr0_mask, guest_xcr0;\n> +\n> +    /* Zero out buffer, then copy legacy region (FP + SSE) and header as-is */\n> +    memset(buf, 0, buflen);\n> +    memcpy(buf, env->xsave_buf, XSAVE_EXT_OFFSET);\n> +\n> +    /*\n> +     * Set XCOMP_BV to indicate compacted format (bit 63) and which\n> +     * components are in the layout.\n> +     *\n> +     * We must explicitly set XCOMP_BV because x86_cpu_xsave_all_areas()\n> +     * produces standard format with XCOMP_BV=0 (buffer is zeroed and only\n> +     * XSTATE_BV is set in the header).\n> +     *\n> +     * XCOMP_BV must reflect the partition's XSAVE capability, not the\n> +     * guest's current XCR0 (env->xcr0). These differ b/c:\n> +     * - A guest's XCR0 is what the guest OS has enabled via XSETBV\n> +     * - The partition's XCR0 mask is the hypervisor's save/restore capability\n> +     *\n> +     * The hypervisor uses XSAVES which saves based on its capability, so the\n> +     * XCOMP_BV value in the buffer we send back must match that capability.\n> +     *\n> +     * We intersect the host XCR0 with the guest's supported XCR0 features\n> +     * (FEAT_XSAVE_XCR0_*) so that features disabled at partition creation\n> +     * (e.g. AMX) are excluded from the compacted layout.\n> +     */\n> +    host_cpuid(0xD, 0, &eax, &ebx, &ecx, &edx);\n> +    host_xcr0_mask = ((uint64_t)edx << 32) | eax;\n> +    guest_xcr0 = ((uint64_t)env->features[FEAT_XSAVE_XCR0_HI] << 32) |\n> +                 env->features[FEAT_XSAVE_XCR0_LO];\n> +    host_xcr0_mask &= guest_xcr0;\n> +    xcomp_bv = buf + XSAVE_XCOMP_BV_OFFSET;\n> +    *xcomp_bv = host_xcr0_mask | (1ULL << 63);\n> +\n> +    /*\n> +     * Process each extended state component in the host's XCR0.\n> +     * The compacted layout must match XCOMP_BV (host capability).\n> +     *\n> +     * For each component:\n> +     * - Get its size and standard offset from host CPUID\n> +     * - Apply 64-byte alignment if required\n> +     * - Copy data only if guest has this component (bit set in env->xcr0)\n> +     * - Always advance offset to maintain correct layout\n> +     */\n> +    compact_offset = XSAVE_EXT_OFFSET;\n> +    for (i = 2; i < 63; i++) {\n> +        if (!((host_xcr0_mask >> i) & 1)) {\n> +            continue;\n> +        }\n> +\n> +        /* Query host CPUID for this component's size and standard offset */\n> +        host_cpuid(0xD, i, &eax, &ebx, &ecx, &edx);\n> +        size = eax;\n> +        src_off = ebx;\n> +        align64 = (ecx >> 1) & 1;\n> +\n> +        if (size == 0) {\n> +            /* Component in host xcr0 but unknown - shouldn't happen */\n> +            continue;\n> +        }\n> +\n> +        /* Apply 64-byte alignment if required by this component */\n> +        if (align64) {\n> +            compact_offset = QEMU_ALIGN_UP(compact_offset, 64);\n> +        }\n> +\n> +        /*\n> +         * Only copy data if guest has this component enabled in XCR0.\n> +         * Otherwise the component remains zeroed (init state), but we\n> +         * still advance the offset to maintain the correct layout.\n> +         */\n> +        if ((env->xcr0 >> i) & 1) {\n> +            memcpy(buf + compact_offset, env->xsave_buf + src_off, size);\n> +        }\n> +\n> +        compact_offset += size;\n> +    }\n> +\n> +    return compact_offset;\n> +}\n> -- \n> 2.34.1\n> \n>","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=unpredictable.fr header.i=@unpredictable.fr\n header.a=rsa-sha256 header.s=sig1 header.b=YvZuZbjj;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists1p.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fxtfV0YrTz1yD3\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 17 Apr 2026 21:57:22 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists1p.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wDhop-0002aT-Qq; Fri, 17 Apr 2026 07:57:07 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <mohamed@unpredictable.fr>)\n id 1wDhoo-0002Zn-Be\n for qemu-devel@nongnu.org; Fri, 17 Apr 2026 07:57:06 -0400","from p-east2-cluster6-host9-snip4-7.eps.apple.com ([57.103.76.198]\n helo=outbound.st.icloud.com)\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <mohamed@unpredictable.fr>)\n id 1wDhom-0002bq-1R\n for qemu-devel@nongnu.org; Fri, 17 Apr 2026 07:57:06 -0400","from outbound.st.icloud.com (unknown [127.0.0.2])\n by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPS id\n 9DF90180056F; Fri, 17 Apr 2026 11:57:02 +0000 (UTC)","from smtpclient.apple (unknown [17.42.251.67])\n by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPSA id\n BBF15180055C; Fri, 17 Apr 2026 11:56:59 +0000 (UTC)"],"Dkim-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=unpredictable.fr;\n s=sig1; t=1776427023; x=1779019023;\n bh=V0LxJmOsNpdFCb1pHr4vpWVXoQ4Zmstpf0VC1VoGZJk=;\n h=Content-Type:Mime-Version:Subject:From:Date:Message-Id:To:x-icloud-hme;\n b=YvZuZbjjQtEWoIKffCt7k1+lRquPRNh2ly0CJba4d9VqFwTwVdn+wZDBVS/UXA3u1QGvCWjMan5fEiOrbZjWAr9/zn6ndoAg/TUD25paHTcTmy0kIJR82huotAqwy4X1xglPaaAYlLRJXfhZjYW/xJt9ZdGsAOLMhwJL6XGhkAcJ92joMxtSxErufBLL36tcuYYnNhqtHjt+LTycOhMAadz2Wh1Rfp8XJnyolU4TeVYz8ZFvOtJ01IeM8cHK/8BPlE4EjDwoVrzteICT5XZD6hZ8whjwW9caxvX0+8Yk5Wq1toIDnuKIkbscwWdE6iq5S2yRcU5zBxy2GS5zDFfDzg==","mail-alias-created-date":"1752046281608","Content-Type":"text/plain;\n\tcharset=us-ascii","Mime-Version":"1.0 (Mac OS X Mail 16.0 \\(3864.500.181\\))","Subject":"Re: [PATCH 30/34] target/i386: add de/compaction to xsave_helper","From":"Mohamed Mediouni <mohamed@unpredictable.fr>","In-Reply-To":"<20260417105618.3621-31-magnuskulke@linux.microsoft.com>","Date":"Fri, 17 Apr 2026 13:56:47 +0200","Cc":"qemu-devel@nongnu.org, kvm@vger.kernel.org,\n Magnus Kulke <magnuskulke@microsoft.com>, Wei Liu <liuwe@microsoft.com>,\n \"Michael S. Tsirkin\" <mst@redhat.com>,\n =?utf-8?q?C=C3=A9dric_Le_Goater?= <clg@redhat.com>,\n Zhao Liu <zhao1.liu@intel.com>,\n Richard Henderson <richard.henderson@linaro.org>,\n Paolo Bonzini <pbonzini@redhat.com>, Wei Liu <wei.liu@kernel.org>,\n Alex Williamson <alex@shazbot.org>,\n Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, =?utf-8?q?Philippe_Mathieu-D?=\n\t=?utf-8?q?aud=C3=A9?= <philmd@linaro.org>,\n Marcelo Tosatti <mtosatti@redhat.com>","Content-Transfer-Encoding":"quoted-printable","Message-Id":"<B59D4B8F-2F54-4E64-9133-C5A4675BFA21@unpredictable.fr>","References":"<20260417105618.3621-1-magnuskulke@linux.microsoft.com>\n <20260417105618.3621-31-magnuskulke@linux.microsoft.com>","To":"Magnus Kulke <magnuskulke@linux.microsoft.com>","X-Mailer":"Apple Mail (2.3864.500.181)","X-Proofpoint-ORIG-GUID":"W5kzSCG9QsEMjadmKQusX76CscdvavML","X-Authority-Info-Out":"v=2.4 cv=OdmVzxTY c=1 sm=1 tr=0 ts=69e2200f\n cx=c_apl:c_pps:t_out a=YrL12D//S6tul8v/L+6tKg==:117\n a=YrL12D//S6tul8v/L+6tKg==:17 a=kj9zAlcOel0A:10 a=A5OVakUREuEA:10\n a=VkNPw1HP01LnGYTKEx00:22 a=yMhMjlubAAAA:8 a=m3H-yIfUdCiYWo78rAoA:9\n a=CjuIK1q_8ugA:10","X-Proofpoint-GUID":"W5kzSCG9QsEMjadmKQusX76CscdvavML","X-Proofpoint-Spam-Details-Enc":"AW1haW4tMjYwNDE3MDExOCBTYWx0ZWRfX2Bui94xGjpeb\n wPXct1TQ82snC7Q2IFfUnoGB22tsO6NUClVqDTCzk4DvuyhWy421f+vTtG9O2zYOUGOY8qpbpfd\n Aafrrblj69IkG/oq4u3dSiTWC1QYMldPI1CBFqWbS21ETTCAemmCHjHSV0JVeydpnht/AAQoT2k\n XpjTJG25hDu/V5ziq9seheavnRxN5gCrY+TPqy80GztQw6ExL6gMqLtK7Q6YlXwNxEKS22JYa4p\n 0nvJabKosQBgTQLlI4jzR9x/w1O8HTHVBhKfNpHJu9nfV9V5jbm5+JhMkB/LtuKWl4Y1IPO4l5s\n SQDDko6EiSak/4paPbR6EZ1BYTrnGRLLLillUWj5LU2SScFW37rm+TPyPMiSIs=","X-Proofpoint-Virus-Version":"vendor=baseguard\n engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49\n definitions=2026-04-17_01,2026-04-17_01,2025-10-01_01","X-Proofpoint-Spam-Details":"rule=notspam policy=default score=0 suspectscore=0\n adultscore=0 spamscore=0 mlxscore=0 phishscore=0\n clxscore=1030\n lowpriorityscore=0 malwarescore=0 mlxlogscore=999 bulkscore=0 classifier=spam\n authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000\n definitions=main-2604170118","Received-SPF":"pass client-ip=57.103.76.198;\n envelope-from=mohamed@unpredictable.fr; helo=outbound.st.icloud.com","X-Spam_score_int":"-20","X-Spam_score":"-2.1","X-Spam_bar":"--","X-Spam_report":"(-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001,\n SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"qemu development <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}},{"id":3678987,"web_url":"http://patchwork.ozlabs.org/comment/3678987/","msgid":"<83C168E7-02D5-4CD9-9A27-545A88942171@unpredictable.fr>","list_archive_url":null,"date":"2026-04-18T17:46:25","subject":"Re: [PATCH 30/34] target/i386: add de/compaction to xsave_helper","submitter":{"id":91318,"url":"http://patchwork.ozlabs.org/api/people/91318/","name":"Mohamed Mediouni","email":"mohamed@unpredictable.fr"},"content":"> On 17. Apr 2026, at 12:56, Magnus Kulke <magnuskulke@linux.microsoft.com> wrote:\n> \n> HyperV use XSAVES which stores extended state in compacted format in\n> which components are packed contiguously, while QEMU's internal XSAVE\n> representation use the standard format in which each component is places\n> at a fixed offset. Hence for this purpose we add two conversion fn's to\n> the xsave helper to roundtrip XSAVE state in a migration.\n> \n> - decompact_xsave_area(): converts compacted format to standard.\n>  XSTATE_BV is masked to host XCR0 since IA32_XSS is managed\n>  by the hypervisor.\n> \n> - compact_xsave_area(): converts standard format back to compacted\n>  format. XCOMP_BV is set from the host's CPUID 0xD.0 rather than the\n>  guest's XCR0, as this is what the hypervisor expects.\n> \n> Both functions use the host's CPUID leaf 0xD subleaves to determine component\n> sizes, offsets, and alignment requirements.\n> \n> There are situations when the host advertises features that we want to\n> disable for the guest, e.g. AMX TILE. In this case we cannot rely on the\n> host's xcr0, but instead we use the feature mask that has been generated\n> in as part of the CPU realization process (x86_cpu_expand_features).\n> \n> Signed-off-by: Magnus Kulke <magnuskulke@linux.microsoft.com>\n\nHi,\n\nThis patch has a tiny problem.\n\n#if defined(CONFIG_KVM) || defined(CONFIG_HVF)\nvoid *xsave_buf;\nuint32_t xsave_buf_len;\n#endif\n\nWith the \"#if defined(CONFIG_KVM) || defined(CONFIG_HVF)” removed,\nthat fixes a TCG only QEMU build.\n\nWas also able to test that this works as expected on WHPX.","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=unpredictable.fr header.i=@unpredictable.fr\n header.a=rsa-sha256 header.s=sig1 header.b=eVHuX7c1;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists1p.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fyfN55Cqjz1yGt\n\tfor <incoming@patchwork.ozlabs.org>; Sun, 19 Apr 2026 03:47:31 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists1p.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wE9kn-00057n-Ei; Sat, 18 Apr 2026 13:46:49 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <mohamed@unpredictable.fr>)\n id 1wE9km-00057e-5l\n for qemu-devel@nongnu.org; Sat, 18 Apr 2026 13:46:48 -0400","from p-west2-cluster5-host8-snip4-10.eps.apple.com ([57.103.71.83]\n helo=outbound.mr.icloud.com)\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <mohamed@unpredictable.fr>)\n id 1wE9kj-00008n-Rs\n for qemu-devel@nongnu.org; Sat, 18 Apr 2026 13:46:47 -0400","from outbound.mr.icloud.com (unknown [127.0.0.2])\n by p00-icloudmta-asmtp-us-west-2a-20-percent-0 (Postfix) with ESMTPS id\n 3178A1800155; Sat, 18 Apr 2026 17:46:39 +0000 (UTC)","from smtpclient.apple (unknown [17.57.152.38])\n by p00-icloudmta-asmtp-us-west-2a-20-percent-0 (Postfix) with ESMTPSA id\n 34E1D18000B5; Sat, 18 Apr 2026 17:46:37 +0000 (UTC)"],"Dkim-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=unpredictable.fr;\n s=sig1; t=1776534401; x=1779126401;\n bh=/s6rV3OPbpGqCO2vJgyxdI+AGTAIfaIifIdj6tNLlBA=;\n h=Content-Type:Mime-Version:Subject:From:Date:Message-Id:To:x-icloud-hme;\n b=eVHuX7c1jjLF2I6WStM3BjC8r34b0pDww79PPaOyfT9FcSPjAWiFmw7kYTPkfSLGfwbCWSdQt5uzr+ZAM7vK+2jFp8TQCQy+BsFkzdye7iHQciwjkMHNKdrFr3QZM+bfR+rcQ/0aUzzz70S4Z9wAqCcRCuBqqP6QRenAvp/NBtxutLfQJEziyyv981ek4uVJ9C1a7B6R5+sEtf77WDpnaF6Vv7CxZlZjiTFvJCcrRRsOC8kfiEN/doUeZB68iDSdIbwODZ4W1nRbRIEYAtmKhPXDY0whG1yT4Nf/9Uz3R/NBURM8sS09SPndnHQGO/dd3ZDfrOIUZR3AiMbc0KsGlQ==","mail-alias-created-date":"1752046281608","Content-Type":"text/plain;\n\tcharset=utf-8","Mime-Version":"1.0 (Mac OS X Mail 16.0 \\(3864.500.181\\))","Subject":"Re: [PATCH 30/34] target/i386: add de/compaction to xsave_helper","From":"Mohamed Mediouni <mohamed@unpredictable.fr>","In-Reply-To":"<20260417105618.3621-31-magnuskulke@linux.microsoft.com>","Date":"Sat, 18 Apr 2026 19:46:25 +0200","Cc":"qemu-devel@nongnu.org, kvm@vger.kernel.org,\n Magnus Kulke <magnuskulke@microsoft.com>, Wei Liu <liuwe@microsoft.com>,\n \"Michael S. Tsirkin\" <mst@redhat.com>,\n =?utf-8?q?C=C3=A9dric_Le_Goater?= <clg@redhat.com>,\n Zhao Liu <zhao1.liu@intel.com>,\n Richard Henderson <richard.henderson@linaro.org>,\n Paolo Bonzini <pbonzini@redhat.com>, Wei Liu <wei.liu@kernel.org>,\n Alex Williamson <alex@shazbot.org>,\n Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, =?utf-8?q?Philippe_Mathieu-D?=\n\t=?utf-8?q?aud=C3=A9?= <philmd@linaro.org>,\n Marcelo Tosatti <mtosatti@redhat.com>","Content-Transfer-Encoding":"quoted-printable","Message-Id":"<83C168E7-02D5-4CD9-9A27-545A88942171@unpredictable.fr>","References":"<20260417105618.3621-1-magnuskulke@linux.microsoft.com>\n <20260417105618.3621-31-magnuskulke@linux.microsoft.com>","To":"Magnus Kulke <magnuskulke@linux.microsoft.com>","X-Mailer":"Apple Mail (2.3864.500.181)","X-Proofpoint-ORIG-GUID":"H3ykXvsMWfb2zIV-QT4XRhlHmDs3jKSP","X-Proofpoint-Spam-Details-Enc":"AW1haW4tMjYwNDE4MDE3NyBTYWx0ZWRfXxyvn7KBVLGfq\n IIPtmNQneJQF3S70UZICiqeiUKCUy3qoz8kEQ/Jn4VLhByyMdLKnrzAi0aJk0Y3W4Vag9/ib5Cj\n 7TFonesYiS6cmCSEeXTtWrtnnSa5R+Biux4q5EpAP5ZYj2Ei2O6WW0+lU2toso8N8cGHoK/zmGM\n kAmO/Mbos1WSQsVQVkoQQJ2b7hlJiWUX9iKtvsrSIeYVdent9VToShqLAS8M4zCBtbBtcobe971\n IzkS5n26oSW5DpJlu334WwCcy6OhAWiBJowYE/5h1PHU9VbU+cvNF9IV5Nuq6se6UUTJTfI1R0I\n dDaaaKSyNFkLja/rsKxxkg/OhDJf0XVHTGfHF3EK8SoOpi7ZQI5HNFaejVoWrY=","X-Authority-Info-Out":"v=2.4 cv=QPllhwLL c=1 sm=1 tr=0 ts=69e3c380\n cx=c_apl:c_pps:t_out a=9OgfyREA4BUYbbCgc0Y0oA==:117\n a=9OgfyREA4BUYbbCgc0Y0oA==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10\n a=VkNPw1HP01LnGYTKEx00:22 a=yMhMjlubAAAA:8 a=DZ2UKWXZgWu71k7g0sQA:9\n a=QEXdDO2ut3YA:10","X-Proofpoint-GUID":"H3ykXvsMWfb2zIV-QT4XRhlHmDs3jKSP","X-Proofpoint-Virus-Version":"vendor=baseguard\n engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49\n definitions=2026-04-18_05,2026-04-17_04,2025-10-01_01","X-Proofpoint-Spam-Details":"rule=notspam policy=default score=0\n lowpriorityscore=0 spamscore=0 bulkscore=0 malwarescore=0 adultscore=0\n suspectscore=0 phishscore=0 clxscore=1030 mlxlogscore=999 mlxscore=0\n classifier=spam authscore=0 adjust=0 reason=mlx scancount=1\n engine=8.22.0-2601150000 definitions=main-2604180177","Received-SPF":"pass client-ip=57.103.71.83;\n envelope-from=mohamed@unpredictable.fr; helo=outbound.mr.icloud.com","X-Spam_score_int":"-27","X-Spam_score":"-2.8","X-Spam_bar":"--","X-Spam_report":"(-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=0.001,\n RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001,\n SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"qemu development <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}},{"id":3679384,"web_url":"http://patchwork.ozlabs.org/comment/3679384/","msgid":"<aeYV158wcT7A7X3O@example.com>","list_archive_url":null,"date":"2026-04-20T12:02:31","subject":"Re: [PATCH 30/34] target/i386: add de/compaction to xsave_helper","submitter":{"id":90753,"url":"http://patchwork.ozlabs.org/api/people/90753/","name":"Magnus Kulke","email":"magnuskulke@linux.microsoft.com"},"content":"On Sat, Apr 18, 2026 at 07:46:25PM +0200, Mohamed Mediouni wrote:\n> \n> \n> > On 17. Apr 2026, at 12:56, Magnus Kulke <magnuskulke@linux.microsoft.com> wrote:\n> This patch has a tiny problem.\n> \n> #if defined(CONFIG_KVM) || defined(CONFIG_HVF)\n> void *xsave_buf;\n> uint32_t xsave_buf_len;\n> #endif\n> \n> With the \"#if defined(CONFIG_KVM) || defined(CONFIG_HVF)” removed,\n> that fixes a TCG only QEMU build.\n> \n> Was also able to test that this works as expected on WHPX.\n\nthx, that's right. we can also change the signature to pass xsave_buf\nand xsave_buf_len as params, so accelerators which do not have xsave\nbuffers do not choke on this, it's probably less invasive rather than\nincluding xsave_buf fields for all accelerators.","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=linux.microsoft.com header.i=@linux.microsoft.com\n header.a=rsa-sha256 header.s=default header.b=AhUmhVwG;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists1p.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fzkf30YLCz1yCv\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 20 Apr 2026 22:03:23 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists1p.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wEnLA-0003fT-G3; Mon, 20 Apr 2026 08:03:01 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <magnuskulke@linux.microsoft.com>)\n id 1wEnKp-0003a7-DT\n for qemu-devel@nongnu.org; Mon, 20 Apr 2026 08:02:46 -0400","from linux.microsoft.com ([13.77.154.182])\n by eggs.gnu.org with esmtp (Exim 4.90_1)\n (envelope-from <magnuskulke@linux.microsoft.com>) id 1wEnKn-0002X4-U0\n for qemu-devel@nongnu.org; Mon, 20 Apr 2026 08:02:39 -0400","from example.com (unknown [167.220.208.49])\n by linux.microsoft.com (Postfix) with ESMTPSA id B660020B7128;\n Mon, 20 Apr 2026 05:02:33 -0700 (PDT)"],"DKIM-Filter":"OpenDKIM Filter v2.11.0 linux.microsoft.com B660020B7128","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;\n s=default; t=1776686556;\n bh=A0yu6PSKAtx4nEVROn2nI1PS6/gjdxCugWFMQbJZxoU=;\n h=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n b=AhUmhVwGTA5crU+qSH8lPZUj9A/Cm14eNVtKL60/h0EYZi285MQHnh4DmtPFDocZp\n s3kqq8CYtJz8wEFJWMFwWAVWHJtghc+4xI95VlCysYUetMIk562mfjcRc/5AdHfJ7N\n wQEbrjhOjHx1uPVDgfKzqXQjOdntayblQec+xmEM=","Date":"Mon, 20 Apr 2026 14:02:31 +0200","From":"Magnus Kulke <magnuskulke@linux.microsoft.com>","To":"Mohamed Mediouni <mohamed@unpredictable.fr>","Cc":"qemu-devel@nongnu.org, kvm@vger.kernel.org,\n Magnus Kulke <magnuskulke@microsoft.com>, Wei Liu <liuwe@microsoft.com>,\n \"Michael S. Tsirkin\" <mst@redhat.com>,\n =?iso-8859-1?q?C=E9dric?= Le Goater <clg@redhat.com>,\n Zhao Liu <zhao1.liu@intel.com>,\n Richard Henderson <richard.henderson@linaro.org>,\n Paolo Bonzini <pbonzini@redhat.com>, Wei Liu <wei.liu@kernel.org>,\n Alex Williamson <alex@shazbot.org>,\n Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Philippe =?iso-8859-1?q?Math?=\n\t=?iso-8859-1?q?ieu-Daud=E9?= <philmd@linaro.org>,\n Marcelo Tosatti <mtosatti@redhat.com>","Subject":"Re: [PATCH 30/34] target/i386: add de/compaction to xsave_helper","Message-ID":"<aeYV158wcT7A7X3O@example.com>","References":"<20260417105618.3621-1-magnuskulke@linux.microsoft.com>\n <20260417105618.3621-31-magnuskulke@linux.microsoft.com>\n <83C168E7-02D5-4CD9-9A27-545A88942171@unpredictable.fr>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<83C168E7-02D5-4CD9-9A27-545A88942171@unpredictable.fr>","Received-SPF":"pass client-ip=13.77.154.182;\n envelope-from=magnuskulke@linux.microsoft.com; helo=linux.microsoft.com","X-Spam_score_int":"-42","X-Spam_score":"-4.3","X-Spam_bar":"----","X-Spam_report":"(-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3,\n SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"qemu development <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}}]