blob: 1addb45961b91bcfe1be28df2318292f6bf0bdb9 [file] [log] [blame]
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -06001/*
Boyan Karatotevaebc7e92025-04-02 11:16:18 +01002 * Copyright (c) 2023-2025, Arm Limited and Contributors. All rights reserved.
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -06003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <string.h>
8
Boyan Karatotevaebc7e92025-04-02 11:16:18 +01009#include <arch_features.h>
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060010#include <common/debug.h>
11#include <context.h>
12#include <lib/el3_runtime/context_mgmt.h>
13#include <lib/el3_runtime/cpu_data.h>
14
15/********************************************************************************
16 * Function that returns the corresponding string constant for a security state
17 * index.
18 *******************************************************************************/
19static const char *get_context_name_by_idx(unsigned int security_state_idx)
20{
21 assert(security_state_idx < CPU_CONTEXT_NUM);
22 static const char * const state_names[] = {
23 "Secure",
24 "Non Secure"
25#if ENABLE_RME
26 , "Realm"
27#endif /* ENABLE_RME */
28 };
29 return state_names[security_state_idx];
30}
31
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060032#define PRINT_MEM_USAGE_SEPARATOR() \
33 do { \
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +010034 printf("+-----------+-----------+-----------" \
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060035 "+-----------+-----------+-----------+\n"); \
36 } while (false)
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060037
38#define NAME_PLACEHOLDER_LEN 14
39
40#define PRINT_DASH(n) \
41 for (; n > 0; n--) { \
42 putchar('-'); \
43 }
44
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +010045#define PRINT_SINGLE_MEM_USAGE_SEP_BLOCK() \
46 do { \
47 printf("+-----------"); \
48 } while (false)
49
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060050/********************************************************************************
51 * This function prints the allocated memory for a specific security state.
52 * Values are grouped by exception level and core. The memory usage for the
53 * global context and the total memory for the security state are also computed.
54 *******************************************************************************/
55static size_t report_allocated_memory(unsigned int security_state_idx)
56{
57 size_t core_total = 0U;
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +010058 size_t gp_total = 0U;
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060059 size_t el3_total = 0U;
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +010060 size_t other_total = 0U;
61 size_t total = 0U;
62 size_t per_world_ctx_size = 0U;
63
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060064#if CTX_INCLUDE_EL2_REGS
65 size_t el2_total = 0U;
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +010066#else
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060067 size_t el1_total = 0U;
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +010068#endif /* CTX_INCLUDE_EL2_REGS */
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +010069 size_t pauth_total = 0U;
Boyan Karatotevaebc7e92025-04-02 11:16:18 +010070
71 if (is_ctx_pauth_supported()) {
72 PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
73 }
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060074
75 PRINT_MEM_USAGE_SEPARATOR();
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +010076
77 printf("| Core | GP | EL3 ");
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060078#if CTX_INCLUDE_EL2_REGS
79 printf("| EL2 ");
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +010080#else
81 printf("| EL1 ");
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060082#endif /* CTX_INCLUDE_EL2_REGS */
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +010083
Boyan Karatotevaebc7e92025-04-02 11:16:18 +010084 if (is_ctx_pauth_supported()) {
85 printf("| PAUTH ");
86 }
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +010087
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +010088 printf("| Other | Total |\n");
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060089
90 /* Compute memory usage for each core's context */
91 for (unsigned int i = 0U; i < PLATFORM_CORE_COUNT; i++) {
92 size_t size_other = 0U;
93 size_t el3_size = 0U;
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +010094 size_t gp_size = 0U;
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060095#if CTX_INCLUDE_EL2_REGS
96 size_t el2_size = 0U;
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +010097#else
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -060098 size_t el1_size = 0U;
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +010099#endif /* CTX_INCLUDE_EL2_REGS */
Boyan Karatotevaebc7e92025-04-02 11:16:18 +0100100
101 if (is_ctx_pauth_supported()) {
102 PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
103 }
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100104
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600105 PRINT_MEM_USAGE_SEPARATOR();
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100106
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600107 cpu_context_t *ctx = (cpu_context_t *)cm_get_context_by_index(i,
108 security_state_idx);
109 core_total = sizeof(*ctx);
110 el3_size = sizeof(ctx->el3state_ctx);
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100111 gp_size = sizeof(ctx->gpregs_ctx);
112 size_other = core_total - (el3_size + gp_size);
113 printf("| %9u | %8luB | %8luB ", i, gp_size, el3_size);
114
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600115#if CTX_INCLUDE_EL2_REGS
116 el2_size = sizeof(ctx->el2_sysregs_ctx);
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600117 size_other -= el2_size;
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100118 el2_total += el2_size;
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600119 printf("| %8luB ", el2_size);
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +0100120#else
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100121 el1_size = sizeof(ctx->el1_sysregs_ctx);
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +0100122 size_other -= el1_size;
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100123 el1_total += el1_size;
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +0100124 printf("| %8luB ", el1_size);
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600125#endif /* CTX_INCLUDE_EL2_REGS */
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100126
Boyan Karatotev400fd5f2025-04-17 15:45:52 +0100127#if CTX_INCLUDE_PAUTH_REGS
Boyan Karatotevaebc7e92025-04-02 11:16:18 +0100128 if (is_ctx_pauth_supported()) {
Boyan Karatotev400fd5f2025-04-17 15:45:52 +0100129 size_t pauth_size = sizeof(ctx->pauth_ctx);
Boyan Karatotevaebc7e92025-04-02 11:16:18 +0100130 size_other -= pauth_size;
131 pauth_total += pauth_size;
132 printf("| %8luB ", pauth_size);
133 }
Boyan Karatotev400fd5f2025-04-17 15:45:52 +0100134#endif
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +0100135 printf("| %8luB | %8luB |\n", size_other, core_total);
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600136
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100137 gp_total += gp_size;
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600138 el3_total += el3_size;
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600139 other_total += size_other;
140 total += core_total;
141 }
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100142
Boyan Karatotevaebc7e92025-04-02 11:16:18 +0100143 if (is_ctx_pauth_supported()) {
144 PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
145 }
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100146
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600147 PRINT_MEM_USAGE_SEPARATOR();
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100148
Boyan Karatotevaebc7e92025-04-02 11:16:18 +0100149 if (is_ctx_pauth_supported()) {
150 PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
151 }
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100152
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600153 PRINT_MEM_USAGE_SEPARATOR();
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100154
155 printf("| All | %8luB | %8luB ", gp_total, el3_total);
156
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600157#if CTX_INCLUDE_EL2_REGS
158 printf("| %8luB ", el2_total);
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +0100159#else
160 printf("| %8luB ", el1_total);
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600161#endif /* CTX_INCLUDE_EL2_REGS */
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100162
Boyan Karatotevaebc7e92025-04-02 11:16:18 +0100163 if (is_ctx_pauth_supported()) {
164 printf("| %8luB ", pauth_total);
165 }
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100166
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +0100167 printf("| %8luB | %8luB |\n", other_total, total);
Jayanth Dodderi Chidanand1b33abf2024-05-20 23:29:05 +0100168
Boyan Karatotevaebc7e92025-04-02 11:16:18 +0100169 if (is_ctx_pauth_supported()) {
170 PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
171 }
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600172 PRINT_MEM_USAGE_SEPARATOR();
173 printf("\n");
174
175 /* Compute memory usage for the global context */
176 per_world_ctx_size = sizeof(per_world_context[security_state_idx]);
177
178 total += per_world_ctx_size;
179
180 printf("Per-world context: %luB\n\n", per_world_ctx_size);
181
182 printf("TOTAL: %luB\n", total);
183
184 return total;
185}
186
187/********************************************************************************
188 * Reports the allocated memory for every security state and then reports the
189 * total system-wide allocated memory.
190 *******************************************************************************/
191void report_ctx_memory_usage(void)
192{
193 INFO("Context memory allocation:\n");
194
195 size_t total = 0U;
196
197 for (unsigned int i = 0U; i < CPU_CONTEXT_NUM; i++) {
198 const char *context_name = get_context_name_by_idx(i);
199 size_t len = 0U;
200
201 printf("Memory usage for %s:\n", context_name);
202 total += report_allocated_memory(i);
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +0100203 printf("------------------------");
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600204 len = NAME_PLACEHOLDER_LEN - printf("End %s", context_name);
205 PRINT_DASH(len);
Jayanth Dodderi Chidanand9abe23b2024-05-07 18:50:57 +0100206 printf("-----------------------\n\n");
Juan Pablo Condeb5ec1382023-11-08 16:14:28 -0600207 }
208
209 printf("Total context memory allocated: %luB\n\n", total);
210}