From patchwork Tue May 21 13:58:30 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: 1937469 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=jdslDGwy; 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=rpDqaw8n; 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 4VkHh35vwxz23tw for ; Wed, 22 May 2024 01:01:23 +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:MIME-Version:References:In-Reply-To: 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: List-Owner; bh=o2HoTf9WJffZAyJKiQFs0+oKxGgTkDyTcBAunoGOfm0=; b=jdslDGwy0ilis5 kp37MUWKD560oP+5kCt24gu9Rh+CCHv4ytlC2kqoHiR0YojVa2vVbfSGD4XJMO2X5LRaJSOwVG+MA m3eXknToolCMrb1NIdMOTwp0dBo/bESk6wZK38t/owaCf3HITUX2NyqLCpNFzg0lunWWjikIINJgw Bnilzt3rkQ9ZJ93hZParGI0fTSIr3QBHc8HITo9y0UWQ6G4ja/EBQILt1+p0v3MUBv4ZhTNDwxxho afkbEWvK7NucZCi+QcQX8IouVtC2Elx09ZLl7DXWansfhrXHuZlEm6+P7vSWBdLWuWxjm+njH/BCc kWI2zuEt67cuwZlINxWQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s9QzH-00000000GkJ-3QxU; Tue, 21 May 2024 15:01:11 +0000 Received: from mail-wr1-f46.google.com ([209.85.221.46]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s9Q0q-000000004nb-0rXu for opensbi@lists.infradead.org; Tue, 21 May 2024 13:58:45 +0000 Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-354c1dd7aaaso240327f8f.1 for ; Tue, 21 May 2024 06:58:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716299921; x=1716904721; 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=o6sKw9HY4IV/M8Nc7G8U82Ygztz0pzJG3fjNw/+JwUk=; b=rpDqaw8nyT6e7+t+YjgTuH4lRp7YzVHih6g53jzzzckB4/FuPVuUItB6XTTiXvlcsU oEZV9dV23+yixoPVpeiKryWNCaEicNqb6mw/MB9ZzPt8ItfVPZRUzzYV43T4p8SQIE3I oCnrPnN3oYFD9cVOUt3U4rE324WXxAiXFFA4Ua+pRCiWRMCSaBo9agNmdbuEZ00g9bXK LcRgc1sPJxUqQe6rmRZLyHWgnmpXbnRP8H5YXTyXUdjW0QcEx2Kel4Ba3FWsKqq+BiWm fj+U8dP0DJerRdHjAfQ1gjlzEs9iDTSyOe9iL0OCMi6GDnlMXZUQHr68eeAndRIJSk9w 4org== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716299921; x=1716904721; 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=o6sKw9HY4IV/M8Nc7G8U82Ygztz0pzJG3fjNw/+JwUk=; b=JMFsnZhPgIg6E+D0JgNN9ddRy27UofiFcn+WwoVTXvbYBBIJY8tpz+RVtUgpmiboW8 eUlM7TaBdjAOGHITWPBoe8xp2JqybAOoCyCLVV+Xt6LcGzQxA7V62tMV8la3I2Xfnm6W WL7pxB06aQu7U+4WI906jnXgNDWprX2kTTtOTbYuoHGvjbK1UByS+O4kJyBD3T75LXZu rrrWYf5zQrjZYKPD0XmkS/941yI5effdQg+sj+OjhqVkt0lI5SdQTWuOwyyQ+6fl4469 gx7i5dvKlRmdsd660aCVoke493OWL5yDnvvlgAQbE48p1JsjybQREu5XBgMD5Rsg4OqU eiaQ== X-Gm-Message-State: AOJu0YwrZHT3PRN/WVwB/Nk2IPVWYqaxHvVU2N8hEjzj2sqo+Qtmrubm N6HZgB7IPIoxfjp0ypOwp+wnbgbQaPgj5iM8I7Pbc9+QHembh/lUwV95hML2VrrZMOx4LSG9kxb ZC4w= X-Google-Smtp-Source: AGHT+IHVOFOIuh57hcuDKlI5LIMtLaBBJ+zUq+3l2bSMIxvD9yXHFQD2vZTyx9uXmwNg9wbHOAeEVw== X-Received: by 2002:adf:e803:0:b0:34d:2726:94e2 with SMTP id ffacd0b85a97d-3504a623f82mr19975883f8f.2.1716299921683; Tue, 21 May 2024 06:58:41 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:999:a3a0:a963:3072:e647:c890]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-41fccce73f5sm470275585e9.21.2024.05.21.06.58.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 06:58:41 -0700 (PDT) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: opensbi@lists.infradead.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Anup Patel , Atish Patra Subject: [PATCH v1 2/5] lib: sbi: add support for firmware features extension Date: Tue, 21 May 2024 15:58:30 +0200 Message-ID: <20240521135835.371485-3-cleger@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240521135835.371485-1-cleger@rivosinc.com> References: <20240521135835.371485-1-cleger@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240521_065844_284463_DD4B08AF X-CRM114-Status: GOOD ( 21.01 ) X-Spam-Score: -0.0 (/) 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__This_extension_allows_the_software_run?= =?unknown-8bit?q?ning_in_supervisor_mode?= =?unknown-8bit?q?_to_control_the_behavior_of_various_features_of_the_SBI_=5B?= =?unknown-8bit?q?1=5D=2E_Implement_the?= =?unknown-8bit?q?_support_for_such_extension=2E_Link=3A_https=3A//lists=2Eri?= =?unknown-8bit?q?scv=2Eorg/g/tech-prs/message/924?= =?unknown-8bit?q?_=5B1=5D_Signed-off-by=3A_Cl=C3=A9ment_L=C3=A9ger_=3Ccleger?= =?unknown-8bit?q?=40rivosinc=2Ecom=3E_---_include/sbi/sbi=5Fecall=5Finterfa?= =?unknown-8bit?q?ce=2Eh?= =?unknown-8bit?q?_=7C_28_+++++_include/sbi/sbi=5Ffwft=2Eh_=7C_23_++++_lib/sb?= =?unknown-8bit?q?i/_=5B=2E=2E=2E=5D_?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=28-0=2E0_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?q?_=5B209=2E85=2E221=2E46_listed_in_list=2Ednswl=2Eorg=5D?= =?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=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily_valid?= =?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=2E0_RCVD=5FIN=5FMSPIKE=5FH2______RBL=3A_Average_reputat?= =?unknown-8bit?q?ion_=28+2=29?= =?unknown-8bit?q?_=5B209=2E85=2E221=2E46_listed_in_wl=2Emailspike=2Enet=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: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This extension allows the software running in supervisor mode to control the behavior of various features of the SBI [1]. Implement the support for such extension. Link: https://lists.riscv.org/g/tech-prs/message/924 [1] Signed-off-by: Clément Léger --- include/sbi/sbi_ecall_interface.h | 28 +++++ include/sbi/sbi_fwft.h | 23 ++++ lib/sbi/objects.mk | 1 + lib/sbi/sbi_fwft.c | 179 ++++++++++++++++++++++++++++++ lib/sbi/sbi_init.c | 11 ++ 5 files changed, 242 insertions(+) create mode 100644 include/sbi/sbi_fwft.h create mode 100644 lib/sbi/sbi_fwft.c diff --git a/include/sbi/sbi_ecall_interface.h b/include/sbi/sbi_ecall_interface.h index 2600b66..e596119 100644 --- a/include/sbi/sbi_ecall_interface.h +++ b/include/sbi/sbi_ecall_interface.h @@ -34,6 +34,7 @@ #define SBI_EXT_CPPC 0x43505043 #define SBI_EXT_DBTR 0x44425452 #define SBI_EXT_SSE 0x535345 +#define SBI_EXT_FWFT 0x46574654 /* SBI function IDs for BASE extension*/ #define SBI_EXT_BASE_GET_SPEC_VERSION 0x0 @@ -117,6 +118,33 @@ #define SBI_EXT_DBTR_TRIGGER_ENABLE 0x6 #define SBI_EXT_DBTR_TRIGGER_DISABLE 0x7 +/* SBI function IDs for FW feature extension */ +#define SBI_EXT_FWFT_SET 0x0 +#define SBI_EXT_FWFT_GET 0x1 + +enum sbi_fwft_feature_t { + SBI_FWFT_MISALIGNED_EXC_DELEG = 0x0, + SBI_FWFT_SOFTWARE_CHECK_EXC_DELEG = 0x1, + SBI_FWFT_LANDING_PAD = 0x2, + SBI_FWFT_SHADOW_STACK = 0x3, + SBI_FWFT_DOUBLE_TRAP = 0x4, + SBI_FWFT_PTE_AD_HW_UPDATING = 0x5, + SBI_FWFT_LOCAL_RESERVED_START = 0x6, + SBI_FWFT_LOCAL_RESERVED_END = 0x3fffffff, + SBI_FWFT_LOCAL_PLATFORM_START = 0x40000000, + SBI_FWFT_LOCAL_PLATFORM_END = 0x7fffffff, + + SBI_FWFT_GLOBAL_RESERVED_START = 0x80000000, + SBI_FWFT_GLOBAL_RESERVED_END = 0xbfffffff, + SBI_FWFT_GLOBAL_PLATFORM_START = 0xc0000000, + SBI_FWFT_GLOBAL_PLATFORM_END = 0xffffffff, +}; + +#define SBI_FWFT_GLOBAL_FEATURE_BIT (1 << 31) +#define SBI_FWFT_PLATFORM_FEATURE_BIT (1 << 30) + +#define SBI_FWFT_SET_FLAG_LOCK (1 << 0) + /** General pmu event codes specified in SBI PMU extension */ enum sbi_pmu_hw_generic_events_t { SBI_PMU_HW_NO_EVENT = 0, diff --git a/include/sbi/sbi_fwft.h b/include/sbi/sbi_fwft.h new file mode 100644 index 0000000..2148820 --- /dev/null +++ b/include/sbi/sbi_fwft.h @@ -0,0 +1,23 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Rivos Inc. + * + * Authors: + * Clément Léger + */ + +#ifndef __SBI_FW_FEATURE_H__ +#define __SBI_FW_FEATURE_H__ + +#include + +struct sbi_scratch; + +int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value, + unsigned long flags); +int sbi_fwft_get(enum sbi_fwft_feature_t feature, unsigned long *out_val); + +int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot); + +#endif diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk index b1c4a50..221e72c 100644 --- a/lib/sbi/objects.mk +++ b/lib/sbi/objects.mk @@ -65,6 +65,7 @@ libsbi-objs-y += sbi_domain_context.o libsbi-objs-y += sbi_domain.o libsbi-objs-y += sbi_emulate_csr.o libsbi-objs-y += sbi_fifo.o +libsbi-objs-y += sbi_fwft.o libsbi-objs-y += sbi_hart.o libsbi-objs-y += sbi_heap.o libsbi-objs-y += sbi_math.o diff --git a/lib/sbi/sbi_fwft.c b/lib/sbi/sbi_fwft.c new file mode 100644 index 0000000..adfe645 --- /dev/null +++ b/lib/sbi/sbi_fwft.c @@ -0,0 +1,179 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Rivos Inc. + * + * Authors: + * Clément Léger + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +/** Offset of pointer to FWFT HART state in scratch space */ +static unsigned long fwft_ptr_offset; + +#define fwft_get_hart_state_ptr(__scratch) \ + sbi_scratch_read_type((__scratch), void *, fwft_ptr_offset) + +#define fwft_thishart_state_ptr() \ + fwft_get_hart_state_ptr(sbi_scratch_thishart_ptr()) + +#define fwft_set_hart_state_ptr(__scratch, __phs) \ + sbi_scratch_write_type((__scratch), void *, fwft_ptr_offset, (__phs)) + +struct fwft_config; + +struct fwft_feature { + enum sbi_fwft_feature_t id; + int (*supported)(struct fwft_config *conf); + int (*set)(struct fwft_config *conf, unsigned long value); + int (*get)(struct fwft_config *conf, unsigned long *value); +}; + +struct fwft_config { + const struct fwft_feature *feature; + unsigned long flags; +}; + +struct fwft_hart_state { + unsigned int config_count; + struct fwft_config configs[]; +}; + +static const unsigned long fwft_defined_features[] = { + SBI_FWFT_MISALIGNED_EXC_DELEG, + SBI_FWFT_SOFTWARE_CHECK_EXC_DELEG, + SBI_FWFT_LANDING_PAD, + SBI_FWFT_SHADOW_STACK, + SBI_FWFT_DOUBLE_TRAP, + SBI_FWFT_PTE_AD_HW_UPDATING, +}; + +static bool fwft_is_defined_feature(enum sbi_fwft_feature_t feature) +{ + int i; + + for (i = 0; i < array_size(fwft_defined_features); i++) { + if (fwft_defined_features[i] == feature) + return true; + } + + return false; +} + +static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature) +{ + int i; + struct fwft_hart_state *fhs = fwft_thishart_state_ptr(); + + if (feature & SBI_FWFT_GLOBAL_FEATURE_BIT) + return NULL; + + for (i = 0; i < fhs->config_count; i++){ + if (feature == fhs->configs[i].feature->id) + return &fhs->configs[i]; + } + + return NULL; +} + +static int fwft_get_feature(enum sbi_fwft_feature_t feature, + struct fwft_config **conf) +{ + int ret; + struct fwft_config *tconf; + + tconf = get_feature_config(feature); + if (!tconf) { + if (fwft_is_defined_feature(feature)) + return SBI_ENOTSUPP; + + return SBI_EDENIED; + } + + if (tconf->feature->supported) { + ret = tconf->feature->supported(tconf); + if (ret) + return ret; + } + *conf = tconf; + + return SBI_SUCCESS; +} + +int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value, + unsigned long flags) +{ + int ret; + struct fwft_config *conf; + + ret = fwft_get_feature(feature, &conf); + if (ret) + return ret; + + if ((flags & ~SBI_FWFT_SET_FLAG_LOCK) != 0) + return SBI_ERR_INVALID_PARAM; + + if (conf->flags & SBI_FWFT_SET_FLAG_LOCK) + return SBI_EDENIED; + + ret = conf->feature->set(conf, value); + if (ret) + return ret; + + conf->flags = flags; + + return SBI_OK; +} + +int sbi_fwft_get(enum sbi_fwft_feature_t feature, unsigned long *out_val) +{ + int ret; + struct fwft_config *conf; + + ret = fwft_get_feature(feature, &conf); + if (ret) + return ret; + + return conf->feature->get(conf, out_val); +} + +static const struct fwft_feature features[] = {}; + +int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot) +{ + int i; + struct fwft_hart_state *fhs; + + if (cold_boot) { + fwft_ptr_offset = sbi_scratch_alloc_type_offset(void *); + if (!fwft_ptr_offset) + return SBI_ENOMEM; + } + + fhs = fwft_get_hart_state_ptr(scratch); + if (!fhs) { + fhs = sbi_zalloc(sizeof(fhs) + array_size(features) * sizeof(struct fwft_config)); + if (!fhs) + return SBI_ENOMEM; + + fhs->config_count = array_size(features); + for (i = 0; i < array_size(features); i++) + fhs->configs[i].feature = &features[i]; + + fwft_set_hart_state_ptr(scratch, fhs); + } + + return 0; +} diff --git a/lib/sbi/sbi_init.c b/lib/sbi/sbi_init.c index 389172a..0f9e14c 100644 --- a/lib/sbi/sbi_init.c +++ b/lib/sbi/sbi_init.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -308,6 +309,12 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid) sbi_hart_hang(); } + rc = sbi_fwft_init(scratch, true); + if (rc) { + sbi_printf("%s: fwft init failed (error %d)\n", __func__, rc); + sbi_hart_hang(); + } + /* * Note: Finalize domains after HSM initialization so that we * can startup non-root domains. @@ -423,6 +430,10 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch, if (rc) sbi_hart_hang(); + rc = sbi_fwft_init(scratch, false); + if (rc) + sbi_hart_hang(); + rc = sbi_platform_final_init(plat, false); if (rc) sbi_hart_hang();