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