xref: /rk3399_ARM-atf/lib/el3_runtime/aarch64/context_debug.c (revision bfef8b908e3a3cc29656c1d30a6b53490c79539b)
1*bfef8b90SJuan Pablo Conde /*
2*bfef8b90SJuan Pablo Conde  * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
3*bfef8b90SJuan Pablo Conde  *
4*bfef8b90SJuan Pablo Conde  * SPDX-License-Identifier: BSD-3-Clause
5*bfef8b90SJuan Pablo Conde  */
6*bfef8b90SJuan Pablo Conde 
7*bfef8b90SJuan Pablo Conde #include <string.h>
8*bfef8b90SJuan Pablo Conde 
9*bfef8b90SJuan Pablo Conde #include <common/debug.h>
10*bfef8b90SJuan Pablo Conde #include <context.h>
11*bfef8b90SJuan Pablo Conde #include <lib/el3_runtime/context_mgmt.h>
12*bfef8b90SJuan Pablo Conde #include <lib/el3_runtime/cpu_data.h>
13*bfef8b90SJuan Pablo Conde 
14*bfef8b90SJuan Pablo Conde /********************************************************************************
15*bfef8b90SJuan Pablo Conde  * Function that returns the corresponding string constant for a security state
16*bfef8b90SJuan Pablo Conde  * index.
17*bfef8b90SJuan Pablo Conde  *******************************************************************************/
18*bfef8b90SJuan Pablo Conde static const char *get_context_name_by_idx(unsigned int security_state_idx)
19*bfef8b90SJuan Pablo Conde {
20*bfef8b90SJuan Pablo Conde 	assert(security_state_idx < CPU_CONTEXT_NUM);
21*bfef8b90SJuan Pablo Conde 	static const char * const state_names[] = {
22*bfef8b90SJuan Pablo Conde 		"Secure",
23*bfef8b90SJuan Pablo Conde 		"Non Secure"
24*bfef8b90SJuan Pablo Conde #if ENABLE_RME
25*bfef8b90SJuan Pablo Conde 		, "Realm"
26*bfef8b90SJuan Pablo Conde #endif /* ENABLE_RME */
27*bfef8b90SJuan Pablo Conde 	};
28*bfef8b90SJuan Pablo Conde 	return state_names[security_state_idx];
29*bfef8b90SJuan Pablo Conde }
30*bfef8b90SJuan Pablo Conde 
31*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
32*bfef8b90SJuan Pablo Conde #define PRINT_MEM_USAGE_SEPARATOR()					\
33*bfef8b90SJuan Pablo Conde 	do {								\
34*bfef8b90SJuan Pablo Conde 		printf("+-----------+-----------+-----------"		\
35*bfef8b90SJuan Pablo Conde 			"+-----------+-----------+-----------+\n");	\
36*bfef8b90SJuan Pablo Conde 	} while (false)
37*bfef8b90SJuan Pablo Conde #else
38*bfef8b90SJuan Pablo Conde #define PRINT_MEM_USAGE_SEPARATOR()					\
39*bfef8b90SJuan Pablo Conde 	do {								\
40*bfef8b90SJuan Pablo Conde 		printf("+-----------+-----------"			\
41*bfef8b90SJuan Pablo Conde 		"+-----------+-----------+-----------+\n");		\
42*bfef8b90SJuan Pablo Conde 	} while (false)
43*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
44*bfef8b90SJuan Pablo Conde 
45*bfef8b90SJuan Pablo Conde #define NAME_PLACEHOLDER_LEN 14
46*bfef8b90SJuan Pablo Conde 
47*bfef8b90SJuan Pablo Conde #define PRINT_DASH(n)							\
48*bfef8b90SJuan Pablo Conde 	for (; n > 0; n--) {						\
49*bfef8b90SJuan Pablo Conde 		putchar('-');						\
50*bfef8b90SJuan Pablo Conde 	}
51*bfef8b90SJuan Pablo Conde 
52*bfef8b90SJuan Pablo Conde /********************************************************************************
53*bfef8b90SJuan Pablo Conde  * This function prints the allocated memory for a specific security state.
54*bfef8b90SJuan Pablo Conde  * Values are grouped by exception level and core. The memory usage for the
55*bfef8b90SJuan Pablo Conde  * global context and the total memory for the security state are also computed.
56*bfef8b90SJuan Pablo Conde  *******************************************************************************/
57*bfef8b90SJuan Pablo Conde static size_t report_allocated_memory(unsigned int security_state_idx)
58*bfef8b90SJuan Pablo Conde {
59*bfef8b90SJuan Pablo Conde 	size_t core_total = 0U;
60*bfef8b90SJuan Pablo Conde 	size_t el3_total = 0U;
61*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
62*bfef8b90SJuan Pablo Conde 	size_t el2_total = 0U;
63*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
64*bfef8b90SJuan Pablo Conde 	size_t el1_total = 0U;
65*bfef8b90SJuan Pablo Conde 	size_t other_total = 0U;
66*bfef8b90SJuan Pablo Conde 	size_t total = 0U;
67*bfef8b90SJuan Pablo Conde 	size_t per_world_ctx_size = 0U;
68*bfef8b90SJuan Pablo Conde 
69*bfef8b90SJuan Pablo Conde 	PRINT_MEM_USAGE_SEPARATOR();
70*bfef8b90SJuan Pablo Conde 	printf("|    Core   |    EL3    ");
71*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
72*bfef8b90SJuan Pablo Conde 	printf("|    EL2    ");
73*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
74*bfef8b90SJuan Pablo Conde 	printf("|    EL1    |   Other   |   Total   |\n");
75*bfef8b90SJuan Pablo Conde 
76*bfef8b90SJuan Pablo Conde 	/* Compute memory usage for each core's context */
77*bfef8b90SJuan Pablo Conde 	for (unsigned int i = 0U; i < PLATFORM_CORE_COUNT; i++) {
78*bfef8b90SJuan Pablo Conde 		size_t size_other = 0U;
79*bfef8b90SJuan Pablo Conde 		size_t el3_size = 0U;
80*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
81*bfef8b90SJuan Pablo Conde 		size_t el2_size = 0U;
82*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
83*bfef8b90SJuan Pablo Conde 		size_t el1_size = 0U;
84*bfef8b90SJuan Pablo Conde 
85*bfef8b90SJuan Pablo Conde 		PRINT_MEM_USAGE_SEPARATOR();
86*bfef8b90SJuan Pablo Conde 		cpu_context_t *ctx = (cpu_context_t *)cm_get_context_by_index(i,
87*bfef8b90SJuan Pablo Conde 			security_state_idx);
88*bfef8b90SJuan Pablo Conde 		core_total = sizeof(*ctx);
89*bfef8b90SJuan Pablo Conde 		el3_size = sizeof(ctx->el3state_ctx);
90*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
91*bfef8b90SJuan Pablo Conde 		el2_size = sizeof(ctx->el2_sysregs_ctx);
92*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
93*bfef8b90SJuan Pablo Conde 		el1_size = sizeof(ctx->el1_sysregs_ctx);
94*bfef8b90SJuan Pablo Conde 
95*bfef8b90SJuan Pablo Conde 		size_other = core_total - el3_size - el1_size;
96*bfef8b90SJuan Pablo Conde 		printf("| %9u | %8luB ", i, el3_size);
97*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
98*bfef8b90SJuan Pablo Conde 		size_other -= el2_size;
99*bfef8b90SJuan Pablo Conde 		printf("| %8luB ", el2_size);
100*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
101*bfef8b90SJuan Pablo Conde 		printf("| %8luB | %8luB | %8luB |\n", el1_size, size_other, core_total);
102*bfef8b90SJuan Pablo Conde 
103*bfef8b90SJuan Pablo Conde 		el3_total += el3_size;
104*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
105*bfef8b90SJuan Pablo Conde 		el2_total += el2_size;
106*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
107*bfef8b90SJuan Pablo Conde 		el1_total += el1_size;
108*bfef8b90SJuan Pablo Conde 		other_total += size_other;
109*bfef8b90SJuan Pablo Conde 		total += core_total;
110*bfef8b90SJuan Pablo Conde 	}
111*bfef8b90SJuan Pablo Conde 	PRINT_MEM_USAGE_SEPARATOR();
112*bfef8b90SJuan Pablo Conde 	PRINT_MEM_USAGE_SEPARATOR();
113*bfef8b90SJuan Pablo Conde 	printf("|    All    | %8luB ", el3_total);
114*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
115*bfef8b90SJuan Pablo Conde 	printf("| %8luB ", el2_total);
116*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
117*bfef8b90SJuan Pablo Conde 	printf("| %8luB | %8luB | %8luB |\n", el1_total, other_total, total);
118*bfef8b90SJuan Pablo Conde 	PRINT_MEM_USAGE_SEPARATOR();
119*bfef8b90SJuan Pablo Conde 	printf("\n");
120*bfef8b90SJuan Pablo Conde 
121*bfef8b90SJuan Pablo Conde 	/* Compute memory usage for the global context */
122*bfef8b90SJuan Pablo Conde 	per_world_ctx_size = sizeof(per_world_context[security_state_idx]);
123*bfef8b90SJuan Pablo Conde 
124*bfef8b90SJuan Pablo Conde 	total += per_world_ctx_size;
125*bfef8b90SJuan Pablo Conde 
126*bfef8b90SJuan Pablo Conde 	printf("Per-world context: %luB\n\n", per_world_ctx_size);
127*bfef8b90SJuan Pablo Conde 
128*bfef8b90SJuan Pablo Conde 	printf("TOTAL: %luB\n", total);
129*bfef8b90SJuan Pablo Conde 
130*bfef8b90SJuan Pablo Conde 	return total;
131*bfef8b90SJuan Pablo Conde }
132*bfef8b90SJuan Pablo Conde 
133*bfef8b90SJuan Pablo Conde /********************************************************************************
134*bfef8b90SJuan Pablo Conde  * Reports the allocated memory for every security state and then reports the
135*bfef8b90SJuan Pablo Conde  * total system-wide allocated memory.
136*bfef8b90SJuan Pablo Conde  *******************************************************************************/
137*bfef8b90SJuan Pablo Conde void report_ctx_memory_usage(void)
138*bfef8b90SJuan Pablo Conde {
139*bfef8b90SJuan Pablo Conde 	INFO("Context memory allocation:\n");
140*bfef8b90SJuan Pablo Conde 
141*bfef8b90SJuan Pablo Conde 	size_t total = 0U;
142*bfef8b90SJuan Pablo Conde 
143*bfef8b90SJuan Pablo Conde 	for (unsigned int i = 0U; i < CPU_CONTEXT_NUM; i++) {
144*bfef8b90SJuan Pablo Conde 		const char *context_name = get_context_name_by_idx(i);
145*bfef8b90SJuan Pablo Conde 		size_t len = 0U;
146*bfef8b90SJuan Pablo Conde 
147*bfef8b90SJuan Pablo Conde 		printf("Memory usage for %s:\n", context_name);
148*bfef8b90SJuan Pablo Conde 		total += report_allocated_memory(i);
149*bfef8b90SJuan Pablo Conde 			printf("------------------------"
150*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
151*bfef8b90SJuan Pablo Conde 				"------"
152*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
153*bfef8b90SJuan Pablo Conde 			      );
154*bfef8b90SJuan Pablo Conde 			len = NAME_PLACEHOLDER_LEN - printf("End %s", context_name);
155*bfef8b90SJuan Pablo Conde 			PRINT_DASH(len);
156*bfef8b90SJuan Pablo Conde 			printf(
157*bfef8b90SJuan Pablo Conde #if CTX_INCLUDE_EL2_REGS
158*bfef8b90SJuan Pablo Conde 				"------"
159*bfef8b90SJuan Pablo Conde #endif /* CTX_INCLUDE_EL2_REGS */
160*bfef8b90SJuan Pablo Conde 				"-----------------------\n\n");
161*bfef8b90SJuan Pablo Conde 	}
162*bfef8b90SJuan Pablo Conde 
163*bfef8b90SJuan Pablo Conde 	printf("Total context memory allocated: %luB\n\n", total);
164*bfef8b90SJuan Pablo Conde }
165