From patchwork Fri Oct 18 08:40:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 1998992 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=1cgdZhkq; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=FJlPjQ6t; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) 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 4XVJNt5pLRz1xth for ; Fri, 18 Oct 2024 19:52:13 +1100 (AEDT) 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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PSBqRmSE+2ks/eT9ocC+1oQrUnv01INDQ/KMtDQGNjY=; b=1cgdZhkq+pjkNY p+4gVJrLkv1iv3MsswacP9Dom8EKQ0E4gBNEoPuLCmADN8mzdPXvwtnynlyyPFM/han4H0O+LriI/ reonYLnZWTIIZNkpKgV6026DJ1F809hZOpCq7a/tAaSCxlr79FqWQFg7ckt+Pl2WNobdBimumVpeo GWfsBj7ze4sL36Uv0GxKgw487evPDFEWxnYeRtOB2MrRpT7w5DfgF13GTRIIxqmpiCPqIOq3VfQ6z BhFT1ufd0KMAT1UEu1JYCXkPBe2KP2RvGuRIMVGAZFrA4BROaqa15pXVYFPWGc192RMGUEZiGNEQT GtVv1K9BLA5+7ZhlW5ew==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t1iiI-000000003Dg-1dV1; Fri, 18 Oct 2024 08:52:02 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t1iXD-0000000Hb7t-24TN for opensbi@lists.infradead.org; Fri, 18 Oct 2024 08:40:37 +0000 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-2e56df894d4so188386a91.3 for ; Fri, 18 Oct 2024 01:40:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1729240834; x=1729845634; darn=lists.infradead.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=3eFgisiOn+ah7w63zcDEzmNY1uMHu1fAZJ9jjRjKwm0=; b=FJlPjQ6t6Ehh+bK16f5L6NyZRYjj+ZK/yGRGKcxizRYnfG9nafnkvTYhdqx7wk6noC YCaALXsyvNLhNpQGD4pif3RgSTt+osgc9TIEJv+otLTwlzqg/Owq5iCTuxPNLOpFXlSR FKfQFn5XKtoTAkjtwp7wB2p2S3oLEbLu8JncT0ngeVxad1O9V7sSF1sSAkPDd+D/TtXQ 8DzJggV/7YixxqIvc5nwriV+6xTpvItFJb7dpWGG0K+QjICthUVUG2NqpJ/ZpeaqLEfi Y53xIP6npgXkiL1KiNw3uf3ZxCHDayUmzxtcBqlN2hdpQagbJlnVNU0gTkx8MMIJJm7f gtbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729240834; x=1729845634; 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=3eFgisiOn+ah7w63zcDEzmNY1uMHu1fAZJ9jjRjKwm0=; b=ouWnH8WuQDbTyyIBw0XRdpB4Cmn/5cDLgUJJ8QRzaA6DvTTvVE3RfevWESqzEwN6BO ++LbwcQHQvgp8gABs95EztQBDVjLeqm7ExXIvAchA83JEH6OypouYgYUgrm4IGqPauD6 dlqiAuNa7a+yoLZTLsNCl37myRMKz+Vjk+wzJDgQAw/SlBGa81qdnWtRrpwTSRTl5sTC RVOvw34cQUfTfLwWzj/F+99KuqXQALJZlfnUErLnyzGWGwPyorsTHFlhm8OxZKMnu81X 9Lor7MH76jcMqyVYvN3msvzmdDW9tQK26hrXvkS5SrHKoJ7cUxgxMGcSG8cuQBhacPAb +HRw== X-Gm-Message-State: AOJu0Ywej3IeB2s07cVev3KYBLRZX9oJ0dT1vla6CL7I7MeAjB0YJp0y OPbJnGA44TS7lMXNDJq4cYVbpNJb31nELw9tljVV1gBJKuGSG/nQmn3Cf8Ayiyl84RFwylsJpEh dKxE= X-Google-Smtp-Source: AGHT+IGEacJ6lXuRIYMUePKdAOQin76zv/FCjrgOwLuLcW2z5DQ4Tw0+hQBB/XCPZlY8ryAm/ZnPgQ== X-Received: by 2002:a17:90b:4b03:b0:2dd:6969:2096 with SMTP id 98e67ed59e1d1-2e561a230ddmr1781121a91.38.1729240833957; Fri, 18 Oct 2024 01:40:33 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e55d8051a9sm1364094a91.27.2024.10.18.01.40.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 01:40:33 -0700 (PDT) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: opensbi@lists.infradead.org Subject: [PATCH v4 1/7] lib: sbi: factorize previous virtualization mode read from regs Date: Fri, 18 Oct 2024 10:40:02 +0200 Message-ID: <20241018084010.406285-2-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018084010.406285-1-cleger@rivosinc.com> References: <20241018084010.406285-1-cleger@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241018_014035_613956_0C5B55BB X-CRM114-Status: UNSURE ( 9.94 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -1.9 (-) X-Spam-Report: =?unknown-8bit?q?Spam_detection_software=2C_running_on_the_sy?= =?unknown-8bit?q?stem_=22bombadil=2Einfradead=2Eorg=22=2C?= =?unknown-8bit?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_ori?= =?unknown-8bit?q?ginal?= =?unknown-8bit?q?_message_has_been_attached_to_this_so_you_can_view_it_or_la?= =?unknown-8bit?q?bel?= =?unknown-8bit?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?unknown-8bit?q?_the_administrator_of_that_system_for_details=2E?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_preview=3A__The_same_pattern_is_used_at_multiple_p?= =?unknown-8bit?q?laces_to_verify_in_which?= =?unknown-8bit?q?_mode_the_exception_was_actually_taken=2E_Factorize_it=2E_S?= =?unknown-8bit?q?igned-off-by=3A_Cl=C3=A9ment?= =?unknown-8bit?q?_L=C3=A9ger_=3Ccleger=40rivosinc=2Ecom=3E_Reviewed-by=3A_Sa?= =?unknown-8bit?q?muel_Holland_=3Csamuel=2Eholland=40sifive=2Ecom=3E?= =?unknown-8bit?q?_---_include/sbi/sbi=5Ftrap=2Eh_=7C_9_+++++++++_lib/sbi/sbi?= =?unknown-8bit?q?=5Femulate=5Fcsr=2Ec_=7C_12_++?= =?unknown-8bit?b?IGxpYi9zYiBbLi4uXSA=?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=28-1=2E9_points=2C_5=2E0_re?= =?unknown-8bit?q?quired=29?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_pts_rule_name______________description?= =?unknown-8bit?q?_----_----------------------_------------------------------?= =?unknown-8bit?q?--------------------?= =?unknown-8bit?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_a?= =?unknown-8bit?q?t_https=3A//www=2Ednswl=2Eorg/=2C_no?= =?unknown-8bit?q?_trust?= =?unknown-8bit?b?IFsyNjA3OmY4YjA6NDg2NDoyMDowOjA6MDoxMDJkIGxpc3RlZCBpbl0=?= =?unknown-8bit?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?unknown-8bit?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publ?= =?unknown-8bit?q?ish_an_SPF_Record?= =?unknown-8bit?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_?= =?unknown-8bit?q?record?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_v?= =?unknown-8bit?q?alid_DKIM_or_DK_signature?= =?unknown-8bit?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily_valid?= =?unknown-8bit?q?_-1=2E9_BAYES=5F00_______________BODY=3A_Bayes_spam_probabi?= =?unknown-8bit?q?lity_is_0_to_1=25?= =?unknown-8bit?q?_=5Bscore=3A_0=2E0000=5D?= 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: , Cc: Anup Patel , =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Atish Patra , Ved Shanbhogue Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The same pattern is used at multiple places to verify in which mode the exception was actually taken. Factorize it. Signed-off-by: Clément Léger Reviewed-by: Samuel Holland --- include/sbi/sbi_trap.h | 9 +++++++++ lib/sbi/sbi_emulate_csr.c | 12 ++---------- lib/sbi/sbi_trap.c | 6 +----- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/include/sbi/sbi_trap.h b/include/sbi/sbi_trap.h index f7c170e..b0c47ce 100644 --- a/include/sbi/sbi_trap.h +++ b/include/sbi/sbi_trap.h @@ -236,6 +236,15 @@ static inline unsigned long sbi_regs_gva(const struct sbi_trap_regs *regs) #endif } +static inline bool sbi_regs_from_virt(const struct sbi_trap_regs *regs) +{ +#if __riscv_xlen == 32 + return (regs->mstatusH & MSTATUSH_MPV) ? true : false; +#else + return (regs->mstatus & MSTATUS_MPV) ? true : false; +#endif +} + int sbi_trap_redirect(struct sbi_trap_regs *regs, const struct sbi_trap_info *trap); diff --git a/lib/sbi/sbi_emulate_csr.c b/lib/sbi/sbi_emulate_csr.c index 3adec78..a5a04cd 100644 --- a/lib/sbi/sbi_emulate_csr.c +++ b/lib/sbi/sbi_emulate_csr.c @@ -47,11 +47,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs, int ret = 0; struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT; -#if __riscv_xlen == 32 - bool virt = (regs->mstatusH & MSTATUSH_MPV) ? true : false; -#else - bool virt = (regs->mstatus & MSTATUS_MPV) ? true : false; -#endif + bool virt = sbi_regs_from_virt(regs); switch (csr_num) { case CSR_HTIMEDELTA: @@ -157,11 +153,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs, { int ret = 0; ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT; -#if __riscv_xlen == 32 - bool virt = (regs->mstatusH & MSTATUSH_MPV) ? true : false; -#else - bool virt = (regs->mstatus & MSTATUS_MPV) ? true : false; -#endif + bool virt = sbi_regs_from_virt(regs); switch (csr_num) { case CSR_HTIMEDELTA: diff --git a/lib/sbi/sbi_trap.c b/lib/sbi/sbi_trap.c index 176e174..ff0668e 100644 --- a/lib/sbi/sbi_trap.c +++ b/lib/sbi/sbi_trap.c @@ -104,11 +104,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs, { ulong hstatus, vsstatus, prev_mode; bool elp = false; -#if __riscv_xlen == 32 - bool prev_virt = (regs->mstatusH & MSTATUSH_MPV) ? true : false; -#else - bool prev_virt = (regs->mstatus & MSTATUS_MPV) ? true : false; -#endif + bool prev_virt = sbi_regs_from_virt(regs); /* By default, we redirect to HS-mode */ bool next_virt = false;