From patchwork Fri Aug 28 06:09:31 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nia Su X-Patchwork-Id: 2293901 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=VDV2Ecl/; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=Cj6Wf3bn; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4hWSf46X4Fz1xvq for ; Fri, 28 Aug 2026 16:09:48 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=F2gsdpLTeMdCNYVug/fCsq8xJpgt+sWYtoNhAKot0Og=; b=VDV2Ecl/Io+jjg PjrzKQQnqR4u+35YFvQP6stVBDDTMEMUZIhbeHUf1/naUzzWxNE6DtaGgHNWpN5WT7A+dd0v0eGz0 ApbpFjnpE36Vg9rZp0DHlAx+q5dwSKoQ3wk9hBtQoJYRvLYYKjwMvO+rqTlY5Wz31a4tfZDQ0MO5W 8F435NmfOQUTCjnDdptMkCgEsiW5fhNfanhqsyN4db9TbvvyqEtaI93SoeZVoNCCTUiMpI9k4ndoI 7cI2zu2WH8lJvtQIWdPulNOKZCEni/xQ3d3scGcch2qFWIy6bkbtcge9wIm51waT5D6J7qQFjob0K Dffi/K1MTsppetMFRw7A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wzpmV-00000005BPq-0fMl; Fri, 28 Aug 2026 06:09:39 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wzpmT-00000005BOj-06A2 for opensbi@lists.infradead.org; Fri, 28 Aug 2026 06:09:38 +0000 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-38759bcd877so605245a91.2 for ; Thu, 27 Aug 2026 23:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1787897376; x=1788502176; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CCghARvrlSSDwf0Pbr/0UIqwsTlbjghwX717x3hCQWo=; b=Cj6Wf3bnvPBpKOEPffN4RZEyRXf3sSmA7l5MplGT7P8dXKPL8ONyL6Z9SbNwuIp3ev Cns5FLzauDoFnTse0Ll1Vdvoew12jKOApd+muYsC1eZaUdU0RHG+2uIPtha3zcy6zZA5 pUfLBJJzbpI3Kmt/CPaFC4Dljbx/kdKxnEiCz4KKB3DvqJGSnnbLB+GZoTo2GAdc6DbI Wbfq/Hoji9ygxWeecViokiUou4NEZnmA8/ad234YcXisDphrZu1PuhKUBSNyFNmIe2f/ fMjrfrG0iVvFpeGvEgSBQdMeh1bmXKqky6ujdS+f2DEOmfg6VkMypVLtn03gl8BP7BhV qdWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787897376; x=1788502176; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=CCghARvrlSSDwf0Pbr/0UIqwsTlbjghwX717x3hCQWo=; b=MBIu692vJw7+gPZlXF/jaX++I9ztqffpOp6Wp8k6GaSW9GUiYN4k+J2TyYDa/5Z2Fe 6hFaAZo1TDQdJ3R4s/qEJ+U7wUEbkk3bqUDgL9qps3H29lzZvItfBt3XOSSL/zCLoX9S TKPk3DomeQQX99jkn+NBne8GJAuLr/ynx9eiW0pPrMdlbD8kkXyfkBLAAOfuKAc/o+IU AZKvmJQBFPolpOj8RVI+BrQqQLT79hUsAPD75XHBW3CP5vvrbudRA52494MyP8KaPg05 Je1jhYRyFB0O388yFxLVhinW2PKR1LBg46oa/9+TgSUfvYEF7PdH0WoQJYDUroo+qhCa 9U3g== X-Gm-Message-State: AFuF++kpZbcd18miST699bf9Eu64E+KJNZRog+q/x8IT91hqgJeXi5pA z4hLnE7r0kwciev6gUar18N8P4mJvE/AEmZTzYGxnpHC/9zfcYeoFS2iHVBGvHWVkTVi64Sns+Y fU91ACv3VPjmR60dQjj6v+HA7UjakhLW1fagOytZtl+BP36J4Ds/cnXAr9jtWk30QUmudWDiPJE sjH9CjNf447M2sb0jgA7wzP473/dpH21VF7XnF8I0zF0DKPzfHdA== X-Gm-Gg: AR+sD101sVhhQH5GLBimImVvmnkBbWhPFt/9ieBORBHdnKyLWfTSbMvvUEITEzPsEfr HQ/ui9XG3eGcdtigLhFFMuZbxxSY/D26gyIaxI5omYwEkzJ+8eUH6LonJSAyuZsUX3qIh/ks71O GeHXb4luJC3jNU3qVVzPQsJUNFvJWYNdPPRIhiVMMLfrTiCx3pPEdQYeRPWQgG2o/E07WEyaAGf Kij3w9FtBrRXxJqADYHoblzLyX8QHhmIxrzqpKvHjgk3rcDxPHt2ny3X4N5cLi/td5nz35GJEn9 jwwpI60Zva6JjBEJHJfsDVAIR2s4bNyyxDoQkhaMhhriATi+GhOLdGf7DSfYua89RC8ut8qFfC+ s4Td3tB0lf6En6Jz8JzIgpCVPfyFxj8rzm+9gUukULpdLBWyDSDzLVOrnx6UHTZRphIdH1dK/7s P+c6E7E4GAspBMBr4qwNmfdLv26vHlhwCq8Nlp6ETrKmvoF7ex925aUU9Wr2jTGlMIZpyLsiw= X-Received: by 2002:a17:90b:574b:b0:385:393e:7124 with SMTP id 98e67ed59e1d1-396d10178d6mr9961164a91.14.1787897375869; Thu, 27 Aug 2026 23:09:35 -0700 (PDT) Received: from sw08.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3286f95a847sm1995864eec.17.2026.08.27.23.09.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2026 23:09:35 -0700 (PDT) From: Nia Su Date: Thu, 27 Aug 2026 23:09:31 -0700 Subject: [PATCH 1/2] lib: sbi: Fix Smrnmi init and non-retentive suspend handling MIME-Version: 1.0 Message-Id: <20260827-rnmi-trap-fixes-v1-1-df371cf92301@sifive.com> References: <20260827-rnmi-trap-fixes-v1-0-df371cf92301@sifive.com> In-Reply-To: <20260827-rnmi-trap-fixes-v1-0-df371cf92301@sifive.com> To: opensbi@lists.infradead.org Cc: Nia Su , Nick Hu , Nylon Chen X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4677; i=nia.su@sifive.com; h=from:subject:message-id; bh=P/dTnUnp+ggK1jQnZHXXUzEUuyNsdf8ejC5+tXggD4Q=; b=owEBbQKS/ZANAwAIAYihNJa3+YfgAcsmYgBqkSYen1gK/mnQfvxfPFr5Ayhj7P0eKpv6YQPqw juHlwrrfyGJAjMEAAEIAB0WIQThGl/T4FqBBU9NhIeIoTSWt/mH4AUCapEmHgAKCRCIoTSWt/mH 4Ck2D/9zY8FVQQ5YNWLbJBrlh/KRfZp8ok21V+2pUrRlAd3PNrFehmxdOddQUgJ+b5OcsUz63KL j1OtQqZKm0CNTLnRncg8L2Kglhs8f3ZVt+j+lPjG8J5Z2fTPS9aRkBu68Z49mYqPObNjDSSotF2 GG8cCtohnzkzVl4Q9zH4nVm8sO8pGZ7XVIWfCJOJ5u1osX3E1hjkR3rPIfFkPWqpxKle/HIhOWB oJf/Zdvhvg4fC0bVSxjwGIE2wBPvr0b0dDI9cdGoLJvwAukrheFjskc32Z5pJuruy6QLKpo0J2W H9BDxIzsNJ8n2L/9hIskXnxC4M3pmw27tY5fd63UXWkVVwFYeDqtLZNBMOm9wii/8OtEXkzS01R 4jBGnCWGASWZnS/NGzNWFFJQyXIxO13gSBHrF2Ojd3dwNsEZrcsEzSaJ9O9rdsbMBubZuIZfyfk aHpkqxaIOZTc3y0XxPhdx96GfuAlFo8sddcM1o2kd5cvL9S3MuNn66Q75dGpcTg2xYD649KQOoC 4vcXipQ85tD5FG9BYm7556sSApw6fDf+qcnIwV8b7GJdISXFSB05TP3BTmVY+lN+nZrMLho9feY Mtp8fhc8MCRJ7/SIaB6oXmcMM2xAZAoVjHxBgJVsM9/+BA7KBqE/DbcU8p1xn8b60BabxQJXsi2 mRdKCngpLEPwSOw== X-Developer-Key: i=nia.su@sifive.com; a=openpgp; fpr=55235C875092ACC578EFCE6CD1699E646B29B1AF X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260827_230937_074433_F14EA89A X-CRM114-Status: GOOD ( 15.71 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Nylon Chen Two fixes for the Smrnmi extension infrastructure: 1. Extract Smrnmi CSR setup into sbi_smrnmi_hart_init() and call it from both hart_detect_features() (early, before trap-based probing) and sbi_hart_reinit() (covers non-retentive suspend resume via i [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:1030 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.0 DMARC_PASS DMARC pass policy X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Nylon Chen Two fixes for the Smrnmi extension infrastructure: 1. Extract Smrnmi CSR setup into sbi_smrnmi_hart_init() and call it from both hart_detect_features() (early, before trap-based probing) and sbi_hart_reinit() (covers non-retentive suspend resume via init_warm_resume()). This removes the need for save/restore of CSR_MNSCRATCH and MNSTATUS in sbi_hsm_data. 2. Change smrnmi_handlers_init callback return type from void to int. Allow NULL callback for platforms that do not need to program a vendor-specific NMI vector register. Only set MNSTATUS.NMIE after the callback succeeds. Fixes: 2d211fe6f9d5 ("lib: sbi: hart: Detect and enable Smrnmi before trap-based feature detection") Signed-off-by: Nylon Chen Co-developed-by: Nia Su Signed-off-by: Nia Su --- include/sbi/sbi_hart.h | 1 + include/sbi/sbi_platform.h | 2 +- lib/sbi/sbi_hart.c | 55 +++++++++++++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/include/sbi/sbi_hart.h b/include/sbi/sbi_hart.h index 543393bba41526017cd95596348e26eaa897850a..03f12717572f402d77e4e78f8d29c93a5fa0343d 100644 --- a/include/sbi/sbi_hart.h +++ b/include/sbi/sbi_hart.h @@ -132,6 +132,7 @@ extern unsigned long hart_features_offset; struct sbi_scratch; +int sbi_smrnmi_hart_init(struct sbi_scratch *scratch); int sbi_hart_reinit(struct sbi_scratch *scratch); int sbi_hart_init(struct sbi_scratch *scratch, bool cold_boot); diff --git a/include/sbi/sbi_platform.h b/include/sbi/sbi_platform.h index 1e9a23c1252558879c683633ad000828fbfa8f3f..d55805defafb0a7935d0d95653aefcc622078f69 100644 --- a/include/sbi/sbi_platform.h +++ b/include/sbi/sbi_platform.h @@ -155,7 +155,7 @@ struct sbi_platform_operations { void (*pmp_disable)(unsigned int n); /** platform specific Smrnmi handlers init on current HART */ - void (*smrnmi_handlers_init)(void (*rnmi_handler)(void), + int (*smrnmi_handlers_init)(void (*rnmi_handler)(void), void (*rnme_handler)(void)); /** platform specific Smrnmi NMI handler. diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c index bee8855772d8bfe452a1f9f086ece2a7ab8bbe6b..287d7ba7c4e110d77b3cb68929a6309e15ccd7fe 100644 --- a/lib/sbi/sbi_hart.c +++ b/lib/sbi/sbi_hart.c @@ -466,6 +466,37 @@ static int hart_mhpm_get_allowed_bits(void) return num_bits; } +int sbi_smrnmi_hart_init(struct sbi_scratch *scratch) +{ + extern void _trap_rnmi_handler(void); + extern void _trap_handler(void); + const struct sbi_platform *plat; + const struct sbi_platform_operations *ops; + int ret; + + if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMRNMI)) + return 0; + + plat = sbi_platform_thishart_ptr(); + ops = plat ? sbi_platform_ops(plat) : NULL; + + /* + * Platforms with fixed or mtvec-based NMI vectors need no + * vendor register programming; NULL callback is valid. + */ + if (ops && ops->smrnmi_handlers_init) { + ret = ops->smrnmi_handlers_init(_trap_rnmi_handler, + _trap_handler); + if (ret) + return ret; + } + + csr_write(CSR_MNSCRATCH, scratch); + csr_set(CSR_MNSTATUS, MNSTATUS_NMIE); + + return 0; +} + static int hart_detect_features(struct sbi_scratch *scratch, bool cold_boot) { struct sbi_trap_info trap = {0}; @@ -487,25 +518,9 @@ static int hart_detect_features(struct sbi_scratch *scratch, bool cold_boot) if (rc) return rc; - if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMRNMI)) { - const struct sbi_platform *plat = sbi_platform_thishart_ptr(); - const struct sbi_platform_operations *ops = sbi_platform_ops(plat); - extern void _trap_rnmi_handler(void); - extern void _trap_handler(void); - - if (!ops || !ops->smrnmi_handlers_init) - sbi_panic("Smrnmi detected, but platform lacks smrnmi_handlers_init callback\n"); - - /* Reuse _trap_handler for the RNME slot since RNME is taken - * as a regular M-mode trap with NMIE=0. */ - ops->smrnmi_handlers_init(_trap_rnmi_handler, _trap_handler); - - /* Initialize MNSCRATCH for the RNMI handler */ - csr_write(CSR_MNSCRATCH, scratch); - - /* Enable NMIs */ - csr_set(CSR_MNSTATUS, MNSTATUS_NMIE); - } + rc = sbi_smrnmi_hart_init(scratch); + if (rc) + return rc; #define __check_hpm_csr(__csr, __mask) \ oldval = csr_read_allowed(__csr, &trap); \ @@ -698,7 +713,7 @@ int sbi_hart_reinit(struct sbi_scratch *scratch) if (rc) return rc; - return 0; + return sbi_smrnmi_hart_init(scratch); } int sbi_hart_init(struct sbi_scratch *scratch, bool cold_boot) From patchwork Fri Aug 28 06:09:32 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nia Su X-Patchwork-Id: 2293902 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=h6e4+Kqo; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=SXzlpZ6i; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4hWSf5265Hz1xyF for ; Fri, 28 Aug 2026 16:09:49 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=22tUmR+/szcJcqc+24fFxDTtKk3nU/wqFZdh6poEgU8=; b=h6e4+KqoULj5Wa KNVRKdn55vbYk+JlcTo8TQnW7ABY83eQWhdxytTeZiM15VIy/AjcA4zUgmsVz64mno3lE+J/tiMSk FXaotriyDK1FBQJUTkeD8TT2fdH4lfFtqeViVL5uQGaS4EzjUt/mZXk4voPoy8Mm96D+niLsnPtw+ TJKJjTVQb+hFSu9ydV+/oIATR9F3c6eDRC7paf1CcfjlWhBgK70HUQlse9MSBp9fNcgAw1/veQIXn 3mE7wt6PxUCI/zXb/sh2m0xNHCcSpqsfTyJ3Dc7ZAs+Dzb7GpAfzRLGFffEhz0Vd5M5t2Jwt92Zvv eaRJSnehAdkFJtYT0zkA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wzpmW-00000005BQG-1Inh; Fri, 28 Aug 2026 06:09:40 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wzpmT-00000005BPM-3Hqe for opensbi@lists.infradead.org; Fri, 28 Aug 2026 06:09:39 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-2d7200b2e15so7437105ad.3 for ; Thu, 27 Aug 2026 23:09:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1787897377; x=1788502177; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=XLOAIA/ALK2N24S3DbM0+X80X6fyZweeVKn8xjFLrRs=; b=SXzlpZ6iqhQZCMRK9IfyG5zpiFdi7wD4p/DlzE2mDpB08bTiOLMH/26jolUbHUE9Jl wbEv0kyAvv+1CFoFSpj+RvkSWU/3oXQOXiNCvH4Gfqvf7IyyG2H41Gz2s2RYnqUDdbPB 2/ZELDnj7W6d7HD3CupnSPpeN8tSLbTGzz1h+pFjbtUTnIXBNsfnlcfCzwMbSO9v1Lt8 nN/cu3JF/os4B1zO3tfq2zZJmgWhLeSsBY0SJs0PT5NIgjr1ZzKk77uh5tcA3tdK9xey aXPwYwM1OklcfrpP142R5k1lD5m3CUL0eDo1v3dMlZo79dzZm8dSsrZ+Sxdsnyxp0eYA f3HA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787897377; x=1788502177; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=XLOAIA/ALK2N24S3DbM0+X80X6fyZweeVKn8xjFLrRs=; b=gd5EBYWF+3lPan4z6Rb/EM3A9R6PD94x1N0sRoC7+Q1Oa7vX/u1EG9WGpFMIgXoZ5H D0cqPv/J/s9D5mLaU0RbH6o0auWaSCRjKUF1shAdgPBK/o406weHiG9PglEzEtEVdMTu 9qXOBsYYqg725PzAfucpggGdKH33OzOBbJROc4hOEZvRz+PkZSjkJ7LRQ7ylxAQS4EQT rtOftlmYPDyYLB7J8zGfn0bu8KaXDsDz5qe1VwaukRrv18v23vz3uMu51HpZ32ZR77px BU6TlYaRzK2YscdWq+C622PmounHcmrlVS4/uts94Qu8UnmQzRbFBRb0sX+9eSqLicuE rsOQ== X-Gm-Message-State: AFuF++nLQuCqkSqDY0aQQX52Z4ulUiGNEe+Dx13WJ1DN22+dVt4pIMj9 lNcee5mW9uSjaObXs1MAx6Y0EMWJJ5soDoARCVLO/vPPX26zQTkJUeGjr0P1E5h0pmqkVaFqO/j bizryTYpN1f6FhShGQvxuWeBrdageO6GO7RxhogVtX5W+8XWCHtqPI0lT68vZEw/HrqNCxbTWAe dpTQZjISNBhJ7nr2g3vQbk1yP3q9B1z4e+TNFnqhM2wFa1o04= X-Gm-Gg: AR+sD11b4MjcZI1GOem16lZkCp4UL66PUzdB7L/PufxGDVvxlaPSLPgxKVR/lF50OJT S9KLZD39+RTd4j/jjw3lNYghpZWPp/zCtYjH0I99NiHlWrm6QjV6OBxcKWjdXEwGqMG7bia06rF SdtMmRlpJTuyRYkYO+bDI92DImhFD9Kfg1YoUJrQqEgRByh+RM881+xRWRY90Mhcwde+k1Babb7 Jd+QkyVRGJMtp8z1c1ssfAhOZ41Ft5+V3Fk3mSnKM1QeU4iz+uTkp2oM5LmvrtBb42GqobXEyMW S4h59VEimeUAIhW+cAWTkhFpStDLlnlYdTgqFV3S5KmWqj3WHA+BHJwDYTyyG0UMduHYz38CVCh gmvH753BDnONKqbV1PTJ6Hf6YP0iDvT+Gwy0tavqYSpUwW7Io/TFEX6QzMtIxuux1jJZRjOhAWQ SnRp88C/zp13mCsGJxvs8z3XeqCt13UFsz4rOsZ/kRBWAVlAnkGZADW2Oy0fGbnc8v/OcJ9bM= X-Received: by 2002:a17:903:13c7:b0:2cc:f5aa:9513 with SMTP id d9443c01a7336-2d74dee93acmr5925635ad.10.1787897376533; Thu, 27 Aug 2026 23:09:36 -0700 (PDT) Received: from sw08.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3286f95a847sm1995864eec.17.2026.08.27.23.09.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2026 23:09:36 -0700 (PDT) From: Nia Su Date: Thu, 27 Aug 2026 23:09:32 -0700 Subject: [PATCH 2/2] lib: sbi: Fix stale prev_context in RNMI handler MIME-Version: 1.0 Message-Id: <20260827-rnmi-trap-fixes-v1-2-df371cf92301@sifive.com> References: <20260827-rnmi-trap-fixes-v1-0-df371cf92301@sifive.com> In-Reply-To: <20260827-rnmi-trap-fixes-v1-0-df371cf92301@sifive.com> To: opensbi@lists.infradead.org Cc: Nia Su , Nick Hu X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3010; i=nia.su@sifive.com; h=from:subject:message-id; bh=Mo/43u74GjoCcJRmdRRONaIUpogHYxGcN+YTKsNu6bU=; b=owEBbQKS/ZANAwAIAYihNJa3+YfgAcsmYgBqkSYeEWu3syfQs3TnWzyPTtt6PTjKoab0rBkAH hp0nZfJcq2JAjMEAAEIAB0WIQThGl/T4FqBBU9NhIeIoTSWt/mH4AUCapEmHgAKCRCIoTSWt/mH 4CQID/92UZSCLndIIdA+W7GwFbzNroq0vwhclyywIClatPtE4oqeOoxixv2ZeaSEM9SKwWf0VlG Ip6vtXzFLmP+M3jiQ2erOUcajZMQiX3gGUQad5+XotSQDSLA9RtBOw8kTIdcuzHV+6RtbyVIGt+ LdPBqk1hftDETm1Bq/nY30xznonTP8UKS8MG1v2J9B2pcfQeXQDTx6GESKR1ePpHMAO1798bio8 K1dnF81zjA3A09hr1FSXONMZKAZKXIUlWxGUngpWyfP1CPusOf9RndqI+3R1zrl5RWVpJ3tv8mX cCudSBXm1eU4Bx8Ih1I+w8jJZjrra/5+UvwEPzKnDNquSgCLLCAsxX6WoIpK8eNW2yjDmNt2Hql PWNk09xLICKhYvymiKpqQ3pCEGa5g85qsFSWoPf5qPJys0QW6E2fSWg9yj51X85c8if9c3Iu63O Avynth7/d4ZY1evjKTKY3nwtkgLEqAxeC0LC7dZ+2r0jGmoo7A+XromNVlh2BAr9ewAmWbJvdVD 1wikG/qQ4Jod9rZGPPZc4dUdNHvFk802rwRLNOGXSibwGrGj9vnYppyHsqhZPtPNB6r7V/2ammJ ittszosHEzy4rllEQLACuUYbJj8Lskcynh+IC9XWae1FLUpAYvgWTaKZabFlI8nabROL5A1A4QU TF3ut78As9WRqHg== X-Developer-Key: i=nia.su@sifive.com; a=openpgp; fpr=55235C875092ACC578EFCE6CD1699E646B29B1AF X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260827_230937_828859_1661F4F2 X-CRM114-Status: GOOD ( 14.46 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Before an RNMI is taken, an earlier SBI trap may have already used the same slot on the M-mode exception stack to save its trap context. When the RNMI later comes in, it reuses that same stack slot fo [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:62a listed in] [list.dnswl.org] -0.0 DMARC_PASS DMARC pass policy X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Before an RNMI is taken, an earlier SBI trap may have already used the same slot on the M-mode exception stack to save its trap context. When the RNMI later comes in, it reuses that same stack slot for its own trap context, but sbi_trap_rnmi_handler() never links prev_context to the previously active trap context, so it still holds the value left there by that earlier trap context. sbi_trap_error() then walks this chain with `for (tc = tcntx; tc; tc = tc->prev_context) depth++;` to count the depth before printing anything. If this stale prev_context happens to point back to tcntx itself, the loop never terminates, hanging the hart before any diagnostics are even printed. Update sbi_trap_rnmi_handler() so that sbi_scratch points to the new trap context on entry, and points back to the previous context on exit, the same way sbi_trap_handler() does. This serves two purposes: it makes tcntx->prev_context always point to the correct previous trap context (or NULL when there isn't one) instead of stale data, fixing the hang described above; and it lets an exception taken while already inside RNMI handling correctly chain back to the RNMI's trap context, instead of the RNMI context being silently dropped from the chain and never printed. Together, these let sbi_trap_error() print every nested trap context in the chain, including the RNMI's own state. Fixes: 8cdb5b1023df ("firmware: Add RNMI handler infrastructure") Suggested-by: Nick Hu Signed-off-by: Nia Su --- lib/sbi/sbi_trap.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/sbi/sbi_trap.c b/lib/sbi/sbi_trap.c index 16774fd2f73059c1268bfb9f43fdf8e9c460c0b6..e694188c25b7b1715df660206af0a501290389fc 100644 --- a/lib/sbi/sbi_trap.c +++ b/lib/sbi/sbi_trap.c @@ -394,9 +394,14 @@ trap_done: struct sbi_trap_context *sbi_trap_rnmi_handler(struct sbi_trap_context *tcntx) { int rc; + struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); const struct sbi_platform *plat = sbi_platform_thishart_ptr(); const struct sbi_platform_operations *ops = sbi_platform_ops(plat); + /* Update trap context pointer so nested traps chain correctly */ + tcntx->prev_context = sbi_trap_get_context(scratch); + sbi_trap_set_context(scratch, tcntx); + /* Call platform-specific NMI handler if registered */ if (ops && ops->rnmi_handler) { rc = ops->rnmi_handler(tcntx); @@ -404,14 +409,15 @@ struct sbi_trap_context *sbi_trap_rnmi_handler(struct sbi_trap_context *tcntx) /* Platform handler failed to handle NMI */ sbi_trap_error("platform NMI handler failed", rc, tcntx); } - return tcntx; + goto done; } /* No platform handler - treat as unhandled NMI */ sbi_trap_error("unhandled NMI (no platform rnmi_handler)", SBI_ENOTSUPP, tcntx); - /* Never returns */ +done: + sbi_trap_set_context(scratch, tcntx->prev_context); return tcntx; }