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 _NVMM_INTERNAL_H_ 30 #define _NVMM_INTERNAL_H_ 31 32 #ifndef _KERNEL 33 #error "This file should not be included by userland programs." 34 #endif 35 36 #include "nvmm_os.h" 37 38 #define NVMM_MAX_MACHINES 128 39 #define NVMM_MAX_VCPUS 128 40 #define NVMM_MAX_HMAPPINGS 32 41 #define NVMM_MAX_RAM (128ULL * 1024ULL * (1 << 30)) 42 43 struct nvmm_owner { 44 pid_t pid; 45 }; 46 47 struct nvmm_cpu { 48 /* Shared. */ 49 bool present; 50 nvmm_cpuid_t cpuid; 51 os_mtx_t lock; 52 53 /* Comm page. */ 54 struct nvmm_comm_page *comm; 55 56 /* Last host CPU on which the VCPU ran. */ 57 int hcpu_last; 58 59 /* Implementation-specific. */ 60 void *cpudata; 61 }; 62 63 struct nvmm_hmapping { 64 bool present; 65 uintptr_t hva; 66 size_t size; 67 os_vmobj_t *vmobj; 68 }; 69 70 struct nvmm_machine { 71 bool present; 72 nvmm_machid_t machid; 73 time_t time; 74 struct nvmm_owner *owner; 75 os_rwl_t lock; 76 77 /* Comm */ 78 os_vmobj_t *commvmobj; 79 80 /* Kernel */ 81 struct vmspace *vm; 82 gpaddr_t gpa_begin; 83 gpaddr_t gpa_end; 84 85 /* Host Mappings */ 86 struct nvmm_hmapping hmap[NVMM_MAX_HMAPPINGS]; 87 88 /* CPU */ 89 volatile unsigned int ncpus; 90 struct nvmm_cpu cpus[NVMM_MAX_VCPUS]; 91 92 /* Implementation-specific */ 93 void *machdata; 94 }; 95 96 struct nvmm_impl { 97 const char *name; 98 bool (*ident)(void); 99 void (*init)(void); 100 void (*fini)(void); 101 void (*capability)(struct nvmm_capability *); 102 103 size_t mach_conf_max; 104 const size_t *mach_conf_sizes; 105 106 size_t vcpu_conf_max; 107 const size_t *vcpu_conf_sizes; 108 109 size_t state_size; 110 111 void (*machine_create)(struct nvmm_machine *); 112 void (*machine_destroy)(struct nvmm_machine *); 113 int (*machine_configure)(struct nvmm_machine *, uint64_t, void *); 114 115 int (*vcpu_create)(struct nvmm_machine *, struct nvmm_cpu *); 116 void (*vcpu_destroy)(struct nvmm_machine *, struct nvmm_cpu *); 117 int (*vcpu_configure)(struct nvmm_cpu *, uint64_t, void *); 118 void (*vcpu_setstate)(struct nvmm_cpu *); 119 void (*vcpu_getstate)(struct nvmm_cpu *); 120 int (*vcpu_inject)(struct nvmm_cpu *); 121 int (*vcpu_run)(struct nvmm_machine *, struct nvmm_cpu *, 122 struct nvmm_vcpu_exit *); 123 }; 124 125 #if defined(__x86_64__) 126 extern const struct nvmm_impl nvmm_x86_svm; 127 extern const struct nvmm_impl nvmm_x86_vmx; 128 #endif 129 130 #endif /* _NVMM_INTERNAL_H_ */ 131