From patchwork Fri Mar 19 09:45:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1455744 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=90.155.92.199; helo=desiato.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=bRzfmrgA; dkim=fail reason="signature verification failed" (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.a=rsa-sha256 header.s=badeba3b8450 header.b=ilrUUfqz; dkim-atps=neutral Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F1zYM6bMvz9sSC for ; Fri, 19 Mar 2021 20:46:15 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=peMi/6BqySDGUhnB14s5F3MUmMKz0KXBymkwvfVC5UY=; b=bRzfmrgA71TjZMjY1cpYL1LcIy gs7JiQ0OMgd0nIl4IoC4dyCdK3PlHCiTAIuZLtJbxJPCQZ80v/ujrJ3qcs7h1ni+moX8OjG6eo/A1 ff4m/SRKvIBgc39DHTnKduoIbX+k29/Vb4ycqw5Zbxa/smRONQ1XEcJZHMpP9Z5Wg1mw3KJqlRAFo a9lsc4DyXeDPF2LdnBmklpHtDo4YXthx4gWKoAe2V6EbYClMXWD1qKb/mL4IHAxHGJaBauMq0cOWB EUX7pv6eHLPcsqwYkJTt91SqDLdkZ26kFbLfbQB/ttFfob3rQgpFg3ztMZq7PGARE6cbGusRutnpx 21HIit1g==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lNBhq-006yIB-7p; Fri, 19 Mar 2021 09:46:10 +0000 Received: from mout.gmx.net ([212.227.15.19]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lNBhl-006yHO-Vs for opensbi@lists.infradead.org; Fri, 19 Mar 2021 09:46:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1616147155; bh=GC1NMP/DUrXrgbxqMB7rrb/GT9+RPHcdJnjO3d2vF5A=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=ilrUUfqzKhcJ2dJ+aXcuYjKAVdt5nWVkxL94bjqSfROvg3mhiW86MOZfmrlSFK4N3 CGuW3xNGRK08jqEn3UcfnDsU/pSoFMNR9/p1YaRxaFnhUdkBjLHUICMu2ZTth/Xy5D 7Y1syAMFLFAFLBg9mD+PZKN5mQI+i3qyIczGQe24= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from workstation4.fritz.box ([62.143.246.89]) by mail.gmx.net (mrgmx005 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MysVs-1laz9l2A78-00w0HQ; Fri, 19 Mar 2021 10:45:55 +0100 From: Heinrich Schuchardt To: Atish Patra Cc: Anup Patel , Xiang W , OpenSBI , Heinrich Schuchardt Subject: [PATCH v2 1/1] lib: sbi: illegal CSR 0x306 access in hpm_allowed() Date: Fri, 19 Mar 2021 10:45:46 +0100 Message-Id: <20210319094546.13896-1-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Provags-ID: V03:K1:p2GYERERi4j4E6WggNEkS838IrsttY8ZRSrevOu5z3B/l3eVcgj hd7GB+kA9jo5kqM6KC6Gn8pOoAspCR+s3v3iZ+UZwTh9pu+CfkYfshGsMxkM0EXDmKuyduw v01RxRc8VFYI2EsEK7GnfqnGx1LaCamPb+0K0UhoJ60ociZJ7yhqJ8RnE/pO+4REW7RlTwc LcwdHEfCyUdbMXYH5oqmQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:R2HVtER2Flk=:aCJVzgZsvUiHIPF6lCAWPl F4K+G2B4uPSIAxUARSZlWEKBJ9DYupFi0QG1KvFpB4hMT1wM7KI+GJS/f5FCEcZSqaOWBlHO4 Kp5058kLWcX7gQo9WfW8n6wKCCBKk7JRCo8oDxHG+MLuVK5uB3Nh3gUflrdrOfuKfT6yK8+px hDm/UOiWhYL2oULWqh2fZtS7rAWe8bsWk6kbK1YXIXtBehAfu8hkcrG+alsLq10aJVeN1ERju 2BtM1SOulRpPx6mohe2wo8UVdNy6qvMzHmdQZR1oOuHuVAw4cC23G9cIM356ONQsA00Mb0aPI 5GgT+Ww4ot0XnHlM9A9a93lYlSzwUZp/LFehpmoy3uSrxTyvIQzAgHmQ5U4qdE18/QfB2qsBh g7zI5LJtvJKlcEEGhQEad8j4b0uggUH5wQ+M31fWdBio+qwOl0ngwcooKwA+ikts4WAmMJvaX tpGFF1En6G+CmlPyL/qX9fVaBJQYgBIFecLiOA7Rmv3VzfYHZMaPy3tKw6CHUd1oGFfcVR1Zh ssN5p++fI7P10sd4Fr+lMX5n8TFH6NlF3z9e+soH93GIB/mJ1TCtEVANTNYQvgXE43KaehpWZ iS0PlmJ56bUcpUIFcBN6jIu8+hcbRhd6han+GxjZZxhinMSBTrl3cWIjM1X5TDNf8KAPyb3XC SEp81SHNVa0HEQ0fN6SSPrTHr3HNw+kCfczQwPnxKvo4+t5j8rGzY9N+6wrdyDNUFwbXsI8pT 6pmApMlG7XEuu9Hdm9+/dPdmK+oz4Q+VW7QkUeazEmElM+PTmNcwi8od8Gg2a3HadF7tRZM5u uznCyPlMQ7Iy9bzGbjjELFeCRPs7S4W789Qi4gyGDWSLhl31o1vBu2BCvMy4S3Hw/26Tl+6xY cIy6mlP3mZeYgdP5Y9Pg== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210319_094606_203687_A9D187A9 X-CRM114-Status: GOOD ( 10.40 ) X-Spam-Score: -0.7 (/) X-Spam-Report: Spam detection software, running on the system "desiato.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: The trap handler sbi_emulate_csr_read() invokes hpm_allowed() which reads CSR 0x306 (mcounteren). The K210 does not support CSR 0x306. While trying to handle a trap occurring in S-mode code this creat [...] Content analysis details: (-0.7 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [212.227.15.19 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [212.227.15.19 listed in wl.mailspike.net] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [xypron.glpk[at]gmx.de] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid 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 The trap handler sbi_emulate_csr_read() invokes hpm_allowed() which reads CSR 0x306 (mcounteren). The K210 does not support CSR 0x306. While trying to handle a trap occurring in S-mode code this creates an additional trap in M-mode. This results in failure to redirect to S-mode and the system hanging in sbi_hart_hang(). In hart_detect_features() we have already determined if CSR 0x306 is available and stored that information in the scratch area. We can use this information to decide if CSR 0x306 shall be accessed in hpm_allowed() and thus avoid the M-mode trap. Likewise if CSR scounteren is not available we have to avoid reading CSR 0x106. Signed-off-by: Heinrich Schuchardt --- v2: consider scounteren (Xiang W) --- lib/sbi/sbi_emulate_csr.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) -- 2.30.2 diff --git a/lib/sbi/sbi_emulate_csr.c b/lib/sbi/sbi_emulate_csr.c index bee7761..c66b5d3 100644 --- a/lib/sbi/sbi_emulate_csr.c +++ b/lib/sbi/sbi_emulate_csr.c @@ -21,14 +21,23 @@ static bool hpm_allowed(int hpm_num, ulong prev_mode, bool virt) { ulong cen = -1UL; + struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); if (prev_mode <= PRV_S) { - cen &= csr_read(CSR_MCOUNTEREN); - if (virt) - cen &= csr_read(CSR_HCOUNTEREN); + if (sbi_hart_has_feature(scratch, SBI_HART_HAS_MCOUNTEREN)) { + cen &= csr_read(CSR_MCOUNTEREN); + if (virt) + cen &= csr_read(CSR_HCOUNTEREN); + } else { + cen = 0; + } + } + if (prev_mode == PRV_U) { + if (!sbi_hart_has_feature(scratch, SBI_HART_HAS_SCOUNTEREN)) + cen &= csr_read(CSR_SCOUNTEREN); + else + cen = 0; } - if (prev_mode == PRV_U) - cen &= csr_read(CSR_SCOUNTEREN); return ((cen >> hpm_num) & 1) ? TRUE : FALSE; }