From nobody Mon Sep 8 09:47:36 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1756868545; cv=none; d=zohomail.com; s=zohoarc; b=cRCKOElVpR4z45rHlawfpf8I83reBsEieMpeY/G+8mqpaGxzril9WsW3JjJs94IlEgZOyIGjYfFI2fOo1DaCAn5PCVyhCAXX7I/hLhFdPR/6KNnkIjfbwge07XmN1vFc1dmIWvXqoUIys5pqUbwBuQVrElssPSAjkaQ0zPsBaX8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756868545; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=r9fobgHF8BPoZ4YDoGnftfY3pCQ30cooqytFfFW2iyM=; b=aBhp9JHDmepUdLks2BG6dO+F51EX+T/ULs5DNIpUTElxgPFOS0zHRHqP4GXOnQPpBZFzwCsNHk2jACasGz8tUVsooBeFN1rkrvvfPrPdbW1rNRRFQG+zw6KJxgECj0PJDagBCmXbzFcyzMCggLAuqJPxuYbVT3ro+Q1PgDLS2Qg= 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1756868545268121.72032585026398; Tue, 2 Sep 2025 20:02:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1utdlI-0000G5-Jy; Tue, 02 Sep 2025 23:02:16 -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 1utdkn-0008LQ-Pq; Tue, 02 Sep 2025 23:01:49 -0400 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1utdkl-0006qQ-QL; Tue, 02 Sep 2025 23:01:45 -0400 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-76e4f2e4c40so5036525b3a.2; Tue, 02 Sep 2025 20:01:42 -0700 (PDT) Received: from lima-default (123.253.189.97.qld.leaptel.network. [123.253.189.97]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7725e419913sm6971760b3a.55.2025.09.02.20.01.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Sep 2025 20:01:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756868501; x=1757473301; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=r9fobgHF8BPoZ4YDoGnftfY3pCQ30cooqytFfFW2iyM=; b=cmKReeEIzc/4heY4efv/00d5+g/TOCx2KjigfoxBNFmkFjxe2VpkHIEhYzRQrSgzz9 /5Eqdg/rXnWDk9VlOv1AtyBhmSKKczcVoRF1NSbSuR0BwQIHpkx/qFvp42liFTZb1k9F HC/lFB5ryf23CDfKN0+JSBiTg76VYQ0CoLiozhxb1Dk4fG/vktwo/JPk3sQN+vC/KeHA UivsLJUAJOabIDH6EhKsgZuqY3WTIZxGWTkVNVrhD+d7kWfF1dzjhVLVVaE/ilc95D+i xU+BW7kt9vYZ2zQ1znziBPwtsLjz+9kXOajLSbQiKaIWAX1Ue2oUus8fJ3sF/GQxwVd6 8kUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756868501; x=1757473301; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=r9fobgHF8BPoZ4YDoGnftfY3pCQ30cooqytFfFW2iyM=; b=sXWy+ITgTlMGJTDCwLuTqMHGodxvXcq6LZSNEGfA6pnc108Fyw8x+Nw9l0jqK+Hi2h V4/s55rNQQ+Wtc1ujm1HQR6EKAIMm78xsWqSLScVSSdylrNFu54230GvHTS+3y8lXWs0 OMLBfbYSzmwM7Vy3zndajvY12SAk9aEoTENy0PexnrUvtou02QAfQLtzPFdhAzYvQdIR nS4cyhcxeovjRjtU9e2CpW64RLay5tZDiMqaRWTfuQMdU+A1Ei6IABEyrWr/zELTbVoc ll2BSOtaA+uJOlanCufGtWHltJQ6EIisiyxgi1IOKjFBTMb87sbiNbPwkXFRA0Cfqp81 yQvw== X-Forwarded-Encrypted: i=1; AJvYcCXocAAkyCmDbNG23Wbev1J6RQHbhFBZ463bMAzAn20+Tom7ByEDBNyfsrNqrkDCyZ6R8c6LHEOGItBZ@nongnu.org X-Gm-Message-State: AOJu0YzRco4UtvT7IJqAUTDRmdaxrMek1QNMabQ9/PsM7Fhr6V8AhTwP 1j0Wyf+3NDVZ61u+a3GsPxhI0UCsqn7GCF2puPygt7BO4m7T+ggT2uyQVLRB7w== X-Gm-Gg: ASbGnctHQA4s2LI4+gIj5qp9EUZutVrf+SO6bvjJ73ghzJECYEVVSNXNFNwrUCROnAC 4Z9koKcg3gCB0rI0vkozqGlJK2IMWa319i/7NQAXJJzGZ5ogWNyNxBlsv3SGQDzR5+7y/sHHU+Z ipYxkkJ+zqxz1Hn6U6fSrt6ZFRbS+znrbVUKOIEuUzbTOYP/5AromYUZ0ilhsjzL70lEANKn1XK eERPwL7enEKkq17oeTT6g5Vxf47n0UQD/612NMJe7OgWgcxWvwgr3TYbTpDobX2U8O3cuOyeqr/ 2n+T+yFSwB+yKzjsRmJDHmRhdSX//04uv9avYkii8qyoaWzQuWrzX+oyUrSWxN+a+BbWJdXBhCo DiIiFqYaC+W5gwPyZsQkaTV/PSWaiHz6l+Nko0AZzxo0MUC/6cGgAi4JuD5C/tzuMM1vwr5mjir okrbmr8ECQX9optrz8OqM= X-Google-Smtp-Source: AGHT+IG8OnK7/aOpVNZmhIhVKGjpj/I0cKLRAcRu13mdMoN5Quvfw+RztT43aPCuV48sOd8W0nJF9w== X-Received: by 2002:a05:6a00:ae10:b0:772:4319:e7f0 with SMTP id d2e1a72fcca58-7724319ecc4mr16866773b3a.32.1756868500965; Tue, 02 Sep 2025 20:01:40 -0700 (PDT) From: Nicholas Piggin To: qemu-riscv@nongnu.org Cc: Nicholas Piggin , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-devel@nongnu.org, Chao Liu , Nicholas Joaquin , Ganesh Valliappan Subject: [PATCH 1/3] target/riscv: Fix IALIGN check in misa write Date: Wed, 3 Sep 2025 13:01:11 +1000 Message-ID: <20250903030114.274535-2-npiggin@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250903030114.274535-1-npiggin@gmail.com> References: <20250903030114.274535-1-npiggin@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2607:f8b0:4864:20::435; envelope-from=npiggin@gmail.com; helo=mail-pf1-x435.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, FREEMAIL_FROM=0.001, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756868547778116600 The instruction alignment check for the C extension was inverted. The new value should be checked for C bit clear (thus increasing IALIGN). If IALIGN is incompatible, then the write to misa should be suppressed, not just ignoring the update to the C bit. From the ISA: Writing misa may increase IALIGN, e.g., by disabling the "C" extension. If an instruction that would write misa increases IALIGN, and the subsequent instruction=E2=80=99s address is not IALIGN-bit aligne= d, the write to misa is suppressed, leaving misa unchanged. This was found with a verification test generator based on RiESCUE. Reported-by: Nicholas Joaquin Reported-by: Ganesh Valliappan Signed-off-by: Nicholas Piggin Reviewed-by: Daniel Henrique Barboza --- target/riscv/csr.c | 16 ++++- tests/tcg/riscv64/Makefile.softmmu-target | 5 ++ tests/tcg/riscv64/misa-ialign.S | 88 +++++++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 tests/tcg/riscv64/misa-ialign.S diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 8842e07a73..64b55b7add 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -2140,9 +2140,19 @@ static RISCVException write_misa(CPURISCVState *env,= int csrno, /* Mask extensions that are not supported by this hart */ val &=3D env->misa_ext_mask; =20 - /* Suppress 'C' if next instruction is not aligned. */ - if ((val & RVC) && (get_next_pc(env, ra) & 3) !=3D 0) { - val &=3D ~RVC; + /* + * misa writes that increase IALIGN beyond alignment of the next + * instruction cause the write to misa to be suppressed. Clearing + * "C" extension increases IALIGN. + */ + if (!(val & RVC) && (get_next_pc(env, ra) & 3) !=3D 0) { + /* + * If the next instruction is unaligned mod 4 then "C" must be + * set or this instruction could not be executing, so we know + * this is is clearing "C" (and not just keeping it clear). + */ + g_assert(env->misa_ext & RVC); + return RISCV_EXCP_NONE; } =20 /* Disable RVG if any of its dependencies are disabled */ diff --git a/tests/tcg/riscv64/Makefile.softmmu-target b/tests/tcg/riscv64/= Makefile.softmmu-target index 3ca595335d..6e470a028f 100644 --- a/tests/tcg/riscv64/Makefile.softmmu-target +++ b/tests/tcg/riscv64/Makefile.softmmu-target @@ -24,5 +24,10 @@ EXTRA_RUNS +=3D run-test-mepc-masking run-test-mepc-masking: test-mepc-masking $(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<) =20 +EXTRA_RUNS +=3D run-misa-ialign +run-misa-ialign: QEMU_OPTS :=3D -cpu rv64,c=3Dtrue,v=3Dtrue,x-misa-w=3Don = $(QEMU_OPTS) +run-misa-ialign: misa-ialign + $(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<) + # We don't currently support the multiarch system tests undefine MULTIARCH_TESTS diff --git a/tests/tcg/riscv64/misa-ialign.S b/tests/tcg/riscv64/misa-ialig= n.S new file mode 100644 index 0000000000..7f1eb30023 --- /dev/null +++ b/tests/tcg/riscv64/misa-ialign.S @@ -0,0 +1,88 @@ +/* + * Test for MISA changing C and related IALIGN alignment cases + * + * This test verifies that the "C" extension can be cleared and set in MIS= A, + * that a branch to 2-byte aligned instructions can be executed when "C" is + * enabled, and that a write to MISA which would increase IALIGN and cause + * the next instruction to be unaligned is ignored. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#define RVC (1 << ('C'-'A')) +#define RVV (1 << ('V'-'A')) + +.option norvc + .text + .global _start +_start: + lla t0, trap + csrw mtvec, t0 + + csrr t0, misa + li t1, RVC + not t1, t1 + and t0, t0, t1 + csrw misa, t0 + csrr t1, misa + li a0, 2 # fail code + bne t0, t1, _exit # Could not clear RVC in MISA + + li t1, RVC + or t0, t0, t1 + csrw misa, t0 + csrr t1, misa + li a0, 3 # fail code + bne t0, t1, _exit # Could not set RVC in MISA + + j unalign +. =3D . + 2 +unalign: + + li t1, RVC + not t1, t1 + and t0, t0, t1 + csrw misa, t0 + csrr t1, misa + li a0, 4 # fail code + beq t0, t1, _exit # Was able to clear RVC in MISA + + li t0, (RVC|RVV) + not t0, t0 + and t0, t0, t1 + csrw misa, t0 + csrr t0, misa + li a0, 5 # fail code + bne t0, t1, _exit # MISA write was not ignored (RVV was cleared) + + j realign +. =3D . + 2 +realign: + + # Success! + li a0, 0 + j _exit + +trap: + # Any trap is a fail code 1 + li a0, 1 + +# Exit code in a0 +_exit: + lla a1, semiargs + li t0, 0x20026 # ADP_Stopped_ApplicationExit + sd t0, 0(a1) + sd a0, 8(a1) + li a0, 0x20 # TARGET_SYS_EXIT_EXTENDED + + # Semihosting call sequence + .balign 16 + slli zero, zero, 0x1f + ebreak + srai zero, zero, 0x7 + j . + + .data + .balign 16 +semiargs: + .space 16 --=20 2.51.0 From nobody Mon Sep 8 09:47:36 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1756868540; cv=none; d=zohomail.com; s=zohoarc; b=NFD4h++K8LtMIcX9xw2VHbxYZM2prxG5uAw8W/Pm0PT3+3bUC1CdLNvhxIDFBCX1QB5QmdFJLs01Ef1Z62MoN8PnefKjkjk6hKVksG49IziJO5dG0hcZSiyV7qL3L+hG6NxYQj0Nvd/6Mkd1ujFVRHwOP5khhhH+6hg0P/S7qcI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756868540; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=gVsPU6o2dAkDdBPoKyfvLVYbO3tTi0w59PoBz8wKzeM=; b=nk+ezks5q9Fe3bj1opTTKtN8WCO64nUynVwcZtXFcwxtzdamm4riRs0grQiEEYVda26BxBaqlXW6a24u1/BdTMMZRVJUED0ngB13w9VNCxhsS8ATJLC154Zzd56JBvo9hQA6DFPfkuOsGiY7HDSIHbCjwZVOde5hf0506flbV2k= 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1756868540452352.716374706568; Tue, 2 Sep 2025 20:02:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1utdlE-00009m-9R; Tue, 02 Sep 2025 23:02:12 -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 1utdks-0008MJ-6U; Tue, 02 Sep 2025 23:01:53 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1utdkq-0006xQ-FE; Tue, 02 Sep 2025 23:01:49 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-77251d7cca6so2524308b3a.3; Tue, 02 Sep 2025 20:01:47 -0700 (PDT) Received: from lima-default (123.253.189.97.qld.leaptel.network. [123.253.189.97]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7725e419913sm6971760b3a.55.2025.09.02.20.01.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Sep 2025 20:01:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756868506; x=1757473306; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gVsPU6o2dAkDdBPoKyfvLVYbO3tTi0w59PoBz8wKzeM=; b=JeID5fNW2BQyl9N2P4otjyWApRTZGvmcd2Zcmj63Cxf6DxaboHKQT1MixRTRAxtE+x Y1n76DQKIoSzVTL+EYaAoADjPPgQpcTnNYmESQkOAZp0lUlyyJrKnMXH9fp2aOiK+TpK snoEHy6nn7artCcitsn9Ette2+5c7DumCVE7LdHtKQ6xfMBidzcRwr4dSdaHWK4kzNiy Z5npaWWnRXoIBgxLpku3TBY6yLY8x+9HeL3uajRuqFGjY2lNVo8l8ZZPgvxEg+sQLAjs msQrrSMxbtzmGR5DvWnSPtjTGveayv2bDBIPPihzvdE2i0OLWk1Dye9g7MNvI8PIKAaI RPpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756868506; x=1757473306; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gVsPU6o2dAkDdBPoKyfvLVYbO3tTi0w59PoBz8wKzeM=; b=P5vOjhmtE2koe1H74HT0YLBGTNsGX/on41loTkXR6fg/D6TD3bzHeuiIRMXwiaX+ZS j+6ko4cdxkhfo8Ak59uOL7Ma/j/P8BDCpldCEaJ66CuaRdTs23+boEj+4yWCmfW87qtx GXukf4oLdVRJzMHmtWvrQuwkREREv8KiBeaWMTUmvybTclQrFSQDTpMHzdvEUyIzJSZz rJuxuCUZiuQ7Pi9CCIMY1mXG52jJj5BCYkYcUbEdFFRKWaDvik5MOlhed8mQUAmN8MJU E8qCVU87bqMsJF0+zflECrfX9v3H9G9OOX6pgVd6GrhzVDRVLxegtbAxyg8SCdSBIqh+ DMFw== X-Forwarded-Encrypted: i=1; AJvYcCUWcrWQUnqwesCIQWxfB/v67ryZo3E4Ogt9OyAk15LUfzWd4ERI9dWeW0VQ5e0VGOWlnh77oympSBAh@nongnu.org X-Gm-Message-State: AOJu0YxlcbgkRMfn4F4vg4BO5iH1+jg/GBBGSBoDXSyFABCoPZhavijT X3M+cUHesuGDA6EY0kVumb3N1rrsCK1pJDla4+9PCcqWzHWcc/wDyidGHVtJew== X-Gm-Gg: ASbGnctPs5An8h/e2kZKKBiS8rEOM9KhEpKlZHYfxeU4hlEfSPzAakk7WKaMKTTbF8i 7pJpo9NI5nZneiN5sJbv7THC6LklJb2+67OczFiOdchNC+Xcy+ikHjN8e05iiikbXemDFZa8MNn gIu1inh4jDjkLZ14fYYG6bkjbgROqxTRSk4DPuX1JsiBbcdXgvRnn0UyGXP9tIxZhaYb6oFvQUQ FK4EJ6457BVwPH3TjWj2b4XJ3ifjP0Jos12uPf2MdHtIXpAPOBAhfLTT32WtyyxIf+pmm/sGLTs tj/s42pwobqwU5wIew15aNy75XwQs6bu04ERVikG3WxGTNMBxv9WbMbWl/iDpR0gCp68UN5GWx2 Ot+ZWxNM/lRYeaLY3BmQzyL7TdFgSWpiZ21I7aI1vIgQP7wvTQnhIxy6e0l2FPA3Qc/993FwtaX k1imOP/XC7 X-Google-Smtp-Source: AGHT+IFmBrlJO5F1Xh9oDfkVtBD83qPDokXEPem7I+MNLnMK1BJ2xszmw4D3mBdEHMaAYaln6BTk4Q== X-Received: by 2002:a05:6a20:3d8d:b0:246:9192:2779 with SMTP id adf61e73a8af0-24691922d8emr2089734637.7.1756868505619; Tue, 02 Sep 2025 20:01:45 -0700 (PDT) From: Nicholas Piggin To: qemu-riscv@nongnu.org Cc: Nicholas Piggin , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-devel@nongnu.org, Chao Liu , Nicholas Joaquin , Ganesh Valliappan Subject: [PATCH 2/3] target/risvc: Fix vector whole ldst vstart check Date: Wed, 3 Sep 2025 13:01:12 +1000 Message-ID: <20250903030114.274535-3-npiggin@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250903030114.274535-1-npiggin@gmail.com> References: <20250903030114.274535-1-npiggin@gmail.com> 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=2607:f8b0:4864:20::42d; envelope-from=npiggin@gmail.com; helo=mail-pf1-x42d.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, FREEMAIL_FROM=0.001, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756868541701116600 Content-Type: text/plain; charset="utf-8" The whole vector ldst instructions do not include a vstart check, so an overflowed vstart can result in an underflowed memory address offset and crash: accel/tcg/cputlb.c:1465:probe_access_flags: assertion failed: (-(addr | TARGET_PAGE_MASK) >=3D size) Add the VSTART_CHECK_EARLY_EXIT() check for these helpers. This was found with a verification test generator based on RiESCUE. Reported-by: Nicholas Joaquin Reported-by: Ganesh Valliappan Signed-off-by: Nicholas Piggin --- target/riscv/vector_helper.c | 2 + tests/tcg/riscv64/Makefile.target | 5 ++ tests/tcg/riscv64/test-vstart-overflow.c | 75 ++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tests/tcg/riscv64/test-vstart-overflow.c diff --git a/target/riscv/vector_helper.c b/target/riscv/vector_helper.c index fc85a34a84..e0e8735000 100644 --- a/target/riscv/vector_helper.c +++ b/target/riscv/vector_helper.c @@ -825,6 +825,8 @@ vext_ldst_whole(void *vd, target_ulong base, CPURISCVSt= ate *env, uint32_t desc, uint32_t esz =3D 1 << log2_esz; int mmu_index =3D riscv_env_mmu_index(env, false); =20 + VSTART_CHECK_EARLY_EXIT(env, evl); + /* Calculate the page range of first page */ addr =3D base + (env->vstart << log2_esz); page_split =3D -(addr | TARGET_PAGE_MASK); diff --git a/tests/tcg/riscv64/Makefile.target b/tests/tcg/riscv64/Makefile= .target index 4da5b9a3b3..19a49b6467 100644 --- a/tests/tcg/riscv64/Makefile.target +++ b/tests/tcg/riscv64/Makefile.target @@ -18,3 +18,8 @@ TESTS +=3D test-fcvtmod test-fcvtmod: CFLAGS +=3D -march=3Drv64imafdc test-fcvtmod: LDFLAGS +=3D -static run-test-fcvtmod: QEMU_OPTS +=3D -cpu rv64,d=3Dtrue,zfa=3Dtrue + +# Test for vstart >=3D vl +TESTS +=3D test-vstart-overflow +test-vstart-overflow: CFLAGS +=3D -march=3Drv64gcv +run-test-vstart-overflow: QEMU_OPTS +=3D -cpu rv64,v=3Don diff --git a/tests/tcg/riscv64/test-vstart-overflow.c b/tests/tcg/riscv64/t= est-vstart-overflow.c new file mode 100644 index 0000000000..72999f2c8a --- /dev/null +++ b/tests/tcg/riscv64/test-vstart-overflow.c @@ -0,0 +1,75 @@ +/* + * Test for VSTART set to overflow VL + * + * TCG vector instructions should call VSTART_CHECK_EARLY_EXIT() to check + * this case, otherwise memory addresses can underflow and misbehave or + * crash QEMU. + * + * TODO: Add stores and other instructions. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include +#include + +#define VSTART_OVERFLOW_TEST(insn) \ +({ \ + uint8_t vmem[64] =3D { 0 }; \ + uint64_t vstart; \ + asm volatile(" \r\n \ + # Set VL=3D52 and VSTART=3D56 \r\n \ + li t0, 52 \r\n \ + vsetvli x0, t0, e8, m4, ta, ma \r\n \ + li t0, 56 \r\n \ + csrrw x0, vstart, t0 \r\n \ + li t1, 64 \r\n \ + " insn " \r\n \ + csrr %0, vstart \r\n \ + " : "=3Dr"(vstart), "+A"(vmem) :: "t0", "t1", "v24", "memory"); \ + vstart; \ +}) + +int run_vstart_overflow_tests() +{ + /* + * An implementation is permitted to raise an illegal instruction + * exception when executing a vector instruction if vstart is set to a + * value that could not be produced by the execution of that instructi= on + * with the same vtype. If TCG is changed to do this, then this test + * could be updated to handle the SIGILL. + */ + if (VSTART_OVERFLOW_TEST("vl1re16.v v24, %1")) { + return 1; + } + + if (VSTART_OVERFLOW_TEST("vs1r.v v24, %1")) { + return 1; + } + + if (VSTART_OVERFLOW_TEST("vle16.v v24, %1")) { + return 1; + } + + if (VSTART_OVERFLOW_TEST("vse16.v v24, %1")) { + return 1; + } + + if (VSTART_OVERFLOW_TEST("vluxei8.v v24, %1, v20")) { + return 1; + } + + if (VSTART_OVERFLOW_TEST("vlse16.v v24, %1, t1")) { + return 1; + } + + if (VSTART_OVERFLOW_TEST("vlseg2e8.v v24, %1")) { + return 1; + } + + return 0; +} + +int main() +{ + return run_vstart_overflow_tests(); +} --=20 2.51.0 From nobody Mon Sep 8 09:47:36 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1756868585; cv=none; d=zohomail.com; s=zohoarc; b=P4yaBRfN6NKWpyEaAmYFx6NN7AquYSWlklExSFAXjwfNv3JuHkJ1roYoFJ9EVLLOLH7kReks+38XFkMxCm288zzpqBd7pT+mGcInQjqyweBf2PWIwtuS3oai4YTOFxrsxK8uSEIYyG5WaVnHR+GGZiD6ub1iBYDU1QJyKdEbI4I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756868585; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=X1j899pl22JtW9r5ApQKmblG74uGWH6HLNrTnd5Uu8o=; b=E8u+pzaw80fwyF0jCM1govjxqMXfTwm2uHhZtpfbh4AvQtBbJauEeQEpNjbumzYKJA+UsWu8C7He/KFkqWzFgshWOlcF65hqiEqSOr3e0/DS15P/qw2PBOBcYz7+NOMoM2fCR0XO2ArJ97VwsEcVsFiTUeNh0edbW7GYfQ52L0k= 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 175686858509518.652665708938684; Tue, 2 Sep 2025 20:03:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1utdlJ-0000Hd-Nt; Tue, 02 Sep 2025 23:02:17 -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 1utdky-0008QF-Tq; Tue, 02 Sep 2025 23:01:56 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1utdku-00077R-0e; Tue, 02 Sep 2025 23:01:56 -0400 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-772301f8ae2so3600729b3a.0; Tue, 02 Sep 2025 20:01:51 -0700 (PDT) Received: from lima-default (123.253.189.97.qld.leaptel.network. [123.253.189.97]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7725e419913sm6971760b3a.55.2025.09.02.20.01.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Sep 2025 20:01:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756868510; x=1757473310; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=X1j899pl22JtW9r5ApQKmblG74uGWH6HLNrTnd5Uu8o=; b=QS/H3wcWVQn9lEpW5pF+K7B5DHNNlb1yYhZ1KykJYJLayAgJN1E9ngHM0xC8v/Qw44 94I7qJT0JH9GIrf6YJulLIs9lTkcnXYzsh/krnFGC3SA5KIAwOxFEqXmepMxLGozL3ce UXnO+HHQYbZZ0Tsb6WGBgoopdB5HOwPKAYdddI+4VuBIWgsgg04U+I0ASRxEXN1SWrz6 dlAUXZpMpfdOCm5Otxvriu8NVjzJDAHExjOms7kX5sOK1RPAWFw1AP24WpLrSX2Ug0lJ lL5rGmDTC0q2E/LVpGyYfE+tbPQddRVNqrntcRZTGIM/I5NkcAVWDy4SKkctd3YIjwN7 5cRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756868510; x=1757473310; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=X1j899pl22JtW9r5ApQKmblG74uGWH6HLNrTnd5Uu8o=; b=KUOkQHeK+SLTZ3jheWkjgioZY4ERHoo9YgKx5G6H1HcI543xms6HuDPGLbVEkfsvIT Vp7At1I2o9boYGbnDWP+FVmk57uNIchaOyldgjVkkNljkKH0v95G+eeDfHSi6H03H4xg BeLIdDNSHYOCrWdGQ4lg8hlF7gISVTEMuWaqGdzHmy1q1nlyh5A7yenHX4uB+ySzHQ5z J5V1Z/QhWKuQnAePhZPJbUyR+umAH94+vrDwlieE/+OWEa1TUCCHdzpuMplKUDJcpJPH 30dbDa6rzZ1LFynebjghUjlReDNBVxcFcYt94YefmuKtZtkO7h6pdvoD5oinafIka3qL VyTQ== X-Forwarded-Encrypted: i=1; AJvYcCVaCXDUihOY8MXqaFzjA1+u3PIAA0ge+z+P0SEI+VGcKF1x1pUfOU/KN+7wRM4OK93FVlwQH9QReSp7@nongnu.org X-Gm-Message-State: AOJu0YyGqXh89/DklCeO57e9ewobaWTqxDTofN4egXv5cnqTPPmZvbHm 7HSx4VnpBxKHOIGkkmOnorL9DW4gkO4Kv6BUGsGNm+TuoroJzrwKhuIJAhf1MQ== X-Gm-Gg: ASbGnctyiD0v9kVCSENbyVqTe5Ddix6QJiixLLyNdIfmzxaEJ4LTuB4RhfS/Nohg0ht YGdfaQOC5+MZsRh1+5J6lX+fiGCPhWoJVZnO23s/ziw4VEUeUs/YTRnpMcvOHm8WFAJUN0QxDaW azt31KV20JKMqnL1livgJq0oTOYDks/VlrGg4jc/8jJ8CYQWWS/DhLdKdNxa6yso7BSlszTScCZ gY9MCqdAyobtAsAGOEhYJT0xKVh0E0sUyDtjmNxUXRVBkjlGBjBrrsh+8BUiHp982iLeh5alBfQ NYG7kzzqBjE7ArL2dRIZKW80uffMIRfc/erTbEyqvoZjBHIBj48jm+jY+BxDZm2gelLekgaHTUd yI+7O0vew2jGJuzNd2TQqqTBuJa2txFsZ7YwOr0WiZok7Mk6lM2weoQ1kFhSwMKM2Tl3nO9OfPg == X-Google-Smtp-Source: AGHT+IE5kv3s6SQvz3CK8rx/1SR4RFv554LJjw8epiQEhdcAh3WDdcJuS/jccu1rTdQV6tkSA/BJXw== X-Received: by 2002:a05:6a00:2306:b0:76e:885a:c32c with SMTP id d2e1a72fcca58-7723e349ebamr14672297b3a.26.1756868509798; Tue, 02 Sep 2025 20:01:49 -0700 (PDT) From: Nicholas Piggin To: qemu-riscv@nongnu.org Cc: Nicholas Piggin , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-devel@nongnu.org, Chao Liu Subject: [PATCH 3/3] tests/tcg: Add riscv test for interrupted vector ops Date: Wed, 3 Sep 2025 13:01:13 +1000 Message-ID: <20250903030114.274535-4-npiggin@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250903030114.274535-1-npiggin@gmail.com> References: <20250903030114.274535-1-npiggin@gmail.com> 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=2607:f8b0:4864:20::434; envelope-from=npiggin@gmail.com; helo=mail-pf1-x434.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, FREEMAIL_FROM=0.001, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1756868586199116600 Content-Type: text/plain; charset="utf-8" riscv vector instructions can be interrupted with a trap, and partial completion is recorded in the vstart register. Some causes are implementation dependent, for example an asynchronous interrupt (which I don't think TCG allows). Others are architectural, typically memory access faults on vector load/store instructions. Add some TCG tests for interrupting vector load instructions and resuming partially completed ones. This would have caught a recent (now reverted) regression in vector stride load implementation, commit 28c12c1f2f50d ("Generate strided vector loads/stores with tcg nodes.") Signed-off-by: Nicholas Piggin --- tests/tcg/riscv64/Makefile.target | 5 + tests/tcg/riscv64/test-interrupted-v.c | 208 +++++++++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 tests/tcg/riscv64/test-interrupted-v.c diff --git a/tests/tcg/riscv64/Makefile.target b/tests/tcg/riscv64/Makefile= .target index 19a49b6467..8f4690ac57 100644 --- a/tests/tcg/riscv64/Makefile.target +++ b/tests/tcg/riscv64/Makefile.target @@ -23,3 +23,8 @@ run-test-fcvtmod: QEMU_OPTS +=3D -cpu rv64,d=3Dtrue,zfa= =3Dtrue TESTS +=3D test-vstart-overflow test-vstart-overflow: CFLAGS +=3D -march=3Drv64gcv run-test-vstart-overflow: QEMU_OPTS +=3D -cpu rv64,v=3Don + +# Test for interrupted vector instructions +TESTS +=3D test-interrupted-v +test-interrupted-v: CFLAGS +=3D -march=3Drv64gcv +run-test-interrupted-v: QEMU_OPTS +=3D -cpu rv64,v=3Don diff --git a/tests/tcg/riscv64/test-interrupted-v.c b/tests/tcg/riscv64/tes= t-interrupted-v.c new file mode 100644 index 0000000000..db4fb6092f --- /dev/null +++ b/tests/tcg/riscv64/test-interrupted-v.c @@ -0,0 +1,208 @@ +/* + * Test for interrupted vector operations. + * + * Some vector instructions can be interrupted partially complete, vstart = will + * be set to where the operation has progressed to, and the instruction ca= n be + * re-executed with vstart !=3D 0. It is implementation dependent as to wh= at + * instructions can be interrupted and what vstart values are permitted wh= en + * executing them. Vector memory operations can typically be interrupted + * (as they can take page faults), so these are easy to test. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static unsigned long page_size; + +static volatile int nr_segv; +static volatile unsigned long fault_start, fault_end; + +/* + * Careful: qemu-user does not save/restore vector state in + * signals yet, so any library or compiler autovec code will + * corrupt our test. + * + * Do only minimal work in the signal handler. + */ +static void SEGV_handler(int signo, siginfo_t *info, void *context) +{ + unsigned long page =3D (unsigned long)info->si_addr & + ~(unsigned long)(page_size - 1); + + assert((unsigned long)info->si_addr >=3D fault_start); + assert((unsigned long)info->si_addr < fault_end); + mprotect((void *)page, page_size, PROT_READ); + nr_segv++; +} + +/* Use noinline to make generated code easier to inspect */ +static __attribute__((noinline)) +uint8_t unit_load(uint8_t *mem, size_t nr, bool ff) +{ + size_t vl; + vuint8m1_t vec, redvec, sum; + + vl =3D __riscv_vsetvl_e8m1(nr); + if (ff) { + vec =3D __riscv_vle8ff_v_u8m1(mem, &vl, vl); + } else { + vec =3D __riscv_vle8_v_u8m1(mem, vl); + } + redvec =3D __riscv_vmv_v_x_u8m1(0, vl); + sum =3D __riscv_vredsum_vs_u8m1_u8m1(vec, redvec, vl); + return __riscv_vmv_x_s_u8m1_u8(sum); +} + +static __attribute__((noinline)) +uint8_t seg2_load(uint8_t *mem, size_t nr, bool ff) +{ + size_t vl; + vuint8m1x2_t segvec; + vuint8m1_t vec, redvec, sum; + + vl =3D __riscv_vsetvl_e8m1(nr); + if (ff) { + segvec =3D __riscv_vlseg2e8ff_v_u8m1x2(mem, &vl, vl); + } else { + segvec =3D __riscv_vlseg2e8_v_u8m1x2(mem, vl); + } + vec =3D __riscv_vadd_vv_u8m1(__riscv_vget_v_u8m1x2_u8m1(segvec, 0), + __riscv_vget_v_u8m1x2_u8m1(segvec, 1), vl); + redvec =3D __riscv_vmv_v_x_u8m1(0, vl); + sum =3D __riscv_vredsum_vs_u8m1_u8m1(vec, redvec, vl); + return __riscv_vmv_x_s_u8m1_u8(sum); +} + +static __attribute__((noinline)) +uint8_t strided_load(uint8_t *mem, size_t nr, size_t stride) +{ + size_t vl; + vuint8m1_t vec, redvec, sum; + + vl =3D __riscv_vsetvl_e8m1(nr); + vec =3D __riscv_vlse8_v_u8m1(mem, stride, vl); + redvec =3D __riscv_vmv_v_x_u8m1(0, vl); + sum =3D __riscv_vredsum_vs_u8m1_u8m1(vec, redvec, vl); + return __riscv_vmv_x_s_u8m1_u8(sum); +} + +static __attribute__((noinline)) +uint8_t indexed_load(uint8_t *mem, size_t nr, uint32_t *indices) +{ + size_t vl; + vuint32m4_t idx; + vuint8m1_t vec, redvec, sum; + + vl =3D __riscv_vsetvl_e8m1(nr); + idx =3D __riscv_vle32_v_u32m4(indices, vl); + vec =3D __riscv_vloxei32_v_u8m1(mem, idx, vl); + redvec =3D __riscv_vmv_v_x_u8m1(0, vl); + sum =3D __riscv_vredsum_vs_u8m1_u8m1(vec, redvec, vl); + return __riscv_vmv_x_s_u8m1_u8(sum); +} + +/* Use e8 elements, 128-bit vectors */ +#define NR_ELEMS 16 + +static int run_interrupted_v_tests(void) +{ + struct sigaction act =3D { 0 }; + uint8_t *mem; + uint32_t indices[NR_ELEMS]; + int i; + + page_size =3D sysconf(_SC_PAGESIZE); + + act.sa_flags =3D SA_SIGINFO; + act.sa_sigaction =3D &SEGV_handler; + if (sigaction(SIGSEGV, &act, NULL) =3D=3D -1) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + + mem =3D mmap(NULL, NR_ELEMS * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + assert(mem !=3D MAP_FAILED); + madvise(mem, NR_ELEMS * page_size, MADV_NOHUGEPAGE); + + /* Unit-stride tests load memory crossing a page boundary */ + memset(mem, 0, NR_ELEMS * page_size); + for (i =3D 0; i < NR_ELEMS; i++) { + mem[page_size - NR_ELEMS + i] =3D 3; + } + for (i =3D 0; i < NR_ELEMS; i++) { + mem[page_size + i] =3D 5; + } + + nr_segv =3D 0; + fault_start =3D (unsigned long)&mem[page_size - (NR_ELEMS / 2)]; + fault_end =3D fault_start + NR_ELEMS; + mprotect(mem, page_size * 2, PROT_NONE); + assert(unit_load(&mem[page_size - (NR_ELEMS / 2)], NR_ELEMS, false) + =3D=3D 8 * NR_ELEMS / 2); + assert(nr_segv =3D=3D 2); + + nr_segv =3D 0; + fault_start =3D (unsigned long)&mem[page_size - NR_ELEMS]; + fault_end =3D fault_start + NR_ELEMS * 2; + mprotect(mem, page_size * 2, PROT_NONE); + assert(seg2_load(&mem[page_size - NR_ELEMS], NR_ELEMS, false) + =3D=3D 8 * NR_ELEMS); + assert(nr_segv =3D=3D 2); + + nr_segv =3D 0; + fault_start =3D (unsigned long)&mem[page_size - (NR_ELEMS / 2)]; + fault_end =3D fault_start + (NR_ELEMS / 2); + mprotect(mem, page_size * 2, PROT_NONE); + assert(unit_load(&mem[page_size - (NR_ELEMS / 2)], NR_ELEMS, true) + =3D=3D 3 * NR_ELEMS / 2); + assert(nr_segv =3D=3D 1); /* fault-first does not fault the second pag= e */ + + nr_segv =3D 0; + fault_start =3D (unsigned long)&mem[page_size - NR_ELEMS]; + fault_end =3D fault_start + NR_ELEMS; + mprotect(mem, page_size * 2, PROT_NONE); + assert(seg2_load(&mem[page_size - NR_ELEMS], NR_ELEMS * 2, true) + =3D=3D 3 * NR_ELEMS); + assert(nr_segv =3D=3D 1); /* fault-first does not fault the second pag= e */ + + /* Following tests load one element from first byte of each page */ + mprotect(mem, page_size * 2, PROT_READ | PROT_WRITE); + memset(mem, 0, NR_ELEMS * page_size); + for (i =3D 0; i < NR_ELEMS; i++) { + mem[i * page_size] =3D 3; + indices[i] =3D i * page_size; + } + + nr_segv =3D 0; + fault_start =3D (unsigned long)mem; + fault_end =3D fault_start + NR_ELEMS * page_size; + mprotect(mem, NR_ELEMS * page_size, PROT_NONE); + assert(strided_load(mem, NR_ELEMS, page_size) =3D=3D 3 * NR_ELEMS); + assert(nr_segv =3D=3D NR_ELEMS); + + nr_segv =3D 0; + fault_start =3D (unsigned long)mem; + fault_end =3D fault_start + NR_ELEMS * page_size; + mprotect(mem, NR_ELEMS * page_size, PROT_NONE); + assert(indexed_load(mem, NR_ELEMS, indices) =3D=3D 3 * NR_ELEMS); + assert(nr_segv =3D=3D NR_ELEMS); + + munmap(mem, NR_ELEMS * page_size); + + return 0; +} + +int main(void) +{ + return run_interrupted_v_tests(); +} --=20 2.51.0