blob: 4f4ec4404415e59c0ad45eb85df830c71dc53653 [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"
24#include "hf/cpu.h"
25#include "hf/dlog.h"
Andrew Sculla9c172d2019-04-03 14:10:00 +010026#include "hf/panic.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000027#include "hf/spci.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010028#include "hf/vm.h"
29
Andrew Scullf35a5c92018-08-07 18:09:46 +010030#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010031
32#include "msr.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010033#include "psci.h"
Andrew Walbran33645652019-04-15 12:29:31 +010034#include "psci_handler.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000035#include "smc.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010036
Andrew Walbran3d84a262018-12-13 14:41:19 +000037#define HCR_EL2_VI (1u << 7)
38
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010039struct hvc_handler_return {
Andrew Scull37402872018-10-24 14:23:06 +010040 uintreg_t user_ret;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010041 struct vcpu *new;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010042};
43
Andrew Scullc960c032018-10-24 15:13:35 +010044/* Gets a reference to the currently executing vCPU. */
45static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000046{
47 return (struct vcpu *)read_msr(tpidr_el2);
48}
49
Andrew Walbran1f8d4872018-12-20 11:21:32 +000050/**
51 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
52 * informs the arch-independent sections that registers have been saved.
53 */
54void complete_saving_state(struct vcpu *vcpu)
55{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010056 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
57 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000058
59 api_regs_state_saved(vcpu);
60
61 /*
62 * If switching away from the primary, copy the current EL0 virtual
63 * timer registers to the corresponding EL2 physical timer registers.
64 * This is used to emulate the virtual timer for the primary in case it
65 * should fire while the secondary is running.
66 */
67 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
68 /*
69 * Clear timer control register before copying compare value, to
70 * avoid a spurious timer interrupt. This could be a problem if
71 * the interrupt is configured as edge-triggered, as it would
72 * then be latched in.
73 */
74 write_msr(cnthp_ctl_el2, 0);
75 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
76 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
77 }
78}
79
80/**
81 * Restores the state of per-vCPU peripherals, such as the virtual timer.
82 */
83void begin_restoring_state(struct vcpu *vcpu)
84{
85 /*
86 * Clear timer control register before restoring compare value, to avoid
87 * a spurious timer interrupt. This could be a problem if the interrupt
88 * is configured as edge-triggered, as it would then be latched in.
89 */
90 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +010091 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
92 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000093
94 /*
95 * If we are switching (back) to the primary, disable the EL2 physical
96 * timer which was being used to emulate the EL0 virtual timer, as the
97 * virtual timer is now running for the primary again.
98 */
99 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
100 write_msr(cnthp_ctl_el2, 0);
101 write_msr(cnthp_cval_el2, 0);
102 }
103}
104
Andrew Walbran1f32e722019-06-07 17:57:26 +0100105/**
Andrew Walbran1f32e722019-06-07 17:57:26 +0100106 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
107 * current VMID.
108 */
109static void invalidate_vm_tlb(void)
110{
Andrew Walbrancff1f682019-07-04 14:52:45 +0100111 /*
112 * Ensure that the last VTTBR write has taken effect so we invalidate
113 * the right set of TLB entries.
114 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100115 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100116
Andrew Walbran1f32e722019-06-07 17:57:26 +0100117 __asm__ volatile("tlbi vmalle1");
Andrew Walbrancff1f682019-07-04 14:52:45 +0100118
119 /*
120 * Ensure that no instructions are fetched for the VM until after the
121 * TLB invalidation has taken effect.
122 */
Andrew Walbran1f32e722019-06-07 17:57:26 +0100123 isb();
Andrew Walbrancff1f682019-07-04 14:52:45 +0100124
125 /*
126 * Ensure that no data reads or writes for the VM happen until after the
127 * TLB invalidation has taken effect. Non-sharable is enough because the
128 * TLB is local to the CPU.
129 */
David Brazdil851948e2019-08-09 12:02:12 +0100130 dsb(nsh);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100131}
132
133/**
134 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
135 * the same VM which was run on the current pCPU.
136 *
137 * This is necessary because VMs may (contrary to the architecture
138 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
139 * workaround:
140 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
141 */
142void maybe_invalidate_tlb(struct vcpu *vcpu)
143{
144 size_t current_cpu_index = cpu_index(vcpu->cpu);
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100145 spci_vcpu_index_t new_vcpu_index = vcpu_index(vcpu);
Andrew Walbran1f32e722019-06-07 17:57:26 +0100146
147 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
148 new_vcpu_index) {
149 /*
150 * The vCPU has changed since the last time this VM was run on
151 * this pCPU, so we need to invalidate the TLB.
152 */
153 invalidate_vm_tlb();
154
155 /* Record the fact that this vCPU is now running on this CPU. */
156 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
157 new_vcpu_index;
158 }
159}
160
Andrew Scullc960c032018-10-24 15:13:35 +0100161noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100162{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000163 (void)elr;
164 (void)spsr;
165
Andrew Sculla9c172d2019-04-03 14:10:00 +0100166 panic("IRQ from current");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100167}
168
Andrew Scullc960c032018-10-24 15:13:35 +0100169noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100170{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000171 (void)elr;
172 (void)spsr;
173
Andrew Sculla9c172d2019-04-03 14:10:00 +0100174 panic("FIQ from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000175}
176
Andrew Scullc960c032018-10-24 15:13:35 +0100177noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000178{
179 (void)elr;
180 (void)spsr;
181
Andrew Sculla9c172d2019-04-03 14:10:00 +0100182 panic("SERR from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000183}
184
Andrew Scullc960c032018-10-24 15:13:35 +0100185noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000186{
187 uintreg_t esr = read_msr(esr_el2);
188
189 (void)spsr;
190
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100191 switch (esr >> 26) {
192 case 0x25: /* EC = 100101, Data abort. */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100193 dlog("Data abort: pc=%#x, esr=%#x, ec=%#x", elr, esr,
Andrew Scull4f170f52018-07-19 12:58:20 +0100194 esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100195 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100196 dlog(", far=%#x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100197 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100198 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100199 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100200
201 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000202 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100203
204 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100205 dlog("Unknown current sync exception pc=%#x, esr=%#x, "
206 "ec=%#x\n",
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100207 elr, esr, esr >> 26);
Andrew Scullc960c032018-10-24 15:13:35 +0100208 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100209 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000210
Andrew Sculla9c172d2019-04-03 14:10:00 +0100211 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100212}
213
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100214/**
Andrew Walbran3d84a262018-12-13 14:41:19 +0000215 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
216 * arch_regs.
217 */
218static void set_virtual_interrupt(struct arch_regs *r, bool enable)
219{
220 if (enable) {
221 r->lazy.hcr_el2 |= HCR_EL2_VI;
222 } else {
223 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
224 }
225}
226
227/**
228 * Sets or clears the VI bit in the HCR_EL2 register.
229 */
230static void set_virtual_interrupt_current(bool enable)
231{
232 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000233
Andrew Walbran3d84a262018-12-13 14:41:19 +0000234 if (enable) {
235 hcr_el2 |= HCR_EL2_VI;
236 } else {
237 hcr_el2 &= ~HCR_EL2_VI;
238 }
239 write_msr(hcr_el2, hcr_el2);
240}
241
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100242static bool smc_check_client_privileges(const struct vcpu *vcpu)
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100243{
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100244 (void)vcpu; /*UNUSED*/
245
246 /*
247 * TODO(b/132421503): Check for privileges based on manifest.
248 * Currently returns false, which maintains existing behavior.
249 */
250
251 return false;
252}
253
254/**
255 * Applies SMC access control according to manifest.
256 * Forwards the call if access is granted.
257 * Returns true if call is forwarded.
258 */
259static bool smc_forwarder(const struct vcpu *vcpu, smc_res_t *ret)
260{
261 uint32_t func = vcpu->regs.r[0];
262 /* TODO(b/132421503): obtain vmid according to new scheme. */
263 uint32_t client_id = vcpu->vm->id;
264
265 if (smc_check_client_privileges(vcpu)) {
Andrew Scull52b8ea12019-08-30 19:16:09 +0100266 *ret = smc_forward(func, vcpu->regs.r[1], vcpu->regs.r[2],
267 vcpu->regs.r[3], vcpu->regs.r[4],
268 vcpu->regs.r[5], vcpu->regs.r[6], client_id);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100269 return true;
270 }
271
272 return false;
273}
274
275/**
276 * Processes SMC instruction calls.
277 */
278static bool smc_handler(struct vcpu *vcpu, smc_res_t *ret, struct vcpu **next)
279{
280 uint32_t func = vcpu->regs.r[0];
281
282 if (psci_handler(vcpu, func, vcpu->regs.r[1], vcpu->regs.r[2],
283 vcpu->regs.r[3], &(ret->res0), next)) {
284 /* SMC PSCI calls are processed by the PSCI handler. */
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100285 return true;
286 }
287
288 switch (func & ~SMCCC_CONVENTION_MASK) {
289 case HF_DEBUG_LOG:
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100290 api_debug_log(vcpu->regs.r[1], vcpu);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100291 return true;
292 }
293
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100294 /* Remaining SMC calls need to be forwarded. */
295 return smc_forwarder(vcpu, ret);
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100296}
297
Andrew Scull37402872018-10-24 14:23:06 +0100298struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1,
299 uintreg_t arg2, uintreg_t arg3)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100300{
301 struct hvc_handler_return ret;
302
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100303 ret.new = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100304
Andrew Walbran33645652019-04-15 12:29:31 +0100305 if (psci_handler(current(), arg0, arg1, arg2, arg3, &ret.user_ret,
306 &ret.new)) {
307 return ret;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100308 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100309
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000310 switch ((uint32_t)arg0) {
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100311 case SPCI_VERSION_32:
312 ret.user_ret = api_spci_version();
313 break;
314
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000315 case HF_VM_GET_ID:
316 ret.user_ret = api_vm_get_id(current());
317 break;
318
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100319 case HF_VM_GET_COUNT:
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100320 ret.user_ret = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100321 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100322
323 case HF_VCPU_GET_COUNT:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100324 ret.user_ret = api_vcpu_get_count(arg1, current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100325 break;
326
327 case HF_VCPU_RUN:
Andrew Scull6d2db332018-10-10 15:28:17 +0100328 ret.user_ret = hf_vcpu_run_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100329 api_vcpu_run(arg1, arg2, current(), &ret.new));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100330 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100331
Jose Marinho135dff32019-02-28 10:25:57 +0000332 case SPCI_YIELD_32:
333 ret.user_ret = api_spci_yield(current(), &ret.new);
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000334 break;
335
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100336 case HF_VM_CONFIGURE:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100337 ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2),
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000338 current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100339 break;
340
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000341 case SPCI_MSG_SEND_32:
342 ret.user_ret = api_spci_msg_send(arg1, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100343 break;
344
Jose Marinho3e2442f2019-03-12 13:30:37 +0000345 case SPCI_MSG_RECV_32:
346 ret.user_ret = api_spci_msg_recv(arg1, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100347 break;
348
Andrew Scullaa039b32018-10-04 15:02:26 +0100349 case HF_MAILBOX_CLEAR:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000350 ret.user_ret = api_mailbox_clear(current(), &ret.new);
351 break;
352
353 case HF_MAILBOX_WRITABLE_GET:
354 ret.user_ret = api_mailbox_writable_get(current());
355 break;
356
357 case HF_MAILBOX_WAITER_GET:
358 ret.user_ret = api_mailbox_waiter_get(arg1, current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100359 break;
360
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000361 case HF_INTERRUPT_ENABLE:
362 ret.user_ret = api_interrupt_enable(arg1, arg2, current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000363 break;
364
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000365 case HF_INTERRUPT_GET:
366 ret.user_ret = api_interrupt_get(current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000367 break;
368
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000369 case HF_INTERRUPT_INJECT:
370 ret.user_ret = api_interrupt_inject(arg1, arg2, arg3, current(),
Andrew Walbran318f5732018-11-20 16:23:42 +0000371 &ret.new);
372 break;
373
Andrew Scull6386f252018-12-06 13:29:10 +0000374 case HF_SHARE_MEMORY:
375 ret.user_ret =
376 api_share_memory(arg1 >> 32, ipa_init(arg2), arg3,
377 arg1 & 0xffffffff, current());
378 break;
379
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100380 case HF_DEBUG_LOG:
381 ret.user_ret = api_debug_log(arg1, current());
382 break;
383
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100384 default:
385 ret.user_ret = -1;
386 }
387
Andrew Walbran3d84a262018-12-13 14:41:19 +0000388 /* Set or clear VI bit. */
389 if (ret.new == NULL) {
390 /*
391 * Not switching vCPUs, set the bit for the current vCPU
392 * directly in the register.
393 */
Andrew Scullec52ddf2019-08-20 10:41:01 +0100394 struct vcpu *vcpu = current();
395
396 sl_lock(&vcpu->lock);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000397 set_virtual_interrupt_current(
Andrew Scullec52ddf2019-08-20 10:41:01 +0100398 vcpu->interrupts.enabled_and_pending_count > 0);
399 sl_unlock(&vcpu->lock);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000400 } else {
401 /*
402 * About to switch vCPUs, set the bit for the vCPU to which we
403 * are switching in the saved copy of the register.
404 */
Andrew Scullec52ddf2019-08-20 10:41:01 +0100405 sl_lock(&ret.new->lock);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000406 set_virtual_interrupt(
407 &ret.new->regs,
408 ret.new->interrupts.enabled_and_pending_count > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +0100409 sl_unlock(&ret.new->lock);
Andrew Walbran3d84a262018-12-13 14:41:19 +0000410 }
411
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100412 return ret;
413}
414
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100415struct vcpu *irq_lower(void)
416{
Andrew Scull9726c252019-01-23 13:44:19 +0000417 /*
418 * Switch back to primary VM, interrupts will be handled there.
419 *
420 * If the VM has aborted, this vCPU will be aborted when the scheduler
421 * tries to run it again. This means the interrupt will not be delayed
422 * by the aborted VM.
423 *
424 * TODO: Only switch when the interrupt isn't for the current VM.
425 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000426 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100427}
428
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000429struct vcpu *fiq_lower(void)
430{
431 return irq_lower();
432}
433
434struct vcpu *serr_lower(void)
435{
436 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000437 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000438}
439
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000440/**
441 * Initialises a fault info structure. It assumes that an FnV bit exists at
442 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
443 * the ESR (the fault status code) are 010000; this is the case for both
444 * instruction and data aborts, but not necessarily for other exception reasons.
445 */
446static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100447 const struct vcpu *vcpu, int mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000448{
449 uint32_t fsc = esr & 0x3f;
450 struct vcpu_fault_info r;
451
452 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000453 r.pc = va_init(vcpu->regs.pc);
454
455 /*
456 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
457 * indicates that we cannot rely on far_el2.
458 */
459 if (fsc == 0x10 && esr & (1u << 10)) {
460 r.vaddr = va_init(0);
461 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
462 } else {
463 r.vaddr = va_init(read_msr(far_el2));
464 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
465 (read_msr(far_el2) & (PAGE_SIZE - 1)));
466 }
467
468 return r;
469}
470
Andrew Scull37402872018-10-24 14:23:06 +0100471struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100472{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100473 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000474 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000475 struct vcpu *new_vcpu;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100476
477 switch (esr >> 26) {
478 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000479 /* Skip the instruction. */
480 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100481 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100482 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000483 /* WFE */
484 /*
485 * TODO: consider giving the scheduler more context,
486 * somehow.
487 */
Jose Marinho135dff32019-02-28 10:25:57 +0000488 api_spci_yield(vcpu, &new_vcpu);
489 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100490 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000491 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000492 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100493
494 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000495 info = fault_info_init(
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100496 esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000497 if (vcpu_handle_page_fault(vcpu, &info)) {
498 return NULL;
499 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000500 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100501
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100502 case 0x20: /* EC = 100000, Instruction abort. */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100503 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000504 if (vcpu_handle_page_fault(vcpu, &info)) {
505 return NULL;
506 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000507 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100508
Andrew Scullc960c032018-10-24 15:13:35 +0100509 case 0x17: /* EC = 010111, SMC instruction. */ {
510 uintreg_t smc_pc = vcpu->regs.pc;
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100511 smc_res_t ret;
Andrew Walbran33645652019-04-15 12:29:31 +0100512 struct vcpu *next = NULL;
Andrew Scullc960c032018-10-24 15:13:35 +0100513
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100514 if (!smc_handler(vcpu, &ret, &next)) {
515 /* TODO(b/132421503): handle SMC forward rejection */
Andrew Walbranac5b2612019-07-12 16:44:19 +0100516 dlog("Unsupported SMC call: %#x\n", vcpu->regs.r[0]);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100517 ret.res0 = PSCI_ERROR_NOT_SUPPORTED;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100518 }
519
520 /* Skip the SMC instruction. */
Andrew Scullc960c032018-10-24 15:13:35 +0100521 vcpu->regs.pc = smc_pc + (esr & (1u << 25) ? 4 : 2);
Fuad Tabba8176e3e2019-08-01 10:40:36 +0100522 vcpu->regs.r[0] = ret.res0;
523 vcpu->regs.r[1] = ret.res1;
524 vcpu->regs.r[2] = ret.res2;
525 vcpu->regs.r[3] = ret.res3;
Andrew Walbran33645652019-04-15 12:29:31 +0100526 return next;
Andrew Scullc960c032018-10-24 15:13:35 +0100527 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100528
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100529 default:
Andrew Walbranac5b2612019-07-12 16:44:19 +0100530 dlog("Unknown lower sync exception pc=%#x, esr=%#x, "
531 "ec=%#x\n",
Andrew Scull4f170f52018-07-19 12:58:20 +0100532 vcpu->regs.pc, esr, esr >> 26);
Andrew Scull9726c252019-01-23 13:44:19 +0000533 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100534 }
535
Andrew Scull9726c252019-01-23 13:44:19 +0000536 /* The exception wasn't handled so abort the VM. */
537 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100538}