blob: bdc3d46c2bf2d2af0f4f79ecfe96c82b8896c6fb [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"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010026
27struct hvc_handler_return {
Andrew Scull37402872018-10-24 14:23:06 +010028 uintreg_t user_ret;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010029 struct vcpu *new;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010030};
31
Andrew Scull37402872018-10-24 14:23:06 +010032int32_t smc(uintreg_t arg0, uintreg_t arg1, uintreg_t arg2, uintreg_t arg3);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010033void cpu_entry(struct cpu *c);
34
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010035static struct vcpu *current(void)
36{
37 return (struct vcpu *)read_msr(tpidr_el2);
38}
39
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000040void irq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010041{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000042 (void)elr;
43 (void)spsr;
44
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010045 dlog("IRQ from current\n");
Andrew Scull7364a8e2018-07-19 15:39:29 +010046 for (;;) {
47 /* do nothing */
48 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010049}
50
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000051void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010052{
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000053 (void)elr;
54 (void)spsr;
55
56 dlog("FIQ from current\n");
57 for (;;) {
58 /* do nothing */
59 }
60}
61
62void serr_current_exception(uintreg_t elr, uintreg_t spsr)
63{
64 (void)elr;
65 (void)spsr;
66
67 dlog("SERR from current\n");
68 for (;;) {
69 /* do nothing */
70 }
71}
72
73void sync_current_exception(uintreg_t elr, uintreg_t spsr)
74{
75 uintreg_t esr = read_msr(esr_el2);
76
77 (void)spsr;
78
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010079 switch (esr >> 26) {
80 case 0x25: /* EC = 100101, Data abort. */
Andrew Scull4f170f52018-07-19 12:58:20 +010081 dlog("Data abort: pc=0x%x, esr=0x%x, ec=0x%x", elr, esr,
82 esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +010083 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull0a029e82018-11-23 16:48:08 +000084 dlog(", far=0x%x", read_msr(far_el2));
Andrew Scull7364a8e2018-07-19 15:39:29 +010085 } else {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010086 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +010087 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010088
89 dlog("\n");
Andrew Scull7364a8e2018-07-19 15:39:29 +010090 for (;;) {
91 /* do nothing */
92 }
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);
Andrew Scull7364a8e2018-07-19 15:39:29 +010098 for (;;) {
99 /* do nothing */
100 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100101 }
Andrew Scull7364a8e2018-07-19 15:39:29 +0100102 for (;;) {
103 /* do nothing */
104 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100105}
106
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100107/**
108 * Handles PSCI requests received via HVC or SMC instructions from the primary
109 * VM only.
110 *
111 * Returns true if the request was a PSCI one, false otherwise.
112 */
Andrew Scull37402872018-10-24 14:23:06 +0100113static bool psci_handler(uint32_t func, uintreg_t arg0, uintreg_t arg1,
114 uintreg_t arg2, int32_t *ret)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100115{
116 struct cpu *c;
117 int32_t sret;
118
119 switch (func & ~PSCI_CONVENTION_MASK) {
120 case PSCI_VERSION:
121 /* Version is 0.2. */
122 *ret = 2;
123 break;
124
125 case PSCI_MIGRATE_INFO_TYPE:
126 /* Trusted OS does not require migration. */
127 *ret = 2;
128 break;
129
130 case PSCI_SYSTEM_OFF:
131 smc(PSCI_SYSTEM_OFF, 0, 0, 0);
132 for (;;) {
133 }
134 break;
135
136 case PSCI_SYSTEM_RESET:
137 smc(PSCI_SYSTEM_RESET, 0, 0, 0);
138 for (;;) {
139 }
140 break;
141
142 case PSCI_AFFINITY_INFO:
143 c = cpu_find(arg0);
144 if (!c) {
145 *ret = PSCI_RETURN_INVALID_PARAMETERS;
146 break;
147 }
148
149 if (arg1 != 0) {
150 *ret = PSCI_RETURN_NOT_SUPPORTED;
151 break;
152 }
153
154 sl_lock(&c->lock);
155 if (c->is_on) {
156 *ret = 0; /* ON */
157 } else {
158 *ret = 1; /* OFF */
159 }
160 sl_unlock(&c->lock);
161 break;
162
163 case PSCI_CPU_OFF:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100164 cpu_off(current()->cpu);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100165 smc(PSCI_CPU_OFF, 0, 0, 0);
166 for (;;) {
167 }
168 break;
169
170 case PSCI_CPU_ON:
171 c = cpu_find(arg0);
172 if (!c) {
173 *ret = PSCI_RETURN_INVALID_PARAMETERS;
174 break;
175 }
176
Andrew Scull1b8d0442018-08-06 15:47:04 +0100177 if (cpu_on(c, ipa_init(arg1), arg2)) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100178 *ret = PSCI_RETURN_ALREADY_ON;
179 break;
180 }
181
182 /*
183 * There's a race when turning a CPU on when it's in the
184 * process of turning off. We need to loop here while it is
185 * reported that the CPU is on (because it's about to turn
186 * itself off).
187 */
188 do {
Andrew Scull37402872018-10-24 14:23:06 +0100189 sret = smc(PSCI_CPU_ON, arg0, (uintreg_t)&cpu_entry,
190 (uintreg_t)c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100191 } while (sret == PSCI_RETURN_ALREADY_ON);
192
193 if (sret == PSCI_RETURN_SUCCESS) {
194 *ret = PSCI_RETURN_SUCCESS;
195 } else {
196 dlog("Unexpected return from PSCI_CPU_ON: 0x%x\n",
197 sret);
198 *ret = PSCI_RETURN_INTERNAL_FAILURE;
199 }
200 break;
201
202 default:
203 return false;
204 }
205
206 return true;
207}
208
Andrew Scull37402872018-10-24 14:23:06 +0100209struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1,
210 uintreg_t arg2, uintreg_t arg3)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100211{
212 struct hvc_handler_return ret;
213
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100214 ret.new = NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100215
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100216 if (current()->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullc0e569a2018-10-02 18:05:21 +0100217 int32_t psci_ret;
218 if (psci_handler(arg0, arg1, arg2, arg3, &psci_ret)) {
219 ret.user_ret = psci_ret;
220 return ret;
221 }
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100222 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100223
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100224 switch ((uint32_t)arg0 & ~PSCI_CONVENTION_MASK) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100225 case HF_VM_GET_COUNT:
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100226 ret.user_ret = api_vm_get_count();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100227 break;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100228
229 case HF_VCPU_GET_COUNT:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100230 ret.user_ret = api_vcpu_get_count(arg1, current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100231 break;
232
233 case HF_VCPU_RUN:
Andrew Scull6d2db332018-10-10 15:28:17 +0100234 ret.user_ret = hf_vcpu_run_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100235 api_vcpu_run(arg1, arg2, current(), &ret.new));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100236 break;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100237
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100238 case HF_VM_CONFIGURE:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100239 ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2),
240 current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100241 break;
242
Andrew Scullaa039b32018-10-04 15:02:26 +0100243 case HF_MAILBOX_SEND:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100244 ret.user_ret =
245 api_mailbox_send(arg1, arg2, current(), &ret.new);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100246 break;
247
Andrew Scullaa039b32018-10-04 15:02:26 +0100248 case HF_MAILBOX_RECEIVE:
Andrew Scull6d2db332018-10-10 15:28:17 +0100249 ret.user_ret = hf_mailbox_receive_return_encode(
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100250 api_mailbox_receive(arg1, current(), &ret.new));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100251 break;
252
Andrew Scullaa039b32018-10-04 15:02:26 +0100253 case HF_MAILBOX_CLEAR:
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100254 ret.user_ret = api_mailbox_clear(current());
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100255 break;
256
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100257 default:
258 ret.user_ret = -1;
259 }
260
261 return ret;
262}
263
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100264struct vcpu *irq_lower(void)
265{
266 /* TODO: Only switch if we know the interrupt was not for the secondary
267 * VM. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100268 /* Switch back to primary VM, interrupts will be handled there. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100269 return api_yield(current());
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100270}
271
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000272struct vcpu *fiq_lower(void)
273{
274 return irq_lower();
275}
276
277struct vcpu *serr_lower(void)
278{
279 dlog("SERR from lower\n");
280 for (;;) {
281 /* do nothing */
282 }
283}
284
Andrew Scull37402872018-10-24 14:23:06 +0100285struct vcpu *sync_lower_exception(uintreg_t esr)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100286{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100287 struct vcpu *vcpu = current();
Andrew Scullc0e569a2018-10-02 18:05:21 +0100288 int32_t ret;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100289
290 switch (esr >> 26) {
291 case 0x01: /* EC = 000001, WFI or WFE. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100292 /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100293 if (esr & 1) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100294 return NULL;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100295 }
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100296 return api_wait_for_interrupt(current());
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100297
298 case 0x24: /* EC = 100100, Data abort. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000299 dlog("Lower data abort: pc=0x%x, esr=0x%x, ec=0x%x, vmid=%u, "
300 "vcpu=%u",
301 vcpu->regs.pc, esr, esr >> 26, vcpu->vm->id,
302 vcpu_index(vcpu));
Andrew Scull7364a8e2018-07-19 15:39:29 +0100303 if (!(esr & (1u << 10))) { /* Check FnV bit. */
Andrew Scull4f170f52018-07-19 12:58:20 +0100304 dlog(", far=0x%x, hpfar=0x%x", read_msr(far_el2),
305 read_msr(hpfar_el2) << 8);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100306 } else {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100307 dlog(", far=invalid");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100308 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100309
310 dlog("\n");
Andrew Scull7364a8e2018-07-19 15:39:29 +0100311 for (;;) {
312 /* do nothing */
313 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100314
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100315 case 0x20: /* EC = 100000, Instruction abort. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000316 dlog("Lower instruction abort: pc=0x%x, esr=0x%x, ec=0x%x, "
317 "vmdid=%u, vcpu=%u",
318 vcpu->regs.pc, esr, esr >> 26, vcpu->vm->id,
319 vcpu_index(vcpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100320 if (!(esr & (1u << 10))) { /* Check FnV bit. */
321 dlog(", far=0x%x, hpfar=0x%x", read_msr(far_el2),
322 read_msr(hpfar_el2) << 8);
323 } else {
324 dlog(", far=invalid");
325 }
326
327 dlog(", vttbr_el2=0x%x", read_msr(vttbr_el2));
328 dlog("\n");
329 for (;;) {
330 /* do nothing */
331 }
332
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100333 case 0x17: /* EC = 010111, SMC instruction. */
Andrew Scull19503262018-09-20 14:48:39 +0100334 if (vcpu->vm->id != HF_PRIMARY_VM_ID ||
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100335 !psci_handler(vcpu->regs.r[0], vcpu->regs.r[1],
336 vcpu->regs.r[2], vcpu->regs.r[3], &ret)) {
337 dlog("Unsupported SMC call: 0x%x\n", vcpu->regs.r[0]);
338 ret = -1;
339 }
340
341 /* Skip the SMC instruction. */
342 vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2;
343 break;
344
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100345 default:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100346 dlog("Unknown lower sync exception pc=0x%x, esr=0x%x, "
347 "ec=0x%x\n",
Andrew Scull4f170f52018-07-19 12:58:20 +0100348 vcpu->regs.pc, esr, esr >> 26);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100349 for (;;) {
350 /* do nothing */
351 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100352 }
353
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100354 vcpu->regs.r[0] = ret;
355
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100356 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100357}