blob: 871a38e5b93a2efcf6afd3ec34056e0490418c4d [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
19#include "hf/arch/init.h"
20
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/api.h"
22#include "hf/cpu.h"
23#include "hf/dlog.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000024#include "hf/spci.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/vm.h"
26
Andrew Scullf35a5c92018-08-07 18:09:46 +010027#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010028
29#include "msr.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010030#include "psci.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000031#include "smc.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010032
Andrew Walbran3d84a262018-12-13 14:41:19 +000033#define HCR_EL2_VI (1u << 7)
34
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010035struct hvc_handler_return {
Andrew Scull37402872018-10-24 14:23:06 +010036 uintreg_t user_ret;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010037 struct vcpu *new;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010038};
39
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010040void cpu_entry(struct cpu *c);
41
Andrew Scullc960c032018-10-24 15:13:35 +010042static uint32_t el3_psci_version = 0;
43
44/* Performs arch specific boot time initialisation. */
45void arch_one_time_init(void)
46{
47 el3_psci_version = smc(PSCI_VERSION, 0, 0, 0);
48
49 /* Check there's nothing unexpected about PSCI. */
50 switch (el3_psci_version) {
51 case PSCI_VERSION_0_2:
52 case PSCI_VERSION_1_0:
53 case PSCI_VERSION_1_1:
54 /* Supported EL3 PSCI version. */
55 dlog("Found PSCI version: 0x%x\n", el3_psci_version);
56 break;
57
58 default:
59 /* Unsupported EL3 PSCI version. Log a warning but continue. */
60 dlog("Warning: unknown PSCI version: 0x%x\n", el3_psci_version);
61 el3_psci_version = 0;
62 break;
63 }
64}
65
66/* Gets a reference to the currently executing vCPU. */
67static 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{
78 vcpu->regs.lazy.cntv_cval_el0 = read_msr(cntv_cval_el0);
79 vcpu->regs.lazy.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
80
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);
113 write_msr(cntv_cval_el0, vcpu->regs.lazy.cntv_cval_el0);
114 write_msr(cntv_ctl_el0, vcpu->regs.lazy.cntv_ctl_el0);
115
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 Scullc960c032018-10-24 15:13:35 +0100127/**
128 * This should never be reached as it means something has gone very wrong.
129 */
130static noreturn void hang(void)
131{
132 dlog("Hang: something went very wrong!\n");
133 for (;;) {
134 /* Do nothing. */
135 }
136}
137
138noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100139{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000140 (void)elr;
141 (void)spsr;
142
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100143 dlog("IRQ from current\n");
Andrew Scullc960c032018-10-24 15:13:35 +0100144 hang();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100145}
146
Andrew Scullc960c032018-10-24 15:13:35 +0100147noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100148{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000149 (void)elr;
150 (void)spsr;
151
152 dlog("FIQ from current\n");
Andrew Scullc960c032018-10-24 15:13:35 +0100153 hang();
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000154}
155
Andrew Scullc960c032018-10-24 15:13:35 +0100156noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000157{
158 (void)elr;
159 (void)spsr;
160
161 dlog("SERR from current\n");
Andrew Scullc960c032018-10-24 15:13:35 +0100162 hang();
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000163}
164
Andrew Scullc960c032018-10-24 15:13:35 +0100165noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000166{
167 uintreg_t esr = read_msr(esr_el2);
168
169 (void)spsr;
170
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100171 switch (esr >> 26) {
172 case 0x25: /* EC = 100101, Data abort. */
Andrew Scull4f170f52018-07-19 12:58:20 +0100173 dlog("Data abort: pc=0x%x, esr=0x%x, ec=0x%x", elr, esr,
174 esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100175 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull0a029e82018-11-23 16:48:08 +0000176 dlog(", far=0x%x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100177 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100178 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100179 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100180
181 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000182 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100183
184 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100185 dlog("Unknown current sync exception pc=0x%x, esr=0x%x, "
186 "ec=0x%x\n",
187 elr, esr, esr >> 26);
Andrew Scullc960c032018-10-24 15:13:35 +0100188 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100189 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000190
Andrew Scullc960c032018-10-24 15:13:35 +0100191 hang();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100192}
193
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100194/**
195 * Handles PSCI requests received via HVC or SMC instructions from the primary
196 * VM only.
197 *
Andrew Scullc960c032018-10-24 15:13:35 +0100198 * A minimal PSCI 1.1 interface is offered which can make use of previous
199 * version of PSCI in EL3 by acting as an adapter.
200 *
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100201 * Returns true if the request was a PSCI one, false otherwise.
202 */
Andrew Scull37402872018-10-24 14:23:06 +0100203static bool psci_handler(uint32_t func, uintreg_t arg0, uintreg_t arg1,
204 uintreg_t arg2, int32_t *ret)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100205{
206 struct cpu *c;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100207
Andrew Scullc960c032018-10-24 15:13:35 +0100208 /*
209 * If there's a problem with the EL3 PSCI, block standard secure service
210 * calls by marking them as unknown. Other calls will be allowed to pass
211 * through.
212 *
213 * This blocks more calls than just PSCI so it may need to be made more
214 * lenient in future.
215 */
216 if (el3_psci_version == 0) {
217 *ret = SMCCC_RETURN_UNKNOWN;
218 return (func & SMCCC_SERVICE_CALL_MASK) ==
219 SMCCC_STANDARD_SECURE_SERVICE_CALL;
220 }
221
222 switch (func & ~SMCCC_CONVENTION_MASK) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100223 case PSCI_VERSION:
Andrew Scullc960c032018-10-24 15:13:35 +0100224 *ret = PSCI_VERSION_1_1;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100225 break;
226
Andrew Scullc960c032018-10-24 15:13:35 +0100227 case PSCI_FEATURES:
228 switch (arg0 & ~SMCCC_CONVENTION_MASK) {
229 case PSCI_CPU_SUSPEND:
230 if (el3_psci_version == PSCI_VERSION_0_2) {
231 /*
232 * PSCI 0.2 doesn't support PSCI_FEATURES so
233 * report PSCI 0.2 compatible features.
234 */
235 *ret = 0;
236 } else {
237 /* PSCI 1.x only defines two feature bits. */
238 *ret = smc(func, arg0, 0, 0) & 0x3;
239 }
240 break;
241
242 case PSCI_VERSION:
243 case PSCI_FEATURES:
244 case PSCI_SYSTEM_OFF:
245 case PSCI_SYSTEM_RESET:
246 case PSCI_AFFINITY_INFO:
247 case PSCI_CPU_OFF:
248 case PSCI_CPU_ON:
249 /* These are supported without special features. */
250 *ret = 0;
251 break;
252
253 default:
254 /* Everything else is unsupported. */
255 *ret = PSCI_RETURN_NOT_SUPPORTED;
256 break;
257 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100258 break;
259
260 case PSCI_SYSTEM_OFF:
261 smc(PSCI_SYSTEM_OFF, 0, 0, 0);
Andrew Scullc960c032018-10-24 15:13:35 +0100262 hang();
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100263 break;
264
265 case PSCI_SYSTEM_RESET:
266 smc(PSCI_SYSTEM_RESET, 0, 0, 0);
Andrew Scullc960c032018-10-24 15:13:35 +0100267 hang();
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100268 break;
269
270 case PSCI_AFFINITY_INFO:
271 c = cpu_find(arg0);
272 if (!c) {
273 *ret = PSCI_RETURN_INVALID_PARAMETERS;
274 break;
275 }
276
277 if (arg1 != 0) {
278 *ret = PSCI_RETURN_NOT_SUPPORTED;
279 break;
280 }
281
282 sl_lock(&c->lock);
283 if (c->is_on) {
284 *ret = 0; /* ON */
285 } else {
286 *ret = 1; /* OFF */
287 }
288 sl_unlock(&c->lock);
289 break;
290
Andrew Scullc960c032018-10-24 15:13:35 +0100291 case PSCI_CPU_SUSPEND: {
292 /*
293 * Update vcpu state to wake from the provided entry point but
294 * if suspend returns, for example because it failed or was a
295 * standby power state, the SMC will return and the updated
296 * vcpu registers will be ignored.
297 */
298 struct vcpu *vcpu = current();
299
300 arch_regs_set_pc_arg(&vcpu->regs, ipa_init(arg1), arg2);
301 *ret = smc(PSCI_CPU_SUSPEND | SMCCC_64_BIT, arg0,
302 (uintreg_t)&cpu_entry, (uintreg_t)vcpu->cpu);
303 break;
304 }
305
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100306 case PSCI_CPU_OFF:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100307 cpu_off(current()->cpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100308 smc(PSCI_CPU_OFF, 0, 0, 0);
Andrew Scullc960c032018-10-24 15:13:35 +0100309 hang();
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100310 break;
311
312 case PSCI_CPU_ON:
313 c = cpu_find(arg0);
314 if (!c) {
315 *ret = PSCI_RETURN_INVALID_PARAMETERS;
316 break;
317 }
318
Andrew Scull1b8d0442018-08-06 15:47:04 +0100319 if (cpu_on(c, ipa_init(arg1), arg2)) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100320 *ret = PSCI_RETURN_ALREADY_ON;
321 break;
322 }
323
324 /*
325 * There's a race when turning a CPU on when it's in the
326 * process of turning off. We need to loop here while it is
327 * reported that the CPU is on (because it's about to turn
328 * itself off).
329 */
330 do {
Andrew Scullc960c032018-10-24 15:13:35 +0100331 *ret = smc(PSCI_CPU_ON | SMCCC_64_BIT, arg0,
332 (uintreg_t)&cpu_entry, (uintreg_t)c);
333 } while (*ret == PSCI_RETURN_ALREADY_ON);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100334
Andrew Scullc960c032018-10-24 15:13:35 +0100335 if (*ret != PSCI_RETURN_SUCCESS) {
336 cpu_off(c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100337 }
338 break;
339
Andrew Scullc960c032018-10-24 15:13:35 +0100340 case PSCI_MIGRATE:
341 case PSCI_MIGRATE_INFO_TYPE:
342 case PSCI_MIGRATE_INFO_UP_CPU:
343 case PSCI_CPU_FREEZE:
344 case PSCI_CPU_DEFAULT_SUSPEND:
345 case PSCI_NODE_HW_STATE:
346 case PSCI_SYSTEM_SUSPEND:
347 case PSCI_SET_SYSPEND_MODE:
348 case PSCI_STAT_RESIDENCY:
349 case PSCI_STAT_COUNT:
350 case PSCI_SYSTEM_RESET2:
351 case PSCI_MEM_PROTECT:
352 case PSCI_MEM_PROTECT_CHECK_RANGE:
353 /* Block all other known PSCI calls. */
354 *ret = PSCI_RETURN_NOT_SUPPORTED;
355 break;
356
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100357 default:
358 return false;
359 }
360
361 return true;
362}
363
Andrew Walbran3d84a262018-12-13 14:41:19 +0000364/**
365 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
366 * arch_regs.
367 */
368static void set_virtual_interrupt(struct arch_regs *r, bool enable)
369{
370 if (enable) {
371 r->lazy.hcr_el2 |= HCR_EL2_VI;
372 } else {
373 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
374 }
375}
376
377/**
378 * Sets or clears the VI bit in the HCR_EL2 register.
379 */
380static void set_virtual_interrupt_current(bool enable)
381{
382 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000383
Andrew Walbran3d84a262018-12-13 14:41:19 +0000384 if (enable) {
385 hcr_el2 |= HCR_EL2_VI;
386 } else {
387 hcr_el2 &= ~HCR_EL2_VI;
388 }
389 write_msr(hcr_el2, hcr_el2);
390}
391
Andrew Scull37402872018-10-24 14:23:06 +0100392struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1,
393 uintreg_t arg2, uintreg_t arg3)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100394{
395 struct hvc_handler_return ret;
396
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100397 ret.new = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100398
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100399 if (current()->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullc0e569a2018-10-02 18:05:21 +0100400 int32_t psci_ret;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000401
Andrew Scullc0e569a2018-10-02 18:05:21 +0100402 if (psci_handler(arg0, arg1, arg2, arg3, &psci_ret)) {
403 ret.user_ret = psci_ret;
404 return ret;
405 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100406 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100407
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000408 switch ((uint32_t)arg0) {
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000409 case HF_VM_GET_ID:
410 ret.user_ret = api_vm_get_id(current());
411 break;
412
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100413 case HF_VM_GET_COUNT:
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100414 ret.user_ret = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100415 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100416
417 case HF_VCPU_GET_COUNT:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100418 ret.user_ret = api_vcpu_get_count(arg1, current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100419 break;
420
421 case HF_VCPU_RUN:
Andrew Scull6d2db332018-10-10 15:28:17 +0100422 ret.user_ret = hf_vcpu_run_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100423 api_vcpu_run(arg1, arg2, current(), &ret.new));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100424 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100425
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000426 case HF_VCPU_YIELD:
427 ret.user_ret = 0;
428 ret.new = api_yield(current());
429 break;
430
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100431 case HF_VM_CONFIGURE:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100432 ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2),
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000433 current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100434 break;
435
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000436 case SPCI_MSG_SEND_32:
437 ret.user_ret = api_spci_msg_send(arg1, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100438 break;
439
Jose Marinho3e2442f2019-03-12 13:30:37 +0000440 case SPCI_MSG_RECV_32:
441 ret.user_ret = api_spci_msg_recv(arg1, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100442 break;
443
Andrew Scullaa039b32018-10-04 15:02:26 +0100444 case HF_MAILBOX_CLEAR:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000445 ret.user_ret = api_mailbox_clear(current(), &ret.new);
446 break;
447
448 case HF_MAILBOX_WRITABLE_GET:
449 ret.user_ret = api_mailbox_writable_get(current());
450 break;
451
452 case HF_MAILBOX_WAITER_GET:
453 ret.user_ret = api_mailbox_waiter_get(arg1, current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100454 break;
455
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000456 case HF_INTERRUPT_ENABLE:
457 ret.user_ret = api_interrupt_enable(arg1, arg2, current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000458 break;
459
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000460 case HF_INTERRUPT_GET:
461 ret.user_ret = api_interrupt_get(current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000462 break;
463
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000464 case HF_INTERRUPT_INJECT:
465 ret.user_ret = api_interrupt_inject(arg1, arg2, arg3, current(),
Andrew Walbran318f5732018-11-20 16:23:42 +0000466 &ret.new);
467 break;
468
Andrew Scull6386f252018-12-06 13:29:10 +0000469 case HF_SHARE_MEMORY:
470 ret.user_ret =
471 api_share_memory(arg1 >> 32, ipa_init(arg2), arg3,
472 arg1 & 0xffffffff, current());
473 break;
474
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100475 default:
476 ret.user_ret = -1;
477 }
478
Andrew Walbran3d84a262018-12-13 14:41:19 +0000479 /* Set or clear VI bit. */
480 if (ret.new == NULL) {
481 /*
482 * Not switching vCPUs, set the bit for the current vCPU
483 * directly in the register.
484 */
485 set_virtual_interrupt_current(
486 current()->interrupts.enabled_and_pending_count > 0);
487 } else {
488 /*
489 * About to switch vCPUs, set the bit for the vCPU to which we
490 * are switching in the saved copy of the register.
491 */
492 set_virtual_interrupt(
493 &ret.new->regs,
494 ret.new->interrupts.enabled_and_pending_count > 0);
495 }
496
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100497 return ret;
498}
499
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100500struct vcpu *irq_lower(void)
501{
Andrew Scull9726c252019-01-23 13:44:19 +0000502 /*
503 * Switch back to primary VM, interrupts will be handled there.
504 *
505 * If the VM has aborted, this vCPU will be aborted when the scheduler
506 * tries to run it again. This means the interrupt will not be delayed
507 * by the aborted VM.
508 *
509 * TODO: Only switch when the interrupt isn't for the current VM.
510 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000511 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100512}
513
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000514struct vcpu *fiq_lower(void)
515{
516 return irq_lower();
517}
518
519struct vcpu *serr_lower(void)
520{
521 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000522 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000523}
524
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000525/**
526 * Initialises a fault info structure. It assumes that an FnV bit exists at
527 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
528 * the ESR (the fault status code) are 010000; this is the case for both
529 * instruction and data aborts, but not necessarily for other exception reasons.
530 */
531static struct vcpu_fault_info fault_info_init(uintreg_t esr,
532 const struct vcpu *vcpu, int mode,
533 uint8_t size)
534{
535 uint32_t fsc = esr & 0x3f;
536 struct vcpu_fault_info r;
537
538 r.mode = mode;
539 r.size = size;
540 r.pc = va_init(vcpu->regs.pc);
541
542 /*
543 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
544 * indicates that we cannot rely on far_el2.
545 */
546 if (fsc == 0x10 && esr & (1u << 10)) {
547 r.vaddr = va_init(0);
548 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
549 } else {
550 r.vaddr = va_init(read_msr(far_el2));
551 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
552 (read_msr(far_el2) & (PAGE_SIZE - 1)));
553 }
554
555 return r;
556}
557
Andrew Scull37402872018-10-24 14:23:06 +0100558struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100559{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100560 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000561 struct vcpu_fault_info info;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100562
563 switch (esr >> 26) {
564 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000565 /* Skip the instruction. */
566 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100567 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100568 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000569 /* WFE */
570 /*
571 * TODO: consider giving the scheduler more context,
572 * somehow.
573 */
574 return api_yield(vcpu);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100575 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000576 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000577 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100578
579 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000580 /*
581 * Determine the size based on the SAS bits, which are only
582 * valid if the ISV bit is set. The WnR bit is used to decide
583 * if it's a read or write.
584 */
585 info = fault_info_init(
586 esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R,
587 (esr & (1u << 24)) ? (1u << ((esr >> 22) & 0x3)) : 0);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100588
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000589 /* Call the platform-independent handler. */
590 if (vcpu_handle_page_fault(vcpu, &info)) {
591 return NULL;
592 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000593 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100594
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100595 case 0x20: /* EC = 100000, Instruction abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000596 /* Determine the size based on the IL bit. */
597 info = fault_info_init(esr, vcpu, MM_MODE_X,
598 (esr & (1u << 25)) ? 4 : 2);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100599
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000600 /* Call the platform-independent handler. */
601 if (vcpu_handle_page_fault(vcpu, &info)) {
602 return NULL;
603 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000604 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100605
Andrew Scullc960c032018-10-24 15:13:35 +0100606 case 0x17: /* EC = 010111, SMC instruction. */ {
607 uintreg_t smc_pc = vcpu->regs.pc;
608 int32_t ret;
609
Andrew Scull19503262018-09-20 14:48:39 +0100610 if (vcpu->vm->id != HF_PRIMARY_VM_ID ||
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100611 !psci_handler(vcpu->regs.r[0], vcpu->regs.r[1],
612 vcpu->regs.r[2], vcpu->regs.r[3], &ret)) {
613 dlog("Unsupported SMC call: 0x%x\n", vcpu->regs.r[0]);
Andrew Scullc960c032018-10-24 15:13:35 +0100614 ret = PSCI_RETURN_NOT_SUPPORTED;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100615 }
616
617 /* Skip the SMC instruction. */
Andrew Scullc960c032018-10-24 15:13:35 +0100618 vcpu->regs.pc = smc_pc + (esr & (1u << 25) ? 4 : 2);
Andrew Scull9726c252019-01-23 13:44:19 +0000619 vcpu->regs.r[0] = ret;
620 return NULL;
Andrew Scullc960c032018-10-24 15:13:35 +0100621 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100622
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100623 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100624 dlog("Unknown lower sync exception pc=0x%x, esr=0x%x, "
625 "ec=0x%x\n",
Andrew Scull4f170f52018-07-19 12:58:20 +0100626 vcpu->regs.pc, esr, esr >> 26);
Andrew Scull9726c252019-01-23 13:44:19 +0000627 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100628 }
629
Andrew Scull9726c252019-01-23 13:44:19 +0000630 /* The exception wasn't handled so abort the VM. */
631 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100632}