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 { |
| 22 | HF_VCPU_RUN_YIELD, |
| 23 | HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 24 | HF_VCPU_RUN_WAKE_UP, |
| 25 | HF_VCPU_RUN_MESSAGE, |
| 26 | HF_VCPU_RUN_SLEEP, |
| 27 | }; |
| 28 | |
| 29 | struct hf_vcpu_run_return { |
| 30 | enum hf_vcpu_run_code code; |
| 31 | union { |
| 32 | struct { |
| 33 | uint32_t vm_id; |
| 34 | uint16_t vcpu; |
| 35 | } wake_up; |
| 36 | struct { |
| 37 | uint32_t size; |
| 38 | } message; |
| 39 | struct { |
| 40 | uint64_t ns; |
| 41 | } sleep; |
| 42 | }; |
| 43 | }; |
| 44 | |
| 45 | struct hf_mailbox_receive_return { |
| 46 | uint32_t vm_id; |
| 47 | uint32_t size; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * Encode an hf_vcpu_run_return struct in the 64-bit packing ABI. |
| 52 | */ |
| 53 | static inline uint64_t hf_vcpu_run_return_encode(struct hf_vcpu_run_return res) |
| 54 | { |
| 55 | uint64_t ret = res.code & 0xff; |
| 56 | switch (res.code) { |
| 57 | case HF_VCPU_RUN_WAKE_UP: |
| 58 | ret |= (uint64_t)res.wake_up.vm_id << 32; |
| 59 | ret |= (uint64_t)res.wake_up.vcpu << 16; |
| 60 | break; |
| 61 | case HF_VCPU_RUN_MESSAGE: |
| 62 | ret |= (uint64_t)res.message.size << 32; |
| 63 | break; |
| 64 | case HF_VCPU_RUN_SLEEP: |
| 65 | ret |= res.sleep.ns << 8; |
| 66 | break; |
| 67 | default: |
| 68 | break; |
| 69 | } |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Decode an hf_vcpu_run_return struct from the 64-bit packing ABI. |
| 75 | */ |
| 76 | static inline struct hf_vcpu_run_return hf_vcpu_run_return_decode(uint64_t res) |
| 77 | { |
Wedson Almeida Filho | bdcd836 | 2018-12-15 03:26:21 +0000 | [diff] [blame^] | 78 | struct hf_vcpu_run_return ret = { |
| 79 | .code = (enum hf_vcpu_run_code)(res & 0xff), |
| 80 | }; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 81 | |
| 82 | /* Some codes include more data. */ |
| 83 | switch (ret.code) { |
| 84 | case HF_VCPU_RUN_WAKE_UP: |
| 85 | ret.wake_up.vm_id = res >> 32; |
| 86 | ret.wake_up.vcpu = (res >> 16) & 0xffff; |
| 87 | break; |
| 88 | case HF_VCPU_RUN_MESSAGE: |
| 89 | ret.message.size = res >> 32; |
| 90 | break; |
| 91 | case HF_VCPU_RUN_SLEEP: |
| 92 | ret.sleep.ns = res >> 8; |
| 93 | break; |
| 94 | default: |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Encode an hf_mailbox_receive_return struct in the 64-bit packing ABI. |
| 103 | */ |
| 104 | static inline uint64_t hf_mailbox_receive_return_encode( |
| 105 | struct hf_mailbox_receive_return res) |
| 106 | { |
| 107 | return res.vm_id | ((uint64_t)res.size << 32); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Decode an hf_mailbox_receive_return struct from the 64-bit packing ABI. |
| 112 | */ |
| 113 | static inline struct hf_mailbox_receive_return hf_mailbox_receive_return_decode( |
| 114 | uint64_t res) |
| 115 | { |
| 116 | return (struct hf_mailbox_receive_return){ |
| 117 | .vm_id = (uint32_t)(res & 0xffffffff), |
| 118 | .size = (uint32_t)(res >> 32), |
| 119 | }; |
| 120 | } |