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