blob: 1125dcf66bbd44a54ce39441dd660af01f59d494 [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
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/**
Andrew Walbran0dd67ff2019-09-12 16:38:50 +010053 * The Client ID field within X7 for an SMC64 call.
54 */
55#define CLIENT_ID_MASK UINT64_C(0xffff)
56
57/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010058 * Returns a reference to the currently executing vCPU.
59 */
Andrew Scullc960c032018-10-24 15:13:35 +010060static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000061{
62 return (struct vcpu *)read_msr(tpidr_el2);
63}
64
Andrew Walbran1f8d4872018-12-20 11:21:32 +000065/**
66 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
67 * informs the arch-independent sections that registers have been saved.
68 */
69void complete_saving_state(struct vcpu *vcpu)
70{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010071 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
72 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000073
74 api_regs_state_saved(vcpu);
75
76 /*
77 * If switching away from the primary, copy the current EL0 virtual
78 * timer registers to the corresponding EL2 physical timer registers.
79 * This is used to emulate the virtual timer for the primary in case it
80 * should fire while the secondary is running.
81 */
82 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
83 /*
84 * Clear timer control register before copying compare value, to
85 * avoid a spurious timer interrupt. This could be a problem if
86 * the interrupt is configured as edge-triggered, as it would
87 * then be latched in.
88 */
89 write_msr(cnthp_ctl_el2, 0);
90 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
91 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
92 }
93}
94
95/**
96 * Restores the state of per-vCPU peripherals, such as the virtual timer.
97 */
98void begin_restoring_state(struct vcpu *vcpu)
99{
100 /*
101 * Clear timer control register before restoring compare value, to avoid
102 * a spurious timer interrupt. This could be a problem if the interrupt
103 * is configured as edge-triggered, as it would then be latched in.
104 */
105 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100106 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
107 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000108
109 /*
110 * If we are switching (back) to the primary, disable the EL2 physical
111 * timer which was being used to emulate the EL0 virtual timer, as the
112 * virtual timer is now running for the primary again.
113 */
114 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
115 write_msr(cnthp_ctl_el2, 0);
116 write_msr(cnthp_cval_el2, 0);
117 }
118}
119
Andrew Walbran1f32e722019-06-07 17:57:26 +0100120/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100121 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
122 * current VMID.
123 */
124static void invalidate_vm_tlb(void)
125{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100126 /*
127 * Ensure that the last VTTBR write has taken effect so we invalidate
128 * the right set of TLB entries.
129 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100130 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100131
Andrew Walbran1f32e722019-06-07 17:57:26 +0100132 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100133
134 /*
135 * Ensure that no instructions are fetched for the VM until after the
136 * TLB invalidation has taken effect.
137 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100138 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100139
140 /*
141 * Ensure that no data reads or writes for the VM happen until after the
142 * TLB invalidation has taken effect. Non-sharable is enough because the
143 * TLB is local to the CPU.
144 */
David Brazdil851948e2019-08-09 12:02:12 +0100145 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100146}
147
148/**
149 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
150 * the same VM which was run on the current pCPU.
151 *
152 * This is necessary because VMs may (contrary to the architecture
153 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
154 * workaround:
155 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
156 */
157void maybe_invalidate_tlb(struct vcpu *vcpu)
158{
159 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100160 spci_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100161
162 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
163 new_vcpu_index) {
164 /*
165 * The vCPU has changed since the last time this VM was run on
166 * this pCPU, so we need to invalidate the TLB.
167 */
168 invalidate_vm_tlb();
169
170 /* Record the fact that this vCPU is now running on this CPU. */
171 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
172 new_vcpu_index;
173 }
174}
175
Andrew Scullc960c032018-10-24 15:13:35 +0100176noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100177{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000178 (void)elr;
179 (void)spsr;
180
Andrew Sculla9c172d2019-04-03 14:10:00 +0100181 panic("IRQ from current");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100182}
183
Andrew Scullc960c032018-10-24 15:13:35 +0100184noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100185{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000186 (void)elr;
187 (void)spsr;
188
Andrew Sculla9c172d2019-04-03 14:10:00 +0100189 panic("FIQ from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000190}
191
Andrew Scullc960c032018-10-24 15:13:35 +0100192noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000193{
194 (void)elr;
195 (void)spsr;
196
Andrew Sculla9c172d2019-04-03 14:10:00 +0100197 panic("SERR from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000198}
199
Andrew Scullc960c032018-10-24 15:13:35 +0100200noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000201{
202 uintreg_t esr = read_msr(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100203 uintreg_t ec = GET_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000204
205 (void)spsr;
206
Fuad Tabbac76466d2019-09-06 10:42:12 +0100207 switch (ec) {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100208 case 0x25: /* EC = 100101, Data abort. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100209 dlog("Data abort: pc=%#x, esr=%#x, ec=%#x", elr, esr, ec);
Andrew Walbrane52006c2019-10-22 18:01:28 +0100210 if (!(esr & (1U << 10))) { /* Check FnV bit. */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100211 dlog(", far=%#x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100212 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100213 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100214 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100215
216 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000217 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100218
219 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100220 dlog("Unknown current sync exception pc=%#x, esr=%#x, "
221 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100222 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100223 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100224 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000225
Andrew Sculla9c172d2019-04-03 14:10:00 +0100226 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100227}
228
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100229/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000230 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
231 * arch_regs.
232 */
233static void set_virtual_interrupt(struct arch_regs *r, bool enable)
234{
235 if (enable) {
236 r->lazy.hcr_el2 |= HCR_EL2_VI;
237 } else {
238 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
239 }
240}
241
242/**
243 * Sets or clears the VI bit in the HCR_EL2 register.
244 */
245static void set_virtual_interrupt_current(bool enable)
246{
247 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000248
Andrew Walbran3d84a262018-12-13 14:41:19 +0000249 if (enable) {
250 hcr_el2 |= HCR_EL2_VI;
251 } else {
252 hcr_el2 &= ~HCR_EL2_VI;
253 }
254 write_msr(hcr_el2, hcr_el2);
255}
256
Andrew Scullae9962e2019-10-03 16:51:16 +0100257/**
258 * Checks whether to block an SMC being forwarded from a VM.
259 */
260static bool smc_is_blocked(const struct vm *vm, uint32_t func)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100261{
Andrew Scullae9962e2019-10-03 16:51:16 +0100262 bool block_by_default = !vm->smc_whitelist.permissive;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100263
Andrew Scullae9962e2019-10-03 16:51:16 +0100264 for (size_t i = 0; i < vm->smc_whitelist.smc_count; ++i) {
265 if (func == vm->smc_whitelist.smcs[i]) {
266 return false;
267 }
268 }
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100269
Andrew Scullae9962e2019-10-03 16:51:16 +0100270 dlog("SMC %#010x attempted from VM %d, blocked=%d\n", func, vm->id,
271 block_by_default);
272
273 /* Access is still allowed in permissive mode. */
274 return block_by_default;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100275}
276
277/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100278 * Applies SMC access control according to manifest and forwards the call if
279 * access is granted.
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100280 */
Andrew Scullae9962e2019-10-03 16:51:16 +0100281static void smc_forwarder(const struct vcpu *vcpu, struct smc_result *ret)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100282{
283 uint32_t func = vcpu->regs.r[0];
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100284 uint32_t client_id = vcpu->vm->id;
Andrew Scullae9962e2019-10-03 16:51:16 +0100285 uintreg_t arg7;
286
287 if (smc_is_blocked(vcpu->vm, func)) {
288 ret->res0 = SMCCC_ERROR_UNKNOWN;
289 return;
290 }
291
Andrew Walbran0dd67ff2019-09-12 16:38:50 +0100292 /*
293 * Set the Client ID but keep the existing Secure OS ID and anything
294 * else (currently unspecified) that the client may have passed in the
295 * upper bits.
296 */
Andrew Scullae9962e2019-10-03 16:51:16 +0100297 arg7 = client_id | (vcpu->regs.r[7] & ~CLIENT_ID_MASK);
298 *ret = smc_forward(func, vcpu->regs.r[1], vcpu->regs.r[2],
299 vcpu->regs.r[3], vcpu->regs.r[4], vcpu->regs.r[5],
300 vcpu->regs.r[6], arg7);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100301
Andrew Scullae9962e2019-10-03 16:51:16 +0100302 /*
303 * Preserve the value passed by the caller, rather than the client_id we
304 * generated. Note that this would also overwrite any return value that
305 * may be in x7, but the SMCs that we are forwarding are legacy calls
306 * from before SMCCC 1.2 so won't have more than 4 return values anyway.
307 */
308 ret->res7 = vcpu->regs.r[7];
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100309}
310
Andrew Walbran7f920af2019-09-03 17:09:30 +0100311static bool spci_handler(struct spci_value *args, struct vcpu **next)
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100312{
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100313 /*
314 * NOTE: When adding new methods to this handler update
315 * api_spci_features accordingly.
316 */
Andrew Walbran7f920af2019-09-03 17:09:30 +0100317 switch (args->func & ~SMCCC_CONVENTION_MASK) {
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100318 case SPCI_VERSION_32:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100319 *args = api_spci_version();
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100320 return true;
Andrew Walbrand230f662019-10-07 18:03:36 +0100321 case SPCI_ID_GET_32:
322 *args = api_spci_id_get(current());
323 return true;
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100324 case SPCI_FEATURES_32:
325 *args = api_spci_features(args->arg1);
326 return true;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100327 case SPCI_YIELD_32:
Andrew Walbran16075b62019-09-03 17:11:07 +0100328 api_yield(current(), next);
329
330 /* SPCI_YIELD always returns SPCI_SUCCESS. */
331 *args = (struct spci_value){.func = SPCI_SUCCESS_32};
332
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100333 return true;
334 case SPCI_MSG_SEND_32:
Andrew Walbran70bc8622019-10-07 14:15:58 +0100335 *args = api_spci_msg_send(spci_msg_send_sender(*args),
336 spci_msg_send_receiver(*args),
337 spci_msg_send_size(*args),
338 spci_msg_send_attributes(*args),
339 current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100340 return true;
Andrew Walbran0de4f162019-09-03 16:44:20 +0100341 case SPCI_MSG_WAIT_32:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100342 *args = api_spci_msg_recv(true, current(), next);
Andrew Walbran0de4f162019-09-03 16:44:20 +0100343 return true;
344 case SPCI_MSG_POLL_32:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100345 *args = api_spci_msg_recv(false, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100346 return true;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100347 case SPCI_RUN_32:
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000348 *args = api_spci_run(spci_vm_id(*args), spci_vcpu_index(*args),
349 current(), next);
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100350 return true;
351 }
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100352
353 return false;
354}
355
356/**
357 * Set or clear VI bit according to pending interrupts.
358 */
359static void update_vi(struct vcpu *next)
360{
361 if (next == NULL) {
362 /*
363 * Not switching vCPUs, set the bit for the current vCPU
364 * directly in the register.
365 */
366 struct vcpu *vcpu = current();
367
368 sl_lock(&vcpu->lock);
369 set_virtual_interrupt_current(
370 vcpu->interrupts.enabled_and_pending_count > 0);
371 sl_unlock(&vcpu->lock);
372 } else {
373 /*
374 * About to switch vCPUs, set the bit for the vCPU to which we
375 * are switching in the saved copy of the register.
376 */
377 sl_lock(&next->lock);
378 set_virtual_interrupt(
379 &next->regs,
380 next->interrupts.enabled_and_pending_count > 0);
381 sl_unlock(&next->lock);
382 }
383}
384
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100385/**
386 * Processes SMC instruction calls.
387 */
Andrew Scullae9962e2019-10-03 16:51:16 +0100388static void smc_handler(struct vcpu *vcpu, struct smc_result *ret,
Andrew Scullce688f02019-09-30 12:54:14 +0100389 struct vcpu **next)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100390{
391 uint32_t func = vcpu->regs.r[0];
392
393 if (psci_handler(vcpu, func, vcpu->regs.r[1], vcpu->regs.r[2],
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100394 vcpu->regs.r[3], &ret->res0, next)) {
Andrew Scullae9962e2019-10-03 16:51:16 +0100395 return;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100396 }
397
398 switch (func & ~SMCCC_CONVENTION_MASK) {
399 case HF_DEBUG_LOG:
Andrew Scullae9962e2019-10-03 16:51:16 +0100400 ret->res0 = api_debug_log(vcpu->regs.r[1], vcpu);
401 return;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100402 }
403
Andrew Scullae9962e2019-10-03 16:51:16 +0100404 smc_forwarder(vcpu, ret);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100405}
406
Andrew Walbran59182d52019-09-23 17:55:39 +0100407struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100408{
Andrew Walbran7f920af2019-09-03 17:09:30 +0100409 struct spci_value args = {
410 .func = vcpu->regs.r[0],
411 .arg1 = vcpu->regs.r[1],
412 .arg2 = vcpu->regs.r[2],
413 .arg3 = vcpu->regs.r[3],
414 .arg4 = vcpu->regs.r[4],
415 .arg5 = vcpu->regs.r[5],
416 .arg6 = vcpu->regs.r[6],
417 .arg7 = vcpu->regs.r[7],
418 };
Andrew Walbran59182d52019-09-23 17:55:39 +0100419 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100420
Andrew Walbran7f920af2019-09-03 17:09:30 +0100421 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
422 &vcpu->regs.r[0], &next)) {
Andrew Walbran59182d52019-09-23 17:55:39 +0100423 return next;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100424 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100425
Andrew Walbran7f920af2019-09-03 17:09:30 +0100426 if (spci_handler(&args, &next)) {
427 vcpu->regs.r[0] = args.func;
428 vcpu->regs.r[1] = args.arg1;
429 vcpu->regs.r[2] = args.arg2;
430 vcpu->regs.r[3] = args.arg3;
431 vcpu->regs.r[4] = args.arg4;
432 vcpu->regs.r[5] = args.arg5;
433 vcpu->regs.r[6] = args.arg6;
434 vcpu->regs.r[7] = args.arg7;
Andrew Walbran59182d52019-09-23 17:55:39 +0100435 update_vi(next);
436 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100437 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100438
Andrew Walbran7f920af2019-09-03 17:09:30 +0100439 switch (args.func) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100440 case HF_VM_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100441 vcpu->regs.r[0] = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100442 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100443
444 case HF_VCPU_GET_COUNT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100445 vcpu->regs.r[0] = api_vcpu_get_count(args.arg1, vcpu);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100446 break;
447
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100448 case HF_VM_CONFIGURE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100449 vcpu->regs.r[0] = api_vm_configure(
450 ipa_init(args.arg1), ipa_init(args.arg2), vcpu, &next);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100451 break;
452
Andrew Scullaa039b32018-10-04 15:02:26 +0100453 case HF_MAILBOX_CLEAR:
Andrew Walbran59182d52019-09-23 17:55:39 +0100454 vcpu->regs.r[0] = api_mailbox_clear(vcpu, &next);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000455 break;
456
457 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100458 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000459 break;
460
461 case HF_MAILBOX_WAITER_GET:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100462 vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100463 break;
464
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000465 case HF_INTERRUPT_ENABLE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100466 vcpu->regs.r[0] =
467 api_interrupt_enable(args.arg1, args.arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000468 break;
469
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000470 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100471 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000472 break;
473
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000474 case HF_INTERRUPT_INJECT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100475 vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2,
476 args.arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000477 break;
478
Andrew Scull6386f252018-12-06 13:29:10 +0000479 case HF_SHARE_MEMORY:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100480 vcpu->regs.r[0] = api_share_memory(
481 args.arg1 >> 32, ipa_init(args.arg2), args.arg3,
482 args.arg1 & 0xffffffff, vcpu);
Andrew Scull6386f252018-12-06 13:29:10 +0000483 break;
484
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100485 case HF_DEBUG_LOG:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100486 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100487 break;
488
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100489 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100490 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100491 }
492
Andrew Walbran59182d52019-09-23 17:55:39 +0100493 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000494
Andrew Walbran59182d52019-09-23 17:55:39 +0100495 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100496}
497
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100498struct vcpu *irq_lower(void)
499{
Andrew Scull9726c252019-01-23 13:44:19 +0000500 /*
501 * Switch back to primary VM, interrupts will be handled there.
502 *
503 * If the VM has aborted, this vCPU will be aborted when the scheduler
504 * tries to run it again. This means the interrupt will not be delayed
505 * by the aborted VM.
506 *
507 * TODO: Only switch when the interrupt isn't for the current VM.
508 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000509 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100510}
511
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000512struct vcpu *fiq_lower(void)
513{
514 return irq_lower();
515}
516
517struct vcpu *serr_lower(void)
518{
519 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000520 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000521}
522
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000523/**
524 * Initialises a fault info structure. It assumes that an FnV bit exists at
525 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
526 * the ESR (the fault status code) are 010000; this is the case for both
527 * instruction and data aborts, but not necessarily for other exception reasons.
528 */
529static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100530 const struct vcpu *vcpu,
531 uint32_t mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000532{
533 uint32_t fsc = esr & 0x3f;
534 struct vcpu_fault_info r;
535
536 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000537 r.pc = va_init(vcpu->regs.pc);
538
539 /*
540 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
541 * indicates that we cannot rely on far_el2.
542 */
Andrew Walbrane52006c2019-10-22 18:01:28 +0100543 if (fsc == 0x10 && esr & (1U << 10)) {
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000544 r.vaddr = va_init(0);
545 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
546 } else {
547 r.vaddr = va_init(read_msr(far_el2));
548 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
549 (read_msr(far_el2) & (PAGE_SIZE - 1)));
550 }
551
552 return r;
553}
554
Andrew Scull37402872018-10-24 14:23:06 +0100555struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100556{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100557 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000558 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000559 struct vcpu *new_vcpu;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100560 uintreg_t ec = GET_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100561
Fuad Tabbac76466d2019-09-06 10:42:12 +0100562 switch (ec) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100563 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000564 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100565 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100566 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100567 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000568 /* WFE */
569 /*
570 * TODO: consider giving the scheduler more context,
571 * somehow.
572 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100573 api_yield(vcpu, &new_vcpu);
Jose Marinho135dff32019-02-28 10:25:57 +0000574 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100575 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000576 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000577 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100578
579 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000580 info = fault_info_init(
Andrew Walbrane52006c2019-10-22 18:01:28 +0100581 esr, vcpu, (esr & (1U << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000582 if (vcpu_handle_page_fault(vcpu, &info)) {
583 return NULL;
584 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000585 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100586
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100587 case 0x20: /* EC = 100000, Instruction abort. */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100588 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000589 if (vcpu_handle_page_fault(vcpu, &info)) {
590 return NULL;
591 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000592 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100593
Andrew Walbran59182d52019-09-23 17:55:39 +0100594 case 0x16: /* EC = 010110, HVC instruction */
595 return hvc_handler(vcpu);
596
Andrew Scullc960c032018-10-24 15:13:35 +0100597 case 0x17: /* EC = 010111, SMC instruction. */ {
598 uintreg_t smc_pc = vcpu->regs.pc;
Andrew Scullae9962e2019-10-03 16:51:16 +0100599 struct vcpu *next = NULL;
Andrew Walbran121f88b2019-10-03 14:29:03 +0100600 struct smc_result ret = {.res4 = vcpu->regs.r[4],
601 .res5 = vcpu->regs.r[5],
602 .res6 = vcpu->regs.r[6],
603 .res7 = vcpu->regs.r[7]};
Andrew Scullc960c032018-10-24 15:13:35 +0100604
Andrew Scullae9962e2019-10-03 16:51:16 +0100605 smc_handler(vcpu, &ret, &next);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100606
607 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100608 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100609 vcpu->regs.r[0] = ret.res0;
610 vcpu->regs.r[1] = ret.res1;
611 vcpu->regs.r[2] = ret.res2;
612 vcpu->regs.r[3] = ret.res3;
Andrew Walbran121f88b2019-10-03 14:29:03 +0100613 vcpu->regs.r[4] = ret.res4;
614 vcpu->regs.r[5] = ret.res5;
615 vcpu->regs.r[6] = ret.res6;
616 vcpu->regs.r[7] = ret.res7;
Andrew Walbran33645652019-04-15 12:29:31 +0100617 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100618 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100619
Fuad Tabbac76466d2019-09-06 10:42:12 +0100620 /*
621 * EC = 011000, MSR, MRS or System instruction execution that is not
622 * reported using EC 000000, 000001 or 000111.
623 */
624 case 0x18:
625 /*
626 * NOTE: This should never be reached because it goes through a
627 * separate path handled by handle_system_register_access().
628 */
629 panic("Handled by handle_system_register_access().");
630
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100631 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100632 dlog("Unknown lower sync exception pc=%#x, esr=%#x, "
633 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100634 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000635 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100636 }
637
Andrew Scull9726c252019-01-23 13:44:19 +0000638 /* The exception wasn't handled so abort the VM. */
639 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100640}
Fuad Tabbac76466d2019-09-06 10:42:12 +0100641
642/**
643 * Handles EC = 011000, msr, mrs instruction traps.
644 * Returns non-null ONLY if the access failed and the vcpu is changing.
645 */
646struct vcpu *handle_system_register_access(uintreg_t esr)
647{
648 struct vcpu *vcpu = current();
649 spci_vm_id_t vm_id = vcpu->vm->id;
650 uintreg_t ec = GET_EC(esr);
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100651 char *direction_str;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100652
653 CHECK(ec == 0x18);
654
655 /*
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100656 * Handle accesses to debug and performance monitor registers.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100657 * Abort when encountering unhandled register accesses.
658 */
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100659 if (debug_el1_is_register_access(esr)) {
660 if (!debug_el1_process_access(vcpu, vm_id, esr)) {
661 goto fail;
662 }
663 } else if (perfmon_is_register_access(esr)) {
664 if (!perfmon_process_access(vcpu, vm_id, esr)) {
665 goto fail;
666 }
667 } else {
668 goto fail;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100669 }
670
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100671 /* Instruction was fulfilled. Skip it and run the next one. */
672 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
673 return NULL;
674
675fail:
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100676 direction_str = ISS_IS_READ(esr) ? "read" : "write";
Fuad Tabbac76466d2019-09-06 10:42:12 +0100677
Fuad Tabbaba8c44d2019-09-23 14:38:58 +0100678 dlog("Unhandled system register %s: op0=%d, op1=%d, crn=%d, "
679 "crm=%d, op2=%d, rt=%d.\n",
680 direction_str, GET_ISS_OP0(esr), GET_ISS_OP1(esr),
681 GET_ISS_CRN(esr), GET_ISS_CRM(esr), GET_ISS_OP2(esr),
682 GET_ISS_RT(esr));
683
684 /* Abort if unable to fulfill the register access. */
685 return api_abort(vcpu);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100686}