blob: 990b212b2153f8a7fb9ceed3338d2967ababeab9 [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
28struct hvc_handler_return {
Andrew Scull37402872018-10-24 14:23:06 +010029 uintreg_t user_ret;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010030 struct vcpu *new;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010031};
32
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010033void cpu_entry(struct cpu *c);
34
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000035void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010036{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000037 (void)elr;
38 (void)spsr;
39
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010040 dlog("IRQ from current\n");
Andrew Scull7364a8e2018-07-19 15:39:29 +010041 for (;;) {
42 /* do nothing */
43 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010044}
45
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000046void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010047{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000048 (void)elr;
49 (void)spsr;
50
51 dlog("FIQ from current\n");
52 for (;;) {
53 /* do nothing */
54 }
55}
56
57void serr_current_exception(uintreg_t elr, uintreg_t spsr)
58{
59 (void)elr;
60 (void)spsr;
61
62 dlog("SERR from current\n");
63 for (;;) {
64 /* do nothing */
65 }
66}
67
68void sync_current_exception(uintreg_t elr, uintreg_t spsr)
69{
70 uintreg_t esr = read_msr(esr_el2);
71
72 (void)spsr;
73
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010074 switch (esr >> 26) {
75 case 0x25: /* EC = 100101, Data abort. */
Andrew Scull4f170f52018-07-19 12:58:20 +010076 dlog("Data abort: pc=0x%x, esr=0x%x, ec=0x%x", elr, esr,
77 esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +010078 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull0a029e82018-11-23 16:48:08 +000079 dlog(", far=0x%x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +010080 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010081 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +010082 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010083
84 dlog("\n");
Andrew Scull7364a8e2018-07-19 15:39:29 +010085 for (;;) {
86 /* do nothing */
87 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010088
89 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010090 dlog("Unknown current sync exception pc=0x%x, esr=0x%x, "
91 "ec=0x%x\n",
92 elr, esr, esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +010093 for (;;) {
94 /* do nothing */
95 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010096 }
Andrew Scull7364a8e2018-07-19 15:39:29 +010097 for (;;) {
98 /* do nothing */
99 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100100}
101
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100102/**
103 * Handles PSCI requests received via HVC or SMC instructions from the primary
104 * VM only.
105 *
106 * Returns true if the request was a PSCI one, false otherwise.
107 */
Andrew Scull37402872018-10-24 14:23:06 +0100108static bool psci_handler(uint32_t func, uintreg_t arg0, uintreg_t arg1,
109 uintreg_t arg2, int32_t *ret)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100110{
111 struct cpu *c;
112 int32_t sret;
113
114 switch (func & ~PSCI_CONVENTION_MASK) {
115 case PSCI_VERSION:
116 /* Version is 0.2. */
117 *ret = 2;
118 break;
119
120 case PSCI_MIGRATE_INFO_TYPE:
121 /* Trusted OS does not require migration. */
122 *ret = 2;
123 break;
124
125 case PSCI_SYSTEM_OFF:
126 smc(PSCI_SYSTEM_OFF, 0, 0, 0);
127 for (;;) {
128 }
129 break;
130
131 case PSCI_SYSTEM_RESET:
132 smc(PSCI_SYSTEM_RESET, 0, 0, 0);
133 for (;;) {
134 }
135 break;
136
137 case PSCI_AFFINITY_INFO:
138 c = cpu_find(arg0);
139 if (!c) {
140 *ret = PSCI_RETURN_INVALID_PARAMETERS;
141 break;
142 }
143
144 if (arg1 != 0) {
145 *ret = PSCI_RETURN_NOT_SUPPORTED;
146 break;
147 }
148
149 sl_lock(&c->lock);
150 if (c->is_on) {
151 *ret = 0; /* ON */
152 } else {
153 *ret = 1; /* OFF */
154 }
155 sl_unlock(&c->lock);
156 break;
157
158 case PSCI_CPU_OFF:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100159 cpu_off(current()->cpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100160 smc(PSCI_CPU_OFF, 0, 0, 0);
161 for (;;) {
162 }
163 break;
164
165 case PSCI_CPU_ON:
166 c = cpu_find(arg0);
167 if (!c) {
168 *ret = PSCI_RETURN_INVALID_PARAMETERS;
169 break;
170 }
171
Andrew Scull1b8d0442018-08-06 15:47:04 +0100172 if (cpu_on(c, ipa_init(arg1), arg2)) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100173 *ret = PSCI_RETURN_ALREADY_ON;
174 break;
175 }
176
177 /*
178 * There's a race when turning a CPU on when it's in the
179 * process of turning off. We need to loop here while it is
180 * reported that the CPU is on (because it's about to turn
181 * itself off).
182 */
183 do {
Andrew Scull37402872018-10-24 14:23:06 +0100184 sret = smc(PSCI_CPU_ON, arg0, (uintreg_t)&cpu_entry,
185 (uintreg_t)c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100186 } while (sret == PSCI_RETURN_ALREADY_ON);
187
188 if (sret == PSCI_RETURN_SUCCESS) {
189 *ret = PSCI_RETURN_SUCCESS;
190 } else {
191 dlog("Unexpected return from PSCI_CPU_ON: 0x%x\n",
192 sret);
193 *ret = PSCI_RETURN_INTERNAL_FAILURE;
194 }
195 break;
196
197 default:
198 return false;
199 }
200
201 return true;
202}
203
Andrew Scull37402872018-10-24 14:23:06 +0100204struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1,
205 uintreg_t arg2, uintreg_t arg3)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100206{
207 struct hvc_handler_return ret;
208
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100209 ret.new = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100210
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100211 if (current()->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullc0e569a2018-10-02 18:05:21 +0100212 int32_t psci_ret;
213 if (psci_handler(arg0, arg1, arg2, arg3, &psci_ret)) {
214 ret.user_ret = psci_ret;
215 return ret;
216 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100217 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100218
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100219 switch ((uint32_t)arg0 & ~PSCI_CONVENTION_MASK) {
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000220 case HF_VM_GET_ID:
221 ret.user_ret = api_vm_get_id(current());
222 break;
223
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100224 case HF_VM_GET_COUNT:
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100225 ret.user_ret = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100226 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100227
228 case HF_VCPU_GET_COUNT:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100229 ret.user_ret = api_vcpu_get_count(arg1, current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100230 break;
231
232 case HF_VCPU_RUN:
Andrew Scull6d2db332018-10-10 15:28:17 +0100233 ret.user_ret = hf_vcpu_run_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100234 api_vcpu_run(arg1, arg2, current(), &ret.new));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100235 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100236
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000237 case HF_VCPU_YIELD:
238 ret.user_ret = 0;
239 ret.new = api_yield(current());
240 break;
241
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100242 case HF_VM_CONFIGURE:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100243 ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2),
244 current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100245 break;
246
Andrew Scullaa039b32018-10-04 15:02:26 +0100247 case HF_MAILBOX_SEND:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100248 ret.user_ret =
249 api_mailbox_send(arg1, arg2, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100250 break;
251
Andrew Scullaa039b32018-10-04 15:02:26 +0100252 case HF_MAILBOX_RECEIVE:
Andrew Scull6d2db332018-10-10 15:28:17 +0100253 ret.user_ret = hf_mailbox_receive_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100254 api_mailbox_receive(arg1, current(), &ret.new));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100255 break;
256
Andrew Scullaa039b32018-10-04 15:02:26 +0100257 case HF_MAILBOX_CLEAR:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100258 ret.user_ret = api_mailbox_clear(current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100259 break;
260
Andrew Walbran318f5732018-11-20 16:23:42 +0000261 case HF_ENABLE_INTERRUPT:
262 ret.user_ret = api_enable_interrupt(arg1, arg2, current());
263 break;
264
265 case HF_GET_AND_ACKNOWLEDGE_INTERRUPT:
266 ret.user_ret = api_get_and_acknowledge_interrupt(current());
267 break;
268
269 case HF_INJECT_INTERRUPT:
270 ret.user_ret = api_inject_interrupt(arg1, arg2, arg3, current(),
271 &ret.new);
272 break;
273
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100274 default:
275 ret.user_ret = -1;
276 }
277
278 return ret;
279}
280
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100281struct vcpu *irq_lower(void)
282{
283 /* TODO: Only switch if we know the interrupt was not for the secondary
284 * VM. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100285 /* Switch back to primary VM, interrupts will be handled there. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100286 return api_yield(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100287}
288
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000289struct vcpu *fiq_lower(void)
290{
291 return irq_lower();
292}
293
294struct vcpu *serr_lower(void)
295{
296 dlog("SERR from lower\n");
297 for (;;) {
298 /* do nothing */
299 }
300}
301
Andrew Scull37402872018-10-24 14:23:06 +0100302struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100303{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100304 struct vcpu *vcpu = current();
Andrew Scullc0e569a2018-10-02 18:05:21 +0100305 int32_t ret;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100306
307 switch (esr >> 26) {
308 case 0x01: /* EC = 000001, WFI or WFE. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100309 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100310 if (esr & 1) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100311 return NULL;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100312 }
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100313 return api_wait_for_interrupt(current());
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100314
315 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000316 dlog("Lower data abort: pc=0x%x, esr=0x%x, ec=0x%x, vmid=%u, "
317 "vcpu=%u",
318 vcpu->regs.pc, esr, esr >> 26, vcpu->vm->id,
319 vcpu_index(vcpu));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100320 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull4f170f52018-07-19 12:58:20 +0100321 dlog(", far=0x%x, hpfar=0x%x", read_msr(far_el2),
322 read_msr(hpfar_el2) << 8);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100323 } else {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100324 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100325 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100326
327 dlog("\n");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100328 for (;;) {
329 /* do nothing */
330 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100331
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100332 case 0x20: /* EC = 100000, Instruction abort. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000333 dlog("Lower instruction abort: pc=0x%x, esr=0x%x, ec=0x%x, "
334 "vmdid=%u, vcpu=%u",
335 vcpu->regs.pc, esr, esr >> 26, vcpu->vm->id,
336 vcpu_index(vcpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100337 if (!(esr & (1u << 10))) { /* Check FnV bit. */
338 dlog(", far=0x%x, hpfar=0x%x", read_msr(far_el2),
339 read_msr(hpfar_el2) << 8);
340 } else {
341 dlog(", far=invalid");
342 }
343
344 dlog(", vttbr_el2=0x%x", read_msr(vttbr_el2));
345 dlog("\n");
346 for (;;) {
347 /* do nothing */
348 }
349
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100350 case 0x17: /* EC = 010111, SMC instruction. */
Andrew Scull19503262018-09-20 14:48:39 +0100351 if (vcpu->vm->id != HF_PRIMARY_VM_ID ||
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100352 !psci_handler(vcpu->regs.r[0], vcpu->regs.r[1],
353 vcpu->regs.r[2], vcpu->regs.r[3], &ret)) {
354 dlog("Unsupported SMC call: 0x%x\n", vcpu->regs.r[0]);
355 ret = -1;
356 }
357
358 /* Skip the SMC instruction. */
359 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
360 break;
361
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100362 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100363 dlog("Unknown lower sync exception pc=0x%x, esr=0x%x, "
364 "ec=0x%x\n",
Andrew Scull4f170f52018-07-19 12:58:20 +0100365 vcpu->regs.pc, esr, esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100366 for (;;) {
367 /* do nothing */
368 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100369 }
370
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100371 vcpu->regs.r[0] = ret;
372
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100373 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100374}