4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * Copyright 2011 various Linux Kernel contributors.
20 * Copyright 2011 Joyent, Inc. All Rights Reserved.
27 * The userland / kernel interface was initially defined by the Linux KVM
28 * project. As a part of our efforts to port it, it's important to maintain
29 * compatibility with the portions of that interface that we implement. A side
30 * effect of this is that we require GNU extensions to C. Rather than let a
31 * consumer go crazy trying to understand and track down odd compiler errors, we
32 * explicitly note that this file is not ISO C.
35 #error "The KVM Header files require GNU C extensions for compatibility."
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/ioccom.h>
43 #define KVM_API_VERSION 12 /* same as linux (for qemu compatability...) */
45 /* for KVM_CREATE_MEMORY_REGION */
46 typedef struct kvm_memory_region
{
49 uint64_t guest_phys_addr
;
50 uint64_t memory_size
; /* bytes */
51 } kvm_memory_region_t
;
53 /* for KVM_SET_USER_MEMORY_REGION */
54 typedef struct kvm_userspace_memory_region
{
57 uint64_t guest_phys_addr
;
58 uint64_t memory_size
; /* bytes */
59 uint64_t userspace_addr
; /* start of the userspace allocated memory */
60 } kvm_userspace_memory_region_t
;
62 /* for kvm_memory_region::flags */
63 #define KVM_MEM_LOG_DIRTY_PAGES 1UL
64 #define KVM_MEMSLOT_INVALID (1UL << 1)
66 /* for KVM_IRQ_LINE */
67 typedef struct kvm_irq_level
{
69 * ACPI gsi notion of irq.
70 * For IA-64 (APIC model) IOAPIC0: irq 0-23; IOAPIC1: irq 24-47..
71 * For X86 (standard AT mode) PIC0/1: irq 0-15. IOAPIC0: 0-23..
80 typedef struct kvm_irqchip
{
84 char dummy
[512]; /* reserving space */
85 struct kvm_pic_state pic
;
86 struct kvm_ioapic_state ioapic
;
90 /* for KVM_CREATE_PIT2 */
91 typedef struct kvm_pit_config
{
96 #define KVM_PIT_SPEAKER_DUMMY 1
98 #define KVM_EXIT_UNKNOWN 0
99 #define KVM_EXIT_EXCEPTION 1
100 #define KVM_EXIT_IO 2
101 #define KVM_EXIT_HYPERCALL 3
102 #define KVM_EXIT_DEBUG 4
103 #define KVM_EXIT_HLT 5
104 #define KVM_EXIT_MMIO 6
105 #define KVM_EXIT_IRQ_WINDOW_OPEN 7
106 #define KVM_EXIT_SHUTDOWN 8
107 #define KVM_EXIT_FAIL_ENTRY 9
108 #define KVM_EXIT_INTR 10
109 #define KVM_EXIT_SET_TPR 11
110 #define KVM_EXIT_TPR_ACCESS 12
111 #define KVM_EXIT_S390_SIEIC 13
112 #define KVM_EXIT_S390_RESET 14
113 #define KVM_EXIT_DCR 15
114 #define KVM_EXIT_NMI 16
115 #define KVM_EXIT_INTERNAL_ERROR 17
117 /* For KVM_EXIT_INTERNAL_ERROR */
118 #define KVM_INTERNAL_ERROR_EMULATION 1
119 #define KVM_INTERNAL_ERROR_SIMUL_EX 2
121 /* for KVM_RUN, returned by mmap(vcpu_fd, offset=0) */
122 typedef struct kvm_run
{
124 unsigned char request_interrupt_window
;
125 unsigned char padding1
[7];
128 uint32_t exit_reason
;
129 unsigned char ready_for_interrupt_injection
;
130 unsigned char if_flag
;
131 unsigned char padding2
[2];
133 /* in (pre_kvm_run), out (post_kvm_run) */
138 /* KVM_EXIT_UNKNOWN */
140 uint64_t hardware_exit_reason
;
142 /* KVM_EXIT_FAIL_ENTRY */
144 uint64_t hardware_entry_failure_reason
;
146 /* KVM_EXIT_EXCEPTION */
153 #define KVM_EXIT_IO_IN 0
154 #define KVM_EXIT_IO_OUT 1
155 unsigned char direction
;
156 unsigned char size
; /* bytes */
159 uint64_t data_offset
; /* relative to kvm_run start */
162 struct kvm_debug_exit_arch arch
;
167 unsigned char data
[8];
169 unsigned char is_write
;
171 /* KVM_EXIT_HYPERCALL */
179 /* KVM_EXIT_TPR_ACCESS */
189 unsigned char is_write
;
193 /* Available with KVM_CAP_INTERNAL_ERROR_DATA: */
197 /* Fix the size of the union. */
202 typedef struct kvm_coalesced_mmio_zone
{
206 } kvm_coalesced_mmio_zone_t
;
208 typedef struct kvm_coalesced_mmio
{
212 unsigned char data
[8];
213 } kvm_coalesced_mmio_t
;
215 typedef struct kvm_coalesced_mmio_ring
{
216 uint32_t first
, last
;
217 struct kvm_coalesced_mmio coalesced_mmio
[1];
218 } kvm_coalesced_mmio_ring_t
;
220 #define KVM_COALESCED_MMIO_MAX \
221 ((PAGESIZE - sizeof (struct kvm_coalesced_mmio_ring)) / \
222 sizeof (struct kvm_coalesced_mmio))
224 /* for KVM_INTERRUPT */
225 typedef struct kvm_interrupt
{
230 /* for KVM_GET_DIRTY_LOG */
231 typedef struct kvm_dirty_log
{
235 void *dirty_bitmap
; /* one bit per page */
240 /* for KVM_SET_SIGNAL_MASK */
241 typedef struct kvm_signal_mask
{
246 /* for KVM_TPR_ACCESS_REPORTING */
247 typedef struct kvm_tpr_access_ctl
{
250 uint32_t reserved
[8];
251 } kvm_tpr_access_ctl_t
;
253 /* for KVM_SET_VAPIC_ADDR */
254 typedef struct kvm_vapic_addr
{
258 /* for KVM_SET_MP_STATE */
259 #define KVM_MP_STATE_RUNNABLE 0
260 #define KVM_MP_STATE_UNINITIALIZED 1
261 #define KVM_MP_STATE_INIT_RECEIVED 2
262 #define KVM_MP_STATE_HALTED 3
263 #define KVM_MP_STATE_SIPI_RECEIVED 4
265 typedef struct kvm_mp_state
{
269 /* for KVM_SET_GUEST_DEBUG */
271 #define KVM_GUESTDBG_ENABLE 0x00000001
272 #define KVM_GUESTDBG_SINGLESTEP 0x00000002
274 typedef struct kvm_guest_debug
{
277 struct kvm_guest_debug_arch arch
;
285 * ioctls for /dev/kvm fds:
287 #define KVM_GET_API_VERSION _IO(KVMIO, 0x00)
288 #define KVM_CREATE_VM _IO(KVMIO, 0x01) /* returns a VM fd */
289 #define KVM_GET_MSR_INDEX_LIST _IOWR(KVMIO, 0x02, struct kvm_msr_list)
290 #define KVM_CLONE _IO(KVMIO, 0x20)
291 #define KVM_NET_QUEUE _IO(KVMIO, 0x21)
294 * Check if a kvm extension is available. Argument is extension number,
295 * return is 1 (yes) or 0 (no, sorry).
297 #define KVM_CHECK_EXTENSION _IO(KVMIO, 0x03)
300 * Get size for mmap(vcpu_fd)
302 #define KVM_GET_VCPU_MMAP_SIZE _IO(KVMIO, 0x04) /* in bytes */
303 #define KVM_GET_SUPPORTED_CPUID _IOWR(KVMIO, 0x05, struct kvm_cpuid2)
306 * Extension capability list.
308 #define KVM_CAP_IRQCHIP 0
309 #define KVM_CAP_HLT 1
310 #define KVM_CAP_MMU_SHADOW_CACHE_CONTROL 2
311 #define KVM_CAP_USER_MEMORY 3
312 #define KVM_CAP_SET_TSS_ADDR 4
313 #define KVM_CAP_VAPIC 6
314 #define KVM_CAP_EXT_CPUID 7
315 #define KVM_CAP_CLOCKSOURCE 8
316 #define KVM_CAP_NR_VCPUS 9
317 #define KVM_CAP_NR_MEMSLOTS 10
318 #define KVM_CAP_PIT 11
319 #define KVM_CAP_NOP_IO_DELAY 12
320 #define KVM_CAP_PV_MMU 13
321 #define KVM_CAP_MP_STATE 14
322 #define KVM_CAP_COALESCED_MMIO 15
323 #define KVM_CAP_SYNC_MMU 16
325 #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
326 #define KVM_CAP_DEVICE_ASSIGNMENT 17
329 #define KVM_CAP_IOMMU 18
331 #ifdef __KVM_HAVE_MSI
332 #define KVM_CAP_DEVICE_MSI 20
335 /* Bug in KVM_SET_USER_MEMORY_REGION fixed: */
336 #define KVM_CAP_DESTROY_MEMORY_REGION_WORKS 21
338 #define KVM_CAP_USER_NMI 22
340 #ifdef __KVM_HAVE_GUEST_DEBUG
341 #define KVM_CAP_SET_GUEST_DEBUG 23
343 #define KVM_CAP_REINJECT_CONTROL 24
344 #define KVM_CAP_IRQ_ROUTING 25
345 #define KVM_CAP_IRQ_INJECT_STATUS 26
346 #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
347 #define KVM_CAP_DEVICE_DEASSIGNMENT 27
349 #ifdef __KVM_HAVE_MSIX
350 #define KVM_CAP_DEVICE_MSIX 28
352 #define KVM_CAP_ASSIGN_DEV_IRQ 29
353 /* Another bug in KVM_SET_USER_MEMORY_REGION fixed: */
354 #define KVM_CAP_JOIN_MEMORY_REGIONS_WORKS 30
355 #define KVM_CAP_MCE 31
356 #define KVM_CAP_IRQFD 32
357 #define KVM_CAP_PIT2 33
358 #define KVM_CAP_SET_BOOT_CPU_ID 34
359 #define KVM_CAP_PIT_STATE2 35
360 #define KVM_CAP_IOEVENTFD 36
361 #define KVM_CAP_SET_IDENTITY_MAP_ADDR 37
362 #define KVM_CAP_XEN_HVM 38
363 #define KVM_CAP_ADJUST_CLOCK 39
364 #define KVM_CAP_INTERNAL_ERROR_DATA 40
365 #define KVM_CAP_VCPU_EVENTS 41
366 #define KVM_CAP_S390_PSW 42
367 #define KVM_CAP_PPC_SEGSTATE 43
368 #define KVM_CAP_HYPERV 44
369 #define KVM_CAP_HYPERV_VAPIC 45
370 #define KVM_CAP_HYPERV_SPIN 46
371 #define KVM_CAP_PCI_SEGMENT 47
372 #define KVM_CAP_X86_ROBUST_SINGLESTEP 51
374 #ifdef KVM_CAP_IRQ_ROUTING
375 typedef struct kvm_irq_routing_irqchip
{
378 } kvm_irq_routing_irqchip_t
;
380 typedef struct kvm_irq_routing_msi
{
385 } kvm_irq_routing_msi_t
;
387 /* gsi routing entry types */
388 #define KVM_IRQ_ROUTING_IRQCHIP 1
389 #define KVM_IRQ_ROUTING_MSI 2
391 typedef struct kvm_irq_routing_entry
{
397 struct kvm_irq_routing_irqchip irqchip
;
398 struct kvm_irq_routing_msi msi
;
401 } kvm_irq_routing_entry_t
;
403 typedef struct kvm_irq_routing
{
406 struct kvm_irq_routing_entry entries
[1];
409 #endif /* KVM_CAP_IRQ_ROUTING */
413 typedef struct kvm_x86_mce
{
422 #endif /* KVM_CAP_MCE */
424 typedef struct kvm_clock_data
{
435 * KVM_CREATE_VCPU receives as a parameter the vcpu slot, and returns
438 #define KVM_CREATE_VCPU _IO(KVMIO, 0x41)
439 #define KVM_GET_DIRTY_LOG _IOW(KVMIO, 0x42, struct kvm_dirty_log)
440 #define KVM_SET_NR_MMU_PAGES _IO(KVMIO, 0x44)
441 #define KVM_GET_NR_MMU_PAGES _IO(KVMIO, 0x45)
442 #define KVM_SET_USER_MEMORY_REGION _IOW(KVMIO, 0x46, \
443 struct kvm_userspace_memory_region)
445 #define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)
446 #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, uint64_t)
448 /* Device model IOC */
449 #define KVM_CREATE_IRQCHIP _IO(KVMIO, 0x60)
450 #define KVM_IRQ_LINE _IOW(KVMIO, 0x61, struct kvm_irq_level)
451 #define KVM_GET_IRQCHIP _IOWR(KVMIO, 0x62, struct kvm_irqchip)
452 #define KVM_SET_IRQCHIP _IOR(KVMIO, 0x63, struct kvm_irqchip)
453 #define KVM_CREATE_PIT _IO(KVMIO, 0x64)
454 #define KVM_GET_PIT _IOWR(KVMIO, 0x65, struct kvm_pit_state)
455 #define KVM_SET_PIT _IOR(KVMIO, 0x66, struct kvm_pit_state)
456 #define KVM_IRQ_LINE_STATUS _IOWR(KVMIO, 0x67, struct kvm_irq_level)
458 #define KVM_REGISTER_COALESCED_MMIO _IOW(KVMIO, 0x67, \
459 struct kvm_coalesced_mmio_zone)
460 #define KVM_UNREGISTER_COALESCED_MMIO _IOW(KVMIO, 0x68, \
461 struct kvm_coalesced_mmio_zone)
462 #define KVM_SET_GSI_ROUTING _IOW(KVMIO, 0x6a, struct kvm_irq_routing)
463 #define KVM_REINJECT_CONTROL _IO(KVMIO, 0x71)
464 #define KVM_CREATE_PIT2 _IOW(KVMIO, 0x77, struct kvm_pit_config)
465 #define KVM_SET_BOOT_CPU_ID _IO(KVMIO, 0x78)
466 #define KVM_SET_CLOCK _IOW(KVMIO, 0x7b, struct kvm_clock_data)
467 #define KVM_GET_CLOCK _IOR(KVMIO, 0x7c, struct kvm_clock_data)
468 /* Available with KVM_CAP_PIT_STATE2 */
469 #define KVM_GET_PIT2 _IOR(KVMIO, 0x9f, struct kvm_pit_state2)
470 #define KVM_SET_PIT2 _IOW(KVMIO, 0xa0, struct kvm_pit_state2)
473 * ioctls for vcpu fds
475 #define KVM_RUN _IO(KVMIO, 0x80)
476 #define KVM_GET_REGS _IOR(KVMIO, 0x81, struct kvm_regs)
477 #define KVM_SET_REGS _IOW(KVMIO, 0x82, struct kvm_regs)
478 #define KVM_GET_SREGS _IOR(KVMIO, 0x83, struct kvm_sregs)
479 #define KVM_SET_SREGS _IOW(KVMIO, 0x84, struct kvm_sregs)
480 #define KVM_INTERRUPT _IOW(KVMIO, 0x86, struct kvm_interrupt)
481 #define KVM_GET_MSRS _IOWR(KVMIO, 0x88, struct kvm_msrs)
482 #define KVM_SET_MSRS _IOW(KVMIO, 0x89, struct kvm_msrs)
483 #define KVM_SET_CPUID _IOW(KVMIO, 0x8a, struct kvm_cpuid)
484 #define KVM_SET_SIGNAL_MASK _IOW(KVMIO, 0x8b, struct kvm_signal_mask)
485 #define KVM_GET_FPU _IOR(KVMIO, 0x8c, struct kvm_fpu)
486 #define KVM_SET_FPU _IOW(KVMIO, 0x8d, struct kvm_fpu)
487 #define KVM_GET_LAPIC _IOR(KVMIO, 0x8e, struct kvm_lapic_state)
488 #define KVM_SET_LAPIC _IOW(KVMIO, 0x8f, struct kvm_lapic_state)
489 #define KVM_SET_CPUID2 _IOW(KVMIO, 0x90, struct kvm_cpuid2)
490 #define KVM_GET_CPUID2 _IOWR(KVMIO, 0x91, struct kvm_cpuid2)
491 /* Available with KVM_CAP_VAPIC */
492 #define KVM_TPR_ACCESS_REPORTING _IOWR(KVMIO, 0x92, struct kvm_tpr_access_ctl)
493 /* Available with KVM_CAP_VAPIC */
494 #define KVM_SET_VAPIC_ADDR _IOW(KVMIO, 0x93, struct kvm_vapic_addr)
495 #define KVM_GET_MP_STATE _IOR(KVMIO, 0x98, struct kvm_mp_state)
496 #define KVM_SET_MP_STATE _IOW(KVMIO, 0x99, struct kvm_mp_state)
497 /* Available with KVM_CAP_NMI */
498 #define KVM_NMI _IO(KVMIO, 0x9a)
500 #define KVM_X86_SETUP_MCE _IOW(KVMIO, 0x9c, uint64_t)
501 #define KVM_X86_GET_MCE_CAP_SUPPORTED _IOR(KVMIO, 0x9d, uint64_t)
502 #define KVM_X86_SET_MCE _IOW(KVMIO, 0x9e, struct kvm_x86_mce)
503 /* Available with KVM_CAP_VCPU_EVENTS */
504 #define KVM_GET_VCPU_EVENTS _IOR(KVMIO, 0x9f, struct kvm_vcpu_events)
505 #define KVM_SET_VCPU_EVENTS _IOW(KVMIO, 0xa0, struct kvm_vcpu_events)