blob: 08f1e05f2fdfbba800b3c68e20c32691175a3dd9 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010018
Andrew Scull13652af2018-09-17 14:49:08 +010019#include <assert.h>
20
Andrew Walbran318f5732018-11-20 16:23:42 +000021#include "hf/arch/cpu.h"
22
23#include "hf/dlog.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/std.h"
25#include "hf/vm.h"
26
Andrew Scullf35a5c92018-08-07 18:09:46 +010027#include "vmapi/hf/call.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010028
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000029/*
30 * To eliminate the risk of deadlocks, we define a partial order for the
31 * acquisition of locks held concurrently by the same physical CPU. Our current
32 * ordering requirements are as follows:
33 *
34 * vm::lock -> vcpu::lock
35 */
36
Andrew Scullaa039b32018-10-04 15:02:26 +010037static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010038 "Currently, a page is mapped for the send and receive buffers so "
39 "the maximum request is the size of a page.");
40
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000041static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000042
43/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000044 * Initialises the API page pool by taking ownership of the contents of the
45 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000046 */
47void api_init(struct mpool *ppool)
48{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000049 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000050}
51
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010052/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010053 * Switches the physical CPU back to the corresponding vcpu of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010054 *
55 * This triggers the scheduling logic to run. Run in the context of secondary VM
56 * to cause HF_VCPU_RUN to return and the primary VM to regain control of the
57 * cpu.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010058 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010059static struct vcpu *api_switch_to_primary(struct vcpu *current,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000060 struct hf_vcpu_run_return primary_ret,
61 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010062{
Andrew Scull19503262018-09-20 14:48:39 +010063 struct vm *primary = vm_get(HF_PRIMARY_VM_ID);
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010064 struct vcpu *next = &primary->vcpus[cpu_index(current->cpu)];
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010065
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010066 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Scull6d2db332018-10-10 15:28:17 +010067 arch_regs_set_retval(&next->regs,
68 hf_vcpu_run_return_encode(primary_ret));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010069
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000070 /* Mark the current vcpu as waiting. */
71 sl_lock(&current->lock);
72 current->state = secondary_state;
73 sl_unlock(&current->lock);
74
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010075 return next;
76}
77
78/**
Andrew Scull33fecd32019-01-08 14:48:27 +000079 * Returns to the primary vm and signals that the vcpu still has work to do so.
80 */
81struct vcpu *api_preempt(struct vcpu *current)
82{
83 struct hf_vcpu_run_return ret = {
84 .code = HF_VCPU_RUN_PREEMPTED,
85 };
86
87 return api_switch_to_primary(current, ret, vcpu_state_ready);
88}
89
90/**
91 * Returns to the primary vm to allow this cpu to be used for other tasks as the
92 * vcpu does not have work to do at this moment. The current vcpu is marked as
93 * ready to be scheduled again.
Andrew Scullaa039b32018-10-04 15:02:26 +010094 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010095struct vcpu *api_yield(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +010096{
Andrew Scull6d2db332018-10-10 15:28:17 +010097 struct hf_vcpu_run_return ret = {
98 .code = HF_VCPU_RUN_YIELD,
99 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000100
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000101 return api_switch_to_primary(current, ret, vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100102}
103
104/**
105 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
106 * vm.
107 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100108struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100109{
Andrew Scull6d2db332018-10-10 15:28:17 +0100110 struct hf_vcpu_run_return ret = {
111 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
112 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000113
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000114 return api_switch_to_primary(current, ret,
115 vcpu_state_blocked_interrupt);
Andrew Scullaa039b32018-10-04 15:02:26 +0100116}
117
118/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000119 * Returns the ID of the VM.
120 */
121int64_t api_vm_get_id(const struct vcpu *current)
122{
123 return current->vm->id;
124}
125
126/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100127 * Returns the number of VMs configured to run.
128 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100129int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100130{
Andrew Scull19503262018-09-20 14:48:39 +0100131 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100132}
133
134/**
135 * Returns the number of vcpus configured in the given VM.
136 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100137int64_t api_vcpu_get_count(uint32_t vm_id, const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100138{
Andrew Scull19503262018-09-20 14:48:39 +0100139 struct vm *vm;
140
141 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100142 if (current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100143 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100144 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100145
Andrew Scull19503262018-09-20 14:48:39 +0100146 vm = vm_get(vm_id);
147 if (vm == NULL) {
148 return -1;
149 }
150
151 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100152}
153
154/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000155 * This function is called by the architecture-specific context switching
156 * function to indicate that register state for the given vcpu has been saved
157 * and can therefore be used by other pcpus.
158 */
159void api_regs_state_saved(struct vcpu *vcpu)
160{
161 sl_lock(&vcpu->lock);
162 vcpu->regs_available = true;
163 sl_unlock(&vcpu->lock);
164}
165
166/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000167 * Retrieves the next waiter and removes it from the wait list if the VM's
168 * mailbox is in a writable state.
169 */
170static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
171{
172 struct wait_entry *entry;
173 struct vm *vm = locked_vm.vm;
174
175 if (vm->mailbox.state != mailbox_state_empty ||
176 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
177 /* The mailbox is not writable or there are no waiters. */
178 return NULL;
179 }
180
181 /* Remove waiter from the wait list. */
182 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
183 wait_links);
184 list_remove(&entry->wait_links);
185 return entry;
186}
187
188/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000189 * Prepares the vcpu to run by updating its state and fetching whether a return
190 * value needs to be forced onto the vCPU.
191 */
192static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
193 struct retval_state *vcpu_retval)
194{
195 bool ret;
196
197 sl_lock(&vcpu->lock);
198 if (vcpu->state != vcpu_state_ready) {
199 ret = false;
200 goto out;
201 }
202
203 vcpu->cpu = current->cpu;
204 vcpu->state = vcpu_state_running;
205
206 /* Fetch return value to inject into vCPU if there is one. */
207 *vcpu_retval = vcpu->retval;
208 if (vcpu_retval->force) {
209 vcpu->retval.force = false;
210 }
211
212 /*
213 * Wait until the registers become available. Care must be taken when
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000214 * looping on this: it shouldn't be done while holding other locks to
215 * avoid deadlocks.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000216 */
217 while (!vcpu->regs_available) {
218 sl_unlock(&vcpu->lock);
219 sl_lock(&vcpu->lock);
220 }
221
222 /*
223 * Mark the registers as unavailable now that we're about to reflect
224 * them onto the real registers. This will also prevent another physical
225 * CPU from trying to read these registers.
226 */
227 vcpu->regs_available = false;
228
229 ret = true;
230
231out:
232 sl_unlock(&vcpu->lock);
233 return ret;
234}
235
236/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100237 * Runs the given vcpu of the given vm.
238 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100239struct hf_vcpu_run_return api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100240 const struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100241 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100242{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100243 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100244 struct vcpu *vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000245 struct retval_state vcpu_retval;
Andrew Scull6d2db332018-10-10 15:28:17 +0100246 struct hf_vcpu_run_return ret = {
247 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
248 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100249
250 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100251 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100252 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100253 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100254
Andrew Scull19503262018-09-20 14:48:39 +0100255 /* Only secondary VM vcpus can be run. */
256 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100257 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100258 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100259
Andrew Scull19503262018-09-20 14:48:39 +0100260 /* The requested VM must exist. */
261 vm = vm_get(vm_id);
262 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100263 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100264 }
265
266 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100267 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100268 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100269 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100270
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000271 /* Update state if allowed. */
Andrew Scullf3d45592018-09-20 14:30:22 +0100272 vcpu = &vm->vcpus[vcpu_idx];
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000273 if (!api_vcpu_prepare_run(current, vcpu, &vcpu_retval)) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100274 ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000275 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100276 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000277
Andrew Scull33fecd32019-01-08 14:48:27 +0000278 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000279 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000280
Andrew Scull33fecd32019-01-08 14:48:27 +0000281 /*
282 * Set a placeholder return code to the scheduler. This will be
283 * overwritten when the switch back to the primary occurs.
284 */
285 ret.code = HF_VCPU_RUN_PREEMPTED;
286
287 /* Update return value for the next vcpu if one was injected. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000288 if (vcpu_retval.force) {
289 arch_regs_set_retval(&vcpu->regs, vcpu_retval.value);
290 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100291
Andrew Scull6d2db332018-10-10 15:28:17 +0100292out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100293 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100294}
295
296/**
Andrew Scull81e85092018-12-12 12:56:20 +0000297 * Check that the mode indicates memory that is valid, owned and exclusive.
298 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000299static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000300{
301 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
302 0;
303}
304
305/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000306 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
307 * after they've succeeded. If a secondary VM is running and there are waiters,
308 * it also switches back to the primary VM for it to wake waiters up.
309 */
310static int64_t api_waiter_result(struct vm_locked locked_vm,
311 struct vcpu *current, struct vcpu **next)
312{
313 struct vm *vm = locked_vm.vm;
314 struct hf_vcpu_run_return ret = {
315 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
316 };
317
318 if (list_empty(&vm->mailbox.waiter_list)) {
319 /* No waiters, nothing else to do. */
320 return 0;
321 }
322
323 if (vm->id == HF_PRIMARY_VM_ID) {
324 /* The caller is the primary VM. Tell it to wake up waiters. */
325 return 1;
326 }
327
328 /*
329 * Switch back to the primary VM, informing it that there are waiters
330 * that need to be notified.
331 */
332 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
333
334 return 0;
335}
336
337/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100338 * Configures the VM to send/receive data through the specified pages. The pages
339 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000340 *
341 * Returns:
342 * - -1 on failure.
343 * - 0 on success if no further action is needed.
344 * - 1 if it was called by the primary VM and the primary VM now needs to wake
345 * up or kick waiters. Waiters should be retrieved by calling
346 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100347 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000348int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
349 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100350{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100351 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000352 struct vm_locked locked;
Andrew Scull80871322018-08-06 12:04:09 +0100353 paddr_t pa_send_begin;
354 paddr_t pa_send_end;
355 paddr_t pa_recv_begin;
356 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000357 int orig_send_mode;
358 int orig_recv_mode;
359 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100360 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100361
362 /* Fail if addresses are not page-aligned. */
Andrew Scull265ada92018-07-30 15:19:01 +0100363 if ((ipa_addr(send) & (PAGE_SIZE - 1)) ||
364 (ipa_addr(recv) & (PAGE_SIZE - 1))) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100365 return -1;
366 }
367
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000368 /* Convert to physical addresses. */
369 pa_send_begin = pa_from_ipa(send);
370 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
371
372 pa_recv_begin = pa_from_ipa(recv);
373 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
374
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100375 /* Fail if the same page is used for the send and receive pages. */
376 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000377 return -1;
378 }
379
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000380 vm_lock(vm, &locked);
Andrew Scull220e6212018-12-21 18:09:00 +0000381
382 /* We only allow these to be setup once. */
383 if (vm->mailbox.send || vm->mailbox.recv) {
384 goto fail;
385 }
386
387 /*
388 * Ensure the pages are valid, owned and exclusive to the VM and that
389 * the VM has the required access to the memory.
390 */
391 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
392 &orig_send_mode) ||
393 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
394 (orig_send_mode & MM_MODE_R) == 0 ||
395 (orig_send_mode & MM_MODE_W) == 0) {
396 goto fail;
397 }
398
399 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
400 &orig_recv_mode) ||
401 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
402 (orig_recv_mode & MM_MODE_R) == 0) {
403 goto fail;
404 }
405
406 /*
407 * Create a local pool so any freed memory can't be used by another
408 * thread. This is to ensure the original mapping can be restored if any
409 * stage of the process fails.
410 */
411 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
412
413 /* Take memory ownership away from the VM and mark as shared. */
414 if (!mm_vm_identity_map(
415 &vm->ptable, pa_send_begin, pa_send_end,
416 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
417 NULL, &local_page_pool)) {
418 goto fail_free_pool;
419 }
420
421 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
422 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
423 NULL, &local_page_pool)) {
424 /* TODO: partial defrag of failed range. */
425 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000426 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000427 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100428 }
429
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100430 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000431 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000432 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100433 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000434 /* TODO: partial defrag of failed range. */
435 /* Recover any memory consumed in failed mapping. */
436 mm_defrag(&local_page_pool);
437 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100438 }
439
440 /*
441 * Map the receive page as writable in the hypervisor address space. On
442 * failure, unmap the send page before returning.
443 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000444 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000445 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100446 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000447 /* TODO: partial defrag of failed range. */
448 /* Recover any memory consumed in failed mapping. */
449 mm_defrag(&local_page_pool);
450 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100451 }
452
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000453 /* Tell caller about waiters, if any. */
454 ret = api_waiter_result(locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000455 goto exit;
456
457 /*
458 * The following mappings will not require more memory than is available
459 * in the local pool.
460 */
461fail_undo_all:
462 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000463 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000464
465fail_undo_send_and_recv:
466 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
467 orig_recv_mode, NULL, &local_page_pool);
468
469fail_undo_send:
470 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
471 orig_send_mode, NULL, &local_page_pool);
472
473fail_free_pool:
474 mpool_fini(&local_page_pool);
475
476fail:
477 ret = -1;
478
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100479exit:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000480 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100481
482 return ret;
483}
484
485/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100486 * Copies data from the sender's send buffer to the recipient's receive buffer
487 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000488 *
489 * If the recipient's receive buffer is busy, it can optionally register the
490 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100491 */
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000492int64_t api_mailbox_send(uint32_t vm_id, size_t size, bool notify,
493 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100494{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100495 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100496 struct vm *to;
497 const void *from_buf;
Andrew Scullaa039b32018-10-04 15:02:26 +0100498 uint16_t vcpu;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100499 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100500
Andrew Scullaa039b32018-10-04 15:02:26 +0100501 /* Limit the size of transfer. */
502 if (size > HF_MAILBOX_SIZE) {
Andrew Scull19503262018-09-20 14:48:39 +0100503 return -1;
504 }
505
506 /* Disallow reflexive requests as this suggests an error in the VM. */
507 if (vm_id == from->id) {
508 return -1;
509 }
510
511 /* Ensure the target VM exists. */
512 to = vm_get(vm_id);
513 if (to == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100514 return -1;
515 }
516
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100517 /*
518 * Check that the sender has configured its send buffer. It is safe to
519 * use from_buf after releasing the lock because the buffer cannot be
520 * modified once it's configured.
521 */
522 sl_lock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100523 from_buf = from->mailbox.send;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100524 sl_unlock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100525 if (from_buf == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100526 return -1;
527 }
528
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100529 sl_lock(&to->lock);
530
Andrew Scullaa039b32018-10-04 15:02:26 +0100531 if (to->mailbox.state != mailbox_state_empty ||
532 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000533 /*
534 * Fail if the target isn't currently ready to receive data,
535 * setting up for notification if requested.
536 */
537 if (notify) {
538 struct wait_entry *entry = &current->vm->wentry[vm_id];
539
540 /* Append waiter only if it's not there yet. */
541 if (list_empty(&entry->wait_links)) {
542 list_append(&to->mailbox.waiter_list,
543 &entry->wait_links);
544 }
545 }
546
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100547 ret = -1;
Andrew Scullaa039b32018-10-04 15:02:26 +0100548 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100549 }
550
Andrew Scullaa039b32018-10-04 15:02:26 +0100551 /* Copy data. */
552 memcpy(to->mailbox.recv, from_buf, size);
553 to->mailbox.recv_bytes = size;
554 to->mailbox.recv_from_id = from->id;
555 to->mailbox.state = mailbox_state_read;
556
557 /* Messages for the primary VM are delivered directly. */
558 if (to->id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000559 struct hf_vcpu_run_return primary_ret = {
560 .code = HF_VCPU_RUN_MESSAGE,
561 .message.size = size,
562 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000563
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000564 *next = api_switch_to_primary(current, primary_ret,
565 vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100566 ret = 0;
567 goto out;
568 }
569
570 /*
571 * Try to find a vcpu to handle the message and tell the scheduler to
572 * run it.
573 */
574 if (to->mailbox.recv_waiter == NULL) {
575 /*
576 * The scheduler must choose a vcpu to interrupt so it can
577 * handle the message.
578 */
579 to->mailbox.state = mailbox_state_received;
580 vcpu = HF_INVALID_VCPU;
581 } else {
582 struct vcpu *to_vcpu = to->mailbox.recv_waiter;
583
584 /*
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000585 * Take target vcpu out of waiter list and mark it as ready to
586 * run again.
Andrew Scullaa039b32018-10-04 15:02:26 +0100587 */
588 sl_lock(&to_vcpu->lock);
589 to->mailbox.recv_waiter = to_vcpu->mailbox_next;
590 to_vcpu->state = vcpu_state_ready;
591
592 /* Return from HF_MAILBOX_RECEIVE. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000593 to_vcpu->retval.force = true;
594 to_vcpu->retval.value = hf_mailbox_receive_return_encode(
595 (struct hf_mailbox_receive_return){
596 .vm_id = to->mailbox.recv_from_id,
597 .size = size,
598 });
Andrew Scullaa039b32018-10-04 15:02:26 +0100599
600 sl_unlock(&to_vcpu->lock);
601
602 vcpu = to_vcpu - to->vcpus;
603 }
604
605 /* Return to the primary VM directly or with a switch. */
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000606 if (from->id == HF_PRIMARY_VM_ID) {
607 ret = vcpu;
608 } else {
609 struct hf_vcpu_run_return primary_ret = {
610 .code = HF_VCPU_RUN_WAKE_UP,
611 .wake_up.vm_id = to->id,
612 .wake_up.vcpu = vcpu,
613 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000614
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000615 *next = api_switch_to_primary(current, primary_ret,
616 vcpu_state_ready);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000617 ret = 0;
618 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100619
620out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100621 sl_unlock(&to->lock);
622
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000623 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100624}
625
626/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100627 * Receives a message from the mailbox. If one isn't available, this function
628 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100629 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100630 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100631 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100632struct hf_mailbox_receive_return api_mailbox_receive(bool block,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100633 struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100634 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100635{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100636 struct vm *vm = current->vm;
Andrew Scull6d2db332018-10-10 15:28:17 +0100637 struct hf_mailbox_receive_return ret = {
638 .vm_id = HF_INVALID_VM_ID,
639 };
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100640
Andrew Scullaa039b32018-10-04 15:02:26 +0100641 /*
642 * The primary VM will receive messages as a status code from running
643 * vcpus and must not call this function.
644 */
Andrew Scull19503262018-09-20 14:48:39 +0100645 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100646 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100647 }
648
649 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100650
Andrew Scullaa039b32018-10-04 15:02:26 +0100651 /* Return pending messages without blocking. */
652 if (vm->mailbox.state == mailbox_state_received) {
653 vm->mailbox.state = mailbox_state_read;
Andrew Scull6d2db332018-10-10 15:28:17 +0100654 ret.vm_id = vm->mailbox.recv_from_id;
655 ret.size = vm->mailbox.recv_bytes;
Andrew Scullaa039b32018-10-04 15:02:26 +0100656 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100657 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100658
659 /* No pending message so fail if not allowed to block. */
660 if (!block) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100661 goto out;
662 }
663
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100664 sl_lock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100665
666 /* Push vcpu into waiter list. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100667 current->mailbox_next = vm->mailbox.recv_waiter;
668 vm->mailbox.recv_waiter = current;
669 sl_unlock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100670
671 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000672 {
673 struct hf_vcpu_run_return run_return = {
674 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
675 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000676
Andrew Walbranb4816552018-12-05 17:35:42 +0000677 *next = api_switch_to_primary(current, run_return,
678 vcpu_state_blocked_mailbox);
679 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100680out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100681 sl_unlock(&vm->lock);
682
683 return ret;
684}
685
686/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000687 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
688 * by this function, the caller must have called api_mailbox_send before with
689 * the notify argument set to true, and this call must have failed because the
690 * mailbox was not available.
691 *
692 * It should be called repeatedly to retrieve a list of VMs.
693 *
694 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
695 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100696 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000697int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100698{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100699 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000700 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100701 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100702
703 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000704 if (list_empty(&vm->mailbox.ready_list)) {
705 ret = -1;
706 goto exit;
707 }
708
709 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
710 ready_links);
711 list_remove(&entry->ready_links);
712 ret = entry - vm->wentry;
713
714exit:
715 sl_unlock(&vm->lock);
716 return ret;
717}
718
719/**
720 * Retrieves the next VM waiting to be notified that the mailbox of the
721 * specified VM became writable. Only primary VMs are allowed to call this.
722 *
723 * Returns -1 if there are no waiters, or the VM id of the next waiter
724 * otherwise.
725 */
726int64_t api_mailbox_waiter_get(uint32_t vm_id, const struct vcpu *current)
727{
728 struct vm *vm;
729 struct vm_locked locked;
730 struct wait_entry *entry;
731 struct vm *waiting_vm;
732
733 /* Only primary VMs are allowed to call this function. */
734 if (current->vm->id != HF_PRIMARY_VM_ID) {
735 return -1;
736 }
737
738 vm = vm_get(vm_id);
739 if (vm == NULL) {
740 return -1;
741 }
742
743 /* Check if there are outstanding notifications from given vm. */
744 vm_lock(vm, &locked);
745 entry = api_fetch_waiter(locked);
746 vm_unlock(&locked);
747
748 if (entry == NULL) {
749 return -1;
750 }
751
752 /* Enqueue notification to waiting VM. */
753 waiting_vm = entry->waiting_vm;
754
755 sl_lock(&waiting_vm->lock);
756 if (list_empty(&entry->ready_links)) {
757 list_append(&waiting_vm->mailbox.ready_list,
758 &entry->ready_links);
759 }
760 sl_unlock(&waiting_vm->lock);
761
762 return waiting_vm->id;
763}
764
765/**
766 * Clears the caller's mailbox so that a new message can be received. The caller
767 * must have copied out all data they wish to preserve as new messages will
768 * overwrite the old and will arrive asynchronously.
769 *
770 * Returns:
771 * - -1 on failure, if the mailbox hasn't been read or is already empty.
772 * - 0 on success if no further action is needed.
773 * - 1 if it was called by the primary VM and the primary VM now needs to wake
774 * up or kick waiters. Waiters should be retrieved by calling
775 * hf_mailbox_waiter_get.
776 */
777int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
778{
779 struct vm *vm = current->vm;
780 struct vm_locked locked;
781 int64_t ret;
782
783 vm_lock(vm, &locked);
Andrew Scullaa039b32018-10-04 15:02:26 +0100784 if (vm->mailbox.state == mailbox_state_read) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000785 ret = api_waiter_result(locked, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +0100786 vm->mailbox.state = mailbox_state_empty;
787 } else {
788 ret = -1;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100789 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000790 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100791
792 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100793}
Andrew Walbran318f5732018-11-20 16:23:42 +0000794
795/**
796 * Enables or disables a given interrupt ID for the calling vCPU.
797 *
798 * Returns 0 on success, or -1 if the intid is invalid.
799 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000800int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000801{
802 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
803 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000804
Andrew Walbran318f5732018-11-20 16:23:42 +0000805 if (intid >= HF_NUM_INTIDS) {
806 return -1;
807 }
808
809 sl_lock(&current->lock);
810 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000811 /*
812 * If it is pending and was not enabled before, increment the
813 * count.
814 */
815 if (current->interrupts.interrupt_pending[intid_index] &
816 ~current->interrupts.interrupt_enabled[intid_index] &
817 intid_mask) {
818 current->interrupts.enabled_and_pending_count++;
819 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000820 current->interrupts.interrupt_enabled[intid_index] |=
821 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +0000822 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000823 /*
824 * If it is pending and was enabled before, decrement the count.
825 */
826 if (current->interrupts.interrupt_pending[intid_index] &
827 current->interrupts.interrupt_enabled[intid_index] &
828 intid_mask) {
829 current->interrupts.enabled_and_pending_count--;
830 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000831 current->interrupts.interrupt_enabled[intid_index] &=
832 ~intid_mask;
833 }
834
835 sl_unlock(&current->lock);
836 return 0;
837}
838
839/**
840 * Returns the ID of the next pending interrupt for the calling vCPU, and
841 * acknowledges it (i.e. marks it as no longer pending). Returns
842 * HF_INVALID_INTID if there are no pending interrupts.
843 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000844uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +0000845{
846 uint8_t i;
847 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +0000848
849 /*
850 * Find the first enabled and pending interrupt ID, return it, and
851 * deactivate it.
852 */
853 sl_lock(&current->lock);
854 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
855 uint32_t enabled_and_pending =
856 current->interrupts.interrupt_enabled[i] &
857 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000858
Andrew Walbran318f5732018-11-20 16:23:42 +0000859 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +0000860 uint8_t bit_index = ctz(enabled_and_pending);
861 /*
862 * Mark it as no longer pending and decrement the count.
863 */
864 current->interrupts.interrupt_pending[i] &=
865 ~(1u << bit_index);
866 current->interrupts.enabled_and_pending_count--;
867 first_interrupt =
868 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +0000869 break;
870 }
871 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000872
873 sl_unlock(&current->lock);
874 return first_interrupt;
875}
876
877/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +0000878 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +0000879 * given VM and vCPU.
880 */
881static inline bool is_injection_allowed(uint32_t target_vm_id,
882 struct vcpu *current)
883{
884 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000885
Andrew Walbran318f5732018-11-20 16:23:42 +0000886 /*
887 * The primary VM is allowed to inject interrupts into any VM. Secondary
888 * VMs are only allowed to inject interrupts into their own vCPUs.
889 */
890 return current_vm_id == HF_PRIMARY_VM_ID ||
891 current_vm_id == target_vm_id;
892}
893
894/**
895 * Injects a virtual interrupt of the given ID into the given target vCPU.
896 * This doesn't cause the vCPU to actually be run immediately; it will be taken
897 * when the vCPU is next run, which is up to the scheduler.
898 *
Andrew Walbran3d84a262018-12-13 14:41:19 +0000899 * Returns:
900 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
901 * ID is invalid, or the current VM is not allowed to inject interrupts to
902 * the target VM.
903 * - 0 on success if no further action is needed.
904 * - 1 if it was called by the primary VM and the primary VM now needs to wake
905 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +0000906 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000907int64_t api_interrupt_inject(uint32_t target_vm_id, uint32_t target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +0000908 uint32_t intid, struct vcpu *current,
909 struct vcpu **next)
910{
911 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
912 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
913 struct vcpu *target_vcpu;
914 struct vm *target_vm = vm_get(target_vm_id);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000915 bool need_vm_lock;
Andrew Walbran3d84a262018-12-13 14:41:19 +0000916 int64_t ret = 0;
Andrew Walbran318f5732018-11-20 16:23:42 +0000917
918 if (intid >= HF_NUM_INTIDS) {
919 return -1;
920 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000921
Andrew Walbran318f5732018-11-20 16:23:42 +0000922 if (target_vm == NULL) {
923 return -1;
924 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000925
Andrew Walbran318f5732018-11-20 16:23:42 +0000926 if (target_vcpu_idx >= target_vm->vcpu_count) {
927 /* The requested vcpu must exist. */
928 return -1;
929 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000930
Andrew Walbran318f5732018-11-20 16:23:42 +0000931 if (!is_injection_allowed(target_vm_id, current)) {
932 return -1;
933 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000934
Andrew Walbran318f5732018-11-20 16:23:42 +0000935 target_vcpu = &target_vm->vcpus[target_vcpu_idx];
936
937 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
938 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
939
940 sl_lock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000941 /*
942 * If we need the target_vm lock we need to release the target_vcpu lock
943 * first to maintain the correct order of locks. In-between releasing
944 * and acquiring it again the state of the vCPU could change in such a
945 * way that we don't actually need to touch the target_vm after all, but
946 * that's alright: we'll take the target_vm lock anyway, but it's safe,
947 * just perhaps a little slow in this unusual case. The reverse is not
948 * possible: if need_vm_lock is false, we don't release the target_vcpu
949 * lock until we are done, so nothing should change in such as way that
950 * we need the VM lock after all.
951 */
Andrew Walbran3d84a262018-12-13 14:41:19 +0000952 need_vm_lock =
953 (target_vcpu->interrupts.interrupt_enabled[intid_index] &
954 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
955 intid_mask) &&
956 target_vcpu->state == vcpu_state_blocked_mailbox;
Andrew Walbran69520dc2018-12-06 11:39:38 +0000957 if (need_vm_lock) {
958 sl_unlock(&target_vcpu->lock);
959 sl_lock(&target_vm->lock);
960 sl_lock(&target_vcpu->lock);
961 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000962
Andrew Walbran3d84a262018-12-13 14:41:19 +0000963 /*
964 * We only need to change state and (maybe) trigger a virtual IRQ if it
965 * is enabled and was not previously pending. Otherwise we can skip
966 * everything except setting the pending bit.
967 *
968 * If you change this logic make sure to update the need_vm_lock logic
969 * above to match.
970 */
971 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
972 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
973 intid_mask)) {
974 goto out;
975 }
976
977 /* Increment the count. */
978 target_vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran318f5732018-11-20 16:23:42 +0000979
Andrew Walbran69520dc2018-12-06 11:39:38 +0000980 /*
Andrew Walbran3d84a262018-12-13 14:41:19 +0000981 * Only need to update state if there was not already an
982 * interrupt enabled and pending.
Andrew Walbran69520dc2018-12-06 11:39:38 +0000983 */
Andrew Walbran3d84a262018-12-13 14:41:19 +0000984 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
985 goto out;
986 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000987
Andrew Walbran3d84a262018-12-13 14:41:19 +0000988 if (target_vcpu->state == vcpu_state_blocked_interrupt) {
989 target_vcpu->state = vcpu_state_ready;
990 } else if (target_vcpu->state == vcpu_state_blocked_mailbox) {
991 /*
992 * If you change this logic make sure to update the need_vm_lock
993 * logic above to match.
994 */
995 target_vcpu->state = vcpu_state_ready;
Andrew Walbran69520dc2018-12-06 11:39:38 +0000996
Andrew Walbran3d84a262018-12-13 14:41:19 +0000997 /* Take target vCPU out of mailbox recv_waiter list. */
998 /*
999 * TODO: Consider using a doubly-linked list for
1000 * the receive waiter list to avoid the linear
1001 * search here.
1002 */
1003 struct vcpu **previous_next_pointer =
1004 &target_vm->mailbox.recv_waiter;
1005 while (*previous_next_pointer != NULL &&
1006 *previous_next_pointer != target_vcpu) {
Andrew Walbran69520dc2018-12-06 11:39:38 +00001007 /*
Andrew Walbran3d84a262018-12-13 14:41:19 +00001008 * TODO(qwandor): Do we need to lock the vCPUs somehow
1009 * while we walk the linked list, or is the VM lock
1010 * enough?
Andrew Walbran69520dc2018-12-06 11:39:38 +00001011 */
Andrew Walbran3d84a262018-12-13 14:41:19 +00001012 previous_next_pointer =
1013 &(*previous_next_pointer)->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +00001014 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001015
Andrew Walbran3d84a262018-12-13 14:41:19 +00001016 if (*previous_next_pointer == NULL) {
1017 dlog("Target VCPU state is vcpu_state_blocked_mailbox "
1018 "but is not in VM mailbox waiter list. This "
1019 "should never happen.\n");
1020 } else {
1021 *previous_next_pointer = target_vcpu->mailbox_next;
Andrew Walbran318f5732018-11-20 16:23:42 +00001022 }
1023 }
1024
Andrew Walbran3d84a262018-12-13 14:41:19 +00001025 if (current->vm->id == HF_PRIMARY_VM_ID) {
1026 /*
1027 * If the call came from the primary VM, let it know that it
1028 * should run or kick the target vCPU.
1029 */
1030 ret = 1;
1031 } else if (current != target_vcpu) {
1032 /*
1033 * Switch to the primary so that it can switch to the target, or
1034 * kick it if it is already running on a different physical CPU.
1035 */
1036 struct hf_vcpu_run_return ret = {
1037 .code = HF_VCPU_RUN_WAKE_UP,
1038 .wake_up.vm_id = target_vm_id,
1039 .wake_up.vcpu = target_vcpu_idx,
1040 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001041
Andrew Walbran3d84a262018-12-13 14:41:19 +00001042 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
1043 }
1044
1045out:
1046 /* Either way, make it pending. */
1047 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
1048
Andrew Walbran318f5732018-11-20 16:23:42 +00001049 sl_unlock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +00001050 if (need_vm_lock) {
1051 sl_unlock(&target_vm->lock);
1052 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001053
Andrew Walbran3d84a262018-12-13 14:41:19 +00001054 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00001055}