use env in kvm_arch_run
[qemu-kvm/amd-iommu.git] / qemu-kvm.h
blob8b5cfb7884890d3aed2069d07745d1838abe6b6e
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 struct kvm_vcpu_context {
80 int fd;
81 struct kvm_run *run;
82 uint32_t id;
85 typedef struct kvm_context *kvm_context_t;
86 typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
88 #include "kvm.h"
89 int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
90 void **vm_mem);
91 int kvm_alloc_userspace_memory(kvm_context_t kvm, unsigned long memory,
92 void **vm_mem);
94 int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
95 void **vm_mem);
97 int kvm_arch_run(CPUState *env);
100 void kvm_show_code(kvm_vcpu_context_t vcpu);
102 int handle_halt(kvm_vcpu_context_t vcpu);
104 #ifndef QEMU_KVM_NO_CPU
106 int handle_shutdown(kvm_context_t kvm, CPUState *env);
107 void post_kvm_run(kvm_context_t kvm, CPUState *env);
108 int pre_kvm_run(kvm_context_t kvm, CPUState *env);
109 int handle_io_window(kvm_context_t kvm);
110 int try_push_interrupts(kvm_context_t kvm);
112 #if defined(__x86_64__) || defined(__i386__)
113 struct kvm_msr_list *kvm_get_msr_list(kvm_context_t);
114 int kvm_get_msrs(kvm_vcpu_context_t, struct kvm_msr_entry *msrs, int n);
115 int kvm_set_msrs(kvm_vcpu_context_t, struct kvm_msr_entry *msrs, int n);
116 int kvm_get_mce_cap_supported(kvm_context_t, uint64_t *mce_cap,
117 int *max_banks);
118 int kvm_setup_mce(kvm_vcpu_context_t vcpu, uint64_t *mcg_cap);
119 struct kvm_x86_mce;
120 int kvm_set_mce(kvm_vcpu_context_t vcpu, struct kvm_x86_mce *mce);
121 #endif
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, unsigned long phys_mem_bytes,
172 void **phys_mem);
173 int kvm_create_vm(kvm_context_t kvm);
174 void kvm_create_irqchip(kvm_context_t kvm);
177 * \brief Create a new virtual cpu
179 * This creates a new virtual cpu (the first vcpu is created by kvm_create()).
180 * Should be called from a thread dedicated to the vcpu.
182 * \param kvm kvm context
183 * \param slot vcpu number (> 0)
184 * \return 0 on success, -errno on failure
186 kvm_vcpu_context_t kvm_create_vcpu(CPUState *env, int id);
189 * \brief Start the VCPU
191 * This starts the VCPU and virtualization is started.\n
192 * \n
193 * This function will not return until any of these conditions are met:
194 * - An IO/MMIO handler does not return "0"
195 * - An exception that neither the guest OS, nor KVM can handle occurs
197 * \note This function will call the callbacks registered in kvm_init()
198 * to emulate those functions
199 * \note If you at any point want to interrupt the VCPU, kvm_run() will
200 * listen to the EINTR signal. This allows you to simulate external interrupts
201 * and asyncronous IO.
203 * \param kvm Pointer to the current kvm_context
204 * \param vcpu Which virtual CPU should be started
205 * \return 0 on success, but you really shouldn't expect this function to
206 * return except for when an error has occured, or when you have sent it
207 * an EINTR signal.
209 int kvm_run(kvm_vcpu_context_t vcpu, void *env);
212 * \brief Get interrupt flag from on last exit to userspace
214 * This gets the CPU interrupt flag as it was on the last exit to userspace.
216 * \param kvm Pointer to the current kvm_context
217 * \param vcpu Which virtual CPU should get dumped
218 * \return interrupt flag value (0 or 1)
220 int kvm_get_interrupt_flag(CPUState *env);
223 * \brief Check if a vcpu is ready for interrupt injection
225 * This checks if vcpu interrupts are not masked by mov ss or sti.
227 * \param kvm Pointer to the current kvm_context
228 * \param vcpu Which virtual CPU should get dumped
229 * \return boolean indicating interrupt injection readiness
231 int kvm_is_ready_for_interrupt_injection(CPUState *env);
234 * \brief Read VCPU registers
236 * This gets the GP registers from the VCPU and outputs them
237 * into a kvm_regs structure
239 * \note This function returns a \b copy of the VCPUs registers.\n
240 * If you wish to modify the VCPUs GP registers, you should call kvm_set_regs()
242 * \param kvm Pointer to the current kvm_context
243 * \param vcpu Which virtual CPU should get dumped
244 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
245 * registers values
246 * \return 0 on success
248 int kvm_get_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
251 * \brief Write VCPU registers
253 * This sets the GP registers on the VCPU from a kvm_regs structure
255 * \note When this function returns, the regs pointer and the data it points to
256 * can be discarded
257 * \param kvm Pointer to the current kvm_context
258 * \param vcpu Which virtual CPU should get dumped
259 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
260 * registers values
261 * \return 0 on success
263 int kvm_set_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
265 * \brief Read VCPU fpu registers
267 * This gets the FPU registers from the VCPU and outputs them
268 * into a kvm_fpu structure
270 * \note This function returns a \b copy of the VCPUs registers.\n
271 * If you wish to modify the VCPU FPU registers, you should call kvm_set_fpu()
273 * \param kvm Pointer to the current kvm_context
274 * \param vcpu Which virtual CPU should get dumped
275 * \param fpu Pointer to a kvm_fpu which will be populated with the VCPUs
276 * fpu registers values
277 * \return 0 on success
279 int kvm_get_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
282 * \brief Write VCPU fpu registers
284 * This sets the FPU registers on the VCPU from a kvm_fpu structure
286 * \note When this function returns, the fpu pointer and the data it points to
287 * can be discarded
288 * \param kvm Pointer to the current kvm_context
289 * \param vcpu Which virtual CPU should get dumped
290 * \param fpu Pointer to a kvm_fpu which holds the new vcpu fpu state
291 * \return 0 on success
293 int kvm_set_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
296 * \brief Read VCPU system registers
298 * This gets the non-GP registers from the VCPU and outputs them
299 * into a kvm_sregs structure
301 * \note This function returns a \b copy of the VCPUs registers.\n
302 * If you wish to modify the VCPUs non-GP registers, you should call
303 * kvm_set_sregs()
305 * \param kvm Pointer to the current kvm_context
306 * \param vcpu Which virtual CPU should get dumped
307 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
308 * registers values
309 * \return 0 on success
311 int kvm_get_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
314 * \brief Write VCPU system registers
316 * This sets the non-GP registers on the VCPU from a kvm_sregs structure
318 * \note When this function returns, the regs pointer and the data it points to
319 * can be discarded
320 * \param kvm Pointer to the current kvm_context
321 * \param vcpu Which virtual CPU should get dumped
322 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
323 * registers values
324 * \return 0 on success
326 int kvm_set_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
328 #ifdef KVM_CAP_MP_STATE
330 * * \brief Read VCPU MP state
333 int kvm_get_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
336 * * \brief Write VCPU MP state
339 int kvm_set_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
341 * * \brief Reset VCPU MP state
344 static inline int kvm_reset_mpstate(kvm_vcpu_context_t vcpu)
346 struct kvm_mp_state mp_state = {.mp_state = KVM_MP_STATE_UNINITIALIZED
348 return kvm_set_mpstate(vcpu, &mp_state);
350 #endif
353 * \brief Simulate an external vectored interrupt
355 * This allows you to simulate an external vectored interrupt.
357 * \param kvm Pointer to the current kvm_context
358 * \param vcpu Which virtual CPU should get dumped
359 * \param irq Vector number
360 * \return 0 on success
362 int kvm_inject_irq(kvm_vcpu_context_t vcpu, unsigned irq);
364 #ifdef KVM_CAP_SET_GUEST_DEBUG
365 int kvm_set_guest_debug(kvm_vcpu_context_t, struct kvm_guest_debug *dbg);
366 #endif
368 #if defined(__i386__) || defined(__x86_64__)
370 * \brief Setup a vcpu's cpuid instruction emulation
372 * Set up a table of cpuid function to cpuid outputs.\n
374 * \param kvm Pointer to the current kvm_context
375 * \param vcpu Which virtual CPU should be initialized
376 * \param nent number of entries to be installed
377 * \param entries cpuid function entries table
378 * \return 0 on success, or -errno on error
380 int kvm_setup_cpuid(kvm_vcpu_context_t vcpu, int nent,
381 struct kvm_cpuid_entry *entries);
384 * \brief Setup a vcpu's cpuid instruction emulation
386 * Set up a table of cpuid function to cpuid outputs.
387 * This call replaces the older kvm_setup_cpuid interface by adding a few
388 * parameters to support cpuid functions that have sub-leaf values.
390 * \param kvm Pointer to the current kvm_context
391 * \param vcpu Which virtual CPU should be initialized
392 * \param nent number of entries to be installed
393 * \param entries cpuid function entries table
394 * \return 0 on success, or -errno on error
396 int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu, int nent,
397 struct kvm_cpuid_entry2 *entries);
400 * \brief Setting the number of shadow pages to be allocated to the vm
402 * \param kvm pointer to kvm_context
403 * \param nrshadow_pages number of pages to be allocated
405 int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages);
408 * \brief Getting the number of shadow pages that are allocated to the vm
410 * \param kvm pointer to kvm_context
411 * \param nrshadow_pages number of pages to be allocated
413 int kvm_get_shadow_pages(kvm_context_t kvm, unsigned int *nrshadow_pages);
415 #endif
418 * \brief Set a vcpu's signal mask for guest mode
420 * A vcpu can have different signals blocked in guest mode and user mode.
421 * This allows guest execution to be interrupted on a signal, without requiring
422 * that the signal be delivered to a signal handler (the signal can be
423 * dequeued using sigwait(2).
425 * \param kvm Pointer to the current kvm_context
426 * \param vcpu Which virtual CPU should be initialized
427 * \param sigset signal mask for guest mode
428 * \return 0 on success, or -errno on error
430 int kvm_set_signal_mask(kvm_vcpu_context_t vcpu, const sigset_t *sigset);
433 * \brief Dump VCPU registers
435 * This dumps some of the information that KVM has about a virtual CPU, namely:
436 * - GP Registers
438 * A much more verbose version of this is available as kvm_dump_vcpu()
440 * \param kvm Pointer to the current kvm_context
441 * \param vcpu Which virtual CPU should get dumped
442 * \return 0 on success
444 void kvm_show_regs(kvm_vcpu_context_t vcpu);
447 void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start,
448 unsigned long len, int log, int writable);
449 void kvm_destroy_phys_mem(kvm_context_t, unsigned long phys_start,
450 unsigned long len);
451 void kvm_unregister_memory_area(kvm_context_t, uint64_t phys_start,
452 unsigned long len);
454 int kvm_is_containing_region(kvm_context_t kvm, unsigned long phys_start,
455 unsigned long size);
456 int kvm_register_phys_mem(kvm_context_t kvm, unsigned long phys_start,
457 void *userspace_addr, unsigned long len, int log);
458 int kvm_get_dirty_pages(kvm_context_t, unsigned long phys_addr, void *buf);
459 int kvm_get_dirty_pages_range(kvm_context_t kvm, unsigned long phys_addr,
460 unsigned long end_addr, void *opaque,
461 int (*cb)(unsigned long start,
462 unsigned long len, void *bitmap,
463 void *opaque));
464 int kvm_register_coalesced_mmio(kvm_context_t kvm, uint64_t addr,
465 uint32_t size);
466 int kvm_unregister_coalesced_mmio(kvm_context_t kvm, uint64_t addr,
467 uint32_t size);
470 * \brief Create a memory alias
472 * Aliases a portion of physical memory to another portion. If the guest
473 * accesses the alias region, it will behave exactly as if it accessed
474 * the target memory.
476 int kvm_create_memory_alias(kvm_context_t, uint64_t phys_start, uint64_t len,
477 uint64_t target_phys);
480 * \brief Destroy a memory alias
482 * Removes an alias created with kvm_create_memory_alias().
484 int kvm_destroy_memory_alias(kvm_context_t, uint64_t phys_start);
487 * \brief Get a bitmap of guest ram pages which are allocated to the guest.
489 * \param kvm Pointer to the current kvm_context
490 * \param phys_addr Memory slot phys addr
491 * \param bitmap Long aligned address of a big enough bitmap (one bit per page)
493 int kvm_get_mem_map(kvm_context_t kvm, unsigned long phys_addr, void *bitmap);
494 int kvm_get_mem_map_range(kvm_context_t kvm, unsigned long phys_addr,
495 unsigned long len, void *buf, void *opaque,
496 int (*cb)(unsigned long start,
497 unsigned long len, void *bitmap,
498 void *opaque));
499 int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status);
501 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm, uint64_t phys_start,
502 uint64_t len);
503 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm, uint64_t phys_start,
504 uint64_t len);
506 * \brief Enable dirty-pages-logging for all memory regions
508 * \param kvm Pointer to the current kvm_context
510 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm);
513 * \brief Disable dirty-page-logging for some memory regions
515 * Disable dirty-pages-logging for those memory regions that were
516 * created with dirty-page-logging disabled.
518 * \param kvm Pointer to the current kvm_context
520 int kvm_dirty_pages_log_reset(kvm_context_t kvm);
522 #ifdef KVM_CAP_IRQCHIP
524 * \brief Dump in kernel IRQCHIP contents
526 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
527 * and IOAPIC into a kvm_irqchip structure
529 * \param kvm Pointer to the current kvm_context
530 * \param chip The irq chip device to be dumped
532 int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
535 * \brief Set in kernel IRQCHIP contents
537 * Write one of the in kernel irq chip devices, including PIC (master/slave)
538 * and IOAPIC
541 * \param kvm Pointer to the current kvm_context
542 * \param chip THe irq chip device to be written
544 int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
546 #if defined(__i386__) || defined(__x86_64__)
548 * \brief Get in kernel local APIC for vcpu
550 * Save the local apic state including the timer of a virtual CPU
552 * \param kvm Pointer to the current kvm_context
553 * \param vcpu Which virtual CPU should be accessed
554 * \param s Local apic state of the specific virtual CPU
556 int kvm_get_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
559 * \brief Set in kernel local APIC for vcpu
561 * Restore the local apic state including the timer of a virtual CPU
563 * \param kvm Pointer to the current kvm_context
564 * \param vcpu Which virtual CPU should be accessed
565 * \param s Local apic state of the specific virtual CPU
567 int kvm_set_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
569 #endif
572 * \brief Simulate an NMI
574 * This allows you to simulate a non-maskable interrupt.
576 * \param kvm Pointer to the current kvm_context
577 * \param vcpu Which virtual CPU should get dumped
578 * \return 0 on success
580 int kvm_inject_nmi(kvm_vcpu_context_t vcpu);
582 #endif
585 * \brief Simulate an x86 MCE
587 * This allows you to simulate a x86 MCE.
589 * \param cenv Which virtual CPU should get MCE injected
590 * \param bank Bank number
591 * \param status MSR_MCI_STATUS
592 * \param mcg_status MSR_MCG_STATUS
593 * \param addr MSR_MCI_ADDR
594 * \param misc MSR_MCI_MISC
595 * \param abort_on_error abort on error
597 void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
598 uint64_t mcg_status, uint64_t addr, uint64_t misc,
599 int abort_on_error);
602 * \brief Query wheather in kernel pit is used
604 * \param kvm Pointer to the current kvm_context
606 int kvm_pit_in_kernel(kvm_context_t kvm);
609 * \brief Initialize coalesced MMIO
611 * Check for coalesced MMIO capability and store in context
613 * \param kvm Pointer to the current kvm_context
615 int kvm_init_coalesced_mmio(kvm_context_t kvm);
617 #ifdef KVM_CAP_PIT
619 #if defined(__i386__) || defined(__x86_64__)
621 * \brief Get in kernel PIT of the virtual domain
623 * Save the PIT state.
625 * \param kvm Pointer to the current kvm_context
626 * \param s PIT state of the virtual domain
628 int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s);
631 * \brief Set in kernel PIT of the virtual domain
633 * Restore the PIT state.
634 * Timer would be retriggerred after restored.
636 * \param kvm Pointer to the current kvm_context
637 * \param s PIT state of the virtual domain
639 int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s);
641 int kvm_reinject_control(kvm_context_t kvm, int pit_reinject);
643 #ifdef KVM_CAP_PIT_STATE2
645 * \brief Check for kvm support of kvm_pit_state2
647 * \param kvm Pointer to the current kvm_context
648 * \return 0 on success
650 int kvm_has_pit_state2(kvm_context_t kvm);
653 * \brief Set in kernel PIT state2 of the virtual domain
656 * \param kvm Pointer to the current kvm_context
657 * \param ps2 PIT state2 of the virtual domain
658 * \return 0 on success
660 int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
663 * \brief Get in kernel PIT state2 of the virtual domain
666 * \param kvm Pointer to the current kvm_context
667 * \param ps2 PIT state2 of the virtual domain
668 * \return 0 on success
670 int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
672 #endif
673 #endif
674 #endif
676 #ifdef KVM_CAP_VAPIC
679 * \brief Enable kernel tpr access reporting
681 * When tpr access reporting is enabled, the kernel will call the
682 * ->tpr_access() callback every time the guest vcpu accesses the tpr.
684 * \param kvm Pointer to the current kvm_context
685 * \param vcpu vcpu to enable tpr access reporting on
687 int kvm_enable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
690 * \brief Disable kernel tpr access reporting
692 * Undoes the effect of kvm_enable_tpr_access_reporting().
694 * \param kvm Pointer to the current kvm_context
695 * \param vcpu vcpu to disable tpr access reporting on
697 int kvm_disable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
699 int kvm_enable_vapic(kvm_vcpu_context_t vcpu, uint64_t vapic);
701 #endif
703 #if defined(__s390__)
704 int kvm_s390_initial_reset(kvm_context_t kvm, int slot);
705 int kvm_s390_interrupt(kvm_context_t kvm, int slot,
706 struct kvm_s390_interrupt *kvmint);
707 int kvm_s390_set_initial_psw(kvm_context_t kvm, int slot, psw_t psw);
708 int kvm_s390_store_status(kvm_context_t kvm, int slot, unsigned long addr);
709 #endif
711 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
713 * \brief Notifies host kernel about a PCI device to be assigned to a guest
715 * Used for PCI device assignment, this function notifies the host
716 * kernel about the assigning of the physical PCI device to a guest.
718 * \param kvm Pointer to the current kvm_context
719 * \param assigned_dev Parameters, like bus, devfn number, etc
721 int kvm_assign_pci_device(kvm_context_t kvm,
722 struct kvm_assigned_pci_dev *assigned_dev);
725 * \brief Assign IRQ for an assigned device
727 * Used for PCI device assignment, this function assigns IRQ numbers for
728 * an physical device and guest IRQ handling.
730 * \param kvm Pointer to the current kvm_context
731 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
733 int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
735 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
737 * \brief Deassign IRQ for an assigned device
739 * Used for PCI device assignment, this function deassigns IRQ numbers
740 * for an assigned device.
742 * \param kvm Pointer to the current kvm_context
743 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
745 int kvm_deassign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
746 #endif
747 #endif
750 * \brief Determines whether destroying memory regions is allowed
752 * KVM before 2.6.29 had a bug when destroying memory regions.
754 * \param kvm Pointer to the current kvm_context
756 int kvm_destroy_memory_region_works(kvm_context_t kvm);
758 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
760 * \brief Notifies host kernel about a PCI device to be deassigned from a guest
762 * Used for hot remove PCI device, this function notifies the host
763 * kernel about the deassigning of the physical PCI device from a guest.
765 * \param kvm Pointer to the current kvm_context
766 * \param assigned_dev Parameters, like bus, devfn number, etc
768 int kvm_deassign_pci_device(kvm_context_t kvm,
769 struct kvm_assigned_pci_dev *assigned_dev);
770 #endif
773 * \brief Checks whether the generic irq routing capability is present
775 * Checks whether kvm can reroute interrupts among the various interrupt
776 * controllers.
778 * \param kvm Pointer to the current kvm_context
780 int kvm_has_gsi_routing(kvm_context_t kvm);
783 * \brief Determines the number of gsis that can be routed
785 * Returns the number of distinct gsis that can be routed by kvm. This is
786 * also the number of distinct routes (if a gsi has two routes, than another
787 * gsi cannot be used...)
789 * \param kvm Pointer to the current kvm_context
791 int kvm_get_gsi_count(kvm_context_t kvm);
794 * \brief Clears the temporary irq routing table
796 * Clears the temporary irq routing table. Nothing is committed to the
797 * running VM.
799 * \param kvm Pointer to the current kvm_context
801 int kvm_clear_gsi_routes(kvm_context_t kvm);
804 * \brief Adds an irq route to the temporary irq routing table
806 * Adds an irq route to the temporary irq routing table. Nothing is
807 * committed to the running VM.
809 * \param kvm Pointer to the current kvm_context
811 int kvm_add_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
814 * \brief Removes an irq route from the temporary irq routing table
816 * Adds an irq route to the temporary irq routing table. Nothing is
817 * committed to the running VM.
819 * \param kvm Pointer to the current kvm_context
821 int kvm_del_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
823 struct kvm_irq_routing_entry;
825 * \brief Adds a routing entry to the temporary irq routing table
827 * Adds a filled routing entry to the temporary irq routing table. Nothing is
828 * committed to the running VM.
830 * \param kvm Pointer to the current kvm_context
832 int kvm_add_routing_entry(kvm_context_t kvm,
833 struct kvm_irq_routing_entry *entry);
836 * \brief Removes a routing from the temporary irq routing table
838 * Remove a routing to the temporary irq routing table. Nothing is
839 * committed to the running VM.
841 * \param kvm Pointer to the current kvm_context
843 int kvm_del_routing_entry(kvm_context_t kvm,
844 struct kvm_irq_routing_entry *entry);
847 * \brief Updates a routing in the temporary irq routing table
849 * Update a routing in the temporary irq routing table
850 * with a new value. entry type and GSI can not be changed.
851 * Nothing is committed to the running VM.
853 * \param kvm Pointer to the current kvm_context
855 int kvm_update_routing_entry(kvm_context_t kvm,
856 struct kvm_irq_routing_entry *entry,
857 struct kvm_irq_routing_entry *newentry);
860 * \brief Commit the temporary irq routing table
862 * Commit the temporary irq routing table to the running VM.
864 * \param kvm Pointer to the current kvm_context
866 int kvm_commit_irq_routes(kvm_context_t kvm);
869 * \brief Get unused GSI number for irq routing table
871 * Get unused GSI number for irq routing table
873 * \param kvm Pointer to the current kvm_context
875 int kvm_get_irq_route_gsi(kvm_context_t kvm);
878 * \brief Create a file descriptor for injecting interrupts
880 * Creates an eventfd based file-descriptor that maps to a specific GSI
881 * in the guest. eventfd compliant signaling (write() from userspace, or
882 * eventfd_signal() from kernelspace) will cause the GSI to inject
883 * itself into the guest at the next available window.
885 * \param kvm Pointer to the current kvm_context
886 * \param gsi GSI to assign to this fd
887 * \param flags reserved, must be zero
889 int kvm_irqfd(kvm_context_t kvm, int gsi, int flags);
891 #ifdef KVM_CAP_DEVICE_MSIX
892 int kvm_assign_set_msix_nr(kvm_context_t kvm,
893 struct kvm_assigned_msix_nr *msix_nr);
894 int kvm_assign_set_msix_entry(kvm_context_t kvm,
895 struct kvm_assigned_msix_entry *entry);
896 #endif
898 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg);
900 #else /* !CONFIG_KVM */
902 typedef struct kvm_context *kvm_context_t;
903 typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
905 struct kvm_pit_state {
908 static inline int kvm_init(int smp_cpus)
910 return 0;
913 #ifndef QEMU_KVM_NO_CPU
915 static inline void kvm_inject_x86_mce(CPUState *cenv, int bank,
916 uint64_t status, uint64_t mcg_status,
917 uint64_t addr, uint64_t misc,
918 int abort_on_error)
920 if (abort_on_error)
921 abort();
924 #endif
926 extern int kvm_allowed;
928 #endif /* !CONFIG_KVM */
931 int kvm_main_loop(void);
932 int kvm_init_ap(void);
933 #ifndef QEMU_KVM_NO_CPU
934 int kvm_vcpu_inited(CPUState *env);
935 void kvm_load_registers(CPUState *env);
936 void kvm_save_registers(CPUState *env);
937 void kvm_load_mpstate(CPUState *env);
938 void kvm_save_mpstate(CPUState *env);
939 int kvm_cpu_exec(CPUState *env);
940 int kvm_insert_breakpoint(CPUState * current_env, target_ulong addr,
941 target_ulong len, int type);
942 int kvm_remove_breakpoint(CPUState * current_env, target_ulong addr,
943 target_ulong len, int type);
944 void kvm_remove_all_breakpoints(CPUState * current_env);
945 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
946 void kvm_apic_init(CPUState *env);
947 /* called from vcpu initialization */
948 void qemu_kvm_load_lapic(CPUState *env);
949 #endif
951 void kvm_hpet_enable_kpit(void);
952 void kvm_hpet_disable_kpit(void);
953 int kvm_set_irq(int irq, int level, int *status);
955 int kvm_physical_memory_set_dirty_tracking(int enable);
956 int kvm_update_dirty_pages_log(void);
958 #ifndef QEMU_KVM_NO_CPU
959 void qemu_kvm_call_with_env(void (*func)(void *), void *data, CPUState *env);
960 void qemu_kvm_cpuid_on_env(CPUState *env);
961 void kvm_inject_interrupt(CPUState *env, int mask);
962 void kvm_update_after_sipi(CPUState *env);
963 void kvm_update_interrupt_request(CPUState *env);
964 #endif
965 void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
966 ram_addr_t phys_offset);
967 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr, unsigned long size,
968 int log, int writable);
970 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
971 unsigned long size);
972 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
973 int log);
974 int kvm_setup_guest_memory(void *area, unsigned long size);
975 int kvm_qemu_create_memory_alias(uint64_t phys_start, uint64_t len,
976 uint64_t target_phys);
977 int kvm_qemu_destroy_memory_alias(uint64_t phys_start);
979 int kvm_arch_qemu_create_context(void);
981 #ifndef QEMU_KVM_NO_CPU
982 void kvm_arch_save_regs(CPUState *env);
983 void kvm_arch_load_regs(CPUState *env);
984 void kvm_arch_load_mpstate(CPUState *env);
985 void kvm_arch_save_mpstate(CPUState *env);
986 int kvm_arch_init_vcpu(CPUState *cenv);
987 void kvm_arch_pre_kvm_run(void *opaque, CPUState *env);
988 void kvm_arch_post_kvm_run(void *opaque, CPUState *env);
989 int kvm_arch_has_work(CPUState *env);
990 void kvm_arch_process_irqchip_events(CPUState *env);
991 int kvm_arch_try_push_interrupts(void *opaque);
992 void kvm_arch_push_nmi(void *opaque);
993 void kvm_arch_cpu_reset(CPUState *env);
994 int kvm_set_boot_cpu_id(uint32_t id);
996 struct kvm_guest_debug;
997 struct kvm_debug_exit_arch;
999 struct kvm_sw_breakpoint {
1000 target_ulong pc;
1001 target_ulong saved_insn;
1002 int use_count;
1003 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
1006 QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
1008 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
1009 int kvm_sw_breakpoints_active(CPUState *env);
1010 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
1011 target_ulong pc);
1012 int kvm_arch_insert_sw_breakpoint(CPUState * current_env,
1013 struct kvm_sw_breakpoint *bp);
1014 int kvm_arch_remove_sw_breakpoint(CPUState * current_env,
1015 struct kvm_sw_breakpoint *bp);
1016 int kvm_arch_insert_hw_breakpoint(target_ulong addr, target_ulong len,
1017 int type);
1018 int kvm_arch_remove_hw_breakpoint(target_ulong addr, target_ulong len,
1019 int type);
1020 void kvm_arch_remove_all_hw_breakpoints(void);
1021 void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
1023 #endif
1025 void qemu_kvm_aio_wait_start(void);
1026 void qemu_kvm_aio_wait(void);
1027 void qemu_kvm_aio_wait_end(void);
1029 void qemu_kvm_notify_work(void);
1031 #ifndef QEMU_KVM_NO_CPU
1032 void kvm_tpr_opt_setup(void);
1033 void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
1034 void kvm_tpr_vcpu_start(CPUState *env);
1035 #endif
1037 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf);
1038 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1039 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1041 int kvm_arch_init_irq_routing(void);
1043 int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t * data, int len);
1044 int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t * data, int len);
1046 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1047 struct ioperm_data;
1049 void kvm_ioperm(CPUState *env, void *data);
1050 void kvm_add_ioperm_data(struct ioperm_data *data);
1051 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num);
1052 void kvm_arch_do_ioperm(void *_data);
1053 #endif
1055 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
1056 #ifndef QEMU_KVM_NO_CPU
1057 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
1058 #endif
1060 #ifdef CONFIG_KVM
1061 #include "qemu-queue.h"
1063 extern int kvm_allowed;
1064 extern int kvm_irqchip;
1065 extern int kvm_pit;
1066 extern int kvm_pit_reinject;
1067 extern int kvm_nested;
1068 extern kvm_context_t kvm_context;
1070 struct ioperm_data {
1071 unsigned long start_port;
1072 unsigned long num;
1073 int turn_on;
1074 QLIST_ENTRY(ioperm_data) entries;
1077 void qemu_kvm_cpu_stop(CPUState *env);
1078 int kvm_arch_halt(kvm_vcpu_context_t vcpu);
1079 int handle_tpr_access(void *opaque, kvm_vcpu_context_t vcpu, uint64_t rip,
1080 int is_write);
1081 int kvm_has_sync_mmu(void);
1083 #define kvm_enabled() (kvm_allowed)
1084 #define qemu_kvm_pit_in_kernel() kvm_pit_in_kernel(kvm_context)
1085 #define qemu_kvm_has_gsi_routing() kvm_has_gsi_routing(kvm_context)
1086 #ifdef TARGET_I386
1087 #define qemu_kvm_has_pit_state2() kvm_has_pit_state2(kvm_context)
1088 #endif
1089 void kvm_init_vcpu(CPUState *env);
1090 void kvm_load_tsc(CPUState *env);
1091 #else
1092 #define kvm_has_sync_mmu() (0)
1093 #define kvm_enabled() (0)
1094 #define kvm_nested 0
1095 #define qemu_kvm_pit_in_kernel() (0)
1096 #define qemu_kvm_has_gsi_routing() (0)
1097 #ifndef QEMU_KVM_NO_CPU
1098 #ifdef TARGET_I386
1099 #define qemu_kvm_has_pit_state2() (0)
1100 #endif
1101 #define kvm_load_registers(env) do {} while(0)
1102 #define kvm_save_registers(env) do {} while(0)
1103 #define qemu_kvm_cpu_stop(env) do {} while(0)
1104 static inline void kvm_init_vcpu(CPUState *env)
1108 static inline void kvm_load_tsc(CPUState *env)
1111 #endif
1112 #endif
1114 void kvm_mutex_unlock(void);
1115 void kvm_mutex_lock(void);
1117 static inline void qemu_mutex_unlock_iothread(void)
1119 if (kvm_enabled())
1120 kvm_mutex_unlock();
1123 static inline void qemu_mutex_lock_iothread(void)
1125 if (kvm_enabled())
1126 kvm_mutex_lock();
1129 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
1130 target_phys_addr_t end_addr);
1132 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len);
1133 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len);
1136 static inline int kvm_sync_vcpus(void)
1138 return 0;
1141 #ifndef QEMU_KVM_NO_CPU
1142 void kvm_arch_get_registers(CPUState *env);
1144 static inline void kvm_arch_put_registers(CPUState *env)
1146 kvm_load_registers(env);
1147 kvm_load_mpstate(env);
1150 void kvm_cpu_synchronize_state(CPUState *env);
1152 static inline void cpu_synchronize_state(CPUState *env)
1154 if (kvm_enabled()) {
1155 kvm_cpu_synchronize_state(env);
1159 uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
1160 int reg);
1163 #endif
1165 static inline int kvm_set_migration_log(int enable)
1167 return kvm_physical_memory_set_dirty_tracking(enable);
1171 int kvm_irqchip_in_kernel(void);
1172 #ifdef CONFIG_KVM
1174 typedef struct KVMSlot {
1175 target_phys_addr_t start_addr;
1176 ram_addr_t memory_size;
1177 ram_addr_t phys_offset;
1178 int slot;
1179 int flags;
1180 } KVMSlot;
1182 typedef struct kvm_dirty_log KVMDirtyLog;
1184 typedef struct KVMState {
1185 KVMSlot slots[32];
1186 int fd;
1187 int vmfd;
1188 int coalesced_mmio;
1189 int broken_set_mem_region;
1190 int migration_log;
1191 #ifdef KVM_CAP_SET_GUEST_DEBUG
1192 QTAILQ_HEAD(, kvm_sw_breakpoint) kvm_sw_breakpoints;
1193 #endif
1194 int irqchip_in_kernel;
1196 struct kvm_context kvm_context;
1197 } KVMState;
1199 extern KVMState *kvm_state;
1201 int kvm_ioctl(KVMState *s, int type, ...);
1202 int kvm_vm_ioctl(KVMState *s, int type, ...);
1203 int kvm_check_extension(KVMState *s, unsigned int ext);
1205 #endif
1207 #endif