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