blob: 0f16ae7270ef534bc092791aec872d48acede68f [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),
32 RMI_STATUS_STRING(ERROR_RTT),
33 RMI_STATUS_STRING(ERROR_IN_USE)
Soby Mathewb4c6df42022-11-09 11:13:29 +000034};
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);
56typedef void (*handler_1_o)(unsigned long arg0, struct smc_result *ret);
57typedef void (*handler_3_o)(unsigned long arg0, unsigned long arg1,
58 unsigned long arg2, struct smc_result *ret);
59
AlexeiFedorov6c119692023-04-21 12:31:15 +010060/*
61 * SMC RMI handler type encoding:
62 * [0:7] - number of arguments
63 * [8:15] - number of output values
64 */
65#define RMI_TYPE(_in, _out) (_in | (_out << 8))
66#define rmi_type(_in, _out) rmi_type_##_in##_out = RMI_TYPE(_in, _out)
67
Soby Mathewb4c6df42022-11-09 11:13:29 +000068enum rmi_type {
AlexeiFedorov6c119692023-04-21 12:31:15 +010069 rmi_type(0, 0), /* 0 arguments, 0 output values */
70 rmi_type(1, 0), /* 1 argument, 0 output values */
71 rmi_type(2, 0), /* 2 arguments, 0 output values */
72 rmi_type(3, 0), /* 3 arguments, 0 output values */
73 rmi_type(4, 0), /* 4 arguments, 0 output values */
74 rmi_type(5, 0), /* 5 arguments, 0 output values */
75 rmi_type(1, 1), /* 1 argument, 1 output value */
76 rmi_type(3, 4) /* 3 arguments, 4 output values */
Soby Mathewb4c6df42022-11-09 11:13:29 +000077};
78
79struct smc_handler {
80 const char *fn_name;
81 enum rmi_type type;
82 union {
AlexeiFedorov6c119692023-04-21 12:31:15 +010083 handler_0 f_00;
84 handler_1 f_10;
85 handler_2 f_20;
86 handler_3 f_30;
87 handler_4 f_40;
88 handler_5 f_50;
89 handler_1_o f_11;
90 handler_3_o f_34;
Soby Mathewb4c6df42022-11-09 11:13:29 +000091 void *fn_dummy;
92 };
93 bool log_exec; /* print handler execution */
94 bool log_error; /* print in case of error status */
Soby Mathewb4c6df42022-11-09 11:13:29 +000095};
96
97/*
98 * Get handler ID from FID
99 * Precondition: FID is an RMI call
100 */
AlexeiFedorov6c119692023-04-21 12:31:15 +0100101#define RMI_HANDLER_ID(_id) SMC64_FID_OFFSET_FROM_RANGE_MIN(RMI, _id)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000102
AlexeiFedorov6c119692023-04-21 12:31:15 +0100103#define HANDLER(_id, _in, _out, _fn, _exec, _error)[RMI_HANDLER_ID(SMC_RMM_##_id)] = { \
104 .fn_name = #_id, \
105 .type = RMI_TYPE(_in, _out), \
106 .f_##_in##_out = _fn, \
107 .log_exec = _exec, \
108 .log_error = _error \
109}
Soby Mathewb4c6df42022-11-09 11:13:29 +0000110
111/*
112 * The 3rd value enables the execution log.
113 * The 4th value enables the error log.
114 */
115static const struct smc_handler smc_handlers[] = {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100116 HANDLER(VERSION, 0, 0, smc_version, true, true),
117 HANDLER(FEATURES, 1, 1, smc_read_feature_register, true, true),
118 HANDLER(GRANULE_DELEGATE, 1, 0, smc_granule_delegate, false, true),
119 HANDLER(GRANULE_UNDELEGATE, 1, 0, smc_granule_undelegate, false, true),
120 HANDLER(REALM_CREATE, 2, 0, smc_realm_create, true, true),
121 HANDLER(REALM_DESTROY, 1, 0, smc_realm_destroy, true, true),
122 HANDLER(REALM_ACTIVATE, 1, 0, smc_realm_activate, true, true),
123 HANDLER(REC_CREATE, 3, 0, smc_rec_create, true, true),
124 HANDLER(REC_DESTROY, 1, 0, smc_rec_destroy, true, true),
125 HANDLER(REC_ENTER, 2, 0, smc_rec_enter, false, true),
126 HANDLER(DATA_CREATE, 5, 0, smc_data_create, false, false),
127 HANDLER(DATA_CREATE_UNKNOWN, 3, 0, smc_data_create_unknown, false, false),
128 HANDLER(DATA_DESTROY, 2, 0, smc_data_destroy, false, true),
129 HANDLER(RTT_CREATE, 4, 0, smc_rtt_create, false, true),
130 HANDLER(RTT_DESTROY, 4, 0, smc_rtt_destroy, false, true),
131 HANDLER(RTT_FOLD, 4, 0, smc_rtt_fold, false, true),
132 HANDLER(RTT_MAP_UNPROTECTED, 4, 0, smc_rtt_map_unprotected, false, false),
133 HANDLER(RTT_UNMAP_UNPROTECTED, 3, 0, smc_rtt_unmap_unprotected, false, false),
134 HANDLER(RTT_READ_ENTRY, 3, 4, smc_rtt_read_entry, false, true),
135 HANDLER(PSCI_COMPLETE, 2, 0, smc_psci_complete, true, true),
136 HANDLER(REC_AUX_COUNT, 1, 1, smc_rec_aux_count, true, true),
137 HANDLER(RTT_INIT_RIPAS, 3, 0, smc_rtt_init_ripas, false, true),
138 HANDLER(RTT_SET_RIPAS, 5, 0, smc_rtt_set_ripas, false, true)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000139};
140
141COMPILER_ASSERT(ARRAY_LEN(smc_handlers) == SMC64_NUM_FIDS_IN_RANGE(RMI));
142
143static bool rmi_call_log_enabled = true;
144
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000145static inline bool rmi_handler_needs_fpu(unsigned long id)
146{
147#ifdef RMM_FPU_USE_AT_REL2
148 if (id == SMC_RMM_REALM_CREATE || id == SMC_RMM_DATA_CREATE ||
149 id == SMC_RMM_REC_CREATE || id == SMC_RMM_RTT_INIT_RIPAS) {
150 return true;
151 }
152#endif
153 return false;
154}
155
Soby Mathewb4c6df42022-11-09 11:13:29 +0000156static void rmi_log_on_exit(unsigned long handler_id,
AlexeiFedorov6c119692023-04-21 12:31:15 +0100157 unsigned long args[],
Soby Mathewb4c6df42022-11-09 11:13:29 +0000158 struct smc_result *ret)
159{
160 const struct smc_handler *handler = &smc_handlers[handler_id];
161 unsigned long function_id = SMC64_RMI_FID(handler_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000162 return_code_t rc;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100163 unsigned int num;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000164
165 if (!handler->log_exec && !handler->log_error) {
166 return;
167 }
168
169 if (function_id == SMC_RMM_VERSION) {
170 /*
171 * RMM_VERSION is special because it returns the
172 * version number, not the error code.
173 */
AlexeiFedorov6c119692023-04-21 12:31:15 +0100174 INFO("SMC_RMM_%-21s > %lx\n", handler->fn_name, ret->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000175 return;
176 }
177
178 rc = unpack_return_code(ret->x[0]);
179
180 if ((handler->log_exec) ||
181 (handler->log_error && (rc.status != RMI_SUCCESS))) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100182 /* Print function name */
183 INFO("SMC_RMM_%-21s", handler->fn_name);
184
185 /* Print arguments */
186 num = handler->type & 0xFF;
187 assert(num <= MAX_NUM_ARGS);
188
189 for (unsigned int i = 0U; i < num; i++) {
190 INFO(" %lx", args[i]);
191 }
192
193 /* Print status */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000194 if (rc.status >= RMI_ERROR_COUNT) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100195 INFO(" > %lx", ret->x[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000196 } else {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100197 INFO(" > RMI_%s", rmi_status_string[rc.status]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000198 }
199
200 /* Check for index */
201 if (((function_id == SMC_RMM_REC_ENTER) &&
202 (rc.status == RMI_ERROR_REALM)) ||
203 (rc.status == RMI_ERROR_RTT)) {
204 INFO(" %x", rc.index);
AlexeiFedorov6c119692023-04-21 12:31:15 +0100205 } else if (rc.status == RMI_SUCCESS) {
206 /* Print output values */
207 num = (handler->type >> 8) & 0xFF;
208 assert(num <= MAX_NUM_OUTPUT_VALS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000209
AlexeiFedorov6c119692023-04-21 12:31:15 +0100210 for (unsigned int i = 1U; i <= num; i++) {
211 INFO(" %lx", ret->x[i]);
212 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000213 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000214 INFO("\n");
215 }
216}
217
218void handle_ns_smc(unsigned long function_id,
219 unsigned long arg0,
220 unsigned long arg1,
221 unsigned long arg2,
222 unsigned long arg3,
223 unsigned long arg4,
224 unsigned long arg5,
225 struct smc_result *ret)
226{
227 unsigned long handler_id;
228 const struct smc_handler *handler = NULL;
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000229 bool restore_ns_simd_state = false;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000230
Arunachalam Ganapathy937b5492023-02-28 11:17:52 +0000231 /* Ignore SVE hint bit, until RMM supports SVE hint bit */
232 function_id &= ~MASK(SMC_SVE_HINT);
233
Soby Mathewb4c6df42022-11-09 11:13:29 +0000234 if (IS_SMC64_RMI_FID(function_id)) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100235 handler_id = RMI_HANDLER_ID(function_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000236 if (handler_id < ARRAY_LEN(smc_handlers)) {
237 handler = &smc_handlers[handler_id];
238 }
239 }
240
241 /*
242 * Check if handler exists and 'fn_dummy' is not NULL
243 * for not implemented 'function_id' calls in SMC RMI range.
244 */
245 if ((handler == NULL) || (handler->fn_dummy == NULL)) {
246 VERBOSE("[%s] unknown function_id: %lx\n",
247 __func__, function_id);
248 ret->x[0] = SMC_UNKNOWN;
249 return;
250 }
251
252 assert_cpu_slots_empty();
253
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000254 /* Current CPU's SIMD state must not be saved when entering RMM */
255 assert(simd_is_state_saved() == false);
256
257 /* If the handler needs FPU, actively save NS simd context. */
258 if (rmi_handler_needs_fpu(function_id) == true) {
259 simd_save_ns_state();
260 restore_ns_simd_state = true;
261 }
262
Soby Mathewb4c6df42022-11-09 11:13:29 +0000263 switch (handler->type) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100264 case rmi_type_00:
265 ret->x[0] = handler->f_00();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000266 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100267 case rmi_type_10:
268 ret->x[0] = handler->f_10(arg0);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000269 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100270 case rmi_type_20:
271 ret->x[0] = handler->f_20(arg0, arg1);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000272 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100273 case rmi_type_30:
274 ret->x[0] = handler->f_30(arg0, arg1, arg2);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000275 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100276 case rmi_type_40:
277 ret->x[0] = handler->f_40(arg0, arg1, arg2, arg3);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000278 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100279 case rmi_type_50:
280 ret->x[0] = handler->f_50(arg0, arg1, arg2, arg3, arg4);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000281 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100282 case rmi_type_11:
283 handler->f_11(arg0, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000284 break;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100285 case rmi_type_34:
286 handler->f_34(arg0, arg1, arg2, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000287 break;
288 default:
289 assert(false);
290 }
291
292 if (rmi_call_log_enabled) {
AlexeiFedorov6c119692023-04-21 12:31:15 +0100293 unsigned long args[] = {arg0, arg1, arg2, arg3, arg4};
294
295 rmi_log_on_exit(handler_id, args, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000296 }
297
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000298 /* If the handler uses FPU, restore the saved NS simd context. */
299 if (restore_ns_simd_state) {
300 simd_restore_ns_state();
301 }
302
303 /* Current CPU's SIMD state must not be saved when exiting RMM */
304 assert(simd_is_state_saved() == false);
305
Soby Mathewb4c6df42022-11-09 11:13:29 +0000306 assert_cpu_slots_empty();
307}
308
309static void report_unexpected(void)
310{
311 unsigned long spsr = read_spsr_el2();
312 unsigned long esr = read_esr_el2();
313 unsigned long elr = read_elr_el2();
314 unsigned long far = read_far_el2();
315
316 INFO("----\n");
317 INFO("Unexpected exception:\n");
318 INFO("SPSR_EL2: 0x%016lx\n", spsr);
319 INFO("ESR_EL2: 0x%016lx\n", esr);
320 INFO("ELR_EL2: 0x%016lx\n", elr);
321 INFO("FAR_EL2: 0x%016lx\n", far);
322 INFO("----\n");
Soby Mathewb4c6df42022-11-09 11:13:29 +0000323}
324
325unsigned long handle_realm_trap(unsigned long *regs)
326{
327 report_unexpected();
328
AlexeiFedorov6c119692023-04-21 12:31:15 +0100329 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000330 wfe();
331 }
332}
333
334/*
335 * Identifies an abort that the RMM may recover from.
336 */
337struct rmm_trap_element {
338 /*
339 * The PC at the time of abort.
340 */
341 unsigned long aborted_pc;
342 /*
343 * New value of the PC.
344 */
345 unsigned long new_pc;
346};
347
348#define RMM_TRAP_HANDLER(_aborted_pc, _new_pc) \
349 { .aborted_pc = (unsigned long)(&_aborted_pc), \
350 .new_pc = (unsigned long)(&_new_pc) }
351
352/*
353 * The registered locations of load/store instructions that access NS memory.
354 */
355extern void *ns_read;
356extern void *ns_write;
357
358/*
359 * The new value of the PC when the GPF occurs on a registered location.
360 */
361extern void *ns_access_ret_0;
362
363struct rmm_trap_element rmm_trap_list[] = {
364 RMM_TRAP_HANDLER(ns_read, ns_access_ret_0),
365 RMM_TRAP_HANDLER(ns_write, ns_access_ret_0),
366};
367#define RMM_TRAP_LIST_SIZE (sizeof(rmm_trap_list)/sizeof(struct rmm_trap_element))
368
369static void fatal_abort(void)
370{
371 report_unexpected();
372
AlexeiFedorov6c119692023-04-21 12:31:15 +0100373 while (true) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000374 wfe();
375 }
376}
377
378static bool is_el2_data_abort_gpf(unsigned long esr)
379{
AlexeiFedorov537bee02023-02-02 13:38:23 +0000380 if (((esr & MASK(ESR_EL2_EC)) == ESR_EL2_EC_DATA_ABORT_SEL) &&
AlexeiFedorov6c119692023-04-21 12:31:15 +0100381 ((esr & MASK(ESR_EL2_ABORT_FSC)) == ESR_EL2_ABORT_FSC_GPF)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000382 return true;
AlexeiFedorov6c119692023-04-21 12:31:15 +0100383 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000384 return false;
385}
386
387/*
388 * Handles the RMM's aborts.
389 * It compares the PC at the time of the abort with the registered addresses.
390 * If it finds a match, it returns the new value of the PC that the RMM should
391 * continue from. Other register values are preserved.
392 * If no match is found, it aborts the RMM.
393 */
394unsigned long handle_rmm_trap(void)
395{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000396 unsigned long esr = read_esr_el2();
397 unsigned long elr = read_elr_el2();
398
399 /*
400 * Only the GPF data aborts are recoverable.
401 */
402 if (!is_el2_data_abort_gpf(esr)) {
403 fatal_abort();
404 }
405
AlexeiFedorov6c119692023-04-21 12:31:15 +0100406 for (unsigned int i = 0U; i < RMM_TRAP_LIST_SIZE; i++) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000407 if (rmm_trap_list[i].aborted_pc == elr) {
408 return rmm_trap_list[i].new_pc;
409 }
410 }
411
412 fatal_abort();
AlexeiFedorov6c119692023-04-21 12:31:15 +0100413 return 0UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000414}