blob: eb8f8dc15a5344cdecd4b0e0c59a38d84d9f8ec9 [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"
24#include "hf/vm.h"
25
Andrew Scullf35a5c92018-08-07 18:09:46 +010026#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027
28#include "msr.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010029#include "psci.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000030#include "smc.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010031
Andrew Walbran3d84a262018-12-13 14:41:19 +000032#define HCR_EL2_VI (1u << 7)
33
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010034struct hvc_handler_return {
Andrew Scull37402872018-10-24 14:23:06 +010035 uintreg_t user_ret;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010036 struct vcpu *new;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010037};
38
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010039void cpu_entry(struct cpu *c);
40
Andrew Scullc960c032018-10-24 15:13:35 +010041static uint32_t el3_psci_version = 0;
42
43/* Performs arch specific boot time initialisation. */
44void arch_one_time_init(void)
45{
46 el3_psci_version = smc(PSCI_VERSION, 0, 0, 0);
47
48 /* Check there's nothing unexpected about PSCI. */
49 switch (el3_psci_version) {
50 case PSCI_VERSION_0_2:
51 case PSCI_VERSION_1_0:
52 case PSCI_VERSION_1_1:
53 /* Supported EL3 PSCI version. */
54 dlog("Found PSCI version: 0x%x\n", el3_psci_version);
55 break;
56
57 default:
58 /* Unsupported EL3 PSCI version. Log a warning but continue. */
59 dlog("Warning: unknown PSCI version: 0x%x\n", el3_psci_version);
60 el3_psci_version = 0;
61 break;
62 }
63}
64
65/* Gets a reference to the currently executing vCPU. */
66static struct vcpu *current(void)
Andrew Walbran3d84a262018-12-13 14:41:19 +000067{
68 return (struct vcpu *)read_msr(tpidr_el2);
69}
70
Andrew Walbran1f8d4872018-12-20 11:21:32 +000071/**
72 * Saves the state of per-vCPU peripherals, such as the virtual timer, and
73 * informs the arch-independent sections that registers have been saved.
74 */
75void complete_saving_state(struct vcpu *vcpu)
76{
77 vcpu->regs.lazy.cntv_cval_el0 = read_msr(cntv_cval_el0);
78 vcpu->regs.lazy.cntv_ctl_el0 = read_msr(cntv_ctl_el0);
79
80 api_regs_state_saved(vcpu);
81
82 /*
83 * If switching away from the primary, copy the current EL0 virtual
84 * timer registers to the corresponding EL2 physical timer registers.
85 * This is used to emulate the virtual timer for the primary in case it
86 * should fire while the secondary is running.
87 */
88 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
89 /*
90 * Clear timer control register before copying compare value, to
91 * avoid a spurious timer interrupt. This could be a problem if
92 * the interrupt is configured as edge-triggered, as it would
93 * then be latched in.
94 */
95 write_msr(cnthp_ctl_el2, 0);
96 write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0));
97 write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0));
98 }
99}
100
101/**
102 * Restores the state of per-vCPU peripherals, such as the virtual timer.
103 */
104void begin_restoring_state(struct vcpu *vcpu)
105{
106 /*
107 * Clear timer control register before restoring compare value, to avoid
108 * a spurious timer interrupt. This could be a problem if the interrupt
109 * is configured as edge-triggered, as it would then be latched in.
110 */
111 write_msr(cntv_ctl_el0, 0);
112 write_msr(cntv_cval_el0, vcpu->regs.lazy.cntv_cval_el0);
113 write_msr(cntv_ctl_el0, vcpu->regs.lazy.cntv_ctl_el0);
114
115 /*
116 * If we are switching (back) to the primary, disable the EL2 physical
117 * timer which was being used to emulate the EL0 virtual timer, as the
118 * virtual timer is now running for the primary again.
119 */
120 if (vcpu->vm->id == HF_PRIMARY_VM_ID) {
121 write_msr(cnthp_ctl_el2, 0);
122 write_msr(cnthp_cval_el2, 0);
123 }
124}
125
Andrew Scullc960c032018-10-24 15:13:35 +0100126/**
127 * This should never be reached as it means something has gone very wrong.
128 */
129static noreturn void hang(void)
130{
131 dlog("Hang: something went very wrong!\n");
132 for (;;) {
133 /* Do nothing. */
134 }
135}
136
137noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100138{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000139 (void)elr;
140 (void)spsr;
141
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100142 dlog("IRQ from current\n");
Andrew Scullc960c032018-10-24 15:13:35 +0100143 hang();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100144}
145
Andrew Scullc960c032018-10-24 15:13:35 +0100146noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100147{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000148 (void)elr;
149 (void)spsr;
150
151 dlog("FIQ from current\n");
Andrew Scullc960c032018-10-24 15:13:35 +0100152 hang();
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000153}
154
Andrew Scullc960c032018-10-24 15:13:35 +0100155noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000156{
157 (void)elr;
158 (void)spsr;
159
160 dlog("SERR from current\n");
Andrew Scullc960c032018-10-24 15:13:35 +0100161 hang();
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000162}
163
Andrew Scullc960c032018-10-24 15:13:35 +0100164noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000165{
166 uintreg_t esr = read_msr(esr_el2);
167
168 (void)spsr;
169
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100170 switch (esr >> 26) {
171 case 0x25: /* EC = 100101, Data abort. */
Andrew Scull4f170f52018-07-19 12:58:20 +0100172 dlog("Data abort: pc=0x%x, esr=0x%x, ec=0x%x", elr, esr,
173 esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100174 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull0a029e82018-11-23 16:48:08 +0000175 dlog(", far=0x%x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100176 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100177 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100178 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100179
180 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000181 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100182
183 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100184 dlog("Unknown current sync exception pc=0x%x, esr=0x%x, "
185 "ec=0x%x\n",
186 elr, esr, esr >> 26);
Andrew Scullc960c032018-10-24 15:13:35 +0100187 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100188 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000189
Andrew Scullc960c032018-10-24 15:13:35 +0100190 hang();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100191}
192
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100193/**
194 * Handles PSCI requests received via HVC or SMC instructions from the primary
195 * VM only.
196 *
Andrew Scullc960c032018-10-24 15:13:35 +0100197 * A minimal PSCI 1.1 interface is offered which can make use of previous
198 * version of PSCI in EL3 by acting as an adapter.
199 *
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100200 * Returns true if the request was a PSCI one, false otherwise.
201 */
Andrew Scull37402872018-10-24 14:23:06 +0100202static bool psci_handler(uint32_t func, uintreg_t arg0, uintreg_t arg1,
203 uintreg_t arg2, int32_t *ret)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100204{
205 struct cpu *c;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100206
Andrew Scullc960c032018-10-24 15:13:35 +0100207 /*
208 * If there's a problem with the EL3 PSCI, block standard secure service
209 * calls by marking them as unknown. Other calls will be allowed to pass
210 * through.
211 *
212 * This blocks more calls than just PSCI so it may need to be made more
213 * lenient in future.
214 */
215 if (el3_psci_version == 0) {
216 *ret = SMCCC_RETURN_UNKNOWN;
217 return (func & SMCCC_SERVICE_CALL_MASK) ==
218 SMCCC_STANDARD_SECURE_SERVICE_CALL;
219 }
220
221 switch (func & ~SMCCC_CONVENTION_MASK) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100222 case PSCI_VERSION:
Andrew Scullc960c032018-10-24 15:13:35 +0100223 *ret = PSCI_VERSION_1_1;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100224 break;
225
Andrew Scullc960c032018-10-24 15:13:35 +0100226 case PSCI_FEATURES:
227 switch (arg0 & ~SMCCC_CONVENTION_MASK) {
228 case PSCI_CPU_SUSPEND:
229 if (el3_psci_version == PSCI_VERSION_0_2) {
230 /*
231 * PSCI 0.2 doesn't support PSCI_FEATURES so
232 * report PSCI 0.2 compatible features.
233 */
234 *ret = 0;
235 } else {
236 /* PSCI 1.x only defines two feature bits. */
237 *ret = smc(func, arg0, 0, 0) & 0x3;
238 }
239 break;
240
241 case PSCI_VERSION:
242 case PSCI_FEATURES:
243 case PSCI_SYSTEM_OFF:
244 case PSCI_SYSTEM_RESET:
245 case PSCI_AFFINITY_INFO:
246 case PSCI_CPU_OFF:
247 case PSCI_CPU_ON:
248 /* These are supported without special features. */
249 *ret = 0;
250 break;
251
252 default:
253 /* Everything else is unsupported. */
254 *ret = PSCI_RETURN_NOT_SUPPORTED;
255 break;
256 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100257 break;
258
259 case PSCI_SYSTEM_OFF:
260 smc(PSCI_SYSTEM_OFF, 0, 0, 0);
Andrew Scullc960c032018-10-24 15:13:35 +0100261 hang();
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100262 break;
263
264 case PSCI_SYSTEM_RESET:
265 smc(PSCI_SYSTEM_RESET, 0, 0, 0);
Andrew Scullc960c032018-10-24 15:13:35 +0100266 hang();
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100267 break;
268
269 case PSCI_AFFINITY_INFO:
270 c = cpu_find(arg0);
271 if (!c) {
272 *ret = PSCI_RETURN_INVALID_PARAMETERS;
273 break;
274 }
275
276 if (arg1 != 0) {
277 *ret = PSCI_RETURN_NOT_SUPPORTED;
278 break;
279 }
280
281 sl_lock(&c->lock);
282 if (c->is_on) {
283 *ret = 0; /* ON */
284 } else {
285 *ret = 1; /* OFF */
286 }
287 sl_unlock(&c->lock);
288 break;
289
Andrew Scullc960c032018-10-24 15:13:35 +0100290 case PSCI_CPU_SUSPEND: {
291 /*
292 * Update vcpu state to wake from the provided entry point but
293 * if suspend returns, for example because it failed or was a
294 * standby power state, the SMC will return and the updated
295 * vcpu registers will be ignored.
296 */
297 struct vcpu *vcpu = current();
298
299 arch_regs_set_pc_arg(&vcpu->regs, ipa_init(arg1), arg2);
300 *ret = smc(PSCI_CPU_SUSPEND | SMCCC_64_BIT, arg0,
301 (uintreg_t)&cpu_entry, (uintreg_t)vcpu->cpu);
302 break;
303 }
304
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100305 case PSCI_CPU_OFF:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100306 cpu_off(current()->cpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100307 smc(PSCI_CPU_OFF, 0, 0, 0);
Andrew Scullc960c032018-10-24 15:13:35 +0100308 hang();
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100309 break;
310
311 case PSCI_CPU_ON:
312 c = cpu_find(arg0);
313 if (!c) {
314 *ret = PSCI_RETURN_INVALID_PARAMETERS;
315 break;
316 }
317
Andrew Scull1b8d0442018-08-06 15:47:04 +0100318 if (cpu_on(c, ipa_init(arg1), arg2)) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100319 *ret = PSCI_RETURN_ALREADY_ON;
320 break;
321 }
322
323 /*
324 * There's a race when turning a CPU on when it's in the
325 * process of turning off. We need to loop here while it is
326 * reported that the CPU is on (because it's about to turn
327 * itself off).
328 */
329 do {
Andrew Scullc960c032018-10-24 15:13:35 +0100330 *ret = smc(PSCI_CPU_ON | SMCCC_64_BIT, arg0,
331 (uintreg_t)&cpu_entry, (uintreg_t)c);
332 } while (*ret == PSCI_RETURN_ALREADY_ON);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100333
Andrew Scullc960c032018-10-24 15:13:35 +0100334 if (*ret != PSCI_RETURN_SUCCESS) {
335 cpu_off(c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100336 }
337 break;
338
Andrew Scullc960c032018-10-24 15:13:35 +0100339 case PSCI_MIGRATE:
340 case PSCI_MIGRATE_INFO_TYPE:
341 case PSCI_MIGRATE_INFO_UP_CPU:
342 case PSCI_CPU_FREEZE:
343 case PSCI_CPU_DEFAULT_SUSPEND:
344 case PSCI_NODE_HW_STATE:
345 case PSCI_SYSTEM_SUSPEND:
346 case PSCI_SET_SYSPEND_MODE:
347 case PSCI_STAT_RESIDENCY:
348 case PSCI_STAT_COUNT:
349 case PSCI_SYSTEM_RESET2:
350 case PSCI_MEM_PROTECT:
351 case PSCI_MEM_PROTECT_CHECK_RANGE:
352 /* Block all other known PSCI calls. */
353 *ret = PSCI_RETURN_NOT_SUPPORTED;
354 break;
355
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100356 default:
357 return false;
358 }
359
360 return true;
361}
362
Andrew Walbran3d84a262018-12-13 14:41:19 +0000363/**
364 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
365 * arch_regs.
366 */
367static void set_virtual_interrupt(struct arch_regs *r, bool enable)
368{
369 if (enable) {
370 r->lazy.hcr_el2 |= HCR_EL2_VI;
371 } else {
372 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
373 }
374}
375
376/**
377 * Sets or clears the VI bit in the HCR_EL2 register.
378 */
379static void set_virtual_interrupt_current(bool enable)
380{
381 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000382
Andrew Walbran3d84a262018-12-13 14:41:19 +0000383 if (enable) {
384 hcr_el2 |= HCR_EL2_VI;
385 } else {
386 hcr_el2 &= ~HCR_EL2_VI;
387 }
388 write_msr(hcr_el2, hcr_el2);
389}
390
Andrew Scull37402872018-10-24 14:23:06 +0100391struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1,
392 uintreg_t arg2, uintreg_t arg3)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100393{
394 struct hvc_handler_return ret;
395
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100396 ret.new = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100397
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100398 if (current()->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullc0e569a2018-10-02 18:05:21 +0100399 int32_t psci_ret;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000400
Andrew Scullc0e569a2018-10-02 18:05:21 +0100401 if (psci_handler(arg0, arg1, arg2, arg3, &psci_ret)) {
402 ret.user_ret = psci_ret;
403 return ret;
404 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100405 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100406
Andrew Scullc960c032018-10-24 15:13:35 +0100407 switch ((uint32_t)arg0 & ~SMCCC_CONVENTION_MASK) {
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000408 case HF_VM_GET_ID:
409 ret.user_ret = api_vm_get_id(current());
410 break;
411
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100412 case HF_VM_GET_COUNT:
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100413 ret.user_ret = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100414 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100415
416 case HF_VCPU_GET_COUNT:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100417 ret.user_ret = api_vcpu_get_count(arg1, current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100418 break;
419
420 case HF_VCPU_RUN:
Andrew Scull6d2db332018-10-10 15:28:17 +0100421 ret.user_ret = hf_vcpu_run_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100422 api_vcpu_run(arg1, arg2, current(), &ret.new));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100423 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100424
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000425 case HF_VCPU_YIELD:
426 ret.user_ret = 0;
427 ret.new = api_yield(current());
428 break;
429
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100430 case HF_VM_CONFIGURE:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100431 ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2),
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000432 current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100433 break;
434
Andrew Scullaa039b32018-10-04 15:02:26 +0100435 case HF_MAILBOX_SEND:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100436 ret.user_ret =
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000437 api_mailbox_send(arg1, arg2, arg3, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100438 break;
439
Andrew Scullaa039b32018-10-04 15:02:26 +0100440 case HF_MAILBOX_RECEIVE:
Andrew Scull6d2db332018-10-10 15:28:17 +0100441 ret.user_ret = hf_mailbox_receive_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100442 api_mailbox_receive(arg1, current(), &ret.new));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100443 break;
444
Andrew Scullaa039b32018-10-04 15:02:26 +0100445 case HF_MAILBOX_CLEAR:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000446 ret.user_ret = api_mailbox_clear(current(), &ret.new);
447 break;
448
449 case HF_MAILBOX_WRITABLE_GET:
450 ret.user_ret = api_mailbox_writable_get(current());
451 break;
452
453 case HF_MAILBOX_WAITER_GET:
454 ret.user_ret = api_mailbox_waiter_get(arg1, current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100455 break;
456
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000457 case HF_INTERRUPT_ENABLE:
458 ret.user_ret = api_interrupt_enable(arg1, arg2, current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000459 break;
460
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000461 case HF_INTERRUPT_GET:
462 ret.user_ret = api_interrupt_get(current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000463 break;
464
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000465 case HF_INTERRUPT_INJECT:
466 ret.user_ret = api_interrupt_inject(arg1, arg2, arg3, current(),
Andrew Walbran318f5732018-11-20 16:23:42 +0000467 &ret.new);
468 break;
469
Andrew Scull6386f252018-12-06 13:29:10 +0000470 case HF_SHARE_MEMORY:
471 ret.user_ret =
472 api_share_memory(arg1 >> 32, ipa_init(arg2), arg3,
473 arg1 & 0xffffffff, current());
474 break;
475
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100476 default:
477 ret.user_ret = -1;
478 }
479
Andrew Walbran3d84a262018-12-13 14:41:19 +0000480 /* Set or clear VI bit. */
481 if (ret.new == NULL) {
482 /*
483 * Not switching vCPUs, set the bit for the current vCPU
484 * directly in the register.
485 */
486 set_virtual_interrupt_current(
487 current()->interrupts.enabled_and_pending_count > 0);
488 } else {
489 /*
490 * About to switch vCPUs, set the bit for the vCPU to which we
491 * are switching in the saved copy of the register.
492 */
493 set_virtual_interrupt(
494 &ret.new->regs,
495 ret.new->interrupts.enabled_and_pending_count > 0);
496 }
497
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100498 return ret;
499}
500
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100501struct vcpu *irq_lower(void)
502{
Andrew Scull9726c252019-01-23 13:44:19 +0000503 /*
504 * Switch back to primary VM, interrupts will be handled there.
505 *
506 * If the VM has aborted, this vCPU will be aborted when the scheduler
507 * tries to run it again. This means the interrupt will not be delayed
508 * by the aborted VM.
509 *
510 * TODO: Only switch when the interrupt isn't for the current VM.
511 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000512 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100513}
514
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000515struct vcpu *fiq_lower(void)
516{
517 return irq_lower();
518}
519
520struct vcpu *serr_lower(void)
521{
522 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000523 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000524}
525
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000526/**
527 * Initialises a fault info structure. It assumes that an FnV bit exists at
528 * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of
529 * the ESR (the fault status code) are 010000; this is the case for both
530 * instruction and data aborts, but not necessarily for other exception reasons.
531 */
532static struct vcpu_fault_info fault_info_init(uintreg_t esr,
533 const struct vcpu *vcpu, int mode,
534 uint8_t size)
535{
536 uint32_t fsc = esr & 0x3f;
537 struct vcpu_fault_info r;
538
539 r.mode = mode;
540 r.size = size;
541 r.pc = va_init(vcpu->regs.pc);
542
543 /*
544 * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It
545 * indicates that we cannot rely on far_el2.
546 */
547 if (fsc == 0x10 && esr & (1u << 10)) {
548 r.vaddr = va_init(0);
549 r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8);
550 } else {
551 r.vaddr = va_init(read_msr(far_el2));
552 r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) |
553 (read_msr(far_el2) & (PAGE_SIZE - 1)));
554 }
555
556 return r;
557}
558
Andrew Scull37402872018-10-24 14:23:06 +0100559struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100560{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100561 struct vcpu *vcpu = current();
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000562 struct vcpu_fault_info info;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100563
564 switch (esr >> 26) {
565 case 0x01: /* EC = 000001, WFI or WFE. */
Andrew Walbran48196eb2019-03-04 14:56:24 +0000566 /* Skip the instruction. */
567 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100568 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100569 if (esr & 1) {
Andrew Walbran48196eb2019-03-04 14:56:24 +0000570 /* WFE */
571 /*
572 * TODO: consider giving the scheduler more context,
573 * somehow.
574 */
575 return api_yield(vcpu);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100576 }
Andrew Walbran48196eb2019-03-04 14:56:24 +0000577 /* WFI */
Andrew Scull9726c252019-01-23 13:44:19 +0000578 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100579
580 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000581 /*
582 * Determine the size based on the SAS bits, which are only
583 * valid if the ISV bit is set. The WnR bit is used to decide
584 * if it's a read or write.
585 */
586 info = fault_info_init(
587 esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R,
588 (esr & (1u << 24)) ? (1u << ((esr >> 22) & 0x3)) : 0);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100589
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000590 /* Call the platform-independent handler. */
591 if (vcpu_handle_page_fault(vcpu, &info)) {
592 return NULL;
593 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000594 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100595
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100596 case 0x20: /* EC = 100000, Instruction abort. */
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000597 /* Determine the size based on the IL bit. */
598 info = fault_info_init(esr, vcpu, MM_MODE_X,
599 (esr & (1u << 25)) ? 4 : 2);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100600
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000601 /* Call the platform-independent handler. */
602 if (vcpu_handle_page_fault(vcpu, &info)) {
603 return NULL;
604 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000605 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100606
Andrew Scullc960c032018-10-24 15:13:35 +0100607 case 0x17: /* EC = 010111, SMC instruction. */ {
608 uintreg_t smc_pc = vcpu->regs.pc;
609 int32_t ret;
610
Andrew Scull19503262018-09-20 14:48:39 +0100611 if (vcpu->vm->id != HF_PRIMARY_VM_ID ||
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100612 !psci_handler(vcpu->regs.r[0], vcpu->regs.r[1],
613 vcpu->regs.r[2], vcpu->regs.r[3], &ret)) {
614 dlog("Unsupported SMC call: 0x%x\n", vcpu->regs.r[0]);
Andrew Scullc960c032018-10-24 15:13:35 +0100615 ret = PSCI_RETURN_NOT_SUPPORTED;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100616 }
617
618 /* Skip the SMC instruction. */
Andrew Scullc960c032018-10-24 15:13:35 +0100619 vcpu->regs.pc = smc_pc + (esr & (1u << 25) ? 4 : 2);
Andrew Scull9726c252019-01-23 13:44:19 +0000620 vcpu->regs.r[0] = ret;
621 return NULL;
Andrew Scullc960c032018-10-24 15:13:35 +0100622 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100623
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100624 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100625 dlog("Unknown lower sync exception pc=0x%x, esr=0x%x, "
626 "ec=0x%x\n",
Andrew Scull4f170f52018-07-19 12:58:20 +0100627 vcpu->regs.pc, esr, esr >> 26);
Andrew Scull9726c252019-01-23 13:44:19 +0000628 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100629 }
630
Andrew Scull9726c252019-01-23 13:44:19 +0000631 /* The exception wasn't handled so abort the VM. */
632 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100633}