blob: 1a43392683b6019eb18ea2bd775ca0a1735f9035 [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 Ganapathy51119932023-03-23 12:32:49 +000011#include <simd.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000012#include <smc-handler.h>
13#include <smc-rmi.h>
14#include <smc.h>
15#include <status.h>
16#include <utils_def.h>
17
AlexeiFedorov6c119692023-04-21 12:31:15 +010018/* Maximum number of supported arguments */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010019#define MAX_NUM_ARGS 5U
Soby Mathewb4c6df42022-11-09 11:13:29 +000020
AlexeiFedorov6c119692023-04-21 12:31:15 +010021/* Maximum number of output values */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010022#define MAX_NUM_OUTPUT_VALS 4U
AlexeiFedorov6c119692023-04-21 12:31:15 +010023
24#define RMI_STATUS_STRING(_id)[RMI_##_id] = #_id
25
AlexeiFedorov04418f42023-09-13 10:50:33 +010026static const char * const rmi_status_string[] = {
AlexeiFedorov6c119692023-04-21 12:31:15 +010027 RMI_STATUS_STRING(SUCCESS),
28 RMI_STATUS_STRING(ERROR_INPUT),
29 RMI_STATUS_STRING(ERROR_REALM),
30 RMI_STATUS_STRING(ERROR_REC),
AlexeiFedorov892abce2023-04-06 16:32:12 +010031 RMI_STATUS_STRING(ERROR_RTT)
Soby Mathewb4c6df42022-11-09 11:13:29 +000032};
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010033
AlexeiFedorov6c119692023-04-21 12:31:15 +010034COMPILER_ASSERT(ARRAY_LEN(rmi_status_string) == RMI_ERROR_COUNT);
Soby Mathewb4c6df42022-11-09 11:13:29 +000035
36/*
37 * At this level (in handle_ns_smc) we distinguish the RMI calls only on:
AlexeiFedorov6c119692023-04-21 12:31:15 +010038 * - The number of input arguments [0..5], and whether
Soby Mathewb4c6df42022-11-09 11:13:29 +000039 * - The function returns up to three output values in addition
40 * to the return status code.
41 * Hence, the naming syntax is:
AlexeiFedorov6c119692023-04-21 12:31:15 +010042 * - `*_[0..5]` when no output values are returned, and
43 * - `*_[0..3]_o` when the function returns some output values.
Soby Mathewb4c6df42022-11-09 11:13:29 +000044 */
Soby Mathewb4c6df42022-11-09 11:13:29 +000045typedef unsigned long (*handler_0)(void);
46typedef unsigned long (*handler_1)(unsigned long arg0);
47typedef unsigned long (*handler_2)(unsigned long arg0, unsigned long arg1);
48typedef unsigned long (*handler_3)(unsigned long arg0, unsigned long arg1,
49 unsigned long arg2);
50typedef unsigned long (*handler_4)(unsigned long arg0, unsigned long arg1,
51 unsigned long arg2, unsigned long arg3);
52typedef unsigned long (*handler_5)(unsigned long arg0, unsigned long arg1,
53 unsigned long arg2, unsigned long arg3,
54 unsigned long arg4);
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010055typedef void (*handler_1_o)(unsigned long arg0, struct smc_result *res);
AlexeiFedorov892abce2023-04-06 16:32:12 +010056typedef void (*handler_2_o)(unsigned long arg0, unsigned long arg1,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010057 struct smc_result *res);
Soby Mathewb4c6df42022-11-09 11:13:29 +000058typedef void (*handler_3_o)(unsigned long arg0, unsigned long arg1,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010059 unsigned long arg2, struct smc_result *res);
AlexeiFedorov892abce2023-04-06 16:32:12 +010060typedef void (*handler_4_o)(unsigned long arg0, unsigned long arg1,
61 unsigned long arg2, unsigned long arg3,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010062 struct smc_result *res);
Soby Mathewb4c6df42022-11-09 11:13:29 +000063
AlexeiFedorov6c119692023-04-21 12:31:15 +010064/*
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))
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010070#define set_rmi_type(_in, _out) rmi_type_##_in##_out = RMI_TYPE(_in, _out)
AlexeiFedorov6c119692023-04-21 12:31:15 +010071
Soby Mathewb4c6df42022-11-09 11:13:29 +000072enum rmi_type {
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010073 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 Mathewb4c6df42022-11-09 11:13:29 +000085};
86
87struct smc_handler {
88 const char *fn_name;
89 enum rmi_type type;
90 union {
AlexeiFedorov6c119692023-04-21 12:31:15 +010091 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;
AlexeiFedorov892abce2023-04-06 16:32:12 +010098 handler_2_o f_22;
99 handler_3_o f_31;
100 handler_3_o f_32;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100101 handler_3_o f_34;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100102 handler_4_o f_41;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000103 void *fn_dummy;
104 };
105 bool log_exec; /* print handler execution */
106 bool log_error; /* print in case of error status */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000107};
108
109/*
110 * Get handler ID from FID
111 * Precondition: FID is an RMI call
112 */
AlexeiFedorov6c119692023-04-21 12:31:15 +0100113#define RMI_HANDLER_ID(_id) SMC64_FID_OFFSET_FROM_RANGE_MIN(RMI, _id)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000114
AlexeiFedorov6c119692023-04-21 12:31:15 +0100115#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 Mathewb4c6df42022-11-09 11:13:29 +0000122
123/*
124 * The 3rd value enables the execution log.
125 * The 4th value enables the error log.
126 */
127static const struct smc_handler smc_handlers[] = {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100128 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),
AlexeiFedorove2002be2023-04-19 17:20:12 +0100140 HANDLER(DATA_DESTROY, 2, 2, smc_data_destroy, false, true),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100141 HANDLER(RTT_CREATE, 4, 0, smc_rtt_create, false, true),
AlexeiFedorove2002be2023-04-19 17:20:12 +0100142 HANDLER(RTT_DESTROY, 3, 2, smc_rtt_destroy, false, true),
143 HANDLER(RTT_FOLD, 3, 1, smc_rtt_fold, false, true),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100144 HANDLER(RTT_MAP_UNPROTECTED, 4, 0, smc_rtt_map_unprotected, false, false),
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100145 HANDLER(RTT_UNMAP_UNPROTECTED, 3, 1, smc_rtt_unmap_unprotected, false, false),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100146 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),
AlexeiFedorov960d1612023-04-25 13:23:39 +0100149 HANDLER(RTT_INIT_RIPAS, 3, 1, smc_rtt_init_ripas, false, true),
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +0100150 HANDLER(RTT_SET_RIPAS, 4, 1, smc_rtt_set_ripas, false, true)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000151};
152
153COMPILER_ASSERT(ARRAY_LEN(smc_handlers) == SMC64_NUM_FIDS_IN_RANGE(RMI));
154
155static bool rmi_call_log_enabled = true;
156
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000157static 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 }
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100164#else
165 (void)id;
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000166#endif
167 return false;
168}
169
Soby Mathewb4c6df42022-11-09 11:13:29 +0000170static void rmi_log_on_exit(unsigned long handler_id,
AlexeiFedorov6c119692023-04-21 12:31:15 +0100171 unsigned long args[],
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100172 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000173{
174 const struct smc_handler *handler = &smc_handlers[handler_id];
175 unsigned long function_id = SMC64_RMI_FID(handler_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000176 return_code_t rc;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100177 unsigned int num;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000178
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 */
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100188 INFO("SMC_RMM_%-21s > %lx\n", handler->fn_name, res->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000189 return;
190 }
191
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100192 rc = unpack_return_code(res->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000193
194 if ((handler->log_exec) ||
195 (handler->log_error && (rc.status != RMI_SUCCESS))) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100196 /* Print function name */
197 INFO("SMC_RMM_%-21s", handler->fn_name);
198
199 /* Print arguments */
AlexeiFedorov84cba4f2023-08-25 13:45:48 +0100200 num = (unsigned int)handler->type & 0xFFU;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100201 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 Mathewb4c6df42022-11-09 11:13:29 +0000208 if (rc.status >= RMI_ERROR_COUNT) {
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100209 INFO(" > %lx", res->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000210 } else {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100211 INFO(" > RMI_%s", rmi_status_string[rc.status]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000212 }
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);
AlexeiFedorov697445b2023-04-25 15:27:57 +0100219 }
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)))) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100225 /* Print output values */
AlexeiFedorov84cba4f2023-08-25 13:45:48 +0100226 num = ((unsigned int)handler->type >> 8) & 0xFFU;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100227 assert(num <= MAX_NUM_OUTPUT_VALS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000228
AlexeiFedorov6c119692023-04-21 12:31:15 +0100229 for (unsigned int i = 1U; i <= num; i++) {
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100230 INFO(" %lx", res->x[i]);
AlexeiFedorov6c119692023-04-21 12:31:15 +0100231 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000232 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000233 INFO("\n");
234 }
235}
236
237void 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,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100244 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000245{
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100246 (void)arg5;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000247 unsigned long handler_id;
248 const struct smc_handler *handler = NULL;
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000249 bool restore_ns_simd_state = false;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000250
Arunachalam Ganapathy937b5492023-02-28 11:17:52 +0000251 /* Ignore SVE hint bit, until RMM supports SVE hint bit */
AlexeiFedorov4faab852023-08-30 15:06:49 +0100252 function_id &= ~SMC_SVE_HINT;
Arunachalam Ganapathy937b5492023-02-28 11:17:52 +0000253
Soby Mathewb4c6df42022-11-09 11:13:29 +0000254 if (IS_SMC64_RMI_FID(function_id)) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100255 handler_id = RMI_HANDLER_ID(function_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000256 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);
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100268 res->x[0] = SMC_UNKNOWN;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000269 return;
270 }
271
272 assert_cpu_slots_empty();
273
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000274 /* 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 Mathewb4c6df42022-11-09 11:13:29 +0000283 switch (handler->type) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100284 case rmi_type_00:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100285 res->x[0] = handler->f_00();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000286 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100287 case rmi_type_10:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100288 res->x[0] = handler->f_10(arg0);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000289 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100290 case rmi_type_20:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100291 res->x[0] = handler->f_20(arg0, arg1);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000292 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100293 case rmi_type_30:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100294 res->x[0] = handler->f_30(arg0, arg1, arg2);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000295 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100296 case rmi_type_40:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100297 res->x[0] = handler->f_40(arg0, arg1, arg2, arg3);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000298 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100299 case rmi_type_50:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100300 res->x[0] = handler->f_50(arg0, arg1, arg2, arg3, arg4);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000301 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100302 case rmi_type_11:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100303 handler->f_11(arg0, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000304 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100305 case rmi_type_22:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100306 handler->f_22(arg0, arg1, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100307 break;
308 case rmi_type_31:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100309 handler->f_31(arg0, arg1, arg2, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100310 break;
311 case rmi_type_32:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100312 handler->f_32(arg0, arg1, arg2, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100313 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100314 case rmi_type_34:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100315 handler->f_34(arg0, arg1, arg2, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000316 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100317 case rmi_type_41:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100318 handler->f_41(arg0, arg1, arg2, arg3, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100319 break;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000320 default:
321 assert(false);
322 }
323
324 if (rmi_call_log_enabled) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100325 unsigned long args[] = {arg0, arg1, arg2, arg3, arg4};
326
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100327 rmi_log_on_exit(handler_id, args, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000328 }
329
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000330 /* 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 Mathewb4c6df42022-11-09 11:13:29 +0000338 assert_cpu_slots_empty();
339}
340
341static 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 Mathewb4c6df42022-11-09 11:13:29 +0000355}
356
AlexeiFedorov57f40fc2023-08-17 14:33:33 +0100357__dead2 unsigned long handle_realm_trap(unsigned long *regs)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000358{
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100359 (void)regs;
360
Soby Mathewb4c6df42022-11-09 11:13:29 +0000361 report_unexpected();
362
AlexeiFedorov6c119692023-04-21 12:31:15 +0100363 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000364 wfe();
365 }
366}
367
368/*
369 * Identifies an abort that the RMM may recover from.
370 */
371struct 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 */
389extern void *ns_read;
390extern void *ns_write;
391
392/*
393 * The new value of the PC when the GPF occurs on a registered location.
394 */
395extern void *ns_access_ret_0;
396
AlexeiFedorov53518ba2023-08-22 14:46:26 +0100397static struct rmm_trap_element rmm_trap_list[] = {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000398 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
AlexeiFedorov57f40fc2023-08-17 14:33:33 +0100403__dead2 static void fatal_abort(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000404{
405 report_unexpected();
406
AlexeiFedorov6c119692023-04-21 12:31:15 +0100407 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000408 wfe();
409 }
410}
411
412static bool is_el2_data_abort_gpf(unsigned long esr)
413{
AlexeiFedorov537bee02023-02-02 13:38:23 +0000414 if (((esr & MASK(ESR_EL2_EC)) == ESR_EL2_EC_DATA_ABORT_SEL) &&
AlexeiFedorov6c119692023-04-21 12:31:15 +0100415 ((esr & MASK(ESR_EL2_ABORT_FSC)) == ESR_EL2_ABORT_FSC_GPF)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000416 return true;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100417 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000418 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 */
428unsigned long handle_rmm_trap(void)
429{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000430 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
AlexeiFedorov6c119692023-04-21 12:31:15 +0100440 for (unsigned int i = 0U; i < RMM_TRAP_LIST_SIZE; i++) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000441 if (rmm_trap_list[i].aborted_pc == elr) {
442 return rmm_trap_list[i].new_pc;
443 }
444 }
445
446 fatal_abort();
AlexeiFedorov6c119692023-04-21 12:31:15 +0100447 return 0UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000448}