Merge commit '70783b9c9be31e98421f17327a1127021abae672' into upstream-merge
[qemu-kvm/markmc.git] / qemu-kvm.h
blobf897c7c8d53b08850d8467e4cb038b7cae8d9ced
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 #ifndef QEMU_KVM_NO_CPU
12 #include "cpu.h"
13 #endif
15 #include <signal.h>
16 #include <stdlib.h>
18 #ifdef CONFIG_KVM
20 #if defined(__s390__)
21 #include <asm/ptrace.h>
22 #endif
24 #include <stdint.h>
26 #ifndef __user
27 #define __user /* temporary, until installed via make headers_install */
28 #endif
30 #include <linux/kvm.h>
32 #include <signal.h>
34 /* FIXME: share this number with kvm */
35 /* FIXME: or dynamically alloc/realloc regions */
36 #ifdef __s390__
37 #define KVM_MAX_NUM_MEM_REGIONS 1u
38 #define MAX_VCPUS 64
39 #define LIBKVM_S390_ORIGIN (0UL)
40 #elif defined(__ia64__)
41 #define KVM_MAX_NUM_MEM_REGIONS 32u
42 #define MAX_VCPUS 256
43 #else
44 #define KVM_MAX_NUM_MEM_REGIONS 32u
45 #define MAX_VCPUS 16
46 #endif
48 /* kvm abi verison variable */
49 extern int kvm_abi;
51 /**
52 * \brief The KVM context
54 * The verbose KVM context
57 struct kvm_context {
58 void *opaque;
59 /// is dirty pages logging enabled for all regions or not
60 int dirty_pages_log_all;
61 /// do not create in-kernel irqchip if set
62 int no_irqchip_creation;
63 /// in-kernel irqchip status
64 int irqchip_in_kernel;
65 /// ioctl to use to inject interrupts
66 int irqchip_inject_ioctl;
67 /// do not create in-kernel pit if set
68 int no_pit_creation;
69 /// in-kernel pit status
70 int pit_in_kernel;
71 #ifdef KVM_CAP_IRQ_ROUTING
72 struct kvm_irq_routing *irq_routes;
73 int nr_allocated_irq_routes;
74 #endif
75 void *used_gsi_bitmap;
76 int max_gsi;
79 typedef struct kvm_context *kvm_context_t;
81 #include "kvm.h"
82 int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
83 void **vm_mem);
84 int kvm_alloc_userspace_memory(kvm_context_t kvm, unsigned long memory,
85 void **vm_mem);
87 int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
88 void **vm_mem);
90 int kvm_arch_run(CPUState *env);
93 void kvm_show_code(CPUState *env);
95 int handle_halt(CPUState *env);
97 #ifndef QEMU_KVM_NO_CPU
99 int handle_shutdown(kvm_context_t kvm, CPUState *env);
100 void post_kvm_run(kvm_context_t kvm, CPUState *env);
101 int pre_kvm_run(kvm_context_t kvm, CPUState *env);
102 int handle_io_window(kvm_context_t kvm);
103 int try_push_interrupts(kvm_context_t kvm);
105 #if defined(__x86_64__) || defined(__i386__)
106 struct kvm_msr_list *kvm_get_msr_list(kvm_context_t);
107 int kvm_get_msrs(CPUState *env, struct kvm_msr_entry *msrs, int n);
108 int kvm_set_msrs(CPUState *env, struct kvm_msr_entry *msrs, int n);
109 int kvm_get_mce_cap_supported(kvm_context_t, uint64_t *mce_cap,
110 int *max_banks);
111 int kvm_setup_mce(CPUState *env, uint64_t *mcg_cap);
112 struct kvm_x86_mce;
113 int kvm_set_mce(CPUState *env, struct kvm_x86_mce *mce);
114 #endif
116 #endif
119 * \brief Create new KVM context
121 * This creates a new kvm_context. A KVM context is a small area of data that
122 * holds information about the KVM instance that gets created by this call.\n
123 * This should always be your first call to KVM.
125 * \param opaque Not used
126 * \return NULL on failure
128 int kvm_init(int smp_cpus);
131 * \brief Disable the in-kernel IRQCHIP creation
133 * In-kernel irqchip is enabled by default. If userspace irqchip is to be used,
134 * this should be called prior to kvm_create().
136 * \param kvm Pointer to the kvm_context
138 void kvm_disable_irqchip_creation(kvm_context_t kvm);
141 * \brief Disable the in-kernel PIT creation
143 * In-kernel pit is enabled by default. If userspace pit is to be used,
144 * this should be called prior to kvm_create().
146 * \param kvm Pointer to the kvm_context
148 void kvm_disable_pit_creation(kvm_context_t kvm);
151 * \brief Create new virtual machine
153 * This creates a new virtual machine, maps physical RAM to it, and creates a
154 * virtual CPU for it.\n
155 * \n
156 * Memory gets mapped for addresses 0->0xA0000, 0xC0000->phys_mem_bytes
158 * \param kvm Pointer to the current kvm_context
159 * \param phys_mem_bytes The amount of physical ram you want the VM to have
160 * \param phys_mem This pointer will be set to point to the memory that
161 * kvm_create allocates for physical RAM
162 * \return 0 on success
164 int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
165 void **phys_mem);
166 int kvm_create_vm(kvm_context_t kvm);
167 void kvm_create_irqchip(kvm_context_t kvm);
170 * \brief Start the VCPU
172 * This starts the VCPU and virtualization is started.\n
173 * \n
174 * This function will not return until any of these conditions are met:
175 * - An IO/MMIO handler does not return "0"
176 * - An exception that neither the guest OS, nor KVM can handle occurs
178 * \note This function will call the callbacks registered in kvm_init()
179 * to emulate those functions
180 * \note If you at any point want to interrupt the VCPU, kvm_run() will
181 * listen to the EINTR signal. This allows you to simulate external interrupts
182 * and asyncronous IO.
184 * \param kvm Pointer to the current kvm_context
185 * \param vcpu Which virtual CPU should be started
186 * \return 0 on success, but you really shouldn't expect this function to
187 * return except for when an error has occured, or when you have sent it
188 * an EINTR signal.
190 int kvm_run(CPUState *env);
193 * \brief Check if a vcpu is ready for interrupt injection
195 * This checks if vcpu interrupts are not masked by mov ss or sti.
197 * \param kvm Pointer to the current kvm_context
198 * \param vcpu Which virtual CPU should get dumped
199 * \return boolean indicating interrupt injection readiness
201 int kvm_is_ready_for_interrupt_injection(CPUState *env);
204 * \brief Read VCPU registers
206 * This gets the GP registers from the VCPU and outputs them
207 * into a kvm_regs structure
209 * \note This function returns a \b copy of the VCPUs registers.\n
210 * If you wish to modify the VCPUs GP registers, you should call kvm_set_regs()
212 * \param kvm Pointer to the current kvm_context
213 * \param vcpu Which virtual CPU should get dumped
214 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
215 * registers values
216 * \return 0 on success
218 int kvm_get_regs(CPUState *env, struct kvm_regs *regs);
221 * \brief Write VCPU registers
223 * This sets the GP registers on the VCPU from a kvm_regs structure
225 * \note When this function returns, the regs pointer and the data it points to
226 * can be discarded
227 * \param kvm Pointer to the current kvm_context
228 * \param vcpu Which virtual CPU should get dumped
229 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
230 * registers values
231 * \return 0 on success
233 int kvm_set_regs(CPUState *env, struct kvm_regs *regs);
235 * \brief Read VCPU fpu registers
237 * This gets the FPU registers from the VCPU and outputs them
238 * into a kvm_fpu structure
240 * \note This function returns a \b copy of the VCPUs registers.\n
241 * If you wish to modify the VCPU FPU registers, you should call kvm_set_fpu()
243 * \param kvm Pointer to the current kvm_context
244 * \param vcpu Which virtual CPU should get dumped
245 * \param fpu Pointer to a kvm_fpu which will be populated with the VCPUs
246 * fpu registers values
247 * \return 0 on success
249 int kvm_get_fpu(CPUState *env, struct kvm_fpu *fpu);
252 * \brief Write VCPU fpu registers
254 * This sets the FPU registers on the VCPU from a kvm_fpu structure
256 * \note When this function returns, the fpu pointer and the data it points to
257 * can be discarded
258 * \param kvm Pointer to the current kvm_context
259 * \param vcpu Which virtual CPU should get dumped
260 * \param fpu Pointer to a kvm_fpu which holds the new vcpu fpu state
261 * \return 0 on success
263 int kvm_set_fpu(CPUState *env, struct kvm_fpu *fpu);
266 * \brief Read VCPU system registers
268 * This gets the non-GP registers from the VCPU and outputs them
269 * into a kvm_sregs structure
271 * \note This function returns a \b copy of the VCPUs registers.\n
272 * If you wish to modify the VCPUs non-GP registers, you should call
273 * kvm_set_sregs()
275 * \param kvm Pointer to the current kvm_context
276 * \param vcpu Which virtual CPU should get dumped
277 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
278 * registers values
279 * \return 0 on success
281 int kvm_get_sregs(CPUState *env, struct kvm_sregs *regs);
284 * \brief Write VCPU system registers
286 * This sets the non-GP registers on the VCPU from a kvm_sregs structure
288 * \note When this function returns, the regs pointer and the data it points to
289 * can be discarded
290 * \param kvm Pointer to the current kvm_context
291 * \param vcpu Which virtual CPU should get dumped
292 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
293 * registers values
294 * \return 0 on success
296 int kvm_set_sregs(CPUState *env, struct kvm_sregs *regs);
298 #ifdef KVM_CAP_MP_STATE
300 * * \brief Read VCPU MP state
303 int kvm_get_mpstate(CPUState *env, struct kvm_mp_state *mp_state);
306 * * \brief Write VCPU MP state
309 int kvm_set_mpstate(CPUState *env, struct kvm_mp_state *mp_state);
311 * * \brief Reset VCPU MP state
314 static inline int kvm_reset_mpstate(CPUState *env)
316 struct kvm_mp_state mp_state = {.mp_state = KVM_MP_STATE_UNINITIALIZED
318 return kvm_set_mpstate(env, &mp_state);
320 #endif
323 * \brief Simulate an external vectored interrupt
325 * This allows you to simulate an external vectored interrupt.
327 * \param kvm Pointer to the current kvm_context
328 * \param vcpu Which virtual CPU should get dumped
329 * \param irq Vector number
330 * \return 0 on success
332 int kvm_inject_irq(CPUState *env, unsigned irq);
334 #ifdef KVM_CAP_SET_GUEST_DEBUG
335 int kvm_set_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
336 #endif
338 #if defined(__i386__) || defined(__x86_64__)
340 * \brief Setup a vcpu's cpuid instruction emulation
342 * Set up a table of cpuid function to cpuid outputs.\n
344 * \param kvm Pointer to the current kvm_context
345 * \param vcpu Which virtual CPU should be initialized
346 * \param nent number of entries to be installed
347 * \param entries cpuid function entries table
348 * \return 0 on success, or -errno on error
350 int kvm_setup_cpuid(CPUState *env, int nent,
351 struct kvm_cpuid_entry *entries);
354 * \brief Setup a vcpu's cpuid instruction emulation
356 * Set up a table of cpuid function to cpuid outputs.
357 * This call replaces the older kvm_setup_cpuid interface by adding a few
358 * parameters to support cpuid functions that have sub-leaf values.
360 * \param kvm Pointer to the current kvm_context
361 * \param vcpu Which virtual CPU should be initialized
362 * \param nent number of entries to be installed
363 * \param entries cpuid function entries table
364 * \return 0 on success, or -errno on error
366 int kvm_setup_cpuid2(CPUState *env, int nent,
367 struct kvm_cpuid_entry2 *entries);
370 * \brief Setting the number of shadow pages to be allocated to the vm
372 * \param kvm pointer to kvm_context
373 * \param nrshadow_pages number of pages to be allocated
375 int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages);
378 * \brief Getting the number of shadow pages that are allocated to the vm
380 * \param kvm pointer to kvm_context
381 * \param nrshadow_pages number of pages to be allocated
383 int kvm_get_shadow_pages(kvm_context_t kvm, unsigned int *nrshadow_pages);
385 #endif
388 * \brief Set a vcpu's signal mask for guest mode
390 * A vcpu can have different signals blocked in guest mode and user mode.
391 * This allows guest execution to be interrupted on a signal, without requiring
392 * that the signal be delivered to a signal handler (the signal can be
393 * dequeued using sigwait(2).
395 * \param kvm Pointer to the current kvm_context
396 * \param vcpu Which virtual CPU should be initialized
397 * \param sigset signal mask for guest mode
398 * \return 0 on success, or -errno on error
400 int kvm_set_signal_mask(CPUState *env, const sigset_t *sigset);
403 * \brief Dump VCPU registers
405 * This dumps some of the information that KVM has about a virtual CPU, namely:
406 * - GP Registers
408 * A much more verbose version of this is available as kvm_dump_vcpu()
410 * \param kvm Pointer to the current kvm_context
411 * \param vcpu Which virtual CPU should get dumped
412 * \return 0 on success
414 void kvm_show_regs(CPUState *env);
417 void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start,
418 unsigned long len, int log, int writable);
419 void kvm_destroy_phys_mem(kvm_context_t, unsigned long phys_start,
420 unsigned long len);
421 void kvm_unregister_memory_area(kvm_context_t, uint64_t phys_start,
422 unsigned long len);
424 int kvm_is_containing_region(kvm_context_t kvm, unsigned long phys_start,
425 unsigned long size);
426 int kvm_register_phys_mem(kvm_context_t kvm, unsigned long phys_start,
427 void *userspace_addr, unsigned long len, int log);
428 int kvm_get_dirty_pages(kvm_context_t, unsigned long phys_addr, void *buf);
429 int kvm_get_dirty_pages_range(kvm_context_t kvm, unsigned long phys_addr,
430 unsigned long end_addr, void *opaque,
431 int (*cb)(unsigned long start,
432 unsigned long len, void *bitmap,
433 void *opaque));
434 int kvm_register_coalesced_mmio(kvm_context_t kvm, uint64_t addr,
435 uint32_t size);
436 int kvm_unregister_coalesced_mmio(kvm_context_t kvm, uint64_t addr,
437 uint32_t size);
440 * \brief Create a memory alias
442 * Aliases a portion of physical memory to another portion. If the guest
443 * accesses the alias region, it will behave exactly as if it accessed
444 * the target memory.
446 int kvm_create_memory_alias(kvm_context_t, uint64_t phys_start, uint64_t len,
447 uint64_t target_phys);
450 * \brief Destroy a memory alias
452 * Removes an alias created with kvm_create_memory_alias().
454 int kvm_destroy_memory_alias(kvm_context_t, uint64_t phys_start);
457 * \brief Get a bitmap of guest ram pages which are allocated to the guest.
459 * \param kvm Pointer to the current kvm_context
460 * \param phys_addr Memory slot phys addr
461 * \param bitmap Long aligned address of a big enough bitmap (one bit per page)
463 int kvm_get_mem_map(kvm_context_t kvm, unsigned long phys_addr, void *bitmap);
464 int kvm_get_mem_map_range(kvm_context_t kvm, unsigned long phys_addr,
465 unsigned long len, void *buf, void *opaque,
466 int (*cb)(unsigned long start,
467 unsigned long len, void *bitmap,
468 void *opaque));
469 int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status);
471 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm, uint64_t phys_start,
472 uint64_t len);
473 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm, uint64_t phys_start,
474 uint64_t len);
476 * \brief Enable dirty-pages-logging for all memory regions
478 * \param kvm Pointer to the current kvm_context
480 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm);
483 * \brief Disable dirty-page-logging for some memory regions
485 * Disable dirty-pages-logging for those memory regions that were
486 * created with dirty-page-logging disabled.
488 * \param kvm Pointer to the current kvm_context
490 int kvm_dirty_pages_log_reset(kvm_context_t kvm);
492 #ifdef KVM_CAP_IRQCHIP
494 * \brief Dump in kernel IRQCHIP contents
496 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
497 * and IOAPIC into a kvm_irqchip structure
499 * \param kvm Pointer to the current kvm_context
500 * \param chip The irq chip device to be dumped
502 int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
505 * \brief Set in kernel IRQCHIP contents
507 * Write one of the in kernel irq chip devices, including PIC (master/slave)
508 * and IOAPIC
511 * \param kvm Pointer to the current kvm_context
512 * \param chip THe irq chip device to be written
514 int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
516 #if defined(__i386__) || defined(__x86_64__)
518 * \brief Get in kernel local APIC for vcpu
520 * Save the local apic state including the timer of a virtual CPU
522 * \param kvm Pointer to the current kvm_context
523 * \param vcpu Which virtual CPU should be accessed
524 * \param s Local apic state of the specific virtual CPU
526 int kvm_get_lapic(CPUState *env, struct kvm_lapic_state *s);
529 * \brief Set in kernel local APIC for vcpu
531 * Restore the local apic state including the timer of a virtual CPU
533 * \param kvm Pointer to the current kvm_context
534 * \param vcpu Which virtual CPU should be accessed
535 * \param s Local apic state of the specific virtual CPU
537 int kvm_set_lapic(CPUState *env, struct kvm_lapic_state *s);
539 #endif
542 * \brief Simulate an NMI
544 * This allows you to simulate a non-maskable interrupt.
546 * \param kvm Pointer to the current kvm_context
547 * \param vcpu Which virtual CPU should get dumped
548 * \return 0 on success
550 int kvm_inject_nmi(CPUState *env);
552 #endif
555 * \brief Simulate an x86 MCE
557 * This allows you to simulate a x86 MCE.
559 * \param cenv Which virtual CPU should get MCE injected
560 * \param bank Bank number
561 * \param status MSR_MCI_STATUS
562 * \param mcg_status MSR_MCG_STATUS
563 * \param addr MSR_MCI_ADDR
564 * \param misc MSR_MCI_MISC
565 * \param abort_on_error abort on error
567 void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
568 uint64_t mcg_status, uint64_t addr, uint64_t misc,
569 int abort_on_error);
572 * \brief Query wheather in kernel pit is used
574 * \param kvm Pointer to the current kvm_context
576 int kvm_pit_in_kernel(kvm_context_t kvm);
579 * \brief Initialize coalesced MMIO
581 * Check for coalesced MMIO capability and store in context
583 * \param kvm Pointer to the current kvm_context
585 int kvm_init_coalesced_mmio(kvm_context_t kvm);
587 #ifdef KVM_CAP_PIT
589 #if defined(__i386__) || defined(__x86_64__)
591 * \brief Get in kernel PIT of the virtual domain
593 * Save the PIT state.
595 * \param kvm Pointer to the current kvm_context
596 * \param s PIT state of the virtual domain
598 int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s);
601 * \brief Set in kernel PIT of the virtual domain
603 * Restore the PIT state.
604 * Timer would be retriggerred after restored.
606 * \param kvm Pointer to the current kvm_context
607 * \param s PIT state of the virtual domain
609 int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s);
611 int kvm_reinject_control(kvm_context_t kvm, int pit_reinject);
613 #ifdef KVM_CAP_PIT_STATE2
615 * \brief Check for kvm support of kvm_pit_state2
617 * \param kvm Pointer to the current kvm_context
618 * \return 0 on success
620 int kvm_has_pit_state2(kvm_context_t kvm);
623 * \brief Set in kernel PIT state2 of the virtual domain
626 * \param kvm Pointer to the current kvm_context
627 * \param ps2 PIT state2 of the virtual domain
628 * \return 0 on success
630 int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
633 * \brief Get in kernel PIT state2 of the virtual domain
636 * \param kvm Pointer to the current kvm_context
637 * \param ps2 PIT state2 of the virtual domain
638 * \return 0 on success
640 int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
642 #endif
643 #endif
644 #endif
646 #ifdef KVM_CAP_VAPIC
649 * \brief Enable kernel tpr access reporting
651 * When tpr access reporting is enabled, the kernel will call the
652 * ->tpr_access() callback every time the guest vcpu accesses the tpr.
654 * \param kvm Pointer to the current kvm_context
655 * \param vcpu vcpu to enable tpr access reporting on
657 int kvm_enable_tpr_access_reporting(CPUState *env);
660 * \brief Disable kernel tpr access reporting
662 * Undoes the effect of kvm_enable_tpr_access_reporting().
664 * \param kvm Pointer to the current kvm_context
665 * \param vcpu vcpu to disable tpr access reporting on
667 int kvm_disable_tpr_access_reporting(CPUState *env);
669 int kvm_enable_vapic(CPUState *env, uint64_t vapic);
671 #endif
673 #if defined(__s390__)
674 int kvm_s390_initial_reset(kvm_context_t kvm, int slot);
675 int kvm_s390_interrupt(kvm_context_t kvm, int slot,
676 struct kvm_s390_interrupt *kvmint);
677 int kvm_s390_set_initial_psw(kvm_context_t kvm, int slot, psw_t psw);
678 int kvm_s390_store_status(kvm_context_t kvm, int slot, unsigned long addr);
679 #endif
681 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
683 * \brief Notifies host kernel about a PCI device to be assigned to a guest
685 * Used for PCI device assignment, this function notifies the host
686 * kernel about the assigning of the physical PCI device to a guest.
688 * \param kvm Pointer to the current kvm_context
689 * \param assigned_dev Parameters, like bus, devfn number, etc
691 int kvm_assign_pci_device(kvm_context_t kvm,
692 struct kvm_assigned_pci_dev *assigned_dev);
695 * \brief Assign IRQ for an assigned device
697 * Used for PCI device assignment, this function assigns IRQ numbers for
698 * an physical device and guest IRQ handling.
700 * \param kvm Pointer to the current kvm_context
701 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
703 int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
705 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
707 * \brief Deassign IRQ for an assigned device
709 * Used for PCI device assignment, this function deassigns IRQ numbers
710 * for an assigned device.
712 * \param kvm Pointer to the current kvm_context
713 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
715 int kvm_deassign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
716 #endif
717 #endif
720 * \brief Determines whether destroying memory regions is allowed
722 * KVM before 2.6.29 had a bug when destroying memory regions.
724 * \param kvm Pointer to the current kvm_context
726 int kvm_destroy_memory_region_works(kvm_context_t kvm);
728 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
730 * \brief Notifies host kernel about a PCI device to be deassigned from a guest
732 * Used for hot remove PCI device, this function notifies the host
733 * kernel about the deassigning of the physical PCI device from a guest.
735 * \param kvm Pointer to the current kvm_context
736 * \param assigned_dev Parameters, like bus, devfn number, etc
738 int kvm_deassign_pci_device(kvm_context_t kvm,
739 struct kvm_assigned_pci_dev *assigned_dev);
740 #endif
743 * \brief Checks whether the generic irq routing capability is present
745 * Checks whether kvm can reroute interrupts among the various interrupt
746 * controllers.
748 * \param kvm Pointer to the current kvm_context
750 int kvm_has_gsi_routing(kvm_context_t kvm);
753 * \brief Determines the number of gsis that can be routed
755 * Returns the number of distinct gsis that can be routed by kvm. This is
756 * also the number of distinct routes (if a gsi has two routes, than another
757 * gsi cannot be used...)
759 * \param kvm Pointer to the current kvm_context
761 int kvm_get_gsi_count(kvm_context_t kvm);
764 * \brief Clears the temporary irq routing table
766 * Clears the temporary irq routing table. Nothing is committed to the
767 * running VM.
769 * \param kvm Pointer to the current kvm_context
771 int kvm_clear_gsi_routes(kvm_context_t kvm);
774 * \brief Adds an irq route to the temporary irq routing table
776 * Adds an irq route to the temporary irq routing table. Nothing is
777 * committed to the running VM.
779 * \param kvm Pointer to the current kvm_context
781 int kvm_add_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
784 * \brief Removes an irq route from the temporary irq routing table
786 * Adds an irq route to the temporary irq routing table. Nothing is
787 * committed to the running VM.
789 * \param kvm Pointer to the current kvm_context
791 int kvm_del_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
793 struct kvm_irq_routing_entry;
795 * \brief Adds a routing entry to the temporary irq routing table
797 * Adds a filled routing entry to the temporary irq routing table. Nothing is
798 * committed to the running VM.
800 * \param kvm Pointer to the current kvm_context
802 int kvm_add_routing_entry(kvm_context_t kvm,
803 struct kvm_irq_routing_entry *entry);
806 * \brief Removes a routing from the temporary irq routing table
808 * Remove a routing to the temporary irq routing table. Nothing is
809 * committed to the running VM.
811 * \param kvm Pointer to the current kvm_context
813 int kvm_del_routing_entry(kvm_context_t kvm,
814 struct kvm_irq_routing_entry *entry);
817 * \brief Updates a routing in the temporary irq routing table
819 * Update a routing in the temporary irq routing table
820 * with a new value. entry type and GSI can not be changed.
821 * Nothing is committed to the running VM.
823 * \param kvm Pointer to the current kvm_context
825 int kvm_update_routing_entry(kvm_context_t kvm,
826 struct kvm_irq_routing_entry *entry,
827 struct kvm_irq_routing_entry *newentry);
830 * \brief Commit the temporary irq routing table
832 * Commit the temporary irq routing table to the running VM.
834 * \param kvm Pointer to the current kvm_context
836 int kvm_commit_irq_routes(kvm_context_t kvm);
839 * \brief Get unused GSI number for irq routing table
841 * Get unused GSI number for irq routing table
843 * \param kvm Pointer to the current kvm_context
845 int kvm_get_irq_route_gsi(kvm_context_t kvm);
848 * \brief Create a file descriptor for injecting interrupts
850 * Creates an eventfd based file-descriptor that maps to a specific GSI
851 * in the guest. eventfd compliant signaling (write() from userspace, or
852 * eventfd_signal() from kernelspace) will cause the GSI to inject
853 * itself into the guest at the next available window.
855 * \param kvm Pointer to the current kvm_context
856 * \param gsi GSI to assign to this fd
857 * \param flags reserved, must be zero
859 int kvm_irqfd(kvm_context_t kvm, int gsi, int flags);
861 #ifdef KVM_CAP_DEVICE_MSIX
862 int kvm_assign_set_msix_nr(kvm_context_t kvm,
863 struct kvm_assigned_msix_nr *msix_nr);
864 int kvm_assign_set_msix_entry(kvm_context_t kvm,
865 struct kvm_assigned_msix_entry *entry);
866 #endif
868 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg);
870 #else /* !CONFIG_KVM */
872 typedef struct kvm_context *kvm_context_t;
873 typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
875 struct kvm_pit_state {
878 static inline int kvm_init(int smp_cpus)
880 return 0;
883 #ifndef QEMU_KVM_NO_CPU
885 static inline void kvm_inject_x86_mce(CPUState *cenv, int bank,
886 uint64_t status, uint64_t mcg_status,
887 uint64_t addr, uint64_t misc,
888 int abort_on_error)
890 if (abort_on_error)
891 abort();
894 #endif
896 extern int kvm_allowed;
898 #endif /* !CONFIG_KVM */
901 int kvm_main_loop(void);
902 int kvm_init_ap(void);
903 #ifndef QEMU_KVM_NO_CPU
904 int kvm_vcpu_inited(CPUState *env);
905 void kvm_load_registers(CPUState *env);
906 void kvm_save_registers(CPUState *env);
907 void kvm_load_mpstate(CPUState *env);
908 void kvm_save_mpstate(CPUState *env);
909 int kvm_cpu_exec(CPUState *env);
910 int kvm_insert_breakpoint(CPUState * current_env, target_ulong addr,
911 target_ulong len, int type);
912 int kvm_remove_breakpoint(CPUState * current_env, target_ulong addr,
913 target_ulong len, int type);
914 void kvm_remove_all_breakpoints(CPUState * current_env);
915 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
916 void kvm_apic_init(CPUState *env);
917 /* called from vcpu initialization */
918 void qemu_kvm_load_lapic(CPUState *env);
919 #endif
921 void kvm_hpet_enable_kpit(void);
922 void kvm_hpet_disable_kpit(void);
923 int kvm_set_irq(int irq, int level, int *status);
925 int kvm_physical_memory_set_dirty_tracking(int enable);
926 int kvm_update_dirty_pages_log(void);
928 #ifndef QEMU_KVM_NO_CPU
929 void qemu_kvm_call_with_env(void (*func)(void *), void *data, CPUState *env);
930 void qemu_kvm_cpuid_on_env(CPUState *env);
931 void kvm_inject_interrupt(CPUState *env, int mask);
932 void kvm_update_after_sipi(CPUState *env);
933 void kvm_update_interrupt_request(CPUState *env);
934 #endif
935 void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
936 ram_addr_t phys_offset);
937 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr, unsigned long size,
938 int log, int writable);
940 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
941 unsigned long size);
942 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
943 int log);
944 int kvm_setup_guest_memory(void *area, unsigned long size);
945 int kvm_qemu_create_memory_alias(uint64_t phys_start, uint64_t len,
946 uint64_t target_phys);
947 int kvm_qemu_destroy_memory_alias(uint64_t phys_start);
949 int kvm_arch_qemu_create_context(void);
951 #ifndef QEMU_KVM_NO_CPU
952 void kvm_arch_save_regs(CPUState *env);
953 void kvm_arch_load_regs(CPUState *env);
954 void kvm_arch_load_mpstate(CPUState *env);
955 void kvm_arch_save_mpstate(CPUState *env);
956 int kvm_arch_init_vcpu(CPUState *cenv);
957 int kvm_arch_pre_run(CPUState *env, struct kvm_run *run);
958 int kvm_arch_post_run(CPUState *env, struct kvm_run *run);
959 int kvm_arch_has_work(CPUState *env);
960 void kvm_arch_process_irqchip_events(CPUState *env);
961 int kvm_arch_try_push_interrupts(void *opaque);
962 void kvm_arch_push_nmi(void *opaque);
963 void kvm_arch_cpu_reset(CPUState *env);
964 int kvm_set_boot_cpu_id(uint32_t id);
966 struct kvm_guest_debug;
967 struct kvm_debug_exit_arch;
969 struct kvm_sw_breakpoint {
970 target_ulong pc;
971 target_ulong saved_insn;
972 int use_count;
973 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
976 QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
978 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
979 int kvm_sw_breakpoints_active(CPUState *env);
980 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
981 target_ulong pc);
982 int kvm_arch_insert_sw_breakpoint(CPUState * current_env,
983 struct kvm_sw_breakpoint *bp);
984 int kvm_arch_remove_sw_breakpoint(CPUState * current_env,
985 struct kvm_sw_breakpoint *bp);
986 int kvm_arch_insert_hw_breakpoint(target_ulong addr, target_ulong len,
987 int type);
988 int kvm_arch_remove_hw_breakpoint(target_ulong addr, target_ulong len,
989 int type);
990 void kvm_arch_remove_all_hw_breakpoints(void);
991 void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
993 #endif
995 void qemu_kvm_aio_wait_start(void);
996 void qemu_kvm_aio_wait(void);
997 void qemu_kvm_aio_wait_end(void);
999 void qemu_kvm_notify_work(void);
1001 #ifndef QEMU_KVM_NO_CPU
1002 void kvm_tpr_opt_setup(void);
1003 void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
1004 void kvm_tpr_vcpu_start(CPUState *env);
1005 #endif
1007 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf);
1008 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1009 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1011 int kvm_arch_init_irq_routing(void);
1013 int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t * data, int len);
1014 int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t * data, int len);
1016 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1017 struct ioperm_data;
1019 void kvm_ioperm(CPUState *env, void *data);
1020 void kvm_add_ioperm_data(struct ioperm_data *data);
1021 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num);
1022 void kvm_arch_do_ioperm(void *_data);
1023 #endif
1025 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
1026 #ifndef QEMU_KVM_NO_CPU
1027 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
1028 #endif
1030 #ifdef CONFIG_KVM
1031 #include "qemu-queue.h"
1033 extern int kvm_allowed;
1034 extern int kvm_irqchip;
1035 extern int kvm_pit;
1036 extern int kvm_pit_reinject;
1037 extern int kvm_nested;
1038 extern kvm_context_t kvm_context;
1040 struct ioperm_data {
1041 unsigned long start_port;
1042 unsigned long num;
1043 int turn_on;
1044 QLIST_ENTRY(ioperm_data) entries;
1047 void qemu_kvm_cpu_stop(CPUState *env);
1048 int kvm_arch_halt(CPUState *env);
1049 int handle_tpr_access(void *opaque, CPUState *env, uint64_t rip,
1050 int is_write);
1051 int kvm_has_sync_mmu(void);
1053 #define kvm_enabled() (kvm_allowed)
1054 #define qemu_kvm_pit_in_kernel() kvm_pit_in_kernel(kvm_context)
1055 #define qemu_kvm_has_gsi_routing() kvm_has_gsi_routing(kvm_context)
1056 #ifdef TARGET_I386
1057 #define qemu_kvm_has_pit_state2() kvm_has_pit_state2(kvm_context)
1058 #endif
1059 void kvm_init_vcpu(CPUState *env);
1060 void kvm_load_tsc(CPUState *env);
1061 #else
1062 #define kvm_has_sync_mmu() (0)
1063 #define kvm_enabled() (0)
1064 #define kvm_nested 0
1065 #define qemu_kvm_pit_in_kernel() (0)
1066 #define qemu_kvm_has_gsi_routing() (0)
1067 #ifndef QEMU_KVM_NO_CPU
1068 #ifdef TARGET_I386
1069 #define qemu_kvm_has_pit_state2() (0)
1070 #endif
1071 #define kvm_load_registers(env) do {} while(0)
1072 #define kvm_save_registers(env) do {} while(0)
1073 #define qemu_kvm_cpu_stop(env) do {} while(0)
1074 static inline void kvm_init_vcpu(CPUState *env)
1078 static inline void kvm_load_tsc(CPUState *env)
1081 #endif
1082 #endif
1084 void kvm_mutex_unlock(void);
1085 void kvm_mutex_lock(void);
1087 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
1088 target_phys_addr_t end_addr);
1090 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len);
1091 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len);
1094 static inline int kvm_sync_vcpus(void)
1096 return 0;
1099 #ifndef QEMU_KVM_NO_CPU
1100 void kvm_arch_get_registers(CPUState *env);
1102 static inline void kvm_arch_put_registers(CPUState *env)
1104 kvm_load_registers(env);
1107 void kvm_cpu_synchronize_state(CPUState *env);
1109 static inline void cpu_synchronize_state(CPUState *env)
1111 if (kvm_enabled()) {
1112 kvm_cpu_synchronize_state(env);
1116 uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
1117 int reg);
1120 #endif
1122 static inline int kvm_set_migration_log(int enable)
1124 return kvm_physical_memory_set_dirty_tracking(enable);
1128 int kvm_irqchip_in_kernel(void);
1129 #ifdef CONFIG_KVM
1131 typedef struct KVMSlot {
1132 target_phys_addr_t start_addr;
1133 ram_addr_t memory_size;
1134 ram_addr_t phys_offset;
1135 int slot;
1136 int flags;
1137 } KVMSlot;
1139 typedef struct kvm_dirty_log KVMDirtyLog;
1141 typedef struct KVMState {
1142 KVMSlot slots[32];
1143 int fd;
1144 int vmfd;
1145 int coalesced_mmio;
1146 int broken_set_mem_region;
1147 int migration_log;
1148 #ifdef KVM_CAP_SET_GUEST_DEBUG
1149 QTAILQ_HEAD(, kvm_sw_breakpoint) kvm_sw_breakpoints;
1150 #endif
1151 int irqchip_in_kernel;
1153 struct kvm_context kvm_context;
1154 } KVMState;
1156 extern KVMState *kvm_state;
1158 int kvm_ioctl(KVMState *s, int type, ...);
1159 int kvm_vm_ioctl(KVMState *s, int type, ...);
1160 int kvm_vcpu_ioctl(CPUState *env, int type, ...);
1161 int kvm_check_extension(KVMState *s, unsigned int ext);
1163 #endif
1165 #endif