blob: 698e7889b0ccc18e7c104ff927c0781f4aaa87b3 [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 */
Andrew Walbranfc6cd9d2019-06-25 15:23:27 +0100211spci_vm_id_t api_vm_get_id(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000212{
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 *
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100383 * It's ok not to return the sleep duration here because
Andrew Scullb06d1752019-02-04 10:15:48 +0000384 * the other physical CPU that is currently running this
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100385 * vCPU will return the sleep duration if needed. The
386 * default return value is
387 * HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no need to set it
388 * explicitly.
Andrew Scullb06d1752019-02-04 10:15:48 +0000389 */
390 ret = false;
391 goto out;
392 }
393
394 sl_unlock(&vcpu->lock);
395 if (need_vm_lock) {
396 sl_unlock(&vcpu->vm->lock);
397 }
398 }
Andrew Scull9726c252019-01-23 13:44:19 +0000399
400 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100401 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Scull82331282019-01-25 10:29:34 +0000402 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
403 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100404 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000405 }
406 ret = false;
407 goto out;
408 }
409
Andrew Walbran508e63c2018-12-20 17:02:37 +0000410 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100411 case VCPU_STATE_RUNNING:
412 case VCPU_STATE_OFF:
413 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000414 ret = false;
415 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000416
Andrew Sculld6ee1102019-04-05 22:12:42 +0100417 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000418 /*
419 * A pending message allows the vCPU to run so the message can
420 * be delivered directly.
421 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100422 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000423 arch_regs_set_retval(&vcpu->regs, SPCI_SUCCESS);
Andrew Sculld6ee1102019-04-05 22:12:42 +0100424 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000425 break;
426 }
427 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100428 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000429 /* Allow virtual interrupts to be delivered. */
430 if (vcpu->interrupts.enabled_and_pending_count > 0) {
431 break;
432 }
433
434 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000435 if (arch_timer_pending(&vcpu->regs)) {
436 break;
437 }
438
439 /*
440 * The vCPU is not ready to run, return the appropriate code to
441 * the primary which called vcpu_run.
442 */
443 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000444 run_ret->code =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100445 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Scullb06d1752019-02-04 10:15:48 +0000446 ? HF_VCPU_RUN_WAIT_FOR_MESSAGE
447 : HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000448 run_ret->sleep.ns =
449 arch_timer_remaining_ns(&vcpu->regs);
450 }
451
452 ret = false;
453 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000454
Andrew Sculld6ee1102019-04-05 22:12:42 +0100455 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000456 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000457 }
458
Andrew Scullb06d1752019-02-04 10:15:48 +0000459 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000460 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100461 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000462
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000463 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000464 * Mark the registers as unavailable now that we're about to reflect
465 * them onto the real registers. This will also prevent another physical
466 * CPU from trying to read these registers.
467 */
468 vcpu->regs_available = false;
469
470 ret = true;
471
472out:
473 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000474 if (need_vm_lock) {
475 sl_unlock(&vcpu->vm->lock);
476 }
477
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000478 return ret;
479}
480
481/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100482 * Runs the given vcpu of the given vm.
483 */
Andrew Walbran42347a92019-05-09 13:59:03 +0100484struct hf_vcpu_run_return api_vcpu_run(spci_vm_id_t vm_id, uint32_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000485 const struct vcpu *current,
486 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100487{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100488 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100489 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100490 struct hf_vcpu_run_return ret = {
491 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
Andrew Scullb06d1752019-02-04 10:15:48 +0000492 .sleep.ns = HF_SLEEP_INDEFINITE,
Andrew Scull6d2db332018-10-10 15:28:17 +0100493 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100494
495 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100496 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100497 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100498 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100499
Andrew Scull19503262018-09-20 14:48:39 +0100500 /* Only secondary VM vcpus can be run. */
501 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100502 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100503 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100504
Andrew Scull19503262018-09-20 14:48:39 +0100505 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100506 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100507 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100508 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100509 }
510
511 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100512 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100513 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100514 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100515
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000516 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100517 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000518 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000519 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100520 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000521
Andrew Walbran508e63c2018-12-20 17:02:37 +0000522 /*
523 * Inject timer interrupt if timer has expired. It's safe to access
524 * vcpu->regs here because api_vcpu_prepare_run already made sure that
525 * regs_available was true (and then set it to false) before returning
526 * true.
527 */
528 if (arch_timer_pending(&vcpu->regs)) {
529 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100530 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
531 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000532
533 /*
534 * Set the mask bit so the hardware interrupt doesn't fire
535 * again. Ideally we wouldn't do this because it affects what
536 * the secondary vCPU sees, but if we don't then we end up with
537 * a loop of the interrupt firing each time we try to return to
538 * the secondary vCPU.
539 */
540 arch_timer_mask(&vcpu->regs);
541 }
542
Andrew Scull33fecd32019-01-08 14:48:27 +0000543 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000544 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000545
Andrew Scull33fecd32019-01-08 14:48:27 +0000546 /*
547 * Set a placeholder return code to the scheduler. This will be
548 * overwritten when the switch back to the primary occurs.
549 */
550 ret.code = HF_VCPU_RUN_PREEMPTED;
551
Andrew Scull6d2db332018-10-10 15:28:17 +0100552out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100553 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100554}
555
556/**
Andrew Scull81e85092018-12-12 12:56:20 +0000557 * Check that the mode indicates memory that is valid, owned and exclusive.
558 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000559static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000560{
561 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
562 0;
563}
564
565/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000566 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
567 * after they've succeeded. If a secondary VM is running and there are waiters,
568 * it also switches back to the primary VM for it to wake waiters up.
569 */
570static int64_t api_waiter_result(struct vm_locked locked_vm,
571 struct vcpu *current, struct vcpu **next)
572{
573 struct vm *vm = locked_vm.vm;
574 struct hf_vcpu_run_return ret = {
575 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
576 };
577
578 if (list_empty(&vm->mailbox.waiter_list)) {
579 /* No waiters, nothing else to do. */
580 return 0;
581 }
582
583 if (vm->id == HF_PRIMARY_VM_ID) {
584 /* The caller is the primary VM. Tell it to wake up waiters. */
585 return 1;
586 }
587
588 /*
589 * Switch back to the primary VM, informing it that there are waiters
590 * that need to be notified.
591 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100592 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000593
594 return 0;
595}
596
597/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100598 * Configures the VM to send/receive data through the specified pages. The pages
599 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000600 *
601 * Returns:
602 * - -1 on failure.
603 * - 0 on success if no further action is needed.
604 * - 1 if it was called by the primary VM and the primary VM now needs to wake
605 * up or kick waiters. Waiters should be retrieved by calling
606 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100607 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000608int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
609 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100610{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100611 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000612 struct vm_locked locked;
Andrew Scull80871322018-08-06 12:04:09 +0100613 paddr_t pa_send_begin;
614 paddr_t pa_send_end;
615 paddr_t pa_recv_begin;
616 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000617 int orig_send_mode;
618 int orig_recv_mode;
619 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100620 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100621
622 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000623 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
624 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100625 return -1;
626 }
627
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000628 /* Convert to physical addresses. */
629 pa_send_begin = pa_from_ipa(send);
630 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
631
632 pa_recv_begin = pa_from_ipa(recv);
633 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
634
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100635 /* Fail if the same page is used for the send and receive pages. */
636 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000637 return -1;
638 }
639
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100640 locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000641
642 /* We only allow these to be setup once. */
643 if (vm->mailbox.send || vm->mailbox.recv) {
644 goto fail;
645 }
646
647 /*
648 * Ensure the pages are valid, owned and exclusive to the VM and that
649 * the VM has the required access to the memory.
650 */
651 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
652 &orig_send_mode) ||
653 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
654 (orig_send_mode & MM_MODE_R) == 0 ||
655 (orig_send_mode & MM_MODE_W) == 0) {
656 goto fail;
657 }
658
659 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
660 &orig_recv_mode) ||
661 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
662 (orig_recv_mode & MM_MODE_R) == 0) {
663 goto fail;
664 }
665
666 /*
667 * Create a local pool so any freed memory can't be used by another
668 * thread. This is to ensure the original mapping can be restored if any
669 * stage of the process fails.
670 */
671 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
672
673 /* Take memory ownership away from the VM and mark as shared. */
674 if (!mm_vm_identity_map(
675 &vm->ptable, pa_send_begin, pa_send_end,
676 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
677 NULL, &local_page_pool)) {
678 goto fail_free_pool;
679 }
680
681 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
682 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
683 NULL, &local_page_pool)) {
684 /* TODO: partial defrag of failed range. */
685 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000686 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000687 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100688 }
689
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100690 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000691 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000692 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100693 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000694 /* TODO: partial defrag of failed range. */
695 /* Recover any memory consumed in failed mapping. */
696 mm_defrag(&local_page_pool);
697 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100698 }
699
700 /*
701 * Map the receive page as writable in the hypervisor address space. On
702 * failure, unmap the send page before returning.
703 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000704 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000705 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100706 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000707 /* TODO: partial defrag of failed range. */
708 /* Recover any memory consumed in failed mapping. */
709 mm_defrag(&local_page_pool);
710 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100711 }
712
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000713 /* Tell caller about waiters, if any. */
714 ret = api_waiter_result(locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000715 goto exit;
716
717 /*
718 * The following mappings will not require more memory than is available
719 * in the local pool.
720 */
721fail_undo_all:
722 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000723 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000724
725fail_undo_send_and_recv:
726 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
727 orig_recv_mode, NULL, &local_page_pool);
728
729fail_undo_send:
730 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
731 orig_send_mode, NULL, &local_page_pool);
732
733fail_free_pool:
734 mpool_fini(&local_page_pool);
735
736fail:
737 ret = -1;
738
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100739exit:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000740 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100741
742 return ret;
743}
744
745/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100746 * Copies data from the sender's send buffer to the recipient's receive buffer
747 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000748 *
749 * If the recipient's receive buffer is busy, it can optionally register the
750 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100751 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000752int32_t api_spci_msg_send(uint32_t attributes, struct vcpu *current,
753 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100754{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100755 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100756 struct vm *to;
Andrew Scullb06d1752019-02-04 10:15:48 +0000757 struct hf_vcpu_run_return primary_ret = {
758 .code = HF_VCPU_RUN_MESSAGE,
759 };
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000760 struct spci_message from_msg_replica;
761 struct spci_message *to_msg;
762 const struct spci_message *from_msg;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100763
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000764 uint32_t size;
Andrew Scull19503262018-09-20 14:48:39 +0100765
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000766 int64_t ret;
767 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
768 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100769
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000770 /*
771 * Check that the sender has configured its send buffer. Copy the
772 * message header. If the tx mailbox at from_msg is configured (i.e.
773 * from_msg != NULL) then it can be safely accessed after releasing the
774 * lock since the tx mailbox address can only be configured once.
775 */
776 sl_lock(&from->lock);
777 from_msg = from->mailbox.send;
778 sl_unlock(&from->lock);
779
780 if (from_msg == NULL) {
781 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100782 }
783
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100784 /*
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000785 * Note that the payload is not copied when the message header is.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100786 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000787 from_msg_replica = *from_msg;
788
789 /* Ensure source VM id corresponds to the current VM. */
790 if (from_msg_replica.source_vm_id != from->id) {
791 return SPCI_INVALID_PARAMETERS;
792 }
793
794 size = from_msg_replica.length;
795 /* Limit the size of transfer. */
Andrew Scull1262ac22019-04-05 12:44:26 +0100796 if (size > SPCI_MSG_PAYLOAD_MAX) {
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000797 return SPCI_INVALID_PARAMETERS;
798 }
799
800 /* Disallow reflexive requests as this suggests an error in the VM. */
801 if (from_msg_replica.target_vm_id == from->id) {
802 return SPCI_INVALID_PARAMETERS;
803 }
804
805 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100806 to = vm_find(from_msg_replica.target_vm_id);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000807 if (to == NULL) {
808 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100809 }
810
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100811 sl_lock(&to->lock);
812
Andrew Sculld6ee1102019-04-05 22:12:42 +0100813 if (to->mailbox.state != MAILBOX_STATE_EMPTY ||
Andrew Scullaa039b32018-10-04 15:02:26 +0100814 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000815 /*
816 * Fail if the target isn't currently ready to receive data,
817 * setting up for notification if requested.
818 */
819 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000820 struct wait_entry *entry =
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000821 &current->vm->wait_entries
822 [from_msg_replica.target_vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000823
824 /* Append waiter only if it's not there yet. */
825 if (list_empty(&entry->wait_links)) {
826 list_append(&to->mailbox.waiter_list,
827 &entry->wait_links);
828 }
829 }
830
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000831 ret = SPCI_BUSY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100832 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100833 }
834
Andrew Scullaa039b32018-10-04 15:02:26 +0100835 /* Copy data. */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000836 to_msg = to->mailbox.recv;
837 *to_msg = from_msg_replica;
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100838 memcpy_s(to_msg->payload, SPCI_MSG_PAYLOAD_MAX,
839 from->mailbox.send->payload, size);
Andrew Scullb06d1752019-02-04 10:15:48 +0000840 primary_ret.message.vm_id = to->id;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000841 ret = SPCI_SUCCESS;
Andrew Scullaa039b32018-10-04 15:02:26 +0100842
843 /* Messages for the primary VM are delivered directly. */
844 if (to->id == HF_PRIMARY_VM_ID) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100845 to->mailbox.state = MAILBOX_STATE_READ;
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000846 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100847 VCPU_STATE_READY);
Andrew Scullaa039b32018-10-04 15:02:26 +0100848 goto out;
849 }
850
Andrew Sculld6ee1102019-04-05 22:12:42 +0100851 to->mailbox.state = MAILBOX_STATE_RECEIVED;
Andrew Scullaa039b32018-10-04 15:02:26 +0100852
853 /* Return to the primary VM directly or with a switch. */
Andrew Scullb06d1752019-02-04 10:15:48 +0000854 if (from->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000855 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100856 VCPU_STATE_READY);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000857 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100858
859out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100860 sl_unlock(&to->lock);
861
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000862 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100863}
864
865/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100866 * Receives a message from the mailbox. If one isn't available, this function
867 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100868 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100869 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100870 */
Jose Marinho3e2442f2019-03-12 13:30:37 +0000871int32_t api_spci_msg_recv(uint32_t attributes, struct vcpu *current,
872 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100873{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100874 struct vm *vm = current->vm;
Jose Marinho3e2442f2019-03-12 13:30:37 +0000875 int32_t return_code;
876 bool block =
877 (attributes & SPCI_MSG_RECV_BLOCK_MASK) == SPCI_MSG_RECV_BLOCK;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100878
Andrew Scullaa039b32018-10-04 15:02:26 +0100879 /*
880 * The primary VM will receive messages as a status code from running
881 * vcpus and must not call this function.
882 */
Andrew Scull19503262018-09-20 14:48:39 +0100883 if (vm->id == HF_PRIMARY_VM_ID) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000884 return SPCI_INTERRUPTED;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100885 }
886
887 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100888
Andrew Scullaa039b32018-10-04 15:02:26 +0100889 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100890 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
891 vm->mailbox.state = MAILBOX_STATE_READ;
Jose Marinho3e2442f2019-03-12 13:30:37 +0000892 return_code = SPCI_SUCCESS;
893 goto out;
894 }
895
896 /* No pending message so fail if not allowed to block. */
897 if (!block) {
898 return_code = SPCI_RETRY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100899 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100900 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100901
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000902 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +0000903 * From this point onward this call can only be interrupted or a message
904 * received. If a message is received the return value will be set at
905 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000906 */
Jose Marinho3e2442f2019-03-12 13:30:37 +0000907 return_code = SPCI_INTERRUPTED;
908
909 /*
910 * Don't block if there are enabled and pending interrupts, to match
911 * behaviour of wait_for_interrupt.
912 */
913 if (current->interrupts.enabled_and_pending_count > 0) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100914 goto out;
915 }
916
Andrew Scullaa039b32018-10-04 15:02:26 +0100917 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000918 {
919 struct hf_vcpu_run_return run_return = {
Andrew Scullb06d1752019-02-04 10:15:48 +0000920 .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE,
Andrew Walbranb4816552018-12-05 17:35:42 +0000921 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000922
Andrew Walbranb4816552018-12-05 17:35:42 +0000923 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100924 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +0000925 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100926out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100927 sl_unlock(&vm->lock);
928
Jose Marinho3e2442f2019-03-12 13:30:37 +0000929 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100930}
931
932/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000933 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
934 * by this function, the caller must have called api_mailbox_send before with
935 * the notify argument set to true, and this call must have failed because the
936 * mailbox was not available.
937 *
938 * It should be called repeatedly to retrieve a list of VMs.
939 *
940 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
941 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100942 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000943int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100944{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100945 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000946 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100947 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100948
949 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000950 if (list_empty(&vm->mailbox.ready_list)) {
951 ret = -1;
952 goto exit;
953 }
954
955 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
956 ready_links);
957 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000958 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000959
960exit:
961 sl_unlock(&vm->lock);
962 return ret;
963}
964
965/**
966 * Retrieves the next VM waiting to be notified that the mailbox of the
967 * specified VM became writable. Only primary VMs are allowed to call this.
968 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000969 * Returns -1 on failure or if there are no waiters; the VM id of the next
970 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000971 */
Andrew Walbran42347a92019-05-09 13:59:03 +0100972int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000973{
974 struct vm *vm;
975 struct vm_locked locked;
976 struct wait_entry *entry;
977 struct vm *waiting_vm;
978
979 /* Only primary VMs are allowed to call this function. */
980 if (current->vm->id != HF_PRIMARY_VM_ID) {
981 return -1;
982 }
983
Andrew Walbran42347a92019-05-09 13:59:03 +0100984 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000985 if (vm == NULL) {
986 return -1;
987 }
988
989 /* Check if there are outstanding notifications from given vm. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100990 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000991 entry = api_fetch_waiter(locked);
992 vm_unlock(&locked);
993
994 if (entry == NULL) {
995 return -1;
996 }
997
998 /* Enqueue notification to waiting VM. */
999 waiting_vm = entry->waiting_vm;
1000
1001 sl_lock(&waiting_vm->lock);
1002 if (list_empty(&entry->ready_links)) {
1003 list_append(&waiting_vm->mailbox.ready_list,
1004 &entry->ready_links);
1005 }
1006 sl_unlock(&waiting_vm->lock);
1007
1008 return waiting_vm->id;
1009}
1010
1011/**
1012 * Clears the caller's mailbox so that a new message can be received. The caller
1013 * must have copied out all data they wish to preserve as new messages will
1014 * overwrite the old and will arrive asynchronously.
1015 *
1016 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001017 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001018 * - 0 on success if no further action is needed.
1019 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1020 * up or kick waiters. Waiters should be retrieved by calling
1021 * hf_mailbox_waiter_get.
1022 */
1023int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
1024{
1025 struct vm *vm = current->vm;
1026 struct vm_locked locked;
1027 int64_t ret;
1028
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001029 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001030 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001031 case MAILBOX_STATE_EMPTY:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001032 ret = 0;
1033 break;
1034
Andrew Sculld6ee1102019-04-05 22:12:42 +01001035 case MAILBOX_STATE_RECEIVED:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001036 ret = -1;
1037 break;
1038
Andrew Sculld6ee1102019-04-05 22:12:42 +01001039 case MAILBOX_STATE_READ:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001040 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001041 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001042 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001043 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001044 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001045
1046 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001047}
Andrew Walbran318f5732018-11-20 16:23:42 +00001048
1049/**
1050 * Enables or disables a given interrupt ID for the calling vCPU.
1051 *
1052 * Returns 0 on success, or -1 if the intid is invalid.
1053 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001054int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001055{
1056 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
1057 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001058
Andrew Walbran318f5732018-11-20 16:23:42 +00001059 if (intid >= HF_NUM_INTIDS) {
1060 return -1;
1061 }
1062
1063 sl_lock(&current->lock);
1064 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001065 /*
1066 * If it is pending and was not enabled before, increment the
1067 * count.
1068 */
1069 if (current->interrupts.interrupt_pending[intid_index] &
1070 ~current->interrupts.interrupt_enabled[intid_index] &
1071 intid_mask) {
1072 current->interrupts.enabled_and_pending_count++;
1073 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001074 current->interrupts.interrupt_enabled[intid_index] |=
1075 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001076 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001077 /*
1078 * If it is pending and was enabled before, decrement the count.
1079 */
1080 if (current->interrupts.interrupt_pending[intid_index] &
1081 current->interrupts.interrupt_enabled[intid_index] &
1082 intid_mask) {
1083 current->interrupts.enabled_and_pending_count--;
1084 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001085 current->interrupts.interrupt_enabled[intid_index] &=
1086 ~intid_mask;
1087 }
1088
1089 sl_unlock(&current->lock);
1090 return 0;
1091}
1092
1093/**
1094 * Returns the ID of the next pending interrupt for the calling vCPU, and
1095 * acknowledges it (i.e. marks it as no longer pending). Returns
1096 * HF_INVALID_INTID if there are no pending interrupts.
1097 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001098uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001099{
1100 uint8_t i;
1101 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001102
1103 /*
1104 * Find the first enabled and pending interrupt ID, return it, and
1105 * deactivate it.
1106 */
1107 sl_lock(&current->lock);
1108 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1109 uint32_t enabled_and_pending =
1110 current->interrupts.interrupt_enabled[i] &
1111 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001112
Andrew Walbran318f5732018-11-20 16:23:42 +00001113 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001114 uint8_t bit_index = ctz(enabled_and_pending);
1115 /*
1116 * Mark it as no longer pending and decrement the count.
1117 */
1118 current->interrupts.interrupt_pending[i] &=
1119 ~(1u << bit_index);
1120 current->interrupts.enabled_and_pending_count--;
1121 first_interrupt =
1122 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001123 break;
1124 }
1125 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001126
1127 sl_unlock(&current->lock);
1128 return first_interrupt;
1129}
1130
1131/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001132 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001133 * given VM and vCPU.
1134 */
1135static inline bool is_injection_allowed(uint32_t target_vm_id,
1136 struct vcpu *current)
1137{
1138 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001139
Andrew Walbran318f5732018-11-20 16:23:42 +00001140 /*
1141 * The primary VM is allowed to inject interrupts into any VM. Secondary
1142 * VMs are only allowed to inject interrupts into their own vCPUs.
1143 */
1144 return current_vm_id == HF_PRIMARY_VM_ID ||
1145 current_vm_id == target_vm_id;
1146}
1147
1148/**
1149 * Injects a virtual interrupt of the given ID into the given target vCPU.
1150 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1151 * when the vCPU is next run, which is up to the scheduler.
1152 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001153 * Returns:
1154 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1155 * ID is invalid, or the current VM is not allowed to inject interrupts to
1156 * the target VM.
1157 * - 0 on success if no further action is needed.
1158 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1159 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001160 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001161int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
1162 uint32_t target_vcpu_idx, uint32_t intid,
1163 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001164{
Andrew Walbran318f5732018-11-20 16:23:42 +00001165 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001166 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001167
1168 if (intid >= HF_NUM_INTIDS) {
1169 return -1;
1170 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001171
Andrew Walbran318f5732018-11-20 16:23:42 +00001172 if (target_vm == NULL) {
1173 return -1;
1174 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001175
Andrew Walbran318f5732018-11-20 16:23:42 +00001176 if (target_vcpu_idx >= target_vm->vcpu_count) {
1177 /* The requested vcpu must exist. */
1178 return -1;
1179 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001180
Andrew Walbran318f5732018-11-20 16:23:42 +00001181 if (!is_injection_allowed(target_vm_id, current)) {
1182 return -1;
1183 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001184
Andrew Walbrane1310df2019-04-29 17:28:28 +01001185 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001186
1187 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1188 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001189 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001190}
Andrew Scull6386f252018-12-06 13:29:10 +00001191
1192/**
1193 * Clears a region of physical memory by overwriting it with zeros. The data is
1194 * flushed from the cache so the memory has been cleared across the system.
1195 */
1196static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1197{
1198 /*
1199 * TODO: change this to a cpu local single page window rather than a
1200 * global mapping of the whole range. Such an approach will limit
1201 * the changes to stage-1 tables and will allow only local
1202 * invalidation.
1203 */
1204 void *ptr = mm_identity_map(begin, end, MM_MODE_W, ppool);
Andrew Walbran2cb43392019-04-17 12:52:45 +01001205 size_t size = pa_difference(begin, end);
Andrew Scull6386f252018-12-06 13:29:10 +00001206
1207 if (!ptr) {
1208 /* TODO: partial defrag of failed range. */
1209 /* Recover any memory consumed in failed mapping. */
1210 mm_defrag(ppool);
1211 return false;
1212 }
1213
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001214 memset_s(ptr, size, 0, size);
Andrew Scull6386f252018-12-06 13:29:10 +00001215 arch_mm_write_back_dcache(ptr, size);
1216 mm_unmap(begin, end, ppool);
1217
1218 return true;
1219}
1220
1221/**
1222 * Shares memory from the calling VM with another. The memory can be shared in
1223 * different modes.
1224 *
1225 * TODO: the interface for sharing memory will need to be enhanced to allow
1226 * sharing with different modes e.g. read-only, informing the recipient
1227 * of the memory they have been given, opting to not wipe the memory and
1228 * possibly allowing multiple blocks to be transferred. What this will
1229 * look like is TBD.
1230 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001231int64_t api_share_memory(spci_vm_id_t vm_id, ipaddr_t addr, size_t size,
Andrew Scull6386f252018-12-06 13:29:10 +00001232 enum hf_share share, struct vcpu *current)
1233{
1234 struct vm *from = current->vm;
1235 struct vm *to;
1236 int orig_from_mode;
1237 int from_mode;
1238 int to_mode;
1239 ipaddr_t begin;
1240 ipaddr_t end;
1241 paddr_t pa_begin;
1242 paddr_t pa_end;
1243 struct mpool local_page_pool;
1244 int64_t ret;
1245
1246 /* Disallow reflexive shares as this suggests an error in the VM. */
1247 if (vm_id == from->id) {
1248 return -1;
1249 }
1250
1251 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001252 to = vm_find(vm_id);
Andrew Scull6386f252018-12-06 13:29:10 +00001253 if (to == NULL) {
1254 return -1;
1255 }
1256
1257 begin = addr;
1258 end = ipa_add(addr, size);
1259
1260 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001261 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1262 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001263 return -1;
1264 }
1265
1266 /* Convert the sharing request to memory management modes. */
1267 switch (share) {
1268 case HF_MEMORY_GIVE:
1269 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1270 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1271 break;
1272
1273 case HF_MEMORY_LEND:
1274 from_mode = MM_MODE_INVALID;
1275 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1276 break;
1277
1278 case HF_MEMORY_SHARE:
1279 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1280 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1281 MM_MODE_SHARED;
1282 break;
1283
1284 default:
1285 /* The input is untrusted so might not be a valid value. */
1286 return -1;
1287 }
1288
1289 /*
1290 * Create a local pool so any freed memory can't be used by another
1291 * thread. This is to ensure the original mapping can be restored if any
1292 * stage of the process fails.
1293 */
1294 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1295
1296 sl_lock_both(&from->lock, &to->lock);
1297
1298 /*
1299 * Ensure that the memory range is mapped with the same mode so that
1300 * changes can be reverted if the process fails.
1301 */
1302 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1303 goto fail;
1304 }
1305
1306 /*
1307 * Ensure the memory range is valid for the sender. If it isn't, the
1308 * sender has either shared it with another VM already or has no claim
1309 * to the memory.
1310 */
1311 if (orig_from_mode & MM_MODE_INVALID) {
1312 goto fail;
1313 }
1314
1315 /*
1316 * The sender must own the memory and have exclusive access to it in
1317 * order to share it. Alternatively, it is giving memory back to the
1318 * owning VM.
1319 */
1320 if (orig_from_mode & MM_MODE_UNOWNED) {
1321 int orig_to_mode;
1322
1323 if (share != HF_MEMORY_GIVE ||
1324 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1325 orig_to_mode & MM_MODE_UNOWNED) {
1326 goto fail;
1327 }
1328 } else if (orig_from_mode & MM_MODE_SHARED) {
1329 goto fail;
1330 }
1331
1332 pa_begin = pa_from_ipa(begin);
1333 pa_end = pa_from_ipa(end);
1334
1335 /*
1336 * First update the mapping for the sender so there is not overlap with
1337 * the recipient.
1338 */
1339 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1340 NULL, &local_page_pool)) {
1341 goto fail;
1342 }
1343
1344 /* Clear the memory so no VM or device can see the previous contents. */
1345 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1346 goto fail_return_to_sender;
1347 }
1348
1349 /* Complete the transfer by mapping the memory into the recipient. */
1350 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1351 &local_page_pool)) {
1352 /* TODO: partial defrag of failed range. */
1353 /* Recover any memory consumed in failed mapping. */
1354 mm_vm_defrag(&from->ptable, &local_page_pool);
1355 goto fail_return_to_sender;
1356 }
1357
1358 ret = 0;
1359 goto out;
1360
1361fail_return_to_sender:
1362 mm_vm_identity_map(&from->ptable, pa_begin, pa_end, orig_from_mode,
1363 NULL, &local_page_pool);
1364
1365fail:
1366 ret = -1;
1367
1368out:
1369 sl_unlock(&from->lock);
1370 sl_unlock(&to->lock);
1371
1372 mpool_fini(&local_page_pool);
1373
1374 return ret;
1375}
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001376
1377/** Returns the version of the implemented SPCI specification. */
1378int32_t api_spci_version(void)
1379{
1380 /*
1381 * Ensure that both major and minor revision representation occupies at
1382 * most 15 bits.
1383 */
1384 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1385 "Major revision representation take more than 15 bits.");
1386 static_assert(0x10000 > SPCI_VERSION_MINOR,
1387 "Minor revision representation take more than 16 bits.");
1388
1389 return (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
1390 SPCI_VERSION_MINOR;
1391}