blob: ffb431b4c5abad30ecff393f385010447a29f466 [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 Scull6386f252018-12-06 13:29:10 +000024#include "hf/mm.h"
25#include "hf/spinlock.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010026#include "hf/std.h"
27#include "hf/vm.h"
28
Andrew Scullf35a5c92018-08-07 18:09:46 +010029#include "vmapi/hf/call.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010030
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000031/*
32 * To eliminate the risk of deadlocks, we define a partial order for the
33 * acquisition of locks held concurrently by the same physical CPU. Our current
34 * ordering requirements are as follows:
35 *
36 * vm::lock -> vcpu::lock
Andrew Scull6386f252018-12-06 13:29:10 +000037 *
38 * Locks of the same kind require the lock of lowest address to be locked first,
39 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000040 */
41
Andrew Scullaa039b32018-10-04 15:02:26 +010042static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010043 "Currently, a page is mapped for the send and receive buffers so "
44 "the maximum request is the size of a page.");
45
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000046static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000047
48/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000049 * Initialises the API page pool by taking ownership of the contents of the
50 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000051 */
52void api_init(struct mpool *ppool)
53{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000054 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000055}
56
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010057/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010058 * Switches the physical CPU back to the corresponding vcpu of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010059 *
60 * This triggers the scheduling logic to run. Run in the context of secondary VM
61 * to cause HF_VCPU_RUN to return and the primary VM to regain control of the
62 * cpu.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010063 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010064static struct vcpu *api_switch_to_primary(struct vcpu *current,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000065 struct hf_vcpu_run_return primary_ret,
66 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010067{
Andrew Scull19503262018-09-20 14:48:39 +010068 struct vm *primary = vm_get(HF_PRIMARY_VM_ID);
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010069 struct vcpu *next = &primary->vcpus[cpu_index(current->cpu)];
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010070
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010071 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Scull6d2db332018-10-10 15:28:17 +010072 arch_regs_set_retval(&next->regs,
73 hf_vcpu_run_return_encode(primary_ret));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010074
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000075 /* Mark the current vcpu as waiting. */
76 sl_lock(&current->lock);
77 current->state = secondary_state;
78 sl_unlock(&current->lock);
79
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010080 return next;
81}
82
83/**
Andrew Scull33fecd32019-01-08 14:48:27 +000084 * Returns to the primary vm and signals that the vcpu still has work to do so.
85 */
86struct vcpu *api_preempt(struct vcpu *current)
87{
88 struct hf_vcpu_run_return ret = {
89 .code = HF_VCPU_RUN_PREEMPTED,
90 };
91
92 return api_switch_to_primary(current, ret, vcpu_state_ready);
93}
94
95/**
Andrew Scullaa039b32018-10-04 15:02:26 +010096 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
97 * vm.
98 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010099struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100100{
Andrew Scull6d2db332018-10-10 15:28:17 +0100101 struct hf_vcpu_run_return ret = {
102 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
103 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000104
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000105 return api_switch_to_primary(current, ret,
106 vcpu_state_blocked_interrupt);
Andrew Scullaa039b32018-10-04 15:02:26 +0100107}
108
109/**
Andrew Scull66d62bf2019-02-01 13:54:10 +0000110 * Returns to the primary vm to allow this cpu to be used for other tasks as the
111 * vcpu does not have work to do at this moment. The current vcpu is marked as
112 * ready to be scheduled again.
113 */
114struct vcpu *api_yield(struct vcpu *current)
115{
116 struct hf_vcpu_run_return ret = {
117 .code = HF_VCPU_RUN_YIELD,
118 };
119
120 if (current->vm->id == HF_PRIMARY_VM_ID) {
121 /* Noop on the primary as it makes the scheduling decisions. */
122 return NULL;
123 }
124
125 return api_switch_to_primary(current, ret, vcpu_state_ready);
126}
127
128/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000129 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000130 */
131struct vcpu *api_abort(struct vcpu *current)
132{
133 struct hf_vcpu_run_return ret = {
134 .code = HF_VCPU_RUN_ABORTED,
135 };
136
137 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
138
139 if (current->vm->id == HF_PRIMARY_VM_ID) {
140 /* TODO: what to do when the primary aborts? */
141 for (;;) {
142 /* Do nothing. */
143 }
144 }
145
146 atomic_store_explicit(&current->vm->aborting, true,
147 memory_order_relaxed);
148
149 /* TODO: free resources once all vCPUs abort. */
150
151 return api_switch_to_primary(current, ret, vcpu_state_aborted);
152}
153
154/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000155 * Returns the ID of the VM.
156 */
157int64_t api_vm_get_id(const struct vcpu *current)
158{
159 return current->vm->id;
160}
161
162/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100163 * Returns the number of VMs configured to run.
164 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100165int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100166{
Andrew Scull19503262018-09-20 14:48:39 +0100167 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100168}
169
170/**
171 * Returns the number of vcpus configured in the given VM.
172 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100173int64_t api_vcpu_get_count(uint32_t vm_id, const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100174{
Andrew Scull19503262018-09-20 14:48:39 +0100175 struct vm *vm;
176
177 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100178 if (current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100179 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100180 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100181
Andrew Scull19503262018-09-20 14:48:39 +0100182 vm = vm_get(vm_id);
183 if (vm == NULL) {
184 return -1;
185 }
186
187 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100188}
189
190/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000191 * This function is called by the architecture-specific context switching
192 * function to indicate that register state for the given vcpu has been saved
193 * and can therefore be used by other pcpus.
194 */
195void api_regs_state_saved(struct vcpu *vcpu)
196{
197 sl_lock(&vcpu->lock);
198 vcpu->regs_available = true;
199 sl_unlock(&vcpu->lock);
200}
201
202/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000203 * Retrieves the next waiter and removes it from the wait list if the VM's
204 * mailbox is in a writable state.
205 */
206static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
207{
208 struct wait_entry *entry;
209 struct vm *vm = locked_vm.vm;
210
211 if (vm->mailbox.state != mailbox_state_empty ||
212 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
213 /* The mailbox is not writable or there are no waiters. */
214 return NULL;
215 }
216
217 /* Remove waiter from the wait list. */
218 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
219 wait_links);
220 list_remove(&entry->wait_links);
221 return entry;
222}
223
224/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000225 * Prepares the vcpu to run by updating its state and fetching whether a return
226 * value needs to be forced onto the vCPU.
227 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000228static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000229 struct retval_state *vcpu_retval)
230{
231 bool ret;
232
233 sl_lock(&vcpu->lock);
Andrew Scull9726c252019-01-23 13:44:19 +0000234
235 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
236 if (vcpu->state != vcpu_state_aborted) {
Andrew Scull82331282019-01-25 10:29:34 +0000237 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
238 vcpu_index(vcpu));
Andrew Scull9726c252019-01-23 13:44:19 +0000239 vcpu->state = vcpu_state_aborted;
240 }
241 ret = false;
242 goto out;
243 }
244
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000245 if (vcpu->state != vcpu_state_ready) {
246 ret = false;
247 goto out;
248 }
249
250 vcpu->cpu = current->cpu;
251 vcpu->state = vcpu_state_running;
252
253 /* Fetch return value to inject into vCPU if there is one. */
254 *vcpu_retval = vcpu->retval;
255 if (vcpu_retval->force) {
256 vcpu->retval.force = false;
257 }
258
259 /*
260 * Wait until the registers become available. Care must be taken when
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000261 * looping on this: it shouldn't be done while holding other locks to
262 * avoid deadlocks.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000263 */
264 while (!vcpu->regs_available) {
265 sl_unlock(&vcpu->lock);
266 sl_lock(&vcpu->lock);
267 }
268
269 /*
270 * Mark the registers as unavailable now that we're about to reflect
271 * them onto the real registers. This will also prevent another physical
272 * CPU from trying to read these registers.
273 */
274 vcpu->regs_available = false;
275
276 ret = true;
277
278out:
279 sl_unlock(&vcpu->lock);
280 return ret;
281}
282
283/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100284 * Runs the given vcpu of the given vm.
285 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100286struct hf_vcpu_run_return api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000287 const struct vcpu *current,
288 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100289{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100290 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100291 struct vcpu *vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000292 struct retval_state vcpu_retval;
Andrew Scull6d2db332018-10-10 15:28:17 +0100293 struct hf_vcpu_run_return ret = {
294 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
295 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100296
297 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100298 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100299 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100300 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100301
Andrew Scull19503262018-09-20 14:48:39 +0100302 /* Only secondary VM vcpus can be run. */
303 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100304 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100305 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100306
Andrew Scull19503262018-09-20 14:48:39 +0100307 /* The requested VM must exist. */
308 vm = vm_get(vm_id);
309 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100310 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100311 }
312
313 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100314 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100315 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100316 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100317
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000318 /* Update state if allowed. */
Andrew Scullf3d45592018-09-20 14:30:22 +0100319 vcpu = &vm->vcpus[vcpu_idx];
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000320 if (!api_vcpu_prepare_run(current, vcpu, &vcpu_retval)) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100321 ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000322 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100323 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000324
Andrew Scull33fecd32019-01-08 14:48:27 +0000325 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000326 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000327
Andrew Scull33fecd32019-01-08 14:48:27 +0000328 /*
329 * Set a placeholder return code to the scheduler. This will be
330 * overwritten when the switch back to the primary occurs.
331 */
332 ret.code = HF_VCPU_RUN_PREEMPTED;
333
334 /* Update return value for the next vcpu if one was injected. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000335 if (vcpu_retval.force) {
336 arch_regs_set_retval(&vcpu->regs, vcpu_retval.value);
337 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100338
Andrew Scull6d2db332018-10-10 15:28:17 +0100339out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100340 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100341}
342
343/**
Andrew Scull81e85092018-12-12 12:56:20 +0000344 * Check that the mode indicates memory that is valid, owned and exclusive.
345 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000346static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000347{
348 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
349 0;
350}
351
352/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000353 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
354 * after they've succeeded. If a secondary VM is running and there are waiters,
355 * it also switches back to the primary VM for it to wake waiters up.
356 */
357static int64_t api_waiter_result(struct vm_locked locked_vm,
358 struct vcpu *current, struct vcpu **next)
359{
360 struct vm *vm = locked_vm.vm;
361 struct hf_vcpu_run_return ret = {
362 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
363 };
364
365 if (list_empty(&vm->mailbox.waiter_list)) {
366 /* No waiters, nothing else to do. */
367 return 0;
368 }
369
370 if (vm->id == HF_PRIMARY_VM_ID) {
371 /* The caller is the primary VM. Tell it to wake up waiters. */
372 return 1;
373 }
374
375 /*
376 * Switch back to the primary VM, informing it that there are waiters
377 * that need to be notified.
378 */
379 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
380
381 return 0;
382}
383
384/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100385 * Configures the VM to send/receive data through the specified pages. The pages
386 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000387 *
388 * Returns:
389 * - -1 on failure.
390 * - 0 on success if no further action is needed.
391 * - 1 if it was called by the primary VM and the primary VM now needs to wake
392 * up or kick waiters. Waiters should be retrieved by calling
393 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100394 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000395int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
396 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100397{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100398 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000399 struct vm_locked locked;
Andrew Scull80871322018-08-06 12:04:09 +0100400 paddr_t pa_send_begin;
401 paddr_t pa_send_end;
402 paddr_t pa_recv_begin;
403 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000404 int orig_send_mode;
405 int orig_recv_mode;
406 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100407 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100408
409 /* Fail if addresses are not page-aligned. */
Andrew Scull265ada92018-07-30 15:19:01 +0100410 if ((ipa_addr(send) & (PAGE_SIZE - 1)) ||
411 (ipa_addr(recv) & (PAGE_SIZE - 1))) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100412 return -1;
413 }
414
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000415 /* Convert to physical addresses. */
416 pa_send_begin = pa_from_ipa(send);
417 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
418
419 pa_recv_begin = pa_from_ipa(recv);
420 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
421
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100422 /* Fail if the same page is used for the send and receive pages. */
423 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000424 return -1;
425 }
426
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000427 vm_lock(vm, &locked);
Andrew Scull220e6212018-12-21 18:09:00 +0000428
429 /* We only allow these to be setup once. */
430 if (vm->mailbox.send || vm->mailbox.recv) {
431 goto fail;
432 }
433
434 /*
435 * Ensure the pages are valid, owned and exclusive to the VM and that
436 * the VM has the required access to the memory.
437 */
438 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
439 &orig_send_mode) ||
440 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
441 (orig_send_mode & MM_MODE_R) == 0 ||
442 (orig_send_mode & MM_MODE_W) == 0) {
443 goto fail;
444 }
445
446 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
447 &orig_recv_mode) ||
448 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
449 (orig_recv_mode & MM_MODE_R) == 0) {
450 goto fail;
451 }
452
453 /*
454 * Create a local pool so any freed memory can't be used by another
455 * thread. This is to ensure the original mapping can be restored if any
456 * stage of the process fails.
457 */
458 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
459
460 /* Take memory ownership away from the VM and mark as shared. */
461 if (!mm_vm_identity_map(
462 &vm->ptable, pa_send_begin, pa_send_end,
463 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
464 NULL, &local_page_pool)) {
465 goto fail_free_pool;
466 }
467
468 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
469 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
470 NULL, &local_page_pool)) {
471 /* TODO: partial defrag of failed range. */
472 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000473 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000474 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100475 }
476
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100477 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000478 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000479 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100480 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000481 /* TODO: partial defrag of failed range. */
482 /* Recover any memory consumed in failed mapping. */
483 mm_defrag(&local_page_pool);
484 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100485 }
486
487 /*
488 * Map the receive page as writable in the hypervisor address space. On
489 * failure, unmap the send page before returning.
490 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000491 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000492 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100493 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000494 /* TODO: partial defrag of failed range. */
495 /* Recover any memory consumed in failed mapping. */
496 mm_defrag(&local_page_pool);
497 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100498 }
499
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000500 /* Tell caller about waiters, if any. */
501 ret = api_waiter_result(locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000502 goto exit;
503
504 /*
505 * The following mappings will not require more memory than is available
506 * in the local pool.
507 */
508fail_undo_all:
509 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000510 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000511
512fail_undo_send_and_recv:
513 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
514 orig_recv_mode, NULL, &local_page_pool);
515
516fail_undo_send:
517 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
518 orig_send_mode, NULL, &local_page_pool);
519
520fail_free_pool:
521 mpool_fini(&local_page_pool);
522
523fail:
524 ret = -1;
525
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100526exit:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000527 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100528
529 return ret;
530}
531
532/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100533 * Copies data from the sender's send buffer to the recipient's receive buffer
534 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000535 *
536 * If the recipient's receive buffer is busy, it can optionally register the
537 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100538 */
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000539int64_t api_mailbox_send(uint32_t vm_id, size_t size, bool notify,
540 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100541{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100542 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100543 struct vm *to;
544 const void *from_buf;
Andrew Scullaa039b32018-10-04 15:02:26 +0100545 uint16_t vcpu;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100546 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100547
Andrew Scullaa039b32018-10-04 15:02:26 +0100548 /* Limit the size of transfer. */
549 if (size > HF_MAILBOX_SIZE) {
Andrew Scull19503262018-09-20 14:48:39 +0100550 return -1;
551 }
552
553 /* Disallow reflexive requests as this suggests an error in the VM. */
554 if (vm_id == from->id) {
555 return -1;
556 }
557
558 /* Ensure the target VM exists. */
559 to = vm_get(vm_id);
560 if (to == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100561 return -1;
562 }
563
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100564 /*
565 * Check that the sender has configured its send buffer. It is safe to
566 * use from_buf after releasing the lock because the buffer cannot be
567 * modified once it's configured.
568 */
569 sl_lock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100570 from_buf = from->mailbox.send;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100571 sl_unlock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100572 if (from_buf == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100573 return -1;
574 }
575
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100576 sl_lock(&to->lock);
577
Andrew Scullaa039b32018-10-04 15:02:26 +0100578 if (to->mailbox.state != mailbox_state_empty ||
579 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000580 /*
581 * Fail if the target isn't currently ready to receive data,
582 * setting up for notification if requested.
583 */
584 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000585 struct wait_entry *entry =
586 &current->vm->wait_entries[vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000587
588 /* Append waiter only if it's not there yet. */
589 if (list_empty(&entry->wait_links)) {
590 list_append(&to->mailbox.waiter_list,
591 &entry->wait_links);
592 }
593 }
594
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100595 ret = -1;
Andrew Scullaa039b32018-10-04 15:02:26 +0100596 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100597 }
598
Andrew Scullaa039b32018-10-04 15:02:26 +0100599 /* Copy data. */
600 memcpy(to->mailbox.recv, from_buf, size);
601 to->mailbox.recv_bytes = size;
602 to->mailbox.recv_from_id = from->id;
603 to->mailbox.state = mailbox_state_read;
604
605 /* Messages for the primary VM are delivered directly. */
606 if (to->id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000607 struct hf_vcpu_run_return primary_ret = {
608 .code = HF_VCPU_RUN_MESSAGE,
609 .message.size = size,
610 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000611
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000612 *next = api_switch_to_primary(current, primary_ret,
613 vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100614 ret = 0;
615 goto out;
616 }
617
618 /*
619 * Try to find a vcpu to handle the message and tell the scheduler to
620 * run it.
621 */
622 if (to->mailbox.recv_waiter == NULL) {
623 /*
624 * The scheduler must choose a vcpu to interrupt so it can
625 * handle the message.
626 */
627 to->mailbox.state = mailbox_state_received;
628 vcpu = HF_INVALID_VCPU;
629 } else {
630 struct vcpu *to_vcpu = to->mailbox.recv_waiter;
631
632 /*
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000633 * Take target vcpu out of waiter list and mark it as ready to
634 * run again.
Andrew Scullaa039b32018-10-04 15:02:26 +0100635 */
636 sl_lock(&to_vcpu->lock);
637 to->mailbox.recv_waiter = to_vcpu->mailbox_next;
638 to_vcpu->state = vcpu_state_ready;
639
640 /* Return from HF_MAILBOX_RECEIVE. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000641 to_vcpu->retval.force = true;
642 to_vcpu->retval.value = hf_mailbox_receive_return_encode(
643 (struct hf_mailbox_receive_return){
644 .vm_id = to->mailbox.recv_from_id,
645 .size = size,
646 });
Andrew Scullaa039b32018-10-04 15:02:26 +0100647
648 sl_unlock(&to_vcpu->lock);
649
650 vcpu = to_vcpu - to->vcpus;
651 }
652
653 /* Return to the primary VM directly or with a switch. */
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000654 if (from->id == HF_PRIMARY_VM_ID) {
655 ret = vcpu;
656 } else {
657 struct hf_vcpu_run_return primary_ret = {
658 .code = HF_VCPU_RUN_WAKE_UP,
659 .wake_up.vm_id = to->id,
660 .wake_up.vcpu = vcpu,
661 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000662
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000663 *next = api_switch_to_primary(current, primary_ret,
664 vcpu_state_ready);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000665 ret = 0;
666 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100667
668out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100669 sl_unlock(&to->lock);
670
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000671 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100672}
673
674/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100675 * Receives a message from the mailbox. If one isn't available, this function
676 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100677 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100678 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100679 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100680struct hf_mailbox_receive_return api_mailbox_receive(bool block,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100681 struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100682 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100683{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100684 struct vm *vm = current->vm;
Andrew Scull6d2db332018-10-10 15:28:17 +0100685 struct hf_mailbox_receive_return ret = {
686 .vm_id = HF_INVALID_VM_ID,
687 };
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100688
Andrew Scullaa039b32018-10-04 15:02:26 +0100689 /*
690 * The primary VM will receive messages as a status code from running
691 * vcpus and must not call this function.
692 */
Andrew Scull19503262018-09-20 14:48:39 +0100693 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100694 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100695 }
696
697 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100698
Andrew Scullaa039b32018-10-04 15:02:26 +0100699 /* Return pending messages without blocking. */
700 if (vm->mailbox.state == mailbox_state_received) {
701 vm->mailbox.state = mailbox_state_read;
Andrew Scull6d2db332018-10-10 15:28:17 +0100702 ret.vm_id = vm->mailbox.recv_from_id;
703 ret.size = vm->mailbox.recv_bytes;
Andrew Scullaa039b32018-10-04 15:02:26 +0100704 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100705 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100706
707 /* No pending message so fail if not allowed to block. */
708 if (!block) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100709 goto out;
710 }
711
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100712 sl_lock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100713
714 /* Push vcpu into waiter list. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100715 current->mailbox_next = vm->mailbox.recv_waiter;
716 vm->mailbox.recv_waiter = current;
717 sl_unlock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100718
719 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000720 {
721 struct hf_vcpu_run_return run_return = {
722 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
723 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000724
Andrew Walbranb4816552018-12-05 17:35:42 +0000725 *next = api_switch_to_primary(current, run_return,
726 vcpu_state_blocked_mailbox);
727 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100728out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100729 sl_unlock(&vm->lock);
730
731 return ret;
732}
733
734/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000735 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
736 * by this function, the caller must have called api_mailbox_send before with
737 * the notify argument set to true, and this call must have failed because the
738 * mailbox was not available.
739 *
740 * It should be called repeatedly to retrieve a list of VMs.
741 *
742 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
743 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100744 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000745int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100746{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100747 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000748 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100749 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100750
751 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000752 if (list_empty(&vm->mailbox.ready_list)) {
753 ret = -1;
754 goto exit;
755 }
756
757 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
758 ready_links);
759 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000760 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000761
762exit:
763 sl_unlock(&vm->lock);
764 return ret;
765}
766
767/**
768 * Retrieves the next VM waiting to be notified that the mailbox of the
769 * specified VM became writable. Only primary VMs are allowed to call this.
770 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000771 * Returns -1 on failure or if there are no waiters; the VM id of the next
772 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000773 */
774int64_t api_mailbox_waiter_get(uint32_t vm_id, const struct vcpu *current)
775{
776 struct vm *vm;
777 struct vm_locked locked;
778 struct wait_entry *entry;
779 struct vm *waiting_vm;
780
781 /* Only primary VMs are allowed to call this function. */
782 if (current->vm->id != HF_PRIMARY_VM_ID) {
783 return -1;
784 }
785
786 vm = vm_get(vm_id);
787 if (vm == NULL) {
788 return -1;
789 }
790
791 /* Check if there are outstanding notifications from given vm. */
792 vm_lock(vm, &locked);
793 entry = api_fetch_waiter(locked);
794 vm_unlock(&locked);
795
796 if (entry == NULL) {
797 return -1;
798 }
799
800 /* Enqueue notification to waiting VM. */
801 waiting_vm = entry->waiting_vm;
802
803 sl_lock(&waiting_vm->lock);
804 if (list_empty(&entry->ready_links)) {
805 list_append(&waiting_vm->mailbox.ready_list,
806 &entry->ready_links);
807 }
808 sl_unlock(&waiting_vm->lock);
809
810 return waiting_vm->id;
811}
812
813/**
814 * Clears the caller's mailbox so that a new message can be received. The caller
815 * must have copied out all data they wish to preserve as new messages will
816 * overwrite the old and will arrive asynchronously.
817 *
818 * Returns:
819 * - -1 on failure, if the mailbox hasn't been read or is already empty.
820 * - 0 on success if no further action is needed.
821 * - 1 if it was called by the primary VM and the primary VM now needs to wake
822 * up or kick waiters. Waiters should be retrieved by calling
823 * hf_mailbox_waiter_get.
824 */
825int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
826{
827 struct vm *vm = current->vm;
828 struct vm_locked locked;
829 int64_t ret;
830
831 vm_lock(vm, &locked);
Andrew Scullaa039b32018-10-04 15:02:26 +0100832 if (vm->mailbox.state == mailbox_state_read) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000833 ret = api_waiter_result(locked, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +0100834 vm->mailbox.state = mailbox_state_empty;
835 } else {
836 ret = -1;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100837 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000838 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100839
840 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100841}
Andrew Walbran318f5732018-11-20 16:23:42 +0000842
843/**
844 * Enables or disables a given interrupt ID for the calling vCPU.
845 *
846 * Returns 0 on success, or -1 if the intid is invalid.
847 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000848int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000849{
850 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
851 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000852
Andrew Walbran318f5732018-11-20 16:23:42 +0000853 if (intid >= HF_NUM_INTIDS) {
854 return -1;
855 }
856
857 sl_lock(&current->lock);
858 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000859 /*
860 * If it is pending and was not enabled before, increment the
861 * count.
862 */
863 if (current->interrupts.interrupt_pending[intid_index] &
864 ~current->interrupts.interrupt_enabled[intid_index] &
865 intid_mask) {
866 current->interrupts.enabled_and_pending_count++;
867 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000868 current->interrupts.interrupt_enabled[intid_index] |=
869 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +0000870 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000871 /*
872 * If it is pending and was enabled before, decrement the count.
873 */
874 if (current->interrupts.interrupt_pending[intid_index] &
875 current->interrupts.interrupt_enabled[intid_index] &
876 intid_mask) {
877 current->interrupts.enabled_and_pending_count--;
878 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000879 current->interrupts.interrupt_enabled[intid_index] &=
880 ~intid_mask;
881 }
882
883 sl_unlock(&current->lock);
884 return 0;
885}
886
887/**
888 * Returns the ID of the next pending interrupt for the calling vCPU, and
889 * acknowledges it (i.e. marks it as no longer pending). Returns
890 * HF_INVALID_INTID if there are no pending interrupts.
891 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000892uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000893{
894 uint8_t i;
895 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +0000896
897 /*
898 * Find the first enabled and pending interrupt ID, return it, and
899 * deactivate it.
900 */
901 sl_lock(&current->lock);
902 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
903 uint32_t enabled_and_pending =
904 current->interrupts.interrupt_enabled[i] &
905 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000906
Andrew Walbran318f5732018-11-20 16:23:42 +0000907 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000908 uint8_t bit_index = ctz(enabled_and_pending);
909 /*
910 * Mark it as no longer pending and decrement the count.
911 */
912 current->interrupts.interrupt_pending[i] &=
913 ~(1u << bit_index);
914 current->interrupts.enabled_and_pending_count--;
915 first_interrupt =
916 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +0000917 break;
918 }
919 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000920
921 sl_unlock(&current->lock);
922 return first_interrupt;
923}
924
925/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +0000926 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +0000927 * given VM and vCPU.
928 */
929static inline bool is_injection_allowed(uint32_t target_vm_id,
930 struct vcpu *current)
931{
932 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000933
Andrew Walbran318f5732018-11-20 16:23:42 +0000934 /*
935 * The primary VM is allowed to inject interrupts into any VM. Secondary
936 * VMs are only allowed to inject interrupts into their own vCPUs.
937 */
938 return current_vm_id == HF_PRIMARY_VM_ID ||
939 current_vm_id == target_vm_id;
940}
941
942/**
943 * Injects a virtual interrupt of the given ID into the given target vCPU.
944 * This doesn't cause the vCPU to actually be run immediately; it will be taken
945 * when the vCPU is next run, which is up to the scheduler.
946 *
Andrew Walbran3d84a262018-12-13 14:41:19 +0000947 * Returns:
948 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
949 * ID is invalid, or the current VM is not allowed to inject interrupts to
950 * the target VM.
951 * - 0 on success if no further action is needed.
952 * - 1 if it was called by the primary VM and the primary VM now needs to wake
953 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +0000954 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000955int64_t api_interrupt_inject(uint32_t target_vm_id, uint32_t target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +0000956 uint32_t intid, struct vcpu *current,
957 struct vcpu **next)
958{
959 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
960 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
961 struct vcpu *target_vcpu;
962 struct vm *target_vm = vm_get(target_vm_id);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000963 bool need_vm_lock;
Andrew Walbran3d84a262018-12-13 14:41:19 +0000964 int64_t ret = 0;
Andrew Walbran318f5732018-11-20 16:23:42 +0000965
966 if (intid >= HF_NUM_INTIDS) {
967 return -1;
968 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000969
Andrew Walbran318f5732018-11-20 16:23:42 +0000970 if (target_vm == NULL) {
971 return -1;
972 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000973
Andrew Walbran318f5732018-11-20 16:23:42 +0000974 if (target_vcpu_idx >= target_vm->vcpu_count) {
975 /* The requested vcpu must exist. */
976 return -1;
977 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000978
Andrew Walbran318f5732018-11-20 16:23:42 +0000979 if (!is_injection_allowed(target_vm_id, current)) {
980 return -1;
981 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000982
Andrew Walbran318f5732018-11-20 16:23:42 +0000983 target_vcpu = &target_vm->vcpus[target_vcpu_idx];
984
985 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
986 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
987
988 sl_lock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000989 /*
990 * If we need the target_vm lock we need to release the target_vcpu lock
991 * first to maintain the correct order of locks. In-between releasing
992 * and acquiring it again the state of the vCPU could change in such a
993 * way that we don't actually need to touch the target_vm after all, but
994 * that's alright: we'll take the target_vm lock anyway, but it's safe,
995 * just perhaps a little slow in this unusual case. The reverse is not
996 * possible: if need_vm_lock is false, we don't release the target_vcpu
997 * lock until we are done, so nothing should change in such as way that
998 * we need the VM lock after all.
999 */
Andrew Walbran3d84a262018-12-13 14:41:19 +00001000 need_vm_lock =
1001 (target_vcpu->interrupts.interrupt_enabled[intid_index] &
1002 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
1003 intid_mask) &&
1004 target_vcpu->state == vcpu_state_blocked_mailbox;
Andrew Walbran69520dc2018-12-06 11:39:38 +00001005 if (need_vm_lock) {
1006 sl_unlock(&target_vcpu->lock);
1007 sl_lock(&target_vm->lock);
1008 sl_lock(&target_vcpu->lock);
1009 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001010
Andrew Walbran3d84a262018-12-13 14:41:19 +00001011 /*
1012 * We only need to change state and (maybe) trigger a virtual IRQ if it
1013 * is enabled and was not previously pending. Otherwise we can skip
1014 * everything except setting the pending bit.
1015 *
1016 * If you change this logic make sure to update the need_vm_lock logic
1017 * above to match.
1018 */
1019 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
1020 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
1021 intid_mask)) {
1022 goto out;
1023 }
1024
1025 /* Increment the count. */
1026 target_vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran318f5732018-11-20 16:23:42 +00001027
Andrew Walbran69520dc2018-12-06 11:39:38 +00001028 /*
Andrew Scull6386f252018-12-06 13:29:10 +00001029 * Only need to update state if there was not already an interrupt
1030 * enabled and pending.
Andrew Walbran69520dc2018-12-06 11:39:38 +00001031 */
Andrew Walbran3d84a262018-12-13 14:41:19 +00001032 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
1033 goto out;
1034 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001035
Andrew Walbran3d84a262018-12-13 14:41:19 +00001036 if (target_vcpu->state == vcpu_state_blocked_interrupt) {
1037 target_vcpu->state = vcpu_state_ready;
1038 } else if (target_vcpu->state == vcpu_state_blocked_mailbox) {
1039 /*
1040 * If you change this logic make sure to update the need_vm_lock
1041 * logic above to match.
1042 */
1043 target_vcpu->state = vcpu_state_ready;
Andrew Walbran69520dc2018-12-06 11:39:38 +00001044
Andrew Walbran3d84a262018-12-13 14:41:19 +00001045 /* Take target vCPU out of mailbox recv_waiter list. */
1046 /*
Andrew Scull6386f252018-12-06 13:29:10 +00001047 * TODO: Consider using a doubly-linked list for the receive
1048 * waiter list to avoid the linear search here.
Andrew Walbran3d84a262018-12-13 14:41:19 +00001049 */
1050 struct vcpu **previous_next_pointer =
1051 &target_vm->mailbox.recv_waiter;
1052 while (*previous_next_pointer != NULL &&
1053 *previous_next_pointer != target_vcpu) {
Andrew Walbran69520dc2018-12-06 11:39:38 +00001054 /*
Andrew Walbran3d84a262018-12-13 14:41:19 +00001055 * TODO(qwandor): Do we need to lock the vCPUs somehow
1056 * while we walk the linked list, or is the VM lock
1057 * enough?
Andrew Walbran69520dc2018-12-06 11:39:38 +00001058 */
Andrew Walbran3d84a262018-12-13 14:41:19 +00001059 previous_next_pointer =
1060 &(*previous_next_pointer)->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +00001061 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001062
Andrew Walbran3d84a262018-12-13 14:41:19 +00001063 if (*previous_next_pointer == NULL) {
1064 dlog("Target VCPU state is vcpu_state_blocked_mailbox "
1065 "but is not in VM mailbox waiter list. This "
1066 "should never happen.\n");
1067 } else {
1068 *previous_next_pointer = target_vcpu->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +00001069 }
1070 }
1071
Andrew Walbran3d84a262018-12-13 14:41:19 +00001072 if (current->vm->id == HF_PRIMARY_VM_ID) {
1073 /*
1074 * If the call came from the primary VM, let it know that it
1075 * should run or kick the target vCPU.
1076 */
1077 ret = 1;
1078 } else if (current != target_vcpu) {
1079 /*
1080 * Switch to the primary so that it can switch to the target, or
1081 * kick it if it is already running on a different physical CPU.
1082 */
1083 struct hf_vcpu_run_return ret = {
1084 .code = HF_VCPU_RUN_WAKE_UP,
1085 .wake_up.vm_id = target_vm_id,
1086 .wake_up.vcpu = target_vcpu_idx,
1087 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001088
Andrew Walbran3d84a262018-12-13 14:41:19 +00001089 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
1090 }
1091
1092out:
1093 /* Either way, make it pending. */
1094 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
1095
Andrew Walbran318f5732018-11-20 16:23:42 +00001096 sl_unlock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +00001097 if (need_vm_lock) {
1098 sl_unlock(&target_vm->lock);
1099 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001100
Andrew Walbran3d84a262018-12-13 14:41:19 +00001101 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00001102}
Andrew Scull6386f252018-12-06 13:29:10 +00001103
1104/**
1105 * Clears a region of physical memory by overwriting it with zeros. The data is
1106 * flushed from the cache so the memory has been cleared across the system.
1107 */
1108static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1109{
1110 /*
1111 * TODO: change this to a cpu local single page window rather than a
1112 * global mapping of the whole range. Such an approach will limit
1113 * the changes to stage-1 tables and will allow only local
1114 * invalidation.
1115 */
1116 void *ptr = mm_identity_map(begin, end, MM_MODE_W, ppool);
1117 size_t size = pa_addr(end) - pa_addr(begin);
1118
1119 if (!ptr) {
1120 /* TODO: partial defrag of failed range. */
1121 /* Recover any memory consumed in failed mapping. */
1122 mm_defrag(ppool);
1123 return false;
1124 }
1125
1126 memset(ptr, 0, size);
1127 arch_mm_write_back_dcache(ptr, size);
1128 mm_unmap(begin, end, ppool);
1129
1130 return true;
1131}
1132
1133/**
1134 * Shares memory from the calling VM with another. The memory can be shared in
1135 * different modes.
1136 *
1137 * TODO: the interface for sharing memory will need to be enhanced to allow
1138 * sharing with different modes e.g. read-only, informing the recipient
1139 * of the memory they have been given, opting to not wipe the memory and
1140 * possibly allowing multiple blocks to be transferred. What this will
1141 * look like is TBD.
1142 */
1143int64_t api_share_memory(uint32_t vm_id, ipaddr_t addr, size_t size,
1144 enum hf_share share, struct vcpu *current)
1145{
1146 struct vm *from = current->vm;
1147 struct vm *to;
1148 int orig_from_mode;
1149 int from_mode;
1150 int to_mode;
1151 ipaddr_t begin;
1152 ipaddr_t end;
1153 paddr_t pa_begin;
1154 paddr_t pa_end;
1155 struct mpool local_page_pool;
1156 int64_t ret;
1157
1158 /* Disallow reflexive shares as this suggests an error in the VM. */
1159 if (vm_id == from->id) {
1160 return -1;
1161 }
1162
1163 /* Ensure the target VM exists. */
1164 to = vm_get(vm_id);
1165 if (to == NULL) {
1166 return -1;
1167 }
1168
1169 begin = addr;
1170 end = ipa_add(addr, size);
1171
1172 /* Fail if addresses are not page-aligned. */
1173 if ((ipa_addr(begin) & (PAGE_SIZE - 1)) ||
1174 (ipa_addr(end) & (PAGE_SIZE - 1))) {
1175 return -1;
1176 }
1177
1178 /* Convert the sharing request to memory management modes. */
1179 switch (share) {
1180 case HF_MEMORY_GIVE:
1181 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1182 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1183 break;
1184
1185 case HF_MEMORY_LEND:
1186 from_mode = MM_MODE_INVALID;
1187 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1188 break;
1189
1190 case HF_MEMORY_SHARE:
1191 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1192 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1193 MM_MODE_SHARED;
1194 break;
1195
1196 default:
1197 /* The input is untrusted so might not be a valid value. */
1198 return -1;
1199 }
1200
1201 /*
1202 * Create a local pool so any freed memory can't be used by another
1203 * thread. This is to ensure the original mapping can be restored if any
1204 * stage of the process fails.
1205 */
1206 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1207
1208 sl_lock_both(&from->lock, &to->lock);
1209
1210 /*
1211 * Ensure that the memory range is mapped with the same mode so that
1212 * changes can be reverted if the process fails.
1213 */
1214 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1215 goto fail;
1216 }
1217
1218 /*
1219 * Ensure the memory range is valid for the sender. If it isn't, the
1220 * sender has either shared it with another VM already or has no claim
1221 * to the memory.
1222 */
1223 if (orig_from_mode & MM_MODE_INVALID) {
1224 goto fail;
1225 }
1226
1227 /*
1228 * The sender must own the memory and have exclusive access to it in
1229 * order to share it. Alternatively, it is giving memory back to the
1230 * owning VM.
1231 */
1232 if (orig_from_mode & MM_MODE_UNOWNED) {
1233 int orig_to_mode;
1234
1235 if (share != HF_MEMORY_GIVE ||
1236 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1237 orig_to_mode & MM_MODE_UNOWNED) {
1238 goto fail;
1239 }
1240 } else if (orig_from_mode & MM_MODE_SHARED) {
1241 goto fail;
1242 }
1243
1244 pa_begin = pa_from_ipa(begin);
1245 pa_end = pa_from_ipa(end);
1246
1247 /*
1248 * First update the mapping for the sender so there is not overlap with
1249 * the recipient.
1250 */
1251 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1252 NULL, &local_page_pool)) {
1253 goto fail;
1254 }
1255
1256 /* Clear the memory so no VM or device can see the previous contents. */
1257 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1258 goto fail_return_to_sender;
1259 }
1260
1261 /* Complete the transfer by mapping the memory into the recipient. */
1262 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1263 &local_page_pool)) {
1264 /* TODO: partial defrag of failed range. */
1265 /* Recover any memory consumed in failed mapping. */
1266 mm_vm_defrag(&from->ptable, &local_page_pool);
1267 goto fail_return_to_sender;
1268 }
1269
1270 ret = 0;
1271 goto out;
1272
1273fail_return_to_sender:
1274 mm_vm_identity_map(&from->ptable, pa_begin, pa_end, orig_from_mode,
1275 NULL, &local_page_pool);
1276
1277fail:
1278 ret = -1;
1279
1280out:
1281 sl_unlock(&from->lock);
1282 sl_unlock(&to->lock);
1283
1284 mpool_fini(&local_page_pool);
1285
1286 return ret;
1287}