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 | * |
Rusty Russell | dea8cae | 2007-07-17 23:12:26 +1000 | [diff] [blame] | 7 | * Note: you must update KVM_API_VERSION if you change this interface. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <asm/types.h> |
| 11 | #include <linux/ioctl.h> |
| 12 | |
Avi Kivity | 2ff81f7 | 2007-04-29 16:25:49 +0300 | [diff] [blame] | 13 | #define KVM_API_VERSION 12 |
Avi Kivity | 0b76e20 | 2006-12-22 01:06:02 -0800 | [diff] [blame] | 14 | |
Rusty Russell | 9eb829c | 2007-07-18 13:05:58 +1000 | [diff] [blame^] | 15 | /* Architectural interrupt line count. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 16 | #define KVM_NR_INTERRUPTS 256 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 17 | |
| 18 | /* for KVM_CREATE_MEMORY_REGION */ |
| 19 | struct kvm_memory_region { |
| 20 | __u32 slot; |
| 21 | __u32 flags; |
| 22 | __u64 guest_phys_addr; |
| 23 | __u64 memory_size; /* bytes */ |
| 24 | }; |
| 25 | |
| 26 | /* for kvm_memory_region::flags */ |
| 27 | #define KVM_MEM_LOG_DIRTY_PAGES 1UL |
| 28 | |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 29 | struct kvm_memory_alias { |
| 30 | __u32 slot; /* this has a different namespace than memory slots */ |
| 31 | __u32 flags; |
| 32 | __u64 guest_phys_addr; |
| 33 | __u64 memory_size; |
| 34 | __u64 target_phys_addr; |
| 35 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 36 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 37 | enum kvm_exit_reason { |
| 38 | KVM_EXIT_UNKNOWN = 0, |
| 39 | KVM_EXIT_EXCEPTION = 1, |
| 40 | KVM_EXIT_IO = 2, |
Avi Kivity | b4e63f5 | 2007-03-04 13:59:30 +0200 | [diff] [blame] | 41 | KVM_EXIT_HYPERCALL = 3, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | KVM_EXIT_DEBUG = 4, |
| 43 | KVM_EXIT_HLT = 5, |
| 44 | KVM_EXIT_MMIO = 6, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 45 | KVM_EXIT_IRQ_WINDOW_OPEN = 7, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 46 | KVM_EXIT_SHUTDOWN = 8, |
Avi Kivity | 8eb7d33 | 2007-03-04 14:17:08 +0200 | [diff] [blame] | 47 | KVM_EXIT_FAIL_ENTRY = 9, |
Avi Kivity | 1b19f3e | 2007-03-04 14:24:03 +0200 | [diff] [blame] | 48 | KVM_EXIT_INTR = 10, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 51 | /* for KVM_RUN, returned by mmap(vcpu_fd, offset=0) */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 52 | struct kvm_run { |
| 53 | /* in */ |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 54 | __u8 request_interrupt_window; |
Avi Kivity | 02c8320 | 2007-04-29 15:02:17 +0300 | [diff] [blame] | 55 | __u8 padding1[7]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 56 | |
| 57 | /* out */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 58 | __u32 exit_reason; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 59 | __u8 ready_for_interrupt_injection; |
| 60 | __u8 if_flag; |
Avi Kivity | 2ff81f7 | 2007-04-29 16:25:49 +0300 | [diff] [blame] | 61 | __u8 padding2[2]; |
Dor Laor | 5481034 | 2007-02-12 00:54:39 -0800 | [diff] [blame] | 62 | |
| 63 | /* in (pre_kvm_run), out (post_kvm_run) */ |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 64 | __u64 cr8; |
| 65 | __u64 apic_base; |
| 66 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 67 | union { |
| 68 | /* KVM_EXIT_UNKNOWN */ |
| 69 | struct { |
Avi Kivity | 8eb7d33 | 2007-03-04 14:17:08 +0200 | [diff] [blame] | 70 | __u64 hardware_exit_reason; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | } hw; |
Avi Kivity | 8eb7d33 | 2007-03-04 14:17:08 +0200 | [diff] [blame] | 72 | /* KVM_EXIT_FAIL_ENTRY */ |
| 73 | struct { |
| 74 | __u64 hardware_entry_failure_reason; |
| 75 | } fail_entry; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 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 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 87 | __u16 port; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 88 | __u32 count; |
| 89 | __u64 data_offset; /* relative to kvm_run start */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 90 | } io; |
| 91 | struct { |
| 92 | } debug; |
| 93 | /* KVM_EXIT_MMIO */ |
| 94 | struct { |
| 95 | __u64 phys_addr; |
| 96 | __u8 data[8]; |
| 97 | __u32 len; |
| 98 | __u8 is_write; |
| 99 | } mmio; |
Avi Kivity | b4e63f5 | 2007-03-04 13:59:30 +0200 | [diff] [blame] | 100 | /* KVM_EXIT_HYPERCALL */ |
| 101 | struct { |
| 102 | __u64 args[6]; |
| 103 | __u64 ret; |
| 104 | __u32 longmode; |
| 105 | __u32 pad; |
| 106 | } hypercall; |
Avi Kivity | 24cbc7e | 2007-07-17 11:45:55 +0300 | [diff] [blame] | 107 | /* Fix the size of the union. */ |
| 108 | char padding[256]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 109 | }; |
| 110 | }; |
| 111 | |
| 112 | /* for KVM_GET_REGS and KVM_SET_REGS */ |
| 113 | struct kvm_regs { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 114 | /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */ |
| 115 | __u64 rax, rbx, rcx, rdx; |
| 116 | __u64 rsi, rdi, rsp, rbp; |
| 117 | __u64 r8, r9, r10, r11; |
| 118 | __u64 r12, r13, r14, r15; |
| 119 | __u64 rip, rflags; |
| 120 | }; |
| 121 | |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 122 | /* for KVM_GET_FPU and KVM_SET_FPU */ |
| 123 | struct kvm_fpu { |
| 124 | __u8 fpr[8][16]; |
| 125 | __u16 fcw; |
| 126 | __u16 fsw; |
| 127 | __u8 ftwx; /* in fxsave format */ |
| 128 | __u8 pad1; |
| 129 | __u16 last_opcode; |
| 130 | __u64 last_ip; |
| 131 | __u64 last_dp; |
| 132 | __u8 xmm[16][16]; |
| 133 | __u32 mxcsr; |
| 134 | __u32 pad2; |
| 135 | }; |
| 136 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 137 | struct kvm_segment { |
| 138 | __u64 base; |
| 139 | __u32 limit; |
| 140 | __u16 selector; |
| 141 | __u8 type; |
| 142 | __u8 present, dpl, db, s, l, g, avl; |
| 143 | __u8 unusable; |
| 144 | __u8 padding; |
| 145 | }; |
| 146 | |
| 147 | struct kvm_dtable { |
| 148 | __u64 base; |
| 149 | __u16 limit; |
| 150 | __u16 padding[3]; |
| 151 | }; |
| 152 | |
| 153 | /* for KVM_GET_SREGS and KVM_SET_SREGS */ |
| 154 | struct kvm_sregs { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 155 | /* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */ |
| 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; |
Rusty Russell | 9eb829c | 2007-07-18 13:05:58 +1000 | [diff] [blame^] | 162 | __u64 interrupt_bitmap[(KVM_NR_INTERRUPTS + 63) / 64]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | struct kvm_msr_entry { |
| 166 | __u32 index; |
| 167 | __u32 reserved; |
| 168 | __u64 data; |
| 169 | }; |
| 170 | |
| 171 | /* for KVM_GET_MSRS and KVM_SET_MSRS */ |
| 172 | struct kvm_msrs { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 173 | __u32 nmsrs; /* number of msrs in entries */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 174 | __u32 pad; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 175 | |
| 176 | struct kvm_msr_entry entries[0]; |
| 177 | }; |
| 178 | |
| 179 | /* for KVM_GET_MSR_INDEX_LIST */ |
| 180 | struct kvm_msr_list { |
| 181 | __u32 nmsrs; /* number of msrs in entries */ |
| 182 | __u32 indices[0]; |
| 183 | }; |
| 184 | |
| 185 | /* for KVM_TRANSLATE */ |
| 186 | struct kvm_translation { |
| 187 | /* in */ |
| 188 | __u64 linear_address; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 189 | |
| 190 | /* out */ |
| 191 | __u64 physical_address; |
| 192 | __u8 valid; |
| 193 | __u8 writeable; |
| 194 | __u8 usermode; |
Avi Kivity | 8cd1330 | 2007-02-12 00:54:41 -0800 | [diff] [blame] | 195 | __u8 pad[5]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | /* for KVM_INTERRUPT */ |
| 199 | struct kvm_interrupt { |
| 200 | /* in */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 201 | __u32 irq; |
| 202 | }; |
| 203 | |
| 204 | struct kvm_breakpoint { |
| 205 | __u32 enabled; |
| 206 | __u32 padding; |
| 207 | __u64 address; |
| 208 | }; |
| 209 | |
| 210 | /* for KVM_DEBUG_GUEST */ |
| 211 | struct kvm_debug_guest { |
| 212 | /* int */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 213 | __u32 enabled; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 214 | __u32 pad; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 215 | struct kvm_breakpoint breakpoints[4]; |
| 216 | __u32 singlestep; |
| 217 | }; |
| 218 | |
| 219 | /* for KVM_GET_DIRTY_LOG */ |
| 220 | struct kvm_dirty_log { |
| 221 | __u32 slot; |
| 222 | __u32 padding; |
| 223 | union { |
| 224 | void __user *dirty_bitmap; /* one bit per page */ |
| 225 | __u64 padding; |
| 226 | }; |
| 227 | }; |
| 228 | |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 229 | struct kvm_cpuid_entry { |
| 230 | __u32 function; |
| 231 | __u32 eax; |
| 232 | __u32 ebx; |
| 233 | __u32 ecx; |
| 234 | __u32 edx; |
| 235 | __u32 padding; |
| 236 | }; |
| 237 | |
| 238 | /* for KVM_SET_CPUID */ |
| 239 | struct kvm_cpuid { |
| 240 | __u32 nent; |
| 241 | __u32 padding; |
| 242 | struct kvm_cpuid_entry entries[0]; |
| 243 | }; |
| 244 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 245 | /* for KVM_SET_SIGNAL_MASK */ |
| 246 | struct kvm_signal_mask { |
| 247 | __u32 len; |
| 248 | __u8 sigset[0]; |
| 249 | }; |
| 250 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 251 | #define KVMIO 0xAE |
| 252 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 253 | /* |
| 254 | * ioctls for /dev/kvm fds: |
| 255 | */ |
Avi Kivity | 739872c | 2007-03-01 17:20:13 +0200 | [diff] [blame] | 256 | #define KVM_GET_API_VERSION _IO(KVMIO, 0x00) |
| 257 | #define KVM_CREATE_VM _IO(KVMIO, 0x01) /* returns a VM fd */ |
| 258 | #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] | 259 | /* |
| 260 | * Check if a kvm extension is available. Argument is extension number, |
| 261 | * return is 1 (yes) or 0 (no, sorry). |
| 262 | */ |
| 263 | #define KVM_CHECK_EXTENSION _IO(KVMIO, 0x03) |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 264 | /* |
| 265 | * Get size for mmap(vcpu_fd) |
| 266 | */ |
| 267 | #define KVM_GET_VCPU_MMAP_SIZE _IO(KVMIO, 0x04) /* in bytes */ |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 268 | |
| 269 | /* |
| 270 | * ioctls for VM fds |
| 271 | */ |
Avi Kivity | 739872c | 2007-03-01 17:20:13 +0200 | [diff] [blame] | 272 | #define KVM_SET_MEMORY_REGION _IOW(KVMIO, 0x40, struct kvm_memory_region) |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 273 | /* |
| 274 | * KVM_CREATE_VCPU receives as a parameter the vcpu slot, and returns |
| 275 | * a vcpu fd. |
| 276 | */ |
Avi Kivity | 739872c | 2007-03-01 17:20:13 +0200 | [diff] [blame] | 277 | #define KVM_CREATE_VCPU _IO(KVMIO, 0x41) |
| 278 | #define KVM_GET_DIRTY_LOG _IOW(KVMIO, 0x42, struct kvm_dirty_log) |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 279 | #define KVM_SET_MEMORY_ALIAS _IOW(KVMIO, 0x43, struct kvm_memory_alias) |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * ioctls for vcpu fds |
| 283 | */ |
Avi Kivity | 739872c | 2007-03-01 17:20:13 +0200 | [diff] [blame] | 284 | #define KVM_RUN _IO(KVMIO, 0x80) |
| 285 | #define KVM_GET_REGS _IOR(KVMIO, 0x81, struct kvm_regs) |
| 286 | #define KVM_SET_REGS _IOW(KVMIO, 0x82, struct kvm_regs) |
| 287 | #define KVM_GET_SREGS _IOR(KVMIO, 0x83, struct kvm_sregs) |
| 288 | #define KVM_SET_SREGS _IOW(KVMIO, 0x84, struct kvm_sregs) |
| 289 | #define KVM_TRANSLATE _IOWR(KVMIO, 0x85, struct kvm_translation) |
| 290 | #define KVM_INTERRUPT _IOW(KVMIO, 0x86, struct kvm_interrupt) |
| 291 | #define KVM_DEBUG_GUEST _IOW(KVMIO, 0x87, struct kvm_debug_guest) |
| 292 | #define KVM_GET_MSRS _IOWR(KVMIO, 0x88, struct kvm_msrs) |
| 293 | #define KVM_SET_MSRS _IOW(KVMIO, 0x89, struct kvm_msrs) |
| 294 | #define KVM_SET_CPUID _IOW(KVMIO, 0x8a, struct kvm_cpuid) |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 295 | #define KVM_SET_SIGNAL_MASK _IOW(KVMIO, 0x8b, struct kvm_signal_mask) |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 296 | #define KVM_GET_FPU _IOR(KVMIO, 0x8c, struct kvm_fpu) |
| 297 | #define KVM_SET_FPU _IOW(KVMIO, 0x8d, struct kvm_fpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 298 | |
| 299 | #endif |