Merge commit '731c54f86988d3f28268f184fabfe9b2a32fb5d3' into upstream-merge
[qemu-kvm/amd-iommu.git] / qemu-kvm.h
blob4523e258631f73f17920952659285e4188e79632
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>
17 #ifdef CONFIG_KVM
19 #if defined(__s390__)
20 #include <asm/ptrace.h>
21 #endif
23 #include <stdint.h>
25 #ifndef __user
26 #define __user /* temporary, until installed via make headers_install */
27 #endif
29 #include <linux/kvm.h>
31 #include <signal.h>
33 /* FIXME: share this number with kvm */
34 /* FIXME: or dynamically alloc/realloc regions */
35 #ifdef __s390__
36 #define KVM_MAX_NUM_MEM_REGIONS 1u
37 #define MAX_VCPUS 64
38 #define LIBKVM_S390_ORIGIN (0UL)
39 #elif defined(__ia64__)
40 #define KVM_MAX_NUM_MEM_REGIONS 32u
41 #define MAX_VCPUS 256
42 #else
43 #define KVM_MAX_NUM_MEM_REGIONS 32u
44 #define MAX_VCPUS 16
45 #endif
47 /* kvm abi verison variable */
48 extern int kvm_abi;
50 /**
51 * \brief The KVM context
53 * The verbose KVM context
56 struct kvm_context {
57 void *opaque;
58 /// is dirty pages logging enabled for all regions or not
59 int dirty_pages_log_all;
60 /// do not create in-kernel irqchip if set
61 int no_irqchip_creation;
62 /// in-kernel irqchip status
63 int irqchip_in_kernel;
64 /// ioctl to use to inject interrupts
65 int irqchip_inject_ioctl;
66 /// do not create in-kernel pit if set
67 int no_pit_creation;
68 /// in-kernel pit status
69 int pit_in_kernel;
70 #ifdef KVM_CAP_IRQ_ROUTING
71 struct kvm_irq_routing *irq_routes;
72 int nr_allocated_irq_routes;
73 #endif
74 void *used_gsi_bitmap;
75 int max_gsi;
78 struct kvm_vcpu_context {
79 int fd;
80 struct kvm_run *run;
81 struct kvm_context *kvm;
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);
96 int kvm_arch_run(kvm_vcpu_context_t vcpu);
99 void kvm_show_code(kvm_vcpu_context_t vcpu);
101 int handle_halt(kvm_vcpu_context_t vcpu);
103 #ifndef QEMU_KVM_NO_CPU
105 int handle_shutdown(kvm_context_t kvm, CPUState *env);
106 void post_kvm_run(kvm_context_t kvm, CPUState *env);
107 int pre_kvm_run(kvm_context_t kvm, CPUState *env);
108 int handle_io_window(kvm_context_t kvm);
109 int handle_debug(kvm_vcpu_context_t vcpu, void *env);
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(kvm_vcpu_context_t vcpu);
223 * \brief Get the value of the APIC_BASE msr as of last exit to userspace
225 * This gets the APIC_BASE msr as it was on the last exit to userspace.
227 * \param kvm Pointer to the current kvm_context
228 * \param vcpu Which virtual CPU should get dumped
229 * \return APIC_BASE msr contents
231 uint64_t kvm_get_apic_base(kvm_vcpu_context_t vcpu);
234 * \brief Check if a vcpu is ready for interrupt injection
236 * This checks if vcpu interrupts are not masked by mov ss or sti.
238 * \param kvm Pointer to the current kvm_context
239 * \param vcpu Which virtual CPU should get dumped
240 * \return boolean indicating interrupt injection readiness
242 int kvm_is_ready_for_interrupt_injection(kvm_vcpu_context_t vcpu);
245 * \brief Read VCPU registers
247 * This gets the GP registers from the VCPU and outputs them
248 * into a kvm_regs structure
250 * \note This function returns a \b copy of the VCPUs registers.\n
251 * If you wish to modify the VCPUs GP registers, you should call kvm_set_regs()
253 * \param kvm Pointer to the current kvm_context
254 * \param vcpu Which virtual CPU should get dumped
255 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
256 * registers values
257 * \return 0 on success
259 int kvm_get_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
262 * \brief Write VCPU registers
264 * This sets the GP registers on the VCPU from a kvm_regs structure
266 * \note When this function returns, the regs pointer and the data it points to
267 * can be discarded
268 * \param kvm Pointer to the current kvm_context
269 * \param vcpu Which virtual CPU should get dumped
270 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
271 * registers values
272 * \return 0 on success
274 int kvm_set_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
276 * \brief Read VCPU fpu registers
278 * This gets the FPU registers from the VCPU and outputs them
279 * into a kvm_fpu structure
281 * \note This function returns a \b copy of the VCPUs registers.\n
282 * If you wish to modify the VCPU FPU registers, you should call kvm_set_fpu()
284 * \param kvm Pointer to the current kvm_context
285 * \param vcpu Which virtual CPU should get dumped
286 * \param fpu Pointer to a kvm_fpu which will be populated with the VCPUs
287 * fpu registers values
288 * \return 0 on success
290 int kvm_get_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
293 * \brief Write VCPU fpu registers
295 * This sets the FPU registers on the VCPU from a kvm_fpu structure
297 * \note When this function returns, the fpu pointer and the data it points to
298 * can be discarded
299 * \param kvm Pointer to the current kvm_context
300 * \param vcpu Which virtual CPU should get dumped
301 * \param fpu Pointer to a kvm_fpu which holds the new vcpu fpu state
302 * \return 0 on success
304 int kvm_set_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
307 * \brief Read VCPU system registers
309 * This gets the non-GP registers from the VCPU and outputs them
310 * into a kvm_sregs structure
312 * \note This function returns a \b copy of the VCPUs registers.\n
313 * If you wish to modify the VCPUs non-GP registers, you should call
314 * kvm_set_sregs()
316 * \param kvm Pointer to the current kvm_context
317 * \param vcpu Which virtual CPU should get dumped
318 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
319 * registers values
320 * \return 0 on success
322 int kvm_get_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
325 * \brief Write VCPU system registers
327 * This sets the non-GP registers on the VCPU from a kvm_sregs structure
329 * \note When this function returns, the regs pointer and the data it points to
330 * can be discarded
331 * \param kvm Pointer to the current kvm_context
332 * \param vcpu Which virtual CPU should get dumped
333 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
334 * registers values
335 * \return 0 on success
337 int kvm_set_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
339 #ifdef KVM_CAP_MP_STATE
341 * * \brief Read VCPU MP state
344 int kvm_get_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
347 * * \brief Write VCPU MP state
350 int kvm_set_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
352 * * \brief Reset VCPU MP state
355 static inline int kvm_reset_mpstate(kvm_vcpu_context_t vcpu)
357 struct kvm_mp_state mp_state = {.mp_state = KVM_MP_STATE_UNINITIALIZED
359 return kvm_set_mpstate(vcpu, &mp_state);
361 #endif
364 * \brief Simulate an external vectored interrupt
366 * This allows you to simulate an external vectored interrupt.
368 * \param kvm Pointer to the current kvm_context
369 * \param vcpu Which virtual CPU should get dumped
370 * \param irq Vector number
371 * \return 0 on success
373 int kvm_inject_irq(kvm_vcpu_context_t vcpu, unsigned irq);
375 #ifdef KVM_CAP_SET_GUEST_DEBUG
376 int kvm_set_guest_debug(kvm_vcpu_context_t, struct kvm_guest_debug *dbg);
377 #endif
379 #if defined(__i386__) || defined(__x86_64__)
381 * \brief Setup a vcpu's cpuid instruction emulation
383 * Set up a table of cpuid function to cpuid outputs.\n
385 * \param kvm Pointer to the current kvm_context
386 * \param vcpu Which virtual CPU should be initialized
387 * \param nent number of entries to be installed
388 * \param entries cpuid function entries table
389 * \return 0 on success, or -errno on error
391 int kvm_setup_cpuid(kvm_vcpu_context_t vcpu, int nent,
392 struct kvm_cpuid_entry *entries);
395 * \brief Setup a vcpu's cpuid instruction emulation
397 * Set up a table of cpuid function to cpuid outputs.
398 * This call replaces the older kvm_setup_cpuid interface by adding a few
399 * parameters to support cpuid functions that have sub-leaf values.
401 * \param kvm Pointer to the current kvm_context
402 * \param vcpu Which virtual CPU should be initialized
403 * \param nent number of entries to be installed
404 * \param entries cpuid function entries table
405 * \return 0 on success, or -errno on error
407 int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu, int nent,
408 struct kvm_cpuid_entry2 *entries);
411 * \brief Setting the number of shadow pages to be allocated to the vm
413 * \param kvm pointer to kvm_context
414 * \param nrshadow_pages number of pages to be allocated
416 int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages);
419 * \brief Getting the number of shadow pages that are allocated to the vm
421 * \param kvm pointer to kvm_context
422 * \param nrshadow_pages number of pages to be allocated
424 int kvm_get_shadow_pages(kvm_context_t kvm, unsigned int *nrshadow_pages);
427 * \brief Set up cr8 for next time the vcpu is executed
429 * This is a fast setter for cr8, which will be applied when the
430 * vcpu next enters guest mode.
432 * \param kvm Pointer to the current kvm_context
433 * \param vcpu Which virtual CPU should get dumped
434 * \param cr8 next cr8 value
436 void kvm_set_cr8(kvm_vcpu_context_t vcpu, uint64_t cr8);
439 * \brief Get cr8 for sync tpr in qemu apic emulation
441 * This is a getter for cr8, which used to sync with the tpr in qemu
442 * apic emualtion.
444 * \param kvm Pointer to the current kvm_context
445 * \param vcpu Which virtual CPU should get dumped
447 __u64 kvm_get_cr8(kvm_vcpu_context_t vcpu);
448 #endif
451 * \brief Set a vcpu's signal mask for guest mode
453 * A vcpu can have different signals blocked in guest mode and user mode.
454 * This allows guest execution to be interrupted on a signal, without requiring
455 * that the signal be delivered to a signal handler (the signal can be
456 * dequeued using sigwait(2).
458 * \param kvm Pointer to the current kvm_context
459 * \param vcpu Which virtual CPU should be initialized
460 * \param sigset signal mask for guest mode
461 * \return 0 on success, or -errno on error
463 int kvm_set_signal_mask(kvm_vcpu_context_t vcpu, const sigset_t *sigset);
466 * \brief Dump VCPU registers
468 * This dumps some of the information that KVM has about a virtual CPU, namely:
469 * - GP Registers
471 * A much more verbose version of this is available as kvm_dump_vcpu()
473 * \param kvm Pointer to the current kvm_context
474 * \param vcpu Which virtual CPU should get dumped
475 * \return 0 on success
477 void kvm_show_regs(kvm_vcpu_context_t vcpu);
480 void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start,
481 unsigned long len, int log, int writable);
482 void kvm_destroy_phys_mem(kvm_context_t, unsigned long phys_start,
483 unsigned long len);
484 void kvm_unregister_memory_area(kvm_context_t, uint64_t phys_start,
485 unsigned long len);
487 int kvm_is_containing_region(kvm_context_t kvm, unsigned long phys_start,
488 unsigned long size);
489 int kvm_register_phys_mem(kvm_context_t kvm, unsigned long phys_start,
490 void *userspace_addr, unsigned long len, int log);
491 int kvm_get_dirty_pages(kvm_context_t, unsigned long phys_addr, void *buf);
492 int kvm_get_dirty_pages_range(kvm_context_t kvm, unsigned long phys_addr,
493 unsigned long end_addr, void *opaque,
494 int (*cb)(unsigned long start,
495 unsigned long len, void *bitmap,
496 void *opaque));
497 int kvm_register_coalesced_mmio(kvm_context_t kvm, uint64_t addr,
498 uint32_t size);
499 int kvm_unregister_coalesced_mmio(kvm_context_t kvm, uint64_t addr,
500 uint32_t size);
503 * \brief Create a memory alias
505 * Aliases a portion of physical memory to another portion. If the guest
506 * accesses the alias region, it will behave exactly as if it accessed
507 * the target memory.
509 int kvm_create_memory_alias(kvm_context_t, uint64_t phys_start, uint64_t len,
510 uint64_t target_phys);
513 * \brief Destroy a memory alias
515 * Removes an alias created with kvm_create_memory_alias().
517 int kvm_destroy_memory_alias(kvm_context_t, uint64_t phys_start);
520 * \brief Get a bitmap of guest ram pages which are allocated to the guest.
522 * \param kvm Pointer to the current kvm_context
523 * \param phys_addr Memory slot phys addr
524 * \param bitmap Long aligned address of a big enough bitmap (one bit per page)
526 int kvm_get_mem_map(kvm_context_t kvm, unsigned long phys_addr, void *bitmap);
527 int kvm_get_mem_map_range(kvm_context_t kvm, unsigned long phys_addr,
528 unsigned long len, void *buf, void *opaque,
529 int (*cb)(unsigned long start,
530 unsigned long len, void *bitmap,
531 void *opaque));
532 int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status);
534 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm, uint64_t phys_start,
535 uint64_t len);
536 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm, uint64_t phys_start,
537 uint64_t len);
539 * \brief Enable dirty-pages-logging for all memory regions
541 * \param kvm Pointer to the current kvm_context
543 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm);
546 * \brief Disable dirty-page-logging for some memory regions
548 * Disable dirty-pages-logging for those memory regions that were
549 * created with dirty-page-logging disabled.
551 * \param kvm Pointer to the current kvm_context
553 int kvm_dirty_pages_log_reset(kvm_context_t kvm);
556 * \brief Query whether in kernel irqchip is used
558 * \param kvm Pointer to the current kvm_context
560 int kvm_irqchip_in_kernel(kvm_context_t kvm);
562 #ifdef KVM_CAP_IRQCHIP
564 * \brief Dump in kernel IRQCHIP contents
566 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
567 * and IOAPIC into a kvm_irqchip structure
569 * \param kvm Pointer to the current kvm_context
570 * \param chip The irq chip device to be dumped
572 int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
575 * \brief Set in kernel IRQCHIP contents
577 * Write one of the in kernel irq chip devices, including PIC (master/slave)
578 * and IOAPIC
581 * \param kvm Pointer to the current kvm_context
582 * \param chip THe irq chip device to be written
584 int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
586 #if defined(__i386__) || defined(__x86_64__)
588 * \brief Get in kernel local APIC for vcpu
590 * Save the local apic state including the timer of a virtual CPU
592 * \param kvm Pointer to the current kvm_context
593 * \param vcpu Which virtual CPU should be accessed
594 * \param s Local apic state of the specific virtual CPU
596 int kvm_get_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
599 * \brief Set in kernel local APIC for vcpu
601 * Restore the local apic state including the timer of a virtual CPU
603 * \param kvm Pointer to the current kvm_context
604 * \param vcpu Which virtual CPU should be accessed
605 * \param s Local apic state of the specific virtual CPU
607 int kvm_set_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
609 #endif
612 * \brief Simulate an NMI
614 * This allows you to simulate a non-maskable interrupt.
616 * \param kvm Pointer to the current kvm_context
617 * \param vcpu Which virtual CPU should get dumped
618 * \return 0 on success
620 int kvm_inject_nmi(kvm_vcpu_context_t vcpu);
622 #endif
625 * \brief Simulate an x86 MCE
627 * This allows you to simulate a x86 MCE.
629 * \param cenv Which virtual CPU should get MCE injected
630 * \param bank Bank number
631 * \param status MSR_MCI_STATUS
632 * \param mcg_status MSR_MCG_STATUS
633 * \param addr MSR_MCI_ADDR
634 * \param misc MSR_MCI_MISC
635 * \param abort_on_error abort on error
637 void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
638 uint64_t mcg_status, uint64_t addr, uint64_t misc,
639 int abort_on_error);
642 * \brief Query wheather in kernel pit is used
644 * \param kvm Pointer to the current kvm_context
646 int kvm_pit_in_kernel(kvm_context_t kvm);
649 * \brief Initialize coalesced MMIO
651 * Check for coalesced MMIO capability and store in context
653 * \param kvm Pointer to the current kvm_context
655 int kvm_init_coalesced_mmio(kvm_context_t kvm);
657 #ifdef KVM_CAP_PIT
659 #if defined(__i386__) || defined(__x86_64__)
661 * \brief Get in kernel PIT of the virtual domain
663 * Save the PIT state.
665 * \param kvm Pointer to the current kvm_context
666 * \param s PIT state of the virtual domain
668 int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s);
671 * \brief Set in kernel PIT of the virtual domain
673 * Restore the PIT state.
674 * Timer would be retriggerred after restored.
676 * \param kvm Pointer to the current kvm_context
677 * \param s PIT state of the virtual domain
679 int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s);
681 int kvm_reinject_control(kvm_context_t kvm, int pit_reinject);
683 #ifdef KVM_CAP_PIT_STATE2
685 * \brief Check for kvm support of kvm_pit_state2
687 * \param kvm Pointer to the current kvm_context
688 * \return 0 on success
690 int kvm_has_pit_state2(kvm_context_t kvm);
693 * \brief Set in kernel PIT state2 of the virtual domain
696 * \param kvm Pointer to the current kvm_context
697 * \param ps2 PIT state2 of the virtual domain
698 * \return 0 on success
700 int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
703 * \brief Get in kernel PIT state2 of the virtual domain
706 * \param kvm Pointer to the current kvm_context
707 * \param ps2 PIT state2 of the virtual domain
708 * \return 0 on success
710 int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
712 #endif
713 #endif
714 #endif
716 #ifdef KVM_CAP_VAPIC
719 * \brief Enable kernel tpr access reporting
721 * When tpr access reporting is enabled, the kernel will call the
722 * ->tpr_access() callback every time the guest vcpu accesses the tpr.
724 * \param kvm Pointer to the current kvm_context
725 * \param vcpu vcpu to enable tpr access reporting on
727 int kvm_enable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
730 * \brief Disable kernel tpr access reporting
732 * Undoes the effect of kvm_enable_tpr_access_reporting().
734 * \param kvm Pointer to the current kvm_context
735 * \param vcpu vcpu to disable tpr access reporting on
737 int kvm_disable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
739 int kvm_enable_vapic(kvm_vcpu_context_t vcpu, uint64_t vapic);
741 #endif
743 #if defined(__s390__)
744 int kvm_s390_initial_reset(kvm_context_t kvm, int slot);
745 int kvm_s390_interrupt(kvm_context_t kvm, int slot,
746 struct kvm_s390_interrupt *kvmint);
747 int kvm_s390_set_initial_psw(kvm_context_t kvm, int slot, psw_t psw);
748 int kvm_s390_store_status(kvm_context_t kvm, int slot, unsigned long addr);
749 #endif
751 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
753 * \brief Notifies host kernel about a PCI device to be assigned to a guest
755 * Used for PCI device assignment, this function notifies the host
756 * kernel about the assigning of the physical PCI device to a guest.
758 * \param kvm Pointer to the current kvm_context
759 * \param assigned_dev Parameters, like bus, devfn number, etc
761 int kvm_assign_pci_device(kvm_context_t kvm,
762 struct kvm_assigned_pci_dev *assigned_dev);
765 * \brief Assign IRQ for an assigned device
767 * Used for PCI device assignment, this function assigns IRQ numbers for
768 * an physical device and guest IRQ handling.
770 * \param kvm Pointer to the current kvm_context
771 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
773 int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
775 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
777 * \brief Deassign IRQ for an assigned device
779 * Used for PCI device assignment, this function deassigns IRQ numbers
780 * for an assigned device.
782 * \param kvm Pointer to the current kvm_context
783 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
785 int kvm_deassign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
786 #endif
787 #endif
790 * \brief Determines whether destroying memory regions is allowed
792 * KVM before 2.6.29 had a bug when destroying memory regions.
794 * \param kvm Pointer to the current kvm_context
796 int kvm_destroy_memory_region_works(kvm_context_t kvm);
798 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
800 * \brief Notifies host kernel about a PCI device to be deassigned from a guest
802 * Used for hot remove PCI device, this function notifies the host
803 * kernel about the deassigning of the physical PCI device from a guest.
805 * \param kvm Pointer to the current kvm_context
806 * \param assigned_dev Parameters, like bus, devfn number, etc
808 int kvm_deassign_pci_device(kvm_context_t kvm,
809 struct kvm_assigned_pci_dev *assigned_dev);
810 #endif
813 * \brief Checks whether the generic irq routing capability is present
815 * Checks whether kvm can reroute interrupts among the various interrupt
816 * controllers.
818 * \param kvm Pointer to the current kvm_context
820 int kvm_has_gsi_routing(kvm_context_t kvm);
823 * \brief Determines the number of gsis that can be routed
825 * Returns the number of distinct gsis that can be routed by kvm. This is
826 * also the number of distinct routes (if a gsi has two routes, than another
827 * gsi cannot be used...)
829 * \param kvm Pointer to the current kvm_context
831 int kvm_get_gsi_count(kvm_context_t kvm);
834 * \brief Clears the temporary irq routing table
836 * Clears the temporary irq routing table. Nothing is committed to the
837 * running VM.
839 * \param kvm Pointer to the current kvm_context
841 int kvm_clear_gsi_routes(kvm_context_t kvm);
844 * \brief Adds an irq route to the temporary irq routing table
846 * Adds an irq route to the temporary irq routing table. Nothing is
847 * committed to the running VM.
849 * \param kvm Pointer to the current kvm_context
851 int kvm_add_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
854 * \brief Removes an irq route from the temporary irq routing table
856 * Adds an irq route to the temporary irq routing table. Nothing is
857 * committed to the running VM.
859 * \param kvm Pointer to the current kvm_context
861 int kvm_del_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
863 struct kvm_irq_routing_entry;
865 * \brief Adds a routing entry to the temporary irq routing table
867 * Adds a filled routing entry to the temporary irq routing table. Nothing is
868 * committed to the running VM.
870 * \param kvm Pointer to the current kvm_context
872 int kvm_add_routing_entry(kvm_context_t kvm,
873 struct kvm_irq_routing_entry *entry);
876 * \brief Removes a routing from the temporary irq routing table
878 * Remove a routing to the temporary irq routing table. Nothing is
879 * committed to the running VM.
881 * \param kvm Pointer to the current kvm_context
883 int kvm_del_routing_entry(kvm_context_t kvm,
884 struct kvm_irq_routing_entry *entry);
887 * \brief Updates a routing in the temporary irq routing table
889 * Update a routing in the temporary irq routing table
890 * with a new value. entry type and GSI can not be changed.
891 * Nothing is committed to the running VM.
893 * \param kvm Pointer to the current kvm_context
895 int kvm_update_routing_entry(kvm_context_t kvm,
896 struct kvm_irq_routing_entry *entry,
897 struct kvm_irq_routing_entry *newentry);
900 * \brief Commit the temporary irq routing table
902 * Commit the temporary irq routing table to the running VM.
904 * \param kvm Pointer to the current kvm_context
906 int kvm_commit_irq_routes(kvm_context_t kvm);
909 * \brief Get unused GSI number for irq routing table
911 * Get unused GSI number for irq routing table
913 * \param kvm Pointer to the current kvm_context
915 int kvm_get_irq_route_gsi(kvm_context_t kvm);
918 * \brief Create a file descriptor for injecting interrupts
920 * Creates an eventfd based file-descriptor that maps to a specific GSI
921 * in the guest. eventfd compliant signaling (write() from userspace, or
922 * eventfd_signal() from kernelspace) will cause the GSI to inject
923 * itself into the guest at the next available window.
925 * \param kvm Pointer to the current kvm_context
926 * \param gsi GSI to assign to this fd
927 * \param flags reserved, must be zero
929 int kvm_irqfd(kvm_context_t kvm, int gsi, int flags);
931 #ifdef KVM_CAP_DEVICE_MSIX
932 int kvm_assign_set_msix_nr(kvm_context_t kvm,
933 struct kvm_assigned_msix_nr *msix_nr);
934 int kvm_assign_set_msix_entry(kvm_context_t kvm,
935 struct kvm_assigned_msix_entry *entry);
936 #endif
938 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg);
940 #else /* !CONFIG_KVM */
942 typedef struct kvm_context *kvm_context_t;
943 typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
945 struct kvm_pit_state {
948 static inline int kvm_init(int smp_cpus)
950 return 0;
953 #ifndef QEMU_KVM_NO_CPU
955 static inline void kvm_inject_x86_mce(CPUState *cenv, int bank,
956 uint64_t status, uint64_t mcg_status,
957 uint64_t addr, uint64_t misc,
958 int abort_on_error)
960 if (abort_on_error)
961 abort();
964 #endif
966 extern int kvm_allowed;
968 #endif /* !CONFIG_KVM */
971 int kvm_main_loop(void);
972 int kvm_init_ap(void);
973 #ifndef QEMU_KVM_NO_CPU
974 int kvm_vcpu_inited(CPUState *env);
975 void kvm_load_registers(CPUState *env);
976 void kvm_save_registers(CPUState *env);
977 void kvm_load_mpstate(CPUState *env);
978 void kvm_save_mpstate(CPUState *env);
979 int kvm_cpu_exec(CPUState *env);
980 int kvm_insert_breakpoint(CPUState * current_env, target_ulong addr,
981 target_ulong len, int type);
982 int kvm_remove_breakpoint(CPUState * current_env, target_ulong addr,
983 target_ulong len, int type);
984 void kvm_remove_all_breakpoints(CPUState * current_env);
985 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
986 void kvm_apic_init(CPUState *env);
987 /* called from vcpu initialization */
988 void qemu_kvm_load_lapic(CPUState *env);
989 #endif
991 void kvm_hpet_enable_kpit(void);
992 void kvm_hpet_disable_kpit(void);
993 int kvm_set_irq(int irq, int level, int *status);
995 int kvm_physical_memory_set_dirty_tracking(int enable);
996 int kvm_update_dirty_pages_log(void);
998 #ifndef QEMU_KVM_NO_CPU
999 void qemu_kvm_call_with_env(void (*func)(void *), void *data, CPUState *env);
1000 void qemu_kvm_cpuid_on_env(CPUState *env);
1001 void kvm_inject_interrupt(CPUState *env, int mask);
1002 void kvm_update_after_sipi(CPUState *env);
1003 void kvm_update_interrupt_request(CPUState *env);
1004 #endif
1005 void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
1006 ram_addr_t phys_offset);
1007 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr, unsigned long size,
1008 int log, int writable);
1010 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1011 unsigned long size);
1012 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
1013 int log);
1014 int kvm_setup_guest_memory(void *area, unsigned long size);
1015 int kvm_qemu_create_memory_alias(uint64_t phys_start, uint64_t len,
1016 uint64_t target_phys);
1017 int kvm_qemu_destroy_memory_alias(uint64_t phys_start);
1019 int kvm_arch_qemu_create_context(void);
1021 #ifndef QEMU_KVM_NO_CPU
1022 void kvm_arch_save_regs(CPUState *env);
1023 void kvm_arch_load_regs(CPUState *env);
1024 void kvm_arch_load_mpstate(CPUState *env);
1025 void kvm_arch_save_mpstate(CPUState *env);
1026 int kvm_arch_init_vcpu(CPUState *cenv);
1027 void kvm_arch_pre_kvm_run(void *opaque, CPUState *env);
1028 void kvm_arch_post_kvm_run(void *opaque, CPUState *env);
1029 int kvm_arch_has_work(CPUState *env);
1030 void kvm_arch_process_irqchip_events(CPUState *env);
1031 int kvm_arch_try_push_interrupts(void *opaque);
1032 void kvm_arch_push_nmi(void *opaque);
1033 void kvm_arch_cpu_reset(CPUState *env);
1034 int kvm_set_boot_cpu_id(uint32_t id);
1036 struct kvm_guest_debug;
1037 struct kvm_debug_exit_arch;
1039 struct kvm_sw_breakpoint {
1040 target_ulong pc;
1041 target_ulong saved_insn;
1042 int use_count;
1043 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
1046 QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
1048 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
1049 int kvm_sw_breakpoints_active(CPUState *env);
1050 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
1051 target_ulong pc);
1052 int kvm_arch_insert_sw_breakpoint(CPUState * current_env,
1053 struct kvm_sw_breakpoint *bp);
1054 int kvm_arch_remove_sw_breakpoint(CPUState * current_env,
1055 struct kvm_sw_breakpoint *bp);
1056 int kvm_arch_insert_hw_breakpoint(target_ulong addr, target_ulong len,
1057 int type);
1058 int kvm_arch_remove_hw_breakpoint(target_ulong addr, target_ulong len,
1059 int type);
1060 void kvm_arch_remove_all_hw_breakpoints(void);
1061 void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
1063 #endif
1065 void qemu_kvm_aio_wait_start(void);
1066 void qemu_kvm_aio_wait(void);
1067 void qemu_kvm_aio_wait_end(void);
1069 void qemu_kvm_notify_work(void);
1071 #ifndef QEMU_KVM_NO_CPU
1072 void kvm_tpr_opt_setup(void);
1073 void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
1074 void kvm_tpr_vcpu_start(CPUState *env);
1075 #endif
1077 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf);
1078 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1079 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1081 int kvm_arch_init_irq_routing(void);
1083 int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t * data, int len);
1084 int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t * data, int len);
1086 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1087 struct ioperm_data;
1089 void kvm_ioperm(CPUState *env, void *data);
1090 void kvm_add_ioperm_data(struct ioperm_data *data);
1091 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num);
1092 void kvm_arch_do_ioperm(void *_data);
1093 #endif
1095 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
1096 #ifndef QEMU_KVM_NO_CPU
1097 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
1098 #endif
1100 #ifdef CONFIG_KVM
1101 #include "qemu-queue.h"
1103 extern int kvm_allowed;
1104 extern int kvm_irqchip;
1105 extern int kvm_pit;
1106 extern int kvm_pit_reinject;
1107 extern int kvm_nested;
1108 extern kvm_context_t kvm_context;
1110 struct ioperm_data {
1111 unsigned long start_port;
1112 unsigned long num;
1113 int turn_on;
1114 QLIST_ENTRY(ioperm_data) entries;
1117 void qemu_kvm_cpu_stop(CPUState *env);
1118 int kvm_arch_halt(void *opaque, kvm_vcpu_context_t vcpu);
1119 int handle_tpr_access(void *opaque, kvm_vcpu_context_t vcpu, uint64_t rip,
1120 int is_write);
1121 int kvm_has_sync_mmu(void);
1123 #define kvm_enabled() (kvm_allowed)
1124 #define qemu_kvm_irqchip_in_kernel() kvm_irqchip_in_kernel(kvm_context)
1125 #define qemu_kvm_pit_in_kernel() kvm_pit_in_kernel(kvm_context)
1126 #define qemu_kvm_has_gsi_routing() kvm_has_gsi_routing(kvm_context)
1127 #ifdef TARGET_I386
1128 #define qemu_kvm_has_pit_state2() kvm_has_pit_state2(kvm_context)
1129 #endif
1130 void kvm_init_vcpu(CPUState *env);
1131 void kvm_load_tsc(CPUState *env);
1132 #else
1133 #define kvm_has_sync_mmu() (0)
1134 #define kvm_enabled() (0)
1135 #define kvm_nested 0
1136 #define qemu_kvm_irqchip_in_kernel() (0)
1137 #define qemu_kvm_pit_in_kernel() (0)
1138 #define qemu_kvm_has_gsi_routing() (0)
1139 #ifndef QEMU_KVM_NO_CPU
1140 #ifdef TARGET_I386
1141 #define qemu_kvm_has_pit_state2() (0)
1142 #endif
1143 #define kvm_load_registers(env) do {} while(0)
1144 #define kvm_save_registers(env) do {} while(0)
1145 #define qemu_kvm_cpu_stop(env) do {} while(0)
1146 static inline void kvm_init_vcpu(CPUState *env)
1150 static inline void kvm_load_tsc(CPUState *env)
1153 #endif
1154 #endif
1156 void kvm_mutex_unlock(void);
1157 void kvm_mutex_lock(void);
1159 static inline void qemu_mutex_unlock_iothread(void)
1161 if (kvm_enabled())
1162 kvm_mutex_unlock();
1165 static inline void qemu_mutex_lock_iothread(void)
1167 if (kvm_enabled())
1168 kvm_mutex_lock();
1171 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
1172 target_phys_addr_t end_addr);
1174 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len);
1175 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len);
1178 static inline int kvm_sync_vcpus(void)
1180 return 0;
1183 #ifndef QEMU_KVM_NO_CPU
1184 void kvm_arch_get_registers(CPUState *env);
1186 static inline void kvm_arch_put_registers(CPUState *env)
1188 kvm_load_registers(env);
1189 kvm_load_mpstate(env);
1192 void kvm_cpu_synchronize_state(CPUState *env);
1194 static inline void cpu_synchronize_state(CPUState *env)
1196 if (kvm_enabled()) {
1197 kvm_cpu_synchronize_state(env);
1201 uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
1202 int reg);
1205 #endif
1207 static inline int kvm_set_migration_log(int enable)
1209 return kvm_physical_memory_set_dirty_tracking(enable);
1212 #ifdef CONFIG_KVM
1214 typedef struct KVMSlot {
1215 target_phys_addr_t start_addr;
1216 ram_addr_t memory_size;
1217 ram_addr_t phys_offset;
1218 int slot;
1219 int flags;
1220 } KVMSlot;
1222 typedef struct kvm_dirty_log KVMDirtyLog;
1224 typedef struct KVMState {
1225 KVMSlot slots[32];
1226 int fd;
1227 int vmfd;
1228 int coalesced_mmio;
1229 int broken_set_mem_region;
1230 int migration_log;
1231 #ifdef KVM_CAP_SET_GUEST_DEBUG
1232 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
1233 #endif
1234 struct kvm_context kvm_context;
1235 } KVMState;
1237 extern KVMState *kvm_state;
1239 int kvm_ioctl(KVMState *s, int type, ...);
1240 int kvm_vm_ioctl(KVMState *s, int type, ...);
1241 int kvm_check_extension(KVMState *s, unsigned int ext);
1243 #endif
1245 #endif