blob: a2c4bf432007bd3270a56290a18f9b51c9c64d19 [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"
Fuad Tabba77a4b012019-11-15 12:13:08 +000034#include "feature_id.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010035#include "msr.h"
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +010036#include "perfmon.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010037#include "psci.h"
Andrew Walbran33645652019-04-15 12:29:31 +010038#include "psci_handler.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000039#include "smc.h"
Fuad Tabbaba8c44d2019-09-23 14:38:58 +010040#include "sysregs.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010041
Fuad Tabbac76466d2019-09-06 10:42:12 +010042/**
43 * Gets the Exception Class from the ESR.
44 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +000045#define GET_ESR_EC(esr) ((esr) >> 26)
46
47/**
48 * Gets the Instruction Length bit for the synchronous exception
49 */
50#define GET_ESR_IL(esr) ((esr) & (1 << 25))
Fuad Tabbac76466d2019-09-06 10:42:12 +010051
52/**
53 * Gets the value to increment for the next PC.
54 * The ESR encodes whether the instruction is 2 bytes or 4 bytes long.
55 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +000056#define GET_NEXT_PC_INC(esr) (GET_ESR_IL(esr) ? 4 : 2)
Fuad Tabbac76466d2019-09-06 10:42:12 +010057
Fuad Tabbac76466d2019-09-06 10:42:12 +010058/**
Andrew Walbran0dd67ff2019-09-12 16:38:50 +010059 * The Client ID field within X7 for an SMC64 call.
60 */
61#define CLIENT_ID_MASK UINT64_C(0xffff)
62
63/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010064 * Returns a reference to the currently executing vCPU.
65 */
Andrew Scullc960c032018-10-24 15:13:35 +010066static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000067{
68 return (struct vcpu *)read_msr(tpidr_el2);
69}
70
Andrew Walbran1f8d4872018-12-20 11:21:32 +000071/**
72 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
73 * informs the arch-independent sections that registers have been saved.
74 */
75void complete_saving_state(struct vcpu *vcpu)
76{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010077 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
78 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000079
80 api_regs_state_saved(vcpu);
81
82 /*
83 * If switching away from the primary, copy the current EL0 virtual
84 * timer registers to the corresponding EL2 physical timer registers.
85 * This is used to emulate the virtual timer for the primary in case it
86 * should fire while the secondary is running.
87 */
88 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
89 /*
90 * Clear timer control register before copying compare value, to
91 * avoid a spurious timer interrupt. This could be a problem if
92 * the interrupt is configured as edge-triggered, as it would
93 * then be latched in.
94 */
95 write_msr(cnthp_ctl_el2, 0);
96 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
97 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
98 }
99}
100
101/**
102 * Restores the state of per-vCPU peripherals, such as the virtual timer.
103 */
104void begin_restoring_state(struct vcpu *vcpu)
105{
106 /*
107 * Clear timer control register before restoring compare value, to avoid
108 * a spurious timer interrupt. This could be a problem if the interrupt
109 * is configured as edge-triggered, as it would then be latched in.
110 */
111 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100112 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
113 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000114
115 /*
116 * If we are switching (back) to the primary, disable the EL2 physical
117 * timer which was being used to emulate the EL0 virtual timer, as the
118 * virtual timer is now running for the primary again.
119 */
120 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
121 write_msr(cnthp_ctl_el2, 0);
122 write_msr(cnthp_cval_el2, 0);
123 }
124}
125
Andrew Walbran1f32e722019-06-07 17:57:26 +0100126/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100127 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
128 * current VMID.
129 */
130static void invalidate_vm_tlb(void)
131{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100132 /*
133 * Ensure that the last VTTBR write has taken effect so we invalidate
134 * the right set of TLB entries.
135 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100136 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100137
Andrew Walbran1f32e722019-06-07 17:57:26 +0100138 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100139
140 /*
141 * Ensure that no instructions are fetched for the VM until after the
142 * TLB invalidation has taken effect.
143 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100144 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100145
146 /*
147 * Ensure that no data reads or writes for the VM happen until after the
Fuad Tabba77a4b012019-11-15 12:13:08 +0000148 * TLB invalidation has taken effect. Non-shareable is enough because
149 * the TLB is local to the CPU.
Andrew Walbrancff1f682019-07-04 14:52:45 +0100150 */
David Brazdil851948e2019-08-09 12:02:12 +0100151 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100152}
153
154/**
155 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
156 * the same VM which was run on the current pCPU.
157 *
158 * This is necessary because VMs may (contrary to the architecture
159 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
160 * workaround:
161 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
162 */
163void maybe_invalidate_tlb(struct vcpu *vcpu)
164{
165 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100166 spci_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100167
168 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
169 new_vcpu_index) {
170 /*
171 * The vCPU has changed since the last time this VM was run on
172 * this pCPU, so we need to invalidate the TLB.
173 */
174 invalidate_vm_tlb();
175
176 /* Record the fact that this vCPU is now running on this CPU. */
177 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
178 new_vcpu_index;
179 }
180}
181
Andrew Scullc960c032018-10-24 15:13:35 +0100182noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100183{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000184 (void)elr;
185 (void)spsr;
186
Andrew Sculla9c172d2019-04-03 14:10:00 +0100187 panic("IRQ from current");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100188}
189
Andrew Scullc960c032018-10-24 15:13:35 +0100190noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100191{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000192 (void)elr;
193 (void)spsr;
194
Andrew Sculla9c172d2019-04-03 14:10:00 +0100195 panic("FIQ from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000196}
197
Andrew Scullc960c032018-10-24 15:13:35 +0100198noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000199{
200 (void)elr;
201 (void)spsr;
202
Andrew Sculla9c172d2019-04-03 14:10:00 +0100203 panic("SERR from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000204}
205
Andrew Scullc960c032018-10-24 15:13:35 +0100206noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000207{
208 uintreg_t esr = read_msr(esr_el2);
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000209 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000210
211 (void)spsr;
212
Fuad Tabbac76466d2019-09-06 10:42:12 +0100213 switch (ec) {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100214 case 0x25: /* EC = 100101, Data abort. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100215 dlog("Data abort: pc=%#x, esr=%#x, ec=%#x", elr, esr, ec);
Andrew Walbrane52006c2019-10-22 18:01:28 +0100216 if (!(esr & (1U << 10))) { /* Check FnV bit. */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100217 dlog(", far=%#x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100218 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100219 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100220 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100221
222 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000223 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100224
225 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100226 dlog("Unknown current sync exception pc=%#x, esr=%#x, "
227 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100228 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100229 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100230 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000231
Andrew Sculla9c172d2019-04-03 14:10:00 +0100232 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100233}
234
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100235/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000236 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
237 * arch_regs.
238 */
239static void set_virtual_interrupt(struct arch_regs *r, bool enable)
240{
241 if (enable) {
242 r->lazy.hcr_el2 |= HCR_EL2_VI;
243 } else {
244 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
245 }
246}
247
248/**
249 * Sets or clears the VI bit in the HCR_EL2 register.
250 */
251static void set_virtual_interrupt_current(bool enable)
252{
253 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000254
Andrew Walbran3d84a262018-12-13 14:41:19 +0000255 if (enable) {
256 hcr_el2 |= HCR_EL2_VI;
257 } else {
258 hcr_el2 &= ~HCR_EL2_VI;
259 }
260 write_msr(hcr_el2, hcr_el2);
261}
262
Andrew Scullae9962e2019-10-03 16:51:16 +0100263/**
264 * Checks whether to block an SMC being forwarded from a VM.
265 */
266static bool smc_is_blocked(const struct vm *vm, uint32_t func)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100267{
Andrew Scullae9962e2019-10-03 16:51:16 +0100268 bool block_by_default = !vm->smc_whitelist.permissive;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100269
Andrew Scullae9962e2019-10-03 16:51:16 +0100270 for (size_t i = 0; i < vm->smc_whitelist.smc_count; ++i) {
271 if (func == vm->smc_whitelist.smcs[i]) {
272 return false;
273 }
274 }
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100275
Andrew Scullae9962e2019-10-03 16:51:16 +0100276 dlog("SMC %#010x attempted from VM %d, blocked=%d\n", func, vm->id,
277 block_by_default);
278
279 /* Access is still allowed in permissive mode. */
280 return block_by_default;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100281}
282
283/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100284 * Applies SMC access control according to manifest and forwards the call if
285 * access is granted.
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100286 */
Andrew Walbran6606d862019-11-15 16:43:18 +0000287static void smc_forwarder(const struct vcpu *vcpu, struct spci_value *ret)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100288{
289 uint32_t func = vcpu->regs.r[0];
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100290 uint32_t client_id = vcpu->vm->id;
Andrew Scullae9962e2019-10-03 16:51:16 +0100291 uintreg_t arg7;
292
293 if (smc_is_blocked(vcpu->vm, func)) {
Andrew Walbran6606d862019-11-15 16:43:18 +0000294 ret->func = SMCCC_ERROR_UNKNOWN;
Andrew Scullae9962e2019-10-03 16:51:16 +0100295 return;
296 }
297
Andrew Walbran0dd67ff2019-09-12 16:38:50 +0100298 /*
299 * Set the Client ID but keep the existing Secure OS ID and anything
300 * else (currently unspecified) that the client may have passed in the
301 * upper bits.
302 */
Andrew Scullae9962e2019-10-03 16:51:16 +0100303 arg7 = client_id | (vcpu->regs.r[7] & ~CLIENT_ID_MASK);
304 *ret = smc_forward(func, vcpu->regs.r[1], vcpu->regs.r[2],
305 vcpu->regs.r[3], vcpu->regs.r[4], vcpu->regs.r[5],
306 vcpu->regs.r[6], arg7);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100307
Andrew Scullae9962e2019-10-03 16:51:16 +0100308 /*
309 * Preserve the value passed by the caller, rather than the client_id we
310 * generated. Note that this would also overwrite any return value that
311 * may be in x7, but the SMCs that we are forwarding are legacy calls
312 * from before SMCCC 1.2 so won't have more than 4 return values anyway.
313 */
Andrew Walbran6606d862019-11-15 16:43:18 +0000314 ret->arg7 = vcpu->regs.r[7];
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100315}
316
Andrew Walbran7f920af2019-09-03 17:09:30 +0100317static bool spci_handler(struct spci_value *args, struct vcpu **next)
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100318{
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100319 /*
320 * NOTE: When adding new methods to this handler update
321 * api_spci_features accordingly.
322 */
Andrew Walbran7f920af2019-09-03 17:09:30 +0100323 switch (args->func & ~SMCCC_CONVENTION_MASK) {
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100324 case SPCI_VERSION_32:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100325 *args = api_spci_version();
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100326 return true;
Andrew Walbrand230f662019-10-07 18:03:36 +0100327 case SPCI_ID_GET_32:
328 *args = api_spci_id_get(current());
329 return true;
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100330 case SPCI_FEATURES_32:
331 *args = api_spci_features(args->arg1);
332 return true;
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +0000333 case SPCI_RX_RELEASE_32:
334 *args = api_spci_rx_release(current(), next);
335 return true;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000336 case SPCI_RXTX_MAP_32:
337 *args = api_spci_rxtx_map(ipa_init(args->arg1),
338 ipa_init(args->arg2), args->arg3,
339 current(), next);
340 return true;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100341 case SPCI_YIELD_32:
Andrew Walbran16075b62019-09-03 17:11:07 +0100342 api_yield(current(), next);
343
344 /* SPCI_YIELD always returns SPCI_SUCCESS. */
345 *args = (struct spci_value){.func = SPCI_SUCCESS_32};
346
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100347 return true;
348 case SPCI_MSG_SEND_32:
Andrew Walbran70bc8622019-10-07 14:15:58 +0100349 *args = api_spci_msg_send(spci_msg_send_sender(*args),
350 spci_msg_send_receiver(*args),
351 spci_msg_send_size(*args),
352 spci_msg_send_attributes(*args),
353 current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100354 return true;
Andrew Walbran0de4f162019-09-03 16:44:20 +0100355 case SPCI_MSG_WAIT_32:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100356 *args = api_spci_msg_recv(true, current(), next);
Andrew Walbran0de4f162019-09-03 16:44:20 +0100357 return true;
358 case SPCI_MSG_POLL_32:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100359 *args = api_spci_msg_recv(false, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100360 return true;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100361 case SPCI_RUN_32:
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000362 *args = api_spci_run(spci_vm_id(*args), spci_vcpu_index(*args),
363 current(), next);
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100364 return true;
365 }
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100366
367 return false;
368}
369
370/**
371 * Set or clear VI bit according to pending interrupts.
372 */
373static void update_vi(struct vcpu *next)
374{
375 if (next == NULL) {
376 /*
377 * Not switching vCPUs, set the bit for the current vCPU
378 * directly in the register.
379 */
380 struct vcpu *vcpu = current();
381
382 sl_lock(&vcpu->lock);
383 set_virtual_interrupt_current(
384 vcpu->interrupts.enabled_and_pending_count > 0);
385 sl_unlock(&vcpu->lock);
386 } else {
387 /*
388 * About to switch vCPUs, set the bit for the vCPU to which we
389 * are switching in the saved copy of the register.
390 */
391 sl_lock(&next->lock);
392 set_virtual_interrupt(
393 &next->regs,
394 next->interrupts.enabled_and_pending_count > 0);
395 sl_unlock(&next->lock);
396 }
397}
398
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100399/**
400 * Processes SMC instruction calls.
401 */
Andrew Walbran6606d862019-11-15 16:43:18 +0000402static void smc_handler(struct vcpu *vcpu, struct spci_value *ret,
Andrew Scullce688f02019-09-30 12:54:14 +0100403 struct vcpu **next)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100404{
405 uint32_t func = vcpu->regs.r[0];
406
407 if (psci_handler(vcpu, func, vcpu->regs.r[1], vcpu->regs.r[2],
Andrew Walbran6606d862019-11-15 16:43:18 +0000408 vcpu->regs.r[3], &ret->func, next)) {
Andrew Scullae9962e2019-10-03 16:51:16 +0100409 return;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100410 }
411
Andrew Walbran4579f7002019-08-30 16:24:58 +0100412 if (spci_handler(ret, next)) {
413 update_vi(*next);
414 return;
415 }
416
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100417 switch (func & ~SMCCC_CONVENTION_MASK) {
418 case HF_DEBUG_LOG:
Andrew Walbran6606d862019-11-15 16:43:18 +0000419 ret->func = api_debug_log(vcpu->regs.r[1], vcpu);
Andrew Scullae9962e2019-10-03 16:51:16 +0100420 return;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100421 }
422
Andrew Scullae9962e2019-10-03 16:51:16 +0100423 smc_forwarder(vcpu, ret);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100424}
425
Andrew Walbran59182d52019-09-23 17:55:39 +0100426struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100427{
Andrew Walbran7f920af2019-09-03 17:09:30 +0100428 struct spci_value args = {
429 .func = vcpu->regs.r[0],
430 .arg1 = vcpu->regs.r[1],
431 .arg2 = vcpu->regs.r[2],
432 .arg3 = vcpu->regs.r[3],
433 .arg4 = vcpu->regs.r[4],
434 .arg5 = vcpu->regs.r[5],
435 .arg6 = vcpu->regs.r[6],
436 .arg7 = vcpu->regs.r[7],
437 };
Andrew Walbran59182d52019-09-23 17:55:39 +0100438 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100439
Andrew Walbran7f920af2019-09-03 17:09:30 +0100440 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
441 &vcpu->regs.r[0], &next)) {
Andrew Walbran59182d52019-09-23 17:55:39 +0100442 return next;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100443 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100444
Andrew Walbran7f920af2019-09-03 17:09:30 +0100445 if (spci_handler(&args, &next)) {
446 vcpu->regs.r[0] = args.func;
447 vcpu->regs.r[1] = args.arg1;
448 vcpu->regs.r[2] = args.arg2;
449 vcpu->regs.r[3] = args.arg3;
450 vcpu->regs.r[4] = args.arg4;
451 vcpu->regs.r[5] = args.arg5;
452 vcpu->regs.r[6] = args.arg6;
453 vcpu->regs.r[7] = args.arg7;
Andrew Walbran59182d52019-09-23 17:55:39 +0100454 update_vi(next);
455 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100456 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100457
Andrew Walbran7f920af2019-09-03 17:09:30 +0100458 switch (args.func) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100459 case HF_VM_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100460 vcpu->regs.r[0] = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100461 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100462
463 case HF_VCPU_GET_COUNT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100464 vcpu->regs.r[0] = api_vcpu_get_count(args.arg1, vcpu);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100465 break;
466
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000467 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100468 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000469 break;
470
471 case HF_MAILBOX_WAITER_GET:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100472 vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100473 break;
474
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000475 case HF_INTERRUPT_ENABLE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100476 vcpu->regs.r[0] =
477 api_interrupt_enable(args.arg1, args.arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000478 break;
479
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000480 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100481 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000482 break;
483
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000484 case HF_INTERRUPT_INJECT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100485 vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2,
486 args.arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000487 break;
488
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100489 case HF_DEBUG_LOG:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100490 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100491 break;
492
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100493 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100494 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100495 }
496
Andrew Walbran59182d52019-09-23 17:55:39 +0100497 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000498
Andrew Walbran59182d52019-09-23 17:55:39 +0100499 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100500}
501
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100502struct vcpu *irq_lower(void)
503{
Andrew Scull9726c252019-01-23 13:44:19 +0000504 /*
505 * Switch back to primary VM, interrupts will be handled there.
506 *
507 * If the VM has aborted, this vCPU will be aborted when the scheduler
508 * tries to run it again. This means the interrupt will not be delayed
509 * by the aborted VM.
510 *
511 * TODO: Only switch when the interrupt isn't for the current VM.
512 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000513 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100514}
515
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000516struct vcpu *fiq_lower(void)
517{
518 return irq_lower();
519}
520
521struct vcpu *serr_lower(void)
522{
523 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000524 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000525}
526
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000527/**
528 * Initialises a fault info structure. It assumes that an FnV bit exists at
529 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
530 * the ESR (the fault status code) are 010000; this is the case for both
531 * instruction and data aborts, but not necessarily for other exception reasons.
532 */
533static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100534 const struct vcpu *vcpu,
535 uint32_t mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000536{
537 uint32_t fsc = esr & 0x3f;
538 struct vcpu_fault_info r;
539
540 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000541 r.pc = va_init(vcpu->regs.pc);
542
543 /*
544 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
545 * indicates that we cannot rely on far_el2.
546 */
Andrew Walbrane52006c2019-10-22 18:01:28 +0100547 if (fsc == 0x10 && esr & (1U << 10)) {
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000548 r.vaddr = va_init(0);
549 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
550 } else {
551 r.vaddr = va_init(read_msr(far_el2));
552 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
553 (read_msr(far_el2) & (PAGE_SIZE - 1)));
554 }
555
556 return r;
557}
558
Andrew Scull37402872018-10-24 14:23:06 +0100559struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100560{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100561 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000562 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000563 struct vcpu *new_vcpu;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000564 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100565
Fuad Tabbac76466d2019-09-06 10:42:12 +0100566 switch (ec) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100567 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000568 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100569 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100570 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100571 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000572 /* WFE */
573 /*
574 * TODO: consider giving the scheduler more context,
575 * somehow.
576 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100577 api_yield(vcpu, &new_vcpu);
Jose Marinho135dff32019-02-28 10:25:57 +0000578 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100579 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000580 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000581 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100582
583 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000584 info = fault_info_init(
Andrew Walbrane52006c2019-10-22 18:01:28 +0100585 esr, vcpu, (esr & (1U << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000586 if (vcpu_handle_page_fault(vcpu, &info)) {
587 return NULL;
588 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000589 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100590
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100591 case 0x20: /* EC = 100000, Instruction abort. */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100592 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000593 if (vcpu_handle_page_fault(vcpu, &info)) {
594 return NULL;
595 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000596 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100597
Andrew Walbran59182d52019-09-23 17:55:39 +0100598 case 0x16: /* EC = 010110, HVC instruction */
599 return hvc_handler(vcpu);
600
Andrew Scullc960c032018-10-24 15:13:35 +0100601 case 0x17: /* EC = 010111, SMC instruction. */ {
602 uintreg_t smc_pc = vcpu->regs.pc;
Andrew Scullae9962e2019-10-03 16:51:16 +0100603 struct vcpu *next = NULL;
Andrew Walbran6606d862019-11-15 16:43:18 +0000604 struct spci_value ret = {.arg4 = vcpu->regs.r[4],
605 .arg5 = vcpu->regs.r[5],
606 .arg6 = vcpu->regs.r[6],
607 .arg7 = vcpu->regs.r[7]};
Andrew Scullc960c032018-10-24 15:13:35 +0100608
Andrew Scullae9962e2019-10-03 16:51:16 +0100609 smc_handler(vcpu, &ret, &next);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100610
611 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100612 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Andrew Walbran6606d862019-11-15 16:43:18 +0000613 vcpu->regs.r[0] = ret.func;
614 vcpu->regs.r[1] = ret.arg1;
615 vcpu->regs.r[2] = ret.arg2;
616 vcpu->regs.r[3] = ret.arg3;
617 vcpu->regs.r[4] = ret.arg4;
618 vcpu->regs.r[5] = ret.arg5;
619 vcpu->regs.r[6] = ret.arg6;
620 vcpu->regs.r[7] = ret.arg7;
Andrew Walbran33645652019-04-15 12:29:31 +0100621 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100622 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100623
Fuad Tabbac76466d2019-09-06 10:42:12 +0100624 /*
625 * EC = 011000, MSR, MRS or System instruction execution that is not
626 * reported using EC 000000, 000001 or 000111.
627 */
628 case 0x18:
629 /*
630 * NOTE: This should never be reached because it goes through a
631 * separate path handled by handle_system_register_access().
632 */
633 panic("Handled by handle_system_register_access().");
634
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100635 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100636 dlog("Unknown lower sync exception pc=%#x, esr=%#x, "
637 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100638 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000639 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100640 }
641
Andrew Scull9726c252019-01-23 13:44:19 +0000642 /* The exception wasn't handled so abort the VM. */
643 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100644}
Fuad Tabbac76466d2019-09-06 10:42:12 +0100645
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000646/*
647 * Exception vector offsets.
648 * See Arm Architecture Reference Manual Armv8-A, D1.10.2.
649 */
650
651/**
652 * Offset for synchronous exceptions at current EL with SPx.
653 */
654#define OFFSET_CURRENT_SPX UINT64_C(0x200)
655
656/**
657 * Offset for synchronous exceptions at lower EL using AArch64.
658 */
659#define OFFSET_LOWER_EL_64 UINT64_C(0x400)
660
661/**
662 * Offset for synchronous exceptions at lower EL using AArch32.
663 */
664#define OFFSET_LOWER_EL_32 UINT64_C(0x600)
665
666/**
667 * Returns the address for the exception handler at EL1.
668 */
669static uintreg_t get_el1_exception_handler_addr(const struct vcpu *vcpu)
670{
671 uintreg_t base_addr = read_msr(vbar_el1);
672 uintreg_t pe_mode = vcpu->regs.spsr & PSR_PE_MODE_MASK;
673 bool is_arch32 = vcpu->regs.spsr & PSR_ARCH_MODE_32;
674
675 if (pe_mode == PSR_PE_MODE_EL0T) {
676 if (is_arch32) {
677 base_addr += OFFSET_LOWER_EL_32;
678 } else {
679 base_addr += OFFSET_LOWER_EL_64;
680 }
681 } else {
682 CHECK(!is_arch32);
683 base_addr += OFFSET_CURRENT_SPX;
684 }
685
686 return base_addr;
687}
688
689/**
690 * Injects an exception with an unknown reason (EC=0x0) to the EL1.
691 * See Arm Architecture Reference Manual Armv8-A, page D13-2924.
692 *
693 * NOTE: This function assumes that the lazy registers haven't been saved, and
694 * writes to the lazy registers of the CPU directly instead of the vCPU.
695 */
696static struct vcpu *inject_el1_unknown_exception(struct vcpu *vcpu,
697 uintreg_t esr_el2)
698{
699 uintreg_t esr_el1_value = GET_ESR_IL(esr_el2);
700 uintreg_t handler_address = get_el1_exception_handler_addr(vcpu);
701 char *direction_str;
702
703 /* Update the CPU state to inject the exception. */
704 write_msr(esr_el1, esr_el1_value);
705 write_msr(elr_el1, vcpu->regs.pc);
706 write_msr(spsr_el1, vcpu->regs.spsr);
707
708 /*
709 * Mask (disable) interrupts and run in EL1h mode.
710 * EL1h mode is used because by default, taking an exception selects the
711 * stack pointer for the target Exception level. The software can change
712 * that later in the handler if needed.
713 * See Arm Architecture Reference Manual Armv8-A, page D13-2924
714 */
715 vcpu->regs.spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
716
717 /* Transfer control to the exception hander. */
718 vcpu->regs.pc = handler_address;
719
720 direction_str = ISS_IS_READ(esr_el2) ? "read" : "write";
721 dlog("Trapped access to system register %s: op0=%d, op1=%d, crn=%d, "
722 "crm=%d, op2=%d, rt=%d.\n",
723 direction_str, GET_ISS_OP0(esr_el2), GET_ISS_OP1(esr_el2),
724 GET_ISS_CRN(esr_el2), GET_ISS_CRM(esr_el2), GET_ISS_OP2(esr_el2),
725 GET_ISS_RT(esr_el2));
726
727 dlog("Injecting Unknown Reason exception into VM%d.\n", vcpu->vm->id);
728 dlog("Exception handler address 0x%x\n", handler_address);
729
730 /* Schedule the same VM to continue running. */
731 return NULL;
732}
733
Fuad Tabbac76466d2019-09-06 10:42:12 +0100734/**
735 * Handles EC = 011000, msr, mrs instruction traps.
736 * Returns non-null ONLY if the access failed and the vcpu is changing.
737 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000738struct vcpu *handle_system_register_access(uintreg_t esr_el2)
Fuad Tabbac76466d2019-09-06 10:42:12 +0100739{
740 struct vcpu *vcpu = current();
741 spci_vm_id_t vm_id = vcpu->vm->id;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000742 uintreg_t ec = GET_ESR_EC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100743
744 CHECK(ec == 0x18);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100745 /*
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100746 * Handle accesses to debug and performance monitor registers.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000747 * Inject an exception for unhandled/unsupported registers.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100748 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000749 if (debug_el1_is_register_access(esr_el2)) {
750 if (!debug_el1_process_access(vcpu, vm_id, esr_el2)) {
751 return inject_el1_unknown_exception(vcpu, esr_el2);
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100752 }
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000753 } else if (perfmon_is_register_access(esr_el2)) {
754 if (!perfmon_process_access(vcpu, vm_id, esr_el2)) {
755 return inject_el1_unknown_exception(vcpu, esr_el2);
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100756 }
Fuad Tabba77a4b012019-11-15 12:13:08 +0000757 } else if (feature_id_is_register_access(esr_el2)) {
758 if (!feature_id_process_access(vcpu, esr_el2)) {
759 return inject_el1_unknown_exception(vcpu, esr_el2);
760 }
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100761 } else {
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000762 return inject_el1_unknown_exception(vcpu, esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100763 }
764
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100765 /* Instruction was fulfilled. Skip it and run the next one. */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000766 vcpu->regs.pc += GET_NEXT_PC_INC(esr_el2);
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100767 return NULL;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100768}