Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/api.h" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 18 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 19 | #include "hf/arch/cpu.h" |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 20 | #include "hf/arch/timer.h" |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 21 | |
Andrew Scull | 5c496a3 | 2019-04-04 11:57:33 +0100 | [diff] [blame] | 22 | #include "hf/assert.h" |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 23 | #include "hf/dlog.h" |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 24 | #include "hf/mm.h" |
| 25 | #include "hf/spinlock.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 26 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 27 | #include "hf/vm.h" |
| 28 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 29 | #include "vmapi/hf/call.h" |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 30 | #include "vmapi/hf/spci.h" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 31 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 32 | /* |
| 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 Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 38 | * |
| 39 | * Locks of the same kind require the lock of lowest address to be locked first, |
| 40 | * see `sl_lock_both()`. |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 41 | */ |
| 42 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 43 | static_assert(HF_MAILBOX_SIZE == PAGE_SIZE, |
Andrew Scull | 13652af | 2018-09-17 14:49:08 +0100 | [diff] [blame] | 44 | "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 Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 47 | static struct mpool api_page_pool; |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 48 | |
| 49 | /** |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 50 | * Initialises the API page pool by taking ownership of the contents of the |
| 51 | * given page pool. |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 52 | */ |
| 53 | void api_init(struct mpool *ppool) |
| 54 | { |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 55 | mpool_init_from(&api_page_pool, ppool); |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 58 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 59 | * Switches the physical CPU back to the corresponding vcpu of the primary VM. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 60 | * |
| 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 Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 64 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 65 | static struct vcpu *api_switch_to_primary(struct vcpu *current, |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 66 | struct hf_vcpu_run_return primary_ret, |
| 67 | enum vcpu_state secondary_state) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 68 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 69 | struct vm *primary = vm_get(HF_PRIMARY_VM_ID); |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame^] | 70 | struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu)); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 71 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 72 | /* |
| 73 | * If the secondary is blocked but has a timer running, sleep until the |
| 74 | * timer fires rather than indefinitely. |
| 75 | */ |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 76 | 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 Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 90 | /* Set the return value for the primary VM's call to HF_VCPU_RUN. */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 91 | arch_regs_set_retval(&next->regs, |
| 92 | hf_vcpu_run_return_encode(primary_ret)); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 93 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 94 | /* Mark the current vcpu as waiting. */ |
| 95 | sl_lock(¤t->lock); |
| 96 | current->state = secondary_state; |
| 97 | sl_unlock(¤t->lock); |
| 98 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 99 | return next; |
| 100 | } |
| 101 | |
| 102 | /** |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 103 | * Returns to the primary vm and signals that the vcpu still has work to do so. |
| 104 | */ |
| 105 | struct vcpu *api_preempt(struct vcpu *current) |
| 106 | { |
| 107 | struct hf_vcpu_run_return ret = { |
| 108 | .code = HF_VCPU_RUN_PREEMPTED, |
| 109 | }; |
| 110 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 111 | return api_switch_to_primary(current, ret, VCPU_STATE_READY); |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 115 | * Puts the current vcpu in wait for interrupt mode, and returns to the primary |
| 116 | * vm. |
| 117 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 118 | struct vcpu *api_wait_for_interrupt(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 119 | { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 120 | struct hf_vcpu_run_return ret = { |
| 121 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 122 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 123 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 124 | return api_switch_to_primary(current, ret, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 125 | VCPU_STATE_BLOCKED_INTERRUPT); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 129 | * Returns to the primary vm to allow this cpu to be used for other tasks as the |
| 130 | * vcpu does not have work to do at this moment. The current vcpu is marked as |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 131 | * ready to be scheduled again. This SPCI function always returns SPCI_SUCCESS. |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 132 | */ |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 133 | int32_t api_spci_yield(struct vcpu *current, struct vcpu **next) |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 134 | { |
| 135 | struct hf_vcpu_run_return ret = { |
| 136 | .code = HF_VCPU_RUN_YIELD, |
| 137 | }; |
| 138 | |
| 139 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 140 | /* Noop on the primary as it makes the scheduling decisions. */ |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 141 | return SPCI_SUCCESS; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 144 | *next = api_switch_to_primary(current, ret, VCPU_STATE_READY); |
| 145 | |
| 146 | /* SPCI_YIELD always returns SPCI_SUCCESS. */ |
| 147 | return SPCI_SUCCESS; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 151 | * Aborts the vCPU and triggers its VM to abort fully. |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 152 | */ |
| 153 | struct vcpu *api_abort(struct vcpu *current) |
| 154 | { |
| 155 | struct hf_vcpu_run_return ret = { |
| 156 | .code = HF_VCPU_RUN_ABORTED, |
| 157 | }; |
| 158 | |
| 159 | dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current)); |
| 160 | |
| 161 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 162 | /* TODO: what to do when the primary aborts? */ |
| 163 | for (;;) { |
| 164 | /* Do nothing. */ |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | atomic_store_explicit(¤t->vm->aborting, true, |
| 169 | memory_order_relaxed); |
| 170 | |
| 171 | /* TODO: free resources once all vCPUs abort. */ |
| 172 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 173 | return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 177 | * Returns the ID of the VM. |
| 178 | */ |
| 179 | int64_t api_vm_get_id(const struct vcpu *current) |
| 180 | { |
| 181 | return current->vm->id; |
| 182 | } |
| 183 | |
| 184 | /** |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 185 | * Returns the number of VMs configured to run. |
| 186 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 187 | int64_t api_vm_get_count(void) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 188 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 189 | return vm_get_count(); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns the number of vcpus configured in the given VM. |
| 194 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 195 | int64_t api_vcpu_get_count(uint32_t vm_id, const struct vcpu *current) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 196 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 197 | struct vm *vm; |
| 198 | |
| 199 | /* Only the primary VM needs to know about vcpus for scheduling. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 200 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 201 | return -1; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 202 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 203 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 204 | vm = vm_get(vm_id); |
| 205 | if (vm == NULL) { |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | return vm->vcpu_count; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 213 | * This function is called by the architecture-specific context switching |
| 214 | * function to indicate that register state for the given vcpu has been saved |
| 215 | * and can therefore be used by other pcpus. |
| 216 | */ |
| 217 | void api_regs_state_saved(struct vcpu *vcpu) |
| 218 | { |
| 219 | sl_lock(&vcpu->lock); |
| 220 | vcpu->regs_available = true; |
| 221 | sl_unlock(&vcpu->lock); |
| 222 | } |
| 223 | |
| 224 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 225 | * Retrieves the next waiter and removes it from the wait list if the VM's |
| 226 | * mailbox is in a writable state. |
| 227 | */ |
| 228 | static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm) |
| 229 | { |
| 230 | struct wait_entry *entry; |
| 231 | struct vm *vm = locked_vm.vm; |
| 232 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 233 | if (vm->mailbox.state != MAILBOX_STATE_EMPTY || |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 234 | vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) { |
| 235 | /* The mailbox is not writable or there are no waiters. */ |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | /* Remove waiter from the wait list. */ |
| 240 | entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry, |
| 241 | wait_links); |
| 242 | list_remove(&entry->wait_links); |
| 243 | return entry; |
| 244 | } |
| 245 | |
| 246 | /** |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 247 | * Assuming that the arguments have already been checked by the caller, injects |
| 248 | * a virtual interrupt of the given ID into the given target vCPU. This doesn't |
| 249 | * cause the vCPU to actually be run immediately; it will be taken when the vCPU |
| 250 | * is next run, which is up to the scheduler. |
| 251 | * |
| 252 | * Returns: |
| 253 | * - 0 on success if no further action is needed. |
| 254 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 255 | * up or kick the target vCPU. |
| 256 | */ |
| 257 | static int64_t internal_interrupt_inject(struct vm *target_vm, |
| 258 | struct vcpu *target_vcpu, |
| 259 | uint32_t intid, struct vcpu *current, |
| 260 | struct vcpu **next) |
| 261 | { |
| 262 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
| 263 | uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 264 | int64_t ret = 0; |
| 265 | |
| 266 | sl_lock(&target_vcpu->lock); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * We only need to change state and (maybe) trigger a virtual IRQ if it |
| 270 | * is enabled and was not previously pending. Otherwise we can skip |
| 271 | * everything except setting the pending bit. |
| 272 | * |
| 273 | * If you change this logic make sure to update the need_vm_lock logic |
| 274 | * above to match. |
| 275 | */ |
| 276 | if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] & |
| 277 | ~target_vcpu->interrupts.interrupt_pending[intid_index] & |
| 278 | intid_mask)) { |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | /* Increment the count. */ |
| 283 | target_vcpu->interrupts.enabled_and_pending_count++; |
| 284 | |
| 285 | /* |
| 286 | * Only need to update state if there was not already an |
| 287 | * interrupt enabled and pending. |
| 288 | */ |
| 289 | if (target_vcpu->interrupts.enabled_and_pending_count != 1) { |
| 290 | goto out; |
| 291 | } |
| 292 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 293 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 294 | /* |
| 295 | * If the call came from the primary VM, let it know that it |
| 296 | * should run or kick the target vCPU. |
| 297 | */ |
| 298 | ret = 1; |
| 299 | } else if (current != target_vcpu && next != NULL) { |
| 300 | /* |
| 301 | * Switch to the primary so that it can switch to the target, or |
| 302 | * kick it if it is already running on a different physical CPU. |
| 303 | */ |
| 304 | struct hf_vcpu_run_return ret = { |
| 305 | .code = HF_VCPU_RUN_WAKE_UP, |
| 306 | .wake_up.vm_id = target_vm->id, |
| 307 | .wake_up.vcpu = target_vcpu - target_vm->vcpus, |
| 308 | }; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 309 | *next = api_switch_to_primary(current, ret, VCPU_STATE_READY); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | out: |
| 313 | /* Either way, make it pending. */ |
| 314 | target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask; |
| 315 | |
| 316 | sl_unlock(&target_vcpu->lock); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 317 | |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | /** |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 322 | * Prepares the vcpu to run by updating its state and fetching whether a return |
| 323 | * value needs to be forced onto the vCPU. |
| 324 | */ |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 325 | static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu, |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 326 | struct hf_vcpu_run_return *run_ret) |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 327 | { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 328 | bool need_vm_lock; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 329 | bool ret; |
| 330 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 331 | /* |
| 332 | * Wait until the registers become available. All locks must be |
| 333 | * released between iterations of this loop to avoid potential deadlocks |
| 334 | * if, on any path, a lock needs to be taken after taking the decision |
| 335 | * to switch context but before the registers have been saved. |
| 336 | * |
| 337 | * The VM lock is not needed in the common case so it must only be taken |
| 338 | * when it is going to be needed. This ensures there are no inter-vCPU |
| 339 | * dependencies in the common run case meaning the sensitive context |
| 340 | * switch performance is consistent. |
| 341 | */ |
| 342 | for (;;) { |
| 343 | sl_lock(&vcpu->lock); |
| 344 | |
| 345 | /* The VM needs to be locked to deliver mailbox messages. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 346 | need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 347 | if (need_vm_lock) { |
| 348 | sl_unlock(&vcpu->lock); |
| 349 | sl_lock(&vcpu->vm->lock); |
| 350 | sl_lock(&vcpu->lock); |
| 351 | } |
| 352 | |
| 353 | if (vcpu->regs_available) { |
| 354 | break; |
| 355 | } |
| 356 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 357 | if (vcpu->state == VCPU_STATE_RUNNING) { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 358 | /* |
| 359 | * vCPU is running on another pCPU. |
| 360 | * |
| 361 | * It's ok to not return the sleep duration here because |
| 362 | * the other physical CPU that is currently running this |
| 363 | * vCPU will return sleep duration if neeed. The default |
| 364 | * return value is HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no |
| 365 | * need to set it explicitly. |
| 366 | */ |
| 367 | ret = false; |
| 368 | goto out; |
| 369 | } |
| 370 | |
| 371 | sl_unlock(&vcpu->lock); |
| 372 | if (need_vm_lock) { |
| 373 | sl_unlock(&vcpu->vm->lock); |
| 374 | } |
| 375 | } |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 376 | |
| 377 | if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 378 | if (vcpu->state != VCPU_STATE_ABORTED) { |
Andrew Scull | 8233128 | 2019-01-25 10:29:34 +0000 | [diff] [blame] | 379 | dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id, |
| 380 | vcpu_index(vcpu)); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 381 | vcpu->state = VCPU_STATE_ABORTED; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 382 | } |
| 383 | ret = false; |
| 384 | goto out; |
| 385 | } |
| 386 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 387 | switch (vcpu->state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 388 | case VCPU_STATE_RUNNING: |
| 389 | case VCPU_STATE_OFF: |
| 390 | case VCPU_STATE_ABORTED: |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 391 | ret = false; |
| 392 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 393 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 394 | case VCPU_STATE_BLOCKED_MAILBOX: |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 395 | /* |
| 396 | * A pending message allows the vCPU to run so the message can |
| 397 | * be delivered directly. |
| 398 | */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 399 | if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) { |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 400 | arch_regs_set_retval(&vcpu->regs, SPCI_SUCCESS); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 401 | vcpu->vm->mailbox.state = MAILBOX_STATE_READ; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | /* Fall through. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 405 | case VCPU_STATE_BLOCKED_INTERRUPT: |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 406 | /* Allow virtual interrupts to be delivered. */ |
| 407 | if (vcpu->interrupts.enabled_and_pending_count > 0) { |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | /* The timer expired so allow the interrupt to be delivered. */ |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 412 | if (arch_timer_pending(&vcpu->regs)) { |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * The vCPU is not ready to run, return the appropriate code to |
| 418 | * the primary which called vcpu_run. |
| 419 | */ |
| 420 | if (arch_timer_enabled(&vcpu->regs)) { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 421 | run_ret->code = |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 422 | vcpu->state == VCPU_STATE_BLOCKED_MAILBOX |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 423 | ? HF_VCPU_RUN_WAIT_FOR_MESSAGE |
| 424 | : HF_VCPU_RUN_WAIT_FOR_INTERRUPT; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 425 | run_ret->sleep.ns = |
| 426 | arch_timer_remaining_ns(&vcpu->regs); |
| 427 | } |
| 428 | |
| 429 | ret = false; |
| 430 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 431 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 432 | case VCPU_STATE_READY: |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 433 | break; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 436 | /* It has been decided that the vCPU should be run. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 437 | vcpu->cpu = current->cpu; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 438 | vcpu->state = VCPU_STATE_RUNNING; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 439 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 440 | /* |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 441 | * Mark the registers as unavailable now that we're about to reflect |
| 442 | * them onto the real registers. This will also prevent another physical |
| 443 | * CPU from trying to read these registers. |
| 444 | */ |
| 445 | vcpu->regs_available = false; |
| 446 | |
| 447 | ret = true; |
| 448 | |
| 449 | out: |
| 450 | sl_unlock(&vcpu->lock); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 451 | if (need_vm_lock) { |
| 452 | sl_unlock(&vcpu->vm->lock); |
| 453 | } |
| 454 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | /** |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 459 | * Runs the given vcpu of the given vm. |
| 460 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 461 | struct hf_vcpu_run_return api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx, |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 462 | const struct vcpu *current, |
| 463 | struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 464 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 465 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 466 | struct vcpu *vcpu; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 467 | struct hf_vcpu_run_return ret = { |
| 468 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 469 | .sleep.ns = HF_SLEEP_INDEFINITE, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 470 | }; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 471 | |
| 472 | /* Only the primary VM can switch vcpus. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 473 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 474 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 475 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 476 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 477 | /* Only secondary VM vcpus can be run. */ |
| 478 | if (vm_id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 479 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 480 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 481 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 482 | /* The requested VM must exist. */ |
| 483 | vm = vm_get(vm_id); |
| 484 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 485 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* The requested vcpu must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 489 | if (vcpu_idx >= vm->vcpu_count) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 490 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 491 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 492 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 493 | /* Update state if allowed. */ |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame^] | 494 | vcpu = vm_get_vcpu(vm, vcpu_idx); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 495 | if (!api_vcpu_prepare_run(current, vcpu, &ret)) { |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 496 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 497 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 498 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 499 | /* |
| 500 | * Inject timer interrupt if timer has expired. It's safe to access |
| 501 | * vcpu->regs here because api_vcpu_prepare_run already made sure that |
| 502 | * regs_available was true (and then set it to false) before returning |
| 503 | * true. |
| 504 | */ |
| 505 | if (arch_timer_pending(&vcpu->regs)) { |
| 506 | /* Make virtual timer interrupt pending. */ |
| 507 | internal_interrupt_inject(vm, vcpu, HF_VIRTUAL_TIMER_INTID, |
| 508 | vcpu, NULL); |
| 509 | |
| 510 | /* |
| 511 | * Set the mask bit so the hardware interrupt doesn't fire |
| 512 | * again. Ideally we wouldn't do this because it affects what |
| 513 | * the secondary vCPU sees, but if we don't then we end up with |
| 514 | * a loop of the interrupt firing each time we try to return to |
| 515 | * the secondary vCPU. |
| 516 | */ |
| 517 | arch_timer_mask(&vcpu->regs); |
| 518 | } |
| 519 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 520 | /* Switch to the vcpu. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 521 | *next = vcpu; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 522 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 523 | /* |
| 524 | * Set a placeholder return code to the scheduler. This will be |
| 525 | * overwritten when the switch back to the primary occurs. |
| 526 | */ |
| 527 | ret.code = HF_VCPU_RUN_PREEMPTED; |
| 528 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 529 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 530 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 534 | * Check that the mode indicates memory that is valid, owned and exclusive. |
| 535 | */ |
Andrew Scull | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame] | 536 | static bool api_mode_valid_owned_and_exclusive(int mode) |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 537 | { |
| 538 | return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) == |
| 539 | 0; |
| 540 | } |
| 541 | |
| 542 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 543 | * Determines the value to be returned by api_vm_configure and api_mailbox_clear |
| 544 | * after they've succeeded. If a secondary VM is running and there are waiters, |
| 545 | * it also switches back to the primary VM for it to wake waiters up. |
| 546 | */ |
| 547 | static int64_t api_waiter_result(struct vm_locked locked_vm, |
| 548 | struct vcpu *current, struct vcpu **next) |
| 549 | { |
| 550 | struct vm *vm = locked_vm.vm; |
| 551 | struct hf_vcpu_run_return ret = { |
| 552 | .code = HF_VCPU_RUN_NOTIFY_WAITERS, |
| 553 | }; |
| 554 | |
| 555 | if (list_empty(&vm->mailbox.waiter_list)) { |
| 556 | /* No waiters, nothing else to do. */ |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | if (vm->id == HF_PRIMARY_VM_ID) { |
| 561 | /* The caller is the primary VM. Tell it to wake up waiters. */ |
| 562 | return 1; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Switch back to the primary VM, informing it that there are waiters |
| 567 | * that need to be notified. |
| 568 | */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 569 | *next = api_switch_to_primary(current, ret, VCPU_STATE_READY); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 575 | * Configures the VM to send/receive data through the specified pages. The pages |
| 576 | * must not be shared. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 577 | * |
| 578 | * Returns: |
| 579 | * - -1 on failure. |
| 580 | * - 0 on success if no further action is needed. |
| 581 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 582 | * up or kick waiters. Waiters should be retrieved by calling |
| 583 | * hf_mailbox_waiter_get. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 584 | */ |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 585 | int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current, |
| 586 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 587 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 588 | struct vm *vm = current->vm; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 589 | struct vm_locked locked; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 590 | paddr_t pa_send_begin; |
| 591 | paddr_t pa_send_end; |
| 592 | paddr_t pa_recv_begin; |
| 593 | paddr_t pa_recv_end; |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 594 | int orig_send_mode; |
| 595 | int orig_recv_mode; |
| 596 | struct mpool local_page_pool; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 597 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 598 | |
| 599 | /* Fail if addresses are not page-aligned. */ |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 600 | if (!is_aligned(ipa_addr(send), PAGE_SIZE) || |
| 601 | !is_aligned(ipa_addr(recv), PAGE_SIZE)) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 602 | return -1; |
| 603 | } |
| 604 | |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 605 | /* Convert to physical addresses. */ |
| 606 | pa_send_begin = pa_from_ipa(send); |
| 607 | pa_send_end = pa_add(pa_send_begin, PAGE_SIZE); |
| 608 | |
| 609 | pa_recv_begin = pa_from_ipa(recv); |
| 610 | pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE); |
| 611 | |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 612 | /* Fail if the same page is used for the send and receive pages. */ |
| 613 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 614 | return -1; |
| 615 | } |
| 616 | |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 617 | locked = vm_lock(vm); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 618 | |
| 619 | /* We only allow these to be setup once. */ |
| 620 | if (vm->mailbox.send || vm->mailbox.recv) { |
| 621 | goto fail; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Ensure the pages are valid, owned and exclusive to the VM and that |
| 626 | * the VM has the required access to the memory. |
| 627 | */ |
| 628 | if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE), |
| 629 | &orig_send_mode) || |
| 630 | !api_mode_valid_owned_and_exclusive(orig_send_mode) || |
| 631 | (orig_send_mode & MM_MODE_R) == 0 || |
| 632 | (orig_send_mode & MM_MODE_W) == 0) { |
| 633 | goto fail; |
| 634 | } |
| 635 | |
| 636 | if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE), |
| 637 | &orig_recv_mode) || |
| 638 | !api_mode_valid_owned_and_exclusive(orig_recv_mode) || |
| 639 | (orig_recv_mode & MM_MODE_R) == 0) { |
| 640 | goto fail; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Create a local pool so any freed memory can't be used by another |
| 645 | * thread. This is to ensure the original mapping can be restored if any |
| 646 | * stage of the process fails. |
| 647 | */ |
| 648 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 649 | |
| 650 | /* Take memory ownership away from the VM and mark as shared. */ |
| 651 | if (!mm_vm_identity_map( |
| 652 | &vm->ptable, pa_send_begin, pa_send_end, |
| 653 | MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W, |
| 654 | NULL, &local_page_pool)) { |
| 655 | goto fail_free_pool; |
| 656 | } |
| 657 | |
| 658 | if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end, |
| 659 | MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R, |
| 660 | NULL, &local_page_pool)) { |
| 661 | /* TODO: partial defrag of failed range. */ |
| 662 | /* Recover any memory consumed in failed mapping. */ |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 663 | mm_vm_defrag(&vm->ptable, &local_page_pool); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 664 | goto fail_undo_send; |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 665 | } |
| 666 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 667 | /* Map the send page as read-only in the hypervisor address space. */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 668 | vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end, |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 669 | MM_MODE_R, &local_page_pool); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 670 | if (!vm->mailbox.send) { |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 671 | /* TODO: partial defrag of failed range. */ |
| 672 | /* Recover any memory consumed in failed mapping. */ |
| 673 | mm_defrag(&local_page_pool); |
| 674 | goto fail_undo_send_and_recv; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Map the receive page as writable in the hypervisor address space. On |
| 679 | * failure, unmap the send page before returning. |
| 680 | */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 681 | vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end, |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 682 | MM_MODE_W, &local_page_pool); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 683 | if (!vm->mailbox.recv) { |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 684 | /* TODO: partial defrag of failed range. */ |
| 685 | /* Recover any memory consumed in failed mapping. */ |
| 686 | mm_defrag(&local_page_pool); |
| 687 | goto fail_undo_all; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 690 | /* Tell caller about waiters, if any. */ |
| 691 | ret = api_waiter_result(locked, current, next); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 692 | goto exit; |
| 693 | |
| 694 | /* |
| 695 | * The following mappings will not require more memory than is available |
| 696 | * in the local pool. |
| 697 | */ |
| 698 | fail_undo_all: |
| 699 | vm->mailbox.send = NULL; |
Andrew Scull | da24197 | 2019-01-05 18:17:48 +0000 | [diff] [blame] | 700 | mm_unmap(pa_send_begin, pa_send_end, &local_page_pool); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 701 | |
| 702 | fail_undo_send_and_recv: |
| 703 | mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end, |
| 704 | orig_recv_mode, NULL, &local_page_pool); |
| 705 | |
| 706 | fail_undo_send: |
| 707 | mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end, |
| 708 | orig_send_mode, NULL, &local_page_pool); |
| 709 | |
| 710 | fail_free_pool: |
| 711 | mpool_fini(&local_page_pool); |
| 712 | |
| 713 | fail: |
| 714 | ret = -1; |
| 715 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 716 | exit: |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 717 | vm_unlock(&locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 718 | |
| 719 | return ret; |
| 720 | } |
| 721 | |
| 722 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 723 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 724 | * and notifies the recipient. |
Wedson Almeida Filho | 17c997f | 2019-01-09 18:50:09 +0000 | [diff] [blame] | 725 | * |
| 726 | * If the recipient's receive buffer is busy, it can optionally register the |
| 727 | * caller to be notified when the recipient's receive buffer becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 728 | */ |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 729 | int32_t api_spci_msg_send(uint32_t attributes, struct vcpu *current, |
| 730 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 731 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 732 | struct vm *from = current->vm; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 733 | struct vm *to; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 734 | struct hf_vcpu_run_return primary_ret = { |
| 735 | .code = HF_VCPU_RUN_MESSAGE, |
| 736 | }; |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 737 | struct spci_message from_msg_replica; |
| 738 | struct spci_message *to_msg; |
| 739 | const struct spci_message *from_msg; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 740 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 741 | uint32_t size; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 742 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 743 | int64_t ret; |
| 744 | bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) == |
| 745 | SPCI_MSG_SEND_NOTIFY; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 746 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 747 | /* |
| 748 | * Check that the sender has configured its send buffer. Copy the |
| 749 | * message header. If the tx mailbox at from_msg is configured (i.e. |
| 750 | * from_msg != NULL) then it can be safely accessed after releasing the |
| 751 | * lock since the tx mailbox address can only be configured once. |
| 752 | */ |
| 753 | sl_lock(&from->lock); |
| 754 | from_msg = from->mailbox.send; |
| 755 | sl_unlock(&from->lock); |
| 756 | |
| 757 | if (from_msg == NULL) { |
| 758 | return SPCI_INVALID_PARAMETERS; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 761 | /* |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 762 | * Note that the payload is not copied when the message header is. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 763 | */ |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 764 | from_msg_replica = *from_msg; |
| 765 | |
| 766 | /* Ensure source VM id corresponds to the current VM. */ |
| 767 | if (from_msg_replica.source_vm_id != from->id) { |
| 768 | return SPCI_INVALID_PARAMETERS; |
| 769 | } |
| 770 | |
| 771 | size = from_msg_replica.length; |
| 772 | /* Limit the size of transfer. */ |
Andrew Scull | 1262ac2 | 2019-04-05 12:44:26 +0100 | [diff] [blame] | 773 | if (size > SPCI_MSG_PAYLOAD_MAX) { |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 774 | return SPCI_INVALID_PARAMETERS; |
| 775 | } |
| 776 | |
| 777 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 778 | if (from_msg_replica.target_vm_id == from->id) { |
| 779 | return SPCI_INVALID_PARAMETERS; |
| 780 | } |
| 781 | |
| 782 | /* Ensure the target VM exists. */ |
| 783 | to = vm_get(from_msg_replica.target_vm_id); |
| 784 | if (to == NULL) { |
| 785 | return SPCI_INVALID_PARAMETERS; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 788 | sl_lock(&to->lock); |
| 789 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 790 | if (to->mailbox.state != MAILBOX_STATE_EMPTY || |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 791 | to->mailbox.recv == NULL) { |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 792 | /* |
| 793 | * Fail if the target isn't currently ready to receive data, |
| 794 | * setting up for notification if requested. |
| 795 | */ |
| 796 | if (notify) { |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 797 | struct wait_entry *entry = |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 798 | ¤t->vm->wait_entries |
| 799 | [from_msg_replica.target_vm_id]; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 800 | |
| 801 | /* Append waiter only if it's not there yet. */ |
| 802 | if (list_empty(&entry->wait_links)) { |
| 803 | list_append(&to->mailbox.waiter_list, |
| 804 | &entry->wait_links); |
| 805 | } |
| 806 | } |
| 807 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 808 | ret = SPCI_BUSY; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 809 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 810 | } |
| 811 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 812 | /* Copy data. */ |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 813 | to_msg = to->mailbox.recv; |
| 814 | *to_msg = from_msg_replica; |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 815 | memcpy_s(to_msg->payload, SPCI_MSG_PAYLOAD_MAX, |
| 816 | from->mailbox.send->payload, size); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 817 | primary_ret.message.vm_id = to->id; |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 818 | ret = SPCI_SUCCESS; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 819 | |
| 820 | /* Messages for the primary VM are delivered directly. */ |
| 821 | if (to->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 822 | to->mailbox.state = MAILBOX_STATE_READ; |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 823 | *next = api_switch_to_primary(current, primary_ret, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 824 | VCPU_STATE_READY); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 825 | goto out; |
| 826 | } |
| 827 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 828 | to->mailbox.state = MAILBOX_STATE_RECEIVED; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 829 | |
| 830 | /* Return to the primary VM directly or with a switch. */ |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 831 | if (from->id != HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 832 | *next = api_switch_to_primary(current, primary_ret, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 833 | VCPU_STATE_READY); |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 834 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 835 | |
| 836 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 837 | sl_unlock(&to->lock); |
| 838 | |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 839 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 843 | * Receives a message from the mailbox. If one isn't available, this function |
| 844 | * can optionally block the caller until one becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 845 | * |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 846 | * No new messages can be received until the mailbox has been cleared. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 847 | */ |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 848 | int32_t api_spci_msg_recv(uint32_t attributes, struct vcpu *current, |
| 849 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 850 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 851 | struct vm *vm = current->vm; |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 852 | int32_t return_code; |
| 853 | bool block = |
| 854 | (attributes & SPCI_MSG_RECV_BLOCK_MASK) == SPCI_MSG_RECV_BLOCK; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 855 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 856 | /* |
| 857 | * The primary VM will receive messages as a status code from running |
| 858 | * vcpus and must not call this function. |
| 859 | */ |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 860 | if (vm->id == HF_PRIMARY_VM_ID) { |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 861 | return SPCI_INTERRUPTED; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | sl_lock(&vm->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 865 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 866 | /* Return pending messages without blocking. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 867 | if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) { |
| 868 | vm->mailbox.state = MAILBOX_STATE_READ; |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 869 | return_code = SPCI_SUCCESS; |
| 870 | goto out; |
| 871 | } |
| 872 | |
| 873 | /* No pending message so fail if not allowed to block. */ |
| 874 | if (!block) { |
| 875 | return_code = SPCI_RETRY; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 876 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 877 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 878 | |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 879 | /* |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 880 | * From this point onward this call can only be interrupted or a message |
| 881 | * received. If a message is received the return value will be set at |
| 882 | * that time to SPCI_SUCCESS. |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 883 | */ |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 884 | return_code = SPCI_INTERRUPTED; |
| 885 | |
| 886 | /* |
| 887 | * Don't block if there are enabled and pending interrupts, to match |
| 888 | * behaviour of wait_for_interrupt. |
| 889 | */ |
| 890 | if (current->interrupts.enabled_and_pending_count > 0) { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 891 | goto out; |
| 892 | } |
| 893 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 894 | /* Switch back to primary vm to block. */ |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 895 | { |
| 896 | struct hf_vcpu_run_return run_return = { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 897 | .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE, |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 898 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 899 | |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 900 | *next = api_switch_to_primary(current, run_return, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 901 | VCPU_STATE_BLOCKED_MAILBOX); |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 902 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 903 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 904 | sl_unlock(&vm->lock); |
| 905 | |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 906 | return return_code; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 910 | * Retrieves the next VM whose mailbox became writable. For a VM to be notified |
| 911 | * by this function, the caller must have called api_mailbox_send before with |
| 912 | * the notify argument set to true, and this call must have failed because the |
| 913 | * mailbox was not available. |
| 914 | * |
| 915 | * It should be called repeatedly to retrieve a list of VMs. |
| 916 | * |
| 917 | * Returns -1 if no VM became writable, or the id of the VM whose mailbox |
| 918 | * became writable. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 919 | */ |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 920 | int64_t api_mailbox_writable_get(const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 921 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 922 | struct vm *vm = current->vm; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 923 | struct wait_entry *entry; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 924 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 925 | |
| 926 | sl_lock(&vm->lock); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 927 | if (list_empty(&vm->mailbox.ready_list)) { |
| 928 | ret = -1; |
| 929 | goto exit; |
| 930 | } |
| 931 | |
| 932 | entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry, |
| 933 | ready_links); |
| 934 | list_remove(&entry->ready_links); |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 935 | ret = entry - vm->wait_entries; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 936 | |
| 937 | exit: |
| 938 | sl_unlock(&vm->lock); |
| 939 | return ret; |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * Retrieves the next VM waiting to be notified that the mailbox of the |
| 944 | * specified VM became writable. Only primary VMs are allowed to call this. |
| 945 | * |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 946 | * Returns -1 on failure or if there are no waiters; the VM id of the next |
| 947 | * waiter otherwise. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 948 | */ |
| 949 | int64_t api_mailbox_waiter_get(uint32_t vm_id, const struct vcpu *current) |
| 950 | { |
| 951 | struct vm *vm; |
| 952 | struct vm_locked locked; |
| 953 | struct wait_entry *entry; |
| 954 | struct vm *waiting_vm; |
| 955 | |
| 956 | /* Only primary VMs are allowed to call this function. */ |
| 957 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
| 958 | return -1; |
| 959 | } |
| 960 | |
| 961 | vm = vm_get(vm_id); |
| 962 | if (vm == NULL) { |
| 963 | return -1; |
| 964 | } |
| 965 | |
| 966 | /* Check if there are outstanding notifications from given vm. */ |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 967 | locked = vm_lock(vm); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 968 | entry = api_fetch_waiter(locked); |
| 969 | vm_unlock(&locked); |
| 970 | |
| 971 | if (entry == NULL) { |
| 972 | return -1; |
| 973 | } |
| 974 | |
| 975 | /* Enqueue notification to waiting VM. */ |
| 976 | waiting_vm = entry->waiting_vm; |
| 977 | |
| 978 | sl_lock(&waiting_vm->lock); |
| 979 | if (list_empty(&entry->ready_links)) { |
| 980 | list_append(&waiting_vm->mailbox.ready_list, |
| 981 | &entry->ready_links); |
| 982 | } |
| 983 | sl_unlock(&waiting_vm->lock); |
| 984 | |
| 985 | return waiting_vm->id; |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * Clears the caller's mailbox so that a new message can be received. The caller |
| 990 | * must have copied out all data they wish to preserve as new messages will |
| 991 | * overwrite the old and will arrive asynchronously. |
| 992 | * |
| 993 | * Returns: |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 994 | * - -1 on failure, if the mailbox hasn't been read. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 995 | * - 0 on success if no further action is needed. |
| 996 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 997 | * up or kick waiters. Waiters should be retrieved by calling |
| 998 | * hf_mailbox_waiter_get. |
| 999 | */ |
| 1000 | int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next) |
| 1001 | { |
| 1002 | struct vm *vm = current->vm; |
| 1003 | struct vm_locked locked; |
| 1004 | int64_t ret; |
| 1005 | |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 1006 | locked = vm_lock(vm); |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1007 | switch (vm->mailbox.state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1008 | case MAILBOX_STATE_EMPTY: |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1009 | ret = 0; |
| 1010 | break; |
| 1011 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1012 | case MAILBOX_STATE_RECEIVED: |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1013 | ret = -1; |
| 1014 | break; |
| 1015 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1016 | case MAILBOX_STATE_READ: |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1017 | ret = api_waiter_result(locked, current, next); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1018 | vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1019 | break; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1020 | } |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1021 | vm_unlock(&locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1022 | |
| 1023 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1024 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1025 | |
| 1026 | /** |
| 1027 | * Enables or disables a given interrupt ID for the calling vCPU. |
| 1028 | * |
| 1029 | * Returns 0 on success, or -1 if the intid is invalid. |
| 1030 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 1031 | int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1032 | { |
| 1033 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
| 1034 | uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1035 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1036 | if (intid >= HF_NUM_INTIDS) { |
| 1037 | return -1; |
| 1038 | } |
| 1039 | |
| 1040 | sl_lock(¤t->lock); |
| 1041 | if (enable) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1042 | /* |
| 1043 | * If it is pending and was not enabled before, increment the |
| 1044 | * count. |
| 1045 | */ |
| 1046 | if (current->interrupts.interrupt_pending[intid_index] & |
| 1047 | ~current->interrupts.interrupt_enabled[intid_index] & |
| 1048 | intid_mask) { |
| 1049 | current->interrupts.enabled_and_pending_count++; |
| 1050 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1051 | current->interrupts.interrupt_enabled[intid_index] |= |
| 1052 | intid_mask; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1053 | } else { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1054 | /* |
| 1055 | * If it is pending and was enabled before, decrement the count. |
| 1056 | */ |
| 1057 | if (current->interrupts.interrupt_pending[intid_index] & |
| 1058 | current->interrupts.interrupt_enabled[intid_index] & |
| 1059 | intid_mask) { |
| 1060 | current->interrupts.enabled_and_pending_count--; |
| 1061 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1062 | current->interrupts.interrupt_enabled[intid_index] &= |
| 1063 | ~intid_mask; |
| 1064 | } |
| 1065 | |
| 1066 | sl_unlock(¤t->lock); |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * Returns the ID of the next pending interrupt for the calling vCPU, and |
| 1072 | * acknowledges it (i.e. marks it as no longer pending). Returns |
| 1073 | * HF_INVALID_INTID if there are no pending interrupts. |
| 1074 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 1075 | uint32_t api_interrupt_get(struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1076 | { |
| 1077 | uint8_t i; |
| 1078 | uint32_t first_interrupt = HF_INVALID_INTID; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* |
| 1081 | * Find the first enabled and pending interrupt ID, return it, and |
| 1082 | * deactivate it. |
| 1083 | */ |
| 1084 | sl_lock(¤t->lock); |
| 1085 | for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) { |
| 1086 | uint32_t enabled_and_pending = |
| 1087 | current->interrupts.interrupt_enabled[i] & |
| 1088 | current->interrupts.interrupt_pending[i]; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1089 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1090 | if (enabled_and_pending != 0) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1091 | uint8_t bit_index = ctz(enabled_and_pending); |
| 1092 | /* |
| 1093 | * Mark it as no longer pending and decrement the count. |
| 1094 | */ |
| 1095 | current->interrupts.interrupt_pending[i] &= |
| 1096 | ~(1u << bit_index); |
| 1097 | current->interrupts.enabled_and_pending_count--; |
| 1098 | first_interrupt = |
| 1099 | i * INTERRUPT_REGISTER_BITS + bit_index; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1100 | break; |
| 1101 | } |
| 1102 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1103 | |
| 1104 | sl_unlock(¤t->lock); |
| 1105 | return first_interrupt; |
| 1106 | } |
| 1107 | |
| 1108 | /** |
Andrew Walbran | 4cf217a | 2018-12-14 15:24:50 +0000 | [diff] [blame] | 1109 | * Returns whether the current vCPU is allowed to inject an interrupt into the |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1110 | * given VM and vCPU. |
| 1111 | */ |
| 1112 | static inline bool is_injection_allowed(uint32_t target_vm_id, |
| 1113 | struct vcpu *current) |
| 1114 | { |
| 1115 | uint32_t current_vm_id = current->vm->id; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1116 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1117 | /* |
| 1118 | * The primary VM is allowed to inject interrupts into any VM. Secondary |
| 1119 | * VMs are only allowed to inject interrupts into their own vCPUs. |
| 1120 | */ |
| 1121 | return current_vm_id == HF_PRIMARY_VM_ID || |
| 1122 | current_vm_id == target_vm_id; |
| 1123 | } |
| 1124 | |
| 1125 | /** |
| 1126 | * Injects a virtual interrupt of the given ID into the given target vCPU. |
| 1127 | * This doesn't cause the vCPU to actually be run immediately; it will be taken |
| 1128 | * when the vCPU is next run, which is up to the scheduler. |
| 1129 | * |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1130 | * Returns: |
| 1131 | * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt |
| 1132 | * ID is invalid, or the current VM is not allowed to inject interrupts to |
| 1133 | * the target VM. |
| 1134 | * - 0 on success if no further action is needed. |
| 1135 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 1136 | * up or kick the target vCPU. |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1137 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 1138 | int64_t api_interrupt_inject(uint32_t target_vm_id, uint32_t target_vcpu_idx, |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1139 | uint32_t intid, struct vcpu *current, |
| 1140 | struct vcpu **next) |
| 1141 | { |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1142 | struct vcpu *target_vcpu; |
| 1143 | struct vm *target_vm = vm_get(target_vm_id); |
| 1144 | |
| 1145 | if (intid >= HF_NUM_INTIDS) { |
| 1146 | return -1; |
| 1147 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1148 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1149 | if (target_vm == NULL) { |
| 1150 | return -1; |
| 1151 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1152 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1153 | if (target_vcpu_idx >= target_vm->vcpu_count) { |
| 1154 | /* The requested vcpu must exist. */ |
| 1155 | return -1; |
| 1156 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1157 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1158 | if (!is_injection_allowed(target_vm_id, current)) { |
| 1159 | return -1; |
| 1160 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1161 | |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame^] | 1162 | target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1163 | |
| 1164 | dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid, |
| 1165 | target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1166 | return internal_interrupt_inject(target_vm, target_vcpu, intid, current, |
| 1167 | next); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1168 | } |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1169 | |
| 1170 | /** |
| 1171 | * Clears a region of physical memory by overwriting it with zeros. The data is |
| 1172 | * flushed from the cache so the memory has been cleared across the system. |
| 1173 | */ |
| 1174 | static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool) |
| 1175 | { |
| 1176 | /* |
| 1177 | * TODO: change this to a cpu local single page window rather than a |
| 1178 | * global mapping of the whole range. Such an approach will limit |
| 1179 | * the changes to stage-1 tables and will allow only local |
| 1180 | * invalidation. |
| 1181 | */ |
| 1182 | void *ptr = mm_identity_map(begin, end, MM_MODE_W, ppool); |
Andrew Walbran | 2cb4339 | 2019-04-17 12:52:45 +0100 | [diff] [blame] | 1183 | size_t size = pa_difference(begin, end); |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1184 | |
| 1185 | if (!ptr) { |
| 1186 | /* TODO: partial defrag of failed range. */ |
| 1187 | /* Recover any memory consumed in failed mapping. */ |
| 1188 | mm_defrag(ppool); |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 1192 | memset_s(ptr, size, 0, size); |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1193 | arch_mm_write_back_dcache(ptr, size); |
| 1194 | mm_unmap(begin, end, ppool); |
| 1195 | |
| 1196 | return true; |
| 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * Shares memory from the calling VM with another. The memory can be shared in |
| 1201 | * different modes. |
| 1202 | * |
| 1203 | * TODO: the interface for sharing memory will need to be enhanced to allow |
| 1204 | * sharing with different modes e.g. read-only, informing the recipient |
| 1205 | * of the memory they have been given, opting to not wipe the memory and |
| 1206 | * possibly allowing multiple blocks to be transferred. What this will |
| 1207 | * look like is TBD. |
| 1208 | */ |
| 1209 | int64_t api_share_memory(uint32_t vm_id, ipaddr_t addr, size_t size, |
| 1210 | enum hf_share share, struct vcpu *current) |
| 1211 | { |
| 1212 | struct vm *from = current->vm; |
| 1213 | struct vm *to; |
| 1214 | int orig_from_mode; |
| 1215 | int from_mode; |
| 1216 | int to_mode; |
| 1217 | ipaddr_t begin; |
| 1218 | ipaddr_t end; |
| 1219 | paddr_t pa_begin; |
| 1220 | paddr_t pa_end; |
| 1221 | struct mpool local_page_pool; |
| 1222 | int64_t ret; |
| 1223 | |
| 1224 | /* Disallow reflexive shares as this suggests an error in the VM. */ |
| 1225 | if (vm_id == from->id) { |
| 1226 | return -1; |
| 1227 | } |
| 1228 | |
| 1229 | /* Ensure the target VM exists. */ |
| 1230 | to = vm_get(vm_id); |
| 1231 | if (to == NULL) { |
| 1232 | return -1; |
| 1233 | } |
| 1234 | |
| 1235 | begin = addr; |
| 1236 | end = ipa_add(addr, size); |
| 1237 | |
| 1238 | /* Fail if addresses are not page-aligned. */ |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 1239 | if (!is_aligned(ipa_addr(begin), PAGE_SIZE) || |
| 1240 | !is_aligned(ipa_addr(end), PAGE_SIZE)) { |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1241 | return -1; |
| 1242 | } |
| 1243 | |
| 1244 | /* Convert the sharing request to memory management modes. */ |
| 1245 | switch (share) { |
| 1246 | case HF_MEMORY_GIVE: |
| 1247 | from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED; |
| 1248 | to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X; |
| 1249 | break; |
| 1250 | |
| 1251 | case HF_MEMORY_LEND: |
| 1252 | from_mode = MM_MODE_INVALID; |
| 1253 | to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED; |
| 1254 | break; |
| 1255 | |
| 1256 | case HF_MEMORY_SHARE: |
| 1257 | from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED; |
| 1258 | to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED | |
| 1259 | MM_MODE_SHARED; |
| 1260 | break; |
| 1261 | |
| 1262 | default: |
| 1263 | /* The input is untrusted so might not be a valid value. */ |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Create a local pool so any freed memory can't be used by another |
| 1269 | * thread. This is to ensure the original mapping can be restored if any |
| 1270 | * stage of the process fails. |
| 1271 | */ |
| 1272 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 1273 | |
| 1274 | sl_lock_both(&from->lock, &to->lock); |
| 1275 | |
| 1276 | /* |
| 1277 | * Ensure that the memory range is mapped with the same mode so that |
| 1278 | * changes can be reverted if the process fails. |
| 1279 | */ |
| 1280 | if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) { |
| 1281 | goto fail; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Ensure the memory range is valid for the sender. If it isn't, the |
| 1286 | * sender has either shared it with another VM already or has no claim |
| 1287 | * to the memory. |
| 1288 | */ |
| 1289 | if (orig_from_mode & MM_MODE_INVALID) { |
| 1290 | goto fail; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * The sender must own the memory and have exclusive access to it in |
| 1295 | * order to share it. Alternatively, it is giving memory back to the |
| 1296 | * owning VM. |
| 1297 | */ |
| 1298 | if (orig_from_mode & MM_MODE_UNOWNED) { |
| 1299 | int orig_to_mode; |
| 1300 | |
| 1301 | if (share != HF_MEMORY_GIVE || |
| 1302 | !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) || |
| 1303 | orig_to_mode & MM_MODE_UNOWNED) { |
| 1304 | goto fail; |
| 1305 | } |
| 1306 | } else if (orig_from_mode & MM_MODE_SHARED) { |
| 1307 | goto fail; |
| 1308 | } |
| 1309 | |
| 1310 | pa_begin = pa_from_ipa(begin); |
| 1311 | pa_end = pa_from_ipa(end); |
| 1312 | |
| 1313 | /* |
| 1314 | * First update the mapping for the sender so there is not overlap with |
| 1315 | * the recipient. |
| 1316 | */ |
| 1317 | if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode, |
| 1318 | NULL, &local_page_pool)) { |
| 1319 | goto fail; |
| 1320 | } |
| 1321 | |
| 1322 | /* Clear the memory so no VM or device can see the previous contents. */ |
| 1323 | if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) { |
| 1324 | goto fail_return_to_sender; |
| 1325 | } |
| 1326 | |
| 1327 | /* Complete the transfer by mapping the memory into the recipient. */ |
| 1328 | if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL, |
| 1329 | &local_page_pool)) { |
| 1330 | /* TODO: partial defrag of failed range. */ |
| 1331 | /* Recover any memory consumed in failed mapping. */ |
| 1332 | mm_vm_defrag(&from->ptable, &local_page_pool); |
| 1333 | goto fail_return_to_sender; |
| 1334 | } |
| 1335 | |
| 1336 | ret = 0; |
| 1337 | goto out; |
| 1338 | |
| 1339 | fail_return_to_sender: |
| 1340 | mm_vm_identity_map(&from->ptable, pa_begin, pa_end, orig_from_mode, |
| 1341 | NULL, &local_page_pool); |
| 1342 | |
| 1343 | fail: |
| 1344 | ret = -1; |
| 1345 | |
| 1346 | out: |
| 1347 | sl_unlock(&from->lock); |
| 1348 | sl_unlock(&to->lock); |
| 1349 | |
| 1350 | mpool_fini(&local_page_pool); |
| 1351 | |
| 1352 | return ret; |
| 1353 | } |