blob: 6c013560771024f8db5c56e0c835ffbff01d90aa [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scullc960c032018-10-24 15:13:35 +01009#include <stdnoreturn.h>
10
Andrew Walbran1f32e722019-06-07 17:57:26 +010011#include "hf/arch/barriers.h"
Andrew Scullc960c032018-10-24 15:13:35 +010012#include "hf/arch/init.h"
Olivier Deprez98ad2d22020-05-20 09:52:43 +020013#include "hf/arch/mmu.h"
Andrew Scull07b6bd32019-12-12 17:19:55 +000014#include "hf/arch/plat/smc.h"
Andrew Scullc960c032018-10-24 15:13:35 +010015
Andrew Scull18c78fc2018-08-20 12:57:41 +010016#include "hf/api.h"
Fuad Tabbac76466d2019-09-06 10:42:12 +010017#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010018#include "hf/cpu.h"
19#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010020#include "hf/ffa.h"
Andrew Sculla9c172d2019-04-03 14:10:00 +010021#include "hf/panic.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010022#include "hf/vm.h"
23
Andrew Scullf35a5c92018-08-07 18:09:46 +010024#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010025
Fuad Tabbac76466d2019-09-06 10:42:12 +010026#include "debug_el1.h"
Fuad Tabba77a4b012019-11-15 12:13:08 +000027#include "feature_id.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010028#include "msr.h"
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +010029#include "perfmon.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010030#include "psci.h"
Andrew Walbran33645652019-04-15 12:29:31 +010031#include "psci_handler.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000032#include "smc.h"
Fuad Tabbaba8c44d2019-09-23 14:38:58 +010033#include "sysregs.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010034
Fuad Tabbac76466d2019-09-06 10:42:12 +010035/**
Olivier Deprez98ad2d22020-05-20 09:52:43 +020036 * Hypervisor Fault Address Register Non-Secure.
37 */
38#define HPFAR_EL2_NS (UINT64_C(0x1) << 63)
39
40/**
41 * Hypervisor Fault Address Register Faulting IPA.
42 */
43#define HPFAR_EL2_FIPA (UINT64_C(0xFFFFFFFFFF0))
44
45/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010046 * Gets the value to increment for the next PC.
47 * The ESR encodes whether the instruction is 2 bytes or 4 bytes long.
48 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +000049#define GET_NEXT_PC_INC(esr) (GET_ESR_IL(esr) ? 4 : 2)
Fuad Tabbac76466d2019-09-06 10:42:12 +010050
Fuad Tabbac76466d2019-09-06 10:42:12 +010051/**
Andrew Walbran0dd67ff2019-09-12 16:38:50 +010052 * The Client ID field within X7 for an SMC64 call.
53 */
54#define CLIENT_ID_MASK UINT64_C(0xffff)
55
56/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010057 * Returns a reference to the currently executing vCPU.
58 */
Andrew Scullc960c032018-10-24 15:13:35 +010059static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000060{
61 return (struct vcpu *)read_msr(tpidr_el2);
62}
63
Andrew Walbran1f8d4872018-12-20 11:21:32 +000064/**
65 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
66 * informs the arch-independent sections that registers have been saved.
67 */
68void complete_saving_state(struct vcpu *vcpu)
69{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010070 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
71 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000072
73 api_regs_state_saved(vcpu);
74
75 /*
76 * If switching away from the primary, copy the current EL0 virtual
77 * timer registers to the corresponding EL2 physical timer registers.
78 * This is used to emulate the virtual timer for the primary in case it
79 * should fire while the secondary is running.
80 */
81 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
82 /*
83 * Clear timer control register before copying compare value, to
84 * avoid a spurious timer interrupt. This could be a problem if
85 * the interrupt is configured as edge-triggered, as it would
86 * then be latched in.
87 */
88 write_msr(cnthp_ctl_el2, 0);
89 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
90 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
91 }
92}
93
94/**
95 * Restores the state of per-vCPU peripherals, such as the virtual timer.
96 */
97void begin_restoring_state(struct vcpu *vcpu)
98{
99 /*
100 * Clear timer control register before restoring compare value, to avoid
101 * a spurious timer interrupt. This could be a problem if the interrupt
102 * is configured as edge-triggered, as it would then be latched in.
103 */
104 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100105 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
106 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000107
108 /*
109 * If we are switching (back) to the primary, disable the EL2 physical
110 * timer which was being used to emulate the EL0 virtual timer, as the
111 * virtual timer is now running for the primary again.
112 */
113 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
114 write_msr(cnthp_ctl_el2, 0);
115 write_msr(cnthp_cval_el2, 0);
116 }
117}
118
Andrew Walbran1f32e722019-06-07 17:57:26 +0100119/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100120 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
121 * current VMID.
122 */
123static void invalidate_vm_tlb(void)
124{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100125 /*
126 * Ensure that the last VTTBR write has taken effect so we invalidate
127 * the right set of TLB entries.
128 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100129 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100130
Andrew Walbran1f32e722019-06-07 17:57:26 +0100131 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100132
133 /*
134 * Ensure that no instructions are fetched for the VM until after the
135 * TLB invalidation has taken effect.
136 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100137 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100138
139 /*
140 * Ensure that no data reads or writes for the VM happen until after the
Fuad Tabba77a4b012019-11-15 12:13:08 +0000141 * TLB invalidation has taken effect. Non-shareable is enough because
142 * the TLB is local to the CPU.
Andrew Walbrancff1f682019-07-04 14:52:45 +0100143 */
David Brazdil851948e2019-08-09 12:02:12 +0100144 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100145}
146
147/**
148 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
149 * the same VM which was run on the current pCPU.
150 *
151 * This is necessary because VMs may (contrary to the architecture
152 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
153 * workaround:
154 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
155 */
156void maybe_invalidate_tlb(struct vcpu *vcpu)
157{
158 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100159 ffa_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100160
161 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
162 new_vcpu_index) {
163 /*
164 * The vCPU has changed since the last time this VM was run on
165 * this pCPU, so we need to invalidate the TLB.
166 */
167 invalidate_vm_tlb();
168
169 /* Record the fact that this vCPU is now running on this CPU. */
170 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
171 new_vcpu_index;
172 }
173}
174
David Brazdil768f69c2019-12-19 15:46:12 +0000175noreturn void irq_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100176{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000177 (void)elr;
178 (void)spsr;
179
Fuad Tabbad1d67982020-01-08 11:28:29 +0000180 panic("IRQ from current exception level.");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100181}
182
David Brazdil768f69c2019-12-19 15:46:12 +0000183noreturn void fiq_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100184{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000185 (void)elr;
186 (void)spsr;
187
Fuad Tabbad1d67982020-01-08 11:28:29 +0000188 panic("FIQ from current exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000189}
190
David Brazdil768f69c2019-12-19 15:46:12 +0000191noreturn void serr_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000192{
193 (void)elr;
194 (void)spsr;
195
Fuad Tabbad1d67982020-01-08 11:28:29 +0000196 panic("SError from current exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000197}
198
David Brazdil768f69c2019-12-19 15:46:12 +0000199noreturn void sync_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000200{
201 uintreg_t esr = read_msr(esr_el2);
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000202 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000203
204 (void)spsr;
205
Fuad Tabbac76466d2019-09-06 10:42:12 +0100206 switch (ec) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000207 case EC_DATA_ABORT_SAME_EL:
Andrew Walbrane52006c2019-10-22 18:01:28 +0100208 if (!(esr & (1U << 10))) { /* Check FnV bit. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000209 dlog_error(
210 "Data abort: pc=%#x, esr=%#x, ec=%#x, "
211 "far=%#x\n",
212 elr, esr, ec, read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100213 } else {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000214 dlog_error(
215 "Data abort: pc=%#x, esr=%#x, ec=%#x, "
216 "far=invalid\n",
217 elr, esr, ec);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100218 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100219
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000220 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100221
222 default:
Andrew Walbran17eebf92020-02-05 16:35:49 +0000223 dlog_error(
224 "Unknown current sync exception pc=%#x, esr=%#x, "
225 "ec=%#x\n",
226 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100227 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100228 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000229
Andrew Sculla9c172d2019-04-03 14:10:00 +0100230 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100231}
232
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100233/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000234 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
235 * arch_regs.
236 */
237static void set_virtual_interrupt(struct arch_regs *r, bool enable)
238{
239 if (enable) {
240 r->lazy.hcr_el2 |= HCR_EL2_VI;
241 } else {
242 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
243 }
244}
245
246/**
247 * Sets or clears the VI bit in the HCR_EL2 register.
248 */
249static void set_virtual_interrupt_current(bool enable)
250{
251 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000252
Andrew Walbran3d84a262018-12-13 14:41:19 +0000253 if (enable) {
254 hcr_el2 |= HCR_EL2_VI;
255 } else {
256 hcr_el2 &= ~HCR_EL2_VI;
257 }
258 write_msr(hcr_el2, hcr_el2);
259}
260
Andrew Scullae9962e2019-10-03 16:51:16 +0100261/**
262 * Checks whether to block an SMC being forwarded from a VM.
263 */
264static bool smc_is_blocked(const struct vm *vm, uint32_t func)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100265{
Andrew Scullae9962e2019-10-03 16:51:16 +0100266 bool block_by_default = !vm->smc_whitelist.permissive;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100267
Andrew Scullae9962e2019-10-03 16:51:16 +0100268 for (size_t i = 0; i < vm->smc_whitelist.smc_count; ++i) {
269 if (func == vm->smc_whitelist.smcs[i]) {
270 return false;
271 }
272 }
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100273
Andrew Walbran17eebf92020-02-05 16:35:49 +0000274 dlog_notice("SMC %#010x attempted from VM %d, blocked=%d\n", func,
275 vm->id, block_by_default);
Andrew Scullae9962e2019-10-03 16:51:16 +0100276
277 /* Access is still allowed in permissive mode. */
278 return block_by_default;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100279}
280
281/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100282 * Applies SMC access control according to manifest and forwards the call if
283 * access is granted.
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100284 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100285static void smc_forwarder(const struct vm *vm, struct ffa_value *args)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100286{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100287 struct ffa_value ret;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000288 uint32_t client_id = vm->id;
289 uintreg_t arg7 = args->arg7;
Andrew Scullae9962e2019-10-03 16:51:16 +0100290
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000291 if (smc_is_blocked(vm, args->func)) {
292 args->func = SMCCC_ERROR_UNKNOWN;
Andrew Scullae9962e2019-10-03 16:51:16 +0100293 return;
294 }
295
Andrew Walbran0dd67ff2019-09-12 16:38:50 +0100296 /*
297 * Set the Client ID but keep the existing Secure OS ID and anything
298 * else (currently unspecified) that the client may have passed in the
299 * upper bits.
300 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000301 args->arg7 = client_id | (arg7 & ~CLIENT_ID_MASK);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000302 ret = smc_forward(args->func, args->arg1, args->arg2, args->arg3,
303 args->arg4, args->arg5, args->arg6, args->arg7);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100304
Andrew Scullae9962e2019-10-03 16:51:16 +0100305 /*
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000306 * Preserve the value passed by the caller, rather than the generated
307 * client_id. Note that this would also overwrite any return value that
Andrew Scullae9962e2019-10-03 16:51:16 +0100308 * may be in x7, but the SMCs that we are forwarding are legacy calls
309 * from before SMCCC 1.2 so won't have more than 4 return values anyway.
310 */
Andrew Scull07b6bd32019-12-12 17:19:55 +0000311 ret.arg7 = arg7;
312
313 plat_smc_post_forward(*args, &ret);
314
315 *args = ret;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100316}
317
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100318static bool ffa_handler(struct ffa_value *args, struct vcpu **next)
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100319{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000320 uint32_t func = args->func & ~SMCCC_CONVENTION_MASK;
321
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100322 /*
323 * NOTE: When adding new methods to this handler update
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100324 * api_ffa_features accordingly.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100325 */
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000326 switch (func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100327 case FFA_VERSION_32:
328 *args = api_ffa_version(args->arg1);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100329 return true;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100330 case FFA_PARTITION_INFO_GET_32: {
331 struct ffa_uuid uuid;
332
333 ffa_uuid_init(args->arg1, args->arg2, args->arg3, args->arg4,
334 &uuid);
335 *args = api_ffa_partition_info_get(current(), &uuid);
336 return true;
337 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338 case FFA_ID_GET_32:
339 *args = api_ffa_id_get(current());
Andrew Walbrand230f662019-10-07 18:03:36 +0100340 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100341 case FFA_FEATURES_32:
342 *args = api_ffa_features(args->arg1);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100343 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100344 case FFA_RX_RELEASE_32:
345 *args = api_ffa_rx_release(current(), next);
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +0000346 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100347 case FFA_RXTX_MAP_32:
348 *args = api_ffa_rxtx_map(ipa_init(args->arg1),
349 ipa_init(args->arg2), args->arg3,
350 current(), next);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000351 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100352 case FFA_YIELD_32:
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000353 *args = api_yield(current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100354 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100355 case FFA_MSG_SEND_32:
356 *args = api_ffa_msg_send(
357 ffa_msg_send_sender(*args),
358 ffa_msg_send_receiver(*args), ffa_msg_send_size(*args),
359 ffa_msg_send_attributes(*args), current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100360 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100361 case FFA_MSG_WAIT_32:
362 *args = api_ffa_msg_recv(true, current(), next);
Andrew Walbran0de4f162019-09-03 16:44:20 +0100363 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364 case FFA_MSG_POLL_32:
365 *args = api_ffa_msg_recv(false, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100366 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100367 case FFA_RUN_32:
368 *args = api_ffa_run(ffa_vm_id(*args), ffa_vcpu_index(*args),
369 current(), next);
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100370 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100371 case FFA_MEM_DONATE_32:
372 case FFA_MEM_LEND_32:
373 case FFA_MEM_SHARE_32:
374 *args = api_ffa_mem_send(func, args->arg1, args->arg2,
375 ipa_init(args->arg3), args->arg4,
Andrew Walbran1a86aa92020-05-15 17:22:28 +0100376 current());
Andrew Walbran82d6d152019-12-24 15:02:06 +0000377 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100378 case FFA_MEM_RETRIEVE_REQ_32:
379 *args = api_ffa_mem_retrieve_req(args->arg1, args->arg2,
380 ipa_init(args->arg3),
381 args->arg4, current());
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000382 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383 case FFA_MEM_RELINQUISH_32:
384 *args = api_ffa_mem_relinquish(current());
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000385 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100386 case FFA_MEM_RECLAIM_32:
387 *args = api_ffa_mem_reclaim(
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100388 ffa_assemble_handle(args->arg1, args->arg2), args->arg3,
389 current());
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000390 return true;
Andrew Walbranca808b12020-05-15 17:22:28 +0100391 case FFA_MEM_FRAG_RX_32:
392 *args = api_ffa_mem_frag_rx(ffa_frag_handle(*args), args->arg3,
393 (args->arg4 >> 16) & 0xffff,
394 current());
395 return true;
396 case FFA_MEM_FRAG_TX_32:
397 *args = api_ffa_mem_frag_tx(ffa_frag_handle(*args), args->arg3,
398 (args->arg4 >> 16) & 0xffff,
399 current());
400 return true;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000401 case FFA_MSG_SEND_DIRECT_REQ_32:
402 *args = api_ffa_msg_send_direct_req(
403 ffa_msg_send_sender(*args),
404 ffa_msg_send_receiver(*args), *args, current(), next);
405 return true;
406 case FFA_MSG_SEND_DIRECT_RESP_32:
407 *args = api_ffa_msg_send_direct_resp(
408 ffa_msg_send_sender(*args),
409 ffa_msg_send_receiver(*args), *args, current(), next);
410 return true;
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100411 }
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100412
413 return false;
414}
415
416/**
417 * Set or clear VI bit according to pending interrupts.
418 */
419static void update_vi(struct vcpu *next)
420{
421 if (next == NULL) {
422 /*
423 * Not switching vCPUs, set the bit for the current vCPU
424 * directly in the register.
425 */
426 struct vcpu *vcpu = current();
427
428 sl_lock(&vcpu->lock);
429 set_virtual_interrupt_current(
430 vcpu->interrupts.enabled_and_pending_count > 0);
431 sl_unlock(&vcpu->lock);
432 } else {
433 /*
434 * About to switch vCPUs, set the bit for the vCPU to which we
435 * are switching in the saved copy of the register.
436 */
437 sl_lock(&next->lock);
438 set_virtual_interrupt(
439 &next->regs,
440 next->interrupts.enabled_and_pending_count > 0);
441 sl_unlock(&next->lock);
442 }
443}
444
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100445/**
446 * Processes SMC instruction calls.
447 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000448static struct vcpu *smc_handler(struct vcpu *vcpu)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100449{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100450 struct ffa_value args = {
Andrew Walbran85c37662019-12-05 16:29:33 +0000451 .func = vcpu->regs.r[0],
452 .arg1 = vcpu->regs.r[1],
453 .arg2 = vcpu->regs.r[2],
454 .arg3 = vcpu->regs.r[3],
455 .arg4 = vcpu->regs.r[4],
456 .arg5 = vcpu->regs.r[5],
457 .arg6 = vcpu->regs.r[6],
458 .arg7 = vcpu->regs.r[7],
459 };
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000460 struct vcpu *next = NULL;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100461
Andrew Walbran85c37662019-12-05 16:29:33 +0000462 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000463 &vcpu->regs.r[0], &next)) {
464 return next;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100465 }
466
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100467 if (ffa_handler(&args, &next)) {
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000468 arch_regs_set_retval(&vcpu->regs, args);
469 update_vi(next);
470 return next;
Andrew Walbran4579f7002019-08-30 16:24:58 +0100471 }
472
Andrew Walbran85c37662019-12-05 16:29:33 +0000473 switch (args.func & ~SMCCC_CONVENTION_MASK) {
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100474 case HF_DEBUG_LOG:
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000475 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000476 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100477 }
478
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000479 smc_forwarder(vcpu->vm, &args);
480 arch_regs_set_retval(&vcpu->regs, args);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000481 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100482}
483
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000484/*
485 * Exception vector offsets.
486 * See Arm Architecture Reference Manual Armv8-A, D1.10.2.
487 */
488
489/**
490 * Offset for synchronous exceptions at current EL with SPx.
491 */
492#define OFFSET_CURRENT_SPX UINT64_C(0x200)
493
494/**
495 * Offset for synchronous exceptions at lower EL using AArch64.
496 */
497#define OFFSET_LOWER_EL_64 UINT64_C(0x400)
498
499/**
500 * Offset for synchronous exceptions at lower EL using AArch32.
501 */
502#define OFFSET_LOWER_EL_32 UINT64_C(0x600)
503
504/**
505 * Returns the address for the exception handler at EL1.
506 */
507static uintreg_t get_el1_exception_handler_addr(const struct vcpu *vcpu)
508{
509 uintreg_t base_addr = read_msr(vbar_el1);
510 uintreg_t pe_mode = vcpu->regs.spsr & PSR_PE_MODE_MASK;
511 bool is_arch32 = vcpu->regs.spsr & PSR_ARCH_MODE_32;
512
513 if (pe_mode == PSR_PE_MODE_EL0T) {
514 if (is_arch32) {
515 base_addr += OFFSET_LOWER_EL_32;
516 } else {
517 base_addr += OFFSET_LOWER_EL_64;
518 }
519 } else {
520 CHECK(!is_arch32);
521 base_addr += OFFSET_CURRENT_SPX;
522 }
523
524 return base_addr;
525}
526
527/**
Fuad Tabbab86325a2020-01-10 13:38:15 +0000528 * Injects an exception with the specified Exception Syndrom Register value into
529 * the EL1.
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000530 *
531 * NOTE: This function assumes that the lazy registers haven't been saved, and
532 * writes to the lazy registers of the CPU directly instead of the vCPU.
533 */
Fuad Tabbac3847c72020-08-11 09:32:25 +0100534static void inject_el1_exception(struct vcpu *vcpu, uintreg_t esr_el1_value,
535 uintreg_t far_el1_value)
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000536{
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000537 uintreg_t handler_address = get_el1_exception_handler_addr(vcpu);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000538
539 /* Update the CPU state to inject the exception. */
540 write_msr(esr_el1, esr_el1_value);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100541 write_msr(far_el1, far_el1_value);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000542 write_msr(elr_el1, vcpu->regs.pc);
543 write_msr(spsr_el1, vcpu->regs.spsr);
544
545 /*
546 * Mask (disable) interrupts and run in EL1h mode.
547 * EL1h mode is used because by default, taking an exception selects the
548 * stack pointer for the target Exception level. The software can change
549 * that later in the handler if needed.
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000550 */
551 vcpu->regs.spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
552
553 /* Transfer control to the exception hander. */
554 vcpu->regs.pc = handler_address;
Fuad Tabbab86325a2020-01-10 13:38:15 +0000555}
556
557/**
558 * Injects a Data Abort exception (same exception level).
559 */
560static void inject_el1_data_abort_exception(struct vcpu *vcpu,
Fuad Tabbac3847c72020-08-11 09:32:25 +0100561 uintreg_t esr_el2,
562 uintreg_t far_el2)
Fuad Tabbab86325a2020-01-10 13:38:15 +0000563{
564 /*
565 * ISS encoding remains the same, but the EC is changed to reflect
566 * where the exception came from.
567 * See Arm Architecture Reference Manual Armv8-A, pages D13-2943/2982.
568 */
569 uintreg_t esr_el1_value = GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) |
570 (EC_DATA_ABORT_SAME_EL << ESR_EC_OFFSET);
571
Andrew Walbran17eebf92020-02-05 16:35:49 +0000572 dlog_notice("Injecting Data Abort exception into VM%d.\n",
573 vcpu->vm->id);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000574
Fuad Tabbac3847c72020-08-11 09:32:25 +0100575 inject_el1_exception(vcpu, esr_el1_value, far_el2);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000576}
577
578/**
579 * Injects a Data Abort exception (same exception level).
580 */
581static void inject_el1_instruction_abort_exception(struct vcpu *vcpu,
Fuad Tabbac3847c72020-08-11 09:32:25 +0100582 uintreg_t esr_el2,
583 uintreg_t far_el2)
Fuad Tabbab86325a2020-01-10 13:38:15 +0000584{
585 /*
586 * ISS encoding remains the same, but the EC is changed to reflect
587 * where the exception came from.
588 * See Arm Architecture Reference Manual Armv8-A, pages D13-2941/2980.
589 */
590 uintreg_t esr_el1_value =
591 GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) |
592 (EC_INSTRUCTION_ABORT_SAME_EL << ESR_EC_OFFSET);
593
Andrew Walbran17eebf92020-02-05 16:35:49 +0000594 dlog_notice("Injecting Instruction Abort exception into VM%d.\n",
595 vcpu->vm->id);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000596
Fuad Tabbac3847c72020-08-11 09:32:25 +0100597 inject_el1_exception(vcpu, esr_el1_value, far_el2);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000598}
599
600/**
601 * Injects an exception with an unknown reason into the EL1.
602 */
603static void inject_el1_unknown_exception(struct vcpu *vcpu, uintreg_t esr_el2)
604{
605 uintreg_t esr_el1_value =
606 GET_ESR_IL(esr_el2) | (EC_UNKNOWN << ESR_EC_OFFSET);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100607
608 /*
609 * The value of the far_el2 register is UNKNOWN in this case,
610 * therefore, don't propagate it to avoid leaking sensitive information.
611 */
612 uintreg_t far_el1_value = 0;
Fuad Tabbab86325a2020-01-10 13:38:15 +0000613 char *direction_str;
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000614
615 direction_str = ISS_IS_READ(esr_el2) ? "read" : "write";
Andrew Walbran17eebf92020-02-05 16:35:49 +0000616 dlog_notice(
617 "Trapped access to system register %s: op0=%d, op1=%d, crn=%d, "
618 "crm=%d, op2=%d, rt=%d.\n",
619 direction_str, GET_ISS_OP0(esr_el2), GET_ISS_OP1(esr_el2),
620 GET_ISS_CRN(esr_el2), GET_ISS_CRM(esr_el2),
621 GET_ISS_OP2(esr_el2), GET_ISS_RT(esr_el2));
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000622
Andrew Walbran17eebf92020-02-05 16:35:49 +0000623 dlog_notice("Injecting Unknown Reason exception into VM%d.\n",
624 vcpu->vm->id);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000625
Fuad Tabbac3847c72020-08-11 09:32:25 +0100626 inject_el1_exception(vcpu, esr_el1_value, far_el1_value);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000627}
628
Andrew Walbran59182d52019-09-23 17:55:39 +0100629struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100630{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100631 struct ffa_value args = {
Andrew Walbran7f920af2019-09-03 17:09:30 +0100632 .func = vcpu->regs.r[0],
633 .arg1 = vcpu->regs.r[1],
634 .arg2 = vcpu->regs.r[2],
635 .arg3 = vcpu->regs.r[3],
636 .arg4 = vcpu->regs.r[4],
637 .arg5 = vcpu->regs.r[5],
638 .arg6 = vcpu->regs.r[6],
639 .arg7 = vcpu->regs.r[7],
640 };
Andrew Walbran59182d52019-09-23 17:55:39 +0100641 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100642
Andrew Walbran7f920af2019-09-03 17:09:30 +0100643 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
644 &vcpu->regs.r[0], &next)) {
Andrew Walbran59182d52019-09-23 17:55:39 +0100645 return next;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100646 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100647
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100648 if (ffa_handler(&args, &next)) {
Andrew Walbran6f56d7b2019-12-05 16:27:34 +0000649 arch_regs_set_retval(&vcpu->regs, args);
Andrew Walbran59182d52019-09-23 17:55:39 +0100650 update_vi(next);
651 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100652 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100653
Andrew Walbran7f920af2019-09-03 17:09:30 +0100654 switch (args.func) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000655 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100656 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000657 break;
658
659 case HF_MAILBOX_WAITER_GET:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100660 vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100661 break;
662
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000663 case HF_INTERRUPT_ENABLE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100664 vcpu->regs.r[0] =
665 api_interrupt_enable(args.arg1, args.arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000666 break;
667
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000668 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100669 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000670 break;
671
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000672 case HF_INTERRUPT_INJECT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100673 vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2,
674 args.arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000675 break;
676
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100677 case HF_DEBUG_LOG:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100678 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100679 break;
680
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100681 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100682 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100683 }
684
Andrew Walbran59182d52019-09-23 17:55:39 +0100685 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000686
Andrew Walbran59182d52019-09-23 17:55:39 +0100687 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100688}
689
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100690struct vcpu *irq_lower(void)
691{
Andrew Scull9726c252019-01-23 13:44:19 +0000692 /*
693 * Switch back to primary VM, interrupts will be handled there.
694 *
695 * If the VM has aborted, this vCPU will be aborted when the scheduler
696 * tries to run it again. This means the interrupt will not be delayed
697 * by the aborted VM.
698 *
699 * TODO: Only switch when the interrupt isn't for the current VM.
700 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000701 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100702}
703
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000704struct vcpu *fiq_lower(void)
705{
706 return irq_lower();
707}
708
Fuad Tabbad1d67982020-01-08 11:28:29 +0000709noreturn struct vcpu *serr_lower(void)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000710{
Fuad Tabbad1d67982020-01-08 11:28:29 +0000711 /*
712 * SError exceptions should be isolated and handled by the responsible
713 * VM/exception level. Getting here indicates a bug, that isolation is
714 * not working, or a processor that does not support ARMv8.2-IESB, in
715 * which case Hafnium routes SError exceptions to EL2 (here).
716 */
717 panic("SError from a lower exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000718}
719
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000720/**
721 * Initialises a fault info structure. It assumes that an FnV bit exists at
722 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
723 * the ESR (the fault status code) are 010000; this is the case for both
724 * instruction and data aborts, but not necessarily for other exception reasons.
725 */
726static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100727 const struct vcpu *vcpu,
728 uint32_t mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000729{
730 uint32_t fsc = esr & 0x3f;
731 struct vcpu_fault_info r;
Olivier Deprez98ad2d22020-05-20 09:52:43 +0200732 uint64_t hpfar_el2_val;
733 uint64_t hpfar_el2_fipa;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000734
735 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000736 r.pc = va_init(vcpu->regs.pc);
737
Olivier Deprez98ad2d22020-05-20 09:52:43 +0200738 /* Get Hypervisor IPA Fault Address value. */
739 hpfar_el2_val = read_msr(hpfar_el2);
740
741 /* Extract Faulting IPA. */
742 hpfar_el2_fipa = (hpfar_el2_val & HPFAR_EL2_FIPA) << 8;
743
744#if SECURE_WORLD == 1
745
746 /**
747 * Determine if faulting IPA targets NS space.
748 * At NS-EL2 hpfar_el2 bit 63 is RES0. At S-EL2, this bit determines if
749 * the faulting Stage-1 address output is a secure or non-secure IPA.
750 */
751 if ((hpfar_el2_val & HPFAR_EL2_NS) != 0) {
752 r.mode |= MM_MODE_NS;
753 }
754
755#endif
756
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000757 /*
758 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
759 * indicates that we cannot rely on far_el2.
760 */
Andrew Walbrane52006c2019-10-22 18:01:28 +0100761 if (fsc == 0x10 && esr & (1U << 10)) {
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000762 r.vaddr = va_init(0);
Olivier Deprez98ad2d22020-05-20 09:52:43 +0200763 r.ipaddr = ipa_init(hpfar_el2_fipa);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000764 } else {
765 r.vaddr = va_init(read_msr(far_el2));
Olivier Deprez98ad2d22020-05-20 09:52:43 +0200766 r.ipaddr = ipa_init(hpfar_el2_fipa |
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000767 (read_msr(far_el2) & (PAGE_SIZE - 1)));
768 }
769
770 return r;
771}
772
Fuad Tabbac3847c72020-08-11 09:32:25 +0100773struct vcpu *sync_lower_exception(uintreg_t esr, uintreg_t far)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100774{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100775 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000776 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000777 struct vcpu *new_vcpu;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000778 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100779
Fuad Tabbac76466d2019-09-06 10:42:12 +0100780 switch (ec) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000781 case EC_WFI_WFE:
Andrew Walbran48196eb2019-03-04 14:56:24 +0000782 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100783 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100784 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100785 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000786 /* WFE */
787 /*
788 * TODO: consider giving the scheduler more context,
789 * somehow.
790 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100791 api_yield(vcpu, &new_vcpu);
Jose Marinho135dff32019-02-28 10:25:57 +0000792 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100793 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000794 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000795 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100796
Fuad Tabbab86325a2020-01-10 13:38:15 +0000797 case EC_DATA_ABORT_LOWER_EL:
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000798 info = fault_info_init(
Andrew Walbrane52006c2019-10-22 18:01:28 +0100799 esr, vcpu, (esr & (1U << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000800 if (vcpu_handle_page_fault(vcpu, &info)) {
801 return NULL;
802 }
Fuad Tabbab86325a2020-01-10 13:38:15 +0000803 /* Inform the EL1 of the data abort. */
Fuad Tabbac3847c72020-08-11 09:32:25 +0100804 inject_el1_data_abort_exception(vcpu, esr, far);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100805
Fuad Tabbab86325a2020-01-10 13:38:15 +0000806 /* Schedule the same VM to continue running. */
807 return NULL;
808
809 case EC_INSTRUCTION_ABORT_LOWER_EL:
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100810 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000811 if (vcpu_handle_page_fault(vcpu, &info)) {
812 return NULL;
813 }
Fuad Tabbab86325a2020-01-10 13:38:15 +0000814 /* Inform the EL1 of the instruction abort. */
Fuad Tabbac3847c72020-08-11 09:32:25 +0100815 inject_el1_instruction_abort_exception(vcpu, esr, far);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100816
Fuad Tabbab86325a2020-01-10 13:38:15 +0000817 /* Schedule the same VM to continue running. */
818 return NULL;
819
820 case EC_HVC:
Andrew Walbran59182d52019-09-23 17:55:39 +0100821 return hvc_handler(vcpu);
822
Fuad Tabbab86325a2020-01-10 13:38:15 +0000823 case EC_SMC: {
Andrew Scullc960c032018-10-24 15:13:35 +0100824 uintreg_t smc_pc = vcpu->regs.pc;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000825 struct vcpu *next = smc_handler(vcpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100826
827 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100828 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000829
Andrew Walbran33645652019-04-15 12:29:31 +0100830 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100831 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100832
Fuad Tabbab86325a2020-01-10 13:38:15 +0000833 case EC_MSR:
Fuad Tabbac76466d2019-09-06 10:42:12 +0100834 /*
835 * NOTE: This should never be reached because it goes through a
836 * separate path handled by handle_system_register_access().
837 */
838 panic("Handled by handle_system_register_access().");
839
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100840 default:
Andrew Walbran17eebf92020-02-05 16:35:49 +0000841 dlog_notice(
842 "Unknown lower sync exception pc=%#x, esr=%#x, "
843 "ec=%#x\n",
844 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000845 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100846 }
847
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000848 /*
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000849 * The exception wasn't handled. Inject to the VM to give it chance to
850 * handle as an unknown exception.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000851 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000852 inject_el1_unknown_exception(vcpu, esr);
853
854 /* Schedule the same VM to continue running. */
855 return NULL;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000856}
857
Fuad Tabbac76466d2019-09-06 10:42:12 +0100858/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000859 * Handles EC = 011000, MSR, MRS instruction traps.
Fuad Tabbaed294af2019-12-20 10:43:01 +0000860 * Returns non-null ONLY if the access failed and the vCPU is changing.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100861 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000862void handle_system_register_access(uintreg_t esr_el2)
Fuad Tabbac76466d2019-09-06 10:42:12 +0100863{
864 struct vcpu *vcpu = current();
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100865 ffa_vm_id_t vm_id = vcpu->vm->id;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000866 uintreg_t ec = GET_ESR_EC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100867
Fuad Tabbab86325a2020-01-10 13:38:15 +0000868 CHECK(ec == EC_MSR);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100869 /*
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100870 * Handle accesses to debug and performance monitor registers.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000871 * Inject an exception for unhandled/unsupported registers.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100872 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000873 if (debug_el1_is_register_access(esr_el2)) {
874 if (!debug_el1_process_access(vcpu, vm_id, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000875 inject_el1_unknown_exception(vcpu, esr_el2);
876 return;
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100877 }
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000878 } else if (perfmon_is_register_access(esr_el2)) {
879 if (!perfmon_process_access(vcpu, vm_id, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000880 inject_el1_unknown_exception(vcpu, esr_el2);
881 return;
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100882 }
Fuad Tabba77a4b012019-11-15 12:13:08 +0000883 } else if (feature_id_is_register_access(esr_el2)) {
884 if (!feature_id_process_access(vcpu, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000885 inject_el1_unknown_exception(vcpu, esr_el2);
886 return;
Fuad Tabba77a4b012019-11-15 12:13:08 +0000887 }
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100888 } else {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000889 inject_el1_unknown_exception(vcpu, esr_el2);
890 return;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100891 }
892
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100893 /* Instruction was fulfilled. Skip it and run the next one. */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000894 vcpu->regs.pc += GET_NEXT_PC_INC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100895}