From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, HTML_MESSAGE,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED4B7C433E0 for ; Wed, 20 Jan 2021 03:05:43 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4C28A23109 for ; Wed, 20 Jan 2021 03:05:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4C28A23109 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=apple.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:58174 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l23oT-0001AD-Mg for qemu-devel@archiver.kernel.org; Tue, 19 Jan 2021 22:05:41 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:44528) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l23nF-0000jN-HN for qemu-devel@nongnu.org; Tue, 19 Jan 2021 22:04:25 -0500 Received: from nwk-aaemail-lapp03.apple.com ([17.151.62.68]:50222) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l23nD-0004sT-Eo for qemu-devel@nongnu.org; Tue, 19 Jan 2021 22:04:25 -0500 Received: from pps.filterd (nwk-aaemail-lapp03.apple.com [127.0.0.1]) by nwk-aaemail-lapp03.apple.com (8.16.0.42/8.16.0.42) with SMTP id 10K2tF3S034825; Tue, 19 Jan 2021 19:04:20 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=apple.com; h=from : message-id : content-type : mime-version : subject : date : in-reply-to : cc : to : references; s=20180706; bh=ykjdXFNm5LydRanBa5feMztCr9M8z9dg/UcGGF0l//c=; b=ZkPbVbAd/D766kCuyDAgk7VbLQAz6zbPYSdNYFCVKxOvI5FcyjKXVBz9nMe4zcSKFOBW uqKxr7Sue1i3X7QjZ8Z1oAq4JBIv6f2A/whBAx3lzIAD+XgM+ZMGwuhS0AX3Z65g60dE /lpWpjG6erprheHCl85W3wofGmo11Ce4HJk9U6E4Fn8vRJBs0tv8VMeJs83fDI8LAZ+a LwmLYZMYEv+eF8yqKJAa4a6PCroBP9lDQ2hYZX4V0v5wzcsIwe5VCU5mOqqHsAQ2+UQa DduH9NwYeeQNKzk6su40tYbUmnAqkLFptYUQ63Ck3YgiDz1L3hOgR/MDAfy0F9LlAPl1 WQ== Received: from rn-mailsvcp-mta-lapp03.rno.apple.com (rn-mailsvcp-mta-lapp03.rno.apple.com [10.225.203.151]) by nwk-aaemail-lapp03.apple.com with ESMTP id 3668r8k0aa-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO); Tue, 19 Jan 2021 19:04:20 -0800 Received: from rn-mailsvcp-mmp-lapp03.rno.apple.com (rn-mailsvcp-mmp-lapp03.rno.apple.com [17.179.253.16]) by rn-mailsvcp-mta-lapp03.rno.apple.com (Oracle Communications Messaging Server 8.1.0.7.20201203 64bit (built Dec 3 2020)) with ESMTPS id <0QN700V5HPV4S470@rn-mailsvcp-mta-lapp03.rno.apple.com>; Tue, 19 Jan 2021 19:04:16 -0800 (PST) Received: from process_milters-daemon.rn-mailsvcp-mmp-lapp03.rno.apple.com by rn-mailsvcp-mmp-lapp03.rno.apple.com (Oracle Communications Messaging Server 8.1.0.7.20201203 64bit (built Dec 3 2020)) id <0QN700Y00PPFJN00@rn-mailsvcp-mmp-lapp03.rno.apple.com>; Tue, 19 Jan 2021 19:04:16 -0800 (PST) X-Va-A: X-Va-T-CD: e1aed8c2df8699cbf816f93d3d4dacf8 X-Va-E-CD: 73f39b88ac49a6242b533b9ce41dce88 X-Va-R-CD: 4ce8a15b1214356ac3198b5769b838d6 X-Va-CD: 0 X-Va-ID: b6bf2bf4-96ec-46ad-a58c-cbd67983e7f9 X-V-A: X-V-T-CD: e1aed8c2df8699cbf816f93d3d4dacf8 X-V-E-CD: 73f39b88ac49a6242b533b9ce41dce88 X-V-R-CD: 4ce8a15b1214356ac3198b5769b838d6 X-V-CD: 0 X-V-ID: 0b507acb-f7e2-4087-b6a7-f4fd3f37bdbd X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.343, 18.0.737 definitions=2021-01-19_15:2021-01-18, 2021-01-19 signatures=0 Received: from smtpclient.apple (unknown [17.232.231.32]) by rn-mailsvcp-mmp-lapp03.rno.apple.com (Oracle Communications Messaging Server 8.1.0.7.20201203 64bit (built Dec 3 2020)) with ESMTPSA id <0QN700JFPPV2XJ00@rn-mailsvcp-mmp-lapp03.rno.apple.com>; Tue, 19 Jan 2021 19:04:16 -0800 (PST) From: Cameron Esfahani Message-id: Content-type: multipart/alternative; boundary="Apple-Mail=_C20F662E-A896-4BD7-BA50-E47110B10754" MIME-version: 1.0 (Mac OS X Mail 14.0 \(3654.80.0.2.6\)) Subject: Re: [PATCH v3] hvf: guard xgetbv call. Date: Tue, 19 Jan 2021 19:04:14 -0800 In-reply-to: To: Hill Ma References: X-Mailer: Apple Mail (2.3654.80.0.2.6) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.343, 18.0.737 definitions=2021-01-19_15:2021-01-18, 2021-01-19 signatures=0 Received-SPF: pass client-ip=17.151.62.68; envelope-from=dirty@apple.com; helo=nwk-aaemail-lapp03.apple.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.195, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: r.bolshakov@yadro.com, richard.henderson@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --Apple-Mail=_C20F662E-A896-4BD7-BA50-E47110B10754 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Reviewed-by: Cameron Esfahani > Cameron Esfahani dirty@apple.com > On Jan 12, 2021, at 10:07 PM, Hill Ma wrote: >=20 > This prevents illegal instruction on cpus do not support xgetbv. >=20 > Buglink: https://bugs.launchpad.net/qemu/+bug/1758819 > Signed-off-by: Hill Ma > --- > v3: addressed feedback. > v2: xgetbv() modified based on feedback. >=20 > target/i386/hvf/x86_cpuid.c | 34 ++++++++++++++++++++++------------ > 1 file changed, 22 insertions(+), 12 deletions(-) >=20 > diff --git a/target/i386/hvf/x86_cpuid.c b/target/i386/hvf/x86_cpuid.c > index a6842912f5..32b0d131df 100644 > --- a/target/i386/hvf/x86_cpuid.c > +++ b/target/i386/hvf/x86_cpuid.c > @@ -27,15 +27,22 @@ > #include "vmx.h" > #include "sysemu/hvf.h" >=20 > -static uint64_t xgetbv(uint32_t xcr) > +static bool xgetbv(uint32_t cpuid_ecx, uint32_t idx, uint64_t *xcr) > { > - uint32_t eax, edx; > + uint32_t xcrl, xcrh; >=20 > - __asm__ volatile ("xgetbv" > - : "=3Da" (eax), "=3Dd" (edx) > - : "c" (xcr)); > + if (cpuid_ecx & CPUID_EXT_OSXSAVE) { > + /* > + * The xgetbv instruction is not available to older versions = of > + * the assembler, so we encode the instruction manually. > + */ > + asm(".byte 0x0f, 0x01, 0xd0" : "=3Da" (xcrl), "=3Dd" (xcrh) : = "c" (idx)); >=20 > - return (((uint64_t)edx) << 32) | eax; > + *xcr =3D (((uint64_t)xcrh) << 32) | xcrl; > + return true; > + } > + > + return false; > } >=20 > uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx, > @@ -100,12 +107,15 @@ uint32_t hvf_get_supported_cpuid(uint32_t func, = uint32_t idx, > break; > case 0xD: > if (idx =3D=3D 0) { > - uint64_t host_xcr0 =3D xgetbv(0); > - uint64_t supp_xcr0 =3D host_xcr0 & (XSTATE_FP_MASK | = XSTATE_SSE_MASK | > - XSTATE_YMM_MASK | = XSTATE_BNDREGS_MASK | > - XSTATE_BNDCSR_MASK | = XSTATE_OPMASK_MASK | > - XSTATE_ZMM_Hi256_MASK | = XSTATE_Hi16_ZMM_MASK); > - eax &=3D supp_xcr0; > + uint64_t host_xcr0; > + if (xgetbv(ecx, 0, &host_xcr0)) { > + uint64_t supp_xcr0 =3D host_xcr0 & (XSTATE_FP_MASK | > + XSTATE_SSE_MASK | XSTATE_YMM_MASK | > + XSTATE_BNDREGS_MASK | = XSTATE_BNDCSR_MASK | > + XSTATE_OPMASK_MASK | = XSTATE_ZMM_Hi256_MASK | > + XSTATE_Hi16_ZMM_MASK); > + eax &=3D supp_xcr0; > + } > } else if (idx =3D=3D 1) { > hv_vmx_read_capability(HV_VMX_CAP_PROCBASED2, &cap); > eax &=3D CPUID_XSAVE_XSAVEOPT | CPUID_XSAVE_XGETBV1; > --=20 > 2.20.1 (Apple Git-117) >=20 --Apple-Mail=_C20F662E-A896-4BD7-BA50-E47110B10754 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii Reviewed-by: Cameron Esfahani <dirty@apple.com>

Cameron = Esfahani
dirty@apple.com

On Jan = 12, 2021, at 10:07 PM, Hill Ma <maahiuzeon@gmail.com> = wrote:

This prevents illegal instruction on cpus do not support = xgetbv.

Buglink: = https://bugs.launchpad.net/qemu/+bug/1758819
Signed-off-by: = Hill Ma <maahiuzeon@gmail.com>
---
v3: = addressed feedback.
v2: xgetbv() modified based on = feedback.

target/i386/hvf/x86_cpuid.c | 34 = ++++++++++++++++++++++------------
1 file changed, 22 = insertions(+), 12 deletions(-)

diff --git = a/target/i386/hvf/x86_cpuid.c b/target/i386/hvf/x86_cpuid.c
index a6842912f5..32b0d131df 100644
--- = a/target/i386/hvf/x86_cpuid.c
+++ = b/target/i386/hvf/x86_cpuid.c
@@ -27,15 +27,22 @@
#include "vmx.h"
#include "sysemu/hvf.h"

-static uint64_t xgetbv(uint32_t xcr)
+static bool xgetbv(uint32_t cpuid_ecx, uint32_t idx, = uint64_t *xcr)
{
- =    uint32_t eax, edx;
+ =    uint32_t xcrl, xcrh;

- =    __asm__ volatile ("xgetbv"
- =             &n= bsp;        : "=3Da" (eax), "=3Dd"= (edx)
- =             &n= bsp;        : "c" (xcr));
+    if (cpuid_ecx & CPUID_EXT_OSXSAVE) = {
+        /*
+         * The = xgetbv instruction is not available to older versions of
+ =         * the assembler, so we = encode the instruction manually.
+ =         */
+ =        asm(".byte 0x0f, 0x01, 0xd0" : = "=3Da" (xcrl), "=3Dd" (xcrh) : "c" (idx));

- =    return (((uint64_t)edx) << 32) | eax;
+        *xcr =3D = (((uint64_t)xcrh) << 32) | xcrl;
+ =        return true;
+ =    }
+
+ =    return false;
}

uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t = idx,
@@ -100,12 +107,15 @@ uint32_t = hvf_get_supported_cpuid(uint32_t func, uint32_t idx,
=         break;
=     case 0xD:
=         if (idx =3D=3D 0) {
- =            uint64_t= host_xcr0 =3D xgetbv(0);
- =            uint64_t= supp_xcr0 =3D host_xcr0 & (XSTATE_FP_MASK | XSTATE_SSE_MASK |
- =             &n= bsp;           &nbs= p;        XSTATE_YMM_MASK | = XSTATE_BNDREGS_MASK |
- =             &n= bsp;           &nbs= p;        XSTATE_BNDCSR_MASK | = XSTATE_OPMASK_MASK |
- =             &n= bsp;           &nbs= p;        XSTATE_ZMM_Hi256_MASK = | XSTATE_Hi16_ZMM_MASK);
- =            eax = &=3D supp_xcr0;
+ =            uint64_t= host_xcr0;
+ =            if = (xgetbv(ecx, 0, &host_xcr0)) {
+ =             &n= bsp;  uint64_t supp_xcr0 =3D host_xcr0 & (XSTATE_FP_MASK = |
+ =             &n= bsp;           &nbs= p;        XSTATE_SSE_MASK | = XSTATE_YMM_MASK |
+ =             &n= bsp;           &nbs= p;        XSTATE_BNDREGS_MASK | = XSTATE_BNDCSR_MASK |
+ =             &n= bsp;           &nbs= p;        XSTATE_OPMASK_MASK | = XSTATE_ZMM_Hi256_MASK |
+ =             &n= bsp;           &nbs= p;        XSTATE_Hi16_ZMM_MASK);+ =             &n= bsp;  eax &=3D supp_xcr0;
+ =            }
        } else if = (idx =3D=3D 1) {
=             hv= _vmx_read_capability(HV_VMX_CAP_PROCBASED2, &cap);
=             ea= x &=3D CPUID_XSAVE_XSAVEOPT | CPUID_XSAVE_XGETBV1;
-- =
2.20.1 (Apple Git-117)


= --Apple-Mail=_C20F662E-A896-4BD7-BA50-E47110B10754--