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 | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 32 | /* The ID of the primary VM which is responsile for scheduling. */ |
| 33 | #define HF_PRIMARY_VM_ID 0 |
| 34 | |
Andrew Scull | 13652af | 2018-09-17 14:49:08 +0100 | [diff] [blame] | 35 | /* Return codes for hf_vcpu_run(). */ |
| 36 | #define HF_VCPU_RUN_YIELD 0x00 |
| 37 | #define HF_VCPU_RUN_WAIT_FOR_INTERRUPT 0x01 |
| 38 | #define HF_VCPU_RUN_WAKE_UP 0x02 |
| 39 | #define HF_VCPU_RUN_RESPONSE_READY 0x03 |
| 40 | |
| 41 | /* Construct and destruct the hf_vcpu_run() response. */ |
| 42 | #define HF_VCPU_RUN_RESPONSE(code, data) ((code & 0xff) | (data << 8)) |
| 43 | #define HF_VCPU_RUN_CODE(ret) (ret & 0xff) |
| 44 | #define HF_VCPU_RUN_DATA(ret) (ret >> 8) |
| 45 | |
| 46 | #define HF_RPC_REQUEST_MAX_SIZE 4096 |
| 47 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 48 | /* clang-format on */ |
| 49 | |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 50 | /** |
| 51 | * This function must be implemented to trigger the architecture specific |
| 52 | * mechanism to call to the hypervisor. |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 53 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame^] | 54 | int64_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] | 55 | |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 56 | /** |
| 57 | * Runs the given vcpu of the given vm. |
| 58 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame^] | 59 | static inline int64_t hf_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx) |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 60 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 61 | return hf_call(HF_VCPU_RUN, vm_id, vcpu_idx, 0); |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 65 | * Returns the number of secondary VMs. |
| 66 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame^] | 67 | static inline int64_t hf_vm_get_count(void) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 68 | { |
| 69 | return hf_call(HF_VM_GET_COUNT, 0, 0, 0); |
| 70 | } |
| 71 | |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 72 | /** |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 73 | * Returns the number of VCPUs configured in the given secondary VM. |
| 74 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame^] | 75 | static inline int64_t hf_vcpu_get_count(uint32_t vm_id) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 76 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 77 | return hf_call(HF_VCPU_GET_COUNT, vm_id, 0, 0); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 80 | /** |
| 81 | * Configures the pages to send/receive data through. The pages must not be |
| 82 | * shared. |
| 83 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame^] | 84 | static inline int64_t hf_vm_configure(hf_ipaddr_t send, hf_ipaddr_t recv) |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 85 | { |
| 86 | return hf_call(HF_VM_CONFIGURE, send, recv, 0); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Called by the primary VM to send an RPC request to a secondary VM. Data is |
| 91 | * copied from the caller's send buffer to the destination's receive buffer. |
| 92 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame^] | 93 | static inline int64_t hf_rpc_request(uint32_t vm_id, size_t size) |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 94 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 95 | return hf_call(HF_RPC_REQUEST, vm_id, size, 0); |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Called by the primary VM to read a request sent from a previous call to |
| 100 | * api_rpc_request. If one isn't available, this function can optionally block |
| 101 | * the caller until one becomes available. |
| 102 | * |
| 103 | * Once the caller has completed handling a request, it must indicate it by |
| 104 | * either calling api_rpc_reply or api_rpc_ack. No new requests can be accepted |
| 105 | * until the current one is acknowledged. |
| 106 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame^] | 107 | static inline int64_t hf_rpc_read_request(bool block) |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 108 | { |
| 109 | return hf_call(HF_RPC_READ_REQUEST, block, 0, 0); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Acknowledges that either a request or a reply has been received and handled. |
| 114 | * After this call completes, the caller will be able to receive additional |
| 115 | * requests or replies. |
| 116 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame^] | 117 | static inline int64_t hf_rpc_ack(void) |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 118 | { |
| 119 | return hf_call(HF_RPC_ACK, 0, 0, 0); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Called by a secondary VM to send a reply to the primary VM. Data is copied |
| 124 | * from the caller's send buffer to the destination's receive buffer. |
| 125 | * |
| 126 | * It can optionally acknowledge the pending request. |
| 127 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame^] | 128 | static inline int64_t hf_rpc_reply(size_t size, bool ack) |
Andrew Scull | 5ac05f0 | 2018-08-10 17:23:22 +0100 | [diff] [blame] | 129 | { |
| 130 | return hf_call(HF_RPC_REPLY, size, ack, 0); |
| 131 | } |