1 /* 2 * Copyright (c) 2018-2021 Maxime Villard, m00nbsd.net 3 * All rights reserved. 4 * 5 * This code is part of the NVMM hypervisor. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #ifndef _LIBNVMM_H_ 30 #define _LIBNVMM_H_ 31 32 #include <stdint.h> 33 #include <stdbool.h> 34 35 #if defined(__NetBSD__) 36 #include <dev/nvmm/nvmm.h> 37 #include <dev/nvmm/nvmm_ioctl.h> 38 #elif defined(__DragonFly__) 39 #include <dev/virtual/nvmm/nvmm.h> 40 #include <dev/virtual/nvmm/nvmm_ioctl.h> 41 #else 42 #error "Unsupported OS." 43 #endif 44 45 #define NVMM_USER_VERSION 1 46 47 struct nvmm_io; 48 struct nvmm_mem; 49 50 struct nvmm_assist_callbacks { 51 void (*io)(struct nvmm_io *); 52 void (*mem)(struct nvmm_mem *); 53 }; 54 55 struct nvmm_machine { 56 nvmm_machid_t machid; 57 struct nvmm_comm_page **pages; 58 void *areas; /* opaque */ 59 }; 60 61 struct nvmm_vcpu { 62 nvmm_cpuid_t cpuid; 63 struct nvmm_assist_callbacks cbs; 64 struct nvmm_vcpu_state *state; 65 struct nvmm_vcpu_event *event; 66 struct nvmm_vcpu_exit *exit; 67 }; 68 69 struct nvmm_io { 70 struct nvmm_machine *mach; 71 struct nvmm_vcpu *vcpu; 72 uint16_t port; 73 bool in; 74 size_t size; 75 uint8_t *data; 76 }; 77 78 struct nvmm_mem { 79 struct nvmm_machine *mach; 80 struct nvmm_vcpu *vcpu; 81 gpaddr_t gpa; 82 bool write; 83 size_t size; 84 uint8_t *data; 85 }; 86 87 #define NVMM_VCPU_CONF_CALLBACKS NVMM_VCPU_CONF_LIBNVMM_BEGIN 88 89 #define NVMM_PROT_READ 0x01 90 #define NVMM_PROT_WRITE 0x02 91 #define NVMM_PROT_EXEC 0x04 92 #define NVMM_PROT_USER 0x08 93 #define NVMM_PROT_ALL 0x0F 94 typedef uint64_t nvmm_prot_t; 95 96 int nvmm_init(void); 97 int nvmm_root_init(void); 98 99 int nvmm_capability(struct nvmm_capability *); 100 101 int nvmm_machine_create(struct nvmm_machine *); 102 int nvmm_machine_destroy(struct nvmm_machine *); 103 int nvmm_machine_configure(struct nvmm_machine *, uint64_t, void *); 104 105 int nvmm_vcpu_create(struct nvmm_machine *, nvmm_cpuid_t, struct nvmm_vcpu *); 106 int nvmm_vcpu_destroy(struct nvmm_machine *, struct nvmm_vcpu *); 107 int nvmm_vcpu_configure(struct nvmm_machine *, struct nvmm_vcpu *, uint64_t, 108 void *); 109 int nvmm_vcpu_setstate(struct nvmm_machine *, struct nvmm_vcpu *, uint64_t); 110 int nvmm_vcpu_getstate(struct nvmm_machine *, struct nvmm_vcpu *, uint64_t); 111 int nvmm_vcpu_inject(struct nvmm_machine *, struct nvmm_vcpu *); 112 int nvmm_vcpu_run(struct nvmm_machine *, struct nvmm_vcpu *); 113 114 int nvmm_gpa_map(struct nvmm_machine *, uintptr_t, gpaddr_t, size_t, int); 115 int nvmm_gpa_unmap(struct nvmm_machine *, uintptr_t, gpaddr_t, size_t); 116 int nvmm_hva_map(struct nvmm_machine *, uintptr_t, size_t); 117 int nvmm_hva_unmap(struct nvmm_machine *, uintptr_t, size_t); 118 119 int nvmm_gva_to_gpa(struct nvmm_machine *, struct nvmm_vcpu *, gvaddr_t, gpaddr_t *, 120 nvmm_prot_t *); 121 int nvmm_gpa_to_hva(struct nvmm_machine *, gpaddr_t, uintptr_t *, 122 nvmm_prot_t *); 123 124 int nvmm_assist_io(struct nvmm_machine *, struct nvmm_vcpu *); 125 int nvmm_assist_mem(struct nvmm_machine *, struct nvmm_vcpu *); 126 127 int nvmm_ctl(int, void *, size_t); 128 129 int nvmm_vcpu_dump(struct nvmm_machine *, struct nvmm_vcpu *); 130 131 #endif /* _LIBNVMM_H_ */ 132