blob: 7de6ad3a02ca61be0200f4dda60f88abcf26a9c1 [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"
21
Andrew Scull18c78fc2018-08-20 12:57:41 +010022#include "hf/api.h"
23#include "hf/cpu.h"
24#include "hf/dlog.h"
Andrew Sculla9c172d2019-04-03 14:10:00 +010025#include "hf/panic.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000026#include "hf/spci.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010027#include "hf/vm.h"
28
Andrew Scullf35a5c92018-08-07 18:09:46 +010029#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010030
31#include "msr.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010032#include "psci.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000033#include "smc.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010034
Andrew Walbran3d84a262018-12-13 14:41:19 +000035#define HCR_EL2_VI (1u << 7)
36
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010037struct hvc_handler_return {
Andrew Scull37402872018-10-24 14:23:06 +010038 uintreg_t user_ret;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010039 struct vcpu *new;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010040};
41
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010042void cpu_entry(struct cpu *c);
43
Andrew Scullc960c032018-10-24 15:13:35 +010044static uint32_t el3_psci_version = 0;
45
46/* Performs arch specific boot time initialisation. */
47void arch_one_time_init(void)
48{
49 el3_psci_version = smc(PSCI_VERSION, 0, 0, 0);
50
51 /* Check there's nothing unexpected about PSCI. */
52 switch (el3_psci_version) {
53 case PSCI_VERSION_0_2:
54 case PSCI_VERSION_1_0:
55 case PSCI_VERSION_1_1:
56 /* Supported EL3 PSCI version. */
57 dlog("Found PSCI version: 0x%x\n", el3_psci_version);
58 break;
59
60 default:
61 /* Unsupported EL3 PSCI version. Log a warning but continue. */
62 dlog("Warning: unknown PSCI version: 0x%x\n", el3_psci_version);
63 el3_psci_version = 0;
64 break;
65 }
66}
67
68/* Gets a reference to the currently executing vCPU. */
69static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000070{
71 return (struct vcpu *)read_msr(tpidr_el2);
72}
73
Andrew Walbran1f8d4872018-12-20 11:21:32 +000074/**
75 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
76 * informs the arch-independent sections that registers have been saved.
77 */
78void complete_saving_state(struct vcpu *vcpu)
79{
Andrew Walbran6480f8f2019-06-05 17:39:14 +010080 vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0);
81 vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +000082
83 api_regs_state_saved(vcpu);
84
85 /*
86 * If switching away from the primary, copy the current EL0 virtual
87 * timer registers to the corresponding EL2 physical timer registers.
88 * This is used to emulate the virtual timer for the primary in case it
89 * should fire while the secondary is running.
90 */
91 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
92 /*
93 * Clear timer control register before copying compare value, to
94 * avoid a spurious timer interrupt. This could be a problem if
95 * the interrupt is configured as edge-triggered, as it would
96 * then be latched in.
97 */
98 write_msr(cnthp_ctl_el2, 0);
99 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
100 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
101 }
102}
103
104/**
105 * Restores the state of per-vCPU peripherals, such as the virtual timer.
106 */
107void begin_restoring_state(struct vcpu *vcpu)
108{
109 /*
110 * Clear timer control register before restoring compare value, to avoid
111 * a spurious timer interrupt. This could be a problem if the interrupt
112 * is configured as edge-triggered, as it would then be latched in.
113 */
114 write_msr(cntv_ctl_el0, 0);
Andrew Walbran6480f8f2019-06-05 17:39:14 +0100115 write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0);
116 write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0);
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000117
118 /*
119 * If we are switching (back) to the primary, disable the EL2 physical
120 * timer which was being used to emulate the EL0 virtual timer, as the
121 * virtual timer is now running for the primary again.
122 */
123 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
124 write_msr(cnthp_ctl_el2, 0);
125 write_msr(cnthp_cval_el2, 0);
126 }
127}
128
Andrew Walbran1f32e722019-06-07 17:57:26 +0100129/**
130 * Ensures all explicit memory access and management instructions for
131 * non-shareable normal memory have completed before continuing.
132 */
133static void dsb_nsh(void)
134{
135 __asm__ volatile("dsb nsh");
136}
137
138/**
139 * Invalidate all stage 1 TLB entries on the current (physical) CPU for the
140 * current VMID.
141 */
142static void invalidate_vm_tlb(void)
143{
144 isb();
145 __asm__ volatile("tlbi vmalle1");
146 isb();
147 dsb_nsh();
148}
149
150/**
151 * Invalidates the TLB if a different vCPU is being run than the last vCPU of
152 * the same VM which was run on the current pCPU.
153 *
154 * This is necessary because VMs may (contrary to the architecture
155 * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar
156 * workaround:
157 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9
158 */
159void maybe_invalidate_tlb(struct vcpu *vcpu)
160{
161 size_t current_cpu_index = cpu_index(vcpu->cpu);
162 size_t new_vcpu_index = vcpu_index(vcpu);
163
164 if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] !=
165 new_vcpu_index) {
166 /*
167 * The vCPU has changed since the last time this VM was run on
168 * this pCPU, so we need to invalidate the TLB.
169 */
170 invalidate_vm_tlb();
171
172 /* Record the fact that this vCPU is now running on this CPU. */
173 vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] =
174 new_vcpu_index;
175 }
176}
177
Andrew Scullc960c032018-10-24 15:13:35 +0100178noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100179{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000180 (void)elr;
181 (void)spsr;
182
Andrew Sculla9c172d2019-04-03 14:10:00 +0100183 panic("IRQ from current");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100184}
185
Andrew Scullc960c032018-10-24 15:13:35 +0100186noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100187{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000188 (void)elr;
189 (void)spsr;
190
Andrew Sculla9c172d2019-04-03 14:10:00 +0100191 panic("FIQ from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000192}
193
Andrew Scullc960c032018-10-24 15:13:35 +0100194noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000195{
196 (void)elr;
197 (void)spsr;
198
Andrew Sculla9c172d2019-04-03 14:10:00 +0100199 panic("SERR from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000200}
201
Andrew Scullc960c032018-10-24 15:13:35 +0100202noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000203{
204 uintreg_t esr = read_msr(esr_el2);
205
206 (void)spsr;
207
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100208 switch (esr >> 26) {
209 case 0x25: /* EC = 100101, Data abort. */
Andrew Scull4f170f52018-07-19 12:58:20 +0100210 dlog("Data abort: pc=0x%x, esr=0x%x, ec=0x%x", elr, esr,
211 esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100212 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull0a029e82018-11-23 16:48:08 +0000213 dlog(", far=0x%x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100214 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100215 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100216 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100217
218 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000219 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100220
221 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100222 dlog("Unknown current sync exception pc=0x%x, esr=0x%x, "
223 "ec=0x%x\n",
224 elr, esr, esr >> 26);
Andrew Scullc960c032018-10-24 15:13:35 +0100225 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100226 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000227
Andrew Sculla9c172d2019-04-03 14:10:00 +0100228 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100229}
230
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100231/**
232 * Handles PSCI requests received via HVC or SMC instructions from the primary
233 * VM only.
234 *
Andrew Scullc960c032018-10-24 15:13:35 +0100235 * A minimal PSCI 1.1 interface is offered which can make use of previous
236 * version of PSCI in EL3 by acting as an adapter.
237 *
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100238 * Returns true if the request was a PSCI one, false otherwise.
239 */
Andrew Scull37402872018-10-24 14:23:06 +0100240static bool psci_handler(uint32_t func, uintreg_t arg0, uintreg_t arg1,
241 uintreg_t arg2, int32_t *ret)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100242{
243 struct cpu *c;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100244
Andrew Scullc960c032018-10-24 15:13:35 +0100245 /*
246 * If there's a problem with the EL3 PSCI, block standard secure service
247 * calls by marking them as unknown. Other calls will be allowed to pass
248 * through.
249 *
250 * This blocks more calls than just PSCI so it may need to be made more
251 * lenient in future.
252 */
253 if (el3_psci_version == 0) {
254 *ret = SMCCC_RETURN_UNKNOWN;
255 return (func & SMCCC_SERVICE_CALL_MASK) ==
256 SMCCC_STANDARD_SECURE_SERVICE_CALL;
257 }
258
259 switch (func & ~SMCCC_CONVENTION_MASK) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100260 case PSCI_VERSION:
Andrew Scullc960c032018-10-24 15:13:35 +0100261 *ret = PSCI_VERSION_1_1;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100262 break;
263
Andrew Scullc960c032018-10-24 15:13:35 +0100264 case PSCI_FEATURES:
265 switch (arg0 & ~SMCCC_CONVENTION_MASK) {
266 case PSCI_CPU_SUSPEND:
267 if (el3_psci_version == PSCI_VERSION_0_2) {
268 /*
269 * PSCI 0.2 doesn't support PSCI_FEATURES so
270 * report PSCI 0.2 compatible features.
271 */
272 *ret = 0;
273 } else {
274 /* PSCI 1.x only defines two feature bits. */
275 *ret = smc(func, arg0, 0, 0) & 0x3;
276 }
277 break;
278
279 case PSCI_VERSION:
280 case PSCI_FEATURES:
281 case PSCI_SYSTEM_OFF:
282 case PSCI_SYSTEM_RESET:
283 case PSCI_AFFINITY_INFO:
284 case PSCI_CPU_OFF:
285 case PSCI_CPU_ON:
286 /* These are supported without special features. */
287 *ret = 0;
288 break;
289
290 default:
291 /* Everything else is unsupported. */
292 *ret = PSCI_RETURN_NOT_SUPPORTED;
293 break;
294 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100295 break;
296
297 case PSCI_SYSTEM_OFF:
298 smc(PSCI_SYSTEM_OFF, 0, 0, 0);
Andrew Sculla9c172d2019-04-03 14:10:00 +0100299 panic("System off failed");
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100300 break;
301
302 case PSCI_SYSTEM_RESET:
303 smc(PSCI_SYSTEM_RESET, 0, 0, 0);
Andrew Sculla9c172d2019-04-03 14:10:00 +0100304 panic("System reset failed");
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100305 break;
306
307 case PSCI_AFFINITY_INFO:
308 c = cpu_find(arg0);
309 if (!c) {
310 *ret = PSCI_RETURN_INVALID_PARAMETERS;
311 break;
312 }
313
314 if (arg1 != 0) {
315 *ret = PSCI_RETURN_NOT_SUPPORTED;
316 break;
317 }
318
319 sl_lock(&c->lock);
320 if (c->is_on) {
321 *ret = 0; /* ON */
322 } else {
323 *ret = 1; /* OFF */
324 }
325 sl_unlock(&c->lock);
326 break;
327
Andrew Scullc960c032018-10-24 15:13:35 +0100328 case PSCI_CPU_SUSPEND: {
329 /*
330 * Update vcpu state to wake from the provided entry point but
331 * if suspend returns, for example because it failed or was a
332 * standby power state, the SMC will return and the updated
333 * vcpu registers will be ignored.
334 */
335 struct vcpu *vcpu = current();
336
337 arch_regs_set_pc_arg(&vcpu->regs, ipa_init(arg1), arg2);
338 *ret = smc(PSCI_CPU_SUSPEND | SMCCC_64_BIT, arg0,
339 (uintreg_t)&cpu_entry, (uintreg_t)vcpu->cpu);
340 break;
341 }
342
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100343 case PSCI_CPU_OFF:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100344 cpu_off(current()->cpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100345 smc(PSCI_CPU_OFF, 0, 0, 0);
Andrew Sculla9c172d2019-04-03 14:10:00 +0100346 panic("CPU off failed");
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100347 break;
348
349 case PSCI_CPU_ON:
350 c = cpu_find(arg0);
351 if (!c) {
352 *ret = PSCI_RETURN_INVALID_PARAMETERS;
353 break;
354 }
355
Andrew Scull1b8d0442018-08-06 15:47:04 +0100356 if (cpu_on(c, ipa_init(arg1), arg2)) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100357 *ret = PSCI_RETURN_ALREADY_ON;
358 break;
359 }
360
361 /*
362 * There's a race when turning a CPU on when it's in the
363 * process of turning off. We need to loop here while it is
364 * reported that the CPU is on (because it's about to turn
365 * itself off).
366 */
367 do {
Andrew Scullc960c032018-10-24 15:13:35 +0100368 *ret = smc(PSCI_CPU_ON | SMCCC_64_BIT, arg0,
369 (uintreg_t)&cpu_entry, (uintreg_t)c);
370 } while (*ret == PSCI_RETURN_ALREADY_ON);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100371
Andrew Scullc960c032018-10-24 15:13:35 +0100372 if (*ret != PSCI_RETURN_SUCCESS) {
373 cpu_off(c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100374 }
375 break;
376
Andrew Scullc960c032018-10-24 15:13:35 +0100377 case PSCI_MIGRATE:
378 case PSCI_MIGRATE_INFO_TYPE:
379 case PSCI_MIGRATE_INFO_UP_CPU:
380 case PSCI_CPU_FREEZE:
381 case PSCI_CPU_DEFAULT_SUSPEND:
382 case PSCI_NODE_HW_STATE:
383 case PSCI_SYSTEM_SUSPEND:
384 case PSCI_SET_SYSPEND_MODE:
385 case PSCI_STAT_RESIDENCY:
386 case PSCI_STAT_COUNT:
387 case PSCI_SYSTEM_RESET2:
388 case PSCI_MEM_PROTECT:
389 case PSCI_MEM_PROTECT_CHECK_RANGE:
390 /* Block all other known PSCI calls. */
391 *ret = PSCI_RETURN_NOT_SUPPORTED;
392 break;
393
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100394 default:
395 return false;
396 }
397
398 return true;
399}
400
Andrew Walbran3d84a262018-12-13 14:41:19 +0000401/**
402 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
403 * arch_regs.
404 */
405static void set_virtual_interrupt(struct arch_regs *r, bool enable)
406{
407 if (enable) {
408 r->lazy.hcr_el2 |= HCR_EL2_VI;
409 } else {
410 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
411 }
412}
413
414/**
415 * Sets or clears the VI bit in the HCR_EL2 register.
416 */
417static void set_virtual_interrupt_current(bool enable)
418{
419 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000420
Andrew Walbran3d84a262018-12-13 14:41:19 +0000421 if (enable) {
422 hcr_el2 |= HCR_EL2_VI;
423 } else {
424 hcr_el2 &= ~HCR_EL2_VI;
425 }
426 write_msr(hcr_el2, hcr_el2);
427}
428
Andrew Scull37402872018-10-24 14:23:06 +0100429struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1,
430 uintreg_t arg2, uintreg_t arg3)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100431{
432 struct hvc_handler_return ret;
433
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100434 ret.new = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100435
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100436 if (current()->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullc0e569a2018-10-02 18:05:21 +0100437 int32_t psci_ret;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000438
Andrew Scullc0e569a2018-10-02 18:05:21 +0100439 if (psci_handler(arg0, arg1, arg2, arg3, &psci_ret)) {
440 ret.user_ret = psci_ret;
441 return ret;
442 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100443 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100444
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000445 switch ((uint32_t)arg0) {
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100446 case SPCI_VERSION_32:
447 ret.user_ret = api_spci_version();
448 break;
449
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000450 case HF_VM_GET_ID:
451 ret.user_ret = api_vm_get_id(current());
452 break;
453
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100454 case HF_VM_GET_COUNT:
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100455 ret.user_ret = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100456 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100457
458 case HF_VCPU_GET_COUNT:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100459 ret.user_ret = api_vcpu_get_count(arg1, current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100460 break;
461
462 case HF_VCPU_RUN:
Andrew Scull6d2db332018-10-10 15:28:17 +0100463 ret.user_ret = hf_vcpu_run_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100464 api_vcpu_run(arg1, arg2, current(), &ret.new));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100465 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100466
Jose Marinho135dff32019-02-28 10:25:57 +0000467 case SPCI_YIELD_32:
468 ret.user_ret = api_spci_yield(current(), &ret.new);
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000469 break;
470
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100471 case HF_VM_CONFIGURE:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100472 ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2),
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000473 current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100474 break;
475
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000476 case SPCI_MSG_SEND_32:
477 ret.user_ret = api_spci_msg_send(arg1, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100478 break;
479
Jose Marinho3e2442f2019-03-12 13:30:37 +0000480 case SPCI_MSG_RECV_32:
481 ret.user_ret = api_spci_msg_recv(arg1, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100482 break;
483
Andrew Scullaa039b32018-10-04 15:02:26 +0100484 case HF_MAILBOX_CLEAR:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000485 ret.user_ret = api_mailbox_clear(current(), &ret.new);
486 break;
487
488 case HF_MAILBOX_WRITABLE_GET:
489 ret.user_ret = api_mailbox_writable_get(current());
490 break;
491
492 case HF_MAILBOX_WAITER_GET:
493 ret.user_ret = api_mailbox_waiter_get(arg1, current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100494 break;
495
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000496 case HF_INTERRUPT_ENABLE:
497 ret.user_ret = api_interrupt_enable(arg1, arg2, current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000498 break;
499
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000500 case HF_INTERRUPT_GET:
501 ret.user_ret = api_interrupt_get(current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000502 break;
503
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000504 case HF_INTERRUPT_INJECT:
505 ret.user_ret = api_interrupt_inject(arg1, arg2, arg3, current(),
Andrew Walbran318f5732018-11-20 16:23:42 +0000506 &ret.new);
507 break;
508
Andrew Scull6386f252018-12-06 13:29:10 +0000509 case HF_SHARE_MEMORY:
510 ret.user_ret =
511 api_share_memory(arg1 >> 32, ipa_init(arg2), arg3,
512 arg1 & 0xffffffff, current());
513 break;
514
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100515 default:
516 ret.user_ret = -1;
517 }
518
Andrew Walbran3d84a262018-12-13 14:41:19 +0000519 /* Set or clear VI bit. */
520 if (ret.new == NULL) {
521 /*
522 * Not switching vCPUs, set the bit for the current vCPU
523 * directly in the register.
524 */
525 set_virtual_interrupt_current(
526 current()->interrupts.enabled_and_pending_count > 0);
527 } else {
528 /*
529 * About to switch vCPUs, set the bit for the vCPU to which we
530 * are switching in the saved copy of the register.
531 */
532 set_virtual_interrupt(
533 &ret.new->regs,
534 ret.new->interrupts.enabled_and_pending_count > 0);
535 }
536
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100537 return ret;
538}
539
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100540struct vcpu *irq_lower(void)
541{
Andrew Scull9726c252019-01-23 13:44:19 +0000542 /*
543 * Switch back to primary VM, interrupts will be handled there.
544 *
545 * If the VM has aborted, this vCPU will be aborted when the scheduler
546 * tries to run it again. This means the interrupt will not be delayed
547 * by the aborted VM.
548 *
549 * TODO: Only switch when the interrupt isn't for the current VM.
550 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000551 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100552}
553
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000554struct vcpu *fiq_lower(void)
555{
556 return irq_lower();
557}
558
559struct vcpu *serr_lower(void)
560{
561 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000562 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000563}
564
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000565/**
566 * Initialises a fault info structure. It assumes that an FnV bit exists at
567 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
568 * the ESR (the fault status code) are 010000; this is the case for both
569 * instruction and data aborts, but not necessarily for other exception reasons.
570 */
571static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100572 const struct vcpu *vcpu, int mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000573{
574 uint32_t fsc = esr & 0x3f;
575 struct vcpu_fault_info r;
576
577 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000578 r.pc = va_init(vcpu->regs.pc);
579
580 /*
581 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
582 * indicates that we cannot rely on far_el2.
583 */
584 if (fsc == 0x10 && esr & (1u << 10)) {
585 r.vaddr = va_init(0);
586 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
587 } else {
588 r.vaddr = va_init(read_msr(far_el2));
589 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
590 (read_msr(far_el2) & (PAGE_SIZE - 1)));
591 }
592
593 return r;
594}
595
Andrew Scull37402872018-10-24 14:23:06 +0100596struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100597{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100598 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000599 struct vcpu_fault_info info;
Jose Marinho135dff32019-02-28 10:25:57 +0000600 struct vcpu *new_vcpu;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100601
602 switch (esr >> 26) {
603 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000604 /* Skip the instruction. */
605 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100606 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100607 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000608 /* WFE */
609 /*
610 * TODO: consider giving the scheduler more context,
611 * somehow.
612 */
Jose Marinho135dff32019-02-28 10:25:57 +0000613 api_spci_yield(vcpu, &new_vcpu);
614 return new_vcpu;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100615 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000616 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000617 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100618
619 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000620 info = fault_info_init(
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100621 esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000622 if (vcpu_handle_page_fault(vcpu, &info)) {
623 return NULL;
624 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000625 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100626
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100627 case 0x20: /* EC = 100000, Instruction abort. */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100628 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000629 if (vcpu_handle_page_fault(vcpu, &info)) {
630 return NULL;
631 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000632 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100633
Andrew Scullc960c032018-10-24 15:13:35 +0100634 case 0x17: /* EC = 010111, SMC instruction. */ {
635 uintreg_t smc_pc = vcpu->regs.pc;
636 int32_t ret;
637
Andrew Scull19503262018-09-20 14:48:39 +0100638 if (vcpu->vm->id != HF_PRIMARY_VM_ID ||
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100639 !psci_handler(vcpu->regs.r[0], vcpu->regs.r[1],
640 vcpu->regs.r[2], vcpu->regs.r[3], &ret)) {
641 dlog("Unsupported SMC call: 0x%x\n", vcpu->regs.r[0]);
Andrew Scullc960c032018-10-24 15:13:35 +0100642 ret = PSCI_RETURN_NOT_SUPPORTED;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100643 }
644
645 /* Skip the SMC instruction. */
Andrew Scullc960c032018-10-24 15:13:35 +0100646 vcpu->regs.pc = smc_pc + (esr & (1u << 25) ? 4 : 2);
Andrew Scull9726c252019-01-23 13:44:19 +0000647 vcpu->regs.r[0] = ret;
648 return NULL;
Andrew Scullc960c032018-10-24 15:13:35 +0100649 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100650
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100651 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100652 dlog("Unknown lower sync exception pc=0x%x, esr=0x%x, "
653 "ec=0x%x\n",
Andrew Scull4f170f52018-07-19 12:58:20 +0100654 vcpu->regs.pc, esr, esr >> 26);
Andrew Scull9726c252019-01-23 13:44:19 +0000655 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100656 }
657
Andrew Scull9726c252019-01-23 13:44:19 +0000658 /* The exception wasn't handled so abort the VM. */
659 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100660}