blob: 2406768b47a4acccd52cf8b7c8ee7bf310588a20 [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 Scullc960c032018-10-24 15:13:35 +010022
Andrew Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/api.h"
Fuad Tabbac76466d2019-09-06 10:42:12 +010024#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/cpu.h"
26#include "hf/dlog.h"
Andrew Sculla9c172d2019-04-03 14:10:00 +010027#include "hf/panic.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000028#include "hf/spci.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010029#include "hf/vm.h"
30
Andrew Scullf35a5c92018-08-07 18:09:46 +010031#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010032
Fuad Tabbac76466d2019-09-06 10:42:12 +010033#include "debug_el1.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010034#include "msr.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010035#include "psci.h"
Andrew Walbran33645652019-04-15 12:29:31 +010036#include "psci_handler.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000037#include "smc.h"
Fuad Tabbaba8c44d2019-09-23 14:38:58 +010038#include "sysregs.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010039
Andrew Walbran3d84a262018-12-13 14:41:19 +000040#define HCR_EL2_VI (1u << 7)
41
Fuad Tabbac76466d2019-09-06 10:42:12 +010042/**
43 * Gets the Exception Class from the ESR.
44 */
45#define GET_EC(esr) ((esr) >> 26)
46
47/**
48 * Gets the value to increment for the next PC.
49 * The ESR encodes whether the instruction is 2 bytes or 4 bytes long.
50 */
51#define GET_NEXT_PC_INC(esr) (((esr) & (1u << 25)) ? 4 : 2)
52
Fuad Tabbac76466d2019-09-06 10:42:12 +010053/**
54 * Returns a reference to the currently executing vCPU.
55 */
Andrew Scullc960c032018-10-24 15:13:35 +010056static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000057{
58 return (struct vcpu *)read_msr(tpidr_el2);
59}
60
Andrew Walbran1f8d4872018-12-20 11:21:32 +000061/**
62 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
63 * informs the arch-independent sections that registers have been saved.
64 */
65void complete_saving_state(struct vcpu *vcpu)
66{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010067 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
68 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000069
70 api_regs_state_saved(vcpu);
71
72 /*
73 * If switching away from the primary, copy the current EL0 virtual
74 * timer registers to the corresponding EL2 physical timer registers.
75 * This is used to emulate the virtual timer for the primary in case it
76 * should fire while the secondary is running.
77 */
78 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
79 /*
80 * Clear timer control register before copying compare value, to
81 * avoid a spurious timer interrupt. This could be a problem if
82 * the interrupt is configured as edge-triggered, as it would
83 * then be latched in.
84 */
85 write_msr(cnthp_ctl_el2, 0);
86 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
87 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
88 }
89}
90
91/**
92 * Restores the state of per-vCPU peripherals, such as the virtual timer.
93 */
94void begin_restoring_state(struct vcpu *vcpu)
95{
96 /*
97 * Clear timer control register before restoring compare value, to avoid
98 * a spurious timer interrupt. This could be a problem if the interrupt
99 * is configured as edge-triggered, as it would then be latched in.
100 */
101 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100102 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
103 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000104
105 /*
106 * If we are switching (back) to the primary, disable the EL2 physical
107 * timer which was being used to emulate the EL0 virtual timer, as the
108 * virtual timer is now running for the primary again.
109 */
110 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
111 write_msr(cnthp_ctl_el2, 0);
112 write_msr(cnthp_cval_el2, 0);
113 }
114}
115
Andrew Walbran1f32e722019-06-07 17:57:26 +0100116/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100117 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
118 * current VMID.
119 */
120static void invalidate_vm_tlb(void)
121{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100122 /*
123 * Ensure that the last VTTBR write has taken effect so we invalidate
124 * the right set of TLB entries.
125 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100126 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100127
Andrew Walbran1f32e722019-06-07 17:57:26 +0100128 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100129
130 /*
131 * Ensure that no instructions are fetched for the VM until after the
132 * TLB invalidation has taken effect.
133 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100134 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100135
136 /*
137 * Ensure that no data reads or writes for the VM happen until after the
138 * TLB invalidation has taken effect. Non-sharable is enough because the
139 * TLB is local to the CPU.
140 */
David Brazdil851948e2019-08-09 12:02:12 +0100141 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100142}
143
144/**
145 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
146 * the same VM which was run on the current pCPU.
147 *
148 * This is necessary because VMs may (contrary to the architecture
149 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
150 * workaround:
151 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
152 */
153void maybe_invalidate_tlb(struct vcpu *vcpu)
154{
155 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100156 spci_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100157
158 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
159 new_vcpu_index) {
160 /*
161 * The vCPU has changed since the last time this VM was run on
162 * this pCPU, so we need to invalidate the TLB.
163 */
164 invalidate_vm_tlb();
165
166 /* Record the fact that this vCPU is now running on this CPU. */
167 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
168 new_vcpu_index;
169 }
170}
171
Andrew Scullc960c032018-10-24 15:13:35 +0100172noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100173{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000174 (void)elr;
175 (void)spsr;
176
Andrew Sculla9c172d2019-04-03 14:10:00 +0100177 panic("IRQ from current");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100178}
179
Andrew Scullc960c032018-10-24 15:13:35 +0100180noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100181{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000182 (void)elr;
183 (void)spsr;
184
Andrew Sculla9c172d2019-04-03 14:10:00 +0100185 panic("FIQ from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000186}
187
Andrew Scullc960c032018-10-24 15:13:35 +0100188noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000189{
190 (void)elr;
191 (void)spsr;
192
Andrew Sculla9c172d2019-04-03 14:10:00 +0100193 panic("SERR from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000194}
195
Andrew Scullc960c032018-10-24 15:13:35 +0100196noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000197{
198 uintreg_t esr = read_msr(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100199 uintreg_t ec = GET_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000200
201 (void)spsr;
202
Fuad Tabbac76466d2019-09-06 10:42:12 +0100203 switch (ec) {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100204 case 0x25: /* EC = 100101, Data abort. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100205 dlog("Data abort: pc=%#x, esr=%#x, ec=%#x", elr, esr, ec);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100206 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100207 dlog(", far=%#x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100208 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100209 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100210 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100211
212 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000213 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100214
215 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100216 dlog("Unknown current sync exception pc=%#x, esr=%#x, "
217 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100218 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100219 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100220 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000221
Andrew Sculla9c172d2019-04-03 14:10:00 +0100222 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100223}
224
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100225/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000226 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
227 * arch_regs.
228 */
229static void set_virtual_interrupt(struct arch_regs *r, bool enable)
230{
231 if (enable) {
232 r->lazy.hcr_el2 |= HCR_EL2_VI;
233 } else {
234 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
235 }
236}
237
238/**
239 * Sets or clears the VI bit in the HCR_EL2 register.
240 */
241static void set_virtual_interrupt_current(bool enable)
242{
243 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000244
Andrew Walbran3d84a262018-12-13 14:41:19 +0000245 if (enable) {
246 hcr_el2 |= HCR_EL2_VI;
247 } else {
248 hcr_el2 &= ~HCR_EL2_VI;
249 }
250 write_msr(hcr_el2, hcr_el2);
251}
252
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100253static bool smc_check_client_privileges(const struct vcpu *vcpu)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100254{
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100255 (void)vcpu; /*UNUSED*/
256
257 /*
258 * TODO(b/132421503): Check for privileges based on manifest.
259 * Currently returns false, which maintains existing behavior.
260 */
261
262 return false;
263}
264
265/**
266 * Applies SMC access control according to manifest.
267 * Forwards the call if access is granted.
268 * Returns true if call is forwarded.
269 */
270static bool smc_forwarder(const struct vcpu *vcpu, smc_res_t *ret)
271{
272 uint32_t func = vcpu->regs.r[0];
273 /* TODO(b/132421503): obtain vmid according to new scheme. */
274 uint32_t client_id = vcpu->vm->id;
275
276 if (smc_check_client_privileges(vcpu)) {
Andrew Scull52b8ea12019-08-30 19:16:09 +0100277 *ret = smc_forward(func, vcpu->regs.r[1], vcpu->regs.r[2],
278 vcpu->regs.r[3], vcpu->regs.r[4],
279 vcpu->regs.r[5], vcpu->regs.r[6], client_id);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100280 return true;
281 }
282
283 return false;
284}
285
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100286static bool spci_handler(uintreg_t func, uintreg_t arg1, uintreg_t arg2,
287 uintreg_t arg3, uintreg_t *ret, struct vcpu **next)
288{
289 (void)arg2;
290 (void)arg3;
291
292 switch (func & ~SMCCC_CONVENTION_MASK) {
293 case SPCI_VERSION_32:
294 *ret = api_spci_version();
295 return true;
296 case SPCI_YIELD_32:
297 *ret = api_spci_yield(current(), next);
298 return true;
299 case SPCI_MSG_SEND_32:
300 *ret = api_spci_msg_send(arg1, current(), next);
301 return true;
Andrew Walbran0de4f162019-09-03 16:44:20 +0100302 case SPCI_MSG_WAIT_32:
303 *ret = api_spci_msg_recv(true, current(), next);
304 return true;
305 case SPCI_MSG_POLL_32:
306 *ret = api_spci_msg_recv(false, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100307 return true;
308 }
309
310 return false;
311}
312
313/**
314 * Set or clear VI bit according to pending interrupts.
315 */
316static void update_vi(struct vcpu *next)
317{
318 if (next == NULL) {
319 /*
320 * Not switching vCPUs, set the bit for the current vCPU
321 * directly in the register.
322 */
323 struct vcpu *vcpu = current();
324
325 sl_lock(&vcpu->lock);
326 set_virtual_interrupt_current(
327 vcpu->interrupts.enabled_and_pending_count > 0);
328 sl_unlock(&vcpu->lock);
329 } else {
330 /*
331 * About to switch vCPUs, set the bit for the vCPU to which we
332 * are switching in the saved copy of the register.
333 */
334 sl_lock(&next->lock);
335 set_virtual_interrupt(
336 &next->regs,
337 next->interrupts.enabled_and_pending_count > 0);
338 sl_unlock(&next->lock);
339 }
340}
341
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100342/**
343 * Processes SMC instruction calls.
344 */
345static bool smc_handler(struct vcpu *vcpu, smc_res_t *ret, struct vcpu **next)
346{
347 uint32_t func = vcpu->regs.r[0];
348
349 if (psci_handler(vcpu, func, vcpu->regs.r[1], vcpu->regs.r[2],
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100350 vcpu->regs.r[3], &ret->res0, next)) {
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100351 /* SMC PSCI calls are processed by the PSCI handler. */
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100352 return true;
353 }
354
355 switch (func & ~SMCCC_CONVENTION_MASK) {
356 case HF_DEBUG_LOG:
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100357 api_debug_log(vcpu->regs.r[1], vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100358 return true;
359 }
360
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100361 /* Remaining SMC calls need to be forwarded. */
362 return smc_forwarder(vcpu, ret);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100363}
364
Andrew Walbran59182d52019-09-23 17:55:39 +0100365struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100366{
Andrew Walbran59182d52019-09-23 17:55:39 +0100367 uint32_t func = vcpu->regs.r[0];
368 uintreg_t arg1 = vcpu->regs.r[1];
369 uintreg_t arg2 = vcpu->regs.r[2];
370 uintreg_t arg3 = vcpu->regs.r[3];
371 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100372
Andrew Walbran59182d52019-09-23 17:55:39 +0100373 if (psci_handler(vcpu, func, arg1, arg2, arg3, &vcpu->regs.r[0],
374 &next)) {
375 return next;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100376 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100377
Andrew Walbran59182d52019-09-23 17:55:39 +0100378 if (spci_handler(func, arg1, arg2, arg3, &vcpu->regs.r[0], &next)) {
379 update_vi(next);
380 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100381 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100382
Andrew Walbran59182d52019-09-23 17:55:39 +0100383 switch (func) {
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000384 case HF_VM_GET_ID:
Andrew Walbran59182d52019-09-23 17:55:39 +0100385 vcpu->regs.r[0] = api_vm_get_id(vcpu);
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000386 break;
387
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100388 case HF_VM_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100389 vcpu->regs.r[0] = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100390 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100391
392 case HF_VCPU_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100393 vcpu->regs.r[0] = api_vcpu_get_count(arg1, vcpu);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100394 break;
395
396 case HF_VCPU_RUN:
Andrew Walbran59182d52019-09-23 17:55:39 +0100397 vcpu->regs.r[0] = hf_vcpu_run_return_encode(
398 api_vcpu_run(arg1, arg2, vcpu, &next));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100399 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100400
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100401 case HF_VM_CONFIGURE:
Andrew Walbran59182d52019-09-23 17:55:39 +0100402 vcpu->regs.r[0] = api_vm_configure(ipa_init(arg1),
403 ipa_init(arg2), vcpu, &next);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100404 break;
405
Andrew Scullaa039b32018-10-04 15:02:26 +0100406 case HF_MAILBOX_CLEAR:
Andrew Walbran59182d52019-09-23 17:55:39 +0100407 vcpu->regs.r[0] = api_mailbox_clear(vcpu, &next);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000408 break;
409
410 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100411 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000412 break;
413
414 case HF_MAILBOX_WAITER_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100415 vcpu->regs.r[0] = api_mailbox_waiter_get(arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100416 break;
417
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000418 case HF_INTERRUPT_ENABLE:
Andrew Walbran59182d52019-09-23 17:55:39 +0100419 vcpu->regs.r[0] = api_interrupt_enable(arg1, arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000420 break;
421
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000422 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100423 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000424 break;
425
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000426 case HF_INTERRUPT_INJECT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100427 vcpu->regs.r[0] =
428 api_interrupt_inject(arg1, arg2, arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000429 break;
430
Andrew Scull6386f252018-12-06 13:29:10 +0000431 case HF_SHARE_MEMORY:
Andrew Walbran59182d52019-09-23 17:55:39 +0100432 vcpu->regs.r[0] =
Andrew Scull6386f252018-12-06 13:29:10 +0000433 api_share_memory(arg1 >> 32, ipa_init(arg2), arg3,
Andrew Walbran59182d52019-09-23 17:55:39 +0100434 arg1 & 0xffffffff, vcpu);
Andrew Scull6386f252018-12-06 13:29:10 +0000435 break;
436
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100437 case HF_DEBUG_LOG:
Andrew Walbran59182d52019-09-23 17:55:39 +0100438 vcpu->regs.r[0] = api_debug_log(arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100439 break;
440
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100441 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100442 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100443 }
444
Andrew Walbran59182d52019-09-23 17:55:39 +0100445 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000446
Andrew Walbran59182d52019-09-23 17:55:39 +0100447 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100448}
449
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100450struct vcpu *irq_lower(void)
451{
Andrew Scull9726c252019-01-23 13:44:19 +0000452 /*
453 * Switch back to primary VM, interrupts will be handled there.
454 *
455 * If the VM has aborted, this vCPU will be aborted when the scheduler
456 * tries to run it again. This means the interrupt will not be delayed
457 * by the aborted VM.
458 *
459 * TODO: Only switch when the interrupt isn't for the current VM.
460 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000461 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100462}
463
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000464struct vcpu *fiq_lower(void)
465{
466 return irq_lower();
467}
468
469struct vcpu *serr_lower(void)
470{
471 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000472 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000473}
474
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000475/**
476 * Initialises a fault info structure. It assumes that an FnV bit exists at
477 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
478 * the ESR (the fault status code) are 010000; this is the case for both
479 * instruction and data aborts, but not necessarily for other exception reasons.
480 */
481static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100482 const struct vcpu *vcpu, int mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000483{
484 uint32_t fsc = esr & 0x3f;
485 struct vcpu_fault_info r;
486
487 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000488 r.pc = va_init(vcpu->regs.pc);
489
490 /*
491 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
492 * indicates that we cannot rely on far_el2.
493 */
494 if (fsc == 0x10 && esr & (1u << 10)) {
495 r.vaddr = va_init(0);
496 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
497 } else {
498 r.vaddr = va_init(read_msr(far_el2));
499 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
500 (read_msr(far_el2) & (PAGE_SIZE - 1)));
501 }
502
503 return r;
504}
505
Andrew Scull37402872018-10-24 14:23:06 +0100506struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100507{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100508 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000509 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000510 struct vcpu *new_vcpu;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100511 uintreg_t ec = GET_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100512
Fuad Tabbac76466d2019-09-06 10:42:12 +0100513 switch (ec) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100514 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000515 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100516 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100517 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100518 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000519 /* WFE */
520 /*
521 * TODO: consider giving the scheduler more context,
522 * somehow.
523 */
Jose Marinho135dff32019-02-28 10:25:57 +0000524 api_spci_yield(vcpu, &new_vcpu);
525 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100526 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000527 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000528 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100529
530 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000531 info = fault_info_init(
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100532 esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000533 if (vcpu_handle_page_fault(vcpu, &info)) {
534 return NULL;
535 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000536 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100537
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100538 case 0x20: /* EC = 100000, Instruction abort. */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100539 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000540 if (vcpu_handle_page_fault(vcpu, &info)) {
541 return NULL;
542 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000543 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100544
Andrew Walbran59182d52019-09-23 17:55:39 +0100545 case 0x16: /* EC = 010110, HVC instruction */
546 return hvc_handler(vcpu);
547
Andrew Scullc960c032018-10-24 15:13:35 +0100548 case 0x17: /* EC = 010111, SMC instruction. */ {
549 uintreg_t smc_pc = vcpu->regs.pc;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100550 smc_res_t ret;
Andrew Walbran33645652019-04-15 12:29:31 +0100551 struct vcpu *next = NULL;
Andrew Scullc960c032018-10-24 15:13:35 +0100552
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100553 if (!smc_handler(vcpu, &ret, &next)) {
554 /* TODO(b/132421503): handle SMC forward rejection */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100555 dlog("Unsupported SMC call: %#x\n", vcpu->regs.r[0]);
Andrew Walbran59182d52019-09-23 17:55:39 +0100556 ret.res0 = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100557 }
558
559 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100560 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100561 vcpu->regs.r[0] = ret.res0;
562 vcpu->regs.r[1] = ret.res1;
563 vcpu->regs.r[2] = ret.res2;
564 vcpu->regs.r[3] = ret.res3;
Andrew Walbran33645652019-04-15 12:29:31 +0100565 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100566 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100567
Fuad Tabbac76466d2019-09-06 10:42:12 +0100568 /*
569 * EC = 011000, MSR, MRS or System instruction execution that is not
570 * reported using EC 000000, 000001 or 000111.
571 */
572 case 0x18:
573 /*
574 * NOTE: This should never be reached because it goes through a
575 * separate path handled by handle_system_register_access().
576 */
577 panic("Handled by handle_system_register_access().");
578
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100579 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100580 dlog("Unknown lower sync exception pc=%#x, esr=%#x, "
581 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100582 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000583 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100584 }
585
Andrew Scull9726c252019-01-23 13:44:19 +0000586 /* The exception wasn't handled so abort the VM. */
587 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100588}
Fuad Tabbac76466d2019-09-06 10:42:12 +0100589
590/**
591 * Handles EC = 011000, msr, mrs instruction traps.
592 * Returns non-null ONLY if the access failed and the vcpu is changing.
593 */
594struct vcpu *handle_system_register_access(uintreg_t esr)
595{
596 struct vcpu *vcpu = current();
597 spci_vm_id_t vm_id = vcpu->vm->id;
598 uintreg_t ec = GET_EC(esr);
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100599 char *direction_str;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100600
601 CHECK(ec == 0x18);
602
603 /*
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100604 * Handle accesses to debug registers.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100605 * Abort when encountering unhandled register accesses.
606 */
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100607 if (is_debug_el1_register_access(esr) &&
608 debug_el1_process_access(vcpu, vm_id, esr)) {
609 /* Instruction was fulfilled. Skip it and run the next one. */
610 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
611 return NULL;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100612 }
613
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100614 direction_str = ISS_IS_READ(esr) ? "read" : "write";
Fuad Tabbac76466d2019-09-06 10:42:12 +0100615
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100616 dlog("Unhandled system register %s: op0=%d, op1=%d, crn=%d, "
617 "crm=%d, op2=%d, rt=%d.\n",
618 direction_str, GET_ISS_OP0(esr), GET_ISS_OP1(esr),
619 GET_ISS_CRN(esr), GET_ISS_CRM(esr), GET_ISS_OP2(esr),
620 GET_ISS_RT(esr));
621
622 /* Abort if unable to fulfill the register access. */
623 return api_abort(vcpu);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100624}