nvmm_x86.h revision 1.15.2.2 1 1.15.2.2 christos /* $NetBSD: nvmm_x86.h,v 1.15.2.2 2019/06/10 22:07:14 christos Exp $ */
2 1.15.2.2 christos
3 1.15.2.2 christos /*
4 1.15.2.2 christos * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.15.2.2 christos * All rights reserved.
6 1.15.2.2 christos *
7 1.15.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.15.2.2 christos * by Maxime Villard.
9 1.15.2.2 christos *
10 1.15.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.15.2.2 christos * modification, are permitted provided that the following conditions
12 1.15.2.2 christos * are met:
13 1.15.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.15.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.15.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.15.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.15.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.15.2.2 christos *
19 1.15.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.15.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.15.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.15.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.15.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.15.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.15.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.15.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.15.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.15.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.15.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.15.2.2 christos */
31 1.15.2.2 christos
32 1.15.2.2 christos #ifndef _NVMM_X86_H_
33 1.15.2.2 christos #define _NVMM_X86_H_
34 1.15.2.2 christos
35 1.15.2.2 christos /* --------------------------------------------------------------------- */
36 1.15.2.2 christos
37 1.15.2.2 christos #ifndef ASM_NVMM
38 1.15.2.2 christos
39 1.15.2.2 christos struct nvmm_exit_memory {
40 1.15.2.2 christos int prot;
41 1.15.2.2 christos gpaddr_t gpa;
42 1.15.2.2 christos uint8_t inst_len;
43 1.15.2.2 christos uint8_t inst_bytes[15];
44 1.15.2.2 christos };
45 1.15.2.2 christos
46 1.15.2.2 christos enum nvmm_exit_io_type {
47 1.15.2.2 christos NVMM_EXIT_IO_IN,
48 1.15.2.2 christos NVMM_EXIT_IO_OUT
49 1.15.2.2 christos };
50 1.15.2.2 christos
51 1.15.2.2 christos struct nvmm_exit_io {
52 1.15.2.2 christos enum nvmm_exit_io_type type;
53 1.15.2.2 christos uint16_t port;
54 1.15.2.2 christos int seg;
55 1.15.2.2 christos uint8_t address_size;
56 1.15.2.2 christos uint8_t operand_size;
57 1.15.2.2 christos bool rep;
58 1.15.2.2 christos bool str;
59 1.15.2.2 christos uint64_t npc;
60 1.15.2.2 christos };
61 1.15.2.2 christos
62 1.15.2.2 christos enum nvmm_exit_msr_type {
63 1.15.2.2 christos NVMM_EXIT_MSR_RDMSR,
64 1.15.2.2 christos NVMM_EXIT_MSR_WRMSR
65 1.15.2.2 christos };
66 1.15.2.2 christos
67 1.15.2.2 christos struct nvmm_exit_msr {
68 1.15.2.2 christos enum nvmm_exit_msr_type type;
69 1.15.2.2 christos uint64_t msr;
70 1.15.2.2 christos uint64_t val;
71 1.15.2.2 christos uint64_t npc;
72 1.15.2.2 christos };
73 1.15.2.2 christos
74 1.15.2.2 christos struct nvmm_exit_insn {
75 1.15.2.2 christos uint64_t npc;
76 1.15.2.2 christos };
77 1.15.2.2 christos
78 1.15.2.2 christos struct nvmm_exit_invalid {
79 1.15.2.2 christos uint64_t hwcode;
80 1.15.2.2 christos };
81 1.15.2.2 christos
82 1.15.2.2 christos union nvmm_exit_md {
83 1.15.2.2 christos struct nvmm_exit_memory mem;
84 1.15.2.2 christos struct nvmm_exit_io io;
85 1.15.2.2 christos struct nvmm_exit_msr msr;
86 1.15.2.2 christos struct nvmm_exit_insn insn;
87 1.15.2.2 christos struct nvmm_exit_invalid inv;
88 1.15.2.2 christos };
89 1.15.2.2 christos
90 1.15.2.2 christos #define NVMM_EXIT_MONITOR 0x0000000000001000ULL
91 1.15.2.2 christos #define NVMM_EXIT_MWAIT 0x0000000000001001ULL
92 1.15.2.2 christos #define NVMM_EXIT_MWAIT_COND 0x0000000000001002ULL
93 1.15.2.2 christos
94 1.15.2.2 christos struct nvmm_cap_md {
95 1.15.2.2 christos uint64_t xcr0_mask;
96 1.15.2.2 christos uint64_t mxcsr_mask;
97 1.15.2.2 christos uint64_t conf_cpuid_maxops;
98 1.15.2.2 christos uint64_t rsvd[5];
99 1.15.2.2 christos };
100 1.15.2.2 christos
101 1.15.2.2 christos #endif
102 1.15.2.2 christos
103 1.15.2.2 christos /* --------------------------------------------------------------------- */
104 1.15.2.2 christos
105 1.15.2.2 christos /* Segments. */
106 1.15.2.2 christos #define NVMM_X64_SEG_ES 0
107 1.15.2.2 christos #define NVMM_X64_SEG_CS 1
108 1.15.2.2 christos #define NVMM_X64_SEG_SS 2
109 1.15.2.2 christos #define NVMM_X64_SEG_DS 3
110 1.15.2.2 christos #define NVMM_X64_SEG_FS 4
111 1.15.2.2 christos #define NVMM_X64_SEG_GS 5
112 1.15.2.2 christos #define NVMM_X64_SEG_GDT 6
113 1.15.2.2 christos #define NVMM_X64_SEG_IDT 7
114 1.15.2.2 christos #define NVMM_X64_SEG_LDT 8
115 1.15.2.2 christos #define NVMM_X64_SEG_TR 9
116 1.15.2.2 christos #define NVMM_X64_NSEG 10
117 1.15.2.2 christos
118 1.15.2.2 christos /* General Purpose Registers. */
119 1.15.2.2 christos #define NVMM_X64_GPR_RAX 0
120 1.15.2.2 christos #define NVMM_X64_GPR_RCX 1
121 1.15.2.2 christos #define NVMM_X64_GPR_RDX 2
122 1.15.2.2 christos #define NVMM_X64_GPR_RBX 3
123 1.15.2.2 christos #define NVMM_X64_GPR_RSP 4
124 1.15.2.2 christos #define NVMM_X64_GPR_RBP 5
125 1.15.2.2 christos #define NVMM_X64_GPR_RSI 6
126 1.15.2.2 christos #define NVMM_X64_GPR_RDI 7
127 1.15.2.2 christos #define NVMM_X64_GPR_R8 8
128 1.15.2.2 christos #define NVMM_X64_GPR_R9 9
129 1.15.2.2 christos #define NVMM_X64_GPR_R10 10
130 1.15.2.2 christos #define NVMM_X64_GPR_R11 11
131 1.15.2.2 christos #define NVMM_X64_GPR_R12 12
132 1.15.2.2 christos #define NVMM_X64_GPR_R13 13
133 1.15.2.2 christos #define NVMM_X64_GPR_R14 14
134 1.15.2.2 christos #define NVMM_X64_GPR_R15 15
135 1.15.2.2 christos #define NVMM_X64_GPR_RIP 16
136 1.15.2.2 christos #define NVMM_X64_GPR_RFLAGS 17
137 1.15.2.2 christos #define NVMM_X64_NGPR 18
138 1.15.2.2 christos
139 1.15.2.2 christos /* Control Registers. */
140 1.15.2.2 christos #define NVMM_X64_CR_CR0 0
141 1.15.2.2 christos #define NVMM_X64_CR_CR2 1
142 1.15.2.2 christos #define NVMM_X64_CR_CR3 2
143 1.15.2.2 christos #define NVMM_X64_CR_CR4 3
144 1.15.2.2 christos #define NVMM_X64_CR_CR8 4
145 1.15.2.2 christos #define NVMM_X64_CR_XCR0 5
146 1.15.2.2 christos #define NVMM_X64_NCR 6
147 1.15.2.2 christos
148 1.15.2.2 christos /* Debug Registers. */
149 1.15.2.2 christos #define NVMM_X64_DR_DR0 0
150 1.15.2.2 christos #define NVMM_X64_DR_DR1 1
151 1.15.2.2 christos #define NVMM_X64_DR_DR2 2
152 1.15.2.2 christos #define NVMM_X64_DR_DR3 3
153 1.15.2.2 christos #define NVMM_X64_DR_DR6 4
154 1.15.2.2 christos #define NVMM_X64_DR_DR7 5
155 1.15.2.2 christos #define NVMM_X64_NDR 6
156 1.15.2.2 christos
157 1.15.2.2 christos /* MSRs. */
158 1.15.2.2 christos #define NVMM_X64_MSR_EFER 0
159 1.15.2.2 christos #define NVMM_X64_MSR_STAR 1
160 1.15.2.2 christos #define NVMM_X64_MSR_LSTAR 2
161 1.15.2.2 christos #define NVMM_X64_MSR_CSTAR 3
162 1.15.2.2 christos #define NVMM_X64_MSR_SFMASK 4
163 1.15.2.2 christos #define NVMM_X64_MSR_KERNELGSBASE 5
164 1.15.2.2 christos #define NVMM_X64_MSR_SYSENTER_CS 6
165 1.15.2.2 christos #define NVMM_X64_MSR_SYSENTER_ESP 7
166 1.15.2.2 christos #define NVMM_X64_MSR_SYSENTER_EIP 8
167 1.15.2.2 christos #define NVMM_X64_MSR_PAT 9
168 1.15.2.2 christos #define NVMM_X64_MSR_TSC 10
169 1.15.2.2 christos #define NVMM_X64_NMSR 11
170 1.15.2.2 christos
171 1.15.2.2 christos #ifndef ASM_NVMM
172 1.15.2.2 christos
173 1.15.2.2 christos #include <sys/types.h>
174 1.15.2.2 christos #include <x86/cpu_extended_state.h>
175 1.15.2.2 christos
176 1.15.2.2 christos struct nvmm_x64_state_seg {
177 1.15.2.2 christos uint16_t selector;
178 1.15.2.2 christos struct { /* hidden */
179 1.15.2.2 christos uint16_t type:4;
180 1.15.2.2 christos uint16_t s:1;
181 1.15.2.2 christos uint16_t dpl:2;
182 1.15.2.2 christos uint16_t p:1;
183 1.15.2.2 christos uint16_t avl:1;
184 1.15.2.2 christos uint16_t l:1;
185 1.15.2.2 christos uint16_t def:1;
186 1.15.2.2 christos uint16_t g:1;
187 1.15.2.2 christos uint16_t rsvd:4;
188 1.15.2.2 christos } attrib;
189 1.15.2.2 christos uint32_t limit; /* hidden */
190 1.15.2.2 christos uint64_t base; /* hidden */
191 1.15.2.2 christos };
192 1.15.2.2 christos
193 1.15.2.2 christos struct nvmm_x64_state_intr {
194 1.15.2.2 christos uint64_t int_shadow:1;
195 1.15.2.2 christos uint64_t int_window_exiting:1;
196 1.15.2.2 christos uint64_t nmi_window_exiting:1;
197 1.15.2.2 christos uint64_t evt_pending:1;
198 1.15.2.2 christos uint64_t rsvd:60;
199 1.15.2.2 christos };
200 1.15.2.2 christos
201 1.15.2.2 christos /* VM exit state indexes. */
202 1.15.2.2 christos #define NVMM_X64_EXITSTATE_CR8 0
203 1.15.2.2 christos #define NVMM_X64_EXITSTATE_RFLAGS 1
204 1.15.2.2 christos #define NVMM_X64_EXITSTATE_INT_SHADOW 2
205 1.15.2.2 christos #define NVMM_X64_EXITSTATE_INT_WINDOW_EXIT 3
206 1.15.2.2 christos #define NVMM_X64_EXITSTATE_NMI_WINDOW_EXIT 4
207 1.15.2.2 christos #define NVMM_X64_EXITSTATE_EVT_PENDING 5
208 1.15.2.2 christos
209 1.15.2.2 christos /* Flags. */
210 1.15.2.2 christos #define NVMM_X64_STATE_SEGS 0x01
211 1.15.2.2 christos #define NVMM_X64_STATE_GPRS 0x02
212 1.15.2.2 christos #define NVMM_X64_STATE_CRS 0x04
213 1.15.2.2 christos #define NVMM_X64_STATE_DRS 0x08
214 1.15.2.2 christos #define NVMM_X64_STATE_MSRS 0x10
215 1.15.2.2 christos #define NVMM_X64_STATE_INTR 0x20
216 1.15.2.2 christos #define NVMM_X64_STATE_FPU 0x40
217 1.15.2.2 christos #define NVMM_X64_STATE_ALL \
218 1.15.2.2 christos (NVMM_X64_STATE_SEGS | NVMM_X64_STATE_GPRS | NVMM_X64_STATE_CRS | \
219 1.15.2.2 christos NVMM_X64_STATE_DRS | NVMM_X64_STATE_MSRS | NVMM_X64_STATE_INTR | \
220 1.15.2.2 christos NVMM_X64_STATE_FPU)
221 1.15.2.2 christos
222 1.15.2.2 christos struct nvmm_x64_state {
223 1.15.2.2 christos struct nvmm_x64_state_seg segs[NVMM_X64_NSEG];
224 1.15.2.2 christos uint64_t gprs[NVMM_X64_NGPR];
225 1.15.2.2 christos uint64_t crs[NVMM_X64_NCR];
226 1.15.2.2 christos uint64_t drs[NVMM_X64_NDR];
227 1.15.2.2 christos uint64_t msrs[NVMM_X64_NMSR];
228 1.15.2.2 christos struct nvmm_x64_state_intr intr;
229 1.15.2.2 christos struct fxsave fpu;
230 1.15.2.2 christos };
231 1.15.2.2 christos
232 1.15.2.2 christos #define nvmm_vcpu_state nvmm_x64_state
233 1.15.2.2 christos
234 1.15.2.2 christos #define NVMM_MACH_CONF_X86_CPUID NVMM_MACH_CONF_MD_BEGIN
235 1.15.2.2 christos #define NVMM_X86_NCONF 1
236 1.15.2.2 christos
237 1.15.2.2 christos struct nvmm_mach_conf_x86_cpuid {
238 1.15.2.2 christos uint32_t leaf;
239 1.15.2.2 christos struct {
240 1.15.2.2 christos uint32_t eax;
241 1.15.2.2 christos uint32_t ebx;
242 1.15.2.2 christos uint32_t ecx;
243 1.15.2.2 christos uint32_t edx;
244 1.15.2.2 christos } set;
245 1.15.2.2 christos struct {
246 1.15.2.2 christos uint32_t eax;
247 1.15.2.2 christos uint32_t ebx;
248 1.15.2.2 christos uint32_t ecx;
249 1.15.2.2 christos uint32_t edx;
250 1.15.2.2 christos } del;
251 1.15.2.2 christos };
252 1.15.2.2 christos
253 1.15.2.2 christos #ifdef _KERNEL
254 1.15.2.2 christos struct nvmm_x86_cpuid_mask {
255 1.15.2.2 christos uint32_t eax;
256 1.15.2.2 christos uint32_t ebx;
257 1.15.2.2 christos uint32_t ecx;
258 1.15.2.2 christos uint32_t edx;
259 1.15.2.2 christos };
260 1.15.2.2 christos extern const struct nvmm_x64_state nvmm_x86_reset_state;
261 1.15.2.2 christos extern const struct nvmm_x86_cpuid_mask nvmm_cpuid_00000001;
262 1.15.2.2 christos extern const struct nvmm_x86_cpuid_mask nvmm_cpuid_00000007;
263 1.15.2.2 christos extern const struct nvmm_x86_cpuid_mask nvmm_cpuid_80000001;
264 1.15.2.2 christos bool nvmm_x86_pat_validate(uint64_t);
265 1.15.2.2 christos #endif
266 1.15.2.2 christos
267 1.15.2.2 christos #endif /* ASM_NVMM */
268 1.15.2.2 christos
269 1.15.2.2 christos #endif /* _NVMM_X86_H_ */
270