From nobody Fri May  9 11:19:54 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=linaro.org
ARC-Seal: i=1; a=rsa-sha256; t=1657549643; cv=none;
	d=zohomail.com; s=zohoarc;
	b=ULa1B0T6JIOOo7g/Du7CxsfOwONoyFitdta7rEJAHWXNKw5ezH37yG/3PPtY8gIkICfok6fSXqJdYFN3emD2+tdDOthcWrWfnTlamNcpQwCeDjuU8uit4JXFV2u0pEEs2RrDr0/HzdbAKHnl7kfiXtRFKcOGZIK8C1ZstcGj3ug=
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com;
 s=zohoarc;
	t=1657549643;
 h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To;
	bh=zRQNKZGKQhKr7J1kk0X1ky1/gLAO7OPNVwA91vM50lQ=;
	b=aobPSPSjFmmapV9UYrizTd4+D8o90oAEJFb8FYxWyyeat5gcIzyMYWdHuaon0M9WEn5tBr6VW2j7lsVzKqhnvQWG0eueH2tKcfYgmH7ZjriD+i3C92/gJnmUuN94rzlZ65nt8nkT82iN0M//gsu+qxRFBg0agTqCVcji9uAT8X4=
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=<peter.maydell@linaro.org> (p=none dis=none)
Return-Path: <qemu-devel-bounces+importer=patchew.org@nongnu.org>
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by
 mx.zohomail.com
	with SMTPS id 1657549643145955.481639304195;
 Mon, 11 Jul 2022 07:27:23 -0700 (PDT)
Received: from localhost ([::1]:38152 helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <qemu-devel-bounces+importer=patchew.org@nongnu.org>)
	id 1oAuNa-0003fF-SX
	for importer@patchew.org; Mon, 11 Jul 2022 10:27:18 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10]:43222)
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peter.maydell@linaro.org>)
 id 1oAtvo-0006mS-1S
 for qemu-devel@nongnu.org; Mon, 11 Jul 2022 09:58:36 -0400
Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]:45576)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <peter.maydell@linaro.org>)
 id 1oAtve-0002uh-RC
 for qemu-devel@nongnu.org; Mon, 11 Jul 2022 09:58:35 -0400
Received: by mail-wr1-x431.google.com with SMTP id a5so7118064wrx.12
 for <qemu-devel@nongnu.org>; Mon, 11 Jul 2022 06:58:25 -0700 (PDT)
Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2])
 by smtp.gmail.com with ESMTPSA id
 q123-20020a1c4381000000b00397402ae674sm7595596wma.11.2022.07.11.06.58.23
 for <qemu-devel@nongnu.org>
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Mon, 11 Jul 2022 06:58:24 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google;
 h=from:to:subject:date:message-id:in-reply-to:references:mime-version
 :content-transfer-encoding;
 bh=zRQNKZGKQhKr7J1kk0X1ky1/gLAO7OPNVwA91vM50lQ=;
 b=dodTiylk8c2Zgq+uMwhnhz1/KCy2tN0oR029p0OWSytK2pg9i1BNquAWp1ip9fksHm
 8mMbQG+PFspwYnjmIl4GhhnXyoXZXOokEAyp8Gg+zwVVK93ZLRxWgYXWQ9uiHrc2eDuH
 u2fem9KxjkQWDivUrmbT94HcOv5QZEpFieVgzCeEpC+fy0dieWIknOtG3ubPIg7+Eku6
 oGOttQIMHwBD7Of6NAVIzhoYW9MekD3rzkRf7AQtp4Q+M6zcuBnaRh2g4UYrV8HLQ2ez
 oL724rbWi2ENs+x3cNP++pYfLsve8Xiypm6Zp42JJnhW3snVLYAmdyGV1Pljh1GodJnl
 UEhA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to
 :references:mime-version:content-transfer-encoding;
 bh=zRQNKZGKQhKr7J1kk0X1ky1/gLAO7OPNVwA91vM50lQ=;
 b=c56jvAu3l9n8I1tYtrCNvJK0LxT/2yN2MrA/+FuOsStTBYWlMX3L9XxJTPpLiAHbei
 BnJlo6Hqe7AKa+P0IONNxg9LL2igKOXpAjUPPASYUOhbXG1uKQMuWnDYHWRhCQbRb8+H
 fEUsFynibPAHtdQAoW2813PHM0UXQ4nNRc5Iqlq3tSfWGLJSzX/0AQNdua+3ontXkDnZ
 NOdgWEl3T6YeOB8sTxc41x3VZe/D5ooqIgXD8TIr+odytmKbqZNFtpdigNfebnwdgPGM
 LeYl7s5gBxTVp5hsw1b138bkv0u6B/nx1f9OrVVT77iG58uq9w4bvbroKG7EBNRlEFWm
 MYhg==
X-Gm-Message-State: AJIora9ki+grUj9JUPa+7Ou8AF9equsrbTs8PGG6U5ATQLfmNi/0k72J
 rdCgKmCdxqkBuV+ET4/5+dPNoJNcowQKlg==
X-Google-Smtp-Source: 
 AGRyM1vnFIAHkgliCHBD6cSmbqsFkAFb8Cl9O3ouPmtKe//bgoxpvi1q2uI2/ZltQpviGQrg4BNJJQ==
X-Received: by 2002:a05:6000:2aa:b0:21d:ac4b:95af with SMTP id
 l10-20020a05600002aa00b0021dac4b95afmr2138572wry.479.1657547904559;
 Mon, 11 Jul 2022 06:58:24 -0700 (PDT)
From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-devel@nongnu.org
Subject: [PULL 39/45] linux-user/aarch64: Move sve record checks into restore
Date: Mon, 11 Jul 2022 14:57:44 +0100
Message-Id: <20220711135750.765803-40-peter.maydell@linaro.org>
X-Mailer: git-send-email 2.25.1
In-Reply-To: <20220711135750.765803-1-peter.maydell@linaro.org>
References: <20220711135750.765803-1-peter.maydell@linaro.org>
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=2a00:1450:4864:20::431;
 envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x431.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,
 T_SCC_BODY_TEXT_LINE=-0.01 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-devel.nongnu.org>
List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>,
 <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>,
 <mailto:qemu-devel-request@nongnu.org?subject=subscribe>
Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org
Sender: "Qemu-devel" <qemu-devel-bounces+importer=patchew.org@nongnu.org>
X-ZohoMail-DKIM: pass (identity @linaro.org)
X-ZM-MESSAGEID: 1657549644629100001
Content-Type: text/plain; charset="utf-8"

From: Richard Henderson <richard.henderson@linaro.org>

Move the checks out of the parsing loop and into the
restore function.  This more closely mirrors the code
structure in the kernel, and is slightly clearer.

Reject rather than silently skip incorrect VL and SVE record sizes,
bringing our checks in to line with those the kernel does.

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-id: 20220708151540.18136-40-richard.henderson@linaro.org
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 linux-user/aarch64/signal.c | 51 +++++++++++++++++++++++++------------
 1 file changed, 35 insertions(+), 16 deletions(-)

diff --git a/linux-user/aarch64/signal.c b/linux-user/aarch64/signal.c
index 9ff79da4be0..22d0b8b4ece 100644
--- a/linux-user/aarch64/signal.c
+++ b/linux-user/aarch64/signal.c
@@ -250,12 +250,36 @@ static void target_restore_fpsimd_record(CPUARMState =
*env,
     }
 }
=20
-static void target_restore_sve_record(CPUARMState *env,
-                                      struct target_sve_context *sve, int =
vq)
+static bool target_restore_sve_record(CPUARMState *env,
+                                      struct target_sve_context *sve,
+                                      int size)
 {
-    int i, j;
+    int i, j, vl, vq;
=20
-    /* Note that SVE regs are stored as a byte stream, with each byte elem=
ent
+    if (!cpu_isar_feature(aa64_sve, env_archcpu(env))) {
+        return false;
+    }
+
+    __get_user(vl, &sve->vl);
+    vq =3D sve_vq(env);
+
+    /* Reject mismatched VL. */
+    if (vl !=3D vq * TARGET_SVE_VQ_BYTES) {
+        return false;
+    }
+
+    /* Accept empty record -- used to clear PSTATE.SM. */
+    if (size <=3D sizeof(*sve)) {
+        return true;
+    }
+
+    /* Reject non-empty but incomplete record. */
+    if (size < TARGET_SVE_SIG_CONTEXT_SIZE(vq)) {
+        return false;
+    }
+
+    /*
+     * Note that SVE regs are stored as a byte stream, with each byte elem=
ent
      * at a subsequent address.  This corresponds to a little-endian load
      * of our 64-bit hunks.
      */
@@ -277,6 +301,7 @@ static void target_restore_sve_record(CPUARMState *env,
             }
         }
     }
+    return true;
 }
=20
 static int target_restore_sigframe(CPUARMState *env,
@@ -287,7 +312,7 @@ static int target_restore_sigframe(CPUARMState *env,
     struct target_sve_context *sve =3D NULL;
     uint64_t extra_datap =3D 0;
     bool used_extra =3D false;
-    int vq =3D 0, sve_size =3D 0;
+    int sve_size =3D 0;
=20
     target_restore_general_frame(env, sf);
=20
@@ -321,15 +346,9 @@ static int target_restore_sigframe(CPUARMState *env,
             if (sve || size < sizeof(struct target_sve_context)) {
                 goto err;
             }
-            if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
-                vq =3D sve_vq(env);
-                sve_size =3D QEMU_ALIGN_UP(TARGET_SVE_SIG_CONTEXT_SIZE(vq)=
, 16);
-                if (size =3D=3D sve_size) {
-                    sve =3D (struct target_sve_context *)ctx;
-                    break;
-                }
-            }
-            goto err;
+            sve =3D (struct target_sve_context *)ctx;
+            sve_size =3D size;
+            break;
=20
         case TARGET_EXTRA_MAGIC:
             if (extra || size !=3D sizeof(struct target_extra_context)) {
@@ -362,8 +381,8 @@ static int target_restore_sigframe(CPUARMState *env,
     }
=20
     /* SVE data, if present, overwrites FPSIMD data.  */
-    if (sve) {
-        target_restore_sve_record(env, sve, vq);
+    if (sve && !target_restore_sve_record(env, sve, sve_size)) {
+        goto err;
     }
     unlock_user(extra, extra_datap, 0);
     return 0;
--=20
2.25.1