blob: ab78add01dd30b653495b8cd6765ed2a1ef05b6c [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
5 */
6
7#include <arch.h>
8#include <arch_helpers.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00009#include <buffer.h>
10#include <esr.h>
11#include <exit.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000012#include <gic.h>
13#include <granule.h>
14#include <inject_exp.h>
15#include <memory_alloc.h>
16#include <psci.h>
17#include <realm.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000018#include <rec.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000019#include <rsi-handler.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000020#include <rsi-logger.h>
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000021#include <run.h>
22#include <simd.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000023#include <smc-rmi.h>
24#include <smc-rsi.h>
25#include <status.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000026#include <sysreg_traps.h>
27#include <table.h>
28
29void save_fpu_state(struct fpu_state *fpu);
30void restore_fpu_state(struct fpu_state *fpu);
31
32static void system_abort(void)
33{
34 /*
35 * TODO: report the abort to the EL3.
36 * We need to establish the exact EL3 API first.
37 */
38 assert(false);
39}
40
41static bool fixup_aarch32_data_abort(struct rec *rec, unsigned long *esr)
42{
43 unsigned long spsr = read_spsr_el2();
44
45 if ((spsr & SPSR_EL2_nRW_AARCH32) != 0UL) {
46 /*
47 * mmio emulation of AArch32 reads/writes is not supported.
48 */
49 *esr &= ~ESR_EL2_ABORT_ISV_BIT;
50 return true;
51 }
52 return false;
53}
54
55static unsigned long get_dabt_write_value(struct rec *rec, unsigned long esr)
56{
57 unsigned int rt = esr_srt(esr);
58
59 /* Handle xzr */
60 if (rt == 31U) {
61 return 0UL;
62 }
63 return rec->regs[rt] & access_mask(esr);
64}
65
66/*
67 * Returns 'true' if access from @rec to @addr is within the Protected IPA space.
68 */
69static bool access_in_rec_par(struct rec *rec, unsigned long addr)
70{
71 /*
72 * It is OK to check only the base address of the access because:
73 * - The Protected IPA space starts at address zero.
74 * - The IPA width is below 64 bits, therefore the access cannot
75 * wrap around.
76 */
77 return addr_in_rec_par(rec, addr);
78}
79
80/*
81 * Returns 'true' if the @ipa is in PAR and its RIPAS is 'empty'.
82 *
83 * @ipa must be aligned to the granule size.
84 */
85static bool ipa_is_empty(unsigned long ipa, struct rec *rec)
86{
AlexeiFedorov8b117102023-04-18 15:32:07 +010087 struct s2_walk_result s2_walk;
88 enum s2_walk_status walk_status;
Soby Mathewb4c6df42022-11-09 11:13:29 +000089
90 assert(GRANULE_ALIGNED(ipa));
91
AlexeiFedorov8b117102023-04-18 15:32:07 +010092 walk_status = realm_ipa_to_pa(rec, ipa, &s2_walk);
93
94 if ((walk_status != WALK_INVALID_PARAMS) &&
AlexeiFedorov7ddb27c2023-04-18 16:16:31 +010095 (s2_walk.ripas_val == RIPAS_EMPTY)) {
AlexeiFedorov8b117102023-04-18 15:32:07 +010096 return true;
Soby Mathewb4c6df42022-11-09 11:13:29 +000097 }
AlexeiFedorov8b117102023-04-18 15:32:07 +010098 return false;
Soby Mathewb4c6df42022-11-09 11:13:29 +000099}
100
101static bool fsc_is_external_abort(unsigned long fsc)
102{
103 if (fsc == ESR_EL2_ABORT_FSC_SEA) {
104 return true;
105 }
106
107 if ((fsc >= ESR_EL2_ABORT_FSC_SEA_TTW_START) &&
108 (fsc <= ESR_EL2_ABORT_FSC_SEA_TTW_END)) {
109 return true;
110 }
111
112 return false;
113}
114
115/*
116 * Handles Data/Instruction Aborts at a lower EL with External Abort fault
117 * status code (D/IFSC).
118 * Returns 'true' if the exception is the external abort and the `rec_exit`
119 * structure is populated, 'false' otherwise.
120 */
121static bool handle_sync_external_abort(struct rec *rec,
122 struct rmi_rec_exit *rec_exit,
123 unsigned long esr)
124{
AlexeiFedorov537bee02023-02-02 13:38:23 +0000125 unsigned long fsc = esr & MASK(ESR_EL2_ABORT_FSC);
126 unsigned long set = esr & MASK(ESR_EL2_ABORT_SET);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000127
128 if (!fsc_is_external_abort(fsc)) {
129 return false;
130 }
131
132 switch (set) {
133 case ESR_EL2_ABORT_SET_UER:
134 /*
135 * The recoverable SEA.
136 * Inject the sync. abort into the Realm.
137 * Report the exception to the host.
138 */
139 inject_sync_idabort(ESR_EL2_ABORT_FSC_SEA);
140 /*
141 * Fall through.
142 */
143 case ESR_EL2_ABORT_SET_UEO:
144 /*
145 * The restartable SEA.
146 * Report the exception to the host.
147 * The REC restarts the same instruction.
148 */
149 rec_exit->esr = esr & ESR_NONEMULATED_ABORT_MASK;
150
151 /*
152 * The value of the HPFAR_EL2 is not provided to the host as
153 * it is undefined for external aborts.
154 *
155 * We also don't provide the content of FAR_EL2 because it
156 * has no practical value to the host without the HPFAR_EL2.
157 */
158 break;
159 case ESR_EL2_ABORT_SET_UC:
160 /*
161 * The uncontainable SEA.
162 * Fatal to the system.
163 */
164 system_abort();
165 break;
166 default:
167 assert(false);
168 }
169
170 return true;
171}
172
173void emulate_stage2_data_abort(struct rec *rec,
174 struct rmi_rec_exit *rec_exit,
175 unsigned long rtt_level)
176{
177 unsigned long fipa = rec->regs[1];
178
179 assert(rtt_level <= RTT_PAGE_LEVEL);
180
181 /*
182 * Setup Exception Syndrom Register to emulate a real data abort
183 * and return to NS host to handle it.
184 */
185 rec_exit->esr = (ESR_EL2_EC_DATA_ABORT |
186 (ESR_EL2_ABORT_FSC_TRANSLATION_FAULT_L0 + rtt_level));
187 rec_exit->far = 0UL;
188 rec_exit->hpfar = fipa >> HPFAR_EL2_FIPA_OFFSET;
189 rec_exit->exit_reason = RMI_EXIT_SYNC;
190}
191
192/*
193 * Returns 'true' if the abort is handled and the RMM should return to the Realm,
194 * and returns 'false' if the exception should be reported to the HS host.
195 */
196static bool handle_data_abort(struct rec *rec, struct rmi_rec_exit *rec_exit,
197 unsigned long esr)
198{
199 unsigned long far = 0UL;
200 unsigned long hpfar = read_hpfar_el2();
AlexeiFedorov537bee02023-02-02 13:38:23 +0000201 unsigned long fipa = (hpfar & MASK(HPFAR_EL2_FIPA)) << HPFAR_EL2_FIPA_OFFSET;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000202 unsigned long write_val = 0UL;
203
204 if (handle_sync_external_abort(rec, rec_exit, esr)) {
205 /*
206 * All external aborts are immediately reported to the host.
207 */
208 return false;
209 }
210
211 /*
212 * The memory access that crosses a page boundary may cause two aborts
213 * with `hpfar_el2` values referring to two consecutive pages.
214 *
215 * Insert the SEA and return to the Realm if the granule's RIPAS is EMPTY.
216 */
217 if (ipa_is_empty(fipa, rec)) {
218 inject_sync_idabort(ESR_EL2_ABORT_FSC_SEA);
219 return true;
220 }
221
222 if (fixup_aarch32_data_abort(rec, &esr) ||
223 access_in_rec_par(rec, fipa)) {
224 esr &= ESR_NONEMULATED_ABORT_MASK;
225 goto end;
226 }
227
228 if (esr_is_write(esr)) {
229 write_val = get_dabt_write_value(rec, esr);
230 }
231
232 far = read_far_el2() & ~GRANULE_MASK;
233 esr &= ESR_EMULATED_ABORT_MASK;
234
235end:
236 rec_exit->esr = esr;
237 rec_exit->far = far;
238 rec_exit->hpfar = hpfar;
239 rec_exit->gprs[0] = write_val;
240
241 return false;
242}
243
244/*
245 * Returns 'true' if the abort is handled and the RMM should return to the Realm,
246 * and returns 'false' if the exception should be reported to the NS host.
247 */
248static bool handle_instruction_abort(struct rec *rec, struct rmi_rec_exit *rec_exit,
249 unsigned long esr)
250{
AlexeiFedorov537bee02023-02-02 13:38:23 +0000251 unsigned long fsc = esr & MASK(ESR_EL2_ABORT_FSC);
252 unsigned long fsc_type = fsc & ~MASK(ESR_EL2_ABORT_FSC_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000253 unsigned long hpfar = read_hpfar_el2();
AlexeiFedorov537bee02023-02-02 13:38:23 +0000254 unsigned long fipa = (hpfar & MASK(HPFAR_EL2_FIPA)) << HPFAR_EL2_FIPA_OFFSET;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000255
256 if (handle_sync_external_abort(rec, rec_exit, esr)) {
257 /*
258 * All external aborts are immediately reported to the host.
259 */
260 return false;
261 }
262
263 /*
264 * Insert the SEA and return to the Realm if:
265 * - The instruction abort is at an Unprotected IPA, or
266 * - The granule's RIPAS is EMPTY
267 */
268 if (!access_in_rec_par(rec, fipa) || ipa_is_empty(fipa, rec)) {
269 inject_sync_idabort(ESR_EL2_ABORT_FSC_SEA);
270 return true;
271 }
272
273 if (fsc_type != ESR_EL2_ABORT_FSC_TRANSLATION_FAULT) {
274 unsigned long far = read_far_el2();
275
276 /*
277 * TODO: Should this ever happen, or is it an indication of an
278 * internal consistency failure in the RMM which should lead
279 * to a panic instead?
280 */
281
282 ERROR("Unhandled instruction abort:\n");
283 ERROR(" FSC: %12s0x%02lx\n", " ", fsc);
284 ERROR(" FAR: %16lx\n", far);
285 ERROR(" HPFAR: %16lx\n", hpfar);
286 return false;
287 }
288
289 rec_exit->hpfar = hpfar;
290 rec_exit->esr = esr & ESR_NONEMULATED_ABORT_MASK;
291
292 return false;
293}
294
295/*
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +0000296 * Handle FPU or SVE exceptions.
297 * Returns: true if the exception is handled.
298 */
AlexeiFedorov97844202023-04-27 15:17:35 +0100299static bool handle_simd_exception(simd_t exp_type, struct rec *rec)
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +0000300{
301 /*
302 * If the REC wants to use SVE and if SVE is not enabled for this REC
303 * then inject undefined abort. This can happen when CPU implements
304 * FEAT_SVE but the Realm didn't request this feature during creation.
305 */
306 if (exp_type == SIMD_SVE && rec_simd_type(rec) != SIMD_SVE) {
307 realm_inject_undef_abort();
308 return true;
309 }
310
311 /* FPU or SVE exception can happen only when REC hasn't used SIMD */
312 assert(rec_is_simd_allowed(rec) == false);
313
314 /*
315 * Allow the REC to use SIMD. Save NS SIMD state and restore REC SIMD
316 * state from memory to registers.
317 */
318 simd_save_ns_state();
319 rec_simd_enable_restore(rec);
320
321 /*
322 * Return 'true' indicating that this exception has been handled and
323 * execution can continue.
324 */
325 return true;
326}
327
Soby Mathewb4c6df42022-11-09 11:13:29 +0000328static void advance_pc(void)
329{
330 unsigned long pc = read_elr_el2();
331
332 write_elr_el2(pc + 4UL);
333}
334
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000335static inline bool rsi_handler_needs_fpu(unsigned int id)
336{
337#ifdef RMM_FPU_USE_AT_REL2
AlexeiFedorov97844202023-04-27 15:17:35 +0100338 if ((id == SMC_RSI_ATTEST_TOKEN_CONTINUE) ||
339 (id == SMC_RSI_MEASUREMENT_EXTEND)) {
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000340 return true;
341 }
342#endif
343 return false;
344}
345
Soby Mathewb4c6df42022-11-09 11:13:29 +0000346/*
347 * Return 'true' if execution should continue in the REC, otherwise return
348 * 'false' to go back to the NS caller of REC.Enter.
349 */
350static bool handle_realm_rsi(struct rec *rec, struct rmi_rec_exit *rec_exit)
351{
AlexeiFedorov97844202023-04-27 15:17:35 +0100352 struct rsi_result res = { 0 };
Shruti Gupta9debb132022-12-13 14:38:49 +0000353 unsigned int function_id = (unsigned int)rec->regs[0];
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000354 bool restore_rec_simd_state = false;
AlexeiFedorov97844202023-04-27 15:17:35 +0100355 bool needs_fpu, ret_to_rec;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000356
AlexeiFedorov6c119692023-04-21 12:31:15 +0100357 RSI_LOG_SET(rec->regs);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000358
Arunachalam Ganapathy937b5492023-02-28 11:17:52 +0000359 /* Ignore SVE hint bit, until RMM supports SVE hint bit */
360 function_id &= ~MASK(SMC_SVE_HINT);
361
AlexeiFedorov97844202023-04-27 15:17:35 +0100362 needs_fpu = rsi_handler_needs_fpu(function_id);
363 if (needs_fpu) {
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000364 /*
365 * RSI handler uses FPU at REL2, so actively save REC SIMD state
366 * if REC is using SIMD or NS SIMD state. Restore the same before
367 * return from this function.
368 */
369 if (rec_is_simd_allowed(rec)) {
370 rec_simd_save_disable(rec);
371 restore_rec_simd_state = true;
372 } else {
373 simd_save_ns_state();
374 }
375 } else if (rec_is_simd_allowed(rec)) {
376 /*
377 * If the REC is allowed to access SIMD, then we will enter RMM
378 * with SIMD traps disabled. So enable SIMD traps as RMM by
379 * default runs with SIMD traps enabled
380 */
Arunachalam Ganapathy43c2c6b2023-03-24 15:06:34 +0000381 simd_disable();
382 }
383
Soby Mathewb4c6df42022-11-09 11:13:29 +0000384 switch (function_id) {
385 case SMCCC_VERSION:
AlexeiFedorov97844202023-04-27 15:17:35 +0100386 res.action = UPDATE_REC_RETURN_TO_REALM;
387 res.smc_res.x[0] = SMCCC_VERSION_NUMBER;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000388 break;
389 case SMC_RSI_ABI_VERSION:
AlexeiFedorov97844202023-04-27 15:17:35 +0100390 handle_rsi_version(&res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000391 break;
392 case SMC32_PSCI_FID_MIN ... SMC32_PSCI_FID_MAX:
AlexeiFedorov97844202023-04-27 15:17:35 +0100393 case SMC64_PSCI_FID_MIN ... SMC64_PSCI_FID_MAX:
394 handle_psci(rec, rec_exit, &res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000395 break;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000396 case SMC_RSI_ATTEST_TOKEN_INIT:
AlexeiFedorov97844202023-04-27 15:17:35 +0100397 handle_rsi_attest_token_init(rec, &res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000398 break;
AlexeiFedorov97844202023-04-27 15:17:35 +0100399 case SMC_RSI_ATTEST_TOKEN_CONTINUE:
400 handle_rsi_attest_token_continue(rec, rec_exit, &res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000401 break;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000402 case SMC_RSI_MEASUREMENT_READ:
AlexeiFedorov97844202023-04-27 15:17:35 +0100403 handle_rsi_measurement_read(rec, &res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000404 break;
405 case SMC_RSI_MEASUREMENT_EXTEND:
AlexeiFedorov97844202023-04-27 15:17:35 +0100406 handle_rsi_measurement_extend(rec, &res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000407 break;
AlexeiFedorov97844202023-04-27 15:17:35 +0100408 case SMC_RSI_REALM_CONFIG:
409 handle_rsi_realm_config(rec, &res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000410 break;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000411 case SMC_RSI_IPA_STATE_SET:
AlexeiFedorov97844202023-04-27 15:17:35 +0100412 handle_rsi_ipa_state_set(rec, rec_exit, &res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000413 break;
AlexeiFedorov97844202023-04-27 15:17:35 +0100414 case SMC_RSI_IPA_STATE_GET:
415 handle_rsi_ipa_state_get(rec, &res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000416 break;
AlexeiFedorov97844202023-04-27 15:17:35 +0100417 case SMC_RSI_HOST_CALL:
418 handle_rsi_host_call(rec, rec_exit, &res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000419 break;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000420 default:
AlexeiFedorov97844202023-04-27 15:17:35 +0100421 res.action = UPDATE_REC_RETURN_TO_REALM;
422 res.smc_res.x[0] = SMC_UNKNOWN;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000423 break;
424 }
425
AlexeiFedorov97844202023-04-27 15:17:35 +0100426 if (needs_fpu) {
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000427 if (restore_rec_simd_state == true) {
428 rec_simd_enable_restore(rec);
429 } else {
430 simd_restore_ns_state();
431 }
432 } else if (rec_is_simd_allowed(rec)) {
Arunachalam Ganapathy43c2c6b2023-03-24 15:06:34 +0000433 simd_enable(rec_simd_type(rec));
434 }
435
AlexeiFedorov97844202023-04-27 15:17:35 +0100436 if ((res.action & FLAG_UPDATE_REC) != 0) {
437 for (unsigned int i = 0U; i < SMC_RESULT_REGS; ++i) {
438 rec->regs[i] = res.smc_res.x[i];
439 }
440 }
441
442 if ((res.action & FLAG_STAGE_2_ABORT) != 0) {
443 emulate_stage2_data_abort(rec, rec_exit, res.rtt_level);
444 } else {
445 advance_pc();
446 }
447
448 ret_to_rec = ((res.action & FLAG_EXIT_TO_HOST) == 0);
449
Soby Mathewb4c6df42022-11-09 11:13:29 +0000450 /* Log RSI call */
AlexeiFedorov6c119692023-04-21 12:31:15 +0100451 RSI_LOG_EXIT(function_id, rec->regs, ret_to_rec);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000452 return ret_to_rec;
453}
454
455/*
456 * Return 'true' if the RMM handled the exception,
457 * 'false' to return to the Non-secure host.
458 */
459static bool handle_exception_sync(struct rec *rec, struct rmi_rec_exit *rec_exit)
460{
461 const unsigned long esr = read_esr_el2();
462
AlexeiFedorov537bee02023-02-02 13:38:23 +0000463 switch (esr & MASK(ESR_EL2_EC)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000464 case ESR_EL2_EC_WFX:
AlexeiFedorov537bee02023-02-02 13:38:23 +0000465 rec_exit->esr = esr & (MASK(ESR_EL2_EC) | ESR_EL2_WFx_TI_BIT);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000466 advance_pc();
467 return false;
468 case ESR_EL2_EC_HVC:
469 realm_inject_undef_abort();
470 return true;
471 case ESR_EL2_EC_SMC:
AlexeiFedorov97844202023-04-27 15:17:35 +0100472 return handle_realm_rsi(rec, rec_exit);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000473 case ESR_EL2_EC_SYSREG: {
474 bool ret = handle_sysreg_access_trap(rec, rec_exit, esr);
AlexeiFedorov5b186ad2023-04-26 14:43:18 +0100475
Soby Mathewb4c6df42022-11-09 11:13:29 +0000476 advance_pc();
477 return ret;
478 }
479 case ESR_EL2_EC_INST_ABORT:
480 return handle_instruction_abort(rec, rec_exit, esr);
481 case ESR_EL2_EC_DATA_ABORT:
482 return handle_data_abort(rec, rec_exit, esr);
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +0000483 case ESR_EL2_EC_FPU:
484 return handle_simd_exception(SIMD_FPU, rec);
485 case ESR_EL2_EC_SVE:
486 return handle_simd_exception(SIMD_SVE, rec);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000487 default:
488 /*
489 * TODO: Check if there are other exit reasons we could
490 * encounter here and handle them appropriately
491 */
492 break;
493 }
494
495 VERBOSE("Unhandled sync exit ESR: %08lx (EC: %lx ISS: %lx)\n",
AlexeiFedorov537bee02023-02-02 13:38:23 +0000496 esr, EXTRACT(ESR_EL2_EC, esr), EXTRACT(ESR_EL2_ISS, esr));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000497
498 /*
499 * Zero values in esr, far & hpfar of 'rec_exit' structure
500 * will be returned to the NS host.
501 * The only information that may leak is when there was
502 * some unhandled/unknown reason for the exception.
503 */
504 return false;
505}
506
507/*
508 * Return 'true' if the RMM handled the exception, 'false' to return to the
509 * Non-secure host.
510 */
511static bool handle_exception_serror_lel(struct rec *rec, struct rmi_rec_exit *rec_exit)
512{
513 const unsigned long esr = read_esr_el2();
514
AlexeiFedorov97844202023-04-27 15:17:35 +0100515 if ((esr & ESR_EL2_SERROR_IDS_BIT) != 0UL) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000516 /*
517 * Implementation defined content of the esr.
518 */
519 system_abort();
520 }
521
AlexeiFedorov537bee02023-02-02 13:38:23 +0000522 if ((esr & MASK(ESR_EL2_SERROR_DFSC)) != ESR_EL2_SERROR_DFSC_ASYNC) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000523 /*
524 * Either Uncategorized or Reserved fault status code.
525 */
526 system_abort();
527 }
528
AlexeiFedorov537bee02023-02-02 13:38:23 +0000529 switch (esr & MASK(ESR_EL2_SERROR_AET)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000530 case ESR_EL2_SERROR_AET_UEU: /* Unrecoverable RAS Error */
531 case ESR_EL2_SERROR_AET_UER: /* Recoverable RAS Error */
532 /*
533 * The abort is fatal to the current S/W. Inject the SError into
534 * the Realm so it can e.g. shut down gracefully or localize the
535 * problem at the specific EL0 application.
536 *
537 * Note: Consider shutting down the Realm here to avoid
538 * the host's attack on unstable Realms.
539 */
540 inject_serror(rec, esr);
541 /*
542 * Fall through.
543 */
544 case ESR_EL2_SERROR_AET_CE: /* Corrected RAS Error */
545 case ESR_EL2_SERROR_AET_UEO: /* Restartable RAS Error */
546 /*
547 * Report the exception to the host.
548 */
549 rec_exit->esr = esr & ESR_SERROR_MASK;
550 break;
551 case ESR_EL2_SERROR_AET_UC: /* Uncontainable RAS Error */
552 system_abort();
553 break;
554 default:
555 /*
556 * Unrecognized Asynchronous Error Type
557 */
558 assert(false);
559 }
560
561 return false;
562}
563
564static bool handle_exception_irq_lel(struct rec *rec, struct rmi_rec_exit *rec_exit)
565{
566 (void)rec;
567
568 rec_exit->exit_reason = RMI_EXIT_IRQ;
569
570 /*
571 * With GIC all virtual interrupt programming
572 * must go via the NS hypervisor.
573 */
574 return false;
575}
576
577/* Returns 'true' when returning to Realm (S) and false when to NS */
578bool handle_realm_exit(struct rec *rec, struct rmi_rec_exit *rec_exit, int exception)
579{
580 switch (exception) {
581 case ARM_EXCEPTION_SYNC_LEL: {
582 bool ret;
583
584 /*
585 * TODO: Sanitize ESR to ensure it doesn't leak sensitive
586 * information.
587 */
588 rec_exit->exit_reason = RMI_EXIT_SYNC;
589 ret = handle_exception_sync(rec, rec_exit);
590 if (!ret) {
591 rec->last_run_info.esr = read_esr_el2();
592 rec->last_run_info.far = read_far_el2();
593 rec->last_run_info.hpfar = read_hpfar_el2();
594 }
595 return ret;
596
597 /*
598 * TODO: Much more detailed handling of exit reasons.
599 */
600 }
601 case ARM_EXCEPTION_IRQ_LEL:
602 return handle_exception_irq_lel(rec, rec_exit);
603 case ARM_EXCEPTION_FIQ_LEL:
604 rec_exit->exit_reason = RMI_EXIT_FIQ;
605 break;
606 case ARM_EXCEPTION_SERROR_LEL: {
607 const unsigned long esr = read_esr_el2();
608 bool ret;
609
610 /*
611 * TODO: Sanitize ESR to ensure it doesn't leak sensitive
612 * information.
613 */
614 rec_exit->exit_reason = RMI_EXIT_SERROR;
615 ret = handle_exception_serror_lel(rec, rec_exit);
616 if (!ret) {
617 rec->last_run_info.esr = esr;
618 rec->last_run_info.far = read_far_el2();
619 rec->last_run_info.hpfar = read_hpfar_el2();
620 }
621 return ret;
622 }
623 default:
624 INFO("Unrecognized exit reason: %d\n", exception);
625 break;
AlexeiFedorov97844202023-04-27 15:17:35 +0100626 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000627
628 return false;
629}