blob: 0a14e1f53f761fd506149f49b421e8f72f731641 [file] [log] [blame]
Juan Pablo Condebfef8b92023-11-08 16:14:28 -06001/*
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +01002 * Copyright (c) 2023-2024, Arm Limited and Contributors. All rights reserved.
Juan Pablo Condebfef8b92023-11-08 16:14:28 -06003 *
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
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060031#define PRINT_MEM_USAGE_SEPARATOR() \
32 do { \
33 printf("+-----------+-----------" \
34 "+-----------+-----------+-----------+\n"); \
35 } while (false)
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060036
37#define NAME_PLACEHOLDER_LEN 14
38
39#define PRINT_DASH(n) \
40 for (; n > 0; n--) { \
41 putchar('-'); \
42 }
43
44/********************************************************************************
45 * This function prints the allocated memory for a specific security state.
46 * Values are grouped by exception level and core. The memory usage for the
47 * global context and the total memory for the security state are also computed.
48 *******************************************************************************/
49static size_t report_allocated_memory(unsigned int security_state_idx)
50{
51 size_t core_total = 0U;
52 size_t el3_total = 0U;
53#if CTX_INCLUDE_EL2_REGS
54 size_t el2_total = 0U;
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010055#else
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060056 size_t el1_total = 0U;
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010057#endif /* CTX_INCLUDE_EL2_REGS */
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060058 size_t other_total = 0U;
59 size_t total = 0U;
60 size_t per_world_ctx_size = 0U;
61
62 PRINT_MEM_USAGE_SEPARATOR();
63 printf("| Core | EL3 ");
64#if CTX_INCLUDE_EL2_REGS
65 printf("| EL2 ");
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010066#else
67 printf("| EL1 ");
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060068#endif /* CTX_INCLUDE_EL2_REGS */
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010069 printf("| Other | Total |\n");
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060070
71 /* Compute memory usage for each core's context */
72 for (unsigned int i = 0U; i < PLATFORM_CORE_COUNT; i++) {
73 size_t size_other = 0U;
74 size_t el3_size = 0U;
75#if CTX_INCLUDE_EL2_REGS
76 size_t el2_size = 0U;
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010077#else
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060078 size_t el1_size = 0U;
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010079#endif /* CTX_INCLUDE_EL2_REGS */
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060080
81 PRINT_MEM_USAGE_SEPARATOR();
82 cpu_context_t *ctx = (cpu_context_t *)cm_get_context_by_index(i,
83 security_state_idx);
84 core_total = sizeof(*ctx);
85 el3_size = sizeof(ctx->el3state_ctx);
86#if CTX_INCLUDE_EL2_REGS
87 el2_size = sizeof(ctx->el2_sysregs_ctx);
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010088#else
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060089 el1_size = sizeof(ctx->el1_sysregs_ctx);
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010090#endif /* CTX_INCLUDE_EL2_REGS */
91 size_other = core_total - el3_size;
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060092 printf("| %9u | %8luB ", i, el3_size);
93#if CTX_INCLUDE_EL2_REGS
94 size_other -= el2_size;
95 printf("| %8luB ", el2_size);
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010096#else
97 size_other -= el1_size;
98 printf("| %8luB ", el1_size);
Juan Pablo Condebfef8b92023-11-08 16:14:28 -060099#endif /* CTX_INCLUDE_EL2_REGS */
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +0100100 printf("| %8luB | %8luB |\n", size_other, core_total);
Juan Pablo Condebfef8b92023-11-08 16:14:28 -0600101
102 el3_total += el3_size;
103#if CTX_INCLUDE_EL2_REGS
104 el2_total += el2_size;
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +0100105#else
Juan Pablo Condebfef8b92023-11-08 16:14:28 -0600106 el1_total += el1_size;
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +0100107#endif /* CTX_INCLUDE_EL2_REGS */
Juan Pablo Condebfef8b92023-11-08 16:14:28 -0600108 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);
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +0100116#else
117 printf("| %8luB ", el1_total);
Juan Pablo Condebfef8b92023-11-08 16:14:28 -0600118#endif /* CTX_INCLUDE_EL2_REGS */
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +0100119 printf("| %8luB | %8luB |\n", other_total, total);
Juan Pablo Condebfef8b92023-11-08 16:14:28 -0600120 PRINT_MEM_USAGE_SEPARATOR();
121 printf("\n");
122
123 /* Compute memory usage for the global context */
124 per_world_ctx_size = sizeof(per_world_context[security_state_idx]);
125
126 total += per_world_ctx_size;
127
128 printf("Per-world context: %luB\n\n", per_world_ctx_size);
129
130 printf("TOTAL: %luB\n", total);
131
132 return total;
133}
134
135/********************************************************************************
136 * Reports the allocated memory for every security state and then reports the
137 * total system-wide allocated memory.
138 *******************************************************************************/
139void report_ctx_memory_usage(void)
140{
141 INFO("Context memory allocation:\n");
142
143 size_t total = 0U;
144
145 for (unsigned int i = 0U; i < CPU_CONTEXT_NUM; i++) {
146 const char *context_name = get_context_name_by_idx(i);
147 size_t len = 0U;
148
149 printf("Memory usage for %s:\n", context_name);
150 total += report_allocated_memory(i);
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +0100151 printf("------------------------");
Juan Pablo Condebfef8b92023-11-08 16:14:28 -0600152 len = NAME_PLACEHOLDER_LEN - printf("End %s", context_name);
153 PRINT_DASH(len);
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +0100154 printf("-----------------------\n\n");
Juan Pablo Condebfef8b92023-11-08 16:14:28 -0600155 }
156
157 printf("Total context memory allocated: %luB\n\n", total);
158}