blob: 9a48895cb53e869461a30fedb2b0437a06bee1ec [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 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scullc960c032018-10-24 15:13:35 +010017#include <stdnoreturn.h>
18
Andrew Walbran1f32e722019-06-07 17:57:26 +010019#include "hf/arch/barriers.h"
Andrew Scullc960c032018-10-24 15:13:35 +010020#include "hf/arch/init.h"
David Brazdil851948e2019-08-09 12:02:12 +010021#include "hf/arch/mm.h"
Andrew Scull07b6bd32019-12-12 17:19:55 +000022#include "hf/arch/plat/smc.h"
Andrew Scullc960c032018-10-24 15:13:35 +010023
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/api.h"
Fuad Tabbac76466d2019-09-06 10:42:12 +010025#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010026#include "hf/cpu.h"
27#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010028#include "hf/ffa.h"
Andrew Sculla9c172d2019-04-03 14:10:00 +010029#include "hf/panic.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010030#include "hf/vm.h"
31
Andrew Scullf35a5c92018-08-07 18:09:46 +010032#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010033
Fuad Tabbac76466d2019-09-06 10:42:12 +010034#include "debug_el1.h"
Fuad Tabba77a4b012019-11-15 12:13:08 +000035#include "feature_id.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010036#include "msr.h"
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +010037#include "perfmon.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010038#include "psci.h"
Andrew Walbran33645652019-04-15 12:29:31 +010039#include "psci_handler.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000040#include "smc.h"
Fuad Tabbaba8c44d2019-09-23 14:38:58 +010041#include "sysregs.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010042
Fuad Tabbac76466d2019-09-06 10:42:12 +010043/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010044 * Gets the value to increment for the next PC.
45 * The ESR encodes whether the instruction is 2 bytes or 4 bytes long.
46 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +000047#define GET_NEXT_PC_INC(esr) (GET_ESR_IL(esr) ? 4 : 2)
Fuad Tabbac76466d2019-09-06 10:42:12 +010048
Fuad Tabbac76466d2019-09-06 10:42:12 +010049/**
Andrew Walbran0dd67ff2019-09-12 16:38:50 +010050 * The Client ID field within X7 for an SMC64 call.
51 */
52#define CLIENT_ID_MASK UINT64_C(0xffff)
53
54/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010055 * Returns a reference to the currently executing vCPU.
56 */
Andrew Scullc960c032018-10-24 15:13:35 +010057static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000058{
59 return (struct vcpu *)read_msr(tpidr_el2);
60}
61
Andrew Walbran1f8d4872018-12-20 11:21:32 +000062/**
63 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
64 * informs the arch-independent sections that registers have been saved.
65 */
66void complete_saving_state(struct vcpu *vcpu)
67{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010068 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
69 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000070
71 api_regs_state_saved(vcpu);
72
73 /*
74 * If switching away from the primary, copy the current EL0 virtual
75 * timer registers to the corresponding EL2 physical timer registers.
76 * This is used to emulate the virtual timer for the primary in case it
77 * should fire while the secondary is running.
78 */
79 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
80 /*
81 * Clear timer control register before copying compare value, to
82 * avoid a spurious timer interrupt. This could be a problem if
83 * the interrupt is configured as edge-triggered, as it would
84 * then be latched in.
85 */
86 write_msr(cnthp_ctl_el2, 0);
87 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
88 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
89 }
90}
91
92/**
93 * Restores the state of per-vCPU peripherals, such as the virtual timer.
94 */
95void begin_restoring_state(struct vcpu *vcpu)
96{
97 /*
98 * Clear timer control register before restoring compare value, to avoid
99 * a spurious timer interrupt. This could be a problem if the interrupt
100 * is configured as edge-triggered, as it would then be latched in.
101 */
102 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100103 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
104 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000105
106 /*
107 * If we are switching (back) to the primary, disable the EL2 physical
108 * timer which was being used to emulate the EL0 virtual timer, as the
109 * virtual timer is now running for the primary again.
110 */
111 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
112 write_msr(cnthp_ctl_el2, 0);
113 write_msr(cnthp_cval_el2, 0);
114 }
115}
116
Andrew Walbran1f32e722019-06-07 17:57:26 +0100117/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100118 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
119 * current VMID.
120 */
121static void invalidate_vm_tlb(void)
122{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100123 /*
124 * Ensure that the last VTTBR write has taken effect so we invalidate
125 * the right set of TLB entries.
126 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100127 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100128
Andrew Walbran1f32e722019-06-07 17:57:26 +0100129 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100130
131 /*
132 * Ensure that no instructions are fetched for the VM until after the
133 * TLB invalidation has taken effect.
134 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100135 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100136
137 /*
138 * Ensure that no data reads or writes for the VM happen until after the
Fuad Tabba77a4b012019-11-15 12:13:08 +0000139 * TLB invalidation has taken effect. Non-shareable is enough because
140 * the TLB is local to the CPU.
Andrew Walbrancff1f682019-07-04 14:52:45 +0100141 */
David Brazdil851948e2019-08-09 12:02:12 +0100142 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100143}
144
145/**
146 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
147 * the same VM which was run on the current pCPU.
148 *
149 * This is necessary because VMs may (contrary to the architecture
150 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
151 * workaround:
152 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
153 */
154void maybe_invalidate_tlb(struct vcpu *vcpu)
155{
156 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100157 ffa_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100158
159 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
160 new_vcpu_index) {
161 /*
162 * The vCPU has changed since the last time this VM was run on
163 * this pCPU, so we need to invalidate the TLB.
164 */
165 invalidate_vm_tlb();
166
167 /* Record the fact that this vCPU is now running on this CPU. */
168 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
169 new_vcpu_index;
170 }
171}
172
David Brazdil768f69c2019-12-19 15:46:12 +0000173noreturn void irq_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100174{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000175 (void)elr;
176 (void)spsr;
177
Fuad Tabbad1d67982020-01-08 11:28:29 +0000178 panic("IRQ from current exception level.");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100179}
180
David Brazdil768f69c2019-12-19 15:46:12 +0000181noreturn void fiq_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100182{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000183 (void)elr;
184 (void)spsr;
185
Fuad Tabbad1d67982020-01-08 11:28:29 +0000186 panic("FIQ from current exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000187}
188
David Brazdil768f69c2019-12-19 15:46:12 +0000189noreturn void serr_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000190{
191 (void)elr;
192 (void)spsr;
193
Fuad Tabbad1d67982020-01-08 11:28:29 +0000194 panic("SError from current exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000195}
196
David Brazdil768f69c2019-12-19 15:46:12 +0000197noreturn void sync_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000198{
199 uintreg_t esr = read_msr(esr_el2);
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000200 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000201
202 (void)spsr;
203
Fuad Tabbac76466d2019-09-06 10:42:12 +0100204 switch (ec) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000205 case EC_DATA_ABORT_SAME_EL:
Andrew Walbrane52006c2019-10-22 18:01:28 +0100206 if (!(esr & (1U << 10))) { /* Check FnV bit. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000207 dlog_error(
208 "Data abort: pc=%#x, esr=%#x, ec=%#x, "
209 "far=%#x\n",
210 elr, esr, ec, read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100211 } else {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000212 dlog_error(
213 "Data abort: pc=%#x, esr=%#x, ec=%#x, "
214 "far=invalid\n",
215 elr, esr, ec);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100216 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100217
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000218 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100219
220 default:
Andrew Walbran17eebf92020-02-05 16:35:49 +0000221 dlog_error(
222 "Unknown current sync exception pc=%#x, esr=%#x, "
223 "ec=%#x\n",
224 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100225 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100226 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000227
Andrew Sculla9c172d2019-04-03 14:10:00 +0100228 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100229}
230
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100231/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000232 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
233 * arch_regs.
234 */
235static void set_virtual_interrupt(struct arch_regs *r, bool enable)
236{
237 if (enable) {
238 r->lazy.hcr_el2 |= HCR_EL2_VI;
239 } else {
240 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
241 }
242}
243
244/**
245 * Sets or clears the VI bit in the HCR_EL2 register.
246 */
247static void set_virtual_interrupt_current(bool enable)
248{
249 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000250
Andrew Walbran3d84a262018-12-13 14:41:19 +0000251 if (enable) {
252 hcr_el2 |= HCR_EL2_VI;
253 } else {
254 hcr_el2 &= ~HCR_EL2_VI;
255 }
256 write_msr(hcr_el2, hcr_el2);
257}
258
Andrew Scullae9962e2019-10-03 16:51:16 +0100259/**
260 * Checks whether to block an SMC being forwarded from a VM.
261 */
262static bool smc_is_blocked(const struct vm *vm, uint32_t func)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100263{
Andrew Scullae9962e2019-10-03 16:51:16 +0100264 bool block_by_default = !vm->smc_whitelist.permissive;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100265
Andrew Scullae9962e2019-10-03 16:51:16 +0100266 for (size_t i = 0; i < vm->smc_whitelist.smc_count; ++i) {
267 if (func == vm->smc_whitelist.smcs[i]) {
268 return false;
269 }
270 }
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100271
Andrew Walbran17eebf92020-02-05 16:35:49 +0000272 dlog_notice("SMC %#010x attempted from VM %d, blocked=%d\n", func,
273 vm->id, block_by_default);
Andrew Scullae9962e2019-10-03 16:51:16 +0100274
275 /* Access is still allowed in permissive mode. */
276 return block_by_default;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100277}
278
279/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100280 * Applies SMC access control according to manifest and forwards the call if
281 * access is granted.
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100282 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100283static void smc_forwarder(const struct vm *vm, struct ffa_value *args)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100284{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100285 struct ffa_value ret;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000286 uint32_t client_id = vm->id;
287 uintreg_t arg7 = args->arg7;
Andrew Scullae9962e2019-10-03 16:51:16 +0100288
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000289 if (smc_is_blocked(vm, args->func)) {
290 args->func = SMCCC_ERROR_UNKNOWN;
Andrew Scullae9962e2019-10-03 16:51:16 +0100291 return;
292 }
293
Andrew Walbran0dd67ff2019-09-12 16:38:50 +0100294 /*
295 * Set the Client ID but keep the existing Secure OS ID and anything
296 * else (currently unspecified) that the client may have passed in the
297 * upper bits.
298 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000299 args->arg7 = client_id | (arg7 & ~CLIENT_ID_MASK);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000300 ret = smc_forward(args->func, args->arg1, args->arg2, args->arg3,
301 args->arg4, args->arg5, args->arg6, args->arg7);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100302
Andrew Scullae9962e2019-10-03 16:51:16 +0100303 /*
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000304 * Preserve the value passed by the caller, rather than the generated
305 * client_id. Note that this would also overwrite any return value that
Andrew Scullae9962e2019-10-03 16:51:16 +0100306 * may be in x7, but the SMCs that we are forwarding are legacy calls
307 * from before SMCCC 1.2 so won't have more than 4 return values anyway.
308 */
Andrew Scull07b6bd32019-12-12 17:19:55 +0000309 ret.arg7 = arg7;
310
311 plat_smc_post_forward(*args, &ret);
312
313 *args = ret;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100314}
315
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100316static bool ffa_handler(struct ffa_value *args, struct vcpu **next)
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100317{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000318 uint32_t func = args->func & ~SMCCC_CONVENTION_MASK;
319
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100320 /*
321 * NOTE: When adding new methods to this handler update
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100322 * api_ffa_features accordingly.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100323 */
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000324 switch (func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100325 case FFA_VERSION_32:
326 *args = api_ffa_version(args->arg1);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100327 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100328 case FFA_ID_GET_32:
329 *args = api_ffa_id_get(current());
Andrew Walbrand230f662019-10-07 18:03:36 +0100330 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100331 case FFA_FEATURES_32:
332 *args = api_ffa_features(args->arg1);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100333 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100334 case FFA_RX_RELEASE_32:
335 *args = api_ffa_rx_release(current(), next);
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +0000336 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100337 case FFA_RXTX_MAP_32:
338 *args = api_ffa_rxtx_map(ipa_init(args->arg1),
339 ipa_init(args->arg2), args->arg3,
340 current(), next);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000341 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100342 case FFA_YIELD_32:
Andrew Walbran16075b62019-09-03 17:11:07 +0100343 api_yield(current(), next);
344
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100345 /* FFA_YIELD always returns FFA_SUCCESS. */
346 *args = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran16075b62019-09-03 17:11:07 +0100347
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100348 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100349 case FFA_MSG_SEND_32:
350 *args = api_ffa_msg_send(
351 ffa_msg_send_sender(*args),
352 ffa_msg_send_receiver(*args), ffa_msg_send_size(*args),
353 ffa_msg_send_attributes(*args), current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100354 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100355 case FFA_MSG_WAIT_32:
356 *args = api_ffa_msg_recv(true, current(), next);
Andrew Walbran0de4f162019-09-03 16:44:20 +0100357 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100358 case FFA_MSG_POLL_32:
359 *args = api_ffa_msg_recv(false, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100360 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100361 case FFA_RUN_32:
362 *args = api_ffa_run(ffa_vm_id(*args), ffa_vcpu_index(*args),
363 current(), next);
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100364 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100365 case FFA_MEM_DONATE_32:
366 case FFA_MEM_LEND_32:
367 case FFA_MEM_SHARE_32:
368 *args = api_ffa_mem_send(func, args->arg1, args->arg2,
369 ipa_init(args->arg3), args->arg4,
Andrew Walbran1a86aa92020-05-15 17:22:28 +0100370 current());
Andrew Walbran82d6d152019-12-24 15:02:06 +0000371 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100372 case FFA_MEM_RETRIEVE_REQ_32:
373 *args = api_ffa_mem_retrieve_req(args->arg1, args->arg2,
374 ipa_init(args->arg3),
375 args->arg4, current());
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000376 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100377 case FFA_MEM_RELINQUISH_32:
378 *args = api_ffa_mem_relinquish(current());
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000379 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100380 case FFA_MEM_RECLAIM_32:
381 *args = api_ffa_mem_reclaim(
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100382 ffa_assemble_handle(args->arg1, args->arg2), args->arg3,
383 current());
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000384 return true;
Andrew Walbranca808b12020-05-15 17:22:28 +0100385 case FFA_MEM_FRAG_RX_32:
386 *args = api_ffa_mem_frag_rx(ffa_frag_handle(*args), args->arg3,
387 (args->arg4 >> 16) & 0xffff,
388 current());
389 return true;
390 case FFA_MEM_FRAG_TX_32:
391 *args = api_ffa_mem_frag_tx(ffa_frag_handle(*args), args->arg3,
392 (args->arg4 >> 16) & 0xffff,
393 current());
394 return true;
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100395 }
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100396
397 return false;
398}
399
400/**
401 * Set or clear VI bit according to pending interrupts.
402 */
403static void update_vi(struct vcpu *next)
404{
405 if (next == NULL) {
406 /*
407 * Not switching vCPUs, set the bit for the current vCPU
408 * directly in the register.
409 */
410 struct vcpu *vcpu = current();
411
412 sl_lock(&vcpu->lock);
413 set_virtual_interrupt_current(
414 vcpu->interrupts.enabled_and_pending_count > 0);
415 sl_unlock(&vcpu->lock);
416 } else {
417 /*
418 * About to switch vCPUs, set the bit for the vCPU to which we
419 * are switching in the saved copy of the register.
420 */
421 sl_lock(&next->lock);
422 set_virtual_interrupt(
423 &next->regs,
424 next->interrupts.enabled_and_pending_count > 0);
425 sl_unlock(&next->lock);
426 }
427}
428
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100429/**
430 * Processes SMC instruction calls.
431 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000432static struct vcpu *smc_handler(struct vcpu *vcpu)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100433{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100434 struct ffa_value args = {
Andrew Walbran85c37662019-12-05 16:29:33 +0000435 .func = vcpu->regs.r[0],
436 .arg1 = vcpu->regs.r[1],
437 .arg2 = vcpu->regs.r[2],
438 .arg3 = vcpu->regs.r[3],
439 .arg4 = vcpu->regs.r[4],
440 .arg5 = vcpu->regs.r[5],
441 .arg6 = vcpu->regs.r[6],
442 .arg7 = vcpu->regs.r[7],
443 };
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000444 struct vcpu *next = NULL;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100445
Andrew Walbran85c37662019-12-05 16:29:33 +0000446 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000447 &vcpu->regs.r[0], &next)) {
448 return next;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100449 }
450
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100451 if (ffa_handler(&args, &next)) {
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000452 arch_regs_set_retval(&vcpu->regs, args);
453 update_vi(next);
454 return next;
Andrew Walbran4579f7002019-08-30 16:24:58 +0100455 }
456
Andrew Walbran85c37662019-12-05 16:29:33 +0000457 switch (args.func & ~SMCCC_CONVENTION_MASK) {
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100458 case HF_DEBUG_LOG:
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000459 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000460 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100461 }
462
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000463 smc_forwarder(vcpu->vm, &args);
464 arch_regs_set_retval(&vcpu->regs, args);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000465 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100466}
467
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000468/*
469 * Exception vector offsets.
470 * See Arm Architecture Reference Manual Armv8-A, D1.10.2.
471 */
472
473/**
474 * Offset for synchronous exceptions at current EL with SPx.
475 */
476#define OFFSET_CURRENT_SPX UINT64_C(0x200)
477
478/**
479 * Offset for synchronous exceptions at lower EL using AArch64.
480 */
481#define OFFSET_LOWER_EL_64 UINT64_C(0x400)
482
483/**
484 * Offset for synchronous exceptions at lower EL using AArch32.
485 */
486#define OFFSET_LOWER_EL_32 UINT64_C(0x600)
487
488/**
489 * Returns the address for the exception handler at EL1.
490 */
491static uintreg_t get_el1_exception_handler_addr(const struct vcpu *vcpu)
492{
493 uintreg_t base_addr = read_msr(vbar_el1);
494 uintreg_t pe_mode = vcpu->regs.spsr & PSR_PE_MODE_MASK;
495 bool is_arch32 = vcpu->regs.spsr & PSR_ARCH_MODE_32;
496
497 if (pe_mode == PSR_PE_MODE_EL0T) {
498 if (is_arch32) {
499 base_addr += OFFSET_LOWER_EL_32;
500 } else {
501 base_addr += OFFSET_LOWER_EL_64;
502 }
503 } else {
504 CHECK(!is_arch32);
505 base_addr += OFFSET_CURRENT_SPX;
506 }
507
508 return base_addr;
509}
510
511/**
Fuad Tabbab86325a2020-01-10 13:38:15 +0000512 * Injects an exception with the specified Exception Syndrom Register value into
513 * the EL1.
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000514 * See Arm Architecture Reference Manual Armv8-A, page D13-2924.
515 *
516 * NOTE: This function assumes that the lazy registers haven't been saved, and
517 * writes to the lazy registers of the CPU directly instead of the vCPU.
518 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000519static void inject_el1_exception(struct vcpu *vcpu, uintreg_t esr_el1_value)
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000520{
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000521 uintreg_t handler_address = get_el1_exception_handler_addr(vcpu);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000522
523 /* Update the CPU state to inject the exception. */
524 write_msr(esr_el1, esr_el1_value);
525 write_msr(elr_el1, vcpu->regs.pc);
526 write_msr(spsr_el1, vcpu->regs.spsr);
527
528 /*
529 * Mask (disable) interrupts and run in EL1h mode.
530 * EL1h mode is used because by default, taking an exception selects the
531 * stack pointer for the target Exception level. The software can change
532 * that later in the handler if needed.
533 * See Arm Architecture Reference Manual Armv8-A, page D13-2924
534 */
535 vcpu->regs.spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
536
537 /* Transfer control to the exception hander. */
538 vcpu->regs.pc = handler_address;
Fuad Tabbab86325a2020-01-10 13:38:15 +0000539}
540
541/**
542 * Injects a Data Abort exception (same exception level).
543 */
544static void inject_el1_data_abort_exception(struct vcpu *vcpu,
545 uintreg_t esr_el2)
546{
547 /*
548 * ISS encoding remains the same, but the EC is changed to reflect
549 * where the exception came from.
550 * See Arm Architecture Reference Manual Armv8-A, pages D13-2943/2982.
551 */
552 uintreg_t esr_el1_value = GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) |
553 (EC_DATA_ABORT_SAME_EL << ESR_EC_OFFSET);
554
Andrew Walbran17eebf92020-02-05 16:35:49 +0000555 dlog_notice("Injecting Data Abort exception into VM%d.\n",
556 vcpu->vm->id);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000557
558 inject_el1_exception(vcpu, esr_el1_value);
559}
560
561/**
562 * Injects a Data Abort exception (same exception level).
563 */
564static void inject_el1_instruction_abort_exception(struct vcpu *vcpu,
565 uintreg_t esr_el2)
566{
567 /*
568 * ISS encoding remains the same, but the EC is changed to reflect
569 * where the exception came from.
570 * See Arm Architecture Reference Manual Armv8-A, pages D13-2941/2980.
571 */
572 uintreg_t esr_el1_value =
573 GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) |
574 (EC_INSTRUCTION_ABORT_SAME_EL << ESR_EC_OFFSET);
575
Andrew Walbran17eebf92020-02-05 16:35:49 +0000576 dlog_notice("Injecting Instruction Abort exception into VM%d.\n",
577 vcpu->vm->id);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000578
579 inject_el1_exception(vcpu, esr_el1_value);
580}
581
582/**
583 * Injects an exception with an unknown reason into the EL1.
584 */
585static void inject_el1_unknown_exception(struct vcpu *vcpu, uintreg_t esr_el2)
586{
587 uintreg_t esr_el1_value =
588 GET_ESR_IL(esr_el2) | (EC_UNKNOWN << ESR_EC_OFFSET);
589 char *direction_str;
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000590
591 direction_str = ISS_IS_READ(esr_el2) ? "read" : "write";
Andrew Walbran17eebf92020-02-05 16:35:49 +0000592 dlog_notice(
593 "Trapped access to system register %s: op0=%d, op1=%d, crn=%d, "
594 "crm=%d, op2=%d, rt=%d.\n",
595 direction_str, GET_ISS_OP0(esr_el2), GET_ISS_OP1(esr_el2),
596 GET_ISS_CRN(esr_el2), GET_ISS_CRM(esr_el2),
597 GET_ISS_OP2(esr_el2), GET_ISS_RT(esr_el2));
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000598
Andrew Walbran17eebf92020-02-05 16:35:49 +0000599 dlog_notice("Injecting Unknown Reason exception into VM%d.\n",
600 vcpu->vm->id);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000601
Fuad Tabbab86325a2020-01-10 13:38:15 +0000602 inject_el1_exception(vcpu, esr_el1_value);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000603}
604
Andrew Walbran59182d52019-09-23 17:55:39 +0100605struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100606{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100607 struct ffa_value args = {
Andrew Walbran7f920af2019-09-03 17:09:30 +0100608 .func = vcpu->regs.r[0],
609 .arg1 = vcpu->regs.r[1],
610 .arg2 = vcpu->regs.r[2],
611 .arg3 = vcpu->regs.r[3],
612 .arg4 = vcpu->regs.r[4],
613 .arg5 = vcpu->regs.r[5],
614 .arg6 = vcpu->regs.r[6],
615 .arg7 = vcpu->regs.r[7],
616 };
Andrew Walbran59182d52019-09-23 17:55:39 +0100617 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100618
Andrew Walbran7f920af2019-09-03 17:09:30 +0100619 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
620 &vcpu->regs.r[0], &next)) {
Andrew Walbran59182d52019-09-23 17:55:39 +0100621 return next;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100622 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100623
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100624 if (ffa_handler(&args, &next)) {
Andrew Walbran6f56d7b2019-12-05 16:27:34 +0000625 arch_regs_set_retval(&vcpu->regs, args);
Andrew Walbran59182d52019-09-23 17:55:39 +0100626 update_vi(next);
627 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100628 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100629
Andrew Walbran7f920af2019-09-03 17:09:30 +0100630 switch (args.func) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100631 case HF_VM_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100632 vcpu->regs.r[0] = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100633 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100634
635 case HF_VCPU_GET_COUNT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100636 vcpu->regs.r[0] = api_vcpu_get_count(args.arg1, vcpu);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100637 break;
638
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000639 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100640 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000641 break;
642
643 case HF_MAILBOX_WAITER_GET:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100644 vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100645 break;
646
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000647 case HF_INTERRUPT_ENABLE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100648 vcpu->regs.r[0] =
649 api_interrupt_enable(args.arg1, args.arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000650 break;
651
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000652 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100653 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000654 break;
655
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000656 case HF_INTERRUPT_INJECT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100657 vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2,
658 args.arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000659 break;
660
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100661 case HF_DEBUG_LOG:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100662 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100663 break;
664
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100665 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100666 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100667 }
668
Andrew Walbran59182d52019-09-23 17:55:39 +0100669 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000670
Andrew Walbran59182d52019-09-23 17:55:39 +0100671 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100672}
673
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100674struct vcpu *irq_lower(void)
675{
Andrew Scull9726c252019-01-23 13:44:19 +0000676 /*
677 * Switch back to primary VM, interrupts will be handled there.
678 *
679 * If the VM has aborted, this vCPU will be aborted when the scheduler
680 * tries to run it again. This means the interrupt will not be delayed
681 * by the aborted VM.
682 *
683 * TODO: Only switch when the interrupt isn't for the current VM.
684 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000685 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100686}
687
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000688struct vcpu *fiq_lower(void)
689{
690 return irq_lower();
691}
692
Fuad Tabbad1d67982020-01-08 11:28:29 +0000693noreturn struct vcpu *serr_lower(void)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000694{
Fuad Tabbad1d67982020-01-08 11:28:29 +0000695 /*
696 * SError exceptions should be isolated and handled by the responsible
697 * VM/exception level. Getting here indicates a bug, that isolation is
698 * not working, or a processor that does not support ARMv8.2-IESB, in
699 * which case Hafnium routes SError exceptions to EL2 (here).
700 */
701 panic("SError from a lower exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000702}
703
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000704/**
705 * Initialises a fault info structure. It assumes that an FnV bit exists at
706 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
707 * the ESR (the fault status code) are 010000; this is the case for both
708 * instruction and data aborts, but not necessarily for other exception reasons.
709 */
710static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100711 const struct vcpu *vcpu,
712 uint32_t mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000713{
714 uint32_t fsc = esr & 0x3f;
715 struct vcpu_fault_info r;
716
717 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000718 r.pc = va_init(vcpu->regs.pc);
719
720 /*
721 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
722 * indicates that we cannot rely on far_el2.
723 */
Andrew Walbrane52006c2019-10-22 18:01:28 +0100724 if (fsc == 0x10 && esr & (1U << 10)) {
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000725 r.vaddr = va_init(0);
726 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
727 } else {
728 r.vaddr = va_init(read_msr(far_el2));
729 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
730 (read_msr(far_el2) & (PAGE_SIZE - 1)));
731 }
732
733 return r;
734}
735
Andrew Scull37402872018-10-24 14:23:06 +0100736struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100737{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100738 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000739 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000740 struct vcpu *new_vcpu;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000741 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100742
Fuad Tabbac76466d2019-09-06 10:42:12 +0100743 switch (ec) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000744 case EC_WFI_WFE:
Andrew Walbran48196eb2019-03-04 14:56:24 +0000745 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100746 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100747 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100748 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000749 /* WFE */
750 /*
751 * TODO: consider giving the scheduler more context,
752 * somehow.
753 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100754 api_yield(vcpu, &new_vcpu);
Jose Marinho135dff32019-02-28 10:25:57 +0000755 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100756 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000757 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000758 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100759
Fuad Tabbab86325a2020-01-10 13:38:15 +0000760 case EC_DATA_ABORT_LOWER_EL:
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000761 info = fault_info_init(
Andrew Walbrane52006c2019-10-22 18:01:28 +0100762 esr, vcpu, (esr & (1U << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000763 if (vcpu_handle_page_fault(vcpu, &info)) {
764 return NULL;
765 }
Fuad Tabbab86325a2020-01-10 13:38:15 +0000766 /* Inform the EL1 of the data abort. */
767 inject_el1_data_abort_exception(vcpu, esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100768
Fuad Tabbab86325a2020-01-10 13:38:15 +0000769 /* Schedule the same VM to continue running. */
770 return NULL;
771
772 case EC_INSTRUCTION_ABORT_LOWER_EL:
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100773 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000774 if (vcpu_handle_page_fault(vcpu, &info)) {
775 return NULL;
776 }
Fuad Tabbab86325a2020-01-10 13:38:15 +0000777 /* Inform the EL1 of the instruction abort. */
778 inject_el1_instruction_abort_exception(vcpu, esr);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100779
Fuad Tabbab86325a2020-01-10 13:38:15 +0000780 /* Schedule the same VM to continue running. */
781 return NULL;
782
783 case EC_HVC:
Andrew Walbran59182d52019-09-23 17:55:39 +0100784 return hvc_handler(vcpu);
785
Fuad Tabbab86325a2020-01-10 13:38:15 +0000786 case EC_SMC: {
Andrew Scullc960c032018-10-24 15:13:35 +0100787 uintreg_t smc_pc = vcpu->regs.pc;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000788 struct vcpu *next = smc_handler(vcpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100789
790 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100791 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000792
Andrew Walbran33645652019-04-15 12:29:31 +0100793 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100794 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100795
Fuad Tabbab86325a2020-01-10 13:38:15 +0000796 case EC_MSR:
Fuad Tabbac76466d2019-09-06 10:42:12 +0100797 /*
798 * NOTE: This should never be reached because it goes through a
799 * separate path handled by handle_system_register_access().
800 */
801 panic("Handled by handle_system_register_access().");
802
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100803 default:
Andrew Walbran17eebf92020-02-05 16:35:49 +0000804 dlog_notice(
805 "Unknown lower sync exception pc=%#x, esr=%#x, "
806 "ec=%#x\n",
807 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000808 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100809 }
810
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000811 /*
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000812 * The exception wasn't handled. Inject to the VM to give it chance to
813 * handle as an unknown exception.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000814 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000815 inject_el1_unknown_exception(vcpu, esr);
816
817 /* Schedule the same VM to continue running. */
818 return NULL;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000819}
820
Fuad Tabbac76466d2019-09-06 10:42:12 +0100821/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000822 * Handles EC = 011000, MSR, MRS instruction traps.
Fuad Tabbaed294af2019-12-20 10:43:01 +0000823 * Returns non-null ONLY if the access failed and the vCPU is changing.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100824 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000825void handle_system_register_access(uintreg_t esr_el2)
Fuad Tabbac76466d2019-09-06 10:42:12 +0100826{
827 struct vcpu *vcpu = current();
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100828 ffa_vm_id_t vm_id = vcpu->vm->id;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000829 uintreg_t ec = GET_ESR_EC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100830
Fuad Tabbab86325a2020-01-10 13:38:15 +0000831 CHECK(ec == EC_MSR);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100832 /*
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100833 * Handle accesses to debug and performance monitor registers.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000834 * Inject an exception for unhandled/unsupported registers.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100835 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000836 if (debug_el1_is_register_access(esr_el2)) {
837 if (!debug_el1_process_access(vcpu, vm_id, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000838 inject_el1_unknown_exception(vcpu, esr_el2);
839 return;
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100840 }
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000841 } else if (perfmon_is_register_access(esr_el2)) {
842 if (!perfmon_process_access(vcpu, vm_id, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000843 inject_el1_unknown_exception(vcpu, esr_el2);
844 return;
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100845 }
Fuad Tabba77a4b012019-11-15 12:13:08 +0000846 } else if (feature_id_is_register_access(esr_el2)) {
847 if (!feature_id_process_access(vcpu, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000848 inject_el1_unknown_exception(vcpu, esr_el2);
849 return;
Fuad Tabba77a4b012019-11-15 12:13:08 +0000850 }
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100851 } else {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000852 inject_el1_unknown_exception(vcpu, esr_el2);
853 return;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100854 }
855
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100856 /* Instruction was fulfilled. Skip it and run the next one. */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000857 vcpu->regs.pc += GET_NEXT_PC_INC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100858}