blob: 9550bb2d7c582d172157c5ea72dd58564fa78fca [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),
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 }
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);
AlexeiFedorov697445b2023-04-25 15:27:57 +0100217 }
218
219 if ((rc.status == RMI_SUCCESS) ||
220 ((rc.status == RMI_ERROR_RTT) &&
221 ((function_id == SMC_RMM_RTT_DESTROY) ||
222 (function_id == SMC_RMM_DATA_DESTROY)))) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100223 /* Print output values */
224 num = (handler->type >> 8) & 0xFF;
225 assert(num <= MAX_NUM_OUTPUT_VALS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000226
AlexeiFedorov6c119692023-04-21 12:31:15 +0100227 for (unsigned int i = 1U; i <= num; i++) {
228 INFO(" %lx", ret->x[i]);
229 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000230 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000231 INFO("\n");
232 }
233}
234
235void handle_ns_smc(unsigned long function_id,
236 unsigned long arg0,
237 unsigned long arg1,
238 unsigned long arg2,
239 unsigned long arg3,
240 unsigned long arg4,
241 unsigned long arg5,
242 struct smc_result *ret)
243{
244 unsigned long handler_id;
245 const struct smc_handler *handler = NULL;
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000246 bool restore_ns_simd_state = false;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000247
Arunachalam Ganapathy937b5492023-02-28 11:17:52 +0000248 /* Ignore SVE hint bit, until RMM supports SVE hint bit */
249 function_id &= ~MASK(SMC_SVE_HINT);
250
Soby Mathewb4c6df42022-11-09 11:13:29 +0000251 if (IS_SMC64_RMI_FID(function_id)) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100252 handler_id = RMI_HANDLER_ID(function_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000253 if (handler_id < ARRAY_LEN(smc_handlers)) {
254 handler = &smc_handlers[handler_id];
255 }
256 }
257
258 /*
259 * Check if handler exists and 'fn_dummy' is not NULL
260 * for not implemented 'function_id' calls in SMC RMI range.
261 */
262 if ((handler == NULL) || (handler->fn_dummy == NULL)) {
263 VERBOSE("[%s] unknown function_id: %lx\n",
264 __func__, function_id);
265 ret->x[0] = SMC_UNKNOWN;
266 return;
267 }
268
269 assert_cpu_slots_empty();
270
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000271 /* Current CPU's SIMD state must not be saved when entering RMM */
272 assert(simd_is_state_saved() == false);
273
274 /* If the handler needs FPU, actively save NS simd context. */
275 if (rmi_handler_needs_fpu(function_id) == true) {
276 simd_save_ns_state();
277 restore_ns_simd_state = true;
278 }
279
Soby Mathewb4c6df42022-11-09 11:13:29 +0000280 switch (handler->type) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100281 case rmi_type_00:
282 ret->x[0] = handler->f_00();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000283 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100284 case rmi_type_10:
285 ret->x[0] = handler->f_10(arg0);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000286 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100287 case rmi_type_20:
288 ret->x[0] = handler->f_20(arg0, arg1);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000289 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100290 case rmi_type_30:
291 ret->x[0] = handler->f_30(arg0, arg1, arg2);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000292 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100293 case rmi_type_40:
294 ret->x[0] = handler->f_40(arg0, arg1, arg2, arg3);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000295 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100296 case rmi_type_50:
297 ret->x[0] = handler->f_50(arg0, arg1, arg2, arg3, arg4);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000298 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100299 case rmi_type_11:
300 handler->f_11(arg0, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000301 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100302 case rmi_type_22:
303 handler->f_22(arg0, arg1, ret);
304 break;
305 case rmi_type_31:
306 handler->f_31(arg0, arg1, arg2, ret);
307 break;
308 case rmi_type_32:
309 handler->f_32(arg0, arg1, arg2, ret);
310 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100311 case rmi_type_34:
312 handler->f_34(arg0, arg1, arg2, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000313 break;
AlexeiFedorov892abce2023-04-06 16:32:12 +0100314 case rmi_type_41:
315 handler->f_41(arg0, arg1, arg2, arg3, ret);
316 break;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000317 default:
318 assert(false);
319 }
320
321 if (rmi_call_log_enabled) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100322 unsigned long args[] = {arg0, arg1, arg2, arg3, arg4};
323
324 rmi_log_on_exit(handler_id, args, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000325 }
326
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000327 /* If the handler uses FPU, restore the saved NS simd context. */
328 if (restore_ns_simd_state) {
329 simd_restore_ns_state();
330 }
331
332 /* Current CPU's SIMD state must not be saved when exiting RMM */
333 assert(simd_is_state_saved() == false);
334
Soby Mathewb4c6df42022-11-09 11:13:29 +0000335 assert_cpu_slots_empty();
336}
337
338static void report_unexpected(void)
339{
340 unsigned long spsr = read_spsr_el2();
341 unsigned long esr = read_esr_el2();
342 unsigned long elr = read_elr_el2();
343 unsigned long far = read_far_el2();
344
345 INFO("----\n");
346 INFO("Unexpected exception:\n");
347 INFO("SPSR_EL2: 0x%016lx\n", spsr);
348 INFO("ESR_EL2: 0x%016lx\n", esr);
349 INFO("ELR_EL2: 0x%016lx\n", elr);
350 INFO("FAR_EL2: 0x%016lx\n", far);
351 INFO("----\n");
Soby Mathewb4c6df42022-11-09 11:13:29 +0000352}
353
354unsigned long handle_realm_trap(unsigned long *regs)
355{
356 report_unexpected();
357
AlexeiFedorov6c119692023-04-21 12:31:15 +0100358 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000359 wfe();
360 }
361}
362
363/*
364 * Identifies an abort that the RMM may recover from.
365 */
366struct rmm_trap_element {
367 /*
368 * The PC at the time of abort.
369 */
370 unsigned long aborted_pc;
371 /*
372 * New value of the PC.
373 */
374 unsigned long new_pc;
375};
376
377#define RMM_TRAP_HANDLER(_aborted_pc, _new_pc) \
378 { .aborted_pc = (unsigned long)(&_aborted_pc), \
379 .new_pc = (unsigned long)(&_new_pc) }
380
381/*
382 * The registered locations of load/store instructions that access NS memory.
383 */
384extern void *ns_read;
385extern void *ns_write;
386
387/*
388 * The new value of the PC when the GPF occurs on a registered location.
389 */
390extern void *ns_access_ret_0;
391
392struct rmm_trap_element rmm_trap_list[] = {
393 RMM_TRAP_HANDLER(ns_read, ns_access_ret_0),
394 RMM_TRAP_HANDLER(ns_write, ns_access_ret_0),
395};
396#define RMM_TRAP_LIST_SIZE (sizeof(rmm_trap_list)/sizeof(struct rmm_trap_element))
397
398static void fatal_abort(void)
399{
400 report_unexpected();
401
AlexeiFedorov6c119692023-04-21 12:31:15 +0100402 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000403 wfe();
404 }
405}
406
407static bool is_el2_data_abort_gpf(unsigned long esr)
408{
AlexeiFedorov537bee02023-02-02 13:38:23 +0000409 if (((esr & MASK(ESR_EL2_EC)) == ESR_EL2_EC_DATA_ABORT_SEL) &&
AlexeiFedorov6c119692023-04-21 12:31:15 +0100410 ((esr & MASK(ESR_EL2_ABORT_FSC)) == ESR_EL2_ABORT_FSC_GPF)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000411 return true;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100412 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000413 return false;
414}
415
416/*
417 * Handles the RMM's aborts.
418 * It compares the PC at the time of the abort with the registered addresses.
419 * If it finds a match, it returns the new value of the PC that the RMM should
420 * continue from. Other register values are preserved.
421 * If no match is found, it aborts the RMM.
422 */
423unsigned long handle_rmm_trap(void)
424{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000425 unsigned long esr = read_esr_el2();
426 unsigned long elr = read_elr_el2();
427
428 /*
429 * Only the GPF data aborts are recoverable.
430 */
431 if (!is_el2_data_abort_gpf(esr)) {
432 fatal_abort();
433 }
434
AlexeiFedorov6c119692023-04-21 12:31:15 +0100435 for (unsigned int i = 0U; i < RMM_TRAP_LIST_SIZE; i++) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000436 if (rmm_trap_list[i].aborted_pc == elr) {
437 return rmm_trap_list[i].new_pc;
438 }
439 }
440
441 fatal_abort();
AlexeiFedorov6c119692023-04-21 12:31:15 +0100442 return 0UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000443}