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