xref: /openbmc/linux/tools/arch/x86/include/uapi/asm/kvm.h (revision 54618888)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _ASM_X86_KVM_H
3 #define _ASM_X86_KVM_H
4 
5 /*
6  * KVM x86 specific structures and definitions
7  *
8  */
9 
10 #include <linux/types.h>
11 #include <linux/ioctl.h>
12 
13 #define KVM_PIO_PAGE_OFFSET 1
14 #define KVM_COALESCED_MMIO_PAGE_OFFSET 2
15 #define KVM_DIRTY_LOG_PAGE_OFFSET 64
16 
17 #define DE_VECTOR 0
18 #define DB_VECTOR 1
19 #define BP_VECTOR 3
20 #define OF_VECTOR 4
21 #define BR_VECTOR 5
22 #define UD_VECTOR 6
23 #define NM_VECTOR 7
24 #define DF_VECTOR 8
25 #define TS_VECTOR 10
26 #define NP_VECTOR 11
27 #define SS_VECTOR 12
28 #define GP_VECTOR 13
29 #define PF_VECTOR 14
30 #define MF_VECTOR 16
31 #define AC_VECTOR 17
32 #define MC_VECTOR 18
33 #define XM_VECTOR 19
34 #define VE_VECTOR 20
35 
36 /* Select x86 specific features in <linux/kvm.h> */
37 #define __KVM_HAVE_PIT
38 #define __KVM_HAVE_IOAPIC
39 #define __KVM_HAVE_IRQ_LINE
40 #define __KVM_HAVE_MSI
41 #define __KVM_HAVE_USER_NMI
42 #define __KVM_HAVE_GUEST_DEBUG
43 #define __KVM_HAVE_MSIX
44 #define __KVM_HAVE_MCE
45 #define __KVM_HAVE_PIT_STATE2
46 #define __KVM_HAVE_XEN_HVM
47 #define __KVM_HAVE_VCPU_EVENTS
48 #define __KVM_HAVE_DEBUGREGS
49 #define __KVM_HAVE_XSAVE
50 #define __KVM_HAVE_XCRS
51 #define __KVM_HAVE_READONLY_MEM
52 
53 /* Architectural interrupt line count. */
54 #define KVM_NR_INTERRUPTS 256
55 
56 /* for KVM_GET_IRQCHIP and KVM_SET_IRQCHIP */
57 struct kvm_pic_state {
58 	__u8 last_irr;	/* edge detection */
59 	__u8 irr;		/* interrupt request register */
60 	__u8 imr;		/* interrupt mask register */
61 	__u8 isr;		/* interrupt service register */
62 	__u8 priority_add;	/* highest irq priority */
63 	__u8 irq_base;
64 	__u8 read_reg_select;
65 	__u8 poll;
66 	__u8 special_mask;
67 	__u8 init_state;
68 	__u8 auto_eoi;
69 	__u8 rotate_on_auto_eoi;
70 	__u8 special_fully_nested_mode;
71 	__u8 init4;		/* true if 4 byte init */
72 	__u8 elcr;		/* PIIX edge/trigger selection */
73 	__u8 elcr_mask;
74 };
75 
76 #define KVM_IOAPIC_NUM_PINS  24
77 struct kvm_ioapic_state {
78 	__u64 base_address;
79 	__u32 ioregsel;
80 	__u32 id;
81 	__u32 irr;
82 	__u32 pad;
83 	union {
84 		__u64 bits;
85 		struct {
86 			__u8 vector;
87 			__u8 delivery_mode:3;
88 			__u8 dest_mode:1;
89 			__u8 delivery_status:1;
90 			__u8 polarity:1;
91 			__u8 remote_irr:1;
92 			__u8 trig_mode:1;
93 			__u8 mask:1;
94 			__u8 reserve:7;
95 			__u8 reserved[4];
96 			__u8 dest_id;
97 		} fields;
98 	} redirtbl[KVM_IOAPIC_NUM_PINS];
99 };
100 
101 #define KVM_IRQCHIP_PIC_MASTER   0
102 #define KVM_IRQCHIP_PIC_SLAVE    1
103 #define KVM_IRQCHIP_IOAPIC       2
104 #define KVM_NR_IRQCHIPS          3
105 
106 #define KVM_RUN_X86_SMM		 (1 << 0)
107 #define KVM_RUN_X86_BUS_LOCK     (1 << 1)
108 
109 /* for KVM_GET_REGS and KVM_SET_REGS */
110 struct kvm_regs {
111 	/* out (KVM_GET_REGS) / in (KVM_SET_REGS) */
112 	__u64 rax, rbx, rcx, rdx;
113 	__u64 rsi, rdi, rsp, rbp;
114 	__u64 r8,  r9,  r10, r11;
115 	__u64 r12, r13, r14, r15;
116 	__u64 rip, rflags;
117 };
118 
119 /* for KVM_GET_LAPIC and KVM_SET_LAPIC */
120 #define KVM_APIC_REG_SIZE 0x400
121 struct kvm_lapic_state {
122 	char regs[KVM_APIC_REG_SIZE];
123 };
124 
125 struct kvm_segment {
126 	__u64 base;
127 	__u32 limit;
128 	__u16 selector;
129 	__u8  type;
130 	__u8  present, dpl, db, s, l, g, avl;
131 	__u8  unusable;
132 	__u8  padding;
133 };
134 
135 struct kvm_dtable {
136 	__u64 base;
137 	__u16 limit;
138 	__u16 padding[3];
139 };
140 
141 
142 /* for KVM_GET_SREGS and KVM_SET_SREGS */
143 struct kvm_sregs {
144 	/* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */
145 	struct kvm_segment cs, ds, es, fs, gs, ss;
146 	struct kvm_segment tr, ldt;
147 	struct kvm_dtable gdt, idt;
148 	__u64 cr0, cr2, cr3, cr4, cr8;
149 	__u64 efer;
150 	__u64 apic_base;
151 	__u64 interrupt_bitmap[(KVM_NR_INTERRUPTS + 63) / 64];
152 };
153 
154 struct kvm_sregs2 {
155 	/* out (KVM_GET_SREGS2) / in (KVM_SET_SREGS2) */
156 	struct kvm_segment cs, ds, es, fs, gs, ss;
157 	struct kvm_segment tr, ldt;
158 	struct kvm_dtable gdt, idt;
159 	__u64 cr0, cr2, cr3, cr4, cr8;
160 	__u64 efer;
161 	__u64 apic_base;
162 	__u64 flags;
163 	__u64 pdptrs[4];
164 };
165 #define KVM_SREGS2_FLAGS_PDPTRS_VALID 1
166 
167 /* for KVM_GET_FPU and KVM_SET_FPU */
168 struct kvm_fpu {
169 	__u8  fpr[8][16];
170 	__u16 fcw;
171 	__u16 fsw;
172 	__u8  ftwx;  /* in fxsave format */
173 	__u8  pad1;
174 	__u16 last_opcode;
175 	__u64 last_ip;
176 	__u64 last_dp;
177 	__u8  xmm[16][16];
178 	__u32 mxcsr;
179 	__u32 pad2;
180 };
181 
182 struct kvm_msr_entry {
183 	__u32 index;
184 	__u32 reserved;
185 	__u64 data;
186 };
187 
188 /* for KVM_GET_MSRS and KVM_SET_MSRS */
189 struct kvm_msrs {
190 	__u32 nmsrs; /* number of msrs in entries */
191 	__u32 pad;
192 
193 	struct kvm_msr_entry entries[];
194 };
195 
196 /* for KVM_GET_MSR_INDEX_LIST */
197 struct kvm_msr_list {
198 	__u32 nmsrs; /* number of msrs in entries */
199 	__u32 indices[];
200 };
201 
202 /* Maximum size of any access bitmap in bytes */
203 #define KVM_MSR_FILTER_MAX_BITMAP_SIZE 0x600
204 
205 /* for KVM_X86_SET_MSR_FILTER */
206 struct kvm_msr_filter_range {
207 #define KVM_MSR_FILTER_READ  (1 << 0)
208 #define KVM_MSR_FILTER_WRITE (1 << 1)
209 #define KVM_MSR_FILTER_RANGE_VALID_MASK (KVM_MSR_FILTER_READ | \
210 					 KVM_MSR_FILTER_WRITE)
211 	__u32 flags;
212 	__u32 nmsrs; /* number of msrs in bitmap */
213 	__u32 base;  /* MSR index the bitmap starts at */
214 	__u8 *bitmap; /* a 1 bit allows the operations in flags, 0 denies */
215 };
216 
217 #define KVM_MSR_FILTER_MAX_RANGES 16
218 struct kvm_msr_filter {
219 #ifndef __KERNEL__
220 #define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0)
221 #endif
222 #define KVM_MSR_FILTER_DEFAULT_DENY  (1 << 0)
223 #define KVM_MSR_FILTER_VALID_MASK (KVM_MSR_FILTER_DEFAULT_DENY)
224 	__u32 flags;
225 	struct kvm_msr_filter_range ranges[KVM_MSR_FILTER_MAX_RANGES];
226 };
227 
228 struct kvm_cpuid_entry {
229 	__u32 function;
230 	__u32 eax;
231 	__u32 ebx;
232 	__u32 ecx;
233 	__u32 edx;
234 	__u32 padding;
235 };
236 
237 /* for KVM_SET_CPUID */
238 struct kvm_cpuid {
239 	__u32 nent;
240 	__u32 padding;
241 	struct kvm_cpuid_entry entries[];
242 };
243 
244 struct kvm_cpuid_entry2 {
245 	__u32 function;
246 	__u32 index;
247 	__u32 flags;
248 	__u32 eax;
249 	__u32 ebx;
250 	__u32 ecx;
251 	__u32 edx;
252 	__u32 padding[3];
253 };
254 
255 #define KVM_CPUID_FLAG_SIGNIFCANT_INDEX		(1 << 0)
256 #define KVM_CPUID_FLAG_STATEFUL_FUNC		(1 << 1)
257 #define KVM_CPUID_FLAG_STATE_READ_NEXT		(1 << 2)
258 
259 /* for KVM_SET_CPUID2 */
260 struct kvm_cpuid2 {
261 	__u32 nent;
262 	__u32 padding;
263 	struct kvm_cpuid_entry2 entries[];
264 };
265 
266 /* for KVM_GET_PIT and KVM_SET_PIT */
267 struct kvm_pit_channel_state {
268 	__u32 count; /* can be 65536 */
269 	__u16 latched_count;
270 	__u8 count_latched;
271 	__u8 status_latched;
272 	__u8 status;
273 	__u8 read_state;
274 	__u8 write_state;
275 	__u8 write_latch;
276 	__u8 rw_mode;
277 	__u8 mode;
278 	__u8 bcd;
279 	__u8 gate;
280 	__s64 count_load_time;
281 };
282 
283 struct kvm_debug_exit_arch {
284 	__u32 exception;
285 	__u32 pad;
286 	__u64 pc;
287 	__u64 dr6;
288 	__u64 dr7;
289 };
290 
291 #define KVM_GUESTDBG_USE_SW_BP		0x00010000
292 #define KVM_GUESTDBG_USE_HW_BP		0x00020000
293 #define KVM_GUESTDBG_INJECT_DB		0x00040000
294 #define KVM_GUESTDBG_INJECT_BP		0x00080000
295 #define KVM_GUESTDBG_BLOCKIRQ		0x00100000
296 
297 /* for KVM_SET_GUEST_DEBUG */
298 struct kvm_guest_debug_arch {
299 	__u64 debugreg[8];
300 };
301 
302 struct kvm_pit_state {
303 	struct kvm_pit_channel_state channels[3];
304 };
305 
306 #define KVM_PIT_FLAGS_HPET_LEGACY     0x00000001
307 #define KVM_PIT_FLAGS_SPEAKER_DATA_ON 0x00000002
308 
309 struct kvm_pit_state2 {
310 	struct kvm_pit_channel_state channels[3];
311 	__u32 flags;
312 	__u32 reserved[9];
313 };
314 
315 struct kvm_reinject_control {
316 	__u8 pit_reinject;
317 	__u8 reserved[31];
318 };
319 
320 /* When set in flags, include corresponding fields on KVM_SET_VCPU_EVENTS */
321 #define KVM_VCPUEVENT_VALID_NMI_PENDING	0x00000001
322 #define KVM_VCPUEVENT_VALID_SIPI_VECTOR	0x00000002
323 #define KVM_VCPUEVENT_VALID_SHADOW	0x00000004
324 #define KVM_VCPUEVENT_VALID_SMM		0x00000008
325 #define KVM_VCPUEVENT_VALID_PAYLOAD	0x00000010
326 #define KVM_VCPUEVENT_VALID_TRIPLE_FAULT	0x00000020
327 
328 /* Interrupt shadow states */
329 #define KVM_X86_SHADOW_INT_MOV_SS	0x01
330 #define KVM_X86_SHADOW_INT_STI		0x02
331 
332 /* for KVM_GET/SET_VCPU_EVENTS */
333 struct kvm_vcpu_events {
334 	struct {
335 		__u8 injected;
336 		__u8 nr;
337 		__u8 has_error_code;
338 		__u8 pending;
339 		__u32 error_code;
340 	} exception;
341 	struct {
342 		__u8 injected;
343 		__u8 nr;
344 		__u8 soft;
345 		__u8 shadow;
346 	} interrupt;
347 	struct {
348 		__u8 injected;
349 		__u8 pending;
350 		__u8 masked;
351 		__u8 pad;
352 	} nmi;
353 	__u32 sipi_vector;
354 	__u32 flags;
355 	struct {
356 		__u8 smm;
357 		__u8 pending;
358 		__u8 smm_inside_nmi;
359 		__u8 latched_init;
360 	} smi;
361 	struct {
362 		__u8 pending;
363 	} triple_fault;
364 	__u8 reserved[26];
365 	__u8 exception_has_payload;
366 	__u64 exception_payload;
367 };
368 
369 /* for KVM_GET/SET_DEBUGREGS */
370 struct kvm_debugregs {
371 	__u64 db[4];
372 	__u64 dr6;
373 	__u64 dr7;
374 	__u64 flags;
375 	__u64 reserved[9];
376 };
377 
378 /* for KVM_CAP_XSAVE and KVM_CAP_XSAVE2 */
379 struct kvm_xsave {
380 	/*
381 	 * KVM_GET_XSAVE2 and KVM_SET_XSAVE write and read as many bytes
382 	 * as are returned by KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)
383 	 * respectively, when invoked on the vm file descriptor.
384 	 *
385 	 * The size value returned by KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)
386 	 * will always be at least 4096. Currently, it is only greater
387 	 * than 4096 if a dynamic feature has been enabled with
388 	 * ``arch_prctl()``, but this may change in the future.
389 	 *
390 	 * The offsets of the state save areas in struct kvm_xsave follow
391 	 * the contents of CPUID leaf 0xD on the host.
392 	 */
393 	__u32 region[1024];
394 	__u32 extra[];
395 };
396 
397 #define KVM_MAX_XCRS	16
398 
399 struct kvm_xcr {
400 	__u32 xcr;
401 	__u32 reserved;
402 	__u64 value;
403 };
404 
405 struct kvm_xcrs {
406 	__u32 nr_xcrs;
407 	__u32 flags;
408 	struct kvm_xcr xcrs[KVM_MAX_XCRS];
409 	__u64 padding[16];
410 };
411 
412 #define KVM_SYNC_X86_REGS      (1UL << 0)
413 #define KVM_SYNC_X86_SREGS     (1UL << 1)
414 #define KVM_SYNC_X86_EVENTS    (1UL << 2)
415 
416 #define KVM_SYNC_X86_VALID_FIELDS \
417 	(KVM_SYNC_X86_REGS| \
418 	 KVM_SYNC_X86_SREGS| \
419 	 KVM_SYNC_X86_EVENTS)
420 
421 /* kvm_sync_regs struct included by kvm_run struct */
422 struct kvm_sync_regs {
423 	/* Members of this structure are potentially malicious.
424 	 * Care must be taken by code reading, esp. interpreting,
425 	 * data fields from them inside KVM to prevent TOCTOU and
426 	 * double-fetch types of vulnerabilities.
427 	 */
428 	struct kvm_regs regs;
429 	struct kvm_sregs sregs;
430 	struct kvm_vcpu_events events;
431 };
432 
433 #define KVM_X86_QUIRK_LINT0_REENABLED		(1 << 0)
434 #define KVM_X86_QUIRK_CD_NW_CLEARED		(1 << 1)
435 #define KVM_X86_QUIRK_LAPIC_MMIO_HOLE		(1 << 2)
436 #define KVM_X86_QUIRK_OUT_7E_INC_RIP		(1 << 3)
437 #define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT	(1 << 4)
438 #define KVM_X86_QUIRK_FIX_HYPERCALL_INSN	(1 << 5)
439 #define KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS	(1 << 6)
440 
441 #define KVM_STATE_NESTED_FORMAT_VMX	0
442 #define KVM_STATE_NESTED_FORMAT_SVM	1
443 
444 #define KVM_STATE_NESTED_GUEST_MODE	0x00000001
445 #define KVM_STATE_NESTED_RUN_PENDING	0x00000002
446 #define KVM_STATE_NESTED_EVMCS		0x00000004
447 #define KVM_STATE_NESTED_MTF_PENDING	0x00000008
448 #define KVM_STATE_NESTED_GIF_SET	0x00000100
449 
450 #define KVM_STATE_NESTED_SMM_GUEST_MODE	0x00000001
451 #define KVM_STATE_NESTED_SMM_VMXON	0x00000002
452 
453 #define KVM_STATE_NESTED_VMX_VMCS_SIZE	0x1000
454 
455 #define KVM_STATE_NESTED_SVM_VMCB_SIZE	0x1000
456 
457 #define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE	0x00000001
458 
459 /* attributes for system fd (group 0) */
460 #define KVM_X86_XCOMP_GUEST_SUPP	0
461 
462 struct kvm_vmx_nested_state_data {
463 	__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
464 	__u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
465 };
466 
467 struct kvm_vmx_nested_state_hdr {
468 	__u64 vmxon_pa;
469 	__u64 vmcs12_pa;
470 
471 	struct {
472 		__u16 flags;
473 	} smm;
474 
475 	__u16 pad;
476 
477 	__u32 flags;
478 	__u64 preemption_timer_deadline;
479 };
480 
481 struct kvm_svm_nested_state_data {
482 	/* Save area only used if KVM_STATE_NESTED_RUN_PENDING.  */
483 	__u8 vmcb12[KVM_STATE_NESTED_SVM_VMCB_SIZE];
484 };
485 
486 struct kvm_svm_nested_state_hdr {
487 	__u64 vmcb_pa;
488 };
489 
490 /* for KVM_CAP_NESTED_STATE */
491 struct kvm_nested_state {
492 	__u16 flags;
493 	__u16 format;
494 	__u32 size;
495 
496 	union {
497 		struct kvm_vmx_nested_state_hdr vmx;
498 		struct kvm_svm_nested_state_hdr svm;
499 
500 		/* Pad the header to 128 bytes.  */
501 		__u8 pad[120];
502 	} hdr;
503 
504 	/*
505 	 * Define data region as 0 bytes to preserve backwards-compatability
506 	 * to old definition of kvm_nested_state in order to avoid changing
507 	 * KVM_{GET,PUT}_NESTED_STATE ioctl values.
508 	 */
509 	union {
510 		struct kvm_vmx_nested_state_data vmx[0];
511 		struct kvm_svm_nested_state_data svm[0];
512 	} data;
513 };
514 
515 /* for KVM_CAP_PMU_EVENT_FILTER */
516 struct kvm_pmu_event_filter {
517 	__u32 action;
518 	__u32 nevents;
519 	__u32 fixed_counter_bitmap;
520 	__u32 flags;
521 	__u32 pad[4];
522 	__u64 events[];
523 };
524 
525 #define KVM_PMU_EVENT_ALLOW 0
526 #define KVM_PMU_EVENT_DENY 1
527 
528 /* for KVM_{GET,SET,HAS}_DEVICE_ATTR */
529 #define KVM_VCPU_TSC_CTRL 0 /* control group for the timestamp counter (TSC) */
530 #define   KVM_VCPU_TSC_OFFSET 0 /* attribute for the TSC offset */
531 
532 #endif /* _ASM_X86_KVM_H */
533