blob: 39b3010d0d058a621dac970b3e88bd621b147318 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
3 *
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 Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/api.h"
18#include "hf/cpu.h"
19#include "hf/dlog.h"
20#include "hf/vm.h"
21
Andrew Scullf35a5c92018-08-07 18:09:46 +010022#include "vmapi/hf/call.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010023
24#include "msr.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "psci.h"
Andrew Scull7fd4bb72018-12-08 23:40:12 +000026#include "smc.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027
Andrew Walbran3d84a262018-12-13 14:41:19 +000028#define HCR_EL2_VI (1u << 7)
29
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010030struct hvc_handler_return {
Andrew Scull37402872018-10-24 14:23:06 +010031 uintreg_t user_ret;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010032 struct vcpu *new;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010033};
34
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010035void cpu_entry(struct cpu *c);
36
Andrew Walbran3d84a262018-12-13 14:41:19 +000037static inline struct vcpu *current(void)
38{
39 return (struct vcpu *)read_msr(tpidr_el2);
40}
41
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000042void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010043{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000044 (void)elr;
45 (void)spsr;
46
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010047 dlog("IRQ from current\n");
Andrew Scull7364a8e2018-07-19 15:39:29 +010048 for (;;) {
49 /* do nothing */
50 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010051}
52
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000053void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010054{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000055 (void)elr;
56 (void)spsr;
57
58 dlog("FIQ from current\n");
59 for (;;) {
60 /* do nothing */
61 }
62}
63
64void serr_current_exception(uintreg_t elr, uintreg_t spsr)
65{
66 (void)elr;
67 (void)spsr;
68
69 dlog("SERR from current\n");
70 for (;;) {
71 /* do nothing */
72 }
73}
74
75void sync_current_exception(uintreg_t elr, uintreg_t spsr)
76{
77 uintreg_t esr = read_msr(esr_el2);
78
79 (void)spsr;
80
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010081 switch (esr >> 26) {
82 case 0x25: /* EC = 100101, Data abort. */
Andrew Scull4f170f52018-07-19 12:58:20 +010083 dlog("Data abort: pc=0x%x, esr=0x%x, ec=0x%x", elr, esr,
84 esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +010085 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull0a029e82018-11-23 16:48:08 +000086 dlog(", far=0x%x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +010087 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010088 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +010089 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010090
91 dlog("\n");
Andrew Scull7364a8e2018-07-19 15:39:29 +010092 for (;;) {
93 /* do nothing */
94 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010095
96 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010097 dlog("Unknown current sync exception pc=0x%x, esr=0x%x, "
98 "ec=0x%x\n",
99 elr, esr, esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100100 for (;;) {
101 /* do nothing */
102 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100103 }
Andrew Scull7364a8e2018-07-19 15:39:29 +0100104 for (;;) {
105 /* do nothing */
106 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100107}
108
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100109/**
110 * Handles PSCI requests received via HVC or SMC instructions from the primary
111 * VM only.
112 *
113 * Returns true if the request was a PSCI one, false otherwise.
114 */
Andrew Scull37402872018-10-24 14:23:06 +0100115static bool psci_handler(uint32_t func, uintreg_t arg0, uintreg_t arg1,
116 uintreg_t arg2, int32_t *ret)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100117{
118 struct cpu *c;
119 int32_t sret;
120
121 switch (func & ~PSCI_CONVENTION_MASK) {
122 case PSCI_VERSION:
123 /* Version is 0.2. */
124 *ret = 2;
125 break;
126
127 case PSCI_MIGRATE_INFO_TYPE:
128 /* Trusted OS does not require migration. */
129 *ret = 2;
130 break;
131
132 case PSCI_SYSTEM_OFF:
133 smc(PSCI_SYSTEM_OFF, 0, 0, 0);
134 for (;;) {
135 }
136 break;
137
138 case PSCI_SYSTEM_RESET:
139 smc(PSCI_SYSTEM_RESET, 0, 0, 0);
140 for (;;) {
141 }
142 break;
143
144 case PSCI_AFFINITY_INFO:
145 c = cpu_find(arg0);
146 if (!c) {
147 *ret = PSCI_RETURN_INVALID_PARAMETERS;
148 break;
149 }
150
151 if (arg1 != 0) {
152 *ret = PSCI_RETURN_NOT_SUPPORTED;
153 break;
154 }
155
156 sl_lock(&c->lock);
157 if (c->is_on) {
158 *ret = 0; /* ON */
159 } else {
160 *ret = 1; /* OFF */
161 }
162 sl_unlock(&c->lock);
163 break;
164
165 case PSCI_CPU_OFF:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100166 cpu_off(current()->cpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100167 smc(PSCI_CPU_OFF, 0, 0, 0);
168 for (;;) {
169 }
170 break;
171
172 case PSCI_CPU_ON:
173 c = cpu_find(arg0);
174 if (!c) {
175 *ret = PSCI_RETURN_INVALID_PARAMETERS;
176 break;
177 }
178
Andrew Scull1b8d0442018-08-06 15:47:04 +0100179 if (cpu_on(c, ipa_init(arg1), arg2)) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100180 *ret = PSCI_RETURN_ALREADY_ON;
181 break;
182 }
183
184 /*
185 * There's a race when turning a CPU on when it's in the
186 * process of turning off. We need to loop here while it is
187 * reported that the CPU is on (because it's about to turn
188 * itself off).
189 */
190 do {
Andrew Scull37402872018-10-24 14:23:06 +0100191 sret = smc(PSCI_CPU_ON, arg0, (uintreg_t)&cpu_entry,
192 (uintreg_t)c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100193 } while (sret == PSCI_RETURN_ALREADY_ON);
194
195 if (sret == PSCI_RETURN_SUCCESS) {
196 *ret = PSCI_RETURN_SUCCESS;
197 } else {
198 dlog("Unexpected return from PSCI_CPU_ON: 0x%x\n",
199 sret);
200 *ret = PSCI_RETURN_INTERNAL_FAILURE;
201 }
202 break;
203
204 default:
205 return false;
206 }
207
208 return true;
209}
210
Andrew Walbran3d84a262018-12-13 14:41:19 +0000211/**
212 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
213 * arch_regs.
214 */
215static void set_virtual_interrupt(struct arch_regs *r, bool enable)
216{
217 if (enable) {
218 r->lazy.hcr_el2 |= HCR_EL2_VI;
219 } else {
220 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
221 }
222}
223
224/**
225 * Sets or clears the VI bit in the HCR_EL2 register.
226 */
227static void set_virtual_interrupt_current(bool enable)
228{
229 uintreg_t hcr_el2 = read_msr(hcr_el2);
230 if (enable) {
231 hcr_el2 |= HCR_EL2_VI;
232 } else {
233 hcr_el2 &= ~HCR_EL2_VI;
234 }
235 write_msr(hcr_el2, hcr_el2);
236}
237
Andrew Scull37402872018-10-24 14:23:06 +0100238struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1,
239 uintreg_t arg2, uintreg_t arg3)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100240{
241 struct hvc_handler_return ret;
242
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100243 ret.new = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100244
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100245 if (current()->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullc0e569a2018-10-02 18:05:21 +0100246 int32_t psci_ret;
247 if (psci_handler(arg0, arg1, arg2, arg3, &psci_ret)) {
248 ret.user_ret = psci_ret;
249 return ret;
250 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100251 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100252
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100253 switch ((uint32_t)arg0 & ~PSCI_CONVENTION_MASK) {
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000254 case HF_VM_GET_ID:
255 ret.user_ret = api_vm_get_id(current());
256 break;
257
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100258 case HF_VM_GET_COUNT:
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100259 ret.user_ret = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100260 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100261
262 case HF_VCPU_GET_COUNT:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100263 ret.user_ret = api_vcpu_get_count(arg1, current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100264 break;
265
266 case HF_VCPU_RUN:
Andrew Scull6d2db332018-10-10 15:28:17 +0100267 ret.user_ret = hf_vcpu_run_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100268 api_vcpu_run(arg1, arg2, current(), &ret.new));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100269 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100270
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000271 case HF_VCPU_YIELD:
272 ret.user_ret = 0;
273 ret.new = api_yield(current());
274 break;
275
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100276 case HF_VM_CONFIGURE:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100277 ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2),
278 current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100279 break;
280
Andrew Scullaa039b32018-10-04 15:02:26 +0100281 case HF_MAILBOX_SEND:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100282 ret.user_ret =
283 api_mailbox_send(arg1, arg2, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100284 break;
285
Andrew Scullaa039b32018-10-04 15:02:26 +0100286 case HF_MAILBOX_RECEIVE:
Andrew Scull6d2db332018-10-10 15:28:17 +0100287 ret.user_ret = hf_mailbox_receive_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100288 api_mailbox_receive(arg1, current(), &ret.new));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100289 break;
290
Andrew Scullaa039b32018-10-04 15:02:26 +0100291 case HF_MAILBOX_CLEAR:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100292 ret.user_ret = api_mailbox_clear(current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100293 break;
294
Andrew Walbran318f5732018-11-20 16:23:42 +0000295 case HF_ENABLE_INTERRUPT:
296 ret.user_ret = api_enable_interrupt(arg1, arg2, current());
297 break;
298
299 case HF_GET_AND_ACKNOWLEDGE_INTERRUPT:
300 ret.user_ret = api_get_and_acknowledge_interrupt(current());
301 break;
302
303 case HF_INJECT_INTERRUPT:
304 ret.user_ret = api_inject_interrupt(arg1, arg2, arg3, current(),
305 &ret.new);
306 break;
307
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100308 default:
309 ret.user_ret = -1;
310 }
311
Andrew Walbran3d84a262018-12-13 14:41:19 +0000312 /* Set or clear VI bit. */
313 if (ret.new == NULL) {
314 /*
315 * Not switching vCPUs, set the bit for the current vCPU
316 * directly in the register.
317 */
318 set_virtual_interrupt_current(
319 current()->interrupts.enabled_and_pending_count > 0);
320 } else {
321 /*
322 * About to switch vCPUs, set the bit for the vCPU to which we
323 * are switching in the saved copy of the register.
324 */
325 set_virtual_interrupt(
326 &ret.new->regs,
327 ret.new->interrupts.enabled_and_pending_count > 0);
328 }
329
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100330 return ret;
331}
332
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100333struct vcpu *irq_lower(void)
334{
335 /* TODO: Only switch if we know the interrupt was not for the secondary
336 * VM. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100337 /* Switch back to primary VM, interrupts will be handled there. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100338 return api_yield(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100339}
340
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000341struct vcpu *fiq_lower(void)
342{
343 return irq_lower();
344}
345
346struct vcpu *serr_lower(void)
347{
348 dlog("SERR from lower\n");
349 for (;;) {
350 /* do nothing */
351 }
352}
353
Andrew Scull37402872018-10-24 14:23:06 +0100354struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100355{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100356 struct vcpu *vcpu = current();
Andrew Scullc0e569a2018-10-02 18:05:21 +0100357 int32_t ret;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100358
359 switch (esr >> 26) {
360 case 0x01: /* EC = 000001, WFI or WFE. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100361 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100362 if (esr & 1) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100363 return NULL;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100364 }
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100365 return api_wait_for_interrupt(current());
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100366
367 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000368 dlog("Lower data abort: pc=0x%x, esr=0x%x, ec=0x%x, vmid=%u, "
369 "vcpu=%u",
370 vcpu->regs.pc, esr, esr >> 26, vcpu->vm->id,
371 vcpu_index(vcpu));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100372 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull4f170f52018-07-19 12:58:20 +0100373 dlog(", far=0x%x, hpfar=0x%x", read_msr(far_el2),
374 read_msr(hpfar_el2) << 8);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100375 } else {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100376 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100377 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100378
379 dlog("\n");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100380 for (;;) {
381 /* do nothing */
382 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100383
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100384 case 0x20: /* EC = 100000, Instruction abort. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000385 dlog("Lower instruction abort: pc=0x%x, esr=0x%x, ec=0x%x, "
386 "vmdid=%u, vcpu=%u",
387 vcpu->regs.pc, esr, esr >> 26, vcpu->vm->id,
388 vcpu_index(vcpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100389 if (!(esr & (1u << 10))) { /* Check FnV bit. */
390 dlog(", far=0x%x, hpfar=0x%x", read_msr(far_el2),
391 read_msr(hpfar_el2) << 8);
392 } else {
393 dlog(", far=invalid");
394 }
395
396 dlog(", vttbr_el2=0x%x", read_msr(vttbr_el2));
397 dlog("\n");
398 for (;;) {
399 /* do nothing */
400 }
401
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100402 case 0x17: /* EC = 010111, SMC instruction. */
Andrew Scull19503262018-09-20 14:48:39 +0100403 if (vcpu->vm->id != HF_PRIMARY_VM_ID ||
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100404 !psci_handler(vcpu->regs.r[0], vcpu->regs.r[1],
405 vcpu->regs.r[2], vcpu->regs.r[3], &ret)) {
406 dlog("Unsupported SMC call: 0x%x\n", vcpu->regs.r[0]);
407 ret = -1;
408 }
409
410 /* Skip the SMC instruction. */
411 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
412 break;
413
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100414 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100415 dlog("Unknown lower sync exception pc=0x%x, esr=0x%x, "
416 "ec=0x%x\n",
Andrew Scull4f170f52018-07-19 12:58:20 +0100417 vcpu->regs.pc, esr, esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100418 for (;;) {
419 /* do nothing */
420 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100421 }
422
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100423 vcpu->regs.r[0] = ret;
424
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100425 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100426}