blob: c6909c6202dde4234cdde88c9f9f31b88a2f66a2 [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"
Olivier Deprezf33a6c72020-06-09 18:28:45 +020015#include "hf/arch/tee.h"
Andrew Scullc960c032018-10-24 15:13:35 +010016
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/api.h"
Fuad Tabbac76466d2019-09-06 10:42:12 +010018#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010019#include "hf/cpu.h"
20#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010021#include "hf/ffa.h"
Andrew Sculla9c172d2019-04-03 14:10:00 +010022#include "hf/panic.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/vm.h"
24
Andrew Scullf35a5c92018-08-07 18:09:46 +010025#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010026
Fuad Tabbac76466d2019-09-06 10:42:12 +010027#include "debug_el1.h"
Fuad Tabba77a4b012019-11-15 12:13:08 +000028#include "feature_id.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010029#include "msr.h"
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +010030#include "perfmon.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010031#include "psci.h"
Andrew Walbran33645652019-04-15 12:29:31 +010032#include "psci_handler.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000033#include "smc.h"
Fuad Tabbaba8c44d2019-09-23 14:38:58 +010034#include "sysregs.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010035
Fuad Tabbac76466d2019-09-06 10:42:12 +010036/**
Olivier Deprez98ad2d22020-05-20 09:52:43 +020037 * Hypervisor Fault Address Register Non-Secure.
38 */
39#define HPFAR_EL2_NS (UINT64_C(0x1) << 63)
40
41/**
42 * Hypervisor Fault Address Register Faulting IPA.
43 */
44#define HPFAR_EL2_FIPA (UINT64_C(0xFFFFFFFFFF0))
45
46/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010047 * Gets the value to increment for the next PC.
48 * The ESR encodes whether the instruction is 2 bytes or 4 bytes long.
49 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +000050#define GET_NEXT_PC_INC(esr) (GET_ESR_IL(esr) ? 4 : 2)
Fuad Tabbac76466d2019-09-06 10:42:12 +010051
Fuad Tabbac76466d2019-09-06 10:42:12 +010052/**
Andrew Walbran0dd67ff2019-09-12 16:38:50 +010053 * The Client ID field within X7 for an SMC64 call.
54 */
55#define CLIENT_ID_MASK UINT64_C(0xffff)
56
57/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010058 * Returns a reference to the currently executing vCPU.
59 */
Andrew Scullc960c032018-10-24 15:13:35 +010060static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000061{
62 return (struct vcpu *)read_msr(tpidr_el2);
63}
64
Andrew Walbran1f8d4872018-12-20 11:21:32 +000065/**
66 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
67 * informs the arch-independent sections that registers have been saved.
68 */
69void complete_saving_state(struct vcpu *vcpu)
70{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010071 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
72 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000073
74 api_regs_state_saved(vcpu);
75
76 /*
77 * If switching away from the primary, copy the current EL0 virtual
78 * timer registers to the corresponding EL2 physical timer registers.
79 * This is used to emulate the virtual timer for the primary in case it
80 * should fire while the secondary is running.
81 */
82 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
83 /*
84 * Clear timer control register before copying compare value, to
85 * avoid a spurious timer interrupt. This could be a problem if
86 * the interrupt is configured as edge-triggered, as it would
87 * then be latched in.
88 */
89 write_msr(cnthp_ctl_el2, 0);
90 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
91 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
92 }
93}
94
95/**
96 * Restores the state of per-vCPU peripherals, such as the virtual timer.
97 */
98void begin_restoring_state(struct vcpu *vcpu)
99{
100 /*
101 * Clear timer control register before restoring compare value, to avoid
102 * a spurious timer interrupt. This could be a problem if the interrupt
103 * is configured as edge-triggered, as it would then be latched in.
104 */
105 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100106 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
107 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000108
109 /*
110 * If we are switching (back) to the primary, disable the EL2 physical
111 * timer which was being used to emulate the EL0 virtual timer, as the
112 * virtual timer is now running for the primary again.
113 */
114 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
115 write_msr(cnthp_ctl_el2, 0);
116 write_msr(cnthp_cval_el2, 0);
117 }
118}
119
Andrew Walbran1f32e722019-06-07 17:57:26 +0100120/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100121 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
122 * current VMID.
123 */
124static void invalidate_vm_tlb(void)
125{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100126 /*
127 * Ensure that the last VTTBR write has taken effect so we invalidate
128 * the right set of TLB entries.
129 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100130 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100131
Andrew Walbran1f32e722019-06-07 17:57:26 +0100132 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100133
134 /*
135 * Ensure that no instructions are fetched for the VM until after the
136 * TLB invalidation has taken effect.
137 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100138 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100139
140 /*
141 * Ensure that no data reads or writes for the VM happen until after the
Fuad Tabba77a4b012019-11-15 12:13:08 +0000142 * TLB invalidation has taken effect. Non-shareable is enough because
143 * the TLB is local to the CPU.
Andrew Walbrancff1f682019-07-04 14:52:45 +0100144 */
David Brazdil851948e2019-08-09 12:02:12 +0100145 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100146}
147
148/**
149 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
150 * the same VM which was run on the current pCPU.
151 *
152 * This is necessary because VMs may (contrary to the architecture
153 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
154 * workaround:
155 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
156 */
157void maybe_invalidate_tlb(struct vcpu *vcpu)
158{
159 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100160 ffa_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100161
162 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
163 new_vcpu_index) {
164 /*
165 * The vCPU has changed since the last time this VM was run on
166 * this pCPU, so we need to invalidate the TLB.
167 */
168 invalidate_vm_tlb();
169
170 /* Record the fact that this vCPU is now running on this CPU. */
171 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
172 new_vcpu_index;
173 }
174}
175
David Brazdil768f69c2019-12-19 15:46:12 +0000176noreturn void irq_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100177{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000178 (void)elr;
179 (void)spsr;
180
Fuad Tabbad1d67982020-01-08 11:28:29 +0000181 panic("IRQ from current exception level.");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100182}
183
David Brazdil768f69c2019-12-19 15:46:12 +0000184noreturn void fiq_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100185{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000186 (void)elr;
187 (void)spsr;
188
Fuad Tabbad1d67982020-01-08 11:28:29 +0000189 panic("FIQ from current exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000190}
191
David Brazdil768f69c2019-12-19 15:46:12 +0000192noreturn void serr_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000193{
194 (void)elr;
195 (void)spsr;
196
Fuad Tabbad1d67982020-01-08 11:28:29 +0000197 panic("SError from current exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000198}
199
David Brazdil768f69c2019-12-19 15:46:12 +0000200noreturn void sync_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000201{
202 uintreg_t esr = read_msr(esr_el2);
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000203 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000204
205 (void)spsr;
206
Fuad Tabbac76466d2019-09-06 10:42:12 +0100207 switch (ec) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000208 case EC_DATA_ABORT_SAME_EL:
Andrew Walbrane52006c2019-10-22 18:01:28 +0100209 if (!(esr & (1U << 10))) { /* Check FnV bit. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000210 dlog_error(
211 "Data abort: pc=%#x, esr=%#x, ec=%#x, "
212 "far=%#x\n",
213 elr, esr, ec, read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100214 } else {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000215 dlog_error(
216 "Data abort: pc=%#x, esr=%#x, ec=%#x, "
217 "far=invalid\n",
218 elr, esr, ec);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100219 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100220
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000221 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100222
223 default:
Andrew Walbran17eebf92020-02-05 16:35:49 +0000224 dlog_error(
225 "Unknown current sync exception pc=%#x, esr=%#x, "
226 "ec=%#x\n",
227 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100228 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100229 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000230
Andrew Sculla9c172d2019-04-03 14:10:00 +0100231 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100232}
233
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100234/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000235 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
236 * arch_regs.
237 */
238static void set_virtual_interrupt(struct arch_regs *r, bool enable)
239{
240 if (enable) {
241 r->lazy.hcr_el2 |= HCR_EL2_VI;
242 } else {
243 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
244 }
245}
246
247/**
248 * Sets or clears the VI bit in the HCR_EL2 register.
249 */
250static void set_virtual_interrupt_current(bool enable)
251{
252 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000253
Andrew Walbran3d84a262018-12-13 14:41:19 +0000254 if (enable) {
255 hcr_el2 |= HCR_EL2_VI;
256 } else {
257 hcr_el2 &= ~HCR_EL2_VI;
258 }
259 write_msr(hcr_el2, hcr_el2);
260}
261
Andrew Scullae9962e2019-10-03 16:51:16 +0100262/**
263 * Checks whether to block an SMC being forwarded from a VM.
264 */
265static bool smc_is_blocked(const struct vm *vm, uint32_t func)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100266{
Andrew Scullae9962e2019-10-03 16:51:16 +0100267 bool block_by_default = !vm->smc_whitelist.permissive;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100268
Andrew Scullae9962e2019-10-03 16:51:16 +0100269 for (size_t i = 0; i < vm->smc_whitelist.smc_count; ++i) {
270 if (func == vm->smc_whitelist.smcs[i]) {
271 return false;
272 }
273 }
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100274
Andrew Walbran17eebf92020-02-05 16:35:49 +0000275 dlog_notice("SMC %#010x attempted from VM %d, blocked=%d\n", func,
276 vm->id, block_by_default);
Andrew Scullae9962e2019-10-03 16:51:16 +0100277
278 /* Access is still allowed in permissive mode. */
279 return block_by_default;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100280}
281
282/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100283 * Applies SMC access control according to manifest and forwards the call if
284 * access is granted.
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100285 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100286static void smc_forwarder(const struct vm *vm, struct ffa_value *args)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100287{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100288 struct ffa_value ret;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000289 uint32_t client_id = vm->id;
290 uintreg_t arg7 = args->arg7;
Andrew Scullae9962e2019-10-03 16:51:16 +0100291
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000292 if (smc_is_blocked(vm, args->func)) {
293 args->func = SMCCC_ERROR_UNKNOWN;
Andrew Scullae9962e2019-10-03 16:51:16 +0100294 return;
295 }
296
Andrew Walbran0dd67ff2019-09-12 16:38:50 +0100297 /*
298 * Set the Client ID but keep the existing Secure OS ID and anything
299 * else (currently unspecified) that the client may have passed in the
300 * upper bits.
301 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000302 args->arg7 = client_id | (arg7 & ~CLIENT_ID_MASK);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000303 ret = smc_forward(args->func, args->arg1, args->arg2, args->arg3,
304 args->arg4, args->arg5, args->arg6, args->arg7);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100305
Andrew Scullae9962e2019-10-03 16:51:16 +0100306 /*
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000307 * Preserve the value passed by the caller, rather than the generated
308 * client_id. Note that this would also overwrite any return value that
Andrew Scullae9962e2019-10-03 16:51:16 +0100309 * may be in x7, but the SMCs that we are forwarding are legacy calls
310 * from before SMCCC 1.2 so won't have more than 4 return values anyway.
311 */
Andrew Scull07b6bd32019-12-12 17:19:55 +0000312 ret.arg7 = arg7;
313
314 plat_smc_post_forward(*args, &ret);
315
316 *args = ret;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100317}
318
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200319/**
320 * In the normal world, ffa_handler is always called from the virtual FF-A
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100321 * instance (from a VM in EL1). In the secure world, ffa_handler may be called
322 * from the virtual (a secure partition in S-EL1) or physical FF-A instance
323 * (from the normal world via EL3). The function returns true when the call is
324 * handled. The *next pointer is updated to the next vCPU to run, which might be
325 * the 'other world' vCPU if the call originated from the virtual FF-A instance
326 * and has to be forwarded down to EL3, or left as is to resume the current
327 * vCPU.
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200328 */
329static bool ffa_handler(struct ffa_value *args, struct vcpu *current,
330 struct vcpu **next)
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100331{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000332 uint32_t func = args->func & ~SMCCC_CONVENTION_MASK;
333
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100334 /*
335 * NOTE: When adding new methods to this handler update
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100336 * api_ffa_features accordingly.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100337 */
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000338 switch (func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100339 case FFA_VERSION_32:
340 *args = api_ffa_version(args->arg1);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100341 return true;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100342 case FFA_PARTITION_INFO_GET_32: {
343 struct ffa_uuid uuid;
344
345 ffa_uuid_init(args->arg1, args->arg2, args->arg3, args->arg4,
346 &uuid);
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200347 *args = api_ffa_partition_info_get(current, &uuid);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100348 return true;
349 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100350 case FFA_ID_GET_32:
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200351 *args = api_ffa_id_get(current);
Andrew Walbrand230f662019-10-07 18:03:36 +0100352 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100353 case FFA_FEATURES_32:
354 *args = api_ffa_features(args->arg1);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100355 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100356 case FFA_RX_RELEASE_32:
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200357 *args = api_ffa_rx_release(current, next);
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +0000358 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100359 case FFA_RXTX_MAP_32:
360 *args = api_ffa_rxtx_map(ipa_init(args->arg1),
361 ipa_init(args->arg2), args->arg3,
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200362 current, next);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000363 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364 case FFA_YIELD_32:
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200365 *args = api_yield(current, next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100366 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100367 case FFA_MSG_SEND_32:
368 *args = api_ffa_msg_send(
369 ffa_msg_send_sender(*args),
370 ffa_msg_send_receiver(*args), ffa_msg_send_size(*args),
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200371 ffa_msg_send_attributes(*args), current, next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100372 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100373 case FFA_MSG_WAIT_32:
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200374 *args = api_ffa_msg_recv(true, current, next);
Andrew Walbran0de4f162019-09-03 16:44:20 +0100375 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100376 case FFA_MSG_POLL_32:
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200377 *args = api_ffa_msg_recv(false, current, next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100378 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100379 case FFA_RUN_32:
380 *args = api_ffa_run(ffa_vm_id(*args), ffa_vcpu_index(*args),
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200381 current, next);
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100382 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383 case FFA_MEM_DONATE_32:
384 case FFA_MEM_LEND_32:
385 case FFA_MEM_SHARE_32:
386 *args = api_ffa_mem_send(func, args->arg1, args->arg2,
387 ipa_init(args->arg3), args->arg4,
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200388 current);
Andrew Walbran82d6d152019-12-24 15:02:06 +0000389 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100390 case FFA_MEM_RETRIEVE_REQ_32:
391 *args = api_ffa_mem_retrieve_req(args->arg1, args->arg2,
392 ipa_init(args->arg3),
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200393 args->arg4, current);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000394 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100395 case FFA_MEM_RELINQUISH_32:
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200396 *args = api_ffa_mem_relinquish(current);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000397 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100398 case FFA_MEM_RECLAIM_32:
399 *args = api_ffa_mem_reclaim(
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100400 ffa_assemble_handle(args->arg1, args->arg2), args->arg3,
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200401 current);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000402 return true;
Andrew Walbranca808b12020-05-15 17:22:28 +0100403 case FFA_MEM_FRAG_RX_32:
404 *args = api_ffa_mem_frag_rx(ffa_frag_handle(*args), args->arg3,
405 (args->arg4 >> 16) & 0xffff,
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200406 current);
Andrew Walbranca808b12020-05-15 17:22:28 +0100407 return true;
408 case FFA_MEM_FRAG_TX_32:
409 *args = api_ffa_mem_frag_tx(ffa_frag_handle(*args), args->arg3,
410 (args->arg4 >> 16) & 0xffff,
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200411 current);
Andrew Walbranca808b12020-05-15 17:22:28 +0100412 return true;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000413 case FFA_MSG_SEND_DIRECT_REQ_32:
414 *args = api_ffa_msg_send_direct_req(
415 ffa_msg_send_sender(*args),
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200416 ffa_msg_send_receiver(*args), *args, current, next);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000417 return true;
418 case FFA_MSG_SEND_DIRECT_RESP_32:
419 *args = api_ffa_msg_send_direct_resp(
420 ffa_msg_send_sender(*args),
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200421 ffa_msg_send_receiver(*args), *args, current, next);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000422 return true;
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100423 }
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100424
425 return false;
426}
427
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200428#if SECURE_WORLD == 1
429
430static struct vcpu *get_other_world_vcpu(struct vcpu *current)
431{
432 struct vm *vm = vm_find(HF_OTHER_WORLD_ID);
433 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
434
435 return vm_get_vcpu(vm, current_cpu_index);
436}
437
438/**
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100439 * Called to switch to the other world and handle FF-A calls from it. Returns
440 * when it is ready to run a secure partition again.
441 *
442 * Expects that `other_world_vcpu` points to the vCPU of the 'other world VM'
443 * which corresponds to this physical CPU, and that `*next` is `NULL`.
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200444 */
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100445static void other_world_switch_loop(struct vcpu *other_world_vcpu,
446 struct vcpu **next)
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200447{
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100448 struct ffa_value other_world_args =
449 arch_regs_get_args(&other_world_vcpu->regs);
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200450
Andrew Walbran45633dd2020-10-07 17:59:54 +0100451 CHECK(!vm_id_is_current_world(other_world_vcpu->vm->id));
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100452 CHECK(*next == NULL);
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200453
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100454 while (*next == NULL) {
455 /*
456 * Either we just entered the function or the last FF-A call
457 * from the other world was handled and next is still NULL,
458 * which means that the result should be passed back to the
459 * other world.
460 */
461 other_world_args = smc_ffa_call(other_world_args);
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200462
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100463 /*
464 * The call to EL3 returned, thus other_world_args contains an
465 * FF-A call from the physical FF-A instance. Handle it. At this
466 * point *next is still NULL, which means that we will return
467 * the result of the call back to EL3 unless the API handler
468 * sets *next to something different.
469 */
470 if (!ffa_handler(&other_world_args, other_world_vcpu, next)) {
471 other_world_args.func = SMCCC_ERROR_UNKNOWN;
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200472 }
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200473 }
474
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100475 /*
Andrew Walbran45633dd2020-10-07 17:59:54 +0100476 * ffa_handler set *next to something, which means it wants to switch
477 * back to an SP in EL1. It must be something in this world though, as
478 * if it wanted to return back to the other world (where the last FF-A
479 * call came from) it wouldn't have set *next at all.
480 */
481 CHECK(vm_id_is_current_world((*next)->vm->id));
482
483 /*
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100484 * Store the return value on the other world vCPU, ready for next time
485 * we switch to it (in case they aren't overwritten at that point by
486 * whatever API function decides to make the switch).
487 */
488 arch_regs_set_retval(&other_world_vcpu->regs, other_world_args);
Olivier Deprezf33a6c72020-06-09 18:28:45 +0200489}
490
491#endif
492
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100493/**
494 * Set or clear VI bit according to pending interrupts.
495 */
496static void update_vi(struct vcpu *next)
497{
498 if (next == NULL) {
499 /*
500 * Not switching vCPUs, set the bit for the current vCPU
501 * directly in the register.
502 */
503 struct vcpu *vcpu = current();
504
505 sl_lock(&vcpu->lock);
506 set_virtual_interrupt_current(
507 vcpu->interrupts.enabled_and_pending_count > 0);
508 sl_unlock(&vcpu->lock);
509 } else {
Andrew Walbran45633dd2020-10-07 17:59:54 +0100510 CHECK(vm_id_is_current_world(next->vm->id));
511
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100512 /*
513 * About to switch vCPUs, set the bit for the vCPU to which we
514 * are switching in the saved copy of the register.
515 */
516 sl_lock(&next->lock);
517 set_virtual_interrupt(
518 &next->regs,
519 next->interrupts.enabled_and_pending_count > 0);
520 sl_unlock(&next->lock);
521 }
522}
523
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100524/**
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100525 * Handles PSCI and FF-A calls and writes the return value back to the registers
526 * of the vCPU. This is shared between smc_handler and hvc_handler.
527 *
528 * Returns true if the call was handled.
529 */
530static bool hvc_smc_handler(struct ffa_value args, struct vcpu *vcpu,
531 struct vcpu **next)
532{
533 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
534 &vcpu->regs.r[0], next)) {
535 return true;
536 }
537
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100538 if (ffa_handler(&args, vcpu, next)) {
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100539 arch_regs_set_retval(&vcpu->regs, args);
Andrew Walbran8e8bf3f2020-10-07 17:58:20 +0100540
541#if SECURE_WORLD == 1
542 struct vcpu *other_world_vcpu = get_other_world_vcpu(current());
543
544 if (*next == other_world_vcpu) {
545 *next = NULL;
546 other_world_switch_loop(other_world_vcpu, next);
547 }
548#endif
549
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100550 update_vi(*next);
551 return true;
552 }
553
554 return false;
555}
556
557/**
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100558 * Processes SMC instruction calls.
559 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000560static struct vcpu *smc_handler(struct vcpu *vcpu)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100561{
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100562 struct ffa_value args = arch_regs_get_args(&vcpu->regs);
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000563 struct vcpu *next = NULL;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100564
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100565 if (hvc_smc_handler(args, vcpu, &next)) {
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000566 return next;
Andrew Walbran4579f7002019-08-30 16:24:58 +0100567 }
568
Andrew Walbran85c37662019-12-05 16:29:33 +0000569 switch (args.func & ~SMCCC_CONVENTION_MASK) {
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100570 case HF_DEBUG_LOG:
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000571 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000572 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100573 }
574
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000575 smc_forwarder(vcpu->vm, &args);
576 arch_regs_set_retval(&vcpu->regs, args);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000577 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100578}
579
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000580/*
581 * Exception vector offsets.
582 * See Arm Architecture Reference Manual Armv8-A, D1.10.2.
583 */
584
585/**
586 * Offset for synchronous exceptions at current EL with SPx.
587 */
588#define OFFSET_CURRENT_SPX UINT64_C(0x200)
589
590/**
591 * Offset for synchronous exceptions at lower EL using AArch64.
592 */
593#define OFFSET_LOWER_EL_64 UINT64_C(0x400)
594
595/**
596 * Offset for synchronous exceptions at lower EL using AArch32.
597 */
598#define OFFSET_LOWER_EL_32 UINT64_C(0x600)
599
600/**
601 * Returns the address for the exception handler at EL1.
602 */
603static uintreg_t get_el1_exception_handler_addr(const struct vcpu *vcpu)
604{
605 uintreg_t base_addr = read_msr(vbar_el1);
606 uintreg_t pe_mode = vcpu->regs.spsr & PSR_PE_MODE_MASK;
607 bool is_arch32 = vcpu->regs.spsr & PSR_ARCH_MODE_32;
608
609 if (pe_mode == PSR_PE_MODE_EL0T) {
610 if (is_arch32) {
611 base_addr += OFFSET_LOWER_EL_32;
612 } else {
613 base_addr += OFFSET_LOWER_EL_64;
614 }
615 } else {
616 CHECK(!is_arch32);
617 base_addr += OFFSET_CURRENT_SPX;
618 }
619
620 return base_addr;
621}
622
623/**
Fuad Tabbab86325a2020-01-10 13:38:15 +0000624 * Injects an exception with the specified Exception Syndrom Register value into
625 * the EL1.
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000626 *
627 * NOTE: This function assumes that the lazy registers haven't been saved, and
628 * writes to the lazy registers of the CPU directly instead of the vCPU.
629 */
Fuad Tabbac3847c72020-08-11 09:32:25 +0100630static void inject_el1_exception(struct vcpu *vcpu, uintreg_t esr_el1_value,
631 uintreg_t far_el1_value)
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000632{
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000633 uintreg_t handler_address = get_el1_exception_handler_addr(vcpu);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000634
635 /* Update the CPU state to inject the exception. */
636 write_msr(esr_el1, esr_el1_value);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100637 write_msr(far_el1, far_el1_value);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000638 write_msr(elr_el1, vcpu->regs.pc);
639 write_msr(spsr_el1, vcpu->regs.spsr);
640
641 /*
642 * Mask (disable) interrupts and run in EL1h mode.
643 * EL1h mode is used because by default, taking an exception selects the
644 * stack pointer for the target Exception level. The software can change
645 * that later in the handler if needed.
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000646 */
647 vcpu->regs.spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
648
649 /* Transfer control to the exception hander. */
650 vcpu->regs.pc = handler_address;
Fuad Tabbab86325a2020-01-10 13:38:15 +0000651}
652
653/**
654 * Injects a Data Abort exception (same exception level).
655 */
656static void inject_el1_data_abort_exception(struct vcpu *vcpu,
Fuad Tabbac3847c72020-08-11 09:32:25 +0100657 uintreg_t esr_el2,
658 uintreg_t far_el2)
Fuad Tabbab86325a2020-01-10 13:38:15 +0000659{
660 /*
661 * ISS encoding remains the same, but the EC is changed to reflect
662 * where the exception came from.
663 * See Arm Architecture Reference Manual Armv8-A, pages D13-2943/2982.
664 */
665 uintreg_t esr_el1_value = GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) |
666 (EC_DATA_ABORT_SAME_EL << ESR_EC_OFFSET);
667
Andrew Walbran17eebf92020-02-05 16:35:49 +0000668 dlog_notice("Injecting Data Abort exception into VM%d.\n",
669 vcpu->vm->id);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000670
Fuad Tabbac3847c72020-08-11 09:32:25 +0100671 inject_el1_exception(vcpu, esr_el1_value, far_el2);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000672}
673
674/**
675 * Injects a Data Abort exception (same exception level).
676 */
677static void inject_el1_instruction_abort_exception(struct vcpu *vcpu,
Fuad Tabbac3847c72020-08-11 09:32:25 +0100678 uintreg_t esr_el2,
679 uintreg_t far_el2)
Fuad Tabbab86325a2020-01-10 13:38:15 +0000680{
681 /*
682 * ISS encoding remains the same, but the EC is changed to reflect
683 * where the exception came from.
684 * See Arm Architecture Reference Manual Armv8-A, pages D13-2941/2980.
685 */
686 uintreg_t esr_el1_value =
687 GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) |
688 (EC_INSTRUCTION_ABORT_SAME_EL << ESR_EC_OFFSET);
689
Andrew Walbran17eebf92020-02-05 16:35:49 +0000690 dlog_notice("Injecting Instruction Abort exception into VM%d.\n",
691 vcpu->vm->id);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000692
Fuad Tabbac3847c72020-08-11 09:32:25 +0100693 inject_el1_exception(vcpu, esr_el1_value, far_el2);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000694}
695
696/**
697 * Injects an exception with an unknown reason into the EL1.
698 */
699static void inject_el1_unknown_exception(struct vcpu *vcpu, uintreg_t esr_el2)
700{
701 uintreg_t esr_el1_value =
702 GET_ESR_IL(esr_el2) | (EC_UNKNOWN << ESR_EC_OFFSET);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100703
704 /*
705 * The value of the far_el2 register is UNKNOWN in this case,
706 * therefore, don't propagate it to avoid leaking sensitive information.
707 */
708 uintreg_t far_el1_value = 0;
Fuad Tabbab86325a2020-01-10 13:38:15 +0000709 char *direction_str;
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000710
711 direction_str = ISS_IS_READ(esr_el2) ? "read" : "write";
Andrew Walbran17eebf92020-02-05 16:35:49 +0000712 dlog_notice(
713 "Trapped access to system register %s: op0=%d, op1=%d, crn=%d, "
714 "crm=%d, op2=%d, rt=%d.\n",
715 direction_str, GET_ISS_OP0(esr_el2), GET_ISS_OP1(esr_el2),
716 GET_ISS_CRN(esr_el2), GET_ISS_CRM(esr_el2),
717 GET_ISS_OP2(esr_el2), GET_ISS_RT(esr_el2));
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000718
Andrew Walbran17eebf92020-02-05 16:35:49 +0000719 dlog_notice("Injecting Unknown Reason exception into VM%d.\n",
720 vcpu->vm->id);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000721
Fuad Tabbac3847c72020-08-11 09:32:25 +0100722 inject_el1_exception(vcpu, esr_el1_value, far_el1_value);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000723}
724
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100725static struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100726{
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100727 struct ffa_value args = arch_regs_get_args(&vcpu->regs);
Andrew Walbran59182d52019-09-23 17:55:39 +0100728 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100729
Andrew Walbrand8d3f5d2020-10-07 18:23:01 +0100730 if (hvc_smc_handler(args, vcpu, &next)) {
Andrew Walbran59182d52019-09-23 17:55:39 +0100731 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100732 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100733
Andrew Walbran7f920af2019-09-03 17:09:30 +0100734 switch (args.func) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000735 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100736 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000737 break;
738
739 case HF_MAILBOX_WAITER_GET:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100740 vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100741 break;
742
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000743 case HF_INTERRUPT_ENABLE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100744 vcpu->regs.r[0] =
745 api_interrupt_enable(args.arg1, args.arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000746 break;
747
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000748 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100749 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000750 break;
751
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000752 case HF_INTERRUPT_INJECT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100753 vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2,
754 args.arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000755 break;
756
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100757 case HF_DEBUG_LOG:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100758 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100759 break;
760
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100761 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100762 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100763 }
764
Andrew Walbran59182d52019-09-23 17:55:39 +0100765 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000766
Andrew Walbran59182d52019-09-23 17:55:39 +0100767 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100768}
769
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100770struct vcpu *irq_lower(void)
771{
Andrew Scull9726c252019-01-23 13:44:19 +0000772 /*
773 * Switch back to primary VM, interrupts will be handled there.
774 *
775 * If the VM has aborted, this vCPU will be aborted when the scheduler
776 * tries to run it again. This means the interrupt will not be delayed
777 * by the aborted VM.
778 *
779 * TODO: Only switch when the interrupt isn't for the current VM.
780 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000781 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100782}
783
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000784struct vcpu *fiq_lower(void)
785{
786 return irq_lower();
787}
788
Fuad Tabbad1d67982020-01-08 11:28:29 +0000789noreturn struct vcpu *serr_lower(void)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000790{
Fuad Tabbad1d67982020-01-08 11:28:29 +0000791 /*
792 * SError exceptions should be isolated and handled by the responsible
793 * VM/exception level. Getting here indicates a bug, that isolation is
794 * not working, or a processor that does not support ARMv8.2-IESB, in
795 * which case Hafnium routes SError exceptions to EL2 (here).
796 */
797 panic("SError from a lower exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000798}
799
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000800/**
801 * Initialises a fault info structure. It assumes that an FnV bit exists at
802 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
803 * the ESR (the fault status code) are 010000; this is the case for both
804 * instruction and data aborts, but not necessarily for other exception reasons.
805 */
806static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100807 const struct vcpu *vcpu,
808 uint32_t mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000809{
810 uint32_t fsc = esr & 0x3f;
811 struct vcpu_fault_info r;
Olivier Deprez98ad2d22020-05-20 09:52:43 +0200812 uint64_t hpfar_el2_val;
813 uint64_t hpfar_el2_fipa;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000814
815 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000816 r.pc = va_init(vcpu->regs.pc);
817
Olivier Deprez98ad2d22020-05-20 09:52:43 +0200818 /* Get Hypervisor IPA Fault Address value. */
819 hpfar_el2_val = read_msr(hpfar_el2);
820
821 /* Extract Faulting IPA. */
822 hpfar_el2_fipa = (hpfar_el2_val & HPFAR_EL2_FIPA) << 8;
823
824#if SECURE_WORLD == 1
825
826 /**
827 * Determine if faulting IPA targets NS space.
828 * At NS-EL2 hpfar_el2 bit 63 is RES0. At S-EL2, this bit determines if
829 * the faulting Stage-1 address output is a secure or non-secure IPA.
830 */
831 if ((hpfar_el2_val & HPFAR_EL2_NS) != 0) {
832 r.mode |= MM_MODE_NS;
833 }
834
835#endif
836
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000837 /*
838 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
839 * indicates that we cannot rely on far_el2.
840 */
Andrew Walbrane52006c2019-10-22 18:01:28 +0100841 if (fsc == 0x10 && esr & (1U << 10)) {
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000842 r.vaddr = va_init(0);
Olivier Deprez98ad2d22020-05-20 09:52:43 +0200843 r.ipaddr = ipa_init(hpfar_el2_fipa);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000844 } else {
845 r.vaddr = va_init(read_msr(far_el2));
Olivier Deprez98ad2d22020-05-20 09:52:43 +0200846 r.ipaddr = ipa_init(hpfar_el2_fipa |
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000847 (read_msr(far_el2) & (PAGE_SIZE - 1)));
848 }
849
850 return r;
851}
852
Fuad Tabbac3847c72020-08-11 09:32:25 +0100853struct vcpu *sync_lower_exception(uintreg_t esr, uintreg_t far)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100854{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100855 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000856 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000857 struct vcpu *new_vcpu;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000858 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100859
Fuad Tabbac76466d2019-09-06 10:42:12 +0100860 switch (ec) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000861 case EC_WFI_WFE:
Andrew Walbran48196eb2019-03-04 14:56:24 +0000862 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100863 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100864 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100865 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000866 /* WFE */
867 /*
868 * TODO: consider giving the scheduler more context,
869 * somehow.
870 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100871 api_yield(vcpu, &new_vcpu);
Jose Marinho135dff32019-02-28 10:25:57 +0000872 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100873 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000874 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000875 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100876
Fuad Tabbab86325a2020-01-10 13:38:15 +0000877 case EC_DATA_ABORT_LOWER_EL:
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000878 info = fault_info_init(
Andrew Walbrane52006c2019-10-22 18:01:28 +0100879 esr, vcpu, (esr & (1U << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000880 if (vcpu_handle_page_fault(vcpu, &info)) {
881 return NULL;
882 }
Fuad Tabbab86325a2020-01-10 13:38:15 +0000883 /* Inform the EL1 of the data abort. */
Fuad Tabbac3847c72020-08-11 09:32:25 +0100884 inject_el1_data_abort_exception(vcpu, esr, far);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100885
Fuad Tabbab86325a2020-01-10 13:38:15 +0000886 /* Schedule the same VM to continue running. */
887 return NULL;
888
889 case EC_INSTRUCTION_ABORT_LOWER_EL:
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100890 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000891 if (vcpu_handle_page_fault(vcpu, &info)) {
892 return NULL;
893 }
Fuad Tabbab86325a2020-01-10 13:38:15 +0000894 /* Inform the EL1 of the instruction abort. */
Fuad Tabbac3847c72020-08-11 09:32:25 +0100895 inject_el1_instruction_abort_exception(vcpu, esr, far);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100896
Fuad Tabbab86325a2020-01-10 13:38:15 +0000897 /* Schedule the same VM to continue running. */
898 return NULL;
899
900 case EC_HVC:
Andrew Walbran59182d52019-09-23 17:55:39 +0100901 return hvc_handler(vcpu);
902
Fuad Tabbab86325a2020-01-10 13:38:15 +0000903 case EC_SMC: {
Andrew Scullc960c032018-10-24 15:13:35 +0100904 uintreg_t smc_pc = vcpu->regs.pc;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000905 struct vcpu *next = smc_handler(vcpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100906
907 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100908 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000909
Andrew Walbran33645652019-04-15 12:29:31 +0100910 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100911 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100912
Fuad Tabbab86325a2020-01-10 13:38:15 +0000913 case EC_MSR:
Fuad Tabbac76466d2019-09-06 10:42:12 +0100914 /*
915 * NOTE: This should never be reached because it goes through a
916 * separate path handled by handle_system_register_access().
917 */
918 panic("Handled by handle_system_register_access().");
919
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100920 default:
Andrew Walbran17eebf92020-02-05 16:35:49 +0000921 dlog_notice(
922 "Unknown lower sync exception pc=%#x, esr=%#x, "
923 "ec=%#x\n",
924 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000925 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100926 }
927
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000928 /*
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000929 * The exception wasn't handled. Inject to the VM to give it chance to
930 * handle as an unknown exception.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000931 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000932 inject_el1_unknown_exception(vcpu, esr);
933
934 /* Schedule the same VM to continue running. */
935 return NULL;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000936}
937
Fuad Tabbac76466d2019-09-06 10:42:12 +0100938/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000939 * Handles EC = 011000, MSR, MRS instruction traps.
Fuad Tabbaed294af2019-12-20 10:43:01 +0000940 * Returns non-null ONLY if the access failed and the vCPU is changing.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100941 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000942void handle_system_register_access(uintreg_t esr_el2)
Fuad Tabbac76466d2019-09-06 10:42:12 +0100943{
944 struct vcpu *vcpu = current();
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100945 ffa_vm_id_t vm_id = vcpu->vm->id;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000946 uintreg_t ec = GET_ESR_EC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100947
Fuad Tabbab86325a2020-01-10 13:38:15 +0000948 CHECK(ec == EC_MSR);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100949 /*
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100950 * Handle accesses to debug and performance monitor registers.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000951 * Inject an exception for unhandled/unsupported registers.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100952 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000953 if (debug_el1_is_register_access(esr_el2)) {
954 if (!debug_el1_process_access(vcpu, vm_id, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000955 inject_el1_unknown_exception(vcpu, esr_el2);
956 return;
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100957 }
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000958 } else if (perfmon_is_register_access(esr_el2)) {
959 if (!perfmon_process_access(vcpu, vm_id, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000960 inject_el1_unknown_exception(vcpu, esr_el2);
961 return;
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100962 }
Fuad Tabba77a4b012019-11-15 12:13:08 +0000963 } else if (feature_id_is_register_access(esr_el2)) {
964 if (!feature_id_process_access(vcpu, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000965 inject_el1_unknown_exception(vcpu, esr_el2);
966 return;
Fuad Tabba77a4b012019-11-15 12:13:08 +0000967 }
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100968 } else {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000969 inject_el1_unknown_exception(vcpu, esr_el2);
970 return;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100971 }
972
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100973 /* Instruction was fulfilled. Skip it and run the next one. */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000974 vcpu->regs.pc += GET_NEXT_PC_INC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100975}