Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #include <arch.h> |
| 7 | #include <arch_helpers.h> |
| 8 | #include <assert.h> |
| 9 | #include <buffer.h> |
| 10 | #include <debug.h> |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame^] | 11 | #include <simd.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 12 | #include <sizes.h> |
| 13 | #include <smc-handler.h> |
| 14 | #include <smc-rmi.h> |
| 15 | #include <smc.h> |
| 16 | #include <status.h> |
| 17 | #include <utils_def.h> |
| 18 | |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 19 | /* Maximum number of supported arguments */ |
| 20 | #define MAX_NUM_ARGS 5 |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 21 | |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 22 | /* Maximum number of output values */ |
| 23 | #define MAX_NUM_OUTPUT_VALS 4 |
| 24 | |
| 25 | #define RMI_STATUS_STRING(_id)[RMI_##_id] = #_id |
| 26 | |
| 27 | const char *rmi_status_string[] = { |
| 28 | RMI_STATUS_STRING(SUCCESS), |
| 29 | RMI_STATUS_STRING(ERROR_INPUT), |
| 30 | RMI_STATUS_STRING(ERROR_REALM), |
| 31 | RMI_STATUS_STRING(ERROR_REC), |
| 32 | RMI_STATUS_STRING(ERROR_RTT), |
| 33 | RMI_STATUS_STRING(ERROR_IN_USE) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 34 | }; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 35 | COMPILER_ASSERT(ARRAY_LEN(rmi_status_string) == RMI_ERROR_COUNT); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * At this level (in handle_ns_smc) we distinguish the RMI calls only on: |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 39 | * - The number of input arguments [0..5], and whether |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 40 | * - The function returns up to three output values in addition |
| 41 | * to the return status code. |
| 42 | * Hence, the naming syntax is: |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 43 | * - `*_[0..5]` when no output values are returned, and |
| 44 | * - `*_[0..3]_o` when the function returns some output values. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 45 | */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 46 | typedef unsigned long (*handler_0)(void); |
| 47 | typedef unsigned long (*handler_1)(unsigned long arg0); |
| 48 | typedef unsigned long (*handler_2)(unsigned long arg0, unsigned long arg1); |
| 49 | typedef unsigned long (*handler_3)(unsigned long arg0, unsigned long arg1, |
| 50 | unsigned long arg2); |
| 51 | typedef unsigned long (*handler_4)(unsigned long arg0, unsigned long arg1, |
| 52 | unsigned long arg2, unsigned long arg3); |
| 53 | typedef unsigned long (*handler_5)(unsigned long arg0, unsigned long arg1, |
| 54 | unsigned long arg2, unsigned long arg3, |
| 55 | unsigned long arg4); |
| 56 | typedef void (*handler_1_o)(unsigned long arg0, struct smc_result *ret); |
| 57 | typedef void (*handler_3_o)(unsigned long arg0, unsigned long arg1, |
| 58 | unsigned long arg2, struct smc_result *ret); |
| 59 | |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 60 | /* |
| 61 | * SMC RMI handler type encoding: |
| 62 | * [0:7] - number of arguments |
| 63 | * [8:15] - number of output values |
| 64 | */ |
| 65 | #define RMI_TYPE(_in, _out) (_in | (_out << 8)) |
| 66 | #define rmi_type(_in, _out) rmi_type_##_in##_out = RMI_TYPE(_in, _out) |
| 67 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 68 | enum rmi_type { |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 69 | rmi_type(0, 0), /* 0 arguments, 0 output values */ |
| 70 | rmi_type(1, 0), /* 1 argument, 0 output values */ |
| 71 | rmi_type(2, 0), /* 2 arguments, 0 output values */ |
| 72 | rmi_type(3, 0), /* 3 arguments, 0 output values */ |
| 73 | rmi_type(4, 0), /* 4 arguments, 0 output values */ |
| 74 | rmi_type(5, 0), /* 5 arguments, 0 output values */ |
| 75 | rmi_type(1, 1), /* 1 argument, 1 output value */ |
| 76 | rmi_type(3, 4) /* 3 arguments, 4 output values */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | struct smc_handler { |
| 80 | const char *fn_name; |
| 81 | enum rmi_type type; |
| 82 | union { |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 83 | handler_0 f_00; |
| 84 | handler_1 f_10; |
| 85 | handler_2 f_20; |
| 86 | handler_3 f_30; |
| 87 | handler_4 f_40; |
| 88 | handler_5 f_50; |
| 89 | handler_1_o f_11; |
| 90 | handler_3_o f_34; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 91 | void *fn_dummy; |
| 92 | }; |
| 93 | bool log_exec; /* print handler execution */ |
| 94 | bool log_error; /* print in case of error status */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * Get handler ID from FID |
| 99 | * Precondition: FID is an RMI call |
| 100 | */ |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 101 | #define RMI_HANDLER_ID(_id) SMC64_FID_OFFSET_FROM_RANGE_MIN(RMI, _id) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 102 | |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 103 | #define HANDLER(_id, _in, _out, _fn, _exec, _error)[RMI_HANDLER_ID(SMC_RMM_##_id)] = { \ |
| 104 | .fn_name = #_id, \ |
| 105 | .type = RMI_TYPE(_in, _out), \ |
| 106 | .f_##_in##_out = _fn, \ |
| 107 | .log_exec = _exec, \ |
| 108 | .log_error = _error \ |
| 109 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * The 3rd value enables the execution log. |
| 113 | * The 4th value enables the error log. |
| 114 | */ |
| 115 | static const struct smc_handler smc_handlers[] = { |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 116 | HANDLER(VERSION, 0, 0, smc_version, true, true), |
| 117 | HANDLER(FEATURES, 1, 1, smc_read_feature_register, true, true), |
| 118 | HANDLER(GRANULE_DELEGATE, 1, 0, smc_granule_delegate, false, true), |
| 119 | HANDLER(GRANULE_UNDELEGATE, 1, 0, smc_granule_undelegate, false, true), |
| 120 | HANDLER(REALM_CREATE, 2, 0, smc_realm_create, true, true), |
| 121 | HANDLER(REALM_DESTROY, 1, 0, smc_realm_destroy, true, true), |
| 122 | HANDLER(REALM_ACTIVATE, 1, 0, smc_realm_activate, true, true), |
| 123 | HANDLER(REC_CREATE, 3, 0, smc_rec_create, true, true), |
| 124 | HANDLER(REC_DESTROY, 1, 0, smc_rec_destroy, true, true), |
| 125 | HANDLER(REC_ENTER, 2, 0, smc_rec_enter, false, true), |
| 126 | HANDLER(DATA_CREATE, 5, 0, smc_data_create, false, false), |
| 127 | HANDLER(DATA_CREATE_UNKNOWN, 3, 0, smc_data_create_unknown, false, false), |
| 128 | HANDLER(DATA_DESTROY, 2, 0, smc_data_destroy, false, true), |
| 129 | HANDLER(RTT_CREATE, 4, 0, smc_rtt_create, false, true), |
| 130 | HANDLER(RTT_DESTROY, 4, 0, smc_rtt_destroy, false, true), |
| 131 | HANDLER(RTT_FOLD, 4, 0, smc_rtt_fold, false, true), |
| 132 | HANDLER(RTT_MAP_UNPROTECTED, 4, 0, smc_rtt_map_unprotected, false, false), |
| 133 | HANDLER(RTT_UNMAP_UNPROTECTED, 3, 0, smc_rtt_unmap_unprotected, false, false), |
| 134 | HANDLER(RTT_READ_ENTRY, 3, 4, smc_rtt_read_entry, false, true), |
| 135 | HANDLER(PSCI_COMPLETE, 2, 0, smc_psci_complete, true, true), |
| 136 | HANDLER(REC_AUX_COUNT, 1, 1, smc_rec_aux_count, true, true), |
| 137 | HANDLER(RTT_INIT_RIPAS, 3, 0, smc_rtt_init_ripas, false, true), |
| 138 | HANDLER(RTT_SET_RIPAS, 5, 0, smc_rtt_set_ripas, false, true) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | COMPILER_ASSERT(ARRAY_LEN(smc_handlers) == SMC64_NUM_FIDS_IN_RANGE(RMI)); |
| 142 | |
| 143 | static bool rmi_call_log_enabled = true; |
| 144 | |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame^] | 145 | static inline bool rmi_handler_needs_fpu(unsigned long id) |
| 146 | { |
| 147 | #ifdef RMM_FPU_USE_AT_REL2 |
| 148 | if (id == SMC_RMM_REALM_CREATE || id == SMC_RMM_DATA_CREATE || |
| 149 | id == SMC_RMM_REC_CREATE || id == SMC_RMM_RTT_INIT_RIPAS) { |
| 150 | return true; |
| 151 | } |
| 152 | #endif |
| 153 | return false; |
| 154 | } |
| 155 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 156 | static void rmi_log_on_exit(unsigned long handler_id, |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 157 | unsigned long args[], |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 158 | struct smc_result *ret) |
| 159 | { |
| 160 | const struct smc_handler *handler = &smc_handlers[handler_id]; |
| 161 | unsigned long function_id = SMC64_RMI_FID(handler_id); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 162 | return_code_t rc; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 163 | unsigned int num; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 164 | |
| 165 | if (!handler->log_exec && !handler->log_error) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | if (function_id == SMC_RMM_VERSION) { |
| 170 | /* |
| 171 | * RMM_VERSION is special because it returns the |
| 172 | * version number, not the error code. |
| 173 | */ |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 174 | INFO("SMC_RMM_%-21s > %lx\n", handler->fn_name, ret->x[0]); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | rc = unpack_return_code(ret->x[0]); |
| 179 | |
| 180 | if ((handler->log_exec) || |
| 181 | (handler->log_error && (rc.status != RMI_SUCCESS))) { |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 182 | /* Print function name */ |
| 183 | INFO("SMC_RMM_%-21s", handler->fn_name); |
| 184 | |
| 185 | /* Print arguments */ |
| 186 | num = handler->type & 0xFF; |
| 187 | assert(num <= MAX_NUM_ARGS); |
| 188 | |
| 189 | for (unsigned int i = 0U; i < num; i++) { |
| 190 | INFO(" %lx", args[i]); |
| 191 | } |
| 192 | |
| 193 | /* Print status */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 194 | if (rc.status >= RMI_ERROR_COUNT) { |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 195 | INFO(" > %lx", ret->x[0]); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 196 | } else { |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 197 | INFO(" > RMI_%s", rmi_status_string[rc.status]); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* Check for index */ |
| 201 | if (((function_id == SMC_RMM_REC_ENTER) && |
| 202 | (rc.status == RMI_ERROR_REALM)) || |
| 203 | (rc.status == RMI_ERROR_RTT)) { |
| 204 | INFO(" %x", rc.index); |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 205 | } else if (rc.status == RMI_SUCCESS) { |
| 206 | /* Print output values */ |
| 207 | num = (handler->type >> 8) & 0xFF; |
| 208 | assert(num <= MAX_NUM_OUTPUT_VALS); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 209 | |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 210 | for (unsigned int i = 1U; i <= num; i++) { |
| 211 | INFO(" %lx", ret->x[i]); |
| 212 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 213 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 214 | INFO("\n"); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void handle_ns_smc(unsigned long function_id, |
| 219 | unsigned long arg0, |
| 220 | unsigned long arg1, |
| 221 | unsigned long arg2, |
| 222 | unsigned long arg3, |
| 223 | unsigned long arg4, |
| 224 | unsigned long arg5, |
| 225 | struct smc_result *ret) |
| 226 | { |
| 227 | unsigned long handler_id; |
| 228 | const struct smc_handler *handler = NULL; |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame^] | 229 | bool restore_ns_simd_state = false; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 230 | |
Arunachalam Ganapathy | 937b549 | 2023-02-28 11:17:52 +0000 | [diff] [blame] | 231 | /* Ignore SVE hint bit, until RMM supports SVE hint bit */ |
| 232 | function_id &= ~MASK(SMC_SVE_HINT); |
| 233 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 234 | if (IS_SMC64_RMI_FID(function_id)) { |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 235 | handler_id = RMI_HANDLER_ID(function_id); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 236 | if (handler_id < ARRAY_LEN(smc_handlers)) { |
| 237 | handler = &smc_handlers[handler_id]; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Check if handler exists and 'fn_dummy' is not NULL |
| 243 | * for not implemented 'function_id' calls in SMC RMI range. |
| 244 | */ |
| 245 | if ((handler == NULL) || (handler->fn_dummy == NULL)) { |
| 246 | VERBOSE("[%s] unknown function_id: %lx\n", |
| 247 | __func__, function_id); |
| 248 | ret->x[0] = SMC_UNKNOWN; |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | assert_cpu_slots_empty(); |
| 253 | |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame^] | 254 | /* Current CPU's SIMD state must not be saved when entering RMM */ |
| 255 | assert(simd_is_state_saved() == false); |
| 256 | |
| 257 | /* If the handler needs FPU, actively save NS simd context. */ |
| 258 | if (rmi_handler_needs_fpu(function_id) == true) { |
| 259 | simd_save_ns_state(); |
| 260 | restore_ns_simd_state = true; |
| 261 | } |
| 262 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 263 | switch (handler->type) { |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 264 | case rmi_type_00: |
| 265 | ret->x[0] = handler->f_00(); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 266 | break; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 267 | case rmi_type_10: |
| 268 | ret->x[0] = handler->f_10(arg0); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 269 | break; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 270 | case rmi_type_20: |
| 271 | ret->x[0] = handler->f_20(arg0, arg1); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 272 | break; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 273 | case rmi_type_30: |
| 274 | ret->x[0] = handler->f_30(arg0, arg1, arg2); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 275 | break; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 276 | case rmi_type_40: |
| 277 | ret->x[0] = handler->f_40(arg0, arg1, arg2, arg3); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 278 | break; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 279 | case rmi_type_50: |
| 280 | ret->x[0] = handler->f_50(arg0, arg1, arg2, arg3, arg4); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 281 | break; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 282 | case rmi_type_11: |
| 283 | handler->f_11(arg0, ret); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 284 | break; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 285 | case rmi_type_34: |
| 286 | handler->f_34(arg0, arg1, arg2, ret); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 287 | break; |
| 288 | default: |
| 289 | assert(false); |
| 290 | } |
| 291 | |
| 292 | if (rmi_call_log_enabled) { |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 293 | unsigned long args[] = {arg0, arg1, arg2, arg3, arg4}; |
| 294 | |
| 295 | rmi_log_on_exit(handler_id, args, ret); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame^] | 298 | /* If the handler uses FPU, restore the saved NS simd context. */ |
| 299 | if (restore_ns_simd_state) { |
| 300 | simd_restore_ns_state(); |
| 301 | } |
| 302 | |
| 303 | /* Current CPU's SIMD state must not be saved when exiting RMM */ |
| 304 | assert(simd_is_state_saved() == false); |
| 305 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 306 | assert_cpu_slots_empty(); |
| 307 | } |
| 308 | |
| 309 | static void report_unexpected(void) |
| 310 | { |
| 311 | unsigned long spsr = read_spsr_el2(); |
| 312 | unsigned long esr = read_esr_el2(); |
| 313 | unsigned long elr = read_elr_el2(); |
| 314 | unsigned long far = read_far_el2(); |
| 315 | |
| 316 | INFO("----\n"); |
| 317 | INFO("Unexpected exception:\n"); |
| 318 | INFO("SPSR_EL2: 0x%016lx\n", spsr); |
| 319 | INFO("ESR_EL2: 0x%016lx\n", esr); |
| 320 | INFO("ELR_EL2: 0x%016lx\n", elr); |
| 321 | INFO("FAR_EL2: 0x%016lx\n", far); |
| 322 | INFO("----\n"); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | unsigned long handle_realm_trap(unsigned long *regs) |
| 326 | { |
| 327 | report_unexpected(); |
| 328 | |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 329 | while (true) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 330 | wfe(); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Identifies an abort that the RMM may recover from. |
| 336 | */ |
| 337 | struct rmm_trap_element { |
| 338 | /* |
| 339 | * The PC at the time of abort. |
| 340 | */ |
| 341 | unsigned long aborted_pc; |
| 342 | /* |
| 343 | * New value of the PC. |
| 344 | */ |
| 345 | unsigned long new_pc; |
| 346 | }; |
| 347 | |
| 348 | #define RMM_TRAP_HANDLER(_aborted_pc, _new_pc) \ |
| 349 | { .aborted_pc = (unsigned long)(&_aborted_pc), \ |
| 350 | .new_pc = (unsigned long)(&_new_pc) } |
| 351 | |
| 352 | /* |
| 353 | * The registered locations of load/store instructions that access NS memory. |
| 354 | */ |
| 355 | extern void *ns_read; |
| 356 | extern void *ns_write; |
| 357 | |
| 358 | /* |
| 359 | * The new value of the PC when the GPF occurs on a registered location. |
| 360 | */ |
| 361 | extern void *ns_access_ret_0; |
| 362 | |
| 363 | struct rmm_trap_element rmm_trap_list[] = { |
| 364 | RMM_TRAP_HANDLER(ns_read, ns_access_ret_0), |
| 365 | RMM_TRAP_HANDLER(ns_write, ns_access_ret_0), |
| 366 | }; |
| 367 | #define RMM_TRAP_LIST_SIZE (sizeof(rmm_trap_list)/sizeof(struct rmm_trap_element)) |
| 368 | |
| 369 | static void fatal_abort(void) |
| 370 | { |
| 371 | report_unexpected(); |
| 372 | |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 373 | while (true) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 374 | wfe(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | static bool is_el2_data_abort_gpf(unsigned long esr) |
| 379 | { |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 380 | if (((esr & MASK(ESR_EL2_EC)) == ESR_EL2_EC_DATA_ABORT_SEL) && |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 381 | ((esr & MASK(ESR_EL2_ABORT_FSC)) == ESR_EL2_ABORT_FSC_GPF)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 382 | return true; |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 383 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Handles the RMM's aborts. |
| 389 | * It compares the PC at the time of the abort with the registered addresses. |
| 390 | * If it finds a match, it returns the new value of the PC that the RMM should |
| 391 | * continue from. Other register values are preserved. |
| 392 | * If no match is found, it aborts the RMM. |
| 393 | */ |
| 394 | unsigned long handle_rmm_trap(void) |
| 395 | { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 396 | unsigned long esr = read_esr_el2(); |
| 397 | unsigned long elr = read_elr_el2(); |
| 398 | |
| 399 | /* |
| 400 | * Only the GPF data aborts are recoverable. |
| 401 | */ |
| 402 | if (!is_el2_data_abort_gpf(esr)) { |
| 403 | fatal_abort(); |
| 404 | } |
| 405 | |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 406 | for (unsigned int i = 0U; i < RMM_TRAP_LIST_SIZE; i++) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 407 | if (rmm_trap_list[i].aborted_pc == elr) { |
| 408 | return rmm_trap_list[i].new_pc; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | fatal_abort(); |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 413 | return 0UL; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 414 | } |