xref: /rk3399_ARM-atf/lib/el3_runtime/aarch64/context_debug.c (revision 48bbb8e45e90db964d6496a3f8a51fd042ac6d44)
1 /*
2  * Copyright (c) 2023-2025, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <string.h>
8 
9 #include <arch_features.h>
10 #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  *******************************************************************************/
get_context_name_by_idx(unsigned int security_state_idx)19 static 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 
32 #define PRINT_MEM_USAGE_SEPARATOR()					\
33 	do {								\
34 		printf("+-----------+-----------+-----------"		\
35 		"+-----------+-----------+-----------+\n");		\
36 	} while (false)
37 
38 #define NAME_PLACEHOLDER_LEN 14
39 
40 #define PRINT_DASH(n)							\
41 	for (; n > 0; n--) {						\
42 		putchar('-');						\
43 	}
44 
45 #define PRINT_SINGLE_MEM_USAGE_SEP_BLOCK()				\
46 	do {								\
47 		printf("+-----------");					\
48 	} while (false)
49 
50 /********************************************************************************
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  *******************************************************************************/
report_allocated_memory(unsigned int security_state_idx)55 static size_t report_allocated_memory(unsigned int security_state_idx)
56 {
57 	size_t core_total = 0U;
58 	size_t gp_total = 0U;
59 	size_t el3_total = 0U;
60 	size_t other_total = 0U;
61 	size_t total = 0U;
62 	size_t per_world_ctx_size = 0U;
63 
64 	size_t elx_total = 0U;
65 	size_t pauth_total = 0U;
66 
67 	if (is_ctx_pauth_supported()) {
68 		PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
69 	}
70 
71 	PRINT_MEM_USAGE_SEPARATOR();
72 
73 	printf("|    Core   |     GP    |    EL3    ");
74 #if CTX_INCLUDE_EL2_REGS
75 	printf("|    EL2    ");
76 #else
77 	printf("|    EL1    ");
78 #endif /* CTX_INCLUDE_EL2_REGS */
79 
80 	if (is_ctx_pauth_supported()) {
81 		printf("|   PAUTH   ");
82 	}
83 
84 	printf("|   Other   |   Total   |\n");
85 
86 	/* Compute memory usage for each core's context */
87 	for (unsigned int i = 0U; i < PLATFORM_CORE_COUNT; i++) {
88 		size_t size_other = 0U;
89 		size_t el3_size = 0U;
90 		size_t gp_size = 0U;
91 		size_t elx_size = 0U;
92 
93 		if (is_ctx_pauth_supported()) {
94 			PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
95 		}
96 
97 		PRINT_MEM_USAGE_SEPARATOR();
98 
99 		cpu_context_t *ctx = (cpu_context_t *)cm_get_context_by_index(i,
100 			security_state_idx);
101 		core_total = sizeof(*ctx);
102 		el3_size = sizeof(ctx->el3state_ctx);
103 		gp_size = sizeof(ctx->gpregs_ctx);
104 		size_other = core_total - (el3_size + gp_size);
105 		printf("| %9u | %8luB | %8luB ", i, gp_size, el3_size);
106 
107 #if CTX_INCLUDE_EL2_REGS
108 		elx_size = sizeof(ctx->el2_sysregs_ctx);
109 #else
110 		elx_size = sizeof(ctx->el1_sysregs_ctx);
111 #endif /* CTX_INCLUDE_EL2_REGS */
112 		size_other -= elx_size;
113 		elx_total += elx_size;
114 		printf("| %8luB ", elx_size);
115 
116 		if (is_ctx_pauth_supported()) {
117 			size_t pauth_size = sizeof(get_pauth_ctx(ctx));
118 			size_other -= pauth_size;
119 			pauth_total += pauth_size;
120 			printf("| %8luB ", pauth_size);
121 		}
122 		printf("| %8luB | %8luB |\n", size_other, core_total);
123 
124 		gp_total += gp_size;
125 		el3_total += el3_size;
126 		other_total += size_other;
127 		total += core_total;
128 	}
129 
130 	if (is_ctx_pauth_supported()) {
131 		PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
132 	}
133 
134 	PRINT_MEM_USAGE_SEPARATOR();
135 
136 	if (is_ctx_pauth_supported()) {
137 		PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
138 	}
139 
140 	PRINT_MEM_USAGE_SEPARATOR();
141 
142 	printf("|    All    | %8luB | %8luB ", gp_total, el3_total);
143 
144 	printf("| %8luB ", elx_total);
145 
146 	if (is_ctx_pauth_supported()) {
147 		printf("| %8luB ", pauth_total);
148 	}
149 
150 	printf("| %8luB | %8luB |\n", other_total, total);
151 
152 	if (is_ctx_pauth_supported()) {
153 		PRINT_SINGLE_MEM_USAGE_SEP_BLOCK();
154 	}
155 	PRINT_MEM_USAGE_SEPARATOR();
156 	printf("\n");
157 
158 	/* Compute memory usage for the global context */
159 	per_world_ctx_size = sizeof(per_world_context[security_state_idx]);
160 
161 	total += per_world_ctx_size;
162 
163 	printf("Per-world context: %luB\n\n", per_world_ctx_size);
164 
165 	printf("TOTAL: %luB\n", total);
166 
167 	return total;
168 }
169 
170 /********************************************************************************
171  * Reports the allocated memory for every security state and then reports the
172  * total system-wide allocated memory.
173  *******************************************************************************/
report_ctx_memory_usage(void)174 void report_ctx_memory_usage(void)
175 {
176 	INFO("Context memory allocation:\n");
177 
178 	size_t total = 0U;
179 
180 	for (unsigned int i = 0U; i < CPU_CONTEXT_NUM; i++) {
181 		const char *context_name = get_context_name_by_idx(i);
182 		size_t len = 0U;
183 
184 		printf("Memory usage for %s:\n", context_name);
185 		total += report_allocated_memory(i);
186 			printf("------------------------");
187 			len = NAME_PLACEHOLDER_LEN - printf("End %s", context_name);
188 			PRINT_DASH(len);
189 			printf("-----------------------\n\n");
190 	}
191 
192 	printf("Total context memory allocated: %luB\n\n", total);
193 }
194