nvmm_x86.h revision 1.16 1 1.16 maxv /* $NetBSD: nvmm_x86.h,v 1.16 2019/10/23 07:01:11 maxv Exp $ */
2 1.1 maxv
3 1.1 maxv /*
4 1.16 maxv * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
5 1.1 maxv * All rights reserved.
6 1.1 maxv *
7 1.1 maxv * This code is derived from software contributed to The NetBSD Foundation
8 1.1 maxv * by Maxime Villard.
9 1.1 maxv *
10 1.1 maxv * Redistribution and use in source and binary forms, with or without
11 1.1 maxv * modification, are permitted provided that the following conditions
12 1.1 maxv * are met:
13 1.1 maxv * 1. Redistributions of source code must retain the above copyright
14 1.1 maxv * notice, this list of conditions and the following disclaimer.
15 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 maxv * notice, this list of conditions and the following disclaimer in the
17 1.1 maxv * documentation and/or other materials provided with the distribution.
18 1.1 maxv *
19 1.1 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 maxv * POSSIBILITY OF SUCH DAMAGE.
30 1.1 maxv */
31 1.1 maxv
32 1.1 maxv #ifndef _NVMM_X86_H_
33 1.1 maxv #define _NVMM_X86_H_
34 1.1 maxv
35 1.16 maxv /* -------------------------------------------------------------------------- */
36 1.12 maxv
37 1.12 maxv #ifndef ASM_NVMM
38 1.12 maxv
39 1.16 maxv struct nvmm_x86_exit_memory {
40 1.12 maxv int prot;
41 1.12 maxv gpaddr_t gpa;
42 1.12 maxv uint8_t inst_len;
43 1.12 maxv uint8_t inst_bytes[15];
44 1.12 maxv };
45 1.12 maxv
46 1.16 maxv struct nvmm_x86_exit_io {
47 1.16 maxv bool in;
48 1.12 maxv uint16_t port;
49 1.16 maxv int8_t seg;
50 1.12 maxv uint8_t address_size;
51 1.12 maxv uint8_t operand_size;
52 1.12 maxv bool rep;
53 1.12 maxv bool str;
54 1.12 maxv uint64_t npc;
55 1.12 maxv };
56 1.12 maxv
57 1.16 maxv struct nvmm_x86_exit_rdmsr {
58 1.16 maxv uint32_t msr;
59 1.16 maxv uint64_t npc;
60 1.12 maxv };
61 1.12 maxv
62 1.16 maxv struct nvmm_x86_exit_wrmsr {
63 1.16 maxv uint32_t msr;
64 1.12 maxv uint64_t val;
65 1.12 maxv uint64_t npc;
66 1.12 maxv };
67 1.12 maxv
68 1.16 maxv struct nvmm_x86_exit_insn {
69 1.12 maxv uint64_t npc;
70 1.12 maxv };
71 1.12 maxv
72 1.16 maxv struct nvmm_x86_exit_invalid {
73 1.12 maxv uint64_t hwcode;
74 1.12 maxv };
75 1.12 maxv
76 1.16 maxv /* Generic. */
77 1.16 maxv #define NVMM_VCPU_EXIT_NONE 0x0000000000000000ULL
78 1.16 maxv #define NVMM_VCPU_EXIT_INVALID 0xFFFFFFFFFFFFFFFFULL
79 1.16 maxv /* x86: operations. */
80 1.16 maxv #define NVMM_VCPU_EXIT_MEMORY 0x0000000000000001ULL
81 1.16 maxv #define NVMM_VCPU_EXIT_IO 0x0000000000000002ULL
82 1.16 maxv /* x86: changes in VCPU state. */
83 1.16 maxv #define NVMM_VCPU_EXIT_SHUTDOWN 0x0000000000001000ULL
84 1.16 maxv #define NVMM_VCPU_EXIT_INT_READY 0x0000000000001001ULL
85 1.16 maxv #define NVMM_VCPU_EXIT_NMI_READY 0x0000000000001002ULL
86 1.16 maxv #define NVMM_VCPU_EXIT_HALTED 0x0000000000001003ULL
87 1.16 maxv /* x86: instructions. */
88 1.16 maxv #define NVMM_VCPU_EXIT_RDMSR 0x0000000000002000ULL
89 1.16 maxv #define NVMM_VCPU_EXIT_WRMSR 0x0000000000002001ULL
90 1.16 maxv #define NVMM_VCPU_EXIT_MONITOR 0x0000000000002002ULL
91 1.16 maxv #define NVMM_VCPU_EXIT_MWAIT 0x0000000000002003ULL
92 1.16 maxv #define NVMM_VCPU_EXIT_CPUID 0x0000000000002004ULL
93 1.16 maxv
94 1.16 maxv struct nvmm_x86_exit {
95 1.16 maxv uint64_t reason;
96 1.16 maxv union {
97 1.16 maxv struct nvmm_x86_exit_memory mem;
98 1.16 maxv struct nvmm_x86_exit_io io;
99 1.16 maxv struct nvmm_x86_exit_rdmsr rdmsr;
100 1.16 maxv struct nvmm_x86_exit_wrmsr wrmsr;
101 1.16 maxv struct nvmm_x86_exit_insn insn;
102 1.16 maxv struct nvmm_x86_exit_invalid inv;
103 1.16 maxv } u;
104 1.16 maxv uint64_t exitstate[8];
105 1.16 maxv };
106 1.16 maxv
107 1.16 maxv #define NVMM_VCPU_EVENT_EXCP 0
108 1.16 maxv #define NVMM_VCPU_EVENT_INTR 1
109 1.16 maxv
110 1.16 maxv struct nvmm_x86_event {
111 1.16 maxv u_int type;
112 1.16 maxv uint8_t vector;
113 1.16 maxv union {
114 1.16 maxv struct {
115 1.16 maxv uint64_t error;
116 1.16 maxv } excp;
117 1.16 maxv } u;
118 1.12 maxv };
119 1.12 maxv
120 1.12 maxv struct nvmm_cap_md {
121 1.12 maxv uint64_t xcr0_mask;
122 1.16 maxv uint32_t mxcsr_mask;
123 1.16 maxv uint32_t conf_cpuid_maxops;
124 1.16 maxv uint64_t rsvd[6];
125 1.12 maxv };
126 1.12 maxv
127 1.12 maxv #endif
128 1.12 maxv
129 1.16 maxv /* -------------------------------------------------------------------------- */
130 1.16 maxv
131 1.16 maxv /*
132 1.16 maxv * Segment state indexes. We use X64 as naming convention, not to confuse with
133 1.16 maxv * X86 which originally implied 32bit.
134 1.16 maxv */
135 1.12 maxv
136 1.1 maxv /* Segments. */
137 1.7 maxv #define NVMM_X64_SEG_ES 0
138 1.7 maxv #define NVMM_X64_SEG_CS 1
139 1.7 maxv #define NVMM_X64_SEG_SS 2
140 1.7 maxv #define NVMM_X64_SEG_DS 3
141 1.7 maxv #define NVMM_X64_SEG_FS 4
142 1.7 maxv #define NVMM_X64_SEG_GS 5
143 1.1 maxv #define NVMM_X64_SEG_GDT 6
144 1.1 maxv #define NVMM_X64_SEG_IDT 7
145 1.1 maxv #define NVMM_X64_SEG_LDT 8
146 1.1 maxv #define NVMM_X64_SEG_TR 9
147 1.1 maxv #define NVMM_X64_NSEG 10
148 1.1 maxv
149 1.1 maxv /* General Purpose Registers. */
150 1.1 maxv #define NVMM_X64_GPR_RAX 0
151 1.4 maxv #define NVMM_X64_GPR_RCX 1
152 1.4 maxv #define NVMM_X64_GPR_RDX 2
153 1.4 maxv #define NVMM_X64_GPR_RBX 3
154 1.4 maxv #define NVMM_X64_GPR_RSP 4
155 1.4 maxv #define NVMM_X64_GPR_RBP 5
156 1.4 maxv #define NVMM_X64_GPR_RSI 6
157 1.4 maxv #define NVMM_X64_GPR_RDI 7
158 1.4 maxv #define NVMM_X64_GPR_R8 8
159 1.4 maxv #define NVMM_X64_GPR_R9 9
160 1.4 maxv #define NVMM_X64_GPR_R10 10
161 1.4 maxv #define NVMM_X64_GPR_R11 11
162 1.4 maxv #define NVMM_X64_GPR_R12 12
163 1.4 maxv #define NVMM_X64_GPR_R13 13
164 1.4 maxv #define NVMM_X64_GPR_R14 14
165 1.4 maxv #define NVMM_X64_GPR_R15 15
166 1.1 maxv #define NVMM_X64_GPR_RIP 16
167 1.1 maxv #define NVMM_X64_GPR_RFLAGS 17
168 1.1 maxv #define NVMM_X64_NGPR 18
169 1.1 maxv
170 1.1 maxv /* Control Registers. */
171 1.1 maxv #define NVMM_X64_CR_CR0 0
172 1.1 maxv #define NVMM_X64_CR_CR2 1
173 1.1 maxv #define NVMM_X64_CR_CR3 2
174 1.1 maxv #define NVMM_X64_CR_CR4 3
175 1.1 maxv #define NVMM_X64_CR_CR8 4
176 1.1 maxv #define NVMM_X64_CR_XCR0 5
177 1.1 maxv #define NVMM_X64_NCR 6
178 1.1 maxv
179 1.1 maxv /* Debug Registers. */
180 1.1 maxv #define NVMM_X64_DR_DR0 0
181 1.1 maxv #define NVMM_X64_DR_DR1 1
182 1.1 maxv #define NVMM_X64_DR_DR2 2
183 1.1 maxv #define NVMM_X64_DR_DR3 3
184 1.1 maxv #define NVMM_X64_DR_DR6 4
185 1.1 maxv #define NVMM_X64_DR_DR7 5
186 1.1 maxv #define NVMM_X64_NDR 6
187 1.1 maxv
188 1.1 maxv /* MSRs. */
189 1.1 maxv #define NVMM_X64_MSR_EFER 0
190 1.1 maxv #define NVMM_X64_MSR_STAR 1
191 1.1 maxv #define NVMM_X64_MSR_LSTAR 2
192 1.1 maxv #define NVMM_X64_MSR_CSTAR 3
193 1.1 maxv #define NVMM_X64_MSR_SFMASK 4
194 1.1 maxv #define NVMM_X64_MSR_KERNELGSBASE 5
195 1.1 maxv #define NVMM_X64_MSR_SYSENTER_CS 6
196 1.1 maxv #define NVMM_X64_MSR_SYSENTER_ESP 7
197 1.1 maxv #define NVMM_X64_MSR_SYSENTER_EIP 8
198 1.1 maxv #define NVMM_X64_MSR_PAT 9
199 1.9 maxv #define NVMM_X64_MSR_TSC 10
200 1.9 maxv #define NVMM_X64_NMSR 11
201 1.1 maxv
202 1.1 maxv #ifndef ASM_NVMM
203 1.1 maxv
204 1.1 maxv #include <sys/types.h>
205 1.1 maxv #include <x86/cpu_extended_state.h>
206 1.1 maxv
207 1.1 maxv struct nvmm_x64_state_seg {
208 1.7 maxv uint16_t selector;
209 1.1 maxv struct { /* hidden */
210 1.7 maxv uint16_t type:4;
211 1.7 maxv uint16_t s:1;
212 1.7 maxv uint16_t dpl:2;
213 1.7 maxv uint16_t p:1;
214 1.7 maxv uint16_t avl:1;
215 1.7 maxv uint16_t l:1;
216 1.7 maxv uint16_t def:1;
217 1.7 maxv uint16_t g:1;
218 1.7 maxv uint16_t rsvd:4;
219 1.1 maxv } attrib;
220 1.7 maxv uint32_t limit; /* hidden */
221 1.1 maxv uint64_t base; /* hidden */
222 1.1 maxv };
223 1.1 maxv
224 1.11 maxv struct nvmm_x64_state_intr {
225 1.11 maxv uint64_t int_shadow:1;
226 1.11 maxv uint64_t int_window_exiting:1;
227 1.11 maxv uint64_t nmi_window_exiting:1;
228 1.11 maxv uint64_t evt_pending:1;
229 1.11 maxv uint64_t rsvd:60;
230 1.11 maxv };
231 1.11 maxv
232 1.1 maxv /* VM exit state indexes. */
233 1.3 maxv #define NVMM_X64_EXITSTATE_CR8 0
234 1.3 maxv #define NVMM_X64_EXITSTATE_RFLAGS 1
235 1.3 maxv #define NVMM_X64_EXITSTATE_INT_SHADOW 2
236 1.3 maxv #define NVMM_X64_EXITSTATE_INT_WINDOW_EXIT 3
237 1.3 maxv #define NVMM_X64_EXITSTATE_NMI_WINDOW_EXIT 4
238 1.11 maxv #define NVMM_X64_EXITSTATE_EVT_PENDING 5
239 1.1 maxv
240 1.1 maxv /* Flags. */
241 1.1 maxv #define NVMM_X64_STATE_SEGS 0x01
242 1.1 maxv #define NVMM_X64_STATE_GPRS 0x02
243 1.1 maxv #define NVMM_X64_STATE_CRS 0x04
244 1.1 maxv #define NVMM_X64_STATE_DRS 0x08
245 1.1 maxv #define NVMM_X64_STATE_MSRS 0x10
246 1.11 maxv #define NVMM_X64_STATE_INTR 0x20
247 1.1 maxv #define NVMM_X64_STATE_FPU 0x40
248 1.1 maxv #define NVMM_X64_STATE_ALL \
249 1.1 maxv (NVMM_X64_STATE_SEGS | NVMM_X64_STATE_GPRS | NVMM_X64_STATE_CRS | \
250 1.11 maxv NVMM_X64_STATE_DRS | NVMM_X64_STATE_MSRS | NVMM_X64_STATE_INTR | \
251 1.1 maxv NVMM_X64_STATE_FPU)
252 1.1 maxv
253 1.1 maxv struct nvmm_x64_state {
254 1.1 maxv struct nvmm_x64_state_seg segs[NVMM_X64_NSEG];
255 1.1 maxv uint64_t gprs[NVMM_X64_NGPR];
256 1.1 maxv uint64_t crs[NVMM_X64_NCR];
257 1.1 maxv uint64_t drs[NVMM_X64_NDR];
258 1.1 maxv uint64_t msrs[NVMM_X64_NMSR];
259 1.11 maxv struct nvmm_x64_state_intr intr;
260 1.1 maxv struct fxsave fpu;
261 1.1 maxv };
262 1.1 maxv
263 1.16 maxv #define NVMM_VCPU_CONF_CPUID NVMM_VCPU_CONF_MD_BEGIN
264 1.14 maxv
265 1.16 maxv struct nvmm_vcpu_conf_cpuid {
266 1.16 maxv /* The options. */
267 1.16 maxv uint32_t mask:1;
268 1.16 maxv uint32_t exit:1;
269 1.16 maxv uint32_t rsvd:30;
270 1.1 maxv
271 1.16 maxv /* The leaf. */
272 1.1 maxv uint32_t leaf;
273 1.16 maxv
274 1.16 maxv /* The params. */
275 1.16 maxv union {
276 1.16 maxv struct {
277 1.16 maxv struct {
278 1.16 maxv uint32_t eax;
279 1.16 maxv uint32_t ebx;
280 1.16 maxv uint32_t ecx;
281 1.16 maxv uint32_t edx;
282 1.16 maxv } set;
283 1.16 maxv struct {
284 1.16 maxv uint32_t eax;
285 1.16 maxv uint32_t ebx;
286 1.16 maxv uint32_t ecx;
287 1.16 maxv uint32_t edx;
288 1.16 maxv } del;
289 1.16 maxv } mask;
290 1.16 maxv } u;
291 1.16 maxv };
292 1.16 maxv
293 1.16 maxv #define nvmm_vcpu_exit nvmm_x86_exit
294 1.16 maxv #define nvmm_vcpu_event nvmm_x86_event
295 1.16 maxv #define nvmm_vcpu_state nvmm_x64_state
296 1.1 maxv
297 1.6 maxv #ifdef _KERNEL
298 1.16 maxv #define NVMM_X86_MACH_NCONF 0
299 1.16 maxv #define NVMM_X86_VCPU_NCONF 1
300 1.8 maxv struct nvmm_x86_cpuid_mask {
301 1.8 maxv uint32_t eax;
302 1.8 maxv uint32_t ebx;
303 1.8 maxv uint32_t ecx;
304 1.8 maxv uint32_t edx;
305 1.8 maxv };
306 1.6 maxv extern const struct nvmm_x64_state nvmm_x86_reset_state;
307 1.8 maxv extern const struct nvmm_x86_cpuid_mask nvmm_cpuid_00000001;
308 1.8 maxv extern const struct nvmm_x86_cpuid_mask nvmm_cpuid_00000007;
309 1.8 maxv extern const struct nvmm_x86_cpuid_mask nvmm_cpuid_80000001;
310 1.10 maxv bool nvmm_x86_pat_validate(uint64_t);
311 1.6 maxv #endif
312 1.6 maxv
313 1.1 maxv #endif /* ASM_NVMM */
314 1.1 maxv
315 1.1 maxv #endif /* _NVMM_X86_H_ */
316