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