Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | #ifndef __LINUX_KVM_H |
| 2 | #define __LINUX_KVM_H |
| 3 | |
| 4 | /* |
| 5 | * Userspace interface for /dev/kvm - kernel based virtual machine |
| 6 | * |
| 7 | * Note: this interface is considered experimental and may change without |
| 8 | * notice. |
| 9 | */ |
| 10 | |
| 11 | #include <asm/types.h> |
| 12 | #include <linux/ioctl.h> |
| 13 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame^] | 14 | #define KVM_API_VERSION 5 |
Avi Kivity | 0b76e20 | 2006-12-22 01:06:02 -0800 | [diff] [blame] | 15 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 16 | /* |
| 17 | * Architectural interrupt line count, and the size of the bitmap needed |
| 18 | * to hold them. |
| 19 | */ |
| 20 | #define KVM_NR_INTERRUPTS 256 |
| 21 | #define KVM_IRQ_BITMAP_SIZE_BYTES ((KVM_NR_INTERRUPTS + 7) / 8) |
| 22 | #define KVM_IRQ_BITMAP_SIZE(type) (KVM_IRQ_BITMAP_SIZE_BYTES / sizeof(type)) |
| 23 | |
| 24 | |
| 25 | /* for KVM_CREATE_MEMORY_REGION */ |
| 26 | struct kvm_memory_region { |
| 27 | __u32 slot; |
| 28 | __u32 flags; |
| 29 | __u64 guest_phys_addr; |
| 30 | __u64 memory_size; /* bytes */ |
| 31 | }; |
| 32 | |
| 33 | /* for kvm_memory_region::flags */ |
| 34 | #define KVM_MEM_LOG_DIRTY_PAGES 1UL |
| 35 | |
| 36 | |
| 37 | #define KVM_EXIT_TYPE_FAIL_ENTRY 1 |
| 38 | #define KVM_EXIT_TYPE_VM_EXIT 2 |
| 39 | |
| 40 | enum kvm_exit_reason { |
| 41 | KVM_EXIT_UNKNOWN = 0, |
| 42 | KVM_EXIT_EXCEPTION = 1, |
| 43 | KVM_EXIT_IO = 2, |
| 44 | KVM_EXIT_CPUID = 3, |
| 45 | KVM_EXIT_DEBUG = 4, |
| 46 | KVM_EXIT_HLT = 5, |
| 47 | KVM_EXIT_MMIO = 6, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 48 | KVM_EXIT_IRQ_WINDOW_OPEN = 7, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 49 | KVM_EXIT_SHUTDOWN = 8, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame^] | 52 | /* for KVM_RUN, returned by mmap(vcpu_fd, offset=0) */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 53 | struct kvm_run { |
| 54 | /* in */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 55 | __u32 emulated; /* skip current instruction */ |
| 56 | __u32 mmio_completed; /* mmio request completed */ |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 57 | __u8 request_interrupt_window; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 58 | __u8 padding1[7]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 59 | |
| 60 | /* out */ |
| 61 | __u32 exit_type; |
| 62 | __u32 exit_reason; |
| 63 | __u32 instruction_length; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 64 | __u8 ready_for_interrupt_injection; |
| 65 | __u8 if_flag; |
| 66 | __u16 padding2; |
Dor Laor | 5481034 | 2007-02-12 00:54:39 -0800 | [diff] [blame] | 67 | |
| 68 | /* in (pre_kvm_run), out (post_kvm_run) */ |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 69 | __u64 cr8; |
| 70 | __u64 apic_base; |
| 71 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 72 | union { |
| 73 | /* KVM_EXIT_UNKNOWN */ |
| 74 | struct { |
| 75 | __u32 hardware_exit_reason; |
| 76 | } hw; |
| 77 | /* KVM_EXIT_EXCEPTION */ |
| 78 | struct { |
| 79 | __u32 exception; |
| 80 | __u32 error_code; |
| 81 | } ex; |
| 82 | /* KVM_EXIT_IO */ |
| 83 | struct { |
| 84 | #define KVM_EXIT_IO_IN 0 |
| 85 | #define KVM_EXIT_IO_OUT 1 |
| 86 | __u8 direction; |
| 87 | __u8 size; /* bytes */ |
| 88 | __u8 string; |
| 89 | __u8 string_down; |
| 90 | __u8 rep; |
| 91 | __u8 pad; |
| 92 | __u16 port; |
| 93 | __u64 count; |
| 94 | union { |
| 95 | __u64 address; |
| 96 | __u32 value; |
| 97 | }; |
| 98 | } io; |
| 99 | struct { |
| 100 | } debug; |
| 101 | /* KVM_EXIT_MMIO */ |
| 102 | struct { |
| 103 | __u64 phys_addr; |
| 104 | __u8 data[8]; |
| 105 | __u32 len; |
| 106 | __u8 is_write; |
| 107 | } mmio; |
| 108 | }; |
| 109 | }; |
| 110 | |
| 111 | /* for KVM_GET_REGS and KVM_SET_REGS */ |
| 112 | struct kvm_regs { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 113 | /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */ |
| 114 | __u64 rax, rbx, rcx, rdx; |
| 115 | __u64 rsi, rdi, rsp, rbp; |
| 116 | __u64 r8, r9, r10, r11; |
| 117 | __u64 r12, r13, r14, r15; |
| 118 | __u64 rip, rflags; |
| 119 | }; |
| 120 | |
| 121 | struct kvm_segment { |
| 122 | __u64 base; |
| 123 | __u32 limit; |
| 124 | __u16 selector; |
| 125 | __u8 type; |
| 126 | __u8 present, dpl, db, s, l, g, avl; |
| 127 | __u8 unusable; |
| 128 | __u8 padding; |
| 129 | }; |
| 130 | |
| 131 | struct kvm_dtable { |
| 132 | __u64 base; |
| 133 | __u16 limit; |
| 134 | __u16 padding[3]; |
| 135 | }; |
| 136 | |
| 137 | /* for KVM_GET_SREGS and KVM_SET_SREGS */ |
| 138 | struct kvm_sregs { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 139 | /* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */ |
| 140 | struct kvm_segment cs, ds, es, fs, gs, ss; |
| 141 | struct kvm_segment tr, ldt; |
| 142 | struct kvm_dtable gdt, idt; |
| 143 | __u64 cr0, cr2, cr3, cr4, cr8; |
| 144 | __u64 efer; |
| 145 | __u64 apic_base; |
| 146 | __u64 interrupt_bitmap[KVM_IRQ_BITMAP_SIZE(__u64)]; |
| 147 | }; |
| 148 | |
| 149 | struct kvm_msr_entry { |
| 150 | __u32 index; |
| 151 | __u32 reserved; |
| 152 | __u64 data; |
| 153 | }; |
| 154 | |
| 155 | /* for KVM_GET_MSRS and KVM_SET_MSRS */ |
| 156 | struct kvm_msrs { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 157 | __u32 nmsrs; /* number of msrs in entries */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 158 | __u32 pad; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 159 | |
| 160 | struct kvm_msr_entry entries[0]; |
| 161 | }; |
| 162 | |
| 163 | /* for KVM_GET_MSR_INDEX_LIST */ |
| 164 | struct kvm_msr_list { |
| 165 | __u32 nmsrs; /* number of msrs in entries */ |
| 166 | __u32 indices[0]; |
| 167 | }; |
| 168 | |
| 169 | /* for KVM_TRANSLATE */ |
| 170 | struct kvm_translation { |
| 171 | /* in */ |
| 172 | __u64 linear_address; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 173 | |
| 174 | /* out */ |
| 175 | __u64 physical_address; |
| 176 | __u8 valid; |
| 177 | __u8 writeable; |
| 178 | __u8 usermode; |
Avi Kivity | 8cd1330 | 2007-02-12 00:54:41 -0800 | [diff] [blame] | 179 | __u8 pad[5]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | /* for KVM_INTERRUPT */ |
| 183 | struct kvm_interrupt { |
| 184 | /* in */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 185 | __u32 irq; |
| 186 | }; |
| 187 | |
| 188 | struct kvm_breakpoint { |
| 189 | __u32 enabled; |
| 190 | __u32 padding; |
| 191 | __u64 address; |
| 192 | }; |
| 193 | |
| 194 | /* for KVM_DEBUG_GUEST */ |
| 195 | struct kvm_debug_guest { |
| 196 | /* int */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 197 | __u32 enabled; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 198 | __u32 pad; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 199 | struct kvm_breakpoint breakpoints[4]; |
| 200 | __u32 singlestep; |
| 201 | }; |
| 202 | |
| 203 | /* for KVM_GET_DIRTY_LOG */ |
| 204 | struct kvm_dirty_log { |
| 205 | __u32 slot; |
| 206 | __u32 padding; |
| 207 | union { |
| 208 | void __user *dirty_bitmap; /* one bit per page */ |
| 209 | __u64 padding; |
| 210 | }; |
| 211 | }; |
| 212 | |
| 213 | #define KVMIO 0xAE |
| 214 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 215 | /* |
| 216 | * ioctls for /dev/kvm fds: |
| 217 | */ |
Avi Kivity | 0b76e20 | 2006-12-22 01:06:02 -0800 | [diff] [blame] | 218 | #define KVM_GET_API_VERSION _IO(KVMIO, 1) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 219 | #define KVM_CREATE_VM _IO(KVMIO, 2) /* returns a VM fd */ |
| 220 | #define KVM_GET_MSR_INDEX_LIST _IOWR(KVMIO, 15, struct kvm_msr_list) |
| 221 | |
| 222 | /* |
| 223 | * ioctls for VM fds |
| 224 | */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 225 | #define KVM_SET_MEMORY_REGION _IOW(KVMIO, 10, struct kvm_memory_region) |
| 226 | /* |
| 227 | * KVM_CREATE_VCPU receives as a parameter the vcpu slot, and returns |
| 228 | * a vcpu fd. |
| 229 | */ |
| 230 | #define KVM_CREATE_VCPU _IOW(KVMIO, 11, int) |
| 231 | #define KVM_GET_DIRTY_LOG _IOW(KVMIO, 12, struct kvm_dirty_log) |
| 232 | |
| 233 | /* |
| 234 | * ioctls for vcpu fds |
| 235 | */ |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame^] | 236 | #define KVM_RUN _IO(KVMIO, 16) |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 237 | #define KVM_GET_REGS _IOR(KVMIO, 3, struct kvm_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 238 | #define KVM_SET_REGS _IOW(KVMIO, 4, struct kvm_regs) |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 239 | #define KVM_GET_SREGS _IOR(KVMIO, 5, struct kvm_sregs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 240 | #define KVM_SET_SREGS _IOW(KVMIO, 6, struct kvm_sregs) |
| 241 | #define KVM_TRANSLATE _IOWR(KVMIO, 7, struct kvm_translation) |
| 242 | #define KVM_INTERRUPT _IOW(KVMIO, 8, struct kvm_interrupt) |
| 243 | #define KVM_DEBUG_GUEST _IOW(KVMIO, 9, struct kvm_debug_guest) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 244 | #define KVM_GET_MSRS _IOWR(KVMIO, 13, struct kvm_msrs) |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 245 | #define KVM_SET_MSRS _IOW(KVMIO, 14, struct kvm_msrs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 246 | |
| 247 | #endif |