blob: 280fb71e94454f4b2018984441660b96b6f49291 [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"
David Brazdil851948e2019-08-09 12:02:12 +010013#include "hf/arch/mm.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/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010036 * Gets the value to increment for the next PC.
37 * The ESR encodes whether the instruction is 2 bytes or 4 bytes long.
38 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +000039#define GET_NEXT_PC_INC(esr) (GET_ESR_IL(esr) ? 4 : 2)
Fuad Tabbac76466d2019-09-06 10:42:12 +010040
Fuad Tabbac76466d2019-09-06 10:42:12 +010041/**
Andrew Walbran0dd67ff2019-09-12 16:38:50 +010042 * The Client ID field within X7 for an SMC64 call.
43 */
44#define CLIENT_ID_MASK UINT64_C(0xffff)
45
46/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010047 * Returns a reference to the currently executing vCPU.
48 */
Andrew Scullc960c032018-10-24 15:13:35 +010049static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000050{
51 return (struct vcpu *)read_msr(tpidr_el2);
52}
53
Andrew Walbran1f8d4872018-12-20 11:21:32 +000054/**
55 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
56 * informs the arch-independent sections that registers have been saved.
57 */
58void complete_saving_state(struct vcpu *vcpu)
59{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010060 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
61 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000062
63 api_regs_state_saved(vcpu);
64
65 /*
66 * If switching away from the primary, copy the current EL0 virtual
67 * timer registers to the corresponding EL2 physical timer registers.
68 * This is used to emulate the virtual timer for the primary in case it
69 * should fire while the secondary is running.
70 */
71 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
72 /*
73 * Clear timer control register before copying compare value, to
74 * avoid a spurious timer interrupt. This could be a problem if
75 * the interrupt is configured as edge-triggered, as it would
76 * then be latched in.
77 */
78 write_msr(cnthp_ctl_el2, 0);
79 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
80 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
81 }
82}
83
84/**
85 * Restores the state of per-vCPU peripherals, such as the virtual timer.
86 */
87void begin_restoring_state(struct vcpu *vcpu)
88{
89 /*
90 * Clear timer control register before restoring compare value, to avoid
91 * a spurious timer interrupt. This could be a problem if the interrupt
92 * is configured as edge-triggered, as it would then be latched in.
93 */
94 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +010095 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
96 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000097
98 /*
99 * If we are switching (back) to the primary, disable the EL2 physical
100 * timer which was being used to emulate the EL0 virtual timer, as the
101 * virtual timer is now running for the primary again.
102 */
103 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
104 write_msr(cnthp_ctl_el2, 0);
105 write_msr(cnthp_cval_el2, 0);
106 }
107}
108
Andrew Walbran1f32e722019-06-07 17:57:26 +0100109/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100110 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
111 * current VMID.
112 */
113static void invalidate_vm_tlb(void)
114{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100115 /*
116 * Ensure that the last VTTBR write has taken effect so we invalidate
117 * the right set of TLB entries.
118 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100119 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100120
Andrew Walbran1f32e722019-06-07 17:57:26 +0100121 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100122
123 /*
124 * Ensure that no instructions are fetched for the VM until after the
125 * TLB invalidation has taken effect.
126 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100127 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100128
129 /*
130 * Ensure that no data reads or writes for the VM happen until after the
Fuad Tabba77a4b012019-11-15 12:13:08 +0000131 * TLB invalidation has taken effect. Non-shareable is enough because
132 * the TLB is local to the CPU.
Andrew Walbrancff1f682019-07-04 14:52:45 +0100133 */
David Brazdil851948e2019-08-09 12:02:12 +0100134 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100135}
136
137/**
138 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
139 * the same VM which was run on the current pCPU.
140 *
141 * This is necessary because VMs may (contrary to the architecture
142 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
143 * workaround:
144 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
145 */
146void maybe_invalidate_tlb(struct vcpu *vcpu)
147{
148 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100149 ffa_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100150
151 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
152 new_vcpu_index) {
153 /*
154 * The vCPU has changed since the last time this VM was run on
155 * this pCPU, so we need to invalidate the TLB.
156 */
157 invalidate_vm_tlb();
158
159 /* Record the fact that this vCPU is now running on this CPU. */
160 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
161 new_vcpu_index;
162 }
163}
164
David Brazdil768f69c2019-12-19 15:46:12 +0000165noreturn void irq_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100166{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000167 (void)elr;
168 (void)spsr;
169
Fuad Tabbad1d67982020-01-08 11:28:29 +0000170 panic("IRQ from current exception level.");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100171}
172
David Brazdil768f69c2019-12-19 15:46:12 +0000173noreturn void fiq_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("FIQ from current exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000179}
180
David Brazdil768f69c2019-12-19 15:46:12 +0000181noreturn void serr_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000182{
183 (void)elr;
184 (void)spsr;
185
Fuad Tabbad1d67982020-01-08 11:28:29 +0000186 panic("SError from current exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000187}
188
David Brazdil768f69c2019-12-19 15:46:12 +0000189noreturn void sync_current_exception_noreturn(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000190{
191 uintreg_t esr = read_msr(esr_el2);
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000192 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000193
194 (void)spsr;
195
Fuad Tabbac76466d2019-09-06 10:42:12 +0100196 switch (ec) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000197 case EC_DATA_ABORT_SAME_EL:
Andrew Walbrane52006c2019-10-22 18:01:28 +0100198 if (!(esr & (1U << 10))) { /* Check FnV bit. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000199 dlog_error(
200 "Data abort: pc=%#x, esr=%#x, ec=%#x, "
201 "far=%#x\n",
202 elr, esr, ec, read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100203 } else {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000204 dlog_error(
205 "Data abort: pc=%#x, esr=%#x, ec=%#x, "
206 "far=invalid\n",
207 elr, esr, ec);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100208 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100209
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000210 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100211
212 default:
Andrew Walbran17eebf92020-02-05 16:35:49 +0000213 dlog_error(
214 "Unknown current sync exception pc=%#x, esr=%#x, "
215 "ec=%#x\n",
216 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100217 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100218 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000219
Andrew Sculla9c172d2019-04-03 14:10:00 +0100220 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100221}
222
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100223/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000224 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
225 * arch_regs.
226 */
227static void set_virtual_interrupt(struct arch_regs *r, bool enable)
228{
229 if (enable) {
230 r->lazy.hcr_el2 |= HCR_EL2_VI;
231 } else {
232 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
233 }
234}
235
236/**
237 * Sets or clears the VI bit in the HCR_EL2 register.
238 */
239static void set_virtual_interrupt_current(bool enable)
240{
241 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000242
Andrew Walbran3d84a262018-12-13 14:41:19 +0000243 if (enable) {
244 hcr_el2 |= HCR_EL2_VI;
245 } else {
246 hcr_el2 &= ~HCR_EL2_VI;
247 }
248 write_msr(hcr_el2, hcr_el2);
249}
250
Andrew Scullae9962e2019-10-03 16:51:16 +0100251/**
252 * Checks whether to block an SMC being forwarded from a VM.
253 */
254static bool smc_is_blocked(const struct vm *vm, uint32_t func)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100255{
Andrew Scullae9962e2019-10-03 16:51:16 +0100256 bool block_by_default = !vm->smc_whitelist.permissive;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100257
Andrew Scullae9962e2019-10-03 16:51:16 +0100258 for (size_t i = 0; i < vm->smc_whitelist.smc_count; ++i) {
259 if (func == vm->smc_whitelist.smcs[i]) {
260 return false;
261 }
262 }
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100263
Andrew Walbran17eebf92020-02-05 16:35:49 +0000264 dlog_notice("SMC %#010x attempted from VM %d, blocked=%d\n", func,
265 vm->id, block_by_default);
Andrew Scullae9962e2019-10-03 16:51:16 +0100266
267 /* Access is still allowed in permissive mode. */
268 return block_by_default;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100269}
270
271/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100272 * Applies SMC access control according to manifest and forwards the call if
273 * access is granted.
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100274 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100275static void smc_forwarder(const struct vm *vm, struct ffa_value *args)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100276{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100277 struct ffa_value ret;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000278 uint32_t client_id = vm->id;
279 uintreg_t arg7 = args->arg7;
Andrew Scullae9962e2019-10-03 16:51:16 +0100280
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000281 if (smc_is_blocked(vm, args->func)) {
282 args->func = SMCCC_ERROR_UNKNOWN;
Andrew Scullae9962e2019-10-03 16:51:16 +0100283 return;
284 }
285
Andrew Walbran0dd67ff2019-09-12 16:38:50 +0100286 /*
287 * Set the Client ID but keep the existing Secure OS ID and anything
288 * else (currently unspecified) that the client may have passed in the
289 * upper bits.
290 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000291 args->arg7 = client_id | (arg7 & ~CLIENT_ID_MASK);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000292 ret = smc_forward(args->func, args->arg1, args->arg2, args->arg3,
293 args->arg4, args->arg5, args->arg6, args->arg7);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100294
Andrew Scullae9962e2019-10-03 16:51:16 +0100295 /*
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000296 * Preserve the value passed by the caller, rather than the generated
297 * client_id. Note that this would also overwrite any return value that
Andrew Scullae9962e2019-10-03 16:51:16 +0100298 * may be in x7, but the SMCs that we are forwarding are legacy calls
299 * from before SMCCC 1.2 so won't have more than 4 return values anyway.
300 */
Andrew Scull07b6bd32019-12-12 17:19:55 +0000301 ret.arg7 = arg7;
302
303 plat_smc_post_forward(*args, &ret);
304
305 *args = ret;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100306}
307
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100308static bool ffa_handler(struct ffa_value *args, struct vcpu **next)
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100309{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000310 uint32_t func = args->func & ~SMCCC_CONVENTION_MASK;
311
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100312 /*
313 * NOTE: When adding new methods to this handler update
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100314 * api_ffa_features accordingly.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100315 */
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000316 switch (func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100317 case FFA_VERSION_32:
318 *args = api_ffa_version(args->arg1);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100319 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100320 case FFA_ID_GET_32:
321 *args = api_ffa_id_get(current());
Andrew Walbrand230f662019-10-07 18:03:36 +0100322 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100323 case FFA_FEATURES_32:
324 *args = api_ffa_features(args->arg1);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100325 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100326 case FFA_RX_RELEASE_32:
327 *args = api_ffa_rx_release(current(), next);
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +0000328 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100329 case FFA_RXTX_MAP_32:
330 *args = api_ffa_rxtx_map(ipa_init(args->arg1),
331 ipa_init(args->arg2), args->arg3,
332 current(), next);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000333 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100334 case FFA_YIELD_32:
Andrew Walbran16075b62019-09-03 17:11:07 +0100335 api_yield(current(), next);
336
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100337 /* FFA_YIELD always returns FFA_SUCCESS. */
338 *args = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran16075b62019-09-03 17:11:07 +0100339
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100340 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100341 case FFA_MSG_SEND_32:
342 *args = api_ffa_msg_send(
343 ffa_msg_send_sender(*args),
344 ffa_msg_send_receiver(*args), ffa_msg_send_size(*args),
345 ffa_msg_send_attributes(*args), current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100346 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100347 case FFA_MSG_WAIT_32:
348 *args = api_ffa_msg_recv(true, current(), next);
Andrew Walbran0de4f162019-09-03 16:44:20 +0100349 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100350 case FFA_MSG_POLL_32:
351 *args = api_ffa_msg_recv(false, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100352 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100353 case FFA_RUN_32:
354 *args = api_ffa_run(ffa_vm_id(*args), ffa_vcpu_index(*args),
355 current(), next);
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100356 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100357 case FFA_MEM_DONATE_32:
358 case FFA_MEM_LEND_32:
359 case FFA_MEM_SHARE_32:
360 *args = api_ffa_mem_send(func, args->arg1, args->arg2,
361 ipa_init(args->arg3), args->arg4,
Andrew Walbran1a86aa92020-05-15 17:22:28 +0100362 current());
Andrew Walbran82d6d152019-12-24 15:02:06 +0000363 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364 case FFA_MEM_RETRIEVE_REQ_32:
365 *args = api_ffa_mem_retrieve_req(args->arg1, args->arg2,
366 ipa_init(args->arg3),
367 args->arg4, current());
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000368 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100369 case FFA_MEM_RELINQUISH_32:
370 *args = api_ffa_mem_relinquish(current());
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000371 return true;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100372 case FFA_MEM_RECLAIM_32:
373 *args = api_ffa_mem_reclaim(
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100374 ffa_assemble_handle(args->arg1, args->arg2), args->arg3,
375 current());
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000376 return true;
Andrew Walbranca808b12020-05-15 17:22:28 +0100377 case FFA_MEM_FRAG_RX_32:
378 *args = api_ffa_mem_frag_rx(ffa_frag_handle(*args), args->arg3,
379 (args->arg4 >> 16) & 0xffff,
380 current());
381 return true;
382 case FFA_MEM_FRAG_TX_32:
383 *args = api_ffa_mem_frag_tx(ffa_frag_handle(*args), args->arg3,
384 (args->arg4 >> 16) & 0xffff,
385 current());
386 return true;
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100387 }
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100388
389 return false;
390}
391
392/**
393 * Set or clear VI bit according to pending interrupts.
394 */
395static void update_vi(struct vcpu *next)
396{
397 if (next == NULL) {
398 /*
399 * Not switching vCPUs, set the bit for the current vCPU
400 * directly in the register.
401 */
402 struct vcpu *vcpu = current();
403
404 sl_lock(&vcpu->lock);
405 set_virtual_interrupt_current(
406 vcpu->interrupts.enabled_and_pending_count > 0);
407 sl_unlock(&vcpu->lock);
408 } else {
409 /*
410 * About to switch vCPUs, set the bit for the vCPU to which we
411 * are switching in the saved copy of the register.
412 */
413 sl_lock(&next->lock);
414 set_virtual_interrupt(
415 &next->regs,
416 next->interrupts.enabled_and_pending_count > 0);
417 sl_unlock(&next->lock);
418 }
419}
420
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100421/**
422 * Processes SMC instruction calls.
423 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000424static struct vcpu *smc_handler(struct vcpu *vcpu)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100425{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100426 struct ffa_value args = {
Andrew Walbran85c37662019-12-05 16:29:33 +0000427 .func = vcpu->regs.r[0],
428 .arg1 = vcpu->regs.r[1],
429 .arg2 = vcpu->regs.r[2],
430 .arg3 = vcpu->regs.r[3],
431 .arg4 = vcpu->regs.r[4],
432 .arg5 = vcpu->regs.r[5],
433 .arg6 = vcpu->regs.r[6],
434 .arg7 = vcpu->regs.r[7],
435 };
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000436 struct vcpu *next = NULL;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100437
Andrew Walbran85c37662019-12-05 16:29:33 +0000438 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000439 &vcpu->regs.r[0], &next)) {
440 return next;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100441 }
442
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100443 if (ffa_handler(&args, &next)) {
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000444 arch_regs_set_retval(&vcpu->regs, args);
445 update_vi(next);
446 return next;
Andrew Walbran4579f7002019-08-30 16:24:58 +0100447 }
448
Andrew Walbran85c37662019-12-05 16:29:33 +0000449 switch (args.func & ~SMCCC_CONVENTION_MASK) {
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100450 case HF_DEBUG_LOG:
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000451 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000452 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100453 }
454
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000455 smc_forwarder(vcpu->vm, &args);
456 arch_regs_set_retval(&vcpu->regs, args);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000457 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100458}
459
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000460/*
461 * Exception vector offsets.
462 * See Arm Architecture Reference Manual Armv8-A, D1.10.2.
463 */
464
465/**
466 * Offset for synchronous exceptions at current EL with SPx.
467 */
468#define OFFSET_CURRENT_SPX UINT64_C(0x200)
469
470/**
471 * Offset for synchronous exceptions at lower EL using AArch64.
472 */
473#define OFFSET_LOWER_EL_64 UINT64_C(0x400)
474
475/**
476 * Offset for synchronous exceptions at lower EL using AArch32.
477 */
478#define OFFSET_LOWER_EL_32 UINT64_C(0x600)
479
480/**
481 * Returns the address for the exception handler at EL1.
482 */
483static uintreg_t get_el1_exception_handler_addr(const struct vcpu *vcpu)
484{
485 uintreg_t base_addr = read_msr(vbar_el1);
486 uintreg_t pe_mode = vcpu->regs.spsr & PSR_PE_MODE_MASK;
487 bool is_arch32 = vcpu->regs.spsr & PSR_ARCH_MODE_32;
488
489 if (pe_mode == PSR_PE_MODE_EL0T) {
490 if (is_arch32) {
491 base_addr += OFFSET_LOWER_EL_32;
492 } else {
493 base_addr += OFFSET_LOWER_EL_64;
494 }
495 } else {
496 CHECK(!is_arch32);
497 base_addr += OFFSET_CURRENT_SPX;
498 }
499
500 return base_addr;
501}
502
503/**
Fuad Tabbab86325a2020-01-10 13:38:15 +0000504 * Injects an exception with the specified Exception Syndrom Register value into
505 * the EL1.
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000506 * See Arm Architecture Reference Manual Armv8-A, page D13-2924.
507 *
508 * NOTE: This function assumes that the lazy registers haven't been saved, and
509 * writes to the lazy registers of the CPU directly instead of the vCPU.
510 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000511static void inject_el1_exception(struct vcpu *vcpu, uintreg_t esr_el1_value)
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000512{
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000513 uintreg_t handler_address = get_el1_exception_handler_addr(vcpu);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000514
515 /* Update the CPU state to inject the exception. */
516 write_msr(esr_el1, esr_el1_value);
517 write_msr(elr_el1, vcpu->regs.pc);
518 write_msr(spsr_el1, vcpu->regs.spsr);
519
520 /*
521 * Mask (disable) interrupts and run in EL1h mode.
522 * EL1h mode is used because by default, taking an exception selects the
523 * stack pointer for the target Exception level. The software can change
524 * that later in the handler if needed.
525 * See Arm Architecture Reference Manual Armv8-A, page D13-2924
526 */
527 vcpu->regs.spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
528
529 /* Transfer control to the exception hander. */
530 vcpu->regs.pc = handler_address;
Fuad Tabbab86325a2020-01-10 13:38:15 +0000531}
532
533/**
534 * Injects a Data Abort exception (same exception level).
535 */
536static void inject_el1_data_abort_exception(struct vcpu *vcpu,
537 uintreg_t esr_el2)
538{
539 /*
540 * ISS encoding remains the same, but the EC is changed to reflect
541 * where the exception came from.
542 * See Arm Architecture Reference Manual Armv8-A, pages D13-2943/2982.
543 */
544 uintreg_t esr_el1_value = GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) |
545 (EC_DATA_ABORT_SAME_EL << ESR_EC_OFFSET);
546
Andrew Walbran17eebf92020-02-05 16:35:49 +0000547 dlog_notice("Injecting Data Abort exception into VM%d.\n",
548 vcpu->vm->id);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000549
550 inject_el1_exception(vcpu, esr_el1_value);
551}
552
553/**
554 * Injects a Data Abort exception (same exception level).
555 */
556static void inject_el1_instruction_abort_exception(struct vcpu *vcpu,
557 uintreg_t esr_el2)
558{
559 /*
560 * ISS encoding remains the same, but the EC is changed to reflect
561 * where the exception came from.
562 * See Arm Architecture Reference Manual Armv8-A, pages D13-2941/2980.
563 */
564 uintreg_t esr_el1_value =
565 GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) |
566 (EC_INSTRUCTION_ABORT_SAME_EL << ESR_EC_OFFSET);
567
Andrew Walbran17eebf92020-02-05 16:35:49 +0000568 dlog_notice("Injecting Instruction Abort exception into VM%d.\n",
569 vcpu->vm->id);
Fuad Tabbab86325a2020-01-10 13:38:15 +0000570
571 inject_el1_exception(vcpu, esr_el1_value);
572}
573
574/**
575 * Injects an exception with an unknown reason into the EL1.
576 */
577static void inject_el1_unknown_exception(struct vcpu *vcpu, uintreg_t esr_el2)
578{
579 uintreg_t esr_el1_value =
580 GET_ESR_IL(esr_el2) | (EC_UNKNOWN << ESR_EC_OFFSET);
581 char *direction_str;
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000582
583 direction_str = ISS_IS_READ(esr_el2) ? "read" : "write";
Andrew Walbran17eebf92020-02-05 16:35:49 +0000584 dlog_notice(
585 "Trapped access to system register %s: op0=%d, op1=%d, crn=%d, "
586 "crm=%d, op2=%d, rt=%d.\n",
587 direction_str, GET_ISS_OP0(esr_el2), GET_ISS_OP1(esr_el2),
588 GET_ISS_CRN(esr_el2), GET_ISS_CRM(esr_el2),
589 GET_ISS_OP2(esr_el2), GET_ISS_RT(esr_el2));
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000590
Andrew Walbran17eebf92020-02-05 16:35:49 +0000591 dlog_notice("Injecting Unknown Reason exception into VM%d.\n",
592 vcpu->vm->id);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000593
Fuad Tabbab86325a2020-01-10 13:38:15 +0000594 inject_el1_exception(vcpu, esr_el1_value);
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000595}
596
Andrew Walbran59182d52019-09-23 17:55:39 +0100597struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100598{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100599 struct ffa_value args = {
Andrew Walbran7f920af2019-09-03 17:09:30 +0100600 .func = vcpu->regs.r[0],
601 .arg1 = vcpu->regs.r[1],
602 .arg2 = vcpu->regs.r[2],
603 .arg3 = vcpu->regs.r[3],
604 .arg4 = vcpu->regs.r[4],
605 .arg5 = vcpu->regs.r[5],
606 .arg6 = vcpu->regs.r[6],
607 .arg7 = vcpu->regs.r[7],
608 };
Andrew Walbran59182d52019-09-23 17:55:39 +0100609 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100610
Andrew Walbran7f920af2019-09-03 17:09:30 +0100611 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
612 &vcpu->regs.r[0], &next)) {
Andrew Walbran59182d52019-09-23 17:55:39 +0100613 return next;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100614 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100615
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100616 if (ffa_handler(&args, &next)) {
Andrew Walbran6f56d7b2019-12-05 16:27:34 +0000617 arch_regs_set_retval(&vcpu->regs, args);
Andrew Walbran59182d52019-09-23 17:55:39 +0100618 update_vi(next);
619 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100620 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100621
Andrew Walbran7f920af2019-09-03 17:09:30 +0100622 switch (args.func) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100623 case HF_VM_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100624 vcpu->regs.r[0] = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100625 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100626
627 case HF_VCPU_GET_COUNT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100628 vcpu->regs.r[0] = api_vcpu_get_count(args.arg1, vcpu);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100629 break;
630
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000631 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100632 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000633 break;
634
635 case HF_MAILBOX_WAITER_GET:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100636 vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100637 break;
638
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000639 case HF_INTERRUPT_ENABLE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100640 vcpu->regs.r[0] =
641 api_interrupt_enable(args.arg1, args.arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000642 break;
643
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000644 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100645 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000646 break;
647
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000648 case HF_INTERRUPT_INJECT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100649 vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2,
650 args.arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000651 break;
652
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100653 case HF_DEBUG_LOG:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100654 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100655 break;
656
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100657 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100658 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100659 }
660
Andrew Walbran59182d52019-09-23 17:55:39 +0100661 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000662
Andrew Walbran59182d52019-09-23 17:55:39 +0100663 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100664}
665
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100666struct vcpu *irq_lower(void)
667{
Andrew Scull9726c252019-01-23 13:44:19 +0000668 /*
669 * Switch back to primary VM, interrupts will be handled there.
670 *
671 * If the VM has aborted, this vCPU will be aborted when the scheduler
672 * tries to run it again. This means the interrupt will not be delayed
673 * by the aborted VM.
674 *
675 * TODO: Only switch when the interrupt isn't for the current VM.
676 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000677 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100678}
679
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000680struct vcpu *fiq_lower(void)
681{
682 return irq_lower();
683}
684
Fuad Tabbad1d67982020-01-08 11:28:29 +0000685noreturn struct vcpu *serr_lower(void)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000686{
Fuad Tabbad1d67982020-01-08 11:28:29 +0000687 /*
688 * SError exceptions should be isolated and handled by the responsible
689 * VM/exception level. Getting here indicates a bug, that isolation is
690 * not working, or a processor that does not support ARMv8.2-IESB, in
691 * which case Hafnium routes SError exceptions to EL2 (here).
692 */
693 panic("SError from a lower exception level.");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000694}
695
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000696/**
697 * Initialises a fault info structure. It assumes that an FnV bit exists at
698 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
699 * the ESR (the fault status code) are 010000; this is the case for both
700 * instruction and data aborts, but not necessarily for other exception reasons.
701 */
702static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100703 const struct vcpu *vcpu,
704 uint32_t mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000705{
706 uint32_t fsc = esr & 0x3f;
707 struct vcpu_fault_info r;
708
709 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000710 r.pc = va_init(vcpu->regs.pc);
711
712 /*
713 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
714 * indicates that we cannot rely on far_el2.
715 */
Andrew Walbrane52006c2019-10-22 18:01:28 +0100716 if (fsc == 0x10 && esr & (1U << 10)) {
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000717 r.vaddr = va_init(0);
718 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
719 } else {
720 r.vaddr = va_init(read_msr(far_el2));
721 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
722 (read_msr(far_el2) & (PAGE_SIZE - 1)));
723 }
724
725 return r;
726}
727
Andrew Scull37402872018-10-24 14:23:06 +0100728struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100729{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100730 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000731 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000732 struct vcpu *new_vcpu;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000733 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100734
Fuad Tabbac76466d2019-09-06 10:42:12 +0100735 switch (ec) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000736 case EC_WFI_WFE:
Andrew Walbran48196eb2019-03-04 14:56:24 +0000737 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100738 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100739 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100740 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000741 /* WFE */
742 /*
743 * TODO: consider giving the scheduler more context,
744 * somehow.
745 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100746 api_yield(vcpu, &new_vcpu);
Jose Marinho135dff32019-02-28 10:25:57 +0000747 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100748 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000749 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000750 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100751
Fuad Tabbab86325a2020-01-10 13:38:15 +0000752 case EC_DATA_ABORT_LOWER_EL:
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000753 info = fault_info_init(
Andrew Walbrane52006c2019-10-22 18:01:28 +0100754 esr, vcpu, (esr & (1U << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000755 if (vcpu_handle_page_fault(vcpu, &info)) {
756 return NULL;
757 }
Fuad Tabbab86325a2020-01-10 13:38:15 +0000758 /* Inform the EL1 of the data abort. */
759 inject_el1_data_abort_exception(vcpu, esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100760
Fuad Tabbab86325a2020-01-10 13:38:15 +0000761 /* Schedule the same VM to continue running. */
762 return NULL;
763
764 case EC_INSTRUCTION_ABORT_LOWER_EL:
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100765 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000766 if (vcpu_handle_page_fault(vcpu, &info)) {
767 return NULL;
768 }
Fuad Tabbab86325a2020-01-10 13:38:15 +0000769 /* Inform the EL1 of the instruction abort. */
770 inject_el1_instruction_abort_exception(vcpu, esr);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100771
Fuad Tabbab86325a2020-01-10 13:38:15 +0000772 /* Schedule the same VM to continue running. */
773 return NULL;
774
775 case EC_HVC:
Andrew Walbran59182d52019-09-23 17:55:39 +0100776 return hvc_handler(vcpu);
777
Fuad Tabbab86325a2020-01-10 13:38:15 +0000778 case EC_SMC: {
Andrew Scullc960c032018-10-24 15:13:35 +0100779 uintreg_t smc_pc = vcpu->regs.pc;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000780 struct vcpu *next = smc_handler(vcpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100781
782 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100783 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000784
Andrew Walbran33645652019-04-15 12:29:31 +0100785 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100786 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100787
Fuad Tabbab86325a2020-01-10 13:38:15 +0000788 case EC_MSR:
Fuad Tabbac76466d2019-09-06 10:42:12 +0100789 /*
790 * NOTE: This should never be reached because it goes through a
791 * separate path handled by handle_system_register_access().
792 */
793 panic("Handled by handle_system_register_access().");
794
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100795 default:
Andrew Walbran17eebf92020-02-05 16:35:49 +0000796 dlog_notice(
797 "Unknown lower sync exception pc=%#x, esr=%#x, "
798 "ec=%#x\n",
799 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000800 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100801 }
802
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000803 /*
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000804 * The exception wasn't handled. Inject to the VM to give it chance to
805 * handle as an unknown exception.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000806 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000807 inject_el1_unknown_exception(vcpu, esr);
808
809 /* Schedule the same VM to continue running. */
810 return NULL;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000811}
812
Fuad Tabbac76466d2019-09-06 10:42:12 +0100813/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000814 * Handles EC = 011000, MSR, MRS instruction traps.
Fuad Tabbaed294af2019-12-20 10:43:01 +0000815 * Returns non-null ONLY if the access failed and the vCPU is changing.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100816 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000817void handle_system_register_access(uintreg_t esr_el2)
Fuad Tabbac76466d2019-09-06 10:42:12 +0100818{
819 struct vcpu *vcpu = current();
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100820 ffa_vm_id_t vm_id = vcpu->vm->id;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000821 uintreg_t ec = GET_ESR_EC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100822
Fuad Tabbab86325a2020-01-10 13:38:15 +0000823 CHECK(ec == EC_MSR);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100824 /*
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100825 * Handle accesses to debug and performance monitor registers.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000826 * Inject an exception for unhandled/unsupported registers.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100827 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000828 if (debug_el1_is_register_access(esr_el2)) {
829 if (!debug_el1_process_access(vcpu, vm_id, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000830 inject_el1_unknown_exception(vcpu, esr_el2);
831 return;
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100832 }
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000833 } else if (perfmon_is_register_access(esr_el2)) {
834 if (!perfmon_process_access(vcpu, vm_id, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000835 inject_el1_unknown_exception(vcpu, esr_el2);
836 return;
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100837 }
Fuad Tabba77a4b012019-11-15 12:13:08 +0000838 } else if (feature_id_is_register_access(esr_el2)) {
839 if (!feature_id_process_access(vcpu, esr_el2)) {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000840 inject_el1_unknown_exception(vcpu, esr_el2);
841 return;
Fuad Tabba77a4b012019-11-15 12:13:08 +0000842 }
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100843 } else {
Fuad Tabbab86325a2020-01-10 13:38:15 +0000844 inject_el1_unknown_exception(vcpu, esr_el2);
845 return;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100846 }
847
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100848 /* Instruction was fulfilled. Skip it and run the next one. */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000849 vcpu->regs.pc += GET_NEXT_PC_INC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100850}