Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +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 | |
| 17 | #pragma once |
| 18 | |
Andrew Walbran | 9553492 | 2019-06-19 11:32:54 +0100 | [diff] [blame] | 19 | #include "hf/spci.h" |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 20 | #include "hf/types.h" |
| 21 | |
| 22 | enum hf_vcpu_run_code { |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 23 | /** |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 24 | * The vCPU has been preempted but still has work to do. If the |
| 25 | * scheduling quantum has not expired, the scheduler MUST call |
| 26 | * `hf_vcpu_run` on the vCPU to allow it to continue. |
| 27 | */ |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 28 | HF_VCPU_RUN_PREEMPTED = 0, |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * The vCPU has voluntarily yielded the CPU. The scheduler SHOULD take a |
| 32 | * scheduling decision to give cycles to those that need them but MUST |
| 33 | * call `hf_vcpu_run` on the vCPU at a later point. |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 34 | */ |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 35 | HF_VCPU_RUN_YIELD = 1, |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 36 | |
| 37 | /** |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 38 | * The vCPU is blocked waiting for an interrupt. The scheduler MUST take |
| 39 | * it off the run queue and not call `hf_vcpu_run` on the vCPU until it |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 40 | * has injected an interrupt, received `HF_VCPU_RUN_WAKE_UP` for it |
| 41 | * from another vCPU or the timeout provided in |
| 42 | * `hf_vcpu_run_return.sleep` is not `HF_SLEEP_INDEFINITE` and the |
| 43 | * specified duration has expired. |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 44 | */ |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 45 | HF_VCPU_RUN_WAIT_FOR_INTERRUPT = 2, |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 48 | * The vCPU is blocked waiting for a message. The scheduler MUST take it |
| 49 | * off the run queue and not call `hf_vcpu_run` on the vCPU until it has |
| 50 | * injected an interrupt, sent it a message, or received |
Andrew Walbran | d7a6ea7 | 2019-10-01 15:26:10 +0100 | [diff] [blame] | 51 | * `HF_VCPU_RUN_WAKE_UP` for it from another vCPU, or the timeout |
| 52 | * provided in `hf_vcpu_run_return.sleep` is not `HF_SLEEP_INDEFINITE` |
| 53 | * and the specified duration has expired. |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 54 | */ |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 55 | HF_VCPU_RUN_WAIT_FOR_MESSAGE = 3, |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 56 | |
| 57 | /** |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 58 | * Hafnium would like `hf_vcpu_run` to be called on another vCPU, |
| 59 | * specified by `hf_vcpu_run_return.wake_up`. The scheduler MUST either |
| 60 | * wake the vCPU in question up if it is blocked, or preempt and re-run |
| 61 | * it if it is already running somewhere. This gives Hafnium a chance to |
| 62 | * update any CPU state which might have changed. |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 63 | */ |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 64 | HF_VCPU_RUN_WAKE_UP = 4, |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 65 | |
| 66 | /** |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 67 | * A message has been sent by the vCPU. The scheduler MUST run a vCPU |
| 68 | * from the recipient VM and priority SHOULD be given to those vCPUs |
| 69 | * that are waiting for a message. |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 70 | */ |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 71 | HF_VCPU_RUN_MESSAGE = 5, |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * The vCPU has made the mailbox writable and there are pending waiters. |
| 75 | * The scheduler MUST call hf_mailbox_waiter_get() repeatedly and notify |
| 76 | * all waiters by injecting an HF_MAILBOX_WRITABLE_INTID interrupt. |
| 77 | */ |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 78 | HF_VCPU_RUN_NOTIFY_WAITERS = 6, |
| 79 | |
| 80 | /** |
| 81 | * The vCPU has aborted triggering the whole VM to abort. The scheduler |
| 82 | * MUST treat this as `HF_VCPU_RUN_WAIT_FOR_INTERRUPT` for this vCPU and |
| 83 | * `HF_VCPU_RUN_WAKE_UP` for all the other vCPUs of the VM. |
| 84 | */ |
| 85 | HF_VCPU_RUN_ABORTED = 7, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | struct hf_vcpu_run_return { |
| 89 | enum hf_vcpu_run_code code; |
| 90 | union { |
| 91 | struct { |
Andrew Walbran | 9553492 | 2019-06-19 11:32:54 +0100 | [diff] [blame] | 92 | spci_vm_id_t vm_id; |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 93 | spci_vcpu_index_t vcpu; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 94 | } wake_up; |
| 95 | struct { |
Andrew Walbran | 9553492 | 2019-06-19 11:32:54 +0100 | [diff] [blame] | 96 | spci_vm_id_t vm_id; |
Andrew Walbran | f1bd632 | 2019-10-03 16:45:11 +0100 | [diff] [blame] | 97 | uint32_t size; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 98 | } message; |
| 99 | struct { |
| 100 | uint64_t ns; |
| 101 | } sleep; |
| 102 | }; |
| 103 | }; |
| 104 | |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 105 | enum hf_share { |
| 106 | /** |
| 107 | * Relinquish ownership and access to the memory and pass them to the |
| 108 | * recipient. |
| 109 | */ |
| 110 | HF_MEMORY_GIVE, |
| 111 | |
| 112 | /** |
| 113 | * Retain ownership of the memory but relinquish access to the |
| 114 | * recipient. |
| 115 | */ |
| 116 | HF_MEMORY_LEND, |
| 117 | |
| 118 | /** |
| 119 | * Retain ownership and access but additionally allow access to the |
| 120 | * recipient. |
| 121 | */ |
| 122 | HF_MEMORY_SHARE, |
| 123 | }; |
| 124 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 125 | /** |
| 126 | * Encode an hf_vcpu_run_return struct in the 64-bit packing ABI. |
| 127 | */ |
| 128 | static inline uint64_t hf_vcpu_run_return_encode(struct hf_vcpu_run_return res) |
| 129 | { |
| 130 | uint64_t ret = res.code & 0xff; |
Andrew Scull | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame] | 131 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 132 | switch (res.code) { |
| 133 | case HF_VCPU_RUN_WAKE_UP: |
| 134 | ret |= (uint64_t)res.wake_up.vm_id << 32; |
| 135 | ret |= (uint64_t)res.wake_up.vcpu << 16; |
| 136 | break; |
| 137 | case HF_VCPU_RUN_MESSAGE: |
Andrew Walbran | f1bd632 | 2019-10-03 16:45:11 +0100 | [diff] [blame] | 138 | ret |= (uint64_t)res.message.size << 32; |
Andrew Scull | 8023236 | 2019-04-01 12:37:41 +0100 | [diff] [blame] | 139 | ret |= res.message.vm_id << 8; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 140 | break; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 141 | case HF_VCPU_RUN_WAIT_FOR_INTERRUPT: |
| 142 | case HF_VCPU_RUN_WAIT_FOR_MESSAGE: |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 143 | ret |= res.sleep.ns << 8; |
| 144 | break; |
| 145 | default: |
| 146 | break; |
| 147 | } |
Andrew Scull | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame] | 148 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Decode an hf_vcpu_run_return struct from the 64-bit packing ABI. |
| 154 | */ |
| 155 | static inline struct hf_vcpu_run_return hf_vcpu_run_return_decode(uint64_t res) |
| 156 | { |
Wedson Almeida Filho | bdcd836 | 2018-12-15 03:26:21 +0000 | [diff] [blame] | 157 | struct hf_vcpu_run_return ret = { |
| 158 | .code = (enum hf_vcpu_run_code)(res & 0xff), |
| 159 | }; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 160 | |
| 161 | /* Some codes include more data. */ |
| 162 | switch (ret.code) { |
| 163 | case HF_VCPU_RUN_WAKE_UP: |
| 164 | ret.wake_up.vm_id = res >> 32; |
| 165 | ret.wake_up.vcpu = (res >> 16) & 0xffff; |
| 166 | break; |
| 167 | case HF_VCPU_RUN_MESSAGE: |
Andrew Walbran | f1bd632 | 2019-10-03 16:45:11 +0100 | [diff] [blame] | 168 | ret.message.size = res >> 32; |
| 169 | ret.message.vm_id = (res >> 8) & 0xffff; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 170 | break; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 171 | case HF_VCPU_RUN_WAIT_FOR_INTERRUPT: |
| 172 | case HF_VCPU_RUN_WAIT_FOR_MESSAGE: |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 173 | ret.sleep.ns = res >> 8; |
| 174 | break; |
| 175 | default: |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | return ret; |
| 180 | } |