Sets some kvm_run out flags before going to userspace.
[freebsd-src/fkvm-freebsd.git] / sys / sys / fkvm.h
blob3a0c921f75d8881acd36c9b234d18898a190ed5d
1 /*-
2 * Copyright (c) 2008 Brent Stephens <brents@rice.edu>
3 * Copyright (c) 2008 Diego Ongaro <diego.ongaro@rice.edu>
4 * Copyright (c) 2008 Kaushik Kumar Ram <kaushik@rice.edu>
5 * Copyright (c) 2008 Oleg Pesok <olegpesok@gmail.com>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #ifndef _FKVM_H_
31 #define _FKVM_H_
33 #include <sys/types.h>
35 #define KVM_NR_INTERRUPTS 256
36 #define MAX_VCPUS 8
38 /* MSR */
40 struct kvm_msr_list {
41 uint32_t nmsrs; /* number of msrs in entries */
42 uint32_t indices[0];
45 struct kvm_msr_entry {
46 uint32_t index;
47 uint32_t reserved;
48 uint64_t data;
51 struct kvm_msrs {
52 uint32_t nmsrs; /* number of msrs in entries */
53 uint32_t pad;
55 struct kvm_msr_entry entries[0];
58 #if defined(_KERNEL) || defined(FKVM_INTERNAL)
59 enum {
60 FKVM_REGS_TYPE_REGS=1,
61 FKVM_REGS_TYPE_SREGS=2,
62 FKVM_REGS_TYPE_MSRS=3,
64 #endif
66 /* get/set regs */
68 enum {
69 KVM_REG_RAX = 0,
70 KVM_REG_RBX = 1,
71 KVM_REG_RCX = 2,
72 KVM_REG_RDX = 3,
73 KVM_REG_RSI = 4,
74 KVM_REG_RDI = 5,
75 KVM_REG_RSP = 6,
76 KVM_REG_RBP = 7,
77 KVM_REG_R8 = 8,
78 KVM_REG_R9 = 9,
79 KVM_REG_R10 = 10,
80 KVM_REG_R11 = 11,
81 KVM_REG_R12 = 12,
82 KVM_REG_R13 = 13,
83 KVM_REG_R14 = 14,
84 KVM_REG_R15 = 15,
85 KVM_REG_RIP = 16,
86 KVM_REG_RFLAGS = 17
89 struct kvm_regs {
90 uint64_t rax;
91 uint64_t rbx;
92 uint64_t rcx;
93 uint64_t rdx;
94 uint64_t rsi;
95 uint64_t rdi;
96 uint64_t rsp;
97 uint64_t rbp;
98 uint64_t r8;
99 uint64_t r9;
100 uint64_t r10;
101 uint64_t r11;
102 uint64_t r12;
103 uint64_t r13;
104 uint64_t r14;
105 uint64_t r15;
106 uint64_t rip;
107 uint64_t rflags;
110 static inline void
111 kvm_regs_set(struct kvm_regs *regs, int idx, uint64_t value)
113 *(((uint64_t*) regs) + idx) = value;
116 static inline uint64_t
117 kvm_regs_get(struct kvm_regs *regs, int idx)
119 return *(((uint64_t*) regs) + idx);
122 /* get/set fpu */
124 struct kvm_fpu {
125 uint8_t fpr[8][16];
126 uint16_t fcw;
127 uint16_t fsw;
128 uint8_t ftwx; /* in fxsave format */
129 uint8_t pad1;
130 uint16_t last_opcode;
131 uint64_t last_ip;
132 uint64_t last_dp;
133 uint8_t xmm[16][16];
134 uint32_t mxcsr;
135 uint32_t pad2;
138 /* get/set sregs */
140 struct kvm_segment {
141 uint64_t base;
142 uint32_t limit;
143 uint16_t selector;
144 uint8_t type;
145 uint8_t present;
146 uint8_t dpl;
147 uint8_t db;
148 uint8_t s;
149 uint8_t l;
150 uint8_t g;
151 uint8_t avl;
152 uint8_t unusable;
153 uint8_t padding;
156 struct kvm_dtable {
157 uint64_t base;
158 uint16_t limit;
159 uint16_t padding[3];
162 struct kvm_sregs {
163 struct kvm_segment cs;
164 struct kvm_segment ds;
165 struct kvm_segment es;
166 struct kvm_segment fs;
167 struct kvm_segment gs;
168 struct kvm_segment ss;
169 struct kvm_segment tr;
170 struct kvm_segment ldt;
171 struct kvm_dtable gdt;
172 struct kvm_dtable idt;
173 uint64_t cr0;
174 uint64_t cr2;
175 uint64_t cr3;
176 uint64_t cr4;
177 uint64_t cr8;
178 uint64_t efer;
179 uint64_t apic_base;
180 uint64_t interrupt_bitmap[(KVM_NR_INTERRUPTS + 63) / 64];
183 /* debug */
185 struct kvm_breakpoint {
186 uint32_t enabled;
187 uint32_t padding;
188 uint64_t address;
191 struct kvm_debug_guest {
192 uint32_t enabled;
193 uint32_t pad;
194 struct kvm_breakpoint breakpoints[4];
195 uint32_t singlestep;
198 /* cpuid */
200 struct kvm_cpuid_entry {
201 uint32_t function;
202 uint32_t eax;
203 uint32_t ebx;
204 uint32_t ecx;
205 uint32_t edx;
206 uint32_t padding;
209 #if defined(_KERNEL) || defined(FKVM_INTERNAL)
211 enum {
212 KVM_EXIT_UNKNOWN = 0,
213 KVM_EXIT_EXCEPTION = 1,
214 KVM_EXIT_IO = 2,
215 KVM_EXIT_HYPERCALL = 3,
216 KVM_EXIT_DEBUG = 4,
217 KVM_EXIT_HLT = 5,
218 KVM_EXIT_MMIO = 6,
219 KVM_EXIT_IRQ_WINDOW_OPEN = 7,
220 KVM_EXIT_SHUTDOWN = 8,
221 KVM_EXIT_FAIL_ENTRY = 9,
222 KVM_EXIT_INTR = 10,
223 KVM_EXIT_SET_TPR = 11,
224 KVM_EXIT_TPR_ACCESS = 12,
225 KVM_EXIT_S390_SIEIC = 13,
226 KVM_EXIT_S390_RESET = 14,
227 KVM_EXIT_DCR = 15,
228 KVM_EXIT_NMI = 16,
229 KVM_EXIT_NMI_WINDOW_OPEN = 17,
230 KVM_EXIT_CPUID = 18,
231 KVM_EXIT_CONTINUE = 19,
234 struct kvm_run {
235 /* in */
236 uint8_t request_interrupt_window;
237 uint8_t request_nmi_window;
238 uint8_t padding1[6];
240 /* out */
241 uint32_t exit_reason;
242 uint8_t ready_for_interrupt_injection;
243 uint8_t if_flag;
244 uint8_t ready_for_nmi_injection;
245 uint8_t padding2;
247 /* in (pre_kvm_run), out (post_kvm_run) */
248 uint64_t cr8;
250 union {
251 /* KVM_EXIT_UNKNOWN */
252 struct {
253 uint64_t hardware_exit_reason;
254 } hw;
255 /* KVM_EXIT_FAIL_ENTRY */
256 struct {
257 uint64_t hardware_entry_failure_reason;
258 } fail_entry;
259 /* KVM_EXIT_EXCEPTION */
260 struct {
261 uint32_t exception;
262 uint32_t error_code;
263 } ex;
264 /* KVM_EXIT_IO */
265 struct kvm_io {
266 #define KVM_EXIT_IO_IN 0
267 #define KVM_EXIT_IO_OUT 1
268 uint8_t in; /* 1 = in 0 = out */
269 uint8_t size; /* bytes */
270 uint8_t string; /* 1 = string */
271 uint8_t rep; /* repeat boolean */
272 uint16_t port;
273 uint32_t count;
275 } io;
276 /* KVM_EXIT_MMIO */
277 struct {
278 uint64_t fault_gpa; /* Faulting Guest Physical Address */
279 uint64_t rip;
280 uint64_t cs_base;
281 } mmio;
282 /* KVM_EXIT_TPR_ACCESS */
283 struct {
284 uint64_t rip;
285 uint32_t is_write;
286 uint32_t pad;
287 } tpr_access;
288 /* KVM_EXIT_DCR */
289 struct {
290 uint32_t dcrn;
291 uint32_t data;
292 uint8_t is_write;
293 } dcr;
294 /* KVM_EXIT_CPUID */
295 struct {
296 uint32_t fn;
297 } cpuid;
298 /* Fix the size of the union. */
299 uint8_t padding[32];
300 } u;
302 #endif
304 #endif /* !_FKVM_H_ */