blob: 95065325c2e4f83521fa51bbb299b71657ea04bb [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/**
96 * Returns to the primary vm to allow this cpu to be used for other tasks as the
97 * vcpu does not have work to do at this moment. The current vcpu is marked as
98 * ready to be scheduled again.
Andrew Scullaa039b32018-10-04 15:02:26 +010099 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100100struct vcpu *api_yield(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100101{
Andrew Scull6d2db332018-10-10 15:28:17 +0100102 struct hf_vcpu_run_return ret = {
103 .code = HF_VCPU_RUN_YIELD,
104 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000105
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000106 return api_switch_to_primary(current, ret, vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100107}
108
109/**
110 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
111 * vm.
112 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100113struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100114{
Andrew Scull6d2db332018-10-10 15:28:17 +0100115 struct hf_vcpu_run_return ret = {
116 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
117 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000118
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000119 return api_switch_to_primary(current, ret,
120 vcpu_state_blocked_interrupt);
Andrew Scullaa039b32018-10-04 15:02:26 +0100121}
122
123/**
Andrew Scull9726c252019-01-23 13:44:19 +0000124 * Aborts the vCPU and triggers it's VM to abort fully.
125 */
126struct vcpu *api_abort(struct vcpu *current)
127{
128 struct hf_vcpu_run_return ret = {
129 .code = HF_VCPU_RUN_ABORTED,
130 };
131
132 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
133
134 if (current->vm->id == HF_PRIMARY_VM_ID) {
135 /* TODO: what to do when the primary aborts? */
136 for (;;) {
137 /* Do nothing. */
138 }
139 }
140
141 atomic_store_explicit(&current->vm->aborting, true,
142 memory_order_relaxed);
143
144 /* TODO: free resources once all vCPUs abort. */
145
146 return api_switch_to_primary(current, ret, vcpu_state_aborted);
147}
148
149/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000150 * Returns the ID of the VM.
151 */
152int64_t api_vm_get_id(const struct vcpu *current)
153{
154 return current->vm->id;
155}
156
157/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100158 * Returns the number of VMs configured to run.
159 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100160int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100161{
Andrew Scull19503262018-09-20 14:48:39 +0100162 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100163}
164
165/**
166 * Returns the number of vcpus configured in the given VM.
167 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100168int64_t api_vcpu_get_count(uint32_t vm_id, const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100169{
Andrew Scull19503262018-09-20 14:48:39 +0100170 struct vm *vm;
171
172 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100173 if (current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100174 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100175 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100176
Andrew Scull19503262018-09-20 14:48:39 +0100177 vm = vm_get(vm_id);
178 if (vm == NULL) {
179 return -1;
180 }
181
182 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100183}
184
185/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000186 * This function is called by the architecture-specific context switching
187 * function to indicate that register state for the given vcpu has been saved
188 * and can therefore be used by other pcpus.
189 */
190void api_regs_state_saved(struct vcpu *vcpu)
191{
192 sl_lock(&vcpu->lock);
193 vcpu->regs_available = true;
194 sl_unlock(&vcpu->lock);
195}
196
197/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000198 * Retrieves the next waiter and removes it from the wait list if the VM's
199 * mailbox is in a writable state.
200 */
201static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
202{
203 struct wait_entry *entry;
204 struct vm *vm = locked_vm.vm;
205
206 if (vm->mailbox.state != mailbox_state_empty ||
207 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
208 /* The mailbox is not writable or there are no waiters. */
209 return NULL;
210 }
211
212 /* Remove waiter from the wait list. */
213 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
214 wait_links);
215 list_remove(&entry->wait_links);
216 return entry;
217}
218
219/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000220 * Prepares the vcpu to run by updating its state and fetching whether a return
221 * value needs to be forced onto the vCPU.
222 */
Andrew Scull9726c252019-01-23 13:44:19 +0000223static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000224 struct retval_state *vcpu_retval)
225{
226 bool ret;
227
228 sl_lock(&vcpu->lock);
Andrew Scull9726c252019-01-23 13:44:19 +0000229
230 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
231 if (vcpu->state != vcpu_state_aborted) {
232 dlog("Aborting VM %u vCPU %u\n", current->vm->id,
233 vcpu_index(current));
234 vcpu->state = vcpu_state_aborted;
235 }
236 ret = false;
237 goto out;
238 }
239
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000240 if (vcpu->state != vcpu_state_ready) {
241 ret = false;
242 goto out;
243 }
244
245 vcpu->cpu = current->cpu;
246 vcpu->state = vcpu_state_running;
247
248 /* Fetch return value to inject into vCPU if there is one. */
249 *vcpu_retval = vcpu->retval;
250 if (vcpu_retval->force) {
251 vcpu->retval.force = false;
252 }
253
254 /*
255 * Wait until the registers become available. Care must be taken when
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000256 * looping on this: it shouldn't be done while holding other locks to
257 * avoid deadlocks.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000258 */
259 while (!vcpu->regs_available) {
260 sl_unlock(&vcpu->lock);
261 sl_lock(&vcpu->lock);
262 }
263
264 /*
265 * Mark the registers as unavailable now that we're about to reflect
266 * them onto the real registers. This will also prevent another physical
267 * CPU from trying to read these registers.
268 */
269 vcpu->regs_available = false;
270
271 ret = true;
272
273out:
274 sl_unlock(&vcpu->lock);
275 return ret;
276}
277
278/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100279 * Runs the given vcpu of the given vm.
280 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100281struct hf_vcpu_run_return api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx,
Andrew Scull9726c252019-01-23 13:44:19 +0000282 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100283{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100284 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100285 struct vcpu *vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000286 struct retval_state vcpu_retval;
Andrew Scull6d2db332018-10-10 15:28:17 +0100287 struct hf_vcpu_run_return ret = {
288 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
289 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100290
291 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100292 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100293 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100294 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100295
Andrew Scull19503262018-09-20 14:48:39 +0100296 /* Only secondary VM vcpus can be run. */
297 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100298 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100299 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100300
Andrew Scull19503262018-09-20 14:48:39 +0100301 /* The requested VM must exist. */
302 vm = vm_get(vm_id);
303 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100304 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100305 }
306
307 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100308 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100309 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100310 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100311
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000312 /* Update state if allowed. */
Andrew Scullf3d45592018-09-20 14:30:22 +0100313 vcpu = &vm->vcpus[vcpu_idx];
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000314 if (!api_vcpu_prepare_run(current, vcpu, &vcpu_retval)) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100315 ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000316 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100317 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000318
Andrew Scull33fecd32019-01-08 14:48:27 +0000319 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000320 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000321
Andrew Scull33fecd32019-01-08 14:48:27 +0000322 /*
323 * Set a placeholder return code to the scheduler. This will be
324 * overwritten when the switch back to the primary occurs.
325 */
326 ret.code = HF_VCPU_RUN_PREEMPTED;
327
328 /* Update return value for the next vcpu if one was injected. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000329 if (vcpu_retval.force) {
330 arch_regs_set_retval(&vcpu->regs, vcpu_retval.value);
331 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100332
Andrew Scull6d2db332018-10-10 15:28:17 +0100333out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100334 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100335}
336
337/**
Andrew Scull81e85092018-12-12 12:56:20 +0000338 * Check that the mode indicates memory that is valid, owned and exclusive.
339 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000340static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000341{
342 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
343 0;
344}
345
346/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000347 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
348 * after they've succeeded. If a secondary VM is running and there are waiters,
349 * it also switches back to the primary VM for it to wake waiters up.
350 */
351static int64_t api_waiter_result(struct vm_locked locked_vm,
352 struct vcpu *current, struct vcpu **next)
353{
354 struct vm *vm = locked_vm.vm;
355 struct hf_vcpu_run_return ret = {
356 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
357 };
358
359 if (list_empty(&vm->mailbox.waiter_list)) {
360 /* No waiters, nothing else to do. */
361 return 0;
362 }
363
364 if (vm->id == HF_PRIMARY_VM_ID) {
365 /* The caller is the primary VM. Tell it to wake up waiters. */
366 return 1;
367 }
368
369 /*
370 * Switch back to the primary VM, informing it that there are waiters
371 * that need to be notified.
372 */
373 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
374
375 return 0;
376}
377
378/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100379 * Configures the VM to send/receive data through the specified pages. The pages
380 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000381 *
382 * Returns:
383 * - -1 on failure.
384 * - 0 on success if no further action is needed.
385 * - 1 if it was called by the primary VM and the primary VM now needs to wake
386 * up or kick waiters. Waiters should be retrieved by calling
387 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100388 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000389int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
390 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100391{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100392 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000393 struct vm_locked locked;
Andrew Scull80871322018-08-06 12:04:09 +0100394 paddr_t pa_send_begin;
395 paddr_t pa_send_end;
396 paddr_t pa_recv_begin;
397 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000398 int orig_send_mode;
399 int orig_recv_mode;
400 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100401 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100402
403 /* Fail if addresses are not page-aligned. */
Andrew Scull265ada92018-07-30 15:19:01 +0100404 if ((ipa_addr(send) & (PAGE_SIZE - 1)) ||
405 (ipa_addr(recv) & (PAGE_SIZE - 1))) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100406 return -1;
407 }
408
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000409 /* Convert to physical addresses. */
410 pa_send_begin = pa_from_ipa(send);
411 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
412
413 pa_recv_begin = pa_from_ipa(recv);
414 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
415
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100416 /* Fail if the same page is used for the send and receive pages. */
417 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000418 return -1;
419 }
420
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000421 vm_lock(vm, &locked);
Andrew Scull220e6212018-12-21 18:09:00 +0000422
423 /* We only allow these to be setup once. */
424 if (vm->mailbox.send || vm->mailbox.recv) {
425 goto fail;
426 }
427
428 /*
429 * Ensure the pages are valid, owned and exclusive to the VM and that
430 * the VM has the required access to the memory.
431 */
432 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
433 &orig_send_mode) ||
434 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
435 (orig_send_mode & MM_MODE_R) == 0 ||
436 (orig_send_mode & MM_MODE_W) == 0) {
437 goto fail;
438 }
439
440 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
441 &orig_recv_mode) ||
442 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
443 (orig_recv_mode & MM_MODE_R) == 0) {
444 goto fail;
445 }
446
447 /*
448 * Create a local pool so any freed memory can't be used by another
449 * thread. This is to ensure the original mapping can be restored if any
450 * stage of the process fails.
451 */
452 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
453
454 /* Take memory ownership away from the VM and mark as shared. */
455 if (!mm_vm_identity_map(
456 &vm->ptable, pa_send_begin, pa_send_end,
457 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
458 NULL, &local_page_pool)) {
459 goto fail_free_pool;
460 }
461
462 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
463 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
464 NULL, &local_page_pool)) {
465 /* TODO: partial defrag of failed range. */
466 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000467 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000468 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100469 }
470
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100471 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000472 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000473 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100474 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000475 /* TODO: partial defrag of failed range. */
476 /* Recover any memory consumed in failed mapping. */
477 mm_defrag(&local_page_pool);
478 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100479 }
480
481 /*
482 * Map the receive page as writable in the hypervisor address space. On
483 * failure, unmap the send page before returning.
484 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000485 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000486 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100487 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000488 /* TODO: partial defrag of failed range. */
489 /* Recover any memory consumed in failed mapping. */
490 mm_defrag(&local_page_pool);
491 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100492 }
493
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000494 /* Tell caller about waiters, if any. */
495 ret = api_waiter_result(locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000496 goto exit;
497
498 /*
499 * The following mappings will not require more memory than is available
500 * in the local pool.
501 */
502fail_undo_all:
503 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000504 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000505
506fail_undo_send_and_recv:
507 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
508 orig_recv_mode, NULL, &local_page_pool);
509
510fail_undo_send:
511 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
512 orig_send_mode, NULL, &local_page_pool);
513
514fail_free_pool:
515 mpool_fini(&local_page_pool);
516
517fail:
518 ret = -1;
519
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100520exit:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000521 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100522
523 return ret;
524}
525
526/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100527 * Copies data from the sender's send buffer to the recipient's receive buffer
528 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000529 *
530 * If the recipient's receive buffer is busy, it can optionally register the
531 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100532 */
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000533int64_t api_mailbox_send(uint32_t vm_id, size_t size, bool notify,
534 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100535{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100536 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100537 struct vm *to;
538 const void *from_buf;
Andrew Scullaa039b32018-10-04 15:02:26 +0100539 uint16_t vcpu;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100540 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100541
Andrew Scullaa039b32018-10-04 15:02:26 +0100542 /* Limit the size of transfer. */
543 if (size > HF_MAILBOX_SIZE) {
Andrew Scull19503262018-09-20 14:48:39 +0100544 return -1;
545 }
546
547 /* Disallow reflexive requests as this suggests an error in the VM. */
548 if (vm_id == from->id) {
549 return -1;
550 }
551
552 /* Ensure the target VM exists. */
553 to = vm_get(vm_id);
554 if (to == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100555 return -1;
556 }
557
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100558 /*
559 * Check that the sender has configured its send buffer. It is safe to
560 * use from_buf after releasing the lock because the buffer cannot be
561 * modified once it's configured.
562 */
563 sl_lock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100564 from_buf = from->mailbox.send;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100565 sl_unlock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100566 if (from_buf == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100567 return -1;
568 }
569
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100570 sl_lock(&to->lock);
571
Andrew Scullaa039b32018-10-04 15:02:26 +0100572 if (to->mailbox.state != mailbox_state_empty ||
573 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000574 /*
575 * Fail if the target isn't currently ready to receive data,
576 * setting up for notification if requested.
577 */
578 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000579 struct wait_entry *entry =
580 &current->vm->wait_entries[vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000581
582 /* Append waiter only if it's not there yet. */
583 if (list_empty(&entry->wait_links)) {
584 list_append(&to->mailbox.waiter_list,
585 &entry->wait_links);
586 }
587 }
588
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100589 ret = -1;
Andrew Scullaa039b32018-10-04 15:02:26 +0100590 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100591 }
592
Andrew Scullaa039b32018-10-04 15:02:26 +0100593 /* Copy data. */
594 memcpy(to->mailbox.recv, from_buf, size);
595 to->mailbox.recv_bytes = size;
596 to->mailbox.recv_from_id = from->id;
597 to->mailbox.state = mailbox_state_read;
598
599 /* Messages for the primary VM are delivered directly. */
600 if (to->id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000601 struct hf_vcpu_run_return primary_ret = {
602 .code = HF_VCPU_RUN_MESSAGE,
603 .message.size = size,
604 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000605
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000606 *next = api_switch_to_primary(current, primary_ret,
607 vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100608 ret = 0;
609 goto out;
610 }
611
612 /*
613 * Try to find a vcpu to handle the message and tell the scheduler to
614 * run it.
615 */
616 if (to->mailbox.recv_waiter == NULL) {
617 /*
618 * The scheduler must choose a vcpu to interrupt so it can
619 * handle the message.
620 */
621 to->mailbox.state = mailbox_state_received;
622 vcpu = HF_INVALID_VCPU;
623 } else {
624 struct vcpu *to_vcpu = to->mailbox.recv_waiter;
625
626 /*
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000627 * Take target vcpu out of waiter list and mark it as ready to
628 * run again.
Andrew Scullaa039b32018-10-04 15:02:26 +0100629 */
630 sl_lock(&to_vcpu->lock);
631 to->mailbox.recv_waiter = to_vcpu->mailbox_next;
632 to_vcpu->state = vcpu_state_ready;
633
634 /* Return from HF_MAILBOX_RECEIVE. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000635 to_vcpu->retval.force = true;
636 to_vcpu->retval.value = hf_mailbox_receive_return_encode(
637 (struct hf_mailbox_receive_return){
638 .vm_id = to->mailbox.recv_from_id,
639 .size = size,
640 });
Andrew Scullaa039b32018-10-04 15:02:26 +0100641
642 sl_unlock(&to_vcpu->lock);
643
644 vcpu = to_vcpu - to->vcpus;
645 }
646
647 /* Return to the primary VM directly or with a switch. */
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000648 if (from->id == HF_PRIMARY_VM_ID) {
649 ret = vcpu;
650 } else {
651 struct hf_vcpu_run_return primary_ret = {
652 .code = HF_VCPU_RUN_WAKE_UP,
653 .wake_up.vm_id = to->id,
654 .wake_up.vcpu = vcpu,
655 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000656
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000657 *next = api_switch_to_primary(current, primary_ret,
658 vcpu_state_ready);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000659 ret = 0;
660 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100661
662out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100663 sl_unlock(&to->lock);
664
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000665 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100666}
667
668/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100669 * Receives a message from the mailbox. If one isn't available, this function
670 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100671 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100672 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100673 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100674struct hf_mailbox_receive_return api_mailbox_receive(bool block,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100675 struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100676 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100677{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100678 struct vm *vm = current->vm;
Andrew Scull6d2db332018-10-10 15:28:17 +0100679 struct hf_mailbox_receive_return ret = {
680 .vm_id = HF_INVALID_VM_ID,
681 };
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100682
Andrew Scullaa039b32018-10-04 15:02:26 +0100683 /*
684 * The primary VM will receive messages as a status code from running
685 * vcpus and must not call this function.
686 */
Andrew Scull19503262018-09-20 14:48:39 +0100687 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100688 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100689 }
690
691 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100692
Andrew Scullaa039b32018-10-04 15:02:26 +0100693 /* Return pending messages without blocking. */
694 if (vm->mailbox.state == mailbox_state_received) {
695 vm->mailbox.state = mailbox_state_read;
Andrew Scull6d2db332018-10-10 15:28:17 +0100696 ret.vm_id = vm->mailbox.recv_from_id;
697 ret.size = vm->mailbox.recv_bytes;
Andrew Scullaa039b32018-10-04 15:02:26 +0100698 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100699 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100700
701 /* No pending message so fail if not allowed to block. */
702 if (!block) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100703 goto out;
704 }
705
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100706 sl_lock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100707
708 /* Push vcpu into waiter list. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100709 current->mailbox_next = vm->mailbox.recv_waiter;
710 vm->mailbox.recv_waiter = current;
711 sl_unlock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100712
713 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000714 {
715 struct hf_vcpu_run_return run_return = {
716 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
717 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000718
Andrew Walbranb4816552018-12-05 17:35:42 +0000719 *next = api_switch_to_primary(current, run_return,
720 vcpu_state_blocked_mailbox);
721 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100722out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100723 sl_unlock(&vm->lock);
724
725 return ret;
726}
727
728/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000729 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
730 * by this function, the caller must have called api_mailbox_send before with
731 * the notify argument set to true, and this call must have failed because the
732 * mailbox was not available.
733 *
734 * It should be called repeatedly to retrieve a list of VMs.
735 *
736 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
737 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100738 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000739int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100740{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100741 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000742 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100743 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100744
745 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000746 if (list_empty(&vm->mailbox.ready_list)) {
747 ret = -1;
748 goto exit;
749 }
750
751 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
752 ready_links);
753 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000754 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000755
756exit:
757 sl_unlock(&vm->lock);
758 return ret;
759}
760
761/**
762 * Retrieves the next VM waiting to be notified that the mailbox of the
763 * specified VM became writable. Only primary VMs are allowed to call this.
764 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000765 * Returns -1 on failure or if there are no waiters; the VM id of the next
766 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000767 */
768int64_t api_mailbox_waiter_get(uint32_t vm_id, const struct vcpu *current)
769{
770 struct vm *vm;
771 struct vm_locked locked;
772 struct wait_entry *entry;
773 struct vm *waiting_vm;
774
775 /* Only primary VMs are allowed to call this function. */
776 if (current->vm->id != HF_PRIMARY_VM_ID) {
777 return -1;
778 }
779
780 vm = vm_get(vm_id);
781 if (vm == NULL) {
782 return -1;
783 }
784
785 /* Check if there are outstanding notifications from given vm. */
786 vm_lock(vm, &locked);
787 entry = api_fetch_waiter(locked);
788 vm_unlock(&locked);
789
790 if (entry == NULL) {
791 return -1;
792 }
793
794 /* Enqueue notification to waiting VM. */
795 waiting_vm = entry->waiting_vm;
796
797 sl_lock(&waiting_vm->lock);
798 if (list_empty(&entry->ready_links)) {
799 list_append(&waiting_vm->mailbox.ready_list,
800 &entry->ready_links);
801 }
802 sl_unlock(&waiting_vm->lock);
803
804 return waiting_vm->id;
805}
806
807/**
808 * Clears the caller's mailbox so that a new message can be received. The caller
809 * must have copied out all data they wish to preserve as new messages will
810 * overwrite the old and will arrive asynchronously.
811 *
812 * Returns:
813 * - -1 on failure, if the mailbox hasn't been read or is already empty.
814 * - 0 on success if no further action is needed.
815 * - 1 if it was called by the primary VM and the primary VM now needs to wake
816 * up or kick waiters. Waiters should be retrieved by calling
817 * hf_mailbox_waiter_get.
818 */
819int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
820{
821 struct vm *vm = current->vm;
822 struct vm_locked locked;
823 int64_t ret;
824
825 vm_lock(vm, &locked);
Andrew Scullaa039b32018-10-04 15:02:26 +0100826 if (vm->mailbox.state == mailbox_state_read) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000827 ret = api_waiter_result(locked, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +0100828 vm->mailbox.state = mailbox_state_empty;
829 } else {
830 ret = -1;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100831 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000832 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100833
834 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100835}
Andrew Walbran318f5732018-11-20 16:23:42 +0000836
837/**
838 * Enables or disables a given interrupt ID for the calling vCPU.
839 *
840 * Returns 0 on success, or -1 if the intid is invalid.
841 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000842int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000843{
844 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
845 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000846
Andrew Walbran318f5732018-11-20 16:23:42 +0000847 if (intid >= HF_NUM_INTIDS) {
848 return -1;
849 }
850
851 sl_lock(&current->lock);
852 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000853 /*
854 * If it is pending and was not enabled before, increment the
855 * count.
856 */
857 if (current->interrupts.interrupt_pending[intid_index] &
858 ~current->interrupts.interrupt_enabled[intid_index] &
859 intid_mask) {
860 current->interrupts.enabled_and_pending_count++;
861 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000862 current->interrupts.interrupt_enabled[intid_index] |=
863 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +0000864 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000865 /*
866 * If it is pending and was enabled before, decrement the count.
867 */
868 if (current->interrupts.interrupt_pending[intid_index] &
869 current->interrupts.interrupt_enabled[intid_index] &
870 intid_mask) {
871 current->interrupts.enabled_and_pending_count--;
872 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000873 current->interrupts.interrupt_enabled[intid_index] &=
874 ~intid_mask;
875 }
876
877 sl_unlock(&current->lock);
878 return 0;
879}
880
881/**
882 * Returns the ID of the next pending interrupt for the calling vCPU, and
883 * acknowledges it (i.e. marks it as no longer pending). Returns
884 * HF_INVALID_INTID if there are no pending interrupts.
885 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000886uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000887{
888 uint8_t i;
889 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +0000890
891 /*
892 * Find the first enabled and pending interrupt ID, return it, and
893 * deactivate it.
894 */
895 sl_lock(&current->lock);
896 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
897 uint32_t enabled_and_pending =
898 current->interrupts.interrupt_enabled[i] &
899 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000900
Andrew Walbran318f5732018-11-20 16:23:42 +0000901 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000902 uint8_t bit_index = ctz(enabled_and_pending);
903 /*
904 * Mark it as no longer pending and decrement the count.
905 */
906 current->interrupts.interrupt_pending[i] &=
907 ~(1u << bit_index);
908 current->interrupts.enabled_and_pending_count--;
909 first_interrupt =
910 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +0000911 break;
912 }
913 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000914
915 sl_unlock(&current->lock);
916 return first_interrupt;
917}
918
919/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +0000920 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +0000921 * given VM and vCPU.
922 */
923static inline bool is_injection_allowed(uint32_t target_vm_id,
924 struct vcpu *current)
925{
926 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000927
Andrew Walbran318f5732018-11-20 16:23:42 +0000928 /*
929 * The primary VM is allowed to inject interrupts into any VM. Secondary
930 * VMs are only allowed to inject interrupts into their own vCPUs.
931 */
932 return current_vm_id == HF_PRIMARY_VM_ID ||
933 current_vm_id == target_vm_id;
934}
935
936/**
937 * Injects a virtual interrupt of the given ID into the given target vCPU.
938 * This doesn't cause the vCPU to actually be run immediately; it will be taken
939 * when the vCPU is next run, which is up to the scheduler.
940 *
Andrew Walbran3d84a262018-12-13 14:41:19 +0000941 * Returns:
942 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
943 * ID is invalid, or the current VM is not allowed to inject interrupts to
944 * the target VM.
945 * - 0 on success if no further action is needed.
946 * - 1 if it was called by the primary VM and the primary VM now needs to wake
947 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +0000948 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000949int64_t api_interrupt_inject(uint32_t target_vm_id, uint32_t target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +0000950 uint32_t intid, struct vcpu *current,
951 struct vcpu **next)
952{
953 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
954 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
955 struct vcpu *target_vcpu;
956 struct vm *target_vm = vm_get(target_vm_id);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000957 bool need_vm_lock;
Andrew Walbran3d84a262018-12-13 14:41:19 +0000958 int64_t ret = 0;
Andrew Walbran318f5732018-11-20 16:23:42 +0000959
960 if (intid >= HF_NUM_INTIDS) {
961 return -1;
962 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000963
Andrew Walbran318f5732018-11-20 16:23:42 +0000964 if (target_vm == NULL) {
965 return -1;
966 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000967
Andrew Walbran318f5732018-11-20 16:23:42 +0000968 if (target_vcpu_idx >= target_vm->vcpu_count) {
969 /* The requested vcpu must exist. */
970 return -1;
971 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000972
Andrew Walbran318f5732018-11-20 16:23:42 +0000973 if (!is_injection_allowed(target_vm_id, current)) {
974 return -1;
975 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000976
Andrew Walbran318f5732018-11-20 16:23:42 +0000977 target_vcpu = &target_vm->vcpus[target_vcpu_idx];
978
979 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
980 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
981
982 sl_lock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000983 /*
984 * If we need the target_vm lock we need to release the target_vcpu lock
985 * first to maintain the correct order of locks. In-between releasing
986 * and acquiring it again the state of the vCPU could change in such a
987 * way that we don't actually need to touch the target_vm after all, but
988 * that's alright: we'll take the target_vm lock anyway, but it's safe,
989 * just perhaps a little slow in this unusual case. The reverse is not
990 * possible: if need_vm_lock is false, we don't release the target_vcpu
991 * lock until we are done, so nothing should change in such as way that
992 * we need the VM lock after all.
993 */
Andrew Walbran3d84a262018-12-13 14:41:19 +0000994 need_vm_lock =
995 (target_vcpu->interrupts.interrupt_enabled[intid_index] &
996 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
997 intid_mask) &&
998 target_vcpu->state == vcpu_state_blocked_mailbox;
Andrew Walbran69520dc2018-12-06 11:39:38 +0000999 if (need_vm_lock) {
1000 sl_unlock(&target_vcpu->lock);
1001 sl_lock(&target_vm->lock);
1002 sl_lock(&target_vcpu->lock);
1003 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001004
Andrew Walbran3d84a262018-12-13 14:41:19 +00001005 /*
1006 * We only need to change state and (maybe) trigger a virtual IRQ if it
1007 * is enabled and was not previously pending. Otherwise we can skip
1008 * everything except setting the pending bit.
1009 *
1010 * If you change this logic make sure to update the need_vm_lock logic
1011 * above to match.
1012 */
1013 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
1014 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
1015 intid_mask)) {
1016 goto out;
1017 }
1018
1019 /* Increment the count. */
1020 target_vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran318f5732018-11-20 16:23:42 +00001021
Andrew Walbran69520dc2018-12-06 11:39:38 +00001022 /*
Andrew Scull6386f252018-12-06 13:29:10 +00001023 * Only need to update state if there was not already an interrupt
1024 * enabled and pending.
Andrew Walbran69520dc2018-12-06 11:39:38 +00001025 */
Andrew Walbran3d84a262018-12-13 14:41:19 +00001026 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
1027 goto out;
1028 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001029
Andrew Walbran3d84a262018-12-13 14:41:19 +00001030 if (target_vcpu->state == vcpu_state_blocked_interrupt) {
1031 target_vcpu->state = vcpu_state_ready;
1032 } else if (target_vcpu->state == vcpu_state_blocked_mailbox) {
1033 /*
1034 * If you change this logic make sure to update the need_vm_lock
1035 * logic above to match.
1036 */
1037 target_vcpu->state = vcpu_state_ready;
Andrew Walbran69520dc2018-12-06 11:39:38 +00001038
Andrew Walbran3d84a262018-12-13 14:41:19 +00001039 /* Take target vCPU out of mailbox recv_waiter list. */
1040 /*
Andrew Scull6386f252018-12-06 13:29:10 +00001041 * TODO: Consider using a doubly-linked list for the receive
1042 * waiter list to avoid the linear search here.
Andrew Walbran3d84a262018-12-13 14:41:19 +00001043 */
1044 struct vcpu **previous_next_pointer =
1045 &target_vm->mailbox.recv_waiter;
1046 while (*previous_next_pointer != NULL &&
1047 *previous_next_pointer != target_vcpu) {
Andrew Walbran69520dc2018-12-06 11:39:38 +00001048 /*
Andrew Walbran3d84a262018-12-13 14:41:19 +00001049 * TODO(qwandor): Do we need to lock the vCPUs somehow
1050 * while we walk the linked list, or is the VM lock
1051 * enough?
Andrew Walbran69520dc2018-12-06 11:39:38 +00001052 */
Andrew Walbran3d84a262018-12-13 14:41:19 +00001053 previous_next_pointer =
1054 &(*previous_next_pointer)->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +00001055 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001056
Andrew Walbran3d84a262018-12-13 14:41:19 +00001057 if (*previous_next_pointer == NULL) {
1058 dlog("Target VCPU state is vcpu_state_blocked_mailbox "
1059 "but is not in VM mailbox waiter list. This "
1060 "should never happen.\n");
1061 } else {
1062 *previous_next_pointer = target_vcpu->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +00001063 }
1064 }
1065
Andrew Walbran3d84a262018-12-13 14:41:19 +00001066 if (current->vm->id == HF_PRIMARY_VM_ID) {
1067 /*
1068 * If the call came from the primary VM, let it know that it
1069 * should run or kick the target vCPU.
1070 */
1071 ret = 1;
1072 } else if (current != target_vcpu) {
1073 /*
1074 * Switch to the primary so that it can switch to the target, or
1075 * kick it if it is already running on a different physical CPU.
1076 */
1077 struct hf_vcpu_run_return ret = {
1078 .code = HF_VCPU_RUN_WAKE_UP,
1079 .wake_up.vm_id = target_vm_id,
1080 .wake_up.vcpu = target_vcpu_idx,
1081 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001082
Andrew Walbran3d84a262018-12-13 14:41:19 +00001083 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
1084 }
1085
1086out:
1087 /* Either way, make it pending. */
1088 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
1089
Andrew Walbran318f5732018-11-20 16:23:42 +00001090 sl_unlock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +00001091 if (need_vm_lock) {
1092 sl_unlock(&target_vm->lock);
1093 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001094
Andrew Walbran3d84a262018-12-13 14:41:19 +00001095 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00001096}
Andrew Scull6386f252018-12-06 13:29:10 +00001097
1098/**
1099 * Clears a region of physical memory by overwriting it with zeros. The data is
1100 * flushed from the cache so the memory has been cleared across the system.
1101 */
1102static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1103{
1104 /*
1105 * TODO: change this to a cpu local single page window rather than a
1106 * global mapping of the whole range. Such an approach will limit
1107 * the changes to stage-1 tables and will allow only local
1108 * invalidation.
1109 */
1110 void *ptr = mm_identity_map(begin, end, MM_MODE_W, ppool);
1111 size_t size = pa_addr(end) - pa_addr(begin);
1112
1113 if (!ptr) {
1114 /* TODO: partial defrag of failed range. */
1115 /* Recover any memory consumed in failed mapping. */
1116 mm_defrag(ppool);
1117 return false;
1118 }
1119
1120 memset(ptr, 0, size);
1121 arch_mm_write_back_dcache(ptr, size);
1122 mm_unmap(begin, end, ppool);
1123
1124 return true;
1125}
1126
1127/**
1128 * Shares memory from the calling VM with another. The memory can be shared in
1129 * different modes.
1130 *
1131 * TODO: the interface for sharing memory will need to be enhanced to allow
1132 * sharing with different modes e.g. read-only, informing the recipient
1133 * of the memory they have been given, opting to not wipe the memory and
1134 * possibly allowing multiple blocks to be transferred. What this will
1135 * look like is TBD.
1136 */
1137int64_t api_share_memory(uint32_t vm_id, ipaddr_t addr, size_t size,
1138 enum hf_share share, struct vcpu *current)
1139{
1140 struct vm *from = current->vm;
1141 struct vm *to;
1142 int orig_from_mode;
1143 int from_mode;
1144 int to_mode;
1145 ipaddr_t begin;
1146 ipaddr_t end;
1147 paddr_t pa_begin;
1148 paddr_t pa_end;
1149 struct mpool local_page_pool;
1150 int64_t ret;
1151
1152 /* Disallow reflexive shares as this suggests an error in the VM. */
1153 if (vm_id == from->id) {
1154 return -1;
1155 }
1156
1157 /* Ensure the target VM exists. */
1158 to = vm_get(vm_id);
1159 if (to == NULL) {
1160 return -1;
1161 }
1162
1163 begin = addr;
1164 end = ipa_add(addr, size);
1165
1166 /* Fail if addresses are not page-aligned. */
1167 if ((ipa_addr(begin) & (PAGE_SIZE - 1)) ||
1168 (ipa_addr(end) & (PAGE_SIZE - 1))) {
1169 return -1;
1170 }
1171
1172 /* Convert the sharing request to memory management modes. */
1173 switch (share) {
1174 case HF_MEMORY_GIVE:
1175 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1176 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1177 break;
1178
1179 case HF_MEMORY_LEND:
1180 from_mode = MM_MODE_INVALID;
1181 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1182 break;
1183
1184 case HF_MEMORY_SHARE:
1185 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1186 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1187 MM_MODE_SHARED;
1188 break;
1189
1190 default:
1191 /* The input is untrusted so might not be a valid value. */
1192 return -1;
1193 }
1194
1195 /*
1196 * Create a local pool so any freed memory can't be used by another
1197 * thread. This is to ensure the original mapping can be restored if any
1198 * stage of the process fails.
1199 */
1200 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1201
1202 sl_lock_both(&from->lock, &to->lock);
1203
1204 /*
1205 * Ensure that the memory range is mapped with the same mode so that
1206 * changes can be reverted if the process fails.
1207 */
1208 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1209 goto fail;
1210 }
1211
1212 /*
1213 * Ensure the memory range is valid for the sender. If it isn't, the
1214 * sender has either shared it with another VM already or has no claim
1215 * to the memory.
1216 */
1217 if (orig_from_mode & MM_MODE_INVALID) {
1218 goto fail;
1219 }
1220
1221 /*
1222 * The sender must own the memory and have exclusive access to it in
1223 * order to share it. Alternatively, it is giving memory back to the
1224 * owning VM.
1225 */
1226 if (orig_from_mode & MM_MODE_UNOWNED) {
1227 int orig_to_mode;
1228
1229 if (share != HF_MEMORY_GIVE ||
1230 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1231 orig_to_mode & MM_MODE_UNOWNED) {
1232 goto fail;
1233 }
1234 } else if (orig_from_mode & MM_MODE_SHARED) {
1235 goto fail;
1236 }
1237
1238 pa_begin = pa_from_ipa(begin);
1239 pa_end = pa_from_ipa(end);
1240
1241 /*
1242 * First update the mapping for the sender so there is not overlap with
1243 * the recipient.
1244 */
1245 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1246 NULL, &local_page_pool)) {
1247 goto fail;
1248 }
1249
1250 /* Clear the memory so no VM or device can see the previous contents. */
1251 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1252 goto fail_return_to_sender;
1253 }
1254
1255 /* Complete the transfer by mapping the memory into the recipient. */
1256 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1257 &local_page_pool)) {
1258 /* TODO: partial defrag of failed range. */
1259 /* Recover any memory consumed in failed mapping. */
1260 mm_vm_defrag(&from->ptable, &local_page_pool);
1261 goto fail_return_to_sender;
1262 }
1263
1264 ret = 0;
1265 goto out;
1266
1267fail_return_to_sender:
1268 mm_vm_identity_map(&from->ptable, pa_begin, pa_end, orig_from_mode,
1269 NULL, &local_page_pool);
1270
1271fail:
1272 ret = -1;
1273
1274out:
1275 sl_unlock(&from->lock);
1276 sl_unlock(&to->lock);
1277
1278 mpool_fini(&local_page_pool);
1279
1280 return ret;
1281}