blob: acbe56254805a8a7d4609b30a8b1d7a77d34158c [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"
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +010035#include "perfmon.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010036#include "psci.h"
Andrew Walbran33645652019-04-15 12:29:31 +010037#include "psci_handler.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000038#include "smc.h"
Fuad Tabbaba8c44d2019-09-23 14:38:58 +010039#include "sysregs.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010040
Andrew Walbran3d84a262018-12-13 14:41:19 +000041#define HCR_EL2_VI (1u << 7)
42
Fuad Tabbac76466d2019-09-06 10:42:12 +010043/**
44 * Gets the Exception Class from the ESR.
45 */
46#define GET_EC(esr) ((esr) >> 26)
47
48/**
49 * Gets the value to increment for the next PC.
50 * The ESR encodes whether the instruction is 2 bytes or 4 bytes long.
51 */
52#define GET_NEXT_PC_INC(esr) (((esr) & (1u << 25)) ? 4 : 2)
53
Fuad Tabbac76466d2019-09-06 10:42:12 +010054/**
Andrew Walbran0dd67ff2019-09-12 16:38:50 +010055 * The Client ID field within X7 for an SMC64 call.
56 */
57#define CLIENT_ID_MASK UINT64_C(0xffff)
58
59/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010060 * Returns a reference to the currently executing vCPU.
61 */
Andrew Scullc960c032018-10-24 15:13:35 +010062static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000063{
64 return (struct vcpu *)read_msr(tpidr_el2);
65}
66
Andrew Walbran1f8d4872018-12-20 11:21:32 +000067/**
68 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
69 * informs the arch-independent sections that registers have been saved.
70 */
71void complete_saving_state(struct vcpu *vcpu)
72{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010073 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
74 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000075
76 api_regs_state_saved(vcpu);
77
78 /*
79 * If switching away from the primary, copy the current EL0 virtual
80 * timer registers to the corresponding EL2 physical timer registers.
81 * This is used to emulate the virtual timer for the primary in case it
82 * should fire while the secondary is running.
83 */
84 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
85 /*
86 * Clear timer control register before copying compare value, to
87 * avoid a spurious timer interrupt. This could be a problem if
88 * the interrupt is configured as edge-triggered, as it would
89 * then be latched in.
90 */
91 write_msr(cnthp_ctl_el2, 0);
92 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
93 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
94 }
95}
96
97/**
98 * Restores the state of per-vCPU peripherals, such as the virtual timer.
99 */
100void begin_restoring_state(struct vcpu *vcpu)
101{
102 /*
103 * Clear timer control register before restoring compare value, to avoid
104 * a spurious timer interrupt. This could be a problem if the interrupt
105 * is configured as edge-triggered, as it would then be latched in.
106 */
107 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100108 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
109 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000110
111 /*
112 * If we are switching (back) to the primary, disable the EL2 physical
113 * timer which was being used to emulate the EL0 virtual timer, as the
114 * virtual timer is now running for the primary again.
115 */
116 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
117 write_msr(cnthp_ctl_el2, 0);
118 write_msr(cnthp_cval_el2, 0);
119 }
120}
121
Andrew Walbran1f32e722019-06-07 17:57:26 +0100122/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100123 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
124 * current VMID.
125 */
126static void invalidate_vm_tlb(void)
127{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100128 /*
129 * Ensure that the last VTTBR write has taken effect so we invalidate
130 * the right set of TLB entries.
131 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100132 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100133
Andrew Walbran1f32e722019-06-07 17:57:26 +0100134 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100135
136 /*
137 * Ensure that no instructions are fetched for the VM until after the
138 * TLB invalidation has taken effect.
139 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100140 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100141
142 /*
143 * Ensure that no data reads or writes for the VM happen until after the
144 * TLB invalidation has taken effect. Non-sharable is enough because the
145 * TLB is local to the CPU.
146 */
David Brazdil851948e2019-08-09 12:02:12 +0100147 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100148}
149
150/**
151 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
152 * the same VM which was run on the current pCPU.
153 *
154 * This is necessary because VMs may (contrary to the architecture
155 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
156 * workaround:
157 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
158 */
159void maybe_invalidate_tlb(struct vcpu *vcpu)
160{
161 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100162 spci_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100163
164 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
165 new_vcpu_index) {
166 /*
167 * The vCPU has changed since the last time this VM was run on
168 * this pCPU, so we need to invalidate the TLB.
169 */
170 invalidate_vm_tlb();
171
172 /* Record the fact that this vCPU is now running on this CPU. */
173 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
174 new_vcpu_index;
175 }
176}
177
Andrew Scullc960c032018-10-24 15:13:35 +0100178noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100179{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000180 (void)elr;
181 (void)spsr;
182
Andrew Sculla9c172d2019-04-03 14:10:00 +0100183 panic("IRQ from current");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100184}
185
Andrew Scullc960c032018-10-24 15:13:35 +0100186noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100187{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000188 (void)elr;
189 (void)spsr;
190
Andrew Sculla9c172d2019-04-03 14:10:00 +0100191 panic("FIQ from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000192}
193
Andrew Scullc960c032018-10-24 15:13:35 +0100194noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000195{
196 (void)elr;
197 (void)spsr;
198
Andrew Sculla9c172d2019-04-03 14:10:00 +0100199 panic("SERR from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000200}
201
Andrew Scullc960c032018-10-24 15:13:35 +0100202noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000203{
204 uintreg_t esr = read_msr(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100205 uintreg_t ec = GET_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000206
207 (void)spsr;
208
Fuad Tabbac76466d2019-09-06 10:42:12 +0100209 switch (ec) {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100210 case 0x25: /* EC = 100101, Data abort. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100211 dlog("Data abort: pc=%#x, esr=%#x, ec=%#x", elr, esr, ec);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100212 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100213 dlog(", far=%#x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100214 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100215 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100216 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100217
218 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000219 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100220
221 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100222 dlog("Unknown current sync exception pc=%#x, esr=%#x, "
223 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100224 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100225 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100226 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000227
Andrew Sculla9c172d2019-04-03 14:10:00 +0100228 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100229}
230
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100231/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000232 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
233 * arch_regs.
234 */
235static void set_virtual_interrupt(struct arch_regs *r, bool enable)
236{
237 if (enable) {
238 r->lazy.hcr_el2 |= HCR_EL2_VI;
239 } else {
240 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
241 }
242}
243
244/**
245 * Sets or clears the VI bit in the HCR_EL2 register.
246 */
247static void set_virtual_interrupt_current(bool enable)
248{
249 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000250
Andrew Walbran3d84a262018-12-13 14:41:19 +0000251 if (enable) {
252 hcr_el2 |= HCR_EL2_VI;
253 } else {
254 hcr_el2 &= ~HCR_EL2_VI;
255 }
256 write_msr(hcr_el2, hcr_el2);
257}
258
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100259static bool smc_check_client_privileges(const struct vcpu *vcpu)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100260{
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100261 (void)vcpu; /*UNUSED*/
262
263 /*
264 * TODO(b/132421503): Check for privileges based on manifest.
265 * Currently returns false, which maintains existing behavior.
266 */
267
268 return false;
269}
270
271/**
272 * Applies SMC access control according to manifest.
273 * Forwards the call if access is granted.
274 * Returns true if call is forwarded.
275 */
Andrew Scullce688f02019-09-30 12:54:14 +0100276static bool smc_forwarder(const struct vcpu *vcpu, struct smc_result *ret)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100277{
278 uint32_t func = vcpu->regs.r[0];
279 /* TODO(b/132421503): obtain vmid according to new scheme. */
280 uint32_t client_id = vcpu->vm->id;
Andrew Walbran0dd67ff2019-09-12 16:38:50 +0100281 /*
282 * Set the Client ID but keep the existing Secure OS ID and anything
283 * else (currently unspecified) that the client may have passed in the
284 * upper bits.
285 */
286 uintreg_t arg7 = client_id | (vcpu->regs.r[7] & ~CLIENT_ID_MASK);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100287
288 if (smc_check_client_privileges(vcpu)) {
Andrew Scull52b8ea12019-08-30 19:16:09 +0100289 *ret = smc_forward(func, vcpu->regs.r[1], vcpu->regs.r[2],
290 vcpu->regs.r[3], vcpu->regs.r[4],
Andrew Walbran0dd67ff2019-09-12 16:38:50 +0100291 vcpu->regs.r[5], vcpu->regs.r[6], arg7);
292 /*
293 * Preserve the value passed by the caller, rather than the
294 * client_id we generated. Note that this would also overwrite
295 * any return value that may be in x7, but the SMCs that we are
296 * forwarding are legacy calls from before SMCCC 1.2 so won't
297 * have more than 4 return values anyway.
298 */
299 ret->res7 = vcpu->regs.r[7];
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100300 return true;
301 }
302
303 return false;
304}
305
Andrew Walbran7f920af2019-09-03 17:09:30 +0100306static bool spci_handler(struct spci_value *args, struct vcpu **next)
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100307{
Andrew Walbran7f920af2019-09-03 17:09:30 +0100308 switch (args->func & ~SMCCC_CONVENTION_MASK) {
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100309 case SPCI_VERSION_32:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100310 *args = api_spci_version();
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100311 return true;
312 case SPCI_YIELD_32:
Andrew Walbran16075b62019-09-03 17:11:07 +0100313 api_yield(current(), next);
314
315 /* SPCI_YIELD always returns SPCI_SUCCESS. */
316 *args = (struct spci_value){.func = SPCI_SUCCESS_32};
317
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100318 return true;
319 case SPCI_MSG_SEND_32:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100320 args->func = api_spci_msg_send(args->arg1, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100321 return true;
Andrew Walbran0de4f162019-09-03 16:44:20 +0100322 case SPCI_MSG_WAIT_32:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100323 *args = api_spci_msg_recv(true, current(), next);
Andrew Walbran0de4f162019-09-03 16:44:20 +0100324 return true;
325 case SPCI_MSG_POLL_32:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100326 *args = api_spci_msg_recv(false, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100327 return true;
328 }
329
330 return false;
331}
332
333/**
334 * Set or clear VI bit according to pending interrupts.
335 */
336static void update_vi(struct vcpu *next)
337{
338 if (next == NULL) {
339 /*
340 * Not switching vCPUs, set the bit for the current vCPU
341 * directly in the register.
342 */
343 struct vcpu *vcpu = current();
344
345 sl_lock(&vcpu->lock);
346 set_virtual_interrupt_current(
347 vcpu->interrupts.enabled_and_pending_count > 0);
348 sl_unlock(&vcpu->lock);
349 } else {
350 /*
351 * About to switch vCPUs, set the bit for the vCPU to which we
352 * are switching in the saved copy of the register.
353 */
354 sl_lock(&next->lock);
355 set_virtual_interrupt(
356 &next->regs,
357 next->interrupts.enabled_and_pending_count > 0);
358 sl_unlock(&next->lock);
359 }
360}
361
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100362/**
363 * Processes SMC instruction calls.
364 */
Andrew Scullce688f02019-09-30 12:54:14 +0100365static bool smc_handler(struct vcpu *vcpu, struct smc_result *ret,
366 struct vcpu **next)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100367{
368 uint32_t func = vcpu->regs.r[0];
369
370 if (psci_handler(vcpu, func, vcpu->regs.r[1], vcpu->regs.r[2],
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100371 vcpu->regs.r[3], &ret->res0, next)) {
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100372 /* SMC PSCI calls are processed by the PSCI handler. */
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100373 return true;
374 }
375
376 switch (func & ~SMCCC_CONVENTION_MASK) {
377 case HF_DEBUG_LOG:
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100378 api_debug_log(vcpu->regs.r[1], vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100379 return true;
380 }
381
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100382 /* Remaining SMC calls need to be forwarded. */
383 return smc_forwarder(vcpu, ret);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100384}
385
Andrew Walbran59182d52019-09-23 17:55:39 +0100386struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100387{
Andrew Walbran7f920af2019-09-03 17:09:30 +0100388 struct spci_value args = {
389 .func = vcpu->regs.r[0],
390 .arg1 = vcpu->regs.r[1],
391 .arg2 = vcpu->regs.r[2],
392 .arg3 = vcpu->regs.r[3],
393 .arg4 = vcpu->regs.r[4],
394 .arg5 = vcpu->regs.r[5],
395 .arg6 = vcpu->regs.r[6],
396 .arg7 = vcpu->regs.r[7],
397 };
Andrew Walbran59182d52019-09-23 17:55:39 +0100398 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100399
Andrew Walbran7f920af2019-09-03 17:09:30 +0100400 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
401 &vcpu->regs.r[0], &next)) {
Andrew Walbran59182d52019-09-23 17:55:39 +0100402 return next;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100403 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100404
Andrew Walbran7f920af2019-09-03 17:09:30 +0100405 if (spci_handler(&args, &next)) {
406 vcpu->regs.r[0] = args.func;
407 vcpu->regs.r[1] = args.arg1;
408 vcpu->regs.r[2] = args.arg2;
409 vcpu->regs.r[3] = args.arg3;
410 vcpu->regs.r[4] = args.arg4;
411 vcpu->regs.r[5] = args.arg5;
412 vcpu->regs.r[6] = args.arg6;
413 vcpu->regs.r[7] = args.arg7;
Andrew Walbran59182d52019-09-23 17:55:39 +0100414 update_vi(next);
415 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100416 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100417
Andrew Walbran7f920af2019-09-03 17:09:30 +0100418 switch (args.func) {
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000419 case HF_VM_GET_ID:
Andrew Walbran59182d52019-09-23 17:55:39 +0100420 vcpu->regs.r[0] = api_vm_get_id(vcpu);
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000421 break;
422
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100423 case HF_VM_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100424 vcpu->regs.r[0] = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100425 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100426
427 case HF_VCPU_GET_COUNT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100428 vcpu->regs.r[0] = api_vcpu_get_count(args.arg1, vcpu);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100429 break;
430
431 case HF_VCPU_RUN:
Andrew Walbran59182d52019-09-23 17:55:39 +0100432 vcpu->regs.r[0] = hf_vcpu_run_return_encode(
Andrew Walbran7f920af2019-09-03 17:09:30 +0100433 api_vcpu_run(args.arg1, args.arg2, vcpu, &next));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100434 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100435
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100436 case HF_VM_CONFIGURE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100437 vcpu->regs.r[0] = api_vm_configure(
438 ipa_init(args.arg1), ipa_init(args.arg2), vcpu, &next);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100439 break;
440
Andrew Scullaa039b32018-10-04 15:02:26 +0100441 case HF_MAILBOX_CLEAR:
Andrew Walbran59182d52019-09-23 17:55:39 +0100442 vcpu->regs.r[0] = api_mailbox_clear(vcpu, &next);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000443 break;
444
445 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100446 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000447 break;
448
449 case HF_MAILBOX_WAITER_GET:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100450 vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100451 break;
452
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000453 case HF_INTERRUPT_ENABLE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100454 vcpu->regs.r[0] =
455 api_interrupt_enable(args.arg1, args.arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000456 break;
457
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000458 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100459 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000460 break;
461
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000462 case HF_INTERRUPT_INJECT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100463 vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2,
464 args.arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000465 break;
466
Andrew Scull6386f252018-12-06 13:29:10 +0000467 case HF_SHARE_MEMORY:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100468 vcpu->regs.r[0] = api_share_memory(
469 args.arg1 >> 32, ipa_init(args.arg2), args.arg3,
470 args.arg1 & 0xffffffff, vcpu);
Andrew Scull6386f252018-12-06 13:29:10 +0000471 break;
472
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100473 case HF_DEBUG_LOG:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100474 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100475 break;
476
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100477 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100478 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100479 }
480
Andrew Walbran59182d52019-09-23 17:55:39 +0100481 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000482
Andrew Walbran59182d52019-09-23 17:55:39 +0100483 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100484}
485
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100486struct vcpu *irq_lower(void)
487{
Andrew Scull9726c252019-01-23 13:44:19 +0000488 /*
489 * Switch back to primary VM, interrupts will be handled there.
490 *
491 * If the VM has aborted, this vCPU will be aborted when the scheduler
492 * tries to run it again. This means the interrupt will not be delayed
493 * by the aborted VM.
494 *
495 * TODO: Only switch when the interrupt isn't for the current VM.
496 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000497 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100498}
499
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000500struct vcpu *fiq_lower(void)
501{
502 return irq_lower();
503}
504
505struct vcpu *serr_lower(void)
506{
507 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000508 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000509}
510
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000511/**
512 * Initialises a fault info structure. It assumes that an FnV bit exists at
513 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
514 * the ESR (the fault status code) are 010000; this is the case for both
515 * instruction and data aborts, but not necessarily for other exception reasons.
516 */
517static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100518 const struct vcpu *vcpu, int mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000519{
520 uint32_t fsc = esr & 0x3f;
521 struct vcpu_fault_info r;
522
523 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000524 r.pc = va_init(vcpu->regs.pc);
525
526 /*
527 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
528 * indicates that we cannot rely on far_el2.
529 */
530 if (fsc == 0x10 && esr & (1u << 10)) {
531 r.vaddr = va_init(0);
532 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
533 } else {
534 r.vaddr = va_init(read_msr(far_el2));
535 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
536 (read_msr(far_el2) & (PAGE_SIZE - 1)));
537 }
538
539 return r;
540}
541
Andrew Scull37402872018-10-24 14:23:06 +0100542struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100543{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100544 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000545 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000546 struct vcpu *new_vcpu;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100547 uintreg_t ec = GET_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100548
Fuad Tabbac76466d2019-09-06 10:42:12 +0100549 switch (ec) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100550 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000551 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100552 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100553 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100554 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000555 /* WFE */
556 /*
557 * TODO: consider giving the scheduler more context,
558 * somehow.
559 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100560 api_yield(vcpu, &new_vcpu);
Jose Marinho135dff32019-02-28 10:25:57 +0000561 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100562 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000563 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000564 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100565
566 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000567 info = fault_info_init(
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100568 esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000569 if (vcpu_handle_page_fault(vcpu, &info)) {
570 return NULL;
571 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000572 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100573
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100574 case 0x20: /* EC = 100000, Instruction abort. */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100575 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000576 if (vcpu_handle_page_fault(vcpu, &info)) {
577 return NULL;
578 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000579 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100580
Andrew Walbran59182d52019-09-23 17:55:39 +0100581 case 0x16: /* EC = 010110, HVC instruction */
582 return hvc_handler(vcpu);
583
Andrew Scullc960c032018-10-24 15:13:35 +0100584 case 0x17: /* EC = 010111, SMC instruction. */ {
585 uintreg_t smc_pc = vcpu->regs.pc;
Andrew Walbran121f88b2019-10-03 14:29:03 +0100586 struct smc_result ret = {.res4 = vcpu->regs.r[4],
587 .res5 = vcpu->regs.r[5],
588 .res6 = vcpu->regs.r[6],
589 .res7 = vcpu->regs.r[7]};
Andrew Walbran33645652019-04-15 12:29:31 +0100590 struct vcpu *next = NULL;
Andrew Scullc960c032018-10-24 15:13:35 +0100591
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100592 if (!smc_handler(vcpu, &ret, &next)) {
593 /* TODO(b/132421503): handle SMC forward rejection */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100594 dlog("Unsupported SMC call: %#x\n", vcpu->regs.r[0]);
Andrew Walbran59182d52019-09-23 17:55:39 +0100595 ret.res0 = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100596 }
597
598 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100599 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100600 vcpu->regs.r[0] = ret.res0;
601 vcpu->regs.r[1] = ret.res1;
602 vcpu->regs.r[2] = ret.res2;
603 vcpu->regs.r[3] = ret.res3;
Andrew Walbran121f88b2019-10-03 14:29:03 +0100604 vcpu->regs.r[4] = ret.res4;
605 vcpu->regs.r[5] = ret.res5;
606 vcpu->regs.r[6] = ret.res6;
607 vcpu->regs.r[7] = ret.res7;
Andrew Walbran33645652019-04-15 12:29:31 +0100608 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100609 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100610
Fuad Tabbac76466d2019-09-06 10:42:12 +0100611 /*
612 * EC = 011000, MSR, MRS or System instruction execution that is not
613 * reported using EC 000000, 000001 or 000111.
614 */
615 case 0x18:
616 /*
617 * NOTE: This should never be reached because it goes through a
618 * separate path handled by handle_system_register_access().
619 */
620 panic("Handled by handle_system_register_access().");
621
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100622 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100623 dlog("Unknown lower sync exception pc=%#x, esr=%#x, "
624 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100625 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000626 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100627 }
628
Andrew Scull9726c252019-01-23 13:44:19 +0000629 /* The exception wasn't handled so abort the VM. */
630 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100631}
Fuad Tabbac76466d2019-09-06 10:42:12 +0100632
633/**
634 * Handles EC = 011000, msr, mrs instruction traps.
635 * Returns non-null ONLY if the access failed and the vcpu is changing.
636 */
637struct vcpu *handle_system_register_access(uintreg_t esr)
638{
639 struct vcpu *vcpu = current();
640 spci_vm_id_t vm_id = vcpu->vm->id;
641 uintreg_t ec = GET_EC(esr);
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100642 char *direction_str;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100643
644 CHECK(ec == 0x18);
645
646 /*
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100647 * Handle accesses to debug and performance monitor registers.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100648 * Abort when encountering unhandled register accesses.
649 */
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100650 if (debug_el1_is_register_access(esr)) {
651 if (!debug_el1_process_access(vcpu, vm_id, esr)) {
652 goto fail;
653 }
654 } else if (perfmon_is_register_access(esr)) {
655 if (!perfmon_process_access(vcpu, vm_id, esr)) {
656 goto fail;
657 }
658 } else {
659 goto fail;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100660 }
661
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100662 /* Instruction was fulfilled. Skip it and run the next one. */
663 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
664 return NULL;
665
666fail:
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100667 direction_str = ISS_IS_READ(esr) ? "read" : "write";
Fuad Tabbac76466d2019-09-06 10:42:12 +0100668
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100669 dlog("Unhandled system register %s: op0=%d, op1=%d, crn=%d, "
670 "crm=%d, op2=%d, rt=%d.\n",
671 direction_str, GET_ISS_OP0(esr), GET_ISS_OP1(esr),
672 GET_ISS_CRN(esr), GET_ISS_CRM(esr), GET_ISS_OP2(esr),
673 GET_ISS_RT(esr));
674
675 /* Abort if unable to fulfill the register access. */
676 return api_abort(vcpu);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100677}