Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 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 | |
| 19 | #include "hf/types.h" |
| 20 | |
| 21 | enum hf_vcpu_run_code { |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 22 | /** |
| 23 | * The vCPU has yielded, forcibly or voluntarily. The scheduler should |
| 24 | * call `hf_vcpu_run` again sometime soon. |
| 25 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 26 | HF_VCPU_RUN_YIELD, |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * The vCPU is blocked waiting for an interrupt. The scheduler should |
| 30 | * take it off the run queue and not call `hf_vcpu_run` until it has |
| 31 | * injected an interrupt, sent it a message, or got a |
| 32 | * `HF_VCPU_RUN_WAKE_UP` for it from another vCPU. |
| 33 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 34 | HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * The vCPU would like `hf_vcpu_run` to be called on another vCPU, |
| 38 | * specified by `hf_vcpu_run_return.wake_up`. The scheduler should |
| 39 | * either wake the vCPU in question up if it is blocked, or preempt and |
| 40 | * re-run it if it is already running somewhere. This gives Hafnium a |
| 41 | * chance to update any CPU state which might have changed. |
| 42 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 43 | HF_VCPU_RUN_WAKE_UP, |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * A new message is available for the scheduler VM, as specified by |
| 47 | * `hf_vcpu_run_return.message`. |
| 48 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 49 | HF_VCPU_RUN_MESSAGE, |
Andrew Walbran | 3d72005 | 2018-12-17 18:37:48 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Like `HF_VCPU_RUN_WAIT_FOR_INTERRUPT`, but for a limited amount of |
| 53 | * time, specified by `hf_vcpu_run_return.sleep`. After at least that |
| 54 | * amount of time has passed, or any of the events listed for |
| 55 | * `HF_VCPU_RUN_WAIT_FOR_INTERRUPT` occur, the scheduler should call |
| 56 | * `hf_vcpu_run` on it again. |
| 57 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 58 | HF_VCPU_RUN_SLEEP, |
| 59 | }; |
| 60 | |
| 61 | struct hf_vcpu_run_return { |
| 62 | enum hf_vcpu_run_code code; |
| 63 | union { |
| 64 | struct { |
| 65 | uint32_t vm_id; |
| 66 | uint16_t vcpu; |
| 67 | } wake_up; |
| 68 | struct { |
| 69 | uint32_t size; |
| 70 | } message; |
| 71 | struct { |
| 72 | uint64_t ns; |
| 73 | } sleep; |
| 74 | }; |
| 75 | }; |
| 76 | |
| 77 | struct hf_mailbox_receive_return { |
| 78 | uint32_t vm_id; |
| 79 | uint32_t size; |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Encode an hf_vcpu_run_return struct in the 64-bit packing ABI. |
| 84 | */ |
| 85 | static inline uint64_t hf_vcpu_run_return_encode(struct hf_vcpu_run_return res) |
| 86 | { |
| 87 | uint64_t ret = res.code & 0xff; |
| 88 | switch (res.code) { |
| 89 | case HF_VCPU_RUN_WAKE_UP: |
| 90 | ret |= (uint64_t)res.wake_up.vm_id << 32; |
| 91 | ret |= (uint64_t)res.wake_up.vcpu << 16; |
| 92 | break; |
| 93 | case HF_VCPU_RUN_MESSAGE: |
| 94 | ret |= (uint64_t)res.message.size << 32; |
| 95 | break; |
| 96 | case HF_VCPU_RUN_SLEEP: |
| 97 | ret |= res.sleep.ns << 8; |
| 98 | break; |
| 99 | default: |
| 100 | break; |
| 101 | } |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Decode an hf_vcpu_run_return struct from the 64-bit packing ABI. |
| 107 | */ |
| 108 | static inline struct hf_vcpu_run_return hf_vcpu_run_return_decode(uint64_t res) |
| 109 | { |
Wedson Almeida Filho | bdcd836 | 2018-12-15 03:26:21 +0000 | [diff] [blame] | 110 | struct hf_vcpu_run_return ret = { |
| 111 | .code = (enum hf_vcpu_run_code)(res & 0xff), |
| 112 | }; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 113 | |
| 114 | /* Some codes include more data. */ |
| 115 | switch (ret.code) { |
| 116 | case HF_VCPU_RUN_WAKE_UP: |
| 117 | ret.wake_up.vm_id = res >> 32; |
| 118 | ret.wake_up.vcpu = (res >> 16) & 0xffff; |
| 119 | break; |
| 120 | case HF_VCPU_RUN_MESSAGE: |
| 121 | ret.message.size = res >> 32; |
| 122 | break; |
| 123 | case HF_VCPU_RUN_SLEEP: |
| 124 | ret.sleep.ns = res >> 8; |
| 125 | break; |
| 126 | default: |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Encode an hf_mailbox_receive_return struct in the 64-bit packing ABI. |
| 135 | */ |
| 136 | static inline uint64_t hf_mailbox_receive_return_encode( |
| 137 | struct hf_mailbox_receive_return res) |
| 138 | { |
| 139 | return res.vm_id | ((uint64_t)res.size << 32); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Decode an hf_mailbox_receive_return struct from the 64-bit packing ABI. |
| 144 | */ |
| 145 | static inline struct hf_mailbox_receive_return hf_mailbox_receive_return_decode( |
| 146 | uint64_t res) |
| 147 | { |
| 148 | return (struct hf_mailbox_receive_return){ |
| 149 | .vm_id = (uint32_t)(res & 0xffffffff), |
| 150 | .size = (uint32_t)(res >> 32), |
| 151 | }; |
| 152 | } |