blob: 438f6da798b5bd8ee5221adb76037ca7cb9defb4 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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 Walbran318f5732018-11-20 16:23:42 +000019#include "hf/arch/cpu.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000020#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000021
Andrew Scull5c496a32019-04-04 11:57:33 +010022#include "hf/assert.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000023#include "hf/dlog.h"
Andrew Scull6386f252018-12-06 13:29:10 +000024#include "hf/mm.h"
25#include "hf/spinlock.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010026#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010027#include "hf/vm.h"
28
Andrew Scullf35a5c92018-08-07 18:09:46 +010029#include "vmapi/hf/call.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000030#include "vmapi/hf/spci.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010031
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000032/*
33 * To eliminate the risk of deadlocks, we define a partial order for the
34 * acquisition of locks held concurrently by the same physical CPU. Our current
35 * ordering requirements are as follows:
36 *
37 * vm::lock -> vcpu::lock
Andrew Scull6386f252018-12-06 13:29:10 +000038 *
39 * Locks of the same kind require the lock of lowest address to be locked first,
40 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000041 */
42
Andrew Scullaa039b32018-10-04 15:02:26 +010043static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010044 "Currently, a page is mapped for the send and receive buffers so "
45 "the maximum request is the size of a page.");
46
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000047static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000048
49/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000050 * Initialises the API page pool by taking ownership of the contents of the
51 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000052 */
53void api_init(struct mpool *ppool)
54{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000055 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000056}
57
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010058/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010059 * Switches the physical CPU back to the corresponding vcpu of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010060 *
61 * This triggers the scheduling logic to run. Run in the context of secondary VM
62 * to cause HF_VCPU_RUN to return and the primary VM to regain control of the
63 * cpu.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010064 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010065static struct vcpu *api_switch_to_primary(struct vcpu *current,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000066 struct hf_vcpu_run_return primary_ret,
67 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010068{
Andrew Walbran42347a92019-05-09 13:59:03 +010069 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010070 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010071
Andrew Walbran508e63c2018-12-20 17:02:37 +000072 /*
73 * If the secondary is blocked but has a timer running, sleep until the
74 * timer fires rather than indefinitely.
75 */
Andrew Scullb06d1752019-02-04 10:15:48 +000076 switch (primary_ret.code) {
77 case HF_VCPU_RUN_WAIT_FOR_INTERRUPT:
78 case HF_VCPU_RUN_WAIT_FOR_MESSAGE:
79 primary_ret.sleep.ns =
80 arch_timer_enabled_current()
81 ? arch_timer_remaining_ns_current()
82 : HF_SLEEP_INDEFINITE;
83 break;
84
85 default:
86 /* Do nothing. */
87 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +000088 }
89
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010090 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Scull6d2db332018-10-10 15:28:17 +010091 arch_regs_set_retval(&next->regs,
92 hf_vcpu_run_return_encode(primary_ret));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010093
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000094 /* Mark the current vcpu as waiting. */
95 sl_lock(&current->lock);
96 current->state = secondary_state;
97 sl_unlock(&current->lock);
98
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010099 return next;
100}
101
102/**
Andrew Scull33fecd32019-01-08 14:48:27 +0000103 * Returns to the primary vm and signals that the vcpu still has work to do so.
104 */
105struct vcpu *api_preempt(struct vcpu *current)
106{
107 struct hf_vcpu_run_return ret = {
108 .code = HF_VCPU_RUN_PREEMPTED,
109 };
110
Andrew Sculld6ee1102019-04-05 22:12:42 +0100111 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000112}
113
114/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100115 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
116 * vm.
117 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100118struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100119{
Andrew Scull6d2db332018-10-10 15:28:17 +0100120 struct hf_vcpu_run_return ret = {
121 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
122 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000123
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000124 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100125 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100126}
127
128/**
Andrew Walbran33645652019-04-15 12:29:31 +0100129 * Puts the current vCPU in off mode, and returns to the primary VM.
130 */
131struct vcpu *api_vcpu_off(struct vcpu *current)
132{
133 struct hf_vcpu_run_return ret = {
134 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
135 };
136
137 /*
138 * Disable the timer, so the scheduler doesn't get told to call back
139 * based on it.
140 */
141 arch_timer_disable_current();
142
143 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
144}
145
146/**
Andrew Scull66d62bf2019-02-01 13:54:10 +0000147 * Returns to the primary vm to allow this cpu to be used for other tasks as the
148 * vcpu does not have work to do at this moment. The current vcpu is marked as
Jose Marinho135dff32019-02-28 10:25:57 +0000149 * ready to be scheduled again. This SPCI function always returns SPCI_SUCCESS.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000150 */
Jose Marinho135dff32019-02-28 10:25:57 +0000151int32_t api_spci_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000152{
153 struct hf_vcpu_run_return ret = {
154 .code = HF_VCPU_RUN_YIELD,
155 };
156
157 if (current->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000158 /* Noop on the primary as it makes the scheduling decisions. */
Jose Marinho135dff32019-02-28 10:25:57 +0000159 return SPCI_SUCCESS;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000160 }
161
Jose Marinho135dff32019-02-28 10:25:57 +0000162 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
163
164 /* SPCI_YIELD always returns SPCI_SUCCESS. */
165 return SPCI_SUCCESS;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000166}
167
168/**
Andrew Walbran33645652019-04-15 12:29:31 +0100169 * Switches to the primary so that it can switch to the target, or kick it if it
170 * is already running on a different physical CPU.
171 */
172struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
173{
174 struct hf_vcpu_run_return ret = {
175 .code = HF_VCPU_RUN_WAKE_UP,
176 .wake_up.vm_id = target_vcpu->vm->id,
177 .wake_up.vcpu = vcpu_index(target_vcpu),
178 };
179 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
180}
181
182/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000183 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000184 */
185struct vcpu *api_abort(struct vcpu *current)
186{
187 struct hf_vcpu_run_return ret = {
188 .code = HF_VCPU_RUN_ABORTED,
189 };
190
191 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
192
193 if (current->vm->id == HF_PRIMARY_VM_ID) {
194 /* TODO: what to do when the primary aborts? */
195 for (;;) {
196 /* Do nothing. */
197 }
198 }
199
200 atomic_store_explicit(&current->vm->aborting, true,
201 memory_order_relaxed);
202
203 /* TODO: free resources once all vCPUs abort. */
204
Andrew Sculld6ee1102019-04-05 22:12:42 +0100205 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000206}
207
208/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000209 * Returns the ID of the VM.
210 */
211int64_t api_vm_get_id(const struct vcpu *current)
212{
213 return current->vm->id;
214}
215
216/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100217 * Returns the number of VMs configured to run.
218 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100219int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100220{
Andrew Scull19503262018-09-20 14:48:39 +0100221 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100222}
223
224/**
225 * Returns the number of vcpus configured in the given VM.
226 */
Andrew Walbran42347a92019-05-09 13:59:03 +0100227int64_t api_vcpu_get_count(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100228{
Andrew Scull19503262018-09-20 14:48:39 +0100229 struct vm *vm;
230
231 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100232 if (current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100233 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100234 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100235
Andrew Walbran42347a92019-05-09 13:59:03 +0100236 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100237 if (vm == NULL) {
238 return -1;
239 }
240
241 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100242}
243
244/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000245 * This function is called by the architecture-specific context switching
246 * function to indicate that register state for the given vcpu has been saved
247 * and can therefore be used by other pcpus.
248 */
249void api_regs_state_saved(struct vcpu *vcpu)
250{
251 sl_lock(&vcpu->lock);
252 vcpu->regs_available = true;
253 sl_unlock(&vcpu->lock);
254}
255
256/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000257 * Retrieves the next waiter and removes it from the wait list if the VM's
258 * mailbox is in a writable state.
259 */
260static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
261{
262 struct wait_entry *entry;
263 struct vm *vm = locked_vm.vm;
264
Andrew Sculld6ee1102019-04-05 22:12:42 +0100265 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000266 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
267 /* The mailbox is not writable or there are no waiters. */
268 return NULL;
269 }
270
271 /* Remove waiter from the wait list. */
272 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
273 wait_links);
274 list_remove(&entry->wait_links);
275 return entry;
276}
277
278/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000279 * Assuming that the arguments have already been checked by the caller, injects
280 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
281 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
282 * is next run, which is up to the scheduler.
283 *
284 * Returns:
285 * - 0 on success if no further action is needed.
286 * - 1 if it was called by the primary VM and the primary VM now needs to wake
287 * up or kick the target vCPU.
288 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100289static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000290 uint32_t intid, struct vcpu *current,
291 struct vcpu **next)
292{
293 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
294 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000295 int64_t ret = 0;
296
297 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000298
299 /*
300 * We only need to change state and (maybe) trigger a virtual IRQ if it
301 * is enabled and was not previously pending. Otherwise we can skip
302 * everything except setting the pending bit.
303 *
304 * If you change this logic make sure to update the need_vm_lock logic
305 * above to match.
306 */
307 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
308 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
309 intid_mask)) {
310 goto out;
311 }
312
313 /* Increment the count. */
314 target_vcpu->interrupts.enabled_and_pending_count++;
315
316 /*
317 * Only need to update state if there was not already an
318 * interrupt enabled and pending.
319 */
320 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
321 goto out;
322 }
323
Andrew Walbran508e63c2018-12-20 17:02:37 +0000324 if (current->vm->id == HF_PRIMARY_VM_ID) {
325 /*
326 * If the call came from the primary VM, let it know that it
327 * should run or kick the target vCPU.
328 */
329 ret = 1;
330 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100331 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000332 }
333
334out:
335 /* Either way, make it pending. */
336 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
337
338 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000339
340 return ret;
341}
342
343/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000344 * Prepares the vcpu to run by updating its state and fetching whether a return
345 * value needs to be forced onto the vCPU.
346 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000347static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000348 struct hf_vcpu_run_return *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000349{
Andrew Scullb06d1752019-02-04 10:15:48 +0000350 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000351 bool ret;
352
Andrew Scullb06d1752019-02-04 10:15:48 +0000353 /*
354 * Wait until the registers become available. All locks must be
355 * released between iterations of this loop to avoid potential deadlocks
356 * if, on any path, a lock needs to be taken after taking the decision
357 * to switch context but before the registers have been saved.
358 *
359 * The VM lock is not needed in the common case so it must only be taken
360 * when it is going to be needed. This ensures there are no inter-vCPU
361 * dependencies in the common run case meaning the sensitive context
362 * switch performance is consistent.
363 */
364 for (;;) {
365 sl_lock(&vcpu->lock);
366
367 /* The VM needs to be locked to deliver mailbox messages. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100368 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
Andrew Scullb06d1752019-02-04 10:15:48 +0000369 if (need_vm_lock) {
370 sl_unlock(&vcpu->lock);
371 sl_lock(&vcpu->vm->lock);
372 sl_lock(&vcpu->lock);
373 }
374
375 if (vcpu->regs_available) {
376 break;
377 }
378
Andrew Sculld6ee1102019-04-05 22:12:42 +0100379 if (vcpu->state == VCPU_STATE_RUNNING) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000380 /*
381 * vCPU is running on another pCPU.
382 *
383 * It's ok to not return the sleep duration here because
384 * the other physical CPU that is currently running this
385 * vCPU will return sleep duration if neeed. The default
386 * return value is HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no
387 * need to set it explicitly.
388 */
389 ret = false;
390 goto out;
391 }
392
393 sl_unlock(&vcpu->lock);
394 if (need_vm_lock) {
395 sl_unlock(&vcpu->vm->lock);
396 }
397 }
Andrew Scull9726c252019-01-23 13:44:19 +0000398
399 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100400 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Scull82331282019-01-25 10:29:34 +0000401 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
402 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100403 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000404 }
405 ret = false;
406 goto out;
407 }
408
Andrew Walbran508e63c2018-12-20 17:02:37 +0000409 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100410 case VCPU_STATE_RUNNING:
411 case VCPU_STATE_OFF:
412 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000413 ret = false;
414 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000415
Andrew Sculld6ee1102019-04-05 22:12:42 +0100416 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000417 /*
418 * A pending message allows the vCPU to run so the message can
419 * be delivered directly.
420 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100421 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000422 arch_regs_set_retval(&vcpu->regs, SPCI_SUCCESS);
Andrew Sculld6ee1102019-04-05 22:12:42 +0100423 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000424 break;
425 }
426 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100427 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000428 /* Allow virtual interrupts to be delivered. */
429 if (vcpu->interrupts.enabled_and_pending_count > 0) {
430 break;
431 }
432
433 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000434 if (arch_timer_pending(&vcpu->regs)) {
435 break;
436 }
437
438 /*
439 * The vCPU is not ready to run, return the appropriate code to
440 * the primary which called vcpu_run.
441 */
442 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000443 run_ret->code =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100444 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Scullb06d1752019-02-04 10:15:48 +0000445 ? HF_VCPU_RUN_WAIT_FOR_MESSAGE
446 : HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000447 run_ret->sleep.ns =
448 arch_timer_remaining_ns(&vcpu->regs);
449 }
450
451 ret = false;
452 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000453
Andrew Sculld6ee1102019-04-05 22:12:42 +0100454 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000455 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000456 }
457
Andrew Scullb06d1752019-02-04 10:15:48 +0000458 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000459 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100460 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000461
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000462 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000463 * Mark the registers as unavailable now that we're about to reflect
464 * them onto the real registers. This will also prevent another physical
465 * CPU from trying to read these registers.
466 */
467 vcpu->regs_available = false;
468
469 ret = true;
470
471out:
472 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000473 if (need_vm_lock) {
474 sl_unlock(&vcpu->vm->lock);
475 }
476
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000477 return ret;
478}
479
480/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100481 * Runs the given vcpu of the given vm.
482 */
Andrew Walbran42347a92019-05-09 13:59:03 +0100483struct hf_vcpu_run_return api_vcpu_run(spci_vm_id_t vm_id, uint32_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000484 const struct vcpu *current,
485 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100486{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100487 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100488 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100489 struct hf_vcpu_run_return ret = {
490 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
Andrew Scullb06d1752019-02-04 10:15:48 +0000491 .sleep.ns = HF_SLEEP_INDEFINITE,
Andrew Scull6d2db332018-10-10 15:28:17 +0100492 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100493
494 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100495 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100496 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100497 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100498
Andrew Scull19503262018-09-20 14:48:39 +0100499 /* Only secondary VM vcpus can be run. */
500 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100501 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100502 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100503
Andrew Scull19503262018-09-20 14:48:39 +0100504 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100505 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100506 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100507 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100508 }
509
510 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100511 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100512 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100513 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100514
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000515 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100516 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000517 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000518 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100519 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000520
Andrew Walbran508e63c2018-12-20 17:02:37 +0000521 /*
522 * Inject timer interrupt if timer has expired. It's safe to access
523 * vcpu->regs here because api_vcpu_prepare_run already made sure that
524 * regs_available was true (and then set it to false) before returning
525 * true.
526 */
527 if (arch_timer_pending(&vcpu->regs)) {
528 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100529 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
530 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000531
532 /*
533 * Set the mask bit so the hardware interrupt doesn't fire
534 * again. Ideally we wouldn't do this because it affects what
535 * the secondary vCPU sees, but if we don't then we end up with
536 * a loop of the interrupt firing each time we try to return to
537 * the secondary vCPU.
538 */
539 arch_timer_mask(&vcpu->regs);
540 }
541
Andrew Scull33fecd32019-01-08 14:48:27 +0000542 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000543 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000544
Andrew Scull33fecd32019-01-08 14:48:27 +0000545 /*
546 * Set a placeholder return code to the scheduler. This will be
547 * overwritten when the switch back to the primary occurs.
548 */
549 ret.code = HF_VCPU_RUN_PREEMPTED;
550
Andrew Scull6d2db332018-10-10 15:28:17 +0100551out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100552 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100553}
554
555/**
Andrew Scull81e85092018-12-12 12:56:20 +0000556 * Check that the mode indicates memory that is valid, owned and exclusive.
557 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000558static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000559{
560 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
561 0;
562}
563
564/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000565 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
566 * after they've succeeded. If a secondary VM is running and there are waiters,
567 * it also switches back to the primary VM for it to wake waiters up.
568 */
569static int64_t api_waiter_result(struct vm_locked locked_vm,
570 struct vcpu *current, struct vcpu **next)
571{
572 struct vm *vm = locked_vm.vm;
573 struct hf_vcpu_run_return ret = {
574 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
575 };
576
577 if (list_empty(&vm->mailbox.waiter_list)) {
578 /* No waiters, nothing else to do. */
579 return 0;
580 }
581
582 if (vm->id == HF_PRIMARY_VM_ID) {
583 /* The caller is the primary VM. Tell it to wake up waiters. */
584 return 1;
585 }
586
587 /*
588 * Switch back to the primary VM, informing it that there are waiters
589 * that need to be notified.
590 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100591 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000592
593 return 0;
594}
595
596/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100597 * Configures the VM to send/receive data through the specified pages. The pages
598 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000599 *
600 * Returns:
601 * - -1 on failure.
602 * - 0 on success if no further action is needed.
603 * - 1 if it was called by the primary VM and the primary VM now needs to wake
604 * up or kick waiters. Waiters should be retrieved by calling
605 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100606 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000607int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
608 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100609{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100610 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000611 struct vm_locked locked;
Andrew Scull80871322018-08-06 12:04:09 +0100612 paddr_t pa_send_begin;
613 paddr_t pa_send_end;
614 paddr_t pa_recv_begin;
615 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000616 int orig_send_mode;
617 int orig_recv_mode;
618 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100619 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100620
621 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000622 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
623 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100624 return -1;
625 }
626
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000627 /* Convert to physical addresses. */
628 pa_send_begin = pa_from_ipa(send);
629 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
630
631 pa_recv_begin = pa_from_ipa(recv);
632 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
633
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100634 /* Fail if the same page is used for the send and receive pages. */
635 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000636 return -1;
637 }
638
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100639 locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000640
641 /* We only allow these to be setup once. */
642 if (vm->mailbox.send || vm->mailbox.recv) {
643 goto fail;
644 }
645
646 /*
647 * Ensure the pages are valid, owned and exclusive to the VM and that
648 * the VM has the required access to the memory.
649 */
650 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
651 &orig_send_mode) ||
652 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
653 (orig_send_mode & MM_MODE_R) == 0 ||
654 (orig_send_mode & MM_MODE_W) == 0) {
655 goto fail;
656 }
657
658 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
659 &orig_recv_mode) ||
660 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
661 (orig_recv_mode & MM_MODE_R) == 0) {
662 goto fail;
663 }
664
665 /*
666 * Create a local pool so any freed memory can't be used by another
667 * thread. This is to ensure the original mapping can be restored if any
668 * stage of the process fails.
669 */
670 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
671
672 /* Take memory ownership away from the VM and mark as shared. */
673 if (!mm_vm_identity_map(
674 &vm->ptable, pa_send_begin, pa_send_end,
675 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
676 NULL, &local_page_pool)) {
677 goto fail_free_pool;
678 }
679
680 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
681 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
682 NULL, &local_page_pool)) {
683 /* TODO: partial defrag of failed range. */
684 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000685 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000686 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100687 }
688
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100689 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000690 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000691 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100692 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000693 /* TODO: partial defrag of failed range. */
694 /* Recover any memory consumed in failed mapping. */
695 mm_defrag(&local_page_pool);
696 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100697 }
698
699 /*
700 * Map the receive page as writable in the hypervisor address space. On
701 * failure, unmap the send page before returning.
702 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000703 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000704 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100705 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000706 /* TODO: partial defrag of failed range. */
707 /* Recover any memory consumed in failed mapping. */
708 mm_defrag(&local_page_pool);
709 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100710 }
711
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000712 /* Tell caller about waiters, if any. */
713 ret = api_waiter_result(locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000714 goto exit;
715
716 /*
717 * The following mappings will not require more memory than is available
718 * in the local pool.
719 */
720fail_undo_all:
721 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000722 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000723
724fail_undo_send_and_recv:
725 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
726 orig_recv_mode, NULL, &local_page_pool);
727
728fail_undo_send:
729 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
730 orig_send_mode, NULL, &local_page_pool);
731
732fail_free_pool:
733 mpool_fini(&local_page_pool);
734
735fail:
736 ret = -1;
737
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100738exit:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000739 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100740
741 return ret;
742}
743
744/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100745 * Copies data from the sender's send buffer to the recipient's receive buffer
746 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000747 *
748 * If the recipient's receive buffer is busy, it can optionally register the
749 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100750 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000751int32_t api_spci_msg_send(uint32_t attributes, struct vcpu *current,
752 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100753{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100754 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100755 struct vm *to;
Andrew Scullb06d1752019-02-04 10:15:48 +0000756 struct hf_vcpu_run_return primary_ret = {
757 .code = HF_VCPU_RUN_MESSAGE,
758 };
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000759 struct spci_message from_msg_replica;
760 struct spci_message *to_msg;
761 const struct spci_message *from_msg;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100762
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000763 uint32_t size;
Andrew Scull19503262018-09-20 14:48:39 +0100764
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000765 int64_t ret;
766 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
767 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100768
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000769 /*
770 * Check that the sender has configured its send buffer. Copy the
771 * message header. If the tx mailbox at from_msg is configured (i.e.
772 * from_msg != NULL) then it can be safely accessed after releasing the
773 * lock since the tx mailbox address can only be configured once.
774 */
775 sl_lock(&from->lock);
776 from_msg = from->mailbox.send;
777 sl_unlock(&from->lock);
778
779 if (from_msg == NULL) {
780 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100781 }
782
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100783 /*
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000784 * Note that the payload is not copied when the message header is.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100785 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000786 from_msg_replica = *from_msg;
787
788 /* Ensure source VM id corresponds to the current VM. */
789 if (from_msg_replica.source_vm_id != from->id) {
790 return SPCI_INVALID_PARAMETERS;
791 }
792
793 size = from_msg_replica.length;
794 /* Limit the size of transfer. */
Andrew Scull1262ac22019-04-05 12:44:26 +0100795 if (size > SPCI_MSG_PAYLOAD_MAX) {
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000796 return SPCI_INVALID_PARAMETERS;
797 }
798
799 /* Disallow reflexive requests as this suggests an error in the VM. */
800 if (from_msg_replica.target_vm_id == from->id) {
801 return SPCI_INVALID_PARAMETERS;
802 }
803
804 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100805 to = vm_find(from_msg_replica.target_vm_id);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000806 if (to == NULL) {
807 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100808 }
809
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100810 sl_lock(&to->lock);
811
Andrew Sculld6ee1102019-04-05 22:12:42 +0100812 if (to->mailbox.state != MAILBOX_STATE_EMPTY ||
Andrew Scullaa039b32018-10-04 15:02:26 +0100813 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000814 /*
815 * Fail if the target isn't currently ready to receive data,
816 * setting up for notification if requested.
817 */
818 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000819 struct wait_entry *entry =
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000820 &current->vm->wait_entries
821 [from_msg_replica.target_vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000822
823 /* Append waiter only if it's not there yet. */
824 if (list_empty(&entry->wait_links)) {
825 list_append(&to->mailbox.waiter_list,
826 &entry->wait_links);
827 }
828 }
829
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000830 ret = SPCI_BUSY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100831 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100832 }
833
Andrew Scullaa039b32018-10-04 15:02:26 +0100834 /* Copy data. */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000835 to_msg = to->mailbox.recv;
836 *to_msg = from_msg_replica;
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100837 memcpy_s(to_msg->payload, SPCI_MSG_PAYLOAD_MAX,
838 from->mailbox.send->payload, size);
Andrew Scullb06d1752019-02-04 10:15:48 +0000839 primary_ret.message.vm_id = to->id;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000840 ret = SPCI_SUCCESS;
Andrew Scullaa039b32018-10-04 15:02:26 +0100841
842 /* Messages for the primary VM are delivered directly. */
843 if (to->id == HF_PRIMARY_VM_ID) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100844 to->mailbox.state = MAILBOX_STATE_READ;
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000845 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100846 VCPU_STATE_READY);
Andrew Scullaa039b32018-10-04 15:02:26 +0100847 goto out;
848 }
849
Andrew Sculld6ee1102019-04-05 22:12:42 +0100850 to->mailbox.state = MAILBOX_STATE_RECEIVED;
Andrew Scullaa039b32018-10-04 15:02:26 +0100851
852 /* Return to the primary VM directly or with a switch. */
Andrew Scullb06d1752019-02-04 10:15:48 +0000853 if (from->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000854 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100855 VCPU_STATE_READY);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000856 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100857
858out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100859 sl_unlock(&to->lock);
860
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000861 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100862}
863
864/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100865 * Receives a message from the mailbox. If one isn't available, this function
866 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100867 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100868 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100869 */
Jose Marinho3e2442f2019-03-12 13:30:37 +0000870int32_t api_spci_msg_recv(uint32_t attributes, struct vcpu *current,
871 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100872{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100873 struct vm *vm = current->vm;
Jose Marinho3e2442f2019-03-12 13:30:37 +0000874 int32_t return_code;
875 bool block =
876 (attributes & SPCI_MSG_RECV_BLOCK_MASK) == SPCI_MSG_RECV_BLOCK;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100877
Andrew Scullaa039b32018-10-04 15:02:26 +0100878 /*
879 * The primary VM will receive messages as a status code from running
880 * vcpus and must not call this function.
881 */
Andrew Scull19503262018-09-20 14:48:39 +0100882 if (vm->id == HF_PRIMARY_VM_ID) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000883 return SPCI_INTERRUPTED;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100884 }
885
886 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100887
Andrew Scullaa039b32018-10-04 15:02:26 +0100888 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100889 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
890 vm->mailbox.state = MAILBOX_STATE_READ;
Jose Marinho3e2442f2019-03-12 13:30:37 +0000891 return_code = SPCI_SUCCESS;
892 goto out;
893 }
894
895 /* No pending message so fail if not allowed to block. */
896 if (!block) {
897 return_code = SPCI_RETRY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100898 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100899 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100900
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000901 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +0000902 * From this point onward this call can only be interrupted or a message
903 * received. If a message is received the return value will be set at
904 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000905 */
Jose Marinho3e2442f2019-03-12 13:30:37 +0000906 return_code = SPCI_INTERRUPTED;
907
908 /*
909 * Don't block if there are enabled and pending interrupts, to match
910 * behaviour of wait_for_interrupt.
911 */
912 if (current->interrupts.enabled_and_pending_count > 0) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100913 goto out;
914 }
915
Andrew Scullaa039b32018-10-04 15:02:26 +0100916 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000917 {
918 struct hf_vcpu_run_return run_return = {
Andrew Scullb06d1752019-02-04 10:15:48 +0000919 .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE,
Andrew Walbranb4816552018-12-05 17:35:42 +0000920 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000921
Andrew Walbranb4816552018-12-05 17:35:42 +0000922 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100923 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +0000924 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100925out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100926 sl_unlock(&vm->lock);
927
Jose Marinho3e2442f2019-03-12 13:30:37 +0000928 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100929}
930
931/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000932 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
933 * by this function, the caller must have called api_mailbox_send before with
934 * the notify argument set to true, and this call must have failed because the
935 * mailbox was not available.
936 *
937 * It should be called repeatedly to retrieve a list of VMs.
938 *
939 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
940 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100941 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000942int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100943{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100944 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000945 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100946 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100947
948 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000949 if (list_empty(&vm->mailbox.ready_list)) {
950 ret = -1;
951 goto exit;
952 }
953
954 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
955 ready_links);
956 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000957 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000958
959exit:
960 sl_unlock(&vm->lock);
961 return ret;
962}
963
964/**
965 * Retrieves the next VM waiting to be notified that the mailbox of the
966 * specified VM became writable. Only primary VMs are allowed to call this.
967 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000968 * Returns -1 on failure or if there are no waiters; the VM id of the next
969 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000970 */
Andrew Walbran42347a92019-05-09 13:59:03 +0100971int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000972{
973 struct vm *vm;
974 struct vm_locked locked;
975 struct wait_entry *entry;
976 struct vm *waiting_vm;
977
978 /* Only primary VMs are allowed to call this function. */
979 if (current->vm->id != HF_PRIMARY_VM_ID) {
980 return -1;
981 }
982
Andrew Walbran42347a92019-05-09 13:59:03 +0100983 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000984 if (vm == NULL) {
985 return -1;
986 }
987
988 /* Check if there are outstanding notifications from given vm. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100989 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000990 entry = api_fetch_waiter(locked);
991 vm_unlock(&locked);
992
993 if (entry == NULL) {
994 return -1;
995 }
996
997 /* Enqueue notification to waiting VM. */
998 waiting_vm = entry->waiting_vm;
999
1000 sl_lock(&waiting_vm->lock);
1001 if (list_empty(&entry->ready_links)) {
1002 list_append(&waiting_vm->mailbox.ready_list,
1003 &entry->ready_links);
1004 }
1005 sl_unlock(&waiting_vm->lock);
1006
1007 return waiting_vm->id;
1008}
1009
1010/**
1011 * Clears the caller's mailbox so that a new message can be received. The caller
1012 * must have copied out all data they wish to preserve as new messages will
1013 * overwrite the old and will arrive asynchronously.
1014 *
1015 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001016 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001017 * - 0 on success if no further action is needed.
1018 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1019 * up or kick waiters. Waiters should be retrieved by calling
1020 * hf_mailbox_waiter_get.
1021 */
1022int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
1023{
1024 struct vm *vm = current->vm;
1025 struct vm_locked locked;
1026 int64_t ret;
1027
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001028 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001029 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001030 case MAILBOX_STATE_EMPTY:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001031 ret = 0;
1032 break;
1033
Andrew Sculld6ee1102019-04-05 22:12:42 +01001034 case MAILBOX_STATE_RECEIVED:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001035 ret = -1;
1036 break;
1037
Andrew Sculld6ee1102019-04-05 22:12:42 +01001038 case MAILBOX_STATE_READ:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001039 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001040 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001041 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001042 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001043 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001044
1045 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001046}
Andrew Walbran318f5732018-11-20 16:23:42 +00001047
1048/**
1049 * Enables or disables a given interrupt ID for the calling vCPU.
1050 *
1051 * Returns 0 on success, or -1 if the intid is invalid.
1052 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001053int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001054{
1055 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
1056 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001057
Andrew Walbran318f5732018-11-20 16:23:42 +00001058 if (intid >= HF_NUM_INTIDS) {
1059 return -1;
1060 }
1061
1062 sl_lock(&current->lock);
1063 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001064 /*
1065 * If it is pending and was not enabled before, increment the
1066 * count.
1067 */
1068 if (current->interrupts.interrupt_pending[intid_index] &
1069 ~current->interrupts.interrupt_enabled[intid_index] &
1070 intid_mask) {
1071 current->interrupts.enabled_and_pending_count++;
1072 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001073 current->interrupts.interrupt_enabled[intid_index] |=
1074 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001075 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001076 /*
1077 * If it is pending and was enabled before, decrement the count.
1078 */
1079 if (current->interrupts.interrupt_pending[intid_index] &
1080 current->interrupts.interrupt_enabled[intid_index] &
1081 intid_mask) {
1082 current->interrupts.enabled_and_pending_count--;
1083 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001084 current->interrupts.interrupt_enabled[intid_index] &=
1085 ~intid_mask;
1086 }
1087
1088 sl_unlock(&current->lock);
1089 return 0;
1090}
1091
1092/**
1093 * Returns the ID of the next pending interrupt for the calling vCPU, and
1094 * acknowledges it (i.e. marks it as no longer pending). Returns
1095 * HF_INVALID_INTID if there are no pending interrupts.
1096 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001097uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001098{
1099 uint8_t i;
1100 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001101
1102 /*
1103 * Find the first enabled and pending interrupt ID, return it, and
1104 * deactivate it.
1105 */
1106 sl_lock(&current->lock);
1107 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1108 uint32_t enabled_and_pending =
1109 current->interrupts.interrupt_enabled[i] &
1110 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001111
Andrew Walbran318f5732018-11-20 16:23:42 +00001112 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001113 uint8_t bit_index = ctz(enabled_and_pending);
1114 /*
1115 * Mark it as no longer pending and decrement the count.
1116 */
1117 current->interrupts.interrupt_pending[i] &=
1118 ~(1u << bit_index);
1119 current->interrupts.enabled_and_pending_count--;
1120 first_interrupt =
1121 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001122 break;
1123 }
1124 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001125
1126 sl_unlock(&current->lock);
1127 return first_interrupt;
1128}
1129
1130/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001131 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001132 * given VM and vCPU.
1133 */
1134static inline bool is_injection_allowed(uint32_t target_vm_id,
1135 struct vcpu *current)
1136{
1137 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001138
Andrew Walbran318f5732018-11-20 16:23:42 +00001139 /*
1140 * The primary VM is allowed to inject interrupts into any VM. Secondary
1141 * VMs are only allowed to inject interrupts into their own vCPUs.
1142 */
1143 return current_vm_id == HF_PRIMARY_VM_ID ||
1144 current_vm_id == target_vm_id;
1145}
1146
1147/**
1148 * Injects a virtual interrupt of the given ID into the given target vCPU.
1149 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1150 * when the vCPU is next run, which is up to the scheduler.
1151 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001152 * Returns:
1153 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1154 * ID is invalid, or the current VM is not allowed to inject interrupts to
1155 * the target VM.
1156 * - 0 on success if no further action is needed.
1157 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1158 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001159 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001160int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
1161 uint32_t target_vcpu_idx, uint32_t intid,
1162 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001163{
Andrew Walbran318f5732018-11-20 16:23:42 +00001164 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001165 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001166
1167 if (intid >= HF_NUM_INTIDS) {
1168 return -1;
1169 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001170
Andrew Walbran318f5732018-11-20 16:23:42 +00001171 if (target_vm == NULL) {
1172 return -1;
1173 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001174
Andrew Walbran318f5732018-11-20 16:23:42 +00001175 if (target_vcpu_idx >= target_vm->vcpu_count) {
1176 /* The requested vcpu must exist. */
1177 return -1;
1178 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001179
Andrew Walbran318f5732018-11-20 16:23:42 +00001180 if (!is_injection_allowed(target_vm_id, current)) {
1181 return -1;
1182 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001183
Andrew Walbrane1310df2019-04-29 17:28:28 +01001184 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001185
1186 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1187 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001188 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001189}
Andrew Scull6386f252018-12-06 13:29:10 +00001190
1191/**
1192 * Clears a region of physical memory by overwriting it with zeros. The data is
1193 * flushed from the cache so the memory has been cleared across the system.
1194 */
1195static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1196{
1197 /*
1198 * TODO: change this to a cpu local single page window rather than a
1199 * global mapping of the whole range. Such an approach will limit
1200 * the changes to stage-1 tables and will allow only local
1201 * invalidation.
1202 */
1203 void *ptr = mm_identity_map(begin, end, MM_MODE_W, ppool);
Andrew Walbran2cb43392019-04-17 12:52:45 +01001204 size_t size = pa_difference(begin, end);
Andrew Scull6386f252018-12-06 13:29:10 +00001205
1206 if (!ptr) {
1207 /* TODO: partial defrag of failed range. */
1208 /* Recover any memory consumed in failed mapping. */
1209 mm_defrag(ppool);
1210 return false;
1211 }
1212
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001213 memset_s(ptr, size, 0, size);
Andrew Scull6386f252018-12-06 13:29:10 +00001214 arch_mm_write_back_dcache(ptr, size);
1215 mm_unmap(begin, end, ppool);
1216
1217 return true;
1218}
1219
1220/**
1221 * Shares memory from the calling VM with another. The memory can be shared in
1222 * different modes.
1223 *
1224 * TODO: the interface for sharing memory will need to be enhanced to allow
1225 * sharing with different modes e.g. read-only, informing the recipient
1226 * of the memory they have been given, opting to not wipe the memory and
1227 * possibly allowing multiple blocks to be transferred. What this will
1228 * look like is TBD.
1229 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001230int64_t api_share_memory(spci_vm_id_t vm_id, ipaddr_t addr, size_t size,
Andrew Scull6386f252018-12-06 13:29:10 +00001231 enum hf_share share, struct vcpu *current)
1232{
1233 struct vm *from = current->vm;
1234 struct vm *to;
1235 int orig_from_mode;
1236 int from_mode;
1237 int to_mode;
1238 ipaddr_t begin;
1239 ipaddr_t end;
1240 paddr_t pa_begin;
1241 paddr_t pa_end;
1242 struct mpool local_page_pool;
1243 int64_t ret;
1244
1245 /* Disallow reflexive shares as this suggests an error in the VM. */
1246 if (vm_id == from->id) {
1247 return -1;
1248 }
1249
1250 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001251 to = vm_find(vm_id);
Andrew Scull6386f252018-12-06 13:29:10 +00001252 if (to == NULL) {
1253 return -1;
1254 }
1255
1256 begin = addr;
1257 end = ipa_add(addr, size);
1258
1259 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001260 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1261 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001262 return -1;
1263 }
1264
1265 /* Convert the sharing request to memory management modes. */
1266 switch (share) {
1267 case HF_MEMORY_GIVE:
1268 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1269 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1270 break;
1271
1272 case HF_MEMORY_LEND:
1273 from_mode = MM_MODE_INVALID;
1274 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1275 break;
1276
1277 case HF_MEMORY_SHARE:
1278 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1279 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1280 MM_MODE_SHARED;
1281 break;
1282
1283 default:
1284 /* The input is untrusted so might not be a valid value. */
1285 return -1;
1286 }
1287
1288 /*
1289 * Create a local pool so any freed memory can't be used by another
1290 * thread. This is to ensure the original mapping can be restored if any
1291 * stage of the process fails.
1292 */
1293 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1294
1295 sl_lock_both(&from->lock, &to->lock);
1296
1297 /*
1298 * Ensure that the memory range is mapped with the same mode so that
1299 * changes can be reverted if the process fails.
1300 */
1301 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1302 goto fail;
1303 }
1304
1305 /*
1306 * Ensure the memory range is valid for the sender. If it isn't, the
1307 * sender has either shared it with another VM already or has no claim
1308 * to the memory.
1309 */
1310 if (orig_from_mode & MM_MODE_INVALID) {
1311 goto fail;
1312 }
1313
1314 /*
1315 * The sender must own the memory and have exclusive access to it in
1316 * order to share it. Alternatively, it is giving memory back to the
1317 * owning VM.
1318 */
1319 if (orig_from_mode & MM_MODE_UNOWNED) {
1320 int orig_to_mode;
1321
1322 if (share != HF_MEMORY_GIVE ||
1323 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1324 orig_to_mode & MM_MODE_UNOWNED) {
1325 goto fail;
1326 }
1327 } else if (orig_from_mode & MM_MODE_SHARED) {
1328 goto fail;
1329 }
1330
1331 pa_begin = pa_from_ipa(begin);
1332 pa_end = pa_from_ipa(end);
1333
1334 /*
1335 * First update the mapping for the sender so there is not overlap with
1336 * the recipient.
1337 */
1338 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1339 NULL, &local_page_pool)) {
1340 goto fail;
1341 }
1342
1343 /* Clear the memory so no VM or device can see the previous contents. */
1344 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1345 goto fail_return_to_sender;
1346 }
1347
1348 /* Complete the transfer by mapping the memory into the recipient. */
1349 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1350 &local_page_pool)) {
1351 /* TODO: partial defrag of failed range. */
1352 /* Recover any memory consumed in failed mapping. */
1353 mm_vm_defrag(&from->ptable, &local_page_pool);
1354 goto fail_return_to_sender;
1355 }
1356
1357 ret = 0;
1358 goto out;
1359
1360fail_return_to_sender:
1361 mm_vm_identity_map(&from->ptable, pa_begin, pa_end, orig_from_mode,
1362 NULL, &local_page_pool);
1363
1364fail:
1365 ret = -1;
1366
1367out:
1368 sl_unlock(&from->lock);
1369 sl_unlock(&to->lock);
1370
1371 mpool_fini(&local_page_pool);
1372
1373 return ret;
1374}
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001375
1376/** Returns the version of the implemented SPCI specification. */
1377int32_t api_spci_version(void)
1378{
1379 /*
1380 * Ensure that both major and minor revision representation occupies at
1381 * most 15 bits.
1382 */
1383 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1384 "Major revision representation take more than 15 bits.");
1385 static_assert(0x10000 > SPCI_VERSION_MINOR,
1386 "Minor revision representation take more than 16 bits.");
1387
1388 return (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
1389 SPCI_VERSION_MINOR;
1390}