blob: f491227cf04747bd26ffbb9570c89f540b75d9f4 [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");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000092 break;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010093
94 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010095 dlog("Unknown current sync exception pc=0x%x, esr=0x%x, "
96 "ec=0x%x\n",
97 elr, esr, esr >> 26);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010098 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000099
Andrew Scull7364a8e2018-07-19 15:39:29 +0100100 for (;;) {
101 /* do nothing */
102 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100103}
104
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100105/**
106 * Handles PSCI requests received via HVC or SMC instructions from the primary
107 * VM only.
108 *
109 * Returns true if the request was a PSCI one, false otherwise.
110 */
Andrew Scull37402872018-10-24 14:23:06 +0100111static bool psci_handler(uint32_t func, uintreg_t arg0, uintreg_t arg1,
112 uintreg_t arg2, int32_t *ret)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100113{
114 struct cpu *c;
115 int32_t sret;
116
117 switch (func & ~PSCI_CONVENTION_MASK) {
118 case PSCI_VERSION:
119 /* Version is 0.2. */
120 *ret = 2;
121 break;
122
123 case PSCI_MIGRATE_INFO_TYPE:
124 /* Trusted OS does not require migration. */
125 *ret = 2;
126 break;
127
128 case PSCI_SYSTEM_OFF:
129 smc(PSCI_SYSTEM_OFF, 0, 0, 0);
130 for (;;) {
131 }
132 break;
133
134 case PSCI_SYSTEM_RESET:
135 smc(PSCI_SYSTEM_RESET, 0, 0, 0);
136 for (;;) {
137 }
138 break;
139
140 case PSCI_AFFINITY_INFO:
141 c = cpu_find(arg0);
142 if (!c) {
143 *ret = PSCI_RETURN_INVALID_PARAMETERS;
144 break;
145 }
146
147 if (arg1 != 0) {
148 *ret = PSCI_RETURN_NOT_SUPPORTED;
149 break;
150 }
151
152 sl_lock(&c->lock);
153 if (c->is_on) {
154 *ret = 0; /* ON */
155 } else {
156 *ret = 1; /* OFF */
157 }
158 sl_unlock(&c->lock);
159 break;
160
161 case PSCI_CPU_OFF:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100162 cpu_off(current()->cpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100163 smc(PSCI_CPU_OFF, 0, 0, 0);
164 for (;;) {
165 }
166 break;
167
168 case PSCI_CPU_ON:
169 c = cpu_find(arg0);
170 if (!c) {
171 *ret = PSCI_RETURN_INVALID_PARAMETERS;
172 break;
173 }
174
Andrew Scull1b8d0442018-08-06 15:47:04 +0100175 if (cpu_on(c, ipa_init(arg1), arg2)) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100176 *ret = PSCI_RETURN_ALREADY_ON;
177 break;
178 }
179
180 /*
181 * There's a race when turning a CPU on when it's in the
182 * process of turning off. We need to loop here while it is
183 * reported that the CPU is on (because it's about to turn
184 * itself off).
185 */
186 do {
Andrew Scull37402872018-10-24 14:23:06 +0100187 sret = smc(PSCI_CPU_ON, arg0, (uintreg_t)&cpu_entry,
188 (uintreg_t)c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100189 } while (sret == PSCI_RETURN_ALREADY_ON);
190
191 if (sret == PSCI_RETURN_SUCCESS) {
192 *ret = PSCI_RETURN_SUCCESS;
193 } else {
194 dlog("Unexpected return from PSCI_CPU_ON: 0x%x\n",
195 sret);
196 *ret = PSCI_RETURN_INTERNAL_FAILURE;
197 }
198 break;
199
200 default:
201 return false;
202 }
203
204 return true;
205}
206
Andrew Walbran3d84a262018-12-13 14:41:19 +0000207/**
208 * Sets or clears the VI bit in the HCR_EL2 register saved in the given
209 * arch_regs.
210 */
211static void set_virtual_interrupt(struct arch_regs *r, bool enable)
212{
213 if (enable) {
214 r->lazy.hcr_el2 |= HCR_EL2_VI;
215 } else {
216 r->lazy.hcr_el2 &= ~HCR_EL2_VI;
217 }
218}
219
220/**
221 * Sets or clears the VI bit in the HCR_EL2 register.
222 */
223static void set_virtual_interrupt_current(bool enable)
224{
225 uintreg_t hcr_el2 = read_msr(hcr_el2);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000226
Andrew Walbran3d84a262018-12-13 14:41:19 +0000227 if (enable) {
228 hcr_el2 |= HCR_EL2_VI;
229 } else {
230 hcr_el2 &= ~HCR_EL2_VI;
231 }
232 write_msr(hcr_el2, hcr_el2);
233}
234
Andrew Scull37402872018-10-24 14:23:06 +0100235struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1,
236 uintreg_t arg2, uintreg_t arg3)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100237{
238 struct hvc_handler_return ret;
239
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100240 ret.new = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100241
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100242 if (current()->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullc0e569a2018-10-02 18:05:21 +0100243 int32_t psci_ret;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000244
Andrew Scullc0e569a2018-10-02 18:05:21 +0100245 if (psci_handler(arg0, arg1, arg2, arg3, &psci_ret)) {
246 ret.user_ret = psci_ret;
247 return ret;
248 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100249 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100250
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100251 switch ((uint32_t)arg0 & ~PSCI_CONVENTION_MASK) {
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000252 case HF_VM_GET_ID:
253 ret.user_ret = api_vm_get_id(current());
254 break;
255
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100256 case HF_VM_GET_COUNT:
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100257 ret.user_ret = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100258 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100259
260 case HF_VCPU_GET_COUNT:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100261 ret.user_ret = api_vcpu_get_count(arg1, current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100262 break;
263
264 case HF_VCPU_RUN:
Andrew Scull6d2db332018-10-10 15:28:17 +0100265 ret.user_ret = hf_vcpu_run_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100266 api_vcpu_run(arg1, arg2, current(), &ret.new));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100267 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100268
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000269 case HF_VCPU_YIELD:
270 ret.user_ret = 0;
271 ret.new = api_yield(current());
272 break;
273
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100274 case HF_VM_CONFIGURE:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100275 ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2),
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000276 current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100277 break;
278
Andrew Scullaa039b32018-10-04 15:02:26 +0100279 case HF_MAILBOX_SEND:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100280 ret.user_ret =
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000281 api_mailbox_send(arg1, arg2, arg3, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100282 break;
283
Andrew Scullaa039b32018-10-04 15:02:26 +0100284 case HF_MAILBOX_RECEIVE:
Andrew Scull6d2db332018-10-10 15:28:17 +0100285 ret.user_ret = hf_mailbox_receive_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100286 api_mailbox_receive(arg1, current(), &ret.new));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100287 break;
288
Andrew Scullaa039b32018-10-04 15:02:26 +0100289 case HF_MAILBOX_CLEAR:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000290 ret.user_ret = api_mailbox_clear(current(), &ret.new);
291 break;
292
293 case HF_MAILBOX_WRITABLE_GET:
294 ret.user_ret = api_mailbox_writable_get(current());
295 break;
296
297 case HF_MAILBOX_WAITER_GET:
298 ret.user_ret = api_mailbox_waiter_get(arg1, current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100299 break;
300
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000301 case HF_INTERRUPT_ENABLE:
302 ret.user_ret = api_interrupt_enable(arg1, arg2, current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000303 break;
304
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000305 case HF_INTERRUPT_GET:
306 ret.user_ret = api_interrupt_get(current());
Andrew Walbran318f5732018-11-20 16:23:42 +0000307 break;
308
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000309 case HF_INTERRUPT_INJECT:
310 ret.user_ret = api_interrupt_inject(arg1, arg2, arg3, current(),
Andrew Walbran318f5732018-11-20 16:23:42 +0000311 &ret.new);
312 break;
313
Andrew Scull6386f252018-12-06 13:29:10 +0000314 case HF_SHARE_MEMORY:
315 ret.user_ret =
316 api_share_memory(arg1 >> 32, ipa_init(arg2), arg3,
317 arg1 & 0xffffffff, current());
318 break;
319
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100320 default:
321 ret.user_ret = -1;
322 }
323
Andrew Walbran3d84a262018-12-13 14:41:19 +0000324 /* Set or clear VI bit. */
325 if (ret.new == NULL) {
326 /*
327 * Not switching vCPUs, set the bit for the current vCPU
328 * directly in the register.
329 */
330 set_virtual_interrupt_current(
331 current()->interrupts.enabled_and_pending_count > 0);
332 } else {
333 /*
334 * About to switch vCPUs, set the bit for the vCPU to which we
335 * are switching in the saved copy of the register.
336 */
337 set_virtual_interrupt(
338 &ret.new->regs,
339 ret.new->interrupts.enabled_and_pending_count > 0);
340 }
341
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100342 return ret;
343}
344
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100345struct vcpu *irq_lower(void)
346{
Andrew Scull9726c252019-01-23 13:44:19 +0000347 /*
348 * Switch back to primary VM, interrupts will be handled there.
349 *
350 * If the VM has aborted, this vCPU will be aborted when the scheduler
351 * tries to run it again. This means the interrupt will not be delayed
352 * by the aborted VM.
353 *
354 * TODO: Only switch when the interrupt isn't for the current VM.
355 */
Andrew Scull33fecd32019-01-08 14:48:27 +0000356 return api_preempt(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100357}
358
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000359struct vcpu *fiq_lower(void)
360{
361 return irq_lower();
362}
363
364struct vcpu *serr_lower(void)
365{
366 dlog("SERR from lower\n");
Andrew Scull9726c252019-01-23 13:44:19 +0000367 return api_abort(current());
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000368}
369
Andrew Scull37402872018-10-24 14:23:06 +0100370struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100371{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100372 struct vcpu *vcpu = current();
Andrew Scullc0e569a2018-10-02 18:05:21 +0100373 int32_t ret;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100374
375 switch (esr >> 26) {
376 case 0x01: /* EC = 000001, WFI or WFE. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100377 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100378 if (esr & 1) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100379 return NULL;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100380 }
Andrew Walbran6b98b852018-12-21 14:35:19 +0000381 /* Skip the WFI instruction. */
382 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
Andrew Scull9726c252019-01-23 13:44:19 +0000383 return api_wait_for_interrupt(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100384
385 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000386 dlog("Lower data abort: pc=0x%x, esr=0x%x, ec=0x%x, vmid=%u, "
387 "vcpu=%u",
388 vcpu->regs.pc, esr, esr >> 26, vcpu->vm->id,
389 vcpu_index(vcpu));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100390 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull4f170f52018-07-19 12:58:20 +0100391 dlog(", far=0x%x, hpfar=0x%x", read_msr(far_el2),
392 read_msr(hpfar_el2) << 8);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100393 } else {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100394 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100395 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100396
397 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000398 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100399
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100400 case 0x20: /* EC = 100000, Instruction abort. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000401 dlog("Lower instruction abort: pc=0x%x, esr=0x%x, ec=0x%x, "
402 "vmdid=%u, vcpu=%u",
403 vcpu->regs.pc, esr, esr >> 26, vcpu->vm->id,
404 vcpu_index(vcpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100405 if (!(esr & (1u << 10))) { /* Check FnV bit. */
406 dlog(", far=0x%x, hpfar=0x%x", read_msr(far_el2),
407 read_msr(hpfar_el2) << 8);
408 } else {
409 dlog(", far=invalid");
410 }
411
412 dlog(", vttbr_el2=0x%x", read_msr(vttbr_el2));
413 dlog("\n");
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000414 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100415
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100416 case 0x17: /* EC = 010111, SMC instruction. */
Andrew Scull19503262018-09-20 14:48:39 +0100417 if (vcpu->vm->id != HF_PRIMARY_VM_ID ||
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100418 !psci_handler(vcpu->regs.r[0], vcpu->regs.r[1],
419 vcpu->regs.r[2], vcpu->regs.r[3], &ret)) {
420 dlog("Unsupported SMC call: 0x%x\n", vcpu->regs.r[0]);
421 ret = -1;
422 }
423
424 /* Skip the SMC instruction. */
425 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
Andrew Scull9726c252019-01-23 13:44:19 +0000426 vcpu->regs.r[0] = ret;
427 return NULL;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100428
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100429 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100430 dlog("Unknown lower sync exception pc=0x%x, esr=0x%x, "
431 "ec=0x%x\n",
Andrew Scull4f170f52018-07-19 12:58:20 +0100432 vcpu->regs.pc, esr, esr >> 26);
Andrew Scull9726c252019-01-23 13:44:19 +0000433 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100434 }
435
Andrew Scull9726c252019-01-23 13:44:19 +0000436 /* The exception wasn't handled so abort the VM. */
437 return api_abort(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100438}