blob: a38b56ecaf3dec9d01f44a9d815c9bebf3f4db25 [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 <sizes.h>
13#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 */
20#define MAX_NUM_ARGS 5
Soby Mathewb4c6df42022-11-09 11:13:29 +000021
AlexeiFedorov6c119692023-04-21 12:31:15 +010022/* Maximum number of output values */
23#define MAX_NUM_OUTPUT_VALS 4
24
25#define RMI_STATUS_STRING(_id)[RMI_##_id] = #_id
26
27const char *rmi_status_string[] = {
28 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};
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);
55typedef void (*handler_1_o)(unsigned long arg0, struct smc_result *ret);
AlexeiFedorov892abce2023-04-06 16:32:12 +010056typedef void (*handler_2_o)(unsigned long arg0, unsigned long arg1,
57 struct smc_result *ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +000058typedef void (*handler_3_o)(unsigned long arg0, unsigned long arg1,
59 unsigned long arg2, struct smc_result *ret);
AlexeiFedorov892abce2023-04-06 16:32:12 +010060typedef void (*handler_4_o)(unsigned long arg0, unsigned long arg1,
61 unsigned long arg2, unsigned long arg3,
62 struct smc_result *ret);
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))
70#define rmi_type(_in, _out) rmi_type_##_in##_out = RMI_TYPE(_in, _out)
71
Soby Mathewb4c6df42022-11-09 11:13:29 +000072enum rmi_type {
AlexeiFedorov6c119692023-04-21 12:31:15 +010073 rmi_type(0, 0), /* 0 arguments, 0 output values */
74 rmi_type(1, 0), /* 1 argument, 0 output values */
75 rmi_type(2, 0), /* 2 arguments, 0 output values */
76 rmi_type(3, 0), /* 3 arguments, 0 output values */
77 rmi_type(4, 0), /* 4 arguments, 0 output values */
78 rmi_type(5, 0), /* 5 arguments, 0 output values */
79 rmi_type(1, 1), /* 1 argument, 1 output value */
AlexeiFedorov892abce2023-04-06 16:32:12 +010080 rmi_type(2, 2), /* 2 arguments, 2 output values */
81 rmi_type(3, 1), /* 3 arguments, 1 output value */
82 rmi_type(3, 2), /* 3 arguments, 2 output values */
83 rmi_type(3, 4), /* 3 arguments, 4 output values */
84 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),
145 HANDLER(RTT_UNMAP_UNPROTECTED, 3, 0, smc_rtt_unmap_unprotected, false, false),
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),
149 HANDLER(RTT_INIT_RIPAS, 3, 0, smc_rtt_init_ripas, false, true),
150 HANDLER(RTT_SET_RIPAS, 5, 0, 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 }
164#endif
165 return false;
166}
167
Soby Mathewb4c6df42022-11-09 11:13:29 +0000168static void rmi_log_on_exit(unsigned long handler_id,
AlexeiFedorov6c119692023-04-21 12:31:15 +0100169 unsigned long args[],
Soby Mathewb4c6df42022-11-09 11:13:29 +0000170 struct smc_result *ret)
171{
172 const struct smc_handler *handler = &smc_handlers[handler_id];
173 unsigned long function_id = SMC64_RMI_FID(handler_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000174 return_code_t rc;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100175 unsigned int num;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000176
177 if (!handler->log_exec && !handler->log_error) {
178 return;
179 }
180
181 if (function_id == SMC_RMM_VERSION) {
182 /*
183 * RMM_VERSION is special because it returns the
184 * version number, not the error code.
185 */
AlexeiFedorov6c119692023-04-21 12:31:15 +0100186 INFO("SMC_RMM_%-21s > %lx\n", handler->fn_name, ret->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000187 return;
188 }
189
190 rc = unpack_return_code(ret->x[0]);
191
192 if ((handler->log_exec) ||
193 (handler->log_error && (rc.status != RMI_SUCCESS))) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100194 /* Print function name */
195 INFO("SMC_RMM_%-21s", handler->fn_name);
196
197 /* Print arguments */
198 num = handler->type & 0xFF;
199 assert(num <= MAX_NUM_ARGS);
200
201 for (unsigned int i = 0U; i < num; i++) {
202 INFO(" %lx", args[i]);
203 }
204
205 /* Print status */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000206 if (rc.status >= RMI_ERROR_COUNT) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100207 INFO(" > %lx", ret->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000208 } else {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100209 INFO(" > RMI_%s", rmi_status_string[rc.status]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000210 }
211
212 /* Check for index */
213 if (((function_id == SMC_RMM_REC_ENTER) &&
214 (rc.status == RMI_ERROR_REALM)) ||
215 (rc.status == RMI_ERROR_RTT)) {
216 INFO(" %x", rc.index);
AlexeiFedorov6c119692023-04-21 12:31:15 +0100217 } else if (rc.status == RMI_SUCCESS) {
218 /* Print output values */
219 num = (handler->type >> 8) & 0xFF;
220 assert(num <= MAX_NUM_OUTPUT_VALS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000221
AlexeiFedorov6c119692023-04-21 12:31:15 +0100222 for (unsigned int i = 1U; i <= num; i++) {
223 INFO(" %lx", ret->x[i]);
224 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000225 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000226 INFO("\n");
227 }
228}
229
230void handle_ns_smc(unsigned long function_id,
231 unsigned long arg0,
232 unsigned long arg1,
233 unsigned long arg2,
234 unsigned long arg3,
235 unsigned long arg4,
236 unsigned long arg5,
237 struct smc_result *ret)
238{
239 unsigned long handler_id;
240 const struct smc_handler *handler = NULL;
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000241 bool restore_ns_simd_state = false;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000242
Arunachalam Ganapathy937b5492023-02-28 11:17:52 +0000243 /* Ignore SVE hint bit, until RMM supports SVE hint bit */
244 function_id &= ~MASK(SMC_SVE_HINT);
245
Soby Mathewb4c6df42022-11-09 11:13:29 +0000246 if (IS_SMC64_RMI_FID(function_id)) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100247 handler_id = RMI_HANDLER_ID(function_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000248 if (handler_id < ARRAY_LEN(smc_handlers)) {
249 handler = &smc_handlers[handler_id];
250 }
251 }
252
253 /*
254 * Check if handler exists and 'fn_dummy' is not NULL
255 * for not implemented 'function_id' calls in SMC RMI range.
256 */
257 if ((handler == NULL) || (handler->fn_dummy == NULL)) {
258 VERBOSE("[%s] unknown function_id: %lx\n",
259 __func__, function_id);
260 ret->x[0] = SMC_UNKNOWN;
261 return;
262 }
263
264 assert_cpu_slots_empty();
265
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000266 /* Current CPU's SIMD state must not be saved when entering RMM */
267 assert(simd_is_state_saved() == false);
268
269 /* If the handler needs FPU, actively save NS simd context. */
270 if (rmi_handler_needs_fpu(function_id) == true) {
271 simd_save_ns_state();
272 restore_ns_simd_state = true;
273 }
274
Soby Mathewb4c6df42022-11-09 11:13:29 +0000275 switch (handler->type) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100276 case rmi_type_00:
277 ret->x[0] = handler->f_00();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000278 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100279 case rmi_type_10:
280 ret->x[0] = handler->f_10(arg0);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000281 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100282 case rmi_type_20:
283 ret->x[0] = handler->f_20(arg0, arg1);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000284 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100285 case rmi_type_30:
286 ret->x[0] = handler->f_30(arg0, arg1, arg2);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000287 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100288 case rmi_type_40:
289 ret->x[0] = handler->f_40(arg0, arg1, arg2, arg3);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000290 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100291 case rmi_type_50:
292 ret->x[0] = handler->f_50(arg0, arg1, arg2, arg3, arg4);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000293 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100294 case rmi_type_11:
295 handler->f_11(arg0, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000296 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100297 case rmi_type_22:
298 handler->f_22(arg0, arg1, ret);
299 break;
300 case rmi_type_31:
301 handler->f_31(arg0, arg1, arg2, ret);
302 break;
303 case rmi_type_32:
304 handler->f_32(arg0, arg1, arg2, ret);
305 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100306 case rmi_type_34:
307 handler->f_34(arg0, arg1, arg2, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000308 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100309 case rmi_type_41:
310 handler->f_41(arg0, arg1, arg2, arg3, ret);
311 break;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000312 default:
313 assert(false);
314 }
315
316 if (rmi_call_log_enabled) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100317 unsigned long args[] = {arg0, arg1, arg2, arg3, arg4};
318
319 rmi_log_on_exit(handler_id, args, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000320 }
321
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000322 /* If the handler uses FPU, restore the saved NS simd context. */
323 if (restore_ns_simd_state) {
324 simd_restore_ns_state();
325 }
326
327 /* Current CPU's SIMD state must not be saved when exiting RMM */
328 assert(simd_is_state_saved() == false);
329
Soby Mathewb4c6df42022-11-09 11:13:29 +0000330 assert_cpu_slots_empty();
331}
332
333static void report_unexpected(void)
334{
335 unsigned long spsr = read_spsr_el2();
336 unsigned long esr = read_esr_el2();
337 unsigned long elr = read_elr_el2();
338 unsigned long far = read_far_el2();
339
340 INFO("----\n");
341 INFO("Unexpected exception:\n");
342 INFO("SPSR_EL2: 0x%016lx\n", spsr);
343 INFO("ESR_EL2: 0x%016lx\n", esr);
344 INFO("ELR_EL2: 0x%016lx\n", elr);
345 INFO("FAR_EL2: 0x%016lx\n", far);
346 INFO("----\n");
Soby Mathewb4c6df42022-11-09 11:13:29 +0000347}
348
349unsigned long handle_realm_trap(unsigned long *regs)
350{
351 report_unexpected();
352
AlexeiFedorov6c119692023-04-21 12:31:15 +0100353 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000354 wfe();
355 }
356}
357
358/*
359 * Identifies an abort that the RMM may recover from.
360 */
361struct rmm_trap_element {
362 /*
363 * The PC at the time of abort.
364 */
365 unsigned long aborted_pc;
366 /*
367 * New value of the PC.
368 */
369 unsigned long new_pc;
370};
371
372#define RMM_TRAP_HANDLER(_aborted_pc, _new_pc) \
373 { .aborted_pc = (unsigned long)(&_aborted_pc), \
374 .new_pc = (unsigned long)(&_new_pc) }
375
376/*
377 * The registered locations of load/store instructions that access NS memory.
378 */
379extern void *ns_read;
380extern void *ns_write;
381
382/*
383 * The new value of the PC when the GPF occurs on a registered location.
384 */
385extern void *ns_access_ret_0;
386
387struct rmm_trap_element rmm_trap_list[] = {
388 RMM_TRAP_HANDLER(ns_read, ns_access_ret_0),
389 RMM_TRAP_HANDLER(ns_write, ns_access_ret_0),
390};
391#define RMM_TRAP_LIST_SIZE (sizeof(rmm_trap_list)/sizeof(struct rmm_trap_element))
392
393static void fatal_abort(void)
394{
395 report_unexpected();
396
AlexeiFedorov6c119692023-04-21 12:31:15 +0100397 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000398 wfe();
399 }
400}
401
402static bool is_el2_data_abort_gpf(unsigned long esr)
403{
AlexeiFedorov537bee02023-02-02 13:38:23 +0000404 if (((esr & MASK(ESR_EL2_EC)) == ESR_EL2_EC_DATA_ABORT_SEL) &&
AlexeiFedorov6c119692023-04-21 12:31:15 +0100405 ((esr & MASK(ESR_EL2_ABORT_FSC)) == ESR_EL2_ABORT_FSC_GPF)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000406 return true;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100407 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000408 return false;
409}
410
411/*
412 * Handles the RMM's aborts.
413 * It compares the PC at the time of the abort with the registered addresses.
414 * If it finds a match, it returns the new value of the PC that the RMM should
415 * continue from. Other register values are preserved.
416 * If no match is found, it aborts the RMM.
417 */
418unsigned long handle_rmm_trap(void)
419{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000420 unsigned long esr = read_esr_el2();
421 unsigned long elr = read_elr_el2();
422
423 /*
424 * Only the GPF data aborts are recoverable.
425 */
426 if (!is_el2_data_abort_gpf(esr)) {
427 fatal_abort();
428 }
429
AlexeiFedorov6c119692023-04-21 12:31:15 +0100430 for (unsigned int i = 0U; i < RMM_TRAP_LIST_SIZE; i++) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000431 if (rmm_trap_list[i].aborted_pc == elr) {
432 return rmm_trap_list[i].new_pc;
433 }
434 }
435
436 fatal_abort();
AlexeiFedorov6c119692023-04-21 12:31:15 +0100437 return 0UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000438}