blob: 7aa3a3e874fae8d058e4bc6ec55e7eecfb65dae2 [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"
Andrew Sculla9c172d2019-04-03 14:10:00 +010024#include "hf/panic.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000025#include "hf/spci.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010026#include "hf/vm.h"
27
Andrew Scullf35a5c92018-08-07 18:09:46 +010028#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010029
30#include "msr.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010031#include "psci.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000032#include "smc.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010033
Andrew Walbran3d84a262018-12-13 14:41:19 +000034#define HCR_EL2_VI (1u << 7)
35
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010036struct hvc_handler_return {
Andrew Scull37402872018-10-24 14:23:06 +010037 uintreg_t user_ret;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010038 struct vcpu *new;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010039};
40
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010041void cpu_entry(struct cpu *c);
42
Andrew Scullc960c032018-10-24 15:13:35 +010043static uint32_t el3_psci_version = 0;
44
45/* Performs arch specific boot time initialisation. */
46void arch_one_time_init(void)
47{
48 el3_psci_version = smc(PSCI_VERSION, 0, 0, 0);
49
50 /* Check there's nothing unexpected about PSCI. */
51 switch (el3_psci_version) {
52 case PSCI_VERSION_0_2:
53 case PSCI_VERSION_1_0:
54 case PSCI_VERSION_1_1:
55 /* Supported EL3 PSCI version. */
56 dlog("Found PSCI version: 0x%x\n", el3_psci_version);
57 break;
58
59 default:
60 /* Unsupported EL3 PSCI version. Log a warning but continue. */
61 dlog("Warning: unknown PSCI version: 0x%x\n", el3_psci_version);
62 el3_psci_version = 0;
63 break;
64 }
65}
66
67/* Gets a reference to the currently executing vCPU. */
68static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000069{
70 return (struct vcpu *)read_msr(tpidr_el2);
71}
72
Andrew Walbran1f8d4872018-12-20 11:21:32 +000073/**
74 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
75 * informs the arch-independent sections that registers have been saved.
76 */
77void complete_saving_state(struct vcpu *vcpu)
78{
79 vcpu->regs.lazy.cntv_cval_el0 = read_msr(cntv_cval_el0);
80 vcpu->regs.lazy.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
81
82 api_regs_state_saved(vcpu);
83
84 /*
85 * If switching away from the primary, copy the current EL0 virtual
86 * timer registers to the corresponding EL2 physical timer registers.
87 * This is used to emulate the virtual timer for the primary in case it
88 * should fire while the secondary is running.
89 */
90 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
91 /*
92 * Clear timer control register before copying compare value, to
93 * avoid a spurious timer interrupt. This could be a problem if
94 * the interrupt is configured as edge-triggered, as it would
95 * then be latched in.
96 */
97 write_msr(cnthp_ctl_el2, 0);
98 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
99 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
100 }
101}
102
103/**
104 * Restores the state of per-vCPU peripherals, such as the virtual timer.
105 */
106void begin_restoring_state(struct vcpu *vcpu)
107{
108 /*
109 * Clear timer control register before restoring compare value, to avoid
110 * a spurious timer interrupt. This could be a problem if the interrupt
111 * is configured as edge-triggered, as it would then be latched in.
112 */
113 write_msr(cntv_ctl_el0, 0);
114 write_msr(cntv_cval_el0, vcpu->regs.lazy.cntv_cval_el0);
115 write_msr(cntv_ctl_el0, vcpu->regs.lazy.cntv_ctl_el0);
116
117 /*
118 * If we are switching (back) to the primary, disable the EL2 physical
119 * timer which was being used to emulate the EL0 virtual timer, as the
120 * virtual timer is now running for the primary again.
121 */
122 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
123 write_msr(cnthp_ctl_el2, 0);
124 write_msr(cnthp_cval_el2, 0);
125 }
126}
127
Andrew Scullc960c032018-10-24 15:13:35 +0100128noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100129{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000130 (void)elr;
131 (void)spsr;
132
Andrew Sculla9c172d2019-04-03 14:10:00 +0100133 panic("IRQ from current");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100134}
135
Andrew Scullc960c032018-10-24 15:13:35 +0100136noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100137{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000138 (void)elr;
139 (void)spsr;
140
Andrew Sculla9c172d2019-04-03 14:10:00 +0100141 panic("FIQ from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000142}
143
Andrew Scullc960c032018-10-24 15:13:35 +0100144noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000145{
146 (void)elr;
147 (void)spsr;
148
Andrew Sculla9c172d2019-04-03 14:10:00 +0100149 panic("SERR from current");
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000150}
151
Andrew Scullc960c032018-10-24 15:13:35 +0100152noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000153{
154 uintreg_t esr = read_msr(esr_el2);
155
156 (void)spsr;
157
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100158 switch (esr >> 26) {
159 case 0x25: /* EC = 100101, Data abort. */
Andrew Scull4f170f52018-07-19 12:58:20 +0100160 dlog("Data abort: pc=0x%x, esr=0x%x, ec=0x%x", elr, esr,
161 esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100162 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull0a029e82018-11-23 16:48:08 +0000163 dlog(", far=0x%x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100164 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100165 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100166 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100167
168 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000169 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100170
171 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100172 dlog("Unknown current sync exception pc=0x%x, esr=0x%x, "
173 "ec=0x%x\n",
174 elr, esr, esr >> 26);
Andrew Scullc960c032018-10-24 15:13:35 +0100175 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100176 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000177
Andrew Sculla9c172d2019-04-03 14:10:00 +0100178 panic("EL2 exception");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100179}
180
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100181/**
182 * Handles PSCI requests received via HVC or SMC instructions from the primary
183 * VM only.
184 *
Andrew Scullc960c032018-10-24 15:13:35 +0100185 * A minimal PSCI 1.1 interface is offered which can make use of previous
186 * version of PSCI in EL3 by acting as an adapter.
187 *
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100188 * Returns true if the request was a PSCI one, false otherwise.
189 */
Andrew Scull37402872018-10-24 14:23:06 +0100190static bool psci_handler(uint32_t func, uintreg_t arg0, uintreg_t arg1,
191 uintreg_t arg2, int32_t *ret)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100192{
193 struct cpu *c;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100194
Andrew Scullc960c032018-10-24 15:13:35 +0100195 /*
196 * If there's a problem with the EL3 PSCI, block standard secure service
197 * calls by marking them as unknown. Other calls will be allowed to pass
198 * through.
199 *
200 * This blocks more calls than just PSCI so it may need to be made more
201 * lenient in future.
202 */
203 if (el3_psci_version == 0) {
204 *ret = SMCCC_RETURN_UNKNOWN;
205 return (func & SMCCC_SERVICE_CALL_MASK) ==
206 SMCCC_STANDARD_SECURE_SERVICE_CALL;
207 }
208
209 switch (func & ~SMCCC_CONVENTION_MASK) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100210 case PSCI_VERSION:
Andrew Scullc960c032018-10-24 15:13:35 +0100211 *ret = PSCI_VERSION_1_1;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100212 break;
213
Andrew Scullc960c032018-10-24 15:13:35 +0100214 case PSCI_FEATURES:
215 switch (arg0 & ~SMCCC_CONVENTION_MASK) {
216 case PSCI_CPU_SUSPEND:
217 if (el3_psci_version == PSCI_VERSION_0_2) {
218 /*
219 * PSCI 0.2 doesn't support PSCI_FEATURES so
220 * report PSCI 0.2 compatible features.
221 */
222 *ret = 0;
223 } else {
224 /* PSCI 1.x only defines two feature bits. */
225 *ret = smc(func, arg0, 0, 0) & 0x3;
226 }
227 break;
228
229 case PSCI_VERSION:
230 case PSCI_FEATURES:
231 case PSCI_SYSTEM_OFF:
232 case PSCI_SYSTEM_RESET:
233 case PSCI_AFFINITY_INFO:
234 case PSCI_CPU_OFF:
235 case PSCI_CPU_ON:
236 /* These are supported without special features. */
237 *ret = 0;
238 break;
239
240 default:
241 /* Everything else is unsupported. */
242 *ret = PSCI_RETURN_NOT_SUPPORTED;
243 break;
244 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100245 break;
246
247 case PSCI_SYSTEM_OFF:
248 smc(PSCI_SYSTEM_OFF, 0, 0, 0);
Andrew Sculla9c172d2019-04-03 14:10:00 +0100249 panic("System off failed");
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100250 break;
251
252 case PSCI_SYSTEM_RESET:
253 smc(PSCI_SYSTEM_RESET, 0, 0, 0);
Andrew Sculla9c172d2019-04-03 14:10:00 +0100254 panic("System reset failed");
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100255 break;
256
257 case PSCI_AFFINITY_INFO:
258 c = cpu_find(arg0);
259 if (!c) {
260 *ret = PSCI_RETURN_INVALID_PARAMETERS;
261 break;
262 }
263
264 if (arg1 != 0) {
265 *ret = PSCI_RETURN_NOT_SUPPORTED;
266 break;
267 }
268
269 sl_lock(&c->lock);
270 if (c->is_on) {
271 *ret = 0; /* ON */
272 } else {
273 *ret = 1; /* OFF */
274 }
275 sl_unlock(&c->lock);
276 break;
277
Andrew Scullc960c032018-10-24 15:13:35 +0100278 case PSCI_CPU_SUSPEND: {
279 /*
280 * Update vcpu state to wake from the provided entry point but
281 * if suspend returns, for example because it failed or was a
282 * standby power state, the SMC will return and the updated
283 * vcpu registers will be ignored.
284 */
285 struct vcpu *vcpu = current();
286
287 arch_regs_set_pc_arg(&vcpu->regs, ipa_init(arg1), arg2);
288 *ret = smc(PSCI_CPU_SUSPEND | SMCCC_64_BIT, arg0,
289 (uintreg_t)&cpu_entry, (uintreg_t)vcpu->cpu);
290 break;
291 }
292
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100293 case PSCI_CPU_OFF:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100294 cpu_off(current()->cpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100295 smc(PSCI_CPU_OFF, 0, 0, 0);
Andrew Sculla9c172d2019-04-03 14:10:00 +0100296 panic("CPU off failed");
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100297 break;
298
299 case PSCI_CPU_ON:
300 c = cpu_find(arg0);
301 if (!c) {
302 *ret = PSCI_RETURN_INVALID_PARAMETERS;
303 break;
304 }
305
Andrew Scull1b8d0442018-08-06 15:47:04 +0100306 if (cpu_on(c, ipa_init(arg1), arg2)) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100307 *ret = PSCI_RETURN_ALREADY_ON;
308 break;
309 }
310
311 /*
312 * There's a race when turning a CPU on when it's in the
313 * process of turning off. We need to loop here while it is
314 * reported that the CPU is on (because it's about to turn
315 * itself off).
316 */
317 do {
Andrew Scullc960c032018-10-24 15:13:35 +0100318 *ret = smc(PSCI_CPU_ON | SMCCC_64_BIT, arg0,
319 (uintreg_t)&cpu_entry, (uintreg_t)c);
320 } while (*ret == PSCI_RETURN_ALREADY_ON);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100321
Andrew Scullc960c032018-10-24 15:13:35 +0100322 if (*ret != PSCI_RETURN_SUCCESS) {
323 cpu_off(c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100324 }
325 break;
326
Andrew Scullc960c032018-10-24 15:13:35 +0100327 case PSCI_MIGRATE:
328 case PSCI_MIGRATE_INFO_TYPE:
329 case PSCI_MIGRATE_INFO_UP_CPU:
330 case PSCI_CPU_FREEZE:
331 case PSCI_CPU_DEFAULT_SUSPEND:
332 case PSCI_NODE_HW_STATE:
333 case PSCI_SYSTEM_SUSPEND:
334 case PSCI_SET_SYSPEND_MODE:
335 case PSCI_STAT_RESIDENCY:
336 case PSCI_STAT_COUNT:
337 case PSCI_SYSTEM_RESET2:
338 case PSCI_MEM_PROTECT:
339 case PSCI_MEM_PROTECT_CHECK_RANGE:
340 /* Block all other known PSCI calls. */
341 *ret = PSCI_RETURN_NOT_SUPPORTED;
342 break;
343
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100344 default:
345 return false;
346 }
347
348 return true;
349}
350
Andrew Walbran3d84a262018-12-13 14:41:19 +0000351/**
352 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
353 * arch_regs.
354 */
355static void set_virtual_interrupt(struct arch_regs *r, bool enable)
356{
357 if (enable) {
358 r->lazy.hcr_el2 |= HCR_EL2_VI;
359 } else {
360 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
361 }
362}
363
364/**
365 * Sets or clears the VI bit in the HCR_EL2 register.
366 */
367static void set_virtual_interrupt_current(bool enable)
368{
369 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000370
Andrew Walbran3d84a262018-12-13 14:41:19 +0000371 if (enable) {
372 hcr_el2 |= HCR_EL2_VI;
373 } else {
374 hcr_el2 &= ~HCR_EL2_VI;
375 }
376 write_msr(hcr_el2, hcr_el2);
377}
378
Andrew Scull37402872018-10-24 14:23:06 +0100379struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1,
380 uintreg_t arg2, uintreg_t arg3)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100381{
382 struct hvc_handler_return ret;
383
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100384 ret.new = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100385
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100386 if (current()->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullc0e569a2018-10-02 18:05:21 +0100387 int32_t psci_ret;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000388
Andrew Scullc0e569a2018-10-02 18:05:21 +0100389 if (psci_handler(arg0, arg1, arg2, arg3, &psci_ret)) {
390 ret.user_ret = psci_ret;
391 return ret;
392 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100393 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100394
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000395 switch ((uint32_t)arg0) {
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000396 case HF_VM_GET_ID:
397 ret.user_ret = api_vm_get_id(current());
398 break;
399
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100400 case HF_VM_GET_COUNT:
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100401 ret.user_ret = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100402 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100403
404 case HF_VCPU_GET_COUNT:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100405 ret.user_ret = api_vcpu_get_count(arg1, current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100406 break;
407
408 case HF_VCPU_RUN:
Andrew Scull6d2db332018-10-10 15:28:17 +0100409 ret.user_ret = hf_vcpu_run_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100410 api_vcpu_run(arg1, arg2, current(), &ret.new));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100411 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100412
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000413 case HF_VCPU_YIELD:
414 ret.user_ret = 0;
415 ret.new = api_yield(current());
416 break;
417
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100418 case HF_VM_CONFIGURE:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100419 ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2),
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000420 current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100421 break;
422
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000423 case SPCI_MSG_SEND_32:
424 ret.user_ret = api_spci_msg_send(arg1, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100425 break;
426
Jose Marinho3e2442f2019-03-12 13:30:37 +0000427 case SPCI_MSG_RECV_32:
428 ret.user_ret = api_spci_msg_recv(arg1, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100429 break;
430
Andrew Scullaa039b32018-10-04 15:02:26 +0100431 case HF_MAILBOX_CLEAR:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000432 ret.user_ret = api_mailbox_clear(current(), &ret.new);
433 break;
434
435 case HF_MAILBOX_WRITABLE_GET:
436 ret.user_ret = api_mailbox_writable_get(current());
437 break;
438
439 case HF_MAILBOX_WAITER_GET:
440 ret.user_ret = api_mailbox_waiter_get(arg1, current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100441 break;
442
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000443 case HF_INTERRUPT_ENABLE:
444 ret.user_ret = api_interrupt_enable(arg1, arg2, current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000445 break;
446
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000447 case HF_INTERRUPT_GET:
448 ret.user_ret = api_interrupt_get(current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000449 break;
450
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000451 case HF_INTERRUPT_INJECT:
452 ret.user_ret = api_interrupt_inject(arg1, arg2, arg3, current(),
Andrew Walbran318f5732018-11-20 16:23:42 +0000453 &ret.new);
454 break;
455
Andrew Scull6386f252018-12-06 13:29:10 +0000456 case HF_SHARE_MEMORY:
457 ret.user_ret =
458 api_share_memory(arg1 >> 32, ipa_init(arg2), arg3,
459 arg1 & 0xffffffff, current());
460 break;
461
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100462 default:
463 ret.user_ret = -1;
464 }
465
Andrew Walbran3d84a262018-12-13 14:41:19 +0000466 /* Set or clear VI bit. */
467 if (ret.new == NULL) {
468 /*
469 * Not switching vCPUs, set the bit for the current vCPU
470 * directly in the register.
471 */
472 set_virtual_interrupt_current(
473 current()->interrupts.enabled_and_pending_count > 0);
474 } else {
475 /*
476 * About to switch vCPUs, set the bit for the vCPU to which we
477 * are switching in the saved copy of the register.
478 */
479 set_virtual_interrupt(
480 &ret.new->regs,
481 ret.new->interrupts.enabled_and_pending_count > 0);
482 }
483
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100484 return ret;
485}
486
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100487struct vcpu *irq_lower(void)
488{
Andrew Scull9726c252019-01-23 13:44:19 +0000489 /*
490 * Switch back to primary VM, interrupts will be handled there.
491 *
492 * If the VM has aborted, this vCPU will be aborted when the scheduler
493 * tries to run it again. This means the interrupt will not be delayed
494 * by the aborted VM.
495 *
496 * TODO: Only switch when the interrupt isn't for the current VM.
497 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000498 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100499}
500
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000501struct vcpu *fiq_lower(void)
502{
503 return irq_lower();
504}
505
506struct vcpu *serr_lower(void)
507{
508 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000509 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000510}
511
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000512/**
513 * Initialises a fault info structure. It assumes that an FnV bit exists at
514 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
515 * the ESR (the fault status code) are 010000; this is the case for both
516 * instruction and data aborts, but not necessarily for other exception reasons.
517 */
518static struct vcpu_fault_info fault_info_init(uintreg_t esr,
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100519 const struct vcpu *vcpu, int mode)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000520{
521 uint32_t fsc = esr & 0x3f;
522 struct vcpu_fault_info r;
523
524 r.mode = mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000525 r.pc = va_init(vcpu->regs.pc);
526
527 /*
528 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
529 * indicates that we cannot rely on far_el2.
530 */
531 if (fsc == 0x10 && esr & (1u << 10)) {
532 r.vaddr = va_init(0);
533 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
534 } else {
535 r.vaddr = va_init(read_msr(far_el2));
536 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
537 (read_msr(far_el2) & (PAGE_SIZE - 1)));
538 }
539
540 return r;
541}
542
Andrew Scull37402872018-10-24 14:23:06 +0100543struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100544{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100545 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000546 struct vcpu_fault_info info;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100547
548 switch (esr >> 26) {
549 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000550 /* Skip the instruction. */
551 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100552 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100553 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000554 /* WFE */
555 /*
556 * TODO: consider giving the scheduler more context,
557 * somehow.
558 */
559 return api_yield(vcpu);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100560 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000561 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000562 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100563
564 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000565 info = fault_info_init(
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100566 esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000567 if (vcpu_handle_page_fault(vcpu, &info)) {
568 return NULL;
569 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000570 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100571
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100572 case 0x20: /* EC = 100000, Instruction abort. */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100573 info = fault_info_init(esr, vcpu, MM_MODE_X);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000574 if (vcpu_handle_page_fault(vcpu, &info)) {
575 return NULL;
576 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000577 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100578
Andrew Scullc960c032018-10-24 15:13:35 +0100579 case 0x17: /* EC = 010111, SMC instruction. */ {
580 uintreg_t smc_pc = vcpu->regs.pc;
581 int32_t ret;
582
Andrew Scull19503262018-09-20 14:48:39 +0100583 if (vcpu->vm->id != HF_PRIMARY_VM_ID ||
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100584 !psci_handler(vcpu->regs.r[0], vcpu->regs.r[1],
585 vcpu->regs.r[2], vcpu->regs.r[3], &ret)) {
586 dlog("Unsupported SMC call: 0x%x\n", vcpu->regs.r[0]);
Andrew Scullc960c032018-10-24 15:13:35 +0100587 ret = PSCI_RETURN_NOT_SUPPORTED;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100588 }
589
590 /* Skip the SMC instruction. */
Andrew Scullc960c032018-10-24 15:13:35 +0100591 vcpu->regs.pc = smc_pc + (esr & (1u << 25) ? 4 : 2);
Andrew Scull9726c252019-01-23 13:44:19 +0000592 vcpu->regs.r[0] = ret;
593 return NULL;
Andrew Scullc960c032018-10-24 15:13:35 +0100594 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100595
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100596 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100597 dlog("Unknown lower sync exception pc=0x%x, esr=0x%x, "
598 "ec=0x%x\n",
Andrew Scull4f170f52018-07-19 12:58:20 +0100599 vcpu->regs.pc, esr, esr >> 26);
Andrew Scull9726c252019-01-23 13:44:19 +0000600 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100601 }
602
Andrew Scull9726c252019-01-23 13:44:19 +0000603 /* The exception wasn't handled so abort the VM. */
604 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100605}