Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CF18BC624D6 for ; Thu, 3 Sep 2026 01:37:43 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x1wNv-0002C1-E8; Wed, 02 Sep 2026 21:36:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1x1wNt-0002BY-NB for qemu-riscv@nongnu.org; Wed, 02 Sep 2026 21:36:57 -0400 Received: from mail-pj2-x05.google.com ([2607:f8b0:4864:39::5]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1x1wNr-0001hV-8c for qemu-riscv@nongnu.org; Wed, 02 Sep 2026 21:36:57 -0400 Received: by mail-pj2-x05.google.com with SMTP id d9443c01a7336-2d942de6574so10299605ad.0 for ; Wed, 02 Sep 2026 18:36:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788399413; x=1789004213; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=Y+Er1ySClcSQZ5eIZigpbczfs6irJAot5XGj3pf/ZPY=; b=a27111/Cle3TOE5GhT8YMrLQGP0FOBF/WRKvNpSBvmAKpXl7RnDMMDNQ6vESBL8UeQ 3zT0CfQSC7MXahPnWAkNmMRBP5MQds+y8pDaU6fH24jpbsgX1x31eK+GUhqEt2kTUDtr J9kJw+6fmN7+7I7UXygdD7BaG52rFqGpmlqhPFiF37oZvVGSlBxW8ZaDq+P6LZIsdDo7 rSP7Xqtw3S86Y/B4TtvWedFjle+CDOFkh/L4C2C7C2YFWDZdzmBJ47nUIbGyG2DFgOu0 D9A+jC+tho52YBD2GU2c6fpdktiHr5k0IejKxCnv7pGYx4d9DLUU4/WALmyg7bGqtWau frQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788399413; x=1789004213; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Y+Er1ySClcSQZ5eIZigpbczfs6irJAot5XGj3pf/ZPY=; b=bosceDaB5L+A28ZPY0ypO76fHTWEykRCdpHs/XLOCmWoRfQreyO6zlMVxBgf9Py5KC wtC/Kq8zvkKzV+D625ZXp9ELRWTpLlyG/6y+RdyHNwvsa8fUFRlrHGJP7shO4+2kn6bY O/ob6Z1yBd+67mQbOOHstASVL9jKvREkCmiNUUPSCNOXM6tXIJHg7uoWv7JbjN8+I0dP EO0joVNXVEB7a1nf2145BaFc2zwFBlyRj3NZKZuuV2JsqBn4cc344ZHHUIrPNUssPeCu cMHJdYQeKAxQVh4nqcI3uy8vmCg/w12TtRM+tbHHy4fhoYfMtend/xIF3YI0ZT1cRS34 oU7Q== X-Gm-Message-State: AFuF++kyUN6a8IXbbp+Wwu9clGSejR4ypxlzO6QXTDOvOwuAlVDtzmWe eRQUfHXyxE9n9xRCsKWLAzLPRzTK4usI+Ei+6gBxknmWNVAKOZuUOO36 X-Gm-Gg: AYBFou3pHuV4HX+krEgvQXY+OttXm9eLQjOG3sWJLFPwhpRsCPQBf/HbL7A6rU59pFV p+hT1j37FM+H28S/0IgOutYgKiWayGUEe5brstThUfiJQrifL9aPkac5x3nBhTuDKCzHyYFzgBV 2yyoRKVrlZrYnwY3GRK4UZBYJ3peEG0yNqn2wxOLMUx5W8Rklyuc5K2HM2OinTS4TrnwYH6Vxhe eLgYWib6dNxiLysDaa9C6RE2omJaGzXXLshg4zkt9GjSUVbf/TTXkbfz38yz7uhsUQGK7pgbhZw cIpCWE+t3ntbBRmd0Hko5WiAK0kjoZ9cFt6v55kzf2rTn86+CcvQ/qxocZCwGsPQIlWF3sirv05 3X+KzqWqmb1N4tHzcywjr6yleai1objMmgEtvmrrRAf2GkJDYDPLdayJEaEITQvNlAjGxnoeDYA Mgf+KljaKjDhsdsC8sSml1gv/ZGRhFgWtdQNXd5POXlLeLyYucDp4DAymJAi37jxIzgGA/YRfuh HoHbaMeSBehZYh2 X-Received: by 2002:a17:902:da87:b0:2d6:d3f9:d394 with SMTP id d9443c01a7336-2daec43fd64mr117603075ad.0.1788399412927; Wed, 02 Sep 2026 18:36:52 -0700 (PDT) Received: from Dell-WorkStation.localdomain ([149.118.62.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2dafe9268d6sm2136705ad.31.2026.09.02.18.36.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Sep 2026 18:36:52 -0700 (PDT) From: Zephyr Li To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , Zephyr Li Subject: [PATCH v2] target/riscv: save ELP in MPELP for NMIE=0 exceptions Date: Thu, 3 Sep 2026 09:36:26 +0800 Message-ID: <20260903013627.1105-1-fritchleybohrer@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:39::5; envelope-from=fritchleybohrer@gmail.com; helo=mail-pj2-x05.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=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-riscv@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-riscv-bounces+qemu-riscv=archiver.kernel.org@nongnu.org Sender: qemu-riscv-bounces+qemu-riscv=archiver.kernel.org@nongnu.org The Smrnmi specification, section 8.5, "RNMI Operation", states: If the hart encounters an exception while executing in M-mode with the mnstatus.NMIE bit clear, the exception is an RNMI exception. Trap state is still saved in the regular M-mode CSRs and the handler returns with MRET. riscv_cpu_do_interrupt() instead saves ELP in mnstatus.MNPELP on this path. MRET restores ELP from mstatus.MPELP, so the expected landing-pad state is lost. Always save ELP in mstatus.MPELP for M-mode exceptions. Keep MNPELP for the actual RNMI interrupt path, which returns with MNRET. Add a TCG test that checks ELP preservation across an NMIE=0 M-mode exception and MRET. Fixes: 0266fd8b56a4 ("target/riscv: Add Zicfilp support for Smrnmi") Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4223 Reviewed-by: Daniel Henrique Barboza Signed-off-by: Zephyr Li --- Changes in v2: - Quote the relevant Smrnmi specification text in the commit message. --- target/riscv/tcg/cpu_helper.c | 18 +--- tests/tcg/riscv64/Makefile.softmmu-target | 8 ++ tests/tcg/riscv64/test-zicfilp-smrnmi.S | 117 ++++++++++++++++++++++ 3 files changed, 127 insertions(+), 16 deletions(-) create mode 100644 tests/tcg/riscv64/test-zicfilp-smrnmi.S diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c index 07d9222652..a0d79b33a5 100644 --- a/target/riscv/tcg/cpu_helper.c +++ b/target/riscv/tcg/cpu_helper.c @@ -2264,23 +2264,9 @@ void riscv_cpu_do_interrupt(CPUState *cs) src = env->sepc; } else { - /* - * If the hart encounters an exception while executing in M-mode - * with the mnstatus.NMIE bit clear, the exception is an RNMI exception. - */ - nnmi_excep = cpu->cfg.ext_smrnmi && - !get_field(env->mnstatus, MNSTATUS_NMIE) && - !async; - - /* handle the trap in M-mode */ - /* save elp status */ + /* Save ELP for MRET. */ if (cpu_get_fcfien(env)) { - if (nnmi_excep) { - env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, - env->elp); - } else { - env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, env->elp); - } + env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, env->elp); } if (riscv_has_ext(env, RVH)) { diff --git a/tests/tcg/riscv64/Makefile.softmmu-target b/tests/tcg/riscv64/Makefile.softmmu-target index 6a219c306c..8522c3fe6f 100644 --- a/tests/tcg/riscv64/Makefile.softmmu-target +++ b/tests/tcg/riscv64/Makefile.softmmu-target @@ -45,6 +45,14 @@ comma:= , run-test-crc32: test-crc32 $(call run-test, $<, $(QEMU) -cpu rv64$(comma)xlrbr=true $(QEMU_OPTS)$<) +EXTRA_RUNS += run-test-zicfilp-smrnmi +run-test-zicfilp-smrnmi: test-zicfilp-smrnmi + $(call run-test, $<, \ + $(QEMU) \ + -cpu rv64$(comma)zicsr=true$(comma)zicfilp=true$(comma)smrnmi=true \ + -global rv64-riscv-cpu.rnmi-exception-vector=0x80000100 \ + $(QEMU_OPTS)$<) + # Zicclsm: misaligned load/store support. Assemble one source twice: the # default build expects every misaligned access to succeed (zicclsm=true), # the -DZICCLSM_DISABLED build expects every one to trap (zicclsm=false). diff --git a/tests/tcg/riscv64/test-zicfilp-smrnmi.S b/tests/tcg/riscv64/test-zicfilp-smrnmi.S new file mode 100644 index 0000000000..b5965bf705 --- /dev/null +++ b/tests/tcg/riscv64/test-zicfilp-smrnmi.S @@ -0,0 +1,117 @@ +/* + * Test that an M-mode exception taken with mnstatus.NMIE clear saves ELP in + * mstatus.MPELP, so that it is restored by MRET. MNPELP is reserved for the + * actual RNMI path, which returns with MNRET. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + + .option norvc + + .equ CSR_MNSTATUS, 0x744 + .equ CSR_MSECCFG, 0x747 + .equ MNSTATUS_NMIE, 1 << 3 + .equ MNSTATUS_MNPELP, 1 << 9 + .equ MSECCFG_MLPE, 1 << 10 + .equ MSTATUS_MPELP, 1 << 41 + .equ EXCP_INST_ACCESS_FAULT, 1 + .equ EXCP_SW_CHECK, 18 + .equ SW_CHECK_FCFI_TVAL, 2 + + .text + .global _start +_start: + /* Verify that NMIE is clear before exercising the exception path. */ + csrr t0, CSR_MNSTATUS + andi t0, t0, MNSTATUS_NMIE + bnez t0, fail_nmie + + /* Enable landing-pad checks in M-mode, then start a new TB. */ + li t0, MSECCFG_MLPE + csrs CSR_MSECCFG, t0 + j tracked_jump + +tracked_jump: + /* Make a Zicfilp-tracked indirect jump to an address that faults. */ + li t1, 0 + jalr zero, 0(t1) + + /* + * The instruction access fault should have redirected to the handler. + */ + li a0, 1 + j _exit + +fail_nmie: + li a0, 6 + j _exit + + /* Must match rnmi-exception-vector in Makefile.softmmu-target. */ + .org 0x100 +rnmi_exception: + csrr t0, mcause + li t1, EXCP_INST_ACCESS_FAULT + beq t0, t1, handle_fetch_fault + li t1, EXCP_SW_CHECK + beq t0, t1, handle_sw_check + + li a0, 4 + j _exit + +handle_fetch_fault: + /* This is an M-mode exception and will return with MRET. */ + csrr t0, mstatus + li t1, MSTATUS_MPELP + and t0, t0, t1 + beqz t0, fail_mpelp + + csrr t0, CSR_MNSTATUS + andi t0, t0, MNSTATUS_MNPELP + bnez t0, fail_mnpelp + + lla t0, resume_non_lpad + csrw mepc, t0 + mret + +resume_non_lpad: + /* MRET must restore ELP, so this instruction must not retire. */ + li a0, 1 + j _exit + +handle_sw_check: + csrr t0, mtval + li t1, SW_CHECK_FCFI_TVAL + bne t0, t1, fail_tval + + li a0, 0 + j _exit + +fail_mpelp: + li a0, 2 + j _exit + +fail_mnpelp: + li a0, 3 + j _exit + +fail_tval: + li a0, 5 + +_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 -- 2.43.0