remove kvm types from handle unhandled
[qemu-kvm/fedora.git] / qemu-kvm.h
blobc90639e548130df67dcf5735dac5970b2dbc2fce
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 /// Filedescriptor to /dev/kvm
56 int fd;
57 int vm_fd;
58 /// Callbacks that KVM uses to emulate various unvirtualizable functionality
59 struct kvm_callbacks *callbacks;
60 void *opaque;
61 /// is dirty pages logging enabled for all regions or not
62 int dirty_pages_log_all;
63 /// do not create in-kernel irqchip if set
64 int no_irqchip_creation;
65 /// in-kernel irqchip status
66 int irqchip_in_kernel;
67 /// ioctl to use to inject interrupts
68 int irqchip_inject_ioctl;
69 /// do not create in-kernel pit if set
70 int no_pit_creation;
71 /// in-kernel pit status
72 int pit_in_kernel;
73 /// in-kernel coalesced mmio
74 int coalesced_mmio;
75 #ifdef KVM_CAP_IRQ_ROUTING
76 struct kvm_irq_routing *irq_routes;
77 int nr_allocated_irq_routes;
78 #endif
79 void *used_gsi_bitmap;
80 int max_gsi;
83 struct kvm_vcpu_context
85 int fd;
86 struct kvm_run *run;
87 struct kvm_context *kvm;
88 uint32_t id;
91 typedef struct kvm_context *kvm_context_t;
92 typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
94 #include "kvm.h"
95 int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
96 void **vm_mem);
97 int kvm_alloc_userspace_memory(kvm_context_t kvm, unsigned long memory,
98 void **vm_mem);
100 int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
101 void **vm_mem);
102 int kvm_arch_run(kvm_vcpu_context_t vcpu);
105 void kvm_show_code(kvm_vcpu_context_t vcpu);
107 int handle_halt(kvm_vcpu_context_t vcpu);
108 int handle_shutdown(kvm_context_t kvm, CPUState *env);
109 void post_kvm_run(kvm_context_t kvm, CPUState *env);
110 int pre_kvm_run(kvm_context_t kvm, CPUState *env);
111 int handle_io_window(kvm_context_t kvm);
112 int handle_debug(kvm_vcpu_context_t vcpu, void *env);
113 int try_push_interrupts(kvm_context_t kvm);
115 #if defined(__x86_64__) || defined(__i386__)
116 struct kvm_msr_list *kvm_get_msr_list(kvm_context_t);
117 int kvm_get_msrs(kvm_vcpu_context_t, struct kvm_msr_entry *msrs, int n);
118 int kvm_set_msrs(kvm_vcpu_context_t, struct kvm_msr_entry *msrs, int n);
119 int kvm_get_mce_cap_supported(kvm_context_t, uint64_t *mce_cap, int *max_banks);
120 int kvm_setup_mce(kvm_vcpu_context_t vcpu, uint64_t *mcg_cap);
121 struct kvm_x86_mce;
122 int kvm_set_mce(kvm_vcpu_context_t vcpu, struct kvm_x86_mce *mce);
123 #endif
126 * \brief Create new KVM context
128 * This creates a new kvm_context. A KVM context is a small area of data that
129 * holds information about the KVM instance that gets created by this call.\n
130 * This should always be your first call to KVM.
132 * \param opaque Not used
133 * \return NULL on failure
135 int kvm_init(int smp_cpus);
138 * \brief Disable the in-kernel IRQCHIP creation
140 * In-kernel irqchip is enabled by default. If userspace irqchip is to be used,
141 * this should be called prior to kvm_create().
143 * \param kvm Pointer to the kvm_context
145 void kvm_disable_irqchip_creation(kvm_context_t kvm);
148 * \brief Disable the in-kernel PIT creation
150 * In-kernel pit is enabled by default. If userspace pit is to be used,
151 * this should be called prior to kvm_create().
153 * \param kvm Pointer to the kvm_context
155 void kvm_disable_pit_creation(kvm_context_t kvm);
158 * \brief Create new virtual machine
160 * This creates a new virtual machine, maps physical RAM to it, and creates a
161 * virtual CPU for it.\n
162 * \n
163 * Memory gets mapped for addresses 0->0xA0000, 0xC0000->phys_mem_bytes
165 * \param kvm Pointer to the current kvm_context
166 * \param phys_mem_bytes The amount of physical ram you want the VM to have
167 * \param phys_mem This pointer will be set to point to the memory that
168 * kvm_create allocates for physical RAM
169 * \return 0 on success
171 int kvm_create(kvm_context_t kvm,
172 unsigned long phys_mem_bytes,
173 void **phys_mem);
174 int kvm_create_vm(kvm_context_t kvm);
175 int kvm_check_extension(kvm_context_t kvm, int ext);
176 void kvm_create_irqchip(kvm_context_t kvm);
179 * \brief Create a new virtual cpu
181 * This creates a new virtual cpu (the first vcpu is created by kvm_create()).
182 * Should be called from a thread dedicated to the vcpu.
184 * \param kvm kvm context
185 * \param slot vcpu number (> 0)
186 * \return 0 on success, -errno on failure
188 kvm_vcpu_context_t kvm_create_vcpu(CPUState *env, int id);
191 * \brief Start the VCPU
193 * This starts the VCPU and virtualization is started.\n
194 * \n
195 * This function will not return until any of these conditions are met:
196 * - An IO/MMIO handler does not return "0"
197 * - An exception that neither the guest OS, nor KVM can handle occurs
199 * \note This function will call the callbacks registered in kvm_init()
200 * to emulate those functions
201 * \note If you at any point want to interrupt the VCPU, kvm_run() will
202 * listen to the EINTR signal. This allows you to simulate external interrupts
203 * and asyncronous IO.
205 * \param kvm Pointer to the current kvm_context
206 * \param vcpu Which virtual CPU should be started
207 * \return 0 on success, but you really shouldn't expect this function to
208 * return except for when an error has occured, or when you have sent it
209 * an EINTR signal.
211 int kvm_run(kvm_vcpu_context_t vcpu, void *env);
214 * \brief Get interrupt flag from on last exit to userspace
216 * This gets the CPU interrupt flag as it was on the last exit to userspace.
218 * \param kvm Pointer to the current kvm_context
219 * \param vcpu Which virtual CPU should get dumped
220 * \return interrupt flag value (0 or 1)
222 int kvm_get_interrupt_flag(kvm_vcpu_context_t vcpu);
225 * \brief Get the value of the APIC_BASE msr as of last exit to userspace
227 * This gets the APIC_BASE msr as it was on the last exit to userspace.
229 * \param kvm Pointer to the current kvm_context
230 * \param vcpu Which virtual CPU should get dumped
231 * \return APIC_BASE msr contents
233 uint64_t kvm_get_apic_base(kvm_vcpu_context_t vcpu);
236 * \brief Check if a vcpu is ready for interrupt injection
238 * This checks if vcpu interrupts are not masked by mov ss or sti.
240 * \param kvm Pointer to the current kvm_context
241 * \param vcpu Which virtual CPU should get dumped
242 * \return boolean indicating interrupt injection readiness
244 int kvm_is_ready_for_interrupt_injection(kvm_vcpu_context_t vcpu);
247 * \brief Read VCPU registers
249 * This gets the GP registers from the VCPU and outputs them
250 * into a kvm_regs structure
252 * \note This function returns a \b copy of the VCPUs registers.\n
253 * If you wish to modify the VCPUs GP registers, you should call kvm_set_regs()
255 * \param kvm Pointer to the current kvm_context
256 * \param vcpu Which virtual CPU should get dumped
257 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
258 * registers values
259 * \return 0 on success
261 int kvm_get_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
264 * \brief Write VCPU registers
266 * This sets the GP registers on the VCPU from a kvm_regs structure
268 * \note When this function returns, the regs pointer and the data it points to
269 * can be discarded
270 * \param kvm Pointer to the current kvm_context
271 * \param vcpu Which virtual CPU should get dumped
272 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
273 * registers values
274 * \return 0 on success
276 int kvm_set_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
278 * \brief Read VCPU fpu registers
280 * This gets the FPU registers from the VCPU and outputs them
281 * into a kvm_fpu structure
283 * \note This function returns a \b copy of the VCPUs registers.\n
284 * If you wish to modify the VCPU FPU registers, you should call kvm_set_fpu()
286 * \param kvm Pointer to the current kvm_context
287 * \param vcpu Which virtual CPU should get dumped
288 * \param fpu Pointer to a kvm_fpu which will be populated with the VCPUs
289 * fpu registers values
290 * \return 0 on success
292 int kvm_get_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
295 * \brief Write VCPU fpu registers
297 * This sets the FPU registers on the VCPU from a kvm_fpu structure
299 * \note When this function returns, the fpu pointer and the data it points to
300 * can be discarded
301 * \param kvm Pointer to the current kvm_context
302 * \param vcpu Which virtual CPU should get dumped
303 * \param fpu Pointer to a kvm_fpu which holds the new vcpu fpu state
304 * \return 0 on success
306 int kvm_set_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
309 * \brief Read VCPU system registers
311 * This gets the non-GP registers from the VCPU and outputs them
312 * into a kvm_sregs structure
314 * \note This function returns a \b copy of the VCPUs registers.\n
315 * If you wish to modify the VCPUs non-GP registers, you should call
316 * kvm_set_sregs()
318 * \param kvm Pointer to the current kvm_context
319 * \param vcpu Which virtual CPU should get dumped
320 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
321 * registers values
322 * \return 0 on success
324 int kvm_get_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
327 * \brief Write VCPU system registers
329 * This sets the non-GP registers on the VCPU from a kvm_sregs structure
331 * \note When this function returns, the regs pointer and the data it points to
332 * can be discarded
333 * \param kvm Pointer to the current kvm_context
334 * \param vcpu Which virtual CPU should get dumped
335 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
336 * registers values
337 * \return 0 on success
339 int kvm_set_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
341 #ifdef KVM_CAP_MP_STATE
343 * * \brief Read VCPU MP state
346 int kvm_get_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
349 * * \brief Write VCPU MP state
352 int kvm_set_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
354 * * \brief Reset VCPU MP state
357 static inline int kvm_reset_mpstate(kvm_vcpu_context_t vcpu)
359 struct kvm_mp_state mp_state = {.mp_state = KVM_MP_STATE_UNINITIALIZED};
360 return kvm_set_mpstate(vcpu, &mp_state);
362 #endif
365 * \brief Simulate an external vectored interrupt
367 * This allows you to simulate an external vectored interrupt.
369 * \param kvm Pointer to the current kvm_context
370 * \param vcpu Which virtual CPU should get dumped
371 * \param irq Vector number
372 * \return 0 on success
374 int kvm_inject_irq(kvm_vcpu_context_t vcpu, unsigned irq);
376 #ifdef KVM_CAP_SET_GUEST_DEBUG
377 int kvm_set_guest_debug(kvm_vcpu_context_t, struct kvm_guest_debug *dbg);
378 #endif
380 #if defined(__i386__) || defined(__x86_64__)
382 * \brief Setup a vcpu's cpuid instruction emulation
384 * Set up a table of cpuid function to cpuid outputs.\n
386 * \param kvm Pointer to the current kvm_context
387 * \param vcpu Which virtual CPU should be initialized
388 * \param nent number of entries to be installed
389 * \param entries cpuid function entries table
390 * \return 0 on success, or -errno on error
392 int kvm_setup_cpuid(kvm_vcpu_context_t vcpu, int nent,
393 struct kvm_cpuid_entry *entries);
396 * \brief Setup a vcpu's cpuid instruction emulation
398 * Set up a table of cpuid function to cpuid outputs.
399 * This call replaces the older kvm_setup_cpuid interface by adding a few
400 * parameters to support cpuid functions that have sub-leaf values.
402 * \param kvm Pointer to the current kvm_context
403 * \param vcpu Which virtual CPU should be initialized
404 * \param nent number of entries to be installed
405 * \param entries cpuid function entries table
406 * \return 0 on success, or -errno on error
408 int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu, int nent,
409 struct kvm_cpuid_entry2 *entries);
412 * \brief Setting the number of shadow pages to be allocated to the vm
414 * \param kvm pointer to kvm_context
415 * \param nrshadow_pages number of pages to be allocated
417 int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages);
420 * \brief Getting the number of shadow pages that are allocated to the vm
422 * \param kvm pointer to kvm_context
423 * \param nrshadow_pages number of pages to be allocated
425 int kvm_get_shadow_pages(kvm_context_t kvm , unsigned int *nrshadow_pages);
428 * \brief Set up cr8 for next time the vcpu is executed
430 * This is a fast setter for cr8, which will be applied when the
431 * vcpu next enters guest mode.
433 * \param kvm Pointer to the current kvm_context
434 * \param vcpu Which virtual CPU should get dumped
435 * \param cr8 next cr8 value
437 void kvm_set_cr8(kvm_vcpu_context_t vcpu, uint64_t cr8);
440 * \brief Get cr8 for sync tpr in qemu apic emulation
442 * This is a getter for cr8, which used to sync with the tpr in qemu
443 * apic emualtion.
445 * \param kvm Pointer to the current kvm_context
446 * \param vcpu Which virtual CPU should get dumped
448 __u64 kvm_get_cr8(kvm_vcpu_context_t vcpu);
449 #endif
452 * \brief Set a vcpu's signal mask for guest mode
454 * A vcpu can have different signals blocked in guest mode and user mode.
455 * This allows guest execution to be interrupted on a signal, without requiring
456 * that the signal be delivered to a signal handler (the signal can be
457 * dequeued using sigwait(2).
459 * \param kvm Pointer to the current kvm_context
460 * \param vcpu Which virtual CPU should be initialized
461 * \param sigset signal mask for guest mode
462 * \return 0 on success, or -errno on error
464 int kvm_set_signal_mask(kvm_vcpu_context_t vcpu, const sigset_t *sigset);
467 * \brief Dump VCPU registers
469 * This dumps some of the information that KVM has about a virtual CPU, namely:
470 * - GP Registers
472 * A much more verbose version of this is available as kvm_dump_vcpu()
474 * \param kvm Pointer to the current kvm_context
475 * \param vcpu Which virtual CPU should get dumped
476 * \return 0 on success
478 void kvm_show_regs(kvm_vcpu_context_t vcpu);
481 void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start,
482 unsigned long len, int log, int writable);
483 void kvm_destroy_phys_mem(kvm_context_t, unsigned long phys_start,
484 unsigned long len);
485 void kvm_unregister_memory_area(kvm_context_t, uint64_t phys_start,
486 unsigned long len);
488 int kvm_is_containing_region(kvm_context_t kvm, unsigned long phys_start, unsigned long size);
489 int kvm_register_phys_mem(kvm_context_t kvm,
490 unsigned long phys_start, void *userspace_addr,
491 unsigned long len, int log);
492 int kvm_get_dirty_pages(kvm_context_t, unsigned long phys_addr, void *buf);
493 int kvm_get_dirty_pages_range(kvm_context_t kvm, unsigned long phys_addr,
494 unsigned long end_addr, void*opaque,
495 int (*cb)(unsigned long start, unsigned long len,
496 void*bitmap, void *opaque));
497 int kvm_register_coalesced_mmio(kvm_context_t kvm,
498 uint64_t addr, uint32_t size);
499 int kvm_unregister_coalesced_mmio(kvm_context_t kvm,
500 uint64_t addr, uint32_t size);
503 * \brief Create a memory alias
505 * Aliases a portion of physical memory to another portion. If the guest
506 * accesses the alias region, it will behave exactly as if it accessed
507 * the target memory.
509 int kvm_create_memory_alias(kvm_context_t,
510 uint64_t phys_start, uint64_t len,
511 uint64_t target_phys);
514 * \brief Destroy a memory alias
516 * Removes an alias created with kvm_create_memory_alias().
518 int kvm_destroy_memory_alias(kvm_context_t, uint64_t phys_start);
521 * \brief Get a bitmap of guest ram pages which are allocated to the guest.
523 * \param kvm Pointer to the current kvm_context
524 * \param phys_addr Memory slot phys addr
525 * \param bitmap Long aligned address of a big enough bitmap (one bit per page)
527 int kvm_get_mem_map(kvm_context_t kvm, unsigned long phys_addr, void *bitmap);
528 int kvm_get_mem_map_range(kvm_context_t kvm, unsigned long phys_addr,
529 unsigned long len, void *buf, void *opaque,
530 int (*cb)(unsigned long start,unsigned long len,
531 void* bitmap, void* opaque));
532 int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status);
534 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm,
535 uint64_t phys_start,
536 uint64_t len);
537 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm,
538 uint64_t phys_start,
539 uint64_t len);
541 * \brief Enable dirty-pages-logging for all memory regions
543 * \param kvm Pointer to the current kvm_context
545 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm);
548 * \brief Disable dirty-page-logging for some memory regions
550 * Disable dirty-pages-logging for those memory regions that were
551 * created with dirty-page-logging disabled.
553 * \param kvm Pointer to the current kvm_context
555 int kvm_dirty_pages_log_reset(kvm_context_t kvm);
558 * \brief Query whether in kernel irqchip is used
560 * \param kvm Pointer to the current kvm_context
562 int kvm_irqchip_in_kernel(kvm_context_t kvm);
564 #ifdef KVM_CAP_IRQCHIP
566 * \brief Dump in kernel IRQCHIP contents
568 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
569 * and IOAPIC into a kvm_irqchip structure
571 * \param kvm Pointer to the current kvm_context
572 * \param chip The irq chip device to be dumped
574 int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
577 * \brief Set in kernel IRQCHIP contents
579 * Write one of the in kernel irq chip devices, including PIC (master/slave)
580 * and IOAPIC
583 * \param kvm Pointer to the current kvm_context
584 * \param chip THe irq chip device to be written
586 int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
588 #if defined(__i386__) || defined(__x86_64__)
590 * \brief Get in kernel local APIC for vcpu
592 * Save the local apic state including the timer of a virtual CPU
594 * \param kvm Pointer to the current kvm_context
595 * \param vcpu Which virtual CPU should be accessed
596 * \param s Local apic state of the specific virtual CPU
598 int kvm_get_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
601 * \brief Set in kernel local APIC for vcpu
603 * Restore the local apic state including the timer of a virtual CPU
605 * \param kvm Pointer to the current kvm_context
606 * \param vcpu Which virtual CPU should be accessed
607 * \param s Local apic state of the specific virtual CPU
609 int kvm_set_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
611 #endif
614 * \brief Simulate an NMI
616 * This allows you to simulate a non-maskable interrupt.
618 * \param kvm Pointer to the current kvm_context
619 * \param vcpu Which virtual CPU should get dumped
620 * \return 0 on success
622 int kvm_inject_nmi(kvm_vcpu_context_t vcpu);
624 #endif
627 * \brief Simulate an x86 MCE
629 * This allows you to simulate a x86 MCE.
631 * \param cenv Which virtual CPU should get MCE injected
632 * \param bank Bank number
633 * \param status MSR_MCI_STATUS
634 * \param mcg_status MSR_MCG_STATUS
635 * \param addr MSR_MCI_ADDR
636 * \param misc MSR_MCI_MISC
638 void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
639 uint64_t mcg_status, uint64_t addr, uint64_t misc);
642 * \brief Query wheather in kernel pit is used
644 * \param kvm Pointer to the current kvm_context
646 int kvm_pit_in_kernel(kvm_context_t kvm);
649 * \brief Initialize coalesced MMIO
651 * Check for coalesced MMIO capability and store in context
653 * \param kvm Pointer to the current kvm_context
655 int kvm_init_coalesced_mmio(kvm_context_t kvm);
657 #ifdef KVM_CAP_PIT
659 #if defined(__i386__) || defined(__x86_64__)
661 * \brief Get in kernel PIT of the virtual domain
663 * Save the PIT state.
665 * \param kvm Pointer to the current kvm_context
666 * \param s PIT state of the virtual domain
668 int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s);
671 * \brief Set in kernel PIT of the virtual domain
673 * Restore the PIT state.
674 * Timer would be retriggerred after restored.
676 * \param kvm Pointer to the current kvm_context
677 * \param s PIT state of the virtual domain
679 int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s);
681 int kvm_reinject_control(kvm_context_t kvm, int pit_reinject);
683 #ifdef KVM_CAP_PIT_STATE2
685 * \brief Check for kvm support of kvm_pit_state2
687 * \param kvm Pointer to the current kvm_context
688 * \return 0 on success
690 int kvm_has_pit_state2(kvm_context_t kvm);
693 * \brief Set in kernel PIT state2 of the virtual domain
696 * \param kvm Pointer to the current kvm_context
697 * \param ps2 PIT state2 of the virtual domain
698 * \return 0 on success
700 int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
703 * \brief Get in kernel PIT state2 of the virtual domain
706 * \param kvm Pointer to the current kvm_context
707 * \param ps2 PIT state2 of the virtual domain
708 * \return 0 on success
710 int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
712 #endif
713 #endif
714 #endif
716 #ifdef KVM_CAP_VAPIC
719 * \brief Enable kernel tpr access reporting
721 * When tpr access reporting is enabled, the kernel will call the
722 * ->tpr_access() callback every time the guest vcpu accesses the tpr.
724 * \param kvm Pointer to the current kvm_context
725 * \param vcpu vcpu to enable tpr access reporting on
727 int kvm_enable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
730 * \brief Disable kernel tpr access reporting
732 * Undoes the effect of kvm_enable_tpr_access_reporting().
734 * \param kvm Pointer to the current kvm_context
735 * \param vcpu vcpu to disable tpr access reporting on
737 int kvm_disable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
739 int kvm_enable_vapic(kvm_vcpu_context_t vcpu, uint64_t vapic);
741 #endif
743 #if defined(__s390__)
744 int kvm_s390_initial_reset(kvm_context_t kvm, int slot);
745 int kvm_s390_interrupt(kvm_context_t kvm, int slot,
746 struct kvm_s390_interrupt *kvmint);
747 int kvm_s390_set_initial_psw(kvm_context_t kvm, int slot, psw_t psw);
748 int kvm_s390_store_status(kvm_context_t kvm, int slot, unsigned long addr);
749 #endif
751 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
753 * \brief Notifies host kernel about a PCI device to be assigned to a guest
755 * Used for PCI device assignment, this function notifies the host
756 * kernel about the assigning of the physical PCI device to a guest.
758 * \param kvm Pointer to the current kvm_context
759 * \param assigned_dev Parameters, like bus, devfn number, etc
761 int kvm_assign_pci_device(kvm_context_t kvm,
762 struct kvm_assigned_pci_dev *assigned_dev);
765 * \brief Assign IRQ for an assigned device
767 * Used for PCI device assignment, this function assigns IRQ numbers for
768 * an physical device and guest IRQ handling.
770 * \param kvm Pointer to the current kvm_context
771 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
773 int kvm_assign_irq(kvm_context_t kvm,
774 struct kvm_assigned_irq *assigned_irq);
776 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
778 * \brief Deassign IRQ for an assigned device
780 * Used for PCI device assignment, this function deassigns IRQ numbers
781 * for an assigned device.
783 * \param kvm Pointer to the current kvm_context
784 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
786 int kvm_deassign_irq(kvm_context_t kvm,
787 struct kvm_assigned_irq *assigned_irq);
788 #endif
789 #endif
792 * \brief Determines whether destroying memory regions is allowed
794 * KVM before 2.6.29 had a bug when destroying memory regions.
796 * \param kvm Pointer to the current kvm_context
798 int kvm_destroy_memory_region_works(kvm_context_t kvm);
800 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
802 * \brief Notifies host kernel about a PCI device to be deassigned from a guest
804 * Used for hot remove PCI device, this function notifies the host
805 * kernel about the deassigning of the physical PCI device from a guest.
807 * \param kvm Pointer to the current kvm_context
808 * \param assigned_dev Parameters, like bus, devfn number, etc
810 int kvm_deassign_pci_device(kvm_context_t kvm,
811 struct kvm_assigned_pci_dev *assigned_dev);
812 #endif
815 * \brief Checks whether the generic irq routing capability is present
817 * Checks whether kvm can reroute interrupts among the various interrupt
818 * controllers.
820 * \param kvm Pointer to the current kvm_context
822 int kvm_has_gsi_routing(kvm_context_t kvm);
825 * \brief Determines the number of gsis that can be routed
827 * Returns the number of distinct gsis that can be routed by kvm. This is
828 * also the number of distinct routes (if a gsi has two routes, than another
829 * gsi cannot be used...)
831 * \param kvm Pointer to the current kvm_context
833 int kvm_get_gsi_count(kvm_context_t kvm);
836 * \brief Clears the temporary irq routing table
838 * Clears the temporary irq routing table. Nothing is committed to the
839 * running VM.
841 * \param kvm Pointer to the current kvm_context
843 int kvm_clear_gsi_routes(kvm_context_t kvm);
846 * \brief Adds an irq route to the temporary irq routing table
848 * Adds an irq route to the temporary irq routing table. Nothing is
849 * committed to the running VM.
851 * \param kvm Pointer to the current kvm_context
853 int kvm_add_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
856 * \brief Removes an irq route from the temporary irq routing table
858 * Adds an irq route to the temporary irq routing table. Nothing is
859 * committed to the running VM.
861 * \param kvm Pointer to the current kvm_context
863 int kvm_del_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
865 struct kvm_irq_routing_entry;
867 * \brief Adds a routing entry to the temporary irq routing table
869 * Adds a filled routing entry to the temporary irq routing table. Nothing is
870 * committed to the running VM.
872 * \param kvm Pointer to the current kvm_context
874 int kvm_add_routing_entry(kvm_context_t kvm,
875 struct kvm_irq_routing_entry* entry);
878 * \brief Removes a routing from the temporary irq routing table
880 * Remove a routing to the temporary irq routing table. Nothing is
881 * committed to the running VM.
883 * \param kvm Pointer to the current kvm_context
885 int kvm_del_routing_entry(kvm_context_t kvm,
886 struct kvm_irq_routing_entry* entry);
889 * \brief Updates a routing in the temporary irq routing table
891 * Update a routing in the temporary irq routing table
892 * with a new value. entry type and GSI can not be changed.
893 * Nothing is committed to the running VM.
895 * \param kvm Pointer to the current kvm_context
897 int kvm_update_routing_entry(kvm_context_t kvm,
898 struct kvm_irq_routing_entry* entry,
899 struct kvm_irq_routing_entry* newentry
903 * \brief Commit the temporary irq routing table
905 * Commit the temporary irq routing table to the running VM.
907 * \param kvm Pointer to the current kvm_context
909 int kvm_commit_irq_routes(kvm_context_t kvm);
912 * \brief Get unused GSI number for irq routing table
914 * Get unused GSI number for irq routing table
916 * \param kvm Pointer to the current kvm_context
918 int kvm_get_irq_route_gsi(kvm_context_t kvm);
921 * \brief Create a file descriptor for injecting interrupts
923 * Creates an eventfd based file-descriptor that maps to a specific GSI
924 * in the guest. eventfd compliant signaling (write() from userspace, or
925 * eventfd_signal() from kernelspace) will cause the GSI to inject
926 * itself into the guest at the next available window.
928 * \param kvm Pointer to the current kvm_context
929 * \param gsi GSI to assign to this fd
930 * \param flags reserved, must be zero
932 int kvm_irqfd(kvm_context_t kvm, int gsi, int flags);
934 #ifdef KVM_CAP_DEVICE_MSIX
935 int kvm_assign_set_msix_nr(kvm_context_t kvm,
936 struct kvm_assigned_msix_nr *msix_nr);
937 int kvm_assign_set_msix_entry(kvm_context_t kvm,
938 struct kvm_assigned_msix_entry *entry);
939 #endif
941 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg);
943 #else /* !CONFIG_KVM */
945 struct kvm_pit_state { };
947 #endif /* !CONFIG_KVM */
950 int kvm_main_loop(void);
951 int kvm_qemu_init(void);
952 int kvm_qemu_create_context(void);
953 int kvm_init_ap(void);
954 int kvm_vcpu_inited(CPUState *env);
955 void kvm_load_registers(CPUState *env);
956 void kvm_save_registers(CPUState *env);
957 void kvm_load_mpstate(CPUState *env);
958 void kvm_save_mpstate(CPUState *env);
959 int kvm_cpu_exec(CPUState *env);
960 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
961 target_ulong len, int type);
962 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
963 target_ulong len, int type);
964 void kvm_remove_all_breakpoints(CPUState *current_env);
965 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
966 int kvm_qemu_init_env(CPUState *env);
967 int kvm_qemu_check_extension(int ext);
968 void kvm_apic_init(CPUState *env);
969 /* called from vcpu initialization */
970 void qemu_kvm_load_lapic(CPUState *env);
972 void kvm_hpet_enable_kpit(void);
973 void kvm_hpet_disable_kpit(void);
974 int kvm_set_irq(int irq, int level, int *status);
976 int kvm_physical_memory_set_dirty_tracking(int enable);
977 int kvm_update_dirty_pages_log(void);
978 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap);
980 void qemu_kvm_call_with_env(void (*func)(void *), void *data, CPUState *env);
981 void qemu_kvm_cpuid_on_env(CPUState *env);
982 void kvm_inject_interrupt(CPUState *env, int mask);
983 void kvm_update_after_sipi(CPUState *env);
984 void kvm_update_interrupt_request(CPUState *env);
985 void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
986 ram_addr_t phys_offset);
987 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
988 unsigned long size, int log, int writable);
990 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
991 unsigned long size);
992 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
993 int log);
994 int kvm_setup_guest_memory(void *area, unsigned long size);
995 int kvm_qemu_create_memory_alias(uint64_t phys_start,
996 uint64_t len,
997 uint64_t target_phys);
998 int kvm_qemu_destroy_memory_alias(uint64_t phys_start);
1000 int kvm_arch_qemu_create_context(void);
1002 void kvm_arch_save_regs(CPUState *env);
1003 void kvm_arch_load_regs(CPUState *env);
1004 void kvm_arch_load_mpstate(CPUState *env);
1005 void kvm_arch_save_mpstate(CPUState *env);
1006 int kvm_arch_qemu_init_env(CPUState *cenv);
1007 void kvm_arch_pre_kvm_run(void *opaque, CPUState *env);
1008 void kvm_arch_post_kvm_run(void *opaque, CPUState *env);
1009 int kvm_arch_has_work(CPUState *env);
1010 void kvm_arch_process_irqchip_events(CPUState *env);
1011 int kvm_arch_try_push_interrupts(void *opaque);
1012 void kvm_arch_push_nmi(void *opaque);
1013 void kvm_arch_update_regs_for_sipi(CPUState *env);
1014 void kvm_arch_cpu_reset(CPUState *env);
1015 int kvm_set_boot_cpu_id(uint32_t id);
1017 struct kvm_guest_debug;
1018 struct kvm_debug_exit_arch;
1020 struct kvm_sw_breakpoint {
1021 target_ulong pc;
1022 target_ulong saved_insn;
1023 int use_count;
1024 TAILQ_ENTRY(kvm_sw_breakpoint) entry;
1027 TAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
1029 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
1030 int kvm_sw_breakpoints_active(CPUState *env);
1031 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env, target_ulong pc);
1032 int kvm_arch_insert_sw_breakpoint(CPUState *current_env,
1033 struct kvm_sw_breakpoint *bp);
1034 int kvm_arch_remove_sw_breakpoint(CPUState *current_env,
1035 struct kvm_sw_breakpoint *bp);
1036 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
1037 target_ulong len, int type);
1038 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
1039 target_ulong len, int type);
1040 void kvm_arch_remove_all_hw_breakpoints(void);
1041 void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
1043 void qemu_kvm_aio_wait_start(void);
1044 void qemu_kvm_aio_wait(void);
1045 void qemu_kvm_aio_wait_end(void);
1047 void qemu_kvm_notify_work(void);
1049 void kvm_tpr_opt_setup(void);
1050 void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
1051 void kvm_tpr_vcpu_start(CPUState *env);
1053 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf);
1054 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1055 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1057 int kvm_arch_init_irq_routing(void);
1059 int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len);
1060 int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len);
1062 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1063 struct ioperm_data;
1065 void kvm_ioperm(CPUState *env, void *data);
1066 void kvm_add_ioperm_data(struct ioperm_data *data);
1067 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num);
1068 void kvm_arch_do_ioperm(void *_data);
1069 #endif
1071 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
1072 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
1074 #ifdef CONFIG_KVM
1075 #include "sys-queue.h"
1077 extern int kvm_allowed;
1078 extern int kvm_irqchip;
1079 extern int kvm_pit;
1080 extern int kvm_pit_reinject;
1081 extern int kvm_nested;
1082 extern kvm_context_t kvm_context;
1084 struct ioperm_data {
1085 unsigned long start_port;
1086 unsigned long num;
1087 int turn_on;
1088 LIST_ENTRY(ioperm_data) entries;
1091 void qemu_kvm_cpu_stop(CPUState *env);
1092 int kvm_arch_halt(void *opaque, kvm_vcpu_context_t vcpu);
1093 int handle_tpr_access(void *opaque, kvm_vcpu_context_t vcpu,
1094 uint64_t rip, int is_write);
1095 int kvm_has_sync_mmu(void);
1097 #define kvm_enabled() (kvm_allowed)
1098 #define qemu_kvm_irqchip_in_kernel() kvm_irqchip_in_kernel(kvm_context)
1099 #define qemu_kvm_pit_in_kernel() kvm_pit_in_kernel(kvm_context)
1100 #define qemu_kvm_has_gsi_routing() kvm_has_gsi_routing(kvm_context)
1101 #ifdef TARGET_I386
1102 #define qemu_kvm_has_pit_state2() kvm_has_pit_state2(kvm_context)
1103 #endif
1104 void kvm_init_vcpu(CPUState *env);
1105 void kvm_load_tsc(CPUState *env);
1106 #else
1107 #define kvm_has_sync_mmu() (0)
1108 #define kvm_enabled() (0)
1109 #define kvm_nested 0
1110 #define qemu_kvm_irqchip_in_kernel() (0)
1111 #define qemu_kvm_pit_in_kernel() (0)
1112 #define qemu_kvm_has_gsi_routing() (0)
1113 #ifdef TARGET_I386
1114 #define qemu_kvm_has_pit_state2() (0)
1115 #endif
1116 #define kvm_load_registers(env) do {} while(0)
1117 #define kvm_save_registers(env) do {} while(0)
1118 #define qemu_kvm_cpu_stop(env) do {} while(0)
1119 static inline void kvm_init_vcpu(CPUState *env) { }
1120 static inline void kvm_load_tsc(CPUState *env) {}
1121 #endif
1123 void kvm_mutex_unlock(void);
1124 void kvm_mutex_lock(void);
1126 static inline void kvm_sleep_begin(void)
1128 if (kvm_enabled())
1129 kvm_mutex_unlock();
1132 static inline void kvm_sleep_end(void)
1134 if (kvm_enabled())
1135 kvm_mutex_lock();
1138 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr);
1140 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len);
1141 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len);
1144 static inline int kvm_sync_vcpus(void) { return 0; }
1146 static inline void kvm_arch_get_registers(CPUState *env)
1148 kvm_save_registers(env);
1149 kvm_save_mpstate(env);
1152 static inline void kvm_arch_put_registers(CPUState *env)
1154 kvm_load_registers(env);
1155 kvm_load_mpstate(env);
1158 static inline void cpu_synchronize_state(CPUState *env, int modified)
1160 if (kvm_enabled()) {
1161 if (modified)
1162 kvm_arch_put_registers(env);
1163 else
1164 kvm_arch_get_registers(env);
1168 uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
1169 int reg);
1172 static inline int kvm_set_migration_log(int enable)
1174 return kvm_physical_memory_set_dirty_tracking(enable);
1177 typedef struct KVMSlot
1179 target_phys_addr_t start_addr;
1180 ram_addr_t memory_size;
1181 ram_addr_t phys_offset;
1182 int slot;
1183 int flags;
1184 } KVMSlot;
1186 typedef struct kvm_dirty_log KVMDirtyLog;
1188 typedef struct KVMState
1190 KVMSlot slots[32];
1191 int fd;
1192 int vmfd;
1193 int coalesced_mmio;
1194 int broken_set_mem_region;
1195 int migration_log;
1196 #ifdef KVM_CAP_SET_GUEST_DEBUG
1197 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
1198 #endif
1199 struct kvm_context kvm_context;
1200 } KVMState;
1203 #endif