Andrew Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 1 | #pragma once |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 2 | |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 3 | #if defined(__linux__) && defined(__KERNEL__) |
| 4 | |
| 5 | #include <linux/types.h> |
| 6 | |
| 7 | typedef phys_addr_t hf_ipaddr_t; |
| 8 | |
| 9 | #else |
| 10 | |
| 11 | #include <stdbool.h> |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 12 | #include <stddef.h> |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | |
| 15 | typedef uintptr_t hf_ipaddr_t; |
| 16 | |
| 17 | #endif |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 18 | |
| 19 | /* Keep macro alignment */ |
| 20 | /* clang-format off */ |
| 21 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 22 | /* TODO: Define constants below according to spec. */ |
| 23 | #define HF_VCPU_RUN 0xff00 |
| 24 | #define HF_VM_GET_COUNT 0xff01 |
| 25 | #define HF_VCPU_GET_COUNT 0xff02 |
| 26 | #define HF_VM_CONFIGURE 0xff03 |
| 27 | #define HF_RPC_REQUEST 0xff04 |
| 28 | #define HF_RPC_READ_REQUEST 0xff05 |
| 29 | #define HF_RPC_ACK 0xff06 |
| 30 | #define HF_RPC_REPLY 0xff07 |
| 31 | |
Andrew Scull | 13652af | 2018-09-17 14:49:08 +0100 | [diff] [blame^] | 32 | /* Return codes for hf_vcpu_run(). */ |
| 33 | #define HF_VCPU_RUN_YIELD 0x00 |
| 34 | #define HF_VCPU_RUN_WAIT_FOR_INTERRUPT 0x01 |
| 35 | #define HF_VCPU_RUN_WAKE_UP 0x02 |
| 36 | #define HF_VCPU_RUN_RESPONSE_READY 0x03 |
| 37 | |
| 38 | /* Construct and destruct the hf_vcpu_run() response. */ |
| 39 | #define HF_VCPU_RUN_RESPONSE(code, data) ((code & 0xff) | (data << 8)) |
| 40 | #define HF_VCPU_RUN_CODE(ret) (ret & 0xff) |
| 41 | #define HF_VCPU_RUN_DATA(ret) (ret >> 8) |
| 42 | |
| 43 | #define HF_RPC_REQUEST_MAX_SIZE 4096 |
| 44 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 45 | /* clang-format on */ |
| 46 | |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 47 | /** |
| 48 | * This function must be implemented to trigger the architecture specific |
| 49 | * mechanism to call to the hypervisor. |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 50 | */ |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 51 | size_t hf_call(size_t arg0, size_t arg1, size_t arg2, size_t arg3); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 52 | |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 53 | /** |
| 54 | * Runs the given vcpu of the given vm. |
| 55 | */ |
| 56 | static inline int32_t hf_vcpu_run(uint32_t vm_idx, uint32_t vcpu_idx) |
| 57 | { |
| 58 | return hf_call(HF_VCPU_RUN, vm_idx, vcpu_idx, 0); |
| 59 | } |
| 60 | |
| 61 | /** |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 62 | * Returns the number of secondary VMs. |
| 63 | */ |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 64 | static inline int32_t hf_vm_get_count(void) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 65 | { |
| 66 | return hf_call(HF_VM_GET_COUNT, 0, 0, 0); |
| 67 | } |
| 68 | |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 69 | /** |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 70 | * Returns the number of VCPUs configured in the given secondary VM. |
| 71 | */ |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 72 | static inline int32_t hf_vcpu_get_count(uint32_t vm_idx) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 73 | { |
| 74 | return hf_call(HF_VCPU_GET_COUNT, vm_idx, 0, 0); |
| 75 | } |
| 76 | |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 77 | /** |
| 78 | * Configures the pages to send/receive data through. The pages must not be |
| 79 | * shared. |
| 80 | */ |
| 81 | static inline int32_t hf_vm_configure(hf_ipaddr_t send, hf_ipaddr_t recv) |
| 82 | { |
| 83 | return hf_call(HF_VM_CONFIGURE, send, recv, 0); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Called by the primary VM to send an RPC request to a secondary VM. Data is |
| 88 | * copied from the caller's send buffer to the destination's receive buffer. |
| 89 | */ |
| 90 | static inline int32_t hf_rpc_request(uint32_t vm_idx, size_t size) |
| 91 | { |
| 92 | return hf_call(HF_RPC_REQUEST, vm_idx, size, 0); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Called by the primary VM to read a request sent from a previous call to |
| 97 | * api_rpc_request. If one isn't available, this function can optionally block |
| 98 | * the caller until one becomes available. |
| 99 | * |
| 100 | * Once the caller has completed handling a request, it must indicate it by |
| 101 | * either calling api_rpc_reply or api_rpc_ack. No new requests can be accepted |
| 102 | * until the current one is acknowledged. |
| 103 | */ |
| 104 | static inline int32_t hf_rpc_read_request(bool block) |
| 105 | { |
| 106 | return hf_call(HF_RPC_READ_REQUEST, block, 0, 0); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Acknowledges that either a request or a reply has been received and handled. |
| 111 | * After this call completes, the caller will be able to receive additional |
| 112 | * requests or replies. |
| 113 | */ |
| 114 | static inline int32_t hf_rpc_ack(void) |
| 115 | { |
| 116 | return hf_call(HF_RPC_ACK, 0, 0, 0); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Called by a secondary VM to send a reply to the primary VM. Data is copied |
| 121 | * from the caller's send buffer to the destination's receive buffer. |
| 122 | * |
| 123 | * It can optionally acknowledge the pending request. |
| 124 | */ |
| 125 | static inline int32_t hf_rpc_reply(size_t size, bool ack) |
| 126 | { |
| 127 | return hf_call(HF_RPC_REPLY, size, ack, 0); |
| 128 | } |