xref: /dragonfly/sys/dev/virtual/nvmm/nvmm_internal.h (revision 7d3e9a5b)
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 
42 #if defined(__NetBSD__)
43 #define NVMM_MAX_RAM		(128ULL * (1 << 30))
44 #elif defined(__DragonFly__)
45 #define NVMM_MAX_RAM		(127ULL * 1024ULL * (1 << 30))
46 #else
47 #error "OS dependency for NVMM_MAX_RAM required"
48 #endif
49 
50 #define NVMM_COMM_PAGE_SIZE	\
51 	(roundup(sizeof(struct nvmm_comm_page), PAGE_SIZE))
52 
53 struct nvmm_owner {
54 	pid_t pid;
55 };
56 
57 struct nvmm_cpu {
58 	/* Shared. */
59 	bool present;
60 	nvmm_cpuid_t cpuid;
61 	os_mtx_t lock;
62 
63 	/* Comm page. */
64 	struct nvmm_comm_page *comm;
65 
66 	/* Last host CPU on which the VCPU ran. */
67 	int hcpu_last;
68 
69 	/* Implementation-specific. */
70 	void *cpudata;
71 };
72 
73 struct nvmm_hmapping {
74 	bool present;
75 	uintptr_t hva;
76 	size_t size;
77 	os_vmobj_t *vmobj;
78 };
79 
80 struct nvmm_machine {
81 	bool present;
82 	nvmm_machid_t machid;
83 	time_t time;
84 	struct nvmm_owner *owner;
85 	os_rwl_t lock;
86 
87 	/* Comm */
88 	os_vmobj_t *commvmobj;
89 
90 	/* Kernel */
91 	struct vmspace *vm;
92 	gpaddr_t gpa_begin;
93 	gpaddr_t gpa_end;
94 
95 	/* Host Mappings */
96 	struct nvmm_hmapping hmap[NVMM_MAX_HMAPPINGS];
97 
98 	/* CPU */
99 	volatile unsigned int ncpus;
100 	struct nvmm_cpu cpus[NVMM_MAX_VCPUS];
101 
102 	/* Implementation-specific */
103 	void *machdata;
104 };
105 
106 struct nvmm_impl {
107 	const char *name;
108 	bool (*ident)(void);
109 	void (*init)(void);
110 	void (*fini)(void);
111 	void (*capability)(struct nvmm_capability *);
112 
113 	size_t mach_conf_max;
114 	const size_t *mach_conf_sizes;
115 
116 	size_t vcpu_conf_max;
117 	const size_t *vcpu_conf_sizes;
118 
119 	size_t state_size;
120 
121 	void (*machine_create)(struct nvmm_machine *);
122 	void (*machine_destroy)(struct nvmm_machine *);
123 	int (*machine_configure)(struct nvmm_machine *, uint64_t, void *);
124 
125 	int (*vcpu_create)(struct nvmm_machine *, struct nvmm_cpu *);
126 	void (*vcpu_destroy)(struct nvmm_machine *, struct nvmm_cpu *);
127 	int (*vcpu_configure)(struct nvmm_cpu *, uint64_t, void *);
128 	void (*vcpu_setstate)(struct nvmm_cpu *);
129 	void (*vcpu_getstate)(struct nvmm_cpu *);
130 	int (*vcpu_inject)(struct nvmm_cpu *);
131 	int (*vcpu_run)(struct nvmm_machine *, struct nvmm_cpu *,
132 	    struct nvmm_vcpu_exit *);
133 };
134 
135 #if defined(__x86_64__)
136 extern const struct nvmm_impl nvmm_x86_svm;
137 extern const struct nvmm_impl nvmm_x86_vmx;
138 #endif
139 
140 extern struct nvmm_owner nvmm_root_owner;
141 extern volatile unsigned int nmachines;
142 extern const struct nvmm_impl *nvmm_impl;
143 
144 const struct nvmm_impl *nvmm_ident(void);
145 int	nvmm_init(void);
146 void	nvmm_fini(void);
147 int	nvmm_ioctl(struct nvmm_owner *, unsigned long, void *);
148 void	nvmm_kill_machines(struct nvmm_owner *);
149 
150 #endif /* _NVMM_INTERNAL_H_ */
151