blob: 203af89e6de2a1d8dff6cd941b6e531a1a5efd08 [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>
AlexeiFedorov4c7d4852024-01-25 14:37:34 +000010#include <cpuid.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000011#include <debug.h>
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +010012#include <run.h>
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000013#include <simd.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000014#include <smc-handler.h>
15#include <smc-rmi.h>
16#include <smc.h>
17#include <status.h>
18#include <utils_def.h>
AlexeiFedorov4c7d4852024-01-25 14:37:34 +000019#include <xlat_high_va.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000020
AlexeiFedorov6c119692023-04-21 12:31:15 +010021/* Maximum number of supported arguments */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010022#define MAX_NUM_ARGS 5U
Soby Mathewb4c6df42022-11-09 11:13:29 +000023
AlexeiFedorov6c119692023-04-21 12:31:15 +010024/* Maximum number of output values */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010025#define MAX_NUM_OUTPUT_VALS 4U
AlexeiFedorov6c119692023-04-21 12:31:15 +010026
27#define RMI_STATUS_STRING(_id)[RMI_##_id] = #_id
28
AlexeiFedorov04418f42023-09-13 10:50:33 +010029static const char * const rmi_status_string[] = {
AlexeiFedorov6c119692023-04-21 12:31:15 +010030 RMI_STATUS_STRING(SUCCESS),
31 RMI_STATUS_STRING(ERROR_INPUT),
32 RMI_STATUS_STRING(ERROR_REALM),
33 RMI_STATUS_STRING(ERROR_REC),
AlexeiFedorov892abce2023-04-06 16:32:12 +010034 RMI_STATUS_STRING(ERROR_RTT)
Soby Mathewb4c6df42022-11-09 11:13:29 +000035};
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010036
AlexeiFedorov6c119692023-04-21 12:31:15 +010037COMPILER_ASSERT(ARRAY_LEN(rmi_status_string) == RMI_ERROR_COUNT);
Soby Mathewb4c6df42022-11-09 11:13:29 +000038
39/*
40 * At this level (in handle_ns_smc) we distinguish the RMI calls only on:
AlexeiFedorov6c119692023-04-21 12:31:15 +010041 * - The number of input arguments [0..5], and whether
Soby Mathewb4c6df42022-11-09 11:13:29 +000042 * - The function returns up to three output values in addition
43 * to the return status code.
44 * Hence, the naming syntax is:
AlexeiFedorov6c119692023-04-21 12:31:15 +010045 * - `*_[0..5]` when no output values are returned, and
46 * - `*_[0..3]_o` when the function returns some output values.
Soby Mathewb4c6df42022-11-09 11:13:29 +000047 */
Soby Mathewb4c6df42022-11-09 11:13:29 +000048typedef unsigned long (*handler_0)(void);
49typedef unsigned long (*handler_1)(unsigned long arg0);
50typedef unsigned long (*handler_2)(unsigned long arg0, unsigned long arg1);
51typedef unsigned long (*handler_3)(unsigned long arg0, unsigned long arg1,
52 unsigned long arg2);
53typedef unsigned long (*handler_4)(unsigned long arg0, unsigned long arg1,
54 unsigned long arg2, unsigned long arg3);
55typedef unsigned long (*handler_5)(unsigned long arg0, unsigned long arg1,
56 unsigned long arg2, unsigned long arg3,
57 unsigned long arg4);
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010058typedef void (*handler_1_o)(unsigned long arg0, struct smc_result *res);
AlexeiFedorov892abce2023-04-06 16:32:12 +010059typedef void (*handler_2_o)(unsigned long arg0, unsigned long arg1,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010060 struct smc_result *res);
Soby Mathewb4c6df42022-11-09 11:13:29 +000061typedef void (*handler_3_o)(unsigned long arg0, unsigned long arg1,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010062 unsigned long arg2, struct smc_result *res);
AlexeiFedorov892abce2023-04-06 16:32:12 +010063typedef void (*handler_4_o)(unsigned long arg0, unsigned long arg1,
64 unsigned long arg2, unsigned long arg3,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +010065 struct smc_result *res);
Soby Mathewb4c6df42022-11-09 11:13:29 +000066
AlexeiFedorov6c119692023-04-21 12:31:15 +010067/*
68 * SMC RMI handler type encoding:
69 * [0:7] - number of arguments
70 * [8:15] - number of output values
71 */
Shruti Gupta9e966b82024-03-21 13:45:24 +000072#define RMI_TYPE(_in, _out) (U(_in) | (U(_out) << 8U))
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010073#define set_rmi_type(_in, _out) rmi_type_##_in##_out = RMI_TYPE(_in, _out)
AlexeiFedorov6c119692023-04-21 12:31:15 +010074
Soby Mathewb4c6df42022-11-09 11:13:29 +000075enum rmi_type {
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010076 set_rmi_type(0, 0), /* 0 arguments, 0 output values */
77 set_rmi_type(1, 0), /* 1 argument, 0 output values */
78 set_rmi_type(2, 0), /* 2 arguments, 0 output values */
79 set_rmi_type(3, 0), /* 3 arguments, 0 output values */
80 set_rmi_type(4, 0), /* 4 arguments, 0 output values */
81 set_rmi_type(5, 0), /* 5 arguments, 0 output values */
82 set_rmi_type(1, 1), /* 1 argument, 1 output value */
AlexeiFedorov2cc6cee2023-10-09 16:19:05 +010083 set_rmi_type(1, 2), /* 1 argument, 2 output values */
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010084 set_rmi_type(2, 2), /* 2 arguments, 2 output values */
85 set_rmi_type(3, 1), /* 3 arguments, 1 output value */
86 set_rmi_type(3, 2), /* 3 arguments, 2 output values */
87 set_rmi_type(3, 4), /* 3 arguments, 4 output values */
88 set_rmi_type(4, 1) /* 4 arguments, 1 output value */
Soby Mathewb4c6df42022-11-09 11:13:29 +000089};
90
91struct smc_handler {
92 const char *fn_name;
Soby Mathewb4c6df42022-11-09 11:13:29 +000093 union {
AlexeiFedorov6c119692023-04-21 12:31:15 +010094 handler_0 f_00;
95 handler_1 f_10;
96 handler_2 f_20;
97 handler_3 f_30;
98 handler_4 f_40;
99 handler_5 f_50;
100 handler_1_o f_11;
AlexeiFedorov2cc6cee2023-10-09 16:19:05 +0100101 handler_1_o f_12;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100102 handler_2_o f_22;
103 handler_3_o f_31;
104 handler_3_o f_32;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100105 handler_3_o f_34;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100106 handler_4_o f_41;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000107 void *fn_dummy;
108 };
Chuyue Luo819aa312023-11-03 10:41:27 +0000109 enum rmi_type type;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000110 bool log_exec; /* print handler execution */
111 bool log_error; /* print in case of error status */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000112};
113
114/*
115 * Get handler ID from FID
116 * Precondition: FID is an RMI call
117 */
AlexeiFedorov6c119692023-04-21 12:31:15 +0100118#define RMI_HANDLER_ID(_id) SMC64_FID_OFFSET_FROM_RANGE_MIN(RMI, _id)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000119
AlexeiFedorov6c119692023-04-21 12:31:15 +0100120#define HANDLER(_id, _in, _out, _fn, _exec, _error)[RMI_HANDLER_ID(SMC_RMM_##_id)] = { \
AlexeiFedorov6a4314e2023-10-20 15:40:14 +0100121 .fn_name = (#_id), \
122 .type = (enum rmi_type)RMI_TYPE(_in, _out), \
123 .f_##_in##_out = (_fn), \
124 .log_exec = (_exec), \
125 .log_error = (_error) \
AlexeiFedorov6c119692023-04-21 12:31:15 +0100126}
Soby Mathewb4c6df42022-11-09 11:13:29 +0000127
128/*
129 * The 3rd value enables the execution log.
130 * The 4th value enables the error log.
131 */
132static const struct smc_handler smc_handlers[] = {
AlexeiFedorov2cc6cee2023-10-09 16:19:05 +0100133 HANDLER(VERSION, 1, 2, smc_version, true, true),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100134 HANDLER(FEATURES, 1, 1, smc_read_feature_register, true, true),
135 HANDLER(GRANULE_DELEGATE, 1, 0, smc_granule_delegate, false, true),
136 HANDLER(GRANULE_UNDELEGATE, 1, 0, smc_granule_undelegate, false, true),
137 HANDLER(REALM_CREATE, 2, 0, smc_realm_create, true, true),
138 HANDLER(REALM_DESTROY, 1, 0, smc_realm_destroy, true, true),
139 HANDLER(REALM_ACTIVATE, 1, 0, smc_realm_activate, true, true),
140 HANDLER(REC_CREATE, 3, 0, smc_rec_create, true, true),
141 HANDLER(REC_DESTROY, 1, 0, smc_rec_destroy, true, true),
142 HANDLER(REC_ENTER, 2, 0, smc_rec_enter, false, true),
143 HANDLER(DATA_CREATE, 5, 0, smc_data_create, false, false),
144 HANDLER(DATA_CREATE_UNKNOWN, 3, 0, smc_data_create_unknown, false, false),
AlexeiFedorove2002be2023-04-19 17:20:12 +0100145 HANDLER(DATA_DESTROY, 2, 2, smc_data_destroy, false, true),
AlexeiFedorov22ba1062023-11-15 16:23:37 +0000146 HANDLER(RTT_CREATE, 4, 0, smc_rtt_create, true, true),
147 HANDLER(RTT_DESTROY, 3, 2, smc_rtt_destroy, true, true),
AlexeiFedorovf22f54f2023-10-26 13:25:34 +0100148 HANDLER(RTT_FOLD, 3, 1, smc_rtt_fold, false, false),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100149 HANDLER(RTT_MAP_UNPROTECTED, 4, 0, smc_rtt_map_unprotected, false, false),
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100150 HANDLER(RTT_UNMAP_UNPROTECTED, 3, 1, smc_rtt_unmap_unprotected, false, false),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100151 HANDLER(RTT_READ_ENTRY, 3, 4, smc_rtt_read_entry, false, true),
AlexeiFedorov120d7d02023-08-02 16:51:48 +0100152 HANDLER(PSCI_COMPLETE, 3, 0, smc_psci_complete, true, true),
AlexeiFedorov6c119692023-04-21 12:31:15 +0100153 HANDLER(REC_AUX_COUNT, 1, 1, smc_rec_aux_count, true, true),
AlexeiFedorov960d1612023-04-25 13:23:39 +0100154 HANDLER(RTT_INIT_RIPAS, 3, 1, smc_rtt_init_ripas, false, true),
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +0100155 HANDLER(RTT_SET_RIPAS, 4, 1, smc_rtt_set_ripas, false, true)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000156};
157
158COMPILER_ASSERT(ARRAY_LEN(smc_handlers) == SMC64_NUM_FIDS_IN_RANGE(RMI));
159
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100160static inline bool rmi_handler_needs_fpu(unsigned int id)
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000161{
162#ifdef RMM_FPU_USE_AT_REL2
AlexeiFedorov7c38ef42023-11-28 11:39:19 +0000163 if ((id == SMC_RMM_REALM_CREATE) || (id == SMC_RMM_DATA_CREATE) ||
164 (id == SMC_RMM_REC_CREATE) || (id == SMC_RMM_RTT_INIT_RIPAS)) {
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000165 return true;
166 }
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100167#else
168 (void)id;
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000169#endif
170 return false;
171}
172
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100173static void rmi_log_on_exit(unsigned int handler_id,
AlexeiFedorov6c119692023-04-21 12:31:15 +0100174 unsigned long args[],
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100175 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000176{
177 const struct smc_handler *handler = &smc_handlers[handler_id];
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100178 unsigned int function_id = SMC64_RMI_FID(handler_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000179 return_code_t rc;
180
181 if (!handler->log_exec && !handler->log_error) {
182 return;
183 }
184
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100185 rc = unpack_return_code(res->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000186
187 if ((handler->log_exec) ||
188 (handler->log_error && (rc.status != RMI_SUCCESS))) {
Shruti Gupta9e966b82024-03-21 13:45:24 +0000189 unsigned int num;
190
AlexeiFedorov6c119692023-04-21 12:31:15 +0100191 /* Print function name */
192 INFO("SMC_RMM_%-21s", handler->fn_name);
193
194 /* Print arguments */
AlexeiFedorov84cba4f2023-08-25 13:45:48 +0100195 num = (unsigned int)handler->type & 0xFFU;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100196 assert(num <= MAX_NUM_ARGS);
197
198 for (unsigned int i = 0U; i < num; i++) {
199 INFO(" %lx", args[i]);
200 }
201
202 /* Print status */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000203 if (rc.status >= RMI_ERROR_COUNT) {
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100204 INFO(" > %lx", res->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000205 } else {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100206 INFO(" > RMI_%s", rmi_status_string[rc.status]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000207 }
208
209 /* Check for index */
210 if (((function_id == SMC_RMM_REC_ENTER) &&
211 (rc.status == RMI_ERROR_REALM)) ||
212 (rc.status == RMI_ERROR_RTT)) {
213 INFO(" %x", rc.index);
AlexeiFedorov697445b2023-04-25 15:27:57 +0100214 }
215
216 if ((rc.status == RMI_SUCCESS) ||
217 ((rc.status == RMI_ERROR_RTT) &&
218 ((function_id == SMC_RMM_RTT_DESTROY) ||
219 (function_id == SMC_RMM_DATA_DESTROY)))) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100220 /* Print output values */
AlexeiFedorov84cba4f2023-08-25 13:45:48 +0100221 num = ((unsigned int)handler->type >> 8) & 0xFFU;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100222 assert(num <= MAX_NUM_OUTPUT_VALS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000223
AlexeiFedorov6c119692023-04-21 12:31:15 +0100224 for (unsigned int i = 1U; i <= num; i++) {
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100225 INFO(" %lx", res->x[i]);
AlexeiFedorov6c119692023-04-21 12:31:15 +0100226 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000227 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000228 INFO("\n");
229 }
230}
231
Shruti Gupta0a0d4ee2024-04-22 21:37:25 +0100232/* cppcheck-suppress misra-c2012-8.4 */
AlexeiFedorovee2fc822023-10-31 14:54:39 +0000233/* coverity[misra_c_2012_rule_8_4_violation:SUPPRESS] */
AlexeiFedorovb0de4d52023-10-10 11:46:16 +0100234/* coverity[misra_c_2012_rule_8_7_violation:SUPPRESS] */
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100235void handle_ns_smc(unsigned int function_id,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000236 unsigned long arg0,
237 unsigned long arg1,
238 unsigned long arg2,
239 unsigned long arg3,
240 unsigned long arg4,
241 unsigned long arg5,
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100242 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000243{
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100244 (void)arg5;
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100245 unsigned int handler_id;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000246 const struct smc_handler *handler = NULL;
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000247 bool restore_ns_simd_state = false;
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100248 struct simd_context *ns_simd_ctx;
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100249 bool sve_hint = false;
AlexeiFedorov7c38ef42023-11-28 11:39:19 +0000250 unsigned long args[] __unused = {arg0, arg1, arg2, arg3, arg4};
Soby Mathewb4c6df42022-11-09 11:13:29 +0000251
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100252 /* Save the SVE hint bit and clear it from the function ID */
253 if ((function_id & SMC_SVE_HINT) != 0U) {
254 sve_hint = true;
255 function_id &= ~SMC_SVE_HINT;
256 }
Arunachalam Ganapathy937b5492023-02-28 11:17:52 +0000257
Soby Mathewb4c6df42022-11-09 11:13:29 +0000258 if (IS_SMC64_RMI_FID(function_id)) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100259 handler_id = RMI_HANDLER_ID(function_id);
Shruti Gupta0a0d4ee2024-04-22 21:37:25 +0100260 /* cppcheck-suppress misra-c2012-17.3 */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000261 if (handler_id < ARRAY_LEN(smc_handlers)) {
262 handler = &smc_handlers[handler_id];
263 }
264 }
265
266 /*
267 * Check if handler exists and 'fn_dummy' is not NULL
268 * for not implemented 'function_id' calls in SMC RMI range.
269 */
270 if ((handler == NULL) || (handler->fn_dummy == NULL)) {
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100271 VERBOSE("[%s] unknown function_id: %x\n",
Soby Mathewb4c6df42022-11-09 11:13:29 +0000272 __func__, function_id);
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100273 res->x[0] = SMC_UNKNOWN;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000274 return;
275 }
276
AlexeiFedorov7c38ef42023-11-28 11:39:19 +0000277 assert(check_cpu_slots_empty());
Soby Mathewb4c6df42022-11-09 11:13:29 +0000278
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000279 /* Current CPU's SIMD state must not be saved when entering RMM */
280 assert(simd_is_state_saved() == false);
281
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100282 /* Get current CPU's NS SIMD context */
283 ns_simd_ctx = get_cpu_ns_simd_context();
284
Arunachalam Ganapathyebfab6a2023-08-24 14:44:23 +0100285 /* Set or clear SVE hint bit in the NS SIMD context */
286 simd_update_smc_sve_hint(ns_simd_ctx, sve_hint);
287
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000288 /* If the handler needs FPU, actively save NS simd context. */
289 if (rmi_handler_needs_fpu(function_id) == true) {
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100290 simd_context_save(ns_simd_ctx);
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000291 restore_ns_simd_state = true;
292 }
293
Soby Mathewb4c6df42022-11-09 11:13:29 +0000294 switch (handler->type) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100295 case rmi_type_00:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100296 res->x[0] = handler->f_00();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000297 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100298 case rmi_type_10:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100299 res->x[0] = handler->f_10(arg0);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000300 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100301 case rmi_type_20:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100302 res->x[0] = handler->f_20(arg0, arg1);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000303 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100304 case rmi_type_30:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100305 res->x[0] = handler->f_30(arg0, arg1, arg2);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000306 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100307 case rmi_type_40:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100308 res->x[0] = handler->f_40(arg0, arg1, arg2, arg3);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000309 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100310 case rmi_type_50:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100311 res->x[0] = handler->f_50(arg0, arg1, arg2, arg3, arg4);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000312 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100313 case rmi_type_11:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100314 handler->f_11(arg0, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000315 break;
AlexeiFedorov2cc6cee2023-10-09 16:19:05 +0100316 case rmi_type_12:
317 handler->f_12(arg0, res);
318 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100319 case rmi_type_22:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100320 handler->f_22(arg0, arg1, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100321 break;
322 case rmi_type_31:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100323 handler->f_31(arg0, arg1, arg2, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100324 break;
325 case rmi_type_32:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100326 handler->f_32(arg0, arg1, arg2, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100327 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100328 case rmi_type_34:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100329 handler->f_34(arg0, arg1, arg2, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000330 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100331 case rmi_type_41:
AlexeiFedorovccce3ad2023-04-28 18:29:47 +0100332 handler->f_41(arg0, arg1, arg2, arg3, res);
AlexeiFedorov892abce2023-04-06 16:32:12 +0100333 break;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000334 default:
335 assert(false);
336 }
337
AlexeiFedorov7c38ef42023-11-28 11:39:19 +0000338 rmi_log_on_exit(handler_id, args, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000339
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100340 /* If the handler uses FPU, restore the saved NS simd context. */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000341 if (restore_ns_simd_state) {
Arunachalam Ganapathy4f601e72023-05-22 11:49:29 +0100342 simd_context_restore(ns_simd_ctx);
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000343 }
344
345 /* Current CPU's SIMD state must not be saved when exiting RMM */
346 assert(simd_is_state_saved() == false);
AlexeiFedorov7c38ef42023-11-28 11:39:19 +0000347 assert(check_cpu_slots_empty());
Soby Mathewb4c6df42022-11-09 11:13:29 +0000348}
349
Soby Mathewb4c6df42022-11-09 11:13:29 +0000350/*
351 * Identifies an abort that the RMM may recover from.
352 */
353struct rmm_trap_element {
354 /*
355 * The PC at the time of abort.
356 */
357 unsigned long aborted_pc;
358 /*
359 * New value of the PC.
360 */
361 unsigned long new_pc;
362};
363
364#define RMM_TRAP_HANDLER(_aborted_pc, _new_pc) \
Chuyue Luod7386682023-09-22 11:55:47 +0100365 { .aborted_pc = (unsigned long)(&(_aborted_pc)), \
366 .new_pc = (unsigned long)(&(_new_pc)) }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000367
368/*
369 * The registered locations of load/store instructions that access NS memory.
370 */
371extern void *ns_read;
372extern void *ns_write;
373
374/*
375 * The new value of the PC when the GPF occurs on a registered location.
376 */
377extern void *ns_access_ret_0;
378
AlexeiFedorov53518ba2023-08-22 14:46:26 +0100379static struct rmm_trap_element rmm_trap_list[] = {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000380 RMM_TRAP_HANDLER(ns_read, ns_access_ret_0),
381 RMM_TRAP_HANDLER(ns_write, ns_access_ret_0),
382};
383#define RMM_TRAP_LIST_SIZE (sizeof(rmm_trap_list)/sizeof(struct rmm_trap_element))
384
AlexeiFedorov4c7d4852024-01-25 14:37:34 +0000385/* Crash dump registers X0 - X29 */
386#define DUMP_REGS 30U
Soby Mathewb4c6df42022-11-09 11:13:29 +0000387
AlexeiFedorov4c7d4852024-01-25 14:37:34 +0000388typedef struct {
389 uint64_t x[DUMP_REGS];
390 uint64_t lr;
391} dump_regs_t;
392
393__dead2 static void fatal_abort(dump_regs_t *regs)
394{
395 __unused uint64_t sp_el2;
396 __unused uint64_t sp_el0 = (uint64_t)regs + sizeof(dump_regs_t);
397 __unused uint64_t lr = regs->lr;
398
399 ERROR("Unexpected exception on CPU #%u:\n", my_cpuid());
400
401 for (unsigned int i = 0U; i < DUMP_REGS; i += 2U) {
402 INFO("X%u:\t\t0x%016lx X%u:\t\t0x%016lx\n",
403 i, regs->x[i], i + 1U, regs->x[i + 1U]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000404 }
AlexeiFedorov4c7d4852024-01-25 14:37:34 +0000405
406 /* Demangle return address */
407 xpaci(lr);
408
409 /* Switch SPSEL to read SP_EL2 register */
410 write_spsel(MODE_SP_ELX);
411 read_sp(sp_el2);
412 write_spsel(MODE_SP_EL0);
413
414 INFO("LR:\t\t0x%016lx\n", lr);
415 INFO("SP_EL0:\t\t0x%016lx\n", sp_el0);
416 INFO("SP_EL2:\t\t0x%016lx%c (0x%016lx-0x%016lx)\n", sp_el2,
417 ((sp_el2 < CPU_STACK_VIRT) ||
418 (sp_el2 >= RMM_CPU_STACK_END_VA)) ? '*' : ' ',
419 CPU_STACK_VIRT, (RMM_CPU_STACK_END_VA - 1UL));
420 INFO("ESR_EL2:\t0x%016lx\n", read_esr_el2());
421 INFO("ELR_EL2:\t0x%016lx\n", read_elr_el2());
422 INFO("FAR_EL2:\t0x%016lx\n", read_far_el2());
423 INFO("SPSR_EL2:\t0x%016lx\n", read_spsr_el2());
424 INFO("HCR_EL2:\t0x%016lx E2H = %c\n", read_hcr_el2(),
425 ((read_hcr_el2() & HCR_E2H) == 0UL) ? '0' : '1');
426 INFO("CPTR_EL2:\t0x%016lx\n", read_cptr_el2());
427 INFO("MDCR_EL2:\t0x%016lx\n", read_mdcr_el2());
428 INFO("SCTLR_EL2:\t0x%016lx\n", read_sctlr_el2());
429
AlexeiFedorovbe9209c2024-02-27 15:16:00 +0000430 /* The AArch64 AAPCS mandates the usage of x29 as Frame Pointer */
431 backtrace((uintptr_t)regs->x[29]);
AlexeiFedorov4c7d4852024-01-25 14:37:34 +0000432 panic();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000433}
434
435static bool is_el2_data_abort_gpf(unsigned long esr)
436{
AlexeiFedorov537bee02023-02-02 13:38:23 +0000437 if (((esr & MASK(ESR_EL2_EC)) == ESR_EL2_EC_DATA_ABORT_SEL) &&
AlexeiFedorov6c119692023-04-21 12:31:15 +0100438 ((esr & MASK(ESR_EL2_ABORT_FSC)) == ESR_EL2_ABORT_FSC_GPF)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000439 return true;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100440 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000441 return false;
442}
443
444/*
445 * Handles the RMM's aborts.
446 * It compares the PC at the time of the abort with the registered addresses.
447 * If it finds a match, it returns the new value of the PC that the RMM should
448 * continue from. Other register values are preserved.
449 * If no match is found, it aborts the RMM.
450 */
Shruti Gupta0a0d4ee2024-04-22 21:37:25 +0100451/* cppcheck-suppress misra-c2012-8.4 */
AlexeiFedorovee2fc822023-10-31 14:54:39 +0000452/* coverity[misra_c_2012_rule_8_4_violation:SUPPRESS] */
Shruti Gupta0a0d4ee2024-04-22 21:37:25 +0100453/* coverity[misra_c_2012_rule_8_7_violation:SUPPRESS] */
AlexeiFedorov4c7d4852024-01-25 14:37:34 +0000454unsigned long handle_rmm_trap(dump_regs_t *regs)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000455{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000456 unsigned long esr = read_esr_el2();
457 unsigned long elr = read_elr_el2();
458
459 /*
460 * Only the GPF data aborts are recoverable.
461 */
462 if (!is_el2_data_abort_gpf(esr)) {
AlexeiFedorov4c7d4852024-01-25 14:37:34 +0000463 fatal_abort(regs);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000464 }
465
AlexeiFedorov6c119692023-04-21 12:31:15 +0100466 for (unsigned int i = 0U; i < RMM_TRAP_LIST_SIZE; i++) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000467 if (rmm_trap_list[i].aborted_pc == elr) {
468 return rmm_trap_list[i].new_pc;
469 }
470 }
471
AlexeiFedorov4c7d4852024-01-25 14:37:34 +0000472 fatal_abort(regs);
AlexeiFedorov6c119692023-04-21 12:31:15 +0100473 return 0UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000474}