1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
6 * KVM x86 specific structures and definitions
10 #include <linux/types.h>
11 #include <linux/ioctl.h>
12 #include <linux/stddef.h>
14 #define KVM_PIO_PAGE_OFFSET 1
15 #define KVM_COALESCED_MMIO_PAGE_OFFSET 2
16 #define KVM_DIRTY_LOG_PAGE_OFFSET 64
37 /* Select x86 specific features in <linux/kvm.h> */
38 #define __KVM_HAVE_PIT
39 #define __KVM_HAVE_IOAPIC
40 #define __KVM_HAVE_IRQ_LINE
41 #define __KVM_HAVE_MSI
42 #define __KVM_HAVE_USER_NMI
43 #define __KVM_HAVE_GUEST_DEBUG
44 #define __KVM_HAVE_MSIX
45 #define __KVM_HAVE_MCE
46 #define __KVM_HAVE_PIT_STATE2
47 #define __KVM_HAVE_XEN_HVM
48 #define __KVM_HAVE_VCPU_EVENTS
49 #define __KVM_HAVE_DEBUGREGS
50 #define __KVM_HAVE_XSAVE
51 #define __KVM_HAVE_XCRS
52 #define __KVM_HAVE_READONLY_MEM
54 /* Architectural interrupt line count. */
55 #define KVM_NR_INTERRUPTS 256
57 /* for KVM_GET_IRQCHIP and KVM_SET_IRQCHIP */
58 struct kvm_pic_state
{
59 __u8 last_irr
; /* edge detection */
60 __u8 irr
; /* interrupt request register */
61 __u8 imr
; /* interrupt mask register */
62 __u8 isr
; /* interrupt service register */
63 __u8 priority_add
; /* highest irq priority */
70 __u8 rotate_on_auto_eoi
;
71 __u8 special_fully_nested_mode
;
72 __u8 init4
; /* true if 4 byte init */
73 __u8 elcr
; /* PIIX edge/trigger selection */
77 #define KVM_IOAPIC_NUM_PINS 24
78 struct kvm_ioapic_state
{
90 __u8 delivery_status
:1;
99 } redirtbl
[KVM_IOAPIC_NUM_PINS
];
102 #define KVM_IRQCHIP_PIC_MASTER 0
103 #define KVM_IRQCHIP_PIC_SLAVE 1
104 #define KVM_IRQCHIP_IOAPIC 2
105 #define KVM_NR_IRQCHIPS 3
107 #define KVM_RUN_X86_SMM (1 << 0)
108 #define KVM_RUN_X86_BUS_LOCK (1 << 1)
110 /* for KVM_GET_REGS and KVM_SET_REGS */
112 /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */
113 __u64 rax
, rbx
, rcx
, rdx
;
114 __u64 rsi
, rdi
, rsp
, rbp
;
115 __u64 r8
, r9
, r10
, r11
;
116 __u64 r12
, r13
, r14
, r15
;
120 /* for KVM_GET_LAPIC and KVM_SET_LAPIC */
121 #define KVM_APIC_REG_SIZE 0x400
122 struct kvm_lapic_state
{
123 char regs
[KVM_APIC_REG_SIZE
];
131 __u8 present
, dpl
, db
, s
, l
, g
, avl
;
143 /* for KVM_GET_SREGS and KVM_SET_SREGS */
145 /* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */
146 struct kvm_segment cs
, ds
, es
, fs
, gs
, ss
;
147 struct kvm_segment tr
, ldt
;
148 struct kvm_dtable gdt
, idt
;
149 __u64 cr0
, cr2
, cr3
, cr4
, cr8
;
152 __u64 interrupt_bitmap
[(KVM_NR_INTERRUPTS
+ 63) / 64];
156 /* out (KVM_GET_SREGS2) / in (KVM_SET_SREGS2) */
157 struct kvm_segment cs
, ds
, es
, fs
, gs
, ss
;
158 struct kvm_segment tr
, ldt
;
159 struct kvm_dtable gdt
, idt
;
160 __u64 cr0
, cr2
, cr3
, cr4
, cr8
;
166 #define KVM_SREGS2_FLAGS_PDPTRS_VALID 1
168 /* for KVM_GET_FPU and KVM_SET_FPU */
173 __u8 ftwx
; /* in fxsave format */
183 struct kvm_msr_entry
{
189 /* for KVM_GET_MSRS and KVM_SET_MSRS */
191 __u32 nmsrs
; /* number of msrs in entries */
194 struct kvm_msr_entry entries
[];
197 /* for KVM_GET_MSR_INDEX_LIST */
198 struct kvm_msr_list
{
199 __u32 nmsrs
; /* number of msrs in entries */
203 /* Maximum size of any access bitmap in bytes */
204 #define KVM_MSR_FILTER_MAX_BITMAP_SIZE 0x600
206 /* for KVM_X86_SET_MSR_FILTER */
207 struct kvm_msr_filter_range
{
208 #define KVM_MSR_FILTER_READ (1 << 0)
209 #define KVM_MSR_FILTER_WRITE (1 << 1)
210 #define KVM_MSR_FILTER_RANGE_VALID_MASK (KVM_MSR_FILTER_READ | \
211 KVM_MSR_FILTER_WRITE)
213 __u32 nmsrs
; /* number of msrs in bitmap */
214 __u32 base
; /* MSR index the bitmap starts at */
215 __u8
*bitmap
; /* a 1 bit allows the operations in flags, 0 denies */
218 #define KVM_MSR_FILTER_MAX_RANGES 16
219 struct kvm_msr_filter
{
220 #define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0)
221 #define KVM_MSR_FILTER_DEFAULT_DENY (1 << 0)
222 #define KVM_MSR_FILTER_VALID_MASK (KVM_MSR_FILTER_DEFAULT_DENY)
224 struct kvm_msr_filter_range ranges
[KVM_MSR_FILTER_MAX_RANGES
];
227 struct kvm_cpuid_entry
{
236 /* for KVM_SET_CPUID */
240 struct kvm_cpuid_entry entries
[];
243 struct kvm_cpuid_entry2
{
254 #define KVM_CPUID_FLAG_SIGNIFCANT_INDEX (1 << 0)
255 #define KVM_CPUID_FLAG_STATEFUL_FUNC (1 << 1)
256 #define KVM_CPUID_FLAG_STATE_READ_NEXT (1 << 2)
258 /* for KVM_SET_CPUID2 */
262 struct kvm_cpuid_entry2 entries
[];
265 /* for KVM_GET_PIT and KVM_SET_PIT */
266 struct kvm_pit_channel_state
{
267 __u32 count
; /* can be 65536 */
279 __s64 count_load_time
;
282 struct kvm_debug_exit_arch
{
290 #define KVM_GUESTDBG_USE_SW_BP 0x00010000
291 #define KVM_GUESTDBG_USE_HW_BP 0x00020000
292 #define KVM_GUESTDBG_INJECT_DB 0x00040000
293 #define KVM_GUESTDBG_INJECT_BP 0x00080000
294 #define KVM_GUESTDBG_BLOCKIRQ 0x00100000
296 /* for KVM_SET_GUEST_DEBUG */
297 struct kvm_guest_debug_arch
{
301 struct kvm_pit_state
{
302 struct kvm_pit_channel_state channels
[3];
305 #define KVM_PIT_FLAGS_HPET_LEGACY 0x00000001
306 #define KVM_PIT_FLAGS_SPEAKER_DATA_ON 0x00000002
308 struct kvm_pit_state2
{
309 struct kvm_pit_channel_state channels
[3];
314 struct kvm_reinject_control
{
319 /* When set in flags, include corresponding fields on KVM_SET_VCPU_EVENTS */
320 #define KVM_VCPUEVENT_VALID_NMI_PENDING 0x00000001
321 #define KVM_VCPUEVENT_VALID_SIPI_VECTOR 0x00000002
322 #define KVM_VCPUEVENT_VALID_SHADOW 0x00000004
323 #define KVM_VCPUEVENT_VALID_SMM 0x00000008
324 #define KVM_VCPUEVENT_VALID_PAYLOAD 0x00000010
325 #define KVM_VCPUEVENT_VALID_TRIPLE_FAULT 0x00000020
327 /* Interrupt shadow states */
328 #define KVM_X86_SHADOW_INT_MOV_SS 0x01
329 #define KVM_X86_SHADOW_INT_STI 0x02
331 /* for KVM_GET/SET_VCPU_EVENTS */
332 struct kvm_vcpu_events
{
364 __u8 exception_has_payload
;
365 __u64 exception_payload
;
368 /* for KVM_GET/SET_DEBUGREGS */
369 struct kvm_debugregs
{
377 /* for KVM_CAP_XSAVE and KVM_CAP_XSAVE2 */
380 * KVM_GET_XSAVE2 and KVM_SET_XSAVE write and read as many bytes
381 * as are returned by KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)
382 * respectively, when invoked on the vm file descriptor.
384 * The size value returned by KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)
385 * will always be at least 4096. Currently, it is only greater
386 * than 4096 if a dynamic feature has been enabled with
387 * ``arch_prctl()``, but this may change in the future.
389 * The offsets of the state save areas in struct kvm_xsave follow
390 * the contents of CPUID leaf 0xD on the host.
396 #define KVM_MAX_XCRS 16
407 struct kvm_xcr xcrs
[KVM_MAX_XCRS
];
411 #define KVM_SYNC_X86_REGS (1UL << 0)
412 #define KVM_SYNC_X86_SREGS (1UL << 1)
413 #define KVM_SYNC_X86_EVENTS (1UL << 2)
415 #define KVM_SYNC_X86_VALID_FIELDS \
416 (KVM_SYNC_X86_REGS| \
417 KVM_SYNC_X86_SREGS| \
420 /* kvm_sync_regs struct included by kvm_run struct */
421 struct kvm_sync_regs
{
422 /* Members of this structure are potentially malicious.
423 * Care must be taken by code reading, esp. interpreting,
424 * data fields from them inside KVM to prevent TOCTOU and
425 * double-fetch types of vulnerabilities.
427 struct kvm_regs regs
;
428 struct kvm_sregs sregs
;
429 struct kvm_vcpu_events events
;
432 #define KVM_X86_QUIRK_LINT0_REENABLED (1 << 0)
433 #define KVM_X86_QUIRK_CD_NW_CLEARED (1 << 1)
434 #define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2)
435 #define KVM_X86_QUIRK_OUT_7E_INC_RIP (1 << 3)
436 #define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT (1 << 4)
437 #define KVM_X86_QUIRK_FIX_HYPERCALL_INSN (1 << 5)
438 #define KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS (1 << 6)
440 #define KVM_STATE_NESTED_FORMAT_VMX 0
441 #define KVM_STATE_NESTED_FORMAT_SVM 1
443 #define KVM_STATE_NESTED_GUEST_MODE 0x00000001
444 #define KVM_STATE_NESTED_RUN_PENDING 0x00000002
445 #define KVM_STATE_NESTED_EVMCS 0x00000004
446 #define KVM_STATE_NESTED_MTF_PENDING 0x00000008
447 #define KVM_STATE_NESTED_GIF_SET 0x00000100
449 #define KVM_STATE_NESTED_SMM_GUEST_MODE 0x00000001
450 #define KVM_STATE_NESTED_SMM_VMXON 0x00000002
452 #define KVM_STATE_NESTED_VMX_VMCS_SIZE 0x1000
454 #define KVM_STATE_NESTED_SVM_VMCB_SIZE 0x1000
456 #define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001
458 /* attributes for system fd (group 0) */
459 #define KVM_X86_XCOMP_GUEST_SUPP 0
461 struct kvm_vmx_nested_state_data
{
462 __u8 vmcs12
[KVM_STATE_NESTED_VMX_VMCS_SIZE
];
463 __u8 shadow_vmcs12
[KVM_STATE_NESTED_VMX_VMCS_SIZE
];
466 struct kvm_vmx_nested_state_hdr
{
477 __u64 preemption_timer_deadline
;
480 struct kvm_svm_nested_state_data
{
481 /* Save area only used if KVM_STATE_NESTED_RUN_PENDING. */
482 __u8 vmcb12
[KVM_STATE_NESTED_SVM_VMCB_SIZE
];
485 struct kvm_svm_nested_state_hdr
{
489 /* for KVM_CAP_NESTED_STATE */
490 struct kvm_nested_state
{
496 struct kvm_vmx_nested_state_hdr vmx
;
497 struct kvm_svm_nested_state_hdr svm
;
499 /* Pad the header to 128 bytes. */
504 * Define data region as 0 bytes to preserve backwards-compatability
505 * to old definition of kvm_nested_state in order to avoid changing
506 * KVM_{GET,PUT}_NESTED_STATE ioctl values.
509 __DECLARE_FLEX_ARRAY(struct kvm_vmx_nested_state_data
, vmx
);
510 __DECLARE_FLEX_ARRAY(struct kvm_svm_nested_state_data
, svm
);
514 /* for KVM_CAP_PMU_EVENT_FILTER */
515 struct kvm_pmu_event_filter
{
518 __u32 fixed_counter_bitmap
;
524 #define KVM_PMU_EVENT_ALLOW 0
525 #define KVM_PMU_EVENT_DENY 1
527 #define KVM_PMU_EVENT_FLAG_MASKED_EVENTS BIT(0)
528 #define KVM_PMU_EVENT_FLAGS_VALID_MASK (KVM_PMU_EVENT_FLAG_MASKED_EVENTS)
531 * Masked event layout.
534 * 7:0 event select (low bits)
537 * 35:32 event select (high bits)
543 #define KVM_PMU_ENCODE_MASKED_ENTRY(event_select, mask, match, exclude) \
544 (((event_select) & 0xFFULL) | (((event_select) & 0XF00ULL) << 24) | \
545 (((mask) & 0xFFULL) << 56) | \
546 (((match) & 0xFFULL) << 8) | \
547 ((__u64)(!!(exclude)) << 55))
549 #define KVM_PMU_MASKED_ENTRY_EVENT_SELECT \
550 (GENMASK_ULL(7, 0) | GENMASK_ULL(35, 32))
551 #define KVM_PMU_MASKED_ENTRY_UMASK_MASK (GENMASK_ULL(63, 56))
552 #define KVM_PMU_MASKED_ENTRY_UMASK_MATCH (GENMASK_ULL(15, 8))
553 #define KVM_PMU_MASKED_ENTRY_EXCLUDE (BIT_ULL(55))
554 #define KVM_PMU_MASKED_ENTRY_UMASK_MASK_SHIFT (56)
556 /* for KVM_{GET,SET,HAS}_DEVICE_ATTR */
557 #define KVM_VCPU_TSC_CTRL 0 /* control group for the timestamp counter (TSC) */
558 #define KVM_VCPU_TSC_OFFSET 0 /* attribute for the TSC offset */
560 /* x86-specific KVM_EXIT_HYPERCALL flags. */
561 #define KVM_EXIT_HYPERCALL_LONG_MODE BIT(0)
563 #endif /* _ASM_X86_KVM_H */