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
19 #include <asm/ptrace.h>
25 #define __user /* temporary, until installed via make headers_install */
28 #include <linux/kvm.h>
32 /* FIXME: share this number with kvm */
33 /* FIXME: or dynamically alloc/realloc regions */
35 #define KVM_MAX_NUM_MEM_REGIONS 1u
37 #define LIBKVM_S390_ORIGIN (0UL)
38 #elif defined(__ia64__)
39 #define KVM_MAX_NUM_MEM_REGIONS 32u
42 #define KVM_MAX_NUM_MEM_REGIONS 32u
46 /* kvm abi verison variable */
50 * \brief The KVM context
52 * The verbose KVM context
57 /// is dirty pages logging enabled for all regions or not
58 int dirty_pages_log_all
;
59 /// do not create in-kernel irqchip if set
60 int no_irqchip_creation
;
61 /// in-kernel irqchip status
62 int irqchip_in_kernel
;
63 /// ioctl to use to inject interrupts
64 int irqchip_inject_ioctl
;
65 /// do not create in-kernel pit if set
67 #ifdef KVM_CAP_IRQ_ROUTING
68 struct kvm_irq_routing
*irq_routes
;
69 int nr_allocated_irq_routes
;
71 void *used_gsi_bitmap
;
75 typedef struct kvm_context
*kvm_context_t
;
78 int kvm_alloc_kernel_memory(kvm_context_t kvm
, unsigned long memory
,
80 int kvm_alloc_userspace_memory(kvm_context_t kvm
, unsigned long memory
,
83 int kvm_arch_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
,
86 int kvm_arch_run(CPUState
*env
);
89 void kvm_show_code(CPUState
*env
);
91 int handle_halt(CPUState
*env
);
93 int handle_shutdown(kvm_context_t kvm
, CPUState
*env
);
94 void post_kvm_run(kvm_context_t kvm
, CPUState
*env
);
95 int pre_kvm_run(kvm_context_t kvm
, CPUState
*env
);
96 int handle_io_window(kvm_context_t kvm
);
97 int try_push_interrupts(kvm_context_t kvm
);
99 #if defined(__x86_64__) || defined(__i386__)
104 * \brief Disable the in-kernel IRQCHIP creation
106 * In-kernel irqchip is enabled by default. If userspace irqchip is to be used,
107 * this should be called prior to kvm_create().
109 * \param kvm Pointer to the kvm_context
111 void kvm_disable_irqchip_creation(kvm_context_t kvm
);
114 * \brief Disable the in-kernel PIT creation
116 * In-kernel pit is enabled by default. If userspace pit is to be used,
117 * this should be called prior to kvm_create().
119 * \param kvm Pointer to the kvm_context
121 void kvm_disable_pit_creation(kvm_context_t kvm
);
124 * \brief Create new virtual machine
126 * This creates a new virtual machine, maps physical RAM to it, and creates a
127 * virtual CPU for it.\n
129 * Memory gets mapped for addresses 0->0xA0000, 0xC0000->phys_mem_bytes
131 * \param kvm Pointer to the current kvm_context
132 * \param phys_mem_bytes The amount of physical ram you want the VM to have
133 * \param phys_mem This pointer will be set to point to the memory that
134 * kvm_create allocates for physical RAM
135 * \return 0 on success
137 int kvm_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
,
139 int kvm_create_vm(kvm_context_t kvm
);
140 void kvm_create_irqchip(kvm_context_t kvm
);
143 * \brief Start the VCPU
145 * This starts the VCPU and virtualization is started.\n
147 * This function will not return until any of these conditions are met:
148 * - An IO/MMIO handler does not return "0"
149 * - An exception that neither the guest OS, nor KVM can handle occurs
151 * \note This function will call the callbacks registered in kvm_init()
152 * to emulate those functions
153 * \note If you at any point want to interrupt the VCPU, kvm_run() will
154 * listen to the EINTR signal. This allows you to simulate external interrupts
155 * and asyncronous IO.
157 * \param kvm Pointer to the current kvm_context
158 * \param vcpu Which virtual CPU should be started
159 * \return 0 on success, but you really shouldn't expect this function to
160 * return except for when an error has occured, or when you have sent it
163 int kvm_run(CPUState
*env
);
166 * \brief Check if a vcpu is ready for interrupt injection
168 * This checks if vcpu interrupts are not masked by mov ss or sti.
170 * \param kvm Pointer to the current kvm_context
171 * \param vcpu Which virtual CPU should get dumped
172 * \return boolean indicating interrupt injection readiness
174 int kvm_is_ready_for_interrupt_injection(CPUState
*env
);
177 * \brief Read VCPU registers
179 * This gets the GP registers from the VCPU and outputs them
180 * into a kvm_regs structure
182 * \note This function returns a \b copy of the VCPUs registers.\n
183 * If you wish to modify the VCPUs GP registers, you should call kvm_set_regs()
185 * \param kvm Pointer to the current kvm_context
186 * \param vcpu Which virtual CPU should get dumped
187 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
189 * \return 0 on success
191 int kvm_get_regs(CPUState
*env
, struct kvm_regs
*regs
);
194 * \brief Write VCPU registers
196 * This sets the GP registers on the VCPU from a kvm_regs structure
198 * \note When this function returns, the regs pointer and the data it points to
200 * \param kvm Pointer to the current kvm_context
201 * \param vcpu Which virtual CPU should get dumped
202 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
204 * \return 0 on success
206 int kvm_set_regs(CPUState
*env
, struct kvm_regs
*regs
);
208 #ifdef KVM_CAP_MP_STATE
210 * * \brief Read VCPU MP state
213 int kvm_get_mpstate(CPUState
*env
, struct kvm_mp_state
*mp_state
);
216 * * \brief Write VCPU MP state
219 int kvm_set_mpstate(CPUState
*env
, struct kvm_mp_state
*mp_state
);
222 #if defined(__i386__) || defined(__x86_64__)
224 * \brief Simulate an external vectored interrupt
226 * This allows you to simulate an external vectored interrupt.
228 * \param kvm Pointer to the current kvm_context
229 * \param vcpu Which virtual CPU should get dumped
230 * \param irq Vector number
231 * \return 0 on success
233 int kvm_inject_irq(CPUState
*env
, unsigned irq
);
237 * \brief Setting the number of shadow pages to be allocated to the vm
239 * \param kvm pointer to kvm_context
240 * \param nrshadow_pages number of pages to be allocated
242 int kvm_set_shadow_pages(kvm_context_t kvm
, unsigned int nrshadow_pages
);
245 * \brief Getting the number of shadow pages that are allocated to the vm
247 * \param kvm pointer to kvm_context
248 * \param nrshadow_pages number of pages to be allocated
250 int kvm_get_shadow_pages(kvm_context_t kvm
, unsigned int *nrshadow_pages
);
255 * \brief Dump VCPU registers
257 * This dumps some of the information that KVM has about a virtual CPU, namely:
260 * A much more verbose version of this is available as kvm_dump_vcpu()
262 * \param kvm Pointer to the current kvm_context
263 * \param vcpu Which virtual CPU should get dumped
264 * \return 0 on success
266 void kvm_show_regs(CPUState
*env
);
269 void *kvm_create_phys_mem(kvm_context_t
, unsigned long phys_start
,
270 unsigned long len
, int log
, int writable
);
271 void kvm_destroy_phys_mem(kvm_context_t
, unsigned long phys_start
,
274 int kvm_is_containing_region(kvm_context_t kvm
, unsigned long phys_start
,
276 int kvm_register_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
277 void *userspace_addr
, unsigned long len
, int log
);
278 int kvm_get_dirty_pages_range(kvm_context_t kvm
, unsigned long phys_addr
,
279 unsigned long end_addr
, void *opaque
,
280 int (*cb
)(unsigned long start
,
281 unsigned long len
, void *bitmap
,
283 int kvm_register_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
,
285 int kvm_unregister_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
,
289 * \brief Get a bitmap of guest ram pages which are allocated to the guest.
291 * \param kvm Pointer to the current kvm_context
292 * \param phys_addr Memory slot phys addr
293 * \param bitmap Long aligned address of a big enough bitmap (one bit per page)
295 int kvm_get_mem_map(kvm_context_t kvm
, unsigned long phys_addr
, void *bitmap
);
296 int kvm_get_mem_map_range(kvm_context_t kvm
, unsigned long phys_addr
,
297 unsigned long len
, void *buf
, void *opaque
,
298 int (*cb
)(unsigned long start
,
299 unsigned long len
, void *bitmap
,
301 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
, int *status
);
303 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm
, uint64_t phys_start
,
305 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm
, uint64_t phys_start
,
308 * \brief Enable dirty-pages-logging for all memory regions
310 * \param kvm Pointer to the current kvm_context
312 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm
);
315 * \brief Disable dirty-page-logging for some memory regions
317 * Disable dirty-pages-logging for those memory regions that were
318 * created with dirty-page-logging disabled.
320 * \param kvm Pointer to the current kvm_context
322 int kvm_dirty_pages_log_reset(kvm_context_t kvm
);
324 #ifdef KVM_CAP_IRQCHIP
326 * \brief Dump in kernel IRQCHIP contents
328 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
329 * and IOAPIC into a kvm_irqchip structure
331 * \param kvm Pointer to the current kvm_context
332 * \param chip The irq chip device to be dumped
334 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
);
337 * \brief Set in kernel IRQCHIP contents
339 * Write one of the in kernel irq chip devices, including PIC (master/slave)
343 * \param kvm Pointer to the current kvm_context
344 * \param chip THe irq chip device to be written
346 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
);
348 #if defined(__i386__) || defined(__x86_64__)
350 * \brief Get in kernel local APIC for vcpu
352 * Save the local apic state including the timer of a virtual CPU
354 * \param kvm Pointer to the current kvm_context
355 * \param vcpu Which virtual CPU should be accessed
356 * \param s Local apic state of the specific virtual CPU
358 int kvm_get_lapic(CPUState
*env
, struct kvm_lapic_state
*s
);
361 * \brief Set in kernel local APIC for vcpu
363 * Restore the local apic state including the timer of a virtual CPU
365 * \param kvm Pointer to the current kvm_context
366 * \param vcpu Which virtual CPU should be accessed
367 * \param s Local apic state of the specific virtual CPU
369 int kvm_set_lapic(CPUState
*env
, struct kvm_lapic_state
*s
);
374 * \brief Simulate an NMI
376 * This allows you to simulate a non-maskable interrupt.
378 * \param kvm Pointer to the current kvm_context
379 * \param vcpu Which virtual CPU should get dumped
380 * \return 0 on success
382 int kvm_inject_nmi(CPUState
*env
);
387 * \brief Initialize coalesced MMIO
389 * Check for coalesced MMIO capability and store in context
391 * \param kvm Pointer to the current kvm_context
393 int kvm_init_coalesced_mmio(kvm_context_t kvm
);
397 #if defined(__i386__) || defined(__x86_64__)
399 * \brief Get in kernel PIT of the virtual domain
401 * Save the PIT state.
403 * \param kvm Pointer to the current kvm_context
404 * \param s PIT state of the virtual domain
406 int kvm_get_pit(kvm_context_t kvm
, struct kvm_pit_state
*s
);
409 * \brief Set in kernel PIT of the virtual domain
411 * Restore the PIT state.
412 * Timer would be retriggerred after restored.
414 * \param kvm Pointer to the current kvm_context
415 * \param s PIT state of the virtual domain
417 int kvm_set_pit(kvm_context_t kvm
, struct kvm_pit_state
*s
);
419 int kvm_reinject_control(kvm_context_t kvm
, int pit_reinject
);
421 #ifdef KVM_CAP_PIT_STATE2
423 * \brief Check for kvm support of kvm_pit_state2
425 * \param kvm Pointer to the current kvm_context
426 * \return 0 on success
428 int kvm_has_pit_state2(kvm_context_t kvm
);
431 * \brief Set in kernel PIT state2 of the virtual domain
434 * \param kvm Pointer to the current kvm_context
435 * \param ps2 PIT state2 of the virtual domain
436 * \return 0 on success
438 int kvm_set_pit2(kvm_context_t kvm
, struct kvm_pit_state2
*ps2
);
441 * \brief Get in kernel PIT state2 of the virtual domain
444 * \param kvm Pointer to the current kvm_context
445 * \param ps2 PIT state2 of the virtual domain
446 * \return 0 on success
448 int kvm_get_pit2(kvm_context_t kvm
, struct kvm_pit_state2
*ps2
);
456 int kvm_enable_vapic(CPUState
*env
, uint64_t vapic
);
460 #if defined(__s390__)
461 int kvm_s390_initial_reset(kvm_context_t kvm
, int slot
);
462 int kvm_s390_interrupt(kvm_context_t kvm
, int slot
,
463 struct kvm_s390_interrupt
*kvmint
);
464 int kvm_s390_set_initial_psw(kvm_context_t kvm
, int slot
, psw_t psw
);
465 int kvm_s390_store_status(kvm_context_t kvm
, int slot
, unsigned long addr
);
468 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
470 * \brief Notifies host kernel about a PCI device to be assigned to a guest
472 * Used for PCI device assignment, this function notifies the host
473 * kernel about the assigning of the physical PCI device to a guest.
475 * \param kvm Pointer to the current kvm_context
476 * \param assigned_dev Parameters, like bus, devfn number, etc
478 int kvm_assign_pci_device(kvm_context_t kvm
,
479 struct kvm_assigned_pci_dev
*assigned_dev
);
482 * \brief Assign IRQ for an assigned device
484 * Used for PCI device assignment, this function assigns IRQ numbers for
485 * an physical device and guest IRQ handling.
487 * \param kvm Pointer to the current kvm_context
488 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
490 int kvm_assign_irq(kvm_context_t kvm
, struct kvm_assigned_irq
*assigned_irq
);
492 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
494 * \brief Deassign IRQ for an assigned device
496 * Used for PCI device assignment, this function deassigns IRQ numbers
497 * for an assigned device.
499 * \param kvm Pointer to the current kvm_context
500 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
502 int kvm_deassign_irq(kvm_context_t kvm
, struct kvm_assigned_irq
*assigned_irq
);
506 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
508 * \brief Notifies host kernel about a PCI device to be deassigned from a guest
510 * Used for hot remove PCI device, this function notifies the host
511 * kernel about the deassigning of the physical PCI device from a guest.
513 * \param kvm Pointer to the current kvm_context
514 * \param assigned_dev Parameters, like bus, devfn number, etc
516 int kvm_deassign_pci_device(kvm_context_t kvm
,
517 struct kvm_assigned_pci_dev
*assigned_dev
);
521 * \brief Determines the number of gsis that can be routed
523 * Returns the number of distinct gsis that can be routed by kvm. This is
524 * also the number of distinct routes (if a gsi has two routes, than another
525 * gsi cannot be used...)
527 * \param kvm Pointer to the current kvm_context
529 int kvm_get_gsi_count(kvm_context_t kvm
);
532 * \brief Clears the temporary irq routing table
534 * Clears the temporary irq routing table. Nothing is committed to the
538 int kvm_clear_gsi_routes(void);
541 * \brief Adds an irq route to the temporary irq routing table
543 * Adds an irq route to the temporary irq routing table. Nothing is
544 * committed to the running VM.
546 int kvm_add_irq_route(int gsi
, int irqchip
, int pin
);
549 * \brief Removes an irq route from the temporary irq routing table
551 * Adds an irq route to the temporary irq routing table. Nothing is
552 * committed to the running VM.
554 int kvm_del_irq_route(int gsi
, int irqchip
, int pin
);
556 struct kvm_irq_routing_entry
;
558 * \brief Adds a routing entry to the temporary irq routing table
560 * Adds a filled routing entry to the temporary irq routing table. Nothing is
561 * committed to the running VM.
563 int kvm_add_routing_entry(struct kvm_irq_routing_entry
*entry
);
566 * \brief Removes a routing from the temporary irq routing table
568 * Remove a routing to the temporary irq routing table. Nothing is
569 * committed to the running VM.
571 int kvm_del_routing_entry(struct kvm_irq_routing_entry
*entry
);
574 * \brief Updates a routing in the temporary irq routing table
576 * Update a routing in the temporary irq routing table
577 * with a new value. entry type and GSI can not be changed.
578 * Nothing is committed to the running VM.
580 int kvm_update_routing_entry(struct kvm_irq_routing_entry
*entry
,
581 struct kvm_irq_routing_entry
*newentry
);
585 * \brief Create a file descriptor for injecting interrupts
587 * Creates an eventfd based file-descriptor that maps to a specific GSI
588 * in the guest. eventfd compliant signaling (write() from userspace, or
589 * eventfd_signal() from kernelspace) will cause the GSI to inject
590 * itself into the guest at the next available window.
592 * \param kvm Pointer to the current kvm_context
593 * \param gsi GSI to assign to this fd
594 * \param flags reserved, must be zero
596 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
);
598 #ifdef KVM_CAP_DEVICE_MSIX
599 int kvm_assign_set_msix_nr(kvm_context_t kvm
,
600 struct kvm_assigned_msix_nr
*msix_nr
);
601 int kvm_assign_set_msix_entry(kvm_context_t kvm
,
602 struct kvm_assigned_msix_entry
*entry
);
605 #else /* !CONFIG_KVM */
607 typedef struct kvm_context
*kvm_context_t
;
608 typedef struct kvm_vcpu_context
*kvm_vcpu_context_t
;
610 struct kvm_pit_state
{
613 #endif /* !CONFIG_KVM */
617 * \brief Create new KVM context
619 * This creates a new kvm_context. A KVM context is a small area of data that
620 * holds information about the KVM instance that gets created by this call.\n
621 * This should always be your first call to KVM.
623 * \param opaque Not used
624 * \return NULL on failure
626 int kvm_init(int smp_cpus
);
628 int kvm_main_loop(void);
629 int kvm_init_ap(void);
630 int kvm_vcpu_inited(CPUState
*env
);
631 void kvm_save_lapic(CPUState
*env
);
632 void kvm_load_lapic(CPUState
*env
);
634 void kvm_hpet_enable_kpit(void);
635 void kvm_hpet_disable_kpit(void);
637 int kvm_physical_memory_set_dirty_tracking(int enable
);
639 void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
);
640 void qemu_kvm_call_with_env(void (*func
)(void *), void *data
, CPUState
*env
);
641 void qemu_kvm_cpuid_on_env(CPUState
*env
);
642 void kvm_inject_interrupt(CPUState
*env
, int mask
);
643 void kvm_update_after_sipi(CPUState
*env
);
644 void kvm_update_interrupt_request(CPUState
*env
);
645 #ifndef CONFIG_USER_ONLY
646 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
, unsigned long size
,
647 int log
, int writable
);
649 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
651 void kvm_qemu_log_memory(target_phys_addr_t start
, target_phys_addr_t size
,
654 int kvm_qemu_create_memory_alias(uint64_t phys_start
, uint64_t len
,
655 uint64_t target_phys
);
656 int kvm_qemu_destroy_memory_alias(uint64_t phys_start
);
658 int kvm_arch_qemu_create_context(void);
660 void kvm_arch_save_regs(CPUState
*env
);
661 void kvm_arch_load_regs(CPUState
*env
, int level
);
662 int kvm_arch_has_work(CPUState
*env
);
663 void kvm_arch_process_irqchip_events(CPUState
*env
);
664 int kvm_arch_try_push_interrupts(void *opaque
);
665 void kvm_arch_push_nmi(void *opaque
);
666 void kvm_arch_cpu_reset(CPUState
*env
);
667 int kvm_set_boot_cpu_id(uint32_t id
);
669 void qemu_kvm_aio_wait_start(void);
670 void qemu_kvm_aio_wait(void);
671 void qemu_kvm_aio_wait_end(void);
673 void kvm_tpr_access_report(CPUState
*env
, uint64_t rip
, int is_write
);
675 int kvm_arch_init_irq_routing(void);
677 int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t * data
, int len
);
678 int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t * data
, int len
);
680 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
683 void kvm_ioperm(CPUState
*env
, void *data
);
684 void kvm_add_ioperm_data(struct ioperm_data
*data
);
685 void kvm_remove_ioperm_data(unsigned long start_port
, unsigned long num
);
686 void kvm_arch_do_ioperm(void *_data
);
689 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
690 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
693 #include "qemu-queue.h"
695 extern int kvm_irqchip
;
697 extern int kvm_pit_reinject
;
698 extern int kvm_nested
;
699 extern kvm_context_t kvm_context
;
702 unsigned long start_port
;
705 QLIST_ENTRY(ioperm_data
) entries
;
708 void qemu_kvm_cpu_stop(CPUState
*env
);
709 int kvm_arch_halt(CPUState
*env
);
710 int handle_tpr_access(void *opaque
, CPUState
*env
, uint64_t rip
,
713 #define qemu_kvm_has_gsi_routing() kvm_has_gsi_routing()
715 #define qemu_kvm_has_pit_state2() kvm_has_pit_state2(kvm_context)
719 #define qemu_kvm_has_gsi_routing() (0)
721 #define qemu_kvm_has_pit_state2() (0)
723 #define qemu_kvm_cpu_stop(env) do {} while(0)
728 typedef struct KVMSlot
{
729 target_phys_addr_t start_addr
;
730 ram_addr_t memory_size
;
731 ram_addr_t phys_offset
;
736 typedef struct kvm_dirty_log KVMDirtyLog
;
743 #ifdef KVM_CAP_COALESCED_MMIO
744 struct kvm_coalesced_mmio_ring
*coalesced_mmio_ring
;
746 int broken_set_mem_region
;
749 int robust_singlestep
;
751 #ifdef KVM_CAP_SET_GUEST_DEBUG
752 QTAILQ_HEAD(, kvm_sw_breakpoint
) kvm_sw_breakpoints
;
754 int irqchip_in_kernel
;
758 struct kvm_context kvm_context
;
761 extern struct KVMState
*kvm_state
;
763 int kvm_tpr_enable_vapic(CPUState
*env
);
765 unsigned long kvm_get_thread_id(void);
766 int kvm_cpu_is_stopped(CPUState
*env
);