blob: 53ae4d055a6a118bdebe43651b2c09ced1b2ebbd [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 Scull07b6bd32019-12-12 17:19:55 +000022#include "hf/arch/plat/smc.h"
Andrew Scullc960c032018-10-24 15:13:35 +010023
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/api.h"
Fuad Tabbac76466d2019-09-06 10:42:12 +010025#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010026#include "hf/cpu.h"
27#include "hf/dlog.h"
Andrew Sculla9c172d2019-04-03 14:10:00 +010028#include "hf/panic.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000029#include "hf/spci.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010030#include "hf/vm.h"
31
Andrew Scullf35a5c92018-08-07 18:09:46 +010032#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010033
Fuad Tabbac76466d2019-09-06 10:42:12 +010034#include "debug_el1.h"
Fuad Tabba77a4b012019-11-15 12:13:08 +000035#include "feature_id.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010036#include "msr.h"
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +010037#include "perfmon.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010038#include "psci.h"
Andrew Walbran33645652019-04-15 12:29:31 +010039#include "psci_handler.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000040#include "smc.h"
Fuad Tabbaba8c44d2019-09-23 14:38:58 +010041#include "sysregs.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010042
Fuad Tabbac76466d2019-09-06 10:42:12 +010043/**
44 * Gets the Exception Class from the ESR.
45 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +000046#define GET_ESR_EC(esr) ((esr) >> 26)
47
48/**
49 * Gets the Instruction Length bit for the synchronous exception
50 */
51#define GET_ESR_IL(esr) ((esr) & (1 << 25))
Fuad Tabbac76466d2019-09-06 10:42:12 +010052
53/**
54 * Gets the value to increment for the next PC.
55 * The ESR encodes whether the instruction is 2 bytes or 4 bytes long.
56 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +000057#define GET_NEXT_PC_INC(esr) (GET_ESR_IL(esr) ? 4 : 2)
Fuad Tabbac76466d2019-09-06 10:42:12 +010058
Fuad Tabbac76466d2019-09-06 10:42:12 +010059/**
Andrew Walbran0dd67ff2019-09-12 16:38:50 +010060 * The Client ID field within X7 for an SMC64 call.
61 */
62#define CLIENT_ID_MASK UINT64_C(0xffff)
63
64/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010065 * Returns a reference to the currently executing vCPU.
66 */
Andrew Scullc960c032018-10-24 15:13:35 +010067static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000068{
69 return (struct vcpu *)read_msr(tpidr_el2);
70}
71
Andrew Walbran1f8d4872018-12-20 11:21:32 +000072/**
73 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
74 * informs the arch-independent sections that registers have been saved.
75 */
76void complete_saving_state(struct vcpu *vcpu)
77{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010078 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
79 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000080
81 api_regs_state_saved(vcpu);
82
83 /*
84 * If switching away from the primary, copy the current EL0 virtual
85 * timer registers to the corresponding EL2 physical timer registers.
86 * This is used to emulate the virtual timer for the primary in case it
87 * should fire while the secondary is running.
88 */
89 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
90 /*
91 * Clear timer control register before copying compare value, to
92 * avoid a spurious timer interrupt. This could be a problem if
93 * the interrupt is configured as edge-triggered, as it would
94 * then be latched in.
95 */
96 write_msr(cnthp_ctl_el2, 0);
97 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
98 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
99 }
100}
101
102/**
103 * Restores the state of per-vCPU peripherals, such as the virtual timer.
104 */
105void begin_restoring_state(struct vcpu *vcpu)
106{
107 /*
108 * Clear timer control register before restoring compare value, to avoid
109 * a spurious timer interrupt. This could be a problem if the interrupt
110 * is configured as edge-triggered, as it would then be latched in.
111 */
112 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100113 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
114 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000115
116 /*
117 * If we are switching (back) to the primary, disable the EL2 physical
118 * timer which was being used to emulate the EL0 virtual timer, as the
119 * virtual timer is now running for the primary again.
120 */
121 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
122 write_msr(cnthp_ctl_el2, 0);
123 write_msr(cnthp_cval_el2, 0);
124 }
125}
126
Andrew Walbran1f32e722019-06-07 17:57:26 +0100127/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100128 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
129 * current VMID.
130 */
131static void invalidate_vm_tlb(void)
132{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100133 /*
134 * Ensure that the last VTTBR write has taken effect so we invalidate
135 * the right set of TLB entries.
136 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100137 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100138
Andrew Walbran1f32e722019-06-07 17:57:26 +0100139 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100140
141 /*
142 * Ensure that no instructions are fetched for the VM until after the
143 * TLB invalidation has taken effect.
144 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100145 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100146
147 /*
148 * Ensure that no data reads or writes for the VM happen until after the
Fuad Tabba77a4b012019-11-15 12:13:08 +0000149 * TLB invalidation has taken effect. Non-shareable is enough because
150 * the TLB is local to the CPU.
Andrew Walbrancff1f682019-07-04 14:52:45 +0100151 */
David Brazdil851948e2019-08-09 12:02:12 +0100152 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100153}
154
155/**
156 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
157 * the same VM which was run on the current pCPU.
158 *
159 * This is necessary because VMs may (contrary to the architecture
160 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
161 * workaround:
162 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
163 */
164void maybe_invalidate_tlb(struct vcpu *vcpu)
165{
166 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100167 spci_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100168
169 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
170 new_vcpu_index) {
171 /*
172 * The vCPU has changed since the last time this VM was run on
173 * this pCPU, so we need to invalidate the TLB.
174 */
175 invalidate_vm_tlb();
176
177 /* Record the fact that this vCPU is now running on this CPU. */
178 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
179 new_vcpu_index;
180 }
181}
182
Andrew Scullc960c032018-10-24 15:13:35 +0100183noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100184{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000185 (void)elr;
186 (void)spsr;
187
Andrew Sculla9c172d2019-04-03 14:10:00 +0100188 panic("IRQ from current");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100189}
190
Andrew Scullc960c032018-10-24 15:13:35 +0100191noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100192{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000193 (void)elr;
194 (void)spsr;
195
Andrew Sculla9c172d2019-04-03 14:10:00 +0100196 panic("FIQ from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000197}
198
Andrew Scullc960c032018-10-24 15:13:35 +0100199noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000200{
201 (void)elr;
202 (void)spsr;
203
Andrew Sculla9c172d2019-04-03 14:10:00 +0100204 panic("SERR from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000205}
206
Andrew Scullc960c032018-10-24 15:13:35 +0100207noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000208{
209 uintreg_t esr = read_msr(esr_el2);
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000210 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000211
212 (void)spsr;
213
Fuad Tabbac76466d2019-09-06 10:42:12 +0100214 switch (ec) {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100215 case 0x25: /* EC = 100101, Data abort. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100216 dlog("Data abort: pc=%#x, esr=%#x, ec=%#x", elr, esr, ec);
Andrew Walbrane52006c2019-10-22 18:01:28 +0100217 if (!(esr & (1U << 10))) { /* Check FnV bit. */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100218 dlog(", far=%#x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100219 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100220 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100221 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100222
223 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000224 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100225
226 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100227 dlog("Unknown current sync exception pc=%#x, esr=%#x, "
228 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100229 elr, esr, ec);
Andrew Scullc960c032018-10-24 15:13:35 +0100230 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100231 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000232
Andrew Sculla9c172d2019-04-03 14:10:00 +0100233 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100234}
235
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100236/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000237 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
238 * arch_regs.
239 */
240static void set_virtual_interrupt(struct arch_regs *r, bool enable)
241{
242 if (enable) {
243 r->lazy.hcr_el2 |= HCR_EL2_VI;
244 } else {
245 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
246 }
247}
248
249/**
250 * Sets or clears the VI bit in the HCR_EL2 register.
251 */
252static void set_virtual_interrupt_current(bool enable)
253{
254 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000255
Andrew Walbran3d84a262018-12-13 14:41:19 +0000256 if (enable) {
257 hcr_el2 |= HCR_EL2_VI;
258 } else {
259 hcr_el2 &= ~HCR_EL2_VI;
260 }
261 write_msr(hcr_el2, hcr_el2);
262}
263
Andrew Scullae9962e2019-10-03 16:51:16 +0100264/**
265 * Checks whether to block an SMC being forwarded from a VM.
266 */
267static bool smc_is_blocked(const struct vm *vm, uint32_t func)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100268{
Andrew Scullae9962e2019-10-03 16:51:16 +0100269 bool block_by_default = !vm->smc_whitelist.permissive;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100270
Andrew Scullae9962e2019-10-03 16:51:16 +0100271 for (size_t i = 0; i < vm->smc_whitelist.smc_count; ++i) {
272 if (func == vm->smc_whitelist.smcs[i]) {
273 return false;
274 }
275 }
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100276
Andrew Scullae9962e2019-10-03 16:51:16 +0100277 dlog("SMC %#010x attempted from VM %d, blocked=%d\n", func, vm->id,
278 block_by_default);
279
280 /* Access is still allowed in permissive mode. */
281 return block_by_default;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100282}
283
284/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100285 * Applies SMC access control according to manifest and forwards the call if
286 * access is granted.
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100287 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000288static void smc_forwarder(const struct vm *vm, struct spci_value *args)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100289{
Andrew Scull07b6bd32019-12-12 17:19:55 +0000290 struct spci_value ret;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000291 uint32_t client_id = vm->id;
292 uintreg_t arg7 = args->arg7;
Andrew Scullae9962e2019-10-03 16:51:16 +0100293
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000294 if (smc_is_blocked(vm, args->func)) {
295 args->func = SMCCC_ERROR_UNKNOWN;
Andrew Scullae9962e2019-10-03 16:51:16 +0100296 return;
297 }
298
Andrew Walbran0dd67ff2019-09-12 16:38:50 +0100299 /*
300 * Set the Client ID but keep the existing Secure OS ID and anything
301 * else (currently unspecified) that the client may have passed in the
302 * upper bits.
303 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000304 args->arg7 = client_id | (arg7 & ~CLIENT_ID_MASK);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000305 ret = smc_forward(args->func, args->arg1, args->arg2, args->arg3,
306 args->arg4, args->arg5, args->arg6, args->arg7);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100307
Andrew Scullae9962e2019-10-03 16:51:16 +0100308 /*
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000309 * Preserve the value passed by the caller, rather than the generated
310 * client_id. Note that this would also overwrite any return value that
Andrew Scullae9962e2019-10-03 16:51:16 +0100311 * 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 Scull07b6bd32019-12-12 17:19:55 +0000314 ret.arg7 = arg7;
315
316 plat_smc_post_forward(*args, &ret);
317
318 *args = ret;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100319}
320
Andrew Walbran7f920af2019-09-03 17:09:30 +0100321static bool spci_handler(struct spci_value *args, struct vcpu **next)
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100322{
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100323 /*
324 * NOTE: When adding new methods to this handler update
325 * api_spci_features accordingly.
326 */
Andrew Walbran7f920af2019-09-03 17:09:30 +0100327 switch (args->func & ~SMCCC_CONVENTION_MASK) {
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100328 case SPCI_VERSION_32:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100329 *args = api_spci_version();
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100330 return true;
Andrew Walbrand230f662019-10-07 18:03:36 +0100331 case SPCI_ID_GET_32:
332 *args = api_spci_id_get(current());
333 return true;
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100334 case SPCI_FEATURES_32:
335 *args = api_spci_features(args->arg1);
336 return true;
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +0000337 case SPCI_RX_RELEASE_32:
338 *args = api_spci_rx_release(current(), next);
339 return true;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000340 case SPCI_RXTX_MAP_32:
341 *args = api_spci_rxtx_map(ipa_init(args->arg1),
342 ipa_init(args->arg2), args->arg3,
343 current(), next);
344 return true;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100345 case SPCI_YIELD_32:
Andrew Walbran16075b62019-09-03 17:11:07 +0100346 api_yield(current(), next);
347
348 /* SPCI_YIELD always returns SPCI_SUCCESS. */
349 *args = (struct spci_value){.func = SPCI_SUCCESS_32};
350
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100351 return true;
352 case SPCI_MSG_SEND_32:
Andrew Walbran70bc8622019-10-07 14:15:58 +0100353 *args = api_spci_msg_send(spci_msg_send_sender(*args),
354 spci_msg_send_receiver(*args),
355 spci_msg_send_size(*args),
356 spci_msg_send_attributes(*args),
357 current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100358 return true;
Andrew Walbran0de4f162019-09-03 16:44:20 +0100359 case SPCI_MSG_WAIT_32:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100360 *args = api_spci_msg_recv(true, current(), next);
Andrew Walbran0de4f162019-09-03 16:44:20 +0100361 return true;
362 case SPCI_MSG_POLL_32:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100363 *args = api_spci_msg_recv(false, current(), next);
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100364 return true;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100365 case SPCI_RUN_32:
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000366 *args = api_spci_run(spci_vm_id(*args), spci_vcpu_index(*args),
367 current(), next);
Andrew Walbranf0c314d2019-10-02 14:24:26 +0100368 return true;
369 }
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100370
371 return false;
372}
373
374/**
375 * Set or clear VI bit according to pending interrupts.
376 */
377static void update_vi(struct vcpu *next)
378{
379 if (next == NULL) {
380 /*
381 * Not switching vCPUs, set the bit for the current vCPU
382 * directly in the register.
383 */
384 struct vcpu *vcpu = current();
385
386 sl_lock(&vcpu->lock);
387 set_virtual_interrupt_current(
388 vcpu->interrupts.enabled_and_pending_count > 0);
389 sl_unlock(&vcpu->lock);
390 } else {
391 /*
392 * About to switch vCPUs, set the bit for the vCPU to which we
393 * are switching in the saved copy of the register.
394 */
395 sl_lock(&next->lock);
396 set_virtual_interrupt(
397 &next->regs,
398 next->interrupts.enabled_and_pending_count > 0);
399 sl_unlock(&next->lock);
400 }
401}
402
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100403/**
404 * Processes SMC instruction calls.
405 */
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000406static struct vcpu *smc_handler(struct vcpu *vcpu)
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100407{
Andrew Walbran85c37662019-12-05 16:29:33 +0000408 struct spci_value args = {
409 .func = vcpu->regs.r[0],
410 .arg1 = vcpu->regs.r[1],
411 .arg2 = vcpu->regs.r[2],
412 .arg3 = vcpu->regs.r[3],
413 .arg4 = vcpu->regs.r[4],
414 .arg5 = vcpu->regs.r[5],
415 .arg6 = vcpu->regs.r[6],
416 .arg7 = vcpu->regs.r[7],
417 };
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000418 struct vcpu *next = NULL;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100419
Andrew Walbran85c37662019-12-05 16:29:33 +0000420 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000421 &vcpu->regs.r[0], &next)) {
422 return next;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100423 }
424
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000425 if (spci_handler(&args, &next)) {
426 arch_regs_set_retval(&vcpu->regs, args);
427 update_vi(next);
428 return next;
Andrew Walbran4579f7002019-08-30 16:24:58 +0100429 }
430
Andrew Walbran85c37662019-12-05 16:29:33 +0000431 switch (args.func & ~SMCCC_CONVENTION_MASK) {
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100432 case HF_DEBUG_LOG:
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000433 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000434 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100435 }
436
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000437 smc_forwarder(vcpu->vm, &args);
438 arch_regs_set_retval(&vcpu->regs, args);
Andrew Scull07b6bd32019-12-12 17:19:55 +0000439 return NULL;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100440}
441
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000442/*
443 * Exception vector offsets.
444 * See Arm Architecture Reference Manual Armv8-A, D1.10.2.
445 */
446
447/**
448 * Offset for synchronous exceptions at current EL with SPx.
449 */
450#define OFFSET_CURRENT_SPX UINT64_C(0x200)
451
452/**
453 * Offset for synchronous exceptions at lower EL using AArch64.
454 */
455#define OFFSET_LOWER_EL_64 UINT64_C(0x400)
456
457/**
458 * Offset for synchronous exceptions at lower EL using AArch32.
459 */
460#define OFFSET_LOWER_EL_32 UINT64_C(0x600)
461
462/**
463 * Returns the address for the exception handler at EL1.
464 */
465static uintreg_t get_el1_exception_handler_addr(const struct vcpu *vcpu)
466{
467 uintreg_t base_addr = read_msr(vbar_el1);
468 uintreg_t pe_mode = vcpu->regs.spsr & PSR_PE_MODE_MASK;
469 bool is_arch32 = vcpu->regs.spsr & PSR_ARCH_MODE_32;
470
471 if (pe_mode == PSR_PE_MODE_EL0T) {
472 if (is_arch32) {
473 base_addr += OFFSET_LOWER_EL_32;
474 } else {
475 base_addr += OFFSET_LOWER_EL_64;
476 }
477 } else {
478 CHECK(!is_arch32);
479 base_addr += OFFSET_CURRENT_SPX;
480 }
481
482 return base_addr;
483}
484
485/**
486 * Injects an exception with an unknown reason (EC=0x0) to the EL1.
487 * See Arm Architecture Reference Manual Armv8-A, page D13-2924.
488 *
489 * NOTE: This function assumes that the lazy registers haven't been saved, and
490 * writes to the lazy registers of the CPU directly instead of the vCPU.
491 */
492static struct vcpu *inject_el1_unknown_exception(struct vcpu *vcpu,
493 uintreg_t esr_el2)
494{
495 uintreg_t esr_el1_value = GET_ESR_IL(esr_el2);
496 uintreg_t handler_address = get_el1_exception_handler_addr(vcpu);
497 char *direction_str;
498
499 /* Update the CPU state to inject the exception. */
500 write_msr(esr_el1, esr_el1_value);
501 write_msr(elr_el1, vcpu->regs.pc);
502 write_msr(spsr_el1, vcpu->regs.spsr);
503
504 /*
505 * Mask (disable) interrupts and run in EL1h mode.
506 * EL1h mode is used because by default, taking an exception selects the
507 * stack pointer for the target Exception level. The software can change
508 * that later in the handler if needed.
509 * See Arm Architecture Reference Manual Armv8-A, page D13-2924
510 */
511 vcpu->regs.spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H;
512
513 /* Transfer control to the exception hander. */
514 vcpu->regs.pc = handler_address;
515
516 direction_str = ISS_IS_READ(esr_el2) ? "read" : "write";
517 dlog("Trapped access to system register %s: op0=%d, op1=%d, crn=%d, "
518 "crm=%d, op2=%d, rt=%d.\n",
519 direction_str, GET_ISS_OP0(esr_el2), GET_ISS_OP1(esr_el2),
520 GET_ISS_CRN(esr_el2), GET_ISS_CRM(esr_el2), GET_ISS_OP2(esr_el2),
521 GET_ISS_RT(esr_el2));
522
523 dlog("Injecting Unknown Reason exception into VM%d.\n", vcpu->vm->id);
524 dlog("Exception handler address 0x%x\n", handler_address);
525
526 /* Schedule the same VM to continue running. */
527 return NULL;
528}
529
Andrew Walbran59182d52019-09-23 17:55:39 +0100530struct vcpu *hvc_handler(struct vcpu *vcpu)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100531{
Andrew Walbran7f920af2019-09-03 17:09:30 +0100532 struct spci_value args = {
533 .func = vcpu->regs.r[0],
534 .arg1 = vcpu->regs.r[1],
535 .arg2 = vcpu->regs.r[2],
536 .arg3 = vcpu->regs.r[3],
537 .arg4 = vcpu->regs.r[4],
538 .arg5 = vcpu->regs.r[5],
539 .arg6 = vcpu->regs.r[6],
540 .arg7 = vcpu->regs.r[7],
541 };
Andrew Walbran59182d52019-09-23 17:55:39 +0100542 struct vcpu *next = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100543
Andrew Walbran7f920af2019-09-03 17:09:30 +0100544 if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3,
545 &vcpu->regs.r[0], &next)) {
Andrew Walbran59182d52019-09-23 17:55:39 +0100546 return next;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100547 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100548
Andrew Walbran7f920af2019-09-03 17:09:30 +0100549 if (spci_handler(&args, &next)) {
Andrew Walbran6f56d7b2019-12-05 16:27:34 +0000550 arch_regs_set_retval(&vcpu->regs, args);
Andrew Walbran59182d52019-09-23 17:55:39 +0100551 update_vi(next);
552 return next;
Andrew Walbran7d28d9a2019-08-30 16:24:58 +0100553 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100554
Andrew Walbran7f920af2019-09-03 17:09:30 +0100555 switch (args.func) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100556 case HF_VM_GET_COUNT:
Andrew Walbran59182d52019-09-23 17:55:39 +0100557 vcpu->regs.r[0] = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100558 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100559
560 case HF_VCPU_GET_COUNT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100561 vcpu->regs.r[0] = api_vcpu_get_count(args.arg1, vcpu);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100562 break;
563
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000564 case HF_MAILBOX_WRITABLE_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100565 vcpu->regs.r[0] = api_mailbox_writable_get(vcpu);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000566 break;
567
568 case HF_MAILBOX_WAITER_GET:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100569 vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100570 break;
571
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000572 case HF_INTERRUPT_ENABLE:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100573 vcpu->regs.r[0] =
574 api_interrupt_enable(args.arg1, args.arg2, vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000575 break;
576
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000577 case HF_INTERRUPT_GET:
Andrew Walbran59182d52019-09-23 17:55:39 +0100578 vcpu->regs.r[0] = api_interrupt_get(vcpu);
Andrew Walbran318f5732018-11-20 16:23:42 +0000579 break;
580
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000581 case HF_INTERRUPT_INJECT:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100582 vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2,
583 args.arg3, vcpu, &next);
Andrew Walbran318f5732018-11-20 16:23:42 +0000584 break;
585
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100586 case HF_DEBUG_LOG:
Andrew Walbran7f920af2019-09-03 17:09:30 +0100587 vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100588 break;
589
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100590 default:
Andrew Walbran59182d52019-09-23 17:55:39 +0100591 vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100592 }
593
Andrew Walbran59182d52019-09-23 17:55:39 +0100594 update_vi(next);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000595
Andrew Walbran59182d52019-09-23 17:55:39 +0100596 return next;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100597}
598
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100599struct vcpu *irq_lower(void)
600{
Andrew Scull9726c252019-01-23 13:44:19 +0000601 /*
602 * Switch back to primary VM, interrupts will be handled there.
603 *
604 * If the VM has aborted, this vCPU will be aborted when the scheduler
605 * tries to run it again. This means the interrupt will not be delayed
606 * by the aborted VM.
607 *
608 * TODO: Only switch when the interrupt isn't for the current VM.
609 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000610 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100611}
612
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000613struct vcpu *fiq_lower(void)
614{
615 return irq_lower();
616}
617
618struct vcpu *serr_lower(void)
619{
620 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000621 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000622}
623
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000624/**
625 * Initialises a fault info structure. It assumes that an FnV bit exists at
626 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
627 * the ESR (the fault status code) are 010000; this is the case for both
628 * instruction and data aborts, but not necessarily for other exception reasons.
629 */
630static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100631 const struct vcpu *vcpu,
632 uint32_t mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000633{
634 uint32_t fsc = esr & 0x3f;
635 struct vcpu_fault_info r;
636
637 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000638 r.pc = va_init(vcpu->regs.pc);
639
640 /*
641 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
642 * indicates that we cannot rely on far_el2.
643 */
Andrew Walbrane52006c2019-10-22 18:01:28 +0100644 if (fsc == 0x10 && esr & (1U << 10)) {
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000645 r.vaddr = va_init(0);
646 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
647 } else {
648 r.vaddr = va_init(read_msr(far_el2));
649 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
650 (read_msr(far_el2) & (PAGE_SIZE - 1)));
651 }
652
653 return r;
654}
655
Andrew Scull37402872018-10-24 14:23:06 +0100656struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100657{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100658 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000659 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000660 struct vcpu *new_vcpu;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000661 uintreg_t ec = GET_ESR_EC(esr);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100662
Fuad Tabbac76466d2019-09-06 10:42:12 +0100663 switch (ec) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100664 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000665 /* Skip the instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100666 vcpu->regs.pc += GET_NEXT_PC_INC(esr);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100667 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100668 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000669 /* WFE */
670 /*
671 * TODO: consider giving the scheduler more context,
672 * somehow.
673 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100674 api_yield(vcpu, &new_vcpu);
Jose Marinho135dff32019-02-28 10:25:57 +0000675 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100676 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000677 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000678 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100679
680 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000681 info = fault_info_init(
Andrew Walbrane52006c2019-10-22 18:01:28 +0100682 esr, vcpu, (esr & (1U << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000683 if (vcpu_handle_page_fault(vcpu, &info)) {
684 return NULL;
685 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000686 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100687
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100688 case 0x20: /* EC = 100000, Instruction abort. */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100689 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000690 if (vcpu_handle_page_fault(vcpu, &info)) {
691 return NULL;
692 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000693 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100694
Andrew Walbran59182d52019-09-23 17:55:39 +0100695 case 0x16: /* EC = 010110, HVC instruction */
696 return hvc_handler(vcpu);
697
Andrew Scullc960c032018-10-24 15:13:35 +0100698 case 0x17: /* EC = 010111, SMC instruction. */ {
699 uintreg_t smc_pc = vcpu->regs.pc;
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000700 struct vcpu *next = smc_handler(vcpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100701
702 /* Skip the SMC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100703 vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr);
Andrew Walbran9dadaf22019-12-05 16:50:55 +0000704
Andrew Walbran33645652019-04-15 12:29:31 +0100705 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100706 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100707
Fuad Tabbac76466d2019-09-06 10:42:12 +0100708 /*
709 * EC = 011000, MSR, MRS or System instruction execution that is not
710 * reported using EC 000000, 000001 or 000111.
711 */
712 case 0x18:
713 /*
714 * NOTE: This should never be reached because it goes through a
715 * separate path handled by handle_system_register_access().
716 */
717 panic("Handled by handle_system_register_access().");
718
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100719 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100720 dlog("Unknown lower sync exception pc=%#x, esr=%#x, "
721 "ec=%#x\n",
Fuad Tabbac76466d2019-09-06 10:42:12 +0100722 vcpu->regs.pc, esr, ec);
Andrew Scull9726c252019-01-23 13:44:19 +0000723 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100724 }
725
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000726 /*
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000727 * The exception wasn't handled. Inject to the VM to give it chance to
728 * handle as an unknown exception.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000729 */
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000730 return inject_el1_unknown_exception(vcpu, esr);
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000731}
732
Fuad Tabbac76466d2019-09-06 10:42:12 +0100733/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000734 * Handles EC = 011000, MSR, MRS instruction traps.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100735 * Returns non-null ONLY if the access failed and the vcpu is changing.
736 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000737struct vcpu *handle_system_register_access(uintreg_t esr_el2)
Fuad Tabbac76466d2019-09-06 10:42:12 +0100738{
739 struct vcpu *vcpu = current();
740 spci_vm_id_t vm_id = vcpu->vm->id;
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000741 uintreg_t ec = GET_ESR_EC(esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100742
743 CHECK(ec == 0x18);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100744 /*
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100745 * Handle accesses to debug and performance monitor registers.
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000746 * Inject an exception for unhandled/unsupported registers.
Fuad Tabbac76466d2019-09-06 10:42:12 +0100747 */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000748 if (debug_el1_is_register_access(esr_el2)) {
749 if (!debug_el1_process_access(vcpu, vm_id, esr_el2)) {
750 return inject_el1_unknown_exception(vcpu, esr_el2);
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100751 }
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000752 } else if (perfmon_is_register_access(esr_el2)) {
753 if (!perfmon_process_access(vcpu, vm_id, esr_el2)) {
754 return inject_el1_unknown_exception(vcpu, esr_el2);
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100755 }
Fuad Tabba77a4b012019-11-15 12:13:08 +0000756 } else if (feature_id_is_register_access(esr_el2)) {
757 if (!feature_id_process_access(vcpu, esr_el2)) {
758 return inject_el1_unknown_exception(vcpu, esr_el2);
759 }
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100760 } else {
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000761 return inject_el1_unknown_exception(vcpu, esr_el2);
Fuad Tabbac76466d2019-09-06 10:42:12 +0100762 }
763
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100764 /* Instruction was fulfilled. Skip it and run the next one. */
Fuad Tabba3e9b0222019-11-11 16:47:50 +0000765 vcpu->regs.pc += GET_NEXT_PC_INC(esr_el2);
Fuad Tabbaf1d6dc52019-09-18 17:33:14 +0100766 return NULL;
Fuad Tabbac76466d2019-09-06 10:42:12 +0100767}