Merge branch 'stable-0.11' of git://git.sv.gnu.org/qemu into stable-0.11
[qemu-kvm/fedora.git] / qemu-kvm.h
blobeb48ff8f73545ba4d08ebddc62f5437f598d4fd6
1 /*
2 * qemu/kvm integration
4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
7 */
8 #ifndef THE_ORIGINAL_AND_TRUE_QEMU_KVM_H
9 #define THE_ORIGINAL_AND_TRUE_QEMU_KVM_H
11 #include "cpu.h"
13 #include <signal.h>
15 #ifdef CONFIG_KVM
17 #if defined(__s390__)
18 #include <asm/ptrace.h>
19 #endif
21 #include <stdint.h>
23 #ifndef __user
24 #define __user /* temporary, until installed via make headers_install */
25 #endif
27 #include <linux/kvm.h>
29 #include <signal.h>
31 /* FIXME: share this number with kvm */
32 /* FIXME: or dynamically alloc/realloc regions */
33 #ifdef __s390__
34 #define KVM_MAX_NUM_MEM_REGIONS 1u
35 #define MAX_VCPUS 64
36 #define LIBKVM_S390_ORIGIN (0UL)
37 #elif defined(__ia64__)
38 #define KVM_MAX_NUM_MEM_REGIONS 32u
39 #define MAX_VCPUS 256
40 #else
41 #define KVM_MAX_NUM_MEM_REGIONS 32u
42 #define MAX_VCPUS 16
43 #endif
45 /* kvm abi verison variable */
46 extern int kvm_abi;
48 /**
49 * \brief The KVM context
51 * The verbose KVM context
54 struct kvm_context {
55 void *opaque;
56 /// is dirty pages logging enabled for all regions or not
57 int dirty_pages_log_all;
58 /// do not create in-kernel irqchip if set
59 int no_irqchip_creation;
60 /// in-kernel irqchip status
61 int irqchip_in_kernel;
62 /// ioctl to use to inject interrupts
63 int irqchip_inject_ioctl;
64 /// do not create in-kernel pit if set
65 int no_pit_creation;
66 /// in-kernel pit status
67 int pit_in_kernel;
68 /// in-kernel coalesced mmio
69 int coalesced_mmio;
70 #ifdef KVM_CAP_IRQ_ROUTING
71 struct kvm_irq_routing *irq_routes;
72 int nr_allocated_irq_routes;
73 #endif
74 void *used_gsi_bitmap;
75 int max_gsi;
78 struct kvm_vcpu_context
80 int fd;
81 struct kvm_run *run;
82 struct kvm_context *kvm;
83 uint32_t id;
86 typedef struct kvm_context *kvm_context_t;
87 typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
89 #include "kvm.h"
90 int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
91 void **vm_mem);
92 int kvm_alloc_userspace_memory(kvm_context_t kvm, unsigned long memory,
93 void **vm_mem);
95 int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
96 void **vm_mem);
97 int kvm_arch_run(kvm_vcpu_context_t vcpu);
100 void kvm_show_code(kvm_vcpu_context_t vcpu);
102 int handle_halt(kvm_vcpu_context_t vcpu);
103 int handle_shutdown(kvm_context_t kvm, CPUState *env);
104 void post_kvm_run(kvm_context_t kvm, CPUState *env);
105 int pre_kvm_run(kvm_context_t kvm, CPUState *env);
106 int handle_io_window(kvm_context_t kvm);
107 int handle_debug(kvm_vcpu_context_t vcpu, void *env);
108 int try_push_interrupts(kvm_context_t kvm);
110 #if defined(__x86_64__) || defined(__i386__)
111 struct kvm_msr_list *kvm_get_msr_list(kvm_context_t);
112 int kvm_get_msrs(kvm_vcpu_context_t, struct kvm_msr_entry *msrs, int n);
113 int kvm_set_msrs(kvm_vcpu_context_t, struct kvm_msr_entry *msrs, int n);
114 int kvm_get_mce_cap_supported(kvm_context_t, uint64_t *mce_cap, int *max_banks);
115 int kvm_setup_mce(kvm_vcpu_context_t vcpu, uint64_t *mcg_cap);
116 struct kvm_x86_mce;
117 int kvm_set_mce(kvm_vcpu_context_t vcpu, struct kvm_x86_mce *mce);
118 #endif
121 * \brief Create new KVM context
123 * This creates a new kvm_context. A KVM context is a small area of data that
124 * holds information about the KVM instance that gets created by this call.\n
125 * This should always be your first call to KVM.
127 * \param opaque Not used
128 * \return NULL on failure
130 int kvm_init(int smp_cpus);
133 * \brief Disable the in-kernel IRQCHIP creation
135 * In-kernel irqchip is enabled by default. If userspace irqchip is to be used,
136 * this should be called prior to kvm_create().
138 * \param kvm Pointer to the kvm_context
140 void kvm_disable_irqchip_creation(kvm_context_t kvm);
143 * \brief Disable the in-kernel PIT creation
145 * In-kernel pit is enabled by default. If userspace pit is to be used,
146 * this should be called prior to kvm_create().
148 * \param kvm Pointer to the kvm_context
150 void kvm_disable_pit_creation(kvm_context_t kvm);
153 * \brief Create new virtual machine
155 * This creates a new virtual machine, maps physical RAM to it, and creates a
156 * virtual CPU for it.\n
157 * \n
158 * Memory gets mapped for addresses 0->0xA0000, 0xC0000->phys_mem_bytes
160 * \param kvm Pointer to the current kvm_context
161 * \param phys_mem_bytes The amount of physical ram you want the VM to have
162 * \param phys_mem This pointer will be set to point to the memory that
163 * kvm_create allocates for physical RAM
164 * \return 0 on success
166 int kvm_create(kvm_context_t kvm,
167 unsigned long phys_mem_bytes,
168 void **phys_mem);
169 int kvm_create_vm(kvm_context_t kvm);
170 void kvm_create_irqchip(kvm_context_t kvm);
173 * \brief Create a new virtual cpu
175 * This creates a new virtual cpu (the first vcpu is created by kvm_create()).
176 * Should be called from a thread dedicated to the vcpu.
178 * \param kvm kvm context
179 * \param slot vcpu number (> 0)
180 * \return 0 on success, -errno on failure
182 kvm_vcpu_context_t kvm_create_vcpu(CPUState *env, int id);
185 * \brief Start the VCPU
187 * This starts the VCPU and virtualization is started.\n
188 * \n
189 * This function will not return until any of these conditions are met:
190 * - An IO/MMIO handler does not return "0"
191 * - An exception that neither the guest OS, nor KVM can handle occurs
193 * \note This function will call the callbacks registered in kvm_init()
194 * to emulate those functions
195 * \note If you at any point want to interrupt the VCPU, kvm_run() will
196 * listen to the EINTR signal. This allows you to simulate external interrupts
197 * and asyncronous IO.
199 * \param kvm Pointer to the current kvm_context
200 * \param vcpu Which virtual CPU should be started
201 * \return 0 on success, but you really shouldn't expect this function to
202 * return except for when an error has occured, or when you have sent it
203 * an EINTR signal.
205 int kvm_run(kvm_vcpu_context_t vcpu, void *env);
208 * \brief Get interrupt flag from on last exit to userspace
210 * This gets the CPU interrupt flag as it was on the last exit to userspace.
212 * \param kvm Pointer to the current kvm_context
213 * \param vcpu Which virtual CPU should get dumped
214 * \return interrupt flag value (0 or 1)
216 int kvm_get_interrupt_flag(kvm_vcpu_context_t vcpu);
219 * \brief Get the value of the APIC_BASE msr as of last exit to userspace
221 * This gets the APIC_BASE msr as it was on the last exit to userspace.
223 * \param kvm Pointer to the current kvm_context
224 * \param vcpu Which virtual CPU should get dumped
225 * \return APIC_BASE msr contents
227 uint64_t kvm_get_apic_base(kvm_vcpu_context_t vcpu);
230 * \brief Check if a vcpu is ready for interrupt injection
232 * This checks if vcpu interrupts are not masked by mov ss or sti.
234 * \param kvm Pointer to the current kvm_context
235 * \param vcpu Which virtual CPU should get dumped
236 * \return boolean indicating interrupt injection readiness
238 int kvm_is_ready_for_interrupt_injection(kvm_vcpu_context_t vcpu);
241 * \brief Read VCPU registers
243 * This gets the GP registers from the VCPU and outputs them
244 * into a kvm_regs structure
246 * \note This function returns a \b copy of the VCPUs registers.\n
247 * If you wish to modify the VCPUs GP registers, you should call kvm_set_regs()
249 * \param kvm Pointer to the current kvm_context
250 * \param vcpu Which virtual CPU should get dumped
251 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
252 * registers values
253 * \return 0 on success
255 int kvm_get_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
258 * \brief Write VCPU registers
260 * This sets the GP registers on the VCPU from a kvm_regs structure
262 * \note When this function returns, the regs pointer and the data it points to
263 * can be discarded
264 * \param kvm Pointer to the current kvm_context
265 * \param vcpu Which virtual CPU should get dumped
266 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
267 * registers values
268 * \return 0 on success
270 int kvm_set_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
272 * \brief Read VCPU fpu registers
274 * This gets the FPU registers from the VCPU and outputs them
275 * into a kvm_fpu structure
277 * \note This function returns a \b copy of the VCPUs registers.\n
278 * If you wish to modify the VCPU FPU registers, you should call kvm_set_fpu()
280 * \param kvm Pointer to the current kvm_context
281 * \param vcpu Which virtual CPU should get dumped
282 * \param fpu Pointer to a kvm_fpu which will be populated with the VCPUs
283 * fpu registers values
284 * \return 0 on success
286 int kvm_get_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
289 * \brief Write VCPU fpu registers
291 * This sets the FPU registers on the VCPU from a kvm_fpu structure
293 * \note When this function returns, the fpu pointer and the data it points to
294 * can be discarded
295 * \param kvm Pointer to the current kvm_context
296 * \param vcpu Which virtual CPU should get dumped
297 * \param fpu Pointer to a kvm_fpu which holds the new vcpu fpu state
298 * \return 0 on success
300 int kvm_set_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
303 * \brief Read VCPU system registers
305 * This gets the non-GP registers from the VCPU and outputs them
306 * into a kvm_sregs structure
308 * \note This function returns a \b copy of the VCPUs registers.\n
309 * If you wish to modify the VCPUs non-GP registers, you should call
310 * kvm_set_sregs()
312 * \param kvm Pointer to the current kvm_context
313 * \param vcpu Which virtual CPU should get dumped
314 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
315 * registers values
316 * \return 0 on success
318 int kvm_get_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
321 * \brief Write VCPU system registers
323 * This sets the non-GP registers on the VCPU from a kvm_sregs structure
325 * \note When this function returns, the regs pointer and the data it points to
326 * can be discarded
327 * \param kvm Pointer to the current kvm_context
328 * \param vcpu Which virtual CPU should get dumped
329 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
330 * registers values
331 * \return 0 on success
333 int kvm_set_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
335 #ifdef KVM_CAP_MP_STATE
337 * * \brief Read VCPU MP state
340 int kvm_get_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
343 * * \brief Write VCPU MP state
346 int kvm_set_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
348 * * \brief Reset VCPU MP state
351 static inline int kvm_reset_mpstate(kvm_vcpu_context_t vcpu)
353 struct kvm_mp_state mp_state = {.mp_state = KVM_MP_STATE_UNINITIALIZED};
354 return kvm_set_mpstate(vcpu, &mp_state);
356 #endif
359 * \brief Simulate an external vectored interrupt
361 * This allows you to simulate an external vectored interrupt.
363 * \param kvm Pointer to the current kvm_context
364 * \param vcpu Which virtual CPU should get dumped
365 * \param irq Vector number
366 * \return 0 on success
368 int kvm_inject_irq(kvm_vcpu_context_t vcpu, unsigned irq);
370 #ifdef KVM_CAP_SET_GUEST_DEBUG
371 int kvm_set_guest_debug(kvm_vcpu_context_t, struct kvm_guest_debug *dbg);
372 #endif
374 #if defined(__i386__) || defined(__x86_64__)
376 * \brief Setup a vcpu's cpuid instruction emulation
378 * Set up a table of cpuid function to cpuid outputs.\n
380 * \param kvm Pointer to the current kvm_context
381 * \param vcpu Which virtual CPU should be initialized
382 * \param nent number of entries to be installed
383 * \param entries cpuid function entries table
384 * \return 0 on success, or -errno on error
386 int kvm_setup_cpuid(kvm_vcpu_context_t vcpu, int nent,
387 struct kvm_cpuid_entry *entries);
390 * \brief Setup a vcpu's cpuid instruction emulation
392 * Set up a table of cpuid function to cpuid outputs.
393 * This call replaces the older kvm_setup_cpuid interface by adding a few
394 * parameters to support cpuid functions that have sub-leaf values.
396 * \param kvm Pointer to the current kvm_context
397 * \param vcpu Which virtual CPU should be initialized
398 * \param nent number of entries to be installed
399 * \param entries cpuid function entries table
400 * \return 0 on success, or -errno on error
402 int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu, int nent,
403 struct kvm_cpuid_entry2 *entries);
406 * \brief Setting the number of shadow pages to be allocated to the vm
408 * \param kvm pointer to kvm_context
409 * \param nrshadow_pages number of pages to be allocated
411 int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages);
414 * \brief Getting the number of shadow pages that are allocated to the vm
416 * \param kvm pointer to kvm_context
417 * \param nrshadow_pages number of pages to be allocated
419 int kvm_get_shadow_pages(kvm_context_t kvm , unsigned int *nrshadow_pages);
422 * \brief Set up cr8 for next time the vcpu is executed
424 * This is a fast setter for cr8, which will be applied when the
425 * vcpu next enters guest mode.
427 * \param kvm Pointer to the current kvm_context
428 * \param vcpu Which virtual CPU should get dumped
429 * \param cr8 next cr8 value
431 void kvm_set_cr8(kvm_vcpu_context_t vcpu, uint64_t cr8);
434 * \brief Get cr8 for sync tpr in qemu apic emulation
436 * This is a getter for cr8, which used to sync with the tpr in qemu
437 * apic emualtion.
439 * \param kvm Pointer to the current kvm_context
440 * \param vcpu Which virtual CPU should get dumped
442 __u64 kvm_get_cr8(kvm_vcpu_context_t vcpu);
443 #endif
446 * \brief Set a vcpu's signal mask for guest mode
448 * A vcpu can have different signals blocked in guest mode and user mode.
449 * This allows guest execution to be interrupted on a signal, without requiring
450 * that the signal be delivered to a signal handler (the signal can be
451 * dequeued using sigwait(2).
453 * \param kvm Pointer to the current kvm_context
454 * \param vcpu Which virtual CPU should be initialized
455 * \param sigset signal mask for guest mode
456 * \return 0 on success, or -errno on error
458 int kvm_set_signal_mask(kvm_vcpu_context_t vcpu, const sigset_t *sigset);
461 * \brief Dump VCPU registers
463 * This dumps some of the information that KVM has about a virtual CPU, namely:
464 * - GP Registers
466 * A much more verbose version of this is available as kvm_dump_vcpu()
468 * \param kvm Pointer to the current kvm_context
469 * \param vcpu Which virtual CPU should get dumped
470 * \return 0 on success
472 void kvm_show_regs(kvm_vcpu_context_t vcpu);
475 void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start,
476 unsigned long len, int log, int writable);
477 void kvm_destroy_phys_mem(kvm_context_t, unsigned long phys_start,
478 unsigned long len);
479 void kvm_unregister_memory_area(kvm_context_t, uint64_t phys_start,
480 unsigned long len);
482 int kvm_is_containing_region(kvm_context_t kvm, unsigned long phys_start, unsigned long size);
483 int kvm_register_phys_mem(kvm_context_t kvm,
484 unsigned long phys_start, void *userspace_addr,
485 unsigned long len, int log);
486 int kvm_get_dirty_pages(kvm_context_t, unsigned long phys_addr, void *buf);
487 int kvm_get_dirty_pages_range(kvm_context_t kvm, unsigned long phys_addr,
488 unsigned long end_addr, void*opaque,
489 int (*cb)(unsigned long start, unsigned long len,
490 void*bitmap, void *opaque));
491 int kvm_register_coalesced_mmio(kvm_context_t kvm,
492 uint64_t addr, uint32_t size);
493 int kvm_unregister_coalesced_mmio(kvm_context_t kvm,
494 uint64_t addr, uint32_t size);
497 * \brief Create a memory alias
499 * Aliases a portion of physical memory to another portion. If the guest
500 * accesses the alias region, it will behave exactly as if it accessed
501 * the target memory.
503 int kvm_create_memory_alias(kvm_context_t,
504 uint64_t phys_start, uint64_t len,
505 uint64_t target_phys);
508 * \brief Destroy a memory alias
510 * Removes an alias created with kvm_create_memory_alias().
512 int kvm_destroy_memory_alias(kvm_context_t, uint64_t phys_start);
515 * \brief Get a bitmap of guest ram pages which are allocated to the guest.
517 * \param kvm Pointer to the current kvm_context
518 * \param phys_addr Memory slot phys addr
519 * \param bitmap Long aligned address of a big enough bitmap (one bit per page)
521 int kvm_get_mem_map(kvm_context_t kvm, unsigned long phys_addr, void *bitmap);
522 int kvm_get_mem_map_range(kvm_context_t kvm, unsigned long phys_addr,
523 unsigned long len, void *buf, void *opaque,
524 int (*cb)(unsigned long start,unsigned long len,
525 void* bitmap, void* opaque));
526 int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status);
528 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm,
529 uint64_t phys_start,
530 uint64_t len);
531 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm,
532 uint64_t phys_start,
533 uint64_t len);
535 * \brief Enable dirty-pages-logging for all memory regions
537 * \param kvm Pointer to the current kvm_context
539 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm);
542 * \brief Disable dirty-page-logging for some memory regions
544 * Disable dirty-pages-logging for those memory regions that were
545 * created with dirty-page-logging disabled.
547 * \param kvm Pointer to the current kvm_context
549 int kvm_dirty_pages_log_reset(kvm_context_t kvm);
552 * \brief Query whether in kernel irqchip is used
554 * \param kvm Pointer to the current kvm_context
556 int kvm_irqchip_in_kernel(kvm_context_t kvm);
558 #ifdef KVM_CAP_IRQCHIP
560 * \brief Dump in kernel IRQCHIP contents
562 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
563 * and IOAPIC into a kvm_irqchip structure
565 * \param kvm Pointer to the current kvm_context
566 * \param chip The irq chip device to be dumped
568 int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
571 * \brief Set in kernel IRQCHIP contents
573 * Write one of the in kernel irq chip devices, including PIC (master/slave)
574 * and IOAPIC
577 * \param kvm Pointer to the current kvm_context
578 * \param chip THe irq chip device to be written
580 int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
582 #if defined(__i386__) || defined(__x86_64__)
584 * \brief Get in kernel local APIC for vcpu
586 * Save the local apic state including the timer of a virtual CPU
588 * \param kvm Pointer to the current kvm_context
589 * \param vcpu Which virtual CPU should be accessed
590 * \param s Local apic state of the specific virtual CPU
592 int kvm_get_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
595 * \brief Set in kernel local APIC for vcpu
597 * Restore the local apic state including the timer of a virtual CPU
599 * \param kvm Pointer to the current kvm_context
600 * \param vcpu Which virtual CPU should be accessed
601 * \param s Local apic state of the specific virtual CPU
603 int kvm_set_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
605 #endif
608 * \brief Simulate an NMI
610 * This allows you to simulate a non-maskable interrupt.
612 * \param kvm Pointer to the current kvm_context
613 * \param vcpu Which virtual CPU should get dumped
614 * \return 0 on success
616 int kvm_inject_nmi(kvm_vcpu_context_t vcpu);
618 #endif
621 * \brief Simulate an x86 MCE
623 * This allows you to simulate a x86 MCE.
625 * \param cenv Which virtual CPU should get MCE injected
626 * \param bank Bank number
627 * \param status MSR_MCI_STATUS
628 * \param mcg_status MSR_MCG_STATUS
629 * \param addr MSR_MCI_ADDR
630 * \param misc MSR_MCI_MISC
632 void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
633 uint64_t mcg_status, uint64_t addr, uint64_t misc);
636 * \brief Query wheather in kernel pit is used
638 * \param kvm Pointer to the current kvm_context
640 int kvm_pit_in_kernel(kvm_context_t kvm);
643 * \brief Initialize coalesced MMIO
645 * Check for coalesced MMIO capability and store in context
647 * \param kvm Pointer to the current kvm_context
649 int kvm_init_coalesced_mmio(kvm_context_t kvm);
651 #ifdef KVM_CAP_PIT
653 #if defined(__i386__) || defined(__x86_64__)
655 * \brief Get in kernel PIT of the virtual domain
657 * Save the PIT state.
659 * \param kvm Pointer to the current kvm_context
660 * \param s PIT state of the virtual domain
662 int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s);
665 * \brief Set in kernel PIT of the virtual domain
667 * Restore the PIT state.
668 * Timer would be retriggerred after restored.
670 * \param kvm Pointer to the current kvm_context
671 * \param s PIT state of the virtual domain
673 int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s);
675 int kvm_reinject_control(kvm_context_t kvm, int pit_reinject);
677 #ifdef KVM_CAP_PIT_STATE2
679 * \brief Check for kvm support of kvm_pit_state2
681 * \param kvm Pointer to the current kvm_context
682 * \return 0 on success
684 int kvm_has_pit_state2(kvm_context_t kvm);
687 * \brief Set in kernel PIT state2 of the virtual domain
690 * \param kvm Pointer to the current kvm_context
691 * \param ps2 PIT state2 of the virtual domain
692 * \return 0 on success
694 int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
697 * \brief Get in kernel PIT state2 of the virtual domain
700 * \param kvm Pointer to the current kvm_context
701 * \param ps2 PIT state2 of the virtual domain
702 * \return 0 on success
704 int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
706 #endif
707 #endif
708 #endif
710 #ifdef KVM_CAP_VAPIC
713 * \brief Enable kernel tpr access reporting
715 * When tpr access reporting is enabled, the kernel will call the
716 * ->tpr_access() callback every time the guest vcpu accesses the tpr.
718 * \param kvm Pointer to the current kvm_context
719 * \param vcpu vcpu to enable tpr access reporting on
721 int kvm_enable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
724 * \brief Disable kernel tpr access reporting
726 * Undoes the effect of kvm_enable_tpr_access_reporting().
728 * \param kvm Pointer to the current kvm_context
729 * \param vcpu vcpu to disable tpr access reporting on
731 int kvm_disable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
733 int kvm_enable_vapic(kvm_vcpu_context_t vcpu, uint64_t vapic);
735 #endif
737 #if defined(__s390__)
738 int kvm_s390_initial_reset(kvm_context_t kvm, int slot);
739 int kvm_s390_interrupt(kvm_context_t kvm, int slot,
740 struct kvm_s390_interrupt *kvmint);
741 int kvm_s390_set_initial_psw(kvm_context_t kvm, int slot, psw_t psw);
742 int kvm_s390_store_status(kvm_context_t kvm, int slot, unsigned long addr);
743 #endif
745 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
747 * \brief Notifies host kernel about a PCI device to be assigned to a guest
749 * Used for PCI device assignment, this function notifies the host
750 * kernel about the assigning of the physical PCI device to a guest.
752 * \param kvm Pointer to the current kvm_context
753 * \param assigned_dev Parameters, like bus, devfn number, etc
755 int kvm_assign_pci_device(kvm_context_t kvm,
756 struct kvm_assigned_pci_dev *assigned_dev);
759 * \brief Assign IRQ for an assigned device
761 * Used for PCI device assignment, this function assigns IRQ numbers for
762 * an physical device and guest IRQ handling.
764 * \param kvm Pointer to the current kvm_context
765 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
767 int kvm_assign_irq(kvm_context_t kvm,
768 struct kvm_assigned_irq *assigned_irq);
770 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
772 * \brief Deassign IRQ for an assigned device
774 * Used for PCI device assignment, this function deassigns IRQ numbers
775 * for an assigned device.
777 * \param kvm Pointer to the current kvm_context
778 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
780 int kvm_deassign_irq(kvm_context_t kvm,
781 struct kvm_assigned_irq *assigned_irq);
782 #endif
783 #endif
786 * \brief Determines whether destroying memory regions is allowed
788 * KVM before 2.6.29 had a bug when destroying memory regions.
790 * \param kvm Pointer to the current kvm_context
792 int kvm_destroy_memory_region_works(kvm_context_t kvm);
794 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
796 * \brief Notifies host kernel about a PCI device to be deassigned from a guest
798 * Used for hot remove PCI device, this function notifies the host
799 * kernel about the deassigning of the physical PCI device from a guest.
801 * \param kvm Pointer to the current kvm_context
802 * \param assigned_dev Parameters, like bus, devfn number, etc
804 int kvm_deassign_pci_device(kvm_context_t kvm,
805 struct kvm_assigned_pci_dev *assigned_dev);
806 #endif
809 * \brief Checks whether the generic irq routing capability is present
811 * Checks whether kvm can reroute interrupts among the various interrupt
812 * controllers.
814 * \param kvm Pointer to the current kvm_context
816 int kvm_has_gsi_routing(kvm_context_t kvm);
819 * \brief Determines the number of gsis that can be routed
821 * Returns the number of distinct gsis that can be routed by kvm. This is
822 * also the number of distinct routes (if a gsi has two routes, than another
823 * gsi cannot be used...)
825 * \param kvm Pointer to the current kvm_context
827 int kvm_get_gsi_count(kvm_context_t kvm);
830 * \brief Clears the temporary irq routing table
832 * Clears the temporary irq routing table. Nothing is committed to the
833 * running VM.
835 * \param kvm Pointer to the current kvm_context
837 int kvm_clear_gsi_routes(kvm_context_t kvm);
840 * \brief Adds an irq route to the temporary irq routing table
842 * Adds an irq route to the temporary irq routing table. Nothing is
843 * committed to the running VM.
845 * \param kvm Pointer to the current kvm_context
847 int kvm_add_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
850 * \brief Removes an irq route from the temporary irq routing table
852 * Adds an irq route to the temporary irq routing table. Nothing is
853 * committed to the running VM.
855 * \param kvm Pointer to the current kvm_context
857 int kvm_del_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
859 struct kvm_irq_routing_entry;
861 * \brief Adds a routing entry to the temporary irq routing table
863 * Adds a filled routing entry to the temporary irq routing table. Nothing is
864 * committed to the running VM.
866 * \param kvm Pointer to the current kvm_context
868 int kvm_add_routing_entry(kvm_context_t kvm,
869 struct kvm_irq_routing_entry* entry);
872 * \brief Removes a routing from the temporary irq routing table
874 * Remove a routing to the temporary irq routing table. Nothing is
875 * committed to the running VM.
877 * \param kvm Pointer to the current kvm_context
879 int kvm_del_routing_entry(kvm_context_t kvm,
880 struct kvm_irq_routing_entry* entry);
883 * \brief Updates a routing in the temporary irq routing table
885 * Update a routing in the temporary irq routing table
886 * with a new value. entry type and GSI can not be changed.
887 * Nothing is committed to the running VM.
889 * \param kvm Pointer to the current kvm_context
891 int kvm_update_routing_entry(kvm_context_t kvm,
892 struct kvm_irq_routing_entry* entry,
893 struct kvm_irq_routing_entry* newentry
897 * \brief Commit the temporary irq routing table
899 * Commit the temporary irq routing table to the running VM.
901 * \param kvm Pointer to the current kvm_context
903 int kvm_commit_irq_routes(kvm_context_t kvm);
906 * \brief Get unused GSI number for irq routing table
908 * Get unused GSI number for irq routing table
910 * \param kvm Pointer to the current kvm_context
912 int kvm_get_irq_route_gsi(kvm_context_t kvm);
915 * \brief Create a file descriptor for injecting interrupts
917 * Creates an eventfd based file-descriptor that maps to a specific GSI
918 * in the guest. eventfd compliant signaling (write() from userspace, or
919 * eventfd_signal() from kernelspace) will cause the GSI to inject
920 * itself into the guest at the next available window.
922 * \param kvm Pointer to the current kvm_context
923 * \param gsi GSI to assign to this fd
924 * \param flags reserved, must be zero
926 int kvm_irqfd(kvm_context_t kvm, int gsi, int flags);
928 #ifdef KVM_CAP_DEVICE_MSIX
929 int kvm_assign_set_msix_nr(kvm_context_t kvm,
930 struct kvm_assigned_msix_nr *msix_nr);
931 int kvm_assign_set_msix_entry(kvm_context_t kvm,
932 struct kvm_assigned_msix_entry *entry);
933 #endif
935 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg);
937 #else /* !CONFIG_KVM */
939 typedef struct kvm_context *kvm_context_t;
940 typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
942 struct kvm_pit_state { };
944 static inline int kvm_init(int smp_cpus) { return 0; }
945 static inline void kvm_inject_x86_mce(
946 CPUState *cenv, int bank,uint64_t status,
947 uint64_t mcg_status, uint64_t addr, uint64_t misc) { }
950 extern int kvm_allowed;
952 #endif /* !CONFIG_KVM */
955 int kvm_main_loop(void);
956 int kvm_qemu_init(void);
957 int kvm_init_ap(void);
958 int kvm_vcpu_inited(CPUState *env);
959 void kvm_load_registers(CPUState *env);
960 void kvm_save_registers(CPUState *env);
961 void kvm_load_mpstate(CPUState *env);
962 void kvm_save_mpstate(CPUState *env);
963 int kvm_cpu_exec(CPUState *env);
964 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
965 target_ulong len, int type);
966 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
967 target_ulong len, int type);
968 void kvm_remove_all_breakpoints(CPUState *current_env);
969 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
970 int kvm_qemu_init_env(CPUState *env);
971 int kvm_qemu_check_extension(int ext);
972 void kvm_apic_init(CPUState *env);
973 /* called from vcpu initialization */
974 void qemu_kvm_load_lapic(CPUState *env);
976 void kvm_hpet_enable_kpit(void);
977 void kvm_hpet_disable_kpit(void);
978 int kvm_set_irq(int irq, int level, int *status);
980 int kvm_physical_memory_set_dirty_tracking(int enable);
981 int kvm_update_dirty_pages_log(void);
982 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap);
984 void qemu_kvm_call_with_env(void (*func)(void *), void *data, CPUState *env);
985 void qemu_kvm_cpuid_on_env(CPUState *env);
986 void kvm_inject_interrupt(CPUState *env, int mask);
987 void kvm_update_after_sipi(CPUState *env);
988 void kvm_update_interrupt_request(CPUState *env);
989 void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
990 ram_addr_t phys_offset);
991 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
992 unsigned long size, int log, int writable);
994 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
995 unsigned long size);
996 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
997 int log);
998 int kvm_setup_guest_memory(void *area, unsigned long size);
999 int kvm_qemu_create_memory_alias(uint64_t phys_start,
1000 uint64_t len,
1001 uint64_t target_phys);
1002 int kvm_qemu_destroy_memory_alias(uint64_t phys_start);
1004 int kvm_arch_qemu_create_context(void);
1006 void kvm_arch_save_regs(CPUState *env);
1007 void kvm_arch_load_regs(CPUState *env);
1008 void kvm_arch_load_mpstate(CPUState *env);
1009 void kvm_arch_save_mpstate(CPUState *env);
1010 int kvm_arch_qemu_init_env(CPUState *cenv);
1011 void kvm_arch_pre_kvm_run(void *opaque, CPUState *env);
1012 void kvm_arch_post_kvm_run(void *opaque, CPUState *env);
1013 int kvm_arch_has_work(CPUState *env);
1014 void kvm_arch_process_irqchip_events(CPUState *env);
1015 int kvm_arch_try_push_interrupts(void *opaque);
1016 void kvm_arch_push_nmi(void *opaque);
1017 void kvm_arch_update_regs_for_sipi(CPUState *env);
1018 void kvm_arch_cpu_reset(CPUState *env);
1019 int kvm_set_boot_cpu_id(uint32_t id);
1021 struct kvm_guest_debug;
1022 struct kvm_debug_exit_arch;
1024 struct kvm_sw_breakpoint {
1025 target_ulong pc;
1026 target_ulong saved_insn;
1027 int use_count;
1028 TAILQ_ENTRY(kvm_sw_breakpoint) entry;
1031 TAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
1033 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
1034 int kvm_sw_breakpoints_active(CPUState *env);
1035 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env, target_ulong pc);
1036 int kvm_arch_insert_sw_breakpoint(CPUState *current_env,
1037 struct kvm_sw_breakpoint *bp);
1038 int kvm_arch_remove_sw_breakpoint(CPUState *current_env,
1039 struct kvm_sw_breakpoint *bp);
1040 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
1041 target_ulong len, int type);
1042 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
1043 target_ulong len, int type);
1044 void kvm_arch_remove_all_hw_breakpoints(void);
1045 void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
1047 void qemu_kvm_aio_wait_start(void);
1048 void qemu_kvm_aio_wait(void);
1049 void qemu_kvm_aio_wait_end(void);
1051 void qemu_kvm_notify_work(void);
1053 void kvm_tpr_opt_setup(void);
1054 void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
1055 void kvm_tpr_vcpu_start(CPUState *env);
1057 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf);
1058 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1059 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1061 int kvm_arch_init_irq_routing(void);
1063 int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len);
1064 int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len);
1066 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1067 struct ioperm_data;
1069 void kvm_ioperm(CPUState *env, void *data);
1070 void kvm_add_ioperm_data(struct ioperm_data *data);
1071 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num);
1072 void kvm_arch_do_ioperm(void *_data);
1073 #endif
1075 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
1076 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
1078 #ifdef CONFIG_KVM
1079 #include "sys-queue.h"
1081 extern int kvm_allowed;
1082 extern int kvm_irqchip;
1083 extern int kvm_pit;
1084 extern int kvm_pit_reinject;
1085 extern int kvm_nested;
1086 extern kvm_context_t kvm_context;
1088 struct ioperm_data {
1089 unsigned long start_port;
1090 unsigned long num;
1091 int turn_on;
1092 LIST_ENTRY(ioperm_data) entries;
1095 void qemu_kvm_cpu_stop(CPUState *env);
1096 int kvm_arch_halt(void *opaque, kvm_vcpu_context_t vcpu);
1097 int handle_tpr_access(void *opaque, kvm_vcpu_context_t vcpu,
1098 uint64_t rip, int is_write);
1099 int kvm_has_sync_mmu(void);
1101 #define kvm_enabled() (kvm_allowed)
1102 #define qemu_kvm_irqchip_in_kernel() kvm_irqchip_in_kernel(kvm_context)
1103 #define qemu_kvm_pit_in_kernel() kvm_pit_in_kernel(kvm_context)
1104 #define qemu_kvm_has_gsi_routing() kvm_has_gsi_routing(kvm_context)
1105 #ifdef TARGET_I386
1106 #define qemu_kvm_has_pit_state2() kvm_has_pit_state2(kvm_context)
1107 #endif
1108 void kvm_init_vcpu(CPUState *env);
1109 void kvm_load_tsc(CPUState *env);
1110 #else
1111 #define kvm_has_sync_mmu() (0)
1112 #define kvm_enabled() (0)
1113 #define kvm_nested 0
1114 #define qemu_kvm_irqchip_in_kernel() (0)
1115 #define qemu_kvm_pit_in_kernel() (0)
1116 #define qemu_kvm_has_gsi_routing() (0)
1117 #ifdef TARGET_I386
1118 #define qemu_kvm_has_pit_state2() (0)
1119 #endif
1120 #define kvm_load_registers(env) do {} while(0)
1121 #define kvm_save_registers(env) do {} while(0)
1122 #define qemu_kvm_cpu_stop(env) do {} while(0)
1123 static inline void kvm_init_vcpu(CPUState *env) { }
1124 static inline void kvm_load_tsc(CPUState *env) {}
1125 #endif
1127 void kvm_mutex_unlock(void);
1128 void kvm_mutex_lock(void);
1130 static inline void qemu_mutex_unlock_iothread(void)
1132 if (kvm_enabled())
1133 kvm_mutex_unlock();
1136 static inline void qemu_mutex_lock_iothread(void)
1138 if (kvm_enabled())
1139 kvm_mutex_lock();
1142 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr);
1144 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len);
1145 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len);
1148 static inline int kvm_sync_vcpus(void) { return 0; }
1150 static inline void kvm_arch_get_registers(CPUState *env)
1152 kvm_save_registers(env);
1153 kvm_save_mpstate(env);
1156 static inline void kvm_arch_put_registers(CPUState *env)
1158 kvm_load_registers(env);
1159 kvm_load_mpstate(env);
1162 static inline void cpu_synchronize_state(CPUState *env, int modified)
1164 if (kvm_enabled()) {
1165 if (modified)
1166 kvm_arch_put_registers(env);
1167 else
1168 kvm_arch_get_registers(env);
1172 uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
1173 int reg);
1176 static inline int kvm_set_migration_log(int enable)
1178 return kvm_physical_memory_set_dirty_tracking(enable);
1181 #ifdef CONFIG_KVM
1183 typedef struct KVMSlot
1185 target_phys_addr_t start_addr;
1186 ram_addr_t memory_size;
1187 ram_addr_t phys_offset;
1188 int slot;
1189 int flags;
1190 } KVMSlot;
1192 typedef struct kvm_dirty_log KVMDirtyLog;
1194 typedef struct KVMState
1196 KVMSlot slots[32];
1197 int fd;
1198 int vmfd;
1199 int coalesced_mmio;
1200 int broken_set_mem_region;
1201 int migration_log;
1202 #ifdef KVM_CAP_SET_GUEST_DEBUG
1203 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
1204 #endif
1205 struct kvm_context kvm_context;
1206 } KVMState;
1208 extern KVMState *kvm_state;
1210 int kvm_ioctl(KVMState *s, int type, ...);
1211 int kvm_vm_ioctl(KVMState *s, int type, ...);
1212 int kvm_check_extension(KVMState *s, unsigned int ext);
1214 #endif
1216 #endif