From nobody Thu Dec 18 18:57:16 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=yeah.net ARC-Seal: i=1; a=rsa-sha256; t=1755286730; cv=none; d=zohomail.com; s=zohoarc; b=IWkCYqIa0OGEdXcjpYMLXAYGpC93b1A+A1b6zEo41kagV1YLaLwDCNl97x0eKbPqu6GgAAEb0vHQDhkaRiHBRD9j8HMQ3jvYNsvFNpDPErENoIIyGJFTM0khHR954RsgnDkEptoKGCgjsZKPWN304keFzEFwuADED4Ph8V4Th+M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755286730; 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=53Gh2kOAmhxsooRyWujrc2UN5c9CsonrKQBJB+x9m4E=; b=J0q8HNTaqEaIVAtAohXctCVX/X7LpUwa27ppz8NuZCgooErXNgymbVNYpnc2qjGB/bhGE6cYW9071X6yt2L/RMqrRL2LQqeaqWoK4CUqib29XUTDylYcVD/gRUH7/dO7O/moI4b3d6aujimdN1/K+QTGeLSgmqytxIax2H3oTOU= 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 175528673006512.413681576110207; Fri, 15 Aug 2025 12:38:50 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1un0Fw-0002OF-4I; Fri, 15 Aug 2025 15:38:28 -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 1un0Fs-0002MO-LD; Fri, 15 Aug 2025 15:38:24 -0400 Received: from mail-m16.yeah.net ([1.95.21.16]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1un0Fo-0005xx-Dc; Fri, 15 Aug 2025 15:38:24 -0400 Received: from ZEVORN-PC (unknown []) by gzsmtp2 (Coremail) with SMTP id Ms8vCgBHf_eFjJ9oXmS4Ag--.35272S3; Sat, 16 Aug 2025 03:37:42 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yeah.net; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=53Gh2kOAmhxsooRyWujrc2UN5c9CsonrKQBJB+x9m4E=; b=cWV4+hIiKss4WydcFnGPvWKNCsZK2JEekTJd22disyUM0gRrQgiVv26xkitmdP lSydPkr4c/jfM5onWWwCC8j4WsOAOEiLu1kRGAz2/2KvNLApvzjAlQOhS48+84AJ gHJi+pArvDn+LNJXu5uCVAJkfTF2iNIGh91ZiwT1xZcIg= From: Chao Liu To: paolo.savini@embecosm.com, dbarboza@ventanamicro.com, ebiggers@kernel.org, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com Cc: qemu-riscv@nongnu.org, qemu-devel@nongnu.org, Chao Liu Subject: [PATCH v1 1/2] target/riscv: fix vector register address calculation in strided LD/ST Date: Sat, 16 Aug 2025 03:37:34 +0800 Message-ID: X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Ms8vCgBHf_eFjJ9oXmS4Ag--.35272S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Cw1UuF1xtr1DGFW5Xr13Jwb_yoW8ZFy5pr sYkry7Kr45WFWfZFZ2yr48JryYqayFgr4jkw4qgwn8XFW5Xr47tF4vga42kF18JrWxur12 9ayUuay5uw13uFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UaD73UUUUU= X-Originating-IP: [240e:b8f:29d6:3800:f820:962e:d888:73d] X-CM-SenderInfo: pfkd0hxolxq5hhdkh0dhw/1tbiIQdGbmifjIfXBwAA3i 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=1.95.21.16; envelope-from=chao.liu@yeah.net; helo=mail-m16.yeah.net 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, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=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 @yeah.net) X-ZM-MESSAGEID: 1755286732904124100 his patch fixes a critical bug in the RISC-V vector instruction translation= that caused incorrect data handling in strided load operations (e.g., vlsseg8e32= ). Problem Description: The `get_log2` function in `trans_rvv.c.inc` returned a value 1 higher than= the actual log2 value. For example, get_log2(4) incorrectly returned 3 instead = of 2. This led to erroneous vector register offset calculations, resulting in data overlap where bytes 32-47 were incorrectly copied to positions 16-31 in Cha= Cha20 encryption code. rvv_test_func: vsetivli zero, 1, e32, m1, ta, ma li t0, 64 vlsseg8e32.v v0, (a0), t0 addi a0, a0, 32 vlsseg8e32.v v8, (a0), t0 vssseg8e32.v v0, (a1), t0 addi a1, a1, 32 vssseg8e32.v v8, (a1), t0 ret Analysis: The original implementation counted the number of right shifts until zero, including the final shift that reduced the value to zero: static inline uint32_t get_log2(uint32_t a) { uint32_t i =3D 0; for (; a > 0;) { a >>=3D 1; i++; } return i; // Returns 3 for a=3D4 (0b100 =E2=86=92 0b10 =E2=86=92 0b1 = =E2=86=92 0b0) } Fix: The corrected function stops shifting when only the highest bit remains and handles the special case of a=3D0: static inline uint32_t get_log2(uint32_t a) { uint32_t i =3D 0; if (a =3D=3D 0) { return i; // Handle edge case } for (; a > 1; a >>=3D 1) { i++; } return i; // Now returns 2 for a=3D4 } Fixes: 28c12c1f2f ("Generate strided vector loads/stores with tcg nodes.") Signed-off-by: Chao Liu --- target/riscv/insn_trans/trans_rvv.c.inc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/target/riscv/insn_trans/trans_rvv.c.inc b/target/riscv/insn_tr= ans/trans_rvv.c.inc index 2b6077ac06..f50b62b1d8 100644 --- a/target/riscv/insn_trans/trans_rvv.c.inc +++ b/target/riscv/insn_trans/trans_rvv.c.inc @@ -877,7 +877,10 @@ static inline uint32_t MAXSZ(DisasContext *s) static inline uint32_t get_log2(uint32_t a) { uint32_t i =3D 0; - for (; a > 0;) { + if (a =3D=3D 0) { + return i; + } + for (; a > 1;) { a >>=3D 1; i++; } --=20 2.50.1 From nobody Thu Dec 18 18:57:16 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=yeah.net ARC-Seal: i=1; a=rsa-sha256; t=1755286720; cv=none; d=zohomail.com; s=zohoarc; b=V1JKa7IQkwxpFkkNDUYpTFxY8/6MFDqQd4lb5RNAzu9tynxkQlBgeKpbQoWzJRo69xSJurWPzv8s96/IPxYvPQusWpkcqoOAUw9CLR+JtxPxGU1MyULpniJrFaMmlK4VA8meyuQC/1bINiq31wH7K0ne91gG44J4r34gr2RsVSM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755286720; 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=PNg+wTrmTTLoLgU6tKSOWk/nt3Gsu/A6S6G6VoF0+Lw=; b=LS5egz1VFX00wLw6SxlJrZutb3cZid0ilrUaSOvSIhXIfk/PUMeHu93DUfiBFW7z6a/k1QKcmYuyt5iCKmDxCf+g2BDAoUq5XaGxu7B+f6IUZod+cjyghP0MihB8bdcWgcVtBM5gavRTqum33WgbSXUcCnDx5PMFqMN4d/RXe4w= 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 1755286720359120.40087373885228; Fri, 15 Aug 2025 12:38:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1un0Fu-0002Ni-6h; Fri, 15 Aug 2025 15:38:26 -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 1un0Fs-0002Lj-CA; Fri, 15 Aug 2025 15:38:24 -0400 Received: from mail-m16.yeah.net ([220.197.32.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1un0Fo-0005xy-BA; Fri, 15 Aug 2025 15:38:24 -0400 Received: from ZEVORN-PC (unknown []) by gzsmtp2 (Coremail) with SMTP id Ms8vCgBHf_eFjJ9oXmS4Ag--.35272S4; Sat, 16 Aug 2025 03:37:43 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yeah.net; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=PN g+wTrmTTLoLgU6tKSOWk/nt3Gsu/A6S6G6VoF0+Lw=; b=ht2yBPHvshOBFbcwKl vI8R9XzRvEz1ELGzaqnH++EuOlVcBP3uHhAjYqDIT+19i+3f6558ZWyyS4ntG4Fa Rfzq7Lc/WFE1wZCMw7xoBusddiSXtTm+Z9uphCM6BuGUKopAhgar9AKG4fvK2MCZ KZQp69fdqQfsjwlawNbVWM6Eg= From: Chao Liu To: paolo.savini@embecosm.com, dbarboza@ventanamicro.com, ebiggers@kernel.org, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com Cc: qemu-riscv@nongnu.org, qemu-devel@nongnu.org, Chao Liu Subject: [PATCH v1 2/2] tests/tcg/riscv64: Add test for vlsseg8e32 instruction Date: Sat, 16 Aug 2025 03:37:35 +0800 Message-ID: <9f9171bf03fb15c4caa9196dd7d3b240d9d76785.1755286233.git.chao.liu@yeah.net> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Ms8vCgBHf_eFjJ9oXmS4Ag--.35272S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxWr4DXr1UXr1xWF1UCr4rXwb_yoW5urW3pr 15Gw4qkr4vq343Ka43GF1UuFyrWr4F9F1UXFy8K3W09rW8ZFZruF4ktFWUtFyrAw4UCr13 uFnYqF1rKanxA3JanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07Uf9NsUUUUU= X-Originating-IP: [240e:b8f:29d6:3800:f820:962e:d888:73d] X-CM-SenderInfo: pfkd0hxolxq5hhdkh0dhw/1tbiBAyqKGifE7XlUgAAss 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=220.197.32.18; envelope-from=chao.liu@yeah.net; helo=mail-m16.yeah.net 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, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=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 @yeah.net) X-ZM-MESSAGEID: 1755286724890124100 Content-Type: text/plain; charset="utf-8" This case, it copied 64 bytes from a0 to a1 with vlsseg8e32. Signed-off-by: Chao Liu --- tests/tcg/riscv64/Makefile.softmmu-target | 8 +- tests/tcg/riscv64/test-vlsseg8e32.S | 108 ++++++++++++++++++++++ 2 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/riscv64/test-vlsseg8e32.S diff --git a/tests/tcg/riscv64/Makefile.softmmu-target b/tests/tcg/riscv64/= Makefile.softmmu-target index 7c1d44d3f4..c3c5b66713 100644 --- a/tests/tcg/riscv64/Makefile.softmmu-target +++ b/tests/tcg/riscv64/Makefile.softmmu-target @@ -7,18 +7,22 @@ VPATH +=3D $(TEST_SRC) =20 LINK_SCRIPT =3D $(TEST_SRC)/semihost.ld LDFLAGS =3D -T $(LINK_SCRIPT) -CFLAGS +=3D -g -Og +CFLAGS +=3D -march=3Drv64gcv -mabi=3Dlp64d -g -Og =20 %.o: %.S $(CC) $(CFLAGS) $< -Wa,--noexecstack -c -o $@ %: %.o $(LINK_SCRIPT) $(LD) $(LDFLAGS) $< -o $@ =20 -QEMU_OPTS +=3D -M virt -display none -semihosting -device loader,file=3D +QEMU_OPTS +=3D -M virt -cpu rv64,v=3Dtrue -display none -semihosting -devi= ce loader,file=3D =20 EXTRA_RUNS +=3D run-issue1060 run-issue1060: issue1060 $(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<) =20 +EXTRA_RUNS +=3D run-vlsseg8e32 +run-vlsseg8e32: test-vlsseg8e32 + $(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<) + # We don't currently support the multiarch system tests undefine MULTIARCH_TESTS diff --git a/tests/tcg/riscv64/test-vlsseg8e32.S b/tests/tcg/riscv64/test-v= lsseg8e32.S new file mode 100644 index 0000000000..2861ff3702 --- /dev/null +++ b/tests/tcg/riscv64/test-vlsseg8e32.S @@ -0,0 +1,108 @@ +# +# QEMU RISC-V Vector Strided Load Instruction testcase +# +# Copyright (c) 2025 Chao Liu chao.liu@yeah.net +# +# SPDX-License-Identifier: GPL-2.0-or-later +# + + .option norvc + + .section .data + .align 4 +source_data: + .asciz "Test the vlsseg8e32 insn by copy 64b and verifying correctness." + .equ source_len, 64 + + .text + .global _start +_start: + lla t0, trap + csrw mtvec, t0 + +enable_rvv: + + li x15, 0x800000000024112d + csrw 0x301, x15 + li x1, 0x2200 + csrr x2, mstatus + or x2, x2, x1 + csrw mstatus, x2 + +rvv_test_func: + la a0, source_data + li a1, 0x80020000 + vsetivli zero, 1, e32, m1, ta, ma + li t0, 64 + + vlsseg8e32.v v0, (a0), t0 + addi a0, a0, 32 + vlsseg8e32.v v8, (a0), t0 + + vssseg8e32.v v0, (a1), t0 + addi a1, a1, 32 + vssseg8e32.v v8, (a1), t0 + +compare_start: + la a0, source_data + li a1, 0x80020000 + li t0, 0 + li t1, source_len + +compare_loop: + # when t0 >=3D len, compare end + bge t0, t1, compare_done + + lb t2, 0(a0) + lb t3, 0(a1) + bne t2, t3, compare_fail + + addi a0, a0, 1 + addi a1, a1, 1 + addi t0, t0, 1 + j compare_loop + +compare_done: + # compare ok, return 0 + li a0, 0 + j _exit + +compare_fail: + # compare failed, return 2 + li a0, 2 + j _exit + +trap: + # When an instruction traps, compare it to the insn in memory. + csrr t0, mepc + csrr t1, mtval + lwu t2, 0(t0) + bne t1, t2, fail + + # Skip the insn and continue. + addi t0, t0, 4 + csrw mepc, t0 + mret + +fail: + 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.50.1