blob: 004122f24cd3d2782d36c1353283b5fb7e05a314 [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"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010018
Andrew Scull13652af2018-09-17 14:49:08 +010019#include <assert.h>
20
Andrew Walbran318f5732018-11-20 16:23:42 +000021#include "hf/arch/cpu.h"
22
23#include "hf/dlog.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/std.h"
25#include "hf/vm.h"
26
Andrew Scullf35a5c92018-08-07 18:09:46 +010027#include "vmapi/hf/call.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010028
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000029/*
30 * To eliminate the risk of deadlocks, we define a partial order for the
31 * acquisition of locks held concurrently by the same physical CPU. Our current
32 * ordering requirements are as follows:
33 *
34 * vm::lock -> vcpu::lock
35 */
36
Andrew Scullaa039b32018-10-04 15:02:26 +010037static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010038 "Currently, a page is mapped for the send and receive buffers so "
39 "the maximum request is the size of a page.");
40
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000041static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000042
43/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000044 * Initialises the API page pool by taking ownership of the contents of the
45 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000046 */
47void api_init(struct mpool *ppool)
48{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000049 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000050}
51
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010052/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010053 * Switches the physical CPU back to the corresponding vcpu of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010054 *
55 * This triggers the scheduling logic to run. Run in the context of secondary VM
56 * to cause HF_VCPU_RUN to return and the primary VM to regain control of the
57 * cpu.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010058 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010059static struct vcpu *api_switch_to_primary(struct vcpu *current,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000060 struct hf_vcpu_run_return primary_ret,
61 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010062{
Andrew Scull19503262018-09-20 14:48:39 +010063 struct vm *primary = vm_get(HF_PRIMARY_VM_ID);
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010064 struct vcpu *next = &primary->vcpus[cpu_index(current->cpu)];
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010065
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010066 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Scull6d2db332018-10-10 15:28:17 +010067 arch_regs_set_retval(&next->regs,
68 hf_vcpu_run_return_encode(primary_ret));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010069
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000070 /* Mark the current vcpu as waiting. */
71 sl_lock(&current->lock);
72 current->state = secondary_state;
73 sl_unlock(&current->lock);
74
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010075 return next;
76}
77
78/**
Andrew Scullaa039b32018-10-04 15:02:26 +010079 * Returns to the primary vm leaving the current vcpu ready to be scheduled
80 * again.
81 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010082struct vcpu *api_yield(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +010083{
Andrew Scull6d2db332018-10-10 15:28:17 +010084 struct hf_vcpu_run_return ret = {
85 .code = HF_VCPU_RUN_YIELD,
86 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000087
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000088 return api_switch_to_primary(current, ret, vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +010089}
90
91/**
92 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
93 * vm.
94 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010095struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +010096{
Andrew Scull6d2db332018-10-10 15:28:17 +010097 struct hf_vcpu_run_return ret = {
98 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
99 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000100
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000101 return api_switch_to_primary(current, ret,
102 vcpu_state_blocked_interrupt);
Andrew Scullaa039b32018-10-04 15:02:26 +0100103}
104
105/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000106 * Returns the ID of the VM.
107 */
108int64_t api_vm_get_id(const struct vcpu *current)
109{
110 return current->vm->id;
111}
112
113/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100114 * Returns the number of VMs configured to run.
115 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100116int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100117{
Andrew Scull19503262018-09-20 14:48:39 +0100118 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100119}
120
121/**
122 * Returns the number of vcpus configured in the given VM.
123 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100124int64_t api_vcpu_get_count(uint32_t vm_id, const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100125{
Andrew Scull19503262018-09-20 14:48:39 +0100126 struct vm *vm;
127
128 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100129 if (current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100130 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100131 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100132
Andrew Scull19503262018-09-20 14:48:39 +0100133 vm = vm_get(vm_id);
134 if (vm == NULL) {
135 return -1;
136 }
137
138 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100139}
140
141/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000142 * This function is called by the architecture-specific context switching
143 * function to indicate that register state for the given vcpu has been saved
144 * and can therefore be used by other pcpus.
145 */
146void api_regs_state_saved(struct vcpu *vcpu)
147{
148 sl_lock(&vcpu->lock);
149 vcpu->regs_available = true;
150 sl_unlock(&vcpu->lock);
151}
152
153/**
154 * Prepares the vcpu to run by updating its state and fetching whether a return
155 * value needs to be forced onto the vCPU.
156 */
157static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
158 struct retval_state *vcpu_retval)
159{
160 bool ret;
161
162 sl_lock(&vcpu->lock);
163 if (vcpu->state != vcpu_state_ready) {
164 ret = false;
165 goto out;
166 }
167
168 vcpu->cpu = current->cpu;
169 vcpu->state = vcpu_state_running;
170
171 /* Fetch return value to inject into vCPU if there is one. */
172 *vcpu_retval = vcpu->retval;
173 if (vcpu_retval->force) {
174 vcpu->retval.force = false;
175 }
176
177 /*
178 * Wait until the registers become available. Care must be taken when
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000179 * looping on this: it shouldn't be done while holding other locks to
180 * avoid deadlocks.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000181 */
182 while (!vcpu->regs_available) {
183 sl_unlock(&vcpu->lock);
184 sl_lock(&vcpu->lock);
185 }
186
187 /*
188 * Mark the registers as unavailable now that we're about to reflect
189 * them onto the real registers. This will also prevent another physical
190 * CPU from trying to read these registers.
191 */
192 vcpu->regs_available = false;
193
194 ret = true;
195
196out:
197 sl_unlock(&vcpu->lock);
198 return ret;
199}
200
201/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100202 * Runs the given vcpu of the given vm.
203 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100204struct hf_vcpu_run_return api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100205 const struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100206 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100207{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100208 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100209 struct vcpu *vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000210 struct retval_state vcpu_retval;
Andrew Scull6d2db332018-10-10 15:28:17 +0100211 struct hf_vcpu_run_return ret = {
212 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
213 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100214
215 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100216 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100217 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100218 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100219
Andrew Scull19503262018-09-20 14:48:39 +0100220 /* Only secondary VM vcpus can be run. */
221 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100222 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100223 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100224
Andrew Scull19503262018-09-20 14:48:39 +0100225 /* The requested VM must exist. */
226 vm = vm_get(vm_id);
227 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100228 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100229 }
230
231 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100232 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100233 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100234 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100235
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000236 /* Update state if allowed. */
Andrew Scullf3d45592018-09-20 14:30:22 +0100237 vcpu = &vm->vcpus[vcpu_idx];
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000238 if (!api_vcpu_prepare_run(current, vcpu, &vcpu_retval)) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100239 ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000240 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100241 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000242
243 *next = vcpu;
244 ret.code = HF_VCPU_RUN_YIELD;
245
246 /* Update return value if one was injected. */
247 if (vcpu_retval.force) {
248 arch_regs_set_retval(&vcpu->regs, vcpu_retval.value);
249 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100250
Andrew Scull6d2db332018-10-10 15:28:17 +0100251out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100252 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100253}
254
255/**
Andrew Scull81e85092018-12-12 12:56:20 +0000256 * Check that the mode indicates memory that is valid, owned and exclusive.
257 */
258bool static api_mode_valid_owned_and_exclusive(int mode)
259{
260 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
261 0;
262}
263
264/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100265 * Configures the VM to send/receive data through the specified pages. The pages
266 * must not be shared.
267 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100268int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv,
269 const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100270{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100271 struct vm *vm = current->vm;
Andrew Scull80871322018-08-06 12:04:09 +0100272 paddr_t pa_send_begin;
273 paddr_t pa_send_end;
274 paddr_t pa_recv_begin;
275 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000276 int orig_send_mode;
277 int orig_recv_mode;
278 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100279 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100280
281 /* Fail if addresses are not page-aligned. */
Andrew Scull265ada92018-07-30 15:19:01 +0100282 if ((ipa_addr(send) & (PAGE_SIZE - 1)) ||
283 (ipa_addr(recv) & (PAGE_SIZE - 1))) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100284 return -1;
285 }
286
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000287 /* Convert to physical addresses. */
288 pa_send_begin = pa_from_ipa(send);
289 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
290
291 pa_recv_begin = pa_from_ipa(recv);
292 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
293
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100294 /* Fail if the same page is used for the send and receive pages. */
295 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000296 return -1;
297 }
298
299 sl_lock(&vm->lock);
300
301 /* We only allow these to be setup once. */
302 if (vm->mailbox.send || vm->mailbox.recv) {
303 goto fail;
304 }
305
306 /*
307 * Ensure the pages are valid, owned and exclusive to the VM and that
308 * the VM has the required access to the memory.
309 */
310 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
311 &orig_send_mode) ||
312 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
313 (orig_send_mode & MM_MODE_R) == 0 ||
314 (orig_send_mode & MM_MODE_W) == 0) {
315 goto fail;
316 }
317
318 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
319 &orig_recv_mode) ||
320 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
321 (orig_recv_mode & MM_MODE_R) == 0) {
322 goto fail;
323 }
324
325 /*
326 * Create a local pool so any freed memory can't be used by another
327 * thread. This is to ensure the original mapping can be restored if any
328 * stage of the process fails.
329 */
330 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
331
332 /* Take memory ownership away from the VM and mark as shared. */
333 if (!mm_vm_identity_map(
334 &vm->ptable, pa_send_begin, pa_send_end,
335 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
336 NULL, &local_page_pool)) {
337 goto fail_free_pool;
338 }
339
340 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
341 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
342 NULL, &local_page_pool)) {
343 /* TODO: partial defrag of failed range. */
344 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000345 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000346 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100347 }
348
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100349 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000350 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000351 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100352 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000353 /* TODO: partial defrag of failed range. */
354 /* Recover any memory consumed in failed mapping. */
355 mm_defrag(&local_page_pool);
356 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100357 }
358
359 /*
360 * Map the receive page as writable in the hypervisor address space. On
361 * failure, unmap the send page before returning.
362 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000363 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000364 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100365 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000366 /* TODO: partial defrag of failed range. */
367 /* Recover any memory consumed in failed mapping. */
368 mm_defrag(&local_page_pool);
369 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100370 }
371
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100372 /* TODO: Notify any waiters. */
373
374 ret = 0;
Andrew Scull220e6212018-12-21 18:09:00 +0000375 goto exit;
376
377 /*
378 * The following mappings will not require more memory than is available
379 * in the local pool.
380 */
381fail_undo_all:
382 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000383 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000384
385fail_undo_send_and_recv:
386 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
387 orig_recv_mode, NULL, &local_page_pool);
388
389fail_undo_send:
390 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
391 orig_send_mode, NULL, &local_page_pool);
392
393fail_free_pool:
394 mpool_fini(&local_page_pool);
395
396fail:
397 ret = -1;
398
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100399exit:
400 sl_unlock(&vm->lock);
401
402 return ret;
403}
404
405/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100406 * Copies data from the sender's send buffer to the recipient's receive buffer
407 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000408 *
409 * If the recipient's receive buffer is busy, it can optionally register the
410 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100411 */
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000412int64_t api_mailbox_send(uint32_t vm_id, size_t size, bool notify,
413 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100414{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100415 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100416 struct vm *to;
417 const void *from_buf;
Andrew Scullaa039b32018-10-04 15:02:26 +0100418 uint16_t vcpu;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100419 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100420
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000421 (void)notify;
422
Andrew Scullaa039b32018-10-04 15:02:26 +0100423 /* Limit the size of transfer. */
424 if (size > HF_MAILBOX_SIZE) {
Andrew Scull19503262018-09-20 14:48:39 +0100425 return -1;
426 }
427
428 /* Disallow reflexive requests as this suggests an error in the VM. */
429 if (vm_id == from->id) {
430 return -1;
431 }
432
433 /* Ensure the target VM exists. */
434 to = vm_get(vm_id);
435 if (to == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100436 return -1;
437 }
438
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100439 /*
440 * Check that the sender has configured its send buffer. It is safe to
441 * use from_buf after releasing the lock because the buffer cannot be
442 * modified once it's configured.
443 */
444 sl_lock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100445 from_buf = from->mailbox.send;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100446 sl_unlock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100447 if (from_buf == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100448 return -1;
449 }
450
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100451 sl_lock(&to->lock);
452
Andrew Scullaa039b32018-10-04 15:02:26 +0100453 if (to->mailbox.state != mailbox_state_empty ||
454 to->mailbox.recv == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100455 /* Fail if the target isn't currently ready to receive data. */
456 ret = -1;
Andrew Scullaa039b32018-10-04 15:02:26 +0100457 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100458 }
459
Andrew Scullaa039b32018-10-04 15:02:26 +0100460 /* Copy data. */
461 memcpy(to->mailbox.recv, from_buf, size);
462 to->mailbox.recv_bytes = size;
463 to->mailbox.recv_from_id = from->id;
464 to->mailbox.state = mailbox_state_read;
465
466 /* Messages for the primary VM are delivered directly. */
467 if (to->id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000468 struct hf_vcpu_run_return primary_ret = {
469 .code = HF_VCPU_RUN_MESSAGE,
470 .message.size = size,
471 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000472
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000473 *next = api_switch_to_primary(current, primary_ret,
474 vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100475 ret = 0;
476 goto out;
477 }
478
479 /*
480 * Try to find a vcpu to handle the message and tell the scheduler to
481 * run it.
482 */
483 if (to->mailbox.recv_waiter == NULL) {
484 /*
485 * The scheduler must choose a vcpu to interrupt so it can
486 * handle the message.
487 */
488 to->mailbox.state = mailbox_state_received;
489 vcpu = HF_INVALID_VCPU;
490 } else {
491 struct vcpu *to_vcpu = to->mailbox.recv_waiter;
492
493 /*
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000494 * Take target vcpu out of waiter list and mark it as ready to
495 * run again.
Andrew Scullaa039b32018-10-04 15:02:26 +0100496 */
497 sl_lock(&to_vcpu->lock);
498 to->mailbox.recv_waiter = to_vcpu->mailbox_next;
499 to_vcpu->state = vcpu_state_ready;
500
501 /* Return from HF_MAILBOX_RECEIVE. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000502 to_vcpu->retval.force = true;
503 to_vcpu->retval.value = hf_mailbox_receive_return_encode(
504 (struct hf_mailbox_receive_return){
505 .vm_id = to->mailbox.recv_from_id,
506 .size = size,
507 });
Andrew Scullaa039b32018-10-04 15:02:26 +0100508
509 sl_unlock(&to_vcpu->lock);
510
511 vcpu = to_vcpu - to->vcpus;
512 }
513
514 /* Return to the primary VM directly or with a switch. */
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000515 if (from->id == HF_PRIMARY_VM_ID) {
516 ret = vcpu;
517 } else {
518 struct hf_vcpu_run_return primary_ret = {
519 .code = HF_VCPU_RUN_WAKE_UP,
520 .wake_up.vm_id = to->id,
521 .wake_up.vcpu = vcpu,
522 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000523
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000524 *next = api_switch_to_primary(current, primary_ret,
525 vcpu_state_ready);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000526 ret = 0;
527 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100528
529out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100530 sl_unlock(&to->lock);
531
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000532 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100533}
534
535/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100536 * Receives a message from the mailbox. If one isn't available, this function
537 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100538 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100539 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100540 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100541struct hf_mailbox_receive_return api_mailbox_receive(bool block,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100542 struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100543 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100544{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100545 struct vm *vm = current->vm;
Andrew Scull6d2db332018-10-10 15:28:17 +0100546 struct hf_mailbox_receive_return ret = {
547 .vm_id = HF_INVALID_VM_ID,
548 };
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100549
Andrew Scullaa039b32018-10-04 15:02:26 +0100550 /*
551 * The primary VM will receive messages as a status code from running
552 * vcpus and must not call this function.
553 */
Andrew Scull19503262018-09-20 14:48:39 +0100554 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100555 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100556 }
557
558 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100559
Andrew Scullaa039b32018-10-04 15:02:26 +0100560 /* Return pending messages without blocking. */
561 if (vm->mailbox.state == mailbox_state_received) {
562 vm->mailbox.state = mailbox_state_read;
Andrew Scull6d2db332018-10-10 15:28:17 +0100563 ret.vm_id = vm->mailbox.recv_from_id;
564 ret.size = vm->mailbox.recv_bytes;
Andrew Scullaa039b32018-10-04 15:02:26 +0100565 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100566 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100567
568 /* No pending message so fail if not allowed to block. */
569 if (!block) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100570 goto out;
571 }
572
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100573 sl_lock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100574
575 /* Push vcpu into waiter list. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100576 current->mailbox_next = vm->mailbox.recv_waiter;
577 vm->mailbox.recv_waiter = current;
578 sl_unlock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100579
580 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000581 {
582 struct hf_vcpu_run_return run_return = {
583 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
584 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000585
Andrew Walbranb4816552018-12-05 17:35:42 +0000586 *next = api_switch_to_primary(current, run_return,
587 vcpu_state_blocked_mailbox);
588 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100589out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100590 sl_unlock(&vm->lock);
591
592 return ret;
593}
594
595/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100596 * Clears the caller's mailbox so that a new message can be received. The caller
597 * must have copied out all data they wish to preserve as new messages will
598 * overwrite the old and will arrive asynchronously.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100599 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100600int64_t api_mailbox_clear(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100601{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100602 struct vm *vm = current->vm;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100603 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100604
605 sl_lock(&vm->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100606 if (vm->mailbox.state == mailbox_state_read) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100607 ret = 0;
Andrew Scullaa039b32018-10-04 15:02:26 +0100608 vm->mailbox.state = mailbox_state_empty;
609 } else {
610 ret = -1;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100611 }
612 sl_unlock(&vm->lock);
613
614 if (ret == 0) {
615 /* TODO: Notify waiters, if any. */
616 }
617
618 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100619}
Andrew Walbran318f5732018-11-20 16:23:42 +0000620
621/**
622 * Enables or disables a given interrupt ID for the calling vCPU.
623 *
624 * Returns 0 on success, or -1 if the intid is invalid.
625 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000626int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000627{
628 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
629 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000630
Andrew Walbran318f5732018-11-20 16:23:42 +0000631 if (intid >= HF_NUM_INTIDS) {
632 return -1;
633 }
634
635 sl_lock(&current->lock);
636 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000637 /*
638 * If it is pending and was not enabled before, increment the
639 * count.
640 */
641 if (current->interrupts.interrupt_pending[intid_index] &
642 ~current->interrupts.interrupt_enabled[intid_index] &
643 intid_mask) {
644 current->interrupts.enabled_and_pending_count++;
645 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000646 current->interrupts.interrupt_enabled[intid_index] |=
647 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +0000648 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000649 /*
650 * If it is pending and was enabled before, decrement the count.
651 */
652 if (current->interrupts.interrupt_pending[intid_index] &
653 current->interrupts.interrupt_enabled[intid_index] &
654 intid_mask) {
655 current->interrupts.enabled_and_pending_count--;
656 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000657 current->interrupts.interrupt_enabled[intid_index] &=
658 ~intid_mask;
659 }
660
661 sl_unlock(&current->lock);
662 return 0;
663}
664
665/**
666 * Returns the ID of the next pending interrupt for the calling vCPU, and
667 * acknowledges it (i.e. marks it as no longer pending). Returns
668 * HF_INVALID_INTID if there are no pending interrupts.
669 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000670uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000671{
672 uint8_t i;
673 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +0000674
675 /*
676 * Find the first enabled and pending interrupt ID, return it, and
677 * deactivate it.
678 */
679 sl_lock(&current->lock);
680 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
681 uint32_t enabled_and_pending =
682 current->interrupts.interrupt_enabled[i] &
683 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000684
Andrew Walbran318f5732018-11-20 16:23:42 +0000685 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000686 uint8_t bit_index = ctz(enabled_and_pending);
687 /*
688 * Mark it as no longer pending and decrement the count.
689 */
690 current->interrupts.interrupt_pending[i] &=
691 ~(1u << bit_index);
692 current->interrupts.enabled_and_pending_count--;
693 first_interrupt =
694 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +0000695 break;
696 }
697 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000698
699 sl_unlock(&current->lock);
700 return first_interrupt;
701}
702
703/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +0000704 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +0000705 * given VM and vCPU.
706 */
707static inline bool is_injection_allowed(uint32_t target_vm_id,
708 struct vcpu *current)
709{
710 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000711
Andrew Walbran318f5732018-11-20 16:23:42 +0000712 /*
713 * The primary VM is allowed to inject interrupts into any VM. Secondary
714 * VMs are only allowed to inject interrupts into their own vCPUs.
715 */
716 return current_vm_id == HF_PRIMARY_VM_ID ||
717 current_vm_id == target_vm_id;
718}
719
720/**
721 * Injects a virtual interrupt of the given ID into the given target vCPU.
722 * This doesn't cause the vCPU to actually be run immediately; it will be taken
723 * when the vCPU is next run, which is up to the scheduler.
724 *
Andrew Walbran3d84a262018-12-13 14:41:19 +0000725 * Returns:
726 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
727 * ID is invalid, or the current VM is not allowed to inject interrupts to
728 * the target VM.
729 * - 0 on success if no further action is needed.
730 * - 1 if it was called by the primary VM and the primary VM now needs to wake
731 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +0000732 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000733int64_t api_interrupt_inject(uint32_t target_vm_id, uint32_t target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +0000734 uint32_t intid, struct vcpu *current,
735 struct vcpu **next)
736{
737 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
738 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
739 struct vcpu *target_vcpu;
740 struct vm *target_vm = vm_get(target_vm_id);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000741 bool need_vm_lock;
Andrew Walbran3d84a262018-12-13 14:41:19 +0000742 int64_t ret = 0;
Andrew Walbran318f5732018-11-20 16:23:42 +0000743
744 if (intid >= HF_NUM_INTIDS) {
745 return -1;
746 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000747
Andrew Walbran318f5732018-11-20 16:23:42 +0000748 if (target_vm == NULL) {
749 return -1;
750 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000751
Andrew Walbran318f5732018-11-20 16:23:42 +0000752 if (target_vcpu_idx >= target_vm->vcpu_count) {
753 /* The requested vcpu must exist. */
754 return -1;
755 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000756
Andrew Walbran318f5732018-11-20 16:23:42 +0000757 if (!is_injection_allowed(target_vm_id, current)) {
758 return -1;
759 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000760
Andrew Walbran318f5732018-11-20 16:23:42 +0000761 target_vcpu = &target_vm->vcpus[target_vcpu_idx];
762
763 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
764 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
765
766 sl_lock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000767 /*
768 * If we need the target_vm lock we need to release the target_vcpu lock
769 * first to maintain the correct order of locks. In-between releasing
770 * and acquiring it again the state of the vCPU could change in such a
771 * way that we don't actually need to touch the target_vm after all, but
772 * that's alright: we'll take the target_vm lock anyway, but it's safe,
773 * just perhaps a little slow in this unusual case. The reverse is not
774 * possible: if need_vm_lock is false, we don't release the target_vcpu
775 * lock until we are done, so nothing should change in such as way that
776 * we need the VM lock after all.
777 */
Andrew Walbran3d84a262018-12-13 14:41:19 +0000778 need_vm_lock =
779 (target_vcpu->interrupts.interrupt_enabled[intid_index] &
780 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
781 intid_mask) &&
782 target_vcpu->state == vcpu_state_blocked_mailbox;
Andrew Walbran69520dc2018-12-06 11:39:38 +0000783 if (need_vm_lock) {
784 sl_unlock(&target_vcpu->lock);
785 sl_lock(&target_vm->lock);
786 sl_lock(&target_vcpu->lock);
787 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000788
Andrew Walbran3d84a262018-12-13 14:41:19 +0000789 /*
790 * We only need to change state and (maybe) trigger a virtual IRQ if it
791 * is enabled and was not previously pending. Otherwise we can skip
792 * everything except setting the pending bit.
793 *
794 * If you change this logic make sure to update the need_vm_lock logic
795 * above to match.
796 */
797 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
798 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
799 intid_mask)) {
800 goto out;
801 }
802
803 /* Increment the count. */
804 target_vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran318f5732018-11-20 16:23:42 +0000805
Andrew Walbran69520dc2018-12-06 11:39:38 +0000806 /*
Andrew Walbran3d84a262018-12-13 14:41:19 +0000807 * Only need to update state if there was not already an
808 * interrupt enabled and pending.
Andrew Walbran69520dc2018-12-06 11:39:38 +0000809 */
Andrew Walbran3d84a262018-12-13 14:41:19 +0000810 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
811 goto out;
812 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000813
Andrew Walbran3d84a262018-12-13 14:41:19 +0000814 if (target_vcpu->state == vcpu_state_blocked_interrupt) {
815 target_vcpu->state = vcpu_state_ready;
816 } else if (target_vcpu->state == vcpu_state_blocked_mailbox) {
817 /*
818 * If you change this logic make sure to update the need_vm_lock
819 * logic above to match.
820 */
821 target_vcpu->state = vcpu_state_ready;
Andrew Walbran69520dc2018-12-06 11:39:38 +0000822
Andrew Walbran3d84a262018-12-13 14:41:19 +0000823 /* Take target vCPU out of mailbox recv_waiter list. */
824 /*
825 * TODO: Consider using a doubly-linked list for
826 * the receive waiter list to avoid the linear
827 * search here.
828 */
829 struct vcpu **previous_next_pointer =
830 &target_vm->mailbox.recv_waiter;
831 while (*previous_next_pointer != NULL &&
832 *previous_next_pointer != target_vcpu) {
Andrew Walbran69520dc2018-12-06 11:39:38 +0000833 /*
Andrew Walbran3d84a262018-12-13 14:41:19 +0000834 * TODO(qwandor): Do we need to lock the vCPUs somehow
835 * while we walk the linked list, or is the VM lock
836 * enough?
Andrew Walbran69520dc2018-12-06 11:39:38 +0000837 */
Andrew Walbran3d84a262018-12-13 14:41:19 +0000838 previous_next_pointer =
839 &(*previous_next_pointer)->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +0000840 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000841
Andrew Walbran3d84a262018-12-13 14:41:19 +0000842 if (*previous_next_pointer == NULL) {
843 dlog("Target VCPU state is vcpu_state_blocked_mailbox "
844 "but is not in VM mailbox waiter list. This "
845 "should never happen.\n");
846 } else {
847 *previous_next_pointer = target_vcpu->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +0000848 }
849 }
850
Andrew Walbran3d84a262018-12-13 14:41:19 +0000851 if (current->vm->id == HF_PRIMARY_VM_ID) {
852 /*
853 * If the call came from the primary VM, let it know that it
854 * should run or kick the target vCPU.
855 */
856 ret = 1;
857 } else if (current != target_vcpu) {
858 /*
859 * Switch to the primary so that it can switch to the target, or
860 * kick it if it is already running on a different physical CPU.
861 */
862 struct hf_vcpu_run_return ret = {
863 .code = HF_VCPU_RUN_WAKE_UP,
864 .wake_up.vm_id = target_vm_id,
865 .wake_up.vcpu = target_vcpu_idx,
866 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000867
Andrew Walbran3d84a262018-12-13 14:41:19 +0000868 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
869 }
870
871out:
872 /* Either way, make it pending. */
873 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
874
Andrew Walbran318f5732018-11-20 16:23:42 +0000875 sl_unlock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000876 if (need_vm_lock) {
877 sl_unlock(&target_vm->lock);
878 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000879
Andrew Walbran3d84a262018-12-13 14:41:19 +0000880 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +0000881}