1 /*
2  * GPL HEADER START
3  *
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.
7  *
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.
12  *
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.
16  *
17  * GPL HEADER END
18  *
19  * Copyright 2011 various Linux Kernel contributors.
20  * Copyright 2011 Joyent, Inc. All Rights Reserved.
21  */
22 
23 #ifndef __KVM_H
24 #define	__KVM_H
25 
26 /*
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.
33  */
34 #ifndef __GNUC__
35 #error "The KVM Header files require GNU C extensions for compatibility."
36 #endif
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/ioccom.h>
41 #include "kvm_x86.h"
42 
43 #define	KVM_API_VERSION 12   /* same as linux (for qemu compatability...) */
44 
45 /* for KVM_CREATE_MEMORY_REGION */
46 typedef struct kvm_memory_region {
47 	uint32_t slot;
48 	uint32_t flags;
49 	uint64_t guest_phys_addr;
50 	uint64_t memory_size; /* bytes */
51 } kvm_memory_region_t;
52 
53 /* for KVM_SET_USER_MEMORY_REGION */
54 typedef struct kvm_userspace_memory_region {
55 	uint32_t slot;
56 	uint32_t flags;
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;
61 
62 /* for kvm_memory_region::flags */
63 #define	KVM_MEM_LOG_DIRTY_PAGES		1UL
64 #define	KVM_MEMSLOT_INVALID		(1UL << 1)
65 
66 /* for KVM_IRQ_LINE */
67 typedef struct kvm_irq_level {
68 	/*
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..
72 	 */
73 	union {
74 		uint32_t irq;
75 		int32_t status;
76 	};
77 	uint32_t level;
78 } kvm_irq_level_t;
79 
80 typedef struct kvm_irqchip {
81 	uint32_t chip_id;
82 	uint32_t pad;
83 	union {
84 		char dummy[512];  /* reserving space */
85 		struct kvm_pic_state pic;
86 		struct kvm_ioapic_state ioapic;
87 	} chip;
88 } kvm_irqchip_t;
89 
90 /* for KVM_CREATE_PIT2 */
91 typedef struct kvm_pit_config {
92 	uint32_t flags;
93 	uint32_t pad[15];
94 } kvm_pit_config_t;
95 
96 #define	KVM_PIT_SPEAKER_DUMMY		1
97 
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
116 
117 /* For KVM_EXIT_INTERNAL_ERROR */
118 #define	KVM_INTERNAL_ERROR_EMULATION	1
119 #define	KVM_INTERNAL_ERROR_SIMUL_EX	2
120 
121 /* for KVM_RUN, returned by mmap(vcpu_fd, offset=0) */
122 typedef struct kvm_run {
123 	/* in */
124 	unsigned char request_interrupt_window;
125 	unsigned char padding1[7];
126 
127 	/* out */
128 	uint32_t exit_reason;
129 	unsigned char ready_for_interrupt_injection;
130 	unsigned char if_flag;
131 	unsigned char padding2[2];
132 
133 	/* in (pre_kvm_run), out (post_kvm_run) */
134 	uint64_t cr8;
135 	uint64_t apic_base;
136 
137 	union {
138 		/* KVM_EXIT_UNKNOWN */
139 		struct {
140 			uint64_t hardware_exit_reason;
141 		} hw;
142 		/* KVM_EXIT_FAIL_ENTRY */
143 		struct {
144 			uint64_t hardware_entry_failure_reason;
145 		} fail_entry;
146 		/* KVM_EXIT_EXCEPTION */
147 		struct {
148 			uint32_t exception;
149 			uint32_t error_code;
150 		} ex;
151 		/* KVM_EXIT_IO */
152 		struct {
153 #define	KVM_EXIT_IO_IN  0
154 #define	KVM_EXIT_IO_OUT 1
155 			unsigned char direction;
156 			unsigned char size; /* bytes */
157 			unsigned short port;
158 			uint32_t count;
159 			uint64_t data_offset; /* relative to kvm_run start */
160 		} io;
161 		struct {
162 			struct kvm_debug_exit_arch arch;
163 		} debug;
164 		/* KVM_EXIT_MMIO */
165 		struct {
166 			uint64_t phys_addr;
167 			unsigned char  data[8];
168 			uint32_t len;
169 			unsigned char  is_write;
170 		} mmio;
171 		/* KVM_EXIT_HYPERCALL */
172 		struct {
173 			uint64_t nr;
174 			uint64_t args[6];
175 			uint64_t ret;
176 			uint32_t longmode;
177 			uint32_t pad;
178 		} hypercall;
179 		/* KVM_EXIT_TPR_ACCESS */
180 		struct {
181 			uint64_t rip;
182 			uint32_t is_write;
183 			uint32_t pad;
184 		} tpr_access;
185 		/* KVM_EXIT_DCR */
186 		struct {
187 			uint32_t dcrn;
188 			uint32_t data;
189 			unsigned char  is_write;
190 		} dcr;
191 		struct {
192 			uint32_t suberror;
193 			/* Available with KVM_CAP_INTERNAL_ERROR_DATA: */
194 			uint32_t ndata;
195 			uint64_t data[16];
196 		} internal;
197 		/* Fix the size of the union. */
198 		char padding[256];
199 	};
200 } kvm_run_t;
201 
202 typedef struct kvm_coalesced_mmio_zone {
203 	uint64_t addr;
204 	uint32_t size;
205 	uint32_t pad;
206 } kvm_coalesced_mmio_zone_t;
207 
208 typedef struct kvm_coalesced_mmio {
209 	uint64_t phys_addr;
210 	uint32_t len;
211 	uint32_t pad;
212 	unsigned char  data[8];
213 } kvm_coalesced_mmio_t;
214 
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;
219 
220 #define	KVM_COALESCED_MMIO_MAX \
221 	((PAGESIZE - sizeof (struct kvm_coalesced_mmio_ring)) / \
222 	sizeof (struct kvm_coalesced_mmio))
223 
224 /* for KVM_INTERRUPT */
225 typedef struct kvm_interrupt {
226 	/* in */
227 	uint32_t irq;
228 } kvm_interrupt_t;
229 
230 /* for KVM_GET_DIRTY_LOG */
231 typedef struct kvm_dirty_log {
232 	uint32_t slot;
233 	uint32_t padding1;
234 	union {
235 		void  *dirty_bitmap; /* one bit per page */
236 		uint64_t padding2;
237 	};
238 } kvm_dirty_log_t;
239 
240 /* for KVM_SET_SIGNAL_MASK */
241 typedef struct kvm_signal_mask {
242 	uint32_t len;
243 	uint8_t sigset[1];
244 } kvm_signal_mask_t;
245 
246 /* for KVM_TPR_ACCESS_REPORTING */
247 typedef struct kvm_tpr_access_ctl {
248 	uint32_t enabled;
249 	uint32_t flags;
250 	uint32_t reserved[8];
251 } kvm_tpr_access_ctl_t;
252 
253 /* for KVM_SET_VAPIC_ADDR */
254 typedef struct kvm_vapic_addr {
255 	uint64_t vapic_addr;
256 } kvm_vapic_addr_t;
257 
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
264 
265 typedef struct kvm_mp_state {
266 	uint32_t mp_state;
267 } kvm_mp_state_t;
268 
269 /* for KVM_SET_GUEST_DEBUG */
270 
271 #define	KVM_GUESTDBG_ENABLE		0x00000001
272 #define	KVM_GUESTDBG_SINGLESTEP		0x00000002
273 
274 typedef struct kvm_guest_debug {
275 	uint32_t control;
276 	uint32_t pad;
277 	struct kvm_guest_debug_arch arch;
278 } kvm_guest_debug_t;
279 
280 /* ioctl commands */
281 
282 #define	KVMIO 0xAE
283 
284 /*
285  * ioctls for /dev/kvm fds:
286  */
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 
292 /*
293  * Check if a kvm extension is available.  Argument is extension number,
294  * return is 1 (yes) or 0 (no, sorry).
295  */
296 #define	KVM_CHECK_EXTENSION	_IO(KVMIO,   0x03)
297 
298 /*
299  * Get size for mmap(vcpu_fd)
300  */
301 #define	KVM_GET_VCPU_MMAP_SIZE	_IO(KVMIO,   0x04) /* in bytes */
302 #define	KVM_GET_SUPPORTED_CPUID	_IOWR(KVMIO, 0x05, struct kvm_cpuid2)
303 
304 /*
305  * Extension capability list.
306  */
307 #define	KVM_CAP_IRQCHIP				0
308 #define	KVM_CAP_HLT				1
309 #define	KVM_CAP_MMU_SHADOW_CACHE_CONTROL	2
310 #define	KVM_CAP_USER_MEMORY			3
311 #define	KVM_CAP_SET_TSS_ADDR			4
312 #define	KVM_CAP_VAPIC				6
313 #define	KVM_CAP_EXT_CPUID			7
314 #define	KVM_CAP_CLOCKSOURCE			8
315 #define	KVM_CAP_NR_VCPUS			9
316 #define	KVM_CAP_NR_MEMSLOTS			10
317 #define	KVM_CAP_PIT				11
318 #define	KVM_CAP_NOP_IO_DELAY			12
319 #define	KVM_CAP_PV_MMU				13
320 #define	KVM_CAP_MP_STATE			14
321 #define	KVM_CAP_COALESCED_MMIO			15
322 #define	KVM_CAP_SYNC_MMU			16
323 
324 #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
325 #define	KVM_CAP_DEVICE_ASSIGNMENT		17
326 #endif
327 
328 #define	KVM_CAP_IOMMU				18
329 
330 #ifdef __KVM_HAVE_MSI
331 #define	KVM_CAP_DEVICE_MSI			20
332 #endif
333 
334 /* Bug in KVM_SET_USER_MEMORY_REGION fixed: */
335 #define	KVM_CAP_DESTROY_MEMORY_REGION_WORKS	21
336 
337 #define	KVM_CAP_USER_NMI			22
338 
339 #ifdef __KVM_HAVE_GUEST_DEBUG
340 #define	KVM_CAP_SET_GUEST_DEBUG			23
341 #endif
342 #define	KVM_CAP_REINJECT_CONTROL		24
343 #define	KVM_CAP_IRQ_ROUTING			25
344 #define	KVM_CAP_IRQ_INJECT_STATUS		26
345 #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
346 #define	KVM_CAP_DEVICE_DEASSIGNMENT		27
347 #endif
348 #ifdef __KVM_HAVE_MSIX
349 #define	KVM_CAP_DEVICE_MSIX			28
350 #endif
351 #define	KVM_CAP_ASSIGN_DEV_IRQ			29
352 /* Another bug in KVM_SET_USER_MEMORY_REGION fixed: */
353 #define	KVM_CAP_JOIN_MEMORY_REGIONS_WORKS	30
354 #define	KVM_CAP_MCE				31
355 #define	KVM_CAP_PIT2				33
356 #define	KVM_CAP_SET_BOOT_CPU_ID			34
357 #define	KVM_CAP_PIT_STATE2			35
358 #define	KVM_CAP_IOEVENTFD			36
359 #define	KVM_CAP_SET_IDENTITY_MAP_ADDR		37
360 #define	KVM_CAP_XEN_HVM				38
361 #define	KVM_CAP_ADJUST_CLOCK			39
362 #define	KVM_CAP_INTERNAL_ERROR_DATA		40
363 #define	KVM_CAP_VCPU_EVENTS			41
364 #define	KVM_CAP_S390_PSW			42
365 #define	KVM_CAP_PPC_SEGSTATE			43
366 #define	KVM_CAP_HYPERV				44
367 #define	KVM_CAP_HYPERV_VAPIC			45
368 #define	KVM_CAP_HYPERV_SPIN			46
369 #define	KVM_CAP_PCI_SEGMENT			47
370 #define	KVM_CAP_X86_ROBUST_SINGLESTEP		51
371 
372 #ifdef KVM_CAP_IRQ_ROUTING
373 typedef struct kvm_irq_routing_irqchip {
374 	uint32_t irqchip;
375 	uint32_t pin;
376 } kvm_irq_routing_irqchip_t;
377 
378 typedef struct kvm_irq_routing_msi {
379 	uint32_t address_lo;
380 	uint32_t address_hi;
381 	uint32_t data;
382 	uint32_t pad;
383 } kvm_irq_routing_msi_t;
384 
385 /* gsi routing entry types */
386 #define	KVM_IRQ_ROUTING_IRQCHIP 1
387 #define	KVM_IRQ_ROUTING_MSI 2
388 
389 typedef struct kvm_irq_routing_entry {
390 	uint32_t gsi;
391 	uint32_t type;
392 	uint32_t flags;
393 	uint32_t pad;
394 	union {
395 		struct kvm_irq_routing_irqchip irqchip;
396 		struct kvm_irq_routing_msi msi;
397 		uint32_t pad[8];
398 	} u;
399 } kvm_irq_routing_entry_t;
400 
401 typedef struct kvm_irq_routing {
402 	uint32_t nr;
403 	uint32_t flags;
404 	struct kvm_irq_routing_entry entries[1];
405 } kvm_irq_routing_t;
406 
407 #endif /* KVM_CAP_IRQ_ROUTING */
408 
409 #ifdef KVM_CAP_MCE
410 /* x86 MCE */
411 typedef struct kvm_x86_mce {
412 	uint64_t status;
413 	uint64_t addr;
414 	uint64_t misc;
415 	uint64_t mcg_status;
416 	uint8_t bank;
417 	uint8_t pad1[7];
418 	uint64_t pad2[3];
419 } kvm_x86_mce_t;
420 #endif /* KVM_CAP_MCE */
421 
422 typedef struct kvm_clock_data {
423 	uint64_t clock;
424 	uint32_t flags;
425 	uint32_t pad[9];
426 } kvm_clock_data_t;
427 
428 /*
429  * ioctls for VM fds
430  */
431 
432 /*
433  * KVM_CREATE_VCPU receives as a parameter the vcpu slot, and returns
434  * a vcpu fd.
435  */
436 #define	KVM_CREATE_VCPU		_IO(KVMIO,   0x41)
437 #define	KVM_GET_DIRTY_LOG	_IOW(KVMIO,  0x42, struct kvm_dirty_log)
438 #define	KVM_SET_NR_MMU_PAGES	_IO(KVMIO,   0x44)
439 #define	KVM_GET_NR_MMU_PAGES	_IO(KVMIO,   0x45)
440 #define	KVM_SET_USER_MEMORY_REGION _IOW(KVMIO, 0x46, \
441 					    struct kvm_userspace_memory_region)
442 
443 #define	KVM_SET_TSS_ADDR	_IO(KVMIO,   0x47)
444 #define	KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO,  0x48, uint64_t)
445 
446 /* Device model IOC */
447 #define	KVM_CREATE_IRQCHIP	_IO(KVMIO,   0x60)
448 #define	KVM_IRQ_LINE		_IOW(KVMIO,  0x61, struct kvm_irq_level)
449 #define	KVM_GET_IRQCHIP		_IOWR(KVMIO, 0x62, struct kvm_irqchip)
450 #define	KVM_SET_IRQCHIP		_IOR(KVMIO,  0x63, struct kvm_irqchip)
451 #define	KVM_CREATE_PIT		_IO(KVMIO,   0x64)
452 #define	KVM_GET_PIT		_IOWR(KVMIO, 0x65, struct kvm_pit_state)
453 #define	KVM_SET_PIT		_IOR(KVMIO,  0x66, struct kvm_pit_state)
454 #define	KVM_IRQ_LINE_STATUS	_IOWR(KVMIO, 0x67, struct kvm_irq_level)
455 
456 #define	KVM_REGISTER_COALESCED_MMIO _IOW(KVMIO,  0x67, \
457 					    struct kvm_coalesced_mmio_zone)
458 #define	KVM_UNREGISTER_COALESCED_MMIO _IOW(KVMIO,  0x68, \
459 					    struct kvm_coalesced_mmio_zone)
460 #define	KVM_SET_GSI_ROUTING	_IOW(KVMIO,  0x6a, struct kvm_irq_routing)
461 #define	KVM_REINJECT_CONTROL	_IO(KVMIO,   0x71)
462 #define	KVM_CREATE_PIT2		_IOW(KVMIO,  0x77, struct kvm_pit_config)
463 #define	KVM_SET_BOOT_CPU_ID	_IO(KVMIO,   0x78)
464 #define	KVM_SET_CLOCK		_IOW(KVMIO,  0x7b, struct kvm_clock_data)
465 #define	KVM_GET_CLOCK		_IOR(KVMIO,  0x7c, struct kvm_clock_data)
466 /* Available with KVM_CAP_PIT_STATE2 */
467 #define	KVM_GET_PIT2		_IOR(KVMIO,  0x9f, struct kvm_pit_state2)
468 #define	KVM_SET_PIT2		_IOW(KVMIO,  0xa0, struct kvm_pit_state2)
469 
470 /*
471  * ioctls for vcpu fds
472  */
473 #define	KVM_RUN			_IO(KVMIO,   0x80)
474 #define	KVM_GET_REGS		_IOR(KVMIO,  0x81, struct kvm_regs)
475 #define	KVM_SET_REGS		_IOW(KVMIO,  0x82, struct kvm_regs)
476 #define	KVM_GET_SREGS		_IOR(KVMIO,  0x83, struct kvm_sregs)
477 #define	KVM_SET_SREGS		_IOW(KVMIO,  0x84, struct kvm_sregs)
478 #define	KVM_INTERRUPT		_IOW(KVMIO,  0x86, struct kvm_interrupt)
479 #define	KVM_GET_MSRS		_IOWR(KVMIO, 0x88, struct kvm_msrs)
480 #define	KVM_SET_MSRS		_IOW(KVMIO,  0x89, struct kvm_msrs)
481 #define	KVM_SET_CPUID		_IOW(KVMIO,  0x8a, struct kvm_cpuid)
482 #define	KVM_SET_SIGNAL_MASK	_IOW(KVMIO,  0x8b, struct kvm_signal_mask)
483 #define	KVM_GET_FPU		_IOR(KVMIO,  0x8c, struct kvm_fpu)
484 #define	KVM_SET_FPU		_IOW(KVMIO,  0x8d, struct kvm_fpu)
485 #define	KVM_GET_LAPIC		_IOR(KVMIO,  0x8e, struct kvm_lapic_state)
486 #define	KVM_SET_LAPIC		_IOW(KVMIO,  0x8f, struct kvm_lapic_state)
487 #define	KVM_SET_CPUID2		_IOW(KVMIO,  0x90, struct kvm_cpuid2)
488 #define	KVM_GET_CPUID2		_IOWR(KVMIO, 0x91, struct kvm_cpuid2)
489 /* Available with KVM_CAP_VAPIC */
490 #define	KVM_TPR_ACCESS_REPORTING _IOWR(KVMIO, 0x92, struct kvm_tpr_access_ctl)
491 /* Available with KVM_CAP_VAPIC */
492 #define	KVM_SET_VAPIC_ADDR	_IOW(KVMIO,  0x93, struct kvm_vapic_addr)
493 #define	KVM_GET_MP_STATE	_IOR(KVMIO,  0x98, struct kvm_mp_state)
494 #define	KVM_SET_MP_STATE	_IOW(KVMIO,  0x99, struct kvm_mp_state)
495 /* Available with KVM_CAP_NMI */
496 #define	KVM_NMI			_IO(KVMIO,   0x9a)
497 /* MCE for x86 */
498 #define	KVM_X86_SETUP_MCE	_IOW(KVMIO,  0x9c, uint64_t)
499 #define	KVM_X86_GET_MCE_CAP_SUPPORTED _IOR(KVMIO,  0x9d, uint64_t)
500 #define	KVM_X86_SET_MCE		_IOW(KVMIO,  0x9e, struct kvm_x86_mce)
501 /* Available with KVM_CAP_VCPU_EVENTS */
502 #define	KVM_GET_VCPU_EVENTS	_IOR(KVMIO,  0x9f, struct kvm_vcpu_events)
503 #define	KVM_SET_VCPU_EVENTS	_IOW(KVMIO,  0xa0, struct kvm_vcpu_events)
504 
505 #endif /* __KVM_H */
506