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 | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 22 | #include "hf/check.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" |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 25 | #include "hf/plat/console.h" |
Jose Marinho | 40d55f3 | 2019-07-01 15:41:54 +0100 | [diff] [blame] | 26 | #include "hf/spci_internal.h" |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 27 | #include "hf/spinlock.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 28 | #include "hf/static_assert.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 29 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 30 | #include "hf/vm.h" |
| 31 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 32 | #include "vmapi/hf/call.h" |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 33 | #include "vmapi/hf/spci.h" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 34 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 35 | /* |
| 36 | * To eliminate the risk of deadlocks, we define a partial order for the |
| 37 | * acquisition of locks held concurrently by the same physical CPU. Our current |
| 38 | * ordering requirements are as follows: |
| 39 | * |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 40 | * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 41 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 42 | * Locks of the same kind require the lock of lowest address to be locked first, |
| 43 | * see `sl_lock_both()`. |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 44 | */ |
| 45 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 46 | static_assert(HF_MAILBOX_SIZE == PAGE_SIZE, |
Andrew Scull | 13652af | 2018-09-17 14:49:08 +0100 | [diff] [blame] | 47 | "Currently, a page is mapped for the send and receive buffers so " |
| 48 | "the maximum request is the size of a page."); |
| 49 | |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 50 | static struct mpool api_page_pool; |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 51 | |
| 52 | /** |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 53 | * Initialises the API page pool by taking ownership of the contents of the |
| 54 | * given page pool. |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 55 | */ |
| 56 | void api_init(struct mpool *ppool) |
| 57 | { |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 58 | mpool_init_from(&api_page_pool, ppool); |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 61 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 62 | * 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] | 63 | * |
| 64 | * This triggers the scheduling logic to run. Run in the context of secondary VM |
Andrew Walbran | f0c314d | 2019-10-02 14:24:26 +0100 | [diff] [blame] | 65 | * to cause SPCI_RUN to return and the primary VM to regain control of the CPU. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 66 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 67 | static struct vcpu *api_switch_to_primary(struct vcpu *current, |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 68 | struct spci_value primary_ret, |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 69 | enum vcpu_state secondary_state) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 70 | { |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 71 | struct vm *primary = vm_find(HF_PRIMARY_VM_ID); |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 72 | struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu)); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 73 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 74 | /* |
| 75 | * If the secondary is blocked but has a timer running, sleep until the |
| 76 | * timer fires rather than indefinitely. |
| 77 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 78 | switch (primary_ret.func) { |
| 79 | case HF_SPCI_RUN_WAIT_FOR_INTERRUPT: |
| 80 | case SPCI_MSG_WAIT_32: { |
| 81 | if (arch_timer_enabled_current()) { |
| 82 | uint64_t remaining_ns = |
| 83 | arch_timer_remaining_ns_current(); |
| 84 | |
| 85 | if (remaining_ns == 0) { |
| 86 | /* |
| 87 | * Timer is pending, so the current vCPU should |
| 88 | * be run again right away. |
| 89 | */ |
| 90 | primary_ret.func = SPCI_INTERRUPT_32; |
| 91 | /* |
| 92 | * primary_ret.arg1 should already be set to the |
| 93 | * current VM ID and vCPU ID. |
| 94 | */ |
| 95 | primary_ret.arg2 = 0; |
| 96 | } else { |
| 97 | primary_ret.arg2 = remaining_ns; |
| 98 | } |
| 99 | } else { |
| 100 | primary_ret.arg2 = SPCI_SLEEP_INDEFINITE; |
| 101 | } |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 102 | break; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 103 | } |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 104 | |
| 105 | default: |
| 106 | /* Do nothing. */ |
| 107 | break; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 110 | /* Set the return value for the primary VM's call to HF_VCPU_RUN. */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 111 | arch_regs_set_retval(&next->regs, primary_ret); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 112 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 113 | /* Mark the current vcpu as waiting. */ |
| 114 | sl_lock(¤t->lock); |
| 115 | current->state = secondary_state; |
| 116 | sl_unlock(¤t->lock); |
| 117 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 118 | return next; |
| 119 | } |
| 120 | |
| 121 | /** |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 122 | * Returns to the primary vm and signals that the vcpu still has work to do so. |
| 123 | */ |
| 124 | struct vcpu *api_preempt(struct vcpu *current) |
| 125 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 126 | struct spci_value ret = { |
| 127 | .func = SPCI_INTERRUPT_32, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame^] | 128 | .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 131 | return api_switch_to_primary(current, ret, VCPU_STATE_READY); |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 135 | * Puts the current vcpu in wait for interrupt mode, and returns to the primary |
| 136 | * vm. |
| 137 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 138 | struct vcpu *api_wait_for_interrupt(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 139 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 140 | struct spci_value ret = { |
| 141 | .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame^] | 142 | .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 143 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 144 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 145 | return api_switch_to_primary(current, ret, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 146 | VCPU_STATE_BLOCKED_INTERRUPT); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 150 | * Puts the current vCPU in off mode, and returns to the primary VM. |
| 151 | */ |
| 152 | struct vcpu *api_vcpu_off(struct vcpu *current) |
| 153 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 154 | struct spci_value ret = { |
| 155 | .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame^] | 156 | .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /* |
| 160 | * Disable the timer, so the scheduler doesn't get told to call back |
| 161 | * based on it. |
| 162 | */ |
| 163 | arch_timer_disable_current(); |
| 164 | |
| 165 | return api_switch_to_primary(current, ret, VCPU_STATE_OFF); |
| 166 | } |
| 167 | |
| 168 | /** |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 169 | * Returns to the primary vm to allow this cpu to be used for other tasks as the |
| 170 | * vcpu does not have work to do at this moment. The current vcpu is marked as |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 171 | * ready to be scheduled again. |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 172 | */ |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 173 | void api_yield(struct vcpu *current, struct vcpu **next) |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 174 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 175 | struct spci_value primary_ret = { |
| 176 | .func = SPCI_YIELD_32, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame^] | 177 | .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 181 | /* Noop on the primary as it makes the scheduling decisions. */ |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 182 | return; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 185 | *next = api_switch_to_primary(current, primary_ret, VCPU_STATE_READY); |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 189 | * Switches to the primary so that it can switch to the target, or kick it if it |
| 190 | * is already running on a different physical CPU. |
| 191 | */ |
| 192 | struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu) |
| 193 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 194 | struct spci_value ret = { |
| 195 | .func = HF_SPCI_RUN_WAKE_UP, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame^] | 196 | .arg1 = spci_vm_vcpu(target_vcpu->vm->id, |
| 197 | vcpu_index(target_vcpu)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 198 | }; |
| 199 | return api_switch_to_primary(current, ret, VCPU_STATE_READY); |
| 200 | } |
| 201 | |
| 202 | /** |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 203 | * Aborts the vCPU and triggers its VM to abort fully. |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 204 | */ |
| 205 | struct vcpu *api_abort(struct vcpu *current) |
| 206 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 207 | struct spci_value ret = spci_error(SPCI_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 208 | |
| 209 | dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current)); |
| 210 | |
| 211 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 212 | /* TODO: what to do when the primary aborts? */ |
| 213 | for (;;) { |
| 214 | /* Do nothing. */ |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | atomic_store_explicit(¤t->vm->aborting, true, |
| 219 | memory_order_relaxed); |
| 220 | |
| 221 | /* TODO: free resources once all vCPUs abort. */ |
| 222 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 223 | return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 227 | * Returns the ID of the VM. |
| 228 | */ |
Andrew Walbran | d230f66 | 2019-10-07 18:03:36 +0100 | [diff] [blame] | 229 | struct spci_value api_spci_id_get(const struct vcpu *current) |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 230 | { |
Andrew Walbran | d230f66 | 2019-10-07 18:03:36 +0100 | [diff] [blame] | 231 | return (struct spci_value){.func = SPCI_SUCCESS_32, |
| 232 | .arg2 = current->vm->id}; |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 236 | * Returns the number of VMs configured to run. |
| 237 | */ |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 238 | spci_vm_count_t api_vm_get_count(void) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 239 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 240 | return vm_get_count(); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 244 | * Returns the number of vCPUs configured in the given VM, or 0 if there is no |
| 245 | * such VM or the caller is not the primary VM. |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 246 | */ |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 247 | spci_vcpu_count_t api_vcpu_get_count(spci_vm_id_t vm_id, |
| 248 | const struct vcpu *current) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 249 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 250 | struct vm *vm; |
| 251 | |
| 252 | /* Only the primary VM needs to know about vcpus for scheduling. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 253 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 254 | return 0; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 255 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 256 | |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 257 | vm = vm_find(vm_id); |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 258 | if (vm == NULL) { |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 259 | return 0; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | return vm->vcpu_count; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 266 | * This function is called by the architecture-specific context switching |
| 267 | * function to indicate that register state for the given vcpu has been saved |
| 268 | * and can therefore be used by other pcpus. |
| 269 | */ |
| 270 | void api_regs_state_saved(struct vcpu *vcpu) |
| 271 | { |
| 272 | sl_lock(&vcpu->lock); |
| 273 | vcpu->regs_available = true; |
| 274 | sl_unlock(&vcpu->lock); |
| 275 | } |
| 276 | |
| 277 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 278 | * Retrieves the next waiter and removes it from the wait list if the VM's |
| 279 | * mailbox is in a writable state. |
| 280 | */ |
| 281 | static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm) |
| 282 | { |
| 283 | struct wait_entry *entry; |
| 284 | struct vm *vm = locked_vm.vm; |
| 285 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 286 | if (vm->mailbox.state != MAILBOX_STATE_EMPTY || |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 287 | vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) { |
| 288 | /* The mailbox is not writable or there are no waiters. */ |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | /* Remove waiter from the wait list. */ |
| 293 | entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry, |
| 294 | wait_links); |
| 295 | list_remove(&entry->wait_links); |
| 296 | return entry; |
| 297 | } |
| 298 | |
| 299 | /** |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 300 | * Assuming that the arguments have already been checked by the caller, injects |
| 301 | * a virtual interrupt of the given ID into the given target vCPU. This doesn't |
| 302 | * cause the vCPU to actually be run immediately; it will be taken when the vCPU |
| 303 | * is next run, which is up to the scheduler. |
| 304 | * |
| 305 | * Returns: |
| 306 | * - 0 on success if no further action is needed. |
| 307 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 308 | * up or kick the target vCPU. |
| 309 | */ |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 310 | static int64_t internal_interrupt_inject(struct vcpu *target_vcpu, |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 311 | uint32_t intid, struct vcpu *current, |
| 312 | struct vcpu **next) |
| 313 | { |
| 314 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 315 | uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 316 | int64_t ret = 0; |
| 317 | |
| 318 | sl_lock(&target_vcpu->lock); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 319 | |
| 320 | /* |
| 321 | * We only need to change state and (maybe) trigger a virtual IRQ if it |
| 322 | * is enabled and was not previously pending. Otherwise we can skip |
| 323 | * everything except setting the pending bit. |
| 324 | * |
| 325 | * If you change this logic make sure to update the need_vm_lock logic |
| 326 | * above to match. |
| 327 | */ |
| 328 | if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] & |
| 329 | ~target_vcpu->interrupts.interrupt_pending[intid_index] & |
| 330 | intid_mask)) { |
| 331 | goto out; |
| 332 | } |
| 333 | |
| 334 | /* Increment the count. */ |
| 335 | target_vcpu->interrupts.enabled_and_pending_count++; |
| 336 | |
| 337 | /* |
| 338 | * Only need to update state if there was not already an |
| 339 | * interrupt enabled and pending. |
| 340 | */ |
| 341 | if (target_vcpu->interrupts.enabled_and_pending_count != 1) { |
| 342 | goto out; |
| 343 | } |
| 344 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 345 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 346 | /* |
| 347 | * If the call came from the primary VM, let it know that it |
| 348 | * should run or kick the target vCPU. |
| 349 | */ |
| 350 | ret = 1; |
| 351 | } else if (current != target_vcpu && next != NULL) { |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 352 | *next = api_wake_up(current, target_vcpu); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | out: |
| 356 | /* Either way, make it pending. */ |
| 357 | target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask; |
| 358 | |
| 359 | sl_unlock(&target_vcpu->lock); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 360 | |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | /** |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 365 | * Constructs an SPCI_MSG_SEND value to return from a successful SPCI_MSG_POLL |
| 366 | * or SPCI_MSG_WAIT call. |
| 367 | */ |
| 368 | static struct spci_value spci_msg_recv_return(const struct vm *receiver) |
| 369 | { |
| 370 | return (struct spci_value){ |
| 371 | .func = SPCI_MSG_SEND_32, |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 372 | .arg1 = (receiver->mailbox.recv_sender << 16) | receiver->id, |
| 373 | .arg3 = receiver->mailbox.recv_size, |
| 374 | .arg4 = receiver->mailbox.recv_attributes}; |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 378 | * Prepares the vcpu to run by updating its state and fetching whether a return |
| 379 | * value needs to be forced onto the vCPU. |
| 380 | */ |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 381 | static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu, |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 382 | struct spci_value *run_ret) |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 383 | { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 384 | bool need_vm_lock; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 385 | bool ret; |
| 386 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 387 | /* |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 388 | * Wait until the registers become available. All locks must be released |
| 389 | * between iterations of this loop to avoid potential deadlocks if, on |
| 390 | * any path, a lock needs to be taken after taking the decision to |
| 391 | * switch context but before the registers have been saved. |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 392 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 393 | * The VM lock is not needed in the common case so it must only be taken |
| 394 | * when it is going to be needed. This ensures there are no inter-vCPU |
| 395 | * dependencies in the common run case meaning the sensitive context |
| 396 | * switch performance is consistent. |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 397 | */ |
| 398 | for (;;) { |
| 399 | sl_lock(&vcpu->lock); |
| 400 | |
| 401 | /* The VM needs to be locked to deliver mailbox messages. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 402 | need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 403 | if (need_vm_lock) { |
| 404 | sl_unlock(&vcpu->lock); |
| 405 | sl_lock(&vcpu->vm->lock); |
| 406 | sl_lock(&vcpu->lock); |
| 407 | } |
| 408 | |
| 409 | if (vcpu->regs_available) { |
| 410 | break; |
| 411 | } |
| 412 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 413 | if (vcpu->state == VCPU_STATE_RUNNING) { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 414 | /* |
| 415 | * vCPU is running on another pCPU. |
| 416 | * |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 417 | * It's okay not to return the sleep duration here |
| 418 | * because the other physical CPU that is currently |
| 419 | * running this vCPU will return the sleep duration if |
| 420 | * needed. |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 421 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 422 | *run_ret = spci_error(SPCI_BUSY); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 423 | ret = false; |
| 424 | goto out; |
| 425 | } |
| 426 | |
| 427 | sl_unlock(&vcpu->lock); |
| 428 | if (need_vm_lock) { |
| 429 | sl_unlock(&vcpu->vm->lock); |
| 430 | } |
| 431 | } |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 432 | |
| 433 | if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 434 | if (vcpu->state != VCPU_STATE_ABORTED) { |
Andrew Scull | 8233128 | 2019-01-25 10:29:34 +0000 | [diff] [blame] | 435 | dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id, |
| 436 | vcpu_index(vcpu)); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 437 | vcpu->state = VCPU_STATE_ABORTED; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 438 | } |
| 439 | ret = false; |
| 440 | goto out; |
| 441 | } |
| 442 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 443 | switch (vcpu->state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 444 | case VCPU_STATE_RUNNING: |
| 445 | case VCPU_STATE_OFF: |
| 446 | case VCPU_STATE_ABORTED: |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 447 | ret = false; |
| 448 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 449 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 450 | case VCPU_STATE_BLOCKED_MAILBOX: |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 451 | /* |
| 452 | * A pending message allows the vCPU to run so the message can |
| 453 | * be delivered directly. |
| 454 | */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 455 | if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 456 | arch_regs_set_retval(&vcpu->regs, |
| 457 | spci_msg_recv_return(vcpu->vm)); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 458 | vcpu->vm->mailbox.state = MAILBOX_STATE_READ; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | /* Fall through. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 462 | case VCPU_STATE_BLOCKED_INTERRUPT: |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 463 | /* Allow virtual interrupts to be delivered. */ |
| 464 | if (vcpu->interrupts.enabled_and_pending_count > 0) { |
| 465 | break; |
| 466 | } |
| 467 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 468 | if (arch_timer_enabled(&vcpu->regs)) { |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 469 | uint64_t timer_remaining_ns = |
| 470 | arch_timer_remaining_ns(&vcpu->regs); |
| 471 | |
| 472 | /* |
| 473 | * The timer expired so allow the interrupt to be |
| 474 | * delivered. |
| 475 | */ |
| 476 | if (timer_remaining_ns == 0) { |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * The vCPU is not ready to run, return the appropriate |
| 482 | * code to the primary which called vcpu_run. |
| 483 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 484 | run_ret->func = |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 485 | vcpu->state == VCPU_STATE_BLOCKED_MAILBOX |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 486 | ? SPCI_MSG_WAIT_32 |
| 487 | : HF_SPCI_RUN_WAIT_FOR_INTERRUPT; |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame^] | 488 | run_ret->arg1 = |
| 489 | spci_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu)); |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 490 | run_ret->arg2 = timer_remaining_ns; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | ret = false; |
| 494 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 495 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 496 | case VCPU_STATE_READY: |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 497 | break; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 500 | /* It has been decided that the vCPU should be run. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 501 | vcpu->cpu = current->cpu; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 502 | vcpu->state = VCPU_STATE_RUNNING; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 503 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 504 | /* |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 505 | * Mark the registers as unavailable now that we're about to reflect |
| 506 | * them onto the real registers. This will also prevent another physical |
| 507 | * CPU from trying to read these registers. |
| 508 | */ |
| 509 | vcpu->regs_available = false; |
| 510 | |
| 511 | ret = true; |
| 512 | |
| 513 | out: |
| 514 | sl_unlock(&vcpu->lock); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 515 | if (need_vm_lock) { |
| 516 | sl_unlock(&vcpu->vm->lock); |
| 517 | } |
| 518 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 519 | return ret; |
| 520 | } |
| 521 | |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 522 | struct spci_value api_spci_run(spci_vm_id_t vm_id, spci_vcpu_index_t vcpu_idx, |
| 523 | const struct vcpu *current, struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 524 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 525 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 526 | struct vcpu *vcpu; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 527 | struct spci_value ret = spci_error(SPCI_INVALID_PARAMETERS); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 528 | |
| 529 | /* Only the primary VM can switch vcpus. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 530 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 531 | ret.arg2 = SPCI_DENIED; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 532 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 533 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 534 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 535 | /* Only secondary VM vcpus can be run. */ |
| 536 | if (vm_id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 537 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 538 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 539 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 540 | /* The requested VM must exist. */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 541 | vm = vm_find(vm_id); |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 542 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 543 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* The requested vcpu must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 547 | if (vcpu_idx >= vm->vcpu_count) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 548 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 549 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 550 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 551 | /* Update state if allowed. */ |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 552 | vcpu = vm_get_vcpu(vm, vcpu_idx); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 553 | if (!api_vcpu_prepare_run(current, vcpu, &ret)) { |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 554 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 555 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 556 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 557 | /* |
| 558 | * Inject timer interrupt if timer has expired. It's safe to access |
| 559 | * vcpu->regs here because api_vcpu_prepare_run already made sure that |
| 560 | * regs_available was true (and then set it to false) before returning |
| 561 | * true. |
| 562 | */ |
| 563 | if (arch_timer_pending(&vcpu->regs)) { |
| 564 | /* Make virtual timer interrupt pending. */ |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 565 | internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu, |
| 566 | NULL); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * Set the mask bit so the hardware interrupt doesn't fire |
| 570 | * again. Ideally we wouldn't do this because it affects what |
| 571 | * the secondary vCPU sees, but if we don't then we end up with |
| 572 | * a loop of the interrupt firing each time we try to return to |
| 573 | * the secondary vCPU. |
| 574 | */ |
| 575 | arch_timer_mask(&vcpu->regs); |
| 576 | } |
| 577 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 578 | /* Switch to the vcpu. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 579 | *next = vcpu; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 580 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 581 | /* |
| 582 | * Set a placeholder return code to the scheduler. This will be |
| 583 | * overwritten when the switch back to the primary occurs. |
| 584 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 585 | ret.func = SPCI_INTERRUPT_32; |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame^] | 586 | ret.arg1 = spci_vm_vcpu(vm_id, vcpu_idx); |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 587 | ret.arg2 = 0; |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 588 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 589 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 590 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 594 | * Check that the mode indicates memory that is valid, owned and exclusive. |
| 595 | */ |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 596 | static bool api_mode_valid_owned_and_exclusive(uint32_t mode) |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 597 | { |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 598 | return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 599 | MM_MODE_SHARED)) == 0; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 603 | * Determines the value to be returned by api_vm_configure and api_mailbox_clear |
| 604 | * after they've succeeded. If a secondary VM is running and there are waiters, |
| 605 | * it also switches back to the primary VM for it to wake waiters up. |
| 606 | */ |
| 607 | static int64_t api_waiter_result(struct vm_locked locked_vm, |
| 608 | struct vcpu *current, struct vcpu **next) |
| 609 | { |
| 610 | struct vm *vm = locked_vm.vm; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 611 | struct spci_value ret = { |
| 612 | .func = SPCI_RX_RELEASE_32, |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 613 | }; |
| 614 | |
| 615 | if (list_empty(&vm->mailbox.waiter_list)) { |
| 616 | /* No waiters, nothing else to do. */ |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | if (vm->id == HF_PRIMARY_VM_ID) { |
| 621 | /* The caller is the primary VM. Tell it to wake up waiters. */ |
| 622 | return 1; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * Switch back to the primary VM, informing it that there are waiters |
| 627 | * that need to be notified. |
| 628 | */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 629 | *next = api_switch_to_primary(current, ret, VCPU_STATE_READY); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | /** |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 635 | * Configures the hypervisor's stage-1 view of the send and receive pages. The |
| 636 | * stage-1 page tables must be locked so memory cannot be taken by another core |
| 637 | * which could result in this transaction being unable to roll back in the case |
| 638 | * of an error. |
| 639 | */ |
| 640 | static bool api_vm_configure_stage1(struct vm_locked vm_locked, |
| 641 | paddr_t pa_send_begin, paddr_t pa_send_end, |
| 642 | paddr_t pa_recv_begin, paddr_t pa_recv_end, |
| 643 | struct mpool *local_page_pool) |
| 644 | { |
| 645 | bool ret; |
| 646 | struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1(); |
| 647 | |
| 648 | /* Map the send page as read-only in the hypervisor address space. */ |
| 649 | vm_locked.vm->mailbox.send = |
| 650 | mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end, |
| 651 | MM_MODE_R, local_page_pool); |
| 652 | if (!vm_locked.vm->mailbox.send) { |
| 653 | /* TODO: partial defrag of failed range. */ |
| 654 | /* Recover any memory consumed in failed mapping. */ |
| 655 | mm_defrag(mm_stage1_locked, local_page_pool); |
| 656 | goto fail; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Map the receive page as writable in the hypervisor address space. On |
| 661 | * failure, unmap the send page before returning. |
| 662 | */ |
| 663 | vm_locked.vm->mailbox.recv = |
| 664 | mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end, |
| 665 | MM_MODE_W, local_page_pool); |
| 666 | if (!vm_locked.vm->mailbox.recv) { |
| 667 | /* TODO: partial defrag of failed range. */ |
| 668 | /* Recover any memory consumed in failed mapping. */ |
| 669 | mm_defrag(mm_stage1_locked, local_page_pool); |
| 670 | goto fail_undo_send; |
| 671 | } |
| 672 | |
| 673 | ret = true; |
| 674 | goto out; |
| 675 | |
| 676 | /* |
| 677 | * The following mappings will not require more memory than is available |
| 678 | * in the local pool. |
| 679 | */ |
| 680 | fail_undo_send: |
| 681 | vm_locked.vm->mailbox.send = NULL; |
Andrew Scull | 7e8de32 | 2019-07-02 13:00:56 +0100 | [diff] [blame] | 682 | CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end, |
| 683 | local_page_pool)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 684 | |
| 685 | fail: |
| 686 | ret = false; |
| 687 | |
| 688 | out: |
| 689 | mm_unlock_stage1(&mm_stage1_locked); |
| 690 | |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Configures the send and receive pages in the VM stage-2 and hypervisor |
| 696 | * stage-1 page tables. Locking of the page tables combined with a local memory |
| 697 | * pool ensures there will always be enough memory to recover from any errors |
| 698 | * that arise. |
| 699 | */ |
| 700 | static bool api_vm_configure_pages(struct vm_locked vm_locked, |
| 701 | paddr_t pa_send_begin, paddr_t pa_send_end, |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 702 | uint32_t orig_send_mode, |
| 703 | paddr_t pa_recv_begin, paddr_t pa_recv_end, |
| 704 | uint32_t orig_recv_mode) |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 705 | { |
| 706 | bool ret; |
| 707 | struct mpool local_page_pool; |
| 708 | |
| 709 | /* |
| 710 | * Create a local pool so any freed memory can't be used by another |
| 711 | * thread. This is to ensure the original mapping can be restored if any |
| 712 | * stage of the process fails. |
| 713 | */ |
| 714 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 715 | |
| 716 | /* Take memory ownership away from the VM and mark as shared. */ |
| 717 | if (!mm_vm_identity_map( |
| 718 | &vm_locked.vm->ptable, pa_send_begin, pa_send_end, |
| 719 | MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W, |
| 720 | NULL, &local_page_pool)) { |
| 721 | goto fail; |
| 722 | } |
| 723 | |
| 724 | if (!mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin, |
| 725 | pa_recv_end, |
| 726 | MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R, |
| 727 | NULL, &local_page_pool)) { |
| 728 | /* TODO: partial defrag of failed range. */ |
| 729 | /* Recover any memory consumed in failed mapping. */ |
| 730 | mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool); |
| 731 | goto fail_undo_send; |
| 732 | } |
| 733 | |
| 734 | if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end, |
| 735 | pa_recv_begin, pa_recv_end, |
| 736 | &local_page_pool)) { |
| 737 | goto fail_undo_send_and_recv; |
| 738 | } |
| 739 | |
| 740 | ret = true; |
| 741 | goto out; |
| 742 | |
| 743 | /* |
| 744 | * The following mappings will not require more memory than is available |
| 745 | * in the local pool. |
| 746 | */ |
| 747 | fail_undo_send_and_recv: |
Andrew Scull | 7e8de32 | 2019-07-02 13:00:56 +0100 | [diff] [blame] | 748 | CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin, |
| 749 | pa_recv_end, orig_recv_mode, NULL, |
| 750 | &local_page_pool)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 751 | |
| 752 | fail_undo_send: |
Andrew Scull | 7e8de32 | 2019-07-02 13:00:56 +0100 | [diff] [blame] | 753 | CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_send_begin, |
| 754 | pa_send_end, orig_send_mode, NULL, |
| 755 | &local_page_pool)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 756 | |
| 757 | fail: |
| 758 | ret = false; |
| 759 | |
| 760 | out: |
| 761 | mpool_fini(&local_page_pool); |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 767 | * Configures the VM to send/receive data through the specified pages. The pages |
| 768 | * must not be shared. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 769 | * |
| 770 | * Returns: |
| 771 | * - -1 on failure. |
| 772 | * - 0 on success if no further action is needed. |
| 773 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 774 | * up or kick waiters. Waiters should be retrieved by calling |
| 775 | * hf_mailbox_waiter_get. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 776 | */ |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 777 | int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current, |
| 778 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 779 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 780 | struct vm *vm = current->vm; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 781 | struct vm_locked vm_locked; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 782 | paddr_t pa_send_begin; |
| 783 | paddr_t pa_send_end; |
| 784 | paddr_t pa_recv_begin; |
| 785 | paddr_t pa_recv_end; |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 786 | uint32_t orig_send_mode; |
| 787 | uint32_t orig_recv_mode; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 788 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 789 | |
| 790 | /* Fail if addresses are not page-aligned. */ |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 791 | if (!is_aligned(ipa_addr(send), PAGE_SIZE) || |
| 792 | !is_aligned(ipa_addr(recv), PAGE_SIZE)) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 793 | return -1; |
| 794 | } |
| 795 | |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 796 | /* Convert to physical addresses. */ |
| 797 | pa_send_begin = pa_from_ipa(send); |
| 798 | pa_send_end = pa_add(pa_send_begin, PAGE_SIZE); |
| 799 | |
| 800 | pa_recv_begin = pa_from_ipa(recv); |
| 801 | pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE); |
| 802 | |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 803 | /* Fail if the same page is used for the send and receive pages. */ |
| 804 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 805 | return -1; |
| 806 | } |
| 807 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 808 | /* |
| 809 | * The hypervisor's memory map must be locked for the duration of this |
| 810 | * operation to ensure there will be sufficient memory to recover from |
| 811 | * any failures. |
| 812 | * |
| 813 | * TODO: the scope of the can be reduced but will require restructuring |
| 814 | * to keep a single unlock point. |
| 815 | */ |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 816 | vm_locked = vm_lock(vm); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 817 | |
| 818 | /* We only allow these to be setup once. */ |
| 819 | if (vm->mailbox.send || vm->mailbox.recv) { |
| 820 | goto fail; |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * Ensure the pages are valid, owned and exclusive to the VM and that |
| 825 | * the VM has the required access to the memory. |
| 826 | */ |
| 827 | if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE), |
| 828 | &orig_send_mode) || |
| 829 | !api_mode_valid_owned_and_exclusive(orig_send_mode) || |
| 830 | (orig_send_mode & MM_MODE_R) == 0 || |
| 831 | (orig_send_mode & MM_MODE_W) == 0) { |
| 832 | goto fail; |
| 833 | } |
| 834 | |
| 835 | if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE), |
| 836 | &orig_recv_mode) || |
| 837 | !api_mode_valid_owned_and_exclusive(orig_recv_mode) || |
| 838 | (orig_recv_mode & MM_MODE_R) == 0) { |
| 839 | goto fail; |
| 840 | } |
| 841 | |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 842 | if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end, |
| 843 | orig_send_mode, pa_recv_begin, pa_recv_end, |
| 844 | orig_recv_mode)) { |
| 845 | goto fail; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 846 | } |
| 847 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 848 | /* Tell caller about waiters, if any. */ |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 849 | ret = api_waiter_result(vm_locked, current, next); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 850 | goto exit; |
| 851 | |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 852 | fail: |
| 853 | ret = -1; |
| 854 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 855 | exit: |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 856 | vm_unlock(&vm_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 857 | |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | /** |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 862 | * Checks whether the given `to` VM's mailbox is currently busy, and optionally |
| 863 | * registers the `from` VM to be notified when it becomes available. |
| 864 | */ |
| 865 | static bool msg_receiver_busy(struct vm_locked to, struct vm_locked from, |
| 866 | bool notify) |
| 867 | { |
| 868 | if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY || |
| 869 | to.vm->mailbox.recv == NULL) { |
| 870 | /* |
| 871 | * Fail if the receiver isn't currently ready to receive data, |
| 872 | * setting up for notification if requested. |
| 873 | */ |
| 874 | if (notify) { |
| 875 | struct wait_entry *entry = |
| 876 | &from.vm->wait_entries[to.vm->id]; |
| 877 | |
| 878 | /* Append waiter only if it's not there yet. */ |
| 879 | if (list_empty(&entry->wait_links)) { |
| 880 | list_append(&to.vm->mailbox.waiter_list, |
| 881 | &entry->wait_links); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | return true; |
| 886 | } |
| 887 | |
| 888 | return false; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * Notifies the `to` VM about the message currently in its mailbox, possibly |
| 893 | * with the help of the primary VM. |
| 894 | */ |
| 895 | static void deliver_msg(struct vm_locked to, struct vm_locked from, |
| 896 | uint32_t size, struct vcpu *current, struct vcpu **next) |
| 897 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 898 | struct spci_value primary_ret = { |
| 899 | .func = SPCI_MSG_SEND_32, |
| 900 | .arg1 = ((uint32_t)from.vm->id << 16) | to.vm->id, |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 901 | }; |
| 902 | |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 903 | /* Messages for the primary VM are delivered directly. */ |
| 904 | if (to.vm->id == HF_PRIMARY_VM_ID) { |
| 905 | /* |
| 906 | * Only tell the primary VM the size if the message is for it, |
| 907 | * to avoid leaking data about messages for other VMs. |
| 908 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 909 | primary_ret.arg3 = size; |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 910 | |
| 911 | to.vm->mailbox.state = MAILBOX_STATE_READ; |
| 912 | *next = api_switch_to_primary(current, primary_ret, |
| 913 | VCPU_STATE_READY); |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | to.vm->mailbox.state = MAILBOX_STATE_RECEIVED; |
| 918 | |
| 919 | /* Return to the primary VM directly or with a switch. */ |
| 920 | if (from.vm->id != HF_PRIMARY_VM_ID) { |
| 921 | *next = api_switch_to_primary(current, primary_ret, |
| 922 | VCPU_STATE_READY); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 927 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 928 | * and notifies the recipient. |
Wedson Almeida Filho | 17c997f | 2019-01-09 18:50:09 +0000 | [diff] [blame] | 929 | * |
| 930 | * If the recipient's receive buffer is busy, it can optionally register the |
| 931 | * 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] | 932 | */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 933 | struct spci_value api_spci_msg_send(spci_vm_id_t sender_vm_id, |
| 934 | spci_vm_id_t receiver_vm_id, uint32_t size, |
| 935 | uint32_t attributes, struct vcpu *current, |
| 936 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 937 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 938 | struct vm *from = current->vm; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 939 | struct vm *to; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 940 | |
Andrew Walbran | f659596 | 2019-10-15 16:47:56 +0100 | [diff] [blame] | 941 | struct two_vm_locked vm_to_from_lock; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 942 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 943 | const void *from_msg; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 944 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 945 | struct spci_value ret; |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 946 | bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) == |
| 947 | SPCI_MSG_SEND_NOTIFY; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 948 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 949 | /* Ensure sender VM ID corresponds to the current VM. */ |
| 950 | if (sender_vm_id != from->id) { |
| 951 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 952 | } |
| 953 | |
| 954 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 955 | if (receiver_vm_id == from->id) { |
| 956 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 957 | } |
| 958 | |
| 959 | /* Limit the size of transfer. */ |
| 960 | if (size > SPCI_MSG_PAYLOAD_MAX) { |
| 961 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 962 | } |
| 963 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 964 | /* |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 965 | * Check that the sender has configured its send buffer. If the tx |
| 966 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 967 | * be safely accessed after releasing the lock since the tx mailbox |
| 968 | * address can only be configured once. |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 969 | */ |
| 970 | sl_lock(&from->lock); |
| 971 | from_msg = from->mailbox.send; |
| 972 | sl_unlock(&from->lock); |
| 973 | |
| 974 | if (from_msg == NULL) { |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 975 | return spci_error(SPCI_INVALID_PARAMETERS); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 978 | /* Ensure the receiver VM exists. */ |
| 979 | to = vm_find(receiver_vm_id); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 980 | if (to == NULL) { |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 981 | return spci_error(SPCI_INVALID_PARAMETERS); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 982 | } |
| 983 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 984 | /* |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 985 | * Hafnium needs to hold the lock on <to> before the mailbox state is |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 986 | * checked. The lock on <to> must be held until the information is |
| 987 | * copied to <to> Rx buffer. Since in |
| 988 | * spci_msg_handle_architected_message we may call api_spci_share_memory |
| 989 | * which must hold the <from> lock, we must hold the <from> lock at this |
| 990 | * point to prevent a deadlock scenario. |
| 991 | */ |
Andrew Walbran | f659596 | 2019-10-15 16:47:56 +0100 | [diff] [blame] | 992 | vm_to_from_lock = vm_lock_both(to, from); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 993 | |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 994 | if (msg_receiver_busy(vm_to_from_lock.vm1, vm_to_from_lock.vm2, |
| 995 | notify)) { |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 996 | ret = spci_error(SPCI_BUSY); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 997 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 998 | } |
| 999 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1000 | /* Handle legacy memory sharing messages. */ |
| 1001 | if ((attributes & SPCI_MSG_SEND_LEGACY_MEMORY_MASK) == |
| 1002 | SPCI_MSG_SEND_LEGACY_MEMORY) { |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1003 | /* |
| 1004 | * Buffer holding the internal copy of the shared memory |
| 1005 | * regions. |
| 1006 | */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1007 | struct spci_architected_message_header |
| 1008 | *architected_message_replica = |
| 1009 | (struct spci_architected_message_header *) |
| 1010 | cpu_get_buffer(current->cpu->id); |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 1011 | uint32_t message_buffer_size = |
| 1012 | cpu_get_buffer_size(current->cpu->id); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1013 | |
| 1014 | struct spci_architected_message_header *architected_header = |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1015 | (struct spci_architected_message_header *)from_msg; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1016 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1017 | if (size > message_buffer_size) { |
| 1018 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1019 | goto out; |
| 1020 | } |
| 1021 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1022 | if (size < sizeof(struct spci_architected_message_header)) { |
| 1023 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1024 | goto out; |
| 1025 | } |
| 1026 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1027 | /* Copy the architected message into the internal buffer. */ |
| 1028 | memcpy_s(architected_message_replica, message_buffer_size, |
| 1029 | architected_header, size); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1030 | |
| 1031 | /* |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1032 | * Note that architected_message_replica is passed as the third |
| 1033 | * parameter to spci_msg_handle_architected_message. The |
| 1034 | * execution flow commencing at |
| 1035 | * spci_msg_handle_architected_message will make several |
| 1036 | * accesses to fields in architected_message_replica. The memory |
| 1037 | * area architected_message_replica must be exclusively owned by |
| 1038 | * Hafnium so that TOCTOU issues do not arise. |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1039 | */ |
| 1040 | ret = spci_msg_handle_architected_message( |
Andrew Walbran | f659596 | 2019-10-15 16:47:56 +0100 | [diff] [blame] | 1041 | vm_to_from_lock.vm1, vm_to_from_lock.vm2, |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1042 | architected_message_replica, size); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1043 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1044 | if (ret.func != SPCI_SUCCESS_32) { |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1045 | goto out; |
| 1046 | } |
| 1047 | } else { |
| 1048 | /* Copy data. */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1049 | memcpy_s(to->mailbox.recv, SPCI_MSG_PAYLOAD_MAX, from_msg, |
| 1050 | size); |
| 1051 | to->mailbox.recv_size = size; |
| 1052 | to->mailbox.recv_sender = sender_vm_id; |
| 1053 | to->mailbox.recv_attributes = 0; |
| 1054 | ret = (struct spci_value){.func = SPCI_SUCCESS_32}; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1055 | } |
| 1056 | |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1057 | deliver_msg(vm_to_from_lock.vm1, vm_to_from_lock.vm2, size, current, |
| 1058 | next); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1059 | |
| 1060 | out: |
Andrew Walbran | f659596 | 2019-10-15 16:47:56 +0100 | [diff] [blame] | 1061 | vm_unlock(&vm_to_from_lock.vm1); |
| 1062 | vm_unlock(&vm_to_from_lock.vm2); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1063 | |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 1064 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /** |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1068 | * Checks whether the vCPU's attempt to block for a message has already been |
| 1069 | * interrupted or whether it is allowed to block. |
| 1070 | */ |
| 1071 | bool api_spci_msg_recv_block_interrupted(struct vcpu *current) |
| 1072 | { |
| 1073 | bool interrupted; |
| 1074 | |
| 1075 | sl_lock(¤t->lock); |
| 1076 | |
| 1077 | /* |
| 1078 | * Don't block if there are enabled and pending interrupts, to match |
| 1079 | * behaviour of wait_for_interrupt. |
| 1080 | */ |
| 1081 | interrupted = (current->interrupts.enabled_and_pending_count > 0); |
| 1082 | |
| 1083 | sl_unlock(¤t->lock); |
| 1084 | |
| 1085 | return interrupted; |
| 1086 | } |
| 1087 | |
| 1088 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1089 | * Receives a message from the mailbox. If one isn't available, this function |
| 1090 | * can optionally block the caller until one becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1091 | * |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1092 | * 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] | 1093 | */ |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1094 | struct spci_value api_spci_msg_recv(bool block, struct vcpu *current, |
| 1095 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1096 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1097 | struct vm *vm = current->vm; |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1098 | struct spci_value return_code; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1099 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1100 | /* |
| 1101 | * The primary VM will receive messages as a status code from running |
| 1102 | * vcpus and must not call this function. |
| 1103 | */ |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 1104 | if (vm->id == HF_PRIMARY_VM_ID) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1105 | return spci_error(SPCI_NOT_SUPPORTED); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | sl_lock(&vm->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1109 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1110 | /* Return pending messages without blocking. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1111 | if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) { |
| 1112 | vm->mailbox.state = MAILBOX_STATE_READ; |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1113 | return_code = spci_msg_recv_return(vm); |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1114 | goto out; |
| 1115 | } |
| 1116 | |
| 1117 | /* No pending message so fail if not allowed to block. */ |
| 1118 | if (!block) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1119 | return_code = spci_error(SPCI_RETRY); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1120 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1121 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1122 | |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 1123 | /* |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1124 | * From this point onward this call can only be interrupted or a message |
| 1125 | * received. If a message is received the return value will be set at |
| 1126 | * that time to SPCI_SUCCESS. |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 1127 | */ |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1128 | return_code = spci_error(SPCI_INTERRUPTED); |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1129 | if (api_spci_msg_recv_block_interrupted(current)) { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1130 | goto out; |
| 1131 | } |
| 1132 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1133 | /* Switch back to primary vm to block. */ |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1134 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 1135 | struct spci_value run_return = { |
| 1136 | .func = SPCI_MSG_WAIT_32, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame^] | 1137 | .arg1 = spci_vm_vcpu(vm->id, vcpu_index(current)), |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1138 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1139 | |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1140 | *next = api_switch_to_primary(current, run_return, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1141 | VCPU_STATE_BLOCKED_MAILBOX); |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1142 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1143 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1144 | sl_unlock(&vm->lock); |
| 1145 | |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1146 | return return_code; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1150 | * Retrieves the next VM whose mailbox became writable. For a VM to be notified |
| 1151 | * by this function, the caller must have called api_mailbox_send before with |
| 1152 | * the notify argument set to true, and this call must have failed because the |
| 1153 | * mailbox was not available. |
| 1154 | * |
| 1155 | * It should be called repeatedly to retrieve a list of VMs. |
| 1156 | * |
| 1157 | * Returns -1 if no VM became writable, or the id of the VM whose mailbox |
| 1158 | * became writable. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1159 | */ |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1160 | int64_t api_mailbox_writable_get(const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1161 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1162 | struct vm *vm = current->vm; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1163 | struct wait_entry *entry; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 1164 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1165 | |
| 1166 | sl_lock(&vm->lock); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1167 | if (list_empty(&vm->mailbox.ready_list)) { |
| 1168 | ret = -1; |
| 1169 | goto exit; |
| 1170 | } |
| 1171 | |
| 1172 | entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry, |
| 1173 | ready_links); |
| 1174 | list_remove(&entry->ready_links); |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 1175 | ret = entry - vm->wait_entries; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1176 | |
| 1177 | exit: |
| 1178 | sl_unlock(&vm->lock); |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * Retrieves the next VM waiting to be notified that the mailbox of the |
| 1184 | * specified VM became writable. Only primary VMs are allowed to call this. |
| 1185 | * |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 1186 | * Returns -1 on failure or if there are no waiters; the VM id of the next |
| 1187 | * waiter otherwise. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1188 | */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1189 | 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] | 1190 | { |
| 1191 | struct vm *vm; |
| 1192 | struct vm_locked locked; |
| 1193 | struct wait_entry *entry; |
| 1194 | struct vm *waiting_vm; |
| 1195 | |
| 1196 | /* Only primary VMs are allowed to call this function. */ |
| 1197 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
| 1198 | return -1; |
| 1199 | } |
| 1200 | |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1201 | vm = vm_find(vm_id); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1202 | if (vm == NULL) { |
| 1203 | return -1; |
| 1204 | } |
| 1205 | |
| 1206 | /* Check if there are outstanding notifications from given vm. */ |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 1207 | locked = vm_lock(vm); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1208 | entry = api_fetch_waiter(locked); |
| 1209 | vm_unlock(&locked); |
| 1210 | |
| 1211 | if (entry == NULL) { |
| 1212 | return -1; |
| 1213 | } |
| 1214 | |
| 1215 | /* Enqueue notification to waiting VM. */ |
| 1216 | waiting_vm = entry->waiting_vm; |
| 1217 | |
| 1218 | sl_lock(&waiting_vm->lock); |
| 1219 | if (list_empty(&entry->ready_links)) { |
| 1220 | list_append(&waiting_vm->mailbox.ready_list, |
| 1221 | &entry->ready_links); |
| 1222 | } |
| 1223 | sl_unlock(&waiting_vm->lock); |
| 1224 | |
| 1225 | return waiting_vm->id; |
| 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * Clears the caller's mailbox so that a new message can be received. The caller |
| 1230 | * must have copied out all data they wish to preserve as new messages will |
| 1231 | * overwrite the old and will arrive asynchronously. |
| 1232 | * |
| 1233 | * Returns: |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1234 | * - -1 on failure, if the mailbox hasn't been read. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1235 | * - 0 on success if no further action is needed. |
| 1236 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 1237 | * up or kick waiters. Waiters should be retrieved by calling |
| 1238 | * hf_mailbox_waiter_get. |
| 1239 | */ |
| 1240 | int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next) |
| 1241 | { |
| 1242 | struct vm *vm = current->vm; |
| 1243 | struct vm_locked locked; |
| 1244 | int64_t ret; |
| 1245 | |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 1246 | locked = vm_lock(vm); |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1247 | switch (vm->mailbox.state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1248 | case MAILBOX_STATE_EMPTY: |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1249 | ret = 0; |
| 1250 | break; |
| 1251 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1252 | case MAILBOX_STATE_RECEIVED: |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1253 | ret = -1; |
| 1254 | break; |
| 1255 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1256 | case MAILBOX_STATE_READ: |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1257 | ret = api_waiter_result(locked, current, next); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1258 | vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1259 | break; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1260 | } |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1261 | vm_unlock(&locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1262 | |
| 1263 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1264 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1265 | |
| 1266 | /** |
| 1267 | * Enables or disables a given interrupt ID for the calling vCPU. |
| 1268 | * |
| 1269 | * Returns 0 on success, or -1 if the intid is invalid. |
| 1270 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 1271 | 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] | 1272 | { |
| 1273 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 1274 | uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1275 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1276 | if (intid >= HF_NUM_INTIDS) { |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | sl_lock(¤t->lock); |
| 1281 | if (enable) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1282 | /* |
| 1283 | * If it is pending and was not enabled before, increment the |
| 1284 | * count. |
| 1285 | */ |
| 1286 | if (current->interrupts.interrupt_pending[intid_index] & |
| 1287 | ~current->interrupts.interrupt_enabled[intid_index] & |
| 1288 | intid_mask) { |
| 1289 | current->interrupts.enabled_and_pending_count++; |
| 1290 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1291 | current->interrupts.interrupt_enabled[intid_index] |= |
| 1292 | intid_mask; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1293 | } else { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1294 | /* |
| 1295 | * If it is pending and was enabled before, decrement the count. |
| 1296 | */ |
| 1297 | if (current->interrupts.interrupt_pending[intid_index] & |
| 1298 | current->interrupts.interrupt_enabled[intid_index] & |
| 1299 | intid_mask) { |
| 1300 | current->interrupts.enabled_and_pending_count--; |
| 1301 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1302 | current->interrupts.interrupt_enabled[intid_index] &= |
| 1303 | ~intid_mask; |
| 1304 | } |
| 1305 | |
| 1306 | sl_unlock(¤t->lock); |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | /** |
| 1311 | * Returns the ID of the next pending interrupt for the calling vCPU, and |
| 1312 | * acknowledges it (i.e. marks it as no longer pending). Returns |
| 1313 | * HF_INVALID_INTID if there are no pending interrupts. |
| 1314 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 1315 | uint32_t api_interrupt_get(struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1316 | { |
| 1317 | uint8_t i; |
| 1318 | uint32_t first_interrupt = HF_INVALID_INTID; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1319 | |
| 1320 | /* |
| 1321 | * Find the first enabled and pending interrupt ID, return it, and |
| 1322 | * deactivate it. |
| 1323 | */ |
| 1324 | sl_lock(¤t->lock); |
| 1325 | for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) { |
| 1326 | uint32_t enabled_and_pending = |
| 1327 | current->interrupts.interrupt_enabled[i] & |
| 1328 | current->interrupts.interrupt_pending[i]; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1329 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1330 | if (enabled_and_pending != 0) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1331 | uint8_t bit_index = ctz(enabled_and_pending); |
| 1332 | /* |
| 1333 | * Mark it as no longer pending and decrement the count. |
| 1334 | */ |
| 1335 | current->interrupts.interrupt_pending[i] &= |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 1336 | ~(1U << bit_index); |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1337 | current->interrupts.enabled_and_pending_count--; |
| 1338 | first_interrupt = |
| 1339 | i * INTERRUPT_REGISTER_BITS + bit_index; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1340 | break; |
| 1341 | } |
| 1342 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1343 | |
| 1344 | sl_unlock(¤t->lock); |
| 1345 | return first_interrupt; |
| 1346 | } |
| 1347 | |
| 1348 | /** |
Andrew Walbran | 4cf217a | 2018-12-14 15:24:50 +0000 | [diff] [blame] | 1349 | * 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] | 1350 | * given VM and vCPU. |
| 1351 | */ |
| 1352 | static inline bool is_injection_allowed(uint32_t target_vm_id, |
| 1353 | struct vcpu *current) |
| 1354 | { |
| 1355 | uint32_t current_vm_id = current->vm->id; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1356 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1357 | /* |
| 1358 | * The primary VM is allowed to inject interrupts into any VM. Secondary |
| 1359 | * VMs are only allowed to inject interrupts into their own vCPUs. |
| 1360 | */ |
| 1361 | return current_vm_id == HF_PRIMARY_VM_ID || |
| 1362 | current_vm_id == target_vm_id; |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * Injects a virtual interrupt of the given ID into the given target vCPU. |
| 1367 | * This doesn't cause the vCPU to actually be run immediately; it will be taken |
| 1368 | * when the vCPU is next run, which is up to the scheduler. |
| 1369 | * |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1370 | * Returns: |
| 1371 | * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt |
| 1372 | * ID is invalid, or the current VM is not allowed to inject interrupts to |
| 1373 | * the target VM. |
| 1374 | * - 0 on success if no further action is needed. |
| 1375 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 1376 | * up or kick the target vCPU. |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1377 | */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1378 | int64_t api_interrupt_inject(spci_vm_id_t target_vm_id, |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 1379 | spci_vcpu_index_t target_vcpu_idx, uint32_t intid, |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1380 | struct vcpu *current, struct vcpu **next) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1381 | { |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1382 | struct vcpu *target_vcpu; |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1383 | struct vm *target_vm = vm_find(target_vm_id); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1384 | |
| 1385 | if (intid >= HF_NUM_INTIDS) { |
| 1386 | return -1; |
| 1387 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1388 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1389 | if (target_vm == NULL) { |
| 1390 | return -1; |
| 1391 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1392 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1393 | if (target_vcpu_idx >= target_vm->vcpu_count) { |
| 1394 | /* The requested vcpu must exist. */ |
| 1395 | return -1; |
| 1396 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1397 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1398 | if (!is_injection_allowed(target_vm_id, current)) { |
| 1399 | return -1; |
| 1400 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1401 | |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 1402 | target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1403 | |
| 1404 | dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid, |
| 1405 | target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id); |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 1406 | return internal_interrupt_inject(target_vcpu, intid, current, next); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1407 | } |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1408 | |
| 1409 | /** |
| 1410 | * Clears a region of physical memory by overwriting it with zeros. The data is |
| 1411 | * flushed from the cache so the memory has been cleared across the system. |
| 1412 | */ |
| 1413 | static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool) |
| 1414 | { |
| 1415 | /* |
| 1416 | * TODO: change this to a cpu local single page window rather than a |
| 1417 | * global mapping of the whole range. Such an approach will limit |
| 1418 | * the changes to stage-1 tables and will allow only local |
| 1419 | * invalidation. |
| 1420 | */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1421 | bool ret; |
| 1422 | struct mm_stage1_locked stage1_locked = mm_lock_stage1(); |
| 1423 | void *ptr = |
| 1424 | mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool); |
Andrew Walbran | 2cb4339 | 2019-04-17 12:52:45 +0100 | [diff] [blame] | 1425 | size_t size = pa_difference(begin, end); |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1426 | |
| 1427 | if (!ptr) { |
| 1428 | /* TODO: partial defrag of failed range. */ |
| 1429 | /* Recover any memory consumed in failed mapping. */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1430 | mm_defrag(stage1_locked, ppool); |
| 1431 | goto fail; |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 1434 | memset_s(ptr, size, 0, size); |
Andrew Scull | c059fbe | 2019-09-12 12:58:40 +0100 | [diff] [blame] | 1435 | arch_mm_flush_dcache(ptr, size); |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1436 | mm_unmap(stage1_locked, begin, end, ppool); |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1437 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1438 | ret = true; |
| 1439 | goto out; |
| 1440 | |
| 1441 | fail: |
| 1442 | ret = false; |
| 1443 | |
| 1444 | out: |
| 1445 | mm_unlock_stage1(&stage1_locked); |
| 1446 | |
| 1447 | return ret; |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1450 | /** TODO: Move function to spci_architected_message.c. */ |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1451 | /** |
| 1452 | * Shares memory from the calling VM with another. The memory can be shared in |
| 1453 | * different modes. |
| 1454 | * |
| 1455 | * This function requires the calling context to hold the <to> and <from> locks. |
| 1456 | * |
| 1457 | * Returns: |
| 1458 | * In case of error one of the following values is returned: |
| 1459 | * 1) SPCI_INVALID_PARAMETERS - The endpoint provided parameters were |
| 1460 | * erroneous; |
Andrew Walbran | 379aa72 | 2019-10-07 14:16:34 +0100 | [diff] [blame] | 1461 | * 2) SPCI_NO_MEMORY - Hafnium did not have sufficient memory to complete |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1462 | * the request. |
| 1463 | * Success is indicated by SPCI_SUCCESS. |
| 1464 | */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1465 | struct spci_value api_spci_share_memory( |
| 1466 | struct vm_locked to_locked, struct vm_locked from_locked, |
| 1467 | struct spci_memory_region *memory_region, uint32_t memory_to_attributes, |
| 1468 | enum spci_memory_share share) |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1469 | { |
| 1470 | struct vm *to = to_locked.vm; |
| 1471 | struct vm *from = from_locked.vm; |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 1472 | uint32_t orig_from_mode; |
| 1473 | uint32_t from_mode; |
| 1474 | uint32_t to_mode; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1475 | struct mpool local_page_pool; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1476 | struct spci_value ret; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1477 | paddr_t pa_begin; |
| 1478 | paddr_t pa_end; |
| 1479 | ipaddr_t begin; |
| 1480 | ipaddr_t end; |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 1481 | struct spci_memory_region_constituent *constituents = |
| 1482 | spci_memory_region_get_constituents(memory_region); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1483 | |
| 1484 | size_t size; |
| 1485 | |
| 1486 | /* Disallow reflexive shares as this suggests an error in the VM. */ |
| 1487 | if (to == from) { |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1488 | return spci_error(SPCI_INVALID_PARAMETERS); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /* |
| 1492 | * Create a local pool so any freed memory can't be used by another |
| 1493 | * thread. This is to ensure the original mapping can be restored if any |
| 1494 | * stage of the process fails. |
| 1495 | */ |
| 1496 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 1497 | |
| 1498 | /* Obtain the single contiguous set of pages from the memory_region. */ |
| 1499 | /* TODO: Add support for multiple constituent regions. */ |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 1500 | size = constituents[0].page_count * PAGE_SIZE; |
| 1501 | begin = ipa_init(constituents[0].address); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1502 | end = ipa_add(begin, size); |
| 1503 | |
| 1504 | /* |
| 1505 | * Check if the state transition is lawful for both VMs involved |
| 1506 | * in the memory exchange, ensure that all constituents of a memory |
| 1507 | * region being shared are at the same state. |
| 1508 | */ |
| 1509 | if (!spci_msg_check_transition(to, from, share, &orig_from_mode, begin, |
| 1510 | end, memory_to_attributes, &from_mode, |
| 1511 | &to_mode)) { |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1512 | return spci_error(SPCI_INVALID_PARAMETERS); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | pa_begin = pa_from_ipa(begin); |
| 1516 | pa_end = pa_from_ipa(end); |
| 1517 | |
| 1518 | /* |
| 1519 | * First update the mapping for the sender so there is not overlap with |
| 1520 | * the recipient. |
| 1521 | */ |
| 1522 | if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode, |
| 1523 | NULL, &local_page_pool)) { |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1524 | ret = spci_error(SPCI_NO_MEMORY); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1525 | goto out; |
| 1526 | } |
| 1527 | |
| 1528 | /* Complete the transfer by mapping the memory into the recipient. */ |
| 1529 | if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL, |
| 1530 | &local_page_pool)) { |
| 1531 | /* TODO: partial defrag of failed range. */ |
| 1532 | /* Recover any memory consumed in failed mapping. */ |
| 1533 | mm_vm_defrag(&from->ptable, &local_page_pool); |
| 1534 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1535 | ret = spci_error(SPCI_NO_MEMORY); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1536 | |
| 1537 | CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end, |
| 1538 | orig_from_mode, NULL, |
| 1539 | &local_page_pool)); |
| 1540 | |
| 1541 | goto out; |
| 1542 | } |
| 1543 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1544 | ret = (struct spci_value){.func = SPCI_SUCCESS_32}; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1545 | |
| 1546 | out: |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1547 | mpool_fini(&local_page_pool); |
| 1548 | |
| 1549 | return ret; |
| 1550 | } |
| 1551 | |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1552 | /** |
| 1553 | * Shares memory from the calling VM with another. The memory can be shared in |
| 1554 | * different modes. |
| 1555 | * |
| 1556 | * TODO: the interface for sharing memory will need to be enhanced to allow |
| 1557 | * sharing with different modes e.g. read-only, informing the recipient |
| 1558 | * of the memory they have been given, opting to not wipe the memory and |
| 1559 | * possibly allowing multiple blocks to be transferred. What this will |
| 1560 | * look like is TBD. |
| 1561 | */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1562 | 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] | 1563 | enum hf_share share, struct vcpu *current) |
| 1564 | { |
| 1565 | struct vm *from = current->vm; |
| 1566 | struct vm *to; |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 1567 | uint32_t orig_from_mode; |
| 1568 | uint32_t from_mode; |
| 1569 | uint32_t to_mode; |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1570 | ipaddr_t begin; |
| 1571 | ipaddr_t end; |
| 1572 | paddr_t pa_begin; |
| 1573 | paddr_t pa_end; |
| 1574 | struct mpool local_page_pool; |
| 1575 | int64_t ret; |
| 1576 | |
| 1577 | /* Disallow reflexive shares as this suggests an error in the VM. */ |
| 1578 | if (vm_id == from->id) { |
| 1579 | return -1; |
| 1580 | } |
| 1581 | |
| 1582 | /* Ensure the target VM exists. */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1583 | to = vm_find(vm_id); |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1584 | if (to == NULL) { |
| 1585 | return -1; |
| 1586 | } |
| 1587 | |
| 1588 | begin = addr; |
| 1589 | end = ipa_add(addr, size); |
| 1590 | |
| 1591 | /* Fail if addresses are not page-aligned. */ |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 1592 | if (!is_aligned(ipa_addr(begin), PAGE_SIZE) || |
| 1593 | !is_aligned(ipa_addr(end), PAGE_SIZE)) { |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1594 | return -1; |
| 1595 | } |
| 1596 | |
| 1597 | /* Convert the sharing request to memory management modes. */ |
| 1598 | switch (share) { |
| 1599 | case HF_MEMORY_GIVE: |
| 1600 | from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED; |
| 1601 | to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X; |
| 1602 | break; |
| 1603 | |
| 1604 | case HF_MEMORY_LEND: |
| 1605 | from_mode = MM_MODE_INVALID; |
| 1606 | to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED; |
| 1607 | break; |
| 1608 | |
| 1609 | case HF_MEMORY_SHARE: |
| 1610 | from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED; |
| 1611 | to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED | |
| 1612 | MM_MODE_SHARED; |
| 1613 | break; |
| 1614 | |
| 1615 | default: |
| 1616 | /* The input is untrusted so might not be a valid value. */ |
| 1617 | return -1; |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | * Create a local pool so any freed memory can't be used by another |
| 1622 | * thread. This is to ensure the original mapping can be restored if any |
| 1623 | * stage of the process fails. |
| 1624 | */ |
| 1625 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 1626 | |
| 1627 | sl_lock_both(&from->lock, &to->lock); |
| 1628 | |
| 1629 | /* |
| 1630 | * Ensure that the memory range is mapped with the same mode so that |
| 1631 | * changes can be reverted if the process fails. |
| 1632 | */ |
| 1633 | if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) { |
| 1634 | goto fail; |
| 1635 | } |
| 1636 | |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 1637 | /* Ensure the address range is normal memory and not a device. */ |
| 1638 | if (orig_from_mode & MM_MODE_D) { |
| 1639 | goto fail; |
| 1640 | } |
| 1641 | |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1642 | /* |
| 1643 | * Ensure the memory range is valid for the sender. If it isn't, the |
| 1644 | * sender has either shared it with another VM already or has no claim |
| 1645 | * to the memory. |
| 1646 | */ |
| 1647 | if (orig_from_mode & MM_MODE_INVALID) { |
| 1648 | goto fail; |
| 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * The sender must own the memory and have exclusive access to it in |
| 1653 | * order to share it. Alternatively, it is giving memory back to the |
| 1654 | * owning VM. |
| 1655 | */ |
| 1656 | if (orig_from_mode & MM_MODE_UNOWNED) { |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 1657 | uint32_t orig_to_mode; |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1658 | |
| 1659 | if (share != HF_MEMORY_GIVE || |
| 1660 | !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) || |
| 1661 | orig_to_mode & MM_MODE_UNOWNED) { |
| 1662 | goto fail; |
| 1663 | } |
| 1664 | } else if (orig_from_mode & MM_MODE_SHARED) { |
| 1665 | goto fail; |
| 1666 | } |
| 1667 | |
| 1668 | pa_begin = pa_from_ipa(begin); |
| 1669 | pa_end = pa_from_ipa(end); |
| 1670 | |
| 1671 | /* |
| 1672 | * First update the mapping for the sender so there is not overlap with |
| 1673 | * the recipient. |
| 1674 | */ |
| 1675 | if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode, |
| 1676 | NULL, &local_page_pool)) { |
| 1677 | goto fail; |
| 1678 | } |
| 1679 | |
| 1680 | /* Clear the memory so no VM or device can see the previous contents. */ |
| 1681 | if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) { |
| 1682 | goto fail_return_to_sender; |
| 1683 | } |
| 1684 | |
| 1685 | /* Complete the transfer by mapping the memory into the recipient. */ |
| 1686 | if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL, |
| 1687 | &local_page_pool)) { |
| 1688 | /* TODO: partial defrag of failed range. */ |
| 1689 | /* Recover any memory consumed in failed mapping. */ |
| 1690 | mm_vm_defrag(&from->ptable, &local_page_pool); |
| 1691 | goto fail_return_to_sender; |
| 1692 | } |
| 1693 | |
| 1694 | ret = 0; |
| 1695 | goto out; |
| 1696 | |
| 1697 | fail_return_to_sender: |
Andrew Scull | 7e8de32 | 2019-07-02 13:00:56 +0100 | [diff] [blame] | 1698 | CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end, |
| 1699 | orig_from_mode, NULL, &local_page_pool)); |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1700 | |
| 1701 | fail: |
| 1702 | ret = -1; |
| 1703 | |
| 1704 | out: |
| 1705 | sl_unlock(&from->lock); |
| 1706 | sl_unlock(&to->lock); |
| 1707 | |
| 1708 | mpool_fini(&local_page_pool); |
| 1709 | |
| 1710 | return ret; |
| 1711 | } |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1712 | |
| 1713 | /** Returns the version of the implemented SPCI specification. */ |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 1714 | struct spci_value api_spci_version(void) |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1715 | { |
| 1716 | /* |
| 1717 | * Ensure that both major and minor revision representation occupies at |
| 1718 | * most 15 bits. |
| 1719 | */ |
| 1720 | static_assert(0x8000 > SPCI_VERSION_MAJOR, |
| 1721 | "Major revision representation take more than 15 bits."); |
| 1722 | static_assert(0x10000 > SPCI_VERSION_MINOR, |
| 1723 | "Minor revision representation take more than 16 bits."); |
| 1724 | |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 1725 | struct spci_value ret = { |
| 1726 | .func = SPCI_SUCCESS_32, |
Andrew Walbran | 455c53a | 2019-10-10 13:56:19 +0100 | [diff] [blame] | 1727 | .arg2 = (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) | |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 1728 | SPCI_VERSION_MINOR}; |
| 1729 | return ret; |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1730 | } |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 1731 | |
| 1732 | int64_t api_debug_log(char c, struct vcpu *current) |
| 1733 | { |
Andrew Scull | d54e1be | 2019-08-20 11:09:42 +0100 | [diff] [blame] | 1734 | bool flush; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 1735 | struct vm *vm = current->vm; |
| 1736 | struct vm_locked vm_locked = vm_lock(vm); |
| 1737 | |
Andrew Scull | d54e1be | 2019-08-20 11:09:42 +0100 | [diff] [blame] | 1738 | if (c == '\n' || c == '\0') { |
| 1739 | flush = true; |
| 1740 | } else { |
| 1741 | vm->log_buffer[vm->log_buffer_length++] = c; |
| 1742 | flush = (vm->log_buffer_length == sizeof(vm->log_buffer)); |
| 1743 | } |
| 1744 | |
| 1745 | if (flush) { |
Andrew Walbran | 7f904bf | 2019-07-12 16:38:38 +0100 | [diff] [blame] | 1746 | dlog_flush_vm_buffer(vm->id, vm->log_buffer, |
| 1747 | vm->log_buffer_length); |
| 1748 | vm->log_buffer_length = 0; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | vm_unlock(&vm_locked); |
| 1752 | |
| 1753 | return 0; |
| 1754 | } |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1755 | |
| 1756 | /** |
| 1757 | * Discovery function returning information about the implementation of optional |
| 1758 | * SPCI interfaces. |
| 1759 | */ |
| 1760 | struct spci_value api_spci_features(uint32_t function_id) |
| 1761 | { |
| 1762 | switch (function_id) { |
| 1763 | case SPCI_ERROR_32: |
| 1764 | case SPCI_SUCCESS_32: |
| 1765 | case SPCI_ID_GET_32: |
| 1766 | case SPCI_YIELD_32: |
| 1767 | case SPCI_VERSION_32: |
| 1768 | case SPCI_FEATURES_32: |
| 1769 | case SPCI_MSG_SEND_32: |
| 1770 | case SPCI_MSG_POLL_32: |
| 1771 | case SPCI_MSG_WAIT_32: |
| 1772 | return (struct spci_value){.func = SPCI_SUCCESS_32}; |
| 1773 | default: |
| 1774 | return spci_error(SPCI_NOT_SUPPORTED); |
| 1775 | } |
| 1776 | } |