4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
8 #ifndef THE_ORIGINAL_AND_TRUE_QEMU_KVM_H
9 #define THE_ORIGINAL_AND_TRUE_QEMU_KVM_H
18 #include <asm/ptrace.h>
24 #define __user /* temporary, until installed via make headers_install */
27 #include <linux/kvm.h>
31 /* FIXME: share this number with kvm */
32 /* FIXME: or dynamically alloc/realloc regions */
34 #define KVM_MAX_NUM_MEM_REGIONS 1u
36 #define LIBKVM_S390_ORIGIN (0UL)
37 #elif defined(__ia64__)
38 #define KVM_MAX_NUM_MEM_REGIONS 32u
41 #define KVM_MAX_NUM_MEM_REGIONS 32u
45 /* kvm abi verison variable */
49 * \brief The KVM context
51 * The verbose KVM context
55 /// Filedescriptor to /dev/kvm
58 /// Callbacks that KVM uses to emulate various unvirtualizable functionality
59 struct kvm_callbacks
*callbacks
;
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
71 /// in-kernel pit status
73 /// in-kernel coalesced mmio
75 #ifdef KVM_CAP_IRQ_ROUTING
76 struct kvm_irq_routing
*irq_routes
;
77 int nr_allocated_irq_routes
;
79 void *used_gsi_bitmap
;
83 struct kvm_vcpu_context
87 struct kvm_context
*kvm
;
91 typedef struct kvm_context
*kvm_context_t
;
92 typedef struct kvm_vcpu_context
*kvm_vcpu_context_t
;
95 int kvm_alloc_kernel_memory(kvm_context_t kvm
, unsigned long memory
,
97 int kvm_alloc_userspace_memory(kvm_context_t kvm
, unsigned long memory
,
100 int kvm_arch_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
,
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
);
122 * \brief Create new KVM context
124 * This creates a new kvm_context. A KVM context is a small area of data that
125 * holds information about the KVM instance that gets created by this call.\n
126 * This should always be your first call to KVM.
128 * \param opaque Not used
129 * \return NULL on failure
131 int kvm_init(int smp_cpus
);
134 * \brief Disable the in-kernel IRQCHIP creation
136 * In-kernel irqchip is enabled by default. If userspace irqchip is to be used,
137 * this should be called prior to kvm_create().
139 * \param kvm Pointer to the kvm_context
141 void kvm_disable_irqchip_creation(kvm_context_t kvm
);
144 * \brief Disable the in-kernel PIT creation
146 * In-kernel pit is enabled by default. If userspace pit is to be used,
147 * this should be called prior to kvm_create().
149 * \param kvm Pointer to the kvm_context
151 void kvm_disable_pit_creation(kvm_context_t kvm
);
154 * \brief Create new virtual machine
156 * This creates a new virtual machine, maps physical RAM to it, and creates a
157 * virtual CPU for it.\n
159 * Memory gets mapped for addresses 0->0xA0000, 0xC0000->phys_mem_bytes
161 * \param kvm Pointer to the current kvm_context
162 * \param phys_mem_bytes The amount of physical ram you want the VM to have
163 * \param phys_mem This pointer will be set to point to the memory that
164 * kvm_create allocates for physical RAM
165 * \return 0 on success
167 int kvm_create(kvm_context_t kvm
,
168 unsigned long phys_mem_bytes
,
170 int kvm_create_vm(kvm_context_t kvm
);
171 int kvm_check_extension(kvm_context_t kvm
, int ext
);
172 void kvm_create_irqchip(kvm_context_t kvm
);
175 * \brief Create a new virtual cpu
177 * This creates a new virtual cpu (the first vcpu is created by kvm_create()).
178 * Should be called from a thread dedicated to the vcpu.
180 * \param kvm kvm context
181 * \param slot vcpu number (> 0)
182 * \return 0 on success, -errno on failure
184 kvm_vcpu_context_t
kvm_create_vcpu(CPUState
*env
, int id
);
187 * \brief Start the VCPU
189 * This starts the VCPU and virtualization is started.\n
191 * This function will not return until any of these conditions are met:
192 * - An IO/MMIO handler does not return "0"
193 * - An exception that neither the guest OS, nor KVM can handle occurs
195 * \note This function will call the callbacks registered in kvm_init()
196 * to emulate those functions
197 * \note If you at any point want to interrupt the VCPU, kvm_run() will
198 * listen to the EINTR signal. This allows you to simulate external interrupts
199 * and asyncronous IO.
201 * \param kvm Pointer to the current kvm_context
202 * \param vcpu Which virtual CPU should be started
203 * \return 0 on success, but you really shouldn't expect this function to
204 * return except for when an error has occured, or when you have sent it
207 int kvm_run(kvm_vcpu_context_t vcpu
, void *env
);
210 * \brief Get interrupt flag from on last exit to userspace
212 * This gets the CPU interrupt flag as it was on the last exit to userspace.
214 * \param kvm Pointer to the current kvm_context
215 * \param vcpu Which virtual CPU should get dumped
216 * \return interrupt flag value (0 or 1)
218 int kvm_get_interrupt_flag(kvm_vcpu_context_t vcpu
);
221 * \brief Get the value of the APIC_BASE msr as of last exit to userspace
223 * This gets the APIC_BASE msr as it was on the last exit to userspace.
225 * \param kvm Pointer to the current kvm_context
226 * \param vcpu Which virtual CPU should get dumped
227 * \return APIC_BASE msr contents
229 uint64_t kvm_get_apic_base(kvm_vcpu_context_t vcpu
);
232 * \brief Check if a vcpu is ready for interrupt injection
234 * This checks if vcpu interrupts are not masked by mov ss or sti.
236 * \param kvm Pointer to the current kvm_context
237 * \param vcpu Which virtual CPU should get dumped
238 * \return boolean indicating interrupt injection readiness
240 int kvm_is_ready_for_interrupt_injection(kvm_vcpu_context_t vcpu
);
243 * \brief Read VCPU registers
245 * This gets the GP registers from the VCPU and outputs them
246 * into a kvm_regs structure
248 * \note This function returns a \b copy of the VCPUs registers.\n
249 * If you wish to modify the VCPUs GP registers, you should call kvm_set_regs()
251 * \param kvm Pointer to the current kvm_context
252 * \param vcpu Which virtual CPU should get dumped
253 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
255 * \return 0 on success
257 int kvm_get_regs(kvm_vcpu_context_t vcpu
, struct kvm_regs
*regs
);
260 * \brief Write VCPU registers
262 * This sets the GP registers on the VCPU from a kvm_regs structure
264 * \note When this function returns, the regs pointer and the data it points to
266 * \param kvm Pointer to the current kvm_context
267 * \param vcpu Which virtual CPU should get dumped
268 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
270 * \return 0 on success
272 int kvm_set_regs(kvm_vcpu_context_t vcpu
, struct kvm_regs
*regs
);
274 * \brief Read VCPU fpu registers
276 * This gets the FPU registers from the VCPU and outputs them
277 * into a kvm_fpu structure
279 * \note This function returns a \b copy of the VCPUs registers.\n
280 * If you wish to modify the VCPU FPU registers, you should call kvm_set_fpu()
282 * \param kvm Pointer to the current kvm_context
283 * \param vcpu Which virtual CPU should get dumped
284 * \param fpu Pointer to a kvm_fpu which will be populated with the VCPUs
285 * fpu registers values
286 * \return 0 on success
288 int kvm_get_fpu(kvm_vcpu_context_t vcpu
, struct kvm_fpu
*fpu
);
291 * \brief Write VCPU fpu registers
293 * This sets the FPU registers on the VCPU from a kvm_fpu structure
295 * \note When this function returns, the fpu pointer and the data it points to
297 * \param kvm Pointer to the current kvm_context
298 * \param vcpu Which virtual CPU should get dumped
299 * \param fpu Pointer to a kvm_fpu which holds the new vcpu fpu state
300 * \return 0 on success
302 int kvm_set_fpu(kvm_vcpu_context_t vcpu
, struct kvm_fpu
*fpu
);
305 * \brief Read VCPU system registers
307 * This gets the non-GP registers from the VCPU and outputs them
308 * into a kvm_sregs structure
310 * \note This function returns a \b copy of the VCPUs registers.\n
311 * If you wish to modify the VCPUs non-GP registers, you should call
314 * \param kvm Pointer to the current kvm_context
315 * \param vcpu Which virtual CPU should get dumped
316 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
318 * \return 0 on success
320 int kvm_get_sregs(kvm_vcpu_context_t vcpu
, struct kvm_sregs
*regs
);
323 * \brief Write VCPU system registers
325 * This sets the non-GP registers on the VCPU from a kvm_sregs structure
327 * \note When this function returns, the regs pointer and the data it points to
329 * \param kvm Pointer to the current kvm_context
330 * \param vcpu Which virtual CPU should get dumped
331 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
333 * \return 0 on success
335 int kvm_set_sregs(kvm_vcpu_context_t vcpu
, struct kvm_sregs
*regs
);
337 #ifdef KVM_CAP_MP_STATE
339 * * \brief Read VCPU MP state
342 int kvm_get_mpstate(kvm_vcpu_context_t vcpu
, struct kvm_mp_state
*mp_state
);
345 * * \brief Write VCPU MP state
348 int kvm_set_mpstate(kvm_vcpu_context_t vcpu
, struct kvm_mp_state
*mp_state
);
350 * * \brief Reset VCPU MP state
353 static inline int kvm_reset_mpstate(kvm_vcpu_context_t vcpu
)
355 struct kvm_mp_state mp_state
= {.mp_state
= KVM_MP_STATE_UNINITIALIZED
};
356 return kvm_set_mpstate(vcpu
, &mp_state
);
361 * \brief Simulate an external vectored interrupt
363 * This allows you to simulate an external vectored interrupt.
365 * \param kvm Pointer to the current kvm_context
366 * \param vcpu Which virtual CPU should get dumped
367 * \param irq Vector number
368 * \return 0 on success
370 int kvm_inject_irq(kvm_vcpu_context_t vcpu
, unsigned irq
);
372 #ifdef KVM_CAP_SET_GUEST_DEBUG
373 int kvm_set_guest_debug(kvm_vcpu_context_t
, struct kvm_guest_debug
*dbg
);
376 #if defined(__i386__) || defined(__x86_64__)
378 * \brief Setup a vcpu's cpuid instruction emulation
380 * Set up a table of cpuid function to cpuid outputs.\n
382 * \param kvm Pointer to the current kvm_context
383 * \param vcpu Which virtual CPU should be initialized
384 * \param nent number of entries to be installed
385 * \param entries cpuid function entries table
386 * \return 0 on success, or -errno on error
388 int kvm_setup_cpuid(kvm_vcpu_context_t vcpu
, int nent
,
389 struct kvm_cpuid_entry
*entries
);
392 * \brief Setup a vcpu's cpuid instruction emulation
394 * Set up a table of cpuid function to cpuid outputs.
395 * This call replaces the older kvm_setup_cpuid interface by adding a few
396 * parameters to support cpuid functions that have sub-leaf values.
398 * \param kvm Pointer to the current kvm_context
399 * \param vcpu Which virtual CPU should be initialized
400 * \param nent number of entries to be installed
401 * \param entries cpuid function entries table
402 * \return 0 on success, or -errno on error
404 int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu
, int nent
,
405 struct kvm_cpuid_entry2
*entries
);
408 * \brief Setting the number of shadow pages to be allocated to the vm
410 * \param kvm pointer to kvm_context
411 * \param nrshadow_pages number of pages to be allocated
413 int kvm_set_shadow_pages(kvm_context_t kvm
, unsigned int nrshadow_pages
);
416 * \brief Getting the number of shadow pages that are allocated to the vm
418 * \param kvm pointer to kvm_context
419 * \param nrshadow_pages number of pages to be allocated
421 int kvm_get_shadow_pages(kvm_context_t kvm
, unsigned int *nrshadow_pages
);
424 * \brief Set up cr8 for next time the vcpu is executed
426 * This is a fast setter for cr8, which will be applied when the
427 * vcpu next enters guest mode.
429 * \param kvm Pointer to the current kvm_context
430 * \param vcpu Which virtual CPU should get dumped
431 * \param cr8 next cr8 value
433 void kvm_set_cr8(kvm_vcpu_context_t vcpu
, uint64_t cr8
);
436 * \brief Get cr8 for sync tpr in qemu apic emulation
438 * This is a getter for cr8, which used to sync with the tpr in qemu
441 * \param kvm Pointer to the current kvm_context
442 * \param vcpu Which virtual CPU should get dumped
444 __u64
kvm_get_cr8(kvm_vcpu_context_t vcpu
);
448 * \brief Set a vcpu's signal mask for guest mode
450 * A vcpu can have different signals blocked in guest mode and user mode.
451 * This allows guest execution to be interrupted on a signal, without requiring
452 * that the signal be delivered to a signal handler (the signal can be
453 * dequeued using sigwait(2).
455 * \param kvm Pointer to the current kvm_context
456 * \param vcpu Which virtual CPU should be initialized
457 * \param sigset signal mask for guest mode
458 * \return 0 on success, or -errno on error
460 int kvm_set_signal_mask(kvm_vcpu_context_t vcpu
, const sigset_t
*sigset
);
463 * \brief Dump VCPU registers
465 * This dumps some of the information that KVM has about a virtual CPU, namely:
468 * A much more verbose version of this is available as kvm_dump_vcpu()
470 * \param kvm Pointer to the current kvm_context
471 * \param vcpu Which virtual CPU should get dumped
472 * \return 0 on success
474 void kvm_show_regs(kvm_vcpu_context_t vcpu
);
477 void *kvm_create_phys_mem(kvm_context_t
, unsigned long phys_start
,
478 unsigned long len
, int log
, int writable
);
479 void kvm_destroy_phys_mem(kvm_context_t
, unsigned long phys_start
,
481 void kvm_unregister_memory_area(kvm_context_t
, uint64_t phys_start
,
484 int kvm_is_containing_region(kvm_context_t kvm
, unsigned long phys_start
, unsigned long size
);
485 int kvm_register_phys_mem(kvm_context_t kvm
,
486 unsigned long phys_start
, void *userspace_addr
,
487 unsigned long len
, int log
);
488 int kvm_get_dirty_pages(kvm_context_t
, unsigned long phys_addr
, void *buf
);
489 int kvm_get_dirty_pages_range(kvm_context_t kvm
, unsigned long phys_addr
,
490 unsigned long end_addr
, void*opaque
,
491 int (*cb
)(unsigned long start
, unsigned long len
,
492 void*bitmap
, void *opaque
));
493 int kvm_register_coalesced_mmio(kvm_context_t kvm
,
494 uint64_t addr
, uint32_t size
);
495 int kvm_unregister_coalesced_mmio(kvm_context_t kvm
,
496 uint64_t addr
, uint32_t size
);
499 * \brief Create a memory alias
501 * Aliases a portion of physical memory to another portion. If the guest
502 * accesses the alias region, it will behave exactly as if it accessed
505 int kvm_create_memory_alias(kvm_context_t
,
506 uint64_t phys_start
, uint64_t len
,
507 uint64_t target_phys
);
510 * \brief Destroy a memory alias
512 * Removes an alias created with kvm_create_memory_alias().
514 int kvm_destroy_memory_alias(kvm_context_t
, uint64_t phys_start
);
517 * \brief Get a bitmap of guest ram pages which are allocated to the guest.
519 * \param kvm Pointer to the current kvm_context
520 * \param phys_addr Memory slot phys addr
521 * \param bitmap Long aligned address of a big enough bitmap (one bit per page)
523 int kvm_get_mem_map(kvm_context_t kvm
, unsigned long phys_addr
, void *bitmap
);
524 int kvm_get_mem_map_range(kvm_context_t kvm
, unsigned long phys_addr
,
525 unsigned long len
, void *buf
, void *opaque
,
526 int (*cb
)(unsigned long start
,unsigned long len
,
527 void* bitmap
, void* opaque
));
528 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
, int *status
);
530 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm
,
533 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm
,
537 * \brief Enable dirty-pages-logging for all memory regions
539 * \param kvm Pointer to the current kvm_context
541 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm
);
544 * \brief Disable dirty-page-logging for some memory regions
546 * Disable dirty-pages-logging for those memory regions that were
547 * created with dirty-page-logging disabled.
549 * \param kvm Pointer to the current kvm_context
551 int kvm_dirty_pages_log_reset(kvm_context_t kvm
);
554 * \brief Query whether in kernel irqchip is used
556 * \param kvm Pointer to the current kvm_context
558 int kvm_irqchip_in_kernel(kvm_context_t kvm
);
560 #ifdef KVM_CAP_IRQCHIP
562 * \brief Dump in kernel IRQCHIP contents
564 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
565 * and IOAPIC into a kvm_irqchip structure
567 * \param kvm Pointer to the current kvm_context
568 * \param chip The irq chip device to be dumped
570 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
);
573 * \brief Set in kernel IRQCHIP contents
575 * Write one of the in kernel irq chip devices, including PIC (master/slave)
579 * \param kvm Pointer to the current kvm_context
580 * \param chip THe irq chip device to be written
582 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
);
584 #if defined(__i386__) || defined(__x86_64__)
586 * \brief Get in kernel local APIC for vcpu
588 * Save the local apic state including the timer of a virtual CPU
590 * \param kvm Pointer to the current kvm_context
591 * \param vcpu Which virtual CPU should be accessed
592 * \param s Local apic state of the specific virtual CPU
594 int kvm_get_lapic(kvm_vcpu_context_t vcpu
, struct kvm_lapic_state
*s
);
597 * \brief Set in kernel local APIC for vcpu
599 * Restore the local apic state including the timer of a virtual CPU
601 * \param kvm Pointer to the current kvm_context
602 * \param vcpu Which virtual CPU should be accessed
603 * \param s Local apic state of the specific virtual CPU
605 int kvm_set_lapic(kvm_vcpu_context_t vcpu
, struct kvm_lapic_state
*s
);
610 * \brief Simulate an NMI
612 * This allows you to simulate a non-maskable interrupt.
614 * \param kvm Pointer to the current kvm_context
615 * \param vcpu Which virtual CPU should get dumped
616 * \return 0 on success
618 int kvm_inject_nmi(kvm_vcpu_context_t vcpu
);
623 * \brief Query wheather in kernel pit is used
625 * \param kvm Pointer to the current kvm_context
627 int kvm_pit_in_kernel(kvm_context_t kvm
);
630 * \brief Initialize coalesced MMIO
632 * Check for coalesced MMIO capability and store in context
634 * \param kvm Pointer to the current kvm_context
636 int kvm_init_coalesced_mmio(kvm_context_t kvm
);
640 #if defined(__i386__) || defined(__x86_64__)
642 * \brief Get in kernel PIT of the virtual domain
644 * Save the PIT state.
646 * \param kvm Pointer to the current kvm_context
647 * \param s PIT state of the virtual domain
649 int kvm_get_pit(kvm_context_t kvm
, struct kvm_pit_state
*s
);
652 * \brief Set in kernel PIT of the virtual domain
654 * Restore the PIT state.
655 * Timer would be retriggerred after restored.
657 * \param kvm Pointer to the current kvm_context
658 * \param s PIT state of the virtual domain
660 int kvm_set_pit(kvm_context_t kvm
, struct kvm_pit_state
*s
);
662 int kvm_reinject_control(kvm_context_t kvm
, int pit_reinject
);
664 #ifdef KVM_CAP_PIT_STATE2
666 * \brief Check for kvm support of kvm_pit_state2
668 * \param kvm Pointer to the current kvm_context
669 * \return 0 on success
671 int kvm_has_pit_state2(kvm_context_t kvm
);
674 * \brief Set in kernel PIT state2 of the virtual domain
677 * \param kvm Pointer to the current kvm_context
678 * \param ps2 PIT state2 of the virtual domain
679 * \return 0 on success
681 int kvm_set_pit2(kvm_context_t kvm
, struct kvm_pit_state2
*ps2
);
684 * \brief Get in kernel PIT state2 of the virtual domain
687 * \param kvm Pointer to the current kvm_context
688 * \param ps2 PIT state2 of the virtual domain
689 * \return 0 on success
691 int kvm_get_pit2(kvm_context_t kvm
, struct kvm_pit_state2
*ps2
);
700 * \brief Enable kernel tpr access reporting
702 * When tpr access reporting is enabled, the kernel will call the
703 * ->tpr_access() callback every time the guest vcpu accesses the tpr.
705 * \param kvm Pointer to the current kvm_context
706 * \param vcpu vcpu to enable tpr access reporting on
708 int kvm_enable_tpr_access_reporting(kvm_vcpu_context_t vcpu
);
711 * \brief Disable kernel tpr access reporting
713 * Undoes the effect of kvm_enable_tpr_access_reporting().
715 * \param kvm Pointer to the current kvm_context
716 * \param vcpu vcpu to disable tpr access reporting on
718 int kvm_disable_tpr_access_reporting(kvm_vcpu_context_t vcpu
);
720 int kvm_enable_vapic(kvm_vcpu_context_t vcpu
, uint64_t vapic
);
724 #if defined(__s390__)
725 int kvm_s390_initial_reset(kvm_context_t kvm
, int slot
);
726 int kvm_s390_interrupt(kvm_context_t kvm
, int slot
,
727 struct kvm_s390_interrupt
*kvmint
);
728 int kvm_s390_set_initial_psw(kvm_context_t kvm
, int slot
, psw_t psw
);
729 int kvm_s390_store_status(kvm_context_t kvm
, int slot
, unsigned long addr
);
732 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
734 * \brief Notifies host kernel about a PCI device to be assigned to a guest
736 * Used for PCI device assignment, this function notifies the host
737 * kernel about the assigning of the physical PCI device to a guest.
739 * \param kvm Pointer to the current kvm_context
740 * \param assigned_dev Parameters, like bus, devfn number, etc
742 int kvm_assign_pci_device(kvm_context_t kvm
,
743 struct kvm_assigned_pci_dev
*assigned_dev
);
746 * \brief Assign IRQ for an assigned device
748 * Used for PCI device assignment, this function assigns IRQ numbers for
749 * an physical device and guest IRQ handling.
751 * \param kvm Pointer to the current kvm_context
752 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
754 int kvm_assign_irq(kvm_context_t kvm
,
755 struct kvm_assigned_irq
*assigned_irq
);
757 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
759 * \brief Deassign IRQ for an assigned device
761 * Used for PCI device assignment, this function deassigns IRQ numbers
762 * for an assigned device.
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_deassign_irq(kvm_context_t kvm
,
768 struct kvm_assigned_irq
*assigned_irq
);
773 * \brief Determines whether destroying memory regions is allowed
775 * KVM before 2.6.29 had a bug when destroying memory regions.
777 * \param kvm Pointer to the current kvm_context
779 int kvm_destroy_memory_region_works(kvm_context_t kvm
);
781 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
783 * \brief Notifies host kernel about a PCI device to be deassigned from a guest
785 * Used for hot remove PCI device, this function notifies the host
786 * kernel about the deassigning of the physical PCI device from a guest.
788 * \param kvm Pointer to the current kvm_context
789 * \param assigned_dev Parameters, like bus, devfn number, etc
791 int kvm_deassign_pci_device(kvm_context_t kvm
,
792 struct kvm_assigned_pci_dev
*assigned_dev
);
796 * \brief Checks whether the generic irq routing capability is present
798 * Checks whether kvm can reroute interrupts among the various interrupt
801 * \param kvm Pointer to the current kvm_context
803 int kvm_has_gsi_routing(kvm_context_t kvm
);
806 * \brief Determines the number of gsis that can be routed
808 * Returns the number of distinct gsis that can be routed by kvm. This is
809 * also the number of distinct routes (if a gsi has two routes, than another
810 * gsi cannot be used...)
812 * \param kvm Pointer to the current kvm_context
814 int kvm_get_gsi_count(kvm_context_t kvm
);
817 * \brief Clears the temporary irq routing table
819 * Clears the temporary irq routing table. Nothing is committed to the
822 * \param kvm Pointer to the current kvm_context
824 int kvm_clear_gsi_routes(kvm_context_t kvm
);
827 * \brief Adds an irq route to the temporary irq routing table
829 * Adds an irq route to the temporary irq routing table. Nothing is
830 * committed to the running VM.
832 * \param kvm Pointer to the current kvm_context
834 int kvm_add_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
);
837 * \brief Removes an irq route from the temporary irq routing table
839 * Adds an irq route to the temporary irq routing table. Nothing is
840 * committed to the running VM.
842 * \param kvm Pointer to the current kvm_context
844 int kvm_del_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
);
846 struct kvm_irq_routing_entry
;
848 * \brief Adds a routing entry to the temporary irq routing table
850 * Adds a filled routing entry to the temporary irq routing table. Nothing is
851 * committed to the running VM.
853 * \param kvm Pointer to the current kvm_context
855 int kvm_add_routing_entry(kvm_context_t kvm
,
856 struct kvm_irq_routing_entry
* entry
);
859 * \brief Removes a routing from the temporary irq routing table
861 * Remove a routing to the temporary irq routing table. Nothing is
862 * committed to the running VM.
864 * \param kvm Pointer to the current kvm_context
866 int kvm_del_routing_entry(kvm_context_t kvm
,
867 struct kvm_irq_routing_entry
* entry
);
870 * \brief Updates a routing in the temporary irq routing table
872 * Update a routing in the temporary irq routing table
873 * with a new value. entry type and GSI can not be changed.
874 * Nothing is committed to the running VM.
876 * \param kvm Pointer to the current kvm_context
878 int kvm_update_routing_entry(kvm_context_t kvm
,
879 struct kvm_irq_routing_entry
* entry
,
880 struct kvm_irq_routing_entry
* newentry
884 * \brief Commit the temporary irq routing table
886 * Commit the temporary irq routing table to the running VM.
888 * \param kvm Pointer to the current kvm_context
890 int kvm_commit_irq_routes(kvm_context_t kvm
);
893 * \brief Get unused GSI number for irq routing table
895 * Get unused GSI number for irq routing table
897 * \param kvm Pointer to the current kvm_context
899 int kvm_get_irq_route_gsi(kvm_context_t kvm
);
902 * \brief Create a file descriptor for injecting interrupts
904 * Creates an eventfd based file-descriptor that maps to a specific GSI
905 * in the guest. eventfd compliant signaling (write() from userspace, or
906 * eventfd_signal() from kernelspace) will cause the GSI to inject
907 * itself into the guest at the next available window.
909 * \param kvm Pointer to the current kvm_context
910 * \param gsi GSI to assign to this fd
911 * \param flags reserved, must be zero
913 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
);
915 #ifdef KVM_CAP_DEVICE_MSIX
916 int kvm_assign_set_msix_nr(kvm_context_t kvm
,
917 struct kvm_assigned_msix_nr
*msix_nr
);
918 int kvm_assign_set_msix_entry(kvm_context_t kvm
,
919 struct kvm_assigned_msix_entry
*entry
);
922 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm
, uint32_t function
, int reg
);
924 #else /* !CONFIG_KVM */
926 struct kvm_pit_state
{ };
928 #endif /* !CONFIG_KVM */
931 int kvm_main_loop(void);
932 int kvm_qemu_init(void);
933 int kvm_qemu_create_context(void);
934 int kvm_init_ap(void);
935 int kvm_vcpu_inited(CPUState
*env
);
936 void kvm_load_registers(CPUState
*env
);
937 void kvm_save_registers(CPUState
*env
);
938 void kvm_load_mpstate(CPUState
*env
);
939 void kvm_save_mpstate(CPUState
*env
);
940 int kvm_cpu_exec(CPUState
*env
);
941 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
942 target_ulong len
, int type
);
943 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
944 target_ulong len
, int type
);
945 void kvm_remove_all_breakpoints(CPUState
*current_env
);
946 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
);
947 int kvm_qemu_init_env(CPUState
*env
);
948 int kvm_qemu_check_extension(int ext
);
949 void kvm_apic_init(CPUState
*env
);
950 /* called from vcpu initialization */
951 void qemu_kvm_load_lapic(CPUState
*env
);
953 void kvm_hpet_enable_kpit(void);
954 void kvm_hpet_disable_kpit(void);
955 int kvm_set_irq(int irq
, int level
, int *status
);
957 int kvm_physical_memory_set_dirty_tracking(int enable
);
958 int kvm_update_dirty_pages_log(void);
959 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
);
961 void qemu_kvm_call_with_env(void (*func
)(void *), void *data
, CPUState
*env
);
962 void qemu_kvm_cpuid_on_env(CPUState
*env
);
963 void kvm_inject_interrupt(CPUState
*env
, int mask
);
964 void kvm_update_after_sipi(CPUState
*env
);
965 void kvm_update_interrupt_request(CPUState
*env
);
966 void kvm_set_phys_mem(target_phys_addr_t start_addr
, ram_addr_t size
,
967 ram_addr_t phys_offset
);
968 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
,
969 unsigned long size
, int log
, int writable
);
971 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
973 void kvm_qemu_log_memory(target_phys_addr_t start
, target_phys_addr_t size
,
975 int kvm_setup_guest_memory(void *area
, unsigned long size
);
976 int kvm_qemu_create_memory_alias(uint64_t phys_start
,
978 uint64_t target_phys
);
979 int kvm_qemu_destroy_memory_alias(uint64_t phys_start
);
981 int kvm_arch_qemu_create_context(void);
983 void kvm_arch_save_regs(CPUState
*env
);
984 void kvm_arch_load_regs(CPUState
*env
);
985 void kvm_arch_load_mpstate(CPUState
*env
);
986 void kvm_arch_save_mpstate(CPUState
*env
);
987 int kvm_arch_qemu_init_env(CPUState
*cenv
);
988 void kvm_arch_pre_kvm_run(void *opaque
, CPUState
*env
);
989 void kvm_arch_post_kvm_run(void *opaque
, CPUState
*env
);
990 int kvm_arch_has_work(CPUState
*env
);
991 void kvm_arch_process_irqchip_events(CPUState
*env
);
992 int kvm_arch_try_push_interrupts(void *opaque
);
993 void kvm_arch_push_nmi(void *opaque
);
994 void kvm_arch_update_regs_for_sipi(CPUState
*env
);
995 void kvm_arch_cpu_reset(CPUState
*env
);
996 int kvm_set_boot_cpu_id(uint32_t id
);
998 struct kvm_guest_debug
;
999 struct kvm_debug_exit_arch
;
1001 struct kvm_sw_breakpoint
{
1003 target_ulong saved_insn
;
1005 TAILQ_ENTRY(kvm_sw_breakpoint
) entry
;
1008 TAILQ_HEAD(kvm_sw_breakpoint_head
, kvm_sw_breakpoint
);
1010 int kvm_arch_debug(struct kvm_debug_exit_arch
*arch_info
);
1011 int kvm_sw_breakpoints_active(CPUState
*env
);
1012 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUState
*env
, target_ulong pc
);
1013 int kvm_arch_insert_sw_breakpoint(CPUState
*current_env
,
1014 struct kvm_sw_breakpoint
*bp
);
1015 int kvm_arch_remove_sw_breakpoint(CPUState
*current_env
,
1016 struct kvm_sw_breakpoint
*bp
);
1017 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1018 target_ulong len
, int type
);
1019 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
1020 target_ulong len
, int type
);
1021 void kvm_arch_remove_all_hw_breakpoints(void);
1022 void kvm_arch_update_guest_debug(CPUState
*env
, struct kvm_guest_debug
*dbg
);
1024 void qemu_kvm_aio_wait_start(void);
1025 void qemu_kvm_aio_wait(void);
1026 void qemu_kvm_aio_wait_end(void);
1028 void qemu_kvm_notify_work(void);
1030 void kvm_tpr_opt_setup(void);
1031 void kvm_tpr_access_report(CPUState
*env
, uint64_t rip
, int is_write
);
1032 void kvm_tpr_vcpu_start(CPUState
*env
);
1034 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
);
1035 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
);
1036 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
);
1038 int kvm_arch_init_irq_routing(void);
1040 int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
);
1041 int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
);
1043 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1046 void kvm_ioperm(CPUState
*env
, void *data
);
1047 void kvm_add_ioperm_data(struct ioperm_data
*data
);
1048 void kvm_remove_ioperm_data(unsigned long start_port
, unsigned long num
);
1049 void kvm_arch_do_ioperm(void *_data
);
1052 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
1053 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
1056 #include "sys-queue.h"
1058 extern int kvm_allowed
;
1059 extern int kvm_irqchip
;
1061 extern int kvm_pit_reinject
;
1062 extern int kvm_nested
;
1063 extern kvm_context_t kvm_context
;
1065 struct ioperm_data
{
1066 unsigned long start_port
;
1069 LIST_ENTRY(ioperm_data
) entries
;
1072 void qemu_kvm_cpu_stop(CPUState
*env
);
1073 int kvm_arch_halt(void *opaque
, kvm_vcpu_context_t vcpu
);
1074 int handle_tpr_access(void *opaque
, kvm_vcpu_context_t vcpu
,
1075 uint64_t rip
, int is_write
);
1076 int kvm_has_sync_mmu(void);
1078 #define kvm_enabled() (kvm_allowed)
1079 #define qemu_kvm_irqchip_in_kernel() kvm_irqchip_in_kernel(kvm_context)
1080 #define qemu_kvm_pit_in_kernel() kvm_pit_in_kernel(kvm_context)
1081 #define qemu_kvm_has_gsi_routing() kvm_has_gsi_routing(kvm_context)
1083 #define qemu_kvm_has_pit_state2() kvm_has_pit_state2(kvm_context)
1085 void kvm_init_vcpu(CPUState
*env
);
1086 void kvm_load_tsc(CPUState
*env
);
1088 #define kvm_has_sync_mmu() (0)
1089 #define kvm_enabled() (0)
1090 #define kvm_nested 0
1091 #define qemu_kvm_irqchip_in_kernel() (0)
1092 #define qemu_kvm_pit_in_kernel() (0)
1093 #define qemu_kvm_has_gsi_routing() (0)
1095 #define qemu_kvm_has_pit_state2() (0)
1097 #define kvm_load_registers(env) do {} while(0)
1098 #define kvm_save_registers(env) do {} while(0)
1099 #define qemu_kvm_cpu_stop(env) do {} while(0)
1100 static inline void kvm_init_vcpu(CPUState
*env
) { }
1101 static inline void kvm_load_tsc(CPUState
*env
) {}
1104 void kvm_mutex_unlock(void);
1105 void kvm_mutex_lock(void);
1107 static inline void kvm_sleep_begin(void)
1113 static inline void kvm_sleep_end(void)
1119 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
, target_phys_addr_t end_addr
);
1121 int kvm_log_start(target_phys_addr_t phys_addr
, target_phys_addr_t len
);
1122 int kvm_log_stop(target_phys_addr_t phys_addr
, target_phys_addr_t len
);
1125 static inline int kvm_sync_vcpus(void) { return 0; }
1127 static inline void kvm_arch_get_registers(CPUState
*env
)
1129 kvm_save_registers(env
);
1130 kvm_save_mpstate(env
);
1133 static inline void kvm_arch_put_registers(CPUState
*env
)
1135 kvm_load_registers(env
);
1136 kvm_load_mpstate(env
);
1139 static inline void cpu_synchronize_state(CPUState
*env
, int modified
)
1141 if (kvm_enabled()) {
1143 kvm_arch_put_registers(env
);
1145 kvm_arch_get_registers(env
);
1149 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
,
1153 static inline int kvm_set_migration_log(int enable
)
1155 return kvm_physical_memory_set_dirty_tracking(enable
);
1158 typedef struct KVMSlot
1160 target_phys_addr_t start_addr
;
1161 ram_addr_t memory_size
;
1162 ram_addr_t phys_offset
;
1167 typedef struct kvm_dirty_log KVMDirtyLog
;
1169 typedef struct KVMState
1175 int broken_set_mem_region
;
1177 #ifdef KVM_CAP_SET_GUEST_DEBUG
1178 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
;
1180 struct kvm_context kvm_context
;