From patchwork Tue Apr 9 05:02:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 787173 Delivered-To: patch@linaro.org Received: by 2002:adf:fdd2:0:b0:346:15ad:a2a with SMTP id i18csp57391wrs; Mon, 8 Apr 2024 22:05:24 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVyZivHPpWv16w6SyBxUfQ84RxVijesom0in7pQz8KaTSabR6OVtb0XE7ZXLDKH7ZGo7urhdXq2shy5Vway25YO X-Google-Smtp-Source: AGHT+IF9pfsqG3PG0R4btwFc8RouO6/KUDs1kVAChHZWxF3vpVrd2Ao+ANmxMTYyIuzitRLSlfJq X-Received: by 2002:a05:622a:15d2:b0:432:e451:9dee with SMTP id d18-20020a05622a15d200b00432e4519deemr12863889qty.52.1712639124002; Mon, 08 Apr 2024 22:05:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712639123; cv=none; d=google.com; s=arc-20160816; b=iX7+JiWFAHIwMJRvbPpWaSrSScTlZGc7XZx6MjmPvh3wjNZVwZD6LVTPdays0aBGsU +3YMnkk6IjQ72BPEpi/P4gDm3idsr1GgbNNvWUSUd+sS+QslgrJytTc12Y8sd7wVgbNh Y792nDNCWlUsUO5nz0Hhea1UlaJs9zhNyWOVrOLfgQCyTanr2pqk3r/vp5bZ7ms2/69g RqtnuM5B+5y4+OlisKH9daXVp7fR0EmsSGblmqizDVGcgIOyONxfQIlHvfRJH5jT4Mtb pSFfwbg+KEvSD+QLJe0Q5VNZEtOkH3CgyQn7wDsriXyVMWRWer/CegXjTMvF32TxtpoH fXPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=4uWY1NxVcrV95bV5MVGH7iWV55UiyAePqOvtwTyZGwY=; fh=PnYt+qEB9tAfMKoqBm2xjKOFpYyFFGPudh5cVIoieJM=; b=kyix/QteX1k1zfi23c8kxFZBdM0BQSpixNSfeoFSAGXZUe6ZqTAfTh2LeDaPr7LT/7 9hgI4PAoujfaUO6MYnwpWF7rO90o4QwzyNyIN+MNhoRHshGr6gYAt6InGu+qCmybdYbF oKB130ciPD2tQY29EVLyAjnmkr2DnjQ7gZLoqBi7RYB4mvmEb9E4A7RBMTaC/HgzswIR eovt9Z7x6bu1LMKEHxklFW/Pwh09WK06gIbKSqinyID1jKyVGWltMkuiLrAalQMsXWoT pdT/UTaIvp0MduNsoRd+b7PtmyVaXE2wjnTqUtet8yWa4mVqyQdVUF66chsD7Xkg/+bY tkbQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bmHw5Uap; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id a12-20020ac85b8c000000b004349685b1dbsi4185316qta.421.2024.04.08.22.05.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Apr 2024 22:05:23 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bmHw5Uap; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ru3e0-0003Fo-Bq; Tue, 09 Apr 2024 01:03: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 1ru3dz-0003FP-D3 for qemu-devel@nongnu.org; Tue, 09 Apr 2024 01:03:39 -0400 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ru3dx-0005O7-FL for qemu-devel@nongnu.org; Tue, 09 Apr 2024 01:03:39 -0400 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-6ed20fb620fso1748011b3a.2 for ; Mon, 08 Apr 2024 22:03:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712639016; x=1713243816; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=4uWY1NxVcrV95bV5MVGH7iWV55UiyAePqOvtwTyZGwY=; b=bmHw5UapAfnfdkg6Vb8NHbpHvm75YWcYqCiDKa2fPrj57mcXYDEqysyPLtrBcBNHgS p9SQw5Nv5g4Kr3ZBXe1J+gotSOtzJi72IIGX+w2jEOSJo9xWUNNJBMJjId7BWw6c79Ze L1MMHv0XZFxx73tLY3W6MwxuKpa0r2TkD8DHgzPthlC0VcLt7CBxLT19dtu8qw5y9wFS 6LDJ5M2UrSlXGLcBSaPz8sdHsUcR+MW26Y8VpzIUxpeuKJMw9TiZ0XoF0XDrmR9CIvFq z2yWVLzoO341hWnPsh59qt3Ps0hx01pgQR4nG41+QTFDMFjpbqTZzo8zQ91b00WtOfBi Q25Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712639016; x=1713243816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4uWY1NxVcrV95bV5MVGH7iWV55UiyAePqOvtwTyZGwY=; b=kne85MlQJGA2Pyd1ypOojhPidNPIyYGTcCMbpL5v1PB1CL9H4cwn59WfixAL1fpnEj 9I/ZZnQT6h52ZNAMpWAckpqsauQHv8sCRsAIsbAsz2A/7RK4wFqMKAm1SPWy4xaUYBC2 6irGVAc5wTHFb3KhcN034V/8OhUye29nan+0oU3BveuKo4UUECpOKqiHpSNn402IG5kI jauQfpySVVkzlp6KvzaXIKEufaRSWN5ynWrRUPEf7K0mxXJ1b9kby0F+ZgEoq3ucbW68 cO94rvM9TqEtJLoYCneY+4aa9a7C2LQQAaZ6T3qdCHy9r1unKoLLdkcdgm+c6nnE6jN0 BVdA== X-Gm-Message-State: AOJu0Yxug0nQdtavZ/2dt/or1K4WNGN1aks4vb04O+SIryo6ZgKilaH1 rjkLdWsY5MpNI4Mwl/ZiBhjbgtmKV1dyq/2TotaFjGM1Pm5LZFiiflTFE+ZHfECatl8DoXQmgbp T X-Received: by 2002:a05:6a00:1888:b0:6ea:baf6:5720 with SMTP id x8-20020a056a00188800b006eabaf65720mr11160918pfh.3.1712639016224; Mon, 08 Apr 2024 22:03:36 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id z26-20020aa785da000000b006e64ddfa71asm7654894pfn.170.2024.04.08.22.03.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 22:03:35 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 25/28] target/i386: Convert do_xrstor to X86Access Date: Mon, 8 Apr 2024 19:02:59 -1000 Message-Id: <20240409050302.1523277-26-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240409050302.1523277-1-richard.henderson@linaro.org> References: <20240409050302.1523277-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::433; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x433.google.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, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Signed-off-by: Richard Henderson --- target/i386/tcg/fpu_helper.c | 106 +++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 42 deletions(-) diff --git a/target/i386/tcg/fpu_helper.c b/target/i386/tcg/fpu_helper.c index d4dd09dc95..909da05f91 100644 --- a/target/i386/tcg/fpu_helper.c +++ b/target/i386/tcg/fpu_helper.c @@ -2902,51 +2902,38 @@ void helper_fxrstor(CPUX86State *env, target_ulong ptr) do_fxrstor(&ac, ptr); } -static void do_xrstor(CPUX86State *env, target_ulong ptr, uint64_t rfbm, uintptr_t ra) +static bool valid_xrstor_header(X86Access *ac, uint64_t *pxsbv, + target_ulong ptr) { uint64_t xstate_bv, xcomp_bv, reserve0; - X86Access ac; - unsigned size, size_ext; - rfbm &= env->xcr0; + xstate_bv = access_ldq(ac, ptr + XO(header.xstate_bv)); + xcomp_bv = access_ldq(ac, ptr + XO(header.xcomp_bv)); + reserve0 = access_ldq(ac, ptr + XO(header.reserve0)); + *pxsbv = xstate_bv; - size = sizeof(X86LegacyXSaveArea) + sizeof(X86XSaveHeader); - access_prepare(&ac, env, ptr, size, MMU_DATA_LOAD, ra); - - xstate_bv = access_ldq(&ac, ptr + XO(header.xstate_bv)); - - if ((int64_t)xstate_bv < 0) { - /* FIXME: Compact form. */ - raise_exception_ra(env, EXCP0D_GPF, ra); + /* + * XCOMP_BV bit 63 indicates compact form, which we do not support, + * and thus must raise #GP. That leaves us in standard form. + * In standard form, bytes 23:8 must be zero -- which is both + * XCOMP_BV and the following 64-bit field. + */ + if (xcomp_bv || reserve0) { + return false; } - /* Standard form. */ - /* The XSTATE_BV field must not set bits not present in XCR0. */ - if (xstate_bv & ~env->xcr0) { - raise_exception_ra(env, EXCP0D_GPF, ra); - } + return (xstate_bv & ~ac->env->xcr0) == 0; +} - /* The XCOMP_BV field must be zero. Note that, as of the April 2016 - revision, the description of the XSAVE Header (Vol 1, Sec 13.4.2) - describes only XCOMP_BV, but the description of the standard form - of XRSTOR (Vol 1, Sec 13.8.1) checks bytes 23:8 for zero, which - includes the next 64-bit field. */ - xcomp_bv = access_ldq(&ac, ptr + XO(header.xcomp_bv)); - reserve0 = access_ldq(&ac, ptr + XO(header.reserve0)); - if (xcomp_bv || reserve0) { - raise_exception_ra(env, EXCP0D_GPF, ra); - } - - size_ext = xsave_area_size(rfbm & xstate_bv, false); - if (size < size_ext) { - /* TODO: See if existing page probe has covered extra size. */ - access_prepare(&ac, env, ptr, size_ext, MMU_DATA_LOAD, ra); - } +static void do_xrstor(X86Access *ac, target_ulong ptr, + uint64_t rfbm, uint64_t xstate_bv) +{ + CPUX86State *env = ac->env; if (rfbm & XSTATE_FP_MASK) { if (xstate_bv & XSTATE_FP_MASK) { - do_xrstor_fpu(&ac, ptr); + do_xrstor_fpu(ac, ptr); } else { do_fninit(env); memset(env->fpregs, 0, sizeof(env->fpregs)); @@ -2955,23 +2942,23 @@ static void do_xrstor(CPUX86State *env, target_ulong ptr, uint64_t rfbm, uintptr if (rfbm & XSTATE_SSE_MASK) { /* Note that the standard form of XRSTOR loads MXCSR from memory whether or not the XSTATE_BV bit is set. */ - do_xrstor_mxcsr(&ac, ptr); + do_xrstor_mxcsr(ac, ptr); if (xstate_bv & XSTATE_SSE_MASK) { - do_xrstor_sse(&ac, ptr); + do_xrstor_sse(ac, ptr); } else { do_clear_sse(env); } } if (rfbm & XSTATE_YMM_MASK) { if (xstate_bv & XSTATE_YMM_MASK) { - do_xrstor_ymmh(&ac, ptr + XO(avx_state)); + do_xrstor_ymmh(ac, ptr + XO(avx_state)); } else { do_clear_ymmh(env); } } if (rfbm & XSTATE_BNDREGS_MASK) { if (xstate_bv & XSTATE_BNDREGS_MASK) { - do_xrstor_bndregs(&ac, ptr + XO(bndreg_state)); + do_xrstor_bndregs(ac, ptr + XO(bndreg_state)); env->hflags |= HF_MPX_IU_MASK; } else { memset(env->bnd_regs, 0, sizeof(env->bnd_regs)); @@ -2980,7 +2967,7 @@ static void do_xrstor(CPUX86State *env, target_ulong ptr, uint64_t rfbm, uintptr } if (rfbm & XSTATE_BNDCSR_MASK) { if (xstate_bv & XSTATE_BNDCSR_MASK) { - do_xrstor_bndcsr(&ac, ptr + XO(bndcsr_state)); + do_xrstor_bndcsr(ac, ptr + XO(bndcsr_state)); } else { memset(&env->bndcs_regs, 0, sizeof(env->bndcs_regs)); } @@ -2989,7 +2976,7 @@ static void do_xrstor(CPUX86State *env, target_ulong ptr, uint64_t rfbm, uintptr if (rfbm & XSTATE_PKRU_MASK) { uint64_t old_pkru = env->pkru; if (xstate_bv & XSTATE_PKRU_MASK) { - do_xrstor_pkru(&ac, ptr + XO(pkru_state)); + do_xrstor_pkru(ac, ptr + XO(pkru_state)); } else { env->pkru = 0; } @@ -3005,9 +2992,27 @@ static void do_xrstor(CPUX86State *env, target_ulong ptr, uint64_t rfbm, uintptr void helper_xrstor(CPUX86State *env, target_ulong ptr, uint64_t rfbm) { uintptr_t ra = GETPC(); + X86Access ac; + uint64_t xstate_bv; + unsigned size, size_ext; do_xsave_chk(env, ptr, ra); - do_xrstor(env, ptr, rfbm, ra); + + /* Begin with just the minimum size to validate the header. */ + size = sizeof(X86LegacyXSaveArea) + sizeof(X86XSaveHeader); + access_prepare(&ac, env, ptr, size, MMU_DATA_LOAD, ra); + if (!valid_xrstor_header(&ac, &xstate_bv, ptr)) { + raise_exception_ra(env, EXCP0D_GPF, ra); + } + + rfbm &= env->xcr0; + size_ext = xsave_area_size(rfbm & xstate_bv, false); + if (size < size_ext) { + /* TODO: See if existing page probe has covered extra size. */ + access_prepare(&ac, env, ptr, size_ext, MMU_DATA_LOAD, ra); + } + + do_xrstor(&ac, ptr, rfbm, xstate_bv); } #if defined(CONFIG_USER_ONLY) @@ -3065,7 +3070,24 @@ void cpu_x86_xsave(CPUX86State *env, target_ulong ptr, uint64_t rfbm) void cpu_x86_xrstor(CPUX86State *env, target_ulong ptr, uint64_t rfbm) { - do_xrstor(env, ptr, rfbm, 0); + X86Access ac; + uint64_t xstate_bv; + unsigned size; + + /* + * Since this is only called from user-level signal handling, + * we should have done the job correctly there. + */ + assert((rfbm & ~env->xcr0) == 0); + size = xsave_area_size(rfbm, false); + access_prepare(&ac, env, ptr, size, MMU_DATA_LOAD, 0); + + if (!valid_xrstor_header(&ac, &xstate_bv, ptr)) { + /* TODO: Report failure to caller. */ + xstate_bv &= env->xcr0; + } + + do_xrstor(&ac, ptr, rfbm, xstate_bv); } #endif