blob: 107f02bf2b9d899e77a3be2d4244eb6ba21f27ce [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"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010038
Andrew Walbran3d84a262018-12-13 14:41:19 +000039#define HCR_EL2_VI (1u << 7)
40
Fuad Tabbac76466d2019-09-06 10:42:12 +010041/**
42 * Gets the Exception Class from the ESR.
43 */
44#define GET_EC(esr) ((esr) >> 26)
45
46/**
47 * Gets the value to increment for the next PC.
48 * The ESR encodes whether the instruction is 2 bytes or 4 bytes long.
49 */
50#define GET_NEXT_PC_INC(esr) (((esr) & (1u << 25)) ? 4 : 2)
51
Fuad Tabbac76466d2019-09-06 10:42:12 +010052/**
53 * Returns a reference to the currently executing vCPU.
54 */
Andrew Scullc960c032018-10-24 15:13:35 +010055static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000056{
57 return (struct vcpu *)read_msr(tpidr_el2);
58}
59
Andrew Walbran1f8d4872018-12-20 11:21:32 +000060/**
61 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
62 * informs the arch-independent sections that registers have been saved.
63 */
64void complete_saving_state(struct vcpu *vcpu)
65{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010066 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
67 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000068
69 api_regs_state_saved(vcpu);
70
71 /*
72 * If switching away from the primary, copy the current EL0 virtual
73 * timer registers to the corresponding EL2 physical timer registers.
74 * This is used to emulate the virtual timer for the primary in case it
75 * should fire while the secondary is running.
76 */
77 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
78 /*
79 * Clear timer control register before copying compare value, to
80 * avoid a spurious timer interrupt. This could be a problem if
81 * the interrupt is configured as edge-triggered, as it would
82 * then be latched in.
83 */
84 write_msr(cnthp_ctl_el2, 0);
85 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
86 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
87 }
88}
89
90/**
91 * Restores the state of per-vCPU peripherals, such as the virtual timer.
92 */
93void begin_restoring_state(struct vcpu *vcpu)
94{
95 /*
96 * Clear timer control register before restoring compare value, to avoid
97 * a spurious timer interrupt. This could be a problem if the interrupt
98 * is configured as edge-triggered, as it would then be latched in.
99 */
100 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100101 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
102 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000103
104 /*
105 * If we are switching (back) to the primary, disable the EL2 physical
106 * timer which was being used to emulate the EL0 virtual timer, as the
107 * virtual timer is now running for the primary again.
108 */
109 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
110 write_msr(cnthp_ctl_el2, 0);
111 write_msr(cnthp_cval_el2, 0);
112 }
113}
114
Andrew Walbran1f32e722019-06-07 17:57:26 +0100115/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100116 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
117 * current VMID.
118 */
119static void invalidate_vm_tlb(void)
120{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100121 /*
122 * Ensure that the last VTTBR write has taken effect so we invalidate
123 * the right set of TLB entries.
124 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100125 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100126
Andrew Walbran1f32e722019-06-07 17:57:26 +0100127 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100128
129 /*
130 * Ensure that no instructions are fetched for the VM until after the
131 * TLB invalidation has taken effect.
132 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100133 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100134
135 /*
136 * Ensure that no data reads or writes for the VM happen until after the
137 * TLB invalidation has taken effect. Non-sharable is enough because the
138 * TLB is local to the CPU.
139 */
David Brazdil851948e2019-08-09 12:02:12 +0100140 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100141}
142
143/**
144 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
145 * the same VM which was run on the current pCPU.
146 *
147 * This is necessary because VMs may (contrary to the architecture
148 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
149 * workaround:
150 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
151 */
152void maybe_invalidate_tlb(struct vcpu *vcpu)
153{
154 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100155 spci_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100156
157 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
158 new_vcpu_index) {
159 /*
160 * The vCPU has changed since the last time this VM was run on
161 * this pCPU, so we need to invalidate the TLB.
162 */
163 invalidate_vm_tlb();
164
165 /* Record the fact that this vCPU is now running on this CPU. */
166 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
167 new_vcpu_index;
168 }
169}
170
Andrew Scullc960c032018-10-24 15:13:35 +0100171noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100172{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000173 (void)elr;
174 (void)spsr;
175
Andrew Sculla9c172d2019-04-03 14:10:00 +0100176 panic("IRQ from current");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100177}
178
Andrew Scullc960c032018-10-24 15:13:35 +0100179noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100180{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000181 (void)elr;
182 (void)spsr;
183
Andrew Sculla9c172d2019-04-03 14:10:00 +0100184 panic("FIQ from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000185}
186
Andrew Scullc960c032018-10-24 15:13:35 +0100187noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000188{
189 (void)elr;
190 (void)spsr;
191
Andrew Sculla9c172d2019-04-03 14:10:00 +0100192 panic("SERR from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000193}
194
Andrew Scullc960c032018-10-24 15:13:35 +0100195noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000196{
197 uintreg_t esr = read_msr(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100198 uintreg_t ec = GET_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000199
200 (void)spsr;
201
Fuad Tabbac76466d2019-09-06 10:42:12 +0100202 switch (ec) {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100203 case 0x25: /* EC = 100101, Data abort. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100204 dlog("Data abort: pc=%#x, esr=%#x, ec=%#x", elr, esr, ec);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100205 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100206 dlog(", far=%#x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100207 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100208 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100209 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100210
211 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000212 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100213
214 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100215 dlog("Unknown current sync exception pc=%#x, esr=%#x, "
216 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100217 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100218 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100219 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000220
Andrew Sculla9c172d2019-04-03 14:10:00 +0100221 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100222}
223
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100224/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000225 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
226 * arch_regs.
227 */
228static void set_virtual_interrupt(struct arch_regs *r, bool enable)
229{
230 if (enable) {
231 r->lazy.hcr_el2 |= HCR_EL2_VI;
232 } else {
233 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
234 }
235}
236
237/**
238 * Sets or clears the VI bit in the HCR_EL2 register.
239 */
240static void set_virtual_interrupt_current(bool enable)
241{
242 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000243
Andrew Walbran3d84a262018-12-13 14:41:19 +0000244 if (enable) {
245 hcr_el2 |= HCR_EL2_VI;
246 } else {
247 hcr_el2 &= ~HCR_EL2_VI;
248 }
249 write_msr(hcr_el2, hcr_el2);
250}
251
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100252static bool smc_check_client_privileges(const struct vcpu *vcpu)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100253{
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100254 (void)vcpu; /*UNUSED*/
255
256 /*
257 * TODO(b/132421503): Check for privileges based on manifest.
258 * Currently returns false, which maintains existing behavior.
259 */
260
261 return false;
262}
263
264/**
265 * Applies SMC access control according to manifest.
266 * Forwards the call if access is granted.
267 * Returns true if call is forwarded.
268 */
269static bool smc_forwarder(const struct vcpu *vcpu, smc_res_t *ret)
270{
271 uint32_t func = vcpu->regs.r[0];
272 /* TODO(b/132421503): obtain vmid according to new scheme. */
273 uint32_t client_id = vcpu->vm->id;
274
275 if (smc_check_client_privileges(vcpu)) {
Andrew Scull52b8ea12019-08-30 19:16:09 +0100276 *ret = smc_forward(func, vcpu->regs.r[1], vcpu->regs.r[2],
277 vcpu->regs.r[3], vcpu->regs.r[4],
278 vcpu->regs.r[5], vcpu->regs.r[6], client_id);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100279 return true;
280 }
281
282 return false;
283}
284
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100285static bool spci_handler(uintreg_t func, uintreg_t arg1, uintreg_t arg2,
286 uintreg_t arg3, uintreg_t *ret, struct vcpu **next)
287{
288 (void)arg2;
289 (void)arg3;
290
291 switch (func & ~SMCCC_CONVENTION_MASK) {
292 case SPCI_VERSION_32:
293 *ret = api_spci_version();
294 return true;
295 case SPCI_YIELD_32:
296 *ret = api_spci_yield(current(), next);
297 return true;
298 case SPCI_MSG_SEND_32:
299 *ret = api_spci_msg_send(arg1, current(), next);
300 return true;
Andrew Walbran0de4f162019-09-03 16:44:20 +0100301 case SPCI_MSG_WAIT_32:
302 *ret = api_spci_msg_recv(true, current(), next);
303 return true;
304 case SPCI_MSG_POLL_32:
305 *ret = api_spci_msg_recv(false, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100306 return true;
307 }
308
309 return false;
310}
311
312/**
313 * Set or clear VI bit according to pending interrupts.
314 */
315static void update_vi(struct vcpu *next)
316{
317 if (next == NULL) {
318 /*
319 * Not switching vCPUs, set the bit for the current vCPU
320 * directly in the register.
321 */
322 struct vcpu *vcpu = current();
323
324 sl_lock(&vcpu->lock);
325 set_virtual_interrupt_current(
326 vcpu->interrupts.enabled_and_pending_count > 0);
327 sl_unlock(&vcpu->lock);
328 } else {
329 /*
330 * About to switch vCPUs, set the bit for the vCPU to which we
331 * are switching in the saved copy of the register.
332 */
333 sl_lock(&next->lock);
334 set_virtual_interrupt(
335 &next->regs,
336 next->interrupts.enabled_and_pending_count > 0);
337 sl_unlock(&next->lock);
338 }
339}
340
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100341/**
342 * Processes SMC instruction calls.
343 */
344static bool smc_handler(struct vcpu *vcpu, smc_res_t *ret, struct vcpu **next)
345{
346 uint32_t func = vcpu->regs.r[0];
347
348 if (psci_handler(vcpu, func, vcpu->regs.r[1], vcpu->regs.r[2],
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100349 vcpu->regs.r[3], &ret->res0, next)) {
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100350 /* SMC PSCI calls are processed by the PSCI handler. */
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100351 return true;
352 }
353
354 switch (func & ~SMCCC_CONVENTION_MASK) {
355 case HF_DEBUG_LOG:
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100356 api_debug_log(vcpu->regs.r[1], vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100357 return true;
358 }
359
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100360 /* Remaining SMC calls need to be forwarded. */
361 return smc_forwarder(vcpu, ret);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100362}
363
Andrew Walbran59182d52019-09-23 17:55:39 +0100364struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100365{
Andrew Walbran59182d52019-09-23 17:55:39 +0100366 uint32_t func = vcpu->regs.r[0];
367 uintreg_t arg1 = vcpu->regs.r[1];
368 uintreg_t arg2 = vcpu->regs.r[2];
369 uintreg_t arg3 = vcpu->regs.r[3];
370 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100371
Andrew Walbran59182d52019-09-23 17:55:39 +0100372 if (psci_handler(vcpu, func, arg1, arg2, arg3, &vcpu->regs.r[0],
373 &next)) {
374 return next;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100375 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100376
Andrew Walbran59182d52019-09-23 17:55:39 +0100377 if (spci_handler(func, arg1, arg2, arg3, &vcpu->regs.r[0], &next)) {
378 update_vi(next);
379 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100380 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100381
Andrew Walbran59182d52019-09-23 17:55:39 +0100382 switch (func) {
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000383 case HF_VM_GET_ID:
Andrew Walbran59182d52019-09-23 17:55:39 +0100384 vcpu->regs.r[0] = api_vm_get_id(vcpu);
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000385 break;
386
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100387 case HF_VM_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100388 vcpu->regs.r[0] = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100389 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100390
391 case HF_VCPU_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100392 vcpu->regs.r[0] = api_vcpu_get_count(arg1, vcpu);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100393 break;
394
395 case HF_VCPU_RUN:
Andrew Walbran59182d52019-09-23 17:55:39 +0100396 vcpu->regs.r[0] = hf_vcpu_run_return_encode(
397 api_vcpu_run(arg1, arg2, vcpu, &next));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100398 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100399
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100400 case HF_VM_CONFIGURE:
Andrew Walbran59182d52019-09-23 17:55:39 +0100401 vcpu->regs.r[0] = api_vm_configure(ipa_init(arg1),
402 ipa_init(arg2), vcpu, &next);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100403 break;
404
Andrew Scullaa039b32018-10-04 15:02:26 +0100405 case HF_MAILBOX_CLEAR:
Andrew Walbran59182d52019-09-23 17:55:39 +0100406 vcpu->regs.r[0] = api_mailbox_clear(vcpu, &next);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000407 break;
408
409 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100410 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000411 break;
412
413 case HF_MAILBOX_WAITER_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100414 vcpu->regs.r[0] = api_mailbox_waiter_get(arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100415 break;
416
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000417 case HF_INTERRUPT_ENABLE:
Andrew Walbran59182d52019-09-23 17:55:39 +0100418 vcpu->regs.r[0] = api_interrupt_enable(arg1, arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000419 break;
420
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000421 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100422 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000423 break;
424
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000425 case HF_INTERRUPT_INJECT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100426 vcpu->regs.r[0] =
427 api_interrupt_inject(arg1, arg2, arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000428 break;
429
Andrew Scull6386f252018-12-06 13:29:10 +0000430 case HF_SHARE_MEMORY:
Andrew Walbran59182d52019-09-23 17:55:39 +0100431 vcpu->regs.r[0] =
Andrew Scull6386f252018-12-06 13:29:10 +0000432 api_share_memory(arg1 >> 32, ipa_init(arg2), arg3,
Andrew Walbran59182d52019-09-23 17:55:39 +0100433 arg1 & 0xffffffff, vcpu);
Andrew Scull6386f252018-12-06 13:29:10 +0000434 break;
435
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100436 case HF_DEBUG_LOG:
Andrew Walbran59182d52019-09-23 17:55:39 +0100437 vcpu->regs.r[0] = api_debug_log(arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100438 break;
439
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100440 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100441 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100442 }
443
Andrew Walbran59182d52019-09-23 17:55:39 +0100444 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000445
Andrew Walbran59182d52019-09-23 17:55:39 +0100446 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100447}
448
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100449struct vcpu *irq_lower(void)
450{
Andrew Scull9726c252019-01-23 13:44:19 +0000451 /*
452 * Switch back to primary VM, interrupts will be handled there.
453 *
454 * If the VM has aborted, this vCPU will be aborted when the scheduler
455 * tries to run it again. This means the interrupt will not be delayed
456 * by the aborted VM.
457 *
458 * TODO: Only switch when the interrupt isn't for the current VM.
459 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000460 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100461}
462
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000463struct vcpu *fiq_lower(void)
464{
465 return irq_lower();
466}
467
468struct vcpu *serr_lower(void)
469{
470 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000471 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000472}
473
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000474/**
475 * Initialises a fault info structure. It assumes that an FnV bit exists at
476 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
477 * the ESR (the fault status code) are 010000; this is the case for both
478 * instruction and data aborts, but not necessarily for other exception reasons.
479 */
480static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100481 const struct vcpu *vcpu, int mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000482{
483 uint32_t fsc = esr & 0x3f;
484 struct vcpu_fault_info r;
485
486 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000487 r.pc = va_init(vcpu->regs.pc);
488
489 /*
490 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
491 * indicates that we cannot rely on far_el2.
492 */
493 if (fsc == 0x10 && esr & (1u << 10)) {
494 r.vaddr = va_init(0);
495 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
496 } else {
497 r.vaddr = va_init(read_msr(far_el2));
498 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
499 (read_msr(far_el2) & (PAGE_SIZE - 1)));
500 }
501
502 return r;
503}
504
Andrew Scull37402872018-10-24 14:23:06 +0100505struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100506{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100507 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000508 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000509 struct vcpu *new_vcpu;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100510 uintreg_t ec = GET_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100511
Fuad Tabbac76466d2019-09-06 10:42:12 +0100512 switch (ec) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100513 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000514 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100515 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100516 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100517 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000518 /* WFE */
519 /*
520 * TODO: consider giving the scheduler more context,
521 * somehow.
522 */
Jose Marinho135dff32019-02-28 10:25:57 +0000523 api_spci_yield(vcpu, &new_vcpu);
524 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100525 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000526 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000527 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100528
529 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000530 info = fault_info_init(
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100531 esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000532 if (vcpu_handle_page_fault(vcpu, &info)) {
533 return NULL;
534 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000535 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100536
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100537 case 0x20: /* EC = 100000, Instruction abort. */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100538 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000539 if (vcpu_handle_page_fault(vcpu, &info)) {
540 return NULL;
541 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000542 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100543
Andrew Walbran59182d52019-09-23 17:55:39 +0100544 case 0x16: /* EC = 010110, HVC instruction */
545 return hvc_handler(vcpu);
546
Andrew Scullc960c032018-10-24 15:13:35 +0100547 case 0x17: /* EC = 010111, SMC instruction. */ {
548 uintreg_t smc_pc = vcpu->regs.pc;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100549 smc_res_t ret;
Andrew Walbran33645652019-04-15 12:29:31 +0100550 struct vcpu *next = NULL;
Andrew Scullc960c032018-10-24 15:13:35 +0100551
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100552 if (!smc_handler(vcpu, &ret, &next)) {
553 /* TODO(b/132421503): handle SMC forward rejection */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100554 dlog("Unsupported SMC call: %#x\n", vcpu->regs.r[0]);
Andrew Walbran59182d52019-09-23 17:55:39 +0100555 ret.res0 = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100556 }
557
558 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100559 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100560 vcpu->regs.r[0] = ret.res0;
561 vcpu->regs.r[1] = ret.res1;
562 vcpu->regs.r[2] = ret.res2;
563 vcpu->regs.r[3] = ret.res3;
Andrew Walbran33645652019-04-15 12:29:31 +0100564 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100565 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100566
Fuad Tabbac76466d2019-09-06 10:42:12 +0100567 /*
568 * EC = 011000, MSR, MRS or System instruction execution that is not
569 * reported using EC 000000, 000001 or 000111.
570 */
571 case 0x18:
572 /*
573 * NOTE: This should never be reached because it goes through a
574 * separate path handled by handle_system_register_access().
575 */
576 panic("Handled by handle_system_register_access().");
577
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100578 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100579 dlog("Unknown lower sync exception pc=%#x, esr=%#x, "
580 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100581 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000582 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100583 }
584
Andrew Scull9726c252019-01-23 13:44:19 +0000585 /* The exception wasn't handled so abort the VM. */
586 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100587}
Fuad Tabbac76466d2019-09-06 10:42:12 +0100588
589/**
590 * Handles EC = 011000, msr, mrs instruction traps.
591 * Returns non-null ONLY if the access failed and the vcpu is changing.
592 */
593struct vcpu *handle_system_register_access(uintreg_t esr)
594{
595 struct vcpu *vcpu = current();
596 spci_vm_id_t vm_id = vcpu->vm->id;
597 uintreg_t ec = GET_EC(esr);
598
599 CHECK(ec == 0x18);
600
601 /*
602 * Handle accesses to other registers that trap with the same EC.
603 * Abort when encountering unhandled register accesses.
604 */
605 if (!is_debug_el1_register_access(esr)) {
606 return api_abort(vcpu);
607 }
608
609 /* Abort if unable to fulfill the debug register access. */
610 if (!debug_el1_process_access(vcpu, vm_id, esr)) {
611 return api_abort(vcpu);
612 }
613
614 /* Instruction was fulfilled above. Skip it and run the next one. */
615 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
616 return NULL;
617}