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> |
| 11 | #include <sizes.h> |
| 12 | #include <smc-handler.h> |
| 13 | #include <smc-rmi.h> |
| 14 | #include <smc.h> |
| 15 | #include <status.h> |
| 16 | #include <utils_def.h> |
| 17 | |
| 18 | #define STATUS_HANDLER(_id)[_id] = #_id |
| 19 | |
| 20 | const char *status_handler[] = { |
| 21 | STATUS_HANDLER(RMI_SUCCESS), |
| 22 | STATUS_HANDLER(RMI_ERROR_INPUT), |
| 23 | STATUS_HANDLER(RMI_ERROR_REALM), |
| 24 | STATUS_HANDLER(RMI_ERROR_REC), |
| 25 | STATUS_HANDLER(RMI_ERROR_RTT), |
| 26 | STATUS_HANDLER(RMI_ERROR_IN_USE) |
| 27 | }; |
| 28 | COMPILER_ASSERT(ARRAY_LEN(status_handler) == RMI_ERROR_COUNT); |
| 29 | |
| 30 | /* |
| 31 | * At this level (in handle_ns_smc) we distinguish the RMI calls only on: |
| 32 | * - The number of input arguments [0..4], and whether |
| 33 | * - The function returns up to three output values in addition |
| 34 | * to the return status code. |
| 35 | * Hence, the naming syntax is: |
| 36 | * - `*_[0..4]` when no output values are returned, and |
| 37 | * - `*_[0..4]_o` when the function returns some output values. |
| 38 | */ |
| 39 | |
| 40 | typedef unsigned long (*handler_0)(void); |
| 41 | typedef unsigned long (*handler_1)(unsigned long arg0); |
| 42 | typedef unsigned long (*handler_2)(unsigned long arg0, unsigned long arg1); |
| 43 | typedef unsigned long (*handler_3)(unsigned long arg0, unsigned long arg1, |
| 44 | unsigned long arg2); |
| 45 | typedef unsigned long (*handler_4)(unsigned long arg0, unsigned long arg1, |
| 46 | unsigned long arg2, unsigned long arg3); |
| 47 | typedef unsigned long (*handler_5)(unsigned long arg0, unsigned long arg1, |
| 48 | unsigned long arg2, unsigned long arg3, |
| 49 | unsigned long arg4); |
| 50 | typedef void (*handler_1_o)(unsigned long arg0, struct smc_result *ret); |
| 51 | typedef void (*handler_3_o)(unsigned long arg0, unsigned long arg1, |
| 52 | unsigned long arg2, struct smc_result *ret); |
| 53 | |
| 54 | enum rmi_type { |
| 55 | rmi_type_0, |
| 56 | rmi_type_1, |
| 57 | rmi_type_2, |
| 58 | rmi_type_3, |
| 59 | rmi_type_4, |
| 60 | rmi_type_5, |
| 61 | rmi_type_1_o, |
| 62 | rmi_type_3_o |
| 63 | }; |
| 64 | |
| 65 | struct smc_handler { |
| 66 | const char *fn_name; |
| 67 | enum rmi_type type; |
| 68 | union { |
| 69 | handler_0 f0; |
| 70 | handler_1 f1; |
| 71 | handler_2 f2; |
| 72 | handler_3 f3; |
| 73 | handler_4 f4; |
| 74 | handler_5 f5; |
| 75 | handler_1_o f1_o; |
| 76 | handler_3_o f3_o; |
| 77 | void *fn_dummy; |
| 78 | }; |
| 79 | bool log_exec; /* print handler execution */ |
| 80 | bool log_error; /* print in case of error status */ |
| 81 | unsigned int out_values; /* number of output values */ |
| 82 | }; |
| 83 | |
| 84 | /* |
| 85 | * Get handler ID from FID |
| 86 | * Precondition: FID is an RMI call |
| 87 | */ |
| 88 | #define SMC_RMI_HANDLER_ID(_fid) SMC64_FID_OFFSET_FROM_RANGE_MIN(RMI, _fid) |
| 89 | |
| 90 | #define HANDLER_0(_id, _fn, _exec, _error)[SMC_RMI_HANDLER_ID(_id)] = { \ |
| 91 | .fn_name = #_id, \ |
| 92 | .type = rmi_type_0, .f0 = _fn, .log_exec = _exec, .log_error = _error, \ |
| 93 | .out_values = 0U } |
| 94 | #define HANDLER_1(_id, _fn, _exec, _error)[SMC_RMI_HANDLER_ID(_id)] = { \ |
| 95 | .fn_name = #_id, \ |
| 96 | .type = rmi_type_1, .f1 = _fn, .log_exec = _exec, .log_error = _error, \ |
| 97 | .out_values = 0U } |
| 98 | #define HANDLER_2(_id, _fn, _exec, _error)[SMC_RMI_HANDLER_ID(_id)] = { \ |
| 99 | .fn_name = #_id, \ |
| 100 | .type = rmi_type_2, .f2 = _fn, .log_exec = _exec, .log_error = _error, \ |
| 101 | .out_values = 0U } |
| 102 | #define HANDLER_3(_id, _fn, _exec, _error)[SMC_RMI_HANDLER_ID(_id)] = { \ |
| 103 | .fn_name = #_id, \ |
| 104 | .type = rmi_type_3, .f3 = _fn, .log_exec = _exec, .log_error = _error, \ |
| 105 | .out_values = 0U } |
| 106 | #define HANDLER_4(_id, _fn, _exec, _error)[SMC_RMI_HANDLER_ID(_id)] = { \ |
| 107 | .fn_name = #_id, \ |
| 108 | .type = rmi_type_4, .f4 = _fn, .log_exec = _exec, .log_error = _error, \ |
| 109 | .out_values = 0U } |
| 110 | #define HANDLER_5(_id, _fn, _exec, _error)[SMC_RMI_HANDLER_ID(_id)] = { \ |
| 111 | .fn_name = #_id, \ |
| 112 | .type = rmi_type_5, .f5 = _fn, .log_exec = _exec, .log_error = _error, \ |
| 113 | .out_values = 0U } |
| 114 | #define HANDLER_1_O(_id, _fn, _exec, _error, _values)[SMC_RMI_HANDLER_ID(_id)] = { \ |
| 115 | .fn_name = #_id, \ |
| 116 | .type = rmi_type_1_o, .f1_o = _fn, .log_exec = _exec, .log_error = _error, \ |
| 117 | .out_values = _values } |
| 118 | #define HANDLER_3_O(_id, _fn, _exec, _error, _values)[SMC_RMI_HANDLER_ID(_id)] = { \ |
| 119 | .fn_name = #_id, \ |
| 120 | .type = rmi_type_3_o, .f3_o = _fn, .log_exec = _exec, .log_error = _error, \ |
| 121 | .out_values = _values } |
| 122 | |
| 123 | /* |
| 124 | * The 3rd value enables the execution log. |
| 125 | * The 4th value enables the error log. |
| 126 | */ |
| 127 | static const struct smc_handler smc_handlers[] = { |
| 128 | HANDLER_0(SMC_RMM_VERSION, smc_version, true, true), |
| 129 | HANDLER_1_O(SMC_RMM_FEATURES, smc_read_feature_register, true, true, 1U), |
| 130 | HANDLER_1(SMC_RMM_GRANULE_DELEGATE, smc_granule_delegate, false, true), |
| 131 | HANDLER_1(SMC_RMM_GRANULE_UNDELEGATE, smc_granule_undelegate, false, true), |
| 132 | HANDLER_2(SMC_RMM_REALM_CREATE, smc_realm_create, true, true), |
| 133 | HANDLER_1(SMC_RMM_REALM_DESTROY, smc_realm_destroy, true, true), |
| 134 | HANDLER_1(SMC_RMM_REALM_ACTIVATE, smc_realm_activate, true, true), |
| 135 | HANDLER_3(SMC_RMM_REC_CREATE, smc_rec_create, true, true), |
| 136 | HANDLER_1(SMC_RMM_REC_DESTROY, smc_rec_destroy, true, true), |
| 137 | HANDLER_2(SMC_RMM_REC_ENTER, smc_rec_enter, false, true), |
| 138 | HANDLER_5(SMC_RMM_DATA_CREATE, smc_data_create, false, false), |
| 139 | HANDLER_3(SMC_RMM_DATA_CREATE_UNKNOWN, smc_data_create_unknown, false, false), |
| 140 | HANDLER_2(SMC_RMM_DATA_DESTROY, smc_data_destroy, false, true), |
| 141 | HANDLER_4(SMC_RMM_RTT_CREATE, smc_rtt_create, false, true), |
| 142 | HANDLER_4(SMC_RMM_RTT_DESTROY, smc_rtt_destroy, false, true), |
| 143 | HANDLER_4(SMC_RMM_RTT_FOLD, smc_rtt_fold, false, true), |
| 144 | HANDLER_4(SMC_RMM_RTT_MAP_UNPROTECTED, smc_rtt_map_unprotected, false, false), |
| 145 | HANDLER_3(SMC_RMM_RTT_UNMAP_UNPROTECTED, smc_rtt_unmap_unprotected, false, false), |
| 146 | HANDLER_3_O(SMC_RMM_RTT_READ_ENTRY, smc_rtt_read_entry, false, true, 4U), |
| 147 | HANDLER_2(SMC_RMM_PSCI_COMPLETE, smc_psci_complete, true, true), |
| 148 | HANDLER_1_O(SMC_RMM_REC_AUX_COUNT, smc_rec_aux_count, true, true, 1U), |
| 149 | HANDLER_3(SMC_RMM_RTT_INIT_RIPAS, smc_rtt_init_ripas, false, true), |
| 150 | HANDLER_5(SMC_RMM_RTT_SET_RIPAS, smc_rtt_set_ripas, false, true) |
| 151 | }; |
| 152 | |
| 153 | COMPILER_ASSERT(ARRAY_LEN(smc_handlers) == SMC64_NUM_FIDS_IN_RANGE(RMI)); |
| 154 | |
| 155 | static bool rmi_call_log_enabled = true; |
| 156 | |
| 157 | static void rmi_log_on_exit(unsigned long handler_id, |
| 158 | unsigned long arg0, |
| 159 | unsigned long arg1, |
| 160 | unsigned long arg2, |
| 161 | unsigned long arg3, |
| 162 | unsigned long arg4, |
| 163 | struct smc_result *ret) |
| 164 | { |
| 165 | const struct smc_handler *handler = &smc_handlers[handler_id]; |
| 166 | unsigned long function_id = SMC64_RMI_FID(handler_id); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 167 | return_code_t rc; |
| 168 | |
| 169 | if (!handler->log_exec && !handler->log_error) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (function_id == SMC_RMM_VERSION) { |
| 174 | /* |
| 175 | * RMM_VERSION is special because it returns the |
| 176 | * version number, not the error code. |
| 177 | */ |
| 178 | INFO("%-29s %8lx %8lx %8lx %8lx %8lx > %lx\n", |
| 179 | handler->fn_name, arg0, arg1, arg2, arg3, arg4, |
| 180 | ret->x[0]); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | rc = unpack_return_code(ret->x[0]); |
| 185 | |
| 186 | if ((handler->log_exec) || |
| 187 | (handler->log_error && (rc.status != RMI_SUCCESS))) { |
| 188 | INFO("%-29s %8lx %8lx %8lx %8lx %8lx > ", |
| 189 | handler->fn_name, arg0, arg1, arg2, arg3, arg4); |
| 190 | if (rc.status >= RMI_ERROR_COUNT) { |
| 191 | INFO("%lx", ret->x[0]); |
| 192 | } else { |
| 193 | INFO("%s", status_handler[rc.status]); |
| 194 | } |
| 195 | |
| 196 | /* Check for index */ |
| 197 | if (((function_id == SMC_RMM_REC_ENTER) && |
| 198 | (rc.status == RMI_ERROR_REALM)) || |
| 199 | (rc.status == RMI_ERROR_RTT)) { |
| 200 | INFO(" %x", rc.index); |
| 201 | } |
| 202 | |
| 203 | /* Print output values */ |
Shruti Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 204 | for (unsigned int i = 1U; i <= handler->out_values; i++) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 205 | INFO(" %8lx", ret->x[i]); |
| 206 | } |
| 207 | |
| 208 | INFO("\n"); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void handle_ns_smc(unsigned long function_id, |
| 213 | unsigned long arg0, |
| 214 | unsigned long arg1, |
| 215 | unsigned long arg2, |
| 216 | unsigned long arg3, |
| 217 | unsigned long arg4, |
| 218 | unsigned long arg5, |
| 219 | struct smc_result *ret) |
| 220 | { |
| 221 | unsigned long handler_id; |
| 222 | const struct smc_handler *handler = NULL; |
| 223 | |
| 224 | if (IS_SMC64_RMI_FID(function_id)) { |
| 225 | handler_id = SMC_RMI_HANDLER_ID(function_id); |
| 226 | if (handler_id < ARRAY_LEN(smc_handlers)) { |
| 227 | handler = &smc_handlers[handler_id]; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Check if handler exists and 'fn_dummy' is not NULL |
| 233 | * for not implemented 'function_id' calls in SMC RMI range. |
| 234 | */ |
| 235 | if ((handler == NULL) || (handler->fn_dummy == NULL)) { |
| 236 | VERBOSE("[%s] unknown function_id: %lx\n", |
| 237 | __func__, function_id); |
| 238 | ret->x[0] = SMC_UNKNOWN; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | assert_cpu_slots_empty(); |
| 243 | |
| 244 | switch (handler->type) { |
| 245 | case rmi_type_0: |
| 246 | ret->x[0] = handler->f0(); |
| 247 | break; |
| 248 | case rmi_type_1: |
| 249 | ret->x[0] = handler->f1(arg0); |
| 250 | break; |
| 251 | case rmi_type_2: |
| 252 | ret->x[0] = handler->f2(arg0, arg1); |
| 253 | break; |
| 254 | case rmi_type_3: |
| 255 | ret->x[0] = handler->f3(arg0, arg1, arg2); |
| 256 | break; |
| 257 | case rmi_type_4: |
| 258 | ret->x[0] = handler->f4(arg0, arg1, arg2, arg3); |
| 259 | break; |
| 260 | case rmi_type_5: |
| 261 | ret->x[0] = handler->f5(arg0, arg1, arg2, arg3, arg4); |
| 262 | break; |
| 263 | case rmi_type_1_o: |
| 264 | handler->f1_o(arg0, ret); |
| 265 | break; |
| 266 | case rmi_type_3_o: |
| 267 | handler->f3_o(arg0, arg1, arg2, ret); |
| 268 | break; |
| 269 | default: |
| 270 | assert(false); |
| 271 | } |
| 272 | |
| 273 | if (rmi_call_log_enabled) { |
| 274 | rmi_log_on_exit(handler_id, arg0, arg1, arg2, arg3, arg4, ret); |
| 275 | } |
| 276 | |
| 277 | assert_cpu_slots_empty(); |
| 278 | } |
| 279 | |
| 280 | static void report_unexpected(void) |
| 281 | { |
| 282 | unsigned long spsr = read_spsr_el2(); |
| 283 | unsigned long esr = read_esr_el2(); |
| 284 | unsigned long elr = read_elr_el2(); |
| 285 | unsigned long far = read_far_el2(); |
| 286 | |
| 287 | INFO("----\n"); |
| 288 | INFO("Unexpected exception:\n"); |
| 289 | INFO("SPSR_EL2: 0x%016lx\n", spsr); |
| 290 | INFO("ESR_EL2: 0x%016lx\n", esr); |
| 291 | INFO("ELR_EL2: 0x%016lx\n", elr); |
| 292 | INFO("FAR_EL2: 0x%016lx\n", far); |
| 293 | INFO("----\n"); |
| 294 | |
| 295 | } |
| 296 | |
| 297 | unsigned long handle_realm_trap(unsigned long *regs) |
| 298 | { |
| 299 | report_unexpected(); |
| 300 | |
| 301 | while (1) { |
| 302 | wfe(); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Identifies an abort that the RMM may recover from. |
| 308 | */ |
| 309 | struct rmm_trap_element { |
| 310 | /* |
| 311 | * The PC at the time of abort. |
| 312 | */ |
| 313 | unsigned long aborted_pc; |
| 314 | /* |
| 315 | * New value of the PC. |
| 316 | */ |
| 317 | unsigned long new_pc; |
| 318 | }; |
| 319 | |
| 320 | #define RMM_TRAP_HANDLER(_aborted_pc, _new_pc) \ |
| 321 | { .aborted_pc = (unsigned long)(&_aborted_pc), \ |
| 322 | .new_pc = (unsigned long)(&_new_pc) } |
| 323 | |
| 324 | /* |
| 325 | * The registered locations of load/store instructions that access NS memory. |
| 326 | */ |
| 327 | extern void *ns_read; |
| 328 | extern void *ns_write; |
| 329 | |
| 330 | /* |
| 331 | * The new value of the PC when the GPF occurs on a registered location. |
| 332 | */ |
| 333 | extern void *ns_access_ret_0; |
| 334 | |
| 335 | struct rmm_trap_element rmm_trap_list[] = { |
| 336 | RMM_TRAP_HANDLER(ns_read, ns_access_ret_0), |
| 337 | RMM_TRAP_HANDLER(ns_write, ns_access_ret_0), |
| 338 | }; |
| 339 | #define RMM_TRAP_LIST_SIZE (sizeof(rmm_trap_list)/sizeof(struct rmm_trap_element)) |
| 340 | |
| 341 | static void fatal_abort(void) |
| 342 | { |
| 343 | report_unexpected(); |
| 344 | |
| 345 | while (1) { |
| 346 | wfe(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static bool is_el2_data_abort_gpf(unsigned long esr) |
| 351 | { |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame^] | 352 | if (((esr & MASK(ESR_EL2_EC)) == ESR_EL2_EC_DATA_ABORT_SEL) && |
| 353 | ((esr & MASK(ESR_EL2_ABORT_FSC)) == ESR_EL2_ABORT_FSC_GPF)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 354 | return true; |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Handles the RMM's aborts. |
| 360 | * It compares the PC at the time of the abort with the registered addresses. |
| 361 | * If it finds a match, it returns the new value of the PC that the RMM should |
| 362 | * continue from. Other register values are preserved. |
| 363 | * If no match is found, it aborts the RMM. |
| 364 | */ |
| 365 | unsigned long handle_rmm_trap(void) |
| 366 | { |
| 367 | int i; |
| 368 | |
| 369 | unsigned long esr = read_esr_el2(); |
| 370 | unsigned long elr = read_elr_el2(); |
| 371 | |
| 372 | /* |
| 373 | * Only the GPF data aborts are recoverable. |
| 374 | */ |
| 375 | if (!is_el2_data_abort_gpf(esr)) { |
| 376 | fatal_abort(); |
| 377 | } |
| 378 | |
| 379 | for (i = 0; i < RMM_TRAP_LIST_SIZE; i++) { |
| 380 | if (rmm_trap_list[i].aborted_pc == elr) { |
| 381 | return rmm_trap_list[i].new_pc; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | fatal_abort(); |
| 386 | return 0; |
| 387 | } |