blob: 5b32e02ce8e5072bc81fd76d318723959e012f9c [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 Scull38772ab2019-01-24 15:16:50 +0000124 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000125 */
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 Scull38772ab2019-01-24 15:16:50 +0000223static bool api_vcpu_prepare_run(const 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 Scull38772ab2019-01-24 15:16:50 +0000282 const struct vcpu *current,
283 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100284{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100285 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100286 struct vcpu *vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000287 struct retval_state vcpu_retval;
Andrew Scull6d2db332018-10-10 15:28:17 +0100288 struct hf_vcpu_run_return ret = {
289 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
290 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100291
292 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100293 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100294 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100295 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100296
Andrew Scull19503262018-09-20 14:48:39 +0100297 /* Only secondary VM vcpus can be run. */
298 if (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 /* The requested VM must exist. */
303 vm = vm_get(vm_id);
304 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100305 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100306 }
307
308 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100309 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100310 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100311 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100312
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000313 /* Update state if allowed. */
Andrew Scullf3d45592018-09-20 14:30:22 +0100314 vcpu = &vm->vcpus[vcpu_idx];
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000315 if (!api_vcpu_prepare_run(current, vcpu, &vcpu_retval)) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100316 ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000317 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100318 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000319
Andrew Scull33fecd32019-01-08 14:48:27 +0000320 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000321 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000322
Andrew Scull33fecd32019-01-08 14:48:27 +0000323 /*
324 * Set a placeholder return code to the scheduler. This will be
325 * overwritten when the switch back to the primary occurs.
326 */
327 ret.code = HF_VCPU_RUN_PREEMPTED;
328
329 /* Update return value for the next vcpu if one was injected. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000330 if (vcpu_retval.force) {
331 arch_regs_set_retval(&vcpu->regs, vcpu_retval.value);
332 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100333
Andrew Scull6d2db332018-10-10 15:28:17 +0100334out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100335 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100336}
337
338/**
Andrew Scull81e85092018-12-12 12:56:20 +0000339 * Check that the mode indicates memory that is valid, owned and exclusive.
340 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000341static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000342{
343 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
344 0;
345}
346
347/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000348 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
349 * after they've succeeded. If a secondary VM is running and there are waiters,
350 * it also switches back to the primary VM for it to wake waiters up.
351 */
352static int64_t api_waiter_result(struct vm_locked locked_vm,
353 struct vcpu *current, struct vcpu **next)
354{
355 struct vm *vm = locked_vm.vm;
356 struct hf_vcpu_run_return ret = {
357 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
358 };
359
360 if (list_empty(&vm->mailbox.waiter_list)) {
361 /* No waiters, nothing else to do. */
362 return 0;
363 }
364
365 if (vm->id == HF_PRIMARY_VM_ID) {
366 /* The caller is the primary VM. Tell it to wake up waiters. */
367 return 1;
368 }
369
370 /*
371 * Switch back to the primary VM, informing it that there are waiters
372 * that need to be notified.
373 */
374 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
375
376 return 0;
377}
378
379/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100380 * Configures the VM to send/receive data through the specified pages. The pages
381 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000382 *
383 * Returns:
384 * - -1 on failure.
385 * - 0 on success if no further action is needed.
386 * - 1 if it was called by the primary VM and the primary VM now needs to wake
387 * up or kick waiters. Waiters should be retrieved by calling
388 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100389 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000390int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
391 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100392{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100393 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000394 struct vm_locked locked;
Andrew Scull80871322018-08-06 12:04:09 +0100395 paddr_t pa_send_begin;
396 paddr_t pa_send_end;
397 paddr_t pa_recv_begin;
398 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000399 int orig_send_mode;
400 int orig_recv_mode;
401 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100402 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100403
404 /* Fail if addresses are not page-aligned. */
Andrew Scull265ada92018-07-30 15:19:01 +0100405 if ((ipa_addr(send) & (PAGE_SIZE - 1)) ||
406 (ipa_addr(recv) & (PAGE_SIZE - 1))) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100407 return -1;
408 }
409
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000410 /* Convert to physical addresses. */
411 pa_send_begin = pa_from_ipa(send);
412 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
413
414 pa_recv_begin = pa_from_ipa(recv);
415 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
416
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100417 /* Fail if the same page is used for the send and receive pages. */
418 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000419 return -1;
420 }
421
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000422 vm_lock(vm, &locked);
Andrew Scull220e6212018-12-21 18:09:00 +0000423
424 /* We only allow these to be setup once. */
425 if (vm->mailbox.send || vm->mailbox.recv) {
426 goto fail;
427 }
428
429 /*
430 * Ensure the pages are valid, owned and exclusive to the VM and that
431 * the VM has the required access to the memory.
432 */
433 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
434 &orig_send_mode) ||
435 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
436 (orig_send_mode & MM_MODE_R) == 0 ||
437 (orig_send_mode & MM_MODE_W) == 0) {
438 goto fail;
439 }
440
441 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
442 &orig_recv_mode) ||
443 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
444 (orig_recv_mode & MM_MODE_R) == 0) {
445 goto fail;
446 }
447
448 /*
449 * Create a local pool so any freed memory can't be used by another
450 * thread. This is to ensure the original mapping can be restored if any
451 * stage of the process fails.
452 */
453 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
454
455 /* Take memory ownership away from the VM and mark as shared. */
456 if (!mm_vm_identity_map(
457 &vm->ptable, pa_send_begin, pa_send_end,
458 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
459 NULL, &local_page_pool)) {
460 goto fail_free_pool;
461 }
462
463 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
464 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
465 NULL, &local_page_pool)) {
466 /* TODO: partial defrag of failed range. */
467 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000468 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000469 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100470 }
471
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100472 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000473 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000474 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100475 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000476 /* TODO: partial defrag of failed range. */
477 /* Recover any memory consumed in failed mapping. */
478 mm_defrag(&local_page_pool);
479 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100480 }
481
482 /*
483 * Map the receive page as writable in the hypervisor address space. On
484 * failure, unmap the send page before returning.
485 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000486 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000487 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100488 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000489 /* TODO: partial defrag of failed range. */
490 /* Recover any memory consumed in failed mapping. */
491 mm_defrag(&local_page_pool);
492 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100493 }
494
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000495 /* Tell caller about waiters, if any. */
496 ret = api_waiter_result(locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000497 goto exit;
498
499 /*
500 * The following mappings will not require more memory than is available
501 * in the local pool.
502 */
503fail_undo_all:
504 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000505 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000506
507fail_undo_send_and_recv:
508 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
509 orig_recv_mode, NULL, &local_page_pool);
510
511fail_undo_send:
512 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
513 orig_send_mode, NULL, &local_page_pool);
514
515fail_free_pool:
516 mpool_fini(&local_page_pool);
517
518fail:
519 ret = -1;
520
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100521exit:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000522 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100523
524 return ret;
525}
526
527/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100528 * Copies data from the sender's send buffer to the recipient's receive buffer
529 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000530 *
531 * If the recipient's receive buffer is busy, it can optionally register the
532 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100533 */
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000534int64_t api_mailbox_send(uint32_t vm_id, size_t size, bool notify,
535 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100536{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100537 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100538 struct vm *to;
539 const void *from_buf;
Andrew Scullaa039b32018-10-04 15:02:26 +0100540 uint16_t vcpu;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100541 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100542
Andrew Scullaa039b32018-10-04 15:02:26 +0100543 /* Limit the size of transfer. */
544 if (size > HF_MAILBOX_SIZE) {
Andrew Scull19503262018-09-20 14:48:39 +0100545 return -1;
546 }
547
548 /* Disallow reflexive requests as this suggests an error in the VM. */
549 if (vm_id == from->id) {
550 return -1;
551 }
552
553 /* Ensure the target VM exists. */
554 to = vm_get(vm_id);
555 if (to == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100556 return -1;
557 }
558
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100559 /*
560 * Check that the sender has configured its send buffer. It is safe to
561 * use from_buf after releasing the lock because the buffer cannot be
562 * modified once it's configured.
563 */
564 sl_lock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100565 from_buf = from->mailbox.send;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100566 sl_unlock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100567 if (from_buf == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100568 return -1;
569 }
570
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100571 sl_lock(&to->lock);
572
Andrew Scullaa039b32018-10-04 15:02:26 +0100573 if (to->mailbox.state != mailbox_state_empty ||
574 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000575 /*
576 * Fail if the target isn't currently ready to receive data,
577 * setting up for notification if requested.
578 */
579 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000580 struct wait_entry *entry =
581 &current->vm->wait_entries[vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000582
583 /* Append waiter only if it's not there yet. */
584 if (list_empty(&entry->wait_links)) {
585 list_append(&to->mailbox.waiter_list,
586 &entry->wait_links);
587 }
588 }
589
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100590 ret = -1;
Andrew Scullaa039b32018-10-04 15:02:26 +0100591 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100592 }
593
Andrew Scullaa039b32018-10-04 15:02:26 +0100594 /* Copy data. */
595 memcpy(to->mailbox.recv, from_buf, size);
596 to->mailbox.recv_bytes = size;
597 to->mailbox.recv_from_id = from->id;
598 to->mailbox.state = mailbox_state_read;
599
600 /* Messages for the primary VM are delivered directly. */
601 if (to->id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000602 struct hf_vcpu_run_return primary_ret = {
603 .code = HF_VCPU_RUN_MESSAGE,
604 .message.size = size,
605 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000606
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000607 *next = api_switch_to_primary(current, primary_ret,
608 vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100609 ret = 0;
610 goto out;
611 }
612
613 /*
614 * Try to find a vcpu to handle the message and tell the scheduler to
615 * run it.
616 */
617 if (to->mailbox.recv_waiter == NULL) {
618 /*
619 * The scheduler must choose a vcpu to interrupt so it can
620 * handle the message.
621 */
622 to->mailbox.state = mailbox_state_received;
623 vcpu = HF_INVALID_VCPU;
624 } else {
625 struct vcpu *to_vcpu = to->mailbox.recv_waiter;
626
627 /*
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000628 * Take target vcpu out of waiter list and mark it as ready to
629 * run again.
Andrew Scullaa039b32018-10-04 15:02:26 +0100630 */
631 sl_lock(&to_vcpu->lock);
632 to->mailbox.recv_waiter = to_vcpu->mailbox_next;
633 to_vcpu->state = vcpu_state_ready;
634
635 /* Return from HF_MAILBOX_RECEIVE. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000636 to_vcpu->retval.force = true;
637 to_vcpu->retval.value = hf_mailbox_receive_return_encode(
638 (struct hf_mailbox_receive_return){
639 .vm_id = to->mailbox.recv_from_id,
640 .size = size,
641 });
Andrew Scullaa039b32018-10-04 15:02:26 +0100642
643 sl_unlock(&to_vcpu->lock);
644
645 vcpu = to_vcpu - to->vcpus;
646 }
647
648 /* Return to the primary VM directly or with a switch. */
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000649 if (from->id == HF_PRIMARY_VM_ID) {
650 ret = vcpu;
651 } else {
652 struct hf_vcpu_run_return primary_ret = {
653 .code = HF_VCPU_RUN_WAKE_UP,
654 .wake_up.vm_id = to->id,
655 .wake_up.vcpu = vcpu,
656 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000657
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000658 *next = api_switch_to_primary(current, primary_ret,
659 vcpu_state_ready);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000660 ret = 0;
661 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100662
663out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100664 sl_unlock(&to->lock);
665
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000666 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100667}
668
669/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100670 * Receives a message from the mailbox. If one isn't available, this function
671 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100672 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100673 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100674 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100675struct hf_mailbox_receive_return api_mailbox_receive(bool block,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100676 struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100677 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100678{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100679 struct vm *vm = current->vm;
Andrew Scull6d2db332018-10-10 15:28:17 +0100680 struct hf_mailbox_receive_return ret = {
681 .vm_id = HF_INVALID_VM_ID,
682 };
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100683
Andrew Scullaa039b32018-10-04 15:02:26 +0100684 /*
685 * The primary VM will receive messages as a status code from running
686 * vcpus and must not call this function.
687 */
Andrew Scull19503262018-09-20 14:48:39 +0100688 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100689 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100690 }
691
692 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100693
Andrew Scullaa039b32018-10-04 15:02:26 +0100694 /* Return pending messages without blocking. */
695 if (vm->mailbox.state == mailbox_state_received) {
696 vm->mailbox.state = mailbox_state_read;
Andrew Scull6d2db332018-10-10 15:28:17 +0100697 ret.vm_id = vm->mailbox.recv_from_id;
698 ret.size = vm->mailbox.recv_bytes;
Andrew Scullaa039b32018-10-04 15:02:26 +0100699 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100700 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100701
702 /* No pending message so fail if not allowed to block. */
703 if (!block) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100704 goto out;
705 }
706
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100707 sl_lock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100708
709 /* Push vcpu into waiter list. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100710 current->mailbox_next = vm->mailbox.recv_waiter;
711 vm->mailbox.recv_waiter = current;
712 sl_unlock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100713
714 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000715 {
716 struct hf_vcpu_run_return run_return = {
717 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
718 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000719
Andrew Walbranb4816552018-12-05 17:35:42 +0000720 *next = api_switch_to_primary(current, run_return,
721 vcpu_state_blocked_mailbox);
722 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100723out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100724 sl_unlock(&vm->lock);
725
726 return ret;
727}
728
729/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000730 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
731 * by this function, the caller must have called api_mailbox_send before with
732 * the notify argument set to true, and this call must have failed because the
733 * mailbox was not available.
734 *
735 * It should be called repeatedly to retrieve a list of VMs.
736 *
737 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
738 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100739 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000740int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100741{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100742 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000743 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100744 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100745
746 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000747 if (list_empty(&vm->mailbox.ready_list)) {
748 ret = -1;
749 goto exit;
750 }
751
752 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
753 ready_links);
754 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000755 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000756
757exit:
758 sl_unlock(&vm->lock);
759 return ret;
760}
761
762/**
763 * Retrieves the next VM waiting to be notified that the mailbox of the
764 * specified VM became writable. Only primary VMs are allowed to call this.
765 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000766 * Returns -1 on failure or if there are no waiters; the VM id of the next
767 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000768 */
769int64_t api_mailbox_waiter_get(uint32_t vm_id, const struct vcpu *current)
770{
771 struct vm *vm;
772 struct vm_locked locked;
773 struct wait_entry *entry;
774 struct vm *waiting_vm;
775
776 /* Only primary VMs are allowed to call this function. */
777 if (current->vm->id != HF_PRIMARY_VM_ID) {
778 return -1;
779 }
780
781 vm = vm_get(vm_id);
782 if (vm == NULL) {
783 return -1;
784 }
785
786 /* Check if there are outstanding notifications from given vm. */
787 vm_lock(vm, &locked);
788 entry = api_fetch_waiter(locked);
789 vm_unlock(&locked);
790
791 if (entry == NULL) {
792 return -1;
793 }
794
795 /* Enqueue notification to waiting VM. */
796 waiting_vm = entry->waiting_vm;
797
798 sl_lock(&waiting_vm->lock);
799 if (list_empty(&entry->ready_links)) {
800 list_append(&waiting_vm->mailbox.ready_list,
801 &entry->ready_links);
802 }
803 sl_unlock(&waiting_vm->lock);
804
805 return waiting_vm->id;
806}
807
808/**
809 * Clears the caller's mailbox so that a new message can be received. The caller
810 * must have copied out all data they wish to preserve as new messages will
811 * overwrite the old and will arrive asynchronously.
812 *
813 * Returns:
814 * - -1 on failure, if the mailbox hasn't been read or is already empty.
815 * - 0 on success if no further action is needed.
816 * - 1 if it was called by the primary VM and the primary VM now needs to wake
817 * up or kick waiters. Waiters should be retrieved by calling
818 * hf_mailbox_waiter_get.
819 */
820int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
821{
822 struct vm *vm = current->vm;
823 struct vm_locked locked;
824 int64_t ret;
825
826 vm_lock(vm, &locked);
Andrew Scullaa039b32018-10-04 15:02:26 +0100827 if (vm->mailbox.state == mailbox_state_read) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000828 ret = api_waiter_result(locked, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +0100829 vm->mailbox.state = mailbox_state_empty;
830 } else {
831 ret = -1;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100832 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000833 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100834
835 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100836}
Andrew Walbran318f5732018-11-20 16:23:42 +0000837
838/**
839 * Enables or disables a given interrupt ID for the calling vCPU.
840 *
841 * Returns 0 on success, or -1 if the intid is invalid.
842 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000843int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000844{
845 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
846 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000847
Andrew Walbran318f5732018-11-20 16:23:42 +0000848 if (intid >= HF_NUM_INTIDS) {
849 return -1;
850 }
851
852 sl_lock(&current->lock);
853 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000854 /*
855 * If it is pending and was not enabled before, increment the
856 * count.
857 */
858 if (current->interrupts.interrupt_pending[intid_index] &
859 ~current->interrupts.interrupt_enabled[intid_index] &
860 intid_mask) {
861 current->interrupts.enabled_and_pending_count++;
862 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000863 current->interrupts.interrupt_enabled[intid_index] |=
864 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +0000865 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000866 /*
867 * If it is pending and was enabled before, decrement the count.
868 */
869 if (current->interrupts.interrupt_pending[intid_index] &
870 current->interrupts.interrupt_enabled[intid_index] &
871 intid_mask) {
872 current->interrupts.enabled_and_pending_count--;
873 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000874 current->interrupts.interrupt_enabled[intid_index] &=
875 ~intid_mask;
876 }
877
878 sl_unlock(&current->lock);
879 return 0;
880}
881
882/**
883 * Returns the ID of the next pending interrupt for the calling vCPU, and
884 * acknowledges it (i.e. marks it as no longer pending). Returns
885 * HF_INVALID_INTID if there are no pending interrupts.
886 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000887uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000888{
889 uint8_t i;
890 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +0000891
892 /*
893 * Find the first enabled and pending interrupt ID, return it, and
894 * deactivate it.
895 */
896 sl_lock(&current->lock);
897 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
898 uint32_t enabled_and_pending =
899 current->interrupts.interrupt_enabled[i] &
900 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000901
Andrew Walbran318f5732018-11-20 16:23:42 +0000902 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000903 uint8_t bit_index = ctz(enabled_and_pending);
904 /*
905 * Mark it as no longer pending and decrement the count.
906 */
907 current->interrupts.interrupt_pending[i] &=
908 ~(1u << bit_index);
909 current->interrupts.enabled_and_pending_count--;
910 first_interrupt =
911 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +0000912 break;
913 }
914 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000915
916 sl_unlock(&current->lock);
917 return first_interrupt;
918}
919
920/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +0000921 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +0000922 * given VM and vCPU.
923 */
924static inline bool is_injection_allowed(uint32_t target_vm_id,
925 struct vcpu *current)
926{
927 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000928
Andrew Walbran318f5732018-11-20 16:23:42 +0000929 /*
930 * The primary VM is allowed to inject interrupts into any VM. Secondary
931 * VMs are only allowed to inject interrupts into their own vCPUs.
932 */
933 return current_vm_id == HF_PRIMARY_VM_ID ||
934 current_vm_id == target_vm_id;
935}
936
937/**
938 * Injects a virtual interrupt of the given ID into the given target vCPU.
939 * This doesn't cause the vCPU to actually be run immediately; it will be taken
940 * when the vCPU is next run, which is up to the scheduler.
941 *
Andrew Walbran3d84a262018-12-13 14:41:19 +0000942 * Returns:
943 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
944 * ID is invalid, or the current VM is not allowed to inject interrupts to
945 * the target VM.
946 * - 0 on success if no further action is needed.
947 * - 1 if it was called by the primary VM and the primary VM now needs to wake
948 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +0000949 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000950int64_t api_interrupt_inject(uint32_t target_vm_id, uint32_t target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +0000951 uint32_t intid, struct vcpu *current,
952 struct vcpu **next)
953{
954 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
955 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
956 struct vcpu *target_vcpu;
957 struct vm *target_vm = vm_get(target_vm_id);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000958 bool need_vm_lock;
Andrew Walbran3d84a262018-12-13 14:41:19 +0000959 int64_t ret = 0;
Andrew Walbran318f5732018-11-20 16:23:42 +0000960
961 if (intid >= HF_NUM_INTIDS) {
962 return -1;
963 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000964
Andrew Walbran318f5732018-11-20 16:23:42 +0000965 if (target_vm == NULL) {
966 return -1;
967 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000968
Andrew Walbran318f5732018-11-20 16:23:42 +0000969 if (target_vcpu_idx >= target_vm->vcpu_count) {
970 /* The requested vcpu must exist. */
971 return -1;
972 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000973
Andrew Walbran318f5732018-11-20 16:23:42 +0000974 if (!is_injection_allowed(target_vm_id, current)) {
975 return -1;
976 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000977
Andrew Walbran318f5732018-11-20 16:23:42 +0000978 target_vcpu = &target_vm->vcpus[target_vcpu_idx];
979
980 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
981 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
982
983 sl_lock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000984 /*
985 * If we need the target_vm lock we need to release the target_vcpu lock
986 * first to maintain the correct order of locks. In-between releasing
987 * and acquiring it again the state of the vCPU could change in such a
988 * way that we don't actually need to touch the target_vm after all, but
989 * that's alright: we'll take the target_vm lock anyway, but it's safe,
990 * just perhaps a little slow in this unusual case. The reverse is not
991 * possible: if need_vm_lock is false, we don't release the target_vcpu
992 * lock until we are done, so nothing should change in such as way that
993 * we need the VM lock after all.
994 */
Andrew Walbran3d84a262018-12-13 14:41:19 +0000995 need_vm_lock =
996 (target_vcpu->interrupts.interrupt_enabled[intid_index] &
997 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
998 intid_mask) &&
999 target_vcpu->state == vcpu_state_blocked_mailbox;
Andrew Walbran69520dc2018-12-06 11:39:38 +00001000 if (need_vm_lock) {
1001 sl_unlock(&target_vcpu->lock);
1002 sl_lock(&target_vm->lock);
1003 sl_lock(&target_vcpu->lock);
1004 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001005
Andrew Walbran3d84a262018-12-13 14:41:19 +00001006 /*
1007 * We only need to change state and (maybe) trigger a virtual IRQ if it
1008 * is enabled and was not previously pending. Otherwise we can skip
1009 * everything except setting the pending bit.
1010 *
1011 * If you change this logic make sure to update the need_vm_lock logic
1012 * above to match.
1013 */
1014 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
1015 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
1016 intid_mask)) {
1017 goto out;
1018 }
1019
1020 /* Increment the count. */
1021 target_vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran318f5732018-11-20 16:23:42 +00001022
Andrew Walbran69520dc2018-12-06 11:39:38 +00001023 /*
Andrew Scull6386f252018-12-06 13:29:10 +00001024 * Only need to update state if there was not already an interrupt
1025 * enabled and pending.
Andrew Walbran69520dc2018-12-06 11:39:38 +00001026 */
Andrew Walbran3d84a262018-12-13 14:41:19 +00001027 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
1028 goto out;
1029 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001030
Andrew Walbran3d84a262018-12-13 14:41:19 +00001031 if (target_vcpu->state == vcpu_state_blocked_interrupt) {
1032 target_vcpu->state = vcpu_state_ready;
1033 } else if (target_vcpu->state == vcpu_state_blocked_mailbox) {
1034 /*
1035 * If you change this logic make sure to update the need_vm_lock
1036 * logic above to match.
1037 */
1038 target_vcpu->state = vcpu_state_ready;
Andrew Walbran69520dc2018-12-06 11:39:38 +00001039
Andrew Walbran3d84a262018-12-13 14:41:19 +00001040 /* Take target vCPU out of mailbox recv_waiter list. */
1041 /*
Andrew Scull6386f252018-12-06 13:29:10 +00001042 * TODO: Consider using a doubly-linked list for the receive
1043 * waiter list to avoid the linear search here.
Andrew Walbran3d84a262018-12-13 14:41:19 +00001044 */
1045 struct vcpu **previous_next_pointer =
1046 &target_vm->mailbox.recv_waiter;
1047 while (*previous_next_pointer != NULL &&
1048 *previous_next_pointer != target_vcpu) {
Andrew Walbran69520dc2018-12-06 11:39:38 +00001049 /*
Andrew Walbran3d84a262018-12-13 14:41:19 +00001050 * TODO(qwandor): Do we need to lock the vCPUs somehow
1051 * while we walk the linked list, or is the VM lock
1052 * enough?
Andrew Walbran69520dc2018-12-06 11:39:38 +00001053 */
Andrew Walbran3d84a262018-12-13 14:41:19 +00001054 previous_next_pointer =
1055 &(*previous_next_pointer)->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +00001056 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001057
Andrew Walbran3d84a262018-12-13 14:41:19 +00001058 if (*previous_next_pointer == NULL) {
1059 dlog("Target VCPU state is vcpu_state_blocked_mailbox "
1060 "but is not in VM mailbox waiter list. This "
1061 "should never happen.\n");
1062 } else {
1063 *previous_next_pointer = target_vcpu->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +00001064 }
1065 }
1066
Andrew Walbran3d84a262018-12-13 14:41:19 +00001067 if (current->vm->id == HF_PRIMARY_VM_ID) {
1068 /*
1069 * If the call came from the primary VM, let it know that it
1070 * should run or kick the target vCPU.
1071 */
1072 ret = 1;
1073 } else if (current != target_vcpu) {
1074 /*
1075 * Switch to the primary so that it can switch to the target, or
1076 * kick it if it is already running on a different physical CPU.
1077 */
1078 struct hf_vcpu_run_return ret = {
1079 .code = HF_VCPU_RUN_WAKE_UP,
1080 .wake_up.vm_id = target_vm_id,
1081 .wake_up.vcpu = target_vcpu_idx,
1082 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001083
Andrew Walbran3d84a262018-12-13 14:41:19 +00001084 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
1085 }
1086
1087out:
1088 /* Either way, make it pending. */
1089 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
1090
Andrew Walbran318f5732018-11-20 16:23:42 +00001091 sl_unlock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +00001092 if (need_vm_lock) {
1093 sl_unlock(&target_vm->lock);
1094 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001095
Andrew Walbran3d84a262018-12-13 14:41:19 +00001096 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00001097}
Andrew Scull6386f252018-12-06 13:29:10 +00001098
1099/**
1100 * Clears a region of physical memory by overwriting it with zeros. The data is
1101 * flushed from the cache so the memory has been cleared across the system.
1102 */
1103static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1104{
1105 /*
1106 * TODO: change this to a cpu local single page window rather than a
1107 * global mapping of the whole range. Such an approach will limit
1108 * the changes to stage-1 tables and will allow only local
1109 * invalidation.
1110 */
1111 void *ptr = mm_identity_map(begin, end, MM_MODE_W, ppool);
1112 size_t size = pa_addr(end) - pa_addr(begin);
1113
1114 if (!ptr) {
1115 /* TODO: partial defrag of failed range. */
1116 /* Recover any memory consumed in failed mapping. */
1117 mm_defrag(ppool);
1118 return false;
1119 }
1120
1121 memset(ptr, 0, size);
1122 arch_mm_write_back_dcache(ptr, size);
1123 mm_unmap(begin, end, ppool);
1124
1125 return true;
1126}
1127
1128/**
1129 * Shares memory from the calling VM with another. The memory can be shared in
1130 * different modes.
1131 *
1132 * TODO: the interface for sharing memory will need to be enhanced to allow
1133 * sharing with different modes e.g. read-only, informing the recipient
1134 * of the memory they have been given, opting to not wipe the memory and
1135 * possibly allowing multiple blocks to be transferred. What this will
1136 * look like is TBD.
1137 */
1138int64_t api_share_memory(uint32_t vm_id, ipaddr_t addr, size_t size,
1139 enum hf_share share, struct vcpu *current)
1140{
1141 struct vm *from = current->vm;
1142 struct vm *to;
1143 int orig_from_mode;
1144 int from_mode;
1145 int to_mode;
1146 ipaddr_t begin;
1147 ipaddr_t end;
1148 paddr_t pa_begin;
1149 paddr_t pa_end;
1150 struct mpool local_page_pool;
1151 int64_t ret;
1152
1153 /* Disallow reflexive shares as this suggests an error in the VM. */
1154 if (vm_id == from->id) {
1155 return -1;
1156 }
1157
1158 /* Ensure the target VM exists. */
1159 to = vm_get(vm_id);
1160 if (to == NULL) {
1161 return -1;
1162 }
1163
1164 begin = addr;
1165 end = ipa_add(addr, size);
1166
1167 /* Fail if addresses are not page-aligned. */
1168 if ((ipa_addr(begin) & (PAGE_SIZE - 1)) ||
1169 (ipa_addr(end) & (PAGE_SIZE - 1))) {
1170 return -1;
1171 }
1172
1173 /* Convert the sharing request to memory management modes. */
1174 switch (share) {
1175 case HF_MEMORY_GIVE:
1176 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1177 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1178 break;
1179
1180 case HF_MEMORY_LEND:
1181 from_mode = MM_MODE_INVALID;
1182 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1183 break;
1184
1185 case HF_MEMORY_SHARE:
1186 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1187 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1188 MM_MODE_SHARED;
1189 break;
1190
1191 default:
1192 /* The input is untrusted so might not be a valid value. */
1193 return -1;
1194 }
1195
1196 /*
1197 * Create a local pool so any freed memory can't be used by another
1198 * thread. This is to ensure the original mapping can be restored if any
1199 * stage of the process fails.
1200 */
1201 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1202
1203 sl_lock_both(&from->lock, &to->lock);
1204
1205 /*
1206 * Ensure that the memory range is mapped with the same mode so that
1207 * changes can be reverted if the process fails.
1208 */
1209 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1210 goto fail;
1211 }
1212
1213 /*
1214 * Ensure the memory range is valid for the sender. If it isn't, the
1215 * sender has either shared it with another VM already or has no claim
1216 * to the memory.
1217 */
1218 if (orig_from_mode & MM_MODE_INVALID) {
1219 goto fail;
1220 }
1221
1222 /*
1223 * The sender must own the memory and have exclusive access to it in
1224 * order to share it. Alternatively, it is giving memory back to the
1225 * owning VM.
1226 */
1227 if (orig_from_mode & MM_MODE_UNOWNED) {
1228 int orig_to_mode;
1229
1230 if (share != HF_MEMORY_GIVE ||
1231 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1232 orig_to_mode & MM_MODE_UNOWNED) {
1233 goto fail;
1234 }
1235 } else if (orig_from_mode & MM_MODE_SHARED) {
1236 goto fail;
1237 }
1238
1239 pa_begin = pa_from_ipa(begin);
1240 pa_end = pa_from_ipa(end);
1241
1242 /*
1243 * First update the mapping for the sender so there is not overlap with
1244 * the recipient.
1245 */
1246 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1247 NULL, &local_page_pool)) {
1248 goto fail;
1249 }
1250
1251 /* Clear the memory so no VM or device can see the previous contents. */
1252 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1253 goto fail_return_to_sender;
1254 }
1255
1256 /* Complete the transfer by mapping the memory into the recipient. */
1257 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1258 &local_page_pool)) {
1259 /* TODO: partial defrag of failed range. */
1260 /* Recover any memory consumed in failed mapping. */
1261 mm_vm_defrag(&from->ptable, &local_page_pool);
1262 goto fail_return_to_sender;
1263 }
1264
1265 ret = 0;
1266 goto out;
1267
1268fail_return_to_sender:
1269 mm_vm_identity_map(&from->ptable, pa_begin, pa_end, orig_from_mode,
1270 NULL, &local_page_pool);
1271
1272fail:
1273 ret = -1;
1274
1275out:
1276 sl_unlock(&from->lock);
1277 sl_unlock(&to->lock);
1278
1279 mpool_fini(&local_page_pool);
1280
1281 return ret;
1282}