From patchwork Mon Sep 23 11:56:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1988571 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=kcWrYbld; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=Pf/3XiD8; 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 4XC1hV2hw6z1xsg for ; Mon, 23 Sep 2024 21:57:46 +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=LP5KZxr5a6L6K6K0PlmZyZKwIfS6d93Oa3NTSblz1n8=; b=kcWrYbldR/ArSk uqwmLxNRalSB2J9VPaAEj0nbdEve5ZjnvZsk7WAtnSZuSUuSnIX1xZMktQ+HBfBN3sL583X55r0Ul DScD0UsAWrmb12B3Vxnj8NE5uqFIcgbZub5KdR/iep5GZ0/JV+KGOTq7g4CHmGG7uzFPmoq1m+55j Qg+GihKTuK07FC9+17J7AT93PlhpHWP/D4Wu94+JiPbnnx35XVG9PUcJ91vsodxucDagVjgvKzDcU ZlOXtdjZab535C0mtEu4Fpj8q/032zkTPzAl/en89zcWH6l8ZfyAkkt2oFXWmDldJZM0sebT3+n7p ueP3oPtUgP7hBFQmmURA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sshhE-0000000H7SR-253v; Mon, 23 Sep 2024 11:57:40 +0000 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sshhB-0000000H7QD-1YiW for opensbi@lists.infradead.org; Mon, 23 Sep 2024 11:57:38 +0000 Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-7db53a20d1fso2587148a12.0 for ; Mon, 23 Sep 2024 04:57:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1727092656; x=1727697456; 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=DaX8/+gNJg8JJ9IqKSfP5fcJjj4yGYID6IkyiNiz03Q=; b=Pf/3XiD8qIioOFFGhdlq1X1QCvJgi981jMf2GgcxgcBUowX+5klyiLekN1KLu+ipdl o5zXCua4KufSs0zeqPIy8OKYDL0u3Sgxwgk7RVDNhdZ6PMc2+Xe+9CfNdYiRo01rD08n Ia38jL9Us0p3Ot7yE/EQ/4cb3KLpWOFDuq464jXYT3yg+L60Y0yd7oIo7izsqqW3Ht2z 52bFYGwe9JlKaaImwAiCTOCyXkAhw8o5GPVp81i7bjm2HLiHdHIipKULJjeDJeeuvs5x NMJ2Q0EfhWTAH1ZzoIlY9EDQbh9G9Ko7jOVstGbmDR18zidTKJK3n6c1SFgX+KbZpcfB ws0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727092656; x=1727697456; 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=DaX8/+gNJg8JJ9IqKSfP5fcJjj4yGYID6IkyiNiz03Q=; b=RNb9yVGDWtHe8A0qRjSYjjCKhwCoqMGVwtdfWjEHae3WmNJrQAiG9D2DIyt689BQ+H EFWbgoLdjBEg4kAtjxOJ3FJUcKA4Cn1JVFU6Mmtv7syFMf0nX5gm13UvF2aG8Q4PZXtl QjeZ/Vdbl+/TljFcxYGYrDNnP6j8M0XrD/p3WG/bgPLB8k5ffoja5buglsh0O8a12S2B sfrZiGR0/mciqi1bOIcGQT1yV2dsvYzAHIoY1EfCUmHsBsMQTnt9IVzNznga/cpg4vaD ygy/KPhQH3fUZgib677WDgXow9NyBw9+sG7iIav6Llmjz0APGPM97j1rbv0aUXEwW/Z+ 43Tg== X-Forwarded-Encrypted: i=1; AJvYcCWZ0kZWE0OdZuIRpuBJyqZYgqRcSUbj3Q3XerANde3JxzsORyfj2cgbrjOmycvIhqaIITO8Tptf@lists.infradead.org X-Gm-Message-State: AOJu0YxuS5TYLHY3xcTxqcCWPktN5SF9RMHGPV3NzemJyPVAGqmUSd85 K2/uffkCTZKnmuVAmGnPA9iwAlocdgOECiEJUPygy4s/BEBoe2cykWlPGdhm+Fw= X-Google-Smtp-Source: AGHT+IH11wevUC27IO/SIlm5vodX+WnMIkIRw4n2m5ajQU3m9bEiNzXGf+8ttWJ5wDsl4oKaltfB2Q== X-Received: by 2002:a17:90b:5248:b0:2cf:c972:7c22 with SMTP id 98e67ed59e1d1-2dd80c64627mr14369986a91.10.1727092656303; Mon, 23 Sep 2024 04:57:36 -0700 (PDT) Received: from anup-ubuntu-vm.localdomain ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2dd6ef361b0sm9252106a91.44.2024.09.23.04.57.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2024 04:57:35 -0700 (PDT) From: Anup Patel To: Atish Patra Cc: Andrew Jones , Anup Patel , opensbi@lists.infradead.org, Anup Patel Subject: [PATCH 8/9] lib: sbi_domain: Use domain data support for per-domain hart context Date: Mon, 23 Sep 2024 17:26:59 +0530 Message-ID: <20240923115700.381916-9-apatel@ventanamicro.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240923115700.381916-1-apatel@ventanamicro.com> References: <20240923115700.381916-1-apatel@ventanamicro.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240923_045737_529804_264D275F X-CRM114-Status: GOOD ( 21.03 ) 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: The per-domain hartindex_to_context_table[] is yet another per-domain data required for implementing hart entry into (or exit from) domain. Use the recently added domain data support for per-domain hart context so that a dedicated hartindex_to_context_table[] in struct sbi_domain is not needed. 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:529 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_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 -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 -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 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 per-domain hartindex_to_context_table[] is yet another per-domain data required for implementing hart entry into (or exit from) domain. Use the recently added domain data support for per-domain hart context so that a dedicated hartindex_to_context_table[] in struct sbi_domain is not needed. Signed-off-by: Anup Patel Reviewed-by: Yu Chien Peter Lin --- include/sbi/sbi_domain.h | 2 - include/sbi/sbi_domain_context.h | 57 +++-------------- lib/sbi/sbi_domain.c | 9 ++- lib/sbi/sbi_domain_context.c | 104 +++++++++++++++++++++++++++---- 4 files changed, 110 insertions(+), 62 deletions(-) diff --git a/include/sbi/sbi_domain.h b/include/sbi/sbi_domain.h index 60d7a776..1ecf3116 100644 --- a/include/sbi/sbi_domain.h +++ b/include/sbi/sbi_domain.h @@ -176,8 +176,6 @@ struct sbi_domain { char name[64]; /** Possible HARTs in this domain */ const struct sbi_hartmask *possible_harts; - /** Contexts for possible HARTs indexed by hartindex */ - struct sbi_context *hartindex_to_context_table[SBI_HARTMASK_MAX_BITS]; /** Array of memory regions terminated by a region with order zero */ struct sbi_domain_memregion *regions; /** HART id of the HART booting this domain */ diff --git a/include/sbi/sbi_domain_context.h b/include/sbi/sbi_domain_context.h index 3f43b622..31a3a7f8 100755 --- a/include/sbi/sbi_domain_context.h +++ b/include/sbi/sbi_domain_context.h @@ -8,56 +8,9 @@ #define __SBI_DOMAIN_CONTEXT_H__ #include -#include struct sbi_domain; -/** Context representation for a hart within a domain */ -struct sbi_context { - /** Trap-related states such as GPRs, mepc, and mstatus */ - struct sbi_trap_context trap_ctx; - - /** Supervisor status register */ - unsigned long sstatus; - /** Supervisor interrupt enable register */ - unsigned long sie; - /** Supervisor trap vector base address register */ - unsigned long stvec; - /** Supervisor scratch register for temporary storage */ - unsigned long sscratch; - /** Supervisor exception program counter register */ - unsigned long sepc; - /** Supervisor cause register */ - unsigned long scause; - /** Supervisor trap value register */ - unsigned long stval; - /** Supervisor interrupt pending register */ - unsigned long sip; - /** Supervisor address translation and protection register */ - unsigned long satp; - /** Counter-enable register */ - unsigned long scounteren; - /** Supervisor environment configuration register */ - unsigned long senvcfg; - - /** Reference to the owning domain */ - struct sbi_domain *dom; - /** Previous context (caller) to jump to during context exits */ - struct sbi_context *prev_ctx; - /** Is context initialized and runnable */ - bool initialized; -}; - -/** Get the context pointer for a given hart index and domain */ -#define sbi_hartindex_to_domain_context(__hartindex, __d) \ - (__d)->hartindex_to_context_table[__hartindex] - -/** Macro to obtain the current hart's context pointer */ -#define sbi_domain_context_thishart_ptr() \ - sbi_hartindex_to_domain_context( \ - sbi_hartid_to_hartindex(current_hartid()), \ - sbi_domain_thishart_ptr()) - /** * Enter a specific domain context synchronously * @param dom pointer to domain @@ -75,4 +28,14 @@ int sbi_domain_context_enter(struct sbi_domain *dom); */ int sbi_domain_context_exit(void); +/** + * Initialize domain context support + * + * @return 0 on success and negative error code on failure + */ +int sbi_domain_context_init(void); + +/* Deinitialize domain context support */ +void sbi_domain_context_deinit(void); + #endif // __SBI_DOMAIN_CONTEXT_H__ diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c index 22c5c752..ee6e2569 100644 --- a/lib/sbi/sbi_domain.c +++ b/lib/sbi/sbi_domain.c @@ -781,11 +781,16 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid) if (!domain_hart_ptr_offset) return SBI_ENOMEM; + /* Initialize domain context support */ + rc = sbi_domain_context_init(); + if (rc) + goto fail_free_domain_hart_ptr_offset; + root_memregs = sbi_calloc(sizeof(*root_memregs), ROOT_REGION_MAX + 1); if (!root_memregs) { sbi_printf("%s: no memory for root regions\n", __func__); rc = SBI_ENOMEM; - goto fail_free_domain_hart_ptr_offset; + goto fail_deinit_context; } root.regions = root_memregs; @@ -850,6 +855,8 @@ fail_free_root_hmask: sbi_free(root_hmask); fail_free_root_memregs: sbi_free(root_memregs); +fail_deinit_context: + sbi_domain_context_deinit(); fail_free_domain_hart_ptr_offset: sbi_scratch_free_offset(domain_hart_ptr_offset); return rc; diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c index 29e2d280..b70db41e 100755 --- a/lib/sbi/sbi_domain_context.c +++ b/lib/sbi/sbi_domain_context.c @@ -14,6 +14,76 @@ #include #include #include +#include + +/** Context representation for a hart within a domain */ +struct hart_context { + /** Trap-related states such as GPRs, mepc, and mstatus */ + struct sbi_trap_context trap_ctx; + + /** Supervisor status register */ + unsigned long sstatus; + /** Supervisor interrupt enable register */ + unsigned long sie; + /** Supervisor trap vector base address register */ + unsigned long stvec; + /** Supervisor scratch register for temporary storage */ + unsigned long sscratch; + /** Supervisor exception program counter register */ + unsigned long sepc; + /** Supervisor cause register */ + unsigned long scause; + /** Supervisor trap value register */ + unsigned long stval; + /** Supervisor interrupt pending register */ + unsigned long sip; + /** Supervisor address translation and protection register */ + unsigned long satp; + /** Counter-enable register */ + unsigned long scounteren; + /** Supervisor environment configuration register */ + unsigned long senvcfg; + + /** Reference to the owning domain */ + struct sbi_domain *dom; + /** Previous context (caller) to jump to during context exits */ + struct hart_context *prev_ctx; + /** Is context initialized and runnable */ + bool initialized; +}; + +struct domain_context_priv { + /** Contexts for possible HARTs indexed by hartindex */ + struct hart_context *hartindex_to_context_table[SBI_HARTMASK_MAX_BITS]; +}; + +static struct sbi_domain_data dcpriv = { + .data_size = sizeof(struct domain_context_priv), +}; + +static inline struct hart_context *hart_context_get(struct sbi_domain *dom, + u32 hartindex) +{ + struct domain_context_priv *dcp = sbi_domain_data_ptr(dom, &dcpriv); + + return (dcp && hartindex < SBI_HARTMASK_MAX_BITS) ? + dcp->hartindex_to_context_table[hartindex] : NULL; +} + +static void hart_context_set(struct sbi_domain *dom, u32 hartindex, + struct hart_context *hc) +{ + struct domain_context_priv *dcp = sbi_domain_data_ptr(dom, &dcpriv); + + if (dcp && hartindex < SBI_HARTMASK_MAX_BITS) { + dcp->hartindex_to_context_table[hartindex] = hc; + } +} + +/** Macro to obtain the current hart's context pointer */ +#define hart_context_thishart_get() \ + hart_context_get(sbi_domain_thishart_ptr(), \ + sbi_hartid_to_hartindex(current_hartid())) /** * Switches the HART context from the current domain to the target domain. @@ -23,8 +93,8 @@ * @param ctx pointer to the current HART context * @param dom_ctx pointer to the target domain context */ -static void switch_to_next_domain_context(struct sbi_context *ctx, - struct sbi_context *dom_ctx) +static void switch_to_next_domain_context(struct hart_context *ctx, + struct hart_context *dom_ctx) { u32 hartindex = sbi_hartid_to_hartindex(current_hartid()); struct sbi_trap_context *trap_ctx; @@ -89,9 +159,9 @@ static void switch_to_next_domain_context(struct sbi_context *ctx, int sbi_domain_context_enter(struct sbi_domain *dom) { - struct sbi_context *ctx = sbi_domain_context_thishart_ptr(); - struct sbi_context *dom_ctx = sbi_hartindex_to_domain_context( - sbi_hartid_to_hartindex(current_hartid()), dom); + struct hart_context *ctx = hart_context_thishart_get(); + struct hart_context *dom_ctx = hart_context_get(dom, + sbi_hartid_to_hartindex(current_hartid())); /* Validate the domain context existence */ if (!dom_ctx) @@ -109,8 +179,8 @@ int sbi_domain_context_exit(void) { u32 hartindex = sbi_hartid_to_hartindex(current_hartid()); struct sbi_domain *dom; - struct sbi_context *ctx = sbi_domain_context_thishart_ptr(); - struct sbi_context *dom_ctx, *tmp; + struct hart_context *ctx = hart_context_thishart_get(); + struct hart_context *dom_ctx, *tmp; /* * If it's first time to call `exit` on the current hart, no @@ -123,16 +193,16 @@ int sbi_domain_context_exit(void) dom->possible_harts)) continue; - dom_ctx = sbi_zalloc(sizeof(struct sbi_context)); + dom_ctx = sbi_zalloc(sizeof(struct hart_context)); if (!dom_ctx) return SBI_ENOMEM; /* Bind context and domain */ dom_ctx->dom = dom; - dom->hartindex_to_context_table[hartindex] = dom_ctx; + hart_context_set(dom, hartindex, dom_ctx); } - ctx = sbi_domain_context_thishart_ptr(); + ctx = hart_context_thishart_get(); } dom_ctx = ctx->prev_ctx; @@ -144,7 +214,7 @@ int sbi_domain_context_exit(void) if (dom == &root || dom == sbi_domain_thishart_ptr()) continue; - tmp = sbi_hartindex_to_domain_context(hartindex, dom); + tmp = hart_context_get(dom, hartindex); if (tmp && !tmp->initialized) { dom_ctx = tmp; break; @@ -154,9 +224,19 @@ int sbi_domain_context_exit(void) /* Take the root domain context if fail to find */ if (!dom_ctx) - dom_ctx = sbi_hartindex_to_domain_context(hartindex, &root); + dom_ctx = hart_context_get(&root, hartindex); switch_to_next_domain_context(ctx, dom_ctx); return 0; } + +int sbi_domain_context_init(void) +{ + return sbi_domain_register_data(&dcpriv); +} + +void sbi_domain_context_deinit(void) +{ + sbi_domain_unregister_data(&dcpriv); +}