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 | b4e63f5 | 2007-03-04 13:59:30 +0200 | [diff] [blame^] | 14 | #define KVM_API_VERSION 7 |
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, |
Avi Kivity | b4e63f5 | 2007-03-04 13:59:30 +0200 | [diff] [blame^] | 44 | KVM_EXIT_HYPERCALL = 3, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 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 | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 55 | __u32 io_completed; /* mmio/pio request completed */ |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 56 | __u8 request_interrupt_window; |
Avi Kivity | 106b552 | 2007-03-01 16:20:40 +0200 | [diff] [blame] | 57 | __u8 padding1[3]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 58 | |
| 59 | /* out */ |
| 60 | __u32 exit_type; |
| 61 | __u32 exit_reason; |
| 62 | __u32 instruction_length; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 63 | __u8 ready_for_interrupt_injection; |
| 64 | __u8 if_flag; |
| 65 | __u16 padding2; |
Dor Laor | 5481034 | 2007-02-12 00:54:39 -0800 | [diff] [blame] | 66 | |
| 67 | /* in (pre_kvm_run), out (post_kvm_run) */ |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 68 | __u64 cr8; |
| 69 | __u64 apic_base; |
| 70 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | union { |
| 72 | /* KVM_EXIT_UNKNOWN */ |
| 73 | struct { |
| 74 | __u32 hardware_exit_reason; |
| 75 | } hw; |
| 76 | /* KVM_EXIT_EXCEPTION */ |
| 77 | struct { |
| 78 | __u32 exception; |
| 79 | __u32 error_code; |
| 80 | } ex; |
| 81 | /* KVM_EXIT_IO */ |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 82 | struct kvm_io { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 83 | #define KVM_EXIT_IO_IN 0 |
| 84 | #define KVM_EXIT_IO_OUT 1 |
| 85 | __u8 direction; |
| 86 | __u8 size; /* bytes */ |
| 87 | __u8 string; |
| 88 | __u8 string_down; |
| 89 | __u8 rep; |
| 90 | __u8 pad; |
| 91 | __u16 port; |
| 92 | __u64 count; |
| 93 | union { |
| 94 | __u64 address; |
| 95 | __u32 value; |
| 96 | }; |
| 97 | } io; |
| 98 | struct { |
| 99 | } debug; |
| 100 | /* KVM_EXIT_MMIO */ |
| 101 | struct { |
| 102 | __u64 phys_addr; |
| 103 | __u8 data[8]; |
| 104 | __u32 len; |
| 105 | __u8 is_write; |
| 106 | } mmio; |
Avi Kivity | b4e63f5 | 2007-03-04 13:59:30 +0200 | [diff] [blame^] | 107 | /* KVM_EXIT_HYPERCALL */ |
| 108 | struct { |
| 109 | __u64 args[6]; |
| 110 | __u64 ret; |
| 111 | __u32 longmode; |
| 112 | __u32 pad; |
| 113 | } hypercall; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 114 | }; |
| 115 | }; |
| 116 | |
| 117 | /* for KVM_GET_REGS and KVM_SET_REGS */ |
| 118 | struct kvm_regs { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 119 | /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */ |
| 120 | __u64 rax, rbx, rcx, rdx; |
| 121 | __u64 rsi, rdi, rsp, rbp; |
| 122 | __u64 r8, r9, r10, r11; |
| 123 | __u64 r12, r13, r14, r15; |
| 124 | __u64 rip, rflags; |
| 125 | }; |
| 126 | |
| 127 | struct kvm_segment { |
| 128 | __u64 base; |
| 129 | __u32 limit; |
| 130 | __u16 selector; |
| 131 | __u8 type; |
| 132 | __u8 present, dpl, db, s, l, g, avl; |
| 133 | __u8 unusable; |
| 134 | __u8 padding; |
| 135 | }; |
| 136 | |
| 137 | struct kvm_dtable { |
| 138 | __u64 base; |
| 139 | __u16 limit; |
| 140 | __u16 padding[3]; |
| 141 | }; |
| 142 | |
| 143 | /* for KVM_GET_SREGS and KVM_SET_SREGS */ |
| 144 | struct kvm_sregs { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 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; |
| 150 | __u64 efer; |
| 151 | __u64 apic_base; |
| 152 | __u64 interrupt_bitmap[KVM_IRQ_BITMAP_SIZE(__u64)]; |
| 153 | }; |
| 154 | |
| 155 | struct kvm_msr_entry { |
| 156 | __u32 index; |
| 157 | __u32 reserved; |
| 158 | __u64 data; |
| 159 | }; |
| 160 | |
| 161 | /* for KVM_GET_MSRS and KVM_SET_MSRS */ |
| 162 | struct kvm_msrs { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 163 | __u32 nmsrs; /* number of msrs in entries */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 164 | __u32 pad; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 165 | |
| 166 | struct kvm_msr_entry entries[0]; |
| 167 | }; |
| 168 | |
| 169 | /* for KVM_GET_MSR_INDEX_LIST */ |
| 170 | struct kvm_msr_list { |
| 171 | __u32 nmsrs; /* number of msrs in entries */ |
| 172 | __u32 indices[0]; |
| 173 | }; |
| 174 | |
| 175 | /* for KVM_TRANSLATE */ |
| 176 | struct kvm_translation { |
| 177 | /* in */ |
| 178 | __u64 linear_address; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 179 | |
| 180 | /* out */ |
| 181 | __u64 physical_address; |
| 182 | __u8 valid; |
| 183 | __u8 writeable; |
| 184 | __u8 usermode; |
Avi Kivity | 8cd1330 | 2007-02-12 00:54:41 -0800 | [diff] [blame] | 185 | __u8 pad[5]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | /* for KVM_INTERRUPT */ |
| 189 | struct kvm_interrupt { |
| 190 | /* in */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 191 | __u32 irq; |
| 192 | }; |
| 193 | |
| 194 | struct kvm_breakpoint { |
| 195 | __u32 enabled; |
| 196 | __u32 padding; |
| 197 | __u64 address; |
| 198 | }; |
| 199 | |
| 200 | /* for KVM_DEBUG_GUEST */ |
| 201 | struct kvm_debug_guest { |
| 202 | /* int */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 203 | __u32 enabled; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 204 | __u32 pad; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 205 | struct kvm_breakpoint breakpoints[4]; |
| 206 | __u32 singlestep; |
| 207 | }; |
| 208 | |
| 209 | /* for KVM_GET_DIRTY_LOG */ |
| 210 | struct kvm_dirty_log { |
| 211 | __u32 slot; |
| 212 | __u32 padding; |
| 213 | union { |
| 214 | void __user *dirty_bitmap; /* one bit per page */ |
| 215 | __u64 padding; |
| 216 | }; |
| 217 | }; |
| 218 | |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 219 | struct kvm_cpuid_entry { |
| 220 | __u32 function; |
| 221 | __u32 eax; |
| 222 | __u32 ebx; |
| 223 | __u32 ecx; |
| 224 | __u32 edx; |
| 225 | __u32 padding; |
| 226 | }; |
| 227 | |
| 228 | /* for KVM_SET_CPUID */ |
| 229 | struct kvm_cpuid { |
| 230 | __u32 nent; |
| 231 | __u32 padding; |
| 232 | struct kvm_cpuid_entry entries[0]; |
| 233 | }; |
| 234 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 235 | #define KVMIO 0xAE |
| 236 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 237 | /* |
| 238 | * ioctls for /dev/kvm fds: |
| 239 | */ |
Avi Kivity | 739872c | 2007-03-01 17:20:13 +0200 | [diff] [blame] | 240 | #define KVM_GET_API_VERSION _IO(KVMIO, 0x00) |
| 241 | #define KVM_CREATE_VM _IO(KVMIO, 0x01) /* returns a VM fd */ |
| 242 | #define KVM_GET_MSR_INDEX_LIST _IOWR(KVMIO, 0x02, struct kvm_msr_list) |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 243 | /* |
| 244 | * Check if a kvm extension is available. Argument is extension number, |
| 245 | * return is 1 (yes) or 0 (no, sorry). |
| 246 | */ |
| 247 | #define KVM_CHECK_EXTENSION _IO(KVMIO, 0x03) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * ioctls for VM fds |
| 251 | */ |
Avi Kivity | 739872c | 2007-03-01 17:20:13 +0200 | [diff] [blame] | 252 | #define KVM_SET_MEMORY_REGION _IOW(KVMIO, 0x40, struct kvm_memory_region) |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 253 | /* |
| 254 | * KVM_CREATE_VCPU receives as a parameter the vcpu slot, and returns |
| 255 | * a vcpu fd. |
| 256 | */ |
Avi Kivity | 739872c | 2007-03-01 17:20:13 +0200 | [diff] [blame] | 257 | #define KVM_CREATE_VCPU _IO(KVMIO, 0x41) |
| 258 | #define KVM_GET_DIRTY_LOG _IOW(KVMIO, 0x42, struct kvm_dirty_log) |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * ioctls for vcpu fds |
| 262 | */ |
Avi Kivity | 739872c | 2007-03-01 17:20:13 +0200 | [diff] [blame] | 263 | #define KVM_RUN _IO(KVMIO, 0x80) |
| 264 | #define KVM_GET_REGS _IOR(KVMIO, 0x81, struct kvm_regs) |
| 265 | #define KVM_SET_REGS _IOW(KVMIO, 0x82, struct kvm_regs) |
| 266 | #define KVM_GET_SREGS _IOR(KVMIO, 0x83, struct kvm_sregs) |
| 267 | #define KVM_SET_SREGS _IOW(KVMIO, 0x84, struct kvm_sregs) |
| 268 | #define KVM_TRANSLATE _IOWR(KVMIO, 0x85, struct kvm_translation) |
| 269 | #define KVM_INTERRUPT _IOW(KVMIO, 0x86, struct kvm_interrupt) |
| 270 | #define KVM_DEBUG_GUEST _IOW(KVMIO, 0x87, struct kvm_debug_guest) |
| 271 | #define KVM_GET_MSRS _IOWR(KVMIO, 0x88, struct kvm_msrs) |
| 272 | #define KVM_SET_MSRS _IOW(KVMIO, 0x89, struct kvm_msrs) |
| 273 | #define KVM_SET_CPUID _IOW(KVMIO, 0x8a, struct kvm_cpuid) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 274 | |
| 275 | #endif |