blob: 3dbff55a7f215bf24302392dfe4e7595fcc900f1 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
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 Ganapathy4f601e72023-05-22 11:49:29 +010011#include <run.h>
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000012#include <simd.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000013#include <smc-handler.h>
14#include <smc-rmi.h>
15#include <smc.h>
16#include <status.h>
17#include <utils_def.h>
18
AlexeiFedorov6c119692023-04-21 12:31:15 +010019/* Maximum number of supported arguments */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010020#define MAX_NUM_ARGS 5U
Soby Mathewb4c6df42022-11-09 11:13:29 +000021
AlexeiFedorov6c119692023-04-21 12:31:15 +010022/* Maximum number of output values */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010023#define MAX_NUM_OUTPUT_VALS 4U
AlexeiFedorov6c119692023-04-21 12:31:15 +010024
25#define RMI_STATUS_STRING(_id)[RMI_##_id] = #_id
26
AlexeiFedorov04418f42023-09-13 10:50:33 +010027static const char * const rmi_status_string[] = {
AlexeiFedorov6c119692023-04-21 12:31:15 +010028 RMI_STATUS_STRING(SUCCESS),
29 RMI_STATUS_STRING(ERROR_INPUT),
30 RMI_STATUS_STRING(ERROR_REALM),
31 RMI_STATUS_STRING(ERROR_REC),
AlexeiFedorov892abce2023-04-06 16:32:12 +010032 RMI_STATUS_STRING(ERROR_RTT)
Soby Mathewb4c6df42022-11-09 11:13:29 +000033};
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010034
AlexeiFedorov6c119692023-04-21 12:31:15 +010035COMPILER_ASSERT(ARRAY_LEN(rmi_status_string) == RMI_ERROR_COUNT);
Soby Mathewb4c6df42022-11-09 11:13:29 +000036
37/*
38 * At this level (in handle_ns_smc) we distinguish the RMI calls only on:
AlexeiFedorov6c119692023-04-21 12:31:15 +010039 * - The number of input arguments [0..5], and whether
Soby Mathewb4c6df42022-11-09 11:13:29 +000040 * - The function returns up to three output values in addition
41 * to the return status code.
42 * Hence, the naming syntax is:
AlexeiFedorov6c119692023-04-21 12:31:15 +010043 * - `*_[0..5]` when no output values are returned, and
44 * - `*_[0..3]_o` when the function returns some output values.
Soby Mathewb4c6df42022-11-09 11:13:29 +000045 */
Soby Mathewb4c6df42022-11-09 11:13:29 +000046typedef unsigned long (*handler_0)(void);
47typedef unsigned long (*handler_1)(unsigned long arg0);
48typedef unsigned long (*handler_2)(unsigned long arg0, unsigned long arg1);
49typedef unsigned long (*handler_3)(unsigned long arg0, unsigned long arg1,
50 unsigned long arg2);
51typedef unsigned long (*handler_4)(unsigned long arg0, unsigned long arg1,
52 unsigned long arg2, unsigned long arg3);
53typedef unsigned long (*handler_5)(unsigned long arg0, unsigned long arg1,
54 unsigned long arg2, unsigned long arg3,
55 unsigned long arg4);
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010056typedef void (*handler_1_o)(unsigned long arg0, struct smc_result *res);
AlexeiFedorov892abce2023-04-06 16:32:12 +010057typedef void (*handler_2_o)(unsigned long arg0, unsigned long arg1,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010058 struct smc_result *res);
Soby Mathewb4c6df42022-11-09 11:13:29 +000059typedef void (*handler_3_o)(unsigned long arg0, unsigned long arg1,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010060 unsigned long arg2, struct smc_result *res);
AlexeiFedorov892abce2023-04-06 16:32:12 +010061typedef void (*handler_4_o)(unsigned long arg0, unsigned long arg1,
62 unsigned long arg2, unsigned long arg3,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010063 struct smc_result *res);
Soby Mathewb4c6df42022-11-09 11:13:29 +000064
AlexeiFedorov6c119692023-04-21 12:31:15 +010065/*
66 * SMC RMI handler type encoding:
67 * [0:7] - number of arguments
68 * [8:15] - number of output values
69 */
Chuyue Luod7386682023-09-22 11:55:47 +010070#define RMI_TYPE(_in, _out) ((_in) | ((_out) << 8))
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010071#define set_rmi_type(_in, _out) rmi_type_##_in##_out = RMI_TYPE(_in, _out)
AlexeiFedorov6c119692023-04-21 12:31:15 +010072
Soby Mathewb4c6df42022-11-09 11:13:29 +000073enum rmi_type {
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010074 set_rmi_type(0, 0), /* 0 arguments, 0 output values */
75 set_rmi_type(1, 0), /* 1 argument, 0 output values */
76 set_rmi_type(2, 0), /* 2 arguments, 0 output values */
77 set_rmi_type(3, 0), /* 3 arguments, 0 output values */
78 set_rmi_type(4, 0), /* 4 arguments, 0 output values */
79 set_rmi_type(5, 0), /* 5 arguments, 0 output values */
80 set_rmi_type(1, 1), /* 1 argument, 1 output value */
81 set_rmi_type(2, 2), /* 2 arguments, 2 output values */
82 set_rmi_type(3, 1), /* 3 arguments, 1 output value */
83 set_rmi_type(3, 2), /* 3 arguments, 2 output values */
84 set_rmi_type(3, 4), /* 3 arguments, 4 output values */
85 set_rmi_type(4, 1) /* 4 arguments, 1 output value */
Soby Mathewb4c6df42022-11-09 11:13:29 +000086};
87
88struct smc_handler {
89 const char *fn_name;
90 enum rmi_type type;
91 union {
AlexeiFedorov6c119692023-04-21 12:31:15 +010092 handler_0 f_00;
93 handler_1 f_10;
94 handler_2 f_20;
95 handler_3 f_30;
96 handler_4 f_40;
97 handler_5 f_50;
98 handler_1_o f_11;
AlexeiFedorov892abce2023-04-06 16:32:12 +010099 handler_2_o f_22;
100 handler_3_o f_31;
101 handler_3_o f_32;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100102 handler_3_o f_34;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100103 handler_4_o f_41;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000104 void *fn_dummy;
105 };
106 bool log_exec; /* print handler execution */
107 bool log_error; /* print in case of error status */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000108};
109
110/*
111 * Get handler ID from FID
112 * Precondition: FID is an RMI call
113 */
AlexeiFedorov6c119692023-04-21 12:31:15 +0100114#define RMI_HANDLER_ID(_id) SMC64_FID_OFFSET_FROM_RANGE_MIN(RMI, _id)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000115
AlexeiFedorov6c119692023-04-21 12:31:15 +0100116#define HANDLER(_id, _in, _out, _fn, _exec, _error)[RMI_HANDLER_ID(SMC_RMM_##_id)] = { \
Chuyue Luod7386682023-09-22 11:55:47 +0100117 .fn_name = (#_id), \
AlexeiFedorov6c119692023-04-21 12:31:15 +0100118 .type = RMI_TYPE(_in, _out), \
Chuyue Luod7386682023-09-22 11:55:47 +0100119 .f_##_in##_out = (_fn), \
120 .log_exec = (_exec), \
121 .log_error = (_error) \
AlexeiFedorov6c119692023-04-21 12:31:15 +0100122}
Soby Mathewb4c6df42022-11-09 11:13:29 +0000123
124/*
125 * The 3rd value enables the execution log.
126 * The 4th value enables the error log.
127 */
128static const struct smc_handler smc_handlers[] = {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100129 HANDLER(VERSION, 0, 0, smc_version, true, true),
130 HANDLER(FEATURES, 1, 1, smc_read_feature_register, true, true),
131 HANDLER(GRANULE_DELEGATE, 1, 0, smc_granule_delegate, false, true),
132 HANDLER(GRANULE_UNDELEGATE, 1, 0, smc_granule_undelegate, false, true),
133 HANDLER(REALM_CREATE, 2, 0, smc_realm_create, true, true),
134 HANDLER(REALM_DESTROY, 1, 0, smc_realm_destroy, true, true),
135 HANDLER(REALM_ACTIVATE, 1, 0, smc_realm_activate, true, true),
136 HANDLER(REC_CREATE, 3, 0, smc_rec_create, true, true),
137 HANDLER(REC_DESTROY, 1, 0, smc_rec_destroy, true, true),
138 HANDLER(REC_ENTER, 2, 0, smc_rec_enter, false, true),
139 HANDLER(DATA_CREATE, 5, 0, smc_data_create, false, false),
140 HANDLER(DATA_CREATE_UNKNOWN, 3, 0, smc_data_create_unknown, false, false),
AlexeiFedorove2002be2023-04-19 17:20:12 +0100141 HANDLER(DATA_DESTROY, 2, 2, smc_data_destroy, false, true),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100142 HANDLER(RTT_CREATE, 4, 0, smc_rtt_create, false, true),
AlexeiFedorove2002be2023-04-19 17:20:12 +0100143 HANDLER(RTT_DESTROY, 3, 2, smc_rtt_destroy, false, true),
144 HANDLER(RTT_FOLD, 3, 1, smc_rtt_fold, false, true),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100145 HANDLER(RTT_MAP_UNPROTECTED, 4, 0, smc_rtt_map_unprotected, false, false),
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100146 HANDLER(RTT_UNMAP_UNPROTECTED, 3, 1, smc_rtt_unmap_unprotected, false, false),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100147 HANDLER(RTT_READ_ENTRY, 3, 4, smc_rtt_read_entry, false, true),
AlexeiFedorov120d7d02023-08-02 16:51:48 +0100148 HANDLER(PSCI_COMPLETE, 3, 0, smc_psci_complete, true, true),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100149 HANDLER(REC_AUX_COUNT, 1, 1, smc_rec_aux_count, true, true),
AlexeiFedorov960d1612023-04-25 13:23:39 +0100150 HANDLER(RTT_INIT_RIPAS, 3, 1, smc_rtt_init_ripas, false, true),
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +0100151 HANDLER(RTT_SET_RIPAS, 4, 1, smc_rtt_set_ripas, false, true)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000152};
153
154COMPILER_ASSERT(ARRAY_LEN(smc_handlers) == SMC64_NUM_FIDS_IN_RANGE(RMI));
155
156static bool rmi_call_log_enabled = true;
157
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100158static inline bool rmi_handler_needs_fpu(unsigned int id)
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000159{
160#ifdef RMM_FPU_USE_AT_REL2
161 if (id == SMC_RMM_REALM_CREATE || id == SMC_RMM_DATA_CREATE ||
162 id == SMC_RMM_REC_CREATE || id == SMC_RMM_RTT_INIT_RIPAS) {
163 return true;
164 }
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100165#else
166 (void)id;
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000167#endif
168 return false;
169}
170
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100171static void rmi_log_on_exit(unsigned int handler_id,
AlexeiFedorov6c119692023-04-21 12:31:15 +0100172 unsigned long args[],
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100173 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000174{
175 const struct smc_handler *handler = &smc_handlers[handler_id];
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100176 unsigned int function_id = SMC64_RMI_FID(handler_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000177 return_code_t rc;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100178 unsigned int num;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000179
180 if (!handler->log_exec && !handler->log_error) {
181 return;
182 }
183
184 if (function_id == SMC_RMM_VERSION) {
185 /*
186 * RMM_VERSION is special because it returns the
187 * version number, not the error code.
188 */
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100189 INFO("SMC_RMM_%-21s > %lx\n", handler->fn_name, res->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000190 return;
191 }
192
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100193 rc = unpack_return_code(res->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000194
195 if ((handler->log_exec) ||
196 (handler->log_error && (rc.status != RMI_SUCCESS))) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100197 /* Print function name */
198 INFO("SMC_RMM_%-21s", handler->fn_name);
199
200 /* Print arguments */
AlexeiFedorov84cba4f2023-08-25 13:45:48 +0100201 num = (unsigned int)handler->type & 0xFFU;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100202 assert(num <= MAX_NUM_ARGS);
203
204 for (unsigned int i = 0U; i < num; i++) {
205 INFO(" %lx", args[i]);
206 }
207
208 /* Print status */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000209 if (rc.status >= RMI_ERROR_COUNT) {
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100210 INFO(" > %lx", res->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000211 } else {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100212 INFO(" > RMI_%s", rmi_status_string[rc.status]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000213 }
214
215 /* Check for index */
216 if (((function_id == SMC_RMM_REC_ENTER) &&
217 (rc.status == RMI_ERROR_REALM)) ||
218 (rc.status == RMI_ERROR_RTT)) {
219 INFO(" %x", rc.index);
AlexeiFedorov697445b2023-04-25 15:27:57 +0100220 }
221
222 if ((rc.status == RMI_SUCCESS) ||
223 ((rc.status == RMI_ERROR_RTT) &&
224 ((function_id == SMC_RMM_RTT_DESTROY) ||
225 (function_id == SMC_RMM_DATA_DESTROY)))) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100226 /* Print output values */
AlexeiFedorov84cba4f2023-08-25 13:45:48 +0100227 num = ((unsigned int)handler->type >> 8) & 0xFFU;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100228 assert(num <= MAX_NUM_OUTPUT_VALS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000229
AlexeiFedorov6c119692023-04-21 12:31:15 +0100230 for (unsigned int i = 1U; i <= num; i++) {
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100231 INFO(" %lx", res->x[i]);
AlexeiFedorov6c119692023-04-21 12:31:15 +0100232 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000233 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000234 INFO("\n");
235 }
236}
237
AlexeiFedorovb0de4d52023-10-10 11:46:16 +0100238/* coverity[misra_c_2012_rule_8_7_violation:SUPPRESS] */
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100239void handle_ns_smc(unsigned int function_id,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000240 unsigned long arg0,
241 unsigned long arg1,
242 unsigned long arg2,
243 unsigned long arg3,
244 unsigned long arg4,
245 unsigned long arg5,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100246 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000247{
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100248 (void)arg5;
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100249 unsigned int handler_id;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000250 const struct smc_handler *handler = NULL;
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000251 bool restore_ns_simd_state = false;
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100252 struct simd_context *ns_simd_ctx;
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100253 bool sve_hint = false;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000254
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100255 /* Save the SVE hint bit and clear it from the function ID */
256 if ((function_id & SMC_SVE_HINT) != 0U) {
257 sve_hint = true;
258 function_id &= ~SMC_SVE_HINT;
259 }
Arunachalam Ganapathy937b5492023-02-28 11:17:52 +0000260
Soby Mathewb4c6df42022-11-09 11:13:29 +0000261 if (IS_SMC64_RMI_FID(function_id)) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100262 handler_id = RMI_HANDLER_ID(function_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000263 if (handler_id < ARRAY_LEN(smc_handlers)) {
264 handler = &smc_handlers[handler_id];
265 }
266 }
267
268 /*
269 * Check if handler exists and 'fn_dummy' is not NULL
270 * for not implemented 'function_id' calls in SMC RMI range.
271 */
272 if ((handler == NULL) || (handler->fn_dummy == NULL)) {
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100273 VERBOSE("[%s] unknown function_id: %x\n",
Soby Mathewb4c6df42022-11-09 11:13:29 +0000274 __func__, function_id);
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100275 res->x[0] = SMC_UNKNOWN;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000276 return;
277 }
278
279 assert_cpu_slots_empty();
280
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000281 /* Current CPU's SIMD state must not be saved when entering RMM */
282 assert(simd_is_state_saved() == false);
283
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100284 /* Get current CPU's NS SIMD context */
285 ns_simd_ctx = get_cpu_ns_simd_context();
286
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100287 /* Set or clear SVE hint bit in the NS SIMD context */
288 simd_update_smc_sve_hint(ns_simd_ctx, sve_hint);
289
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000290 /* If the handler needs FPU, actively save NS simd context. */
291 if (rmi_handler_needs_fpu(function_id) == true) {
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100292 simd_context_save(ns_simd_ctx);
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000293 restore_ns_simd_state = true;
294 }
295
Soby Mathewb4c6df42022-11-09 11:13:29 +0000296 switch (handler->type) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100297 case rmi_type_00:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100298 res->x[0] = handler->f_00();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000299 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100300 case rmi_type_10:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100301 res->x[0] = handler->f_10(arg0);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000302 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100303 case rmi_type_20:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100304 res->x[0] = handler->f_20(arg0, arg1);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000305 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100306 case rmi_type_30:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100307 res->x[0] = handler->f_30(arg0, arg1, arg2);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000308 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100309 case rmi_type_40:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100310 res->x[0] = handler->f_40(arg0, arg1, arg2, arg3);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000311 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100312 case rmi_type_50:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100313 res->x[0] = handler->f_50(arg0, arg1, arg2, arg3, arg4);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000314 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100315 case rmi_type_11:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100316 handler->f_11(arg0, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000317 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100318 case rmi_type_22:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100319 handler->f_22(arg0, arg1, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100320 break;
321 case rmi_type_31:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100322 handler->f_31(arg0, arg1, arg2, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100323 break;
324 case rmi_type_32:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100325 handler->f_32(arg0, arg1, arg2, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100326 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100327 case rmi_type_34:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100328 handler->f_34(arg0, arg1, arg2, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000329 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100330 case rmi_type_41:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100331 handler->f_41(arg0, arg1, arg2, arg3, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100332 break;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000333 default:
334 assert(false);
335 }
336
337 if (rmi_call_log_enabled) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100338 unsigned long args[] = {arg0, arg1, arg2, arg3, arg4};
339
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100340 rmi_log_on_exit(handler_id, args, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000341 }
342
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100343 /* If the handler uses FPU, restore the saved NS simd context. */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000344 if (restore_ns_simd_state) {
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100345 simd_context_restore(ns_simd_ctx);
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000346 }
347
348 /* Current CPU's SIMD state must not be saved when exiting RMM */
349 assert(simd_is_state_saved() == false);
350
Soby Mathewb4c6df42022-11-09 11:13:29 +0000351 assert_cpu_slots_empty();
352}
353
354static void report_unexpected(void)
355{
356 unsigned long spsr = read_spsr_el2();
357 unsigned long esr = read_esr_el2();
358 unsigned long elr = read_elr_el2();
359 unsigned long far = read_far_el2();
360
361 INFO("----\n");
362 INFO("Unexpected exception:\n");
363 INFO("SPSR_EL2: 0x%016lx\n", spsr);
364 INFO("ESR_EL2: 0x%016lx\n", esr);
365 INFO("ELR_EL2: 0x%016lx\n", elr);
366 INFO("FAR_EL2: 0x%016lx\n", far);
367 INFO("----\n");
Soby Mathewb4c6df42022-11-09 11:13:29 +0000368}
369
AlexeiFedorov57f40fc2023-08-17 14:33:33 +0100370__dead2 unsigned long handle_realm_trap(unsigned long *regs)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000371{
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100372 (void)regs;
373
Soby Mathewb4c6df42022-11-09 11:13:29 +0000374 report_unexpected();
375
AlexeiFedorov6c119692023-04-21 12:31:15 +0100376 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000377 wfe();
378 }
379}
380
381/*
382 * Identifies an abort that the RMM may recover from.
383 */
384struct rmm_trap_element {
385 /*
386 * The PC at the time of abort.
387 */
388 unsigned long aborted_pc;
389 /*
390 * New value of the PC.
391 */
392 unsigned long new_pc;
393};
394
395#define RMM_TRAP_HANDLER(_aborted_pc, _new_pc) \
Chuyue Luod7386682023-09-22 11:55:47 +0100396 { .aborted_pc = (unsigned long)(&(_aborted_pc)), \
397 .new_pc = (unsigned long)(&(_new_pc)) }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000398
399/*
400 * The registered locations of load/store instructions that access NS memory.
401 */
402extern void *ns_read;
403extern void *ns_write;
404
405/*
406 * The new value of the PC when the GPF occurs on a registered location.
407 */
408extern void *ns_access_ret_0;
409
AlexeiFedorov53518ba2023-08-22 14:46:26 +0100410static struct rmm_trap_element rmm_trap_list[] = {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000411 RMM_TRAP_HANDLER(ns_read, ns_access_ret_0),
412 RMM_TRAP_HANDLER(ns_write, ns_access_ret_0),
413};
414#define RMM_TRAP_LIST_SIZE (sizeof(rmm_trap_list)/sizeof(struct rmm_trap_element))
415
AlexeiFedorov57f40fc2023-08-17 14:33:33 +0100416__dead2 static void fatal_abort(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000417{
418 report_unexpected();
419
AlexeiFedorov6c119692023-04-21 12:31:15 +0100420 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000421 wfe();
422 }
423}
424
425static bool is_el2_data_abort_gpf(unsigned long esr)
426{
AlexeiFedorov537bee02023-02-02 13:38:23 +0000427 if (((esr & MASK(ESR_EL2_EC)) == ESR_EL2_EC_DATA_ABORT_SEL) &&
AlexeiFedorov6c119692023-04-21 12:31:15 +0100428 ((esr & MASK(ESR_EL2_ABORT_FSC)) == ESR_EL2_ABORT_FSC_GPF)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000429 return true;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100430 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000431 return false;
432}
433
434/*
435 * Handles the RMM's aborts.
436 * It compares the PC at the time of the abort with the registered addresses.
437 * If it finds a match, it returns the new value of the PC that the RMM should
438 * continue from. Other register values are preserved.
439 * If no match is found, it aborts the RMM.
440 */
441unsigned long handle_rmm_trap(void)
442{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000443 unsigned long esr = read_esr_el2();
444 unsigned long elr = read_elr_el2();
445
446 /*
447 * Only the GPF data aborts are recoverable.
448 */
449 if (!is_el2_data_abort_gpf(esr)) {
450 fatal_abort();
451 }
452
AlexeiFedorov6c119692023-04-21 12:31:15 +0100453 for (unsigned int i = 0U; i < RMM_TRAP_LIST_SIZE; i++) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000454 if (rmm_trap_list[i].aborted_pc == elr) {
455 return rmm_trap_list[i].new_pc;
456 }
457 }
458
459 fatal_abort();
AlexeiFedorov6c119692023-04-21 12:31:15 +0100460 return 0UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000461}